Submission #62872

# Submission time Handle Problem Language Result Execution time Memory
62872 2018-07-30T15:19:40 Z win11905 Cats or Dogs (JOI18_catdog) C++11
100 / 100
1506 ms 78740 KB
#include "catdog.h"
#include <bits/stdc++.h>
#define pii pair<int, int>
#define x first
#define y second
using namespace std;

const int N = 1<<17, INF = 1e9;

int n, col[N];
vector<vector<int> > g(N);
int par[N], spi[N], rot[N], dep[N], pos[N], last[N];
pii lastupd[N];

struct item {
  int d[4];
  item() { }
  friend item operator+(const item &a, const item &b) {
    item z;
    for(int i = 0; i < 4; ++i) {
      z.d[i] = INF;
      for(int j = 0; j < 4; ++j) z.d[i] = min(z.d[i], a.d[i&1|((j&1)<<1)] + b.d[(i&2)|((j&2)>>1)] + (j == 2 || j == 1));
    }
    return z;
  }
};

item t[N<<1];

void build(int p = 1, int l = 1, int r = n) {
  if(l == r) {
    t[p].d[0] = t[p].d[3] = 0;
    t[p].d[1] = t[p].d[2] = INF;
    return;
  }
  int m = l + r >> 1;
  build(p<<1, l, m), build(p<<1|1, m+1, r);
  t[p] = t[p<<1] + t[p<<1|1];
}

int dfs(int u, int p) {
  dep[u] = dep[p] + 1, par[u] = p;
  pii ret(0, -1);
  int sz = 1;
  for(int v : g[u]) if(v != p) {
    int now = dfs(v, u);
    if(now > ret.x) ret = pii(now, v);
    sz += now;
  }
  spi[u] = ret.y;
  return sz;
}

void hld() {
  dfs(1, 0);
  for(int i = 1, id = 0; i <= n; ++i) if(spi[par[i]] != i) 
    for(int j = i; j != -1; j = spi[j]) pos[j] = ++id, rot[j] = i, last[i] = j;
  build();
}

void updt(int x, pii v, int p = 1, int l = 1, int r = n) {
  if(l == r) {
    t[p].d[0] += v.x, t[p].d[3] += v.y; 
    return;
  }
  int m = l + r >> 1;
  if(x <= m) updt(x, v, p<<1, l, m);
  else updt(x, v, p<<1|1, m+1, r);
  t[p] = t[p<<1] + t[p<<1|1];
}

item query(int x, int y, int p = 1, int l = 1, int r = n) {
  if(x <= l && r <= y) return t[p];
  int m = l + r >> 1;
  if(y <= m) return query(x, y, p<<1, l, m);
  if(x > m) return query(x, y, p<<1|1, m+1, r);
  return query(x, y, p<<1, l, m) + query(x, y, p<<1|1, m+1, r);
}

void update(int x, pii v) {
  while(x) { 
    updt(pos[x], v);
    item ret = query(pos[rot[x]], pos[last[rot[x]]]);
    int b0 = min(ret.d[0], ret.d[2]), b1 = min(ret.d[1], ret.d[3]);
    pii nlast(min(b0, 1 + b1), min(b1, 1 + b0));
    v = pii(nlast.x - lastupd[rot[x]].x, nlast.y - lastupd[rot[x]].y);
    lastupd[rot[x]] = nlast;
    x = par[rot[x]];
  }
}

void initialize(int N, vector<int> A, vector<int> B) {
  n = N;
  for(int i = 0; i < n-1; ++i) {
    g[A[i]].emplace_back(B[i]);
    g[B[i]].emplace_back(A[i]);
  }
  hld();
}

int getMin() {
  item ret = query(1, pos[last[1]]);
  return *min_element(ret.d, ret.d+4);
}

int cat(int v) {
  col[v] = 0;
  update(v, pii(0, INF));
  return getMin();
}

int dog(int v) {
  col[v] = 1;
  update(v, pii(INF, 0));
  return getMin();
}

