# 【BZOJ4548】小奇的糖果

Description

Input

k) 描述点的颜色。

Output

Sample Input

1

10 3

1 2 3

2 1 1

2 4 2

3 5 3

4 4 2

5 1 2

6 3 1

6 7 1

7 2 3

9 4 2

Sample Output

5
HINT

Source

By Hzwer

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 100010
#define lowbit(x) (x&(-x))
#define GET (ch>='0'&&ch<='9')
#define LL long long
using namespace std;
template<class classname>
inline void in(classname &x)
{
char ch=getchar();x=0;int flag=1;
while (!GET)    flag=ch=='-'?-1:1,ch=getchar();
while (GET) x=x*10+ch-'0',ch=getchar();x*=flag;
}
int ans,T,n,k;
int c[MAXN],sta[MAXN];
int last[MAXN],pos[MAXN],l[MAXN],r[MAXN];
struct node {   int x,y,w,id;   }s[MAXN];
inline void add(int x,int val)  {   for (;x<=n+1;x+=lowbit(x))  c[x]+=val;  }
inline int query(int x)
{
int ret=0;
for (;x;x-=lowbit(x))   ret+=c[x];
return ret;
}
inline bool cmpx(node a,node b) {   return a.x<b.x; }
inline bool cmpy(node a,node b) {   return a.y<b.y; }
void solve()
{
memset(last,0,sizeof(last));memset(c,0,sizeof(c));
sort(s+1,s+n+1,cmpx);pos[0]=0;pos[n+1]=n+1;
for (int i=1;i<=n;++i)
{
int now=s[i].id,L=last[s[i].w];
l[now]=L;r[now]=n+1;last[s[i].w]=now;
if (L)  r[L]=now;
if (pos[L]+1<=pos[now]-1)   ans=max(ans,query(pos[now]-1)-query(pos[L]));
}
for (int i=1;i<=k;i++)  if (pos[last[i]]+1<=n)  ans=max(ans,query(n+1)-query(pos[last[i]]));
sort(s+1,s+n+1,cmpy);
for (int i=1,j=1;i<=n;++i)
{
int now=s[i].id;
l[r[now]]=l[now];r[l[now]]=r[now];
if (pos[r[now]]-1>=pos[l[now]]+1)   ans=max(ans,query(pos[r[now]]-1)-query(pos[l[now]]));
}
}
int main()
{
for (in(T);T;T--)
{
ans=0;in(n);in(k);
for (int i=1;i<=n;++i)  in(s[i].x),in(s[i].y),in(s[i].w),s[i].id=i,sta[i]=s[i].x;
sort(sta+1,sta+n+1);
for (int i=1;i<=n;++i)  s[i].x=lower_bound(sta+1,sta+n+1,s[i].x)-sta,pos[i]=s[i].x;
solve();
for (int i=1;i<=n;++i)  s[i].y=-s[i].y;
solve();
printf("%d\n",ans);
}
}