题目链接: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. C++ Template之函数模版

    函数模版的定义: template <typename T> T const& max(const T& a,const T b) { return a > b ? ...

  2. protobuf的安装和使用

    以下全部基于win7系统. protobuf是什么,有什么用网上说的已经很多了.这里就是说一下怎么使用.就当给自己做个笔记吧. .proto文件的语法什么的也请网上查看,挺多的. 第一步: 下载pro ...

  3. ie 与 Chrome 时间格式化问题.

    ie 与 Chrome 时间格式化通用: new Date(res[i].Time.replaceAll("-", "/")).format("yyy ...

  4. IO端口和IO内存

    为什么会有IO端口和IO内存 这主要原因是因为处理器的架构不同,这里我们使用arm来代表典型的使用IO内存架构,intel 80x86代表典型的使用IO端口架构.简单来说arm把所有寄存器(包括外部设 ...

  5. C# 模拟一个处理消息队列的线程类 Message Queue

    // 模拟一个处理消息队列的类 class MessageHandler { // 消息队列 private Queue<string> messageQue = new Queue< ...

  6. 使用 Swagger UI 与 Swashbuckle 创建 RESTful Web API 帮助文件

    作者:Sreekanth Mothukuru 2016年2月18日 本文旨在介绍如何使用常用的 Swagger 和 Swashbuckle 框架创建描述 Restful API 的交互界面,并为 AP ...

  7. Error Code: 1175 Mysql中更新或删除时报错(未带关键字条件)

    SET SQL_SAFE_UPDATES = 0; SQL_SAFE_UPDATES = {0 | 1} 如果设置为0,则MySQL会放弃在WHERE子句或LIMIT子句中不使用关键字的UPDATE或 ...

  8. HDU 4569 Special equations(数学推论)

    题目 //想不出来,看了解题报告 /* 题意:给你一个最高幂为4的一元多项式,让你求出一个x使其结果模p*p为0. 题解:f(x)%(p*p)=0那么一定有f(x)%p=0,f(x)%p=0那么一定有 ...

  9. [C++]不能被继承的类

    前面讲到,派生类的构造函数和析构函数会自动调用基类的构造函数和析构函数,那么要让一个类不能被继承,那么就将它的构造函数和析构函数私有函数(派生类可以访问保护函数).那么怎样才能得到该类的实例呢? 这倒 ...

  10. SQL技术内幕-5 比较特殊 insert into 数据的写法

    ---比较特殊,第一次看到这种写法,记录下来 create table Student --学生成绩表 ( id int, --主键 Grade int, --班级 Score int --分数 ) ...