按Dfs序逐个插入点,建立可持久化线段树,每次查询即可,具体详见代码。

不知道为什么,代码慢的要死,,

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstdio>
  4. #include <cstdlib>
  5. #include <cstring>
  6. #include <cmath>
  7. #include <ctime>
  8. #include <vector>
  9.  
  10. using namespace std;
  11.  
  12. template<const int _n,const int _m>
  13. struct Edge
  14. {
  15. struct Edge_base { int to,next; }e[_m];
  16. int cnt,p[_n];
  17. Edge() { clear(); }
  18. void insert(const int x,const int y)
  19. { e[++cnt].to=y; e[cnt].next=p[x]; p[x]=cnt; return ; }
  20. int start(const int x) { return p[x]; }
  21. void clear() { cnt=,memset(p,,sizeof(p)); }
  22. Edge_base& operator[](const int x) { return e[x]; }
  23. };
  24.  
  25. Edge<,> e;
  26. vector<int> vec;
  27. int n,q,tot,cnt;
  28. int a[],root[],Left[],Right[];
  29. int val[],f[][],depth[],lg2[];
  30.  
  31. void Init()
  32. {
  33. for(int j=;(<<j)<=n;++j)
  34. for(int i=;i<=n;++i)
  35. f[i][j]=f[f[i][j-]][j-];
  36. for(int i=;i<=n;++i)lg2[i]=lg2[i>>]+;
  37. return ;
  38. }
  39.  
  40. int Lca(int A,int B)
  41. {
  42. int i,j;
  43. if(depth[A]<depth[B])swap(A,B);
  44.  
  45. j=lg2[depth[A]];
  46.  
  47. for(i=j;i>=;--i)
  48. {
  49. if(depth[A]-(<<i)>=depth[B])A=f[A][i];
  50. }
  51.  
  52. if(A==B)return A;
  53.  
  54. for(i=j;i>=;--i)
  55. {
  56. if(f[A][i] && f[A][i]!=f[B][i])
  57. A=f[A][i],B=f[B][i];
  58. }
  59. return f[A][];
  60. }
  61.  
  62. void Insert(const int l,const int r,const int root_l,int& root_r,const int d)
  63. {
  64. val[root_r=++tot]=val[root_l]+;
  65. if(l==r)return ;
  66.  
  67. int mid=l+((r-l)>>);
  68. if(d<=mid)
  69. {
  70. Right[root_r]=Right[root_l];
  71. Insert(l,mid,Left[root_l],Left[root_r],d);
  72. }
  73. else
  74. {
  75. Left[root_r]=Left[root_l];
  76. Insert(mid+,r,Right[root_l],Right[root_r],d);
  77. }
  78.  
  79. return ;
  80. }
  81.  
  82. void Dfs(const int S,const int fa)
  83. {
  84. depth[S]=depth[fa]+;
  85. f[S][]=fa;
  86.  
  87. a[S]=lower_bound(vec.begin(),vec.end(),a[S])-vec.begin()+;
  88. Insert(,vec.size(),root[fa],root[S],a[S]);
  89.  
  90. for(int i=e.start(S);i;i=e[i].next)
  91. {
  92. if(e[i].to==fa)continue;
  93. Dfs(e[i].to,S);
  94. }
  95. }
  96.  
  97. int kth(int l,int r,int r1,int r2,int r3,int r4,int d)
  98. {
  99. while(true)
  100. {
  101. if(l==r)return l;
  102. int mid=l+((r-l)>>),temp;
  103. temp=val[Left[r1]]+val[Left[r2]]-val[Left[r3]]-val[Left[r4]];
  104. if(temp>=d)r=mid,r1=Left[r1],r2=Left[r2],r3=Left[r3],r4=Left[r4];
  105. else l=mid+,r1=Right[r1],r2=Right[r2],r3=Right[r3],r4=Right[r4],d=d-temp;
  106. }
  107. return ;
  108. }
  109.  
  110. int main()
  111. {
  112. //freopen("in","r",stdin);
  113.  
  114. int i,x,y,op;
  115.  
  116. vec.resize();
  117. scanf("%d%d",&n,&q);
  118.  
  119. for(i=;i<=n;++i)
  120. scanf("%d",&a[i]),vec.push_back(a[i]);
  121.  
  122. sort(vec.begin(),vec.end());
  123. vec.erase(unique(vec.begin(),vec.end()),vec.end());
  124.  
  125. for(i=;i<n;++i)
  126. {
  127. scanf("%d%d",&x,&y);
  128. e.insert(x,y);
  129. e.insert(y,x);
  130. }
  131.  
  132. Dfs(,);
  133. Init();
  134.  
  135. for(i=;i<=q;++i)
  136. {
  137. scanf("%d%d%d",&op,&x,&y);
  138. int temp=Lca(x,y),temp1;
  139. if(op==)
  140. temp1=kth(,vec.size(),
  141. root[x],root[y],
  142. root[temp],root[f[temp][]],);
  143. else if(op==)
  144. temp1=kth(,vec.size(),
  145. root[x],root[y],
  146. root[temp],root[f[temp][]],
  147. depth[x]+depth[y]-(depth[temp]<<)+);
  148. else temp1=kth(,vec.size(),
  149. root[x],root[y],
  150. root[temp],root[f[temp][]],
  151. ((depth[x]+depth[y]-(depth[temp]<<)+)>>)+);
  152.  
  153. printf("%d\n",vec[temp1-]);
  154. }
  155.  
  156. return ;
  157. }

