nrk009

SQRT DECOMP

Aug 8th, 2019
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.92 KB | None | 0 0
  1.  
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4.  
  5. #define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
  6. #define ll long long
  7. #define ld long double
  8. #define pb push_back
  9. #define fe first
  10. #define se second
  11. #define nl "\n"
  12. #define pp pair < ll , ll >
  13. #define sz(x) (ll)x.size()
  14. #define st(x) sort(x.begin(),x.end())
  15. #define rst(x) sort(x.rbegin(), x.rend())
  16. #define all(x) x.begin(),x.end()
  17. long double pi = 3.14159265358979323;
  18.  
  19. const double EPS = 1e-12;
  20. const int N = 2e5 + 1;
  21. const int mod = 1e9 + 7;
  22.  
  23. // square root decomposition
  24. ll a[N],  bsize ;
  25. ll block[N];
  26. // bsize is the size of block
  27. void pre(ll n){
  28.     //we can find the block number using i/bsize
  29.     for(int i = 0 ; i < n; i++ ){
  30.         block[i/bsize] += a[i];
  31.     }
  32. }
  33. void update(ll idx , ll val){
  34.     int id = idx/bsize;
  35.     block[id] += val - a[idx];
  36.     a[idx] = val;
  37. }
  38.  
  39. ll query(ll l ,ll r){
  40.     ll sum = 0 ;
  41.     //for query there are three points
  42.     //1.find the sum of block which starts from l
  43.     //we are taking l < r because in last loop we are considring the case l <= r
  44.     while(l % bsize != 0 && l < r){
  45.         sum += a[l];
  46.         l++;
  47.     }
  48.     //2.find the sum of all the block b/w l and r
  49.     //take the case l starts from 1 and blk size is 3 then l + p -1 will points to 3
  50.     //which is second block
  51.     while(l + bsize - 1 <= r){
  52.         sum += block[l/bsize];
  53.         l += bsize;
  54.     }
  55.     //3.find the sum of block where r ends
  56.  
  57.     while(l <= r){
  58.         sum += a[l];
  59.         l++;
  60.     }
  61.     return sum;
  62.  
  63. }
  64. //time complexity of query will be O(sqrt(n));
  65.  
  66. int main()
  67. {
  68.     fast;
  69.     ll n;
  70.     cin >> n ;
  71.     bsize = (sqrt(n));
  72.    //   cout << bsize << " ";
  73.    
  74.     for(int i = 0 ; i < n; i++ )
  75.         cin >> a[i];
  76.     pre(n);
  77.    
  78.     ll q;
  79.     cin >> q;
  80.     while(q--){
  81.         ll ch;
  82.         cin >> ch ;
  83.         if(ch == 1){
  84.             ll idx , val;
  85.             cin >> idx >> val;
  86.             idx--;
  87.             update(idx ,val);
  88.         }
  89.         else{
  90.             ll a , b;
  91.             cin >> a >> b ;
  92.             a-- , b--;
  93.             cout << query(a,b) << "\n";
  94.         }
  95.     }
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.    
  118.     return 0;
  119.    
  120. }
Advertisement
Add Comment
Please, Sign In to add comment