QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#605062#9416. Intersection of PathsmaspyAC ✓942ms143108kbC++2031.3kb2024-10-02 15:18:322024-10-02 15:18:34

Judging History

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

  • [2024-10-02 15:18:34]
  • 评测
  • 测评结果:AC
  • 用时:942ms
  • 内存:143108kb
  • [2024-10-02 15:18:32]
  • 提交

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 u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
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 2 "library/graph/tree.hpp"

#line 2 "library/graph/base.hpp"

template <typename T>
struct Edge {
  int frm, to;
  T cost;
  int id;
};

template <typename T = int, bool directed = false>
struct Graph {
  static constexpr bool is_directed = directed;
  int N, M;
  using cost_type = T;
  using edge_type = Edge<T>;
  vector<edge_type> edges;
  vector<int> indptr;
  vector<edge_type> csr_edges;
  vc<int> vc_deg, vc_indeg, vc_outdeg;
  bool prepared;

  class OutgoingEdges {
  public:
    OutgoingEdges(const Graph* G, int l, int r) : G(G), l(l), r(r) {}

    const edge_type* begin() const {
      if (l == r) { return 0; }
      return &G->csr_edges[l];
    }

    const edge_type* end() const {
      if (l == r) { return 0; }
      return &G->csr_edges[r];
    }

  private:
    const Graph* G;
    int l, r;
  };

  bool is_prepared() { return prepared; }

  Graph() : N(0), M(0), prepared(0) {}
  Graph(int N) : N(N), M(0), prepared(0) {}

  void build(int n) {
    N = n, M = 0;
    prepared = 0;
    edges.clear();
    indptr.clear();
    csr_edges.clear();
    vc_deg.clear();
    vc_indeg.clear();
    vc_outdeg.clear();
  }

  void add(int frm, int to, T cost = 1, int i = -1) {
    assert(!prepared);
    assert(0 <= frm && 0 <= to && to < N);
    if (i == -1) i = M;
    auto e = edge_type({frm, to, cost, i});
    edges.eb(e);
    ++M;
  }

#ifdef FASTIO
  // wt, off
  void read_tree(bool wt = false, int off = 1) { read_graph(N - 1, wt, off); }

  void read_graph(int M, bool wt = false, int off = 1) {
    for (int m = 0; m < M; ++m) {
      INT(a, b);
      a -= off, b -= off;
      if (!wt) {
        add(a, b);
      } else {
        T c;
        read(c);
        add(a, b, c);
      }
    }
    build();
  }
#endif

  void build() {
    assert(!prepared);
    prepared = true;
    indptr.assign(N + 1, 0);
    for (auto&& e: edges) {
      indptr[e.frm + 1]++;
      if (!directed) indptr[e.to + 1]++;
    }
    for (int v = 0; v < N; ++v) { indptr[v + 1] += indptr[v]; }
    auto counter = indptr;
    csr_edges.resize(indptr.back() + 1);
    for (auto&& e: edges) {
      csr_edges[counter[e.frm]++] = e;
      if (!directed)
        csr_edges[counter[e.to]++] = edge_type({e.to, e.frm, e.cost, e.id});
    }
  }

  OutgoingEdges operator[](int v) const {
    assert(prepared);
    return {this, indptr[v], indptr[v + 1]};
  }

  vc<int> deg_array() {
    if (vc_deg.empty()) calc_deg();
    return vc_deg;
  }

  pair<vc<int>, vc<int>> deg_array_inout() {
    if (vc_indeg.empty()) calc_deg_inout();
    return {vc_indeg, vc_outdeg};
  }

  int deg(int v) {
    if (vc_deg.empty()) calc_deg();
    return vc_deg[v];
  }

  int in_deg(int v) {
    if (vc_indeg.empty()) calc_deg_inout();
    return vc_indeg[v];
  }

  int out_deg(int v) {
    if (vc_outdeg.empty()) calc_deg_inout();
    return vc_outdeg[v];
  }

#ifdef FASTIO
  void debug() {
    print("Graph");
    if (!prepared) {
      print("frm to cost id");
      for (auto&& e: edges) print(e.frm, e.to, e.cost, e.id);
    } else {
      print("indptr", indptr);
      print("frm to cost id");
      FOR(v, N) for (auto&& e: (*this)[v]) print(e.frm, e.to, e.cost, e.id);
    }
  }
#endif

  vc<int> new_idx;
  vc<bool> used_e;

  // G における頂点 V[i] が、新しいグラフで i になるようにする
  // {G, es}
  // sum(deg(v)) の計算量になっていて、
  // 新しいグラフの n+m より大きい可能性があるので注意
  Graph<T, directed> rearrange(vc<int> V, bool keep_eid = 0) {
    if (len(new_idx) != N) new_idx.assign(N, -1);
    int n = len(V);
    FOR(i, n) new_idx[V[i]] = i;
    Graph<T, directed> G(n);
    vc<int> history;
    FOR(i, n) {
      for (auto&& e: (*this)[V[i]]) {
        if (len(used_e) <= e.id) used_e.resize(e.id + 1);
        if (used_e[e.id]) continue;
        int a = e.frm, b = e.to;
        if (new_idx[a] != -1 && new_idx[b] != -1) {
          history.eb(e.id);
          used_e[e.id] = 1;
          int eid = (keep_eid ? e.id : -1);
          G.add(new_idx[a], new_idx[b], e.cost, eid);
        }
      }
    }
    FOR(i, n) new_idx[V[i]] = -1;
    for (auto&& eid: history) used_e[eid] = 0;
    G.build();
    return G;
  }

  Graph<T, true> to_directed_tree(int root = -1) {
    if (root == -1) root = 0;
    assert(!is_directed && prepared && M == N - 1);
    Graph<T, true> G1(N);
    vc<int> par(N, -1);
    auto dfs = [&](auto& dfs, int v) -> void {
      for (auto& e: (*this)[v]) {
        if (e.to == par[v]) continue;
        par[e.to] = v, dfs(dfs, e.to);
      }
    };
    dfs(dfs, root);
    for (auto& e: edges) {
      int a = e.frm, b = e.to;
      if (par[a] == b) swap(a, b);
      assert(par[b] == a);
      G1.add(a, b, e.cost);
    }
    G1.build();
    return G1;
  }

private:
  void calc_deg() {
    assert(vc_deg.empty());
    vc_deg.resize(N);
    for (auto&& e: edges) vc_deg[e.frm]++, vc_deg[e.to]++;
  }

  void calc_deg_inout() {
    assert(vc_indeg.empty());
    vc_indeg.resize(N);
    vc_outdeg.resize(N);
    for (auto&& e: edges) { vc_indeg[e.to]++, vc_outdeg[e.frm]++; }
  }
};
#line 4 "library/graph/tree.hpp"

// HLD euler tour をとっていろいろ。
template <typename GT>
struct Tree {
  using Graph_type = GT;
  GT &G;
  using WT = typename GT::cost_type;
  int N;
  vector<int> LID, RID, head, V, parent, VtoE;
  vc<int> depth;
  vc<WT> depth_weighted;

  Tree(GT &G, int r = 0, bool hld = 1) : G(G) { build(r, hld); }

  void build(int r = 0, bool hld = 1) {
    if (r == -1) return; // build を遅延したいとき
    N = G.N;
    LID.assign(N, -1), RID.assign(N, -1), head.assign(N, r);
    V.assign(N, -1), parent.assign(N, -1), VtoE.assign(N, -1);
    depth.assign(N, -1), depth_weighted.assign(N, 0);
    assert(G.is_prepared());
    int t1 = 0;
    dfs_sz(r, -1, hld);
    dfs_hld(r, t1);
  }

  void dfs_sz(int v, int p, bool hld) {
    auto &sz = RID;
    parent[v] = p;
    depth[v] = (p == -1 ? 0 : depth[p] + 1);
    sz[v] = 1;
    int l = G.indptr[v], r = G.indptr[v + 1];
    auto &csr = G.csr_edges;
    // 使う辺があれば先頭にする
    for (int i = r - 2; i >= l; --i) {
      if (hld && depth[csr[i + 1].to] == -1) swap(csr[i], csr[i + 1]);
    }
    int hld_sz = 0;
    for (int i = l; i < r; ++i) {
      auto e = csr[i];
      if (depth[e.to] != -1) continue;
      depth_weighted[e.to] = depth_weighted[v] + e.cost;
      VtoE[e.to] = e.id;
      dfs_sz(e.to, v, hld);
      sz[v] += sz[e.to];
      if (hld && chmax(hld_sz, sz[e.to]) && l < i) { swap(csr[l], csr[i]); }
    }
  }

  void dfs_hld(int v, int &times) {
    LID[v] = times++;
    RID[v] += LID[v];
    V[LID[v]] = v;
    bool heavy = true;
    for (auto &&e: G[v]) {
      if (depth[e.to] <= depth[v]) continue;
      head[e.to] = (heavy ? head[v] : e.to);
      heavy = false;
      dfs_hld(e.to, times);
    }
  }

  vc<int> heavy_path_at(int v) {
    vc<int> P = {v};
    while (1) {
      int a = P.back();
      for (auto &&e: G[a]) {
        if (e.to != parent[a] && head[e.to] == v) {
          P.eb(e.to);
          break;
        }
      }
      if (P.back() == a) break;
    }
    return P;
  }

  int heavy_child(int v) {
    int k = LID[v] + 1;
    if (k == N) return -1;
    int w = V[k];
    return (parent[w] == v ? w : -1);
  }

  int e_to_v(int eid) {
    auto e = G.edges[eid];
    return (parent[e.frm] == e.to ? e.frm : e.to);
  }
  int v_to_e(int v) { return VtoE[v]; }
  int get_eid(int u, int v) {
    if (parent[u] != v) swap(u, v);
    assert(parent[u] == v);
    return VtoE[u];
  }

  int ELID(int v) { return 2 * LID[v] - depth[v]; }
  int ERID(int v) { return 2 * RID[v] - depth[v] - 1; }

  // 目標地点へ進む個数が k
  int LA(int v, int k) {
    assert(k <= depth[v]);
    while (1) {
      int u = head[v];
      if (LID[v] - k >= LID[u]) return V[LID[v] - k];
      k -= LID[v] - LID[u] + 1;
      v = parent[u];
    }
  }
  int la(int u, int v) { return LA(u, v); }

  int LCA(int u, int v) {
    for (;; v = parent[head[v]]) {
      if (LID[u] > LID[v]) swap(u, v);
      if (head[u] == head[v]) return u;
    }
  }

  int meet(int a, int b, int c) { return LCA(a, b) ^ LCA(a, c) ^ LCA(b, c); }
  int lca(int u, int v) { return LCA(u, v); }

  int subtree_size(int v, int root = -1) {
    if (root == -1) return RID[v] - LID[v];
    if (v == root) return N;
    int x = jump(v, root, 1);
    if (in_subtree(v, x)) return RID[v] - LID[v];
    return N - RID[x] + LID[x];
  }

  int dist(int a, int b) {
    int c = LCA(a, b);
    return depth[a] + depth[b] - 2 * depth[c];
  }

  WT dist_weighted(int a, int b) {
    int c = LCA(a, b);
    return depth_weighted[a] + depth_weighted[b] - WT(2) * depth_weighted[c];
  }

  // a is in b
  bool in_subtree(int a, int b) { return LID[b] <= LID[a] && LID[a] < RID[b]; }

  int jump(int a, int b, ll k) {
    if (k == 1) {
      if (a == b) return -1;
      return (in_subtree(b, a) ? LA(b, depth[b] - depth[a] - 1) : parent[a]);
    }
    int c = LCA(a, b);
    int d_ac = depth[a] - depth[c];
    int d_bc = depth[b] - depth[c];
    if (k > d_ac + d_bc) return -1;
    if (k <= d_ac) return LA(a, k);
    return LA(b, d_ac + d_bc - k);
  }

  vc<int> collect_child(int v) {
    vc<int> res;
    for (auto &&e: G[v])
      if (e.to != parent[v]) res.eb(e.to);
    return res;
  }

  vc<int> collect_light(int v) {
    vc<int> res;
    bool skip = true;
    for (auto &&e: G[v])
      if (e.to != parent[v]) {
        if (!skip) res.eb(e.to);
        skip = false;
      }
    return res;
  }

  vc<pair<int, int>> get_path_decomposition(int u, int v, bool edge) {
    // [始点, 終点] の"閉"区間列。
    vc<pair<int, int>> up, down;
    while (1) {
      if (head[u] == head[v]) break;
      if (LID[u] < LID[v]) {
        down.eb(LID[head[v]], LID[v]);
        v = parent[head[v]];
      } else {
        up.eb(LID[u], LID[head[u]]);
        u = parent[head[u]];
      }
    }
    if (LID[u] < LID[v]) down.eb(LID[u] + edge, LID[v]);
    elif (LID[v] + edge <= LID[u]) up.eb(LID[u], LID[v] + edge);
    reverse(all(down));
    up.insert(up.end(), all(down));
    return up;
  }

