QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#535352#9228. ICPC InferencemaspyAC ✓371ms48356kbC++2032.1kb2024-08-27 23:46:302024-08-27 23:46:31

Judging History

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

  • [2024-08-27 23:46:31]
  • 评测
  • 测评结果:AC
  • 用时:371ms
  • 内存:48356kb
  • [2024-08-27 23:46:30]
  • 提交

answer

#line 1 "library/my_template.hpp"
#if defined(LOCAL)
#include <my_template_compiled.hpp>
#else

// https://codeforces.com/blog/entry/96344
#pragma GCC optimize("Ofast,unroll-loops")
// いまの CF だとこれ入れると動かない?
// #pragma GCC target("avx2,popcnt")

#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'010'000'000;
template <>
constexpr ll infty<ll> = 2'020'000'000'000'000'000;
template <>
constexpr u32 infty<u32> = infty<int>;
template <>
constexpr u64 infty<u64> = infty<ll>;
template <>
constexpr i128 infty<i128> = i128(infty<ll>) * 2'000'000'000'000'000'000;
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;
}

template <typename T, typename... Vectors>
void concat(vc<T> &first, const Vectors &... others) {
  vc<T> &res = first;
  (res.insert(res.end(), others.begin(), others.end()), ...);
}
#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;

#if defined(LOCAL)
#define SHOW(...) SHOW_IMPL(__VA_ARGS__, SHOW6, SHOW5, SHOW4, SHOW3, SHOW2, SHOW1)(__VA_ARGS__)
#define SHOW_IMPL(_1, _2, _3, _4, _5, _6, NAME, ...) NAME
#define SHOW1(x) print(#x, "=", (x)), flush()
#define SHOW2(x, y) print(#x, "=", (x), #y, "=", (y)), flush()
#define SHOW3(x, y, z) print(#x, "=", (x), #y, "=", (y), #z, "=", (z)), flush()
#define SHOW4(x, y, z, w) print(#x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w)), flush()
#define SHOW5(x, y, z, w, v) print(#x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w), #v, "=", (v)), flush()
#define SHOW6(x, y, z, w, v, u) print(#x, "=", (x), #y, "=", (y), #z, "=", (z), #w, "=", (w), #v, "=", (v), #u, "=", (u)), flush()
#else
#define SHOW(...)
#endif

#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 1 "library/ds/bit_vector.hpp"
struct Bit_Vector {
  int n;
  bool prepared = 0;
  vc<pair<u64, u32>> dat;
  Bit_Vector(int n) : n(n) { dat.assign((n + 127) >> 6, {0, 0}); }
  void set(int i) {
    assert(!prepared);
    dat[i >> 6].fi |= u64(1) << (i & 63);
  }
  void reset() {
    fill(all(dat), pair<u64, u32>{0, 0});
    prepared = 0;
  }
  void build() {
    prepared = 1;
    FOR(i, len(dat) - 1) dat[i + 1].se = dat[i].se + popcnt(dat[i].fi);
  }
  // [0, k) 内の 1 の個数
  bool operator[](int i) { return dat[i >> 6].fi >> (i & 63) & 1; }
  int count_prefix(int k, bool f = true) {
    assert(prepared);
    auto [a, b] = dat[k >> 6];
    int ret = b + popcnt(a & ((u64(1) << (k & 63)) - 1));
    return (f ? ret : k - ret);
  }
  int count(int L, int R, bool f = true) { return count_prefix(R, f) - count_prefix(L, f); }
  string to_string() {
    string ans;
    FOR(i, n) ans += '0' + (dat[i / 64].fi >> (i % 64) & 1);
    return ans;
  }
};
#line 1 "library/ds/index_compression.hpp"
template <typename T>
struct Index_Compression_DISTINCT_SMALL {
  static_assert(is_same_v<T, int>);
  int mi, ma;
  vc<int> dat;
  vc<int> build(vc<int> X) {
    mi = 0, ma = -1;
    if (!X.empty()) mi = MIN(X), ma = MAX(X);
    dat.assign(ma - mi + 2, 0);
    for (auto& x: X) dat[x - mi + 1]++;
    FOR(i, len(dat) - 1) dat[i + 1] += dat[i];
    for (auto& x: X) { x = dat[x - mi]++; }
    FOR_R(i, 1, len(dat)) dat[i] = dat[i - 1];
    dat[0] = 0;
    return X;
  }
  int operator()(ll x) { return dat[clamp<ll>(x - mi, 0, ma - mi + 1)]; }
};

template <typename T>
struct Index_Compression_SAME_SMALL {
  static_assert(is_same_v<T, int>);
  int mi, ma;
  vc<int> dat;
  vc<int> build(vc<int> X) {
    mi = 0, ma = -1;
    if (!X.empty()) mi = MIN(X), ma = MAX(X);
    dat.assign(ma - mi + 2, 0);
    for (auto& x: X) dat[x - mi + 1] = 1;
    FOR(i, len(dat) - 1) dat[i + 1] += dat[i];
    for (auto& x: X) { x = dat[x - mi]; }
    return X;
  }
  int operator()(ll x) { return dat[clamp<ll>(x - mi, 0, ma - mi + 1)]; }
};

template <typename T>
struct Index_Compression_SAME_LARGE {
  vc<T> dat;
  vc<int> build(vc<T> X) {
    vc<int> I = argsort(X);
    vc<int> res(len(X));
    for (auto& i: I) {
      if (!dat.empty() && dat.back() == X[i]) {
        res[i] = len(dat) - 1;
      } else {
        res[i] = len(dat);
        dat.eb(X[i]);
      }
    }
    dat.shrink_to_fit();
    return res;
  }
  int operator()(T x) { return LB(dat, x); }
};

