线段树 + 扫描线 路过大佬帮忙看一下 qwq 代码比较短了

P1502 窗口的星星

@[Mount_](/user/519384) 你的pos忘记清空了
by Prean @ 2021-10-01 17:01:11


啊啊啊 蟹蟹 dalao提醒
by Link_Cut_Y @ 2021-10-01 19:00:33


@[Prean](/user/160839) 我清空了还是 RE 啊
by Link_Cut_Y @ 2021-10-01 19:01:54


@[Mount_](/user/519384) build 应该是l==r的时候retun吧
by Prean @ 2021-10-01 19:06:26


@[Prean](/user/160839) 还是RE了 o(╥﹏╥)o
by Link_Cut_Y @ 2021-10-01 20:11:06


只AC了一个点
by Link_Cut_Y @ 2021-10-01 20:11:47


@[Mount_](/user/519384) 草,你是不是忘记lowerbound了
by Prean @ 2021-10-01 20:18:22


@[Prean](/user/160839) 我lower_bound了也没有用啊QAQ ``` #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <vector> #define int long long using namespace std; const int N = 2e4 + 10; typedef long long LL; int n, w, h; vector<int> pos; struct Seg { int l, r, len, lazy; }tr[N << 2]; struct node { int x, y1, y2, f; }p[N]; int cmp(node a, node b) { return a.x < b.x || (a.x == b.x && a.f < 0); } void pushup(int u) { tr[u].len = max(tr[u << 1].len, tr[u << 1 | 1].len) + tr[u].lazy; } int find(int y) { return lower_bound(pos.begin(), pos.end(), y) - pos.begin(); } void build(int u, int l, int r) { tr[u] = {l, r, 0, 0}; if (r - l == 1) return; int mid = (l + r) >> 1; build(u << 1, l, mid); build(u << 1 | 1, mid + 1, r); pushup(u); } void modify(int u, int l, int r, int k) { if (tr[u].l >= l && tr[u].r <= r) { tr[u].lazy += k; tr[u].len += k; return; } if (l < tr[u << 1].r) modify(u << 1, l, min(r, tr[u << 1].r), k); if (r > tr[u << 1 | 1].l) modify(u << 1 | 1, max(l, tr[u << 1 | 1].l), r, k); pushup(u); } signed main() { while (scanf("%lld%lld%lld", &n, &w, &h) != EOF) { int cnt = 0; for (int i = 1; i <= n; i ++ ) { int a, b, c; scanf("%lld%lld%lld", &a, &b, &c); p[cnt] = {a, b, b + h, c}; cnt ++ ; p[cnt] = {a + w, b, b + h, -c}; cnt ++ ; pos.push_back(b); pos.push_back(b + h); } sort(pos.begin(), pos.end()); pos.erase(unique(pos.begin(), pos.end()), pos.end()); sort(p, p + cnt, cmp); build(1, 1, pos.size()); int ans = 0; for (int i = 0; i < cnt; i ++ ) { modify(1, find(p[i].y1), find(p[i].y2), p[i].f); if (p[i].f > 0) ans = max(ans, tr[1].len); } cout << ans << endl; } return 0; } ```
by Link_Cut_Y @ 2021-10-01 21:29:21


@[Prean](/user/160839) 又调了调 发现不用find 错的是一些别的地方 ``` #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <vector> #define int long long using namespace std; const int N = 2e4 + 10; typedef long long LL; int n, w, h; int pos[N]; struct Seg { int l, r, len, lazy; }tr[N << 2]; struct node { int x, y1, y2, f; }p[N]; int cmp(node a, node b) { return a.x < b.x || (a.x == b.x && a.f < 0); } void pushup(int u) { tr[u].len = max(tr[u << 1].len, tr[u << 1 | 1].len) + tr[u].lazy; } void build(int u, int l, int r) { tr[u] = {pos[l], pos[r], 0, 0}; // 错误在这里有一个 if (r - l == 1) return; // 这里没有错 int mid = (l + r) >> 1; build(u << 1, l, mid); build(u << 1 | 1, mid, r); // 这里的 mid 不能加一 pushup(u); } void modify(int u, int l, int r, int k) { if (tr[u].l >= l && tr[u].r <= r) { tr[u].lazy += k; tr[u].len += k; return; } if (l < tr[u << 1].r) modify(u << 1, l, min(r, tr[u << 1].r), k); if (r > tr[u << 1 | 1].l) modify(u << 1 | 1, max(l, tr[u << 1 | 1].l), r, k); pushup(u); } signed main() { while (scanf("%lld%lld%lld", &n, &w, &h) != EOF) { int cnt = 0, num = 1; // 下标从一开始 for (int i = 1; i <= n; i ++ ) { int a, b, c; scanf("%lld%lld%lld", &a, &b, &c); p[cnt] = {a, b, b + h, c}; cnt ++ ; p[cnt] = {a + w, b, b + h, -c}; cnt ++ ; pos[num ++ ] = b; pos[num ++ ] = b + h; } sort(pos + 1, pos + num); // pos.erase(unique(pos.begin(), pos.end()), pos.end()); num = unique(pos + 1, pos + num) - (pos + 1); sort(p, p + cnt, cmp); build(1, 1, num); int ans = 0; for (int i = 0; i < cnt; i ++ ) { modify(1, p[i].y1, p[i].y2, p[i].f); if (p[i].f > 0) ans = max(ans, tr[1].len); } cout << ans << endl; } return 0; } ```
by Link_Cut_Y @ 2021-10-01 22:01:56


@[Prean](/user/160839) 但是只有在别的平台(acwing)上能过,在luogu上ac不了 on luogu [https://www.luogu.com.cn/record/58879428](https://www.luogu.com.cn/record/58879428) on acwing [https://www.acwing.com/problem/content/description/250/](https://www.acwing.com/problem/content/description/250/)
by Link_Cut_Y @ 2021-10-01 22:07:58


| 下一页