#include <bits/stdc++.h>
#define task "C"
#define all(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (l); i <= (r); ++i)
#define Rep(i, r, l) for (int i = (r); i >= (l); --i)
#define DB(X) { cerr << #X << " = " << (X) << '\n'; }
#define DB1(A, _) { cerr << #A << "[" << _ << "] = " << (A[_]) << '\n'; }
#define DB2(A, _, __) { cerr << #A << "[" << _ << "][" << __ << "] = " << (A[_][__]) << '\n'; }
#define DB3(A, _, __, ___) { cerr << #A << "[" << _ << "][" << __ << "][" << ___ << "] = " << (A[_][__][___]) << '\n'; }
#define PR(A, l, r) { cerr << '\n'; rep(_, l, r) DB1(A, _); cerr << '\n';}
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back
#define pf push_front
#define F first
#define S second
#define by(x) [](const auto& a, const auto& b) { return a.x < b.x; } // sort(arr, arr + N, by(a));
#define next ___next
#define prev ___prev
#define y1 ___y1
#define left ___left
#define right ___right
#define y0 ___y0
#define div ___div
#define j0 ___j0
#define jn ___jn
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using namespace std;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vl;
const int N = 1e5 + 2;
int n, a[N], head[N], sz[N], dep[N], bit[N], par[N], cur = 0;
vi g[N], com;
vii edge, rollback;
deque<ii> seg[N];
void upd(int p, int v)
{
for (; p <= n; p += p & -p) bit[p] += v;
}
int query(int p)
{
int res = 0;
for (; p; p -= p & -p) res += bit[p];
return res;
}
void dfs(int u, int p)
{
par[u] = p;
dep[u] = dep[p] + 1;
sz[u] = 1;
for (int v : g[u]) if (v != p)
{
dfs(v, u);
sz[u] += sz[v];
}
}
void hld(int u, int p, int h)
{
head[u] = h;
int mx = 0, big = -1;
for (int v : g[u]) if (v != p && mx < sz[v]) mx = sz[v], big = v;
if (big != -1) hld(big, u, h);
for (int v : g[u]) if (v != p && v != big) hld(v, u, v);
}
void solve(int id, int col, int len)
{
if (seg[id].empty())
{
seg[id].push_front({col, len});
return;
}
vii tem;
int num = 0;
while (1)
{
ii it = seg[id].front();
seg[id].pop_front();
num += it.S;
if (num >= len || seg[id].empty())
{
if (num - len > 0) seg[id].push_front({it.F, num - len});
tem.eb(it.F, it.S - max(0, (num - len)));
seg[id].push_front({col, len});
break;
}
tem.eb(it);
}
Rep(i, SZ(tem) - 1, 0)
{
//cerr << tem[i].F << ' ' << tem[i].S << '\n';
rollback.eb(tem[i]);
upd(tem[i].F, tem[i].S);
cur += tem[i].S * query(tem[i].F - 1);
}
}
int main()
{
#ifdef HynDuf
freopen(task".in", "r", stdin);
//freopen(task".out", "w", stdout);
#else
ios_base::sync_with_stdio(false); cin.tie(nullptr);
#endif
cin >> n;
rep(i, 1, n) cin >> a[i], com.eb(a[i]);
sort(all(com));
com.erase(unique(all(com)), com.end());
rep(i, 1, n) a[i] = lower_bound(all(com), a[i]) - com.begin() + 1;
rep(i, 2, n)
{
int u, v;
cin >> u >> v;
g[u].eb(v);
g[v].eb(u);
edge.eb(u, v);
}
dfs(1, 0);
hld(1, 0, 1);
seg[1].eb(a[1], 1);
for (ii E : edge)
{
int u = E.S;
while (u)
{
solve(head[u], a[E.S], dep[u] - dep[head[u]] + 1);
u = par[head[u]];
}
cout << cur << '\n';
//for (ii v : seg[1]) cout << '(' << v.F << ", " << v.S << "), ";
//cout << '\n';
for (ii v : rollback) upd(v.F, -v.S);
cur = 0;
rollback.clear();
rep(i, 1, n) assert(bit[i] == 0);
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
70008 KB |
Output is correct |
2 |
Correct |
54 ms |
70008 KB |
Output is correct |
3 |
Correct |
59 ms |
70136 KB |
Output is correct |
4 |
Correct |
58 ms |
70140 KB |
Output is correct |
5 |
Correct |
60 ms |
70264 KB |
Output is correct |
6 |
Correct |
60 ms |
70136 KB |
Output is correct |
7 |
Correct |
59 ms |
70136 KB |
Output is correct |
8 |
Correct |
56 ms |
70272 KB |
Output is correct |
9 |
Correct |
55 ms |
70136 KB |
Output is correct |
10 |
Correct |
55 ms |
70136 KB |
Output is correct |
11 |
Correct |
58 ms |
70392 KB |
Output is correct |
12 |
Correct |
60 ms |
70268 KB |
Output is correct |
13 |
Correct |
61 ms |
70244 KB |
Output is correct |
14 |
Correct |
59 ms |
70272 KB |
Output is correct |
15 |
Correct |
60 ms |
70136 KB |
Output is correct |
16 |
Correct |
59 ms |
70136 KB |
Output is correct |
17 |
Correct |
60 ms |
70136 KB |
Output is correct |
18 |
Correct |
59 ms |
70136 KB |
Output is correct |
19 |
Correct |
60 ms |
70280 KB |
Output is correct |
20 |
Correct |
60 ms |
70268 KB |
Output is correct |
21 |
Correct |
59 ms |
70264 KB |
Output is correct |
22 |
Correct |
59 ms |
70136 KB |
Output is correct |
23 |
Correct |
60 ms |
70136 KB |
Output is correct |
24 |
Correct |
58 ms |
70136 KB |
Output is correct |
25 |
Correct |
59 ms |
70248 KB |
Output is correct |
26 |
Correct |
55 ms |
70136 KB |
Output is correct |
27 |
Correct |
59 ms |
70400 KB |
Output is correct |
28 |
Correct |
59 ms |
70264 KB |
Output is correct |
29 |
Correct |
61 ms |
70264 KB |
Output is correct |
30 |
Correct |
63 ms |
70136 KB |
Output is correct |
31 |
Correct |
60 ms |
70136 KB |
Output is correct |
32 |
Correct |
59 ms |
70264 KB |
Output is correct |
33 |
Correct |
59 ms |
70136 KB |
Output is correct |
34 |
Correct |
60 ms |
70136 KB |
Output is correct |
35 |
Correct |
61 ms |
70136 KB |
Output is correct |
36 |
Correct |
59 ms |
70136 KB |
Output is correct |
37 |
Correct |
60 ms |
70136 KB |
Output is correct |
38 |
Correct |
59 ms |
70140 KB |
Output is correct |
39 |
Correct |
61 ms |
70352 KB |
Output is correct |
40 |
Correct |
59 ms |
70136 KB |
Output is correct |
41 |
Correct |
59 ms |
70136 KB |
Output is correct |
42 |
Correct |
58 ms |
70140 KB |
Output is correct |
43 |
Correct |
60 ms |
70252 KB |
Output is correct |
44 |
Correct |
61 ms |
70136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
70008 KB |
Output is correct |
2 |
Correct |
54 ms |
70008 KB |
Output is correct |
3 |
Correct |
59 ms |
70136 KB |
Output is correct |
4 |
Correct |
58 ms |
70140 KB |
Output is correct |
5 |
Correct |
60 ms |
70264 KB |
Output is correct |
6 |
Correct |
60 ms |
70136 KB |
Output is correct |
7 |
Correct |
59 ms |
70136 KB |
Output is correct |
8 |
Correct |
56 ms |
70272 KB |
Output is correct |
9 |
Correct |
55 ms |
70136 KB |
Output is correct |
10 |
Correct |
55 ms |
70136 KB |
Output is correct |
11 |
Correct |
58 ms |
70392 KB |
Output is correct |
12 |
Correct |
60 ms |
70268 KB |
Output is correct |
13 |
Correct |
61 ms |
70244 KB |
Output is correct |
14 |
Correct |
59 ms |
70272 KB |
Output is correct |
15 |
Correct |
60 ms |
70136 KB |
Output is correct |
16 |
Correct |
59 ms |
70136 KB |
Output is correct |
17 |
Correct |
60 ms |
70136 KB |
Output is correct |
18 |
Correct |
59 ms |
70136 KB |
Output is correct |
19 |
Correct |
60 ms |
70280 KB |
Output is correct |
20 |
Correct |
60 ms |
70268 KB |
Output is correct |
21 |
Correct |
59 ms |
70264 KB |
Output is correct |
22 |
Correct |
59 ms |
70136 KB |
Output is correct |
23 |
Correct |
60 ms |
70136 KB |
Output is correct |
24 |
Correct |
58 ms |
70136 KB |
Output is correct |
25 |
Correct |
59 ms |
70248 KB |
Output is correct |
26 |
Correct |
55 ms |
70136 KB |
Output is correct |
27 |
Correct |
59 ms |
70400 KB |
Output is correct |
28 |
Correct |
59 ms |
70264 KB |
Output is correct |
29 |
Correct |
61 ms |
70264 KB |
Output is correct |
30 |
Correct |
63 ms |
70136 KB |
Output is correct |
31 |
Correct |
60 ms |
70136 KB |
Output is correct |
32 |
Correct |
59 ms |
70264 KB |
Output is correct |
33 |
Correct |
59 ms |
70136 KB |
Output is correct |
34 |
Correct |
60 ms |
70136 KB |
Output is correct |
35 |
Correct |
61 ms |
70136 KB |
Output is correct |
36 |
Correct |
59 ms |
70136 KB |
Output is correct |
37 |
Correct |
60 ms |
70136 KB |
Output is correct |
38 |
Correct |
59 ms |
70140 KB |
Output is correct |
39 |
Correct |
61 ms |
70352 KB |
Output is correct |
40 |
Correct |
59 ms |
70136 KB |
Output is correct |
41 |
Correct |
59 ms |
70136 KB |
Output is correct |
42 |
Correct |
58 ms |
70140 KB |
Output is correct |
43 |
Correct |
60 ms |
70252 KB |
Output is correct |
44 |
Correct |
61 ms |
70136 KB |
Output is correct |
45 |
Correct |
61 ms |
70392 KB |
Output is correct |
46 |
Correct |
82 ms |
71416 KB |
Output is correct |
47 |
Correct |
84 ms |
71416 KB |
Output is correct |
48 |
Correct |
84 ms |
71416 KB |
Output is correct |
49 |
Correct |
75 ms |
70736 KB |
Output is correct |
50 |
Correct |
74 ms |
70648 KB |
Output is correct |
51 |
Correct |
77 ms |
70648 KB |
Output is correct |
52 |
Correct |
79 ms |
71544 KB |
Output is correct |
53 |
Correct |
80 ms |
71600 KB |
Output is correct |
54 |
Correct |
84 ms |
71672 KB |
Output is correct |
55 |
Correct |
80 ms |
71672 KB |
Output is correct |
56 |
Correct |
82 ms |
71544 KB |
Output is correct |
57 |
Correct |
86 ms |
71544 KB |
Output is correct |
58 |
Correct |
86 ms |
71416 KB |
Output is correct |
59 |
Correct |
85 ms |
71416 KB |
Output is correct |
60 |
Correct |
84 ms |
71420 KB |
Output is correct |
61 |
Correct |
83 ms |
71544 KB |
Output is correct |
62 |
Correct |
81 ms |
71548 KB |
Output is correct |
63 |
Correct |
81 ms |
71544 KB |
Output is correct |
64 |
Correct |
82 ms |
71416 KB |
Output is correct |
65 |
Correct |
83 ms |
71416 KB |
Output is correct |
66 |
Correct |
83 ms |
71416 KB |
Output is correct |
67 |
Correct |
83 ms |
71416 KB |
Output is correct |
68 |
Correct |
75 ms |
70652 KB |
Output is correct |
69 |
Correct |
82 ms |
71544 KB |
Output is correct |
70 |
Correct |
81 ms |
71672 KB |
Output is correct |
71 |
Correct |
81 ms |
71544 KB |
Output is correct |
72 |
Correct |
85 ms |
71420 KB |
Output is correct |
73 |
Correct |
84 ms |
71380 KB |
Output is correct |
74 |
Correct |
80 ms |
71548 KB |
Output is correct |
75 |
Correct |
80 ms |
71544 KB |
Output is correct |
76 |
Correct |
80 ms |
71416 KB |
Output is correct |
77 |
Correct |
81 ms |
71544 KB |
Output is correct |
78 |
Correct |
82 ms |
71416 KB |
Output is correct |
79 |
Correct |
80 ms |
71500 KB |
Output is correct |
80 |
Correct |
80 ms |
71416 KB |
Output is correct |
81 |
Correct |
80 ms |
71480 KB |
Output is correct |
82 |
Correct |
81 ms |
71420 KB |
Output is correct |
83 |
Correct |
82 ms |
71416 KB |
Output is correct |
84 |
Correct |
81 ms |
71416 KB |
Output is correct |
85 |
Correct |
81 ms |
71416 KB |
Output is correct |
86 |
Correct |
80 ms |
71416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
70008 KB |
Output is correct |
2 |
Correct |
54 ms |
70008 KB |
Output is correct |
3 |
Correct |
59 ms |
70136 KB |
Output is correct |
4 |
Correct |
58 ms |
70140 KB |
Output is correct |
5 |
Correct |
60 ms |
70264 KB |
Output is correct |
6 |
Correct |
60 ms |
70136 KB |
Output is correct |
7 |
Correct |
59 ms |
70136 KB |
Output is correct |
8 |
Correct |
56 ms |
70272 KB |
Output is correct |
9 |
Correct |
55 ms |
70136 KB |
Output is correct |
10 |
Correct |
55 ms |
70136 KB |
Output is correct |
11 |
Correct |
58 ms |
70392 KB |
Output is correct |
12 |
Correct |
60 ms |
70268 KB |
Output is correct |
13 |
Correct |
61 ms |
70244 KB |
Output is correct |
14 |
Correct |
59 ms |
70272 KB |
Output is correct |
15 |
Correct |
60 ms |
70136 KB |
Output is correct |
16 |
Correct |
59 ms |
70136 KB |
Output is correct |
17 |
Correct |
60 ms |
70136 KB |
Output is correct |
18 |
Correct |
59 ms |
70136 KB |
Output is correct |
19 |
Correct |
60 ms |
70280 KB |
Output is correct |
20 |
Correct |
60 ms |
70268 KB |
Output is correct |
21 |
Correct |
59 ms |
70264 KB |
Output is correct |
22 |
Correct |
59 ms |
70136 KB |
Output is correct |
23 |
Correct |
60 ms |
70136 KB |
Output is correct |
24 |
Correct |
58 ms |
70136 KB |
Output is correct |
25 |
Correct |
59 ms |
70248 KB |
Output is correct |
26 |
Correct |
55 ms |
70136 KB |
Output is correct |
27 |
Correct |
59 ms |
70400 KB |
Output is correct |
28 |
Correct |
59 ms |
70264 KB |
Output is correct |
29 |
Correct |
61 ms |
70264 KB |
Output is correct |
30 |
Correct |
63 ms |
70136 KB |
Output is correct |
31 |
Correct |
60 ms |
70136 KB |
Output is correct |
32 |
Correct |
59 ms |
70264 KB |
Output is correct |
33 |
Correct |
59 ms |
70136 KB |
Output is correct |
34 |
Correct |
60 ms |
70136 KB |
Output is correct |
35 |
Correct |
61 ms |
70136 KB |
Output is correct |
36 |
Correct |
59 ms |
70136 KB |
Output is correct |
37 |
Correct |
60 ms |
70136 KB |
Output is correct |
38 |
Correct |
59 ms |
70140 KB |
Output is correct |
39 |
Correct |
61 ms |
70352 KB |
Output is correct |
40 |
Correct |
59 ms |
70136 KB |
Output is correct |
41 |
Correct |
59 ms |
70136 KB |
Output is correct |
42 |
Correct |
58 ms |
70140 KB |
Output is correct |
43 |
Correct |
60 ms |
70252 KB |
Output is correct |
44 |
Correct |
61 ms |
70136 KB |
Output is correct |
45 |
Correct |
61 ms |
70392 KB |
Output is correct |
46 |
Correct |
82 ms |
71416 KB |
Output is correct |
47 |
Correct |
84 ms |
71416 KB |
Output is correct |
48 |
Correct |
84 ms |
71416 KB |
Output is correct |
49 |
Correct |
75 ms |
70736 KB |
Output is correct |
50 |
Correct |
74 ms |
70648 KB |
Output is correct |
51 |
Correct |
77 ms |
70648 KB |
Output is correct |
52 |
Correct |
79 ms |
71544 KB |
Output is correct |
53 |
Correct |
80 ms |
71600 KB |
Output is correct |
54 |
Correct |
84 ms |
71672 KB |
Output is correct |
55 |
Correct |
80 ms |
71672 KB |
Output is correct |
56 |
Correct |
82 ms |
71544 KB |
Output is correct |
57 |
Correct |
86 ms |
71544 KB |
Output is correct |
58 |
Correct |
86 ms |
71416 KB |
Output is correct |
59 |
Correct |
85 ms |
71416 KB |
Output is correct |
60 |
Correct |
84 ms |
71420 KB |
Output is correct |
61 |
Correct |
83 ms |
71544 KB |
Output is correct |
62 |
Correct |
81 ms |
71548 KB |
Output is correct |
63 |
Correct |
81 ms |
71544 KB |
Output is correct |
64 |
Correct |
82 ms |
71416 KB |
Output is correct |
65 |
Correct |
83 ms |
71416 KB |
Output is correct |
66 |
Correct |
83 ms |
71416 KB |
Output is correct |
67 |
Correct |
83 ms |
71416 KB |
Output is correct |
68 |
Correct |
75 ms |
70652 KB |
Output is correct |
69 |
Correct |
82 ms |
71544 KB |
Output is correct |
70 |
Correct |
81 ms |
71672 KB |
Output is correct |
71 |
Correct |
81 ms |
71544 KB |
Output is correct |
72 |
Correct |
85 ms |
71420 KB |
Output is correct |
73 |
Correct |
84 ms |
71380 KB |
Output is correct |
74 |
Correct |
80 ms |
71548 KB |
Output is correct |
75 |
Correct |
80 ms |
71544 KB |
Output is correct |
76 |
Correct |
80 ms |
71416 KB |
Output is correct |
77 |
Correct |
81 ms |
71544 KB |
Output is correct |
78 |
Correct |
82 ms |
71416 KB |
Output is correct |
79 |
Correct |
80 ms |
71500 KB |
Output is correct |
80 |
Correct |
80 ms |
71416 KB |
Output is correct |
81 |
Correct |
80 ms |
71480 KB |
Output is correct |
82 |
Correct |
81 ms |
71420 KB |
Output is correct |
83 |
Correct |
82 ms |
71416 KB |
Output is correct |
84 |
Correct |
81 ms |
71416 KB |
Output is correct |
85 |
Correct |
81 ms |
71416 KB |
Output is correct |
86 |
Correct |
80 ms |
71416 KB |
Output is correct |
87 |
Correct |
184 ms |
73548 KB |
Output is correct |
88 |
Correct |
1086 ms |
80776 KB |
Output is correct |
89 |
Execution timed out |
2105 ms |
84332 KB |
Time limit exceeded |
90 |
Halted |
0 ms |
0 KB |
- |