[TS-A1505] [清橙2013中国国家集训队第二次作业] 树 [可持久化线段树,求树上路径第k大]
按Dfs序逐个插入点,建立可持久化线段树,每次查询即可,具体详见代码。
不知道为什么,代码慢的要死,,
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <vector> using namespace std; template<const int _n,const int _m>
struct Edge
{
struct Edge_base { int to,next; }e[_m];
int cnt,p[_n];
Edge() { clear(); }
void insert(const int x,const int y)
{ e[++cnt].to=y; e[cnt].next=p[x]; p[x]=cnt; return ; }
int start(const int x) { return p[x]; }
void clear() { cnt=,memset(p,,sizeof(p)); }
Edge_base& operator[](const int x) { return e[x]; }
}; Edge<,> e;
vector<int> vec;
int n,q,tot,cnt;
int a[],root[],Left[],Right[];
int val[],f[][],depth[],lg2[]; void Init()
{
for(int j=;(<<j)<=n;++j)
for(int i=;i<=n;++i)
f[i][j]=f[f[i][j-]][j-];
for(int i=;i<=n;++i)lg2[i]=lg2[i>>]+;
return ;
} int Lca(int A,int B)
{
int i,j;
if(depth[A]<depth[B])swap(A,B); j=lg2[depth[A]]; for(i=j;i>=;--i)
{
if(depth[A]-(<<i)>=depth[B])A=f[A][i];
} if(A==B)return A; for(i=j;i>=;--i)
{
if(f[A][i] && f[A][i]!=f[B][i])
A=f[A][i],B=f[B][i];
}
return f[A][];
} void Insert(const int l,const int r,const int root_l,int& root_r,const int d)
{
val[root_r=++tot]=val[root_l]+;
if(l==r)return ; int mid=l+((r-l)>>);
if(d<=mid)
{
Right[root_r]=Right[root_l];
Insert(l,mid,Left[root_l],Left[root_r],d);
}
else
{
Left[root_r]=Left[root_l];
Insert(mid+,r,Right[root_l],Right[root_r],d);
} return ;
} void Dfs(const int S,const int fa)
{
depth[S]=depth[fa]+;
f[S][]=fa; a[S]=lower_bound(vec.begin(),vec.end(),a[S])-vec.begin()+;
Insert(,vec.size(),root[fa],root[S],a[S]); for(int i=e.start(S);i;i=e[i].next)
{
if(e[i].to==fa)continue;
Dfs(e[i].to,S);
}
} int kth(int l,int r,int r1,int r2,int r3,int r4,int d)
{
while(true)
{
if(l==r)return l;
int mid=l+((r-l)>>),temp;
temp=val[Left[r1]]+val[Left[r2]]-val[Left[r3]]-val[Left[r4]];
if(temp>=d)r=mid,r1=Left[r1],r2=Left[r2],r3=Left[r3],r4=Left[r4];
else l=mid+,r1=Right[r1],r2=Right[r2],r3=Right[r3],r4=Right[r4],d=d-temp;
}
return ;
} int main()
{
//freopen("in","r",stdin); int i,x,y,op; vec.resize();
scanf("%d%d",&n,&q); for(i=;i<=n;++i)
scanf("%d",&a[i]),vec.push_back(a[i]); sort(vec.begin(),vec.end());
vec.erase(unique(vec.begin(),vec.end()),vec.end()); for(i=;i<n;++i)
{
scanf("%d%d",&x,&y);
e.insert(x,y);
e.insert(y,x);
} Dfs(,);
Init(); for(i=;i<=q;++i)
{
scanf("%d%d%d",&op,&x,&y);
int temp=Lca(x,y),temp1;
if(op==)
temp1=kth(,vec.size(),
root[x],root[y],
root[temp],root[f[temp][]],);
else if(op==)
temp1=kth(,vec.size(),
root[x],root[y],
root[temp],root[f[temp][]],
depth[x]+depth[y]-(depth[temp]<<)+);
else temp1=kth(,vec.size(),
root[x],root[y],
root[temp],root[f[temp][]],
((depth[x]+depth[y]-(depth[temp]<<)+)>>)+); printf("%d\n",vec[temp1-]);
} return ;
}
[TS-A1505] [清橙2013中国国家集训队第二次作业] 树 [可持久化线段树,求树上路径第k大]的更多相关文章
- [tsA1490][2013中国国家集训队第二次作业]osu![概率dp+线段树+矩阵乘法]
这样的题解只能舔题解了,,,qaq 清橙资料里有.. #include <iostream> #include <cstdio> #include <cstdlib> ...
- [tsA1491][2013中国国家集训队第二次作业]家族[并查集]
m方枚举,并查集O(1)维护,傻逼题,,被自己吓死搞成神题了... #include <bits/stdc++.h> using namespace std; struct tri { i ...
- [TS-A1489][2013中国国家集训队第二次作业]抽奖[概率dp]
概率dp第一题,开始根本没搞懂,后来看了09年汤可因论文才基本搞懂,关键就是递推的时候做差比较一下,考虑新加入的情况对期望值的贡献,然后推推公式(好像还是不太会推qaq...) #include &l ...
- [TS-A1488][2013中国国家集训队第二次作业]魔法波[高斯消元]
暴力直接解异或方程组,O(n^6)无法接受,那么我们考虑把格子分块,横着和竖着分别分为互不影响的块,这样因为障碍物最多不超过200个,那么块的个数最多为2*(800+200)=2000个,最后用bit ...
- [TS-A1487][2013中国国家集训队第二次作业]分配游戏[二分]
根据题意,设$3n$次比较中胜了$w$次,负了$l$次,平了$d$次,所有场次中胜了$W$次,负了$L$次,平了$D$次.如果一场赢了,那么$w-l$就会$+1$,相同地,$W-L$也会$+1$:如果 ...
- [TS-A1486][2013中国国家集训队第二次作业]树[树的重心,点分治]
首先考虑暴力,可以枚举每两个点求lca进行计算,复杂度O(n^3logn),再考虑如果枚举每个点作为lca去枚举这个点的子树中的点复杂度会大幅下降,如果我们将每个点递归考虑,每次计算过这个点就把这个点 ...
- < < < 2013年国家集训队作业 > > >
完成题数/总题数: 道/37道 1. A1504. Book(王迪): 数论+贪心 ★★☆ 2013中国国家集训队第二次作业 2. A1505. 树(张闻涛): 倍增LCA+可 ...
- Tsinsen A1505. 树(张闻涛) 倍增LCA,可持久化线段树,DFS序
题目:http://www.tsinsen.com/A1505 A1505. 树(张闻涛) 时间限制:1.0s 内存限制:512.0MB 总提交次数:196 AC次数:65 平均分: ...
- [BZOJ 3123] [SDOI 2013]森林(可持久化线段树+并查集+启发式合并)
[BZOJ 3123] [SDOI 2013]森林(可持久化线段树+启发式合并) 题面 给出一个n个节点m条边的森林,每个节点都有一个权值.有两种操作: Q x y k查询点x到点y路径上所有的权值中 ...
随机推荐
- 新版chrome调整开发者工具位置方式改变
转自:https://blog.csdn.net/gsls200808/article/details/70244150 本文所指新版56.0.2924.87 (64-bit) 原来F12可以看到窗口 ...
- [Swift通天遁地]四、网络和线程-(9)上传图片并实时显示上传进度
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- [Swift通天遁地]八、媒体与动画-(10)在项目中播放GIF动画
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- 安装git,创建本地版本库
安装 由于我使用的是Ubuntu,因此安装很简单,输入:sudo apt-get install git 如果是其他Linux版本,可以直接通过源码安装.先从Git官网下载源码,然后解压,依次输入:. ...
- 解决gradle project refresh failed: protocol family unavailable问题的几种方法
Android Studio从版本1.5更新到2.1之后,打开Android Studio一直提示: gradle project refresh failed: protocol family un ...
- Linux-fork()函数详解,附代码注释
// // main.c // Project_C // // Created by LiJinxu on 16/8/13. // Copyright © 2016年 LiJinxu-NEU. All ...
- 跨域请求之jsonp
1.什么是跨域请求: 服务器A上的一个页面,要请求服务器B上的一个处理程序,这就叫做跨域请求 本次的测试页面为: 处理程序kimhandler.ashx,如下: http://qxw119243026 ...
- Android Studio查看CPU使用率。
进入AS自带的CMD,依次输入: (1)进入Android Atudio安卓的目录: 1.H: 2.cd AndroidStudio\sdk\platform-tools (2)adb shell ( ...
- .net 大数据量,查找Where优化(List的Contains与Dictionary的ContainsKey的比较)
最近优化一个where查询条件,查询时间很慢,改为用Dictionary就很快了. 一.样例 假设:listPicsTemp 有100w条数据,pictures有1000w条数据. 使用第1段代码执 ...
- SQLServer 异常捕获,回滚,再抛出
一个存储过程中多个更新操作,后面的更新操作出现异常,如果不手动回滚前面修改的数据是不会自动撤销的! BEGIN TRY BEGIN TRAN -- ..... COMMIT TRAN END TRY ...