  // 辺の列の情報 (frm,to,str)
  // str = "heavy_up", "heavy_down", "light_up", "light_down"
  vc<tuple<int, int, string>> get_path_decomposition_detail(int u, int v) {
    vc<tuple<int, int, string>> up, down;
    while (1) {
      if (head[u] == head[v]) break;
      if (LID[u] < LID[v]) {
        if (v != head[v]) down.eb(head[v], v, "heavy_down"), v = head[v];
        down.eb(parent[v], v, "light_down"), v = parent[v];
      } else {
        if (u != head[u]) up.eb(u, head[u], "heavy_up"), u = head[u];
        up.eb(u, parent[u], "light_up"), u = parent[u];
      }
    }
    if (LID[u] < LID[v]) down.eb(u, v, "heavy_down");
    elif (LID[v] < LID[u]) up.eb(u, v, "heavy_up");
    reverse(all(down));
    concat(up, down);
    return up;
  }

  vc<int> restore_path(int u, int v) {
    vc<int> P;
    for (auto &&[a, b]: get_path_decomposition(u, v, 0)) {
      if (a <= b) {
        FOR(i, a, b + 1) P.eb(V[i]);
      } else {
        FOR_R(i, b, a + 1) P.eb(V[i]);
      }
    }
    return P;
  }

  // path [a,b] と [c,d] の交わり. 空ならば {-1,-1}.
  // https://codeforces.com/problemset/problem/500/G
  pair<int, int> path_intersection(int a, int b, int c, int d) {
    int ab = lca(a, b), ac = lca(a, c), ad = lca(a, d);
    int bc = lca(b, c), bd = lca(b, d), cd = lca(c, d);
    int x = ab ^ ac ^ bc, y = ab ^ ad ^ bd; // meet(a,b,c), meet(a,b,d)
    if (x != y) return {x, y};
    int z = ac ^ ad ^ cd;
    if (x != z) x = -1;
    return {x, x};
  }
};
#line 2 "library/graph/ds/static_toptree.hpp"

/*
参考 joitour tatyam
クラスタは根が virtual なもののみであるような簡易版
N 個の (頂+辺) をマージしていって,木全体+根から親への辺とする.
single(v) : v とその親辺を合わせたクラスタ
rake(L,R) : L の boundary を維持
compress(L,R)  (top-down) 順に x,y
*/
template <typename TREE>
struct Static_TopTree {
  int N;
  TREE &tree;
  vc<int> par, lch, rch, A, B; // A, B boundary (top-down)
  vc<bool> is_compress;

  Static_TopTree(TREE &tree) : tree(tree) { build(); }

  void build() {
    N = tree.N;
    par.assign(N, -1), lch.assign(N, -1), rch.assign(N, -1), A.assign(N, -1), B.assign(N, -1), is_compress.assign(N, 0);
    FOR(v, N) { A[v] = tree.parent[v], B[v] = v; }
    build_dfs(tree.V[0]);
    assert(len(par) == 2 * N - 1);
  }

  // 木全体での集約値を得る
  // single(v) : v とその親辺を合わせたクラスタ
  // rake(x, y, u, v) uv(top down) が boundary になるように rake (maybe v=-1)
  // compress(x,y,a,b,c)  (top-down) 順に (a,b] + (b,c]
  template <typename TREE_DP, typename F>
  typename TREE_DP::value_type tree_dp(F single) {
    using Data = typename TREE_DP::value_type;
    auto dfs = [&](auto &dfs, int k) -> Data {
      if (0 <= k && k < N) return single(k);
      Data x = dfs(dfs, lch[k]), y = dfs(dfs, rch[k]);
      if (is_compress[k]) {
        assert(B[lch[k]] == A[rch[k]]);
        return TREE_DP::compress(x, y);
      }
      return TREE_DP::rake(x, y);
    };
    return dfs(dfs, 2 * N - 2);
  }

private:
  int new_node(int l, int r, int a, int b, bool c) {
    int v = len(par);
    par.eb(-1), lch.eb(l), rch.eb(r), A.eb(a), B.eb(b), is_compress.eb(c);
    par[l] = par[r] = v;
    return v;
  }

