2013
11-11

# POJ 2482 Stars in Your Window [解题报告] Java

These days, having parted with friends, roommates and classmates one after another, I still cannot believe the fact that after waving hands, these familiar faces will soon vanish from our life and become no more than a memory. I will move out from school tomorrow. And you are planning to fly far far away, to pursue your future and fulfill your dreams. Perhaps we will not meet each other any more if without fate and luck. So tonight, I was wandering around your dormitory building hoping to meet you there by chance. But contradictorily, your appearance must quicken my heartbeat and my clumsy tongue might be not able to belch out a word. I cannot remember how many times I have passed your dormitory building both in Zhuhai and Guangzhou, and each time aspired to see you appear in the balcony or your silhouette that cast on the window. I cannot remember how many times this idea comes to my mind: call her out to have dinner or at least a conversation. But each time, thinking of your excellence and my commonness, the predominance of timidity over courage drove me leave silently.

Graduation, means the end of life in university, the end of these glorious, romantic years. Your lovely smile which is my original incentive to work hard and this unrequited love will be both sealed as a memory in the deep of my heart and my mind. Graduation, also means a start of new life, a footprint on the way to bright prospect. I truly hope you will be happy everyday abroad and everything goes well. Meanwhile, I will try to get out from puerility and become more sophisticated. To pursue my own love and happiness here in reality will be my ideal I never desert.

Farewell, my princess!

If someday, somewhere, we have a chance to gather, even as gray-haired man and woman, at that time, I hope we can be good friends to share this memory proudly to relight the youthful and joyful emotions. If this chance never comes, I wish I were the stars in the sky and twinkling in your window, to bless you far away, as friends, to accompany you every night, sharing the sweet dreams or going through the nightmares together.

Here comes the problem: Assume the sky is a flat plane. All the stars lie on it with a location (x, y). for each star, there is a grade ranging from 1 to 100, representing its brightness, where 100 is the brightest and 1 is the weakest. The window is a rectangle whose edges are parallel to the x-axis or y-axis. Your task is to tell where I should put the window in order to maximize the sum of the brightness of the stars within the window. Note, the stars which are right on the edge of the window does not count. The window can be translated but rotation is not allowed.

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.

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

3 5 4
1 2 3
2 3 2
6 3 1
3 5 4
1 2 3
2 3 2
5 3 1


5
6


import java.util.*;

public class Main{

static long height[];
static long tree[];
static XY p[];
static TreeSet< Long> ts;
static Iterator< Long> iterator;
static int m;
static long sum[];
static long maxSum[];

public static void main(String[] args)throws Exception {

Scanner scan = new Scanner(System.in);

while(scan.hasNext()){
int n = scan.nextInt();
int W = scan.nextInt();
int H = scan.nextInt();
height = new long[2*n];
ts = new TreeSet< Long>();

p = new XY[n << 1];

for(int i=0;i< n;i++){
p[i] = new XY(scan.nextLong(),scan.nextLong(),scan.nextLong());
//x扫描线当遇到p[i].x+W 表示 以p[i].x为左下角的矩形已搜索结束
p[i+n] = new XY(p[i].x+W,p[i].y,-p[i].c);
}
m = ts.size();
iterator = ts.iterator();
tree = new long[m<< 1];
sum = new long[(m<< 1)+2];
maxSum = new long[(m<< 1)+2];

createLineTree(1);

java.util.Arrays.sort(p);

n *= 2;

long ans = 0;
for(int i=0;i< n;i++){             //原理同 x扫描线 相似
insert(p[i].y,p[i].c);
insert(p[i].y+H,-p[i].c);
ans = Math.max(ans, maxSum[1]);
}
System.out.println(ans);
}

}

private static void insert(long y, long c) {

int p = 1;
while(tree[p]!=y){            // 搜索要修改的点
if(y< tree[p])
p <<= 1;
else
p = (p<<1)+1;
}

while(p!=0){//更新 前面找到的修改点以及 它所有祖先点
sum[p] += c;
long t1 = maxSum[2*p]; //每个根结点是左右子树一起更新的结果
//  因为此树是有序的 所以每一棵子树代表一个范围左根右）
long t2 = sum[p]-sum[2*p+1]+maxSum[2*p+1];
// 而这个范围的maxSum 值 就放在根中根中的sum值是其所有子树的总和
maxSum[p] = Math.max(t1, t2);
p /= 2;
}

}

public static void createLineTree(int p) {

if(2*p<=m)  // 建立二叉 排序树
createLineTree(2*p);
tree[p] = iterator.next();
if(2*p+1<=m)
createLineTree(2*p+1);
}

}
class XY implements Comparable< XY>{

long x;
long y;
long c;

public XY(long x, long y, long c) {
super();
this.x = x;
this.y = y;
this.c = c;
}

public int compareTo(XY e) {

if(this.x< e.x)
return -1;
else if(this.x>e.x)
return 1;
else{
if(this.c< e.c)
return -1;
else if(this.c>e.c)
return 1;
return 0;
}

}

}

1. 换句话说，A[k/2-1]不可能大于两数组合并之后的第k小值，所以我们可以将其抛弃。
应该是，不可能小于合并后的第K小值吧