답안 #937166

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
937166 2024-03-03T14:51:57 Z nguyentunglam Cats or Dogs (JOI18_catdog) C++17
100 / 100
522 ms 27000 KB
#include<bits/stdc++.h>
#define fi first
#define se second
#define endl "\n"
#define ii pair<int, int>
using namespace std;

const int N = 1e5 + 10;

int n, a[N], c[N], d[N];

int heavy[N], sz[N], rev_st[N], st[N], ed[N], p[N], root[N], timer;

vector<int> adj[N];

struct dp {
  int f[2][2];
};

dp T[N << 2];

dp zero, _zero;

dp gather (dp left, dp right) {
  dp ret;
  if (left.f[0][0] < 0) return right;
  if (right.f[0][0] < 0) return left;
  for(int x = 0; x < 2; x++) for(int y = 0; y < 2; y++) {
    ret.f[x][y] = 1e9;
    for(int z = 0; z < 2; z++) {
      ret.f[x][y] = min({ret.f[x][y], left.f[x][z] + right.f[z][y], left.f[x][z] + right.f[z ^ 1][y] + 1});
    }
    assert(ret.f[x][y] <= 1e9);
  }
  return ret;
}

void up (int s, int l, int r, int pos) {
  if (l == r) {
    int u = rev_st[pos];
    T[s] = zero;
    if (a[u] != 2) T[s].f[0][0] = c[u];
    if (a[u] != 1) T[s].f[1][1] = d[u];
//    cout << T[s].f[0][0] << " " << T[s].f[0][1] << " " << T[s].f[1][0] << " " << T[s].f[1][1] << endl;
    return;
  }
  int mid = l + r >> 1;
  if (pos <= mid) up(s << 1, l, mid, pos);
  else up(s << 1 | 1, mid + 1, r, pos);
  T[s] = gather(T[s << 1], T[s << 1 | 1]);
//  if (pos == 2) {
//    cout << l << " " << r << endl;
//    cout << T[s].f[0][0] << " " << T[s].f[0][1] << " " << T[s].f[1][0] << " " << T[s].f[1][1] << endl;
//  }
}

dp get (int s, int l, int r, int from, int to) {
  if (l > to || r < from) return _zero;
  if (from <= l && r <= to) return T[s];
  int mid = l + r >> 1;
  return gather(get(s << 1, l, mid, from, to), get(s << 1 | 1, mid + 1, r, from, to));
}

void dfs(int u) {
  sz[u] = 1;
  for(int &v : adj[u]) if (!sz[v]) {
    p[v] = u;
    dfs(v);
    sz[u] += sz[v];
    if (sz[v] > sz[heavy[u]]) heavy[u] = v;
  }
}

void hld (int u) {
  st[u] = ++timer;
  rev_st[timer] = u;
  if (!root[u]) root[u] = u;
  ed[root[u]] = timer;

  if (heavy[u]) {
    root[heavy[u]] = root[u];
    hld(heavy[u]);
  }

  for(int &v : adj[u]) if (v != heavy[u] && v != p[u]) hld(v);
}

void print(dp x) {
  cout << x.f[0][0] << " " << x.f[0][1] << " " << x.f[1][0] << " " << x.f[1][1] << endl;
}

void initialize (int N, vector<int> A, vector<int> B) {
  n = N;
  zero.f[0][0] = zero.f[0][1] = zero.f[1][0] = zero.f[1][1] = 1e9;
  _zero.f[0][0] = _zero.f[0][1] = _zero.f[1][0] = _zero.f[1][1] = -1;
  for(int i = 0; i < n - 1; i++) {
    adj[A[i]].push_back(B[i]);
    adj[B[i]].push_back(A[i]);
  }
  dfs(1);
  hld(1);
  for(int i = 1; i <= n; i++) up(1, 1, n, i);
}

