Advertisement
i_love_rao_khushboo

Example Usage.cpp

Oct 14th, 2021
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.35 KB | None | 0 0
  1. // Problem: C. First element at least X
  2. // Contest: Codeforces - ITMO Academy: pilot course - Segment Tree, part 1 - Step 2
  3. // URL: https://codeforces.com/edu/course/2/lesson/4/2/practice/contest/273278/problem/C
  4. // Memory Limit: 1024 MB
  5. // Time Limit: 1000 ms
  6. // Parsed on: 14-10-2021 15:10:54 IST (UTC+05:30)
  7. // Author: Kapil Choudhary
  8. // ********************************************************************
  9. // कर्मण्येवाधिकारस्ते मा फलेषु कदाचन |
  10. // मा कर्मफलहेतुर्भूर्मा ते सङ्गोऽस्त्वकर्मणि || १.४७ ||
  11. // ********************************************************************
  12.  
  13. #include<bits/stdc++.h>
  14. using namespace std;
  15.  
  16. #define ll long long
  17. #define ld long double
  18. #define ull unsigned long long
  19. #define pb push_back
  20. #define ppb pop_back
  21. #define pf push_front
  22. #define ppf pop_front
  23. #define mp make_pair
  24. #define F first
  25. #define S second
  26. #define PI 3.1415926535897932384626
  27. #define sz(x) ((int)(x).size())
  28. #define vset(v, n, val) v.clear(); v.resize(n, val)
  29.  
  30. typedef pair<int, int> pii;
  31. typedef pair<ll, ll> pll;
  32. typedef vector<int> vi;
  33. typedef vector<ll> vll;
  34. typedef vector<ull> vull;
  35. typedef vector<bool> vb;
  36. typedef vector<char> vc;
  37. typedef vector<string> vs;
  38. typedef vector<pii> vpii;
  39. typedef vector<pll> vpll;
  40. typedef vector<vi> vvi;
  41. typedef vector<vll> vvll;
  42. typedef vector<vull> vvull;
  43. typedef vector<vb> vvb;
  44. typedef vector<vc> vvc;
  45. typedef vector<vs> vvs;
  46.  
  47. /************************************************** DEBUGGER *******************************************************************************************************/
  48.  
  49. #ifndef ONLINE_JUDGE
  50. #define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
  51. #else
  52. #define debug(x)
  53. #endif
  54.  
  55. void _print(ll t) { cerr << t; }
  56. void _print(int t) { cerr << t; }
  57. void _print(string t) { cerr << t; }
  58. void _print(char t) { cerr << t; }
  59. void _print(ld t) { cerr << t; }
  60. void _print(double t) { cerr << t; }
  61. void _print(ull t) { cerr << t; }
  62.  
  63. template <class T, class V> void _print(pair <T, V> p);
  64. template <class T> void _print(vector <T> v);
  65. template <class T> void _print(vector <vector<T>> v);
  66. template <class T> void _print(set <T> v);
  67. template <class T, class V> void _print(map <T, V> v);
  68. template <class T> void _print(multiset <T> v);
  69. template <class T, class V> void _print(multimap <T, V> v);
  70. template <class T> void _print(queue <T> v);
  71. template <class T> void _print(priority_queue <T> v);
  72. template <class T> void _print(stack <T> s);
  73.  
  74. // modify it's definition below as per need such as it can be used for STL containers with custom args passed
  75. template <class T> void _print(T v);
  76.  
  77. template <class T, class V> void _print(pair <T, V> p) { cerr << "{"; _print(p.F); cerr << ","; _print(p.S); cerr << "}"; }
  78. template <class T> void _print(vector <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  79. template <class T> void _print(vector <vector<T>> v) { cerr << "==>" << endl; for (vector<T> vec : v) { for(T i : vec) {_print(i); cerr << " "; } cerr << endl; } }
  80. template <class T> void _print(set <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  81. template <class T, class V> void _print(map <T, V> v) { cerr << "[ "; for (auto i : v) {_print(i); cerr << " "; } cerr << "]"; }
  82. template <class T> void _print(multiset <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  83. template <class T, class V> void _print(multimap <T, V> v) { cerr << "[ "; for (auto i : v) {_print(i); cerr << " "; } cerr << "]"; }
  84. template <class T> void _print(queue <T> v) { cerr << "[ "; while(!v.empty()) {_print(v.front()); v.pop(); cerr << " "; } cerr << "]"; }
  85. template <class T> void _print(priority_queue <T> v) { cerr << "[ "; while(!v.empty()) {_print(v.top()); v.pop(); cerr << " "; } cerr << "]"; }
  86. template <class T> void _print(stack <T> v) { cerr << "[ "; while(!v.empty()) {_print(v.top()); v.pop(); cerr << " "; } cerr << "]"; }
  87. template <class T> void _print(T v) {  }
  88.  
  89. /*******************************************************************************************************************************************************************/
  90.  
  91. mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
  92. int rng(int lim) {
  93.     uniform_int_distribution<int> uid(0,lim-1);
  94.     return uid(rang);
  95. }
  96.  
  97. const int INF = 0x3f3f3f3f;
  98. const int mod = 1e9+7;
  99.  
  100. ll mod_exp(ll a, ll b) { a %= mod; if(a == 0) return 0LL; ll res = 1LL;
  101.                          while(b > 0) { if(b & 1) res = (res * a) % mod; a = (a * a) % mod; b >>= 1; } return res; }
  102.                          
  103. ll mod_inv(ll a) { return mod_exp(a, mod - 2); } // works only for prime value of "mod"
  104. ll GCD(ll a, ll b) { return (b == 0) ? a : GCD(b, a % b); }
  105.  
  106. /******************************************************************************************************************************/
  107.  
  108. /*
  109. KEYNOTES:
  110. ------------------------------------------------------------------------------------------
  111. merge(X,identity_element) = X
  112. ------------------------------------------------------------------------------------------
  113. // (i.e no transformation at all or if we combine any new update with the identity update
  114. // it should give the newer update)
  115. identity_transformation.combine(X) = X
  116. ------------------------------------------------------------------------------------------
  117. ALWAYS: older_update.combine(newer_update)
  118. ------------------------------------------------------------------------------------------
  119. */
  120.  
  121. // this structure depends on the nature of query
  122. struct node {
  123.     // 1. change(s) required
  124.     // use more variables if you want more information
  125.     // these default values should be identity_element
  126.     int mx = -INF;
  127.        
  128.     node() {}
  129.     node(int val) {
  130.         // 2. change(s) required
  131.         mx = val;
  132.     }
  133.    
  134.     // associative merging function, store the thing you wanna query
  135.     void merge(const node &l, const node &r){
  136.         // 3. change(s) required
  137.         mx = max(l.mx, r.mx);
  138.     }
  139. };
  140.  
  141. // this structure depends on the nature of update
  142. struct update {
  143.     // 4. change(s) required
  144.     // use more variables if you want more information
  145.     // these default values should be identity_transformation or identity_update
  146.     int y = 0;
  147.    
  148.     update() {}
  149.     update(int val) {
  150.         // 5. change(s) required
  151.         y = val;
  152.     }
  153.    
  154.     // combine the current update with the other update (see keynotes)
  155.     // basically combine older lazy value(s) (if exist) with newer one(s).
  156.     // for this function, you can be sure that the "other" is newer than current
  157.     void combine(update &other, const int32_t &tl, const int32_t &tr) {
  158.         // 6. change(s) required
  159.         y = other.y;
  160.     }
  161.    
  162.     // store the correct information in the node x
  163.     void apply(node &x, const int32_t &tl, const int32_t &tr) {
  164.         // 7. change(s) required
  165.         x.mx = y;
  166.     }
  167. };
  168.  
  169. template<typename node, typename update>
  170. struct segtree {
  171.     int len;
  172.     vector<node> t;
  173.     vector<update> u;
  174.     vector<bool> lazy;
  175.     node identity_element;
  176.     update identity_transformation;
  177.    
  178.     segtree(int l) {
  179.         len = l;
  180.         t.clear(); t.resize(4 * len);
  181.         u.clear(); u.resize(4 * len);
  182.         lazy.clear(); lazy.resize(4 * len);
  183.         identity_element = node();
  184.         identity_transformation = update();
  185.     }
  186.    
  187.     void pushdown(const int32_t &v, const int32_t &tl, const int32_t &tr) {
  188.         if(!lazy[v]) return;
  189.         int32_t tm = (tl + tr) >> 1;
  190.         apply(v << 1, tl, tm, u[v]);
  191.         apply((v << 1) | 1, tm + 1, tr, u[v]);
  192.         u[v] = identity_transformation;
  193.         lazy[v] = 0;
  194.     }
  195.    
  196.     void apply(const int32_t &v, const int32_t &tl, const int32_t &tr, update upd) {
  197.         if(tl != tr) {
  198.             lazy[v] = 1;
  199.             u[v].combine(upd, tl, tr);
  200.         }
  201.        
  202.         upd.apply(t[v], tl, tr);
  203.     }
  204.    
  205.     template<typename T>
  206.     void build(const T &arr, const int32_t &v, const int32_t &tl, const int32_t &tr) {
  207.         if(tl == tr) {
  208.             t[v] = arr[tl];
  209.             return;
  210.         }
  211.        
  212.         int32_t tm = (tl + tr) >> 1;
  213.         build(arr, v << 1, tl, tm);
  214.         build(arr, (v << 1) | 1, tm + 1, tr);
  215.         t[v].merge(t[v << 1],t[(v << 1) | 1]);
  216.     }
  217.    
  218.     node query(const int32_t &v, const int32_t &tl, const int32_t &tr, const int32_t &l, const int32_t &r) {
  219.         if(l > tr || r < tl) {
  220.             return identity_element;
  221.         }
  222.        
  223.         if(tl >= l && tr <= r){
  224.             return t[v];
  225.         }
  226.        
  227.         pushdown(v, tl, tr);
  228.         int32_t tm = (tl + tr) >> 1;
  229.         node a = query(v << 1, tl, tm, l, r), b = query((v << 1) | 1, tm + 1, tr, l, r), ans;
  230.         ans.merge(a, b);
  231.         return ans;
  232.     }
  233.    
  234.     void rupd(const int32_t &v, const int32_t &tl, const int32_t &tr, const int32_t &l, const int32_t &r, const update &upd) {
  235.         if(l > tr || r < tl){
  236.             return;
  237.         }
  238.        
  239.         if(tl >= l && tr <= r){
  240.             apply(v,tl,tr,upd);
  241.             return;
  242.         }
  243.        
  244.         pushdown(v, tl, tr);
  245.         int32_t tm = (tl + tr) >> 1;
  246.         rupd(v << 1, tl, tm, l, r, upd);
  247.         rupd((v << 1) | 1, tm + 1, tr, l, r, upd);
  248.         t[v].merge(t[v << 1], t[(v << 1) | 1]);
  249.     }
  250.    
  251.     public:
  252.     template<typename T>
  253.     void build(const T &arr) {
  254.         build(arr, 1, 0, len - 1);
  255.     }
  256.    
  257.     node query(const int32_t &l,const int32_t &r) {
  258.         return query(1, 0, len - 1, l, r);
  259.     }
  260.    
  261.     void rupd(const int32_t &l, const int32_t &r, const update &upd) {
  262.         rupd(1, 0, len - 1, l, r, upd);
  263.     }
  264. };
  265.  
  266. // https://www.youtube.com/watch?v=Hooe5ORdFsc
  267. void solve()
  268. {
  269.     int n, m; cin >> n >> m;
  270.     vi v(n);
  271.     for(int i = 0; i < n; i++) cin >> v[i];
  272.    
  273.     struct segtree<node, update> s(n);
  274.     s.build(v);
  275.    
  276.     while(m--) {
  277.         int tp; cin >> tp;
  278.        
  279.         if(tp == 1) {
  280.             int idx, val; cin >> idx >> val;
  281.             s.rupd(idx, idx, val);
  282.         }
  283.        
  284.         else {
  285.             int x; cin >> x;
  286.            
  287.             int L = 0, R = n - 1;
  288.             int ans = INF;
  289.            
  290.             while(L <= R) {
  291.                 int mid = L + ((R - L) >> 1);
  292.                 if(s.query(L, mid).mx < x) L = mid + 1;
  293.                 else ans = min(ans, mid), R = mid - 1;
  294.             }
  295.            
  296.             if(ans == INF) ans = -1;
  297.             cout << ans << "\n";
  298.         }
  299.     }
  300. }
  301.  
  302. int main()
  303. {
  304.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  305.     srand(chrono::high_resolution_clock::now().time_since_epoch().count());
  306.  
  307.     // #ifndef ONLINE_JUDGE
  308.     //     freopen("input.txt", "r", stdin);
  309.     //     freopen("output.txt", "w", stdout);
  310.     // #endif
  311.    
  312.     // #ifndef ONLINE_JUDGE
  313.     //      freopen("error.txt", "w", stderr);
  314.     // #endif
  315.    
  316.     int t = 1;
  317.     // int test = 1;
  318.     // cin >> t;
  319.     while(t--) {
  320.         // cout << "Case #" << test++ << ": ";
  321.         solve();
  322.     }
  323.  
  324.     return 0;
  325. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement