题目

P3258 [JLOI2014]松鼠的新家

解析

非常裸的一道树剖题

链上修改+单点查询的板子

记录一下所经过的点\(now[i]\),每次更新\(now[i-1]到now[i]\)

我们链上更新时上一次到的终点,是这一次一次更新的起点,又因为在\(a_n\)处可以不放糖,所以我们每次链上更新完成后,在上条链的终点位置处糖数\(-1\)。

然后套板子直接做

代码

#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 10;
int n, m, num, cnt;
int head[N], size[N], f[N], top[N], son[N], dep[N], now[N], id[N];
class node {
public :
int v, nx;
} e[N];
class tree {
public :
int sum, lazy;
int len;
} t[N]; #define lson rt << 1
#define rson rt << 1 | 1 template<class T>inline void read(T &x) {
x = 0; int f = 0; char ch = getchar();
while (!isdigit(ch)) f |= (ch == '-'), ch = getchar();
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
x = f ? -x : x;
return ;
} inline void add(int u, int v) {
e[++num].nx = head[u], e[num].v = v, head[u] = num;
} void dfs1(int u, int fa) {
size[u] = 1;
for (int i = head[u]; ~i; i = e[i].nx) {
int v = e[i].v;
if (v != fa) {
dep[v] = dep[u] + 1;
f[v] = u;
dfs1(v, u);
size[u] += size[v];
if (size[v] > size[son[u]]) son[u] = v;
}
}
} void dfs2(int u, int t) {
id[u] = ++cnt; top[u] = t;
if (son[u]) dfs2(son[u], t);
for (int i = head[u]; ~i; i = e[i].nx) {
int v = e[i].v;
if (v != f[u] && v != son[u]) dfs2(v, v); //WRONG
}
} inline void pushup(int rt) {
t[rt].sum = t[lson].sum + t[rson].sum;
} void build(int l, int r, int rt) {
t[rt].len = r - l + 1;
if (l == r) return;
int m = (l + r) >> 1;
build(l, m, lson);
build(m + 1, r, rson);
} inline void pushdown(int rt) {
if (t[rt].lazy) {
t[lson].lazy += t[rt].lazy;
t[rson].lazy += t[rt].lazy;
t[lson].sum += t[rt].lazy * t[lson].len;
t[rson].sum += t[rt].lazy * t[rson].len;
t[rt].lazy = 0;
}
} void update(int L, int R, int c, int l, int r, int rt) {
if (L <= l && r <= R) {
t[rt].sum += (t[rt].len * c);
t[rt].lazy += c;
return;
}
pushdown(rt);
int m = (l + r) >> 1;
if (L <= m) update(L, R, c, l, m, lson);
if (R > m) update(L, R, c, m + 1, r, rson);
pushup(rt);
} int query(int L, int R, int l, int r, int rt) {
if (L <= l && r <= R) return t[rt].sum;
pushdown(rt);
int m = (l + r) >> 1, ans = 0;
if (L <= m) ans += query(L, R, l, m, lson);
if (R > m) ans += query(L, R, m + 1, r, rson);
return ans;
} void update_chain(int x, int y, int z) {
int fx = top[x], fy = top[y];
while (fx != fy) {
if (dep[fx] < dep[fy]) swap(fx, fy), swap(x, y);
update(id[fx], id[x], z, 1, cnt, 1);
x = f[fx], fx = top[x];
}
if (id[x] > id[y]) swap(x, y);
update(id[x], id[y], z, 1, cnt, 1);
} int main() {
memset(head, -1, sizeof(head));
read(n);
for (int i = 1, x; i <= n; ++i) read(x), now[i] = x;
for (int i = 1, x, y; i < n; ++i) read(x), read(y), add(x, y), add(y, x);
f[1] = 0, dep[1] = 0;
dfs1(1, 0);
dfs2(1, 1);
build(1, n, 1);
for (int i = 2; i <= n; ++i) update_chain(now[i - 1], now[i], 1), update_chain(now[i], now[i], -1);
for (int i = 1; i <= n; ++i) printf("%d\n", query(id[i], id[i], 1, n, 1));
return 0;
}

[JLOI2014]松鼠的新家 (树剖)的更多相关文章

  1. Bzoj 3631: [JLOI2014]松鼠的新家(树链剖分+线段树)

    3631: [JLOI2014]松鼠的新家 Time Limit: 10 Sec Memory Limit: 128 MB Description 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个 ...

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

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

  3. BZOJ 3631: [JLOI2014]松鼠的新家( 树链剖分 )

    裸树链剖分... ------------------------------------------------------------------- #include<bits/stdc++ ...

  4. P3258 [JLOI2014]松鼠的新家 树链剖分

    这个题就是一道树剖板子题,就是每走一步就把所有的经过点加一就行了.还有,我的树剖板子没问题!!!谁知道为什么板子T3个点!我不管了!反正这道题正常写A了. 题干: 题目描述 松鼠的新家是一棵树,前几天 ...

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

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

  6. [JLOI2014]松鼠的新家-树链剖分

    最开始的时候我在写线段树部分的时候还打了一个build,后来一想,打个球球大作战的build啊!!!有个锤子的用啊!!! #include<bits/stdc++.h> using nam ...

  7. 树链剖分 [JLOI2014]松鼠的新家

    [JLOI2014]松鼠的新家 时间限制: 1 Sec  内存限制: 128 MB 题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达, ...

  8. [JLOI2014]松鼠的新家(树链剖分)

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

  9. [填坑]树上差分 例题:[JLOI2014]松鼠的新家(LCA)

    今天算是把LCA这个坑填上了一点点,又复习(其实是预习)了一下树上差分.其实普通的差分我还是会的,树上的嘛,也是懂原理的就是没怎么打过. 我们先来把树上差分能做到的看一下: 1.找所有路径公共覆盖的边 ...

随机推荐

  1. 主题模型TopicModel:主题模型LDA的应用

    http://blog.csdn.net/pipisorry/article/details/45665779 主题模型LDA的应用 拿到这些topic后继续后面的这些应用怎么做呢:除了推断出这些主题 ...

  2. Emotion Recognition Using Graph Convolutional Networks

    Emotion Recognition Using Graph Convolutional Networks 2019-10-22 09:26:56 This blog is from: https: ...

  3. 支付宝小程序开发——获取位置API没有城市区号的最佳处理方案

    前言: 需要对城市区号进行判断,但是支付宝小程序提供的my.getLocation() API返回的数据中只有6位的城市行政代码,诸如:深圳(440300),并没有区号(0755),那么怎么办呢? 需 ...

  4. 010-Spring aop 001-核心说明-拦截指定类与方法、基于自定义注解的切面

    一.概述 面向切面编程(AOP)是针对面向对象编程(OOP)的补充,可以非侵入式的为多个不具有继承关系的对象引入相同的公共行为例如日志.安全.事务.性能监控等等.SpringAOP允许将公共行为从业务 ...

  5. google cloud storage products

    https://cloud.google.com/products/storage/ BigTable Cloud Bigtable 是 Google 面向大数据领域的 NoSQL 数据库服务.它也是 ...

  6. Invalid prop: custom validator check failed for prop "pagination" <Table> vue.runtime.esm

    错误如图 原因,返回数据中没有包括分布的属性

  7. echo的色彩处理

    在Shell脚本中,可以使用echo的-e选项使显示内容呈现出不同的颜色. 格式1:echo -e "\033[背景颜色代码;文字颜色代码m 输出的字符串 \033[0m" 格式2 ...

  8. oracle 涨工资

    declare cursor cemp is select empno ,sal from emp order by sal; --定义参数 pempno emp.empno%type; psal e ...

  9. sql 在查询到的语句基础上添加行号

    正常查询语句: SELECT TagName FROM ps_status a WHERE a.TagName LIKE "DTmk_zybf%1bxxjcqh.PV" 查询结果: ...

  10. [No000019A]IDEA 设置手册

    [No000019A]idea设置手册.rar IDEA 设置手册 IDEA 设置手册 plugin lgnore files and folesrs 代码管控 程序框架 部署方式 useless 3 ...