#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
typedef long long ll;
using namespace std;
const int inf = 1e9 + 5;
struct intervalac
{
int n;
vector<int> st;
void init(int N) { n = N, st.assign(2 * n, 0); }
void upd(int i, int x) { for (i += n; i > 0; i >>= 1) st[i] += x; }
ll query(int l, int r)
{
int ans = 0;
for (l += n, r += n + 1; l < r; l >>= 1, r >>= 1)
{
if (l & 1) ans += st[l++];
if (r & 1) ans += st[--r];
}
return ans;
}
};
int n;
int cnt = 0;
vector<set<pair<pair<int, int>, int> > > zac; // zaciatky usekov v retaziach
vector<int> r, s, h, p, d, a, b, c; // moja retaz, moja velkost, najvyssi v mojej retazi, moj otec
vector<vector<int> > g;
void dfss(int u)
{
s[u] = 1;
for (int v : g[u]) d[v] = d[u] + 1, dfss(v), s[u] += s[v];
}
void dfs(int u)
{
if (r[u] == -1) h[cnt] = u, r[u] = cnt++;
int v2 = -1;
for (int v : g[u]) if (v2 == -1 || s[v] > s[v2]) v2 = v;
if (v2 != -1) r[v2] = r[u];
else zac[r[u]].insert({ {d[h[r[u]]], d[u]}, c[n] });
for (int v : g[u]) dfs(v);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
zac.assign(n, {}), r.assign(n, -1), s.assign(n, -1), h.assign(n, -1), p.assign(n, 0), d.assign(n, 0), a.assign(n - 1, 0), b.assign(n - 1, 0), c.assign(n, 0), g.assign(n, {});
for (int i = 0; i < n; i++) cin >> c[i];
c.push_back(inf);
vector<int> c2 = c;
sort(c2.begin(), c2.end());
for (int i = 0; i <= n; i++) c[i] = lower_bound(c2.begin(), c2.end(), c[i]) - c2.begin();
for (int i = 0; i < n - 1; i++) cin >> a[i] >> b[i], g[--a[i]].push_back(--b[i]), p[b[i]] = a[i];
dfss(0);
dfs(0);
intervalac in;
in.init(n + 1);
for (int i = 0; i < n - 1; i++)
{
ll ans = 0;
int u = b[i];
vector<int> reset;
while (true)
{
auto it = zac[r[u]].lower_bound({ {d[u] + 1, -1}, -1 });
if (it == zac[r[u]].begin()) // uz sme vyhodili vsetky
{
zac[r[u]].insert({ {d[h[r[u]]], d[u]}, c[b[i]] });
if (h[r[u]])
{
u = p[h[r[u]]];
continue;
}
else break;
continue;
}
it--;
int dl = it->first.first, dr = it->first.second, col = it->second;
reset.push_back(col);
zac[r[u]].erase(it);
ans += min(dr - dl + 1, d[u] - dl + 1) * 1ll * in.query(0, col - 1);
in.upd(col, min(dr - dl + 1, d[u] - dl + 1));
if (d[u] < dr) zac[r[u]].insert({ {d[u] + 1, dr}, col });
}
for (int j : reset) in.upd(j, -in.st[n + 1 + j]);
cout << ans << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
464 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
456 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
456 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
344 KB |
Output is correct |
41 |
Correct |
1 ms |
348 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
464 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
456 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
456 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
344 KB |
Output is correct |
41 |
Correct |
1 ms |
348 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
2 ms |
604 KB |
Output is correct |
46 |
Correct |
5 ms |
1116 KB |
Output is correct |
47 |
Correct |
6 ms |
1188 KB |
Output is correct |
48 |
Correct |
6 ms |
1116 KB |
Output is correct |
49 |
Correct |
4 ms |
1372 KB |
Output is correct |
50 |
Correct |
4 ms |
1372 KB |
Output is correct |
51 |
Correct |
3 ms |
1372 KB |
Output is correct |
52 |
Correct |
3 ms |
1372 KB |
Output is correct |
53 |
Correct |
4 ms |
1372 KB |
Output is correct |
54 |
Correct |
3 ms |
1372 KB |
Output is correct |
55 |
Correct |
4 ms |
1372 KB |
Output is correct |
56 |
Correct |
3 ms |
1264 KB |
Output is correct |
57 |
Correct |
7 ms |
1116 KB |
Output is correct |
58 |
Correct |
8 ms |
1116 KB |
Output is correct |
59 |
Correct |
8 ms |
1116 KB |
Output is correct |
60 |
Correct |
8 ms |
1116 KB |
Output is correct |
61 |
Correct |
3 ms |
1116 KB |
Output is correct |
62 |
Correct |
3 ms |
1116 KB |
Output is correct |
63 |
Correct |
4 ms |
1116 KB |
Output is correct |
64 |
Correct |
5 ms |
1180 KB |
Output is correct |
65 |
Correct |
5 ms |
1116 KB |
Output is correct |
66 |
Correct |
5 ms |
1220 KB |
Output is correct |
67 |
Correct |
6 ms |
1116 KB |
Output is correct |
68 |
Correct |
2 ms |
1116 KB |
Output is correct |
69 |
Correct |
4 ms |
1368 KB |
Output is correct |
70 |
Correct |
3 ms |
1372 KB |
Output is correct |
71 |
Correct |
3 ms |
1116 KB |
Output is correct |
72 |
Correct |
7 ms |
1116 KB |
Output is correct |
73 |
Correct |
8 ms |
1116 KB |
Output is correct |
74 |
Correct |
4 ms |
1116 KB |
Output is correct |
75 |
Correct |
4 ms |
1116 KB |
Output is correct |
76 |
Correct |
3 ms |
1116 KB |
Output is correct |
77 |
Correct |
3 ms |
1116 KB |
Output is correct |
78 |
Correct |
3 ms |
1116 KB |
Output is correct |
79 |
Correct |
3 ms |
1116 KB |
Output is correct |
80 |
Correct |
3 ms |
1180 KB |
Output is correct |
81 |
Correct |
4 ms |
1140 KB |
Output is correct |
82 |
Correct |
4 ms |
1116 KB |
Output is correct |
83 |
Correct |
3 ms |
1116 KB |
Output is correct |
84 |
Correct |
3 ms |
1116 KB |
Output is correct |
85 |
Correct |
3 ms |
1116 KB |
Output is correct |
86 |
Correct |
4 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
464 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
456 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
456 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
344 KB |
Output is correct |
41 |
Correct |
1 ms |
348 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
2 ms |
604 KB |
Output is correct |
46 |
Correct |
5 ms |
1116 KB |
Output is correct |
47 |
Correct |
6 ms |
1188 KB |
Output is correct |
48 |
Correct |
6 ms |
1116 KB |
Output is correct |
49 |
Correct |
4 ms |
1372 KB |
Output is correct |
50 |
Correct |
4 ms |
1372 KB |
Output is correct |
51 |
Correct |
3 ms |
1372 KB |
Output is correct |
52 |
Correct |
3 ms |
1372 KB |
Output is correct |
53 |
Correct |
4 ms |
1372 KB |
Output is correct |
54 |
Correct |
3 ms |
1372 KB |
Output is correct |
55 |
Correct |
4 ms |
1372 KB |
Output is correct |
56 |
Correct |
3 ms |
1264 KB |
Output is correct |
57 |
Correct |
7 ms |
1116 KB |
Output is correct |
58 |
Correct |
8 ms |
1116 KB |
Output is correct |
59 |
Correct |
8 ms |
1116 KB |
Output is correct |
60 |
Correct |
8 ms |
1116 KB |
Output is correct |
61 |
Correct |
3 ms |
1116 KB |
Output is correct |
62 |
Correct |
3 ms |
1116 KB |
Output is correct |
63 |
Correct |
4 ms |
1116 KB |
Output is correct |
64 |
Correct |
5 ms |
1180 KB |
Output is correct |
65 |
Correct |
5 ms |
1116 KB |
Output is correct |
66 |
Correct |
5 ms |
1220 KB |
Output is correct |
67 |
Correct |
6 ms |
1116 KB |
Output is correct |
68 |
Correct |
2 ms |
1116 KB |
Output is correct |
69 |
Correct |
4 ms |
1368 KB |
Output is correct |
70 |
Correct |
3 ms |
1372 KB |
Output is correct |
71 |
Correct |
3 ms |
1116 KB |
Output is correct |
72 |
Correct |
7 ms |
1116 KB |
Output is correct |
73 |
Correct |
8 ms |
1116 KB |
Output is correct |
74 |
Correct |
4 ms |
1116 KB |
Output is correct |
75 |
Correct |
4 ms |
1116 KB |
Output is correct |
76 |
Correct |
3 ms |
1116 KB |
Output is correct |
77 |
Correct |
3 ms |
1116 KB |
Output is correct |
78 |
Correct |
3 ms |
1116 KB |
Output is correct |
79 |
Correct |
3 ms |
1116 KB |
Output is correct |
80 |
Correct |
3 ms |
1180 KB |
Output is correct |
81 |
Correct |
4 ms |
1140 KB |
Output is correct |
82 |
Correct |
4 ms |
1116 KB |
Output is correct |
83 |
Correct |
3 ms |
1116 KB |
Output is correct |
84 |
Correct |
3 ms |
1116 KB |
Output is correct |
85 |
Correct |
3 ms |
1116 KB |
Output is correct |
86 |
Correct |
4 ms |
1116 KB |
Output is correct |
87 |
Correct |
16 ms |
2396 KB |
Output is correct |
88 |
Correct |
60 ms |
6228 KB |
Output is correct |
89 |
Correct |
218 ms |
20112 KB |
Output is correct |
90 |
Correct |
218 ms |
20168 KB |
Output is correct |
91 |
Correct |
213 ms |
20336 KB |
Output is correct |
92 |
Correct |
79 ms |
23496 KB |
Output is correct |
93 |
Correct |
79 ms |
23496 KB |
Output is correct |
94 |
Correct |
79 ms |
23344 KB |
Output is correct |
95 |
Correct |
104 ms |
24524 KB |
Output is correct |
96 |
Correct |
110 ms |
25032 KB |
Output is correct |
97 |
Correct |
115 ms |
25036 KB |
Output is correct |
98 |
Correct |
112 ms |
24980 KB |
Output is correct |
99 |
Correct |
84 ms |
22052 KB |
Output is correct |
100 |
Correct |
300 ms |
21964 KB |
Output is correct |
101 |
Correct |
378 ms |
22472 KB |
Output is correct |
102 |
Correct |
355 ms |
22476 KB |
Output is correct |
103 |
Correct |
369 ms |
22472 KB |
Output is correct |
104 |
Correct |
102 ms |
21980 KB |
Output is correct |
105 |
Correct |
97 ms |
21932 KB |
Output is correct |
106 |
Correct |
98 ms |
21964 KB |
Output is correct |
107 |
Correct |
151 ms |
19404 KB |
Output is correct |
108 |
Correct |
179 ms |
19344 KB |
Output is correct |
109 |
Correct |
212 ms |
19660 KB |
Output is correct |
110 |
Correct |
75 ms |
22728 KB |
Output is correct |
111 |
Correct |
106 ms |
24524 KB |
Output is correct |
112 |
Correct |
100 ms |
24264 KB |
Output is correct |
113 |
Correct |
77 ms |
21452 KB |
Output is correct |
114 |
Correct |
307 ms |
22220 KB |
Output is correct |
115 |
Correct |
326 ms |
21784 KB |
Output is correct |
116 |
Correct |
90 ms |
21272 KB |
Output is correct |
117 |
Correct |
91 ms |
20120 KB |
Output is correct |
118 |
Correct |
90 ms |
19596 KB |
Output is correct |
119 |
Correct |
92 ms |
19148 KB |
Output is correct |
120 |
Correct |
79 ms |
19636 KB |
Output is correct |
121 |
Correct |
78 ms |
19112 KB |
Output is correct |
122 |
Correct |
94 ms |
18924 KB |
Output is correct |
123 |
Correct |
106 ms |
20172 KB |
Output is correct |
124 |
Correct |
118 ms |
19548 KB |
Output is correct |
125 |
Correct |
108 ms |
19404 KB |
Output is correct |
126 |
Correct |
95 ms |
19916 KB |
Output is correct |
127 |
Correct |
95 ms |
19392 KB |
Output is correct |
128 |
Correct |
101 ms |
19144 KB |
Output is correct |