Submission #70454143


Source Code Expand

#define LOCAL
#include <bits/stdc++.h>

using namespace std;

template <typename A, typename B>
ostream& operator <<(ostream& out, const pair<A, B>& a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
ostream& operator <<(ostream& out, const array<T, N>& a) {
  out << "["; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
  return out;
}
template <typename T>
ostream& operator <<(ostream& out, const vector<T>& a) {
  out << "["; bool first = true;
  for (auto v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
  return out;
}
template <typename T, class Cmp>
ostream& operator <<(ostream& out, const set<T, Cmp>& a) {
  out << "{"; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "}";
  return out;
}
template <typename T, class Cmp>
ostream& operator <<(ostream& out, const multiset<T, Cmp>& a) {
  out << "{"; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream& operator <<(ostream& out, const map<U, T, Cmp>& a) {
  out << "{"; bool first = true;
  for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0;} out << "}";
  return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

template <class T> auto vect(const T& v, int n) { return vector<T>(n, v); }
template <class T, class... D> auto vect(const T& v, int n, D... m) {
  return vector<decltype(vect(v, m...))>(n, vect(v, m...));
}

using int64 = long long;
using int128 = __int128_t;
using ii = pair<int, int>;
#define SZ(x) (int)((x).size())
template <typename T> static constexpr T inf = numeric_limits<T>::max() / 2;
const int MOD = 1e9 + 7;
// const int MOD = 998244353;
mt19937_64 mrand(random_device{}());
int64 rnd(int64 x) { return mrand() % x; }
constexpr inline int lg2(int64 x) { return x == 0 ? -1 : sizeof(int64) * 8 - 1 - __builtin_clzll(x); }
constexpr inline int64 p2ceil(int64 x) { return 1LL << (lg2(x - 1) + 1); }
template <class T> void out(const vector<T>& a) { for (int i = 0; i < SZ(a); ++i) cout << a[i] << " \n"[i + 1 == SZ(a)]; }
template <class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template <class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
template <class T> void dedup(vector<T>& v) { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); }
inline void add_mod(int& x, int y) { x += y; if (x >= MOD) x -= MOD; }
inline void sub_mod(int& x, int y) { x += MOD - y; if (x >= MOD) x -= MOD; }
inline int mod(int x) { return x >= MOD ? x - MOD : x; }

struct fast_ios {
  fast_ios() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  };
} fast_ios_;

const int M = 3;
// typedef array<array<int, M>, M> mat;
typedef array<int, M> vec;

struct mat : array<array<int, M>, M> {
  mat() {
    for (int i = 0; i < M; ++i) {
      for (int j = 0; j < M; ++j) {
        (*this)[i][j] = (i == j ? 0 : inf<int>);
      }
    }
  }
};
mat operator *(const mat& a, const mat& b) {
  mat ret {};
  for (int i = 0; i < M; ++i) {
    for (int j = 0; j < M; ++j) {
      ret[i][j] = inf<int>;
      for (int k = 0; k < M; ++k) {
        ckmin(ret[i][j], a[i][k] + b[k][j]);
      }
    }
  }
  return ret;
}

vec operator *(const vec& b, const mat& a) {
  vec ret {};
  for (int i = 0; i < M; ++i) {
    ret[i] = inf<int>;
    for (int j = 0; j < M; ++j) {
      ckmin(ret[i], a[j][i] + b[j]);
    }
  }
  return ret;
}

template<size_t N,
         class T,
         T (*op)(const T&, const T&),
         T (*e)()>
struct SegmentTree {
  struct Node {
    Node *left, *right;
    T val;
    void pushup() { val = op(left->val, right->val); }
  };
  Node pool[N << 1], *last, *rt;
  int n;
  SegmentTree(const vector<T>& v): n(SZ(v)) {
    last = pool;
    rt = build(0, n, v);
  }
  SegmentTree(int n): SegmentTree(vector<T>(n, e())) {}
  Node* build(int a, int b, const vector<T>& v) {
    Node* ret = last++;
    if (a + 1 == b) { ret->val = v[a]; return ret; }
    int mid = (a + b) / 2;
    ret->left = build(a, mid, v);
    ret->right = build(mid, b, v);
    ret->pushup();
    return ret;
  }
  void set(int pos, T val) { return set(rt, 0, n, pos, val); }
  void set(Node* cur, int a, int b, int pos, T val) {
    if (pos < a || pos >= b) return;
    if (a + 1 == b) { cur->val = val; return; }
    int mid = (a + b) / 2;
    set(cur->left, a, mid, pos, val);
    set(cur->right, mid, b, pos, val);
    cur->pushup();
  }
  T query(int ll, int rr) { return query(rt, 0, n, ll, rr); }
  T query(Node* cur, int a, int b, int ll, int rr) {
    if (ll >= rr || a >= rr || b <= ll) return e();
    if (a >= ll && b <= rr) return cur->val;
    int mid = (a + b) / 2;
    return op(query(cur->left, a, mid, ll, rr), query(cur->right, mid, b, ll, rr));
  }
};

const int N = 2e5 + 10;

// sum
template<typename T> T add(const T& a, const T& b) { return a * b; }
template<typename T> T id() { return T(); }
using segtree = SegmentTree<N, mat, add, id>;


int main() {
  int n;
  cin >> n;
  vector<string> s(3);
  for (int i = 0; i < 3; ++i) cin >> s[i];
  vector<mat> A(8);
  for (int mask = 0; mask < 8; ++mask) {
    auto& cur = A[mask];
    bitset<3> v(mask);
    for (int i = 0; i < 3; ++i) {
      cur[i] = {inf<int>, inf<int>, inf<int>};
      queue<int> Q;
      if (!v[i]) {
        cur[i][i] = 1;
        Q.push(i);
      }
      while (!Q.empty()) {
        auto j = Q.front();
        Q.pop();
        if (j - 1 >= 0 && !v[j - 1] && cur[i][j - 1] > cur[i][j] + 1) {
          cur[i][j - 1] = cur[i][j] + 1;
          Q.push(j - 1);
        }
        if (j + 1 < 3 && !v[j + 1] && cur[i][j + 1] > cur[i][j] + 1) {
          cur[i][j + 1] = cur[i][j] + 1;
          Q.push(j + 1);
        }
      }
    }
  }
  vector<mat> v(n);
  for (int j = 0; j < n; ++j) {
    int mask = 0;
    for (int i = 0; i < 3; ++i) {
      mask |= (s[i][j] == '#') << i;
    }
    v[j] = A[mask];
    // trace(j, mask, v[j]);
  }

  segtree tr(v);
  int q;
  cin >> q;
  while (q--) {
    int x, y;
    cin >> x >> y;
    --x; --y;
    s[x][y] ^= '#' ^ '.';
    int mask = 0;
    for (int i = 0; i < 3; ++i) {
      mask |= (s[i][y] == '#') << i;
    }
    tr.set(y, A[mask]);
    auto cur = tr.query(1, n);
    // trace(cur);
    vec V {};
    V[0] = s[0][0] == '.' ? 0 : inf<int>;
    V[1] = s[0][0] == '.' && s[1][0] == '.' ? 1 : inf<int>;
    V[2] = s[0][0] == '.' && s[1][0] == '.' && s[2][0] == '.' ? 2 :
      inf<int>;
    // trace(V);
    auto W = V * cur;
    cout << (W[2] < inf<int> ? W[2] : -1) << '\n';
  }
  return 0;
}

Submission Info

Submission Time
Task F - Shortest Path Query
User cuiaoxiang
Language C++ 20 (gcc 12.2)
Score 525
Code Size 7384 Byte
Status AC
Exec Time 424 ms
Memory 32748 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 525 / 525
Status
AC × 2
AC × 30
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt
All 00_sample_00.txt, 00_sample_01.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 11 ms 25364 KiB
00_sample_01.txt AC 11 ms 25368 KiB
01_random_00.txt AC 48 ms 25508 KiB
01_random_01.txt AC 47 ms 25308 KiB
01_random_02.txt AC 47 ms 25288 KiB
01_random_03.txt AC 47 ms 25372 KiB
01_random_04.txt AC 326 ms 28664 KiB
01_random_05.txt AC 278 ms 27528 KiB
01_random_06.txt AC 186 ms 25512 KiB
01_random_07.txt AC 363 ms 30692 KiB
01_random_08.txt AC 249 ms 26320 KiB
01_random_09.txt AC 382 ms 32704 KiB
01_random_10.txt AC 380 ms 32748 KiB
01_random_11.txt AC 393 ms 32600 KiB
01_random_12.txt AC 375 ms 32584 KiB
01_random_13.txt AC 370 ms 32512 KiB
01_random_14.txt AC 400 ms 32516 KiB
01_random_15.txt AC 354 ms 32516 KiB
01_random_16.txt AC 423 ms 32600 KiB
01_random_17.txt AC 424 ms 32704 KiB
01_random_18.txt AC 415 ms 32652 KiB
01_random_19.txt AC 391 ms 32744 KiB
01_random_20.txt AC 395 ms 32628 KiB
01_random_21.txt AC 381 ms 32652 KiB
01_random_22.txt AC 387 ms 32560 KiB
01_random_23.txt AC 388 ms 32748 KiB
01_random_24.txt AC 391 ms 32556 KiB
01_random_25.txt AC 381 ms 32616 KiB
01_random_26.txt AC 384 ms 32512 KiB
01_random_27.txt AC 372 ms 32628 KiB