zhujiajun2013 @ 2024-07-24 13:57:07
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <string>
#include <algorithm>
#include <cstring>
#include <map>
#include <memory>
using namespace std;
const int N=100000;
long long n,t,a[N+5],tr[4*N+5];
void pushup(int rt){
tr[rt]=tr[rt<<1]+tr[rt<<1|1];
}
void build(int l,int r,int rt){
if(l==r){
tr[rt]=a[l];
return ;
}
int mid=(l+r)>>1;
build(l,mid,rt<<1);
build(mid+1,r,rt<<1|1);
pushup(rt);
}
void change(int rt,int l,int r,int x,int y,int k){
if(l==x&&r==y){
tr[rt] += k;
return ;
}
int mid=(l+r)>>1;
if(y<=mid)change(rt<<1,l,mid,x,y,k);
else if(x > mid) change(rt<<1|1,mid+1,r,x,y,k);
else {
change(rt<<1,l,mid,x,mid,k);
change(rt<<1|1,mid+1,r,mid+1,y,k);
}
}
int f3(int l,int r,int rt,int L,int R){
if(L<=l&&r<=R){
return tr[rt];
}
int mid=(l+r)>>1;
if(R<=mid)return f3(l,mid,rt<<1,L,R);
else if(L>mid) return f3(mid+1,r,rt<<1|1,L,R);
else return f3(l,mid,rt<<1,L,mid)+f3(mid+1,r,rt<<1|1,mid+1,R);
}
int main(){
scanf("%d%d",&n,&t);
for(int i=1;i<=n;i++)scanf("%d",a+i);
build(1,n,1);
while(t--){
int opt,x,y;
cin>>opt>>x>>y;
if(opt==1){
int k;
cin>>k;
change(1,1,n,x,y,k);
}
if(opt==2)
cout<<f3(1,n,1,x,y)<<endl;
}
return 0;
}/*
样例输出:
11
6
12
*/
by zhujiajun2013 @ 2024-07-25 12:47:49
过了,此帖结
by lzdqwq @ 2024-07-28 10:05:24
@SBAAAA0 ,我终于调出正解了。
#include<bits/stdc++.h>
using namespace std;
#define lowbit(x) x & -x
#define int long long
int tree[500005];
int n,m;
void add(int x,int num) {
while(x<=n){
tree[x]+=num;
x+=lowbit(x);
}
}
int Answer(int x){
int ans=0;
while(x){
ans+=tree[x];
x-=lowbit(x);
}
return ans;
}
signed main(){
cin>>n>>m;
int last=0,now;
for (int i=1;i<=n;i++){
cin>>now;
add(i,now -last);
last=now;
}
int opt;
while(m--){
cin>>opt;
if(opt==1){
int x,y,k;
cin>>x>>y>>k;
add(x,k);
add(y+1,-k);
}else if(opt == 2){
int x;
cin>>x;
cout<<Answer(x)<<endl;
}
}
return 0;
}