Portal
memset0 praise
Problem-solving ideas
Compare a data structure that disgusting
look at the problem face, immediately thought of discretization.
Then the one-dimensional sort, another dimension with Fenwick tree or tree line maintenance.
Then no idea of the
I found a property:
does not contain all of the colors, of course, so that only one color is not selected.
Then we can open a color for each \ (\ text {the SET} \) , so that you can compare easily find coverage at some point one color.
For up and down, as long as enough to run positive and negative sides.
Details Notes
- Gugu Gu
Reference Code
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#include <set>
#define rg register
using namespace std;
template < typename T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while (!isdigit(c)) f |= (c == '-'), c = getchar();
while (isdigit(c)) s = s * 10 + (c ^ 48), c = getchar();
s = f ? -s : s;
}
const int _ = 100010;
int t[_];
set < int > s[_];
set < int > ::iterator it;
int n, m, ans, X[_]; struct node{ int x, y, z; }p[_];
inline bool cmp(const node& a, const node& b) { return a.y < b.y; }
inline int lb(int x) { return x & -x; }
inline void update(int x) { for (rg int i = x; i <= n; i += lb(i)) ++t[i]; }
inline int query(int x) { int res = 0; for (rg int i = x; i >= 1; i -= lb(i)) res += t[i]; return res; }
inline void solve() {
memset(t, 0, sizeof t);
for (rg int i = 1; i <= m; ++i) s[i].clear(), s[i].insert(0), s[i].insert(n + 1);
for (rg int i = 1, j = 1; i <= n; i = j) {
while (j <= n && p[i].y == p[j].y) ++j;
for (rg int k = i; k < j; ++k) {
int r = *s[p[k].z].lower_bound(p[k].x) - 1;
int l = *--s[p[k].z].upper_bound(p[k].x);
ans = max(ans, query(r) - query(l));
}
for (rg int k = i; k < j; ++k) update(p[k].x), s[p[k].z].insert(p[k].x);
}
for (rg int x, i = 1; i <= m; ++i)
for (it = s[i].begin(); it != s[i].end(); )
x = *it, ans = max(ans, query(*++it - 1) - query(x));
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
#endif
int T; read(T);
while (T--) {
read(n), read(m);
for (rg int i = 1; i <= n; ++i)
read(p[i].x), read(p[i].y), read(p[i].z), X[i] = p[i].x;
sort(X + 1, X + n + 1), sort(p + 1, p + n + 1, cmp);
for (rg int i = 1; i <= n; ++i)
p[i].x = lower_bound(X + 1, X + n + 1, p[i].x) - X;
ans = 0;
solve(), reverse(p + 1, p + n + 1), solve();
printf("%d\n", ans);
}
return 0;
}
End Sahua \ (qwq \)