#include <bits/stdc++.h>
using namespace std;
//#define FILE_IO
typedef pair<int, int> pii;
typedef long long LL;
const int NMAX = 1e5 + 5;
int N, K;
int v[NMAX], sz[NMAX], chn[NMAX], f[NMAX], pos[NMAX];
vector<int> nodes[NMAX], edg[NMAX];
vector<pii> edges, blk, aux, blocks[NMAX];
class Normalizer
{
public:
map<int, int> mp;
vector<int> ord;
void add(int x)
{
if(mp.count(x)) return;
mp[x]; ord.push_back(x);
}
int get(int x) { return mp[x]; }
void normalize()
{
sort(begin(ord), end(ord));
for(int i = 0; i < ord.size(); i++)
mp[ ord[i] ] = i + 1;
}
}nrm;
class BinaryIndexedTree
{
public:
vector<int> aib;
void init(int N)
{
aib.resize(N + 5);
}
void U(int pos, int val)
{
for(; pos < aib.size(); pos += (pos & (-pos)))
aib[pos] += val;
}
int Q(int pos)
{
int ans = 0;
for(; pos > 0; pos -= (pos & (-pos)))
ans += aib[pos];
return ans;
}
}aib;
void DFS(int nod, int fth = 0)
{
sz[nod] = 1;
f[nod] = fth;
for(auto &nxt: edg[nod])
{
DFS(nxt, nod);
sz[nod] += sz[nxt];
if(sz[nxt] > sz[ edg[nod][0] ]) swap(nxt, edg[nod][0]);
}
if(edg[nod].empty())
chn[nod] = ++K;
else
chn[nod] = chn[ edg[nod][0] ];
nodes[ chn[nod] ].push_back(nod);
pos[nod] = int(nodes[ chn[nod] ].size()) - 1;
}
int main()
{
#ifdef FILE_IO
freopen("1.in", "r", stdin);
//freopen("1.out", "w", stdout);
#endif
scanf("%d", &N);
for(int i = 1; i <= N; i++) scanf("%d", &v[i]), nrm.add(v[i]);
nrm.normalize();
for(int i = 1; i <= N; i++) v[i] = nrm.get(v[i]);
for(int i = 1; i < N; i++)
{
int x, y;
scanf("%d%d", &x, &y);
edg[x].push_back(y);
edges.push_back({x, y});
}
DFS(1);
aib.init(N);
blocks[ chn[1] ].push_back({v[1], 1});
for(auto &e: edges)
{
int x = e.first, y = e.second;
int p = pos[x], c = chn[x];
blk.clear();
while(1)
{
int rem = int(nodes[c].size()) - p;
aux.clear();
while(rem)
{
if(blocks[c].back().second > rem)
{
aux.push_back({blocks[c].back().first, rem});
blocks[c][ int(blocks[c].size()) - 1 ].second -= rem;
break;
}
rem -= blocks[c].back().second;
aux.push_back(blocks[c].back());
blocks[c].pop_back();
}
reverse(aux.begin(), aux.end());
for(auto &x: aux) blk.push_back(x);
int cnt = int(nodes[c].size() - p);
if(chn[y] == c) cnt++;
blocks[c].push_back({v[y], cnt});
if(c == chn[1]) break;
int nod = f[ nodes[c].back() ];
c = chn[nod], p = pos[nod];
}
if(chn[x] != chn[y]) blocks[ chn[y] ].push_back({v[y], 1});
LL ans = 0LL;
for(auto &b: blk)
{
ans += 1LL * b.second * aib.Q(b.first - 1);
aib.U(b.first, b.second);
}
printf("%lld\n", ans);
for(auto &b: blk)
aib.U(b.first, -b.second);
}
return 0;
}
Compilation message
construction.cpp: In member function 'void Normalizer::normalize()':
construction.cpp:34:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < ord.size(); i++)
~~^~~~~~~~~~~~
construction.cpp: In member function 'void BinaryIndexedTree::U(int, int)':
construction.cpp:51:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(; pos < aib.size(); pos += (pos & (-pos)))
~~~~^~~~~~~~~~~~
construction.cpp: In function 'int main()':
construction.cpp:91:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &N);
~~~~~^~~~~~~~~~
construction.cpp:92:51: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i = 1; i <= N; i++) scanf("%d", &v[i]), nrm.add(v[i]);
~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
construction.cpp:98:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &x, &y);
~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
7424 KB |
Output is correct |
2 |
Correct |
10 ms |
7552 KB |
Output is correct |
3 |
Correct |
10 ms |
7416 KB |
Output is correct |
4 |
Correct |
11 ms |
7424 KB |
Output is correct |
5 |
Correct |
12 ms |
7424 KB |
Output is correct |
6 |
Correct |
11 ms |
7548 KB |
Output is correct |
7 |
Correct |
10 ms |
7424 KB |
Output is correct |
8 |
Correct |
11 ms |
7424 KB |
Output is correct |
9 |
Correct |
11 ms |
7424 KB |
Output is correct |
10 |
Correct |
11 ms |
7424 KB |
Output is correct |
11 |
Correct |
9 ms |
7424 KB |
Output is correct |
12 |
Correct |
12 ms |
7680 KB |
Output is correct |
13 |
Correct |
12 ms |
7424 KB |
Output is correct |
14 |
Correct |
11 ms |
7424 KB |
Output is correct |
15 |
Correct |
11 ms |
7416 KB |
Output is correct |
16 |
Correct |
9 ms |
7424 KB |
Output is correct |
17 |
Correct |
13 ms |
7424 KB |
Output is correct |
18 |
Correct |
11 ms |
7424 KB |
Output is correct |
19 |
Correct |
10 ms |
7552 KB |
Output is correct |
20 |
Correct |
10 ms |
7424 KB |
Output is correct |
21 |
Correct |
11 ms |
7552 KB |
Output is correct |
22 |
Correct |
11 ms |
7552 KB |
Output is correct |
23 |
Correct |
10 ms |
7396 KB |
Output is correct |
24 |
Correct |
11 ms |
7424 KB |
Output is correct |
25 |
Correct |
11 ms |
7424 KB |
Output is correct |
26 |
Correct |
11 ms |
7424 KB |
Output is correct |
27 |
Correct |
11 ms |
7424 KB |
Output is correct |
28 |
Correct |
10 ms |
7424 KB |
Output is correct |
29 |
Correct |
13 ms |
7424 KB |
Output is correct |
30 |
Correct |
11 ms |
7424 KB |
Output is correct |
31 |
Correct |
12 ms |
7424 KB |
Output is correct |
32 |
Correct |
10 ms |
7424 KB |
Output is correct |
33 |
Correct |
10 ms |
7424 KB |
Output is correct |
34 |
Correct |
9 ms |
7424 KB |
Output is correct |
35 |
Correct |
11 ms |
7424 KB |
Output is correct |
36 |
Correct |
9 ms |
7424 KB |
Output is correct |
37 |
Correct |
9 ms |
7424 KB |
Output is correct |
38 |
Correct |
9 ms |
7424 KB |
Output is correct |
39 |
Correct |
11 ms |
7424 KB |
Output is correct |
40 |
Correct |
11 ms |
7296 KB |
Output is correct |
41 |
Correct |
11 ms |
7552 KB |
Output is correct |
42 |
Correct |
9 ms |
7424 KB |
Output is correct |
43 |
Correct |
9 ms |
7424 KB |
Output is correct |
44 |
Correct |
10 ms |
7424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
7424 KB |
Output is correct |
2 |
Correct |
10 ms |
7552 KB |
Output is correct |
3 |
Correct |
10 ms |
7416 KB |
Output is correct |
4 |
Correct |
11 ms |
7424 KB |
Output is correct |
5 |
Correct |
12 ms |
7424 KB |
Output is correct |
6 |
Correct |
11 ms |
7548 KB |
Output is correct |
7 |
Correct |
10 ms |
7424 KB |
Output is correct |
8 |
Correct |
11 ms |
7424 KB |
Output is correct |
9 |
Correct |
11 ms |
7424 KB |
Output is correct |
10 |
Correct |
11 ms |
7424 KB |
Output is correct |
11 |
Correct |
9 ms |
7424 KB |
Output is correct |
12 |
Correct |
12 ms |
7680 KB |
Output is correct |
13 |
Correct |
12 ms |
7424 KB |
Output is correct |
14 |
Correct |
11 ms |
7424 KB |
Output is correct |
15 |
Correct |
11 ms |
7416 KB |
Output is correct |
16 |
Correct |
9 ms |
7424 KB |
Output is correct |
17 |
Correct |
13 ms |
7424 KB |
Output is correct |
18 |
Correct |
11 ms |
7424 KB |
Output is correct |
19 |
Correct |
10 ms |
7552 KB |
Output is correct |
20 |
Correct |
10 ms |
7424 KB |
Output is correct |
21 |
Correct |
11 ms |
7552 KB |
Output is correct |
22 |
Correct |
11 ms |
7552 KB |
Output is correct |
23 |
Correct |
10 ms |
7396 KB |
Output is correct |
24 |
Correct |
11 ms |
7424 KB |
Output is correct |
25 |
Correct |
11 ms |
7424 KB |
Output is correct |
26 |
Correct |
11 ms |
7424 KB |
Output is correct |
27 |
Correct |
11 ms |
7424 KB |
Output is correct |
28 |
Correct |
10 ms |
7424 KB |
Output is correct |
29 |
Correct |
13 ms |
7424 KB |
Output is correct |
30 |
Correct |
11 ms |
7424 KB |
Output is correct |
31 |
Correct |
12 ms |
7424 KB |
Output is correct |
32 |
Correct |
10 ms |
7424 KB |
Output is correct |
33 |
Correct |
10 ms |
7424 KB |
Output is correct |
34 |
Correct |
9 ms |
7424 KB |
Output is correct |
35 |
Correct |
11 ms |
7424 KB |
Output is correct |
36 |
Correct |
9 ms |
7424 KB |
Output is correct |
37 |
Correct |
9 ms |
7424 KB |
Output is correct |
38 |
Correct |
9 ms |
7424 KB |
Output is correct |
39 |
Correct |
11 ms |
7424 KB |
Output is correct |
40 |
Correct |
11 ms |
7296 KB |
Output is correct |
41 |
Correct |
11 ms |
7552 KB |
Output is correct |
42 |
Correct |
9 ms |
7424 KB |
Output is correct |
43 |
Correct |
9 ms |
7424 KB |
Output is correct |
44 |
Correct |
10 ms |
7424 KB |
Output is correct |
45 |
Correct |
13 ms |
7552 KB |
Output is correct |
46 |
Correct |
14 ms |
8056 KB |
Output is correct |
47 |
Correct |
19 ms |
8064 KB |
Output is correct |
48 |
Correct |
17 ms |
8064 KB |
Output is correct |
49 |
Correct |
16 ms |
8192 KB |
Output is correct |
50 |
Correct |
18 ms |
8172 KB |
Output is correct |
51 |
Correct |
16 ms |
8144 KB |
Output is correct |
52 |
Correct |
18 ms |
8192 KB |
Output is correct |
53 |
Correct |
20 ms |
8320 KB |
Output is correct |
54 |
Correct |
16 ms |
8192 KB |
Output is correct |
55 |
Correct |
16 ms |
8140 KB |
Output is correct |
56 |
Correct |
20 ms |
8184 KB |
Output is correct |
57 |
Correct |
22 ms |
7936 KB |
Output is correct |
58 |
Correct |
21 ms |
8068 KB |
Output is correct |
59 |
Correct |
23 ms |
8056 KB |
Output is correct |
60 |
Correct |
20 ms |
8064 KB |
Output is correct |
61 |
Correct |
22 ms |
8140 KB |
Output is correct |
62 |
Correct |
18 ms |
8064 KB |
Output is correct |
63 |
Correct |
15 ms |
8192 KB |
Output is correct |
64 |
Correct |
15 ms |
7808 KB |
Output is correct |
65 |
Correct |
16 ms |
7808 KB |
Output is correct |
66 |
Correct |
14 ms |
7808 KB |
Output is correct |
67 |
Correct |
19 ms |
7936 KB |
Output is correct |
68 |
Correct |
13 ms |
7936 KB |
Output is correct |
69 |
Correct |
19 ms |
8192 KB |
Output is correct |
70 |
Correct |
16 ms |
7928 KB |
Output is correct |
71 |
Correct |
15 ms |
7936 KB |
Output is correct |
72 |
Correct |
20 ms |
7968 KB |
Output is correct |
73 |
Correct |
20 ms |
7808 KB |
Output is correct |
74 |
Correct |
14 ms |
7936 KB |
Output is correct |
75 |
Correct |
16 ms |
8064 KB |
Output is correct |
76 |
Correct |
17 ms |
8064 KB |
Output is correct |
77 |
Correct |
18 ms |
8028 KB |
Output is correct |
78 |
Correct |
20 ms |
7856 KB |
Output is correct |
79 |
Correct |
15 ms |
7808 KB |
Output is correct |
80 |
Correct |
14 ms |
7808 KB |
Output is correct |
81 |
Correct |
15 ms |
8064 KB |
Output is correct |
82 |
Correct |
16 ms |
8064 KB |
Output is correct |
83 |
Correct |
19 ms |
8036 KB |
Output is correct |
84 |
Correct |
13 ms |
7808 KB |
Output is correct |
85 |
Correct |
13 ms |
7808 KB |
Output is correct |
86 |
Correct |
13 ms |
7808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
7424 KB |
Output is correct |
2 |
Correct |
10 ms |
7552 KB |
Output is correct |
3 |
Correct |
10 ms |
7416 KB |
Output is correct |
4 |
Correct |
11 ms |
7424 KB |
Output is correct |
5 |
Correct |
12 ms |
7424 KB |
Output is correct |
6 |
Correct |
11 ms |
7548 KB |
Output is correct |
7 |
Correct |
10 ms |
7424 KB |
Output is correct |
8 |
Correct |
11 ms |
7424 KB |
Output is correct |
9 |
Correct |
11 ms |
7424 KB |
Output is correct |
10 |
Correct |
11 ms |
7424 KB |
Output is correct |
11 |
Correct |
9 ms |
7424 KB |
Output is correct |
12 |
Correct |
12 ms |
7680 KB |
Output is correct |
13 |
Correct |
12 ms |
7424 KB |
Output is correct |
14 |
Correct |
11 ms |
7424 KB |
Output is correct |
15 |
Correct |
11 ms |
7416 KB |
Output is correct |
16 |
Correct |
9 ms |
7424 KB |
Output is correct |
17 |
Correct |
13 ms |
7424 KB |
Output is correct |
18 |
Correct |
11 ms |
7424 KB |
Output is correct |
19 |
Correct |
10 ms |
7552 KB |
Output is correct |
20 |
Correct |
10 ms |
7424 KB |
Output is correct |
21 |
Correct |
11 ms |
7552 KB |
Output is correct |
22 |
Correct |
11 ms |
7552 KB |
Output is correct |
23 |
Correct |
10 ms |
7396 KB |
Output is correct |
24 |
Correct |
11 ms |
7424 KB |
Output is correct |
25 |
Correct |
11 ms |
7424 KB |
Output is correct |
26 |
Correct |
11 ms |
7424 KB |
Output is correct |
27 |
Correct |
11 ms |
7424 KB |
Output is correct |
28 |
Correct |
10 ms |
7424 KB |
Output is correct |
29 |
Correct |
13 ms |
7424 KB |
Output is correct |
30 |
Correct |
11 ms |
7424 KB |
Output is correct |
31 |
Correct |
12 ms |
7424 KB |
Output is correct |
32 |
Correct |
10 ms |
7424 KB |
Output is correct |
33 |
Correct |
10 ms |
7424 KB |
Output is correct |
34 |
Correct |
9 ms |
7424 KB |
Output is correct |
35 |
Correct |
11 ms |
7424 KB |
Output is correct |
36 |
Correct |
9 ms |
7424 KB |
Output is correct |
37 |
Correct |
9 ms |
7424 KB |
Output is correct |
38 |
Correct |
9 ms |
7424 KB |
Output is correct |
39 |
Correct |
11 ms |
7424 KB |
Output is correct |
40 |
Correct |
11 ms |
7296 KB |
Output is correct |
41 |
Correct |
11 ms |
7552 KB |
Output is correct |
42 |
Correct |
9 ms |
7424 KB |
Output is correct |
43 |
Correct |
9 ms |
7424 KB |
Output is correct |
44 |
Correct |
10 ms |
7424 KB |
Output is correct |
45 |
Correct |
13 ms |
7552 KB |
Output is correct |
46 |
Correct |
14 ms |
8056 KB |
Output is correct |
47 |
Correct |
19 ms |
8064 KB |
Output is correct |
48 |
Correct |
17 ms |
8064 KB |
Output is correct |
49 |
Correct |
16 ms |
8192 KB |
Output is correct |
50 |
Correct |
18 ms |
8172 KB |
Output is correct |
51 |
Correct |
16 ms |
8144 KB |
Output is correct |
52 |
Correct |
18 ms |
8192 KB |
Output is correct |
53 |
Correct |
20 ms |
8320 KB |
Output is correct |
54 |
Correct |
16 ms |
8192 KB |
Output is correct |
55 |
Correct |
16 ms |
8140 KB |
Output is correct |
56 |
Correct |
20 ms |
8184 KB |
Output is correct |
57 |
Correct |
22 ms |
7936 KB |
Output is correct |
58 |
Correct |
21 ms |
8068 KB |
Output is correct |
59 |
Correct |
23 ms |
8056 KB |
Output is correct |
60 |
Correct |
20 ms |
8064 KB |
Output is correct |
61 |
Correct |
22 ms |
8140 KB |
Output is correct |
62 |
Correct |
18 ms |
8064 KB |
Output is correct |
63 |
Correct |
15 ms |
8192 KB |
Output is correct |
64 |
Correct |
15 ms |
7808 KB |
Output is correct |
65 |
Correct |
16 ms |
7808 KB |
Output is correct |
66 |
Correct |
14 ms |
7808 KB |
Output is correct |
67 |
Correct |
19 ms |
7936 KB |
Output is correct |
68 |
Correct |
13 ms |
7936 KB |
Output is correct |
69 |
Correct |
19 ms |
8192 KB |
Output is correct |
70 |
Correct |
16 ms |
7928 KB |
Output is correct |
71 |
Correct |
15 ms |
7936 KB |
Output is correct |
72 |
Correct |
20 ms |
7968 KB |
Output is correct |
73 |
Correct |
20 ms |
7808 KB |
Output is correct |
74 |
Correct |
14 ms |
7936 KB |
Output is correct |
75 |
Correct |
16 ms |
8064 KB |
Output is correct |
76 |
Correct |
17 ms |
8064 KB |
Output is correct |
77 |
Correct |
18 ms |
8028 KB |
Output is correct |
78 |
Correct |
20 ms |
7856 KB |
Output is correct |
79 |
Correct |
15 ms |
7808 KB |
Output is correct |
80 |
Correct |
14 ms |
7808 KB |
Output is correct |
81 |
Correct |
15 ms |
8064 KB |
Output is correct |
82 |
Correct |
16 ms |
8064 KB |
Output is correct |
83 |
Correct |
19 ms |
8036 KB |
Output is correct |
84 |
Correct |
13 ms |
7808 KB |
Output is correct |
85 |
Correct |
13 ms |
7808 KB |
Output is correct |
86 |
Correct |
13 ms |
7808 KB |
Output is correct |
87 |
Correct |
33 ms |
9004 KB |
Output is correct |
88 |
Correct |
101 ms |
12020 KB |
Output is correct |
89 |
Correct |
537 ms |
23148 KB |
Output is correct |
90 |
Correct |
486 ms |
22892 KB |
Output is correct |
91 |
Correct |
530 ms |
23280 KB |
Output is correct |
92 |
Correct |
397 ms |
26364 KB |
Output is correct |
93 |
Correct |
414 ms |
26348 KB |
Output is correct |
94 |
Correct |
398 ms |
26348 KB |
Output is correct |
95 |
Correct |
370 ms |
26536 KB |
Output is correct |
96 |
Correct |
332 ms |
26864 KB |
Output is correct |
97 |
Correct |
357 ms |
27024 KB |
Output is correct |
98 |
Correct |
342 ms |
26860 KB |
Output is correct |
99 |
Correct |
378 ms |
25364 KB |
Output is correct |
100 |
Correct |
660 ms |
22384 KB |
Output is correct |
101 |
Correct |
701 ms |
22636 KB |
Output is correct |
102 |
Correct |
731 ms |
22664 KB |
Output is correct |
103 |
Correct |
617 ms |
22508 KB |
Output is correct |
104 |
Correct |
341 ms |
25352 KB |
Output is correct |
105 |
Correct |
411 ms |
25448 KB |
Output is correct |
106 |
Correct |
302 ms |
25312 KB |
Output is correct |
107 |
Correct |
287 ms |
17000 KB |
Output is correct |
108 |
Correct |
309 ms |
17152 KB |
Output is correct |
109 |
Correct |
362 ms |
18984 KB |
Output is correct |
110 |
Correct |
169 ms |
20584 KB |
Output is correct |
111 |
Correct |
368 ms |
26448 KB |
Output is correct |
112 |
Correct |
190 ms |
21356 KB |
Output is correct |
113 |
Correct |
164 ms |
19708 KB |
Output is correct |
114 |
Correct |
644 ms |
22208 KB |
Output is correct |
115 |
Correct |
472 ms |
17020 KB |
Output is correct |
116 |
Correct |
201 ms |
19560 KB |
Output is correct |
117 |
Correct |
377 ms |
23568 KB |
Output is correct |
118 |
Correct |
383 ms |
23148 KB |
Output is correct |
119 |
Correct |
424 ms |
23092 KB |
Output is correct |
120 |
Correct |
210 ms |
18296 KB |
Output is correct |
121 |
Correct |
209 ms |
18096 KB |
Output is correct |
122 |
Correct |
177 ms |
17900 KB |
Output is correct |
123 |
Correct |
407 ms |
23652 KB |
Output is correct |
124 |
Correct |
387 ms |
23272 KB |
Output is correct |
125 |
Correct |
470 ms |
23224 KB |
Output is correct |
126 |
Correct |
252 ms |
18508 KB |
Output is correct |
127 |
Correct |
237 ms |
18144 KB |
Output is correct |
128 |
Correct |
224 ms |
17900 KB |
Output is correct |