//#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.tie(0);
ios_base::sync_with_stdio(0);
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:128: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]
128 | for(int i=0;i<vc.size();i++){
| ~^~~~~~~~~~
construction.cpp:134: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]
134 | for(int i=0;i<fvc.size();i++){
| ~^~~~~~~~~~~
construction.cpp:141: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]
141 | for(int i=0;i<fvc.size();i++){
| ~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
20824 KB |
Output is correct |
2 |
Correct |
4 ms |
20828 KB |
Output is correct |
3 |
Correct |
4 ms |
20828 KB |
Output is correct |
4 |
Correct |
4 ms |
20828 KB |
Output is correct |
5 |
Correct |
4 ms |
20828 KB |
Output is correct |
6 |
Correct |
4 ms |
20824 KB |
Output is correct |
7 |
Correct |
4 ms |
20944 KB |
Output is correct |
8 |
Correct |
4 ms |
21084 KB |
Output is correct |
9 |
Correct |
4 ms |
21080 KB |
Output is correct |
10 |
Correct |
4 ms |
21084 KB |
Output is correct |
11 |
Correct |
4 ms |
20944 KB |
Output is correct |
12 |
Correct |
4 ms |
21084 KB |
Output is correct |
13 |
Correct |
4 ms |
21084 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 |
20928 KB |
Output is correct |
17 |
Correct |
4 ms |
21084 KB |
Output is correct |
18 |
Correct |
4 ms |
20828 KB |
Output is correct |
19 |
Correct |
4 ms |
21084 KB |
Output is correct |
20 |
Correct |
4 ms |
20828 KB |
Output is correct |
21 |
Correct |
4 ms |
21084 KB |
Output is correct |
22 |
Correct |
4 ms |
21084 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 |
21084 KB |
Output is correct |
27 |
Correct |
4 ms |
20928 KB |
Output is correct |
28 |
Correct |
4 ms |
21084 KB |
Output is correct |
29 |
Correct |
4 ms |
21084 KB |
Output is correct |
30 |
Correct |
4 ms |
20828 KB |
Output is correct |
31 |
Correct |
4 ms |
20828 KB |
Output is correct |
32 |
Correct |
4 ms |
21084 KB |
Output is correct |
33 |
Correct |
4 ms |
20828 KB |
Output is correct |
34 |
Correct |
4 ms |
20828 KB |
Output is correct |
35 |
Correct |
4 ms |
20828 KB |
Output is correct |
36 |
Correct |
4 ms |
20824 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 |
20828 KB |
Output is correct |
40 |
Correct |
4 ms |
20828 KB |
Output is correct |
41 |
Correct |
4 ms |
20824 KB |
Output is correct |
42 |
Correct |
4 ms |
20828 KB |
Output is correct |
43 |
Correct |
4 ms |
20828 KB |
Output is correct |
44 |
Correct |
4 ms |
20940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
20824 KB |
Output is correct |
2 |
Correct |
4 ms |
20828 KB |
Output is correct |
3 |
Correct |
4 ms |
20828 KB |
Output is correct |
4 |
Correct |
4 ms |
20828 KB |
Output is correct |
5 |
Correct |
4 ms |
20828 KB |
Output is correct |
6 |
Correct |
4 ms |
20824 KB |
Output is correct |
7 |
Correct |
4 ms |
20944 KB |
Output is correct |
8 |
Correct |
4 ms |
21084 KB |
Output is correct |
9 |
Correct |
4 ms |
21080 KB |
Output is correct |
10 |
Correct |
4 ms |
21084 KB |
Output is correct |
11 |
Correct |
4 ms |
20944 KB |
Output is correct |
12 |
Correct |
4 ms |
21084 KB |
Output is correct |
13 |
Correct |
4 ms |
21084 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 |
20928 KB |
Output is correct |
17 |
Correct |
4 ms |
21084 KB |
Output is correct |
18 |
Correct |
4 ms |
20828 KB |
Output is correct |
19 |
Correct |
4 ms |
21084 KB |
Output is correct |
20 |
Correct |
4 ms |
20828 KB |
Output is correct |
21 |
Correct |
4 ms |
21084 KB |
Output is correct |
22 |
Correct |
4 ms |
21084 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 |
21084 KB |
Output is correct |
27 |
Correct |
4 ms |
20928 KB |
Output is correct |
28 |
Correct |
4 ms |
21084 KB |
Output is correct |
29 |
Correct |
4 ms |
21084 KB |
Output is correct |
30 |
Correct |
4 ms |
20828 KB |
Output is correct |
31 |
Correct |
4 ms |
20828 KB |
Output is correct |
32 |
Correct |
4 ms |
21084 KB |
Output is correct |
33 |
Correct |
4 ms |
20828 KB |
Output is correct |
34 |
Correct |
4 ms |
20828 KB |
Output is correct |
35 |
Correct |
4 ms |
20828 KB |
Output is correct |
36 |
Correct |
4 ms |
20824 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 |
20828 KB |
Output is correct |
40 |
Correct |
4 ms |
20828 KB |
Output is correct |
41 |
Correct |
4 ms |
20824 KB |
Output is correct |
42 |
Correct |
4 ms |
20828 KB |
Output is correct |
43 |
Correct |
4 ms |
20828 KB |
Output is correct |
44 |
Correct |
4 ms |
20940 KB |
Output is correct |
45 |
Correct |
5 ms |
21084 KB |
Output is correct |
46 |
Correct |
11 ms |
21392 KB |
Output is correct |
47 |
Correct |
9 ms |
21340 KB |
Output is correct |
48 |
Correct |
9 ms |
21336 KB |
Output is correct |
49 |
Correct |
6 ms |
21340 KB |
Output is correct |
50 |
Correct |
6 ms |
21340 KB |
Output is correct |
51 |
Correct |
6 ms |
21540 KB |
Output is correct |
52 |
Correct |
6 ms |
21596 KB |
Output is correct |
53 |
Correct |
7 ms |
21596 KB |
Output is correct |
54 |
Correct |
7 ms |
21592 KB |
Output is correct |
55 |
Correct |
7 ms |
21596 KB |
Output is correct |
56 |
Correct |
7 ms |
21460 KB |
Output is correct |
57 |
Correct |
11 ms |
21340 KB |
Output is correct |
58 |
Correct |
12 ms |
21272 KB |
Output is correct |
59 |
Correct |
11 ms |
21340 KB |
Output is correct |
60 |
Correct |
13 ms |
21340 KB |
Output is correct |
61 |
Correct |
7 ms |
21340 KB |
Output is correct |
62 |
Correct |
7 ms |
21332 KB |
Output is correct |
63 |
Correct |
7 ms |
21592 KB |
Output is correct |
64 |
Correct |
9 ms |
21268 KB |
Output is correct |
65 |
Correct |
8 ms |
21336 KB |
Output is correct |
66 |
Correct |
8 ms |
21340 KB |
Output is correct |
67 |
Correct |
8 ms |
21236 KB |
Output is correct |
68 |
Correct |
6 ms |
21440 KB |
Output is correct |
69 |
Correct |
6 ms |
21596 KB |
Output is correct |
70 |
Correct |
6 ms |
21596 KB |
Output is correct |
71 |
Correct |
6 ms |
21336 KB |
Output is correct |
72 |
Correct |
11 ms |
21268 KB |
Output is correct |
73 |
Correct |
11 ms |
21340 KB |
Output is correct |
74 |
Correct |
6 ms |
21340 KB |
Output is correct |
75 |
Correct |
7 ms |
21272 KB |
Output is correct |
76 |
Correct |
7 ms |
21340 KB |
Output is correct |
77 |
Correct |
8 ms |
21180 KB |
Output is correct |
78 |
Correct |
6 ms |
21340 KB |
Output is correct |
79 |
Correct |
9 ms |
21336 KB |
Output is correct |
80 |
Correct |
6 ms |
21340 KB |
Output is correct |
81 |
Correct |
9 ms |
21380 KB |
Output is correct |
82 |
Correct |
7 ms |
21336 KB |
Output is correct |
83 |
Correct |
7 ms |
21336 KB |
Output is correct |
84 |
Correct |
6 ms |
21336 KB |
Output is correct |
85 |
Correct |
7 ms |
21340 KB |
Output is correct |
86 |
Correct |
7 ms |
21340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
20824 KB |
Output is correct |
2 |
Correct |
4 ms |
20828 KB |
Output is correct |
3 |
Correct |
4 ms |
20828 KB |
Output is correct |
4 |
Correct |
4 ms |
20828 KB |
Output is correct |
5 |
Correct |
4 ms |
20828 KB |
Output is correct |
6 |
Correct |
4 ms |
20824 KB |
Output is correct |
7 |
Correct |
4 ms |
20944 KB |
Output is correct |
8 |
Correct |
4 ms |
21084 KB |
Output is correct |
9 |
Correct |
4 ms |
21080 KB |
Output is correct |
10 |
Correct |
4 ms |
21084 KB |
Output is correct |
11 |
Correct |
4 ms |
20944 KB |
Output is correct |
12 |
Correct |
4 ms |
21084 KB |
Output is correct |
13 |
Correct |
4 ms |
21084 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 |
20928 KB |
Output is correct |
17 |
Correct |
4 ms |
21084 KB |
Output is correct |
18 |
Correct |
4 ms |
20828 KB |
Output is correct |
19 |
Correct |
4 ms |
21084 KB |
Output is correct |
20 |
Correct |
4 ms |
20828 KB |
Output is correct |
21 |
Correct |
4 ms |
21084 KB |
Output is correct |
22 |
Correct |
4 ms |
21084 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 |
21084 KB |
Output is correct |
27 |
Correct |
4 ms |
20928 KB |
Output is correct |
28 |
Correct |
4 ms |
21084 KB |
Output is correct |
29 |
Correct |
4 ms |
21084 KB |
Output is correct |
30 |
Correct |
4 ms |
20828 KB |
Output is correct |
31 |
Correct |
4 ms |
20828 KB |
Output is correct |
32 |
Correct |
4 ms |
21084 KB |
Output is correct |
33 |
Correct |
4 ms |
20828 KB |
Output is correct |
34 |
Correct |
4 ms |
20828 KB |
Output is correct |
35 |
Correct |
4 ms |
20828 KB |
Output is correct |
36 |
Correct |
4 ms |
20824 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 |
20828 KB |
Output is correct |
40 |
Correct |
4 ms |
20828 KB |
Output is correct |
41 |
Correct |
4 ms |
20824 KB |
Output is correct |
42 |
Correct |
4 ms |
20828 KB |
Output is correct |
43 |
Correct |
4 ms |
20828 KB |
Output is correct |
44 |
Correct |
4 ms |
20940 KB |
Output is correct |
45 |
Correct |
5 ms |
21084 KB |
Output is correct |
46 |
Correct |
11 ms |
21392 KB |
Output is correct |
47 |
Correct |
9 ms |
21340 KB |
Output is correct |
48 |
Correct |
9 ms |
21336 KB |
Output is correct |
49 |
Correct |
6 ms |
21340 KB |
Output is correct |
50 |
Correct |
6 ms |
21340 KB |
Output is correct |
51 |
Correct |
6 ms |
21540 KB |
Output is correct |
52 |
Correct |
6 ms |
21596 KB |
Output is correct |
53 |
Correct |
7 ms |
21596 KB |
Output is correct |
54 |
Correct |
7 ms |
21592 KB |
Output is correct |
55 |
Correct |
7 ms |
21596 KB |
Output is correct |
56 |
Correct |
7 ms |
21460 KB |
Output is correct |
57 |
Correct |
11 ms |
21340 KB |
Output is correct |
58 |
Correct |
12 ms |
21272 KB |
Output is correct |
59 |
Correct |
11 ms |
21340 KB |
Output is correct |
60 |
Correct |
13 ms |
21340 KB |
Output is correct |
61 |
Correct |
7 ms |
21340 KB |
Output is correct |
62 |
Correct |
7 ms |
21332 KB |
Output is correct |
63 |
Correct |
7 ms |
21592 KB |
Output is correct |
64 |
Correct |
9 ms |
21268 KB |
Output is correct |
65 |
Correct |
8 ms |
21336 KB |
Output is correct |
66 |
Correct |
8 ms |
21340 KB |
Output is correct |
67 |
Correct |
8 ms |
21236 KB |
Output is correct |
68 |
Correct |
6 ms |
21440 KB |
Output is correct |
69 |
Correct |
6 ms |
21596 KB |
Output is correct |
70 |
Correct |
6 ms |
21596 KB |
Output is correct |
71 |
Correct |
6 ms |
21336 KB |
Output is correct |
72 |
Correct |
11 ms |
21268 KB |
Output is correct |
73 |
Correct |
11 ms |
21340 KB |
Output is correct |
74 |
Correct |
6 ms |
21340 KB |
Output is correct |
75 |
Correct |
7 ms |
21272 KB |
Output is correct |
76 |
Correct |
7 ms |
21340 KB |
Output is correct |
77 |
Correct |
8 ms |
21180 KB |
Output is correct |
78 |
Correct |
6 ms |
21340 KB |
Output is correct |
79 |
Correct |
9 ms |
21336 KB |
Output is correct |
80 |
Correct |
6 ms |
21340 KB |
Output is correct |
81 |
Correct |
9 ms |
21380 KB |
Output is correct |
82 |
Correct |
7 ms |
21336 KB |
Output is correct |
83 |
Correct |
7 ms |
21336 KB |
Output is correct |
84 |
Correct |
6 ms |
21336 KB |
Output is correct |
85 |
Correct |
7 ms |
21340 KB |
Output is correct |
86 |
Correct |
7 ms |
21340 KB |
Output is correct |
87 |
Correct |
18 ms |
21772 KB |
Output is correct |
88 |
Correct |
51 ms |
25328 KB |
Output is correct |
89 |
Correct |
191 ms |
30636 KB |
Output is correct |
90 |
Correct |
197 ms |
30644 KB |
Output is correct |
91 |
Correct |
188 ms |
30792 KB |
Output is correct |
92 |
Correct |
74 ms |
34236 KB |
Output is correct |
93 |
Correct |
75 ms |
34240 KB |
Output is correct |
94 |
Correct |
85 ms |
34180 KB |
Output is correct |
95 |
Correct |
96 ms |
39796 KB |
Output is correct |
96 |
Correct |
114 ms |
39772 KB |
Output is correct |
97 |
Correct |
103 ms |
39624 KB |
Output is correct |
98 |
Correct |
105 ms |
39752 KB |
Output is correct |
99 |
Correct |
85 ms |
32944 KB |
Output is correct |
100 |
Correct |
270 ms |
31088 KB |
Output is correct |
101 |
Correct |
312 ms |
31148 KB |
Output is correct |
102 |
Correct |
309 ms |
30996 KB |
Output is correct |
103 |
Correct |
310 ms |
31092 KB |
Output is correct |
104 |
Correct |
90 ms |
32940 KB |
Output is correct |
105 |
Correct |
92 ms |
32684 KB |
Output is correct |
106 |
Correct |
91 ms |
32884 KB |
Output is correct |
107 |
Correct |
171 ms |
29844 KB |
Output is correct |
108 |
Correct |
192 ms |
30840 KB |
Output is correct |
109 |
Correct |
190 ms |
30632 KB |
Output is correct |
110 |
Correct |
70 ms |
34248 KB |
Output is correct |
111 |
Correct |
98 ms |
39604 KB |
Output is correct |
112 |
Correct |
96 ms |
39720 KB |
Output is correct |
113 |
Correct |
80 ms |
32760 KB |
Output is correct |
114 |
Correct |
257 ms |
31148 KB |
Output is correct |
115 |
Correct |
317 ms |
31268 KB |
Output is correct |
116 |
Correct |
88 ms |
32888 KB |
Output is correct |
117 |
Correct |
89 ms |
31152 KB |
Output is correct |
118 |
Correct |
90 ms |
30700 KB |
Output is correct |
119 |
Correct |
100 ms |
30720 KB |
Output is correct |
120 |
Correct |
85 ms |
31176 KB |
Output is correct |
121 |
Correct |
90 ms |
30744 KB |
Output is correct |
122 |
Correct |
89 ms |
30468 KB |
Output is correct |
123 |
Correct |
98 ms |
31152 KB |
Output is correct |
124 |
Correct |
99 ms |
30636 KB |
Output is correct |
125 |
Correct |
101 ms |
30372 KB |
Output is correct |
126 |
Correct |
93 ms |
31156 KB |
Output is correct |
127 |
Correct |
98 ms |
30644 KB |
Output is correct |
128 |
Correct |
106 ms |
30504 KB |
Output is correct |