# 【BZOJ2138】stone（线段树，Hall定理）

BZOJ

## 题解

$$f[i]=s[i]-TR[i],g[i]=s[i]-TL[i]$$，所以是$$f[R]\ge g[L-1]$$

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define ll long long
#define RG register
#define MAX 40040
#define lson (now<<1)
#define rson (now<<1|1)
{
RG int x=0,t=1;RG char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=-1,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return x*t;
}
int n,m,a[MAX],K[MAX],s[MAX];
struct SegMentTree_Max
{
int t[MAX<<2],tag[MAX<<2];
void puttag(int now,int w){t[now]+=w;tag[now]+=w;}
void pushdown(int now,int l,int r){puttag(lson,tag[now]);puttag(rson,tag[now]);tag[now]=0;}
void Build(int now,int l,int r)
{
if(l==r){t[now]=s[l];return;}
int mid=(l+r)>>1;
Build(lson,l,mid);Build(rson,mid+1,r);
t[now]=max(t[lson],t[rson]);
}
void Modify(int now,int l,int r,int L,int R,int w)
{
if(L<=l&&r<=R){puttag(now,w);return;}
int mid=(l+r)>>1;pushdown(now,l,r);
if(L<=mid)Modify(lson,l,mid,L,R,w);
if(R>mid)Modify(rson,mid+1,r,L,R,w);
t[now]=max(t[lson],t[rson]);
}
int Query(int now,int l,int r,int L,int R)
{
if(L>R)return 0;if(L<=l&&r<=R)return t[now];
int mid=(l+r)>>1,ret=0;pushdown(now,l,r);
if(L<=mid)ret=max(ret,Query(lson,l,mid,L,R));
if(R>mid)ret=max(ret,Query(rson,mid+1,r,L,R));
return ret;
}
}G;
struct SegMentTree_Min
{
int t[MAX<<2],tag[MAX<<2];
void puttag(int now,int w){t[now]+=w;tag[now]+=w;}
void pushdown(int now,int l,int r){puttag(lson,tag[now]);puttag(rson,tag[now]);tag[now]=0;}
void Build(int now,int l,int r)
{
if(l==r){t[now]=s[l];return;}
int mid=(l+r)>>1;
Build(lson,l,mid);Build(rson,mid+1,r);
t[now]=min(t[lson],t[rson]);
}
void Modify(int now,int l,int r,int L,int R,int w)
{
if(L<=l&&r<=R){puttag(now,w);return;}
int mid=(l+r)>>1;pushdown(now,l,r);
if(L<=mid)Modify(lson,l,mid,L,R,w);
if(R>mid)Modify(rson,mid+1,r,L,R,w);
t[now]=min(t[lson],t[rson]);
}
int Query(int now,int l,int r,int L,int R)
{
if(L>R)return 0;if(L<=l&&r<=R)return t[now];
int mid=(l+r)>>1,ret=1e9;pushdown(now,l,r);
if(L<=mid)ret=min(ret,Query(lson,l,mid,L,R));
if(R>mid)ret=min(ret,Query(rson,mid+1,r,L,R));
return ret;
}
}F;
int main()
{
freopen("stone.in","r",stdin);
freopen("stone.out","w",stdout);
for(int i=1;i<=n;++i)s[i]=a[i]=(1ll*(i-X)*(i-X)%P+1ll*(i-Y)*(i-Y)%P+1ll*(i-Z)*(i-Z)%P)%P;
for(int i=1;i<=n;++i)s[i]+=s[i-1];
for(int i=3;i<=m;++i)K[i]=(1ll*K[i-1]*X%P+1ll*K[i-2]*Y%P+Z)%P;
if(!m)return 0;G.Build(1,1,n);F.Build(1,1,n);
for(int i=1;i<=m;++i)
{