不知道为什么数组开到2000才能过
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<cmath>
#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MAX 2010
using namespace std;
int cy[MAX*2];
class line
{
public:
int x,y1,y2,flag;
line(){}
line(int a,int b,int c,int d):x(a),y1(b),y2(c),flag(d)
{}
bool operator < (const line b)const
{
if(x!=b.x)
return x<b.x;
return flag>b.flag;
}
}lines[MAX*2];
struct node
{
int l,r,cover,max;
}a[MAX*3];
void build(int t,int l,int r)
{
a[t].l=l;
a[t].r=r;
a[t].cover=0;
a[t].max=0;
if(l==r)return;
int mid=(l+r)>>1;
build(L(t),l,mid);
build(R(t),mid+1,r);
}
void update(int t,line s)
{
if(s.y1<=cy[a[t].l]&&cy[a[t].r]<=s.y2)
{
a[t].cover+=s.flag;
a[t].max+=s.flag;
return;
}
if(a[t].l==a[t].r)return;
a[L(t)].cover+=a[t].cover;
a[L(t)].max+=a[t].cover;
a[R(t)].cover+=a[t].cover;
a[R(t)].max+=a[t].cover;
a[t].cover=0;
int mid=(a[t].l+a[t].r)>>1;
if(s.y1<=cy[mid])
update(L(t),s);
if(s.y2>cy[mid])
update(R(t),s);
a[t].max=max(a[L(t)].max,a[R(t)].max);
}
int main()
{
int m,n,i,x,y;
int numx,numy,numl;
int ans;
while(scanf("%d%d",&n,&m)!=EOF)
{
numx=0;
numy=0;
numl=0;
ans=0;
while(n--)
{
scanf("%d%d",&x,&y);
cy[numy++]=y;
cy[numy++]=y+m;
lines[numl++]=line(x,y,y+m,1);
lines[numl++]=line(x+m,y,y+m,-1);
}
sort(lines,lines+numl);
sort(cy,cy+numy);
numy=unique(cy,cy+numy)-cy;
build(1,0,numy-1);
for(i=0;i<numl;i++)
{
update(1,lines[i]);
ans=max(ans,a[1].max);
}
printf("%d\n",ans);
}
}