QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#649694#9465. 基础 01 练习题hos_lyric50 1529ms473288kbC++1416.1kb2024-10-18 09:00:072024-10-18 09:00:12

Judging History

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

  • [2024-10-18 09:00:12]
  • 评测
  • 测评结果:50
  • 用时:1529ms
  • 内存:473288kb
  • [2024-10-18 09:00:07]
  • 提交

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")

////////////////////////////////////////////////////////////////////////////////
// 2^61 - 1 = 2'305'843'009'213'693'951
struct ModLong61 {
  static constexpr unsigned long long M = (1ULL << 61) - 1;
  unsigned long long x;
  constexpr ModLong61() : x(0ULL) {}
  constexpr ModLong61(unsigned x_) : x(x_) {}
  constexpr ModLong61(unsigned long long x_) : x(x_ % M) {}
  constexpr ModLong61(int x_) : x((x_ < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
  constexpr ModLong61(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
  ModLong61 &operator+=(const ModLong61 &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
  ModLong61 &operator-=(const ModLong61 &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
  ModLong61 &operator*=(const ModLong61 &a) {
    const unsigned __int128 y = static_cast<unsigned __int128>(x) * a.x;
    x = (y >> 61) + (y & M);
    x = (x >= M) ? (x - M) : x;
    return *this;
  }
  ModLong61 &operator/=(const ModLong61 &a) { return (*this *= a.inv()); }
  ModLong61 pow(long long e) const {
    if (e < 0) return inv().pow(-e);
    ModLong61 a = *this, b = 1ULL; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
  }
  ModLong61 inv() const {
    unsigned long long a = M, b = x; long long y = 0, z = 1;
    for (; b; ) { const unsigned long long q = a / b; const unsigned long long c = a - q * b; a = b; b = c; const long long w = y - static_cast<long long>(q) * z; y = z; z = w; }
    assert(a == 1ULL); return ModLong61(y);
  }
  ModLong61 operator+() const { return *this; }
  ModLong61 operator-() const { ModLong61 a; a.x = x ? (M - x) : 0ULL; return a; }
  ModLong61 operator+(const ModLong61 &a) const { return (ModLong61(*this) += a); }
  ModLong61 operator-(const ModLong61 &a) const { return (ModLong61(*this) -= a); }
  ModLong61 operator*(const ModLong61 &a) const { return (ModLong61(*this) *= a); }
  ModLong61 operator/(const ModLong61 &a) const { return (ModLong61(*this) /= a); }
  template <class T> friend ModLong61 operator+(T a, const ModLong61 &b) { return (ModLong61(a) += b); }
  template <class T> friend ModLong61 operator-(T a, const ModLong61 &b) { return (ModLong61(a) -= b); }
  template <class T> friend ModLong61 operator*(T a, const ModLong61 &b) { return (ModLong61(a) *= b); }
  template <class T> friend ModLong61 operator/(T a, const ModLong61 &b) { return (ModLong61(a) /= b); }
  explicit operator bool() const { return x; }
  bool operator==(const ModLong61 &a) const { return (x == a.x); }
  bool operator!=(const ModLong61 &a) const { return (x != a.x); }
  friend std::ostream &operator<<(std::ostream &os, const ModLong61 &a) { return os << a.x; }
};
////////////////////////////////////////////////////////////////////////////////

#include <chrono>
#ifdef LOCAL
// mt19937_64 rng(58);
const ModLong61 BASE = 10;
#else
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const ModLong61 BASE = static_cast<unsigned long long>(rng());
#endif


////////////////////////////////////////////////////////////////////////////////


// 0 for null
// range 0/1 flip, range hash
namespace seg {
constexpr int MAX_NUM_NODES = 1 << 24;
int n, id, ls[MAX_NUM_NODES], rs[MAX_NUM_NODES];
vector<ModLong61> pw;
int sz[MAX_NUM_NODES], lz[MAX_NUM_NODES];
ModLong61 vals[MAX_NUM_NODES][2];
void init(int n_) {
  n = n_;
  id = 1;
  pw.resize(n + 1);
  pw[0] = 1;
  for (int i = 1; i <= n; ++i) pw[i] = pw[i - 1] * BASE;
}
int newNode() {
  assert(id < MAX_NUM_NODES);
  const int u = id++;
  ls[u] = rs[u] = 0;
  sz[u] = 0;
  lz[u] = 0;
  for (int q = 0; q < 2; ++q) vals[u][q] = 0;
  return u;
}
int copyNode(int u0) {
  const int u = newNode();
  ls[u] = ls[u0];
  rs[u] = rs[u0];
  sz[u] = sz[u0];
  lz[u] = lz[u0];
  for (int q = 0; q < 2; ++q) vals[u][q] = vals[u0][q];
  return u;
}
int leafNode() {
  const int u = newNode();
  sz[u] = 1;
  vals[u][0] = 0;
  vals[u][1] = 1;
  return u;
}
int pullNode(int uL, int uR, int f) {
  const int u = newNode();
  ls[u] = uL;
  rs[u] = uR;
  sz[u] = sz[uL] + sz[uR];
  lz[u] = f;
  for (int q = 0; q < 2; ++q) {
    vals[u][q] = vals[uL][f ^ q] + pw[sz[uL]] * vals[uR][f ^ q];
  }
  return u;
}
int build(int l, int r) {
  if (l + 1 == r) {
    return leafNode();
  }
  const int mid = (l + r) / 2;
  const int uL = build(l, mid);
  const int uR = build(mid, r);
  return pullNode(uL, uR, 0);
}
int flip(int u, int l, int r, int a, int b) {
  if (b <= l || r <= a) return u;
  if (a <= l && r <= b) {
    const int v = copyNode(u);
    lz[v] ^= 1;
    swap(vals[v][0], vals[v][1]);
    return v;
  }
  const int mid = (l + r) / 2;
  const int uL = flip(ls[u], l, mid, a, b);
  const int uR = flip(rs[u], mid, r, a, b);
  return pullNode(uL, uR, lz[u]);
}
int flip(int u, int a, int b) {
  return flip(u, 0, n, a, b);
}
int cmp(int u, int v, int fu, int fv) {
  assert(vals[u][fu] != vals[v][fv]);
  if (!ls[u]) return (vals[u][fu].x < vals[v][fv].x) ? -1 : +1;
  fu ^= lz[u];
  fv ^= lz[v];
  return (vals[ls[u]][fu] != vals[ls[v]][fv])
      ? cmp(ls[u], ls[v], fu, fv)
      : cmp(rs[u], rs[v], fu, fv);
}
int cmp(int u, int v) {
  if (vals[u][0] == vals[v][0]) return false;
  return cmp(u, v, 0, 0);
}
string toString(int u, int f = 0) {
  if (!ls[u]) return to_string(vals[u][f].x);
  f ^= lz[u];
  return toString(ls[u], f) + toString(rs[u], f);
}
}  // seg


// T: monoid representing information of an interval.
//   T()  should return the identity.
//   T(S s)  should represent a single element of the array.
//   T::push(T &l, T &r)  should push the lazy update.
//   T::pull(const T &l, const T &r)  should pull two intervals.
template <class T> struct SegmentTreeRange {
  int logN, n;
  vector<T> ts;
  SegmentTreeRange() : logN(0), n(0) {}
  explicit SegmentTreeRange(int n_) {
    for (logN = 0, n = 1; n < n_; ++logN, n <<= 1) {}
    ts.resize(n << 1);
  }
  template <class S> explicit SegmentTreeRange(const vector<S> &ss) {
    const int n_ = ss.size();
    for (logN = 0, n = 1; n < n_; ++logN, n <<= 1) {}
    ts.resize(n << 1);
    for (int i = 0; i < n_; ++i) at(i) = T(ss[i]);
    build();
  }
  T &at(int i) {
    return ts[n + i];
  }
  void build() {
    for (int u = n; --u; ) pull(u);
  }

  inline void push(int u) {
    ts[u].push(ts[u << 1], ts[u << 1 | 1]);
  }
  inline void pull(int u) {
    ts[u].pull(ts[u << 1], ts[u << 1 | 1]);
  }

  // Applies T::f(args...) to [a, b).
  template <class F, class... Args>
  void ch(int a, int b, F f, Args &&... args) {
    assert(0 <= a); assert(a <= b); assert(b <= n);
    if (a == b) return;
    a += n; b += n;
    for (int h = logN; h; --h) {
      const int aa = a >> h, bb = b >> h;
      if (aa == bb) {
        if ((aa << h) != a || (bb << h) != b) push(aa);
      } else {
        if ((aa << h) != a) push(aa);
        if ((bb << h) != b) push(bb);
      }
    }
    for (int aa = a, bb = b; aa < bb; aa >>= 1, bb >>= 1) {
      if (aa & 1) (ts[aa++].*f)(args...);
      if (bb & 1) (ts[--bb].*f)(args...);
    }
    for (int h = 1; h <= logN; ++h) {
      const int aa = a >> h, bb = b >> h;
      if (aa == bb) {
        if ((aa << h) != a || (bb << h) != b) pull(aa);
      } else {
        if ((aa << h) != a) pull(aa);
        if ((bb << h) != b) pull(bb);
      }
    }
  }

  // Calculates the product for [a, b).
  T get(int a, int b) {
    assert(0 <= a); assert(a <= b); assert(b <= n);
    if (a == b) return T();
    a += n; b += n;
    for (int h = logN; h; --h) {
      const int aa = a >> h, bb = b >> h;
      if (aa == bb) {
        if ((aa << h) != a || (bb << h) != b) push(aa);
      } else {
        if ((aa << h) != a) push(aa);
        if ((bb << h) != b) push(bb);
      }
    }
    T prodL, prodR, t;
    for (int aa = a, bb = b; aa < bb; aa >>= 1, bb >>= 1) {
      if (aa & 1) { t.pull(prodL, ts[aa++]); prodL = t; }
      if (bb & 1) { t.pull(ts[--bb], prodR); prodR = t; }
    }
    t.pull(prodL, prodR);
    return t;
  }

  // Calculates T::f(args...) of a monoid type for [a, b).
  //   op(-, -)  should calculate the product.
  //   e()  should return the identity.
  template <class Op, class E, class F, class... Args>
#if __cplusplus >= 201402L
  auto
#else
  decltype((std::declval<T>().*F())())
#endif
  get(int a, int b, Op op, E e, F f, Args &&... args) {
    assert(0 <= a); assert(a <= b); assert(b <= n);
    if (a == b) return e();
    a += n; b += n;
    for (int h = logN; h; --h) {
      const int aa = a >> h, bb = b >> h;
      if (aa == bb) {
        if ((aa << h) != a || (bb << h) != b) push(aa);
      } else {
        if ((aa << h) != a) push(aa);
        if ((bb << h) != b) push(bb);
      }
    }
    auto prodL = e(), prodR = e();
    for (int aa = a, bb = b; aa < bb; aa >>= 1, bb >>= 1) {
      if (aa & 1) prodL = op(prodL, (ts[aa++].*f)(args...));
      if (bb & 1) prodR = op((ts[--bb].*f)(args...), prodR);
    }
    return op(prodL, prodR);
  }

  // Find min b s.t. T::f(args...) returns true,
  // when called for the partition of [a, b) from left to right.
  //   Returns n + 1 if there is no such b.
  template <class F, class... Args>
  int findRight(int a, F f, Args &&... args) {
    assert(0 <= a); assert(a <= n);
    if ((T().*f)(args...)) return a;
    if (a == n) return n + 1;
    a += n;
    for (int h = logN; h; --h) push(a >> h);
    for (; ; a >>= 1) if (a & 1) {
      if ((ts[a].*f)(args...)) {
        for (; a < n; ) {
          push(a);
          if (!(ts[a <<= 1].*f)(args...)) ++a;
        }
        return a - n + 1;
      }
      ++a;
      if (!(a & (a - 1))) return n + 1;
    }
  }

  // Find max a s.t. T::f(args...) returns true,
  // when called for the partition of [a, b) from right to left.
  //   Returns -1 if there is no such a.
  template <class F, class... Args>
  int findLeft(int b, F f, Args &&... args) {
    assert(0 <= b); assert(b <= n);
    if ((T().*f)(args...)) return b;
    if (b == 0) return -1;
    b += n;
    for (int h = logN; h; --h) push((b - 1) >> h);
    for (; ; b >>= 1) if ((b & 1) || b == 2) {
      if ((ts[b - 1].*f)(args...)) {
        for (; b <= n; ) {
          push(b - 1);
          if (!(ts[(b <<= 1) - 1].*f)(args...)) --b;
        }
        return b - n - 1;
      }
      --b;
      if (!(b & (b - 1))) return -1;
    }
  }
};

////////////////////////////////////////////////////////////////////////////////

constexpr int INF = 1001001001;

struct Node {
  int mn[2], mx[2];
  int lz;
  Node() : mn{+INF, +INF}, mx{-INF, -INF}, lz(0) {}
  void push(Node &l, Node &r) {
  }
  void pull(const Node &l, const Node &r) {
    for (int q = 0; q < 2; ++q) {
      mn[q] = min(l.mn[lz ^ q], r.mn[lz ^ q]);
      mx[q] = max(l.mx[lz ^ q], r.mx[lz ^ q]);
    }
  }
  void flip() {
    lz ^= 1;
    swap(mn[0], mn[1]);
    swap(mx[0], mx[1]);
  }
};


vector<int> uf;
int root(int u) {
  return (uf[u] < 0) ? u : (uf[u] = root(uf[u]));
}
bool connect(int u, int v) {
  u = root(u);
  v = root(v);
  if (u == v) return false;
  if (uf[u] > uf[v]) swap(u, v);
  uf[u] += uf[v];
  uf[v] = u;
  return true;
}


/*
  A[x][y] = 0: y -> x
  A[x][y] = 1: x -> y
  
  SCC contains m rows and n columns ==> (m n) cell edges
  as long as m = 0 or n = 0, we can add edges between rows or between columns
  ~~> tournament
  
  sort decr. by lex.
  - A[x] = A[x']: identify x and x'  ~~>  can assume x -> x'
  - A[x] > A[x']: \exists y s.t. A[x][y] = 1, A[x'][y] = 0  ~~>  x -> y -> x'
*/

int N, Q, O;
vector<int> X0, X1, Y0, Y1;

Int now;
vector<int> ms, ns;
vector<int> bot;
vector<int> wait;
void init() {
  now = 0;
  uf.assign(N, -1);
  ms.assign(N, 1);
  ns.assign(N, 0);
  bot.resize(N);
  for (int i = 0; i < N; ++i) bot[i] = i + 1;
  wait.assign(N + 1, 0);
}
// i-1 and i
void conn(int i) {
  int u = root(i - 1), v = root(i);
  assert(u != v);
  connect(u, v);
  if (uf[v] != u) swap(u, v);
  chmax(bot[u], bot[v]);
  now -= max((Int)ms[u] * ns[u] - 1, 0LL);
  now -= max((Int)ms[v] * ns[v] - 1, 0LL);
  ms[u] += ms[v];
  ns[u] += ns[v];
  ns[u] += wait[i];
  wait[i] = 0;
  now += max((Int)ms[u] * ns[u] - 1, 0LL);
}
void addN(int i) {
  const int u = root(i);
  now -= max((Int)ms[u] * ns[u] - 1, 0LL);
  ++ns[u];
  now += max((Int)ms[u] * ns[u] - 1, 0LL);
}

int main() {
  for (; ~scanf("%d%d%d", &N, &Q, &O); ) {
    X0.resize(Q);
    X1.resize(Q);
    Y0.resize(Q);
    Y1.resize(Q);
    for (int q = 0; q < Q; ++q) {
      scanf("%d%d%d%d", &X0[q], &X1[q], &Y0[q], &Y1[q]);
      --X0[q];
      --Y0[q];
    }
    
    vector<int> segs(N);
    seg::init(N);
    {
      vector<vector<int>> qss(N + 1);
      for (int q = 0; q < Q; ++q) {
        qss[X0[q]].push_back(q);
        qss[X1[q]].push_back(q);
      }
      int u = seg::build(0, N);
      for (int x = 0; x < N; ++x) {
        for (const int q : qss[x]) {
          u = seg::flip(u, Y0[q], Y1[q]);
        }
        segs[x] = u;
      }
    }
    vector<int> xs(N);
    for (int x = 0; x < N; ++x) xs[x] = x;
// for(const int x:xs)cerr<<"segs["<<x<<"] = "<<seg::toString(segs[x])<<endl;
    sort(xs.begin(), xs.end(), [&](int x0, int x1) -> bool {
// cerr<<"cmp "<<x0<<" "<<x1<<endl;
      return (seg::cmp(segs[x0], segs[x1]) > 0);
    });
// for(const int x:xs)cerr<<"segs["<<x<<"] = "<<seg::toString(segs[x])<<endl;
    
    vector<Int> ans(N);
    {
      vector<vector<int>> qss(N + 1);
      for (int q = 0; q < Q; ++q) {
        qss[Y0[q]].push_back(q);
        qss[Y1[q]].push_back(q);
      }
      SegmentTreeRange<Node> se(N);
      for (int i = 0; i < N; ++i) {
        se.at(xs[i]).mn[0] = i;
        se.at(xs[i]).mx[0] = i;
      }
      se.build();
      init();
      for (int y = 0; y < N; ++y) {
        for (const int q : qss[y]) {
          se.ch(X0[q], X1[q], &Node::flip);
        }
        // topmost 0, bottommost 1
        const int i0 = min(se.ts[1].mn[0], N);
        const int i1 = max(se.ts[1].mx[1], -1);
// cerr<<COLOR("33")<<"y = "<<y<<": i0 = "<<i0<<", i1 = "<<i1<<COLOR()<<endl;
        if (i0 < i1) {
          // connect y with xs[i0], ..., xs[i1]
          for (int i = i0; ; ) {
            const int b = bot[root(i)];
            if (i1 < b) break;
            conn(b);
          }
          addN(i0);
        } else {
          // 1...1 y 0...0
          if (0 < i0 && i0 < N && root(i0 - 1) == root(i0)) {
            addN(i0);
          } else {
            ++wait[i0];
          }
        }
// for(int i=0;i<N;i=bot[i]){i=root(i);cerr<<"["<<i<<","<<bot[i]<<"):"<<ms[i]<<","<<ns[i]<<" ";}cerr<<endl;
// cerr<<"wait = "<<wait<<endl;
        ans[y] = (Int)N * (y + 1) - now;
      }
    }
    
    int ou = 0;
    for (int y = O ? 0 : (N - 1); y < N; ++y) {
      if (ou++) printf(" ");
      printf("%lld", ans[y]);
    }
    puts("");
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 5
Accepted

Test #1:

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

input:

4 1000 0
2 3 1 2
1 3 1 3
1 2 1 2
1 2 3 4
1 4 2 4
1 3 1 2
1 4 1 2
1 3 1 4
3 3 2 3
1 2 2 4
4 4 1 3
3 3 3 4
3 4 3 4
2 3 1 1
1 2 2 4
1 4 3 4
3 4 1 2
1 2 2 3
3 4 3 3
1 2 4 4
4 4 2 4
1 4 1 1
1 1 1 3
2 3 2 3
1 1 2 4
2 3 2 4
3 3 1 4
3 3 3 3
1 3 3 3
2 3 2 4
3 3 2 2
1 3 2 4
1 3 1 2
3 4 1 2
2 3 1 3
1 1 1 2
1 2...

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: 5
Accepted
time: 2ms
memory: 12168kb

input:

4 1000 0
1 4 3 3
2 3 4 4
3 4 3 4
3 4 1 2
1 4 2 4
2 3 1 3
3 4 2 4
2 3 3 3
3 4 1 3
1 3 1 4
2 3 1 3
1 1 2 2
1 4 3 4
1 4 1 3
1 2 3 4
1 2 1 2
2 3 1 4
2 2 2 2
1 3 1 3
2 2 2 4
1 2 1 4
1 1 1 1
1 2 3 4
4 4 1 3
2 4 1 3
1 1 1 3
1 4 2 2
2 3 1 2
2 2 1 2
1 2 1 4
1 4 2 4
1 2 1 3
1 2 1 3
2 4 2 2
1 2 1 1
1 2 1 3
2 4...

output:

1

result:

ok 1 number(s): "1"

Test #3:

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

input:

4 1000 0
1 4 1 2
1 4 2 2
1 4 3 4
2 4 4 4
2 3 3 4
2 4 2 4
1 2 2 2
4 4 2 4
1 3 1 3
1 4 1 4
3 3 3 4
4 4 2 3
2 3 1 4
2 2 1 3
2 3 2 4
2 2 1 4
1 2 2 3
1 4 1 3
4 4 1 4
3 4 1 4
1 2 1 2
1 2 1 3
2 2 3 3
1 2 1 4
1 1 1 4
2 2 1 4
1 4 3 4
2 4 2 4
2 2 1 4
3 4 1 3
2 3 2 4
1 3 1 4
1 3 1 4
3 3 1 3
1 2 1 3
3 3 1 4
1 4...

output:

5

result:

ok 1 number(s): "5"

Subtask #2:

score: 10
Accepted

Test #4:

score: 10
Accepted
time: 117ms
memory: 162276kb

input:

50 200000 0
1 45 2 6
29 44 2 6
31 37 2 50
2 37 1 19
7 13 8 38
38 46 19 38
10 30 30 46
22 42 1 45
5 35 24 27
10 36 19 31
20 47 17 35
7 9 23 42
15 26 31 42
7 8 7 42
1 26 33 48
2 5 30 36
17 44 21 44
5 44 24 36
19 47 15 17
29 36 2 42
31 34 11 41
9 24 12 30
30 43 8 20
2 12 13 20
11 12 10 15
14 22 3 29
2 ...

output:

1

result:

ok 1 number(s): "1"

Test #5:

score: 10
Accepted
time: 2ms
memory: 12320kb

input:

50 70 0
1 50 1 50
24 50 1 1
50 50 2 2
34 50 3 3
36 50 4 4
32 50 5 5
18 50 6 6
12 50 7 7
6 50 8 8
28 50 9 9
38 50 10 10
4 50 11 11
26 50 12 12
14 50 13 13
46 50 14 14
2 50 15 15
8 50 16 16
44 50 17 17
10 50 18 18
30 50 19 19
22 50 20 20
48 50 21 21
20 50 22 22
42 50 23 23
40 50 24 24
16 50 25 25
16 5...

output:

2280

result:

ok 1 number(s): "2280"

Test #6:

score: 10
Accepted
time: 0ms
memory: 12336kb

input:

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

output:

339

result:

ok 1 number(s): "339"

Test #7:

score: 10
Accepted
time: 1ms
memory: 12148kb

input:

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

output:

51

result:

ok 1 number(s): "51"

Subtask #3:

score: 10
Accepted

Test #8:

score: 10
Accepted
time: 392ms
memory: 410860kb

input:

5000 200000 0
1438 2561 3478 4930
1740 4634 87 3003
590 3275 1376 1681
2035 2793 2004 4945
567 3159 550 4470
61 3039 3431 3519
2654 3834 3460 4960
591 3560 409 443
345 2599 746 2891
1288 4570 1577 4402
249 377 1951 4534
2411 2455 294 1192
1679 3153 1645 4259
1735 1856 601 668
477 4881 411 2094
424 1...

output:

1

result:

ok 1 number(s): "1"

Test #9:

score: 10
Accepted
time: 132ms
memory: 151436kb

input:

5000 200000 0
4336 5000 1 1
686 5000 2 2
3130 5000 3 3
672 5000 4 4
1664 5000 5 5
1480 5000 6 6
1326 5000 7 7
3726 5000 8 8
4170 5000 9 9
4794 5000 10 10
3374 5000 11 11
1836 5000 12 12
310 5000 13 13
2146 5000 14 14
3266 5000 15 15
820 5000 16 16
1152 5000 17 17
2876 5000 18 18
134 5000 19 19
828 5...

output:

24995

result:

ok 1 number(s): "24995"

Test #10:

score: 10
Accepted
time: 116ms
memory: 150388kb

input:

5000 200000 0
1410 5000 1 1
3340 5000 2 2
4202 5000 3 3
4450 5000 4 4
914 5000 5 5
4514 5000 6 6
4 5000 7 7
238 5000 8 8
3182 5000 9 9
3302 5000 10 10
2136 5000 11 11
1504 5000 12 12
3204 5000 13 13
2078 5000 14 14
4026 5000 15 15
3690 5000 16 16
4430 5000 17 17
1304 5000 18 18
2156 5000 19 19
4154 ...

output:

10000

result:

ok 1 number(s): "10000"

Test #11:

score: 10
Accepted
time: 181ms
memory: 204792kb

input:

5000 200000 0
1556 3445 1 1
1803 3198 2 2
790 4211 3 3
564 4437 4 4
1128 3873 5 5
129 4872 6 6
2062 2939 7 7
1480 3521 8 8
1252 3749 9 9
942 4059 10 10
111 4890 11 11
915 4086 12 12
1575 3426 13 13
2186 2815 14 14
392 4609 15 15
1689 3312 16 16
492 4509 17 17
866 4135 18 18
381 4620 19 19
92 4909 20...

output:

34989

result:

ok 1 number(s): "34989"

Test #12:

score: 10
Accepted
time: 214ms
memory: 232648kb

input:

5000 200000 0
1 1 2330 2671
2 2 789 4212
3 3 1593 3408
4 4 438 4563
5 5 2048 2953
6 6 491 4510
7 7 578 4423
8 8 770 4231
9 9 482 4519
10 10 395 4606
11 11 1960 3041
12 12 1289 3712
13 13 621 4380
14 14 2235 2766
15 15 916 4085
16 16 781 4220
17 17 1440 3561
18 18 902 4099
19 19 1998 3003
20 20 641 4...

output:

49977

result:

ok 1 number(s): "49977"

Test #13:

score: 10
Accepted
time: 292ms
memory: 355720kb

input:

5000 200000 0
1 661 1 2
1 1385 3 4
1 225 5 6
1 1833 7 8
1 58 9 10
1 2064 11 12
1 235 13 14
1 1918 15 16
1 2137 17 18
1 538 19 20
1 513 21 22
1 1405 23 24
1 1376 25 26
1 1711 27 28
1 165 29 30
1 209 31 32
1 68 33 34
1 1864 35 36
1 1455 37 38
1 425 39 40
1 669 41 42
1 2326 43 44
1 133 45 46
1 2257 47 ...

output:

5001

result:

ok 1 number(s): "5001"

Subtask #4:

score: 10
Accepted

Test #14:

score: 10
Accepted
time: 402ms
memory: 410244kb

input:

5000 200000 1
565 4401 1659 1826
429 1640 2999 3495
572 3994 9 3863
3844 4284 2307 3144
1054 1943 358 2592
727 4248 29 1171
1685 2392 4559 4929
1149 2787 1204 1947
2349 2619 405 998
1910 2786 25 1275
912 3475 4384 4387
3822 4895 1849 4548
3082 4749 3457 4220
3174 4885 117 1085
2517 3919 4325 4869
17...

output:

5000 5653 3715 1781 1031 823 540 185 190 71 56 61 66 71 76 81 86 91 96 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...

result:

ok 5000 numbers

Test #15:

score: 10
Accepted
time: 128ms
memory: 128952kb

input:

5000 200000 1
1 1 4840 5000
2 2 1690 5000
3 3 908 5000
4 4 1212 5000
5 5 2712 5000
6 6 3950 5000
7 7 4724 5000
8 8 3706 5000
9 9 1888 5000
10 10 4056 5000
11 11 1074 5000
12 12 3806 5000
13 13 3756 5000
14 14 2822 5000
15 15 1264 5000
16 16 4088 5000
17 17 796 5000
18 18 4888 5000
19 19 4610 5000
20...

output:

5000 9997 14997 19997 24997 29994 34974 39958 44944 49928 54902 59872 64858 69833 74833 79777 84760 89728 94710 99675 104675 109599 114578 119536 124492 129423 134374 139298 144270 149188 154130 159041 163948 168851 173814 178678 183638 188563 193522 198522 203288 208204 213158 218030 222898 227719 ...

result:

ok 5000 numbers

Test #16:

score: 10
Accepted
time: 125ms
memory: 151492kb

input:

5000 200000 1
4090 5000 1 1
3772 5000 2 2
3880 5000 3 3
1888 5000 4 4
2602 5000 5 5
4934 5000 6 6
4224 5000 7 7
226 5000 8 8
176 5000 9 9
2862 5000 10 10
224 5000 11 11
1204 5000 12 12
3202 5000 13 13
2726 5000 14 14
3468 5000 15 15
2134 5000 16 16
4392 5000 17 17
4814 5000 18 18
3748 5000 19 19
390...

output:

5000 10000 15000 20000 25000 30000 35000 40000 45000 50000 55000 60000 65000 70000 75000 80000 85000 90000 95000 100000 105000 110000 115000 120000 125000 130000 135000 140000 145000 150000 155000 160000 165000 170000 175000 180000 185000 190000 195000 200000 205000 210000 215000 220000 225000 23000...

result:

ok 5000 numbers

Test #17:

score: 10
Accepted
time: 179ms
memory: 211132kb

input:

5000 200000 1
1443 3558 1 1
2025 2976 2 2
837 4164 3 3
2197 2804 4 4
1545 3456 5 5
2471 2530 6 6
764 4237 7 7
2462 2539 8 8
44 4957 9 9
2283 2718 10 10
742 4259 11 11
959 4042 12 12
1558 3443 13 13
1111 3890 14 14
2090 2911 15 15
687 4314 16 16
509 4492 17 17
1461 3540 18 18
2060 2941 19 19
2218 278...

output:

5000 10000 15000 20000 25000 30000 35000 40000 45000 50000 55000 60000 65000 70000 75000 80000 85000 90000 95000 100000 105000 110000 115000 120000 125000 130000 135000 140000 145000 150000 155000 160000 165000 170000 175000 180000 185000 190000 195000 200000 205000 210000 215000 220000 225000 23000...

result:

ok 5000 numbers

Test #18:

score: 10
Accepted
time: 175ms
memory: 231820kb

input:

5000 200000 1
1 1 1194 3807
2 2 721 4280
3 3 471 4530
4 4 142 4859
5 5 1727 3274
6 6 1549 3452
7 7 551 4450
8 8 981 4020
9 9 1649 3352
10 10 2233 2768
11 11 980 4021
12 12 1498 3503
13 13 196 4805
14 14 381 4620
15 15 2085 2916
16 16 1559 3442
17 17 890 4111
18 18 1853 3148
19 19 527 4474
20 20 881 ...

output:

5000 10000 14973 19950 24950 29950 34950 39833 44833 49758 54691 59605 64557 69557 74557 79557 84557 89151 94065 98975 103975 108975 113975 118975 123975 128251 133251 138030 143030 147768 152768 157768 162768 167163 172009 177009 181653 186486 191486 196140 200961 205778 210591 215591 220205 225205...

result:

ok 5000 numbers

Test #19:

score: 10
Accepted
time: 319ms
memory: 358712kb

input:

5000 200000 1
1 2402 1 2
1 85 3 4
1 610 5 6
1 531 7 8
1 1258 9 10
1 2389 11 12
1 885 13 14
1 1658 15 16
1 2210 17 18
1 643 19 20
1 1079 21 22
1 499 23 24
1 1768 25 26
1 224 27 28
1 237 29 30
1 124 31 32
1 359 33 34
1 248 35 36
1 1896 37 38
1 1867 39 40
1 625 41 42
1 2407 43 44
1 2370 45 46
1 2177 47...

output:

5000 7635 2635 2261 1936 1645 1611 1705 1873 2021 2223 2401 2601 2801 3001 3201 3401 3601 3801 4001 4201 4401 4601 4801 5001 5201 5401 5601 5801 6001 6201 6401 6601 6801 7001 7201 7401 7601 7801 8001 8201 8401 8386 8581 8776 8971 9166 9361 9556 9751 9946 10141 10336 10531 10726 10921 11116 11311 115...

result:

ok 5000 numbers

Test #20:

score: 10
Accepted
time: 296ms
memory: 330760kb

input:

5000 200000 1
1 2 1 1040
3 4 1 742
5 6 1 187
7 8 1 378
9 10 1 595
11 12 1 1296
13 14 1 1104
15 16 1 2266
17 18 1 1137
19 20 1 1383
21 22 1 2167
23 24 1 1540
25 26 1 118
27 28 1 1783
29 30 1 2023
31 32 1 2185
33 34 1 1951
35 36 1 2292
37 38 1 2268
39 40 1 458
41 42 1 88
43 44 1 120
45 46 1 1782
47 48...

output:

5000 10000 15000 20000 25000 30000 35000 40000 45000 50000 55000 60000 65000 70000 75000 80000 85000 90000 95000 100000 105000 110000 115000 120000 125000 130000 135000 140000 145000 150000 155000 160000 165000 170000 175000 180000 185000 190000 195000 200000 205000 210000 215000 220000 225000 23000...

result:

ok 5000 numbers

Subtask #5:

score: 15
Accepted

Test #21:

score: 15
Accepted
time: 1046ms
memory: 289980kb

input:

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

output:

200000 400000 532619 597930 598645 533081 733081 933081 631687 831687 1031687 1064777 1264777 1464777 1664777 1864777 1897874 2097874 2297874 2330961 2530961 2730961 2764057 2964057 3164057 3364057 3564057 3764057 3964057 3997153 4197153 4397153 4597153 4797153 4997153 5197153 5230249 5430249 563024...

result:

ok 200000 numbers

Test #22:

score: 15
Accepted
time: 807ms
memory: 302364kb

input:

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

output:

200000 400000 599992 799992 999981 1199971 1399971 1599938 1799938 1999921 2199911 2399901 2599901 2799901 2999891 3199869 3399858 3599847 3799806 3999793 4199686 4399686 4599671 4799656 4999593 5199551 5399533 5599515 5799469 5999421 6199421 6399371 6599371 6799319 6999137 7199044 7399015 7598986 7...

result:

ok 200000 numbers

Test #23:

score: 15
Accepted
time: 882ms
memory: 302136kb

input:

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

output:

200000 400000 600000 800000 1000000 1199971 1399965 1599952 1799945 1999929 2199901 2399869 2599833 2799819 2999819 3199819 3399777 3599746 3799729 3999712 4199695 4399659 4599641 4799602 4999539 5199518 5399497 5599476 5799476 5999476 6199429 6399353 6599301 6799276 6999221 7199195 7399137 7599110 ...

result:

ok 200000 numbers

Test #24:

score: 15
Accepted
time: 1150ms
memory: 302608kb

input:

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

output:

200000 244003 133246 133117 111726 66913 1 1 200001 200001 400001 400001 600001 800001 1000001 1200001 1400001 1400001 1400001 1600001 1800001 1800001 2000001 2200001 2400001 2600001 2600001 2600001 2800001 3000001 3000001 3000001 3200001 3200001 3400001 3600001 3600001 3800001 4000001 4000001 40000...

result:

ok 200000 numbers

Test #25:

score: 15
Accepted
time: 1529ms
memory: 297500kb

input:

200000 200000 1
62179 62180 166600 166600
168902 168904 109106 109107
71739 71741 40856 40856
68155 68155 50355 50355
82427 82427 131433 131435
134495 134497 97523 97524
100523 100523 163640 163642
103078 103078 39321 39321
75997 75997 52778 52780
141945 141946 67489 67489
20781 20782 198096 198098
...

output:

200000 399993 599993 799980 999965 1199946 1399935 1599889 1799873 1999848 2199848 2399848 2599801 2799801 2999801 3199759 3399759 3599725 3799611 3999581 4199551 4399457 4599372 4799263 4999165 5199061 5399014 5598923 5798828 5998828 6198633 6398501 6598311 6798165 6998013 7197942 7397721 7597645 7...

result:

ok 200000 numbers

Test #26:

score: 15
Accepted
time: 1434ms
memory: 306144kb

input:

200000 200000 1
172635 172635 165118 165120
182060 182062 140709 140711
79621 79622 120595 120597
22131 22132 38357 38357
15637 15637 73583 73583
163025 163027 90579 90579
98127 98129 186936 186938
15578 15580 67768 67769
170985 170986 105541 105542
166284 166285 199715 199715
179347 179347 86139 86...

output:

200000 400000 600000 799989 999965 1199941 1399911 1599889 1799849 1999794 2199771 2399771 2599715 2799677 2999611 3199611 3399511 3599416 3799345 3999270 4199227 4399070 4598981 4798867 4998813 5198713 5398585 5598451 5798207 5998057 6197985 6397826 6597826 6797691 6997521 7197377 7397377 7597163 7...

result:

ok 200000 numbers

Test #27:

score: 15
Accepted
time: 1489ms
memory: 304824kb

input:

200000 200000 1
192777 192779 177750 177752
143767 143769 170842 170843
26652 26654 75434 75435
43426 43427 188344 188345
139929 139929 172505 172506
176663 176665 58244 58246
32815 32815 193800 193801
148127 148127 103366 103368
136791 136793 152591 152591
33978 33979 63334 63334
108117 108117 1561...

output:

200000 400000 599993 799965 999946 1199929 1399917 1599881 1799821 1999801 2199737 2399653 2599533 2799469 2999356 3199281 3399236 3599137 3799089 3999041 4199041 4399041 4599041 4799041 4998972 5198879 5398713 5598657 5798551 5998389 6198327 6398181 6598087 6798087 6997991 7197800 7397729 7597394 7...

result:

ok 200000 numbers

Subtask #6:

score: 0
Runtime Error

Test #28:

score: 0
Runtime Error

input:

200000 200000 0
91264 123676 6826 154505
121351 188051 108158 131448
65413 163961 26771 116304
93852 110556 34929 187363
31794 142162 33578 38712
26574 67763 178013 197235
46436 146042 95 122860
11683 50463 60177 195245
60862 194711 37817 97212
144366 176271 113551 171098
120095 170517 73555 167299
...

output:


result:


Subtask #7:

score: 0
Runtime Error

Test #37:

score: 35
Accepted
time: 878ms
memory: 445880kb

input:

100000 200000 1
1 22878 1 2
1 7957 3 4
1 21779 5 6
1 34321 7 8
1 41692 9 10
1 49473 11 12
1 10254 13 14
1 43995 15 16
1 46975 17 18
1 668 19 20
1 25996 21 22
1 24975 23 24
1 43259 25 26
1 4174 27 28
1 39330 29 30
1 35462 31 32
1 27523 33 34
1 5574 35 36
1 47955 37 38
1 47013 39 40
1 3846 41 42
1 276...

output:

100000 200000 195499 260665 271141 281125 247871 244233 167815 186461 119505 130369 136332 146819 139966 149297 158628 167959 38799 40841 42883 44925 46967 49009 51051 53093 55135 57177 59219 61261 63303 65345 67387 69429 71471 73513 75555 77597 79639 81681 83723 85765 87807 89849 82711 75257 69937 ...

result:

ok 100000 numbers

Test #38:

score: 35
Accepted
time: 888ms
memory: 399180kb

input:

100000 200000 1
1 2 1 49478
3 4 1 14485
5 6 1 31201
7 8 1 26895
9 10 1 40944
11 12 1 35847
13 14 1 19021
15 16 1 43774
17 18 1 20305
19 20 1 39980
21 22 1 13770
23 24 1 16404
25 26 1 45734
27 28 1 48842
29 30 1 16814
31 32 1 1987
33 34 1 29392
35 36 1 12267
37 38 1 17547
39 40 1 28676
41 42 1 9513
4...

output:

100000 200000 300000 400000 500000 600000 700000 800000 900000 1000000 1100000 1200000 1300000 1400000 1500000 1600000 1700000 1800000 1900000 2000000 2100000 2200000 2300000 2400000 2500000 2600000 2700000 2800000 2900000 3000000 3100000 3200000 3300000 3400000 3500000 3600000 3700000 3800000 39000...

result:

ok 100000 numbers

Test #39:

score: 35
Accepted
time: 1048ms
memory: 473288kb

input:

100000 200000 1
1 2 1 35162
3 4 1 31702
5 6 1 1660
7 8 1 10240
9 10 1 8763
11 12 1 31436
13 14 1 18048
15 16 1 33735
17 18 1 36181
19 20 1 282
21 22 1 9633
23 24 1 5237
25 26 1 9143
27 28 1 42670
29 30 1 46242
31 32 1 2303
33 34 1 2467
35 36 1 38417
37 38 1 3307
39 40 1 19414
41 42 1 33762
43 44 1 3...

output:

100000 200000 300000 400000 500000 600000 700000 800000 900000 1000000 1100000 1200000 1300000 1400000 1500000 1600000 1700000 1800000 1900000 2000000 2100000 2200000 2300000 2400000 2500000 2600000 2700000 2800000 2900000 3000000 3100000 3200000 3300000 3400000 3500000 3600000 3700000 3800000 39000...

result:

ok 100000 numbers

Test #40:

score: 35
Accepted
time: 1029ms
memory: 274944kb

input:

100000 200000 1
43178 56823 1 1
675 99326 2 2
15099 84902 3 3
4203 95798 4 4
23354 76647 5 5
41222 58779 6 6
15927 84074 7 7
39572 60429 8 8
40253 59748 9 9
32007 67994 10 10
15324 84677 11 11
29454 70547 12 12
31223 68778 13 13
7737 92264 14 14
33351 66650 15 15
45210 54791 16 16
28837 71164 17 17
...

output:

100000 200000 300000 400000 500000 600000 700000 800000 900000 1000000 1100000 1200000 1300000 1400000 1500000 1600000 1700000 1800000 1900000 2000000 2100000 2200000 2300000 2400000 2500000 2600000 2700000 2800000 2900000 3000000 3100000 3200000 3300000 3400000 3500000 3600000 3700000 3800000 39000...

result:

ok 100000 numbers

Test #41:

score: 0
Runtime Error

input:

100000 200000 1
24 82240 56597 91255
26810 82165 18477 22393
9362 24598 53820 92593
68653 83682 16581 31580
17086 81545 70066 71299
26891 53343 4122 35933
27137 61932 16905 52055
9728 69119 26769 80202
7404 34071 3636 52818
59726 95606 6460 97518
7723 22242 61797 94241
25097 71751 56647 65839
45036 ...

output:


result: