真的想了蛮久的了还是想不通(
官方题解 做法,为了避免讨论区题解我就不详细写方法了,如果您有哪点不懂我在干啥可以直接问。
怀疑是 std::set::upper_bound 的锅。
#include <set>
#include <cstdio>
#include <algorithm>
const int N = 2e5 + 10;
struct node
{
int u, v, n;
bool operator<(const node& x) const { return u < x.u; }
bool operator>(const node& x) const { return u > x.u; }
bool operator==(const node& x) const { return u == x.u; }
bool operator!=(const node& x) const { return u != x.u; }
}c[N], a[N]; int tc, ta;
std::set<node> s;
int main()
{
int n; scanf("%d", &n);
for (int i = 1; i <= n; ++i)
{
int q; scanf("%d", &q);
if (q == 1) ++tc, scanf("%d%d%d", &c[tc].u, &c[tc].v, &c[tc].n);
else ++ta, scanf("%d%d%d", &a[ta].u, &a[ta].v, &a[ta].n);
}
for (int i = 1; i <= tc; ++i)
{
int t = c[i].u, x = c[i].v;
c[i].u = t + x; c[i].v = t - x;
}
for (int i = 1; i <= ta; ++i)
{
int t = a[i].u, x = a[i].v;
a[i].u = t + x; a[i].v = t - x;
}
auto cmp = [](const node& x, const node& y) { return x.v < y.v; };
std::sort(a + 1, a + ta + 1, cmp);
std::sort(c + 1, c + tc + 1, cmp);
int now = 1, ans = 0;
for (int i = 1; i <= ta; ++i)
{
while (now <= tc && c[now].v <= a[i].v) s.insert(c[now++]);
auto tmp = s.upper_bound(a[i]);
if (tmp == s.begin()) continue;
--tmp;
while (true)
{
auto p = *tmp; s.erase(tmp);
int minx = std::min(a[i].n, p.n);
a[i].n -= minx; p.n -= minx; ans += minx;
if (p.n) s.insert(p); if (!a[i].n) break;
tmp = s.upper_bound(a[i]);
if (tmp == s.begin()) break;
--tmp;
}
}
printf("%d\n", ans); return 0;
}