bzoj2588

题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2588

就是静态区间第K大的变形。
每个节点为一棵线段树,表示到根的路径中,以权值为下标的线段树。
每个节点建树的时候,以父亲为历史版本。
对于询问点x和点y的时候,就是求ask(x)+ask(y)-ask(lca)-ask(fa[lca]),然后用类似于静态区间第K的方法决定是去左子树还是右子树。
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<utility>
#include<set>
#include<bitset>
#include<vector>
#include<functional>
#include<deque>
#include<cctype>
#include<climits>
#include<complex>
//#include<bits/stdc++.h>适用于CF,UOJ,但不适用于poj
 
using namespace std;

typedef long long LL;
typedef double DB;
typedef pair<int,int> PII;
typedef complex<DB> CP;

#define mmst(a,v) memset(a,v,sizeof(a))
#define mmcy(a,b) memcpy(a,b,sizeof(a))
#define re(i,a,b)  for(i=a;i<=b;i++)
#define red(i,a,b) for(i=a;i>=b;i--)
#define fi first
#define se second
#define m_p(a,b) make_pair(a,b)
#define SF scanf
#define PF printf
#define two(k) (1<<(k))

template<class T>inline T sqr(T x){return x*x;}
template<class T>inline void upmin(T &t,T tmp){if(t>tmp)t=tmp;}
template<class T>inline void upmax(T &t,T tmp){if(t<tmp)t=tmp;}

const DB EPS=1e-9;
inline int sgn(DB x){if(abs(x)<EPS)return 0;return(x>0)?1:-1;}
const DB Pi=acos(-1.0);

inline int gint()
  {
        int res=0;bool neg=0;char z;
        for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
        if(z==EOF)return 0;
        if(z=='-'){neg=1;z=getchar();}
        for(;z!=EOF && isdigit(z);res=res*10+z-'0',z=getchar());
        return (neg)?-res:res; 
    }
inline LL gll()
  {
      LL res=0;bool neg=0;char z;
        for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
        if(z==EOF)return 0;
        if(z=='-'){neg=1;z=getchar();}
        for(;z!=EOF && isdigit(z);res=res*10+z-'0',z=getchar());
        return (neg)?-res:res; 
    }

const int maxN=100000;

int N,M;
int a[maxN+100];
int bak[maxN+100],cnt;
int first[maxN+100],now;
struct Tedge{int v,next;}edge[2*maxN+100];

inline void addedge(int u,int v){now++;edge[now].v=v;edge[now].next=first[u];first[u]=now;}

int head,tail,que[maxN+100];
int fa[maxN+100],dep[maxN+100];
int jump[35][maxN+100];

struct Tnode{int val,son[2];}sn[maxN*70+100000];int idx;

int root[maxN+100];

inline void update(int past,int p,int l,int r,int x)
  {
      if(x<=l && r<=x){sn[p].val=sn[past].val+1;return;}
      int mid=(l+r)/2,f=(x<=mid);
      sn[p].son[f]=sn[past].son[f];
      sn[p].son[f^1]=++idx;
      if(x<=mid) update(sn[past].son[0],sn[p].son[0],l,mid,x); else update(sn[past].son[1],sn[p].son[1],mid+1,r,x);
      sn[p].val=sn[sn[p].son[0]].val+sn[sn[p].son[1]].val;
  }
inline int ask(int r1,int r2,int r3,int r4,int l,int r,int k)
  {
      if(l==r)return bak[l];
      int mid=(l+r)/2;
      int ge=sn[sn[r1].son[0]].val+sn[sn[r2].son[0]].val-sn[sn[r3].son[0]].val-sn[sn[r4].son[0]].val;
      if(ge<k)
        return ask(sn[r1].son[1],sn[r2].son[1],sn[r3].son[1],sn[r4].son[1],mid+1,r,k-ge);
      else
        return ask(sn[r1].son[0],sn[r2].son[0],sn[r3].son[0],sn[r4].son[0],l,mid,k);
  }

inline void BFS()
  {
      int i,j;
      mmst(fa,-1);
      fa[que[head=tail=0]=1]=0;
      dep[1]=1;
      while(head<=tail)
        {
            int u=que[head++],v;
            for(i=first[u],v=edge[i].v;i!=-1;i=edge[i].next,v=edge[i].v)if(fa[v]==-1){fa[que[++tail]=v]=u;dep[v]=dep[u]+1;}
        }
      re(i,0,N)root[i]=++idx;
      re(i,0,tail)
          {
              int u=que[i];
                update(root[fa[u]],root[u],1,cnt,a[u]);
            }
      re(j,0,30)jump[j][1]=1;
      re(i,1,tail)
        {
            int u=que[i];
            jump[0][u]=fa[u];
            re(j,1,30)jump[j][u]=jump[j-1][jump[j-1][u]];
        }
  }

inline void swim(int &x,int H){int i;for(i=0;H!=0;H/=2,i++)if(H&1)x=jump[i][x];}
inline int ask_lca(int x,int y)
  {
      if(dep[x]<dep[y])swap(x,y);
      swim(x,dep[x]-dep[y]);
      if(x==y)return x;
      int i;
      red(i,30,0)if(jump[i][x]!=jump[i][y]){x=jump[i][x];y=jump[i][y];}
      return jump[0][x];
  }

int main()
  {
      freopen("bzoj2588.in","r",stdin);
      freopen("bzoj2588.out","w",stdout);
      int i;
      N=gint();M=gint();
      re(i,1,N)a[i]=gint();
      re(i,1,N)bak[i]=a[i];
      sort(bak+1,bak+N+1);
      cnt=unique(bak+1,bak+N+1)-bak-1;
      re(i,1,N)a[i]=lower_bound(bak+1,bak+cnt+1,a[i])-bak;
      mmst(first,-1);now=-1;
      re(i,1,N-1){int x=gint(),y=gint();addedge(x,y);addedge(y,x);}
      BFS();
      int lastans=0;
      while(M--)
        {
            int x=gint()^lastans,y=gint(),lca=ask_lca(x,y),k=gint();
            lastans=ask(root[x],root[y],root[lca],root[fa[lca]],1,cnt,k);
            printf("%d",lastans);if(M!=0)printf("\n");
        }
      return 0;
  }
View Code

 

posted @ 2015-07-15 21:00  maijing  阅读(197)  评论(0编辑  收藏  举报