题目链接: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]松鼠的新家】 题解的更多相关文章

  1. 洛谷 P3258 [JLOI2014]松鼠的新家 题解

    P3258 [JLOI2014]松鼠的新家 题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他 ...

  2. [Luogu] P3258 [JLOI2014]松鼠的新家

    题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他居然真的住在”树“上. 松鼠想邀请小熊维尼前 ...

  3. P3258 [JLOI2014]松鼠的新家题解

    题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有\(n\)个房间,并且有\(n-1\)根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他居然真的住在"树& ...

  4. P3258 [JLOI2014]松鼠的新家

    P3258 [JLOI2014]松鼠的新家倍增lca+树上差分,从叶子节点向根节点求前缀和,dfs求子树和即可,最后,把每次的起点和终点都. #include<iostream> #inc ...

  5. 洛谷 P3258 [JLOI2014]松鼠的新家 解题报告

    P3258 [JLOI2014]松鼠的新家 题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他 ...

  6. 【洛谷】【lca+树上差分】P3258 [JLOI2014]松鼠的新家

    [题目描述:] 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n(2 ≤ n ≤ 300000)个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他居然真 ...

  7. [Luogu 3258] JLOI2014 松鼠的新家

    [Luogu 3258] JLOI2014 松鼠的新家 LCA + 树上差分. 我呢,因为是树剖求的 LCA,预处理了 DFN(DFS 序),于是简化成了序列差分. qwq不讲了不讲了,贴代码. #i ...

  8. 洛谷P3258 [JLOI2014]松鼠的新家

    P3258 [JLOI2014]松鼠的新家 题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他 ...

  9. P3258[JLOI2014]松鼠的新家(LCA 树上差分)

    P3258 [JLOI2014]松鼠的新家 题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他 ...

随机推荐

  1. C#学习笔记-接口与抽象类

    namespace ClassLesson { class Program { static void Main(string[] args) { ); Console.WriteLine(perso ...

  2. 第三天-基本数据类型 int bool str

    # python基础数据类型 # 1. int 整数 # 2.str 字符串.不会用字符串保存大量的数据 # 3.bool 布尔值. True, False # 4.list 列表(重点) 存放大量的 ...

  3. JSON和JSONP 实例

    来源:http://www.cnblogs.com/dowinning/archive/2012/04/19/json-jsonp-jquery.html 前言: 说到AJAX就会不可避免的面临两个问 ...

  4. 理解webpack4.splitChunks之其余要点

    splitChunks除了之前文章提到的规则外,还有一些要点或是叫疑惑因为没有找到官方文档的明确说明,所以是通过我自己测试总结出来的,只代表我自己的测试结果,不一定正确. splitChunks.ca ...

  5. 利用localStorage事件来跨标签页共享sessionStorage

    //干货 利用localStorage事件来跨标签页共享sessionStorage //因为cookie保存字节数量有限,很多童鞋考虑用html5 storage来保存临时数据,Sessionsto ...

  6. JS判断浏览器版本

    CSS html,body{ height: 100%; } body{ margin: 0 } div{ padding-left: 50px; } .span{ padding: 5px 15px ...

  7. Angular入门教程三

    4.6指令(directive) 通过使用模板,我们可以把model和controller中的数据组装起来呈现给浏览器,还可以通过数据绑定,实时更新视图,让我们的页面变成动态的. 模板中可以使用的东西 ...

  8. 推荐下:开源ckplayer 网页播放器, 跨平台(html5, mobile),flv, f4v, mp4, rtmp协议. webm, ogg, m3u8 !

    视频播放, 原本是想h5 自带视频播放,使用很简单,结果现实很骨感. <video controls="controls" preload="auto" ...

  9. SSM 框架-03-MyEclipse+Tomcat+MAVEN+SVN项目完整环境搭建

    SSM 框架-03-MyEclipse+Tomcat+MAVEN+SVN项目完整环境搭建 如果你是使用 Eclipse 你需要先安装 MyEclipse,请参考:SSM 框架-02-MyEclipse ...

  10. 基于Vue的WebApp项目开发(四)

    实现新闻咨询页面 目录结构 步骤一:创建newslist.vue文件 <template> <div id="tml"> <!--使用mui框架,实现 ...