  // height, node idx
  // compress 参考:https://atcoder.jp/contests/abc351/editorial/9910
  // ただし heavy path の選び方までは考慮しない
  pair<int, int> build_dfs(int v) {
    assert(tree.head[v] == v);
    auto path = tree.heavy_path_at(v);
    vc<pair<int, int>> stack;
    stack.eb(0, path[0]);
    auto merge_last_two = [&]() -> void {
      auto [h2, k2] = POP(stack);
      auto [h1, k1] = POP(stack);
      stack.eb(max(h1, h2) + 1, new_node(k1, k2, A[k1], B[k2], true));
    };

    FOR(i, 1, len(path)) {
      pqg<pair<int, int>> que;
      int k = path[i];
      que.emplace(0, k);
      for (auto &c: tree.collect_light(path[i - 1])) { que.emplace(build_dfs(c)); }
      while (len(que) >= 2) {
        auto [h1, i1] = POP(que);
        auto [h2, i2] = POP(que);
        if (i2 == k) swap(i1, i2);
        int i3 = new_node(i1, i2, A[i1], B[i1], false);
        if (k == i1) k = i3;
        que.emplace(max(h1, h2) + 1, i3);
      }
      stack.eb(POP(que));

      while (1) {
        int n = len(stack);
        if (n >= 3 && (stack[n - 3].fi == stack[n - 2].fi || stack[n - 3].fi <= stack[n - 1].fi)) {
          auto [h3, k3] = POP(stack);
          merge_last_two(), stack.eb(h3, k3);
        }
        elif (n >= 2 && stack[n - 2].fi <= stack[n - 1].fi) { merge_last_two(); }
        else break;
      }
    }
    while (len(stack) >= 2) { merge_last_two(); }
    return POP(stack);
  }
};
#line 2 "library/graph/ds/dynamic_tree_dp.hpp"

// reroot できない簡易版
// https://codeforces.com/contest/1172/problem/E
// https://codeforces.com/contest/1942/problem/H
// single(v) : v とその親辺を合わせたクラスタ
// rake(L,R) : L の boundary を維持
// compress(L,R)  (top-down) 順に L,R
template <typename TREE, typename TREE_DP>
struct Dynamic_Tree_Dp {
  using X = typename TREE_DP::value_type;
  Static_TopTree<TREE> STT;
  vc<X> dp;

  template <typename F>
  Dynamic_Tree_Dp(TREE& tree, F single) : STT(tree) {
    int N = tree.N;
    dp.resize(2 * N - 1);
    FOR(i, N) dp[i] = single(i);
    FOR(i, N, 2 * N - 1) update(i);
  }

  void set(int v, X x) {
    dp[v] = x;
    for (int i = STT.par[v]; i != -1; i = STT.par[i]) update(i);
  }

  X prod_all() { return dp.back(); }

private:
  inline void update(int i) {
    X &L = dp[STT.lch[i]], &R = dp[STT.rch[i]];
    dp[i] = (STT.is_compress[i] ? TREE_DP::compress(L, R) : TREE_DP::rake(L, R));
  }
};
#line 6 "main.cpp"

struct Data {
  ll ans = 0, L = 0, R = 0;
  ll LR = -infty<ll>;
  void out() { SHOW(ans, L, R, LR); }
};

struct TDP {
  using value_type = Data;
  static Data rake(Data A, Data B) {
    Data X{};
    chmax(X.ans, A.ans);
    chmax(X.ans, B.ans);
    chmax(X.ans, A.L + B.L);
    X.L = max(A.L, B.L);
    X.R = max(A.R, A.LR + B.L);
    X.LR = A.LR;
    return X;
  }
  static Data compress(Data A, Data B) {
    Data X{};
    chmax(X.ans, A.ans);
    chmax(X.ans, B.ans);
    chmax(X.ans, A.R + B.L);
    X.L = max(A.L, A.LR + B.L);
    X.R = max(B.R, B.LR + A.R);
    X.LR = -infty<ll>;
    if (A.LR >= 0 && B.LR >= 0) chmax(X.LR, A.LR + B.LR);
    return X;
  }
};

struct QT {
  int eid, b, k;
};

