QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#624612 | #8649. Escape Route 2 | hhoppitree | 0 | 0ms | 0kb | C++17 | 5.7kb | 2024-10-09 16:14:46 | 2024-10-09 16:14:48 |
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5, Q = 3e5 + 5;
int tl[N], tr[N], nxt[N], Vnxt[N], pre[N], Vpre[N];
pair<int, int> nd[N];
int *Nxt[N], *BIGNxt[N];
long long *VNxt[N], *BIGVNxt[N];
int *Pre[N], *BIGPre[N];
long long *VPre[N], *BIGVPre[N];
int id[Q];
long long ans[Q];
signed main() {
int n, T, tot = 0; scanf("%d%d", &n, &T);
for (int i = 1, x; i < n; ++i) {
scanf("%d", &x);
tl[i] = tot + 1, tr[i] = tot + x;
for (int j = 1; j <= x; ++j) {
++tot;
scanf("%d%d", &nd[tot].first, &nd[tot].second);
}
}
for (int i = 1; i + 1 < n; ++i) {
vector< pair< pair<int, int>, int> > o;
pair<int, int> mn = {T + 1, 0};
for (int j = tl[i + 1]; j <= tr[i + 1]; ++j) {
o.push_back({nd[j], j});
mn = min(mn, {nd[j].second, j});
}
sort(o.begin(), o.end());
vector< pair<int, int> > to;
for (int j = tl[i]; j <= tr[i]; ++j) {
to.push_back({nd[j].second, j});
}
sort(to.begin(), to.end());
for (int i = (int)to.size() - 1, j = (int)o.size() - 1, tv = T + 1, wh = 0; ~i; --i) {
while (~j && o[j].first.first >= to[i].first) {
if (o[j].first.second < tv) {
tv = o[j].first.second, wh = o[j].second;
}
--j;
}
if (tv <= T) nxt[to[i].second] = wh, Vnxt[to[i].second] = tv - to[i].first;
else nxt[to[i].second] = mn.second, Vnxt[to[i].second] = T - to[i].first + mn.first;
}
}
for (int i = 1; i + 1 < n; ++i) {
vector< pair< pair<int, int>, int> > o;
pair<int, int> mx = {-1, 0};
for (int j = tl[i]; j <= tr[i]; ++j) {
o.push_back({{nd[j].second, nd[j].first}, j});
mx = max(mx, {nd[j].first, j});
}
sort(o.rbegin(), o.rend());
vector< pair<int, int> > to;
for (int j = tl[i + 1]; j <= tr[i + 1]; ++j) {
to.push_back({nd[j].first, j});
}
sort(to.rbegin(), to.rend());
for (int i = (int)to.size() - 1, j = (int)o.size() - 1, tv = -1, wh = 0; ~i; --i) {
while (~j && o[j].first.first <= to[i].first) {
if (o[j].first.second > tv) {
tv = o[j].first.second, wh = o[j].second;
}
--j;
}
if (tv >= 0) pre[to[i].second] = wh, Vpre[to[i].second] = to[i].first - tv;
else pre[to[i].second] = mx.second, Vpre[to[i].second] = to[i].first + T - mx.first;
}
}
int q; scanf("%d", &q);
map< pair<int, int>, int> M;
vector< tuple<int, int, int> > qL, qR;
for (int i = 1; i <= q; ++i) {
int l, r; scanf("%d%d", &l, &r);
if (M.count({l, r})) {
id[i] = M[{l, r}];
continue;
}
id[i] = M[{l, r}] = i;
if (tr[l] - tl[l] < tr[r - 1] - tl[r - 1]) {
qL.push_back({l, r, i});
} else {
qR.push_back({l, r, i});
}
}
int sz = sqrt(n) + 1;
for (int i = 1; i <= tot; ++i) {
Nxt[i] = (int*)malloc(sizeof(int) * 320);
BIGNxt[i] = (int*)malloc(sizeof(int) * 320);
VNxt[i] = (long long*)malloc(sizeof(long long) * 320);
BIGVNxt[i] = (long long*)malloc(sizeof(long long) * 320);
}
for (int i = 1; i <= tot; ++i) {
Nxt[i][0] = i;
for (int j = 1; j <= sz; ++j) {
Nxt[i][j] = nxt[Nxt[i][j - 1]];
VNxt[i][j] = VNxt[i][j - 1] + Vnxt[Nxt[i][j - 1]];
}
BIGNxt[i][0] = i, BIGNxt[i][1] = Nxt[i][sz], BIGVNxt[i][1] = VNxt[i][sz];
}
for (int i = 1; i <= tot; ++i) {
for (int j = 2; j <= sz; ++j) {
BIGNxt[i][j] = BIGNxt[BIGNxt[i][j - 1]][1];
BIGVNxt[i][j] = BIGVNxt[i][j - 1] + BIGVNxt[BIGNxt[i][j - 1]][1];
}
}
for (int i = 1; i <= tot; ++i) {
delete(Nxt[i]);
delete(BIGNxt[i]);
delete(VNxt[i]);
delete(BIGVNxt[i]);
}
for (auto [l, r, tid] : qL) {
long long res = 1e18;
int step = r - l - 1;
for (int i = tl[l]; i <= tr[l]; ++i) {
long long S = nd[i].second - nd[i].first;
int now = i;
S += VNxt[now][step % sz] + BIGVNxt[Nxt[now][step % sz]][step / sz];
res = min(res, S);
}
ans[tid] = res;
}
for (int i = 1; i <= tot; ++i) {
Pre[i] = (int*)malloc(sizeof(int) * 320);
BIGPre[i] = (int*)malloc(sizeof(int) * 320);
VPre[i] = (long long*)malloc(sizeof(long long) * 320);
BIGVPre[i] = (long long*)malloc(sizeof(long long) * 320);
}
for (int i = tot; i >= 1; --i) {
Pre[i][0] = i;
for (int j = 1; j <= sz; ++j) {
Pre[i][j] = pre[Pre[i][j - 1]];
VPre[i][j] = VPre[i][j - 1] + Vpre[Pre[i][j - 1]];
}
BIGPre[i][0] = i, BIGPre[i][1] = Pre[i][sz], BIGVPre[i][1] = VPre[i][sz];
}
for (int i = tot; i >= 1; --i) {
for (int j = 2; j <= sz; ++j) {
BIGPre[i][j] = BIGPre[BIGPre[i][j - 1]][1];
BIGVPre[i][j] = BIGVPre[i][j - 1] + BIGVPre[BIGPre[i][j - 1]][1];
}
}
for (auto [l, r, tid] : qR) {
long long res = 1e18;
int step = r - l - 1;
for (int i = tl[r - 1]; i <= tr[r - 1]; ++i) {
long long S = nd[i].second - nd[i].first;
int now = i;
S += VPre[now][step % sz] + BIGVPre[Pre[now][step % sz]][step / sz];
res = min(res, S);
}
ans[tid] = res;
}
for (int i = 1; i <= q; ++i) {
printf("%lld\n", ans[id[i]]);
}
return 0;
}
詳細信息
Subtask #1:
score: 0
Runtime Error
Test #1:
score: 0
Runtime Error
input:
2 1000000000 1 359893566 955414858 300000 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 ...
output:
result:
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #1:
0%
Subtask #4:
score: 0
Skipped
Dependency #1:
0%
Subtask #5:
score: 0
Runtime Error
Test #39:
score: 0
Runtime Error
input:
301 1000000000 300 863578477 865166395 261293731 262628986 290161866 292035987 31029640 32135494 288138979 289416854 321254857 322352244 163393949 166291828 897880953 899050317 840019366 842900569 100947276 102350870 520716771 522094941 820182602 822928836 766708508 769688128 727827782 728874133 740...
output:
result:
Subtask #6:
score: 0
Skipped
Dependency #1:
0%