Advertisement
Kambarych

Treap swap

Jul 11th, 2024
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.02 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define For(i, n)           for(int i = 0; i < n; ++i)
  6. #define all(x)              (x).begin(),(x).end()
  7. #define rall(x)             (x).rbegin(),(x).rend()
  8. #define ls(x)               x+x+1
  9. #define rs(x)               x+x+2
  10. #define endl                '\n'
  11.  
  12. #define ld                  long double
  13. #define pii                 pair<int, int>
  14. #define vt                  vector
  15. #define ll                  long long
  16.  
  17. template<typename T> void read(vt<T> & a) {For(i, a.size()) cin >> a[i];}
  18. template<typename T> void read2(vt<vt<T> > & a) {For(i, a.size()) read(a[i]);}
  19. template<typename T> void print(vt<T> & a) {For(i, a.size()) cout << a[i] << " "; cout << endl;}
  20. template<typename T> void print2(vt<vt<T> > & a) {For(i, a.size()) print(a[i]);}
  21.  
  22. const int MAX = 1e9;
  23. const int MOD = 1000000007;
  24. const ll  INF = 1e18;
  25. const ld  PI  = 3.14159265358979323846;
  26.  
  27. mt19937 rnd;
  28.  
  29. struct Node {
  30.     ll sum, pref, suff, ans;
  31.     Node() {
  32.         this->sum = this->pref = this->suff = this->ans = 0;
  33.     }
  34.     Node(ll value) {
  35.         this->sum = value;
  36.         this->pref = this->suff = this->ans = max(0LL, value);
  37.     }
  38. };
  39.  
  40. Node merge(Node n1, Node n2) {
  41.     Node r;
  42.     r.sum = n1.sum + n2.sum;
  43.     r.pref = max (n1.pref, n1.sum + n2.pref);
  44.     r.suff = max (n2.suff, n2.sum + n1.suff);
  45.     r.ans = max (max (n1.ans, n2.ans), n1.suff + n2.pref);
  46.     return r;
  47. }
  48.  
  49. struct Treap {
  50.     Node data;
  51.     ll value;
  52.     int priority;
  53.     array<Treap*, 2> kids;
  54.     int subtree_size;
  55.  
  56.     Treap(int data);
  57. };
  58.  
  59. int size(Treap *me) {
  60.     return me == nullptr ? 0 : me->subtree_size;
  61. }
  62.  
  63. void recalc(Treap *me) {
  64.     if (me==nullptr) return;
  65.     me->subtree_size = 1;
  66.     Node result = Node();
  67.     if (me->kids[0] != nullptr) {
  68.         result = merge(result, me->kids[0]->data);
  69.         me->subtree_size += me->kids[0]->subtree_size;
  70.     }
  71.     result = merge(result, Node(me->value));
  72.     if (me->kids[1] != nullptr) {
  73.         result = merge(result, me->kids[1]->data);
  74.         me->subtree_size += me->kids[1]->subtree_size;
  75.     }
  76.     me->data = result;
  77. }
  78.  
  79. Treap* merge(Treap *l, Treap *r) {
  80.     if (l==nullptr) return r;
  81.     if (r==nullptr) return l;
  82.     if (l->priority < r->priority) {
  83.         l->kids[1]=merge(l->kids[1], r);
  84.         recalc(l);
  85.         return l;
  86.     }
  87.     else {
  88.         r->kids[0]=merge(l, r->kids[0]);
  89.         recalc(r);
  90.         return r;
  91.     }
  92. }
  93.  
  94. array<Treap*, 2> split(Treap *me, int nInLeft) {
  95.     if (me == nullptr) return {nullptr, nullptr};
  96.     if (size(me->kids[0])>=nInLeft) {
  97.         array<Treap*, 2> leftRes=split(me->kids[0], nInLeft);
  98.         me->kids[0]=leftRes[1];
  99.         recalc(me);
  100.         return {leftRes[0], me};
  101.     }
  102.     else {
  103.         nInLeft = nInLeft - size(me->kids[0]) - 1;
  104.         array<Treap*, 2> rightRes = split(me->kids[1], nInLeft);
  105.         me->kids[1] = rightRes[0];
  106.         recalc(me);
  107.         return {me, rightRes[1]};
  108.     }
  109.     return {nullptr, nullptr};
  110. }
  111.  
  112. Treap::Treap(int value) {
  113.     kids={nullptr, nullptr};
  114.     this->value = ((value == 0) ? -MAX : value);
  115.     this->data = Node(this->value);
  116.     this->priority = rnd();
  117.     this->subtree_size = 1;
  118.     recalc(this);
  119. }
  120.  
  121. template<class F>
  122. void each(Treap *n, F f) {
  123.     if (n) { each(n->kids[0], f); f(n); each(n->kids[1], f); }
  124. }
  125.  
  126. // https://codeforces.com/gym/102787/problem/Y
  127.  
  128. void solve() {
  129.     int n, q; cin >> n >> q;
  130.     string s; cin >> s;
  131.     Treap *root = nullptr;
  132.     Treap *iroot = nullptr;
  133.     For(i, n) {
  134.         Treap *t = new Treap(s[i] - '0');
  135.         Treap *it = new Treap((s[i] - '0') ^ 1);
  136.         root = merge(root, t);
  137.         iroot = merge(iroot, it);
  138.     }
  139.     For(i, q) {
  140.         int _, l, r; cin >> _ >> l >> r;
  141.         auto [other, third] = split(root, r);
  142.         auto [first, second] = split(other, l - 1);
  143.        
  144.         auto [iother, ithird] = split(iroot, r);
  145.         auto [ifirst, isecond] = split(iother, l - 1);
  146.  
  147.  
  148.         root = merge(first, merge(isecond, third));
  149.         iroot = merge(ifirst, merge(second, ithird));
  150.  
  151.         ll ans = max(root->data.ans, iroot->data.ans);
  152.         cout << ans << endl;
  153.     }
  154. }
  155.  
  156. int main() {
  157.     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  158.     solve();
  159.     return 0;
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement