【LOJ #6062】「2017 山東一輪集訓 Day2」Pair(霍爾定理+線段樹)

傳送門

霍爾定理就是對於二分圖的完美匹配
一定有一邊的任意一個子集SS
向另一邊的相連的集合大小>=S>=|S|

於是把BB排序後
連的一定是一個後綴
用線段樹維護每個點及以後連了多少條邊
那麼必須滿足vii>=0v_i-i>=0
線段樹維護即可

#include<bits/stdc++.h>
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair<int,int>
#define ll long long
#define fi first
#define se second
#define bg begin
cs int RLEN=1<<20|1;
inline char gc(){
    static char ibuf[RLEN],*ib,*ob;
    (ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
    return (ib==ob)?EOF:*ib++;
}
inline int read(){
    char ch=gc();
    int res=0;bool f=1;
    while(!isdigit(ch))f^=ch=='-',ch=gc();
    while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
    return f?res:-res;
}
template<class tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
template<class tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
cs int N=150005;
namespace Seg{
	cs int N=::N<<2;
	int mx[N],tag[N];
	#define lc (u<<1)
	#define rc ((u<<1)|1)
	#define mid ((l+r)>>1)
	inline void pushup(int u){
		mx[u]=min(mx[lc],mx[rc]);
	}
	inline void pushnow(int u,int k){
		tag[u]+=k,mx[u]+=k;
	}
	inline void pushdown(int u){
		if(!tag[u])return;
		pushnow(lc,tag[u]);
		pushnow(rc,tag[u]);
		tag[u]=0;
	}
	void build(int u,int l,int r){
		if(l==r)return mx[u]=-l,void();
		build(lc,l,mid),build(rc,mid+1,r);
		pushup(u);
	}
	void update(int u,int l,int r,int st,int des,int k){
		if(st>des)return;
		if(st<=l&&r<=des)return pushnow(u,k);
		pushdown(u);
		if(st<=mid)update(lc,l,mid,st,des,k);
		if(mid<des)update(rc,mid+1,r,st,des,k);
		pushup(u);
	}
}
int n,m,h;
int a[N],b[N];
int main(){
	#ifdef Stargazer
	freopen("lx.in","r",stdin);
	#endif
	n=read(),m=read(),h=read();
	for(int i=1;i<=m;i++)b[i]=read();
	Seg::build(1,1,m);
	sort(b+1,b+m+1);
	for(int i=1;i<=n;i++)a[i]=read();
	for(int i=1;i<m;i++){
		int pos=lower_bound(b+1,b+m+1,h-a[i])-b;
		Seg::update(1,1,m,pos,m,1);
	}
	int ret=0;
	for(int i=m;i<=n;i++){
		if(i>m){int pos=lower_bound(b+1,b+m+1,h-a[i-m])-b;Seg::update(1,1,m,pos,m,-1);}
		int pos=lower_bound(b+1,b+m+1,h-a[i])-b;Seg::update(1,1,m,pos,m,1);
		if(Seg::mx[1]>=0)ret++;
	}
	cout<<ret<<'\n';
}
發佈了579 篇原創文章 · 獲贊 193 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章