1369-answering Queries
|
PDF (中文版) |
Statistics |
Forum |
Time Limit:3 second (s) |
Memory limit:32 MB |
The problem need to solve are pretty simple. Give a function f (A, N), where a is an array of integers and n is the number of element s in the array. f (A, N) is defined as follows:
Long long f (int a[], int n) { //n = size of A
A long long sum = 0;
for (int i = 0; i < n; i++)
for (int j = i + 1; j < N; j + +)
Sum + = A[i]-a[j];
return sum;
}
Given the array A and an integer n, and some queries of the form:
1) 0 x V (0≤x < n, 0≤v≤106), meaning, and the value of a[x] to v.
2) 1, meaning that there is to find F as described above.
Input
Input starts with an integer T (≤5), denoting the number of test cases.
Each case starts with a line containing the integers: n and Q (1 ≤n, q≤105). The next line contains n space separated integers between 0 and 106 denoting the array a as described above.
Each of the next Q lines contains one query as described above.
Output
For each case, print the case number in a single line first. Then for each query-type "1" print one single line containing the value of f (A, N).
Sample Input |
Output for Sample Input |
1 3 5 1 2 3 1 0 0 3 1 0 2 1 1 |
Case 1: -4 0 4 |
Note
The Dataset is huge, and the use faster I/O methods.
Problem Setter:hasnain Heickal jamispecial thanks:jane ALAM Jan Idea: Derivation of the formula on the line: sum= (n-2*i+1) *bns[i]; and then change it, just change it. Minus the original plus the present;
1#include <stdio.h>2#include <algorithm>3#include <iostream>4#include <string.h>5#include <math.h>6#include <stdlib.h>7typedefLong LongLL;8LL bns[200000];9 intMainvoid)Ten { One intK; A inti,j; -scanf"%d",&k); - ints; the intp,q; -LL ans=0; - for(s=1; s<=k; s++) - { +scanf"%d%d",&p,&q); - for(i=1; i<=p; i++) + { Ascanf"%lld",&bns[i]); at } -ans=0; - for(i=1; i<=p; i++) - { -ans+= (LL) (P-2*i+1)*(LL) bns[i]; - } inprintf"Case %d:\n", s); - while(q--) to { + intask; - intn,m; thescanf"%d",&ask); * if(ask==1) $ {Panax Notoginsengprintf"%lld\n", ans); - } the Else + { Ascanf"%d%d",&n,&m); the + { -ans-= (LL) (P-2* (n+1)+1) *bns[n+1]; $ans+= (LL) (P-2* (n+1)+1)*(LL) m; $bns[n+1]=m; - } - } the } - }Wuyi return 0; the}
1369-answering Queries