#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];
ll 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;
}
node seg[400009];
ll lazy[400009];
void push(ll v, ll tl, ll tm, ll 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(ll v, ll tl, ll tr, ll l, ll r){
if(r < tl || tr < l)
return node();
if(l <= tl && tr <= r)
return seg[v];
else{
ll 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(ll v, ll tl, ll tr, ll l, ll r, ll 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{
ll 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);
seg[v] = merge(seg[2*v], seg[2*v+1]);
}
}
ll gettree(ll 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(ll idx, ll 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 |
19 ms |
24568 KB |
Output is correct |
2 |
Correct |
19 ms |
24572 KB |
Output is correct |
3 |
Correct |
22 ms |
24696 KB |
Output is correct |
4 |
Correct |
45 ms |
24696 KB |
Output is correct |
5 |
Correct |
97 ms |
24948 KB |
Output is correct |
6 |
Correct |
95 ms |
24952 KB |
Output is correct |
7 |
Correct |
97 ms |
24804 KB |
Output is correct |
8 |
Correct |
25 ms |
24952 KB |
Output is correct |
9 |
Correct |
20 ms |
24696 KB |
Output is correct |
10 |
Correct |
21 ms |
24700 KB |
Output is correct |
11 |
Correct |
61 ms |
24952 KB |
Output is correct |
12 |
Correct |
58 ms |
24952 KB |
Output is correct |
13 |
Correct |
58 ms |
24952 KB |
Output is correct |
14 |
Correct |
46 ms |
24828 KB |
Output is correct |
15 |
Correct |
103 ms |
24824 KB |
Output is correct |
16 |
Correct |
131 ms |
24952 KB |
Output is correct |
17 |
Correct |
121 ms |
24824 KB |
Output is correct |
18 |
Correct |
117 ms |
24896 KB |
Output is correct |
19 |
Correct |
45 ms |
24824 KB |
Output is correct |
20 |
Correct |
45 ms |
24824 KB |
Output is correct |
21 |
Correct |
48 ms |
24952 KB |
Output is correct |
22 |
Correct |
26 ms |
24696 KB |
Output is correct |
23 |
Correct |
35 ms |
24696 KB |
Output is correct |
24 |
Correct |
69 ms |
24824 KB |
Output is correct |
25 |
Correct |
82 ms |
24824 KB |
Output is correct |
26 |
Correct |
21 ms |
24696 KB |
Output is correct |
27 |
Correct |
53 ms |
24952 KB |
Output is correct |
28 |
Correct |
29 ms |
24824 KB |
Output is correct |
29 |
Correct |
26 ms |
24824 KB |
Output is correct |
30 |
Correct |
106 ms |
24824 KB |
Output is correct |
31 |
Correct |
56 ms |
24824 KB |
Output is correct |
32 |
Correct |
27 ms |
24696 KB |
Output is correct |
33 |
Correct |
52 ms |
24824 KB |
Output is correct |
34 |
Correct |
57 ms |
24824 KB |
Output is correct |
35 |
Correct |
64 ms |
24952 KB |
Output is correct |
36 |
Correct |
29 ms |
24824 KB |
Output is correct |
37 |
Correct |
33 ms |
24696 KB |
Output is correct |
38 |
Correct |
32 ms |
24700 KB |
Output is correct |
39 |
Correct |
52 ms |
24952 KB |
Output is correct |
40 |
Correct |
54 ms |
24824 KB |
Output is correct |
41 |
Correct |
56 ms |
24828 KB |
Output is correct |
42 |
Correct |
29 ms |
24824 KB |
Output is correct |
43 |
Correct |
32 ms |
24696 KB |
Output is correct |
44 |
Correct |
32 ms |
24696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
24568 KB |
Output is correct |
2 |
Correct |
19 ms |
24572 KB |
Output is correct |
3 |
Correct |
22 ms |
24696 KB |
Output is correct |
4 |
Correct |
45 ms |
24696 KB |
Output is correct |
5 |
Correct |
97 ms |
24948 KB |
Output is correct |
6 |
Correct |
95 ms |
24952 KB |
Output is correct |
7 |
Correct |
97 ms |
24804 KB |
Output is correct |
8 |
Correct |
25 ms |
24952 KB |
Output is correct |
9 |
Correct |
20 ms |
24696 KB |
Output is correct |
10 |
Correct |
21 ms |
24700 KB |
Output is correct |
11 |
Correct |
61 ms |
24952 KB |
Output is correct |
12 |
Correct |
58 ms |
24952 KB |
Output is correct |
13 |
Correct |
58 ms |
24952 KB |
Output is correct |
14 |
Correct |
46 ms |
24828 KB |
Output is correct |
15 |
Correct |
103 ms |
24824 KB |
Output is correct |
16 |
Correct |
131 ms |
24952 KB |
Output is correct |
17 |
Correct |
121 ms |
24824 KB |
Output is correct |
18 |
Correct |
117 ms |
24896 KB |
Output is correct |
19 |
Correct |
45 ms |
24824 KB |
Output is correct |
20 |
Correct |
45 ms |
24824 KB |
Output is correct |
21 |
Correct |
48 ms |
24952 KB |
Output is correct |
22 |
Correct |
26 ms |
24696 KB |
Output is correct |
23 |
Correct |
35 ms |
24696 KB |
Output is correct |
24 |
Correct |
69 ms |
24824 KB |
Output is correct |
25 |
Correct |
82 ms |
24824 KB |
Output is correct |
26 |
Correct |
21 ms |
24696 KB |
Output is correct |
27 |
Correct |
53 ms |
24952 KB |
Output is correct |
28 |
Correct |
29 ms |
24824 KB |
Output is correct |
29 |
Correct |
26 ms |
24824 KB |
Output is correct |
30 |
Correct |
106 ms |
24824 KB |
Output is correct |
31 |
Correct |
56 ms |
24824 KB |
Output is correct |
32 |
Correct |
27 ms |
24696 KB |
Output is correct |
33 |
Correct |
52 ms |
24824 KB |
Output is correct |
34 |
Correct |
57 ms |
24824 KB |
Output is correct |
35 |
Correct |
64 ms |
24952 KB |
Output is correct |
36 |
Correct |
29 ms |
24824 KB |
Output is correct |
37 |
Correct |
33 ms |
24696 KB |
Output is correct |
38 |
Correct |
32 ms |
24700 KB |
Output is correct |
39 |
Correct |
52 ms |
24952 KB |
Output is correct |
40 |
Correct |
54 ms |
24824 KB |
Output is correct |
41 |
Correct |
56 ms |
24828 KB |
Output is correct |
42 |
Correct |
29 ms |
24824 KB |
Output is correct |
43 |
Correct |
32 ms |
24696 KB |
Output is correct |
44 |
Correct |
32 ms |
24696 KB |
Output is correct |
45 |
Correct |
349 ms |
25208 KB |
Output is correct |
46 |
Execution timed out |
2089 ms |
26204 KB |
Time limit exceeded |
47 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
24568 KB |
Output is correct |
2 |
Correct |
19 ms |
24572 KB |
Output is correct |
3 |
Correct |
22 ms |
24696 KB |
Output is correct |
4 |
Correct |
45 ms |
24696 KB |
Output is correct |
5 |
Correct |
97 ms |
24948 KB |
Output is correct |
6 |
Correct |
95 ms |
24952 KB |
Output is correct |
7 |
Correct |
97 ms |
24804 KB |
Output is correct |
8 |
Correct |
25 ms |
24952 KB |
Output is correct |
9 |
Correct |
20 ms |
24696 KB |
Output is correct |
10 |
Correct |
21 ms |
24700 KB |
Output is correct |
11 |
Correct |
61 ms |
24952 KB |
Output is correct |
12 |
Correct |
58 ms |
24952 KB |
Output is correct |
13 |
Correct |
58 ms |
24952 KB |
Output is correct |
14 |
Correct |
46 ms |
24828 KB |
Output is correct |
15 |
Correct |
103 ms |
24824 KB |
Output is correct |
16 |
Correct |
131 ms |
24952 KB |
Output is correct |
17 |
Correct |
121 ms |
24824 KB |
Output is correct |
18 |
Correct |
117 ms |
24896 KB |
Output is correct |
19 |
Correct |
45 ms |
24824 KB |
Output is correct |
20 |
Correct |
45 ms |
24824 KB |
Output is correct |
21 |
Correct |
48 ms |
24952 KB |
Output is correct |
22 |
Correct |
26 ms |
24696 KB |
Output is correct |
23 |
Correct |
35 ms |
24696 KB |
Output is correct |
24 |
Correct |
69 ms |
24824 KB |
Output is correct |
25 |
Correct |
82 ms |
24824 KB |
Output is correct |
26 |
Correct |
21 ms |
24696 KB |
Output is correct |
27 |
Correct |
53 ms |
24952 KB |
Output is correct |
28 |
Correct |
29 ms |
24824 KB |
Output is correct |
29 |
Correct |
26 ms |
24824 KB |
Output is correct |
30 |
Correct |
106 ms |
24824 KB |
Output is correct |
31 |
Correct |
56 ms |
24824 KB |
Output is correct |
32 |
Correct |
27 ms |
24696 KB |
Output is correct |
33 |
Correct |
52 ms |
24824 KB |
Output is correct |
34 |
Correct |
57 ms |
24824 KB |
Output is correct |
35 |
Correct |
64 ms |
24952 KB |
Output is correct |
36 |
Correct |
29 ms |
24824 KB |
Output is correct |
37 |
Correct |
33 ms |
24696 KB |
Output is correct |
38 |
Correct |
32 ms |
24700 KB |
Output is correct |
39 |
Correct |
52 ms |
24952 KB |
Output is correct |
40 |
Correct |
54 ms |
24824 KB |
Output is correct |
41 |
Correct |
56 ms |
24828 KB |
Output is correct |
42 |
Correct |
29 ms |
24824 KB |
Output is correct |
43 |
Correct |
32 ms |
24696 KB |
Output is correct |
44 |
Correct |
32 ms |
24696 KB |
Output is correct |
45 |
Correct |
349 ms |
25208 KB |
Output is correct |
46 |
Execution timed out |
2089 ms |
26204 KB |
Time limit exceeded |
47 |
Halted |
0 ms |
0 KB |
- |