QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#75958#5463. Range Closest Pair of Points QuerymaspyAC ✓2010ms358900kbC++2021.2kb2023-02-06 18:44:102023-02-06 18:44:12

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-02-06 18:44:12]
  • 评测
  • 测评结果:AC
  • 用时:2010ms
  • 内存:358900kb
  • [2023-02-06 18:44:10]
  • 提交

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) {
  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 2 "library/random/base.hpp"

u64 RNG_64() {
  static uint64_t x_
      = uint64_t(chrono::duration_cast<chrono::nanoseconds>(
                     chrono::high_resolution_clock::now().time_since_epoch())
                     .count())
        * 10150724397891781847ULL;
  x_ ^= x_ << 7;
  return x_ ^= x_ >> 9;
}

u64 RNG(u64 lim) { return RNG_64() % lim; }

ll RNG(ll l, ll r) { return l + RNG_64() % (r - l); }
#line 3 "library/ds/hashmap.hpp"

// u64 -> Val
template <typename Val, int LOG = 20>
struct HashMap {
  int N;
  u64* keys;
  Val* vals;
  vc<int> IDS;
  bitset<1 << LOG> used;
  const int shift;
  const u64 r = 11995408973635179863ULL;
  HashMap()
      : N(1 << LOG), keys(new u64[N]), vals(new Val[N]), shift(64 - __lg(N)) {}
  int hash(ll x) {
    static const u64 FIXED_RANDOM
        = std::chrono::steady_clock::now().time_since_epoch().count();
    return (u64(x + FIXED_RANDOM) * r) >> shift;
  }

  int index(const u64& key) {
    int i = 0;
    for (i = hash(key); used[i] && keys[i] != key; (i += 1) &= (N - 1)) {}
    return i;
  }

  // [] した時点で要素は作られる
  Val& operator[](const u64& key) {
    int i = index(key);
    if (!used[i]) IDS.eb(i), used[i] = 1, keys[i] = key, vals[i] = Val{};
    return vals[i];
  }

  Val get(const u64& key, Val default_value) {
    int i = index(key);
    if (!used[i]) return default_value;
    return vals[i];
  }

  bool count(const u64& key) {
    int i = index(key);
    return used[i] && keys[i] == key;
  }

  void reset() {
    for (auto&& i: IDS) used[i] = 0;
    IDS.clear();
  }

  // f(key, val)
  template <typename F>
  void enumerate_all(F f) {
    for (auto&& i: IDS) f(keys[i], vals[i]);
  }
};
#line 2 "library/ds/segtree/dual_segtree.hpp"

template <typename Monoid>
struct Dual_SegTree {
  using MA = Monoid;
  using A = typename MA::value_type;
  int n, log, size;
  vc<A> laz;

  Dual_SegTree() : Dual_SegTree(0) {}
  Dual_SegTree(int n) { build(n); }

  void build(int m) {
    n = m;
    log = 1;
    while ((1 << log) < n) ++log;
    size = 1 << log;
    laz.assign(size << 1, MA::unit());
  }

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

  vc<A> get_all() {
    FOR(i, size) push(i);
    return {laz.begin() + size, laz.begin() + size + n};
  }

  void apply(int l, int r, const A& a) {
    assert(0 <= l && l <= r && r <= n);
    if (l == r) return;
    l += size, r += size;
    if (!MA::commute) {
      for (int i = log; i >= 1; i--) {
        if (((l >> i) << i) != l) push(l >> i);
        if (((r >> i) << i) != r) push((r - 1) >> i);
      }
    }
    while (l < r) {
      if (l & 1) all_apply(l++, a);
      if (r & 1) all_apply(--r, a);
      l >>= 1, r >>= 1;
    }
  }

private:
  void push(int k) {
    if (laz[k] == MA::unit()) return;
    all_apply(2 * k, laz[k]), all_apply(2 * k + 1, laz[k]);
    laz[k] = MA::unit();
  }
  void all_apply(int k, A a) { laz[k] = MA::op(laz[k], a); }
};
#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 4 "library/geo/range_closest_pair_query.hpp"

// 点群 {p_i | i in [l, r)} に対する最近点対の計算を行うクエリ
// O(KNlogKN + QlogN)
// https://codeforces.com/gym/104172/attachments/download/18933/Hong_Kong_Tutorial.pdf
template <int LOG = 30>
struct Range_Closest_Pair_Query {
  /*
  ・R を増やしながら、L ごとの答を管理する
  ・2^{k-1} <= ANS[L:R] < 2^{k} となる L :レベル k (レベル 0:距離 0)
  ・レベル 0, 1, 2, ..., 29 のグリッドを用意する
   ・幅は 2^k
   ・一辺 1.99 の正方形内で点対距離が 1 以上 → 8 個までありうる
  ・レベル 29, 28, ..., 0 の順に探索する:9 近傍
   ・答が見つかったらレベルを下げる。左向きに伝搬。
   ・レベルの減少は 30N 回までしか起きない
  */
  vc<pair<int, int>> point;
  vc<pair<int, int>> query;

  void add_point(int x, int y) {
    assert(0 <= x && x < (1 << LOG));
    assert(0 <= y && y < (1 << LOG));
    point.eb(x, y);
  }
  void add_query(int L, int R) {
    assert(R - L >= 2);
    query.eb(L, R);
  }
  ll dist(int i, int j) {
    ll dx = point[i].fi - point[j].fi;
    ll dy = point[i].se - point[j].se;
    return dx * dx + dy * dy;
  }

  vc<ll> calc() {
    static HashMap<int> MP;
    const int K = LOG;
    const int N = len(point), Q = len(query);
    using A9 = array<int, 9>;
    // それぞれのレベルのときのセル番号
    vv(int, IDX, K, N, -1);
    // 各セル番号に対する近傍
    vc<A9> nbd;
    FOR(k, 1, K) {
      auto to_ll = [&](int x, int y) -> ll { return ll(x) << 30 | y; };
      int off = len(nbd);
      int p = off;
      MP.reset();
      FOR(i, N) {
        int x = point[i].fi >> (k);
        int y = point[i].se >> (k);
        ll key = to_ll(x, y);
        if (!MP.count(key)) MP[key] = p++;
        IDX[k][i] = MP[key];
      }
      nbd.resize(p);
      FOR(i, N) {
        int x = point[i].fi >> (k);
        int y = point[i].se >> (k);
        int me = MP[to_ll(x, y)];
        int s = 0;
        FOR(dx, -1, 2) FOR(dy, -1, 2) {
          ll key = to_ll(x + dx, y + dy);
          nbd[me][s++] = (MP.count(key) ? MP[key] : -1);
        }
      }
    }

    vc<array<int, 8>> dat(len(nbd), {-1, -1, -1, -1, -1, -1, -1, -1});
    auto add = [&](int k, int i) -> void {
      int idx = IDX[k][i];
      for (auto&& j: dat[idx]) {
        if (j == -1) {
          j = i;
          return;
        }
      }
    };
    auto rm = [&](int k, int i) -> void {
      int idx = IDX[k][i];
      for (auto&& j: dat[idx]) {
        if (j == i) {
          j = -1;
          return;
        }
      }
    };

    auto solve_level = [&](int k, int i) -> vc<pair<int, ll>> {
      // レベル k の点群に対する答の計算
      vc<pair<int, ll>> res;
      int me = IDX[k][i];
      for (auto&& idx: nbd[me]) {
        if (idx == -1) continue;
        for (auto&& j: dat[idx]) {
          if (j == -1) continue;
          res.eb(j, dist(i, j));
        }
      }
      return res;
    };
    Dual_SegTree<Monoid_Min<ll>> seg(N);
    vc<int> LEVEL(N, -1);
    auto get_lv = [&](ll d) -> int {
      if (d == 0) return 0;
      return topbit(d) / 2 + 1;
    };

    vc<int> left(Q);
    vvc<int> query_at(N);
    FOR(qid, Q) {
      auto [L, R] = query[qid];
      left[qid] = L;
      query_at[--R].eb(qid);
    }

    vi ANS(Q);

    FOR(R, N) {
      // R 番目の点を用いた答の更新
      vc<pair<int, ll>> upd;
      FOR(k, 1, K) {
        auto res = solve_level(k, R);
        upd.insert(upd.end(), all(res));
      }

      for (auto [i, d]: upd) {
        int lv = get_lv(d);
        if (seg.get(i) < d) continue;
        // 答えの更新
        seg.apply(0, i + 1, d);
        // レベルの更新
        while (i >= 0 && LEVEL[i] > lv) {
          rm(LEVEL[i], i);
          LEVEL[i] = lv;
          if (lv) add(lv, i);
          --i;
        }
      }
      LEVEL[R] = K - 1;
      add(K - 1, R);
      for (auto&& qid: query_at[R]) { ANS[qid] = seg.get(left[qid]); }
    }
    return ANS;
  }
};
#line 4 "main.cpp"

void solve() {
  LL(N, Q);
  Range_Closest_Pair_Query<30> X;
  FOR(N) {
    INT(a, b);
    X.add_point(a, b);
  }
  FOR(Q) {
    INT(l, r);
    X.add_query(--l, r);
  }
  for (auto&& x: X.calc()) print(x);
}

signed main() {
  solve();
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

5 5
2 4
1 1
3 3
5 1
4 2
1 5
2 3
2 4
3 5
1 3

output:

2
8
8
2
2

result:

ok 5 number(s): "2 8 8 2 2"

Test #2:

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

input:

2 1
1 1
1 1
1 2

output:

0

result:

ok 1 number(s): "0"

Test #3:

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

input:

2 1
1 100000000
100000000 1
1 2

output:

19999999600000002

result:

ok 1 number(s): "19999999600000002"

Test #4:

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

input:

20000 250000
3 10
5 4
4 7
1 5
2 1
10 6
2 3
8 4
2 1
8 5
9 8
7 7
4 5
2 7
9 4
9 10
3 2
9 5
10 2
9 2
3 1
9 9
6 5
9 5
9 10
9 1
1 2
8 8
3 4
7 6
6 2
6 8
6 6
8 4
10 2
1 1
10 2
8 3
4 4
5 5
5 1
4 9
7 6
6 8
6 4
1 6
10 3
3 2
4 10
6 8
9 7
2 10
7 8
10 7
3 2
5 1
6 4
7 9
1 3
4 9
4 8
9 4
5 2
2 2
9 2
9 2
9 6
6 9
8 7
...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 250000 numbers

Test #5:

score: 0
Accepted
time: 97ms
memory: 26228kb

input:

20000 250000
72 45
72 34
34 10
20 96
79 39
43 5
72 49
56 85
1 72
44 70
73 89
69 76
49 89
57 38
39 9
33 47
22 3
96 41
90 82
25 6
72 92
73 38
53 21
16 88
59 9
54 2
14 6
7 94
99 68
27 82
70 50
81 81
60 81
2 98
33 19
98 9
35 36
49 66
86 7
3 95
32 89
62 42
68 88
65 16
94 6
85 10
51 69
90 36
70 87
13 79
4...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
10
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0...

result:

ok 250000 numbers

Test #6:

score: 0
Accepted
time: 119ms
memory: 29580kb

input:

20000 250000
116 165
150 677
951 676
552 324
267 222
739 755
705 663
235 142
152 714
735 641
414 201
313 663
683 300
403 739
37 521
42 833
553 733
886 449
86 913
55 637
731 932
143 161
500 891
719 79
916 237
431 992
804 210
643 332
165 362
178 332
821 510
762 34
188 83
283 357
743 407
750 487
19 658...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
45
1
0
1
0
0
0
2
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
4
0
0
0
0
0
0
0
0
0
2
0
0
0
0
0
0
0
0
0
0
52
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
2
0
0
0
0
5
0
0
0
0
0
1
0
0
0
1
1
0
0
0
2
0
0
0
0
0
0
...

result:

ok 250000 numbers

Test #7:

score: 0
Accepted
time: 139ms
memory: 43048kb

input:

20000 250000
193144 901630
894860 3728
802840 155641
625273 792794
433205 942335
223506 874548
425235 550629
341169 950916
49296 305542
709463 512381
9742 994078
106664 845553
38691 373010
184728 331946
344567 438182
854583 291040
94405 555036
56330 494420
928479 123077
796593 798314
300374 490072
2...

output:

3576676
1219565
93925
2336788
8872
68
137
842657
137
8560936
13914025
28521
88362
8872
8872
52996
12869
86297
68
8137625
93925
12869
86297
8872
8872
8872
47888
8872
12869
107860
12869
59536
59536
425540
12869
59536
8872
93925
117225
137
137
52996
68
52996
137
8872
68
12869
137
68
86297
1514
159700
6...

result:

ok 250000 numbers

Test #8:

score: 0
Accepted
time: 151ms
memory: 52712kb

input:

20000 250000
65468917 46637638
46041830 58072288
95596278 49154795
57837493 40861050
21328886 69542502
7729300 7126264
2317013 48080367
75669670 20165373
93996778 88884044
8523082 62327896
123901 11925128
71901024 73104267
94991893 98591670
53591520 11543761
76785613 86286274
64694742 89591932
75687...

output:

185588005
3282196826
141969393
25769005
141969393
185588005
576346153849
141969393
141969393
185588005
141969393
141969393
141969393
141969393
135584833
141969393
141969393
485404589
1182793
1182793
35224007589
135584833
185588005
931246420
185588005
25769005
375240717
141969393
2245310308
239709665...

result:

ok 250000 numbers

Test #9:

score: 0
Accepted
time: 771ms
memory: 60928kb

input:

250000 250000
1 2
1 1
3 2
3 3
1 2
3 2
1 2
1 3
3 1
2 1
1 3
2 3
3 3
1 3
3 1
3 1
1 3
2 1
1 2
1 1
1 2
2 2
1 2
1 2
1 3
3 3
1 1
3 2
1 2
2 2
1 1
2 3
3 2
2 1
3 3
2 1
2 3
3 1
2 3
3 2
2 1
1 1
3 2
1 2
1 3
3 1
2 3
2 1
1 2
1 1
1 2
3 3
1 2
2 2
3 1
3 1
3 1
1 2
2 2
1 1
3 3
1 3
1 3
1 2
1 2
3 1
3 2
1 2
2 2
1 2
1 1
2 ...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 250000 numbers

Test #10:

score: 0
Accepted
time: 1098ms
memory: 73796kb

input:

250000 250000
65 333
64 141
52 164
104 499
329 292
187 279
178 394
92 488
223 487
457 262
355 475
466 223
450 293
397 22
390 379
257 389
339 162
228 267
446 78
116 3
28 400
63 319
255 491
459 301
340 321
183 340
469 6
288 268
383 446
456 13
478 383
109 79
142 317
132 219
168 347
30 398
59 453
192 33...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 250000 numbers

Test #11:

score: 0
Accepted
time: 1536ms
memory: 138652kb

input:

250000 250000
5939 214
4869 2285
3576 8124
1179 6365
863 9874
6034 7110
9688 5453
1631 1975
7330 8868
1035 8771
9222 9417
7858 1642
3145 4403
8553 6105
8162 2232
2192 4946
5925 8017
1235 5374
6897 5409
8507 8625
7239 4621
5581 4870
4979 1749
35 1282
9138 5489
1030 8851
4444 905
5808 4770
348 7535
16...

output:

1
4
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
2
0
0
0
2
0
0
0
0
0
0
0
0
0
0
0
0
25
1
0
0
0
1
0
0
0
0
0
0
2
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
2
0
2
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
1
0
4
0
0
0
1
21925
0
0
0
0
0
0
0
0
0
0
0
0...

result:

ok 250000 numbers

Test #12:

score: 0
Accepted
time: 2010ms
memory: 357272kb

input:

250000 250000
7455187 75182576
14834779 53171998
80916167 45846874
44479602 88587946
22419247 21232863
45054521 14420458
26938419 38366452
40688894 64933635
58825078 27729802
43992064 49857990
80761962 17266078
67198634 69525730
78961694 90909521
86333066 79243240
75184949 63327693
20070526 51545836...

output:

345163988
17297
17297
17297
3342826
2692082
9929385
17297
17297
8796836
17297
1024786
17297
17297
11911909
17297
17297
1518912
39645
3342826
623969
17297
17297
9929385
17297
17297
1931549
346277845
17297
17297
11911909
117753730
17297
5756500
623969
1024786
17297
17297
4290746
17297
17297
623969
172...

result:

ok 250000 numbers

Test #13:

score: 0
Accepted
time: 1872ms
memory: 358900kb

input:

250000 250000
46988183 58848304
68248752 17909655
83617101 6540168
35239739 61188742
53626925 43849602
93050637 37107186
81895363 18264817
49186829 46896179
432608 14970834
27450067 64324424
95545626 36896275
99221358 60404199
53496911 74842046
75080552 5484539
24301428 65640142
96567779 65503273
98...

output:

364165
364165
364165
364165
1118420
364165
2875970
364165
364165
18782189
364165
2443444
2443444
364165
364165
2443444
477485
4069129
4832930
6161897
26896946
364165
26896946
2875970
1028485
16319272
1028485
364165
10848521
364165
893349
8344033
1861925
893349
16319272
2875970
2443444
364165
1028485...

result:

ok 250000 numbers

Test #14:

score: 0
Accepted
time: 1876ms
memory: 357428kb

input:

250000 250000
91248546 19112361
80003885 9173894
78946509 40597624
69292429 19443411
62836211 54539297
21113442 50348948
79804335 68234343
72628821 86879420
57634006 80551531
56031660 58204680
64883168 19157561
57708639 65267544
16502324 51487377
52951273 64713455
36315517 95949044
53263309 72456798...

output:

173585
1749061
17137028
173585
173585
173585
173585
173585
829306
245785
173585
3192869
173585
1100852
39267488
173585
173585
245785
14222466
245785
173585
173585
173585
173585
2558933
173585
173585
27213905
829306
353040509
1100852
173585
173585
20949364
1100852
173585
173585
173585
11259905
919450...

result:

ok 250000 numbers

Test #15:

score: 0
Accepted
time: 1187ms
memory: 79208kb

input:

240032 250000
50000000 50000000
50002262 76010790
50001271 86525300
98641920 50000129
50002536 73103650
33803461 50003187
50001627 17251861
84626110 50001450
14694851 50001386
50002865 30387041
50002785 29538241
3543741 50000335
50002609 72329120
10652441 50001005
69740280 50002853
50000834 8838131
...

output:

112470002
112470002
112470002
112470002
112470002
112470002
112470002
112470002
112470002
112470002
112470002
112470002
112470002
112470002
112470002
112470002
112470002
112470002
112470002
112470002
112470002
112470002
112470002
112470002
112470002
112470002
112470002
112470002
112470002
112470002
...

result:

ok 250000 numbers

Test #16:

score: 0
Accepted
time: 1115ms
memory: 79192kb

input:

240032 250000
50000000 50000000
35893631 50003384
39734451 50003746
87851550 50001146
22238561 50002097
22864551 50002156
50000020 201591
24784961 50002337
50001288 13655071
50003130 66801310
33485161 50003157
4360711 50000412
16731971 50001578
11214771 50001058
50002790 70408710
50002188 23204071
5...

output:

112470002
112470002
112470002
112470002
112470002
112470002
112470002
112470002
112470002
112470002
112470002
112470002
112470002
112470002
112470002
112470002
112470002
112470002
112470002
112470002
112470002
112470002
112470002
112470002
112470002
112470002
112470002
112470002
112470002
112470002
...

result:

ok 250000 numbers

Test #17:

score: 0
Accepted
time: 871ms
memory: 65568kb

input:

250000 250000
50000000 50000000
50000001 49999999
50000002 49999998
50000003 49999997
50000004 49999996
50000005 49999995
50000006 49999994
50000007 49999993
50000008 49999992
50000009 49999991
50000010 49999990
50000011 49999989
50000012 49999988
50000013 49999987
50000014 49999986
50000015 4999998...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 250000 numbers

Test #18:

score: 0
Accepted
time: 1099ms
memory: 73756kb

input:

250000 250000
50000000 50000000
50000001 49999999
50000002 49999998
50000003 49999997
50000004 49999996
50000005 49999995
50000006 49999994
50000007 49999993
50000008 49999992
50000009 49999991
50000010 49999990
50000011 49999989
50000012 49999988
50000013 49999987
50000014 49999986
50000015 4999998...

output:

1
2
2
1
2
2
2
2
2
2
1
2
2
2
1
2
2
2
2
2
1
2
1
2
2
2
2
2
2
2
2
1
2
2
1
2
1
1
2
2
1
2
2
2
2
2
2
2
2
2
2
2
1
2
2
2
2
1
2
2
1
2
2
2
2
2
2
2
2
2
2
2
2
2
2
1
2
2
1
2
2
1
1
2
2
2
1
2
2
2
2
2
2
1
2
2
2
2
2
1
2
2
2
1
1
2
2
2
2
2
2
2
1
2
1
1
2
1
2
2
2
1
2
2
2
2
2
1
2
1
1
2
1
2
2
2
2
1
1
1
2
2
1
2
2
2
1
2
1
1
...

result:

ok 250000 numbers