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. Oracle Database 11g : SQL 基础

    简介 1:课程目标 2:课程 目标 3:Oracle Database 11g 以及相关产品概览 1:Oracle Database 11g :重点领域 2:Oracle Fusion Middlew ...

  2. Confluence 6 MySQL 数据库设置准备

    请查看 Supported Platforms 页面来获得 Confluence 系统支持的 MySQL 数据库版本.你需要在安装 Confluence 之前升级你的 MySQL 数据库. 如果你从其 ...

  3. day 13 装饰器

    装饰器基础 装饰器的目的是为了给被装饰 对象,增加新功能,或者说增加某种能力 在程序中工具就是函数 如此一来,装饰器指的就是一个函数,被装饰着也是一个函数 总结;装饰器就是用一个函数去拓展另外一个已存 ...

  4. input标签 disabled 和 readonly的区别

    需求描述:今天提交代码,老大审了一下,给我指出了一个改正的地方,XXX的详细信息页面(不是修改页面) input的内容是不能改的,给我指出的时候,我立马就知道了这个该怎么改了,加个readonly不就 ...

  5. PDF如何添加水印,PDF添加水印工具的使用方法

    PDF文件在编辑修改的时候是需要借助工具才可以编辑,PDF文件不像普通的文件可以直接打开编辑,PDF编辑工具是PDF文件进行编辑的重要工具,就以添加水印为例,能够在PDF中添加水印的工具有哪些呢?要怎 ...

  6. C# Parallel并发执行相关问题

    1.Parallel并发执行 using System;using System.Collections.Generic;using System.Linq;using System.Text;usi ...

  7. zoj3659

    #include<iostream> #include<algorithm> #include<cstring> #define ll long long #inc ...

  8. Linux文件系统及文件类型

    Linux文件系统: 根文件系统(rootfs) root filesystem LSB, FHS: (FileSystem... /etc,  /usr,  /var,  /root.... /bo ...

  9. 论文阅读笔记二十八:You Only Look Once: Unified,Real-Time Object Detection(YOLO v1 CVPR2015)

    论文源址:https://arxiv.org/abs/1506.02640 tensorflow代码:https://github.com/nilboy/tensorflow-yolo 摘要 该文提出 ...

  10. Left-pad

    L1-032 Left-pad (20 分)   根据新浪微博上的消息,有一位开发者不满NPM(Node Package Manager)的做法,收回了自己的开源代码,其中包括一个叫left-pad的 ...