QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#114646#6304. RectanglemaspyTL 5714ms38780kbC++2336.5kb2023-06-22 18:14:582023-06-22 18:15:01

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-22 18:15:01]
  • 评测
  • 测评结果:TL
  • 用时:5714ms
  • 内存:38780kb
  • [2023-06-22 18:14:58]
  • 提交

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;

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); }
// (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, typename U>
T ceil(T x, U y) {
  return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U>
T floor(T x, U y) {
  return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T, typename U>
pair<T, T> divmod(T x, U y) {
  T q = floor(x, y);
  return {q, x - q * y};
}

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

#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) {
  assert(!que.empty());
  T a = que.top();
  que.pop();
  return a;
}
template <typename T>
T POP(vc<T> &que) {
  assert(!que.empty());
  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;
    tie(ok, ng) = (check(x) ? mp(x, ng) : mp(ok, 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;
    tie(ok, ng) = (check(x) ? mp(x, ng) : mp(ok, 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"
// based on yosupo's fastio
#include <unistd.h>

namespace fastio {
#define FASTIO
// クラスが read(), print() を持っているかを判定するメタ関数
struct has_write_impl {
  template <class T>
  static auto check(T &&x) -> decltype(x.write(), std::true_type{});

  template <class T>
  static auto check(...) -> std::false_type;
};

template <class T>
class has_write : public decltype(has_write_impl::check<T>(std::declval<T>())) {
};

struct has_read_impl {
  template <class T>
  static auto check(T &&x) -> decltype(x.read(), std::true_type{});

  template <class T>
  static auto check(...) -> std::false_type;
};

template <class T>
class has_read : public decltype(has_read_impl::check<T>(std::declval<T>())) {};

struct Scanner {
  FILE *fp;
  char line[(1 << 15) + 1];
  size_t st = 0, ed = 0;
  void reread() {
    memmove(line, line + st, ed - st);
    ed -= st;
    st = 0;
    ed += fread(line + ed, 1, (1 << 15) - ed, fp);
    line[ed] = '\0';
  }
  bool succ() {
    while (true) {
      if (st == ed) {
        reread();
        if (st == ed) return false;
      }
      while (st != ed && isspace(line[st])) st++;
      if (st != ed) break;
    }
    if (ed - st <= 50) {
      bool sep = false;
      for (size_t i = st; i < ed; i++) {
        if (isspace(line[i])) {
          sep = true;
          break;
        }
      }
      if (!sep) reread();
    }
    return true;
  }
  template <class T, enable_if_t<is_same<T, string>::value, int> = 0>
  bool read_single(T &ref) {
    if (!succ()) return false;
    while (true) {
      size_t sz = 0;
      while (st + sz < ed && !isspace(line[st + sz])) sz++;
      ref.append(line + st, sz);
      st += sz;
      if (!sz || st != ed) break;
      reread();
    }
    return true;
  }
  template <class T, enable_if_t<is_integral<T>::value, int> = 0>
  bool read_single(T &ref) {
    if (!succ()) return false;
    bool neg = false;
    if (line[st] == '-') {
      neg = true;
      st++;
    }
    ref = T(0);
    while (isdigit(line[st])) { ref = 10 * ref + (line[st++] & 0xf); }
    if (neg) ref = -ref;
    return true;
  }
  template <typename T,
            typename enable_if<has_read<T>::value>::type * = nullptr>
  inline bool read_single(T &x) {
    x.read();
    return true;
  }
  bool read_single(double &ref) {
    string s;
    if (!read_single(s)) return false;
    ref = std::stod(s);
    return true;
  }
  bool read_single(char &ref) {
    string s;
    if (!read_single(s) || s.size() != 1) return false;
    ref = s[0];
    return true;
  }
  template <class T>
  bool read_single(vector<T> &ref) {
    for (auto &d: ref) {
      if (!read_single(d)) return false;
    }
    return true;
  }
  template <class T, class U>
  bool read_single(pair<T, U> &p) {
    return (read_single(p.first) && read_single(p.second));
  }
  template <size_t N = 0, typename T>
  void read_single_tuple(T &t) {
    if constexpr (N < std::tuple_size<T>::value) {
      auto &x = std::get<N>(t);
      read_single(x);
      read_single_tuple<N + 1>(t);
    }
  }
  template <class... T>
  bool read_single(tuple<T...> &tpl) {
    read_single_tuple(tpl);
    return true;
  }
  void read() {}
  template <class H, class... T>
  void read(H &h, T &... t) {
    bool f = read_single(h);
    assert(f);
    read(t...);
  }
  Scanner(FILE *fp) : fp(fp) {}
};

struct Printer {
  Printer(FILE *_fp) : fp(_fp) {}
  ~Printer() { flush(); }

  static constexpr size_t SIZE = 1 << 15;
  FILE *fp;
  char line[SIZE], small[50];
  size_t pos = 0;
  void flush() {
    fwrite(line, 1, pos, fp);
    pos = 0;
  }
  void write(const char val) {
    if (pos == SIZE) flush();
    line[pos++] = val;
  }
  template <class T, enable_if_t<is_integral<T>::value, int> = 0>
  void write(T val) {
    if (pos > (1 << 15) - 50) flush();
    if (val == 0) {
      write('0');
      return;
    }
    if (val < 0) {
      write('-');
      val = -val; // todo min
    }
    size_t len = 0;
    while (val) {
      small[len++] = char(0x30 | (val % 10));
      val /= 10;
    }
    for (size_t i = 0; i < len; i++) { line[pos + i] = small[len - 1 - i]; }
    pos += len;
  }
  void write(const string s) {
    for (char c: s) write(c);
  }
  void write(const char *s) {
    size_t len = strlen(s);
    for (size_t i = 0; i < len; i++) write(s[i]);
  }
  void write(const double x) {
    ostringstream oss;
    oss << fixed << setprecision(15) << x;
    string s = oss.str();
    write(s);
  }
  void write(const long double x) {
    ostringstream oss;
    oss << fixed << setprecision(15) << x;
    string s = oss.str();
    write(s);
  }
  template <typename T,
            typename enable_if<has_write<T>::value>::type * = nullptr>
  inline void write(T x) {
    x.write();
  }
  template <class T>
  void write(const vector<T> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
      if (i) write(' ');
      write(val[i]);
    }
  }
  template <class T, class U>
  void write(const pair<T, U> val) {
    write(val.first);
    write(' ');
    write(val.second);
  }
  template <size_t N = 0, typename T>
  void write_tuple(const T t) {
    if constexpr (N < std::tuple_size<T>::value) {
      if constexpr (N > 0) { write(' '); }
      const auto x = std::get<N>(t);
      write(x);
      write_tuple<N + 1>(t);
    }
  }
  template <class... T>
  bool write(tuple<T...> tpl) {
    write_tuple(tpl);
    return true;
  }
  template <class T, size_t S>
  void write(const array<T, S> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
      if (i) write(' ');
      write(val[i]);
    }
  }
  void write(i128 val) {
    string s;
    bool negative = 0;
    if (val < 0) {
      negative = 1;
      val = -val;
    }
    while (val) {
      s += '0' + int(val % 10);
      val /= 10;
    }
    if (negative) s += "-";
    reverse(all(s));
    if (len(s) == 0) s = "0";
    write(s);
  }
};
Scanner scanner = Scanner(stdin);
Printer printer = Printer(stdout);
void flush() { printer.flush(); }
void print() { printer.write('\n'); }
template <class Head, class... Tail>
void print(Head &&head, Tail &&... tail) {
  printer.write(head);
  if (sizeof...(Tail)) printer.write(' ');
  print(forward<Tail>(tail)...);
}

void read() {}
template <class Head, class... Tail>
void read(Head &head, Tail &... tail) {
  scanner.read(head);
  read(tail...);
}
} // namespace fastio
using fastio::print;
using fastio::flush;
using fastio::read;

#define INT(...)   \
  int __VA_ARGS__; \
  read(__VA_ARGS__)
#define LL(...)   \
  ll __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/mod/modint_common.hpp"

struct has_mod_impl {
  template <class T>
  static auto check(T &&x) -> decltype(x.get_mod(), std::true_type{});
  template <class T>
  static auto check(...) -> std::false_type;
};

template <class T>
class has_mod : public decltype(has_mod_impl::check<T>(std::declval<T>())) {};

template <typename mint>
mint inv(int n) {
  static const int mod = mint::get_mod();
  static vector<mint> dat = {0, 1};
  assert(0 <= n);
  if (n >= mod) n %= mod;
  while (len(dat) <= n) {
    int k = len(dat);
    int q = (mod + k - 1) / k;
    dat.eb(dat[k * q - mod] * mint(q));
  }
  return dat[n];
}

template <typename mint>
mint fact(int n) {
  static const int mod = mint::get_mod();
  assert(0 <= n);
  if (n >= mod) return 0;
  static vector<mint> dat = {1, 1};
  while (len(dat) <= n) dat.eb(dat[len(dat) - 1] * mint(len(dat)));
  return dat[n];
}

template <typename mint>
mint fact_inv(int n) {
  static vector<mint> dat = {1, 1};
  if (n < 0) return mint(0);
  while (len(dat) <= n) dat.eb(dat[len(dat) - 1] * inv<mint>(len(dat)));
  return dat[n];
}

template <class mint, class... Ts>
mint fact_invs(Ts... xs) {
  return (mint(1) * ... * fact_inv<mint>(xs));
}

template <typename mint, class Head, class... Tail>
mint multinomial(Head &&head, Tail &&... tail) {
  return fact<mint>(head) * fact_invs<mint>(std::forward<Tail>(tail)...);
}

template <typename mint>
mint C_dense(int n, int k) {
  static vvc<mint> C;
  static int H = 0, W = 0;
  auto calc = [&](int i, int j) -> mint {
    if (i == 0) return (j == 0 ? mint(1) : mint(0));
    return C[i - 1][j] + (j ? C[i - 1][j - 1] : 0);
  };
  if (W <= k) {
    FOR(i, H) {
      C[i].resize(k + 1);
      FOR(j, W, k + 1) { C[i][j] = calc(i, j); }
    }
    W = k + 1;
  }
  if (H <= n) {
    C.resize(n + 1);
    FOR(i, H, n + 1) {
      C[i].resize(W);
      FOR(j, W) { C[i][j] = calc(i, j); }
    }
    H = n + 1;
  }
  return C[n][k];
}

template <typename mint, bool large = false, bool dense = false>
mint C(ll n, ll k) {
  assert(n >= 0);
  if (k < 0 || n < k) return 0;
  if (dense) return C_dense<mint>(n, k);
  if (!large) return multinomial<mint>(n, k, n - k);
  k = min(k, n - k);
  mint x(1);
  FOR(i, k) x *= mint(n - i);
  return x * fact_inv<mint>(k);
}

template <typename mint, bool large = false>
mint C_inv(ll n, ll k) {
  assert(n >= 0);
  assert(0 <= k && k <= n);
  if (!large) return fact_inv<mint>(n) * fact<mint>(k) * fact<mint>(n - k);
  return mint(1) / C<mint, 1>(n, k);
}

// [x^d] (1-x) ^ {-n} の計算
template <typename mint, bool large = false, bool dense = false>
mint C_negative(ll n, ll d) {
  assert(n >= 0);
  if (d < 0) return mint(0);
  if (n == 0) { return (d == 0 ? mint(1) : mint(0)); }
  return C<mint, large, dense>(n + d - 1, d);
}
#line 3 "library/mod/modint.hpp"

template <int mod>
struct modint {
  static_assert(mod < (1 << 30));
  int val;
  constexpr modint(const ll val = 0) noexcept
      : val(val >= 0 ? val % mod : (mod - (-val) % mod) % mod) {}
  bool operator<(const modint &other) const {
    return val < other.val;
  } // To use std::map
  modint &operator+=(const modint &p) {
    if ((val += p.val) >= mod) val -= mod;
    return *this;
  }
  modint &operator-=(const modint &p) {
    if ((val += mod - p.val) >= mod) val -= mod;
    return *this;
  }
  modint &operator*=(const modint &p) {
    val = (int)(1LL * val * p.val % mod);
    return *this;
  }
  modint &operator/=(const modint &p) {
    *this *= p.inverse();
    return *this;
  }
  modint operator-() const { return modint(-val); }
  modint operator+(const modint &p) const { return modint(*this) += p; }
  modint operator-(const modint &p) const { return modint(*this) -= p; }
  modint operator*(const modint &p) const { return modint(*this) *= p; }
  modint operator/(const modint &p) const { return modint(*this) /= p; }
  bool operator==(const modint &p) const { return val == p.val; }
  bool operator!=(const modint &p) const { return val != p.val; }
  modint inverse() const {
    int a = val, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b), swap(u -= t * v, v);
    }
    return modint(u);
  }
  modint pow(ll n) const {
    assert(n >= 0);
    modint ret(1), mul(val);
    while (n > 0) {
      if (n & 1) ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }
#ifdef FASTIO
  void write() { fastio::printer.write(val); }
  void read() { fastio::scanner.read(val); }
#endif
  static constexpr int get_mod() { return mod; }
  // (n, r), r は 1 の 2^n 乗根
  static constexpr pair<int, int> ntt_info() {
    if (mod == 167772161) return {25, 17};
    if (mod == 469762049) return {26, 30};
    if (mod == 754974721) return {24, 362};
    if (mod == 880803841) return {23, 211};
    if (mod == 998244353) return {23, 31};
    if (mod == 1045430273) return {20, 363};
    if (mod == 1051721729) return {20, 330};
    if (mod == 1053818881) return {20, 2789};
    return {-1, -1};
  }
  static constexpr bool can_ntt() { return ntt_info().fi != -1; }
};

using modint107 = modint<1000000007>;
using modint998 = modint<998244353>;
#line 2 "library/alg/monoid/min.hpp"

template <typename E>
struct Monoid_Min {
  using X = E;
  using value_type = X;
  static constexpr X op(const X &x, const X &y) noexcept { return min(x, y); }
  static constexpr X unit() { return infty<E>; }
  static constexpr bool commute = true;
};
#line 2 "library/alg/monoid/max.hpp"

template <typename E>
struct Monoid_Max {
  using X = E;
  using value_type = X;
  static constexpr X op(const X &x, const X &y) noexcept { return max(x, y); }
  static constexpr X unit() { return -infty<E>; }
  static constexpr bool commute = true;
};
#line 7 "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 1 "library/ds/offline_query/add_remove_query.hpp"
/*
・時刻 t に x を追加する
・時刻 t に x を削除する
があるときに、
・時刻 [l, r) に x を追加する
に変換する。
クエリが時系列順に来ることが分かっているときは monotone = true の方が高速。
*/
template <typename X, bool monotone>
struct Add_Remove_Query {
  map<X, int> MP;
  vc<tuple<int, int, X>> dat;
  map<X, vc<int>> ADD;
  map<X, vc<int>> RM;

  void add(int time, X x) {
    if (monotone) return add_monotone(time, x);
    ADD[x].eb(time);
  }
  void remove(int time, X x) {
    if (monotone) return remove_monotone(time, x);
    RM[x].eb(time);
  }

  // すべてのクエリが終わった現在時刻を渡す
  vc<tuple<int, int, X>> calc(int time) {
    if (monotone) return calc_monotone(time);
    vc<tuple<int, int, X>> dat;
    for (auto&& [x, A]: ADD) {
      vc<int> B;
      if (RM.count(x)) {
        B = RM[x];
        RM.erase(x);
      }
      if (len(B) < len(A)) B.eb(time);
      assert(len(A) == len(B));

      sort(all(A));
      sort(all(B));
      FOR(i, len(A)) {
        assert(A[i] <= B[i]);
        if (A[i] < B[i]) dat.eb(A[i], B[i], x);
      }
    }
    assert(len(RM) == 0);
    return dat;
  }

private:
  void add_monotone(int time, X x) {
    assert(!MP.count(x));
    MP[x] = time;
  }
  void remove_monotone(int time, X x) {
    auto it = MP.find(x);
    assert(it != MP.end());
    int t = (*it).se;
    MP.erase(it);
    if (t == time) return;
    dat.eb(t, time, x);
  }
  vc<tuple<int, int, X>> calc_monotone(int time) {
    for (auto&& [x, t]: MP) {
      if (t == time) continue;
      dat.eb(t, time, x);
    }
    return dat;
  }
};
#line 10 "main.cpp"

#line 2 "library/alg/monoid/assign.hpp"

template <typename X, X none_val>
struct Monoid_Assign {
  using value_type = X;
  static X op(X x, X y) { return (y == none_val ? x : y); }
  static constexpr X unit() { return none_val; }
  static constexpr bool commute = false;
};
#line 2 "library/ds/rollback_array.hpp"

template <typename T>
struct Rollback_Array {
  int N;
  vc<T> dat;
  vc<pair<int, T>> history;

  Rollback_Array() {}
  Rollback_Array(vc<T> x) : N(len(x)), dat(x) {}
  Rollback_Array(int N) : N(N), dat(N) {}
  template <typename F>
  Rollback_Array(int N, F f) : N(N) {
    dat.reserve(N);
    FOR(i, N) dat.eb(f(i));
  }

  int time() { return len(history); }
  void rollback(int t) {
    FOR_R(i, t, time()) {
      auto& [idx, v] = history[i];
      dat[idx] = v;
    }
    history.resize(t);
  }
  T get(int idx) { return dat[idx]; }
  void set(int idx, T x) {
    history.eb(idx, dat[idx]);
    dat[idx] = x;
  }

  vc<T> get_all() {
    vc<T> res(N);
    FOR(i, N) res[i] = get(i);
    return res;
  }
};
#line 13 "main.cpp"

template <typename ActedMonoid>
struct Rollback_Lazy_SegTree {
  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;
  Rollback_Array<X> dat;
  Rollback_Array<A> laz;

  Rollback_Lazy_SegTree() {}
  Rollback_Lazy_SegTree(int n) { build(n); }
  template <typename F>
  Rollback_Lazy_SegTree(int n, F f) {
    build(n, f);
  }
  Rollback_Lazy_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 = Rollback_Array<X>(vc<X>(size << 1, MX::unit()));
    laz = Rollback_Array<A>(vc<A>(size, MA::unit()));
    FOR(i, n) dat.set(size + i, f(i));
    FOR_R(i, 1, size) update(i);
  }

  void update(int k) { dat.set(k, MX::op(dat.get(2 * k), dat.get(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.set(p, x);
    for (int i = 1; i <= log; i++) update(p >> i);
  }
  void multiply(int p, const X& x) {
    assert(0 <= p && p < n);
    p += size;
    for (int i = log; i >= 1; i--) push(p >> i);
    dat.set(p, MX::op(dat.get(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.get(p);
  }

  vc<X> get_all() {
    auto tmp = dat.get_all();
    FOR(k, 1, size) push(k);
    return {tmp.begin() + size, tmp.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.get(l++));
      if (r & 1) xr = MX::op(dat.get(--r), xr);
      l >>= 1, r >>= 1;
    }
    return MX::op(xl, xr);
  }

  X prod_all() { return dat.get(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);
    }
  }

  template <typename F>
  int max_right(const F check, int l) {
    assert(0 <= l && l <= n);
    assert(check(MX::unit()));
    if (l == n) return n;
    l += size;
    for (int i = log; i >= 1; i--) push(l >> i);
    X sm = MX::unit();
    do {
      while (l % 2 == 0) l >>= 1;
      if (!check(MX::op(sm, dat.get(l)))) {
        while (l < size) {
          push(l);
          l = (2 * l);
          if (check(MX::op(sm, dat.get(l)))) { sm = MX::op(sm, dat.get(l++)); }
        }
        return l - size;
      }
      sm = MX::op(sm, dat.get(l++));
    } while ((l & -l) != l);
    return n;
  }

  template <typename F>
  int min_left(const F check, int r) {
    assert(0 <= r && r <= n);
    assert(check(MX::unit()));
    if (r == 0) return 0;
    r += size;
    for (int i = log; i >= 1; i--) push((r - 1) >> i);
    X sm = MX::unit();
    do {
      r--;
      while (r > 1 && (r % 2)) r >>= 1;
      if (!check(MX::op(dat.get(r), sm))) {
        while (r < size) {
          push(r);
          r = (2 * r + 1);
          if (check(MX::op(dat.get(r), sm))) { sm = MX::op(dat.get(r--), sm); }
        }
        return r + 1 - size;
      }
      sm = MX::op(dat.get(r), sm);
    } while ((r & -r) != r);
    return 0;
  }

  pair<int, int> time() { return {dat.time(), laz.time()}; }
  void rollback(pair<int, int> t) { dat.rollback(t.fi), laz.rollback(t.se); }

private:
  void apply_at(int k, A a) {
    ll sz = 1 << (log - topbit(k));
    dat.set(k, AM::act(dat.get(k), a, sz));
    if (k < size) laz.set(k, MA::op(laz.get(k), a));
  }
  void push(int k) {
    if (laz.get(k) == MA::unit()) return;
    apply_at(2 * k, laz.get(k)), apply_at(2 * k + 1, laz.get(k));
    laz.set(k, MA::unit());
  }
};

using mint = modint998;

struct Data {
  int cnt, max;
  mint sum;
};

struct Mono {
  using value_type = Data;
  using X = value_type;
  static X op(X x, X y) {
    if (x.cnt == 0) return y;
    if (y.cnt == 0) return x;
    return Data{x.cnt + y.cnt, max(x.max, y.max), x.sum + y.sum};
  }
  static constexpr X unit() { return Data{0, -infty<int>, 0}; }
  static constexpr bool commute = 1;
};

struct ActedMonoid {
  using Monoid_X = Mono;
  using Monoid_A = Monoid_Assign<int, -1>;
  using X = typename Monoid_X::value_type;
  using A = typename Monoid_A::value_type;
  static X act(const X& x, const A& a, const ll& size) {
    if (a == -1) return x;
    return Data{x.cnt, a, mint(x.cnt) * mint(a)};
  }
};

// 全部 x = t の形
mint solve_1(vi X1, vi X2, vi Y1, vi Y2) {
  // X しか関係ない
  vi X = {1, 1000000000 + 1};
  for (auto&& x: X1) X.eb(x);
  for (auto&& x: X2) X.eb(x);
  UNIQUE(X);
  for (auto&& x: X1) x = LB(X, x);
  for (auto&& x: X2) x = LB(X, x);
  ll K = len(X) - 1;
  vc<int> DX(K);
  FOR(k, K) DX[k] = X[k + 1] - X[k];
  ll N = len(X1);

  mint ANS = 0;

  /*
  ・x が小さい区間について、Lmax, Rmin
  ・x が大きい区間について、Lmax, Rmin

  R -> Lmax, R->Rmin
  L -> Lmax, L->Rmin
  */
  SegTree<Monoid_Max<int>> segRL(K + 1);
  SegTree<Monoid_Min<int>> segRR(K + 1);
  SegTree<Monoid_Max<int>> segLL(K + 1);
  SegTree<Monoid_Min<int>> segLR(K + 1);

  FOR(i, N) {
    int a = X1[i], b = X2[i];
    segRL.multiply(b, a);
    segRR.multiply(b, b);
    segLR.multiply(a, b);
    segLL.multiply(a, a);
  }

  FOR(b, K) {
    int a1 = segRL.prod(0, b + 1);
    int a2 = segRR.prod(0, b + 1);
    int b1 = segLL.prod(b + 1, K + 1);
    int b2 = segLR.prod(b + 1, K + 1);

    // (b,b,b)
    if (a1 == -infty<int> && b1 == -infty<int>) {
      mint x = DX[b];
      ANS += x * (x - 1) * (x - 2) * inv<mint>(6);
    }
    // (b,b,c)
    if (a1 == -infty<int> && b1 < b2) {
      int lo = max<int>(b1, b + 1);
      int hi = min<int>(b2, K);
      mint x = DX[b];
      x = x * (x - 1) * inv<mint>(2);
      ANS += x * mint(X[hi] - X[lo]);
    }
    // (a,b,b)
    if (b1 == -infty<int> && a1 < a2) {
      int lo = max<int>(a1, 0);
      int hi = min<int>(a2, b);
      mint x = DX[b];
      x = x * (x - 1) * inv<mint>(2);
      ANS += x * mint(X[hi] - X[lo]);
    }
    // (a,b,c)
    chmax(a1, 0), chmin(a2, b);
    chmax(b1, b + 1), chmin(b2, K);
    if (a1 < a2 && b1 < b2) {
      ANS += mint(DX[b]) * mint(X[a2] - X[a1]) * mint(X[b2] - X[b1]);
    }
  }
  return ANS;
}

// y = t をひとつ使って、x = t をふたつ使う
mint solve_2(vi X1, vi X2, vi Y1, vi Y2) {
  const int N = len(X1);
  vi X = {1, 1000000000 + 1};
  for (auto&& x: X1) X.eb(x);
  for (auto&& x: X2) X.eb(x);
  UNIQUE(X);
  for (auto&& x: X1) x = LB(X, x);
  for (auto&& x: X2) x = LB(X, x);

  vi Y = {1, 1000000000 + 1};
  for (auto&& x: Y1) Y.eb(x);
  for (auto&& x: Y2) Y.eb(x);
  UNIQUE(Y);
  for (auto&& x: Y1) x = LB(Y, x);
  for (auto&& x: Y2) x = LB(Y, x);

  ll N1 = len(X) - 1, N2 = len(Y) - 1;

  vc<int> DX(N1), DY(N2);
  FOR(i, N1) DX[i] = X[i + 1] - X[i];
  FOR(i, N2) DY[i] = Y[i + 1] - Y[i];

  Add_Remove_Query<int, false> ARQ;
  FOR(i, N) {
    ARQ.add(0, i);
    ARQ.remove(Y1[i], i);
    ARQ.add(Y2[i], i);
    ARQ.remove(N2, i);
  }

  // rollback_dfs
  auto upd = ARQ.calc(N2);
  vc<mint> ANS(N2);
  vc<int> I(len(upd));
  iota(all(I), 0);

  vc<int> LO(N1), HI(N1);
  FOR(i, N1) LO[i] = 1 + i, HI[i] = N1;
  int r_min = N1, l_max = 0;

  vc<mint> C2(N1);
  FOR(i, N1) C2[i] = mint(DX[i]) * mint(DX[i] - 1) * inv<mint>(2);
  C2 = cumsum<mint>(C2);

  Rollback_Lazy_SegTree<ActedMonoid> seg_1(N1, [&](int i) -> Data {
    Data x;
    x.cnt = DX[i];
    x.max = X[i + 1];
    x.sum = mint(x.cnt) * mint(X[i + 1]);
    return x;
  });

  Rollback_Lazy_SegTree<ActedMonoid> seg_2(N1, [&](int i) -> Data {
    Data x;
    x.cnt = DX[i];
    x.max = X[N1];
    x.sum = mint(x.cnt) * mint(X[N1]);
    return x;
  });

  auto dfs = [&](auto& dfs, vc<int>& upd_query_I, int begin, int end) -> void {
    if (begin == end) return;
    // snapshot
    // vc<int> LO_memo = LO, HI_memo = HI;
    auto seg_1_time = seg_1.time();
    auto seg_2_time = seg_2.time();
    int r_min_memo = r_min;
    int l_max_memo = l_max;

    vc<int> IL, IR;
    int mid = (begin + end) / 2;
    for (auto&& i: upd_query_I) {
      auto [a, b, idx] = upd[i];
      if (a <= begin && end <= b) {
        int x1 = X1[idx], x2 = X2[idx];
        // FOR(p, x1) { chmax(LO[p], x1), chmin(HI[p], x2); }
        seg_2.apply(x2, N1, 0);

        // LO[p] < x1 となる範囲を探索
        int k = 0;
        k = seg_1.max_right([&](Data d) -> bool { return d.max < X[x1]; }, 0);
        seg_1.apply(0, k, X[x1]);
        k = seg_2.max_right([&](Data d) -> bool { return d.max < X[x2]; }, 0);
        if (k < x1) seg_2.apply(k, x1, X[x2]);
        chmin(r_min, x2), chmax(l_max, x1);
      } else {
        if (a < mid) IL.eb(i);
        if (mid < b) IR.eb(i);
      }
    }
    if (begin + 1 == end) {
      // 求値クエリ
      int qid = begin;
      /*
      int s = 0;
      while (s < len(LO) && LO[s] > HI[s]) ++s;
      */
      int s = binary_search(
          [&](int s) -> bool {
            if (s >= r_min) return true;
            int lo = seg_1.prod(s, s + 1).max;
            int hi = seg_2.prod(s, s + 1).max;
            return lo <= hi;
          },
          r_min, -1);

      if (s < r_min) {
        mint hi_sum = seg_2.prod(s, r_min).sum;
        mint lo_sum = seg_1.prod(s, r_min).sum;
        ANS[qid] += hi_sum - lo_sum;
      }
      /*
      if (s < r_min) {
        FOR(i, s, r_min) {
          assert(LO[i] <= HI[i]);
          mint x = X[HI[i]] - X[LO[i]];
          ANS[qid] += x * mint(DX[i]);
        }
      }
      */
      if (l_max <= r_min) ANS[qid] += C2[r_min] - C2[l_max];
    } else {
      dfs(dfs, IL, begin, mid);
      dfs(dfs, IR, mid, end);
    }
    // rollback
    // LO = LO_memo, HI = HI_memo;
    seg_1.rollback(seg_1_time);
    seg_2.rollback(seg_2_time);
    r_min = r_min_memo;
    l_max = l_max_memo;
  };
  dfs(dfs, I, 0, N2);
  mint ans = 0;
  FOR(i, N2) ans += mint(DY[i]) * ANS[i];
  return ans;
}

void solve() {
  LL(N);
  vi X1(N), X2(N), Y1(N), Y2(N);
  FOR(i, N) { read(X1[i]), read(Y1[i]), read(X2[i]), read(Y2[i]); }
  FOR(i, N) X2[i]++, Y2[i]++;
  mint ANS = 0;
  ANS += solve_1(X1, X2, Y1, Y2);
  ANS += solve_2(X1, X2, Y1, Y2);
  ANS += solve_1(Y1, Y2, X1, X2);
  ANS += solve_2(Y1, Y2, X1, X2);
  print(ANS);
}

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

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3568kb

input:

3
1
1 1 1000000000 1000000000
3
1 1 2 2
3 3 4 4
5 5 6 6
5
581574116 47617804 999010750 826131769
223840663 366320907 613364068 926991396
267630832 51913575 488301124 223957497
217461197 492085159 999485867 913732845
28144453 603781668 912516656 993160442

output:

230616300
64
977066618

result:

ok 3 number(s): "230616300 64 977066618"

Test #2:

score: 0
Accepted
time: 37ms
memory: 3620kb

input:

1000
10
5 7 6 10
4 3 6 9
1 6 3 7
1 1 7 10
1 2 7 7
5 2 8 3
2 1 5 7
1 1 10 6
1 7 2 8
4 7 5 9
10
6 6 8 10
4 4 9 9
2 4 6 5
5 3 10 10
1 3 4 4
2 2 3 6
7 5 8 6
2 7 8 8
5 7 10 10
2 4 7 8
10
3 6 6 10
1 2 7 4
7 5 10 9
4 5 8 9
2 7 5 9
2 2 9 7
3 3 8 4
1 1 8 6
5 4 10 6
3 9 7 10
10
3 1 8 9
1 3 8 10
4 1 7 4
2 4 5 ...

output:

28090346
21067815
91293528
63203269
14045217
24579047
49158123
56180656
10533942
83
28090372
101827338
512901428
38624242
10533932
42135595
7022614
7022661
7022622
31601641
21067817
35112979
7022628
7022682
17556485
42135554
59692019
28090452
14045202
73737099
42135505
31601703
49158091
28090434
108...

result:

ok 1000 numbers

Test #3:

score: 0
Accepted
time: 61ms
memory: 3620kb

input:

1000
10
56 6 85 86
2 67 79 76
41 32 57 94
7 24 95 72
4 82 98 99
21 16 64 95
5 15 97 50
75 34 93 63
65 1 74 40
62 50 91 57
10
1 66 4 99
73 28 80 35
9 46 84 72
57 12 74 75
24 20 72 86
30 35 51 52
20 32 80 48
1 27 38 38
79 30 91 86
49 11 78 31
10
84 36 95 84
18 76 83 96
87 6 97 24
59 74 79 81
36 6 49 1...

output:

286940182
6719
3879
10985
284425706
1482
154507014
337096188
15634
15198
13957
311811545
2065
487051821
104322525
4160
6232
3566
259869863
676660625
533734216
1108
210694302
941064564
9524057
366655439
960
712817222
294969344
505637269
5277
216
807601960
6489
192
252834684
9863
523061934
1036
370
16...

result:

ok 1000 numbers

Test #4:

score: 0
Accepted
time: 58ms
memory: 3596kb

input:

1000
10
151949335 343818689 226887829 843487881
26268786 629172470 727820988 753588469
239557045 129989050 828912527 803511337
216216272 737211068 952614934 901139965
9412307 270208240 969836975 781270622
210767204 691143582 734743967 978765608
115072779 149374200 365237395 723260248
373039270 50881...

output:

989992685
889170389
199600526
811602467
101647877
422274637
988817681
775346897
987827054
201664770
0
425324155
654779513
129937888
504567840
567363794
953468940
390846625
863893486
832900735
549488122
626520957
651708706
325695215
265584217
535054615
654784437
835844534
970196650
259827660
73563096...

result:

ok 1000 numbers

Test #5:

score: 0
Accepted
time: 1197ms
memory: 3688kb

input:

20000
10
387518709 362080654 857718988 413892967
259574026 383071239 349422952 406322216
252781835 421214199 960954668 766986966
709548059 152868851 829506776 286195984
533564379 238783143 835360470 804665480
37715197 439312193 350269160 510361284
760325088 379134217 996444460 640572941
75706031 242...

output:

425031005
145199488
76594243
608094392
105901554
767793557
925027675
718008576
542146803
257776847
83948409
557131094
957234323
316994452
711146361
878596101
863265391
86278462
882556696
943381219
171834801
312110039
509815322
995001589
267270543
321796762
646607323
535110629
612892338
264981338
862...

result:

ok 20000 numbers

Test #6:

score: 0
Accepted
time: 1200ms
memory: 3688kb

input:

20000
9
23397362 5465077 922916650 905326448
334129892 259574026 806023297 349422952
85390000 14589070 252781835 159363469
518493829 9904109 881263301 763351170
376515120 592421912 709548059 922381128
522936 888125869 919020884 968891551
192627293 528719402 238783143 846674462
269993278 155196111 41...

output:

38446691
441652008
80634077
86958519
313123987
37147655
292453230
135822548
347213034
875178269
572593450
121333363
910761745
288628833
144542647
336001702
492148086
504465362
307137735
40715447
936972207
895075762
698856636
442121431
960601688
728799681
619152060
51416671
586371383
183976942
996194...

result:

ok 20000 numbers

Test #7:

score: 0
Accepted
time: 2066ms
memory: 3836kb

input:

2000
98
441828066 355550044 980140398 758338808
139187839 379489833 732496729 915149220
6198442 7406064 811667085 671521385
634108502 54340068 750182168 712125681
51518185 164417838 414217749 690302726
26971686 349814847 96913121 613904116
18885592 344395345 969337141 761958781
6500321 288362602 695...

output:

847505316
69861689
762022665
942187496
610542723
297361682
408287130
37288980
129194998
781348133
138625309
885806848
596340815
251557403
136654463
415973838
348935020
217451841
607735223
183258123
453186006
29511771
486730947
90933161
744360742
932334149
464917933
58351031
36268611
777434481
348682...

result:

ok 2000 numbers

Test #8:

score: 0
Accepted
time: 3246ms
memory: 5848kb

input:

200
993
336152525 31049117 970839548 34483370
257968529 73378100 760295564 459870661
769587893 565770848 979251259 884779692
92706327 715631888 976631772 730467480
102936760 674056008 996888200 756831534
141490448 466112222 761954523 960644829
601216664 500053814 974949771 928192751
298296452 359164...

output:

163043779
101789580
214159985
605122084
222495872
595662571
919415389
27790333
940923361
745272650
432367810
269413997
881525570
275197159
128519736
759744770
394059985
858394070
885939030
507707772
380648232
853123251
659897376
142847962
866652391
78179639
672081467
655879491
267275050
880872481
74...

result:

ok 200 numbers

Test #9:

score: 0
Accepted
time: 372ms
memory: 4120kb

input:

200
989
1 8 5 9
6 4 9 7
9 4 10 10
3 3 8 7
7 1 9 5
9 4 10 6
4 3 7 8
3 1 7 4
2 5 3 6
3 4 4 7
4 3 7 9
1 2 9 8
4 3 6 4
2 2 6 10
6 8 7 10
2 1 3 9
1 1 4 10
1 5 4 8
2 3 4 9
4 3 9 9
1 2 6 7
7 4 9 5
4 3 8 10
4 1 8 10
7 2 8 5
2 4 3 8
5 4 10 6
9 1 10 5
5 3 7 7
6 4 10 10
6 6 7 9
1 1 3 10
2 2 9 8
2 3 7 8
3 9 10 ...

output:

3
1
2
1
2
1
2
3511294
1
3511294
2
2
3511295
3
2
2
6
1
2
1
1
3
1
3511294
1
1
10533874
1
1
1
2
2
1
6
432141794
1
1
2
4
1
3511294
2
3511294
3
3
1
2
1
853749714
3
3
7022585
3511294
2
2
3
7022585
3
14045164
1
2
1
1
1
4
2
4
3511295
3
3
3511295
3511295
7022585
1
2
1
3
2
1
7022585
2
2
2
3
2
7022585
2
4
3
2
...

result:

ok 200 numbers

Test #10:

score: 0
Accepted
time: 904ms
memory: 4420kb

input:

200
993
5 48 42 78
55 33 66 92
6 5 38 34
38 34 82 48
9 76 34 86
50 39 72 44
46 54 96 82
4 13 68 24
39 25 93 56
36 53 98 86
19 10 73 24
54 27 97 84
34 21 93 76
44 7 70 89
63 73 97 98
50 24 94 35
8 30 98 67
3 15 81 67
39 9 78 24
8 65 100 98
13 5 86 33
54 67 99 84
3 2 43 4
53 51 70 86
1 61 61 90
46 78 ...

output:

1
1
2
1
2
3
2
1
2
1
1
1
1
1
2
2
1
2
2
17
1
3
1
10
1
2
2
1
1
81
15
1
170
1
4
1
2
1
1
2
1
2
1
2
1
1
4
1
2
1
2
1
1
1
1
2
3511413
1
2
1
2
2
7022597
1
2
2
2
2
2
1
1
1
1
2
1
1
2
1
1
1
1
1
1
3
2
4
1
2
3
1
6
1
1
1
2
1
1
1
1
2
10
6
5
1
1
1
1
1
1
2
10
6
2
4
3
3511332
2
2
1
1
1
4
1
2
1
2
1
5
7
6
1
1
2
1
1
1
1
...

result:

ok 200 numbers

Test #11:

score: 0
Accepted
time: 5714ms
memory: 38780kb

input:

20
9956
444627993 347200064 774678572 839098978
96936514 121569633 839637347 729127099
343857875 554213034 875416430 628610537
15566043 187047055 405963021 764745923
487340755 59747358 604299543 832826844
486772462 67273090 826002755 268527861
703758831 112254125 887710074 874858855
569284980 830139...

output:

723891885
824191538
424987081
659035365
778857924
125675099
919713447
291966121
841813
399938856
822967409
178787426
958377822
295302425
835192235
569958073
114037901
814150865
893384876
929070768

result:

ok 20 numbers

Test #12:

score: -100
Time Limit Exceeded

input:

2
99774
247800693 19906287 955213467 53123614
752909581 205074868 772413424 686934334
565298662 150234672 941079197 750300220
29485217 794172007 678447605 874228231
524081254 14156413 775198532 256753797
121163010 271762780 489047491 996802646
61272893 135510320 459683721 975698463
37577668 16804082...

output:


result: