Submission #501045

# Submission time Handle Problem Language Result Execution time Memory
501045 2022-01-02T09:41:02 Z erray Crossing (JOI21_crossing) C++17
100 / 100
457 ms 21736 KB
#undef DEBUG
// author: erray
#include <bits/stdc++.h>
#ifdef DEBUG
  #include "debug.h"
#else
  #define debug(...) void(37)
  #define here void(37)
#endif

using namespace std;

constexpr int B = 4;
constexpr int md0 = int(1e9) + 7;
constexpr int md1 = 999999937;
vector<int> pw0({1});
vector<int> pw1({1});
int pw(int b, int t) {
  assert(b == md0 || b == md1);
  auto& a = (b == md0 ? pw0 : pw1);
  while (int(a.size()) <= t) {
    a.push_back(int(a.back() * 1LL * B % b));
  }
  return a[t];
}

template<int md> struct Hash {
  int val = 0;
  int size = 0;
  Hash() { size = 0,  val = 0; }
  Hash operator+(const Hash& ot) {
    Hash res;
    res.val = int(((1LL * pw(md, size) * ot.val) + val) % md); 
    res.size = size + ot.size;
    return res; 
  }
  Hash operator *(int x) {
    Hash res;
    res.val = int(1LL * val * x % md);
    return res;
  }
};

template<int T> bool operator<(Hash<T> x, Hash<T> y) {
  return x.val < y.val;
}

struct Rhash {
  Hash<md0> h0;
  Hash<md1> h1;
  Rhash() : h0(), h1() { h0.size = h1.size = 0; } 
  Rhash(int x) { 
    h0.val = x;
    h1.val = x;
    h0.size = h1.size = 1;
  }
  Rhash operator+(const Rhash& ot) {
    Rhash res;
    res.h0 = h0 + ot.h0;
    res.h1 = h1 + ot.h1;
    return res;
  }
  Rhash operator*(const int& x) {
    Rhash res;
    res.h0 = h0 * x;
    res.h1 = h1 * x;
    return res;
  }
  Rhash& operator=(const Rhash& ot) {
    h0 = ot.h0;                             
    h1 = ot.h1;
    return *this;
  }
};

bool operator<(Rhash x, Rhash y) {
  return tuple{x.h0, x.h1, x.h0.size} < tuple{y.h0, y.h1, y.h0.size};  
}

/*
string to_string(Rhash x) {
  return to_string(tuple{x.h0.val, x.h1.val, x.h0.size});
}
*/

vector<Rhash> def;

struct SegTree {
  struct node {
    Rhash val;
    int tag = -1;
    void modify(int l, int r, int x) {
      int size = (r - l + 1);
      tag = x;
      val = (def[size] * x);
      val.h0.size = val.h1.size = size;
    }

    node operator+(const node& ot) {
      node res;
      res.val = val + ot.val;
      return res; 
    }
  };
  
  int n;
  vector<node> tree;
  #define init int mid = (l + r) >> 1, rv = v + ((mid - l + 1) << 1)
  void pull(int v, int rv) {
    tree[v] = tree[v + 1] + tree[rv];
  }

  void push(int v, int l, int r) {
    init;
    if (tree[v].tag != -1) {
      tree[v + 1].modify(l, mid, tree[v].tag);
      tree[rv].modify(mid + 1, r, tree[v].tag);
      tree[v].tag = -1;
    }
  }

  void build(int v, int l, int r) {
    if (l == r) {
      tree[v].val = Rhash(1);
      return;
    }
    init;
    build(v + 1, l, mid);
    build(rv, mid + 1, r);
    pull(v, rv);
  }
   
  SegTree(int _n) : n(_n) {
    tree.resize((n << 1) - 1);
    //build(0, 0, n - 1); 
  }

  void modify(int v, int l, int r, int ll, int rr, int x) {
    if (l >= ll && rr >= r) {
      tree[v].modify(l, r, x);
      debug(tree[v].val);
      return;
    }
    init;
    push(v, l, r);
    if (mid >= ll) {
      modify(v + 1, l, mid, ll, rr, x);
    }
    if (mid < rr) {
      modify(rv, mid + 1, r, ll, rr, x);
    }              
    debug(l, r, tree[v].val);
    pull(v, rv);
  }

  node get() {
    return tree[0];
  }
  void modify(int ll, int rr, int x) {
    assert(ll >= 0 && rr < n && ll <= rr && x > 0 && x < 4);   
    modify(0, 0, n - 1, ll, rr, x);
  } 
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int N;
  cin >> N;
  def.resize(N + 1);
  for (int i = 0; i < N; ++i) {
    def[i + 1] = def[i] + Rhash(1);
  }
  debug(def);
  array<string, 3> S;
  for (int i = 0; i < 3; ++i) {
    cin >> S[i];
  }

