Advertisement
Guest User

OBI - arranhaceu

a guest
Sep 18th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1.  
  2. #include <iostream>
  3.  
  4. #define MAX 100000
  5.  
  6. using namespace std;
  7.  
  8. int bit[MAX+1], moradores[MAX+1];
  9. int N;
  10.  
  11. void update(int x, int v) {
  12.   while (x <= N){
  13.     bit[x] += v;
  14.     x += x&-x;
  15.   }
  16. }
  17.  
  18. int query(int x) {
  19.   int r = 0;
  20.   while( x ){
  21.     r += bit[x];
  22.     x -= x&-x;
  23.   }
  24.   return r;
  25. }
  26.  
  27. int main()
  28. {
  29.   ios::sync_with_stdio(0);
  30.  
  31.   int Q,i;
  32.   cin >> N >> Q;
  33.  
  34.   for ( i = 1; i <= N; i++ ){
  35.     cin >> moradores[i];
  36.     update( i, moradores[i] );
  37.   }
  38.  
  39.   while ( Q-- > 0 ){
  40.     int e,m,k;
  41.  
  42.     cin >> e;
  43.     if ( e == 0 ){
  44.       cin >> i >> m;
  45.       int d = m - moradores[i];
  46.       moradores[i] = m;
  47.       update( i, d );
  48.     } else {
  49.       cin >> k;
  50.       cout << query(k) << endl;
  51.     }
  52.  
  53.   }
  54.  
  55.   return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement