QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#789095 | #114. Construction of Highway | _8_8_# | 0 | 3ms | 9980kb | C++23 | 3.5kb | 2024-11-27 19:15:29 | 2024-11-27 19:15:29 |
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = (int)1e5 + 12, MOD = (int)1e9 + 7;
int B = 18, p[N];
int n, c[N], sum = 0, up[N][18];
void make() {
vector<int> cl;
for(int i = 1; i <= n; i++) {
cl.push_back(c[i]);
}
sort(cl.begin(), cl.end());
cl.resize(unique(cl.begin(), cl.end()) - cl.begin());
for(int i = 1; i <= n; i++) {
c[i] = lower_bound(cl.begin(), cl.end(), c[i]) - cl.begin() + 1;
}
}
int t[N], a[N], b[N], tin[N], tout[N];
vector<int> g[N];
pair<int, int> s[N * 4];
void upd(int pos, pair<int, int> val, int v = 1, int tl = 1, int tr = n) {
if(tl == tr) {
s[v] = val;
} else {
int tm = (tl + tr) >> 1;
if(pos <= tm) upd(pos, val, v + v, tl, tm);
else upd(pos, val, v + v + 1, tm + 1, tr);
s[v] = max(s[v + v], s[v + v + 1]);
}
}
pair<int, int> get1(int l, int r, int v = 1, int tl = 1, int tr = n) {
if(l > r || tl > r || l > tr) return {0, 0};
if(tl >= l && tr <= r) return s[v];
int tm = (tl + tr) >> 1;
return max(get1(l, r, v + v, tl, tm), get1(l, r, v + v + 1, tm + 1, tr));
}
void add(int pos, int val) {
while(pos <= n) {
t[pos] += val;
pos += pos & -pos;
}
}
int get(int i) {
int ret = 0;
while(i) {
ret += t[i];
i -= i & -i;
}
return ret;
}
int get(int l, int r) {
return get(r) - get(l - 1);
}
void edge(int u, int v) {
up[v][0] = u;
g[u].push_back(v);
for(int i = 1; i < B; i++) {
up[v][i] = up[up[v][i - 1]][i - 1];
}
}
int timer = 0;
void dfs(int v) {
tin[v] = ++timer;
for(int to : g[v]) {
dfs(to);
}
tout[v] =timer;
}
bool is(int v, int u) {
return (tin[v] <= tin[u] && tout[v] >= tout[u]);
}
int f(int v, int vv) {
if(p[v] == v || !is(vv, p[v])) return v;
return p[v] = f(p[v], vv);
}
bool vis[N];
void test() {
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> c[i];
p[i] = i;
}
make();
for(int i = 1; i <= n - 1; i++) {
cin >> a[i] >> b[i];
edge(a[i], b[i]);
}
dfs(1);
upd(tin[1],{0, c[1]});
for(int i = 1; i <= n - 1; i++) {
vector<pair<int, int>> x;
int v = a[i];
vis[b[i]] = 1;
vector<pair<int, int>> e;
e.emplace_back(a[i], b[i]);
while(v) {
int col = 1;
int val = f(v, v);
for(int i = B - 1; i >= 0; i--) {
int nv = up[v][i];
if(nv && f(nv, nv) == val) {
v = nv;
col += (1 << i);
}
}
// cout << v << ' ' << f(v) << '\n';
if(up[v][0]) e.emplace_back(up[v][0], v);
x.push_back({c[val], col});
v = up[v][0];
}
for(auto [j, t]: e) {
p[f(j, j)] = t;
}
// cout << f(2) << "x\n";
reverse(x.begin(), x.end());
sum += (int)x.size();assert(sum <= 20 * n);
ll res = 0;
int m = (int)x.size();
for(auto [j, k] : x) {
res += get(j + 1, n) * 1ll * k;
add(j, k);
}
for(auto [j, k] : x) {
add(j, -k);
}
cout << res << '\n';
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
while(t--)
test();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 7
Accepted
time: 3ms
memory: 9908kb
input:
2 804289384 846930887 1 2
output:
0
result:
ok single line: '0'
Test #2:
score: 7
Accepted
time: 2ms
memory: 7868kb
input:
10 505335291 738766720 190686789 260874576 747983062 906156499 502820865 142559278 261608746 380759628 1 3 1 5 5 7 3 8 1 4 3 10 7 6 5 9 5 2
output:
0 0 0 1 0 1 0 0 0
result:
ok 9 lines
Test #3:
score: 7
Accepted
time: 2ms
memory: 9980kb
input:
100 205554747 483147986 844158169 953350441 612121426 310914941 210224073 856883377 922860802 495649265 8614859 989089925 378651394 344681740 29100603 816952842 21468265 552076976 87517202 953369896 374612516 787097143 126313439 207815259 287632274 886964648 220723886 119448938 444268469 865680799 6...
output:
0 0 0 0 0 0 0 0 0 1 0 0 2 0 0 0 0 0 2 0 3 4 3 0 0 1 2 1 0 2 0 2 0 5 6 1 1 2 0 0 1 1 0 1 0 2 4 0 4 1 2 2 3 0 2 0 0 0 8 0 2 2 0 1 3 2 8 2 0 0 2 0 0 1 0 4 2 0 3 0 2 6 3 0 1 1 0 0 4 6 1 1 0 0 1 6 1 2 0
result:
ok 99 lines
Test #4:
score: 0
Wrong Answer
time: 0ms
memory: 7752kb
input:
300 968078302 287724084 410622275 558519327 460165364 773440538 901520026 404622364 417397029 665131386 88500545 246243955 225558715 439197965 991031404 638538415 465622903 21944942 554535402 204144150 501551718 340552605 608463969 970964280 749109574 736758719 557300323 501093883 605082721 41831082...
output:
0 0 0 0 0 0 0 4 0 0 2 0 0 0 0 0 0 1 0 0 0 5 0 0 1 0 0 1 5 4 2 7 5 0 0 1 0 0 3 2 2 2 5 0 0 0 0 0 3 6 5 3 0 4 3 4 2 4 0 6 0 0 0 0 6 3 0 3 4 4 7 1 6 3 0 7 3 0 2 8 3 0 0 0 2 4 0 11 2 6 4 7 9 4 0 8 3 3 12 2 6 6 4 4 0 5 3 5 3 8 4 4 0 3 0 7 0 0 0 12 0 0 8 0 0 0 4 5 16 4 0 2 12 0 11 4 0 3 5 4 3 6 19 4 3 2 0...
result:
wrong answer 112th lines differ - expected: '0', found: '4'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #1:
0%