template <typename T>
struct Index_Compression_DISTINCT_LARGE {
  vc<T> dat;
  vc<int> build(vc<T> X) {
    vc<int> I = argsort(X);
    vc<int> res(len(X));
    for (auto& i: I) { res[i] = len(dat), dat.eb(X[i]); }
    dat.shrink_to_fit();
    return res;
  }
  int operator()(T x) { return LB(dat, x); }
};

template <typename T, bool SMALL>
using Index_Compression_DISTINCT =
    typename std::conditional<SMALL, Index_Compression_DISTINCT_SMALL<T>,
                              Index_Compression_DISTINCT_LARGE<T>>::type;
template <typename T, bool SMALL>
using Index_Compression_SAME =
    typename std::conditional<SMALL, Index_Compression_SAME_SMALL<T>,
                              Index_Compression_SAME_LARGE<T>>::type;

// SAME: [2,3,2] -> [0,1,0]
// DISTINCT: [2,2,3] -> [0,2,1]
// (x): lower_bound(X,x) をかえす
template <typename T, bool SAME, bool SMALL>
using Index_Compression =
    typename std::conditional<SAME, Index_Compression_SAME<T, SMALL>,
                              Index_Compression_DISTINCT<T, SMALL>>::type;
#line 2 "library/alg/monoid/add.hpp"

template <typename E>
struct Monoid_Add {
  using X = E;
  using value_type = X;
  static constexpr X op(const X &x, const X &y) noexcept { return x + y; }
  static constexpr X inverse(const X &x) noexcept { return -x; }
  static constexpr X power(const X &x, ll n) noexcept { return X(n) * x; }
  static constexpr X unit() { return X(0); }
  static constexpr bool commute = true;
};
#line 4 "library/ds/wavelet_matrix/wavelet_matrix.hpp"

// 静的メソッドinverseの存在をチェックするテンプレート
template <typename, typename = std::void_t<>>
struct has_inverse : std::false_type {};

template <typename T>
struct has_inverse<T, std::void_t<decltype(
                          T::inverse(std::declval<typename T::value_type>()))>>
    : std::true_type {};

struct Dummy_Data_Structure {
  using MX = Monoid_Add<bool>;
  void build(const vc<bool>& A) {}
};

template <typename Y, bool SMALL_Y, typename SEGTREE = Dummy_Data_Structure>
struct Wavelet_Matrix {
  using Mono = typename SEGTREE::MX;
  using T = typename Mono::value_type;
  static_assert(Mono::commute);

  int n, log, K;
  Index_Compression<Y, true, SMALL_Y> IDX;
  vc<Y> ItoY;
  vc<int> mid;
  vc<Bit_Vector> bv;
  vc<SEGTREE> seg;

  Wavelet_Matrix() {}
  Wavelet_Matrix(const vc<Y>& A) { build(A); }
  Wavelet_Matrix(const vc<Y>& A, vc<T>& SUM_Data) { build(A, SUM_Data); }
  template <typename F>
  Wavelet_Matrix(int n, F f) {
    build(n, f);
  }

  template <typename F>
  void build(int m, F f) {
    vc<Y> A(m);
    vc<T> S(m);
    for (int i = 0; i < m; ++i) tie(A[i], S[i]) = f(i);
    build(A, S);
  }

  void build(const vc<Y>& A) { build(A, vc<T>(len(A), Mono::unit())); }
  void build(const vc<Y>& A, vc<T> S) {
    n = len(A);
    vc<int> B = IDX.build(A);
    K = 0;
    for (auto& x: B) chmax(K, x + 1);
    ItoY.resize(K);
    FOR(i, n) ItoY[B[i]] = A[i];
    log = 0;
    while ((1 << log) < K) ++log;
    mid.resize(log), bv.assign(log, Bit_Vector(n));
    vc<int> B0(n), B1(n);
    vc<T> S0(n), S1(n);
    seg.resize(log + 1);
    seg[log].build(S);
    for (int d = log - 1; d >= 0; --d) {
      int p0 = 0, p1 = 0;
      for (int i = 0; i < n; ++i) {
        bool f = (B[i] >> d & 1);
        if (!f) { B0[p0] = B[i], S0[p0] = S[i], p0++; }
        if (f) { bv[d].set(i), B1[p1] = B[i], S1[p1] = S[i], p1++; }
      }
      swap(B, B0), swap(S, S0);
      move(B1.begin(), B1.begin() + p1, B.begin() + p0);
      move(S1.begin(), S1.begin() + p1, S.begin() + p0);
      mid[d] = p0, bv[d].build(), seg[d].build(S);
    }
  }

  // [L,R) x [0,y)
  int prefix_count(int L, int R, Y y) {
    int p = IDX(y);
    if (L == R || p == 0) return 0;
    if (p == K) return R - L;
    int cnt = 0;
    for (int d = log - 1; d >= 0; --d) {
      int l0 = bv[d].count_prefix(L, 0), r0 = bv[d].count_prefix(R, 0);
      int l1 = L + mid[d] - l0, r1 = R + mid[d] - r0;
      if (p >> d & 1) cnt += r0 - l0, L = l1, R = r1;
      if (!(p >> d & 1)) L = l0, R = r0;
    }
    return cnt;
  }

  // [L,R) x [y1,y2)
  int count(int L, int R, Y y1, Y y2) {
    return prefix_count(L, R, y2) - prefix_count(L, R, y1);
  }

