Guest User

Untitled

a guest
Jan 20th, 2025
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.37 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4.  
  5. using namespace std;
  6. using namespace __gnu_pbds;
  7. using namespace chrono;
  8.  
  9. #define mod 1000000007
  10. #define pb push_back
  11. #define mp make_pair
  12. #define ff first
  13. #define ss second
  14. #define pii pair<int, int>
  15. #define vb vector<bool>
  16. #define int long long
  17. #define ld long double
  18. #define vi vector<int>
  19. #define vvi vector<vector<int>>
  20. #define vvc vector<vector<char>>
  21. #define vll vector<long long>
  22. #define vc vector<char>
  23. #define vs vector<string>
  24. #define s string
  25. #define all(x) (x).begin(), (x).end()
  26. #define cneg cout << -1 << endl;
  27. #define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
  28.  
  29. void print(auto a)
  30. {
  31.   for (auto i : a)
  32.   {
  33.     cout << i.ff << " ";
  34.   }
  35.   cout << endl;
  36. }
  37.  
  38. template <typename T>
  39. using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  40. // order_of_key (k) : Number of items strictly smaller than k
  41.  
  42. class STree
  43. {
  44.   int sz;
  45.   vector<ordered_set<pii>> vals;
  46.   int cnt = 0;
  47.  
  48.   void _print()
  49.   {
  50.     for(auto i : vals)
  51.     {
  52.       print(i);
  53.     }
  54.   }
  55.  
  56.   ordered_set<pii> expr(int x)
  57.   {
  58.     auto st = vals[2*x + 1];
  59.     for(auto i : vals[2*x + 2])
  60.     {
  61.       st.insert(i);
  62.     }
  63.     return st; // CHANGE THIS FOR OTHER USES
  64.   }
  65.  
  66.   void build(vi& a, int x, int lx, int rx)
  67.   {
  68.     if(rx - lx == 1) // leaf node
  69.     {
  70.       if(lx < (int)a.size())
  71.       {
  72.         vals[x].insert({a[lx], cnt++});
  73.       }
  74.       return;
  75.     }
  76.     int m = lx + (rx - lx)/2;
  77.  
  78.     build(a, 2*x + 1, lx, m);
  79.     build(a, 2*x + 2, m, rx);
  80.  
  81.     vals[x] = expr(x);
  82.   }
  83.  
  84.   pii set(int i, int v, int x, int lx, int rx)
  85.   {
  86.     if(rx - lx == 1)
  87.     {
  88.       pii ele = *vals[x].begin();
  89.       vals[x].erase(ele);
  90.       vals[x].insert({v, ele.ss});
  91.       return ele;
  92.     }
  93.     int m = lx + (rx - lx)/2;
  94.  
  95.     if(i < m)
  96.     {
  97.       pii c = set(i, v, 2*x + 1, lx, m);
  98.       vals[x].erase(c);
  99.       vals[x].insert({v, c.ss});
  100.  
  101.       return c;
  102.     }
  103.    
  104.     pii c = set(i, v, 2*x + 2, m, rx);
  105.     vals[x].erase(c);
  106.     vals[x].insert({v, c.ss});
  107.  
  108.     return c;
  109.   }
  110.  
  111.   int _query(int l, int r)
  112.   {
  113.     auto itrL = vals[0].lower_bound({l, 0ll});
  114.     if(itrL == vals[0].end()) return 0;
  115.  
  116.     int cntLessL = vals[0].order_of_key(*itrL);
  117.  
  118.     auto itrR = vals[0].upper_bound({r, LLONG_MAX});
  119.     if(itrR == vals[0].end()) return vals[0].size() - cntLessL;
  120.  
  121.     int cntLessR = vals[0].order_of_key(*itrR);
  122.  
  123.     return cntLessR - cntLessL;
  124.   }
  125. public:
  126.   STree(vi& a)
  127.   {
  128.     int n = a.size();
  129.  
  130.     sz = 1;
  131.  
  132.     while(sz < n) sz <<= 1;
  133.  
  134.     vals.assign(2*sz, ordered_set<pii>());
  135.  
  136.     build(a);
  137.   }
  138.  
  139.   void build(vi& a)
  140.   {
  141.     build(a, 0, 0, sz);
  142.   }
  143.  
  144.   void set(int i, int v) // sets ith value (0-indexed) to v
  145.   {
  146.     set(i, v, 0, 0, sz);
  147.   }
  148.  
  149.   void _printT()
  150.   {
  151.     _print();
  152.   }
  153.  
  154.   int query(int l, int r) // range is [l, r]
  155.   {
  156.     return _query(l, r);
  157.   }
  158. };
  159.  
  160. void solve()
  161. {
  162.   int n, m;
  163.   cin >> n >> m;
  164.  
  165.   vi a(n);
  166.   ordered_set<pii> st;
  167.   for (int i = 0; i < n; i++)
  168.   {
  169.     cin >> a[i];
  170.     st.insert({a[i], i});
  171.   }
  172.  
  173.   // STree sgt(a);
  174.  
  175.   for(int i = 0; i < m; i++)
  176.   {
  177.     char ch;
  178.     cin >> ch;
  179.  
  180.     if(ch == '?')
  181.     {
  182.       int l, r;
  183.       cin >> l >> r;
  184.  
  185.       auto itrL = st.lower_bound({l, 0});
  186.  
  187.       if(itrL == st.end())
  188.       {
  189.         cout << 0 << endl;
  190.         continue;
  191.       }
  192.  
  193.       auto itrR = st.upper_bound({r, INT_MAX});
  194.  
  195.       if(itrR == st.end())
  196.       {
  197.         cout << n - st.order_of_key(*itrL) << endl;
  198.         continue;
  199.       }
  200.  
  201.       cout << st.order_of_key(*itrR) - st.order_of_key(*itrL) << endl;
  202.     }
  203.     else
  204.     {
  205.       int k, x;
  206.       cin >> k >> x;
  207.  
  208.       st.erase({a[k - 1], k - 1});
  209.       st.insert({x, k - 1});
  210.       a[k - 1] = x;
  211.     }
  212.   }
  213. }
  214.  
  215. #undef int
  216. int main()
  217. {
  218. #ifndef ONLINE_JUDGE
  219.   freopen("input.txt", "r", stdin);
  220.   freopen("output.txt", "w", stdout);
  221.   freopen("error.txt", "w", stderr);
  222. #endif
  223.   fastio();
  224.  
  225.   auto start1 = high_resolution_clock::now();
  226.  
  227.   int t = 1;
  228.   // cin >> t;
  229.  
  230.   while (t--)
  231.   {
  232.     solve();
  233.   }
  234.  
  235.   auto stop1 = high_resolution_clock::now();
  236.   auto duration = duration_cast<milliseconds>(stop1 - start1);
  237. #ifndef ONLINE_JUDGE
  238.   cerr << "Time: " << duration.count() << " ms" << endl;
  239. #endif
  240.  
  241.   return 0;
  242. }
  243.  
  244.  
Advertisement
Add Comment
Please, Sign In to add comment