Advertisement
Araf_12

Square_Root_Decomposition

Apr 17th, 2025
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. int main() {
  6.     int n, ara[1000], block[1000] = {}, block_size;
  7.     cin >> n;
  8.     block_size = sqrt(n);
  9.    
  10.     // Input array elements
  11.     for (int i = 0; i < n; i++) {
  12.         cin >> ara[i];
  13.     }
  14.    
  15.     // Calculate the blocks
  16.     for (int i = 0; i < n; i++) {
  17.         block[i / block_size] += ara[i];
  18.     }
  19.    
  20.     cout << "block size: " << block_size << endl;
  21.    
  22.     int q;
  23.     cin >> q;
  24.    
  25.     while (q--) {
  26.         int op, x, y;
  27.         cin >> op >> x >> y;
  28.        
  29.         if (op == 1) {
  30.             // Update operation
  31.             ara[x] += y;
  32.             block[x / block_size] += y;
  33.         } else {
  34.             // Query operation (range sum)
  35.             int sum = 0;
  36.             for (int i = x; i <= y; ) {
  37.                 if (i % block_size == 0 && i + block_size - 1 <= y) {
  38.                     sum += block[i / block_size];
  39.                     i += block_size;
  40.                 } else {
  41.                     sum += ara[i];
  42.                     i++;
  43.                 }
  44.             }
  45.             cout << sum << endl;
  46.         }
  47.     }
  48.    
  49.     return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement