【BZOJ4548】小奇的糖果

来源:互联网 发布:意大利语自学软件 编辑:程序博客网 时间:2024/04/27 19:12

Description

有 N 个彩色糖果在平面上。小奇想在平面上取一条水平的线段,并拾起它上方或下方的所有糖果。求出最多能够拾

起多少糖果,使得获得的糖果并不包含所有的颜色。
Input

包含多组测试数据,第一行输入一个正整数 T 表示测试数据组数。

接下来 T 组测试数据,对于每组测试数据,第一行输入两个正整数 N、K,分别表示点数和颜色数。
接下来 N 行,每行描述一个点,前两个数 x, y (|x|, |y| ≤ 2^30 - 1) 描述点的位置,最后一个数 z (1 ≤ z ≤
k) 描述点的颜色。
对于 100% 的数据,N ≤ 100000,K ≤ 100000,T ≤ 3
Output

对于每组数据在一行内输出一个非负整数 ans,表示答案

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

跟CERC那题差不多..
因为有负数所以正着一遍反着一遍

#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 longusing 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)  add(s[i].x,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;        while (j<=n&&s[j].y==s[i].y)    add(s[j++].x,-1);        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);    }}
1 0
原创粉丝点击