bzoj 2588 Spoj 10628. Count on a tree

来源:互联网 发布:qq三国关羽打技能数据 编辑:程序博客网 时间:2024/05/21 17:45

Description

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

Input

第一行两个整数N,M。
第二行有N个整数,其中第i个整数表示点i的权值。
后面N-1行每行两个整数(x,y),表示点x到点y有一条边。
最后M行每行两个整数(u,v,k),表示一组询问。

Output

M行,表示每个询问的答案。

HINT:

N,M<=100000

本题是树上的可持久化线段树,与普通可持久化差不多,不同的是对于每个节点的插入,它的前棵线段树树应是它在树上的父亲节点对应的线段树,对于原树做一次dfs序,在到达每个节点时将其插入线段树即可;
由此构造出的权值线段树显然有着一个性质,每棵权值线段树都维护着一个节点到根结点的路径,由于权值线段树支持相互加减,所以树上任意两点u,v间的路径即为root[u]+root[v]-root[lca(u,v)]-root[fa[lca(u,v)][0]];然后对于每个询问按照可持久化线段树的操作查询即可。
注意主席树的空间要比平时多开十几倍,还有在建立原树时要用bfs,而且树上的边要存双向边;虽然是常识。。。但我依旧RE了许久。。
代码如下(第一次用倍增写LCA。。) :

#include<iostream>#include<cstdio>#include<cmath>#include<queue>#include<cstdlib>#include<cstring>#include<algorithm>#define N 100010#define M 2000010using namespace std;struct Edge { int to,next;Edge() { next=-1,to=-1; } }edge[N*2];struct node { int size;node *ch[2];node() { size=0;ch[0]=ch[1]=NULL; } }o[M];struct tree { int num,fa,d;tree() { d=num=fa=0; } }t[N];int n,m,head[N],tot,f[N][18],s[N];node *root[N],*null;node *out() { node *now=o+tot++;now->ch[0]=now->ch[1]=null;return now; }void Add_edge(int a,int b) {     edge[tot].to=b;edge[tot].next=head[a];head[a]=tot++;    edge[tot].to=a;edge[tot].next=head[b];head[b]=tot++;}int in() {    int s=0,v=0;char c;    while((c=getchar())<'0'||c>'9') if(c=='-') v=1;s=c-'0';    while((c=getchar())>='0'&&c<='9') s=s*10+c-'0';    return v?-s:s;}void insert(node *now,node *last,int l,int r,int x) {    now->size=last->size+1;if(last==NULL) last=null;    if(l==r) return;  int mid=l+r>>1,d=x>s[mid];    now->ch[d]=out();now->ch[d^1]=last->ch[d^1];    if(d) l=mid+1;else r=mid; insert(now->ch[d],last->ch[d],l,r,x);}int LCA(int x,int y) {    if(t[x].d>t[y].d) swap(x,y);int tmp=t[y].d-t[x].d;    for(int i=0;i<=17;i++) if(tmp&(1<<i)) y=f[y][i];    if(x==y) return x;    for(int i=17;i>=0;i--) if(f[x][i]!=f[y][i]) x=f[x][i],y=f[y][i];    return f[x][0];}void bfs() {    queue<int> q;q.push(1);t[1].d=1;    while(!q.empty()) {      int s=q.front();q.pop();      int i=head[s],next=edge[i].next;head[s]=-1;      for(i;i!=-1;i=next) {        if(t[edge[i].to].d) { next=edge[i].next;continue; }        next=edge[i].next;edge[i].next=head[s];head[s]=i;        t[edge[i].to].d=t[s].d+1; q.push(edge[i].to);      }    }return;}void build(int now,int fa) {    root[now]=out();insert(root[now],root[fa],1,s[0],t[now].num);    int i=0;f[now][i]=fa;while(f[now][i++]!=0&&i<=17) f[now][i]=f[f[now][i-1]][i-1];    for(int i=head[now];i!=-1;i=edge[i].next) { build(edge[i].to,now); }}int solve(int u,int v,int fa,int gr,int k) {    node *now[4];int ti=0,l=1,r=s[0];    now[0]=root[u];now[1]=root[v];    now[2]=root[fa];now[3]=root[gr];    while(l!=r) {      int mid=l+r>>1;      ti=now[0]->ch[0]->size+now[1]->ch[0]->size-now[2]->ch[0]->size-now[3]->ch[0]->size;      int d=ti<k; for(int i=0;i<4;i++) now[i]=now[i]->ch[d];      if(d) k-=ti,l=mid+1;else r=mid;    } return s[l];}int main() {    n=in();m=in();memset(head,-1,sizeof(head));    for(int i=1;i<=n;i++) s[i]=t[i].num=in(); sort(s+1,s+n+1);s[0]=n;    for(int i=1;i<n;i++) { int u=in(),v=in();if(u!=v) Add_edge(u,v); }    tot=0;null=out();null->ch[0]=null->ch[1]=null;    for(int i=0;i<=N;i++) root[i]=null; bfs();build(1,0);    int ans=0;for(int i=1;i<=m;i++) {      int u=in()^ans,v=in(),k=in();int fa=LCA(u,v);      ans=solve(u,v,fa,f[fa][0],k);printf("%d",ans);if(m-i) printf("\n");    } return 0;}
0 0
原创粉丝点击