poj 2104 K-th Number(函数式线段树)
来源:互联网 发布:python字体设置 编辑:程序博客网 时间:2024/05/21 17:48
若要转载请注明http://blog.csdn.net/q775968375 尊重别人的成果是对自己人格的负责——by 就叫我厌世好不好
K-th Number
Time Limit: 20000MS Memory Limit: 65536KTotal Submissions: 31500 Accepted: 9704Case 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.
Source
Northeastern Europe 2004, Northern Subregion
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define N 100010#define mid(i,j) ((i+j)>>1)struct node{int l,r,cnt;void init(int ll,int rr,int s){l=ll;r=rr;cnt=s;}}T[N*20];int a[N],b[N],r[N],n,m,cnt;void build(int l,int r,int &k){k==cnt++;T[k].init(0,0,0);if(l==r)return;int mid=mid(l,r);build(l,mid,T[k].l);build(mid+1,r,T[k].r);}void update(int p,int x,int l,int r,int &k){k=cnt++;T[k].init(T[p].l,T[p].r,T[p].cnt+1);if(l==r)return;int mid=mid(l,r);if(mid>=x)update(T[p].l,x,l,mid,T[k].l);//住左else update(T[p].r,x,mid+1,r,T[k].r);//往右}int query(int i,int j,int l,int r,int k){//[i,j]中第k大if(l==r)return r;int mid=mid(l,r);int t=T[T[j].l].cnt-T[T[i].l].cnt;if(t>=k)return query(T[i].l,T[j].l,l,mid,k);else return query(T[i].r,T[j].r,mid+1,r,k-t);}int main(){int i,j,k;while(scanf("%d%d",&n,&m)!=EOF){cnt=1;for(i=1;i<=n;i++){scanf("%d",&a[i]);b[i]=a[i];}sort(b+1,b+n+1);//从小到大排int num=unique(b+1,b+n+1)-b-1;//除相邻重复元素后的元素个数build(1,num,r[0]); for(i=1;i<=n;i++){a[i]=lower_bound(b+1,b+num+1,a[i])-b;//返回第一个大于a[i]的位置update(r[i-1],a[i],1,num,r[i]);}while(m--){scanf("%d%d%d",&i,&j,&k);printf("%d\n",b[query(r[i-1],r[j],1,num,k)]);}}return 0;}
- poj 2104 K-th Number 函数式线段树
- poj 2104 K-th Number(函数式线段树)
- POJ-2104-K-th Number(函数式线段树)
- poj-2104 K-th Number[主席树/函数式线段树/可持久化线段树]
- POJ 2104 K-th Number 线段树
- POJ 2104 K-th Number (线段树)
- [POJ 2104]K-th Number (不带修改的区间k大,函数式线段树)
- POJ 2104 K-th number 主席树 函数式线段树
- POJ 2104 K-th Number (可持久化线段树)
- poj 2104 K-th Number(划分线段树)
- poj 2104 K-th Number(线段树)
- [poj]-2104-K-th Number-可持久化线段树
- K-th Number (poj 2104 线段树+二分)
- poj 2104 K-th Number 可持久化线段树
- POJ 2104 K-th Number(可持久化线段树)
- POJ 2104 K-th Number 可持久化线段树
- poj2104 K-th Number (函数式线段树)
- POJ-2104 K-th Number(线段树[归并树]-区间第k大)
- hdu 4552
- What Is Clean Code? – In Quotes
- luaplus编译
- Oracle Hints详解
- coco2d-x使用备忘录
- poj 2104 K-th Number(函数式线段树)
- stc单片机ad转换程序心得
- Linux中查看进程
- 1. 给一个有N个整数的数组S..和另一个整数X,判断S里有没有2个数的和为X,请设计成O(n*log2(n))的算法。
- 学习心得
- Spring @Transactional声明式事务管理 getCurrentSession
- opencv-Harris 角点检测
- xmu 1328.不高兴的bobo
- ZOJ——5013