萌新刚学OI,求救树状数组

P3374 【模板】树状数组 1

大佬们都好强啊,改完之后还是回去学线段树好了。。。lxl说树状数组先背下来就好了。。。
by mushroom_knight @ 2020-05-31 09:41:50


@[幽竹烟雨](/user/162203) 哦是这样才对 ```cpp int find(int x){ int ans=0; for(;x;x-=lowbit(x)){ ans+=t[x]; } return ans; } ```
by 幽竹烟雨 @ 2020-05-31 09:42:07


@[Ace_Translator](/user/338786) 我谔谔 ~~原来lxl的bit是背的,了解了~~
by Chthollytxdy @ 2020-05-31 09:42:52


@[Ace_Translator](/user/338786) 改好啦 ```cpp #include<bits/stdc++.h> using namespace std; int t[600001]; int a[500001]; int n,m; int lowbit(int i){ return i&-i; } void modify(int x,int y){ for(;x<=n;x+=lowbit(x)){ t[x]+=y; } } int find(int x){ int ans=0; for(;x;x-=lowbit(x)){ ans+=t[x]; } return ans; } void init(){ for(register int i=1;i<=n;++i){ modify(i,a[i]); } } int cmd,x,y; int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>n>>m; for(register int i=1;i<=n;++i){ cin>>a[i]; } init(); for(register int i=1;i<=m;++i){ cin>>cmd>>x>>y; if(cmd==1){ modify(x,y); } else{ cout<<find(y)-find(x-1)<<'\n'; } } return 0; } ```
by 幽竹烟雨 @ 2020-05-31 09:43:05


然后modify也挂了
by SamariumPhosphide @ 2020-05-31 09:43:11


>@[Brian_WBY](/user/297433) #Q,找到问题了
by mushroom_knight @ 2020-05-31 09:44:03


@[Ace_Translator](/user/338786) 还是不熟,建议自己再多打几遍
by 幽竹烟雨 @ 2020-05-31 09:44:04


@[Chthollytxdy](/user/338442) lxl说的是我这种初学者先背下来。。。
by mushroom_knight @ 2020-05-31 09:44:49


@[幽竹烟雨](/user/162203) #Q
by mushroom_knight @ 2020-05-31 09:45:10


bushi您这种一看就知道是初入DS的 ~~一入DS深似海,从此AC是路人~~
by Chthollytxdy @ 2020-05-31 09:48:49


上一页 | 下一页