Codeforces-846D:Monitor(二维线段树)

来源:互联网 发布:淘宝爱用交易怎么样 编辑:程序博客网 时间:2024/05/17 21:18

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 52 1 82 2 81 2 11 3 42 3 2
output
8
input
3 3 2 51 2 22 2 12 3 53 2 102 1 100
output
-1
思路:每个格子记录一个时间,然后暴力查询k*k的格子中时间的最大值,取这些最大值中的最小值作为答案。查询就要用到二维线段树了。

#include<bits/stdc++.h>using namespace std;int a[5000][5000];int n,m;void clobuild(int tag,int p,int k,int l,int r){    if(l==r)    {        if(tag){a[p][k]=1e9+7;return;}        a[p][k]=max(a[2*p][k],a[2*p+1][k]);        return;    }    clobuild(tag,p,2*k,l,(l+r)/2);    clobuild(tag,p,2*k+1,(l+r)/2+1,r);    if(tag)a[p][k]=max(a[p][2*k],a[p][2*k+1]);    else a[p][k]=max(a[2*p][k],a[2*p+1][k]);}void rowbuild(int k,int l,int r){    if(l==r){clobuild(1,k,1,1,m);return;}    rowbuild(2*k,l,(l+r)/2);    rowbuild(2*k+1,(l+r)/2+1,r);    clobuild(0,k,1,1,m);}void clochange(int tag,int p,int k,int y,int t,int l,int r){    if(l==r)    {        if(tag)a[p][k]=t;        else a[p][k]=max(a[2*p][k],a[2*p+1][k]);        return;    }    if(y<=(l+r)/2)clochange(tag,p,2*k,y,t,l,(l+r)/2);    else clochange(tag,p,2*k+1,y,t,(l+r)/2+1,r);    if(tag)a[p][k]=max(a[p][2*k],a[p][2*k+1]);    else a[p][k]=max(a[2*p][k],a[2*p+1][k]);}void rowchange(int k,int x,int y,int t,int l,int r){    if(l==r)clochange(1,k,1,y,t,1,m);return;    if(x<=(l+r)/2)rowchange(2*k,x,y,t,l,(l+r)/2);    else rowchange(2*k+1,x,y,t,(l+r)/2+1,r);    clochange(0,k,1,y,t,1,m);}int cloask(int p,int k,int y,int y1,int l,int r){    if(y<=l&&y1>=r)return a[p][k];    if(y1<=(l+r)/2)return cloask(p,2*k,y,y1,l,(l+r)/2);    else if(y>=((l+r)/2+1))return cloask(p,2*k+1,y,y1,(l+r)/2+1,r);    return max(cloask(p,2*k,y,y1,l,(l+r)/2),cloask(p,2*k+1,y,y1,(l+r)/2+1,r));}int rowask(int k,int x,int y,int x1,int y1,int l,int r){    if(x<=l&&x1>=r)return cloask(k,1,y,y1,1,m);    if(x1<=(l+r)/2)return rowask(2*k,x,y,x1,y1,l,(l+r)/2);    else if(x>=(l+r)/2+1)return rowask(2*k+1,x,y,x1,y1,(l+r)/2+1,r);    return max(rowask(2*k,x,y,x1,y1,l,(l+r)/2),rowask(2*k+1,x,y,x1,y1,(l+r)/2+1,r));}int main(){    int k,T;    scanf("%d%d%d%d",&n,&m,&k,&T);    rowbuild(1,1,n);    while(T--)    {        int x,y,t;        scanf("%d%d%d",&x,&y,&t);        rowchange(1,x,y,t,1,n);    }    int ans=1e9+7;    for(int i=1;i+k-1<=n;i++)    {        for(int j=1;j+k-1<=m;j++)        {           ans=min(ans,rowask(1,i,j,i+k-1,j+k-1,1,n));        }    }    if(ans==1e9+7)ans=-1;    cout<<ans<<endl;    return 0;}