Stars in Your Windows POJ2482 (segment array scanning line, the maximum interval, the interval update)

Stem that:
There are many stars (viewed as plane rectangular coordinate system) in a sky, the known coordinates and luminance of each star (are integers) seeking a width w, height h of the rectangle (w, h is an integer ) the sum of the brightness of the stars can be circled is the largest number (stars on the rectangular boundary is not).
Input

There are several test cases in the input. The first line of each case contains 3 integers: n, W, H, indicating the number of stars, the horizontal length and the vertical height of the rectangle-shaped window. Then n lines follow, with 3 integers each: x, y, c, telling the location (x, y) and the brightness of each star. No two stars are on the same point.

There are at least 1 and at most 10000 stars in the sky. 1<=W,H<=1000000, 0<=x,y<2^31.
Output

For each test case, output the maximum brightness in a single line.
Sample Input

3 5 4
1 2 3
2 3 2
6 3 1
3 5 4
1 2 3
2 3 2
5 3 1
Sample Output

. 5
. 6
Solution:
Because a fixed size rectangle, the rectangle may be a vertex of its position uniquely determined by its arbitrary. We can consider the upper right corner of the rectangle into what vertex position, the sum of the maximum brightness of the stars circled.

For a star (x, y, C), wherein x, y coordinates, c is the luminance, "rectangle can be encircled that star in the upper right corner of the range of the apex" as shown in FIG. This range is also a rectangle, the bottom left vertex (x, y), the top right vertex (x + w, y + h). For the avoidance of doubt, in the following discussion, we have to replace this range with the word "region."

Here Insert Picture Description
Into question, there are several areas on ** plane, each region having a weight value, find the area on which the weight and maximum coordinates overlap. ** wherein each region is generated by a star, the star equal to the luminance weight value, the upper right corner of the original rectangle problems in this area, which can be encircled few stars.
Here Insert Picture Description
After conversion problem, we use the scan line algorithm, taken around the boundary of each region, stored into two four-tuple (x, y, y + h -1, c) , and (x + w, y, y + h -1, -c). These four-tuple (a first dimension value) sorted abscissa.

About the same time ordinate values ​​to establish a tree line, the maximum maintenance intervals, initially all zeros. We can think of a segment tree "y" of the representative element interval [y, y + 1], and the interval [y, y + h-1] can be expressed as the segment tree y, y + 1, y + 2, ..., y + h-1 these values. Thus, the maintenance segment tree is the sequence of a plurality of numerical values.

Individually scanning each of the four-tuple (x, y1, y2, c), the line segment tree modification executing section (available delay flag implemented), the [y1, y2] Each value C are added, then the root node with the value dat The answer can be updated.

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int u=20010;
struct rec{unsigned int x,l,r; int c;}a[u];
struct{int p,l,r,dat,add;}t[u*4];
unsigned int b[u],c[u],x,y,w,h;
int n,m,p,i,ans,z;

bool cmp(rec a,rec b)
{
	return a.x<b.x||a.x==b.x&&a.c<b.c;
}

void build(int p,int l,int r)
{
	t[p].l=l,t[p].r=r,t[p].dat=t[p].add=0;
	if(l==r) return;
	int mid=(l+r)>>1;
	build(p*2,l,mid);
	build(p*2+1,mid+1,r);
}

void spread(int p)
{
	if(t[p].add)
	{
		t[p*2].dat+=t[p].add,t[p*2].add+=t[p].add;
		t[p*2+1].dat+=t[p].add,t[p*2+1].add+=t[p].add;
		t[p].add=0;
	}
}

void change(int p,int l,int r,int c)
{
	if(l<=t[p].l&&r>=t[p].r)
	{
		t[p].dat+=c,t[p].add+=c;
		return;
	}
	spread(p);
	int mid=(t[p].l+t[p].r)>>1;
	if(l<=mid) change(p*2,l,r,c);
	if(r>mid) change(p*2+1,l,r,c);
	t[p].dat=max(t[p*2].dat,t[p*2+1].dat);
}

int main()
{
	while(cin>>n>>w>>h)
	{
		for(m=p=0,i=1;i<=n;i++)
		{
			scanf("%u%u%d",&x,&y,&z);
			a[i].l=a[n+i].l=y,a[i].r=a[n+i].r=y+h-1;
			a[i].x=x,a[n+i].x=x+w,a[i].c=z,a[n+i].c=-z;
			b[++m]=y,b[++m]=y+h-1;
		}
		sort(b+1,b+m+1);
		for(i=1;i<=m;i++)
			if(i==1||b[i]!=b[i-1]) c[++p]=b[i];
		for(n*=2,i=1;i<=n;i++)
		{
			a[i].l=lower_bound(c+1,c+p+1,a[i].l)-c;
			a[i].r=lower_bound(c+1,c+p+1,a[i].r)-c;
		}
		sort(a+1,a+n+1,cmp);
		build(1,1,p);
		for(ans=0,i=1;i<=n;i++)
		{
			change(1,a[i].l,a[i].r,a[i].c);
			ans=max(ans,t[1].dat);
		}
		cout<<ans<<endl;
	}
	return 0;
}
Published 26 original articles · won praise 0 · Views 708

Guess you like

Origin blog.csdn.net/hwdn3000/article/details/104917459