  int Q;
  string T;
  cin >> Q >> T;
  array<int, 256> cid = {};
  cid['J'] = 0;
  cid['O'] = 1;
  cid['I'] = 2;
  array<vector<int>, 3> vals;
  for (int i = 0; i < 3; ++i) {
    vals[i].resize(N);
    for (int j = 0; j < N; ++j) {
      vals[i][j] = cid[S[i][j]];
    }
  }
  debug(vals);

  vector<bool> vis(27);  
  queue<int> que;
  vector<Rhash> can;
  auto P = [&](int x) {
    if (vis[x]) {
      return;
    }
    vis[x] = true;
    que.push(x);
    vector<int> res(N, 0);
    for (int j = 0; j < 3; ++j) {
      int c = x % 3;
      for (int i = 0; i < N; ++i) {
        res[i] += c * vals[j][i];   
      }
      x /= 3;
    }

    for (auto& e : res) {
      e %= 3;
    }
    debug(res);
    Rhash add;
    for (int i = 0; i < N; ++i) {
      add = add + Rhash(1 + res[i]);
      debug(add);
    }
    can.push_back(add);
  };
  P(1);
  P(3);
  P(9);

  while (!que.empty()) {
    int v = que.front();
    que.pop();
    array<int, 3> me;
    int stv = v;
    for (int i = 0; i < 3; ++i) {
      me[i] = stv % 3;
      stv /= 3;
    }

    for (int add = 0; add < 3; ++add) {
      auto nw = me;
      (nw[add] += 1) %= 3;
      int res = 0;
      for (int j = 2; j >= 0; --j) {
        res *= 3;
        nw[j] = (3 - nw[j]) % 3;
        res += nw[j];
      }
      P(res);
    }
  }

  SegTree st(N);
  for (int i = 0; i < N; ++i) {
    debug(cid[T[i]] + 1);
    st.modify(i, i, cid[T[i]] + 1);  
  }
  
  debug(can);
  auto Can = [&](Rhash x) {
    for (auto e : can) {
      if (e.h0.val == x.h0.val && e.h1.val == x.h1.val) {
        return true;
      }  
    }
    return false;
  };
  
