12pts 代码求条 qwq
查看原帖
12pts 代码求条 qwq
907119
koukilee楼主2024/9/27 16:47

用的树剖,不知道思路对不对,没有大数据不好检查,,

大概思路就是先求出lca,判断可不可以到达,不可以就看是从lca左边跳还是右边

/* The code is from koukilee*/
#include <cstdio>
#include <utility>

using i32 = int; using i64 = long long; // std::mt19937 rd(time(0));
constexpr i64 MAXN = 1010100, mod = 1e9 + 7, INF = 992009100720100812; 

static char buf[1000000],*p1=buf,*p2=buf,obuf[1000000],*p3=obuf;
#define getchar() p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++
#define putchar(x) (p3-obuf<1000000)?(*p3++=x):(fwrite(obuf,p3-obuf,1,stdout),p3=obuf,*p3++=x)
inline void read (i64 &sum) noexcept {
    i64 tf = 0; char ch = getchar(); sum = 0;
    while (ch < '0' || ch > '9') tf = ch == '-' ? 1 : 0, ch = getchar();
    while (ch >= '0' && ch <= '9') sum = (sum << 1) + (sum << 3) + (ch ^ 48), ch = getchar();
    (tf) && (sum =- sum);
}template <typename i64,typename ...Args>
inline void read (i64 &tmp, Args &...tmps) noexcept{read(tmp); read(tmps...);}
void printt (i64 x) noexcept {if(x >= 10) printt(x / 10); putchar(x % 10 + '0');} 
inline void print (i64 x) noexcept{x < 0 ? putchar('-'), printt(-x) : printt(x);}
inline void put (i64 x) noexcept{print(x), putchar('\n');}

inline void swap(i64& a, i64& b) noexcept{i64 c = a; a = b; b = c;}

struct edge{i64 u, v, d, nex;}g[MAXN << 1];

i64 n, m, k, cnt, first[MAXN], size[MAXN], dep[MAXN], fa[MAXN], son[MAXN], t, id[MAXN], top[MAXN], rev[MAXN];

inline void update(i64 u, i64 v, i64 d = 0) noexcept{
	g[++cnt] = (edge){u, v, d, first[u]}, first[u] = cnt;
}

void dfs1(i64 u, i64 dad) noexcept{
	size[u] = 1, dep[u] = dep[dad] + 1, fa[u] = dad;
	
	for (i32 i = first[u]; i; i = g[i].nex){
		if (g[i].v != dad){
			dfs1(g[i].v, u), size[u] += size[g[i].v];
			if (size[g[i].v] > size[son[u]])
				son[u] = g[i].v;
		}
	}
} 

void dfs2(i64 u, i64 dad) noexcept{
	if (son[u]){
		id[son[u]] = ++t, top[son[u]] = top[u], rev[t] = son[u];
		dfs2(son[u], u);
	}
	
	for (i32 i = first[u]; i; i = g[i].nex){
		if (g[i].v != dad && !top[g[i].v]){
			id[g[i].v] = ++t, top[g[i].v] = g[i].v, rev[t] = g[i].v;
			dfs2(g[i].v, u);
		}
	}
}

inline std::pair<i64, i64> Lca(i64 x, i64 y) noexcept{
	i64 L = 0, dis = 0;
	
	while (top[x] != top[y]){
		if (dep[top[x]] < dep[top[y]])
			swap(x, y);
		dis += id[x] - id[top[x]] + 1;
		x = fa[top[x]];
	}
	
	
	if (id[x] < id[y]) swap(x, y);
	L = y; dis += id[x] - id[y];
	
	return std::make_pair(L, dis);
}

inline i64 rechange(i64 x, i64 d) noexcept{
	while (true){
		if (id[x] - id[top[x]] >= d){
			return rev[id[x] - d];
		}
		d -= id[x] - id[top[x]] + 1;
	}
}

int main() noexcept{
	read(n, m, k);
	for (i32 i = 1; i < n; i++){
		i64 x, y; read(x, y);
		update(x, y), update(y, x);
	}
	
	dfs1(1, 0);
	id[1] = ++t, top[1] = 1, rev[1] = 1;
	dfs2(1, 0);
	
	
	while (k--){
		i64 x, d; read(x, d);
		std::pair<i64, i64> val = Lca(m, x);
		
		if (d >= val.second)
			m = x;
		else {
			std::pair<i64, i64> valv = Lca(m, val.first);
			if (valv.second >= d)
				m = rechange(m, d);
			else
				m = rechange(x, val.second - (d - valv.second));
		}
		
		print(m), putchar(' ');
	}
    fwrite(obuf,p3-obuf,1,stdout);
    return 0;
}
2024/9/27 16:47
加载中...