poj 3468 线段树

A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 119121 Accepted: 36996
Case Time Limit: 2000MS

Description

You have N integers, A1A2, ... , 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 A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+1, ... , Ab.

Output

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

裸的线段树,就是注意数据溢出,数组开long long ,不知道不用懒惰标记能不能过,我用了懒惰标记。。。嗯,就是刚开始标记数组忘了开long long  结果WA到怀疑人参(手黄再)。

#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<cmath>
#include<vector>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<stack>
#include<memory.h>
#include<ctype.h>
using namespace std;
typedef long long ll;

const int maxn = 100000 + 5;
int n, m;
ll tree[maxn * 4];
ll add[maxn * 4];//标记数组
void init(int l, int r, int pos) {//建树
	if (l == r) {
		scanf("%I64d", &tree[pos]);
		return;
	}
	int mid = (l + r) / 2;
	init(l, mid, pos << 1);
	init(mid + 1, r, pos << 1 | 1);
	tree[pos] = tree[pos << 1] + tree[pos << 1 | 1];
}
void pushdown(int pos, int ln, int rn) {//下推函数 ln,rn:左右节点数
	if (add[pos]) {
		add[pos << 1] += add[pos];
		add[pos << 1 | 1] += add[pos];
		tree[pos << 1] += ll(add[pos] * ln);//
		tree[pos << 1 | 1] += ll(add[pos] * rn);
		add[pos] = 0;
	}
}
/*
void Push_Down(int root,int length)//length:当前区间长度
{
if(add[root])
{
add[root<<1]+=add[root];
add[root<<1|1]+=add[root];
sum[root<<1]+=add[root]*(length-(length>>1));
sum[root<<1|1]+=add[root]*(length>>1);
add[root]=0;
}
}
*/
void updata(int L, int R, int val, int l, int r, int pos) {
	/*L,R:要操作区间,val:值,l,r:当前区间,pos:当前节点*/
	if (L <= l&&r <= R) {
		tree[pos] += ll(val*(r - l + 1));//注意数据溢出
		add[pos] += val;
		return;
	}
	int m = (l + r) >> 1;
	pushdown(pos, m - l + 1, r - m);//标记下推
	if (L <= m)updata(L, R, val, l, m, pos << 1);
	if (R > m)updata(L, R, val, m + 1, r, pos << 1 | 1);

	tree[pos] = tree[pos << 1] + tree[pos << 1 | 1];

}
ll query(int L, int R, int l, int r, int pos) {
	if (L <= l && r <= R) return tree[pos];

	int mid = (l + r) / 2;
	pushdown(pos, mid - l + 1, r - mid);
	ll ans = 0;
	if (L <= mid)ans += query(L, R, l, mid, pos << 1);
	if (R>mid) ans += query(L, R, mid + 1, r, pos << 1 | 1);

	return ans;
}
int main() {
	cin >> n >> m;
	init(1, n, 1);
	char words[2];
	while (m--) {
		scanf("%s", words);
		if (words[0] == 'Q') {
			int x, y;
			scanf("%d%d", &x, &y);
			printf("%I64d\n", query(x, y, 1, n, 1));
		}
		else {
			int x, y, z;
			scanf("%d%d%d", &x, &y, &z);
			updata(x, y, z, 1, n, 1);
		}

	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值