#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 1 << 17;
#define debug(...) fprintf(stderr, __VA_ARGS__)
//#define debug(...)
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define fillchar(a, s) memset((a), (s), sizeof(a))
int N;
int A[MAXN], B[MAXN];
int C[MAXN];
vector<int> adj[MAXN];
int ent[MAXN], exi[MAXN];
struct segtree {
pii tree[2 * MAXN]; //pii(time, value)
void update (int x, pii v) {
tree[x += MAXN] = v;
while (x >>= 1) {
tree[x] = max(tree[2 * x], tree[2 * x + 1]);
}
}
pii query (int a, int b, int cur = 1, int lt = 0, int rt = MAXN) {
if (rt <= a || b <= lt) {
return pii(-1, -1);
}
if (a <= lt && rt <= b) {
return tree[cur];
}
int mid = (lt + rt) / 2;
return max(query(a, b, 2 * cur, lt, mid), query(a, b, 2 * cur + 1, mid, rt));
}
} seg;
void updpath (int x) {
static int updtime = 0;
seg.update(ent[x], pii(++updtime, C[x]));
}
int lastupd (int x) {
//last update of this one
return seg.query(ent[x], exi[x]).se;
}
void dfs (int x) {
static int cur = 0;
ent[x] = cur++;
for (int y : adj[x]) {
dfs(y);
}
exi[x] = cur;
updpath(x);
assert(lastupd(x) == C[x]);
}
struct node {
node *ch[2], *p;
int id;
int size;
} *null = new node, nodes[MAXN];
int getdir (node *n) {
if (n->p == null) {
return -1;
}
if (n == n->p->ch[0]) {
return 0;
}
if (n == n->p->ch[1]) {
return 1;
}
return -1;
}
bool isroot (node *n) {
return getdir(n) == -1;
}
void setc (node *n, node *c, int d) {
n->ch[d] = c;
if (c != null) {
c->p = n;
}
}
void pull (node *n) {
n->size = n->ch[0]->size + 1 + n->ch[1]->size;
}
void rot (node *n) {
node *p = n->p, *g = p->p;
bool d = getdir(n);
//note that this matters in an LCT. cannot cal setc on g and n
if (!isroot(p)) {
g->ch[getdir(p)] = n;
}
setc(p, n->ch[!d], d);
n->p = g; //this line forgot include...
setc(n, p, !d);
pull(p);
pull(n);
}
void splay (node *n) {
while (!isroot(n)) {
node *p = n->p;
if (!isroot(p)) {
rot(getdir(n) == getdir(p) ? p : n);
}
rot(n);
}
}
vector<pii> expose (node *u) {
//pii(size, value of node)
vector<pii> res;
int psize = 0;
for (node *cur = u, *v = null; cur != null; v = cur, cur = cur->p) {
splay(cur);
assert(v == null || v->p == cur);
cur->ch[1] = v;
pull(cur);
if (v != null) {
res.push_back(pii(lastupd(cur->id), cur->size - psize));
}
psize = cur->size;
}
splay(u);
return res;
}
struct fenwick {
ll bit[MAXN];
void update (int x, ll v) {
for (x++; x < MAXN; x += (x & -x)) {
bit[x] += v;
}
}
ll query (int x) {
ll res = 0;
for (x++; x; x &= (x - 1)) {
res += bit[x];
}
return res;
}
} fen;
int main() {
scanf("%d", &N);
for (int i = 0; i < N; i++) {
scanf("%d", &C[i]);
}
//compress C
vector<int> vals(C, C + N);
sort(all(vals));
vals.resize(unique(all(vals)) - vals.begin());
for (int i = 0; i < N; i++) {
C[i] = lower_bound(all(vals), C[i]) - vals.begin();
}
for (int i = 0; i < N - 1; i++) {
scanf("%d %d", &A[i], &B[i]);
A[i]--;
B[i]--;
adj[A[i]].push_back(B[i]);
}
dfs(0);
//NOW THE NODES!
null->ch[0] = null->ch[1] = null->p = null;
null->id = -1073741819;
null->size = 0;
for (int i = 0; i < N; i++) {
nodes[i].ch[0] = nodes[i].ch[1] = nodes[i].p = null;
nodes[i].id = i;
nodes[i].size = 1;
}
for (int i = 0; i < N - 1; i++) {
nodes[B[i]].p = nodes + A[i];
}
int sumsz = 0;
for (int i = 0; i < N - 1; i++) {
int b = B[i];
vector<pii> v = expose(nodes + b);
//we want the number of strictly less than this value.
ll ans = 0;
sumsz += v.size();
for (pii p : v) {
ans += p.se * fen.query(p.fi - 1);
fen.update(p.fi, p.se);
}
for (pii p : v) {
fen.update(p.fi, -p.se);
}
printf("%lld\n", ans);
updpath(b);
}
}
Compilation message
construction.cpp: In function 'int main()':
construction.cpp:165:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &N);
~~~~~^~~~~~~~~~
construction.cpp:167:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &C[i]);
~~~~~^~~~~~~~~~~~~
construction.cpp:178:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &A[i], &B[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
3576 KB |
Output is correct |
2 |
Correct |
7 ms |
3680 KB |
Output is correct |
3 |
Correct |
7 ms |
3700 KB |
Output is correct |
4 |
Correct |
8 ms |
3772 KB |
Output is correct |
5 |
Correct |
9 ms |
3772 KB |
Output is correct |
6 |
Correct |
9 ms |
3772 KB |
Output is correct |
7 |
Correct |
9 ms |
3772 KB |
Output is correct |
8 |
Correct |
8 ms |
3772 KB |
Output is correct |
9 |
Correct |
8 ms |
3772 KB |
Output is correct |
10 |
Correct |
8 ms |
3796 KB |
Output is correct |
11 |
Correct |
7 ms |
3800 KB |
Output is correct |
12 |
Correct |
8 ms |
3800 KB |
Output is correct |
13 |
Correct |
8 ms |
3924 KB |
Output is correct |
14 |
Correct |
8 ms |
3924 KB |
Output is correct |
15 |
Correct |
9 ms |
3924 KB |
Output is correct |
16 |
Correct |
9 ms |
3924 KB |
Output is correct |
17 |
Correct |
8 ms |
3924 KB |
Output is correct |
18 |
Correct |
9 ms |
3924 KB |
Output is correct |
19 |
Correct |
8 ms |
3924 KB |
Output is correct |
20 |
Correct |
8 ms |
3924 KB |
Output is correct |
21 |
Correct |
8 ms |
3924 KB |
Output is correct |
22 |
Correct |
9 ms |
3924 KB |
Output is correct |
23 |
Correct |
8 ms |
3924 KB |
Output is correct |
24 |
Correct |
9 ms |
3924 KB |
Output is correct |
25 |
Correct |
7 ms |
3924 KB |
Output is correct |
26 |
Correct |
8 ms |
3924 KB |
Output is correct |
27 |
Correct |
8 ms |
3924 KB |
Output is correct |
28 |
Correct |
7 ms |
3924 KB |
Output is correct |
29 |
Correct |
8 ms |
3924 KB |
Output is correct |
30 |
Correct |
9 ms |
3924 KB |
Output is correct |
31 |
Correct |
8 ms |
3924 KB |
Output is correct |
32 |
Correct |
8 ms |
3924 KB |
Output is correct |
33 |
Correct |
9 ms |
3924 KB |
Output is correct |
34 |
Correct |
7 ms |
3924 KB |
Output is correct |
35 |
Correct |
8 ms |
3924 KB |
Output is correct |
36 |
Correct |
8 ms |
3924 KB |
Output is correct |
37 |
Correct |
8 ms |
3924 KB |
Output is correct |
38 |
Correct |
7 ms |
3924 KB |
Output is correct |
39 |
Correct |
9 ms |
3924 KB |
Output is correct |
40 |
Correct |
10 ms |
3948 KB |
Output is correct |
41 |
Correct |
8 ms |
3948 KB |
Output is correct |
42 |
Correct |
8 ms |
3948 KB |
Output is correct |
43 |
Correct |
7 ms |
3948 KB |
Output is correct |
44 |
Correct |
9 ms |
3948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
3576 KB |
Output is correct |
2 |
Correct |
7 ms |
3680 KB |
Output is correct |
3 |
Correct |
7 ms |
3700 KB |
Output is correct |
4 |
Correct |
8 ms |
3772 KB |
Output is correct |
5 |
Correct |
9 ms |
3772 KB |
Output is correct |
6 |
Correct |
9 ms |
3772 KB |
Output is correct |
7 |
Correct |
9 ms |
3772 KB |
Output is correct |
8 |
Correct |
8 ms |
3772 KB |
Output is correct |
9 |
Correct |
8 ms |
3772 KB |
Output is correct |
10 |
Correct |
8 ms |
3796 KB |
Output is correct |
11 |
Correct |
7 ms |
3800 KB |
Output is correct |
12 |
Correct |
8 ms |
3800 KB |
Output is correct |
13 |
Correct |
8 ms |
3924 KB |
Output is correct |
14 |
Correct |
8 ms |
3924 KB |
Output is correct |
15 |
Correct |
9 ms |
3924 KB |
Output is correct |
16 |
Correct |
9 ms |
3924 KB |
Output is correct |
17 |
Correct |
8 ms |
3924 KB |
Output is correct |
18 |
Correct |
9 ms |
3924 KB |
Output is correct |
19 |
Correct |
8 ms |
3924 KB |
Output is correct |
20 |
Correct |
8 ms |
3924 KB |
Output is correct |
21 |
Correct |
8 ms |
3924 KB |
Output is correct |
22 |
Correct |
9 ms |
3924 KB |
Output is correct |
23 |
Correct |
8 ms |
3924 KB |
Output is correct |
24 |
Correct |
9 ms |
3924 KB |
Output is correct |
25 |
Correct |
7 ms |
3924 KB |
Output is correct |
26 |
Correct |
8 ms |
3924 KB |
Output is correct |
27 |
Correct |
8 ms |
3924 KB |
Output is correct |
28 |
Correct |
7 ms |
3924 KB |
Output is correct |
29 |
Correct |
8 ms |
3924 KB |
Output is correct |
30 |
Correct |
9 ms |
3924 KB |
Output is correct |
31 |
Correct |
8 ms |
3924 KB |
Output is correct |
32 |
Correct |
8 ms |
3924 KB |
Output is correct |
33 |
Correct |
9 ms |
3924 KB |
Output is correct |
34 |
Correct |
7 ms |
3924 KB |
Output is correct |
35 |
Correct |
8 ms |
3924 KB |
Output is correct |
36 |
Correct |
8 ms |
3924 KB |
Output is correct |
37 |
Correct |
8 ms |
3924 KB |
Output is correct |
38 |
Correct |
7 ms |
3924 KB |
Output is correct |
39 |
Correct |
9 ms |
3924 KB |
Output is correct |
40 |
Correct |
10 ms |
3948 KB |
Output is correct |
41 |
Correct |
8 ms |
3948 KB |
Output is correct |
42 |
Correct |
8 ms |
3948 KB |
Output is correct |
43 |
Correct |
7 ms |
3948 KB |
Output is correct |
44 |
Correct |
9 ms |
3948 KB |
Output is correct |
45 |
Correct |
11 ms |
3948 KB |
Output is correct |
46 |
Correct |
24 ms |
4076 KB |
Output is correct |
47 |
Correct |
22 ms |
4076 KB |
Output is correct |
48 |
Correct |
24 ms |
4188 KB |
Output is correct |
49 |
Correct |
18 ms |
4364 KB |
Output is correct |
50 |
Correct |
18 ms |
4364 KB |
Output is correct |
51 |
Correct |
16 ms |
4380 KB |
Output is correct |
52 |
Correct |
19 ms |
4380 KB |
Output is correct |
53 |
Correct |
20 ms |
4380 KB |
Output is correct |
54 |
Correct |
21 ms |
4380 KB |
Output is correct |
55 |
Correct |
19 ms |
4380 KB |
Output is correct |
56 |
Correct |
22 ms |
4380 KB |
Output is correct |
57 |
Correct |
40 ms |
4380 KB |
Output is correct |
58 |
Correct |
39 ms |
4380 KB |
Output is correct |
59 |
Correct |
40 ms |
4380 KB |
Output is correct |
60 |
Correct |
40 ms |
4380 KB |
Output is correct |
61 |
Correct |
19 ms |
4380 KB |
Output is correct |
62 |
Correct |
20 ms |
4380 KB |
Output is correct |
63 |
Correct |
19 ms |
4380 KB |
Output is correct |
64 |
Correct |
23 ms |
4380 KB |
Output is correct |
65 |
Correct |
24 ms |
4380 KB |
Output is correct |
66 |
Correct |
25 ms |
4380 KB |
Output is correct |
67 |
Correct |
25 ms |
4380 KB |
Output is correct |
68 |
Correct |
18 ms |
4396 KB |
Output is correct |
69 |
Correct |
16 ms |
4396 KB |
Output is correct |
70 |
Correct |
17 ms |
4396 KB |
Output is correct |
71 |
Correct |
19 ms |
4396 KB |
Output is correct |
72 |
Correct |
42 ms |
4396 KB |
Output is correct |
73 |
Correct |
39 ms |
4404 KB |
Output is correct |
74 |
Correct |
20 ms |
4404 KB |
Output is correct |
75 |
Correct |
18 ms |
4404 KB |
Output is correct |
76 |
Correct |
18 ms |
4404 KB |
Output is correct |
77 |
Correct |
18 ms |
4404 KB |
Output is correct |
78 |
Correct |
18 ms |
4404 KB |
Output is correct |
79 |
Correct |
16 ms |
4404 KB |
Output is correct |
80 |
Correct |
20 ms |
4404 KB |
Output is correct |
81 |
Correct |
18 ms |
4404 KB |
Output is correct |
82 |
Correct |
21 ms |
4404 KB |
Output is correct |
83 |
Correct |
21 ms |
4404 KB |
Output is correct |
84 |
Correct |
18 ms |
4404 KB |
Output is correct |
85 |
Correct |
19 ms |
4404 KB |
Output is correct |
86 |
Correct |
19 ms |
4404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
3576 KB |
Output is correct |
2 |
Correct |
7 ms |
3680 KB |
Output is correct |
3 |
Correct |
7 ms |
3700 KB |
Output is correct |
4 |
Correct |
8 ms |
3772 KB |
Output is correct |
5 |
Correct |
9 ms |
3772 KB |
Output is correct |
6 |
Correct |
9 ms |
3772 KB |
Output is correct |
7 |
Correct |
9 ms |
3772 KB |
Output is correct |
8 |
Correct |
8 ms |
3772 KB |
Output is correct |
9 |
Correct |
8 ms |
3772 KB |
Output is correct |
10 |
Correct |
8 ms |
3796 KB |
Output is correct |
11 |
Correct |
7 ms |
3800 KB |
Output is correct |
12 |
Correct |
8 ms |
3800 KB |
Output is correct |
13 |
Correct |
8 ms |
3924 KB |
Output is correct |
14 |
Correct |
8 ms |
3924 KB |
Output is correct |
15 |
Correct |
9 ms |
3924 KB |
Output is correct |
16 |
Correct |
9 ms |
3924 KB |
Output is correct |
17 |
Correct |
8 ms |
3924 KB |
Output is correct |
18 |
Correct |
9 ms |
3924 KB |
Output is correct |
19 |
Correct |
8 ms |
3924 KB |
Output is correct |
20 |
Correct |
8 ms |
3924 KB |
Output is correct |
21 |
Correct |
8 ms |
3924 KB |
Output is correct |
22 |
Correct |
9 ms |
3924 KB |
Output is correct |
23 |
Correct |
8 ms |
3924 KB |
Output is correct |
24 |
Correct |
9 ms |
3924 KB |
Output is correct |
25 |
Correct |
7 ms |
3924 KB |
Output is correct |
26 |
Correct |
8 ms |
3924 KB |
Output is correct |
27 |
Correct |
8 ms |
3924 KB |
Output is correct |
28 |
Correct |
7 ms |
3924 KB |
Output is correct |
29 |
Correct |
8 ms |
3924 KB |
Output is correct |
30 |
Correct |
9 ms |
3924 KB |
Output is correct |
31 |
Correct |
8 ms |
3924 KB |
Output is correct |
32 |
Correct |
8 ms |
3924 KB |
Output is correct |
33 |
Correct |
9 ms |
3924 KB |
Output is correct |
34 |
Correct |
7 ms |
3924 KB |
Output is correct |
35 |
Correct |
8 ms |
3924 KB |
Output is correct |
36 |
Correct |
8 ms |
3924 KB |
Output is correct |
37 |
Correct |
8 ms |
3924 KB |
Output is correct |
38 |
Correct |
7 ms |
3924 KB |
Output is correct |
39 |
Correct |
9 ms |
3924 KB |
Output is correct |
40 |
Correct |
10 ms |
3948 KB |
Output is correct |
41 |
Correct |
8 ms |
3948 KB |
Output is correct |
42 |
Correct |
8 ms |
3948 KB |
Output is correct |
43 |
Correct |
7 ms |
3948 KB |
Output is correct |
44 |
Correct |
9 ms |
3948 KB |
Output is correct |
45 |
Correct |
11 ms |
3948 KB |
Output is correct |
46 |
Correct |
24 ms |
4076 KB |
Output is correct |
47 |
Correct |
22 ms |
4076 KB |
Output is correct |
48 |
Correct |
24 ms |
4188 KB |
Output is correct |
49 |
Correct |
18 ms |
4364 KB |
Output is correct |
50 |
Correct |
18 ms |
4364 KB |
Output is correct |
51 |
Correct |
16 ms |
4380 KB |
Output is correct |
52 |
Correct |
19 ms |
4380 KB |
Output is correct |
53 |
Correct |
20 ms |
4380 KB |
Output is correct |
54 |
Correct |
21 ms |
4380 KB |
Output is correct |
55 |
Correct |
19 ms |
4380 KB |
Output is correct |
56 |
Correct |
22 ms |
4380 KB |
Output is correct |
57 |
Correct |
40 ms |
4380 KB |
Output is correct |
58 |
Correct |
39 ms |
4380 KB |
Output is correct |
59 |
Correct |
40 ms |
4380 KB |
Output is correct |
60 |
Correct |
40 ms |
4380 KB |
Output is correct |
61 |
Correct |
19 ms |
4380 KB |
Output is correct |
62 |
Correct |
20 ms |
4380 KB |
Output is correct |
63 |
Correct |
19 ms |
4380 KB |
Output is correct |
64 |
Correct |
23 ms |
4380 KB |
Output is correct |
65 |
Correct |
24 ms |
4380 KB |
Output is correct |
66 |
Correct |
25 ms |
4380 KB |
Output is correct |
67 |
Correct |
25 ms |
4380 KB |
Output is correct |
68 |
Correct |
18 ms |
4396 KB |
Output is correct |
69 |
Correct |
16 ms |
4396 KB |
Output is correct |
70 |
Correct |
17 ms |
4396 KB |
Output is correct |
71 |
Correct |
19 ms |
4396 KB |
Output is correct |
72 |
Correct |
42 ms |
4396 KB |
Output is correct |
73 |
Correct |
39 ms |
4404 KB |
Output is correct |
74 |
Correct |
20 ms |
4404 KB |
Output is correct |
75 |
Correct |
18 ms |
4404 KB |
Output is correct |
76 |
Correct |
18 ms |
4404 KB |
Output is correct |
77 |
Correct |
18 ms |
4404 KB |
Output is correct |
78 |
Correct |
18 ms |
4404 KB |
Output is correct |
79 |
Correct |
16 ms |
4404 KB |
Output is correct |
80 |
Correct |
20 ms |
4404 KB |
Output is correct |
81 |
Correct |
18 ms |
4404 KB |
Output is correct |
82 |
Correct |
21 ms |
4404 KB |
Output is correct |
83 |
Correct |
21 ms |
4404 KB |
Output is correct |
84 |
Correct |
18 ms |
4404 KB |
Output is correct |
85 |
Correct |
19 ms |
4404 KB |
Output is correct |
86 |
Correct |
19 ms |
4404 KB |
Output is correct |
87 |
Correct |
60 ms |
4904 KB |
Output is correct |
88 |
Correct |
203 ms |
6700 KB |
Output is correct |
89 |
Correct |
959 ms |
13600 KB |
Output is correct |
90 |
Correct |
954 ms |
13744 KB |
Output is correct |
91 |
Correct |
991 ms |
13744 KB |
Output is correct |
92 |
Correct |
340 ms |
19628 KB |
Output is correct |
93 |
Correct |
384 ms |
19756 KB |
Output is correct |
94 |
Correct |
380 ms |
19756 KB |
Output is correct |
95 |
Correct |
372 ms |
19756 KB |
Output is correct |
96 |
Correct |
431 ms |
19756 KB |
Output is correct |
97 |
Correct |
410 ms |
19756 KB |
Output is correct |
98 |
Correct |
407 ms |
19756 KB |
Output is correct |
99 |
Correct |
383 ms |
19756 KB |
Output is correct |
100 |
Correct |
1438 ms |
19756 KB |
Output is correct |
101 |
Correct |
1792 ms |
19756 KB |
Output is correct |
102 |
Correct |
1686 ms |
19756 KB |
Output is correct |
103 |
Correct |
1739 ms |
19756 KB |
Output is correct |
104 |
Correct |
523 ms |
19756 KB |
Output is correct |
105 |
Correct |
508 ms |
19756 KB |
Output is correct |
106 |
Correct |
560 ms |
19756 KB |
Output is correct |
107 |
Correct |
836 ms |
19756 KB |
Output is correct |
108 |
Correct |
818 ms |
19756 KB |
Output is correct |
109 |
Correct |
905 ms |
19756 KB |
Output is correct |
110 |
Correct |
390 ms |
19756 KB |
Output is correct |
111 |
Correct |
396 ms |
19756 KB |
Output is correct |
112 |
Correct |
393 ms |
19756 KB |
Output is correct |
113 |
Correct |
365 ms |
19756 KB |
Output is correct |
114 |
Correct |
1614 ms |
19756 KB |
Output is correct |
115 |
Correct |
1468 ms |
19756 KB |
Output is correct |
116 |
Correct |
482 ms |
19756 KB |
Output is correct |
117 |
Correct |
394 ms |
19756 KB |
Output is correct |
118 |
Correct |
416 ms |
19756 KB |
Output is correct |
119 |
Correct |
422 ms |
19756 KB |
Output is correct |
120 |
Correct |
374 ms |
19756 KB |
Output is correct |
121 |
Correct |
355 ms |
19756 KB |
Output is correct |
122 |
Correct |
372 ms |
19756 KB |
Output is correct |
123 |
Correct |
578 ms |
19756 KB |
Output is correct |
124 |
Correct |
527 ms |
19756 KB |
Output is correct |
125 |
Correct |
571 ms |
19756 KB |
Output is correct |
126 |
Correct |
509 ms |
19756 KB |
Output is correct |
127 |
Correct |
533 ms |
19756 KB |
Output is correct |
128 |
Correct |
495 ms |
19756 KB |
Output is correct |