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

 #include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<set>
#include<map>
#include<stack>
#include<cstring>
#define inf 2147483647
#define For(i,a,b) for(register int i=a;i<=b;i++)
#define p(a) putchar(a)
#define g() getchar()
//by war
//2017.11.8
using namespace std;
int f[][];
int a[],b[];
int deep[];
int change[];
bool vis[];
int lca;
int x,y;
int n;
struct node
{
int n;
node *next;
}*e[]; inline void in(register int &x)
{
int y=;
char c=g();x=;
while(c<''||c>'')
{
if(c=='-')
y=-;
c=g();
}
while(c<=''&&c>='')x=(x<<)+(x<<)+c-'',c=g();
x*=y;
}
inline void o(register int x)
{
if(x<)
{
p('-');
x=-x;
}
if(x>)o(x/);
p(x%+'');
} inline void push(register int x,register int y)
{
node *p;
p=new node();
p->n=y;
if(e[x]==NULL)
e[x]=p;
else
{
p->next=e[x]->next;
e[x]->next=p;
}
} inline void build(register int now)
{
deep[now]=deep[f[now][]]+;
for(register int i=;(<<i)<=n;i++)
f[now][i]=f[f[now][i-]][i-];
for(node *i=e[now];i!=NULL;i=i->next)
{
if(f[now][]!=i->n)
{
f[i->n][]=now;
build(i->n);
}
}
} inline int query(register int x,register int y)
{
if(deep[x]<deep[y])
swap(x,y);
int c=deep[x]-deep[y];
for(register int i=;(<<i)<=c;i++)
if((<<i)&c)
x=f[x][i];
if(x==y)
return x;
for(register int i=log2(deep[x]);i>=;i--)
{
if(f[x][i]!=f[y][i])
{
x=f[x][i];
y=f[y][i];
}
}
return f[x][];
} int dfs(int now)
{
if(vis[now])
return b[now];
vis[now]=true;
for(node *i=e[now];i!=NULL;i=i->next)
if(!vis[i->n])
{
x=dfs(i->n);
b[now]+=x;
}
return b[now];
} int main()
{
// freopen("t.in","r",stdin);
// freopen("t2.out","w",stdout);
in(n);
For(i,,n)
in(a[i]);
For(i,,n-)
{
in(x),in(y);
push(x,y);
push(y,x);
}
// f[0][0]=0;
build();
For(i,,n-)
{
x=a[i];
y=a[i+];
lca=query(x,y);
change[x]++;
change[y]++;
change[lca]--;
change[f[lca][]]--;
}
For(i,,n)
b[i]=change[i];
b[]=dfs();
For(i,,n)
b[a[i]]--;
For(i,,n)
o(b[i]),p('\n');
return ;
}

P3258 [JLOI2014]松鼠的新家的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. 洛谷 P3258 [JLOI2014]松鼠的新家 树链剖分+差分前缀和优化

    目录 题面 题目链接 题目描述 输入输出格式 输入格式 输出格式 输入输出样例 输入样例: 输出样例: 说明 说明 思路 AC代码 优化 优化后AC代码 总结 题面 题目链接 P3258 [JLOI2 ...

  7. 洛谷 P3258 [JLOI2014]松鼠的新家(树链剖分)

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

  8. 洛谷P3258 [JLOI2014]松鼠的新家(树上差分+树剖)

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

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

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

随机推荐

  1. 在 Linux 中自动启动 Confluence 6

    在 Linux/Solaris 环境下,最好的办法是对每一个服务进行安装和配置(包括 Confluence),同时配置这些服务权限为他们所在用户需要的服务权限即可, 为实例创建一个 Confluenc ...

  2. 设置 Confluence 6 日志

    Confluence 使用的是 Apache's log4j 日志服务.能够允许管理员通过编辑配置文件来控制日志的表现和日志输出文件.在系统中有 6 个日志输出级别,请参考 log4j logging ...

  3. vue-cli 3配置接口代理

    vue.config.js vue.config.js是一个可选的配置文件,新建该文件,存放在项目根目录(将自动加载)中 // 作为配置文件,直接导出配置对象即可 module.exports = { ...

  4. Redis简介与安装

    目录 Redis概述与安装使用 Redis概述与安装使用 Author:SimpleWu GitHub-redis Redis简介 Redis英语全称:( REmote DIctionary Serv ...

  5. Linux编程学习笔记(一)

    Linux的发展趋势势在必行,国内的服务器的操作系统Linux占到主导地位,不光是操作系统,还有嵌入式系统. 1. 今天就Linux的其中一个版本做一介绍,如下是Centos的版本之间的区别. ins ...

  6. vsftpd中的local_umask和anon_umask

    umask是在linux中常见的一个东西,它其实是一个掩码.当然,也有umask这样一个命令,它是对用户建立的文件的默认属性的定义.该 定义为: 假设umask为022,则对于一个文件夹的话,它的默认 ...

  7. java 托盘 实现二级菜单

    package com.zs; import java.awt.AWTException; import java.awt.CheckboxMenuItem; import java.awt.Fram ...

  8. SpringMVC + MyBatis + Mysql + Redis(作为二级缓存) 配置

    2016年03月03日 10:37:47 标签: mysql / redis / mybatis / spring mvc / spring 33805 项目环境: 在SpringMVC + MyBa ...

  9. python修改hosts

    #coding=utf-8 host = ['192.168.10.240 store.wondershare.com', '192.168.10.240 store.wondershare.jp', ...

  10. C++ Primer 笔记——IO类

    1.C++语言并未定义任何输入输出语句,取而代之,包含了一个全面的标准库来提供IO机制. 由上图能够知道,I/O操作的基类是ios_base,各个类的用途例如以下: <iostream> ...