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/JudgeOnline/problem.php?id=2588
Description
给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权。其中lastans是上一个询问的答案,初始为0,即第一个询问的u是明文。
Input
Output
Sample Input
8 5 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
2 8 9 105 7
HINT
题意
题解:
树上跑主席树
这类题都是这样搞……
代码来自hzwer:
代码:
//qscqesze #include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <bitset> #include <vector> #include <sstream> #include <queue> #include <typeinfo> #include <fstream> #include <map> #include <stack> typedef long long ll; using namespace std; //freopen("D.in","r",stdin); //freopen("D.out","w",stdout); #define sspeed ios_base::sync_with_stdio(0);cin.tie(0) #define maxn 2000005 #define eps 1e-9 int Num; //const int inf=0x7fffffff; //§ß§é§à§é¨f§3 const int inf=0x3f3f3f3f; inline ll read() {ll x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f; } //************************************************************************************** #define N 100005 #define M 2000005 int n,m,tot,sz,cnt,ind,last; int num[N],pos[N]; int v[N],tmp[N],hash[N],root[N]; int ls[M],rs[M],sum[M]; int deep[N],fa[N][17]; struct data{int to,next;}e[200005];int head[N]; void ins(int u,int v) {e[++cnt].to=v;e[cnt].next=head[u];head[u]=cnt;} void insert(int u,int v) {ins(u,v);ins(v,u);} int find(int x)//二分 {int l=1,r=tot;while(l<=r){int mid=(l+r)>>1;if(hash[mid]<x)l=mid+1;else if(hash[mid]==x)return mid;else r=mid-1;} } void dfs(int x)//lca预处理 {ind++;num[ind]=x;pos[x]=ind;for(int i=1;i<=16;i++)if((1<<i)<=deep[x])fa[x][i]=fa[fa[x][i-1]][i-1];else break;for(int i=head[x];i;i=e[i].next)if(fa[x][0]!=e[i].to){deep[e[i].to]=deep[x]+1;fa[e[i].to][0]=x;dfs(e[i].to);} } int lca(int x,int y)//lca {if(deep[x]<deep[y])swap(x,y);int t=deep[x]-deep[y];for(int i=0;i<=16;i++)if((1<<i)&t)x=fa[x][i];for(int i=16;i>=0;i--)if(fa[x][i]!=fa[y][i])x=fa[x][i],y=fa[y][i];if(x==y)return x;return fa[x][0]; } void update(int l,int r,int x,int &y,int num)//更新 {y=++sz;sum[y]=sum[x]+1;if(l==r)return;ls[y]=ls[x];rs[y]=rs[x];int mid=(l+r)>>1;if(num<=mid)update(l,mid,ls[x],ls[y],num);else update(mid+1,r,rs[x],rs[y],num); } int que(int x,int y,int rk) {int a=x,b=y,c=lca(x,y),d=fa[c][0];a=root[pos[a]],b=root[pos[b]],c=root[pos[c]],d=root[pos[d]];int l=1,r=tot;while(l<r){int mid=(l+r)>>1;int tmp=sum[ls[a]]+sum[ls[b]]-sum[ls[c]]-sum[ls[d]];if(tmp>=rk)r=mid,a=ls[a],b=ls[b],c=ls[c],d=ls[d];else rk-=tmp,l=mid+1,a=rs[a],b=rs[b],c=rs[c],d=rs[d];}return hash[l]; } int main() {//freopen("test.txt","r",stdin);n=read(),m=read();for(int i=1;i<=n;i++)v[i]=read(),tmp[i]=v[i];sort(tmp+1,tmp+n+1);hash[++tot]=tmp[1];for(int i=2;i<=n;i++)if(tmp[i]!=tmp[i-1])hash[++tot]=tmp[i];for(int i=1;i<=n;i++)v[i]=find(v[i]);for(int i=1;i<n;i++){int u=read(),v=read();insert(u,v);}dfs(1);for(int i=1;i<=n;i++){int t=num[i];update(1,tot,root[pos[fa[t][0]]],root[i],v[t]);}for(int i=1;i<=m;i++){int x=read(),y=read(),rk=read();x^=last;last=que(x,y,rk);printf("%d",last);if(i!=m)printf("\n");}return 0; }
转载于:https://www.cnblogs.com/qscqesze/p/4785256.html
BZOJ 2588: Spoj 10628. Count on a tree 树上跑主席树相关推荐
- BZOJ 2588 Spoj 10628 Count on a tree | 树上主席树
BZOJ 2588 Count on a tree 题面 求树上两点之间路径上第k大的点权. 题解 一开始看到这道题觉得是树剖,然后又听说是主席树,然后以为是主席树+树剖,差点没吓死-- 然后发现,如 ...
- BZOJ 2588: Spoj 10628. Count on a tree( LCA + 主席树 )
Orz..跑得还挺快的#10 自从会树链剖分后LCA就没写过倍增了... 这道题用可持久化线段树..点x的线段树表示ROOT到x的这条路径上的权值线段树 ----------------------- ...
- bzoj 2588 Spoj 10628. Count on a tree (可持久化线段树)
Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MB Submit: 7669 Solved: 1894 [Sub ...
- 主席树 || 可持久化线段树 || LCA || BZOJ 2588: Spoj 10628. Count on a tree || Luogu P2633 Count on a tree...
题面: Count on a tree 题解: 主席树维护每个节点到根节点的权值出现次数,大体和主席树典型做法差不多,对于询问(X,Y),答案要计算ans(X)+ans(Y)-ans(LCA(X,Y) ...
- 洛谷 2633 BZOJ 2588 Spoj 10628. Count on a tree
[题解] 蜜汁强制在线... 每个点开一个从它到根的可持久化权值线段树.查询的时候利用差分的思想在树上左右横跳就好了. 1 #include<cstdio> 2 #include<a ...
- SPOJ 10628 Count on a tree 主席树 附数据生成器
很奇怪的题啊,感觉思路和别人一样,但是我得用快速IO才能AC--不然就T 没用快速output,只用了快速input 而且居然限制代码长度...代码要短于6000B,我改了好久啊 题目大意:给一棵树, ...
- bzoj2588: Spoj 10628. Count on a tree(树上第k大)(主席树)
每个节点继承父节点的树,则答案为query(root[x]+root[y]-root[lca(x,y)]-root[fa[lca(x,y)]]) #include<iostream> #i ...
- bzoj2588: Spoj 10628. Count on a tree 主席树
在每一个点的父亲做主席树,每次访问时用两个点的和减去其LCA和LCA父亲的和即可. #include<bits/stdc++.h> using namespace std; inline ...
- 【SPOJ】Count On A Tree II(树上莫队)
[SPOJ]Count On A Tree II(树上莫队) 题面 洛谷 Vjudge 洛谷上有翻译啦 题解 如果不在树上就是一个很裸很裸的莫队 现在在树上,就是一个很裸很裸的树上莫队啦. #incl ...
最新文章
- ×××S 2008 实用小技巧
- linux x64 asm 参数传递,NASM汇编学习系列(1)——系统调用和参数传递
- mysql本地连接报错1130_mySql连接问题(本地连接加远程连接)
- c mysql安装教程 pdf_MySQL下载安装、配置与使用教程详细版(win7x64)
- 用手机EchoEcho问询朋友所在的位置
- android:paddingtop 百分比,相对层中的百分比宽度
- vue踩坑-This relative module was not found
- Interval(区间类问题)
- java虚拟机jvm_java虚拟机jvm - zhuyuansj的个人空间 - OSCHINA - 中文开源技术交流社区...
- 我的“技术架构”之旅
- 【Vue: 使用pdf.js顯示PDF Viewer】
- Ubuntu中将otf字体转换为ttf字体
- python中文版加密解密_python - 入门-对中英文加密解密
- Chrome 常用扩展插件
- 解除网页无法选择文字_无法复制
- Python绘制简单版K线图
- JAVA乐居租房网的设计与实现计算机毕业设计Mybatis+系统+数据库+调试部署
- 盘点科技公司十大裁员风波:诺基亚累计裁员近半(转)
- mysql limit 丢数据_产品操作MySQL第6篇 – 数据过滤-LIMIT子句
- Android+iOS自动化基于appium环境搭建
热门文章
- linux 股票指南针,IOS开发入门之ios指南针
- 操作系统:Win10系统下LocalNow和Roaming文件夹介绍
- 计算机基础:存储系统知识笔记(一)
- 常用 SQL Server 规范集锦
- 什么叫工作到位?很深刻!
- ds证据理论python实现_ALI模型理论以及Python实现
- 403 forbidden_[SpringSecurity] 自定义403页面
- android 屏幕旋转不重新加载,Android webview旋转屏幕导致页面重新加载问题解决办法...
- session中存放一个对象,只修改对象的属性,不将修改后的对象存放session,发现session中存放的对象也发生改变!
- netflix 工作原理_Netflix如何在屏幕后面工作?