题目传送门

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. 【BZOJ】4559: [JLoi2016]成绩比较 计数DP+排列组合+拉格朗日插值

    [题意]n位同学(其中一位是B神),m门必修课,每门必修课的分数是[1,Ui].B神碾压了k位同学(所有课分数<=B神),且第x门课有rx-1位同学的分数高于B神,求满足条件的分数情况数.当有一 ...

  2. 什么是EOF -- 转

    转载地址:http://www.ruanyifeng.com/blog/2011/11/eof.html 我学习C语言的时候,遇到的一个问题就是EOF. 它是end of file的缩写,表示&quo ...

  3. 基本控件文档-UIScrollView

    CHENYILONG                                      基本控件文档-UIScrollView Fullscreen   基本使用作用UIScrollView可 ...

  4. 添加 MySql 服务、Tomcat服务到windows服务中

    添加 MySql 服务到windows服务中: cmd --> F:\MySql\MySqlServer5.1\bin\mysqld --install 这样用默认的 MySQL 为名称添加一个 ...

  5. VUE项目用hbuilder 打包为手机APP

    一.测试项目是否可以正确运行    指令:npm run dev 首先我们先建立一个vue的项目,本人用的是vue-cli随便建立的,然后运行项目 不必非得是像我这样的,这一步的目的只是测试一下咱们的 ...

  6. Sicily 1211. 商人的宣传

    题目链接:http://soj.me/1211 Description Bruce是K国的商人,他在A州成立了自己的公司,这次他的公司生产出了一批性能很好的产品,准备宣传活动开始后的第L天到达B州进行 ...

  7. elasticsearch6.5集群环境搭建的一些坑

    都说el配置很简单,确实比solr简单多了,不用手动配置一大堆,不过第一次配置也不轻松,因为马虎老是漏掉了许多地方 配置一个半小时才启动成功: 这里主要记录一下一些遇到的坑: 一 不能用root启动, ...

  8. #ifdef __cplusplus extern "C" { #endif”的定义的含义

    看一些程序的时候老是有“#ifdef __cplusplusextern "C" {#endif”的定义,搞搞清楚是怎么回事: Microsoft-Specific Predefi ...

  9. Linux修改主机名【转】

    一.永久修改修改/etc/sysconfig/network,在里面指定主机名称HOSTNAME=然后执行命令hostname 主机名这个时候可以注销一下系统,再重登录之后就行了. 或者修改/etc/ ...

  10. 一步一步搭建 oracle 11gR2 rac + dg 之前传 (一)【转】

    一步一步在RHEL6.5+VMware Workstation 10上搭建 oracle 11gR2 rac + dg  之前传 (一) 转自 一步一步搭建 oracle 11gR2 rac + dg ...