QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#275378#7740. Puzzle: Question MarkmaspyAC ✓246ms54760kbC++2025.9kb2023-12-04 17:33:142023-12-04 17:33:14

Judging History

你现在查看的是最新测评结果

  • [2023-12-04 17:33:14]
  • 评测
  • 测评结果:AC
  • 用时:246ms
  • 内存:54760kb
  • [2023-12-04 17:33:14]
  • 提交

answer

#line 1 "library/my_template.hpp"
#if defined(LOCAL)
#include <my_template_compiled.hpp>
#else
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using u32 = unsigned int;
using u64 = unsigned long long;
using i128 = __int128;
using u128 = unsigned __int128;
using f128 = __float128;

template <class T>
constexpr T infty = 0;
template <>
constexpr int infty<int> = 1'000'000'000;
template <>
constexpr ll infty<ll> = ll(infty<int>) * infty<int> * 2;
template <>
constexpr u32 infty<u32> = infty<int>;
template <>
constexpr u64 infty<u64> = infty<ll>;
template <>
constexpr i128 infty<i128> = i128(infty<ll>) * infty<ll>;
template <>
constexpr double infty<double> = infty<ll>;
template <>
constexpr long double infty<long double> = infty<ll>;

using pi = pair<ll, ll>;
using vi = vector<ll>;
template <class T>
using vc = vector<T>;
template <class T>
using vvc = vector<vc<T>>;
template <class T>
using vvvc = vector<vvc<T>>;
template <class T>
using vvvvc = vector<vvvc<T>>;
template <class T>
using vvvvvc = vector<vvvvc<T>>;
template <class T>
using pq = priority_queue<T>;
template <class T>
using pqg = priority_queue<T, vector<T>, greater<T>>;

#define vv(type, name, h, ...) \
  vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define vvv(type, name, h, w, ...)   \
  vector<vector<vector<type>>> name( \
      h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...)       \
  vector<vector<vector<vector<type>>>> name( \
      a, vector<vector<vector<type>>>(       \
             b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))

// https://trap.jp/post/1224/
#define FOR1(a) for (ll _ = 0; _ < ll(a); ++_)
#define FOR2(i, a) for (ll i = 0; i < ll(a); ++i)
#define FOR3(i, a, b) for (ll i = a; i < ll(b); ++i)
#define FOR4(i, a, b, c) for (ll i = a; i < ll(b); i += (c))
#define FOR1_R(a) for (ll i = (a)-1; i >= ll(0); --i)
#define FOR2_R(i, a) for (ll i = (a)-1; i >= ll(0); --i)
#define FOR3_R(i, a, b) for (ll i = (b)-1; i >= ll(a); --i)
#define overload4(a, b, c, d, e, ...) e
#define overload3(a, b, c, d, ...) d
#define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__)
#define FOR_R(...) overload3(__VA_ARGS__, FOR3_R, FOR2_R, FOR1_R)(__VA_ARGS__)

#define FOR_subset(t, s) \
  for (ll t = (s); t >= 0; t = (t == 0 ? -1 : (t - 1) & (s)))
#define all(x) x.begin(), x.end()
#define len(x) ll(x.size())
#define elif else if

#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second

#define stoi stoll

int popcnt(int x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(ll x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }
int popcnt_mod_2(int x) { return __builtin_parity(x); }
int popcnt_mod_2(u32 x) { return __builtin_parity(x); }
int popcnt_mod_2(ll x) { return __builtin_parityll(x); }
int popcnt_mod_2(u64 x) { return __builtin_parityll(x); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2)
int topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
int topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2)
int lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
int lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }

template <typename T>
T floor(T a, T b) {
  return a / b - (a % b && (a ^ b) < 0);
}
template <typename T>
T ceil(T x, T y) {
  return floor(x + y - 1, y);
}
template <typename T>
T bmod(T x, T y) {
  return x - y * floor(x, y);
}
template <typename T>
pair<T, T> divmod(T x, T y) {
  T q = floor(x, y);
  return {q, x - q * y};
}

template <typename T, typename U>
T SUM(const vector<U> &A) {
  T sm = 0;
  for (auto &&a: A) sm += a;
  return sm;
}

#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define UNIQUE(x) \
  sort(all(x)), x.erase(unique(all(x)), x.end()), x.shrink_to_fit()

template <typename T>
T POP(deque<T> &que) {
  T a = que.front();
  que.pop_front();
  return a;
}
template <typename T>
T POP(pq<T> &que) {
  T a = que.top();
  que.pop();
  return a;
}
template <typename T>
T POP(pqg<T> &que) {
  T a = que.top();
  que.pop();
  return a;
}
template <typename T>
T POP(vc<T> &que) {
  T a = que.back();
  que.pop_back();
  return a;
}

template <typename F>
ll binary_search(F check, ll ok, ll ng, bool check_ok = true) {
  if (check_ok) assert(check(ok));
  while (abs(ok - ng) > 1) {
    auto x = (ng + ok) / 2;
    (check(x) ? ok : ng) = x;
  }
  return ok;
}
template <typename F>
double binary_search_real(F check, double ok, double ng, int iter = 100) {
  FOR(iter) {
    double x = (ok + ng) / 2;
    (check(x) ? ok : ng) = x;
  }
  return (ok + ng) / 2;
}

