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

102 lines
2.4 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;
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].add += tr[u].add;
tr[u << 1].maxc += tr[u].add;
tr[u << 1 | 1].add += tr[u].add;
tr[u << 1 | 1].maxc = tr[u << 1 | 1].maxc + 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() {
//加快读入
cin.tie(0), ios::sync_with_stdio(false);
while (cin >> n >> w >> h) {
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};
seg[2 * i] = {x + w, 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;
}