Submission #421330

# Submission time Handle Problem Language Result Execution time Memory
421330 2021-06-09T04:06:14 Z 송준혁(#7507) Cats or Dogs (JOI18_catdog) C++17
100 / 100
919 ms 26024 KB
#include "catdog.h"
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define lb lower_bound
#define MOD 1000000007
#define INF (1ll<<62)
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;

int N, Q;
int A[101010], C[101010], D[101010];
vector<int> adj[101010];
int I[101010], Pa[101010], num;
int L[101010], R[101010];
int P[101010], W[101010];

struct Node{
  int cc, cd, dc, dd;
  Node(){cc=dd=0, cd=dc=1;}
  void trm(){
    cc = min(cc, cd+1), cc = min(cc, dc+1), cc = min(cc, dd+2);
    cd = min(cd, cc+1), cd = min(cd, dd+1), cd = min(cd, dc+2);
    dc = min(dc, cc+1), dc = min(dc, dd+1), dc = min(dc, cd+2);
    dd = min(dd, cd+1), dd = min(dd, dc+1), dd = min(dd, cc+2);
  }
  Node operator+(const Node &r)const{
    Node ret;
    ret.cc = min(cc+r.cc, cd+r.dc);
    ret.cd = min(cc+r.cd, cd+r.dd);
    ret.dc = min(dc+r.cc, dd+r.dc);
    ret.dd = min(dc+r.cd, dd+r.dd);
    ret.trm();
    return ret;
  }
} T[404040];

void dfs(int u, int p){
  W[u] = 1, Pa[u] = p;
  for (int v : adj[u]){
    if (v == p) continue;
    dfs(v, u), W[u]+=W[v];
  }
  sort(adj[u].begin(), adj[u].end(), [&](int a, int b){
    if (a == p) return false;
    if (b == p) return true;
    return W[a] > W[b];
  });
  if (u>1) adj[u].pop_back();
}

void hld(int u, int r){
  I[u] = ++num, L[I[u]] = I[r], R[I[r]] = I[u];
  if (!adj[u].size()) return;
  hld(adj[u][0], r);
  for (int i=1; i<adj[u].size(); i++) hld(adj[u][i], adj[u][i]);
}

void upd(int id, int s, int e, int t){
  if (e < t || t < s) return;
  if (s == e){
    T[id].cc=T[id].cd=T[id].dc=T[id].dd=N;
    if (A[t] != 1) T[id].dd=D[t];
    if (A[t] != 2) T[id].cc=C[t];
    T[id].trm();
    return;
  }
  int m=s+e>>1;
  upd(id*2, s, m, t), upd(id*2+1, m+1, e, t);
  T[id] = T[id*2] + T[id*2+1];
}

Node sum(int id, int s, int e, int ts, int te){
  if (e < ts || te < s) return Node();
  if (ts <= s && e <= te) return T[id];
  int m=s+e>>1;
  return sum(id*2, s, m, ts, te)+sum(id*2+1, m+1, e, ts, te);
}

void initialize(int _N, std::vector<int> _A, std::vector<int> _B) {
  N = _N;
  for (int i=0; i<N-1; i++) adj[_A[i]].pb(_B[i]), adj[_B[i]].pb(_A[i]);
  dfs(1, 0), hld(1, 1);
  for (int i=1; i<=N; i++) P[I[i]] = I[Pa[i]];
}

void update(int u){
  Node t;
  while (u){
    t = sum(1, 1, N, L[u], R[L[u]]);
    C[P[L[u]]] -= min(t.cc, t.cd);
    D[P[L[u]]] -= min(t.dc, t.dd);
    upd(1, 1, N, u);
    t = sum(1, 1, N, L[u], R[L[u]]);
    C[P[L[u]]] += min(t.cc, t.cd);
    D[P[L[u]]] += min(t.dc, t.dd);
    u = P[L[u]];
  }
}

int cat(int u) {
  u = I[u], A[u] = 1;
  update(u);
  return min(C[0], D[0]);
}

int dog(int u) {
  u = I[u], A[u] = 2;
  update(u);
  return min(C[0], D[0]);
}

int neighbor(int u) {
  u = I[u], A[u] = 3;
  update(u);
  return min(C[0], D[0]);
}

Compilation message

catdog.cpp: In function 'void hld(int, int)':
catdog.cpp:58:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |   for (int i=1; i<adj[u].size(); i++) hld(adj[u][i], adj[u][i]);
      |                 ~^~~~~~~~~~~~~~
catdog.cpp: In function 'void upd(int, int, int, int)':
catdog.cpp:70:10: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   70 |   int m=s+e>>1;
      |         ~^~
catdog.cpp: In function 'Node sum(int, int, int, int, int)':
catdog.cpp:78:10: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   78 |   int m=s+e>>1;
      |         ~^~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9000 KB Output is correct
2 Correct 5 ms 8980 KB Output is correct
3 Correct 6 ms 9036 KB Output is correct
4 Correct 5 ms 9036 KB Output is correct
5 Correct 5 ms 8996 KB Output is correct
6 Correct 6 ms 9036 KB Output is correct
7 Correct 6 ms 9036 KB Output is correct
8 Correct 6 ms 9036 KB Output is correct
9 Correct 6 ms 8908 KB Output is correct
10 Correct 5 ms 9036 KB Output is correct
11 Correct 5 ms 9036 KB Output is correct
12 Correct 5 ms 9036 KB Output is correct
13 Correct 5 ms 9000 KB Output is correct
14 Correct 5 ms 9012 KB Output is correct
15 Correct 6 ms 8908 KB Output is correct
16 Correct 7 ms 9036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9000 KB Output is correct
2 Correct 5 ms 8980 KB Output is correct
3 Correct 6 ms 9036 KB Output is correct
4 Correct 5 ms 9036 KB Output is correct
5 Correct 5 ms 8996 KB Output is correct
6 Correct 6 ms 9036 KB Output is correct
7 Correct 6 ms 9036 KB Output is correct
8 Correct 6 ms 9036 KB Output is correct
9 Correct 6 ms 8908 KB Output is correct
10 Correct 5 ms 9036 KB Output is correct
11 Correct 5 ms 9036 KB Output is correct
12 Correct 5 ms 9036 KB Output is correct
13 Correct 5 ms 9000 KB Output is correct
14 Correct 5 ms 9012 KB Output is correct
15 Correct 6 ms 8908 KB Output is correct
16 Correct 7 ms 9036 KB Output is correct
17 Correct 8 ms 9036 KB Output is correct
18 Correct 8 ms 9036 KB Output is correct
19 Correct 6 ms 9036 KB Output is correct
20 Correct 5 ms 9036 KB Output is correct
21 Correct 6 ms 9036 KB Output is correct
22 Correct 7 ms 8964 KB Output is correct
23 Correct 8 ms 9036 KB Output is correct
24 Correct 8 ms 9008 KB Output is correct
25 Correct 7 ms 9004 KB Output is correct
26 Correct 6 ms 9036 KB Output is correct
27 Correct 6 ms 9036 KB Output is correct
28 Correct 6 ms 9036 KB Output is correct
29 Correct 6 ms 9132 KB Output is correct
30 Correct 6 ms 9036 KB Output is correct
31 Correct 6 ms 9000 KB Output is correct
32 Correct 6 ms 9036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9000 KB Output is correct
2 Correct 5 ms 8980 KB Output is correct
3 Correct 6 ms 9036 KB Output is correct
4 Correct 5 ms 9036 KB Output is correct
5 Correct 5 ms 8996 KB Output is correct
6 Correct 6 ms 9036 KB Output is correct
7 Correct 6 ms 9036 KB Output is correct
8 Correct 6 ms 9036 KB Output is correct
9 Correct 6 ms 8908 KB Output is correct
10 Correct 5 ms 9036 KB Output is correct
11 Correct 5 ms 9036 KB Output is correct
12 Correct 5 ms 9036 KB Output is correct
13 Correct 5 ms 9000 KB Output is correct
14 Correct 5 ms 9012 KB Output is correct
15 Correct 6 ms 8908 KB Output is correct
16 Correct 7 ms 9036 KB Output is correct
17 Correct 8 ms 9036 KB Output is correct
18 Correct 8 ms 9036 KB Output is correct
19 Correct 6 ms 9036 KB Output is correct
20 Correct 5 ms 9036 KB Output is correct
21 Correct 6 ms 9036 KB Output is correct
22 Correct 7 ms 8964 KB Output is correct
23 Correct 8 ms 9036 KB Output is correct
24 Correct 8 ms 9008 KB Output is correct
25 Correct 7 ms 9004 KB Output is correct
26 Correct 6 ms 9036 KB Output is correct
27 Correct 6 ms 9036 KB Output is correct
28 Correct 6 ms 9036 KB Output is correct
29 Correct 6 ms 9132 KB Output is correct
30 Correct 6 ms 9036 KB Output is correct
31 Correct 6 ms 9000 KB Output is correct
32 Correct 6 ms 9036 KB Output is correct
33 Correct 495 ms 15052 KB Output is correct
34 Correct 137 ms 14916 KB Output is correct
35 Correct 468 ms 13872 KB Output is correct
36 Correct 771 ms 19124 KB Output is correct
37 Correct 22 ms 11852 KB Output is correct
38 Correct 830 ms 20136 KB Output is correct
39 Correct 903 ms 20136 KB Output is correct
40 Correct 847 ms 20140 KB Output is correct
41 Correct 919 ms 20136 KB Output is correct
42 Correct 741 ms 20144 KB Output is correct
43 Correct 793 ms 20128 KB Output is correct
44 Correct 721 ms 20156 KB Output is correct
45 Correct 733 ms 20140 KB Output is correct
46 Correct 750 ms 20136 KB Output is correct
47 Correct 773 ms 20096 KB Output is correct
48 Correct 229 ms 16440 KB Output is correct
49 Correct 283 ms 18200 KB Output is correct
50 Correct 88 ms 11192 KB Output is correct
51 Correct 100 ms 12656 KB Output is correct
52 Correct 48 ms 10868 KB Output is correct
53 Correct 340 ms 18404 KB Output is correct
54 Correct 264 ms 13452 KB Output is correct
55 Correct 633 ms 17124 KB Output is correct
56 Correct 408 ms 14308 KB Output is correct
57 Correct 543 ms 18316 KB Output is correct
58 Correct 49 ms 12636 KB Output is correct
59 Correct 104 ms 12412 KB Output is correct
60 Correct 234 ms 17220 KB Output is correct
61 Correct 224 ms 17712 KB Output is correct
62 Correct 130 ms 15660 KB Output is correct
63 Correct 59 ms 16068 KB Output is correct
64 Correct 61 ms 17660 KB Output is correct
65 Correct 79 ms 22368 KB Output is correct
66 Correct 118 ms 12704 KB Output is correct
67 Correct 89 ms 18604 KB Output is correct
68 Correct 181 ms 22836 KB Output is correct
69 Correct 55 ms 10516 KB Output is correct
70 Correct 14 ms 9300 KB Output is correct
71 Correct 79 ms 15556 KB Output is correct
72 Correct 110 ms 20960 KB Output is correct
73 Correct 308 ms 26024 KB Output is correct
74 Correct 355 ms 22452 KB Output is correct
75 Correct 216 ms 25920 KB Output is correct
76 Correct 197 ms 24624 KB Output is correct
77 Correct 388 ms 22840 KB Output is correct