//Then
#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define faster ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair <int, int>;
mt19937_64 Rand(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 1e5 + 1;
//const int Mod = 1e9 + 7;
//const int inf =
int n;
int a[maxN], b[maxN], c[maxN];
struct TSeg{
int l, r, t;
};
deque <TSeg> S[maxN];
vector <int> adj[maxN];
int head[maxN], id[maxN], num[maxN];
int child[maxN], depth[maxN], par[maxN];
int timedfs, cnt = 1;
vector <int> node;
struct TBIT{
int bit[maxN];
TBIT(){
memset(bit, 0, sizeof(bit));
}
void update(int x, int v){
for (; x <= n; x += x & -x){
bit[x] += v;
}
}
int get(int x){
int res = 0;
for (; x; x -= x & -x){
res += bit[x];
}
return res;
}
};
TBIT bit;
void dfs(int u, int p){
depth[u] = depth[p] + 1;
par[u] = p;
for (int i: adj[u]){
dfs(i, u);
child[u] += child[i] + 1;
}
}
void hld(int u){
num[u] = ++timedfs;
if (!head[cnt]) head[cnt] = u;
id[u] = cnt;
pii maxx = {-1, -1};
for (int i: adj[u]){
maxx = max(maxx, {child[i], i});
}
if (maxx.se != -1){
hld(maxx.se);
}
for (int i: adj[u]){
if (i == maxx.se) continue;
++cnt;
hld(i);
}
}
ll query(int u, int v){
ll res = 0;
vector <TSeg> save;
while (u){
int p = id[u];
vector <TSeg> tem;
while (!S[p].empty() && S[p].back().r <= num[u]){
tem.pb(S[p].back());
S[p].pop_back();
}
if (!S[p].empty() && S[p].back().l <= num[u]){
tem.pb({S[p].back().l, num[u], S[p].back().t});
S[p].back().l = num[u] + 1;
}
reverse(tem.begin(), tem.end());
for (auto i: tem){
res += 1LL * (i.r - i.l + 1) * bit.get(i.t - 1);
bit.update(i.t, i.r - i.l + 1);
}
move(tem.begin(), tem.end(), back_inserter(save));
S[p].pb({num[head[p]], num[u], c[v]});
u = par[head[p]];
}
for (auto i: save){
bit.update(i.t, i.l - i.r - 1);
}
S[id[v]].push_front({num[v], num[v], c[v]});
return res;
}
void Init(){
cin >> n;
for (int i = 1; i <= n; ++i){
cin >> c[i];
node.pb(c[i]);
}
sort(node.begin(), node.end());
node.resize(unique(node.begin(), node.end()) - node.begin());
for (int i = 1; i <= n; ++i){
c[i] = upper_bound(node.begin(), node.end(), c[i]) - node.begin();
}
for (int i = 1; i < n; ++i){
cin >> a[i] >> b[i];
adj[a[i]].pb(b[i]);
}
dfs(1, 0);
hld(1);
S[1].pb({1, 1, c[1]});
for (int i = 1; i < n; ++i){
cout << query(a[i], b[i]) << "\n";
}
}
#define debug
#define taskname "test"
signed main(){
faster
if (fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
freopen(taskname".out", "w", stdout);
}
int tt = 1;
//cin >> tt;
while (tt--){
Init();
}
if (fopen("timeout.txt", "r")){
ofstream timeout("timeout.txt");
timeout << signed(double(clock()) / CLOCKS_PER_SEC * 1000);
timeout.close();
#ifndef debug
cerr << "Time elapsed: " << signed(double(clock()) / CLOCKS_PER_SEC * 1000) << "ms\n";
#endif // debug
}
}
Compilation message
construction.cpp: In function 'int main()':
construction.cpp:133:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
133 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
construction.cpp:134:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
134 | freopen(taskname".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
68836 KB |
Output is correct |
2 |
Correct |
39 ms |
68796 KB |
Output is correct |
3 |
Correct |
46 ms |
68824 KB |
Output is correct |
4 |
Correct |
42 ms |
68928 KB |
Output is correct |
5 |
Correct |
42 ms |
68988 KB |
Output is correct |
6 |
Correct |
40 ms |
68980 KB |
Output is correct |
7 |
Correct |
44 ms |
68892 KB |
Output is correct |
8 |
Correct |
39 ms |
68916 KB |
Output is correct |
9 |
Correct |
40 ms |
68828 KB |
Output is correct |
10 |
Correct |
46 ms |
68812 KB |
Output is correct |
11 |
Correct |
40 ms |
69032 KB |
Output is correct |
12 |
Correct |
42 ms |
69012 KB |
Output is correct |
13 |
Correct |
41 ms |
69036 KB |
Output is correct |
14 |
Correct |
44 ms |
68940 KB |
Output is correct |
15 |
Correct |
45 ms |
68980 KB |
Output is correct |
16 |
Correct |
40 ms |
68940 KB |
Output is correct |
17 |
Correct |
45 ms |
69004 KB |
Output is correct |
18 |
Correct |
43 ms |
68884 KB |
Output is correct |
19 |
Correct |
44 ms |
68952 KB |
Output is correct |
20 |
Correct |
43 ms |
68956 KB |
Output is correct |
21 |
Correct |
43 ms |
68960 KB |
Output is correct |
22 |
Correct |
50 ms |
68944 KB |
Output is correct |
23 |
Correct |
43 ms |
68884 KB |
Output is correct |
24 |
Correct |
41 ms |
68980 KB |
Output is correct |
25 |
Correct |
43 ms |
68948 KB |
Output is correct |
26 |
Correct |
43 ms |
68844 KB |
Output is correct |
27 |
Correct |
40 ms |
69032 KB |
Output is correct |
28 |
Correct |
40 ms |
68968 KB |
Output is correct |
29 |
Correct |
45 ms |
68956 KB |
Output is correct |
30 |
Correct |
40 ms |
68972 KB |
Output is correct |
31 |
Correct |
45 ms |
68952 KB |
Output is correct |
32 |
Correct |
50 ms |
68944 KB |
Output is correct |
33 |
Correct |
52 ms |
69008 KB |
Output is correct |
34 |
Correct |
46 ms |
69072 KB |
Output is correct |
35 |
Correct |
44 ms |
68972 KB |
Output is correct |
36 |
Correct |
54 ms |
69012 KB |
Output is correct |
37 |
Correct |
44 ms |
68948 KB |
Output is correct |
38 |
Correct |
50 ms |
68940 KB |
Output is correct |
39 |
Correct |
44 ms |
68964 KB |
Output is correct |
40 |
Correct |
46 ms |
68972 KB |
Output is correct |
41 |
Correct |
44 ms |
69012 KB |
Output is correct |
42 |
Correct |
43 ms |
69016 KB |
Output is correct |
43 |
Correct |
40 ms |
68948 KB |
Output is correct |
44 |
Correct |
43 ms |
69008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
68836 KB |
Output is correct |
2 |
Correct |
39 ms |
68796 KB |
Output is correct |
3 |
Correct |
46 ms |
68824 KB |
Output is correct |
4 |
Correct |
42 ms |
68928 KB |
Output is correct |
5 |
Correct |
42 ms |
68988 KB |
Output is correct |
6 |
Correct |
40 ms |
68980 KB |
Output is correct |
7 |
Correct |
44 ms |
68892 KB |
Output is correct |
8 |
Correct |
39 ms |
68916 KB |
Output is correct |
9 |
Correct |
40 ms |
68828 KB |
Output is correct |
10 |
Correct |
46 ms |
68812 KB |
Output is correct |
11 |
Correct |
40 ms |
69032 KB |
Output is correct |
12 |
Correct |
42 ms |
69012 KB |
Output is correct |
13 |
Correct |
41 ms |
69036 KB |
Output is correct |
14 |
Correct |
44 ms |
68940 KB |
Output is correct |
15 |
Correct |
45 ms |
68980 KB |
Output is correct |
16 |
Correct |
40 ms |
68940 KB |
Output is correct |
17 |
Correct |
45 ms |
69004 KB |
Output is correct |
18 |
Correct |
43 ms |
68884 KB |
Output is correct |
19 |
Correct |
44 ms |
68952 KB |
Output is correct |
20 |
Correct |
43 ms |
68956 KB |
Output is correct |
21 |
Correct |
43 ms |
68960 KB |
Output is correct |
22 |
Correct |
50 ms |
68944 KB |
Output is correct |
23 |
Correct |
43 ms |
68884 KB |
Output is correct |
24 |
Correct |
41 ms |
68980 KB |
Output is correct |
25 |
Correct |
43 ms |
68948 KB |
Output is correct |
26 |
Correct |
43 ms |
68844 KB |
Output is correct |
27 |
Correct |
40 ms |
69032 KB |
Output is correct |
28 |
Correct |
40 ms |
68968 KB |
Output is correct |
29 |
Correct |
45 ms |
68956 KB |
Output is correct |
30 |
Correct |
40 ms |
68972 KB |
Output is correct |
31 |
Correct |
45 ms |
68952 KB |
Output is correct |
32 |
Correct |
50 ms |
68944 KB |
Output is correct |
33 |
Correct |
52 ms |
69008 KB |
Output is correct |
34 |
Correct |
46 ms |
69072 KB |
Output is correct |
35 |
Correct |
44 ms |
68972 KB |
Output is correct |
36 |
Correct |
54 ms |
69012 KB |
Output is correct |
37 |
Correct |
44 ms |
68948 KB |
Output is correct |
38 |
Correct |
50 ms |
68940 KB |
Output is correct |
39 |
Correct |
44 ms |
68964 KB |
Output is correct |
40 |
Correct |
46 ms |
68972 KB |
Output is correct |
41 |
Correct |
44 ms |
69012 KB |
Output is correct |
42 |
Correct |
43 ms |
69016 KB |
Output is correct |
43 |
Correct |
40 ms |
68948 KB |
Output is correct |
44 |
Correct |
43 ms |
69008 KB |
Output is correct |
45 |
Correct |
51 ms |
69132 KB |
Output is correct |
46 |
Correct |
49 ms |
69920 KB |
Output is correct |
47 |
Correct |
49 ms |
69864 KB |
Output is correct |
48 |
Correct |
51 ms |
69944 KB |
Output is correct |
49 |
Correct |
46 ms |
69348 KB |
Output is correct |
50 |
Correct |
45 ms |
69452 KB |
Output is correct |
51 |
Correct |
43 ms |
69412 KB |
Output is correct |
52 |
Correct |
48 ms |
70240 KB |
Output is correct |
53 |
Correct |
47 ms |
70348 KB |
Output is correct |
54 |
Correct |
47 ms |
70360 KB |
Output is correct |
55 |
Correct |
47 ms |
70352 KB |
Output is correct |
56 |
Correct |
47 ms |
70240 KB |
Output is correct |
57 |
Correct |
52 ms |
69804 KB |
Output is correct |
58 |
Correct |
53 ms |
69844 KB |
Output is correct |
59 |
Correct |
53 ms |
69808 KB |
Output is correct |
60 |
Correct |
52 ms |
69804 KB |
Output is correct |
61 |
Correct |
47 ms |
70200 KB |
Output is correct |
62 |
Correct |
49 ms |
70228 KB |
Output is correct |
63 |
Correct |
47 ms |
70244 KB |
Output is correct |
64 |
Correct |
49 ms |
69844 KB |
Output is correct |
65 |
Correct |
54 ms |
69932 KB |
Output is correct |
66 |
Correct |
48 ms |
69936 KB |
Output is correct |
67 |
Correct |
49 ms |
69876 KB |
Output is correct |
68 |
Correct |
44 ms |
69320 KB |
Output is correct |
69 |
Correct |
47 ms |
70220 KB |
Output is correct |
70 |
Correct |
46 ms |
70228 KB |
Output is correct |
71 |
Correct |
51 ms |
70220 KB |
Output is correct |
72 |
Correct |
51 ms |
69836 KB |
Output is correct |
73 |
Correct |
50 ms |
69768 KB |
Output is correct |
74 |
Correct |
47 ms |
70252 KB |
Output is correct |
75 |
Correct |
48 ms |
70192 KB |
Output is correct |
76 |
Correct |
53 ms |
70116 KB |
Output is correct |
77 |
Correct |
47 ms |
70164 KB |
Output is correct |
78 |
Correct |
46 ms |
70116 KB |
Output is correct |
79 |
Correct |
46 ms |
70240 KB |
Output is correct |
80 |
Correct |
47 ms |
70108 KB |
Output is correct |
81 |
Correct |
48 ms |
70136 KB |
Output is correct |
82 |
Correct |
48 ms |
70144 KB |
Output is correct |
83 |
Correct |
47 ms |
70116 KB |
Output is correct |
84 |
Correct |
48 ms |
70116 KB |
Output is correct |
85 |
Correct |
47 ms |
70096 KB |
Output is correct |
86 |
Correct |
49 ms |
70196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
68836 KB |
Output is correct |
2 |
Correct |
39 ms |
68796 KB |
Output is correct |
3 |
Correct |
46 ms |
68824 KB |
Output is correct |
4 |
Correct |
42 ms |
68928 KB |
Output is correct |
5 |
Correct |
42 ms |
68988 KB |
Output is correct |
6 |
Correct |
40 ms |
68980 KB |
Output is correct |
7 |
Correct |
44 ms |
68892 KB |
Output is correct |
8 |
Correct |
39 ms |
68916 KB |
Output is correct |
9 |
Correct |
40 ms |
68828 KB |
Output is correct |
10 |
Correct |
46 ms |
68812 KB |
Output is correct |
11 |
Correct |
40 ms |
69032 KB |
Output is correct |
12 |
Correct |
42 ms |
69012 KB |
Output is correct |
13 |
Correct |
41 ms |
69036 KB |
Output is correct |
14 |
Correct |
44 ms |
68940 KB |
Output is correct |
15 |
Correct |
45 ms |
68980 KB |
Output is correct |
16 |
Correct |
40 ms |
68940 KB |
Output is correct |
17 |
Correct |
45 ms |
69004 KB |
Output is correct |
18 |
Correct |
43 ms |
68884 KB |
Output is correct |
19 |
Correct |
44 ms |
68952 KB |
Output is correct |
20 |
Correct |
43 ms |
68956 KB |
Output is correct |
21 |
Correct |
43 ms |
68960 KB |
Output is correct |
22 |
Correct |
50 ms |
68944 KB |
Output is correct |
23 |
Correct |
43 ms |
68884 KB |
Output is correct |
24 |
Correct |
41 ms |
68980 KB |
Output is correct |
25 |
Correct |
43 ms |
68948 KB |
Output is correct |
26 |
Correct |
43 ms |
68844 KB |
Output is correct |
27 |
Correct |
40 ms |
69032 KB |
Output is correct |
28 |
Correct |
40 ms |
68968 KB |
Output is correct |
29 |
Correct |
45 ms |
68956 KB |
Output is correct |
30 |
Correct |
40 ms |
68972 KB |
Output is correct |
31 |
Correct |
45 ms |
68952 KB |
Output is correct |
32 |
Correct |
50 ms |
68944 KB |
Output is correct |
33 |
Correct |
52 ms |
69008 KB |
Output is correct |
34 |
Correct |
46 ms |
69072 KB |
Output is correct |
35 |
Correct |
44 ms |
68972 KB |
Output is correct |
36 |
Correct |
54 ms |
69012 KB |
Output is correct |
37 |
Correct |
44 ms |
68948 KB |
Output is correct |
38 |
Correct |
50 ms |
68940 KB |
Output is correct |
39 |
Correct |
44 ms |
68964 KB |
Output is correct |
40 |
Correct |
46 ms |
68972 KB |
Output is correct |
41 |
Correct |
44 ms |
69012 KB |
Output is correct |
42 |
Correct |
43 ms |
69016 KB |
Output is correct |
43 |
Correct |
40 ms |
68948 KB |
Output is correct |
44 |
Correct |
43 ms |
69008 KB |
Output is correct |
45 |
Correct |
51 ms |
69132 KB |
Output is correct |
46 |
Correct |
49 ms |
69920 KB |
Output is correct |
47 |
Correct |
49 ms |
69864 KB |
Output is correct |
48 |
Correct |
51 ms |
69944 KB |
Output is correct |
49 |
Correct |
46 ms |
69348 KB |
Output is correct |
50 |
Correct |
45 ms |
69452 KB |
Output is correct |
51 |
Correct |
43 ms |
69412 KB |
Output is correct |
52 |
Correct |
48 ms |
70240 KB |
Output is correct |
53 |
Correct |
47 ms |
70348 KB |
Output is correct |
54 |
Correct |
47 ms |
70360 KB |
Output is correct |
55 |
Correct |
47 ms |
70352 KB |
Output is correct |
56 |
Correct |
47 ms |
70240 KB |
Output is correct |
57 |
Correct |
52 ms |
69804 KB |
Output is correct |
58 |
Correct |
53 ms |
69844 KB |
Output is correct |
59 |
Correct |
53 ms |
69808 KB |
Output is correct |
60 |
Correct |
52 ms |
69804 KB |
Output is correct |
61 |
Correct |
47 ms |
70200 KB |
Output is correct |
62 |
Correct |
49 ms |
70228 KB |
Output is correct |
63 |
Correct |
47 ms |
70244 KB |
Output is correct |
64 |
Correct |
49 ms |
69844 KB |
Output is correct |
65 |
Correct |
54 ms |
69932 KB |
Output is correct |
66 |
Correct |
48 ms |
69936 KB |
Output is correct |
67 |
Correct |
49 ms |
69876 KB |
Output is correct |
68 |
Correct |
44 ms |
69320 KB |
Output is correct |
69 |
Correct |
47 ms |
70220 KB |
Output is correct |
70 |
Correct |
46 ms |
70228 KB |
Output is correct |
71 |
Correct |
51 ms |
70220 KB |
Output is correct |
72 |
Correct |
51 ms |
69836 KB |
Output is correct |
73 |
Correct |
50 ms |
69768 KB |
Output is correct |
74 |
Correct |
47 ms |
70252 KB |
Output is correct |
75 |
Correct |
48 ms |
70192 KB |
Output is correct |
76 |
Correct |
53 ms |
70116 KB |
Output is correct |
77 |
Correct |
47 ms |
70164 KB |
Output is correct |
78 |
Correct |
46 ms |
70116 KB |
Output is correct |
79 |
Correct |
46 ms |
70240 KB |
Output is correct |
80 |
Correct |
47 ms |
70108 KB |
Output is correct |
81 |
Correct |
48 ms |
70136 KB |
Output is correct |
82 |
Correct |
48 ms |
70144 KB |
Output is correct |
83 |
Correct |
47 ms |
70116 KB |
Output is correct |
84 |
Correct |
48 ms |
70116 KB |
Output is correct |
85 |
Correct |
47 ms |
70096 KB |
Output is correct |
86 |
Correct |
49 ms |
70196 KB |
Output is correct |
87 |
Correct |
60 ms |
71524 KB |
Output is correct |
88 |
Correct |
94 ms |
77100 KB |
Output is correct |
89 |
Correct |
275 ms |
96504 KB |
Output is correct |
90 |
Correct |
268 ms |
96440 KB |
Output is correct |
91 |
Correct |
253 ms |
96448 KB |
Output is correct |
92 |
Correct |
128 ms |
84036 KB |
Output is correct |
93 |
Correct |
134 ms |
84124 KB |
Output is correct |
94 |
Correct |
128 ms |
83996 KB |
Output is correct |
95 |
Correct |
141 ms |
106456 KB |
Output is correct |
96 |
Correct |
152 ms |
106912 KB |
Output is correct |
97 |
Correct |
150 ms |
106992 KB |
Output is correct |
98 |
Correct |
142 ms |
106940 KB |
Output is correct |
99 |
Correct |
147 ms |
104796 KB |
Output is correct |
100 |
Correct |
343 ms |
90876 KB |
Output is correct |
101 |
Correct |
417 ms |
91348 KB |
Output is correct |
102 |
Correct |
359 ms |
91140 KB |
Output is correct |
103 |
Correct |
355 ms |
91184 KB |
Output is correct |
104 |
Correct |
180 ms |
104676 KB |
Output is correct |
105 |
Correct |
172 ms |
104740 KB |
Output is correct |
106 |
Correct |
169 ms |
104736 KB |
Output is correct |
107 |
Correct |
237 ms |
95808 KB |
Output is correct |
108 |
Correct |
245 ms |
95920 KB |
Output is correct |
109 |
Correct |
253 ms |
95964 KB |
Output is correct |
110 |
Correct |
123 ms |
83536 KB |
Output is correct |
111 |
Correct |
143 ms |
106464 KB |
Output is correct |
112 |
Correct |
134 ms |
106300 KB |
Output is correct |
113 |
Correct |
137 ms |
104020 KB |
Output is correct |
114 |
Correct |
347 ms |
90772 KB |
Output is correct |
115 |
Correct |
389 ms |
90552 KB |
Output is correct |
116 |
Correct |
157 ms |
104112 KB |
Output is correct |
117 |
Correct |
149 ms |
102800 KB |
Output is correct |
118 |
Correct |
150 ms |
102212 KB |
Output is correct |
119 |
Correct |
155 ms |
101960 KB |
Output is correct |
120 |
Correct |
139 ms |
102372 KB |
Output is correct |
121 |
Correct |
146 ms |
101976 KB |
Output is correct |
122 |
Correct |
140 ms |
101700 KB |
Output is correct |
123 |
Correct |
175 ms |
102908 KB |
Output is correct |
124 |
Correct |
192 ms |
102372 KB |
Output is correct |
125 |
Correct |
190 ms |
102116 KB |
Output is correct |
126 |
Correct |
173 ms |
102528 KB |
Output is correct |
127 |
Correct |
181 ms |
101980 KB |
Output is correct |
128 |
Correct |
160 ms |
101788 KB |
Output is correct |