int neighbor(int v) {
  if(col[v]) update(v, pii(-INF, 0));
  else update(v, pii(0, -INF));
  return getMin();
}

Compilation message

catdog.cpp: In function 'item operator+(const item&, const item&)':
catdog.cpp:22:60: warning: suggest parentheses around arithmetic in operand of '|' [-Wparentheses]
       for(int j = 0; j < 4; ++j) z.d[i] = min(z.d[i], a.d[i&1|((j&1)<<1)] + b.d[(i&2)|((j&2)>>1)] + (j == 2 || j == 1));
                                                           ~^~
catdog.cpp: In function 'void build(int, int, int)':
catdog.cpp:36:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   int m = l + r >> 1;
           ~~^~~
catdog.cpp: In function 'void updt(int, std::pair<int, int>, int, int, int)':
catdog.cpp:66:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   int m = l + r >> 1;
           ~~^~~
catdog.cpp: In function 'item query(int, int, int, int, int)':
catdog.cpp:74:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   int m = l + r >> 1;
           ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 3448 KB Output is correct
2 Correct 5 ms 3568 KB Output is correct
3 Correct 5 ms 3680 KB Output is correct
4 Correct 6 ms 3680 KB Output is correct
5 Correct 5 ms 3712 KB Output is correct
6 Correct 7 ms 3720 KB Output is correct
7 Correct 6 ms 3724 KB Output is correct
8 Correct 6 ms 3856 KB Output is correct
9 Correct 6 ms 3856 KB Output is correct
10 Correct 6 ms 3856 KB Output is correct
11 Correct 5 ms 3856 KB Output is correct
12 Correct 5 ms 3856 KB Output is correct
13 Correct 6 ms 3856 KB Output is correct
14 Correct 5 ms 3856 KB Output is correct
15 Correct 6 ms 3856 KB Output is correct
16 Correct 6 ms 3856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 3448 KB Output is correct
2 Correct 5 ms 3568 KB Output is correct
3 Correct 5 ms 3680 KB Output is correct
4 Correct 6 ms 3680 KB Output is correct
5 Correct 5 ms 3712 KB Output is correct
6 Correct 7 ms 3720 KB Output is correct
7 Correct 6 ms 3724 KB Output is correct
8 Correct 6 ms 3856 KB Output is correct
9 Correct 6 ms 3856 KB Output is correct
10 Correct 6 ms 3856 KB Output is correct
11 Correct 5 ms 3856 KB Output is correct
12 Correct 5 ms 3856 KB Output is correct
13 Correct 6 ms 3856 KB Output is correct
14 Correct 5 ms 3856 KB Output is correct
15 Correct 6 ms 3856 KB Output is correct
16 Correct 6 ms 3856 KB Output is correct
17 Correct 10 ms 4068 KB Output is correct
18 Correct 9 ms 4068 KB Output is correct
19 Correct 7 ms 4068 KB Output is correct
20 Correct 6 ms 4068 KB Output is correct
21 Correct 6 ms 4068 KB Output is correct
22 Correct 7 ms 4148 KB Output is correct
23 Correct 9 ms 4148 KB Output is correct
24 Correct 11 ms 4148 KB Output is correct
25 Correct 8 ms 4148 KB Output is correct
26 Correct 8 ms 4148 KB Output is correct
27 Correct 7 ms 4148 KB Output is correct
28 Correct 9 ms 4148 KB Output is correct
29 Correct 9 ms 4156 KB Output is correct
30 Correct 8 ms 4156 KB Output is correct
31 Correct 7 ms 4236 KB Output is correct
32 Correct 7 ms 4236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 3448 KB Output is correct
2 Correct 5 ms 3568 KB Output is correct
3 Correct 5 ms 3680 KB Output is correct
4 Correct 6 ms 3680 KB Output is correct
5 Correct 5 ms 3712 KB Output is correct
6 Correct 7 ms 3720 KB Output is correct
7 Correct 6 ms 3724 KB Output is correct
8 Correct 6 ms 3856 KB Output is correct
9 Correct 6 ms 3856 KB Output is correct
10 Correct 6 ms 3856 KB Output is correct
11 Correct 5 ms 3856 KB Output is correct
12 Correct 5 ms 3856 KB Output is correct
13 Correct 6 ms 3856 KB Output is correct
14 Correct 5 ms 3856 KB Output is correct
15 Correct 6 ms 3856 KB Output is correct
16 Correct 6 ms 3856 KB Output is correct
17 Correct 10 ms 4068 KB Output is correct
18 Correct 9 ms 4068 KB Output is correct
19 Correct 7 ms 4068 KB Output is correct
20 Correct 6 ms 4068 KB Output is correct
21 Correct 6 ms 4068 KB Output is correct
22 Correct 7 ms 4148 KB Output is correct
23 Correct 9 ms 4148 KB Output is correct
24 Correct 11 ms 4148 KB Output is correct
25 Correct 8 ms 4148 KB Output is correct
26 Correct 8 ms 4148 KB Output is correct
27 Correct 7 ms 4148 KB Output is correct
28 Correct 9 ms 4148 KB Output is correct
29 Correct 9 ms 4156 KB Output is correct
30 Correct 8 ms 4156 KB Output is correct
31 Correct 7 ms 4236 KB Output is correct
32 Correct 7 ms 4236 KB Output is correct
33 Correct 767 ms 12196 KB Output is correct
34 Correct 237 ms 13276 KB Output is correct
35 Correct 609 ms 13276 KB Output is correct
36 Correct 1020 ms 21340 KB Output is correct
37 Correct 35 ms 21340 KB Output is correct
38 Correct 1229 ms 24392 KB Output is correct
39 Correct 1434 ms 26412 KB Output is correct
40 Correct 1258 ms 28356 KB Output is correct
41 Correct 1119 ms 30296 KB Output is correct
42 Correct 1422 ms 32172 KB Output is correct
43 Correct 1506 ms 34132 KB Output is correct
44 Correct 1018 ms 36092 KB Output is correct
45 Correct 1247 ms 38088 KB Output is correct
46 Correct 1489 ms 39884 KB Output is correct
47 Correct 1192 ms 41792 KB Output is correct
48 Correct 361 ms 41792 KB Output is correct
49 Correct 458 ms 43724 KB Output is correct
50 Correct 127 ms 43724 KB Output is correct
51 Correct 182 ms 43724 KB Output is correct
52 Correct 58 ms 43724 KB Output is correct
53 Correct 582 ms 46232 KB Output is correct
54 Correct 409 ms 46232 KB Output is correct
55 Correct 1187 ms 47196 KB Output is correct
56 Correct 683 ms 47196 KB Output is correct
57 Correct 916 ms 50988 KB Output is correct
58 Correct 59 ms 50988 KB Output is correct
59 Correct 141 ms 50988 KB Output is correct
60 Correct 307 ms 53156 KB Output is correct
61 Correct 432 ms 55316 KB Output is correct
62 Correct 203 ms 55316 KB Output is correct
63 Correct 122 ms 55316 KB Output is correct
64 Correct 128 ms 55996 KB Output is correct
65 Correct 176 ms 63608 KB Output is correct
66 Correct 215 ms 63608 KB Output is correct
67 Correct 174 ms 63608 KB Output is correct
68 Correct 363 ms 66896 KB Output is correct
69 Correct 107 ms 66896 KB Output is correct
70 Correct 32 ms 66896 KB Output is correct
71 Correct 194 ms 66896 KB Output is correct
72 Correct 186 ms 67872 KB Output is correct
73 Correct 720 ms 74228 KB Output is correct
74 Correct 704 ms 74228 KB Output is correct
75 Correct 419 ms 77964 KB Output is correct
76 Correct 451 ms 78644 KB Output is correct
77 Correct 554 ms 78740 KB Output is correct