int update(int u) {
  while (1) {
    dp old = zero;
    if (root[u] != 1) old = get(1, 1, n, st[root[u]], ed[root[u]]);
    up(1, 1, n, st[u]);
    dp nxt = get(1, 1, n, st[root[u]], ed[root[u]]);
//    print(nxt);
    u = p[root[u]];
    if (u == 0) return min({nxt.f[0][0], nxt.f[0][1], nxt.f[1][0], nxt.f[1][1]});
    else {
      int _c, _d;
      _c = min(old.f[0][0], old.f[0][1]);
      _d = min(old.f[1][0], old.f[1][1]);
      c[u] -= min(_c, _d + 1);
      d[u] -= min(_d, _c + 1);

      _c = min(nxt.f[0][0], nxt.f[0][1]);
      _d = min(nxt.f[1][0], nxt.f[1][1]);
      c[u] += min(_c, _d + 1);
      d[u] += min(_d, _c + 1);
    }
  }
}

int cat(int v) {
  a[v] = 1;
  return update(v);
}

int dog(int v) {
  a[v] = 2;
  return update(v);
}

int neighbor(int v) {
  a[v] = 0;
  return update(v);
}

#ifdef ngu
int main() {

  freopen ("task.inp", "r", stdin);
  freopen ("task.out", "w", stdout);

  int n; cin >> n;

  vector<int> a(n - 1), b(n - 1);

  for(int i = 0; i < n - 1; i++) cin >> a[i] >> b[i];

  initialize(n, a, b);

  int q; cin >> q;

  while (q--) {
    int t, v; cin >> t >> v;
    if (t == 1) cout << cat(v);
    if (t == 2) cout << dog(v);
    if (t == 3) cout << neighbor(v);
    cout << endl;
  }
}
#endif // ngu

Compilation message

catdog.cpp: In function 'void up(int, int, int, int)':
catdog.cpp:47:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   47 |   int mid = l + r >> 1;
      |             ~~^~~
catdog.cpp: In function 'dp get(int, int, int, int, int)':
catdog.cpp:60:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   60 |   int mid = l + r >> 1;
      |             ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8536 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 1 ms 8792 KB Output is correct
