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
思路:二分时间或者二分会被毁坏的像素点数量也可以吧,后者虽然没写但是我觉得可以。
  然后运用二维前缀和,来查找是否存在k*k的像素缺口。
关于二维前缀和 https://blog.csdn.net/yzyyylx/article/details/78298318
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int P = ;
const int inf = 0x3f3f3f3f;
const int maxn = ;
struct node{
ll x,y;
ll t;
}pos[maxn];
ll n,m,k,q;
ll mp[][];
ll a[][],b[][];
int cmp(struct node & a, struct node & b){
return a.t<b.t;
} int check(ll rr){
memset(a,,sizeof(a));
memset(b,,sizeof(b));
for(int i = ; pos[i].t<= rr&&i <= q ; i++){
a[pos[i].x][pos[i].y] = ;
}
for(int i = ; i <= n ; i++){
for(int j = ; j <= m ; j++){
b[i][j] = b[i-][j]+b[i][j-]-b[i-][j-]+a[i][j];
}
}
for(int i = k ; i <= n ; i++){
for(int j = k; j <= m ; j++){
if(b[i][j]+b[i-k][j-k]-b[i-k][j]-b[i][j-k] == k*k) return ;
}
}
return ;
} ll ans = -;
ll Min = 1e10,Max = -; int main(){
memset(mp,-,sizeof(mp));
scanf("%lld %lld %lld %lld",&n,&m,&k,&q);
for(int i = ; i <= q ; i++){
scanf("%lld %lld %lld",&pos[i].x,&pos[i].y,&pos[i].t);
mp[pos[i].x][pos[i].y] = pos[i].t;
Min = min(Min,pos[i].t);
Max = max(Max,pos[i].t);
}
sort(pos + , pos + + q ,cmp);
ll l = Min , r = Max + ;
while(r - l > ){
ll mid = (r + l)/;
if(check(mid)){
ans = mid;
r = mid;
}else{
l = mid + ;
}
}
printf("%lld\n",ans);
return ;
}

一个从很久以前就开始做的梦。

C - Monitor CodeForces - 846D (二维前缀和 + 二分)的更多相关文章

  1. [CSP-S模拟测试]:physics(二维前缀和+二分+剪枝)

    题目传送门(内部题26) 输入格式 第一行有$3$个整数$n,m,q$.然后有$n$行,每行有一个长度为$m$的字符串,$+$表示正电粒子,$-$表示负电粒子.然后有$q$行,每行$2$个整数$x,y ...

  2. Codeforces 835C - Star sky - [二维前缀和]

    题目链接:http://codeforces.com/problemset/problem/835/C 题意: 在天空上划定一个直角坐标系,有 $n$ 颗星星,每颗星星都有坐标 $(x_i,y_i)$ ...

  3. HDU-6514 Monitor(二维前缀和+差分)

    http://acm.hdu.edu.cn/showproblem.php?pid=6514 Problem Description Xiaoteng has a large area of land ...

  4. Codeforces 1262E Arson In Berland Forest(二维前缀和+二维差分+二分)

     题意是需要求最大的扩散时间,最后输出的是一开始的火源点,那么我们比较容易想到的是二分找最大值,但是我们在这满足这样的点的时候可以发现,在当前扩散时间k下,以这个点为中心的(2k+1)2的正方形块内必 ...

  5. Codeforces 1080C 题解(思维+二维前缀和)

    题面 传送门 题目大意: 有一个黑白的棋盘,现在将棋盘上的一个子矩形全部染成黑色,另一个子矩形全部染成白色 求染完色后黑,白格子的总数 分析 我们可以发现,对于一个(1,1)到(x,y)的矩形,若xy ...

  6. openjudge1768 最大子矩阵[二维前缀和or递推|DP]

    总时间限制:  1000ms 内存限制:  65536kB 描述 已知矩阵的大小定义为矩阵中所有元素的和.给定一个矩阵,你的任务是找到最大的非空(大小至少是1 * 1)子矩阵. 比如,如下4 * 4的 ...

  7. COGS1752 [BOI2007]摩基亚Mokia(CDQ分治 + 二维前缀和 + 线段树)

    题目这么说的: 摩尔瓦多的移动电话公司摩基亚(Mokia)设计出了一种新的用户定位系统.和其他的定位系统一样,它能够迅速回答任何形如“用户C的位置在哪?”的问题,精确到毫米.但其真正高科技之处在于,它 ...

  8. poj-3739. Special Squares(二维前缀和)

    题目链接: I. Special Squares There are some points and lines parellel to x-axis or y-axis on the plane. ...

  9. Good Bye 2015 C. New Year and Domino 二维前缀

    C. New Year and Domino   They say "years are like dominoes, tumbling one after the other". ...

随机推荐

  1. border-radius 在 浏览器开发者工具移动端里是有效的,在真机是无效的。

    border-radius 在 浏览器开发者工具移动端里是有效的,在真机是无效的,怎么解决? 答案是 border-radius:20px !important 加上!important 就好了.

  2. ubuntu12.04 eclipse安装PyDev

    在ubuntu软件中心安装的eclipse版本为3.7,install new software时,搜索出来的PyDev版本较高(5.6...): 高版本的PyDev要求较高版本的eclipse.详情 ...

  3. [NOIP2017] T4 跳房子 DP+二分

    Description 跳房子,也叫跳飞机,是一种世界性的儿童游戏,也是中国民间传统的体育游戏之一.跳房子的游戏规则如下:  在地面上确定一个起点,然后在起点右侧画 n 个格子,这些格子都在同一条直线 ...

  4. XSS跨站脚本攻击与CSRF跨站请求伪造攻击的学习总结(转载)

    转载自 https://blog.csdn.net/baidu_24024601/article/details/51957270 之前就了解过这方面的知识,但是没有系统地总结.今天在这总结一下,也让 ...

  5. pythpon--类操作

    #coding=utf-8import numpy as npimport tensorflow as tfimport osos.environ["CUDA_VISIBLE_DEVICES ...

  6. TX2开发板Ubuntu16.04安装中文输入法

    打开终端输入安装输入法: sudo apt-get install fcitx fcitx-googlepinyin fcitx-module-cloudpinyin fcitx-sunpinyin ...

  7. SQL注入类型

    本文转自:https://www.cnblogs.com/cui0x01/p/6322826.html Sql注入_类型 1.sql注入 通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询 ...

  8. UML-持久框架-目标&关键思想

    1.目标 1).使用模板方法.状态模式.命令模式来设计部分框架 2).介绍对象-关系(O-R)映射中的一些问题 3).使用虚代理实现的滞后具体化 2.关键思想 1).映射(Mapping) 类--表 ...

  9. 2pc和3pc区别

    2pc和3pc区别 3pc背景:    2pc协议在协调者和执行者同时宕机时(协调者和执行者不同时宕机时,都能确定事务状态),选出协调者之后 无法确定事务状态,会等待宕机者恢复才会继续执行(无法利用定 ...

  10. Ubuntu使用小技巧汇总

    1. Ubuntu创建/切换root用户 sudo passwd rootsu root 2. 解决Ubuntu14.04系统没有自带右键打开终端的问题 sudo apt-get install na ...