为何线段树/分块无法通过树状数组的题目?

学术版

@[WydnksqhbD](/user/1139577) 说明你写挂了
by murder_drones @ 2024-03-18 22:48:43


@[murder_drones](/user/610393) (不是
by WydnksqhbD @ 2024-03-18 22:49:13


写挂了呗。。。
by Fractured_Angel @ 2024-03-18 22:49:49


@[murder_drones](/user/610393) 通过了 P3372 的线段树(
by WydnksqhbD @ 2024-03-18 22:50:10


@[WydnksqhbD](/user/1139577) 有记录或者代码吗,以及题目。
by murder_drones @ 2024-03-18 22:50:56


贴个代码: ```cpp #include<bits/stdc++.h> #define int long long using namespace std; const int N=1e5+5; int n,m; int a[N],tree[N*4],change[N*4]; void pushup(int p) { tree[p]=tree[p*2]+tree[p*2+1]; } void pushdown(int len,int p) { change[p*2]+=change[p]; change[p*2+1]+=change[p]; tree[p*2]+=change[p]*(len-len/2); tree[p*2+1]+=change[p]*(len/2); change[p]=0; } void build(int l=1,int r=n,int p=1) { if(l==r)tree[p]=a[l]; else { int mid=(l+r)/2; build(l,mid,p*2); build(mid+1,r,p*2+1); pushup(p); } } void update(int l,int r,int k,int cl=1,int cr=n,int p=1) { if(cl>r||cr<l)return; if(cl>=l&&cr<=r)tree[p]+=(cr-cl+1)*k,change[p]+=k; else { int mid=(cl+cr)/2; pushdown(cr-cl+1,p); update(l,r,k,cl,mid,p*2); update(l,r,k,mid+1,cr,p*2+1); pushup(p); } } int query(int l,int r,int cl=1,int cr=n,int p=1) { if(cl>r||cr<l)return 0; if(cl>=l&&cr<=r)return tree[p]; int mid=(cl+cr)/2; pushdown(cr-cl+1,p); return query(l,r,cl,mid,p*2)+query(l,r,mid+1,cr,p*2+1); } signed main() { cin>>n>>m; for(int i=1;i<=n;i++)cin>>a[i]; build(); while(m--) { int op,l,r; cin>>op>>l>>r; if(op==1) { int k; cin>>k; update(l,r,k); } else { cout<<query(l,r)<<endl; } } return 0; } ```
by WydnksqhbD @ 2024-03-18 22:51:12


@[WydnksqhbD](/user/1139577) 哪一题/yiw
by murder_drones @ 2024-03-18 22:55:02


@[murder_drones](/user/610393) P3372 呀(((
by WydnksqhbD @ 2024-03-18 22:55:57


@[WydnksqhbD](/user/1139577) 3372不就是线段树过得嘛。。。
by shin_chan_jiang @ 2024-03-18 22:57:18


@[WydnksqhbD](/user/1139577) 我问的是树状数组的题/xk
by murder_drones @ 2024-03-18 22:57:23


| 下一页