记录编号 600109 评测结果 AAAAAAAAAAAAAAA
题目名称 数列操作A 最终得分 100
用户昵称 Gravatar李奇文 是否通过 通过
代码语言 C++ 运行时间 4.049 s
提交时间 2025-04-15 21:32:57 内存使用 8.93 MiB
显示代码纯文本
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int Maxn=5e5+5;
ll a[Maxn*2];
struct tree {
	ll l,r,tb,lazy;
} f[Maxn*4];
ll n,m;
void build(ll p,ll l,ll r) { //建树
	f[p].l=l,f[p].r=r;
	if(l==r) {
		f[p].tb=a[l];
		return;
	}
	ll mid=(l+r)/2;
	build(p*2,l,mid);
	build(p*2+1,mid+1,r);
	f[p].tb=f[p*2].tb+f[p*2+1].tb;
	return;
}
void addpt(ll p,ll x,ll k) { //单点修改add	
	if(f[p].l==f[p].r) {
		f[p].tb+=k;
		return;
	}
	ll mid=(f[p].l+f[p].r)/2;
	if(x<=mid) {
		addpt(p*2,x,k);
	} else {
		addpt(p*2+1,x,k);
	}
	f[p].tb=f[p*2].tb+f[p*2+1].tb;
	return;
} 
void pushlazy(ll p){//懒惰
	if(!f[p].lazy){
		return;
	}else{
		ll lp=p*2,rp=p*2+1;
		f[lp].lazy+=f[p].lazy;
		f[rp].lazy+=f[p].lazy;
		ll mid=(f[p].l+f[p].r)>>1;
		f[lp].tb+=(mid-f[p].l+1)*f[p].lazy;
		f[rp].tb+=(f[p].r-mid)*f[p].lazy;
		f[p].lazy=0;
	}
	return;
}
ll ptt(ll p,ll x,ll y) {//区间查询sum
	if(x<=f[p].l&&f[p].r<=y) {
		return f[p].tb;
	}
	pushlazy(p);
	ll mid=(f[p].l+f[p].r)/2,sum=0;
	if(x<=mid) {
		sum+=ptt(p*2,x,y);
	}
	if(mid<y) {
		sum+=ptt(p*2+1,x,y);
	}
	return sum;
}
void change(ll p,ll l,ll r,ll k){//区间修改add
	if(l<=f[p].l&&f[p].r<=r){
		f[p].lazy+=k;
		f[p].tb+=(f[p].r-f[p].l+1)*k;
		return;
	}else{
		pushlazy(p);
		ll mid=(f[p].l+f[p].r)/2;
		if(l<=mid) change(p*2,l,r,k);
		if(mid<r) change(p*2+1,l,r,k);
		f[p].tb=f[p*2].tb+f[p*2+1].tb;
	}
	return;
}
int main() {
	freopen("shulie.in","r",stdin);
	freopen("shulie.out","w",stdout);
	std::cin>>n;
	for(ll i=1; i<=n; i++) {
		std::cin>>a[i];
	}
	std::cin>>m;
	build(1,1,n);
	for(ll i=1; i<=m; i++) {
		ll ssy,x,y,k;
		string sy;
		std::cin>>sy>>x>>y;
		if(sy=="ADD") {
			addpt(1,x,y);
			//std::cin>>k;
			//change(1,x,y,k);
		} else {
			std::cout<<ptt(1,x,y)<<endl;
		}
	}
	return 0;
}