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. RabbitMQ (1) 环境安装

    1.下载erlang, 设置系统的环境变量 下载地址:http://www.erlang.org/downloads ERLANG_HOME=D:\Program\erl9.3 Path = %ERL ...

  2. 数据结构学习-AVL平衡树

    环境:C++ 11 + win10 IDE:Clion 2018.3 AVL平衡树是在BST二叉查找树的基础上添加了平衡机制. 我们把平衡的BST认为是任一节点的左子树和右子树的高度差为-1,0,1中 ...

  3. scrapy笔记2

    cookies的使用: 使用 scrapy.http.cookie.CookieJar 类的extract_cookies方法,CookieJar._cookies就是我们需要的cookies,是一个 ...

  4. 4,Flask 中的 request

    每个框架中都有处理请求的机制(request),但是每个框架的处理方式和机制是不同的 为了了解Flask的request中都有什么东西,首先我们要写一个前后端的交互 基于HTML + Flask 写一 ...

  5. 二、mysql数据库之基本操作和存储引擎

    一.知识储备 数据库服务器:一台计算机(对内存要求比较高) 数据库管理系统:如mysql,是一个软件 数据库:oldboy_stu,相当于文件夹 表:student,scholl,class_list ...

  6. 在ddms 里面查看data/data里面的东西 不显示data/data

    今天我要查看data/anr/tarces.txt,没办法,我只有root手机. 可是root之后,我发现还是不能查看或者导出traces.txt. 后来我才知道,root之后,文件夹权限没有变,所以 ...

  7. SetConsoleCtrlHandler

    Excerpt: Registering a Control Handler Function   This is an example of the SetConsoleCtrlHandler fu ...

  8. Windows环境下svn服务器的安装步骤

    做为一个程序开发人员,就算自己一个人写程序,也应该有一个SVN版本控制系统,以便对开发代码进行有效的管理. 下载SVN服务器 下载地址是:http://subversion.apache.org/pa ...

  9. 一道关于C++ 继承/虚函数 笔试题 [转]

    转自:http://www.cnblogs.com/yangyh/archive/2011/06/04/2072393.html 首先这位作者, 因为看了这篇简短的一个博文, 我相同了关于虚函数方面的 ...

  10. istringstream输入数据到数组

    istringstream iss(line); ; while (!(iss >> dat[n]).fail()) n++;