题解:P7408 [JOI 2021 Final] ダンジョン 3
考虑单组询问,一些较为明显的观察:如果没有油量的限制,那么每一条边的油,都来自于它对应油费的前缀最小值。
加上油量也差不多,对于一个点
可以求一个前缀和
那么对于每个点,可以用一个区间
现在
而对于
复杂度
#define N 200110
int n, m;
int a[N], b[N], tmp[N], hb[N];
int l_[N], r_[N], u_[N];
struct RMQ
{
pii ST[N][20];
void init(int *a, int op)
{
for(int i = 1; i <= n + 1; i++) ST[i][0] = mkp(a[i] * op, i);
for(int j = 1; j < 20; j++) for(int i = 1; i + (1 << j) - 1 <= n + 1; i++) ST[i][j] = std::min(ST[i][j - 1], ST[i + (1 << (j - 1))][j - 1]);
}
pii query(int l, int r)
{
int b = hb[r - l + 1];
return std::min(ST[l][b], ST[r - (1 << b) + 1][b]);
}
}STA, STB;
struct node { int u, i, op; };
std::vector <node> qvec[N];
int ans[N], pre[N];
int nxt[N], sta[N], top;
int trk[N], trb[N];
struct Lish
{
int sta[N], top;
void ins(int x) { sta[++top] = x; }
void build() { std::sort(sta + 1, sta + 1 + top); top = std::unique(sta + 1, sta + 1 + top) - sta - 1; }
int lower(int x) { return std::lower_bound(sta + 1, sta + 1 + top, x) - sta; }
int upper(int x) { return std::upper_bound(sta + 1, sta + 1 + top, x) - sta; }
}X;
void ins(int x, int k, int b) { for(; x <= X.top; x += x & (-x)) trk[x] += k, trb[x] += b; }
void ins(int l, int r, int k, int b)
{
l = X.lower(l); r = X.upper(r) - 1;
if(l > r) return;
ins(l, k, b); ins(r + 1, -k, -b);
}
int queryk(int x) { int res = 0; for(; x; x -= x & (-x)) res += trk[x]; return res; }
int queryb(int x) { int res = 0; for(; x; x -= x & (-x)) res += trb[x]; return res; }
int query(int x) { return queryk(X.lower(x)) * x + queryb(X.lower(x)); }
void solve()
{
// memset(h, idx = -1, sizeof(h));
n = read(), m = read();
for(int i = 1; i <= n; i++) a[i] = read();
for(int i = 1; i <= n; i++) b[i] = read();
b[n + 1] = 0;
pre[0] = -2000000000ll;
for(int i = 2; i <= n + 1; i++) pre[i] = pre[i - 1] + a[i - 1];
STA.init(a, -1); STB.init(b, 1);
for(int i = 1, l, r, u; i <= m; i++)
{
l = l_[i] = read(), r = r_[i] = read(), u = u_[i] = read();
X.ins(u_[i]);
if(-STA.query(l, r - 1).fi > u) { ans[i] = -1; continue; }
int p = std::lower_bound(pre + 1, pre + 2 + n, pre[r] - u) - pre;
ckmax(p, l); pii tmp = STB.query(p, r);
p = tmp.se;
ans[i] += tmp.fi * (pre[r] - pre[p]);
qvec[l].push_back((node){u, i, 1});
qvec[p].push_back((node){u, i, -1});
}
X.build();
sta[++top] = n + 1;
for(int i = n; i >= 1; i--)
{
auto push = [&](int x, int y, int z, int op) -> void
{
int base = pre[y] - pre[x], c2 = base, c1 = pre[z] - pre[y], lim = pre[z] - pre[x] - 1;
// base + min(c1, U) - max(c2, U)
if(c1 <= c2)
{
ins(1, std::min(c1 - 1, lim), 1 * op, (base - c2) * op);
ins(c1, std::min(c2, lim), 0, (base + c1 - c2) * op);
ins(c2 + 1, lim, -1 * op, (base + c1) * op);
}
else
{
ins(1, std::min(c2 - 1, lim), 1 * op, (base - c2) * op);
ins(c2, std::min(c1, lim), 0, (base) * op);
ins(c1 + 1, lim, -1 * op, (base + c1) * op);
}
};
for(; top && b[sta[top]] > b[i]; push(0, sta[top], nxt[sta[top]], -b[sta[top]]), push(i, sta[top], nxt[sta[top]], b[sta[top]]), top--);
nxt[i] = sta[top]; push(0, i, sta[top], b[i]); sta[++top] = i;
for(node t : qvec[i]) ans[t.i] += query(t.u) * t.op;
}
for(int i = 1; i <= m; i++) print(ans[i], '\n');
}
void init()
{
int n = N - 10;
hb[0] = -1;
for(int i = 1; i <= n; i++) hb[i] = hb[i >> 1] + 1;
}