QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#440163 | #4898. 基础图论练习题 | alpha1022 | 0 | 85ms | 28532kb | C++14 | 3.6kb | 2024-06-13 10:59:29 | 2024-06-13 10:59:29 |
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;
int n, 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;
}
vector<pair<ll, ll>> 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) {
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;
vector<pair<ll, ll>> 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;
}
};
int main() {
scanf("%d%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(e0.size());
int cnt = n % mod;
for (int i = 0; i < e0.size(); ++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 < e0.size()) 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()),
solve(0, e0.size() - 1, {s}), 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);
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 85ms
memory: 28532kb
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:
459587306
result:
wrong answer 1st numbers differ - expected: '359714743', found: '459587306'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Wrong Answer
Test #11:
score: 0
Wrong Answer
time: 0ms
memory: 3792kb
input:
569435269457904707 2 0 490445920091092693 772271583 144842828305643603 609043885
output:
0
result:
wrong answer 1st numbers differ - expected: '884694794', found: '0'
Subtask #4:
score: 0
Skipped
Dependency #3:
0%
Subtask #5:
score: 0
Wrong Answer
Test #31:
score: 0
Wrong Answer
time: 1ms
memory: 3880kb
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:
0
result:
wrong answer 1st numbers differ - expected: '546044429', found: '0'
Subtask #6:
score: 0
Skipped
Dependency #3:
0%
Subtask #7:
score: 0
Skipped
Dependency #3:
0%
Subtask #8:
score: 0
Skipped
Dependency #5:
0%
Subtask #9:
score: 0
Skipped
Dependency #1:
0%