#include<bits/stdc++.h>
#define endl '\n'
#define fi first
#define se second
using namespace std;
const int MAXN=1e5+5;
struct Fenwick
{
int n;
int tree[MAXN];
void init(int _n)
{
n=_n;
fill(tree+1,tree+n+1,0);
}
void update(int idx,int val)
{
for(;idx<=n;idx+=idx&(-idx))
{
tree[idx]+=val;
}
}
int query(int idx)
{
int ret=0;
for(;idx>0;idx-=idx&(-idx))
{
ret+=tree[idx];
}
return ret;
}
int query(int l,int r)
{
return query(r)-query(l-1);
}
};
int n;
int a[MAXN];
pair<int,int> edges[MAXN];
vector<int> adj[MAXN];
int par[MAXN];
int depth[MAXN];
int heavy[MAXN];
int head[MAXN];
int pos[MAXN];
int ptr=1;
deque<pair<int,int> > chain[MAXN];
Fenwick tree;
int dfs(int v)
{
int sz=1;
int maxSz=0;
for(auto u: adj[v])
{
depth[u]=depth[v]+1;
int curSz=dfs(u);
if(curSz>maxSz)
{
maxSz=curSz;
heavy[v]=u;
}
sz+=curSz;
}
return sz;
}
void decompose(int v,int h)
{
head[v]=h;
pos[v]=pos[h];
if(heavy[v]) decompose(heavy[v],h);
for(auto u: adj[v])
{
if(u==heavy[v]) continue;
pos[u]=ptr++;
decompose(u,u);
}
}
void HLD()
{
depth[1]=1;
dfs(1);
pos[1]=ptr++;
decompose(1,1);
}
vector<pair<int,int> > path;
set<int> s;
map<int,int> code;
long long calcInv(int v)
{
path.clear();
while(v>=1)
{
int h=head[v];
int len=depth[v]-depth[h]+1;
vector<pair<int,int> > tmp;
for(int i=0;i<chain[pos[v]].size();i++)
{
auto cur=chain[pos[v]][i];
tmp.push_back({cur.fi, min(cur.se,len)});
len-=cur.se;
if(len<=0) break;
}
reverse(tmp.begin(),tmp.end());
for(auto x: tmp) path.push_back(x);
v=par[h];
}
s.clear();
for(auto cur: path) s.insert(cur.fi);
int ptr=0;
for(auto x: s) code[x]=++ptr;
for(auto &cur: path) cur.fi=code[cur.fi];
tree.init(ptr);
long long ret=0;
for(auto cur: path)
{
ret+=1LL*tree.query(cur.fi-1)*cur.se;
tree.update(cur.fi,cur.se);
}
return ret;
}
void addEdge(int v,int val)
{
while(v>=1)
{
int h=head[v];
int len=depth[v]-depth[h]+1;
while(!chain[pos[v]].empty() && len>0)
{
auto cur=chain[pos[v]].front();
chain[pos[v]].pop_front();
len-=cur.se;
if(len<0)
{
chain[pos[v]].push_front({cur.fi, -len});
}
}
len=depth[v]-depth[h]+1;
chain[pos[v]].push_front({val,len});
v=par[h];
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=1;i<n;i++)
{
cin>>edges[i].fi>>edges[i].se;
adj[edges[i].fi].push_back(edges[i].se);
par[edges[i].se]=edges[i].fi;
}
HLD();
chain[pos[1]].push_front({a[1],1});
for(int i=1;i<n;i++)
{
cout<<calcInv(edges[i].fi)<<endl;
addEdge(edges[i].se, a[edges[i].se]);
}
return 0;
}
Compilation message
construction.cpp: In function 'long long int calcInv(int)':
construction.cpp:109:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::deque<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
109 | for(int i=0;i<chain[pos[v]].size();i++)
| ~^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
69972 KB |
Output is correct |
2 |
Correct |
28 ms |
69976 KB |
Output is correct |
3 |
Correct |
32 ms |
69980 KB |
Output is correct |
4 |
Correct |
35 ms |
70228 KB |
Output is correct |
5 |
Correct |
34 ms |
70324 KB |
Output is correct |
6 |
Correct |
33 ms |
70236 KB |
Output is correct |
7 |
Correct |
35 ms |
70224 KB |
Output is correct |
8 |
Correct |
33 ms |
70052 KB |
Output is correct |
9 |
Correct |
30 ms |
70224 KB |
Output is correct |
10 |
Correct |
34 ms |
70228 KB |
Output is correct |
11 |
Correct |
31 ms |
70224 KB |
Output is correct |
12 |
Correct |
38 ms |
70172 KB |
Output is correct |
13 |
Correct |
33 ms |
70232 KB |
Output is correct |
14 |
Correct |
32 ms |
70228 KB |
Output is correct |
15 |
Correct |
37 ms |
70236 KB |
Output is correct |
16 |
Correct |
36 ms |
70236 KB |
Output is correct |
17 |
Correct |
34 ms |
70260 KB |
Output is correct |
18 |
Correct |
34 ms |
70232 KB |
Output is correct |
19 |
Correct |
44 ms |
70300 KB |
Output is correct |
20 |
Correct |
34 ms |
70356 KB |
Output is correct |
21 |
Correct |
33 ms |
70212 KB |
Output is correct |
22 |
Correct |
33 ms |
70228 KB |
Output is correct |
23 |
Correct |
36 ms |
70316 KB |
Output is correct |
24 |
Correct |
29 ms |
70228 KB |
Output is correct |
25 |
Correct |
31 ms |
70236 KB |
Output is correct |
26 |
Correct |
32 ms |
70232 KB |
Output is correct |
27 |
Correct |
30 ms |
70236 KB |
Output is correct |
28 |
Correct |
34 ms |
70236 KB |
Output is correct |
29 |
Correct |
34 ms |
70236 KB |
Output is correct |
30 |
Correct |
32 ms |
70232 KB |
Output is correct |
31 |
Correct |
31 ms |
70236 KB |
Output is correct |
32 |
Correct |
33 ms |
70172 KB |
Output is correct |
33 |
Correct |
30 ms |
70236 KB |
Output is correct |
34 |
Correct |
35 ms |
70308 KB |
Output is correct |
35 |
Correct |
34 ms |
70228 KB |
Output is correct |
36 |
Correct |
35 ms |
70236 KB |
Output is correct |
37 |
Correct |
37 ms |
70236 KB |
Output is correct |
38 |
Correct |
33 ms |
70236 KB |
Output is correct |
39 |
Correct |
35 ms |
70236 KB |
Output is correct |
40 |
Correct |
36 ms |
70232 KB |
Output is correct |
41 |
Correct |
33 ms |
70288 KB |
Output is correct |
42 |
Correct |
34 ms |
70144 KB |
Output is correct |
43 |
Correct |
33 ms |
70240 KB |
Output is correct |
44 |
Correct |
35 ms |
70224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
69972 KB |
Output is correct |
2 |
Correct |
28 ms |
69976 KB |
Output is correct |
3 |
Correct |
32 ms |
69980 KB |
Output is correct |
4 |
Correct |
35 ms |
70228 KB |
Output is correct |
5 |
Correct |
34 ms |
70324 KB |
Output is correct |
6 |
Correct |
33 ms |
70236 KB |
Output is correct |
7 |
Correct |
35 ms |
70224 KB |
Output is correct |
8 |
Correct |
33 ms |
70052 KB |
Output is correct |
9 |
Correct |
30 ms |
70224 KB |
Output is correct |
10 |
Correct |
34 ms |
70228 KB |
Output is correct |
11 |
Correct |
31 ms |
70224 KB |
Output is correct |
12 |
Correct |
38 ms |
70172 KB |
Output is correct |
13 |
Correct |
33 ms |
70232 KB |
Output is correct |
14 |
Correct |
32 ms |
70228 KB |
Output is correct |
15 |
Correct |
37 ms |
70236 KB |
Output is correct |
16 |
Correct |
36 ms |
70236 KB |
Output is correct |
17 |
Correct |
34 ms |
70260 KB |
Output is correct |
18 |
Correct |
34 ms |
70232 KB |
Output is correct |
19 |
Correct |
44 ms |
70300 KB |
Output is correct |
20 |
Correct |
34 ms |
70356 KB |
Output is correct |
21 |
Correct |
33 ms |
70212 KB |
Output is correct |
22 |
Correct |
33 ms |
70228 KB |
Output is correct |
23 |
Correct |
36 ms |
70316 KB |
Output is correct |
24 |
Correct |
29 ms |
70228 KB |
Output is correct |
25 |
Correct |
31 ms |
70236 KB |
Output is correct |
26 |
Correct |
32 ms |
70232 KB |
Output is correct |
27 |
Correct |
30 ms |
70236 KB |
Output is correct |
28 |
Correct |
34 ms |
70236 KB |
Output is correct |
29 |
Correct |
34 ms |
70236 KB |
Output is correct |
30 |
Correct |
32 ms |
70232 KB |
Output is correct |
31 |
Correct |
31 ms |
70236 KB |
Output is correct |
32 |
Correct |
33 ms |
70172 KB |
Output is correct |
33 |
Correct |
30 ms |
70236 KB |
Output is correct |
34 |
Correct |
35 ms |
70308 KB |
Output is correct |
35 |
Correct |
34 ms |
70228 KB |
Output is correct |
36 |
Correct |
35 ms |
70236 KB |
Output is correct |
37 |
Correct |
37 ms |
70236 KB |
Output is correct |
38 |
Correct |
33 ms |
70236 KB |
Output is correct |
39 |
Correct |
35 ms |
70236 KB |
Output is correct |
40 |
Correct |
36 ms |
70232 KB |
Output is correct |
41 |
Correct |
33 ms |
70288 KB |
Output is correct |
42 |
Correct |
34 ms |
70144 KB |
Output is correct |
43 |
Correct |
33 ms |
70240 KB |
Output is correct |
44 |
Correct |
35 ms |
70224 KB |
Output is correct |
45 |
Correct |
35 ms |
70492 KB |
Output is correct |
46 |
Correct |
37 ms |
71508 KB |
Output is correct |
47 |
Correct |
39 ms |
71496 KB |
Output is correct |
48 |
Correct |
38 ms |
71596 KB |
Output is correct |
49 |
Correct |
34 ms |
70932 KB |
Output is correct |
50 |
Correct |
36 ms |
70740 KB |
Output is correct |
51 |
Correct |
35 ms |
70968 KB |
Output is correct |
52 |
Correct |
36 ms |
71768 KB |
Output is correct |
53 |
Correct |
35 ms |
71764 KB |
Output is correct |
54 |
Correct |
35 ms |
71760 KB |
Output is correct |
55 |
Correct |
37 ms |
71904 KB |
Output is correct |
56 |
Correct |
37 ms |
71672 KB |
Output is correct |
57 |
Correct |
40 ms |
71512 KB |
Output is correct |
58 |
Correct |
49 ms |
71512 KB |
Output is correct |
59 |
Correct |
48 ms |
71504 KB |
Output is correct |
60 |
Correct |
44 ms |
71508 KB |
Output is correct |
61 |
Correct |
33 ms |
71760 KB |
Output is correct |
62 |
Correct |
37 ms |
71764 KB |
Output is correct |
63 |
Correct |
37 ms |
71772 KB |
Output is correct |
64 |
Correct |
39 ms |
71256 KB |
Output is correct |
65 |
Correct |
42 ms |
71196 KB |
Output is correct |
66 |
Correct |
42 ms |
71256 KB |
Output is correct |
67 |
Correct |
36 ms |
71516 KB |
Output is correct |
68 |
Correct |
36 ms |
70736 KB |
Output is correct |
69 |
Correct |
32 ms |
71764 KB |
Output is correct |
70 |
Correct |
36 ms |
71508 KB |
Output is correct |
71 |
Correct |
39 ms |
71508 KB |
Output is correct |
72 |
Correct |
41 ms |
71504 KB |
Output is correct |
73 |
Correct |
43 ms |
71252 KB |
Output is correct |
74 |
Correct |
38 ms |
71504 KB |
Output is correct |
75 |
Correct |
34 ms |
71512 KB |
Output is correct |
76 |
Correct |
37 ms |
71516 KB |
Output is correct |
77 |
Correct |
38 ms |
71504 KB |
Output is correct |
78 |
Correct |
35 ms |
71248 KB |
Output is correct |
79 |
Correct |
34 ms |
71252 KB |
Output is correct |
80 |
Correct |
36 ms |
71356 KB |
Output is correct |
81 |
Correct |
36 ms |
71536 KB |
Output is correct |
82 |
Correct |
34 ms |
71516 KB |
Output is correct |
83 |
Correct |
32 ms |
71432 KB |
Output is correct |
84 |
Correct |
39 ms |
71508 KB |
Output is correct |
85 |
Correct |
39 ms |
71248 KB |
Output is correct |
86 |
Correct |
36 ms |
71256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
69972 KB |
Output is correct |
2 |
Correct |
28 ms |
69976 KB |
Output is correct |
3 |
Correct |
32 ms |
69980 KB |
Output is correct |
4 |
Correct |
35 ms |
70228 KB |
Output is correct |
5 |
Correct |
34 ms |
70324 KB |
Output is correct |
6 |
Correct |
33 ms |
70236 KB |
Output is correct |
7 |
Correct |
35 ms |
70224 KB |
Output is correct |
8 |
Correct |
33 ms |
70052 KB |
Output is correct |
9 |
Correct |
30 ms |
70224 KB |
Output is correct |
10 |
Correct |
34 ms |
70228 KB |
Output is correct |
11 |
Correct |
31 ms |
70224 KB |
Output is correct |
12 |
Correct |
38 ms |
70172 KB |
Output is correct |
13 |
Correct |
33 ms |
70232 KB |
Output is correct |
14 |
Correct |
32 ms |
70228 KB |
Output is correct |
15 |
Correct |
37 ms |
70236 KB |
Output is correct |
16 |
Correct |
36 ms |
70236 KB |
Output is correct |
17 |
Correct |
34 ms |
70260 KB |
Output is correct |
18 |
Correct |
34 ms |
70232 KB |
Output is correct |
19 |
Correct |
44 ms |
70300 KB |
Output is correct |
20 |
Correct |
34 ms |
70356 KB |
Output is correct |
21 |
Correct |
33 ms |
70212 KB |
Output is correct |
22 |
Correct |
33 ms |
70228 KB |
Output is correct |
23 |
Correct |
36 ms |
70316 KB |
Output is correct |
24 |
Correct |
29 ms |
70228 KB |
Output is correct |
25 |
Correct |
31 ms |
70236 KB |
Output is correct |
26 |
Correct |
32 ms |
70232 KB |
Output is correct |
27 |
Correct |
30 ms |
70236 KB |
Output is correct |
28 |
Correct |
34 ms |
70236 KB |
Output is correct |
29 |
Correct |
34 ms |
70236 KB |
Output is correct |
30 |
Correct |
32 ms |
70232 KB |
Output is correct |
31 |
Correct |
31 ms |
70236 KB |
Output is correct |
32 |
Correct |
33 ms |
70172 KB |
Output is correct |
33 |
Correct |
30 ms |
70236 KB |
Output is correct |
34 |
Correct |
35 ms |
70308 KB |
Output is correct |
35 |
Correct |
34 ms |
70228 KB |
Output is correct |
36 |
Correct |
35 ms |
70236 KB |
Output is correct |
37 |
Correct |
37 ms |
70236 KB |
Output is correct |
38 |
Correct |
33 ms |
70236 KB |
Output is correct |
39 |
Correct |
35 ms |
70236 KB |
Output is correct |
40 |
Correct |
36 ms |
70232 KB |
Output is correct |
41 |
Correct |
33 ms |
70288 KB |
Output is correct |
42 |
Correct |
34 ms |
70144 KB |
Output is correct |
43 |
Correct |
33 ms |
70240 KB |
Output is correct |
44 |
Correct |
35 ms |
70224 KB |
Output is correct |
45 |
Correct |
35 ms |
70492 KB |
Output is correct |
46 |
Correct |
37 ms |
71508 KB |
Output is correct |
47 |
Correct |
39 ms |
71496 KB |
Output is correct |
48 |
Correct |
38 ms |
71596 KB |
Output is correct |
49 |
Correct |
34 ms |
70932 KB |
Output is correct |
50 |
Correct |
36 ms |
70740 KB |
Output is correct |
51 |
Correct |
35 ms |
70968 KB |
Output is correct |
52 |
Correct |
36 ms |
71768 KB |
Output is correct |
53 |
Correct |
35 ms |
71764 KB |
Output is correct |
54 |
Correct |
35 ms |
71760 KB |
Output is correct |
55 |
Correct |
37 ms |
71904 KB |
Output is correct |
56 |
Correct |
37 ms |
71672 KB |
Output is correct |
57 |
Correct |
40 ms |
71512 KB |
Output is correct |
58 |
Correct |
49 ms |
71512 KB |
Output is correct |
59 |
Correct |
48 ms |
71504 KB |
Output is correct |
60 |
Correct |
44 ms |
71508 KB |
Output is correct |
61 |
Correct |
33 ms |
71760 KB |
Output is correct |
62 |
Correct |
37 ms |
71764 KB |
Output is correct |
63 |
Correct |
37 ms |
71772 KB |
Output is correct |
64 |
Correct |
39 ms |
71256 KB |
Output is correct |
65 |
Correct |
42 ms |
71196 KB |
Output is correct |
66 |
Correct |
42 ms |
71256 KB |
Output is correct |
67 |
Correct |
36 ms |
71516 KB |
Output is correct |
68 |
Correct |
36 ms |
70736 KB |
Output is correct |
69 |
Correct |
32 ms |
71764 KB |
Output is correct |
70 |
Correct |
36 ms |
71508 KB |
Output is correct |
71 |
Correct |
39 ms |
71508 KB |
Output is correct |
72 |
Correct |
41 ms |
71504 KB |
Output is correct |
73 |
Correct |
43 ms |
71252 KB |
Output is correct |
74 |
Correct |
38 ms |
71504 KB |
Output is correct |
75 |
Correct |
34 ms |
71512 KB |
Output is correct |
76 |
Correct |
37 ms |
71516 KB |
Output is correct |
77 |
Correct |
38 ms |
71504 KB |
Output is correct |
78 |
Correct |
35 ms |
71248 KB |
Output is correct |
79 |
Correct |
34 ms |
71252 KB |
Output is correct |
80 |
Correct |
36 ms |
71356 KB |
Output is correct |
81 |
Correct |
36 ms |
71536 KB |
Output is correct |
82 |
Correct |
34 ms |
71516 KB |
Output is correct |
83 |
Correct |
32 ms |
71432 KB |
Output is correct |
84 |
Correct |
39 ms |
71508 KB |
Output is correct |
85 |
Correct |
39 ms |
71248 KB |
Output is correct |
86 |
Correct |
36 ms |
71256 KB |
Output is correct |
87 |
Correct |
53 ms |
73808 KB |
Output is correct |
88 |
Correct |
110 ms |
81236 KB |
Output is correct |
89 |
Correct |
387 ms |
107588 KB |
Output is correct |
90 |
Correct |
383 ms |
107864 KB |
Output is correct |
91 |
Correct |
393 ms |
107604 KB |
Output is correct |
92 |
Correct |
127 ms |
91180 KB |
Output is correct |
93 |
Correct |
121 ms |
91032 KB |
Output is correct |
94 |
Correct |
124 ms |
91216 KB |
Output is correct |
95 |
Correct |
127 ms |
114372 KB |
Output is correct |
96 |
Correct |
173 ms |
114884 KB |
Output is correct |
97 |
Correct |
182 ms |
114876 KB |
Output is correct |
98 |
Correct |
175 ms |
114880 KB |
Output is correct |
99 |
Correct |
151 ms |
111708 KB |
Output is correct |
100 |
Correct |
424 ms |
99744 KB |
Output is correct |
101 |
Correct |
713 ms |
100120 KB |
Output is correct |
102 |
Correct |
674 ms |
100180 KB |
Output is correct |
103 |
Correct |
690 ms |
100204 KB |
Output is correct |
104 |
Correct |
178 ms |
111512 KB |
Output is correct |
105 |
Correct |
175 ms |
111696 KB |
Output is correct |
106 |
Correct |
179 ms |
111524 KB |
Output is correct |
107 |
Correct |
154 ms |
102232 KB |
Output is correct |
108 |
Correct |
230 ms |
102480 KB |
Output is correct |
109 |
Correct |
336 ms |
103928 KB |
Output is correct |
110 |
Correct |
87 ms |
85844 KB |
Output is correct |
111 |
Correct |
131 ms |
114368 KB |
Output is correct |
112 |
Correct |
111 ms |
107732 KB |
Output is correct |
113 |
Correct |
111 ms |
106316 KB |
Output is correct |
114 |
Correct |
416 ms |
99668 KB |
Output is correct |
115 |
Correct |
393 ms |
94800 KB |
Output is correct |
116 |
Correct |
116 ms |
106320 KB |
Output is correct |
117 |
Correct |
141 ms |
109012 KB |
Output is correct |
118 |
Correct |
156 ms |
108340 KB |
Output is correct |
119 |
Correct |
156 ms |
108112 KB |
Output is correct |
120 |
Correct |
111 ms |
104276 KB |
Output is correct |
121 |
Correct |
116 ms |
103572 KB |
Output is correct |
122 |
Correct |
129 ms |
103420 KB |
Output is correct |
123 |
Correct |
178 ms |
109172 KB |
Output is correct |
124 |
Correct |
171 ms |
108368 KB |
Output is correct |
125 |
Correct |
173 ms |
108120 KB |
Output is correct |
126 |
Correct |
138 ms |
104276 KB |
Output is correct |
127 |
Correct |
135 ms |
103584 KB |
Output is correct |
128 |
Correct |
137 ms |
103252 KB |
Output is correct |