POJ 3468 A Simple Problem with Integers

A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 51656 Accepted: 15382
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.

线段树
按照大白的写的,但是一直WA,后来调试改了下才AC了,不知道是不是大白错了?求大神指点下

#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <queue>
#include <set>

using namespace std;

//#define WIN
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#endif

#define SI(a) scanf("%d", &(a))
#define SDI(a, b) scanf("%d%d", &(a), &(b))
#define S64I(a) scanf(iform, &(a))
#define SS(a) scanf("%s", (a))
#define SDS(a, b) scanf("%s%s", (a), (b))
#define SC(a) scanf("%c", &(a))
#define PI(a) printf("%d\n", (a))
#define PS(a) puts(a)
#define P64I(a) printf(oform, (a))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define MSET(a, b) (memset((a), (b), sizeof(a)))
#define Mid(L, R) ((L) + ((R) - (L))/2)
#define Abs(a) ((a) >= 0 ? (a) : -(a))
#define REP(i, n) for(int (i)=0; (i) < (n); (i)++)
#define FOR(i, a, n) for(int (i)=(a); (i) <= (n); (i)++)
const int INF = 0x3f3f3f3f;
const double eps = 10e-9;

const int maxn = 100000 + 20;
const int maxo = maxn * 4;
LL A[maxn];
LL sumv[maxo];
LL addv[maxo];

void build(int o, int L, int R) {
	int M = L + (R - L) / 2;
	if(L == R) {
		sumv[o] = A[L];
	} else {
		if(L <= M) build(o*2, L, M);
		if(M < R) build(o*2+1, M+1, R);
		sumv[o] = sumv[o*2] + sumv[o*2+1];
	}
}

void maintain(int o, int L, int R) {
	int lc = o*2, rc = o*2+1;
	if(R > L) {
		sumv[o] = sumv[lc] + sumv[rc];
	}
	sumv[o] += addv[o] * (R - L +1);
	if(L == R) addv[o] = 0;   //大白没有这句,加上这句AC,不加WA
}

int y1, y2;
LL v;
void update(int o, int L, int R) {
	int lc = o*2, rc = o*2+1;
	if(y1 <= L && y2 >= R) {
		addv[o] += v;
	} else {
		int M = L + (R - L) / 2;
		if(y1 <= M) update(lc, L, M);
		if(y2 > M) update(rc, M+1, R);
	}
	maintain(o, L, R);
}

LL _sum;
void query(int o, int L, int R, LL add) {
	if(y1 <= L && y2 >= R) {
		_sum += sumv[o] + add * (R-L+1);
	} else {
		int M = L + (R - L) / 2;
		if(y1 <= M) query(o*2, L, M, add + addv[o]);
		if(y2 > M) query(o*2+1, M+1, R, add + addv[o]);
	}
}

int main() {
	int n, Q;

	SDI(n, Q);
	REP(i, n) S64I(A[i+1]);
	MSET(addv, 0);
	build(1, 1, n);

	while(Q--) {
		char c = getchar();
		while(c!='Q' && c!='C') c = getchar();
		if(c == 'Q') {
			scanf("%d%d", &y1, &y2);
			_sum = 0;
			query(1, 1, n, 0);
			P64I(_sum);
		} else if(c == 'C') {
			scanf("%d%d", &y1, &y2);
			S64I(v);
			update(1, 1, n);
		}
	}

	return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值