题目链接: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. Linux-debian系统 /etc/network/interface 文件解读

    原文 http://wiki.slimdevices.com/index.php/SqueezeOS_networking 话说Debian系的网卡配置跟Redhat系很不一样,Redhat是放在/e ...

  2. MIME格式说明,电子邮件格式(转载)

    邮件格式说明 Mutiple Internet Mail Extensions Refer to Internet Official Protocol Standards RFC 822 1 概述 网 ...

  3. Hibernate入门(四)—— 查询

    一.Hibernate查询 1.Hibernate检索(查询)方式的分类 OID检索 :根据主键查询,get/load 对象导航检索 :通过一个对象获得其关联对象.[重点] Category cate ...

  4. Algorithm——无重复字符的最长子串

    一.问题 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "a ...

  5. Sspring bean被初始化加载2次

    Sspring bean被初始化加载2次 spring框架的web项目时,启动的时候发现某个bean被加载了两次,比如使用SchedulingConfigurer或者使用@PostConstruct的 ...

  6. 关于 PHPMailer 邮件发送类的使用心得(含多文件上传)

    This is important for send mail PHPMailer 核心文件 class.phpmailer.php class.phpmaileroauth.php class.ph ...

  7. WinForm实现Rabbitmq官网6个案例-Routing

    代码: namespace RabbitMQDemo { public partial class Routing : Form { private string exchangeName = &qu ...

  8. Android学习笔记(1)----播放音乐文件

    原文地址:http://www.cnblogs.com/wynet/p/5526905.html 这里介绍两种播放资源文件的方法: 第一种. assets类资源放在工程根目录的assets子目录下,它 ...

  9. vmware虚拟机安装CentOS教程

    linux是企业最常用的服务器系统之一,CentOS是免费的,所以用的企业也挺多,今天给大家分享怎么在自己电脑的虚拟机中安装CentOS-6.5,以便用来玩耍,没事的时候可以学学linux的一些知识. ...

  10. NAT原理简介、各种 ADSL Modem 及路由器的端口映射方法

    NAT原理简介 NAT英文全称是“Network Address Translation”,中文意思是“网络地址转换”,它是一个IETF(Internet Engineering Task Force ...