Submission #536508

# Submission time Handle Problem Language Result Execution time Memory
536508 2022-03-13T13:09:54 Z cadmiumsky Cats or Dogs (JOI18_catdog) C++14
100 / 100
551 ms 33292 KB
#include <bits/stdc++.h>
#include "catdog.h"
using namespace std;
const int nmax = 1e5 + 5, qmax = 1e5 + 5, inf = 1e8 + 8;
#define red first
#define blue second
#define pii pair<int,int>
// -1 -- insipid, 0 -- rosu, 1 -- albastru 
int n;
vector<int> g[nmax];
namespace HLD {
  namespace AINT {
    struct Mat {
      int dp[2][2];
      #warning poate pica memset
      Mat() {dp[0][0] = dp[1][1] = 0, dp[0][1] = dp[1][0] = inf;} 
      Mat(int _00, int _11) {
        dp[0][0] = _00;
        dp[1][1] = _11;
        dp[0][1] = inf;
        dp[1][0] = inf;
      }
      Mat(int _00, int _01, int _10, int _11) {
        dp[0][0] = _00;
        dp[0][1] = _01;
        dp[1][0] = _10;
        dp[1][1] = _11;
      }
      Mat operator + (const Mat& x) const {
        Mat rez;
        for(int l = 0; l < 2; l++)
          for(int r = 0; r < 2; r++) {
            rez.dp[l][r] = inf;
            for(int i = 0; i < 2; i++)
              for(int j = 0; j < 2; j++)
                rez.dp[l][r] = min(rez.dp[l][r], dp[l][i] + x.dp[j][r] + (i ^ j));
          }
        return rez;
      }
    };
    Mat aint[nmax * 4];
    void upd(int poz, int l, int r, int node = 1, int cl = 1, int cr = n) {
      if(cl == cr) {
        aint[node] = Mat(l, r);
        return;
      }
      int mid = cl + cr >> 1;
      if(poz <= mid) 
        upd(poz, l, r, 2 * node, cl, mid);
      else
        upd(poz, l, r, 2 * node + 1, mid + 1, cr);
      aint[node] = aint[2 * node] + aint[2 * node + 1];
      //cout << '\t' << cl << ' ' << cr <<'\n';
      auto rez = aint[node];
       //cout << "\t>  " << rez.dp[0][0] << ' ' << rez.dp[0][1] << "\n\t>  " << rez.dp[1][0] << ' ' << rez.dp[1][1] << '\n';
     
      return;
    }
    Mat GetQuery(int l, int r, int node = 1, int cl = 1, int cr = n) {
      if(r < cl || cr < l)
        return Mat();
      if(l <= cl && cr <= r)
        return aint[node];
      int ok = 0, mid = cl + cr >> 1;
      Mat rez;
      if(l <= mid)
        rez = GetQuery(l, r, 2 * node ,cl, mid), ok = 1;
      if(mid < r) 
        if(ok)
          rez = rez + GetQuery(l, r, 2 * node + 1, mid + 1, cr);
        else
          rez = GetQuery(l, r, 2 * node + 1, mid + 1, cr);
      return rez;
    }
    pii query(int l, int r) {
      Mat rez = GetQuery(l, r);
      //cout << l << ' ' << r << '\n';
      //cout << ">  " << rez.dp[0][0] << ' ' << rez.dp[0][1] << "\n>  " << rez.dp[1][0] << ' ' << rez.dp[1][1] << '\n';
      return {min(rez.dp[0][0], rez.dp[0][1]),
              min(rez.dp[1][0], rez.dp[1][1])};
    }
  }
  int p[nmax], preord[nmax], pin[nmax], lch[nmax], rch[nmax], atrch[nmax], inp = 1, inch = 0;
  int area[nmax], color[nmax];
  pii sons[nmax], dp[nmax], total[nmax];
  
