minimario

<Inverted Segment Tree> TEMPLATE

Nov 26th, 2015
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.44 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ll long long
  5. #define mp make_pair
  6. #define pb push_back
  7. #define pii pair <int, int>
  8. #define vi vector<int>
  9.  
  10. #define read1(a) int a; scanf("%d", &a)
  11. #define read2(a, b) int a, b; scanf("%d %d", &a, &b)
  12. #define read3(a, b, c) int a, b, c; scanf("%d %d %d", &a, &b, &c)
  13.  
  14. #define FOR(i, a, b) for (int i=a; i<b; i++)
  15. #define F0R(i, a) for (int i=0; i<a; i++)
  16.  
  17. #define readgi(n) F0R(i, n) { scanf("%d", &arr[i+1]); }
  18. #define readgs(n) F0R(i, n) { scanf(" %c", &arr[i]); }
  19.  
  20. #define f first
  21. #define s second
  22.  
  23. const int MOD = 1000000007;
  24. const int MAX = 200005;
  25.  
  26. int arr[4*MAX];
  27. int pref[4*MAX];
  28. int tree[4*MAX];
  29.  
  30. void build(int node, int a, int b){
  31.     if(a == b){
  32.     tree[node] = arr[a];
  33.     }
  34.     else{
  35.         build(2*node, a, (a+b)/2);
  36.     build(2*node+1, (a+b)/2+1, b);
  37.     }
  38. }
  39.  
  40. void update(int node, int a, int b, int i, int j, int value) {
  41.     if (a > b || a > j || b < i) {
  42.         return;
  43.     }
  44.  
  45.     if (i <= a && b <= j) {
  46.         tree[node] += value;
  47.         return;
  48.     }
  49.  
  50.     update(2*node, a, (a+b)/2, i, j, value);
  51.     update(2*node+1, (a+b)/2 + 1, b, i, j, value);
  52. }
  53.  
  54. int query(int node, int a, int b, int i) {
  55.     if (a > b || a > i || b < i) {
  56.         return 0;
  57.     }
  58.  
  59.     if (a == b) {
  60.         return tree[node];
  61.     }
  62.  
  63.     int x = query(2*node, a, (a+b)/2, i);
  64.     int y = query(2*node+1, (a+b)/2 + 1, b, i);
  65.  
  66.     return x + y + tree[node];
  67. }
Advertisement
Add Comment
Please, Sign In to add comment