#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define sd(x) scanf("%d", &(x))
#define pii pair<int, int>
#define F first
#define S second
#define all(c) ((c).begin()), ((c).end())
#define sz(x) ((int)(x).size())
#define ld long double
template<class T,class U>
ostream& operator<<(ostream& os,const pair<T,U>& p){
os<<"("<<p.first<<", "<<p.second<<")";
return os;
}
template<class T>
ostream& operator <<(ostream& os,const vector<T>& v){
os<<"{";
for(int i = 0;i < (int)v.size(); i++){
if(i)os<<", ";
os<<v[i];
}
os<<"}";
return os;
}
#ifdef LOCAL
#define cerr cout
#else
#endif
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif
const int N = 100005;
const int logN = 20;
int arrival[N], par[logN][N], depth[N], A[N], B[N], C[N];
int st[N], en[N], timer;
vector<int> children[N];
void dfs(int s, int d){
depth[s] = d;
st[s] = timer++;
for(int v : children[s]) dfs(v, d + 1);
en[s] = timer - 1;
}
// 0-indexed
template<class T>
struct segtree{
int n;
vector<T> t;
T def;
inline T combine(T a, T b){
return arrival[a] > arrival[b] ? a : b;
}
segtree(vector<T> & inp, T def = T()) : n(sz(inp)), def(def){
t.resize(2 * n, def);
for(int i = 0; i < n; i++) t[n + i] = inp[i];
for(int i = n - 1; i > 0; --i) t[i] = combine(t[i<<1], t[i<<1|1]);
}
void modify(int p, T value) { // modify a[p] = value
// value = combine(value, t[p + n]); // if a[p] = combine(a[p], value)
for (t[p += n] = value; p >>= 1; ) t[p] = combine(t[p<<1], t[p<<1|1]);
}
T query(int l, int r) { // compute on interval [l, r]
r++;
T resl = def, resr = def;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l&1) resl = combine(resl, t[l++]);
if (r&1) resr = combine(t[--r], resr);
}
return combine(resl, resr);
}
};
ll bit[N];
void add(int x, int y){
x++;
for(; x < N; x += x & -x) bit[x] += y;
}
ll get(int x){
x++;
ll ret = 0;
for(; x; x -= x & -x) ret += bit[x];
return ret;
}
ll get(int l, int r){
return get(r) - get(l - 1);
}
int getKth(int x, int k){
for(int i = 0; i < logN; i++) if(k >> i & 1) x = par[i][x];
return x;
}
int main(){
int n; sd(n);
vector<int> V(n);
segtree<int> stree(V);
map<int, int> compress; set<int> vals;
for(int i = 0; i < n; i++){
sd(C[i]);
vals.insert(C[i]);
}
int pos = 0;
for(int v : vals) compress[v] = pos++;
for(int i = 0; i < n; i++) C[i] = compress[C[i]];
for(int i = 1; i < n; i++){
sd(A[i]); sd(B[i]);
A[i]--; B[i]--;
arrival[B[i]] = i;
par[0][B[i]] = A[i];
children[A[i]].push_back(B[i]);
}
dfs(0, 0);
for(int j = 1; j < logN; j++) for(int i = 0; i < n; i++) par[j][i] = par[j - 1][par[j - 1][i]];
for(int i = 1; i < n; i++){
int x = B[i];
int curr = 1;
vector<pii> vec;
ll num = 0;
while(curr <= depth[x]){
int y = getKth(x, curr);
int latestNode = stree.query(st[y], en[y]); // last added in x's subtree
// maximum with the same value
int lo = curr, hi = depth[x];
while(lo < hi){
int mid = (lo + hi + 1) >> 1;
int node = getKth(x, mid);
if(stree.query(st[node], en[node]) == latestNode) lo = mid;
else hi = mid - 1;
}
int v = C[latestNode], cnt = lo - curr + 1;
vec.push_back({v, cnt}); // v comes cnt times
num += cnt * (ll) get(0, v - 1); // the values below should be smaller than this
add(v, cnt);
curr = lo + 1;
}
for(auto it : vec) add(it.F, -it.S); // reinitialize bit to 0
printf("%lld\n", num);
stree.modify(st[x], x);
}
}
Compilation message
construction.cpp: In function 'int main()':
construction.cpp:5:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
#define sd(x) scanf("%d", &(x))
~~~~~^~~~~~~~~~~~
construction.cpp:119:9: note: in expansion of macro 'sd'
int n; sd(n);
^~
construction.cpp:5:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
#define sd(x) scanf("%d", &(x))
~~~~~^~~~~~~~~~~~
construction.cpp:125:3: note: in expansion of macro 'sd'
sd(C[i]);
^~
construction.cpp:5:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
#define sd(x) scanf("%d", &(x))
~~~~~^~~~~~~~~~~~
construction.cpp:135:3: note: in expansion of macro 'sd'
sd(A[i]); sd(B[i]);
^~
construction.cpp:5:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
#define sd(x) scanf("%d", &(x))
~~~~~^~~~~~~~~~~~
construction.cpp:135:13: note: in expansion of macro 'sd'
sd(A[i]); sd(B[i]);
^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2816 KB |
Output is correct |
2 |
Correct |
7 ms |
2816 KB |
Output is correct |
3 |
Correct |
7 ms |
2816 KB |
Output is correct |
4 |
Correct |
7 ms |
2944 KB |
Output is correct |
5 |
Correct |
8 ms |
2944 KB |
Output is correct |
6 |
Correct |
8 ms |
2904 KB |
Output is correct |
7 |
Correct |
8 ms |
2944 KB |
Output is correct |
8 |
Correct |
7 ms |
2944 KB |
Output is correct |
9 |
Correct |
8 ms |
2944 KB |
Output is correct |
10 |
Correct |
9 ms |
2944 KB |
Output is correct |
11 |
Correct |
8 ms |
2944 KB |
Output is correct |
12 |
Correct |
7 ms |
2944 KB |
Output is correct |
13 |
Correct |
8 ms |
2944 KB |
Output is correct |
14 |
Correct |
9 ms |
2944 KB |
Output is correct |
15 |
Correct |
8 ms |
2944 KB |
Output is correct |
16 |
Correct |
9 ms |
2944 KB |
Output is correct |
17 |
Correct |
8 ms |
2944 KB |
Output is correct |
18 |
Correct |
8 ms |
2944 KB |
Output is correct |
19 |
Correct |
7 ms |
2944 KB |
Output is correct |
20 |
Correct |
8 ms |
2944 KB |
Output is correct |
21 |
Correct |
7 ms |
2944 KB |
Output is correct |
22 |
Correct |
9 ms |
2900 KB |
Output is correct |
23 |
Correct |
7 ms |
2944 KB |
Output is correct |
24 |
Correct |
7 ms |
2944 KB |
Output is correct |
25 |
Correct |
7 ms |
2944 KB |
Output is correct |
26 |
Correct |
8 ms |
2944 KB |
Output is correct |
27 |
Correct |
12 ms |
2944 KB |
Output is correct |
28 |
Correct |
7 ms |
2944 KB |
Output is correct |
29 |
Correct |
8 ms |
2944 KB |
Output is correct |
30 |
Correct |
8 ms |
2944 KB |
Output is correct |
31 |
Correct |
8 ms |
2944 KB |
Output is correct |
32 |
Correct |
7 ms |
2944 KB |
Output is correct |
33 |
Correct |
7 ms |
2944 KB |
Output is correct |
34 |
Correct |
7 ms |
2944 KB |
Output is correct |
35 |
Correct |
7 ms |
2944 KB |
Output is correct |
36 |
Correct |
7 ms |
2936 KB |
Output is correct |
37 |
Correct |
7 ms |
2944 KB |
Output is correct |
38 |
Correct |
7 ms |
2944 KB |
Output is correct |
39 |
Correct |
7 ms |
2944 KB |
Output is correct |
40 |
Correct |
7 ms |
2944 KB |
Output is correct |
41 |
Correct |
7 ms |
2944 KB |
Output is correct |
42 |
Correct |
7 ms |
2944 KB |
Output is correct |
43 |
Correct |
7 ms |
2944 KB |
Output is correct |
44 |
Correct |
7 ms |
2944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2816 KB |
Output is correct |
2 |
Correct |
7 ms |
2816 KB |
Output is correct |
3 |
Correct |
7 ms |
2816 KB |
Output is correct |
4 |
Correct |
7 ms |
2944 KB |
Output is correct |
5 |
Correct |
8 ms |
2944 KB |
Output is correct |
6 |
Correct |
8 ms |
2904 KB |
Output is correct |
7 |
Correct |
8 ms |
2944 KB |
Output is correct |
8 |
Correct |
7 ms |
2944 KB |
Output is correct |
9 |
Correct |
8 ms |
2944 KB |
Output is correct |
10 |
Correct |
9 ms |
2944 KB |
Output is correct |
11 |
Correct |
8 ms |
2944 KB |
Output is correct |
12 |
Correct |
7 ms |
2944 KB |
Output is correct |
13 |
Correct |
8 ms |
2944 KB |
Output is correct |
14 |
Correct |
9 ms |
2944 KB |
Output is correct |
15 |
Correct |
8 ms |
2944 KB |
Output is correct |
16 |
Correct |
9 ms |
2944 KB |
Output is correct |
17 |
Correct |
8 ms |
2944 KB |
Output is correct |
18 |
Correct |
8 ms |
2944 KB |
Output is correct |
19 |
Correct |
7 ms |
2944 KB |
Output is correct |
20 |
Correct |
8 ms |
2944 KB |
Output is correct |
21 |
Correct |
7 ms |
2944 KB |
Output is correct |
22 |
Correct |
9 ms |
2900 KB |
Output is correct |
23 |
Correct |
7 ms |
2944 KB |
Output is correct |
24 |
Correct |
7 ms |
2944 KB |
Output is correct |
25 |
Correct |
7 ms |
2944 KB |
Output is correct |
26 |
Correct |
8 ms |
2944 KB |
Output is correct |
27 |
Correct |
12 ms |
2944 KB |
Output is correct |
28 |
Correct |
7 ms |
2944 KB |
Output is correct |
29 |
Correct |
8 ms |
2944 KB |
Output is correct |
30 |
Correct |
8 ms |
2944 KB |
Output is correct |
31 |
Correct |
8 ms |
2944 KB |
Output is correct |
32 |
Correct |
7 ms |
2944 KB |
Output is correct |
33 |
Correct |
7 ms |
2944 KB |
Output is correct |
34 |
Correct |
7 ms |
2944 KB |
Output is correct |
35 |
Correct |
7 ms |
2944 KB |
Output is correct |
36 |
Correct |
7 ms |
2936 KB |
Output is correct |
37 |
Correct |
7 ms |
2944 KB |
Output is correct |
38 |
Correct |
7 ms |
2944 KB |
Output is correct |
39 |
Correct |
7 ms |
2944 KB |
Output is correct |
40 |
Correct |
7 ms |
2944 KB |
Output is correct |
41 |
Correct |
7 ms |
2944 KB |
Output is correct |
42 |
Correct |
7 ms |
2944 KB |
Output is correct |
43 |
Correct |
7 ms |
2944 KB |
Output is correct |
44 |
Correct |
7 ms |
2944 KB |
Output is correct |
45 |
Correct |
9 ms |
3072 KB |
Output is correct |
46 |
Correct |
19 ms |
3840 KB |
Output is correct |
47 |
Correct |
20 ms |
3892 KB |
Output is correct |
48 |
Correct |
24 ms |
3840 KB |
Output is correct |
49 |
Correct |
17 ms |
4096 KB |
Output is correct |
50 |
Correct |
23 ms |
4096 KB |
Output is correct |
51 |
Correct |
17 ms |
4096 KB |
Output is correct |
52 |
Correct |
20 ms |
3968 KB |
Output is correct |
53 |
Correct |
19 ms |
3968 KB |
Output is correct |
54 |
Correct |
20 ms |
3968 KB |
Output is correct |
55 |
Correct |
20 ms |
3968 KB |
Output is correct |
56 |
Correct |
19 ms |
3968 KB |
Output is correct |
57 |
Correct |
26 ms |
3840 KB |
Output is correct |
58 |
Correct |
27 ms |
3840 KB |
Output is correct |
59 |
Correct |
27 ms |
3840 KB |
Output is correct |
60 |
Correct |
27 ms |
3840 KB |
Output is correct |
61 |
Correct |
23 ms |
4096 KB |
Output is correct |
62 |
Correct |
22 ms |
4016 KB |
Output is correct |
63 |
Correct |
22 ms |
3968 KB |
Output is correct |
64 |
Correct |
17 ms |
3456 KB |
Output is correct |
65 |
Correct |
17 ms |
3456 KB |
Output is correct |
66 |
Correct |
18 ms |
3456 KB |
Output is correct |
67 |
Correct |
17 ms |
3584 KB |
Output is correct |
68 |
Correct |
15 ms |
3712 KB |
Output is correct |
69 |
Correct |
19 ms |
3968 KB |
Output is correct |
70 |
Correct |
19 ms |
3584 KB |
Output is correct |
71 |
Correct |
17 ms |
3456 KB |
Output is correct |
72 |
Correct |
26 ms |
3840 KB |
Output is correct |
73 |
Correct |
25 ms |
3584 KB |
Output is correct |
74 |
Correct |
19 ms |
3456 KB |
Output is correct |
75 |
Correct |
18 ms |
3840 KB |
Output is correct |
76 |
Correct |
18 ms |
3840 KB |
Output is correct |
77 |
Correct |
18 ms |
3840 KB |
Output is correct |
78 |
Correct |
16 ms |
3456 KB |
Output is correct |
79 |
Correct |
15 ms |
3456 KB |
Output is correct |
80 |
Correct |
15 ms |
3456 KB |
Output is correct |
81 |
Correct |
21 ms |
3840 KB |
Output is correct |
82 |
Correct |
20 ms |
3840 KB |
Output is correct |
83 |
Correct |
20 ms |
3816 KB |
Output is correct |
84 |
Correct |
19 ms |
3456 KB |
Output is correct |
85 |
Correct |
18 ms |
3456 KB |
Output is correct |
86 |
Correct |
18 ms |
3712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2816 KB |
Output is correct |
2 |
Correct |
7 ms |
2816 KB |
Output is correct |
3 |
Correct |
7 ms |
2816 KB |
Output is correct |
4 |
Correct |
7 ms |
2944 KB |
Output is correct |
5 |
Correct |
8 ms |
2944 KB |
Output is correct |
6 |
Correct |
8 ms |
2904 KB |
Output is correct |
7 |
Correct |
8 ms |
2944 KB |
Output is correct |
8 |
Correct |
7 ms |
2944 KB |
Output is correct |
9 |
Correct |
8 ms |
2944 KB |
Output is correct |
10 |
Correct |
9 ms |
2944 KB |
Output is correct |
11 |
Correct |
8 ms |
2944 KB |
Output is correct |
12 |
Correct |
7 ms |
2944 KB |
Output is correct |
13 |
Correct |
8 ms |
2944 KB |
Output is correct |
14 |
Correct |
9 ms |
2944 KB |
Output is correct |
15 |
Correct |
8 ms |
2944 KB |
Output is correct |
16 |
Correct |
9 ms |
2944 KB |
Output is correct |
17 |
Correct |
8 ms |
2944 KB |
Output is correct |
18 |
Correct |
8 ms |
2944 KB |
Output is correct |
19 |
Correct |
7 ms |
2944 KB |
Output is correct |
20 |
Correct |
8 ms |
2944 KB |
Output is correct |
21 |
Correct |
7 ms |
2944 KB |
Output is correct |
22 |
Correct |
9 ms |
2900 KB |
Output is correct |
23 |
Correct |
7 ms |
2944 KB |
Output is correct |
24 |
Correct |
7 ms |
2944 KB |
Output is correct |
25 |
Correct |
7 ms |
2944 KB |
Output is correct |
26 |
Correct |
8 ms |
2944 KB |
Output is correct |
27 |
Correct |
12 ms |
2944 KB |
Output is correct |
28 |
Correct |
7 ms |
2944 KB |
Output is correct |
29 |
Correct |
8 ms |
2944 KB |
Output is correct |
30 |
Correct |
8 ms |
2944 KB |
Output is correct |
31 |
Correct |
8 ms |
2944 KB |
Output is correct |
32 |
Correct |
7 ms |
2944 KB |
Output is correct |
33 |
Correct |
7 ms |
2944 KB |
Output is correct |
34 |
Correct |
7 ms |
2944 KB |
Output is correct |
35 |
Correct |
7 ms |
2944 KB |
Output is correct |
36 |
Correct |
7 ms |
2936 KB |
Output is correct |
37 |
Correct |
7 ms |
2944 KB |
Output is correct |
38 |
Correct |
7 ms |
2944 KB |
Output is correct |
39 |
Correct |
7 ms |
2944 KB |
Output is correct |
40 |
Correct |
7 ms |
2944 KB |
Output is correct |
41 |
Correct |
7 ms |
2944 KB |
Output is correct |
42 |
Correct |
7 ms |
2944 KB |
Output is correct |
43 |
Correct |
7 ms |
2944 KB |
Output is correct |
44 |
Correct |
7 ms |
2944 KB |
Output is correct |
45 |
Correct |
9 ms |
3072 KB |
Output is correct |
46 |
Correct |
19 ms |
3840 KB |
Output is correct |
47 |
Correct |
20 ms |
3892 KB |
Output is correct |
48 |
Correct |
24 ms |
3840 KB |
Output is correct |
49 |
Correct |
17 ms |
4096 KB |
Output is correct |
50 |
Correct |
23 ms |
4096 KB |
Output is correct |
51 |
Correct |
17 ms |
4096 KB |
Output is correct |
52 |
Correct |
20 ms |
3968 KB |
Output is correct |
53 |
Correct |
19 ms |
3968 KB |
Output is correct |
54 |
Correct |
20 ms |
3968 KB |
Output is correct |
55 |
Correct |
20 ms |
3968 KB |
Output is correct |
56 |
Correct |
19 ms |
3968 KB |
Output is correct |
57 |
Correct |
26 ms |
3840 KB |
Output is correct |
58 |
Correct |
27 ms |
3840 KB |
Output is correct |
59 |
Correct |
27 ms |
3840 KB |
Output is correct |
60 |
Correct |
27 ms |
3840 KB |
Output is correct |
61 |
Correct |
23 ms |
4096 KB |
Output is correct |
62 |
Correct |
22 ms |
4016 KB |
Output is correct |
63 |
Correct |
22 ms |
3968 KB |
Output is correct |
64 |
Correct |
17 ms |
3456 KB |
Output is correct |
65 |
Correct |
17 ms |
3456 KB |
Output is correct |
66 |
Correct |
18 ms |
3456 KB |
Output is correct |
67 |
Correct |
17 ms |
3584 KB |
Output is correct |
68 |
Correct |
15 ms |
3712 KB |
Output is correct |
69 |
Correct |
19 ms |
3968 KB |
Output is correct |
70 |
Correct |
19 ms |
3584 KB |
Output is correct |
71 |
Correct |
17 ms |
3456 KB |
Output is correct |
72 |
Correct |
26 ms |
3840 KB |
Output is correct |
73 |
Correct |
25 ms |
3584 KB |
Output is correct |
74 |
Correct |
19 ms |
3456 KB |
Output is correct |
75 |
Correct |
18 ms |
3840 KB |
Output is correct |
76 |
Correct |
18 ms |
3840 KB |
Output is correct |
77 |
Correct |
18 ms |
3840 KB |
Output is correct |
78 |
Correct |
16 ms |
3456 KB |
Output is correct |
79 |
Correct |
15 ms |
3456 KB |
Output is correct |
80 |
Correct |
15 ms |
3456 KB |
Output is correct |
81 |
Correct |
21 ms |
3840 KB |
Output is correct |
82 |
Correct |
20 ms |
3840 KB |
Output is correct |
83 |
Correct |
20 ms |
3816 KB |
Output is correct |
84 |
Correct |
19 ms |
3456 KB |
Output is correct |
85 |
Correct |
18 ms |
3456 KB |
Output is correct |
86 |
Correct |
18 ms |
3712 KB |
Output is correct |
87 |
Correct |
43 ms |
5376 KB |
Output is correct |
88 |
Correct |
131 ms |
10576 KB |
Output is correct |
89 |
Correct |
616 ms |
28792 KB |
Output is correct |
90 |
Correct |
615 ms |
28864 KB |
Output is correct |
91 |
Correct |
617 ms |
28664 KB |
Output is correct |
92 |
Correct |
546 ms |
34936 KB |
Output is correct |
93 |
Correct |
536 ms |
34808 KB |
Output is correct |
94 |
Correct |
532 ms |
34808 KB |
Output is correct |
95 |
Correct |
714 ms |
31164 KB |
Output is correct |
96 |
Correct |
692 ms |
31608 KB |
Output is correct |
97 |
Correct |
687 ms |
31648 KB |
Output is correct |
98 |
Correct |
704 ms |
31612 KB |
Output is correct |
99 |
Correct |
685 ms |
31096 KB |
Output is correct |
100 |
Correct |
1047 ms |
28956 KB |
Output is correct |
101 |
Correct |
1062 ms |
29176 KB |
Output is correct |
102 |
Correct |
1087 ms |
29288 KB |
Output is correct |
103 |
Correct |
1062 ms |
29304 KB |
Output is correct |
104 |
Correct |
1152 ms |
31324 KB |
Output is correct |
105 |
Correct |
1143 ms |
30968 KB |
Output is correct |
106 |
Correct |
1163 ms |
31096 KB |
Output is correct |
107 |
Correct |
482 ms |
17656 KB |
Output is correct |
108 |
Correct |
488 ms |
17912 KB |
Output is correct |
109 |
Correct |
598 ms |
21112 KB |
Output is correct |
110 |
Correct |
446 ms |
24060 KB |
Output is correct |
111 |
Correct |
660 ms |
31224 KB |
Output is correct |
112 |
Correct |
578 ms |
20852 KB |
Output is correct |
113 |
Correct |
586 ms |
20344 KB |
Output is correct |
114 |
Correct |
1094 ms |
28792 KB |
Output is correct |
115 |
Correct |
961 ms |
18560 KB |
Output is correct |
116 |
Correct |
1004 ms |
20344 KB |
Output is correct |
117 |
Correct |
654 ms |
29048 KB |
Output is correct |
118 |
Correct |
612 ms |
28792 KB |
Output is correct |
119 |
Correct |
590 ms |
28536 KB |
Output is correct |
120 |
Correct |
522 ms |
19064 KB |
Output is correct |
121 |
Correct |
513 ms |
18936 KB |
Output is correct |
122 |
Correct |
495 ms |
18552 KB |
Output is correct |
123 |
Correct |
1073 ms |
29320 KB |
Output is correct |
124 |
Correct |
980 ms |
28792 KB |
Output is correct |
125 |
Correct |
927 ms |
28664 KB |
Output is correct |
126 |
Correct |
955 ms |
19064 KB |
Output is correct |
127 |
Correct |
858 ms |
19020 KB |
Output is correct |
128 |
Correct |
837 ms |
18552 KB |
Output is correct |