zoukankan      html  css  js  c++  java
  • 【最近公共祖先】【线段树】URAL

    Few people know, but a long time ago a developed state existed on Mars. It consisted of n cities, numbered by integers from 1 to n, the capital had the number 1. Some pairs of cities were connected by a road. The residents of the state were very prudent, therefore, between any two cities, there was exactly one path (possibly consisting of several roads).
    Due to the fact that the state was developed, its residents loved traveling. Tourist route on Mars was described by two numbers L and R. This meant that the tourist started the route in the city L, then went to the city L + 1 (without going into the cities, that did not lie on the path between L and L + 1), then went to the city L + 2, and so on. The last city on the route was the city R. A city that was the closest to the capital among all cities visited on this route (if to count a distance between the cities by the roads) was considered the main attraction of the route.
    Knowing the map of the Martian state and all the tourist routes, find for each route its main attraction.

    Input

    The first line contains an integer n that is the number of cities (1 ≤ n ≤ 2 · 105).
    The following n − 1 lines describe the roads. Each road is described by two numbers of cities that are connected by it (1 ≤  v i,  u i ≤  nv i ≠  u i).
    The ( n + 1)-th line contains an integer q that is the number of tourist routes (0 ≤ q ≤ 10 6).
    Then q lines describe the routes themselves. Each route is described by a pair of integers L iR i (1 ≤ L i ≤ R i ≤ n).

    Output

    Output q integers, one per line — for each route the number of its main attraction. These numbers should be output in the same order in which the respective routes were described.

    Example

    inputoutput
    7
    1 2
    1 3
    2 4
    2 5
    2 6
    3 7
    3
    4 6
    3 4
    5 7
    
    2
    1
    1
    
    7
    1 3
    3 5
    5 6
    7 5
    1 4
    2 4
    3
    4 5
    5 6
    6 7
    
    1
    5
    5
    

    Notes

    This problem has a big input and output data size and a strict Time Limit. If you write your solution in C++ we recommend you to use Visual C++ 2013 compiler.

    题意就不描述了,是个水题,预处理出i和i+1的lca,然后询问的时候跑线段树就行,这里线段树用到了寻找区间中最小的点的位置,存一下。

    懒得改非递归,要手动开栈。

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    #define N 200010
    int n,m;
    int v[N<<1],__next[N<<1],first[N],e;
    void AddEdge(int U,int V)
    {
        v[++e]=V;
        __next[e]=first[U];
        first[U]=e;
    }
    int fa[N],dep[N],top[N],son[N],siz[N],tot;
    void dfs(int U)
    {
        siz[U]=1;
        for(int i=first[U];i;i=__next[i])
          if(v[i]!=fa[U])
            {
              fa[v[i]]=U;
              dep[v[i]]=dep[U]+1;
              dfs(v[i]);
              siz[U]+=siz[v[i]];
              if(siz[v[i]]>siz[son[U]])
                son[U]=v[i];
            }
    }
    void df2(int U)
    {
        if(son[U])
          {
            top[son[U]]=top[U];
            df2(son[U]);
          }
        for(int i=first[U];i;i=__next[i])
          if(v[i]!=fa[U]&&v[i]!=son[U])
            {
              top[v[i]]=v[i];
              df2(v[i]);
            }
    }
    int lca(int U,int V)
    {
        while(top[U]!=top[V])
          {
            if(dep[top[U]]<dep[top[V]])
              swap(U,V);
            U=fa[top[U]];
          }
        if(dep[U]>dep[V])
          swap(U,V);
        return U;
    }
    int minv[N<<2];
    void update(int p,int v,int rt,int l,int r)
    {
    	if(l==r)
    	  {
    	  	minv[rt]=v;
    	  	return;
    	  }
    	int m=(l+r>>1);
    	if(p<=m) update(p,v,rt<<1,l,m);
    	else update(p,v,rt<<1|1,m+1,r);
    	minv[rt]=min(minv[rt<<1],minv[rt<<1|1]);
    }
    int lcas[N];
    int query(int ql,int qr,int rt,int l,int r)
    {
    	if(ql<=l&&r<=qr) return minv[rt];
    	int m=(l+r>>1),res=2147483647;
    	if(ql<=m) res=min(res,query(ql,qr,rt<<1,l,m));
        if(m<qr) res=min(res,query(ql,qr,rt<<1|1,m+1,r));
        return res;
    }
    int Find2(int v,int rt,int l,int r)
    {
    	if(l==r) return l;
    	int m=(l+r>>1);
    	if(minv[rt<<1]==v) return Find2(v,rt<<1,l,m);
    	else return Find2(v,rt<<1|1,m+1,r);
    }
    int Findp(int ql,int qr,int v,int rt,int l,int r)
    {
    	if(ql<=l&&r<=qr)
    	  {
    	  	if(minv[rt]==v)
    	  	  return Find2(v,rt,l,r);
    	  	return -1;
    	  }
    	int m=(l+r>>1);
    	if(ql<=m)
    	  {
    	  	int tmp=Findp(ql,qr,v,rt<<1,l,m);
    		if(tmp!=-1) return tmp;
    	  }
    	return Findp(ql,qr,v,rt<<1|1,m+1,r);
    }
    int main()
    {
    //	freopen("j.in","r",stdin);
    	int x,y;
    	scanf("%d",&n);
    	for(int i=1;i<n;++i)
          {
            scanf("%d%d",&x,&y);
            AddEdge(x,y);
            AddEdge(y,x);
          }
        top[1]=1;
        dfs(1);
        df2(1);
    	for(int i=1;i<n;++i)
    	  {
    	  	lcas[i]=lca(i,i+1);
    	  	update(i,dep[lcas[i]],1,1,n-1);
    	  }
    	scanf("%d",&m);
    	for(int i=1;i<=m;++i)
    	  {
    	  	scanf("%d%d",&x,&y);
    	  	if(x==y)
    		  printf("%d
    ",x);
    		else
    	  	  printf("%d
    ",lcas[Findp(x,y-1,query(x,y-1,1,1,n-1),1,1,n-1)]);
    	  }
    	return 0;
    }
  • 相关阅读:
    webkit and the browser
    【技术原创】京东商城价格图片分析解析源代码下载(C#),附演示程序 转
    maglev need 64bit linux version
    sed awk
    VMware公司SpringSource部门宣布收购Gemstone Systems公司
    新手?为什么需要关怀?
    【技术原创】京东商城价格图片分析解析源代码下载(C#),附演示程序 转
    【技术原创】京东商城价格图片分析解析源代码下载(C#),附演示程序 转
    C语言正则表达式库pcre介绍
    人工翻译
  • 原文地址:https://www.cnblogs.com/autsky-jadek/p/6337636.html
Copyright © 2011-2022 走看看