跟悬线法有点像
#include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring> #define cl(x) memset(x,0,sizeof(x)) using namespace std; inline char nc() { static char buf[100000],*p1=buf,*p2=buf; if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; } return *p1++; } inline void read(int &x) { char c=nc(),b=1; for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1; for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b; } const int N=100005; namespace BIT{ #define lowbit(x) ((x)&-(x)) int maxn,c[N]; inline void init(int n){ maxn=n; cl(c); } inline void add(int x,int r){ for (int i=x;i<=maxn;i+=lowbit(i)) c[i]+=r; } inline int sum(int x){ int ret=0; for (int i=x;i;i-=lowbit(i)) ret+=c[i]; return ret; } inline int sum(int l,int r){ if (r<l) return 0; return sum(r)-sum(l-1); } } struct abcd{ int x,y,w; int idx; int rx; }a[N]; bool cmpx(abcd A,abcd B){ return A.x<B.x; } bool cmpy(abcd A,abcd B){ return A.y<B.y; } int sx[N],icnt; inline int Bin(int x){ return lower_bound(sx+1,sx+icnt+1,x)-sx; } int n,K,ans; int pos[N],last[N]; int l[N],r[N]; inline void Solve() { using namespace BIT; int idx,lst; init(icnt); cl(last); for (int i=1;i<=n;i++) add(a[i].x,1); for (int i=1;i<=n;i++) l[i]=0,r[i]=n+1; sort(a+1,a+n+1,cmpx); pos[0]=0; pos[n+1]=icnt+1; for (int i=1;i<=n;i++){ a[i].rx=i; pos[i]=a[i].x; lst=last[a[i].w]; last[a[i].w]=i; l[i]=lst; if (lst) r[lst]=i; ans=max(ans,sum(a[lst].x+1,a[i].x-1)); } for (int i=1;i<=K;i++) ans=max(ans,sum(a[last[i]].x+1,icnt)); sort(a+1,a+n+1,cmpy); for (int i=1,j=1;i<=n;i++) { while (j<=n && a[i].y==a[j].y) add(a[j++].x,-1); ans=max(ans,sum(pos[l[a[i].rx]]+1,pos[r[a[i].rx]]-1)); if (r[a[i].rx]) l[r[a[i].rx]]=l[a[i].rx]; if (l[a[i].rx]) r[l[a[i].rx]]=r[a[i].rx]; r[a[i].rx]=l[a[i].rx]=0; } } int main() { int Q; freopen("t.in","r",stdin); freopen("t.out","w",stdout); read(Q); while (Q--) { icnt=0; ans=0; read(n); read(K); for (int i=1;i<=n;i++) read(a[i].x),read(a[i].y),read(a[i].w),sx[++icnt]=a[i].x; sort(sx+1,sx+icnt+1); icnt=unique(sx+1,sx+icnt+1)-sx-1; for (int i=1;i<=n;i++) a[i].x=Bin(a[i].x),pos[i]=a[i].x; Solve(); for (int i=1;i<=n;i++) a[i].y=-a[i].y; Solve(); printf("%d\n",ans); } return 0; }