답안 #492978

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
492978 2021-12-09T21:33:46 Z ivan_tudor Cats or Dogs (JOI18_catdog) C++14
100 / 100
486 ms 60372 KB
#include "catdog.h"
#include<assert.h>
using namespace std;
const int N = 500005;
const int INF = 1e9;
int n;
int scores[2][N];
int curl[2][N];
struct SegmentTree{
  int ans[2][2];
  SegmentTree(){
    for(int i =0; i< 2; i++){
      for(int j = 0; j < 2; j++)
        ans[i][j] = INF;
    }
  }
};
SegmentTree NIL;
SegmentTree join(SegmentTree a, SegmentTree b){
  SegmentTree rsp;
  for(int i = 0; i <= 1; i++){
    for(int j = 0; j <= 1; j++){
      for(int midl = 0; midl <=1 ;midl++){
        for(int midr = 0; midr <=1; midr++){
          rsp.ans[i][j] = min(rsp.ans[i][j], a.ans[i][midl] + b.ans[midr][j] + (midl!=midr));
        }
      }
    }
  }
  return rsp;
}
SegmentTree build(int nod, int animal){
  SegmentTree rsp;
  for(int i =0; i <= 1; i++){
    if(animal == i || animal == 2)
      rsp.ans[i][i] = 0;
    rsp.ans[i][i] += scores[i][nod];
  }
  return rsp;
}
SegmentTree aint[4*N];
int getminscore(SegmentTree chain, int pet){
  int ans = INF;
  for(int i = 0; i <= 1; i++){
    ans = min(ans, chain.ans[pet][i]);
    ans = min(ans, chain.ans[pet^1][i] + 1);
  }
  return ans;
}
void update(int nod, int l, int r, int upoz, int utype){
  if(l > upoz || r < upoz)
    return;
  if(l == r){
    aint[nod] = build(upoz, utype);
    return;
  }
  int mid = (l + r)/2;
  update(2*nod, l, mid, upoz, utype);
  update(2*nod + 1, mid +1, r, upoz, utype);
  aint[nod] = join(aint[2*nod], aint[2*nod + 1]);
}
SegmentTree query(int nod, int l, int r, int ql, int qr){
  if(ql <= l && r <= qr)
    return aint[nod];
  int mid = (l + r)/2;
  SegmentTree rsp;
  if(ql <= mid && mid < qr)
    rsp = join(query(2*nod, l, mid, ql, qr), query(2*nod + 1, mid + 1, r, ql, qr));
  else if(qr <= mid)
    rsp = query(2*nod, l, mid, ql, qr);
  else if(ql > mid)
    rsp = query(2*nod + 1, mid + 1, r, ql, qr);
  else
    assert(0);
  return rsp;
}

vector<int> gr[N];
int par[N];
int sz[N];
int id[N];
int head[N];
int low[N];
int tp[N];
void dfs_init(int nod, int dad){
  par[nod] = dad;
  sz[nod] = 1;
  for(auto x:gr[nod]){
    if(x == dad)
      continue;
    dfs_init(x, nod);
    sz[nod] += sz[x];
  }
}
void dfs_heavy(int nod, int dad,int &idd, int chead){
  head[nod] = chead;
  low[chead] = nod;
  id[nod] = idd++;
  int mson = -1;
  for(auto x:gr[nod]){
    if(x == dad)
      continue;
    if(mson == -1 || sz[x] > sz[mson])
      mson = x;
  }
  if(mson == -1)
    return;
  dfs_heavy(mson, nod, idd, chead);
  for(auto x:gr[nod]){
    if(x == dad || x == mson)
      continue;
    dfs_heavy(x, nod, idd, x);
  }
}
SegmentTree update_heavy(int nod, int type){
  tp[nod] = type;
  int hd = head[nod];
  update(1, 1, n, id[nod], type);
  SegmentTree chain = query(1, 1, n, id[hd], id[low[hd]]);
  if(hd == 1)
    return chain;
  for(int i = 0; i<=1; i++){
    scores[i][id[par[hd]]] -= curl[i][id[hd]];
    curl[i][id[hd]] = getminscore(chain, i);
    scores[i][id[par[hd]]] += curl[i][id[hd]];
  }
  return update_heavy(par[hd], tp[par[hd]]);
}

