Advertisement
Vince14

/<> 1275 (sqrt decomposition)

Sep 9th, 2023
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.77 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <vector>
  7. #include <set>
  8. #include <map>
  9. #include <stack>
  10. #include <queue>
  11. #include <deque>
  12. #include <unordered_map>
  13. #include <numeric>
  14. #include <iomanip>
  15. using namespace std;
  16. #define pii pair<long long, long long>
  17. #define ll long long
  18. #define FAST ios_base::sync_with_stdio(false); cin.tie(NULL)
  19. const long long dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
  20. const long long dl[2] = {1, -1};
  21. const long long MOD = 1000000007;
  22. const long long MAXN = 1000005;
  23.  
  24.  
  25. int N, Q;
  26. long long arr[MAXN];
  27. int sq;
  28. long long sq_arr[MAXN];
  29.  
  30. void build_sqrt(){
  31.     for(int i = 0; i < N; i++){
  32.         sq_arr[i / sq] += arr[i];
  33.     }
  34. }
  35.  
  36. long long query(int x, int y){
  37.     int xdec = x / sq;
  38.     int ydec = y / sq;
  39.     long long ret = 0;
  40.     if(xdec == ydec){
  41.         for(int i = x; i <= y; i++){
  42.             ret += arr[i];
  43.         }
  44.         return ret;
  45.     }
  46.     for(int i = x; i < (xdec + 1) * sq; i++){
  47.         ret += arr[i];
  48.     }
  49.     for(int i = xdec + 1; i < ydec; i++){
  50.         ret += sq_arr[i];
  51.     }
  52.     for(int i = ydec * sq; i <= y; i++){
  53.         ret += arr[i];
  54.     }
  55.     return ret;
  56. }
  57.  
  58. void update(int a, long long b){
  59.     sq_arr[a / sq] -= arr[a];
  60.     sq_arr[a / sq] += b;
  61.     arr[a] = b;
  62. }
  63.  
  64. int main() {
  65.     FAST;
  66.     cin >> N >> Q;
  67.     for(int i = 0; i < N; i++){
  68.         cin >> arr[i];
  69.     }
  70.     sq = (int) sqrt(N);
  71.     build_sqrt();
  72.     for(int i = 0; i < Q; i++){
  73.         int x, y, a;
  74.         long long b;
  75.         cin >> x >> y >> a >> b;
  76.         x--; y--; a--;
  77.         if(x > y){
  78.             swap(x, y);
  79.         }
  80.         cout << query(x, y) << "\n";
  81.         update(a, b);
  82.     }
  83. }
  84.  
  85. /*
  86. 5 2
  87. 1 2 3 4 5
  88. 2 3 3 1
  89. 3 5 4 1
  90.  */
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement