Advertisement
lmarioza

Untitled

Oct 8th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.71 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <unordered_map>
  3. #define maxn 300001
  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(p == *sum[block].fim)block++;
  70.     pos[e] = block;
  71.     while(sum[block].qtd== bs){
  72.         pos[*sum[block].fim]++;
  73.         sum[block+1].ini = sum[block].fim;
  74.         sum[block].fim--;
  75.         block++;
  76.     }
  77.     if(sum[block].qtd ==bs-1) sum[block].fim--;
  78.     sum[block].qtd++;
  79. }
  80.  
  81.  
  82. int remove(int p) {
  83.     int block = pos[p];
  84.     auto it = sum[block].ini;
  85.     while(*it!=p)it++;
  86.     v.erase(it);
  87.     if(p == *sum[block].ini) sum[block].ini--;
  88.     while(sum[block].qtd == bs){
  89.         pos[*sum[block+1].ini]--;
  90.         sum[block].fim =sum[block+1].ini;
  91.         sum[block+1].ini++;
  92.         block++;
  93.     }
  94.     if(sum[block].qtd == 0)block--;
  95.     sum[block].qtd--;
  96. }
  97.  
  98.  
  99. void build () {
  100.     int block =0;
  101.     for(int i=0;i<n;i++){
  102.         v.push_back(in[i]);
  103.         pos[i]=block;
  104.         list<int>::iterator end = v.end();
  105.         end--;
  106.         if(sum[block].qtd == 0) sum[block].ini = end;
  107.         sum[block].qtd++;
  108.         if(sum[block].qtd == bs){
  109.             sum[block].fim=end;
  110.             block++;
  111.         }
  112.     }
  113. }
  114.  
  115. void printlist(){
  116.     for(auto it = v.begin();it!=v.end();it++){
  117.         printf("%d ",*it );
  118.     }
  119.     printf("\n");
  120. }
  121.  
  122. int main () {
  123.     scanf("%d", &n);   
  124.     bs =sqrt(maxn);
  125.  
  126.     for (int i = 0; i < n; i++) {
  127.         scanf("%d", in+i);
  128.     }
  129.     build();
  130.  
  131.     int q; scanf("%d", &q);
  132.  
  133.     while(q--) {
  134.         //printlist();
  135.         char c; scanf(" %c", &c);
  136.         if (c == 'I') {
  137.             int p, e; scanf("%d %d", &p, &e);
  138.             insert(p,e);
  139.         } else if (c == 'R') {
  140.             int e; scanf("%d", &e);
  141.             remove(e);
  142.         } else {
  143.             int a, b; scanf("%d %d", &a, &b);
  144.             if(pos[a]>pos[b]){
  145.                 int aux =a;
  146.                 a = b;
  147.                 b = aux;
  148.             }else if(pos[a] == pos[b]){
  149.                 for(auto it = sum[pos[a]].ini;it!=sum[pos[a]].fim;it++){
  150.                     if(*it == a)break;
  151.                     if(*it == b){
  152.                         int aux = a;
  153.                         a = b;
  154.                         b = aux;
  155.                         break;
  156.                     }
  157.                 }
  158.             }
  159.             printf("%d\n",query(a,b) );
  160.         }
  161.     }
  162.  
  163.     return 0;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement