您的位置:首页 > 其它

poj3468 A Simple Problem with Integers 线段树区间更新

2016-09-30 21:46 375 查看
A Simple Problem with Integers

Time Limit: 5000MSMemory Limit: 131072K
Total Submissions: 97722Accepted: 30543
Case Time Limit: 2000MS
Description

You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.

Input

The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.

Output

You need to answer all Q commands in order. One answer in a line.

Sample Input

10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4

Sample Output

4
55
9
15

Hint

The sums may exceed the range of 32-bit integers.

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <cstring>
#define FOR(i, a, b)  for(int i = (a); i <= (b); i++)
#define RE(i, n) FOR(i, 1, n)
#define FORP(i, a, b) for(int i = (a); i >= (b); i--)
#define REP(i, n) for(int i = 0; i <(n); ++i)
#define SZ(x) ((int)(x).size )
#define ALL(x) (x).begin(), (x.end())
#define MSET(a, x) memset(a, x, sizeof(a))
using namespace std;

typedef long long int ll;
typedef pair<int, int> P;
int read() {
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9') {
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9') {
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
const double pi=3.14159265358979323846264338327950288L;
const double eps=1e-6;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int MAXN = 100003;
const int xi[] = {0, 0, 1, -1};
const int yi[] = {1, -1, 0, 0};

int N, T;
struct seg {
ll sum, add;
} a[MAXN<<2];
ll b[MAXN];

void pushdown(int k, int m){
if(a[k].add){
a[k<<1].add += a[k].add;
a[k<<1|1].add += a[k].add;
a[k<<1].sum += a[k].add*(m - (m>>1));
a[k<<1|1].sum += a[k].add*(m>>1);
a[k].add = 0;
}
}
void build(int k, int l, int r) {
a[k].add = 0;
if(r == l) {
a[k].sum = b[l];
return;
}
int m = (l+r)>>1;
build(k<<1, l, m);
build(k<<1|1, m+1, r);
a[k].sum = a[k<<1].sum + a[k<<1|1].sum;
}
void update(int k, int x, int add, int l, int r) {
if(l == r ) {
a[k].sum += add;
return;
}
int m = (l + r)>>1;
if(x <= m) {
update(k<<1, x, add, l, m);
} else update(k<<1|1, x, add, m+1, r);
a[k].sum = a[k<<1].sum + a[k<<1|1].sum;
}
void change(int k, int l, int r, int ca, int cb, ll c) {
if(ca <= l && cb >= r){
a[k].add += c;
a[k].sum += c*(r - l + 1);
return;
}
pushdown(k, r-l+1);
int m = (l+r) >>1;
if(ca <= m) change(k<<1, l, m, ca, cb, c);
if(cb > m) change(k <<1|1, m+1, r, ca, cb, c);
a[k].sum = a[k<<1].sum + a[k<<1|1].sum;
}
ll query(int k, int l, int r, int qa, int qb) {
if(qa <= l && qb >= r) return a[k].sum;
pushdown(k, r-l+1);
int m = (l+r) >>1;
ll ans = 0;
if(qa <= m) ans += query(k<<1, l, m, qa, qb);
if(qb > m) ans += query(k<<1|1, m+1, r, qa, qb);
return ans;
}

int main() {
//freopen("in.txt", "r", stdin);
int n, Q;
scanf("%d%d", &n, &Q);
for(int i = 1; i <= n; i++) {
scanf("%I64d", &b[i]);
}
build(1, 1, n);
char s[10];
int ca, cb;
while(Q--) {
scanf("%s%d%d", s, &ca, &cb);
if(s[0] == 'Q') {
printf("%I64d\n", query(1, 1, n, ca, cb));
}
else {
ll add;
scanf("%I64d", &add);
//            printf("SSS\n");
change(1, 1, n, ca, cb, add);
//            printf("SSS\n");
}
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: