#include <bits/stdc++.h>
#define owo(i,a, b) for(int i=(a); i<(b); ++i)
#define uwu(i,a, b) for(int i=(a)-1; i>=(b); --i)
#define senpai push_back
#define ttgl pair<int, int>
#define ayaya cout<<"debug"<<endl
using namespace std;
using ll = long long;
using ld = long double;
const ll MOD = 1e9+7;
const double PI = acos(-1);
const int INF = 0x3f3f3f3f;
const int NINF = 0x3f3f3f40;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const ll NINFLL = 0x3f3f3f3f3f3f3f40;
const int mxN = 100001;
int n;
int cnt = 0;
ttgl inout[mxN];
ttgl edges[mxN];
set<int> stt;
map<int, int> mp;
int arr[mxN];
int seg[4*mxN];
int par[mxN][18];
vector<int> adj[mxN];
void upd(int p, int val) {
for(seg[p+=2*n] = val; p>1; p>>=1) seg[p>>1] = max(seg[p], seg[p^1]);
}
int qmax(int l, int r) {
int res = 0;
r++;
for(l+=2*n, r+=2*n; l<r; l>>=1, r>>=1) {
if(l&1) res= max(res, seg[l++]);
if(r&1) res = max(res, seg[--r]);
}
return res;
}
int BIT[mxN];
void add(int i, int val) {
while(i<=n) {
BIT[i]+= val;
i +=i&-i;
}
}
void del(int i) {
while(i<=n) {
BIT[i] = 0;
i +=i&-i;
}
}
int sum (int i) {
int res = 0;
while(i>0) {
res+=BIT[i];
i-=i&-i;
}
return res;
}
void dfs(int u, int p) {
par[u][0] = p;
inout[u].first = cnt++;
for(int v: adj[u]) {
if(v==p)continue;
dfs(v, u);
}
inout[u].second = cnt++;
}
int query(int k) {
//cout<<"\n";
//cout<<k<<" "<<inout[k].first<<" "<<inout[k].second<<" "<<qmax(inout[k].first, inout[k].second)<<" hi\n";
return qmax(inout[k].first, inout[k].second);
}
int main()
{
//freopen("filename.in", "r", stdin);
//freopen("filename.out", "w", stdout);
cin.tie(0)->sync_with_stdio(0);
cin>>n;
owo(i, 0, n) {
cin>>arr[i];
stt.insert(arr[i]);
}
int idx = 0;
auto it = stt.begin();
while(it!=stt.end()) {
mp[*it] = idx++;
it++;
}
owo(i, 0, n) {
arr[i] = mp[arr[i]];
}
owo(i, 0, n-1) {
cin>>edges[i].first>>edges[i].second;
edges[i].first--;
edges[i].second--;
adj[edges[i].first].senpai(edges[i].second);
adj[edges[i].second].senpai(edges[i].first);
}
dfs(0, -1);
owo(lg, 1, 18) {
owo(i, 0, n) {
if(par[i][lg-1]==-1) {
par[i][lg] = -1;
}else {
par[i][lg] = par[par[i][lg-1]][lg-1];
}
}
}
owo(i, 0, 2*mxN) {
seg[i] = -1;
}
upd(inout[0].first, 0);
owo(i, 1, n) {
int a = edges[i-1].first;
int b = edges[i-1].second;
int ans = 0;
vector<int> path;
//cout<<i<<"\n";
int val = query(a)==0 ? arr[a] : arr[edges[query(a)-1].second];
while(a!=-1) {
int num= 1;
//cout<<a<<" ";
uwu(i, 18, 0) {
if(par[a][i]!=-1&&query(par[a][i])==query(a)) {
a = par[a][i];
num+=(1<<i);
}
}
//cout<<a<<" "<<num<<" "<<val<<" "<<sum(val+1)<<"\n";
ans+=num*sum(val);
add(val+1, num);
path.senpai(val+1);
a = par[a][0];
val = arr[edges[query(a)-1].second];
//cout<<query(a)<<"\n";
}
for(int k: path) {
del(k);
}
cout<<ans<<"\n";
upd(inout[b].first, i);
//cout<<inout[b].first<<" "<<i<<" cool\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
3456 KB |
Output is correct |
2 |
Correct |
6 ms |
3456 KB |
Output is correct |
3 |
Correct |
6 ms |
3456 KB |
Output is correct |
4 |
Correct |
7 ms |
3584 KB |
Output is correct |
5 |
Correct |
7 ms |
3584 KB |
Output is correct |
6 |
Correct |
7 ms |
3584 KB |
Output is correct |
7 |
Correct |
7 ms |
3584 KB |
Output is correct |
8 |
Correct |
7 ms |
3584 KB |
Output is correct |
9 |
Correct |
7 ms |
3712 KB |
Output is correct |
10 |
Correct |
7 ms |
3712 KB |
Output is correct |
11 |
Correct |
7 ms |
3584 KB |
Output is correct |
12 |
Correct |
7 ms |
3584 KB |
Output is correct |
13 |
Correct |
8 ms |
3712 KB |
Output is correct |
14 |
Correct |
7 ms |
3584 KB |
Output is correct |
15 |
Correct |
8 ms |
3584 KB |
Output is correct |
16 |
Correct |
9 ms |
3584 KB |
Output is correct |
17 |
Correct |
7 ms |
3584 KB |
Output is correct |
18 |
Correct |
9 ms |
3584 KB |
Output is correct |
19 |
Correct |
7 ms |
3584 KB |
Output is correct |
20 |
Correct |
8 ms |
3584 KB |
Output is correct |
21 |
Correct |
8 ms |
3584 KB |
Output is correct |
22 |
Correct |
7 ms |
3584 KB |
Output is correct |
23 |
Correct |
7 ms |
3584 KB |
Output is correct |
24 |
Correct |
7 ms |
3584 KB |
Output is correct |
25 |
Correct |
7 ms |
3584 KB |
Output is correct |
26 |
Correct |
7 ms |
3584 KB |
Output is correct |
27 |
Correct |
7 ms |
3584 KB |
Output is correct |
28 |
Correct |
7 ms |
3584 KB |
Output is correct |
29 |
Correct |
7 ms |
3584 KB |
Output is correct |
30 |
Correct |
7 ms |
3584 KB |
Output is correct |
31 |
Correct |
7 ms |
3584 KB |
Output is correct |
32 |
Correct |
8 ms |
3584 KB |
Output is correct |
33 |
Correct |
7 ms |
3584 KB |
Output is correct |
34 |
Correct |
7 ms |
3584 KB |
Output is correct |
35 |
Correct |
7 ms |
3584 KB |
Output is correct |
36 |
Correct |
7 ms |
3584 KB |
Output is correct |
37 |
Correct |
7 ms |
3584 KB |
Output is correct |
38 |
Correct |
7 ms |
3584 KB |
Output is correct |
39 |
Correct |
7 ms |
3584 KB |
Output is correct |
40 |
Correct |
8 ms |
3584 KB |
Output is correct |
41 |
Correct |
7 ms |
3584 KB |
Output is correct |
42 |
Correct |
7 ms |
3584 KB |
Output is correct |
43 |
Correct |
7 ms |
3584 KB |
Output is correct |
44 |
Correct |
7 ms |
3584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
3456 KB |
Output is correct |
2 |
Correct |
6 ms |
3456 KB |
Output is correct |
3 |
Correct |
6 ms |
3456 KB |
Output is correct |
4 |
Correct |
7 ms |
3584 KB |
Output is correct |
5 |
Correct |
7 ms |
3584 KB |
Output is correct |
6 |
Correct |
7 ms |
3584 KB |
Output is correct |
7 |
Correct |
7 ms |
3584 KB |
Output is correct |
8 |
Correct |
7 ms |
3584 KB |
Output is correct |
9 |
Correct |
7 ms |
3712 KB |
Output is correct |
10 |
Correct |
7 ms |
3712 KB |
Output is correct |
11 |
Correct |
7 ms |
3584 KB |
Output is correct |
12 |
Correct |
7 ms |
3584 KB |
Output is correct |
13 |
Correct |
8 ms |
3712 KB |
Output is correct |
14 |
Correct |
7 ms |
3584 KB |
Output is correct |
15 |
Correct |
8 ms |
3584 KB |
Output is correct |
16 |
Correct |
9 ms |
3584 KB |
Output is correct |
17 |
Correct |
7 ms |
3584 KB |
Output is correct |
18 |
Correct |
9 ms |
3584 KB |
Output is correct |
19 |
Correct |
7 ms |
3584 KB |
Output is correct |
20 |
Correct |
8 ms |
3584 KB |
Output is correct |
21 |
Correct |
8 ms |
3584 KB |
Output is correct |
22 |
Correct |
7 ms |
3584 KB |
Output is correct |
23 |
Correct |
7 ms |
3584 KB |
Output is correct |
24 |
Correct |
7 ms |
3584 KB |
Output is correct |
25 |
Correct |
7 ms |
3584 KB |
Output is correct |
26 |
Correct |
7 ms |
3584 KB |
Output is correct |
27 |
Correct |
7 ms |
3584 KB |
Output is correct |
28 |
Correct |
7 ms |
3584 KB |
Output is correct |
29 |
Correct |
7 ms |
3584 KB |
Output is correct |
30 |
Correct |
7 ms |
3584 KB |
Output is correct |
31 |
Correct |
7 ms |
3584 KB |
Output is correct |
32 |
Correct |
8 ms |
3584 KB |
Output is correct |
33 |
Correct |
7 ms |
3584 KB |
Output is correct |
34 |
Correct |
7 ms |
3584 KB |
Output is correct |
35 |
Correct |
7 ms |
3584 KB |
Output is correct |
36 |
Correct |
7 ms |
3584 KB |
Output is correct |
37 |
Correct |
7 ms |
3584 KB |
Output is correct |
38 |
Correct |
7 ms |
3584 KB |
Output is correct |
39 |
Correct |
7 ms |
3584 KB |
Output is correct |
40 |
Correct |
8 ms |
3584 KB |
Output is correct |
41 |
Correct |
7 ms |
3584 KB |
Output is correct |
42 |
Correct |
7 ms |
3584 KB |
Output is correct |
43 |
Correct |
7 ms |
3584 KB |
Output is correct |
44 |
Correct |
7 ms |
3584 KB |
Output is correct |
45 |
Correct |
8 ms |
3712 KB |
Output is correct |
46 |
Correct |
17 ms |
4352 KB |
Output is correct |
47 |
Correct |
18 ms |
4352 KB |
Output is correct |
48 |
Correct |
17 ms |
4352 KB |
Output is correct |
49 |
Correct |
14 ms |
4608 KB |
Output is correct |
50 |
Correct |
14 ms |
4656 KB |
Output is correct |
51 |
Correct |
18 ms |
4608 KB |
Output is correct |
52 |
Correct |
16 ms |
4480 KB |
Output is correct |
53 |
Correct |
16 ms |
4480 KB |
Output is correct |
54 |
Correct |
16 ms |
4480 KB |
Output is correct |
55 |
Correct |
16 ms |
4480 KB |
Output is correct |
56 |
Correct |
16 ms |
4480 KB |
Output is correct |
57 |
Correct |
34 ms |
4472 KB |
Output is correct |
58 |
Correct |
33 ms |
4480 KB |
Output is correct |
59 |
Correct |
25 ms |
4480 KB |
Output is correct |
60 |
Correct |
25 ms |
4480 KB |
Output is correct |
61 |
Correct |
17 ms |
4480 KB |
Output is correct |
62 |
Correct |
17 ms |
4480 KB |
Output is correct |
63 |
Correct |
17 ms |
4480 KB |
Output is correct |
64 |
Correct |
15 ms |
3968 KB |
Output is correct |
65 |
Correct |
15 ms |
3968 KB |
Output is correct |
66 |
Correct |
16 ms |
4096 KB |
Output is correct |
67 |
Correct |
16 ms |
4224 KB |
Output is correct |
68 |
Correct |
12 ms |
4224 KB |
Output is correct |
69 |
Correct |
16 ms |
4480 KB |
Output is correct |
70 |
Correct |
14 ms |
4096 KB |
Output is correct |
71 |
Correct |
15 ms |
4144 KB |
Output is correct |
72 |
Correct |
24 ms |
4352 KB |
Output is correct |
73 |
Correct |
23 ms |
3968 KB |
Output is correct |
74 |
Correct |
15 ms |
4096 KB |
Output is correct |
75 |
Correct |
16 ms |
4480 KB |
Output is correct |
76 |
Correct |
15 ms |
4352 KB |
Output is correct |
77 |
Correct |
15 ms |
4352 KB |
Output is correct |
78 |
Correct |
14 ms |
4096 KB |
Output is correct |
79 |
Correct |
13 ms |
4096 KB |
Output is correct |
80 |
Correct |
13 ms |
4096 KB |
Output is correct |
81 |
Correct |
17 ms |
4480 KB |
Output is correct |
82 |
Correct |
17 ms |
4352 KB |
Output is correct |
83 |
Correct |
16 ms |
4480 KB |
Output is correct |
84 |
Correct |
16 ms |
4096 KB |
Output is correct |
85 |
Correct |
15 ms |
4088 KB |
Output is correct |
86 |
Correct |
15 ms |
3968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
3456 KB |
Output is correct |
2 |
Correct |
6 ms |
3456 KB |
Output is correct |
3 |
Correct |
6 ms |
3456 KB |
Output is correct |
4 |
Correct |
7 ms |
3584 KB |
Output is correct |
5 |
Correct |
7 ms |
3584 KB |
Output is correct |
6 |
Correct |
7 ms |
3584 KB |
Output is correct |
7 |
Correct |
7 ms |
3584 KB |
Output is correct |
8 |
Correct |
7 ms |
3584 KB |
Output is correct |
9 |
Correct |
7 ms |
3712 KB |
Output is correct |
10 |
Correct |
7 ms |
3712 KB |
Output is correct |
11 |
Correct |
7 ms |
3584 KB |
Output is correct |
12 |
Correct |
7 ms |
3584 KB |
Output is correct |
13 |
Correct |
8 ms |
3712 KB |
Output is correct |
14 |
Correct |
7 ms |
3584 KB |
Output is correct |
15 |
Correct |
8 ms |
3584 KB |
Output is correct |
16 |
Correct |
9 ms |
3584 KB |
Output is correct |
17 |
Correct |
7 ms |
3584 KB |
Output is correct |
18 |
Correct |
9 ms |
3584 KB |
Output is correct |
19 |
Correct |
7 ms |
3584 KB |
Output is correct |
20 |
Correct |
8 ms |
3584 KB |
Output is correct |
21 |
Correct |
8 ms |
3584 KB |
Output is correct |
22 |
Correct |
7 ms |
3584 KB |
Output is correct |
23 |
Correct |
7 ms |
3584 KB |
Output is correct |
24 |
Correct |
7 ms |
3584 KB |
Output is correct |
25 |
Correct |
7 ms |
3584 KB |
Output is correct |
26 |
Correct |
7 ms |
3584 KB |
Output is correct |
27 |
Correct |
7 ms |
3584 KB |
Output is correct |
28 |
Correct |
7 ms |
3584 KB |
Output is correct |
29 |
Correct |
7 ms |
3584 KB |
Output is correct |
30 |
Correct |
7 ms |
3584 KB |
Output is correct |
31 |
Correct |
7 ms |
3584 KB |
Output is correct |
32 |
Correct |
8 ms |
3584 KB |
Output is correct |
33 |
Correct |
7 ms |
3584 KB |
Output is correct |
34 |
Correct |
7 ms |
3584 KB |
Output is correct |
35 |
Correct |
7 ms |
3584 KB |
Output is correct |
36 |
Correct |
7 ms |
3584 KB |
Output is correct |
37 |
Correct |
7 ms |
3584 KB |
Output is correct |
38 |
Correct |
7 ms |
3584 KB |
Output is correct |
39 |
Correct |
7 ms |
3584 KB |
Output is correct |
40 |
Correct |
8 ms |
3584 KB |
Output is correct |
41 |
Correct |
7 ms |
3584 KB |
Output is correct |
42 |
Correct |
7 ms |
3584 KB |
Output is correct |
43 |
Correct |
7 ms |
3584 KB |
Output is correct |
44 |
Correct |
7 ms |
3584 KB |
Output is correct |
45 |
Correct |
8 ms |
3712 KB |
Output is correct |
46 |
Correct |
17 ms |
4352 KB |
Output is correct |
47 |
Correct |
18 ms |
4352 KB |
Output is correct |
48 |
Correct |
17 ms |
4352 KB |
Output is correct |
49 |
Correct |
14 ms |
4608 KB |
Output is correct |
50 |
Correct |
14 ms |
4656 KB |
Output is correct |
51 |
Correct |
18 ms |
4608 KB |
Output is correct |
52 |
Correct |
16 ms |
4480 KB |
Output is correct |
53 |
Correct |
16 ms |
4480 KB |
Output is correct |
54 |
Correct |
16 ms |
4480 KB |
Output is correct |
55 |
Correct |
16 ms |
4480 KB |
Output is correct |
56 |
Correct |
16 ms |
4480 KB |
Output is correct |
57 |
Correct |
34 ms |
4472 KB |
Output is correct |
58 |
Correct |
33 ms |
4480 KB |
Output is correct |
59 |
Correct |
25 ms |
4480 KB |
Output is correct |
60 |
Correct |
25 ms |
4480 KB |
Output is correct |
61 |
Correct |
17 ms |
4480 KB |
Output is correct |
62 |
Correct |
17 ms |
4480 KB |
Output is correct |
63 |
Correct |
17 ms |
4480 KB |
Output is correct |
64 |
Correct |
15 ms |
3968 KB |
Output is correct |
65 |
Correct |
15 ms |
3968 KB |
Output is correct |
66 |
Correct |
16 ms |
4096 KB |
Output is correct |
67 |
Correct |
16 ms |
4224 KB |
Output is correct |
68 |
Correct |
12 ms |
4224 KB |
Output is correct |
69 |
Correct |
16 ms |
4480 KB |
Output is correct |
70 |
Correct |
14 ms |
4096 KB |
Output is correct |
71 |
Correct |
15 ms |
4144 KB |
Output is correct |
72 |
Correct |
24 ms |
4352 KB |
Output is correct |
73 |
Correct |
23 ms |
3968 KB |
Output is correct |
74 |
Correct |
15 ms |
4096 KB |
Output is correct |
75 |
Correct |
16 ms |
4480 KB |
Output is correct |
76 |
Correct |
15 ms |
4352 KB |
Output is correct |
77 |
Correct |
15 ms |
4352 KB |
Output is correct |
78 |
Correct |
14 ms |
4096 KB |
Output is correct |
79 |
Correct |
13 ms |
4096 KB |
Output is correct |
80 |
Correct |
13 ms |
4096 KB |
Output is correct |
81 |
Correct |
17 ms |
4480 KB |
Output is correct |
82 |
Correct |
17 ms |
4352 KB |
Output is correct |
83 |
Correct |
16 ms |
4480 KB |
Output is correct |
84 |
Correct |
16 ms |
4096 KB |
Output is correct |
85 |
Correct |
15 ms |
4088 KB |
Output is correct |
86 |
Correct |
15 ms |
3968 KB |
Output is correct |
87 |
Correct |
39 ms |
5760 KB |
Output is correct |
88 |
Correct |
122 ms |
10232 KB |
Output is correct |
89 |
Correct |
593 ms |
26728 KB |
Output is correct |
90 |
Correct |
582 ms |
26744 KB |
Output is correct |
91 |
Correct |
587 ms |
26616 KB |
Output is correct |
92 |
Correct |
355 ms |
30712 KB |
Output is correct |
93 |
Correct |
369 ms |
30700 KB |
Output is correct |
94 |
Correct |
388 ms |
30688 KB |
Output is correct |
95 |
Correct |
457 ms |
29304 KB |
Output is correct |
96 |
Correct |
449 ms |
29080 KB |
Output is correct |
97 |
Correct |
480 ms |
29312 KB |
Output is correct |
98 |
Correct |
441 ms |
29048 KB |
Output is correct |
99 |
Correct |
472 ms |
29212 KB |
Output is correct |
100 |
Correct |
1092 ms |
26636 KB |
Output is correct |
101 |
Correct |
1089 ms |
28748 KB |
Output is correct |
102 |
Correct |
1097 ms |
28792 KB |
Output is correct |
103 |
Correct |
1084 ms |
28700 KB |
Output is correct |
104 |
Correct |
560 ms |
31096 KB |
Output is correct |
105 |
Correct |
570 ms |
30968 KB |
Output is correct |
106 |
Correct |
596 ms |
30968 KB |
Output is correct |
107 |
Correct |
473 ms |
18172 KB |
Output is correct |
108 |
Correct |
473 ms |
18296 KB |
Output is correct |
109 |
Correct |
560 ms |
21368 KB |
Output is correct |
110 |
Correct |
268 ms |
22520 KB |
Output is correct |
111 |
Correct |
439 ms |
30840 KB |
Output is correct |
112 |
Correct |
363 ms |
20984 KB |
Output is correct |
113 |
Correct |
361 ms |
20704 KB |
Output is correct |
114 |
Correct |
1058 ms |
28408 KB |
Output is correct |
115 |
Correct |
993 ms |
18508 KB |
Output is correct |
116 |
Correct |
560 ms |
20664 KB |
Output is correct |
117 |
Correct |
442 ms |
29188 KB |
Output is correct |
118 |
Correct |
420 ms |
28604 KB |
Output is correct |
119 |
Correct |
406 ms |
28664 KB |
Output is correct |
120 |
Correct |
340 ms |
19424 KB |
Output is correct |
121 |
Correct |
335 ms |
19192 KB |
Output is correct |
122 |
Correct |
320 ms |
18808 KB |
Output is correct |
123 |
Correct |
575 ms |
29208 KB |
Output is correct |
124 |
Correct |
542 ms |
28792 KB |
Output is correct |
125 |
Correct |
504 ms |
28536 KB |
Output is correct |
126 |
Correct |
463 ms |
19608 KB |
Output is correct |
127 |
Correct |
432 ms |
19064 KB |
Output is correct |
128 |
Correct |
453 ms |
18936 KB |
Output is correct |