# POJ 3657 Haybale Guessing 并查集+二分答案

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn=25000+5,maxm=1000000+5;
int n,q,fa[maxm];
struct interval{
int x,y,MIN;
friend bool operator < (interval a,interval b){
return a.MIN>b.MIN;
}
}s[maxn],S[maxn];
inline int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
}
inline bool check(int pos){
for(int i=1;i<=n;i++)
fa[i]=i;
for(int i=1;i<=pos;i++)
S[i]=s[i];
sort(S+1,S+pos+1);
for(int i=1,j;i<=pos;i=j+1){
int x=S[i].x,X=x,y=S[i].y,Y=y;
j=i;
while(S[j+1].MIN==S[j].MIN&&j+1<=pos){
j++;
x=max(x,S[j].x),y=min(y,S[j].y);
X=min(X,S[j].x),Y=max(Y,S[j].y);
}
if(x>y||x>find(y))
return false;
while(X<=Y){
if(find(Y)==Y)
fa[Y]=find(X-1),Y--;
else
Y=find(Y);
}
}
return true;
}
signed main(void){
scanf("%d%d",&n,&q);
for(int i=1;i<=q;i++)
scanf("%d%d%d",&s[i].x,&s[i].y,&s[i].MIN);
int l=1,r=q,ans=0;
while(l<=r){
int mid=(l+r)>>1;
if(check(mid))
l=mid+1;
else
r=mid-1,ans=mid;
}
cout<<ans<<endl;
return 0;
}

by >_< neighthorn

举报原因： 您举报文章：POJ 3657 Haybale Guessing 并查集+二分答案 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)