//#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[100005];
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<=100000){
bit[pl]+=v;
pl+=lowbit(pl);
}
}
void undo(){
int pl=uvc.back().first;int v=uvc.back().second;
v*=-1;
while(pl<=100000){
bit[pl]+=v;
pl+=lowbit(pl);
}
uvc.pop_back();
}
int n;
int ar[100005];
vector<int>e[100005];
int dph[100005];
int hd[100005];
int sbsz[100005];
int fa[100005];
set<pair<pair<int,int>,int>>st[100005];
pair<int,int>qar[100005];
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(int, int)':
construction.cpp:45:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<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: 'int' and 'std::vector<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(int, int)':
construction.cpp:61:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<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: 'int' and 'std::vector<std::pair<std::pair<int, int>, 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: 'int' and 'std::vector<std::pair<std::pair<int, int>, 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: 'int' and 'std::vector<std::pair<std::pair<int, int>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
141 | for(int i=0;i<fvc.size();i++){
| ~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9564 KB |
Output is correct |
2 |
Correct |
2 ms |
9564 KB |
Output is correct |
3 |
Correct |
2 ms |
9564 KB |
Output is correct |
4 |
Correct |
2 ms |
9648 KB |
Output is correct |
5 |
Correct |
2 ms |
9564 KB |
Output is correct |
6 |
Correct |
3 ms |
9756 KB |
Output is correct |
7 |
Correct |
3 ms |
9564 KB |
Output is correct |
8 |
Correct |
2 ms |
9564 KB |
Output is correct |
9 |
Correct |
2 ms |
9564 KB |
Output is correct |
10 |
Correct |
2 ms |
9564 KB |
Output is correct |
11 |
Correct |
2 ms |
9564 KB |
Output is correct |
12 |
Correct |
3 ms |
9560 KB |
Output is correct |
13 |
Correct |
2 ms |
9564 KB |
Output is correct |
14 |
Correct |
2 ms |
9564 KB |
Output is correct |
15 |
Correct |
3 ms |
9564 KB |
Output is correct |
16 |
Correct |
3 ms |
9564 KB |
Output is correct |
17 |
Correct |
2 ms |
9564 KB |
Output is correct |
18 |
Correct |
3 ms |
9564 KB |
Output is correct |
19 |
Correct |
2 ms |
9564 KB |
Output is correct |
20 |
Correct |
2 ms |
9560 KB |
Output is correct |
21 |
Correct |
2 ms |
9564 KB |
Output is correct |
22 |
Correct |
2 ms |
9636 KB |
Output is correct |
23 |
Correct |
3 ms |
9564 KB |
Output is correct |
24 |
Correct |
3 ms |
9660 KB |
Output is correct |
25 |
Correct |
3 ms |
9564 KB |
Output is correct |
26 |
Correct |
2 ms |
9652 KB |
Output is correct |
27 |
Correct |
3 ms |
9564 KB |
Output is correct |
28 |
Correct |
2 ms |
9564 KB |
Output is correct |
29 |
Correct |
2 ms |
9564 KB |
Output is correct |
30 |
Correct |
2 ms |
9564 KB |
Output is correct |
31 |
Correct |
2 ms |
9564 KB |
Output is correct |
32 |
Correct |
3 ms |
9564 KB |
Output is correct |
33 |
Correct |
2 ms |
9564 KB |
Output is correct |
34 |
Correct |
3 ms |
9564 KB |
Output is correct |
35 |
Correct |
2 ms |
9564 KB |
Output is correct |
36 |
Correct |
3 ms |
9564 KB |
Output is correct |
37 |
Correct |
2 ms |
9564 KB |
Output is correct |
38 |
Correct |
2 ms |
9564 KB |
Output is correct |
39 |
Correct |
2 ms |
9564 KB |
Output is correct |
40 |
Correct |
3 ms |
9564 KB |
Output is correct |
41 |
Correct |
3 ms |
9564 KB |
Output is correct |
42 |
Correct |
3 ms |
9564 KB |
Output is correct |
43 |
Correct |
3 ms |
9564 KB |
Output is correct |
44 |
Correct |
2 ms |
9564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9564 KB |
Output is correct |
2 |
Correct |
2 ms |
9564 KB |
Output is correct |
3 |
Correct |
2 ms |
9564 KB |
Output is correct |
4 |
Correct |
2 ms |
9648 KB |
Output is correct |
5 |
Correct |
2 ms |
9564 KB |
Output is correct |
6 |
Correct |
3 ms |
9756 KB |
Output is correct |
7 |
Correct |
3 ms |
9564 KB |
Output is correct |
8 |
Correct |
2 ms |
9564 KB |
Output is correct |
9 |
Correct |
2 ms |
9564 KB |
Output is correct |
10 |
Correct |
2 ms |
9564 KB |
Output is correct |
11 |
Correct |
2 ms |
9564 KB |
Output is correct |
12 |
Correct |
3 ms |
9560 KB |
Output is correct |
13 |
Correct |
2 ms |
9564 KB |
Output is correct |
14 |
Correct |
2 ms |
9564 KB |
Output is correct |
15 |
Correct |
3 ms |
9564 KB |
Output is correct |
16 |
Correct |
3 ms |
9564 KB |
Output is correct |
17 |
Correct |
2 ms |
9564 KB |
Output is correct |
18 |
Correct |
3 ms |
9564 KB |
Output is correct |
19 |
Correct |
2 ms |
9564 KB |
Output is correct |
20 |
Correct |
2 ms |
9560 KB |
Output is correct |
21 |
Correct |
2 ms |
9564 KB |
Output is correct |
22 |
Correct |
2 ms |
9636 KB |
Output is correct |
23 |
Correct |
3 ms |
9564 KB |
Output is correct |
24 |
Correct |
3 ms |
9660 KB |
Output is correct |
25 |
Correct |
3 ms |
9564 KB |
Output is correct |
26 |
Correct |
2 ms |
9652 KB |
Output is correct |
27 |
Correct |
3 ms |
9564 KB |
Output is correct |
28 |
Correct |
2 ms |
9564 KB |
Output is correct |
29 |
Correct |
2 ms |
9564 KB |
Output is correct |
30 |
Correct |
2 ms |
9564 KB |
Output is correct |
31 |
Correct |
2 ms |
9564 KB |
Output is correct |
32 |
Correct |
3 ms |
9564 KB |
Output is correct |
33 |
Correct |
2 ms |
9564 KB |
Output is correct |
34 |
Correct |
3 ms |
9564 KB |
Output is correct |
35 |
Correct |
2 ms |
9564 KB |
Output is correct |
36 |
Correct |
3 ms |
9564 KB |
Output is correct |
37 |
Correct |
2 ms |
9564 KB |
Output is correct |
38 |
Correct |
2 ms |
9564 KB |
Output is correct |
39 |
Correct |
2 ms |
9564 KB |
Output is correct |
40 |
Correct |
3 ms |
9564 KB |
Output is correct |
41 |
Correct |
3 ms |
9564 KB |
Output is correct |
42 |
Correct |
3 ms |
9564 KB |
Output is correct |
43 |
Correct |
3 ms |
9564 KB |
Output is correct |
44 |
Correct |
2 ms |
9564 KB |
Output is correct |
45 |
Correct |
3 ms |
9564 KB |
Output is correct |
46 |
Correct |
9 ms |
9820 KB |
Output is correct |
47 |
Correct |
7 ms |
9964 KB |
Output is correct |
48 |
Correct |
8 ms |
9820 KB |
Output is correct |
49 |
Correct |
6 ms |
10072 KB |
Output is correct |
50 |
Correct |
5 ms |
10076 KB |
Output is correct |
51 |
Correct |
5 ms |
10076 KB |
Output is correct |
52 |
Correct |
5 ms |
10156 KB |
Output is correct |
53 |
Correct |
5 ms |
10076 KB |
Output is correct |
54 |
Correct |
5 ms |
10072 KB |
Output is correct |
55 |
Correct |
5 ms |
10076 KB |
Output is correct |
56 |
Correct |
4 ms |
10076 KB |
Output is correct |
57 |
Correct |
9 ms |
10076 KB |
Output is correct |
58 |
Correct |
9 ms |
9820 KB |
Output is correct |
59 |
Correct |
10 ms |
9968 KB |
Output is correct |
60 |
Correct |
10 ms |
10076 KB |
Output is correct |
61 |
Correct |
4 ms |
10072 KB |
Output is correct |
62 |
Correct |
5 ms |
10076 KB |
Output is correct |
63 |
Correct |
5 ms |
10076 KB |
Output is correct |
64 |
Correct |
6 ms |
9820 KB |
Output is correct |
65 |
Correct |
7 ms |
9820 KB |
Output is correct |
66 |
Correct |
6 ms |
9820 KB |
Output is correct |
67 |
Correct |
7 ms |
9820 KB |
Output is correct |
68 |
Correct |
4 ms |
10072 KB |
Output is correct |
69 |
Correct |
5 ms |
10076 KB |
Output is correct |
70 |
Correct |
4 ms |
10076 KB |
Output is correct |
71 |
Correct |
5 ms |
10076 KB |
Output is correct |
72 |
Correct |
8 ms |
9820 KB |
Output is correct |
73 |
Correct |
9 ms |
9816 KB |
Output is correct |
74 |
Correct |
5 ms |
10076 KB |
Output is correct |
75 |
Correct |
5 ms |
9964 KB |
Output is correct |
76 |
Correct |
5 ms |
9820 KB |
Output is correct |
77 |
Correct |
5 ms |
9820 KB |
Output is correct |
78 |
Correct |
4 ms |
9816 KB |
Output is correct |
79 |
Correct |
4 ms |
9820 KB |
Output is correct |
80 |
Correct |
4 ms |
9820 KB |
Output is correct |
81 |
Correct |
5 ms |
9820 KB |
Output is correct |
82 |
Correct |
5 ms |
9996 KB |
Output is correct |
83 |
Correct |
5 ms |
9816 KB |
Output is correct |
84 |
Correct |
5 ms |
9820 KB |
Output is correct |
85 |
Correct |
5 ms |
9820 KB |
Output is correct |
86 |
Correct |
5 ms |
9972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9564 KB |
Output is correct |
2 |
Correct |
2 ms |
9564 KB |
Output is correct |
3 |
Correct |
2 ms |
9564 KB |
Output is correct |
4 |
Correct |
2 ms |
9648 KB |
Output is correct |
5 |
Correct |
2 ms |
9564 KB |
Output is correct |
6 |
Correct |
3 ms |
9756 KB |
Output is correct |
7 |
Correct |
3 ms |
9564 KB |
Output is correct |
8 |
Correct |
2 ms |
9564 KB |
Output is correct |
9 |
Correct |
2 ms |
9564 KB |
Output is correct |
10 |
Correct |
2 ms |
9564 KB |
Output is correct |
11 |
Correct |
2 ms |
9564 KB |
Output is correct |
12 |
Correct |
3 ms |
9560 KB |
Output is correct |
13 |
Correct |
2 ms |
9564 KB |
Output is correct |
14 |
Correct |
2 ms |
9564 KB |
Output is correct |
15 |
Correct |
3 ms |
9564 KB |
Output is correct |
16 |
Correct |
3 ms |
9564 KB |
Output is correct |
17 |
Correct |
2 ms |
9564 KB |
Output is correct |
18 |
Correct |
3 ms |
9564 KB |
Output is correct |
19 |
Correct |
2 ms |
9564 KB |
Output is correct |
20 |
Correct |
2 ms |
9560 KB |
Output is correct |
21 |
Correct |
2 ms |
9564 KB |
Output is correct |
22 |
Correct |
2 ms |
9636 KB |
Output is correct |
23 |
Correct |
3 ms |
9564 KB |
Output is correct |
24 |
Correct |
3 ms |
9660 KB |
Output is correct |
25 |
Correct |
3 ms |
9564 KB |
Output is correct |
26 |
Correct |
2 ms |
9652 KB |
Output is correct |
27 |
Correct |
3 ms |
9564 KB |
Output is correct |
28 |
Correct |
2 ms |
9564 KB |
Output is correct |
29 |
Correct |
2 ms |
9564 KB |
Output is correct |
30 |
Correct |
2 ms |
9564 KB |
Output is correct |
31 |
Correct |
2 ms |
9564 KB |
Output is correct |
32 |
Correct |
3 ms |
9564 KB |
Output is correct |
33 |
Correct |
2 ms |
9564 KB |
Output is correct |
34 |
Correct |
3 ms |
9564 KB |
Output is correct |
35 |
Correct |
2 ms |
9564 KB |
Output is correct |
36 |
Correct |
3 ms |
9564 KB |
Output is correct |
37 |
Correct |
2 ms |
9564 KB |
Output is correct |
38 |
Correct |
2 ms |
9564 KB |
Output is correct |
39 |
Correct |
2 ms |
9564 KB |
Output is correct |
40 |
Correct |
3 ms |
9564 KB |
Output is correct |
41 |
Correct |
3 ms |
9564 KB |
Output is correct |
42 |
Correct |
3 ms |
9564 KB |
Output is correct |
43 |
Correct |
3 ms |
9564 KB |
Output is correct |
44 |
Correct |
2 ms |
9564 KB |
Output is correct |
45 |
Correct |
3 ms |
9564 KB |
Output is correct |
46 |
Correct |
9 ms |
9820 KB |
Output is correct |
47 |
Correct |
7 ms |
9964 KB |
Output is correct |
48 |
Correct |
8 ms |
9820 KB |
Output is correct |
49 |
Correct |
6 ms |
10072 KB |
Output is correct |
50 |
Correct |
5 ms |
10076 KB |
Output is correct |
51 |
Correct |
5 ms |
10076 KB |
Output is correct |
52 |
Correct |
5 ms |
10156 KB |
Output is correct |
53 |
Correct |
5 ms |
10076 KB |
Output is correct |
54 |
Correct |
5 ms |
10072 KB |
Output is correct |
55 |
Correct |
5 ms |
10076 KB |
Output is correct |
56 |
Correct |
4 ms |
10076 KB |
Output is correct |
57 |
Correct |
9 ms |
10076 KB |
Output is correct |
58 |
Correct |
9 ms |
9820 KB |
Output is correct |
59 |
Correct |
10 ms |
9968 KB |
Output is correct |
60 |
Correct |
10 ms |
10076 KB |
Output is correct |
61 |
Correct |
4 ms |
10072 KB |
Output is correct |
62 |
Correct |
5 ms |
10076 KB |
Output is correct |
63 |
Correct |
5 ms |
10076 KB |
Output is correct |
64 |
Correct |
6 ms |
9820 KB |
Output is correct |
65 |
Correct |
7 ms |
9820 KB |
Output is correct |
66 |
Correct |
6 ms |
9820 KB |
Output is correct |
67 |
Correct |
7 ms |
9820 KB |
Output is correct |
68 |
Correct |
4 ms |
10072 KB |
Output is correct |
69 |
Correct |
5 ms |
10076 KB |
Output is correct |
70 |
Correct |
4 ms |
10076 KB |
Output is correct |
71 |
Correct |
5 ms |
10076 KB |
Output is correct |
72 |
Correct |
8 ms |
9820 KB |
Output is correct |
73 |
Correct |
9 ms |
9816 KB |
Output is correct |
74 |
Correct |
5 ms |
10076 KB |
Output is correct |
75 |
Correct |
5 ms |
9964 KB |
Output is correct |
76 |
Correct |
5 ms |
9820 KB |
Output is correct |
77 |
Correct |
5 ms |
9820 KB |
Output is correct |
78 |
Correct |
4 ms |
9816 KB |
Output is correct |
79 |
Correct |
4 ms |
9820 KB |
Output is correct |
80 |
Correct |
4 ms |
9820 KB |
Output is correct |
81 |
Correct |
5 ms |
9820 KB |
Output is correct |
82 |
Correct |
5 ms |
9996 KB |
Output is correct |
83 |
Correct |
5 ms |
9816 KB |
Output is correct |
84 |
Correct |
5 ms |
9820 KB |
Output is correct |
85 |
Correct |
5 ms |
9820 KB |
Output is correct |
86 |
Correct |
5 ms |
9972 KB |
Output is correct |
87 |
Correct |
16 ms |
10324 KB |
Output is correct |
88 |
Correct |
48 ms |
11744 KB |
Output is correct |
89 |
Correct |
193 ms |
16428 KB |
Output is correct |
90 |
Correct |
167 ms |
16448 KB |
Output is correct |
91 |
Correct |
217 ms |
16444 KB |
Output is correct |
92 |
Correct |
70 ms |
20032 KB |
Output is correct |
93 |
Correct |
69 ms |
20048 KB |
Output is correct |
94 |
Correct |
68 ms |
20188 KB |
Output is correct |
95 |
Correct |
94 ms |
23664 KB |
Output is correct |
96 |
Correct |
103 ms |
23612 KB |
Output is correct |
97 |
Correct |
99 ms |
23584 KB |
Output is correct |
98 |
Correct |
95 ms |
23580 KB |
Output is correct |
99 |
Correct |
77 ms |
18748 KB |
Output is correct |
100 |
Correct |
225 ms |
17112 KB |
Output is correct |
101 |
Correct |
275 ms |
17068 KB |
Output is correct |
102 |
Correct |
275 ms |
16960 KB |
Output is correct |
103 |
Correct |
254 ms |
16900 KB |
Output is correct |
104 |
Correct |
96 ms |
18624 KB |
Output is correct |
105 |
Correct |
101 ms |
18572 KB |
Output is correct |
106 |
Correct |
99 ms |
18644 KB |
Output is correct |
107 |
Correct |
148 ms |
16008 KB |
Output is correct |
108 |
Correct |
181 ms |
16264 KB |
Output is correct |
109 |
Correct |
169 ms |
16444 KB |
Output is correct |
110 |
Correct |
68 ms |
20044 KB |
Output is correct |
111 |
Correct |
90 ms |
23540 KB |
Output is correct |
112 |
Correct |
93 ms |
23512 KB |
Output is correct |
113 |
Correct |
79 ms |
18752 KB |
Output is correct |
114 |
Correct |
258 ms |
17112 KB |
Output is correct |
115 |
Correct |
292 ms |
16960 KB |
Output is correct |
116 |
Correct |
77 ms |
18640 KB |
Output is correct |
117 |
Correct |
81 ms |
16956 KB |
Output is correct |
118 |
Correct |
93 ms |
16600 KB |
Output is correct |
119 |
Correct |
87 ms |
16236 KB |
Output is correct |
120 |
Correct |
78 ms |
16920 KB |
Output is correct |
121 |
Correct |
79 ms |
16436 KB |
Output is correct |
122 |
Correct |
83 ms |
16188 KB |
Output is correct |
123 |
Correct |
100 ms |
17216 KB |
Output is correct |
124 |
Correct |
96 ms |
16668 KB |
Output is correct |
125 |
Correct |
93 ms |
16340 KB |
Output is correct |
126 |
Correct |
88 ms |
17144 KB |
Output is correct |
127 |
Correct |
88 ms |
16700 KB |
Output is correct |
128 |
Correct |
90 ms |
16364 KB |
Output is correct |