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

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 const int maxn = 3e6+5;
  4 int n;
  5 int e,begin[maxn],next[maxn],to[maxn],a[maxn];
  6 int tree[maxn<<2],lazy[maxn<<2];
  7 int son[maxn],father[maxn],id[maxn],cnt,deep[maxn],size[maxn],top[maxn];
  8 int res;
  9 inline int read(){
 10    int s=0,w=1;
 11    char ch=getchar();
 12    while(ch<='0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
 13    while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
 14    return s*w;
 15 }
 16 inline void add(int x,int y){
 17     to[++e] = y;
 18     next[e] = begin[x];
 19     begin[x] = e;
 20 }
 21 inline void pushup(int root){
 22     tree[root] = tree[root<<1]+tree[root<<1|1];
 23 }
 24 inline void pushdown(int root,int pos){
 25     lazy[root<<1] += lazy[root];
 26     lazy[root<<1|1] += lazy[root];
 27     tree[root<<1] += lazy[root]*(pos-(pos>>1));
 28     tree[root<<1|1] += lazy[root]*(pos>>1);
 29     lazy[root] = 0;
 30 }
 31 inline void query(int root,int l,int r,int al,int ar){
 32     if(al <= l && ar >= r){
 33         res += tree[root];
 34         return;
 35     }
 36     if(lazy[root])pushdown(root,r-l+1);
 37     int mid = l+r>>1;
 38     if(al <= mid)query(root<<1,l,mid,al,ar);
 39     if(ar > mid)query(root<<1|1,mid+1,r,al,ar);
 40 }
 41 inline void update(int root,int l,int r,int al,int ar,int k){
 42     if(al <= l && ar >= r){
 43         lazy[root] += k;
 44         tree[root] += k*(r-l+1);
 45         return;
 46     }
 47     if(lazy[root])pushdown(root,r-l+1);
 48     int mid = l+r>>1;
 49     if(al <= mid)update(root<<1,l,mid,al,ar,k);
 50     if(ar > mid)update(root<<1|1,mid+1,r,al,ar,k);
 51     pushup(root);
 52 }
 53 inline void update_range(int x,int y,int k){
 54     while(top[x] != top[y]){
 55         if(deep[top[x]] < deep[top[y]])swap(x,y);
 56         update(1,1,n,id[top[x]],id[x],k);
 57         x = father[top[x]];
 58     }
 59     if(deep[x] > deep[y])swap(x,y);
 60     update(1,1,n,id[x],id[y],k);
 61 }
 62 inline void dfs1(int x,int fa,int dep){
 63     deep[x] = dep;
 64     father[x] = fa;
 65     size[x] = 1;
 66     int maxson = -1;
 67     for(int i = begin[x];i;i = next[i]){
 68         int y = to[i];
 69         if(y == fa)continue;
 70         dfs1(y,x,dep+1);
 71         size[x] += size[y];
 72         if(size[y] > maxson)son[x] = y,maxson = size[y];
 73     }
 74 }
 75 inline void dfs2(int x,int topf){
 76     id[x] = ++cnt;
 77     top[x] = topf;
 78     if(!son[x])return;
 79     dfs2(son[x],topf);
 80     for(int i = begin[x];i;i = next[i]){
 81         int y = to[i];
 82         if(y == father[x] || y == son[x])continue;
 83         dfs2(y,y);
 84     }
 85 }
 86 int main(){
 87     n = read();
 88     for(int i = 1;i <= n;i++)a[i] = read();
 89     for(int i = 1,x,y;i < n;i++){
 90         x = read();
 91         y = read();
 92         add(x,y);
 93         add(y,x);
 94     }
 95     dfs1(1,0,1);
 96     dfs2(1,1);
 97     for(int i = 1;i < n;i++){
 98         update_range(a[i],a[i+1],1);
 99         update_range(a[i+1],a[i+1],-1);
100     }
101     for(int i = 1;i <= n;i++)res = 0,query(1,1,n,id[i],id[i]),printf("%d\n",res);
102     return 0;
103 }

转载于:https://www.cnblogs.com/wangyifan124/p/10312332.html

[JLOI2014]松鼠的新家-树链剖分相关推荐

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

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

  2. [BZOJ3631][JLOI2014]松鼠的新家(链剖)

    题目描述 传送门 题解 小傻逼手残 随便写链剖. 代码 #include<iostream> #include<cstring> #include<cstdio> ...

  3. P3258 [JLOI2014]松鼠的新家

    文章目录 题意: 题解: 树上差分 代码: 树链剖分 代码: P3258 [JLOI2014]松鼠的新家 题意: n个点,n-1条边,给出每个点的拜访顺序,问每个点经过几次(最后一次移动不算拜访) 题 ...

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

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

  5. 3631: [JLOI2014]松鼠的新家

    3631: [JLOI2014]松鼠的新家 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 707  Solved: 342 [Submit][Sta ...

  6. 【BZOJ 3631】 [JLOI2014]松鼠的新家

    3631: [JLOI2014]松鼠的新家 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 681 Solved: 329 [Submit][Statu ...

  7. bzoj3631[JLOI2014]松鼠的新家

    bzoj3631[JLOI2014]松鼠的新家 题意: 给个n点树,再给个节点的游览顺序,每经过一个节点(包括上一个游览的点到下一个游览的点路径上的点)就可以从这个节点拿走一个糖,问所有节点一开始要放 ...

  8. [Luogu 3258] JLOI2014 松鼠的新家

    [Luogu 3258] JLOI2014 松鼠的新家 LCA + 树上差分. 我呢,因为是树剖求的 LCA,预处理了 DFN(DFS 序),于是简化成了序列差分. qwq不讲了不讲了,贴代码. #i ...

  9. bzoj 3631: [JLOI2014]松鼠的新家(LCA+树上差分)

    3631: [JLOI2014]松鼠的新家 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 2059  Solved: 1030 [Submit][S ...

最新文章

  1. javascript十六进制数字和ASCII字符之间转换
  2. 基于MySQL数据库下亿级数据的分库分表
  3. 用 go 实现跨平台 Autoit/AutoHotkey 和按键精灵功能示例代码
  4. gis怎么通过水库划分子流域_到底是谁在革GIS的命?
  5. linux用两种方法找到cuond,很基础的Linuxshell脚本学习.doc
  6. spring WebSocket详解
  7. 数据分析词数统计和词的重要程度统计
  8. 用分布式缓存提升ASP.NET Core性能
  9. mysql报错 DuplicateKeyException分析与解决
  10. Node路由简单的处理
  11. Android 自定义RecyclerView 实现真正的Gallery效果
  12. alpha冲刺-事后诸葛亮
  13. 斯坦福大学深度学习公开课cs231n学习笔记(7)神经网络防止数据过拟合:损失函数和正则化
  14. 7内置数据结构_dict
  15. matlab图像模糊处理
  16. 现在出纳记账手写还是用计算机,请问:出纳现金日记账和银行日记账是否一定要用手写归档能否用电脑打印归档?...
  17. 挑战程序设计竞赛-小笔记
  18. mschart控件 用法
  19. php中文九九乘法表,PHP输出九九乘法表代码实例
  20. uni-app Android端获取设备已连接的WiFi IP地址

热门文章

  1. mysql开通远程连接不上去_解决MySQL远程连接不上的问题
  2. 胸大肌(02):史密斯卧推
  3. 无心剑汉英双语诗002.《春江》
  4. Mac打开swf文件
  5. 计算机英语口语练习,计算机英语会话(MP3+中英字幕) 第22期:办公自动化(1)
  6. 51nod 3034 矩形的数量 V2
  7. VIM实用指南(16)vim粘贴格式错乱
  8. 2004-12-01 终于把自己卖了,南京中兴
  9. JAVA系列06:30天Java核心技术P72~P80
  10. html工地项目管理,工程项目管理系统Demo 进行在线体验