QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#715888 | #9569. Subway | OIer_kzc | TL | 25ms | 79764kb | C++17 | 4.0kb | 2024-11-06 13:41:57 | 2024-11-06 13:41:57 |
Judging History
answer
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <set>
#include <queue>
#include <algorithm>
#include <numeric>
#define LOG(FMT...) fprintf(stderr, FMT)
#define eb emplace_back
#define em emplace
using namespace std;
typedef long long LL;
constexpr int N = 400005;
constexpr LL INFLL = 0x3f3f3f3f3f3f3f3fll;
int n, m;
int a[N], b[N];
struct Dat {
int x, y; LL d;
Dat() {}
Dat(int _x, int _y, LL _d) : x(_x), y(_y), d(_d) {}
bool operator < (const Dat &t) const {
return d > t.d;
}
};
priority_queue<Dat> q;
struct Subway {
vector<int> sites, we, ord;
vector<LL> dis;
vector<bool> was;
int size() const {
return (int)sites.size();
}
void prework() {
int cnt;
scanf("%d", &cnt);
// sites, we
sites.resize(cnt);
we.resize(cnt - 1);
for (int i = 0; i < cnt; ++i) {
scanf("%d", &sites[i]);
sites[i] -= 1;
if (i < cnt - 1) {
scanf("%d", &we[i]);
}
}
// dis
dis.resize(cnt);
fill(dis.begin(), dis.end(), INFLL);
// ord
ord.resize(cnt);
iota(ord.begin(), ord.end(), 0);
sort(ord.begin(), ord.end(), [&](int x, int y) {
return sites[x] < sites[y];
});
// was
was.resize(cnt);
fill(was.begin(), was.end(), false);
/* for (int x : sites) {
LOG("%d ", x);
}
LOG("\n"); */
}
int rk(int site) const {
int l = 0, r = size(), md;
while (l < r) {
md = l + r >> 1;
if (sites[ord[md]] >= site) {
r = md;
} else {
l = md + 1;
}
}
assert(sites[ord[r]] == site);
return ord[r];
}
int operator[] (int k) const {
assert(0 <= k && k < (int) sites.size());
return sites[k];
}
bool chk(int site) const {
return was[rk(site)];
}
} subw[N];
vector<int> ordA[N];
struct Li {
LL y; int k;
Li() {}
Li(LL _y, int _k) : y(_y), k(_k) {}
LL val(int x) const {
return y + k * (LL)x;
}
};
void updSite(vector<Li> &v, int k, LL y) {
while (v.size() && y <= v.back().y && k <= v.back().k) {
v.pop_back();
}
if (v.size() && k >= v.back().k) {
return;
}
v.eb(y, k);
}
vector<Li> vs[N];
void getSubw(int i, int &k, LL &d) {
vector<int> &t = ordA[i];
while (t.size() && subw[t.back()].chk(i)) {
t.pop_back();
}
if (t.empty()) {
k = -1;
d = -1ll;
return;
}
const vector<Li> &v = vs[i];
assert((int)v.size() > 0);
// LOG("?? %ld\n", v.size());
k = t.back();
int x = a[k];
int l = 0, r = (int)v.size() - 1, md;
while (l < r) {
md = l + r >> 1;
if (v[md].val(x) > v[md + 1].val(x)) {
l = md + 1;
} else {
r = md;
}
}
d = v[r].val(x);
}
LL dis[N];
void updQ(int x, int y, LL d) {
// LOG("upd: %d, %d, %lld\n", x, y, d);
if (subw[x].dis[y] <= d) {
return;
}
subw[x].dis[y] = d;
q.em(x, y, d);
int site = subw[x][y];
if (d < dis[site]) {
dis[site] = d;
}
}
void transi(int site, int k, LL d) {
updSite(vs[site], b[k], d);
LL nd;
getSubw(site, k, nd);
if (k == -1) {
return;
}
if (nd < d) {
LOG("??\n");
while (true);
}
updQ(k, subw[k].rk(site), nd);
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < m; ++i) {
scanf("%d", a + i);
}
for (int i = 0; i < m; ++i) {
scanf("%d", b + i);
}
memset(dis, 0x3f, n << 3);
dis[0] = 0ll;
for (int i = 0; i < m; ++i) {
subw[i].prework();
for (int j = 0; j < subw[i].size(); ++j) {
int site = subw[i][j];
// LOG("%d %d %lld\n", i, j, dis[site]);
q.em(i, j, dis[site]);
subw[i].dis[j] = dis[site];
ordA[site].eb(i);
}
}
for (int i = 0; i < n; ++i) {
vector<int> &t = ordA[i];
sort(t.begin(), t.end(), [&](int x, int y) {
return a[x] > a[y];
});
}
while (q.size()) {
auto [x, y, d] = q.top();
// LOG("O: %d %d %d %lld\n", x, y, subw[x][y], d);
q.pop();
if (d != subw[x].dis[y]) {
continue;
}
subw[x].was[y] = true;
int site = subw[x][y];
transi(site, x, d);
if (y + 1 < subw[x].size()) {
updQ(x, y + 1, d + subw[x].we[y]);
}
}
for (int x = 1; x < n; ++x) {
printf("%lld ", dis[x]);
}
puts("");
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 7ms
memory: 77588kb
input:
6 3 1 5 1 5 5 1 3 1 2 2 3 3 3 5 1 2 1 4 3 3 4 5 4 6
output:
2 5 21 14 18
result:
ok 5 number(s): "2 5 21 14 18"
Test #2:
score: 0
Accepted
time: 8ms
memory: 79696kb
input:
6 3 1 5 1 5 5 1 5 1 2 2 100 3 100 6 1 4 5 1 100 2 4 3 100 5 1 4 2 3 1 5
output:
2 31 43 37 136
result:
ok 5 number(s): "2 31 43 37 136"
Test #3:
score: 0
Accepted
time: 15ms
memory: 79692kb
input:
5 9 278281 70119 511791 834898 25300 63487 609134 236836 394497 835557 287345 579404 879128 636344 306393 569430 152565 47119 2 3 823004250 4 2 1 25427550 3 2 1 15849621 3 2 1 701911826 5 3 5 679672631 3 907176714 2 2 1 817370554 2 2 3 697987914 2 2 4 873900795 2 2 1 814305954 5
output:
817370554 15849621 80811085745 701911826
result:
ok 4 number(s): "817370554 15849621 80811085745 701911826"
Test #4:
score: 0
Accepted
time: 11ms
memory: 79552kb
input:
5 10 436148 103565 528276 212202 680282 92724 609031 560815 80390 406327 546832 581372 731920 348686 791433 98906 112247 118131 361076 724950 4 1 213029090 4 415633732 5 581145231 3 2 4 306227294 2 2 1 713116112 4 2 3 99672714 5 2 3 975143846 1 5 4 249118026 5 689334413 1 597093740 2 553624319 3 3 4...
output:
597093740 765908995 213029090 628662822
result:
ok 4 number(s): "597093740 765908995 213029090 628662822"
Test #5:
score: 0
Accepted
time: 8ms
memory: 79552kb
input:
3 5 696710 837216 390019 431068 960618 589388 829806 692481 154511 282620 2 1 711629163 3 2 1 781784306 3 2 1 686636041 3 2 3 794790206 2 2 1 844212542 2
output:
844212542 686636041
result:
ok 2 number(s): "844212542 686636041"
Test #6:
score: 0
Accepted
time: 11ms
memory: 79560kb
input:
3 8 344877 101098 328614 735002 476606 635558 573861 261083 964379 333960 25186 276560 258996 683650 765559 582374 2 3 838262394 1 2 2 863940316 3 2 2 476918371 3 3 3 320092619 1 400754003 2 3 3 150885055 2 90507792 1 2 3 190275693 2 2 2 600234969 3 2 2 679446528 3
output:
400754003 29224357199
result:
ok 2 number(s): "400754003 29224357199"
Test #7:
score: 0
Accepted
time: 12ms
memory: 79708kb
input:
50 52 895514 29433 851800 887860 340384 95967 506578 666268 850660 602220 717255 242039 34055 752012 248567 170469 505996 823344 143369 390858 112988 892365 15368 617804 351619 557340 788960 990487 283825 272924 24678 130649 341049 980236 558990 254726 682005 963825 953074 603477 706464 340694 23541...
output:
632126151 479346918 492618840 3695787776 22624579200 174047740 416387993526 21429733469 15831777447 203893499 522142321620 977566721 279122223 30345963113 804573598 73397618725 6389037892 224856032596 416404080694 75356833904 69909552850 4504114918 13173874327 1104455938 275720760247 136977429637 22...
result:
ok 49 numbers
Test #8:
score: 0
Accepted
time: 25ms
memory: 79764kb
input:
50 186 909405 536090 349598 989013 812836 176449 79855 101754 179492 328610 751840 298905 429840 327083 222463 770826 222448 679356 524717 759894 186015 464746 390432 205629 893518 619709 777762 985329 612480 308146 507216 337177 463052 10105 150939 411855 75743 831031 391242 914978 198839 259846 36...
output:
279207921 30546650 3955157971 366881738 678883775 822326137 828131153 510756214 2799968412 2039093375 2727811374 2032421076 1966318841 894245748 2686792493 218322703 721713806 962052764 2586140415 201519887 722596289 49603757 329009601 562632121 73396118 399363911 1252345566 631600417 106456928 5055...
result:
ok 49 numbers
Test #9:
score: -100
Time Limit Exceeded
input:
50 746 109522 187240 733057 796323 66411 270785 937748 93610 470338 557856 166396 453860 431444 832088 13928 85925 15012 650718 328138 113957 812853 420621 146202 494017 985809 97315 96915 329400 118469 310530 501365 375790 461794 395440 784193 383084 825278 651991 584960 198686 644465 936326 856317...