本文主要是介绍1037 Stars in Your Window 线段树 离散化 扫描线变式(需要push_down),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
链接:https://ac.nowcoder.com/acm/problem/51112
来源:牛客网
题目描述
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.
输入描述:
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<231.1 \leq W,H \leq 1000000, 0 \leq x,y<2^{31}.1≤W,H≤1000000,0≤x,y<231.
输出描述:
For each test case, output the maximum brightness in a single line.
示例1
输入
复制
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
分析
在一段区间内有无数个点,每个点有不同权值,求用一个W * H的矩形区域(不包含边框)包裹住这个空间任意位置所能得到的最大的权值
为什么用扫描线算法?
扫描线算法用来解决多个矩形,求这个矩形整体围成的区间面积,
这里把每个点看成矩形,求的是一个矩形能包裹住的最大的权值。
某种程度上比较相似吧,只不过扫描线维护的是被覆盖的线的长度,这题维护的是矩形内的最大权值。
将每个点看成是(x,y) 到(x+W,y+H)的矩形区间,左边的线段就是x,y,y+H。右边的线段是x+W,y,y+H。
为什么要push_down?
扫描线算法要求的是线的长度,得到的是当前扫描到的线被覆盖的长度,两者是一样的,直接push_up传递到上层即可,tr[1].len 就是被覆盖的线段的总长度。没必要push_down去修改某个特定位置的值。它算的是确实是一个区间的值,但是这个值增加了,所有区间就会整体增加,这个值减少了,所有区间就会整体减少,它只有当cnt = 0,或者1的时候才会增减。且是固定的,反正就是一个区间不会影响到别的区间
这里把每个点看成矩形,求的是一个矩形能包裹住的最大的权值,也就是说push_up要往树的上层区间返回的是 矩形区间内权值的最大值。
但是,push_up只能更改当前的矩形的部分权值。当删除一个点的时候,没办法把当前点的权值传递到别的节点,这样最后计算最大值push_up的时候也会受到影响。
换句话说,一个矩形修改的是一个区间的值,它没有特殊性,不是固定的,可能会影响到别的区间,所以得push_down实现区间修改。保证每个位置的值正确。
为什么不包含边框,只用将W和H减1?
另外:
由于数据量是1e9太大了,所以要离散化到一个vector,然后二分找到对应的位置。
//-------------------------代码----------------------------
#define int ll
const int N = 1e6+10;
int n,W,H,m,idx;
vector<int>v;
struct segment {
int x,y1,y2;
int k;
bool operator<(const segment &w) const {
if(x == w.x) return k > w.k;
return x < w.x;
};
} seg[2 * N];
struct node {
int l,r,sum,lz;
} tr[N<<2];
int find(double x) {
return lower_bound(v.begin(), v.end(), x) - v.begin();
}
void upt(int u,int lz) {
tr[u].lz += lz;
tr[u].sum += lz;
}
void push_up(int u) {
tr[u].sum = max(tr[ul].sum,tr[ur].sum);
}
void build(int u,int l,int r) {
tr[u] = {l,r,0,0};
if(l == r) rt;
build(ul,l,tr_mid);build(ur,tr_mid+1,r);
push_up(u);
}
void push_down(int u) {
upt(ul,tr[u].lz);
upt(ur,tr[u].lz);
tr[u].lz = 0;
}
void modify(int u,int l,int r,int k) {
if(l<=tr[u].l && tr[u].r<=r) {
upt(u,k);rt;
}
push_down(u);
if(l <= tr_mid) modify(ul,l,r,k);
if(tr_mid < r) modify(ur,l,r,k);
push_up(u);
}
int find(int x) {
return lower_bound(v.begin(), v.end(), x) - v.begin();
}
void solve()
{
cin>>W>>H;W--,H--;
int tot = 0;
fo(i,1,n) {
int x,y,c;cin>>x>>y>>c;
seg[++tot] = {x,y,y+H,c};
seg[++tot] = {x+W,y,y+H,-c};
v.pb(y);v.pb(y+H);
}
sort(v.begin(),v.end());
sort(seg+1,seg+1+tot);
v.erase(unique(all(v)),v.end());
build(1,1,v.size());
int ans = 0;
fo(i,1,tot) {
modify(1,find(seg[i].y1),find(seg[i].y2),seg[i].k);
ans = max(ans,tr[1].sum);
}
cout<<ans<<endl;
}
void main_init() {}
signed main(){
AC();clapping();TLE;
cout<<fixed<<setprecision(12);
main_init();
while(cin>>n)
// while(cin>>n>>m,n,m)
// int t;cin>>t;while(t -- )
solve();
// {solve(); }
return 0;
}
/*样例区
*/
//------------------------------------------------------------
这篇关于1037 Stars in Your Window 线段树 离散化 扫描线变式(需要push_down)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!