QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#410650 | #6662. 기지 간소화 | nguyentunglam | 0 | 764ms | 61164kb | C++17 | 2.7kb | 2024-05-14 10:49:21 | 2024-05-14 10:49:21 |
Judging History
answer
#include<bits/stdc++.h>
#define fi first
#define se second
#define endl "\n"
#define ii pair<int, int>
using namespace std;
const int N = 3e5 + 10, mod = 1e9 + 7;
int st[N], ed[N], rev_st[N], sz[N], cost[N];
int pref[2][N << 2], suff[2][N << 2], g[N << 2], sum[N << 2];
vector<pair<int, int> > adj[N];
int timer, n;
long long ans;
void up(int s, int l, int r, int pos, int val) {
if (l == r) {
pref[0][s] = pref[1][s] = suff[0][s] = suff[1][s] = 0;
pref[val][s] = suff[val][s] = 1;
sum[s] = 1;
g[s] = val;
return;
}
int mid = l + r >> 1;
if (pos <= mid) up(s << 1, l, mid, pos, val);
else up(s << 1 | 1, mid + 1, r, pos, val);
g[s] = g[s << 1] + g[s << 1 | 1];
sum[s] = (sum[s << 1] + sum[s << 1 | 1]) % mod;
for(int j = 0; j < 2; j++) {
pref[j][s] = pref[j][s << 1];
if (g[s << 1] == (mid - l + 1) * j) pref[j][s] += pref[j][s << 1 | 1];
suff[j][s] = suff[j][s << 1 | 1];
if (g[s << 1 | 1] == (r - mid) * j) suff[j][s] += suff[j][s << 1];
sum[s] = (sum[s] + 1LL * suff[j][s << 1] * pref[j][s << 1 | 1] % mod) % mod;
}
}
void prep (int u, int p) {
// cout << u << endl;
st[u] = ++timer;
rev_st[timer] = u;
sz[u] = 1;
for(auto &[v, c] : adj[u]) if (v != p) {
cost[v] = c;
prep(v, u);
sz[u] += sz[v];
}
ed[u] = timer;
}
void dfs(int u, int p) {
int heavy = -1;
cout << u << endl;
for(auto &[v, w] : adj[u]) if (v != p && (heavy < 0 || sz[v] > sz[heavy])) heavy = v;
for(auto &[v, w] : adj[u]) if (v != p && v != heavy) {
dfs(v, u);
for(int j = st[v]; j <= ed[v]; j++) {
up(1, 0, n - 1, rev_st[j], 0);
}
}
if (heavy >= 0) dfs(heavy, u);
up(1, 0, n - 1, u, 1);
for(auto &[v, w] : adj[u]) if (v != p && v != heavy) {
for(int j = st[v]; j <= ed[v]; j++) {
up(1, 0, n - 1, rev_st[j], 1);
}
}
int ways = (1LL * n * (n + 1) / 2 % mod - sum[1] + mod) % mod * cost[u] % mod;
ans += ways;
ans %= mod;
}
int maintenance_costs_sum (vector<int> U, vector<int> V, vector<int> W) {
n = U.size() + 1;
for(int i = 0; i < n; i++) up(1, 0, n - 1, i, 0);
for(int i = 0; i < n - 1; i++) {
adj[U[i]].emplace_back(V[i], W[i]);
adj[V[i]].emplace_back(U[i], W[i]);
}
prep(0, 0);
dfs(0, 0);
// exit(0);
return ans;
}
#ifdef ngu
int main() {
freopen ("task.inp", "r", stdin);
freopen ("task.out", "w", stdout);
int n; cin >> n;
vector<int> u(n), v(n), w(n);
for(int i = 1; i < n; i++) u[i] = i - 1, v[i] = i, w[i] = i;
cout << maintenance_costs_sum(u, v, w);
// int tot = maintenance_costs_sum({0, 2, 2, 0}, {2, 1, 4, 3}, {2, 1, 1, 1});
// cout << tot << endl;
}
#endif // ngu
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 23216kb
input:
2 1 0 1
output:
Unauthorized output
result:
wrong answer 1st lines differ - expected: '1', found: 'Unauthorized output'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Wrong Answer
Test #36:
score: 0
Wrong Answer
time: 764ms
memory: 44276kb
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:
Unauthorized output
result:
wrong answer 1st lines differ - expected: '714152529', found: 'Unauthorized output'
Subtask #4:
score: 0
Wrong Answer
Test #48:
score: 0
Wrong Answer
time: 436ms
memory: 61164kb
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:
Unauthorized output
result:
wrong answer 1st lines differ - expected: '497361697', found: 'Unauthorized output'
Subtask #5:
score: 0
Skipped
Dependency #1:
0%