标题效果:两棵树之间的首次查询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) 是持久的段树的更多相关文章

  1. bzoj 2588 Count on a tree 解题报告

    Count on a tree 题目描述 给定一棵\(N\)个节点的树,每个点有一个权值,对于\(M\)个询问\((u,v,k)\),你需要回答\(u\) \(xor\) \(lastans\)和\( ...

  2. BZOJ.2588.Count on a tree(主席树 静态树上第k小)

    题目链接 /* 序列上的主席树 某点是利用前一个点的根建树 同理 树上的主席树 某个节点可以利用其父节点(is unique)的根建树 排名可以利用树上前缀和求得: 对于(u,v),w=LCA(u,v ...

  3. bzoj 2588 Count on a tree

    Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始 ...

  4. 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 ...

  5. 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  ...

  6. 2588: Count on a tree

    敢问和zwt的树有何区别..改了读入直接交..四百个人A,三百多个PE..于是果断贡献几发PE.. http://ideone.com/9XCg3D

  7. 【BZOJ】【2588】COT(Count On a Tree)

    可持久化线段树 maya……树么……转化成序列……所以就写了个树链剖分……然后每个点保存的是从它到根的可持久化线段树. 然后就像序列一样查询……注意是多个左端点和多个右端点,处理方法类似BZOJ 19 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. mysql联合索引的应用

    有一个log表,结构是这样的: CREATE TABLE `weblog` (   `id` int(10) unsigned NOT NULL AUTO_INCREMENT,   `ip` varc ...

  2. Java程序猿笔试面试之String1

    1.怎样实现字符串的反转比如:"how are you"--->"you are how" public class InverseString { pu ...

  3. Learning Cocos2d-x for WP8(4)——中文显示

    原文:Learning Cocos2d-x for WP8(4)--中文显示 C#(wp7)兄弟篇Learning Cocos2d-x for XNA(4)——中文显示 Cocos2d-x中文显示,似 ...

  4. (转)SWT的CheckBoxTreeViewer的相关用法

    最近在项目中需要用到遍历某个目录下所有文件,并按照树形结构展示,同时还需要提供对树形菜单的选择展开等操作.在eclipse中提供了CheckboxTreeViewer组件来满足所需要的功能 下面是需要 ...

  5. 基于.net开发chrome核心浏览器【四】

    原文:基于.net开发chrome核心浏览器[四] 一: 上周去北京出差,给国家电网的项目做架构方案,每天都很晚睡,客户那边的副总也这样拼命工作. 累的不行了,直接导致第四篇文章没有按时发出来. 希望 ...

  6. Python入门(转)

    Python 简洁的语法和对动态输入的支持,再加上解释性语言的本质, 使得它在大多数平台上的许多领域都是一个理想的脚本语言,特别适用于快速的应用程序开发 特点:简单.易学.免费.开源.高层语言.可移植 ...

  7. Cocos2d-x 3.0 红孩儿私人义务教育 - 第一章 熟人Cocos2d-x 3.0project

     Cocos2d-x 3.0 红孩儿私家必修 前言: 时光飞逝,每每看到博客上的回复和微博上的鼓舞,总会认为亏欠大家点什么.停下来太久.总是认为不太对劲,哈哈,时习之吧,望以此勉励大家. 红孩儿C ...

  8. 将本地文件上传到指定的服务器(HttpWebRequest方法)

    将本地文件上传到指定的服务器(HttpWebRequest方法),通过文件流,带文件名,同文件一同上传的表单文本域及值. ///<summary> /// 将本地文件上传到指定的服务器(H ...

  9. 【Java先进】Lock、通过使用线程池

    import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util ...

  10. Java引用类型变量

    Java引用类型变量 1.编译时类型:由声明该变量时使用的类型决定 2.执行时类型:由实际赋给该变量的对象决定    类型不一致的假设编译时类型和执行,可能会出现多态性 版权声明:本文博主原创文章.博 ...