Advertisement
LA77

Untitled

Feb 25th, 2025
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.34 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define Read_Optimizations ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
  5. #define FOR(n) for(int i = 1; i <= n; ++ i)
  6. #define REP(i, n) for(int idx = i; idx <= n; ++ idx)
  7. #define int long long
  8. #define pb push_back
  9. #define pii pair<int, int>
  10. using ll = long long;
  11. using ci = const int;
  12. using cll = const long long;
  13.  
  14. /// INPUT / OUTPUT
  15. const string problem = "aib";
  16. ifstream fin(problem + ".in");
  17. ofstream fout(problem + ".out");
  18. /////////////////////////////////////////////////////////////////////
  19.  
  20. /// STRUCTURES
  21. struct SegTree
  22. {
  23.     int offset;
  24.     vector<int>tree;
  25.  
  26.     inline int next_power_of_two(int x)
  27.     {
  28.         int p = 1;
  29.         while(p <= x) p<<=1;
  30.         return p;
  31.     }
  32.  
  33.     SegTree(int n)
  34.     {
  35.         offset = next_power_of_two(n);
  36.         tree.resize(offset * 2, 0);
  37.     }
  38.  
  39.     inline void update(int pos, int val)
  40.     {
  41.         for(pos = pos + offset - 1; pos > 0; pos >>= 1)
  42.             tree[pos] += val;
  43.     }
  44.  
  45.     inline int _query(int node, int left, int right, int tree_left, int tree_right)
  46.     {
  47.         if(left >= tree_left && right <= tree_right) return tree[node];
  48.  
  49.         if(left > tree_right || right < tree_left) return 0;
  50.  
  51.         int mid = (left + right) >> 1;
  52.         int left_query = _query(node * 2, left, mid, tree_left, tree_right);
  53.         int right_query = _query(node * 2 + 1, mid + 1, right, tree_left, tree_right);
  54.  
  55.         return left_query + right_query;
  56.     }
  57.  
  58.     inline int query(int tree_left, int tree_right)
  59.     {
  60.         return _query(1, 1, offset, tree_left, tree_right);
  61.     }
  62.  
  63.     inline int _binary_search(int node, int left, int right, int tree_left, int x, int &suma)
  64.     {
  65.         if(right < tree_left) return left;
  66.  
  67.         if(tree[node] + suma < x)
  68.         {
  69.             suma += tree[node];
  70.             return right;
  71.         }
  72.  
  73.         if(left == right) return left - 1;
  74.  
  75.         int mid = (left + right) >> 1;
  76.         int query_left = _binary_search(node * 2, left, mid, tree_left, x, suma);
  77.  
  78.         if(query_left < mid) return query_left;
  79.         else return _binary_search(node * 2 + 1, mid + 1, right, tree_left, x, suma);
  80.     }
  81.  
  82.     inline int binary_searchg(int x)
  83.     {
  84.         int sum = 0;
  85.         return _binary_search(1, 1, offset, 1, x, sum);
  86.     }
  87. };
  88. /////////////////////////////////////////////////////////////////////
  89.  
  90. /// GLOBAL VARIABLES
  91. ci NMAX = 1e5 + 5;
  92. int n, m;
  93. SegTree arbint(NMAX);
  94. /////////////////////////////////////////////////////////////////////
  95.  
  96. /// FUNCTIONS
  97.  
  98. /////////////////////////////////////////////////////////////////////
  99.  
  100. /// SOLUTION
  101. signed main()
  102. {
  103.     Read_Optimizations
  104.  
  105.     fin >> n >> m;
  106.  
  107.     FOR(n)
  108.     {
  109.         int x; fin >> x;
  110.         arbint.update(i, x);
  111.     }
  112.  
  113.     FOR(m)
  114.     {
  115.         int type; fin >> type;
  116.         if(type == 0)
  117.         {
  118.             int a, b; fin >> a >> b;
  119.             arbint.update(a, b);
  120.         }
  121.         if(type == 1)
  122.         {
  123.             int a, b; fin >> a >> b;
  124.             fout << arbint.query(a, b) << '\n';
  125.         }
  126.         if(type == 2)
  127.         {
  128.             int a; fin >> a;
  129.             int x = arbint.binary_searchg(a);
  130.             if(arbint.query(1, x + 1) == a)
  131.                 fout << x + 1 << '\n';
  132.             else
  133.                 fout << -1 << '\n';
  134.         }
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement