QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#624462 | #8649. Escape Route 2 | hhoppitree | 0 | 43ms | 16180kb | C++17 | 4.7kb | 2024-10-09 15:53:39 | 2024-10-09 15:53:40 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int tl[N], tr[N], nxt[N], Vnxt[N], pre[N], Vpre[N];
pair<int, int> nd[N];
int Nxt[N][400], BIGNxt[N][400];
long long VNxt[N][400], BIGVNxt[N][400];
int Pre[N][400], BIGPre[N][400];
long long VPre[N][400], BIGVPre[N][400];
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 sz = sqrt(n) + 1;
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 = 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];
}
}
int q; scanf("%d", &q);
map< pair<int, int>, long long> M;
while (q--) {
int l, r; scanf("%d%d", &l, &r);
if (M.count({l, r})) {
printf("%lld\n", M[{l, r}]);
continue;
}
long long res = 1e18;
int step = r - l - 1;
if (tr[l] - tl[l] < tr[r - 1] - tl[r - 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);
}
} else {
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);
}
}
printf("%lld\n", M[{l, r}] = res);
}
return 0;
}
詳細信息
Subtask #1:
score: 0
Runtime Error
Test #1:
score: 6
Accepted
time: 43ms
memory: 16180kb
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:
595521292 595521292 595521292 595521292 595521292 595521292 595521292 595521292 595521292 595521292 595521292 595521292 595521292 595521292 595521292 595521292 595521292 595521292 595521292 595521292 595521292 595521292 595521292 595521292 595521292 595521292 595521292 595521292 595521292 595521292 ...
result:
ok 300000 lines
Test #2:
score: 0
Runtime Error
input:
1384 702597566 1 93593482 288383752 1 483624997 516514674 1 217174776 378882844 1 381889032 694179867 1 143192510 343368096 1 20552425 654877612 1 34995000 223673833 1 86047336 507288111 1 58193455 564074888 1 543118270 579455813 1 42236607 257802041 1 244371899 634806939 1 173261583 634917538 1 245...
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%