树状数组20分求调

P3374 【模板】树状数组 1

@[Yaco_Naomi_Jane](/user/520638) 让我想想怎么用整活的方式回答你。。
by GWOI @ 2024-08-09 15:12:33


@[GWOI](/user/1164217) 大佬————(期待ing)
by Yaco_Naomi_Jane @ 2024-08-09 15:15:12


@[Yaco_Naomi_Jane](/user/520638) ```cpp #include<bits/stdc++.h> using namespace std; #define lowbit(x) x&(-x) long long a[50000005]; long long n, m; void add(int x, long long k) { while(x<=n) { a[x]+=k; x+=lowbit(x); } } long long Solve(int x) { if(x==0) return 0; long long ans=0; for(int i=x; i>0; i-=lowbit(i)) ans+=a[i]; return ans; } void coot(int x, int y) { long long ans=Solve(x-1); long long ans2=Solve(y); cout<<(long long)(ans2-ans)<<endl; } void Create(int i) { long long x=lowbit(i); long long xx=x/2, now=i-x; while(xx>0) { a[i]+=a[now+xx]; now=now+xx; xx/=2; } } int main() { long long j, x, y; long long k; long long p; cin>>n>>m; for(int i=1; i<=n; i++) { cin>>p; add(i, p); } while(m--) { cin>>j; if(j==1) { cin>>x>>k; add(x, k); } else { cin>>x>>y; coot(x, y); } } return 0; } ``` 输入的数据不要和 $a$ 数组定义搞混,直接 `add(i, (sth you cin))` 即可。
by GWOI @ 2024-08-09 15:16:34


@[Yaco_Naomi_Jane](/user/520638) 终究没能整活,,,好吧
by GWOI @ 2024-08-09 15:16:56


谢谢大佬(合十
by Yaco_Naomi_Jane @ 2024-08-09 15:19:44


|