4 Correct 2 ms 8540 KB Output is correct
5 Correct 1 ms 8540 KB Output is correct
6 Correct 1 ms 8540 KB Output is correct
7 Correct 2 ms 8540 KB Output is correct
8 Correct 1 ms 8540 KB Output is correct
9 Correct 1 ms 8540 KB Output is correct
10 Correct 2 ms 8536 KB Output is correct
11 Correct 1 ms 8540 KB Output is correct
12 Correct 2 ms 8540 KB Output is correct
13 Correct 1 ms 8540 KB Output is correct
14 Correct 2 ms 8540 KB Output is correct
15 Correct 1 ms 8536 KB Output is correct
16 Correct 1 ms 8536 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8536 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 1 ms 8792 KB Output is correct
4 Correct 2 ms 8540 KB Output is correct
5 Correct 1 ms 8540 KB Output is correct
6 Correct 1 ms 8540 KB Output is correct
7 Correct 2 ms 8540 KB Output is correct
8 Correct 1 ms 8540 KB Output is correct
9 Correct 1 ms 8540 KB Output is correct
10 Correct 2 ms 8536 KB Output is correct
11 Correct 1 ms 8540 KB Output is correct
12 Correct 2 ms 8540 KB Output is correct
13 Correct 1 ms 8540 KB Output is correct
14 Correct 2 ms 8540 KB Output is correct
15 Correct 1 ms 8536 KB Output is correct
16 Correct 1 ms 8536 KB Output is correct
17 Correct 3 ms 8540 KB Output is correct
18 Correct 3 ms 8540 KB Output is correct
19 Correct 2 ms 8664 KB Output is correct
20 Correct 1 ms 8540 KB Output is correct
21 Correct 2 ms 8536 KB Output is correct
22 Correct 2 ms 8540 KB Output is correct
23 Correct 3 ms 8540 KB Output is correct
24 Correct 3 ms 8540 KB Output is correct
25 Correct 2 ms 8540 KB Output is correct
26 Correct 2 ms 8540 KB Output is correct
27 Correct 2 ms 8540 KB Output is correct
28 Correct 2 ms 8540 KB Output is correct
29 Correct 2 ms 8788 KB Output is correct
30 Correct 2 ms 8540 KB Output is correct
31 Correct 2 ms 8668 KB Output is correct
32 Correct 2 ms 8540 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8536 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 1 ms 8792 KB Output is correct
4 Correct 2 ms 8540 KB Output is correct
5 Correct 1 ms 8540 KB Output is correct
6 Correct 1 ms 8540 KB Output is correct
7 Correct 2 ms 8540 KB Output is correct
8 Correct 1 ms 8540 KB Output is correct
9 Correct 1 ms 8540 KB Output is correct
10 Correct 2 ms 8536 KB Output is correct
11 Correct 1 ms 8540 KB Output is correct
12 Correct 2 ms 8540 KB Output is correct
13 Correct 1 ms 8540 KB Output is correct
14 Correct 2 ms 8540 KB Output is correct
15 Correct 1 ms 8536 KB Output is correct
16 Correct 1 ms 8536 KB Output is correct
17 Correct 3 ms 8540 KB Output is correct
18 Correct 3 ms 8540 KB Output is correct
19 Correct 2 ms 8664 KB Output is correct
20 Correct 1 ms 8540 KB Output is correct
21 Correct 2 ms 8536 KB Output is correct
22 Correct 2 ms 8540 KB Output is correct
23 Correct 3 ms 8540 KB Output is correct
24 Correct 3 ms 8540 KB Output is correct
25 Correct 2 ms 8540 KB Output is correct
26 Correct 2 ms 8540 KB Output is correct
27 Correct 2 ms 8540 KB Output is correct
28 Correct 2 ms 8540 KB Output is correct
29 Correct 2 ms 8788 KB Output is correct
30 Correct 2 ms 8540 KB Output is correct
31 Correct 2 ms 8668 KB Output is correct
32 Correct 2 ms 8540 KB Output is correct
33 Correct 285 ms 15096 KB Output is correct
34 Correct 102 ms 14932 KB Output is correct
35 Correct 260 ms 14404 KB Output is correct
36 Correct 430 ms 19868 KB Output is correct
37 Correct 21 ms 12780 KB Output is correct
38 Correct 473 ms 20540 KB Output is correct
39 Correct 469 ms 20544 KB Output is correct
40 Correct 522 ms 20356 KB Output is correct
41 Correct 491 ms 20456 KB Output is correct
42 Correct 435 ms 20556 KB Output is correct
43 Correct 445 ms 20544 KB Output is correct
44 Correct 430 ms 20800 KB Output is correct
45 Correct 430 ms 20552 KB Output is correct
46 Correct 446 ms 20548 KB Output is correct
47 Correct 459 ms 20544 KB Output is correct
48 Correct 131 ms 18380 KB Output is correct
49 Correct 149 ms 19524 KB Output is correct
50 Correct 51 ms 12628 KB Output is correct
51 Correct 59 ms 13528 KB Output is correct
52 Correct 25 ms 12244 KB Output is correct
53 Correct 198 ms 19148 KB Output is correct
54 Correct 147 ms 13708 KB Output is correct
55 Correct 386 ms 18508 KB Output is correct
56 Correct 233 ms 14612 KB Output is correct
57 Correct 323 ms 19284 KB Output is correct
58 Correct 30 ms 13528 KB Output is correct
59 Correct 54 ms 13408 KB Output is correct
60 Correct 124 ms 18984 KB Output is correct
61 Correct 134 ms 19404 KB Output is correct
62 Correct 85 ms 17876 KB Output is correct
63 Correct 50 ms 16220 KB Output is correct
64 Correct 51 ms 17668 KB Output is correct
65 Correct 73 ms 23268 KB Output is correct
66 Correct 59 ms 13836 KB Output is correct
67 Correct 72 ms 20820 KB Output is correct
68 Correct 134 ms 23776 KB Output is correct
69 Correct 26 ms 12124 KB Output is correct
70 Correct 6 ms 8916 KB Output is correct
71 Correct 54 ms 15964 KB Output is correct
72 Correct 84 ms 22324 KB Output is correct
73 Correct 208 ms 27000 KB Output is correct
74 Correct 260 ms 23640 KB Output is correct
75 Correct 144 ms 26196 KB Output is correct
76 Correct 142 ms 25296 KB Output is correct
77 Correct 253 ms 23936 KB Output is correct