#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct segTree{
int tree[400002];
int lazy[400002]; /// 즉시 대입
void init(int i, int l, int r){
tree[i] = lazy[i] = 0;
if(l==r) return;
int m = (l+r)>>1;
init(i*2, l, m);
init(i*2+1, m+1, r);
}
void propagate(int i, int l, int r){
if(!lazy[i]) return;
if(lazy[i]) tree[i] = lazy[i];
if(l!=r){
lazy[i*2] = lazy[i*2+1] = lazy[i];
}
lazy[i] = 0;
}
void update(int i, int l, int r, int s, int e, int val){
propagate(i, l, r);
if(r<s || e<l) return;
if(s<=l && r<=e){
lazy[i] = val;
propagate(i, l, r);
return;
}
int m = (l+r)>>1;
update(i*2, l, m, s, e, val);
update(i*2+1, m+1, r, s, e, val);
}
int getVal(int i, int l, int r, int idx){
propagate(i, l, r);
if(l==r) return tree[i];
int m = (l+r)>>1;
if(idx <= m) return getVal(i*2, l, m, idx);
else return getVal(i*2+1, m+1, r, idx);
}
} tree;
struct Fenwick{
int n;
int arr[100002];
void init(int _n){
n = _n;
fill(arr+1, arr+_n+1, 0);
}
void update(int x, int val){
while(x<=n){
arr[x] += val;
x += x&-x;
}
}
int sum(int x){
int ret = 0;
while(x){
ret += arr[x];
x -= x&-x;
}
return ret;
}
} fenwick;
int n;
int arr[100002];
int par[100002];
int qx[100002], qy[100002];
vector<int> link[100002];
int in[100002], sz[100002], idx[100002], top[100002], depth[100002], inCnt;
int LCADB[100002][20];
void dfs_sz(int x){
LCADB[x][0] = par[x];
sz[x] = 1;
for(auto &y: link[x]){
depth[y] = depth[x] + 1;
dfs_sz(y);
sz[x] += sz[y];
if(sz[y] > sz[link[x][0]]) swap(y, link[x][0]);
}
}
void dfs_hld(int x){
in[x] = ++inCnt;
idx[in[x]] = x;
for(auto y: link[x]){
top[y] = (y == link[x][0]) ? top[x] : y;
dfs_hld(y);
}
}
int kth_parent(int x, int k){
for(int d=0; d<20; d++) if((k>>d)&1) x = LCADB[x][d];
return x;
}
void renumber(){
vector<int> tarr;
for(int i=1; i<=n; i++) tarr.push_back(arr[i]);
sort(tarr.begin(), tarr.end());
tarr.erase(unique(tarr.begin(), tarr.end()), tarr.end());
for(int i=1; i<=n; i++) arr[i] = lower_bound(tarr.begin(), tarr.end(), arr[i]) - tarr.begin() + 1;
}
int main(){
scanf("%d", &n);
for(int i=1; i<=n; i++){
scanf("%d", &arr[i]);
}
renumber();
for(int i=1; i<n; i++){
scanf("%d %d", &qx[i], &qy[i]);
par[qy[i]] = qx[i];
link[qx[i]].push_back(qy[i]);
}
depth[1] = 1;
dfs_sz(1);
top[1] = 1;
dfs_hld(1);
tree.init(1, 1, n);
for(int d=1; d<20; d++) for(int i=1; i<=n; i++) LCADB[i][d] = LCADB[LCADB[i][d-1]][d-1];
for(int i=1; i<=n; i++){
tree.update(1, 1, n, i, i, i);
}
fenwick.init(n);
for(int i=1; i<n; i++){
vector<pair<int, int> > vec;
int x = qx[i];
vec.push_back(make_pair(tree.getVal(1, 1, n, in[x]), depth[x]));
while(x){
if(vec.back().first == tree.getVal(1, 1, n, in[top[x]])){
x = par[top[x]];
continue;
}
int MIN = depth[top[x]], MAX = depth[x], ANS = MIN;
while(MIN <= MAX){
int MID = (MIN + MAX) / 2;
int kth = kth_parent(x, depth[x]-MID);
if(vec.back().first == tree.getVal(1, 1, n, in[kth])){
MAX = MID - 1;
}
else{
ANS = MID;
MIN = MID + 1;
}
}
x = kth_parent(x, depth[x]-ANS);
vec.push_back(make_pair(tree.getVal(1, 1, n, in[x]), depth[x]));
}
for(int i=0; i<(int)vec.size(); i++){
vec[i].first = arr[vec[i].first];
vec[i].second = (i == (int)vec.size()-1) ? vec[i].second : vec[i].second - vec[i+1].second;
}
ll rans = 0;
for(auto p: vec){
rans += ll(p.second) * ll(fenwick.sum(p.first-1));
fenwick.update(p.first, p.second);
}
for(auto p: vec) fenwick.update(p.first, -p.second);
printf("%lld\n", rans);
x = qy[i];
while(x){
tree.update(1, 1, n, in[top[x]], in[x], qy[i]);
x = par[top[x]];
}
}
}
Compilation message
construction.cpp: In function 'int main()':
construction.cpp:116:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
116 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
construction.cpp:118:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
118 | scanf("%d", &arr[i]);
| ~~~~~^~~~~~~~~~~~~~~
construction.cpp:122:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
122 | scanf("%d %d", &qx[i], &qy[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2796 KB |
Output is correct |
2 |
Correct |
2 ms |
2796 KB |
Output is correct |
3 |
Correct |
2 ms |
2796 KB |
Output is correct |
4 |
Correct |
3 ms |
2796 KB |
Output is correct |
5 |
Correct |
3 ms |
2816 KB |
Output is correct |
6 |
Correct |
3 ms |
2796 KB |
Output is correct |
7 |
Correct |
3 ms |
2796 KB |
Output is correct |
8 |
Correct |
3 ms |
2872 KB |
Output is correct |
9 |
Correct |
3 ms |
2796 KB |
Output is correct |
10 |
Correct |
3 ms |
2796 KB |
Output is correct |
11 |
Correct |
3 ms |
2796 KB |
Output is correct |
12 |
Correct |
3 ms |
2796 KB |
Output is correct |
13 |
Correct |
3 ms |
2796 KB |
Output is correct |
14 |
Correct |
3 ms |
2796 KB |
Output is correct |
15 |
Correct |
4 ms |
2796 KB |
Output is correct |
16 |
Correct |
4 ms |
2796 KB |
Output is correct |
17 |
Correct |
5 ms |
2796 KB |
Output is correct |
18 |
Correct |
4 ms |
2796 KB |
Output is correct |
19 |
Correct |
3 ms |
2944 KB |
Output is correct |
20 |
Correct |
3 ms |
2796 KB |
Output is correct |
21 |
Correct |
3 ms |
2796 KB |
Output is correct |
22 |
Correct |
3 ms |
2796 KB |
Output is correct |
23 |
Correct |
3 ms |
2796 KB |
Output is correct |
24 |
Correct |
3 ms |
2796 KB |
Output is correct |
25 |
Correct |
3 ms |
2796 KB |
Output is correct |
26 |
Correct |
3 ms |
2796 KB |
Output is correct |
27 |
Correct |
3 ms |
2924 KB |
Output is correct |
28 |
Correct |
3 ms |
2796 KB |
Output is correct |
29 |
Correct |
3 ms |
2796 KB |
Output is correct |
30 |
Correct |
4 ms |
2796 KB |
Output is correct |
31 |
Correct |
4 ms |
2796 KB |
Output is correct |
32 |
Correct |
3 ms |
2796 KB |
Output is correct |
33 |
Correct |
3 ms |
2796 KB |
Output is correct |
34 |
Correct |
3 ms |
2796 KB |
Output is correct |
35 |
Correct |
3 ms |
2796 KB |
Output is correct |
36 |
Correct |
3 ms |
2796 KB |
Output is correct |
37 |
Correct |
3 ms |
2796 KB |
Output is correct |
38 |
Correct |
3 ms |
2796 KB |
Output is correct |
39 |
Correct |
3 ms |
2796 KB |
Output is correct |
40 |
Correct |
3 ms |
2796 KB |
Output is correct |
41 |
Correct |
3 ms |
2796 KB |
Output is correct |
42 |
Correct |
3 ms |
2796 KB |
Output is correct |
43 |
Correct |
3 ms |
2796 KB |
Output is correct |
44 |
Correct |
4 ms |
2796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2796 KB |
Output is correct |
2 |
Correct |
2 ms |
2796 KB |
Output is correct |
3 |
Correct |
2 ms |
2796 KB |
Output is correct |
4 |
Correct |
3 ms |
2796 KB |
Output is correct |
5 |
Correct |
3 ms |
2816 KB |
Output is correct |
6 |
Correct |
3 ms |
2796 KB |
Output is correct |
7 |
Correct |
3 ms |
2796 KB |
Output is correct |
8 |
Correct |
3 ms |
2872 KB |
Output is correct |
9 |
Correct |
3 ms |
2796 KB |
Output is correct |
10 |
Correct |
3 ms |
2796 KB |
Output is correct |
11 |
Correct |
3 ms |
2796 KB |
Output is correct |
12 |
Correct |
3 ms |
2796 KB |
Output is correct |
13 |
Correct |
3 ms |
2796 KB |
Output is correct |
14 |
Correct |
3 ms |
2796 KB |
Output is correct |
15 |
Correct |
4 ms |
2796 KB |
Output is correct |
16 |
Correct |
4 ms |
2796 KB |
Output is correct |
17 |
Correct |
5 ms |
2796 KB |
Output is correct |
18 |
Correct |
4 ms |
2796 KB |
Output is correct |
19 |
Correct |
3 ms |
2944 KB |
Output is correct |
20 |
Correct |
3 ms |
2796 KB |
Output is correct |
21 |
Correct |
3 ms |
2796 KB |
Output is correct |
22 |
Correct |
3 ms |
2796 KB |
Output is correct |
23 |
Correct |
3 ms |
2796 KB |
Output is correct |
24 |
Correct |
3 ms |
2796 KB |
Output is correct |
25 |
Correct |
3 ms |
2796 KB |
Output is correct |
26 |
Correct |
3 ms |
2796 KB |
Output is correct |
27 |
Correct |
3 ms |
2924 KB |
Output is correct |
28 |
Correct |
3 ms |
2796 KB |
Output is correct |
29 |
Correct |
3 ms |
2796 KB |
Output is correct |
30 |
Correct |
4 ms |
2796 KB |
Output is correct |
31 |
Correct |
4 ms |
2796 KB |
Output is correct |
32 |
Correct |
3 ms |
2796 KB |
Output is correct |
33 |
Correct |
3 ms |
2796 KB |
Output is correct |
34 |
Correct |
3 ms |
2796 KB |
Output is correct |
35 |
Correct |
3 ms |
2796 KB |
Output is correct |
36 |
Correct |
3 ms |
2796 KB |
Output is correct |
37 |
Correct |
3 ms |
2796 KB |
Output is correct |
38 |
Correct |
3 ms |
2796 KB |
Output is correct |
39 |
Correct |
3 ms |
2796 KB |
Output is correct |
40 |
Correct |
3 ms |
2796 KB |
Output is correct |
41 |
Correct |
3 ms |
2796 KB |
Output is correct |
42 |
Correct |
3 ms |
2796 KB |
Output is correct |
43 |
Correct |
3 ms |
2796 KB |
Output is correct |
44 |
Correct |
4 ms |
2796 KB |
Output is correct |
45 |
Correct |
5 ms |
2924 KB |
Output is correct |
46 |
Correct |
19 ms |
3436 KB |
Output is correct |
47 |
Correct |
22 ms |
3436 KB |
Output is correct |
48 |
Correct |
16 ms |
3436 KB |
Output is correct |
49 |
Correct |
8 ms |
3692 KB |
Output is correct |
50 |
Correct |
7 ms |
3692 KB |
Output is correct |
51 |
Correct |
7 ms |
3692 KB |
Output is correct |
52 |
Correct |
10 ms |
3584 KB |
Output is correct |
53 |
Correct |
10 ms |
3564 KB |
Output is correct |
54 |
Correct |
10 ms |
3564 KB |
Output is correct |
55 |
Correct |
12 ms |
3564 KB |
Output is correct |
56 |
Correct |
11 ms |
3564 KB |
Output is correct |
57 |
Correct |
25 ms |
3436 KB |
Output is correct |
58 |
Correct |
25 ms |
3436 KB |
Output is correct |
59 |
Correct |
26 ms |
3436 KB |
Output is correct |
60 |
Correct |
25 ms |
3436 KB |
Output is correct |
61 |
Correct |
12 ms |
3564 KB |
Output is correct |
62 |
Correct |
13 ms |
3564 KB |
Output is correct |
63 |
Correct |
12 ms |
3564 KB |
Output is correct |
64 |
Correct |
15 ms |
3436 KB |
Output is correct |
65 |
Correct |
15 ms |
3436 KB |
Output is correct |
66 |
Correct |
20 ms |
3436 KB |
Output is correct |
67 |
Correct |
16 ms |
3436 KB |
Output is correct |
68 |
Correct |
7 ms |
3692 KB |
Output is correct |
69 |
Correct |
10 ms |
3564 KB |
Output is correct |
70 |
Correct |
10 ms |
3564 KB |
Output is correct |
71 |
Correct |
10 ms |
3564 KB |
Output is correct |
72 |
Correct |
25 ms |
3436 KB |
Output is correct |
73 |
Correct |
26 ms |
3436 KB |
Output is correct |
74 |
Correct |
12 ms |
3692 KB |
Output is correct |
75 |
Correct |
11 ms |
3436 KB |
Output is correct |
76 |
Correct |
11 ms |
3436 KB |
Output is correct |
77 |
Correct |
10 ms |
3436 KB |
Output is correct |
78 |
Correct |
10 ms |
3436 KB |
Output is correct |
79 |
Correct |
10 ms |
3436 KB |
Output is correct |
80 |
Correct |
10 ms |
3436 KB |
Output is correct |
81 |
Correct |
13 ms |
3436 KB |
Output is correct |
82 |
Correct |
14 ms |
3436 KB |
Output is correct |
83 |
Correct |
13 ms |
3436 KB |
Output is correct |
84 |
Correct |
13 ms |
3436 KB |
Output is correct |
85 |
Correct |
12 ms |
3436 KB |
Output is correct |
86 |
Correct |
12 ms |
3436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2796 KB |
Output is correct |
2 |
Correct |
2 ms |
2796 KB |
Output is correct |
3 |
Correct |
2 ms |
2796 KB |
Output is correct |
4 |
Correct |
3 ms |
2796 KB |
Output is correct |
5 |
Correct |
3 ms |
2816 KB |
Output is correct |
6 |
Correct |
3 ms |
2796 KB |
Output is correct |
7 |
Correct |
3 ms |
2796 KB |
Output is correct |
8 |
Correct |
3 ms |
2872 KB |
Output is correct |
9 |
Correct |
3 ms |
2796 KB |
Output is correct |
10 |
Correct |
3 ms |
2796 KB |
Output is correct |
11 |
Correct |
3 ms |
2796 KB |
Output is correct |
12 |
Correct |
3 ms |
2796 KB |
Output is correct |
13 |
Correct |
3 ms |
2796 KB |
Output is correct |
14 |
Correct |
3 ms |
2796 KB |
Output is correct |
15 |
Correct |
4 ms |
2796 KB |
Output is correct |
16 |
Correct |
4 ms |
2796 KB |
Output is correct |
17 |
Correct |
5 ms |
2796 KB |
Output is correct |
18 |
Correct |
4 ms |
2796 KB |
Output is correct |
19 |
Correct |
3 ms |
2944 KB |
Output is correct |
20 |
Correct |
3 ms |
2796 KB |
Output is correct |
21 |
Correct |
3 ms |
2796 KB |
Output is correct |
22 |
Correct |
3 ms |
2796 KB |
Output is correct |
23 |
Correct |
3 ms |
2796 KB |
Output is correct |
24 |
Correct |
3 ms |
2796 KB |
Output is correct |
25 |
Correct |
3 ms |
2796 KB |
Output is correct |
26 |
Correct |
3 ms |
2796 KB |
Output is correct |
27 |
Correct |
3 ms |
2924 KB |
Output is correct |
28 |
Correct |
3 ms |
2796 KB |
Output is correct |
29 |
Correct |
3 ms |
2796 KB |
Output is correct |
30 |
Correct |
4 ms |
2796 KB |
Output is correct |
31 |
Correct |
4 ms |
2796 KB |
Output is correct |
32 |
Correct |
3 ms |
2796 KB |
Output is correct |
33 |
Correct |
3 ms |
2796 KB |
Output is correct |
34 |
Correct |
3 ms |
2796 KB |
Output is correct |
35 |
Correct |
3 ms |
2796 KB |
Output is correct |
36 |
Correct |
3 ms |
2796 KB |
Output is correct |
37 |
Correct |
3 ms |
2796 KB |
Output is correct |
38 |
Correct |
3 ms |
2796 KB |
Output is correct |
39 |
Correct |
3 ms |
2796 KB |
Output is correct |
40 |
Correct |
3 ms |
2796 KB |
Output is correct |
41 |
Correct |
3 ms |
2796 KB |
Output is correct |
42 |
Correct |
3 ms |
2796 KB |
Output is correct |
43 |
Correct |
3 ms |
2796 KB |
Output is correct |
44 |
Correct |
4 ms |
2796 KB |
Output is correct |
45 |
Correct |
5 ms |
2924 KB |
Output is correct |
46 |
Correct |
19 ms |
3436 KB |
Output is correct |
47 |
Correct |
22 ms |
3436 KB |
Output is correct |
48 |
Correct |
16 ms |
3436 KB |
Output is correct |
49 |
Correct |
8 ms |
3692 KB |
Output is correct |
50 |
Correct |
7 ms |
3692 KB |
Output is correct |
51 |
Correct |
7 ms |
3692 KB |
Output is correct |
52 |
Correct |
10 ms |
3584 KB |
Output is correct |
53 |
Correct |
10 ms |
3564 KB |
Output is correct |
54 |
Correct |
10 ms |
3564 KB |
Output is correct |
55 |
Correct |
12 ms |
3564 KB |
Output is correct |
56 |
Correct |
11 ms |
3564 KB |
Output is correct |
57 |
Correct |
25 ms |
3436 KB |
Output is correct |
58 |
Correct |
25 ms |
3436 KB |
Output is correct |
59 |
Correct |
26 ms |
3436 KB |
Output is correct |
60 |
Correct |
25 ms |
3436 KB |
Output is correct |
61 |
Correct |
12 ms |
3564 KB |
Output is correct |
62 |
Correct |
13 ms |
3564 KB |
Output is correct |
63 |
Correct |
12 ms |
3564 KB |
Output is correct |
64 |
Correct |
15 ms |
3436 KB |
Output is correct |
65 |
Correct |
15 ms |
3436 KB |
Output is correct |
66 |
Correct |
20 ms |
3436 KB |
Output is correct |
67 |
Correct |
16 ms |
3436 KB |
Output is correct |
68 |
Correct |
7 ms |
3692 KB |
Output is correct |
69 |
Correct |
10 ms |
3564 KB |
Output is correct |
70 |
Correct |
10 ms |
3564 KB |
Output is correct |
71 |
Correct |
10 ms |
3564 KB |
Output is correct |
72 |
Correct |
25 ms |
3436 KB |
Output is correct |
73 |
Correct |
26 ms |
3436 KB |
Output is correct |
74 |
Correct |
12 ms |
3692 KB |
Output is correct |
75 |
Correct |
11 ms |
3436 KB |
Output is correct |
76 |
Correct |
11 ms |
3436 KB |
Output is correct |
77 |
Correct |
10 ms |
3436 KB |
Output is correct |
78 |
Correct |
10 ms |
3436 KB |
Output is correct |
79 |
Correct |
10 ms |
3436 KB |
Output is correct |
80 |
Correct |
10 ms |
3436 KB |
Output is correct |
81 |
Correct |
13 ms |
3436 KB |
Output is correct |
82 |
Correct |
14 ms |
3436 KB |
Output is correct |
83 |
Correct |
13 ms |
3436 KB |
Output is correct |
84 |
Correct |
13 ms |
3436 KB |
Output is correct |
85 |
Correct |
12 ms |
3436 KB |
Output is correct |
86 |
Correct |
12 ms |
3436 KB |
Output is correct |
87 |
Correct |
44 ms |
4552 KB |
Output is correct |
88 |
Correct |
148 ms |
8180 KB |
Output is correct |
89 |
Correct |
756 ms |
20444 KB |
Output is correct |
90 |
Correct |
683 ms |
20700 KB |
Output is correct |
91 |
Correct |
700 ms |
20640 KB |
Output is correct |
92 |
Correct |
211 ms |
28252 KB |
Output is correct |
93 |
Correct |
194 ms |
28124 KB |
Output is correct |
94 |
Correct |
201 ms |
28252 KB |
Output is correct |
95 |
Correct |
314 ms |
24028 KB |
Output is correct |
96 |
Correct |
330 ms |
24540 KB |
Output is correct |
97 |
Correct |
331 ms |
24412 KB |
Output is correct |
98 |
Correct |
343 ms |
24540 KB |
Output is correct |
99 |
Correct |
336 ms |
23712 KB |
Output is correct |
100 |
Correct |
1184 ms |
21024 KB |
Output is correct |
101 |
Correct |
1255 ms |
20980 KB |
Output is correct |
102 |
Correct |
1241 ms |
20996 KB |
Output is correct |
103 |
Correct |
1245 ms |
21212 KB |
Output is correct |
104 |
Correct |
737 ms |
23772 KB |
Output is correct |
105 |
Correct |
707 ms |
23796 KB |
Output is correct |
106 |
Correct |
708 ms |
23840 KB |
Output is correct |
107 |
Correct |
665 ms |
19804 KB |
Output is correct |
108 |
Correct |
667 ms |
20072 KB |
Output is correct |
109 |
Correct |
717 ms |
20188 KB |
Output is correct |
110 |
Correct |
184 ms |
27612 KB |
Output is correct |
111 |
Correct |
315 ms |
24156 KB |
Output is correct |
112 |
Correct |
311 ms |
23900 KB |
Output is correct |
113 |
Correct |
328 ms |
22996 KB |
Output is correct |
114 |
Correct |
1209 ms |
20956 KB |
Output is correct |
115 |
Correct |
1283 ms |
20700 KB |
Output is correct |
116 |
Correct |
691 ms |
23004 KB |
Output is correct |
117 |
Correct |
341 ms |
21852 KB |
Output is correct |
118 |
Correct |
367 ms |
21348 KB |
Output is correct |
119 |
Correct |
343 ms |
21084 KB |
Output is correct |
120 |
Correct |
333 ms |
21380 KB |
Output is correct |
121 |
Correct |
331 ms |
20956 KB |
Output is correct |
122 |
Correct |
324 ms |
20700 KB |
Output is correct |
123 |
Correct |
786 ms |
21888 KB |
Output is correct |
124 |
Correct |
713 ms |
21508 KB |
Output is correct |
125 |
Correct |
684 ms |
20956 KB |
Output is correct |
126 |
Correct |
758 ms |
21532 KB |
Output is correct |
127 |
Correct |
750 ms |
20956 KB |
Output is correct |
128 |
Correct |
715 ms |
20828 KB |
Output is correct |