zyx_dzpd @ 2024-08-14 16:02:55
改代码会关注
#include<bits/stdc++.h>
using namespace std;
long long tree[1000],va[1000];
void build(long long l,long long r,long long i){//建树
if(l==r) tree[i]=va[r];
else{
long long mid=(r+l)/2;
build(l,mid,2*i);
build(mid-1,r,2*i+1);
tree[i]=tree[2*i]+tree[i*2+1];
}
}
void date(long long l,long long r,long long i,long long id,long long val){//改单点
if(l==r) tree[i]+=val;
else{
int mid=l+(r-l)/2;
if(id<=mid) date(l,mid,i*2,id,val);
else date(mid+1,r,i*2+1,id,val);
tree[i]=tree[i*2]+tree[i*2+1];
}
}
int cq(long long l,long long r,long long i,long long cl,long long cr){//查询
if(r<=cl||l>=cr) return 0;
if(l>=cl&&r<=cr) return tree[i];
int mid=l+(r-l)/2;
int s=0;
if(cl<=mid) s+=cq(l,mid,i*2,cl,cr);
if(cr>mid) s+cq(mid+1,r,i*2+1,cl,cr);
return s;
}
int main(){
int n,m,i,j,k,x,y;
cin>>n>>m;
for(long long i=1;i<=n;i++)
scanf("%lld",&va[i]);
build(1,n,1);
for(i=1;i<=n;i++) cin>>va[i];
for(i=1;i<=m;i++){
scanf("%lld",&j);
switch(j){
case 1:{
scanf("%lld%lld%lld",&x,&y,&k);
for(i=x;i<=y;i++){
date(1,n,1,i,k);
}
break;
}
case 2:{
scanf("%lld%lld",&x,&y);
cout<<cq(1,n,1,x,y);
break;
}
}
}
return 0;
}
by YotsubaNakano @ 2024-08-14 16:16:50
不写懒惰标记可以交这个https://www.luogu.com.cn/problem/P3374
by I_like_play_eggy @ 2024-08-14 16:17:40
省流:通过标记 Lazy-tag 进行区间延迟修改
by I_like_play_eggy @ 2024-08-14 16:20:10
额,帖主哪去了,不会不知道我们在说啥吧(
by zyx_dzpd @ 2024-08-14 16:22:27
@I_like_play_eggy @YotsubaNakano 所以怎么改
by InfiniteRobin @ 2024-08-14 16:28:45
@zyx_dzpd 不会懒标记?点这里!
by I_like_play_eggy @ 2024-08-14 16:31:59
@zyx_dzpd 建议:
int
和 long long
混用,可以写 #define int long long
;