QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#440184 | #4898. 基础图论练习题 | alpha1022 | 28 | 99ms | 32720kb | C++14 | 4.0kb | 2024-06-13 11:26:18 | 2024-06-13 11:26:18 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll inf = 0x3f3f3f3f3f3f3f3f;
const int mod = 998244353;
void add(int &x, int y) { if ((x += y - mod) < 0) x += mod; }
const int N = 5e4;
ll n;
int m, q;
vector<pair<int, ll>> e0;
vector<vector<ll>> d;
vector<tuple<int, ll, ll>> e1;
int countComponents(ll n, vector<ll> &d) {
int ret = 0;
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pQ;
for (ll i : d) pQ.emplace(i, i); d.clear();
ll tag = 0;
while (!pQ.empty()) {
auto [d0, t] = pQ.top(); pQ.pop(), d.push_back(t), d0 -= tag;
if (!d0) continue; if (n < d0) break;
ll d1 = pQ.empty() ? inf : pQ.top().first - tag;
ll k = min(n, d1) / d0; n -= k * d0, tag += k * d0, pQ.emplace(d0 + tag, t);
if (n < d0) ret = (ret + d0 - n) % mod;
}
return ret = (ret + n) % mod;
}
auto getComponents(ll n, vector<ll> d, vector<ll> s) {
vector<pair<ll, ll>> ret;
for (ll u : s) ret.emplace_back(u, u);
priority_queue<ll, vector<ll>, greater<ll>> pQ(d.begin(), d.end());
ll tag = 0;
while (!pQ.empty()) {
auto d0 = pQ.top(); pQ.pop(), d0 -= tag;
if (!d0) continue; if (n < d0) break;
ll d1 = pQ.empty() ? inf : pQ.top() - tag;
ll k = min(n, d1) / d0; n -= k * d0, tag += k * d0, pQ.push(d0 + tag);
for (int i = 0; i < ret.size(); ++i) {
ll &u = ret[i].first;
if (u >= n + k * d0) continue;
u -= max<ll>(k - 1 - (n + k * d0 - u - 1) / d0, 0) * d0;
if (u >= d0 && u >= n) u -= d0;
}
}
return ret;
}
int ans;
void solve(int l, int r, vector<vector<ll>> vec) {
if (vec.empty()) return ;
if (l == r) {
for (auto s : vec) {
if (s.empty()) continue;
ans = (ans + (ll)(mod - s.size() + 1) * e0[l].first) % mod;
for (int i = 1; i < s.size(); ++i) e1.emplace_back(e0[l].first, s[0], s[i]);
}
return ;
}
int mid = (l + r) >> 1;
vector<vector<ll>> L, R;
for (auto s : vec) {
if (s.empty()) continue;
auto res = getComponents(n, d[mid], s); sort(res.begin(), res.end());
R.emplace_back();
for (int i = 0, j; i < res.size(); i = j + 1) {
L.emplace_back(1, res[i].second), R.back().push_back(res[i].second);
for (j = i; j + 1 < res.size() && res[j + 1].first == res[i].first; ++j)
L.back().push_back(res[j + 1].second);
}
}
solve(l, mid, L), solve(mid + 1, r, R);
}
struct UnionFind {
vector<int> fa;
UnionFind(int n) : fa(n, -1) {}
bool isRoot(int u) { return !~fa[u]; }
int find(int u) { return isRoot(u) ? u : fa[u] = find(fa[u]); }
bool merge(int u, int v) {
int fu = find(u), fv = find(v);
if (fu == fv) return 0;
fa[fv] = fu; return 1;
}
};
// {0, 3}
// 1
// 2
int main() {
scanf("%lld%d%d", &n, &m, &q), e0.resize(m), e1.resize(q);
for (auto &[x, d] : e0) scanf("%lld%d", &d, &x);
sort(e0.begin(), e0.end()), d.resize(m);
int cnt = n % mod;
for (int i = 0; i < m; ++i) {
auto [x, d0] = e0[i];
ans = (ans + (ll)x * cnt) % mod, d[i].push_back(d0),
ans = (ans + (ll)(mod - x) * (cnt = countComponents(n, d[i]))) % mod;
if (i + 1 < m) d[i + 1] = d[i];
}
vector<ll> s;
for (auto &[w, u, v] : e1) scanf("%lld%lld%d", &u, &v, &w), s.push_back(u), s.push_back(v);
sort(s.begin(), s.end()), s.erase(unique(s.begin(), s.end()), s.end());
if (m) {
auto res = getComponents(n, d[m - 1], s); sort(res.begin(), res.end());
vector<vector<ll>> vec;
for (int i = 0, j; i < res.size(); i = j + 1) {
vec.emplace_back(1, res[i].second);
for (j = i; j + 1 < res.size() && res[j + 1].first == res[i].first; ++j)
vec.back().push_back(res[j + 1].second);
}
solve(0, m - 1, vec);
}
sort(e1.begin(), e1.end()); UnionFind uf(s.size());
for (auto [w, u, v] : e1) {
u = lower_bound(s.begin(), s.end(), u) - s.begin(),
v = lower_bound(s.begin(), s.end(), v) - s.begin();
if (uf.merge(u, v)) add(ans, w);
}
printf("%d\n", ans);
}
詳細信息
Subtask #1:
score: 4
Accepted
Test #1:
score: 4
Accepted
time: 99ms
memory: 30264kb
input:
161199 9 46510 147335 540442844 159493 801351455 149342 821625305 128476 843250712 95524 275754315 139315 106523502 93575 680460786 155498 328812257 146020 410466645 79992 141967 50596784 152210 68644 268349216 72549 96959 42994091 93869 27394 945120577 2909 81886 270684270 12735 35026 871917997 974...
output:
359714743
result:
ok 1 number(s): "359714743"
Test #2:
score: 0
Accepted
time: 69ms
memory: 25804kb
input:
168549 9 49402 160577 34610415 114623 670751010 74448 676966248 53782 845469137 130729 375561046 31610 261496571 134601 154875802 136129 905308676 166248 499420220 69637 72676 875637640 160442 125460 1269794 146261 61770 714794725 137610 1291 490170432 162092 81850 488118013 106400 48193 276190368 4...
output:
520439176
result:
ok 1 number(s): "520439176"
Test #3:
score: 0
Accepted
time: 58ms
memory: 21908kb
input:
127164 9 45109 56483 490066497 70966 229077054 87305 993081887 72423 442762798 80262 200507011 101712 162752728 67532 590730535 44956 565466274 124237 429166816 13030 8906 742024040 97259 101468 187678659 13401 4301 143856524 125750 80473 258719294 106155 10339 592121345 120034 92354 50915550 112430...
output:
211463174
result:
ok 1 number(s): "211463174"
Test #4:
score: 0
Accepted
time: 97ms
memory: 32720kb
input:
158784 9 48415 138305 177767002 147417 50196642 85527 776201932 144377 990389932 118355 310906417 145220 218744495 145002 132736644 51947 834751363 139733 839880491 158443 157692 159261414 111518 14927 747973081 37498 66196 69874791 11597 115114 22394413 16704 133459 109302190 112143 46551 813021872...
output:
151875883
result:
ok 1 number(s): "151875883"
Test #5:
score: 0
Accepted
time: 27ms
memory: 5660kb
input:
111371 0 45933 13298 59545 852258097 94111 54245 459369673 40744 23311 644404848 37039 92443 220984611 17374 43165 421794343 57652 57965 470479953 62977 14481 563172671 102144 3471 36594913 46628 43278 11508424 55965 80136 777230453 56962 35374 349098036 34825 27995 339605509 43021 17657 780921827 5...
output:
92500087
result:
ok 1 number(s): "92500087"
Subtask #2:
score: 0
Time Limit Exceeded
Dependency #1:
100%
Accepted
Test #6:
score: 0
Time Limit Exceeded
input:
191116 49595 45279 87483 815631830 153579 433065789 167569 346797140 98560 154881536 170720 13622837 133236 561208103 155537 421316363 140536 514298139 6005 986290017 154400 85233907 166826 351094521 174419 304435906 173900 61174962 112778 693574534 104503 745038995 134920 31228457 117606 662581798 ...
output:
result:
Subtask #3:
score: 6
Accepted
Test #11:
score: 6
Accepted
time: 1ms
memory: 3816kb
input:
569435269457904707 2 0 490445920091092693 772271583 144842828305643603 609043885
output:
884694794
result:
ok 1 number(s): "884694794"
Test #12:
score: 0
Accepted
time: 1ms
memory: 3808kb
input:
946929772456816659 2 0 589193907831915013 196301185 485768367910597533 207014034
output:
790540706
result:
ok 1 number(s): "790540706"
Test #13:
score: 0
Accepted
time: 1ms
memory: 3796kb
input:
693038683299151358 2 0 654733556025919068 724998910 450253521190874799 187460097
output:
122292064
result:
ok 1 number(s): "122292064"
Test #14:
score: 0
Accepted
time: 0ms
memory: 3788kb
input:
572269482188906358 2 0 545978502848607475 331750201 488577730099900109 477584735
output:
429885702
result:
ok 1 number(s): "429885702"
Test #15:
score: 0
Accepted
time: 1ms
memory: 3816kb
input:
984888155303961325 2 0 421568681423492040 823358650 324408005979881943 905919848
output:
551223124
result:
ok 1 number(s): "551223124"
Test #16:
score: 0
Accepted
time: 1ms
memory: 3860kb
input:
968068649251960108 2 0 932666179822285222 303897491 422068063538287737 405622211
output:
516717723
result:
ok 1 number(s): "516717723"
Test #17:
score: 0
Accepted
time: 1ms
memory: 3784kb
input:
973235486287221374 2 0 604729607242747292 566399250 440704799734330948 93237801
output:
772791524
result:
ok 1 number(s): "772791524"
Test #18:
score: 0
Accepted
time: 1ms
memory: 3884kb
input:
980842002786834388 2 0 921076927921054095 989436809 917078581302025088 354268450
output:
387335763
result:
ok 1 number(s): "387335763"
Test #19:
score: 0
Accepted
time: 1ms
memory: 3820kb
input:
584600268153835325 2 0 436736455094118542 788823700 379215887395241676 440751386
output:
178749302
result:
ok 1 number(s): "178749302"
Test #20:
score: 0
Accepted
time: 0ms
memory: 3744kb
input:
984888155303961325 2 0 421568681423492040 823358650 324408005979881943 905919848
output:
551223124
result:
ok 1 number(s): "551223124"
Subtask #4:
score: 18
Accepted
Dependency #3:
100%
Accepted
Test #21:
score: 18
Accepted
time: 57ms
memory: 25564kb
input:
569435269457904707 2 48002 490445920091092693 772271583 144842828305643603 609043885 71626464779726163 20936760728342582 933619218 254533877531926689 561120543297327423 444805145 102181371350776436 64807827761321835 63236550 442490347461393187 274703226312639148 379888813 153103619447430279 56932615...
output:
264605976
result:
ok 1 number(s): "264605976"
Test #22:
score: 0
Accepted
time: 42ms
memory: 26188kb
input:
504260032580621389 2 49361 270823699250720124 30858888 336528726133157676 686676838 411951311400351555 331964440631830249 849153007 395362490592741772 476242043133170955 233632950 183299785979517028 300783233878432816 890373397 101096990546501308 294220083765028120 482548478 30494794811726538 492575...
output:
374280020
result:
ok 1 number(s): "374280020"
Test #23:
score: 0
Accepted
time: 56ms
memory: 25684kb
input:
908984547406193258 2 49006 553146168947167785 921235648 439052241502823206 685482302 602040034167963673 319806281814227523 602421493 889680730869149610 662785644521343266 319100701 855861307841385482 123218837422189032 958395288 789474388061739888 886525713531875881 485625803 506682328285523072 8679...
output:
411258291
result:
ok 1 number(s): "411258291"
Test #24:
score: 0
Accepted
time: 53ms
memory: 24608kb
input:
645595087071518014 2 46364 502798671238091149 130131399 324145073511001064 249141640 529562079328876365 298584769486918994 793053817 104449532477780267 612956797119263913 599706657 537335025824879813 509591120306867932 422994501 3573858933523744 362779176725767538 466503134 355006270273722975 606167...
output:
3939553
result:
ok 1 number(s): "3939553"
Test #25:
score: 0
Accepted
time: 45ms
memory: 25380kb
input:
719221297460377128 2 47571 344669347369453785 109414971 630436210393683647 527587080 494442208767214644 568762326175380228 274970054 57477492106404787 655245493834324395 382109587 22155928017304041 59482593547715744 873161380 187440545343246007 190303544007160534 159038457 506940482480256741 6413591...
output:
70514973
result:
ok 1 number(s): "70514973"
Test #26:
score: 0
Accepted
time: 47ms
memory: 23868kb
input:
895088201401004405 2 45041 276028463639596405 456551182 805436189268999970 73771 21429629043534406 631368195310636941 265763227 662737085246639506 143087521945488388 635548439 377675072184922400 804129509385729008 716798383 350029179084366085 117553290957648227 20894738 122339684090997249 7205501057...
output:
559035760
result:
ok 1 number(s): "559035760"
Test #27:
score: 0
Accepted
time: 65ms
memory: 19516kb
input:
853901589698398947 2 47600 504553695105130092 847799116 138986940711272376 645918409 805868840752605206 78417023376575599 841040724 729516722093143233 218204564225574757 336393903 730920155682863361 695576676583032360 406662382 387801338291686723 202931704070108854 337663750 247673343610665743 24573...
output:
20383713
result:
ok 1 number(s): "20383713"
Test #28:
score: 0
Accepted
time: 53ms
memory: 24692kb
input:
670502957421329993 2 45217 483745611802893710 733999054 668071203574411469 828666336 420971137268931813 366553146611242395 965231439 287424331604163221 506949430264341972 601249827 149442959137057706 557729764807337099 145435283 24271514949210121 655375054865364550 969273095 570625553631888431 32471...
output:
937105664
result:
ok 1 number(s): "937105664"
Test #29:
score: 0
Accepted
time: 51ms
memory: 25884kb
input:
563845195733711553 2 48989 229866420401531786 558292747 545061569237416562 6907852 132266473456293640 75289038464304103 118029834 32731247863057693 356593250668739234 378210865 66114516634328274 92146176364809198 445824305 231042900349456054 325921825145329565 425319020 423843183122176900 5050925061...
output:
651838351
result:
ok 1 number(s): "651838351"
Test #30:
score: 0
Accepted
time: 58ms
memory: 23796kb
input:
804068805652796381 2 45790 713896086745970460 932094415 451836086076043686 180589234 797324209326094324 458554421458651867 300552452 666099368215435761 398615680976044224 517160772 93127604003167259 65073324216076012 364948453 746562120109527370 478791782647716593 625536788 584650813747492507 584407...
output:
91959720
result:
ok 1 number(s): "91959720"
Subtask #5:
score: 0
Time Limit Exceeded
Test #31:
score: 0
Time Limit Exceeded
input:
755526150476311190 942 0 492334667739348527 1 755523898623296976 1 532486636690994793 1 755526150476030559 1 755526150476249097 1 502164090270592200 1 657422656495814703 1 487200614853438190 1 311037325561173142 1 755526150475651155 1 125287404340238660 1 755524914808674090 1 755526150476177007 1 75...
output:
result:
Subtask #6:
score: 0
Skipped
Dependency #3:
100%
Accepted
Dependency #5:
0%
Subtask #7:
score: 0
Skipped
Dependency #3:
100%
Accepted
Dependency #5:
0%
Subtask #8:
score: 0
Skipped
Dependency #5:
0%
Subtask #9:
score: 0
Skipped
Dependency #1:
100%
Accepted
Dependency #2:
0%