template <class T, class S>
inline bool chmax(T &a, const S &b) {
  return (a < b ? a = b, 1 : 0);
}
template <class T, class S>
inline bool chmin(T &a, const S &b) {
  return (a > b ? a = b, 1 : 0);
}

// ? は -1
vc<int> s_to_vi(const string &S, char first_char) {
  vc<int> A(S.size());
  FOR(i, S.size()) { A[i] = (S[i] != '?' ? S[i] - first_char : -1); }
  return A;
}

template <typename T, typename U>
vector<T> cumsum(vector<U> &A, int off = 1) {
  int N = A.size();
  vector<T> B(N + 1);
  FOR(i, N) { B[i + 1] = B[i] + A[i]; }
  if (off == 0) B.erase(B.begin());
  return B;
}

// stable sort
template <typename T>
vector<int> argsort(const vector<T> &A) {
  vector<int> ids(len(A));
  iota(all(ids), 0);
  sort(all(ids),
       [&](int i, int j) { return (A[i] == A[j] ? i < j : A[i] < A[j]); });
  return ids;
}

// A[I[0]], A[I[1]], ...
template <typename T>
vc<T> rearrange(const vc<T> &A, const vc<int> &I) {
  vc<T> B(len(I));
  FOR(i, len(I)) B[i] = A[I[i]];
  return B;
}
#endif
#line 1 "library/other/io.hpp"
#define FASTIO
#include <unistd.h>

// https://judge.yosupo.jp/submission/21623
namespace fastio {
static constexpr uint32_t SZ = 1 << 17;
char ibuf[SZ];
char obuf[SZ];
char out[100];
// pointer of ibuf, obuf
uint32_t pil = 0, pir = 0, por = 0;

struct Pre {
  char num[10000][4];
  constexpr Pre() : num() {
    for (int i = 0; i < 10000; i++) {
      int n = i;
      for (int j = 3; j >= 0; j--) {
        num[i][j] = n % 10 | '0';
        n /= 10;
      }
    }
  }
} constexpr pre;

inline void load() {
  memcpy(ibuf, ibuf + pil, pir - pil);
  pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);
  pil = 0;
  if (pir < SZ) ibuf[pir++] = '\n';
}

inline void flush() {
  fwrite(obuf, 1, por, stdout);
  por = 0;
}

void rd(char &c) {
  do {
    if (pil + 1 > pir) load();
    c = ibuf[pil++];
  } while (isspace(c));
}

void rd(string &x) {
  x.clear();
  char c;
  do {
    if (pil + 1 > pir) load();
    c = ibuf[pil++];
  } while (isspace(c));
  do {
    x += c;
    if (pil == pir) load();
    c = ibuf[pil++];
  } while (!isspace(c));
}

template <typename T>
void rd_real(T &x) {
  string s;
  rd(s);
  x = stod(s);
}

template <typename T>
void rd_integer(T &x) {
  if (pil + 100 > pir) load();
  char c;
  do
    c = ibuf[pil++];
  while (c < '-');
  bool minus = 0;
  if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
    if (c == '-') { minus = 1, c = ibuf[pil++]; }
  }
  x = 0;
  while ('0' <= c) { x = x * 10 + (c & 15), c = ibuf[pil++]; }
  if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
    if (minus) x = -x;
  }
}

void rd(int &x) { rd_integer(x); }
void rd(ll &x) { rd_integer(x); }
void rd(i128 &x) { rd_integer(x); }
void rd(u32 &x) { rd_integer(x); }
void rd(u64 &x) { rd_integer(x); }
void rd(u128 &x) { rd_integer(x); }
void rd(double &x) { rd_real(x); }
void rd(long double &x) { rd_real(x); }
void rd(f128 &x) { rd_real(x); }

template <class T, class U>
void rd(pair<T, U> &p) {
  return rd(p.first), rd(p.second);
}
template <size_t N = 0, typename T>
void rd_tuple(T &t) {
  if constexpr (N < std::tuple_size<T>::value) {
    auto &x = std::get<N>(t);
    rd(x);
    rd_tuple<N + 1>(t);
  }
}
template <class... T>
void rd(tuple<T...> &tpl) {
  rd_tuple(tpl);
}

template <size_t N = 0, typename T>
void rd(array<T, N> &x) {
  for (auto &d: x) rd(d);
}
template <class T>
void rd(vc<T> &x) {
  for (auto &d: x) rd(d);
}

void read() {}
template <class H, class... T>
void read(H &h, T &... t) {
  rd(h), read(t...);
}

void wt(const char c) {
  if (por == SZ) flush();
  obuf[por++] = c;
}
void wt(const string s) {
  for (char c: s) wt(c);
}
void wt(const char *s) {
  size_t len = strlen(s);
  for (size_t i = 0; i < len; i++) wt(s[i]);
}

