Advertisement
lmarioza

Untitled

Oct 8th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.13 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <unordered_map>
  3. #define maxn 100001
  4. #define smaxn 600
  5. #define F first
  6. #define S second
  7. #define mp make_pair
  8. #define pb push_back
  9.  
  10. using namespace std;
  11.  
  12.  
  13.  
  14. int bs;
  15. list<int> v;
  16. int in[maxn];
  17. unordered_map<int,int> pos;
  18. int n;
  19.  
  20. struct Block{
  21.     list<int>::iterator ini,fim;
  22.     int qtd;
  23.     Block(){
  24.         fim=ini=v.end();
  25.         qtd =0;
  26.     }
  27. };
  28.  
  29. Block sum[smaxn];
  30.  
  31. int query(int l,int r) {
  32.     auto it = sum[pos[l]].ini;
  33.     while(*it !=l)it++;
  34.     it++;
  35.     int block = pos[*it];
  36.     int ans=0;
  37.     if(it != sum[block].ini){
  38.         while(it !=sum[block].fim){
  39.             if(*it == r) return ans;
  40.             ans++;
  41.             it++;
  42.         }
  43.         if(*it == r) return ans;
  44.         ans++;
  45.         it++;
  46.         block++;
  47.     }
  48.     while(block<pos[r]){
  49.         ans+=bs;
  50.         block++;
  51.     }
  52.     it = sum[block].ini;
  53.  
  54.     while(*it !=r){
  55.         ans++;
  56.         it++;
  57.     }
  58.     return ans;
  59. }  
  60.  
  61.  
  62. void insert(int p, int e) {
  63.     int block = pos[e];
  64.     auto it = sum[block].ini;
  65.     //printf("%d %d\n",p,e );
  66.     while(*it!=e)it++;
  67.     it++;
  68.     v.insert(it,p);
  69.     if(e == *sum[block].fim){
  70.         block++;
  71.         it--;
  72.         sum[block].ini =it;
  73.     }
  74.     pos[p] = block;
  75.     while(sum[block].qtd== bs){
  76.         pos[*sum[block].fim]++;
  77.         sum[block+1].ini = sum[block].fim;
  78.         sum[block].fim--;
  79.         block++;
  80.     }
  81.     if(sum[block].qtd ==bs-1) sum[block].fim--;
  82.     sum[block].qtd++;
  83. }
  84.  
  85.  
  86. int remove(int p) {
  87.     int block = pos[p];
  88.     auto it = sum[block].ini;
  89.     while(*it!=p)it++;
  90.     if(p == *sum[block].ini) sum[block].ini++;
  91.     v.erase(it);
  92.     while(sum[block].qtd == bs){
  93.         if(sum[block+1].qtd>0){
  94.             pos[*sum[block+1].ini]--;
  95.             sum[block].fim =sum[block+1].ini;
  96.             sum[block+1].ini++;
  97.         }else{
  98.             sum[block].fim = v.end();
  99.         }
  100.         block++;
  101.     }
  102.     if(sum[block].qtd == 0)block--;
  103.     sum[block].qtd--;
  104. }
  105.  
  106.  
  107. void build () {
  108.     int block =0;
  109.     for(int i=0;i<n;i++){
  110.         v.push_back(in[i]);
  111.         pos[in[i]]=block;
  112.         list<int>::iterator end = v.end();
  113.         end--;
  114.         if(sum[block].qtd == 0) sum[block].ini = end;
  115.         sum[block].qtd++;
  116.         if(sum[block].qtd == bs){
  117.             sum[block].fim=end;
  118.             block++;
  119.         }
  120.     }
  121.  
  122. }
  123.  
  124. void printlist(){
  125.     for(auto it = v.begin();it!=v.end();it++){
  126.         printf("%d ",*it );
  127.     }
  128.     printf("\n");
  129.     for(auto it = v.begin();it!=v.end();it++){
  130.         printf("%d ",pos[*it] );
  131.     }
  132.     printf("\n");
  133.     int i=0;
  134.     while(sum[i].qtd == bs){
  135.         printf("%d %d %d %d\n",i,*sum[i].ini,*sum[i].fim,sum[i].qtd );
  136.         i++;
  137.     }
  138.         printf("%d %d %d %d\n",i,*sum[i].ini,*sum[i].fim,sum[i].qtd );
  139.     printf("\n");
  140. }
  141.  
  142. int main () {
  143.     scanf("%d", &n);   
  144.     bs =sqrt(maxn);
  145.  
  146.     for (int i = 0; i < n; i++) {
  147.         scanf("%d", in+i);
  148.     }
  149.     build();
  150.  
  151.     int q; scanf("%d", &q);
  152.  
  153.     while(q--) {
  154.         //printlist();
  155.         char c; scanf(" %c", &c);
  156.         if (c == 'I') {
  157.             int p, e; scanf("%d %d", &p, &e);
  158.             insert(p,e);
  159.         } else if (c == 'R') {
  160.             int e; scanf("%d", &e);
  161.             remove(e);
  162.         } else {
  163.             int a, b; scanf("%d %d", &a, &b);
  164.             if(pos[a]>pos[b]){
  165.                 int aux =a;
  166.                 a = b;
  167.                 b = aux;
  168.             }else if(pos[a] == pos[b]){
  169.                 for(auto it = sum[pos[a]].ini;it!=sum[pos[a]].fim;it++){
  170.                     if(*it == a)break;
  171.                     if(*it == b){
  172.                         int aux = a;
  173.                         a = b;
  174.                         b = aux;
  175.                         break;
  176.                     }
  177.                 }
  178.             }
  179.             printf("%d\n",query(a,b) );
  180.         }
  181.         //printlist();
  182.     }
  183.  
  184.     return 0;
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement