博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ2104 K-th Number [整体二分]
阅读量:5173 次
发布时间:2019-06-13

本文共 3768 字,大约阅读时间需要 12 分钟。

  

K-th Number
Time Limit: 20000MS   Memory Limit: 65536K
Total Submissions: 69053   Accepted: 24471
Case 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.

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).

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

, Northern Subregion

  分析:  
  没错,这是主席树的模板,但是这里我们也可以用整体二分来做(shui)。
  关于整体二分,这个博主也才刚学,知识点什么的也不多讲,只放模板代码,关于知识点可以参考一下这个的博客。
  Code:
//It is made by HolseLee on 5th Oct 2018//POJ2104#include
#include
#include
#include
#define inf 0x3f3f3f3fusing namespace std;const int N=2e5+7;int n,m,ans[N],cnt,c[N];struct Node { int x,y,k,pos,type; Node() {} Node(const int _x,const int _y,const int _k,const int _p,const int _t): x(_x), y(_y), k(_k), pos(_p), type(_t) {}}q[N],q1[N],q2[N];inline int read(){ char ch=getchar(); int num=0; bool flag=false; while( ch<'0' || ch>'9' ) { if( ch=='-' ) flag=true; ch=getchar(); } while( ch>='0' && ch<='9' ) { num=num*10+ch-'0'; ch=getchar(); } return flag ? -num : num;}inline int lowbit(int x){ return x&(-x);}inline void add(int pos,int x){ for(; pos<=n; pos+=lowbit(pos)) c[pos]+=x;}inline int quary(int pos){ int ret=0; for(; pos>0; pos-=lowbit(pos)) ret+=c[pos]; return ret;}void solve(int l,int r,int L,int R){ if( l>r || L>R ) return; if( l==r ) { for(int i=L; i<=R; ++i) if( q[i].type ) ans[q[i].pos]=l; return ; } int mid=(l+r)>>1, cnt1=0, cnt2=0; for(int i=L; i<=R; ++i) if( q[i].type ) { int tmp=quary(q[i].y)-quary(q[i].x-1); if( tmp>=q[i].k ) q1[++cnt1]=q[i]; else q[i].k-=tmp, q2[++cnt2]=q[i]; } else { if( q[i].x<=mid ) q1[++cnt1]=q[i], add(q[i].pos,1); else q2[++cnt2]=q[i]; } for(int i=1; i<=cnt1; ++i) if(!q1[i].type) add(q1[i].pos,-1); for(int i=1; i<=cnt1; ++i) q[L+i-1]=q1[i]; for(int i=1; i<=cnt2; ++i) q[L+cnt1+i-1]=q2[i]; solve(l,mid,L,L+cnt1-1), solve(mid+1,r,L+cnt1,R);}int main(){ n=read(), m=read(); int x,y,z; for(int i=1; i<=n; ++i) x=read(), q[++cnt]=Node(x,1,0,i,0); for(int i=1; i<=m; ++i) { x=read(), y=read(), z=read(); q[++cnt]=Node(x,y,z,i,1); } solve(-inf,inf,1,cnt); for(int i=1; i<=m; ++i) printf("%d\n",ans[i]); return 0;}

 

转载于:https://www.cnblogs.com/cytus/p/9744152.html

你可能感兴趣的文章
ilspy 点击根节点后进行解析的方法
查看>>
promise原理及使用方法
查看>>
MVC实例应用模式
查看>>
C++Primer学习笔记《三》
查看>>
HTML 速查列表
查看>>
英语音标发音技巧难点—英语培训学习资料_伯明汉吧_百度贴吧
查看>>
第二次冲刺第三天
查看>>
详解通信 C# Socket之问题
查看>>
基于linux操作系统下s5pv210板子的按键中断实验
查看>>
构建高性能ASP.NET站点 网站优化需要考虑的方面
查看>>
百科知识 .e,.ec文件如何打开
查看>>
【转】以太网帧、IP报文格式
查看>>
【java】控制台实现贪吃蛇小游戏-LinkedList、Scanner
查看>>
iOS第4天数组排序
查看>>
shell监控自动备份是否成功(判断文件是否存在)
查看>>
接口的理解
查看>>
JavaScript快速入门
查看>>
python---socket与socketserver
查看>>
第五周翻译
查看>>
asp.net response.redirect 打开新窗口(转载)
查看>>