template <typename T>
void wt_integer(T x) {
  if (por > SZ - 100) flush();
  if (x < 0) { obuf[por++] = '-', x = -x; }
  int outi;
  for (outi = 96; x >= 10000; outi -= 4) {
    memcpy(out + outi, pre.num[x % 10000], 4);
    x /= 10000;
  }
  if (x >= 1000) {
    memcpy(obuf + por, pre.num[x], 4);
    por += 4;
  } else if (x >= 100) {
    memcpy(obuf + por, pre.num[x] + 1, 3);
    por += 3;
  } else if (x >= 10) {
    int q = (x * 103) >> 10;
    obuf[por] = q | '0';
    obuf[por + 1] = (x - q * 10) | '0';
    por += 2;
  } else
    obuf[por++] = x | '0';
  memcpy(obuf + por, out + outi + 4, 96 - outi);
  por += 96 - outi;
}

template <typename T>
void wt_real(T x) {
  ostringstream oss;
  oss << fixed << setprecision(15) << double(x);
  string s = oss.str();
  wt(s);
}

void wt(int x) { wt_integer(x); }
void wt(ll x) { wt_integer(x); }
void wt(i128 x) { wt_integer(x); }
void wt(u32 x) { wt_integer(x); }
void wt(u64 x) { wt_integer(x); }
void wt(u128 x) { wt_integer(x); }
void wt(double x) { wt_real(x); }
void wt(long double x) { wt_real(x); }
void wt(f128 x) { wt_real(x); }

template <class T, class U>
void wt(const pair<T, U> val) {
  wt(val.first);
  wt(' ');
  wt(val.second);
}
template <size_t N = 0, typename T>
void wt_tuple(const T t) {
  if constexpr (N < std::tuple_size<T>::value) {
    if constexpr (N > 0) { wt(' '); }
    const auto x = std::get<N>(t);
    wt(x);
    wt_tuple<N + 1>(t);
  }
}
template <class... T>
void wt(tuple<T...> tpl) {
  wt_tuple(tpl);
}
template <class T, size_t S>
void wt(const array<T, S> val) {
  auto n = val.size();
  for (size_t i = 0; i < n; i++) {
    if (i) wt(' ');
    wt(val[i]);
  }
}
template <class T>
void wt(const vector<T> val) {
  auto n = val.size();
  for (size_t i = 0; i < n; i++) {
    if (i) wt(' ');
    wt(val[i]);
  }
}

void print() { wt('\n'); }
template <class Head, class... Tail>
void print(Head &&head, Tail &&... tail) {
  wt(head);
  if (sizeof...(Tail)) wt(' ');
  print(forward<Tail>(tail)...);
}

// gcc expansion. called automaticall after main.
void __attribute__((destructor)) _d() { flush(); }
} // namespace fastio
using fastio::read;
using fastio::print;
using fastio::flush;

#define INT(...)   \
  int __VA_ARGS__; \
  read(__VA_ARGS__)
#define LL(...)   \
  ll __VA_ARGS__; \
  read(__VA_ARGS__)
#define U32(...)   \
  u32 __VA_ARGS__; \
  read(__VA_ARGS__)
#define U64(...)   \
  u64 __VA_ARGS__; \
  read(__VA_ARGS__)
#define STR(...)      \
  string __VA_ARGS__; \
  read(__VA_ARGS__)
#define CHAR(...)   \
  char __VA_ARGS__; \
  read(__VA_ARGS__)
#define DBL(...)      \
  double __VA_ARGS__; \
  read(__VA_ARGS__)

#define VEC(type, name, size) \
  vector<type> name(size);    \
  read(name)
#define VV(type, name, h, w)                     \
  vector<vector<type>> name(h, vector<type>(w)); \
  read(name)

void YES(bool t = 1) { print(t ? "YES" : "NO"); }
void NO(bool t = 1) { YES(!t); }
void Yes(bool t = 1) { print(t ? "Yes" : "No"); }
void No(bool t = 1) { Yes(!t); }
void yes(bool t = 1) { print(t ? "yes" : "no"); }
void no(bool t = 1) { yes(!t); }
#line 3 "main.cpp"

#line 2 "library/ds/segtree/segtree.hpp"

template <class Monoid>
struct SegTree {
  using MX = Monoid;
  using X = typename MX::value_type;
  using value_type = X;
  vc<X> dat;
  int n, log, size;

  SegTree() {}
  SegTree(int n) { build(n); }
  template <typename F>
  SegTree(int n, F f) {
    build(n, f);
  }
  SegTree(const vc<X>& v) { build(v); }

  void build(int m) {
    build(m, [](int i) -> X { return MX::unit(); });
  }
  void build(const vc<X>& v) {
    build(len(v), [&](int i) -> X { return v[i]; });
  }
  template <typename F>
  void build(int m, F f) {
    n = m, log = 1;
    while ((1 << log) < n) ++log;
    size = 1 << log;
    dat.assign(size << 1, MX::unit());
    FOR(i, n) dat[size + i] = f(i);
    FOR_R(i, 1, size) update(i);
  }

  X get(int i) { return dat[size + i]; }
  vc<X> get_all() { return {dat.begin() + size, dat.begin() + size + n}; }

  void update(int i) { dat[i] = Monoid::op(dat[2 * i], dat[2 * i + 1]); }
  void set(int i, const X& x) {
    assert(i < n);
    dat[i += size] = x;
    while (i >>= 1) update(i);
  }

