比赛 线段数树状数组 评测结果 AAAAAAAAAAAAAAA
题目名称 数列操作A 最终得分 100
用户昵称 忆轩 运行时间 6.567 s
代码语言 C++ 内存使用 6.42 MiB
提交时间 2018-06-17 10:39:22
显示代码纯文本
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#define LL long long
#define N 100005
using namespace std;
LL n, m, x, y;
string s;
struct nond {
	int ll, rr;
	LL sum;
}tree[4*N];
void up(int now) {
	tree[now].sum = tree[now*2].sum + tree[now*2+1].sum;
}
void build(int now, int l, int r) {
	tree[now].ll = l; tree[now].rr = r;
	if(l == r) {
		scanf("%lld", &tree[now].sum);
		return ;
	}
	int mid = (l+r) / 2;
	build(now*2, l, mid);
	build(now*2+1, mid+1, r);
	up(now);
}
void change(int now, int x, int y) {
	if(tree[now].ll == tree[now].rr) {
		tree[now].sum += y;
		return ;
	}
	int mid = (tree[now].ll+tree[now].rr) / 2;
	if(x <= mid) change(now*2, x, y);
	else change(now*2+1, x, y);
	up(now);
}
LL query(int now, int l, int r) {
	if(tree[now].ll==l && tree[now].rr==r) return tree[now].sum;
	int mid = (tree[now].ll+tree[now].rr) / 2;
	if(l<=mid && mid<r) return query(now*2, l, mid) + query(now*2+1, mid+1, r);
	else if(r<=mid) return query(now*2, l, r);
	else return query(now*2+1, l, r);
}
int main() {
	freopen("shulie.in","r",stdin);
	freopen("shulie.out","w",stdout);
	scanf("%lld", &n);
	build(1, 1, n);
	scanf("%lld", &m);
	for(int i = 1; i <= m; i++) {
		cin >> s >> x >> y;
		if(s[0] == 'A') change(1, x, y);
		else printf("%lld\n", query(1, x, y)); 
	}
	fclose(stdin); fclose(stdout);
	return 0;
}