Morass

Fast LCA

Jun 29th, 2017
459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.62 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define PB push_back
  4. #define ZERO (1e-10)
  5. #define INF (1<<29)
  6. #define CL(A,I) (memset(A,I,sizeof(A)))
  7. #define DEB printf("DEB!\n");
  8. #define D(X) cout<<"  "<<#X": "<<X<<endl;
  9. #define EQ(A,B) (A+ZERO>B&&A-ZERO<B)
  10. typedef long long ll;
  11. typedef long double ld;
  12. typedef pair<ll,ll> pll;
  13. typedef vector<int> vi;
  14. typedef pair<int,int> ii;
  15. typedef vector<ii> vii;
  16. #define IN(n) int n; cin >> n;
  17. #define FOR(i, m, n) for (int i(m); i < n; i++)
  18. #define REP(i, n) FOR(i, 0, n)
  19. #define F(n) REP(i, n)
  20. #define FF(n) REP(j, n)
  21. #define FT(m, n) FOR(k, m, n)
  22. #define aa first
  23. #define bb second
  24. void ga(int N,int *A){F(N)scanf("%d",A+i);}
  25. #define LG (20)
  26. #define MX (1<<LG)
  27. #define BT (12) //K =~= log(N)/2
  28. #define SZ (MX/BT+1)
  29. #define P2(v) (!(v&(v-1)))
  30. //structure for RMQ which differs exactly by 1
  31. struct RMQ{
  32.     /*
  33.      * T[b][i][j]: Smallest element on interval i→ j, in gradient of shape "b"
  34.      * G: Precalculate logarithms
  35.      * B: Bitmask of gradients of each interval of size BT
  36.      * I: Lowest element on each little interval
  37.      * E: Value of lowest element on each interval
  38.      * dp: RMQ: Sparse table
  39.      */
  40.     char T[1<<BT][BT][BT],G[MX];
  41.     short B[SZ];
  42.     int X,I[SZ],E[SZ],O=-1,dp[SZ][LG];
  43.     //Precaltucaliton of table T: easy simulation of process for each possible bitmask
  44.     //Complexity is B^2*2^B
  45.     void tb(int B){
  46.         F(1<<B)FF(B+1){
  47.             int d=0,b=0,I=j;
  48.             FT(j,B+1){
  49.                 T[i][j][k]=I;
  50.                 d+=i&1<<k?1:-1;
  51.                 if(b>d)b=d,I=k+1;
  52.             }
  53.         }
  54.     }
  55.     //Makes bitmask of an interval
  56.     //It is just BT-1 (not BT) since we are interested in differences only
  57.     int bt(int*A){
  58.         int b=0;
  59.         //Check difference and shift it to i-th position
  60.         F(BT-1)b|=(A[i+1]>A[i])<<i;
  61.         return b;
  62.     }
  63.     //Body of function which calls other functions - setting proper values to arrays
  64.     void ini(int N,int*A){
  65.         if(!X++){tb(BT-1);FT(1,MX)G[k]=O+=P2(k);}
  66.         F(BT*2)A[N+i]=A[N+i-1]+1;
  67.         F(N/BT+1)E[i]=*min_element(A+i*BT,A+i*BT+BT),I[i]=min_element(A+i*BT,A+i*BT+BT)-A,B[i]=bt(A+i*BT);
  68.         ST(N/BT+1);
  69.     }
  70.     //Function which calculates SPARSE TIBLE (of sizeN/BT)
  71.     void ST(int N){
  72.         F(N)dp[i][0]=i;
  73.         //Precalcute so that there is index to array "E".
  74.         //Length 2^i: Minimal value from index i to i+2^i-1
  75.         //Counting intervals from smaller to bigger
  76.         FT(1,k-(1<<k)+N+1)F(N+1-(1<<k))
  77.             if(E[dp[i][k-1]]<E[dp[i+(1<<(k-1))][k-1]])
  78.                 dp[i][k]=dp[i][k-1];
  79.             else dp[i][k]=dp[i+(1<<(k-1))][k-1];
  80.     }
  81.     //Returns minimal value on L → R interval
  82.     //j: logarithm of interval-size
  83.     //Taking interval from L to L+2^j-1 and from R-2^j+1 to R, and takes
  84.     //minimum of thos (it will surely cover the whole interval)
  85.     int QQ(int L,int R,int*A){
  86.         int j=G[R-L+1];return A[dp[L][j]]<=A[dp[R-(1<<j)+1][j]]?dp[L][j]:dp[R-(1<<j)+1][j];
  87.     }
  88.     inline int sc(int a){return a-a%BT;}
  89.     //Query to interval of size BT
  90.     //Determines borders of interval
  91.     void qp(int L,int R,int&b,int&J,int*A){
  92.         int u=T[B[L/BT]][L%BT][R%BT];
  93.         if(b>A[sc(L)+u])b=A[J=sc(L)+u];
  94.     }
  95.     //Function which solves RMQ on interval
  96.     //It solves it in multiple steps: it solves it on the BIG interval + it solves the
  97.     //borders of it
  98.     //All call are O(1)
  99.     int qy(int L,int R,int*A){
  100.         int a,b=INF,J=-1;
  101.         if(L/BT==R/BT)return qp(L,R,b,J,A),J;
  102.         if(L%BT)a=L,L=sc(L)+BT,qp(a,L-1,b,J,A);
  103.         if(R%BT+1!=BT)a=R,R=sc(R)-1,qp(R+1,a,b,J,A);
  104.         if(L<=R&&E[a=QQ(L/BT,R/BT,E)]<b)b=a,J=I[a];
  105.         return J;
  106.     }
  107. };
  108. struct LCA{
  109.     /*
  110.      * R: RMQ for +/-1
  111.      * g: Edges
  112.      * l: Size of E
  113.      * E: Flattened tree in rder of Euler Tour (with nesting)
  114.      * L: Depth of node
  115.      * H: Index of first position of node int 'E'
  116.      * D: Depth of node. With parallel to 'E'
  117.      */
  118.     RMQ R;
  119.     vi g[MX];
  120.     int l,E[MX],L[MX],H[MX],D[MX];
  121.     //dfs for euler tour: Node, Parent, Depth, Indext to E
  122.     void dfs(int u,int p,int d,int&l){
  123.         H[u]=l,L[u]=d++,E[l]=u,D[l++]=L[u];
  124.         for(auto&h:g[u])if(h^p)dfs(h,u,d,l),E[l]=u,D[l++]=L[u];
  125.     }
  126.     //Init - nothing interesting (just calls)
  127.     void pre(int N,int r=0){dfs(r,r,0,l=0),R.ini(l,D);}
  128.     //Returns LCA: Returns value of E on index given by RMQ
  129.     int lca(int a,int b){a=H[a],b=H[b];if(a>b)swap(a,b);return E[R.qy(a,b,D)];}
  130.     //Adds edge
  131.     void ADD(int A,int B){g[A].PB(B),g[B].PB(A);}
  132.     //Clears graph
  133.     void CLR(){for(auto&h:g)h.clear();}
  134. }L;
Advertisement
Add Comment
Please, Sign In to add comment