void build_aint(int nod, int l, int r){
  if(l == r){
    aint[nod] = build(nod, tp[l]);
    return;
  }
  int mid = (l + r)/2;
  build_aint(2*nod, l, mid);
  build_aint(2*nod + 1, mid + 1, r);
  aint[nod] = join(aint[2*nod], aint[2*nod + 1]);
}

void initialize(int N, std::vector<int> A, std::vector<int> B){
  NIL = build(0, 2);
  n = N;
  for(int i = 0; i < N - 1; i++){
    gr[A[i]].push_back(B[i]);
    gr[B[i]].push_back(A[i]);
  }
  for(int i = 1; i<=n; i++)
    tp[i] = 2;
  build_aint(1,  1,  n);
  dfs_init(1, 0);
  int cnt = 1;
  dfs_heavy(1, 0, cnt, 1);
};
int cat(int v){
  SegmentTree rsp = update_heavy(v, 0);
  return min(getminscore(rsp, 0), getminscore(rsp, 1));
}
int dog(int v){
  SegmentTree rsp = update_heavy(v, 1);
  return min(getminscore(rsp, 0), getminscore(rsp, 1));
}
int neighbor(int v){
  SegmentTree rsp = update_heavy(v, 2);
  return min(getminscore(rsp, 0), getminscore(rsp, 1));
}
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 43340 KB Output is correct
2 Correct 18 ms 43340 KB Output is correct
3 Correct 22 ms 43412 KB Output is correct
4 Correct 18 ms 43340 KB Output is correct
5 Correct 19 ms 43408 KB Output is correct
6 Correct 18 ms 43340 KB Output is correct
7 Correct 21 ms 43388 KB Output is correct
8 Correct 19 ms 43340 KB Output is correct
9 Correct 19 ms 43384 KB Output is correct
10 Correct 19 ms 43340 KB Output is correct
11 Correct 19 ms 43340 KB Output is correct
12 Correct 20 ms 43420 KB Output is correct
13 Correct 22 ms 43324 KB Output is correct
14 Correct 21 ms 43408 KB Output is correct
15 Correct 18 ms 43308 KB Output is correct
16 Correct 19 ms 43340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 43340 KB Output is correct
2 Correct 18 ms 43340 KB Output is correct
3 Correct 22 ms 43412 KB Output is correct
4 Correct 18 ms 43340 KB Output is correct
5 Correct 19 ms 43408 KB Output is correct
6 Correct 18 ms 43340 KB Output is correct
7 Correct 21 ms 43388 KB Output is correct
8 Correct 19 ms 43340 KB Output is correct
9 Correct 19 ms 43384 KB Output is correct
10 Correct 19 ms 43340 KB Output is correct
11 Correct 19 ms 43340 KB Output is correct
12 Correct 20 ms 43420 KB Output is correct
13 Correct 22 ms 43324 KB Output is correct
14 Correct 21 ms 43408 KB Output is correct
15 Correct 18 ms 43308 KB Output is correct
16 Correct 19 ms 43340 KB Output is correct
17 Correct 20 ms 43468 KB Output is correct
18 Correct 21 ms 43460 KB Output is correct
19 Correct 20 ms 43468 KB Output is correct
20 Correct 19 ms 43340 KB Output is correct
21 Correct 18 ms 43440 KB Output is correct
22 Correct 19 ms 43340 KB Output is correct
23 Correct 21 ms 43384 KB Output is correct
24 Correct 20 ms 43468 KB Output is correct
25 Correct 20 ms 43452 KB Output is correct
26 Correct 19 ms 43344 KB Output is correct
27 Correct 19 ms 43304 KB Output is correct
28 Correct 18 ms 43432 KB Output is correct
29 Correct 19 ms 43456 KB Output is correct
30 Correct 19 ms 43316 KB Output is correct
31 Correct 21 ms 43368 KB Output is correct
32 Correct 20 ms 43340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 43340 KB Output is correct
2 Correct 18 ms 43340 KB Output is correct
3 Correct 22 ms 43412 KB Output is correct
4 Correct 18 ms 43340 KB Output is correct
5 Correct 19 ms 43408 KB Output is correct
6 Correct 18 ms 43340 KB Output is correct
7 Correct 21 ms 43388 KB Output is correct
8 Correct 19 ms 43340 KB Output is correct
9 Correct 19 ms 43384 KB Output is correct
10 Correct 19 ms 43340 KB Output is correct
11 Correct 19 ms 43340 KB Output is correct
12 Correct 20 ms 43420 KB Output is correct
13 Correct 22 ms 43324 KB Output is correct
14 Correct 21 ms 43408 KB Output is correct
15 Correct 18 ms 43308 KB Output is correct
16 Correct 19 ms 43340 KB Output is correct
17 Correct 20 ms 43468 KB Output is correct
18 Correct 21 ms 43460 KB Output is correct
19 Correct 20 ms 43468 KB Output is correct
20 Correct 19 ms 43340 KB Output is correct
21 Correct 18 ms 43440 KB Output is correct
22 Correct 19 ms 43340 KB Output is correct
23 Correct 21 ms 43384 KB Output is correct
24 Correct 20 ms 43468 KB Output is correct
25 Correct 20 ms 43452 KB Output is correct
26 Correct 19 ms 43344 KB Output is correct
27 Correct 19 ms 43304 KB Output is correct
28 Correct 18 ms 43432 KB Output is correct
29 Correct 19 ms 43456 KB Output is correct
30 Correct 19 ms 43316 KB Output is correct
31 Correct 21 ms 43368 KB Output is correct
32 Correct 20 ms 43340 KB Output is correct
33 Correct 257 ms 48516 KB Output is correct
34 Correct 106 ms 49576 KB Output is correct
35 Correct 241 ms 48360 KB Output is correct
36 Correct 417 ms 53848 KB Output is correct
37 Correct 36 ms 46276 KB Output is correct
38 Correct 464 ms 54896 KB Output is correct
39 Correct 486 ms 54896 KB Output is correct
40 Correct 406 ms 54848 KB Output is correct
41 Correct 463 ms 54900 KB Output is correct
42 Correct 453 ms 54912 KB Output is correct
43 Correct 434 ms 54964 KB Output is correct
44 Correct 390 ms 54908 KB Output is correct
45 Correct 390 ms 54904 KB Output is correct
46 Correct 414 ms 54900 KB Output is correct
47 Correct 413 ms 54988 KB Output is correct
48 Correct 132 ms 51100 KB Output is correct
49 Correct 143 ms 52844 KB Output is correct
50 Correct 61 ms 45636 KB Output is correct
51 Correct 69 ms 47172 KB Output is correct
52 Correct 40 ms 45304 KB Output is correct
53 Correct 184 ms 53196 KB Output is correct
54 Correct 136 ms 47996 KB Output is correct
55 Correct 331 ms 51900 KB Output is correct
56 Correct 206 ms 48836 KB Output is correct
57 Correct 280 ms 53072 KB Output is correct
58 Correct 41 ms 47040 KB Output is correct
59 Correct 66 ms 46884 KB Output is correct
60 Correct 125 ms 51960 KB Output is correct
61 Correct 135 ms 52420 KB Output is correct
62 Correct 92 ms 50204 KB Output is correct
63 Correct 58 ms 50372 KB Output is correct
64 Correct 63 ms 52036 KB Output is correct
65 Correct 74 ms 56776 KB Output is correct
66 Correct 77 ms 47008 KB Output is correct
67 Correct 75 ms 52936 KB Output is correct
68 Correct 135 ms 57168 KB Output is correct
69 Correct 48 ms 44880 KB Output is correct
70 Correct 30 ms 43620 KB Output is correct
71 Correct 68 ms 50008 KB Output is correct
72 Correct 91 ms 55368 KB Output is correct
73 Correct 199 ms 60372 KB Output is correct
74 Correct 228 ms 56920 KB Output is correct
75 Correct 155 ms 60364 KB Output is correct
76 Correct 150 ms 59120 KB Output is correct
77 Correct 232 ms 57184 KB Output is correct