void solve() {
  LL(N, Q);
  Graph<int, 0> G(N);
  G.read_tree(1);
  Tree<decltype(G)> tree(G);

  vc<QT> query;
  FOR(q, Q) {
    INT(a, b, c);
    --a;
    query.eb(a, b, c);
  }

  vvc<int> I(N + 1);
  vvc<int> QI(N + 1);
  FOR(q, Q) { QI[query[q].k].eb(q); }
  FOR(i, N - 1) {
    auto e = G.edges[i];
    int a = e.frm, b = e.to;
    int n = min(tree.subtree_size(a, b), tree.subtree_size(b, a));
    I[n].eb(i);
  }

  auto f = [&](ll x) -> Data { return {max(0LL, x), max(0LL, x), max(0LL, x), x}; };

  Dynamic_Tree_Dp<decltype(tree), TDP> X(tree, [&](int v) -> Data { return f(-infty<ll>); });

  vi ANS(Q);

  FOR_R(t, N + 1) {
    for (auto& i: I[t]) {
      auto [a, b, c, eid] = G.edges[i];
      int j = tree.e_to_v(eid);
      SHOW("set", j, c);
      X.set(j, f(c));
    }
    for (auto& q: QI[t]) {
      auto [eid, wt, k] = query[q];
      auto [a, b, c, idx] = G.edges[eid];
      int j = tree.e_to_v(eid);
      int n = min(tree.subtree_size(a, b), tree.subtree_size(b, a));
      if (n >= t) {
        SHOW("set", j, wt);
        X.set(j, f(wt));
      }
      //
      ANS[q] = X.prod_all().ans;
      SHOW(ANS[q]);
      if (n >= t) {
        SHOW("set", j, c);
        X.set(j, f(c));
      }
    }
  }
  for (auto& x: ANS) print(x);
}

signed main() { solve(); }

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

詳細信息

Test #1:

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

input:

7 3
1 2 20
2 3 10
2 4 40
4 6 10
1 5 30
5 7 10
2 100 1
5 50 2
2 100 3

output:

160
110
20

result:

ok 3 lines

Test #2:

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

input:

200 500
124 178 427099307
158 192 319431399
117 104 710101310
194 96 839101283
136 4 101584313
105 185 76238080
84 121 653168782
143 136 831330689
147 53 258107910
183 161 822725527
171 165 701914427
127 83 753685257
94 167 437105834
41 173 974941718
33 54 850655642
140 32 414784060
40 24 166931598
...

output:

4662267167
6250994729
4662267167
0
9861651445
0
0
2874455352
1871656394
1444557087
4662267167
1444557087
6250994729
5898042200
5898042200
0
1444557087
2155191170
1444557087
1444557087
649122524
1444557087
0
5715608407
649122524
0
1444557087
5715608407
0
2874455352
649122524
0
0
6585664630
5898042200...

result:

ok 500 lines

Test #3:

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

input:

200 500
112 128 943053618
196 10 109992136
92 19 372527046
193 51 8504187
111 176 997847081
64 7 511677289
87 64 59358634
92 121 204355409
33 122 426611600
19 28 382475107
113 187 928826711
68 64 914241911
133 40 791046827
8 193 140839139
155 38 35149576
166 82 350823876
139 151 293902264
148 41 921...

output:

8195272055
18038155199
18015634184
6114250165
10575951438
6114250165
13052879291
7036092739
0
13052879291
6077290338
2194906169
12123675487
0
3064424495
3934400794
19314790986
7036092739
0
8706949344
7036092739
11130725173
0
13052879291
0
12123675487
3934400794
3934006014
6553353146
6077290338
12123...

result:

ok 500 lines

Test #4:

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

input:

200 500
30 4 813691610
84 120 817637491
88 111 663721050
5 72 404108850
70 36 348963382
150 134 45804645
188 158 37935708
169 16 882040499
80 12 694412572
97 170 811712290
127 38 9501854
128 115 389754928
75 192 597486526
133 193 975891833
41 100 780943771
161 142 197395217
50 21 884175932
83 84 995...

output:

22172546870
52354768762
27703719679
20639364056
40927307908
14352624650
13315172638
14352624650
42670461106
39281433571
21716516085
50064335349
40927307908
45161769836
33491991214
45263858848
43550274975
26858825538
51968402130
24594720223
46510771787
25974649606
3468011441
55153680913
2278040758
54...

result:

ok 500 lines

Test #5:

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

input:

200 500
50 185 630130276
185 90 932042903
6 187 368883288
14 103 932774738
145 93 183033691
132 60 234965135
184 80 467923508
103 166 358970344
61 98 647414179
9 2 588228845
168 93 442821577
100 61 992122892
60 187 290721521
184 68 552742642
25 38 68998047
13 1 870165451
58 60 931574059
60 125 20953...

output:

0
0
785362711
0
0
0
0
0
0
0
0
1719788928
0
0
0
785362711
1719788928
0
0
0
1531836478
0
3536428150
0
0
0
0
1531836478
0
1583664077
0
0
0
1531836478
2714881481
785362711
0
0
0
0
0
1719788928
1719788928
1583664077
0
1873786210
0
0
0
0
785362711
0
0
0
1719788928
0
0
0
0
0
0
0
0
0
1531836478
1531836478
2...

result:

ok 500 lines

Test #6:

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

input:

200 500
5 27 388148671
5 119 680224137
173 87 986442294
66 147 118501544
22 147 676951375
161 5 727540296
93 5 232588510
147 88 385371846
196 147 314746005
35 178 19636528
98 5 242192853
195 178 544655136
55 5 271306482
5 155 942384947
56 178 303301289
181 5 671105997
147 153 54431483
5 142 49742903...

output:

0
0
0
194315768
0
0
201368555
0
0
194315768
0
0
194315768
0
194315768
0
0
0
395268412
0
0
194315768
3142868981
0
0
0
0
194315768
376961479
395268412
0
194315768
0
0
194315768
194315768
0
0
194315768
0
395268412
0
0
194315768
376961479
194315768
0
395268412
395268412
376961479
0
201368555
0
194315768...

result:

ok 500 lines

Test #7:

score: 0
Accepted
time: 724ms
memory: 140152kb

input:

500000 500000
317156 54965 230759833
353491 77148 577942447
69059 132442 385326926
271915 94996 349962753
426805 265784 125714287
275095 49947 676489232
27293 430253 301562436
187863 475264 955971338
353263 433269 143567602
357681 310259 581877350
226404 39050 934609526
239119 204471 523754243
59587...

output:

1035108567
6886920194
393499496
9261470755
550314199
1035108567
1035108567
14330359099
550314199
393499496
0
1035108567
0
550314199
0
550314199
7341784214
393499496
550314199
550314199
393499496
550314199
550314199
0
1035108567
12626406441
14330359099
0
550314199
550314199
14330359099
1035108567
0
5...

result:

ok 500000 lines

Test #8:

score: 0
Accepted
time: 623ms
memory: 140252kb

input:

500000 500000
101136 182341 255570946
45109 39454 824459295
292096 91801 196644175
300463 233417 961460180
5484 385014 198620547
464291 129845 942200533
99694 311784 122024892
27995 363914 559175166
200488 154575 773940047
92565 121726 574493648
180505 383816 397190682
231591 367594 936808806
4973 6...

output:

1720496473
0
16441040338
21036781141
0
14014394631
29189202442
8978191390
14014394631
29189202442
1720496473
14014394631
59812673829
7305842926
1720496473
1720496473
14014394631
10376295567
57941139695
25226868378
1720496473
14014394631
1720496473
7305842926
1720496473
8978191390
21036781141
3515557...

result:

ok 500000 lines

Test #9:

score: 0
Accepted
time: 790ms
memory: 140224kb

input:

500000 500000
117422 56054 567560567
37609 51426 693318154
369948 25732 191341054
321431 469509 292868280
195218 150408 289330899
243114 32964 644463794
177181 3035 79162702
415066 204754 968798525
173772 440746 519973405
353210 75063 871376863
496610 208175 604393048
56122 126699 574969590
471302 3...

output:

19395057376
0
243758554869
0
19395057376
217129057419
0
86785348108
0
19395057376
0
0
406352878791
110510030058
326764779846
203656112567
28076461907
86785348108
301036711918
39759627385
0
0
86785348108
140469878703
86785348108
28076461907
28548372637
217129057419
19395057376
38312190750
21712905741...

result:

ok 500000 lines

Test #10:

score: 0
Accepted
time: 861ms
memory: 140836kb

input:

500000 500000
385104 316086 203846482
90329 213814 14603218
119475 462619 752784726
498372 392323 865975281
304606 356710 129730417
242416 90188 373943907
292652 212607 63942753
111888 117722 855017284
362999 185574 323831658
302995 193341 143516276
370319 477011 115160893
212555 260622 552905885
40...

output:

