P1527 [国家集训队]矩阵乘法 [整体二分]
权值排序,整体二分,没了。
// by Isaunoya
#include <bits/stdc++.h>
using namespace std;
#define rep(i, x, y) for (register int i = (x); i <= (y); ++i)
#define Rep(i, x, y) for (register int i = (x); i >= (y); --i)
#define int long long
const int _ = 1 << 21;
struct I {
char fin[_], *p1 = fin, *p2 = fin;
inline char gc() {
return (p1 == p2) && (p2 = (p1 = fin) + fread(fin, 1, _, stdin), p1 == p2) ? EOF : *p1++;
}
inline I& operator>>(int& x) {
bool sign = 1;
char c = 0;
while (c < 48) ((c = gc()) == 45) && (sign = 0);
x = (c & 15);
while ((c = gc()) > 47) x = (x << 1) + (x << 3) + (c & 15);
x = sign ? x : -x;
return *this;
}
inline I& operator>>(double& x) {
bool sign = 1;
char c = 0;
while (c < 48) ((c = gc()) == 45) && (sign = 0);
x = (c - 48);
while ((c = gc()) > 47) x = x * 10 + (c - 48);
if (c == '.') {
double d = 1.0;
while ((c = gc()) > 47) d = d * 0.1, x = x + (d * (c - 48));
}
x = sign ? x : -x;
return *this;
}
inline I& operator>>(char& x) {
do
x = gc();
while (isspace(x));
return *this;
}
inline I& operator>>(string& s) {
s = "";
char c = gc();
while (isspace(c)) c = gc();
while (!isspace(c) && c != EOF) s += c, c = gc();
return *this;
}
} in;
struct O {
char st[100], fout[_];
signed stk = 0, top = 0;
inline void flush() {
fwrite(fout, 1, top, stdout), fflush(stdout), top = 0;
}
inline O& operator<<(int x) {
if (top > (1 << 20)) flush();
if (x < 0) fout[top++] = 45, x = -x;
do
st[++stk] = x % 10 ^ 48, x /= 10;
while (x);
while (stk) fout[top++] = st[stk--];
return *this;
}
inline O& operator<<(char x) {
fout[top++] = x;
return *this;
}
inline O& operator<<(string s) {
if (top > (1 << 20)) flush();
for (char x : s) fout[top++] = x;
return *this;
}
} out;
#define pb emplace_back
#define fir first
#define sec second
template < class T > inline void cmax(T & x , const T & y) {
(x < y) && (x = y) ;
}
template < class T > inline void cmin(T & x , const T & y) {
(x > y) && (x = y) ;
}
int n , m ;
const int N = 500 + 10 ;
const int M = 6e4 + 10 ;
struct point { int x , y , val ; } a[N * N] ;
struct que { int x , y , x2 , y2 , k , id ; } q[M] ;
int mcnt = 0 ;
int c[N][N] ;
int low(int x) { return x & -x ; }
void add(int x , int y , int v) {
for(int i = x ; i <= n ; i += low(i)) for(int j = y ; j <= n ; j += low(j)) c[i][j] += v ;
}
int qry(int x , int y) {
if(! x || ! y) return 0 ;
int ans = 0 ;
for(int i = x ; i > 0 ; i ^= low(i)) for(int j = y ; j > 0 ; j ^= low(j)) ans += c[i][j] ;
return ans ;
}
int qry(int x , int y , int x2 , int y2) {
return qry(x2 , y2) - qry(x - 1 , y2) - qry(x2 , y - 1) + qry(x - 1 , y - 1) ;
}
int ans[M] ;
que t1[M] , t2[M] ;
void solve(int l , int r , int ql , int qr) {
if(ql > qr) return ;
if(l == r) {
rep(i , ql , qr) ans[q[i].id] = a[l].val ;
return ;
}
int mid = l + r >> 1 ;
rep(i , l , mid) add(a[i].x , a[i].y , 1) ;
int cnt1 = 0 , cnt2 = 0 ;
rep(i , ql , qr) {
int res = qry(q[i].x , q[i].y , q[i].x2 , q[i].y2) ;
if(res >= q[i].k) t1[++ cnt1] = q[i] ;
else { t2[++ cnt2] = q[i] ; t2[cnt2].k -= res ; }
}
rep(i , l , mid) add(a[i].x , a[i].y , -1) ;
rep(i , 1 , cnt1) q[i + ql - 1] = t1[i] ;
rep(i , 1 , cnt2) q[ql + cnt1 + i - 1] = t2[i] ;
solve(l , mid , ql , ql + cnt1 - 1) ;
solve(mid + 1 , r , ql + cnt1 , qr) ;
}
signed main() {
#ifdef _WIN64
freopen("testdata.in" , "r" , stdin) ;
#endif
in >> n >> m ;
rep(i , 1 , n) rep(j , 1 , n) { int k ; in >> k ; a[++ mcnt] = { i , j , k } ; }
rep(i , 1 , m) { in >> q[i].x >> q[i].y >> q[i].x2 >> q[i].y2 >> q[i].k ; q[i].id = i ; }
sort(a + 1 , a + mcnt + 1 , [](point x , point y) { return x.val < y.val ; }) ;
solve(1 , mcnt , 1 , m) ; rep(i , 1 , m) out << ans[i] << '\n' ;
return out.flush(), 0;
}
P1527 [国家集训队]矩阵乘法 [整体二分]的更多相关文章
- 洛谷P1527 [国家集训队] 矩阵乘法 [整体二分,二维树状数组]
题目传送门 矩阵乘法 题目描述 给你一个N*N的矩阵,不用算矩阵乘法,但是每次询问一个子矩形的第K小数. 输入输出格式 输入格式: 第一行两个数N,Q,表示矩阵大小和询问组数: 接下来N行N列一共N* ...
- 洛谷$P1527$ [国家集训队]矩阵乘法 整体二分
正解:整体二分 解题报告: 传送门$QwQ$ 阿看到这种查询若干次第$k$小显然就想到整体二分$QwQ$? 然后现在就只要考虑怎么快速求出一个矩形内所有小于某个数的数的个数? 开始我的想法是离散化然后 ...
- Luogu1527 [国家集训队]矩阵乘法 (整体二分)(Unfinished)
全线RE的代码... 先搁这吧,下次再说.flag //#include <iostream> #include <cstdio> //#include <cstring ...
- 洛谷 P1527 [国家集训队]矩阵乘法 解题报告
P1527 [国家集训队]矩阵乘法 题目描述 给你一个\(N*N\)的矩阵,不用算矩阵乘法,但是每次询问一个子矩形的第\(K\)小数. 输入输出格式 输入格式: 第一行两个数\(N,Q\),表示矩阵大 ...
- [洛谷P1527] [国家集训队]矩阵乘法
洛谷题目链接:[国家集训队]矩阵乘法 题目背景 原 <补丁VS错误>请前往P2761 题目描述 给你一个N*N的矩阵,不用算矩阵乘法,但是每次询问一个子矩形的第K小数. 输入输出格式 输入 ...
- P1527 [国家集训队]矩阵乘法(整体二分)
Link 整体二分的经典例题. 对于整体二分,我个人的理解是二分答案套分治. 具体来说就是对答案进行二分,然后对于询问进行类似于权值线段树求区间第 \(k\) 大的分治做法. 首先,我们暴力做法就是对 ...
- P1527 [国家集训队]矩阵乘法
\(\color{#0066ff}{ 题目描述 }\) 给你一个N*N的矩阵,不用算矩阵乘法,但是每次询问一个子矩形的第K小数. \(\color{#0066ff}{输入格式}\) 第一行两个数N,Q ...
- 【LG1527】[国家集训队]矩阵乘法
[LG1527][国家集训队]矩阵乘法 题面 洛谷 题解 我也不知道为什么取个这样的名字... 其实就是区间\(kth\)扩展到二维 还是用整体二分搞啦,把树状数组换成二维的 其他的基本没有什么差别 ...
- Luogu-1527 [国家集训队]矩阵乘法
Luogu-1527 [国家集训队]矩阵乘法 题面 Luogu-1527 题解 昨天学CDQ分治时做了一些题,但是因为题(wo)太(tai)水(lan)了(le)并没有整理 学了一晚上的整体二分,拿这 ...
随机推荐
- mapreduce清洗数据
继上篇 MapReduce清洗数据 package mapreduce; import java.io.IOException; import org.apache.hadoop.conf.Confi ...
- 文件传输协议-FTP
一.FTP概述 FTP(File Transfer Protocol 文件传输协议)C/S结构的应用层协议.由服务端和客户端两个部分共同实现文件传输功能 FTP服务器普遍部署于内网中,具有容易部署.方 ...
- javascript Math对象 常用数字操作方法
var t='1.2'; parseInt(t) parseFloat(t)//1.2 Number(1.2)//1.2强制转换为数字 2.向上取整,有小数就整数部分加1 js: Math.ceil( ...
- Integer缓存机制-基本数据类型和包装类型-自动拆装箱
Integer缓存机制 总结: 1.jdk1.5对Integer新增了缓存机制,范围在-128-127(这个范围的整数值使用频率最高)内的自动装箱返回的是缓存对象,不会new新的对象,所以只要在缓存范 ...
- KubeSphere企业级分布式多租户容器管理平台
KubeSphere企业级分布式多租户容器管理平台 KubeSphere安装部署2.1.0DEV版本
- tomcat solr服务无法搜索中文
把tomcat的配置文件修改一下就好了, vi server.xml <Connector port="8080" protocol="HTTP/1.1" ...
- FastDFS 配置文件 storage.conf
FastDFS 版本5.05 配置文件分为三部分 控制器:tracker.conf存储器:storage.conf 客户端:client.conf 文件位置:/etc/fdfsstorage.co ...
- Windwos日志分析
Windows日志分析工具 查看系统日志方法: 在“开始”菜单上,依次指向“所有程序”.“管理工具”,然后单击“事件查看器” 按 "Window+R",输入 ”eventvwr.m ...
- MySQL命令随手记之alter
修改表名 alter table 表名 rename 新表名; //修改table名 添加.删除.修改字段 alter table 表名 add [column] 列名 数据类型; //添加colum ...
- python网络爬虫(三)requests库的13个控制访问参数及简单案例
酱酱~小编又来啦~