Advertisement
Guest User

COJ 2418 - Ivan Operations

a guest
Dec 10th, 2013
788
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.97 KB | None | 0 0
  1. //============================================================================
  2. // Name        : Jose Carlos Gonzalez Fernandez
  3. // Author      : Kaga2
  4. // Version     :
  5. // Copyright   : UCI2ndTWF
  6. // Description : Hello World in C++, Ansi-style
  7. //============================================================================
  8.  
  9.  
  10. #include <iostream>
  11. #include <algorithm>
  12. #include <stdio.h>
  13. #include <string.h>
  14.  
  15. #include <vector>
  16. #include <limits>
  17. #include <queue>
  18. #include <cstdlib>
  19. #include <string>
  20. #include <map>
  21. #include <math.h>
  22. #include <limits>
  23. #include <time.h>
  24. #include <bitset>
  25. #include <set>
  26. #include <stack>
  27. #include <complex>
  28.  
  29. using namespace std;
  30. #define ll long long
  31. #define ld long double
  32. #define ull unsigned long long
  33. #define uint unsigned int
  34. #define MP make_pair
  35.  
  36. struct node{
  37.     node *p,*l,*r;
  38.  
  39.     int car, C[26], vsame, size;
  40.     bool same,rev;
  41.  
  42.     node(){}
  43.     node(int c){
  44.         car = c;
  45.         C[car] = 1;
  46.         size = 1;
  47.         same = rev = 0;
  48.         l = r = p = NULL;
  49.     }
  50.  
  51.  
  52.     void make_same(char x){
  53.         same = 1;
  54.         car = vsame = x;
  55.         memset(C,0,sizeof(C));
  56.         C[car] = size;
  57.     }
  58.  
  59.     void reverse(){
  60.         rev ^= 1;
  61.         swap(l,r);
  62.     }
  63.  
  64.     void update(){
  65.         size = 1;
  66.         memset(C,0,sizeof(C));
  67.         C[car] = 1;
  68.         if (l){
  69.             size += l->size;
  70.             for(int i=0;i<26;i++) C[i] += l->C[i];
  71.         }
  72.         if (r) {
  73.             size += r->size;
  74.             for(int i=0;i<26;i++) C[i] += r->C[i];
  75.         }
  76.     }
  77.  
  78.     void push_down(){
  79.         if (rev){
  80.             rev = 0;
  81.             if (l) l->reverse();
  82.             if (r) r->reverse();
  83.         }
  84.         if (same){
  85.             same = 0;
  86.             if (l) l->make_same(vsame);
  87.             if (r) r->make_same(vsame);
  88.         }
  89.     }
  90. };
  91.  
  92. void zig(node *p){
  93.     node *q = p->p;
  94.     node *r = q->p;
  95.     if ((q->l = p->r)) q->l->p = q;
  96.     p->r = q, q->p = p;
  97.     if ((p->p = r)){
  98.         if (r->l == q) r->l = p;
  99.         if (r->r == q) r->r = p;
  100.     }
  101.     q->update();
  102. }
  103.  
  104. void zag(node *p){
  105.     node *q = p->p;
  106.     node *r = q->p;
  107.     if ((q->r = p->l)) q->r->p = q;
  108.     p->l = q, q->p = p;
  109.     if ((p->p = r)){
  110.         if (r->l == q) r->l = p;
  111.         if (r->r == q) r->r = p;
  112.     }
  113.     q->update();
  114. }
  115.  
  116. node *root;
  117. void splay(node *p, node *fa = NULL){
  118.     while (p->p != fa){
  119.         node *q = p->p;
  120.         if (q->p == fa){
  121.             if (q->l == p)
  122.                 zig(p);
  123.             else
  124.                 zag(p);
  125.         }
  126.         else{
  127.             node *r = q->p;
  128.             if (r->l == q)
  129.                 if (q->l == p)
  130.                     zig(q),zig(p);
  131.                 else
  132.                     zag(p),zig(p);
  133.             else
  134.                 if (q->l == p)
  135.                     zig(p),zag(p);
  136.                 else
  137.                     zag(q),zag(p);
  138.         }
  139.     }
  140.     p->update();
  141.     if (fa == NULL)
  142.         root = p;
  143.     else
  144.         fa->update();
  145. }
  146.  
  147.  
  148. char cad[100005];
  149.  
  150. node *built(int a,int b){
  151.     if (b < a) return NULL;
  152.     int med = (a + b) / 2;
  153.     node *x = new node(cad[med] - 'a');
  154.     if ((x->l = built(a,med-1)))
  155.         x->l->p = x;
  156.     if ((x->r = built(med+1,b)))
  157.         x->r->p = x;
  158.     x->update();
  159.     return x;
  160. }
  161.  
  162. node *find(int k){
  163.     node *now = root;
  164.     while (true){
  165.         now->push_down();
  166.         if (now->l){
  167.             if (now->l->size >= k){
  168.                 now = now->l; continue;}
  169.             k -= now->l->size;
  170.         }
  171.         if (k == 1) break;
  172.         k--;
  173.         now = now->r;
  174.     }
  175.     return now;
  176. }
  177.  
  178. void REVERSE(int a,int b){
  179.     node *A = find(a);
  180.     node *B = find(b+2);
  181.     splay(A), splay(B,A);
  182.     B->l->reverse();
  183. }
  184.  
  185. void MAKE_SAME(int a,int b,int c){
  186.     node *A = find(a);
  187.     node *B = find(b+2);
  188.     splay(A), splay(B,A);
  189.     B->l->make_same(c);
  190.     B->update();
  191.     A->update();
  192. }
  193.  
  194. int QUERY(int a,int b,int c){
  195.     node *A = find(a);
  196.     node *B = find(b+2);
  197.     splay(A), splay(B,A);
  198.     return B->l->C[c];
  199. }
  200.  
  201.  
  202. int main(){
  203.     //freopen("/home/jose/Escritorio/mining_sample.in","r",stdin);
  204.  
  205.     scanf("%s",cad);
  206.     int n = strlen(cad);
  207.  
  208.     root = new node(0);
  209.     root->l = new node(0);
  210.     root->l->p = root;
  211.     root->l->r = built(0,n-1);
  212.     root->l->r->p = root->l;
  213.  
  214.     root->l->update();
  215.     root->update();
  216.  
  217.     char op[23];
  218.     int q;scanf("%d",&q);
  219.     int a,b;
  220.     char c;
  221.     while(q--){
  222.         scanf("%s%d%d",op,&a,&b);
  223.         if (op[0] != 'R') scanf(" %c",&c);
  224.  
  225.         switch(op[0]){
  226.  
  227.         case 'S':
  228.             MAKE_SAME(a,b,c - 'a');
  229.             break;
  230.  
  231.         case 'R':
  232.             REVERSE(a,b);
  233.             break;
  234.  
  235.         case 'C':
  236.             printf("%d\n",QUERY(a,b,c - 'a'));
  237.             break;
  238.         }
  239.     }
  240.  
  241.     return 0;
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement