Monitor(二维RMQ)

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 qpixels 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

题意:给出一个n*m矩阵某些点的数字,没有给出的都是INF,查询每个K*K区域的最大值,输出其中最小的一个。

题解:模板二维RMQ维护区域最大值,这里直接有上一篇我写的模板。

//#include"bits/stdc++.h"
//#include<unordered_map>
//#include<unordered_set>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<set>
#include<vector>
#include<bitset>
#include<climits>
#include<queue>
#include<iomanip>
#include<cmath>
#include<stack>
#include<map>
#include<ctime>
#include<new>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define MT(a,b) memset(a,b,sizeof(a))
#define lson l, mid, node << 1
#define rson mid + 1, r, node << 1 | 1
const int INF  =  0x3f3f3f3f;
const int O    =  1e5;
const int mod  =  1e9 + 7;
const int maxn =  1e5+5;
const double PI  =  acos(-1.0);
const double E   =  2.718281828459;

int dp[505][505][10][10];
int val[505][505];
int num[505];

void rmq(int n, int m){
    
    int nn = log2(n), mm = log2(m);
    for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) dp[i][j][0][0] = val[i][j];
    
    for(int ii=0; ii<=nn; ii++)
    for(int jj=0; jj<=mm; jj++)
    if(ii + jj) {
        for(int i=1; i+(1<<ii)-1 <= n; i++)
        for(int j=1; j+(1<<jj)-1 <= m; j++) {
            if(ii) dp[i][j][ii][jj] = max(dp[i][j][ii-1][jj], dp[i+(1<<(ii-1))][j][ii-1][jj]);
            else dp[i][j][ii][jj] = max(dp[i][j][ii][jj-1], dp[i][j+(1<<(jj-1))][ii][jj-1]);
        }
    }
}

int query(int x1, int y1, int x2, int y2){
    int k1 = log2(x2-x1+1); //num[x2-x1+1];
    int k2 = log2(y2-y1+1); //num[y2-y1+1];
    x2 = x2 - (1<<k1) + 1;
    y2 = y2 - (1<<k2) + 1;
    
    int ans1 = max(dp[x1][y1][k1][k2], dp[x1][y2][k1][k2]);
    int ans2 = max(dp[x2][y1][k1][k2], dp[x2][y2][k1][k2]);
    return max(ans1, ans2);
}

void init(){ // 计算1-500的位数
    num[0] = -1;
    for(int i=1; i<=500; i++) num[i] = (i & (i-1)) ? num[i-1] : num[i-1] + 1;
}

int main(){
    //init();
    int n, m, k, q; scanf("%d%d%d%d", &n, &m, &k, &q);
    MT(val, INF);
    while(q --) {
        int x, y, t; scanf("%d%d%d", &x, &y, &t);
        val[x][y] = t;
    }
    rmq (n, m);
    int ans = INF;
    for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) {
        int x1 = i, x2 = i + k - 1, y1 = j, y2 = j + k - 1;
        if(x2 > n || y2 > m) continue;
        ans = min(query(x1, y1, x2, y2), ans );
    }
    printf("%d\n", ans == INF ? -1 : ans);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值