Pku oj 2104(划分树)
来源:互联网 发布:配眼镜需要的数据 编辑:程序博客网 时间:2024/06/03 18:09
K-th Number
Time Limit: 20000MS Memory Limit: 65536KTotal Submissions: 48842 Accepted: 16489Case Time Limit: 2000MS
Description
You are working for Macrohard company in data structures department. After failing your previous task about key insertion you were asked to write a new data structure that would be able to return quickly k-th order statistics in the array segment.
That is, given an array a[1...n] of different integer numbers, your program must answer a series of questions Q(i, j, k) in the form: "What would be the k-th number in a[i...j] segment, if this segment was sorted?"
For example, consider the array a = (1, 5, 2, 6, 3, 7, 4). Let the question be Q(2, 5, 3). The segment a[2...5] is (5, 2, 6, 3). If we sort this segment, we get (2, 3, 5, 6), the third number is 5, and therefore the answer to the question is 5.
That is, given an array a[1...n] of different integer numbers, your program must answer a series of questions Q(i, j, k) in the form: "What would be the k-th number in a[i...j] segment, if this segment was sorted?"
For example, consider the array a = (1, 5, 2, 6, 3, 7, 4). Let the question be Q(2, 5, 3). The segment a[2...5] is (5, 2, 6, 3). If we sort this segment, we get (2, 3, 5, 6), the third number is 5, and therefore the answer to the question is 5.
Input
The first line of the input file contains n --- the size of the array, and m --- the number of questions to answer (1 <= n <= 100 000, 1 <= m <= 5 000).
The second line contains n different integer numbers not exceeding 109 by their absolute values --- the array for which the answers should be given.
The following m lines contain question descriptions, each description consists of three numbers: i, j, and k (1 <= i <= j <= n, 1 <= k <= j - i + 1) and represents the question Q(i, j, k).
The second line contains n different integer numbers not exceeding 109 by their absolute values --- the array for which the answers should be given.
The following m lines contain question descriptions, each description consists of three numbers: i, j, and k (1 <= i <= j <= n, 1 <= k <= j - i + 1) and represents the question Q(i, j, k).
Output
For each question output the answer to it --- the k-th number in sorted a[i...j] segment.
Sample Input
7 31 5 2 6 3 7 42 5 34 4 11 7 3
Sample Output
563
Hint
This problem has huge input,so please use c-style input(scanf,printf),or you may got time limit exceed.
划分树模板题,区间第K大
#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#include <cstdlib>#include <cmath>#define lson(rt) rt<<1#define rson(rt) rt<<1|1#define cl(a) memset(a,0,sizeof(a))#define ss(a) scanf("%d",&a)using namespace std;const int N=100100;struct seg_tree{ int left; int right; int midd() { return (left+right)>>1; }}tree[N<<3];int val[20][N],toleft[20][N],a[N];void build(int l,int r,int d,int rt){ int i; tree[rt].left=l; tree[rt].right=r; if (l==r) return; int mid=tree[rt].midd(); int lsame=mid-l+1,same=0; for (i=l;i<=r;i++) if (val[d][i]<a[mid]) lsame--; int lpos=l,rpos=mid+1; for (i=l;i<=r;i++) { int x=val[d][i]; if (i==l) toleft[d][i]=0; else toleft[d][i]=toleft[d][i-1]; if (val[d][i]<a[mid]) { toleft[d][i]++; val[d+1][lpos++]=x; } else if (val[d][i]>a[mid]) { val[d+1][rpos++]=x; } else if (same<lsame) { same++; toleft[d][i]++; val[d+1][lpos++]=x; } else val[d+1][rpos++]=x; } build(l,mid,d+1,lson(rt)); build(mid+1,r,d+1,rson(rt));}int query(int k,int l,int r,int d,int rt){ if (l==r) return val[d][l]; int s,ss,mid,le,newl,newr; mid=tree[rt].midd(); le=tree[rt].left; if (l==le) { ss=0; s=toleft[d][r]; } else { ss=toleft[d][l-1]; s=toleft[d][r]-toleft[d][l-1]; } if (s>=k) { newl=le+ss; newr=le+ss+s-1; return query(k,newl,newr,d+1,lson(rt)); } else { int bb=l-le-ss; int b=r-l+1-s; newl=mid+bb+1; newr=mid+bb+b; return query(k-s,newl,newr,d+1,rson(rt)); }}int main(){ int n,m,i; while (~scanf("%d%d",&n,&m)) { cl(a);cl(val);cl(toleft); for (i=1;i<=n;i++) { scanf("%d",&a[i]); val[0][i]=a[i]; } sort(a,a+n+1); build(1,n,0,1); for (i=1;i<=m;i++) { int le,ri,k; scanf("%d%d%d",&le,&ri,&k); int res=query(k,le,ri,0,1); printf("%d\n",res); } } return 0;}
0 0
- Pku oj 2104(划分树)
- pku 2104划分树
- pku 3140 Contestants Division(树的划分)
- Pku oj 1573 Robot Motion(模拟)
- Pku oj 2159 Ancient Cipher(字符串)
- Pku oj 2027 No Brainer(水题)
- Pku oj 3461 Oulipo(kmp)
- Pku oj 1528 Agri-Net(MST)
- Pku oj 2485 Highways(MST)
- Pku oj 3264 Balanced Lineup(RMQ)
- Pku oj 3071 Football(概率dp)
- Pku oj 1003 Hangover(水题)
- Pku oj 1004 Financial Management(水题)
- 哈理工OJ 2004 整数划分(整数划分问题)
- 南阳 oj 176 整数划分(二)
- 南阳理工oj 整数划分(DP)
- Pku oj 3468 A Simple Problem with Integers(线段树Lazy标记)
- pku oj 1001 Exponentiation
- 提取assets.car中的图片 .car文件的解压
- 【安卓开发】JAVA代码设置selector不同状态下的背景颜色
- navicat for mysql注册码,各种版本都有
- poj1745 Divisibility(动态规划经典题)
- 一步步学会使用SeaJS 2.0
- Pku oj 2104(划分树)
- hdu5213 莫队算法
- [Paper Reading] DeepLab v1 & v2
- PCA的数学原理
- 欢迎使用CSDN-markdown编辑器
- 【Android】各个透明度的取值
- C++面试之__C++4种转换类型
- Tomcat性能调优
- INTO TABLE与APPENDING TABLE的区别