#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
const int nmax=100005;
map<int,int> norm;
vector<int> v[nmax];
vector< pair<int,int> > l[nmax];
pair<int,int> vv[2*nmax];
long long aib[nmax];
int p[nmax],w[nmax],tt[nmax],lev[nmax],c[nmax],a[nmax],b[nmax],x[nmax];
int n,i,j,paths,nr,in,val;
long long ans;
void dfs(int x)
{
int nod,big=0;
w[x]=1;
for(int i=0;i<v[x].size();i++)
{
nod=v[x][i];lev[nod]=lev[x]+1;
dfs(nod);
w[x]+=w[nod];
if(w[nod]>w[big])
big=nod;
}
if(!big) p[x]=++paths;
else p[x]=p[big];
for(int i=0;i<v[x].size();i++)
if(v[x][i]!=big)
{
tt[p[v[x][i]]]=x;
}
}
int get(int wh,int x)
{
int ret=0;
for(int p=16;p>=0;p--)
if(ret+(1<<p)<l[wh].size()&&lev[l[wh][ret+(1<<p)].first]>=lev[x])
ret+=(1<<p);
ret++;
return ret;
}
inline int lbit(int x)
{
return ((x^(x-1))&x);
}
void update(int poz,long long val)
{
for(int idx=poz;idx<=n;idx+=lbit(idx))
aib[idx]+=val;
}
long long compute(int poz)
{
long long ret=0;
for(int idx=poz;idx>0;idx-=lbit(idx))
ret+=1LL*aib[idx];
return ret;
}
void ffffffffft(int x)
{
int wh;
nr=0;
while(x!=0)
{
in=get(p[x],x);
wh=p[x];
vv[++nr]={lev[x],l[wh][in-1].second};
for(i=in;i<l[wh].size();i++)
vv[++nr]={lev[l[wh][i].first],l[wh][i].second};
x=tt[p[x]];
}
vv[nr+1]={0,0};
ans=0;
for(val=1;val>=-1;val-=2)
{
for(i=1;i<=nr;i++)
{
update(vv[i].second,(vv[i].first-vv[i+1].first)*val);
if(val==1)
ans+=1LL*compute(vv[i].second-1)*(vv[i].first-vv[i+1].first);
}
}
}
void nttttttttt(int x,int col)
{
int wh;
while(x!=0)
{
wh=p[x];
while(l[wh].size()&&lev[l[wh].back().first]<=lev[x])
l[wh].pop_back();
l[wh].push_back({x,col});
x=tt[p[x]];
}
}
int main()
{
//freopen("data.in","r",stdin);
ios_base::sync_with_stdio(false);
cin>>n;
for(i=1;i<=n;i++)
cin>>c[i],x[i]=c[i];
sort(x+1,x+n+1);
for(i=1;i<=n;i++)
norm[x[i]]=i;
for(i=1;i<=n;i++)
c[i]=norm[c[i]];
for(i=1;i<=n-1;i++)
{
cin>>a[i]>>b[i];
v[a[i]].push_back(b[i]);
}
lev[1]=1;
dfs(1);
l[p[1]].push_back({1,c[1]});
for(int cnt=1;cnt<=n-1;cnt++)
{
ffffffffft(a[cnt]);
nttttttttt(b[cnt],c[b[cnt]]);
cout<<ans<<'\n';
}
return 0;
}
Compilation message
construction.cpp: In function 'void dfs(int)':
construction.cpp:20:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<v[x].size();i++)
~^~~~~~~~~~~~
construction.cpp:30:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<v[x].size();i++)
~^~~~~~~~~~~~
construction.cpp: In function 'int get(int, int)':
construction.cpp:40:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(ret+(1<<p)<l[wh].size()&&lev[l[wh][ret+(1<<p)].first]>=lev[x])
~~~~~~~~~~^~~~~~~~~~~~~
construction.cpp: In function 'void ffffffffft(int)':
construction.cpp:70:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=in;i<l[wh].size();i++)
~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5240 KB |
Output is correct |
3 |
Correct |
6 ms |
5240 KB |
Output is correct |
4 |
Correct |
6 ms |
5240 KB |
Output is correct |
5 |
Correct |
8 ms |
5252 KB |
Output is correct |
6 |
Correct |
6 ms |
5308 KB |
Output is correct |
7 |
Correct |
7 ms |
5316 KB |
Output is correct |
8 |
Correct |
6 ms |
5316 KB |
Output is correct |
9 |
Correct |
6 ms |
5436 KB |
Output is correct |
10 |
Correct |
6 ms |
5436 KB |
Output is correct |
11 |
Correct |
8 ms |
5436 KB |
Output is correct |
12 |
Correct |
6 ms |
5436 KB |
Output is correct |
13 |
Correct |
7 ms |
5436 KB |
Output is correct |
14 |
Correct |
6 ms |
5436 KB |
Output is correct |
15 |
Correct |
7 ms |
5580 KB |
Output is correct |
16 |
Correct |
7 ms |
5580 KB |
Output is correct |
17 |
Correct |
7 ms |
5580 KB |
Output is correct |
18 |
Correct |
9 ms |
5580 KB |
Output is correct |
19 |
Correct |
7 ms |
5636 KB |
Output is correct |
20 |
Correct |
8 ms |
5636 KB |
Output is correct |
21 |
Correct |
7 ms |
5636 KB |
Output is correct |
22 |
Correct |
6 ms |
5636 KB |
Output is correct |
23 |
Correct |
7 ms |
5636 KB |
Output is correct |
24 |
Correct |
6 ms |
5636 KB |
Output is correct |
25 |
Correct |
6 ms |
5636 KB |
Output is correct |
26 |
Correct |
7 ms |
5636 KB |
Output is correct |
27 |
Correct |
7 ms |
5636 KB |
Output is correct |
28 |
Correct |
6 ms |
5636 KB |
Output is correct |
29 |
Correct |
6 ms |
5644 KB |
Output is correct |
30 |
Correct |
6 ms |
5652 KB |
Output is correct |
31 |
Correct |
7 ms |
5704 KB |
Output is correct |
32 |
Correct |
6 ms |
5704 KB |
Output is correct |
33 |
Correct |
6 ms |
5704 KB |
Output is correct |
34 |
Correct |
7 ms |
5704 KB |
Output is correct |
35 |
Correct |
6 ms |
5704 KB |
Output is correct |
36 |
Correct |
6 ms |
5748 KB |
Output is correct |
37 |
Correct |
7 ms |
5836 KB |
Output is correct |
38 |
Correct |
6 ms |
5836 KB |
Output is correct |
39 |
Correct |
6 ms |
5836 KB |
Output is correct |
40 |
Correct |
8 ms |
5836 KB |
Output is correct |
41 |
Correct |
7 ms |
5836 KB |
Output is correct |
42 |
Correct |
7 ms |
5836 KB |
Output is correct |
43 |
Correct |
7 ms |
5836 KB |
Output is correct |
44 |
Correct |
6 ms |
5836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5240 KB |
Output is correct |
3 |
Correct |
6 ms |
5240 KB |
Output is correct |
4 |
Correct |
6 ms |
5240 KB |
Output is correct |
5 |
Correct |
8 ms |
5252 KB |
Output is correct |
6 |
Correct |
6 ms |
5308 KB |
Output is correct |
7 |
Correct |
7 ms |
5316 KB |
Output is correct |
8 |
Correct |
6 ms |
5316 KB |
Output is correct |
9 |
Correct |
6 ms |
5436 KB |
Output is correct |
10 |
Correct |
6 ms |
5436 KB |
Output is correct |
11 |
Correct |
8 ms |
5436 KB |
Output is correct |
12 |
Correct |
6 ms |
5436 KB |
Output is correct |
13 |
Correct |
7 ms |
5436 KB |
Output is correct |
14 |
Correct |
6 ms |
5436 KB |
Output is correct |
15 |
Correct |
7 ms |
5580 KB |
Output is correct |
16 |
Correct |
7 ms |
5580 KB |
Output is correct |
17 |
Correct |
7 ms |
5580 KB |
Output is correct |
18 |
Correct |
9 ms |
5580 KB |
Output is correct |
19 |
Correct |
7 ms |
5636 KB |
Output is correct |
20 |
Correct |
8 ms |
5636 KB |
Output is correct |
21 |
Correct |
7 ms |
5636 KB |
Output is correct |
22 |
Correct |
6 ms |
5636 KB |
Output is correct |
23 |
Correct |
7 ms |
5636 KB |
Output is correct |
24 |
Correct |
6 ms |
5636 KB |
Output is correct |
25 |
Correct |
6 ms |
5636 KB |
Output is correct |
26 |
Correct |
7 ms |
5636 KB |
Output is correct |
27 |
Correct |
7 ms |
5636 KB |
Output is correct |
28 |
Correct |
6 ms |
5636 KB |
Output is correct |
29 |
Correct |
6 ms |
5644 KB |
Output is correct |
30 |
Correct |
6 ms |
5652 KB |
Output is correct |
31 |
Correct |
7 ms |
5704 KB |
Output is correct |
32 |
Correct |
6 ms |
5704 KB |
Output is correct |
33 |
Correct |
6 ms |
5704 KB |
Output is correct |
34 |
Correct |
7 ms |
5704 KB |
Output is correct |
35 |
Correct |
6 ms |
5704 KB |
Output is correct |
36 |
Correct |
6 ms |
5748 KB |
Output is correct |
37 |
Correct |
7 ms |
5836 KB |
Output is correct |
38 |
Correct |
6 ms |
5836 KB |
Output is correct |
39 |
Correct |
6 ms |
5836 KB |
Output is correct |
40 |
Correct |
8 ms |
5836 KB |
Output is correct |
41 |
Correct |
7 ms |
5836 KB |
Output is correct |
42 |
Correct |
7 ms |
5836 KB |
Output is correct |
43 |
Correct |
7 ms |
5836 KB |
Output is correct |
44 |
Correct |
6 ms |
5836 KB |
Output is correct |
45 |
Correct |
7 ms |
5900 KB |
Output is correct |
46 |
Correct |
13 ms |
6304 KB |
Output is correct |
47 |
Correct |
12 ms |
6380 KB |
Output is correct |
48 |
Correct |
13 ms |
6532 KB |
Output is correct |
49 |
Correct |
10 ms |
6804 KB |
Output is correct |
50 |
Correct |
12 ms |
6896 KB |
Output is correct |
51 |
Correct |
10 ms |
6972 KB |
Output is correct |
52 |
Correct |
10 ms |
6972 KB |
Output is correct |
53 |
Correct |
11 ms |
6976 KB |
Output is correct |
54 |
Correct |
12 ms |
7072 KB |
Output is correct |
55 |
Correct |
11 ms |
7256 KB |
Output is correct |
56 |
Correct |
11 ms |
7256 KB |
Output is correct |
57 |
Correct |
13 ms |
7256 KB |
Output is correct |
58 |
Correct |
14 ms |
7256 KB |
Output is correct |
59 |
Correct |
14 ms |
7316 KB |
Output is correct |
60 |
Correct |
15 ms |
7392 KB |
Output is correct |
61 |
Correct |
11 ms |
7596 KB |
Output is correct |
62 |
Correct |
11 ms |
7676 KB |
Output is correct |
63 |
Correct |
10 ms |
7876 KB |
Output is correct |
64 |
Correct |
10 ms |
7876 KB |
Output is correct |
65 |
Correct |
11 ms |
7876 KB |
Output is correct |
66 |
Correct |
13 ms |
7876 KB |
Output is correct |
67 |
Correct |
11 ms |
7876 KB |
Output is correct |
68 |
Correct |
10 ms |
8028 KB |
Output is correct |
69 |
Correct |
11 ms |
8076 KB |
Output is correct |
70 |
Correct |
9 ms |
8076 KB |
Output is correct |
71 |
Correct |
9 ms |
8076 KB |
Output is correct |
72 |
Correct |
12 ms |
8108 KB |
Output is correct |
73 |
Correct |
12 ms |
8108 KB |
Output is correct |
74 |
Correct |
9 ms |
8108 KB |
Output is correct |
75 |
Correct |
10 ms |
8256 KB |
Output is correct |
76 |
Correct |
10 ms |
8316 KB |
Output is correct |
77 |
Correct |
10 ms |
8380 KB |
Output is correct |
78 |
Correct |
9 ms |
8380 KB |
Output is correct |
79 |
Correct |
9 ms |
8380 KB |
Output is correct |
80 |
Correct |
9 ms |
8412 KB |
Output is correct |
81 |
Correct |
10 ms |
8584 KB |
Output is correct |
82 |
Correct |
11 ms |
8652 KB |
Output is correct |
83 |
Correct |
12 ms |
8720 KB |
Output is correct |
84 |
Correct |
9 ms |
8720 KB |
Output is correct |
85 |
Correct |
9 ms |
8720 KB |
Output is correct |
86 |
Correct |
10 ms |
8748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5240 KB |
Output is correct |
3 |
Correct |
6 ms |
5240 KB |
Output is correct |
4 |
Correct |
6 ms |
5240 KB |
Output is correct |
5 |
Correct |
8 ms |
5252 KB |
Output is correct |
6 |
Correct |
6 ms |
5308 KB |
Output is correct |
7 |
Correct |
7 ms |
5316 KB |
Output is correct |
8 |
Correct |
6 ms |
5316 KB |
Output is correct |
9 |
Correct |
6 ms |
5436 KB |
Output is correct |
10 |
Correct |
6 ms |
5436 KB |
Output is correct |
11 |
Correct |
8 ms |
5436 KB |
Output is correct |
12 |
Correct |
6 ms |
5436 KB |
Output is correct |
13 |
Correct |
7 ms |
5436 KB |
Output is correct |
14 |
Correct |
6 ms |
5436 KB |
Output is correct |
15 |
Correct |
7 ms |
5580 KB |
Output is correct |
16 |
Correct |
7 ms |
5580 KB |
Output is correct |
17 |
Correct |
7 ms |
5580 KB |
Output is correct |
18 |
Correct |
9 ms |
5580 KB |
Output is correct |
19 |
Correct |
7 ms |
5636 KB |
Output is correct |
20 |
Correct |
8 ms |
5636 KB |
Output is correct |
21 |
Correct |
7 ms |
5636 KB |
Output is correct |
22 |
Correct |
6 ms |
5636 KB |
Output is correct |
23 |
Correct |
7 ms |
5636 KB |
Output is correct |
24 |
Correct |
6 ms |
5636 KB |
Output is correct |
25 |
Correct |
6 ms |
5636 KB |
Output is correct |
26 |
Correct |
7 ms |
5636 KB |
Output is correct |
27 |
Correct |
7 ms |
5636 KB |
Output is correct |
28 |
Correct |
6 ms |
5636 KB |
Output is correct |
29 |
Correct |
6 ms |
5644 KB |
Output is correct |
30 |
Correct |
6 ms |
5652 KB |
Output is correct |
31 |
Correct |
7 ms |
5704 KB |
Output is correct |
32 |
Correct |
6 ms |
5704 KB |
Output is correct |
33 |
Correct |
6 ms |
5704 KB |
Output is correct |
34 |
Correct |
7 ms |
5704 KB |
Output is correct |
35 |
Correct |
6 ms |
5704 KB |
Output is correct |
36 |
Correct |
6 ms |
5748 KB |
Output is correct |
37 |
Correct |
7 ms |
5836 KB |
Output is correct |
38 |
Correct |
6 ms |
5836 KB |
Output is correct |
39 |
Correct |
6 ms |
5836 KB |
Output is correct |
40 |
Correct |
8 ms |
5836 KB |
Output is correct |
41 |
Correct |
7 ms |
5836 KB |
Output is correct |
42 |
Correct |
7 ms |
5836 KB |
Output is correct |
43 |
Correct |
7 ms |
5836 KB |
Output is correct |
44 |
Correct |
6 ms |
5836 KB |
Output is correct |
45 |
Correct |
7 ms |
5900 KB |
Output is correct |
46 |
Correct |
13 ms |
6304 KB |
Output is correct |
47 |
Correct |
12 ms |
6380 KB |
Output is correct |
48 |
Correct |
13 ms |
6532 KB |
Output is correct |
49 |
Correct |
10 ms |
6804 KB |
Output is correct |
50 |
Correct |
12 ms |
6896 KB |
Output is correct |
51 |
Correct |
10 ms |
6972 KB |
Output is correct |
52 |
Correct |
10 ms |
6972 KB |
Output is correct |
53 |
Correct |
11 ms |
6976 KB |
Output is correct |
54 |
Correct |
12 ms |
7072 KB |
Output is correct |
55 |
Correct |
11 ms |
7256 KB |
Output is correct |
56 |
Correct |
11 ms |
7256 KB |
Output is correct |
57 |
Correct |
13 ms |
7256 KB |
Output is correct |
58 |
Correct |
14 ms |
7256 KB |
Output is correct |
59 |
Correct |
14 ms |
7316 KB |
Output is correct |
60 |
Correct |
15 ms |
7392 KB |
Output is correct |
61 |
Correct |
11 ms |
7596 KB |
Output is correct |
62 |
Correct |
11 ms |
7676 KB |
Output is correct |
63 |
Correct |
10 ms |
7876 KB |
Output is correct |
64 |
Correct |
10 ms |
7876 KB |
Output is correct |
65 |
Correct |
11 ms |
7876 KB |
Output is correct |
66 |
Correct |
13 ms |
7876 KB |
Output is correct |
67 |
Correct |
11 ms |
7876 KB |
Output is correct |
68 |
Correct |
10 ms |
8028 KB |
Output is correct |
69 |
Correct |
11 ms |
8076 KB |
Output is correct |
70 |
Correct |
9 ms |
8076 KB |
Output is correct |
71 |
Correct |
9 ms |
8076 KB |
Output is correct |
72 |
Correct |
12 ms |
8108 KB |
Output is correct |
73 |
Correct |
12 ms |
8108 KB |
Output is correct |
74 |
Correct |
9 ms |
8108 KB |
Output is correct |
75 |
Correct |
10 ms |
8256 KB |
Output is correct |
76 |
Correct |
10 ms |
8316 KB |
Output is correct |
77 |
Correct |
10 ms |
8380 KB |
Output is correct |
78 |
Correct |
9 ms |
8380 KB |
Output is correct |
79 |
Correct |
9 ms |
8380 KB |
Output is correct |
80 |
Correct |
9 ms |
8412 KB |
Output is correct |
81 |
Correct |
10 ms |
8584 KB |
Output is correct |
82 |
Correct |
11 ms |
8652 KB |
Output is correct |
83 |
Correct |
12 ms |
8720 KB |
Output is correct |
84 |
Correct |
9 ms |
8720 KB |
Output is correct |
85 |
Correct |
9 ms |
8720 KB |
Output is correct |
86 |
Correct |
10 ms |
8748 KB |
Output is correct |
87 |
Correct |
25 ms |
9692 KB |
Output is correct |
88 |
Correct |
66 ms |
12700 KB |
Output is correct |
89 |
Correct |
275 ms |
23308 KB |
Output is correct |
90 |
Correct |
268 ms |
25292 KB |
Output is correct |
91 |
Correct |
288 ms |
27552 KB |
Output is correct |
92 |
Correct |
178 ms |
36996 KB |
Output is correct |
93 |
Correct |
178 ms |
39044 KB |
Output is correct |
94 |
Correct |
178 ms |
41156 KB |
Output is correct |
95 |
Correct |
176 ms |
41156 KB |
Output is correct |
96 |
Correct |
188 ms |
42184 KB |
Output is correct |
97 |
Correct |
175 ms |
44264 KB |
Output is correct |
98 |
Correct |
182 ms |
46332 KB |
Output is correct |
99 |
Correct |
191 ms |
47828 KB |
Output is correct |
100 |
Correct |
348 ms |
47828 KB |
Output is correct |
101 |
Correct |
364 ms |
47828 KB |
Output is correct |
102 |
Correct |
364 ms |
49884 KB |
Output is correct |
103 |
Correct |
373 ms |
52076 KB |
Output is correct |
104 |
Correct |
188 ms |
57972 KB |
Output is correct |
105 |
Correct |
179 ms |
60164 KB |
Output is correct |
106 |
Correct |
181 ms |
62148 KB |
Output is correct |
107 |
Correct |
154 ms |
62148 KB |
Output is correct |
108 |
Correct |
190 ms |
62148 KB |
Output is correct |
109 |
Correct |
248 ms |
62148 KB |
Output is correct |
110 |
Correct |
103 ms |
67084 KB |
Output is correct |
111 |
Correct |
179 ms |
70636 KB |
Output is correct |
112 |
Correct |
125 ms |
70636 KB |
Output is correct |
113 |
Correct |
107 ms |
70636 KB |
Output is correct |
114 |
Correct |
315 ms |
70636 KB |
Output is correct |
115 |
Correct |
303 ms |
70636 KB |
Output is correct |
116 |
Correct |
110 ms |
70636 KB |
Output is correct |
117 |
Correct |
169 ms |
70636 KB |
Output is correct |
118 |
Correct |
176 ms |
70636 KB |
Output is correct |
119 |
Correct |
181 ms |
70636 KB |
Output is correct |
120 |
Correct |
103 ms |
70636 KB |
Output is correct |
121 |
Correct |
106 ms |
70636 KB |
Output is correct |
122 |
Correct |
113 ms |
70636 KB |
Output is correct |
123 |
Correct |
194 ms |
70636 KB |
Output is correct |
124 |
Correct |
185 ms |
70636 KB |
Output is correct |
125 |
Correct |
193 ms |
70636 KB |
Output is correct |
126 |
Correct |
130 ms |
70636 KB |
Output is correct |
127 |
Correct |
119 ms |
70636 KB |
Output is correct |
128 |
Correct |
116 ms |
70636 KB |
Output is correct |