您的位置:首页 > 产品设计 > UI/UE

1369 - Answering Queries (线段树的单点更新)

2015-12-05 16:58 417 查看
[align=center]1369 - Answering Queries [/align]



PDF (English)
Statistics
Forum
Time Limit: 3 second(s)Memory Limit: 32 MB
The problem you need to solve here is pretty simple. You aregive a function
f(A, n)
, where A is an array of integers and nis the number of elements in the array.
f(A, n) is defined as follows:

long long f( int A[], int n ) { //
n = size ofA
   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 aninteger n, and some queries of the form:

1)     0 x v (0 ≤ x < n, 0 ≤ v ≤ 106),meaning that you have to change the value of
A[x] to v.

2)     1, meaning that you have to find f asdescribed above.

Input

Input starts with an integer T (≤ 5),denoting the number of test cases.

Each case starts with a line containing two integers: nand
q (1 ≤ n, q ≤ 105)
.The next line contains n space separated integers between
0 and 106denoting the array
A
as described above.

Each of the next q lines contains one query as describedabove.

Output

For each case, print the case number in a single line first.Then for each query-type
"1" print one single line containingthe 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

Dataset is huge, use faster I/O methods.

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <string>
#include <stdlib.h>
#include <memory.h>

#define REP(i,a,b) for(int i=(a);i<(b);++i)
#define RREP(i,b,a) for(int i =(b);i >= (a); --i)
#define CLR(a,x) memset(a,x,sizeof(a))
#define ALL(o) o.begin(),o.end()
#define PB push_back

using namespace std;
typedef long long ll;

template<class T>
void read(T & x) {
char ch = getchar();
bool sign = false;
x = 0;
while (ch < '0' || ch > '9') {
if (ch == '-') sign = true;
ch = getchar();
}
while ('0' <= ch && ch <= '9') {
x = 10 * x + ch - '0';
ch = getchar();
}
if (sign) x = -x;
}

template<class T>
void print(T x) {
if (x > 9) print(x / 10);
putchar('0' + (x % 10));
}

template<class T>
void println(T x) {
print(x);
puts("");
}

template<class T>
inline T sqr(T a) {
return a * a;
}

struct Fract {
Fract(ll a = 0, ll b = 1)
:fenzi(a), fenmu(b) {
sim();
}

Fract(const Fract & b) {
fenzi = b.fenzi;
fenmu = b.fenmu;
}

Fract & operator = (const Fract & b) {
fenzi = b.fenzi;
fenmu = b.fenmu;
return *this;
}

void sim() {
ll g = __gcd(abs(fenzi), abs(fenmu));
if (g > 1) {
fenzi /= g;
fenmu /= g;
}
if (fenmu < 0) {
fenzi = -fenzi;
fenmu = -fenmu;
}
}

Fract & operator += (const Fract & b) {
fenzi = fenzi * b.fenmu + b.fenzi * fenmu;
fenmu *= b.fenmu;
sim();
return *this;
}

Fract & operator -= (const Fract & b) {
fenzi = fenzi * b.fenmu - b.fenzi * fenmu;
fenmu *= b.fenmu;
sim();
return *this;
}

Fract & operator *= (const Fract & b) {
fenzi *= b.fenzi;
fenmu *= b.fenmu;
sim();
return *this;
}

Fract & operator /= (const Fract & b) {
fenzi *= b.fenmu;
fenmu *= b.fenzi;
sim();
return *this;
}

Fract operator + (const Fract & b) const {
Fract result = *this;
result += b;
return result;
}

Fract operator - (const Fract & b) const {
Fract result = *this;
result -= b;
return result;
}

Fract operator * (const Fract & b) const {
Fract result = *this;
result *= b;
return result;
}

Fract operator / (const Fract & b) const {
Fract result = *this;
result /= b;
return result;
}
ll fenzi, fenmu;
};
//#===============================================
/**
*
*
*
*/
const int N = (int)(1e5 + 10);

struct SGTree {
int num[N << 2];
ll sum[N << 2],ans[N << 2];
void pushup(int u) {
int ls = u << 1,rs = u << 1 | 1;
sum[u] = sum[ls] + sum[rs];
ans[u] = ans[ls] + ans[rs];
num[u] = num[ls] + num[rs];
ans[u] += sum[rs] * num[ls] - sum[ls] * num[rs];
}
void build(int u,int l,int r) {
if(l == r) {
scanf("%lld",&sum[u]);
ans[u] = 0;
num[u] = 1;
} else {
int mid = (l + r) >> 1;
build(u << 1, l, mid);
build(u << 1 | 1, mid + 1, r);
pushup(u);
}
}
void update(int u,int l,int r,int x,int val) {
if(l == r) {
sum[u] = val;
ans[u] = 0;
num[u] = 1;
} else {
int mid = (l + r) >> 1;
if(x <= mid) update(u << 1, l, mid, x, val);
else update(u << 1 | 1, mid + 1, r, x, val);
pushup(u);
}
}
} sgTree;

int main() {
//freopen("in.txt","r",stdin);
int T,cnt = 0;
cin>>T;
while(T--) {
int n,q,x;
scanf("%d %d",&n,&q);
sgTree.build(1, 1, n);
printf("Case %d:\n",++cnt);
while(q--) {
scanf("%d",&x);
if(x == 1) {
printf("%lld\n",-sgTree.ans[1]);
} else {
int v;
scanf("%d %d",&x,&v);
x++;
sgTree.update(1, 1, n, x, v);
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据结构 线段树