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:04:35
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];
}
by Delete_error @ 2024-08-14 16:04:38
虽然但是你这空间开小了
by I_like_play_eggy @ 2024-08-14 16:07:39
去掉 cq(): if(r<=cl||l>=cr) return 0;
date(): int mid=l+(r-l)/2;
混用 int
和 long long
by InfiniteRobin @ 2024-08-14 16:07:44
你这没有懒标记真的会过吗?
by I_like_play_eggy @ 2024-08-14 16:08:30
《if(cr>mid) s+cq(mid+1,r,i*2+1,cl,cr);
》
by I_like_play_eggy @ 2024-08-14 16:08:44
@zyx_dzpd
by I_like_play_eggy @ 2024-08-14 16:11:26
没有懒标是
by I_like_play_eggy @ 2024-08-14 16:14:24
@zyx_dzpd 建议:
int
和 long long
混用,可以写 #define int long long
;by InfiniteRobin @ 2024-08-14 16:16:05
不会懒标记?点这里!
by InfiniteRobin @ 2024-08-14 16:16:34
@zyx_dzpd