hpnq

PIVO_YAVNOYE

Sep 11th, 2022
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.27 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. #define loop(x, n) for(ll i = x; i < n; i++ )
  4. #define pb push_back
  5. #define MOD ll(1e9+7)
  6. #define all(x) x.begin(), x.end()
  7. #define f first
  8. #define s second
  9. #define DEBUG 1
  10. typedef long long ll;
  11. using namespace std;
  12. int next(int n, int k){
  13.     return (n+k)%26;
  14. }
  15.  
  16. ///
  17. struct Node{
  18.     int prior, size = 1;
  19.     int key;
  20.     Node *l = 0, *r = 0;
  21.     Node(int _key) {key = _key, prior = rand();}
  22. };
  23. ///
  24.  
  25. typedef pair<Node*, Node*> Pair;
  26.  
  27. int size(Node *v){return v ? v->size : 0;}
  28. void upd(Node *v){v->size = size(v->l) + size(v->r) + 1;}
  29.  
  30.  
  31. Node *merge(Node *l, Node *r){
  32.  
  33.     if(!l) return r;
  34.     if(!r) return l;
  35.     cout << "merge\n";
  36. //    cout << l->size << " " <<r->size << "\n";
  37.     if(l->prior > r->prior){
  38.         l->r = merge(l->r, r);
  39.         upd(l);
  40.         return l;
  41.     }else{
  42.         r->l = merge(l, r->l);
  43.         upd(r);
  44.         return r;
  45.     }
  46. }
  47.  
  48. Pair split(Node *p, int x) {
  49.     if (!p) return {0, 0};
  50.     if (p->key <= x) {
  51.         Pair q = split(p->r, x);
  52.         p->r = q.first;
  53.         return {p, q.second};
  54.     }
  55.     else {
  56.         Pair q = split(p->l, x);
  57.         p->l = q.second;
  58.         return {q.first, p};
  59.     }
  60. }
  61.  
  62. //Node *root = 0;
  63. Node* insert(Node *root, int x) {
  64.     Pair q= split(root, x);
  65.     Node *t = new Node(x);
  66. //    cout << q.f << " " << q.s << "\n";
  67.     root = merge(q.first, merge(t, q.second ));
  68. //    cout <<root->size <<"\n";
  69.     return root;
  70. }
  71. ///////
  72. ///////
  73.  
  74.  
  75. Node* ctrlx (Node *root, int l, int r) {
  76.     Pair q1 = split(root, r);
  77.     Pair q2 = split(q1.first, l);
  78.  
  79.     root = merge(q2.first, q1.second);
  80.     return q2.second;
  81. }
  82.  
  83. void ctrlv (Node *root, Node *v, int k) {
  84.     Pair q = split(root, k);
  85.     root = merge(q.first, merge(v, q.second));
  86.  
  87. }
  88.  
  89.  
  90. ///////
  91. ///////
  92.  
  93. //vector<Node*> abc(26);
  94. Node* abc[26];
  95. void solve(){
  96.     int n, m;
  97.     cin >> n >> m;
  98.     string a;
  99.     cin >> a;
  100.     ///построить дд /// nlogn
  101.  
  102.  
  103. //    loop(0, n){
  104. ////        Node q1 = insert(*abc[a[i]-'a'], i);
  105. ////        if(abc[a[i]-'a'] == 0){
  106. ////
  107. ////        }
  108. //        abc[a[i]-'a'] = insert(abc[a[i]-'a'], i);
  109. //
  110. ////        cout << abc[a[i]-'a'] << " " << abc[a[i]-'a']->size <<" ";
  111. ////        abc[a[i]-'a'] = &q1;
  112. //        cout <<"loop- "<< abc[a[i]-'a']->size <<" " << abc[a[i]-'a'] << " " << abc[a[i]-'a']->l << " " << abc[a[i]-'a']->r<<"\n";
  113. //
  114. //
  115. //    }
  116.  
  117.  
  118.     loop(0, m){
  119.         int q;
  120.         cin >> q;
  121.         if(q == 1){
  122.             //next
  123.             int r, l, k;
  124.             cin >> l >> r >> k;
  125.             Node *v;
  126.             loop(0, 26){
  127.                 v = ctrlx(abc[i], l, r);
  128.                 ctrlv(abc[(i+1)%26], v, l);
  129.  
  130.             }
  131.  
  132.         }else{
  133.             // f(l, r)
  134.             int l, r;
  135.             cin >> l >> r;
  136.             loop(0, 26){
  137.                 Pair qr = split(abc[i], r);
  138.                 Pair ql = split(qr.first, l);
  139.                 cout << ql.first;
  140.             }
  141.  
  142.         }
  143.     }
  144.  
  145. }
  146.  
  147. int main() {
  148. #ifdef DEBUG
  149.     freopen("text.txt", "r", stdin);
  150.     cout <<"DEBUG" <<"\n";
  151. #else
  152. #endif
  153. //    int t;
  154. //    cin >> t;
  155. //    loop(0, t) {
  156. //        solve();
  157. ////    cout<< solve_all("ananas");
  158. //
  159. //    }
  160.     solve();
  161. //    int n;
  162. //    cin >> n;
  163. //    cout << n;
  164.     return 0;
  165.  
  166.  
  167. }
Add Comment
Please, Sign In to add comment