  // [L,R) x [0,y)
  pair<int, T> prefix_count_and_prod(int L, int R, Y y) {
    int p = IDX(y);
    if (p == 0) return {0, Mono::unit()};
    if (p == K) return {R - L, seg[log].prod(L, R)};
    int cnt = 0;
    T t = Mono::unit();
    for (int d = log - 1; d >= 0; --d) {
      int l0 = bv[d].count_prefix(L, 0), r0 = bv[d].count_prefix(R, 0);
      int l1 = L + mid[d] - l0, r1 = R + mid[d] - r0;
      if (p >> d & 1) {
        cnt += r0 - l0, t = Mono::op(t, seg[d].prod(l0, r0)), L = l1, R = r1;
      }
      if (!(p >> d & 1)) L = l0, R = r0;
    }
    return {cnt, t};
  }

  // [L,R) x [y1,y2)
  pair<int, T> count_and_prod(int L, int R, Y y1, Y y2) {
    if constexpr (has_inverse<Mono>::value) {
      auto [c1, t1] = prefix_count_and_prod(L, R, y1);
      auto [c2, t2] = prefix_count_and_prod(L, R, y2);
      return {c2 - c1, Mono::op(Mono::inverse(t1), t2)};
    }
    int lo = IDX(y1), hi = IDX(y2), cnt = 0;
    T t = Mono::unit();
    auto dfs = [&](auto& dfs, int d, int L, int R, int a, int b) -> void {
      assert(b - a == (1 << d));
      if (hi <= a || b <= lo) return;
      if (lo <= a && b <= hi) {
        cnt += R - L, t = Mono::op(t, seg[d].prod(L, R));
        return;
      }
      --d;
      int c = (a + b) / 2;
      int l0 = bv[d].count_prefix(L, 0), r0 = bv[d].count_prefix(R, 0);
      int l1 = L + mid[d] - l0, r1 = R + mid[d] - r0;
      dfs(dfs, d, l0, r0, a, c), dfs(dfs, d, l1, r1, c, b);
    };
    dfs(dfs, log, L, R, 0, 1 << log);
    return {cnt, t};
  }

  // [L,R) x [y1,y2)
  T prefix_prod(int L, int R, Y y) { return prefix_count_and_prod(L, R, y).se; }
  // [L,R) x [y1,y2)
  T prod(int L, int R, Y y1, Y y2) { return count_and_prod(L, R, y1, y2).se; }
  T prod_all(int L, int R) { return seg[log].prod(L, R); }

  Y kth(int L, int R, int k) {
    assert(0 <= k && k < R - L);
    int p = 0;
    for (int d = log - 1; d >= 0; --d) {
      int l0 = bv[d].count_prefix(L, 0), r0 = bv[d].count_prefix(R, 0);
      int l1 = L + mid[d] - l0, r1 = R + mid[d] - r0;
      if (k < r0 - l0) {
        L = l0, R = r0;
      } else {
        k -= r0 - l0, L = l1, R = r1, p |= 1 << d;
      }
    }
    return ItoY[p];
  }

  // y 以上最小 OR infty<Y>
  Y next(int L, int R, Y y) {
    int k = IDX(y);
    int p = K;

    auto dfs = [&](auto& dfs, int d, int L, int R, int a, int b) -> void {
      if (p <= a || L == R || b <= k) return;
      if (d == 0) {
        chmin(p, a);
        return;
      }
      --d;
      int c = (a + b) / 2;
      int l0 = bv[d].count_prefix(L, 0), r0 = bv[d].count_prefix(R, 0);
      int l1 = L + mid[d] - l0, r1 = R + mid[d] - r0;
      dfs(dfs, d, l0, r0, a, c), dfs(dfs, d, l1, r1, c, b);
    };
    dfs(dfs, log, L, R, 0, 1 << log);
    return (p == K ? infty<Y> : ItoY[p]);
  }

  // y 以下最大 OR -infty<T>
  Y prev(int L, int R, Y y) {
    int k = IDX(y + 1);
    int p = -1;
    auto dfs = [&](auto& dfs, int d, int L, int R, int a, int b) -> void {
      if (b - 1 <= p || L == R || k <= a) return;
      if (d == 0) {
        chmax(p, a);
        return;
      }
      --d;
      int c = (a + b) / 2;
      int l0 = bv[d].count_prefix(L, 0), r0 = bv[d].count_prefix(R, 0);
      int l1 = L + mid[d] - l0, r1 = R + mid[d] - r0;
      dfs(dfs, d, l1, r1, c, b), dfs(dfs, d, l0, r0, a, c);
    };
    dfs(dfs, log, L, R, 0, 1 << log);
    return (p == -1 ? -infty<Y> : ItoY[p]);
  }

  Y median(bool UPPER, int L, int R) {
    assert(0 <= L && L < R && R <= n);
    int k = (UPPER ? (R - L) / 2 : (R - L - 1) / 2);
    return kth(L, R, k);
  }

  pair<Y, T> kth_value_and_prod(int L, int R, int k) {
    assert(0 <= k && k <= R - L);
    if (k == R - L) return {infty<Y>, seg[log].prod(L, R)};
    int p = 0;
    T t = Mono::unit();
    for (int d = log - 1; d >= 0; --d) {
      int l0 = bv[d].count_prefix(L, 0), r0 = bv[d].count_prefix(R, 0);
      int l1 = L + mid[d] - l0, r1 = R + mid[d] - r0;
      if (k < r0 - l0) {
        L = l0, R = r0;
      } else {
        t = Mono::op(t, seg[d].prod(l0, r0)), k -= r0 - l0, L = l1, R = r1,
        p |= 1 << d;
      }
    }
    t = Mono::op(t, seg[0].prod(L, L + k));
    return {ItoY[p], t};
  }

  T prod_index_range(int L, int R, int k1, int k2) {
    static_assert(has_inverse<Mono>::value);
    T t1 = kth_value_and_prod(L, R, k1).se;
    T t2 = kth_value_and_prod(L, R, k2).se;
    return Mono::op(Mono::inverse(t1), t2);
  }

  // [L,R) x [0,y) での check(cnt, prod) が true となる最大の (cnt,prod)
  template <typename F>
  pair<int, T> max_right(F check, int L, int R) {
    int cnt = 0;
    T t = Mono::unit();
    assert(check(0, Mono::unit()));
    if (check(R - L, seg[log].prod(L, R))) {
      return {R - L, seg[log].prod(L, R)};
    }
    for (int d = log - 1; d >= 0; --d) {
      int l0 = bv[d].count_prefix(L, 0), r0 = bv[d].count_prefix(R, 0);
      int l1 = L + mid[d] - l0, r1 = R + mid[d] - r0;
      int cnt1 = cnt + r0 - l0;
      T t1 = Mono::op(t, seg[d].prod(l0, r0));
      if (check(cnt1, t1)) {
        cnt = cnt1, t = t1, L = l1, R = r1;
      } else {
        L = l0, R = r0;
      }
    }
    return {cnt, t};
  }

  void set(int i, T t) {
    assert(0 <= i && i < n);
    int L = i, R = i + 1;
    seg[log].set(L, t);
    for (int d = log - 1; d >= 0; --d) {
      int l0 = bv[d].count_prefix(L, 0), r0 = bv[d].count_prefix(R, 0);
      int l1 = L + mid[d] - l0, r1 = R + mid[d] - r0;
      if (l0 < r0) L = l0, R = r0;
      if (l0 == r0) L = l1, R = r1;
      seg[d].set(L, t);
    }
  }
  void multiply(int i, T t) {
    assert(0 <= i && i < n);
    int L = i, R = i + 1;
    seg[log].multiply(L, t);
    for (int d = log - 1; d >= 0; --d) {
      int l0 = bv[d].count_prefix(L, 0), r0 = bv[d].count_prefix(R, 0);
      int l1 = L + mid[d] - l0, r1 = R + mid[d] - r0;
      if (l0 < r0) L = l0, R = r0;
      if (l0 == r0) L = l1, R = r1;
      seg[d].multiply(L, t);
    }
  }
};
#line 2 "library/ds/wavelet_matrix/wavelet_matrix_2d_range.hpp"

template <typename XY, bool SMALL_X, bool SMALL_Y,
          typename SEGTREE = Dummy_Data_Structure>
struct Wavelet_Matrix_2D_Range {
  // 点群を X 昇順に並べる.
  Wavelet_Matrix<XY, SMALL_Y, SEGTREE> WM;
  using Mono = typename SEGTREE::MX;
  using T = typename Mono::value_type;
  static_assert(Mono::commute);

  Index_Compression<XY, false, SMALL_X> IDX_X;

  int n;
  vc<int> new_idx;

  template <typename F>
  Wavelet_Matrix_2D_Range(int n, F f) {
    build(n, f);
  }

  template <typename F>
  void build(int m, F f) {
    n = m;
    vc<XY> X(n), Y(n);
    vc<T> S(n);
    FOR(i, n) {
      auto tmp = f(i);
      X[i] = get<0>(tmp), Y[i] = get<1>(tmp), S[i] = get<2>(tmp);
    }
    new_idx = IDX_X.build(X);
    vc<int> I(n);
    FOR(i, n) I[new_idx[i]] = i;
    Y = rearrange(Y, I);
    S = rearrange(S, I);
    WM.build(Y, S);
  }

  int count(XY x1, XY x2, XY y1, XY y2) {
    return WM.count(IDX_X(x1), IDX_X(x2), y1, y2);
  }

  // [L,R) x [-inf,y)
  pair<int, T> prefix_count_and_prod(XY x1, XY x2, XY y) {
    return WM.prefix_count_and_prod(IDX_X(x1), IDX_X(x2), y);
  }

  // [L,R) x [y1,y2)
  pair<int, T> count_and_prod(XY x1, XY x2, XY y1, XY y2) {
    return WM.count_and_prod(IDX_X(x1), IDX_X(x2), y1, y2);
  }

  // [L,R) x [-inf,inf)
  T prod_all(XY x1, XY x2) { return WM.prod_all(IDX_X(x1), IDX_X(x2)); }
  // [L,R) x [-inf,y)
  T prefix_prod(XY x1, XY x2, XY y) {
    return WM.prefix_prod(IDX_X(x1), IDX_X(x2), y);
  }
  // [L,R) x [y1,y2)
  T prod(XY x1, XY x2, XY y1, XY y2) {
    return WM.prod(IDX_X(x1), IDX_X(x2), y1, y2);
  }

  // [L,R) x [-inf,y) での check(cnt, prod) が true となる最大の (cnt,prod)
  template <typename F>
  pair<int, T> max_right(F check, XY x1, XY x2) {
    return WM.max_right(check, IDX_X(x1), IDX_X(x2));
  }

  // i は最初に渡したインデックス
  void set(int i, T t) { WM.set(new_idx[i], t); }
  // i は最初に渡したインデックス
  void multiply(int i, T t) { WM.multiply(new_idx[i], t); }
};
#line 5 "main.cpp"

constexpr ll thresh = 200;