  void multiply(int i, const X& x) {
    assert(i < n);
    i += size;
    dat[i] = Monoid::op(dat[i], x);
    while (i >>= 1) update(i);
  }

  X prod(int L, int R) {
    assert(0 <= L && L <= R && R <= n);
    X vl = Monoid::unit(), vr = Monoid::unit();
    L += size, R += size;
    while (L < R) {
      if (L & 1) vl = Monoid::op(vl, dat[L++]);
      if (R & 1) vr = Monoid::op(dat[--R], vr);
      L >>= 1, R >>= 1;
    }
    return Monoid::op(vl, vr);
  }

  X prod_all() { return dat[1]; }

  template <class F>
  int max_right(F check, int L) {
    assert(0 <= L && L <= n && check(Monoid::unit()));
    if (L == n) return n;
    L += size;
    X sm = Monoid::unit();
    do {
      while (L % 2 == 0) L >>= 1;
      if (!check(Monoid::op(sm, dat[L]))) {
        while (L < size) {
          L = 2 * L;
          if (check(Monoid::op(sm, dat[L]))) { sm = Monoid::op(sm, dat[L++]); }
        }
        return L - size;
      }
      sm = Monoid::op(sm, dat[L++]);
    } while ((L & -L) != L);
    return n;
  }

  template <class F>
  int min_left(F check, int R) {
    assert(0 <= R && R <= n && check(Monoid::unit()));
    if (R == 0) return 0;
    R += size;
    X sm = Monoid::unit();
    do {
      --R;
      while (R > 1 && (R % 2)) R >>= 1;
      if (!check(Monoid::op(dat[R], sm))) {
        while (R < size) {
          R = 2 * R + 1;
          if (check(Monoid::op(dat[R], sm))) { sm = Monoid::op(dat[R--], sm); }
        }
        return R + 1 - size;
      }
      sm = Monoid::op(dat[R], sm);
    } while ((R & -R) != R);
    return 0;
  }

  // prod_{l<=i<r} A[i xor x]
  X xor_prod(int l, int r, int xor_val) {
    static_assert(Monoid::commute);
    X x = Monoid::unit();
    for (int k = 0; k < log + 1; ++k) {
      if (l >= r) break;
      if (l & 1) { x = Monoid::op(x, dat[(size >> k) + ((l++) ^ xor_val)]); }
      if (r & 1) { x = Monoid::op(x, dat[(size >> k) + ((--r) ^ xor_val)]); }
      l /= 2, r /= 2, xor_val /= 2;
    }
    return x;
  }
};
#line 2 "library/alg/monoid/max_idx.hpp"

template <typename T, bool tie_is_left = true>
struct Monoid_Max_Idx {
  using value_type = pair<T, int>;
  using X = value_type;
  static X op(X x, X y) {
    if (x.fi > y.fi) return x;
    if (x.fi < y.fi) return y;
    if (x.se > y.se) swap(x, y);
    return (tie_is_left ? x : y);
  }
  static constexpr X unit() { return {-infty<T>, -1}; }
  static constexpr bool commute = true;
};
#line 2 "library/ds/segtree/segtree_beats.hpp"

template <typename ActedMonoid>
struct SegTree_Beats {
  using AM = ActedMonoid;
  using MX = typename AM::Monoid_X;
  using MA = typename AM::Monoid_A;
  using X = typename MX::value_type;
  using A = typename MA::value_type;
  int n, log, size;
  vc<X> dat;
  vc<A> laz;

  SegTree_Beats() {}
  SegTree_Beats(int n) { build(n); }
  template <typename F>
  SegTree_Beats(int n, F f) {
    build(n, f);
  }
  SegTree_Beats(const vc<X>& v) { build(v); }

  void build(int m) {
    build(m, [](int i) -> X { return MX::unit(); });
  }
  void build(const vc<X>& v) {
    build(len(v), [&](int i) -> X { return v[i]; });
  }
  template <typename F>
  void build(int m, F f) {
    n = m, log = 1;
    while ((1 << log) < n) ++log;
    size = 1 << log;
    dat.assign(size << 1, MX::unit());
    laz.assign(size, MA::unit());
    FOR(i, n) dat[size + i] = f(i);
    FOR_R(i, 1, size) update(i);
  }

  void update(int k) { dat[k] = MX::op(dat[2 * k], dat[2 * k + 1]); }
  void set(int p, X x) {
    assert(0 <= p && p < n);
    p += size;
    for (int i = log; i >= 1; i--) push(p >> i);
    dat[p] = x;
    for (int i = 1; i <= log; i++) update(p >> i);
  }

  X get(int p) {
    assert(0 <= p && p < n);
    p += size;
    for (int i = log; i >= 1; i--) push(p >> i);
    return dat[p];
  }

  /*
  void all_apply(int k, A a) {
    dat[k] = ActedMonoid::act(dat[k], a);
    if (k < size) {
      laz[k] = MA::op(laz[k], a);
      if (dat[k].fail) push(k), update(k);
    }
  }
  */

  vc<X> get_all() {
    FOR(k, 1, size) { push(k); }
    return {dat.begin() + size, dat.begin() + size + n};
  }

  X prod(int l, int r) {
    assert(0 <= l && l <= r && r <= n);
    if (l == r) return MX::unit();
    l += size, r += size;
    for (int i = log; i >= 1; i--) {
      if (((l >> i) << i) != l) push(l >> i);
      if (((r >> i) << i) != r) push((r - 1) >> i);
    }
    X xl = MX::unit(), xr = MX::unit();
    while (l < r) {
      if (l & 1) xl = MX::op(xl, dat[l++]);
      if (r & 1) xr = MX::op(dat[--r], xr);
      l >>= 1, r >>= 1;
    }
    return MX::op(xl, xr);
  }

  X prod_all() { return dat[1]; }

  void apply(int l, int r, A a) {
    assert(0 <= l && l <= r && r <= n);
    if (l == r) return;
    l += size, r += size;
    for (int i = log; i >= 1; i--) {
      if (((l >> i) << i) != l) push(l >> i);
      if (((r >> i) << i) != r) push((r - 1) >> i);
    }
    int l2 = l, r2 = r;
    while (l < r) {
      if (l & 1) apply_at(l++, a);
      if (r & 1) apply_at(--r, a);
      l >>= 1, r >>= 1;
    }
    l = l2, r = r2;
    for (int i = 1; i <= log; i++) {
      if (((l >> i) << i) != l) update(l >> i);
      if (((r >> i) << i) != r) update((r - 1) >> i);
    }
  }

private:
  void apply_at(int k, A a) {
    int sz = 1 << (log - topbit(k));
    dat[k] = AM::act(dat[k], a, sz);
    if (k < size) {
      laz[k] = MA::op(laz[k], a);
      if (dat[k].fail) push(k), update(k);
    }
  }

  void push(int k) {
    if (laz[k] == MA::unit()) return;
    apply_at(2 * k, laz[k]), apply_at(2 * k + 1, laz[k]);
    laz[k] = MA::unit();
  }
};
#line 2 "library/ds/segtree/beats_summin_chmax.hpp"
template <typename T>
struct Beats_SumMin_Chmax {
  struct SumMin {
    struct X {
      T sum, min, minc, min2;
      bool fail;
    };
    using value_type = X;
    static X op(const X& x, const X& y) {
      if (x.min == infty<T>) return y;
      if (y.min == infty<T>) return x;
      X z;
      z.sum = x.sum + y.sum;

      z.min = min(x.min, y.min);
      z.minc = (x.min == z.min ? x.minc : 0) + (y.min == z.min ? y.minc : 0);

      z.min2 = infty<T>;
      if (z.min < x.min && x.min < z.min2) z.min2 = x.min;
      if (z.min < x.min2 && x.min2 < z.min2) z.min2 = x.min2;
      if (z.min < y.min && y.min < z.min2) z.min2 = y.min;
      if (z.min < y.min2 && y.min2 < z.min2) z.min2 = y.min2;

      z.fail = 0;
      return z;
    }
    static constexpr X unit() { return {0, infty<T>, 0, infty<T>, 0}; }
    bool commute = true;
  };
  struct AddChmax {
    using X = pair<T, T>;
    using value_type = X;
    static constexpr X op(const X& x, const X& y) {
      auto [a, c] = x;
      auto [d, f] = y;
      a += d, c += d, c = max(c, f);
      return {a, c};
    }
    static constexpr X unit() { return {0, -infty<T>}; }
    bool commute = false;
  };
  struct Beats {
    using Monoid_X = SumMin;
    using Monoid_A = AddChmax;
    using X = typename Monoid_X::value_type;
    using A = typename Monoid_A::value_type;
    static X act(X& x, const A& a, int cnt) {
      assert(!x.fail);
      if (x.min == infty<T>) return x;
      auto [add, ma] = a;
      x.sum += cnt * add, x.min += add, x.min2 += add;
      if (ma == -infty<T>) return x;

      T before_min = x.min;
      x.min = max(x.min, ma);
      if (x.minc == cnt) { x.min2 = x.min, x.sum = cnt * x.min; }
      elif (x.min2 > x.min) { x.sum += (x.min - before_min) * x.minc; }
      else {
        x.fail = 1;
      }
      return x;
    }
  };
  using X = typename SumMin::X;
  SegTree_Beats<Beats> seg;
  Beats_SumMin_Chmax(vc<T>& A) {
    seg.build(len(A), [&](int i) -> X { return from_element(A[i]); });
  }
  template <typename F>
  Beats_SumMin_Chmax(int n, F f) {
    seg.build(n, [&](int i) -> X { return from_element(f(i)); });
  }
  void set(int i, T x) { seg.set(i, from_element(x)); }

  // (sum, min)
  pair<T, T> prod(int l, int r) {
    auto e = seg.prod(l, r);
    return {e.sum, e.min};
  }
  static X from_element(T x) { return {x, x, 1, x}; }

  void chmax(int l, int r, T x) { seg.apply(l, r, {0, x}); }
  void add(int l, int r, T x) { seg.apply(l, r, {x, -infty<T>}); }
};
#line 7 "main.cpp"

void from_string(vvc<int>& A, int& p, vc<string> X) {
  ll N = len(X);
  map<char, vc<pair<int, int>>> MP;
  FOR(i, N) FOR(j, N) {
    char x = X[i][j];
    MP[x].eb(i, j);
  }

  for (auto& [x, pos]: MP) {
    if (x == '.') { continue; }
    for (auto& [i, j]: pos) { A[i][j] = p; }
    p++;
  }
}

void out(vvc<int> A) {
  ll N = len(A);
  ll ans = 0;
  FOR(i, N) FOR(j, N) chmax(ans, A[i][j]);
  vc<int> CNT(ans + 1);
  FOR(i, N) FOR(j, N) { CNT[A[i][j]]++; }
  FOR(x, 1, ans + 1) { assert(CNT[x] == 4); }
  print(ans);
  FOR(i, N) print(A[i]);
}

void put_24(vvc<int>& A, int& p, int i, int j) {
  A[i + 0][j + 0] = p + 0;
  A[i + 0][j + 1] = p + 0;
  A[i + 0][j + 2] = p + 1;
  A[i + 0][j + 3] = p + 1;
  A[i + 1][j + 0] = p + 0;
  A[i + 1][j + 1] = p + 1;
  A[i + 1][j + 2] = p + 0;
  A[i + 1][j + 3] = p + 1;
  p += 2;
}

void put_42(vvc<int>& A, int& p, int i, int j) {
  A[i + 0][j + 0] = p + 0;
  A[i + 1][j + 0] = p + 0;
  A[i + 2][j + 0] = p + 1;
  A[i + 3][j + 0] = p + 1;
  A[i + 0][j + 1] = p + 0;
  A[i + 1][j + 1] = p + 1;
  A[i + 2][j + 1] = p + 0;
  A[i + 3][j + 1] = p + 1;
  p += 2;
}

void fill_rect(vvc<int>& A, int& p, int x1, int x2, int y1, int y2) {
  if ((x2 - x1) % 4 == 0) {
    assert((y2 - y1) % 2 == 0);
    FOR(i, (x2 - x1) / 4) {
      FOR(j, (y2 - y1) / 2) { put_42(A, p, x1 + 4 * i, y1 + 2 * j); }
    }
  } else {
    assert((x2 - x1) % 2 == 0);
    assert((y2 - y1) % 4 == 0);
    FOR(i, (x2 - x1) / 2) {
      FOR(j, (y2 - y1) / 4) { put_24(A, p, x1 + 2 * i, y1 + 4 * j); }
    }
  }
}

void solve_4(ll N) {
  vv(int, A, N, N);
  int p = 1;
  fill_rect(A, p, 0, N, 0, N);
  out(A);
}

void solve_2(ll N) {
  vv(int, A, N, N);
  int p = 1;
  fill_rect(A, p, 0, N, 0, N - 2);
  fill_rect(A, p, 0, N - 2, N - 2, N);
  out(A);
}

vvc<int> solve_odd(ll N) {
  if (N == 1) {
    vv(int, A, N, N);
    return A;
  }
  if (N == 3) {
    vv(int, A, 3, 3);
    A[0] = {1, 2, 1};
    A[1] = {2, 1, 1};
    A[2] = {2, 2, 0};
    return A;
  }
  if (N == 5) {
    vv(int, A, N, N);
    int p = 1;
    vc<string> X = {"aabdb", "acbbd", "caedd", "cc.e.", "..ee."};
    from_string(A, p, X);
    return A;
  }
  if (N == 7) {
    vv(int, A, N, N);
    int p = 1;
    vc<string> X = {"ababcdc", "aabbccd", "eefhfdd", "egffhii",
                    "gejhhki", "gg.j.ik", "..jj.kk"};
    from_string(A, p, X);
    return A;
  }
  if (N == 9) {
    vv(int, A, N, N);
    int p = 1;
    vc<string> X
        = {"aacbcdede", "fabccddee", "afbbgghih", "ffjjgkhhi", "lljmmgkii",
           "nlnjmkkoo", "lnnmpqpro", "ststppqor", "sstt.qqrr"};
    from_string(A, p, X);
    return A;
  }
  if (N == 13) {
    vv(int, A, N, N);
    int p = 1;
    vc<string> X = {"!!ZZ##$%$%&'&", "(!)Z*#$$%%&&'", "!(Z)#*++,-,''",
                    "(())**+/,,-00", "1132344+/--50", "6123347//8805",
                    "16229947::855", "66;;9<77:8=>=", "??;@@9<AA:==>",
                    "B?B;@<<ACAC>>", "?BB@DDEEFCCGG", "HIHIDJEJEFKGK",
                    "HHII.DJJFFKKG"};
    from_string(A, p, X);
    return A;
  }
  if (N % 4 == 3) {
    auto A = solve_odd(N - 2);
    A.resize(N);
    FOR(i, N) A[i].resize(N);
    int p = (N - 2) * (N - 2) / 4 + 1;
    fill_rect(A, p, N - 2, N, 0, N - 3);
    fill_rect(A, p, 0, N - 3, N - 2, N);
    A[N - 3][N - 2] = p, A[N - 3][N - 1] = p;
    A[N - 2][N - 1] = p, A[N - 1][N - 2] = p;
    ++p;
    A[N - 2][N - 3] = p, A[N - 2][N - 2] = p;
    A[N - 1][N - 3] = p, A[N - 1][N - 1] = p;
    ++p;
    return A;
  }
  assert(N % 4 == 1 && N >= 17);

  vvc<int> B = solve_odd(N - 8);
  int p = (N - 8) * (N - 8) / 4 + 1;
  vv(int, A, N, N);
  FOR(i, N - 8) FOR(j, N - 8) { A[8 + i][8 + j] = B[i][j]; }
  fill_rect(A, p, 13, N, 0, 8);
  fill_rect(A, p, 0, 8, 13, N);

  vv(int, C, 13, 13);
  vc<string> X
      = {".....!!ZZ##$$", ".....%!&Z'#($", ".....!%Z&#'$(", ".....%%&&''((",
         ".....)*)*+,+,", "/-/00))**++,,", "-//0101232344", "--55116223374",
         "88599::6;<;47", "=8=59:66;;<77", "8==9>>:??<<@@", "ABABC>CD?DE@E",
         "AABB>CC?DDEE@"};
  from_string(C, p, X);
  reverse(all(C));
  FOR(i, 13) reverse(all(C[i]));
  FOR(i, 13) FOR(j, 13) {
    if (C[i][j] != 0) A[i][j] = C[i][j];
  }
  return A;
}