  debug(st.get().val);
  cout << (Can(st.get().val) ? "Yes" : "No") << '\n';
  while (Q--) {
    int L, R;
    char C;
    cin >> L >> R >> C;
    --L, --R;
    debug(L, R, cid[C]);
    st.modify(L, R, cid[C] + 1);
    debug(st.get().val);
    cout << (Can(st.get().val) ? "Yes" : "No") << '\n';
  }
}           	
# Verdict Execution time Memory Grader output
1 Correct 82 ms 2240 KB Output is correct
2 Correct 93 ms 2364 KB Output is correct
3 Correct 99 ms 2268 KB Output is correct
4 Correct 82 ms 2244 KB Output is correct
5 Correct 91 ms 2264 KB Output is correct
6 Correct 78 ms 2276 KB Output is correct
7 Correct 86 ms 2288 KB Output is correct
8 Correct 95 ms 2436 KB Output is correct
9 Correct 83 ms 2408 KB Output is correct
10 Correct 88 ms 2500 KB Output is correct
11 Correct 111 ms 2364 KB Output is correct
12 Correct 82 ms 2428 KB Output is correct
13 Correct 83 ms 2388 KB Output is correct
14 Correct 87 ms 2372 KB Output is correct
15 Correct 84 ms 2344 KB Output is correct
16 Correct 81 ms 2344 KB Output is correct
17 Correct 84 ms 2336 KB Output is correct
18 Correct 98 ms 2256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 82 ms 2240 KB Output is correct
2 Correct 93 ms 2364 KB Output is correct
3 Correct 99 ms 2268 KB Output is correct
4 Correct 82 ms 2244 KB Output is correct
5 Correct 91 ms 2264 KB Output is correct
6 Correct 78 ms 2276 KB Output is correct
7 Correct 86 ms 2288 KB Output is correct
8 Correct 95 ms 2436 KB Output is correct
9 Correct 83 ms 2408 KB Output is correct
10 Correct 88 ms 2500 KB Output is correct
11 Correct 111 ms 2364 KB Output is correct
12 Correct 82 ms 2428 KB Output is correct
13 Correct 83 ms 2388 KB Output is correct
14 Correct 87 ms 2372 KB Output is correct
15 Correct 84 ms 2344 KB Output is correct
16 Correct 81 ms 2344 KB Output is correct
17 Correct 84 ms 2336 KB Output is correct
18 Correct 98 ms 2256 KB Output is correct
19 Correct 401 ms 21444 KB Output is correct
20 Correct 418 ms 21320 KB Output is correct
21 Correct 300 ms 20212 KB Output is correct
22 Correct 278 ms 18452 KB Output is correct
23 Correct 174 ms 4288 KB Output is correct
24 Correct 146 ms 4152 KB Output is correct
25 Correct 318 ms 21396 KB Output is correct
26 Correct 325 ms 21392 KB Output is correct
27 Correct 372 ms 21384 KB Output is correct
28 Correct 379 ms 21376 KB Output is correct
29 Correct 349 ms 20904 KB Output is correct
30 Correct 152 ms 4132 KB Output is correct
31 Correct 369 ms 21408 KB Output is correct
32 Correct 333 ms 19820 KB Output is correct
33 Correct 154 ms 4160 KB Output is correct
34 Correct 363 ms 21392 KB Output is correct
35 Correct 277 ms 16656 KB Output is correct
36 Correct 155 ms 4096 KB Output is correct
37 Correct 142 ms 4036 KB Output is correct
38 Correct 386 ms 21216 KB Output is correct
39 Correct 290 ms 21432 KB Output is correct
40 Correct 270 ms 15232 KB Output is correct
41 Correct 457 ms 21736 KB Output is correct
42 Correct 206 ms 20836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 82 ms 2240 KB Output is correct
2 Correct 93 ms 2364 KB Output is correct
3 Correct 99 ms 2268 KB Output is correct
4 Correct 82 ms 2244 KB Output is correct
5 Correct 91 ms 2264 KB Output is correct
6 Correct 78 ms 2276 KB Output is correct
7 Correct 86 ms 2288 KB Output is correct
8 Correct 95 ms 2436 KB Output is correct
9 Correct 83 ms 2408 KB Output is correct
10 Correct 88 ms 2500 KB Output is correct
11 Correct 111 ms 2364 KB Output is correct
12 Correct 82 ms 2428 KB Output is correct
13 Correct 83 ms 2388 KB Output is correct
14 Correct 87 ms 2372 KB Output is correct
15 Correct 84 ms 2344 KB Output is correct
16 Correct 81 ms 2344 KB Output is correct
17 Correct 84 ms 2336 KB Output is correct
18 Correct 98 ms 2256 KB Output is correct
19 Correct 86 ms 2300 KB Output is correct
20 Correct 125 ms 2264 KB Output is correct
21 Correct 86 ms 2388 KB Output is correct
22 Correct 73 ms 2228 KB Output is correct
23 Correct 85 ms 2380 KB Output is correct
24 Correct 82 ms 2312 KB Output is correct
25 Correct 85 ms 2400 KB Output is correct
26 Correct 77 ms 2232 KB Output is correct
27 Correct 94 ms 2372 KB Output is correct
28 Correct 75 ms 2124 KB Output is correct
29 Correct 88 ms 2372 KB Output is correct
30 Correct 75 ms 2160 KB Output is correct
31 Correct 84 ms 2520 KB Output is correct
32 Correct 86 ms 2320 KB Output is correct
33 Correct 94 ms 2356 KB Output is correct
34 Correct 73 ms 2296 KB Output is correct
35 Correct 91 ms 2420 KB Output is correct
36 Correct 96 ms 2384 KB Output is correct
37 Correct 82 ms 2372 KB Output is correct
38 Correct 105 ms 2412 KB Output is correct
39 Correct 81 ms 2400 KB Output is correct
40 Correct 95 ms 2368 KB Output is correct
41 Correct 81 ms 2360 KB Output is correct
42 Correct 83 ms 2388 KB Output is correct
43 Correct 82 ms 2360 KB Output is correct
44 Correct 83 ms 2384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 82 ms 2240 KB Output is correct
2 Correct 93 ms 2364 KB Output is correct
3 Correct 99 ms 2268 KB Output is correct
4 Correct 82 ms 2244 KB Output is correct
5 Correct 91 ms 2264 KB Output is correct
6 Correct 78 ms 2276 KB Output is correct
7 Correct 86 ms 2288 KB Output is correct
8 Correct 95 ms 2436 KB Output is correct
9 Correct 83 ms 2408 KB Output is correct
10 Correct 88 ms 2500 KB Output is correct
11 Correct 111 ms 2364 KB Output is correct
12 Correct 82 ms 2428 KB Output is correct
13 Correct 83 ms 2388 KB Output is correct
14 Correct 87 ms 2372 KB Output is correct
15 Correct 84 ms 2344 KB Output is correct
16 Correct 81 ms 2344 KB Output is correct
17 Correct 84 ms 2336 KB Output is correct
18 Correct 98 ms 2256 KB Output is correct
19 Correct 401 ms 21444 KB Output is correct
20 Correct 418 ms 21320 KB Output is correct
21 Correct 300 ms 20212 KB Output is correct
22 Correct 278 ms 18452 KB Output is correct
23 Correct 174 ms 4288 KB Output is correct
24 Correct 146 ms 4152 KB Output is correct
25 Correct 318 ms 21396 KB Output is correct
26 Correct 325 ms 21392 KB Output is correct
27 Correct 372 ms 21384 KB Output is correct
28 Correct 379 ms 21376 KB Output is correct
29 Correct 349 ms 20904 KB Output is correct
30 Correct 152 ms 4132 KB Output is correct
31 Correct 369 ms 21408 KB Output is correct
32 Correct 333 ms 19820 KB Output is correct
33 Correct 154 ms 4160 KB Output is correct
34 Correct 363 ms 21392 KB Output is correct
35 Correct 277 ms 16656 KB Output is correct
36 Correct 155 ms 4096 KB Output is correct
37 Correct 142 ms 4036 KB Output is correct
38 Correct 386 ms 21216 KB Output is correct
39 Correct 290 ms 21432 KB Output is correct
40 Correct 270 ms 15232 KB Output is correct
41 Correct 457 ms 21736 KB Output is correct
42 Correct 206 ms 20836 KB Output is correct
43 Correct 86 ms 2300 KB Output is correct
44 Correct 125 ms 2264 KB Output is correct
45 Correct 86 ms 2388 KB Output is correct
46 Correct 73 ms 2228 KB Output is correct
47 Correct 85 ms 2380 KB Output is correct
48 Correct 82 ms 2312 KB Output is correct
49 Correct 85 ms 2400 KB Output is correct
50 Correct 77 ms 2232 KB Output is correct
51 Correct 94 ms 2372 KB Output is correct
52 Correct 75 ms 2124 KB Output is correct
53 Correct 88 ms 2372 KB Output is correct
54 Correct 75 ms 2160 KB Output is correct
55 Correct 84 ms 2520 KB Output is correct
56 Correct 86 ms 2320 KB Output is correct
57 Correct 94 ms 2356 KB Output is correct
58 Correct 73 ms 2296 KB Output is correct
59 Correct 91 ms 2420 KB Output is correct
60 Correct 96 ms 2384 KB Output is correct
61 Correct 82 ms 2372 KB Output is correct
62 Correct 105 ms 2412 KB Output is correct
63 Correct 81 ms 2400 KB Output is correct
64 Correct 95 ms 2368 KB Output is correct
65 Correct 81 ms 2360 KB Output is correct
66 Correct 83 ms 2388 KB Output is correct
67 Correct 82 ms 2360 KB Output is correct
68 Correct 83 ms 2384 KB Output is correct
69 Correct 419 ms 18272 KB Output is correct
70 Correct 455 ms 21440 KB Output is correct
71 Correct 172 ms 4148 KB Output is correct
72 Correct 147 ms 4152 KB Output is correct
73 Correct 141 ms 4152 KB Output is correct
74 Correct 296 ms 18280 KB Output is correct
75 Correct 140 ms 4140 KB Output is correct
76 Correct 372 ms 21464 KB Output is correct
77 Correct 332 ms 18380 KB Output is correct
78 Correct 143 ms 4148 KB Output is correct
79 Correct 182 ms 4120 KB Output is correct
80 Correct 260 ms 16068 KB Output is correct
81 Correct 168 ms 4096 KB Output is correct
82 Correct 330 ms 21412 KB Output is correct
83 Correct 298 ms 20176 KB Output is correct
84 Correct 141 ms 4072 KB Output is correct
85 Correct 137 ms 4092 KB Output is correct
86 Correct 319 ms 16888 KB Output is correct
87 Correct 350 ms 21500 KB Output is correct
88 Correct 166 ms 4136 KB Output is correct
89 Correct 310 ms 18904 KB Output is correct
90 Correct 342 ms 21400 KB Output is correct
91 Correct 167 ms 4076 KB Output is correct
92 Correct 280 ms 16624 KB Output is correct
93 Correct 160 ms 4012 KB Output is correct
94 Correct 139 ms 4040 KB Output is correct
95 Correct 172 ms 4204 KB Output is correct
96 Correct 361 ms 21268 KB Output is correct
97 Correct 298 ms 21464 KB Output is correct
98 Correct 250 ms 15284 KB Output is correct
99 Correct 439 ms 21604 KB Output is correct
100 Correct 175 ms 20856 KB Output is correct