题目
Description
Fleeting time does not blur my memory of you. Can it really be 4 years since I first saw you? I still remember, vividly, on the beautiful Zhuhai Campus, 4 years ago, from the moment I saw you smile, as you were walking out of the classroom and turned your head back, with the soft sunset glow shining on your rosy cheek, I knew, I knew that I was already drunk on you. Then, after several months’ observation and prying, your grace and your wisdom, your attitude to life and your aspiration for future were all strongly impressed on my memory. You were the glamorous and sunny girl whom I always dream of to share the rest of my life with. Alas, actually you were far beyond my wildest dreams and I had no idea about how to bridge that gulf between you and me. So I schemed nothing but to wait, to wait for an appropriate opportunity. Till now — the arrival of graduation, I realize I am such an idiot that one should create the opportunity and seize it instead of just waiting.
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.
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
题目解析
题目背景把我一个单身20年的纯种单身狗看哭了,实在太感人了,比起那些矫揉造作的肥皂剧,这种纯情专一的暗恋好太多了。虽然我们码农不配拥有爱情。。。
然而,感动过后,题还是要做的。
这道题在扫描线里算是经典的例题了。
这里是要移动一个矩形的窗口,求能得到的内部星星权值和的最值,移动矩形其实在代码实现上非常不友好,我们可以换种思考方式,把星星做成矩形,移动的是点,点在矩形内时可以获得矩形的权值,这就好做很多。
既然星星是矩形,还有权值,那么扫描线的想法自然而然就出来了。
把每个星星等价为一个矩形,再等价为两条线段,前后横坐标相差w,权值互为相反数,纵坐标覆盖范围相同。然后开一颗线段树维护最值,每次更新线段覆盖的区间,线段树区间修改用懒标记,这个是前置技能,就不细讲了。然后每次更新后的
t
r
e
e
[
1
]
tree[1]
tree[1]的权值的最大值即为答案。
有个坑点,虽然保证了
x
,
y
<
2
31
x, y<2^{31}
x,y<231,但是我们把
x
x
x加上
w
w
w,
y
y
y加上
h
h
h后,不能保证还是在
i
n
t
int
int内,要开long long(造孽啊。。。)
AC代码:
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
#define ll long long
const int maxn = 2e4 + 10;
int n;
ll w, h;
struct node{
int val, lzy;
} tree[maxn*8];
struct dot{
ll x, y;
int y1, y2;
int val;
bool operator < (const dot &oth) const{
if(x==oth.x) return val < oth.val;
else return x < oth.x;
}
}s[maxn];
ll y[maxn];
void build(int l, int r, int dex)
{
tree[dex].lzy = tree[dex].val = 0;
if(l != r){
int mid = (l+r)>>1;
build(l, mid, dex*2), build(mid+1, r, dex*2+1);
}
}
void update(int l, int r, int dex, int a, int b, int val)
{
if(l >= a && r <= b) tree[dex].lzy += val, tree[dex].val += val;
else if(l > b || r < a) return;
else{
int mid = (l+r)>>1;
tree[dex*2].lzy += tree[dex].lzy, tree[dex*2+1].lzy += tree[dex].lzy;
tree[dex*2].val += tree[dex].lzy, tree[dex*2+1].val += tree[dex].lzy;
tree[dex].lzy = 0;
update(l, mid, dex*2, a, b, val), update(mid+1, r, dex*2+1, a, b, val);
tree[dex].val = max(tree[dex*2].val, tree[dex*2+1].val);
}
}
void solve()
{
for(int i = 1; i <= n; i++){
scanf("%lld%lld%d", &s[i].x, &s[i].y, &s[i].val);
s[i+n].x = s[i].x+w, s[i+n].y = s[i].y, s[i+n].val = -s[i].val;
y[i] = s[i].y, y[i+n] = s[i].y+h;
}
sort(y+1, y+1+2*n);
int m = unique(y+1, y+1+2*n) - y -1;
for(int i = 1; i <= n; i++){
s[i].y1 = lower_bound(y+1, y+m+1, s[i].y) - y;
s[i].y2 = lower_bound(y+1, y+m+1, s[i].y+h) - y;
s[i+n].y1 = s[i].y1, s[i+n].y2 = s[i].y2;
}
sort(s+1, s+1+2*n);
build(1, m, 1);
int dex = 1, res = 0;
for(int i = 1; i <= 2*n; i++){
update(1, m, 1, s[i].y1, s[i].y2-1, s[i].val);
res = max(res, tree[1].val);
}
printf("%d\n", res);
}
int main()
{
while(scanf("%d%lld%lld", &n, &w, &h) != EOF)
solve();
}