#include "bits/stdc++.h"
using namespace std;
// #define ORD_SET
// #define ROPE
#ifdef ORD_SET
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#endif
#ifdef ROPE
#include <ext/rope>
using namespace __gnu_cxx;
#endif
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
using ll = long long;
using ld = long double;
#define pb push_back
#define ff first
#define ss second
#define sz(x) (ll)(x).size()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
// #define ll int
void freopen(string s) { freopen((s+".in").c_str(), "r", stdin); freopen((s+".out").c_str(), "w", stdout); }
void NotInteractive() { ios_base::sync_with_stdio(false); cin.tie(NULL); }
void pre(ll a) { cout<<fixed<<setprecision(a); }
ll bit(ll a) { return __builtin_popcountll(a); }
ll binmul(ll a, ll b, ll c) { ll res = 0; while(b) { if(b&1) (res += a) %= c; (a += a) %= c; b >>= 1; } return res; }
ll binpow(ll a, ll b, ll c) { ll res = 1; while(b) { if(b&1) (res *= a) %= c; (a *= a) %= c; b >>= 1; } return res; }
template<typename T> T gcd(T a, T b) { if(b==0) return a; return gcd(b, a%b); }
template<typename T> T lcm(T a, T b) { return a/gcd(a, b)*b; }
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ld rnd() { return rng()%INT_MAX*1.0/INT_MAX; }
struct sn { ll l, r, x; };
ll B = 316;
const ll mod = 1e9+7;
const ll inf = 1e18+7;
const ll MX = LLONG_MAX;
const ll MN = LLONG_MIN;
const ld P = acos(-1.0);
const ll N = 3e5+5;
ll c[N], t[N*4];
vector<ll> g[N];
void update(ll v, ll tl, ll tr, ll pos) {
if(tl==tr) {
t[v]++;
return;
}
ll tm = (tl+tr)/2;
if(pos<=tm) update(v*2, tl, tm, pos);
else update(v*2+1, tm+1, tr, pos);
t[v] = t[v*2]+t[v*2+1];
}
ll get(ll v, ll tl, ll tr, ll l, ll r) {
if(r<tl || tr<l) return 0;
if(l<=tl && tr<=r) return t[v];
ll tm = (tl+tr)/2;
return get(v*2, tl, tm, l, r)+get(v*2+1, tm+1, tr, l, r);
}
void kigash() {
ll n;
cin>>n;
map<ll, ll> m; set<ll> st; ll ks = 0;
for(ll i=1; i<=n; i++) cin>>c[i], st.insert(c[i]);
for(auto to:st) m[to] = ++ks;
for(ll i=1; i<=n; i++) c[i] = m[c[i]];
g[1].pb(1);
for(ll k=1; k<n; k++) {
ll u, v, cnt = 0;
cin>>u>>v;
for(ll i=0; i<=(n+1)*4; i++) t[i] = 0;
for(ll i=0; i<sz(g[u]); i++) {
cnt += get(1, 1, n+1, c[g[u][i]]+1, n+1);
update(1, 1, n+1, c[g[u][i]]);
}
cout<<cnt<<"\n";
for(ll i=0; i<sz(g[u]); i++) c[g[u][i]] = c[v];
g[v] = g[u];
g[v].pb(v);
}
return;
}
signed main(/*Kigash Amir*/) {
// freopen("");
NotInteractive();
// precalc();
ll tt = 1;
// cin>>tt;
for(ll i=1; i<=tt; i++) kigash();
exit(0);
}
Compilation message
construction.cpp: In function 'void freopen(std::string)':
construction.cpp:31:33: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
31 | void freopen(string s) { freopen((s+".in").c_str(), "r", stdin); freopen((s+".out").c_str(), "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
construction.cpp:31:73: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
31 | void freopen(string s) { freopen((s+".in").c_str(), "r", stdin); freopen((s+".out").c_str(), "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
4 ms |
7508 KB |
Output is correct |
6 |
Correct |
5 ms |
7476 KB |
Output is correct |
7 |
Correct |
4 ms |
7508 KB |
Output is correct |
8 |
Correct |
16 ms |
9912 KB |
Output is correct |
9 |
Correct |
16 ms |
9828 KB |
Output is correct |
10 |
Correct |
15 ms |
9828 KB |
Output is correct |
11 |
Correct |
9 ms |
8404 KB |
Output is correct |
12 |
Correct |
9 ms |
8448 KB |
Output is correct |
13 |
Correct |
9 ms |
8404 KB |
Output is correct |
14 |
Correct |
12 ms |
8464 KB |
Output is correct |
15 |
Correct |
5 ms |
7508 KB |
Output is correct |
16 |
Correct |
6 ms |
7508 KB |
Output is correct |
17 |
Correct |
5 ms |
7508 KB |
Output is correct |
18 |
Correct |
7 ms |
7428 KB |
Output is correct |
19 |
Correct |
10 ms |
8404 KB |
Output is correct |
20 |
Correct |
10 ms |
8404 KB |
Output is correct |
21 |
Correct |
10 ms |
8360 KB |
Output is correct |
22 |
Correct |
4 ms |
7380 KB |
Output is correct |
23 |
Correct |
4 ms |
7380 KB |
Output is correct |
24 |
Correct |
4 ms |
7380 KB |
Output is correct |
25 |
Correct |
4 ms |
7380 KB |
Output is correct |
26 |
Correct |
15 ms |
9812 KB |
Output is correct |
27 |
Correct |
9 ms |
8404 KB |
Output is correct |
28 |
Correct |
9 ms |
8388 KB |
Output is correct |
29 |
Correct |
11 ms |
8532 KB |
Output is correct |
30 |
Correct |
6 ms |
7508 KB |
Output is correct |
31 |
Correct |
4 ms |
7380 KB |
Output is correct |
32 |
Correct |
9 ms |
8396 KB |
Output is correct |
33 |
Correct |
7 ms |
7892 KB |
Output is correct |
34 |
Correct |
6 ms |
7764 KB |
Output is correct |
35 |
Correct |
7 ms |
7636 KB |
Output is correct |
36 |
Correct |
7 ms |
7892 KB |
Output is correct |
37 |
Correct |
6 ms |
7636 KB |
Output is correct |
38 |
Correct |
5 ms |
7636 KB |
Output is correct |
39 |
Correct |
7 ms |
7892 KB |
Output is correct |
40 |
Correct |
6 ms |
7764 KB |
Output is correct |
41 |
Correct |
6 ms |
7728 KB |
Output is correct |
42 |
Correct |
8 ms |
7892 KB |
Output is correct |
43 |
Correct |
6 ms |
7636 KB |
Output is correct |
44 |
Correct |
6 ms |
7636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
4 ms |
7508 KB |
Output is correct |
6 |
Correct |
5 ms |
7476 KB |
Output is correct |
7 |
Correct |
4 ms |
7508 KB |
Output is correct |
8 |
Correct |
16 ms |
9912 KB |
Output is correct |
9 |
Correct |
16 ms |
9828 KB |
Output is correct |
10 |
Correct |
15 ms |
9828 KB |
Output is correct |
11 |
Correct |
9 ms |
8404 KB |
Output is correct |
12 |
Correct |
9 ms |
8448 KB |
Output is correct |
13 |
Correct |
9 ms |
8404 KB |
Output is correct |
14 |
Correct |
12 ms |
8464 KB |
Output is correct |
15 |
Correct |
5 ms |
7508 KB |
Output is correct |
16 |
Correct |
6 ms |
7508 KB |
Output is correct |
17 |
Correct |
5 ms |
7508 KB |
Output is correct |
18 |
Correct |
7 ms |
7428 KB |
Output is correct |
19 |
Correct |
10 ms |
8404 KB |
Output is correct |
20 |
Correct |
10 ms |
8404 KB |
Output is correct |
21 |
Correct |
10 ms |
8360 KB |
Output is correct |
22 |
Correct |
4 ms |
7380 KB |
Output is correct |
23 |
Correct |
4 ms |
7380 KB |
Output is correct |
24 |
Correct |
4 ms |
7380 KB |
Output is correct |
25 |
Correct |
4 ms |
7380 KB |
Output is correct |
26 |
Correct |
15 ms |
9812 KB |
Output is correct |
27 |
Correct |
9 ms |
8404 KB |
Output is correct |
28 |
Correct |
9 ms |
8388 KB |
Output is correct |
29 |
Correct |
11 ms |
8532 KB |
Output is correct |
30 |
Correct |
6 ms |
7508 KB |
Output is correct |
31 |
Correct |
4 ms |
7380 KB |
Output is correct |
32 |
Correct |
9 ms |
8396 KB |
Output is correct |
33 |
Correct |
7 ms |
7892 KB |
Output is correct |
34 |
Correct |
6 ms |
7764 KB |
Output is correct |
35 |
Correct |
7 ms |
7636 KB |
Output is correct |
36 |
Correct |
7 ms |
7892 KB |
Output is correct |
37 |
Correct |
6 ms |
7636 KB |
Output is correct |
38 |
Correct |
5 ms |
7636 KB |
Output is correct |
39 |
Correct |
7 ms |
7892 KB |
Output is correct |
40 |
Correct |
6 ms |
7764 KB |
Output is correct |
41 |
Correct |
6 ms |
7728 KB |
Output is correct |
42 |
Correct |
8 ms |
7892 KB |
Output is correct |
43 |
Correct |
6 ms |
7636 KB |
Output is correct |
44 |
Correct |
6 ms |
7636 KB |
Output is correct |
45 |
Correct |
7 ms |
7648 KB |
Output is correct |
46 |
Correct |
24 ms |
8596 KB |
Output is correct |
47 |
Correct |
24 ms |
8508 KB |
Output is correct |
48 |
Correct |
23 ms |
8456 KB |
Output is correct |
49 |
Correct |
835 ms |
117096 KB |
Output is correct |
50 |
Correct |
846 ms |
117028 KB |
Output is correct |
51 |
Correct |
884 ms |
117064 KB |
Output is correct |
52 |
Correct |
428 ms |
54228 KB |
Output is correct |
53 |
Correct |
470 ms |
54184 KB |
Output is correct |
54 |
Correct |
432 ms |
54220 KB |
Output is correct |
55 |
Correct |
446 ms |
54380 KB |
Output is correct |
56 |
Correct |
425 ms |
56072 KB |
Output is correct |
57 |
Correct |
26 ms |
8640 KB |
Output is correct |
58 |
Correct |
26 ms |
8608 KB |
Output is correct |
59 |
Correct |
26 ms |
8652 KB |
Output is correct |
60 |
Correct |
26 ms |
8736 KB |
Output is correct |
61 |
Correct |
439 ms |
54392 KB |
Output is correct |
62 |
Correct |
436 ms |
54428 KB |
Output is correct |
63 |
Correct |
437 ms |
54348 KB |
Output is correct |
64 |
Correct |
21 ms |
8084 KB |
Output is correct |
65 |
Correct |
20 ms |
8020 KB |
Output is correct |
66 |
Correct |
22 ms |
8148 KB |
Output is correct |
67 |
Correct |
23 ms |
8220 KB |
Output is correct |
68 |
Correct |
886 ms |
116536 KB |
Output is correct |
69 |
Correct |
437 ms |
54108 KB |
Output is correct |
70 |
Correct |
469 ms |
53984 KB |
Output is correct |
71 |
Correct |
466 ms |
55460 KB |
Output is correct |
72 |
Correct |
31 ms |
8660 KB |
Output is correct |
73 |
Correct |
23 ms |
8148 KB |
Output is correct |
74 |
Correct |
445 ms |
54024 KB |
Output is correct |
75 |
Correct |
231 ms |
36484 KB |
Output is correct |
76 |
Correct |
165 ms |
28732 KB |
Output is correct |
77 |
Correct |
125 ms |
24312 KB |
Output is correct |
78 |
Correct |
236 ms |
36220 KB |
Output is correct |
79 |
Correct |
169 ms |
28384 KB |
Output is correct |
80 |
Correct |
128 ms |
23912 KB |
Output is correct |
81 |
Correct |
227 ms |
35792 KB |
Output is correct |
82 |
Correct |
163 ms |
28576 KB |
Output is correct |
83 |
Correct |
125 ms |
23824 KB |
Output is correct |
84 |
Correct |
237 ms |
35528 KB |
Output is correct |
85 |
Correct |
162 ms |
28088 KB |
Output is correct |
86 |
Correct |
133 ms |
23584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7380 KB |
Output is correct |
2 |
Correct |
4 ms |
7380 KB |
Output is correct |
3 |
Correct |
4 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
4 ms |
7508 KB |
Output is correct |
6 |
Correct |
5 ms |
7476 KB |
Output is correct |
7 |
Correct |
4 ms |
7508 KB |
Output is correct |
8 |
Correct |
16 ms |
9912 KB |
Output is correct |
9 |
Correct |
16 ms |
9828 KB |
Output is correct |
10 |
Correct |
15 ms |
9828 KB |
Output is correct |
11 |
Correct |
9 ms |
8404 KB |
Output is correct |
12 |
Correct |
9 ms |
8448 KB |
Output is correct |
13 |
Correct |
9 ms |
8404 KB |
Output is correct |
14 |
Correct |
12 ms |
8464 KB |
Output is correct |
15 |
Correct |
5 ms |
7508 KB |
Output is correct |
16 |
Correct |
6 ms |
7508 KB |
Output is correct |
17 |
Correct |
5 ms |
7508 KB |
Output is correct |
18 |
Correct |
7 ms |
7428 KB |
Output is correct |
19 |
Correct |
10 ms |
8404 KB |
Output is correct |
20 |
Correct |
10 ms |
8404 KB |
Output is correct |
21 |
Correct |
10 ms |
8360 KB |
Output is correct |
22 |
Correct |
4 ms |
7380 KB |
Output is correct |
23 |
Correct |
4 ms |
7380 KB |
Output is correct |
24 |
Correct |
4 ms |
7380 KB |
Output is correct |
25 |
Correct |
4 ms |
7380 KB |
Output is correct |
26 |
Correct |
15 ms |
9812 KB |
Output is correct |
27 |
Correct |
9 ms |
8404 KB |
Output is correct |
28 |
Correct |
9 ms |
8388 KB |
Output is correct |
29 |
Correct |
11 ms |
8532 KB |
Output is correct |
30 |
Correct |
6 ms |
7508 KB |
Output is correct |
31 |
Correct |
4 ms |
7380 KB |
Output is correct |
32 |
Correct |
9 ms |
8396 KB |
Output is correct |
33 |
Correct |
7 ms |
7892 KB |
Output is correct |
34 |
Correct |
6 ms |
7764 KB |
Output is correct |
35 |
Correct |
7 ms |
7636 KB |
Output is correct |
36 |
Correct |
7 ms |
7892 KB |
Output is correct |
37 |
Correct |
6 ms |
7636 KB |
Output is correct |
38 |
Correct |
5 ms |
7636 KB |
Output is correct |
39 |
Correct |
7 ms |
7892 KB |
Output is correct |
40 |
Correct |
6 ms |
7764 KB |
Output is correct |
41 |
Correct |
6 ms |
7728 KB |
Output is correct |
42 |
Correct |
8 ms |
7892 KB |
Output is correct |
43 |
Correct |
6 ms |
7636 KB |
Output is correct |
44 |
Correct |
6 ms |
7636 KB |
Output is correct |
45 |
Correct |
7 ms |
7648 KB |
Output is correct |
46 |
Correct |
24 ms |
8596 KB |
Output is correct |
47 |
Correct |
24 ms |
8508 KB |
Output is correct |
48 |
Correct |
23 ms |
8456 KB |
Output is correct |
49 |
Correct |
835 ms |
117096 KB |
Output is correct |
50 |
Correct |
846 ms |
117028 KB |
Output is correct |
51 |
Correct |
884 ms |
117064 KB |
Output is correct |
52 |
Correct |
428 ms |
54228 KB |
Output is correct |
53 |
Correct |
470 ms |
54184 KB |
Output is correct |
54 |
Correct |
432 ms |
54220 KB |
Output is correct |
55 |
Correct |
446 ms |
54380 KB |
Output is correct |
56 |
Correct |
425 ms |
56072 KB |
Output is correct |
57 |
Correct |
26 ms |
8640 KB |
Output is correct |
58 |
Correct |
26 ms |
8608 KB |
Output is correct |
59 |
Correct |
26 ms |
8652 KB |
Output is correct |
60 |
Correct |
26 ms |
8736 KB |
Output is correct |
61 |
Correct |
439 ms |
54392 KB |
Output is correct |
62 |
Correct |
436 ms |
54428 KB |
Output is correct |
63 |
Correct |
437 ms |
54348 KB |
Output is correct |
64 |
Correct |
21 ms |
8084 KB |
Output is correct |
65 |
Correct |
20 ms |
8020 KB |
Output is correct |
66 |
Correct |
22 ms |
8148 KB |
Output is correct |
67 |
Correct |
23 ms |
8220 KB |
Output is correct |
68 |
Correct |
886 ms |
116536 KB |
Output is correct |
69 |
Correct |
437 ms |
54108 KB |
Output is correct |
70 |
Correct |
469 ms |
53984 KB |
Output is correct |
71 |
Correct |
466 ms |
55460 KB |
Output is correct |
72 |
Correct |
31 ms |
8660 KB |
Output is correct |
73 |
Correct |
23 ms |
8148 KB |
Output is correct |
74 |
Correct |
445 ms |
54024 KB |
Output is correct |
75 |
Correct |
231 ms |
36484 KB |
Output is correct |
76 |
Correct |
165 ms |
28732 KB |
Output is correct |
77 |
Correct |
125 ms |
24312 KB |
Output is correct |
78 |
Correct |
236 ms |
36220 KB |
Output is correct |
79 |
Correct |
169 ms |
28384 KB |
Output is correct |
80 |
Correct |
128 ms |
23912 KB |
Output is correct |
81 |
Correct |
227 ms |
35792 KB |
Output is correct |
82 |
Correct |
163 ms |
28576 KB |
Output is correct |
83 |
Correct |
125 ms |
23824 KB |
Output is correct |
84 |
Correct |
237 ms |
35528 KB |
Output is correct |
85 |
Correct |
162 ms |
28088 KB |
Output is correct |
86 |
Correct |
133 ms |
23584 KB |
Output is correct |
87 |
Correct |
120 ms |
10296 KB |
Output is correct |
88 |
Correct |
837 ms |
16420 KB |
Output is correct |
89 |
Execution timed out |
2094 ms |
27064 KB |
Time limit exceeded |
90 |
Halted |
0 ms |
0 KB |
- |