Dang_Quan_10_Tin

HSGO 2020 PERMSWAP (dsu rollback + SQRT Decomposition)

Apr 16th, 2022
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.96 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ll int
  5. #define pb push_back
  6. #define pll pair<ll,ll>
  7. #define ff first
  8. #define ss second
  9. #define endl "\n"
  10. const ll maxn=1e5+10;
  11. const ll base=700;
  12. const ll mod=998244353;
  13. const long double eps=1e-10;
  14.  
  15. ll a[maxn];
  16. ll x[maxn];
  17. ll y[maxn];
  18. ll n, q;
  19. bool dd[maxn];
  20. ll get()
  21. {
  22.     ll ans=0;
  23.     for (int i=1; i<=n; i++)
  24.     {
  25.         if (dd[i])
  26.             continue;
  27.         ll h=i;
  28.         dd[i]=1;
  29.         while (1)
  30.         {
  31.             ll nxt=a[h];
  32.             if (nxt==i)
  33.                 break;
  34.             dd[nxt]=1;
  35.             h=nxt;
  36.         }
  37.         ans++;
  38.     }
  39.     return n-ans;
  40. }
  41.  
  42. bool chk[maxn];
  43. ll res[maxn];
  44. ll nxt[maxn];
  45.  
  46. int main()
  47. {
  48.     ios_base::sync_with_stdio(false);
  49.     cin.tie(0);
  50.     cout.tie(0);
  51.     if (fopen("t.inp", "r"))
  52.     {
  53.         freopen("test.inp", "r", stdin);
  54.         freopen("test.out", "w", stdout) ;
  55.     }
  56.     cin>> n>> q;
  57.     for (int i=1; i<=n; i++)
  58.         cin>>a[i];
  59.     ll ans=get();
  60.     for (int i=1; i<=q; i++)
  61.     {
  62.         cin>>x[i]>>y[i];
  63.     }
  64.     for (int i=1; i<=n; i++)
  65.         dd[i]=0;
  66.     //  cout <<ans<<endl;
  67.     for (int i=1; i<=q; i+=base)
  68.     {
  69.         vector<ll> vt;
  70.         ll l=i;
  71.         ll r=min(i+base-1,q);
  72.         for (int j=l; j<=r; j++)
  73.         {
  74.             dd[x[j]]=1;
  75.             dd[a[x[j]]]=1;
  76.             dd[y[j]]=1;
  77.             dd[a[y[j]]]=1;
  78.             vt.pb(x[j]);
  79.             vt.pb(a[x[j]]);
  80.             vt.pb(y[j]);
  81.             vt.pb(a[y[j]]);
  82.             swap(a[x[j]],a[y[j]]);
  83.         }
  84.         for (int j=r;j>=l;j--) swap(a[x[j]],a[y[j]]);
  85.  
  86.         ll cnt=0;
  87.         for (int i=1; i<=n; i++)
  88.         {
  89.             if (chk[i])
  90.                 continue;
  91.             if (!dd[i])
  92.                 continue;
  93.             ll h=i;
  94.             chk[i]=1;
  95.             ll pre=i;
  96.             while (1)
  97.             {
  98.                 ll nxt1=a[h];
  99.                 if (dd[nxt1])
  100.                 {
  101.                     nxt[pre]=nxt1;
  102.                     pre=nxt1;
  103.                 }
  104.                 if (nxt1==i)
  105.                     break;
  106.  
  107.                 chk[nxt1]=1;
  108.                 h=nxt1;
  109.             }
  110.         }
  111.         //for (int i=1;i<=n;i++) cout <<nxt[i]<<endl;
  112.      //   cout <<"WTF"<<endl;
  113.         for (int j=l; j<=r; j++)
  114.         {
  115.             ll p=x[j];
  116.             bool kt=false;
  117.             while (1)
  118.             {
  119.                 if (p==y[j])
  120.                 {
  121.                     kt=true;
  122.                     break;
  123.                 }
  124.                 if (nxt[p]==x[j]) break;
  125.                 p=nxt[p];
  126.             }
  127.             if (!kt) ans++;
  128.             else ans--;
  129.             res[j]=ans;
  130.             nxt[x[j]]=a[y[j]];
  131.             nxt[y[j]]=a[x[j]];
  132.             swap(a[x[j]],a[y[j]]);
  133.         }
  134.  
  135.         for (int i=1; i<=n; i++)
  136.             chk[i]=0;
  137.         for (auto to:vt) dd[to]=0;
  138.  
  139.  
  140.     }
  141.     for (int i=1; i<=q; i++)
  142.         cout <<res[i]<<endl;
  143.  
  144.  
  145.  
  146. }
Add Comment
Please, Sign In to add comment