#include <bits/stdc++.h>
#define MAXN 100007
#define MAXL 18
using namespace std;
vector<int> g[MAXN],vl,br,vr;
int in[MAXN],out[MAXN],p[MAXL][MAXN],t,n,c[MAXN],rd[MAXN],d[MAXN],seg[4*MAXN];
long long bit[MAXN];
map<int,int> mp;
void updseg(int l,int r,int x,int v,int ind)
{
if(l==r) {seg[ind]=v; return;}
int s=(l+r)/2;
if(x<=s) updseg(l,s,x,v,2*ind);
else updseg(s+1,r,x,v,2*ind+1);
seg[ind]=max(seg[2*ind],seg[2*ind+1]);
}
int val(int l,int r,int lt,int rt,int ind)
{
if(l>=lt && r<=rt) return seg[ind];
if(r<lt || l>rt) return 0;
int s=(l+r)/2;
return max(val(l,s,lt,rt,2*ind),val(s+1,r,lt,rt,2*ind+1));
}
void dfs(int s,int dub)
{
in[s]=++t;
d[s]=dub;
for(int i=0;i<g[s].size();i++) dfs(g[s][i],dub+1);
out[s]=t;
}
void lcainit()
{
dfs(1,0); p[0][1]=1;
for(int i=1;i<MAXL;i++) for(int j=1;j<=n;j++) p[i][j]=p[i-1][p[i-1][j]];
}
bool insub(int u,int v) {return in[u]<=in[v] && out[u]>=out[v];}
int lca(int u,int v)
{
for(int i=MAXL-1;i>=0;i--) if(!insub(p[i][u],v)) u=p[i][u];
return u;
}
void upd(int x,int val)
{
while(x<MAXN)
{
bit[x]+=val;
x+=x&-x;
}
}
long long fnd(int x)
{
long long sm=0;
while(x)
{
sm+=bit[x];
x-=x&-x;
}
return sm;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++) {scanf("%d",&c[i]); vl.push_back(c[i]);}
sort(vl.begin(),vl.end());
for(int i=0;i<n;i++) mp[vl[i]]=n-i;
for(int i=1;i<=n;i++) c[i]=mp[c[i]];
for(int i=1;i<n;i++)
{
int t1,t2;
scanf("%d%d",&t1,&t2);
g[t1].push_back(t2);
p[0][t2]=t1;
rd[i]=t2;
}
lcainit();
rd[0]=1;
for(int i=1;i<n;i++)
{
int x=1; long long sol=0;
while(x!=rd[i])
{
int r=val(1,n,in[x],out[x],1);
int w=lca(rd[i],rd[r]);
br.push_back(c[rd[r]]); vr.push_back(d[x]-d[w]);
sol+=((long long)d[w]-d[x])*fnd(c[rd[r]]-1);
upd(c[rd[r]],d[w]-d[x]);
x=w;
}
updseg(1,n,in[rd[i]],i,1);
for(int j=0;j<br.size();j++) upd(br[j],vr[j]);
br.clear(); vr.clear();
printf("%lld\n",sol);
}
}
Compilation message
construction.cpp: In function 'void dfs(int, int)':
construction.cpp:28:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[s].size();i++) dfs(g[s][i],dub+1);
~^~~~~~~~~~~~
construction.cpp: In function 'int main()':
construction.cpp:90:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0;j<br.size();j++) upd(br[j],vr[j]);
~^~~~~~~~~~
construction.cpp:62:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
construction.cpp:63:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i=1;i<=n;i++) {scanf("%d",&c[i]); vl.push_back(c[i]);}
~~~~~^~~~~~~~~~~~
construction.cpp:70:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&t1,&t2);
~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2816 KB |
Output is correct |
2 |
Correct |
4 ms |
2816 KB |
Output is correct |
3 |
Correct |
4 ms |
2816 KB |
Output is correct |
4 |
Correct |
4 ms |
2816 KB |
Output is correct |
5 |
Correct |
5 ms |
2944 KB |
Output is correct |
6 |
Correct |
6 ms |
2944 KB |
Output is correct |
7 |
Correct |
5 ms |
2816 KB |
Output is correct |
8 |
Correct |
6 ms |
2944 KB |
Output is correct |
9 |
Correct |
6 ms |
2944 KB |
Output is correct |
10 |
Correct |
5 ms |
2944 KB |
Output is correct |
11 |
Correct |
5 ms |
2944 KB |
Output is correct |
12 |
Correct |
5 ms |
2944 KB |
Output is correct |
13 |
Correct |
5 ms |
2944 KB |
Output is correct |
14 |
Correct |
6 ms |
2944 KB |
Output is correct |
15 |
Correct |
6 ms |
2944 KB |
Output is correct |
16 |
Correct |
7 ms |
2944 KB |
Output is correct |
17 |
Correct |
6 ms |
2944 KB |
Output is correct |
18 |
Correct |
6 ms |
2816 KB |
Output is correct |
19 |
Correct |
5 ms |
2944 KB |
Output is correct |
20 |
Correct |
5 ms |
2944 KB |
Output is correct |
21 |
Correct |
5 ms |
2944 KB |
Output is correct |
22 |
Correct |
4 ms |
2816 KB |
Output is correct |
23 |
Correct |
5 ms |
2944 KB |
Output is correct |
24 |
Correct |
5 ms |
2944 KB |
Output is correct |
25 |
Correct |
5 ms |
2816 KB |
Output is correct |
26 |
Correct |
5 ms |
2944 KB |
Output is correct |
27 |
Correct |
4 ms |
2944 KB |
Output is correct |
28 |
Correct |
5 ms |
2816 KB |
Output is correct |
29 |
Correct |
5 ms |
2816 KB |
Output is correct |
30 |
Correct |
5 ms |
2816 KB |
Output is correct |
31 |
Correct |
6 ms |
2816 KB |
Output is correct |
32 |
Correct |
5 ms |
2944 KB |
Output is correct |
33 |
Correct |
6 ms |
2944 KB |
Output is correct |
34 |
Correct |
5 ms |
2816 KB |
Output is correct |
35 |
Correct |
5 ms |
2816 KB |
Output is correct |
36 |
Correct |
5 ms |
2944 KB |
Output is correct |
37 |
Correct |
6 ms |
2944 KB |
Output is correct |
38 |
Correct |
5 ms |
2816 KB |
Output is correct |
39 |
Correct |
6 ms |
2944 KB |
Output is correct |
40 |
Correct |
6 ms |
2944 KB |
Output is correct |
41 |
Correct |
6 ms |
2944 KB |
Output is correct |
42 |
Correct |
5 ms |
2816 KB |
Output is correct |
43 |
Correct |
5 ms |
2816 KB |
Output is correct |
44 |
Correct |
6 ms |
2816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2816 KB |
Output is correct |
2 |
Correct |
4 ms |
2816 KB |
Output is correct |
3 |
Correct |
4 ms |
2816 KB |
Output is correct |
4 |
Correct |
4 ms |
2816 KB |
Output is correct |
5 |
Correct |
5 ms |
2944 KB |
Output is correct |
6 |
Correct |
6 ms |
2944 KB |
Output is correct |
7 |
Correct |
5 ms |
2816 KB |
Output is correct |
8 |
Correct |
6 ms |
2944 KB |
Output is correct |
9 |
Correct |
6 ms |
2944 KB |
Output is correct |
10 |
Correct |
5 ms |
2944 KB |
Output is correct |
11 |
Correct |
5 ms |
2944 KB |
Output is correct |
12 |
Correct |
5 ms |
2944 KB |
Output is correct |
13 |
Correct |
5 ms |
2944 KB |
Output is correct |
14 |
Correct |
6 ms |
2944 KB |
Output is correct |
15 |
Correct |
6 ms |
2944 KB |
Output is correct |
16 |
Correct |
7 ms |
2944 KB |
Output is correct |
17 |
Correct |
6 ms |
2944 KB |
Output is correct |
18 |
Correct |
6 ms |
2816 KB |
Output is correct |
19 |
Correct |
5 ms |
2944 KB |
Output is correct |
20 |
Correct |
5 ms |
2944 KB |
Output is correct |
21 |
Correct |
5 ms |
2944 KB |
Output is correct |
22 |
Correct |
4 ms |
2816 KB |
Output is correct |
23 |
Correct |
5 ms |
2944 KB |
Output is correct |
24 |
Correct |
5 ms |
2944 KB |
Output is correct |
25 |
Correct |
5 ms |
2816 KB |
Output is correct |
26 |
Correct |
5 ms |
2944 KB |
Output is correct |
27 |
Correct |
4 ms |
2944 KB |
Output is correct |
28 |
Correct |
5 ms |
2816 KB |
Output is correct |
29 |
Correct |
5 ms |
2816 KB |
Output is correct |
30 |
Correct |
5 ms |
2816 KB |
Output is correct |
31 |
Correct |
6 ms |
2816 KB |
Output is correct |
32 |
Correct |
5 ms |
2944 KB |
Output is correct |
33 |
Correct |
6 ms |
2944 KB |
Output is correct |
34 |
Correct |
5 ms |
2816 KB |
Output is correct |
35 |
Correct |
5 ms |
2816 KB |
Output is correct |
36 |
Correct |
5 ms |
2944 KB |
Output is correct |
37 |
Correct |
6 ms |
2944 KB |
Output is correct |
38 |
Correct |
5 ms |
2816 KB |
Output is correct |
39 |
Correct |
6 ms |
2944 KB |
Output is correct |
40 |
Correct |
6 ms |
2944 KB |
Output is correct |
41 |
Correct |
6 ms |
2944 KB |
Output is correct |
42 |
Correct |
5 ms |
2816 KB |
Output is correct |
43 |
Correct |
5 ms |
2816 KB |
Output is correct |
44 |
Correct |
6 ms |
2816 KB |
Output is correct |
45 |
Correct |
7 ms |
3072 KB |
Output is correct |
46 |
Correct |
16 ms |
3584 KB |
Output is correct |
47 |
Correct |
17 ms |
3584 KB |
Output is correct |
48 |
Correct |
18 ms |
3584 KB |
Output is correct |
49 |
Correct |
11 ms |
3840 KB |
Output is correct |
50 |
Correct |
11 ms |
3840 KB |
Output is correct |
51 |
Correct |
12 ms |
3840 KB |
Output is correct |
52 |
Correct |
12 ms |
3712 KB |
Output is correct |
53 |
Correct |
38 ms |
3656 KB |
Output is correct |
54 |
Correct |
12 ms |
3712 KB |
Output is correct |
55 |
Correct |
15 ms |
3712 KB |
Output is correct |
56 |
Correct |
12 ms |
3712 KB |
Output is correct |
57 |
Correct |
26 ms |
3584 KB |
Output is correct |
58 |
Correct |
26 ms |
3584 KB |
Output is correct |
59 |
Correct |
29 ms |
3584 KB |
Output is correct |
60 |
Correct |
24 ms |
3584 KB |
Output is correct |
61 |
Correct |
13 ms |
3712 KB |
Output is correct |
62 |
Correct |
11 ms |
3712 KB |
Output is correct |
63 |
Correct |
12 ms |
3712 KB |
Output is correct |
64 |
Correct |
19 ms |
3328 KB |
Output is correct |
65 |
Correct |
15 ms |
3300 KB |
Output is correct |
66 |
Correct |
18 ms |
3424 KB |
Output is correct |
67 |
Correct |
17 ms |
3456 KB |
Output is correct |
68 |
Correct |
10 ms |
3584 KB |
Output is correct |
69 |
Correct |
11 ms |
3584 KB |
Output is correct |
70 |
Correct |
11 ms |
3456 KB |
Output is correct |
71 |
Correct |
14 ms |
3456 KB |
Output is correct |
72 |
Correct |
22 ms |
3584 KB |
Output is correct |
73 |
Correct |
24 ms |
3328 KB |
Output is correct |
74 |
Correct |
10 ms |
3456 KB |
Output is correct |
75 |
Correct |
12 ms |
3576 KB |
Output is correct |
76 |
Correct |
12 ms |
3584 KB |
Output is correct |
77 |
Correct |
13 ms |
3712 KB |
Output is correct |
78 |
Correct |
11 ms |
3328 KB |
Output is correct |
79 |
Correct |
10 ms |
3456 KB |
Output is correct |
80 |
Correct |
11 ms |
3456 KB |
Output is correct |
81 |
Correct |
12 ms |
3584 KB |
Output is correct |
82 |
Correct |
13 ms |
3584 KB |
Output is correct |
83 |
Correct |
12 ms |
3712 KB |
Output is correct |
84 |
Correct |
11 ms |
3328 KB |
Output is correct |
85 |
Correct |
11 ms |
3456 KB |
Output is correct |
86 |
Correct |
12 ms |
3456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2816 KB |
Output is correct |
2 |
Correct |
4 ms |
2816 KB |
Output is correct |
3 |
Correct |
4 ms |
2816 KB |
Output is correct |
4 |
Correct |
4 ms |
2816 KB |
Output is correct |
5 |
Correct |
5 ms |
2944 KB |
Output is correct |
6 |
Correct |
6 ms |
2944 KB |
Output is correct |
7 |
Correct |
5 ms |
2816 KB |
Output is correct |
8 |
Correct |
6 ms |
2944 KB |
Output is correct |
9 |
Correct |
6 ms |
2944 KB |
Output is correct |
10 |
Correct |
5 ms |
2944 KB |
Output is correct |
11 |
Correct |
5 ms |
2944 KB |
Output is correct |
12 |
Correct |
5 ms |
2944 KB |
Output is correct |
13 |
Correct |
5 ms |
2944 KB |
Output is correct |
14 |
Correct |
6 ms |
2944 KB |
Output is correct |
15 |
Correct |
6 ms |
2944 KB |
Output is correct |
16 |
Correct |
7 ms |
2944 KB |
Output is correct |
17 |
Correct |
6 ms |
2944 KB |
Output is correct |
18 |
Correct |
6 ms |
2816 KB |
Output is correct |
19 |
Correct |
5 ms |
2944 KB |
Output is correct |
20 |
Correct |
5 ms |
2944 KB |
Output is correct |
21 |
Correct |
5 ms |
2944 KB |
Output is correct |
22 |
Correct |
4 ms |
2816 KB |
Output is correct |
23 |
Correct |
5 ms |
2944 KB |
Output is correct |
24 |
Correct |
5 ms |
2944 KB |
Output is correct |
25 |
Correct |
5 ms |
2816 KB |
Output is correct |
26 |
Correct |
5 ms |
2944 KB |
Output is correct |
27 |
Correct |
4 ms |
2944 KB |
Output is correct |
28 |
Correct |
5 ms |
2816 KB |
Output is correct |
29 |
Correct |
5 ms |
2816 KB |
Output is correct |
30 |
Correct |
5 ms |
2816 KB |
Output is correct |
31 |
Correct |
6 ms |
2816 KB |
Output is correct |
32 |
Correct |
5 ms |
2944 KB |
Output is correct |
33 |
Correct |
6 ms |
2944 KB |
Output is correct |
34 |
Correct |
5 ms |
2816 KB |
Output is correct |
35 |
Correct |
5 ms |
2816 KB |
Output is correct |
36 |
Correct |
5 ms |
2944 KB |
Output is correct |
37 |
Correct |
6 ms |
2944 KB |
Output is correct |
38 |
Correct |
5 ms |
2816 KB |
Output is correct |
39 |
Correct |
6 ms |
2944 KB |
Output is correct |
40 |
Correct |
6 ms |
2944 KB |
Output is correct |
41 |
Correct |
6 ms |
2944 KB |
Output is correct |
42 |
Correct |
5 ms |
2816 KB |
Output is correct |
43 |
Correct |
5 ms |
2816 KB |
Output is correct |
44 |
Correct |
6 ms |
2816 KB |
Output is correct |
45 |
Correct |
7 ms |
3072 KB |
Output is correct |
46 |
Correct |
16 ms |
3584 KB |
Output is correct |
47 |
Correct |
17 ms |
3584 KB |
Output is correct |
48 |
Correct |
18 ms |
3584 KB |
Output is correct |
49 |
Correct |
11 ms |
3840 KB |
Output is correct |
50 |
Correct |
11 ms |
3840 KB |
Output is correct |
51 |
Correct |
12 ms |
3840 KB |
Output is correct |
52 |
Correct |
12 ms |
3712 KB |
Output is correct |
53 |
Correct |
38 ms |
3656 KB |
Output is correct |
54 |
Correct |
12 ms |
3712 KB |
Output is correct |
55 |
Correct |
15 ms |
3712 KB |
Output is correct |
56 |
Correct |
12 ms |
3712 KB |
Output is correct |
57 |
Correct |
26 ms |
3584 KB |
Output is correct |
58 |
Correct |
26 ms |
3584 KB |
Output is correct |
59 |
Correct |
29 ms |
3584 KB |
Output is correct |
60 |
Correct |
24 ms |
3584 KB |
Output is correct |
61 |
Correct |
13 ms |
3712 KB |
Output is correct |
62 |
Correct |
11 ms |
3712 KB |
Output is correct |
63 |
Correct |
12 ms |
3712 KB |
Output is correct |
64 |
Correct |
19 ms |
3328 KB |
Output is correct |
65 |
Correct |
15 ms |
3300 KB |
Output is correct |
66 |
Correct |
18 ms |
3424 KB |
Output is correct |
67 |
Correct |
17 ms |
3456 KB |
Output is correct |
68 |
Correct |
10 ms |
3584 KB |
Output is correct |
69 |
Correct |
11 ms |
3584 KB |
Output is correct |
70 |
Correct |
11 ms |
3456 KB |
Output is correct |
71 |
Correct |
14 ms |
3456 KB |
Output is correct |
72 |
Correct |
22 ms |
3584 KB |
Output is correct |
73 |
Correct |
24 ms |
3328 KB |
Output is correct |
74 |
Correct |
10 ms |
3456 KB |
Output is correct |
75 |
Correct |
12 ms |
3576 KB |
Output is correct |
76 |
Correct |
12 ms |
3584 KB |
Output is correct |
77 |
Correct |
13 ms |
3712 KB |
Output is correct |
78 |
Correct |
11 ms |
3328 KB |
Output is correct |
79 |
Correct |
10 ms |
3456 KB |
Output is correct |
80 |
Correct |
11 ms |
3456 KB |
Output is correct |
81 |
Correct |
12 ms |
3584 KB |
Output is correct |
82 |
Correct |
13 ms |
3584 KB |
Output is correct |
83 |
Correct |
12 ms |
3712 KB |
Output is correct |
84 |
Correct |
11 ms |
3328 KB |
Output is correct |
85 |
Correct |
11 ms |
3456 KB |
Output is correct |
86 |
Correct |
12 ms |
3456 KB |
Output is correct |
87 |
Correct |
145 ms |
4840 KB |
Output is correct |
88 |
Correct |
176 ms |
8700 KB |
Output is correct |
89 |
Correct |
907 ms |
22620 KB |
Output is correct |
90 |
Correct |
858 ms |
22768 KB |
Output is correct |
91 |
Correct |
870 ms |
22768 KB |
Output is correct |
92 |
Correct |
552 ms |
28784 KB |
Output is correct |
93 |
Correct |
525 ms |
28852 KB |
Output is correct |
94 |
Correct |
448 ms |
28912 KB |
Output is correct |
95 |
Correct |
431 ms |
25164 KB |
Output is correct |
96 |
Correct |
467 ms |
25616 KB |
Output is correct |
97 |
Correct |
462 ms |
25616 KB |
Output is correct |
98 |
Correct |
491 ms |
25660 KB |
Output is correct |
99 |
Correct |
451 ms |
25072 KB |
Output is correct |
100 |
Correct |
1333 ms |
22872 KB |
Output is correct |
101 |
Correct |
1496 ms |
23280 KB |
Output is correct |
102 |
Correct |
1480 ms |
23284 KB |
Output is correct |
103 |
Correct |
1323 ms |
23124 KB |
Output is correct |
104 |
Correct |
666 ms |
24944 KB |
Output is correct |
105 |
Correct |
634 ms |
25068 KB |
Output is correct |
106 |
Correct |
629 ms |
24972 KB |
Output is correct |
107 |
Correct |
803 ms |
16564 KB |
Output is correct |
108 |
Correct |
793 ms |
17236 KB |
Output is correct |
109 |
Correct |
831 ms |
18928 KB |
Output is correct |
110 |
Correct |
419 ms |
23440 KB |
Output is correct |
111 |
Correct |
464 ms |
25200 KB |
Output is correct |
112 |
Correct |
373 ms |
20184 KB |
Output is correct |
113 |
Correct |
396 ms |
19696 KB |
Output is correct |
114 |
Correct |
1217 ms |
22756 KB |
Output is correct |
115 |
Correct |
1195 ms |
17908 KB |
Output is correct |
116 |
Correct |
553 ms |
19824 KB |
Output is correct |
117 |
Correct |
491 ms |
23312 KB |
Output is correct |
118 |
Correct |
520 ms |
23024 KB |
Output is correct |
119 |
Correct |
507 ms |
22640 KB |
Output is correct |
120 |
Correct |
395 ms |
18456 KB |
Output is correct |
121 |
Correct |
409 ms |
18032 KB |
Output is correct |
122 |
Correct |
425 ms |
17936 KB |
Output is correct |
123 |
Correct |
652 ms |
23260 KB |
Output is correct |
124 |
Correct |
574 ms |
22896 KB |
Output is correct |
125 |
Correct |
556 ms |
22772 KB |
Output is correct |
126 |
Correct |
515 ms |
18540 KB |
Output is correct |
127 |
Correct |
616 ms |
18060 KB |
Output is correct |
128 |
Correct |
630 ms |
17904 KB |
Output is correct |