树剖,裸题,鉴定完毕。

我是题面

读完题,恩,树剖,裸题,没劲。

处理很简单,既然每到一个房间吃一块糖,那么就在每条路径上的每个房间放一颗糖,但是每条路径的终点也就是下一条路径的起点,在这里只能加一次,所以别忘记处理完再-1,又因为最后一个点不需要糖,所以直接每条路径的终点的糖-1即可

上代码

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cctype>
#define ll long long
#define gc() getchar()
#define maxn 300005
using namespace std; inline ll read(){
ll a=0;int f=0;char p=gc();
while(!isdigit(p)){f|=p=='-';p=gc();}
while(isdigit(p)){a=(a<<3)+(a<<1)+(p^48);p=gc();}
return f?-a:a;
}
void write(ll a){
if(a>9)write(a/10);
putchar(a%10+'0');
}
int n,m,a[maxn]; int tot,head[maxn];
struct ahaha1{
int to,next;
}e[maxn<<1];
inline void add(int u,int v){
e[tot].to=v;e[tot].next=head[u];head[u]=tot++;
} int sz[maxn],dep[maxn],son[maxn],f[maxn];
void dfs(int u,int fa){
int maxa=0;sz[u]=1;
for(int i=head[u];~i;i=e[i].next){
int v=e[i].to;if(v==fa)continue;
f[v]=u;dep[v]=dep[u]+1;
dfs(v,u);sz[u]+=sz[v];
if(maxa<sz[v])maxa=sz[v],son[u]=v;
}
}
int top[maxn],in[maxn],b[maxn];
void dfs(int u,int fa,int topf){
top[u]=topf;in[u]=++tot;b[tot]=u;
if(!son[u])return;
dfs(son[u],u,topf);
for(int i=head[u];~i;i=e[i].next){
int v=e[i].to;if(v==fa||v==son[u])continue;
dfs(v,u,v);
}
} #define lc p<<1
#define rc p<<1|1
struct ahaha2{
ll v,lz;
}t[maxn<<2];
inline void pushup(int p){
t[p].v=t[lc].v+t[rc].v;
}
inline void pushdown(int p,int l,int r){
int m=l+r>>1;
t[lc].v+=t[p].lz*(m-l+1);t[lc].lz+=t[p].lz;
t[rc].v+=t[p].lz*(r-m);t[rc].lz+=t[p].lz;
t[p].lz=0;
}
void update(int p,int l,int r,int L,int R){
if(l>R||r<L)return;
if(L<=l&&r<=R){t[p].v+=r-l+1;t[p].lz++;return;}
int m=l+r>>1;if(t[p].lz)pushdown(p,l,r);
update(lc,l,m,L,R);update(rc,m+1,r,L,R);
pushup(p);
}
void update2(int p,int l,int r,int L){
if(l==r){t[p].v--;return;}
int m=l+r>>1;if(t[p].lz)pushdown(p,l,r);
if(m>=L)update2(lc,l,m,L);
else update2(rc,m+1,r,L);
pushup(p);
}
ll query(int p,int l,int r,int L){
if(l==r)return t[p].v;
int m=l+r>>1;if(t[p].lz)pushdown(p,l,r);
if(m>=L)return query(lc,l,m,L);
else return query(rc,m+1,r,L);
} inline void solve_1(int x,int y){
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]])swap(x,y);
update(1,1,n,in[top[x]],in[x]);
x=f[top[x]];
}
if(dep[x]>dep[y])swap(x,y);
update(1,1,n,in[x],in[y]);
} int main(){memset(head,-1,sizeof head);
n=read();
for(int i=1;i<=n;++i)a[i]=read();
for(int i=1;i<n;++i){
int x=read(),y=read();
add(x,y);add(y,x);
}
tot=0;dfs(1,-1);dfs(1,-1,1);
for(int i=2;i<=n;++i)
solve_1(a[i-1],a[i]),update2(1,1,n,in[a[i]]);
for(int i=1;i<=n;++i)
write(query(1,1,n,in[i])),putchar('\n');
return 0;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    https://www.luogu.org/problem/show?pid=3258 题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到 ...

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

    简要题意 树上n个节点,给定路径,求每个点经过次数 题意分析 对于每两个点,有两种情况,第一种,他们的lca为本身,第二种,他们有公共祖先,又要求他们的点经过次数,暴力是不可能的,复杂度不对,所以可以 ...

  9. 【洛谷P3258】松鼠的新家

    很好的一道题 LCA+树上前缀和 sum数组是前缀和数组, 分类讨论一下, 1.访问到一个点p1,若下一个点p2需要往儿子下面找的话,那么lca就是这个点p1,则sum[p1]--; sum[p2]+ ...

随机推荐

  1. PostgreSQL的 synchronous_standby_names 参数学习

    磨砺技术珠矶,践行数据之道,追求卓越价值回到上一级页面: PostgreSQL集群方案相关索引页     回到顶级页面:PostgreSQL索引页[作者 高健@博客园  luckyjackgao@gm ...

  2. RHCSA-考前准备

    1.考前准备 RHCSA classroom虚拟机和server虚拟机 将两台虚拟机切换到初始化快照 打开虚拟机电源,当出现提示时选择我已移动该虚拟机 系统密码: classroom: root As ...

  3. .net core 使用windows版redis

    在项目中为了减少程序占用内存(将结果保存在全局变量里面,会占用内存),要求使用redis.开始了爬坑的过程.o(╥﹏╥)o c#操作redis 基本就这3中情况: ServiceStack.Redis ...

  4. 搜索引擎ElasticSearch系列(四): ElasticSearch2.4.4 sql插件安装

    一:ElasticSearch sql插件简介 With this plugin you can query elasticsearch using familiar SQL syntax. You ...

  5. 【C#利用后台动态加载数据】Winform“防界面卡死”【BackgroundWorker】类

    using System.ComponentModel 直接使用EgProgressBar方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 2 ...

  6. Python range() 函数用法

    函数语法 range(start, stop[, step]) 参数说明: start: 计数从 start 开始.默认是从 0 开始.例如range(5)等价于range(0, 5); stop: ...

  7. 你的第一个接口测试:Python 接口测试

    前言: 首先我们先明确一个概念,什么叫接口.什么叫接口测试? 接口的全称叫[Application Programming Interface 又叫API],是提供应用程序与开发人员基于某软件或硬件得 ...

  8. (转)python+opencv实现动态物体追踪

    原文链接:https://blog.csdn.net/cike14/article/details/50649811 import cv2 import numpy as np camera=cv2. ...

  9. PHPCMS如何让手机站点取消浏览大图直接加载原图

    一.然后找到phpcms\modules\wap\functions\global.func.php 文件,找到相关代码,如下图: return '<img src="'.thumb( ...

  10. nodejs express 加载html模板

    在nodejs中如使用express框架,她默认的是ejs和jade渲染模板.由于我在使用的时候觉得她的代码书写方式很不爽还是想用html的形式去书写,于是我找了使用了html模板. 直接上代码,主要 ...