961669517763
0
415448480082
125646612706
404921129645
0
378221674796
328765391722
262703284594
434520533651
415448480082
1168166453100
41888634064
114198363306
1064978063360
934717591514
729363217
221837076088
0
415448480082
305153161603
281434671538
2953480407378
630659988687
729363217
221837076088...

result:

ok 500000 lines

Test #11:

score: 0
Accepted
time: 786ms
memory: 143108kb

input:

500000 500000
212178 335920 810458391
3968 383346 371060553
254524 168866 389037327
128669 470844 205547455
58828 217327 475404547
26846 200911 855903434
439798 233100 958501687
106285 324294 316767886
148013 368341 95627209
325855 279353 343917744
370568 273994 95266415
181067 395743 971354233
1930...

output:

1657697652188
588524410090
3986517524575
4292654087134
14745007507790
6859218245479
8699750506287
5877970905246
7621845358668
13425818817641
4291312458514
8886887696888
3970061425113
883771794077
2146471095474
6356949872854
9677944159886
601648300654
2818441761905
8473319373686
2772802043808
1320412...

result:

ok 500000 lines

Test #12:

score: 0
Accepted
time: 942ms
memory: 141004kb

input:

500000 500000
31555 288134 394613692
355957 458485 375579843
126806 46407 545440829
406838 419632 549006915
172681 188725 236842242
105275 431054 900847846
478484 376936 229539493
433118 387062 650606705
23384 213517 58546600
254648 521 62347332
135409 21964 709872982
480882 107159 111319611
136690 ...

output:

248260194
248260194
248260194
765530529
765530529
0
248260194
248260194
0
0
248260194
1631260856
0
0
0
0
248260194
248260194
0
248260194
248260194
248260194
248260194
0
0
248260194
248260194
765530529
0
0
0
0
248260194
0
0
0
0
248260194
248260194
248260194
0
248260194
0
248260194
248260194
248260194...

result:

ok 500000 lines

Test #13:

score: 0
Accepted
time: 489ms
memory: 141396kb

input:

500000 500000
401547 432681 418666883
374165 94786 721328975
389649 185309 178564131
444634 139701 493003972
34811 377124 601980813
42045 398192 535069177
67009 437437 668672486
495940 197831 745314009
255457 10319 552592132
152468 134332 101366144
112209 341038 945768202
67542 275586 477864247
8089...

output:

0
854433336
0
0
0
0
1869108742
0
0
1517850672
0
0
0
0
854433336
854433336
1844438150
0
854433336
0
0
0
1517850672
0
0
0
0
0
0
0
1517850672
0
0
0
0
854433336
0
1517850672
0
0
0
0
0
0
0
0
0
0
0
1517850672
0
0
0
854433336
0
854433336
0
854433336
0
0
1517850672
0
0
1517850672
0
1517850672
1517850672
0
0...

result:

ok 500000 lines

Test #14:

score: 0
Accepted
time: 697ms
memory: 139868kb

input:

500000 500000
457852 271238 917738694
266814 474275 414331550
435124 77090 357330460
229795 188172 589247914
205253 173271 504388850
59713 483685 539037652
284974 376871 359402095
60301 499649 709358880
144507 224701 328191001
457309 40020 802196106
301404 356725 210765686
96399 290906 484238562
383...

output:

0
0
0
0
0
936571055
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
936571055
0
0
0
0
0
0
0
0
1511230822
0
0
0
0
936571055
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
3654148537
0
0
0
936571055
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
936571055
0
0
0
0
1986401741
0
0
0
0
0
0
0
0
0...

result:

ok 500000 lines

Test #15:

score: 0
Accepted
time: 611ms
memory: 139416kb

input:

500000 500000
43888 48212 179626807
450799 64537 503182622
271313 327157 748716512
89686 209891 668339242
244235 178213 13339384
387281 297281 487239241
362793 154117 821483132
225770 172442 336980690
5589 184727 928795315
310841 366390 410466687
131748 359089 752402192
61148 236886 35578549
490278 ...

output:

1371380934
0
0
0
0
0
0
0
0
0
0
0
1371380934
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
794586902
0
0
0
0
1371380934
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1968089286
0
1436483857
0
0
1831337801
0
0
0
1905534453
0
0
0
794586902
0
0
0
0
0
0
0
0
0
0
0
0
794586902
0
0
0
0
794586902
0
0
0
0
0
0
0
0
0
0
0
0
0
1642806249
0
0
0
...

result:

ok 500000 lines

Extra Test:

score: 0
Extra Test Passed