题目链接:http://www.spoj.com/problems/PT07J/

题意:给出一个有根树,1为根节点,每个节点有权值。若干询问,询问以u为根的子树中权值第K小的节点编号。

思路:DFS一次,记录每个节点在DFS序列中的开始和结束位置。那么以u为节点的子树的所有点都在两个u之间。那么询问就转化成询问区间的第K小值,可以将DFS序列建立划分树解决。

#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <map>

#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))
#define abs(x) ((x)>=0?(x):-(x))
#define i64 long long
#define u32 unsigned int
#define u64 unsigned long long
#define clr(x,y) memset(x,y,sizeof(x))
#define CLR(x) x.clear()
#define ph(x) push(x)
#define pb(x) push_back(x)
#define Len(x) x.length()
#define SZ(x) x.size()
#define PI acos(-1.0)
#define sqr(x) ((x)*(x))
#define MP(x,y) make_pair(x,y)
#define EPS 1e-10

#define FOR0(i,x) for(i=0;i<x;i++)
#define FOR1(i,x) for(i=1;i<=x;i++)
#define FOR(i,a,b) for(i=a;i<=b;i++)
#define FORL0(i,a) for(i=a;i>=0;i--)
#define FORL1(i,a) for(i=a;i>=1;i--)
#define FORL(i,a,b)for(i=a;i>=b;i--)

#define rush() int CC;for(scanf("%d",&CC);CC--;)
#define Rush(n)  while(scanf("%d",&n)!=-1)
using namespace std;

void RD(int &x){scanf("%d",&x);}
void RD(i64 &x){scanf("%lld",&x);}
void RD(u64 &x){scanf("%I64u",&x);}
void RD(u32 &x){scanf("%u",&x);}
void RD(double &x){scanf("%lf",&x);}
void RD(int &x,int &y){scanf("%d%d",&x,&y);}
void RD(i64 &x,i64 &y){scanf("%lld%lld",&x,&y);}
void RD(u32 &x,u32 &y){scanf("%u%u",&x,&y);}
void RD(double &x,double &y){scanf("%lf%lf",&x,&y);}
void RD(int &x,int &y,int &z){scanf("%d%d%d",&x,&y,&z);}
void RD(i64 &x,i64 &y,i64 &z){scanf("%lld%lld%lld",&x,&y,&z);}
void RD(u32 &x,u32 &y,u32 &z){scanf("%u%u%u",&x,&y,&z);}
void RD(double &x,double &y,double &z){scanf("%lf%lf%lf",&x,&y,&z);}
void RD(char &x){x=getchar();}
void RD(char *s){scanf("%s",s);}
void RD(string &s){cin>>s;}

void PR(int x) {printf("%d\n",x);}
void PR(int x,int y) {printf("%d %d\n",x,y);}
void PR(i64 x) {printf("%lld\n",x);}
void PR(u32 x) {printf("%u\n",x);}
void PR(u64 x) {printf("%llu\n",x);}
void PR(double x) {printf("%.2lf\n",x);}
void PR(char x) {printf("%c\n",x);}
void PR(char *x) {printf("%s\n",x);}
void PR(string x) {cout<<x<<endl;}

const int mod=10007;
const i64 inf=((i64)1)<<60;
const double dinf=1000000000000000000.0;
const int INF=2147483647;
const int N=500005;

struct Node
{
    int v,next;
};

Node edges[N];
int head[N],e;
int key[N];
int n,m;

void Add(int u,int v)
{
    edges[e].v=v;
    edges[e].next=head[u];
    head[u]=e++;
}

struct node
{
    int L,R;
};

node a[N<<2];
int s[N],t[35][N],tot[35][N];
int cnt;
int pos[N][2];

void dfs(int u,int pre)
{
    s[++cnt]=key[u]; pos[u][0]=cnt;
    int i,v;
    for(i=head[u];i!=-1;i=edges[i].next)
    {
        v=edges[i].v;
        if(v==pre) continue;
        dfs(v,u);
    }
    pos[u][1]=cnt;
}

void build(int dep,int u,int L,int R)
{
    a[u].L=L;
    a[u].R=R;
    if(L==R) return;
    int mid=(L+R)>>1;
    int sameNum=mid-L+1,i;
    for(i=L;i<=R;i++) if(t[dep][i]<s[mid]) sameNum--;
    int LL=L,LR=mid,RL=mid+1,RR=R;
    int Lnum=0,Rnum=0;
    int x;
    for(i=L;i<=R;i++)
    {
        if(i==L) tot[dep][i]=0;
        else tot[dep][i]=tot[dep][i-1];
        x=t[dep][i];
        if(x<s[mid])
        {
            tot[dep][i]++;
            t[dep+1][LL+Lnum]=x;
            Lnum++;
        }
        else if(x>s[mid])
        {
            t[dep+1][RL+Rnum]=x;
            Rnum++;
        }
        else
        {
            if(sameNum>0)
            {
                sameNum--;
                tot[dep][i]++;
                t[dep+1][LL+Lnum]=x;
                Lnum++;
            }
            else
            {
                t[dep+1][RL+Rnum]=x;
                Rnum++;
            }
        }
    }
    build(dep+1,u*2,LL,LR);
    build(dep+1,u*2+1,RL,RR);
}

