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 5 2 1 8 2 2 8 1 2 1 1 3 4 2 3 2
8
3 3 2 5 1 2 2 2 2 1 2 3 5 3 2 10 2 1 100
-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 ", ans); } return 0; }