BZOJ3126: [Usaco2013 Open]Photo
n<=200000个点,m<=100000个区间,每个区间有且仅有一个点,求最多几个点,无解-1。
http://www.cnblogs.com/Chorolop/p/7570191.html
WA了两次:看成最少几个点;判无解是<0而不一定=-inf。
1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 #include<algorithm> 5 //#include<iostream> 6 using namespace std; 7 8 int n,m; 9 #define maxn 200011 10 int l[maxn],r[maxn],f[maxn]; 11 int que[maxn],head,tail; 12 int x,y;const int inf=0x3f3f3f3f; 13 int main() 14 { 15 scanf("%d%d",&n,&m); 16 for (int i=1;i<=n+1;i++) r[i]=i-1; 17 memset(l,0,sizeof(l)); 18 for (int i=1;i<=m;i++) 19 { 20 scanf("%d%d",&x,&y); 21 r[y]=min(r[y],x-1); 22 l[y+1]=max(l[y+1],x); 23 } 24 for (int i=n;i>=1;i--) r[i]=min(r[i+1],r[i]); 25 for (int i=2;i<=n+1;i++) l[i]=max(l[i-1],l[i]); 26 que[head=(tail=1)-1]=0;f[0]=0; 27 for (int i=1;i<=n+1;i++) 28 { 29 for (int j=r[i-1]+1;j<=r[i];j++) 30 { 31 while (head<tail && f[que[tail-1]]<=f[j]) tail--; 32 que[tail++]=j; 33 } 34 while (head<tail && que[head]<l[i]) head++; 35 if (head<tail) f[i]=f[que[head]]+1; 36 else f[i]=-inf; 37 } 38 if (f[n+1]<0) puts("-1"); 39 else printf("%d\n",f[n+1]-1); 40 return 0; 41 }