Advertisement
Vince14

/<> 14428 (top down seg tree carry root)

Sep 21st, 2023 (edited)
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.17 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 = 100005;
  23.  
  24. int N, M;
  25. int arr[MAXN];
  26. pii seg[MAXN * 4];
  27.  
  28. void build(int x, int s, int e){
  29.     if(s == e){
  30.         seg[x] = {arr[s], s};
  31.         return;
  32.     }
  33.     int mid = (s + e) / 2;
  34.     build(x * 2, s, mid);
  35.     build(x * 2 + 1, mid + 1, e);
  36.     if(seg[x * 2].first <= seg[x * 2 + 1].first){
  37.         seg[x] = seg[x * 2];
  38.     }
  39.     else{
  40.         seg[x] = seg[x * 2 + 1];
  41.     }
  42. }
  43.  
  44. void update(int x, int s, int e, int idx, int val){
  45.     if(idx < s or idx > e) return;
  46.     if(s == e){
  47.         seg[x] = {val, idx};
  48.         return;
  49.     }
  50.     int mid = (s + e) / 2;
  51.     update(x * 2, s, mid, idx, val);
  52.     update(x * 2 + 1, mid + 1, e, idx, val);
  53.     if(seg[x * 2].first <= seg[x * 2 + 1].first){
  54.         seg[x] = seg[x * 2];
  55.     }
  56.     else{
  57.         seg[x] = seg[x * 2 + 1];
  58.     }
  59. }
  60.  
  61. pii query(int x, int s, int e, int a, int b){
  62.     if(e < a or s > b) return {2e9, -1};
  63.     if(a <= s and e <= b) return seg[x];
  64.  
  65.     int mid = (s + e) / 2;
  66.     pii q1 = query(x * 2, s, mid, a, b);
  67.     pii q2 = query(x * 2 + 1, mid + 1, e, a, b);
  68.     if(q1.first <= q2.first){
  69.         return q1;
  70.     }
  71.     else{
  72.         return q2;
  73.     }
  74. }
  75.  
  76.  
  77. int main() {
  78.     FAST;
  79.     cin >> N;
  80.     for(int i = 1; i <= N; i++){
  81.         cin >> arr[i];
  82.     }
  83.     build(1, 1, N);
  84.     cin >> M;
  85.     for(int a, b, c, i = 1; i <= M; i++){
  86.         cin >> a >> b >> c;
  87.         if(a == 1){
  88.             update(1, 1, N, b, c);
  89.         }
  90.         else{
  91.             if(b > c){
  92.                 swap(b, c);
  93.                 arr[b] = c;
  94.             }
  95.             cout << query(1, 1, N, b, c).second << "\n";
  96.         }
  97.     }
  98. }
  99.  
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement