Files
python/TangDou/AcWing/SegmentTree/248_1.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

105 lines
2.6 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
using namespace std;
typedef long long LL;
const int N = 10010;
//在对 x 坐标进行升序排序时,将 k 值按降序排序,这样才能处理两个矩形贴合的情况
struct Seg {
LL x, y1, y2, c;
bool operator<(const Seg &t) const {
if (x == t.x) return c > t.c; // 1在前,-1在后,先处理入边再处理出边
return x < t.x;
}
} seg[N << 1];
struct Node {
int l, r;
int maxc, add;
} tr[N << 3];
int n, w, h;
vector<LL> ys;
void pushup(int u) {
tr[u].maxc = max(tr[u << 1].maxc, tr[u << 1 | 1].maxc);
}
void build(int u, int l, int r) {
tr[u] = {l, r, 0, 0};
if (l == r) return;
int mid = (l + r) >> 1;
build(u << 1, l, mid);
build(u << 1 | 1, mid + 1, r);
}
void pushdown(int u) {
if (tr[u].add) {
tr[u << 1].maxc += tr[u].add;
tr[u << 1].add += tr[u].add;
tr[u << 1 | 1].maxc += tr[u].add;
tr[u << 1 | 1].add += tr[u].add;
tr[u].add = 0;
}
}
void modify(int u, int l, int r, int c) {
if (l <= tr[u].l && tr[u].r <= r) {
tr[u].maxc += c;
tr[u].add += c;
return;
}
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1;
if (l <= mid) modify(u << 1, l, r, c);
if (r > mid) modify(u << 1 | 1, l, r, c);
pushup(u);
}
int find(LL x) {
return lower_bound(ys.begin(), ys.end(), x) - ys.begin();
}
int main() {
//加快读入
ios::sync_with_stdio(false), cin.tie(0);
while (cin >> n >> w >> h) {
ys.clear();
for (int i = 1; i <= n; i++) {
LL x, y;
int c;
cin >> x >> y >> c;
//区别在这里!
/*
需配合:
if (x == t.x) return c > t.c; // 1在前,-1在后,先处理入边再处理出边
*/
seg[2 * i - 1] = {x, y, y + h - 1, c};
//此处,x+w-1,注意:多减了一个1~
seg[2 * i] = {x + w - 1, y, y + h - 1, -c};
ys.push_back(y), ys.push_back(y + h - 1);
}
sort(ys.begin(), ys.end());
ys.erase(unique(ys.begin(), ys.end()), ys.end());
sort(seg + 1, seg + 1 + 2 * n);
build(1, 0, ys.size() - 1);
int res = 0;
for (int i = 1; i <= 2 * n; i++) {
res = max(res, tr[1].maxc);
modify(1, find(seg[i].y1), find(seg[i].y2), seg[i].c);
}
printf("%d\n", res);
}
return 0;
}