https://www.luogu.org/problemnew/show/P3979

3种情况

x=root,很显然此时应当查询整棵树

lca(root,x)!=x ,此时直接查询x的子树即可,与换根无关

lca(root,x)=x,此时我们应当查询与x相邻的节点中与root最近的点v在整棵树中的补集

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
using namespace std; typedef long long ll;
typedef double dd;
const int maxn=;
const ll INF = (ll)1E14 + ; int h[maxn], n, m, top[maxn], lca[maxn][], son[maxn], edge, sz[maxn], dep[maxn];
int L[maxn], R[maxn], root, num, x, y, tx, ty, t, opt;
ll val, a[maxn]; struct Edge {
int to, ne;
} e[maxn * ]; struct Seg {
ll minn, same;
} seg[maxn << ]; void close() {
exit();
} void addedge(int x,int y) {
e[edge].to = y;
e[edge].ne = h[x];
h[x] = edge++;
} void dfs(int k,int from) {
sz[k] = ;
dep[k] = dep[from] + ;
son[k] = ;
for (int p=h[k]; p!=-; p=e[p].ne) {
int to = e[p].to;
if (to == from) continue;
lca[to][] = k;
for (int i=; i<=; i++)
lca[to][i] = lca[lca[to][i-]][i-];
dfs(to, k);
sz[k] += sz[to];
if (sz[to] > sz[son[k]]) son[k] = to;
}
} void build(int k,int from) {
L[k] = ++num;
top[k] = from;
if (son[k]) build(son[k], from);
for (int p=h[k]; p!=-; p=e[p].ne) {
int to = e[p].to;
if (to != lca[k][] && to != son[k])
build(to, to);
}
R[k] = num;
} int get_lca(int x,int y) {
if (dep[x] < dep[y]) swap(x, y);
int depth = dep[x] - dep[y];
for (int i=; i>=; i--)
if (depth & ( << i))
x = lca[x][i];
if (x == y) return x;
for (int i=; i>=; i--) {
if (lca[x][i] != lca[y][i]) {
x = lca[x][i];
y = lca[y][i];
}
}
return lca[x][];
} void pushup(int rt) {
seg[rt].minn = min(seg[rt<<].minn, seg[rt<<|].minn);
} void same(int rt,ll val) {
seg[rt].minn = val;
seg[rt].same = val;
} void pushdown(int rt) {
if (seg[rt].same) {
same(rt << , seg[rt].same);
same(rt << | , seg[rt].same);
seg[rt].same = ;
}
} void change(int L,int R,ll val,int l,int r,int rt) {
if (L <= l && r <= R) {
same(rt, val);
return;
}
int mid = (l + r) >> ;
pushdown(rt);
if (L <= mid)
change(L,R,val,lson);
if (mid + <= R)
change(L,R,val,rson);
pushup(rt);
} ll query(int L,int R,int l,int r,int rt) {
if (L > R) return INF;
if (L <= l && r <= R) {
return seg[rt].minn;
}
int mid = (l + r) >> ;
pushdown(rt);
ll ans = INF;
if (L <= mid)
ans = min(ans, query(L,R,lson));
if (mid + <= R)
ans = min(ans, query(L,R,rson));
pushup(rt);
return ans;
} void cc() {
tx = top[x];
ty = top[y];
while (tx != ty) {
if (dep[tx] < dep[ty]) {
swap(x, y);
swap(tx, ty);
}
change(L[tx], L[x], val, , n, );
x = lca[tx][];
tx = top[x];
}
if (dep[x] < dep[y])
swap(x, y);
change(L[y], L[x], val, , n, );
} void work() {
if (root == x) {
printf("%lld\n", seg[].minn);
return;
}
t = get_lca(root, x);
if (t != x) {
printf("%lld\n", query(L[x], R[x], , n, ));
return;
}
int depth = dep[root] - dep[x] - ;
int haha = root;
for (int i=; i>=; i--)
if (depth & ( << i))
haha = lca[haha][i];
printf("%lld\n", min(query(, L[haha] - , , n, ), query(R[haha] + , n, , n, )) );
} void init() {
scanf("%d %d",&n,&m);
memset(h, -, sizeof(h));
for (int i=; i<=n-; i++) {
scanf("%d %d",&x, &y);
addedge(x, y);
addedge(y, x);
}
for (int i=; i<=n; i++)
scanf("%lld",&a[i]);
dfs(, );
build(, );
for (int i=; i<=n; i++)
change(L[i], L[i], a[i], , n, );
scanf("%d",&root);
while (m--) {
scanf("%d",&opt);
if (opt == ) scanf("%d",&root);
if (opt == ) {
scanf("%d %d %lld",&x,&y,&val);
cc();
}
if (opt == ) {
scanf("%d",&x);
work();
}
}
} int main () {
init();
close();
return ;
}

[Luogu] 遥远的国度的更多相关文章

  1. 【luogu P3979 遥远的国度】 题解

    题目链接:https://www.luogu.org/problemnew/show/P3979 除了换根操作都是裸的树剖 所以换根时考虑: 1.我查询的根等于换的根:无影响 2.我查询的根是换的根的 ...

  2. Luogu 3979 遥远的国度

    树剖已经是人尽皆知的sb题了吗…… 很早以前就想填掉这坑了…… 考虑到树链唯一,进行操作并不会对换根产生影响,那么我们的换根操作只要记下root在哪里就好了 询问的时候分类讨论: 1:root ==  ...

  3. 【Luogu】P3979遥远的国度(树链剖分)

    题目链接 不会换根从暑假开始就困扰我了……拖到现在…… 会了还是很激动的. 换根操作事实上不需要(也不能)改树剖本来的dfs序……只是在query上动动手脚…… 设全树的集合为G,以root为根,u在 ...

  4. [luogu3979][bzoj3083]遥远的国度

    [luogu传送门] [bzoj传送门] 题目描述 zcwwzdjn在追杀十分sb的zhx,而zhx逃入了一个遥远的国度.当zcwwzdjn准备进入遥远的国度继续追杀时,守护神RapiD阻拦了zcww ...

  5. Luogu 1514 引水入城 (搜索,动态规划)

    Luogu 1514 引水入城 (搜索,动态规划) Description 在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政区划十分特殊,刚好构成一个N行M列的矩形,如上图 ...

  6. [luogu]P1514 引水入城[搜索][记忆化][DP]

    [luogu]P1514 引水入城 引水入城 题目描述在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政区划十分特殊,刚好构成一个N 行M 列的矩形 ,如下图所示,其中每个格 ...

  7. BZOJ 3083: 遥远的国度 [树链剖分 DFS序 LCA]

    3083: 遥远的国度 Time Limit: 10 Sec  Memory Limit: 1280 MBSubmit: 3127  Solved: 795[Submit][Status][Discu ...

  8. Luogu 魔法学院杯-第二弹(萌新的第一法blog)

    虽然有点久远  还是放一下吧. 传送门:https://www.luogu.org/contest/show?tid=754 第一题  沉迷游戏,伤感情 #include <queue> ...

  9. luogu p1268 树的重量——构造,真正考验编程能力

    题目链接:http://www.luogu.org/problem/show?pid=1268#sub -------- 这道题费了我不少心思= =其实思路和标称毫无差别,但是由于不习惯ACM风格的题 ...

随机推荐

  1. 【满k叉树】Perfect Tree

    题目描述 Given a positive integer k, we define a rooted tree to be k-perfect, if and only if it meets bo ...

  2. Solr综合案例深入练习

    1. 综合案例 1.1. 需求 使用Solr实现电商网站中商品信息搜索功能,可以根据关键字.分类.价格搜索商品信息,也可以根据价格进行排序,并且实现分页功能. 界面如下: 1.2. 分析 开发人员需要 ...

  3. shell习题第27题:带选项的增删用户脚本

    [题目要求] 写一个支持选项的增加或删除用户的shell脚本 #!/bin/bash ]; then echo "Wrong, use bash $0 --add username, or ...

  4. 本地Pycharm将spark程序发送到远端spark集群进行处理

    前言 最近在搞hadoop+spark+python,所以就搭建了一个本地的hadoop环境,基础环境搭建地址hadoop2.7.7 分布式集群安装与配置,spark集群安装并集成到hadoop集群, ...

  5. jvm垃圾回收器介绍

    上篇文章中我们讨论了jvm的内存区域,这篇文章我们来讨论针对的内存区域的垃圾回收机制. 其实针对垃圾回收我们通常考虑三个问题:1.哪些内存需要回收?2.什么时候回收?3.如何回收?下面我们针对这三个问 ...

  6. 奇妙的算法【10】TX--有效号码、最,小耗时、最小差值、差值输出、异或结果

    昨晚刚刚写的几道算法题,难度也还行,就是全部AC有些困难,当时第一题AC.第二题AC 60%,第四题AC 40%,第五题没有时间写完了,这个应该全部AC了:其中第三题没有写出来 1,是否存在符合规范的 ...

  7. asp.net类似于js中的setTimeOut()的函数作用?

    asp.net类似于js中的setTimeOut()的函数作用? 插入这行即可,定时2秒,再运行下一步: System.Threading.Thread.Sleep(); 加个随机数 Random r ...

  8. ASP.NET WEB应用程序(.network4.5)MVC Razor视图引擎2 视图模板页

    https://www.cnblogs.com/xlhblogs/archive/2013/06/09/3129449.html MVC Razor模板引擎 @RenderBody.@RenderPa ...

  9. luogu题解 P3950部落冲突--树链剖分

    题目链接 https://www.luogu.org/problemnew/show/P3950 分析 大佬都用LCT,我太弱只会树链剖分 一个很裸的维护边权树链剖分题.按照套路,对于一条边\(< ...

  10. java封装数据类型——Integer 缓存策略验证

    上一篇学习 Integer 类型源码,知道了它使用缓存策略,默认对 [-128, 127] 范围的对象进行类加载时自动创建缓存. Integer 源码学习:https://www.cnblogs.co ...