最开始的时候我在写线段树部分的时候还打了一个build,后来一想,打个球球大作战的build啊!!!有个锤子的用啊!!!

 #include<bits/stdc++.h>
using namespace std;
const int maxn = 3e6+;
int n;
int e,begin[maxn],next[maxn],to[maxn],a[maxn];
int tree[maxn<<],lazy[maxn<<];
int son[maxn],father[maxn],id[maxn],cnt,deep[maxn],size[maxn],top[maxn];
int res;
inline int read(){
int s=,w=;
char ch=getchar();
while(ch<=''||ch>''){if(ch=='-')w=-;ch=getchar();}
while(ch>=''&&ch<='') s=s*+ch-'',ch=getchar();
return s*w;
}
inline void add(int x,int y){
to[++e] = y;
next[e] = begin[x];
begin[x] = e;
}
inline void pushup(int root){
tree[root] = tree[root<<]+tree[root<<|];
}
inline void pushdown(int root,int pos){
lazy[root<<] += lazy[root];
lazy[root<<|] += lazy[root];
tree[root<<] += lazy[root]*(pos-(pos>>));
tree[root<<|] += lazy[root]*(pos>>);
lazy[root] = ;
}
inline void query(int root,int l,int r,int al,int ar){
if(al <= l && ar >= r){
res += tree[root];
return;
}
if(lazy[root])pushdown(root,r-l+);
int mid = l+r>>;
if(al <= mid)query(root<<,l,mid,al,ar);
if(ar > mid)query(root<<|,mid+,r,al,ar);
}
inline void update(int root,int l,int r,int al,int ar,int k){
if(al <= l && ar >= r){
lazy[root] += k;
tree[root] += k*(r-l+);
return;
}
if(lazy[root])pushdown(root,r-l+);
int mid = l+r>>;
if(al <= mid)update(root<<,l,mid,al,ar,k);
if(ar > mid)update(root<<|,mid+,r,al,ar,k);
pushup(root);
}
inline void update_range(int x,int y,int k){
while(top[x] != top[y]){
if(deep[top[x]] < deep[top[y]])swap(x,y);
update(,,n,id[top[x]],id[x],k);
x = father[top[x]];
}
if(deep[x] > deep[y])swap(x,y);
update(,,n,id[x],id[y],k);
}
inline void dfs1(int x,int fa,int dep){
deep[x] = dep;
father[x] = fa;
size[x] = ;
int maxson = -;
for(int i = begin[x];i;i = next[i]){
int y = to[i];
if(y == fa)continue;
dfs1(y,x,dep+);
size[x] += size[y];
if(size[y] > maxson)son[x] = y,maxson = size[y];
}
}
inline void dfs2(int x,int topf){
id[x] = ++cnt;
top[x] = topf;
if(!son[x])return;
dfs2(son[x],topf);
for(int i = begin[x];i;i = next[i]){
int y = to[i];
if(y == father[x] || y == son[x])continue;
dfs2(y,y);
}
}
int main(){
n = read();
for(int i = ;i <= n;i++)a[i] = read();
for(int i = ,x,y;i < n;i++){
x = read();
y = read();
add(x,y);
add(y,x);
}
dfs1(,,);
dfs2(,);
for(int i = ;i < n;i++){
update_range(a[i],a[i+],);
update_range(a[i+],a[i+],-);
}
for(int i = ;i <= n;i++)res = ,query(,,n,id[i],id[i]),printf("%d\n",res);
return ;
}

[JLOI2014]松鼠的新家-树链剖分的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. BZOJ3631 松鼠的新家(树链剖分)

    题目链接 松鼠的新家 差不多可以说是树链剖分的模板题了,直接维护即可. #include <bits/stdc++.h> using namespace std; #define REP( ...

  7. 【BZOJ3631】松鼠的新家 树链剖分

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

  8. P3285 松鼠的新家 (树链剖分)

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

  9. BZOJ - 3631 松鼠的新家 (树链剖分)

    题目链接 树链剖分基础题,路径权值修改+差分 #include<bits/stdc++.h> using namespace std; typedef long long ll; ; in ...

随机推荐

  1. Python进阶11---异常及模块化

    异常处理 异常Exception 产生异常

  2. Flask--特殊装饰器, CBV, 三方组件

    一. Flask中的特殊装饰器 before_request # before_request 是在视图函数执行之前执行的 @app.before_request def before(): prin ...

  3. POJ2385——Apple Catching

                                                $Apple~Catching$ Time Limit: 1000MS   Memory Limit: 6553 ...

  4. feign多文件上传

    参考地址:https://www.cnblogs.com/standup/p/9090113.html https://www.cnblogs.com/standup/p/9093753.html 1 ...

  5. PAT 甲级真题题解(1-62)

    准备每天刷两题PAT真题.(一句话题解) 1001 A+B Format  模拟输出,注意格式 #include <cstdio> #include <cstring> #in ...

  6. 01Design and Analysis Algorithm Using Python-程振波

    1.(p14)比较两个数的大小 a = int(input('num:')) b = int(input('num:')) def getMax(a,b): if a>b : print('Th ...

  7. Arrays和String单元测试(课下作业,选做)

    在IDEA中以TDD的方式对String类和Arrays类进行学习- 测试相关方法的正常,错误和边界情况 - String类 - charAt - split - Arrays类 - sort - b ...

  8. CodeForces - 314C Sereja and Subsequences (树状数组+dp)

    Sereja has a sequence that consists of n positive integers, a1, a2, ..., an. First Sereja took a pie ...

  9. HTTP协议7之Cookie--转

    Cookie是HTTP协议中非常重要的东西, 之前拜读了Fish Li 写的[细说Cookie], 让我学到了很多东西.Fish的这篇文章写得太经典了. 所以我这篇文章就没有太多内容了. 最近我打算写 ...

  10. hadoop记录-hadoop集群日常运维命令

    hadoop集群日常运维命令 #1.namenode hadoop namenode -format #格式化,慎用 su hdfs hadoop-daemon.sh start namenode h ...