BZOJ 2951: [Poi2001]Goldmine
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int N=15010;
const int M=60010;
const int Lim=30000;
int n,s,w,ans;
struct pp{int x,y;} a[N];
bool cmp(pp u,pp v){return u.x<v.x;}
int Ls[M*3],Rs[M*3],Sum[M*3],Lazy[M*3];
void Build(int x,int l,int r)
{
Ls[x]=l,Rs[x]=r;
if(l==r) return;
int mid=(l+r)>>1;
Build(x<<1,l,mid);
Build(x<<1|1,mid+1,r);
}
void Pushup(int x)
{
Sum[x]=max(Sum[x<<1],Sum[x<<1|1]);
}
void Pushdown(int x)
{
int lc=x<<1,rc=x<<1|1;
Sum[lc]+=Lazy[x];
Lazy[lc]+=Lazy[x];
Sum[rc]+=Lazy[x];
Lazy[rc]+=Lazy[x];
Lazy[x]=0;
}
void Modify(int x,int l,int r,int v)
{
if(Ls[x]==l&&Rs[x]==r)
{
Sum[x]+=v;
Lazy[x]+=v;
return;
}
int mid=(Ls[x]+Rs[x])>>1;
if(Lazy[x]) Pushdown(x);
if(r<=mid) Modify(x<<1,l,r,v);
else if(l>mid) Modify(x<<1|1,l,r,v);
else Modify(x<<1,l,mid,v),Modify(x<<1|1,mid+1,r,v);
Pushup(x);
}
int main()
{
scanf("%d%d%d",&s,&w,&n);
for(int i=1;i<=n;i++)
scanf("%d%d",&a[i].x,&a[i].y);
Build(1,-Lim,Lim);
sort(a+1,a+n+1,cmp);
int ans=0,j=1;
for(int i=1;i<=n;i++)
{
while(a[j].x<a[i].x-s)
{
Modify(1,a[j].y,min(a[j].y+w,30000),-1);
j++;
}
Modify(1,a[i].y,min(a[i].y+w,30000),1);
ans=max(ans,Sum[1]);
}
printf("%d",ans);
}
Description
Input
Output
Sample Input
12
0 0
1 1
2 2
3 3
4 5
5 5
4 2
1 4
0 5
5 0
2 3
3 2