//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#include <bits/stdc++.h>
using namespace std;
#define int long long
int bit[200005];
int lowbit(int x){return x&-x;}
int qsum(int pl){
int ret=0;
while(pl){
ret+=bit[pl];
pl-=lowbit(pl);
}
return ret;
}
vector<pair<int,int>>uvc;
void updbit(int pl,int v){
uvc.push_back({pl,v});
while(pl<=200000){
bit[pl]+=v;
pl+=lowbit(pl);
}
}
void undo(){
int pl=uvc.back().first;int v=uvc.back().second;
v*=-1;
while(pl<=200000){
bit[pl]+=v;
pl+=lowbit(pl);
}
uvc.pop_back();
}
int n;
int ar[200005];
vector<int>e[200005];
int dph[200005];
int hd[200005];
int sbsz[200005];
int fa[200005];
set<pair<pair<int,int>,int>>st[200005];
pair<int,int>qar[200005];
void dfssbsz(int nw,int pa){
dph[nw]=dph[pa]+1;
sbsz[nw]=1;
for(int i=0;i<e[nw].size();i++){
int v=e[nw][i];
dfssbsz(v,nw);
sbsz[nw]+=sbsz[v];
}
for(int i=1;i<e[nw].size();i++){
if(sbsz[e[nw][i]]>sbsz[e[nw][0]]){
swap(e[nw][i],e[nw][0]);
}
}
}
void dfshd(int nw,int head){
hd[nw]=head;
if(e[nw].size()){
dfshd(e[nw][0],head);
}
for(int i=1;i<e[nw].size();i++){
dfshd(e[nw][i],e[nw][i]);
}
}
void lisanhua(){
vector<int>vc;
for(int i=1;i<=n;i++){
vc.push_back(ar[i]);
}
sort(vc.begin(),vc.end());
for(int i=1;i<=n;i++){
ar[i]=1+lower_bound(vc.begin(),vc.end(),ar[i])-vc.begin();
}
}
signed main()
{
cin>>n;
for(int i=1;i<=n;i++){
cin>>ar[i];
}
lisanhua();
for(int i=1;i<=n-1;i++){
cin>>qar[i].first>>qar[i].second;
fa[qar[i].second]=qar[i].first;
e[qar[i].first].push_back(qar[i].second);
}
dfssbsz(1,0);
dfshd(1,1);
/*for(int i=1;i<=n;i++){
cout<<hd[i]<<" ";
}cout<<endl;*/
st[1].insert({{1,1},ar[1]});
for(int qqq=1;qqq<=n-1;qqq++){
int nw=qar[qqq].first;
vector<pair<pair<int,int>,int>>fvc;
while(nw!=0){
vector<pair<pair<int,int>,int>>vc;
int hdd=hd[nw];
while(st[hdd].size()){
auto it=st[hdd].begin();
pair<pair<int,int>,int>pr=*it;
if(pr.first.second<=dph[nw]){
vc.push_back(pr);
st[hdd].erase(it);
continue;
}
if(pr.first.first<=dph[nw]){
int ogr=pr.first.second;
pr.first.second=dph[nw];
vc.push_back(pr);
st[hdd].erase(it);
pr={ {dph[nw]+1,ogr} ,pr.second };
st[hdd].insert(pr);
break;
}
break;
}
reverse(vc.begin(),vc.end());
for(int i=0;i<vc.size();i++){
fvc.push_back(vc[i]);
}
nw=fa[hd[nw]];
}
int ans=0;
for(int i=0;i<fvc.size();i++){
int len=fvc[i].first.second-fvc[i].first.first+1;
ans+=len*qsum(fvc[i].second-1);
// cout<<fvc[i].first.first<<" "<<fvc[i].first.second<<" "<<fvc[i].second<<"\n";
updbit(fvc[i].second,len);
}
for(int i=0;i<fvc.size();i++){
undo();
}/*
for(int i=0;i<=200000;i++){
bit[i]=0;
}*/
cout<<ans<<"\n";
nw=qar[qqq].second;
int clr=ar[nw];
while(nw!=0){
int r=dph[nw];
int l=dph[hd[nw]];
st[hd[nw]].insert({{l,r},clr});
nw=fa[hd[nw]];
}
}
}
Compilation message
construction.cpp: In function 'void dfssbsz(long long int, long long int)':
construction.cpp:45:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for(int i=0;i<e[nw].size();i++){
| ~^~~~~~~~~~~~~
construction.cpp:50:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | for(int i=1;i<e[nw].size();i++){
| ~^~~~~~~~~~~~~
construction.cpp: In function 'void dfshd(long long int, long long int)':
construction.cpp:61:14: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for(int i=1;i<e[nw].size();i++){
| ~^~~~~~~~~~~~~
construction.cpp: In function 'int main()':
construction.cpp:126:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<std::pair<long long int, long long int>, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
126 | for(int i=0;i<vc.size();i++){
| ~^~~~~~~~~~
construction.cpp:132:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<std::pair<long long int, long long int>, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
132 | for(int i=0;i<fvc.size();i++){
| ~^~~~~~~~~~~
construction.cpp:139:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<std::pair<long long int, long long int>, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
139 | for(int i=0;i<fvc.size();i++){
| ~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
20828 KB |
Output is correct |
2 |
Correct |
4 ms |
20828 KB |
Output is correct |
3 |
Correct |
4 ms |
20828 KB |
Output is correct |
4 |
Correct |
5 ms |
20928 KB |
Output is correct |
5 |
Correct |
4 ms |
21004 KB |
Output is correct |
6 |
Correct |
5 ms |
20828 KB |
Output is correct |
7 |
Correct |
4 ms |
20828 KB |
Output is correct |
8 |
Correct |
4 ms |
20828 KB |
Output is correct |
9 |
Correct |
5 ms |
20828 KB |
Output is correct |
10 |
Correct |
4 ms |
20828 KB |
Output is correct |
11 |
Correct |
4 ms |
21084 KB |
Output is correct |
12 |
Correct |
4 ms |
21084 KB |
Output is correct |
13 |
Correct |
4 ms |
21080 KB |
Output is correct |
14 |
Correct |
4 ms |
20828 KB |
Output is correct |
15 |
Correct |
4 ms |
20828 KB |
Output is correct |
16 |
Correct |
4 ms |
20828 KB |
Output is correct |
17 |
Correct |
5 ms |
20828 KB |
Output is correct |
18 |
Correct |
4 ms |
20828 KB |
Output is correct |
19 |
Correct |
4 ms |
20828 KB |
Output is correct |
20 |
Correct |
4 ms |
20828 KB |
Output is correct |
21 |
Correct |
4 ms |
20828 KB |
Output is correct |
22 |
Correct |
4 ms |
20828 KB |
Output is correct |
23 |
Correct |
4 ms |
20828 KB |
Output is correct |
24 |
Correct |
4 ms |
20828 KB |
Output is correct |
25 |
Correct |
4 ms |
20828 KB |
Output is correct |
26 |
Correct |
4 ms |
20828 KB |
Output is correct |
27 |
Correct |
4 ms |
21084 KB |
Output is correct |
28 |
Correct |
4 ms |
20968 KB |
Output is correct |
29 |
Correct |
4 ms |
20828 KB |
Output is correct |
30 |
Correct |
5 ms |
20828 KB |
Output is correct |
31 |
Correct |
5 ms |
20828 KB |
Output is correct |
32 |
Correct |
4 ms |
20828 KB |
Output is correct |
33 |
Correct |
5 ms |
20828 KB |
Output is correct |
34 |
Correct |
5 ms |
20828 KB |
Output is correct |
35 |
Correct |
4 ms |
20828 KB |
Output is correct |
36 |
Correct |
4 ms |
20828 KB |
Output is correct |
37 |
Correct |
4 ms |
20828 KB |
Output is correct |
38 |
Correct |
4 ms |
20828 KB |
Output is correct |
39 |
Correct |
4 ms |
21060 KB |
Output is correct |
40 |
Correct |
5 ms |
21084 KB |
Output is correct |
41 |
Correct |
4 ms |
20828 KB |
Output is correct |
42 |
Correct |
5 ms |
20828 KB |
Output is correct |
43 |
Correct |
5 ms |
20828 KB |
Output is correct |
44 |
Correct |
4 ms |
20828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
20828 KB |
Output is correct |
2 |
Correct |
4 ms |
20828 KB |
Output is correct |
3 |
Correct |
4 ms |
20828 KB |
Output is correct |
4 |
Correct |
5 ms |
20928 KB |
Output is correct |
5 |
Correct |
4 ms |
21004 KB |
Output is correct |
6 |
Correct |
5 ms |
20828 KB |
Output is correct |
7 |
Correct |
4 ms |
20828 KB |
Output is correct |
8 |
Correct |
4 ms |
20828 KB |
Output is correct |
9 |
Correct |
5 ms |
20828 KB |
Output is correct |
10 |
Correct |
4 ms |
20828 KB |
Output is correct |
11 |
Correct |
4 ms |
21084 KB |
Output is correct |
12 |
Correct |
4 ms |
21084 KB |
Output is correct |
13 |
Correct |
4 ms |
21080 KB |
Output is correct |
14 |
Correct |
4 ms |
20828 KB |
Output is correct |
15 |
Correct |
4 ms |
20828 KB |
Output is correct |
16 |
Correct |
4 ms |
20828 KB |
Output is correct |
17 |
Correct |
5 ms |
20828 KB |
Output is correct |
18 |
Correct |
4 ms |
20828 KB |
Output is correct |
19 |
Correct |
4 ms |
20828 KB |
Output is correct |
20 |
Correct |
4 ms |
20828 KB |
Output is correct |
21 |
Correct |
4 ms |
20828 KB |
Output is correct |
22 |
Correct |
4 ms |
20828 KB |
Output is correct |
23 |
Correct |
4 ms |
20828 KB |
Output is correct |
24 |
Correct |
4 ms |
20828 KB |
Output is correct |
25 |
Correct |
4 ms |
20828 KB |
Output is correct |
26 |
Correct |
4 ms |
20828 KB |
Output is correct |
27 |
Correct |
4 ms |
21084 KB |
Output is correct |
28 |
Correct |
4 ms |
20968 KB |
Output is correct |
29 |
Correct |
4 ms |
20828 KB |
Output is correct |
30 |
Correct |
5 ms |
20828 KB |
Output is correct |
31 |
Correct |
5 ms |
20828 KB |
Output is correct |
32 |
Correct |
4 ms |
20828 KB |
Output is correct |
33 |
Correct |
5 ms |
20828 KB |
Output is correct |
34 |
Correct |
5 ms |
20828 KB |
Output is correct |
35 |
Correct |
4 ms |
20828 KB |
Output is correct |
36 |
Correct |
4 ms |
20828 KB |
Output is correct |
37 |
Correct |
4 ms |
20828 KB |
Output is correct |
38 |
Correct |
4 ms |
20828 KB |
Output is correct |
39 |
Correct |
4 ms |
21060 KB |
Output is correct |
40 |
Correct |
5 ms |
21084 KB |
Output is correct |
41 |
Correct |
4 ms |
20828 KB |
Output is correct |
42 |
Correct |
5 ms |
20828 KB |
Output is correct |
43 |
Correct |
5 ms |
20828 KB |
Output is correct |
44 |
Correct |
4 ms |
20828 KB |
Output is correct |
45 |
Correct |
5 ms |
21084 KB |
Output is correct |
46 |
Correct |
10 ms |
21340 KB |
Output is correct |
47 |
Correct |
11 ms |
21312 KB |
Output is correct |
48 |
Correct |
10 ms |
21340 KB |
Output is correct |
49 |
Correct |
8 ms |
21340 KB |
Output is correct |
50 |
Correct |
8 ms |
21428 KB |
Output is correct |
51 |
Correct |
8 ms |
21340 KB |
Output is correct |
52 |
Correct |
9 ms |
21596 KB |
Output is correct |
53 |
Correct |
11 ms |
21596 KB |
Output is correct |
54 |
Correct |
9 ms |
21548 KB |
Output is correct |
55 |
Correct |
8 ms |
21596 KB |
Output is correct |
56 |
Correct |
8 ms |
21340 KB |
Output is correct |
57 |
Correct |
13 ms |
21336 KB |
Output is correct |
58 |
Correct |
13 ms |
21296 KB |
Output is correct |
59 |
Correct |
13 ms |
21340 KB |
Output is correct |
60 |
Correct |
13 ms |
21340 KB |
Output is correct |
61 |
Correct |
8 ms |
21336 KB |
Output is correct |
62 |
Correct |
8 ms |
21340 KB |
Output is correct |
63 |
Correct |
9 ms |
21340 KB |
Output is correct |
64 |
Correct |
11 ms |
21352 KB |
Output is correct |
65 |
Correct |
10 ms |
21340 KB |
Output is correct |
66 |
Correct |
10 ms |
21340 KB |
Output is correct |
67 |
Correct |
10 ms |
21352 KB |
Output is correct |
68 |
Correct |
7 ms |
21340 KB |
Output is correct |
69 |
Correct |
8 ms |
21596 KB |
Output is correct |
70 |
Correct |
8 ms |
21592 KB |
Output is correct |
71 |
Correct |
8 ms |
21292 KB |
Output is correct |
72 |
Correct |
13 ms |
21340 KB |
Output is correct |
73 |
Correct |
12 ms |
21340 KB |
Output is correct |
74 |
Correct |
8 ms |
21340 KB |
Output is correct |
75 |
Correct |
8 ms |
21340 KB |
Output is correct |
76 |
Correct |
9 ms |
21232 KB |
Output is correct |
77 |
Correct |
9 ms |
21336 KB |
Output is correct |
78 |
Correct |
7 ms |
21336 KB |
Output is correct |
79 |
Correct |
11 ms |
21340 KB |
Output is correct |
80 |
Correct |
8 ms |
21340 KB |
Output is correct |
81 |
Correct |
9 ms |
21336 KB |
Output is correct |
82 |
Correct |
9 ms |
21336 KB |
Output is correct |
83 |
Correct |
9 ms |
21340 KB |
Output is correct |
84 |
Correct |
8 ms |
21340 KB |
Output is correct |
85 |
Correct |
9 ms |
21176 KB |
Output is correct |
86 |
Correct |
8 ms |
21336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
20828 KB |
Output is correct |
2 |
Correct |
4 ms |
20828 KB |
Output is correct |
3 |
Correct |
4 ms |
20828 KB |
Output is correct |
4 |
Correct |
5 ms |
20928 KB |
Output is correct |
5 |
Correct |
4 ms |
21004 KB |
Output is correct |
6 |
Correct |
5 ms |
20828 KB |
Output is correct |
7 |
Correct |
4 ms |
20828 KB |
Output is correct |
8 |
Correct |
4 ms |
20828 KB |
Output is correct |
9 |
Correct |
5 ms |
20828 KB |
Output is correct |
10 |
Correct |
4 ms |
20828 KB |
Output is correct |
11 |
Correct |
4 ms |
21084 KB |
Output is correct |
12 |
Correct |
4 ms |
21084 KB |
Output is correct |
13 |
Correct |
4 ms |
21080 KB |
Output is correct |
14 |
Correct |
4 ms |
20828 KB |
Output is correct |
15 |
Correct |
4 ms |
20828 KB |
Output is correct |
16 |
Correct |
4 ms |
20828 KB |
Output is correct |
17 |
Correct |
5 ms |
20828 KB |
Output is correct |
18 |
Correct |
4 ms |
20828 KB |
Output is correct |
19 |
Correct |
4 ms |
20828 KB |
Output is correct |
20 |
Correct |
4 ms |
20828 KB |
Output is correct |
21 |
Correct |
4 ms |
20828 KB |
Output is correct |
22 |
Correct |
4 ms |
20828 KB |
Output is correct |
23 |
Correct |
4 ms |
20828 KB |
Output is correct |
24 |
Correct |
4 ms |
20828 KB |
Output is correct |
25 |
Correct |
4 ms |
20828 KB |
Output is correct |
26 |
Correct |
4 ms |
20828 KB |
Output is correct |
27 |
Correct |
4 ms |
21084 KB |
Output is correct |
28 |
Correct |
4 ms |
20968 KB |
Output is correct |
29 |
Correct |
4 ms |
20828 KB |
Output is correct |
30 |
Correct |
5 ms |
20828 KB |
Output is correct |
31 |
Correct |
5 ms |
20828 KB |
Output is correct |
32 |
Correct |
4 ms |
20828 KB |
Output is correct |
33 |
Correct |
5 ms |
20828 KB |
Output is correct |
34 |
Correct |
5 ms |
20828 KB |
Output is correct |
35 |
Correct |
4 ms |
20828 KB |
Output is correct |
36 |
Correct |
4 ms |
20828 KB |
Output is correct |
37 |
Correct |
4 ms |
20828 KB |
Output is correct |
38 |
Correct |
4 ms |
20828 KB |
Output is correct |
39 |
Correct |
4 ms |
21060 KB |
Output is correct |
40 |
Correct |
5 ms |
21084 KB |
Output is correct |
41 |
Correct |
4 ms |
20828 KB |
Output is correct |
42 |
Correct |
5 ms |
20828 KB |
Output is correct |
43 |
Correct |
5 ms |
20828 KB |
Output is correct |
44 |
Correct |
4 ms |
20828 KB |
Output is correct |
45 |
Correct |
5 ms |
21084 KB |
Output is correct |
46 |
Correct |
10 ms |
21340 KB |
Output is correct |
47 |
Correct |
11 ms |
21312 KB |
Output is correct |
48 |
Correct |
10 ms |
21340 KB |
Output is correct |
49 |
Correct |
8 ms |
21340 KB |
Output is correct |
50 |
Correct |
8 ms |
21428 KB |
Output is correct |
51 |
Correct |
8 ms |
21340 KB |
Output is correct |
52 |
Correct |
9 ms |
21596 KB |
Output is correct |
53 |
Correct |
11 ms |
21596 KB |
Output is correct |
54 |
Correct |
9 ms |
21548 KB |
Output is correct |
55 |
Correct |
8 ms |
21596 KB |
Output is correct |
56 |
Correct |
8 ms |
21340 KB |
Output is correct |
57 |
Correct |
13 ms |
21336 KB |
Output is correct |
58 |
Correct |
13 ms |
21296 KB |
Output is correct |
59 |
Correct |
13 ms |
21340 KB |
Output is correct |
60 |
Correct |
13 ms |
21340 KB |
Output is correct |
61 |
Correct |
8 ms |
21336 KB |
Output is correct |
62 |
Correct |
8 ms |
21340 KB |
Output is correct |
63 |
Correct |
9 ms |
21340 KB |
Output is correct |
64 |
Correct |
11 ms |
21352 KB |
Output is correct |
65 |
Correct |
10 ms |
21340 KB |
Output is correct |
66 |
Correct |
10 ms |
21340 KB |
Output is correct |
67 |
Correct |
10 ms |
21352 KB |
Output is correct |
68 |
Correct |
7 ms |
21340 KB |
Output is correct |
69 |
Correct |
8 ms |
21596 KB |
Output is correct |
70 |
Correct |
8 ms |
21592 KB |
Output is correct |
71 |
Correct |
8 ms |
21292 KB |
Output is correct |
72 |
Correct |
13 ms |
21340 KB |
Output is correct |
73 |
Correct |
12 ms |
21340 KB |
Output is correct |
74 |
Correct |
8 ms |
21340 KB |
Output is correct |
75 |
Correct |
8 ms |
21340 KB |
Output is correct |
76 |
Correct |
9 ms |
21232 KB |
Output is correct |
77 |
Correct |
9 ms |
21336 KB |
Output is correct |
78 |
Correct |
7 ms |
21336 KB |
Output is correct |
79 |
Correct |
11 ms |
21340 KB |
Output is correct |
80 |
Correct |
8 ms |
21340 KB |
Output is correct |
81 |
Correct |
9 ms |
21336 KB |
Output is correct |
82 |
Correct |
9 ms |
21336 KB |
Output is correct |
83 |
Correct |
9 ms |
21340 KB |
Output is correct |
84 |
Correct |
8 ms |
21340 KB |
Output is correct |
85 |
Correct |
9 ms |
21176 KB |
Output is correct |
86 |
Correct |
8 ms |
21336 KB |
Output is correct |
87 |
Correct |
23 ms |
22016 KB |
Output is correct |
88 |
Correct |
68 ms |
26072 KB |
Output is correct |
89 |
Correct |
243 ms |
32912 KB |
Output is correct |
90 |
Correct |
254 ms |
32920 KB |
Output is correct |
91 |
Correct |
240 ms |
32792 KB |
Output is correct |
92 |
Correct |
122 ms |
36264 KB |
Output is correct |
93 |
Correct |
122 ms |
36284 KB |
Output is correct |
94 |
Correct |
121 ms |
36260 KB |
Output is correct |
95 |
Correct |
137 ms |
41376 KB |
Output is correct |
96 |
Correct |
150 ms |
41892 KB |
Output is correct |
97 |
Correct |
148 ms |
41732 KB |
Output is correct |
98 |
Correct |
148 ms |
41880 KB |
Output is correct |
99 |
Correct |
135 ms |
34848 KB |
Output is correct |
100 |
Correct |
310 ms |
33116 KB |
Output is correct |
101 |
Correct |
418 ms |
33208 KB |
Output is correct |
102 |
Correct |
379 ms |
33192 KB |
Output is correct |
103 |
Correct |
358 ms |
33180 KB |
Output is correct |
104 |
Correct |
161 ms |
35032 KB |
Output is correct |
105 |
Correct |
137 ms |
34840 KB |
Output is correct |
106 |
Correct |
140 ms |
34984 KB |
Output is correct |
107 |
Correct |
215 ms |
31276 KB |
Output is correct |
108 |
Correct |
223 ms |
31912 KB |
Output is correct |
109 |
Correct |
233 ms |
32428 KB |
Output is correct |
110 |
Correct |
106 ms |
35620 KB |
Output is correct |
111 |
Correct |
140 ms |
41528 KB |
Output is correct |
112 |
Correct |
134 ms |
41244 KB |
Output is correct |
113 |
Correct |
116 ms |
34216 KB |
Output is correct |
114 |
Correct |
314 ms |
33220 KB |
Output is correct |
115 |
Correct |
337 ms |
32668 KB |
Output is correct |
116 |
Correct |
122 ms |
34356 KB |
Output is correct |
117 |
Correct |
148 ms |
32952 KB |
Output is correct |
118 |
Correct |
133 ms |
32556 KB |
Output is correct |
119 |
Correct |
134 ms |
32156 KB |
Output is correct |
120 |
Correct |
132 ms |
32724 KB |
Output is correct |
121 |
Correct |
126 ms |
32196 KB |
Output is correct |
122 |
Correct |
144 ms |
32004 KB |
Output is correct |
123 |
Correct |
146 ms |
33092 KB |
Output is correct |
124 |
Correct |
156 ms |
32552 KB |
Output is correct |
125 |
Correct |
148 ms |
32164 KB |
Output is correct |
126 |
Correct |
134 ms |
32676 KB |
Output is correct |
127 |
Correct |
158 ms |
32256 KB |
Output is correct |
128 |
Correct |
146 ms |
31904 KB |
Output is correct |