POJ 2482 Stars in Your Window (scan line method, line segment tree interval plus maintenance maximum value, discretization)

POJ 2482 Stars in Your Window

Blue Book Line Segment Tree Exercises
POJ 2482 Stars in Your Window

Meaning: Given nnn stars,wwlongw highhhh frame. Each star is given coordinates(x, y) (x, y)(x,y ) , brightnessccc . Ask the maximum brightness sum in the frame. (The brightness on the border is not recorded)

Data range: n: 1 e 4, w / h: 1 e 6, x / y: 2 31 n: 1e4, w/h:1e6, x/y: 2^{31}n1e4w/h:1e6,x / y:231

Ideas:

  • Problem conversion : The position of the frame that can circle the i-th star constitutes a square, assuming that the coordinates of the star are (x, y) (x,y)(x,y ) , the two boundaries that can be enclosed are(x, y, y + h), (x + w, y, y + h) (x,y,y+h),(x+w,y, y+h)(x,and ,and+h),(x+w,and ,and+h)
  • Consider the boundary problem (the stars on the edges are not included in the current box) : Because the coordinates are integers, all stars are from (x, y) (x, y)(x,y ) becomes(x − 0.5, y − 0.5) (x-0.5,y-0.5)(x0.5,and0 . . 5 ) , the boundary becomes as long as(x, y, y + h(x,and ,and+h1), ( x + w , y , y + h − 1 ) (x+w,y,y+h-1) (x+w,and ,and+h1 ) , then the star enclosed by this frame must not be on the boundary.
  • Boundary contribution : For the left boundary (x, y, y + h − 1) (x,y,y+h-1)(x,and ,and+h1 ) The contribution ofci c_ici, For the right boundary (x + w, y, y + h − 1) (x+w,y,y+h-1)(x+w,and ,and+h1 ) 's contribution is− ci -c_ici
  • Specific algorithm :
  1. First store the (x, y, z, c) (x,y,z,c) of each boundary(x,and ,with ,c ) (z is the upper vertex of each boundary), sort the boundaries in ascending order by x.
  2. For each yyy (the interval of the line segment tree) is discretized (the data range is too large, n is relatively small)
  3. Build a tree, traverse 2 n 2n2 The interval is added to n boundaries, and the interval maximum dat is maintained. The dat of each root node is the maximum brightness of the current boundary position, and the answer is the maximum of all boundary root nodes.

Pay attention to open LL

LL n,w,h;
struct SegmentTree{
    
    
	int l,r;
	LL dat,add;
	#define l(x) tree[x].l
    #define r(x) tree[x].r
    #define dat(x) tree[x].dat
    #define add(x) tree[x].add
}tree[maxn*8];
struct node{
    
    
    LL x,y,z;
    LL c;
    bool operator <(const node aa)const{
    
    
        return x<aa.x||(x==aa.x&&c<aa.c);
    }
}a[maxn*2];
LL  b[maxn*2];

void build(int p,int l,int r){
    
    
	l(p)=l,r(p)=r;
	dat(p)=0;add(p)=0;
	if(l==r) {
    
    return;}
	int mid=(l+r)/2;
	build(p*2,l,mid);
	build(p*2+1,mid+1,r);
}

void lazy(int p){
    
    
	if(add(p)){
    
    
		dat(p*2)+=add(p);
		dat(p*2+1)+=add(p);
		add(p*2)+=add(p);
		add(p*2+1)+=add(p);
		add(p)=0;
	}
}

void change(int p,int l,int r,LL z){
    
    
	if(l<=l(p)&&r>=r(p)){
    
    
		dat(p)+=z;
		add(p)+=z;
		return;
	}
	lazy(p);
	int mid=(l(p)+r(p))/2;
	if(l<=mid) change(p*2,l,r,z);
    if(r>mid) change(p*2+1,l,r,z);
	dat(p)=max(dat(p*2),dat(p*2+1));
}

LL ask(int p,int l,int r)
{
    
    
	if(l<=l(p)&&r>=r(p)) return dat(p);
	lazy(p);
    int mid=(l(p)+r(p))/2;
	LL ans=0;
    if(l<=mid) ans+=ask(p*2,l,r);
    if(r>mid) ans+=ask(p*2+1,l,r);
    return ans;
}

int main(){
    
    
    LL co,ans;
    while(scanf("%lld%lld%lld",&n,&w,&h)!=EOF){
    
    
        ans=0ll;
        for(int i=1;i<=n;i++){
    
    
            scanf("%lld%lld%lld",&a[2*i-1].x,&a[2*i-1].y,&a[i*2-1].c);
            a[2*i].x=a[2*i-1].x+w;
            b[i*2-1]=a[2*i].y=a[2*i-1].y;
            b[2*i]=a[2*i].z=a[2*i-1].z=a[2*i-1].y+h-1;
            a[2*i].c=-1*a[2*i-1].c;
        }
        n*=2;
        sort(b+1,b+1+n);
        co=unique(b+1,b+1+n)-b-1;
        for(int i=1;i<=n;i++){
    
    
            a[i].y=lower_bound(b+1,b+co+1,a[i].y)-b;
            a[i].z=lower_bound(b+1,b+1+co,a[i].z)-b;
        }
        sort(a+1,a+1+n);
        build(1,1,co);
        for(int i=1;i<=n;i++){
    
    
            change(1,a[i].y,a[i].z,a[i].c);
            ans=max(ans,tree[1].dat);
        }
        cout<<ans<<endl;
    }
}

Guess you like

Origin blog.csdn.net/weixin_44986601/article/details/105807204