【luogu P3258 [JLOI2014]松鼠的新家】 题解
题目链接:https://www.luogu.org/problemnew/show/P3258
谁说树剖过不去会RE呢?
我今天就是要强行树剖了
树剖强艹
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define ll long long
using namespace std;
const int maxn = 500010;
ll ans[maxn], son[maxn], deep[maxn], size[maxn], fa[maxn], top[maxn], seg[maxn], num, n, m, path[maxn];
struct edge{
ll to, next;
}e[maxn<<2];
ll head[maxn], cnt;
void add(ll u, ll v)
{
e[++cnt].to = v; e[cnt].next = head[u]; head[u] = cnt;
e[++cnt].to = u; e[cnt].next = head[v]; head[v] = cnt;
}
class Segment_Tree{
public:
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1
ll tree[maxn<<2], lazy[maxn<<2];
void build(ll l, ll r, ll rt)
{
if(l == r)
{
tree[rt] = 0;
return;
}
ll mid = (l + r) >> 1;
build(lson);
build(rson);
PushUP(rt);
}
void update(ll left, ll right, ll add, ll l, ll r, ll rt)
{
if(left <= l && right >= r)
{
lazy[rt] += add;
tree[rt] += (r - l + 1) * add;
return;
}
ll mid = (l + r) >> 1;
PushDOWN(l, r, rt);
if(left <= mid) update(left, right, add, lson);
if(right > mid) update(left, right, add, rson);
PushUP(rt);
}
ll query(ll left, ll right, ll l, ll r, ll rt)
{
ll res = 0;
if(left <= l && right >= r)
{
return tree[rt];
}
ll mid = (l + r) >> 1;
PushDOWN(l, r, rt);
if(left <= mid) res += query(left, right, lson);
if(right > mid) res += query(left, right, rson);
return res;
}
private:
void PushUP(ll rt)
{
tree[rt] = tree[rt<<1] + tree[rt<<1|1];
}
void PushDOWN(ll l, ll r, ll rt)
{
ll mid = (l + r) >> 1;
if(lazy[rt])
{
lazy[rt<<1] += lazy[rt];
lazy[rt<<1|1] += lazy[rt];
tree[rt<<1] += (mid-l+1)*lazy[rt];
tree[rt<<1|1] += (r-mid)*lazy[rt];
lazy[rt] = 0;
}
}
}T;
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;
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 LCA(ll x, ll 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)
{
ll res = 0;
res += T.query(seg[x], seg[x]+size[x]-1, 1, n, 1);
return res;
}
void Updson(ll x, ll y)
{
T.update(seg[x], seg[x]+size[x]-1, y, 1, n, 1);
}
ll Qrange(ll x, ll y)
{
ll res = 0;
while(top[x] != top[y])
{
if(deep[top[x]] < deep[top[y]]) swap(x, y);
res += T.query(seg[top[x]], seg[x], 1, n, 1);
x = fa[top[x]];
}
if(deep[x] < deep[y]) swap(x, y);
res += T.query(seg[y], seg[x], 1, n, 1);
return res;
}
void Updrange(ll x, ll y, ll z)
{
while(top[x] != top[y])
{
if(deep[top[x]] < deep[top[y]]) swap(x, y);
T.update(seg[top[x]], seg[x], z, 1, n, 1);
x = fa[top[x]];
}
if(deep[x] < deep[y]) swap(x, y);
T.update(seg[y], seg[x], z, 1, n, 1);
}
int main()
{
memset(head, -1, sizeof(head));
scanf("%lld%lld",&n,&path[1]);
for(ll i = 2; i <= n; i++)
{
scanf("%lld",&path[i]);
ans[path[i]]--;
}
for(ll i = 1; i <= n-1; i++)
{
ll u, v;
scanf("%lld%lld",&u,&v);
add(u, v);
}
dfs1(1, 0, 1); dfs2(1, 1);
T.build(1, n, 1);
for(ll i = 2; i <= n; i++)
{
Updrange(path[i], path[i-1], 1);
}
for(ll i = 1; i <= n; i++) ans[i] += Qrange(i, i);
for(ll i = 1; i <= n; i++)
{
printf("%lld\n",ans[i]);
}
return 0;
}
【luogu P3258 [JLOI2014]松鼠的新家】 题解的更多相关文章
- 洛谷 P3258 [JLOI2014]松鼠的新家 题解
P3258 [JLOI2014]松鼠的新家 题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他 ...
- [Luogu] P3258 [JLOI2014]松鼠的新家
题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他居然真的住在”树“上. 松鼠想邀请小熊维尼前 ...
- P3258 [JLOI2014]松鼠的新家题解
题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有\(n\)个房间,并且有\(n-1\)根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他居然真的住在"树& ...
- P3258 [JLOI2014]松鼠的新家
P3258 [JLOI2014]松鼠的新家倍增lca+树上差分,从叶子节点向根节点求前缀和,dfs求子树和即可,最后,把每次的起点和终点都. #include<iostream> #inc ...
- 洛谷 P3258 [JLOI2014]松鼠的新家 解题报告
P3258 [JLOI2014]松鼠的新家 题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他 ...
- 【洛谷】【lca+树上差分】P3258 [JLOI2014]松鼠的新家
[题目描述:] 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n(2 ≤ n ≤ 300000)个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他居然真 ...
- [Luogu 3258] JLOI2014 松鼠的新家
[Luogu 3258] JLOI2014 松鼠的新家 LCA + 树上差分. 我呢,因为是树剖求的 LCA,预处理了 DFN(DFS 序),于是简化成了序列差分. qwq不讲了不讲了,贴代码. #i ...
- 洛谷P3258 [JLOI2014]松鼠的新家
P3258 [JLOI2014]松鼠的新家 题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他 ...
- P3258[JLOI2014]松鼠的新家(LCA 树上差分)
P3258 [JLOI2014]松鼠的新家 题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他 ...
随机推荐
- C++ STL:vector实现
练习一发,主要是使用placement new在原始内存上创建对象.半路md面试电话来了,赶紧存档,看Java大法 #include <iostream> #include <cst ...
- select实现三级联动
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- [转]web使用Quartz.NET实现作业调度
转自 https://www.cnblogs.com/best/p/7658573.html 一.Quartz.NET介绍 Quartz.NET是一个强大.开源.轻量的作业调度框架,是 OpenSym ...
- 搭建VueMint-ui框架
搭建VueMint-ui框架 2018年01月07日 22:29:58 阅读数:2660 vueweb vue project 检查项目环境 一.检查是否安装node.js # 检查node版本 $ ...
- vue2.0 学习 ,开始学习
先看官网的介绍上面的教程 https://cn.vuejs.org/v2/guide/ 尝试 Vue.js 最简单的方法是使用 JSFiddle Hello World 例子.你可以在浏览器新标签 ...
- 空间分析开源库GEOS
History of GEOS GEOS中Geometry的结构 GEOS分析功能简介 1.History of GEOS JTS Topology Suite是Ma ...
- INSERT 语句总结
一.基本语句 1.向指定的列中插入值(在插入批量数据时方式2优于方式1) <!--插入一条值--> INSERT INTO t1(field1,field2) VALUE(v001,v00 ...
- SQL Server中【case...end】的用法
在SQL Server中 case...end 语句,一般有如下两种用法: 1.相当于C#中if...else,例: select CName,头衔=case when CLevel='A1' the ...
- Linux常用命令(二)————压缩+解压
tar -c: 建立压缩档案-x:解压-t:查看内容-r:向压缩归档文件末尾追加文件-u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个.下面的 ...
- iOS9 News 应用
iOS9 News 应用 iOS9 中国区虽然没有 News 应用,但最新的开发工具中是有的,以下是笔者截取的模拟器gif图,供君欣赏: