两个天线两个天线两个天线两个天线两个天线……
lnk。
zght 和 kubic 神仙极力推荐(?)的题,只能说属实 NB……现在满脑子都是天线天线天线……应该能算是一道数据结构超级好题,没啥科技但就是特 NB。
闲话:在写这篇解题报告的时候这个图全程踩点我 BGM,感觉莫名上头。
写完才发现动图是四个天线,能把两和四搞混我大概已经魔怔了。
update:图挂了
……
……
……
……
……
……
……
……
……
……
……
……
……
……
……
……
……
……
……
……
……
……
……
……
……
……
……
……
……
……
……
思路大概就是:
首先绝对值显然可以先算一次不带绝对值的,再把所有高度取反再算一次不带绝对值的取个最大值(这里已经需要离线了,于是后面就不考虑在线算法了)。
我们下面就考虑一次不带绝对值的计算,不妨计算的是
然后,发现这个双向通信,如果对于给定的 一下亿下 vincent163)。
于是我们显然由于我太菜了而不显然地想到扫描线。也就是说,枚举右端点。至于左端点
我们接下来考虑怎么统计这个最大值。
我们把这个
梳理一下我们要维护的东西:
-
(标记)区间对某数取最大值;
-
(值)单点修改;
-
区间查询和加标记的最大值。
到这里就很显然可以用一棵线段树维护了,一操作打懒标记二三操作直接做就可以了。
然后就是实际上线段树要维护和,懒标记,和加标记三个玩意……事实上这个标记顾名思义全部都是以懒标记的形式存在的,贡献在和加标记里更新计算也是用的懒标记。然后这玩意目测就细节贼多,事实也确实是这样,调了我好久……
献上缝缝补补凑出来的代码,马蜂用的 Clang-Format 不知道啥格式懒得调:
#include <bits/stdc++.h>
using namespace std;
constexpr int INF = 1e9, N = 2e5 + 9;
int n, m, h[N], a[N], b[N];
vector<int> del[N << 1], add[N << 1];
vector<pair<int, int>> qry[N];
int ans[N];
int zkw[N << 2], tag[N << 2], rval[N << 2], M = 1;
void updtag(int x, int tg) {
zkw[x] = max(zkw[x], rval[x] + (tag[x] = max(tag[x], tg)));
}
void pushup(int x) {
if (x >= M) return;
zkw[x] = max(zkw[x << 1], zkw[x << 1 | 1]),
rval[x] = max(rval[x << 1], rval[x << 1 | 1]);
}
void pushdown(int x) {
if (x >= M || tag[x] == -INF) return;
updtag(x << 1, tag[x]), updtag(x << 1 | 1, tag[x]);
tag[x] = -INF;
}
void pushtags(int x) {
for (int i = 1, sz = M; i < M; i = i << 1 | !!(x & (sz >>= 1))) pushdown(i);
}
void update(int x, int val) {
pushtags(x += M), rval[x] = val, tag[x] = -INF;
while (x >>= 1) pushup(x);
}
void update(int l, int r, int val) {
for (pushtags(l += M - 1), pushtags(r += M + 1); l ^ r ^ 1;
pushup(l >>= 1), pushup(r >>= 1)) {
if (~l & 1) updtag(l ^ 1, val);
if (r & 1) updtag(r ^ 1, val);
}
while (l >>= 1) pushup(l);
}
int query(int l, int r) {
int ret = -INF;
for (pushtags(l += M - 1), pushtags(r += M + 1); l ^ r ^ 1;
l >>= 1, r >>= 1) {
if (~l & 1) ret = max(ret, zkw[l ^ 1]);
if (r & 1) ret = max(ret, zkw[r ^ 1]);
}
return ret;
}
void solve() {
fill(rval + 1, rval + (M << 1), -INF);
fill(zkw + 1, zkw + (M << 1), -INF);
fill(tag + 1, tag + (M << 1), -INF);
for (int i = 1; i <= n; ++i) {
for (int x : add[i]) update(x, -h[x]);
if (i > a[i]) update(max(1, i - b[i]), i - a[i], h[i]);
for (auto [l, id] : qry[i]) ans[id] = max(ans[id], query(l, i));
for (int x : del[i]) update(x, -INF);
}
}
signed main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
cin >> n;
while (M <= n) M <<= 1;
for (int i = 1; i <= n; ++i) {
cin >> h[i] >> a[i] >> b[i];
if (i + a[i] <= n) add[i + a[i]].push_back(i);
if (i + b[i] < n) del[i + b[i]].push_back(i);
}
cin >> m;
for (int l, r, i = 1; i <= m; ++i) cin >> l >> r, qry[r].emplace_back(l, i);
memset(ans + 1, -1, sizeof(int) * m);
solve();
for (int i = 1; i <= n; ++i) h[i] = -h[i];
solve();
for (int i = 1; i <= m; ++i) cout << ans[i] << '\n';
return 0;
}