BZOJ 2588: Spoj 10628. Count on a tree|主席树
来源:互联网 发布:java记事本课程设计 编辑:程序博客网 时间:2024/05/21 19:50
用主席树维护这个点到根上的数,然后Lca上的数单独处理!!!!!!!!!
#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<queue>#include<vector>#include<set>#include<map>#include<iostream>#include<algorithm>#define mx 1e9#define lowbit(x) (x&(-x))#define N 505using namespace std;int sc(){ int i=0,f=1; char c=getchar(); while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();} while(c>='0'&&c<='9')i=i*10+c-'0',c=getchar(); return i*f;}struct W{int x,pos;}A[100010];int head[100010],nxt[200010],lst[200010];int size[100010],deep[100010],fa[100010],top[100010];int root[100010],ch[10000010][2],sum[10000010];int v[100010];int n,m,tot,cnt,ans;void insert(int x,int y){ lst[++tot]=y; nxt[tot]=head[x]; head[x]=tot;}bool cmp(W a,W b){return a.x<b.x;}void add(int pre,int &x,int l,int r,int v){ if(!x)x=++cnt; sum[x]=sum[pre]+1; if(l==r)return; int mid=l+r>>1; if(v<=mid) { ch[x][1]=ch[pre][1]; add(ch[pre][0],ch[x][0],l,mid,v); } else { ch[x][0]=ch[pre][0]; add(ch[pre][1],ch[x][1],mid+1,r,v); }}void dfs(int x,int f){ add(root[f],root[x],1,n,v[x]); size[x]=1; for(int i=head[x];i;i=nxt[i]) if(lst[i]!=f) { fa[lst[i]]=x; deep[lst[i]]=deep[x]+1; dfs(lst[i],x); size[x]+=size[lst[i]]; }}void Dfs(int x,int htp){ top[x]=htp; int k=0; for(int i=head[x];i;i=nxt[i]) if(lst[i]!=fa[x]&&size[lst[i]]>size[k])k=lst[i]; if(!k)return;Dfs(k,htp); for(int i=head[x];i;i=nxt[i]) if(lst[i]!=fa[x]&&lst[i]!=k) Dfs(lst[i],lst[i]);}int Lca(int x,int y){ while(top[x]!=top[y]) { if(deep[top[x]]<deep[top[y]])swap(x,y); x=fa[top[x]]; } return deep[x]<deep[y]?x:y;}int ask(int x,int y,int k){ int f=Lca(x,y),l=1,r=n,w=v[f]; int a=root[x],b=root[y],c=root[f]; while(l!=r) { int e=0,mid=l+r>>1; int Lsum=sum[ch[a][0]]+sum[ch[b][0]]-2*sum[ch[c][0]]+(w<=mid&&w>=l); if(Lsum<k) k-=Lsum,e=1,l=mid+1; else r=mid; a=ch[a][e]; b=ch[b][e]; c=ch[c][e]; } return A[l].x;}int main(){ n=sc(),m=sc(); for(int i=1;i<=n;i++) { A[i].x=sc(); A[i].pos=i; } sort(A+1,A+n+1,cmp); for(int i=1;i<=n;i++) v[A[i].pos]=i; for(int i=1;i<n;i++) { int x=sc(),y=sc(); insert(x,y); insert(y,x); } dfs(1,0); Dfs(1,1); for(int i=1;i<=m;i++) { int x=sc(),y=sc(),k=sc(); printf("%d",ans=ask(x^ans,y,k)); if(i!=m)puts(""); } return 0;}
0 0
- BZOJ 2588: Spoj 10628. Count on a tree|主席树
- [主席树] BZOJ 2588 Spoj 10628. Count on a tree
- BZOJ 2588 Spoj 10628. Count on a tree 主席树
- BZOJ BZOJ 2588: Spoj 10628. Count on a tree 树上主席树
- 【BZOJ 2588】 Spoj 10628. Count on a tree|树上K大|树链剖分|主席树
- bzoj 2588: Spoj 10628. Count on a tree(树上主席树)
- 【BZOJ 2588】Spoj 10628. Count on a tree 主席树+树上差分
- Bzoj 2588 Spoj 10628. Count on a tree(树链剖分LCA+主席树)
- [BZOJ]2588: Spoj 10628. Count on a tree 主席树+LCA
- 【BZOJ】2588 Spoj 10628. Count on a tree LCA+主席树
- 2588: Spoj 10628. Count on a tree 主席树+LCA
- 【主席树】 SPOJ Count on a tree
- spoj Count on a tree【主席树】
- [BZOJ 2588][SPOJ COT]Count On a Tree(DFS序主席树)
- [省选前题目整理][BZOJ 2588][SPOJ COT]Count On a Tree(DFS序主席树)
- bzoj 2588 Spoj 10628 Count on a tree 树链剖分 主席树
- 【bzoj2588】Spoj 10628. Count on a tree LCA+主席树
- 【BZOJ2588】Spoj 10628. Count on a tree【主席树】【LCA】
- android判断是否联网----无耻的截图别人的
- 程序出错后 程序员给测试人员的20条高频回复
- c/c++题目汇总一
- CDZSC_2016寒假个人赛(2)-L(模拟)
- 成功的背后!(给所有IT人)
- BZOJ 2588: Spoj 10628. Count on a tree|主席树
- ZOJ-2970-Faster, Higher, Stronger【5th浙江省赛】
- mybatis 技术总结
- 从视频中获取每一帧图片(包括第一帧)
- 简单代码收集
- Java认证考试实例疑难辨析(10)
- 讲讲移动测试的流程
- 【spider】之 Scrapy初次体验
- 快速运行solr