BZOJ 2588 Count on a tree (COT) 是持久的段树
标题效果:两棵树之间的首次查询k大点的权利。
思维:树木覆盖树,事实上,它是正常的树木覆盖了持久段树。
由于使用权值段树可以寻求区间k大,然后应用到持久段树思想,间隔可以做减法。详见代码。
CODE:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 100010
#define NIL (tree[0])
using namespace std; pair<int,int> src[MAX]; struct Complex{
Complex *son[2];
int cnt; Complex(Complex *_,Complex *__,int ___):cnt(___) {
son[0] = _;
son[1] = __;
}
Complex() {}
}*tree[MAX]; int points,asks;
int xx[MAX]; int head[MAX],total;
int next[MAX << 1],aim[MAX << 1]; int father[MAX][20],deep[MAX]; inline void Add(int x,int y);
void DFS(int x);
void SparseTable(); Complex *BuildTree(Complex *pos,int x,int y,int val);
int GetAns(Complex *l,Complex *r,Complex *f,Complex *p,int x,int y,int k);
int GetLCA(int x,int y);
int Ask(int x,int y,int k); int main()
{
cin >> points >> asks;
for(int i = 1;i <= points; ++i)
scanf("%d",&src[i].first),src[i].second = i;
sort(src + 1,src + points + 1);
for(int i = 1;i <= points; ++i)
xx[src[i].second] = i;
for(int x,y,i = 1;i < points; ++i) {
scanf("%d%d",&x,&y);
Add(x,y),Add(y,x);
}
NIL = new Complex(NULL,NULL,0);
NIL->son[0] = NIL->son[1] = NIL;
DFS(1);
SparseTable();
int now = 0;
for(int x,y,k,i = 1;i <= asks; ++i) {
scanf("%d%d%d",&x,&y,&k);
printf("%d",now = src[Ask(x ^ now,y,k)].first);
if(i != asks) puts("");
}
return 0;
} inline void Add(int x,int y)
{
next[++total] = head[x];
aim[total] = y;
head[x] = total;
} void DFS(int x)
{
deep[x] = deep[father[x][0]] + 1;
tree[x] = BuildTree(tree[father[x][0]],1,points,xx[x]);
for(int i = head[x];i;i = next[i]) {
if(aim[i] == father[x][0]) continue;
father[aim[i]][0] = x;
DFS(aim[i]);
}
} void SparseTable()
{
for(int j = 1;j <= 19; ++j)
for(int i = 1;i <= points; ++i)
father[i][j] = father[father[i][j - 1]][j - 1];
} int Ask(int x,int y,int k)
{
int lca = GetLCA(x,y);
return GetAns(tree[x],tree[y],tree[lca],tree[father[lca][0]],1,points,k);
} Complex *BuildTree(Complex *pos,int x,int y,int val)
{
int mid = (x + y) >> 1;
if(x == y) return new Complex(NIL,NIL,pos->cnt + 1);
if(val <= mid) return new Complex(BuildTree(pos->son[0],x,mid,val),pos->son[1],pos->cnt + 1);
return new Complex(pos->son[0],BuildTree(pos->son[1],mid + 1,y,val),pos->cnt + 1);
} int GetLCA(int x,int y)
{
if(deep[x] < deep[y]) swap(x,y);
for(int i = 19; ~i; --i)
if(deep[father[x][i]] >= deep[y])
x = father[x][i];
if(x == y) return x;
for(int i = 19; ~i; --i)
if(father[x][i] != father[y][i])
x = father[x][i],y = father[y][i];
return father[x][0];
} int GetAns(Complex *l,Complex *r,Complex *f,Complex *p,int x,int y,int k)
{
if(x == y) return x;
int mid = (x + y) >> 1;
int temp = l->son[0]->cnt + r->son[0]->cnt - f->son[0]->cnt - p->son[0]->cnt;
if(k <= temp) return GetAns(l->son[0],r->son[0],f->son[0],p->son[0],x,mid,k);
return GetAns(l->son[1],r->son[1],f->son[1],p->son[1],mid + 1,y,k - temp);
}
版权声明:本文博客原创文章。博客,未经同意,不得转载。
BZOJ 2588 Count on a tree (COT) 是持久的段树的更多相关文章
- bzoj 2588 Count on a tree 解题报告
Count on a tree 题目描述 给定一棵\(N\)个节点的树,每个点有一个权值,对于\(M\)个询问\((u,v,k)\),你需要回答\(u\) \(xor\) \(lastans\)和\( ...
- BZOJ.2588.Count on a tree(主席树 静态树上第k小)
题目链接 /* 序列上的主席树 某点是利用前一个点的根建树 同理 树上的主席树 某个节点可以利用其父节点(is unique)的根建树 排名可以利用树上前缀和求得: 对于(u,v),w=LCA(u,v ...
- bzoj 2588 Count on a tree
Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始 ...
- SPOJ 10628 COT - Count on a tree(在树上建立主席树)(LCA)
COT - Count on a tree #tree You are given a tree with N nodes.The tree nodes are numbered from 1 to ...
- SPOJ 10628 Count on a tree(Tarjan离线LCA+主席树求树上第K小)
COT - Count on a tree #tree You are given a tree with N nodes.The tree nodes are numbered from 1 to ...
- 2588: Count on a tree
敢问和zwt的树有何区别..改了读入直接交..四百个人A,三百多个PE..于是果断贡献几发PE.. http://ideone.com/9XCg3D
- 【BZOJ】【2588】COT(Count On a Tree)
可持久化线段树 maya……树么……转化成序列……所以就写了个树链剖分……然后每个点保存的是从它到根的可持久化线段树. 然后就像序列一样查询……注意是多个左端点和多个右端点,处理方法类似BZOJ 19 ...
- BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 5217 Solved: 1233 ...
- BZOJ 2588: Spoj 10628. Count on a tree 树上跑主席树
2588: Spoj 10628. Count on a tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/J ...
随机推荐
- ural1018(树形dp)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=17662 题意:给一棵边有权值的二叉树,节点编号为1-n,1是根节点 ...
- [WPF] 使用Grid与GridSplitter排版布局
原文:[WPF] 使用Grid与GridSplitter排版布局 前言 在開發應用程式時,一個很重要的工作項目就是設計使用者介面的排版布局.WPF中所提供的Grid控制項,讓開發人員擁有將版面分割為欄 ...
- vi 按了ctrl+s之后
再windows不管是写程序.还是用Word写文件.已经习惯了按ctrl+s 保存代码. 在用vi的时候.常常无意中按了ctrl+s,结果就是如同终端死掉了一样. 这是由于ctrl+s 终止屏幕输出( ...
- ASP.NET 成员资格 Part.2(使用安全控件 Login)
原文:ASP.NET 成员资格 Part.2(使用安全控件 Login) 准备好提供程序以及用户信息的存储,就可以开始构建验证用户.注册用户或者让用户能够重置密码的用户界面了.ASP.N ...
- EF 批量 循环删除
var list = db.T_xAppRecord.Where(u => u.Id == 1).ToList(); //2.0 遍历集合,将 要删除的 对象 的代理对象的State 设置为 D ...
- log4net和一般的记录日志方法
下载 http://files.cnblogs.com/crazyair/log4net.zip 1 在web项目中新建一个 Log4Net.config <?xml version=" ...
- Android编程心得-Service数据绑定初步
在Android里,Service的数据绑定是一种重要的用法,我们知道Service与Activity一样是运行在当前应用进程的主线程里面的,他们之间交互的方式有多种,下面我来介绍一下如何使用数据绑定 ...
- Java Evaluate Reverse Polish Notation(逆波兰式)
表情:: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) ...
- 怎么样MyEclipse配置Tomcat?
1.下载tomcat免安装版.tomcat路径不包含空格 http://download.csdn.net/detail/u014112584/7549191 2.windows -preferenc ...
- ACE定时器
每一秒钟打印一行 http://www.tuicool.com/articles/Zb263e 计时器的打开和关闭封装 http://andylin02.iteye.com/blog/440572 自 ...