题目传送门

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 3
1 5 2 6 3 7 4
2 5 3
4 4 1
1 7 3

Sample Output

5
6
3

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

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

POJ2104 K-th Number [整体二分]的更多相关文章

  1. POJ2104 K-th Number(整体二分)

    题解 又一次做这个题上一次用的是线段树上二分.这次用的是整体二分.结果: (第一个是整体二分) 整体二分就是对于所有查询都二分一个值.然后根据能不能成立把询问修改分成两部分,然后第二部分继承第一部分的 ...

  2. BZOJ 3110: [Zjoi2013]K大数查询 [整体二分]

    有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c如果是2 a b c形式,表示询问从第a个位置到第b个位置,第C大的数是多少. N ...

  3. BZOJ3110:[ZJOI2013]K大数查询(整体二分)

    Description 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c.如果是2 a b c形式,表示询问从第a个位置到第b个位 ...

  4. BZOJ 3110 K大数查询 | 整体二分

    BZOJ 3110 K大数查询 题面 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c 如果是2 a b c形式,表示询问从第a个 ...

  5. BZOJ.3110.[ZJOI2013]K大数查询(整体二分 树状数组/线段树)

    题目链接 BZOJ 洛谷 整体二分求的是第K小(利用树状数组).求第K大可以转为求第\(n-K+1\)小,但是这样好像得求一个\(n\). 注意到所有数的绝对值\(\leq N\),将所有数的大小关系 ...

  6. 【BZOJ-3110】K大数查询 整体二分 + 线段树

    3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 6265  Solved: 2060[Submit][Sta ...

  7. 静态区间第K小(整体二分、主席树)

    题目链接 题解 主席树入门题 但是这里给出整体二分解法 整体二分顾名思义是把所有操作放在一起二分 想想,如果求\([1-n]\)的第\(k\)小怎么二分求得? 我们可以二分答案\(k\), \(O(n ...

  8. ZOJ 1112 Dynamic Rankings【动态区间第K大,整体二分】

    题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1112 题意: 求动态区间第K大. 分析: 把修改操作看成删除与增加 ...

  9. [ZJOI2013]K大数查询——整体二分

    题目描述 有N个位置,M个操作.操作有两种,每次操作如果是: 1 a b c:表示在第a个位置到第b个位置,每个位置加上一个数c 2 a b c:表示询问从第a个位置到第b个位置,第C大的数是多少. ...

随机推荐

  1. 经典幻灯片插件Swiper

    照着写的demo,搞清楚什么叫分页器Pagination,什么叫nav,搞清楚DOM结构,container,wrapper之类的,就能写了.效果掉渣天! <!DOCTYPE html> ...

  2. 数据库日志文件(databasename_log.ldf)太大 如何清除

    在SQL2008中清除日志就必须在简单模式下进行,等清除动作完毕再调回到完全模式.方案一:完全命令模式USE[master] GO ALTER DATABASE DNName SET RECOVERY ...

  3. Asp.net 中,在服务端向客户端写脚本的常用方法

    在Asp.net 服务端处理脚本,一般都用 ClientScriptManager ,即web窗体服务端的this.ClientScript.该对象比较常用的方法: 1.RegisterArrayDe ...

  4. linux查询进程 kill进程

    查询进程 #ps aux #查看全部进程 #ps aux|grep firewall #查询与firewall相关的进程 kill进程一 kill进程pid为711进程: #pkill -9 711 ...

  5. CodeForces 816B 前缀和

    To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee aficionado, want ...

  6. python初步学习-python 模块之 sys(持续补充)

    sys sys 模块包括了一组非常实用的服务,内含很多函数方法和变量 sys 模块重要函数变量 sys.stdin 标准输出流 sys.stdout 标准输出流 sys.stderr 标准错误流 sy ...

  7. host映射方法

    host映射: host是根据TCP/IP for Windows 的标准来工作的,它的作用是包含IP地址和Host name(主机名)的映射关系,是一个映射IP地址和Host name(主机名)的规 ...

  8. v4l2API无法执行VIDIOC_DQBUF的问题

    一.PC虚拟机下ubuntu14.04.1环境下 源文件:show.c  USB摄像头格式yuyv: 申请缓冲帧数2,分辨率640*480  阻塞无法执行 申请缓冲帧数4,分辨率640*480  阻塞 ...

  9. Dream------Java--ant zip 对压缩文件进行指定位置的修改

    ant zip 对压缩文件进行指定位置的修改 实现功能: 对2中文件进行修改: 需求: 在XX文件中,从二进制流的200字节位置开始,往后的30位字节数量.插入一个值 由于涉及到公司内部,不方便写太多 ...

  10. CentOS时区GMT修改为CST

    GMT:格林尼标准时间 北京时间=GMT时间+8小时 [root@sa~]# date -R 查看目前服务器的时间标准 [root@sa~]# vi /etc/sysconfig/clock 将ZON ...