我真是哔狗了。。。
在路上yy出一个主席树的做法。。。然而似乎很麻烦。。。
结果一到机房就很坚定地把程序敲完了。。。
样例过了。。。
交一发诶怎么WA了。。。
改了几个觉得可能边界错了的地方。。。
继续WA。。。于是乎对拍。。。
然!后!发!现!有个地方不能直接求。。。要求的话得两个log而且代码量又会大一些。。。。
弃疗了一会。。。QAQ
后来还是在fsf的教导下冷静了会。。。
才想出了一个log的容易写的解法。。。
一个很直接的想法是,既然不能取全部的颜色,那么我可以一个颜色一个颜色的舍弃。那么对于某个颜色的某个点,假设我舍弃了它,那么相同颜色的也都得舍弃掉。因此如果将水平线定在当前点上,答案是在一个区间里面的。
那么就是扫描线。
如果扫x轴,就是之前sb的做法,大概得主席树两个log
如果扫y轴,那么就一个树状数组加K个set就可以了。
思路还是挺明显的。
时间复杂度 O(nlogn) ,空间 O(n)
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
#define pii pair<int , int>
#define mp make_pair
#define maxn 200007
#define maxm 4000007
inline int rd() {
char c = getchar();
while (!isdigit(c)) c = getchar() ; int x = c - '0';
while (isdigit(c = getchar())) x = x * 10 + c - '0';
return x;
}
inline void upmax(int&a , int b) { if (a < b) a = b ; }
struct Pic {
int x , y , c;
Pic() {}
Pic(int x , int y , int c):x(x) , y(y) , c(c) { }
}pic[maxn];
bool cmp(const Pic a , const Pic b) {
return (a.y < b.y) || (a.y == b.y && a.x < b.x);
}
typedef int arr[maxn];
typedef int seg[maxm];
typedef set<int>::iterator iter_set;
arr h;
int tot , n , K;
namespace BITS {
arr sum;
int n;
#define lowbit(x) (x & (-x))
void add(int x , int v) {
for (;x <= n;x += lowbit(x))
sum[x] += v;
}
int get(int x) {
int ret = 0;
for (;x;x -= lowbit(x))
ret += sum[x];
return ret;
}
void init(int _n) {
memset(sum , 0 , sizeof sum);
n = _n;
}
}
set<int> S[maxn];
void input() {
n = rd() , K = rd() , tot = 0;
BITS::init(n);
rep (i , 1 , n) {
int x = rd() , y = rd() , c = rd();
h[i] = x;
pic[i] = Pic(x , y , c);
}
sort(h + 1 , h + 1 + n);
tot = unique(h + 1 , h + 1 + n) - h - 1;
rep (i , 1 , n) pic[i].x = lower_bound(h + 1 , h + 1 + tot , pic[i].x) - h;
sort(pic + 1 , pic + 1 + n , cmp);
}
void solve() {
int ans = 0;
rep (i , 1 , K) S[i].clear();
rep (i , 1 , K) S[i].insert(0) , S[i].insert(tot + 1);
for (int i = 1 , j;i <= n;) {
for (j = i;j <= n && pic[i].y == pic[j].y;j ++) {
iter_set it = S[pic[j].c].lower_bound(pic[j].x);
int r = *it , l = *(-- it);
upmax(ans , BITS::get(r - 1) - BITS::get(l));
}
for (;i < j;i ++) {
BITS::add(pic[i].x , 1);
S[pic[i].c].insert(pic[i].x);
}
}
rep (i , 1 , K) {
int pre = 0;
for (iter_set it = ++ S[i].begin();it != S[i].end();it ++) {
int t = *it;
upmax(ans , BITS::get(t - 1) - BITS::get(pre));
pre = t;
}
}
printf("%d\n" , ans);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("data.txt" , "r" , stdin);
freopen("data.out" , "w" , stdout);
#endif
per (T , rd() , 1) {
input();
solve();
}
return 0;
}