HDU 5266 pog loves szh III ( LCA + SegTree||RMQ )

pog loves szh III

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 470    Accepted Submission(s): 97


Problem Description
Pog and Szh are playing games. Firstly Pog draw a tree on the paper. Here we define 1 as the root of the tree.Then Szh choose some nodes from the tree. He wants Pog helps to find the least common ancestor (LCA) of these node.The question is too difficult for Pog.So he decided to simplify the problems.The nodes picked are consecutive numbers from  li to ri ([li,ri]).

Hint : You should be careful about stack overflow !
 

 

Input
Several groups of data (no more than 3 groups, n10000 or Q10000).

The following line contains ans integers,n(2n300000).

AT The following n1 line, two integers are bi and ci at every line, it shows an edge connecting bi and ci

The following line contains ans integers,Q(Q300000).

AT The following Q line contains two integers li and ri(1lirin).
 

 

Output
For each case,output  Q integers means the LCA of [li,ri].
 

 

Sample Input
5
1 2
1 3
3 4
4 5
5
1 2
2 3
3 4
3 5
1 5
 

 

Sample Output
1
1
3
3
1
Hint
Be careful about stack overflow.

 

这个题有点裸,以为LCA是满足结合律的,所以直接拿个线段树来搞就可以了~

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cstdio>
using namespace std ;
typedef long long LL ;
const int N = 400030 ;
const int DEG = 30 ;

int n , lca[N<<2] ;

struct Edge
{
    int to,next;
}edge[N*2];

int head[N],tot;

void addedge(int u,int v)
{
    edge[tot].to = v;
    edge[tot].next = head[u];
    head[u] = tot++;
}

void init()
{
    tot = 0;
    memset(head,-1,sizeof(head));
}

int fa[N][DEG] , deg[N];

void BFS(int root)
{
    queue<int>que;
    deg[root] = 0;
    fa[root][0] = root;
    que.push(root);
    while(!que.empty())
    {
        int tmp = que.front();
        que.pop();
        for(int i = 1;i < DEG;i++)
            fa[tmp][i] = fa[fa[tmp][i-1]][i-1];
        for(int i = head[tmp]; i != -1;i = edge[i].next)
        {
            int v = edge[i].to;
            if(v == fa[tmp][0])continue;
            deg[v] = deg[tmp] + 1;
            fa[v][0] = tmp;
            que.push(v);
        }
    }
}

int LCA(int u,int v)
{
    if(deg[u] > deg[v])swap(u,v);
    int hu = deg[u], hv = deg[v];
    int tu = u, tv = v;
    for(int det = hv-hu, i = 0; det ;det>>=1, i++)
        if(det&1)
            tv = fa[tv][i];
    if(tu == tv)return tu;
    for(int i = DEG-1; i >= 0; i--)
    {
        if(fa[tu][i] == fa[tv][i])
            continue;
        tu = fa[tu][i];
        tv = fa[tv][i];
    }
    return fa[tu][0];
}

#define root 1,n,1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define lr rt<<1
#define rr rt<<1|1

void Up( int rt ) {
    lca[rt] = LCA( lca[lr] , lca[rr] ) ;
}

void build( int l , int r , int rt ) {
    if( l == r ) {
        lca[rt] = l ;
        return ;
    }
    int mid = (l+r) >> 1 ;
    build(lson),build(rson);
    Up(rt);
}

int query( int l , int r , int rt , int L , int R ) {
    if( L == l && r == R ) return lca[rt] ;
    int mid = (l+r) >> 1 ;
    if( R <= mid ) return query( lson , L , R ) ;
    else if( L > mid ) return query( rson , L , R ) ;
    else return LCA( query( lson , L , mid ) , query( rson , mid + 1 , R ) ) ;
}

int main() {
    #ifdef LOCAL
        freopen("in.txt","r",stdin);
    #endif // LOCAL
    while( ~scanf("%d",&n) ) {
        init();
        for( int i = 1 ; i < n ; ++i ) {
            int u , v ; scanf("%d%d",&u,&v);
            addedge( u , v ) ;
            addedge( v , u ) ;
        }
        BFS(1);
        build(root) ;
        int q ; scanf("%d",&q);
        while( q-- ) {
            int x , y ; scanf("%d%d",&x,&y);
            printf("%d\n",query(root,x,y));
        }
    }
    return 0 ;
}
View Code

 

转载于:https://www.cnblogs.com/hlmark/p/4562397.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值