bingxuan9112

treap題 debug中

Apr 4th, 2019
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.14 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define REP(i,l,r) for(int i=(l);i<(r);i++)
  3. #define debug(x) 1&&(cout<<#x<<':'<<x<<'\n')
  4. #define siz(v) (ll(v.size()))
  5. #define all(v) begin(v),end(v)
  6. #define pb emplace_back
  7. #define ff first
  8. #define ss second
  9.  
  10. using namespace std;
  11. typedef long long ll;
  12. typedef pair<ll,ll> pll;
  13. constexpr ll N = 100000, INF = 1e18;
  14.  
  15. struct node{
  16.     int sz,val;
  17.     node *l,*r,*p;
  18.     node(int val=0):l(0),r(0),p(0),sz(1),val(val){}
  19.     void pull(){
  20.         sz = 1, p = nullptr;
  21.         //cout<<"val:"<<val+1<<'\n';
  22.         if(l)sz += l->sz, l->p = this;
  23.         if(r)sz += r->sz, r->p = this;
  24.     }
  25. } *nodes[N+1];
  26. ll n,m,v[N+1] = {},pos[N+1] = {};
  27. ll rseed=712;
  28. ll RAND(){return rseed=(rseed*0xdefaced)%1000000007;}
  29. int size(node *p){return p?p->sz:0;}
  30. node *anc(node *x){
  31.     if(!x)return nullptr;
  32.     while(x->p)x = x->p;
  33.     return x;
  34. }
  35. ll orderof(node *x){
  36.     if(!x)return 0;
  37.     ll c = size(x->l);
  38.     while(x->p){
  39.         if(x==x->p->r) c+=size(x->p->l)+1;
  40.         x = x->p;
  41.     }
  42.     return c;
  43. }
  44. node *merges(node *a,node *b){
  45.     if(!a||!b)return a?a:b;
  46.     if(RAND()&4) return a->r = merges(a->r,b), a->pull(), a;
  47.     else return b->l = merges(a,b->l), b->pull(), b;
  48. }
  49. void split(node *o, node *&a, node *&b, int R){
  50.     if(!o) return a=b=nullptr,void();
  51.     if(R <= size(o->l))b=o,split(o->l,a,b->l,R);
  52.     else a=o,split(o->r,a->r,b,R-size(o->l)-1);
  53.     o->pull();
  54. }
  55. void traversal(node *o,int d=0){
  56.     if(!o)return;
  57.     traversal(o->l,d+1);
  58.     REP(i,0,d) cout << '\t'; cout << "val:" << o->val+1 << '\n';
  59.     traversal(o->r,d+1);
  60. }
  61. signed main(){
  62.     //ios::sync_with_stdio(0),cin.tie(0);
  63.     ll res,x,y;
  64.     cin >> n >> m;
  65.     res = n;
  66.     REP(i,0,n) cin >> v[i], --v[i], pos[v[i]]=i;
  67.     REP(i,0,n) if(!nodes[i]){
  68.         node *r = nullptr;
  69.         int j = i;
  70.         do nodes[j] = new node(j), r = merges(r,nodes[j]), j = v[j];
  71.         while(j != i);
  72.         res--;
  73.     }
  74.     cout << res << '\n';
  75.     //cout << "======================\n";
  76.     //REP(k,0,n) if(nodes[k]==anc(nodes[k]))cout << "......\n",traversal(nodes[k]);
  77.     REP(_,0,m){
  78.         cin >> x >> y, --x, --y;
  79.         x = pos[x], y = pos[y];
  80.         swap(pos[v[x]],pos[v[y]]);
  81.         swap(v[x],v[y]);
  82.         node *u,*v,*t,*w;
  83.         node *a=nodes[x],*b=nodes[y];
  84.         if(anc(a) == anc(b)){
  85.             res--;
  86.             split(anc(a),u,v,orderof(a)+1);
  87.             //cout<<"OAO\n",traversal(anc(u)),cout<<"OAO\n",traversal(anc(v));
  88.             merges(v,u);
  89.             //cout<<"= =\n",traversal(anc(a));
  90.             //orderof(b);
  91.             //debug(orderof(b)+1);
  92.             split(anc(b),u,v,orderof(b)+1);
  93.             //cout<<"OAO\n",traversal(anc(a)),cout<<"OAO\n",traversal(anc(b));
  94.             assert(anc(a)!=anc(b));
  95.         }else{
  96.             res++;
  97.             split(anc(a),u,v,orderof(a)), merges(v,u);
  98.             split(anc(b),u,v,orderof(b)+1),merges(v,u);
  99.             merges(anc(a),anc(b));
  100.             assert(anc(a)==anc(b));
  101.         }
  102.         cout << res << '\n';
  103.         //cout << "======================\n";
  104.         //REP(k,0,n) if(nodes[k]==anc(nodes[k]))cout << "......\n",traversal(nodes[k]);
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment