POJ2104 K-th Number [整体二分]
| Time Limit: 20000MS | Memory Limit: 65536K | |
| Total Submissions: 69053 | Accepted: 24471 | |
| Case Time Limit: 2000MS | ||
Description
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 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
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
Source
分析:
//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 [整体二分]的更多相关文章
- POJ2104 K-th Number(整体二分)
题解 又一次做这个题上一次用的是线段树上二分.这次用的是整体二分.结果: (第一个是整体二分) 整体二分就是对于所有查询都二分一个值.然后根据能不能成立把询问修改分成两部分,然后第二部分继承第一部分的 ...
- BZOJ 3110: [Zjoi2013]K大数查询 [整体二分]
有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c如果是2 a b c形式,表示询问从第a个位置到第b个位置,第C大的数是多少. N ...
- BZOJ3110:[ZJOI2013]K大数查询(整体二分)
Description 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c.如果是2 a b c形式,表示询问从第a个位置到第b个位 ...
- BZOJ 3110 K大数查询 | 整体二分
BZOJ 3110 K大数查询 题面 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c 如果是2 a b c形式,表示询问从第a个 ...
- BZOJ.3110.[ZJOI2013]K大数查询(整体二分 树状数组/线段树)
题目链接 BZOJ 洛谷 整体二分求的是第K小(利用树状数组).求第K大可以转为求第\(n-K+1\)小,但是这样好像得求一个\(n\). 注意到所有数的绝对值\(\leq N\),将所有数的大小关系 ...
- 【BZOJ-3110】K大数查询 整体二分 + 线段树
3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 6265 Solved: 2060[Submit][Sta ...
- 静态区间第K小(整体二分、主席树)
题目链接 题解 主席树入门题 但是这里给出整体二分解法 整体二分顾名思义是把所有操作放在一起二分 想想,如果求\([1-n]\)的第\(k\)小怎么二分求得? 我们可以二分答案\(k\), \(O(n ...
- ZOJ 1112 Dynamic Rankings【动态区间第K大,整体二分】
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1112 题意: 求动态区间第K大. 分析: 把修改操作看成删除与增加 ...
- [ZJOI2013]K大数查询——整体二分
题目描述 有N个位置,M个操作.操作有两种,每次操作如果是: 1 a b c:表示在第a个位置到第b个位置,每个位置加上一个数c 2 a b c:表示询问从第a个位置到第b个位置,第C大的数是多少. ...
随机推荐
- lvm--pv丢失后恢复
[root@db-backup ~]# vgcfgrestore vg_backup Couldn't find device with uuid JgYDQu-R1AG-wrD2-AHpX-A14 ...
- JavaScript 生成n位随机数
function RndNum(n){ var rnd=""; for(var i=0;i<n;i++) rnd+=Math.floor(Math.random()*10); ...
- 《java语言程序设计》初步学习——各种小Demo
发现现在的天下几乎都是java的天下啊,虽然我个人对java没什么好感,但是迫于生活压力,还是学一下吧,我关注的应该主要还是web方面,所以应该学的是 java server page(JSP),所以 ...
- golang sql.DB
数据库 sql.DB连接池需知: sql.DB内置连接池,连接不足时会自动创建新连接,新创建的连接使用sql.Open()时传入的dsn来构造. sql.DBClose时只会关闭连接池中的连接,未归还 ...
- 钉钉头像大小设置 阿里cdn尺寸截取参数设置
默认api的接口返回的avatar字段,是原始图片大小字段,尺寸和空间都是原始大小,如果想节省流量或统一尺寸,可以用阿里cdn自带的尺寸截取功能, 比如钉钉头像 avatar字段 返回值为原始大小ht ...
- 2016.5.18——Excel Sheet Column Number
Excel Sheet Column Number 本题收获: 1.对于字符串中字母转为ASIIC码:string s ;res = s[i]-'A'; 这个res就是数字s[i]-'A'是对ASII ...
- 配置子目录Web.config使其消除继承,iis7.0设置路由
iis7.0设置路由 ,url转向,伪静态 <system.webServer> <modules runAllManagedModulesForAllRequests=& ...
- haproxy代理https配置方法【转】
记得在之前的一篇文章中介绍了nginx反向代理https的方法,今天这里介绍下haproxy代理https的方法: haproxy代理https有两种方式:1)haproxy服务器本身提供ssl证书, ...
- 【bzoj3065】: 带插入区间K小值 详解——替罪羊套函数式线段树
不得不说,做过最爽的树套树———— 由于有了区间操作,我们很容易把区间看成一棵平衡树,对他进行插入,那么外面一层就是平衡树了,这就与我们之前所见到的不同了.我们之前所见到的大多数是线段树套平衡树而此题 ...
- vue实现结算淘宝购物车效果
实现单选时的价格,全选时价格 单选效果图 全选效果图 html <template> <!-- 淘宝结算购物车 --> <div class="settleme ...