SPOJ 1487 Query on a tree III(划分树)
题目链接: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(划分树)的更多相关文章
- 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 ...
- SP1487 PT07J - Query on a tree III (主席树)
SP1487 PT07J - Query on a tree III 题意翻译 你被给定一棵带点权的n个点的有根数,点从1到n编号. 定义查询 query(x,k): 寻找以x为根的k大点的编号(从小 ...
- 【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 ...
- SPOJ 375. Query on a tree (动态树)
375. Query on a tree Problem code: QTREE You are given a tree (an acyclic undirected connected graph ...
- Problem: Query on the tree(二分+划分树)
题目链接: Problem: Query on the tree Time limit: 1s Mem limit: 64 MB Problem Description There ...
- [ SPOJ PT07J ] Query on a tree III
\(\\\) Description 其实这题才是正版的 Qtree3...... 给定 \(n\) 个点,以 \(1\) 号节点为根的树,点有点权. \(m\) 次询问 以 \(x\) 为根的子树内 ...
- SP1487 PT07J - Query on a tree III 主席树+dfs序
Code: #include<iostream> #include<cstdio> #include<algorithm> #include<string&g ...
- BZOJ1803Spoj1487 Query on a tree III——主席树
题目大意 给一棵有点权的n个点的有根树,保证任意两点的点权不同,m次询问每次询问x的子树中权值第k大的点. 输入 先输入n,然后每个点点权,再输入n-1行每行两个数x,y代表x和y相连,再输入m,之后 ...
- bzoj 1803: Spoj1487 Query on a tree III(主席树)
题意 你被给定一棵带点权的n个点的有根数,点从1到n编号. 定义查询 query(x,k): 寻找以x为根的k大点的编号(从小到大排序第k个点) 假设没有两个相同的点权. 输入格式: 第一行为整数n, ...
随机推荐
- 【LCA】CodeForce #326 Div.2 E:Duff in the Army
C. Duff in the Army Recently Duff has been a soldier in the army. Malek is her commander. Their coun ...
- 【BZOJ】【2480】【SPOJ 3105】Mod
扩展BSGS Orz zyf……然而他的题解对AC大神的题解作了引用……而坑爹的百度云……呵呵了... 扩展BSGS模板题 /************************************* ...
- 重定向 vs output redirect
http://asawicki.info/files/visual_cpp_redirect.png http://asawicki.info/news_1496_redirecting_output ...
- oracle Array类型作为参数传入函数(存储过程) 大字符串参数解决方案
1. 创建自定义的类型.由于Oracle没有提供现成的array类型,这里用table类型来模拟. CREATE OR REPLACE TYPE varchar_array is Table OF v ...
- Linux 的多线程编程的高效开发经验
http://www.ibm.com/developerworks/cn/linux/l-cn-mthreadps/ 背景 Linux 平台上的多线程程序开发相对应其他平台(比如 Windows)的多 ...
- JavaScript之setcookie()讲解
function setcookie(name,value){ var Days = 30; var exp = new Date(); exp ...
- IE8下兼容rgba颜色的半透明背景
在工作中做一个图片半透明遮罩时发现在IE8下不兼容 一查再知道IE8不支持rgba颜色,再搜搜兼容性方法,没想到这么快就解决了. 先说说rgba的含义: r代表red,g代表green,b代表blue ...
- JAVA 异常对于性能的影响
陶炳哲 - MAY 12, 2015 在对OneAPM的客户做技术支持时,我们常常会看到很多客户根本没意识到的异常.在消除了这些异常之后,代码运行速度与以前相比大幅提升.这让我们产生一种猜测,就是在代 ...
- Android 4.4KitKat AudioRecord 流程分析
Android是架构分为三层: 底层 Linux Kernel 中间层 主要由C++实现 (Android 60%源码都是C++实现) 应用层 主要由JAVA开发的应用程序 应用程序执行 ...
- 项目中用到的SQL-总结
基本sql总结: Group by的理解:having子句,分组函数 Group by使用的限定: 1.出现在Select列表中的字段或者出现在order by后面的字段,如果不是包含在分组函数中,那 ...