【BZOJ-2588】Count on a tree 主席树 + 倍增
2588: Spoj 10628. Count on a tree
Time Limit: 12 Sec Memory Limit: 128 MB
Submit: 3749 Solved: 873
[Submit][Status][Discuss]
Description
xor lastans和v这两个节点间第K小的点权。其中lastans是上一个询问的答案,初始为0,即第一个询问的u是明文。
Input
Output
Sample Input
105 2 9 3 8 5 7 7
1 2
1
3
1 4
3 5
3 6
3 7
4 8
2 5 1
0 5 2
10 5 3
11 5
4
110 8 2
Sample Output
8
9
105
7
HINT
Source
Solution
区间第k小?必然是主席树
树上第k小?显然把主席树建在树上即可,亦或把主席树建在DFS序上
那么考虑把主席树建在树上,仍旧需要DFS序,还需要预处理出LCA,对建树和询问做一些修改即可
Code
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
int read()
{
int x=,f=; char ch=getchar();
while (ch<'' || ch>'') {if (ch=='-') f=-; ch=getchar();}
while (ch>='' && ch<='') {x=x*+ch-''; ch=getchar();}
return x*f;
}
#define maxn 100010
int n,m;int val[maxn];int ls[maxn],num;
struct data{int to,next;}edge[maxn<<];
int head[maxn],cnt;
void add(int u,int v){cnt++;edge[cnt].to=v;edge[cnt].next=head[u];head[u]=cnt;}
void insert(int u,int v){add(u,v);add(v,u);}
int steck[maxn],top,father[maxn][],deep[maxn],pos[maxn];
void dfs(int x)
{
steck[++top]=x; pos[x]=top;
for (int i=; i<=; i++)
if (deep[x]>=(<<i))
father[x][i]=father[father[x][i-]][i-];
else break;
for (int i=head[x]; i; i=edge[i].next)
if (father[x][]!=edge[i].to)
father[edge[i].to][]=x,deep[edge[i].to]=deep[x]+,dfs(edge[i].to);
}
int LCA(int x,int y)
{
if (deep[x]<deep[y]) swap(x,y);
int dd=deep[x]-deep[y];
for (int i=; i<=; i++)
if (dd&(<<i)) x=father[x][i];
for (int i=; i>=; i--)
if (father[x][i]!=father[y][i])
x=father[x][i],y=father[y][i];
if (x==y) return x; else return father[x][];
}
int sum[maxn*],ll[maxn*],rr[maxn*],root[maxn<<],sz;
void update(int l,int r,int &now,int fat,int va)
{
now=++sz; sum[now]=sum[fat]+;
if (l==r) return;
ll[now]=ll[fat],rr[now]=rr[fat];
int mid=(l+r)>>;
if (va<=mid) update(l,mid,ll[now],ll[fat],va);
else update(mid+,r,rr[now],rr[fat],va);
}
int query(int l,int r,int L,int R,int k)
{
int lca=LCA(L,R),fa=father[lca][];
int a=root[pos[L]],b=root[pos[R]],c=root[pos[lca]],d=root[pos[fa]];
while (l<r)
{
int mid=(l+r)>>;
int summ=sum[ll[a]]+sum[ll[b]]-sum[ll[c]]-sum[ll[d]];
if (summ>=k) r=mid,a=ll[a],b=ll[b],c=ll[c],d=ll[d];
else k-=summ,l=mid+,a=rr[a],b=rr[b],c=rr[c],d=rr[d];
}
return l;
}
int main()
{
n=read(),m=read();
for (int i=; i<=n; i++) val[i]=read(),ls[i]=val[i];
sort(ls+,ls+n+);
for (int i=; i<=n; i++) if (ls[i]!=ls[i-]) ls[++num]=ls[i];
for (int i=; i<=n; i++) val[i]=lower_bound(ls+,ls+num+,val[i])-ls;
for (int u,v,i=; i<=n-; i++)
u=read(),v=read(),insert(u,v);
dfs();
for (int tmp,i=; i<=n; i++)
tmp=steck[i],update(,num,root[i],root[pos[father[tmp][]]],val[tmp]);
int lastans=,ans=;
for (int u,v,k,i=; i<=m; i++)
{
u=read(),v=read(),k=read(); u^=lastans;
ans=query(,num,u,v,k); ans=ls[ans];
printf("%d",ans); if (i!=m) puts("");
lastans=ans;
}
return ;
}
你丫这题卡最后一行的行末换行...强行PE...好像切题又被蛋蛋发现了...