void solve() {
  LL(N);
  if (N % 4 == 0) return solve_4(N);
  if (N % 4 == 2) return solve_2(N);
  auto A = solve_odd(N);
  out(A);
}

signed main() {
  INT(T);
  FOR(T) solve();
  return 0;
}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3776kb

input:

2
3
4

output:

2
1 2 1
2 1 1
2 2 0
4
1 1 3 3
1 2 3 4
2 1 4 3
2 2 4 4

result:

ok Correct. (2 test cases)

Test #2:

score: 0
Accepted
time: 74ms
memory: 5084kb

input:

246
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
...

output:

0
0
0
0 0
0 0
2
1 2 1
2 1 1
2 2 0
4
1 1 3 3
1 2 3 4
2 1 4 3
2 2 4 4
5
1 1 2 4 2
1 3 2 2 4
3 1 5 4 4
3 3 0 5 0
0 0 5 5 0
8
1 1 2 2 7 7
1 2 1 2 7 8
3 3 4 4 8 7
3 4 3 4 8 8
5 5 6 6 0 0
5 6 5 6 0 0
11
1 2 1 2 3 4 3
1 1 2 2 3 3 4
5 5 6 8 6 4 4
5 7 6 6 8 9 9
7 5 10 8 8 11 9
7 7 0 10 0 9 11
0 0 10 10 0 11 ...

result:

ok Correct. (246 test cases)

Test #3:

score: 0
Accepted
time: 66ms
memory: 4964kb

input:

64
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310

output:

15252
15000 15005 15005 15004 15004 14999 15003 15003 14998 15002 15002 15001 15001 14507 14507 14509 14509 14511 14511 14513 14513 14515 14515 14517 14517 14519 14519 14521 14521 14523 14523 14525 14525 14527 14527 14529 14529 14531 14531 14533 14533 14535 14535 14537 14537 14539 14539 14541 14541 ...

result:

ok Correct. (64 test cases)

Test #4:

score: 0
Accepted
time: 68ms
memory: 5492kb

input:

45
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355

output:

24180
23864 23869 23869 23868 23868 23863 23867 23867 23862 23866 23866 23865 23865 23243 23243 23245 23245 23247 23247 23249 23249 23251 23251 23253 23253 23255 23255 23257 23257 23259 23259 23261 23261 23263 23263 23265 23265 23267 23267 23269 23269 23271 23271 23273 23273 23275 23275 23277 23277 ...

result:

ok Correct. (45 test cases)

Test #5:

score: 0
Accepted
time: 57ms
memory: 5624kb

input:

35
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390

output:

31684
1 1 3 3 5 5 7 7 9 9 11 11 13 13 15 15 17 17 19 19 21 21 23 23 25 25 27 27 29 29 31 31 33 33 35 35 37 37 39 39 41 41 43 43 45 45 47 47 49 49 51 51 53 53 55 55 57 57 59 59 61 61 63 63 65 65 67 67 69 69 71 71 73 73 75 75 77 77 79 79 81 81 83 83 85 85 87 87 89 89 91 91 93 93 95 95 97 97 99 99 101 ...

result:

ok Correct. (35 test cases)

Test #6:

score: 0
Accepted
time: 56ms
memory: 5972kb

input:

30
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420

output:

38220
37824 37829 37829 37828 37828 37823 37827 37827 37822 37826 37826 37825 37825 37043 37043 37045 37045 37047 37047 37049 37049 37051 37051 37053 37053 37055 37055 37057 37057 37059 37059 37061 37061 37063 37063 37065 37065 37067 37067 37069 37069 37071 37071 37073 37073 37075 37075 37077 37077 ...

result:

ok Correct. (30 test cases)

Test #7:

score: 0
Accepted
time: 42ms
memory: 38928kb

input:

2
2000
1000

output:

