Tourism on Mars URAL - 2109 (LCA,线段树)

2023-11-09 16:11
文章标签 lca 线段 ural mars 2109 tourism

本文主要是介绍Tourism on Mars URAL - 2109 (LCA,线段树),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

LCA博客  https://blog.csdn.net/sdut_jk17_zhangming/article/details/84592285

 

 

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.

#include<cstdio>
#include<vector>
#include <bits/stdc++.h>
using namespace std;
struct node
{int v,next;
}e[5000005];
int tr[2000006];
int n;
int cnt;
int head[200005];
int dep[200005];
int f[200005][30];
void add(int u,int v)
{e[cnt] = node{v,head[u]};head[u] = cnt++;
}
void dfs(int x,int p,int d)
{f[x][0] = p;dep[x] = d;for(int i = head[x];i!=-1;i=e[i].next){int to = e[i].v;if(to!=p){dfs(to,x,d+1);}}
}
void init()
{dfs(1,0,0);for(int i=1;i<=21;i++){for(int j = 1;j<=n;j++){f[j][i] = f[f[j][i-1]][i-1];}}
}
int lca(int x,int y)
{if(dep[x] < dep[y]) swap(x,y);for(int i=21;i>=0;i--){if(dep[x]>dep[y]&&dep[f[x][i]] >= dep[y]&&f[x][i] != 0){x = f[x][i];}}if(x == y) return x;for(int i=21;i>=0;i--){if(f[x][i] != f[y][i]){x = f[x][i];y = f[y][i];}}return f[x][0];
}
void build(int rt,int l,int r)
{if(l==r){tr[rt] = lca(l,l+1);return ;}int mid = (l+r)>>1;build(rt<<1,l,mid);build(rt<<1|1,mid+1,r);if(dep[tr[rt<<1]] < dep[tr[rt<<1|1]]){tr[rt] = tr[rt<<1];}else tr[rt] = tr[rt<<1|1];
}
int minn,ans;
void query(int rt,int l,int r,int L,int R)
{if(L <= l&& R >= r){if(dep[tr[rt]] < minn){minn = dep[tr[rt]];ans = tr[rt];}return ;}int mid = (l+r)>>1;if(L <= mid) query(rt<<1,l,mid,L,R);if(R > mid) query(rt<<1|1,mid+1,r,L,R);
}
int main()
{scanf("%d",&n);int i,u,v;cnt = 0;for(i=1;i<=n;i++){head[i] = -1;}for(i=1;i<n;i++){scanf("%d%d",&u,&v);add(u,v);add(v,u);}if(n==1){scanf("%d",&n);while(n--){scanf("%d%d",&u,&v);printf("1\n");}return 0;}memset(f,0,sizeof(f));init();build(1,1,n-1);int m;scanf("%d",&m);int a,b;while(m--){scanf("%d%d",&a,&b);if(a == b){printf("%d\n",a);continue;}minn = 0x3f3f3f3f;query(1,1,n-1,a,b-1);printf("%d\n",ans);}return 0;
}

 

这篇关于Tourism on Mars URAL - 2109 (LCA,线段树)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/377184

相关文章

poj3468(线段树成段更新模板题)

题意:包括两个操作:1、将[a.b]上的数字加上v;2、查询区间[a,b]上的和 下面的介绍是下解题思路: 首先介绍  lazy-tag思想:用一个变量记录每一个线段树节点的变化值,当这部分线段的一致性被破坏我们就将这个变化值传递给子区间,大大增加了线段树的效率。 比如现在需要对[a,b]区间值进行加c操作,那么就从根节点[1,n]开始调用update函数进行操作,如果刚好执行到一个子节点,

hdu1394(线段树点更新的应用)

题意:求一个序列经过一定的操作得到的序列的最小逆序数 这题会用到逆序数的一个性质,在0到n-1这些数字组成的乱序排列,将第一个数字A移到最后一位,得到的逆序数为res-a+(n-a-1) 知道上面的知识点后,可以用暴力来解 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#in

hdu1689(线段树成段更新)

两种操作:1、set区间[a,b]上数字为v;2、查询[ 1 , n ]上的sum 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<queue>#include<set>#include<map>#include<stdio.h>#include<stdl

poj1330(LCA最近公共祖先)

题意:求最近公共祖先 思路:之前学习了树链剖分,然后我就用树链剖分的一小部分知识就可以解这个题目了,记录每个结点的fa和depth。然后查找时,每次将depth大的结点往上走直到x = y。 代码如下: #include<iostream>#include<algorithm>#include<stdio.h>#include<math.h>#include<cstring>

hdu 1754 I Hate It(线段树,单点更新,区间最值)

题意是求一个线段中的最大数。 线段树的模板题,试用了一下交大的模板。效率有点略低。 代码: #include <stdio.h>#include <string.h>#define TREE_SIZE (1 << (20))//const int TREE_SIZE = 200000 + 10;int max(int a, int b){return a > b ? a :

hdu 1166 敌兵布阵(树状数组 or 线段树)

题意是求一个线段的和,在线段上可以进行加减的修改。 树状数组的模板题。 代码: #include <stdio.h>#include <string.h>const int maxn = 50000 + 1;int c[maxn];int n;int lowbit(int x){return x & -x;}void add(int x, int num){while

poj 1127 线段相交的判定

题意: 有n根木棍,每根的端点坐标分别是 px, py, qx, qy。 判断每对木棍是否相连,当他们之间有公共点时,就认为他们相连。 并且通过相连的木棍相连的木棍也是相连的。 解析: 线段相交的判定。 首先,模板中的线段相交是不判端点的,所以要加一个端点在直线上的判定; 然后,端点在直线上的判定这个函数是不判定两个端点是同一个端点的情况的,所以要加是否端点相等的判断。 最后

HDU4737线段树

题目大意:给定一系列数,F(i,j)表示对从ai到aj连续求或运算,(i<=j)求F(i,j)<=m的总数。 const int Max_N = 100008 ;int sum[Max_N<<2] , x[Max_N] ;int n , m ;void push_up(int t){sum[t] = sum[t<<1] | sum[t<<1|1] ;}void upd

zoj 1721 判断2条线段(完全)相交

给出起点,终点,与一些障碍线段。 求起点到终点的最短路。 枚举2点的距离,然后最短路。 2点可达条件:没有线段与这2点所构成的线段(完全)相交。 const double eps = 1e-8 ;double add(double x , double y){if(fabs(x+y) < eps*(fabs(x) + fabs(y))) return 0 ;return x + y ;

圆与线段的交点

poj 3819  给出一条线段的两个端点,再给出n个圆,求出这条线段被所有圆覆盖的部分占了整条线段的百分比。 圆与线段的交点 : 向量AB 的参数方程  P = A + t * (B - A)      0<=t<=1 ; 将点带入圆的方程即可。  注意: 有交点 0 <= t <= 1 ; 此题求覆盖的部分。 则 若求得 t  满足 ; double ask(d