Advertisement
Vince14

/<> 2243 (top down seg tree special query)

Sep 21st, 2023
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.61 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 M;
  26. int seg[MAXN * 4];
  27.  
  28. void update(int x, int s, int e, int idx, int up){
  29.     if(idx < s or idx > e) return;
  30.     if(s == e and s == idx){
  31.         seg[x] += up;
  32.         return;
  33.     }
  34.  
  35.     int mid = (s + e) / 2;
  36.     update(x * 2, s, mid, idx, up);
  37.     update(x * 2 + 1, mid + 1, e, idx, up);
  38.     seg[x] = seg[x * 2] + seg[x * 2 + 1];
  39. }
  40.  
  41. int query(int x, int s, int e, int n){
  42.     if(s == e){
  43.         return s;
  44.     }
  45.     int mid = (s + e) / 2;
  46.     if(seg[x * 2] >= n){
  47.         return query(x * 2, s, mid, n);
  48.     }
  49.     else{
  50.         return query(x * 2 + 1, mid + 1, e, n - seg[x * 2]);
  51.     }
  52. }
  53.  
  54. int main() {
  55.     FAST;
  56.     cin >> M;
  57.     for(int a, b, c, i = 0; i < M; i++){
  58.         cin >> a;
  59.         if(a == 1){
  60.             cin >> b;
  61.             int q = query(1, 1, 1000000, b);
  62.             cout << q << "\n";
  63.             update(1, 1, 1000000, q, -1);
  64.  
  65.         }
  66.         else{
  67.             cin >> b >> c;
  68.             update(1, 1, 1000000, b, c);
  69.  
  70.         }
  71.     }
  72. }
  73.  
  74. /*
  75. 6
  76. 2 1 2
  77. 2 3 3
  78. 1 2
  79. 1 2
  80. 2 1 -1
  81. 1 2
  82.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement