题目链接:https://www.luogu.org/problemnew/show/P3979

除了换根操作都是裸的树剖

所以换根时考虑:

1.我查询的根等于换的根:无影响

2.我查询的根是换的根的子树:无影响

3.我查询的根是换的根的祖先:查询 除换的根及其往上直到为要查询的根的第一层儿子的祖先(设为S)的子树 以外的所有节点 (此时满足seg[S] <= seg[root] <= seg[S]+size[S]-1)

求LCA 查询1到seg[S]-1 和 seg[S]+size[S]到n 正好避开子树seg[S] 到 seg[S]+size[S]-1

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define ll long long
#define lson left, mid, rt<<1
#define rson mid + 1, right, rt<<1|1
using namespace std;
const ll maxn = 200000 + 10;
ll tree[maxn<<2], lazy[maxn<<2];
ll seg[maxn], fa[maxn], size[maxn], rev[maxn], son[maxn], deep[maxn], top[maxn], num;
ll node[maxn], res, n, m, root, r;
struct edge{
ll from, to, next;
}e[maxn<<2];
ll head[maxn], cnt;
void add(ll u, ll v)
{
e[++cnt].from = u;
e[cnt].next = head[u];
e[cnt].to = v;
head[u] = cnt;
}
//--------------------------segment_tree
void PushUP(ll rt)
{
tree[rt] = min(tree[rt<<1], tree[rt<<1|1]);
}
void build(ll left, ll right, ll rt)
{
if(left == right)
{
tree[rt] = rev[left];
return;
}
ll mid = (left + right)>>1;
build(lson);
build(rson);
PushUP(rt);
}
void PushDOWN(ll rt)
{
lazy[rt<<1] = lazy[rt];
lazy[rt<<1|1] = lazy[rt];
tree[rt<<1] = lazy[rt];
tree[rt<<1|1] = lazy[rt];
lazy[rt] = 0;
}
void update(ll l, ll r, ll add, ll left, ll right, ll rt)
{
if(l <= left && r >= right)
{
tree[rt] = add;
lazy[rt] = add;
return;
}
ll mid = (left + right)>>1;
if(lazy[rt]) PushDOWN(rt);
if(l <= mid) update(l, r, add, lson);
if(r > mid) update(l, r, add, rson);
PushUP(rt);
}
ll query(ll l, ll r, ll left, ll right, ll rt)
{
ll res = 0x7fffffff;
if(l <= left && r >= right)
{
return tree[rt];
}
ll mid = (left + right)>>1;
if(lazy[rt]) PushDOWN(rt);
if(l <= mid) res = min(res, query(l, r, lson));
if(r > mid) res = min(res, query(l, r, rson));
return res;
}
//------------------------
void dfs1(ll u, ll f, ll d)
{
ll maxson = -1;
size[u] = 1;
deep[u] = d;
fa[u] = f;
for(ll i = head[u]; i != -1; i = e[i].next)
{
ll v = e[i].to;
if(f != v)
{
dfs1(v, u, d+1);
size[u] += size[v];
if(size[v] > maxson) son[u] = v, maxson = size[v];
}
}
}
void dfs2(ll u, ll t)
{
seg[u] = ++num;
rev[num] = node[u];
top[u] = t;
if(!son[u]) return;
dfs2(son[u], t);
for(ll i = head[u]; i != -1; i = e[i].next)
{
ll v = e[i].to;
if(v == son[u] || v == fa[u]) continue;
dfs2(v,v);
}
}
/*ll qRange(ll x, ll y)
{
ll ans = 0;
while(top[x] != top[y])
{
if(deep[top[x]] < deep[top[y]]) swap(x, y);
res = 0;
res = query(seg[top[x]], seg[x], 1, n, 1);
ans = (ans + res);
x = fa[top[x]];
}
if(deep[x] > deep[y]) swap(x, y);
res = 0;
res = query(seg[x], seg[y], 1, n, 1);
ans = (ans + res);
return ans;
}*/
void updRange(ll x, ll y, ll k)
{
while(top[x] != top[y])
{
if(deep[top[x]] < deep[top[y]]) swap(x, y);
update(seg[top[x]], seg[x], k, 1, n, 1);
x = fa[top[x]];
}
if(deep[x] > deep[y]) swap(x, y);
update(seg[x], seg[y], k, 1, n, 1);
}
ll LCA(int x, int y)
{
while(top[x] != top[y])
{
if(deep[top[x]] < deep[top[y]]) swap(x, y);
x = fa[top[x]];
}
return deep[x] < deep[y] ? x : y;
}
ll qSon(ll x)
{
if(x == r)
return query(1, n, 1, n, 1);
int p = LCA(r, x);
if(p != x)
return query(seg[x], seg[x]+size[x]-1, 1, n, 1);
int S;
for(int i = head[x]; i != -1; i = e[i].next)
{
int v = e[i].to;
if(seg[v] <= seg[r] && seg[r] <= seg[v] + size[v] -1 && v != fa[x])
{
S = v; break;
}
}
return min(query(1, seg[S]-1, 1, n, 1), query(seg[S]+size[S], n, 1, n, 1));
}
int main()
{
memset(head, -1, sizeof(head));
scanf("%lld%lld",&n,&m);
for(ll i = 1; i < n; i++)
{
ll u, v;
scanf("%lld%lld",&u,&v);
add(u, v), add(v, u);
}
for(ll i = 1; i <= n; i++)
scanf("%lld",&node[i]);
scanf("%lld",&root);
dfs1(root, 0, 1);
dfs2(root, root);
build(1,n,1);
r = root;
for(ll i = 1; i <= m; i++)
{
ll opt, x, y, z;
scanf("%lld",&opt);
if(opt == 1)
{
scanf("%lld",&x);
r = x;
}
if(opt == 2)
{
scanf("%lld%lld%lld",&x,&y,&z);
updRange(x, y, z);
}
if(opt == 3)
{
scanf("%lld",&x);
printf("%lld\n",qSon(x));
}
}
return 0;
}

【luogu P3979 遥远的国度】 题解的更多相关文章

  1. P3979 遥远的国度

    P3979 遥远的国度 思路 一开始我用这个函数得到左端点 int get_l(int x,int y) { if(top[x]==top[y]) return son[x]; int last=to ...

  2. P3979 遥远的国度 树剖

    P3979 遥远的国度 树剖 题面 需要想一下的树剖题,对于询问三需要处理换跟后的情况.我们以1为树根跑一遍剖分,对于换跟进行分类讨论,算出实际答案.讨论有三种情况: (以1为树根的树上) 跟在询问节 ...

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

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

  4. 【树链剖分换根】P3979 遥远的国度

    Description zcwwzdjn在追杀十分sb的zhx,而zhx逃入了一个遥远的国度.当zcwwzdjn准备进入遥远的国度继续追杀时,守护神RapiD阻拦了zcwwzdjn的去路,他需要zcw ...

  5. 【luogu P3946 ことりのおやつ】 题解

    题目链接:https://www.luogu.org/problemnew/show/P3946 交好几遍是因为虽然能过一直有提醒..强迫症qwq #include <bits/stdc++.h ...

  6. [洛谷P3979]遥远的国度

    题目大意:有一棵$n$个点的树,每个点有一个点权,有三种操作: $1\;x:$把根变成$x$ $2\;u\;v\;x:$把路径$u->v$上的点权改为$x$ $3\;x:$询问以$x$为根的子树 ...

  7. Luogu P2210 Haywire 题解

    其实这题吧...有一种玄学解法 这题的要求的就是一个最小化的顺序 那么,我们就不进想到了一种显然的写法 就是random_shuffle 什么?这不是乱搞的非正解吗 然而,正如一句话说的好 一个算法, ...

  8. [Luogu P4178]Tree 题解(点分治+平衡树)

    题目大意 给定一棵树,边带权,问有多少点对满足二者间距离$\leq K$,$n \leq 40000$. 题解 点分治专题首杀!$Jackpot!$ (本来看着题意比较简单想捡个软柿子捏,结果手断了… ...

  9. [火星补锅] 水题大战Vol.2 T1 && luogu P1904 天际线 题解 (线段树)

    前言: 当时考场上并没有想出来...后来也是看了题解才明白 解析: 大家(除了我)都知道,奇点和偶点会成对出现,而出现的前提就是建筑的高度突然发生变化.(这个性质挺重要的,我之前没看出来) 所以就可以 ...

随机推荐

  1. opengl键盘回调函数不能获取Ctrl+c的问题

    我要令窗口在按下 Ctrl+c 之后关闭. 关键代码如下: /* 这段代码位于键盘回调函数中 */ if ((glutGetModifiers() == GLUT_ACTIVE_CTRL) & ...

  2. pat09-散列3. Hashing - Hard Version (30)

    09-散列3. Hashing - Hard Version (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 HE, Qin ...

  3. Smart3D基础理论

    目录: 1. Smart3D发展进程 2. 硬件要求与建模原理 3. Smart3D建模优势 4.Smart3D的应用领域 5. Smart3D的软件组成 6. Samrt3D主控台概述 1. Sma ...

  4. 数据降维(Dimensionality reduction)

    数据降维(Dimensionality reduction) 应用范围 无监督学习 图片压缩(需要的时候在还原回来) 数据压缩 数据可视化 数据压缩(Data Compression) 将高维的数据转 ...

  5. [转]Using NLog for ASP.NET Core to write custom information to the database

    本文转自:https://github.com/NLog/NLog/issues/1366 In the previous versions of NLog it was easily possibl ...

  6. javascript 实现函数/方法重载效果

    什么是重载? 在C#和JAVA等编程语言中函数重载是指在一个类中可以定义多个方法名相同但是方法参数和顺序不同的方法,以此来实现不同的功能和操作,这就是重载. JS没有重载,只能模拟重载 一般来说,如果 ...

  7. web.config节点

    1.clientCache 源码: <system.webServer> <staticContent> <clientCache cacheControlMode=&q ...

  8. C#学习笔记9

    1.多播委托:由与delegate关键字声明的委托,在编译后默认继承Delegate与MulticastDelegate类型,所以声明的委托自然就含有多播委托的特性,即一个委托变量可以调用一个方法链( ...

  9. Spring注解之Controller中获取请求参数及验证使用

    1.处理request的uri部分的参数:@PathVariable. 2.处理request header部分的参数:@RequestHeader,@CookieValue@RequestHeade ...

  10. 阿里云 linux 系统的架构

    简单说,/lib是内核级的,/usr/lib是系统级的,/usr/local/lib是用户级的. /lib/ — 包含许多被 /bin/ 和 /sbin/ 中的程序使用的库文件.目录 /usr/lib ...