  void initarea(int node, int f = 1) {
    area[node] = 1;
    color[node] = -1;
    atrch[node] = rch[node - 1] = 0;
    lch[node - 1] = inf; 
    p[node] = f;
    for(auto x : g[node]) {
      if(x != f)
        initarea(x, node), area[node] += area[x];
    }
  }
  void initdfs(int node, int f = 1) {
    atrch[node] = inch;
    lch[inch] = min(inp, lch[inch]); 
    rch[inch] = max(inp, rch[inch]);
    pin[node] = inp, preord[inp++] = node;
    //cerr << node << ' ' << pin[node] << ' ' << inch << '\t' << lch[inch] << ' ' << rch[inch]<< '\n';
    if(node != f && g[node].size() == 1)
      return;
    vector<int> son;
    for(auto x : g[node]) {
      if(x == f)
        continue;
      son.push_back(x);
    }
    sort(son.begin(), son.end(), [&](auto x, auto y){return area[x] < area[y];});
    initdfs(son.back(), node);
    son.pop_back();
    for(auto x : son) {
      inch++;
      initdfs(x, node);
    }
  }
  int fch(int ch) {
    return preord[lch[ch]];
  }
  int gotop(int u) {
    int ch = atrch[u];
    dp[u] = sons[u];
    if(color[u] == 0)
      dp[u].blue = inf;
    else if(color[u] == 1)
      dp[u].red = inf;
    //cerr << u << ' ' << dp[u].red << ' ' << dp[u].blue << '\n';
    AINT::upd(pin[u], dp[u].red, dp[u].blue);
    pii rez = AINT::query(lch[ch], rch[ch]);
    //cerr << rez.red << ' ' << rez.blue << '\n';
    //cerr << '\n';
    if(ch == 0)
      return min(rez.red, rez.blue);
    u = fch(ch);
    int f = p[u];
    sons[f].red -= min(total[u].red, total[u].blue + 1), sons[f].blue -= min(total[u].red + 1, total[u].blue);
    sons[f].red += min(rez.red, rez.blue + 1), sons[f].blue += min(rez.red + 1, rez.blue);
    total[u] = rez;
    return gotop(f);
  }
  int setcolor(int u, int x) {
    color[u] = x;
    int temp =  gotop(u);
    //cerr << '\n';
    return temp;
  }
}


void initialize(int N, std::vector<int> A, std::vector<int> B) {
  n = N;
  for(int i = 0; i < N - 1; i++)
    g[A[i]].push_back(B[i]),
    g[B[i]].push_back(A[i]);
  HLD::initarea(1);
  HLD::initdfs(1);
}

int cat(int v) {
  return HLD::setcolor(v, 0);
}

int dog(int v) {
  return HLD::setcolor(v, 1);
}

int neighbor(int v) {
  return HLD::setcolor(v, -1);
}

Compilation message

catdog.cpp:15:8: warning: #warning poate pica memset [-Wcpp]
   15 |       #warning poate pica memset
      |        ^~~~~~~
catdog.cpp: In function 'void HLD::AINT::upd(int, int, int, int, int, int)':
catdog.cpp:47:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   47 |       int mid = cl + cr >> 1;
      |                 ~~~^~~~
catdog.cpp:54:12: warning: variable 'rez' set but not used [-Wunused-but-set-variable]
   54 |       auto rez = aint[node];
      |            ^~~
catdog.cpp: In function 'HLD::AINT::Mat HLD::AINT::GetQuery(int, int, int, int, int)':
catdog.cpp:64:28: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   64 |       int ok = 0, mid = cl + cr >> 1;
      |                         ~~~^~~~
catdog.cpp:68:9: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
   68 |       if(mid < r)
      |         ^
