HdU5266 (LCA + line segment tree) and hdu5266lca line segment tree

Source: Internet
Author: User

HdU5266 (LCA + line segment tree) and hdu5266lca line segment tree

Pog loves szh III Accepts: 63 Submissions: 483 Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Problem Description
Pog is playing games with szh. First, pog draws a rooted tree on the paper. Here we define 1 as the root of the tree, and then szh selects several points in the tree, I want pog to help me find out where the nearest common ancestor of these points is. A vertex is the nearest common ancestor of S. Only when the subtree with the vertex as the root contains all vertices in S, and the depth is the maximum. However, this problem is very difficult. Out of szh's love for pog, he decided to only find the serial number continuous point, that is
 
  
Li
 ~
 
  
Ri
 .
Input description
Several groups of data (no more
 
  
3
 Group
 
  
N ≥ 10000
 Or
 
  
Q ≥ 10000
 ). The first line of each group of data is an integer.
 
  
N (1 ≤ n ≤ 300000)
 Number of nodes in the tree. Next
 
  
N−1
 Row, two numbers per line
 
  
Ai, Bi
 Indicates that an edge exists to connect the two nodes. Number of the next row
 
  
Q (1 ≤ q≤300000)
 , Indicates that
 
  
Q
 Group inquiry. Next, there are two numbers in each row in the Q row.
 
  
Li, ri (1 ≤ li ≤ ri ≤ n)
 Indicates that the query number is
 
  
Li
 ~
 
  
Ri
 The nearest common ancestor of the vertex.
Output description
For each query in each group, one line is output, indicating the number is li ~ Number of the nearest common ancestor of the ri point.
Input example
51 21 33 44 551 22 33 43 51 5
Output example
11331
Hint
Cherish life and stay away from Stack explosion.
Statistic | Submit | Clarifications | Back

After preprocessing, the LCA is O (1), and then the query result can be obtained in the logn time using the line segment tree!


#pragma comment(linker, "/STACK:102400000,102400000")#include <cstdio>#include <vector>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const int maxn = 3e5 + 10;const int inf = 1e8;int pos[maxn],depth[maxn],head[maxn];int d[maxn<<1][20],width[maxn<<1],tot,e;typedef pair<int,int> Edge;Edge edges[maxn<<1];void AddEdge(int u,int v){    edges[++e] = make_pair(v,head[u]);head[u] = e;    edges[++e] = make_pair(u,head[v]);head[v] = e;}void pre(int u,int fa,int dep = 0){    d[++tot][0] = u;    if(!pos[u]) {        pos[u] = tot;        depth[u] = dep;     }    for(int eid = head[u]; eid ; eid = edges[eid].second) {        int &v = edges[eid].first;        if(v==fa)continue;        pre(v,u,dep+1);        d[++tot][0] = u;    }}void RMQ_init(int n){    for(int j = 1; (1<<j) <= n; j++) {        for(int i = 1; i + (1<<j) - 1 <= n; i++) {            d[i][j] = depth[d[i][j-1]] < depth[d[i+(1<<(j-1))][j-1]] ? d[i][j-1] : d[i+(1<<(j-1))][j-1];        }    }    for(int i = 1,w = 1; i <= n; i++) {        if((1<<w) <= i) w++;        width[i] = w;    }}int LCA(int u,int v){    int L = pos[u],R = pos[v];    if(L > R) swap(L,R);    int k = width[R-L+1] - 1;    return depth[d[L][k]] < depth[d[R-(1<<k)+1][k]] ? d[L][k] : d[R-(1<<k)+1][k];}int seg[maxn<<1];void built(int o,int L,int R){    if(L==R) {        seg[o] = L;        return;    }    int mid = (L + R) >> 1;    built(o<<1,L,mid);    built(o<<1|1,mid+1,R);    seg[o] = LCA(seg[o<<1],seg[o<<1|1]);}int ql,qr;int Query(int o,int L,int R){    if(ql<=L&&qr>=R) {        return seg[o];    }    int mid = (L + R) >> 1;    int ans = -1;    if(ql <= mid) ans = Query(o<<1,L,mid);    if(qr > mid) {        int t = Query(o<<1|1,mid+1,R);        if(ans==-1) ans = t;        else ans = LCA(ans,t);    }    return ans;}int main(int argc, char const *argv[]){    int n;    while(scanf("%d",&n)==1) {        memset(head,0,sizeof(head[0])*(n+1));        for(int i = 1; i < n; i++) {            int u, v;scanf("%d%d",&u,&v);            AddEdge(u,v);        }        e = tot = 0;        memset(pos,0,sizeof(pos[0])*(n+1));        pre(1,-1);        RMQ_init(tot);        built(1,1,n);        int Q;scanf("%d",&Q);        while(Q--) {            scanf("%d%d",&ql,&qr);            if(ql > qr) swap(ql,qr);            printf("%d\n", Query(1,1,n));        }     }    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.