Codeforces-846D:Monitor(二维线段树)
来源:互联网 发布:淘宝爱用交易怎么样 编辑:程序博客网 时间:2024/05/17 21:18
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).
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.
Print one number — the minimum moment the monitor became broken, or "-1" if it's still not broken after these q pixels stopped working.
2 3 2 52 1 82 2 81 2 11 3 42 3 2
8
3 3 2 51 2 22 2 12 3 53 2 102 1 100
-1
#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;}
- Codeforces-846D:Monitor(二维线段树)
- Codeforces 846D Monitor(二维前缀和)
- codeforces 846D (二分+二维前缀和)Monitor
- Codeforces 677D 二维线段树 + 重要技巧
- Educational Codeforces Round 28 D. Monitor 二维rmq
- codeforces round 355 div2 D Vanya and Treasure Dp 二维线段树
- codeforces 19D 线段树
- CodeForces 370D Broken Monitor
- coderforces 846D Monitor
- Codeforces 12D (线段树 树状数组
- codeforces 46D 线段树 区间合并
- Codeforces 19D Points 线段树+set
- 【CodeForces】343D Water Tree 线段树
- codeforces 46D 线段树区间合并
- Points - CodeForces 19 D 线段树
- 【CodeForces】19D Points 线段树+set
- Codeforces 46D Parking Lot(线段树)
- codeforces 483d Interesting Array 线段树
- PHP环境搭建问题
- 前端面试中常见的算法问题总结
- uva 11988
- Codeforces 846 C Four Segments(前缀和+枚举)
- 杭电2955- Robberies
- Codeforces-846D:Monitor(二维线段树)
- Visual Studio 代码生成 运行时库的选择
- 王者荣耀T恤图案图形设计大赛—英雄台词字体设计
- 机器学习入门笔记(五)----过拟合问题
- 一起艳学SpringBoot
- CentOS 7下安装Python3.5
- linux env
- Android 6.0一个完整的native service
- mysql 密码修改过后忘记了重置的方法