void solve() {
  LL(N, D, L);
  vvc<ll> dat(D);
  FOR(N) {
    LL(i, x);
    --i;
    dat[i].eb(x);
  }
  {
    vvc<ll> tmp;
    for (auto& x: dat) {
      if (x.empty()) continue;
      tmp.eb(x);
    }
    swap(dat, tmp);
    N = len(dat);
  }

  ll K = L + 100;

  vc<ll> best(N), worst(N);
  auto to_ll = [&](ll AC, ll t) -> ll {
    if (AC == 3) return 0;
    if (AC == 2) return 1 + min(t, 2 * K - 1);
    assert(AC == 1);
    return 1 + 2 * K + min(t, K - 1);
  };
  FOR(i, N) {
    auto& A = dat[i];
    ll n = len(A);
    if (n >= 3) { best[i] = to_ll(3, 0); }
    elif (n == 2) { best[i] = to_ll(2, A[0] + A[1]); }
    elif (n == 1) { best[i] = to_ll(1, A[0]); }
    ll t = A.back();
    t += 20 * (n - 1);
    worst[i] = to_ll(1, t);
  }
  SHOW(best);
  SHOW(worst);

  ll mx = 3 * K + 1;

  vi Cb(mx + 1);
  vi Cw(mx + 1);
  for (auto& x: best) Cb[x]++;
  for (auto& x: worst) Cw[x]++;
  Cb = cumsum<ll>(Cb);
  Cw = cumsum<ll>(Cw);

  auto gen_all_B = [&](ll i) -> vi {
    SHOW(__LINE__);
    auto& A = dat[i];
    vi ANS;
    ll n = len(A);
    if (n <= thresh) {
      SHOW(__LINE__);
      // 2 完最遅
      if (n >= 2) {
        ll t = A[n - 1] + A[n - 2] + 20 * (n - 2);
        ANS.eb(to_ll(2, t));
      }
      SHOW(__LINE__);
      // 1 完
      FOR(i, n) { FOR(j, i + 1) ANS.eb(to_ll(1, A[i] + j * 20)); }
      UNIQUE(ANS);
      SHOW(__LINE__);
      return ANS;
    }
    SHOW(__LINE__);
    // n が大きい
    // 等差数列加算 みたいなことをやる
    if (n >= 2) {
      ll t = A[n - 1] + A[n - 2] + 20 * (n - 2);
      ANS.eb(to_ll(2, t));
    }
    // 20刻みでの累積和を利用
    vi F(K + 20);
    FOR(i, n) {
      ll a = A[i];
      ll b = a + i * 20;
      if (b >= K) {
        ll k = ceil<ll>(b - K + 1, 20);
        b -= 20 * k;
      }
      assert(a <= b);
      F[a]++;
      F[b + 20]--;
    }
    FOR(k, K) { F[k + 20] += F[k]; }
    FOR(x, K) if (F[x]) ANS.eb(to_ll(1, x));

    return ANS;
  };

  vi JUST(mx + 1);
  SHOW(__LINE__);

  ll ANS = 0;
  FOR(b, N) {
    auto B = gen_all_B(b);
    SHOW(b, B);
    SHOW(__LINE__);
    for (auto& x: B) JUST[x]++;
    SHOW(__LINE__);
    ll p = 0;
    FOR(i, len(B)) {
      ll q = B[i] + 1;
      // a in [p, q)
      ll NA = Cb[q] - Cb[p];
      if (p <= best[b] && best[b] < q) --NA;
      SHOW(__LINE__);
      ll NC = Cw[mx + 1] - Cw[B[i]];
      SHOW(__LINE__);
      if (B[i] <= worst[b]) --NC;
      ANS += NA * NC;
      p = q;
    }
    SHOW(b, ANS);
  }

  SHOW(ANS);
  SHOW(__LINE__);

  vi sub(N);

  // - |A|>=3: すべての B が該当する
  // うそ
  // best(B)<=worst(A) であるようなものがすべて該当
  FOR(a, N) {
    if (len(dat[a]) >= 3) { sub[a] += Cb[worst[a] + 1] - 1; }
  }
  /*
  - |A|==2 かつ |B|>=2
      - best(A) <= B の 2 完最遅 ならば満たす
      - (B の 2 完最遅) < best(A) かつ (B の 1 完最速) <= worst(A) ならば満たす
      - 長方形の点を数える問題
  */

  {
    vc<pair<int, int>> point;
    FOR(b, N) {
      if (len(dat[b]) == 1) continue;
      // 2 完最遅
      auto& A = dat[b];
      ll n = len(A);
      ll t = A[n - 1] + A[n - 2] + 20 * (n - 2);
      ll x = to_ll(2, t);
      ll y = A[0];
      point.eb(x, y);
    }
    Wavelet_Matrix_2D_Range<int, 1, 1> WM(len(point), [&](int i) -> tuple<int, int, int> {
      auto [x, y] = point[i];
      return {x, y, 0};
    });
    FOR(a, N) {
      auto& A = dat[a];
      if (len(A) != 2) continue;
      ll ans = 0;
      ans += WM.count(best[a], mx + 1, 0, mx + 1);
      ans += WM.count(0, best[a], 0, worst[a] + 1);
      ans -= 1;
      sub[a] += ans;
    }
  }
  // - |A|==2 かつ |B|==1
  {
    vi F(mx + 1);
    FOR(b, N) {
      auto& A = dat[b];
      if (len(A) == 1) F[best[b]]++;
    }
    F = cumsum<ll>(F);
    FOR(a, N) {
      if (len(dat[a]) == 2) sub[a] += F[worst[a] + 1];
    }
  }
  // - |A|==1
  FOR(a, N) {
    if (len(dat[a]) != 1) continue;
    ll x = best[a];
    sub[a] += JUST[x] - 1;
  }

  SHOW(sub);

  ANS -= SUM<ll>(sub);
  print(ANS);
}

signed main() { solve(); }

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

4 3 300
1 10
2 25
2 30
3 50

output:

3

result:

ok 1 number(s): "3"

Test #2:

score: 0
Accepted
time: 3ms
memory: 26572kb

input:

4 6 200000
6 1
6 1
1 2
2 2

output:

4

result:

ok 1 number(s): "4"

Test #3:

score: 0
Accepted
time: 28ms
memory: 12712kb

input:

191580 64997 56
24878 1
35060 1
24245 1
64330 1
9650 1
15423 1
34953 1
21456 1
36718 1
21395 1
17613 1
16995 1
45257 1
31277 1
20026 1
1870 1
25581 1
9997 1
54701 1
30752 1
32269 1
705 1
64186 1
58881 1
24614 1
55311 1
18259 1
58886 1
23296 1
17628 1
3411 1
37469 1
47951 1
12188 1
60720 1
54168 1
45...

output:

274533940012863

result:

ok 1 number(s): "274533940012863"

Test #4:

score: 0
Accepted
time: 29ms
memory: 16232kb

input:

192309 96431 357
56446 1
42487 1
47313 1
71736 1
74439 1
19895 1
52024 1
66203 1
992 1
78744 1
9911 1
85130 1
73814 1
64499 1
92961 1
66255 1
88807 1
82217 1
36788 1
66999 1
35107 1
47933 1
34196 1
50534 1
83014 1
75035 1
30407 1
36014 1
7248 1
69915 1
57348 1
5356 1
37088 1
36455 1
29365 1
71376 1
...

output:

868523468626161

result:

ok 1 number(s): "868523468626161"

Test #5:

score: 0
Accepted
time: 213ms
memory: 31120kb

input:

200000 200000 200000
151464 4
1477 6
95966 8
121582 8
19239 11
668 13
109329 15
173254 15
153807 16
75865 16
123829 17
101156 17
8881 18
116717 18
124985 18
125918 18
132143 19
35899 20
90547 20
106065 22
176481 23
11727 23
527 24
77206 25
85757 25
169873 26
139187 26
5970 28
37134 29
199855 30
9598...

output:

149096043

result:

ok 1 number(s): "149096043"

Test #6:

score: 0
Accepted
time: 4ms
memory: 29900kb

input:

200000 200000 200000
200000 200000
200000 200000
200000 200000
200000 200000
200000 200000
200000 200000
200000 200000
200000 200000
200000 200000
200000 200000
200000 200000
200000 200000
200000 200000
200000 200000
200000 200000
200000 200000
200000 200000
200000 200000
200000 200000
200000 200000...

output:

0

result:

ok 1 number(s): "0"

Test #7:

score: 0
Accepted
time: 41ms
memory: 38428kb

input:

200000 200000 200000
80855 2
16643 3
158423 4
160007 6
83405 9
148393 10
94889 10
146919 11
56648 12
60318 12
182241 13
144187 14
96195 16
72396 16
10048 17
32575 19
75743 21
49152 21
21380 22
64386 28
11608 29
49440 30
125557 35
170781 36
5487 37
104466 37
136650 37
84688 38
38173 40
122020 40
9383...

output:

689247584152428

result:

ok 1 number(s): "689247584152428"

Test #8:

score: 0
Accepted
time: 87ms
memory: 31612kb

input:

200000 28000 200000
5152 1
5152 1
26010 1
12173 1
12173 1
12166 1
26010 1
24704 1
12173 1
24704 1
26010 1
12071 1
12173 1
12173 1
12166 1
26010 1
24704 1
12166 1
6044 1
24704 1
6044 1
12173 1
24704 1
6733 1
12173 1
12166 1
12173 1
12166 1
24704 1
12166 1
24704 1
12166 1
5152 1
12166 1
12166 1
26010 ...

output:

13273777158527

result:

ok 1 number(s): "13273777158527"

Test #9:

score: 0
Accepted
time: 0ms
memory: 26764kb

input:

4 6 200000
6 1
6 1
1 2
2 2

output:

4

result:

ok 1 number(s): "4"

Test #10:

score: 0
Accepted
time: 39ms
memory: 34308kb

input:

199999 200000 199995
22477 1
124061 1
102846 2
124061 2
124061 3
124061 3
124061 4
124061 5
59212 5
169893 5
124061 6
80004 6
112429 7
31273 11
124061 12
67631 12
124061 14
10017 15
124061 16
70773 16
124061 17
168853 18
88973 19
124061 19
61672 19
196994 20
48373 21
113531 22
92390 23
152850 25
998...

output:

150416508568041

result:

ok 1 number(s): "150416508568041"

Test #11:

score: 0
Accepted
time: 3ms
memory: 26796kb

input:

3 199993 199995
165540 12988
2883 39410
66825 115392

output:

1

result:

ok 1 number(s): "1"

Test #12:

score: 0
Accepted
time: 26ms
memory: 13312kb

input:

193973 65702 62
51610 1
53512 1
12563 1
56075 1
29395 1
42082 1
60371 1
17038 1
29443 1
33664 1
12471 1
34225 1
49958 1
54960 1
59860 1
33819 1
7499 1
34862 1
6704 1
52200 1
22803 1
7726 1
61422 1
51120 1
17203 1
11462 1
13292 1
20641 1
21050 1
28979 1
35347 1
55821 1
52326 1
50557 1
8296 1
53862 1
...

output:

283586156841885

result:

ok 1 number(s): "283586156841885"

Test #13:

score: 0
Accepted
time: 35ms
memory: 32488kb

input:

199975 199975 100000
27676 1
116023 2
40052 2
154967 2
82099 3
2503 4
159303 5
136744 5
89330 6
117095 6
182013 6
131786 7
180992 7
73734 7
122549 8
16427 8
104713 8
46057 9
63743 9
160535 10
109106 11
135371 12
65002 13
194754 14
15088 15
144270 15
106306 15
84597 16
143308 16
67042 16
103147 17
15...

output:

1332840068163275

result:

ok 1 number(s): "1332840068163275"

Test #14:

score: 0
Accepted
time: 313ms
memory: 31132kb

input:

200000 312 200000
155 1
241 1
93 1
157 1
308 1
7 1
148 1
240 1
172 1
77 1
151 1
141 1
150 1
190 1
226 1
265 1
247 1
171 1
251 1
115 1
164 1
185 1
234 1
176 1
63 1
21 1
107 1
132 1
224 1
293 1
80 1
162 1
113 1
243 1
287 1
104 1
298 1
197 1
270 1
6 1
252 1
289 1
2 1
160 1
229 1
116 1
165 1
216 1
159 1...

output:

30079920

result:

ok 1 number(s): "30079920"

Test #15:

score: 0
Accepted
time: 4ms
memory: 28956kb

input:

24 16 200000
6 1
5 2
9 3
6 3
6 3
1 4
15 12
11 13
15 13
14 13
11 13
14 14
12 22
8 23
16 24
10 42
2 43
4 44
13 199998
13 199998
13 199998
7 199999
7 199999
3 200000

output:

1479

result:

ok 1 number(s): "1479"

Test #16:

score: 0
Accepted
time: 33ms
memory: 40740kb

input:

199998 140790 200000
20382 1
75175 1
1261 1
30469 1
8689 1
134949 1
10677 1
33222 1
42480 1
80518 1
111286 1
125548 1
78740 1
1530 1
98335 1
76194 1
56788 1
113516 1
101478 1
101211 1
112396 1
62315 1
119913 1
70262 1
16488 1
135245 1
24429 1
17435 1
61254 1
98349 1
11844 1
15041 1
11510 1
71156 1
6...

output:

1101885707898743

result:

ok 1 number(s): "1101885707898743"

Test #17:

score: 0
Accepted
time: 29ms
memory: 11620kb

input:

196811 51280 24
17192 1
25079 1
27464 1
39867 1
1832 1
17482 1
4693 1
4819 1
15064 1
39031 1
24107 1
29585 1
6585 1
33020 1
47567 1
13253 1
39048 1
1418 1
25729 1
45804 1
24904 1
14061 1
4574 1
28867 1
48344 1
9938 1
27903 1
16751 1
23071 1
21052 1
36834 1
45839 1
8894 1
8677 1
34756 1
19157 1
40181...

output:

134808013718613

result:

ok 1 number(s): "134808013718613"

Test #18:

score: 0
Accepted
time: 5ms
memory: 29060kb

input:

13 10 200000
10 1
10 3
10 3
1 20
7 21
5 22
9 62
8 63
3 64
4 199979
4 199979
2 200000
2 200000

output:

184

result:

ok 1 number(s): "184"

Test #19:

score: 0
Accepted
time: 1ms
memory: 3728kb

input:

1000 514 100
276 1
169 1
267 1
386 1
305 1
301 1
190 1
392 1
22 2
93 2
3 2
245 2
327 2
317 2
425 2
292 2
252 2
212 2
377 2
255 3
108 3
352 3
399 3
369 3
45 3
282 3
236 3
371 3
434 3
439 3
71 3
231 3
314 3
7 3
434 3
389 4
87 4
57 4
313 4
219 4
381 4
222 5
492 5
194 5
148 5
186 5
21 5
185 5
170 6
394 ...

output:

102673572

result:

ok 1 number(s): "102673572"

Test #20:

score: 0
Accepted
time: 167ms
memory: 34596kb

input:

200000 114052 200000
95 1
55490 2
61481 2
29754 3
104570 5
97903 6
89479 11
88722 13
54292 13
5540 16
31838 17
48936 19
112373 20
48291 20
90401 22
42152 23
30304 26
8433 28
66247 28
49887 32
80338 33
57507 39
65209 42
37033 45
9055 45
93908 49
11908 50
48143 51
108424 51
107522 55
106177 56
51263 5...

output:

88358595246486

result:

ok 1 number(s): "88358595246486"

Test #21:

score: 0
Accepted
time: 0ms
memory: 4008kb

input:

4 3 300
1 10
2 25
2 30
3 50

output:

3

result:

ok 1 number(s): "3"

Test #22:

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

input:

200000 200000 200000
94701 3
156689 6
61741 6
155743 8
107012 8
169644 9
92832 11
120561 11
121290 12
831 13
152158 16
196171 16
128201 18
132493 19
81786 21
69528 21
18357 22
47647 22
168114 23
52100 23
151112 23
128866 25
119837 26
12669 30
184846 31
168755 32
29744 33
138894 41
191980 41
89643 42...

output:

1333333381066300

result:

ok 1 number(s): "1333333381066300"

Test #23:

score: 0
Accepted
time: 0ms
memory: 4004kb

input:

3 3 3
1 1
1 1
1 1

output:

0

result:

ok 1 number(s): "0"

Test #24:

score: 0
Accepted
time: 41ms
memory: 37468kb

input:

200000 200000 200000
59008 1
52339 3
56532 4
39193 4
36351 4
196219 4
121276 4
171679 6
59990 7
173361 8
13495 8
34421 9
194365 9
152300 10
122594 10
31107 11
24540 11
14394 11
147019 12
148917 15
124501 16
119218 17
9372 18
37864 18
57690 22
127336 22
15616 22
95247 23
100393 24
177953 27
17061 28
...

output:

962377606544272

result:

ok 1 number(s): "962377606544272"

Test #25:

score: 0
Accepted
time: 1ms
memory: 3880kb

input:

1024 146 997
54 1
101 1
76 1
104 1
130 1
141 1
8 1
131 1
54 1
54 1
122 1
134 1
45 1
100 1
8 1
44 1
68 2
104 2
130 2
76 2
44 2
141 2
141 2
68 2
131 2
54 2
101 2
122 2
100 2
143 2
8 2
68 2
51 2
44 2
134 2
8 2
8 3
36 3
131 3
76 3
8 3
54 3
122 3
134 3
100 3
128 3
101 3
68 3
8 3
141 3
68 3
68 3
117 3
36 ...

