BZOJ4604:The kth maximum number
浅谈离线分治算法:https://www.cnblogs.com/AKMer/p/10415556.html
题目传送门:https://lydsy.com/JudgeOnline/problem.php?id=4604
整体二分套\(CDQ\)分治,判断一个询问的答案是否大于等于\(mid\)的时候用\(cdq\)分治数点就行了。
注意\(x\)相同的时候先修改。
时间复杂度:\(O(nlog^3n)\)
空间复杂度:\(O(n)\)
代码如下:
#include <cstdio>
#include <algorithm>
using namespace std;
#define low(i) ((i)&(-(i)))
const int maxn=5e5+5;
bool bo[maxn];
int ans[maxn];
int n,Q,ans_cnt,maxv;
int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
}
struct query {
int id,x1,x2,y1,y2,k;
query() {}
query(int _id,int _x1,int _y1,int _x2,int _y2,int _k) {
id=_id,x1=_x1,x2=_x2,y1=_y1,y2=_y2,k=_k;
}
bool operator<(const query &a)const {
if(x1==a.x1)return id<a.id;
return x1<a.x1;
}
}p[maxn],tmp[maxn<<2];
struct tree_array {
int c[maxn];
void add(int pos,int v) {
for(int i=pos;i<=n;i+=low(i))
c[i]+=v;
}
int query(int pos) {
int res=0;
for(int i=pos;i;i-=low(i))
res+=c[i];
return res;
}
}T;
void cdq_solve(int l,int r) {
if(l>=r)return;
int mid=(l+r)>>1;
cdq_solve(l,mid),cdq_solve(mid+1,r);
sort(tmp+l,tmp+r+1);
for(int i=l;i<=r;i++)
if(!tmp[i].id&&tmp[i].y2<=mid)T.add(tmp[i].y1,1);
else if(tmp[i].id&&tmp[i].y2>mid)
ans[tmp[i].id]+=tmp[i].x2*T.query(tmp[i].y1);
for(int i=l;i<=r;i++)
if(!tmp[i].id&&tmp[i].y2<=mid)T.add(tmp[i].y1,-1);
}
void solve(int l,int r,int st,int ed) {
if(ed<st)return;
if(l==r) {
for(int i=st;i<=ed;i++)
ans[p[i].id]=l;
return;
}
int mid=(l+r)>>1;
int cnt=0;
for(int i=st;i<=ed;i++)
if(!p[i].id) {
if(p[i].k>mid)
tmp[++cnt]=p[i],tmp[cnt].y2=cnt;
}
else {
ans[p[i].id]=0;
++cnt,tmp[cnt]=query(p[i].id,p[i].x2,p[i].y2,1,cnt,0);
++cnt,tmp[cnt]=query(p[i].id,p[i].x2,p[i].y1-1,-1,cnt,0);
++cnt,tmp[cnt]=query(p[i].id,p[i].x1-1,p[i].y2,-1,cnt,0);
++cnt,tmp[cnt]=query(p[i].id,p[i].x1-1,p[i].y1-1,1,cnt,0);
}
cdq_solve(1,cnt),cnt=0;
for(int i=st;i<=ed;i++)
if(!p[i].id) {
if(p[i].k>mid)bo[i]=0;
else bo[i]=1,cnt++;
}
else {
if(ans[p[i].id]>=p[i].k)bo[i]=0;
else bo[i]=1,cnt++,p[i].k-=ans[p[i].id];
}
int ED=st,ST=st+cnt;
for(int i=st;i<=ed;i++)
if(bo[i])tmp[ED++]=p[i];
else tmp[ST++]=p[i];
for(int i=st;i<=ed;i++)p[i]=tmp[i];
solve(l,mid,st,ED-1),solve(mid+1,r,ED,ed);
}
int main() {
n=read(),Q=read();
for(int i=1;i<=Q;i++) {
int opt=read();
if(opt==1) {
int x=read(),y=read(),k=read();
p[i]=query(0,x,y,0,0,k),maxv=max(maxv,k);
}
else {
int x1=read(),y1=read(),x2=read(),y2=read(),k=read();
p[i]=query(++ans_cnt,x1,y1,x2,y2,k);
}
}
solve(0,maxv,1,Q);
for(int i=1;i<=ans_cnt;i++)
if(ans[i])printf("%d\n",ans[i]);
else puts("NAIVE!ORZzyz.");
return 0;
}
BZOJ4604:The kth maximum number的更多相关文章
- 【bzoj4604】The kth maximum number
暴力 #include<algorithm> #include<iostream> #include<cstdlib> #include<cstring> ...
- hdu 4006 The kth great number (优先队列)
/********************************************************** 题目: The kth great number(HDU 4006) 链接: h ...
- 转:java.io.IOException: Exceeeded maximum number of redirects: 5 解决版本
Jmeter运行的时候出现的重定向超过n次的问题: When trying to test a Silverlight application, I get the below error. Has ...
- 错误:maximum number of expressions in a list is 1000
某一日发现这么如下这么一个错误 --> maximum number of expressions in a list is 1000 原因:因为SQL语句中用到了IN字句,而IN中的元素个数 ...
- 【POI】导出xls文件报错:The maximum number of cell styles was exceeded. You can define up to 4000 styles in a .xls workbook
使用POI导出xls文件,由于数据过多,导致导出xls报错如下: The maximum number of cell styles was exceeded. You can define up t ...
- 错误: 实例 "ahwater-linux-core" 执行所请求操作失败,实例处于错误状态。: 请稍后再试 [错误: Exceeded maximum number of retries. Exceeded max scheduling attempts 3 for instance 7c1609c9-9d0f-4836-85b3-cefd45f942a7. Last exception: [u
错误: 实例 "ahwater-linux-core" 执行所请求操作失败,实例处于错误状态.: 请稍后再试 [错误: Exceeded maximum number of ret ...
- leetcode解题报告(15):Third Maximum Number
描述 Given a non-empty array of integers, return the third maximum number in this array. If it does no ...
- iOS---The maximum number of apps for free development profiles has been reached.
真机调试免费App ID出现的问题The maximum number of apps for free development profiles has been reached.免费应用程序调试最 ...
- [LeetCode] Third Maximum Number 第三大的数
Given a non-empty array of integers, return the third maximum number in this array. If it does not e ...
随机推荐
- 试坑不完美的 clip-path (我说的 CSS 的那个)
需求跟我说,咱们要创新,想做一个蜂巢状的列表,年少无知的我竟然一口答应了,全然因为刚接触了 clip-path: But,然而,不幸的是,这只是坎坷路途的开始.... clip-path 的教程很多了 ...
- CSS 命名里面有大学问
其实迟迟不敢开始写关于样式里布局方面的心得, 因为大多数人眼中,哪需要管那么多,只需要最终效果达到了就行了呗. 然而,即使是如今国内外顶级大牛也不敢说自己是个优秀的 CSS 工程师. 一般大公司都是 ...
- java.lang.ClassFormatError Duplicate field name&signature in class file XXXXXX【转】
本文转载自:https://blog.csdn.net/ylchou/article/details/7739742 2012-7-5 15:06:25org.apache.catalina.core ...
- Go 文件操作
一.读取文件 普通版 ioutil版 bufio版 二.文件写入 普通版 ioutil版 bufio版 三.文件复制 ioCopy 1.普通版读取文件 package main import ( &q ...
- json对象与字符串互转方法
字符串转json对象: var data = eval( '(' + str + ')' ); json对象转字符串: var jsonStr = JSON.stringify( obj );
- SQL server 2008 T-sql 总结
数据库的实现 1.添加数据:insert [into] 表名 (字段1,字段2,···) values (值1,值2,····) 其中,into可选. 2.修改数据:update 表名 set ...
- linux命令:mv命令
mv命令是move的缩写,可以用来移动文件或者将文件改名(move (rename) files),是Linux系统下常用的命令,经常用来备份文件或者目录. 1.命令格式: mv [选项] 源文件或目 ...
- Struts2学习(1)
struts2概述 1.struts2框架应用javaee三层结构中web层框架. 2.strut2框架在struts1和webwork基础之上发展全新的框架. 3.struts2解决的问题: 4.版 ...
- review06
使用关键字interface来定义一个接口.接口的定义和类定义很相似,分为接口声明和接口体. 接口体中包含常量的声明(没有变量)和抽象方法两部分.接口中只有抽象方法,没有普通方法.而且接口体中所有的常 ...
- 考虑 PHP 5.0~5.6 各版本兼容性的 cURL 文件上传
最近做的一个需求,要通过PHP调用cURL,以multipart/form-data格式上传文件.踩坑若干,够一篇文章了. 重要警告 没事不要读PHP的官方中文文档!版本跟不上坑死你! 不同版本PHP ...