【BZOJ-2588】Count on a tree 主席树 + 倍增的更多相关文章
- BZOJ.2588.Count on a tree(主席树 静态树上第k小)
题目链接 /* 序列上的主席树 某点是利用前一个点的根建树 同理 树上的主席树 某个节点可以利用其父节点(is unique)的根建树 排名可以利用树上前缀和求得: 对于(u,v),w=LCA(u,v ...
- Bzoj 2588: Spoj 10628. Count on a tree 主席树,离散化,可持久,倍增LCA
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2588 2588: Spoj 10628. Count on a tree Time Limit ...
- BZOJ 2588: Spoj 10628. Count on a tree 主席树+lca
分析:树上第k小,然后我想说的是主席树并不局限于线性表 详细分析请看http://www.cnblogs.com/rausen/p/4006116.html,讲的很好, 然后因为这个熟悉了主席树,真是 ...
- 洛谷P2633/bzoj2588 Count on a tree (主席树)
洛谷P2633/bzoj2588 Count on a tree 题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K ...
- spoj cot: Count on a tree 主席树
10628. Count on a tree Problem code: COT You are given a tree with N nodes.The tree nodes are number ...
- 【BZOJ2588】Spoj 10628. Count on a tree 主席树+LCA
[BZOJ2588]Spoj 10628. Count on a tree Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lasta ...
- SPOJ Count on a tree(主席树+LCA)
一.题目 COT - Count on a tree You are given a tree with N nodes. The tree nodes are numbered from 1 to ...
- bzoj 2588 Count on a tree 解题报告
Count on a tree 题目描述 给定一棵\(N\)个节点的树,每个点有一个权值,对于\(M\)个询问\((u,v,k)\),你需要回答\(u\) \(xor\) \(lastans\)和\( ...
- 洛谷P2633 Count on a tree(主席树上树)
题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始为0,即第一个 ...
随机推荐
- 对Spring的IoC和DI最生动的解释
首先想说说IoC(Inversion of Control,控制倒转).这是spring的核心,贯穿始终.所谓IoC,对于spring框架来说,就是由spring来负责控制对象的生命周期和对象间的关系 ...
- 我发现:在StackOverflow上拯救歪果仁十分有意思!
菊长:火星特工们!今天是周五了,大家有什么提议? BeJavaGod:报告菊长!我发现,在StackOverflow上拯救歪果仁十分有意思! 噗哈哈,时不时遇到问题会使用到StackOverflow, ...
- Adivisor
1.Adivisor是一种特殊的Aspect,Advisor代表spring中的Aspect 2.区别:advisor只持有一个Pointcut和一个advice,而aspect可以多个pointcu ...
- Gitub
1.下载地址(注册:jackchn,jackchn@foxmail.com) http://windows.github.com/ 2.使用 github for Windows使用介绍 搭建一个免费 ...
- 如何利用ThoughtWorks.QRCode 生成二维码
1.引用ThoughtWorks.QRCode.dll 在nuget上查找即可引用,也可自行下载 2.生成二维码静态方法 参数: 二维码内容:fileUrl 二维码图片名:typeName #regi ...
- [4]Telerik Grid 简单使用方法
1.columns <% Html.Telerik().Grid(Model) .Name("Orders") .Columns(columns => { //绑定列名 ...
- android studio使用说明
一.学习的基本配置文档,搞好各种参数的基本配置,熟练使用. C:\Program Files\Java\jdk1.7.0_09\bin 二.problems meet in weather and ...
- WebApi 消息拦截
最近公司要求对WebApi 实现服务端信息的监控(服务端信息拦截),由于本人之前没有做过这方便的相关项目所以在做的过程中也是困难重重,探索的过程也是非常痛苦的,好歹最终也算实现了这个功能.所以将这个分 ...
- [资源]PHP使用消息队列
利用PHP操作Linux消息队列完成进程间通信 基于HTTP协议的轻量级开源简单队列服务:HTTPSQS[原创] Redis队列——PHP操作简单示例 入队操作 <?php $redis = n ...
- Caffe学习系列(21):caffe图形化操作工具digits的安装与运行
经过前面一系列的学习,我们基本上学会了如何在linux下运行caffe程序,也学会了如何用python接口进行数据及参数的可视化. 如果还没有学会的,请自行细细阅读: caffe学习系列:http:/ ...