#include <iostream>
#include <algorithm>
#include <vector>
#include <unordered_map>
using namespace std;
const int MAXN=1e5+10;
#define endl '\n'
int a[MAXN], b[MAXN], c[MAXN], d[MAXN];
int ss[MAXN], par[MAXN];
int heavy[MAXN], start[MAXN], op[MAXN], seg_pos[MAXN];
int heavy_cnt, cnt;
int tree[4*MAXN], lazy[4*MAXN];
int fen[MAXN];
unordered_map <int,int> um;
vector <int> v[MAXN];
vector <pair <int,long long> > arr;
int n;
void dfs(int x, int p) {
ss[x]=0;
for (auto i:v[x]) {
if (i==p) continue;
dfs(i,x);
par[i]=x;
ss[x]=max(ss[x],ss[i]+1);
}
}
void decompose(int x, int curh) {
seg_pos[x]=cnt;
heavy[x]=curh;
op[cnt]=x;
cnt++;
int maxind=-1;
for (auto i:v[x]) {
if (i==par[x]) continue;
if (maxind==-1 || ss[maxind]<ss[i]) maxind=i;
}
if (maxind==-1) return;
decompose(maxind,curh);
for (auto i:v[x]) {
if (i==par[x] || i==maxind) continue;
heavy_cnt++;
start[heavy_cnt]=i;
decompose(i,heavy_cnt);
}
}
int combine(int l, int r) {
if (l==-2) return r;
if (r==-2) return l;
if (l==r) return l;
return -1;
}
void push_lazy(int node, int l, int r) {
if (!lazy[node]) return;
if (l!=r) {
lazy[node*2]=lazy[node*2+1]=lazy[node];
tree[node*2]=tree[node*2+1]=lazy[node];
}
lazy[node]=0;
}
void update(int node, int l, int r, int ql, int qr, int ch) {
if (ql>r || qr<l) return;
push_lazy(node,l,r);
if (l>=ql && r<=qr) {
tree[node]=lazy[node]=ch;
return;
}
int mid=(l+r)/2;
update(node*2,l,mid,ql,qr,ch);
update(node*2+1,mid+1,r,ql,qr,ch);
tree[node]=combine(tree[node*2],tree[node*2+1]);
}
int query(int node, int l, int r, int ql, int qr) {
if (ql>r || qr<l) return -2;
push_lazy(node,l,r);
if (l>=ql && r<=qr) return tree[node];
int mid=(l+r)/2;
return combine(query(node*2,l,mid,ql,qr),query(node*2+1,mid+1,r,ql,qr));
}
void add_edge(int x, int val) {
while (x!=-1) {
int nxt=start[heavy[x]];
update(1,1,n,seg_pos[nxt],seg_pos[x],val);
x=par[nxt];
}
}
void update_fen(int ind, int ch) {
while (ind<=n) {
fen[ind]+=ch;
ind+=(ind & (-ind));
}
}
int query_fen(int ind) {
int res=0;
while (ind>0) {
res+=fen[ind];
ind-=(ind & (-ind));
}
return res;
}
int find_next(int l, int r, int val) {
int cur=r;
int mid;
while (l<=r) {
mid=(l+r)/2;
if (query(1,1,n,mid,cur)==val) r=mid-1;
else l=mid+1;
}
return op[r];
}
long long find_ans(int x) {
if (!arr.empty()) arr.clear();
int sa=-1;
while (x!=-1) {
int curv=query(1,1,n,seg_pos[x],seg_pos[x]);
arr.push_back({curv,0});
sa++;
int nxt;
while (x!=-1) {
nxt=start[heavy[x]];
int s=query(1,1,n,seg_pos[nxt],seg_pos[x]);
if (s!=curv) break;
arr[sa].second+=seg_pos[x]-seg_pos[nxt]+1;
x=par[nxt];
}
if (x!=-1) {
int newx=find_next(seg_pos[nxt],seg_pos[x],curv);
arr[sa].second+=seg_pos[x]-seg_pos[newx];
x=newx;
}
}
long long ans=0;
for (int i=arr.size()-1;i>=0;i--) {
long long curs=query_fen(n)-query_fen(arr[i].first);
curs*=arr[i].second;
ans+=curs;
update_fen(arr[i].first,arr[i].second);
}
for (int i=0;i<arr.size();i++) update_fen(arr[i].first,-arr[i].second);
return ans;
}
int main () {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i=1;i<=n;i++) {
cin >> c[i];
d[i]=c[i];
}
sort(d+1,d+n+1);
for (int i=1;i<=n;i++) {
if (um[d[i]]==0) um[d[i]]=i;
}
for (int i=1;i<=n;i++) c[i]=um[c[i]];
for (int i=0;i<n-1;i++) {
cin >> a[i] >> b[i];
v[a[i]].push_back(b[i]);
v[b[i]].push_back(a[i]);
}
dfs(1,-1);
start[1]=1;
heavy_cnt=cnt=1;
par[1]=-1;
decompose(1,1);
update(1,1,n,1,1,1);
for (int i=0;i<n-1;i++) {
cout << find_ans(a[i]) << endl;
add_edge(b[i],c[b[i]]);
}
return 0;
}
Compilation message
construction.cpp: In function 'long long int find_ans(int)':
construction.cpp:138:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
138 | for (int i=0;i<arr.size();i++) update_fen(arr[i].first,-arr[i].second);
| ~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
9048 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
3 ms |
8796 KB |
Output is correct |
7 |
Correct |
3 ms |
8900 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 KB |
Output is correct |
9 |
Correct |
2 ms |
8796 KB |
Output is correct |
10 |
Correct |
2 ms |
8796 KB |
Output is correct |
11 |
Correct |
2 ms |
8804 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
2 ms |
8796 KB |
Output is correct |
15 |
Correct |
3 ms |
8796 KB |
Output is correct |
16 |
Correct |
3 ms |
8796 KB |
Output is correct |
17 |
Correct |
3 ms |
8796 KB |
Output is correct |
18 |
Correct |
3 ms |
8796 KB |
Output is correct |
19 |
Correct |
2 ms |
8796 KB |
Output is correct |
20 |
Correct |
2 ms |
8796 KB |
Output is correct |
21 |
Correct |
2 ms |
8796 KB |
Output is correct |
22 |
Correct |
2 ms |
8796 KB |
Output is correct |
23 |
Correct |
3 ms |
8840 KB |
Output is correct |
24 |
Correct |
5 ms |
8840 KB |
Output is correct |
25 |
Correct |
3 ms |
8796 KB |
Output is correct |
26 |
Correct |
2 ms |
8796 KB |
Output is correct |
27 |
Correct |
3 ms |
8796 KB |
Output is correct |
28 |
Correct |
2 ms |
8796 KB |
Output is correct |
29 |
Correct |
2 ms |
8792 KB |
Output is correct |
30 |
Correct |
4 ms |
8796 KB |
Output is correct |
31 |
Correct |
3 ms |
8796 KB |
Output is correct |
32 |
Correct |
2 ms |
8836 KB |
Output is correct |
33 |
Correct |
2 ms |
8796 KB |
Output is correct |
34 |
Correct |
2 ms |
8796 KB |
Output is correct |
35 |
Correct |
2 ms |
8796 KB |
Output is correct |
36 |
Correct |
2 ms |
8796 KB |
Output is correct |
37 |
Correct |
2 ms |
8796 KB |
Output is correct |
38 |
Correct |
2 ms |
8796 KB |
Output is correct |
39 |
Correct |
2 ms |
8796 KB |
Output is correct |
40 |
Correct |
2 ms |
8796 KB |
Output is correct |
41 |
Correct |
2 ms |
8836 KB |
Output is correct |
42 |
Correct |
2 ms |
8792 KB |
Output is correct |
43 |
Correct |
2 ms |
8796 KB |
Output is correct |
44 |
Correct |
2 ms |
8796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
9048 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
3 ms |
8796 KB |
Output is correct |
7 |
Correct |
3 ms |
8900 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 KB |
Output is correct |
9 |
Correct |
2 ms |
8796 KB |
Output is correct |
10 |
Correct |
2 ms |
8796 KB |
Output is correct |
11 |
Correct |
2 ms |
8804 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
2 ms |
8796 KB |
Output is correct |
15 |
Correct |
3 ms |
8796 KB |
Output is correct |
16 |
Correct |
3 ms |
8796 KB |
Output is correct |
17 |
Correct |
3 ms |
8796 KB |
Output is correct |
18 |
Correct |
3 ms |
8796 KB |
Output is correct |
19 |
Correct |
2 ms |
8796 KB |
Output is correct |
20 |
Correct |
2 ms |
8796 KB |
Output is correct |
21 |
Correct |
2 ms |
8796 KB |
Output is correct |
22 |
Correct |
2 ms |
8796 KB |
Output is correct |
23 |
Correct |
3 ms |
8840 KB |
Output is correct |
24 |
Correct |
5 ms |
8840 KB |
Output is correct |
25 |
Correct |
3 ms |
8796 KB |
Output is correct |
26 |
Correct |
2 ms |
8796 KB |
Output is correct |
27 |
Correct |
3 ms |
8796 KB |
Output is correct |
28 |
Correct |
2 ms |
8796 KB |
Output is correct |
29 |
Correct |
2 ms |
8792 KB |
Output is correct |
30 |
Correct |
4 ms |
8796 KB |
Output is correct |
31 |
Correct |
3 ms |
8796 KB |
Output is correct |
32 |
Correct |
2 ms |
8836 KB |
Output is correct |
33 |
Correct |
2 ms |
8796 KB |
Output is correct |
34 |
Correct |
2 ms |
8796 KB |
Output is correct |
35 |
Correct |
2 ms |
8796 KB |
Output is correct |
36 |
Correct |
2 ms |
8796 KB |
Output is correct |
37 |
Correct |
2 ms |
8796 KB |
Output is correct |
38 |
Correct |
2 ms |
8796 KB |
Output is correct |
39 |
Correct |
2 ms |
8796 KB |
Output is correct |
40 |
Correct |
2 ms |
8796 KB |
Output is correct |
41 |
Correct |
2 ms |
8836 KB |
Output is correct |
42 |
Correct |
2 ms |
8792 KB |
Output is correct |
43 |
Correct |
2 ms |
8796 KB |
Output is correct |
44 |
Correct |
2 ms |
8796 KB |
Output is correct |
45 |
Correct |
4 ms |
8968 KB |
Output is correct |
46 |
Correct |
15 ms |
9048 KB |
Output is correct |
47 |
Correct |
15 ms |
9052 KB |
Output is correct |
48 |
Correct |
15 ms |
9052 KB |
Output is correct |
49 |
Correct |
6 ms |
9564 KB |
Output is correct |
50 |
Correct |
5 ms |
9564 KB |
Output is correct |
51 |
Correct |
5 ms |
9356 KB |
Output is correct |
52 |
Correct |
9 ms |
9304 KB |
Output is correct |
53 |
Correct |
11 ms |
9308 KB |
Output is correct |
54 |
Correct |
11 ms |
9440 KB |
Output is correct |
55 |
Correct |
9 ms |
9308 KB |
Output is correct |
56 |
Correct |
9 ms |
9308 KB |
Output is correct |
57 |
Correct |
27 ms |
9052 KB |
Output is correct |
58 |
Correct |
28 ms |
9052 KB |
Output is correct |
59 |
Correct |
28 ms |
9260 KB |
Output is correct |
60 |
Correct |
27 ms |
9052 KB |
Output is correct |
61 |
Correct |
11 ms |
9308 KB |
Output is correct |
62 |
Correct |
11 ms |
9308 KB |
Output is correct |
63 |
Correct |
11 ms |
9424 KB |
Output is correct |
64 |
Correct |
11 ms |
9052 KB |
Output is correct |
65 |
Correct |
13 ms |
9048 KB |
Output is correct |
66 |
Correct |
14 ms |
9052 KB |
Output is correct |
67 |
Correct |
14 ms |
9052 KB |
Output is correct |
68 |
Correct |
4 ms |
9308 KB |
Output is correct |
69 |
Correct |
9 ms |
9364 KB |
Output is correct |
70 |
Correct |
8 ms |
9052 KB |
Output is correct |
71 |
Correct |
8 ms |
9244 KB |
Output is correct |
72 |
Correct |
26 ms |
9268 KB |
Output is correct |
73 |
Correct |
28 ms |
9048 KB |
Output is correct |
74 |
Correct |
10 ms |
9052 KB |
Output is correct |
75 |
Correct |
10 ms |
9100 KB |
Output is correct |
76 |
Correct |
9 ms |
9304 KB |
Output is correct |
77 |
Correct |
9 ms |
9308 KB |
Output is correct |
78 |
Correct |
9 ms |
9052 KB |
Output is correct |
79 |
Correct |
9 ms |
9048 KB |
Output is correct |
80 |
Correct |
9 ms |
9260 KB |
Output is correct |
81 |
Correct |
12 ms |
9564 KB |
Output is correct |
82 |
Correct |
12 ms |
9308 KB |
Output is correct |
83 |
Correct |
11 ms |
9308 KB |
Output is correct |
84 |
Correct |
11 ms |
9108 KB |
Output is correct |
85 |
Correct |
12 ms |
9052 KB |
Output is correct |
86 |
Correct |
12 ms |
9048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
9048 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
3 ms |
8796 KB |
Output is correct |
7 |
Correct |
3 ms |
8900 KB |
Output is correct |
8 |
Correct |
2 ms |
8796 KB |
Output is correct |
9 |
Correct |
2 ms |
8796 KB |
Output is correct |
10 |
Correct |
2 ms |
8796 KB |
Output is correct |
11 |
Correct |
2 ms |
8804 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
2 ms |
8796 KB |
Output is correct |
15 |
Correct |
3 ms |
8796 KB |
Output is correct |
16 |
Correct |
3 ms |
8796 KB |
Output is correct |
17 |
Correct |
3 ms |
8796 KB |
Output is correct |
18 |
Correct |
3 ms |
8796 KB |
Output is correct |
19 |
Correct |
2 ms |
8796 KB |
Output is correct |
20 |
Correct |
2 ms |
8796 KB |
Output is correct |
21 |
Correct |
2 ms |
8796 KB |
Output is correct |
22 |
Correct |
2 ms |
8796 KB |
Output is correct |
23 |
Correct |
3 ms |
8840 KB |
Output is correct |
24 |
Correct |
5 ms |
8840 KB |
Output is correct |
25 |
Correct |
3 ms |
8796 KB |
Output is correct |
26 |
Correct |
2 ms |
8796 KB |
Output is correct |
27 |
Correct |
3 ms |
8796 KB |
Output is correct |
28 |
Correct |
2 ms |
8796 KB |
Output is correct |
29 |
Correct |
2 ms |
8792 KB |
Output is correct |
30 |
Correct |
4 ms |
8796 KB |
Output is correct |
31 |
Correct |
3 ms |
8796 KB |
Output is correct |
32 |
Correct |
2 ms |
8836 KB |
Output is correct |
33 |
Correct |
2 ms |
8796 KB |
Output is correct |
34 |
Correct |
2 ms |
8796 KB |
Output is correct |
35 |
Correct |
2 ms |
8796 KB |
Output is correct |
36 |
Correct |
2 ms |
8796 KB |
Output is correct |
37 |
Correct |
2 ms |
8796 KB |
Output is correct |
38 |
Correct |
2 ms |
8796 KB |
Output is correct |
39 |
Correct |
2 ms |
8796 KB |
Output is correct |
40 |
Correct |
2 ms |
8796 KB |
Output is correct |
41 |
Correct |
2 ms |
8836 KB |
Output is correct |
42 |
Correct |
2 ms |
8792 KB |
Output is correct |
43 |
Correct |
2 ms |
8796 KB |
Output is correct |
44 |
Correct |
2 ms |
8796 KB |
Output is correct |
45 |
Correct |
4 ms |
8968 KB |
Output is correct |
46 |
Correct |
15 ms |
9048 KB |
Output is correct |
47 |
Correct |
15 ms |
9052 KB |
Output is correct |
48 |
Correct |
15 ms |
9052 KB |
Output is correct |
49 |
Correct |
6 ms |
9564 KB |
Output is correct |
50 |
Correct |
5 ms |
9564 KB |
Output is correct |
51 |
Correct |
5 ms |
9356 KB |
Output is correct |
52 |
Correct |
9 ms |
9304 KB |
Output is correct |
53 |
Correct |
11 ms |
9308 KB |
Output is correct |
54 |
Correct |
11 ms |
9440 KB |
Output is correct |
55 |
Correct |
9 ms |
9308 KB |
Output is correct |
56 |
Correct |
9 ms |
9308 KB |
Output is correct |
57 |
Correct |
27 ms |
9052 KB |
Output is correct |
58 |
Correct |
28 ms |
9052 KB |
Output is correct |
59 |
Correct |
28 ms |
9260 KB |
Output is correct |
60 |
Correct |
27 ms |
9052 KB |
Output is correct |
61 |
Correct |
11 ms |
9308 KB |
Output is correct |
62 |
Correct |
11 ms |
9308 KB |
Output is correct |
63 |
Correct |
11 ms |
9424 KB |
Output is correct |
64 |
Correct |
11 ms |
9052 KB |
Output is correct |
65 |
Correct |
13 ms |
9048 KB |
Output is correct |
66 |
Correct |
14 ms |
9052 KB |
Output is correct |
67 |
Correct |
14 ms |
9052 KB |
Output is correct |
68 |
Correct |
4 ms |
9308 KB |
Output is correct |
69 |
Correct |
9 ms |
9364 KB |
Output is correct |
70 |
Correct |
8 ms |
9052 KB |
Output is correct |
71 |
Correct |
8 ms |
9244 KB |
Output is correct |
72 |
Correct |
26 ms |
9268 KB |
Output is correct |
73 |
Correct |
28 ms |
9048 KB |
Output is correct |
74 |
Correct |
10 ms |
9052 KB |
Output is correct |
75 |
Correct |
10 ms |
9100 KB |
Output is correct |
76 |
Correct |
9 ms |
9304 KB |
Output is correct |
77 |
Correct |
9 ms |
9308 KB |
Output is correct |
78 |
Correct |
9 ms |
9052 KB |
Output is correct |
79 |
Correct |
9 ms |
9048 KB |
Output is correct |
80 |
Correct |
9 ms |
9260 KB |
Output is correct |
81 |
Correct |
12 ms |
9564 KB |
Output is correct |
82 |
Correct |
12 ms |
9308 KB |
Output is correct |
83 |
Correct |
11 ms |
9308 KB |
Output is correct |
84 |
Correct |
11 ms |
9108 KB |
Output is correct |
85 |
Correct |
12 ms |
9052 KB |
Output is correct |
86 |
Correct |
12 ms |
9048 KB |
Output is correct |
87 |
Correct |
54 ms |
9820 KB |
Output is correct |
88 |
Correct |
155 ms |
12128 KB |
Output is correct |
89 |
Correct |
649 ms |
20172 KB |
Output is correct |
90 |
Correct |
622 ms |
20460 KB |
Output is correct |
91 |
Correct |
618 ms |
20196 KB |
Output is correct |
92 |
Correct |
116 ms |
27992 KB |
Output is correct |
93 |
Correct |
121 ms |
27916 KB |
Output is correct |
94 |
Correct |
115 ms |
27988 KB |
Output is correct |
95 |
Correct |
296 ms |
25276 KB |
Output is correct |
96 |
Correct |
306 ms |
25744 KB |
Output is correct |
97 |
Correct |
310 ms |
26116 KB |
Output is correct |
98 |
Correct |
300 ms |
25800 KB |
Output is correct |
99 |
Correct |
337 ms |
24404 KB |
Output is correct |
100 |
Correct |
1235 ms |
20048 KB |
Output is correct |
101 |
Correct |
1258 ms |
20124 KB |
Output is correct |
102 |
Correct |
1278 ms |
20464 KB |
Output is correct |
103 |
Correct |
1301 ms |
20248 KB |
Output is correct |
104 |
Correct |
401 ms |
24000 KB |
Output is correct |
105 |
Correct |
419 ms |
24176 KB |
Output is correct |
106 |
Correct |
401 ms |
24004 KB |
Output is correct |
107 |
Correct |
427 ms |
15172 KB |
Output is correct |
108 |
Correct |
621 ms |
15656 KB |
Output is correct |
109 |
Correct |
653 ms |
16660 KB |
Output is correct |
110 |
Correct |
101 ms |
22868 KB |
Output is correct |
111 |
Correct |
303 ms |
25544 KB |
Output is correct |
112 |
Correct |
287 ms |
19916 KB |
Output is correct |
113 |
Correct |
291 ms |
19092 KB |
Output is correct |
114 |
Correct |
1261 ms |
20168 KB |
Output is correct |
115 |
Correct |
1275 ms |
15296 KB |
Output is correct |
116 |
Correct |
378 ms |
19024 KB |
Output is correct |
117 |
Correct |
332 ms |
21748 KB |
Output is correct |
118 |
Correct |
320 ms |
20964 KB |
Output is correct |
119 |
Correct |
328 ms |
20680 KB |
Output is correct |
120 |
Correct |
309 ms |
17056 KB |
Output is correct |
121 |
Correct |
313 ms |
16488 KB |
Output is correct |
122 |
Correct |
306 ms |
16140 KB |
Output is correct |
123 |
Correct |
464 ms |
21696 KB |
Output is correct |
124 |
Correct |
450 ms |
21040 KB |
Output is correct |
125 |
Correct |
456 ms |
20808 KB |
Output is correct |
126 |
Correct |
445 ms |
17176 KB |
Output is correct |
127 |
Correct |
452 ms |
16420 KB |
Output is correct |
128 |
Correct |
443 ms |
16304 KB |
Output is correct |