Advertisement
adfasdfadsfasdf

Untitled

Nov 15th, 2023 (edited)
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.14 KB | None | 0 0
  1. ```cpp
  2. #include <array>
  3. #include <cstdint>
  4. #include <cstddef>
  5. #include <iostream>
  6. #include <vector>
  7.  
  8. using namespace std;
  9.  
  10. const string lend = "\n";
  11. const string sep = " ";
  12.  
  13. using u8 = uint8_t;
  14. using u16 = uint16_t;
  15. using u32 = uint32_t;
  16. using u64 = uint64_t;
  17.  
  18. using i8 = int8_t;
  19. using i16 = int16_t;
  20. using i32 = int32_t;
  21. using i64 = int64_t;
  22.  
  23. using f32 = float;
  24. using f64 = double;
  25.  
  26. using usize = size_t;
  27.  
  28. int main() {
  29.     ios_base::sync_with_stdio(0);
  30.     cin.tie(0); cout.tie(0);
  31.  
  32.     i32 n, q;
  33.     cin >> n >> q;
  34.     vector<vector<i32>> tree_ranges(4 * n + 1, vector<i32>(2, 0));
  35.     vector<i32> tree(4 * n + 1, 0);
  36.     vector<i32> nums(n, 0);
  37.     for (i32 i = 0; i < n; i += 1) {
  38.         cin >> nums[i];
  39.     }
  40.  
  41.     auto init = [&](auto& self, auto idx, auto left, auto right) {
  42.         tree_ranges[idx] = {left, right};
  43.         if (left == right) {
  44.             tree[idx] = nums[left];
  45.             return;
  46.         }
  47.         i32 mid = left + (right - left) / 2;
  48.         self(self, 2 * idx, left, mid);
  49.         self(self, 2 * idx + 1, mid + 1, right);
  50.         tree[idx] = tree[2 * idx] + tree[2 * idx + 1];
  51.     };
  52.  
  53.     auto update = [&](auto& self, auto tree_idx, auto nums_idx, auto value) {
  54.         if (tree_ranges[tree_idx][0] == tree_ranges[tree_idx][1]) {
  55.             if (nums_idx == tree_ranges[tree_idx][0]) {
  56.                 tree[tree_idx] = value;
  57.             }
  58.             return;
  59.         }
  60.         self(self, 2 * tree_idx, nums_idx, value);
  61.         self(self, 2 * tree_idx + 1, nums_idx, value);
  62.         tree[tree_idx] -= nums[nums_idx];
  63.         tree[tree_idx] += value;
  64.     };
  65.  
  66.     auto update_root = [&](auto nums_idx, auto value) {
  67.         update(update, 1, nums_idx, value);
  68.         nums[nums_idx] = value;
  69.     };
  70.  
  71.     auto query = [&](auto& self, auto tree_idx, auto left, auto right) {
  72.         if (left > right) return 0;
  73.         auto tleft = tree_ranges[tree_idx][0];
  74.         auto tright = tree_ranges[tree_idx][1];
  75.         if (left == tleft and tright == right) {
  76.             return tree[tree_idx];
  77.         }
  78.         if (right < tleft) return 0;
  79.         if (left > tright) return 0;
  80.  
  81.         auto left_node_rb = tree_ranges[tree_idx * 2][1];
  82.         auto right_node_lb = tree_ranges[tree_idx * 2 + 1][0];
  83.         i32 left_ans =  self(self, tree_idx * 2,     left,          left_node_rb);
  84.         i32 right_ans = self(self, tree_idx * 2 + 1, right_node_lb, right       );
  85.         return left_ans + right_ans;
  86.     };
  87.  
  88.     auto query_root = [&](auto left, auto right) {
  89.         return query(query, 1, left, right);
  90.     };
  91.  
  92.     init(init, 1, 0, n - 1);
  93.  
  94.     /* for (i32 idx = 0; idx < 2 * n + 1; idx += 1) { */
  95.     /*     cout << idx << sep << tree_ranges[idx][0] << ":" << tree_ranges[idx][1] << sep << tree[idx] << lend; */
  96.     /* } */
  97.  
  98.     /* cout << query_root(1, 4) << lend; */
  99.  
  100.     for (i32 qidx = 0; qidx < q; q += 1) {
  101.         i32 type, val1, val2;
  102.         cin >> type >> val1 >> val2;
  103.         if (type == 1) {
  104.             update_root(val1 - 1, val2);
  105.         } else {
  106.             cout << query_root(val1 - 1, val2 - 1) << lend;
  107.         }
  108.     }
  109.     return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement