Codeforces 846D Monitor(简单二分+二维BIT)
2 seconds
256 megabytes
standard input
standard output
Recently Luba bought a monitor. Monitor is a rectangular matrix of size n × m. But then she started to notice that some pixels cease to work properly. Luba thinks that the monitor will become broken the first moment when it contains a square k × k consisting entirely of broken pixels. She knows that q pixels are already broken, and for each of them she knows the moment when it stopped working. Help Luba to determine when the monitor became broken (or tell that it's still not broken even after all q pixels stopped working).
The first line contains four integer numbers n, m, k, q (1 ≤ n, m ≤ 500, 1 ≤ k ≤ min(n, m), 0 ≤ q ≤ n·m) — the length and width of the monitor, the size of a rectangle such that the monitor is broken if there is a broken rectangle with this size, and the number of broken pixels.
Each of next q lines contain three integer numbers xi, yi, ti (1 ≤ xi ≤ n, 1 ≤ yi ≤ m, 0 ≤ t ≤ 109) — coordinates of i-th broken pixel (its row and column in matrix) and the moment it stopped working. Each pixel is listed at most once.
We consider that pixel is already broken at moment ti.
Print one number — the minimum moment the monitor became broken, or "-1" if it's still not broken after these q pixels stopped working.
2 3 2 5
2 1 8
2 2 8
1 2 1
1 3 4
2 3 2
8
3 3 2 5
1 2 2
2 2 1
2 3 5
3 2 10
2 1 100
-1
题目链接:CF 846D
显然二分一下时间,然后把小于等于二分时间t的坏点坐标加入到二维矩阵中,然后暴力枚举k*k矩阵的右下角端点看是否存在矩阵的和刚好为k*k,若存在则说明这一块全坏了,看范围懂做法的水题。。。
代码:
#include <stdio.h>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <bitset>
#include <string>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define fin(name) freopen(name,"r",stdin)
#define fout(name) freopen(name,"w",stdout)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 510;
int T[N][N];
struct info
{
int x, y, t;
bool operator<(const info &rhs)const
{
return t < rhs.t;
}
};
inline int lowbit(const int &x)
{
return x & (-x);
}
info arr[N * N];
int n, m, k, q;
int kk; inline void add(int x, int y)
{
for (int i = x; i < N; i += lowbit(i))
for (int j = y; j < N; j += lowbit(j))
T[i][j] += 1;
}
inline sum(int x, int y)
{
int r = 0;
for (int i = x; i > 0; i -= lowbit(i))
for (int j = y; j > 0; j -= lowbit(j))
r += T[i][j];
return r;
}
inline bool check(int t)
{
CLR(T, 0);
int i, j;
for (i = 0; i < q; ++i)
{
if (arr[i].t <= t)
{
add(arr[i].x, arr[i].y);
}
else
break;
}
for (i = k; i <= n; ++i)
{
for (j = k; j <= m; ++j)
{
int area = sum(i, j) - sum(i - k, j) - sum(i, j - k) + sum(i - k, j - k);
if (area == kk)
return 1;
}
}
return 0;
}
int main(void)
{
int i;
while (~scanf("%d%d%d%d", &n, &m, &k, &q))
{
int L = 0, R = 0;
kk = k * k;
for (i = 0; i < q; ++i)
{
scanf("%d%d%d", &arr[i].x, &arr[i].y, &arr[i].t);
R = max(R, arr[i].t);
}
sort(arr, arr + q);
int ans = -1;
while (L <= R)
{
int mid = MID(L, R);
if (check(mid))
{
ans = mid;
R = mid - 1;
}
else
L = mid + 1;
}
printf("%d\n", ans);
}
return 0;
}
Codeforces 846D Monitor(简单二分+二维BIT)的更多相关文章
- 使用C语言实现二维,三维绘图算法(3)-简单的二维分形
使用C语言实现二维,三维绘图算法(3)-简单的二维分形 ---- 引言---- 每次使用OpenGL或DirectX写三维程序的时候, 都有一种隔靴搔痒的感觉, 对于内部的三维算法的实现不甚了解. 其 ...
- [BZOJ2738]矩阵乘法(整体二分+二维树状数组)
整体二分+二维树状数组. 好题啊!写了一个来小时. 一看这道题,主席树不会搞,只能用离线的做法了. 整体二分真是个好东西,啥都可以搞,尤其是区间第 \(k\) 大这种东西. 我们二分答案,然后用二维树 ...
- VC6下OpenGL 开发环境的构建外加一个简单的二维网络棋盘绘制示例
一.安装GLUT 工具包 GLUT 不是OpenGL 所必须的,但它会给我们的学习带来一定的方便,推荐安装. Windows 环境下的GLUT 本地下载地址:glut-install.zip(大小约为 ...
- 【bzoj2738】矩阵乘法 整体二分+二维树状数组
题目描述 给你一个N*N的矩阵,不用算矩阵乘法,但是每次询问一个子矩形的第K小数. 输入 第一行两个数N,Q,表示矩阵大小和询问组数:接下来N行N列一共N*N个数,表示这个矩阵:再接下来Q行每行5个数 ...
- 猫狗大战("简单的二维背包")
题面:https://www.luogu.com.cn/problem/P1489 看上去是一道简单的二维费用背包,但是要特别小心循环顺序. Ⅰ先循环物品,再循环限制条件. Ⅱ每一个限制条件都必须从后 ...
- codeforces 713D D. Animals and Puzzle 二分+二维rmq
题目链接 给一个01矩阵, 然后每个询问给出两个坐标(x1, y1), (x2, y2). 问你这个范围内的最大全1正方形的边长是多少. 我们dp算出以i, j为右下角的正方形边长最大值. 然后用二维 ...
- 【算法系列学习】codeforces D. Mike and distribution 二维贪心
http://codeforces.com/contest/798/problem/D http://blog.csdn.net/yasola/article/details/70477816 对于二 ...
- c#简单实现二维数组和二维数组列表List<>的转置
刚看到网上一篇文章里用sql实现了行列转置.sql server 2005/2008只用一个pivot函数就可以实现sql server 2000很多行的复杂实现.提到转置,立刻想起还在求学阶段曾经做 ...
- BZOJ2738矩阵乘法——整体二分+二维树状数组
题目描述 给你一个N*N的矩阵,不用算矩阵乘法,但是每次询问一个子矩形的第K小数. 输入 第一行两个数N,Q,表示矩阵大小和询问组数:接下来N行N列一共N*N个数,表示这个矩阵:再接下来Q行每行5 ...
随机推荐
- mysql存储问题
为什么标题要起这个名字呢?commen sence指的是那些大家都应该知道的事情,但往往大家又会会略这些东西,或者对这些东西一知半解,今天我总结下自己在mysql中遇到的一些commen sense类 ...
- Integer和int使用==比较的总结
public static void main(String[] args) { int i1 = 128; Integer i2 = 128; Integer i3 = new Integer(12 ...
- glibc2.12升级至2.15
1.操作系统版本 [root@localhost ~]# cat /etc/redhat-release #CentOS release 6.9 (Final) 2.当前glibc版本 [root@l ...
- php面向对象(2)值传递
PHP中值传递方式,2中 值传递:传递的时候,拷贝的是数据本身.默认都是值传递 结果:传递完成,有了2份同样的数据,且2个变量“相互独立”,不会相互影响 引用传递:传递的时候,拷贝的是引用关系(数据的 ...
- 【nginx】root alias 区别,以及server root , location root 区别
nginx-root-alias-详解 最近在研究前后端分离站点配置在同一域名下,发现root,alias有区别,而且所有的root如果都放置在location下面访问无效的问题,才有此总结,本文只是 ...
- 【linux】【指令】systemctl 指令部分解读
systemctl [OPTIONS...] {COMMAND} ... Query or send control commands to the systemd manager. -h --hel ...
- <Docker学习>6. docker使用网络
在容器中部署一个web应用,外部如何访问? 容器与容器间如何访问? 外部访问容器 容器可以运行一些网络应用,让外部也可以访问的话,需要进行服务器和容器的端口映射 -p 或者 -P -P默认会分配一个4 ...
- Choosing Capital for Treeland CodeForces - 219D (树形DP)
传送门 The country Treeland consists of n cities, some pairs of them are connected with unidirectional ...
- Codeforces Round #428 (Div. 2) C. dfs
C. Journey time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...
- python——集合
在python中,字典的亲戚就是集合,集合就是无映射关系的字典,花括号并不是字典的特权.如下面程序所示: >>> num = {} >>> type(num) &l ...