Luogu1502 窗口的星星 (线段树扫描线)

2022-10-28,,,

将每个点拓展为矩形,将\(y\)离散,延\(x\)轴扫描,每次更新最值

用了一百年的pushdown操作疑似有问题,亦或这道题特殊,我乱改了pushdown位置就过了,我能怎么办,WA了一发,y数组没开够又RE了一发。。。

话说POJ上的情书让我回忆起童年那个彪悍的女孩,一晃十年了

  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.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long //#define ON_DEBUG #ifdef ON_DEBUG #define D_e_Line printf("\n\n----------\n\n")
#define D_e(x) cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin); #else #define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ; #endif struct ios{
template<typename ATP>ios& operator >> (ATP &x){
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
x*= f;
return *this;
}
}io;
using namespace std;
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r const int N = 10007; struct Line{
int X, Y1, Y2, w;
bool operator < (const Line &com) const{
if(X != com.X) return X < com.X;
return w > com.w;
}
}a[N << 1];
struct Tree{
int mx, tag;
}t[N << 3]; inline void Pushup(int rt){
t[rt].mx = Max(t[rt << 1].mx, t[rt << 1 | 1].mx);
}
inline void Pushdown(int rt, int l, int r){
if(!t[rt].tag) return;
t[rt].mx += t[rt].tag;
if(l != r){
t[rt << 1].tag += t[rt].tag;
t[rt << 1 | 1].tag += t[rt].tag;
}
t[rt].tag = 0;
}
inline void Updata(int rt, int l, int r, int L, int R, int w){
if(L <= l && r <= R){
t[rt].tag += w;
// Pushdown(rt, l, r);
return;
}
// Pushdown(rt, l, r);
int mid = (l + r) >> 1;
if(L <= mid) Updata(lson, L, R, w);
if(R > mid) Updata(rson, L, R, w); Pushdown(lson), Pushdown(rson); // QAQ Pushup(rt);
} int y[N << 1];
int main(){
int Tasks;
io >> Tasks;
while(Tasks--){
Fill(t, 0);
int n, W, H;
io >> n >> W >> H;
R(i,1,n){
int X, Y, val;
io >> X >> Y >> val;
a[i] = (Line){X, Y, Y + H - 1, val};
a[i + n] = (Line){X + W - 1, Y, Y + H - 1, -val};
y[i] = Y;
y[i + n] = Y + H - 1;
} n <<= 1;
sort(a + 1, a + n + 1);
sort(y + 1, y + n + 1);
int m = unique(y + 1, y + n + 1) - y - 1; int ans = 0;
R(i,1,n){
int l = lower_bound(y + 1, y + m + 1, a[i].Y1) - y;
int r = lower_bound(y + 1, y + m + 1, a[i].Y2) - y;
Updata(1, 1, m, l, r, a[i].w);
ans = Max(ans, t[1].mx);
} printf("%d\n", ans);
} return 0;
}


破案了,我pushdown傻逼了, 一中午愉悦的聊天后 终于圆满了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long //#define ON_DEBUG #ifdef ON_DEBUG #define D_e_Line printf("\n\n----------\n\n")
#define D_e(x) cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin); #else #define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ; #endif struct ios{
template<typename ATP>ios& operator >> (ATP &x){
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
x*= f;
return *this;
}
}io;
using namespace std;
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r const int N = 10007; struct Line{
int X, Y1, Y2, w;
bool operator < (const Line &com) const{
if(X != com.X) return X < com.X;
return w > com.w;
}
}a[N << 1];
struct Tree{
int mx, tag;
}t[N << 3]; inline void Pushup(int rt){
t[rt].mx = Max(t[rt << 1].mx, t[rt << 1 | 1].mx);
}
inline void Pushdown(int rt, int l, int r){
if(!t[rt].tag) return;
t[rt << 1].mx += t[rt].tag;
t[rt << 1 | 1].mx += t[rt].tag;
t[rt << 1].tag += t[rt].tag;
t[rt << 1 | 1].tag += t[rt].tag;
t[rt].tag = 0;
}
inline void Updata(int rt, int l, int r, int L, int R, int w){
if(L <= l && r <= R){
t[rt].mx += w;
t[rt].tag += w;
return;
}
Pushdown(rt, l, r);
int mid = (l + r) >> 1;
if(L <= mid) Updata(lson, L, R, w);
if(R > mid) Updata(rson, L, R, w);
Pushup(rt);
} int y[N << 1];
int main(){
FileOpen();
int Tasks;
io >> Tasks;
while(Tasks--){
Fill(t, 0);
int n, W, H;
io >> n >> W >> H;
R(i,1,n){
int X, Y, val;
io >> X >> Y >> val;
a[i] = (Line){X, Y, Y + H - 1, val};
a[i + n] = (Line){X + W - 1, Y, Y + H - 1, -val};
y[i] = Y;
y[i + n] = Y + H - 1;
} n <<= 1;
sort(a + 1, a + n + 1);
sort(y + 1, y + n + 1);
int m = unique(y + 1, y + n + 1) - y - 1; int ans = 0;
R(i,1,n){
int l = lower_bound(y + 1, y + m + 1, a[i].Y1) - y;
int r = lower_bound(y + 1, y + m + 1, a[i].Y2) - y;
Updata(1, 1, m, l, r, a[i].w);
ans = Max(ans, t[1].mx);
} printf("%d\n", ans);
} return 0;
}

Luogu1502 窗口星星线段树扫描线)的相关教程结束。

《Luogu1502 窗口的星星 (线段树扫描线).doc》

下载本文的Word格式文档,以方便收藏与打印。