# Verdict Execution time Memory Grader output
1 Correct 5 ms 8952 KB Output is correct
2 Correct 5 ms 8916 KB Output is correct
3 Correct 6 ms 8924 KB Output is correct
4 Correct 5 ms 8924 KB Output is correct
5 Correct 5 ms 8872 KB Output is correct
6 Correct 5 ms 8916 KB Output is correct
7 Correct 5 ms 8924 KB Output is correct
8 Correct 4 ms 8916 KB Output is correct
9 Correct 4 ms 8924 KB Output is correct
10 Correct 6 ms 8916 KB Output is correct
11 Correct 5 ms 8916 KB Output is correct
12 Correct 5 ms 8916 KB Output is correct
13 Correct 4 ms 8924 KB Output is correct
14 Correct 5 ms 8916 KB Output is correct
15 Correct 5 ms 8916 KB Output is correct
16 Correct 5 ms 8916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 8952 KB Output is correct
2 Correct 5 ms 8916 KB Output is correct
3 Correct 6 ms 8924 KB Output is correct
4 Correct 5 ms 8924 KB Output is correct
5 Correct 5 ms 8872 KB Output is correct
6 Correct 5 ms 8916 KB Output is correct
7 Correct 5 ms 8924 KB Output is correct
8 Correct 4 ms 8916 KB Output is correct
9 Correct 4 ms 8924 KB Output is correct
10 Correct 6 ms 8916 KB Output is correct
11 Correct 5 ms 8916 KB Output is correct
12 Correct 5 ms 8916 KB Output is correct
13 Correct 4 ms 8924 KB Output is correct
14 Correct 5 ms 8916 KB Output is correct
15 Correct 5 ms 8916 KB Output is correct
16 Correct 5 ms 8916 KB Output is correct
17 Correct 7 ms 9020 KB Output is correct
18 Correct 8 ms 9000 KB Output is correct
19 Correct 5 ms 9044 KB Output is correct
20 Correct 5 ms 8924 KB Output is correct
21 Correct 5 ms 8916 KB Output is correct
22 Correct 5 ms 8916 KB Output is correct
23 Correct 6 ms 9056 KB Output is correct
24 Correct 6 ms 9044 KB Output is correct
25 Correct 6 ms 8924 KB Output is correct
26 Correct 6 ms 8916 KB Output is correct
27 Correct 5 ms 8916 KB Output is correct
28 Correct 5 ms 9056 KB Output is correct
29 Correct 6 ms 9056 KB Output is correct
30 Correct 7 ms 8916 KB Output is correct
31 Correct 6 ms 9044 KB Output is correct
32 Correct 6 ms 8924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 8952 KB Output is correct
2 Correct 5 ms 8916 KB Output is correct
3 Correct 6 ms 8924 KB Output is correct
4 Correct 5 ms 8924 KB Output is correct
5 Correct 5 ms 8872 KB Output is correct
6 Correct 5 ms 8916 KB Output is correct
7 Correct 5 ms 8924 KB Output is correct
8 Correct 4 ms 8916 KB Output is correct
9 Correct 4 ms 8924 KB Output is correct
10 Correct 6 ms 8916 KB Output is correct
11 Correct 5 ms 8916 KB Output is correct
12 Correct 5 ms 8916 KB Output is correct
13 Correct 4 ms 8924 KB Output is correct
14 Correct 5 ms 8916 KB Output is correct
15 Correct 5 ms 8916 KB Output is correct
16 Correct 5 ms 8916 KB Output is correct
17 Correct 7 ms 9020 KB Output is correct
18 Correct 8 ms 9000 KB Output is correct
19 Correct 5 ms 9044 KB Output is correct
20 Correct 5 ms 8924 KB Output is correct
21 Correct 5 ms 8916 KB Output is correct
22 Correct 5 ms 8916 KB Output is correct
23 Correct 6 ms 9056 KB Output is correct
24 Correct 6 ms 9044 KB Output is correct
25 Correct 6 ms 8924 KB Output is correct
26 Correct 6 ms 8916 KB Output is correct
27 Correct 5 ms 8916 KB Output is correct
28 Correct 5 ms 9056 KB Output is correct
29 Correct 6 ms 9056 KB Output is correct
30 Correct 7 ms 8916 KB Output is correct
31 Correct 6 ms 9044 KB Output is correct
32 Correct 6 ms 8924 KB Output is correct
33 Correct 229 ms 15948 KB Output is correct
34 Correct 100 ms 16124 KB Output is correct
35 Correct 262 ms 14440 KB Output is correct
36 Correct 398 ms 20796 KB Output is correct
37 Correct 25 ms 12372 KB Output is correct
38 Correct 463 ms 22108 KB Output is correct
39 Correct 475 ms 21988 KB Output is correct
40 Correct 483 ms 21996 KB Output is correct
41 Correct 551 ms 22100 KB Output is correct
42 Correct 431 ms 22008 KB Output is correct
43 Correct 459 ms 21996 KB Output is correct
44 Correct 491 ms 22056 KB Output is correct
45 Correct 456 ms 22008 KB Output is correct
46 Correct 438 ms 21996 KB Output is correct
47 Correct 482 ms 21932 KB Output is correct
48 Correct 159 ms 17824 KB Output is correct
49 Correct 136 ms 19836 KB Output is correct
50 Correct 55 ms 11660 KB Output is correct
51 Correct 72 ms 13348 KB Output is correct
52 Correct 27 ms 11300 KB Output is correct
53 Correct 176 ms 20304 KB Output is correct
54 Correct 125 ms 14212 KB Output is correct
55 Correct 376 ms 18368 KB Output is correct
56 Correct 203 ms 15108 KB Output is correct
57 Correct 341 ms 19984 KB Output is correct
58 Correct 27 ms 13404 KB Output is correct
59 Correct 55 ms 13128 KB Output is correct
60 Correct 142 ms 18692 KB Output is correct
61 Correct 152 ms 19264 KB Output is correct
62 Correct 79 ms 16824 KB Output is correct
63 Correct 44 ms 18552 KB Output is correct
64 Correct 57 ms 21324 KB Output is correct
65 Correct 92 ms 27912 KB Output is correct
66 Correct 66 ms 13312 KB Output is correct
67 Correct 69 ms 22480 KB Output is correct
68 Correct 144 ms 27828 KB Output is correct
69 Correct 41 ms 10700 KB Output is correct
70 Correct 11 ms 9172 KB Output is correct
71 Correct 59 ms 18272 KB Output is correct
72 Correct 82 ms 26088 KB Output is correct
73 Correct 220 ms 33292 KB Output is correct
74 Correct 256 ms 26828 KB Output is correct
75 Correct 153 ms 33104 KB Output is correct
76 Correct 151 ms 30808 KB Output is correct
77 Correct 215 ms 27488 KB Output is correct