Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- using namespace std;
- int s[111111],n,q;
- void modify(int pos, int value){ //прибавить value в позицию pos
- for (;pos<=n;pos|=pos-1,++pos) s[pos]+=value;
- }
- int findsum(int l, int r){ //запрос суммы от l до r
- int res=0,p;
- for (p=r;p>0;p&=p-1) res+=s[p];
- for (p=l-1;p>0;p&=p-1) res-=s[p];
- return res;
- }
- int main(){
- // тут тупо для тестирования
- cin>>n>>q; //вводим размер массива и число запросов
- for (int i=1;i<=q;i++){
- int a,b,c;
- cin>>a>>b>>c;
- if (a==1) modify(b,c); // если а=1, то прибавляем с в позицию b
- else cout<<findsum(b,c)<<endl; // иначе выводим сумму от b до с
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment