結果
問題 | No.918 LISGRID |
ユーザー | バイト |
提出日時 | 2019-11-01 14:40:34 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 83 ms / 2,000 ms |
コード長 | 59,031 bytes |
コンパイル時間 | 2,863 ms |
コンパイル使用メモリ | 224,472 KB |
実行使用メモリ | 20,732 KB |
最終ジャッジ日時 | 2024-09-14 22:41:05 |
合計ジャッジ時間 | 7,098 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge6 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 40 ms
13,440 KB |
testcase_02 | AC | 13 ms
6,784 KB |
testcase_03 | AC | 18 ms
8,192 KB |
testcase_04 | AC | 14 ms
7,168 KB |
testcase_05 | AC | 54 ms
15,308 KB |
testcase_06 | AC | 50 ms
14,616 KB |
testcase_07 | AC | 76 ms
19,764 KB |
testcase_08 | AC | 40 ms
13,052 KB |
testcase_09 | AC | 55 ms
15,788 KB |
testcase_10 | AC | 58 ms
15,688 KB |
testcase_11 | AC | 55 ms
15,820 KB |
testcase_12 | AC | 62 ms
16,856 KB |
testcase_13 | AC | 48 ms
14,412 KB |
testcase_14 | AC | 71 ms
19,004 KB |
testcase_15 | AC | 83 ms
20,732 KB |
testcase_16 | AC | 24 ms
9,600 KB |
testcase_17 | AC | 25 ms
10,132 KB |
testcase_18 | AC | 27 ms
9,728 KB |
testcase_19 | AC | 8 ms
5,504 KB |
testcase_20 | AC | 38 ms
12,540 KB |
testcase_21 | AC | 10 ms
6,016 KB |
testcase_22 | AC | 7 ms
5,376 KB |
testcase_23 | AC | 22 ms
8,704 KB |
testcase_24 | AC | 3 ms
5,376 KB |
testcase_25 | AC | 2 ms
5,376 KB |
testcase_26 | AC | 2 ms
5,376 KB |
testcase_27 | AC | 2 ms
5,376 KB |
testcase_28 | AC | 2 ms
5,376 KB |
testcase_29 | AC | 2 ms
5,376 KB |
testcase_30 | AC | 2 ms
5,376 KB |
testcase_31 | AC | 2 ms
5,376 KB |
testcase_32 | AC | 2 ms
5,376 KB |
testcase_33 | AC | 2 ms
5,376 KB |
testcase_34 | AC | 4 ms
5,376 KB |
testcase_35 | AC | 5 ms
5,376 KB |
testcase_36 | AC | 3 ms
5,376 KB |
testcase_37 | AC | 35 ms
12,120 KB |
testcase_38 | AC | 22 ms
9,088 KB |
ソースコード
//#pragma GCC optimize ("-O3") #include <bits/stdc++.h> using namespace std; //@起動時 struct initon { initon() { cin.tie(0); ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(16); srand((unsigned) clock() + (unsigned) time(NULL)); }; } __initon; //衝突対策 #define ws ___ws struct T { int f, s, t; T() { f = -1, s = -1, t = -1; } T(int f, int s, int t) : f(f), s(s), t(t) {} bool operator<(const T &r) const { return f != r.f ? f < r.f : s != r.s ? s < r.s : t < r.t; //return f != r.f ? f > r.f : s != r.s ? s > r.s : t > r.t; 大きい順 } bool operator>(const T &r) const { return f != r.f ? f > r.f : s != r.s ? s > r.s : t > r.t; //return f != r.f ? f > r.f : s != r.s ? s > r.s : t > r.t; 小さい順 } bool operator==(const T &r) const { return f == r.f && s == r.s && t == r.t; } bool operator!=(const T &r) const { return f != r.f || s != r.s || t != r.t; } int operator[](int i) { assert(i < 3); return i == 0 ? f : i == 1 ? s : t; } }; #define int long long #define ll long long #define double long double #define pow powl #define ull unsigned long long using dou = double; using itn = int; using str = string; using bo= bool; #define au auto using P = pair<ll, ll>; #define fi first #define se second #define vec vector #define beg begin #define rbeg rbegin #define con continue #define bre break #define brk break #define is == //マクロ省略系 コンテナ //マクロ省略系 コンテナ using vi = vector<ll>; using vb = vector<bool>; using vs = vector<string>; using vd = vector<double>; using vc = vector<char>; using vp = vector<P>; using vt = vector<T>; #define vec vector #define o_vvt(o1, o2, o3, o4, name, ...) name #define vvt0(t) vec<vec<t>> #define vvt1(t, a) vec<vec<t>>a #define vvt2(t, a, b) vec<vec<t>>a(b) #define vvt3(t, a, b, c) vec<vec<t>> a(b,vec<t>(c)) #define vvt4(t, a, b, c, d) vec<vec<t>> a(b,vec<t>(c,d)) #define vvi(...) o_vvt(__VA_ARGS__,vvt4,vvt3,vvt2 ,vvt1,vvt0)(ll,__VA_ARGS__) #define vvb(...) o_vvt(__VA_ARGS__,vvt4,vvt3,vvt2 ,vvt1,vvt0)(bool,__VA_ARGS__) #define vvs(...) o_vvt(__VA_ARGS__,vvt4,vvt3,vvt2 ,vvt1,vvt0)(string,__VA_ARGS__) #define vvd(...) o_vvt(__VA_ARGS__,vvt4,vvt3,vvt2 ,vvt1,vvt0)(double,__VA_ARGS__) #define vvc(...) o_vvt(__VA_ARGS__,vvt4,vvt3,vvt2 ,vvt1,vvt0)(char,__VA_ARGS__) #define vvp(...) o_vvt(__VA_ARGS__,vvt4,vvt3,vvt2 ,vvt1,vvt0)(P,__VA_ARGS__) #define vvt(...) o_vvt(__VA_ARGS__,vvt4,vvt3,vvt2 ,vvt1,vvt0)(T,__VA_ARGS__) template<typename T> vector<T> make_v(size_t a) { return vector<T>(a); } template<typename T, typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); } #define vni(name, ...) auto name = make_v<ll>(__VA_ARGS__) #define vnb(name, ...) auto name = make_v<bool>(__VA_ARGS__) #define vns(name, ...) auto name = make_v<string>(__VA_ARGS__) #define vnd(name, ...) auto name = make_v<double>(__VA_ARGS__) #define vnc(name, ...) auto name = make_v<char>(__VA_ARGS__) #define vnp(name, ...) auto name = make_v<P>(__VA_ARGS__) #define v3i(a, b, c, d) vector<vector<vi>> a(b, vector<vi>(c, vi(d))) #define v3d(a, b, c, d) vector<vector<vd>> a(b, vector<vd>(c, vd(d))) #define v3m(a, b, c, d) vector<vector<vm>> a(b, vector<vm>(c, vm(d))) #define _vvi vector<vi> #define _vvl vector<vl> #define _vvb vector<vb> #define _vvs vector<vs> #define _vvd vector<vd> #define _vvc vector<vc> #define _vvp vector<vp> #define PQ priority_queue<ll, vector<ll>, greater<ll> > #define tos to_string using mapi = map<int, int>; using mapd = map<dou, int>; using mapc = map<char, int>; using maps = map<str, int>; using seti = set<int>; using setd = set<dou>; using setc = set<char>; using sets = set<str>; using qui = queue<int>; #define bset bitset #define uset unordered_set #define mset multiset //#define umap unordered_map //#define umapi unordered_map<int,int> //#define umapp unordered_map<P,int> #define mmap multimap //マクロ 繰り返し #define _overloadrep(_1, _2, _3, _4, name, ...) name # define _rep(i, n) for(int i = 0,_lim=n; i < _lim ; i++) #define repi(i, m, n) for(int i = m,_lim=n; i < _lim ; i++) #define repadd(i, m, n, ad) for(int i = m,_lim=n; i < _lim ; i+= ad) #define rep(...) _overloadrep(__VA_ARGS__,repadd,repi,_rep,)(__VA_ARGS__) #define _rer(i, n) for(int i = n; i >= 0 ; i--) #define reri(i, m, n) for(int i = m,_lim=n; i >= _lim ; i--) #define rerdec(i, m, n, dec) for(int i = m,_lim=n; i >= _lim ; i-=dec) #define rer(...) _overloadrep(__VA_ARGS__,rerdec,reri,_rer,)(__VA_ARGS__) #define fora(a, b) for(auto&& a : b) //マクロ 定数 #define k3 1010 #define k4 10101 #define k5 101010 #define k6 1010101 #define k7 10101010 const int inf = (int) 1e9 + 100; const ll linf = (ll) 1e18 + 100; const double eps = 1e-9; const double PI = 3.1415926535897932384626433832795029L; ll ma = numeric_limits<ll>::min(); ll mi = numeric_limits<ll>::max(); const int y4[] = {-1, 1, 0, 0}; const int x4[] = {0, 0, -1, 1}; const int y8[] = {0, 1, 0, -1, -1, 1, 1, -1}; const int x8[] = {1, 0, -1, 0, 1, -1, 1, -1}; //マクロ省略形 関数等 #define arsz(a) (sizeof(a)/sizeof(a[0])) #define sz(a) ((int)(a).size()) #define rs resize #define mp make_pair #define pb push_back #define pf push_front #define eb emplace_back #define all(a) (a).begin(),(a).end() #define rall(a) (a).rbegin(),(a).rend() inline void sort(string &a) { sort(a.begin(), a.end()); } template<class T> inline void sort(vector<T> &a) { sort(a.begin(), a.end()); }; template<class T> inline void sort(vector<T> &a, int len) { sort(a.begin(), a.begin() + len); }; template<class T, class F> inline void sort(vector<T> &a, F f) { sort(a.begin(), a.end(), [&](T l, T r) { return f(l) < f(r); }); }; enum ___pcomparator { fisi, fisd, fdsi, fdsd, sifi, sifd, sdfi, sdfd }; inline void sort(vector<P> &a, ___pcomparator type) { switch (type) { case fisi: sort(all(a), [&](P l, P r) { return l.fi != r.fi ? l.fi < r.fi : l.se < r.se; }); break; case fisd: sort(all(a), [&](P l, P r) { return l.fi != r.fi ? l.fi < r.fi : l.se > r.se; }); break; case fdsi: sort(all(a), [&](P l, P r) { return l.fi != r.fi ? l.fi > r.fi : l.se < r.se; }); break; case fdsd: sort(all(a), [&](P l, P r) { return l.fi != r.fi ? l.fi > r.fi : l.se > r.se; }); break; case sifi: sort(all(a), [&](P l, P r) { return l.se != r.se ? l.se < r.se : l.fi < r.fi; }); break; case sifd: sort(all(a), [&](P l, P r) { return l.se != r.se ? l.se < r.se : l.fi > r.fi; }); break; case sdfi: sort(all(a), [&](P l, P r) { return l.se != r.se ? l.se > r.se : l.fi < r.fi; }); break; case sdfd: sort(all(a), [&](P l, P r) { return l.se != r.se ? l.se > r.se : l.fi > r.fi; }); break; } }; inline void sort(vector<T> &a, ___pcomparator type) { switch (type) { case fisi: sort(all(a), [&](T l, T r) { return l.f != r.f ? l.f < r.f : l.s < r.s; }); break; case fisd: sort(all(a), [&](T l, T r) { return l.f != r.f ? l.f < r.f : l.s > r.s; }); break; case fdsi: sort(all(a), [&](T l, T r) { return l.f != r.f ? l.f > r.f : l.s < r.s; }); break; case fdsd: sort(all(a), [&](T l, T r) { return l.f != r.f ? l.f > r.f : l.s > r.s; }); break; case sifi: sort(all(a), [&](T l, T r) { return l.s != r.s ? l.s < r.s : l.f < r.f; }); break; case sifd: sort(all(a), [&](T l, T r) { return l.s != r.s ? l.s < r.s : l.f > r.f; }); break; case sdfi: sort(all(a), [&](T l, T r) { return l.s != r.s ? l.s > r.s : l.f < r.f; }); break; case sdfd: sort(all(a), [&](T l, T r) { return l.s != r.s ? l.s > r.s : l.f > r.f; }); break; } }; template<class T> inline void rsort(vector<T> &a) { sort(a.begin(), a.end(), greater<T>()); }; template<class T> inline void rsort(vector<T> &a, int len) { sort(a.begin(), a.begin() + len, greater<T>()); }; template<class U, class F> inline void rsort(vector<U> &a, F f) { sort(a.begin(), a.end(), [&](U l, U r) { return f(l) > f(r); }); }; template<class U> inline void sortp(vector<U> &a, vector<U> &b) { vp c; int n = sz(a); assert(n == sz(b)); rep(i, n)c.eb(a[i], b[i]); sort(c); rep(i, n) { a[i] = c[i].first; b[i] = c[i].second;; } }; //F = T<T> //例えばreturn p.fi + p.se; template<class U, class F> inline void sortp(vector<U> &a, vector<U> &b, F f) { vp c; int n = sz(a); assert(n == sz(b)); rep(i, n)c.eb(a[i], b[i]); sort(c, f); rep(i, n) { a[i] = c[i].first; b[i] = c[i].second; } }; template<class U, class F> inline void sortp(vector<U> &a, vector<U> &b, char type) { vp c; int n = sz(a); assert(n == sz(b)); rep(i, n)c.eb(a[i], b[i]); sort(c, type); rep(i, n) { a[i] = c[i].first; b[i] = c[i].second; } }; template<class U> inline void rsortp(vector<U> &a, vector<U> &b) { vp c; int n = sz(a); assert(n == sz(b)); rep(i, n)c.eb(a[i], b[i]); rsort(c); rep(i, n) { a[i] = c[i].first; b[i] = c[i].second; } }; template<class U, class F> inline void rsortp(vector<U> &a, vector<U> &b, F f) { vp c; int n = sz(a); assert(n == sz(b)); rep(i, n)c.eb(a[i], b[i]); rsort(c, f); rep(i, n) { a[i] = c[i].first; b[i] = c[i].second; } }; template<class U> inline void sortt(vector<U> &a, vector<U> &b, vector<U> &c) { vt r; int n = sz(a); assert(n == sz(b)); assert(n == sz(c)); rep(i, n)r.eb(a[i], b[i], c[i]); sort(r); rep(i, n) { a[i] = r[i].f; b[i] = r[i].s; c[i] = r[i].t; } }; template<class U, class F> inline void sortt(vector<U> &a, vector<U> &b, vector<U> &c, F f) { vt r; int n = sz(a); assert(n == sz(b)); assert(n == sz(c)); rep(i, n)r.eb(a[i], b[i], c[i]); sort(r, f); rep(i, n) { a[i] = r[i].f; b[i] = r[i].s; c[i] = r[i].t; } }; template<class U, class F> inline void rsortt(vector<U> &a, vector<U> &b, vector<U> &c, F f) { vt r; int n = sz(a); assert(n == sz(b)); assert(n == sz(c)); rep(i, n)r.eb(a[i], b[i], c[i]); rsort(r, f); rep(i, n) { a[i] = r[i].f; b[i] = r[i].s; c[i] = r[i].t; } }; template<class T> inline void sort2(vector<vector<T>> &a) { for (int i = 0, n = a.size(); i < n; i++)sort(a[i]); } template<class T> inline void rsort2(vector<vector<T>> &a) { for (int i = 0, n = a.size(); i < n; i++)rsort(a[i]); } template<typename A, size_t N, typename T> void fill(A (&a)[N], const T &v) { rep(i, N)a[i] = v; } template<typename A, size_t N, size_t O, typename T> void fill(A (&a)[N][O], const T &v) { rep(i, N)rep(j, O)a[i][j] = v; } template<typename A, size_t N, size_t O, size_t P, typename T> void fill(A (&a)[N][O][P], const T &v) { rep(i, N)rep(j, O)rep(k, P)a[i][j][k] = v; } template<typename A, size_t N, size_t O, size_t P, size_t Q, typename T> void fill(A (&a)[N][O][P][Q], const T &v) { rep(i, N)rep(j, O)rep(k, P)rep(l, Q)a[i][j][k][l] = v; } template<typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, typename T> void fill(A (&a)[N][O][P][Q][R], const T &v) { rep(i, N)rep(j, O)rep(k, P)rep(l, Q)rep(m, R)a[i][j][k][l][m] = v; } template<typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S, typename T> void fill(A (&a)[N][O][P][Q][R][S], const T &v) { rep(i, N)rep(j, O)rep(k, P)rep(l, Q)rep(m, R)rep(n, S)a[i][j][k][l][m][n] = v; } template<typename V, typename T> void fill(V &xx, const T vall) { xx = vall; } template<typename V, typename T> void fill(vector<V> &vecc, const T vall) { for (auto &&vx: vecc) fill(vx, vall); } //@汎用便利関数 入力 template<typename T = int> T _in() { T x; cin >> x; return (x); } #define _overloadin(_1, _2, _3, _4, name, ...) name #define in0() _in() #define in1(a) cin>>a #define in2(a, b) cin>>a>>b #define in3(a, b, c) cin>>a>>b>>c #define in4(a, b, c, d) cin>>a>>b>>c>>d #define in(...) _overloadin(__VA_ARGS__,in4,in3,in2 ,in1,in0)(__VA_ARGS__) #define _overloaddin(_1, _2, _3, _4, name, ...) name #define din1(a) int a;cin>>a #define din2(a, b) int a,b;cin>>a>>b #define din3(a, b, c) int a,b,c;cin>>a>>b>>c #define din4(a, b, c, d) int a,b,c,d;cin>>a>>b>>c>>d #define din(...) _overloadin(__VA_ARGS__,din4,din3,din2 ,din1)(__VA_ARGS__) #define _overloaddind(_1, _2, _3, _4, name, ...) name #define din1d(a) int a;cin>>a;a-- #define din2d(a, b) int a,b;cin>>a>>b;a--,b-- #define din3d(a, b, c) int a,b,c;cin>>a>>b>>c;a--,b--,c-- #define din4d(a, b, c, d) int a,b,c,d;cin>>a>>b>>c>>d;;a--,b--,c--,d-- #define dind(...) _overloaddind(__VA_ARGS__,din4d,din3d,din2d ,din1d)(__VA_ARGS__) string sin() { return _in<string>(); } ll lin() { return _in<ll>(); } #define na(a, n) a.resize(n); rep(nai,n) cin >> a[nai]; #define nao(a, n) a.resize(n+1); rep(i,n) cin >> a[i+1]; #define nad(a, n) a.resize(n); rep(i,n){ cin >> a[i]; a[i]--;} #define na2(a, b, n) a.resize(n),b.resize(n);rep(i, n)cin >> a[i] >> b[i]; #define na2d(a, b, n) a.resize(n),b.resize(n);rep(i, n){cin >> a[i] >> b[i];a[i]--,b[i]--;} #define na3(a, b, c, n) a.resize(n),b.resize(n),c.resize(n); rep(i, n)cin >> a[i] >> b[i] >> c[i]; #define na3d(a, b, c, n) a.resize(n),b.resize(n),c.resize(n); rep(i, n){cin >> a[i] >> b[i] >> c[i];a[i]--,b[i]--,c[i]--;} #define nt(a, h, w) a.resize(h);rep(i,h)a[i].resize(w); rep(hi,h)rep(wi,w) cin >> a[hi][wi]; #define ntd(a, h, w) rs(a,h,w);rep(hi,h)rep(wi,w) cin >> a[hi][wi], a[hi][wi]--; #define ntp(a, h, w) fill(a,'#');rep(hi,1,h+1)rep(wi,1,w+1) cin >> a[hi][wi]; #ifdef _DEBUG template<class T> void err2(T &&head) { cerr << head; } template<class T, class... U> void err2(T &&head, U &&... tail) { cerr << head << " "; err2(tail...); } template<class T, class... U> void err(T &&head, U &&... tail) { cerr << head << " "; err2(tail...); cerr << "" << endl; } template<class T> void err(T &&head) { cerr << head << endl; } void err() { cerr << "" << endl; } template<class T> string out_m2(vector<T> &a, ll W = inf) { stringstream ss; if (W == inf)W = min(sz(a), 12ll); if (sz(a) == 0)return ss.str(); rep(i, W) { ss << a[i] << " "; } return ss.str(); } template<class T> string out_m2(vector<vector<T> > &a, ll H = inf, ll W = inf, int key = -1) { H = min({H, sz(a), 12ll}); W = min({W, sz(a[0]), 12ll}); stringstream ss; ss << endl; if (key == -1)ss << " *|"; else ss << " " << key << "|"; rep(w, W)ss << std::right << std::setw(4) << w; ss << "" << endl; rep(w, W * 4 + 3)ss << "_"; ss << "" << endl; rep(h, H) { ss << std::right << std::setw(2) << h << "|"; rep(w, min(sz(a[h]), 12ll)) { if (abs(a[h][w]) == linf) ss << " e" << ""; else ss << std::right << std::setw(4) << a[h][w]; } ss << "" << endl; } return ss.str(); } /*@formatter:off*/ template<class T> string out_m2(vector<vector<vector<T> > > &a, ll H = inf, ll W = inf, ll U = inf) {stringstream ss; if (H == inf)H = 5; H = min(H, sz(a)); rep(i, H) { ss << endl; ss << out_m2(a[i], W, U, i); } return ss.str();} string out_m2(int a) {stringstream ss;ss << a;return ss.str();} template<class T> string out_m2(T &a) {stringstream ss;ss << a;return ss.str();} template<class T> string out_m(vector<T> &a, ll W = inf) {stringstream ss; if (W == inf)W = min(sz(a), 12ll); if(sz(a)==0)return ss.str(); rep(i, W) { ss << a[i] << " "; } ss << "" << endl; return ss.str();} template<class T> string out_m(vector<vector<T> > &a, ll H = inf, ll W = inf, int key = -1) { H = min({H, sz(a), 12ll}); W = min({W, sz(a[0]), 12ll}); stringstream ss; ss << endl; if (key == -1)ss << " *|"; else ss << " " << key << "|"; rep(w, W)ss << std::right << std::setw(4) << w; ss << "" << endl; rep(w, W * 4 + 3)ss << "_"; ss << "" << endl; rep(h, H) { ss << std::right << std::setw(2) << h << "|"; rep(w, min(sz(a[h]),12ll)) { if (abs(a[h][w]) == linf) ss << " e" << ""; else ss << std::right << std::setw(4) << a[h][w]; } ss << "" << endl; } ss << endl; return ss.str(); } /*@formatter:off*/ template<class T> string out_m(vector<vector<vector<T> > > &a, ll H = inf, ll W = inf, ll U = inf) {stringstream ss; if (H == inf)H = 5; H = min(H, sz(a)); rep(i, H) { ss << endl; ss << out_m(a[i], W, U, i); } ss << endl; return ss.str();} string out_m(int a) {stringstream ss;ss << a << endl;return ss.str();} template<class T> string out_m(T &a) {stringstream ss;ss << a << endl;return ss.str();} template<class T> void outv(vector<T> &a, ll W=inf) {cout << out_m(a,W) << endl;} template<class T> void outv(vector<vector<T> > &a, ll H = linf, ll W = linf,int key=-1) { cout << out_m(a,H,W,key) << endl;} template<class T> void outv(vector<vector<vector<T> > > &a, ll H = linf, ll W = linf,ll U = linf) {cout << out_m(a,H,W,U)<< endl;} #else template<class T> void outv(vector<T> &a, ll W = inf) { rep(i, min(W, sz(a))) { cout << a[i] << " "; } cout << "" << endl; } template<class T> void outv(vector<vector<T> > &a, ll H = linf, ll W = linf, int key = -1) { rep(i, min(H, sz(a))) { outv(a[i], W); }} template<class T> void outv(vector<vector<vector<T> > > &a, ll H = linf, ll W = linf, ll U = linf) { ; } #define err(...); #endif #define debugName(VariableName) # VariableName #define deb1(x) debugName(x)<<" = "<<out_m2(x) #define deb2(x, ...) deb1(x) <<", "<< deb1(__VA_ARGS__) #define deb3(x, ...) deb1(x) <<", "<< deb2(__VA_ARGS__) #define deb4(x, ...) deb1(x) <<", "<< deb3(__VA_ARGS__) #define deb5(x, ...) deb1(x) <<", "<< deb4(__VA_ARGS__) #define deb6(x, ...) deb1(x) <<", "<< deb5(__VA_ARGS__) #define deb7(x, ...) deb1(x) <<", "<< deb6(__VA_ARGS__) #define deb8(x, ...) deb1(x) <<", "<< deb7(__VA_ARGS__) #define deb9(x, ...) deb1(x) <<", "<< deb8(__VA_ARGS__) #define deb10(x, ...) deb1(x) <<", "<< deb9(__VA_ARGS__) #define o_ebug(o1, o2, o3, o4, o5, o6, o7, o8, o9, o10, name, ...) name #ifdef _DEBUG #define deb(...) cerr<< o_ebug(__VA_ARGS__,deb10,deb9,deb8,deb7,deb6,deb5,deb4,deb3,deb2,deb1)(__VA_ARGS__) <<endl #else #define deb(...) ; #endif #define debugline(x) cerr << x << " " << "(L:" << __LINE__ << ")" << '\n' void ole() { #ifdef _DEBUG debugline("ole"); exit(0); #endif string a = "a"; rep(i, 30)a += a; rep(i, 1 << 17)cout << a << endl; cout << "OLE 出力長制限超過" << endl; exit(0); } void tle() { while (inf)cout << inf << endl; } ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll gcd(vi b) { ll res = b[0]; for (auto &&v :b)res = gcd(v, res); return res; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll rev(ll a) { ll res = 0; while (a) { res *= 10; res += a % 10; a /= 10; } return res; } template<class T> void rev(vector<T> &a) { reverse(all(a)); } void rev(string &a) { reverse(all(a)); } ll ceil(ll a, ll b) { if (b == 0) { debugline("ceil"); deb(a, b); ole(); return -1; } else return (a + b - 1) / b; } ll sqrt(ll a) { if (a < 0) { debugline("sqrt"); deb(a); ole(); } ll res = (ll) std::sqrt(a); while (res * res < a)res++; return res; } double log(double e, double x) { return log(x) / log(e); } ll sig(ll t) { return (1 + t) * t / 2; } ll sig(ll s, ll t) { return (s + t) * (t - s + 1) / 2; } /*@formatter:off*/ inline bool inside(int h, int w, int H, int W) { return h >= 0 && w >= 0 && h < H && w < W; } inline bool inside(int v, int l, int r) { return l <= v && v < r; } #define ins inside ll u(ll a) { return a < 0 ? 0 : a; } template<class T> vector<T> u(const vector<T> &a) { vector<T> ret = a; fora(v, ret)v = u(v); return ret; } #define MIN(a) numeric_limits<a>::min() #define MAX(a) numeric_limits<a>::max() void yn(bool a) { if (a)cout << "yes" << endl; else cout << "no" << endl; } void Yn(bool a) { if (a)cout << "Yes" << endl; else cout << "No" << endl; } void YN(bool a) { if (a)cout << "YES" << endl; else cout << "NO" << endl; } void fyn(bool a) { if (a)cout << "yes" << endl; else cout << "no" << endl; exit(0); } void fYn(bool a) { if (a)cout << "Yes" << endl; else cout << "No" << endl; exit(0); } void fYN(bool a) { if (a)cout << "YES" << endl; else cout << "NO" << endl; exit(0); } void Possible(bool a) { if (a)cout << "Possible" << endl; else cout << "Impossible" << endl; exit(0); } void POSSIBLE(bool a) { if (a)cout << "POSSIBLE" << endl; else cout << "IMPOSSIBLE" << endl; exit(0); } template<class T, class U> set<T> &operator+=(set<T> &a, U v) { a.insert(v); return a; } template<class T, class U> vector<T> &operator+=(vector<T> &a, U v) { a.pb(v); return a; } template<class T> T sum(vector<T> &v, int s = 0, int t = inf) { T ret = 0; rep(i, s, min(sz(v), t))ret += v[i]; return ret; } void mod(int &a, int m) { a = (a % m + m) % m; } template<class F> inline int mgr(int ok, int ng, F f) { #define _mgrbody int mid = (ok + ng) / 2; if (f(mid))ok = mid; else ng = mid; if (ok < ng)while (ng - ok > 1) { _mgrbody } else while (ok - ng > 1) { _mgrbody } return ok; } template<class F> inline int mgr(int ok, int ng, int second, F f) { #define _mgrbody2 int mid = (ok + ng) / 2; if (f(mid, second))ok = mid; else ng = mid; if (ok < ng) while (ng - ok > 1) { _mgrbody2 } else while (ok - ng > 1) { _mgrbody2 } return ok; } vp run_length(vi &a) { vp ret; ret.eb(a[0], 1); rep(i, 1, sz(a)) { if (ret.back().fi == a[i]) { ret.back().se++; } else { ret.eb(a[i], 1); }} return ret;} vector<pair<char, ll>> run_length(string &a) { vector<pair<char, ll>> ret; ret.eb(a[0], 1); rep(i, 1, sz(a)) { if (ret.back().fi == a[i]) { ret.back().se++; } else { ret.eb(a[i], 1); }} return ret;} template<typename T> ostream &operator<<(ostream &os, vector<T> &m) { for (auto &&v:m) os << v << " "; return os; } template<typename T> ostream &operator<<(ostream &os, vector<vector<T>> &m) { for (auto &&v:m) os << v <<endl; return os; } constexpr bool bget(ll m, int keta) { return (m >> keta) & 1; } int bget(ll m, int keta, int sinsuu) { m /= (ll) pow(sinsuu, keta); return m % sinsuu; } ll bit(int n) { return (1LL << (n)); } ll bit(int n, int sinsuu) { return (ll) pow(sinsuu, n); } int mask(int n) { return (1ll << n) - 1; } #define bcou __builtin_popcountll // //imoは0-indexed //ruiは1-indexed template<class T> vector<T> imo(vector<T> &v) { vector<T> ret = v; rep(i, sz(ret) - 1)ret[i + 1] += ret[i]; return ret;} //kと同じものの数 template<class T, class U> vi imo(vector<T> &a, U k) {vector<T> ret = a;rep(i, sz(ret))ret[i] = a[i] == k;rep(i, sz(ret) - 1)ret[i + 1] += ret[i];return ret;} template<class T> vector<T> imox(vector<T> &v) { vector<T> ret = v; rep(i, sz(ret) - 1)ret[i + 1] ^= ret[i]; return ret;} //漸化的に最小を持つ template<class T> vector<T> imi(vector<T> &v) { vector<T> ret = v; rep(i, sz(ret) - 1)chmi(ret[i + 1], ret[i]); return ret;} template<class T> struct ruiC { const vector<T> rui; ruiC(vector<T> &ru) : rui(ru) {} T operator()(ll l, ll r) { if (l > r) { cerr<<"ruic ";deb(l, r);assert(0); } return rui[r] - rui[l]; } T operator[](ll i) { return rui[i]; } T back() { return rui.back(); } ll size() { return rui.size(); }}; template<class T> struct rruic { const T *rrui; rruic(T *ru) : rrui(ru) {} T operator()(ll l, ll r) { assert(l >= r); return rrui[r] - rrui[l]; } T operator[](ll i) { return rrui[i]; }}; template<class T>ostream &operator<<(ostream &os, ruiC<T> a) {fora(v,a.rui)os<<v<<" ";return os;} template<class T> vector<T> ruiv(vector<T> &a) { vector<T> ret(a.size() + 1); rep(i, a.size())ret[i + 1] = ret[i] + a[i]; return ret;} template<class T> ruiC<T> ruic(vector<T> &a) { vector<T> ret = ruiv(a); return ruiC<T>(ret);} vector<ll> ruiv(string &a) { if (sz(a) == 0)return vi(1); ll dec = ('0' <= a[0] && a[0] <= '9') ? '0' : 0; vector<ll> ret(a.size() + 1); rep(i, a.size())ret[i + 1] = ret[i] + a[i] - dec; return ret;} ruiC<ll> ruic(string &a) { vector<ll> ret = ruiv(a); return ruiC<ll>(ret);} //kと同じものの数 template<class T, class U> vi ruiv(T &a, U k) { vi ret(a.size() + 1); rep(i, a.size())ret[i + 1] = ret[i] + (a[i] == k); return ret;} template<class T, class U> ruiC<ll> ruic(T &a, U k) { vi ret = ruiv(a, k); return ruiC<ll>(ret);} //xor template<class T> struct ruixC { const vector<T> rui; ruixC(vector<T> &ru) : rui(ru) {} T operator()(ll l, ll r) { if (l > r) { cerr << "ruiXc "; deb(l, r); assert(0); } return rui[r] ^ rui[l]; } T operator[](ll i) { return rui[i]; } T back() { return rui.back(); } ll size() { return rui.size(); }}; template<class T> vector<T> ruix(vector<T> &a) { vector<T> ret(a.size() + 1); rep(i, a.size())ret[i + 1] = ret[i] ^ a[i]; return ret;} template<class T> ruixC<ll> ruixc(vector<T> &a) {vi ret = ruix(a);return ruixC<ll>(ret);} template<class T> vector<T> ruim(vector<T> &a) { vector<T> res(a.size() + 1, 1); rep(i, a.size())res[i + 1] = res[i] * a[i]; return res;} //漸化的に最小を1indexで持つ template<class T> vector<T> ruimi(vector<T> &a) { ll n = sz(a); vector<T> ret(n + 1); rep(i, 1, n) { ret[i] = a[i - 1]; chmi(ret[i + 1], ret[i]); } return ret;} //template<class T> T *rrui(vector<T> &a) { //右から左にかけての半開区間 (-1 n-1] template<class T> rruic<T> rrui(vector<T> &a) { ll len = a.size(); T *body = (T *) malloc((len + 1) * sizeof(T)); T *res = body + 1; rer(i, len - 1)res[i - 1] = res[i] + a[i]; return rruic<T>(res);} //掛け算 template<class T> T *rruim(vector<T> &a) { ll len = a.size(); T *body = (T *) malloc((len + 1) * sizeof(T)); T *res = body + 1; res[len - 1] = 1; rer(i, len - 1)res[i - 1] = res[i] * a[i]; return res;} // template<class T, class U> inline bool chma(T &a, const U &b) { if (a < b) { a = b; return true; } return false; } template<class U> inline bool chma(const U &b) { return chma(ma, b); } template<class T, class U> inline bool chmi(T &a, const U &b) { if (b < a) { a = b; return true; } return false; } template<class U> inline bool chmi(const U &b) { return chmi(mi, b); } #define unique(v) v.erase( unique(v.begin(), v.end()), v.end() ); int max(vi &a) { int res = a[0]; fora(v, a) { res = max(res, v); } return res; } int min(vi &a) { int res = a[0]; fora(v, a) { res = min(res, v); } return res; } template<typename T> class fixed_point : T {public: explicit constexpr fixed_point(T &&t) noexcept: T(std::forward<T>(t)) {} template<typename... Args> constexpr decltype(auto) operator()(Args &&... args) const { return T::operator()(*this, std::forward<Args>(args)...); }};template<typename T> static inline constexpr decltype(auto) fix(T &&t) noexcept { return fixed_point<T>{std::forward<T>(t)}; } #define lam(right) [&](auto& p){return p right;} template<typename W, typename H> void resize(vector<W> &vec, const H head) { vec.resize(head); }template<typename W, typename H, typename ... T> void resize(vector<W> &vec, const H &head, const T ... tail) {vec.resize(head);for (auto &v: vec)resize(v, tail...);} template<typename T, typename F> bool all_of2(T &v, F f) { return f(v); } template<typename T, typename F> bool all_of2(vector<T> &v, F f) { rep(i, sz(v)) { if (!all_of2(v[i], f))return false; } return true;} template<typename T, typename F> bool any_of2(T &v, F f) { return f(v); } template<typename T, typename F> bool any_of2(vector<T> &v, F f) { rep(i, sz(v)) { if (any_of2(v[i], f))return true; } return false;} template<typename T, typename F> bool none_of2(T &v, F f) { return f(v); } template<typename T, typename F> bool none_of2(vector<T> &v, F f) { rep(i, sz(v)) { if (none_of2(v[i], f))return false; } return true;} template<typename T, typename F> bool find_if2(T &v, F f) { return f(v); } template<typename T, typename F> ll find_if2(vector<T> &v, F f) { rep(i, sz(v)) { if (find_if2(v[i], f))return i; } return sz(v);} template<typename T, typename F> bool rfind_if2(T &v, F f) { return f(v); } template<typename T, typename F> ll rfind_if2(vector<T> &v, F f) { rer(i, sz(v) - 1) { if (rfind_if2(v[i], f))return i; } return -1;} template<class T> bool contains(string &s, const T &v) { return s.find(v) != string::npos; } template<typename T> bool contains(vector<T> &v, const T &val) { return std::find(v.begin(), v.end(), val) != v.end(); } template<typename T, typename F> bool contains_if2(vector<T> &v, F f) { return find_if(v.begin(), v.end(), f) != v.end(); } template<typename T, typename F> ll count_if2(T &v, F f) { return f(v); } template<typename T, typename F> ll count_if2(vector<T> &vec, F f) { ll ret = 0; fora(v, vec)ret += count_if2(v, f); return ret;} template<typename T, typename F> void for_each2(T &v, F f) { f(v); } template<typename T, typename F> void for_each2(vector<T> &vec, F f) { fora(v, vec)for_each2(v, f); } template<typename W> ll count_od(vector<W> &a) {return count_if2(a,[](ll v){return v&1 ;});} template<typename W> ll count_ev(vector<W> &a) {return count_if2(a,[](ll v){return !(v&1) ;});} //削除された要素の数を返す template<typename T, typename F> int erase_if2(vector<T> &v, F f) {vector<T> nv;int cou = 0;rep(i, sz(v)) {if (f(v[i])) {cou++;} else {nv.push_back(v[i]);}}v = nv;return cou;} template<typename T, typename F> int erase_if2(vector<vector<T>> &v, F f) {int cou = 0;rep(i, sz(v)) {cou += erase_if2(v[i], f);}return cou;} #define all_of(a,right) all_of2(a,lam(right)) #define all_of_f(a,f) all_of2(a,f) #define any_of(a,right) any_of2(a,lam(right)) #define any_of_f(a,f) any_of2(a,f) #define none_of(a,right) none_of2(a,lam(right)) #define none_of_f(a,f) none_of2(a,f) #define find_if(a,right) find_if2(a,lam(right)) #define find_if_f(a,f) find_if2(a,f) #define rfind_if(a,right) rfind_if2(a,lam(right)) #define rfind_if_f(a,f) rfind_if2(a,f) #define contains_if(a,right) contains_if2(a,lam(right)) #define contains_if_f(a,f) contains_if2(a,f) #define count_if(a, right) count_if2(a,lam(right)) #define count_if_f(a, f) count_if2(a,f) #define for_each(a, right) do{fora(v,a){v right;}}while(0) #define for_each_f(a, f) do{fora(v,a){f(v);}}while(0) #define erase_if(a,right) erase_if2(a,lam(right)) #define erase_if_f(a,f) erase_if2(a,f) /*@formatter:on*/ int N, K, H, W; vi A, B, C; /*@formatter:off*/ #define forg(gi, ve) for (ll gi = 0,forglim = ve.size(), f, t, c; gi < forglim && (f = ve[gi].f, t = ve[gi].t, c = ve[gi].c, true); ++gi) #define fort(gi, ve) for (ll gi = 0, f, t, c; gi < ve.size() && (f = ve[gi].f, t = ve[gi].t, c = ve[gi].c, true); ++gi)if(t!=p) #define fore(gi, ve) for (ll gi = 0,forglim = ve.size(), f, t, c; gi < forglim && (f = ve[gi].f, t = ve[gi].t, c = ve[gi].c, true); ++gi) template<class T> struct edge { int f, t; T c; edge(int f, int t, T c = 1) : f(f), t(t), c(c) {} bool operator<(const edge &b) const { return c < b.c; } bool operator>(const edge &b) const { return c > b.c; }}; template<class T> ostream &operator<<(ostream &os, edge<T> &e) {os << e.f << " " << e.t << " " << e.c;return os;} template<typename T> class graph { public : vector<vector<edge<T>>> g; vector<edge<T>> edges; int n; explicit graph(int n) : n(n) { g.resize(n); } void clear() { g.clear(), edges.clear(); } void resize(int n) {this->n = n;g.resize(n);} int size() { return n; } vector<edge<T> > &operator[](int i) { return g[i]; } virtual void add(int f, int t, T c) = 0; virtual void set_edges() = 0; }; template<typename T =ll> class digraph : public graph<T> { public: using graph<T>::g; using graph<T>::n; using graph<T>::edges; explicit digraph(int n) : graph<T>(n) {} void add(int f, int t, T c = 1) { if (!(0 <= f && f < n && 0 <= t && t < n)) {debugline("digraph add");deb(f, t, c);ole();} g[f].emplace_back(f, t, c); edges.emplace_back(f, t, c);//edgesを使わないなら消せる } void ing(int n, int m, int minus = 1) { this->resize(n); rep(i, m) { int f, t; cin >> f >> t; f -= minus; t -= minus; add(f, t); } } void ingc(int n, int m, int minus = 1) { this->resize(n); rep(i, m) { int f, t, c; cin >> f >> t >> c; f -= minus; t -= minus; add(f, t, c); } } void set_edges() override{ if (sz(edges))return; rep(i, n)fora(e, g[i])edges.push_back(e); } }; template<class T=int> class undigraph : public graph<T> { public: using graph<T>::g; using graph<T>::n; using graph<T>::edges; explicit undigraph(int n) : graph<T>(n) {} // f < t void add(int f, int t, T c = 1) { if (!(0 <= f && f < n && 0 <= t && t < n)) { debugline("undigraph add"); deb(f, t, c); ole(); } g[f].emplace_back(f, t, c); g[t].emplace_back(t, f, c); edges.emplace_back(f, t, c);//edgesを使わないなら消せる edges.emplace_back(t, f, c); } void add(edge<T> &e) { int f = e.f, t = e.t; T c = e.c; add(f, t, c); } void ing(int n, int m, int minus = 1) { this->resize(n); rep(i, m) { int f, t; cin >> f >> t; f -= minus; t -= minus; add(f, t); } } void ingc(int n, int m, int minus = 1) { this->resize(n); rep(i, m) { int f, t, c; cin >> f >> t >> c; f -= minus; t -= minus; add(f, t, c); } } void set_edges () override{ if (sz(edges))return; rep(i, n)fora(e, g[i])edges.push_back(e); } }; template<class T> vector<T> dijkstra_mitu(const graph<T> &g, int s, int init_value = -1) { if (!(0 <= s && s < g.n)) { debugline("dijkstra_mitu"); deb(s, g.n); ole(); } T initValue = MAX(T); vector<T> dis(g.n, initValue); dis[s] = 0; vb used(g.n); while (true) { int v = -1; rep(i, g.n) { if (!used[i] && ((v == -1 && dis[i] != initValue) || (dis[i] < dis[v]))) { v = i; } } if (v == -1)break; used[v] = 1; for (auto &&e : g.g[v]) { if (dis[e.t] > dis[v] + e.c) { dis[e.t] = dis[v] + e.c; } } } /*基本、たどり着かないなら-1*/ for (auto &&d :dis) { if (d == initValue) { d = init_value; } } return dis;} template<class T> vector<T> dijkstra_normal(const graph<T> &g, int s, int init_value = -1) { if (!(0 <= s && s < g.n)) { debugline("dijkstra"); deb(s, g.n); ole(); } if ((g.n + sz(g.edges)) * 20 > g.n * g.n) { return dijkstra_mitu(g, s, init_value); } T initValue = MAX(T); vector<T> dis(g.n, initValue); priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> q; dis[s] = 0; q.emplace(0, s); while (q.size()) { T nowc = q.top().fi; int i = q.top().se; q.pop(); if (dis[i] != nowc)continue; for (auto &&e : g.g[i]) { int to = e.t; T c = nowc + e.c; if (dis[to] > c) { dis[to] = c; q.emplace(dis[to], to); } } } /*基本、たどり着かないなら-1*/ for (auto &&d :dis) if (d == initValue)d = init_value; return dis;} template<class T> vector<T> dijkstra_01(graph<T> &g, int s) { int N = g.n; vi dis(N, linf); dis[s] = 0; deque<int> q; q.push_back(s); vb was(N); while (!q.empty()) { int f = q.front(); q.pop_front(); if (was[f])continue; was[f] = true; fora(e, g[f]) { if (dis[e.t] > dis[f] + e.c) { if (e.c) { dis[e.t] = dis[f] + 1; q.push_back(e.t); } else { dis[e.t] = dis[f]; q.push_front(e.t); } } } } return dis;} //dijkstra_cou<mint> : 数える型で書く return vp(dis,cou) template<class COU,class T=int> auto dijkstra_cou(const graph<T> &g, int s, int init_value = -1) { if (!(0 <= s && s < g.n)) { debugline("dijkstra"); deb(s, g.n); ole(); } err("count by type COU "); err("int or mint"); T initValue = MAX(T); vector<T> dis(g.n, initValue); vector<COU> cou(g.n); cou[s] = 1; priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> q; dis[s] = 0; q.emplace(0, s); while (q.size()) { T nowc = q.top().fi; int i = q.top().se; q.pop(); if (dis[i] != nowc)continue; for (auto &&e : g.g[i]) { int to = e.t; T c = nowc + e.c; if (dis[to] > c) { dis[to] = c; cou[to] = cou[e.f]; q.emplace(dis[to], to); } else if (dis[to] == c) { cou[to] += cou[e.f]; } } } /*基本、たどり着かないなら-1*/ for (auto &&d :dis) if (d == initValue)d = init_value; return vtop(dis, cou);} //密グラフの時、warshallに投げる template<class T> vector<vector<T>> dijkstra_all(const graph<T> &g, int init_value = -1) {int n = g.n;assert(n < 1e4);if (n * n < (n + sz(g.edges)) * 14) { /*O(N^3) vs O(N (N+M)log N)*/ return warshall(g, init_value); }vector<vector<T>> dis(n);rep(i, n) { dis[i] = dijkstra(g, i, init_value); }return dis;} //コストを無限に減らせる := -linf //たどり着けない := linf template<class T> vector<T> bell(graph<T> &g, int s) { if (g.n >= 1e4) { cout << "bell size too big" << endl; exit(0); } vector<T> res(g.n, linf); res[s] = 0; vb can(g.n); /*頂点から行けない頂点を持つ、辺を消しておく */ fix([&](auto ds, int p, int i) -> void { if (can[i])return; can[i] = true; forg(gi, g[i])if (t != p)ds(i, t); })(-1, 0); vector<edge<T>> es; fora(e, g.edges) { if (can[e.f])es += e; } rep(i, g.n) { bool upd = false; fora(e, es) { if (res[e.f] != linf && res[e.t] > res[e.f] + e.c) { upd = true; res[e.t] = res[e.f] + e.c; } } if (!upd)break; } rep(i, g.n * 2) { bool upd = 0; fora(e, g.edges) { if (res[e.f] != linf && res[e.t] != -linf && res[e.t] > res[e.f] + e.c) { upd = 1; res[e.t] = -linf; } } if (!upd)break; } return res;} //コストを無限に増やせる := linf //たどり着けない := -linf template<class T> vector<T> bell_far(graph<T> &g, int s) { if (g.n >= 1e4) { cout << "bell_far size too big" << endl; exit(0); } vector<T> res(g.n, linf); res[s] = 0; vb can(g.n); /*頂点から行けない頂点を持つ、辺を消しておく*/ fix([&](auto ds, int p, int i) -> void { if (can[i])return; can[i] = true; forg(gi, g[i])if (t != p)ds(i, t); })(-1, 0); vector<edge<T>> es; fora(e, g.edges) { if (can[e.f])es += e; } rep(i, g.n) { bool upd = false; fora(e, es) { if (res[e.f] != linf && res[e.t] > res[e.f] - e.c) {/*-c*/ upd = true; res[e.t] = res[e.f] - e.c;/*-c*/ } } if (!upd)break; } rep(i, g.n * 2) { bool upd = 0; fora(e, g.edges) { if (res[e.f] != linf && res[e.t] != -linf && res[e.t] > res[e.f] - e.c) {/*-c*/ upd = 1; res[e.t] = -linf; } } if (!upd)break; } rep(i, g.n)res[i] *= -1; return res;} template<class T> vector<vector<T>> warshall(const graph<T> &g, int init_value = -1) { int n = g.n; assert(n < 1e4); vector<vector<T> > dis(n, vector<T>(n, linf)); rep(i, n)fora(e, g.g[i]) { if (dis[e.f][e.t] > e.c) { dis[e.f][e.t] = e.c; } } rep(i, n)dis[i][i] = 0; rep(k, n) rep(i, n) rep(j, n) { if (dis[i][j] > dis[i][k] + dis[k][j]) { dis[i][j] = dis[i][k] + dis[k][j]; } } rep(i, n)rep(j, n) if (dis[i][j] == linf)dis[i][j] = init_value; return dis;} template<class T> class MinOp { public: T operator()(T a, T b) { return min(a, b); }}; template<typename OpFunc> struct SparseTable { OpFunc op; signed size; vector<signed> lg; vector<vector<pair<signed, signed>>> table; void init(const vector<pair<signed, signed>> &array, OpFunc opfunc) { signed n = array.size(); op = opfunc; lg.assign(n + 1, 0); for (signed i = 1; i <= n; i++) { lg[i] = 31 - __builtin_clz(i); } table.assign(lg[n] + 1, array); for (signed i = 1; i <= lg[n]; i++) { for (signed j = 0; j < n; j++) { if (j + (1 << (i - 1)) < n) { table[i][j] = op(table[i - 1][j], table[i - 1][j + (1 << (i - 1))]); } else { table[i][j] = table[i - 1][j]; }}} } pair<signed, signed> query(signed l, signed r) { assert(l < r); return op(table[lg[r - l]][l], table[lg[r - l]][r - (1 << lg[r - l])]); }}; struct PMORMQ { vector<signed> a; SparseTable<MinOp<pair<signed, signed> > > sparse_table; vector<vector<vector<signed> > > lookup_table; vector<signed> block_type; signed block_size, n_block; void init(const vector<signed> &array) { a = array; signed n = a.size(); block_size = std::max(1, (31 - __builtin_clz(n)) / 2); while (n % block_size != 0) { a.push_back(a.back() + 1); n++; } n_block = n / block_size; vector<pair<signed, signed> > b(n_block, make_pair(INT_MAX, INT_MAX)); for (signed i = 0; i < n; i++) { b[i / block_size] = min(b[i / block_size], make_pair(a[i], i)); } sparse_table.init(b, MinOp<pair<signed, signed> >()); block_type.assign(n_block, 0); for (signed i = 0; i < n_block; i++) { signed cur = 0; for (signed j = 0; j < block_size - 1; j++) { signed ind = i * block_size + j; if (a[ind] < a[ind + 1]) { cur |= 1 << j; } } block_type[i] = cur; } lookup_table.assign(1 << (block_size - 1), vector<vector<signed> >(block_size, vector<signed>(block_size + 1))); for (signed i = 0; i < (1 << (block_size - 1)); i++) { for (signed j = 0; j < block_size; j++) { signed res = 0; signed cur = 0; signed pos = j; for (signed k = j + 1; k <= block_size; k++) { lookup_table[i][j][k] = pos; if (i & (1 << (k - 1))) { cur++; } else { cur--; } if (res > cur) { pos = k; res = cur; } } } } } signed query(signed l, signed r) { assert(l < r); signed lb = l / block_size; signed rb = r / block_size; if (lb == rb) { return lb * block_size + lookup_table[block_type[lb]][l % block_size][r % block_size]; } signed pl = lb * block_size + lookup_table[block_type[lb]][l % block_size][block_size]; signed pr = rb * block_size + lookup_table[block_type[rb]][0][r % block_size]; signed pos = pl; if (r % block_size > 0 && a[pl] > a[pr]) { pos = pr; } if (lb + 1 == rb) { return pos; } signed spv = sparse_table.query(lb + 1, rb).second; if (a[pos] > a[spv]) { return spv; } return pos; }}; template<class T=int> class tree : public undigraph<T> {PMORMQ rmq; int cnt; vector<signed> id, in; bool never = true; bool never_hld = true; void dfs(int x, int p, int d, int dis = 0) { id[cnt] = x; par[x] = p; rmq_dep.push_back(d); disv[x] = dis; in[x] = cnt++; forg(gi, g[x]) { if (t == p) { continue; } dfs(t, x, d + 1, dis + c); id[cnt] = x; rmq_dep.push_back(d); cnt++; } } void precalc() { never = false; cnt = 0; rmq_dep.clear(); disv.assign(n, 0); in.assign(n, -1); id.assign(2 * n - 1, -1); par.assign(n, -1); dfs(root, -1, 0); rmq.init(rmq_dep); #ifdef _DEBUG if(n>=100)return;cerr << "---tree---" << endl; rep(i, n) { if (!(i == root || sz(g[i]) > 1))continue; cerr << i << " -> "; vi ts; forg(gi, g[i]) { if (t != par[i])ts.push_back(t); } rep(i, sz(ts) - 1)cerr << ts[i] << ", "; if(sz(ts))cerr << ts.back() << endl; } cerr << endl; #endif } int pos; void hld_build() { never_hld = false; if (never)precalc(); g.resize(n); vid.resize(n, -1); head.resize(n); heavy.resize(n, -1); depth.resize(n); inv.resize(n); subl.resize(n); subr.resize(n); dfs(root, -1); t = 0; dfs_hld(root); #ifdef _DEBUG if(n>=100)return;cerr << "---hld_index---" << endl; vi inds; rep(i, n) if (sz(g[i]))inds.push_back(i); rep(i, sz(inds)) { str s = tos(bel(inds[i])); cerr << std::right << std::setw(sz(s) + (i ? 1 : 0)) << inds[i]; } cerr << endl; rep(i, sz(inds)) { cerr << bel(inds[i]) << " "; } cerr << endl << endl; cerr << "---hld_edge_index---" << endl; fora(e, edges) { if (e.f <= e.t) cerr << e.f << "-" << e.t << " " << bel(e) << endl; } cerr << endl << endl; cerr << "!!edge or not edge carefull!!" << endl; cerr << "!!if(f<t) seg.add(bel(f,t),c) carefully(f,t) (t,f)!!" << endl; #endif } int dfs(int curr, int prev) { int sub = 1, max_sub = 0; heavy[curr] = -1; forg(i, g[curr]) { int next = t; if (next != prev) { depth[next] = depth[curr] + 1; int sub_next = dfs(next, curr); sub += sub_next; if (max_sub < sub_next) max_sub = sub_next, heavy[curr] = next; } } return sub; } int t = 0; void dfs_hld(int v = 0) { vid[v] = subl[v] = t; t++; inv[subl[v]] = v; if (0 <= heavy[v]) { head[heavy[v]] = head[v]; dfs_hld(heavy[v]); } forg(i, g[v])if (depth[v] < depth[t]) if (t != heavy[v]) { head[t] = t; dfs_hld(t); } subr[v] = t; }vector<signed> rmq_dep; public: using undigraph<T>::g; using undigraph<T>::n; using undigraph<T>::edges; vector<int> disv; //部分木の [左端、右端) index //部分木の辺に加算する場合 //add(subl[i],subr[i],x) //add(sub[i],sub[i+1],-x) vector<int> vid, head, heavy, par, depth, inv, subl, subr; int root; tree(int n_, int root = 0) : undigraph<T>(n_), root(root) { n = n_; } int lca(int a, int b) { if (never)precalc(); int x = in[a]; int y = in[b]; if (x > y) { swap(x, y); } int pos = rmq.query(x, y + 1); return id[pos]; } int dis(int a, int b) { if (never)precalc(); int x = in[a]; int y = in[b]; if (x > y) { swap(x, y); } int pos = rmq.query(x, y + 1); int p = id[pos]; return disv[a] + disv[b] - disv[p] * 2; } /*O(N) hldを使わず木を普通にたどる*/ void for_each_l(int u, int v, function<void(int)> fnode) { int r = lca(u, v); while (u != r) { fnode(u); u = par[u]; } while (v != r) { fnode(v); v = par[v]; } fnode(r); } void for_each_edge_l/*O(N) 頂点に対しての処理順が可換*/(int u, int v, function<void(edge<int> &)> fedge) { int r = lca(u, v); auto sub = [&](int u, int r) { while (u != r) { forg(gi, g[u]) { if (t == par[u]) { fedge(g[u][gi]); u = par[u]; break; } } } }; sub(u, r); sub(v, r); } //Fは半開 (u,v)は木の頂点 //中ではhldの頂点を見るため、seg木のupdateはhldのindexで行なう void for_each_/*[l,r)*/(int u, int v, const function<void(int, int)> &f) { if (never_hld)hld_build(); while (1) { if (vid[u] > vid[v]) swap(u, v); int l = max(vid[head[v]], vid[u]); int r = vid[v] + 1; f(l, r); if (head[u] != head[v]) v = par[head[v]]; else break; } } void for_each_edge/*[l,r) O(log(N)) 辺を頂点として扱っている 上と同じ感じで使える*/(int u, int v, const function<void(int, int)> &f) { if (never_hld)hld_build(); while (1) { if (vid[u] > vid[v]) swap(u, v); if (head[u] != head[v]) { int l = vid[head[v]]; int r = vid[v] + 1; f(l, r); v = par[head[v]]; } else { if (u != v) { int l = vid[u] + 1; int r = vid[v] + 1; f(l, r); } break; } } } int bel(int v) { /*hld内での頂点番号を返す*/ if (never_hld)hld_build();return vid[v];} //下の頂点に辺のクエリを持たせている int bel(int f, int t) { /*辺のクエリを扱うときどの頂点に持たせればいいか(vidを返すのでそのままupd出来る)*/ if (never_hld)hld_build();return depth[f] > depth[t] ? vid[f] : vid[t];} int bel(edge<T> &e) { /*辺のクエリを扱うときどの頂点に持たせればいいか(vidを返すのでそのままupd出来る)*/ if (never_hld)hld_build();return depth[e.f] > depth[e.t] ? vid[e.f] : vid[e.t];} template<class ... U> int operator()(U ... args) { return bel(args...); } //path l -> r += v template<class S> void seg_add(S &seg, int lhei, int rhei, int v) {for_each_(lhei, rhei, [&](int l, int r) { seg.add(l, r, v); });} template<class S> void seg_update(S &seg, int lhei, int rhei, int v) {for_each_(lhei, rhei, [&](int l, int r) { seg.update(l, r, v); });} template<class S> int seg_get(S &seg, int lhei, int rhei) { int res = 0; for_each_(lhei, rhei, [&](int l, int r) { res += seg.get(l, r); }); return res; } template<class S> void seg_add_edge(S &seg, int lhei, int rhei, int v) {for_each_edge(lhei, rhei, [&](int l, int r) { seg.add(l, r, v); });} template<class S> void seg_update_edge(S &seg, int lhei, int rhei, int v) {for_each_edge(lhei, rhei, [&](int l, int r) { seg.update(l, r, v); });} template<class S> int seg_get_edge(S &seg, int lhei, int rhei) {int res = 0;for_each_edge(lhei, rhei, [&](int l, int r) { res += seg.get(l, r); });return res;} //部分木iに対するクエリ template<class S> void seg_add_sub(S &seg, int i, int v) {if (never_hld)hld_build();seg.add(subl[i], subr[i], v);} template<class S> void seg_update_sub(S &seg, int i, int v) {if (never_hld)hld_build();seg.update(subl[i], subr[i], v);} template<class S> int seg_get_sub(S &seg, int i, int v) {if (never_hld)hld_build();return seg.get(subl[i], subr[i], v);} template<class S> void seg_add_sub_edge(S &seg, int i, int v) {if (never_hld)hld_build();/*iの上の辺が数えられてしまうため、i+1から*/seg.add(subl[i] + 1, subr[i], v);} template<class S> void seg_update_sub_edge(S &seg, int i, int v) {if (never_hld)hld_build();/*iの上の辺が数えられてしまうため、i+1から*/seg.update(subl[i] + 1, subr[i], v);} template<class S> int seg_get_sub_edge(S &seg, int i, int v) {if (never_hld)hld_build();/*iの上の辺が数えられてしまうため、i+1から*/return seg.get(subl[i] + 1, subr[i], v);} }; //辺が多いのでedgesを持たない //cost oo, ox, xo, xx 渡す template<class T=int> class grid_k6 : public undigraph<T> { vi costs; public: using undigraph<T>::g; using undigraph<T>::n; using undigraph<T>::edges; int H, W; vector<vector<char>> ba; char wall; void add(int f, int t, T c = 1){ cout << "grid" << endl; if (!(0 <= f && f < n && 0 <= t && t < n)) { debugline("grid_k6 add"); deb(f, t, c); ole(); } g[f].emplace_back(f, t, c); g[t].emplace_back(t, f, c); //undigraphと違い、edgesを持たない } int getid(int h, int w) { assert(ins(h, w, H, W)); return W * h + w; } int getid(P p) { return getid(p.first, p.second); } P get2(int id) { return mp(id / W, id % W); } P operator()(int id) { return get2(id); } int operator()(int h, int w) { return getid(h, w); } int operator()(P p) { return getid(p); } //辺は無い grid_k6(int H, int W) : H(H), W(W), undigraph<T>(H * W) {} grid_k6(vector<vector<char>> ba, char wall = '#') : H(sz(ba)), W(sz(ba[0])), undigraph<T>(sz(ba) * sz(ba[0])) { rep(h, H) { rep(w, W) { if (ba[h][w] == wall)con; int f = getid(h, w); if (w + 1 < W && ba[h][w + 1] != wall) { add(f, getid(h, w + 1)); } if (h + 1 < H && ba[h + 1][w] != wall) { add(f, getid(h + 1, w)); } } } } /*o -> o, o -> x, x-> x*/ grid_k6(vector<vector<char>> ba, int oo, int ox, int xo, int xx, char wall = '#') : H(sz(ba)), W(sz(ba[0])), undigraph<T>(sz(ba) * sz(ba[0])), costs({oo, ox, xo, xx}), ba(ba), wall(wall) { rep(h, H) { rep(w, W) { add(h, w, h + 1, w); add(h, w, h - 1, w); add(h, w, h, w + 1); add(h, w, h, w - 1); } } } void add(int fh, int fw, int th, int tw) { if (ins(fh, fw, H, W) && ins(th, tw, H, W)) { int cm = 0; if (ba[fh][fw] == wall) { cm += 2; } if (ba[th][tw] == wall) { cm++; } int f = getid(fh, fw); int t = getid(th, tw); g[f].emplace_back(f, t, costs[cm]); } } void set_edges() { rep(i, n)fora(e, g[i])if (e.f < e.t)edges.push_back(e); } }; //辺によりメモリを大量消費ためedgesを消している //頂点10^6でメモリを190MB(制限の8割)使う //左上から右下に移動できる template<class T=int> class digrid_k6 : public digraph<T> { public: using digraph<T>::g; using digraph<T>::n; using digraph<T>::edges; int H, W; void add(int f, int t, T c = 1) { if (!(0 <= f && f < n && 0 <= t && t < n)) { debugline("digrid_k6 add"); deb(f, t, c); ole(); } g[f].emplace_back(f, t, c); /*digraphと違いedgesを持たない*/ } int getid(int h, int w) { if (!ins(h, w, H, W))return -1; return W * h + w; } P get2(int id) { return mp(id / W, id % W); } P operator()(int id) { return get2(id); } int operator()(int h, int w) { return getid(h, w); } digrid_k6(int H, int W) : H(H), W(W), digraph<T>(H * W){} digrid_k6(vector<vector<char>> ba, char wall = '#') : H(sz(ba)), W(sz(ba[0])), digraph<T>(sz(ba) * sz(ba[0])) { rep(h, H) { rep(w, W) { if (ba[h][w] == wall)con; int f = getid(h, w); if (w + 1 < W && ba[h][w + 1] != wall) { add(f, getid(h, w + 1)); } if (h + 1 < H && ba[h + 1][w] != wall) { add(f, getid(h + 1, w)); } } } } void add(int fh, int fw, int th, int tw) { add(getid(fh, fw), getid(th, tw)); } void set_edges() { rep(i, n)fora(e, g[i])edges.push_back(e); } }; //@出力 template<class T> ostream &operator<<(ostream &os, digraph<T> &g) { os << endl << g.n << " " << sz(g.edges) << endl; fore(gi, g.edges) { os << f << " " << t << " " << c << endl; } return os;}template<class T> ostream &operator<<(ostream &os, undigraph<T> &g) { os << endl << g.n << " " << sz(g.edges) << endl; fore(gi, g.edges) { if (f < t)os << f << " " << t << " " << c << endl; } return os;} //@変形 //閉路がなければtrue bool topo(vi &res, digraph<int> &g) { int n = g.g.size(); vi nyu(n); rep(i, n)for (auto &&e :g[i])nyu[e.t]++; queue<int> st; rep(i, n)if (nyu[i] == 0)st.push(i); while (st.size()) { int v = st.front(); st.pop(); res.push_back(v); fora(e, g[v]) if (--nyu[e.t] == 0)st.push(e.t); } return res.size() == n;} //辞書順最小トポロジカルソート bool topos(vi &res, digraph<int> &g) { int n = g.g.size(); vi nyu(n); rep(i, n)for (auto &&e :g[i])nyu[e.t]++; /*小さい順*/ priority_queue<int, vector<int>, greater<int> > q; rep(i, n)if (nyu[i] == 0)q.push(i); while (q.size()) { int i = q.top(); q.pop(); res.push_back(i); fora(e, g[i])if (--nyu[e.t] == 0)q.push(e.t); } return res.size() == n;} //type,idが使いたい場合はgraty signed main() { in(H,W); na(A,H); na(B,W); sort(A); sort(B); digrid_k6<> g(H,W); rep(h, H) { //yoko rep(w, A[h]-1) { g.add(h,w,h,w+1); } rep(w,A[h],W){ g.add(h,w,h,w-1); } } rep(w, W) { rep(h, B[w]-1) { g.add(h,w,h+1,w); } rep(h,B[w],H){ g.add(h,w,h-1,w); } } vi d; topo(d,g); vvi(res,H,W); rep(i,sz(d)){ int h ,w; tie(h,w) = g.get2(d[i]); res[h][w]=i+1; } cout << res << endl; }