[TS-A1505] [清橙2013中国国家集训队第二次作业] 树 [可持久化线段树,求树上路径第k大]的更多相关文章

  1. [tsA1490][2013中国国家集训队第二次作业]osu![概率dp+线段树+矩阵乘法]

    这样的题解只能舔题解了,,,qaq 清橙资料里有.. #include <iostream> #include <cstdio> #include <cstdlib> ...

  2. [tsA1491][2013中国国家集训队第二次作业]家族[并查集]

    m方枚举,并查集O(1)维护,傻逼题,,被自己吓死搞成神题了... #include <bits/stdc++.h> using namespace std; struct tri { i ...

  3. [TS-A1489][2013中国国家集训队第二次作业]抽奖[概率dp]

    概率dp第一题,开始根本没搞懂,后来看了09年汤可因论文才基本搞懂,关键就是递推的时候做差比较一下,考虑新加入的情况对期望值的贡献,然后推推公式(好像还是不太会推qaq...) #include &l ...

  4. [TS-A1488][2013中国国家集训队第二次作业]魔法波[高斯消元]

    暴力直接解异或方程组,O(n^6)无法接受,那么我们考虑把格子分块,横着和竖着分别分为互不影响的块,这样因为障碍物最多不超过200个,那么块的个数最多为2*(800+200)=2000个,最后用bit ...

  5. [TS-A1487][2013中国国家集训队第二次作业]分配游戏[二分]

    根据题意,设$3n$次比较中胜了$w$次,负了$l$次,平了$d$次,所有场次中胜了$W$次,负了$L$次,平了$D$次.如果一场赢了,那么$w-l$就会$+1$,相同地,$W-L$也会$+1$:如果 ...

  6. [TS-A1486][2013中国国家集训队第二次作业]树[树的重心,点分治]

    首先考虑暴力,可以枚举每两个点求lca进行计算,复杂度O(n^3logn),再考虑如果枚举每个点作为lca去枚举这个点的子树中的点复杂度会大幅下降,如果我们将每个点递归考虑,每次计算过这个点就把这个点 ...

  7. < < < 2013年国家集训队作业 > > >

    完成题数/总题数:  道/37道 1.  A1504. Book(王迪): 数论+贪心   ★★☆        2013中国国家集训队第二次作业 2.  A1505. 树(张闻涛): 倍增LCA+可 ...

  8. Tsinsen A1505. 树(张闻涛) 倍增LCA,可持久化线段树,DFS序

    题目:http://www.tsinsen.com/A1505 A1505. 树(张闻涛) 时间限制:1.0s   内存限制:512.0MB    总提交次数:196   AC次数:65   平均分: ...

  9. [BZOJ 3123] [SDOI 2013]森林(可持久化线段树+并查集+启发式合并)

    [BZOJ 3123] [SDOI 2013]森林(可持久化线段树+启发式合并) 题面 给出一个n个节点m条边的森林,每个节点都有一个权值.有两种操作: Q x y k查询点x到点y路径上所有的权值中 ...

随机推荐

  1. bzoj2194

    http://www.lydsy.com/JudgeOnline/problem.php?id=2194 卷积... 卷积并不高深,其实卷积就是两个多项式相乘的系数,但是得满足一点条件,就是f[n]= ...

  2. 腾讯云linux服务器安装lnmp一键包

    这边域名已经实名了. 然后修改DNS服务器 然后备案吧 还是先不备案,直接云解析DNS 哦,想起来了,阿里云自己都可以生成SSL证书.重新弄一次吧,其实腾讯云也可以申请域名型免费版DV

  3. go多进程

    package main import "fmt" import "time"func loop() { for i := 0; i < 10; i++ ...

  4. [App Store Connect帮助]三、管理 App 和版本(2.5)输入 App 信息:本地化 App Store 信息

    在添加 App 至您的帐户之后,您可以在“App 信息”页面添加语言并输入本地化元数据.若要查看受支持的语言列表,请参见 App Store 本地化.若要了解您可以本地化的属性,请参见必填项.可本地化 ...

  5. JAVA、C、C++、Python同样是高级语言,为什么只有C和C++可以编写单片机程序?

    JAVA.C.C++.Python这四种编程语言,前三种玩的比较多,python做为兴趣爱好或者玩脚本的时候弄过,编程语言在使用的时候主要还是适合不合适,单片机使用的场景属于功能简单,成本相对较低,现 ...

  6. Java常用的数组排序算法(面试宝典)

    这段时间有些忙,今天空闲出来给大家分享下Java中常用的数组排序算,有冒泡排序.快速排序.选择排序.插入排序.希尔算法.并归排序算法.堆排序算法,以上排序算法中,前面几种相对后面的比较容易理解一些.下 ...

  7. 汇编程序44:检测点13.1 (jmp near ptr 标号指令的中断例程)

    安装程序: assume cs:code //jmp near ptr 标号指令的替代实现,使用iret指令 code segment start: mov ax,cs mov ds,ax mov s ...

  8. 看无可看 分治FFT+特征值方程

    题面: 看无可看(see.pas/cpp/c) 题目描述 “What’s left to see when our eyes won’t open?” “若彼此瞑目在即,是否终亦看无可看?” ---- ...

  9. hdu2027

    http://acm.hdu.edu.cn/showproblem.php?pid=2027 #include<iostream> #include<stdio.h> #inc ...

  10. 题解报告:hdu 1232 畅通工程(并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1232 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了 ...