#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pull;
typedef pair<int, int> pii;
typedef pair<ld, ld> pld;
int n;
vector<int> querry;
int val[100009];
vector<int> g[100009];
int prt[100009];
int subsz[100009];
int dfs1(int v){
subsz[v] = 1;
for(auto u : g[v]){
prt[u] = v;
dfs1(u);
subsz[v] += subsz[u];
}
}
int eul[100009];
int chain[100009];
int cureul = 0;
int dfs2(int v){
//cout << v << " -- " << cureul << '\n';
eul[v] = cureul++;
if(eul[prt[v]] == eul[v]-1)
chain[v] = chain[prt[v]];
else
chain[v] = v;
int biggest = 0, id = 0;
for(auto u : g[v])
if(subsz[u] > biggest){
biggest = subsz[u];
id = u;
}
if(id != 0)
dfs2(id);
for(auto u : g[v])
if(u != id)
dfs2(u);
}
struct node{
map<int, int> mpp;
int ret = 0;
};
node merge(node n1, node n2){
node n = node();
n.ret = n1.ret+n2.ret;
auto it = n2.mpp.begin();
int cur = 0;
for(auto u : n1.mpp){
while(it != n2.mpp.end() && (*it).ff < u.ff){
cur += (*it).ss;
++it;
}
n.ret += cur*u.ss;
}
for(auto u : n1.mpp)
n.mpp[u.ff] += u.ss;
for(auto u : n2.mpp)
n.mpp[u.ff] += u.ss;
return n;
}
void mergeref(node &n1, node &n2, node &n){
n.ret = n1.ret+n2.ret;
n.mpp.clear();
auto it = n2.mpp.begin();
int cur = 0;
for(auto u : n1.mpp){
while(it != n2.mpp.end() && (*it).ff < u.ff){
cur += (*it).ss;
++it;
}
n.ret += cur*u.ss;
}
for(auto u : n1.mpp)
n.mpp[u.ff] += u.ss;
for(auto u : n2.mpp)
n.mpp[u.ff] += u.ss;
}
node seg[400009];
int lazy[400009];
void push(int v, int tl, int tm, int tr){
if(lazy[v] == 0) return;
seg[2*v].ret = seg[2*v+1].ret = 0;
seg[2*v].mpp.clear();
seg[2*v+1].mpp.clear();
seg[2*v].mpp[lazy[v]] = (tm-tl+1);
seg[2*v+1].mpp[lazy[v]] = (tr-tm);
lazy[2*v] = lazy[2*v+1] = lazy[v];
lazy[v] = 0;
}
node get(int v, int tl, int tr, int l, int r){
if(r < tl || tr < l)
return node();
if(l <= tl && tr <= r)
return seg[v];
else{
int tm = (tl+tr)/2;
push(v, tl, tm, tr);
return merge(get(2*v, tl, tm, l, r), get(2*v+1, tm+1, tr, l, r));
}
}
void upd(int v, int tl, int tr, int l, int r, int val){
if(r < tl || tr < l)
return;
if(l <= tl && tr <= r){
lazy[v] = val;
seg[v].ret = 0;
seg[v].mpp.clear();
seg[v].mpp[val] = (tr-tl+1);
}
else{
int tm = (tl+tr)/2;
push(v, tl, tm, tr);
upd(2*v, tl, tm, l, r, val);
upd(2*v+1, tm+1, tr, l, r, val);
mergeref(seg[2*v], seg[2*v+1], seg[v]);
}
}
int gettree(int idx){
node cur = node();
while(idx != 0){
cur = merge( get(1, 0, n-1, eul[chain[idx]], eul[idx]) ,cur);
idx = prt[chain[idx]];
}
return cur.ret;
}
void updtree(int idx, int val){
while(idx != 0){
upd(1, 0, n-1, eul[chain[idx]], eul[idx], val);
idx = prt[chain[idx]];
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
cin >> n;
for(int i = 1; i <= n; ++i)
cin >> val[i];
for(int i = 1; i < n; ++i){
int x, y;
cin >> x >> y;
g[x].pb(y);
querry.pb(y);
}
dfs1(1);
dfs2(1);
for(auto u : querry){
cout << gettree(u) << '\n';
updtree(u, val[u]);
}
}
Compilation message
construction.cpp: In function 'int dfs1(int)':
construction.cpp:27:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
construction.cpp: In function 'int dfs2(int)':
construction.cpp:49:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
24568 KB |
Output is correct |
2 |
Correct |
19 ms |
24696 KB |
Output is correct |
3 |
Correct |
21 ms |
24696 KB |
Output is correct |
4 |
Correct |
40 ms |
24696 KB |
Output is correct |
5 |
Correct |
75 ms |
24824 KB |
Output is correct |
6 |
Correct |
71 ms |
24824 KB |
Output is correct |
7 |
Correct |
74 ms |
24824 KB |
Output is correct |
8 |
Correct |
20 ms |
24696 KB |
Output is correct |
9 |
Correct |
20 ms |
24696 KB |
Output is correct |
10 |
Correct |
20 ms |
24696 KB |
Output is correct |
11 |
Correct |
48 ms |
24824 KB |
Output is correct |
12 |
Correct |
48 ms |
24952 KB |
Output is correct |
13 |
Correct |
47 ms |
24952 KB |
Output is correct |
14 |
Correct |
38 ms |
24824 KB |
Output is correct |
15 |
Correct |
78 ms |
24792 KB |
Output is correct |
16 |
Correct |
91 ms |
24824 KB |
Output is correct |
17 |
Correct |
94 ms |
24824 KB |
Output is correct |
18 |
Correct |
92 ms |
24820 KB |
Output is correct |
19 |
Correct |
38 ms |
24832 KB |
Output is correct |
20 |
Correct |
38 ms |
24824 KB |
Output is correct |
21 |
Correct |
39 ms |
24824 KB |
Output is correct |
22 |
Correct |
23 ms |
24696 KB |
Output is correct |
23 |
Correct |
30 ms |
24828 KB |
Output is correct |
24 |
Correct |
48 ms |
24696 KB |
Output is correct |
25 |
Correct |
64 ms |
24952 KB |
Output is correct |
26 |
Correct |
20 ms |
24696 KB |
Output is correct |
27 |
Correct |
43 ms |
24828 KB |
Output is correct |
28 |
Correct |
26 ms |
24824 KB |
Output is correct |
29 |
Correct |
23 ms |
24696 KB |
Output is correct |
30 |
Correct |
76 ms |
24824 KB |
Output is correct |
31 |
Correct |
44 ms |
24696 KB |
Output is correct |
32 |
Correct |
25 ms |
24696 KB |
Output is correct |
33 |
Correct |
42 ms |
24824 KB |
Output is correct |
34 |
Correct |
47 ms |
24824 KB |
Output is correct |
35 |
Correct |
50 ms |
24952 KB |
Output is correct |
36 |
Correct |
25 ms |
24728 KB |
Output is correct |
37 |
Correct |
28 ms |
24696 KB |
Output is correct |
38 |
Correct |
28 ms |
24696 KB |
Output is correct |
39 |
Correct |
42 ms |
24824 KB |
Output is correct |
40 |
Correct |
45 ms |
24824 KB |
Output is correct |
41 |
Correct |
47 ms |
24824 KB |
Output is correct |
42 |
Correct |
26 ms |
24952 KB |
Output is correct |
43 |
Correct |
27 ms |
24700 KB |
Output is correct |
44 |
Correct |
28 ms |
24696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
24568 KB |
Output is correct |
2 |
Correct |
19 ms |
24696 KB |
Output is correct |
3 |
Correct |
21 ms |
24696 KB |
Output is correct |
4 |
Correct |
40 ms |
24696 KB |
Output is correct |
5 |
Correct |
75 ms |
24824 KB |
Output is correct |
6 |
Correct |
71 ms |
24824 KB |
Output is correct |
7 |
Correct |
74 ms |
24824 KB |
Output is correct |
8 |
Correct |
20 ms |
24696 KB |
Output is correct |
9 |
Correct |
20 ms |
24696 KB |
Output is correct |
10 |
Correct |
20 ms |
24696 KB |
Output is correct |
11 |
Correct |
48 ms |
24824 KB |
Output is correct |
12 |
Correct |
48 ms |
24952 KB |
Output is correct |
13 |
Correct |
47 ms |
24952 KB |
Output is correct |
14 |
Correct |
38 ms |
24824 KB |
Output is correct |
15 |
Correct |
78 ms |
24792 KB |
Output is correct |
16 |
Correct |
91 ms |
24824 KB |
Output is correct |
17 |
Correct |
94 ms |
24824 KB |
Output is correct |
18 |
Correct |
92 ms |
24820 KB |
Output is correct |
19 |
Correct |
38 ms |
24832 KB |
Output is correct |
20 |
Correct |
38 ms |
24824 KB |
Output is correct |
21 |
Correct |
39 ms |
24824 KB |
Output is correct |
22 |
Correct |
23 ms |
24696 KB |
Output is correct |
23 |
Correct |
30 ms |
24828 KB |
Output is correct |
24 |
Correct |
48 ms |
24696 KB |
Output is correct |
25 |
Correct |
64 ms |
24952 KB |
Output is correct |
26 |
Correct |
20 ms |
24696 KB |
Output is correct |
27 |
Correct |
43 ms |
24828 KB |
Output is correct |
28 |
Correct |
26 ms |
24824 KB |
Output is correct |
29 |
Correct |
23 ms |
24696 KB |
Output is correct |
30 |
Correct |
76 ms |
24824 KB |
Output is correct |
31 |
Correct |
44 ms |
24696 KB |
Output is correct |
32 |
Correct |
25 ms |
24696 KB |
Output is correct |
33 |
Correct |
42 ms |
24824 KB |
Output is correct |
34 |
Correct |
47 ms |
24824 KB |
Output is correct |
35 |
Correct |
50 ms |
24952 KB |
Output is correct |
36 |
Correct |
25 ms |
24728 KB |
Output is correct |
37 |
Correct |
28 ms |
24696 KB |
Output is correct |
38 |
Correct |
28 ms |
24696 KB |
Output is correct |
39 |
Correct |
42 ms |
24824 KB |
Output is correct |
40 |
Correct |
45 ms |
24824 KB |
Output is correct |
41 |
Correct |
47 ms |
24824 KB |
Output is correct |
42 |
Correct |
26 ms |
24952 KB |
Output is correct |
43 |
Correct |
27 ms |
24700 KB |
Output is correct |
44 |
Correct |
28 ms |
24696 KB |
Output is correct |
45 |
Correct |
270 ms |
25116 KB |
Output is correct |
46 |
Execution timed out |
2066 ms |
25772 KB |
Time limit exceeded |
47 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
24568 KB |
Output is correct |
2 |
Correct |
19 ms |
24696 KB |
Output is correct |
3 |
Correct |
21 ms |
24696 KB |
Output is correct |
4 |
Correct |
40 ms |
24696 KB |
Output is correct |
5 |
Correct |
75 ms |
24824 KB |
Output is correct |
6 |
Correct |
71 ms |
24824 KB |
Output is correct |
7 |
Correct |
74 ms |
24824 KB |
Output is correct |
8 |
Correct |
20 ms |
24696 KB |
Output is correct |
9 |
Correct |
20 ms |
24696 KB |
Output is correct |
10 |
Correct |
20 ms |
24696 KB |
Output is correct |
11 |
Correct |
48 ms |
24824 KB |
Output is correct |
12 |
Correct |
48 ms |
24952 KB |
Output is correct |
13 |
Correct |
47 ms |
24952 KB |
Output is correct |
14 |
Correct |
38 ms |
24824 KB |
Output is correct |
15 |
Correct |
78 ms |
24792 KB |
Output is correct |
16 |
Correct |
91 ms |
24824 KB |
Output is correct |
17 |
Correct |
94 ms |
24824 KB |
Output is correct |
18 |
Correct |
92 ms |
24820 KB |
Output is correct |
19 |
Correct |
38 ms |
24832 KB |
Output is correct |
20 |
Correct |
38 ms |
24824 KB |
Output is correct |
21 |
Correct |
39 ms |
24824 KB |
Output is correct |
22 |
Correct |
23 ms |
24696 KB |
Output is correct |
23 |
Correct |
30 ms |
24828 KB |
Output is correct |
24 |
Correct |
48 ms |
24696 KB |
Output is correct |
25 |
Correct |
64 ms |
24952 KB |
Output is correct |
26 |
Correct |
20 ms |
24696 KB |
Output is correct |
27 |
Correct |
43 ms |
24828 KB |
Output is correct |
28 |
Correct |
26 ms |
24824 KB |
Output is correct |
29 |
Correct |
23 ms |
24696 KB |
Output is correct |
30 |
Correct |
76 ms |
24824 KB |
Output is correct |
31 |
Correct |
44 ms |
24696 KB |
Output is correct |
32 |
Correct |
25 ms |
24696 KB |
Output is correct |
33 |
Correct |
42 ms |
24824 KB |
Output is correct |
34 |
Correct |
47 ms |
24824 KB |
Output is correct |
35 |
Correct |
50 ms |
24952 KB |
Output is correct |
36 |
Correct |
25 ms |
24728 KB |
Output is correct |
37 |
Correct |
28 ms |
24696 KB |
Output is correct |
38 |
Correct |
28 ms |
24696 KB |
Output is correct |
39 |
Correct |
42 ms |
24824 KB |
Output is correct |
40 |
Correct |
45 ms |
24824 KB |
Output is correct |
41 |
Correct |
47 ms |
24824 KB |
Output is correct |
42 |
Correct |
26 ms |
24952 KB |
Output is correct |
43 |
Correct |
27 ms |
24700 KB |
Output is correct |
44 |
Correct |
28 ms |
24696 KB |
Output is correct |
45 |
Correct |
270 ms |
25116 KB |
Output is correct |
46 |
Execution timed out |
2066 ms |
25772 KB |
Time limit exceeded |
47 |
Halted |
0 ms |
0 KB |
- |