【Line segment tree】【Scan line】Petrozavodsk Winter Training Camp 2018 Day 5: Grand Prix of Korea, Sunday, February 4, 2018 Problem A. Donut

The meaning of the question: There are n points on the plane, each point has a positive or negative weight, let you put a square box with an inner side length of 2l and an outer side length of 2r on the plane, and ask you to circle the maximum What is the sum of weights?

It is easy to deduce that the range that can be framed to the center of the box at each point is also a box of the same size centered on that point.

So, split the box of each point into 4 lines. Sweep from bottom to top, the bottom line, add the weight of the point to the [R, R] interval, then subtract [L, L] from the top, and then add to [L, L] on the top , and then subtract it from [R, R]. Each time you scan a line, try to update the answer with the global maximum of the segment tree.

Two pit points: First, since the discrete points are stored in the line segment tree, there may be some dead ends, so insert another point between every two points after the discretization, so that there will be no dead ends.

Secondly, the line corresponding to the outgoing edge should be moved up by one unit, and the left and right endpoints should be reduced by one unit inward, which is convenient for processing.

(UPDATE: Although AC answered this question, it was HACKed. It is suspected that "the line corresponding to the outgoing edge should be moved up by one unit, and the left and right endpoints should be reduced by one unit inward." There is a BUG here)

#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
struct data{
	int p;
	ll v;
}t[1200005];
int a[1200005];
bool cmp(const data &a,const data &b){
	return a.v<b.v;
}
struct LINE{
	int y;
	int l, r;
	int w;
	bool in;
}lines[400005];
bool cm2(const LINE &a,const LINE &b){
	return a.y!=b.y ? a.y<b.y : a.in>b.in;
}
int n, L, R, e, zy;
int y[100005],z[100005],years;
int maxv[4800005],delta[4800005];
void pushdown(int rt){
	if(delta[rt]){
		delta[rt<<1]+=delta[rt];
		delta[rt<<1|1]+=delta[rt];
		maxv[rt<<1]+=delta[rt];
		maxv[rt<<1|1]+=delta[rt];
		delta[rt]=0;
	}
}
void update (int ql, int qr, int v, int rt, int l, int r) {
	if(ql<=l && r<=qr){
		maxv[rt]+=v;
		delta [rt] + = v;
		return;
	}
	int m=(l+r>>1);
	pushdown(rt);
	if(ql<=m){
		update(ql,qr,v,rt<<1,l,m);
	}
	if(m<qr){
		update(ql,qr,v,rt<<1|1,m+1,r);
	}
	maxv[rt]=max(maxv[rt<<1],maxv[rt<<1|1]);
}
int main(){
//	freopen("a.in","r",stdin);
	int x;
	scanf("%d%d%d",&n,&L,&R);
	for(int i=1;i<=n;++i){
		scanf("%d%d%d",&x,&y[i],&z[i]);
		t[++e].v=(ll)(x-R)*2ll;
		t[e].p=e;
		t[++e].v=(ll)(x-R)*2ll-1ll;
		t[e].p=e;
		t[++e].v=(ll)(x-R)*2ll+1ll;
		t[e].p=e;
		
		t[++e].v=(ll)(x-L+1)*2ll;
		t[e].p=e;
		t[++e].v=(ll)(x-L+1)*2ll-1ll;
		t[e].p=e;
		t[++e].v=(ll)(x-L+1)*2ll+1ll;
		t[e].p=e;
		
		t[++e].v=(ll)(x+L-1)*2ll;
		t[e].p=e;
		t[++e].v=(ll)(x+L-1)*2ll-1ll;
		t[e].p=e;
		t[++e].v=(ll)(x+L-1)*2ll+1ll;
		t[e].p=e;
		
		t[++e].v=(ll)(x+R)*2;
		t[e].p=e;
		t[++e].v=(ll)(x+R)*2-1;
		t[e].p=e;
		t[++e].v=(ll)(x+R)*2+1;
		t[e].p=e;
	}
	sort(t+1,t+e+1,cmp);
	a [t [1] .p] = ++ zy;
	for(int i=2;i<=e;++i){
		if(t[i].v!=t[i-1].v){
			++zy;
		}
		a [t [i] .p] = zy;
	}
	for(int i=1;i<=n;++i){
		lines[i*4-3].y=y[i]-R;
		lines[i*4-2].y=y[i]-L+1;
		lines[i*4-1].y=y[i]+L;
		lines[i*4-0].y=y[i]+R+1;
		
		lines[i*4-3].l=a[i*12-11];
		lines[i*4-2].l=a[i*12-8];
		lines[i*4-1].l=a[i*12-8];
		lines[i*4-0].l=a[i*12-11];
		
		lines[i*4-3].r=a[i*12-2];
		lines[i*4-2].r=a[i*12-5];
		lines[i*4-1].r=a[i*12-5];
		lines[i*4-0].r=a[i*12-2];
		
		lines [i * 4-3] .w = z [i];
		lines [i * 4-2] .w = z [i];
		lines [i * 4-1] .w = z [i];
		lines [i * 4-0] .w = z [i];
		
		lines[i*4-3].in=1;
		lines[i*4-2].in=0;
		lines[i*4-1].in=1;
		lines[i*4-0].in=0;
	}
	sort(lines+1,lines+(n<<2|1),cm2);
	for(int i=1;i<=(n<<2);++i){
		if(lines[i].in){
			update(lines[i].l,lines[i].r,lines[i].w,1,1,zy);
		}
		else{
			update(lines[i].l,lines[i].r,-lines[i].w,1,1,zy);
		}
		if(lines[i].y!=lines[i+1].y){
			ans=max(ans,maxv[1]);
		}
	}
	printf("%d\n",ans);
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325148166&siteId=291194637