#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define rc(x) return cout<<x<<endl,0
#define pb push_back
#define mkp make_pair
#define in insert
#define er erase
#define fd find
#define fr first
#define sc second
#define all(x) x.begin(),x.end()
typedef long long ll;
typedef long double ld;
const ll INF=0x3f3f3f3f3f3f3f3f;
const ll llinf=(1LL<<62);
const int inf=(1<<30);
const int nmax=1e5+50;
const ll mod=1e9+7;
using namespace std;
int x,y,i,n,up[nmax][20],tin[nmax],tout[nmax],tt,ex[nmax],ey[nmax],c[nmax],d,z,u,j,lvl[nmax],sz,nr,lc,fw[nmax];
ll rs;
vector<int>a[nmax];
pair<int,int>st[4*nmax],pr;
vector<int>vc;
vector<pair<int,int> >vec;
void ufw(int i,int v)
{
for(;i>=1;i-=i&(-i))fw[i]+=v;
}
int qfw(int i)
{
int ans=0;
for(;i<=sz;i+=i&(-i))ans+=fw[i];
return ans;
}
void dfs(int x,int p)
{
lvl[x]=lvl[p]+1;
tin[x]=++tt;
up[x][0]=p;
for(int i=1;i<17;i++)up[x][i]=up[up[x][i-1]][i-1];
for(int i=0;i<(int)a[x].size();i++)dfs(a[x][i],x);
tout[x]=tt;
}
int anc(int x,int y)
{
return tin[x]<=tin[y] && tout[x]>=tout[y];
}
int lca(int x,int y)
{
if(anc(x,y))return x;
if(anc(y,x))return y;
for(int i=16;i>=0;i--)if(!anc(up[x][i],y))x=up[x][i];
return up[x][0];
}
void upd(int nod,int l,int r,int p,pair<int,int>v)
{
if(l==r)
{
st[nod]=v;
return;
}
int mid=(l+r)/2;
if(p<=mid)upd(2*nod,l,mid,p,v);
else upd(2*nod+1,mid+1,r,p,v);
st[nod]=max(st[2*nod],st[2*nod+1]);
}
pair<int,int> qry(int nod,int l,int r,int tl,int tr)
{
if(tr<l || tl>r)return mkp(0,0);
if(tl<=l && r<=tr)return st[nod];
int mid=(l+r)/2;
return max(qry(2*nod,l,mid,tl,tr),qry(2*nod+1,mid+1,r,tl,tr));
}
int main()
{
//freopen("sol.in","r",stdin);
//freopen("sol.out","w",stdout);
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
cin>>n;
for(i=1;i<=n;i++)cin>>c[i],vc.pb(c[i]);
sort(all(vc));
vc.resize(unique(all(vc))-vc.begin());
sz=(int)vc.size();
for(i=1;i<=n;i++)c[i]=lower_bound(all(vc),c[i])-vc.begin()+1;
for(i=1;i<n;i++)
{
cin>>ex[i]>>ey[i];
a[ex[i]].pb(ey[i]);
}
dfs(1,1);
for(i=1;i<n;i++)
{
x=ex[i],y=ey[i];
vec.clear();
z=1,rs=0;
while(1)
{
pr=qry(1,1,n,tin[z],tout[z]);
if(!pr.fr)break;
u=pr.sc;
lc=lca(y,u);
nr=qfw(c[u]+1);
rs+=1LL*nr*1LL*(lvl[lc]-lvl[z]+1);
ufw(c[u],lvl[lc]-lvl[z]+1);
vec.pb(mkp(c[u],lvl[lc]-lvl[z]+1));
if(lc==x)break;
d=lvl[y]-lvl[lc]-1;
z=y;
for(j=16;j>=0;j--)if(d&(1<<j))z=up[z][j];
}
upd(1,1,n,tin[y],mkp(i,y));
for(j=0;j<(int)vec.size();j++)ufw(vec[j].fr,-vec[j].sc);
cout<<rs<<'\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Correct |
6 ms |
2688 KB |
Output is correct |
3 |
Correct |
6 ms |
2688 KB |
Output is correct |
4 |
Correct |
7 ms |
2944 KB |
Output is correct |
5 |
Correct |
7 ms |
2816 KB |
Output is correct |
6 |
Correct |
7 ms |
2816 KB |
Output is correct |
7 |
Correct |
8 ms |
2816 KB |
Output is correct |
8 |
Correct |
6 ms |
2816 KB |
Output is correct |
9 |
Correct |
7 ms |
2816 KB |
Output is correct |
10 |
Correct |
7 ms |
2816 KB |
Output is correct |
11 |
Correct |
8 ms |
2816 KB |
Output is correct |
12 |
Correct |
7 ms |
2816 KB |
Output is correct |
13 |
Correct |
7 ms |
2816 KB |
Output is correct |
14 |
Correct |
7 ms |
2816 KB |
Output is correct |
15 |
Correct |
7 ms |
2816 KB |
Output is correct |
16 |
Correct |
7 ms |
2816 KB |
Output is correct |
17 |
Correct |
7 ms |
2816 KB |
Output is correct |
18 |
Correct |
7 ms |
2816 KB |
Output is correct |
19 |
Correct |
6 ms |
2816 KB |
Output is correct |
20 |
Correct |
7 ms |
2816 KB |
Output is correct |
21 |
Correct |
6 ms |
2816 KB |
Output is correct |
22 |
Correct |
7 ms |
2816 KB |
Output is correct |
23 |
Correct |
7 ms |
2816 KB |
Output is correct |
24 |
Correct |
7 ms |
2816 KB |
Output is correct |
25 |
Correct |
7 ms |
2816 KB |
Output is correct |
26 |
Correct |
8 ms |
2944 KB |
Output is correct |
27 |
Correct |
7 ms |
2816 KB |
Output is correct |
28 |
Correct |
7 ms |
2816 KB |
Output is correct |
29 |
Correct |
7 ms |
2816 KB |
Output is correct |
30 |
Correct |
8 ms |
2816 KB |
Output is correct |
31 |
Correct |
8 ms |
2816 KB |
Output is correct |
32 |
Correct |
7 ms |
2816 KB |
Output is correct |
33 |
Correct |
7 ms |
2816 KB |
Output is correct |
34 |
Correct |
7 ms |
2816 KB |
Output is correct |
35 |
Correct |
7 ms |
2816 KB |
Output is correct |
36 |
Correct |
7 ms |
2816 KB |
Output is correct |
37 |
Correct |
7 ms |
2816 KB |
Output is correct |
38 |
Correct |
7 ms |
2944 KB |
Output is correct |
39 |
Correct |
7 ms |
2816 KB |
Output is correct |
40 |
Correct |
21 ms |
2816 KB |
Output is correct |
41 |
Correct |
7 ms |
2816 KB |
Output is correct |
42 |
Correct |
7 ms |
2816 KB |
Output is correct |
43 |
Correct |
7 ms |
2816 KB |
Output is correct |
44 |
Correct |
7 ms |
2816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Correct |
6 ms |
2688 KB |
Output is correct |
3 |
Correct |
6 ms |
2688 KB |
Output is correct |
4 |
Correct |
7 ms |
2944 KB |
Output is correct |
5 |
Correct |
7 ms |
2816 KB |
Output is correct |
6 |
Correct |
7 ms |
2816 KB |
Output is correct |
7 |
Correct |
8 ms |
2816 KB |
Output is correct |
8 |
Correct |
6 ms |
2816 KB |
Output is correct |
9 |
Correct |
7 ms |
2816 KB |
Output is correct |
10 |
Correct |
7 ms |
2816 KB |
Output is correct |
11 |
Correct |
8 ms |
2816 KB |
Output is correct |
12 |
Correct |
7 ms |
2816 KB |
Output is correct |
13 |
Correct |
7 ms |
2816 KB |
Output is correct |
14 |
Correct |
7 ms |
2816 KB |
Output is correct |
15 |
Correct |
7 ms |
2816 KB |
Output is correct |
16 |
Correct |
7 ms |
2816 KB |
Output is correct |
17 |
Correct |
7 ms |
2816 KB |
Output is correct |
18 |
Correct |
7 ms |
2816 KB |
Output is correct |
19 |
Correct |
6 ms |
2816 KB |
Output is correct |
20 |
Correct |
7 ms |
2816 KB |
Output is correct |
21 |
Correct |
6 ms |
2816 KB |
Output is correct |
22 |
Correct |
7 ms |
2816 KB |
Output is correct |
23 |
Correct |
7 ms |
2816 KB |
Output is correct |
24 |
Correct |
7 ms |
2816 KB |
Output is correct |
25 |
Correct |
7 ms |
2816 KB |
Output is correct |
26 |
Correct |
8 ms |
2944 KB |
Output is correct |
27 |
Correct |
7 ms |
2816 KB |
Output is correct |
28 |
Correct |
7 ms |
2816 KB |
Output is correct |
29 |
Correct |
7 ms |
2816 KB |
Output is correct |
30 |
Correct |
8 ms |
2816 KB |
Output is correct |
31 |
Correct |
8 ms |
2816 KB |
Output is correct |
32 |
Correct |
7 ms |
2816 KB |
Output is correct |
33 |
Correct |
7 ms |
2816 KB |
Output is correct |
34 |
Correct |
7 ms |
2816 KB |
Output is correct |
35 |
Correct |
7 ms |
2816 KB |
Output is correct |
36 |
Correct |
7 ms |
2816 KB |
Output is correct |
37 |
Correct |
7 ms |
2816 KB |
Output is correct |
38 |
Correct |
7 ms |
2944 KB |
Output is correct |
39 |
Correct |
7 ms |
2816 KB |
Output is correct |
40 |
Correct |
21 ms |
2816 KB |
Output is correct |
41 |
Correct |
7 ms |
2816 KB |
Output is correct |
42 |
Correct |
7 ms |
2816 KB |
Output is correct |
43 |
Correct |
7 ms |
2816 KB |
Output is correct |
44 |
Correct |
7 ms |
2816 KB |
Output is correct |
45 |
Correct |
8 ms |
2944 KB |
Output is correct |
46 |
Correct |
17 ms |
3456 KB |
Output is correct |
47 |
Correct |
15 ms |
3456 KB |
Output is correct |
48 |
Correct |
15 ms |
3456 KB |
Output is correct |
49 |
Correct |
10 ms |
3584 KB |
Output is correct |
50 |
Correct |
10 ms |
3584 KB |
Output is correct |
51 |
Correct |
10 ms |
3584 KB |
Output is correct |
52 |
Correct |
13 ms |
3584 KB |
Output is correct |
53 |
Correct |
10 ms |
3456 KB |
Output is correct |
54 |
Correct |
11 ms |
3456 KB |
Output is correct |
55 |
Correct |
10 ms |
3456 KB |
Output is correct |
56 |
Correct |
11 ms |
3488 KB |
Output is correct |
57 |
Correct |
19 ms |
3328 KB |
Output is correct |
58 |
Correct |
20 ms |
3456 KB |
Output is correct |
59 |
Correct |
19 ms |
3456 KB |
Output is correct |
60 |
Correct |
20 ms |
3456 KB |
Output is correct |
61 |
Correct |
12 ms |
3456 KB |
Output is correct |
62 |
Correct |
11 ms |
3456 KB |
Output is correct |
63 |
Correct |
11 ms |
3456 KB |
Output is correct |
64 |
Correct |
14 ms |
3456 KB |
Output is correct |
65 |
Correct |
15 ms |
3440 KB |
Output is correct |
66 |
Correct |
16 ms |
3456 KB |
Output is correct |
67 |
Correct |
14 ms |
3456 KB |
Output is correct |
68 |
Correct |
9 ms |
3584 KB |
Output is correct |
69 |
Correct |
10 ms |
3456 KB |
Output is correct |
70 |
Correct |
10 ms |
3456 KB |
Output is correct |
71 |
Correct |
10 ms |
3456 KB |
Output is correct |
72 |
Correct |
19 ms |
3456 KB |
Output is correct |
73 |
Correct |
19 ms |
3456 KB |
Output is correct |
74 |
Correct |
11 ms |
3456 KB |
Output is correct |
75 |
Correct |
10 ms |
3456 KB |
Output is correct |
76 |
Correct |
10 ms |
3456 KB |
Output is correct |
77 |
Correct |
10 ms |
3456 KB |
Output is correct |
78 |
Correct |
9 ms |
3328 KB |
Output is correct |
79 |
Correct |
10 ms |
3328 KB |
Output is correct |
80 |
Correct |
9 ms |
3456 KB |
Output is correct |
81 |
Correct |
12 ms |
3456 KB |
Output is correct |
82 |
Correct |
11 ms |
3456 KB |
Output is correct |
83 |
Correct |
11 ms |
3456 KB |
Output is correct |
84 |
Correct |
11 ms |
3456 KB |
Output is correct |
85 |
Correct |
10 ms |
3408 KB |
Output is correct |
86 |
Correct |
11 ms |
3388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Correct |
6 ms |
2688 KB |
Output is correct |
3 |
Correct |
6 ms |
2688 KB |
Output is correct |
4 |
Correct |
7 ms |
2944 KB |
Output is correct |
5 |
Correct |
7 ms |
2816 KB |
Output is correct |
6 |
Correct |
7 ms |
2816 KB |
Output is correct |
7 |
Correct |
8 ms |
2816 KB |
Output is correct |
8 |
Correct |
6 ms |
2816 KB |
Output is correct |
9 |
Correct |
7 ms |
2816 KB |
Output is correct |
10 |
Correct |
7 ms |
2816 KB |
Output is correct |
11 |
Correct |
8 ms |
2816 KB |
Output is correct |
12 |
Correct |
7 ms |
2816 KB |
Output is correct |
13 |
Correct |
7 ms |
2816 KB |
Output is correct |
14 |
Correct |
7 ms |
2816 KB |
Output is correct |
15 |
Correct |
7 ms |
2816 KB |
Output is correct |
16 |
Correct |
7 ms |
2816 KB |
Output is correct |
17 |
Correct |
7 ms |
2816 KB |
Output is correct |
18 |
Correct |
7 ms |
2816 KB |
Output is correct |
19 |
Correct |
6 ms |
2816 KB |
Output is correct |
20 |
Correct |
7 ms |
2816 KB |
Output is correct |
21 |
Correct |
6 ms |
2816 KB |
Output is correct |
22 |
Correct |
7 ms |
2816 KB |
Output is correct |
23 |
Correct |
7 ms |
2816 KB |
Output is correct |
24 |
Correct |
7 ms |
2816 KB |
Output is correct |
25 |
Correct |
7 ms |
2816 KB |
Output is correct |
26 |
Correct |
8 ms |
2944 KB |
Output is correct |
27 |
Correct |
7 ms |
2816 KB |
Output is correct |
28 |
Correct |
7 ms |
2816 KB |
Output is correct |
29 |
Correct |
7 ms |
2816 KB |
Output is correct |
30 |
Correct |
8 ms |
2816 KB |
Output is correct |
31 |
Correct |
8 ms |
2816 KB |
Output is correct |
32 |
Correct |
7 ms |
2816 KB |
Output is correct |
33 |
Correct |
7 ms |
2816 KB |
Output is correct |
34 |
Correct |
7 ms |
2816 KB |
Output is correct |
35 |
Correct |
7 ms |
2816 KB |
Output is correct |
36 |
Correct |
7 ms |
2816 KB |
Output is correct |
37 |
Correct |
7 ms |
2816 KB |
Output is correct |
38 |
Correct |
7 ms |
2944 KB |
Output is correct |
39 |
Correct |
7 ms |
2816 KB |
Output is correct |
40 |
Correct |
21 ms |
2816 KB |
Output is correct |
41 |
Correct |
7 ms |
2816 KB |
Output is correct |
42 |
Correct |
7 ms |
2816 KB |
Output is correct |
43 |
Correct |
7 ms |
2816 KB |
Output is correct |
44 |
Correct |
7 ms |
2816 KB |
Output is correct |
45 |
Correct |
8 ms |
2944 KB |
Output is correct |
46 |
Correct |
17 ms |
3456 KB |
Output is correct |
47 |
Correct |
15 ms |
3456 KB |
Output is correct |
48 |
Correct |
15 ms |
3456 KB |
Output is correct |
49 |
Correct |
10 ms |
3584 KB |
Output is correct |
50 |
Correct |
10 ms |
3584 KB |
Output is correct |
51 |
Correct |
10 ms |
3584 KB |
Output is correct |
52 |
Correct |
13 ms |
3584 KB |
Output is correct |
53 |
Correct |
10 ms |
3456 KB |
Output is correct |
54 |
Correct |
11 ms |
3456 KB |
Output is correct |
55 |
Correct |
10 ms |
3456 KB |
Output is correct |
56 |
Correct |
11 ms |
3488 KB |
Output is correct |
57 |
Correct |
19 ms |
3328 KB |
Output is correct |
58 |
Correct |
20 ms |
3456 KB |
Output is correct |
59 |
Correct |
19 ms |
3456 KB |
Output is correct |
60 |
Correct |
20 ms |
3456 KB |
Output is correct |
61 |
Correct |
12 ms |
3456 KB |
Output is correct |
62 |
Correct |
11 ms |
3456 KB |
Output is correct |
63 |
Correct |
11 ms |
3456 KB |
Output is correct |
64 |
Correct |
14 ms |
3456 KB |
Output is correct |
65 |
Correct |
15 ms |
3440 KB |
Output is correct |
66 |
Correct |
16 ms |
3456 KB |
Output is correct |
67 |
Correct |
14 ms |
3456 KB |
Output is correct |
68 |
Correct |
9 ms |
3584 KB |
Output is correct |
69 |
Correct |
10 ms |
3456 KB |
Output is correct |
70 |
Correct |
10 ms |
3456 KB |
Output is correct |
71 |
Correct |
10 ms |
3456 KB |
Output is correct |
72 |
Correct |
19 ms |
3456 KB |
Output is correct |
73 |
Correct |
19 ms |
3456 KB |
Output is correct |
74 |
Correct |
11 ms |
3456 KB |
Output is correct |
75 |
Correct |
10 ms |
3456 KB |
Output is correct |
76 |
Correct |
10 ms |
3456 KB |
Output is correct |
77 |
Correct |
10 ms |
3456 KB |
Output is correct |
78 |
Correct |
9 ms |
3328 KB |
Output is correct |
79 |
Correct |
10 ms |
3328 KB |
Output is correct |
80 |
Correct |
9 ms |
3456 KB |
Output is correct |
81 |
Correct |
12 ms |
3456 KB |
Output is correct |
82 |
Correct |
11 ms |
3456 KB |
Output is correct |
83 |
Correct |
11 ms |
3456 KB |
Output is correct |
84 |
Correct |
11 ms |
3456 KB |
Output is correct |
85 |
Correct |
10 ms |
3408 KB |
Output is correct |
86 |
Correct |
11 ms |
3388 KB |
Output is correct |
87 |
Correct |
29 ms |
4480 KB |
Output is correct |
88 |
Correct |
95 ms |
7680 KB |
Output is correct |
89 |
Correct |
420 ms |
19956 KB |
Output is correct |
90 |
Correct |
435 ms |
19792 KB |
Output is correct |
91 |
Correct |
423 ms |
19996 KB |
Output is correct |
92 |
Correct |
135 ms |
25076 KB |
Output is correct |
93 |
Correct |
135 ms |
25040 KB |
Output is correct |
94 |
Correct |
141 ms |
25076 KB |
Output is correct |
95 |
Correct |
160 ms |
21872 KB |
Output is correct |
96 |
Correct |
165 ms |
22260 KB |
Output is correct |
97 |
Correct |
174 ms |
22260 KB |
Output is correct |
98 |
Correct |
175 ms |
22256 KB |
Output is correct |
99 |
Correct |
161 ms |
21744 KB |
Output is correct |
100 |
Correct |
726 ms |
20084 KB |
Output is correct |
101 |
Correct |
743 ms |
20464 KB |
Output is correct |
102 |
Correct |
773 ms |
20296 KB |
Output is correct |
103 |
Correct |
771 ms |
20360 KB |
Output is correct |
104 |
Correct |
222 ms |
21832 KB |
Output is correct |
105 |
Correct |
223 ms |
21748 KB |
Output is correct |
106 |
Correct |
228 ms |
21748 KB |
Output is correct |
107 |
Correct |
335 ms |
18548 KB |
Output is correct |
108 |
Correct |
373 ms |
18744 KB |
Output is correct |
109 |
Correct |
406 ms |
19188 KB |
Output is correct |
110 |
Correct |
115 ms |
24180 KB |
Output is correct |
111 |
Correct |
163 ms |
22048 KB |
Output is correct |
112 |
Correct |
146 ms |
21360 KB |
Output is correct |
113 |
Correct |
139 ms |
20724 KB |
Output is correct |
114 |
Correct |
744 ms |
19828 KB |
Output is correct |
115 |
Correct |
721 ms |
19172 KB |
Output is correct |
116 |
Correct |
212 ms |
20852 KB |
Output is correct |
117 |
Correct |
162 ms |
20340 KB |
Output is correct |
118 |
Correct |
154 ms |
20084 KB |
Output is correct |
119 |
Correct |
155 ms |
19928 KB |
Output is correct |
120 |
Correct |
146 ms |
19572 KB |
Output is correct |
121 |
Correct |
131 ms |
19316 KB |
Output is correct |
122 |
Correct |
131 ms |
19188 KB |
Output is correct |
123 |
Correct |
240 ms |
20340 KB |
Output is correct |
124 |
Correct |
236 ms |
20080 KB |
Output is correct |
125 |
Correct |
237 ms |
19960 KB |
Output is correct |
126 |
Correct |
225 ms |
19916 KB |
Output is correct |
127 |
Correct |
230 ms |
19444 KB |
Output is correct |
128 |
Correct |
229 ms |
19316 KB |
Output is correct |