ismaeil

Algo pro log n

Jul 23rd, 2020
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.61 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef pair<int,int> ii;
  5. const int N = 1e5 + 5e1;
  6. #define rg (p << 1) | 1
  7. #define lf (p << 1)
  8. #define OO INT_MAX
  9. #define S second
  10. #define F first
  11.  
  12. int Tree[N][2] ,idx[N];
  13. int n ,Q;
  14.  
  15. int DfsSpTree[N * 2][2]; /// DFS Spanning Tree
  16. int DfsSize = 1;    /// DFS Spanning Tree Size
  17.  
  18. class SegTree {
  19.     private:
  20.             vector< ii > Seg;
  21.  
  22.             ii Compain(ii a ,ii b) {
  23.                     return (a.S < b.S) ? a : b;
  24.             }
  25.  
  26.             void Build(int p ,int L ,int R){
  27.                     if( L == R ) {
  28.                             Seg[p] = ii( DfsSpTree[L][0] ,DfsSpTree[L][1] );
  29.                             return;
  30.                     }
  31.  
  32.                     int Mid = (L + R) >> 1;
  33.                     Build(lf ,L ,Mid);
  34.                     Build(rg ,Mid + 1 ,R);
  35.                     Seg[p] = Compain(Seg[lf] ,Seg[rg]);
  36.             }
  37.  
  38.             ii QRY(int i ,int j ,int p ,int L ,int R){
  39.                     if( i >  R || L >  j ) return ii(OO ,OO);
  40.                     if( i <= L && R <= j ) return Seg[p];
  41.  
  42.                     int Mid = (L + R) >> 1;
  43.                     ii LF  = QRY(i ,j ,lf ,L ,Mid);
  44.                     ii RG  = QRY(i ,j ,rg ,Mid + 1 ,R);
  45.                     return Compain(LF ,RG);
  46.             }
  47.  
  48.     public:
  49.             void Build() {
  50.                     Build(1 ,1 ,DfsSize - 1);
  51.             }
  52.             void ReSize(int n) {
  53.                     Seg.assign(n * 4 ,ii(0,OO));
  54.             }
  55.             ii QRY(int L ,int R) {
  56.                     return QRY(L ,R ,1 ,1 ,DfsSize - 1);
  57.             }
  58. } ST ;
  59.  
  60. void Dfs_Pre_Order(int u = 1 ,int Depth = 1) {
  61.         DfsSpTree[ DfsSize ][0] = u;
  62.         DfsSpTree[ DfsSize ][1] = Depth;
  63.  
  64.         if(idx[u] == -1) idx[u] = DfsSize;
  65.         DfsSize ++;
  66.  
  67.         for(int j = 0 ; j < 2 ; j++){
  68.                 int v = Tree[u][j];
  69.                 if( v != -1 ){
  70.                         Dfs_Pre_Order(v ,Depth + 1);
  71.                         DfsSpTree[ DfsSize ][0] = u;
  72.                         DfsSpTree[ DfsSize ][1] = Depth;
  73.                         DfsSize++;
  74.                 }
  75.         }
  76. }
  77.  
  78. /// ------------------------------------------------------------------ ///
  79.  
  80. void SolveQuery(){
  81.         int u ,v;
  82.         scanf("%d%d" ,&u ,&v);
  83.  
  84.         int L = idx[u] ,R = idx[v];
  85.         if( L > R )swap(L ,R);
  86.         ii Ans = ST.QRY(L ,R);
  87.  
  88.         printf("%d\n" ,Ans.F);
  89. }
  90.  
  91. int main() {
  92.         scanf("%d" ,&n);
  93.         for(int i=1 ; i <= n ; i++) {
  94.                 int L ,R; scanf("%d%d" ,&L ,&R);
  95.                 Tree[i][0] = L;
  96.                 Tree[i][1] = R;
  97.         }
  98.  
  99.         memset(idx ,-1 ,sizeof idx);
  100.         Dfs_Pre_Order();
  101.  
  102.         ST.ReSize(DfsSize + 1);
  103.         ST.Build();
  104.  
  105.         scanf("%d" ,&Q);
  106.         while( Q-- ) SolveQuery();
  107.     return 0;
  108. }
  109.  
  110.  
  111. /// --- \STUFF --- ///
  112. /*
  113.     void PrintDfsSpTree(){
  114.         cout << "DFS ST Size = " << DfsSize << endl;
  115.         for(int i = 1 ; i < DfsSize ; i++)
  116.             cout << DfsSpTree[i][0] << ' '; cout << endl;
  117.         for(int i = 1 ; i < DfsSize ; i++)
  118.             cout << DfsSpTree[i][1] << ' '; cout << endl;
  119.     }
  120.  
  121. /// --- \TestCases --- ///
  122.  
  123. INPUT:
  124. 6
  125. 2 3
  126. 4 5
  127. -1 -1
  128. 6 -1
  129. -1 -1
  130. -1 -1
  131. 2
  132. 2 3
  133. 6 3
  134.  
  135. OUTPUT:
  136. 1
  137. 2
  138. ---------------------------------
  139.  
  140. INPUT:
  141. 7
  142. 2 3
  143. 4 5
  144. -1 6
  145. -1 -1
  146. -1 7
  147. -1 -1
  148. -1 -1
  149. 4
  150. 5 6
  151. 4 3
  152. 7 6
  153. 6 1
  154.  
  155. OUTPUT:
  156. 1
  157. 1 || 2
  158. 1 || 2
  159. 3
  160. ---------------------------------
  161.  
  162. INPUT:
  163. 14
  164. 2 3
  165. 4 5
  166. 6 7
  167. 10 -1
  168. -1 -1
  169. 8 9
  170. -1 -1
  171. 11 12
  172. 13 14
  173. -1 -1
  174. -1 -1
  175. -1 -1
  176. -1 -1
  177. -1 -1
  178. 10
  179. 11 7
  180. 10 5
  181. 8 9
  182. 6 1
  183. 13 8
  184. 13 14
  185. 9 7
  186. 4 5
  187. 10 12
  188. 2 8
  189.  
  190. OUTPUT:
  191.  
  192.  
  193. */
  194.  
Advertisement
Add Comment
Please, Sign In to add comment