QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#733083 | #9568. Left Shifting 3 | ucup-team5008# | RE | 0ms | 0kb | C++23 | 2.7kb | 2024-11-10 17:08:22 | 2024-11-10 17:08:24 |
answer
#include<bits/stdc++.h>
#define rep2(i,j,k) for(ll i = ll(j); i < ll(k); i++)
#define rep(i,k) rep2(i,0,k)
#define rrep2(i,j,k) for(ll i = ll(j)-1; i >= ll(k); i--)
#define rrep(i,k) rrep2(i,k,0)
#define all(a) a.begin(),a.end()
#define SZ(a) ll(a.size())
#define eb emplace_back
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vp = vector<P>;
using vvp = vector<vp>;
const ll inf = LLONG_MAX/4;
bool chmin(auto& a, auto b) { return a > b ? a = b, 1 : 0;}
bool chmax(auto& a, auto b) {return a < b ? a = b, 1 : 0;}
ll int_ceil(ll a, ll b) {
assert(a > 0 and b > 0);
return (a+b-1)/b;
}
int main(){
cin.tie(0) -> sync_with_stdio(0);
int n, k;
cin >> n >> k;
vl a(k), b(k);
rep(i, k) cin >> a[i];
rep(i, k) cin >> b[i];
vvl x(k), w(k);
vl len(k);
vvl dist(k);
vector<vector<bool>> seen(k);
using T = tuple<ll, int, int>;
priority_queue<T, vector<T>, greater<>> pq;
vvp ord(n);
rep(i, k) {
cin >> len[i];
x[i].resize(len[i]);
w[i].resize(len[i]-1);
rep(j, len[i]-1) cin >> x[i][j] >> w[i][j];
cin >> x[i].back();
dist[i].assign(len[i], inf);
seen[i].assign(len[i], false);
rep(j, len[i]) if(x[i][j] == 0) {
dist[i][j] = 0;
pq.emplace(0, i, j);
}
rep(j, len[i]) {
ord[x[i][j]].eb(i, j);
}
}
rep(i, n) sort(all(ord[i]), [&](P p, P q) { return a[p.first] < a[q.first]; });
vl iter(n);
vvp lines(n);
// px + q
auto add_line = [&](int i, ll p, ll q) {
vp &ls = lines[i];
assert(ls.empty() or ls.back().second <= q);
if(!ls.empty() and ls.back().first <= p) return;
if(!ls.empty() and ls.back().second == q) ls.pop_back();
while(SZ(ls) >= 2) {
auto [p1, q1] = ls[SZ(ls)-2];
auto [p2, q2] = ls[SZ(ls)-1];
ll x1 = int_ceil(q2-q1, p1-p2);
ll x2 = int_ceil(q-q2, p2-p);
if(x1 >= x2) ls.pop_back();
else break;
}
ls.eb(p, q);
while(iter[i] < SZ(ord[i])) {
auto[ni, nj] = ord[i][iter[i]];
if(seen[ni][nj]) ++iter[i];
else break;
}
if(iter[i] == SZ(ord[i])) return;
auto [ni, nj] = ord[i][iter[i]];
int ok = 0, ng = SZ(ls);
auto f = [&](int now) {
auto [p1, q1] = ls[now-1];
auto [p2, q2] = ls[now];
ll x = int_ceil(q2-q1, p1-p2);
return a[ni] >= x;
};
while(ng-ok > 1) {
int mid = (ng+ok)/2;
if(f(mid)) ok = mid;
else ng = mid;
}
tie(p, q) = ls[ok];
ll nd = p * a[ni] + q;
if(chmin(dist[ni][nj], nd)) pq.emplace(dist[ni][nj], nj, nj);
};
vl ans(n, inf);
while(!pq.empty()) {
auto [d, i, j] = pq.top();
pq.pop();
if(seen[i][j]) continue;
seen[i][j] = true;
int p = x[i][j];
chmin(ans[p], d);
if(j < len[i]-1) {
if(chmin(dist[i][j+1], d + w[i][j])) pq.emplace(dist[i][j+1], i, j+1);
}
add_line(p, b[i], d);
}
rep2(i, 1, n) cout << ans[i] << " \n"[i == n-1];
}
详细
Test #1:
score: 0
Runtime Error
input:
4 21 10 jingicpcnanjingsuanan 21 0 jingicpcnanjingsuanan 21 3 nanjingnanjingnanjing 4 100 icpc