整体二分(模板一)静态区间第K大
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#include <cstdio>//sprintf islower isupper
#include <cstdlib>//malloc exit strcat itoa system("cls")
#include <iostream>//pair
#include <fstream>//freopen("C:\\Users\\13606\\Desktop\\Input.txt","r",stdin);
#include <bitset>
//#include <map>
//#include<unordered_map>
#include <vector>
#include <stack>
#include <set>
#include <string.h>//strstr substr strcat
#include <string>
#include <time.h>// srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
#include <cmath>
#include <deque>
#include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
#include <vector>//emplace_back
//#include <math.h>
#include <cassert>
#include <iomanip>
//#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
#include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
//******************
clock_t __START,__END;
double __TOTALTIME;
void _MS(){__START=clock();}
void _ME(){__END=clock();__TOTALTIME=(double)(__END-__START)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;}
//***********************
#define rint register int
#define fo(a,b,c) for(rint a=b;a<=c;++a)
#define fr(a,b,c) for(rint a=b;a>=c;--a)
#define mem(a,b) memset(a,b,sizeof(a))
#define pr printf
#define sc scanf
#define ls rt<<1
#define rs rt<<1|1
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef long long ll;
const double E=2.718281828;
const double PI=acos(-1.0);//
const ll INF=(1LL<<);
const int inf=(<<);
const double ESP=1e-;
const int mod=(int)1e9+;
const int N=(int)1e6+; PII a[N];
int ans[N];
int c[N],maxn,now;
int lowbit(int n){return n&(-n);}
void add(int i,int t)
{
while(i<=maxn)
{
c[i]+=t;
i+=lowbit(i);
}
}
int Sum(int i)
{
int sum=;
while(i)
{
sum+=c[i];
i-=lowbit(i);
}
return sum;
}
struct node
{
int l,r,k,id;
}q[N],temp[N]; void solve(int l,int r,int ql,int qr,int n)
{
int L=ql,R=qr,mid=l+r>>; if(l==r)
{
// pr("1\n");
for(int i=ql;i<=qr;++i)ans[q[i].id]=l;
return;
} while(a[now+].first<=mid&&now+<=n)++now,add(a[now].second,);//这边保证了一个位置最大经过3次;
while(now&&a[now].first>mid)add(a[now].second,-),--now;// 全清空会TLE; for(int i=ql;i<=qr;++i)
{
int res=Sum(q[i].r)-Sum(q[i].l-);
if(res>=q[i].k)temp[L++]=q[i];
else temp[R--]=q[i];
}
for(int i=ql;i<=qr;++i)
q[i]=temp[i];
if(L>ql)solve(l,mid,ql,L-,n);
if(R<qr)solve(mid+,r,R+,qr,n);
} int main()
{
int T;
sc("%d",&T);
while(T--)
{
int n,m;
sc("%d%d",&n,&m);
for(int i=;i<=n;++i)sc("%d",&a[i].first),a[i].second=i,c[i]=;
maxn=n;
now=;
for(int i=;i<=m;++i)
sc("%d%d%d",&q[i].l,&q[i].r,&q[i].k),q[i].id=i;
sort(a+,a++n);
solve(-inf,inf,,m,n);
for(int i=;i<=m;++i)
pr("%d\n",ans[i]);
}
return ;
} /**************************************************************************************/
整体二分(模板一)静态区间第K大的更多相关文章
- 整体二分(模板) 求区间第k小
整体二分,将询问与初值一起放入一个结构体里,然后每次二分判断询问在哪边,树状数组维护,时间复杂度O((n+Q)lognlogMAX_a[i] 代码 #include<iostream> # ...
- poj2104&&poj2761 (主席树&&划分树)主席树静态区间第k大模板
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 43315 Accepted: 14296 Ca ...
- 静态区间第k大(归并树)
POJ 2104为例 思想: 利用归并排序的思想: 建树过程和归并排序类似,每个数列都是子树序列的合并与排序. 查询过程,如果所查询区间完全包含在当前区间中,则直接返回当前区间内小于所求数的元素个数, ...
- 可持久化线段树(主席树)——静态区间第k大
主席树基本操作:静态区间第k大 #include<bits/stdc++.h> using namespace std; typedef long long LL; ,MAXN=2e5+, ...
- HDU3473--Minimum Sum(静态区间第k大)
Minimum Sum Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- 主席树(静态区间第k大)
前言 如果要求一些数中的第k大值,怎么做? 可以先就这些数离散化,用线段树记录每个数字出现了多少次. ... 那么考虑用类似的方法来求静态区间第k大. 原理 假设现在要有一些数 我们可以对于每个数都建 ...
- 整体二分初探 两类区间第K大问题 poj2104 & hdu5412
看到好多讲解都把整体二分和$CDQ$分治放到一起讲 不过自己目前还没学会$CDQ$分治 就单独谈谈整体二分好了 先推荐一下$XHR$的 <浅谈数据结构题的几个非经典解法> 整体二分在当中有 ...
- 数据结构2 静态区间第K大/第K小
给定数组$A[1...N]$, 区间$[L,R]$中第$K$大/小的数的指将$A[L...R]$中的数从大到小/从小到大排序后的第$K$个. "静态"指的是不带修改. 这个问题有多 ...
- 主席树学习笔记(静态区间第k大)
题目背景 这是个非常经典的主席树入门题——静态区间第K小 数据已经过加强,请使用主席树.同时请注意常数优化 题目描述 如题,给定N个整数构成的序列,将对于指定的闭区间查询其区间内的第K小值. 输入输出 ...
- 主席树初步学习笔记(可持久化数组?静态区间第k大?)
我接触 OI也快1年了,然而只写了3篇博客...(而且还是从DP跳到了主席树),不知道我这个机房吊车尾什么时候才能摸到大佬们的脚后跟orz... 前言:主席树这个东西,可以说是一种非常畸形的数据结构( ...
随机推荐
- 实战 Prometheus 搭建监控系统
实战 Prometheus 搭建监控系统 Prometheus 是一款基于时序数据库的开源监控告警系统,说起 Prometheus 则不得不提 SoundCloud,这是一个在线音乐分享的平台,类似于 ...
- c++ 容器反转
// reverse algorithm example #include <iostream> // std::cout #include <algorithm> // st ...
- input上传mp3格式文件,预览并且获取时间
<input type="file" id="file" name="file" class="upfile" o ...
- jenkins之自动化部署github上maven项目
部署流程:将代码从github上拉取下来,使用maven打包,将打包后的jar通过ssh发送到服务器上,然后构建docker镜像,运行容器. 1.安装插件 如果是第一次使用jenkins,需要检查并确 ...
- mysql中的union操作(整理)
mysql中的union操作(整理) 一.总结 一句话总结: union两侧的字段数和字段类型要是一样的 union可以接多个 orderby和排序可以在最后的union组合之后 1.union简单实 ...
- 执行git pull时提示Connection reset by 13.229.188.59 port 22
问题如下图: 解决办法: 1. 2. 3. 4. 5. 6.
- JAVA踩坑录
以前踩了很多坑,大多忘了.现在踩了坑,想起了一定记下来. 1. 字符串分割,这种工具类,首次使用一定要先看一眼,不然跳坑 commons-lang StringUtils.split分割时会去掉空串: ...
- 文件上传对servlet的要求
request.getParamter(String name)方法不能再使用了 需要使用request.getInputStream()获取输入流对象然后在进行读取数据 解析数据 ServletIn ...
- 013-java中的IO操作-InputStream/Reader、OutputStream/Writer
一.概述 IO流用来处理设备之间的数据传输,上传文件和下载文件,Java对数据的操作是通过流的方式,Java用于操作流的对象都在IO包中. 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称 ...
- 阶段5 3.微服务项目【学成在线】_day05 消息中间件RabbitMQ_9.RabbitMQ研究-工作模式-发布订阅模式-消费者
消费者需要写两个消费者 定义邮件的类 复制以前的代码到邮件类里面进行修改 最上面 声明队列的名称和交换机的名称 监听修改为email 的队列的名称 手机短信接收端 复制一份email的接收端的代码 改 ...