QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#596359 | #9416. Intersection of Paths | ucup-team987# | AC ✓ | 1436ms | 336408kb | C++23 | 41.9kb | 2024-09-28 15:35:53 | 2024-09-28 15:35:54 |
Judging History
answer
/**
* date : 2024-09-28 16:35:37
* author : Nyaan
*/
#define NDEBUG
#include <ranges>
using namespace std;
// intrinstic
#include <immintrin.h>
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfenv>
#include <cfloat>
#include <chrono>
#include <cinttypes>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdarg>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <initializer_list>
#include <iomanip>
#include <ios>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <streambuf>
#include <string>
#include <tr2/dynamic_bitset>
#include <tuple>
#include <type_traits>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// utility
namespace Nyaan {
using ll = long long;
using i64 = long long;
using u64 = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
template <typename T>
using V = vector<T>;
template <typename T>
using VV = vector<vector<T>>;
using vi = vector<int>;
using vl = vector<long long>;
using vd = V<double>;
using vs = V<string>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<long long>>;
template <typename T>
using minpq = priority_queue<T, vector<T>, greater<T>>;
template <typename T, typename U>
struct P : pair<T, U> {
template <typename... Args>
constexpr P(Args... args) : pair<T, U>(args...) {}
using pair<T, U>::first;
using pair<T, U>::second;
P &operator+=(const P &r) {
first += r.first;
second += r.second;
return *this;
}
P &operator-=(const P &r) {
first -= r.first;
second -= r.second;
return *this;
}
P &operator*=(const P &r) {
first *= r.first;
second *= r.second;
return *this;
}
template <typename S>
P &operator*=(const S &r) {
first *= r, second *= r;
return *this;
}
P operator+(const P &r) const { return P(*this) += r; }
P operator-(const P &r) const { return P(*this) -= r; }
P operator*(const P &r) const { return P(*this) *= r; }
template <typename S>
P operator*(const S &r) const {
return P(*this) *= r;
}
P operator-() const { return P{-first, -second}; }
};
using pl = P<ll, ll>;
using pi = P<int, int>;
using vp = V<pl>;
constexpr int inf = 1001001001;
constexpr long long infLL = 4004004004004004004LL;
template <typename T>
int sz(const T &t) {
return t.size();
}
template <typename T, typename U>
inline bool amin(T &x, U y) {
return (y < x) ? (x = y, true) : false;
}
template <typename T, typename U>
inline bool amax(T &x, U y) {
return (x < y) ? (x = y, true) : false;
}
template <typename T>
inline T Max(const vector<T> &v) {
return *max_element(begin(v), end(v));
}
template <typename T>
inline T Min(const vector<T> &v) {
return *min_element(begin(v), end(v));
}
template <typename T>
inline long long Sum(const vector<T> &v) {
return accumulate(begin(v), end(v), 0LL);
}
template <typename T>
int lb(const vector<T> &v, const T &a) {
return lower_bound(begin(v), end(v), a) - begin(v);
}
template <typename T>
int ub(const vector<T> &v, const T &a) {
return upper_bound(begin(v), end(v), a) - begin(v);
}
constexpr long long TEN(int n) {
long long ret = 1, x = 10;
for (; n; x *= x, n >>= 1) ret *= (n & 1 ? x : 1);
return ret;
}
template <typename T, typename U>
pair<T, U> mkp(const T &t, const U &u) {
return make_pair(t, u);
}
template <typename T>
vector<T> mkrui(const vector<T> &v, bool rev = false) {
vector<T> ret(v.size() + 1);
if (rev) {
for (int i = int(v.size()) - 1; i >= 0; i--) ret[i] = v[i] + ret[i + 1];
} else {
for (int i = 0; i < int(v.size()); i++) ret[i + 1] = ret[i] + v[i];
}
return ret;
};
template <typename T>
vector<T> mkuni(const vector<T> &v) {
vector<T> ret(v);
sort(ret.begin(), ret.end());
ret.erase(unique(ret.begin(), ret.end()), ret.end());
return ret;
}
template <typename F>
vector<int> mkord(int N, F f) {
vector<int> ord(N);
iota(begin(ord), end(ord), 0);
sort(begin(ord), end(ord), f);
return ord;
}
template <typename T>
vector<int> mkinv(vector<T> &v) {
int max_val = *max_element(begin(v), end(v));
vector<int> inv(max_val + 1, -1);
for (int i = 0; i < (int)v.size(); i++) inv[v[i]] = i;
return inv;
}
vector<int> mkiota(int n) {
vector<int> ret(n);
iota(begin(ret), end(ret), 0);
return ret;
}
template <typename T>
T mkrev(const T &v) {
T w{v};
reverse(begin(w), end(w));
return w;
}
template <typename T>
bool nxp(T &v) {
return next_permutation(begin(v), end(v));
}
// 返り値の型は入力の T に依存
// i 要素目 : [0, a[i])
template <typename T>
vector<vector<T>> product(const vector<T> &a) {
vector<vector<T>> ret;
vector<T> v;
auto dfs = [&](auto rc, int i) -> void {
if (i == (int)a.size()) {
ret.push_back(v);
return;
}
for (int j = 0; j < a[i]; j++) v.push_back(j), rc(rc, i + 1), v.pop_back();
};
dfs(dfs, 0);
return ret;
}
// F : function(void(T&)), mod を取る操作
// T : 整数型のときはオーバーフローに注意する
template <typename T>
T Power(T a, long long n, const T &I, const function<void(T &)> &f) {
T res = I;
for (; n; f(a = a * a), n >>= 1) {
if (n & 1) f(res = res * a);
}
return res;
}
// T : 整数型のときはオーバーフローに注意する
template <typename T>
T Power(T a, long long n, const T &I = T{1}) {
return Power(a, n, I, function<void(T &)>{[](T &) -> void {}});
}
template <typename T>
T Rev(const T &v) {
T res = v;
reverse(begin(res), end(res));
return res;
}
template <typename T>
vector<T> Transpose(const vector<T> &v) {
using U = typename T::value_type;
if(v.empty()) return {};
int H = v.size(), W = v[0].size();
vector res(W, T(H, U{}));
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
res[j][i] = v[i][j];
}
}
return res;
}
template <typename T>
vector<T> Rotate(const vector<T> &v, int clockwise = true) {
using U = typename T::value_type;
int H = v.size(), W = v[0].size();
vector res(W, T(H, U{}));
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (clockwise) {
res[W - 1 - j][i] = v[i][j];
} else {
res[j][H - 1 - i] = v[i][j];
}
}
}
return res;
}
} // namespace Nyaan
// bit operation
namespace Nyaan {
__attribute__((target("popcnt"))) inline int popcnt(const u64 &a) {
return __builtin_popcountll(a);
}
inline int lsb(const u64 &a) { return a ? __builtin_ctzll(a) : 64; }
inline int ctz(const u64 &a) { return a ? __builtin_ctzll(a) : 64; }
inline int msb(const u64 &a) { return a ? 63 - __builtin_clzll(a) : -1; }
template <typename T>
inline int gbit(const T &a, int i) {
return (a >> i) & 1;
}
template <typename T>
inline void sbit(T &a, int i, bool b) {
if (gbit(a, i) != b) a ^= T(1) << i;
}
constexpr long long PW(int n) { return 1LL << n; }
constexpr long long MSK(int n) { return (1LL << n) - 1; }
} // namespace Nyaan
// inout
namespace Nyaan {
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
os << p.first << " " << p.second;
return os;
}
template <typename T, typename U>
istream &operator>>(istream &is, pair<T, U> &p) {
is >> p.first >> p.second;
return is;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
int s = (int)v.size();
for (int i = 0; i < s; i++) os << (i ? " " : "") << v[i];
return os;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
for (auto &x : v) is >> x;
return is;
}
istream &operator>>(istream &is, __int128_t &x) {
string S;
is >> S;
x = 0;
int flag = 0;
for (auto &c : S) {
if (c == '-') {
flag = true;
continue;
}
x *= 10;
x += c - '0';
}
if (flag) x = -x;
return is;
}
istream &operator>>(istream &is, __uint128_t &x) {
string S;
is >> S;
x = 0;
for (auto &c : S) {
x *= 10;
x += c - '0';
}
return is;
}
ostream &operator<<(ostream &os, __int128_t x) {
if (x == 0) return os << 0;
if (x < 0) os << '-', x = -x;
string S;
while (x) S.push_back('0' + x % 10), x /= 10;
reverse(begin(S), end(S));
return os << S;
}
ostream &operator<<(ostream &os, __uint128_t x) {
if (x == 0) return os << 0;
string S;
while (x) S.push_back('0' + x % 10), x /= 10;
reverse(begin(S), end(S));
return os << S;
}
void in() {}
template <typename T, class... U>
void in(T &t, U &...u) {
cin >> t;
in(u...);
}
void out() { cout << "\n"; }
template <typename T, class... U, char sep = ' '>
void out(const T &t, const U &...u) {
cout << t;
if (sizeof...(u)) cout << sep;
out(u...);
}
struct IoSetupNya {
IoSetupNya() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
cerr << fixed << setprecision(7);
}
} iosetupnya;
} // namespace Nyaan
// debug
#ifdef NyaanDebug
#define trc(...) (void(0))
#else
#define trc(...) (void(0))
#endif
#ifdef NyaanLocal
#define trc2(...) (void(0))
#else
#define trc2(...) (void(0))
#endif
// macro
#define each(x, v) for (auto&& x : v)
#define each2(x, y, v) for (auto&& [x, y] : v)
#define all(v) (v).begin(), (v).end()
#define rep(i, N) for (long long i = 0; i < (long long)(N); i++)
#define repr(i, N) for (long long i = (long long)(N)-1; i >= 0; i--)
#define rep1(i, N) for (long long i = 1; i <= (long long)(N); i++)
#define repr1(i, N) for (long long i = (N); (long long)(i) > 0; i--)
#define reg(i, a, b) for (long long i = (a); i < (b); i++)
#define regr(i, a, b) for (long long i = (b)-1; i >= (a); i--)
#define fi first
#define se second
#define ini(...) \
int __VA_ARGS__; \
in(__VA_ARGS__)
#define inl(...) \
long long __VA_ARGS__; \
in(__VA_ARGS__)
#define ins(...) \
string __VA_ARGS__; \
in(__VA_ARGS__)
#define in2(s, t) \
for (int i = 0; i < (int)s.size(); i++) { \
in(s[i], t[i]); \
}
#define in3(s, t, u) \
for (int i = 0; i < (int)s.size(); i++) { \
in(s[i], t[i], u[i]); \
}
#define in4(s, t, u, v) \
for (int i = 0; i < (int)s.size(); i++) { \
in(s[i], t[i], u[i], v[i]); \
}
#define die(...) \
do { \
Nyaan::out(__VA_ARGS__); \
return; \
} while (0)
namespace Nyaan {
void solve();
}
int main() { Nyaan::solve(); }
//
template <typename T>
struct edge {
int src, to;
T cost;
edge(int _to, T _cost) : src(-1), to(_to), cost(_cost) {}
edge(int _src, int _to, T _cost) : src(_src), to(_to), cost(_cost) {}
edge &operator=(const int &x) {
to = x;
return *this;
}
operator int() const { return to; }
};
template <typename T>
using Edges = vector<edge<T>>;
template <typename T>
using WeightedGraph = vector<Edges<T>>;
using UnweightedGraph = vector<vector<int>>;
// Input of (Unweighted) Graph
UnweightedGraph graph(int N, int M = -1, bool is_directed = false,
bool is_1origin = true) {
UnweightedGraph g(N);
if (M == -1) M = N - 1;
for (int _ = 0; _ < M; _++) {
int x, y;
cin >> x >> y;
if (is_1origin) x--, y--;
g[x].push_back(y);
if (!is_directed) g[y].push_back(x);
}
return g;
}
// Input of Weighted Graph
template <typename T>
WeightedGraph<T> wgraph(int N, int M = -1, bool is_directed = false,
bool is_1origin = true) {
WeightedGraph<T> g(N);
if (M == -1) M = N - 1;
for (int _ = 0; _ < M; _++) {
int x, y;
cin >> x >> y;
T c;
cin >> c;
if (is_1origin) x--, y--;
g[x].emplace_back(x, y, c);
if (!is_directed) g[y].emplace_back(y, x, c);
}
return g;
}
// Input of Edges
template <typename T>
Edges<T> esgraph([[maybe_unused]] int N, int M, int is_weighted = true,
bool is_1origin = true) {
Edges<T> es;
for (int _ = 0; _ < M; _++) {
int x, y;
cin >> x >> y;
T c;
if (is_weighted)
cin >> c;
else
c = 1;
if (is_1origin) x--, y--;
es.emplace_back(x, y, c);
}
return es;
}
// Input of Adjacency Matrix
template <typename T>
vector<vector<T>> adjgraph(int N, int M, T INF, int is_weighted = true,
bool is_directed = false, bool is_1origin = true) {
vector<vector<T>> d(N, vector<T>(N, INF));
for (int _ = 0; _ < M; _++) {
int x, y;
cin >> x >> y;
T c;
if (is_weighted)
cin >> c;
else
c = 1;
if (is_1origin) x--, y--;
d[x][y] = c;
if (!is_directed) d[y][x] = c;
}
return d;
}
/**
* @brief グラフテンプレート
* @docs docs/graph/graph-template.md
*/
// 一般のグラフのstからの距離!!!!
// unvisited nodes : d = -1
vector<int> Depth(const UnweightedGraph &g, int start = 0) {
int n = g.size();
vector<int> ds(n, -1);
ds[start] = 0;
queue<int> q;
q.push(start);
while (!q.empty()) {
int c = q.front();
q.pop();
int dc = ds[c];
for (auto &d : g[c]) {
if (ds[d] == -1) {
ds[d] = dc + 1;
q.push(d);
}
}
}
return ds;
}
// Depth of Rooted Weighted Tree
// unvisited nodes : d = -1
template <typename T>
vector<T> Depth(const WeightedGraph<T> &g, int start = 0) {
vector<T> d(g.size(), -1);
auto dfs = [&](auto rec, int cur, T val, int par = -1) -> void {
d[cur] = val;
for (auto &dst : g[cur]) {
if (dst == par) continue;
rec(rec, dst, val + dst.cost, cur);
}
};
dfs(dfs, start, 0);
return d;
}
// Diameter of Tree
// return value : { {u, v}, length }
pair<pair<int, int>, int> Diameter(const UnweightedGraph &g) {
auto d = Depth(g, 0);
int u = max_element(begin(d), end(d)) - begin(d);
d = Depth(g, u);
int v = max_element(begin(d), end(d)) - begin(d);
return make_pair(make_pair(u, v), d[v]);
}
// Diameter of Weighted Tree
// return value : { {u, v}, length }
template <typename T>
pair<pair<int, int>, T> Diameter(const WeightedGraph<T> &g) {
auto d = Depth(g, 0);
int u = max_element(begin(d), end(d)) - begin(d);
d = Depth(g, u);
int v = max_element(begin(d), end(d)) - begin(d);
return make_pair(make_pair(u, v), d[v]);
}
// nodes on the path u-v ( O(N) )
template <typename G>
vector<int> Path(G &g, int u, int v) {
vector<int> ret;
int end = 0;
auto dfs = [&](auto rec, int cur, int par = -1) -> void {
ret.push_back(cur);
if (cur == v) {
end = 1;
return;
}
for (int dst : g[cur]) {
if (dst == par) continue;
rec(rec, dst, cur);
if (end) return;
}
if (end) return;
ret.pop_back();
};
dfs(dfs, u);
return ret;
}
/**
* @brief グラフユーティリティ
* @docs docs/graph/graph-utility.md
*/
//
using namespace std;
using namespace std;
namespace internal {
template <typename T>
using is_broadly_integral =
typename conditional_t<is_integral_v<T> || is_same_v<T, __int128_t> ||
is_same_v<T, __uint128_t>,
true_type, false_type>::type;
template <typename T>
using is_broadly_signed =
typename conditional_t<is_signed_v<T> || is_same_v<T, __int128_t>,
true_type, false_type>::type;
template <typename T>
using is_broadly_unsigned =
typename conditional_t<is_unsigned_v<T> || is_same_v<T, __uint128_t>,
true_type, false_type>::type;
#define ENABLE_VALUE(x) \
template <typename T> \
constexpr bool x##_v = x<T>::value;
ENABLE_VALUE(is_broadly_integral);
ENABLE_VALUE(is_broadly_signed);
ENABLE_VALUE(is_broadly_unsigned);
#undef ENABLE_VALUE
#define ENABLE_HAS_TYPE(var) \
template <class, class = void> \
struct has_##var : false_type {}; \
template <class T> \
struct has_##var<T, void_t<typename T::var>> : true_type {}; \
template <class T> \
constexpr auto has_##var##_v = has_##var<T>::value;
#define ENABLE_HAS_VAR(var) \
template <class, class = void> \
struct has_##var : false_type {}; \
template <class T> \
struct has_##var<T, void_t<decltype(T::var)>> : true_type {}; \
template <class T> \
constexpr auto has_##var##_v = has_##var<T>::value;
} // namespace internal
namespace fastio {
static constexpr int SZ = 1 << 17;
static constexpr int offset = 64;
char inbuf[SZ], outbuf[SZ];
int in_left = 0, in_right = 0, out_right = 0;
struct Pre {
char num[40000];
constexpr Pre() : num() {
for (int i = 0; i < 10000; i++) {
int n = i;
for (int j = 3; j >= 0; j--) {
num[i * 4 + j] = n % 10 + '0';
n /= 10;
}
}
}
} constexpr pre;
void load() {
int len = in_right - in_left;
memmove(inbuf, inbuf + in_left, len);
in_right = len + fread(inbuf + len, 1, SZ - len, stdin);
in_left = 0;
}
void flush() {
fwrite(outbuf, 1, out_right, stdout);
out_right = 0;
}
void skip_space() {
if (in_left + offset > in_right) load();
while (inbuf[in_left] <= ' ') in_left++;
}
void single_read(char& c) {
if (in_left + offset > in_right) load();
skip_space();
c = inbuf[in_left++];
}
void single_read(string& S) {
skip_space();
while (true) {
if (in_left == in_right) load();
int i = in_left;
for (; i != in_right; i++) {
if (inbuf[i] <= ' ') break;
}
copy(inbuf + in_left, inbuf + i, back_inserter(S));
in_left = i;
if (i != in_right) break;
}
}
template <typename T,
enable_if_t<internal::is_broadly_integral_v<T>>* = nullptr>
void single_read(T& x) {
if (in_left + offset > in_right) load();
skip_space();
char c = inbuf[in_left++];
[[maybe_unused]] bool minus = false;
if constexpr (internal::is_broadly_signed_v<T>) {
if (c == '-') minus = true, c = inbuf[in_left++];
}
x = 0;
while (c >= '0') {
x = x * 10 + (c & 15);
c = inbuf[in_left++];
}
if constexpr (internal::is_broadly_signed_v<T>) {
if (minus) x = -x;
}
}
void rd() {}
template <typename Head, typename... Tail>
void rd(Head& head, Tail&... tail) {
single_read(head);
rd(tail...);
}
void single_write(const char& c) {
if (out_right > SZ - offset) flush();
outbuf[out_right++] = c;
}
void single_write(const bool& b) {
if (out_right > SZ - offset) flush();
outbuf[out_right++] = b ? '1' : '0';
}
void single_write(const string& S) {
flush(), fwrite(S.data(), 1, S.size(), stdout);
}
void single_write(const char* p) { flush(), fwrite(p, 1, strlen(p), stdout); }
template <typename T,
enable_if_t<internal::is_broadly_integral_v<T>>* = nullptr>
void single_write(const T& _x) {
if (out_right > SZ - offset) flush();
if (_x == 0) {
outbuf[out_right++] = '0';
return;
}
T x = _x;
if constexpr (internal::is_broadly_signed_v<T>) {
if (x < 0) outbuf[out_right++] = '-', x = -x;
}
constexpr int buffer_size = sizeof(T) * 10 / 4;
char buf[buffer_size];
int i = buffer_size;
while (x >= 10000) {
i -= 4;
memcpy(buf + i, pre.num + (x % 10000) * 4, 4);
x /= 10000;
}
if (x < 100) {
if (x < 10) {
outbuf[out_right] = '0' + x;
++out_right;
} else {
uint32_t q = (uint32_t(x) * 205) >> 11;
uint32_t r = uint32_t(x) - q * 10;
outbuf[out_right] = '0' + q;
outbuf[out_right + 1] = '0' + r;
out_right += 2;
}
} else {
if (x < 1000) {
memcpy(outbuf + out_right, pre.num + (x << 2) + 1, 3);
out_right += 3;
} else {
memcpy(outbuf + out_right, pre.num + (x << 2), 4);
out_right += 4;
}
}
memcpy(outbuf + out_right, buf + i, buffer_size - i);
out_right += buffer_size - i;
}
void wt() {}
template <typename Head, typename... Tail>
void wt(const Head& head, const Tail&... tail) {
single_write(head);
wt(std::forward<const Tail>(tail)...);
}
template <typename... Args>
void wtn(const Args&... x) {
wt(std::forward<const Args>(x)...);
wt('\n');
}
struct Dummy {
Dummy() { atexit(flush); }
} dummy;
} // namespace fastio
using fastio::rd;
using fastio::skip_space;
using fastio::wt;
using fastio::wtn;
using namespace std;
struct Timer {
chrono::high_resolution_clock::time_point st;
Timer() { reset(); }
void reset() { st = chrono::high_resolution_clock::now(); }
long long elapsed() {
auto ed = chrono::high_resolution_clock::now();
return chrono::duration_cast<chrono::milliseconds>(ed - st).count();
}
long long operator()() { return elapsed(); }
};
//
using namespace std;
using namespace std;
template <typename T>
struct has_cost {
private:
template <typename U>
static auto confirm(U u) -> decltype(u.cost, std::true_type());
static auto confirm(...) -> std::false_type;
public:
enum : bool { value = decltype(confirm(std::declval<T>()))::value };
};
template <typename T>
vector<vector<T>> inverse_tree(const vector<vector<T>>& g) {
int N = (int)g.size();
vector<vector<T>> rg(N);
for (int i = 0; i < N; i++) {
for (auto& e : g[i]) {
if constexpr (is_same<T, int>::value) {
rg[e].push_back(i);
} else if constexpr (has_cost<T>::value) {
rg[e].emplace_back(e.to, i, e.cost);
} else {
assert(0);
}
}
}
return rg;
}
template <typename T>
vector<vector<T>> rooted_tree(const vector<vector<T>>& g, int root = 0) {
int N = (int)g.size();
vector<vector<T>> rg(N);
vector<char> v(N, false);
v[root] = true;
queue<int> que;
que.emplace(root);
while (!que.empty()) {
auto p = que.front();
que.pop();
for (auto& e : g[p]) {
if (v[e] == false) {
v[e] = true;
que.push(e);
rg[p].push_back(e);
}
}
}
return rg;
}
/**
* @brief 根付き木・逆辺からなる木への変換
*/
template <typename G>
struct HeavyLightDecomposition {
private:
void dfs_sz(int cur) {
size[cur] = 1;
for (auto& dst : g[cur]) {
if (dst == par[cur]) {
if (g[cur].size() >= 2 && int(dst) == int(g[cur][0]))
swap(g[cur][0], g[cur][1]);
else
continue;
}
depth[dst] = depth[cur] + 1;
par[dst] = cur;
dfs_sz(dst);
size[cur] += size[dst];
if (size[dst] > size[g[cur][0]]) {
swap(dst, g[cur][0]);
}
}
}
void dfs_hld(int cur) {
down[cur] = id++;
for (auto dst : g[cur]) {
if (dst == par[cur]) continue;
nxt[dst] = (int(dst) == int(g[cur][0]) ? nxt[cur] : int(dst));
dfs_hld(dst);
}
up[cur] = id;
}
// [u, v)
vector<pair<int, int>> ascend(int u, int v) const {
vector<pair<int, int>> res;
while (nxt[u] != nxt[v]) {
res.emplace_back(down[u], down[nxt[u]]);
u = par[nxt[u]];
}
if (u != v) res.emplace_back(down[u], down[v] + 1);
return res;
}
// (u, v]
vector<pair<int, int>> descend(int u, int v) const {
if (u == v) return {};
if (nxt[u] == nxt[v]) return {{down[u] + 1, down[v]}};
auto res = descend(u, par[nxt[v]]);
res.emplace_back(down[nxt[v]], down[v]);
return res;
}
public:
G& g;
int root, id;
vector<int> size, depth, down, up, nxt, par;
HeavyLightDecomposition(G& _g, int _root = 0)
: g(_g),
root(_root),
id(0),
size(g.size(), 0),
depth(g.size(), 0),
down(g.size(), -1),
up(g.size(), -1),
nxt(g.size(), root),
par(g.size(), root) {
dfs_sz(root);
dfs_hld(root);
}
pair<int, int> idx(int i) const { return make_pair(down[i], up[i]); }
template <typename F>
void path_query(int u, int v, bool vertex, const F& f) {
int l = lca(u, v);
for (auto&& [a, b] : ascend(u, l)) {
int s = a + 1, t = b;
s > t ? f(t, s) : f(s, t);
}
if (vertex) f(down[l], down[l] + 1);
for (auto&& [a, b] : descend(l, v)) {
int s = a, t = b + 1;
s > t ? f(t, s) : f(s, t);
}
}
template <typename F>
void path_noncommutative_query(int u, int v, bool vertex, const F& f) {
int l = lca(u, v);
for (auto&& [a, b] : ascend(u, l)) f(a + 1, b);
if (vertex) f(down[l], down[l] + 1);
for (auto&& [a, b] : descend(l, v)) f(a, b + 1);
}
template <typename F>
void subtree_query(int u, bool vertex, const F& f) {
f(down[u] + int(!vertex), up[u]);
}
int lca(int a, int b) {
while (nxt[a] != nxt[b]) {
if (down[a] < down[b]) swap(a, b);
a = par[nxt[a]];
}
return depth[a] < depth[b] ? a : b;
}
int dist(int a, int b) { return depth[a] + depth[b] - depth[lca(a, b)] * 2; }
};
/**
* @brief Heavy Light Decomposition(重軽分解)
* @docs docs/tree/heavy-light-decomposition.md
*/
namespace StaticTopTreeVertexBasedImpl {
enum Type { Vertex, Compress, Rake, Add_Edge, Add_Vertex };
template <typename G>
struct StaticTopTreeVertexBased {
const HeavyLightDecomposition<G>& hld;
vector<vector<int>> g;
int root; // 元の木の root
int tt_root; // top tree の root
vector<int> P, L, R;
vector<Type> T;
StaticTopTreeVertexBased(const HeavyLightDecomposition<G>& _hld) : hld(_hld) {
root = hld.root;
g = rooted_tree(hld.g, root);
int n = g.size();
P.resize(n, -1), L.resize(n, -1), R.resize(n, -1);
T.resize(n, Type::Vertex);
build();
}
private:
int add(int l, int r, Type t) {
if (t == Type::Compress or t == Type::Rake) {
assert(l != -1 and r != -1);
}
if (t == Type::Add_Edge) {
assert(l != -1 and r == -1);
}
assert(t != Type::Vertex and t != Type::Add_Vertex);
int k = P.size();
P.push_back(-1), L.push_back(l), R.push_back(r), T.push_back(t);
if (l != -1) P[l] = k;
if (r != -1) P[r] = k;
return k;
}
int add2(int k, int l, int r, Type t) {
assert(k < (int)g.size());
assert(t == Type::Vertex or t == Type::Add_Vertex);
if (t == Type::Vertex) {
assert(l == -1 and r == -1);
} else {
assert(l != -1 and r == -1);
}
P[k] = -1, L[k] = l, R[k] = r, T[k] = t;
if (l != -1) P[l] = k;
if (r != -1) P[r] = k;
return k;
}
pair<int, int> merge(const vector<pair<int, int>>& a, Type t) {
assert(!a.empty());
if (a.size() == 1) return a[0];
int sum_s = 0;
for (auto& [_, s] : a) sum_s += s;
vector<pair<int, int>> b, c;
for (auto& [i, s] : a) {
(sum_s > s ? b : c).emplace_back(i, s);
sum_s -= s * 2;
}
auto [i, si] = merge(b, t);
auto [j, sj] = merge(c, t);
return {add(i, j, t), si + sj};
}
pair<int, int> compress(int i) {
vector<pair<int, int>> chs;
while (true) {
chs.push_back(add_vertex(i));
if (g[i].empty()) break;
i = g[i][0];
}
return merge(chs, Type::Compress);
}
pair<int, int> rake(int i) {
vector<pair<int, int>> chs;
for (int j = 1; j < (int)g[i].size(); j++) chs.push_back(add_edge(g[i][j]));
if (chs.empty()) return {-1, 0};
return merge(chs, Type::Rake);
}
pair<int, int> add_edge(int i) {
auto [j, sj] = compress(i);
return {add(j, -1, Type::Add_Edge), sj};
}
pair<int, int> add_vertex(int i) {
auto [j, sj] = rake(i);
return {add2(i, j, -1, j == -1 ? Type::Vertex : Type::Add_Vertex), sj + 1};
}
void build() {
auto [i, n] = compress(root);
assert((int)g.size() == n);
tt_root = i;
}
};
template <typename G, typename Path, typename Point, typename Vertex,
typename Compress, typename Rake, typename Add_edge,
typename Add_vertex>
struct DPonStaticTopTreeVertexBased {
const StaticTopTreeVertexBased<G> tt;
vector<Path> path;
vector<Point> point;
const Vertex vertex;
const Compress compress;
const Rake rake;
const Add_edge add_edge;
const Add_vertex add_vertex;
DPonStaticTopTreeVertexBased(const HeavyLightDecomposition<G>& hld,
const Vertex& _vertex, const Compress& _compress,
const Rake& _rake, const Add_edge& _add_edge,
const Add_vertex& _add_vertex)
: tt(hld),
vertex(_vertex),
compress(_compress),
rake(_rake),
add_edge(_add_edge),
add_vertex(_add_vertex) {
int n = tt.P.size();
path.resize(n), point.resize(n);
dfs(tt.tt_root);
}
Path get() { return path[tt.tt_root]; }
void update(int k) {
while (k != -1) _update(k), k = tt.P[k];
}
private:
void _update(int k) {
if (tt.T[k] == Type::Vertex) {
path[k] = vertex(k);
} else if (tt.T[k] == Type::Compress) {
path[k] = compress(path[tt.L[k]], path[tt.R[k]]);
} else if (tt.T[k] == Type::Rake) {
point[k] = rake(point[tt.L[k]], point[tt.R[k]]);
} else if (tt.T[k] == Type::Add_Edge) {
point[k] = add_edge(path[tt.L[k]]);
} else {
path[k] = add_vertex(point[tt.L[k]], k);
}
}
void dfs(int k) {
if (tt.L[k] != -1) dfs(tt.L[k]);
if (tt.R[k] != -1) dfs(tt.R[k]);
_update(k);
}
};
} // namespace StaticTopTreeVertexBasedImpl
using StaticTopTreeVertexBasedImpl::DPonStaticTopTreeVertexBased;
using StaticTopTreeVertexBasedImpl::StaticTopTreeVertexBased;
/*
// template
using Path = ;
using Point = ;
auto vertex = [&](int i) -> Path {
};
auto compress = [&](const Path& p, const Path& c) -> Path {
};
auto rake = [&](const Point& a, const Point& b) -> Point {
};
auto add_edge = [&](const Path& a) -> Point {
};
auto add_vertex = [&](const Point& a, int i) -> Path {
};
HeavyLightDecomposition hld{g};
DPonStaticTopTreeVertexBased<vector<vector<int>>, Path, Point,
decltype(vertex), decltype(compress), decltype(rake), decltype(add_edge),
decltype(add_vertex)>
dp(hld, vertex, compress, rake, add_edge, add_vertex);
*/
/**
* @brief Static Top Tree
*/
namespace DynamicDiameterImpl {
template <typename T>
struct HalfPath {
T d;
// int u;
friend HalfPath max(const HalfPath& lhs, const HalfPath& rhs) {
return lhs.d > rhs.d ? lhs : rhs;
// return lhs.u > rhs.u ? lhs : rhs;
}
};
template <typename T>
struct Path {
T d;
// int u, v;
friend Path max(const Path& lhs, const Path& rhs) {
return lhs.d > rhs.d ? lhs : rhs;
// if (lhs.u != rhs.u) return lhs.u > rhs.u ? lhs : rhs;
// return lhs.v > rhs.v ? lhs : rhs;
}
};
template <typename T>
struct L {
Path<T> dia;
HalfPath<T> d1, d2;
};
template <typename T>
struct H {
Path<T> dia;
HalfPath<T> pd, cd;
T p_to_c;
// int p, c;
};
template <typename T>
H<T> vertex(T x) {
H<T> r;
r.dia = {x};
r.pd = r.cd = {x};
r.p_to_c = x;
// r.p = r.c = i;
return r;
}
template <typename T>
H<T> compress(const H<T>& p, const H<T>& c) {
H<T> r;
r.dia = max(max(p.dia, c.dia), {p.cd.d + c.pd.d});
r.pd = max(p.pd, {p.p_to_c + c.pd.d});
r.cd = max(c.cd, {c.p_to_c + p.cd.d});
r.p_to_c = p.p_to_c + c.p_to_c;
// r.p = p.p, r.c = c.c;
return r;
}
template <typename T>
L<T> rake(const L<T>& a, const L<T>& b) {
L<T> r;
r.dia = max(a.dia, b.dia);
if (a.d1.d > b.d1.d) {
r.d1 = a.d1;
r.d2 = max(a.d2, b.d1);
} else {
r.d1 = b.d1;
r.d2 = max(b.d2, a.d1);
}
return r;
}
template <typename T>
L<T> add_edge(const H<T>& a) {
L<T> r;
r.dia = a.dia;
r.d1 = a.pd;
r.d2 = {0};
return r;
}
template <typename T>
H<T> add_vertex(const L<T>& a, T x) {
H<T> r;
r.dia = max(a.dia, {a.d1.d + x + a.d2.d});
r.pd = r.cd = {a.d1.d + x};
r.p_to_c = x;
// r.p = r.c = i;
return r;
}
template <typename T>
struct Aux_Tree {
int N, _buf;
const WeightedGraph<T>& g;
vector<vector<int>> aux;
vector<T> w;
vector<pair<pair<int, int>, int>> e_to_id;
Aux_Tree(const WeightedGraph<T>& _g) : g(_g) {
N = g.size();
aux.resize(2 * N - 1);
w.resize(2 * N - 1);
_buf = N;
dfs(0, -1);
assert(_buf == 2 * N - 1);
sort(begin(e_to_id), end(e_to_id));
}
void dfs(int c, int p) {
for (auto& d : g[c]) {
if (d == p) continue;
int id = _buf++;
aux[id].push_back(c), aux[c].push_back(id);
aux[id].push_back(d), aux[d].push_back(id);
w[id] = d.cost;
e_to_id.emplace_back(minmax<int>(c, d), id);
dfs(d, c);
}
}
int get_id(int u, int v) {
pair<int, int> p = minmax(u, v);
return lower_bound(begin(e_to_id), end(e_to_id), make_pair(p, -1))->second;
}
};
template <typename T>
struct DynamicDiameter {
const WeightedGraph<T>& g;
int n;
Aux_Tree<T> aux;
HeavyLightDecomposition<vector<vector<int>>> hld;
DPonStaticTopTreeVertexBased<
vector<vector<int>>, H<T>, L<T>, function<H<T>(int)>,
function<H<T>(const H<T>&, const H<T>&)>,
function<L<T>(const L<T>&, const L<T>&)>, function<L<T>(const H<T>&)>,
function<H<T>(const L<T>&, int)>>
dp;
DynamicDiameter(const WeightedGraph<T>& _g)
: g(_g),
n(g.size()),
aux(g),
hld(aux.aux),
dp(
hld, [&](int i) { return vertex(aux.w[i], i < n ? i : -1); },
[&](const H<T>& p, const H<T>& c) { return compress(p, c); },
[&](const L<T>& a, const L<T>& b) { return rake(a, b); },
[&](const H<T>& a) { return add_edge(a); },
[&](const L<T>& a, int i) {
return add_vertex(a, aux.w[i], i < n ? i : -1);
}) {}
pair<T, pair<int, int>> get() {
auto [d, u, v] = dp.get().dia;
return make_pair(d, make_pair(u, v));
}
void update(int u, int v, T x) {
assert(aux.e_to_id.count(minmax(u, v)));
int i = aux.e_to_id[minmax(u, v)];
aux.w[i] = x;
dp.update(i);
}
};
} // namespace DynamicDiameterImpl
using DynamicDiameterImpl::DynamicDiameter;
using namespace Nyaan;
namespace yosupo {
using ull = unsigned long long;
template <class T>
struct FlattenVector {
std::vector<T> v;
std::vector<int> start;
FlattenVector(int n, const std::vector<std::pair<int, T>>& _v)
: start(n + 1) {
for (const auto& x : _v) {
start[x.first + 1]++;
}
for (int i = 1; i <= n; i++) {
start[i] += start[i - 1];
}
v = std::vector<T>(start[n]);
auto pos = start;
for (const auto& x : _v) {
v[pos[x.first]] = x.second;
pos[x.first]++;
}
}
auto at(int i) {
return v | std::ranges::views::take(start[i + 1]) |
std::ranges::views::drop(start[i]);
}
};
template <class TreeDP>
struct StaticTopTree {
using Point = TreeDP::Point;
using Path = TreeDP::Path;
int n;
std::vector<std::pair<int, int>> edges;
TreeDP& dp;
StaticTopTree(int _n, TreeDP& _dp)
: n(_n), dp(_dp), points(n + 1, dp.id()), node_ids(n, {n, -1, -1}) {
edges.reserve(2 * (n - 1));
}
void add_edge(int u, int v) {
edges.push_back({u, v});
edges.push_back({v, u});
}
// compress / rake
template <class D>
struct Inner {
pair<D, D> d;
int par;
};
V<Inner<Path>> compressed;
V<Inner<Point>> raked;
Path op(const Path& l, const Path& r) { return dp.compress(l, r); }
Point op(const Point& l, const Point& r) { return dp.rake(l, r); }
// compress / rake / leaf
template <class D>
struct Node {
D p;
int* par;
};
template <class D>
Node<D> merge(const Node<D>& l, const Node<D>& r, V<Inner<D>>& nodes) {
int id = int(nodes.size());
assert(nodes.size() < nodes.capacity());
*l.par = 2 * id;
*r.par = 2 * id + 1;
nodes.push_back({{l.p, r.p}, -1});
return {op(l.p, r.p), &nodes[id].par};
}
void init(int r = 0) {
FlattenVector tree(n, edges);
V<int> topo;
{
V<int> parent(n, -1);
topo.reserve(n);
topo.push_back(r);
for (int i = 0; i < n; i++) {
int u = topo[i];
for (int v : tree.at(u)) {
if (v == parent[u]) continue;
parent[v] = u;
topo.push_back(v);
}
}
edges.erase(remove_if(edges.begin(), edges.end(),
[&](auto edge) {
return parent[edge.second] != edge.first;
}),
edges.end());
tree = FlattenVector(n, edges);
}
V<int> heavy_child(n, -1);
V<ull> mask(n);
V<pair<int, Node<Path>>> b;
b.reserve(n);
auto build_compress = [&](int u) {
b.clear();
auto merge_last = [&]() {
auto y = b.back();
b.pop_back();
auto x = b.back();
b.pop_back();
b.push_back(
{max(x.first, y.first) + 1, merge(x.second, y.second, compressed)});
};
while (u != -1) {
b.push_back({countr_zero(bit_ceil(mask[u])),
{dp.add_vertex(points[u], u), &node_ids[u].c_id}});
while (true) {
int len = int(b.size());
if (len >= 3 && (b[len - 3].first == b[len - 2].first ||
b[len - 3].first <= b[len - 1].first)) {
auto last = b.back();
b.pop_back();
merge_last();
b.push_back(last);
} else if (len >= 2 && b[len - 2].first <= b[len - 1].first) {
merge_last();
} else {
break;
}
}
u = heavy_child[u];
}
while (b.size() != 1) {
merge_last();
}
return b.back().second.p;
};
compressed.reserve(n - 1);
raked.reserve(n - 1);
const int MAX_H = 128;
array<Node<Point>, MAX_H> q;
bitset<MAX_H> has = {};
for (int u : topo | std::ranges::views::reverse) {
ull sum_rake = 0;
for (int v : tree.at(u)) {
sum_rake += bit_ceil(mask[v]) << 1;
}
mask[u] = bit_ceil(sum_rake);
for (int v : tree.at(u)) {
int d = countr_zero(bit_ceil(sum_rake - (bit_ceil(mask[v]) << 1)));
ull s = ((mask[v] + (1ull << d) - 1) >> d << d) + (1ull << d);
if (s <= mask[u]) {
mask[u] = s;
heavy_child[u] = v;
}
}
for (int v : tree.at(u)) {
if (v == heavy_child[u]) continue;
int d = countr_zero(bit_ceil(sum_rake - (bit_ceil(mask[v]) << 1)));
Point point = dp.to_point(build_compress(v));
Node<Point> data = {point, &node_ids[v].r_id};
while (has.test(d)) {
has.reset(d);
data = merge(q[d], data, raked);
d++;
}
q[d] = data;
has.set(d);
}
int d = int(has._Find_first());
if (d == int(has.size())) continue;
Node data = q[d];
has.reset(d);
while (true) {
d = int(has._Find_first());
if (d == int(has.size())) break;
has.reset(d);
data = merge(q[d], data, raked);
}
points[u] = data.p;
for (int v0 : tree.at(u)) {
if (v0 == heavy_child[u]) continue;
int v = v0;
while (v != -1) {
node_ids[v].h_par = u;
node_ids[v].r_id = node_ids[v0].r_id;
v = heavy_child[v];
}
}
}
build_compress(r);
}
V<Point> points;
struct ID {
int h_par, c_id, r_id;
};
V<ID> node_ids;
Point update(int u) {
auto up = [&](int id, auto p, auto& nodes) {
while (id >= 0) {
if (id % 2 == 0) {
nodes[id / 2].d.first = p;
} else {
nodes[id / 2].d.second = p;
}
p = op(nodes[id / 2].d.first, nodes[id / 2].d.second);
id = nodes[id / 2].par;
}
return p;
};
while (u != n) {
auto [h_par, c_id, r_id] = node_ids[u];
Point p = dp.to_point(up(c_id, dp.add_vertex(points[u], u), compressed));
points[h_par] = up(r_id, p, raked);
u = h_par;
}
return points[n];
}
};
struct TreeDP {
using T = ll;
struct Point {
T dia, d1, d2;
};
struct Path {
T dia, pd, cd, p_to_c;
};
using Vertex = T;
vector<Vertex> f;
Point id() { return Point{T{0}, T{0}, T{0}}; }
Path compress(const Path& p, const Path& c) {
Path r;
r.dia = max(max(p.dia, c.dia), {p.cd + c.pd});
r.pd = max(p.pd, {p.p_to_c + c.pd});
r.cd = max(c.cd, {c.p_to_c + p.cd});
r.p_to_c = p.p_to_c + c.p_to_c;
// r.p = p.p, r.c = c.c;
return r;
}
Point rake(const Point& a, const Point& b) {
Point r;
r.dia = max(a.dia, b.dia);
if (a.d1 > b.d1) {
r.d1 = a.d1;
r.d2 = max(a.d2, b.d1);
} else {
r.d1 = b.d1;
r.d2 = max(b.d2, a.d1);
}
return r;
}
Point to_point(const Path& a) {
Point r;
r.dia = a.dia;
r.d1 = a.pd;
r.d2 = {0};
return r;
}
Path add_vertex(const Point& a, int i) {
Path r;
r.dia = max(a.dia, {a.d1 + f[i] + a.d2});
r.pd = r.cd = {a.d1 + f[i]};
r.p_to_c = f[i];
return r;
}
};
} // namespace yosupo
void q() {
Timer timer;
int N, Q;
rd(N, Q);
vi U(N - 1), V(N - 1), W(N - 1);
rep(i, N - 1) rd(U[i], V[i], W[i]);
each(x, U)-- x;
each(x, V)-- x;
vi A(Q), B(Q), K(Q);
rep(i, Q) rd(A[i], B[i], K[i]);
each(x, A)-- x;
map<pair<int, int>, int> mp;
rep(i, N - 1) mp[minmax(U[i], V[i])] = i;
vvi on(N / 2 + 1);
{
vvi g(N);
rep(i, N - 1) g[U[i]].push_back(V[i]), g[V[i]].push_back(U[i]);
auto dfs = [&](auto rc, int c, int p) -> int {
int res = 1;
each(d, g[c]) {
if (d == p) continue;
int cur = rc(rc, d, c);
int idx = mp[minmax<int>(c, d)];
on[min(cur, N - cur)].push_back(idx);
res += cur;
}
return res;
};
dfs(dfs, 0, -1);
}
vvi qs(N / 2 + 1);
rep(i, Q) qs[K[i]].push_back(i);
vi flag(N - 1);
vl ans(Q, -1);
WeightedGraph<ll> g(N);
rep(i, N - 1) {
g[U[i]].emplace_back(U[i], V[i], -inf);
g[V[i]].emplace_back(V[i], U[i], -inf);
}
DynamicDiameterImpl::Aux_Tree aux(g);
yosupo::TreeDP dp;
dp.f = aux.w;
yosupo::StaticTopTree tr(2 * N - 1, dp);
rep(i, 2 * N - 1) each(j, aux.aux[i]) if (i < j) tr.add_edge(i, j);
tr.init();
ll cur = 0;
auto update = [&](int u, int v, ll x) {
int i = aux.get_id(u, v);
dp.f[i] = aux.w[i] = x;
cur = tr.update(i).dia;
};
repr1(n, N / 2) {
each(i, on[n]) {
flag[i] = 1;
update(U[i], V[i], W[i]);
}
each(q, qs[n]) {
int a = A[q], b = B[q];
if (flag[a]) update(U[a], V[a], b);
ans[q] = cur;
if (flag[a]) update(U[a], V[a], W[a]);
}
}
each(x, ans) wtn(x);
cerr << timer() << endl;
}
void Nyaan::solve() {
int t = 1;
// in(t);
while (t--) q();
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3788kb
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: 3832kb
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: 1ms
memory: 3892kb
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: 1ms
memory: 3900kb
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: 4144kb
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: 1ms
memory: 3956kb
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: 1368ms
memory: 331448kb
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: 1423ms
memory: 330756kb
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: 1414ms
memory: 331232kb
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: 1426ms
memory: 333848kb
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: 1436ms
memory: 336408kb
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: 1216ms
memory: 335612kb
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: 1088ms
memory: 335568kb
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: 1051ms
memory: 335512kb
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: 1120ms
memory: 334540kb
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