QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#715839 | #9569. Subway | OIer_kzc | WA | 14ms | 77692kb | C++17 | 4.1kb | 2024-11-06 13:32:32 | 2024-11-06 13:32:38 |
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) {
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);
LL nd;
getSubw(site, k, nd);
if (k == -1) {
return;
}
if (nd < d) {
LOG("??\n");
while (true);
}
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];
});
}
LL last = 0ll;
while (q.size()) {
auto [x, y, d] = q.top();
last = d;
// 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: 0
Wrong Answer
time: 14ms
memory: 77692kb
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 3 9 13
result:
wrong answer 3rd numbers differ - expected: '21', found: '3'