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大的数是多少. ...
随机推荐
- 八卦Minsky打压神经网络始末
八卦Minsky打压神经网络始末 谈下Minsky造成的神经网络冰河事件:57年一个叫弗兰克的大概只有二流水平的学者搞出了感知机,理论和实践证明了对线性可分问题的有效性,引起一阵轰动,特别是非科学圈类 ...
- 20155233 2016-2017-2 《Java程序设计》第6周学习总结
20155233 2016-2017-2 <Java程序设计>第6周学习总结 学习目标 理解流与IO 理解InputStream/OutPutStream的继承架构 理解Reader/Wr ...
- 【转】.NET+AE开发中常见几种非托管对象的释放
尝试读取或写入受保护的内存.这通常指示其他内存已损坏. 今天在开发时遇到一个问题:" 未处理 System.AccessViolationException Message="尝试 ...
- Xor Sum(HDU4825 + 字典树)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4825 题目: 题意: 先给你n个数,再进行q次查询,每次查询数s与原来给的n个数异或和最大的数. 思 ...
- curator框架的使用以及实现分布式锁等应用与zkclient操作zookeeper,简化复杂原生API
打开zookeeper集群 先体会一下原生API有多麻烦(可略过): //地址 static final String ADDR = "192.168.171.128:2181,192.16 ...
- 全网最全JS正则表达式 校验数字
Js代码 <script type="text/javascript"> function SubmitCk() { var reg = /^([a-zA-Z0-9]+ ...
- Redis—数据结构之list
Redis的列表对象底层所使用的数据结构其中之一就是list. list Redis的list是一个双端链表,其由3部分构成:链表节点.链表迭代器.链表.这一设计思想和STL的list是一样的,STL ...
- 【内核】几个重要的linux内核文件【转】
转自:http://www.cnblogs.com/lcw/p/3159394.html Preface 当用户编译一个linux内核代码后,会产生几个文件:vmlinz.initrd.img, 以及 ...
- MySQL主从复制-指定数据库复制
在/etc/my.cnf添加需要进行同步的数据库信息 #需要进行同步的数据库 #replicate-do-db=custmgr #replicate-do-db=sdata #replicate-ig ...
- marshmallow: 简化Python对象系列化
转载:http://www.thinksaas.cn/topics/0/594/594368.html marshmallow -一个轻量级的库用于将复杂对象转成简单的Python数据类型.或从简单的 ...