int query(int dep,int u,int L,int R,int K)
{
    if(L==R) return t[dep][L];
    int x,y,xx,yy,l,r;
    int mid=(a[u].L+a[u].R)>>1;
    if(L==a[u].L) x=0;
    else x=tot[dep][L-1];
    y=tot[dep][R]-x;
    if(K<=y)
    {
        l=a[u].L+x;
        r=a[u].L+x+y-1;
        return query(dep+1,u*2,l,r,K);
    }
    else
    {
        xx=L-a[u].L-x;
        yy=R-L+1-y;
        l=mid+1+xx;
        r=mid+1+xx+yy-1;
        return query(dep+1,u*2+1,l,r,K-y);
    }
}

map<int,int> mp;

int main()
{
    RD(n);
    int i,j;
    FOR1(i,n) RD(key[i]),mp[key[i]]=i;
    clr(head,-1); e=0;
    int x,y;
    FOR1(i,n-1)
    {
        RD(x,y); Add(x,y); Add(y,x);
    }
    dfs(1,-1);
    FOR1(i,cnt) t[1][i]=s[i];
    sort(s+1,s+cnt+1);
    build(1,1,1,cnt);
    RD(m);
    int ans;
    while(m--)
    {
        RD(x,y);
        ans=query(1,1,pos[x][0],pos[x][1],y);
        PR(mp[ans]);
    }
}

SPOJ 1487 Query on a tree III(划分树)的更多相关文章

  1. SPOJ PT07J - Query on a tree III(划分树)

    PT07J - Query on a tree III #tree You are given a node-labeled rooted tree with n nodes. Define the ...

  2. SP1487 PT07J - Query on a tree III (主席树)

    SP1487 PT07J - Query on a tree III 题意翻译 你被给定一棵带点权的n个点的有根数,点从1到n编号. 定义查询 query(x,k): 寻找以x为根的k大点的编号(从小 ...

  3. 【BZOJ1803】Spoj1487 Query on a tree III 主席树+DFS序

    [BZOJ1803]Spoj1487 Query on a tree III Description You are given a node-labeled rooted tree with n n ...

  4. SPOJ 375. Query on a tree (动态树)

    375. Query on a tree Problem code: QTREE You are given a tree (an acyclic undirected connected graph ...

  5. Problem: Query on the tree(二分+划分树)

    题目链接: Problem: Query on the tree Time limit: 1s     Mem limit: 64 MB      Problem Description There ...

  6. [ SPOJ PT07J ] Query on a tree III

    \(\\\) Description 其实这题才是正版的 Qtree3...... 给定 \(n\) 个点,以 \(1\) 号节点为根的树,点有点权. \(m\) 次询问 以 \(x\) 为根的子树内 ...

  7. SP1487 PT07J - Query on a tree III 主席树+dfs序

    Code: #include<iostream> #include<cstdio> #include<algorithm> #include<string&g ...

  8. BZOJ1803Spoj1487 Query on a tree III——主席树

    题目大意 给一棵有点权的n个点的有根树,保证任意两点的点权不同,m次询问每次询问x的子树中权值第k大的点. 输入 先输入n,然后每个点点权,再输入n-1行每行两个数x,y代表x和y相连,再输入m,之后 ...

  9. bzoj 1803: Spoj1487 Query on a tree III(主席树)

    题意 你被给定一棵带点权的n个点的有根数,点从1到n编号. 定义查询 query(x,k): 寻找以x为根的k大点的编号(从小到大排序第k个点) 假设没有两个相同的点权. 输入格式: 第一行为整数n, ...

随机推荐

  1. 【BZOJ】【1177】【APIO2009】Oil

    DP 找出三个正方形,可以转化为将整个油田切成三个矩形块,每块中各找一个正方形区域,切的形式只有6种,分类更新ans即可 题解:http://trinklee.blog.163.com/blog/st ...

  2. UIResponder类

    UIResponder类 UIResponder类是所有视图类的父类,包括UIView, UIApplication, UIWindow. UIResponder类定义了一些响应和处理事件的方法.事件 ...

  3. WARNING: Calls to any function that may require a gradient calculation inside a conditional block may return undefined results

    GLES2.0: Some device will give a warning on compling shaders(yet the compling will succeed), and the ...

  4. int型整数的数值范围

    假设int型用两个字节表示对于有符号的整数,用补码表示的话,最高位是符号位,后面15位用来表示数据.1.正数,表示的范围为0000 0000 0000 0001-0111 1111 1111 1111 ...

  5. VMware下Ubuntu与宿主Windows共享文件夹

    概述1.安装VMware Tool2.设置共享 步骤开始安装VMware Tool 显示如下画面(如果宿主无法访问外网,可能会出现一个更新失败,可以无视之) 通过下列命令解压.执行,分别是下面的tar ...

  6. 取得DisplayMerics手机屏幕大小的应用

    DisplayMerics:A structure describing general information about a display, such as its size, density, ...

  7. Lock wait timeout exceeded; try restarting transaction

    What gives this away is the word transaction. It is evident by the statement that the query was atte ...

  8. Log4net学习

    转自:http://www.cnblogs.com/sirkevin/archive/2012/06/13/2548449.html Log4net简介根据日志类别保存到不同的文件,并按照日期生成不同 ...

  9. SVN的使用(转载)

    MyEclipse中的SVN操作手册   导入项目 点击工具栏中的File-Import,进入下图: 点击Nex进入下图: 点击Next进入下图,输入你SVN服务器的IP地址,包括端口号和文件夹等完整 ...

  10. 内存分析_.Net内存原理介绍

    内存原理介绍 1.       .Net应用程序中的内存 1.1.Net内存类型 Windows使用一个系统:虚拟寻址系统.这个系统的作用是将程序可用的内存地址映射到硬件内存中的实际地址上.其实际结果 ...