D. Monitor
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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).

Input

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.

Output

Print one number — the minimum moment the monitor became broken, or "-1" if it's still not broken after these q pixels stopped working.

Examples
input
2 3 2 5
2 1 8
2 2 8
1 2 1
1 3 4
2 3 2
output
8
input
3 3 2 5
1 2 2
2 2 1
2 3 5
3 2 10
2 1 100
output
-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)的更多相关文章

  1. 使用C语言实现二维,三维绘图算法(3)-简单的二维分形

    使用C语言实现二维,三维绘图算法(3)-简单的二维分形 ---- 引言---- 每次使用OpenGL或DirectX写三维程序的时候, 都有一种隔靴搔痒的感觉, 对于内部的三维算法的实现不甚了解. 其 ...

  2. [BZOJ2738]矩阵乘法(整体二分+二维树状数组)

    整体二分+二维树状数组. 好题啊!写了一个来小时. 一看这道题,主席树不会搞,只能用离线的做法了. 整体二分真是个好东西,啥都可以搞,尤其是区间第 \(k\) 大这种东西. 我们二分答案,然后用二维树 ...

  3. VC6下OpenGL 开发环境的构建外加一个简单的二维网络棋盘绘制示例

    一.安装GLUT 工具包 GLUT 不是OpenGL 所必须的,但它会给我们的学习带来一定的方便,推荐安装. Windows 环境下的GLUT 本地下载地址:glut-install.zip(大小约为 ...

  4. 【bzoj2738】矩阵乘法 整体二分+二维树状数组

    题目描述 给你一个N*N的矩阵,不用算矩阵乘法,但是每次询问一个子矩形的第K小数. 输入 第一行两个数N,Q,表示矩阵大小和询问组数:接下来N行N列一共N*N个数,表示这个矩阵:再接下来Q行每行5个数 ...

  5. 猫狗大战("简单的二维背包")

    题面:https://www.luogu.com.cn/problem/P1489 看上去是一道简单的二维费用背包,但是要特别小心循环顺序. Ⅰ先循环物品,再循环限制条件. Ⅱ每一个限制条件都必须从后 ...

  6. codeforces 713D D. Animals and Puzzle 二分+二维rmq

    题目链接 给一个01矩阵, 然后每个询问给出两个坐标(x1, y1), (x2, y2). 问你这个范围内的最大全1正方形的边长是多少. 我们dp算出以i, j为右下角的正方形边长最大值. 然后用二维 ...

  7. 【算法系列学习】codeforces D. Mike and distribution 二维贪心

    http://codeforces.com/contest/798/problem/D http://blog.csdn.net/yasola/article/details/70477816 对于二 ...

  8. c#简单实现二维数组和二维数组列表List&lt;&gt;的转置

    刚看到网上一篇文章里用sql实现了行列转置.sql server 2005/2008只用一个pivot函数就可以实现sql server 2000很多行的复杂实现.提到转置,立刻想起还在求学阶段曾经做 ...

  9. BZOJ2738矩阵乘法——整体二分+二维树状数组

    题目描述 给你一个N*N的矩阵,不用算矩阵乘法,但是每次询问一个子矩形的第K小数. 输入   第一行两个数N,Q,表示矩阵大小和询问组数:接下来N行N列一共N*N个数,表示这个矩阵:再接下来Q行每行5 ...

随机推荐

  1. Ajax (Asynchronous javascript xml) 搜索框核心代码(JQuery) Ajax判断用户名存在核心代码 附:原生js的Ajax代码 其中有json的一句话解释

    前端 <script type="text/javascript"> $(function(){ $("#tid").keyup(function( ...

  2. OCCI线程安全

    线程是任务调度的基本单位,一个进程中可以有多个线程,每个线程有自己的堆栈空间, 进程中的代码段.数据段和堆栈对进程中的线程是可见的.在使用线程时通常都要考虑数据的安全访问. 常用的线程同步方法有: 互 ...

  3. Java反射+注解案例

    注解类代码,注解的属性可以有多个: package reflect; import java.lang.annotation.Retention; import java.lang.annotatio ...

  4. 汇编:实现C语言的 ||与&&运算

    ;C程序转汇编(或运算链接) DATAS SEGMENT a Dw b dw cc dw d dw m dw n dw string db dup(?) DATAS ends CODES SEGMEN ...

  5. 第五章 标准I/O

    5.1 引言 本章说明标准 I/O 库.因为不仅在 UNIX 上,而且在很多操作系统上都实现了此库,所以它由 ISO C 标准说明. 标准 I/O 库处理很多细节,例如缓冲区分配,以优化长度执行 I/ ...

  6. POJ 3254 状压DP(基础题)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17749   Accepted: 9342 Desc ...

  7. 012---Django的用户认证组件

    知识预览 用户认证 回到顶部 用户认证 auth模块 ? 1 from django.contrib import auth django.contrib.auth中提供了许多方法,这里主要介绍其中的 ...

  8. [BZOJ2120]数颜色(莫队算法)

    Description 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会像你发布如下指令: 1. Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜 ...

  9. UCLOUD使用云主机

    购买云主机后,购买弹性ip: 设置外网防火墙,浏览器否则无法访问服务器资源: 在云主机绑定自定义的防火墙: 使用ssh登录服务器: 一般centos自带apache,没有的话安装,具体教程百度: 安装 ...

  10. 3 破解密码,xshell连接

    1.破解root密码 (1)启动电脑,按上下键进入启动菜单界面,选择第二个Red Hat Enterprise Linux Server, with Linux 0-rescue-* (2)按 'e' ...