QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#219046 | #6662. 기지 간소화 | Huasushis | 0 | 292ms | 89148kb | C++14 | 4.3kb | 2023-10-18 22:58:16 | 2023-10-18 22:58:17 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define fi first
#define se second
#define mod 1000000007ll
#define t tie
using ll = long long;
int n;
ll ans;
#define N 250010
struct edg {
int v;
ll w;
edg(int v, ll w): v(v), w(w) {}
};
vector <edg> g[N];
set<pair<int, ll> >a[N];
ll f[N];
void dfs(int u, int v, ll co) {
// a[u].emplace(n + 1, 0);
// a[u].emplace(0, 1);
a[u].emplace(u, 1);
f[u] = 1 + (ll)(u - 1) * u / 2 % mod + (ll)(n - u) * (n - u + 1) / 2 % mod;
f[u] %= mod;
for (auto&& tmp : g[u]) {
int i = tmp.v;
ll w = tmp.w;
if (i == v) continue;
dfs(i, u, w);
if (u == 1) continue;
if (a[i].size() > a[u].size()) {
swap(a[i], a[u]);
swap(f[i], f[u]);
}
for (auto j = a[i].begin(); j != a[i].end(); ++j) {
// if (!j -> se) continue;
auto it = a[u].upper_bound(*j);
if (it == a[u].begin()) {
ll cz = it -> fi - 1;
ll clz = j -> fi - 1;
ll ctz = it -> fi - j -> fi - j -> se;
f[u] += clz * (clz + 1) / 2 % mod + ctz * (ctz + 1) / 2 % mod - cz * (cz + 1) / 2 % mod;
f[u] %= mod;
if (f[u] < 0) f[u] += mod;
pair<int, int> ins = *j;
if (ins.fi + ins.se == it -> fi) {
f[u] += -(ll)(ins.se) * (ins.se + 1) / 2 % mod + (ll)(ins.se + it -> se) * (ins.se + it -> se + 1) / 2 % mod;
f[u] %= mod;
if (f[u] < 0) f[u] += mod;
ins.se += it -> se;
a[u].erase(it);
} else {
f[u] += (ll)(ins.se) * (ins.se + 1) / 2 % mod;
f[u] %= mod;
}
a[u].emplace(ins);
continue;
}
auto pt = prev(it);
if (it == a[u].end()) {
ll cz = n + 1 - pt -> fi - pt -> se;
ll clz = j -> fi - pt -> fi - pt -> se;
ll ctz = n + 1 - j -> fi - j -> se;
f[u] += clz * (clz + 1) / 2 % mod + ctz * (ctz + 1) / 2 % mod - cz * (cz + 1) / 2 % mod;
f[u] %= mod;
if (f[u] < 0) f[u] += mod;
pair<int, int> ins = *j;
if (pt -> fi + pt -> se == j -> fi) {
f[u] += -(ll)(pt -> se) * (pt -> se + 1) / 2 % mod + (ll)(pt -> se + j -> se) * (pt -> se + j -> se + 1) / 2 % mod;
f[u] %= mod;
if (f[u] < 0) f[u] += mod;
ins.fi = pt -> fi;
ins.se = pt -> se + j -> se;
a[u].erase(pt);
} else {
f[u] += (ll)(ins.se) * (ins.se + 1) / 2 % mod;
f[u] %= mod;
}
a[u].emplace(ins);
continue;
}
ll cz = it -> fi - pt -> fi - pt -> se;
ll clz = j -> fi - pt -> fi - pt -> se;
ll ctz = it -> fi - j -> fi - j -> se;
f[u] += clz * (clz + 1) / 2 % mod + ctz * (ctz + 1) / 2 % mod - cz * (cz + 1) / 2 % mod;
f[u] %= mod;
if (f[u] < 0) f[u] += mod;
pair<int, int> ins = *j;
if (pt -> fi + pt -> se == j -> fi) {
f[u] += -(ll)(pt -> se) * (pt -> se + 1) / 2 % mod + (ll)(pt -> se + j -> se) * (pt -> se + j -> se + 1) / 2 % mod;
f[u] %= mod;
if (f[u] < 0) f[u] += mod;
ins.fi = pt -> fi;
ins.se = pt -> se + j -> se;
a[u].erase(pt);
} else {
f[u] += (ll)(ins.se) * (ins.se + 1) / 2 % mod;
f[u] %= mod;
}
if (ins.fi + ins.se == it -> fi) {
f[u] += -(ll)(ins.se) * (ins.se + 1) / 2 % mod + (ll)(ins.se + it -> se) * (ins.se + it -> se + 1) / 2 % mod;
f[u] %= mod;
if (f[u] < 0) f[u] += mod;
ins.se += it -> se;
a[u].erase(it);
}
a[u].emplace(ins);
}
}
// if (u == 3) {
// for (auto i : a[u]) {
// cout << i.fi << ' ' << i.se << ';';
// }
// puts("");
// }
if (u == 1) return;
ll zs = (ll)n * (n + 1) / 2 % mod - f[u];
if (zs < 0) zs += mod;
// cout << u << ' ' << zs << endl;
ans += zs * co % mod;
ans %= mod;
}
int maintenance_costs_sum(vector<int> U, vector<int> V, vector<int> W) {
n = U.size() + 1;
for (decltype(U.size()) i = 0; i < U.size(); ++i) {
g[U[i] + 1].emplace_back(V[i] + 1, W[i]);
g[V[i] + 1].emplace_back(U[i] + 1, W[i]);
}
dfs(1, 0, 0);
return ans;
}
//#define localtest
#ifdef localtest
int main() {
int tot = maintenance_costs_sum({0, 2, 2, 0}, {2, 1, 4, 3}, {2, 3, 6, 5});
cout << tot << endl;
return 0;
}
#endif
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 5
Accepted
time: 6ms
memory: 22748kb
input:
2 1 0 1
output:
1
result:
ok single line: '1'
Test #2:
score: -5
Wrong Answer
time: 0ms
memory: 22144kb
input:
299 72 263 662908099 170 230 583287081 181 87 461245480 117 116 41098264 282 218 300936390 238 128 561969086 175 105 305200697 152 28 927649982 211 58 290232523 233 188 304617152 246 74 61325892 283 120 724838352 207 94 123021801 138 241 893659708 171 283 82846115 104 250 142703714 111 63 547249068 ...
output:
149011341
result:
wrong answer 1st lines differ - expected: '93964028', found: '149011341'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Wrong Answer
Test #36:
score: 0
Wrong Answer
time: 110ms
memory: 57488kb
input:
249494 137621 137622 1 198127 198128 1 117358 117359 1 155777 155778 1 112742 112743 1 235668 235669 1 20632 20622 1 41333 41334 1 170699 170692 1 130269 130268 1 137208 137207 1 37791 37767 1 130187 130178 1 89795 89817 1 91408 91359 1 243301 243574 1 22017 22018 1 116466 116467 1 160094 160095 1 4...
output:
443419112
result:
wrong answer 1st lines differ - expected: '714152529', found: '443419112'
Subtask #4:
score: 0
Wrong Answer
Test #48:
score: 0
Wrong Answer
time: 292ms
memory: 89148kb
input:
249876 133760 107716 545485826 57898 35556 542825636 159559 78814 588304799 9037 105623 735470824 242676 240955 258616989 58653 143501 194781066 36591 97835 699376285 95049 123298 35300031 91751 203920 865003045 53908 18382 376452723 211462 200538 638209824 48693 89388 132147210 238496 151742 322726...
output:
834409050
result:
wrong answer 1st lines differ - expected: '497361697', found: '834409050'
Subtask #5:
score: 0
Skipped
Dependency #1:
0%