#include "catdog.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
struct chash
{
int operator()(int x) const
{
x ^= (x >> 20) ^ (x >> 12);
return x ^ (x >> 7) ^ (x >> 4);
}
int operator()(long long x) const
{
return x ^ (x >> 32);
}
};
template<typename T> using orderedset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T, typename U> using hashtable = gp_hash_table<T, U, chash>;
template<class T>
void ckmin(T &a, T b)
{
a = min(a, b);
}
template<class T>
void ckmax(T &a, T b)
{
a = max(a, b);
}
template<class T, class U>
T normalize(T x, U mod = 1000000007)
{
return (((x % mod) + mod) % mod);
}
static long long randomizell(long long mod)
{
return ((1ll << 45) * rand() + (1ll << 30) * rand() + (1ll << 15) * rand() + rand()) % mod;
}
static int randomize(int mod)
{
return ((1ll << 15) * rand() + rand()) % mod;
}
#define y0 ___y0
#define y1 ___y1
#define MP make_pair
#define MT make_tuple
#define PB push_back
#define PF push_front
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define debug(x) cerr << #x << " = " << x << endl;
const long double PI = 4.0 * atan(1.0);
const long double EPS = 1e-10;
#define MAGIC 347
#define SINF 10007
#define CO 1000007
#define INF 1000000007
#define BIG 1000000931
#define LARGE 1696969696967ll
#define GIANT 2564008813937411ll
#define LLINF 2696969696969696969ll
#define MAXN 100013
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<pll, pll> ppp;
typedef pair<ld, ld> pdd;
int N;
vector<int> edge[MAXN];
int parent[MAXN], depth[MAXN];
int heavy[MAXN], head[MAXN], ranking[MAXN], subtree[MAXN], siz[MAXN];
int arr[MAXN];
pll dp[MAXN];
pll big[3] = {{0, 0}, {0, INF}, {INF, 0}};
ppp comb(ppp lt, ppp rt)
{
ppp res;
res.fi.fi = min(lt.fi.fi + rt.fi.fi, lt.fi.se + rt.se.fi);
res.fi.se = min(lt.fi.fi + rt.fi.se, lt.fi.se + rt.se.se);
res.se.fi = min(lt.se.fi + rt.fi.fi, lt.se.se + rt.se.fi);
res.se.se = min(lt.se.fi + rt.fi.se, lt.se.se + rt.se.se);
return res;
}
struct segtree
{
//range[L...R] = cost to change from color x to color y at L...R INCLUDING ALL THE DPS ON THE WAY
//excepts ranges are reversed: L...R means cost from R...L - 1
vector<ppp> seg;
void resize(int x)
{
seg.resize(x);
}
void update(int w, int L, int R, int a, pll p)
{
if (a < L || R < a)
{
return;
}
if (L == R)
{
//each time you change something, make it super expensive to update its parents
seg[w].fi.fi += p.fi;
seg[w].fi.se += p.fi;
seg[w].se.fi += p.se;
seg[w].se.se += p.se;
return;
}
int mid = (L + R) >> 1;
update(w << 1, L, mid, a, p);
update(w << 1 | 1, mid + 1, R, a, p);
seg[w] = comb(seg[w << 1], seg[w << 1 | 1]);
}
};
//cost of considering x -> x + 1
segtree seg[MAXN];
pll operator + (const pll &a, const pll &b)
{
return {a.fi + b.fi, a.se + b.se};
}
pll operator - (const pll &a, const pll &b)
{
return {a.fi - b.fi, a.se - b.se};
}
pll trans(int u, int v)
{
return big[v] - big[u];
}
void dfs(int u)
{
subtree[u] = 1;
for (int v : edge[u])
{
if (v == parent[u]) continue;
parent[v] = u;
depth[v] = depth[u] + 1;
dfs(v);
subtree[u] += subtree[v];
}
heavy[u] = N;
for (int v : edge[u])
{
if (v == parent[u]) continue;
if (subtree[v] * 2 >= subtree[u]) heavy[u] = v;
}
}
void dfs_heavy(int u)
{
if (heavy[u] != N)
{
head[heavy[u]] = head[u];
ranking[heavy[u]] = ranking[u] + 1;
dfs_heavy(heavy[u]);
}
for (int v : edge[u])
{
if (v == parent[u]) continue;
if (v == heavy[u]) continue;
ranking[v] = 0;
head[v] = v;
dfs_heavy(v);
}
return;
}
void initialize(int n, vector<int> a, vector<int> b)
{
N = n;
for (int i = 0; i < N - 1; i++)
{
int u = a[i], v = b[i];
u--; v--;
edge[u].PB(v);
edge[v].PB(u);
}
parent[N] = N; parent[0] = N;
dfs(0);
dfs_heavy(0);
for (int i = 0; i < N; i++)
{
// cerr << head[i] << ' ';
siz[head[i]]++;
}
// cerr << endl;
for (int i = 0; i < N; i++)
{
ranking[i] = siz[head[i]] - ranking[i] - 1;
}
for (int i = 0; i < N; i++)
{
if (siz[i] == 0) continue;
seg[i].resize((1 << (33 - __builtin_clz(siz[i] + 1))) + 1);
fill(seg[i].seg.begin(), seg[i].seg.end(), MP(MP(0, 1), MP(1, 0)));
}
}
//0, 3, 4; 1, 2; 5, 6; 8, 9
//0 -> 3 -> 4; 2 -> 1; 6 -> 5; 8; 9;
void upd(int u, pll p)
{
//cost here is actually cost L...R + 1; we want to change cost L, L + 1 once L gets changed
//each seg INCLUDES the guys from its bad children!!!
// cerr << "upd " << u << ' ' << p.fi << ' ' << p.se << endl;
ppp stor = seg[head[u]].seg[1];
pll was = {min(stor.fi.fi, stor.se.fi), min(stor.fi.se, stor.se.se)};
seg[head[u]].update(1, 0, siz[head[u]] - 1, ranking[u], p);
stor = seg[head[u]].seg[1];
pll cur = {min(stor.fi.fi, stor.se.fi), min(stor.fi.se, stor.se.se)};
// cerr << "ending at " << parent[head[u]] << " increases " << (cur - was).fi << ' ' << (cur - was).se << endl;
// cerr << cur1.fi << ' ' << cur1.se << endl;
if (head[u] == 0)
{
return;
}
upd(parent[head[u]], cur - was);
}
int setval(int u, int v)
{
// cerr << "setval " << u << ' ' << v << endl;
pll change = trans(arr[u], v);
arr[u] = v;
upd(u, change);
ppp ans = seg[0].seg[1];
// cerr << "color " << v << " ans " << ans.fi.fi << ' ' << ans.fi.se << ' ' << ans.se.fi << ' ' << ans.se.se << endl;
return min(min(ans.fi.fi, ans.fi.se), min(ans.se.fi, ans.se.se));
}
int cat(int u)
{
u--;
return setval(u, 1);
}
int dog(int u)
{
u--;
return setval(u, 2);
}
int neighbor(int u)
{
u--;
return setval(u, 0);
}
Compilation message
catdog.cpp:44:12: warning: 'int randomize(int)' defined but not used [-Wunused-function]
static int randomize(int mod)
^~~~~~~~~
catdog.cpp:40:18: warning: 'long long int randomizell(long long int)' defined but not used [-Wunused-function]
static long long randomizell(long long mod)
^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5112 KB |
Output is correct |
2 |
Correct |
7 ms |
5112 KB |
Output is correct |
3 |
Correct |
8 ms |
5156 KB |
Output is correct |
4 |
Correct |
7 ms |
5192 KB |
Output is correct |
5 |
Correct |
7 ms |
5196 KB |
Output is correct |
6 |
Correct |
7 ms |
5312 KB |
Output is correct |
7 |
Correct |
7 ms |
5364 KB |
Output is correct |
8 |
Correct |
8 ms |
5412 KB |
Output is correct |
9 |
Correct |
9 ms |
5416 KB |
Output is correct |
10 |
Correct |
7 ms |
5420 KB |
Output is correct |
11 |
Correct |
8 ms |
5556 KB |
Output is correct |
12 |
Correct |
7 ms |
5556 KB |
Output is correct |
13 |
Correct |
7 ms |
5556 KB |
Output is correct |
14 |
Correct |
8 ms |
5556 KB |
Output is correct |
15 |
Correct |
7 ms |
5556 KB |
Output is correct |
16 |
Correct |
7 ms |
5556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5112 KB |
Output is correct |
2 |
Correct |
7 ms |
5112 KB |
Output is correct |
3 |
Correct |
8 ms |
5156 KB |
Output is correct |
4 |
Correct |
7 ms |
5192 KB |
Output is correct |
5 |
Correct |
7 ms |
5196 KB |
Output is correct |
6 |
Correct |
7 ms |
5312 KB |
Output is correct |
7 |
Correct |
7 ms |
5364 KB |
Output is correct |
8 |
Correct |
8 ms |
5412 KB |
Output is correct |
9 |
Correct |
9 ms |
5416 KB |
Output is correct |
10 |
Correct |
7 ms |
5420 KB |
Output is correct |
11 |
Correct |
8 ms |
5556 KB |
Output is correct |
12 |
Correct |
7 ms |
5556 KB |
Output is correct |
13 |
Correct |
7 ms |
5556 KB |
Output is correct |
14 |
Correct |
8 ms |
5556 KB |
Output is correct |
15 |
Correct |
7 ms |
5556 KB |
Output is correct |
16 |
Correct |
7 ms |
5556 KB |
Output is correct |
17 |
Correct |
7 ms |
5636 KB |
Output is correct |
18 |
Correct |
9 ms |
5776 KB |
Output is correct |
19 |
Correct |
9 ms |
5776 KB |
Output is correct |
20 |
Correct |
9 ms |
5776 KB |
Output is correct |
21 |
Correct |
10 ms |
5776 KB |
Output is correct |
22 |
Correct |
9 ms |
5776 KB |
Output is correct |
23 |
Correct |
11 ms |
5776 KB |
Output is correct |
24 |
Correct |
10 ms |
5864 KB |
Output is correct |
25 |
Correct |
10 ms |
5864 KB |
Output is correct |
26 |
Correct |
10 ms |
5864 KB |
Output is correct |
27 |
Correct |
7 ms |
5864 KB |
Output is correct |
28 |
Correct |
8 ms |
5864 KB |
Output is correct |
29 |
Correct |
8 ms |
5864 KB |
Output is correct |
30 |
Correct |
9 ms |
5864 KB |
Output is correct |
31 |
Correct |
8 ms |
5956 KB |
Output is correct |
32 |
Correct |
8 ms |
5956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5112 KB |
Output is correct |
2 |
Correct |
7 ms |
5112 KB |
Output is correct |
3 |
Correct |
8 ms |
5156 KB |
Output is correct |
4 |
Correct |
7 ms |
5192 KB |
Output is correct |
5 |
Correct |
7 ms |
5196 KB |
Output is correct |
6 |
Correct |
7 ms |
5312 KB |
Output is correct |
7 |
Correct |
7 ms |
5364 KB |
Output is correct |
8 |
Correct |
8 ms |
5412 KB |
Output is correct |
9 |
Correct |
9 ms |
5416 KB |
Output is correct |
10 |
Correct |
7 ms |
5420 KB |
Output is correct |
11 |
Correct |
8 ms |
5556 KB |
Output is correct |
12 |
Correct |
7 ms |
5556 KB |
Output is correct |
13 |
Correct |
7 ms |
5556 KB |
Output is correct |
14 |
Correct |
8 ms |
5556 KB |
Output is correct |
15 |
Correct |
7 ms |
5556 KB |
Output is correct |
16 |
Correct |
7 ms |
5556 KB |
Output is correct |
17 |
Correct |
7 ms |
5636 KB |
Output is correct |
18 |
Correct |
9 ms |
5776 KB |
Output is correct |
19 |
Correct |
9 ms |
5776 KB |
Output is correct |
20 |
Correct |
9 ms |
5776 KB |
Output is correct |
21 |
Correct |
10 ms |
5776 KB |
Output is correct |
22 |
Correct |
9 ms |
5776 KB |
Output is correct |
23 |
Correct |
11 ms |
5776 KB |
Output is correct |
24 |
Correct |
10 ms |
5864 KB |
Output is correct |
25 |
Correct |
10 ms |
5864 KB |
Output is correct |
26 |
Correct |
10 ms |
5864 KB |
Output is correct |
27 |
Correct |
7 ms |
5864 KB |
Output is correct |
28 |
Correct |
8 ms |
5864 KB |
Output is correct |
29 |
Correct |
8 ms |
5864 KB |
Output is correct |
30 |
Correct |
9 ms |
5864 KB |
Output is correct |
31 |
Correct |
8 ms |
5956 KB |
Output is correct |
32 |
Correct |
8 ms |
5956 KB |
Output is correct |
33 |
Correct |
212 ms |
20628 KB |
Output is correct |
34 |
Correct |
130 ms |
24368 KB |
Output is correct |
35 |
Correct |
194 ms |
24368 KB |
Output is correct |
36 |
Correct |
362 ms |
34608 KB |
Output is correct |
37 |
Correct |
50 ms |
34608 KB |
Output is correct |
38 |
Correct |
364 ms |
39568 KB |
Output is correct |
39 |
Correct |
350 ms |
41600 KB |
Output is correct |
40 |
Correct |
349 ms |
43388 KB |
Output is correct |
41 |
Correct |
376 ms |
45356 KB |
Output is correct |
42 |
Correct |
315 ms |
47232 KB |
Output is correct |
43 |
Correct |
316 ms |
49116 KB |
Output is correct |
44 |
Correct |
329 ms |
51036 KB |
Output is correct |
45 |
Correct |
389 ms |
53224 KB |
Output is correct |
46 |
Correct |
354 ms |
55180 KB |
Output is correct |
47 |
Correct |
337 ms |
56848 KB |
Output is correct |
48 |
Correct |
156 ms |
59080 KB |
Output is correct |
49 |
Correct |
206 ms |
68104 KB |
Output is correct |
50 |
Correct |
50 ms |
68104 KB |
Output is correct |
51 |
Correct |
68 ms |
68104 KB |
Output is correct |
52 |
Correct |
33 ms |
68104 KB |
Output is correct |
53 |
Correct |
194 ms |
68104 KB |
Output is correct |
54 |
Correct |
107 ms |
68104 KB |
Output is correct |
55 |
Correct |
217 ms |
68104 KB |
Output is correct |
56 |
Correct |
147 ms |
68104 KB |
Output is correct |
57 |
Correct |
196 ms |
68104 KB |
Output is correct |
58 |
Correct |
50 ms |
68104 KB |
Output is correct |
59 |
Correct |
56 ms |
68104 KB |
Output is correct |
60 |
Correct |
122 ms |
74200 KB |
Output is correct |
61 |
Correct |
133 ms |
77040 KB |
Output is correct |
62 |
Correct |
111 ms |
77040 KB |
Output is correct |
63 |
Correct |
88 ms |
77040 KB |
Output is correct |
64 |
Correct |
99 ms |
77040 KB |
Output is correct |
65 |
Correct |
121 ms |
77040 KB |
Output is correct |
66 |
Correct |
74 ms |
77040 KB |
Output is correct |
67 |
Correct |
102 ms |
77040 KB |
Output is correct |
68 |
Correct |
185 ms |
77040 KB |
Output is correct |
69 |
Correct |
31 ms |
77040 KB |
Output is correct |
70 |
Correct |
16 ms |
77040 KB |
Output is correct |
71 |
Correct |
82 ms |
77040 KB |
Output is correct |
72 |
Correct |
120 ms |
77040 KB |
Output is correct |
73 |
Correct |
245 ms |
80320 KB |
Output is correct |
74 |
Correct |
220 ms |
80320 KB |
Output is correct |
75 |
Correct |
233 ms |
84204 KB |
Output is correct |
76 |
Correct |
206 ms |
86764 KB |
Output is correct |
77 |
Correct |
236 ms |
86764 KB |
Output is correct |