QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#715398 | #9569. Subway | OIer_kzc# | WA | 13ms | 79648kb | C++17 | 4.0kb | 2024-11-06 11:49:17 | 2024-11-06 11:49:21 |
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) {
if (v.size() && k >= v.back().k) {
return;
}
while (v.size() && y <= v.back().y && k <= v.back().k) {
assert(y == v.back().y);
v.pop_back();
}
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);
getSubw(site, k, d);
if (k == -1) {
return;
}
updQ(k, subw[k].rk(site), d);
}
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];
});
reverse(t.begin(), t.end());
}
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;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 13ms
memory: 79648kb
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: 11ms
memory: 79540kb
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: -100
Wrong Answer
time: 7ms
memory: 79544kb
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 162075978395 701911826
result:
wrong answer 3rd numbers differ - expected: '80811085745', found: '162075978395'