結果
問題 |
No.2114 01 Matching
|
ユーザー |
|
提出日時 | 2025-05-24 15:41:59 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 27,584 bytes |
コンパイル時間 | 5,299 ms |
コンパイル使用メモリ | 274,444 KB |
実行使用メモリ | 343,684 KB |
最終ジャッジ日時 | 2025-05-24 15:42:51 |
合計ジャッジ時間 | 49,810 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 37 WA * 14 |
ソースコード
#ifndef HIDDEN_IN_VS // 折りたたみ用 // 警告の抑制 #define _CRT_SECURE_NO_WARNINGS // ライブラリの読み込み #include <bits/stdc++.h> using namespace std; // 型名の短縮 using ll = long long; using ull = unsigned long long; // -2^63 ~ 2^63 = 9e18(int は -2^31 ~ 2^31 = 2e9) using pii = pair<int, int>; using pll = pair<ll, ll>; using pil = pair<int, ll>; using pli = pair<ll, int>; using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>; using vvvvi = vector<vvvi>; using vl = vector<ll>; using vvl = vector<vl>; using vvvl = vector<vvl>; using vvvvl = vector<vvvl>; using vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>; using vc = vector<char>; using vvc = vector<vc>; using vvvc = vector<vvc>; using vd = vector<double>; using vvd = vector<vd>; using vvvd = vector<vvd>; template <class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>; using Graph = vvi; // 定数の定義 const double PI = acos(-1); int DX[4] = { 1, 0, -1, 0 }; // 4 近傍(下,右,上,左) int DY[4] = { 0, 1, 0, -1 }; int INF = 1001001001; ll INFL = 4004004003094073385LL; // (int)INFL = INF, (int)(-INFL) = -INF; // 入出力高速化 struct fast_io { fast_io() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(18); } } fastIOtmp; // 汎用マクロの定義 #define all(a) (a).begin(), (a).end() #define sz(x) ((int)(x).size()) #define lbpos(a, x) (int)distance((a).begin(), std::lower_bound(all(a), (x))) #define ubpos(a, x) (int)distance((a).begin(), std::upper_bound(all(a), (x))) #define Yes(b) {cout << ((b) ? "Yes\n" : "No\n");} #define rep(i, n) for(int i = 0, i##_len = int(n); i < i##_len; ++i) // 0 から n-1 まで昇順 #define repi(i, s, t) for(int i = int(s), i##_end = int(t); i <= i##_end; ++i) // s から t まで昇順 #define repir(i, s, t) for(int i = int(s), i##_end = int(t); i >= i##_end; --i) // s から t まで降順 #define repe(v, a) for(const auto& v : (a)) // a の全要素(変更不可能) #define repea(v, a) for(auto& v : (a)) // a の全要素(変更可能) #define repb(set, d) for(int set = 0, set##_ub = 1 << int(d); set < set##_ub; ++set) // d ビット全探索(昇順) #define repis(i, set) for(int i = lsb(set), bset##i = set; i < 32; bset##i -= 1 << i, i = lsb(bset##i)) // set の全要素(昇順) #define repp(a) sort(all(a)); for(bool a##_perm = true; a##_perm; a##_perm = next_permutation(all(a))) // a の順列全て(昇順) #define uniq(a) {sort(all(a)); (a).erase(unique(all(a)), (a).end());} // 重複除去 #define EXIT(a) {cout << (a) << endl; exit(0);} // 強制終了 #define inQ(x, y, u, l, d, r) ((u) <= (x) && (l) <= (y) && (x) < (d) && (y) < (r)) // 半開矩形内判定 // 汎用関数の定義 template <class T> inline ll powi(T n, int k) { ll v = 1; rep(i, k) v *= n; return v; } template <class T> inline bool chmax(T& M, const T& x) { if (M < x) { M = x; return true; } return false; } // 最大値を更新(更新されたら true を返す) template <class T> inline bool chmin(T& m, const T& x) { if (m > x) { m = x; return true; } return false; } // 最小値を更新(更新されたら true を返す) template <class T> inline T getb(T set, int i) { return (set >> i) & T(1); } template <class T> inline T smod(T n, T m) { n %= m; if (n < 0) n += m; return n; } // 非負mod // 演算子オーバーロード template <class T, class U> inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; } template <class T> inline istream& operator>>(istream& is, vector<T>& v) { repea(x, v) is >> x; return is; } template <class T> inline vector<T>& operator--(vector<T>& v) { repea(x, v) --x; return v; } template <class T> inline vector<T>& operator++(vector<T>& v) { repea(x, v) ++x; return v; } #endif // 折りたたみ用 #if __has_include(<atcoder/all>) #include <atcoder/all> using namespace atcoder; #ifdef _MSC_VER #include "localACL.hpp" #endif using mint = modint998244353; //using mint = static_modint<(int)1e9 + 7>; //using mint = modint; // mint::set_mod(m); namespace atcoder { inline istream& operator>>(istream& is, mint& x) { ll x_; is >> x_; x = x_; return is; } inline ostream& operator<<(ostream& os, const mint& x) { os << x.val(); return os; } } using vm = vector<mint>; using vvm = vector<vm>; using vvvm = vector<vvm>; using vvvvm = vector<vvvm>; using pim = pair<int, mint>; #endif #ifdef _MSC_VER // 手元環境(Visual Studio) #include "local.hpp" #else // 提出用(gcc) inline int popcount(int n) { return __builtin_popcount(n); } inline int popcount(ll n) { return __builtin_popcountll(n); } inline int lsb(int n) { return n != 0 ? __builtin_ctz(n) : 32; } inline int lsb(ll n) { return n != 0 ? __builtin_ctzll(n) : 64; } inline int msb(int n) { return n != 0 ? (31 - __builtin_clz(n)) : -1; } inline int msb(ll n) { return n != 0 ? (63 - __builtin_clzll(n)) : -1; } #define dump(...) #define dumpel(v) #define dump_math(v) #define input_from_file(f) #define output_to_file(f) #define Assert(b) { if (!(b)) { vc MLE(1<<30); EXIT(MLE.back()); } } // RE の代わりに MLE を出す #endif //【Implicit Treap(可換モノイド)】 /* * Implicit_treap<S, op, e>() : O(1) * 空で初期化する. * 要素は可換モノイド (S, op, e) の元とする. * * Implicit_treap<S, op, e>(vS a) : O(n log n) * 配列 a[0..n) で初期化する. * * bool empty() : O(1) * 空かを返す. * * int size() : O(1) * 要素数を返す. * * set(int i, S x) : O(log n) * a[i] = x とする(なければ何もしない) * * S get(int i) : O(log n) * a[i] を返す(なければ e() を返す) * * S sum() : O(1) * Σa[0..n) を返す(空なら e() を返す) * * S sum(int l, int r) : O(log n) * Σa[l..r) を返す(空なら e() を返す) * * int max_right(int l, function<bool(S)> g) : O(log n) * g( Σa[l..r) ) = true となる最大の r を返す. * 制約:g( e() ) = true かつ g は単調 * * int min_left(int r, function<bool(S)> g) : O(log n) * g( Σa[l..r) ) = true となる最小の l を返す. * 制約:g( e() ) = true かつ g は単調 * * insert(int i, S x) : O(log n) * a[i] = x を挿入する(元々あった要素は右に移動する) * * erase(int i) : O(log n) * a[i] の要素を削除し左詰めする(なければ何もしない) * * reverse(int l, int r) : O(log n) * a[l..r) を左右反転する. * * rotate(int l, int m, int r) : O(log n) * a[l..r) を,a[m] が先頭にくるよう巡回シフトする. * 制約:0 ≦ l ≦ m < r ≦ n * * Implicit_treap split(int key) : O(log n) * 自身から位置 key 以上の要素を切り出し,切り出してできた木を返す. * * Implicit_treap split_max_right(function<bool(S)> g) : O(log n) * g( Σa[0..r) ) = true となる最大の r までを残し,a[r..n) を切り出してできた木を返す. * * Implicit_treap split_min_left(function<bool(S)> g) : O(log n) * g( Σa[l..n) ) = true となる最大の l までを残し,a[0..l) を切り出してできた木を返す. * * void merge_right(Implicit_treap IT) : O(log n) * 自身の右側に IT をマージする. * * void merge_left(Implicit_treap IT) : O(log n) * 自身の左側に IT をマージする. * * vS get_all() : O(n) * 全要素のリストを返す. */ template <class S, S(*op)(S, S), S(*e)()> class Implicit_treap { // 参考 : https://xuzijian629.hatenablog.com/entry/2018/12/08/000452 //【備考】 // 非可換モノイドでも reverse(), rotate() 以外なら使える. inline static bool first_call = true; inline static mt19937 rnd; struct Node { S value; // 頂点の値 S acc; // 部分木のノードの総和 unsigned int priority; // ランダムに決めた優先度 int cnt; // 部分木のノード数 bool rev; // 部分木が反転されているか Node* l, * r; // 左右の子へのポインタ //// 参考 : https://qiita.com/tubo28/items/f058582e457f6870a800 //static inline int node_count = 0; //// 静的に確保した配列から返す //void* operator new(std::size_t) { // constexpr int MAX_N = (int)6e5 + 10; // 適切な大きさに設定 // static Node pool[MAX_N]; // return pool + node_count++; //} Node() : value(e()), acc(e()), priority(0), cnt(0), rev(false), l(nullptr), r(nullptr) {} Node(S value, unsigned int priority) : value(value), acc(e()), priority(priority), cnt(1), rev(false), l(nullptr), r(nullptr) { } }; Node* root; // 部分木 t のノード数を返す. int cnt(Node* t) { return t ? t->cnt : 0; } // Σ(部分木 t) を返す. S acc(Node* t) { return t ? t->acc : e(); } // 部分木 t の cnt と acc を更新する(子は更新済であること) void pushup(Node* t) { if (t) { t->cnt = cnt(t->l) + 1 + cnt(t->r); t->acc = op(acc(t->l), op(t->value, acc(t->r))); } } // 遅延評価を適用する. void pushdown(Node* t) { // 部分木 t の反転フラグが true なら,実際に反転させた上で反転フラグを false にする. if (t && t->rev) { t->rev = false; swap(t->l, t->r); // t の子については反転フラグを flip しておくだけにする. if (t->l) t->l->rev ^= 1; if (t->r) t->r->rev ^= 1; } // 部分木 t の cnt と acc を更新する. pushup(t); } // 部分木 t を位置 key 未満[以上] に分割し,それぞれの根へのポインタを l[ r ] に格納する. void split(Node* t, int key, Node*& l, Node*& r) { // 空なら分割しなくていい. if (!t) { l = r = nullptr; return; } // t の情報を更新する. pushdown(t); // 部分木 t 内の自身の位置を得る. int implicit_key = cnt(t->l); if (key <= implicit_key) { // 左の木を分割しその左側を l に採用する.小さくなった右側は t->l に繋ぎ直す. split(t->l, key, l, t->l); r = t; } else { // 右の木を分割しその右側を r に採用する.小さくなった左側は t->r に繋ぎ直す. split(t->r, key - implicit_key - 1, t->r, r); l = t; } // 繋ぎ変えで部分木 t の cnt と acc が壊れたので更新する. pushup(t); } void split_max_right(Node* t, S& x, const function<bool(S)>& g, Node*& l, Node*& r) { // 空なら分割しなくていい. if (!t) { l = r = nullptr; return; } // t の情報を更新する. pushdown(t); auto tmp = op(x, op(acc(t->l), t->value)); if (!g(tmp)) { // 左の木を分割しその左側を l に採用する.小さくなった右側は t->l に繋ぎ直す. split_max_right(t->l, x, g, l, t->l); r = t; } else { // 右の木を分割しその右側を r に採用する.小さくなった左側は t->r に繋ぎ直す. x = tmp; split_max_right(t->r, x, g, t->r, r); l = t; } // 繋ぎ変えで部分木 t の acc が壊れたので更新する. pushup(t); } void split_min_left(Node* t, S& x, const function<bool(S)>& g, Node*& l, Node*& r) { // 空なら分割しなくていい. if (!t) { l = r = nullptr; return; } // t の情報を更新する. pushdown(t); auto tmp = op(t->value, op(acc(t->r), x)); if (g(tmp)) { // 左の木を分割しその左側を l に採用する.小さくなった右側は t->l に繋ぎ直す. x = tmp; split_min_left(t->l, x, g, l, t->l); r = t; } else { // 右の木を分割しその右側を r に採用する.小さくなった左側は t->r に繋ぎ直す. split_min_left(t->r, x, g, t->r, r); l = t; } // 繋ぎ変えで部分木 t の acc が壊れたので更新する. pushup(t); } // 部分木 l, r をこの順にマージした部分木を t に格納する. void merge(Node*& t, Node* l, Node* r) { // l, r の情報を更新する. pushdown(l); pushdown(r); // 片方が空ならもう一方を根とすればよい. if (!l) t = r; else if (!r) t = l; // 優先度が高い方を根とし,もう一方をその子とマージする. else if (l->priority > r->priority) { merge(l->r, l->r, r); t = l; } else { merge(r->l, l, r->l); t = r; } // 部分木 t の cnt と acc を更新する. pushup(t); } int max_right(Node* t, S x, int offset, const function<bool(S)>& g) { if (!t) return offset; // t の情報を更新する. pushdown(t); // 左の子の中に答えがあるなら左の子へ if (t->l) { S nx = op(x, t->l->acc); if (!g(nx)) return max_right(t->l, x, offset, g); x = nx; } // 自身が答えならそれを返す. S nx = op(x, t->value); if (!g(nx)) return offset + cnt(t->l); x = nx; // 右の子の中に答えがあるなら右の子へ if (t->r) { S nx = op(x, t->r->acc); if (!g(nx)) return max_right(t->r, x, offset + cnt(t->l) + 1, g); x = nx; } // どこにもないなら右端を返す. return offset + cnt(t->l) + 1 + cnt(t->r); } int min_left(Node* t, S x, int offset, const function<bool(S)>& g) { if (!t) return offset; // t の情報を更新する. pushdown(t); // 右の子の中に答えがあるなら右の子へ if (t->r) { S nx = op(t->r->acc, x); if (!g(nx)) return min_left(t->r, x, offset + cnt(t->l) + 1, g); x = nx; } // 自身が答えならそれを返す. S nx = op(t->value, x); if (!g(nx)) return offset + cnt(t->l) + 1; x = nx; // 左の子の中に答えがあるなら左の子へ if (t->l) { S nx = op(t->l->acc, x); if (!g(nx)) return min_left(t->l, x, offset, g); x = nx; } // どこにもないなら左端を返す. return offset; } void get_all(Node* t, vector<S>& seq) { if (!t) return; pushdown(t); get_all(t->l, seq); seq.emplace_back(t->value); get_all(t->r, seq); } public: // 空で初期化する. Implicit_treap() : root(nullptr) { if (Implicit_treap::first_call) { rnd = mt19937((int)time(NULL)); Implicit_treap::first_call = false; } } // 配列 a[0..n) で初期化する. Implicit_treap(const vector<S>& a) : root(nullptr) { // verify : https://atcoder.jp/contests/code-festival-2015-morning-hard/tasks/cf_2015_morning_hard_c if (Implicit_treap::first_call) { rnd = mt19937((int)time(NULL)); Implicit_treap::first_call = false; } rep(i, sz(a)) insert(i, a[i]); } // 要素が空かを返す. bool empty() { return !(bool)root; } // 要素数を返す. int size() { return cnt(root); } // Σa[0..n) を返す(空なら e() を返す) S sum() { // verify : https://atcoder.jp/contests/abc406/tasks/abc406_g return acc(root); } // Σa[l..r) を返す(空なら e() を返す) S sum(int l, int r) { // verify : https://atcoder.jp/contests/code-festival-2015-morning-hard/tasks/cf_2015_morning_hard_c if (l >= r) return e(); Node* lt, * mt, * rt; // [l..r) に対応する部分木 mt を切り出してくる. split(root, l, lt, rt); split(rt, r - l, mt, rt); // 値は既に acc に格納されている. S res = acc(mt); // 木を元に戻しておく. merge(rt, mt, rt); merge(root, lt, rt); return res; } // a[i] を返す(なければ e() を返す) S get(int i) { // verify : https://atcoder.jp/contests/code-festival-2015-morning-hard/tasks/cf_2015_morning_hard_c return sum(i, i + 1); } // a[i] = x とする. void set(int i, S x) { Node* lt, * mt, * rt; // [i] に対応する部分木 mt を切り出してくる. split(root, i, lt, rt); split(rt, 1, mt, rt); // 値を x に更新する. if (mt) { mt->value = x; mt->acc = x; } // 木を元に戻しておく. merge(rt, mt, rt); merge(root, lt, rt); } // g( Σa[l..r) ) = true となる最大の r を返す. int max_right(int l, const function<bool(S)>& g) { // verify : https://atcoder.jp/contests/code-festival-2015-morning-hard/tasks/cf_2015_morning_hard_c Node* lt, * rt; // [l..n) に対応する部分木 rt を切り出してくる. split(root, l, lt, rt); // 部分木 rt が空なら l を返す if (!rt) return l; S x = e(); int res = max_right(rt, x, l, g); // 木を元に戻しておく. merge(root, lt, rt); return res; } // g( Σa[l..r) ) = true となる最小の l を返す. int min_left(int r, const function<bool(S)>& g) { Node* lt, * rt; // [0..r) に対応する部分木 lt を切り出してくる. split(root, r, lt, rt); S x = e(); int res = min_left(lt, x, 0, g); // 木を元に戻しておく. merge(root, lt, rt); return res; } // a[i] = x を挿入する(元々あった要素は右に移動する) void insert(int i, S x) { // verify : https://atcoder.jp/contests/code-festival-2015-morning-hard/tasks/cf_2015_morning_hard_c Node* lt, * rt; // 一旦 i で分割し,x を挟んでからマージする. split(root, i, lt, rt); merge(lt, lt, new Node(x, rnd())); merge(root, lt, rt); } // a[i] の要素を削除し左詰めする(なければ何もしない) void erase(int i) { Node* lt, * mt, * rt; // i の前後で分割し,i だけ除いてマージする. split(root, i + 1, lt, rt); split(lt, i, lt, mt); merge(root, lt, rt); } // a[l..r) を左右反転する. void reverse(int l, int r) { if (l >= r) return; Node* lt, * mt, * rt; // [l..r) に対応する部分木 mt を切り出してくる. split(root, l, lt, rt); split(rt, r - l, mt, rt); // 反転フラグを flip する. if (mt) mt->rev ^= 1; // 木を元に戻しておく. merge(rt, mt, rt); merge(root, lt, rt); } // a[l..r) を,a[m] が先頭にくるよう巡回シフトする. void rotate(int l, int m, int r) { // verify : https://atcoder.jp/contests/code-festival-2015-morning-hard/tasks/cf_2015_morning_hard_c // 全体を反転 reverse(l, r); // 左右それぞれを反転 reverse(l, l + r - m); reverse(l + r - m, r); } // 自身から位置 key 以上の要素を切り出し,切り出して出来た木を返す. Implicit_treap split(int key) { // verify : https://atcoder.jp/contests/abc406/tasks/abc406_g Node* l, * r; split(root, key, l, r); root = l; Implicit_treap ret; ret.root = r; return ret; } // g( Σa[0..r) ) = true となる最大の r までを残し,a[r..n) を切り出してできた木を返す. Implicit_treap split_max_right(const function<bool(S)>& g) { // verify : https://atcoder.jp/contests/abc406/tasks/abc406_g S acc = e(); Node* l, * r; split_max_right(root, acc, g, l, r); root = l; Implicit_treap ret; ret.root = r; return ret; } // g( Σa[l..n) ) = true となる最大の l までを残し,a[0..l) を切り出してできた木を返す. Implicit_treap split_min_left(const function<bool(S)>& g) { // verify : https://atcoder.jp/contests/kupc2016/tasks/kupc2016_h S acc = e(); Node* l, * r; split_min_left(root, acc, g, l, r); root = r; Implicit_treap ret; ret.root = l; return ret; } // 自身の右側に IT をマージする. void merge_right(Implicit_treap& IT) { // verify : https://atcoder.jp/contests/abc406/tasks/abc406_g merge(root, root, IT.root); } // 自身の右側に IT をマージする. void merge_left(Implicit_treap& IT) { // verify : https://atcoder.jp/contests/kupc2016/tasks/kupc2016_h merge(root, IT.root, root); } // 全要素のリストを返す. vector<S> get_all() { vector<S> seq; get_all(root, seq); return seq; } #ifdef _MSC_VER friend ostream& operator<<(ostream& os, Implicit_treap& IT) { os << IT.get_all(); return os; } #endif }; //【Slope Trick(平衡二分探索木)】 /* * Slope_trick_BBST() : O(1) * [-INF..INF] 上の n=1 個の線分からなる関数 f(x) = 0 で初期化する. * * Slope_trick_BBST(ll x_min, ll x_max) : O(1) * [x_min..x_max] 上の n=1 個の線分からなる関数 f(x) = 0 で初期化する. * * pll min() : O(log n) * f(x) が x=x0 で最小値をとるとき,(x0, f(x0)) を返す(x0 は最左) * * sub_add_p(ll x0, ll p_sub, ll p_add) : O(log n) * x-p グラフにおいて,x<x0 の範囲から p_sub を引き,x>x0 の範囲に p_add を加える. * * sub_add_x(ll p0, ll x_sub, ll x_add) : O(log n) * x-p グラフにおいて,p<p0 の範囲から x_sub を引き,p>p0 の範囲に x_add を加える. * * sub_add_p_undo(ll x0, ll p_sub, ll p_add) : O(log n) * 直前に行った sub_add_p(x0, p_sub, p_add) を取り消す. * * sub_add_x_undo(ll p0, ll x_sub, ll x_add) : O(log n) * 直前に行った sub_add_x(p0, x_sub, x_add) を取り消す. * * add_const(ll y0) : O(1) * f(x) += y0 とする. * * add_right(ll x0, ll c=1) : O(log n) * f(x) += c max(x - x0, 0) とする.(_/ の形を加算する.) * * add_left(ll x0, ll c=1) : O(log n) * f(x) += c max(x0 - x, 0) とする.(\_ の形を加算する.) * * add_abs(ll x0, ll c=1) : O(log n) * f(x) += c |x - x0| とする.(\/ の形を加算する.) * * acc_min_left() : O(log n) * f(x) を左から累積最小値をとったものに置き換える.(\_ の形にする.) * * acc_min_right() : O(log n) * f(x) を右から累積最小値をとったものに置き換える.(_/ の形にする.) * * shift(ll x0) : O(1) * f(x) を右に x0 だけ平行移動する.(f(x) ← f(x - x0)) * * sliding_window_min(ll dl, ll dr) : O(log n) * f(x) を min f([x-dl..x+dr]) に置き換える.(\__/ の形にする.) * * 利用:【Implicit Treap(可換モノイド)】 */ using S_st = array<ll, 3>; // (x, p, y) (p=dy/dx) S_st op_st(S_st a, S_st b) { auto [ax, ap, ay] = a; auto [bx, bp, by] = b; return { ax + bx, ap + bp, ap * bx + ay + by }; } S_st e_st() { return { 0, 0, 0 }; } S_st inv_st(S_st a) { auto [x, p, y] = a; return { -x, -p, p * x - y }; } struct Slope_trick_BBST { S_st xpyL, xpyR; using IT = Implicit_treap<S_st, op_st, e_st>; IT s; public: Slope_trick_BBST() : xpyL({ -INF, 0, 0 }), xpyR({ INF, 0, 0 }) { // verify : https://atcoder.jp/contests/abc127/tasks/abc127_f s.insert(0, S_st({ 2 * INF, 0, 0 })); } Slope_trick_BBST(ll x_min, ll x_max) : xpyL({ x_min, 0, 0 }), xpyR({ x_max, 0, 0 }) { // verify : https://atcoder.jp/contests/kupc2016/tasks/kupc2016_h s.insert(0, S_st({ x_max - x_min, 0, 0 })); } pll min() { // verify : https://atcoder.jp/contests/abc127/tasks/abc127_f auto sR = s.split_max_right([&](S_st mat) { return op_st(xpyL, mat)[1] < 0; }); auto tmp = op_st(xpyL, s.sum()); s.merge_right(sR); return { tmp[0], tmp[2] }; } void sub_add_p(ll x, ll p_sub, ll p_add) { auto sM = s.split_max_right([&](S_st mat) { return op_st(xpyL, mat)[0] <= x; }); auto sR = sM.split(1); auto tmp = op_st(xpyL, s.sum()); ll dxL = x - tmp[0]; tmp = op_st(tmp, sM.sum()); ll dxR = tmp[0] - x; IT nsM; if (dxL) nsM.insert(0, S_st({ dxL, 0, 0 })); nsM.insert(1, S_st({ 0, p_add + p_sub, 0 })); if (dxR) nsM.insert(2, S_st({ dxR, 0, 0 })); s.merge_right(nsM); s.merge_right(sR); xpyL[1] -= p_sub; xpyL[2] += (x - xpyL[0]) * p_sub; xpyR[1] += p_add; xpyR[2] += (xpyR[0] - x) * p_add; } void sub_add_p_undo(ll x, ll p_sub, ll p_add) { auto sL = s.split_min_left([&](S_st mat) { return op_st(xpyR, inv_st(mat))[0] >= x; }); auto sR = s.split_max_right([&](S_st mat) { return mat[0] == 0; }); auto tmp = s.sum(); s = sL; sR.insert(0, S_st({ 0, tmp[1] - (p_add + p_sub), 0 })); s.merge_right(sR); xpyL[1] += p_sub; xpyL[2] -= (x - xpyL[0]) * p_sub; xpyR[1] -= p_add; xpyR[2] -= (xpyR[0] - x) * p_add; } void sub_add_x(ll p, ll x_sub, ll x_add) { auto sM = s.split_max_right([&](S_st mat) { return op_st(xpyL, mat)[1] <= p; }); auto sR = sM.split(1); auto tmp = op_st(xpyL, s.sum()); ll dpD = p - tmp[1]; tmp = op_st(tmp, sM.sum()); ll dpU = tmp[1] - p; IT nsM; if (dpD) nsM.insert(0, S_st({ 0, dpD, 0 })); nsM.insert(1, S_st({ x_add + x_sub, 0, 0 })); if (dpU) nsM.insert(2, S_st({ 0, dpU, 0 })); s.merge_right(nsM); s.merge_right(sR); xpyL[0] -= x_sub; xpyL[2] -= x_sub * p; xpyR[0] += x_add; xpyR[2] -= x_add * p; } void sub_add_x_undo(ll p, ll x_sub, ll x_add) { auto sL = s.split_min_left([&](S_st mat) { return op_st(xpyR, inv_st(mat))[1] >= p; }); auto sR = s.split_max_right([&](S_st mat) { return mat[1] == 0; }); auto tmp = s.sum(); s = sL; sR.insert(0, S_st({ tmp[0] - (x_add + x_sub), 0, 0 })); s.merge_right(sR); xpyL[0] += x_sub; xpyL[2] += x_sub * p; xpyR[0] -= x_add; xpyR[2] += x_add * p; } void add_const(ll y_add) { // verify : https://atcoder.jp/contests/abc127/tasks/abc127_f xpyL[2] += y_add; xpyR[2] += y_add; } void add_right(ll x0, ll c = 1) { sub_add_p(x0, 0, c); } void add_left(ll x0, ll c = 1) { sub_add_p(x0, c, 0); } void add_abs(ll x0, ll c = 1) { // verify : https://atcoder.jp/contests/abc127/tasks/abc127_f sub_add_p(x0, c, c); } void acc_min_left() { // verify : https://atcoder.jp/contests/kupc2016/tasks/kupc2016_h // sub_add_p(0, 0, ∞) だが,不要な部分は削除したいので別で書く. auto sR = s.split_max_right([&](S_st mat) { return op_st(xpyL, mat)[1] <= 0; }); auto tmp = op_st(xpyL, s.sum()); ll dpD = 0 - tmp[1]; auto tmp2 = sR.sum(); IT nsR; if (dpD) nsR.insert(0, S_st({ 0, dpD, 0 })); if (tmp2[0]) nsR.insert(1, S_st({ tmp2[0], 0, 0 })); s.merge_right(nsR); xpyR[1] = 0; xpyR[2] = tmp[2]; } void acc_min_right() { // verify : https://atcoder.jp/contests/kupc2016/tasks/kupc2016_h // sub_add_p(0, ∞, 0) だが,不要な部分は削除したいので別で書く. auto sL = s.split_min_left([&](S_st mat) { return op_st(xpyR, inv_st(mat))[1] >= 0; }); auto tmp = op_st(xpyR, inv_st(s.sum())); ll dpU = tmp[1] - 0; auto tmp2 = sL.sum(); IT nsL; if (tmp2[0]) nsL.insert(0, S_st({ tmp2[0], 0, 0 })); if (dpU) nsL.insert(1, S_st({ 0, dpU, 0 })); s.merge_left(nsL); xpyL[1] = 0; xpyL[2] = tmp[2]; } void shift(ll x_add) { // verify : https://atcoder.jp/contests/arc123/tasks/arc123_d // sub_add_p(-∞, 0, x_add) だが,O(1) で済むので別で書く. xpyL[0] += x_add; xpyR[0] += x_add; } void sliding_window_min(ll dl, ll dr) { // verify : https://atcoder.jp/contests/arc070/tasks/arc070_c sub_add_x(0, dr, dl); } #ifdef _MSC_VER friend ostream& operator<<(ostream& os, Slope_trick_BBST& IT) { os << IT.xpyL << "/ " << IT.s; return os; } #endif }; int main() { // input_from_file("input.txt"); // output_to_file("output.txt"); int n, m; ll k; cin >> n >> m >> k; vl a(n), b(m); cin >> a >> b; if (n > m) { swap(n, m); swap(a, b); } unordered_map<ll, vector<pli>> r_to_qc; rep(i, n) r_to_qc[a[i] % k].push_back({ a[i] / k, 1 }); rep(j, m) r_to_qc[b[j] % k].push_back({ b[j] / k, -1 }); ll res = 0; repe(tmp, r_to_qc) { auto qc = tmp.second; sort(all(qc)); int K = sz(qc); Slope_trick_BBST S(-(ll)3e5, (ll)3e5); S.add_abs(0, INF); rep(k, K) { auto [q, c] = qc[k]; if (c == 1) { S.shift(1); } else { S.sliding_window_min(0, 1); } if (k < K - 1) { auto [q2, c2] = qc[k + 1]; S.add_abs(0, q2 - q); } dump(S); } S.add_abs(0, INF); res += S.min().second; } if (res >= INF) res = -1; EXIT(res); }