# bzoj1594 [Usaco2008 Jan]猜数游戏（二分答案+线段树/并查集）

upd: leoly表示这题哪有这么简单！是数据太水了！我来加强给你看！然后我就52了orz

## 线段树版

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 1000010
#define inf 0x3f3f3f3f
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
return x*f;
}
int n,m;
struct que{
int l,r,x;
}q[25010],a[25010];
struct node{
int x,len;bool lazy;//x-区间和
}tree[N<<2];
inline bool cmp(que x,que y){return x.x>y.x;}
void build(int p,int l,int r){
tree[p].x=tree[p].lazy=0;tree[p].len=r-l+1;if(l==r) return;
int mid=l+r>>1;
build(p<<1,l,mid);build(p<<1|1,mid+1,r);
}
inline void pushdown(int p){
if(!tree[p].lazy) return;
tree[p<<1].x=tree[p<<1].len;tree[p<<1|1].x=tree[p<<1|1].len;
tree[p<<1].lazy=1;tree[p<<1|1].lazy=1;tree[p].lazy=0;
}
inline void pushup(int p){
tree[p].x=tree[p<<1].x+tree[p<<1|1].x;
}
int ask(int p,int l,int r,int x,int y){//1-全覆盖
if(x<=l&&r<=y) return tree[p].x;
int mid=l+r>>1;pushdown(p);int res=0;
}
void cover(int p,int l,int r,int x,int y){
if(tree[p].x==tree[p].len) return;
if(x<=l&&r<=y){tree[p].x=tree[p].len;tree[p].lazy=1;return;}
int mid=l+r>>1;pushdown(p);
if(x<=mid) cover(p<<1,l,mid,x,y);
if(y>mid) cover(p<<1|1,mid+1,r,x,y);pushup(p);
}
bool jud(int mid){
for(int i=1;i<=mid;++i) a[i]=q[i];sort(a+1,a+mid+1,cmp);
build(1,1,n);int top=1e9;
for(int i=1;i<=mid;++i){
int last=i+1,l=a[i].l,r=a[i].r,l1=l,r1=r;//l,r--并集，l1,r1--交集
while(last<=mid&&a[last].x==a[i].x){
if(a[last].l>r1||a[last].r<l1) return 0;
l1=max(l1,a[last].l);r1=min(r1,a[last].r);
l=min(l,a[last].l);r=max(r,a[last].r);++last;
int cnt=tree[1].x;cover(1,1,n,l,r);cnt=tree[1].x-cnt;
if(top-a[i].x+1<cnt) return 0;top-=cnt;//判断能不能把新位置都填上
}return 1;
}
int main(){
//  freopen("a.in","r",stdin);
int l=1,r=m;
while(l<=r){
int mid=l+r>>1;
if(jud(mid)) l=mid+1;
else r=mid-1;
}if(r==m) puts("0");
else printf("%d\n",r+1);
return 0;
}

## 并查集版

#include<cstdio>
#include<algorithm>
#define Q 30000
#define N 1100000
#define inf 0x3f3f3f3f
using namespace std;
inline char gc(){
static char now[1<<16],*T,*S;
return *S++;
}
int x=0;char ch=gc();
while (ch<'0'||ch>'9') ch=gc();
while (ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=gc();}
return x;
}
struct node{
int l,r,v;
}data[Q],data1[Q];
inline bool cmp(node a,node b){
if (a.v>b.v) return true;else if (a.v<b.v) return false;
return a.l<b.l;
}
int fa[N],n;
inline int find(int x){
return x==fa[x]?x:fa[x]=find(fa[x]);
}
inline bool check(int x){
int l=0,r=inf;int l1=inf,r1=0;
for (int i=1;i<=n+1;++i) fa[i]=i;
for (int i=1;i<=x;++i) data[i]=data1[i];sort(data+1,data+x+1,cmp);
int last_v=data[1].v;l=l1=data[1].l;r=r1=data[1].r;
for (int i=2;i<=x;++i){
if (last_v!=data[i].v){
int x=find(l);
if(x<=r) {int now=find(l1);
while (now<=r1) fa[now]=fa[now+1],now=find(now+1);
}else return false;
last_v=data[i].v;l=l1=data[i].l;r=r1=data[i].r;
}else{
l=max(l,data[i].l);r=min(r,data[i].r);l1=min(l1,data[i].l);r1=max(r1,data[i].r);
if (l>r) return false;
};
}if (find(l)>r) return false;
return true;
}
int l,r,q;
int main(){
//freopen("bzoj1594.in","r",stdin);
}