1000000
1 1 3 3 5 5 7 7 9 9 11 11 13 13 15 15 17 17 19 19 21 21 23 23 25 25 27 27 29 29 31 31 33 33 35 35 37 37 39 39 41 41 43 43 45 45 47 47 49 49 51 51 53 53 55 55 57 57 59 59 61 61 63 63 65 65 67 67 69 69 71 71 73 73 75 75 77 77 79 79 81 81 83 83 85 85 87 87 89 89 91 91 93 93 95 95 97 97 99 99 10...

result:

ok Correct. (2 test cases)

Test #8:

score: 0
Accepted
time: 240ms
memory: 54760kb

input:

2
1999
999

output:

999000
996996 997001 997001 997000 997000 996995 996999 996999 996994 996998 996998 996997 996997 992999 992999 993001 993001 993003 993003 993005 993005 993007 993007 993009 993009 993011 993011 993013 993013 993015 993015 993017 993017 993019 993019 993021 993021 993023 993023 993025 993025 993027...

result:

ok Correct. (2 test cases)

Test #9:

score: 0
Accepted
time: 46ms
memory: 38460kb

input:

2
1998
998

output:

998000
1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20 21 21 22 22 23 23 24 24 25 25 26 26 27 27 28 28 29 29 30 30 31 31 32 32 33 33 34 34 35 35 36 36 37 37 38 38 39 39 40 40 41 41 42 42 43 43 44 44 45 45 46 46 47 47 48 48 49 49 50 50 51 51 52 52...

result:

ok Correct. (2 test cases)

Test #10:

score: 0
Accepted
time: 246ms
memory: 39312kb

input:

2
1997
997

output:

997002
996996 997001 997001 997000 997000 996995 996999 996999 996994 996998 996998 996997 996997 992999 992999 993001 993001 993003 993003 993005 993005 993007 993007 993009 993009 993011 993011 993013 993013 993015 993015 993017 993017 993019 993019 993021 993021 993023 993023 993025 993025 993027...

result:

ok Correct. (2 test cases)

Test #11:

score: 0
Accepted
time: 52ms
memory: 38536kb

input:

2
1996
996

output:

996004
1 1 3 3 5 5 7 7 9 9 11 11 13 13 15 15 17 17 19 19 21 21 23 23 25 25 27 27 29 29 31 31 33 33 35 35 37 37 39 39 41 41 43 43 45 45 47 47 49 49 51 51 53 53 55 55 57 57 59 59 61 61 63 63 65 65 67 67 69 69 71 71 73 73 75 75 77 77 79 79 81 81 83 83 85 85 87 87 89 89 91 91 93 93 95 95 97 97 99 99 101...

result:

ok Correct. (2 test cases)

Test #12:

score: 0
Accepted
time: 233ms
memory: 54656kb

input:

2
1995
995

output:

995006
993006 993011 993011 993010 993010 993005 993009 993009 993004 993008 993008 993007 993007 989017 989017 989019 989019 989021 989021 989023 989023 989025 989025 989027 989027 989029 989029 989031 989031 989033 989033 989035 989035 989037 989037 989039 989039 989041 989041 989043 989043 989045...

result:

ok Correct. (2 test cases)

Test #13:

score: 0
Accepted
time: 34ms
memory: 38480kb

input:

2
1994
994

output:

994008
1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20 21 21 22 22 23 23 24 24 25 25 26 26 27 27 28 28 29 29 30 30 31 31 32 32 33 33 34 34 35 35 36 36 37 37 38 38 39 39 40 40 41 41 42 42 43 43 44 44 45 45 46 46 47 47 48 48 49 49 50 50 51 51 52 52...

result:

ok Correct. (2 test cases)

Test #14:

score: 0
Accepted
time: 229ms
memory: 39460kb

input:

2
1993
993

output:

993012
993006 993011 993011 993010 993010 993005 993009 993009 993004 993008 993008 993007 993007 989017 989017 989019 989019 989021 989021 989023 989023 989025 989025 989027 989027 989029 989029 989031 989031 989033 989033 989035 989035 989037 989037 989039 989039 989041 989041 989043 989043 989045...

result:

ok Correct. (2 test cases)

Test #15:

score: 0
Accepted
time: 38ms
memory: 38388kb

input:

2
1992
992

output:

992016
1 1 3 3 5 5 7 7 9 9 11 11 13 13 15 15 17 17 19 19 21 21 23 23 25 25 27 27 29 29 31 31 33 33 35 35 37 37 39 39 41 41 43 43 45 45 47 47 49 49 51 51 53 53 55 55 57 57 59 59 61 61 63 63 65 65 67 67 69 69 71 71 73 73 75 75 77 77 79 79 81 81 83 83 85 85 87 87 89 89 91 91 93 93 95 95 97 97 99 99 101...

result:

ok Correct. (2 test cases)

Test #16:

score: 0
Accepted
time: 234ms
memory: 54404kb

input:

2
1991
991

output:

991020
989024 989029 989029 989028 989028 989023 989027 989027 989022 989026 989026 989025 989025 985043 985043 985045 985045 985047 985047 985049 985049 985051 985051 985053 985053 985055 985055 985057 985057 985059 985059 985061 985061 985063 985063 985065 985065 985067 985067 985069 985069 985071...

result:

ok Correct. (2 test cases)

Extra Test:

score: 0
Extra Test Passed