output:

1948646

result:

ok 1 number(s): "1948646"

Test #26:

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

input:

200000 200000 200000
35797 200000
167458 200000
39806 200000
88546 200000
165714 200000
117657 200000
168133 200000
81774 200000
140104 200000
176682 200000
188272 200000
177500 200000
2880 200000
76995 200000
124498 200000
198682 200000
25866 200000
189009 200000
187674 200000
189285 200000
63188 2...

output:

7999880000400000

result:

ok 1 number(s): "7999880000400000"

Test #27:

score: 0
Accepted
time: 371ms
memory: 31712kb

input:

200000 200000 200000
89814 1
116496 1
118564 1
464 1
126025 1
89571 2
197312 2
9858 2
85590 5
65225 8
148868 10
129076 11
103169 11
73239 12
142418 12
116100 13
46404 13
63424 14
66422 17
146171 17
145610 17
56698 17
180250 18
148868 19
129755 19
139751 20
154900 20
186629 21
62893 21
85249 21
71296...

output:

1188676324

result:

ok 1 number(s): "1188676324"

Test #28:

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

input:

199995 199993 3
139758 1
105092 1
116815 1
155810 1
117619 1
32962 1
165462 1
75347 1
58622 1
83533 1
198966 1
158315 1
157783 1
178123 1
56804 1
22718 1
135200 1
189569 1
5238 1
160413 1
85655 1
23921 1
83704 1
106882 1
38863 1
105112 1
65676 1
113998 1
29107 1
167276 1
124461 1
173183 1
110513 1
1...

output:

1434302202794094

result:

ok 1 number(s): "1434302202794094"

Test #29:

score: 0
Accepted
time: 28ms
memory: 33208kb

input:

197710 51514 199999
18607 199976
11648 199976
48300 199976
36251 199976
20168 199976
39939 199976
49063 199976
40145 199976
15002 199976
17993 199976
14752 199976
19829 199976
44936 199976
42612 199976
16625 199976
2013 199976
41690 199976
11181 199976
45441 199976
25242 199976
15854 199976
2123 199...

output:

136662051779304

result:

ok 1 number(s): "136662051779304"

Test #30:

score: 0
Accepted
time: 346ms
memory: 31156kb

input:

200000 74614 200000
22630 1
27567 1
60945 1
15633 1
3882 1
47524 1
45604 1
36741 1
63680 1
66142 1
60811 1
45697 1
9511 1
25248 1
35203 1
40929 1
35698 1
13641 1
46717 1
11294 1
54187 1
5471 1
25873 1
25207 1
20331 1
61719 1
68557 1
69570 1
61108 1
26112 1
1772 1
56132 1
11287 1
6368 1
13528 1
63069...

output:

135796230

result:

ok 1 number(s): "135796230"

Test #31:

score: 0
Accepted
time: 101ms
memory: 32192kb

input:

200000 31247 200000
10554 1
24385 1
9400 1
21999 1
24385 1
10874 1
16922 1
14381 1
16922 1
5492 1
5901 1
21999 1
16922 1
21999 1
16922 1
9400 1
5901 1
10337 1
9400 1
10337 1
21999 1
23957 1
16922 1
10874 1
5901 1
4181 1
16922 1
9400 1
5901 1
9400 1
5901 1
4181 1
10874 1
26165 1
10874 1
24385 1
10337...

output:

17606590183202

result:

ok 1 number(s): "17606590183202"

Test #32:

score: 0
Accepted
time: 293ms
memory: 30648kb

input:

200000 515 200000
454 1
477 1
15 1
188 1
394 1
20 1
513 1
378 1
509 1
322 1
392 1
215 1
473 1
465 1
367 1
155 1
265 1
239 1
264 1
511 1
300 1
8 1
86 1
17 1
96 1
84 1
50 1
440 1
89 1
336 1
55 1
144 1
25 1
235 1
59 1
182 1
296 1
227 1
154 1
233 1
289 1
246 1
270 1
200 1
418 1
311 1
474 1
206 1
338 1
4...

output:

135796230

result:

ok 1 number(s): "135796230"

Test #33:

score: 0
Accepted
time: 267ms
memory: 30408kb

input:

200000 200000 200000
119672 1
172761 1
158391 3
157543 4
113113 7
3794 10
193802 10
193228 13
190363 14
78983 14
157724 14
81793 15
142847 15
66734 16
60442 17
73056 17
66734 18
199261 19
86256 19
107766 23
87752 24
101826 26
135559 26
39392 27
93143 28
49876 28
123967 29
136767 31
54217 32
21378 33...

output:

294844949

result:

ok 1 number(s): "294844949"

Test #34:

score: 0
Accepted
time: 3ms
memory: 30180kb

input:

199995 3 199993
1 2
1 3
3 4
1 4
2 4
1 4
3 5
1 5
2 5
2 7
1 8
2 9
3 9
2 10
1 13
1 15
3 15
1 17
1 20
2 21
2 22
2 29
2 30
1 32
2 33
2 33
2 33
1 34
1 34
2 37
3 39
3 40
1 41
2 47
3 47
2 48
3 48
1 49
1 49
3 50
3 51
2 52
3 52
2 56
3 56
2 58
2 59
2 60
3 60
1 60
3 61
3 63
2 64
1 64
3 65
1 67
3 67
2 68
3 68
1 ...

output:

6

result:

ok 1 number(s): "6"

Extra Test:

score: 0
Extra Test Passed