Hydracody

Untitled

Nov 11th, 2023
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.68 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #include<ext/pb_ds/assoc_container.hpp>
  4. #include<ext/pb_ds/tree_policy.hpp>
  5. using namespace __gnu_pbds;
  6. #define LagaKar ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
  7. #define pb push_back
  8. #define pf push_front
  9. #define pob pop_back
  10. #define pof pop_front
  11. #define mp make_pair
  12. #define fi first
  13. #define se second
  14. #define lb lower_bound
  15. #define ub upper_bound
  16. #define ook order_of_key   //number of elements less than k
  17. #define fbo find_by_order  //k th element
  18. #define nline "\n"
  19. #define YES cout<<"YES"<<nline
  20. #define NO cout<<"NO"<<nline
  21. #define Yes cout<<"Yes"<<nline
  22. #define No cout<<"No"<<nline
  23. //--------------------------------------------------------------------------------------------------------------
  24. #define precise(ans)  cout<<fixed<<setprecision(15)<<ans
  25. #define fo(i,n) for(ll i=0;i<n;i++)
  26. #define Fo(i,k,n) for(ll i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
  27. #define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
  28. #define sz(x) ((ll)(x).size())
  29. #define all(x) x.begin(), x.end()
  30. #define MAX(x) *max_element(all(x))
  31. #define MIN(x) *min_element(all(x))
  32. #define SUM(x) accumulate(all(x), 0LL)
  33. #define CNT(x) __builtin_popcountll(x)
  34. //--------------------------------------------------------------------------------------------------------------
  35. typedef long long ll; typedef unsigned long long ull; typedef long double lld;
  36. template <typename T> using o_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  37. template <typename T, typename R> using o_map = tree<T, R, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  38. typedef pair<ll, ll>  pl;   typedef vector<ll>  vl;typedef vector<vl>  vvl;
  39. typedef vector<pl>  vpl;    template <typename T> using prq_mx  = priority_queue<T>;
  40. template <typename T> using prq_mn = priority_queue<T, vector<T>, greater<T>>;
  41. //------------------------------------------------------------------------------------------------------------
  42. #ifdef hydracody
  43.     #include <C:\Users\Anurag Dwivedi\Desktop\cpc\debug.h>
  44. #else
  45. #define debug(x)
  46. #endif
  47. //------------------------------------------------------------------------------------------------------------
  48. const double eps=1e-9;const ll INF=(ll)1e9;const ll inf64=2e18;const ll INF64=9e18;
  49. #define PI 3.1415926535897932384626
  50. #define MOD 1000000007
  51. #define MOD1 998244353
  52. #define MOD2 1000000009
  53. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  54. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  55. ll gcd(ll a, ll b) {if (b > a) {return gcd(b, a);} if (b == 0) {return a;} return gcd(b, a % b);}
  56. ll power(ll x,ll y, ll p){if(y==0)return 1;ll res = 1;x = x % p;if (x == 0) return 0;while (y > 0){if (y & 1)res = (res*x) % p;y = y>>1;x = (x*x) % p;}return res;}
  57. ll pinv(ll a, ll m){ return power(a, m - 2, m); }
  58. ll int_sqrt (ll x) {ll ans = 0; for (ll k = 1LL << 30; k != 0; k /= 2) {if ((ans + k) * (ans + k) <= x) {ans += k;}} return ans;}
  59. ll logceil(ll x){ll s=0;while(x>0){s++;x=x/2;}return s;}
  60. ll mod_add(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;}
  61. ll mod_mul(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;}
  62. ll mod_sub(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;}
  63. ll mod_div(ll a, ll b, ll m) {a = a % m; b = b % m; return (mod_mul(a, pinv(b, m), m) + m) % m;}  //only for prime m
  64. ll phin(ll n) {ll number = n; if (n % 2 == 0) {number /= 2; while (n % 2 == 0) n /= 2;} for (ll i = 3; i <= sqrt(n); i += 2) {if (n % i == 0) {while (n % i == 0)n /= i; number = (number / i * (i - 1));}} if (n > 1)number = (number / n * (n - 1)) ; return number;} //O(sqrt(N))
  65. ll getRandomNumber(ll l, ll r) {return uniform_int_distribution<ll>(l, r)(rng);}
  66. void starter(int t) {cout << "Case #" << t << ": ";}
  67.  
  68. /////////////////////////////////////////////////////////////////////////////////////////////////
  69. /*Don't loss hope, one day you will make it. Till then work hard.
  70. One day these bad days will go and you will rock.*/
  71.  
  72. struct Segtree{
  73.     vector<ll> t, lazy, sz;
  74.     Segtree(ll n) {
  75.         t.assign(4*n, 0);
  76.         lazy.assign(4*n, 0);
  77.         for(ll i=0;i<4*n;i++){
  78.           lazy[i] = -1;
  79.         }
  80.         sz.assign(4*n, 0);
  81.     }
  82.     void build(vector<ll>& a, ll v, ll tl, ll tr){
  83.         if(tl == tr){
  84.             t[v] = 0;
  85.             sz[v] = 1;
  86.         }
  87.         else{
  88.             ll tm = (tl + tr)/2;
  89.             build(a, v*2, tl, tm);
  90.             build(a, v*2+1, tm+1, tr);
  91.             t[v] = t[v*2] + t[v*2+1];
  92.             sz[v] = sz[v*2] + sz[v*2+1];
  93.         }
  94.     }
  95.     void push(ll v){
  96.       if(lazy[v]==-1) return;
  97.         t[v*2] = sz[v*2] -t[v*2] ;
  98.         lazy[v*2] = lazy[v];
  99.         t[v*2+1] = sz[v*2+1] -t[v*2+1];
  100.         lazy[v*2+1] = lazy[v];
  101.         lazy[v] = -1;
  102.     }
  103.     void update(ll v, ll tl, ll tr, ll l, ll r){
  104.         if(l > r){
  105.             return;
  106.         }
  107.         if(l <= tl && tr <= r){
  108.             t[v] = sz[v]-t[v] ;
  109.             lazy[v] = 1;
  110.         } else {
  111.             push(v);
  112.             ll tm = (tl + tr)/2;
  113.             update(v*2, tl, tm, l, min(r, tm));
  114.             update(v*2+1, tm+1, tr, max(l, tm+1), r);
  115.             t[v] = t[v*2]+t[v*2+1];
  116.         }
  117.     }
  118.     ll query(ll v, ll tl, ll tr, ll l, ll r) {
  119.         if(l>r) return 0;
  120.         if(l <= tl && tr <= r){
  121.             return t[v];
  122.         }
  123.         push(v);
  124.         ll tm = (tl + tr)/2;
  125.         return query(v*2, tl, tm, l, min(r, tm)) + query(v*2+1, tm+1, tr, max(l, tm+1), r);
  126.     }
  127. };
  128. /*
  129. Segtree s(n);
  130. s.build(a,1,0,n-1);
  131. s.query(1,0,n-1,l,r);
  132. s.update(1,0,n-1,l,r,val);  [l,r]
  133. */
  134.  
  135.  
  136.  
  137.  
  138.  
  139. void  chal(){
  140.   ll n,m;
  141.   cin>>n>>m;
  142.   vl aa(n,0);
  143.   Segtree s(n);
  144.   s.build(aa,1,0,n-1);
  145.  fo(i,m){
  146.       ll x;
  147.       cin>>x;
  148.       if(x==1){
  149.         ll l,r;
  150.         cin>>l>>r;
  151.         s.update(1,0,n-1,l,r-1);
  152.       }else{
  153.         ll k;
  154.         cin>>k;
  155.         ll str=0,end=n-1,pos=0;
  156.         while(str<=end){
  157.           ll mid=str+(end-str)/2;
  158.           if(s.query(1,0,n-1,0,mid)>k){
  159.             pos=mid;
  160.             end=mid-1;
  161.           }else{
  162.             str=mid+1;
  163.           }
  164.         }
  165.         cout<<pos<<nline;
  166.  
  167.       }
  168.  
  169.   }
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185. }
  186.  
  187.  
  188.  
  189.  
  190. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  191. int32_t main() {
  192.   LagaKar;
  193. #ifdef hydracody
  194.   freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); freopen("error.txt", "w", stderr);
  195. #endif
  196.   ll  t; t = 1;
  197.   // cin>>t;
  198.   for (ll i = 1; i <= t; i++) {
  199.     // cerr<<"Case : "<<i<<endl;
  200.     // starter();
  201.     chal();
  202.   }
  203.   return 0;
  204. }
Advertisement
Add Comment
Please, Sign In to add comment