Datasets:

problem_id
stringlengths
6
6
buggy_code
stringlengths
8
526k
fixed_code
stringlengths
12
526k
labels
listlengths
0
15
buggy_submission_id
int64
1
1.54M
fixed_submission_id
int64
2
1.54M
user_id
stringlengths
10
10
language
stringclasses
8 values
p02945
#include <algorithm> #include <iostream> using namespace std; int main() { int A, B, list[3]; cin >> A >> B; list[0] = A + B; list[1] = A - B; list[2] = A * B; sort(list, list + 2, greater<int>()); cout << list[0] << endl; return 0; }
#include <algorithm> #include <iostream> using namespace std; int main() { int A, B, list[3]; cin >> A >> B; list[0] = A + B; list[1] = A - B; list[2] = A * B; sort(list, list + 3, greater<int>()); cout << list[0] << endl; return 0; }
[ "literal.number.change", "call.arguments.change", "expression.operation.binary.change" ]
743,892
743,893
u313578168
cpp
p02945
#include <bits/stdc++.h> #define print(x) cout << x << endl; using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(false); int A, B, tmp; cin >> A; cin >> B; tmp = max(A + B, A - B); tmp = max(A + B, A * B); print(tmp); return 0; }
#include <bits/stdc++.h> #define print(x) cout << x << endl; using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(false); int A, B, tmp; cin >> A; cin >> B; tmp = max(A + B, A - B); tmp = max(tmp, A * B); print(tmp); return 0; }
[ "assignment.value.change", "call.arguments.change", "expression.operation.binary.change", "expression.operation.binary.remove" ]
743,894
743,895
u854413258
cpp
p02945
#include <algorithm> #include <bitset> #include <cmath> #include <iostream> #include <list> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <tuple> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) typedef long long ll; typedef pair<int, int> P; const int inf = 1012345678; int dx[] = {1, -1, 0, 0}; int dy[] = {0, 0, 1, -1}; int N; int main() { int A, B; cin >> A >> B; cout << max((A * B, A - B), A + B) << endl; }
#include <algorithm> #include <bitset> #include <cmath> #include <iostream> #include <list> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <tuple> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) typedef long long ll; typedef pair<int, int> P; const int inf = 1012345678; int dx[] = {1, -1, 0, 0}; int dy[] = {0, 0, 1, -1}; int N; int main() { int A, B; cin >> A >> B; cout << max(max(A * B, A - B), A + B) << endl; }
[ "call.add", "call.arguments.change" ]
743,905
743,906
u950174376
cpp
p02945
#include <bits/stdc++.h> #define ll long long #define all(x) (x).begin(), (x).end() #define SZ(x) (x).size() #define fir first #define sec second void scan() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } using namespace std; const int maxn = 2e5 + 7; int a[maxn], pre[maxn]; int main() { scan(); ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); int a, b, c; cin >> a >> b; cout << max(a + b, max(a - b, a * b)) << endl; return 0; }
#include <bits/stdc++.h> #define ll long long #define all(x) (x).begin(), (x).end() #define SZ(x) (x).size() #define fir first #define sec second void scan() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } using namespace std; const int maxn = 2e5 + 7; int a[maxn], pre[maxn]; int main() { // scan(); ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); int a, b, c; cin >> a >> b; cout << max(a + b, max(a - b, a * b)) << endl; return 0; }
[ "call.remove" ]
744,043
744,044
u620866522
cpp
p02945
#include <bits/stdc++.h> using namespace std; int main() { long long a, b, c, d, ans, ans2, p; cin >> a >> b; cout << max(a * b, min(a + b, a - b)) << endl; }
#include <bits/stdc++.h> using namespace std; int main() { long long a, b, c, d, ans, ans2, p; cin >> a >> b; cout << max(a * b, max(a + b, a - b)) << endl; }
[ "misc.opposites", "identifier.change", "call.function.change", "io.output.change" ]
744,062
744,063
u902787159
cpp
p02945
#include <bits/stdc++.h> using namespace std; int sum(int x, int y) { return x + y; } int sub(int x, int y) { return x - y; } int mul(int x, int y) { return x * y; } int main(void) { int a, b, sm, sb, ml, gr; scanf("%d %d", &a, &b); sm = sum(a, b); sb = sub(a, b); ml = mul(a, b); if (sm >= sb && sm >= ml) gr = sm; else if (sb >= sm && sb >= ml) gr = sb; else if (ml >= sm && ml >= sb) gr = ml; else printf("%d\n", gr); }
#include <bits/stdc++.h> using namespace std; int sum(int x, int y) { return x + y; } int sub(int x, int y) { return x - y; } int mul(int x, int y) { return x * y; } int main(void) { int a, b, sm, sb, ml, gr = 1010; scanf("%d %d", &a, &b); sm = sum(a, b); sb = sub(a, b); ml = mul(a, b); if (sm >= sb && sm >= ml) gr = sm; else if (sb >= sm && sb >= ml) gr = sb; else if (ml >= sm && ml >= sb) gr = ml; printf("%d\n", gr); }
[ "variable_declaration.value.change", "control_flow.branch.else.remove" ]
744,087
744,088
u308280771
cpp
p02945
#include <bits/stdc++.h> using namespace std; int sum(int x, int y) { return x + y; } int sub(int x, int y) { return x - y; } int mul(int x, int y) { return x * y; } int main(void) { int a, b, sm, sb, ml, gr; scanf("%d %d", &a, &b); sm = sum(a, b); sb = sub(a, b); ml = mul(a, b); if (sm >= sb && sm >= ml) gr = sm; else if (sb >= sm && sb >= ml) gr = sb; else if (ml >= sm && ml >= sb) gr = ml; else printf("%d\n", gr); }
#include <bits/stdc++.h> using namespace std; int sum(int x, int y) { return x + y; } int sub(int x, int y) { return x - y; } int mul(int x, int y) { return x * y; } int main(void) { int a, b, sm, sb, ml, gr; scanf("%d %d", &a, &b); sm = sum(a, b); sb = sub(a, b); ml = mul(a, b); if (sm >= sb && sm >= ml) gr = sm; else if (sb >= sm && sb >= ml) gr = sb; else if (ml >= sm && ml >= sb) gr = ml; printf("%d\n", gr); }
[ "control_flow.branch.else.remove" ]
744,087
744,089
u308280771
cpp
p02945
#include <bits/stdc++.h> using namespace std; main() { int M, A, B; cin >> A, B; M = max(max(A + B, A - B), A * B); cout << M << endl; }
#include <bits/stdc++.h> using namespace std; main() { int M, A, B; cin >> A >> B; M = max(max(A + B, A - B), A * B); cout << M << endl; }
[]
744,097
744,098
u384561774
cpp
p02945
#include <iostream> using namespace std; int main() { int A, B; cin >> A >> B; cout << max((A + B, A - B), A * B) << endl; return 0; }
#include <iostream> using namespace std; int main() { int A, B; cin >> A >> B; cout << max(max(A + B, A - B), A * B) << endl; return 0; }
[ "call.add", "call.arguments.change" ]
744,099
744,100
u470566982
cpp
p02945
#include <bits/stdc++.h> using namespace std; #define DB(x) cout << #x << " = " << x << endl; #define LEFT(n) (2 * (n)) #define RIGHT(n) (2 * (n) + 1) typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> node; // typedef vector <vector <ll>> matrix; const ll INF = 1e9; const double EPS = 1e-9; const ll MOD = (ll)(1000000007); const ll MAXV = (ll)(2e5 + 10); const ll MAXE = (ll)(1e6 + 10); ll x, y; int main() { cin.sync_with_stdio(0); cin.tie(0); cin >> x >> y; cout << (max(x + y, x - y), x * y) << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; #define DB(x) cout << #x << " = " << x << endl; #define LEFT(n) (2 * (n)) #define RIGHT(n) (2 * (n) + 1) typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> node; // typedef vector <vector <ll>> matrix; const ll INF = 1e9; const double EPS = 1e-9; const ll MOD = (ll)(1000000007); const ll MAXV = (ll)(2e5 + 10); const ll MAXE = (ll)(1e6 + 10); ll x, y; int main() { cin.sync_with_stdio(0); cin.tie(0); cin >> x >> y; cout << max(max(x + y, x - y), x * y) << "\n"; return 0; }
[ "call.add" ]
744,110
744,111
u351158222
cpp
p02945
#include <stdio.h> int sum(int x, int y) { return x + y; } int sub(int x, int y) { return x - y; } int mul(int x, int y) { return x * y; } int main() { int a, b, sm, sb, ml, gr; scanf("%d %d", &a, &b); sm = sum(a, b); sb = sub(a, b); ml = mul(a, b); if (sm > sb && sm > ml) gr = sm; else if (sb > sm && sb > ml) gr = sb; else if (ml > sm && ml > sb) gr = ml; printf("%d\n", gr); main(); return 0; }
#include <stdio.h> int sum(int x, int y) { return x + y; } int sub(int x, int y) { return x - y; } int mul(int x, int y) { return x * y; } int main() { int a, b, sm, sb, ml, gr; scanf("%d %d", &a, &b); sm = sum(a, b); sb = sub(a, b); ml = mul(a, b); if (sm > sb && sm > ml) gr = sm; else if (sb > sm && sb > ml) gr = sb; else if (ml > sm && ml > sb) gr = ml; printf("%d\n", gr); return 0; }
[ "call.remove" ]
744,112
744,113
u601685273
cpp
p02816
//#define _GLIBCXX_DEBUG #include <bits/stdc++.h> #define FOR(i, a, b) for (ll i = a; i < b; i++) #define rep(i, n) FOR(i, 0, n) #define ROF(i, a, b) for (ll i = a; i >= b; i--) #define per(i, a) ROF(i, a, 0) #define pb push_back using namespace std; using ll = long long; using ld = long double; using ch = char; typedef pair<ll, ll> P; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<P> vP; typedef vector<ch> vc; typedef vector<vc> vvc; const ll MOD = 1000000007; const ll MOD2 = 998244353; const ld PI = acos(-1); const ll INF = 1e18; struct edge { ll to, cost; }; struct edge2 { ll from, to, cost; }; template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } // https://qiita.com/keymoon/items/11fac5627672a6d6a9f6から class Rollinghash { ll MASK30 = (1UL << 30) - 1; ll MASK31 = (1UL << 31) - 1; ll MD = (1UL << 61) - 1; ll MASK61 = MD; // a*b mod 2^61-1を返す関数(最後にModを取る) ll Mul(ll a, ll b) { ll au = a >> 31; ll ad = a & MASK31; ll bu = b >> 31; ll bd = b & MASK31; ll mid = ad * bu + au * bd; ll midu = mid >> 30; ll midd = mid & MASK30; return CalcMod(au * bu * 2 + midu + (midd << 31) + ad * bd); } // mod 2^61-1を計算する関数 ll CalcMod(ll x) { ll xu = x >> 61; ll xd = x & MASK61; ll res = xu + xd; if (res >= MD) res -= MD; return res; } ll base_() { mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count()); uniform_int_distribution<ll> rand(2, MD - 2); return rand(mt); } ll base = base_(); vl hash, power; public: Rollinghash(vl &s) { ll n = s.size(); hash.assign(n + 1, 0); power.assign(n + 1, 1); rep(i, n) { power[i + 1] = Mul(power[i], base) % MD; hash[i + 1] = (Mul(hash[i], base) + s[i]) % MD; } } ll get(int l, int r) { return (hash[r] + MD - Mul(hash[l], power[r - l]) % MD) % MD; } int LCP(int l0, int r0, int l1, int r1) { int ok = 0, ng = min(r0 - l0, r1 - l1); while (abs(ok - ng) > 1) { int rh_mid = (ok + ng) / 2; if (get(l0, l0 + rh_mid) == get(l1, l1 + rh_mid)) ok = rh_mid; else ng = rh_mid; } return ok; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); ll N; cin >> N; vl A(N), B(N); rep(i, N) { cin >> A[i]; } rep(i, N) { cin >> B[i]; } vl C(2 * N), D(N); rep(i, N) { C[i] = C[N + i] = A[i] ^ A[(i + 1) % N]; D[i] = B[i] ^ B[(i + 1) % N]; } Rollinghash rh0(C); Rollinghash rh1(D); rep(K, N) { if (rh0.get(K, K + N) == rh1.get(0, N)) { cout << K << ' ' << (A[K] ^ B[0]) << endl; } } }
//#define _GLIBCXX_DEBUG #include <bits/stdc++.h> #define FOR(i, a, b) for (ll i = a; i < b; i++) #define rep(i, n) FOR(i, 0, n) #define ROF(i, a, b) for (ll i = a; i >= b; i--) #define per(i, a) ROF(i, a, 0) #define pb push_back using namespace std; using ll = long long; using ld = long double; using ch = char; typedef pair<ll, ll> P; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<P> vP; typedef vector<ch> vc; typedef vector<vc> vvc; const ll MOD = 1000000007; const ll MOD2 = 998244353; const ld PI = acos(-1); const ll INF = 1e18; struct edge { ll to, cost; }; struct edge2 { ll from, to, cost; }; template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } // https://qiita.com/keymoon/items/11fac5627672a6d6a9f6から class Rollinghash { ll MASK30 = (1UL << 30) - 1; ll MASK31 = (1UL << 31) - 1; ll MD = (1UL << 61) - 1; ll MASK61 = MD; // a*b mod 2^61-1を返す関数(最後にModを取る) ll Mul(ll a, ll b) { ll au = a >> 31; ll ad = a & MASK31; ll bu = b >> 31; ll bd = b & MASK31; ll mid = ad * bu + au * bd; ll midu = mid >> 30; ll midd = mid & MASK30; return CalcMod(au * bu * 2 + midu + (midd << 31) + ad * bd); } // mod 2^61-1を計算する関数 ll CalcMod(ll x) { ll xu = x >> 61; ll xd = x & MASK61; ll res = xu + xd; if (res >= MD) res -= MD; return res; } ll base_() { mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count()); uniform_int_distribution<ll> rand(2, MD - 2); return rand(mt); } ll base /*=base_()*/ = MOD; vl hash, power; public: Rollinghash(vl &s) { ll n = s.size(); hash.assign(n + 1, 0); power.assign(n + 1, 1); rep(i, n) { power[i + 1] = Mul(power[i], base) % MD; hash[i + 1] = (Mul(hash[i], base) + s[i]) % MD; } } ll get(int l, int r) { return (hash[r] + MD - Mul(hash[l], power[r - l]) % MD) % MD; } int LCP(int l0, int r0, int l1, int r1) { int ok = 0, ng = min(r0 - l0, r1 - l1); while (abs(ok - ng) > 1) { int rh_mid = (ok + ng) / 2; if (get(l0, l0 + rh_mid) == get(l1, l1 + rh_mid)) ok = rh_mid; else ng = rh_mid; } return ok; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); ll N; cin >> N; vl A(N), B(N); rep(i, N) { cin >> A[i]; } rep(i, N) { cin >> B[i]; } vl C(2 * N), D(N); rep(i, N) { C[i] = C[N + i] = A[i] ^ A[(i + 1) % N]; D[i] = B[i] ^ B[(i + 1) % N]; } Rollinghash rh0(C); Rollinghash rh1(D); rep(K, N) { if (rh0.get(K, K + N) == rh1.get(0, N)) { cout << K << ' ' << (A[K] ^ B[0]) << endl; } } }
[ "call.arguments.change" ]
743,926
743,927
u328811800
cpp
p02816
#include <bits/stdc++.h> using namespace std; const int max_n = 2e5 + 5; int start_1[max_n], start_2[max_n], tab_1[max_n], tab_2[max_n], napis[3 * max_n], P[3 * max_n], n; vector<pair<int, int>> wynik; void wczytaj(int *tab, int *start) { cin >> start[0]; for (int i = 1; i < n; i++) { cin >> start[i]; tab[i - 1] = start[i] ^ start[i - 1]; } tab[n - 1] = start[n - 1] ^ start[0]; } void stworz_napis() { for (int i = 1; i <= n; i++) napis[i] = tab_1[i - 1]; napis[n + 1] = -4; for (int i = 0; i < n; i++) napis[i + n + 2] = tab_2[i]; for (int i = 0; i < n - 1; i++) napis[i + 2 * n + 2] = tab_2[i]; } void kmp() { P[1] = 0; // cout << "0 "; for (int i = 2; i <= 3 * n; i++) { int pom = i - 1; while (pom && napis[P[pom] + 1] != napis[i]) pom = P[pom]; P[i] = P[pom]; if (napis[P[pom] + 1] == napis[i]) P[i]++; if (P[i] == n) { // cout << i << "xd\n"; wynik.emplace_back((n - (i - 2 * n - 1)) % n, start_1[n - (i - 2 * n - 1)] ^ start_2[0]); } // cout << P[i] << " "; } // cout << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; wczytaj(tab_1, start_1); wczytaj(tab_2, start_2); stworz_napis(); /*for (int i = 0; i < n; i++) cout << tab_1[i] << " "; cout << "\n"; for (int i = 0; i < n; i++) cout << tab_2[i] << " "; cout << "\n"; for (int i = 1; i <= 3 * n; i++) cout << napis[i] << " "; cout << "\n"; */ kmp(); // return 0; sort(wynik.begin(), wynik.end()); for (pair<int, int> i : wynik) cout << i.first << " " << i.second << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; const int max_n = 2e5 + 5; int start_1[max_n], start_2[max_n], tab_1[max_n], tab_2[max_n], napis[3 * max_n], P[3 * max_n], n; vector<pair<int, int>> wynik; void wczytaj(int *tab, int *start) { cin >> start[0]; for (int i = 1; i < n; i++) { cin >> start[i]; tab[i - 1] = start[i] ^ start[i - 1]; } tab[n - 1] = start[n - 1] ^ start[0]; } void stworz_napis() { for (int i = 1; i <= n; i++) napis[i] = tab_1[i - 1]; napis[n + 1] = -4; for (int i = 0; i < n; i++) napis[i + n + 2] = tab_2[i]; for (int i = 0; i < n - 1; i++) napis[i + 2 * n + 2] = tab_2[i]; } void kmp() { P[1] = 0; // cout << "0 "; for (int i = 2; i <= 3 * n; i++) { int pom = i - 1; while (pom && napis[P[pom] + 1] != napis[i]) pom = P[pom]; P[i] = P[pom]; if (napis[P[pom] + 1] == napis[i]) P[i]++; if (P[i] == n) { // cout << i << "xd\n"; wynik.emplace_back((n - (i - 2 * n - 1)) % n, start_1[(n - (i - 2 * n - 1)) % n] ^ start_2[0]); // wynik.emplace_back((n - (i - 2 * n - 1)) % n, (n - (i - 2 * n - 1)) % // n); } // cout << P[i] << " "; } // cout << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; wczytaj(tab_1, start_1); wczytaj(tab_2, start_2); stworz_napis(); /*for (int i = 0; i < n; i++) cout << tab_1[i] << " "; cout << "\n"; for (int i = 0; i < n; i++) cout << tab_2[i] << " "; cout << "\n"; for (int i = 1; i <= 3 * n; i++) cout << napis[i] << " "; cout << "\n"; */ kmp(); // return 0; sort(wynik.begin(), wynik.end()); for (pair<int, int> i : wynik) cout << i.first << " " << i.second << "\n"; // cout << (start_1[0] ^ start_2[0]) << " " << (start_1[1] ^ start_2[1]) << // "\n"; return 0; }
[ "call.arguments.change" ]
743,928
743,929
u535632797
cpp
p02816
#include <bits/stdc++.h> using namespace std; // M A F I A - M A F I A - M A F I A - M A F I A - M A F I A - M A F I A // #define int long long #define lop(i, s, e) for (int i = s; i < (int)e; i++) #define rlop(i, e, s) for (int i = e; i >= s; i--) #define prvec(v) \ lop(R15, 0, v.size()) cout << v[R15] << " "; \ cout << endl; #define prarr(arr, s1, e1) \ lop(R15, s1, e1) cout << arr[R15] << " "; \ cout << endl; #define endl '\n' #define ff first #define ss second #define pii pair<int, int> #define pb push_back #define all(v) v.begin(), v.end() // M A F I A - M A F I A - M A F I A - M A F I A - M A F I A - M A F I A // const int N = 3e6 + 7, M = 1000000007, inf = INT_MAX; const long long INF = LLONG_MAX; int n, Q; int arr[N + 2], brr[N], a1[N], b1[N]; // mafia-->: hash_it #define pii pair<int, int> const int MAX_LEN = (int)2e5 + 17; // check it pii p_pwr[MAX_LEN]; int p1 = 40, p2 = 34, mod1 = (int)1e9 + 9, mod2 = (int)1e7 + 79; // --> check it int mxp = MAX_LEN - 1; auto clk = clock(); mt19937 rang(chrono::high_resolution_clock::now().time_since_epoch().count()); int rng(int l, int r) { uniform_int_distribution<int> uid(l, r); return uid(rang); } void pre() { p_pwr[0].ff = 1; for (int i = 1; i < MAX_LEN; i++) p_pwr[i].ff = (1LL * p_pwr[i - 1].ff * p1) % mod1; p_pwr[0].ss = 1; for (int i = 1; i < MAX_LEN; i++) p_pwr[i].ss = (1LL * p_pwr[i - 1].ss * p2) % mod2; } inline int add(int a, int b, int &mod) { b %= mod; int res = a + b; return res >= mod ? res - mod : res; } inline int sub(int a, int b, int &mod) { b %= mod; int res = a - b; return res < 0 ? res + mod : res; } inline int mul(int a, int b, int &mod) { b %= mod; int res = (1LL * a * b) % mod; return res; } class hash_it { public: static int cnt; int len, d; vector<pii> hashes; inline int get(int x) { return x + 1; } // check it( x-'a'+1) void init(int *s, int d1, int l) { len = l; d = d1; hashes.resize(len + 1); for (int i = 0, val1 = 0, val2 = 0; i < len; i++) { val1 = add(val1, mul(get(s[i]), p_pwr[i].ff, mod1), mod1); hashes[i].ff = val1; if (d) val2 = add(val2, mul(get(s[i]), p_pwr[i].ss, mod2), mod2); hashes[i].ss = val2; } } pii get(int i, int j) { pii v1 = {0, 0}; v1.ff = mul(sub(hashes[j].ff, (i ? hashes[i - 1].ff : 0), mod1), p_pwr[0].ff, mod1); if (d) v1.ss = mul(sub(hashes[j].ss, (i ? hashes[i - 1].ss : 0), mod2), p_pwr[0].ss, mod2); return v1; } }; void prepre() { p1 = rng(31, 100); // check range p2 = rng(31, 100); // check range if (p1 > 50) swap(mod1, mod2); mxp = MAX_LEN - 3; pre(); } void solve() { cin >> n; lop(i, 0, n) cin >> arr[i]; lop(i, 0, n) cin >> brr[i]; lop(i, 0, n) a1[i] = (arr[i] ^ arr[(i + 1) % n]); lop(i, 0, n) b1[i] = (brr[i] ^ brr[(i + 1) % n]); // prarr(a1, 0, n); // prarr(b1, 0, n); hash_it h1, h2; h1.init(a1, 0, n); h2.init(b1, 0, n); int x1 = h1.hashes[n - 1].ff; int x2 = h2.hashes[n - 1].ff; // cout << x1 << " " << x2 << endl; x1 = h1.get(0, n - 1).ff; x2 = h2.get(0, n - 1).ff; // cout << x1 << " " << x2 << endl; vector<pii> vec; if (x1 == x2) vec.pb({0, -1}); for (int i = 1; i <= n; i++) { x2 = add(x2, -mul((b1[n - i] + 1) % mod1, p_pwr[n - 1].ff, mod1), mod1); x2 = mul(x2, p1, mod1); x2 = add(x2, (b1[n - i] + 1) % mod1, mod1); // cout << x2 << " : " << b1[n - i] << endl; if (i < n and x2 == x1) vec.pb({i, -1}); } for (auto &i : vec) { int x = (arr[i.ff] ^ brr[0]); i.ss = x; } for (auto &i : vec) cout << i.ff << " " << i.ss << endl; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); prepre(); int test = 1; // cin >> test; while (test--) solve(); }
#include <bits/stdc++.h> using namespace std; // M A F I A - M A F I A - M A F I A - M A F I A - M A F I A - M A F I A // #define int long long #define lop(i, s, e) for (int i = s; i < (int)e; i++) #define rlop(i, e, s) for (int i = e; i >= s; i--) #define prvec(v) \ lop(R15, 0, v.size()) cout << v[R15] << " "; \ cout << endl; #define prarr(arr, s1, e1) \ lop(R15, s1, e1) cout << arr[R15] << " "; \ cout << endl; #define endl '\n' #define ff first #define ss second #define pii pair<int, int> #define pb push_back #define all(v) v.begin(), v.end() // M A F I A - M A F I A - M A F I A - M A F I A - M A F I A - M A F I A // const int N = 3e6 + 7, M = 1000000007, inf = INT_MAX; const long long INF = LLONG_MAX; int n, Q; int arr[N + 2], brr[N], a1[N], b1[N]; // mafia-->: hash_it #define pii pair<int, int> const int MAX_LEN = (int)2e5 + 17; // check it pii p_pwr[MAX_LEN]; int p1 = 40, p2 = 34, mod1 = (int)1e9 + 9, mod2 = (int)1e7 + 79; // --> check it int mxp = MAX_LEN - 1; auto clk = clock(); mt19937 rang(chrono::high_resolution_clock::now().time_since_epoch().count()); int rng(int l, int r) { uniform_int_distribution<int> uid(l, r); return uid(rang); } void pre() { p_pwr[0].ff = 1; for (int i = 1; i < MAX_LEN; i++) p_pwr[i].ff = (1LL * p_pwr[i - 1].ff * p1) % mod1; p_pwr[0].ss = 1; for (int i = 1; i < MAX_LEN; i++) p_pwr[i].ss = (1LL * p_pwr[i - 1].ss * p2) % mod2; } inline int add(int a, int b, int &mod) { b %= mod; int res = a + b; return res >= mod ? res - mod : res; } inline int sub(int a, int b, int &mod) { b %= mod; int res = a - b; return res < 0 ? res + mod : res; } inline int mul(int a, int b, int &mod) { b %= mod; int res = (1LL * a * b) % mod; return res; } class hash_it { public: static int cnt; int len, d; vector<pii> hashes; inline int get(int x) { return x + 1; } // check it( x-'a'+1) void init(int *s, int d1, int l) { len = l; d = d1; hashes.resize(len + 1); for (int i = 0, val1 = 0, val2 = 0; i < len; i++) { val1 = add(val1, mul(get(s[i]), p_pwr[i].ff, mod1), mod1); hashes[i].ff = val1; if (d) val2 = add(val2, mul(get(s[i]), p_pwr[i].ss, mod2), mod2); hashes[i].ss = val2; } } pii get(int i, int j) { pii v1 = {0, 0}; v1.ff = mul(sub(hashes[j].ff, (i ? hashes[i - 1].ff : 0), mod1), p_pwr[0].ff, mod1); if (d) v1.ss = mul(sub(hashes[j].ss, (i ? hashes[i - 1].ss : 0), mod2), p_pwr[0].ss, mod2); return v1; } }; void prepre() { p1 = rng(31, 100); // check range p2 = rng(31, 100); // check range if (p1 > 50) swap(mod1, mod2); mxp = MAX_LEN - 3; pre(); } void solve() { cin >> n; lop(i, 0, n) cin >> arr[i]; lop(i, 0, n) cin >> brr[i]; lop(i, 0, n) a1[i] = (arr[i] ^ arr[(i + 1) % n]); lop(i, 0, n) b1[i] = (brr[i] ^ brr[(i + 1) % n]); // prarr(a1, 0, n); // prarr(b1, 0, n); hash_it h1, h2; h1.init(a1, 0, n); h2.init(b1, 0, n); int x1 = h1.hashes[n - 1].ff; int x2 = h2.hashes[n - 1].ff; // cout << x1 << " " << x2 << endl; x1 = h1.get(0, n - 1).ff; x2 = h2.get(0, n - 1).ff; // cout << x1 << " " << x2 << endl; vector<pii> vec; if (x1 == x2) vec.pb({0, -1}); for (int i = 1; i <= n; i++) { x2 = sub(x2, mul((b1[n - i] + 1) % mod1, p_pwr[n - 1].ff, mod1), mod1); x2 = mul(x2, p1, mod1); x2 = add(x2, (b1[n - i] + 1) % mod1, mod1); // cout << x2 << " : " << b1[n - i] << endl; if (i < n and x2 == x1) vec.pb({i, -1}); } for (auto &i : vec) { int x = (arr[i.ff] ^ brr[0]); i.ss = x; } for (auto &i : vec) cout << i.ff << " " << i.ss << endl; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); prepre(); int test = 1; // cin >> test; while (test--) solve(); }
[ "assignment.value.change", "identifier.change", "call.function.change", "expression.operation.unary.arithmetic.remove", "call.arguments.change" ]
743,937
743,938
u504543992
cpp
p02816
#include <bits/stdc++.h> #define rep(i, n) for (double i = 0; i < n; ++i) using namespace std; typedef long long ll; typedef pair<double, double> P; const int INF = 1001001001; // Morris-Pratt template <typename T> struct MP { int n; T t; vector<int> a; MP() {} MP(const T &t) : t(t) { n = t.size(); a = vector<int>(n + 1); //便宜上最初に-1を入れるため(1 indexにした方が良い) a[0] = -1; int j = -1; for (int i = 0; i < n; ++i) { while (j != -1 && t[j] != t[i]) j = a[j]; //次の文字が一致するようなaを見つける j++; a[i + 1] = j; } } int operator[](int i) { return a[i]; } //添字アクセスできるように vector<int> findAll(const T &s) { vector<int> res; int j = 0; for (int i = 0; i < s.size(); ++i) { while (j != -1 && t[j] != s[i]) j = a[j]; j++; if (j == n) { res.push_back(i - j + 1); j = a[j]; } } return res; } }; int main() { int n; cin >> n; vector<int> a(n); vector<int> b(n); vector<int> a1(n); vector<int> b1(n); rep(i, n) cin >> a[i]; rep(i, n) cin >> b[i]; rep(i, n - 1) { a1[i + 1] = a[i] ^ a[i + 1]; } rep(i, n - 1) { b1[i + 1] = b[i] ^ b[i + 1]; } a1[0] = a[0] ^ a[n - 1]; b1[0] = b[0] ^ b[n - 1]; vector<int> A = a1; A.insert(A.end(), A.begin(), A.end()); // rep(i,A.size())cout<<A[i]; MP<vector<int>> mp(b1); vector<int> res = mp.findAll(A); rep(i, res.size()) { if (i >= n) continue; int now = res[i]; int x = b[0] ^ a[now]; cout << res[i] << " " << x << endl; } }
#include <bits/stdc++.h> #define rep(i, n) for (double i = 0; i < n; ++i) using namespace std; typedef long long ll; typedef pair<double, double> P; const int INF = 1001001001; // Morris-Pratt template <typename T> struct MP { int n; T t; vector<int> a; MP() {} MP(const T &t) : t(t) { n = t.size(); a = vector<int>(n + 1); //便宜上最初に-1を入れるため(1 indexにした方が良い) a[0] = -1; int j = -1; for (int i = 0; i < n; ++i) { while (j != -1 && t[j] != t[i]) j = a[j]; //次の文字が一致するようなaを見つける j++; a[i + 1] = j; } } int operator[](int i) { return a[i]; } //添字アクセスできるように vector<int> findAll(const T &s) { vector<int> res; int j = 0; for (int i = 0; i < s.size(); ++i) { while (j != -1 && t[j] != s[i]) j = a[j]; j++; if (j == n) { res.push_back(i - j + 1); j = a[j]; } } return res; } }; int main() { int n; cin >> n; vector<int> a(n); vector<int> b(n); vector<int> a1(n); vector<int> b1(n); rep(i, n) cin >> a[i]; rep(i, n) cin >> b[i]; rep(i, n - 1) { a1[i + 1] = a[i] ^ a[i + 1]; } rep(i, n - 1) { b1[i + 1] = b[i] ^ b[i + 1]; } a1[0] = a[0] ^ a[n - 1]; b1[0] = b[0] ^ b[n - 1]; vector<int> A = a1; A.insert(A.end(), A.begin(), A.end()); // rep(i,A.size())cout<<A[i]; MP<vector<int>> mp(b1); vector<int> res = mp.findAll(A); rep(i, res.size()) { if (res[i] >= n) continue; int now = res[i]; int x = b[0] ^ a[now]; cout << res[i] << " " << x << endl; } }
[ "control_flow.branch.if.condition.change" ]
743,944
743,945
u289381123
cpp
p02816
#include <bits/stdc++.h> #define rep(i, n) for (ll i = 0; i < n; ++i) using namespace std; using ll = long long; using ull = long long unsigned; using P = pair<ll, ll>; vector<int> f(vector<int> &a) { int n = a.size(); vector<int> res(n); rep(i, n) res[i] = a[i] ^ a[(i + 1) % n]; return res; } // Morris-Pratt template <typename T> struct MP { int n; T t; vector<int> a; MP() {} MP(const T &t) : t(t) { n = t.size(); a = vector<int>(n + 1); a[0] = -1; int j = -1; for (int i = 0; i < n; ++i) { while (j != -1 && t[j] != t[i]) j = a[j]; j++; a[i + 1] = j; } } int operator[](int i) { return a[i]; } vector<int> findAll(const T &s) { vector<int> res; int j = 0; for (int i = 0; i < s.size(); ++i) { while (j != -1 && t[j] != s[i]) j = a[j]; j++; if (j == n) { res.push_back(i - j + 1); j = a[j]; } } return res; } }; int main() { ll n; cin >> n; vector<ll> a(n); vector<ll> b(n); vector<ll> xa(n); vector<ll> xb(n); rep(i, n) { cin >> a[i]; } rep(i, n) { cin >> b[i]; } rep(i, n - 1) { ll temp = a[i] ^ a[i + 1]; xa[i] = temp; } xa[n - 1] = a[0] ^ a[n - 1]; rep(i, n - 1) { i %= n; ll temp = b[i] ^ b[i + 1]; xb[i] = temp; } xb[n - 1] = b[0] ^ b[n - 1]; xb.insert(xb.end(), xb.begin(), xb.end()); MP<vector<ll>> mp(xa); vector<int> FA; FA = mp.findAll(xb); // rep(i,xa.size())cout<<xa[i]<<endl; // cout<<endl; // rep(i,xb.size())cout<<xb[i]<<endl; // rep(i,FA.size())cout<<FA[i]<<endl; vector<ll> K; rep(i, FA.size()) K.push_back(n - i); sort(K.begin(), K.end()); rep(i, K.size()) { if (K[i] >= n) continue; ll k = K[i]; ll xnow = a[k] ^ b[0]; cout << k << ' ' << xnow << endl; } }
#include <bits/stdc++.h> #define rep(i, n) for (ll i = 0; i < n; ++i) using namespace std; using ll = long long; using ull = long long unsigned; using P = pair<ll, ll>; vector<int> f(vector<int> &a) { int n = a.size(); vector<int> res(n); rep(i, n) res[i] = a[i] ^ a[(i + 1) % n]; return res; } // Morris-Pratt template <typename T> struct MP { int n; T t; vector<int> a; MP() {} MP(const T &t) : t(t) { n = t.size(); a = vector<int>(n + 1); a[0] = -1; int j = -1; for (int i = 0; i < n; ++i) { while (j != -1 && t[j] != t[i]) j = a[j]; j++; a[i + 1] = j; } } int operator[](int i) { return a[i]; } vector<int> findAll(const T &s) { vector<int> res; int j = 0; for (int i = 0; i < s.size(); ++i) { while (j != -1 && t[j] != s[i]) j = a[j]; j++; if (j == n) { res.push_back(i - j + 1); j = a[j]; } } return res; } }; int main() { ll n; cin >> n; vector<ll> a(n); vector<ll> b(n); vector<ll> xa(n); vector<ll> xb(n); rep(i, n) { cin >> a[i]; } rep(i, n) { cin >> b[i]; } rep(i, n - 1) { ll temp = a[i] ^ a[i + 1]; xa[i] = temp; } xa[n - 1] = a[0] ^ a[n - 1]; rep(i, n - 1) { i %= n; ll temp = b[i] ^ b[i + 1]; xb[i] = temp; } xb[n - 1] = b[0] ^ b[n - 1]; xb.insert(xb.end(), xb.begin(), xb.end()); MP<vector<ll>> mp(xa); vector<int> FA; FA = mp.findAll(xb); // rep(i,xa.size())cout<<xa[i]<<endl; // cout<<endl; // rep(i,xb.size())cout<<xb[i]<<endl; // rep(i,FA.size())cout<<FA[i]<<endl; vector<ll> K; rep(i, FA.size()) K.push_back(n - FA[i]); sort(K.begin(), K.end()); rep(i, K.size()) { if (K[i] >= n) continue; ll k = K[i]; ll xnow = a[k] ^ b[0]; cout << k << ' ' << xnow << endl; } }
[ "call.arguments.change" ]
743,946
743,947
u289381123
cpp
p02816
#include <algorithm> #include <iostream> #include <map> #include <set> int const MAXN = 2e5; int a[MAXN], b[MAXN]; int c[MAXN - 1], d[MAXN + MAXN - 2]; int next_pos[MAXN]; int main() { int N; std::cin >> N; for (int i = 0; i < N; ++i) std::cin >> a[i]; for (int i = 0; i < N; ++i) std::cin >> b[i]; for (int i = 0; i < N - 1; ++i) c[i] = a[i] ^ a[i + 1]; for (int i = 0; i < 2 * N - 2; ++i) d[i] = b[i % N] ^ b[(i + 1) % N]; std::set<int> ans; // KMP int i = 1, j = 0; next_pos[1] = 0; while (i < N - 1) { if (c[i] == c[j]) next_pos[++i] = ++j; else if (j == 0) next_pos[++i] = j; else j = next_pos[j]; } i = j = 0; while (i < 2 * N - 2 && j < N - 1) { if (d[i] == c[j]) { ++i; ++j; } else if (j == 0) ++i; else j = next_pos[j]; if (j == N - 1) { ans.insert((N + N - 1 - i) % N); --i; --j; j = next_pos[j]; } } // ans for (auto k : ans) { int x = a[0] ^ b[N - k]; std::cout << k << " " << x << std::endl; } return 0; }
#include <algorithm> #include <iostream> #include <map> #include <set> int const MAXN = 2e5; int a[MAXN], b[MAXN]; int c[MAXN - 1], d[MAXN + MAXN - 2]; int next_pos[MAXN]; int main() { int N; std::cin >> N; for (int i = 0; i < N; ++i) std::cin >> a[i]; for (int i = 0; i < N; ++i) std::cin >> b[i]; for (int i = 0; i < N - 1; ++i) c[i] = a[i] ^ a[i + 1]; for (int i = 0; i < 2 * N - 2; ++i) d[i] = b[i % N] ^ b[(i + 1) % N]; std::set<int> ans; // KMP int i = 1, j = 0; next_pos[1] = 0; while (i < N - 1) { if (c[i] == c[j]) next_pos[++i] = ++j; else if (j == 0) next_pos[++i] = j; else j = next_pos[j]; } i = j = 0; while (i < 2 * N - 2 && j < N - 1) { if (d[i] == c[j]) { ++i; ++j; } else if (j == 0) ++i; else j = next_pos[j]; if (j == N - 1) { ans.insert((N + N - 1 - i) % N); --i; --j; j = next_pos[j]; } } // ans for (auto k : ans) { int x = a[0] ^ b[(N - k) % N]; std::cout << k << " " << x << std::endl; } return 0; }
[ "assignment.change" ]
743,965
743,966
u294638337
cpp
p02816
#include <bits/stdc++.h> using namespace std; using lint = long long int; using pint = pair<int, int>; using plint = pair<lint, lint>; struct fast_ios { fast_ios() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((lint)(x).size()) #define POW2(n) (1LL << (n)) #define FOR(i, begin, end) \ for (int i = (begin), i##_end_ = (end); i < i##_end_; i++) #define IFOR(i, begin, end) \ for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--) #define REP(i, n) FOR(i, 0, n) #define IREP(i, n) IFOR(i, 0, n) template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; } template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ","; os << "]"; return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_set<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) { os << "(" << pa.first << "," << pa.second << ")"; return os; } template <typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp) { os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; } template <typename TK, typename TV> ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp) { os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; } template <typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); } template <typename T, typename... Args> void ndarray(vector<T> &vec, int len, Args... args) { vec.resize(len); for (auto &v : vec) ndarray(v, args...); } template <typename T> bool mmax(T &m, const T q) { if (m < q) { m = q; return true; } else return false; } template <typename T> bool mmin(T &m, const T q) { if (m > q) { m = q; return true; } else return false; } template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); } template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); } #define dbg(x) \ cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl; /* #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tag_and_trait.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; // find_by_order(), order_of_key() template<typename TK> using pbds_set = tree<TK, null_type, less<TK>, rb_tree_tag, tree_order_statistics_node_update>; template<typename TK, typename TV> using pbds_map = tree<TK, TV, less<TK>, rb_tree_tag, tree_order_statistics_node_update>; */ vector<int> z_algorithm(const vector<int> &s) { vector<int> ans(s.size()); ans[0] = s.size(); int i = 1, j = 0; while (i < (int)s.size()) { while (i + j < (int)s.size() && s[j] == s[i + j]) ++j; ans[i] = j; if (!j) { ++i; continue; } int k = 1; while (i + k < (int)s.size() && k + ans[k] < j) ans[i + k] = ans[k], ++k; i += k; j -= k; } return ans; } int main() { int N; cin >> N; vector<int> a(N), b(N); cin >> a >> b; vector<int> xx(N * 3 - 1); xx[N - 1] = -1; REP(i, N - 1) xx[i] = b[i] ^ b[i + 1]; REP(i, 2 * N) xx[i + N] = a[i % N] ^ a[(i + 1) % N]; vector<int> z = z_algorithm(xx); REP(i, N) { if (z[N + i] >= N - 1) { printf("%d %d\n", i, a[i] ^ b[0]); } } }
#include <bits/stdc++.h> using namespace std; using lint = long long int; using pint = pair<int, int>; using plint = pair<lint, lint>; struct fast_ios { fast_ios() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((lint)(x).size()) #define POW2(n) (1LL << (n)) #define FOR(i, begin, end) \ for (int i = (begin), i##_end_ = (end); i < i##_end_; i++) #define IFOR(i, begin, end) \ for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--) #define REP(i, n) FOR(i, 0, n) #define IREP(i, n) IFOR(i, 0, n) template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; } template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ","; os << "]"; return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_set<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) { os << "(" << pa.first << "," << pa.second << ")"; return os; } template <typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp) { os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; } template <typename TK, typename TV> ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp) { os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; } template <typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); } template <typename T, typename... Args> void ndarray(vector<T> &vec, int len, Args... args) { vec.resize(len); for (auto &v : vec) ndarray(v, args...); } template <typename T> bool mmax(T &m, const T q) { if (m < q) { m = q; return true; } else return false; } template <typename T> bool mmin(T &m, const T q) { if (m > q) { m = q; return true; } else return false; } template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); } template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); } #define dbg(x) \ cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl; /* #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tag_and_trait.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; // find_by_order(), order_of_key() template<typename TK> using pbds_set = tree<TK, null_type, less<TK>, rb_tree_tag, tree_order_statistics_node_update>; template<typename TK, typename TV> using pbds_map = tree<TK, TV, less<TK>, rb_tree_tag, tree_order_statistics_node_update>; */ vector<int> z_algorithm(const vector<int> &s) { vector<int> ans(s.size()); ans[0] = s.size(); int i = 1, j = 0; while (i < (int)s.size()) { while (i + j < (int)s.size() && s[j] == s[i + j]) ++j; ans[i] = j; if (!j) { ++i; continue; } int k = 1; while (i + k < (int)s.size() && k + ans[k] < j) ans[i + k] = ans[k], ++k; i += k; j -= k; } return ans; } int main() { int N; cin >> N; vector<int> a(N), b(N); cin >> a >> b; vector<int> xx(N * 3); xx[N - 1] = -1; REP(i, N - 1) xx[i] = b[i] ^ b[i + 1]; REP(i, 2 * N) xx[i + N] = a[i % N] ^ a[(i + 1) % N]; vector<int> z = z_algorithm(xx); REP(i, N) { if (z[N + i] >= N - 1) { printf("%d %d\n", i, a[i] ^ b[0]); } } }
[ "expression.operation.binary.remove" ]
743,967
743,968
u478275206
cpp
p02816
#include <bits/stdc++.h> #define ll long long #define ull unsigned long long #define MP make_pair #define PII pair<int, int> #define PLL pair<ll, ll> #define PLI pair<ll, int> #define PIL pair<int, ll> #define STR string #define Fst first #define Snd second #define SHN 1e9 #define LSHN 1e18 using namespace std; int n; int a[200010], b[200010]; int ai[200010], bi[200010]; int z[600010], res[600010]; vector<PII> v; void zalgor() { int l = 0, r = 0; for (int i = 1; i < 3 * n; i++) { if (i > r) { while (i + res[i] < 3 * n && z[i + res[i]] == z[res[i]]) res[i]++; l = i, r = i + res[i] - 1; } else { res[i] = min(r - i + 1, res[i - l]); while (i + res[i] < 3 * n && z[i + res[i]] == z[res[i]]) res[i]++; if (i + res[i] - 1 > r) l = i, r = i + res[i] - 1; } } } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); if (i) ai[i - 1] = a[i] ^ a[i - 1]; } ai[n - 1] = a[n - 1] ^ a[0]; for (int i = 0; i < n; i++) { scanf("%d", &b[i]); if (i) bi[i - 1] = b[i] ^ b[i - 1]; } bi[n - 1] = b[n - 1] ^ b[0]; for (int i = 0; i < n; i++) z[i] = bi[i]; for (int i = n; i < 2 * n; i++) z[i] = ai[i - n]; for (int i = 2 * n; i < 3 * n; i++) z[i] = ai[i - 2 * n]; /*for (int i=0;i<n;i++) cout <<ai[i]<<" "; cout <<"\n"; for (int i=0;i<n;i++) cout <<bi[i]<<" "; cout <<"\n"; zalgor(); for (int i=0;i<3*n;i++) cout <<z[i]<<" "; cout <<"\n"; for (int i=0;i<3*n;i++) cout <<res[n+i]<<" "; cout <<"\n";*/ for (int i = 0; i < n; i++) { if (res[n + i] >= n) { int t = (i < n ? i : i - n); v.push_back(MP(t, a[t] ^ b[0])); } } for (int i = 0; i < v.size(); i++) printf("%d %d\n", v[i].Fst, v[i].Snd); return 0; }
#include <bits/stdc++.h> #define ll long long #define ull unsigned long long #define MP make_pair #define PII pair<int, int> #define PLL pair<ll, ll> #define PLI pair<ll, int> #define PIL pair<int, ll> #define STR string #define Fst first #define Snd second #define SHN 1e9 #define LSHN 1e18 using namespace std; int n; int a[200010], b[200010]; int ai[200010], bi[200010]; int z[600010], res[600010]; vector<PII> v; void zalgor() { int l = 0, r = 0; for (int i = 1; i < 3 * n; i++) { if (i > r) { while (i + res[i] < 3 * n && z[i + res[i]] == z[res[i]]) res[i]++; l = i, r = i + res[i] - 1; } else { res[i] = min(r - i + 1, res[i - l]); while (i + res[i] < 3 * n && z[i + res[i]] == z[res[i]]) res[i]++; if (i + res[i] - 1 > r) l = i, r = i + res[i] - 1; } } } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); if (i) ai[i - 1] = a[i] ^ a[i - 1]; } ai[n - 1] = a[n - 1] ^ a[0]; for (int i = 0; i < n; i++) { scanf("%d", &b[i]); if (i) bi[i - 1] = b[i] ^ b[i - 1]; } bi[n - 1] = b[n - 1] ^ b[0]; for (int i = 0; i < n; i++) z[i] = bi[i]; for (int i = n; i < 2 * n; i++) z[i] = ai[i - n]; for (int i = 2 * n; i < 3 * n; i++) z[i] = ai[i - 2 * n]; zalgor(); /*for (int i=0;i<n;i++) cout <<ai[i]<<" "; cout <<"\n"; for (int i=0;i<n;i++) cout <<bi[i]<<" "; cout <<"\n"; for (int i=0;i<3*n;i++) cout <<z[i]<<" "; cout <<"\n"; for (int i=0;i<3*n;i++) cout <<res[n+i]<<" "; cout <<"\n";*/ for (int i = 0; i < n; i++) { if (res[n + i] >= n) { int t = (i < n ? i : i - n); v.push_back(MP(t, a[t] ^ b[0])); } } for (int i = 0; i < v.size(); i++) printf("%d %d\n", v[i].Fst, v[i].Snd); return 0; }
[ "call.add" ]
743,973
743,974
u152424169
cpp
p02816
#include <bits/stdc++.h> using namespace std; const long long md = 2038077799; long long n, a[202020], b[202020], aa[202020], bb[202020], ha, hb, bs = 1400312813; vector<long long> v; long long pw(long long x, long long y) { long long rt = 1; while (y) { if (y & 1) { rt *= x; rt %= md; } y >>= 1; x *= x; x %= md; } return rt; } int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { cin >> b[i]; } for (int i = 0; i < n; i++) { aa[i] = a[(i + 1) % n] ^ a[i]; bb[i] = b[(i + 1) % n] ^ b[i]; } for (int i = 0; i < n; i++) { ha *= bs; ha %= md; ha += aa[i]; ha %= md; } for (int i = 0; i < n; i++) { hb *= bs; hb %= md; hb += bb[i]; hb %= md; } long long nw = pw(bs, n - 1); for (int i = 0; i < n; i++) { if (ha == hb) { v.push_back(i); } long long vl = nw * aa[i]; vl %= md; ha += md - vl; ha %= md; ha *= bs; ha %= md; ha += aa[i]; ha %= md; } for (int i = 0; i < v.size(); i++) { cout << v[i] << ' ' << (a[0] ^ (b[n - v[i]])) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const long long md = 2038077799; long long n, a[202020], b[202020], aa[202020], bb[202020], ha, hb, bs = 1400312813; vector<long long> v; long long pw(long long x, long long y) { long long rt = 1; while (y) { if (y & 1) { rt *= x; rt %= md; } y >>= 1; x *= x; x %= md; } return rt; } int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { cin >> b[i]; } for (int i = 0; i < n; i++) { aa[i] = a[(i + 1) % n] ^ a[i]; bb[i] = b[(i + 1) % n] ^ b[i]; } for (int i = 0; i < n; i++) { ha *= bs; ha %= md; ha += aa[i]; ha %= md; } for (int i = 0; i < n; i++) { hb *= bs; hb %= md; hb += bb[i]; hb %= md; } long long nw = pw(bs, n - 1); for (int i = 0; i < n; i++) { if (ha == hb) { v.push_back(i); } long long vl = nw * aa[i]; vl %= md; ha += md - vl; ha %= md; ha *= bs; ha %= md; ha += aa[i]; ha %= md; } for (int i = 0; i < v.size(); i++) { cout << v[i] << ' ' << (a[0] ^ (b[(n - v[i]) % n])) << endl; } return 0; }
[ "expression.operation.binary.add" ]
743,975
743,976
u386107860
cpp
p02816
#include <bits/stdc++.h> using namespace std; long long n, a[200003], b[200003], x[200003], y[200003], pw[200003]; const long long base = 1100000000, mod = 2147483647; bool check(int i) { long long vala = (i == 0 ? 0 : x[i - 1]) + (x[n - 1] - ((i == 0) ? 0 : x[i - 1] * pw[n - i])) * pw[i], valb = y[n - 1]; vala = (vala % mod + mod) % mod; return vala == valb; } int main() { scanf("%lld", &n); for (int i = 0; i < n; i++) scanf("%lld", a + i); for (int i = 0; i < n; i++) scanf("%lld", b + i); for (int i = 0; i < n; i++) x[i] = a[i % n] ^ a[(i + 1) % n]; for (int i = 0; i < n; i++) y[i] = b[i % n] ^ b[(i + 1) % n]; for (int i = 1; i < n; i++) x[i] = (x[i - 1] * base + x[i]) % mod; for (int i = 1; i < n; i++) y[i] = (y[i - 1] * base + y[i]) % mod; pw[0] = 1; for (int i = 1; i <= n; i++) pw[i] = pw[i - 1] * base % mod; for (int i = 0; i < n; i++) if (check(i)) cout << i << ' ' << (a[i] ^ b[0]) << endl; }
#include <bits/stdc++.h> using namespace std; long long n, a[200003], b[200003], x[200003], y[200003], pw[200003]; const long long base = 1100000000, mod = 2147483647; bool check(int i) { long long vala = (i == 0 ? 0 : x[i - 1]) + (x[n - 1] - ((i == 0) ? 0 : x[i - 1] * pw[n - i] % mod)) * pw[i], valb = y[n - 1]; vala = (vala % mod + mod) % mod; return vala == valb; } int main() { scanf("%lld", &n); for (int i = 0; i < n; i++) scanf("%lld", a + i); for (int i = 0; i < n; i++) scanf("%lld", b + i); for (int i = 0; i < n; i++) x[i] = a[i % n] ^ a[(i + 1) % n]; for (int i = 0; i < n; i++) y[i] = b[i % n] ^ b[(i + 1) % n]; for (int i = 1; i < n; i++) x[i] = (x[i - 1] * base + x[i]) % mod; for (int i = 1; i < n; i++) y[i] = (y[i - 1] * base + y[i]) % mod; pw[0] = 1; for (int i = 1; i <= n; i++) pw[i] = pw[i - 1] * base % mod; for (int i = 0; i < n; i++) if (check(i)) cout << i << ' ' << (a[i] ^ b[0]) << endl; }
[ "assignment.change" ]
743,977
743,978
u123270724
cpp
p02816
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < n; i++) #define REPR(i, n) for (int i = n; i >= 0; i--) #define FOR(i, m, n) for (int i = m; i < n; i++) #define FORR(i, m, n) for (int i = m; i >= n; i--) #define SORT(v, n) sort(v, v + n); #define VSORT(v) sort(v.begin(), v.end()); #define VRSORT(v) sort(v.begin(), v.end(), [](int a, int b) { return a > b; }); #define ALL(X) (X).begin(), (X).end() #define INF 999999999 #define M_PI 3.14159265358979323846 #define MOD 1000000007 #ifdef __LOCAL #define DBG(X) cout << #X << " = " << (X) << endl; #define SAY(X) cout << (X) << endl; #else #define DBG(X) #define SAY(X) #endif #ifdef __LOCAL #include <filesystem> namespace fs = std::filesystem; #endif using namespace std; using ll = long long int; using ull = unsigned long long int; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; int dx[] = {1, 0, -1, 0, 0}; int dy[] = {0, 1, 0, -1, 0}; const int INT_INF = (int)(2e9); const ll LL_INF = (ll)(2e18); const int NIL = -1; static mt19937 _g(time(nullptr)); std::string pad(int num) { char buffer[4]; std::snprintf(buffer, sizeof(buffer), "%03d", num); return buffer; } inline ll randint(ll a, ll b) { ll w = (_g() << 31LL) ^ _g(); return a + w % (b - a + 1); } inline void fast_io() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); }; template <typename T> inline T sign(T x) { return T(x > 0) - T(x < 0); } template <typename T, typename S> inline ostream &operator<<(ostream &os, const pair<T, S> p) { cout << "[" << p.first << ";" << p.second << "]"; return os; } template <typename T> inline ostream &operator<<(ostream &os, const vector<T> &v) { for (auto el : v) cout << el << " "; return os; } template <typename T> inline ostream &operator<<(ostream &os, const deque<T> &v) { for (auto el : v) cout << el << " "; return os; } template <typename T> inline T fetch() { T ret; cin >> ret; return ret; } template <typename T> inline vector<T> fetch_vec(int sz) { vector<T> ret(sz); for (auto &elem : ret) cin >> elem; return ret; } int N; // deque<ll> A,B; vector<ll> A, B; void input() { #ifdef __LOCAL fs::path p = __FILE__; fs::path input, output; input = output = p.parent_path(); input += string("/input/") + string(p.stem()) + string(".txt"); output += string("/output/") + string(p.stem()) + string(".txt"); freopen(input.c_str(), "r", stdin); freopen(output.c_str(), "w", stdout); #endif cin >> N; REP(_, N) { ll a; cin >> a; A.push_back(a); } REP(_, N) { ll b; cin >> b; B.push_back(b); } } // deque<ll> next_deq(deque<ll>& X){ // ll x = X.front(); // X.pop_front(); // X.push_back(x); // return X; // } // bool isAllXorSame(deque<ll> A,deque<ll> B){ // ll x = A[0]^B[0]; // for(int i=1;i<N;i++){ // ll tmp = x; // x = A[i]^B[i]; // if(tmp!=x) return false; // } // return true; // } // int mysolve(){ // ll ALLX = 0LL; // for(auto a:A) ALLX ^= a; // for(auto b:B) ALLX ^= b; // if((N%2==0)&(ALLX!=0)) return 0; // REP(k,N){ // if(isAllXorSame(A,B)){ // cout << k << " " << (A[0]^B[0]) << "\n"; // } // next_deq(A); // } // return 0; // } template <typename T> vector<int> z_algorithm(const T &s) { vector<int> ans(s.size()); ans[0] = s.size(); int i = 1, j = 0; while (i < (int)s.size()) { while (i + j < (int)s.size() && s[j] == s[i + j]) ++j; ans[i] = j; if (!j) { ++i; continue; } int k = 1; while (i + k < (int)s.size() && k + ans[k] < j) ans[i + k] = ans[k], ++k; i += k; j -= k; } return ans; } int solve() { vector<ll> zalg; for (int i = 0; i < N; i++) { zalg.push_back(B[i] ^ B[(i + 1) % N]); } for (int i = 0; i < N; i++) { zalg.push_back(A[i] ^ A[(i + 1) % N]); } for (int i = 0; i < N; i++) { zalg.push_back(A[i] ^ A[(i + 1) % N]); } auto zz = z_algorithm(zalg); vector<ll> res; for (int i = N; i < 2 * N; i++) { if (zz[i] == N) { ll k = i - N; res.push_back(k); } } for (auto k : res) { ll x = A[k] ^ B[0]; cout << k << " " << x << endl; } return 0; } int main() { input(); solve(); // vector<int> T={1,2,2,3,1,5,4,2,1,1,2,2,3,2,1,1,3}; // string // s="adfasdfewoiralkjfdklsjflkajsdklfjslkadjfoiwnmef,kndsklnfkjashbndvdkjahslkfjhlkjawe"; // DBG(z_algorithm(T)); // DBG(z_algorithm(s)); return 0; }
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < n; i++) #define REPR(i, n) for (int i = n; i >= 0; i--) #define FOR(i, m, n) for (int i = m; i < n; i++) #define FORR(i, m, n) for (int i = m; i >= n; i--) #define SORT(v, n) sort(v, v + n); #define VSORT(v) sort(v.begin(), v.end()); #define VRSORT(v) sort(v.begin(), v.end(), [](int a, int b) { return a > b; }); #define ALL(X) (X).begin(), (X).end() #define INF 999999999 #define M_PI 3.14159265358979323846 #define MOD 1000000007 #ifdef __LOCAL #define DBG(X) cout << #X << " = " << (X) << endl; #define SAY(X) cout << (X) << endl; #else #define DBG(X) #define SAY(X) #endif #ifdef __LOCAL #include <filesystem> namespace fs = std::filesystem; #endif using namespace std; using ll = long long int; using ull = unsigned long long int; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; int dx[] = {1, 0, -1, 0, 0}; int dy[] = {0, 1, 0, -1, 0}; const int INT_INF = (int)(2e9); const ll LL_INF = (ll)(2e18); const int NIL = -1; static mt19937 _g(time(nullptr)); std::string pad(int num) { char buffer[4]; std::snprintf(buffer, sizeof(buffer), "%03d", num); return buffer; } inline ll randint(ll a, ll b) { ll w = (_g() << 31LL) ^ _g(); return a + w % (b - a + 1); } inline void fast_io() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); }; template <typename T> inline T sign(T x) { return T(x > 0) - T(x < 0); } template <typename T, typename S> inline ostream &operator<<(ostream &os, const pair<T, S> p) { cout << "[" << p.first << ";" << p.second << "]"; return os; } template <typename T> inline ostream &operator<<(ostream &os, const vector<T> &v) { for (auto el : v) cout << el << " "; return os; } template <typename T> inline ostream &operator<<(ostream &os, const deque<T> &v) { for (auto el : v) cout << el << " "; return os; } template <typename T> inline T fetch() { T ret; cin >> ret; return ret; } template <typename T> inline vector<T> fetch_vec(int sz) { vector<T> ret(sz); for (auto &elem : ret) cin >> elem; return ret; } int N; // deque<ll> A,B; vector<ll> A, B; void input() { #ifdef __LOCAL fs::path p = __FILE__; fs::path input, output; input = output = p.parent_path(); input += string("/input/") + string(p.stem()) + string(".txt"); output += string("/output/") + string(p.stem()) + string(".txt"); freopen(input.c_str(), "r", stdin); freopen(output.c_str(), "w", stdout); #endif cin >> N; REP(_, N) { ll a; cin >> a; A.push_back(a); } REP(_, N) { ll b; cin >> b; B.push_back(b); } } // deque<ll> next_deq(deque<ll>& X){ // ll x = X.front(); // X.pop_front(); // X.push_back(x); // return X; // } // bool isAllXorSame(deque<ll> A,deque<ll> B){ // ll x = A[0]^B[0]; // for(int i=1;i<N;i++){ // ll tmp = x; // x = A[i]^B[i]; // if(tmp!=x) return false; // } // return true; // } // int mysolve(){ // ll ALLX = 0LL; // for(auto a:A) ALLX ^= a; // for(auto b:B) ALLX ^= b; // if((N%2==0)&(ALLX!=0)) return 0; // REP(k,N){ // if(isAllXorSame(A,B)){ // cout << k << " " << (A[0]^B[0]) << "\n"; // } // next_deq(A); // } // return 0; // } template <typename T> vector<int> z_algorithm(const T &s) { vector<int> ans(s.size()); ans[0] = s.size(); int i = 1, j = 0; while (i < (int)s.size()) { while (i + j < (int)s.size() && s[j] == s[i + j]) ++j; ans[i] = j; if (!j) { ++i; continue; } int k = 1; while (i + k < (int)s.size() && k + ans[k] < j) ans[i + k] = ans[k], ++k; i += k; j -= k; } return ans; } int solve() { vector<ll> zalg; for (int i = 0; i < N; i++) { zalg.push_back(B[i] ^ B[(i + 1) % N]); } for (int i = 0; i < N; i++) { zalg.push_back(A[i] ^ A[(i + 1) % N]); } for (int i = 0; i < N; i++) { zalg.push_back(A[i] ^ A[(i + 1) % N]); } auto zz = z_algorithm(zalg); vector<ll> res; for (int i = N; i < 2 * N; i++) { if (zz[i] >= N) { ll k = i - N; res.push_back(k); } } for (auto k : res) { ll x = A[k] ^ B[0]; cout << k << " " << x << endl; } return 0; } int main() { input(); solve(); // vector<int> T={1,2,2,3,1,5,4,2,1,1,2,2,3,2,1,1,3}; // string // s="adfasdfewoiralkjfdklsjflkajsdklfjslkadjfoiwnmef,kndsklnfkjashbndvdkjahslkfjhlkjawe"; // DBG(z_algorithm(T)); // DBG(z_algorithm(s)); return 0; }
[ "expression.operator.compare.change", "control_flow.branch.if.condition.change" ]
743,983
743,984
u858670323
cpp
p02816
#include <bits/stdc++.h> using namespace std; using int64 = long long; constexpr int DEBUG = 0; // Verified: ABC135F vector<int> FindAllByRollingHash(const vector<int64> &s, const vector<int64> &key) { if (s.size() < key.size()) return {}; constexpr int64 P1 = 1000000007; constexpr int64 P2 = 999999937; constexpr int64 B = 257; int m = key.size(); int64 bm1 = 1; int64 bm2 = 1; for (int i = 0; i < m; i++) { bm1 = bm1 * B % P1; bm2 = bm2 * B % P2; } int64 kh1 = 0; int64 kh2 = 0; for (int i = 0; i < m; i++) { kh1 = (kh1 * B + key[i]) % P1; kh2 = (kh2 * B + key[i]) % P2; } vector<int> positions; int64 h1 = 0; int64 h2 = 0; for (int i = 0; i < m; i++) { h1 = (h1 * B + s[i]) % P1; h2 = (h2 * B + s[i]) % P2; } if (h1 == kh1 && h2 == kh2) { positions.push_back(0); } for (int i = 1; i + m <= s.size(); i++) { h1 = (h1 * B + s[i + m - 1]) % P1; h1 = (h1 + P1 - (bm1 * s[i - 1] % P1)) % P1; h2 = (h2 * B + s[i + m - 1]) % P2; h2 = (h2 + P2 - (bm2 * s[i - 1] % P2)) % P2; if (h1 == kh1 && h2 == kh2) { positions.push_back(i); } } return positions; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int64> xs(n); vector<int64> ys(n); for (int i = 0; i < n; i++) { cin >> xs[i]; } for (int i = 0; i < n; i++) { cin >> ys[i]; } vector<int64> as(2 * n); vector<int64> bs(n); for (int i = 0; i < 2 * n; i++) { as[i] = xs[(i + 1) % n] ^ xs[i % n]; } for (int i = 0; i < n; i++) { bs[i] = ys[(i + 1) % n] ^ ys[i % n]; } auto ps = FindAllByRollingHash(as, bs); for (int p : ps) { cout << p << " " << (xs[p] ^ ys[0]) << "\n"; } }
#include <bits/stdc++.h> using namespace std; using int64 = long long; constexpr int DEBUG = 0; // Verified: ABC135F vector<int> FindAllByRollingHash(const vector<int64> &s, const vector<int64> &key) { if (s.size() < key.size()) return {}; constexpr int64 P1 = 1000000007; constexpr int64 P2 = 999999937; constexpr int64 B = 257; int m = key.size(); int64 bm1 = 1; int64 bm2 = 1; for (int i = 0; i < m; i++) { bm1 = bm1 * B % P1; bm2 = bm2 * B % P2; } int64 kh1 = 0; int64 kh2 = 0; for (int i = 0; i < m; i++) { kh1 = (kh1 * B + key[i]) % P1; kh2 = (kh2 * B + key[i]) % P2; } vector<int> positions; int64 h1 = 0; int64 h2 = 0; for (int i = 0; i < m; i++) { h1 = (h1 * B + s[i]) % P1; h2 = (h2 * B + s[i]) % P2; } if (h1 == kh1 && h2 == kh2) { positions.push_back(0); } for (int i = 1; i + m <= s.size(); i++) { h1 = (h1 * B + s[i + m - 1]) % P1; h1 = (h1 + P1 - (bm1 * s[i - 1] % P1)) % P1; h2 = (h2 * B + s[i + m - 1]) % P2; h2 = (h2 + P2 - (bm2 * s[i - 1] % P2)) % P2; if (h1 == kh1 && h2 == kh2) { positions.push_back(i); } } return positions; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int64> xs(n); vector<int64> ys(n); for (int i = 0; i < n; i++) { cin >> xs[i]; } for (int i = 0; i < n; i++) { cin >> ys[i]; } vector<int64> as(2 * n); vector<int64> bs(n); for (int i = 0; i < 2 * n; i++) { as[i] = xs[(i + 1) % n] ^ xs[i % n]; } for (int i = 0; i < n; i++) { bs[i] = ys[(i + 1) % n] ^ ys[i % n]; } auto ps = FindAllByRollingHash(as, bs); for (int p : ps) { if (p < n) cout << p << " " << (xs[p] ^ ys[0]) << "\n"; } }
[ "control_flow.branch.if.add" ]
744,014
744,015
u771276542
cpp
p02816
#include <algorithm> #include <bits/stdc++.h> #include <cmath> #include <cstdio> #include <iostream> #include <map> #include <queue> #include <set> #include <utility> #include <vector> #define fir first #define sec second #define sz(s) (s).size() #define pb push_back #define get(n) scanf("%d", &n) #define gets(s) \ string s; \ cin >> (s) #define prfi(n) printf("%d", &n) #define prfd(n) printf("%lf", &n) #define All(s) (s).begin(), (s).end() #define rep(i, j, k) for (int(i) = (j); (i) <= (k); (i)++) #define rep0(i, j) for (int(i) = 0; (i) < (j); (i)++) #define repdown(i, j, k) for (int(i) = (j); (i) >= (k); (i)--) #define repdown0(i, j) for (int(i) = (j); i >= 0; i--) #define vfor(c, v) for (auto(c) : v) #define dump(x) std::cout << #x << " = " << (x) << std::endl; #define debug(x) \ cout << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl; using ll = long long; using pii = std::pair<int, int>; using vi = std::vector<int>; using vvi = std::vector<vi>; using vll = std::vector<ll>; using vvll = std::vector<vll>; using vd = std::vector<double>; using vvd = std::vector<vd>; using qi = std::queue<int>; using vpii = std::vector<std::pair<int, int>>; using namespace std; const int Mod = (1e9) + 7; const int max_n = 3 * (1e5) + 1; const int max_m = 83 * (1e5) + 1; const int INF = 10241024; const ll INFL = (1ll << 62) - 1; const int dx[] = {-1, 0, 0, 1}; const int dy[] = {0, -1, 1, 0}; //_____________________________________Templates_________________________________________// template <class T1, class T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; } template <class T1, class T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; } // mainly use for dynamic prog template <class T1, class T2> void update(T1 &a, T2 b) { a += b; if (a > Mod) a %= Mod; } inline void IN(void) { return; } template <typename First, typename... Rest> void IN(First &first, Rest &...rest) { cin >> first; IN(rest...); return; } inline void OUT(void) { cout << "\n"; return; } template <typename First, typename... Rest> void OUT(First first, Rest... rest) { cout << first << " "; OUT(rest...); return; } template <class T> struct segt { int n; T def; vector<T> dat; function<T(T, T)> operation; function<T(T, T)> update; segt(size_t _n, T _def, function<T(T, T)> _operation, function<T(T, T)> _update) : def(_def), operation(_operation), update(_update) { n = 1; while (n < _n) { n *= 2; } dat = vector<T>(2 * n - 1, _def); } T _query(int a, int b, int k, int l, int r) { if (r <= a || b <= l) return def; if (a <= r && l <= b) return dat[k]; else { T vl = _query(a, b, k * 2 + 1, l, (l + r) / 2); T vr = _query(a, b, k * 2 + 2, (l + r) / 2, r); return operation(vl, vr); } } T query(int a, int b) { return _query(a, b, 0, 0, n); } void change(int i, T value) { i += n - 1; dat[i] = update(dat[i], value); while (i > 0) { i = (i - 1) / 2; dat[i] = operation(dat[i * 2 + 1], dat[i * 2 + 2]); } } T operator[](int i) { return dat[i + n - 1]; } }; //_____________________array calc____________________________________// /* vvi mul(vvi &A, vvi &B){ vvi C(A.size(), vi(B.size())) rep(i,A.size()){ rep(j,B.size()){ rep(k,B[0].size()){ C[i][j] = (C[i][j] A[i][k] + B[k][j]) % Mod; } } } return C; } vvi pow(vvi A, ll n){ vvi B(A.size(), vi(A.size())); rep(i=0;i<A.size();i++){ B[i][i] = 1; } while (n >0){ if (n & 1) B = mul(B, A); A = mul(A, A); n = n >> 1; } return B; } */ //_____________________Bynary Indexed Tree __________________________// /* struct BIT{ #define bs (1 << 20) int bit[bs]; int sum (int i){ int s = 0; while(i > 0){ s += bit[i]; i -= i & -i; } return s; } void add(int i, int x){ while(i <= n){ bit[i] += x; i += i & -i; } } } */ //____________________RMQ____________________________// /*struct RMQ{ #define s (1 << 20) ll dat[s]; int n; void init(){ memset(dat, -1e10, sizeof(dat)); } void upd(int k, ll a){ k += s/2 - 1; dat[k] = a; while(k > 0){ k = (k -1) /2; dat[k] = max(dat[k*2+1], dat[k*2+2]); } } ll query(int a, int b, int k, int l, int r){ if(r <= a || b <= l) return -1e8; if( a <= l && r <= b)return dat[k]; else { ll vl = query(a, b, k*2+1, l, (l+r)/2); ll vr = query(a, b, k*2+2, (l+r)/2, r); return max(vl, vr); } } void Debug(int a){ a += s/2 - 1; rep(i,s/2 - 1,a){ dump(dat[i]); } } }; */ //_____________________ following sorce code_________________________// int n, m, k, x, y; // vvll dp(400, vll(400)); vll dp(1 << 16, -1e18); string s; vvll v(16, vll(16)); vll cost(1 << 16); vi f(const vi &x) { int n = sz(x); vi res(n); rep(i, 0, n - 1) { res[i] = x[i] ^ x[(i + 1) % n]; } return res; } template <class T> struct MP { int n; vi a; T t; MP() {} MP(const T &t) : t(t) { n = sz(t); a = vi(n + 1); a[0] = -1; int j = -1; rep(i, 0, n - 1) { while (j != -1 && t[j] != t[i]) j = a[j]; j++; a[i + 1] = j; } } int operator[](int i) { return a[i]; } vi findAll(const T &s) { vi res; int j = 0; rep(i, 0, sz(s) - 1) { if (j != -1 && s[i] != t[j]) j = a[j]; j++; if (j == n) { res.pb(i - j + 1); j = a[j]; } } return res; } }; int main() { cin.tie(0); ios::sync_with_stdio(false); IN(n); vi a(n), b(n); rep0(i, n) IN(a[i]); rep0(i, n) IN(b[i]); vi xa = f(a), xb = f(b); xb.insert(xb.end(), xb.begin(), xb.end()); MP<vi> mp(xa); vi res = mp.findAll(xb); vi ans; for (auto c : res) { ans.pb(n - c); } sort(All(ans)); for (auto c : ans) { if (c >= n) continue; int x = a[k] ^ b[0]; OUT(c, x); } return 0; }
#include <algorithm> #include <bits/stdc++.h> #include <cmath> #include <cstdio> #include <iostream> #include <map> #include <queue> #include <set> #include <utility> #include <vector> #define fir first #define sec second #define sz(s) (s).size() #define pb push_back #define get(n) scanf("%d", &n) #define gets(s) \ string s; \ cin >> (s) #define prfi(n) printf("%d", &n) #define prfd(n) printf("%lf", &n) #define All(s) (s).begin(), (s).end() #define rep(i, j, k) for (int(i) = (j); (i) <= (k); (i)++) #define rep0(i, j) for (int(i) = 0; (i) < (j); (i)++) #define repdown(i, j, k) for (int(i) = (j); (i) >= (k); (i)--) #define repdown0(i, j) for (int(i) = (j); i >= 0; i--) #define vfor(c, v) for (auto(c) : v) #define dump(x) std::cout << #x << " = " << (x) << std::endl; #define debug(x) \ cout << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl; using ll = long long; using pii = std::pair<int, int>; using vi = std::vector<int>; using vvi = std::vector<vi>; using vll = std::vector<ll>; using vvll = std::vector<vll>; using vd = std::vector<double>; using vvd = std::vector<vd>; using qi = std::queue<int>; using vpii = std::vector<std::pair<int, int>>; using namespace std; const int Mod = (1e9) + 7; const int max_n = 3 * (1e5) + 1; const int max_m = 83 * (1e5) + 1; const int INF = 10241024; const ll INFL = (1ll << 62) - 1; const int dx[] = {-1, 0, 0, 1}; const int dy[] = {0, -1, 1, 0}; //_____________________________________Templates_________________________________________// template <class T1, class T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; } template <class T1, class T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; } // mainly use for dynamic prog template <class T1, class T2> void update(T1 &a, T2 b) { a += b; if (a > Mod) a %= Mod; } inline void IN(void) { return; } template <typename First, typename... Rest> void IN(First &first, Rest &...rest) { cin >> first; IN(rest...); return; } inline void OUT(void) { cout << "\n"; return; } template <typename First, typename... Rest> void OUT(First first, Rest... rest) { cout << first << " "; OUT(rest...); return; } template <class T> struct segt { int n; T def; vector<T> dat; function<T(T, T)> operation; function<T(T, T)> update; segt(size_t _n, T _def, function<T(T, T)> _operation, function<T(T, T)> _update) : def(_def), operation(_operation), update(_update) { n = 1; while (n < _n) { n *= 2; } dat = vector<T>(2 * n - 1, _def); } T _query(int a, int b, int k, int l, int r) { if (r <= a || b <= l) return def; if (a <= r && l <= b) return dat[k]; else { T vl = _query(a, b, k * 2 + 1, l, (l + r) / 2); T vr = _query(a, b, k * 2 + 2, (l + r) / 2, r); return operation(vl, vr); } } T query(int a, int b) { return _query(a, b, 0, 0, n); } void change(int i, T value) { i += n - 1; dat[i] = update(dat[i], value); while (i > 0) { i = (i - 1) / 2; dat[i] = operation(dat[i * 2 + 1], dat[i * 2 + 2]); } } T operator[](int i) { return dat[i + n - 1]; } }; //_____________________array calc____________________________________// /* vvi mul(vvi &A, vvi &B){ vvi C(A.size(), vi(B.size())) rep(i,A.size()){ rep(j,B.size()){ rep(k,B[0].size()){ C[i][j] = (C[i][j] A[i][k] + B[k][j]) % Mod; } } } return C; } vvi pow(vvi A, ll n){ vvi B(A.size(), vi(A.size())); rep(i=0;i<A.size();i++){ B[i][i] = 1; } while (n >0){ if (n & 1) B = mul(B, A); A = mul(A, A); n = n >> 1; } return B; } */ //_____________________Bynary Indexed Tree __________________________// /* struct BIT{ #define bs (1 << 20) int bit[bs]; int sum (int i){ int s = 0; while(i > 0){ s += bit[i]; i -= i & -i; } return s; } void add(int i, int x){ while(i <= n){ bit[i] += x; i += i & -i; } } } */ //____________________RMQ____________________________// /*struct RMQ{ #define s (1 << 20) ll dat[s]; int n; void init(){ memset(dat, -1e10, sizeof(dat)); } void upd(int k, ll a){ k += s/2 - 1; dat[k] = a; while(k > 0){ k = (k -1) /2; dat[k] = max(dat[k*2+1], dat[k*2+2]); } } ll query(int a, int b, int k, int l, int r){ if(r <= a || b <= l) return -1e8; if( a <= l && r <= b)return dat[k]; else { ll vl = query(a, b, k*2+1, l, (l+r)/2); ll vr = query(a, b, k*2+2, (l+r)/2, r); return max(vl, vr); } } void Debug(int a){ a += s/2 - 1; rep(i,s/2 - 1,a){ dump(dat[i]); } } }; */ //_____________________ following sorce code_________________________// int n, m, k, x, y; // vvll dp(400, vll(400)); vll dp(1 << 16, -1e18); string s; vvll v(16, vll(16)); vll cost(1 << 16); vi f(const vi &x) { int n = sz(x); vi res(n); rep(i, 0, n - 1) { res[i] = x[i] ^ x[(i + 1) % n]; } return res; } template <class T> struct MP { int n; vi a; T t; MP() {} MP(const T &t) : t(t) { n = sz(t); a = vi(n + 1); a[0] = -1; int j = -1; rep(i, 0, n - 1) { while (j != -1 && t[j] != t[i]) j = a[j]; j++; a[i + 1] = j; } } int operator[](int i) { return a[i]; } vi findAll(const T &s) { vi res; int j = 0; rep(i, 0, sz(s) - 1) { if (j != -1 && s[i] != t[j]) j = a[j]; j++; if (j == n) { res.pb(i - j + 1); j = a[j]; } } return res; } }; int main() { cin.tie(0); ios::sync_with_stdio(false); IN(n); vi a(n), b(n); rep0(i, n) IN(a[i]); rep0(i, n) IN(b[i]); vi xa = f(a), xb = f(b); xb.insert(xb.end(), xb.begin(), xb.end()); MP<vi> mp(xa); vi res = mp.findAll(xb); vi ans; for (auto c : res) { ans.pb(n - c); } sort(All(ans)); for (auto c : ans) { if (c >= n) continue; int x = a[c] ^ b[0]; OUT(c, x); } return 0; }
[ "identifier.change", "variable_access.subscript.index.change", "expression.operation.binary.change" ]
744,019
744,020
u264405855
cpp
p02816
#include <bits/stdc++.h> #define GO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0) using namespace std; typedef long long ll; typedef vector<int> vec; const int Max = 1e6 + 5; const int Mod = 1e9 + 7; int n, a[Max], b[Max], cnt[Max], ans[Max]; int f[Max]; void KMP(int sh) { for (int i = 1, j = 0; i < n; i++) { while (j && (b[i] & 1 << sh) != (b[j] & 1 << sh)) j = f[j - 1]; if ((b[i] & 1 << sh) == (b[j] & 1 << sh)) j++; f[i] = j; } for (int i = 1, j = 0; i < 2 * n; i++) { while (j && (a[i] & 1 << sh) != (b[j] & 1 << sh)) j = f[j - 1]; if ((a[i] & 1 << sh) == (b[j] & 1 << sh)) j++; if (j == n) cnt[i - n]++, j = f[j - 1]; } for (int i = 1, j = 0; i < 2 * n; i++) { while (j && (a[i] & 1 << sh) == (b[j] & 1 << sh)) j = f[j - 1]; if ((a[i] & 1 << sh) != (b[j] & 1 << sh)) j++; if (j == n) cnt[i - n]++, ans[i - n] |= 1 << sh, j = f[j - 1]; } } int main() { GO; cin >> n; for (int i = 0; i < n; i++) cin >> a[i], a[i + n] = a[i]; for (int i = 0; i < n; i++) cin >> b[i]; for (int i = 0; i < 30; i++) KMP(i); set<pair<int, int>> st; for (int i = 0; i < 2 * n; i++) if (cnt[i] == 30) st.insert({i % n, ans[i]}); for (auto i : st) cout << i.first << ' ' << i.second << '\n'; return 0; }
#include <bits/stdc++.h> #define GO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0) using namespace std; typedef long long ll; typedef vector<int> vec; const int Max = 1e6 + 5; const int Mod = 1e9 + 7; int n, a[Max], b[Max], cnt[Max], ans[Max]; int f[Max]; void KMP(int sh) { for (int i = 1, j = 0; i < n; i++) { while (j && (b[i] & 1 << sh) != (b[j] & 1 << sh)) j = f[j - 1]; if ((b[i] & 1 << sh) == (b[j] & 1 << sh)) j++; f[i] = j; } for (int i = 1, j = 0; i < 2 * n; i++) { while (j && (a[i] & 1 << sh) != (b[j] & 1 << sh)) j = f[j - 1]; if ((a[i] & 1 << sh) == (b[j] & 1 << sh)) j++; if (j == n) cnt[i - n + 1]++, j = f[j - 1]; } for (int i = 1, j = 0; i < 2 * n; i++) { while (j && (a[i] & 1 << sh) == (b[j] & 1 << sh)) j = f[j - 1]; if ((a[i] & 1 << sh) != (b[j] & 1 << sh)) j++; if (j == n) cnt[i - n + 1]++, ans[i - n + 1] |= 1 << sh, j = f[j - 1]; } } int main() { GO; cin >> n; for (int i = 0; i < n; i++) cin >> a[i], a[i + n] = a[i]; for (int i = 0; i < n; i++) cin >> b[i]; for (int i = 0; i < 30; i++) KMP(i); set<pair<int, int>> st; for (int i = 0; i < 2 * n; i++) if (cnt[i] == 30) st.insert({i % n, ans[i]}); for (auto i : st) cout << i.first << ' ' << i.second << '\n'; return 0; }
[ "assignment.change" ]
744,026
744,027
u073836689
cpp
p02816
#include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <complex> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <time.h> #include <tuple> #include <unistd.h> #include <vector> #define _USE_MATH_DEFINES #define _GLIBCXX_DEBUG using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> plglg; typedef pair<double, ll> pdlg; typedef tuple<int, int, int> tiii; typedef tuple<ll, ll, ll> tlglglg; typedef tuple<double, double, double> tddd; typedef complex<double> xy_t; typedef vector<ll> vll; typedef vector<vector<ll>> matrix; #define REP(i, x, y) for (ll i = (ll)x; i < (ll)y; i++) #define DREP(i, x, y, d) for (ll i = (ll)x; i < (ll)y; i += (ll)d) #define PER(i, x, y) for (ll i = (ll)x; i > (ll)y; i--) #define DPER(i, x, y, d) for (ll i = (ll)x; i > (ll)y; i -= (ll)d) int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; double pi = 3.141592653589793; ll mod = 1000000007; int intmax = 2147483647; int intmin = -2147483648; ll llmax = 9223372036854775807; ll llmin = -9223372036854775807; int iinf = intmax / 8; ll inf = llmax / 8; double eps = 1e-12; int main() { cin.tie(0); ios::sync_with_stdio(false); ll N; cin >> N; ll a[N], b[N]; REP(i, 0, N) { cin >> a[i]; } REP(i, 0, N) { cin >> b[i]; } ll aa[N], bb[N]; REP(i, 0, N - 1) { aa[i] = a[i] ^ a[i + 1]; bb[i] = b[i] ^ b[i + 1]; } aa[N - 1] = a[N - 1] ^ a[0]; bb[N - 1] = b[N - 1] ^ b[0]; ll cyc = -1; REP(i, 1, N + 1) { if (N % i != 0) { continue; } bool ok = true; REP(j, 0, N) { if (aa[j] != aa[(j + i) % N]) { ok = false; break; } } if (ok) { cyc = i; break; } } ll ansbase = -1; map<ll, ll> mpa; map<ll, ll> mpb; REP(i, 0, N) { if (mpa.count(aa[i]) == 0) { mpa[aa[i]] = 1; } else { mpa[aa[i]]++; } if (mpb.count(bb[i]) == 0) { mpb[bb[i]] = 1; } else { mpb[bb[i]]++; } } REP(i, 0, N) { if (mpa[aa[i]] != mpb[bb[i]]) { return 0; } } ll minnum = -1; ll minval = inf; REP(i, 0, N) { if (mpa[aa[i]] < minval) { minnum = i; minval = mpa[aa[i]]; } } REP(i, 0, cyc) { bool ok = true; REP(j, 0, N) { if (aa[(minnum + j + i) % N] != bb[(minnum + j) % N]) { ok = false; break; } } if (ok) { ansbase = i; break; } } if (ansbase != -1) { vector<plglg> ans; ll num = ansbase; while (num < N) { ll val = a[num] ^ b[0]; ans.push_back(plglg(num, val)); num += cyc; } REP(i, 0, ans.size()) { cout << ans[i].first << " " << ans[i].second << endl; } } }
#include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <complex> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <time.h> #include <tuple> #include <unistd.h> #include <vector> #define _USE_MATH_DEFINES #define _GLIBCXX_DEBUG using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> plglg; typedef pair<double, ll> pdlg; typedef tuple<int, int, int> tiii; typedef tuple<ll, ll, ll> tlglglg; typedef tuple<double, double, double> tddd; typedef complex<double> xy_t; typedef vector<ll> vll; typedef vector<vector<ll>> matrix; #define REP(i, x, y) for (ll i = (ll)x; i < (ll)y; i++) #define DREP(i, x, y, d) for (ll i = (ll)x; i < (ll)y; i += (ll)d) #define PER(i, x, y) for (ll i = (ll)x; i > (ll)y; i--) #define DPER(i, x, y, d) for (ll i = (ll)x; i > (ll)y; i -= (ll)d) int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; double pi = 3.141592653589793; ll mod = 1000000007; int intmax = 2147483647; int intmin = -2147483648; ll llmax = 9223372036854775807; ll llmin = -9223372036854775807; int iinf = intmax / 8; ll inf = llmax / 8; double eps = 1e-12; int main() { cin.tie(0); ios::sync_with_stdio(false); ll N; cin >> N; ll a[N], b[N]; REP(i, 0, N) { cin >> a[i]; } REP(i, 0, N) { cin >> b[i]; } ll aa[N], bb[N]; REP(i, 0, N - 1) { aa[i] = a[i] ^ a[i + 1]; bb[i] = b[i] ^ b[i + 1]; } aa[N - 1] = a[N - 1] ^ a[0]; bb[N - 1] = b[N - 1] ^ b[0]; ll cyc = -1; REP(i, 1, N + 1) { if (N % i != 0) { continue; } bool ok = true; REP(j, 0, N) { if (aa[j] != aa[(j + i) % N]) { ok = false; break; } } if (ok) { cyc = i; break; } } ll ansbase = -1; map<ll, ll> mpa; map<ll, ll> mpb; REP(i, 0, N) { if (mpa.count(aa[i]) == 0) { mpa[aa[i]] = 1; } else { mpa[aa[i]]++; } if (mpb.count(bb[i]) == 0) { mpb[bb[i]] = 1; } else { mpb[bb[i]]++; } } REP(i, 0, N) { if (mpa[aa[i]] != mpb[aa[i]]) { return 0; } } ll minnum = -1; ll minval = inf; REP(i, 0, N) { if (mpa[aa[i]] < minval) { minnum = i; minval = mpa[aa[i]]; } } REP(i, 0, cyc) { bool ok = true; REP(j, 0, N) { if (aa[(minnum + j + i) % N] != bb[(minnum + j) % N]) { ok = false; break; } } if (ok) { ansbase = i; break; } } if (ansbase != -1) { vector<plglg> ans; ll num = ansbase; while (num < N) { ll val = a[num] ^ b[0]; ans.push_back(plglg(num, val)); num += cyc; } REP(i, 0, ans.size()) { cout << ans[i].first << " " << ans[i].second << endl; } } }
[ "identifier.change", "variable_access.subscript.index.change", "control_flow.branch.if.condition.change" ]
744,028
744,029
u006204672
cpp
p02816
#include <iostream> #include <vector> #define int long long int using namespace std; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; vector<int> a(n), b(n); for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) cin >> b[i]; vector<int> aa(n), bb(n); for (int i = 0; i < n; i++) { aa[i] = (a[i] ^ a[(i + 1) % n]); bb[i] = (b[i] ^ b[(i + 1) % n]); } vector<int> s; for (int i = 0; i < n; i++) s.push_back(bb[i]); s.push_back(-1); for (int i = 0; i < n; i++) s.push_back(aa[i]); for (int i = 0; i < n; i++) s.push_back(aa[i]); vector<int> p(s.size() + 1); p[0] = -1; for (int i = 1; i < p.size(); i++) { int k = p[i - 1]; while (k >= 0) { if (s[k] == s[i - 1]) { p[i] = k + 1; break; } k = p[k]; } if (p[i] == n) { int res1 = i - n - n - 1; int res2 = (b[0] ^ a[res1]); cout << res1 << " " << res2 << "\n"; } } return 0; }
#include <iostream> #include <vector> #define int long long int using namespace std; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; vector<int> a(n + 5), b(n); for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) cin >> b[i]; vector<int> aa(n), bb(n); for (int i = 0; i < n; i++) { aa[i] = (a[i] ^ a[(i + 1) % n]); bb[i] = (b[i] ^ b[(i + 1) % n]); } vector<int> s; for (int i = 0; i < n; i++) s.push_back(bb[i]); s.push_back(-1); for (int i = 0; i < n; i++) s.push_back(aa[i]); for (int i = 0; i < n; i++) s.push_back(aa[i]); vector<int> p(s.size() + 1); p[0] = -1; for (int i = 1; i < p.size() - 1; i++) { int k = p[i - 1]; while (k >= 0) { if (s[k] == s[i - 1]) { p[i] = k + 1; break; } k = p[k]; } if (p[i] == n) { int res1 = i - n - n - 1; int res2 = (b[0] ^ a[res1]); cout << res1 << " " << res2 << "\n"; } } return 0; }
[ "control_flow.loop.for.condition.change", "misc.off_by_one" ]
744,030
744,031
u778887443
cpp
p02816
//#undef _DEBUG //#pragma GCC optimize("Ofast") //不動小数点の計算高速化 //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace std::chrono; #define int long long // todo 消したら動かない intの代わりにsignedを使う #define ll long long auto start_time = system_clock::now(); auto past_time = system_clock::now(); #define debugName(VariableName) #VariableName //最大引数がN #define over2(o1, o2, name, ...) name #define over3(o1, o2, o3, name, ...) name #define over4(o1, o2, o3, o4, name, ...) name #define over5(o1, o2, o3, o4, o5, name, ...) name #define over6(o1, o2, o3, o4, o5, o6, name, ...) name #define over7(o1, o2, o3, o4, o5, o6, o7, name, ...) name #define over8(o1, o2, o3, o4, o5, o6, o7, o8, name, ...) name #define over9(o1, o2, o3, o4, o5, o6, o7, o8, o9, name, ...) name #define over10(o1, o2, o3, o4, o5, o6, o7, o8, o9, o10, name, ...) name #define endl '\n' /*@formatter:off*/ //[-n, n)にアクセスできる //また、外部関数resizeに渡せる // sizeは[-n, n)でnを返す template <class T> class mvec { vector<T> v; int n; public: mvec() : n(0), v(0) {} mvec(int n) : n(n), v(n * 2) {} mvec(int n, T val) : n(n), v(n * 2, val) {} auto &operator[](int i) { return v[i + n]; } auto size() { return n; } void resize(int sn) { assert(n == 0); n = sn; v.resize(sn * 2); } auto begin() { return v.begin(); } auto rbegin() { return v.rbegin(); } auto end() { return v.end(); } auto rend() { return v.rend(); } }; //[]でboolは参照を返さないため特殊化が必要 template <> struct mvec<bool> { vector<bool> v; int n; mvec() : n(0), v(0) {} mvec(int n) : n(n), v(n * 2) {} mvec(int n, bool val) : n(n), v(n * 2, val) {} auto operator[](int i) { return v[i + n]; } auto size() { return v.size(); } void resize(int sn) { assert(n == 0); n = sn; v.resize(sn * 2); } auto begin() { return v.begin(); } auto rbegin() { return v.rbegin(); } auto end() { return v.end(); } auto rend() { return v.rend(); } }; template <class T> ostream &operator<<(ostream &os, mvec<T> &a) { int spa = 3; for (auto &&v : a) { spa = max(spa, (int)(to_string(v).size()) + 1); } int n = (int)a.size(); os << endl; for (int i = -n; i < n; i++) { int need = spa - ((int)to_string(i).size()); if (i == -n) { need -= min(need, spa - ((int)to_string(a[i]).size())); } while (need--) { os << " "; } os << i; } os << endl; int i = -n; for (auto &&v : a) { int need = spa - ((int)to_string(v).size()); if (i == -n) { need -= min(need, spa - ((int)to_string(i).size())); } while (need--) { os << " "; } os << v; i++; } return os; } #define mv mvec #define MV mvec using mvi = mvec<ll>; using mvb = mvec<bool>; using mvs = mvec<string>; using mvd = mvec<double>; using mvc = mvec<char>; #define mvvt0(t) mvec<mvec<t>> #define mvvt1(t, a) mvec<mvec<t>> a #define mvvt2(t, a, b) mvec<mvec<t>> a(b) #define mvvt3(t, a, b, c) mvec<mvec<t>> a(b, mvec<t>(c)) #define mvvt4(t, a, b, c, d) mvec<mvec<t>> a(b, mvec<t>(c, d)) #define mvvi(...) \ over4(__VA_ARGS__, mvvt4, mvvt3, mvvt2, mvvt1, mvvt0)(ll, __VA_ARGS__) template <typename T> mvec<T> make_mv(size_t a) { return mvec<T>(a); } template <typename T, typename... Ts> auto make_mv(size_t a, Ts... ts) { return mvec<decltype(make_mv<T>(ts...))>(a, make_mv<T>(ts...)); } #define mvni(name, ...) auto name = make_mv<ll>(__VA_ARGS__) #ifdef _DEBUG // https://marycore.jp/prog/cpp/class-extension-methods/ 違うかも template <class T, class A = std::allocator<T>> struct debtor : std::vector<T, A> { using std::vector<T, A>::vector; template <class U> int deb_v(U a, int v) { return v; } template <class U> int deb_v(debtor<U> &a, int v = 0) { cerr << a.size() << " "; return deb_v(a.at(0), v + 1); } template <class U> void deb_o(U a) { cerr << a << " "; } template <class U> void deb_o(debtor<U> &a) { for (int i = 0; i < min((int)a.size(), 15ll); i++) { deb_o(a[i]); } if ((int)a.size() > 15) { cerr << "..."; } cerr << endl; } typename std::vector<T>::reference operator[](typename std::vector<T>::size_type n) { if (n < 0 || n >= (int)this->size()) { int siz = (int)this->size(); cerr << "vector size = "; int dim = deb_v((*this)); cerr << endl; cerr << "out index at " << n << endl; cerr << endl; if (dim <= 2) { deb_o((*this)); } exit(0); } return this->at(n); } }; #define vector debtor #endif #ifdef _DEBUG //区間削除は出来ない template <class T> struct my_pbds_tree { set<T> s; auto begin() { return s.begin(); } auto end() { return s.end(); } auto rbegin() { return s.rbegin(); } auto rend() { return s.rend(); } auto empty() { return s.empty(); } auto size() { return s.size(); } void clear() { s.clear(); } template <class U> void insert(U v) { s.insert(v); } template <class U> void operator+=(U v) { insert(v); } template <class F> auto erase(F v) { return s.erase(v); } template <class U> auto find(U v) { return s.find(v); } template <class U> auto lower_bound(U v) { return s.lower_bound(v); } template <class U> auto upper_bound(U v) { return s.upper_bound(v); } auto find_by_order(ll k) { auto it = s.begin(); for (ll i = 0; i < k; i++) it++; return it; } auto order_of_key(ll v) { auto it = s.begin(); ll i = 0; for (; it != s.end() && *it < v; i++) it++; return i; } }; #define pbds(T) my_pbds_tree<T> // gp_hash_tableでcountを使えないようにするため template <class T, class U> struct my_unordered_map { unordered_map<T, U> m; my_unordered_map(){}; auto begin() { return m.begin(); } auto end() { return m.end(); } auto cbegin() { return m.cbegin(); } auto cend() { return m.cend(); } template <class V> auto erase(V v) { return m.erase(v); } void clear() { m.clear(); } /*countは gp_hash_tableに存在しない*/ /*!= m.end()*/ template <class V> auto find(V v) { return m.find(v); } template <class V> auto &operator[](V n) { return m[n]; } }; #define unordered_map my_unordered_map #define umapi unordered_map<ll, ll> #define umapp unordered_map<P, ll> #define umapu unordered_map<uint64_t, ll> #define umapip unordered_map<ll, P> #else // umapはunorderd_mapになる // umapiはgp_hash_table // find_by_order(k) k番目のイテレーター // order_of_key(k) k以上が前から何番目か #define pbds(U) \ __gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag, \ __gnu_pbds::tree_order_statistics_node_update> #define umapi __gnu_pbds::gp_hash_table<ll, ll, xorshift> #define umapp __gnu_pbds::gp_hash_table<P, ll, xorshift> #define umapu __gnu_pbds::gp_hash_table<uint64_t, ll, xorshift> #define umapip __gnu_pbds::gp_hash_table<ll, P, xorshift> #endif /*@formatter:on*/ struct xorshift { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } size_t operator()(std::pair<ll, ll> x) const { ll v = ((x.first) << 32) | x.second; static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(v + FIXED_RANDOM); } }; /*@formatter:off*/ template <class U, class L> void operator+=( __gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update> &s, L v) { s.insert(v); } //衝突対策 #define ws ws_ template <class A, class B, class C> struct T2 { A f; B s; C t; T2() { f = 0, s = 0, t = 0; } T2(A f, B s, C t) : f(f), s(s), t(t) {} bool operator<(const T2 &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 ?n s > r.s : t > r.t; 大きい順 */ } bool operator>(const T2 &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 T2 &r) const { return f == r.f && s == r.s && t == r.t; } bool operator!=(const T2 &r) const { return f != r.f || s != r.s || t != r.t; } }; template <class A, class B, class C, class D> struct F2 { A a; B b; C c; D d; F2() { a = 0, b = 0, c = 0, d = 0; } F2(A a, B b, C c, D d) : a(a), b(b), c(c), d(d) {} bool operator<(const F2 &r) const { return a != r.a ? a < r.a : b != r.b ? b < r.b : c != r.c ? c < r.c : d < r.d; /* return a != r.a ? a > r.a : b != r.b ? b > r.b : c != r.c ? c > r.c : d > r.d;*/ } bool operator>(const F2 &r) const { return a != r.a ? a > r.a : b != r.b ? b > r.b : c != r.c ? c > r.c : d > r.d; /* return a != r.a ? a < r.a : b != r.b ? b < r.b : c != r.c ? c < r.c : d < r.d;*/ } bool operator==(const F2 &r) const { return a == r.a && b == r.b && c == r.c && d == r.d; } bool operator!=(const F2 &r) const { return a != r.a || b != r.b || c != r.c || d != r.d; } ll operator[](ll i) { assert(i < 4); return i == 0 ? a : i == 1 ? b : i == 2 ? c : d; } }; typedef T2<ll, ll, ll> T; typedef F2<ll, ll, ll, ll> F; T mt(ll a, ll b, ll c) { return T(a, b, c); } F mf(ll a, ll b, ll c, ll d) { return F(a, b, c, d); } //@マクロ省略系 型,構造 #define double long double #define pow powl using dou = double; const double eps = 1e-9; //基本コメントアウト /* struct epsdou { double v; epsdou(double v = 0) : v(v) {} template<class T> epsdou &operator+=(T b) { v += (double) b; return (*this); } template<class T> epsdou &operator-=(T b) { v -= (double) b; return (*this); } template<class T> epsdou &operator*=(T b) { v *= (double) b; return (*this); } template<class T> epsdou &operator/=(T b) { v /= (double) b; return (*this); } epsdou operator+(epsdou b) { return v + (double) b; } epsdou operator-(epsdou b) { return v - (double) b; } epsdou operator*(epsdou b) { return v * (double) b; } epsdou operator/(epsdou b) { return v / (double) b; } epsdou operator-() const { return epsdou(-v); } template<class T> bool operator<(T b) { return v < (double) b; } template<class T> bool operator>(T b) { return v > (double) b; } template<class T> bool operator==(T b) { return fabs(v - (double) b) <= eps; } template<class T> bool operator<=(T b) { return v < (double) b || fabs(v - b) <= eps; } template<class T> bool operator>=(T b) { return v > (double) b || fabs(v - b) <= eps; } operator double() { return v; }};istream &operator>>(istream &iss, epsdou &a) { iss >> a.v; return iss;}ostream &operator<<(ostream &os, epsdou &a) { os << a.v; return os;} #define eps_conr_t(o) template<class T> epsdou operator o(T b, epsdou a){return a.v o (dou)b;} #define eps_conl_t(o) template<class T> epsdou operator o(epsdou a, T b){return a.v o (dou)b;} eps_conl_t(+)eps_conl_t(-)eps_conl_t(*)eps_conl_t(/)eps_conr_t(+)eps_conr_t(-)eps_conr_t(*)eps_conr_t(/) #undef double #define double epsdou */ #define ull unsigned long long using itn = int; using str = string; using bo = bool; #define au auto using P = pair<ll, ll>; using mvp = mvec<P>; using mvt = mvec<T>; #define MIN(a) numeric_limits<a>::min() #define MAX(a) numeric_limits<a>::max() #define fi first #define se second #define beg begin #define rbeg rbegin #define con continue #define bre break #define brk break #define is == #define el else #define elf else if #define upd update #define sstream stringstream #define maxq 1 #define minq -1 #define ZERO(a) memset(a, 0, sizeof(a)) #define MINUS(a) memset(a, 0xff, sizeof(a)) #define MALLOC(type, len) (type *)malloc((len) * sizeof(type)) #define lam(right) [&](auto &p) { return p right; } //マクロ省略系 コンテナ 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 V vector #define vvt0(t) vector<vector<t>> #define vvt1(t, a) vector<vector<t>> a #define vvt2(t, a, b) vector<vector<t>> a(b) #define vvt3(t, a, b, c) vector<vector<t>> a(b, vector<t>(c)) #define vvt4(t, a, b, c, d) vector<vector<t>> a(b, vector<t>(c, d)) #define vvi(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(ll, __VA_ARGS__) #define vvb(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(bool, __VA_ARGS__) #define vvs(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(string, __VA_ARGS__) #define vvd(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(double, __VA_ARGS__) #define vvc(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(char, __VA_ARGS__) #define vvp(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(P, __VA_ARGS__) #define vvt(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(T, __VA_ARGS__) #define vv(type, ...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(type, __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 vn(type, name, ...) auto name = make_v<type>(__VA_ARGS__) #define PQ priority_queue<ll, vector<ll>, greater<ll>> #define tos to_string using mapi = map<ll, ll>; using mapp = map<P, ll>; using mapd = map<dou, ll>; using mapc = map<char, ll>; using maps = map<str, ll>; using seti = set<ll>; using setd = set<dou>; using setc = set<char>; using sets = set<str>; using qui = queue<ll>; #define uset unordered_set #define useti unordered_set<ll, xorshift> #define mset multiset #define mseti multiset<ll> #define umap unordered_map #define mmap multimap //任意のマクロサポート用 使う度に初期化する int index_, v1_, v2_, v3_; template <class T> struct pq { priority_queue<T, vector<T>, greater<T>> q; /*小さい順*/ T su = 0; void clear() { q = priority_queue<T, vector<T>, greater<T>>(); su = 0; } void operator+=(T v) { su += v; q.push(v); } T sum() { return su; } T top() { return q.top(); } void pop() { su -= q.top(); q.pop(); } T poll() { T ret = q.top(); su -= ret; q.pop(); return ret; } ll size() { return q.size(); } }; template <class T> struct pqg { priority_queue<T> q; /*大きい順*/ T su = 0; void clear() { q = priority_queue<T>(); su = 0; } void operator+=(T v) { su += v; q.push(v); } T sum() { return su; } T top() { return q.top(); } void pop() { su -= q.top(); q.pop(); } T poll() { T ret = q.top(); su -= ret; q.pop(); return ret; } ll size() { return q.size(); } }; #define pqi pq<ll> #define pqgi pqg<ll> //マクロ 繰り返し //↓@オーバーロード隔離 #define rep1(n) for (ll rep1i = 0, rep1lim = n; rep1i < rep1lim; ++rep1i) #define rep2(i, n) for (ll i = 0, rep2lim = n; i < rep2lim; ++i) #define rep3(i, m, n) for (ll i = m, rep3lim = n; i < rep3lim; ++i) #define rep4(i, m, n, ad) for (ll i = m, rep4lim = n; i < rep4lim; i += ad) //逆順 閉区間 #define rer2(i, n) for (ll i = n; i >= 0; i--) #define rer3(i, m, n) for (ll i = m, rer3lim = n; i >= rer3lim; i--) #define rer4(i, m, n, dec) for (ll i = m, rer4lim = n; i >= rer4lim; i -= dec) //ループを一つにまとめないとフォーマットで汚くなるため #define nex_ind1(i) i++ #define nex_ind2(i, j, J) \ i = (j + 1 == J) ? i + 1 : i, j = (j + 1 == J ? 0 : j + 1) #define nex_ind3(i, j, k, J, K) \ i = (j + 1 == J && k + 1 == K) ? i + 1 : i, \ j = (k + 1 == K) ? (j + 1 == J ? 0 : j + 1) : j, \ k = (k + 1 == K ? 0 : k + 1) #define nex_ind4(i, j, k, l, J, K, L) \ i = (j + 1 == J && k + 1 == K && l + 1 == L) ? i + 1 : i, \ j = (k + 1 == K && l + 1 == L) ? (j + 1 == J ? 0 : j + 1) : j, \ k = (l + 1 == L ? (k + 1 == K ? 0 : k + 1) : k), l = l + 1 == L ? 0 : l + 1 #define nex_ind5(i, j, k, l, m, J, K, L, M) \ i = (j + 1 == J && k + 1 == K && l + 1 == L && m + 1 == M) ? i + 1 : i, \ j = (k + 1 == K && l + 1 == L && m + 1 == M) ? (j + 1 == J ? 0 : j + 1) : j, \ k = (l + 1 == L && m + 1 == M ? (k + 1 == K ? 0 : k + 1) : k), \ l = m + 1 == M ? l + 1 == L ? 0 : l + 1 : l, m = m + 1 == M ? 0 : m + 1 #define repss2(i, I) for (int i = 0; i < I; i++) #define repss4(i, j, I, J) \ for (int i = (J ? 0 : I), j = 0; i < I; nex_ind2(i, j, J)) #define repss6(i, j, k, I, J, K) \ for (int i = (J && K ? 0 : I), j = 0, k = 0; i < I; nex_ind3(i, j, k, J, K)) #define repss8(i, j, k, l, I, J, K, L) \ for (int i = (J && K && L ? 0 : I), j = 0, k = 0, l = 0; i < I; \ nex_ind4(i, j, k, l, J, K, L)) #define repss10(i, j, k, l, m, I, J, K, L, M) \ for (int i = (J && K && L && M ? 0 : I), j = 0, k = 0, l = 0, m = 0; i < I; \ nex_ind5(i, j, k, l, m, J, K, L, M)) // i,j,k...をnまで見る #define reps2(i, n) repss2(i, n) #define reps3(i, j, n) repss4(i, j, n, n) #define reps4(i, j, k, n) repss6(i, j, k, n, n, n) #define reps5(i, j, k, l, n) repss8(i, j, k, l, n, n, n, n) template <class T> void nex_repv2(int &i, int &j, int &I, int &J, vector<vector<T>> &s) { while (1) { j++; if (j >= J) { j = 0; i++; if (i < I) { J = (int)s[i].size(); } } if (i >= I || J) return; } } template <class T> void nex_repv3(int &i, int &j, int &k, int &I, int &J, int &K, vector<vector<vector<T>>> &s) { while (1) { k++; if (k >= K) { k = 0; j++; if (j >= J) { j = 0; i++; if (i >= I) return; } } J = (int)s[i].size(); K = (int)s[i][j].size(); if (J && K) return; } } #define repv_2(i, a) repss2(i, sz(a)) //正方形である必要はない //直前を持つのとどっちが早いか #define repv_3(i, j, a) \ for (int I = (int)a.size(), J = (int)a[0].size(), i = 0, j = 0; i < I; \ nex_repv2(i, j, I, J, a)) //箱状になっている事が要求される つまり[i] 次元目の要素数は一定 #define repv_4(i, j, k, a) \ for (int I = (int)a.size(), J = (int)a[0].size(), K = (int)a[0][0].size(), \ i = 0, j = 0, k = 0; \ i < I; nex_repv3(i, j, k, I, J, K, a)) #define repv_5(i, j, k, l, a) \ repss8(i, j, k, l, sz(a), sz(a[0]), sz(a[0][0]), sz(a[0][0][0])) #define repv_6(i, j, k, l, m, a) \ repss10(i, j, k, l, m, sz(a), sz(a[0]), sz(a[0][0]), sz(a[0][0][0]), \ sz(a[0][0][0][0])) template <typename T> struct has_rbegin_rend { private: template <typename U> static auto check(U &&obj) -> decltype(std::rbegin(obj), std::rend(obj), std::true_type{}); static std::false_type check(...); public: static constexpr bool value = decltype(check(std::declval<T>()))::value; }; template <typename T> constexpr bool has_rbegin_rend_v = has_rbegin_rend<T>::value; template <typename Iterator> class Range { public: Range(Iterator &&begin, Iterator &&end) noexcept : m_begin(std::forward<Iterator>(begin)), m_end(std::forward<Iterator>(end)) {} Iterator begin() const noexcept { return m_begin; } Iterator end() const noexcept { return m_end; } private: const Iterator m_begin; const Iterator m_end; }; template <typename Iterator> static inline Range<Iterator> makeRange(Iterator &&begin, Iterator &&end) noexcept { return Range<Iterator>{std::forward<Iterator>(begin), std::forward<Iterator>(end)}; } template <typename T> static inline decltype(auto) makeReversedRange(const std::initializer_list<T> &iniList) noexcept { return makeRange(std::rbegin(iniList), std::rend(iniList)); } template <typename T, typename std::enable_if_t<has_rbegin_rend_v<T>, std::nullptr_t> = nullptr> static inline decltype(auto) makeReversedRange(T &&c) noexcept { return makeRange(std::rbegin(c), std::rend(c)); } /* rbegin(), rend()を持たないものはこっちに分岐させて,エラーメッセージを少なくする*/ template <typename T, typename std::enable_if<!has_rbegin_rend<T>::value, std::nullptr_t>::type = nullptr> static inline void makeReversedRange(T &&) noexcept { static_assert(has_rbegin_rend<T>::value, "Specified argument doesn't have reverse iterator."); } #define form1(st) \ for (auto &&form_it = st.begin(); form_it != st.end(); ++form_it) #define form3(k, v, st) \ for (auto &&form_it = st.begin(); form_it != st.end(); ++form_it) #define form4(k, v, st, r) \ for (auto &&form_it = st.begin(); form_it != st.end() && (*form_it).fi < r; \ ++form_it) #define form5(k, v, st, l, r) \ for (auto &&form_it = st.lower_bound(l); \ form_it != st.end() && (*form_it).fi < r; ++form_it) #define fors1(st) \ for (auto &&fors_it = st.begin(); fors_it != st.end(); ++fors_it) #define fors2(v, st) \ for (auto &&fors_it = st.begin(); fors_it != st.end(); ++fors_it) #define fors3(v, st, r) \ for (auto &&fors_it = st.begin(); fors_it != st.end() && (*fors_it) < r; \ ++fors_it) #define fors4(v, st, l, r) \ for (auto &&fors_it = st.lower_bound(l); \ fors_it != st.end() && (*fors_it) < r; ++fors_it) #define forslr3(st, a, b) \ for (auto &&forslr_it = st.begin(); forslr_it != st.end(); ++forslr_it) #define forslr4(v, st, a, b) \ for (auto &&forslr_it = st.begin(); forslr_it != st.end(); ++forslr_it) #define forslr5(v, st, r, a, b) \ for (auto &&forslr_it = st.begin(); \ forslr_it != st.end() && (*forslr_it) < r; ++forslr_it) #define forslr6(v, st, l, r, a, b) \ for (auto &&forslr_it = st.lower_bound(l); \ forslr_it != st.end() && (*forslr_it) < r; ++forslr_it) template <class U> vector<U> to1d(vector<U> &a) { return a; } template <class U> vector<U> to1d(V<V<U>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) res.push_back(a2); return res; } template <class U> vector<U> to1d(V<V<V<U>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) res.push_back(a3); return res; } template <class U> vector<U> to1d(V<V<V<V<U>>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) for (auto &&a4 : a3) res.push_back(a4); return res; } template <class U> vector<U> to1d(V<V<V<V<V<U>>>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) for (auto &&a4 : a3) for (auto &&a5 : a4) res.push_back(a5); return res; } template <class U> vector<U> to1d(V<V<V<V<V<V<U>>>>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) for (auto &&a4 : a3) for (auto &&a5 : a4) for (auto &&a6 : a5) res.push_back(a6); return res; } #define fora_init_2(a, A) ; #define fora_init_4(a, b, A, B) \ auto &&a = A[fora_i]; \ auto &&b = B[fora_i]; #define fora_init_6(a, b, c, A, B, C) \ auto &&a = A[fora_i]; \ auto &&b = B[fora_i]; \ auto &&c = C[fora_i]; #define fora_init_8(a, b, c, d, A, B, C, D) \ auto &&a = A[fora_i]; \ auto &&b = B[fora_i]; \ auto &&c = C[fora_i]; \ auto &&d = D[fora_i]; #define fora_init(...) \ over8(__VA_ARGS__, fora_init_8, a, fora_init_6, a, fora_init_4, a, \ fora_init_2)(__VA_ARGS__) #define forr_init_2(a, A) auto &&a = A[forr_i]; #define forr_init_4(a, b, A, B) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; #define forr_init_6(a, b, c, A, B, C) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; \ auto &&c = C[forr_i]; #define forr_init_8(a, b, c, d, A, B, C, D) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; \ auto &&c = C[forr_i]; \ auto &&d = D[forr_i]; #define forr_init(...) \ over8(__VA_ARGS__, forr_init_8, a, forr_init_6, a, forr_init_4, a, \ forr_init_2)(__VA_ARGS__) #define forp_init(k, v, ...) \ auto &&k = (*forp_it).fi; \ auto &&v = (*forp_it).se; #define form_init(k, v, ...) \ auto &&k = (*form_it).fi; \ auto &&v = (*form_it).se; #define fors_init(v, ...) auto &&v = (*fors_it); #define forlr_init(a, A, ngl, ngr) \ auto a = A[forlr_i]; \ auto prev = forlr_i ? A[forlr_i - 1] : ngl; \ auto next = forlr_i + 1 < rep2lim ? A[forlr_i + 1] : ngr; #define forslr_init4(a, A, ngl, ngr) \ auto a = (*forslr_it); \ auto prev = (forslr_it != A.begin()) ? (*std::prev(forslr_it)) : ngl; \ auto next = (forslr_it != std::prev(A.end())) ? (*std::next(forslr_it)) : ngr; #define forslr_init5(a, A, r, ngl, ngr) \ auto a = (*forslr_it); \ auto prev = (forslr_it != A.begin()) ? (*std::prev(forslr_it)) : ngl; \ auto next = (forslr_it != std::prev(A.end())) ? (*std::next(forslr_it)) : ngr; #define forslr_init6(a, A, l, r, ngl, ngr) \ auto a = (*forslr_it); \ auto prev = (forslr_it != A.begin()) ? (*std::prev(forslr_it)) : ngl; \ auto next = (forslr_it != std::prev(A.end())) ? (*std::next(forslr_it)) : ngr; #define forslr_init(...) \ over6(__VA_ARGS__, forslr_init6, forslr_init5, forslr_init4)(__VA_ARGS__); #define fora_2(a, A) for (auto &&a : A) #define fora_4(a, b, A, B) rep(fora_i, sz(A)) #define fora_6(a, b, c, A, B, C) rep(fora_i, sz(A)) #define fora_8(a, b, c, d, A, B, C, D) rep(fora_i, sz(A)) #define forr_2(a, A) rer(forr_i, sz(A) - 1) #define forr_4(a, b, A, B) rer(forr_i, sz(A) - 1) #define forr_6(a, b, c, A, B, C) rer(forr_i, sz(A) - 1) #define forr_8(a, b, c, d, A, B, C, D) rer(forr_i, sz(A) - 1) //↑@オーバーロード隔離 // rep系はインデックス、for系は中身 #define rep(...) over4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__) #define rer(...) over4(__VA_ARGS__, rer4, rer3, rer2, )(__VA_ARGS__) // char用のrep #define repc(i, m, n) for (char i = m, repc3lim = n; i < repc3lim; ++i) // i,j,k...をnまで見る #define reps(...) over5(__VA_ARGS__, reps5, reps4, reps3, reps2, )(__VA_ARGS__) #define repss(...) \ over10(__VA_ARGS__, repss10, a, repss8, a, repss6, a, repss4, a, \ repss2)(__VA_ARGS__) // vectorのindexを走査する // repv(i,j,vvi) #define repv(...) \ over6(__VA_ARGS__, repv_6, repv_5, repv_4, repv_3, repv_2, )(__VA_ARGS__) #define rerv(i, A) for (int i = sz(A) - 1; i >= 0; i--) // repvn(dp) nは次元 #define repv1(a) repv(i, a) #define repv2(a) repv(i, j, a) #define repv3(a) repv(i, j, k, a) #define repv4(a) repv(i, j, k, l, a) #define fora(...) \ over8(__VA_ARGS__, fora_8, a, fora_6, a, fora_4, a, fora_2)(__VA_ARGS__) #define forr(...) \ over8(__VA_ARGS__, forr_8, a, forr_6, a, forr_4, a, forr_2)(__VA_ARGS__) //#define forr(v, a) for(auto&& v : makeReversedRange(a)) //参照を取らない #define forv(a, b) for (auto a : to1d(b)) //インデックスを前後含めて走査 #define ring(i, s, len) \ for (int i = s, prev = (s == 0) ? len - 1 : s - 1, \ next = (s == len - 1) ? 0 : s + 1, cou = 0; \ cou < len; \ cou++, prev = i, i = next, next = (next == len - 1) ? 0 : next + 1) //値と前後を見る #define ringv(v, d) \ index_ = 0; \ for (auto prev = d[sz(d) - 1], next = (int)d.size() > 1 ? d[1] : d[0], \ v = d[0]; \ index_ < sz(d); index_++, prev = v, v = next, \ next = (index_ >= sz(d) - 1 ? d[0] : d[index_ + 1])) // 左右をnext prevで見る 0の左と nの右 #define forlr(v, d, banpei_l, banpei_r) rep(forlr_i, sz(d)) #define form(...) \ over5(__VA_ARGS__, form5, form4, form3, form2, form1)(__VA_ARGS__) #define fors(...) over4(__VA_ARGS__, fors4, fors3, fors2, fors1)(__VA_ARGS__) #define forslr(...) \ over6(__VA_ARGS__, forslr6, forslr5, forslr4, forslr3)(__VA_ARGS__) #define forp(k, v, st) \ for (auto &&forp_it = st.begin(); forp_it != st.end(); ++forp_it) //マクロ 定数 #define k3 1010 #define k4 10101 #define k5 101010 #define k6 1010101 #define k7 10101010 const ll inf = (ll)1e9 + 100; const ll linf = (ll)1e18 + 100; const dou dinf = (dou)linf * linf; const char infc = '{'; const string infs = "{"; const double PI = 3.1415926535897932384626433832795029L; //マクロ省略形 関数等 #define arsz(a) (sizeof(a) / sizeof(a[0])) #define sz(a) ((ll)(a).size()) #define mp make_pair #define pb pop_back #define pf push_front #define eb emplace_back #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() constexpr bool ev(ll a) { return !(a & 1); } constexpr bool od(ll a) { return (a & 1); } //@拡張系 こう出来るべきというもの //埋め込み 存在を意識せずに機能を増やされているもの namespace std { template <> class hash<std::pair<signed, signed>> { public: size_t operator()(const std::pair<signed, signed> &x) const { return hash<ll>()(((ll)x.first << 32) | x.second); } }; template <> class hash<std::pair<ll, ll>> { public : /*大きいllが渡されると、<<32でオーバーフローするがとりあえず問題ないと判断*/ size_t operator()(const std::pair<ll, ll> &x) const { return hash<ll>()(((ll)x.first << 32) | x.second); } }; } // namespace std // stream まとめ /*@formatter:on*/ istream &operator>>(istream &iss, P &a) { iss >> a.first >> a.second; return iss; } template <typename T> istream &operator>>(istream &iss, vector<T> &vec) { for (T &x : vec) iss >> x; return iss; } template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> p) { os << p.fi << " " << p.se; return os; } ostream &operator<<(ostream &os, T p) { os << p.f << " " << p.s << " " << p.t; return os; } ostream &operator<<(ostream &os, F p) { os << p.a << " " << p.b << " " << p.c << " " << p.d; return os; } template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) { for (ll i = 0; i < vec.size(); ++i) os << vec[i] << (i + 1 == vec.size() ? "" : " "); return os; } template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) { for (ll i = 0; i < vec.size(); ++i) { for (ll j = 0; j < vec[i].size(); ++j) { os << vec[i][j] << " "; } os << endl; } return os; } template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &m) { for (auto &&v : m) os << v; return os; } template <class T> ostream &operator<<(ostream &os, set<T> s) { fora(v, s) { os << v << " "; } return os; } template <class T> ostream &operator<<(ostream &os, deque<T> a) { fora(v, a) os << v << " "; return os; } ostream &operator<<(ostream &os, vector<vector<char>> &vec) { rep(h, sz(vec)) { rep(w, sz(vec[0])) { os << vec[h][w]; } os << endl; } return os; } // template<class T,class U>ostream &operator<<(ostream &os, vector<pair<T,U>>& // a) {fora(v,a)os<<v<<endl;return os;} /*@formatter:off*/ template <typename W, typename H> void resize(W &vec, const H head) { vec.resize(head); } template <typename W, typename H, typename... T> void resize(W &vec, const H &head, const T... tail) { vec.resize(head); for (auto &v : vec) resize(v, tail...); } // 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); }); } //削除した後のvectorを返す template <typename T, typename F> vector<T> erase_if2(vector<T> &v, F f) { vector<T> nv; rep(i, sz(v)) { if (!f(v[i])) { nv.push_back(v[i]); } } return nv; } template <typename T, typename F> vector<vector<T>> erase_if2(vector<vector<T>> &v, F f) { vector<vector<T>> res; rep(i, sz(v)) { res[i] = erase_if2(v[i], f); } return res; } // all_of(A, %2); // all_of(A, a, a%2); #define all_of__2(a, right) all_of2(a, lam(right)) #define all_of__3(a, v, siki) all_of2(a, [&](auto v) { return siki; }) #define all_of(...) over3(__VA_ARGS__, all_of__3, all_of__2)(__VA_ARGS__) #define all_of_f(a, f) all_of2(a, f) #define any_of__2(a, right) any_of2(a, lam(right)) #define any_of__3(a, v, siki) any_of2(a, [&](auto v) { return siki; }) #define any_of(...) over3(__VA_ARGS__, any_of__3, any_of__2)(__VA_ARGS__) #define any_of_f(a, f) any_of2(a, f) #define none_of__2(a, right) none_of2(a, lam(right)) #define none_of__3(a, v, siki) none_of2(a, [&](auto v) { return siki; }) #define none_of(...) over3(__VA_ARGS__, none_of__3, none_of__2)(__VA_ARGS__) #define none_of_f(a, f) none_of2(a, f) #define find_if__2(a, right) find_if2(a, lam(right)) #define find_if__3(a, v, siki) find_if2(a, [&](auto v) { return siki; }) #define find_if(...) over3(__VA_ARGS__, find_if__3, find_if__2)(__VA_ARGS__) #define find_if_f(a, f) find_if2(a, f) #define rfind_if__2(a, right) rfind_if2(a, lam(right)) #define rfind_if__3(a, v, siki) rfind_if2(a, [&](auto v) { return siki; }) #define rfind_if(...) over3(__VA_ARGS__, rfind_if__3, rfind_if__2)(__VA_ARGS__) #define rfind_if_f(a, f) rfind_if2(a, f) #define contains_if__2(a, right) contains_if2(a, lam(right)) #define contains_if__3(a, v, siki) contains_if2(a, [&](auto v) { return siki; }) #define contains_if(...) \ over3(__VA_ARGS__, contains_if__3, contains_if__2)(__VA_ARGS__) #define contains_if_f(a, f) contains_if2(a, f) #define count_if__2(a, right) count_if2(a, lam(right)) #define count_if__3(a, v, siki) count_if2(a, [&](auto v) { return siki; }) #define count_if(...) over3(__VA_ARGS__, count_if__3, count_if__2)(__VA_ARGS__) #define count_if_f(a, f) count_if2(a, f) #define for_each__2(a, right) \ do { \ fora(v, a) { v right; } \ } while (0) #define for_each__3(a, v, siki) \ do { \ fora(v, a) { siki; } \ } while (0) #define for_each(...) over3(__VA_ARGS__, for_each__3, for_each__2)(__VA_ARGS__) #define for_each_f(a, f) \ do { \ fora(v, a) { f(v); } \ } while (0) #define erase_if__2(a, right) erase_if2(a, lam(right)) #define erase_if__3(a, v, siki) erase_if2(a, [&](auto v) { return siki; }) #define erase_if(...) over3(__VA_ARGS__, erase_if__3, erase_if__2)(__VA_ARGS__) #define erase_if_f(a, f) erase_if2(a, f) #define entry_if__2(a, right) erase_if2(a, [&](auto v) { return !(v right); }) #define entry_if__3(a, v, siki) erase_if2(a, [&](auto v) { return !(siki); }) #define entry_if(...) over3(__VA_ARGS__, entry_if__3, entry_if__2)(__VA_ARGS__) #define entry_if_f(a, f) erase_if2(a, [&](auto v) { return !f(v); }) template <class T, class U, class W> void replace(vector<W> &a, T key, U v) { rep(i, sz(a)) if (a[i] == key) a[i] = v; } template <class T, class U, class W> void replace(vector<vector<W>> &A, T key, U v) { rep(i, sz(A)) replace(A[i], key, v); } void replace(str &a, char key, str v) { if (v == "") a.erase(remove(all(a), key), a.end()); } void replace(str &a, char key, char v) { replace(all(a), key, v); } // keyと同じかどうか01で置き換える template <class T, class U> void replace(vector<T> &a, U k) { rep(i, sz(a)) a[i] = a[i] == k; } template <class T, class U> void replace(vector<vector<T>> &a, U k) { rep(i, sz(a)) rep(j, sz(a[0])) a[i][j] = a[i][j] == k; } template <class T> void replace(T &a) { replace(a, '#'); } void replace(str &a, str key, str v) { stringstream t; ll kn = sz(key); std::string::size_type Pos(a.find(key)); ll l = 0; while (Pos != std::string::npos) { t << a.substr(l, Pos - l); t << v; l = Pos + kn; Pos = a.find(key, Pos + kn); } t << a.substr(l, sz(a) - l); a = t.str(); } template <class T> bool includes(vector<T> &a, vector<T> &b) { vi c = a; vi d = b; sort(all(c)); sort(all(d)); return includes(all(c), all(d)); } template <class T> bool is_permutation(vector<T> &a, vector<T> &b) { return is_permutation(all(a), all(b)); } template <class T> bool next_permutation(vector<T> &a) { return next_permutation(all(a)); } void iota(vector<ll> &ve, ll s, ll n) { ve.resize(n); iota(all(ve), s); } vi iota(ll s, ll len) { vi ve(len); iota(all(ve), s); return ve; } template <class A, class B> auto vtop(vector<A> &a, vector<B> &b) { assert(sz(a) == sz(b)); /*stringを0で初期化できない */ vector<pair<A, B>> res; rep(i, sz(a)) res.eb(a[i], b[i]); return res; } template <class A, class B> void ptov(vector<pair<A, B>> &p, vector<A> &a, vector<B> &b) { a.resize(sz(p)), b.resize(sz(p)); rep(i, sz(p)) a[i] = p[i].fi, b[i] = p[i].se; } template <class A, class B, class C> auto vtot(vector<A> &a, vector<B> &b, vector<C> &c) { assert(sz(a) == sz(b) && sz(b) == sz(c)); vector<T2<A, B, C>> res; rep(i, sz(a)) res.eb(a[i], b[i], c[i]); return res; } template <class A, class B, class C, class D> auto vtof(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { assert(sz(a) == sz(b) && sz(b) == sz(c) && sz(c) == sz(d)); vector<F2<A, B, C, D>> res; rep(i, sz(a)) res.eb(a[i], b[i], c[i], d[i]); return res; } enum pcomparator { fisi, fisd, fdsi, fdsd, sifi, sifd, sdfi, sdfd }; enum tcomparator { fisiti, fisitd, fisdti, fisdtd, fdsiti, fdsitd, fdsdti, fdsdtd, fitisi, fitisd, fitdsi, fitdsd, fdtisi, fdtisd, fdtdsi, fdtdsd, sifiti, sifitd, sifdti, sifdtd, sdfiti, sdfitd, sdfdti, sdfdtd, sitifi, sitifd, sitdfi, sitdfd, sdtifi, sdtifd, sdtdfi, sdfdfd, tifisi, tifisd, tifdsi, tifdsd, tdfisi, tdfisd, tdfdsi, tdfdsd, tisifi, tisifd, tisdfi, tisdfd, tdsifi, tdsifd, tdsdfi, tdsdfd }; template <class A, class B> void sort(vector<pair<A, B>> &a, pcomparator type) { typedef pair<A, B> U; if (type == fisi) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se < r.se; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se > r.se; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se < r.se; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se > r.se; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi < r.fi; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi > r.fi; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi < r.fi; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi > r.fi; }); }; template <class U> void sort(vector<U> &a, pcomparator type) { if (type == fisi) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f > r.f; }); }; template <class A, class B, class C, class D> void sort(vector<F2<A, B, C, D>> &a, pcomparator type) { typedef F2<A, B, C, D> U; if (type == fisi) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a > r.a; }); }; template <class U> void sort(vector<U> &a, tcomparator type) { if (type == 0) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t < r.t; }); else if (type == 1) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t > r.t; }); else if (type == 2) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t < r.t; }); else if (type == 3) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t > r.t; }); else if (type == 4) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t < r.t; }); else if (type == 5) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t > r.t; }); else if (type == 6) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t < r.t; }); else if (type == 7) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t > r.t; }); else if (type == 8) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s < r.s; }); else if (type == 9) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s > r.s; }); else if (type == 10) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s < r.s; }); else if (type == 11) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s > r.s; }); else if (type == 12) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s < r.s; }); else if (type == 13) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s > r.s; }); else if (type == 14) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s < r.s; }); else if (type == 15) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s > r.s; }); else if (type == 16) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t < r.t; }); else if (type == 17) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t > r.t; }); else if (type == 18) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t < r.t; }); else if (type == 19) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t > r.t; }); else if (type == 20) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t < r.t; }); else if (type == 21) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t > r.t; }); else if (type == 22) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t < r.t; }); else if (type == 23) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t > r.t; }); else if (type == 24) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f < r.f; }); else if (type == 25) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f > r.f; }); else if (type == 26) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f < r.f; }); else if (type == 27) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f > r.f; }); else if (type == 28) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f < r.f; }); else if (type == 29) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f > r.f; }); else if (type == 30) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f < r.f; }); else if (type == 31) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f > r.f; }); else if (type == 32) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == 33) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == 34) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == 35) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == 36) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == 37) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == 38) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == 39) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == 40) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == 41) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == 42) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == 43) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f > r.f; }); else if (type == 44) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == 45) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == 46) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == 47) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f > r.f; }); } template <class A, class B, class C, class D> void sort(vector<F2<A, B, C, D>> &a, tcomparator type) { typedef F2<A, B, C, D> U; if (type == 0) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c < r.c; }); else if (type == 1) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c > r.c; }); else if (type == 2) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c < r.c; }); else if (type == 3) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c > r.c; }); else if (type == 4) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c < r.c; }); else if (type == 5) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c > r.c; }); else if (type == 6) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c < r.c; }); else if (type == 7) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c > r.c; }); else if (type == 8) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b < r.b; }); else if (type == 9) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b > r.b; }); else if (type == 10) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b < r.b; }); else if (type == 11) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b > r.b; }); else if (type == 12) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b < r.b; }); else if (type == 13) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b > r.b; }); else if (type == 14) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b < r.b; }); else if (type == 15) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b > r.b; }); else if (type == 16) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c < r.c; }); else if (type == 17) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c > r.c; }); else if (type == 18) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c < r.c; }); else if (type == 19) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c > r.c; }); else if (type == 20) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c < r.c; }); else if (type == 21) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c > r.c; }); else if (type == 22) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c < r.c; }); else if (type == 23) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c > r.c; }); else if (type == 24) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a < r.a; }); else if (type == 25) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a > r.a; }); else if (type == 26) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a < r.a; }); else if (type == 27) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a > r.a; }); else if (type == 28) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a < r.a; }); else if (type == 29) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a > r.a; }); else if (type == 30) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a < r.a; }); else if (type == 31) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a > r.a; }); else if (type == 32) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == 33) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == 34) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == 35) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == 36) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == 37) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == 38) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == 39) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == 40) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == 41) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == 42) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == 43) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a > r.a; }); else if (type == 44) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == 45) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == 46) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == 47) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a > r.a; }); } void sort(string &a) { sort(all(a)); } void sort(int &a, int &b) { if (a > b) swap(a, b); } void sort(int &a, int &b, int &c) { sort(a, b); sort(a, c); sort(b, c); } void rsort(int &a, int &b) { if (a < b) swap(a, b); } void rsort(int &a, int &b, int &c) { rsort(a, b); rsort(a, c); rsort(b, c); } template <class T> void sort(vector<T> &a) { sort(all(a)); } // P l, P rで f(P) の形で渡す template <class U, class F> void sort(vector<U> &a, F f) { sort(all(a), [&](U l, U r) { return f(l) < f(r); }); }; template <class T> void rsort(vector<T> &a) { sort(all(a), greater<T>()); }; template <class U, class F> void rsort(vector<U> &a, F f) { sort(all(a), [&](U l, U r) { return f(l) > f(r); }); }; // F = T<T> //例えばreturn p.fi + p.se; template <class A, class B> void sortp(vector<A> &a, vector<B> &b) { auto c = vtop(a, b); sort(c); rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se; } template <class A, class B, class F> void sortp(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); sort(c, f); rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se; } template <class A, class B> void rsortp(vector<A> &a, vector<B> &b) { auto c = vtop(a, b); rsort(c); rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second; } template <class A, class B, class F> void rsortp(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); rsort(c, f); rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second; } template <class A, class B, class C> void sortt(vector<A> &a, vector<B> &b, vector<C> &c) { auto d = vtot(a, b, c); sort(d); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class F> void sortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); sort(d, f); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C> void rsortt(vector<A> &a, vector<B> &b, vector<C> &c) { auto d = vtot(a, b, c); rsort(d); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class F> void rsortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); rsort(d, f); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class D> void sortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { auto e = vtof(a, b, c, d); sort(e); rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d; } template <class A, class B, class C, class D> void rsortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { auto e = vtof(a, b, c, d); rsort(e); rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d; } // sortindex 元のvectorはソートしない template <class T> vi sorti(vector<T> &a) { auto b = a; vi ind = iota(0, sz(a)); sortp(b, ind); return ind; } /*indexの分で型が変わるためpcomparatorが必要*/ template <class T> vi sorti(vector<T> &a, pcomparator f) { auto b = a; vi ind = iota(0, sz(a)); sortp(b, ind, f); return ind; } template <class T, class F> vi sorti(vector<T> &a, F f) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(a[x]) < f(a[y]); }); return ind; } template <class T> vi rsorti(vector<T> &a) { auto b = a; vi ind = iota(0, sz(a)); rsortp(b, ind); return ind; } template <class T, class F> vi rsorti(vector<T> &a, F f) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(a[x]) > f(a[y]); }); return ind; } template <class A, class B, class F> vi sortpi(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(c[x]) < f(c[y]); }); return ind; } template <class A, class B> vi sortpi(vector<A> &a, vector<B> &b, pcomparator f) { vi ind = iota(0, sz(a)); auto c = a; auto d = b; sortt(c, d, ind, f); return ind; } template <class A, class B> vi sortpi(vector<A> &a, vector<B> &b) { return sortpi(a, b, fisi); }; template <class A, class B, class F> vi rsortpi(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(c[x]) > f(c[y]); }); return ind; } template <class A, class B> vi rsortpi(vector<A> &a, vector<B> &b) { return sortpi(a, b, fdsd); }; template <class A, class B, class C, class F> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(d[x]) < f(d[y]); }); return ind; } template <class A, class B, class C> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, pcomparator f) { vi ind = iota(0, sz(a)); auto d = vtof(a, b, c, ind); sort(d, f); rep(i, sz(a)) ind[i] = d[i].d; return ind; } template <class A, class B, class C> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { if (a[x] == a[y]) { if (b[x] == b[y]) return c[x] < c[y]; else return b[x] < b[y]; } else { return a[x] < a[y]; } }); return ind; } template <class A, class B, class C, class F> vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(d[x]) > f(d[y]); }); return ind; } template <class A, class B, class C> vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { if (a[x] == a[y]) { if (b[x] == b[y]) return c[x] > c[y]; else return b[x] > b[y]; } else { return a[x] > a[y]; } }); return ind; } template <class T> void sort2(vector<vector<T>> &a) { for (ll i = 0, n = a.size(); i < n; ++i) sort(a[i]); } template <class T> void rsort2(vector<vector<T>> &a) { for (ll i = 0, n = a.size(); i < n; ++i) rsort(a[i]); } template <class... T, class U> auto sorted(U head, T... a) { sort(head, a...); return head; } 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 W, typename T> void fill(W &xx, const T vall) { xx = vall; } template <typename W, typename T> void fill(vector<W> &vecc, const T vall) { for (auto &&vx : vecc) fill(vx, vall); } template <typename W, typename T> void fill(vector<W> &xx, ll len, const T v) { rep(i, len) xx[i] = v; } template <typename W, typename T> void fill(vector<vector<W>> &xx, int sh, int th, int sw, int tw, T v) { rep(h, sh, th) rep(w, sw, tw) xx[h][w] = v; } template <class T, class U> void fill(vector<T> &a, vi &ind, U val) { fora(v, ind) a[v] = val; } template <class W, class T> void fill(mvec<W> &xx, const T v) { fora(x, xx) fill(x, v); } template <typename A, size_t N> A sum(A (&a)[N]) { A res = 0; rep(i, N) res += a[i]; return res; } template <typename A, size_t N, size_t O> A sum(A (&a)[N][O]) { A res = 0; rep(i, N) rep(j, O) res += a[i][j]; return res; } template <typename A, size_t N, size_t O, size_t P> A sum(A (&a)[N][O][P]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) res += a[i][j][k]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A sum(A (&a)[N][O][P][Q]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) res += a[i][j][k][l]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A sum(A (&a)[N][O][P][Q][R]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) res += a[i][j][k][l][m]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A sum(A (&a)[N][O][P][Q][R][S]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) rep(n, S) res += a[i][j][k][l][m][n]; return res; } //@汎用便利関数 入力 ll in() { ll ret; cin >> ret; return ret; } string sin() { string ret; cin >> ret; return ret; } template <class T> void in(T &head) { cin >> head; } template <class T, class... U> void in(T &head, U &...tail) { cin >> head; in(tail...); } #define din1(a) \ ll a; \ cin >> a #define din2(a, b) \ ll a, b; \ cin >> a >> b #define din3(a, b, c) \ ll a, b, c; \ cin >> a >> b >> c #define din4(a, b, c, d) \ ll a, b, c, d; \ cin >> a >> b >> c >> d #define din5(a, b, c, d, e) \ ll a, b, c, d, e; \ cin >> a >> b >> c >> d >> e #define din6(a, b, c, d, e, f) \ ll a, b, c, d, e, f; \ cin >> a >> b >> c >> d >> e >> f #define din(...) \ over6(__VA_ARGS__, din6, din5, din4, din3, din2, din1)(__VA_ARGS__) #define dins1(a) \ str a; \ cin >> a #define dins2(a, b) \ str a, b; \ cin >> a >> b #define dins3(a, b, c) \ str a, b, c; \ cin >> a >> b >> c #define dins4(a, b, c, d) \ str a, b, c, d; \ cin >> a >> b >> c >> d #define dins5(a, b, c, d, e) \ str a, b, c, d, e; \ cin >> a >> b >> c >> d >> e #define dins6(a, b, c, d, e, f) \ str a, b, c, d, e, f; \ cin >> a >> b >> c >> d >> e >> f #define dins(...) \ over6(__VA_ARGS__, dins6, dins5, dins4, dins3, dins2, dins1)(__VA_ARGS__) #define din1d(a) \ din1(a); \ a-- #define din2d(a, b) \ din2(a, b); \ a--, b-- #define din3d(a, b, c) \ din3(a, b, c); \ a--, b--, c-- #define din4d(a, b, c, d) \ din4(a, b, c, d); \ a--, b--, c--, d-- #define dind(...) over4(__VA_ARGS__, din4d, din3d, din2d, din1d)(__VA_ARGS__) template <class T> void out2(T &&head) { cout << head; } template <class T, class... U> void out2(T &&head, U &&...tail) { cout << head << " "; out2(tail...); } template <class T, class... U> void out(T &&head, U &&...tail) { cout << head << " "; out2(tail...); cout << "" << endl; } template <class T> void out(T &&head) { cout << head << endl; } void out() { cout << "" << endl; } #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; } /*@formatter:on*/ 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]; if (typeid(a[i]) == typeid(P)) { ss << endl; } else { ss << " "; } } return ss.str(); } /*@formatter:off*/ 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(); } 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 = 12; H = min(H, sz(a)); rep(i, H) { ss << endl; ss << out_m2(a[i], W, U, i); } return ss.str(); } template <class T, size_t N> string out_m2(T (&a)[N]) { vector<T> b; resize(b, N); rep(i, N) { b[i] = a[i]; } return out_m2(b); } template <class T, size_t N, size_t M> string out_m2(T (&a)[N][M]) { vector<vector<T>> b; resize(b, N, M); rep(i, N) { rep(j, M) { b[i][j] = a[i][j]; } } return out_m2(b); } template <class T, size_t N, size_t M, size_t O> string out_m2(T (&a)[N][M][O]) { vector<vector<vector<T>>> b; resize(b, N, M, O); rep(i, N) { rep(j, M) { rep(k, O) { b[i][j][k] = a[i][j][k]; } } } return out_m2(b); } string out_m2(int a) { stringstream ss; ss << a; return ss.str(); } /*@formatter:on*/ template <class T> string out_m2(mvec<mvec<T>> &a, ll H = inf, ll W = inf, int key = inf) { H = min({H, sz(a), 6ll}); W = min({W, sz(a[0]), 6ll}); stringstream ss; ss << endl; // if (key == inf)ss << " *|"; else ss << " " << key << "|"; if (key == inf) ss << " *|"; else { ss << std::right << std::setw(2) << key; ss << "|"; } rep(w, -W, W) ss << std::right << std::setw(4) << w; ss << "" << endl; rep(w, W * 8 + 3) ss << "_"; ss << "" << endl; rep(h, -H, H) { ss << std::right << std::setw(2) << h << "|"; int NW = min(sz(a[h]), 6ll); rep(w, -NW, NW) { if (abs(a[h][w]) == linf) ss << " e" << ""; else ss << std::right << std::setw(4) << a[h][w]; } ss << "" << endl; } return ss.str(); } /*@formatter:on*/ template <class T> string out_m2(mvec<mvec<mvec<T>>> &a, ll H = inf, ll W = inf, ll U = inf) { stringstream ss; if (H == inf) H = 6; H = min(H, sz(a)); rep(i, -H, H) { ss << endl; ss << out_m2(a[i], W, U, i); } return ss.str(); } /*@formatter:off*/ template <class T> string out_m2(T &a) { stringstream ss; ss << a; return ss.str(); } /*@formatter:on*/ 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(); } /*@formatter:off*/ 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(); } 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 template <class T> void outl(vector<T> &a, int n = inf) { rep(i, min(n, sz(a))) cout << a[i] << endl; } //テーブルをスペースなしで出力 template <class T> void outt(vector<vector<T>> &a) { rep(i, sz(a)) { rep(j, sz(a[i])) { cout << a[i][j]; } cout << endl; } } // int型をbit表記で出力 void outb(int a) { cout << bitset<20>(a) << endl; } template <class T> void na(vector<T> &a, ll n) { a.resize(n); rep(i, n) cin >> a[i]; } #define dna(a, n) \ vi a(n); \ rep(dnai, n) cin >> a[dnai]; #define dnad(a, n) \ vi a(n); \ rep(dnai, n) cin >> a[dnai], a[dnai]--; template <class T> void nao(vector<T> &a, ll n) { a.resize(n + 1); a[0] = 0; rep(i, n) cin >> a[i + 1]; } template <class T> void naod(vector<T> &a, ll n) { a.resize(n + 1); a[0] = 0; rep(i, n) cin >> a[i + 1], a[i + 1]--; } template <class T> void nad(vector<T> &a, ll n) { a.resize(n); rep(i, n) cin >> a[i], a[i]--; } template <class T, class U> void na2(vector<T> &a, vector<U> &b, ll n) { a.resize(n); b.resize(n); rep(i, n) cin >> a[i] >> b[i]; } #define dna2(a, b, n) \ vi a(n), b(n); \ rep(dna2i, n) cin >> a[dna2i] >> b[dna2i]; template <class T, class U> void nao2(vector<T> &a, vector<U> &b, ll n) { a.resize(n + 1); b.resize(n + 1); a[0] = b[0] = 0; rep(i, n) cin >> a[i + 1] >> b[i + 1]; } #define dna2d(a, b, n) \ vi a(n), b(n); \ rep(dna2di, n) { \ cin >> a[dna2di] >> b[dna2di]; \ a[dna2di]--, b[dna2di]--; \ } template <class T, class U> void na2d(vector<T> &a, vector<U> &b, ll n) { a.resize(n); b.resize(n); rep(i, n) cin >> a[i] >> b[i], a[i]--, b[i]--; } template <class T, class U, class W> void na3(vector<T> &a, vector<U> &b, vector<W> &c, ll n) { a.resize(n); b.resize(n); c.resize(n); rep(i, n) cin >> a[i] >> b[i] >> c[i]; } #define dna3(a, b, c, n) \ vi a(n), b(n), c(n); \ rep(dna3i, n) cin >> a[dna3i] >> b[dna3i] >> c[dna3i]; template <class T, class U, class W> void na3d(vector<T> &a, vector<U> &b, vector<W> &c, ll 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 dna3d(a, b, c, n) \ vi a(n), b(n), c(n); \ rep(dna3di, n) { \ cin >> a[dna3di] >> b[dna3di] >> c[dna3di]; \ a[dna3di]--, b[dna3di]--, c[dna3di]--; \ } template <class T, class U, class W, class X> void na4(vector<T> &a, vector<U> &b, vector<W> &c, vector<X> &d, ll n) { a.resize(n); b.resize(n); c.resize(n); d.resize(n); rep(i, n) cin >> a[i] >> b[i] >> c[i] >> d[i]; } #define dna4(a, b, c, d, n) \ vi a(n), b(n), c(n), d(n); \ rep(dna4i, n) cin >> a[dna4i] >> b[dna4i] >> c[dna4i] >> d[dna4i]; #define dna4d(a, b, c, d, n) \ vi a(n), b(n), c(n), d(n); \ rep(dna4i, n) cin >> a[dna4i] >> b[dna4i] >> c[dna4i] >> d[dna4i], \ --a[dna4i], --b[dna4i], --c[dna4i], --d[dna4i]; #define nt(a, h, w) \ resize(a, h, w); \ rep(nthi, h) rep(ntwi, w) cin >> a[nthi][ntwi]; #define ntd(a, h, w) \ resize(a, h, w); \ rep(ntdhi, h) rep(ntdwi, w) cin >> a[ntdhi][ntdwi], a[ntdhi][ntdwi]--; #define ntp(a, h, w) \ resize(a, h + 2, w + 2); \ fill(a, '#'); \ rep(ntphi, 1, h + 1) rep(ntpwi, 1, w + 1) cin >> a[ntphi][ntpwi]; //デバッグ #define sp << " " << #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__) #ifdef _DEBUG #define deb(...) \ cerr << over10(__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' //@formatter:off //よく使うクラス、構造体 // graphでredefineしているため、書き換えたら向こうも書き換える struct unionfind { vector<ll> par; vector<ll> siz; vector<ll> es; ll n, trees; //連結グループの数(親の種類) unionfind(ll n) : n(n), trees(n) { par.resize(n); siz.resize(n); es.resize(n); for (ll i = 0; i < n; i++) { par[i] = i; siz[i] = 1; } } ll root(ll x) { if (par[x] == x) { return x; } else { return par[x] = root(par[x]); } } ll operator()(ll x) { return root(x); } bool unite(ll x, ll y) { x = root(x); y = root(y); es[x]++; if (x == y) return false; if (siz[x] > siz[y]) swap(x, y); trees--; par[x] = y; siz[y] += siz[x]; es[y] += es[x]; return true; } bool same(ll x, ll y) { return root(x) == root(y); } ll size(ll x) { return siz[root(x)]; } ll esize(ll x) { return es[root(x)]; } vi sizes() { vi cou(n); vi ret; ret.reserve(n); rep(i, n) { cou[root(i)]++; } rep(i, n) { if (cou[i]) ret.push_back(cou[i]); } return ret; } //つながりを無向グラフと見なし、xが閉路に含まれるか判定 bool close(ll x) { return esize(x) >= size(x); } vector<vi> sets() { vi ind(n, -1); ll i = 0; vvi(res, trees); rep(j, n) { ll r = root(j); if (ind[r] == -1) ind[r] = i++; res[ind[r]].push_back(j); } rep(i, trees) { ll r = root(res[i][0]); if (res[i][0] == r) continue; rep(j, 1, sz(res[i])) { if (res[i][j] == r) { swap(res[i][0], res[i][j]); break; } } } return res; } }; //@formatter:off using bll = __int128; using u32 = unsigned; using u64 = unsigned long long; using u128 = __uint128_t; std::ostream &operator<<(std::ostream &dest, __int128_t value) { std::ostream::sentry s(dest); if (s) { __uint128_t tmp = value < 0 ? -value : value; char buffer[128]; char *d = std::end(buffer); do { --d; *d = "0123456789"[tmp % 10]; tmp /= 10; } while (tmp != 0); if (value < 0) { --d; *d = '-'; } ll len = std::end(buffer) - d; if (dest.rdbuf()->sputn(d, len) != len) { dest.setstate(std::ios_base::badbit); } } return dest; } //__int128 toi128(string &s) { __int128 ret = 0; for (ll i = 0; i < //s.length(); ++i) if ('0' <= s[i] && s[i] <= '9') ret = 10 * //ret + s[i] - '0'; return ret;} //エラー 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 re() { assert(0 == 1); exit(0); } void tle() { while (inf) cout << inf << endl; } //便利関数 //テスト用 #define rand xor128_ unsigned long xor128_(void) { static unsigned long x = 123456789, y = 362436069, z = 521288629, w = 88675123; unsigned long t; t = (x ^ (x << 11)); x = y; y = z; z = w; return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8))); } char ranc() { return (char)('a' + rand() % 26); } ll rand(ll min, ll max) { assert(min <= max); if (min >= 0 && max >= 0) { return rand() % (max + 1 - min) + min; } else if (max < 0) { return -rand(-max, -min); } else { if (rand() % 2) { return rand(0, max); } else { return -rand(0, -min); } } } vi ranv(ll n, ll min, ll max) { vi v(n); rep(i, n) v[i] = rand(min, max); return v; } str ransu(ll n) { str s; rep(i, n) s += (char)rand('A', 'Z'); return s; } str ransl(ll n) { str s; rep(i, n) s += (char)rand('a', 'z'); return s; } //単調増加 vi ranvinc(ll n, ll min, ll max) { vi v(n); bool bad = 1; while (bad) { bad = 0; v.resize(n); rep(i, n) { if (i && min > max - v[i - 1]) { bad = 1; break; } if (i) v[i] = v[i - 1] + rand(min, max - v[i - 1]); else v[i] = rand(min, max); } } return v; } //便利 汎用 void ranvlr(ll n, ll min, ll max, vi &l, vi &r) { l.resize(n); r.resize(n); rep(i, n) { l[i] = rand(min, max); r[i] = l[i] + rand(0, max - l[i]); } } template <class T> vector<pair<T, int>> run_length(vector<T> &a) { vector<pair<T, int>> 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 <class F> ll mgr(ll ok, ll ng, F f) { bool han = true; if (ok < ng) while (ng - ok > 1) { ll mid = (ok + ng) >> 1; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; deb(mid, han); } else while (ok - ng > 1) { ll mid = (ok + ng) >> 1; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; deb(mid, han); } return ok; } template <class F> dou mgrd(dou ok, dou ng, F f, int kai = 100) { bool han = true; if (ok < ng) rep(i, kai) { dou mid = (ok + ng) / 2; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; deb(mid, han); } else rep(i, kai) { dou mid = (ok + ng) / 2; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; deb(mid, han); } return ok; } // strを整数として比較 string smax(str &a, str b) { if (sz(a) < sz(b)) { return b; } else if (sz(a) > sz(b)) { return a; } else if (a < b) return b; else return a; } // strを整数として比較 string smin(str &a, str b) { if (sz(a) > sz(b)) { return b; } else if (sz(a) < sz(b)) { return a; } else if (a > b) return b; else return a; } //エラー-1 template <typename W, typename T> ll find(vector<W> &a, int l, const T key) { rep(i, l, sz(a)) if (a[i] == key) return i; return -1; } template <typename W, typename T> ll find(vector<W> &a, const T key) { rep(i, sz(a)) if (a[i] == key) return i; return -1; } template <typename W, typename T> P find(vector<vector<W>> &a, const T key) { rep(i, sz(a)) rep(j, sz(a[0])) if (a[i][j] == key) return mp(i, j); return mp(-1, -1); } template <typename W, typename U> T find(vector<vector<vector<W>>> &a, const U key) { rep(i, sz(a)) rep(j, sz(a[0])) rep(k, sz(a[0][0])) if (a[i][j][k] == key) return mt(i, j, k); return mt(-1, -1, -1); } // stringも書く int find(string &s, const string key) { int klen = sz(key); rep(i, sz(s) - klen + 1) { if (s[i] != key[0]) continue; if (s.substr(i, klen) == key) { return i; } } return -1; } int find(string &s, int l, const string key) { int klen = sz(key); rep(i, l, sz(s) - klen + 1) { if (s[i] != key[0]) continue; if (s.substr(i, klen) == key) { return i; } } return -1; } int find(string &s, const char key) { rep(i, sz(s)) { if (s[i] == key) return i; } return -1; } int find(string &s, int l, const char key) { rep(i, l, sz(s)) { if (s[i] == key) return i; } return -1; } template <typename W, typename T> ll count2(W &a, const T k) { return a == k; } template <typename W, typename T> ll count2(vector<W> &a, const T k) { ll ret = 0; fora(v, a) ret += count2(v, k); return ret; } template <typename W, typename T> ll count(vector<W> &a, const T k) { ll ret = 0; fora(v, a) ret += count2(v, k); return ret; } vi count(vi &a) { int ma = 0; fora(v, a) { if (ma < v) ma = v; } vi res(ma + 1); fora(v, a) { res[v]++; } return res; } ll count(str &a, str k) { ll ret = 0, len = k.length(); auto pos = a.find(k); while (pos != string::npos) pos = a.find(k, pos + len), ++ret; return ret; } vi count(str &a) { vi cou(26); char c = 'a'; if ('A' <= a[0] && a[0] <= 'Z') c = 'A'; rep(i, sz(a))++ cou[a[i] - c]; return cou; } #define couif count_if // algorythm 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)); } template <class U> void rev(vector<vector<U>> &a) { vector<vector<U>> b(sz(a[0]), vector<U>(sz(a))); rep(h, sz(a)) rep(w, sz(a[0])) b[w][h] = a[h][w]; a = b; } void rev(string &a) { reverse(all(a)); } constexpr ll p10[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000ll, 100000000000ll, 1000000000000ll, 10000000000000ll, 100000000000000ll, 1000000000000000ll, 10000000000000000ll, 100000000000000000ll, 1000000000000000000ll}; ll get(ll a, ll keta) { return (a / (ll)pow(10, keta)) % 10; } // 0は0桁 ll keta(ll v) { if (v < p10[9]) { if (v < p10[4]) { if (v < p10[2]) { if (v < p10[1]) { if (v < p10[0]) return 0; else return 1; } else return 2; } else { if (v < p10[3]) return 3; else return 4; } } else { if (v < p10[7]) { if (v < p10[5]) return 5; else if (v < p10[6]) return 6; else return 7; } else { if (v < p10[8]) return 8; else return 9; } } } else { if (v < p10[13]) { if (v < p10[11]) { if (v < p10[10]) return 10; else return 11; } else { if (v < p10[12]) return 12; else return 13; } } else { if (v < p10[15]) { if (v < p10[14]) return 14; else return 15; } else { if (v < p10[17]) { if (v < p10[16]) return 16; else return 17; } else { if (v < p10[18]) return 18; else return 19; } } } } } ll dsum(ll v, ll sin = 10) { ll ret = 0; for (; v; v /= sin) ret += v % sin; return ret; } ll mask10(ll v) { return p10[v] - 1; } //変換系 //[v] := iとなるようなvectorを返す //存在しない物は-1 template <class T> auto keys(T &a) { vector<decltype((a.begin())->fi)> res; for (auto &&k : a) res.push_back(k.fi); return res; } template <class T> auto values(T &a) { vector<decltype((a.begin())->se)> res; for (auto &&k : a) res.push_back(k.se); return res; } template <class T, class U> bool chma(T &a, const U &b) { if (a < b) { a = b; return true; } return false; } template <class T, class U> bool chmi(T &a, const U &b) { if (b < a) { a = b; return true; } return false; } template <class T> constexpr T min(T a, signed b) { return a < b ? a : b; } template <class T> constexpr T max(T a, signed b) { return a < b ? b : a; } template <class T> constexpr T min(T a, T b, T c) { return a >= b ? b >= c ? c : b : a >= c ? c : a; } template <class T> constexpr T max(T a, T b, T c) { return a <= b ? b <= c ? c : b : a <= c ? c : a; } template <class T> T min(vector<T> &a) { return *min_element(all(a)); } template <class T> T mini(vector<T> &a) { return min_element(all(a)) - a.begin(); } template <class T> T min(vector<T> &a, ll n) { return *min_element(a.begin(), a.begin() + min(n, sz(a))); } template <class T> T min(vector<T> &a, ll s, ll n) { return *min_element(a.begin() + s, a.begin() + min(n, sz(a))); } template <class T> T max(vector<T> &a) { return *max_element(all(a)); } template <class T, class U> T max(vector<T> &a, vector<U> &b) { return max(*max_element(all(a)), *max_element(all(b))); } template <class T> T maxi(vector<T> &a) { return max_element(all(a)) - a.begin(); } template <class T> T max(vector<T> &a, ll n) { return *max_element(a.begin(), a.begin() + min(n, sz(a))); } template <class T> T max(vector<T> &a, ll s, ll n) { return *max_element(a.begin() + s, a.begin() + min(n, sz(a))); } template <typename A, size_t N> A max(A (&a)[N]) { A res = a[0]; rep(i, N) res = max(res, a[i]); return res; } template <typename A, size_t N, size_t O> A max(A (&a)[N][O]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P> A max(A (&a)[N][O][P]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A max(A (&a)[N][O][P][Q], const T &v) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A max(A (&a)[N][O][P][Q][R]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A max(A (&a)[N][O][P][Q][R][S]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N> A min(A (&a)[N]) { A res = a[0]; rep(i, N) res = min(res, a[i]); return res; } template <typename A, size_t N, size_t O> A min(A (&a)[N][O]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P> A min(A (&a)[N][O][P]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A min(A (&a)[N][O][P][Q], const T &v) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A min(A (&a)[N][O][P][Q][R]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A min(A (&a)[N][O][P][Q][R][S]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <class T> T sum(vector<T> &v, ll s, ll t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += v[i]; return ret; } template <class T> T sum(vector<T> &v, ll t = inf) { return sum(v, 0, t); } template <class T> T sum(vector<vector<T>> &v, int s, int t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<T>> &v, int t = inf) { return sum(v, 0, t); } template <class T> T sum(vector<vector<vector<T>>> &v, int s, int t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<T>>> &v, int t = inf) { return sum(v, 0, t); } template <class T> T sum(vector<vector<vector<vector<T>>>> &v, int s, int t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<vector<T>>>> &v, int t = inf) { return sum(v, 0, t); } template <class T> T sum(vector<vector<vector<vector<vector<T>>>>> &v, int s, int t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<vector<vector<T>>>>> &v, int t = inf) { return sum(v, 0, t); } template <class T> auto sum(priority_queue<T, vector<T>, greater<T>> &r) { auto q = r; T ret = 0; while (sz(q)) { ret += q.top(); q.pop(); } return ret; } template <class T> auto sum(priority_queue<T> &r) { auto q = r; T ret = 0; while (sz(q)) { ret += q.top(); q.pop(); } return ret; } // template<class T, class U, class... W> auto sumn(vector<T> &v, U head, W... // tail) { auto ret = sum(v[0], tail...); rep(i, 1, min(sz(v), head))ret += // sum(v[i], tail...); return ret;} vi v_i(vi &a) { int n = max(a) + 1; vi ret(n, -1); rep(i, sz(a)) { ret[a[i]] = i; } return ret; } void clear(PQ &q) { q = PQ(); } void clear(priority_queue<int> &q) { q = priority_queue<int>(); } template <class T> void clear(queue<T> &q) { while (q.size()) q.pop(); } template <class T> T *negarr(ll size) { T *body = (T *)malloc((size * 2 + 1) * sizeof(T)); return body + size; } template <class T> T *negarr2(ll h, ll w) { double **dummy1 = new double *[2 * h + 1]; double *dummy2 = new double[(2 * h + 1) * (2 * w + 1)]; dummy1[0] = dummy2 + w; for (ll i = 1; i <= 2 * h + 1; ++i) { dummy1[i] = dummy1[i - 1] + 2 * w + 1; } double **a = dummy1 + h; return a; } // 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> vector<T> ima(vector<T> &v) { vector<T> ret = v; rep(i, sz(ret) - 1) chma(ret[i + 1], ret[i]); return ret; } // template<class T> vector<T> rimi(vector<T> &v) { vector<T> ret = v; rer(i, // sz(ret) - 1, 1)chmi(ret[i - 1], ret[i]); return ret;} template<class T> // vector<T> rima(vector<T> &v) { vector<T> ret = v; rer(i, sz(ret) - 1, // 1)chma(ret[i - 1], ret[i]); return ret;} template <class T> struct ruiC { vector<T> rui; ruiC(vector<T> &ru) : rui(ru) {} /*先頭0*/ ruiC() : rui(1, 0) {} T operator()(ll l, ll r) { if (l > r) { cerr << "ruic "; deb(l, r); assert(0); } return rui[r] - rui[l]; } T operator()(int r) { return operator()(0, r); } /*ruiv[]をruic[]に変えた際意味が変わるのがまずいため()と統一*/ /*単体iを返す 累積でないことに注意(seg木との統一でこうしている)*/ // T operator[](ll i) { return rui[i + 1] - rui[i]; } T operator[](ll i) { return rui[i]; } /*0から順に追加される必要がある*/ void operator+=(T v) { rui.push_back(rui.back() + v); } void add(int i, T v) { if (sz(rui) - 1 != i) ole(); operator+=(v); } T back() { return rui.back(); } ll size() { return rui.size(); } auto begin() { return rui.begin(); } auto end() { return rui.end(); } }; template <class T> struct ruimax { template <typename Monoid> struct SegmentTree { /*pairで処理*/ int sz; vector<Monoid> seg; const Monoid M1 = mp(MIN(T), -1); Monoid f(Monoid a, Monoid b) { return max(a, b); } void build(vector<T> &a) { int n = sz(a); sz = 1; while (sz < n) sz <<= 1; seg.assign(2 * sz, M1); rep(i, n) { seg[i + sz] = mp(a[i], i); } for (int k = sz - 1; k > 0; k--) { seg[k] = f(seg[k << 1], seg[(k << 1) | 1]); } } Monoid query(int a, int b) { Monoid L = M1, R = M1; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) L = f(L, seg[a++]); if (b & 1) R = f(seg[--b], R); } return f(L, R); } Monoid operator[](const int &k) const { return seg[k + sz]; } }; private: vector<T> ve; SegmentTree<pair<T, int>> seg; vector<T> rv; vector<T> ri; bool build = false; public: int n; ruimax(vector<T> &a) : ve(a), n(sz(a)) { int index = -1; int ma = MIN(T); rv.resize(n + 1); ri.resize(n + 1); rv[0] = -linf; ri[0] = -1; rep(i, n) { if (chma(ma, a[i])) { index = i; } rv[i + 1] = ma; ri[i + 1] = index; } } T operator()(int l, int r) { if (!(l <= r && 0 <= l && r <= n)) { deb(l, r, n); assert(0); } if (l == 0) { return rv[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).first; } } T operator()(int r = inf) { return operator()(0, min(r, n)); } T operator[](int r) { return operator()(0, r); } T getv(int l, int r) { return operator()(l, r); } T geti(int l, int r) { assert(l <= r && 0 <= l && r <= n); if (l == 0) { return ri[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).second; } } T geti(int r = inf) { return geti(0, min(r, n)); }; T getv(int r = inf) { return getv(0, min(r, n)); }; auto begin() { return rv.begin(); } auto end() { return rv.end(); } }; template <class T> struct ruimin { template <typename Monoid> struct SegmentTree { /*pairで処理*/ int sz; vector<Monoid> seg; const Monoid M1 = mp(MAX(T), -1); Monoid f(Monoid a, Monoid b) { return min(a, b); } void build(vector<T> &a) { int n = sz(a); sz = 1; while (sz < n) sz <<= 1; seg.assign(2 * sz, M1); rep(i, n) { seg[i + sz] = mp(a[i], i); } for (int k = sz - 1; k > 0; k--) { seg[k] = f(seg[k << 1], seg[(k << 1) | 1]); } } Monoid query(int a, int b) { Monoid L = M1, R = M1; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) L = f(L, seg[a++]); if (b & 1) R = f(seg[--b], R); } return f(L, R); } Monoid operator[](const int &k) const { return seg[k + sz]; } }; private: vector<T> ve; SegmentTree<pair<T, int>> seg; vector<T> rv; vector<T> ri; bool build = false; int n; public: ruimin(vector<T> &a) : ve(a), n(sz(a)) { int index = -1; int mi = MAX(T); rv.resize(n + 1); ri.resize(n + 1); rv[0] = linf; ri[0] = -1; rep(i, n) { if (chmi(mi, a[i])) { index = i; } rv[i + 1] = mi; ri[i + 1] = index; } } T operator()(int l, int r) { assert(l <= r && 0 <= l && r <= n); if (l == 0) { return rv[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).first; } } T operator()(int r = inf) { return operator()(0, min(r, n)); } T operator[](int r) { return operator()(0, r); } T getv(int l, int r) { return operator()(l, r); } T geti(int l, int r) { assert(l <= r && 0 <= l && r <= n); if (l == 0) { return ri[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).second; } } T geti(int r = inf) { return geti(0, min(r, n)); }; T getv(int r = inf) { return getv(0, min(r, n)); }; auto begin() { return rv.begin(); } auto end() { return rv.end(); } }; 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() { return ruiC<T>(); } template <class T> ruiC<T> ruic(vector<T> &a) { vector<T> ret = ruiv(a); return ruiC<T>(ret); } vvi() ruib(vi &a) { vvi(res, 61, sz(a) + 1); rep(k, 61) { rep(i, sz(a)) { res[k][i + 1] = res[k][i] + ((a[i] >> k) & 1); } } return res; } vector<ruiC<int>> ruibc(vi &a) { vector<ruiC<int>> ret(61); vvi(res, 61, sz(a)); rep(k, 61) { rep(i, sz(a)) { res[k][i] = (a[i] >> k) & 1; } ret[k] = ruic(res[k]); } return 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); } // h query template <class T> vector<T> imoh(vector<vector<T>> &v, int w) { vector<T> ret(sz(v)); rep(h, sz(ret)) { ret[h] = v[h][w]; } rep(i, sz(ret) - 1) { ret[i + 1] += ret[i]; } return ret; } template <class T> vector<T> ruih(vector<vector<T>> &v, int w) { vector<T> ret(sz(v) + 1); rep(h, sz(v)) { ret[h + 1] = v[h][w]; } rep(i, sz(v)) { ret[i + 1] += ret[i]; } return ret; } template <class T> ruiC<T> ruihc(vector<vector<T>> &a, int w) { vector<T> ret = ruih(a, w); return ruiC<T>(ret); } // xor template <class T> struct ruixC { 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> struct rruiC { vector<T> rui; int n; rruiC(vector<T> &a) : n(sz(a)) { rui.resize(n + 1); rer(i, n - 1) { rui[i] = rui[i + 1] + a[i]; } } /*[r l)*/ T operator()(int r, int l) { r++; l++; assert(l <= r && l >= 0 && r <= n); return rui[l] - rui[r]; } T operator()(int l) { return operator()(n - 1, l); } T operator[](int i) { return operator()(i); } }; template <class T> ostream &operator<<(ostream &os, rruiC<T> a) { fora(v, a.rui) os << v << " "; return os; } #define rrui rruic template <class T> rruiC<T> rruic(vector<T> &a) { return rruiC<T>(a); } //掛け算 template <class T> struct ruimulC { vector<T> rv; int n; ruimulC(vector<T> &a) : rv(a), n(sz(a)) { rv.resize(n + 1); rv[0] = 1; rep(i, n) { rv[i + 1] = a[i] * rv[i]; } } ruimulC() : n(0) { rv.resize(n + 1); rv[0] = 1; } void operator+=(T v) { rv.push_back(rv.back() * v); n++; } T operator()(int l, int r) { assert(l <= r && 0 <= l && r <= n); return rv[r] / rv[l]; } T operator()(int r = inf) { return operator()(0, min(r, n)); } T operator[](int r) { return operator()(0, r); } auto begin() { return rv.begin(); } auto end() { return rv.end(); } }; template <class T> ruimulC<T> ruimul(vector<T> &a) { return ruimulC<T>(a); } template <class T> ruimulC<T> ruimul() { vector<T> a; return ruimulC<T>(a); } /*@formatter:off*/ 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> void inc(pair<T, U> &a, U v = 1) { a.first += v, a.second += v; } template <class T, class U> void inc(T &a, U v = 1) { a += v; } template <class T, class U> void inc(vector<T> &a, U v = 1) { for (auto &u : a) inc(u, v); } template <class T, class U> void dec(T &a, U v = 1) { a -= v; } template <class T, class U> void dec(vector<T> &a, U v = 1) { for (auto &u : a) dec(u, v); } template <class U> void dec(string &a, U v = 1) { for (auto &u : a) dec(u, v); } template <class T, class U, class W> void dec(vector<T> &a, vector<U> &b, W v = 1) { for (auto &u : a) dec(u, v); for (auto &u : b) dec(u, v); } template <class T, class U, class W> void dec(vector<T> &a, vector<U> &b, vector<W> &c) { for (auto &u : a) dec(u, 1); for (auto &u : b) dec(u, 1); for (auto &u : c) dec(u, 1); } bool ins(ll h, ll w, ll H, ll W) { return h >= 0 && w >= 0 && h < H && w < W; } bool ins(ll l, ll v, ll r) { return l <= v && v < r; } template <class T> bool ins(vector<T> &a, ll i, ll j = 0) { return ins(0, i, sz(a)) && ins(0, j, sz(a)); } #define inside ins ll u(ll a) { return a < 0 ? 0 : a; } template <class T> vector<T> u(vector<T> &a) { vector<T> ret = a; fora(v, ret) v = u(v); return ret; } //添え字を返す template <class F> ll goldd_l(ll left, ll right, F calc) { double GRATIO = 1.6180339887498948482045868343656; ll lm = left + (ll)((right - left) / (GRATIO + 1.0)); ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); ll fl = calc(lm); ll fr = calc(rm); while (right - left > 10) { if (fl < fr) { right = rm; rm = lm; fr = fl; lm = left + (ll)((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } ll minScore = MAX(ll); ll resIndex = left; for (ll i = left; i < right + 1; ++i) { ll score = calc(i); if (minScore > score) { minScore = score; resIndex = i; } } return resIndex; } template <class F> ll goldt_l(ll left, ll right, F calc) { double GRATIO = 1.6180339887498948482045868343656; ll lm = left + (ll)((right - left) / (GRATIO + 1.0)); ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); ll fl = calc(lm); ll fr = calc(rm); while (right - left > 10) { if (fl > fr) { right = rm; rm = lm; fr = fl; lm = left + (ll)((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } if (left > right) { ll l = left; left = right; right = l; } ll maxScore = MIN(ll); ll resIndex = left; for (ll i = left; i < right + 1; ++i) { ll score = calc(i); if (maxScore < score) { maxScore = score; resIndex = i; } } return resIndex; } /*loopは200にすればおそらく大丈夫 余裕なら300に*/ template <class F> dou goldd_d(dou left, dou right, F calc, ll loop = 200) { dou GRATIO = 1.6180339887498948482045868343656; dou lm = left + ((right - left) / (GRATIO + 1.0)); dou rm = lm + ((right - lm) / (GRATIO + 1.0)); dou fl = calc(lm); dou fr = calc(rm); /*200にすればおそらく大丈夫*/ /*余裕なら300に*/ ll k = 141; loop++; while (--loop) { if (fl < fr) { right = rm; rm = lm; fr = fl; lm = left + ((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + ((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } return left; } template <class F> dou goldt_d(dou left, dou right, F calc, ll loop = 200) { double GRATIO = 1.6180339887498948482045868343656; dou lm = left + ((right - left) / (GRATIO + 1.0)); dou rm = lm + ((right - lm) / (GRATIO + 1.0)); dou fl = calc(lm); dou fr = calc(rm); loop++; while (--loop) { if (fl > fr) { right = rm; rm = lm; fr = fl; lm = left + ((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + ((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } return left; } // l ~ rを複数の区間に分割し、極致を与えるiを返す time-20 msまで探索 template <class F> ll goldd_ls(ll l, ll r, F calc, ll time = 2000) { auto lim = milliseconds(time - 20); ll mini = 0, minv = MAX(ll); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/ ll nl = l; ll nr = l + haba; rep(i, k) { ll ni = goldd_l(nl, nr, calc); if (chmi(minv, calc(ni))) mini = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return mini; } template <class F> ll goldt_ls(ll l, ll r, F calc, ll time = 2000) { auto lim = milliseconds(time - 20); ll maxi = 0, maxv = MIN(ll); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/ ll nl = l; ll nr = l + haba; rep(i, k) { ll ni = goldt_l(nl, nr, calc); if (chma(maxv, calc(ni))) maxi = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return maxi; } template <class F> dou goldd_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を持つ*/ auto lim = milliseconds(time - 20); dou mini = 0, minv = MAX(dou); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); dou haba = (r - l) / k; dou nl = l; dou nr = l + haba; rep(i, k) { dou ni = goldd_d(nl, nr, calc); if (chmi(minv, calc(ni))) mini = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return mini; } template <class F> dou goldt_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を残している*/ auto lim = milliseconds(time - 20); dou maxi = 0, maxv = MIN(dou); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); dou haba = (r - l) / k; dou nl = l; dou nr = l + haba; rep(i, k) { dou ni = goldt_d(nl, nr, calc); if (chma(maxv, calc(ni))) maxi = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return maxi; } template <class T> T min(vector<vector<T>> &a) { T res = MAX(T); rep(i, a.size()) chmi(res, *min_element(all(a[i]))); return res; } template <class T> T max(vector<vector<T>> &a) { T res = MIN(T); rep(i, a.size()) chma(res, *max_element(all(a[i]))); return res; } constexpr bool bget(ll m, ll keta) { #ifdef _DEBUG assert(keta <= 62); //オーバーフロー 1^62までしか扱えない #endif return (m >> keta) & 1; } // bget(n)次元 // NならN-1まで vector<vi> bget2(vi &a, int keta_size) { vvi(res, keta_size, sz(a)); rep(k, keta_size) { rep(i, sz(a)) { res[k][i] = bget(a[i], k); } } return res; } vi bget1(vi &a, int keta) { vi res(sz(a)); rep(i, sz(a)) { res[i] = bget(a[i], keta); } return res; } ll bget(ll m, ll keta, ll sinsuu) { m /= (ll)pow(sinsuu, keta); return m % sinsuu; } constexpr ll bit(ll n) { #ifdef _DEBUG assert(n <= 62); //オーバーフロー 1^62までしか扱えない #endif return (1LL << (n)); } ll bit(ll n, ll sinsuu) { return (ll)pow(sinsuu, n); } ll mask(ll n) { return (1ll << n) - 1; } // aをbitに置きなおす //{0, 2} -> 101 ll biti(vi &a) { int m = 0; for (auto &&v : a) m |= bit(v); return m; } //{1, 1, 0} -> 011 ll bitb(vi &a) { int m = 0; rep(i, sz(a)) if (a[i]) m |= bit(i); return m; } #define bcou __builtin_popcountll //最下位ビット ll lbit(ll n) { return n & -n; } ll lbiti(ll n) { return log2(n & -n); } //最上位ビット ll hbit(ll n) { n |= (n >> 1); n |= (n >> 2); n |= (n >> 4); n |= (n >> 8); n |= (n >> 16); n |= (n >> 32); return n - (n >> 1); } ll hbiti(ll n) { return log2(hbit(n)); } ll hbitk(ll n) { ll k = 0; rer(i, 5) { ll a = k + (1ll << i); ll b = 1ll << a; if (b <= n) k += 1ll << i; } return k; } //初期化は0を渡す ll nextComb(ll &mask, ll n, ll r) { if (!mask) return mask = (1LL << r) - 1; ll x = mask & -mask; /*最下位の1*/ ll y = mask + x; /*連続した下の1を繰り上がらせる*/ ll res = ((mask & ~y) / x >> 1) | y; if (bget(res, n)) return mask = 0; else return mask = res; } // n桁以下でビットがr個立っているもののvectorを返す vi bitCombList(ll n, ll r) { vi res; ll m = 0; while (nextComb(m, n, r)) { res.push_back(m); } return res; } // masの立ってるindexを見る #define forbit(i, mas) \ for (int forbitj = lbit(mas), forbitm = mas, i = log2(forbitj); forbitm; \ forbitm = forbitj ? forbitm ^ forbitj : 0, forbitj = lbit(forbitm), \ i = log2(forbitj)) char itoal(ll i) { return 'a' + i; } char itoaL(ll i) { return 'A' + i; } ll altoi(char c) { if ('A' <= c && c <= 'Z') return c - 'A'; return c - 'a'; } ll ctoi(char c) { return c - '0'; } char itoc(ll i) { return i + '0'; } ll vtoi(vi &v) { ll res = 0; if (sz(v) > 18) { debugline("vtoi"); deb(sz(v)); ole(); } rep(i, sz(v)) { res *= 10; res += v[i]; } return res; } vi itov(ll i) { vi res; while (i) { res.push_back(i % 10); i /= 10; } rev(res); return res; } vi stov(string &a) { ll n = sz(a); vi ret(n); rep(i, n) { ret[i] = a[i] - '0'; } return ret; } //基準を満たさないものは0になる vi stov(string &a, char one) { ll n = sz(a); vi ret(n); rep(i, n) ret[i] = a[i] == one; return ret; } vector<vector<ll>> ctoi(vector<vector<char>> s, char c) { ll n = sz(s), m = sz(s[0]); vector<vector<ll>> res(n, vector<ll>(m)); rep(i, n) rep(j, m) res[i][j] = s[i][j] == c; return res; } #define unique(v) v.erase(unique(v.begin(), v.end()), v.end()); //[i] := vを返す // aは0~n-1で置き換えられる vi compress(vi &a) { vi b; ll len = a.size(); for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { a[i] = lower_bound(all(b), a[i]) - b.begin(); } ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, umap<ll, ll> &map) { vi b; ll len = a.size(); for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { ll v = a[i]; a[i] = lower_bound(all(b), a[i]) - b.begin(); map[v] = a[i]; } ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, vi &r) { vi b; ll len = a.size(); fora(v, a) b.push_back(v); fora(v, r) b.push_back(v); sort(b); unique(b); for (ll i = 0; i < len; ++i) a[i] = lower_bound(all(b), a[i]) - b.begin(); for (ll i = 0; i < sz(r); ++i) r[i] = lower_bound(all(b), r[i]) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, vi &r, vi &s) { vi b; ll len = a.size(); fora(v, a) b.push_back(v); fora(v, r) b.push_back(v); fora(v, s) b.push_back(v); sort(b); unique(b); for (ll i = 0; i < len; ++i) a[i] = lower_bound(all(b), a[i]) - b.begin(); for (ll i = 0; i < sz(r); ++i) r[i] = lower_bound(all(b), r[i]) - b.begin(); for (ll i = 0; i < sz(s); ++i) r[i] = lower_bound(all(b), s[i]) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vector<vi> &a) { vi b; fora(vv, a) fora(v, vv) b.push_back(v); sort(b); unique(b); fora(vv, a) fora(v, vv) v = lower_bound(all(b), v) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vector<vector<vi>> &a) { vi b; fora(vvv, a) fora(vv, vvv) fora(v, vv) b.push_back(v); sort(b); unique(b); fora(vvv, a) fora(vv, vvv) fora(v, vv) v = lower_bound(all(b), v) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } void compress(ll a[], ll len) { vi b; for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { a[i] = lower_bound(all(b), a[i]) - b.begin(); } } //要素が見つからなかったときに困る #define binarySearch(a, v) (binary_search(all(a), v)) #define lowerIndex(a, v) (lower_bound(all(a), v) - a.begin()) #define upperIndex(a, v) (upper_bound(all(a), v) - a.begin()) #define rlowerIndex(a, v) (upper_bound(all(a), v) - a.begin() - 1) #define rupperIndex(a, v) (lower_bound(all(a), v) - a.begin() - 1) template <class T, class U, class W> T lowerBound(vector<T> &a, U v, W banpei) { auto it = lower_bound(a.begin(), a.end(), v); if (it == a.end()) return banpei; else return *it; } template <class T, class U, class W> T lowerBound(ruiC<T> &a, U v, W banpei) { return lowerBound(a.rui, v, banpei); } template <class T, class U, class W> T upperBound(vector<T> &a, U v, W banpei) { auto it = upper_bound(a.begin(), a.end(), v); if (it == a.end()) return banpei; else return *it; } template <class T, class U, class W> T upperBound(ruiC<T> &a, U v, W banpei) { return upperBound(a.rui, v, banpei); } template <class T, class U, class W> T rlowerBound(vector<T> &a, U v, W banpei) { auto it = upper_bound(a.begin(), a.end(), v); if (it == a.begin()) return banpei; else { return *(--it); } } template <class T, class U, class W> T rlowerBound(ruiC<T> &a, U v, W banpei) { return rlowerBound(a.rui, v, banpei); } template <class T, class U, class W> T rupperBound(vector<T> &a, U v, W banpei) { auto it = lower_bound(a.begin(), a.end(), v); if (it == a.begin()) return banpei; else { return *(--it); } } template <class T, class U, class W> T rupperBound(ruiC<T> &a, U v, W banpei) { return rupperBound(a.rui, v, banpei); } #define next2(a) next(next(a)) #define prev2(a) prev(prev(a)) //狭義の単調増加列 長さを返す template <class T> int lis(vector<T> &a) { int n = sz(a); vi tail(n + 1, MAX(T)); rep(i, n) { int id = lowerIndex(tail, a[i]); /**/ tail[id] = a[i]; } return lowerIndex(tail, MAX(T)); } template <class T> int lis_eq(vector<T> &a) { int n = sz(a); vi tail(n + 1, MAX(T)); rep(i, n) { int id = upperIndex(tail, a[i]); /**/ tail[id] = a[i]; } return lowerIndex(tail, MAX(T)); } // iteratorを返す // valueが1以上の物を返す 0は見つけ次第削除 // vを減らす場合 (*it).se--でいい template <class T, class U, class V> auto lower_map(map<T, U> &m, V k) { auto ret = m.lower_bound(k); while (ret != m.end() && (*ret).second == 0) { ret = m.erase(ret); } return ret; } template <class T, class U, class V> auto upper_map(map<T, U> &m, V k) { auto ret = m.upper_bound(k); while (ret != m.end() && (*ret).second == 0) { ret = m.erase(ret); } return ret; } //存在しなければエラー template <class T, class U, class V> auto rlower_map(map<T, U> &m, V k) { auto ret = upper_map(m, k); assert(ret != m.begin()); ret--; while (1) { if ((*ret).second != 0) break; assert(ret != m.begin()); auto next = ret; --next; m.erase(ret); ret = next; } return ret; } template <class T, class U, class V> auto rupper_map(map<T, U> &m, V k) { auto ret = lower_map(m, k); assert(ret != m.begin()); ret--; while (1) { if ((*ret).second != 0) break; assert(ret != m.begin()); auto next = ret; --next; m.erase(ret); ret = next; } return ret; } template <class... T> void fin(T... s) { out(s...); exit(0); } //便利 数学 math // sub ⊂ top bool subset(int sub, int top) { return (sub & top) == sub; } //-180 ~ 180 degree double atand(double h, double w) { return atan2(h, w) / PI * 180; } //% -mの場合、最小の正の数を返す ll mod(ll a, ll m) { if (m < 0) m *= -1; return (a % m + m) % m; } ll pow(ll a) { return a * a; }; ll fact(ll v) { return v <= 1 ? 1 : v * fact(v - 1); } dou factd(int v) { static vd fact(2, 1); if (sz(fact) <= v) { rep(i, sz(fact), v + 1) { fact.push_back(fact.back() * i); } } return fact[v]; } ll comi(ll n, ll r) { assert(n < 100); static vvi(pas, 100, 100); if (pas[0][0]) return pas[n][r]; pas[0][0] = 1; rep(i, 1, 100) { pas[i][0] = 1; rep(j, 1, i + 1) pas[i][j] = pas[i - 1][j - 1] + pas[i - 1][j]; } return pas[n][r]; } double comd2(ll n, ll r) { static vvd(comb, 2020, 2020); if (comb[0][0] == 0) { comb[0][0] = 1; rep(i, 2000) { comb[i + 1][0] = 1; rep(j, 1, i + 2) { comb[i + 1][j] = comb[i][j] + comb[i][j - 1]; } } } return comb[n][r]; } double comd(int n, int r) { if (r < 0 || r > n) return 0; if (n < 2020) return comd2(n, r); static vd fact(2, 1); if (sz(fact) <= n) { rep(i, sz(fact), n + 1) { fact.push_back(fact.back() * i); } } return fact[n] / fact[n - r] / fact[r]; } ll gcd(ll a, ll b) { while (b) a %= b, swap(a, b); return abs(a); } ll gcd(vi b) { ll res = b[0]; rep(i, 1, sz(b)) res = gcd(b[i], res); return res; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll lcm(vi a) { ll res = a[0]; rep(i, 1, sz(a)) res = lcm(a[i], res); return res; } ll ceil(ll a, ll b) { if (b == 0) { debugline("ceil"); deb(a, b); ole(); return -1; } else if (a < 0) { return 0; } else { return (a + b - 1) / b; } } ll sig0(ll t) { return t <= 0 ? 0 : ((1 + t) * t) >> 1; } // ll sig(ll s, ll t) { return ((s + t) * (t - s + 1)) >> 1; } ll sig(ll s, ll t) { if (s > t) swap(s, t); return sig0(t - s) + s * (t - s + 1); } #define tousa_i tosa_i #define lower_tousa_i lower_tosa_i #define upper_tousa upper_tosa #define upper_tousa_i upper_tosa_i ll tosa_i(ll st, ll ad, ll v) { assert(((v - st) % ad) == 0); return (v - st) / ad; } ll tosa_s(ll st, ll ad, ll len) { return st * len + sig0(len - 1) * ad; } // ax + r (x は非負整数) で表せる整数のうち、v 以上となる最小の整数 ll lower_tosa(ll st, ll ad, ll v) { if (st >= v) return st; return (v - st + ad - 1) / ad * ad + st; } //第何項か ll lower_tosa_i(ll st, ll ad, ll v) { if (st >= v) return 0; return (v - st + ad - 1) / ad; } ll upper_tosa(ll st, ll ad, ll v) { return lower_tosa(st, ad, v + 1); } ll upper_tosa_i(ll st, ll ad, ll v) { return lower_tosa_i(st, ad, v + 1); } // v * v >= aとなる最小のvを返す 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); } /*@formatter:off*/ //機能拡張 #define dtie(a, b) \ int a, b; \ tie(a, b) template <class T, class U> string to_string(T a, U b) { string res = ""; res += a; res += b; return res; } template <class T, class U, class V> string to_string(T a, U b, V c) { string res = ""; res += a; res += b; res += c; return res; } template <class T, class U, class V, class W> string to_string(T a, U b, V c, W d) { string res = ""; res += a; res += b; res += c; res += d; return res; } template <class T, class U, class V, class W, class X> string to_string(T a, U b, V c, W d, X e) { string res = ""; res += a; res += b; res += c; res += d; res += e; return res; } constexpr int bsetlen = k5 * 2; // constexpr int bsetlen = 5050; #define bset bitset<bsetlen> bool operator<(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] < b[i]) return true; if (a[i] > b[i]) return false; } return false; } bool operator>(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] > b[i]) return true; if (a[i] < b[i]) return false; } return false; } bool operator<=(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] < b[i]) return true; if (a[i] > b[i]) return false; } return true; } bool operator>=(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] > b[i]) return true; if (a[i] < b[i]) return false; } return true; } string operator~(string &a) { string res = a; for (auto &&c : res) { if (c == '0') c = '1'; else if (c == '1') c = '0'; else { cerr << "cant ~" << a << "must bit" << endl; exit(0); } } return res; } ostream &operator<<(ostream &os, bset &a) { bitset<10> b; vi list; rep(i, bsetlen) { if (a[i]) list.push_back(i), b[i] = 1; } os << b << ", " << list; return os; } int hbiti(bset &a) { rer(i, bsetlen) { if (a[i]) return i; } return -1; } #define hk(a, b, c) (a <= b && b < c) // O(N/64) bset nap(bset &a, int v) { bset r = a | a << v; return r; } bset nap(bset &a, bset &v) { bset r = a; rep(i, bsetlen) { if (v[i]) r |= a << i; } return r; } template <class T> void seth(vector<vector<T>> &S, int w, vector<T> &v) { assert(sz(S) == sz(v)); assert(w < sz(S[0])); rep(h, sz(S)) { S[h][w] = v[h]; } } template <class T, class U> void operator+=(pair<T, U> &a, pair<T, U> &b) { a.fi += b.fi; a.se += b.se; } template <class T, class U> pair<T, U> operator+(pair<T, U> &a, pair<T, U> &b) { return pair<T, U>(a.fi + b.fi, a.se + b.se); } template <typename CharT, typename Traits, typename Alloc> basic_string<CharT, Traits, Alloc> operator+(const basic_string<CharT, Traits, Alloc> &lhs, const int rv) { basic_string<CharT, Traits, Alloc> str(lhs); str.append(to_string(rv)); return str; } template <typename CharT, typename Traits, typename Alloc> void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const int rv) { lhs += to_string(rv); } template <typename CharT, typename Traits, typename Alloc> basic_string<CharT, Traits, Alloc> operator+(const basic_string<CharT, Traits, Alloc> &lhs, const signed rv) { basic_string<CharT, Traits, Alloc> str(lhs); str.append(to_string(rv)); return str; } template <typename CharT, typename Traits, typename Alloc> void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const signed rv) { lhs += to_string(rv); } template <typename CharT, typename Traits, typename Alloc> void operator*=(basic_string<CharT, Traits, Alloc> &s, int num) { auto bek = s; s = ""; for (; num; num >>= 1) { if (num & 1) { s += bek; } bek += bek; } } template <class T, class U> void operator+=(queue<T> &a, U v) { a.push(v); } template <class T, class U> void operator+=(deque<T> &a, U v) { a.push_back(v); } template <class T> priority_queue<T, vector<T>, greater<T>> & operator+=(priority_queue<T, vector<T>, greater<T>> &a, vector<T> &v) { fora(d, v) a.push(d); return a; } template <class T, class U> priority_queue<T, vector<T>, greater<T>> & operator+=(priority_queue<T, vector<T>, greater<T>> &a, U v) { a.push(v); return a; } template <class T, class U> priority_queue<T> &operator+=(priority_queue<T> &a, U v) { a.push(v); return a; } template <class T> set<T> &operator+=(set<T> &a, vector<T> v) { fora(d, v) a.insert(d); return a; } template <class T, class U> auto operator+=(set<T> &a, U v) { return a.insert(v); } template <class T, class U> auto operator-=(set<T> &a, U v) { return a.erase(v); } template <class T, class U> auto operator+=(mset<T> &a, U v) { return a.insert(v); } template <class T, class U> set<T, greater<T>> &operator+=(set<T, greater<T>> &a, U v) { a.insert(v); return a; } template <class T, class U> vector<T> &operator+=(vector<T> &a, U v) { a.push_back(v); return a; } template <class T, class U> vector<T> operator+(vector<T> &a, U v) { vector<T> ret = a; ret += v; return ret; } template <class T, class U> vector<T> operator+(U v, vector<T> &a) { vector<T> ret = a; ret.insert(ret.begin(), v); return ret; } template <class T> vector<T> operator+(vector<T> a, vector<T> b) { vector<T> ret; ret = a; fora(v, b) ret += v; return ret; } template <class T> vector<T> &operator+=(vector<T> &a, vector<T> &b) { rep(i, sz(b)) { /*こうしないとa+=aで両辺が増え続けてバグる*/ a.push_back(b[i]); } return a; } template <class T, class U> map<T, U> &operator+=(map<T, U> &a, map<T, U> &b) { fora(bv, b) { a[bv.first] += bv.second; } return a; } template <class T> vector<T> &operator-=(vector<T> &a, vector<T> &b) { if (sz(a) != sz(b)) { debugline("vector<T> operator-="); deb(a); deb(b); exit(0); } rep(i, sz(a)) a[i] -= b[i]; return a; } template <class T> vector<T> operator-(vector<T> &a, vector<T> &b) { if (sz(a) != sz(b)) { debugline("vector<T> operator-"); deb(a); deb(b); ole(); } vector<T> res(sz(a)); rep(i, sz(a)) res[i] = a[i] - b[i]; return res; } template <class T, class U> void operator*=(vector<T> &a, U b) { vector<T> ta = a; rep(b - 1) { a += ta; } } template <typename T> void erase(vector<T> &v, unsigned ll i) { v.erase(v.begin() + i); } template <typename T> void erase(vector<T> &v, unsigned ll s, unsigned ll e) { v.erase(v.begin() + s, v.begin() + e); } template <class T, class U> void erase(map<T, U> &m, ll okl, ll ngr) { m.erase(m.lower_bound(okl), m.lower_bound(ngr)); } template <class T> void erase(set<T> &m, ll okl, ll ngr) { m.erase(m.lower_bound(okl), m.lower_bound(ngr)); } template <typename T> void erasen(vector<T> &v, unsigned ll s, unsigned ll n) { v.erase(v.begin() + s, v.begin() + s + n); } template <typename T, typename U> void insert(vector<T> &v, unsigned ll i, U t) { v.insert(v.begin() + i, t); } template <typename T, typename U> void push_front(vector<T> &v, U t) { v.insert(v.begin(), t); } template <typename T, typename U> void insert(vector<T> &v, unsigned ll i, vector<T> list) { for (auto &&va : list) v.insert(v.begin() + i++, va); } template <typename T> void insert(set<T> &v, vector<T> list) { for (auto &&va : list) v.insert(va); } vector<string> split(const string a, const char deli) { string b = a + deli; ll l = 0, r = 0, n = b.size(); vector<string> res; rep(i, n) { if (b[i] == deli) { r = i; if (l < r) res.push_back(b.substr(l, r - l)); l = i + 1; } } return res; } vector<string> split(const string a, const string deli) { vector<string> res; ll kn = sz(deli); std::string::size_type Pos(a.find(deli)); ll l = 0; while (Pos != std::string::npos) { if (Pos - l) res.push_back(a.substr(l, Pos - l)); l = Pos + kn; Pos = a.find(deli, Pos + kn); } if (sz(a) - l) res.push_back(a.substr(l, sz(a) - l)); return res; } 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 fAb(bool a) { if (a) cout << "Alice" << endl; else cout << "Bob"; } 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 <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)}; } //未分類 // 0,2,1 1番目と2番目の次元を入れ替える template <class T> auto irekae(vector<vector<vector<T>>> &A, int x, int y, int z) { #define irekae_resize_loop(a, b, c) \ resize(res, a, b, c); \ rep(i, a) rep(j, b) rep(k, c) vector<vector<vector<T>>> res; if (x == 0 && y == 1 && z == 2) { res = A; } else if (x == 0 && y == 2 && z == 1) { irekae_resize_loop(sz(A), sz(A[0][0]), sz(A[0])) { res[i][j][k] = A[i][k][j]; } } else if (x == 1 && y == 0 && z == 2) { irekae_resize_loop(sz(A[0]), sz(A), sz(A[0][0])) { res[i][j][k] = A[j][i][k]; } } else if (x == 1 && y == 2 && z == 0) { irekae_resize_loop(sz(A[0]), sz(A[0][0]), sz(A)) { res[i][j][k] = A[k][i][j]; } } else if (x == 2 && y == 0 && z == 1) { irekae_resize_loop(sz(A[0][0]), sz(A), sz(A[0])) { res[i][j][k] = A[j][k][i]; } } else if (x == 2 && y == 1 && z == 0) { irekae_resize_loop(sz(A[0][0]), sz(A[0]), sz(A)) { res[i][j][k] = A[k][j][i]; } } return res; #undef irekae_resize_loop } template <class T> auto irekae(vector<vector<T>> &A, int i = 1, int j = 0) { vvt(res, sz(A[0]), sz(A)); rep(i, sz(A)) { rep(j, sz(A[0])) { res[j][i] = A[i][j]; } } return res; } // tou分割する template <typename T> vector<vector<T>> cut(vector<T> &a, int tou = 2) { int N = sz(a); vector<vector<T>> res(tou); int hab = N / tou; vi lens(tou, hab); rep(i, N % tou) { lens[tou - 1 - i]++; } int l = 0; rep(i, tou) { int len = lens[i]; int r = l + len; res[i].resize(len); std::copy(a.begin() + l, a.begin() + r, res[i].begin()); l = r; } return res; } //長さn毎に分割する template <typename T> vector<vector<T>> cutn(vector<T> &a, int len) { int N = sz(a); vector<vector<T>> res(ceil(N, len)); vi lens(N / len, len); if (N % len) lens.push_back(N % len); int l = 0; rep(i, sz(lens)) { int len = lens[i]; int r = l + len; res[i].resize(len); std::copy(a.begin() + l, a.begin() + r, res[i].begin()); l = r; } return res; } vi inds_(vi &a) { int n = sz(a); vb was(n); vi res(n); rep(i, n) { assert(!was[a[i]]); res[a[i]] = i; was[a[i]] = true; } return res; } //@起動時 struct initon { initon() { cin.tie(0); ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(16); srand((unsigned)clock() + (unsigned)time(NULL)); }; } initonv; #define pre prev #define nex next // gra mll pr //上下左右 const string udlr = "udlr"; string UDLR = "UDLR"; // x4と連動 UDLR.find('U') := x4[0] //右、上が正 constexpr ll h4[] = {1, -1, 0, 0}; constexpr ll w4[] = {0, 0, -1, 1}; constexpr ll h8[] = {0, 1, 0, -1, -1, 1, 1, -1}; constexpr ll w8[] = {1, 0, -1, 0, 1, -1, 1, -1}; int mei_inc(int h, int w, int H, int W, int i) { while (++i < 4) { if (inside(h + h4[i], w + w4[i], H, W)) return i; } return i; } #define mei(nh, nw, h, w) \ for (int i = mei_inc(h, w, H, W, -1), nh = i < 4 ? h + h4[i] : 0, \ nw = i < 4 ? w + w4[i] : 0; \ i < 4; i = mei_inc(h, w, H, W, i), nh = h + h4[i], nw = w + w4[i]) //グラフ内で #undef getid //#define getidとしているため、ここを書き直したらgraphも書き直す #define getid_2(h, w) (h * W + w) #define getid_1(p) (p.first * W + p.second) #define getid(...) over2(__VA_ARGS__, getid_2, getid_1)(__VA_ARGS__) #define getp(id) mp(id / W, id % W) //#define set_shuffle() std::random_device seed_gen;std::mt19937 //engine(seed_gen()) #define shuffle(a) std::shuffle((a).begin(), (a).end(), //engine); 1980 開始からtime ms経っていたらtrue bool timeup(int time) { auto end_time = system_clock::now(); auto part = duration_cast<milliseconds>(end_time - start_time); auto lim = milliseconds(time); return part >= lim; } void set_time() { past_time = system_clock::now(); } // MS型(millisecqnds)で返る // set_timeをしてからの時間 auto calc_time_milli() { auto now = system_clock::now(); auto part = duration_cast<milliseconds>(now - past_time); return part; } auto calc_time_micro() { auto now = system_clock::now(); auto part = duration_cast<microseconds>(now - past_time); return part; } auto calc_time_nano() { auto now = system_clock::now(); auto part = duration_cast<nanoseconds>(now - past_time); return part; } bool calc_time(int zikan) { return calc_time_micro() >= microseconds(zikan); } using MS = std::chrono::microseconds; int div(microseconds a, microseconds b) { return a / b; } int div(nanoseconds a, nanoseconds b) { if (b < nanoseconds(1)) { return a / nanoseconds(1); } int v = a / b; return v; } // set_time(); // rep(i,lim)shori // lim*=time_nanbai(); // rep(i,lim)shoriと使う //全体でmilliかかっていいときにlimを何倍してもう一回できるかを返す int time_nanbai(int milli) { auto dec = duration_cast<nanoseconds>(past_time - start_time); auto part = calc_time_nano(); auto can_time = nanoseconds(milli * 1000 * 1000); can_time -= part; can_time -= dec; return div(can_time, part); } /*@formatter:on*/ // vectorで取れる要素数 // bool=> 1e9 * 8.32 // int => 1e8 * 2.6 // ll => 1e8 * 1.3 // 3次元以上取るとメモリがヤバい // static配列を使う vvc(ba); ll N, M, H, W; vi A, B, C; /*@formatter:off*/ template <class T> vector<int> kmp_makeTable(T &s) { int n = s.size(); vector<int> ret(n + 1); ret[0] = -1; int j = -1; for (int i = 0; i < n; i++) { while (j >= 0 && s[i] != s[j]) j = ret[j]; ret[i + 1] = ++j; } return ret; } // str の中に word とマッチする場所のリストを返す // ret のそれぞれの要素 el は, 「str[el] からの文字列が word // と一致する」ことを示す template <class T> vector<int> kmp(T &str, T &word) { vector<int> table = kmp_makeTable(word), ret; int m = 0, i = 0, n = str.size(); while (m + i < n) { if (word[i] == str[m + i]) { if (++i == (int)(word.size())) { ret.push_back(m); m = m + i - table[i]; i = table[i]; } } else { m = m + i - table[i]; if (i > 0) i = table[i]; } } return ret; } template <class T> vector<int> z_algorithm(T &s) { vector<int> prefix(s.size()); for (int i = 1, j = 0; i < s.size(); i++) { if (i + prefix[i - j] < j + prefix[j]) { prefix[i] = prefix[i - j]; } else { int k = max(0, j + prefix[j] - i); while (i + k < s.size() && s[k] == s[i + k]) ++k; prefix[i] = k; j = i; } } prefix[0] = (int)s.size(); return prefix; } /*@formatter:on*/ template <class T> vector<int> z_algorithm(T &A, T word) { int w = sz(word); word += A; vector<int> z = z_algorithm(word); vi res(sz(A)); rep(i, w, sz(z)) { res[i - w] = min(w, z[i - w]); } return res; } // 11:33 void solve() { in(N); na(A, N); na(B, N); auto ta = A, tb = B; A *= 3; B *= 3; rep(i, N * 3 - 1) { A[i] ^= A[i + 1]; B[i] ^= B[i + 1]; } B.resize(N); auto s = z_algorithm(A, B); rep(k, N) { if (s[k] >= N) out(k, ta[k] ^ tb[0]); } } auto my(ll n, vi &a) { return 0; } auto sister(ll n, vi &a) { ll ret = 0; return ret; } signed main() { solve(); #define arg n, a #ifdef _DEBUG bool bad = 0; for (ll i = 0, ok = 1; i < k5 && ok; ++i) { ll n = rand(1, 8); vi a = ranv(n, 1, 10); auto myres = my(arg); auto res = sister(arg); ok = myres == res; if (!ok) { out(arg); cerr << "AC : " << res << endl; cerr << "MY : " << myres << endl; bad = 1; break; } } if (!bad) { // cout << "完璧 : solveを書き直そう" << endl; // cout << " : そして、solve()を呼び出すのだ" << endl; // cout << " : cin>>n; na(a,n);も忘れるな" << endl; } #endif return 0; };
//#undef _DEBUG //#pragma GCC optimize("Ofast") //不動小数点の計算高速化 //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace std::chrono; #define int long long // todo 消したら動かない intの代わりにsignedを使う #define ll long long auto start_time = system_clock::now(); auto past_time = system_clock::now(); #define debugName(VariableName) #VariableName //最大引数がN #define over2(o1, o2, name, ...) name #define over3(o1, o2, o3, name, ...) name #define over4(o1, o2, o3, o4, name, ...) name #define over5(o1, o2, o3, o4, o5, name, ...) name #define over6(o1, o2, o3, o4, o5, o6, name, ...) name #define over7(o1, o2, o3, o4, o5, o6, o7, name, ...) name #define over8(o1, o2, o3, o4, o5, o6, o7, o8, name, ...) name #define over9(o1, o2, o3, o4, o5, o6, o7, o8, o9, name, ...) name #define over10(o1, o2, o3, o4, o5, o6, o7, o8, o9, o10, name, ...) name #define endl '\n' /*@formatter:off*/ //[-n, n)にアクセスできる //また、外部関数resizeに渡せる // sizeは[-n, n)でnを返す template <class T> class mvec { vector<T> v; int n; public: mvec() : n(0), v(0) {} mvec(int n) : n(n), v(n * 2) {} mvec(int n, T val) : n(n), v(n * 2, val) {} auto &operator[](int i) { return v[i + n]; } auto size() { return n; } void resize(int sn) { assert(n == 0); n = sn; v.resize(sn * 2); } auto begin() { return v.begin(); } auto rbegin() { return v.rbegin(); } auto end() { return v.end(); } auto rend() { return v.rend(); } }; //[]でboolは参照を返さないため特殊化が必要 template <> struct mvec<bool> { vector<bool> v; int n; mvec() : n(0), v(0) {} mvec(int n) : n(n), v(n * 2) {} mvec(int n, bool val) : n(n), v(n * 2, val) {} auto operator[](int i) { return v[i + n]; } auto size() { return v.size(); } void resize(int sn) { assert(n == 0); n = sn; v.resize(sn * 2); } auto begin() { return v.begin(); } auto rbegin() { return v.rbegin(); } auto end() { return v.end(); } auto rend() { return v.rend(); } }; template <class T> ostream &operator<<(ostream &os, mvec<T> &a) { int spa = 3; for (auto &&v : a) { spa = max(spa, (int)(to_string(v).size()) + 1); } int n = (int)a.size(); os << endl; for (int i = -n; i < n; i++) { int need = spa - ((int)to_string(i).size()); if (i == -n) { need -= min(need, spa - ((int)to_string(a[i]).size())); } while (need--) { os << " "; } os << i; } os << endl; int i = -n; for (auto &&v : a) { int need = spa - ((int)to_string(v).size()); if (i == -n) { need -= min(need, spa - ((int)to_string(i).size())); } while (need--) { os << " "; } os << v; i++; } return os; } #define mv mvec #define MV mvec using mvi = mvec<ll>; using mvb = mvec<bool>; using mvs = mvec<string>; using mvd = mvec<double>; using mvc = mvec<char>; #define mvvt0(t) mvec<mvec<t>> #define mvvt1(t, a) mvec<mvec<t>> a #define mvvt2(t, a, b) mvec<mvec<t>> a(b) #define mvvt3(t, a, b, c) mvec<mvec<t>> a(b, mvec<t>(c)) #define mvvt4(t, a, b, c, d) mvec<mvec<t>> a(b, mvec<t>(c, d)) #define mvvi(...) \ over4(__VA_ARGS__, mvvt4, mvvt3, mvvt2, mvvt1, mvvt0)(ll, __VA_ARGS__) template <typename T> mvec<T> make_mv(size_t a) { return mvec<T>(a); } template <typename T, typename... Ts> auto make_mv(size_t a, Ts... ts) { return mvec<decltype(make_mv<T>(ts...))>(a, make_mv<T>(ts...)); } #define mvni(name, ...) auto name = make_mv<ll>(__VA_ARGS__) #ifdef _DEBUG // https://marycore.jp/prog/cpp/class-extension-methods/ 違うかも template <class T, class A = std::allocator<T>> struct debtor : std::vector<T, A> { using std::vector<T, A>::vector; template <class U> int deb_v(U a, int v) { return v; } template <class U> int deb_v(debtor<U> &a, int v = 0) { cerr << a.size() << " "; return deb_v(a.at(0), v + 1); } template <class U> void deb_o(U a) { cerr << a << " "; } template <class U> void deb_o(debtor<U> &a) { for (int i = 0; i < min((int)a.size(), 15ll); i++) { deb_o(a[i]); } if ((int)a.size() > 15) { cerr << "..."; } cerr << endl; } typename std::vector<T>::reference operator[](typename std::vector<T>::size_type n) { if (n < 0 || n >= (int)this->size()) { int siz = (int)this->size(); cerr << "vector size = "; int dim = deb_v((*this)); cerr << endl; cerr << "out index at " << n << endl; cerr << endl; if (dim <= 2) { deb_o((*this)); } exit(0); } return this->at(n); } }; #define vector debtor #endif #ifdef _DEBUG //区間削除は出来ない template <class T> struct my_pbds_tree { set<T> s; auto begin() { return s.begin(); } auto end() { return s.end(); } auto rbegin() { return s.rbegin(); } auto rend() { return s.rend(); } auto empty() { return s.empty(); } auto size() { return s.size(); } void clear() { s.clear(); } template <class U> void insert(U v) { s.insert(v); } template <class U> void operator+=(U v) { insert(v); } template <class F> auto erase(F v) { return s.erase(v); } template <class U> auto find(U v) { return s.find(v); } template <class U> auto lower_bound(U v) { return s.lower_bound(v); } template <class U> auto upper_bound(U v) { return s.upper_bound(v); } auto find_by_order(ll k) { auto it = s.begin(); for (ll i = 0; i < k; i++) it++; return it; } auto order_of_key(ll v) { auto it = s.begin(); ll i = 0; for (; it != s.end() && *it < v; i++) it++; return i; } }; #define pbds(T) my_pbds_tree<T> // gp_hash_tableでcountを使えないようにするため template <class T, class U> struct my_unordered_map { unordered_map<T, U> m; my_unordered_map(){}; auto begin() { return m.begin(); } auto end() { return m.end(); } auto cbegin() { return m.cbegin(); } auto cend() { return m.cend(); } template <class V> auto erase(V v) { return m.erase(v); } void clear() { m.clear(); } /*countは gp_hash_tableに存在しない*/ /*!= m.end()*/ template <class V> auto find(V v) { return m.find(v); } template <class V> auto &operator[](V n) { return m[n]; } }; #define unordered_map my_unordered_map #define umapi unordered_map<ll, ll> #define umapp unordered_map<P, ll> #define umapu unordered_map<uint64_t, ll> #define umapip unordered_map<ll, P> #else // umapはunorderd_mapになる // umapiはgp_hash_table // find_by_order(k) k番目のイテレーター // order_of_key(k) k以上が前から何番目か #define pbds(U) \ __gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag, \ __gnu_pbds::tree_order_statistics_node_update> #define umapi __gnu_pbds::gp_hash_table<ll, ll, xorshift> #define umapp __gnu_pbds::gp_hash_table<P, ll, xorshift> #define umapu __gnu_pbds::gp_hash_table<uint64_t, ll, xorshift> #define umapip __gnu_pbds::gp_hash_table<ll, P, xorshift> #endif /*@formatter:on*/ struct xorshift { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } size_t operator()(std::pair<ll, ll> x) const { ll v = ((x.first) << 32) | x.second; static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(v + FIXED_RANDOM); } }; /*@formatter:off*/ template <class U, class L> void operator+=( __gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update> &s, L v) { s.insert(v); } //衝突対策 #define ws ws_ template <class A, class B, class C> struct T2 { A f; B s; C t; T2() { f = 0, s = 0, t = 0; } T2(A f, B s, C t) : f(f), s(s), t(t) {} bool operator<(const T2 &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 ?n s > r.s : t > r.t; 大きい順 */ } bool operator>(const T2 &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 T2 &r) const { return f == r.f && s == r.s && t == r.t; } bool operator!=(const T2 &r) const { return f != r.f || s != r.s || t != r.t; } }; template <class A, class B, class C, class D> struct F2 { A a; B b; C c; D d; F2() { a = 0, b = 0, c = 0, d = 0; } F2(A a, B b, C c, D d) : a(a), b(b), c(c), d(d) {} bool operator<(const F2 &r) const { return a != r.a ? a < r.a : b != r.b ? b < r.b : c != r.c ? c < r.c : d < r.d; /* return a != r.a ? a > r.a : b != r.b ? b > r.b : c != r.c ? c > r.c : d > r.d;*/ } bool operator>(const F2 &r) const { return a != r.a ? a > r.a : b != r.b ? b > r.b : c != r.c ? c > r.c : d > r.d; /* return a != r.a ? a < r.a : b != r.b ? b < r.b : c != r.c ? c < r.c : d < r.d;*/ } bool operator==(const F2 &r) const { return a == r.a && b == r.b && c == r.c && d == r.d; } bool operator!=(const F2 &r) const { return a != r.a || b != r.b || c != r.c || d != r.d; } ll operator[](ll i) { assert(i < 4); return i == 0 ? a : i == 1 ? b : i == 2 ? c : d; } }; typedef T2<ll, ll, ll> T; typedef F2<ll, ll, ll, ll> F; T mt(ll a, ll b, ll c) { return T(a, b, c); } F mf(ll a, ll b, ll c, ll d) { return F(a, b, c, d); } //@マクロ省略系 型,構造 #define double long double #define pow powl using dou = double; const double eps = 1e-9; //基本コメントアウト /* struct epsdou { double v; epsdou(double v = 0) : v(v) {} template<class T> epsdou &operator+=(T b) { v += (double) b; return (*this); } template<class T> epsdou &operator-=(T b) { v -= (double) b; return (*this); } template<class T> epsdou &operator*=(T b) { v *= (double) b; return (*this); } template<class T> epsdou &operator/=(T b) { v /= (double) b; return (*this); } epsdou operator+(epsdou b) { return v + (double) b; } epsdou operator-(epsdou b) { return v - (double) b; } epsdou operator*(epsdou b) { return v * (double) b; } epsdou operator/(epsdou b) { return v / (double) b; } epsdou operator-() const { return epsdou(-v); } template<class T> bool operator<(T b) { return v < (double) b; } template<class T> bool operator>(T b) { return v > (double) b; } template<class T> bool operator==(T b) { return fabs(v - (double) b) <= eps; } template<class T> bool operator<=(T b) { return v < (double) b || fabs(v - b) <= eps; } template<class T> bool operator>=(T b) { return v > (double) b || fabs(v - b) <= eps; } operator double() { return v; }};istream &operator>>(istream &iss, epsdou &a) { iss >> a.v; return iss;}ostream &operator<<(ostream &os, epsdou &a) { os << a.v; return os;} #define eps_conr_t(o) template<class T> epsdou operator o(T b, epsdou a){return a.v o (dou)b;} #define eps_conl_t(o) template<class T> epsdou operator o(epsdou a, T b){return a.v o (dou)b;} eps_conl_t(+)eps_conl_t(-)eps_conl_t(*)eps_conl_t(/)eps_conr_t(+)eps_conr_t(-)eps_conr_t(*)eps_conr_t(/) #undef double #define double epsdou */ #define ull unsigned long long using itn = int; using str = string; using bo = bool; #define au auto using P = pair<ll, ll>; using mvp = mvec<P>; using mvt = mvec<T>; #define MIN(a) numeric_limits<a>::min() #define MAX(a) numeric_limits<a>::max() #define fi first #define se second #define beg begin #define rbeg rbegin #define con continue #define bre break #define brk break #define is == #define el else #define elf else if #define upd update #define sstream stringstream #define maxq 1 #define minq -1 #define ZERO(a) memset(a, 0, sizeof(a)) #define MINUS(a) memset(a, 0xff, sizeof(a)) #define MALLOC(type, len) (type *)malloc((len) * sizeof(type)) #define lam(right) [&](auto &p) { return p right; } //マクロ省略系 コンテナ 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 V vector #define vvt0(t) vector<vector<t>> #define vvt1(t, a) vector<vector<t>> a #define vvt2(t, a, b) vector<vector<t>> a(b) #define vvt3(t, a, b, c) vector<vector<t>> a(b, vector<t>(c)) #define vvt4(t, a, b, c, d) vector<vector<t>> a(b, vector<t>(c, d)) #define vvi(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(ll, __VA_ARGS__) #define vvb(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(bool, __VA_ARGS__) #define vvs(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(string, __VA_ARGS__) #define vvd(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(double, __VA_ARGS__) #define vvc(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(char, __VA_ARGS__) #define vvp(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(P, __VA_ARGS__) #define vvt(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(T, __VA_ARGS__) #define vv(type, ...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(type, __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 vn(type, name, ...) auto name = make_v<type>(__VA_ARGS__) #define PQ priority_queue<ll, vector<ll>, greater<ll>> #define tos to_string using mapi = map<ll, ll>; using mapp = map<P, ll>; using mapd = map<dou, ll>; using mapc = map<char, ll>; using maps = map<str, ll>; using seti = set<ll>; using setd = set<dou>; using setc = set<char>; using sets = set<str>; using qui = queue<ll>; #define uset unordered_set #define useti unordered_set<ll, xorshift> #define mset multiset #define mseti multiset<ll> #define umap unordered_map #define mmap multimap //任意のマクロサポート用 使う度に初期化する int index_, v1_, v2_, v3_; template <class T> struct pq { priority_queue<T, vector<T>, greater<T>> q; /*小さい順*/ T su = 0; void clear() { q = priority_queue<T, vector<T>, greater<T>>(); su = 0; } void operator+=(T v) { su += v; q.push(v); } T sum() { return su; } T top() { return q.top(); } void pop() { su -= q.top(); q.pop(); } T poll() { T ret = q.top(); su -= ret; q.pop(); return ret; } ll size() { return q.size(); } }; template <class T> struct pqg { priority_queue<T> q; /*大きい順*/ T su = 0; void clear() { q = priority_queue<T>(); su = 0; } void operator+=(T v) { su += v; q.push(v); } T sum() { return su; } T top() { return q.top(); } void pop() { su -= q.top(); q.pop(); } T poll() { T ret = q.top(); su -= ret; q.pop(); return ret; } ll size() { return q.size(); } }; #define pqi pq<ll> #define pqgi pqg<ll> //マクロ 繰り返し //↓@オーバーロード隔離 #define rep1(n) for (ll rep1i = 0, rep1lim = n; rep1i < rep1lim; ++rep1i) #define rep2(i, n) for (ll i = 0, rep2lim = n; i < rep2lim; ++i) #define rep3(i, m, n) for (ll i = m, rep3lim = n; i < rep3lim; ++i) #define rep4(i, m, n, ad) for (ll i = m, rep4lim = n; i < rep4lim; i += ad) //逆順 閉区間 #define rer2(i, n) for (ll i = n; i >= 0; i--) #define rer3(i, m, n) for (ll i = m, rer3lim = n; i >= rer3lim; i--) #define rer4(i, m, n, dec) for (ll i = m, rer4lim = n; i >= rer4lim; i -= dec) //ループを一つにまとめないとフォーマットで汚くなるため #define nex_ind1(i) i++ #define nex_ind2(i, j, J) \ i = (j + 1 == J) ? i + 1 : i, j = (j + 1 == J ? 0 : j + 1) #define nex_ind3(i, j, k, J, K) \ i = (j + 1 == J && k + 1 == K) ? i + 1 : i, \ j = (k + 1 == K) ? (j + 1 == J ? 0 : j + 1) : j, \ k = (k + 1 == K ? 0 : k + 1) #define nex_ind4(i, j, k, l, J, K, L) \ i = (j + 1 == J && k + 1 == K && l + 1 == L) ? i + 1 : i, \ j = (k + 1 == K && l + 1 == L) ? (j + 1 == J ? 0 : j + 1) : j, \ k = (l + 1 == L ? (k + 1 == K ? 0 : k + 1) : k), l = l + 1 == L ? 0 : l + 1 #define nex_ind5(i, j, k, l, m, J, K, L, M) \ i = (j + 1 == J && k + 1 == K && l + 1 == L && m + 1 == M) ? i + 1 : i, \ j = (k + 1 == K && l + 1 == L && m + 1 == M) ? (j + 1 == J ? 0 : j + 1) : j, \ k = (l + 1 == L && m + 1 == M ? (k + 1 == K ? 0 : k + 1) : k), \ l = m + 1 == M ? l + 1 == L ? 0 : l + 1 : l, m = m + 1 == M ? 0 : m + 1 #define repss2(i, I) for (int i = 0; i < I; i++) #define repss4(i, j, I, J) \ for (int i = (J ? 0 : I), j = 0; i < I; nex_ind2(i, j, J)) #define repss6(i, j, k, I, J, K) \ for (int i = (J && K ? 0 : I), j = 0, k = 0; i < I; nex_ind3(i, j, k, J, K)) #define repss8(i, j, k, l, I, J, K, L) \ for (int i = (J && K && L ? 0 : I), j = 0, k = 0, l = 0; i < I; \ nex_ind4(i, j, k, l, J, K, L)) #define repss10(i, j, k, l, m, I, J, K, L, M) \ for (int i = (J && K && L && M ? 0 : I), j = 0, k = 0, l = 0, m = 0; i < I; \ nex_ind5(i, j, k, l, m, J, K, L, M)) // i,j,k...をnまで見る #define reps2(i, n) repss2(i, n) #define reps3(i, j, n) repss4(i, j, n, n) #define reps4(i, j, k, n) repss6(i, j, k, n, n, n) #define reps5(i, j, k, l, n) repss8(i, j, k, l, n, n, n, n) template <class T> void nex_repv2(int &i, int &j, int &I, int &J, vector<vector<T>> &s) { while (1) { j++; if (j >= J) { j = 0; i++; if (i < I) { J = (int)s[i].size(); } } if (i >= I || J) return; } } template <class T> void nex_repv3(int &i, int &j, int &k, int &I, int &J, int &K, vector<vector<vector<T>>> &s) { while (1) { k++; if (k >= K) { k = 0; j++; if (j >= J) { j = 0; i++; if (i >= I) return; } } J = (int)s[i].size(); K = (int)s[i][j].size(); if (J && K) return; } } #define repv_2(i, a) repss2(i, sz(a)) //正方形である必要はない //直前を持つのとどっちが早いか #define repv_3(i, j, a) \ for (int I = (int)a.size(), J = (int)a[0].size(), i = 0, j = 0; i < I; \ nex_repv2(i, j, I, J, a)) //箱状になっている事が要求される つまり[i] 次元目の要素数は一定 #define repv_4(i, j, k, a) \ for (int I = (int)a.size(), J = (int)a[0].size(), K = (int)a[0][0].size(), \ i = 0, j = 0, k = 0; \ i < I; nex_repv3(i, j, k, I, J, K, a)) #define repv_5(i, j, k, l, a) \ repss8(i, j, k, l, sz(a), sz(a[0]), sz(a[0][0]), sz(a[0][0][0])) #define repv_6(i, j, k, l, m, a) \ repss10(i, j, k, l, m, sz(a), sz(a[0]), sz(a[0][0]), sz(a[0][0][0]), \ sz(a[0][0][0][0])) template <typename T> struct has_rbegin_rend { private: template <typename U> static auto check(U &&obj) -> decltype(std::rbegin(obj), std::rend(obj), std::true_type{}); static std::false_type check(...); public: static constexpr bool value = decltype(check(std::declval<T>()))::value; }; template <typename T> constexpr bool has_rbegin_rend_v = has_rbegin_rend<T>::value; template <typename Iterator> class Range { public: Range(Iterator &&begin, Iterator &&end) noexcept : m_begin(std::forward<Iterator>(begin)), m_end(std::forward<Iterator>(end)) {} Iterator begin() const noexcept { return m_begin; } Iterator end() const noexcept { return m_end; } private: const Iterator m_begin; const Iterator m_end; }; template <typename Iterator> static inline Range<Iterator> makeRange(Iterator &&begin, Iterator &&end) noexcept { return Range<Iterator>{std::forward<Iterator>(begin), std::forward<Iterator>(end)}; } template <typename T> static inline decltype(auto) makeReversedRange(const std::initializer_list<T> &iniList) noexcept { return makeRange(std::rbegin(iniList), std::rend(iniList)); } template <typename T, typename std::enable_if_t<has_rbegin_rend_v<T>, std::nullptr_t> = nullptr> static inline decltype(auto) makeReversedRange(T &&c) noexcept { return makeRange(std::rbegin(c), std::rend(c)); } /* rbegin(), rend()を持たないものはこっちに分岐させて,エラーメッセージを少なくする*/ template <typename T, typename std::enable_if<!has_rbegin_rend<T>::value, std::nullptr_t>::type = nullptr> static inline void makeReversedRange(T &&) noexcept { static_assert(has_rbegin_rend<T>::value, "Specified argument doesn't have reverse iterator."); } #define form1(st) \ for (auto &&form_it = st.begin(); form_it != st.end(); ++form_it) #define form3(k, v, st) \ for (auto &&form_it = st.begin(); form_it != st.end(); ++form_it) #define form4(k, v, st, r) \ for (auto &&form_it = st.begin(); form_it != st.end() && (*form_it).fi < r; \ ++form_it) #define form5(k, v, st, l, r) \ for (auto &&form_it = st.lower_bound(l); \ form_it != st.end() && (*form_it).fi < r; ++form_it) #define fors1(st) \ for (auto &&fors_it = st.begin(); fors_it != st.end(); ++fors_it) #define fors2(v, st) \ for (auto &&fors_it = st.begin(); fors_it != st.end(); ++fors_it) #define fors3(v, st, r) \ for (auto &&fors_it = st.begin(); fors_it != st.end() && (*fors_it) < r; \ ++fors_it) #define fors4(v, st, l, r) \ for (auto &&fors_it = st.lower_bound(l); \ fors_it != st.end() && (*fors_it) < r; ++fors_it) #define forslr3(st, a, b) \ for (auto &&forslr_it = st.begin(); forslr_it != st.end(); ++forslr_it) #define forslr4(v, st, a, b) \ for (auto &&forslr_it = st.begin(); forslr_it != st.end(); ++forslr_it) #define forslr5(v, st, r, a, b) \ for (auto &&forslr_it = st.begin(); \ forslr_it != st.end() && (*forslr_it) < r; ++forslr_it) #define forslr6(v, st, l, r, a, b) \ for (auto &&forslr_it = st.lower_bound(l); \ forslr_it != st.end() && (*forslr_it) < r; ++forslr_it) template <class U> vector<U> to1d(vector<U> &a) { return a; } template <class U> vector<U> to1d(V<V<U>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) res.push_back(a2); return res; } template <class U> vector<U> to1d(V<V<V<U>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) res.push_back(a3); return res; } template <class U> vector<U> to1d(V<V<V<V<U>>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) for (auto &&a4 : a3) res.push_back(a4); return res; } template <class U> vector<U> to1d(V<V<V<V<V<U>>>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) for (auto &&a4 : a3) for (auto &&a5 : a4) res.push_back(a5); return res; } template <class U> vector<U> to1d(V<V<V<V<V<V<U>>>>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) for (auto &&a4 : a3) for (auto &&a5 : a4) for (auto &&a6 : a5) res.push_back(a6); return res; } #define fora_init_2(a, A) ; #define fora_init_4(a, b, A, B) \ auto &&a = A[fora_i]; \ auto &&b = B[fora_i]; #define fora_init_6(a, b, c, A, B, C) \ auto &&a = A[fora_i]; \ auto &&b = B[fora_i]; \ auto &&c = C[fora_i]; #define fora_init_8(a, b, c, d, A, B, C, D) \ auto &&a = A[fora_i]; \ auto &&b = B[fora_i]; \ auto &&c = C[fora_i]; \ auto &&d = D[fora_i]; #define fora_init(...) \ over8(__VA_ARGS__, fora_init_8, a, fora_init_6, a, fora_init_4, a, \ fora_init_2)(__VA_ARGS__) #define forr_init_2(a, A) auto &&a = A[forr_i]; #define forr_init_4(a, b, A, B) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; #define forr_init_6(a, b, c, A, B, C) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; \ auto &&c = C[forr_i]; #define forr_init_8(a, b, c, d, A, B, C, D) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; \ auto &&c = C[forr_i]; \ auto &&d = D[forr_i]; #define forr_init(...) \ over8(__VA_ARGS__, forr_init_8, a, forr_init_6, a, forr_init_4, a, \ forr_init_2)(__VA_ARGS__) #define forp_init(k, v, ...) \ auto &&k = (*forp_it).fi; \ auto &&v = (*forp_it).se; #define form_init(k, v, ...) \ auto &&k = (*form_it).fi; \ auto &&v = (*form_it).se; #define fors_init(v, ...) auto &&v = (*fors_it); #define forlr_init(a, A, ngl, ngr) \ auto a = A[forlr_i]; \ auto prev = forlr_i ? A[forlr_i - 1] : ngl; \ auto next = forlr_i + 1 < rep2lim ? A[forlr_i + 1] : ngr; #define forslr_init4(a, A, ngl, ngr) \ auto a = (*forslr_it); \ auto prev = (forslr_it != A.begin()) ? (*std::prev(forslr_it)) : ngl; \ auto next = (forslr_it != std::prev(A.end())) ? (*std::next(forslr_it)) : ngr; #define forslr_init5(a, A, r, ngl, ngr) \ auto a = (*forslr_it); \ auto prev = (forslr_it != A.begin()) ? (*std::prev(forslr_it)) : ngl; \ auto next = (forslr_it != std::prev(A.end())) ? (*std::next(forslr_it)) : ngr; #define forslr_init6(a, A, l, r, ngl, ngr) \ auto a = (*forslr_it); \ auto prev = (forslr_it != A.begin()) ? (*std::prev(forslr_it)) : ngl; \ auto next = (forslr_it != std::prev(A.end())) ? (*std::next(forslr_it)) : ngr; #define forslr_init(...) \ over6(__VA_ARGS__, forslr_init6, forslr_init5, forslr_init4)(__VA_ARGS__); #define fora_2(a, A) for (auto &&a : A) #define fora_4(a, b, A, B) rep(fora_i, sz(A)) #define fora_6(a, b, c, A, B, C) rep(fora_i, sz(A)) #define fora_8(a, b, c, d, A, B, C, D) rep(fora_i, sz(A)) #define forr_2(a, A) rer(forr_i, sz(A) - 1) #define forr_4(a, b, A, B) rer(forr_i, sz(A) - 1) #define forr_6(a, b, c, A, B, C) rer(forr_i, sz(A) - 1) #define forr_8(a, b, c, d, A, B, C, D) rer(forr_i, sz(A) - 1) //↑@オーバーロード隔離 // rep系はインデックス、for系は中身 #define rep(...) over4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__) #define rer(...) over4(__VA_ARGS__, rer4, rer3, rer2, )(__VA_ARGS__) // char用のrep #define repc(i, m, n) for (char i = m, repc3lim = n; i < repc3lim; ++i) // i,j,k...をnまで見る #define reps(...) over5(__VA_ARGS__, reps5, reps4, reps3, reps2, )(__VA_ARGS__) #define repss(...) \ over10(__VA_ARGS__, repss10, a, repss8, a, repss6, a, repss4, a, \ repss2)(__VA_ARGS__) // vectorのindexを走査する // repv(i,j,vvi) #define repv(...) \ over6(__VA_ARGS__, repv_6, repv_5, repv_4, repv_3, repv_2, )(__VA_ARGS__) #define rerv(i, A) for (int i = sz(A) - 1; i >= 0; i--) // repvn(dp) nは次元 #define repv1(a) repv(i, a) #define repv2(a) repv(i, j, a) #define repv3(a) repv(i, j, k, a) #define repv4(a) repv(i, j, k, l, a) #define fora(...) \ over8(__VA_ARGS__, fora_8, a, fora_6, a, fora_4, a, fora_2)(__VA_ARGS__) #define forr(...) \ over8(__VA_ARGS__, forr_8, a, forr_6, a, forr_4, a, forr_2)(__VA_ARGS__) //#define forr(v, a) for(auto&& v : makeReversedRange(a)) //参照を取らない #define forv(a, b) for (auto a : to1d(b)) //インデックスを前後含めて走査 #define ring(i, s, len) \ for (int i = s, prev = (s == 0) ? len - 1 : s - 1, \ next = (s == len - 1) ? 0 : s + 1, cou = 0; \ cou < len; \ cou++, prev = i, i = next, next = (next == len - 1) ? 0 : next + 1) //値と前後を見る #define ringv(v, d) \ index_ = 0; \ for (auto prev = d[sz(d) - 1], next = (int)d.size() > 1 ? d[1] : d[0], \ v = d[0]; \ index_ < sz(d); index_++, prev = v, v = next, \ next = (index_ >= sz(d) - 1 ? d[0] : d[index_ + 1])) // 左右をnext prevで見る 0の左と nの右 #define forlr(v, d, banpei_l, banpei_r) rep(forlr_i, sz(d)) #define form(...) \ over5(__VA_ARGS__, form5, form4, form3, form2, form1)(__VA_ARGS__) #define fors(...) over4(__VA_ARGS__, fors4, fors3, fors2, fors1)(__VA_ARGS__) #define forslr(...) \ over6(__VA_ARGS__, forslr6, forslr5, forslr4, forslr3)(__VA_ARGS__) #define forp(k, v, st) \ for (auto &&forp_it = st.begin(); forp_it != st.end(); ++forp_it) //マクロ 定数 #define k3 1010 #define k4 10101 #define k5 101010 #define k6 1010101 #define k7 10101010 const ll inf = (ll)1e9 + 100; const ll linf = (ll)1e18 + 100; const dou dinf = (dou)linf * linf; const char infc = '{'; const string infs = "{"; const double PI = 3.1415926535897932384626433832795029L; //マクロ省略形 関数等 #define arsz(a) (sizeof(a) / sizeof(a[0])) #define sz(a) ((ll)(a).size()) #define mp make_pair #define pb pop_back #define pf push_front #define eb emplace_back #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() constexpr bool ev(ll a) { return !(a & 1); } constexpr bool od(ll a) { return (a & 1); } //@拡張系 こう出来るべきというもの //埋め込み 存在を意識せずに機能を増やされているもの namespace std { template <> class hash<std::pair<signed, signed>> { public: size_t operator()(const std::pair<signed, signed> &x) const { return hash<ll>()(((ll)x.first << 32) | x.second); } }; template <> class hash<std::pair<ll, ll>> { public : /*大きいllが渡されると、<<32でオーバーフローするがとりあえず問題ないと判断*/ size_t operator()(const std::pair<ll, ll> &x) const { return hash<ll>()(((ll)x.first << 32) | x.second); } }; } // namespace std // stream まとめ /*@formatter:on*/ istream &operator>>(istream &iss, P &a) { iss >> a.first >> a.second; return iss; } template <typename T> istream &operator>>(istream &iss, vector<T> &vec) { for (T &x : vec) iss >> x; return iss; } template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> p) { os << p.fi << " " << p.se; return os; } ostream &operator<<(ostream &os, T p) { os << p.f << " " << p.s << " " << p.t; return os; } ostream &operator<<(ostream &os, F p) { os << p.a << " " << p.b << " " << p.c << " " << p.d; return os; } template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) { for (ll i = 0; i < vec.size(); ++i) os << vec[i] << (i + 1 == vec.size() ? "" : " "); return os; } template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) { for (ll i = 0; i < vec.size(); ++i) { for (ll j = 0; j < vec[i].size(); ++j) { os << vec[i][j] << " "; } os << endl; } return os; } template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &m) { for (auto &&v : m) os << v; return os; } template <class T> ostream &operator<<(ostream &os, set<T> s) { fora(v, s) { os << v << " "; } return os; } template <class T> ostream &operator<<(ostream &os, deque<T> a) { fora(v, a) os << v << " "; return os; } ostream &operator<<(ostream &os, vector<vector<char>> &vec) { rep(h, sz(vec)) { rep(w, sz(vec[0])) { os << vec[h][w]; } os << endl; } return os; } // template<class T,class U>ostream &operator<<(ostream &os, vector<pair<T,U>>& // a) {fora(v,a)os<<v<<endl;return os;} /*@formatter:off*/ template <typename W, typename H> void resize(W &vec, const H head) { vec.resize(head); } template <typename W, typename H, typename... T> void resize(W &vec, const H &head, const T... tail) { vec.resize(head); for (auto &v : vec) resize(v, tail...); } // 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); }); } //削除した後のvectorを返す template <typename T, typename F> vector<T> erase_if2(vector<T> &v, F f) { vector<T> nv; rep(i, sz(v)) { if (!f(v[i])) { nv.push_back(v[i]); } } return nv; } template <typename T, typename F> vector<vector<T>> erase_if2(vector<vector<T>> &v, F f) { vector<vector<T>> res; rep(i, sz(v)) { res[i] = erase_if2(v[i], f); } return res; } // all_of(A, %2); // all_of(A, a, a%2); #define all_of__2(a, right) all_of2(a, lam(right)) #define all_of__3(a, v, siki) all_of2(a, [&](auto v) { return siki; }) #define all_of(...) over3(__VA_ARGS__, all_of__3, all_of__2)(__VA_ARGS__) #define all_of_f(a, f) all_of2(a, f) #define any_of__2(a, right) any_of2(a, lam(right)) #define any_of__3(a, v, siki) any_of2(a, [&](auto v) { return siki; }) #define any_of(...) over3(__VA_ARGS__, any_of__3, any_of__2)(__VA_ARGS__) #define any_of_f(a, f) any_of2(a, f) #define none_of__2(a, right) none_of2(a, lam(right)) #define none_of__3(a, v, siki) none_of2(a, [&](auto v) { return siki; }) #define none_of(...) over3(__VA_ARGS__, none_of__3, none_of__2)(__VA_ARGS__) #define none_of_f(a, f) none_of2(a, f) #define find_if__2(a, right) find_if2(a, lam(right)) #define find_if__3(a, v, siki) find_if2(a, [&](auto v) { return siki; }) #define find_if(...) over3(__VA_ARGS__, find_if__3, find_if__2)(__VA_ARGS__) #define find_if_f(a, f) find_if2(a, f) #define rfind_if__2(a, right) rfind_if2(a, lam(right)) #define rfind_if__3(a, v, siki) rfind_if2(a, [&](auto v) { return siki; }) #define rfind_if(...) over3(__VA_ARGS__, rfind_if__3, rfind_if__2)(__VA_ARGS__) #define rfind_if_f(a, f) rfind_if2(a, f) #define contains_if__2(a, right) contains_if2(a, lam(right)) #define contains_if__3(a, v, siki) contains_if2(a, [&](auto v) { return siki; }) #define contains_if(...) \ over3(__VA_ARGS__, contains_if__3, contains_if__2)(__VA_ARGS__) #define contains_if_f(a, f) contains_if2(a, f) #define count_if__2(a, right) count_if2(a, lam(right)) #define count_if__3(a, v, siki) count_if2(a, [&](auto v) { return siki; }) #define count_if(...) over3(__VA_ARGS__, count_if__3, count_if__2)(__VA_ARGS__) #define count_if_f(a, f) count_if2(a, f) #define for_each__2(a, right) \ do { \ fora(v, a) { v right; } \ } while (0) #define for_each__3(a, v, siki) \ do { \ fora(v, a) { siki; } \ } while (0) #define for_each(...) over3(__VA_ARGS__, for_each__3, for_each__2)(__VA_ARGS__) #define for_each_f(a, f) \ do { \ fora(v, a) { f(v); } \ } while (0) #define erase_if__2(a, right) erase_if2(a, lam(right)) #define erase_if__3(a, v, siki) erase_if2(a, [&](auto v) { return siki; }) #define erase_if(...) over3(__VA_ARGS__, erase_if__3, erase_if__2)(__VA_ARGS__) #define erase_if_f(a, f) erase_if2(a, f) #define entry_if__2(a, right) erase_if2(a, [&](auto v) { return !(v right); }) #define entry_if__3(a, v, siki) erase_if2(a, [&](auto v) { return !(siki); }) #define entry_if(...) over3(__VA_ARGS__, entry_if__3, entry_if__2)(__VA_ARGS__) #define entry_if_f(a, f) erase_if2(a, [&](auto v) { return !f(v); }) template <class T, class U, class W> void replace(vector<W> &a, T key, U v) { rep(i, sz(a)) if (a[i] == key) a[i] = v; } template <class T, class U, class W> void replace(vector<vector<W>> &A, T key, U v) { rep(i, sz(A)) replace(A[i], key, v); } void replace(str &a, char key, str v) { if (v == "") a.erase(remove(all(a), key), a.end()); } void replace(str &a, char key, char v) { replace(all(a), key, v); } // keyと同じかどうか01で置き換える template <class T, class U> void replace(vector<T> &a, U k) { rep(i, sz(a)) a[i] = a[i] == k; } template <class T, class U> void replace(vector<vector<T>> &a, U k) { rep(i, sz(a)) rep(j, sz(a[0])) a[i][j] = a[i][j] == k; } template <class T> void replace(T &a) { replace(a, '#'); } void replace(str &a, str key, str v) { stringstream t; ll kn = sz(key); std::string::size_type Pos(a.find(key)); ll l = 0; while (Pos != std::string::npos) { t << a.substr(l, Pos - l); t << v; l = Pos + kn; Pos = a.find(key, Pos + kn); } t << a.substr(l, sz(a) - l); a = t.str(); } template <class T> bool includes(vector<T> &a, vector<T> &b) { vi c = a; vi d = b; sort(all(c)); sort(all(d)); return includes(all(c), all(d)); } template <class T> bool is_permutation(vector<T> &a, vector<T> &b) { return is_permutation(all(a), all(b)); } template <class T> bool next_permutation(vector<T> &a) { return next_permutation(all(a)); } void iota(vector<ll> &ve, ll s, ll n) { ve.resize(n); iota(all(ve), s); } vi iota(ll s, ll len) { vi ve(len); iota(all(ve), s); return ve; } template <class A, class B> auto vtop(vector<A> &a, vector<B> &b) { assert(sz(a) == sz(b)); /*stringを0で初期化できない */ vector<pair<A, B>> res; rep(i, sz(a)) res.eb(a[i], b[i]); return res; } template <class A, class B> void ptov(vector<pair<A, B>> &p, vector<A> &a, vector<B> &b) { a.resize(sz(p)), b.resize(sz(p)); rep(i, sz(p)) a[i] = p[i].fi, b[i] = p[i].se; } template <class A, class B, class C> auto vtot(vector<A> &a, vector<B> &b, vector<C> &c) { assert(sz(a) == sz(b) && sz(b) == sz(c)); vector<T2<A, B, C>> res; rep(i, sz(a)) res.eb(a[i], b[i], c[i]); return res; } template <class A, class B, class C, class D> auto vtof(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { assert(sz(a) == sz(b) && sz(b) == sz(c) && sz(c) == sz(d)); vector<F2<A, B, C, D>> res; rep(i, sz(a)) res.eb(a[i], b[i], c[i], d[i]); return res; } enum pcomparator { fisi, fisd, fdsi, fdsd, sifi, sifd, sdfi, sdfd }; enum tcomparator { fisiti, fisitd, fisdti, fisdtd, fdsiti, fdsitd, fdsdti, fdsdtd, fitisi, fitisd, fitdsi, fitdsd, fdtisi, fdtisd, fdtdsi, fdtdsd, sifiti, sifitd, sifdti, sifdtd, sdfiti, sdfitd, sdfdti, sdfdtd, sitifi, sitifd, sitdfi, sitdfd, sdtifi, sdtifd, sdtdfi, sdfdfd, tifisi, tifisd, tifdsi, tifdsd, tdfisi, tdfisd, tdfdsi, tdfdsd, tisifi, tisifd, tisdfi, tisdfd, tdsifi, tdsifd, tdsdfi, tdsdfd }; template <class A, class B> void sort(vector<pair<A, B>> &a, pcomparator type) { typedef pair<A, B> U; if (type == fisi) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se < r.se; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se > r.se; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se < r.se; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se > r.se; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi < r.fi; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi > r.fi; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi < r.fi; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi > r.fi; }); }; template <class U> void sort(vector<U> &a, pcomparator type) { if (type == fisi) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f > r.f; }); }; template <class A, class B, class C, class D> void sort(vector<F2<A, B, C, D>> &a, pcomparator type) { typedef F2<A, B, C, D> U; if (type == fisi) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a > r.a; }); }; template <class U> void sort(vector<U> &a, tcomparator type) { if (type == 0) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t < r.t; }); else if (type == 1) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t > r.t; }); else if (type == 2) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t < r.t; }); else if (type == 3) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t > r.t; }); else if (type == 4) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t < r.t; }); else if (type == 5) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t > r.t; }); else if (type == 6) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t < r.t; }); else if (type == 7) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t > r.t; }); else if (type == 8) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s < r.s; }); else if (type == 9) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s > r.s; }); else if (type == 10) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s < r.s; }); else if (type == 11) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s > r.s; }); else if (type == 12) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s < r.s; }); else if (type == 13) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s > r.s; }); else if (type == 14) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s < r.s; }); else if (type == 15) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s > r.s; }); else if (type == 16) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t < r.t; }); else if (type == 17) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t > r.t; }); else if (type == 18) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t < r.t; }); else if (type == 19) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t > r.t; }); else if (type == 20) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t < r.t; }); else if (type == 21) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t > r.t; }); else if (type == 22) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t < r.t; }); else if (type == 23) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t > r.t; }); else if (type == 24) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f < r.f; }); else if (type == 25) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f > r.f; }); else if (type == 26) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f < r.f; }); else if (type == 27) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f > r.f; }); else if (type == 28) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f < r.f; }); else if (type == 29) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f > r.f; }); else if (type == 30) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f < r.f; }); else if (type == 31) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f > r.f; }); else if (type == 32) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == 33) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == 34) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == 35) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == 36) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == 37) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == 38) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == 39) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == 40) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == 41) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == 42) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == 43) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f > r.f; }); else if (type == 44) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == 45) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == 46) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == 47) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f > r.f; }); } template <class A, class B, class C, class D> void sort(vector<F2<A, B, C, D>> &a, tcomparator type) { typedef F2<A, B, C, D> U; if (type == 0) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c < r.c; }); else if (type == 1) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c > r.c; }); else if (type == 2) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c < r.c; }); else if (type == 3) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c > r.c; }); else if (type == 4) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c < r.c; }); else if (type == 5) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c > r.c; }); else if (type == 6) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c < r.c; }); else if (type == 7) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c > r.c; }); else if (type == 8) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b < r.b; }); else if (type == 9) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b > r.b; }); else if (type == 10) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b < r.b; }); else if (type == 11) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b > r.b; }); else if (type == 12) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b < r.b; }); else if (type == 13) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b > r.b; }); else if (type == 14) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b < r.b; }); else if (type == 15) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b > r.b; }); else if (type == 16) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c < r.c; }); else if (type == 17) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c > r.c; }); else if (type == 18) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c < r.c; }); else if (type == 19) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c > r.c; }); else if (type == 20) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c < r.c; }); else if (type == 21) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c > r.c; }); else if (type == 22) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c < r.c; }); else if (type == 23) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c > r.c; }); else if (type == 24) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a < r.a; }); else if (type == 25) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a > r.a; }); else if (type == 26) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a < r.a; }); else if (type == 27) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a > r.a; }); else if (type == 28) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a < r.a; }); else if (type == 29) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a > r.a; }); else if (type == 30) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a < r.a; }); else if (type == 31) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a > r.a; }); else if (type == 32) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == 33) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == 34) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == 35) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == 36) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == 37) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == 38) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == 39) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == 40) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == 41) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == 42) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == 43) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a > r.a; }); else if (type == 44) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == 45) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == 46) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == 47) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a > r.a; }); } void sort(string &a) { sort(all(a)); } void sort(int &a, int &b) { if (a > b) swap(a, b); } void sort(int &a, int &b, int &c) { sort(a, b); sort(a, c); sort(b, c); } void rsort(int &a, int &b) { if (a < b) swap(a, b); } void rsort(int &a, int &b, int &c) { rsort(a, b); rsort(a, c); rsort(b, c); } template <class T> void sort(vector<T> &a) { sort(all(a)); } // P l, P rで f(P) の形で渡す template <class U, class F> void sort(vector<U> &a, F f) { sort(all(a), [&](U l, U r) { return f(l) < f(r); }); }; template <class T> void rsort(vector<T> &a) { sort(all(a), greater<T>()); }; template <class U, class F> void rsort(vector<U> &a, F f) { sort(all(a), [&](U l, U r) { return f(l) > f(r); }); }; // F = T<T> //例えばreturn p.fi + p.se; template <class A, class B> void sortp(vector<A> &a, vector<B> &b) { auto c = vtop(a, b); sort(c); rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se; } template <class A, class B, class F> void sortp(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); sort(c, f); rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se; } template <class A, class B> void rsortp(vector<A> &a, vector<B> &b) { auto c = vtop(a, b); rsort(c); rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second; } template <class A, class B, class F> void rsortp(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); rsort(c, f); rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second; } template <class A, class B, class C> void sortt(vector<A> &a, vector<B> &b, vector<C> &c) { auto d = vtot(a, b, c); sort(d); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class F> void sortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); sort(d, f); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C> void rsortt(vector<A> &a, vector<B> &b, vector<C> &c) { auto d = vtot(a, b, c); rsort(d); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class F> void rsortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); rsort(d, f); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class D> void sortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { auto e = vtof(a, b, c, d); sort(e); rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d; } template <class A, class B, class C, class D> void rsortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { auto e = vtof(a, b, c, d); rsort(e); rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d; } // sortindex 元のvectorはソートしない template <class T> vi sorti(vector<T> &a) { auto b = a; vi ind = iota(0, sz(a)); sortp(b, ind); return ind; } /*indexの分で型が変わるためpcomparatorが必要*/ template <class T> vi sorti(vector<T> &a, pcomparator f) { auto b = a; vi ind = iota(0, sz(a)); sortp(b, ind, f); return ind; } template <class T, class F> vi sorti(vector<T> &a, F f) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(a[x]) < f(a[y]); }); return ind; } template <class T> vi rsorti(vector<T> &a) { auto b = a; vi ind = iota(0, sz(a)); rsortp(b, ind); return ind; } template <class T, class F> vi rsorti(vector<T> &a, F f) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(a[x]) > f(a[y]); }); return ind; } template <class A, class B, class F> vi sortpi(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(c[x]) < f(c[y]); }); return ind; } template <class A, class B> vi sortpi(vector<A> &a, vector<B> &b, pcomparator f) { vi ind = iota(0, sz(a)); auto c = a; auto d = b; sortt(c, d, ind, f); return ind; } template <class A, class B> vi sortpi(vector<A> &a, vector<B> &b) { return sortpi(a, b, fisi); }; template <class A, class B, class F> vi rsortpi(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(c[x]) > f(c[y]); }); return ind; } template <class A, class B> vi rsortpi(vector<A> &a, vector<B> &b) { return sortpi(a, b, fdsd); }; template <class A, class B, class C, class F> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(d[x]) < f(d[y]); }); return ind; } template <class A, class B, class C> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, pcomparator f) { vi ind = iota(0, sz(a)); auto d = vtof(a, b, c, ind); sort(d, f); rep(i, sz(a)) ind[i] = d[i].d; return ind; } template <class A, class B, class C> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { if (a[x] == a[y]) { if (b[x] == b[y]) return c[x] < c[y]; else return b[x] < b[y]; } else { return a[x] < a[y]; } }); return ind; } template <class A, class B, class C, class F> vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(d[x]) > f(d[y]); }); return ind; } template <class A, class B, class C> vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { if (a[x] == a[y]) { if (b[x] == b[y]) return c[x] > c[y]; else return b[x] > b[y]; } else { return a[x] > a[y]; } }); return ind; } template <class T> void sort2(vector<vector<T>> &a) { for (ll i = 0, n = a.size(); i < n; ++i) sort(a[i]); } template <class T> void rsort2(vector<vector<T>> &a) { for (ll i = 0, n = a.size(); i < n; ++i) rsort(a[i]); } template <class... T, class U> auto sorted(U head, T... a) { sort(head, a...); return head; } 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 W, typename T> void fill(W &xx, const T vall) { xx = vall; } template <typename W, typename T> void fill(vector<W> &vecc, const T vall) { for (auto &&vx : vecc) fill(vx, vall); } template <typename W, typename T> void fill(vector<W> &xx, ll len, const T v) { rep(i, len) xx[i] = v; } template <typename W, typename T> void fill(vector<vector<W>> &xx, int sh, int th, int sw, int tw, T v) { rep(h, sh, th) rep(w, sw, tw) xx[h][w] = v; } template <class T, class U> void fill(vector<T> &a, vi &ind, U val) { fora(v, ind) a[v] = val; } template <class W, class T> void fill(mvec<W> &xx, const T v) { fora(x, xx) fill(x, v); } template <typename A, size_t N> A sum(A (&a)[N]) { A res = 0; rep(i, N) res += a[i]; return res; } template <typename A, size_t N, size_t O> A sum(A (&a)[N][O]) { A res = 0; rep(i, N) rep(j, O) res += a[i][j]; return res; } template <typename A, size_t N, size_t O, size_t P> A sum(A (&a)[N][O][P]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) res += a[i][j][k]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A sum(A (&a)[N][O][P][Q]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) res += a[i][j][k][l]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A sum(A (&a)[N][O][P][Q][R]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) res += a[i][j][k][l][m]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A sum(A (&a)[N][O][P][Q][R][S]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) rep(n, S) res += a[i][j][k][l][m][n]; return res; } //@汎用便利関数 入力 ll in() { ll ret; cin >> ret; return ret; } string sin() { string ret; cin >> ret; return ret; } template <class T> void in(T &head) { cin >> head; } template <class T, class... U> void in(T &head, U &...tail) { cin >> head; in(tail...); } #define din1(a) \ ll a; \ cin >> a #define din2(a, b) \ ll a, b; \ cin >> a >> b #define din3(a, b, c) \ ll a, b, c; \ cin >> a >> b >> c #define din4(a, b, c, d) \ ll a, b, c, d; \ cin >> a >> b >> c >> d #define din5(a, b, c, d, e) \ ll a, b, c, d, e; \ cin >> a >> b >> c >> d >> e #define din6(a, b, c, d, e, f) \ ll a, b, c, d, e, f; \ cin >> a >> b >> c >> d >> e >> f #define din(...) \ over6(__VA_ARGS__, din6, din5, din4, din3, din2, din1)(__VA_ARGS__) #define dins1(a) \ str a; \ cin >> a #define dins2(a, b) \ str a, b; \ cin >> a >> b #define dins3(a, b, c) \ str a, b, c; \ cin >> a >> b >> c #define dins4(a, b, c, d) \ str a, b, c, d; \ cin >> a >> b >> c >> d #define dins5(a, b, c, d, e) \ str a, b, c, d, e; \ cin >> a >> b >> c >> d >> e #define dins6(a, b, c, d, e, f) \ str a, b, c, d, e, f; \ cin >> a >> b >> c >> d >> e >> f #define dins(...) \ over6(__VA_ARGS__, dins6, dins5, dins4, dins3, dins2, dins1)(__VA_ARGS__) #define din1d(a) \ din1(a); \ a-- #define din2d(a, b) \ din2(a, b); \ a--, b-- #define din3d(a, b, c) \ din3(a, b, c); \ a--, b--, c-- #define din4d(a, b, c, d) \ din4(a, b, c, d); \ a--, b--, c--, d-- #define dind(...) over4(__VA_ARGS__, din4d, din3d, din2d, din1d)(__VA_ARGS__) template <class T> void out2(T &&head) { cout << head; } template <class T, class... U> void out2(T &&head, U &&...tail) { cout << head << " "; out2(tail...); } template <class T, class... U> void out(T &&head, U &&...tail) { cout << head << " "; out2(tail...); cout << "" << endl; } template <class T> void out(T &&head) { cout << head << endl; } void out() { cout << "" << endl; } #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; } /*@formatter:on*/ 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]; if (typeid(a[i]) == typeid(P)) { ss << endl; } else { ss << " "; } } return ss.str(); } /*@formatter:off*/ 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(); } 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 = 12; H = min(H, sz(a)); rep(i, H) { ss << endl; ss << out_m2(a[i], W, U, i); } return ss.str(); } template <class T, size_t N> string out_m2(T (&a)[N]) { vector<T> b; resize(b, N); rep(i, N) { b[i] = a[i]; } return out_m2(b); } template <class T, size_t N, size_t M> string out_m2(T (&a)[N][M]) { vector<vector<T>> b; resize(b, N, M); rep(i, N) { rep(j, M) { b[i][j] = a[i][j]; } } return out_m2(b); } template <class T, size_t N, size_t M, size_t O> string out_m2(T (&a)[N][M][O]) { vector<vector<vector<T>>> b; resize(b, N, M, O); rep(i, N) { rep(j, M) { rep(k, O) { b[i][j][k] = a[i][j][k]; } } } return out_m2(b); } string out_m2(int a) { stringstream ss; ss << a; return ss.str(); } /*@formatter:on*/ template <class T> string out_m2(mvec<mvec<T>> &a, ll H = inf, ll W = inf, int key = inf) { H = min({H, sz(a), 6ll}); W = min({W, sz(a[0]), 6ll}); stringstream ss; ss << endl; // if (key == inf)ss << " *|"; else ss << " " << key << "|"; if (key == inf) ss << " *|"; else { ss << std::right << std::setw(2) << key; ss << "|"; } rep(w, -W, W) ss << std::right << std::setw(4) << w; ss << "" << endl; rep(w, W * 8 + 3) ss << "_"; ss << "" << endl; rep(h, -H, H) { ss << std::right << std::setw(2) << h << "|"; int NW = min(sz(a[h]), 6ll); rep(w, -NW, NW) { if (abs(a[h][w]) == linf) ss << " e" << ""; else ss << std::right << std::setw(4) << a[h][w]; } ss << "" << endl; } return ss.str(); } /*@formatter:on*/ template <class T> string out_m2(mvec<mvec<mvec<T>>> &a, ll H = inf, ll W = inf, ll U = inf) { stringstream ss; if (H == inf) H = 6; H = min(H, sz(a)); rep(i, -H, H) { ss << endl; ss << out_m2(a[i], W, U, i); } return ss.str(); } /*@formatter:off*/ template <class T> string out_m2(T &a) { stringstream ss; ss << a; return ss.str(); } /*@formatter:on*/ 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(); } /*@formatter:off*/ 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(); } 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 template <class T> void outl(vector<T> &a, int n = inf) { rep(i, min(n, sz(a))) cout << a[i] << endl; } //テーブルをスペースなしで出力 template <class T> void outt(vector<vector<T>> &a) { rep(i, sz(a)) { rep(j, sz(a[i])) { cout << a[i][j]; } cout << endl; } } // int型をbit表記で出力 void outb(int a) { cout << bitset<20>(a) << endl; } template <class T> void na(vector<T> &a, ll n) { a.resize(n); rep(i, n) cin >> a[i]; } #define dna(a, n) \ vi a(n); \ rep(dnai, n) cin >> a[dnai]; #define dnad(a, n) \ vi a(n); \ rep(dnai, n) cin >> a[dnai], a[dnai]--; template <class T> void nao(vector<T> &a, ll n) { a.resize(n + 1); a[0] = 0; rep(i, n) cin >> a[i + 1]; } template <class T> void naod(vector<T> &a, ll n) { a.resize(n + 1); a[0] = 0; rep(i, n) cin >> a[i + 1], a[i + 1]--; } template <class T> void nad(vector<T> &a, ll n) { a.resize(n); rep(i, n) cin >> a[i], a[i]--; } template <class T, class U> void na2(vector<T> &a, vector<U> &b, ll n) { a.resize(n); b.resize(n); rep(i, n) cin >> a[i] >> b[i]; } #define dna2(a, b, n) \ vi a(n), b(n); \ rep(dna2i, n) cin >> a[dna2i] >> b[dna2i]; template <class T, class U> void nao2(vector<T> &a, vector<U> &b, ll n) { a.resize(n + 1); b.resize(n + 1); a[0] = b[0] = 0; rep(i, n) cin >> a[i + 1] >> b[i + 1]; } #define dna2d(a, b, n) \ vi a(n), b(n); \ rep(dna2di, n) { \ cin >> a[dna2di] >> b[dna2di]; \ a[dna2di]--, b[dna2di]--; \ } template <class T, class U> void na2d(vector<T> &a, vector<U> &b, ll n) { a.resize(n); b.resize(n); rep(i, n) cin >> a[i] >> b[i], a[i]--, b[i]--; } template <class T, class U, class W> void na3(vector<T> &a, vector<U> &b, vector<W> &c, ll n) { a.resize(n); b.resize(n); c.resize(n); rep(i, n) cin >> a[i] >> b[i] >> c[i]; } #define dna3(a, b, c, n) \ vi a(n), b(n), c(n); \ rep(dna3i, n) cin >> a[dna3i] >> b[dna3i] >> c[dna3i]; template <class T, class U, class W> void na3d(vector<T> &a, vector<U> &b, vector<W> &c, ll 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 dna3d(a, b, c, n) \ vi a(n), b(n), c(n); \ rep(dna3di, n) { \ cin >> a[dna3di] >> b[dna3di] >> c[dna3di]; \ a[dna3di]--, b[dna3di]--, c[dna3di]--; \ } template <class T, class U, class W, class X> void na4(vector<T> &a, vector<U> &b, vector<W> &c, vector<X> &d, ll n) { a.resize(n); b.resize(n); c.resize(n); d.resize(n); rep(i, n) cin >> a[i] >> b[i] >> c[i] >> d[i]; } #define dna4(a, b, c, d, n) \ vi a(n), b(n), c(n), d(n); \ rep(dna4i, n) cin >> a[dna4i] >> b[dna4i] >> c[dna4i] >> d[dna4i]; #define dna4d(a, b, c, d, n) \ vi a(n), b(n), c(n), d(n); \ rep(dna4i, n) cin >> a[dna4i] >> b[dna4i] >> c[dna4i] >> d[dna4i], \ --a[dna4i], --b[dna4i], --c[dna4i], --d[dna4i]; #define nt(a, h, w) \ resize(a, h, w); \ rep(nthi, h) rep(ntwi, w) cin >> a[nthi][ntwi]; #define ntd(a, h, w) \ resize(a, h, w); \ rep(ntdhi, h) rep(ntdwi, w) cin >> a[ntdhi][ntdwi], a[ntdhi][ntdwi]--; #define ntp(a, h, w) \ resize(a, h + 2, w + 2); \ fill(a, '#'); \ rep(ntphi, 1, h + 1) rep(ntpwi, 1, w + 1) cin >> a[ntphi][ntpwi]; //デバッグ #define sp << " " << #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__) #ifdef _DEBUG #define deb(...) \ cerr << over10(__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' //@formatter:off //よく使うクラス、構造体 // graphでredefineしているため、書き換えたら向こうも書き換える struct unionfind { vector<ll> par; vector<ll> siz; vector<ll> es; ll n, trees; //連結グループの数(親の種類) unionfind(ll n) : n(n), trees(n) { par.resize(n); siz.resize(n); es.resize(n); for (ll i = 0; i < n; i++) { par[i] = i; siz[i] = 1; } } ll root(ll x) { if (par[x] == x) { return x; } else { return par[x] = root(par[x]); } } ll operator()(ll x) { return root(x); } bool unite(ll x, ll y) { x = root(x); y = root(y); es[x]++; if (x == y) return false; if (siz[x] > siz[y]) swap(x, y); trees--; par[x] = y; siz[y] += siz[x]; es[y] += es[x]; return true; } bool same(ll x, ll y) { return root(x) == root(y); } ll size(ll x) { return siz[root(x)]; } ll esize(ll x) { return es[root(x)]; } vi sizes() { vi cou(n); vi ret; ret.reserve(n); rep(i, n) { cou[root(i)]++; } rep(i, n) { if (cou[i]) ret.push_back(cou[i]); } return ret; } //つながりを無向グラフと見なし、xが閉路に含まれるか判定 bool close(ll x) { return esize(x) >= size(x); } vector<vi> sets() { vi ind(n, -1); ll i = 0; vvi(res, trees); rep(j, n) { ll r = root(j); if (ind[r] == -1) ind[r] = i++; res[ind[r]].push_back(j); } rep(i, trees) { ll r = root(res[i][0]); if (res[i][0] == r) continue; rep(j, 1, sz(res[i])) { if (res[i][j] == r) { swap(res[i][0], res[i][j]); break; } } } return res; } }; //@formatter:off using bll = __int128; using u32 = unsigned; using u64 = unsigned long long; using u128 = __uint128_t; std::ostream &operator<<(std::ostream &dest, __int128_t value) { std::ostream::sentry s(dest); if (s) { __uint128_t tmp = value < 0 ? -value : value; char buffer[128]; char *d = std::end(buffer); do { --d; *d = "0123456789"[tmp % 10]; tmp /= 10; } while (tmp != 0); if (value < 0) { --d; *d = '-'; } ll len = std::end(buffer) - d; if (dest.rdbuf()->sputn(d, len) != len) { dest.setstate(std::ios_base::badbit); } } return dest; } //__int128 toi128(string &s) { __int128 ret = 0; for (ll i = 0; i < //s.length(); ++i) if ('0' <= s[i] && s[i] <= '9') ret = 10 * //ret + s[i] - '0'; return ret;} //エラー 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 re() { assert(0 == 1); exit(0); } void tle() { while (inf) cout << inf << endl; } //便利関数 //テスト用 #define rand xor128_ unsigned long xor128_(void) { static unsigned long x = 123456789, y = 362436069, z = 521288629, w = 88675123; unsigned long t; t = (x ^ (x << 11)); x = y; y = z; z = w; return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8))); } char ranc() { return (char)('a' + rand() % 26); } ll rand(ll min, ll max) { assert(min <= max); if (min >= 0 && max >= 0) { return rand() % (max + 1 - min) + min; } else if (max < 0) { return -rand(-max, -min); } else { if (rand() % 2) { return rand(0, max); } else { return -rand(0, -min); } } } vi ranv(ll n, ll min, ll max) { vi v(n); rep(i, n) v[i] = rand(min, max); return v; } str ransu(ll n) { str s; rep(i, n) s += (char)rand('A', 'Z'); return s; } str ransl(ll n) { str s; rep(i, n) s += (char)rand('a', 'z'); return s; } //単調増加 vi ranvinc(ll n, ll min, ll max) { vi v(n); bool bad = 1; while (bad) { bad = 0; v.resize(n); rep(i, n) { if (i && min > max - v[i - 1]) { bad = 1; break; } if (i) v[i] = v[i - 1] + rand(min, max - v[i - 1]); else v[i] = rand(min, max); } } return v; } //便利 汎用 void ranvlr(ll n, ll min, ll max, vi &l, vi &r) { l.resize(n); r.resize(n); rep(i, n) { l[i] = rand(min, max); r[i] = l[i] + rand(0, max - l[i]); } } template <class T> vector<pair<T, int>> run_length(vector<T> &a) { vector<pair<T, int>> 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 <class F> ll mgr(ll ok, ll ng, F f) { bool han = true; if (ok < ng) while (ng - ok > 1) { ll mid = (ok + ng) >> 1; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; deb(mid, han); } else while (ok - ng > 1) { ll mid = (ok + ng) >> 1; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; deb(mid, han); } return ok; } template <class F> dou mgrd(dou ok, dou ng, F f, int kai = 100) { bool han = true; if (ok < ng) rep(i, kai) { dou mid = (ok + ng) / 2; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; deb(mid, han); } else rep(i, kai) { dou mid = (ok + ng) / 2; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; deb(mid, han); } return ok; } // strを整数として比較 string smax(str &a, str b) { if (sz(a) < sz(b)) { return b; } else if (sz(a) > sz(b)) { return a; } else if (a < b) return b; else return a; } // strを整数として比較 string smin(str &a, str b) { if (sz(a) > sz(b)) { return b; } else if (sz(a) < sz(b)) { return a; } else if (a > b) return b; else return a; } //エラー-1 template <typename W, typename T> ll find(vector<W> &a, int l, const T key) { rep(i, l, sz(a)) if (a[i] == key) return i; return -1; } template <typename W, typename T> ll find(vector<W> &a, const T key) { rep(i, sz(a)) if (a[i] == key) return i; return -1; } template <typename W, typename T> P find(vector<vector<W>> &a, const T key) { rep(i, sz(a)) rep(j, sz(a[0])) if (a[i][j] == key) return mp(i, j); return mp(-1, -1); } template <typename W, typename U> T find(vector<vector<vector<W>>> &a, const U key) { rep(i, sz(a)) rep(j, sz(a[0])) rep(k, sz(a[0][0])) if (a[i][j][k] == key) return mt(i, j, k); return mt(-1, -1, -1); } // stringも書く int find(string &s, const string key) { int klen = sz(key); rep(i, sz(s) - klen + 1) { if (s[i] != key[0]) continue; if (s.substr(i, klen) == key) { return i; } } return -1; } int find(string &s, int l, const string key) { int klen = sz(key); rep(i, l, sz(s) - klen + 1) { if (s[i] != key[0]) continue; if (s.substr(i, klen) == key) { return i; } } return -1; } int find(string &s, const char key) { rep(i, sz(s)) { if (s[i] == key) return i; } return -1; } int find(string &s, int l, const char key) { rep(i, l, sz(s)) { if (s[i] == key) return i; } return -1; } template <typename W, typename T> ll count2(W &a, const T k) { return a == k; } template <typename W, typename T> ll count2(vector<W> &a, const T k) { ll ret = 0; fora(v, a) ret += count2(v, k); return ret; } template <typename W, typename T> ll count(vector<W> &a, const T k) { ll ret = 0; fora(v, a) ret += count2(v, k); return ret; } vi count(vi &a) { int ma = 0; fora(v, a) { if (ma < v) ma = v; } vi res(ma + 1); fora(v, a) { res[v]++; } return res; } ll count(str &a, str k) { ll ret = 0, len = k.length(); auto pos = a.find(k); while (pos != string::npos) pos = a.find(k, pos + len), ++ret; return ret; } vi count(str &a) { vi cou(26); char c = 'a'; if ('A' <= a[0] && a[0] <= 'Z') c = 'A'; rep(i, sz(a))++ cou[a[i] - c]; return cou; } #define couif count_if // algorythm 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)); } template <class U> void rev(vector<vector<U>> &a) { vector<vector<U>> b(sz(a[0]), vector<U>(sz(a))); rep(h, sz(a)) rep(w, sz(a[0])) b[w][h] = a[h][w]; a = b; } void rev(string &a) { reverse(all(a)); } constexpr ll p10[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000ll, 100000000000ll, 1000000000000ll, 10000000000000ll, 100000000000000ll, 1000000000000000ll, 10000000000000000ll, 100000000000000000ll, 1000000000000000000ll}; ll get(ll a, ll keta) { return (a / (ll)pow(10, keta)) % 10; } // 0は0桁 ll keta(ll v) { if (v < p10[9]) { if (v < p10[4]) { if (v < p10[2]) { if (v < p10[1]) { if (v < p10[0]) return 0; else return 1; } else return 2; } else { if (v < p10[3]) return 3; else return 4; } } else { if (v < p10[7]) { if (v < p10[5]) return 5; else if (v < p10[6]) return 6; else return 7; } else { if (v < p10[8]) return 8; else return 9; } } } else { if (v < p10[13]) { if (v < p10[11]) { if (v < p10[10]) return 10; else return 11; } else { if (v < p10[12]) return 12; else return 13; } } else { if (v < p10[15]) { if (v < p10[14]) return 14; else return 15; } else { if (v < p10[17]) { if (v < p10[16]) return 16; else return 17; } else { if (v < p10[18]) return 18; else return 19; } } } } } ll dsum(ll v, ll sin = 10) { ll ret = 0; for (; v; v /= sin) ret += v % sin; return ret; } ll mask10(ll v) { return p10[v] - 1; } //変換系 //[v] := iとなるようなvectorを返す //存在しない物は-1 template <class T> auto keys(T &a) { vector<decltype((a.begin())->fi)> res; for (auto &&k : a) res.push_back(k.fi); return res; } template <class T> auto values(T &a) { vector<decltype((a.begin())->se)> res; for (auto &&k : a) res.push_back(k.se); return res; } template <class T, class U> bool chma(T &a, const U &b) { if (a < b) { a = b; return true; } return false; } template <class T, class U> bool chmi(T &a, const U &b) { if (b < a) { a = b; return true; } return false; } template <class T> constexpr T min(T a, signed b) { return a < b ? a : b; } template <class T> constexpr T max(T a, signed b) { return a < b ? b : a; } template <class T> constexpr T min(T a, T b, T c) { return a >= b ? b >= c ? c : b : a >= c ? c : a; } template <class T> constexpr T max(T a, T b, T c) { return a <= b ? b <= c ? c : b : a <= c ? c : a; } template <class T> T min(vector<T> &a) { return *min_element(all(a)); } template <class T> T mini(vector<T> &a) { return min_element(all(a)) - a.begin(); } template <class T> T min(vector<T> &a, ll n) { return *min_element(a.begin(), a.begin() + min(n, sz(a))); } template <class T> T min(vector<T> &a, ll s, ll n) { return *min_element(a.begin() + s, a.begin() + min(n, sz(a))); } template <class T> T max(vector<T> &a) { return *max_element(all(a)); } template <class T, class U> T max(vector<T> &a, vector<U> &b) { return max(*max_element(all(a)), *max_element(all(b))); } template <class T> T maxi(vector<T> &a) { return max_element(all(a)) - a.begin(); } template <class T> T max(vector<T> &a, ll n) { return *max_element(a.begin(), a.begin() + min(n, sz(a))); } template <class T> T max(vector<T> &a, ll s, ll n) { return *max_element(a.begin() + s, a.begin() + min(n, sz(a))); } template <typename A, size_t N> A max(A (&a)[N]) { A res = a[0]; rep(i, N) res = max(res, a[i]); return res; } template <typename A, size_t N, size_t O> A max(A (&a)[N][O]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P> A max(A (&a)[N][O][P]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A max(A (&a)[N][O][P][Q], const T &v) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A max(A (&a)[N][O][P][Q][R]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A max(A (&a)[N][O][P][Q][R][S]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N> A min(A (&a)[N]) { A res = a[0]; rep(i, N) res = min(res, a[i]); return res; } template <typename A, size_t N, size_t O> A min(A (&a)[N][O]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P> A min(A (&a)[N][O][P]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A min(A (&a)[N][O][P][Q], const T &v) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A min(A (&a)[N][O][P][Q][R]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A min(A (&a)[N][O][P][Q][R][S]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <class T> T sum(vector<T> &v, ll s, ll t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += v[i]; return ret; } template <class T> T sum(vector<T> &v, ll t = inf) { return sum(v, 0, t); } template <class T> T sum(vector<vector<T>> &v, int s, int t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<T>> &v, int t = inf) { return sum(v, 0, t); } template <class T> T sum(vector<vector<vector<T>>> &v, int s, int t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<T>>> &v, int t = inf) { return sum(v, 0, t); } template <class T> T sum(vector<vector<vector<vector<T>>>> &v, int s, int t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<vector<T>>>> &v, int t = inf) { return sum(v, 0, t); } template <class T> T sum(vector<vector<vector<vector<vector<T>>>>> &v, int s, int t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<vector<vector<T>>>>> &v, int t = inf) { return sum(v, 0, t); } template <class T> auto sum(priority_queue<T, vector<T>, greater<T>> &r) { auto q = r; T ret = 0; while (sz(q)) { ret += q.top(); q.pop(); } return ret; } template <class T> auto sum(priority_queue<T> &r) { auto q = r; T ret = 0; while (sz(q)) { ret += q.top(); q.pop(); } return ret; } // template<class T, class U, class... W> auto sumn(vector<T> &v, U head, W... // tail) { auto ret = sum(v[0], tail...); rep(i, 1, min(sz(v), head))ret += // sum(v[i], tail...); return ret;} vi v_i(vi &a) { int n = max(a) + 1; vi ret(n, -1); rep(i, sz(a)) { ret[a[i]] = i; } return ret; } void clear(PQ &q) { q = PQ(); } void clear(priority_queue<int> &q) { q = priority_queue<int>(); } template <class T> void clear(queue<T> &q) { while (q.size()) q.pop(); } template <class T> T *negarr(ll size) { T *body = (T *)malloc((size * 2 + 1) * sizeof(T)); return body + size; } template <class T> T *negarr2(ll h, ll w) { double **dummy1 = new double *[2 * h + 1]; double *dummy2 = new double[(2 * h + 1) * (2 * w + 1)]; dummy1[0] = dummy2 + w; for (ll i = 1; i <= 2 * h + 1; ++i) { dummy1[i] = dummy1[i - 1] + 2 * w + 1; } double **a = dummy1 + h; return a; } // 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> vector<T> ima(vector<T> &v) { vector<T> ret = v; rep(i, sz(ret) - 1) chma(ret[i + 1], ret[i]); return ret; } // template<class T> vector<T> rimi(vector<T> &v) { vector<T> ret = v; rer(i, // sz(ret) - 1, 1)chmi(ret[i - 1], ret[i]); return ret;} template<class T> // vector<T> rima(vector<T> &v) { vector<T> ret = v; rer(i, sz(ret) - 1, // 1)chma(ret[i - 1], ret[i]); return ret;} template <class T> struct ruiC { vector<T> rui; ruiC(vector<T> &ru) : rui(ru) {} /*先頭0*/ ruiC() : rui(1, 0) {} T operator()(ll l, ll r) { if (l > r) { cerr << "ruic "; deb(l, r); assert(0); } return rui[r] - rui[l]; } T operator()(int r) { return operator()(0, r); } /*ruiv[]をruic[]に変えた際意味が変わるのがまずいため()と統一*/ /*単体iを返す 累積でないことに注意(seg木との統一でこうしている)*/ // T operator[](ll i) { return rui[i + 1] - rui[i]; } T operator[](ll i) { return rui[i]; } /*0から順に追加される必要がある*/ void operator+=(T v) { rui.push_back(rui.back() + v); } void add(int i, T v) { if (sz(rui) - 1 != i) ole(); operator+=(v); } T back() { return rui.back(); } ll size() { return rui.size(); } auto begin() { return rui.begin(); } auto end() { return rui.end(); } }; template <class T> struct ruimax { template <typename Monoid> struct SegmentTree { /*pairで処理*/ int sz; vector<Monoid> seg; const Monoid M1 = mp(MIN(T), -1); Monoid f(Monoid a, Monoid b) { return max(a, b); } void build(vector<T> &a) { int n = sz(a); sz = 1; while (sz < n) sz <<= 1; seg.assign(2 * sz, M1); rep(i, n) { seg[i + sz] = mp(a[i], i); } for (int k = sz - 1; k > 0; k--) { seg[k] = f(seg[k << 1], seg[(k << 1) | 1]); } } Monoid query(int a, int b) { Monoid L = M1, R = M1; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) L = f(L, seg[a++]); if (b & 1) R = f(seg[--b], R); } return f(L, R); } Monoid operator[](const int &k) const { return seg[k + sz]; } }; private: vector<T> ve; SegmentTree<pair<T, int>> seg; vector<T> rv; vector<T> ri; bool build = false; public: int n; ruimax(vector<T> &a) : ve(a), n(sz(a)) { int index = -1; int ma = MIN(T); rv.resize(n + 1); ri.resize(n + 1); rv[0] = -linf; ri[0] = -1; rep(i, n) { if (chma(ma, a[i])) { index = i; } rv[i + 1] = ma; ri[i + 1] = index; } } T operator()(int l, int r) { if (!(l <= r && 0 <= l && r <= n)) { deb(l, r, n); assert(0); } if (l == 0) { return rv[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).first; } } T operator()(int r = inf) { return operator()(0, min(r, n)); } T operator[](int r) { return operator()(0, r); } T getv(int l, int r) { return operator()(l, r); } T geti(int l, int r) { assert(l <= r && 0 <= l && r <= n); if (l == 0) { return ri[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).second; } } T geti(int r = inf) { return geti(0, min(r, n)); }; T getv(int r = inf) { return getv(0, min(r, n)); }; auto begin() { return rv.begin(); } auto end() { return rv.end(); } }; template <class T> struct ruimin { template <typename Monoid> struct SegmentTree { /*pairで処理*/ int sz; vector<Monoid> seg; const Monoid M1 = mp(MAX(T), -1); Monoid f(Monoid a, Monoid b) { return min(a, b); } void build(vector<T> &a) { int n = sz(a); sz = 1; while (sz < n) sz <<= 1; seg.assign(2 * sz, M1); rep(i, n) { seg[i + sz] = mp(a[i], i); } for (int k = sz - 1; k > 0; k--) { seg[k] = f(seg[k << 1], seg[(k << 1) | 1]); } } Monoid query(int a, int b) { Monoid L = M1, R = M1; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) L = f(L, seg[a++]); if (b & 1) R = f(seg[--b], R); } return f(L, R); } Monoid operator[](const int &k) const { return seg[k + sz]; } }; private: vector<T> ve; SegmentTree<pair<T, int>> seg; vector<T> rv; vector<T> ri; bool build = false; int n; public: ruimin(vector<T> &a) : ve(a), n(sz(a)) { int index = -1; int mi = MAX(T); rv.resize(n + 1); ri.resize(n + 1); rv[0] = linf; ri[0] = -1; rep(i, n) { if (chmi(mi, a[i])) { index = i; } rv[i + 1] = mi; ri[i + 1] = index; } } T operator()(int l, int r) { assert(l <= r && 0 <= l && r <= n); if (l == 0) { return rv[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).first; } } T operator()(int r = inf) { return operator()(0, min(r, n)); } T operator[](int r) { return operator()(0, r); } T getv(int l, int r) { return operator()(l, r); } T geti(int l, int r) { assert(l <= r && 0 <= l && r <= n); if (l == 0) { return ri[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).second; } } T geti(int r = inf) { return geti(0, min(r, n)); }; T getv(int r = inf) { return getv(0, min(r, n)); }; auto begin() { return rv.begin(); } auto end() { return rv.end(); } }; 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() { return ruiC<T>(); } template <class T> ruiC<T> ruic(vector<T> &a) { vector<T> ret = ruiv(a); return ruiC<T>(ret); } vvi() ruib(vi &a) { vvi(res, 61, sz(a) + 1); rep(k, 61) { rep(i, sz(a)) { res[k][i + 1] = res[k][i] + ((a[i] >> k) & 1); } } return res; } vector<ruiC<int>> ruibc(vi &a) { vector<ruiC<int>> ret(61); vvi(res, 61, sz(a)); rep(k, 61) { rep(i, sz(a)) { res[k][i] = (a[i] >> k) & 1; } ret[k] = ruic(res[k]); } return 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); } // h query template <class T> vector<T> imoh(vector<vector<T>> &v, int w) { vector<T> ret(sz(v)); rep(h, sz(ret)) { ret[h] = v[h][w]; } rep(i, sz(ret) - 1) { ret[i + 1] += ret[i]; } return ret; } template <class T> vector<T> ruih(vector<vector<T>> &v, int w) { vector<T> ret(sz(v) + 1); rep(h, sz(v)) { ret[h + 1] = v[h][w]; } rep(i, sz(v)) { ret[i + 1] += ret[i]; } return ret; } template <class T> ruiC<T> ruihc(vector<vector<T>> &a, int w) { vector<T> ret = ruih(a, w); return ruiC<T>(ret); } // xor template <class T> struct ruixC { 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> struct rruiC { vector<T> rui; int n; rruiC(vector<T> &a) : n(sz(a)) { rui.resize(n + 1); rer(i, n - 1) { rui[i] = rui[i + 1] + a[i]; } } /*[r l)*/ T operator()(int r, int l) { r++; l++; assert(l <= r && l >= 0 && r <= n); return rui[l] - rui[r]; } T operator()(int l) { return operator()(n - 1, l); } T operator[](int i) { return operator()(i); } }; template <class T> ostream &operator<<(ostream &os, rruiC<T> a) { fora(v, a.rui) os << v << " "; return os; } #define rrui rruic template <class T> rruiC<T> rruic(vector<T> &a) { return rruiC<T>(a); } //掛け算 template <class T> struct ruimulC { vector<T> rv; int n; ruimulC(vector<T> &a) : rv(a), n(sz(a)) { rv.resize(n + 1); rv[0] = 1; rep(i, n) { rv[i + 1] = a[i] * rv[i]; } } ruimulC() : n(0) { rv.resize(n + 1); rv[0] = 1; } void operator+=(T v) { rv.push_back(rv.back() * v); n++; } T operator()(int l, int r) { assert(l <= r && 0 <= l && r <= n); return rv[r] / rv[l]; } T operator()(int r = inf) { return operator()(0, min(r, n)); } T operator[](int r) { return operator()(0, r); } auto begin() { return rv.begin(); } auto end() { return rv.end(); } }; template <class T> ruimulC<T> ruimul(vector<T> &a) { return ruimulC<T>(a); } template <class T> ruimulC<T> ruimul() { vector<T> a; return ruimulC<T>(a); } /*@formatter:off*/ 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> void inc(pair<T, U> &a, U v = 1) { a.first += v, a.second += v; } template <class T, class U> void inc(T &a, U v = 1) { a += v; } template <class T, class U> void inc(vector<T> &a, U v = 1) { for (auto &u : a) inc(u, v); } template <class T, class U> void dec(T &a, U v = 1) { a -= v; } template <class T, class U> void dec(vector<T> &a, U v = 1) { for (auto &u : a) dec(u, v); } template <class U> void dec(string &a, U v = 1) { for (auto &u : a) dec(u, v); } template <class T, class U, class W> void dec(vector<T> &a, vector<U> &b, W v = 1) { for (auto &u : a) dec(u, v); for (auto &u : b) dec(u, v); } template <class T, class U, class W> void dec(vector<T> &a, vector<U> &b, vector<W> &c) { for (auto &u : a) dec(u, 1); for (auto &u : b) dec(u, 1); for (auto &u : c) dec(u, 1); } bool ins(ll h, ll w, ll H, ll W) { return h >= 0 && w >= 0 && h < H && w < W; } bool ins(ll l, ll v, ll r) { return l <= v && v < r; } template <class T> bool ins(vector<T> &a, ll i, ll j = 0) { return ins(0, i, sz(a)) && ins(0, j, sz(a)); } #define inside ins ll u(ll a) { return a < 0 ? 0 : a; } template <class T> vector<T> u(vector<T> &a) { vector<T> ret = a; fora(v, ret) v = u(v); return ret; } //添え字を返す template <class F> ll goldd_l(ll left, ll right, F calc) { double GRATIO = 1.6180339887498948482045868343656; ll lm = left + (ll)((right - left) / (GRATIO + 1.0)); ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); ll fl = calc(lm); ll fr = calc(rm); while (right - left > 10) { if (fl < fr) { right = rm; rm = lm; fr = fl; lm = left + (ll)((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } ll minScore = MAX(ll); ll resIndex = left; for (ll i = left; i < right + 1; ++i) { ll score = calc(i); if (minScore > score) { minScore = score; resIndex = i; } } return resIndex; } template <class F> ll goldt_l(ll left, ll right, F calc) { double GRATIO = 1.6180339887498948482045868343656; ll lm = left + (ll)((right - left) / (GRATIO + 1.0)); ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); ll fl = calc(lm); ll fr = calc(rm); while (right - left > 10) { if (fl > fr) { right = rm; rm = lm; fr = fl; lm = left + (ll)((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } if (left > right) { ll l = left; left = right; right = l; } ll maxScore = MIN(ll); ll resIndex = left; for (ll i = left; i < right + 1; ++i) { ll score = calc(i); if (maxScore < score) { maxScore = score; resIndex = i; } } return resIndex; } /*loopは200にすればおそらく大丈夫 余裕なら300に*/ template <class F> dou goldd_d(dou left, dou right, F calc, ll loop = 200) { dou GRATIO = 1.6180339887498948482045868343656; dou lm = left + ((right - left) / (GRATIO + 1.0)); dou rm = lm + ((right - lm) / (GRATIO + 1.0)); dou fl = calc(lm); dou fr = calc(rm); /*200にすればおそらく大丈夫*/ /*余裕なら300に*/ ll k = 141; loop++; while (--loop) { if (fl < fr) { right = rm; rm = lm; fr = fl; lm = left + ((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + ((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } return left; } template <class F> dou goldt_d(dou left, dou right, F calc, ll loop = 200) { double GRATIO = 1.6180339887498948482045868343656; dou lm = left + ((right - left) / (GRATIO + 1.0)); dou rm = lm + ((right - lm) / (GRATIO + 1.0)); dou fl = calc(lm); dou fr = calc(rm); loop++; while (--loop) { if (fl > fr) { right = rm; rm = lm; fr = fl; lm = left + ((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + ((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } return left; } // l ~ rを複数の区間に分割し、極致を与えるiを返す time-20 msまで探索 template <class F> ll goldd_ls(ll l, ll r, F calc, ll time = 2000) { auto lim = milliseconds(time - 20); ll mini = 0, minv = MAX(ll); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/ ll nl = l; ll nr = l + haba; rep(i, k) { ll ni = goldd_l(nl, nr, calc); if (chmi(minv, calc(ni))) mini = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return mini; } template <class F> ll goldt_ls(ll l, ll r, F calc, ll time = 2000) { auto lim = milliseconds(time - 20); ll maxi = 0, maxv = MIN(ll); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/ ll nl = l; ll nr = l + haba; rep(i, k) { ll ni = goldt_l(nl, nr, calc); if (chma(maxv, calc(ni))) maxi = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return maxi; } template <class F> dou goldd_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を持つ*/ auto lim = milliseconds(time - 20); dou mini = 0, minv = MAX(dou); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); dou haba = (r - l) / k; dou nl = l; dou nr = l + haba; rep(i, k) { dou ni = goldd_d(nl, nr, calc); if (chmi(minv, calc(ni))) mini = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return mini; } template <class F> dou goldt_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を残している*/ auto lim = milliseconds(time - 20); dou maxi = 0, maxv = MIN(dou); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); dou haba = (r - l) / k; dou nl = l; dou nr = l + haba; rep(i, k) { dou ni = goldt_d(nl, nr, calc); if (chma(maxv, calc(ni))) maxi = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return maxi; } template <class T> T min(vector<vector<T>> &a) { T res = MAX(T); rep(i, a.size()) chmi(res, *min_element(all(a[i]))); return res; } template <class T> T max(vector<vector<T>> &a) { T res = MIN(T); rep(i, a.size()) chma(res, *max_element(all(a[i]))); return res; } constexpr bool bget(ll m, ll keta) { #ifdef _DEBUG assert(keta <= 62); //オーバーフロー 1^62までしか扱えない #endif return (m >> keta) & 1; } // bget(n)次元 // NならN-1まで vector<vi> bget2(vi &a, int keta_size) { vvi(res, keta_size, sz(a)); rep(k, keta_size) { rep(i, sz(a)) { res[k][i] = bget(a[i], k); } } return res; } vi bget1(vi &a, int keta) { vi res(sz(a)); rep(i, sz(a)) { res[i] = bget(a[i], keta); } return res; } ll bget(ll m, ll keta, ll sinsuu) { m /= (ll)pow(sinsuu, keta); return m % sinsuu; } constexpr ll bit(ll n) { #ifdef _DEBUG assert(n <= 62); //オーバーフロー 1^62までしか扱えない #endif return (1LL << (n)); } ll bit(ll n, ll sinsuu) { return (ll)pow(sinsuu, n); } ll mask(ll n) { return (1ll << n) - 1; } // aをbitに置きなおす //{0, 2} -> 101 ll biti(vi &a) { int m = 0; for (auto &&v : a) m |= bit(v); return m; } //{1, 1, 0} -> 011 ll bitb(vi &a) { int m = 0; rep(i, sz(a)) if (a[i]) m |= bit(i); return m; } #define bcou __builtin_popcountll //最下位ビット ll lbit(ll n) { return n & -n; } ll lbiti(ll n) { return log2(n & -n); } //最上位ビット ll hbit(ll n) { n |= (n >> 1); n |= (n >> 2); n |= (n >> 4); n |= (n >> 8); n |= (n >> 16); n |= (n >> 32); return n - (n >> 1); } ll hbiti(ll n) { return log2(hbit(n)); } ll hbitk(ll n) { ll k = 0; rer(i, 5) { ll a = k + (1ll << i); ll b = 1ll << a; if (b <= n) k += 1ll << i; } return k; } //初期化は0を渡す ll nextComb(ll &mask, ll n, ll r) { if (!mask) return mask = (1LL << r) - 1; ll x = mask & -mask; /*最下位の1*/ ll y = mask + x; /*連続した下の1を繰り上がらせる*/ ll res = ((mask & ~y) / x >> 1) | y; if (bget(res, n)) return mask = 0; else return mask = res; } // n桁以下でビットがr個立っているもののvectorを返す vi bitCombList(ll n, ll r) { vi res; ll m = 0; while (nextComb(m, n, r)) { res.push_back(m); } return res; } // masの立ってるindexを見る #define forbit(i, mas) \ for (int forbitj = lbit(mas), forbitm = mas, i = log2(forbitj); forbitm; \ forbitm = forbitj ? forbitm ^ forbitj : 0, forbitj = lbit(forbitm), \ i = log2(forbitj)) char itoal(ll i) { return 'a' + i; } char itoaL(ll i) { return 'A' + i; } ll altoi(char c) { if ('A' <= c && c <= 'Z') return c - 'A'; return c - 'a'; } ll ctoi(char c) { return c - '0'; } char itoc(ll i) { return i + '0'; } ll vtoi(vi &v) { ll res = 0; if (sz(v) > 18) { debugline("vtoi"); deb(sz(v)); ole(); } rep(i, sz(v)) { res *= 10; res += v[i]; } return res; } vi itov(ll i) { vi res; while (i) { res.push_back(i % 10); i /= 10; } rev(res); return res; } vi stov(string &a) { ll n = sz(a); vi ret(n); rep(i, n) { ret[i] = a[i] - '0'; } return ret; } //基準を満たさないものは0になる vi stov(string &a, char one) { ll n = sz(a); vi ret(n); rep(i, n) ret[i] = a[i] == one; return ret; } vector<vector<ll>> ctoi(vector<vector<char>> s, char c) { ll n = sz(s), m = sz(s[0]); vector<vector<ll>> res(n, vector<ll>(m)); rep(i, n) rep(j, m) res[i][j] = s[i][j] == c; return res; } #define unique(v) v.erase(unique(v.begin(), v.end()), v.end()); //[i] := vを返す // aは0~n-1で置き換えられる vi compress(vi &a) { vi b; ll len = a.size(); for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { a[i] = lower_bound(all(b), a[i]) - b.begin(); } ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, umap<ll, ll> &map) { vi b; ll len = a.size(); for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { ll v = a[i]; a[i] = lower_bound(all(b), a[i]) - b.begin(); map[v] = a[i]; } ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, vi &r) { vi b; ll len = a.size(); fora(v, a) b.push_back(v); fora(v, r) b.push_back(v); sort(b); unique(b); for (ll i = 0; i < len; ++i) a[i] = lower_bound(all(b), a[i]) - b.begin(); for (ll i = 0; i < sz(r); ++i) r[i] = lower_bound(all(b), r[i]) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, vi &r, vi &s) { vi b; ll len = a.size(); fora(v, a) b.push_back(v); fora(v, r) b.push_back(v); fora(v, s) b.push_back(v); sort(b); unique(b); for (ll i = 0; i < len; ++i) a[i] = lower_bound(all(b), a[i]) - b.begin(); for (ll i = 0; i < sz(r); ++i) r[i] = lower_bound(all(b), r[i]) - b.begin(); for (ll i = 0; i < sz(s); ++i) r[i] = lower_bound(all(b), s[i]) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vector<vi> &a) { vi b; fora(vv, a) fora(v, vv) b.push_back(v); sort(b); unique(b); fora(vv, a) fora(v, vv) v = lower_bound(all(b), v) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vector<vector<vi>> &a) { vi b; fora(vvv, a) fora(vv, vvv) fora(v, vv) b.push_back(v); sort(b); unique(b); fora(vvv, a) fora(vv, vvv) fora(v, vv) v = lower_bound(all(b), v) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } void compress(ll a[], ll len) { vi b; for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { a[i] = lower_bound(all(b), a[i]) - b.begin(); } } //要素が見つからなかったときに困る #define binarySearch(a, v) (binary_search(all(a), v)) #define lowerIndex(a, v) (lower_bound(all(a), v) - a.begin()) #define upperIndex(a, v) (upper_bound(all(a), v) - a.begin()) #define rlowerIndex(a, v) (upper_bound(all(a), v) - a.begin() - 1) #define rupperIndex(a, v) (lower_bound(all(a), v) - a.begin() - 1) template <class T, class U, class W> T lowerBound(vector<T> &a, U v, W banpei) { auto it = lower_bound(a.begin(), a.end(), v); if (it == a.end()) return banpei; else return *it; } template <class T, class U, class W> T lowerBound(ruiC<T> &a, U v, W banpei) { return lowerBound(a.rui, v, banpei); } template <class T, class U, class W> T upperBound(vector<T> &a, U v, W banpei) { auto it = upper_bound(a.begin(), a.end(), v); if (it == a.end()) return banpei; else return *it; } template <class T, class U, class W> T upperBound(ruiC<T> &a, U v, W banpei) { return upperBound(a.rui, v, banpei); } template <class T, class U, class W> T rlowerBound(vector<T> &a, U v, W banpei) { auto it = upper_bound(a.begin(), a.end(), v); if (it == a.begin()) return banpei; else { return *(--it); } } template <class T, class U, class W> T rlowerBound(ruiC<T> &a, U v, W banpei) { return rlowerBound(a.rui, v, banpei); } template <class T, class U, class W> T rupperBound(vector<T> &a, U v, W banpei) { auto it = lower_bound(a.begin(), a.end(), v); if (it == a.begin()) return banpei; else { return *(--it); } } template <class T, class U, class W> T rupperBound(ruiC<T> &a, U v, W banpei) { return rupperBound(a.rui, v, banpei); } #define next2(a) next(next(a)) #define prev2(a) prev(prev(a)) //狭義の単調増加列 長さを返す template <class T> int lis(vector<T> &a) { int n = sz(a); vi tail(n + 1, MAX(T)); rep(i, n) { int id = lowerIndex(tail, a[i]); /**/ tail[id] = a[i]; } return lowerIndex(tail, MAX(T)); } template <class T> int lis_eq(vector<T> &a) { int n = sz(a); vi tail(n + 1, MAX(T)); rep(i, n) { int id = upperIndex(tail, a[i]); /**/ tail[id] = a[i]; } return lowerIndex(tail, MAX(T)); } // iteratorを返す // valueが1以上の物を返す 0は見つけ次第削除 // vを減らす場合 (*it).se--でいい template <class T, class U, class V> auto lower_map(map<T, U> &m, V k) { auto ret = m.lower_bound(k); while (ret != m.end() && (*ret).second == 0) { ret = m.erase(ret); } return ret; } template <class T, class U, class V> auto upper_map(map<T, U> &m, V k) { auto ret = m.upper_bound(k); while (ret != m.end() && (*ret).second == 0) { ret = m.erase(ret); } return ret; } //存在しなければエラー template <class T, class U, class V> auto rlower_map(map<T, U> &m, V k) { auto ret = upper_map(m, k); assert(ret != m.begin()); ret--; while (1) { if ((*ret).second != 0) break; assert(ret != m.begin()); auto next = ret; --next; m.erase(ret); ret = next; } return ret; } template <class T, class U, class V> auto rupper_map(map<T, U> &m, V k) { auto ret = lower_map(m, k); assert(ret != m.begin()); ret--; while (1) { if ((*ret).second != 0) break; assert(ret != m.begin()); auto next = ret; --next; m.erase(ret); ret = next; } return ret; } template <class... T> void fin(T... s) { out(s...); exit(0); } //便利 数学 math // sub ⊂ top bool subset(int sub, int top) { return (sub & top) == sub; } //-180 ~ 180 degree double atand(double h, double w) { return atan2(h, w) / PI * 180; } //% -mの場合、最小の正の数を返す ll mod(ll a, ll m) { if (m < 0) m *= -1; return (a % m + m) % m; } ll pow(ll a) { return a * a; }; ll fact(ll v) { return v <= 1 ? 1 : v * fact(v - 1); } dou factd(int v) { static vd fact(2, 1); if (sz(fact) <= v) { rep(i, sz(fact), v + 1) { fact.push_back(fact.back() * i); } } return fact[v]; } ll comi(ll n, ll r) { assert(n < 100); static vvi(pas, 100, 100); if (pas[0][0]) return pas[n][r]; pas[0][0] = 1; rep(i, 1, 100) { pas[i][0] = 1; rep(j, 1, i + 1) pas[i][j] = pas[i - 1][j - 1] + pas[i - 1][j]; } return pas[n][r]; } double comd2(ll n, ll r) { static vvd(comb, 2020, 2020); if (comb[0][0] == 0) { comb[0][0] = 1; rep(i, 2000) { comb[i + 1][0] = 1; rep(j, 1, i + 2) { comb[i + 1][j] = comb[i][j] + comb[i][j - 1]; } } } return comb[n][r]; } double comd(int n, int r) { if (r < 0 || r > n) return 0; if (n < 2020) return comd2(n, r); static vd fact(2, 1); if (sz(fact) <= n) { rep(i, sz(fact), n + 1) { fact.push_back(fact.back() * i); } } return fact[n] / fact[n - r] / fact[r]; } ll gcd(ll a, ll b) { while (b) a %= b, swap(a, b); return abs(a); } ll gcd(vi b) { ll res = b[0]; rep(i, 1, sz(b)) res = gcd(b[i], res); return res; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll lcm(vi a) { ll res = a[0]; rep(i, 1, sz(a)) res = lcm(a[i], res); return res; } ll ceil(ll a, ll b) { if (b == 0) { debugline("ceil"); deb(a, b); ole(); return -1; } else if (a < 0) { return 0; } else { return (a + b - 1) / b; } } ll sig0(ll t) { return t <= 0 ? 0 : ((1 + t) * t) >> 1; } // ll sig(ll s, ll t) { return ((s + t) * (t - s + 1)) >> 1; } ll sig(ll s, ll t) { if (s > t) swap(s, t); return sig0(t - s) + s * (t - s + 1); } #define tousa_i tosa_i #define lower_tousa_i lower_tosa_i #define upper_tousa upper_tosa #define upper_tousa_i upper_tosa_i ll tosa_i(ll st, ll ad, ll v) { assert(((v - st) % ad) == 0); return (v - st) / ad; } ll tosa_s(ll st, ll ad, ll len) { return st * len + sig0(len - 1) * ad; } // ax + r (x は非負整数) で表せる整数のうち、v 以上となる最小の整数 ll lower_tosa(ll st, ll ad, ll v) { if (st >= v) return st; return (v - st + ad - 1) / ad * ad + st; } //第何項か ll lower_tosa_i(ll st, ll ad, ll v) { if (st >= v) return 0; return (v - st + ad - 1) / ad; } ll upper_tosa(ll st, ll ad, ll v) { return lower_tosa(st, ad, v + 1); } ll upper_tosa_i(ll st, ll ad, ll v) { return lower_tosa_i(st, ad, v + 1); } // v * v >= aとなる最小のvを返す 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); } /*@formatter:off*/ //機能拡張 #define dtie(a, b) \ int a, b; \ tie(a, b) template <class T, class U> string to_string(T a, U b) { string res = ""; res += a; res += b; return res; } template <class T, class U, class V> string to_string(T a, U b, V c) { string res = ""; res += a; res += b; res += c; return res; } template <class T, class U, class V, class W> string to_string(T a, U b, V c, W d) { string res = ""; res += a; res += b; res += c; res += d; return res; } template <class T, class U, class V, class W, class X> string to_string(T a, U b, V c, W d, X e) { string res = ""; res += a; res += b; res += c; res += d; res += e; return res; } constexpr int bsetlen = k5 * 2; // constexpr int bsetlen = 5050; #define bset bitset<bsetlen> bool operator<(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] < b[i]) return true; if (a[i] > b[i]) return false; } return false; } bool operator>(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] > b[i]) return true; if (a[i] < b[i]) return false; } return false; } bool operator<=(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] < b[i]) return true; if (a[i] > b[i]) return false; } return true; } bool operator>=(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] > b[i]) return true; if (a[i] < b[i]) return false; } return true; } string operator~(string &a) { string res = a; for (auto &&c : res) { if (c == '0') c = '1'; else if (c == '1') c = '0'; else { cerr << "cant ~" << a << "must bit" << endl; exit(0); } } return res; } ostream &operator<<(ostream &os, bset &a) { bitset<10> b; vi list; rep(i, bsetlen) { if (a[i]) list.push_back(i), b[i] = 1; } os << b << ", " << list; return os; } int hbiti(bset &a) { rer(i, bsetlen) { if (a[i]) return i; } return -1; } #define hk(a, b, c) (a <= b && b < c) // O(N/64) bset nap(bset &a, int v) { bset r = a | a << v; return r; } bset nap(bset &a, bset &v) { bset r = a; rep(i, bsetlen) { if (v[i]) r |= a << i; } return r; } template <class T> void seth(vector<vector<T>> &S, int w, vector<T> &v) { assert(sz(S) == sz(v)); assert(w < sz(S[0])); rep(h, sz(S)) { S[h][w] = v[h]; } } template <class T, class U> void operator+=(pair<T, U> &a, pair<T, U> &b) { a.fi += b.fi; a.se += b.se; } template <class T, class U> pair<T, U> operator+(pair<T, U> &a, pair<T, U> &b) { return pair<T, U>(a.fi + b.fi, a.se + b.se); } template <typename CharT, typename Traits, typename Alloc> basic_string<CharT, Traits, Alloc> operator+(const basic_string<CharT, Traits, Alloc> &lhs, const int rv) { basic_string<CharT, Traits, Alloc> str(lhs); str.append(to_string(rv)); return str; } template <typename CharT, typename Traits, typename Alloc> void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const int rv) { lhs += to_string(rv); } template <typename CharT, typename Traits, typename Alloc> basic_string<CharT, Traits, Alloc> operator+(const basic_string<CharT, Traits, Alloc> &lhs, const signed rv) { basic_string<CharT, Traits, Alloc> str(lhs); str.append(to_string(rv)); return str; } template <typename CharT, typename Traits, typename Alloc> void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const signed rv) { lhs += to_string(rv); } template <typename CharT, typename Traits, typename Alloc> void operator*=(basic_string<CharT, Traits, Alloc> &s, int num) { auto bek = s; s = ""; for (; num; num >>= 1) { if (num & 1) { s += bek; } bek += bek; } } template <class T, class U> void operator+=(queue<T> &a, U v) { a.push(v); } template <class T, class U> void operator+=(deque<T> &a, U v) { a.push_back(v); } template <class T> priority_queue<T, vector<T>, greater<T>> & operator+=(priority_queue<T, vector<T>, greater<T>> &a, vector<T> &v) { fora(d, v) a.push(d); return a; } template <class T, class U> priority_queue<T, vector<T>, greater<T>> & operator+=(priority_queue<T, vector<T>, greater<T>> &a, U v) { a.push(v); return a; } template <class T, class U> priority_queue<T> &operator+=(priority_queue<T> &a, U v) { a.push(v); return a; } template <class T> set<T> &operator+=(set<T> &a, vector<T> v) { fora(d, v) a.insert(d); return a; } template <class T, class U> auto operator+=(set<T> &a, U v) { return a.insert(v); } template <class T, class U> auto operator-=(set<T> &a, U v) { return a.erase(v); } template <class T, class U> auto operator+=(mset<T> &a, U v) { return a.insert(v); } template <class T, class U> set<T, greater<T>> &operator+=(set<T, greater<T>> &a, U v) { a.insert(v); return a; } template <class T, class U> vector<T> &operator+=(vector<T> &a, U v) { a.push_back(v); return a; } template <class T, class U> vector<T> operator+(vector<T> &a, U v) { vector<T> ret = a; ret += v; return ret; } template <class T, class U> vector<T> operator+(U v, vector<T> &a) { vector<T> ret = a; ret.insert(ret.begin(), v); return ret; } template <class T> vector<T> operator+(vector<T> a, vector<T> b) { vector<T> ret; ret = a; fora(v, b) ret += v; return ret; } template <class T> vector<T> &operator+=(vector<T> &a, vector<T> &b) { rep(i, sz(b)) { /*こうしないとa+=aで両辺が増え続けてバグる*/ a.push_back(b[i]); } return a; } template <class T, class U> map<T, U> &operator+=(map<T, U> &a, map<T, U> &b) { fora(bv, b) { a[bv.first] += bv.second; } return a; } template <class T> vector<T> &operator-=(vector<T> &a, vector<T> &b) { if (sz(a) != sz(b)) { debugline("vector<T> operator-="); deb(a); deb(b); exit(0); } rep(i, sz(a)) a[i] -= b[i]; return a; } template <class T> vector<T> operator-(vector<T> &a, vector<T> &b) { if (sz(a) != sz(b)) { debugline("vector<T> operator-"); deb(a); deb(b); ole(); } vector<T> res(sz(a)); rep(i, sz(a)) res[i] = a[i] - b[i]; return res; } template <class T, class U> void operator*=(vector<T> &a, U b) { vector<T> ta = a; rep(b - 1) { a += ta; } } template <typename T> void erase(vector<T> &v, unsigned ll i) { v.erase(v.begin() + i); } template <typename T> void erase(vector<T> &v, unsigned ll s, unsigned ll e) { v.erase(v.begin() + s, v.begin() + e); } template <class T, class U> void erase(map<T, U> &m, ll okl, ll ngr) { m.erase(m.lower_bound(okl), m.lower_bound(ngr)); } template <class T> void erase(set<T> &m, ll okl, ll ngr) { m.erase(m.lower_bound(okl), m.lower_bound(ngr)); } template <typename T> void erasen(vector<T> &v, unsigned ll s, unsigned ll n) { v.erase(v.begin() + s, v.begin() + s + n); } template <typename T, typename U> void insert(vector<T> &v, unsigned ll i, U t) { v.insert(v.begin() + i, t); } template <typename T, typename U> void push_front(vector<T> &v, U t) { v.insert(v.begin(), t); } template <typename T, typename U> void insert(vector<T> &v, unsigned ll i, vector<T> list) { for (auto &&va : list) v.insert(v.begin() + i++, va); } template <typename T> void insert(set<T> &v, vector<T> list) { for (auto &&va : list) v.insert(va); } vector<string> split(const string a, const char deli) { string b = a + deli; ll l = 0, r = 0, n = b.size(); vector<string> res; rep(i, n) { if (b[i] == deli) { r = i; if (l < r) res.push_back(b.substr(l, r - l)); l = i + 1; } } return res; } vector<string> split(const string a, const string deli) { vector<string> res; ll kn = sz(deli); std::string::size_type Pos(a.find(deli)); ll l = 0; while (Pos != std::string::npos) { if (Pos - l) res.push_back(a.substr(l, Pos - l)); l = Pos + kn; Pos = a.find(deli, Pos + kn); } if (sz(a) - l) res.push_back(a.substr(l, sz(a) - l)); return res; } 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 fAb(bool a) { if (a) cout << "Alice" << endl; else cout << "Bob"; } 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 <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)}; } //未分類 // 0,2,1 1番目と2番目の次元を入れ替える template <class T> auto irekae(vector<vector<vector<T>>> &A, int x, int y, int z) { #define irekae_resize_loop(a, b, c) \ resize(res, a, b, c); \ rep(i, a) rep(j, b) rep(k, c) vector<vector<vector<T>>> res; if (x == 0 && y == 1 && z == 2) { res = A; } else if (x == 0 && y == 2 && z == 1) { irekae_resize_loop(sz(A), sz(A[0][0]), sz(A[0])) { res[i][j][k] = A[i][k][j]; } } else if (x == 1 && y == 0 && z == 2) { irekae_resize_loop(sz(A[0]), sz(A), sz(A[0][0])) { res[i][j][k] = A[j][i][k]; } } else if (x == 1 && y == 2 && z == 0) { irekae_resize_loop(sz(A[0]), sz(A[0][0]), sz(A)) { res[i][j][k] = A[k][i][j]; } } else if (x == 2 && y == 0 && z == 1) { irekae_resize_loop(sz(A[0][0]), sz(A), sz(A[0])) { res[i][j][k] = A[j][k][i]; } } else if (x == 2 && y == 1 && z == 0) { irekae_resize_loop(sz(A[0][0]), sz(A[0]), sz(A)) { res[i][j][k] = A[k][j][i]; } } return res; #undef irekae_resize_loop } template <class T> auto irekae(vector<vector<T>> &A, int i = 1, int j = 0) { vvt(res, sz(A[0]), sz(A)); rep(i, sz(A)) { rep(j, sz(A[0])) { res[j][i] = A[i][j]; } } return res; } // tou分割する template <typename T> vector<vector<T>> cut(vector<T> &a, int tou = 2) { int N = sz(a); vector<vector<T>> res(tou); int hab = N / tou; vi lens(tou, hab); rep(i, N % tou) { lens[tou - 1 - i]++; } int l = 0; rep(i, tou) { int len = lens[i]; int r = l + len; res[i].resize(len); std::copy(a.begin() + l, a.begin() + r, res[i].begin()); l = r; } return res; } //長さn毎に分割する template <typename T> vector<vector<T>> cutn(vector<T> &a, int len) { int N = sz(a); vector<vector<T>> res(ceil(N, len)); vi lens(N / len, len); if (N % len) lens.push_back(N % len); int l = 0; rep(i, sz(lens)) { int len = lens[i]; int r = l + len; res[i].resize(len); std::copy(a.begin() + l, a.begin() + r, res[i].begin()); l = r; } return res; } vi inds_(vi &a) { int n = sz(a); vb was(n); vi res(n); rep(i, n) { assert(!was[a[i]]); res[a[i]] = i; was[a[i]] = true; } return res; } //@起動時 struct initon { initon() { cin.tie(0); ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(16); srand((unsigned)clock() + (unsigned)time(NULL)); }; } initonv; #define pre prev #define nex next // gra mll pr //上下左右 const string udlr = "udlr"; string UDLR = "UDLR"; // x4と連動 UDLR.find('U') := x4[0] //右、上が正 constexpr ll h4[] = {1, -1, 0, 0}; constexpr ll w4[] = {0, 0, -1, 1}; constexpr ll h8[] = {0, 1, 0, -1, -1, 1, 1, -1}; constexpr ll w8[] = {1, 0, -1, 0, 1, -1, 1, -1}; int mei_inc(int h, int w, int H, int W, int i) { while (++i < 4) { if (inside(h + h4[i], w + w4[i], H, W)) return i; } return i; } #define mei(nh, nw, h, w) \ for (int i = mei_inc(h, w, H, W, -1), nh = i < 4 ? h + h4[i] : 0, \ nw = i < 4 ? w + w4[i] : 0; \ i < 4; i = mei_inc(h, w, H, W, i), nh = h + h4[i], nw = w + w4[i]) //グラフ内で #undef getid //#define getidとしているため、ここを書き直したらgraphも書き直す #define getid_2(h, w) (h * W + w) #define getid_1(p) (p.first * W + p.second) #define getid(...) over2(__VA_ARGS__, getid_2, getid_1)(__VA_ARGS__) #define getp(id) mp(id / W, id % W) //#define set_shuffle() std::random_device seed_gen;std::mt19937 //engine(seed_gen()) #define shuffle(a) std::shuffle((a).begin(), (a).end(), //engine); 1980 開始からtime ms経っていたらtrue bool timeup(int time) { auto end_time = system_clock::now(); auto part = duration_cast<milliseconds>(end_time - start_time); auto lim = milliseconds(time); return part >= lim; } void set_time() { past_time = system_clock::now(); } // MS型(millisecqnds)で返る // set_timeをしてからの時間 auto calc_time_milli() { auto now = system_clock::now(); auto part = duration_cast<milliseconds>(now - past_time); return part; } auto calc_time_micro() { auto now = system_clock::now(); auto part = duration_cast<microseconds>(now - past_time); return part; } auto calc_time_nano() { auto now = system_clock::now(); auto part = duration_cast<nanoseconds>(now - past_time); return part; } bool calc_time(int zikan) { return calc_time_micro() >= microseconds(zikan); } using MS = std::chrono::microseconds; int div(microseconds a, microseconds b) { return a / b; } int div(nanoseconds a, nanoseconds b) { if (b < nanoseconds(1)) { return a / nanoseconds(1); } int v = a / b; return v; } // set_time(); // rep(i,lim)shori // lim*=time_nanbai(); // rep(i,lim)shoriと使う //全体でmilliかかっていいときにlimを何倍してもう一回できるかを返す int time_nanbai(int milli) { auto dec = duration_cast<nanoseconds>(past_time - start_time); auto part = calc_time_nano(); auto can_time = nanoseconds(milli * 1000 * 1000); can_time -= part; can_time -= dec; return div(can_time, part); } /*@formatter:on*/ // vectorで取れる要素数 // bool=> 1e9 * 8.32 // int => 1e8 * 2.6 // ll => 1e8 * 1.3 // 3次元以上取るとメモリがヤバい // static配列を使う vvc(ba); ll N, M, H, W; vi A, B, C; /*@formatter:off*/ template <class T> vector<int> kmp_makeTable(T &s) { int n = s.size(); vector<int> ret(n + 1); ret[0] = -1; int j = -1; for (int i = 0; i < n; i++) { while (j >= 0 && s[i] != s[j]) j = ret[j]; ret[i + 1] = ++j; } return ret; } // str の中に word とマッチする場所のリストを返す // ret のそれぞれの要素 el は, 「str[el] からの文字列が word // と一致する」ことを示す template <class T> vector<int> kmp(T &str, T &word) { vector<int> table = kmp_makeTable(word), ret; int m = 0, i = 0, n = str.size(); while (m + i < n) { if (word[i] == str[m + i]) { if (++i == (int)(word.size())) { ret.push_back(m); m = m + i - table[i]; i = table[i]; } } else { m = m + i - table[i]; if (i > 0) i = table[i]; } } return ret; } template <class T> vector<int> z_algorithm(T &s) { vector<int> prefix(s.size()); for (int i = 1, j = 0; i < s.size(); i++) { if (i + prefix[i - j] < j + prefix[j]) { prefix[i] = prefix[i - j]; } else { int k = max(0, j + prefix[j] - i); while (i + k < s.size() && s[k] == s[i + k]) ++k; prefix[i] = k; j = i; } } prefix[0] = (int)s.size(); return prefix; } /*@formatter:on*/ template <class T> vector<int> z_algorithm(T &A, T word) { int w = sz(word); word += A; vector<int> z = z_algorithm(word); vi res(sz(A)); rep(i, w, sz(z)) { res[i - w] = min(w, z[i]); } return res; } // 11:33 void solve() { in(N); na(A, N); na(B, N); auto ta = A, tb = B; A *= 3; B *= 3; rep(i, N * 3 - 1) { A[i] ^= A[i + 1]; B[i] ^= B[i + 1]; } B.resize(N); auto s = z_algorithm(A, B); rep(k, N) { if (s[k] >= N) out(k, ta[k] ^ tb[0]); } } auto my(ll n, vi &a) { return 0; } auto sister(ll n, vi &a) { ll ret = 0; return ret; } signed main() { solve(); #define arg n, a #ifdef _DEBUG bool bad = 0; for (ll i = 0, ok = 1; i < k5 && ok; ++i) { ll n = rand(1, 8); vi a = ranv(n, 1, 10); auto myres = my(arg); auto res = sister(arg); ok = myres == res; if (!ok) { out(arg); cerr << "AC : " << res << endl; cerr << "MY : " << myres << endl; bad = 1; break; } } if (!bad) { // cout << "完璧 : solveを書き直そう" << endl; // cout << " : そして、solve()を呼び出すのだ" << endl; // cout << " : cin>>n; na(a,n);も忘れるな" << endl; } #endif return 0; };
[ "expression.operation.binary.remove" ]
744,034
744,035
u986437843
cpp
p02816
//#undef _DEBUG //#pragma GCC optimize("Ofast") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace std::chrono; #define int long long // todo 消したら動かない intの代わりにsignedを使う #define ll long long auto start_time = system_clock::now(); auto past_time = system_clock::now(); #define debugName(VariableName) #VariableName //最大引数がN #define over2(o1, o2, name, ...) name #define over3(o1, o2, o3, name, ...) name #define over4(o1, o2, o3, o4, name, ...) name #define over5(o1, o2, o3, o4, o5, name, ...) name #define over6(o1, o2, o3, o4, o5, o6, name, ...) name #define over7(o1, o2, o3, o4, o5, o6, o7, name, ...) name #define over8(o1, o2, o3, o4, o5, o6, o7, o8, name, ...) name #define over9(o1, o2, o3, o4, o5, o6, o7, o8, o9, name, ...) name #define over10(o1, o2, o3, o4, o5, o6, o7, o8, o9, o10, name, ...) name #define rep(i, a, b) for (int i = a; i < b; i++) #define rrep(i, a, b) for (int i = a; i >= b; i--) #define fore(i, a) for (auto &i : a) #define all(x) (x).begin(), (x).end() #define rand xor128_ /*@formatter:on*/ unsigned long xor128_(void) { static unsigned long x = 123456789, y = 362436069, z = 521288629, w = 88675123; unsigned long t; t = (x ^ (x << 11)); x = y; y = z; z = w; return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8))); } /*@formatter:on*/ ll rand(ll min, ll max) { assert(min <= max); if (min >= 0 && max >= 0) { return rand() % (max + 1 - min) + min; } else if (max < 0) { return -rand(-max, -min); } else { if (rand() % 2) { return rand(0, max); } else { return -rand(0, -min); } } } #define sz(a) ((int)a.size()) const int inf = (int)1e9 + 100; /*@formatter:off*/ //@起動時 struct initon { initon() { cin.tie(0); ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(16); srand((unsigned)clock() + (unsigned)time(NULL)); }; } initonv; #define pre prev #define nex next // hashを共有したくない場合、中に書く const uint64_t base = rand((uint64_t)868491463, ((uint64_t)1 << 61) - 1 - 1); //安全で爆速なrollinghash class roll { /*@formatter:on*/ static constexpr uint64_t mask30 = ((uint64_t)1 << 30) - 1; static constexpr uint64_t mask31 = ((uint64_t)1 << 31) - 1; static constexpr uint64_t mask61 = ((uint64_t)1 << 61) - 1; static constexpr uint64_t offset = mask61 * 7; static constexpr uint64_t mod(const uint64_t x) { const uint64_t y = (x >> 61) + (x & mask61); return y >= mask61 ? y - mask61 : y; } static constexpr uint64_t mul(const uint64_t x, const uint64_t y) { const uint64_t xh = x >> 31, xl = x & mask31; const uint64_t yh = y >> 31, yl = y & mask31; const uint64_t z = xl * yh + xh * yl; const uint64_t zh = z >> 30, zl = z & mask30; return xh * yh * 2 + zh + (zl << 31) + xl * yl; } const std::size_t siz; vector<uint64_t> pow, h; public: template <typename T> roll(T &A) : siz(sz(A)), pow(siz + 1, 1), h(siz + 1, 0) { for (std::size_t i = 1; i <= siz; i++) { pow[i] = mod(mul(pow[i - 1], base)), h[i] = mod(mul(h[i - 1], base) + A[i - 1]); } } uint64_t operator()(const std::size_t l, const std::size_t r) { return mod(h[r] + offset - mul(h[l], pow[r - l])); } uint64_t operator()(int r = inf) { return operator()(0, min((int)siz, r)); } // get lcp of S[a:] and T[b:] //接頭語がどれだけ一致するか //範囲の重複を含む int lcp_dup(int a, int b) { int len = min((int)siz - a, (int)siz - b); int low = 0, high = len; while (high - low > 1) { int mid = (low + high) >> 1; if (operator()(a, a + mid) != operator()(b, b + mid)) high = mid; else low = mid; } return low; } //重複を認めない int lcp_not(int a, int b) { return min(lcp_dup(a, b), b - a); } //@formatter:on }; //動かない場合roll2 // roll ro(list); //--------------------------------------------------------------------------------------------------- template <int MOD> struct ModInt { static const int Mod = MOD; unsigned x; ModInt() : x(0) {} ModInt(signed sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; } ModInt(signed long long sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; } int get() const { return (int)x; } ModInt &operator+=(ModInt that) { if ((x += that.x) >= MOD) x -= MOD; return *this; } ModInt &operator-=(ModInt that) { if ((x += MOD - that.x) >= MOD) x -= MOD; return *this; } ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; } ModInt &operator/=(ModInt that) { return *this *= that.inverse(); } ModInt operator+(ModInt that) const { return ModInt(*this) += that; } ModInt operator-(ModInt that) const { return ModInt(*this) -= that; } ModInt operator*(ModInt that) const { return ModInt(*this) *= that; } ModInt operator/(ModInt that) const { return ModInt(*this) /= that; } ModInt inverse() const { long long a = x, b = MOD, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); } return ModInt(u); } bool operator==(ModInt that) const { return x == that.x; } bool operator!=(ModInt that) const { return x != that.x; } ModInt operator-() const { ModInt t; t.x = x == 0 ? 0 : Mod - x; return t; } }; template <int MOD> ModInt<MOD> operator^(ModInt<MOD> a, unsigned long long k) { ModInt<MOD> r = 1; while (k) { if (k & 1) r *= a; a *= a; k >>= 1; } return r; } int getrandmax() { static uint32_t y = time(NULL); y ^= (y << 13); y ^= (y >> 17); y ^= (y << 5); return abs((int)y); } int getrand(int l, int r) { // [l, r] return getrandmax() % (r - l + 1) + l; } typedef ModInt<1000000007> mint1; typedef ModInt<1000000009> mint2; int primes[10] = {10007, 10009, 10037, 10039, 10061, 10067, 10069, 10079, 10091, 10093}; bool isShuffle = false; struct RollingHash { mint1 p1; mint2 p2; int n; vector<mint1> m1; vector<mint2> m2; vector<mint1> v1; vector<mint2> v2; RollingHash() { if (!isShuffle) { rep(i, 0, 101) { int a = getrand(0, 9); int b = getrand(0, 9); swap(primes[a], primes[b]); } isShuffle = true; } p1 = primes[0], p2 = primes[1]; } void init(string s, char o = 'a') { vector<int> v; fore(c, s) v.push_back(c - o); init(v); } void init(vector<int> s) { n = s.size(); m1.resize(n); m2.resize(n); v1.resize(n); v2.resize(n); m1[0] = 1; m2[0] = 1; v1[0] = s[0]; v2[0] = s[0]; rep(i, 1, n) { m1[i] = m1[i - 1] * p1; m2[i] = m2[i - 1] * p2; v1[i] = v1[i - 1] + m1[i] * s[i]; v2[i] = v2[i - 1] + m2[i] * s[i]; } } inline pair<mint1, mint2> hash(int l, int r) { // s[l..r] assert(l <= r); assert(r < n); mint1 a = v1[r]; if (l) a -= v1[l - 1]; a *= m1[n - 1 - r]; mint2 b = v2[r]; if (l) b -= v2[l - 1]; b *= m2[n - 1 - r]; return {a, b}; } inline ll llhash(int l, int r) { // s[l..r] auto h = hash(l, r); return 1LL * h.first.get() * h.second.Mod + h.second.get(); } }; using vi = vector<int>; int N; void solve() { cin >> N; vi A(N), B(N), A2(N), B2(N); rep(i, 0, N) cin >> A[i]; rep(i, 0, N) cin >> B[i]; rep(i, 0, N) A2[i] = A[i] ^ A[(i + 1) % N]; rep(i, 0, N) B2[i] = B[i] ^ B[(i + 1) % N]; vector<int> v; rep(i, 0, N) v.push_back(A2[i]); rep(i, 0, N) v.push_back(A2[i]); rep(i, 0, N) v.push_back(B2[i]); RollingHash rh; rh.init(v); rep(k, 0, N) if (rh.hash(k, k + N) == rh.hash(N * 2, N * 2 + N)) { int x = A[k] ^ B[0]; cout << k << " " << x << endl; } } auto my(ll n, vi &a) { return 0; } auto sister(ll n, vi &a) { ll ret = 0; return ret; } signed main() { solve(); return 0; };
//#undef _DEBUG //#pragma GCC optimize("Ofast") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace std::chrono; #define int long long // todo 消したら動かない intの代わりにsignedを使う #define ll long long auto start_time = system_clock::now(); auto past_time = system_clock::now(); #define debugName(VariableName) #VariableName //最大引数がN #define over2(o1, o2, name, ...) name #define over3(o1, o2, o3, name, ...) name #define over4(o1, o2, o3, o4, name, ...) name #define over5(o1, o2, o3, o4, o5, name, ...) name #define over6(o1, o2, o3, o4, o5, o6, name, ...) name #define over7(o1, o2, o3, o4, o5, o6, o7, name, ...) name #define over8(o1, o2, o3, o4, o5, o6, o7, o8, name, ...) name #define over9(o1, o2, o3, o4, o5, o6, o7, o8, o9, name, ...) name #define over10(o1, o2, o3, o4, o5, o6, o7, o8, o9, o10, name, ...) name #define rep(i, a, b) for (int i = a; i < b; i++) #define rrep(i, a, b) for (int i = a; i >= b; i--) #define fore(i, a) for (auto &i : a) #define all(x) (x).begin(), (x).end() #define rand xor128_ /*@formatter:on*/ unsigned long xor128_(void) { static unsigned long x = 123456789, y = 362436069, z = 521288629, w = 88675123; unsigned long t; t = (x ^ (x << 11)); x = y; y = z; z = w; return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8))); } /*@formatter:on*/ ll rand(ll min, ll max) { assert(min <= max); if (min >= 0 && max >= 0) { return rand() % (max + 1 - min) + min; } else if (max < 0) { return -rand(-max, -min); } else { if (rand() % 2) { return rand(0, max); } else { return -rand(0, -min); } } } #define sz(a) ((int)a.size()) const int inf = (int)1e9 + 100; /*@formatter:off*/ //@起動時 struct initon { initon() { cin.tie(0); ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(16); srand((unsigned)clock() + (unsigned)time(NULL)); }; } initonv; #define pre prev #define nex next // hashを共有したくない場合、中に書く const uint64_t base = rand((uint64_t)868491463, ((uint64_t)1 << 61) - 1 - 1); //安全で爆速なrollinghash class roll { /*@formatter:on*/ static constexpr uint64_t mask30 = ((uint64_t)1 << 30) - 1; static constexpr uint64_t mask31 = ((uint64_t)1 << 31) - 1; static constexpr uint64_t mask61 = ((uint64_t)1 << 61) - 1; static constexpr uint64_t offset = mask61 * 7; static constexpr uint64_t mod(const uint64_t x) { const uint64_t y = (x >> 61) + (x & mask61); return y >= mask61 ? y - mask61 : y; } static constexpr uint64_t mul(const uint64_t x, const uint64_t y) { const uint64_t xh = x >> 31, xl = x & mask31; const uint64_t yh = y >> 31, yl = y & mask31; const uint64_t z = xl * yh + xh * yl; const uint64_t zh = z >> 30, zl = z & mask30; return xh * yh * 2 + zh + (zl << 31) + xl * yl; } const std::size_t siz; vector<uint64_t> pow, h; public: template <typename T> roll(T &A) : siz(sz(A)), pow(siz + 1, 1), h(siz + 1, 0) { for (std::size_t i = 1; i <= siz; i++) { pow[i] = mod(mul(pow[i - 1], base)), h[i] = mod(mul(h[i - 1], base) + A[i - 1]); } } uint64_t operator()(const std::size_t l, const std::size_t r) { return mod(h[r] + offset - mul(h[l], pow[r - l])); } uint64_t operator()(int r = inf) { return operator()(0, min((int)siz, r)); } // get lcp of S[a:] and T[b:] //接頭語がどれだけ一致するか //範囲の重複を含む int lcp_dup(int a, int b) { int len = min((int)siz - a, (int)siz - b); int low = 0, high = len; while (high - low > 1) { int mid = (low + high) >> 1; if (operator()(a, a + mid) != operator()(b, b + mid)) high = mid; else low = mid; } return low; } //重複を認めない int lcp_not(int a, int b) { return min(lcp_dup(a, b), b - a); } //@formatter:on }; //動かない場合roll2 // roll ro(list); //--------------------------------------------------------------------------------------------------- template <int MOD> struct ModInt { static const int Mod = MOD; unsigned x; ModInt() : x(0) {} ModInt(signed sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; } ModInt(signed long long sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; } int get() const { return (int)x; } ModInt &operator+=(ModInt that) { if ((x += that.x) >= MOD) x -= MOD; return *this; } ModInt &operator-=(ModInt that) { if ((x += MOD - that.x) >= MOD) x -= MOD; return *this; } ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; } ModInt &operator/=(ModInt that) { return *this *= that.inverse(); } ModInt operator+(ModInt that) const { return ModInt(*this) += that; } ModInt operator-(ModInt that) const { return ModInt(*this) -= that; } ModInt operator*(ModInt that) const { return ModInt(*this) *= that; } ModInt operator/(ModInt that) const { return ModInt(*this) /= that; } ModInt inverse() const { long long a = x, b = MOD, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); } return ModInt(u); } bool operator==(ModInt that) const { return x == that.x; } bool operator!=(ModInt that) const { return x != that.x; } ModInt operator-() const { ModInt t; t.x = x == 0 ? 0 : Mod - x; return t; } }; template <int MOD> ModInt<MOD> operator^(ModInt<MOD> a, unsigned long long k) { ModInt<MOD> r = 1; while (k) { if (k & 1) r *= a; a *= a; k >>= 1; } return r; } int getrandmax() { static uint32_t y = time(NULL); y ^= (y << 13); y ^= (y >> 17); y ^= (y << 5); return abs((int)y); } int getrand(int l, int r) { // [l, r] return getrandmax() % (r - l + 1) + l; } typedef ModInt<1000000007> mint1; typedef ModInt<1000000009> mint2; int primes[10] = {10007, 10009, 10037, 10039, 10061, 10067, 10069, 10079, 10091, 10093}; bool isShuffle = false; struct RollingHash { mint1 p1; mint2 p2; int n; vector<mint1> m1; vector<mint2> m2; vector<mint1> v1; vector<mint2> v2; RollingHash() { if (!isShuffle) { rep(i, 0, 101) { int a = getrand(0, 9); int b = getrand(0, 9); swap(primes[a], primes[b]); } isShuffle = true; } p1 = primes[0], p2 = primes[1]; } void init(string s, char o = 'a') { vector<int> v; fore(c, s) v.push_back(c - o); init(v); } void init(vector<int> s) { n = s.size(); m1.resize(n); m2.resize(n); v1.resize(n); v2.resize(n); m1[0] = 1; m2[0] = 1; v1[0] = s[0]; v2[0] = s[0]; rep(i, 1, n) { m1[i] = m1[i - 1] * p1; m2[i] = m2[i - 1] * p2; v1[i] = v1[i - 1] + m1[i] * s[i]; v2[i] = v2[i - 1] + m2[i] * s[i]; } } inline pair<mint1, mint2> hash(int l, int r) { // s[l..r] assert(l <= r); assert(r < n); mint1 a = v1[r]; if (l) a -= v1[l - 1]; a *= m1[n - 1 - r]; mint2 b = v2[r]; if (l) b -= v2[l - 1]; b *= m2[n - 1 - r]; return {a, b}; } inline ll llhash(int l, int r) { // s[l..r] auto h = hash(l, r); return 1LL * h.first.get() * h.second.Mod + h.second.get(); } }; using vi = vector<int>; int N; void solve() { cin >> N; vi A(N), B(N), A2(N), B2(N); rep(i, 0, N) cin >> A[i]; rep(i, 0, N) cin >> B[i]; rep(i, 0, N) A2[i] = A[i] ^ A[(i + 1) % N]; rep(i, 0, N) B2[i] = B[i] ^ B[(i + 1) % N]; vector<int> v; rep(i, 0, N) v.push_back(A2[i]); rep(i, 0, N) v.push_back(A2[i]); rep(i, 0, N) v.push_back(B2[i]); RollingHash rh; rh.init(v); rep(k, 0, N) if (rh.hash(k, k + N - 1) == rh.hash(N * 2, N * 2 + N - 1)) { int x = A[k] ^ B[0]; cout << k << " " << x << endl; } } auto my(ll n, vi &a) { return 0; } auto sister(ll n, vi &a) { ll ret = 0; return ret; } signed main() { solve(); return 0; };
[ "control_flow.branch.if.condition.change" ]
744,036
744,037
u986437843
cpp
p02816
//#undef _DEBUG //#pragma GCC optimize("Ofast") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace std::chrono; #define int long long // todo 消したら動かない intの代わりにsignedを使う #define ll long long auto start_time = system_clock::now(); auto past_time = system_clock::now(); #define debugName(VariableName) #VariableName //最大引数がN #define over2(o1, o2, name, ...) name #define over3(o1, o2, o3, name, ...) name #define over4(o1, o2, o3, o4, name, ...) name #define over5(o1, o2, o3, o4, o5, name, ...) name #define over6(o1, o2, o3, o4, o5, o6, name, ...) name #define over7(o1, o2, o3, o4, o5, o6, o7, name, ...) name #define over8(o1, o2, o3, o4, o5, o6, o7, o8, name, ...) name #define over9(o1, o2, o3, o4, o5, o6, o7, o8, o9, name, ...) name #define over10(o1, o2, o3, o4, o5, o6, o7, o8, o9, o10, name, ...) name /*@formatter:off*/ //[-n, n)にアクセスできる //また、外部関数resizeに渡せる // sizeは[-n, n)でnを返す template <class T> class mvec { vector<T> v; int n; public: mvec() : n(0), v(0) {} mvec(int n) : n(n), v(n * 2) {} mvec(int n, T val) : n(n), v(n * 2, val) {} auto &operator[](int i) { return v[i + n]; } auto size() { return n; } void resize(int sn) { assert(n == 0); n = sn; v.resize(sn * 2); } auto begin() { return v.begin(); } auto rbegin() { return v.rbegin(); } auto end() { return v.end(); } auto rend() { return v.rend(); } }; //[]でboolは参照を返さないため特殊化が必要 template <> struct mvec<bool> { vector<bool> v; int n; mvec() : n(0), v(0) {} mvec(int n) : n(n), v(n * 2) {} mvec(int n, bool val) : n(n), v(n * 2, val) {} auto operator[](int i) { return v[i + n]; } auto size() { return v.size(); } void resize(int sn) { assert(n == 0); n = sn; v.resize(sn * 2); } auto begin() { return v.begin(); } auto rbegin() { return v.rbegin(); } auto end() { return v.end(); } auto rend() { return v.rend(); } }; template <class T> ostream &operator<<(ostream &os, mvec<T> &a) { int spa = 3; for (auto &&v : a) { spa = max(spa, (int)(to_string(v).size()) + 1); } int n = (int)a.size(); os << endl; for (int i = -n; i < n; i++) { int need = spa - ((int)to_string(i).size()); if (i == -n) { need -= min(need, spa - ((int)to_string(a[i]).size())); } while (need--) { os << " "; } os << i; } os << endl; int i = -n; for (auto &&v : a) { int need = spa - ((int)to_string(v).size()); if (i == -n) { need -= min(need, spa - ((int)to_string(i).size())); } while (need--) { os << " "; } os << v; i++; } return os; } #define mv mvec #define MV mvec using mvi = mvec<ll>; using mvb = mvec<bool>; using mvs = mvec<string>; using mvd = mvec<double>; using mvc = mvec<char>; #define mvvt0(t) mvec<mvec<t>> #define mvvt1(t, a) mvec<mvec<t>> a #define mvvt2(t, a, b) mvec<mvec<t>> a(b) #define mvvt3(t, a, b, c) mvec<mvec<t>> a(b, mvec<t>(c)) #define mvvt4(t, a, b, c, d) mvec<mvec<t>> a(b, mvec<t>(c, d)) #define mvvi(...) \ over4(__VA_ARGS__, mvvt4, mvvt3, mvvt2, mvvt1, mvvt0)(ll, __VA_ARGS__) template <typename T> mvec<T> make_mv(size_t a) { return mvec<T>(a); } template <typename T, typename... Ts> auto make_mv(size_t a, Ts... ts) { return mvec<decltype(make_mv<T>(ts...))>(a, make_mv<T>(ts...)); } #define mvni(name, ...) auto name = make_mv<ll>(__VA_ARGS__) #ifdef _DEBUG // https://marycore.jp/prog/cpp/class-extension-methods/ 違うかも template <class T, class A = std::allocator<T>> struct debtor : std::vector<T, A> { using std::vector<T, A>::vector; template <class U> int deb_v(U a, int v) { return v; } template <class U> int deb_v(debtor<U> &a, int v = 0) { cerr << a.size() << " "; return deb_v(a.at(0), v + 1); } template <class U> void deb_o(U a) { cerr << a << " "; } template <class U> void deb_o(debtor<U> &a) { for (int i = 0; i < min((int)a.size(), 15ll); i++) { deb_o(a[i]); } if ((int)a.size() > 15) { cerr << "..."; } cerr << endl; } typename std::vector<T>::reference operator[](typename std::vector<T>::size_type n) { if (n < 0 || n >= (int)this->size()) { int siz = (int)this->size(); cerr << "vector size = "; int dim = deb_v((*this)); cerr << endl; cerr << "out index at " << n << endl; cerr << endl; if (dim <= 2) { deb_o((*this)); } exit(0); } return this->at(n); } }; #define vector debtor #endif #ifdef _DEBUG //区間削除は出来ない template <class T> struct my_pbds_tree { set<T> s; auto begin() { return s.begin(); } auto end() { return s.end(); } auto rbegin() { return s.rbegin(); } auto rend() { return s.rend(); } auto empty() { return s.empty(); } auto size() { return s.size(); } void clear() { s.clear(); } template <class U> void insert(U v) { s.insert(v); } template <class U> void operator+=(U v) { insert(v); } template <class F> auto erase(F v) { return s.erase(v); } template <class U> auto find(U v) { return s.find(v); } template <class U> auto lower_bound(U v) { return s.lower_bound(v); } template <class U> auto upper_bound(U v) { return s.upper_bound(v); } auto find_by_order(ll k) { auto it = s.begin(); for (ll i = 0; i < k; i++) it++; return it; } auto order_of_key(ll v) { auto it = s.begin(); ll i = 0; for (; it != s.end() && *it < v; i++) it++; return i; } }; #define pbds(T) my_pbds_tree<T> // gp_hash_tableでcountを使えないようにするため template <class T, class U> struct my_unordered_map { unordered_map<T, U> m; my_unordered_map(){}; auto begin() { return m.begin(); } auto end() { return m.end(); } auto cbegin() { return m.cbegin(); } auto cend() { return m.cend(); } template <class V> auto erase(V v) { return m.erase(v); } void clear() { m.clear(); } /*countは gp_hash_tableに存在しない*/ /*!= m.end()*/ template <class V> auto find(V v) { return m.find(v); } template <class V> auto &operator[](V n) { return m[n]; } }; #define unordered_map my_unordered_map #define umapi unordered_map<ll, ll> #define umapp unordered_map<P, ll> #define umapu unordered_map<uint64_t, ll> #define umapip unordered_map<ll, P> #else // umapはunorderd_mapになる // umapiはgp_hash_table // find_by_order(k) k番目のイテレーター // order_of_key(k) k以上が前から何番目か #define pbds(U) \ __gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag, \ __gnu_pbds::tree_order_statistics_node_update> #define umapi __gnu_pbds::gp_hash_table<ll, ll, xorshift> #define umapp __gnu_pbds::gp_hash_table<P, ll, xorshift> #define umapu __gnu_pbds::gp_hash_table<uint64_t, ll, xorshift> #define umapip __gnu_pbds::gp_hash_table<ll, P, xorshift> #endif /*@formatter:on*/ struct xorshift { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } size_t operator()(std::pair<ll, ll> x) const { ll v = ((x.first) << 32) | x.second; static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(v + FIXED_RANDOM); } }; /*@formatter:off*/ template <class U, class L> void operator+=( __gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update> &s, L v) { s.insert(v); } //衝突対策 #define ws ws_ template <class A, class B, class C> struct T2 { A f; B s; C t; T2() { f = 0, s = 0, t = 0; } T2(A f, B s, C t) : f(f), s(s), t(t) {} bool operator<(const T2 &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 ?n s > r.s : t > r.t; 大きい順 */ } bool operator>(const T2 &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 T2 &r) const { return f == r.f && s == r.s && t == r.t; } bool operator!=(const T2 &r) const { return f != r.f || s != r.s || t != r.t; } }; template <class A, class B, class C, class D> struct F2 { A a; B b; C c; D d; F2() { a = 0, b = 0, c = 0, d = 0; } F2(A a, B b, C c, D d) : a(a), b(b), c(c), d(d) {} bool operator<(const F2 &r) const { return a != r.a ? a < r.a : b != r.b ? b < r.b : c != r.c ? c < r.c : d < r.d; /* return a != r.a ? a > r.a : b != r.b ? b > r.b : c != r.c ? c > r.c : d > r.d;*/ } bool operator>(const F2 &r) const { return a != r.a ? a > r.a : b != r.b ? b > r.b : c != r.c ? c > r.c : d > r.d; /* return a != r.a ? a < r.a : b != r.b ? b < r.b : c != r.c ? c < r.c : d < r.d;*/ } bool operator==(const F2 &r) const { return a == r.a && b == r.b && c == r.c && d == r.d; } bool operator!=(const F2 &r) const { return a != r.a || b != r.b || c != r.c || d != r.d; } ll operator[](ll i) { assert(i < 4); return i == 0 ? a : i == 1 ? b : i == 2 ? c : d; } }; typedef T2<ll, ll, ll> T; typedef F2<ll, ll, ll, ll> F; T mt(ll a, ll b, ll c) { return T(a, b, c); } F mf(ll a, ll b, ll c, ll d) { return F(a, b, c, d); } //@マクロ省略系 型,構造 #define double long double #define pow powl using dou = double; const double eps = 1e-9; //基本コメントアウト /* struct epsdou { double v; epsdou(double v = 0) : v(v) {} template<class T> epsdou &operator+=(T b) { v += (double) b; return (*this); } template<class T> epsdou &operator-=(T b) { v -= (double) b; return (*this); } template<class T> epsdou &operator*=(T b) { v *= (double) b; return (*this); } template<class T> epsdou &operator/=(T b) { v /= (double) b; return (*this); } epsdou operator+(epsdou b) { return v + (double) b; } epsdou operator-(epsdou b) { return v - (double) b; } epsdou operator*(epsdou b) { return v * (double) b; } epsdou operator/(epsdou b) { return v / (double) b; } epsdou operator-() const { return epsdou(-v); } template<class T> bool operator<(T b) { return v < (double) b; } template<class T> bool operator>(T b) { return v > (double) b; } template<class T> bool operator==(T b) { return fabs(v - (double) b) <= eps; } template<class T> bool operator<=(T b) { return v < (double) b || fabs(v - b) <= eps; } template<class T> bool operator>=(T b) { return v > (double) b || fabs(v - b) <= eps; } operator double() { return v; }};istream &operator>>(istream &iss, epsdou &a) { iss >> a.v; return iss;}ostream &operator<<(ostream &os, epsdou &a) { os << a.v; return os;} #define eps_conr_t(o) template<class T> epsdou operator o(T b, epsdou a){return a.v o (dou)b;} #define eps_conl_t(o) template<class T> epsdou operator o(epsdou a, T b){return a.v o (dou)b;} eps_conl_t(+)eps_conl_t(-)eps_conl_t(*)eps_conl_t(/)eps_conr_t(+)eps_conr_t(-)eps_conr_t(*)eps_conr_t(/) #undef double #define double epsdou */ #define ull unsigned long long using itn = int; using str = string; using bo = bool; #define au auto using P = pair<ll, ll>; using mvp = mvec<P>; using mvt = mvec<T>; #define MIN(a) numeric_limits<a>::min() #define MAX(a) numeric_limits<a>::max() #define fi first #define se second #define beg begin #define rbeg rbegin #define con continue #define bre break #define brk break #define is == #define el else #define elf else if #define upd update #define sstream stringstream #define maxq 1 #define minq -1 #define ZERO(a) memset(a, 0, sizeof(a)) #define MINUS(a) memset(a, 0xff, sizeof(a)) #define MALLOC(type, len) (type *)malloc((len) * sizeof(type)) #define lam(right) [&](auto &p) { return p right; } //マクロ省略系 コンテナ 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 V vector #define vvt0(t) vector<vector<t>> #define vvt1(t, a) vector<vector<t>> a #define vvt2(t, a, b) vector<vector<t>> a(b) #define vvt3(t, a, b, c) vector<vector<t>> a(b, vector<t>(c)) #define vvt4(t, a, b, c, d) vector<vector<t>> a(b, vector<t>(c, d)) #define vvi(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(ll, __VA_ARGS__) #define vvb(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(bool, __VA_ARGS__) #define vvs(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(string, __VA_ARGS__) #define vvd(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(double, __VA_ARGS__) #define vvc(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(char, __VA_ARGS__) #define vvp(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(P, __VA_ARGS__) #define vvt(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(T, __VA_ARGS__) #define vv(type, ...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(type, __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 vn(type, name, ...) auto name = make_v<type>(__VA_ARGS__) #define PQ priority_queue<ll, vector<ll>, greater<ll>> #define tos to_string using mapi = map<ll, ll>; using mapp = map<P, ll>; using mapd = map<dou, ll>; using mapc = map<char, ll>; using maps = map<str, ll>; using seti = set<ll>; using setd = set<dou>; using setc = set<char>; using sets = set<str>; using qui = queue<ll>; #define uset unordered_set #define useti unordered_set<ll, xorshift> #define mset multiset #define mseti multiset<ll> #define umap unordered_map #define mmap multimap //任意のマクロサポート用 使う度に初期化する int index_, v1_, v2_, v3_; template <class T> struct pq { priority_queue<T, vector<T>, greater<T>> q; /*小さい順*/ T su = 0; void clear() { q = priority_queue<T, vector<T>, greater<T>>(); su = 0; } void operator+=(T v) { su += v; q.push(v); } T sum() { return su; } T top() { return q.top(); } void pop() { su -= q.top(); q.pop(); } T poll() { T ret = q.top(); su -= ret; q.pop(); return ret; } ll size() { return q.size(); } }; template <class T> struct pqg { priority_queue<T> q; /*大きい順*/ T su = 0; void clear() { q = priority_queue<T>(); su = 0; } void operator+=(T v) { su += v; q.push(v); } T sum() { return su; } T top() { return q.top(); } void pop() { su -= q.top(); q.pop(); } T poll() { T ret = q.top(); su -= ret; q.pop(); return ret; } ll size() { return q.size(); } }; #define pqi pq<ll> #define pqgi pqg<ll> //マクロ 繰り返し //↓@オーバーロード隔離 #define rep1(n) for (ll rep1i = 0, rep1lim = n; rep1i < rep1lim; ++rep1i) #define rep2(i, n) for (ll i = 0, rep2lim = n; i < rep2lim; ++i) #define rep3(i, m, n) for (ll i = m, rep3lim = n; i < rep3lim; ++i) #define rep4(i, m, n, ad) for (ll i = m, rep4lim = n; i < rep4lim; i += ad) //逆順 閉区間 #define rer2(i, n) for (ll i = n; i >= 0; i--) #define rer3(i, m, n) for (ll i = m, rer3lim = n; i >= rer3lim; i--) #define rer4(i, m, n, dec) for (ll i = m, rer4lim = n; i >= rer4lim; i -= dec) //ループを一つにまとめないとフォーマットで汚くなるため #define nex_ind1(i) i++ #define nex_ind2(i, j, J) \ i = (j + 1 == J) ? i + 1 : i, j = (j + 1 == J ? 0 : j + 1) #define nex_ind3(i, j, k, J, K) \ i = (j + 1 == J && k + 1 == K) ? i + 1 : i, \ j = (k + 1 == K) ? (j + 1 == J ? 0 : j + 1) : j, \ k = (k + 1 == K ? 0 : k + 1) #define nex_ind4(i, j, k, l, J, K, L) \ i = (j + 1 == J && k + 1 == K && l + 1 == L) ? i + 1 : i, \ j = (k + 1 == K && l + 1 == L) ? (j + 1 == J ? 0 : j + 1) : j, \ k = (l + 1 == L ? (k + 1 == K ? 0 : k + 1) : k), l = l + 1 == L ? 0 : l + 1 #define nex_ind5(i, j, k, l, m, J, K, L, M) \ i = (j + 1 == J && k + 1 == K && l + 1 == L && m + 1 == M) ? i + 1 : i, \ j = (k + 1 == K && l + 1 == L && m + 1 == M) ? (j + 1 == J ? 0 : j + 1) : j, \ k = (l + 1 == L && m + 1 == M ? (k + 1 == K ? 0 : k + 1) : k), \ l = m + 1 == M ? l + 1 == L ? 0 : l + 1 : l, m = m + 1 == M ? 0 : m + 1 #define repss2(i, I) for (int i = 0; i < I; i++) #define repss4(i, j, I, J) \ for (int i = (J ? 0 : I), j = 0; i < I; nex_ind2(i, j, J)) #define repss6(i, j, k, I, J, K) \ for (int i = (J && K ? 0 : I), j = 0, k = 0; i < I; nex_ind3(i, j, k, J, K)) #define repss8(i, j, k, l, I, J, K, L) \ for (int i = (J && K && L ? 0 : I), j = 0, k = 0, l = 0; i < I; \ nex_ind4(i, j, k, l, J, K, L)) #define repss10(i, j, k, l, m, I, J, K, L, M) \ for (int i = (J && K && L && M ? 0 : I), j = 0, k = 0, l = 0, m = 0; i < I; \ nex_ind5(i, j, k, l, m, J, K, L, M)) // i,j,k...をnまで見る #define reps2(i, n) repss2(i, n) #define reps3(i, j, n) repss4(i, j, n, n) #define reps4(i, j, k, n) repss6(i, j, k, n, n, n) #define reps5(i, j, k, l, n) repss8(i, j, k, l, n, n, n, n) template <class T> void nex_repv2(int &i, int &j, int &I, int &J, vector<vector<T>> &s) { while (1) { j++; if (j >= J) { j = 0; i++; if (i < I) { J = (int)s[i].size(); } } if (i >= I || J) return; } } template <class T> void nex_repv3(int &i, int &j, int &k, int &I, int &J, int &K, vector<vector<vector<T>>> &s) { while (1) { k++; if (k >= K) { k = 0; j++; if (j >= J) { j = 0; i++; if (i >= I) return; } } J = (int)s[i].size(); K = (int)s[i][j].size(); if (J && K) return; } } #define repv_2(i, a) repss2(i, sz(a)) //正方形である必要はない //直前を持つのとどっちが早いか #define repv_3(i, j, a) \ for (int I = (int)a.size(), J = (int)a[0].size(), i = 0, j = 0; i < I; \ nex_repv2(i, j, I, J, a)) //箱状になっている事が要求される つまり[i] 次元目の要素数は一定 #define repv_4(i, j, k, a) \ for (int I = (int)a.size(), J = (int)a[0].size(), K = (int)a[0][0].size(), \ i = 0, j = 0, k = 0; \ i < I; nex_repv3(i, j, k, I, J, K, a)) #define repv_5(i, j, k, l, a) \ repss8(i, j, k, l, sz(a), sz(a[0]), sz(a[0][0]), sz(a[0][0][0])) #define repv_6(i, j, k, l, m, a) \ repss10(i, j, k, l, m, sz(a), sz(a[0]), sz(a[0][0]), sz(a[0][0][0]), \ sz(a[0][0][0][0])) template <typename T> struct has_rbegin_rend { private: template <typename U> static auto check(U &&obj) -> decltype(std::rbegin(obj), std::rend(obj), std::true_type{}); static std::false_type check(...); public: static constexpr bool value = decltype(check(std::declval<T>()))::value; }; template <typename T> constexpr bool has_rbegin_rend_v = has_rbegin_rend<T>::value; template <typename Iterator> class Range { public: Range(Iterator &&begin, Iterator &&end) noexcept : m_begin(std::forward<Iterator>(begin)), m_end(std::forward<Iterator>(end)) {} Iterator begin() const noexcept { return m_begin; } Iterator end() const noexcept { return m_end; } private: const Iterator m_begin; const Iterator m_end; }; template <typename Iterator> static inline Range<Iterator> makeRange(Iterator &&begin, Iterator &&end) noexcept { return Range<Iterator>{std::forward<Iterator>(begin), std::forward<Iterator>(end)}; } template <typename T> static inline decltype(auto) makeReversedRange(const std::initializer_list<T> &iniList) noexcept { return makeRange(std::rbegin(iniList), std::rend(iniList)); } template <typename T, typename std::enable_if_t<has_rbegin_rend_v<T>, std::nullptr_t> = nullptr> static inline decltype(auto) makeReversedRange(T &&c) noexcept { return makeRange(std::rbegin(c), std::rend(c)); } /* rbegin(), rend()を持たないものはこっちに分岐させて,エラーメッセージを少なくする*/ template <typename T, typename std::enable_if<!has_rbegin_rend<T>::value, std::nullptr_t>::type = nullptr> static inline void makeReversedRange(T &&) noexcept { static_assert(has_rbegin_rend<T>::value, "Specified argument doesn't have reverse iterator."); } #define form1(st) \ for (auto &&form_it = st.begin(); form_it != st.end(); ++form_it) #define form3(k, v, st) \ for (auto &&form_it = st.begin(); form_it != st.end(); ++form_it) #define form4(k, v, st, r) \ for (auto &&form_it = st.begin(); form_it != st.end() && (*form_it).fi < r; \ ++form_it) #define form5(k, v, st, l, r) \ for (auto &&form_it = st.lower_bound(l); \ form_it != st.end() && (*form_it).fi < r; ++form_it) #define fors1(st) \ for (auto &&fors_it = st.begin(); fors_it != st.end(); ++fors_it) #define fors2(v, st) \ for (auto &&fors_it = st.begin(); fors_it != st.end(); ++fors_it) #define fors3(v, st, r) \ for (auto &&fors_it = st.begin(); fors_it != st.end() && (*fors_it) < r; \ ++fors_it) #define fors4(v, st, l, r) \ for (auto &&fors_it = st.lower_bound(l); \ fors_it != st.end() && (*fors_it) < r; ++fors_it) #define forslr3(st, a, b) \ for (auto &&forslr_it = st.begin(); forslr_it != st.end(); ++forslr_it) #define forslr4(v, st, a, b) \ for (auto &&forslr_it = st.begin(); forslr_it != st.end(); ++forslr_it) #define forslr5(v, st, r, a, b) \ for (auto &&forslr_it = st.begin(); \ forslr_it != st.end() && (*forslr_it) < r; ++forslr_it) #define forslr6(v, st, l, r, a, b) \ for (auto &&forslr_it = st.lower_bound(l); \ forslr_it != st.end() && (*forslr_it) < r; ++forslr_it) template <class U> vector<U> to1d(vector<U> &a) { return a; } template <class U> vector<U> to1d(V<V<U>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) res.push_back(a2); return res; } template <class U> vector<U> to1d(V<V<V<U>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) res.push_back(a3); return res; } template <class U> vector<U> to1d(V<V<V<V<U>>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) for (auto &&a4 : a3) res.push_back(a4); return res; } template <class U> vector<U> to1d(V<V<V<V<V<U>>>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) for (auto &&a4 : a3) for (auto &&a5 : a4) res.push_back(a5); return res; } template <class U> vector<U> to1d(V<V<V<V<V<V<U>>>>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) for (auto &&a4 : a3) for (auto &&a5 : a4) for (auto &&a6 : a5) res.push_back(a6); return res; } #define fora_init_2(a, A) ; #define fora_init_4(a, b, A, B) \ auto &&a = A[fora_i]; \ auto &&b = B[fora_i]; #define fora_init_6(a, b, c, A, B, C) \ auto &&a = A[fora_i]; \ auto &&b = B[fora_i]; \ auto &&c = C[fora_i]; #define fora_init_8(a, b, c, d, A, B, C, D) \ auto &&a = A[fora_i]; \ auto &&b = B[fora_i]; \ auto &&c = C[fora_i]; \ auto &&d = D[fora_i]; #define fora_init(...) \ over8(__VA_ARGS__, fora_init_8, a, fora_init_6, a, fora_init_4, a, \ fora_init_2)(__VA_ARGS__) #define forr_init_2(a, A) auto &&a = A[forr_i]; #define forr_init_4(a, b, A, B) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; #define forr_init_6(a, b, c, A, B, C) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; \ auto &&c = C[forr_i]; #define forr_init_8(a, b, c, d, A, B, C, D) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; \ auto &&c = C[forr_i]; \ auto &&d = D[forr_i]; #define forr_init(...) \ over8(__VA_ARGS__, forr_init_8, a, forr_init_6, a, forr_init_4, a, \ forr_init_2)(__VA_ARGS__) #define forp_init(k, v, ...) \ auto &&k = (*forp_it).fi; \ auto &&v = (*forp_it).se; #define form_init(k, v, ...) \ auto &&k = (*form_it).fi; \ auto &&v = (*form_it).se; #define fors_init(v, ...) auto &&v = (*fors_it); #define forlr_init(a, A, ngl, ngr) \ auto a = A[forlr_i]; \ auto prev = forlr_i ? A[forlr_i - 1] : ngl; \ auto next = forlr_i + 1 < rep2lim ? A[forlr_i + 1] : ngr; #define forslr_init4(a, A, ngl, ngr) \ auto a = (*forslr_it); \ auto prev = (forslr_it != A.begin()) ? (*std::prev(forslr_it)) : ngl; \ auto next = (forslr_it != std::prev(A.end())) ? (*std::next(forslr_it)) : ngr; #define forslr_init5(a, A, r, ngl, ngr) \ auto a = (*forslr_it); \ auto prev = (forslr_it != A.begin()) ? (*std::prev(forslr_it)) : ngl; \ auto next = (forslr_it != std::prev(A.end())) ? (*std::next(forslr_it)) : ngr; #define forslr_init6(a, A, l, r, ngl, ngr) \ auto a = (*forslr_it); \ auto prev = (forslr_it != A.begin()) ? (*std::prev(forslr_it)) : ngl; \ auto next = (forslr_it != std::prev(A.end())) ? (*std::next(forslr_it)) : ngr; #define forslr_init(...) \ over6(__VA_ARGS__, forslr_init6, forslr_init5, forslr_init4)(__VA_ARGS__); #define fora_2(a, A) for (auto &&a : A) #define fora_4(a, b, A, B) rep(fora_i, sz(A)) #define fora_6(a, b, c, A, B, C) rep(fora_i, sz(A)) #define fora_8(a, b, c, d, A, B, C, D) rep(fora_i, sz(A)) #define forr_2(a, A) rer(forr_i, sz(A) - 1) #define forr_4(a, b, A, B) rer(forr_i, sz(A) - 1) #define forr_6(a, b, c, A, B, C) rer(forr_i, sz(A) - 1) #define forr_8(a, b, c, d, A, B, C, D) rer(forr_i, sz(A) - 1) //↑@オーバーロード隔離 // rep系はインデックス、for系は中身 #define rep(...) over4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__) #define rer(...) over4(__VA_ARGS__, rer4, rer3, rer2, )(__VA_ARGS__) // char用のrep #define repc(i, m, n) for (char i = m, repc3lim = n; i < repc3lim; ++i) // i,j,k...をnまで見る #define reps(...) over5(__VA_ARGS__, reps5, reps4, reps3, reps2, )(__VA_ARGS__) #define repss(...) \ over10(__VA_ARGS__, repss10, a, repss8, a, repss6, a, repss4, a, \ repss2)(__VA_ARGS__) // vectorのindexを走査する // repv(i,j,vvi) #define repv(...) \ over6(__VA_ARGS__, repv_6, repv_5, repv_4, repv_3, repv_2, )(__VA_ARGS__) #define rerv(i, A) for (int i = sz(A) - 1; i >= 0; i--) // repvn(dp) nは次元 #define repv1(a) repv(i, a) #define repv2(a) repv(i, j, a) #define repv3(a) repv(i, j, k, a) #define repv4(a) repv(i, j, k, l, a) #define fora(...) \ over8(__VA_ARGS__, fora_8, a, fora_6, a, fora_4, a, fora_2)(__VA_ARGS__) #define forr(...) \ over8(__VA_ARGS__, forr_8, a, forr_6, a, forr_4, a, forr_2)(__VA_ARGS__) //#define forr(v, a) for(auto&& v : makeReversedRange(a)) //参照を取らない #define forv(a, b) for (auto a : to1d(b)) //インデックスを前後含めて走査 #define ring(i, s, len) \ for (int i = s, prev = (s == 0) ? len - 1 : s - 1, \ next = (s == len - 1) ? 0 : s + 1, cou = 0; \ cou < len; \ cou++, prev = i, i = next, next = (next == len - 1) ? 0 : next + 1) //値と前後を見る #define ringv(v, d) \ index_ = 0; \ for (auto prev = d[sz(d) - 1], next = (int)d.size() > 1 ? d[1] : d[0], \ v = d[0]; \ index_ < sz(d); index_++, prev = v, v = next, \ next = (index_ >= sz(d) - 1 ? d[0] : d[index_ + 1])) // 左右をnext prevで見る 0の左と nの右 #define forlr(v, d, banpei_l, banpei_r) rep(forlr_i, sz(d)) #define form(...) \ over5(__VA_ARGS__, form5, form4, form3, form2, form1)(__VA_ARGS__) #define fors(...) over4(__VA_ARGS__, fors4, fors3, fors2, fors1)(__VA_ARGS__) #define forslr(...) \ over6(__VA_ARGS__, forslr6, forslr5, forslr4, forslr3)(__VA_ARGS__) #define forp(k, v, st) \ for (auto &&forp_it = st.begin(); forp_it != st.end(); ++forp_it) //マクロ 定数 #define k3 1010 #define k4 10101 #define k5 101010 #define k6 1010101 #define k7 10101010 const ll inf = (ll)1e9 + 100; const ll linf = (ll)1e18 + 100; const dou dinf = (dou)linf * linf; const char infc = '{'; const string infs = "{"; const double PI = 3.1415926535897932384626433832795029L; //マクロ省略形 関数等 #define arsz(a) (sizeof(a) / sizeof(a[0])) #define sz(a) ((ll)(a).size()) #define mp make_pair #define pb pop_back #define pf push_front #define eb emplace_back #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() constexpr bool ev(ll a) { return !(a & 1); } constexpr bool od(ll a) { return (a & 1); } //@拡張系 こう出来るべきというもの //埋め込み 存在を意識せずに機能を増やされているもの namespace std { template <> class hash<std::pair<signed, signed>> { public: size_t operator()(const std::pair<signed, signed> &x) const { return hash<ll>()(((ll)x.first << 32) | x.second); } }; template <> class hash<std::pair<ll, ll>> { public : /*大きいllが渡されると、<<32でオーバーフローするがとりあえず問題ないと判断*/ size_t operator()(const std::pair<ll, ll> &x) const { return hash<ll>()(((ll)x.first << 32) | x.second); } }; } // namespace std // stream まとめ /*@formatter:on*/ istream &operator>>(istream &iss, P &a) { iss >> a.first >> a.second; return iss; } template <typename T> istream &operator>>(istream &iss, vector<T> &vec) { for (T &x : vec) iss >> x; return iss; } template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> p) { os << p.fi << " " << p.se; return os; } ostream &operator<<(ostream &os, T p) { os << p.f << " " << p.s << " " << p.t; return os; } ostream &operator<<(ostream &os, F p) { os << p.a << " " << p.b << " " << p.c << " " << p.d; return os; } template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) { for (ll i = 0; i < vec.size(); ++i) os << vec[i] << (i + 1 == vec.size() ? "" : " "); return os; } template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) { for (ll i = 0; i < vec.size(); ++i) { for (ll j = 0; j < vec[i].size(); ++j) { os << vec[i][j] << " "; } os << endl; } return os; } template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &m) { for (auto &&v : m) os << v; return os; } template <class T> ostream &operator<<(ostream &os, set<T> s) { fora(v, s) { os << v << " "; } return os; } template <class T> ostream &operator<<(ostream &os, deque<T> a) { fora(v, a) os << v << " "; return os; } ostream &operator<<(ostream &os, vector<vector<char>> &vec) { rep(h, sz(vec)) { rep(w, sz(vec[0])) { os << vec[h][w]; } os << endl; } return os; } // template<class T,class U>ostream &operator<<(ostream &os, vector<pair<T,U>>& // a) {fora(v,a)os<<v<<endl;return os;} /*@formatter:off*/ template <typename W, typename H> void resize(W &vec, const H head) { vec.resize(head); } template <typename W, typename H, typename... T> void resize(W &vec, const H &head, const T... tail) { vec.resize(head); for (auto &v : vec) resize(v, tail...); } // 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); }); } //削除した後のvectorを返す template <typename T, typename F> vector<T> erase_if2(vector<T> &v, F f) { vector<T> nv; rep(i, sz(v)) { if (!f(v[i])) { nv.push_back(v[i]); } } return nv; } template <typename T, typename F> vector<vector<T>> erase_if2(vector<vector<T>> &v, F f) { vector<vector<T>> res; rep(i, sz(v)) { res[i] = erase_if2(v[i], f); } return res; } // all_of(A, %2); // all_of(A, a, a%2); #define all_of__2(a, right) all_of2(a, lam(right)) #define all_of__3(a, v, siki) all_of2(a, [&](auto v) { return siki; }) #define all_of(...) over3(__VA_ARGS__, all_of__3, all_of__2)(__VA_ARGS__) #define all_of_f(a, f) all_of2(a, f) #define any_of__2(a, right) any_of2(a, lam(right)) #define any_of__3(a, v, siki) any_of2(a, [&](auto v) { return siki; }) #define any_of(...) over3(__VA_ARGS__, any_of__3, any_of__2)(__VA_ARGS__) #define any_of_f(a, f) any_of2(a, f) #define none_of__2(a, right) none_of2(a, lam(right)) #define none_of__3(a, v, siki) none_of2(a, [&](auto v) { return siki; }) #define none_of(...) over3(__VA_ARGS__, none_of__3, none_of__2)(__VA_ARGS__) #define none_of_f(a, f) none_of2(a, f) #define find_if__2(a, right) find_if2(a, lam(right)) #define find_if__3(a, v, siki) find_if2(a, [&](auto v) { return siki; }) #define find_if(...) over3(__VA_ARGS__, find_if__3, find_if__2)(__VA_ARGS__) #define find_if_f(a, f) find_if2(a, f) #define rfind_if__2(a, right) rfind_if2(a, lam(right)) #define rfind_if__3(a, v, siki) rfind_if2(a, [&](auto v) { return siki; }) #define rfind_if(...) over3(__VA_ARGS__, rfind_if__3, rfind_if__2)(__VA_ARGS__) #define rfind_if_f(a, f) rfind_if2(a, f) #define contains_if__2(a, right) contains_if2(a, lam(right)) #define contains_if__3(a, v, siki) contains_if2(a, [&](auto v) { return siki; }) #define contains_if(...) \ over3(__VA_ARGS__, contains_if__3, contains_if__2)(__VA_ARGS__) #define contains_if_f(a, f) contains_if2(a, f) #define count_if__2(a, right) count_if2(a, lam(right)) #define count_if__3(a, v, siki) count_if2(a, [&](auto v) { return siki; }) #define count_if(...) over3(__VA_ARGS__, count_if__3, count_if__2)(__VA_ARGS__) #define count_if_f(a, f) count_if2(a, f) #define for_each__2(a, right) \ do { \ fora(v, a) { v right; } \ } while (0) #define for_each__3(a, v, siki) \ do { \ fora(v, a) { siki; } \ } while (0) #define for_each(...) over3(__VA_ARGS__, for_each__3, for_each__2)(__VA_ARGS__) #define for_each_f(a, f) \ do { \ fora(v, a) { f(v); } \ } while (0) #define erase_if__2(a, right) erase_if2(a, lam(right)) #define erase_if__3(a, v, siki) erase_if2(a, [&](auto v) { return siki; }) #define erase_if(...) over3(__VA_ARGS__, erase_if__3, erase_if__2)(__VA_ARGS__) #define erase_if_f(a, f) erase_if2(a, f) #define entry_if__2(a, right) erase_if2(a, [&](auto v) { return !(v right); }) #define entry_if__3(a, v, siki) erase_if2(a, [&](auto v) { return !(siki); }) #define entry_if(...) over3(__VA_ARGS__, entry_if__3, entry_if__2)(__VA_ARGS__) #define entry_if_f(a, f) erase_if2(a, [&](auto v) { return !f(v); }) template <class T, class U, class W> void replace(vector<W> &a, T key, U v) { rep(i, sz(a)) if (a[i] == key) a[i] = v; } template <class T, class U, class W> void replace(vector<vector<W>> &A, T key, U v) { rep(i, sz(A)) replace(A[i], key, v); } void replace(str &a, char key, str v) { if (v == "") a.erase(remove(all(a), key), a.end()); } void replace(str &a, char key, char v) { replace(all(a), key, v); } // keyと同じかどうか01で置き換える template <class T, class U> void replace(vector<T> &a, U k) { rep(i, sz(a)) a[i] = a[i] == k; } template <class T, class U> void replace(vector<vector<T>> &a, U k) { rep(i, sz(a)) rep(j, sz(a[0])) a[i][j] = a[i][j] == k; } template <class T> void replace(T &a) { replace(a, '#'); } void replace(str &a, str key, str v) { stringstream t; ll kn = sz(key); std::string::size_type Pos(a.find(key)); ll l = 0; while (Pos != std::string::npos) { t << a.substr(l, Pos - l); t << v; l = Pos + kn; Pos = a.find(key, Pos + kn); } t << a.substr(l, sz(a) - l); a = t.str(); } template <class T> bool includes(vector<T> &a, vector<T> &b) { vi c = a; vi d = b; sort(all(c)); sort(all(d)); return includes(all(c), all(d)); } template <class T> bool is_permutation(vector<T> &a, vector<T> &b) { return is_permutation(all(a), all(b)); } template <class T> bool next_permutation(vector<T> &a) { return next_permutation(all(a)); } void iota(vector<ll> &ve, ll s, ll n) { ve.resize(n); iota(all(ve), s); } vi iota(ll s, ll len) { vi ve(len); iota(all(ve), s); return ve; } template <class A, class B> auto vtop(vector<A> &a, vector<B> &b) { assert(sz(a) == sz(b)); /*stringを0で初期化できない */ vector<pair<A, B>> res; rep(i, sz(a)) res.eb(a[i], b[i]); return res; } template <class A, class B> void ptov(vector<pair<A, B>> &p, vector<A> &a, vector<B> &b) { a.resize(sz(p)), b.resize(sz(p)); rep(i, sz(p)) a[i] = p[i].fi, b[i] = p[i].se; } template <class A, class B, class C> auto vtot(vector<A> &a, vector<B> &b, vector<C> &c) { assert(sz(a) == sz(b) && sz(b) == sz(c)); vector<T2<A, B, C>> res; rep(i, sz(a)) res.eb(a[i], b[i], c[i]); return res; } template <class A, class B, class C, class D> auto vtof(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { assert(sz(a) == sz(b) && sz(b) == sz(c) && sz(c) == sz(d)); vector<F2<A, B, C, D>> res; rep(i, sz(a)) res.eb(a[i], b[i], c[i], d[i]); return res; } enum pcomparator { fisi, fisd, fdsi, fdsd, sifi, sifd, sdfi, sdfd }; enum tcomparator { fisiti, fisitd, fisdti, fisdtd, fdsiti, fdsitd, fdsdti, fdsdtd, fitisi, fitisd, fitdsi, fitdsd, fdtisi, fdtisd, fdtdsi, fdtdsd, sifiti, sifitd, sifdti, sifdtd, sdfiti, sdfitd, sdfdti, sdfdtd, sitifi, sitifd, sitdfi, sitdfd, sdtifi, sdtifd, sdtdfi, sdfdfd, tifisi, tifisd, tifdsi, tifdsd, tdfisi, tdfisd, tdfdsi, tdfdsd, tisifi, tisifd, tisdfi, tisdfd, tdsifi, tdsifd, tdsdfi, tdsdfd }; template <class A, class B> void sort(vector<pair<A, B>> &a, pcomparator type) { typedef pair<A, B> U; if (type == fisi) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se < r.se; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se > r.se; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se < r.se; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se > r.se; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi < r.fi; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi > r.fi; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi < r.fi; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi > r.fi; }); }; template <class U> void sort(vector<U> &a, pcomparator type) { if (type == fisi) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f > r.f; }); }; template <class A, class B, class C, class D> void sort(vector<F2<A, B, C, D>> &a, pcomparator type) { typedef F2<A, B, C, D> U; if (type == fisi) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a > r.a; }); }; template <class U> void sort(vector<U> &a, tcomparator type) { if (type == 0) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t < r.t; }); else if (type == 1) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t > r.t; }); else if (type == 2) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t < r.t; }); else if (type == 3) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t > r.t; }); else if (type == 4) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t < r.t; }); else if (type == 5) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t > r.t; }); else if (type == 6) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t < r.t; }); else if (type == 7) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t > r.t; }); else if (type == 8) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s < r.s; }); else if (type == 9) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s > r.s; }); else if (type == 10) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s < r.s; }); else if (type == 11) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s > r.s; }); else if (type == 12) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s < r.s; }); else if (type == 13) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s > r.s; }); else if (type == 14) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s < r.s; }); else if (type == 15) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s > r.s; }); else if (type == 16) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t < r.t; }); else if (type == 17) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t > r.t; }); else if (type == 18) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t < r.t; }); else if (type == 19) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t > r.t; }); else if (type == 20) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t < r.t; }); else if (type == 21) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t > r.t; }); else if (type == 22) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t < r.t; }); else if (type == 23) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t > r.t; }); else if (type == 24) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f < r.f; }); else if (type == 25) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f > r.f; }); else if (type == 26) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f < r.f; }); else if (type == 27) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f > r.f; }); else if (type == 28) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f < r.f; }); else if (type == 29) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f > r.f; }); else if (type == 30) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f < r.f; }); else if (type == 31) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f > r.f; }); else if (type == 32) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == 33) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == 34) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == 35) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == 36) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == 37) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == 38) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == 39) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == 40) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == 41) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == 42) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == 43) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f > r.f; }); else if (type == 44) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == 45) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == 46) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == 47) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f > r.f; }); } template <class A, class B, class C, class D> void sort(vector<F2<A, B, C, D>> &a, tcomparator type) { typedef F2<A, B, C, D> U; if (type == 0) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c < r.c; }); else if (type == 1) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c > r.c; }); else if (type == 2) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c < r.c; }); else if (type == 3) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c > r.c; }); else if (type == 4) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c < r.c; }); else if (type == 5) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c > r.c; }); else if (type == 6) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c < r.c; }); else if (type == 7) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c > r.c; }); else if (type == 8) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b < r.b; }); else if (type == 9) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b > r.b; }); else if (type == 10) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b < r.b; }); else if (type == 11) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b > r.b; }); else if (type == 12) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b < r.b; }); else if (type == 13) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b > r.b; }); else if (type == 14) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b < r.b; }); else if (type == 15) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b > r.b; }); else if (type == 16) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c < r.c; }); else if (type == 17) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c > r.c; }); else if (type == 18) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c < r.c; }); else if (type == 19) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c > r.c; }); else if (type == 20) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c < r.c; }); else if (type == 21) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c > r.c; }); else if (type == 22) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c < r.c; }); else if (type == 23) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c > r.c; }); else if (type == 24) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a < r.a; }); else if (type == 25) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a > r.a; }); else if (type == 26) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a < r.a; }); else if (type == 27) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a > r.a; }); else if (type == 28) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a < r.a; }); else if (type == 29) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a > r.a; }); else if (type == 30) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a < r.a; }); else if (type == 31) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a > r.a; }); else if (type == 32) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == 33) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == 34) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == 35) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == 36) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == 37) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == 38) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == 39) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == 40) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == 41) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == 42) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == 43) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a > r.a; }); else if (type == 44) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == 45) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == 46) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == 47) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a > r.a; }); } void sort(string &a) { sort(all(a)); } void sort(int &a, int &b) { if (a > b) swap(a, b); } void sort(int &a, int &b, int &c) { sort(a, b); sort(a, c); sort(b, c); } void rsort(int &a, int &b) { if (a < b) swap(a, b); } void rsort(int &a, int &b, int &c) { rsort(a, b); rsort(a, c); rsort(b, c); } template <class T> void sort(vector<T> &a) { sort(all(a)); } // P l, P rで f(P) の形で渡す template <class U, class F> void sort(vector<U> &a, F f) { sort(all(a), [&](U l, U r) { return f(l) < f(r); }); }; template <class T> void rsort(vector<T> &a) { sort(all(a), greater<T>()); }; template <class U, class F> void rsort(vector<U> &a, F f) { sort(all(a), [&](U l, U r) { return f(l) > f(r); }); }; // F = T<T> //例えばreturn p.fi + p.se; template <class A, class B> void sortp(vector<A> &a, vector<B> &b) { auto c = vtop(a, b); sort(c); rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se; } template <class A, class B, class F> void sortp(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); sort(c, f); rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se; } template <class A, class B> void rsortp(vector<A> &a, vector<B> &b) { auto c = vtop(a, b); rsort(c); rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second; } template <class A, class B, class F> void rsortp(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); rsort(c, f); rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second; } template <class A, class B, class C> void sortt(vector<A> &a, vector<B> &b, vector<C> &c) { auto d = vtot(a, b, c); sort(d); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class F> void sortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); sort(d, f); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C> void rsortt(vector<A> &a, vector<B> &b, vector<C> &c) { auto d = vtot(a, b, c); rsort(d); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class F> void rsortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); rsort(d, f); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class D> void sortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { auto e = vtof(a, b, c, d); sort(e); rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d; } template <class A, class B, class C, class D> void rsortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { auto e = vtof(a, b, c, d); rsort(e); rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d; } // sortindex 元のvectorはソートしない template <class T> vi sorti(vector<T> &a) { auto b = a; vi ind = iota(0, sz(a)); sortp(b, ind); return ind; } /*indexの分で型が変わるためpcomparatorが必要*/ template <class T> vi sorti(vector<T> &a, pcomparator f) { auto b = a; vi ind = iota(0, sz(a)); sortp(b, ind, f); return ind; } template <class T, class F> vi sorti(vector<T> &a, F f) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(a[x]) < f(a[y]); }); return ind; } template <class T> vi rsorti(vector<T> &a) { auto b = a; vi ind = iota(0, sz(a)); rsortp(b, ind); return ind; } template <class T, class F> vi rsorti(vector<T> &a, F f) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(a[x]) > f(a[y]); }); return ind; } template <class A, class B, class F> vi sortpi(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(c[x]) < f(c[y]); }); return ind; } template <class A, class B> vi sortpi(vector<A> &a, vector<B> &b, pcomparator f) { vi ind = iota(0, sz(a)); auto c = a; auto d = b; sortt(c, d, ind, f); return ind; } template <class A, class B> vi sortpi(vector<A> &a, vector<B> &b) { return sortpi(a, b, fisi); }; template <class A, class B, class F> vi rsortpi(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(c[x]) > f(c[y]); }); return ind; } template <class A, class B> vi rsortpi(vector<A> &a, vector<B> &b) { return sortpi(a, b, fdsd); }; template <class A, class B, class C, class F> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(d[x]) < f(d[y]); }); return ind; } template <class A, class B, class C> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, pcomparator f) { vi ind = iota(0, sz(a)); auto d = vtof(a, b, c, ind); sort(d, f); rep(i, sz(a)) ind[i] = d[i].d; return ind; } template <class A, class B, class C> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { if (a[x] == a[y]) { if (b[x] == b[y]) return c[x] < c[y]; else return b[x] < b[y]; } else { return a[x] < a[y]; } }); return ind; } template <class A, class B, class C, class F> vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(d[x]) > f(d[y]); }); return ind; } template <class A, class B, class C> vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { if (a[x] == a[y]) { if (b[x] == b[y]) return c[x] > c[y]; else return b[x] > b[y]; } else { return a[x] > a[y]; } }); return ind; } template <class T> void sort2(vector<vector<T>> &a) { for (ll i = 0, n = a.size(); i < n; ++i) sort(a[i]); } template <class T> void rsort2(vector<vector<T>> &a) { for (ll i = 0, n = a.size(); i < n; ++i) rsort(a[i]); } template <class... T, class U> auto sorted(U head, T... a) { sort(head, a...); return head; } 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 W, typename T> void fill(W &xx, const T vall) { xx = vall; } template <typename W, typename T> void fill(vector<W> &vecc, const T vall) { for (auto &&vx : vecc) fill(vx, vall); } template <typename W, typename T> void fill(vector<W> &xx, ll len, const T v) { rep(i, len) xx[i] = v; } template <typename W, typename T> void fill(vector<vector<W>> &xx, int sh, int th, int sw, int tw, T v) { rep(h, sh, th) rep(w, sw, tw) xx[h][w] = v; } template <class T, class U> void fill(vector<T> &a, vi &ind, U val) { fora(v, ind) a[v] = val; } template <class W, class T> void fill(mvec<W> &xx, const T v) { fora(x, xx) fill(x, v); } template <typename A, size_t N> A sum(A (&a)[N]) { A res = 0; rep(i, N) res += a[i]; return res; } template <typename A, size_t N, size_t O> A sum(A (&a)[N][O]) { A res = 0; rep(i, N) rep(j, O) res += a[i][j]; return res; } template <typename A, size_t N, size_t O, size_t P> A sum(A (&a)[N][O][P]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) res += a[i][j][k]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A sum(A (&a)[N][O][P][Q]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) res += a[i][j][k][l]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A sum(A (&a)[N][O][P][Q][R]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) res += a[i][j][k][l][m]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A sum(A (&a)[N][O][P][Q][R][S]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) rep(n, S) res += a[i][j][k][l][m][n]; return res; } //@汎用便利関数 入力 ll in() { ll ret; cin >> ret; return ret; } string sin() { string ret; cin >> ret; return ret; } template <class T> void in(T &head) { cin >> head; } template <class T, class... U> void in(T &head, U &...tail) { cin >> head; in(tail...); } #define din1(a) \ ll a; \ cin >> a #define din2(a, b) \ ll a, b; \ cin >> a >> b #define din3(a, b, c) \ ll a, b, c; \ cin >> a >> b >> c #define din4(a, b, c, d) \ ll a, b, c, d; \ cin >> a >> b >> c >> d #define din5(a, b, c, d, e) \ ll a, b, c, d, e; \ cin >> a >> b >> c >> d >> e #define din6(a, b, c, d, e, f) \ ll a, b, c, d, e, f; \ cin >> a >> b >> c >> d >> e >> f #define din(...) \ over6(__VA_ARGS__, din6, din5, din4, din3, din2, din1)(__VA_ARGS__) #define dins1(a) \ str a; \ cin >> a #define dins2(a, b) \ str a, b; \ cin >> a >> b #define dins3(a, b, c) \ str a, b, c; \ cin >> a >> b >> c #define dins4(a, b, c, d) \ str a, b, c, d; \ cin >> a >> b >> c >> d #define dins5(a, b, c, d, e) \ str a, b, c, d, e; \ cin >> a >> b >> c >> d >> e #define dins6(a, b, c, d, e, f) \ str a, b, c, d, e, f; \ cin >> a >> b >> c >> d >> e >> f #define dins(...) \ over6(__VA_ARGS__, dins6, dins5, dins4, dins3, dins2, dins1)(__VA_ARGS__) #define din1d(a) \ din1(a); \ a-- #define din2d(a, b) \ din2(a, b); \ a--, b-- #define din3d(a, b, c) \ din3(a, b, c); \ a--, b--, c-- #define din4d(a, b, c, d) \ din4(a, b, c, d); \ a--, b--, c--, d-- #define dind(...) over4(__VA_ARGS__, din4d, din3d, din2d, din1d)(__VA_ARGS__) template <class T> void out2(T &&head) { cout << head; } template <class T, class... U> void out2(T &&head, U &&...tail) { cout << head << " "; out2(tail...); } template <class T, class... U> void out(T &&head, U &&...tail) { cout << head << " "; out2(tail...); cout << "" << endl; } template <class T> void out(T &&head) { cout << head << endl; } void out() { cout << "" << endl; } #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; } /*@formatter:on*/ 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]; if (typeid(a[i]) == typeid(P)) { ss << endl; } else { ss << " "; } } return ss.str(); } /*@formatter:off*/ 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(); } 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 = 12; H = min(H, sz(a)); rep(i, H) { ss << endl; ss << out_m2(a[i], W, U, i); } return ss.str(); } template <class T, size_t N> string out_m2(T (&a)[N]) { vector<T> b; resize(b, N); rep(i, N) { b[i] = a[i]; } return out_m2(b); } template <class T, size_t N, size_t M> string out_m2(T (&a)[N][M]) { vector<vector<T>> b; resize(b, N, M); rep(i, N) { rep(j, M) { b[i][j] = a[i][j]; } } return out_m2(b); } template <class T, size_t N, size_t M, size_t O> string out_m2(T (&a)[N][M][O]) { vector<vector<vector<T>>> b; resize(b, N, M, O); rep(i, N) { rep(j, M) { rep(k, O) { b[i][j][k] = a[i][j][k]; } } } return out_m2(b); } string out_m2(int a) { stringstream ss; ss << a; return ss.str(); } /*@formatter:on*/ template <class T> string out_m2(mvec<mvec<T>> &a, ll H = inf, ll W = inf, int key = inf) { H = min({H, sz(a), 6ll}); W = min({W, sz(a[0]), 6ll}); stringstream ss; ss << endl; // if (key == inf)ss << " *|"; else ss << " " << key << "|"; if (key == inf) ss << " *|"; else { ss << std::right << std::setw(2) << key; ss << "|"; } rep(w, -W, W) ss << std::right << std::setw(4) << w; ss << "" << endl; rep(w, W * 8 + 3) ss << "_"; ss << "" << endl; rep(h, -H, H) { ss << std::right << std::setw(2) << h << "|"; int NW = min(sz(a[h]), 6ll); rep(w, -NW, NW) { if (abs(a[h][w]) == linf) ss << " e" << ""; else ss << std::right << std::setw(4) << a[h][w]; } ss << "" << endl; } return ss.str(); } /*@formatter:on*/ template <class T> string out_m2(mvec<mvec<mvec<T>>> &a, ll H = inf, ll W = inf, ll U = inf) { stringstream ss; if (H == inf) H = 6; H = min(H, sz(a)); rep(i, -H, H) { ss << endl; ss << out_m2(a[i], W, U, i); } return ss.str(); } /*@formatter:off*/ template <class T> string out_m2(T &a) { stringstream ss; ss << a; return ss.str(); } /*@formatter:on*/ 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(); } /*@formatter:off*/ 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(); } 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 template <class T> void outl(vector<T> &a, int n = inf) { rep(i, min(n, sz(a))) cout << a[i] << endl; } //テーブルをスペースなしで出力 template <class T> void outt(vector<vector<T>> &a) { rep(i, sz(a)) { rep(j, sz(a[i])) { cout << a[i][j]; } cout << endl; } } // int型をbit表記で出力 void outb(int a) { cout << bitset<20>(a) << endl; } template <class T> void na(vector<T> &a, ll n) { a.resize(n); rep(i, n) cin >> a[i]; } #define dna(a, n) \ vi a(n); \ rep(dnai, n) cin >> a[dnai]; #define dnad(a, n) \ vi a(n); \ rep(dnai, n) cin >> a[dnai], a[dnai]--; template <class T> void nao(vector<T> &a, ll n) { a.resize(n + 1); a[0] = 0; rep(i, n) cin >> a[i + 1]; } template <class T> void naod(vector<T> &a, ll n) { a.resize(n + 1); a[0] = 0; rep(i, n) cin >> a[i + 1], a[i + 1]--; } template <class T> void nad(vector<T> &a, ll n) { a.resize(n); rep(i, n) cin >> a[i], a[i]--; } template <class T, class U> void na2(vector<T> &a, vector<U> &b, ll n) { a.resize(n); b.resize(n); rep(i, n) cin >> a[i] >> b[i]; } #define dna2(a, b, n) \ vi a(n), b(n); \ rep(dna2i, n) cin >> a[dna2i] >> b[dna2i]; template <class T, class U> void nao2(vector<T> &a, vector<U> &b, ll n) { a.resize(n + 1); b.resize(n + 1); a[0] = b[0] = 0; rep(i, n) cin >> a[i + 1] >> b[i + 1]; } #define dna2d(a, b, n) \ vi a(n), b(n); \ rep(dna2di, n) { \ cin >> a[dna2di] >> b[dna2di]; \ a[dna2di]--, b[dna2di]--; \ } template <class T, class U> void na2d(vector<T> &a, vector<U> &b, ll n) { a.resize(n); b.resize(n); rep(i, n) cin >> a[i] >> b[i], a[i]--, b[i]--; } template <class T, class U, class W> void na3(vector<T> &a, vector<U> &b, vector<W> &c, ll n) { a.resize(n); b.resize(n); c.resize(n); rep(i, n) cin >> a[i] >> b[i] >> c[i]; } #define dna3(a, b, c, n) \ vi a(n), b(n), c(n); \ rep(dna3i, n) cin >> a[dna3i] >> b[dna3i] >> c[dna3i]; template <class T, class U, class W> void na3d(vector<T> &a, vector<U> &b, vector<W> &c, ll 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 dna3d(a, b, c, n) \ vi a(n), b(n), c(n); \ rep(dna3di, n) { \ cin >> a[dna3di] >> b[dna3di] >> c[dna3di]; \ a[dna3di]--, b[dna3di]--, c[dna3di]--; \ } template <class T, class U, class W, class X> void na4(vector<T> &a, vector<U> &b, vector<W> &c, vector<X> &d, ll n) { a.resize(n); b.resize(n); c.resize(n); d.resize(n); rep(i, n) cin >> a[i] >> b[i] >> c[i] >> d[i]; } #define dna4(a, b, c, d, n) \ vi a(n), b(n), c(n), d(n); \ rep(dna4i, n) cin >> a[dna4i] >> b[dna4i] >> c[dna4i] >> d[dna4i]; #define dna4d(a, b, c, d, n) \ vi a(n), b(n), c(n), d(n); \ rep(dna4i, n) cin >> a[dna4i] >> b[dna4i] >> c[dna4i] >> d[dna4i], \ --a[dna4i], --b[dna4i], --c[dna4i], --d[dna4i]; #define nt(a, h, w) \ resize(a, h, w); \ rep(nthi, h) rep(ntwi, w) cin >> a[nthi][ntwi]; #define ntd(a, h, w) \ resize(a, h, w); \ rep(ntdhi, h) rep(ntdwi, w) cin >> a[ntdhi][ntdwi], a[ntdhi][ntdwi]--; #define ntp(a, h, w) \ resize(a, h + 2, w + 2); \ fill(a, '#'); \ rep(ntphi, 1, h + 1) rep(ntpwi, 1, w + 1) cin >> a[ntphi][ntpwi]; //デバッグ #define sp << " " << #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__) #ifdef _DEBUG #define deb(...) \ cerr << over10(__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' //@formatter:off //よく使うクラス、構造体 // graphでredefineしているため、書き換えたら向こうも書き換える struct unionfind { vector<ll> par; vector<ll> siz; vector<ll> es; ll n, trees; //連結グループの数(親の種類) unionfind(ll n) : n(n), trees(n) { par.resize(n); siz.resize(n); es.resize(n); for (ll i = 0; i < n; i++) { par[i] = i; siz[i] = 1; } } ll root(ll x) { if (par[x] == x) { return x; } else { return par[x] = root(par[x]); } } ll operator()(ll x) { return root(x); } bool unite(ll x, ll y) { x = root(x); y = root(y); es[x]++; if (x == y) return false; if (siz[x] > siz[y]) swap(x, y); trees--; par[x] = y; siz[y] += siz[x]; es[y] += es[x]; return true; } bool same(ll x, ll y) { return root(x) == root(y); } ll size(ll x) { return siz[root(x)]; } ll esize(ll x) { return es[root(x)]; } vi sizes() { vi cou(n); vi ret; ret.reserve(n); rep(i, n) { cou[root(i)]++; } rep(i, n) { if (cou[i]) ret.push_back(cou[i]); } return ret; } //つながりを無向グラフと見なし、xが閉路に含まれるか判定 bool close(ll x) { return esize(x) >= size(x); } vector<vi> sets() { vi ind(n, -1); ll i = 0; vvi(res, trees); rep(j, n) { ll r = root(j); if (ind[r] == -1) ind[r] = i++; res[ind[r]].push_back(j); } rep(i, trees) { ll r = root(res[i][0]); if (res[i][0] == r) continue; rep(j, 1, sz(res[i])) { if (res[i][j] == r) { swap(res[i][0], res[i][j]); break; } } } return res; } }; //@formatter:off using bll = __int128; using u32 = unsigned; using u64 = unsigned long long; using u128 = __uint128_t; std::ostream &operator<<(std::ostream &dest, __int128_t value) { std::ostream::sentry s(dest); if (s) { __uint128_t tmp = value < 0 ? -value : value; char buffer[128]; char *d = std::end(buffer); do { --d; *d = "0123456789"[tmp % 10]; tmp /= 10; } while (tmp != 0); if (value < 0) { --d; *d = '-'; } ll len = std::end(buffer) - d; if (dest.rdbuf()->sputn(d, len) != len) { dest.setstate(std::ios_base::badbit); } } return dest; } //__int128 toi128(string &s) { __int128 ret = 0; for (ll i = 0; i < //s.length(); ++i) if ('0' <= s[i] && s[i] <= '9') ret = 10 * //ret + s[i] - '0'; return ret;} //エラー 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 re() { assert(0 == 1); exit(0); } void tle() { while (inf) cout << inf << endl; } //便利関数 //テスト用 #define rand xor128_ unsigned long xor128_(void) { static unsigned long x = 123456789, y = 362436069, z = 521288629, w = 88675123; unsigned long t; t = (x ^ (x << 11)); x = y; y = z; z = w; return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8))); } char ranc() { return (char)('a' + rand() % 26); } ll rand(ll min, ll max) { assert(min <= max); if (min >= 0 && max >= 0) { return rand() % (max + 1 - min) + min; } else if (max < 0) { return -rand(-max, -min); } else { if (rand() % 2) { return rand(0, max); } else { return -rand(0, -min); } } } vi ranv(ll n, ll min, ll max) { vi v(n); rep(i, n) v[i] = rand(min, max); return v; } str ransu(ll n) { str s; rep(i, n) s += (char)rand('A', 'Z'); return s; } str ransl(ll n) { str s; rep(i, n) s += (char)rand('a', 'z'); return s; } //単調増加 vi ranvinc(ll n, ll min, ll max) { vi v(n); bool bad = 1; while (bad) { bad = 0; v.resize(n); rep(i, n) { if (i && min > max - v[i - 1]) { bad = 1; break; } if (i) v[i] = v[i - 1] + rand(min, max - v[i - 1]); else v[i] = rand(min, max); } } return v; } //便利 汎用 void ranvlr(ll n, ll min, ll max, vi &l, vi &r) { l.resize(n); r.resize(n); rep(i, n) { l[i] = rand(min, max); r[i] = l[i] + rand(0, max - l[i]); } } template <class T> vector<pair<T, int>> run_length(vector<T> &a) { vector<pair<T, int>> 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 <class F> ll mgr(ll ok, ll ng, F f) { bool han = true; if (ok < ng) while (ng - ok > 1) { ll mid = (ok + ng) >> 1; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; deb(mid, han); } else while (ok - ng > 1) { ll mid = (ok + ng) >> 1; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; deb(mid, han); } return ok; } template <class F> dou mgrd(dou ok, dou ng, F f, int kai = 100) { bool han = true; if (ok < ng) rep(i, kai) { dou mid = (ok + ng) / 2; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; deb(mid, han); } else rep(i, kai) { dou mid = (ok + ng) / 2; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; deb(mid, han); } return ok; } // strを整数として比較 string smax(str &a, str b) { if (sz(a) < sz(b)) { return b; } else if (sz(a) > sz(b)) { return a; } else if (a < b) return b; else return a; } // strを整数として比較 string smin(str &a, str b) { if (sz(a) > sz(b)) { return b; } else if (sz(a) < sz(b)) { return a; } else if (a > b) return b; else return a; } //エラー-1 template <typename W, typename T> ll find(vector<W> &a, int l, const T key) { rep(i, l, sz(a)) if (a[i] == key) return i; return -1; } template <typename W, typename T> ll find(vector<W> &a, const T key) { rep(i, sz(a)) if (a[i] == key) return i; return -1; } template <typename W, typename T> P find(vector<vector<W>> &a, const T key) { rep(i, sz(a)) rep(j, sz(a[0])) if (a[i][j] == key) return mp(i, j); return mp(-1, -1); } template <typename W, typename U> T find(vector<vector<vector<W>>> &a, const U key) { rep(i, sz(a)) rep(j, sz(a[0])) rep(k, sz(a[0][0])) if (a[i][j][k] == key) return mt(i, j, k); return mt(-1, -1, -1); } // stringも書く int find(string &s, const string key) { int klen = sz(key); rep(i, sz(s) - klen + 1) { if (s[i] != key[0]) continue; if (s.substr(i, klen) == key) { return i; } } return -1; } int find(string &s, int l, const string key) { int klen = sz(key); rep(i, l, sz(s) - klen + 1) { if (s[i] != key[0]) continue; if (s.substr(i, klen) == key) { return i; } } return -1; } int find(string &s, const char key) { rep(i, sz(s)) { if (s[i] == key) return i; } return -1; } int find(string &s, int l, const char key) { rep(i, l, sz(s)) { if (s[i] == key) return i; } return -1; } template <typename W, typename T> ll count2(W &a, const T k) { return a == k; } template <typename W, typename T> ll count2(vector<W> &a, const T k) { ll ret = 0; fora(v, a) ret += count2(v, k); return ret; } template <typename W, typename T> ll count(vector<W> &a, const T k) { ll ret = 0; fora(v, a) ret += count2(v, k); return ret; } vi count(vi &a) { int ma = 0; fora(v, a) { if (ma < v) ma = v; } vi res(ma + 1); fora(v, a) { res[v]++; } return res; } ll count(str &a, str k) { ll ret = 0, len = k.length(); auto pos = a.find(k); while (pos != string::npos) pos = a.find(k, pos + len), ++ret; return ret; } vi count(str &a) { vi cou(26); char c = 'a'; if ('A' <= a[0] && a[0] <= 'Z') c = 'A'; rep(i, sz(a))++ cou[a[i] - c]; return cou; } #define couif count_if // algorythm 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)); } template <class U> void rev(vector<vector<U>> &a) { vector<vector<U>> b(sz(a[0]), vector<U>(sz(a))); rep(h, sz(a)) rep(w, sz(a[0])) b[w][h] = a[h][w]; a = b; } void rev(string &a) { reverse(all(a)); } constexpr ll p10[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000ll, 100000000000ll, 1000000000000ll, 10000000000000ll, 100000000000000ll, 1000000000000000ll, 10000000000000000ll, 100000000000000000ll, 1000000000000000000ll}; ll get(ll a, ll keta) { return (a / (ll)pow(10, keta)) % 10; } // 0は0桁 ll keta(ll v) { if (v < p10[9]) { if (v < p10[4]) { if (v < p10[2]) { if (v < p10[1]) { if (v < p10[0]) return 0; else return 1; } else return 2; } else { if (v < p10[3]) return 3; else return 4; } } else { if (v < p10[7]) { if (v < p10[5]) return 5; else if (v < p10[6]) return 6; else return 7; } else { if (v < p10[8]) return 8; else return 9; } } } else { if (v < p10[13]) { if (v < p10[11]) { if (v < p10[10]) return 10; else return 11; } else { if (v < p10[12]) return 12; else return 13; } } else { if (v < p10[15]) { if (v < p10[14]) return 14; else return 15; } else { if (v < p10[17]) { if (v < p10[16]) return 16; else return 17; } else { if (v < p10[18]) return 18; else return 19; } } } } } ll dsum(ll v, ll sin = 10) { ll ret = 0; for (; v; v /= sin) ret += v % sin; return ret; } ll mask10(ll v) { return p10[v] - 1; } //変換系 //[v] := iとなるようなvectorを返す //存在しない物は-1 template <class T> auto keys(T &a) { vector<decltype((a.begin())->fi)> res; for (auto &&k : a) res.push_back(k.fi); return res; } template <class T> auto values(T &a) { vector<decltype((a.begin())->se)> res; for (auto &&k : a) res.push_back(k.se); return res; } template <class T, class U> bool chma(T &a, const U &b) { if (a < b) { a = b; return true; } return false; } template <class T, class U> bool chmi(T &a, const U &b) { if (b < a) { a = b; return true; } return false; } template <class T> constexpr T min(T a, signed b) { return a < b ? a : b; } template <class T> constexpr T max(T a, signed b) { return a < b ? b : a; } template <class T> constexpr T min(T a, T b, T c) { return a >= b ? b >= c ? c : b : a >= c ? c : a; } template <class T> constexpr T max(T a, T b, T c) { return a <= b ? b <= c ? c : b : a <= c ? c : a; } template <class T> T min(vector<T> &a) { return *min_element(all(a)); } template <class T> T mini(vector<T> &a) { return min_element(all(a)) - a.begin(); } template <class T> T min(vector<T> &a, ll n) { return *min_element(a.begin(), a.begin() + min(n, sz(a))); } template <class T> T min(vector<T> &a, ll s, ll n) { return *min_element(a.begin() + s, a.begin() + min(n, sz(a))); } template <class T> T max(vector<T> &a) { return *max_element(all(a)); } template <class T, class U> T max(vector<T> &a, vector<U> &b) { return max(*max_element(all(a)), *max_element(all(b))); } template <class T> T maxi(vector<T> &a) { return max_element(all(a)) - a.begin(); } template <class T> T max(vector<T> &a, ll n) { return *max_element(a.begin(), a.begin() + min(n, sz(a))); } template <class T> T max(vector<T> &a, ll s, ll n) { return *max_element(a.begin() + s, a.begin() + min(n, sz(a))); } template <typename A, size_t N> A max(A (&a)[N]) { A res = a[0]; rep(i, N) res = max(res, a[i]); return res; } template <typename A, size_t N, size_t O> A max(A (&a)[N][O]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P> A max(A (&a)[N][O][P]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A max(A (&a)[N][O][P][Q], const T &v) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A max(A (&a)[N][O][P][Q][R]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A max(A (&a)[N][O][P][Q][R][S]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N> A min(A (&a)[N]) { A res = a[0]; rep(i, N) res = min(res, a[i]); return res; } template <typename A, size_t N, size_t O> A min(A (&a)[N][O]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P> A min(A (&a)[N][O][P]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A min(A (&a)[N][O][P][Q], const T &v) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A min(A (&a)[N][O][P][Q][R]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A min(A (&a)[N][O][P][Q][R][S]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <class T> T sum(vector<T> &v, ll s, ll t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += v[i]; return ret; } template <class T> T sum(vector<T> &v, ll t = inf) { return sum(v, 0, t); } template <class T> T sum(vector<vector<T>> &v, int s, int t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<T>> &v, int t = inf) { return sum(v, 0, t); } template <class T> T sum(vector<vector<vector<T>>> &v, int s, int t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<T>>> &v, int t = inf) { return sum(v, 0, t); } template <class T> T sum(vector<vector<vector<vector<T>>>> &v, int s, int t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<vector<T>>>> &v, int t = inf) { return sum(v, 0, t); } template <class T> T sum(vector<vector<vector<vector<vector<T>>>>> &v, int s, int t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<vector<vector<T>>>>> &v, int t = inf) { return sum(v, 0, t); } template <class T> auto sum(priority_queue<T, vector<T>, greater<T>> &r) { auto q = r; T ret = 0; while (sz(q)) { ret += q.top(); q.pop(); } return ret; } template <class T> auto sum(priority_queue<T> &r) { auto q = r; T ret = 0; while (sz(q)) { ret += q.top(); q.pop(); } return ret; } // template<class T, class U, class... W> auto sumn(vector<T> &v, U head, W... // tail) { auto ret = sum(v[0], tail...); rep(i, 1, min(sz(v), head))ret += // sum(v[i], tail...); return ret;} vi v_i(vi &a) { int n = max(a) + 1; vi ret(n, -1); rep(i, sz(a)) { ret[a[i]] = i; } return ret; } void clear(PQ &q) { q = PQ(); } void clear(priority_queue<int> &q) { q = priority_queue<int>(); } template <class T> void clear(queue<T> &q) { while (q.size()) q.pop(); } template <class T> T *negarr(ll size) { T *body = (T *)malloc((size * 2 + 1) * sizeof(T)); return body + size; } template <class T> T *negarr2(ll h, ll w) { double **dummy1 = new double *[2 * h + 1]; double *dummy2 = new double[(2 * h + 1) * (2 * w + 1)]; dummy1[0] = dummy2 + w; for (ll i = 1; i <= 2 * h + 1; ++i) { dummy1[i] = dummy1[i - 1] + 2 * w + 1; } double **a = dummy1 + h; return a; } // 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> vector<T> ima(vector<T> &v) { vector<T> ret = v; rep(i, sz(ret) - 1) chma(ret[i + 1], ret[i]); return ret; } // template<class T> vector<T> rimi(vector<T> &v) { vector<T> ret = v; rer(i, // sz(ret) - 1, 1)chmi(ret[i - 1], ret[i]); return ret;} template<class T> // vector<T> rima(vector<T> &v) { vector<T> ret = v; rer(i, sz(ret) - 1, // 1)chma(ret[i - 1], ret[i]); return ret;} template <class T> struct ruiC { vector<T> rui; ruiC(vector<T> &ru) : rui(ru) {} /*先頭0*/ ruiC() : rui(1, 0) {} T operator()(ll l, ll r) { if (l > r) { cerr << "ruic "; deb(l, r); assert(0); } return rui[r] - rui[l]; } T operator()(int r) { return operator()(0, r); } /*ruiv[]をruic[]に変えた際意味が変わるのがまずいため()と統一*/ /*単体iを返す 累積でないことに注意(seg木との統一でこうしている)*/ // T operator[](ll i) { return rui[i + 1] - rui[i]; } T operator[](ll i) { return rui[i]; } /*0から順に追加される必要がある*/ void operator+=(T v) { rui.push_back(rui.back() + v); } void add(int i, T v) { if (sz(rui) - 1 != i) ole(); operator+=(v); } T back() { return rui.back(); } ll size() { return rui.size(); } auto begin() { return rui.begin(); } auto end() { return rui.end(); } }; template <class T> struct ruimax { template <typename Monoid> struct SegmentTree { /*pairで処理*/ int sz; vector<Monoid> seg; const Monoid M1 = mp(MIN(T), -1); Monoid f(Monoid a, Monoid b) { return max(a, b); } void build(vector<T> &a) { int n = sz(a); sz = 1; while (sz < n) sz <<= 1; seg.assign(2 * sz, M1); rep(i, n) { seg[i + sz] = mp(a[i], i); } for (int k = sz - 1; k > 0; k--) { seg[k] = f(seg[k << 1], seg[(k << 1) | 1]); } } Monoid query(int a, int b) { Monoid L = M1, R = M1; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) L = f(L, seg[a++]); if (b & 1) R = f(seg[--b], R); } return f(L, R); } Monoid operator[](const int &k) const { return seg[k + sz]; } }; private: vector<T> ve; SegmentTree<pair<T, int>> seg; vector<T> rv; vector<T> ri; bool build = false; public: int n; ruimax(vector<T> &a) : ve(a), n(sz(a)) { int index = -1; int ma = MIN(T); rv.resize(n + 1); ri.resize(n + 1); rv[0] = -linf; ri[0] = -1; rep(i, n) { if (chma(ma, a[i])) { index = i; } rv[i + 1] = ma; ri[i + 1] = index; } } T operator()(int l, int r) { if (!(l <= r && 0 <= l && r <= n)) { deb(l, r, n); assert(0); } if (l == 0) { return rv[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).first; } } T operator()(int r = inf) { return operator()(0, min(r, n)); } T operator[](int r) { return operator()(0, r); } T getv(int l, int r) { return operator()(l, r); } T geti(int l, int r) { assert(l <= r && 0 <= l && r <= n); if (l == 0) { return ri[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).second; } } T geti(int r = inf) { return geti(0, min(r, n)); }; T getv(int r = inf) { return getv(0, min(r, n)); }; auto begin() { return rv.begin(); } auto end() { return rv.end(); } }; template <class T> struct ruimin { template <typename Monoid> struct SegmentTree { /*pairで処理*/ int sz; vector<Monoid> seg; const Monoid M1 = mp(MAX(T), -1); Monoid f(Monoid a, Monoid b) { return min(a, b); } void build(vector<T> &a) { int n = sz(a); sz = 1; while (sz < n) sz <<= 1; seg.assign(2 * sz, M1); rep(i, n) { seg[i + sz] = mp(a[i], i); } for (int k = sz - 1; k > 0; k--) { seg[k] = f(seg[k << 1], seg[(k << 1) | 1]); } } Monoid query(int a, int b) { Monoid L = M1, R = M1; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) L = f(L, seg[a++]); if (b & 1) R = f(seg[--b], R); } return f(L, R); } Monoid operator[](const int &k) const { return seg[k + sz]; } }; private: vector<T> ve; SegmentTree<pair<T, int>> seg; vector<T> rv; vector<T> ri; bool build = false; int n; public: ruimin(vector<T> &a) : ve(a), n(sz(a)) { int index = -1; int mi = MAX(T); rv.resize(n + 1); ri.resize(n + 1); rv[0] = linf; ri[0] = -1; rep(i, n) { if (chmi(mi, a[i])) { index = i; } rv[i + 1] = mi; ri[i + 1] = index; } } T operator()(int l, int r) { assert(l <= r && 0 <= l && r <= n); if (l == 0) { return rv[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).first; } } T operator()(int r = inf) { return operator()(0, min(r, n)); } T operator[](int r) { return operator()(0, r); } T getv(int l, int r) { return operator()(l, r); } T geti(int l, int r) { assert(l <= r && 0 <= l && r <= n); if (l == 0) { return ri[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).second; } } T geti(int r = inf) { return geti(0, min(r, n)); }; T getv(int r = inf) { return getv(0, min(r, n)); }; auto begin() { return rv.begin(); } auto end() { return rv.end(); } }; 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() { return ruiC<T>(); } template <class T> ruiC<T> ruic(vector<T> &a) { vector<T> ret = ruiv(a); return ruiC<T>(ret); } vvi() ruib(vi &a) { vvi(res, 61, sz(a) + 1); rep(k, 61) { rep(i, sz(a)) { res[k][i + 1] = res[k][i] + ((a[i] >> k) & 1); } } return res; } vector<ruiC<int>> ruibc(vi &a) { vector<ruiC<int>> ret(61); vvi(res, 61, sz(a)); rep(k, 61) { rep(i, sz(a)) { res[k][i] = (a[i] >> k) & 1; } ret[k] = ruic(res[k]); } return 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); } // h query template <class T> vector<T> imoh(vector<vector<T>> &v, int w) { vector<T> ret(sz(v)); rep(h, sz(ret)) { ret[h] = v[h][w]; } rep(i, sz(ret) - 1) { ret[i + 1] += ret[i]; } return ret; } template <class T> vector<T> ruih(vector<vector<T>> &v, int w) { vector<T> ret(sz(v) + 1); rep(h, sz(v)) { ret[h + 1] = v[h][w]; } rep(i, sz(v)) { ret[i + 1] += ret[i]; } return ret; } template <class T> ruiC<T> ruihc(vector<vector<T>> &a, int w) { vector<T> ret = ruih(a, w); return ruiC<T>(ret); } // xor template <class T> struct ruixC { 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> struct rruiC { vector<T> rui; int n; rruiC(vector<T> &a) : n(sz(a)) { rui.resize(n + 1); rer(i, n - 1) { rui[i] = rui[i + 1] + a[i]; } } /*[r l)*/ T operator()(int r, int l) { r++; l++; assert(l <= r && l >= 0 && r <= n); return rui[l] - rui[r]; } T operator()(int l) { return operator()(n - 1, l); } T operator[](int i) { return operator()(i); } }; template <class T> ostream &operator<<(ostream &os, rruiC<T> a) { fora(v, a.rui) os << v << " "; return os; } #define rrui rruic template <class T> rruiC<T> rruic(vector<T> &a) { return rruiC<T>(a); } //掛け算 template <class T> struct ruimulC { vector<T> rv; int n; ruimulC(vector<T> &a) : rv(a), n(sz(a)) { rv.resize(n + 1); rv[0] = 1; rep(i, n) { rv[i + 1] = a[i] * rv[i]; } } ruimulC() : n(0) { rv.resize(n + 1); rv[0] = 1; } void operator+=(T v) { rv.push_back(rv.back() * v); n++; } T operator()(int l, int r) { assert(l <= r && 0 <= l && r <= n); return rv[r] / rv[l]; } T operator()(int r = inf) { return operator()(0, min(r, n)); } T operator[](int r) { return operator()(0, r); } auto begin() { return rv.begin(); } auto end() { return rv.end(); } }; template <class T> ruimulC<T> ruimul(vector<T> &a) { return ruimulC<T>(a); } template <class T> ruimulC<T> ruimul() { vector<T> a; return ruimulC<T>(a); } /*@formatter:off*/ 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> void inc(pair<T, U> &a, U v = 1) { a.first += v, a.second += v; } template <class T, class U> void inc(T &a, U v = 1) { a += v; } template <class T, class U> void inc(vector<T> &a, U v = 1) { for (auto &u : a) inc(u, v); } template <class T, class U> void dec(T &a, U v = 1) { a -= v; } template <class T, class U> void dec(vector<T> &a, U v = 1) { for (auto &u : a) dec(u, v); } template <class U> void dec(string &a, U v = 1) { for (auto &u : a) dec(u, v); } template <class T, class U, class W> void dec(vector<T> &a, vector<U> &b, W v = 1) { for (auto &u : a) dec(u, v); for (auto &u : b) dec(u, v); } template <class T, class U, class W> void dec(vector<T> &a, vector<U> &b, vector<W> &c) { for (auto &u : a) dec(u, 1); for (auto &u : b) dec(u, 1); for (auto &u : c) dec(u, 1); } bool ins(ll h, ll w, ll H, ll W) { return h >= 0 && w >= 0 && h < H && w < W; } bool ins(ll l, ll v, ll r) { return l <= v && v < r; } template <class T> bool ins(vector<T> &a, ll i, ll j = 0) { return ins(0, i, sz(a)) && ins(0, j, sz(a)); } #define inside ins ll u(ll a) { return a < 0 ? 0 : a; } template <class T> vector<T> u(vector<T> &a) { vector<T> ret = a; fora(v, ret) v = u(v); return ret; } //添え字を返す template <class F> ll goldd_l(ll left, ll right, F calc) { double GRATIO = 1.6180339887498948482045868343656; ll lm = left + (ll)((right - left) / (GRATIO + 1.0)); ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); ll fl = calc(lm); ll fr = calc(rm); while (right - left > 10) { if (fl < fr) { right = rm; rm = lm; fr = fl; lm = left + (ll)((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } ll minScore = MAX(ll); ll resIndex = left; for (ll i = left; i < right + 1; ++i) { ll score = calc(i); if (minScore > score) { minScore = score; resIndex = i; } } return resIndex; } template <class F> ll goldt_l(ll left, ll right, F calc) { double GRATIO = 1.6180339887498948482045868343656; ll lm = left + (ll)((right - left) / (GRATIO + 1.0)); ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); ll fl = calc(lm); ll fr = calc(rm); while (right - left > 10) { if (fl > fr) { right = rm; rm = lm; fr = fl; lm = left + (ll)((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } if (left > right) { ll l = left; left = right; right = l; } ll maxScore = MIN(ll); ll resIndex = left; for (ll i = left; i < right + 1; ++i) { ll score = calc(i); if (maxScore < score) { maxScore = score; resIndex = i; } } return resIndex; } /*loopは200にすればおそらく大丈夫 余裕なら300に*/ template <class F> dou goldd_d(dou left, dou right, F calc, ll loop = 200) { dou GRATIO = 1.6180339887498948482045868343656; dou lm = left + ((right - left) / (GRATIO + 1.0)); dou rm = lm + ((right - lm) / (GRATIO + 1.0)); dou fl = calc(lm); dou fr = calc(rm); /*200にすればおそらく大丈夫*/ /*余裕なら300に*/ ll k = 141; loop++; while (--loop) { if (fl < fr) { right = rm; rm = lm; fr = fl; lm = left + ((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + ((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } return left; } template <class F> dou goldt_d(dou left, dou right, F calc, ll loop = 200) { double GRATIO = 1.6180339887498948482045868343656; dou lm = left + ((right - left) / (GRATIO + 1.0)); dou rm = lm + ((right - lm) / (GRATIO + 1.0)); dou fl = calc(lm); dou fr = calc(rm); loop++; while (--loop) { if (fl > fr) { right = rm; rm = lm; fr = fl; lm = left + ((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + ((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } return left; } // l ~ rを複数の区間に分割し、極致を与えるiを返す time-20 msまで探索 template <class F> ll goldd_ls(ll l, ll r, F calc, ll time = 2000) { auto lim = milliseconds(time - 20); ll mini = 0, minv = MAX(ll); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/ ll nl = l; ll nr = l + haba; rep(i, k) { ll ni = goldd_l(nl, nr, calc); if (chmi(minv, calc(ni))) mini = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return mini; } template <class F> ll goldt_ls(ll l, ll r, F calc, ll time = 2000) { auto lim = milliseconds(time - 20); ll maxi = 0, maxv = MIN(ll); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/ ll nl = l; ll nr = l + haba; rep(i, k) { ll ni = goldt_l(nl, nr, calc); if (chma(maxv, calc(ni))) maxi = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return maxi; } template <class F> dou goldd_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を持つ*/ auto lim = milliseconds(time - 20); dou mini = 0, minv = MAX(dou); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); dou haba = (r - l) / k; dou nl = l; dou nr = l + haba; rep(i, k) { dou ni = goldd_d(nl, nr, calc); if (chmi(minv, calc(ni))) mini = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return mini; } template <class F> dou goldt_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を残している*/ auto lim = milliseconds(time - 20); dou maxi = 0, maxv = MIN(dou); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); dou haba = (r - l) / k; dou nl = l; dou nr = l + haba; rep(i, k) { dou ni = goldt_d(nl, nr, calc); if (chma(maxv, calc(ni))) maxi = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return maxi; } template <class T> T min(vector<vector<T>> &a) { T res = MAX(T); rep(i, a.size()) chmi(res, *min_element(all(a[i]))); return res; } template <class T> T max(vector<vector<T>> &a) { T res = MIN(T); rep(i, a.size()) chma(res, *max_element(all(a[i]))); return res; } constexpr bool bget(ll m, ll keta) { #ifdef _DEBUG assert(keta <= 62); //オーバーフロー 1^62までしか扱えない #endif return (m >> keta) & 1; } // bget(n)次元 // NならN-1まで vector<vi> bget2(vi &a, int keta_size) { vvi(res, keta_size, sz(a)); rep(k, keta_size) { rep(i, sz(a)) { res[k][i] = bget(a[i], k); } } return res; } vi bget1(vi &a, int keta) { vi res(sz(a)); rep(i, sz(a)) { res[i] = bget(a[i], keta); } return res; } ll bget(ll m, ll keta, ll sinsuu) { m /= (ll)pow(sinsuu, keta); return m % sinsuu; } constexpr ll bit(ll n) { #ifdef _DEBUG assert(n <= 62); //オーバーフロー 1^62までしか扱えない #endif return (1LL << (n)); } ll bit(ll n, ll sinsuu) { return (ll)pow(sinsuu, n); } ll mask(ll n) { return (1ll << n) - 1; } // aをbitに置きなおす //{0, 2} -> 101 ll biti(vi &a) { int m = 0; for (auto &&v : a) m |= bit(v); return m; } //{1, 1, 0} -> 011 ll bitb(vi &a) { int m = 0; rep(i, sz(a)) if (a[i]) m |= bit(i); return m; } #define bcou __builtin_popcountll //最下位ビット ll lbit(ll n) { return n & -n; } ll lbiti(ll n) { return log2(n & -n); } //最上位ビット ll hbit(ll n) { n |= (n >> 1); n |= (n >> 2); n |= (n >> 4); n |= (n >> 8); n |= (n >> 16); n |= (n >> 32); return n - (n >> 1); } ll hbiti(ll n) { return log2(hbit(n)); } ll hbitk(ll n) { ll k = 0; rer(i, 5) { ll a = k + (1ll << i); ll b = 1ll << a; if (b <= n) k += 1ll << i; } return k; } //初期化は0を渡す ll nextComb(ll &mask, ll n, ll r) { if (!mask) return mask = (1LL << r) - 1; ll x = mask & -mask; /*最下位の1*/ ll y = mask + x; /*連続した下の1を繰り上がらせる*/ ll res = ((mask & ~y) / x >> 1) | y; if (bget(res, n)) return mask = 0; else return mask = res; } // n桁以下でビットがr個立っているもののvectorを返す vi bitCombList(ll n, ll r) { vi res; ll m = 0; while (nextComb(m, n, r)) { res.push_back(m); } return res; } // masの立ってるindexを見る #define forbit(i, mas) \ for (int forbitj = lbit(mas), forbitm = mas, i = log2(forbitj); forbitm; \ forbitm = forbitj ? forbitm ^ forbitj : 0, forbitj = lbit(forbitm), \ i = log2(forbitj)) char itoal(ll i) { return 'a' + i; } char itoaL(ll i) { return 'A' + i; } ll altoi(char c) { if ('A' <= c && c <= 'Z') return c - 'A'; return c - 'a'; } ll ctoi(char c) { return c - '0'; } char itoc(ll i) { return i + '0'; } ll vtoi(vi &v) { ll res = 0; if (sz(v) > 18) { debugline("vtoi"); deb(sz(v)); ole(); } rep(i, sz(v)) { res *= 10; res += v[i]; } return res; } vi itov(ll i) { vi res; while (i) { res.push_back(i % 10); i /= 10; } rev(res); return res; } vi stov(string &a) { ll n = sz(a); vi ret(n); rep(i, n) { ret[i] = a[i] - '0'; } return ret; } //基準を満たさないものは0になる vi stov(string &a, char one) { ll n = sz(a); vi ret(n); rep(i, n) ret[i] = a[i] == one; return ret; } vector<vector<ll>> ctoi(vector<vector<char>> s, char c) { ll n = sz(s), m = sz(s[0]); vector<vector<ll>> res(n, vector<ll>(m)); rep(i, n) rep(j, m) res[i][j] = s[i][j] == c; return res; } #define unique(v) v.erase(unique(v.begin(), v.end()), v.end()); //[i] := vを返す // aは0~n-1で置き換えられる vi compress(vi &a) { vi b; ll len = a.size(); for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { a[i] = lower_bound(all(b), a[i]) - b.begin(); } ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, umap<ll, ll> &map) { vi b; ll len = a.size(); for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { ll v = a[i]; a[i] = lower_bound(all(b), a[i]) - b.begin(); map[v] = a[i]; } ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, vi &r) { vi b; ll len = a.size(); fora(v, a) b.push_back(v); fora(v, r) b.push_back(v); sort(b); unique(b); for (ll i = 0; i < len; ++i) a[i] = lower_bound(all(b), a[i]) - b.begin(); for (ll i = 0; i < sz(r); ++i) r[i] = lower_bound(all(b), r[i]) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, vi &r, vi &s) { vi b; ll len = a.size(); fora(v, a) b.push_back(v); fora(v, r) b.push_back(v); fora(v, s) b.push_back(v); sort(b); unique(b); for (ll i = 0; i < len; ++i) a[i] = lower_bound(all(b), a[i]) - b.begin(); for (ll i = 0; i < sz(r); ++i) r[i] = lower_bound(all(b), r[i]) - b.begin(); for (ll i = 0; i < sz(s); ++i) r[i] = lower_bound(all(b), s[i]) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vector<vi> &a) { vi b; fora(vv, a) fora(v, vv) b.push_back(v); sort(b); unique(b); fora(vv, a) fora(v, vv) v = lower_bound(all(b), v) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vector<vector<vi>> &a) { vi b; fora(vvv, a) fora(vv, vvv) fora(v, vv) b.push_back(v); sort(b); unique(b); fora(vvv, a) fora(vv, vvv) fora(v, vv) v = lower_bound(all(b), v) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } void compress(ll a[], ll len) { vi b; for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { a[i] = lower_bound(all(b), a[i]) - b.begin(); } } //要素が見つからなかったときに困る #define binarySearch(a, v) (binary_search(all(a), v)) #define lowerIndex(a, v) (lower_bound(all(a), v) - a.begin()) #define upperIndex(a, v) (upper_bound(all(a), v) - a.begin()) #define rlowerIndex(a, v) (upper_bound(all(a), v) - a.begin() - 1) #define rupperIndex(a, v) (lower_bound(all(a), v) - a.begin() - 1) template <class T, class U, class W> T lowerBound(vector<T> &a, U v, W banpei) { auto it = lower_bound(a.begin(), a.end(), v); if (it == a.end()) return banpei; else return *it; } template <class T, class U, class W> T lowerBound(ruiC<T> &a, U v, W banpei) { return lowerBound(a.rui, v, banpei); } template <class T, class U, class W> T upperBound(vector<T> &a, U v, W banpei) { auto it = upper_bound(a.begin(), a.end(), v); if (it == a.end()) return banpei; else return *it; } template <class T, class U, class W> T upperBound(ruiC<T> &a, U v, W banpei) { return upperBound(a.rui, v, banpei); } template <class T, class U, class W> T rlowerBound(vector<T> &a, U v, W banpei) { auto it = upper_bound(a.begin(), a.end(), v); if (it == a.begin()) return banpei; else { return *(--it); } } template <class T, class U, class W> T rlowerBound(ruiC<T> &a, U v, W banpei) { return rlowerBound(a.rui, v, banpei); } template <class T, class U, class W> T rupperBound(vector<T> &a, U v, W banpei) { auto it = lower_bound(a.begin(), a.end(), v); if (it == a.begin()) return banpei; else { return *(--it); } } template <class T, class U, class W> T rupperBound(ruiC<T> &a, U v, W banpei) { return rupperBound(a.rui, v, banpei); } #define next2(a) next(next(a)) #define prev2(a) prev(prev(a)) //狭義の単調増加列 長さを返す template <class T> int lis(vector<T> &a) { int n = sz(a); vi tail(n + 1, MAX(T)); rep(i, n) { int id = lowerIndex(tail, a[i]); /**/ tail[id] = a[i]; } return lowerIndex(tail, MAX(T)); } template <class T> int lis_eq(vector<T> &a) { int n = sz(a); vi tail(n + 1, MAX(T)); rep(i, n) { int id = upperIndex(tail, a[i]); /**/ tail[id] = a[i]; } return lowerIndex(tail, MAX(T)); } // iteratorを返す // valueが1以上の物を返す 0は見つけ次第削除 // vを減らす場合 (*it).se--でいい template <class T, class U, class V> auto lower_map(map<T, U> &m, V k) { auto ret = m.lower_bound(k); while (ret != m.end() && (*ret).second == 0) { ret = m.erase(ret); } return ret; } template <class T, class U, class V> auto upper_map(map<T, U> &m, V k) { auto ret = m.upper_bound(k); while (ret != m.end() && (*ret).second == 0) { ret = m.erase(ret); } return ret; } //存在しなければエラー template <class T, class U, class V> auto rlower_map(map<T, U> &m, V k) { auto ret = upper_map(m, k); assert(ret != m.begin()); ret--; while (1) { if ((*ret).second != 0) break; assert(ret != m.begin()); auto next = ret; --next; m.erase(ret); ret = next; } return ret; } template <class T, class U, class V> auto rupper_map(map<T, U> &m, V k) { auto ret = lower_map(m, k); assert(ret != m.begin()); ret--; while (1) { if ((*ret).second != 0) break; assert(ret != m.begin()); auto next = ret; --next; m.erase(ret); ret = next; } return ret; } template <class... T> void fin(T... s) { out(s...); exit(0); } //便利 数学 math // sub ⊂ top bool subset(int sub, int top) { return (sub & top) == sub; } //-180 ~ 180 degree double atand(double h, double w) { return atan2(h, w) / PI * 180; } //% -mの場合、最小の正の数を返す ll mod(ll a, ll m) { if (m < 0) m *= -1; return (a % m + m) % m; } ll pow(ll a) { return a * a; }; ll fact(ll v) { return v <= 1 ? 1 : v * fact(v - 1); } dou factd(int v) { static vd fact(2, 1); if (sz(fact) <= v) { rep(i, sz(fact), v + 1) { fact.push_back(fact.back() * i); } } return fact[v]; } ll comi(ll n, ll r) { assert(n < 100); static vvi(pas, 100, 100); if (pas[0][0]) return pas[n][r]; pas[0][0] = 1; rep(i, 1, 100) { pas[i][0] = 1; rep(j, 1, i + 1) pas[i][j] = pas[i - 1][j - 1] + pas[i - 1][j]; } return pas[n][r]; } double comd2(ll n, ll r) { static vvd(comb, 2020, 2020); if (comb[0][0] == 0) { comb[0][0] = 1; rep(i, 2000) { comb[i + 1][0] = 1; rep(j, 1, i + 2) { comb[i + 1][j] = comb[i][j] + comb[i][j - 1]; } } } return comb[n][r]; } double comd(int n, int r) { if (r < 0 || r > n) return 0; if (n < 2020) return comd2(n, r); static vd fact(2, 1); if (sz(fact) <= n) { rep(i, sz(fact), n + 1) { fact.push_back(fact.back() * i); } } return fact[n] / fact[n - r] / fact[r]; } ll gcd(ll a, ll b) { while (b) a %= b, swap(a, b); return abs(a); } ll gcd(vi b) { ll res = b[0]; rep(i, 1, sz(b)) res = gcd(b[i], res); return res; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll lcm(vi a) { ll res = a[0]; rep(i, 1, sz(a)) res = lcm(a[i], res); return res; } ll ceil(ll a, ll b) { if (b == 0) { debugline("ceil"); deb(a, b); ole(); return -1; } else if (a < 0) { return 0; } else { return (a + b - 1) / b; } } ll sig0(ll t) { return t <= 0 ? 0 : ((1 + t) * t) >> 1; } // ll sig(ll s, ll t) { return ((s + t) * (t - s + 1)) >> 1; } ll sig(ll s, ll t) { if (s > t) swap(s, t); return sig0(t - s) + s * (t - s + 1); } #define tousa_i tosa_i #define lower_tousa_i lower_tosa_i #define upper_tousa upper_tosa #define upper_tousa_i upper_tosa_i ll tosa_i(ll st, ll ad, ll v) { assert(((v - st) % ad) == 0); return (v - st) / ad; } ll tosa_s(ll st, ll ad, ll len) { return st * len + sig0(len - 1) * ad; } // ax + r (x は非負整数) で表せる整数のうち、v 以上となる最小の整数 ll lower_tosa(ll st, ll ad, ll v) { if (st >= v) return st; return (v - st + ad - 1) / ad * ad + st; } //第何項か ll lower_tosa_i(ll st, ll ad, ll v) { if (st >= v) return 0; return (v - st + ad - 1) / ad; } ll upper_tosa(ll st, ll ad, ll v) { return lower_tosa(st, ad, v + 1); } ll upper_tosa_i(ll st, ll ad, ll v) { return lower_tosa_i(st, ad, v + 1); } // v * v >= aとなる最小のvを返す 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); } /*@formatter:off*/ //機能拡張 #define dtie(a, b) \ int a, b; \ tie(a, b) template <class T, class U> string to_string(T a, U b) { string res = ""; res += a; res += b; return res; } template <class T, class U, class V> string to_string(T a, U b, V c) { string res = ""; res += a; res += b; res += c; return res; } template <class T, class U, class V, class W> string to_string(T a, U b, V c, W d) { string res = ""; res += a; res += b; res += c; res += d; return res; } template <class T, class U, class V, class W, class X> string to_string(T a, U b, V c, W d, X e) { string res = ""; res += a; res += b; res += c; res += d; res += e; return res; } constexpr int bsetlen = k5 * 2; // constexpr int bsetlen = 5050; #define bset bitset<bsetlen> bool operator<(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] < b[i]) return true; if (a[i] > b[i]) return false; } return false; } bool operator>(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] > b[i]) return true; if (a[i] < b[i]) return false; } return false; } bool operator<=(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] < b[i]) return true; if (a[i] > b[i]) return false; } return true; } bool operator>=(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] > b[i]) return true; if (a[i] < b[i]) return false; } return true; } string operator~(string &a) { string res = a; for (auto &&c : res) { if (c == '0') c = '1'; else if (c == '1') c = '0'; else { cerr << "cant ~" << a << "must bit" << endl; exit(0); } } return res; } ostream &operator<<(ostream &os, bset &a) { bitset<10> b; vi list; rep(i, bsetlen) { if (a[i]) list.push_back(i), b[i] = 1; } os << b << ", " << list; return os; } int hbiti(bset &a) { rer(i, bsetlen) { if (a[i]) return i; } return -1; } #define hk(a, b, c) (a <= b && b < c) // O(N/64) bset nap(bset &a, int v) { bset r = a | a << v; return r; } bset nap(bset &a, bset &v) { bset r = a; rep(i, bsetlen) { if (v[i]) r |= a << i; } return r; } template <class T> void seth(vector<vector<T>> &S, int w, vector<T> &v) { assert(sz(S) == sz(v)); assert(w < sz(S[0])); rep(h, sz(S)) { S[h][w] = v[h]; } } template <class T, class U> void operator+=(pair<T, U> &a, pair<T, U> &b) { a.fi += b.fi; a.se += b.se; } template <class T, class U> pair<T, U> operator+(pair<T, U> &a, pair<T, U> &b) { return pair<T, U>(a.fi + b.fi, a.se + b.se); } template <typename CharT, typename Traits, typename Alloc> basic_string<CharT, Traits, Alloc> operator+(const basic_string<CharT, Traits, Alloc> &lhs, const int rv) { basic_string<CharT, Traits, Alloc> str(lhs); str.append(to_string(rv)); return str; } template <typename CharT, typename Traits, typename Alloc> void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const int rv) { lhs += to_string(rv); } template <typename CharT, typename Traits, typename Alloc> basic_string<CharT, Traits, Alloc> operator+(const basic_string<CharT, Traits, Alloc> &lhs, const signed rv) { basic_string<CharT, Traits, Alloc> str(lhs); str.append(to_string(rv)); return str; } template <typename CharT, typename Traits, typename Alloc> void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const signed rv) { lhs += to_string(rv); } template <typename CharT, typename Traits, typename Alloc> void operator*=(basic_string<CharT, Traits, Alloc> &s, int num) { auto bek = s; s = ""; for (; num; num >>= 1) { if (num & 1) { s += bek; } bek += bek; } } template <class T, class U> void operator+=(queue<T> &a, U v) { a.push(v); } template <class T, class U> void operator+=(deque<T> &a, U v) { a.push_back(v); } template <class T> priority_queue<T, vector<T>, greater<T>> & operator+=(priority_queue<T, vector<T>, greater<T>> &a, vector<T> &v) { fora(d, v) a.push(d); return a; } template <class T, class U> priority_queue<T, vector<T>, greater<T>> & operator+=(priority_queue<T, vector<T>, greater<T>> &a, U v) { a.push(v); return a; } template <class T, class U> priority_queue<T> &operator+=(priority_queue<T> &a, U v) { a.push(v); return a; } template <class T> set<T> &operator+=(set<T> &a, vector<T> v) { fora(d, v) a.insert(d); return a; } template <class T, class U> auto operator+=(set<T> &a, U v) { return a.insert(v); } template <class T, class U> auto operator-=(set<T> &a, U v) { return a.erase(v); } template <class T, class U> auto operator+=(mset<T> &a, U v) { return a.insert(v); } template <class T, class U> set<T, greater<T>> &operator+=(set<T, greater<T>> &a, U v) { a.insert(v); return a; } template <class T, class U> vector<T> &operator+=(vector<T> &a, U v) { a.push_back(v); return a; } template <class T, class U> vector<T> operator+(vector<T> &a, U v) { vector<T> ret = a; ret += v; return ret; } template <class T, class U> vector<T> operator+(U v, vector<T> &a) { vector<T> ret = a; ret.insert(ret.begin(), v); return ret; } template <class T> vector<T> operator+(vector<T> a, vector<T> b) { vector<T> ret; ret = a; fora(v, b) ret += v; return ret; } template <class T> vector<T> &operator+=(vector<T> &a, vector<T> &b) { rep(i, sz(b)) { /*こうしないとa+=aで両辺が増え続けてバグる*/ a.push_back(b[i]); } return a; } template <class T, class U> map<T, U> &operator+=(map<T, U> &a, map<T, U> &b) { fora(bv, b) { a[bv.first] += bv.second; } return a; } template <class T> vector<T> &operator-=(vector<T> &a, vector<T> &b) { if (sz(a) != sz(b)) { debugline("vector<T> operator-="); deb(a); deb(b); exit(0); } rep(i, sz(a)) a[i] -= b[i]; return a; } template <class T> vector<T> operator-(vector<T> &a, vector<T> &b) { if (sz(a) != sz(b)) { debugline("vector<T> operator-"); deb(a); deb(b); ole(); } vector<T> res(sz(a)); rep(i, sz(a)) res[i] = a[i] - b[i]; return res; } template <class T, class U> void operator*=(vector<T> &a, U b) { vector<T> ta = a; rep(b - 1) { a += ta; } } template <typename T> void erase(vector<T> &v, unsigned ll i) { v.erase(v.begin() + i); } template <typename T> void erase(vector<T> &v, unsigned ll s, unsigned ll e) { v.erase(v.begin() + s, v.begin() + e); } template <class T, class U> void erase(map<T, U> &m, ll okl, ll ngr) { m.erase(m.lower_bound(okl), m.lower_bound(ngr)); } template <class T> void erase(set<T> &m, ll okl, ll ngr) { m.erase(m.lower_bound(okl), m.lower_bound(ngr)); } template <typename T> void erasen(vector<T> &v, unsigned ll s, unsigned ll n) { v.erase(v.begin() + s, v.begin() + s + n); } template <typename T, typename U> void insert(vector<T> &v, unsigned ll i, U t) { v.insert(v.begin() + i, t); } template <typename T, typename U> void push_front(vector<T> &v, U t) { v.insert(v.begin(), t); } template <typename T, typename U> void insert(vector<T> &v, unsigned ll i, vector<T> list) { for (auto &&va : list) v.insert(v.begin() + i++, va); } template <typename T> void insert(set<T> &v, vector<T> list) { for (auto &&va : list) v.insert(va); } vector<string> split(const string a, const char deli) { string b = a + deli; ll l = 0, r = 0, n = b.size(); vector<string> res; rep(i, n) { if (b[i] == deli) { r = i; if (l < r) res.push_back(b.substr(l, r - l)); l = i + 1; } } return res; } vector<string> split(const string a, const string deli) { vector<string> res; ll kn = sz(deli); std::string::size_type Pos(a.find(deli)); ll l = 0; while (Pos != std::string::npos) { if (Pos - l) res.push_back(a.substr(l, Pos - l)); l = Pos + kn; Pos = a.find(deli, Pos + kn); } if (sz(a) - l) res.push_back(a.substr(l, sz(a) - l)); return res; } 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 fAb(bool a) { if (a) cout << "Alice" << endl; else cout << "Bob"; } 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 <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)}; } //未分類 // 0,2,1 1番目と2番目の次元を入れ替える template <class T> auto irekae(vector<vector<vector<T>>> &A, int x, int y, int z) { #define irekae_resize_loop(a, b, c) \ resize(res, a, b, c); \ rep(i, a) rep(j, b) rep(k, c) vector<vector<vector<T>>> res; if (x == 0 && y == 1 && z == 2) { res = A; } else if (x == 0 && y == 2 && z == 1) { irekae_resize_loop(sz(A), sz(A[0][0]), sz(A[0])) { res[i][j][k] = A[i][k][j]; } } else if (x == 1 && y == 0 && z == 2) { irekae_resize_loop(sz(A[0]), sz(A), sz(A[0][0])) { res[i][j][k] = A[j][i][k]; } } else if (x == 1 && y == 2 && z == 0) { irekae_resize_loop(sz(A[0]), sz(A[0][0]), sz(A)) { res[i][j][k] = A[k][i][j]; } } else if (x == 2 && y == 0 && z == 1) { irekae_resize_loop(sz(A[0][0]), sz(A), sz(A[0])) { res[i][j][k] = A[j][k][i]; } } else if (x == 2 && y == 1 && z == 0) { irekae_resize_loop(sz(A[0][0]), sz(A[0]), sz(A)) { res[i][j][k] = A[k][j][i]; } } return res; #undef irekae_resize_loop } template <class T> auto irekae(vector<vector<T>> &A, int i = 1, int j = 0) { vvt(res, sz(A[0]), sz(A)); rep(i, sz(A)) { rep(j, sz(A[0])) { res[j][i] = A[i][j]; } } return res; } // tou分割する template <typename T> vector<vector<T>> cut(vector<T> &a, int tou = 2) { int N = sz(a); vector<vector<T>> res(tou); int hab = N / tou; vi lens(tou, hab); rep(i, N % tou) { lens[tou - 1 - i]++; } int l = 0; rep(i, tou) { int len = lens[i]; int r = l + len; res[i].resize(len); std::copy(a.begin() + l, a.begin() + r, res[i].begin()); l = r; } return res; } //長さn毎に分割する template <typename T> vector<vector<T>> cutn(vector<T> &a, int len) { int N = sz(a); vector<vector<T>> res(ceil(N, len)); vi lens(N / len, len); if (N % len) lens.push_back(N % len); int l = 0; rep(i, sz(lens)) { int len = lens[i]; int r = l + len; res[i].resize(len); std::copy(a.begin() + l, a.begin() + r, res[i].begin()); l = r; } return res; } vi inds_(vi &a) { int n = sz(a); vb was(n); vi res(n); rep(i, n) { assert(!was[a[i]]); res[a[i]] = i; was[a[i]] = true; } return res; } //@起動時 struct initon { initon() { cin.tie(0); ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(16); srand((unsigned)clock() + (unsigned)time(NULL)); }; } initonv; #define pre prev #define nex next // gra mll pr //上下左右 const string udlr = "udlr"; string UDLR = "UDLR"; // x4と連動 UDLR.find('U') := x4[0] //右、上が正 constexpr ll h4[] = {1, -1, 0, 0}; constexpr ll w4[] = {0, 0, -1, 1}; constexpr ll h8[] = {0, 1, 0, -1, -1, 1, 1, -1}; constexpr ll w8[] = {1, 0, -1, 0, 1, -1, 1, -1}; int mei_inc(int h, int w, int H, int W, int i) { while (++i < 4) { if (inside(h + h4[i], w + w4[i], H, W)) return i; } return i; } #define mei(nh, nw, h, w) \ for (int i = mei_inc(h, w, H, W, -1), nh = i < 4 ? h + h4[i] : 0, \ nw = i < 4 ? w + w4[i] : 0; \ i < 4; i = mei_inc(h, w, H, W, i), nh = h + h4[i], nw = w + w4[i]) //グラフ内で #undef getid //#define getidとしているため、ここを書き直したらgraphも書き直す #define getid_2(h, w) (h * W + w) #define getid_1(p) (p.first * W + p.second) #define getid(...) over2(__VA_ARGS__, getid_2, getid_1)(__VA_ARGS__) #define getp(id) mp(id / W, id % W) //#define set_shuffle() std::random_device seed_gen;std::mt19937 //engine(seed_gen()) #define shuffle(a) std::shuffle((a).begin(), (a).end(), //engine); 1980 開始からtime ms経っていたらtrue bool timeup(int time) { auto end_time = system_clock::now(); auto part = duration_cast<milliseconds>(end_time - start_time); auto lim = milliseconds(time); return part >= lim; } void set_time() { past_time = system_clock::now(); } // MS型(millisecqnds)で返る // set_timeをしてからの時間 auto calc_time_milli() { auto now = system_clock::now(); auto part = duration_cast<milliseconds>(now - past_time); return part; } auto calc_time_micro() { auto now = system_clock::now(); auto part = duration_cast<microseconds>(now - past_time); return part; } auto calc_time_nano() { auto now = system_clock::now(); auto part = duration_cast<nanoseconds>(now - past_time); return part; } bool calc_time(int zikan) { return calc_time_micro() >= microseconds(zikan); } using MS = std::chrono::microseconds; int div(microseconds a, microseconds b) { return a / b; } int div(nanoseconds a, nanoseconds b) { if (b < nanoseconds(1)) { return a / nanoseconds(1); } int v = a / b; return v; } // set_time(); // rep(i,lim)shori // lim*=time_nanbai(); // rep(i,lim)shoriと使う //全体でmilliかかっていいときにlimを何倍してもう一回できるかを返す int time_nanbai(int milli) { auto dec = duration_cast<nanoseconds>(past_time - start_time); auto part = calc_time_nano(); auto can_time = nanoseconds(milli * 1000 * 1000); can_time -= part; can_time -= dec; return div(can_time, part); } /*@formatter:on*/ // vectorで取れる要素数 // bool=> 1e9 * 8.32 // int => 1e8 * 2.6 // ll => 1e8 * 1.3 // 3次元以上取るとメモリがヤバい // static配列を使う vvc(ba); ll N, M, H, W; vi A, B, C; //遅い 文字列以外も扱えるはず template <class T = string> struct rolling_hash2 { //@formatter:off const int b[2] = {11731, 11839}; const int m[2] = {1000002571LL, 1000002631LL}; int n; T s; // stringやvector vector<long long> hash[2], pow[2]; rolling_hash2() {} rolling_hash2(T &S) : s(S) { n = (int)S.size(); for (int i = 0; i < 2; ++i) { hash[i].assign(n + 1, 0); pow[i].assign(n + 1, 1); for (int j = 0; j < n; ++j) { hash[i][j + 1] = (hash[i][j] * b[i] + S[j]) % m[i]; pow[i][j + 1] = pow[i][j] * b[i] % m[i]; } } } // get hash of S[left:right] ll get(int l, int r, int id) { ll res = hash[id][r] - hash[id][l] * pow[id][r - l] % m[id]; if (res < 0) res += m[id]; return res; } ll get(int l, int r) { return (get(l, r, 0) << 32) | get(l, r, 1); } ll operator()(int l, int r) { return get(l, r, 0); } ll operator()() { return get(0, n, 0); } // get lcp of S[a:] and S[b:] int lcp(int a, int b) { int len = min((int)s.size() - a, (int)s.size() - b); int low = -1, high = len + 1; while (high - low > 1) { int mid = (low + high) / 2; if (get(a, a + mid, 0) != get(b, b + mid, 0)) high = mid; else if (get(a, a + mid, 1) != get(b, b + mid, 1)) high = mid; else low = mid; } return low; } // get lcp of S[a:] and T[b:] int lcp(rolling_hash2 &t, int a, int b) { int len = min((int)s.size() - a, (int)s.size() - b); int low = -1, high = len + 1; while (high - low > 1) { int mid = (low + high) / 2; if (get(a, a + mid, 0) != get(b, b + mid, 0)) high = mid; else if (get(a, a + mid, 1) != get(b, b + mid, 1)) high = mid; else low = mid; } return low; } }; //早い struct rolling_hash { static constexpr int base1 = 1007, base2 = 2009; static constexpr int mod1 = 1000000007, mod2 = 1000000009; vector<long long> hash1, hash2, power1, power2; int n; rolling_hash(string &S) { n = (int)S.size(); hash1.assign(n + 1, 0); hash2.assign(n + 1, 0); power1.assign(n + 1, 1); power2.assign(n + 1, 1); for (int i = 0; i < n; ++i) { hash1[i + 1] = (hash1[i] * base1 + S[i]) % mod1; hash2[i + 1] = (hash2[i] * base2 + S[i]) % mod2; power1[i + 1] = (power1[i] * base1) % mod1; power2[i + 1] = (power2[i] * base2) % mod2; } } template <class T> rolling_hash(vector<T> &S) { n = (int)S.size(); hash1.assign(n + 1, 0); hash2.assign(n + 1, 0); power1.assign(n + 1, 1); power2.assign(n + 1, 1); for (int i = 0; i < n; ++i) { hash1[i + 1] = (hash1[i] * base1 + S[i]) % mod1; hash2[i + 1] = (hash2[i] * base2 + S[i]) % mod2; power1[i + 1] = (power1[i] * base1) % mod1; power2[i + 1] = (power2[i] * base2) % mod2; } } inline pair<int, int> get(int l, int r) { long long res1 = hash1[r] - hash1[l] * power1[r - l] % mod1; if (res1 < 0) res1 += mod1; long long res2 = hash2[r] - hash2[l] * power2[r - l] % mod2; if (res2 < 0) res2 += mod2; return {res1, res2}; } inline pair<int, int> operator()(int l, int r) { return get(l, r); } inline pair<int, int> operator()() { return get(0, n); } // get lcp of S[a:] and T[b:] //接頭語がどれだけ一致するか //範囲の重複を含む inline int lcp_dup(int a, int b) { int len = min((int)hash1.size() - a, (int)hash1.size() - b); int low = 0, high = len; while (high - low > 1) { int mid = (low + high) >> 1; if (get(a, a + mid) != get(b, b + mid)) high = mid; else low = mid; } return low; } //重複を認めない inline int lcp_not(int a, int b) { return min(lcp_dup(a, b), b - a); } }; // hashを共有したくない場合、中に書く const uint64_t base = rand((uint64_t)868491463, ((uint64_t)1 << 61) - 1 - 1); //安全で爆速なrollinghash class roll { static constexpr uint64_t mask30 = ((uint64_t)1 << 30) - 1; static constexpr uint64_t mask31 = ((uint64_t)1 << 31) - 1; static constexpr uint64_t mask61 = ((uint64_t)1 << 61) - 1; static constexpr uint64_t offset = mask61 * 7; static constexpr uint64_t mod(const uint64_t x) { const uint64_t y = (x >> 61) + (x & mask61); return y >= mask61 ? y - mask61 : y; } static constexpr uint64_t mul(const uint64_t x, const uint64_t y) { const uint64_t xh = x >> 31, xl = x & mask31; const uint64_t yh = y >> 31, yl = y & mask31; const uint64_t z = xl * yh + xh * yl; const uint64_t zh = z >> 30, zl = z & mask30; return xh * yh * 2 + zh + (zl << 31) + xl * yl; } const std::size_t siz; vector<uint64_t> pow, h; public: template <typename T> roll(T &A) : siz(sz(A)), pow(siz + 1, 1), h(siz + 1, 0) { for (std::size_t i = 1; i <= siz; i++) { pow[i] = mod(mul(pow[i - 1], base)), h[i] = mod(mul(h[i - 1], base) + A[i - 1]); } } uint64_t operator()(const std::size_t l, const std::size_t r) { return mod(h[r] + offset - mul(h[l], pow[r - l])); } uint64_t operator()(int r = inf) { return operator()(0, min(siz, r)); } // get lcp of S[a:] and T[b:] //接頭語がどれだけ一致するか //範囲の重複を含む int lcp_dup(int a, int b) { int len = min((int)siz - a, (int)siz - b); int low = 0, high = len; while (high - low > 1) { int mid = (low + high) >> 1; if (operator()(a, a + mid) != operator()(b, b + mid)) high = mid; else low = mid; } return low; } //重複を認めない int lcp_not(int a, int b) { return min(lcp_dup(a, b), b - a); } //@formatter:on }; //動かない場合roll2 // roll ro(list); void solve() { cin >> N; vi A(N), B(N), A2(N), B2(N); rep(i, 0, N) cin >> A[i]; rep(i, 0, N) cin >> B[i]; rep(i, 0, N) A2[i] = A[i] ^ A[(i + 1) % N]; rep(i, 0, N) B2[i] = B[i] ^ B[(i + 1) % N]; vector<int> v; rep(i, 0, N) v.push_back(A2[i]); rep(i, 0, N) v.push_back(A2[i]); rep(i, 0, N) v.push_back(B2[i]); roll rh(v); rep(k, 0, N) if (rh(k, k + N - 2) == rh(N * 2, N * 2 + N - 2)) { int x = A[k] ^ B[0]; out(k, x); } } auto my(ll n, vi &a) { return 0; } auto sister(ll n, vi &a) { ll ret = 0; return ret; } signed main() { solve(); #define arg n, a #ifdef _DEBUG bool bad = 0; for (ll i = 0, ok = 1; i < k5 && ok; ++i) { ll n = rand(1, 8); vi a = ranv(n, 1, 10); auto myres = my(arg); auto res = sister(arg); ok = myres == res; if (!ok) { out(arg); cerr << "AC : " << res << endl; cerr << "MY : " << myres << endl; bad = 1; break; } } if (!bad) { // cout << "完璧 : solveを書き直そう" << endl; // cout << " : そして、solve()を呼び出すのだ" << endl; // cout << " : cin>>n; na(a,n);も忘れるな" << endl; } #endif return 0; };
//#undef _DEBUG //#pragma GCC optimize("Ofast") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace std::chrono; #define int long long // todo 消したら動かない intの代わりにsignedを使う #define ll long long auto start_time = system_clock::now(); auto past_time = system_clock::now(); #define debugName(VariableName) #VariableName //最大引数がN #define over2(o1, o2, name, ...) name #define over3(o1, o2, o3, name, ...) name #define over4(o1, o2, o3, o4, name, ...) name #define over5(o1, o2, o3, o4, o5, name, ...) name #define over6(o1, o2, o3, o4, o5, o6, name, ...) name #define over7(o1, o2, o3, o4, o5, o6, o7, name, ...) name #define over8(o1, o2, o3, o4, o5, o6, o7, o8, name, ...) name #define over9(o1, o2, o3, o4, o5, o6, o7, o8, o9, name, ...) name #define over10(o1, o2, o3, o4, o5, o6, o7, o8, o9, o10, name, ...) name /*@formatter:off*/ //[-n, n)にアクセスできる //また、外部関数resizeに渡せる // sizeは[-n, n)でnを返す template <class T> class mvec { vector<T> v; int n; public: mvec() : n(0), v(0) {} mvec(int n) : n(n), v(n * 2) {} mvec(int n, T val) : n(n), v(n * 2, val) {} auto &operator[](int i) { return v[i + n]; } auto size() { return n; } void resize(int sn) { assert(n == 0); n = sn; v.resize(sn * 2); } auto begin() { return v.begin(); } auto rbegin() { return v.rbegin(); } auto end() { return v.end(); } auto rend() { return v.rend(); } }; //[]でboolは参照を返さないため特殊化が必要 template <> struct mvec<bool> { vector<bool> v; int n; mvec() : n(0), v(0) {} mvec(int n) : n(n), v(n * 2) {} mvec(int n, bool val) : n(n), v(n * 2, val) {} auto operator[](int i) { return v[i + n]; } auto size() { return v.size(); } void resize(int sn) { assert(n == 0); n = sn; v.resize(sn * 2); } auto begin() { return v.begin(); } auto rbegin() { return v.rbegin(); } auto end() { return v.end(); } auto rend() { return v.rend(); } }; template <class T> ostream &operator<<(ostream &os, mvec<T> &a) { int spa = 3; for (auto &&v : a) { spa = max(spa, (int)(to_string(v).size()) + 1); } int n = (int)a.size(); os << endl; for (int i = -n; i < n; i++) { int need = spa - ((int)to_string(i).size()); if (i == -n) { need -= min(need, spa - ((int)to_string(a[i]).size())); } while (need--) { os << " "; } os << i; } os << endl; int i = -n; for (auto &&v : a) { int need = spa - ((int)to_string(v).size()); if (i == -n) { need -= min(need, spa - ((int)to_string(i).size())); } while (need--) { os << " "; } os << v; i++; } return os; } #define mv mvec #define MV mvec using mvi = mvec<ll>; using mvb = mvec<bool>; using mvs = mvec<string>; using mvd = mvec<double>; using mvc = mvec<char>; #define mvvt0(t) mvec<mvec<t>> #define mvvt1(t, a) mvec<mvec<t>> a #define mvvt2(t, a, b) mvec<mvec<t>> a(b) #define mvvt3(t, a, b, c) mvec<mvec<t>> a(b, mvec<t>(c)) #define mvvt4(t, a, b, c, d) mvec<mvec<t>> a(b, mvec<t>(c, d)) #define mvvi(...) \ over4(__VA_ARGS__, mvvt4, mvvt3, mvvt2, mvvt1, mvvt0)(ll, __VA_ARGS__) template <typename T> mvec<T> make_mv(size_t a) { return mvec<T>(a); } template <typename T, typename... Ts> auto make_mv(size_t a, Ts... ts) { return mvec<decltype(make_mv<T>(ts...))>(a, make_mv<T>(ts...)); } #define mvni(name, ...) auto name = make_mv<ll>(__VA_ARGS__) #ifdef _DEBUG // https://marycore.jp/prog/cpp/class-extension-methods/ 違うかも template <class T, class A = std::allocator<T>> struct debtor : std::vector<T, A> { using std::vector<T, A>::vector; template <class U> int deb_v(U a, int v) { return v; } template <class U> int deb_v(debtor<U> &a, int v = 0) { cerr << a.size() << " "; return deb_v(a.at(0), v + 1); } template <class U> void deb_o(U a) { cerr << a << " "; } template <class U> void deb_o(debtor<U> &a) { for (int i = 0; i < min((int)a.size(), 15ll); i++) { deb_o(a[i]); } if ((int)a.size() > 15) { cerr << "..."; } cerr << endl; } typename std::vector<T>::reference operator[](typename std::vector<T>::size_type n) { if (n < 0 || n >= (int)this->size()) { int siz = (int)this->size(); cerr << "vector size = "; int dim = deb_v((*this)); cerr << endl; cerr << "out index at " << n << endl; cerr << endl; if (dim <= 2) { deb_o((*this)); } exit(0); } return this->at(n); } }; #define vector debtor #endif #ifdef _DEBUG //区間削除は出来ない template <class T> struct my_pbds_tree { set<T> s; auto begin() { return s.begin(); } auto end() { return s.end(); } auto rbegin() { return s.rbegin(); } auto rend() { return s.rend(); } auto empty() { return s.empty(); } auto size() { return s.size(); } void clear() { s.clear(); } template <class U> void insert(U v) { s.insert(v); } template <class U> void operator+=(U v) { insert(v); } template <class F> auto erase(F v) { return s.erase(v); } template <class U> auto find(U v) { return s.find(v); } template <class U> auto lower_bound(U v) { return s.lower_bound(v); } template <class U> auto upper_bound(U v) { return s.upper_bound(v); } auto find_by_order(ll k) { auto it = s.begin(); for (ll i = 0; i < k; i++) it++; return it; } auto order_of_key(ll v) { auto it = s.begin(); ll i = 0; for (; it != s.end() && *it < v; i++) it++; return i; } }; #define pbds(T) my_pbds_tree<T> // gp_hash_tableでcountを使えないようにするため template <class T, class U> struct my_unordered_map { unordered_map<T, U> m; my_unordered_map(){}; auto begin() { return m.begin(); } auto end() { return m.end(); } auto cbegin() { return m.cbegin(); } auto cend() { return m.cend(); } template <class V> auto erase(V v) { return m.erase(v); } void clear() { m.clear(); } /*countは gp_hash_tableに存在しない*/ /*!= m.end()*/ template <class V> auto find(V v) { return m.find(v); } template <class V> auto &operator[](V n) { return m[n]; } }; #define unordered_map my_unordered_map #define umapi unordered_map<ll, ll> #define umapp unordered_map<P, ll> #define umapu unordered_map<uint64_t, ll> #define umapip unordered_map<ll, P> #else // umapはunorderd_mapになる // umapiはgp_hash_table // find_by_order(k) k番目のイテレーター // order_of_key(k) k以上が前から何番目か #define pbds(U) \ __gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag, \ __gnu_pbds::tree_order_statistics_node_update> #define umapi __gnu_pbds::gp_hash_table<ll, ll, xorshift> #define umapp __gnu_pbds::gp_hash_table<P, ll, xorshift> #define umapu __gnu_pbds::gp_hash_table<uint64_t, ll, xorshift> #define umapip __gnu_pbds::gp_hash_table<ll, P, xorshift> #endif /*@formatter:on*/ struct xorshift { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } size_t operator()(std::pair<ll, ll> x) const { ll v = ((x.first) << 32) | x.second; static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(v + FIXED_RANDOM); } }; /*@formatter:off*/ template <class U, class L> void operator+=( __gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update> &s, L v) { s.insert(v); } //衝突対策 #define ws ws_ template <class A, class B, class C> struct T2 { A f; B s; C t; T2() { f = 0, s = 0, t = 0; } T2(A f, B s, C t) : f(f), s(s), t(t) {} bool operator<(const T2 &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 ?n s > r.s : t > r.t; 大きい順 */ } bool operator>(const T2 &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 T2 &r) const { return f == r.f && s == r.s && t == r.t; } bool operator!=(const T2 &r) const { return f != r.f || s != r.s || t != r.t; } }; template <class A, class B, class C, class D> struct F2 { A a; B b; C c; D d; F2() { a = 0, b = 0, c = 0, d = 0; } F2(A a, B b, C c, D d) : a(a), b(b), c(c), d(d) {} bool operator<(const F2 &r) const { return a != r.a ? a < r.a : b != r.b ? b < r.b : c != r.c ? c < r.c : d < r.d; /* return a != r.a ? a > r.a : b != r.b ? b > r.b : c != r.c ? c > r.c : d > r.d;*/ } bool operator>(const F2 &r) const { return a != r.a ? a > r.a : b != r.b ? b > r.b : c != r.c ? c > r.c : d > r.d; /* return a != r.a ? a < r.a : b != r.b ? b < r.b : c != r.c ? c < r.c : d < r.d;*/ } bool operator==(const F2 &r) const { return a == r.a && b == r.b && c == r.c && d == r.d; } bool operator!=(const F2 &r) const { return a != r.a || b != r.b || c != r.c || d != r.d; } ll operator[](ll i) { assert(i < 4); return i == 0 ? a : i == 1 ? b : i == 2 ? c : d; } }; typedef T2<ll, ll, ll> T; typedef F2<ll, ll, ll, ll> F; T mt(ll a, ll b, ll c) { return T(a, b, c); } F mf(ll a, ll b, ll c, ll d) { return F(a, b, c, d); } //@マクロ省略系 型,構造 #define double long double #define pow powl using dou = double; const double eps = 1e-9; //基本コメントアウト /* struct epsdou { double v; epsdou(double v = 0) : v(v) {} template<class T> epsdou &operator+=(T b) { v += (double) b; return (*this); } template<class T> epsdou &operator-=(T b) { v -= (double) b; return (*this); } template<class T> epsdou &operator*=(T b) { v *= (double) b; return (*this); } template<class T> epsdou &operator/=(T b) { v /= (double) b; return (*this); } epsdou operator+(epsdou b) { return v + (double) b; } epsdou operator-(epsdou b) { return v - (double) b; } epsdou operator*(epsdou b) { return v * (double) b; } epsdou operator/(epsdou b) { return v / (double) b; } epsdou operator-() const { return epsdou(-v); } template<class T> bool operator<(T b) { return v < (double) b; } template<class T> bool operator>(T b) { return v > (double) b; } template<class T> bool operator==(T b) { return fabs(v - (double) b) <= eps; } template<class T> bool operator<=(T b) { return v < (double) b || fabs(v - b) <= eps; } template<class T> bool operator>=(T b) { return v > (double) b || fabs(v - b) <= eps; } operator double() { return v; }};istream &operator>>(istream &iss, epsdou &a) { iss >> a.v; return iss;}ostream &operator<<(ostream &os, epsdou &a) { os << a.v; return os;} #define eps_conr_t(o) template<class T> epsdou operator o(T b, epsdou a){return a.v o (dou)b;} #define eps_conl_t(o) template<class T> epsdou operator o(epsdou a, T b){return a.v o (dou)b;} eps_conl_t(+)eps_conl_t(-)eps_conl_t(*)eps_conl_t(/)eps_conr_t(+)eps_conr_t(-)eps_conr_t(*)eps_conr_t(/) #undef double #define double epsdou */ #define ull unsigned long long using itn = int; using str = string; using bo = bool; #define au auto using P = pair<ll, ll>; using mvp = mvec<P>; using mvt = mvec<T>; #define MIN(a) numeric_limits<a>::min() #define MAX(a) numeric_limits<a>::max() #define fi first #define se second #define beg begin #define rbeg rbegin #define con continue #define bre break #define brk break #define is == #define el else #define elf else if #define upd update #define sstream stringstream #define maxq 1 #define minq -1 #define ZERO(a) memset(a, 0, sizeof(a)) #define MINUS(a) memset(a, 0xff, sizeof(a)) #define MALLOC(type, len) (type *)malloc((len) * sizeof(type)) #define lam(right) [&](auto &p) { return p right; } //マクロ省略系 コンテナ 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 V vector #define vvt0(t) vector<vector<t>> #define vvt1(t, a) vector<vector<t>> a #define vvt2(t, a, b) vector<vector<t>> a(b) #define vvt3(t, a, b, c) vector<vector<t>> a(b, vector<t>(c)) #define vvt4(t, a, b, c, d) vector<vector<t>> a(b, vector<t>(c, d)) #define vvi(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(ll, __VA_ARGS__) #define vvb(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(bool, __VA_ARGS__) #define vvs(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(string, __VA_ARGS__) #define vvd(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(double, __VA_ARGS__) #define vvc(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(char, __VA_ARGS__) #define vvp(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(P, __VA_ARGS__) #define vvt(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(T, __VA_ARGS__) #define vv(type, ...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(type, __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 vn(type, name, ...) auto name = make_v<type>(__VA_ARGS__) #define PQ priority_queue<ll, vector<ll>, greater<ll>> #define tos to_string using mapi = map<ll, ll>; using mapp = map<P, ll>; using mapd = map<dou, ll>; using mapc = map<char, ll>; using maps = map<str, ll>; using seti = set<ll>; using setd = set<dou>; using setc = set<char>; using sets = set<str>; using qui = queue<ll>; #define uset unordered_set #define useti unordered_set<ll, xorshift> #define mset multiset #define mseti multiset<ll> #define umap unordered_map #define mmap multimap //任意のマクロサポート用 使う度に初期化する int index_, v1_, v2_, v3_; template <class T> struct pq { priority_queue<T, vector<T>, greater<T>> q; /*小さい順*/ T su = 0; void clear() { q = priority_queue<T, vector<T>, greater<T>>(); su = 0; } void operator+=(T v) { su += v; q.push(v); } T sum() { return su; } T top() { return q.top(); } void pop() { su -= q.top(); q.pop(); } T poll() { T ret = q.top(); su -= ret; q.pop(); return ret; } ll size() { return q.size(); } }; template <class T> struct pqg { priority_queue<T> q; /*大きい順*/ T su = 0; void clear() { q = priority_queue<T>(); su = 0; } void operator+=(T v) { su += v; q.push(v); } T sum() { return su; } T top() { return q.top(); } void pop() { su -= q.top(); q.pop(); } T poll() { T ret = q.top(); su -= ret; q.pop(); return ret; } ll size() { return q.size(); } }; #define pqi pq<ll> #define pqgi pqg<ll> //マクロ 繰り返し //↓@オーバーロード隔離 #define rep1(n) for (ll rep1i = 0, rep1lim = n; rep1i < rep1lim; ++rep1i) #define rep2(i, n) for (ll i = 0, rep2lim = n; i < rep2lim; ++i) #define rep3(i, m, n) for (ll i = m, rep3lim = n; i < rep3lim; ++i) #define rep4(i, m, n, ad) for (ll i = m, rep4lim = n; i < rep4lim; i += ad) //逆順 閉区間 #define rer2(i, n) for (ll i = n; i >= 0; i--) #define rer3(i, m, n) for (ll i = m, rer3lim = n; i >= rer3lim; i--) #define rer4(i, m, n, dec) for (ll i = m, rer4lim = n; i >= rer4lim; i -= dec) //ループを一つにまとめないとフォーマットで汚くなるため #define nex_ind1(i) i++ #define nex_ind2(i, j, J) \ i = (j + 1 == J) ? i + 1 : i, j = (j + 1 == J ? 0 : j + 1) #define nex_ind3(i, j, k, J, K) \ i = (j + 1 == J && k + 1 == K) ? i + 1 : i, \ j = (k + 1 == K) ? (j + 1 == J ? 0 : j + 1) : j, \ k = (k + 1 == K ? 0 : k + 1) #define nex_ind4(i, j, k, l, J, K, L) \ i = (j + 1 == J && k + 1 == K && l + 1 == L) ? i + 1 : i, \ j = (k + 1 == K && l + 1 == L) ? (j + 1 == J ? 0 : j + 1) : j, \ k = (l + 1 == L ? (k + 1 == K ? 0 : k + 1) : k), l = l + 1 == L ? 0 : l + 1 #define nex_ind5(i, j, k, l, m, J, K, L, M) \ i = (j + 1 == J && k + 1 == K && l + 1 == L && m + 1 == M) ? i + 1 : i, \ j = (k + 1 == K && l + 1 == L && m + 1 == M) ? (j + 1 == J ? 0 : j + 1) : j, \ k = (l + 1 == L && m + 1 == M ? (k + 1 == K ? 0 : k + 1) : k), \ l = m + 1 == M ? l + 1 == L ? 0 : l + 1 : l, m = m + 1 == M ? 0 : m + 1 #define repss2(i, I) for (int i = 0; i < I; i++) #define repss4(i, j, I, J) \ for (int i = (J ? 0 : I), j = 0; i < I; nex_ind2(i, j, J)) #define repss6(i, j, k, I, J, K) \ for (int i = (J && K ? 0 : I), j = 0, k = 0; i < I; nex_ind3(i, j, k, J, K)) #define repss8(i, j, k, l, I, J, K, L) \ for (int i = (J && K && L ? 0 : I), j = 0, k = 0, l = 0; i < I; \ nex_ind4(i, j, k, l, J, K, L)) #define repss10(i, j, k, l, m, I, J, K, L, M) \ for (int i = (J && K && L && M ? 0 : I), j = 0, k = 0, l = 0, m = 0; i < I; \ nex_ind5(i, j, k, l, m, J, K, L, M)) // i,j,k...をnまで見る #define reps2(i, n) repss2(i, n) #define reps3(i, j, n) repss4(i, j, n, n) #define reps4(i, j, k, n) repss6(i, j, k, n, n, n) #define reps5(i, j, k, l, n) repss8(i, j, k, l, n, n, n, n) template <class T> void nex_repv2(int &i, int &j, int &I, int &J, vector<vector<T>> &s) { while (1) { j++; if (j >= J) { j = 0; i++; if (i < I) { J = (int)s[i].size(); } } if (i >= I || J) return; } } template <class T> void nex_repv3(int &i, int &j, int &k, int &I, int &J, int &K, vector<vector<vector<T>>> &s) { while (1) { k++; if (k >= K) { k = 0; j++; if (j >= J) { j = 0; i++; if (i >= I) return; } } J = (int)s[i].size(); K = (int)s[i][j].size(); if (J && K) return; } } #define repv_2(i, a) repss2(i, sz(a)) //正方形である必要はない //直前を持つのとどっちが早いか #define repv_3(i, j, a) \ for (int I = (int)a.size(), J = (int)a[0].size(), i = 0, j = 0; i < I; \ nex_repv2(i, j, I, J, a)) //箱状になっている事が要求される つまり[i] 次元目の要素数は一定 #define repv_4(i, j, k, a) \ for (int I = (int)a.size(), J = (int)a[0].size(), K = (int)a[0][0].size(), \ i = 0, j = 0, k = 0; \ i < I; nex_repv3(i, j, k, I, J, K, a)) #define repv_5(i, j, k, l, a) \ repss8(i, j, k, l, sz(a), sz(a[0]), sz(a[0][0]), sz(a[0][0][0])) #define repv_6(i, j, k, l, m, a) \ repss10(i, j, k, l, m, sz(a), sz(a[0]), sz(a[0][0]), sz(a[0][0][0]), \ sz(a[0][0][0][0])) template <typename T> struct has_rbegin_rend { private: template <typename U> static auto check(U &&obj) -> decltype(std::rbegin(obj), std::rend(obj), std::true_type{}); static std::false_type check(...); public: static constexpr bool value = decltype(check(std::declval<T>()))::value; }; template <typename T> constexpr bool has_rbegin_rend_v = has_rbegin_rend<T>::value; template <typename Iterator> class Range { public: Range(Iterator &&begin, Iterator &&end) noexcept : m_begin(std::forward<Iterator>(begin)), m_end(std::forward<Iterator>(end)) {} Iterator begin() const noexcept { return m_begin; } Iterator end() const noexcept { return m_end; } private: const Iterator m_begin; const Iterator m_end; }; template <typename Iterator> static inline Range<Iterator> makeRange(Iterator &&begin, Iterator &&end) noexcept { return Range<Iterator>{std::forward<Iterator>(begin), std::forward<Iterator>(end)}; } template <typename T> static inline decltype(auto) makeReversedRange(const std::initializer_list<T> &iniList) noexcept { return makeRange(std::rbegin(iniList), std::rend(iniList)); } template <typename T, typename std::enable_if_t<has_rbegin_rend_v<T>, std::nullptr_t> = nullptr> static inline decltype(auto) makeReversedRange(T &&c) noexcept { return makeRange(std::rbegin(c), std::rend(c)); } /* rbegin(), rend()を持たないものはこっちに分岐させて,エラーメッセージを少なくする*/ template <typename T, typename std::enable_if<!has_rbegin_rend<T>::value, std::nullptr_t>::type = nullptr> static inline void makeReversedRange(T &&) noexcept { static_assert(has_rbegin_rend<T>::value, "Specified argument doesn't have reverse iterator."); } #define form1(st) \ for (auto &&form_it = st.begin(); form_it != st.end(); ++form_it) #define form3(k, v, st) \ for (auto &&form_it = st.begin(); form_it != st.end(); ++form_it) #define form4(k, v, st, r) \ for (auto &&form_it = st.begin(); form_it != st.end() && (*form_it).fi < r; \ ++form_it) #define form5(k, v, st, l, r) \ for (auto &&form_it = st.lower_bound(l); \ form_it != st.end() && (*form_it).fi < r; ++form_it) #define fors1(st) \ for (auto &&fors_it = st.begin(); fors_it != st.end(); ++fors_it) #define fors2(v, st) \ for (auto &&fors_it = st.begin(); fors_it != st.end(); ++fors_it) #define fors3(v, st, r) \ for (auto &&fors_it = st.begin(); fors_it != st.end() && (*fors_it) < r; \ ++fors_it) #define fors4(v, st, l, r) \ for (auto &&fors_it = st.lower_bound(l); \ fors_it != st.end() && (*fors_it) < r; ++fors_it) #define forslr3(st, a, b) \ for (auto &&forslr_it = st.begin(); forslr_it != st.end(); ++forslr_it) #define forslr4(v, st, a, b) \ for (auto &&forslr_it = st.begin(); forslr_it != st.end(); ++forslr_it) #define forslr5(v, st, r, a, b) \ for (auto &&forslr_it = st.begin(); \ forslr_it != st.end() && (*forslr_it) < r; ++forslr_it) #define forslr6(v, st, l, r, a, b) \ for (auto &&forslr_it = st.lower_bound(l); \ forslr_it != st.end() && (*forslr_it) < r; ++forslr_it) template <class U> vector<U> to1d(vector<U> &a) { return a; } template <class U> vector<U> to1d(V<V<U>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) res.push_back(a2); return res; } template <class U> vector<U> to1d(V<V<V<U>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) res.push_back(a3); return res; } template <class U> vector<U> to1d(V<V<V<V<U>>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) for (auto &&a4 : a3) res.push_back(a4); return res; } template <class U> vector<U> to1d(V<V<V<V<V<U>>>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) for (auto &&a4 : a3) for (auto &&a5 : a4) res.push_back(a5); return res; } template <class U> vector<U> to1d(V<V<V<V<V<V<U>>>>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) for (auto &&a4 : a3) for (auto &&a5 : a4) for (auto &&a6 : a5) res.push_back(a6); return res; } #define fora_init_2(a, A) ; #define fora_init_4(a, b, A, B) \ auto &&a = A[fora_i]; \ auto &&b = B[fora_i]; #define fora_init_6(a, b, c, A, B, C) \ auto &&a = A[fora_i]; \ auto &&b = B[fora_i]; \ auto &&c = C[fora_i]; #define fora_init_8(a, b, c, d, A, B, C, D) \ auto &&a = A[fora_i]; \ auto &&b = B[fora_i]; \ auto &&c = C[fora_i]; \ auto &&d = D[fora_i]; #define fora_init(...) \ over8(__VA_ARGS__, fora_init_8, a, fora_init_6, a, fora_init_4, a, \ fora_init_2)(__VA_ARGS__) #define forr_init_2(a, A) auto &&a = A[forr_i]; #define forr_init_4(a, b, A, B) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; #define forr_init_6(a, b, c, A, B, C) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; \ auto &&c = C[forr_i]; #define forr_init_8(a, b, c, d, A, B, C, D) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; \ auto &&c = C[forr_i]; \ auto &&d = D[forr_i]; #define forr_init(...) \ over8(__VA_ARGS__, forr_init_8, a, forr_init_6, a, forr_init_4, a, \ forr_init_2)(__VA_ARGS__) #define forp_init(k, v, ...) \ auto &&k = (*forp_it).fi; \ auto &&v = (*forp_it).se; #define form_init(k, v, ...) \ auto &&k = (*form_it).fi; \ auto &&v = (*form_it).se; #define fors_init(v, ...) auto &&v = (*fors_it); #define forlr_init(a, A, ngl, ngr) \ auto a = A[forlr_i]; \ auto prev = forlr_i ? A[forlr_i - 1] : ngl; \ auto next = forlr_i + 1 < rep2lim ? A[forlr_i + 1] : ngr; #define forslr_init4(a, A, ngl, ngr) \ auto a = (*forslr_it); \ auto prev = (forslr_it != A.begin()) ? (*std::prev(forslr_it)) : ngl; \ auto next = (forslr_it != std::prev(A.end())) ? (*std::next(forslr_it)) : ngr; #define forslr_init5(a, A, r, ngl, ngr) \ auto a = (*forslr_it); \ auto prev = (forslr_it != A.begin()) ? (*std::prev(forslr_it)) : ngl; \ auto next = (forslr_it != std::prev(A.end())) ? (*std::next(forslr_it)) : ngr; #define forslr_init6(a, A, l, r, ngl, ngr) \ auto a = (*forslr_it); \ auto prev = (forslr_it != A.begin()) ? (*std::prev(forslr_it)) : ngl; \ auto next = (forslr_it != std::prev(A.end())) ? (*std::next(forslr_it)) : ngr; #define forslr_init(...) \ over6(__VA_ARGS__, forslr_init6, forslr_init5, forslr_init4)(__VA_ARGS__); #define fora_2(a, A) for (auto &&a : A) #define fora_4(a, b, A, B) rep(fora_i, sz(A)) #define fora_6(a, b, c, A, B, C) rep(fora_i, sz(A)) #define fora_8(a, b, c, d, A, B, C, D) rep(fora_i, sz(A)) #define forr_2(a, A) rer(forr_i, sz(A) - 1) #define forr_4(a, b, A, B) rer(forr_i, sz(A) - 1) #define forr_6(a, b, c, A, B, C) rer(forr_i, sz(A) - 1) #define forr_8(a, b, c, d, A, B, C, D) rer(forr_i, sz(A) - 1) //↑@オーバーロード隔離 // rep系はインデックス、for系は中身 #define rep(...) over4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__) #define rer(...) over4(__VA_ARGS__, rer4, rer3, rer2, )(__VA_ARGS__) // char用のrep #define repc(i, m, n) for (char i = m, repc3lim = n; i < repc3lim; ++i) // i,j,k...をnまで見る #define reps(...) over5(__VA_ARGS__, reps5, reps4, reps3, reps2, )(__VA_ARGS__) #define repss(...) \ over10(__VA_ARGS__, repss10, a, repss8, a, repss6, a, repss4, a, \ repss2)(__VA_ARGS__) // vectorのindexを走査する // repv(i,j,vvi) #define repv(...) \ over6(__VA_ARGS__, repv_6, repv_5, repv_4, repv_3, repv_2, )(__VA_ARGS__) #define rerv(i, A) for (int i = sz(A) - 1; i >= 0; i--) // repvn(dp) nは次元 #define repv1(a) repv(i, a) #define repv2(a) repv(i, j, a) #define repv3(a) repv(i, j, k, a) #define repv4(a) repv(i, j, k, l, a) #define fora(...) \ over8(__VA_ARGS__, fora_8, a, fora_6, a, fora_4, a, fora_2)(__VA_ARGS__) #define forr(...) \ over8(__VA_ARGS__, forr_8, a, forr_6, a, forr_4, a, forr_2)(__VA_ARGS__) //#define forr(v, a) for(auto&& v : makeReversedRange(a)) //参照を取らない #define forv(a, b) for (auto a : to1d(b)) //インデックスを前後含めて走査 #define ring(i, s, len) \ for (int i = s, prev = (s == 0) ? len - 1 : s - 1, \ next = (s == len - 1) ? 0 : s + 1, cou = 0; \ cou < len; \ cou++, prev = i, i = next, next = (next == len - 1) ? 0 : next + 1) //値と前後を見る #define ringv(v, d) \ index_ = 0; \ for (auto prev = d[sz(d) - 1], next = (int)d.size() > 1 ? d[1] : d[0], \ v = d[0]; \ index_ < sz(d); index_++, prev = v, v = next, \ next = (index_ >= sz(d) - 1 ? d[0] : d[index_ + 1])) // 左右をnext prevで見る 0の左と nの右 #define forlr(v, d, banpei_l, banpei_r) rep(forlr_i, sz(d)) #define form(...) \ over5(__VA_ARGS__, form5, form4, form3, form2, form1)(__VA_ARGS__) #define fors(...) over4(__VA_ARGS__, fors4, fors3, fors2, fors1)(__VA_ARGS__) #define forslr(...) \ over6(__VA_ARGS__, forslr6, forslr5, forslr4, forslr3)(__VA_ARGS__) #define forp(k, v, st) \ for (auto &&forp_it = st.begin(); forp_it != st.end(); ++forp_it) //マクロ 定数 #define k3 1010 #define k4 10101 #define k5 101010 #define k6 1010101 #define k7 10101010 const ll inf = (ll)1e9 + 100; const ll linf = (ll)1e18 + 100; const dou dinf = (dou)linf * linf; const char infc = '{'; const string infs = "{"; const double PI = 3.1415926535897932384626433832795029L; //マクロ省略形 関数等 #define arsz(a) (sizeof(a) / sizeof(a[0])) #define sz(a) ((ll)(a).size()) #define mp make_pair #define pb pop_back #define pf push_front #define eb emplace_back #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() constexpr bool ev(ll a) { return !(a & 1); } constexpr bool od(ll a) { return (a & 1); } //@拡張系 こう出来るべきというもの //埋め込み 存在を意識せずに機能を増やされているもの namespace std { template <> class hash<std::pair<signed, signed>> { public: size_t operator()(const std::pair<signed, signed> &x) const { return hash<ll>()(((ll)x.first << 32) | x.second); } }; template <> class hash<std::pair<ll, ll>> { public : /*大きいllが渡されると、<<32でオーバーフローするがとりあえず問題ないと判断*/ size_t operator()(const std::pair<ll, ll> &x) const { return hash<ll>()(((ll)x.first << 32) | x.second); } }; } // namespace std // stream まとめ /*@formatter:on*/ istream &operator>>(istream &iss, P &a) { iss >> a.first >> a.second; return iss; } template <typename T> istream &operator>>(istream &iss, vector<T> &vec) { for (T &x : vec) iss >> x; return iss; } template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> p) { os << p.fi << " " << p.se; return os; } ostream &operator<<(ostream &os, T p) { os << p.f << " " << p.s << " " << p.t; return os; } ostream &operator<<(ostream &os, F p) { os << p.a << " " << p.b << " " << p.c << " " << p.d; return os; } template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) { for (ll i = 0; i < vec.size(); ++i) os << vec[i] << (i + 1 == vec.size() ? "" : " "); return os; } template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) { for (ll i = 0; i < vec.size(); ++i) { for (ll j = 0; j < vec[i].size(); ++j) { os << vec[i][j] << " "; } os << endl; } return os; } template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &m) { for (auto &&v : m) os << v; return os; } template <class T> ostream &operator<<(ostream &os, set<T> s) { fora(v, s) { os << v << " "; } return os; } template <class T> ostream &operator<<(ostream &os, deque<T> a) { fora(v, a) os << v << " "; return os; } ostream &operator<<(ostream &os, vector<vector<char>> &vec) { rep(h, sz(vec)) { rep(w, sz(vec[0])) { os << vec[h][w]; } os << endl; } return os; } // template<class T,class U>ostream &operator<<(ostream &os, vector<pair<T,U>>& // a) {fora(v,a)os<<v<<endl;return os;} /*@formatter:off*/ template <typename W, typename H> void resize(W &vec, const H head) { vec.resize(head); } template <typename W, typename H, typename... T> void resize(W &vec, const H &head, const T... tail) { vec.resize(head); for (auto &v : vec) resize(v, tail...); } // 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); }); } //削除した後のvectorを返す template <typename T, typename F> vector<T> erase_if2(vector<T> &v, F f) { vector<T> nv; rep(i, sz(v)) { if (!f(v[i])) { nv.push_back(v[i]); } } return nv; } template <typename T, typename F> vector<vector<T>> erase_if2(vector<vector<T>> &v, F f) { vector<vector<T>> res; rep(i, sz(v)) { res[i] = erase_if2(v[i], f); } return res; } // all_of(A, %2); // all_of(A, a, a%2); #define all_of__2(a, right) all_of2(a, lam(right)) #define all_of__3(a, v, siki) all_of2(a, [&](auto v) { return siki; }) #define all_of(...) over3(__VA_ARGS__, all_of__3, all_of__2)(__VA_ARGS__) #define all_of_f(a, f) all_of2(a, f) #define any_of__2(a, right) any_of2(a, lam(right)) #define any_of__3(a, v, siki) any_of2(a, [&](auto v) { return siki; }) #define any_of(...) over3(__VA_ARGS__, any_of__3, any_of__2)(__VA_ARGS__) #define any_of_f(a, f) any_of2(a, f) #define none_of__2(a, right) none_of2(a, lam(right)) #define none_of__3(a, v, siki) none_of2(a, [&](auto v) { return siki; }) #define none_of(...) over3(__VA_ARGS__, none_of__3, none_of__2)(__VA_ARGS__) #define none_of_f(a, f) none_of2(a, f) #define find_if__2(a, right) find_if2(a, lam(right)) #define find_if__3(a, v, siki) find_if2(a, [&](auto v) { return siki; }) #define find_if(...) over3(__VA_ARGS__, find_if__3, find_if__2)(__VA_ARGS__) #define find_if_f(a, f) find_if2(a, f) #define rfind_if__2(a, right) rfind_if2(a, lam(right)) #define rfind_if__3(a, v, siki) rfind_if2(a, [&](auto v) { return siki; }) #define rfind_if(...) over3(__VA_ARGS__, rfind_if__3, rfind_if__2)(__VA_ARGS__) #define rfind_if_f(a, f) rfind_if2(a, f) #define contains_if__2(a, right) contains_if2(a, lam(right)) #define contains_if__3(a, v, siki) contains_if2(a, [&](auto v) { return siki; }) #define contains_if(...) \ over3(__VA_ARGS__, contains_if__3, contains_if__2)(__VA_ARGS__) #define contains_if_f(a, f) contains_if2(a, f) #define count_if__2(a, right) count_if2(a, lam(right)) #define count_if__3(a, v, siki) count_if2(a, [&](auto v) { return siki; }) #define count_if(...) over3(__VA_ARGS__, count_if__3, count_if__2)(__VA_ARGS__) #define count_if_f(a, f) count_if2(a, f) #define for_each__2(a, right) \ do { \ fora(v, a) { v right; } \ } while (0) #define for_each__3(a, v, siki) \ do { \ fora(v, a) { siki; } \ } while (0) #define for_each(...) over3(__VA_ARGS__, for_each__3, for_each__2)(__VA_ARGS__) #define for_each_f(a, f) \ do { \ fora(v, a) { f(v); } \ } while (0) #define erase_if__2(a, right) erase_if2(a, lam(right)) #define erase_if__3(a, v, siki) erase_if2(a, [&](auto v) { return siki; }) #define erase_if(...) over3(__VA_ARGS__, erase_if__3, erase_if__2)(__VA_ARGS__) #define erase_if_f(a, f) erase_if2(a, f) #define entry_if__2(a, right) erase_if2(a, [&](auto v) { return !(v right); }) #define entry_if__3(a, v, siki) erase_if2(a, [&](auto v) { return !(siki); }) #define entry_if(...) over3(__VA_ARGS__, entry_if__3, entry_if__2)(__VA_ARGS__) #define entry_if_f(a, f) erase_if2(a, [&](auto v) { return !f(v); }) template <class T, class U, class W> void replace(vector<W> &a, T key, U v) { rep(i, sz(a)) if (a[i] == key) a[i] = v; } template <class T, class U, class W> void replace(vector<vector<W>> &A, T key, U v) { rep(i, sz(A)) replace(A[i], key, v); } void replace(str &a, char key, str v) { if (v == "") a.erase(remove(all(a), key), a.end()); } void replace(str &a, char key, char v) { replace(all(a), key, v); } // keyと同じかどうか01で置き換える template <class T, class U> void replace(vector<T> &a, U k) { rep(i, sz(a)) a[i] = a[i] == k; } template <class T, class U> void replace(vector<vector<T>> &a, U k) { rep(i, sz(a)) rep(j, sz(a[0])) a[i][j] = a[i][j] == k; } template <class T> void replace(T &a) { replace(a, '#'); } void replace(str &a, str key, str v) { stringstream t; ll kn = sz(key); std::string::size_type Pos(a.find(key)); ll l = 0; while (Pos != std::string::npos) { t << a.substr(l, Pos - l); t << v; l = Pos + kn; Pos = a.find(key, Pos + kn); } t << a.substr(l, sz(a) - l); a = t.str(); } template <class T> bool includes(vector<T> &a, vector<T> &b) { vi c = a; vi d = b; sort(all(c)); sort(all(d)); return includes(all(c), all(d)); } template <class T> bool is_permutation(vector<T> &a, vector<T> &b) { return is_permutation(all(a), all(b)); } template <class T> bool next_permutation(vector<T> &a) { return next_permutation(all(a)); } void iota(vector<ll> &ve, ll s, ll n) { ve.resize(n); iota(all(ve), s); } vi iota(ll s, ll len) { vi ve(len); iota(all(ve), s); return ve; } template <class A, class B> auto vtop(vector<A> &a, vector<B> &b) { assert(sz(a) == sz(b)); /*stringを0で初期化できない */ vector<pair<A, B>> res; rep(i, sz(a)) res.eb(a[i], b[i]); return res; } template <class A, class B> void ptov(vector<pair<A, B>> &p, vector<A> &a, vector<B> &b) { a.resize(sz(p)), b.resize(sz(p)); rep(i, sz(p)) a[i] = p[i].fi, b[i] = p[i].se; } template <class A, class B, class C> auto vtot(vector<A> &a, vector<B> &b, vector<C> &c) { assert(sz(a) == sz(b) && sz(b) == sz(c)); vector<T2<A, B, C>> res; rep(i, sz(a)) res.eb(a[i], b[i], c[i]); return res; } template <class A, class B, class C, class D> auto vtof(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { assert(sz(a) == sz(b) && sz(b) == sz(c) && sz(c) == sz(d)); vector<F2<A, B, C, D>> res; rep(i, sz(a)) res.eb(a[i], b[i], c[i], d[i]); return res; } enum pcomparator { fisi, fisd, fdsi, fdsd, sifi, sifd, sdfi, sdfd }; enum tcomparator { fisiti, fisitd, fisdti, fisdtd, fdsiti, fdsitd, fdsdti, fdsdtd, fitisi, fitisd, fitdsi, fitdsd, fdtisi, fdtisd, fdtdsi, fdtdsd, sifiti, sifitd, sifdti, sifdtd, sdfiti, sdfitd, sdfdti, sdfdtd, sitifi, sitifd, sitdfi, sitdfd, sdtifi, sdtifd, sdtdfi, sdfdfd, tifisi, tifisd, tifdsi, tifdsd, tdfisi, tdfisd, tdfdsi, tdfdsd, tisifi, tisifd, tisdfi, tisdfd, tdsifi, tdsifd, tdsdfi, tdsdfd }; template <class A, class B> void sort(vector<pair<A, B>> &a, pcomparator type) { typedef pair<A, B> U; if (type == fisi) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se < r.se; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se > r.se; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se < r.se; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se > r.se; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi < r.fi; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi > r.fi; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi < r.fi; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi > r.fi; }); }; template <class U> void sort(vector<U> &a, pcomparator type) { if (type == fisi) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f > r.f; }); }; template <class A, class B, class C, class D> void sort(vector<F2<A, B, C, D>> &a, pcomparator type) { typedef F2<A, B, C, D> U; if (type == fisi) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a > r.a; }); }; template <class U> void sort(vector<U> &a, tcomparator type) { if (type == 0) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t < r.t; }); else if (type == 1) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t > r.t; }); else if (type == 2) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t < r.t; }); else if (type == 3) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t > r.t; }); else if (type == 4) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t < r.t; }); else if (type == 5) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t > r.t; }); else if (type == 6) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t < r.t; }); else if (type == 7) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t > r.t; }); else if (type == 8) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s < r.s; }); else if (type == 9) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s > r.s; }); else if (type == 10) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s < r.s; }); else if (type == 11) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s > r.s; }); else if (type == 12) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s < r.s; }); else if (type == 13) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s > r.s; }); else if (type == 14) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s < r.s; }); else if (type == 15) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s > r.s; }); else if (type == 16) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t < r.t; }); else if (type == 17) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t > r.t; }); else if (type == 18) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t < r.t; }); else if (type == 19) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t > r.t; }); else if (type == 20) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t < r.t; }); else if (type == 21) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t > r.t; }); else if (type == 22) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t < r.t; }); else if (type == 23) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t > r.t; }); else if (type == 24) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f < r.f; }); else if (type == 25) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f > r.f; }); else if (type == 26) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f < r.f; }); else if (type == 27) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f > r.f; }); else if (type == 28) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f < r.f; }); else if (type == 29) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f > r.f; }); else if (type == 30) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f < r.f; }); else if (type == 31) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f > r.f; }); else if (type == 32) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == 33) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == 34) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == 35) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == 36) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == 37) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == 38) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == 39) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == 40) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == 41) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == 42) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == 43) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f > r.f; }); else if (type == 44) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == 45) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == 46) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == 47) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f > r.f; }); } template <class A, class B, class C, class D> void sort(vector<F2<A, B, C, D>> &a, tcomparator type) { typedef F2<A, B, C, D> U; if (type == 0) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c < r.c; }); else if (type == 1) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c > r.c; }); else if (type == 2) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c < r.c; }); else if (type == 3) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c > r.c; }); else if (type == 4) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c < r.c; }); else if (type == 5) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c > r.c; }); else if (type == 6) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c < r.c; }); else if (type == 7) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c > r.c; }); else if (type == 8) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b < r.b; }); else if (type == 9) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b > r.b; }); else if (type == 10) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b < r.b; }); else if (type == 11) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b > r.b; }); else if (type == 12) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b < r.b; }); else if (type == 13) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b > r.b; }); else if (type == 14) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b < r.b; }); else if (type == 15) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b > r.b; }); else if (type == 16) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c < r.c; }); else if (type == 17) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c > r.c; }); else if (type == 18) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c < r.c; }); else if (type == 19) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c > r.c; }); else if (type == 20) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c < r.c; }); else if (type == 21) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c > r.c; }); else if (type == 22) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c < r.c; }); else if (type == 23) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c > r.c; }); else if (type == 24) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a < r.a; }); else if (type == 25) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a > r.a; }); else if (type == 26) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a < r.a; }); else if (type == 27) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a > r.a; }); else if (type == 28) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a < r.a; }); else if (type == 29) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a > r.a; }); else if (type == 30) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a < r.a; }); else if (type == 31) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a > r.a; }); else if (type == 32) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == 33) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == 34) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == 35) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == 36) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == 37) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == 38) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == 39) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == 40) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == 41) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == 42) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == 43) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a > r.a; }); else if (type == 44) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == 45) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == 46) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == 47) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a > r.a; }); } void sort(string &a) { sort(all(a)); } void sort(int &a, int &b) { if (a > b) swap(a, b); } void sort(int &a, int &b, int &c) { sort(a, b); sort(a, c); sort(b, c); } void rsort(int &a, int &b) { if (a < b) swap(a, b); } void rsort(int &a, int &b, int &c) { rsort(a, b); rsort(a, c); rsort(b, c); } template <class T> void sort(vector<T> &a) { sort(all(a)); } // P l, P rで f(P) の形で渡す template <class U, class F> void sort(vector<U> &a, F f) { sort(all(a), [&](U l, U r) { return f(l) < f(r); }); }; template <class T> void rsort(vector<T> &a) { sort(all(a), greater<T>()); }; template <class U, class F> void rsort(vector<U> &a, F f) { sort(all(a), [&](U l, U r) { return f(l) > f(r); }); }; // F = T<T> //例えばreturn p.fi + p.se; template <class A, class B> void sortp(vector<A> &a, vector<B> &b) { auto c = vtop(a, b); sort(c); rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se; } template <class A, class B, class F> void sortp(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); sort(c, f); rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se; } template <class A, class B> void rsortp(vector<A> &a, vector<B> &b) { auto c = vtop(a, b); rsort(c); rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second; } template <class A, class B, class F> void rsortp(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); rsort(c, f); rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second; } template <class A, class B, class C> void sortt(vector<A> &a, vector<B> &b, vector<C> &c) { auto d = vtot(a, b, c); sort(d); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class F> void sortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); sort(d, f); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C> void rsortt(vector<A> &a, vector<B> &b, vector<C> &c) { auto d = vtot(a, b, c); rsort(d); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class F> void rsortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); rsort(d, f); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class D> void sortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { auto e = vtof(a, b, c, d); sort(e); rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d; } template <class A, class B, class C, class D> void rsortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { auto e = vtof(a, b, c, d); rsort(e); rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d; } // sortindex 元のvectorはソートしない template <class T> vi sorti(vector<T> &a) { auto b = a; vi ind = iota(0, sz(a)); sortp(b, ind); return ind; } /*indexの分で型が変わるためpcomparatorが必要*/ template <class T> vi sorti(vector<T> &a, pcomparator f) { auto b = a; vi ind = iota(0, sz(a)); sortp(b, ind, f); return ind; } template <class T, class F> vi sorti(vector<T> &a, F f) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(a[x]) < f(a[y]); }); return ind; } template <class T> vi rsorti(vector<T> &a) { auto b = a; vi ind = iota(0, sz(a)); rsortp(b, ind); return ind; } template <class T, class F> vi rsorti(vector<T> &a, F f) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(a[x]) > f(a[y]); }); return ind; } template <class A, class B, class F> vi sortpi(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(c[x]) < f(c[y]); }); return ind; } template <class A, class B> vi sortpi(vector<A> &a, vector<B> &b, pcomparator f) { vi ind = iota(0, sz(a)); auto c = a; auto d = b; sortt(c, d, ind, f); return ind; } template <class A, class B> vi sortpi(vector<A> &a, vector<B> &b) { return sortpi(a, b, fisi); }; template <class A, class B, class F> vi rsortpi(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(c[x]) > f(c[y]); }); return ind; } template <class A, class B> vi rsortpi(vector<A> &a, vector<B> &b) { return sortpi(a, b, fdsd); }; template <class A, class B, class C, class F> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(d[x]) < f(d[y]); }); return ind; } template <class A, class B, class C> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, pcomparator f) { vi ind = iota(0, sz(a)); auto d = vtof(a, b, c, ind); sort(d, f); rep(i, sz(a)) ind[i] = d[i].d; return ind; } template <class A, class B, class C> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { if (a[x] == a[y]) { if (b[x] == b[y]) return c[x] < c[y]; else return b[x] < b[y]; } else { return a[x] < a[y]; } }); return ind; } template <class A, class B, class C, class F> vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(d[x]) > f(d[y]); }); return ind; } template <class A, class B, class C> vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { if (a[x] == a[y]) { if (b[x] == b[y]) return c[x] > c[y]; else return b[x] > b[y]; } else { return a[x] > a[y]; } }); return ind; } template <class T> void sort2(vector<vector<T>> &a) { for (ll i = 0, n = a.size(); i < n; ++i) sort(a[i]); } template <class T> void rsort2(vector<vector<T>> &a) { for (ll i = 0, n = a.size(); i < n; ++i) rsort(a[i]); } template <class... T, class U> auto sorted(U head, T... a) { sort(head, a...); return head; } 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 W, typename T> void fill(W &xx, const T vall) { xx = vall; } template <typename W, typename T> void fill(vector<W> &vecc, const T vall) { for (auto &&vx : vecc) fill(vx, vall); } template <typename W, typename T> void fill(vector<W> &xx, ll len, const T v) { rep(i, len) xx[i] = v; } template <typename W, typename T> void fill(vector<vector<W>> &xx, int sh, int th, int sw, int tw, T v) { rep(h, sh, th) rep(w, sw, tw) xx[h][w] = v; } template <class T, class U> void fill(vector<T> &a, vi &ind, U val) { fora(v, ind) a[v] = val; } template <class W, class T> void fill(mvec<W> &xx, const T v) { fora(x, xx) fill(x, v); } template <typename A, size_t N> A sum(A (&a)[N]) { A res = 0; rep(i, N) res += a[i]; return res; } template <typename A, size_t N, size_t O> A sum(A (&a)[N][O]) { A res = 0; rep(i, N) rep(j, O) res += a[i][j]; return res; } template <typename A, size_t N, size_t O, size_t P> A sum(A (&a)[N][O][P]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) res += a[i][j][k]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A sum(A (&a)[N][O][P][Q]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) res += a[i][j][k][l]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A sum(A (&a)[N][O][P][Q][R]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) res += a[i][j][k][l][m]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A sum(A (&a)[N][O][P][Q][R][S]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) rep(n, S) res += a[i][j][k][l][m][n]; return res; } //@汎用便利関数 入力 ll in() { ll ret; cin >> ret; return ret; } string sin() { string ret; cin >> ret; return ret; } template <class T> void in(T &head) { cin >> head; } template <class T, class... U> void in(T &head, U &...tail) { cin >> head; in(tail...); } #define din1(a) \ ll a; \ cin >> a #define din2(a, b) \ ll a, b; \ cin >> a >> b #define din3(a, b, c) \ ll a, b, c; \ cin >> a >> b >> c #define din4(a, b, c, d) \ ll a, b, c, d; \ cin >> a >> b >> c >> d #define din5(a, b, c, d, e) \ ll a, b, c, d, e; \ cin >> a >> b >> c >> d >> e #define din6(a, b, c, d, e, f) \ ll a, b, c, d, e, f; \ cin >> a >> b >> c >> d >> e >> f #define din(...) \ over6(__VA_ARGS__, din6, din5, din4, din3, din2, din1)(__VA_ARGS__) #define dins1(a) \ str a; \ cin >> a #define dins2(a, b) \ str a, b; \ cin >> a >> b #define dins3(a, b, c) \ str a, b, c; \ cin >> a >> b >> c #define dins4(a, b, c, d) \ str a, b, c, d; \ cin >> a >> b >> c >> d #define dins5(a, b, c, d, e) \ str a, b, c, d, e; \ cin >> a >> b >> c >> d >> e #define dins6(a, b, c, d, e, f) \ str a, b, c, d, e, f; \ cin >> a >> b >> c >> d >> e >> f #define dins(...) \ over6(__VA_ARGS__, dins6, dins5, dins4, dins3, dins2, dins1)(__VA_ARGS__) #define din1d(a) \ din1(a); \ a-- #define din2d(a, b) \ din2(a, b); \ a--, b-- #define din3d(a, b, c) \ din3(a, b, c); \ a--, b--, c-- #define din4d(a, b, c, d) \ din4(a, b, c, d); \ a--, b--, c--, d-- #define dind(...) over4(__VA_ARGS__, din4d, din3d, din2d, din1d)(__VA_ARGS__) template <class T> void out2(T &&head) { cout << head; } template <class T, class... U> void out2(T &&head, U &&...tail) { cout << head << " "; out2(tail...); } template <class T, class... U> void out(T &&head, U &&...tail) { cout << head << " "; out2(tail...); cout << "" << endl; } template <class T> void out(T &&head) { cout << head << endl; } void out() { cout << "" << endl; } #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; } /*@formatter:on*/ 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]; if (typeid(a[i]) == typeid(P)) { ss << endl; } else { ss << " "; } } return ss.str(); } /*@formatter:off*/ 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(); } 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 = 12; H = min(H, sz(a)); rep(i, H) { ss << endl; ss << out_m2(a[i], W, U, i); } return ss.str(); } template <class T, size_t N> string out_m2(T (&a)[N]) { vector<T> b; resize(b, N); rep(i, N) { b[i] = a[i]; } return out_m2(b); } template <class T, size_t N, size_t M> string out_m2(T (&a)[N][M]) { vector<vector<T>> b; resize(b, N, M); rep(i, N) { rep(j, M) { b[i][j] = a[i][j]; } } return out_m2(b); } template <class T, size_t N, size_t M, size_t O> string out_m2(T (&a)[N][M][O]) { vector<vector<vector<T>>> b; resize(b, N, M, O); rep(i, N) { rep(j, M) { rep(k, O) { b[i][j][k] = a[i][j][k]; } } } return out_m2(b); } string out_m2(int a) { stringstream ss; ss << a; return ss.str(); } /*@formatter:on*/ template <class T> string out_m2(mvec<mvec<T>> &a, ll H = inf, ll W = inf, int key = inf) { H = min({H, sz(a), 6ll}); W = min({W, sz(a[0]), 6ll}); stringstream ss; ss << endl; // if (key == inf)ss << " *|"; else ss << " " << key << "|"; if (key == inf) ss << " *|"; else { ss << std::right << std::setw(2) << key; ss << "|"; } rep(w, -W, W) ss << std::right << std::setw(4) << w; ss << "" << endl; rep(w, W * 8 + 3) ss << "_"; ss << "" << endl; rep(h, -H, H) { ss << std::right << std::setw(2) << h << "|"; int NW = min(sz(a[h]), 6ll); rep(w, -NW, NW) { if (abs(a[h][w]) == linf) ss << " e" << ""; else ss << std::right << std::setw(4) << a[h][w]; } ss << "" << endl; } return ss.str(); } /*@formatter:on*/ template <class T> string out_m2(mvec<mvec<mvec<T>>> &a, ll H = inf, ll W = inf, ll U = inf) { stringstream ss; if (H == inf) H = 6; H = min(H, sz(a)); rep(i, -H, H) { ss << endl; ss << out_m2(a[i], W, U, i); } return ss.str(); } /*@formatter:off*/ template <class T> string out_m2(T &a) { stringstream ss; ss << a; return ss.str(); } /*@formatter:on*/ 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(); } /*@formatter:off*/ 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(); } 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 template <class T> void outl(vector<T> &a, int n = inf) { rep(i, min(n, sz(a))) cout << a[i] << endl; } //テーブルをスペースなしで出力 template <class T> void outt(vector<vector<T>> &a) { rep(i, sz(a)) { rep(j, sz(a[i])) { cout << a[i][j]; } cout << endl; } } // int型をbit表記で出力 void outb(int a) { cout << bitset<20>(a) << endl; } template <class T> void na(vector<T> &a, ll n) { a.resize(n); rep(i, n) cin >> a[i]; } #define dna(a, n) \ vi a(n); \ rep(dnai, n) cin >> a[dnai]; #define dnad(a, n) \ vi a(n); \ rep(dnai, n) cin >> a[dnai], a[dnai]--; template <class T> void nao(vector<T> &a, ll n) { a.resize(n + 1); a[0] = 0; rep(i, n) cin >> a[i + 1]; } template <class T> void naod(vector<T> &a, ll n) { a.resize(n + 1); a[0] = 0; rep(i, n) cin >> a[i + 1], a[i + 1]--; } template <class T> void nad(vector<T> &a, ll n) { a.resize(n); rep(i, n) cin >> a[i], a[i]--; } template <class T, class U> void na2(vector<T> &a, vector<U> &b, ll n) { a.resize(n); b.resize(n); rep(i, n) cin >> a[i] >> b[i]; } #define dna2(a, b, n) \ vi a(n), b(n); \ rep(dna2i, n) cin >> a[dna2i] >> b[dna2i]; template <class T, class U> void nao2(vector<T> &a, vector<U> &b, ll n) { a.resize(n + 1); b.resize(n + 1); a[0] = b[0] = 0; rep(i, n) cin >> a[i + 1] >> b[i + 1]; } #define dna2d(a, b, n) \ vi a(n), b(n); \ rep(dna2di, n) { \ cin >> a[dna2di] >> b[dna2di]; \ a[dna2di]--, b[dna2di]--; \ } template <class T, class U> void na2d(vector<T> &a, vector<U> &b, ll n) { a.resize(n); b.resize(n); rep(i, n) cin >> a[i] >> b[i], a[i]--, b[i]--; } template <class T, class U, class W> void na3(vector<T> &a, vector<U> &b, vector<W> &c, ll n) { a.resize(n); b.resize(n); c.resize(n); rep(i, n) cin >> a[i] >> b[i] >> c[i]; } #define dna3(a, b, c, n) \ vi a(n), b(n), c(n); \ rep(dna3i, n) cin >> a[dna3i] >> b[dna3i] >> c[dna3i]; template <class T, class U, class W> void na3d(vector<T> &a, vector<U> &b, vector<W> &c, ll 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 dna3d(a, b, c, n) \ vi a(n), b(n), c(n); \ rep(dna3di, n) { \ cin >> a[dna3di] >> b[dna3di] >> c[dna3di]; \ a[dna3di]--, b[dna3di]--, c[dna3di]--; \ } template <class T, class U, class W, class X> void na4(vector<T> &a, vector<U> &b, vector<W> &c, vector<X> &d, ll n) { a.resize(n); b.resize(n); c.resize(n); d.resize(n); rep(i, n) cin >> a[i] >> b[i] >> c[i] >> d[i]; } #define dna4(a, b, c, d, n) \ vi a(n), b(n), c(n), d(n); \ rep(dna4i, n) cin >> a[dna4i] >> b[dna4i] >> c[dna4i] >> d[dna4i]; #define dna4d(a, b, c, d, n) \ vi a(n), b(n), c(n), d(n); \ rep(dna4i, n) cin >> a[dna4i] >> b[dna4i] >> c[dna4i] >> d[dna4i], \ --a[dna4i], --b[dna4i], --c[dna4i], --d[dna4i]; #define nt(a, h, w) \ resize(a, h, w); \ rep(nthi, h) rep(ntwi, w) cin >> a[nthi][ntwi]; #define ntd(a, h, w) \ resize(a, h, w); \ rep(ntdhi, h) rep(ntdwi, w) cin >> a[ntdhi][ntdwi], a[ntdhi][ntdwi]--; #define ntp(a, h, w) \ resize(a, h + 2, w + 2); \ fill(a, '#'); \ rep(ntphi, 1, h + 1) rep(ntpwi, 1, w + 1) cin >> a[ntphi][ntpwi]; //デバッグ #define sp << " " << #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__) #ifdef _DEBUG #define deb(...) \ cerr << over10(__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' //@formatter:off //よく使うクラス、構造体 // graphでredefineしているため、書き換えたら向こうも書き換える struct unionfind { vector<ll> par; vector<ll> siz; vector<ll> es; ll n, trees; //連結グループの数(親の種類) unionfind(ll n) : n(n), trees(n) { par.resize(n); siz.resize(n); es.resize(n); for (ll i = 0; i < n; i++) { par[i] = i; siz[i] = 1; } } ll root(ll x) { if (par[x] == x) { return x; } else { return par[x] = root(par[x]); } } ll operator()(ll x) { return root(x); } bool unite(ll x, ll y) { x = root(x); y = root(y); es[x]++; if (x == y) return false; if (siz[x] > siz[y]) swap(x, y); trees--; par[x] = y; siz[y] += siz[x]; es[y] += es[x]; return true; } bool same(ll x, ll y) { return root(x) == root(y); } ll size(ll x) { return siz[root(x)]; } ll esize(ll x) { return es[root(x)]; } vi sizes() { vi cou(n); vi ret; ret.reserve(n); rep(i, n) { cou[root(i)]++; } rep(i, n) { if (cou[i]) ret.push_back(cou[i]); } return ret; } //つながりを無向グラフと見なし、xが閉路に含まれるか判定 bool close(ll x) { return esize(x) >= size(x); } vector<vi> sets() { vi ind(n, -1); ll i = 0; vvi(res, trees); rep(j, n) { ll r = root(j); if (ind[r] == -1) ind[r] = i++; res[ind[r]].push_back(j); } rep(i, trees) { ll r = root(res[i][0]); if (res[i][0] == r) continue; rep(j, 1, sz(res[i])) { if (res[i][j] == r) { swap(res[i][0], res[i][j]); break; } } } return res; } }; //@formatter:off using bll = __int128; using u32 = unsigned; using u64 = unsigned long long; using u128 = __uint128_t; std::ostream &operator<<(std::ostream &dest, __int128_t value) { std::ostream::sentry s(dest); if (s) { __uint128_t tmp = value < 0 ? -value : value; char buffer[128]; char *d = std::end(buffer); do { --d; *d = "0123456789"[tmp % 10]; tmp /= 10; } while (tmp != 0); if (value < 0) { --d; *d = '-'; } ll len = std::end(buffer) - d; if (dest.rdbuf()->sputn(d, len) != len) { dest.setstate(std::ios_base::badbit); } } return dest; } //__int128 toi128(string &s) { __int128 ret = 0; for (ll i = 0; i < //s.length(); ++i) if ('0' <= s[i] && s[i] <= '9') ret = 10 * //ret + s[i] - '0'; return ret;} //エラー 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 re() { assert(0 == 1); exit(0); } void tle() { while (inf) cout << inf << endl; } //便利関数 //テスト用 #define rand xor128_ unsigned long xor128_(void) { static unsigned long x = 123456789, y = 362436069, z = 521288629, w = 88675123; unsigned long t; t = (x ^ (x << 11)); x = y; y = z; z = w; return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8))); } char ranc() { return (char)('a' + rand() % 26); } ll rand(ll min, ll max) { assert(min <= max); if (min >= 0 && max >= 0) { return rand() % (max + 1 - min) + min; } else if (max < 0) { return -rand(-max, -min); } else { if (rand() % 2) { return rand(0, max); } else { return -rand(0, -min); } } } vi ranv(ll n, ll min, ll max) { vi v(n); rep(i, n) v[i] = rand(min, max); return v; } str ransu(ll n) { str s; rep(i, n) s += (char)rand('A', 'Z'); return s; } str ransl(ll n) { str s; rep(i, n) s += (char)rand('a', 'z'); return s; } //単調増加 vi ranvinc(ll n, ll min, ll max) { vi v(n); bool bad = 1; while (bad) { bad = 0; v.resize(n); rep(i, n) { if (i && min > max - v[i - 1]) { bad = 1; break; } if (i) v[i] = v[i - 1] + rand(min, max - v[i - 1]); else v[i] = rand(min, max); } } return v; } //便利 汎用 void ranvlr(ll n, ll min, ll max, vi &l, vi &r) { l.resize(n); r.resize(n); rep(i, n) { l[i] = rand(min, max); r[i] = l[i] + rand(0, max - l[i]); } } template <class T> vector<pair<T, int>> run_length(vector<T> &a) { vector<pair<T, int>> 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 <class F> ll mgr(ll ok, ll ng, F f) { bool han = true; if (ok < ng) while (ng - ok > 1) { ll mid = (ok + ng) >> 1; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; deb(mid, han); } else while (ok - ng > 1) { ll mid = (ok + ng) >> 1; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; deb(mid, han); } return ok; } template <class F> dou mgrd(dou ok, dou ng, F f, int kai = 100) { bool han = true; if (ok < ng) rep(i, kai) { dou mid = (ok + ng) / 2; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; deb(mid, han); } else rep(i, kai) { dou mid = (ok + ng) / 2; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; deb(mid, han); } return ok; } // strを整数として比較 string smax(str &a, str b) { if (sz(a) < sz(b)) { return b; } else if (sz(a) > sz(b)) { return a; } else if (a < b) return b; else return a; } // strを整数として比較 string smin(str &a, str b) { if (sz(a) > sz(b)) { return b; } else if (sz(a) < sz(b)) { return a; } else if (a > b) return b; else return a; } //エラー-1 template <typename W, typename T> ll find(vector<W> &a, int l, const T key) { rep(i, l, sz(a)) if (a[i] == key) return i; return -1; } template <typename W, typename T> ll find(vector<W> &a, const T key) { rep(i, sz(a)) if (a[i] == key) return i; return -1; } template <typename W, typename T> P find(vector<vector<W>> &a, const T key) { rep(i, sz(a)) rep(j, sz(a[0])) if (a[i][j] == key) return mp(i, j); return mp(-1, -1); } template <typename W, typename U> T find(vector<vector<vector<W>>> &a, const U key) { rep(i, sz(a)) rep(j, sz(a[0])) rep(k, sz(a[0][0])) if (a[i][j][k] == key) return mt(i, j, k); return mt(-1, -1, -1); } // stringも書く int find(string &s, const string key) { int klen = sz(key); rep(i, sz(s) - klen + 1) { if (s[i] != key[0]) continue; if (s.substr(i, klen) == key) { return i; } } return -1; } int find(string &s, int l, const string key) { int klen = sz(key); rep(i, l, sz(s) - klen + 1) { if (s[i] != key[0]) continue; if (s.substr(i, klen) == key) { return i; } } return -1; } int find(string &s, const char key) { rep(i, sz(s)) { if (s[i] == key) return i; } return -1; } int find(string &s, int l, const char key) { rep(i, l, sz(s)) { if (s[i] == key) return i; } return -1; } template <typename W, typename T> ll count2(W &a, const T k) { return a == k; } template <typename W, typename T> ll count2(vector<W> &a, const T k) { ll ret = 0; fora(v, a) ret += count2(v, k); return ret; } template <typename W, typename T> ll count(vector<W> &a, const T k) { ll ret = 0; fora(v, a) ret += count2(v, k); return ret; } vi count(vi &a) { int ma = 0; fora(v, a) { if (ma < v) ma = v; } vi res(ma + 1); fora(v, a) { res[v]++; } return res; } ll count(str &a, str k) { ll ret = 0, len = k.length(); auto pos = a.find(k); while (pos != string::npos) pos = a.find(k, pos + len), ++ret; return ret; } vi count(str &a) { vi cou(26); char c = 'a'; if ('A' <= a[0] && a[0] <= 'Z') c = 'A'; rep(i, sz(a))++ cou[a[i] - c]; return cou; } #define couif count_if // algorythm 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)); } template <class U> void rev(vector<vector<U>> &a) { vector<vector<U>> b(sz(a[0]), vector<U>(sz(a))); rep(h, sz(a)) rep(w, sz(a[0])) b[w][h] = a[h][w]; a = b; } void rev(string &a) { reverse(all(a)); } constexpr ll p10[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000ll, 100000000000ll, 1000000000000ll, 10000000000000ll, 100000000000000ll, 1000000000000000ll, 10000000000000000ll, 100000000000000000ll, 1000000000000000000ll}; ll get(ll a, ll keta) { return (a / (ll)pow(10, keta)) % 10; } // 0は0桁 ll keta(ll v) { if (v < p10[9]) { if (v < p10[4]) { if (v < p10[2]) { if (v < p10[1]) { if (v < p10[0]) return 0; else return 1; } else return 2; } else { if (v < p10[3]) return 3; else return 4; } } else { if (v < p10[7]) { if (v < p10[5]) return 5; else if (v < p10[6]) return 6; else return 7; } else { if (v < p10[8]) return 8; else return 9; } } } else { if (v < p10[13]) { if (v < p10[11]) { if (v < p10[10]) return 10; else return 11; } else { if (v < p10[12]) return 12; else return 13; } } else { if (v < p10[15]) { if (v < p10[14]) return 14; else return 15; } else { if (v < p10[17]) { if (v < p10[16]) return 16; else return 17; } else { if (v < p10[18]) return 18; else return 19; } } } } } ll dsum(ll v, ll sin = 10) { ll ret = 0; for (; v; v /= sin) ret += v % sin; return ret; } ll mask10(ll v) { return p10[v] - 1; } //変換系 //[v] := iとなるようなvectorを返す //存在しない物は-1 template <class T> auto keys(T &a) { vector<decltype((a.begin())->fi)> res; for (auto &&k : a) res.push_back(k.fi); return res; } template <class T> auto values(T &a) { vector<decltype((a.begin())->se)> res; for (auto &&k : a) res.push_back(k.se); return res; } template <class T, class U> bool chma(T &a, const U &b) { if (a < b) { a = b; return true; } return false; } template <class T, class U> bool chmi(T &a, const U &b) { if (b < a) { a = b; return true; } return false; } template <class T> constexpr T min(T a, signed b) { return a < b ? a : b; } template <class T> constexpr T max(T a, signed b) { return a < b ? b : a; } template <class T> constexpr T min(T a, T b, T c) { return a >= b ? b >= c ? c : b : a >= c ? c : a; } template <class T> constexpr T max(T a, T b, T c) { return a <= b ? b <= c ? c : b : a <= c ? c : a; } template <class T> T min(vector<T> &a) { return *min_element(all(a)); } template <class T> T mini(vector<T> &a) { return min_element(all(a)) - a.begin(); } template <class T> T min(vector<T> &a, ll n) { return *min_element(a.begin(), a.begin() + min(n, sz(a))); } template <class T> T min(vector<T> &a, ll s, ll n) { return *min_element(a.begin() + s, a.begin() + min(n, sz(a))); } template <class T> T max(vector<T> &a) { return *max_element(all(a)); } template <class T, class U> T max(vector<T> &a, vector<U> &b) { return max(*max_element(all(a)), *max_element(all(b))); } template <class T> T maxi(vector<T> &a) { return max_element(all(a)) - a.begin(); } template <class T> T max(vector<T> &a, ll n) { return *max_element(a.begin(), a.begin() + min(n, sz(a))); } template <class T> T max(vector<T> &a, ll s, ll n) { return *max_element(a.begin() + s, a.begin() + min(n, sz(a))); } template <typename A, size_t N> A max(A (&a)[N]) { A res = a[0]; rep(i, N) res = max(res, a[i]); return res; } template <typename A, size_t N, size_t O> A max(A (&a)[N][O]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P> A max(A (&a)[N][O][P]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A max(A (&a)[N][O][P][Q], const T &v) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A max(A (&a)[N][O][P][Q][R]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A max(A (&a)[N][O][P][Q][R][S]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N> A min(A (&a)[N]) { A res = a[0]; rep(i, N) res = min(res, a[i]); return res; } template <typename A, size_t N, size_t O> A min(A (&a)[N][O]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P> A min(A (&a)[N][O][P]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A min(A (&a)[N][O][P][Q], const T &v) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A min(A (&a)[N][O][P][Q][R]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A min(A (&a)[N][O][P][Q][R][S]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <class T> T sum(vector<T> &v, ll s, ll t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += v[i]; return ret; } template <class T> T sum(vector<T> &v, ll t = inf) { return sum(v, 0, t); } template <class T> T sum(vector<vector<T>> &v, int s, int t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<T>> &v, int t = inf) { return sum(v, 0, t); } template <class T> T sum(vector<vector<vector<T>>> &v, int s, int t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<T>>> &v, int t = inf) { return sum(v, 0, t); } template <class T> T sum(vector<vector<vector<vector<T>>>> &v, int s, int t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<vector<T>>>> &v, int t = inf) { return sum(v, 0, t); } template <class T> T sum(vector<vector<vector<vector<vector<T>>>>> &v, int s, int t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<vector<vector<T>>>>> &v, int t = inf) { return sum(v, 0, t); } template <class T> auto sum(priority_queue<T, vector<T>, greater<T>> &r) { auto q = r; T ret = 0; while (sz(q)) { ret += q.top(); q.pop(); } return ret; } template <class T> auto sum(priority_queue<T> &r) { auto q = r; T ret = 0; while (sz(q)) { ret += q.top(); q.pop(); } return ret; } // template<class T, class U, class... W> auto sumn(vector<T> &v, U head, W... // tail) { auto ret = sum(v[0], tail...); rep(i, 1, min(sz(v), head))ret += // sum(v[i], tail...); return ret;} vi v_i(vi &a) { int n = max(a) + 1; vi ret(n, -1); rep(i, sz(a)) { ret[a[i]] = i; } return ret; } void clear(PQ &q) { q = PQ(); } void clear(priority_queue<int> &q) { q = priority_queue<int>(); } template <class T> void clear(queue<T> &q) { while (q.size()) q.pop(); } template <class T> T *negarr(ll size) { T *body = (T *)malloc((size * 2 + 1) * sizeof(T)); return body + size; } template <class T> T *negarr2(ll h, ll w) { double **dummy1 = new double *[2 * h + 1]; double *dummy2 = new double[(2 * h + 1) * (2 * w + 1)]; dummy1[0] = dummy2 + w; for (ll i = 1; i <= 2 * h + 1; ++i) { dummy1[i] = dummy1[i - 1] + 2 * w + 1; } double **a = dummy1 + h; return a; } // 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> vector<T> ima(vector<T> &v) { vector<T> ret = v; rep(i, sz(ret) - 1) chma(ret[i + 1], ret[i]); return ret; } // template<class T> vector<T> rimi(vector<T> &v) { vector<T> ret = v; rer(i, // sz(ret) - 1, 1)chmi(ret[i - 1], ret[i]); return ret;} template<class T> // vector<T> rima(vector<T> &v) { vector<T> ret = v; rer(i, sz(ret) - 1, // 1)chma(ret[i - 1], ret[i]); return ret;} template <class T> struct ruiC { vector<T> rui; ruiC(vector<T> &ru) : rui(ru) {} /*先頭0*/ ruiC() : rui(1, 0) {} T operator()(ll l, ll r) { if (l > r) { cerr << "ruic "; deb(l, r); assert(0); } return rui[r] - rui[l]; } T operator()(int r) { return operator()(0, r); } /*ruiv[]をruic[]に変えた際意味が変わるのがまずいため()と統一*/ /*単体iを返す 累積でないことに注意(seg木との統一でこうしている)*/ // T operator[](ll i) { return rui[i + 1] - rui[i]; } T operator[](ll i) { return rui[i]; } /*0から順に追加される必要がある*/ void operator+=(T v) { rui.push_back(rui.back() + v); } void add(int i, T v) { if (sz(rui) - 1 != i) ole(); operator+=(v); } T back() { return rui.back(); } ll size() { return rui.size(); } auto begin() { return rui.begin(); } auto end() { return rui.end(); } }; template <class T> struct ruimax { template <typename Monoid> struct SegmentTree { /*pairで処理*/ int sz; vector<Monoid> seg; const Monoid M1 = mp(MIN(T), -1); Monoid f(Monoid a, Monoid b) { return max(a, b); } void build(vector<T> &a) { int n = sz(a); sz = 1; while (sz < n) sz <<= 1; seg.assign(2 * sz, M1); rep(i, n) { seg[i + sz] = mp(a[i], i); } for (int k = sz - 1; k > 0; k--) { seg[k] = f(seg[k << 1], seg[(k << 1) | 1]); } } Monoid query(int a, int b) { Monoid L = M1, R = M1; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) L = f(L, seg[a++]); if (b & 1) R = f(seg[--b], R); } return f(L, R); } Monoid operator[](const int &k) const { return seg[k + sz]; } }; private: vector<T> ve; SegmentTree<pair<T, int>> seg; vector<T> rv; vector<T> ri; bool build = false; public: int n; ruimax(vector<T> &a) : ve(a), n(sz(a)) { int index = -1; int ma = MIN(T); rv.resize(n + 1); ri.resize(n + 1); rv[0] = -linf; ri[0] = -1; rep(i, n) { if (chma(ma, a[i])) { index = i; } rv[i + 1] = ma; ri[i + 1] = index; } } T operator()(int l, int r) { if (!(l <= r && 0 <= l && r <= n)) { deb(l, r, n); assert(0); } if (l == 0) { return rv[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).first; } } T operator()(int r = inf) { return operator()(0, min(r, n)); } T operator[](int r) { return operator()(0, r); } T getv(int l, int r) { return operator()(l, r); } T geti(int l, int r) { assert(l <= r && 0 <= l && r <= n); if (l == 0) { return ri[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).second; } } T geti(int r = inf) { return geti(0, min(r, n)); }; T getv(int r = inf) { return getv(0, min(r, n)); }; auto begin() { return rv.begin(); } auto end() { return rv.end(); } }; template <class T> struct ruimin { template <typename Monoid> struct SegmentTree { /*pairで処理*/ int sz; vector<Monoid> seg; const Monoid M1 = mp(MAX(T), -1); Monoid f(Monoid a, Monoid b) { return min(a, b); } void build(vector<T> &a) { int n = sz(a); sz = 1; while (sz < n) sz <<= 1; seg.assign(2 * sz, M1); rep(i, n) { seg[i + sz] = mp(a[i], i); } for (int k = sz - 1; k > 0; k--) { seg[k] = f(seg[k << 1], seg[(k << 1) | 1]); } } Monoid query(int a, int b) { Monoid L = M1, R = M1; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) L = f(L, seg[a++]); if (b & 1) R = f(seg[--b], R); } return f(L, R); } Monoid operator[](const int &k) const { return seg[k + sz]; } }; private: vector<T> ve; SegmentTree<pair<T, int>> seg; vector<T> rv; vector<T> ri; bool build = false; int n; public: ruimin(vector<T> &a) : ve(a), n(sz(a)) { int index = -1; int mi = MAX(T); rv.resize(n + 1); ri.resize(n + 1); rv[0] = linf; ri[0] = -1; rep(i, n) { if (chmi(mi, a[i])) { index = i; } rv[i + 1] = mi; ri[i + 1] = index; } } T operator()(int l, int r) { assert(l <= r && 0 <= l && r <= n); if (l == 0) { return rv[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).first; } } T operator()(int r = inf) { return operator()(0, min(r, n)); } T operator[](int r) { return operator()(0, r); } T getv(int l, int r) { return operator()(l, r); } T geti(int l, int r) { assert(l <= r && 0 <= l && r <= n); if (l == 0) { return ri[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).second; } } T geti(int r = inf) { return geti(0, min(r, n)); }; T getv(int r = inf) { return getv(0, min(r, n)); }; auto begin() { return rv.begin(); } auto end() { return rv.end(); } }; 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() { return ruiC<T>(); } template <class T> ruiC<T> ruic(vector<T> &a) { vector<T> ret = ruiv(a); return ruiC<T>(ret); } vvi() ruib(vi &a) { vvi(res, 61, sz(a) + 1); rep(k, 61) { rep(i, sz(a)) { res[k][i + 1] = res[k][i] + ((a[i] >> k) & 1); } } return res; } vector<ruiC<int>> ruibc(vi &a) { vector<ruiC<int>> ret(61); vvi(res, 61, sz(a)); rep(k, 61) { rep(i, sz(a)) { res[k][i] = (a[i] >> k) & 1; } ret[k] = ruic(res[k]); } return 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); } // h query template <class T> vector<T> imoh(vector<vector<T>> &v, int w) { vector<T> ret(sz(v)); rep(h, sz(ret)) { ret[h] = v[h][w]; } rep(i, sz(ret) - 1) { ret[i + 1] += ret[i]; } return ret; } template <class T> vector<T> ruih(vector<vector<T>> &v, int w) { vector<T> ret(sz(v) + 1); rep(h, sz(v)) { ret[h + 1] = v[h][w]; } rep(i, sz(v)) { ret[i + 1] += ret[i]; } return ret; } template <class T> ruiC<T> ruihc(vector<vector<T>> &a, int w) { vector<T> ret = ruih(a, w); return ruiC<T>(ret); } // xor template <class T> struct ruixC { 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> struct rruiC { vector<T> rui; int n; rruiC(vector<T> &a) : n(sz(a)) { rui.resize(n + 1); rer(i, n - 1) { rui[i] = rui[i + 1] + a[i]; } } /*[r l)*/ T operator()(int r, int l) { r++; l++; assert(l <= r && l >= 0 && r <= n); return rui[l] - rui[r]; } T operator()(int l) { return operator()(n - 1, l); } T operator[](int i) { return operator()(i); } }; template <class T> ostream &operator<<(ostream &os, rruiC<T> a) { fora(v, a.rui) os << v << " "; return os; } #define rrui rruic template <class T> rruiC<T> rruic(vector<T> &a) { return rruiC<T>(a); } //掛け算 template <class T> struct ruimulC { vector<T> rv; int n; ruimulC(vector<T> &a) : rv(a), n(sz(a)) { rv.resize(n + 1); rv[0] = 1; rep(i, n) { rv[i + 1] = a[i] * rv[i]; } } ruimulC() : n(0) { rv.resize(n + 1); rv[0] = 1; } void operator+=(T v) { rv.push_back(rv.back() * v); n++; } T operator()(int l, int r) { assert(l <= r && 0 <= l && r <= n); return rv[r] / rv[l]; } T operator()(int r = inf) { return operator()(0, min(r, n)); } T operator[](int r) { return operator()(0, r); } auto begin() { return rv.begin(); } auto end() { return rv.end(); } }; template <class T> ruimulC<T> ruimul(vector<T> &a) { return ruimulC<T>(a); } template <class T> ruimulC<T> ruimul() { vector<T> a; return ruimulC<T>(a); } /*@formatter:off*/ 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> void inc(pair<T, U> &a, U v = 1) { a.first += v, a.second += v; } template <class T, class U> void inc(T &a, U v = 1) { a += v; } template <class T, class U> void inc(vector<T> &a, U v = 1) { for (auto &u : a) inc(u, v); } template <class T, class U> void dec(T &a, U v = 1) { a -= v; } template <class T, class U> void dec(vector<T> &a, U v = 1) { for (auto &u : a) dec(u, v); } template <class U> void dec(string &a, U v = 1) { for (auto &u : a) dec(u, v); } template <class T, class U, class W> void dec(vector<T> &a, vector<U> &b, W v = 1) { for (auto &u : a) dec(u, v); for (auto &u : b) dec(u, v); } template <class T, class U, class W> void dec(vector<T> &a, vector<U> &b, vector<W> &c) { for (auto &u : a) dec(u, 1); for (auto &u : b) dec(u, 1); for (auto &u : c) dec(u, 1); } bool ins(ll h, ll w, ll H, ll W) { return h >= 0 && w >= 0 && h < H && w < W; } bool ins(ll l, ll v, ll r) { return l <= v && v < r; } template <class T> bool ins(vector<T> &a, ll i, ll j = 0) { return ins(0, i, sz(a)) && ins(0, j, sz(a)); } #define inside ins ll u(ll a) { return a < 0 ? 0 : a; } template <class T> vector<T> u(vector<T> &a) { vector<T> ret = a; fora(v, ret) v = u(v); return ret; } //添え字を返す template <class F> ll goldd_l(ll left, ll right, F calc) { double GRATIO = 1.6180339887498948482045868343656; ll lm = left + (ll)((right - left) / (GRATIO + 1.0)); ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); ll fl = calc(lm); ll fr = calc(rm); while (right - left > 10) { if (fl < fr) { right = rm; rm = lm; fr = fl; lm = left + (ll)((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } ll minScore = MAX(ll); ll resIndex = left; for (ll i = left; i < right + 1; ++i) { ll score = calc(i); if (minScore > score) { minScore = score; resIndex = i; } } return resIndex; } template <class F> ll goldt_l(ll left, ll right, F calc) { double GRATIO = 1.6180339887498948482045868343656; ll lm = left + (ll)((right - left) / (GRATIO + 1.0)); ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); ll fl = calc(lm); ll fr = calc(rm); while (right - left > 10) { if (fl > fr) { right = rm; rm = lm; fr = fl; lm = left + (ll)((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } if (left > right) { ll l = left; left = right; right = l; } ll maxScore = MIN(ll); ll resIndex = left; for (ll i = left; i < right + 1; ++i) { ll score = calc(i); if (maxScore < score) { maxScore = score; resIndex = i; } } return resIndex; } /*loopは200にすればおそらく大丈夫 余裕なら300に*/ template <class F> dou goldd_d(dou left, dou right, F calc, ll loop = 200) { dou GRATIO = 1.6180339887498948482045868343656; dou lm = left + ((right - left) / (GRATIO + 1.0)); dou rm = lm + ((right - lm) / (GRATIO + 1.0)); dou fl = calc(lm); dou fr = calc(rm); /*200にすればおそらく大丈夫*/ /*余裕なら300に*/ ll k = 141; loop++; while (--loop) { if (fl < fr) { right = rm; rm = lm; fr = fl; lm = left + ((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + ((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } return left; } template <class F> dou goldt_d(dou left, dou right, F calc, ll loop = 200) { double GRATIO = 1.6180339887498948482045868343656; dou lm = left + ((right - left) / (GRATIO + 1.0)); dou rm = lm + ((right - lm) / (GRATIO + 1.0)); dou fl = calc(lm); dou fr = calc(rm); loop++; while (--loop) { if (fl > fr) { right = rm; rm = lm; fr = fl; lm = left + ((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + ((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } return left; } // l ~ rを複数の区間に分割し、極致を与えるiを返す time-20 msまで探索 template <class F> ll goldd_ls(ll l, ll r, F calc, ll time = 2000) { auto lim = milliseconds(time - 20); ll mini = 0, minv = MAX(ll); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/ ll nl = l; ll nr = l + haba; rep(i, k) { ll ni = goldd_l(nl, nr, calc); if (chmi(minv, calc(ni))) mini = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return mini; } template <class F> ll goldt_ls(ll l, ll r, F calc, ll time = 2000) { auto lim = milliseconds(time - 20); ll maxi = 0, maxv = MIN(ll); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/ ll nl = l; ll nr = l + haba; rep(i, k) { ll ni = goldt_l(nl, nr, calc); if (chma(maxv, calc(ni))) maxi = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return maxi; } template <class F> dou goldd_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を持つ*/ auto lim = milliseconds(time - 20); dou mini = 0, minv = MAX(dou); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); dou haba = (r - l) / k; dou nl = l; dou nr = l + haba; rep(i, k) { dou ni = goldd_d(nl, nr, calc); if (chmi(minv, calc(ni))) mini = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return mini; } template <class F> dou goldt_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を残している*/ auto lim = milliseconds(time - 20); dou maxi = 0, maxv = MIN(dou); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); dou haba = (r - l) / k; dou nl = l; dou nr = l + haba; rep(i, k) { dou ni = goldt_d(nl, nr, calc); if (chma(maxv, calc(ni))) maxi = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return maxi; } template <class T> T min(vector<vector<T>> &a) { T res = MAX(T); rep(i, a.size()) chmi(res, *min_element(all(a[i]))); return res; } template <class T> T max(vector<vector<T>> &a) { T res = MIN(T); rep(i, a.size()) chma(res, *max_element(all(a[i]))); return res; } constexpr bool bget(ll m, ll keta) { #ifdef _DEBUG assert(keta <= 62); //オーバーフロー 1^62までしか扱えない #endif return (m >> keta) & 1; } // bget(n)次元 // NならN-1まで vector<vi> bget2(vi &a, int keta_size) { vvi(res, keta_size, sz(a)); rep(k, keta_size) { rep(i, sz(a)) { res[k][i] = bget(a[i], k); } } return res; } vi bget1(vi &a, int keta) { vi res(sz(a)); rep(i, sz(a)) { res[i] = bget(a[i], keta); } return res; } ll bget(ll m, ll keta, ll sinsuu) { m /= (ll)pow(sinsuu, keta); return m % sinsuu; } constexpr ll bit(ll n) { #ifdef _DEBUG assert(n <= 62); //オーバーフロー 1^62までしか扱えない #endif return (1LL << (n)); } ll bit(ll n, ll sinsuu) { return (ll)pow(sinsuu, n); } ll mask(ll n) { return (1ll << n) - 1; } // aをbitに置きなおす //{0, 2} -> 101 ll biti(vi &a) { int m = 0; for (auto &&v : a) m |= bit(v); return m; } //{1, 1, 0} -> 011 ll bitb(vi &a) { int m = 0; rep(i, sz(a)) if (a[i]) m |= bit(i); return m; } #define bcou __builtin_popcountll //最下位ビット ll lbit(ll n) { return n & -n; } ll lbiti(ll n) { return log2(n & -n); } //最上位ビット ll hbit(ll n) { n |= (n >> 1); n |= (n >> 2); n |= (n >> 4); n |= (n >> 8); n |= (n >> 16); n |= (n >> 32); return n - (n >> 1); } ll hbiti(ll n) { return log2(hbit(n)); } ll hbitk(ll n) { ll k = 0; rer(i, 5) { ll a = k + (1ll << i); ll b = 1ll << a; if (b <= n) k += 1ll << i; } return k; } //初期化は0を渡す ll nextComb(ll &mask, ll n, ll r) { if (!mask) return mask = (1LL << r) - 1; ll x = mask & -mask; /*最下位の1*/ ll y = mask + x; /*連続した下の1を繰り上がらせる*/ ll res = ((mask & ~y) / x >> 1) | y; if (bget(res, n)) return mask = 0; else return mask = res; } // n桁以下でビットがr個立っているもののvectorを返す vi bitCombList(ll n, ll r) { vi res; ll m = 0; while (nextComb(m, n, r)) { res.push_back(m); } return res; } // masの立ってるindexを見る #define forbit(i, mas) \ for (int forbitj = lbit(mas), forbitm = mas, i = log2(forbitj); forbitm; \ forbitm = forbitj ? forbitm ^ forbitj : 0, forbitj = lbit(forbitm), \ i = log2(forbitj)) char itoal(ll i) { return 'a' + i; } char itoaL(ll i) { return 'A' + i; } ll altoi(char c) { if ('A' <= c && c <= 'Z') return c - 'A'; return c - 'a'; } ll ctoi(char c) { return c - '0'; } char itoc(ll i) { return i + '0'; } ll vtoi(vi &v) { ll res = 0; if (sz(v) > 18) { debugline("vtoi"); deb(sz(v)); ole(); } rep(i, sz(v)) { res *= 10; res += v[i]; } return res; } vi itov(ll i) { vi res; while (i) { res.push_back(i % 10); i /= 10; } rev(res); return res; } vi stov(string &a) { ll n = sz(a); vi ret(n); rep(i, n) { ret[i] = a[i] - '0'; } return ret; } //基準を満たさないものは0になる vi stov(string &a, char one) { ll n = sz(a); vi ret(n); rep(i, n) ret[i] = a[i] == one; return ret; } vector<vector<ll>> ctoi(vector<vector<char>> s, char c) { ll n = sz(s), m = sz(s[0]); vector<vector<ll>> res(n, vector<ll>(m)); rep(i, n) rep(j, m) res[i][j] = s[i][j] == c; return res; } #define unique(v) v.erase(unique(v.begin(), v.end()), v.end()); //[i] := vを返す // aは0~n-1で置き換えられる vi compress(vi &a) { vi b; ll len = a.size(); for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { a[i] = lower_bound(all(b), a[i]) - b.begin(); } ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, umap<ll, ll> &map) { vi b; ll len = a.size(); for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { ll v = a[i]; a[i] = lower_bound(all(b), a[i]) - b.begin(); map[v] = a[i]; } ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, vi &r) { vi b; ll len = a.size(); fora(v, a) b.push_back(v); fora(v, r) b.push_back(v); sort(b); unique(b); for (ll i = 0; i < len; ++i) a[i] = lower_bound(all(b), a[i]) - b.begin(); for (ll i = 0; i < sz(r); ++i) r[i] = lower_bound(all(b), r[i]) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, vi &r, vi &s) { vi b; ll len = a.size(); fora(v, a) b.push_back(v); fora(v, r) b.push_back(v); fora(v, s) b.push_back(v); sort(b); unique(b); for (ll i = 0; i < len; ++i) a[i] = lower_bound(all(b), a[i]) - b.begin(); for (ll i = 0; i < sz(r); ++i) r[i] = lower_bound(all(b), r[i]) - b.begin(); for (ll i = 0; i < sz(s); ++i) r[i] = lower_bound(all(b), s[i]) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vector<vi> &a) { vi b; fora(vv, a) fora(v, vv) b.push_back(v); sort(b); unique(b); fora(vv, a) fora(v, vv) v = lower_bound(all(b), v) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vector<vector<vi>> &a) { vi b; fora(vvv, a) fora(vv, vvv) fora(v, vv) b.push_back(v); sort(b); unique(b); fora(vvv, a) fora(vv, vvv) fora(v, vv) v = lower_bound(all(b), v) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } void compress(ll a[], ll len) { vi b; for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { a[i] = lower_bound(all(b), a[i]) - b.begin(); } } //要素が見つからなかったときに困る #define binarySearch(a, v) (binary_search(all(a), v)) #define lowerIndex(a, v) (lower_bound(all(a), v) - a.begin()) #define upperIndex(a, v) (upper_bound(all(a), v) - a.begin()) #define rlowerIndex(a, v) (upper_bound(all(a), v) - a.begin() - 1) #define rupperIndex(a, v) (lower_bound(all(a), v) - a.begin() - 1) template <class T, class U, class W> T lowerBound(vector<T> &a, U v, W banpei) { auto it = lower_bound(a.begin(), a.end(), v); if (it == a.end()) return banpei; else return *it; } template <class T, class U, class W> T lowerBound(ruiC<T> &a, U v, W banpei) { return lowerBound(a.rui, v, banpei); } template <class T, class U, class W> T upperBound(vector<T> &a, U v, W banpei) { auto it = upper_bound(a.begin(), a.end(), v); if (it == a.end()) return banpei; else return *it; } template <class T, class U, class W> T upperBound(ruiC<T> &a, U v, W banpei) { return upperBound(a.rui, v, banpei); } template <class T, class U, class W> T rlowerBound(vector<T> &a, U v, W banpei) { auto it = upper_bound(a.begin(), a.end(), v); if (it == a.begin()) return banpei; else { return *(--it); } } template <class T, class U, class W> T rlowerBound(ruiC<T> &a, U v, W banpei) { return rlowerBound(a.rui, v, banpei); } template <class T, class U, class W> T rupperBound(vector<T> &a, U v, W banpei) { auto it = lower_bound(a.begin(), a.end(), v); if (it == a.begin()) return banpei; else { return *(--it); } } template <class T, class U, class W> T rupperBound(ruiC<T> &a, U v, W banpei) { return rupperBound(a.rui, v, banpei); } #define next2(a) next(next(a)) #define prev2(a) prev(prev(a)) //狭義の単調増加列 長さを返す template <class T> int lis(vector<T> &a) { int n = sz(a); vi tail(n + 1, MAX(T)); rep(i, n) { int id = lowerIndex(tail, a[i]); /**/ tail[id] = a[i]; } return lowerIndex(tail, MAX(T)); } template <class T> int lis_eq(vector<T> &a) { int n = sz(a); vi tail(n + 1, MAX(T)); rep(i, n) { int id = upperIndex(tail, a[i]); /**/ tail[id] = a[i]; } return lowerIndex(tail, MAX(T)); } // iteratorを返す // valueが1以上の物を返す 0は見つけ次第削除 // vを減らす場合 (*it).se--でいい template <class T, class U, class V> auto lower_map(map<T, U> &m, V k) { auto ret = m.lower_bound(k); while (ret != m.end() && (*ret).second == 0) { ret = m.erase(ret); } return ret; } template <class T, class U, class V> auto upper_map(map<T, U> &m, V k) { auto ret = m.upper_bound(k); while (ret != m.end() && (*ret).second == 0) { ret = m.erase(ret); } return ret; } //存在しなければエラー template <class T, class U, class V> auto rlower_map(map<T, U> &m, V k) { auto ret = upper_map(m, k); assert(ret != m.begin()); ret--; while (1) { if ((*ret).second != 0) break; assert(ret != m.begin()); auto next = ret; --next; m.erase(ret); ret = next; } return ret; } template <class T, class U, class V> auto rupper_map(map<T, U> &m, V k) { auto ret = lower_map(m, k); assert(ret != m.begin()); ret--; while (1) { if ((*ret).second != 0) break; assert(ret != m.begin()); auto next = ret; --next; m.erase(ret); ret = next; } return ret; } template <class... T> void fin(T... s) { out(s...); exit(0); } //便利 数学 math // sub ⊂ top bool subset(int sub, int top) { return (sub & top) == sub; } //-180 ~ 180 degree double atand(double h, double w) { return atan2(h, w) / PI * 180; } //% -mの場合、最小の正の数を返す ll mod(ll a, ll m) { if (m < 0) m *= -1; return (a % m + m) % m; } ll pow(ll a) { return a * a; }; ll fact(ll v) { return v <= 1 ? 1 : v * fact(v - 1); } dou factd(int v) { static vd fact(2, 1); if (sz(fact) <= v) { rep(i, sz(fact), v + 1) { fact.push_back(fact.back() * i); } } return fact[v]; } ll comi(ll n, ll r) { assert(n < 100); static vvi(pas, 100, 100); if (pas[0][0]) return pas[n][r]; pas[0][0] = 1; rep(i, 1, 100) { pas[i][0] = 1; rep(j, 1, i + 1) pas[i][j] = pas[i - 1][j - 1] + pas[i - 1][j]; } return pas[n][r]; } double comd2(ll n, ll r) { static vvd(comb, 2020, 2020); if (comb[0][0] == 0) { comb[0][0] = 1; rep(i, 2000) { comb[i + 1][0] = 1; rep(j, 1, i + 2) { comb[i + 1][j] = comb[i][j] + comb[i][j - 1]; } } } return comb[n][r]; } double comd(int n, int r) { if (r < 0 || r > n) return 0; if (n < 2020) return comd2(n, r); static vd fact(2, 1); if (sz(fact) <= n) { rep(i, sz(fact), n + 1) { fact.push_back(fact.back() * i); } } return fact[n] / fact[n - r] / fact[r]; } ll gcd(ll a, ll b) { while (b) a %= b, swap(a, b); return abs(a); } ll gcd(vi b) { ll res = b[0]; rep(i, 1, sz(b)) res = gcd(b[i], res); return res; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll lcm(vi a) { ll res = a[0]; rep(i, 1, sz(a)) res = lcm(a[i], res); return res; } ll ceil(ll a, ll b) { if (b == 0) { debugline("ceil"); deb(a, b); ole(); return -1; } else if (a < 0) { return 0; } else { return (a + b - 1) / b; } } ll sig0(ll t) { return t <= 0 ? 0 : ((1 + t) * t) >> 1; } // ll sig(ll s, ll t) { return ((s + t) * (t - s + 1)) >> 1; } ll sig(ll s, ll t) { if (s > t) swap(s, t); return sig0(t - s) + s * (t - s + 1); } #define tousa_i tosa_i #define lower_tousa_i lower_tosa_i #define upper_tousa upper_tosa #define upper_tousa_i upper_tosa_i ll tosa_i(ll st, ll ad, ll v) { assert(((v - st) % ad) == 0); return (v - st) / ad; } ll tosa_s(ll st, ll ad, ll len) { return st * len + sig0(len - 1) * ad; } // ax + r (x は非負整数) で表せる整数のうち、v 以上となる最小の整数 ll lower_tosa(ll st, ll ad, ll v) { if (st >= v) return st; return (v - st + ad - 1) / ad * ad + st; } //第何項か ll lower_tosa_i(ll st, ll ad, ll v) { if (st >= v) return 0; return (v - st + ad - 1) / ad; } ll upper_tosa(ll st, ll ad, ll v) { return lower_tosa(st, ad, v + 1); } ll upper_tosa_i(ll st, ll ad, ll v) { return lower_tosa_i(st, ad, v + 1); } // v * v >= aとなる最小のvを返す 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); } /*@formatter:off*/ //機能拡張 #define dtie(a, b) \ int a, b; \ tie(a, b) template <class T, class U> string to_string(T a, U b) { string res = ""; res += a; res += b; return res; } template <class T, class U, class V> string to_string(T a, U b, V c) { string res = ""; res += a; res += b; res += c; return res; } template <class T, class U, class V, class W> string to_string(T a, U b, V c, W d) { string res = ""; res += a; res += b; res += c; res += d; return res; } template <class T, class U, class V, class W, class X> string to_string(T a, U b, V c, W d, X e) { string res = ""; res += a; res += b; res += c; res += d; res += e; return res; } constexpr int bsetlen = k5 * 2; // constexpr int bsetlen = 5050; #define bset bitset<bsetlen> bool operator<(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] < b[i]) return true; if (a[i] > b[i]) return false; } return false; } bool operator>(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] > b[i]) return true; if (a[i] < b[i]) return false; } return false; } bool operator<=(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] < b[i]) return true; if (a[i] > b[i]) return false; } return true; } bool operator>=(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] > b[i]) return true; if (a[i] < b[i]) return false; } return true; } string operator~(string &a) { string res = a; for (auto &&c : res) { if (c == '0') c = '1'; else if (c == '1') c = '0'; else { cerr << "cant ~" << a << "must bit" << endl; exit(0); } } return res; } ostream &operator<<(ostream &os, bset &a) { bitset<10> b; vi list; rep(i, bsetlen) { if (a[i]) list.push_back(i), b[i] = 1; } os << b << ", " << list; return os; } int hbiti(bset &a) { rer(i, bsetlen) { if (a[i]) return i; } return -1; } #define hk(a, b, c) (a <= b && b < c) // O(N/64) bset nap(bset &a, int v) { bset r = a | a << v; return r; } bset nap(bset &a, bset &v) { bset r = a; rep(i, bsetlen) { if (v[i]) r |= a << i; } return r; } template <class T> void seth(vector<vector<T>> &S, int w, vector<T> &v) { assert(sz(S) == sz(v)); assert(w < sz(S[0])); rep(h, sz(S)) { S[h][w] = v[h]; } } template <class T, class U> void operator+=(pair<T, U> &a, pair<T, U> &b) { a.fi += b.fi; a.se += b.se; } template <class T, class U> pair<T, U> operator+(pair<T, U> &a, pair<T, U> &b) { return pair<T, U>(a.fi + b.fi, a.se + b.se); } template <typename CharT, typename Traits, typename Alloc> basic_string<CharT, Traits, Alloc> operator+(const basic_string<CharT, Traits, Alloc> &lhs, const int rv) { basic_string<CharT, Traits, Alloc> str(lhs); str.append(to_string(rv)); return str; } template <typename CharT, typename Traits, typename Alloc> void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const int rv) { lhs += to_string(rv); } template <typename CharT, typename Traits, typename Alloc> basic_string<CharT, Traits, Alloc> operator+(const basic_string<CharT, Traits, Alloc> &lhs, const signed rv) { basic_string<CharT, Traits, Alloc> str(lhs); str.append(to_string(rv)); return str; } template <typename CharT, typename Traits, typename Alloc> void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const signed rv) { lhs += to_string(rv); } template <typename CharT, typename Traits, typename Alloc> void operator*=(basic_string<CharT, Traits, Alloc> &s, int num) { auto bek = s; s = ""; for (; num; num >>= 1) { if (num & 1) { s += bek; } bek += bek; } } template <class T, class U> void operator+=(queue<T> &a, U v) { a.push(v); } template <class T, class U> void operator+=(deque<T> &a, U v) { a.push_back(v); } template <class T> priority_queue<T, vector<T>, greater<T>> & operator+=(priority_queue<T, vector<T>, greater<T>> &a, vector<T> &v) { fora(d, v) a.push(d); return a; } template <class T, class U> priority_queue<T, vector<T>, greater<T>> & operator+=(priority_queue<T, vector<T>, greater<T>> &a, U v) { a.push(v); return a; } template <class T, class U> priority_queue<T> &operator+=(priority_queue<T> &a, U v) { a.push(v); return a; } template <class T> set<T> &operator+=(set<T> &a, vector<T> v) { fora(d, v) a.insert(d); return a; } template <class T, class U> auto operator+=(set<T> &a, U v) { return a.insert(v); } template <class T, class U> auto operator-=(set<T> &a, U v) { return a.erase(v); } template <class T, class U> auto operator+=(mset<T> &a, U v) { return a.insert(v); } template <class T, class U> set<T, greater<T>> &operator+=(set<T, greater<T>> &a, U v) { a.insert(v); return a; } template <class T, class U> vector<T> &operator+=(vector<T> &a, U v) { a.push_back(v); return a; } template <class T, class U> vector<T> operator+(vector<T> &a, U v) { vector<T> ret = a; ret += v; return ret; } template <class T, class U> vector<T> operator+(U v, vector<T> &a) { vector<T> ret = a; ret.insert(ret.begin(), v); return ret; } template <class T> vector<T> operator+(vector<T> a, vector<T> b) { vector<T> ret; ret = a; fora(v, b) ret += v; return ret; } template <class T> vector<T> &operator+=(vector<T> &a, vector<T> &b) { rep(i, sz(b)) { /*こうしないとa+=aで両辺が増え続けてバグる*/ a.push_back(b[i]); } return a; } template <class T, class U> map<T, U> &operator+=(map<T, U> &a, map<T, U> &b) { fora(bv, b) { a[bv.first] += bv.second; } return a; } template <class T> vector<T> &operator-=(vector<T> &a, vector<T> &b) { if (sz(a) != sz(b)) { debugline("vector<T> operator-="); deb(a); deb(b); exit(0); } rep(i, sz(a)) a[i] -= b[i]; return a; } template <class T> vector<T> operator-(vector<T> &a, vector<T> &b) { if (sz(a) != sz(b)) { debugline("vector<T> operator-"); deb(a); deb(b); ole(); } vector<T> res(sz(a)); rep(i, sz(a)) res[i] = a[i] - b[i]; return res; } template <class T, class U> void operator*=(vector<T> &a, U b) { vector<T> ta = a; rep(b - 1) { a += ta; } } template <typename T> void erase(vector<T> &v, unsigned ll i) { v.erase(v.begin() + i); } template <typename T> void erase(vector<T> &v, unsigned ll s, unsigned ll e) { v.erase(v.begin() + s, v.begin() + e); } template <class T, class U> void erase(map<T, U> &m, ll okl, ll ngr) { m.erase(m.lower_bound(okl), m.lower_bound(ngr)); } template <class T> void erase(set<T> &m, ll okl, ll ngr) { m.erase(m.lower_bound(okl), m.lower_bound(ngr)); } template <typename T> void erasen(vector<T> &v, unsigned ll s, unsigned ll n) { v.erase(v.begin() + s, v.begin() + s + n); } template <typename T, typename U> void insert(vector<T> &v, unsigned ll i, U t) { v.insert(v.begin() + i, t); } template <typename T, typename U> void push_front(vector<T> &v, U t) { v.insert(v.begin(), t); } template <typename T, typename U> void insert(vector<T> &v, unsigned ll i, vector<T> list) { for (auto &&va : list) v.insert(v.begin() + i++, va); } template <typename T> void insert(set<T> &v, vector<T> list) { for (auto &&va : list) v.insert(va); } vector<string> split(const string a, const char deli) { string b = a + deli; ll l = 0, r = 0, n = b.size(); vector<string> res; rep(i, n) { if (b[i] == deli) { r = i; if (l < r) res.push_back(b.substr(l, r - l)); l = i + 1; } } return res; } vector<string> split(const string a, const string deli) { vector<string> res; ll kn = sz(deli); std::string::size_type Pos(a.find(deli)); ll l = 0; while (Pos != std::string::npos) { if (Pos - l) res.push_back(a.substr(l, Pos - l)); l = Pos + kn; Pos = a.find(deli, Pos + kn); } if (sz(a) - l) res.push_back(a.substr(l, sz(a) - l)); return res; } 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 fAb(bool a) { if (a) cout << "Alice" << endl; else cout << "Bob"; } 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 <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)}; } //未分類 // 0,2,1 1番目と2番目の次元を入れ替える template <class T> auto irekae(vector<vector<vector<T>>> &A, int x, int y, int z) { #define irekae_resize_loop(a, b, c) \ resize(res, a, b, c); \ rep(i, a) rep(j, b) rep(k, c) vector<vector<vector<T>>> res; if (x == 0 && y == 1 && z == 2) { res = A; } else if (x == 0 && y == 2 && z == 1) { irekae_resize_loop(sz(A), sz(A[0][0]), sz(A[0])) { res[i][j][k] = A[i][k][j]; } } else if (x == 1 && y == 0 && z == 2) { irekae_resize_loop(sz(A[0]), sz(A), sz(A[0][0])) { res[i][j][k] = A[j][i][k]; } } else if (x == 1 && y == 2 && z == 0) { irekae_resize_loop(sz(A[0]), sz(A[0][0]), sz(A)) { res[i][j][k] = A[k][i][j]; } } else if (x == 2 && y == 0 && z == 1) { irekae_resize_loop(sz(A[0][0]), sz(A), sz(A[0])) { res[i][j][k] = A[j][k][i]; } } else if (x == 2 && y == 1 && z == 0) { irekae_resize_loop(sz(A[0][0]), sz(A[0]), sz(A)) { res[i][j][k] = A[k][j][i]; } } return res; #undef irekae_resize_loop } template <class T> auto irekae(vector<vector<T>> &A, int i = 1, int j = 0) { vvt(res, sz(A[0]), sz(A)); rep(i, sz(A)) { rep(j, sz(A[0])) { res[j][i] = A[i][j]; } } return res; } // tou分割する template <typename T> vector<vector<T>> cut(vector<T> &a, int tou = 2) { int N = sz(a); vector<vector<T>> res(tou); int hab = N / tou; vi lens(tou, hab); rep(i, N % tou) { lens[tou - 1 - i]++; } int l = 0; rep(i, tou) { int len = lens[i]; int r = l + len; res[i].resize(len); std::copy(a.begin() + l, a.begin() + r, res[i].begin()); l = r; } return res; } //長さn毎に分割する template <typename T> vector<vector<T>> cutn(vector<T> &a, int len) { int N = sz(a); vector<vector<T>> res(ceil(N, len)); vi lens(N / len, len); if (N % len) lens.push_back(N % len); int l = 0; rep(i, sz(lens)) { int len = lens[i]; int r = l + len; res[i].resize(len); std::copy(a.begin() + l, a.begin() + r, res[i].begin()); l = r; } return res; } vi inds_(vi &a) { int n = sz(a); vb was(n); vi res(n); rep(i, n) { assert(!was[a[i]]); res[a[i]] = i; was[a[i]] = true; } return res; } //@起動時 struct initon { initon() { cin.tie(0); ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(16); srand((unsigned)clock() + (unsigned)time(NULL)); }; } initonv; #define pre prev #define nex next // gra mll pr //上下左右 const string udlr = "udlr"; string UDLR = "UDLR"; // x4と連動 UDLR.find('U') := x4[0] //右、上が正 constexpr ll h4[] = {1, -1, 0, 0}; constexpr ll w4[] = {0, 0, -1, 1}; constexpr ll h8[] = {0, 1, 0, -1, -1, 1, 1, -1}; constexpr ll w8[] = {1, 0, -1, 0, 1, -1, 1, -1}; int mei_inc(int h, int w, int H, int W, int i) { while (++i < 4) { if (inside(h + h4[i], w + w4[i], H, W)) return i; } return i; } #define mei(nh, nw, h, w) \ for (int i = mei_inc(h, w, H, W, -1), nh = i < 4 ? h + h4[i] : 0, \ nw = i < 4 ? w + w4[i] : 0; \ i < 4; i = mei_inc(h, w, H, W, i), nh = h + h4[i], nw = w + w4[i]) //グラフ内で #undef getid //#define getidとしているため、ここを書き直したらgraphも書き直す #define getid_2(h, w) (h * W + w) #define getid_1(p) (p.first * W + p.second) #define getid(...) over2(__VA_ARGS__, getid_2, getid_1)(__VA_ARGS__) #define getp(id) mp(id / W, id % W) //#define set_shuffle() std::random_device seed_gen;std::mt19937 //engine(seed_gen()) #define shuffle(a) std::shuffle((a).begin(), (a).end(), //engine); 1980 開始からtime ms経っていたらtrue bool timeup(int time) { auto end_time = system_clock::now(); auto part = duration_cast<milliseconds>(end_time - start_time); auto lim = milliseconds(time); return part >= lim; } void set_time() { past_time = system_clock::now(); } // MS型(millisecqnds)で返る // set_timeをしてからの時間 auto calc_time_milli() { auto now = system_clock::now(); auto part = duration_cast<milliseconds>(now - past_time); return part; } auto calc_time_micro() { auto now = system_clock::now(); auto part = duration_cast<microseconds>(now - past_time); return part; } auto calc_time_nano() { auto now = system_clock::now(); auto part = duration_cast<nanoseconds>(now - past_time); return part; } bool calc_time(int zikan) { return calc_time_micro() >= microseconds(zikan); } using MS = std::chrono::microseconds; int div(microseconds a, microseconds b) { return a / b; } int div(nanoseconds a, nanoseconds b) { if (b < nanoseconds(1)) { return a / nanoseconds(1); } int v = a / b; return v; } // set_time(); // rep(i,lim)shori // lim*=time_nanbai(); // rep(i,lim)shoriと使う //全体でmilliかかっていいときにlimを何倍してもう一回できるかを返す int time_nanbai(int milli) { auto dec = duration_cast<nanoseconds>(past_time - start_time); auto part = calc_time_nano(); auto can_time = nanoseconds(milli * 1000 * 1000); can_time -= part; can_time -= dec; return div(can_time, part); } /*@formatter:on*/ // vectorで取れる要素数 // bool=> 1e9 * 8.32 // int => 1e8 * 2.6 // ll => 1e8 * 1.3 // 3次元以上取るとメモリがヤバい // static配列を使う vvc(ba); ll N, M, H, W; vi A, B, C; //遅い 文字列以外も扱えるはず template <class T = string> struct rolling_hash2 { //@formatter:off const int b[2] = {11731, 11839}; const int m[2] = {1000002571LL, 1000002631LL}; int n; T s; // stringやvector vector<long long> hash[2], pow[2]; rolling_hash2() {} rolling_hash2(T &S) : s(S) { n = (int)S.size(); for (int i = 0; i < 2; ++i) { hash[i].assign(n + 1, 0); pow[i].assign(n + 1, 1); for (int j = 0; j < n; ++j) { hash[i][j + 1] = (hash[i][j] * b[i] + S[j]) % m[i]; pow[i][j + 1] = pow[i][j] * b[i] % m[i]; } } } // get hash of S[left:right] ll get(int l, int r, int id) { ll res = hash[id][r] - hash[id][l] * pow[id][r - l] % m[id]; if (res < 0) res += m[id]; return res; } ll get(int l, int r) { return (get(l, r, 0) << 32) | get(l, r, 1); } ll operator()(int l, int r) { return get(l, r, 0); } ll operator()() { return get(0, n, 0); } // get lcp of S[a:] and S[b:] int lcp(int a, int b) { int len = min((int)s.size() - a, (int)s.size() - b); int low = -1, high = len + 1; while (high - low > 1) { int mid = (low + high) / 2; if (get(a, a + mid, 0) != get(b, b + mid, 0)) high = mid; else if (get(a, a + mid, 1) != get(b, b + mid, 1)) high = mid; else low = mid; } return low; } // get lcp of S[a:] and T[b:] int lcp(rolling_hash2 &t, int a, int b) { int len = min((int)s.size() - a, (int)s.size() - b); int low = -1, high = len + 1; while (high - low > 1) { int mid = (low + high) / 2; if (get(a, a + mid, 0) != get(b, b + mid, 0)) high = mid; else if (get(a, a + mid, 1) != get(b, b + mid, 1)) high = mid; else low = mid; } return low; } }; //早い struct rolling_hash { static constexpr int base1 = 1007, base2 = 2009; static constexpr int mod1 = 1000000007, mod2 = 1000000009; vector<long long> hash1, hash2, power1, power2; int n; rolling_hash(string &S) { n = (int)S.size(); hash1.assign(n + 1, 0); hash2.assign(n + 1, 0); power1.assign(n + 1, 1); power2.assign(n + 1, 1); for (int i = 0; i < n; ++i) { hash1[i + 1] = (hash1[i] * base1 + S[i]) % mod1; hash2[i + 1] = (hash2[i] * base2 + S[i]) % mod2; power1[i + 1] = (power1[i] * base1) % mod1; power2[i + 1] = (power2[i] * base2) % mod2; } } template <class T> rolling_hash(vector<T> &S) { n = (int)S.size(); hash1.assign(n + 1, 0); hash2.assign(n + 1, 0); power1.assign(n + 1, 1); power2.assign(n + 1, 1); for (int i = 0; i < n; ++i) { hash1[i + 1] = (hash1[i] * base1 + S[i]) % mod1; hash2[i + 1] = (hash2[i] * base2 + S[i]) % mod2; power1[i + 1] = (power1[i] * base1) % mod1; power2[i + 1] = (power2[i] * base2) % mod2; } } inline pair<int, int> get(int l, int r) { long long res1 = hash1[r] - hash1[l] * power1[r - l] % mod1; if (res1 < 0) res1 += mod1; long long res2 = hash2[r] - hash2[l] * power2[r - l] % mod2; if (res2 < 0) res2 += mod2; return {res1, res2}; } inline pair<int, int> operator()(int l, int r) { return get(l, r); } inline pair<int, int> operator()() { return get(0, n); } // get lcp of S[a:] and T[b:] //接頭語がどれだけ一致するか //範囲の重複を含む inline int lcp_dup(int a, int b) { int len = min((int)hash1.size() - a, (int)hash1.size() - b); int low = 0, high = len; while (high - low > 1) { int mid = (low + high) >> 1; if (get(a, a + mid) != get(b, b + mid)) high = mid; else low = mid; } return low; } //重複を認めない inline int lcp_not(int a, int b) { return min(lcp_dup(a, b), b - a); } }; // hashを共有したくない場合、中に書く const uint64_t base = rand((uint64_t)868491463, ((uint64_t)1 << 61) - 1 - 1); //安全で爆速なrollinghash class roll { static constexpr uint64_t mask30 = ((uint64_t)1 << 30) - 1; static constexpr uint64_t mask31 = ((uint64_t)1 << 31) - 1; static constexpr uint64_t mask61 = ((uint64_t)1 << 61) - 1; static constexpr uint64_t offset = mask61 * 7; static constexpr uint64_t mod(const uint64_t x) { const uint64_t y = (x >> 61) + (x & mask61); return y >= mask61 ? y - mask61 : y; } static constexpr uint64_t mul(const uint64_t x, const uint64_t y) { const uint64_t xh = x >> 31, xl = x & mask31; const uint64_t yh = y >> 31, yl = y & mask31; const uint64_t z = xl * yh + xh * yl; const uint64_t zh = z >> 30, zl = z & mask30; return xh * yh * 2 + zh + (zl << 31) + xl * yl; } const std::size_t siz; vector<uint64_t> pow, h; public: template <typename T> roll(T &A) : siz(sz(A)), pow(siz + 1, 1), h(siz + 1, 0) { for (std::size_t i = 1; i <= siz; i++) { pow[i] = mod(mul(pow[i - 1], base)), h[i] = mod(mul(h[i - 1], base) + A[i - 1]); } } uint64_t operator()(const std::size_t l, const std::size_t r) { return mod(h[r] + offset - mul(h[l], pow[r - l])); } uint64_t operator()(int r = inf) { return operator()(0, min(siz, r)); } // get lcp of S[a:] and T[b:] //接頭語がどれだけ一致するか //範囲の重複を含む int lcp_dup(int a, int b) { int len = min((int)siz - a, (int)siz - b); int low = 0, high = len; while (high - low > 1) { int mid = (low + high) >> 1; if (operator()(a, a + mid) != operator()(b, b + mid)) high = mid; else low = mid; } return low; } //重複を認めない int lcp_not(int a, int b) { return min(lcp_dup(a, b), b - a); } //@formatter:on }; //動かない場合roll2 // roll ro(list); void solve() { cin >> N; vi A(N), B(N), A2(N), B2(N); rep(i, 0, N) cin >> A[i]; rep(i, 0, N) cin >> B[i]; rep(i, 0, N) A2[i] = A[i] ^ A[(i + 1) % N]; rep(i, 0, N) B2[i] = B[i] ^ B[(i + 1) % N]; vector<signed> v; rep(i, 0, N) v.push_back(A2[i]); rep(i, 0, N) v.push_back(A2[i]); rep(i, 0, N) v.push_back(B2[i]); roll rh(v); rep(k, 0, N) if (rh(k, k + N) == rh(N * 2, N * 2 + N)) { int x = A[k] ^ B[0]; out(k, x); } } auto my(ll n, vi &a) { return 0; } auto sister(ll n, vi &a) { ll ret = 0; return ret; } signed main() { solve(); #define arg n, a #ifdef _DEBUG bool bad = 0; for (ll i = 0, ok = 1; i < k5 && ok; ++i) { ll n = rand(1, 8); vi a = ranv(n, 1, 10); auto myres = my(arg); auto res = sister(arg); ok = myres == res; if (!ok) { out(arg); cerr << "AC : " << res << endl; cerr << "MY : " << myres << endl; bad = 1; break; } } if (!bad) { // cout << "完璧 : solveを書き直そう" << endl; // cout << " : そして、solve()を呼び出すのだ" << endl; // cout << " : cin>>n; na(a,n);も忘れるな" << endl; } #endif return 0; };
[ "variable_declaration.type.primitive.change", "expression.operation.binary.remove" ]
744,038
744,039
u986437843
cpp
p02816
//#undef _DEBUG //#pragma GCC optimize("Ofast") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace std::chrono; #define int long long // todo 消したら動かない intの代わりにsignedを使う #define ll long long auto start_time = system_clock::now(); auto past_time = system_clock::now(); #define debugName(VariableName) #VariableName //最大引数がN #define over2(o1, o2, name, ...) name #define over3(o1, o2, o3, name, ...) name #define over4(o1, o2, o3, o4, name, ...) name #define over5(o1, o2, o3, o4, o5, name, ...) name #define over6(o1, o2, o3, o4, o5, o6, name, ...) name #define over7(o1, o2, o3, o4, o5, o6, o7, name, ...) name #define over8(o1, o2, o3, o4, o5, o6, o7, o8, name, ...) name #define over9(o1, o2, o3, o4, o5, o6, o7, o8, o9, name, ...) name #define over10(o1, o2, o3, o4, o5, o6, o7, o8, o9, o10, name, ...) name /*@formatter:off*/ //[-n, n)にアクセスできる //また、外部関数resizeに渡せる // sizeは[-n, n)でnを返す template <class T> class mvec { vector<T> v; int n; public: mvec() : n(0), v(0) {} mvec(int n) : n(n), v(n * 2) {} mvec(int n, T val) : n(n), v(n * 2, val) {} auto &operator[](int i) { return v[i + n]; } auto size() { return n; } void resize(int sn) { assert(n == 0); n = sn; v.resize(sn * 2); } auto begin() { return v.begin(); } auto rbegin() { return v.rbegin(); } auto end() { return v.end(); } auto rend() { return v.rend(); } }; //[]でboolは参照を返さないため特殊化が必要 template <> struct mvec<bool> { vector<bool> v; int n; mvec() : n(0), v(0) {} mvec(int n) : n(n), v(n * 2) {} mvec(int n, bool val) : n(n), v(n * 2, val) {} auto operator[](int i) { return v[i + n]; } auto size() { return v.size(); } void resize(int sn) { assert(n == 0); n = sn; v.resize(sn * 2); } auto begin() { return v.begin(); } auto rbegin() { return v.rbegin(); } auto end() { return v.end(); } auto rend() { return v.rend(); } }; template <class T> ostream &operator<<(ostream &os, mvec<T> &a) { int spa = 3; for (auto &&v : a) { spa = max(spa, (int)(to_string(v).size()) + 1); } int n = (int)a.size(); os << endl; for (int i = -n; i < n; i++) { int need = spa - ((int)to_string(i).size()); if (i == -n) { need -= min(need, spa - ((int)to_string(a[i]).size())); } while (need--) { os << " "; } os << i; } os << endl; int i = -n; for (auto &&v : a) { int need = spa - ((int)to_string(v).size()); if (i == -n) { need -= min(need, spa - ((int)to_string(i).size())); } while (need--) { os << " "; } os << v; i++; } return os; } #define mv mvec #define MV mvec using mvi = mvec<ll>; using mvb = mvec<bool>; using mvs = mvec<string>; using mvd = mvec<double>; using mvc = mvec<char>; #define mvvt0(t) mvec<mvec<t>> #define mvvt1(t, a) mvec<mvec<t>> a #define mvvt2(t, a, b) mvec<mvec<t>> a(b) #define mvvt3(t, a, b, c) mvec<mvec<t>> a(b, mvec<t>(c)) #define mvvt4(t, a, b, c, d) mvec<mvec<t>> a(b, mvec<t>(c, d)) #define mvvi(...) \ over4(__VA_ARGS__, mvvt4, mvvt3, mvvt2, mvvt1, mvvt0)(ll, __VA_ARGS__) template <typename T> mvec<T> make_mv(size_t a) { return mvec<T>(a); } template <typename T, typename... Ts> auto make_mv(size_t a, Ts... ts) { return mvec<decltype(make_mv<T>(ts...))>(a, make_mv<T>(ts...)); } #define mvni(name, ...) auto name = make_mv<ll>(__VA_ARGS__) #ifdef _DEBUG // https://marycore.jp/prog/cpp/class-extension-methods/ 違うかも template <class T, class A = std::allocator<T>> struct debtor : std::vector<T, A> { using std::vector<T, A>::vector; template <class U> int deb_v(U a, int v) { return v; } template <class U> int deb_v(debtor<U> &a, int v = 0) { cerr << a.size() << " "; return deb_v(a.at(0), v + 1); } template <class U> void deb_o(U a) { cerr << a << " "; } template <class U> void deb_o(debtor<U> &a) { for (int i = 0; i < min((int)a.size(), 15ll); i++) { deb_o(a[i]); } if ((int)a.size() > 15) { cerr << "..."; } cerr << endl; } typename std::vector<T>::reference operator[](typename std::vector<T>::size_type n) { if (n < 0 || n >= (int)this->size()) { int siz = (int)this->size(); cerr << "vector size = "; int dim = deb_v((*this)); cerr << endl; cerr << "out index at " << n << endl; cerr << endl; if (dim <= 2) { deb_o((*this)); } exit(0); } return this->at(n); } }; #define vector debtor #endif #ifdef _DEBUG //区間削除は出来ない template <class T> struct my_pbds_tree { set<T> s; auto begin() { return s.begin(); } auto end() { return s.end(); } auto rbegin() { return s.rbegin(); } auto rend() { return s.rend(); } auto empty() { return s.empty(); } auto size() { return s.size(); } void clear() { s.clear(); } template <class U> void insert(U v) { s.insert(v); } template <class U> void operator+=(U v) { insert(v); } template <class F> auto erase(F v) { return s.erase(v); } template <class U> auto find(U v) { return s.find(v); } template <class U> auto lower_bound(U v) { return s.lower_bound(v); } template <class U> auto upper_bound(U v) { return s.upper_bound(v); } auto find_by_order(ll k) { auto it = s.begin(); for (ll i = 0; i < k; i++) it++; return it; } auto order_of_key(ll v) { auto it = s.begin(); ll i = 0; for (; it != s.end() && *it < v; i++) it++; return i; } }; #define pbds(T) my_pbds_tree<T> // gp_hash_tableでcountを使えないようにするため template <class T, class U> struct my_unordered_map { unordered_map<T, U> m; my_unordered_map(){}; auto begin() { return m.begin(); } auto end() { return m.end(); } auto cbegin() { return m.cbegin(); } auto cend() { return m.cend(); } template <class V> auto erase(V v) { return m.erase(v); } void clear() { m.clear(); } /*countは gp_hash_tableに存在しない*/ /*!= m.end()*/ template <class V> auto find(V v) { return m.find(v); } template <class V> auto &operator[](V n) { return m[n]; } }; #define unordered_map my_unordered_map #define umapi unordered_map<ll, ll> #define umapp unordered_map<P, ll> #define umapu unordered_map<uint64_t, ll> #define umapip unordered_map<ll, P> #else // umapはunorderd_mapになる // umapiはgp_hash_table // find_by_order(k) k番目のイテレーター // order_of_key(k) k以上が前から何番目か #define pbds(U) \ __gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag, \ __gnu_pbds::tree_order_statistics_node_update> #define umapi __gnu_pbds::gp_hash_table<ll, ll, xorshift> #define umapp __gnu_pbds::gp_hash_table<P, ll, xorshift> #define umapu __gnu_pbds::gp_hash_table<uint64_t, ll, xorshift> #define umapip __gnu_pbds::gp_hash_table<ll, P, xorshift> #endif /*@formatter:on*/ struct xorshift { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } size_t operator()(std::pair<ll, ll> x) const { ll v = ((x.first) << 32) | x.second; static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(v + FIXED_RANDOM); } }; /*@formatter:off*/ template <class U, class L> void operator+=( __gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update> &s, L v) { s.insert(v); } //衝突対策 #define ws ws_ template <class A, class B, class C> struct T2 { A f; B s; C t; T2() { f = 0, s = 0, t = 0; } T2(A f, B s, C t) : f(f), s(s), t(t) {} bool operator<(const T2 &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 ?n s > r.s : t > r.t; 大きい順 */ } bool operator>(const T2 &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 T2 &r) const { return f == r.f && s == r.s && t == r.t; } bool operator!=(const T2 &r) const { return f != r.f || s != r.s || t != r.t; } }; template <class A, class B, class C, class D> struct F2 { A a; B b; C c; D d; F2() { a = 0, b = 0, c = 0, d = 0; } F2(A a, B b, C c, D d) : a(a), b(b), c(c), d(d) {} bool operator<(const F2 &r) const { return a != r.a ? a < r.a : b != r.b ? b < r.b : c != r.c ? c < r.c : d < r.d; /* return a != r.a ? a > r.a : b != r.b ? b > r.b : c != r.c ? c > r.c : d > r.d;*/ } bool operator>(const F2 &r) const { return a != r.a ? a > r.a : b != r.b ? b > r.b : c != r.c ? c > r.c : d > r.d; /* return a != r.a ? a < r.a : b != r.b ? b < r.b : c != r.c ? c < r.c : d < r.d;*/ } bool operator==(const F2 &r) const { return a == r.a && b == r.b && c == r.c && d == r.d; } bool operator!=(const F2 &r) const { return a != r.a || b != r.b || c != r.c || d != r.d; } ll operator[](ll i) { assert(i < 4); return i == 0 ? a : i == 1 ? b : i == 2 ? c : d; } }; typedef T2<ll, ll, ll> T; typedef F2<ll, ll, ll, ll> F; T mt(ll a, ll b, ll c) { return T(a, b, c); } F mf(ll a, ll b, ll c, ll d) { return F(a, b, c, d); } //@マクロ省略系 型,構造 #define double long double #define pow powl using dou = double; const double eps = 1e-9; //基本コメントアウト /* struct epsdou { double v; epsdou(double v = 0) : v(v) {} template<class T> epsdou &operator+=(T b) { v += (double) b; return (*this); } template<class T> epsdou &operator-=(T b) { v -= (double) b; return (*this); } template<class T> epsdou &operator*=(T b) { v *= (double) b; return (*this); } template<class T> epsdou &operator/=(T b) { v /= (double) b; return (*this); } epsdou operator+(epsdou b) { return v + (double) b; } epsdou operator-(epsdou b) { return v - (double) b; } epsdou operator*(epsdou b) { return v * (double) b; } epsdou operator/(epsdou b) { return v / (double) b; } epsdou operator-() const { return epsdou(-v); } template<class T> bool operator<(T b) { return v < (double) b; } template<class T> bool operator>(T b) { return v > (double) b; } template<class T> bool operator==(T b) { return fabs(v - (double) b) <= eps; } template<class T> bool operator<=(T b) { return v < (double) b || fabs(v - b) <= eps; } template<class T> bool operator>=(T b) { return v > (double) b || fabs(v - b) <= eps; } operator double() { return v; }};istream &operator>>(istream &iss, epsdou &a) { iss >> a.v; return iss;}ostream &operator<<(ostream &os, epsdou &a) { os << a.v; return os;} #define eps_conr_t(o) template<class T> epsdou operator o(T b, epsdou a){return a.v o (dou)b;} #define eps_conl_t(o) template<class T> epsdou operator o(epsdou a, T b){return a.v o (dou)b;} eps_conl_t(+)eps_conl_t(-)eps_conl_t(*)eps_conl_t(/)eps_conr_t(+)eps_conr_t(-)eps_conr_t(*)eps_conr_t(/) #undef double #define double epsdou */ #define ull unsigned long long using itn = int; using str = string; using bo = bool; #define au auto using P = pair<ll, ll>; using mvp = mvec<P>; using mvt = mvec<T>; #define MIN(a) numeric_limits<a>::min() #define MAX(a) numeric_limits<a>::max() #define fi first #define se second #define beg begin #define rbeg rbegin #define con continue #define bre break #define brk break #define is == #define el else #define elf else if #define upd update #define sstream stringstream #define maxq 1 #define minq -1 #define ZERO(a) memset(a, 0, sizeof(a)) #define MINUS(a) memset(a, 0xff, sizeof(a)) #define MALLOC(type, len) (type *)malloc((len) * sizeof(type)) #define lam(right) [&](auto &p) { return p right; } //マクロ省略系 コンテナ 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 V vector #define vvt0(t) vector<vector<t>> #define vvt1(t, a) vector<vector<t>> a #define vvt2(t, a, b) vector<vector<t>> a(b) #define vvt3(t, a, b, c) vector<vector<t>> a(b, vector<t>(c)) #define vvt4(t, a, b, c, d) vector<vector<t>> a(b, vector<t>(c, d)) #define vvi(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(ll, __VA_ARGS__) #define vvb(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(bool, __VA_ARGS__) #define vvs(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(string, __VA_ARGS__) #define vvd(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(double, __VA_ARGS__) #define vvc(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(char, __VA_ARGS__) #define vvp(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(P, __VA_ARGS__) #define vvt(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(T, __VA_ARGS__) #define vv(type, ...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(type, __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 vn(type, name, ...) auto name = make_v<type>(__VA_ARGS__) #define PQ priority_queue<ll, vector<ll>, greater<ll>> #define tos to_string using mapi = map<ll, ll>; using mapp = map<P, ll>; using mapd = map<dou, ll>; using mapc = map<char, ll>; using maps = map<str, ll>; using seti = set<ll>; using setd = set<dou>; using setc = set<char>; using sets = set<str>; using qui = queue<ll>; #define uset unordered_set #define useti unordered_set<ll, xorshift> #define mset multiset #define mseti multiset<ll> #define umap unordered_map #define mmap multimap //任意のマクロサポート用 使う度に初期化する int index_, v1_, v2_, v3_; template <class T> struct pq { priority_queue<T, vector<T>, greater<T>> q; /*小さい順*/ T su = 0; void clear() { q = priority_queue<T, vector<T>, greater<T>>(); su = 0; } void operator+=(T v) { su += v; q.push(v); } T sum() { return su; } T top() { return q.top(); } void pop() { su -= q.top(); q.pop(); } T poll() { T ret = q.top(); su -= ret; q.pop(); return ret; } ll size() { return q.size(); } }; template <class T> struct pqg { priority_queue<T> q; /*大きい順*/ T su = 0; void clear() { q = priority_queue<T>(); su = 0; } void operator+=(T v) { su += v; q.push(v); } T sum() { return su; } T top() { return q.top(); } void pop() { su -= q.top(); q.pop(); } T poll() { T ret = q.top(); su -= ret; q.pop(); return ret; } ll size() { return q.size(); } }; #define pqi pq<ll> #define pqgi pqg<ll> //マクロ 繰り返し //↓@オーバーロード隔離 #define rep1(n) for (ll rep1i = 0, rep1lim = n; rep1i < rep1lim; ++rep1i) #define rep2(i, n) for (ll i = 0, rep2lim = n; i < rep2lim; ++i) #define rep3(i, m, n) for (ll i = m, rep3lim = n; i < rep3lim; ++i) #define rep4(i, m, n, ad) for (ll i = m, rep4lim = n; i < rep4lim; i += ad) //逆順 閉区間 #define rer2(i, n) for (ll i = n; i >= 0; i--) #define rer3(i, m, n) for (ll i = m, rer3lim = n; i >= rer3lim; i--) #define rer4(i, m, n, dec) for (ll i = m, rer4lim = n; i >= rer4lim; i -= dec) //ループを一つにまとめないとフォーマットで汚くなるため #define nex_ind1(i) i++ #define nex_ind2(i, j, J) \ i = (j + 1 == J) ? i + 1 : i, j = (j + 1 == J ? 0 : j + 1) #define nex_ind3(i, j, k, J, K) \ i = (j + 1 == J && k + 1 == K) ? i + 1 : i, \ j = (k + 1 == K) ? (j + 1 == J ? 0 : j + 1) : j, \ k = (k + 1 == K ? 0 : k + 1) #define nex_ind4(i, j, k, l, J, K, L) \ i = (j + 1 == J && k + 1 == K && l + 1 == L) ? i + 1 : i, \ j = (k + 1 == K && l + 1 == L) ? (j + 1 == J ? 0 : j + 1) : j, \ k = (l + 1 == L ? (k + 1 == K ? 0 : k + 1) : k), l = l + 1 == L ? 0 : l + 1 #define nex_ind5(i, j, k, l, m, J, K, L, M) \ i = (j + 1 == J && k + 1 == K && l + 1 == L && m + 1 == M) ? i + 1 : i, \ j = (k + 1 == K && l + 1 == L && m + 1 == M) ? (j + 1 == J ? 0 : j + 1) : j, \ k = (l + 1 == L && m + 1 == M ? (k + 1 == K ? 0 : k + 1) : k), \ l = m + 1 == M ? l + 1 == L ? 0 : l + 1 : l, m = m + 1 == M ? 0 : m + 1 #define repss2(i, I) for (int i = 0; i < I; i++) #define repss4(i, j, I, J) \ for (int i = (J ? 0 : I), j = 0; i < I; nex_ind2(i, j, J)) #define repss6(i, j, k, I, J, K) \ for (int i = (J && K ? 0 : I), j = 0, k = 0; i < I; nex_ind3(i, j, k, J, K)) #define repss8(i, j, k, l, I, J, K, L) \ for (int i = (J && K && L ? 0 : I), j = 0, k = 0, l = 0; i < I; \ nex_ind4(i, j, k, l, J, K, L)) #define repss10(i, j, k, l, m, I, J, K, L, M) \ for (int i = (J && K && L && M ? 0 : I), j = 0, k = 0, l = 0, m = 0; i < I; \ nex_ind5(i, j, k, l, m, J, K, L, M)) // i,j,k...をnまで見る #define reps2(i, n) repss2(i, n) #define reps3(i, j, n) repss4(i, j, n, n) #define reps4(i, j, k, n) repss6(i, j, k, n, n, n) #define reps5(i, j, k, l, n) repss8(i, j, k, l, n, n, n, n) template <class T> void nex_repv2(int &i, int &j, int &I, int &J, vector<vector<T>> &s) { while (1) { j++; if (j >= J) { j = 0; i++; if (i < I) { J = (int)s[i].size(); } } if (i >= I || J) return; } } template <class T> void nex_repv3(int &i, int &j, int &k, int &I, int &J, int &K, vector<vector<vector<T>>> &s) { while (1) { k++; if (k >= K) { k = 0; j++; if (j >= J) { j = 0; i++; if (i >= I) return; } } J = (int)s[i].size(); K = (int)s[i][j].size(); if (J && K) return; } } #define repv_2(i, a) repss2(i, sz(a)) //正方形である必要はない //直前を持つのとどっちが早いか #define repv_3(i, j, a) \ for (int I = (int)a.size(), J = (int)a[0].size(), i = 0, j = 0; i < I; \ nex_repv2(i, j, I, J, a)) //箱状になっている事が要求される つまり[i] 次元目の要素数は一定 #define repv_4(i, j, k, a) \ for (int I = (int)a.size(), J = (int)a[0].size(), K = (int)a[0][0].size(), \ i = 0, j = 0, k = 0; \ i < I; nex_repv3(i, j, k, I, J, K, a)) #define repv_5(i, j, k, l, a) \ repss8(i, j, k, l, sz(a), sz(a[0]), sz(a[0][0]), sz(a[0][0][0])) #define repv_6(i, j, k, l, m, a) \ repss10(i, j, k, l, m, sz(a), sz(a[0]), sz(a[0][0]), sz(a[0][0][0]), \ sz(a[0][0][0][0])) template <typename T> struct has_rbegin_rend { private: template <typename U> static auto check(U &&obj) -> decltype(std::rbegin(obj), std::rend(obj), std::true_type{}); static std::false_type check(...); public: static constexpr bool value = decltype(check(std::declval<T>()))::value; }; template <typename T> constexpr bool has_rbegin_rend_v = has_rbegin_rend<T>::value; template <typename Iterator> class Range { public: Range(Iterator &&begin, Iterator &&end) noexcept : m_begin(std::forward<Iterator>(begin)), m_end(std::forward<Iterator>(end)) {} Iterator begin() const noexcept { return m_begin; } Iterator end() const noexcept { return m_end; } private: const Iterator m_begin; const Iterator m_end; }; template <typename Iterator> static inline Range<Iterator> makeRange(Iterator &&begin, Iterator &&end) noexcept { return Range<Iterator>{std::forward<Iterator>(begin), std::forward<Iterator>(end)}; } template <typename T> static inline decltype(auto) makeReversedRange(const std::initializer_list<T> &iniList) noexcept { return makeRange(std::rbegin(iniList), std::rend(iniList)); } template <typename T, typename std::enable_if_t<has_rbegin_rend_v<T>, std::nullptr_t> = nullptr> static inline decltype(auto) makeReversedRange(T &&c) noexcept { return makeRange(std::rbegin(c), std::rend(c)); } /* rbegin(), rend()を持たないものはこっちに分岐させて,エラーメッセージを少なくする*/ template <typename T, typename std::enable_if<!has_rbegin_rend<T>::value, std::nullptr_t>::type = nullptr> static inline void makeReversedRange(T &&) noexcept { static_assert(has_rbegin_rend<T>::value, "Specified argument doesn't have reverse iterator."); } #define form1(st) \ for (auto &&form_it = st.begin(); form_it != st.end(); ++form_it) #define form3(k, v, st) \ for (auto &&form_it = st.begin(); form_it != st.end(); ++form_it) #define form4(k, v, st, r) \ for (auto &&form_it = st.begin(); form_it != st.end() && (*form_it).fi < r; \ ++form_it) #define form5(k, v, st, l, r) \ for (auto &&form_it = st.lower_bound(l); \ form_it != st.end() && (*form_it).fi < r; ++form_it) #define fors1(st) \ for (auto &&fors_it = st.begin(); fors_it != st.end(); ++fors_it) #define fors2(v, st) \ for (auto &&fors_it = st.begin(); fors_it != st.end(); ++fors_it) #define fors3(v, st, r) \ for (auto &&fors_it = st.begin(); fors_it != st.end() && (*fors_it) < r; \ ++fors_it) #define fors4(v, st, l, r) \ for (auto &&fors_it = st.lower_bound(l); \ fors_it != st.end() && (*fors_it) < r; ++fors_it) #define forslr3(st, a, b) \ for (auto &&forslr_it = st.begin(); forslr_it != st.end(); ++forslr_it) #define forslr4(v, st, a, b) \ for (auto &&forslr_it = st.begin(); forslr_it != st.end(); ++forslr_it) #define forslr5(v, st, r, a, b) \ for (auto &&forslr_it = st.begin(); \ forslr_it != st.end() && (*forslr_it) < r; ++forslr_it) #define forslr6(v, st, l, r, a, b) \ for (auto &&forslr_it = st.lower_bound(l); \ forslr_it != st.end() && (*forslr_it) < r; ++forslr_it) template <class U> vector<U> to1d(vector<U> &a) { return a; } template <class U> vector<U> to1d(V<V<U>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) res.push_back(a2); return res; } template <class U> vector<U> to1d(V<V<V<U>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) res.push_back(a3); return res; } template <class U> vector<U> to1d(V<V<V<V<U>>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) for (auto &&a4 : a3) res.push_back(a4); return res; } template <class U> vector<U> to1d(V<V<V<V<V<U>>>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) for (auto &&a4 : a3) for (auto &&a5 : a4) res.push_back(a5); return res; } template <class U> vector<U> to1d(V<V<V<V<V<V<U>>>>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) for (auto &&a4 : a3) for (auto &&a5 : a4) for (auto &&a6 : a5) res.push_back(a6); return res; } #define fora_init_2(a, A) ; #define fora_init_4(a, b, A, B) \ auto &&a = A[fora_i]; \ auto &&b = B[fora_i]; #define fora_init_6(a, b, c, A, B, C) \ auto &&a = A[fora_i]; \ auto &&b = B[fora_i]; \ auto &&c = C[fora_i]; #define fora_init_8(a, b, c, d, A, B, C, D) \ auto &&a = A[fora_i]; \ auto &&b = B[fora_i]; \ auto &&c = C[fora_i]; \ auto &&d = D[fora_i]; #define fora_init(...) \ over8(__VA_ARGS__, fora_init_8, a, fora_init_6, a, fora_init_4, a, \ fora_init_2)(__VA_ARGS__) #define forr_init_2(a, A) auto &&a = A[forr_i]; #define forr_init_4(a, b, A, B) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; #define forr_init_6(a, b, c, A, B, C) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; \ auto &&c = C[forr_i]; #define forr_init_8(a, b, c, d, A, B, C, D) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; \ auto &&c = C[forr_i]; \ auto &&d = D[forr_i]; #define forr_init(...) \ over8(__VA_ARGS__, forr_init_8, a, forr_init_6, a, forr_init_4, a, \ forr_init_2)(__VA_ARGS__) #define forp_init(k, v, ...) \ auto &&k = (*forp_it).fi; \ auto &&v = (*forp_it).se; #define form_init(k, v, ...) \ auto &&k = (*form_it).fi; \ auto &&v = (*form_it).se; #define fors_init(v, ...) auto &&v = (*fors_it); #define forlr_init(a, A, ngl, ngr) \ auto a = A[forlr_i]; \ auto prev = forlr_i ? A[forlr_i - 1] : ngl; \ auto next = forlr_i + 1 < rep2lim ? A[forlr_i + 1] : ngr; #define forslr_init4(a, A, ngl, ngr) \ auto a = (*forslr_it); \ auto prev = (forslr_it != A.begin()) ? (*std::prev(forslr_it)) : ngl; \ auto next = (forslr_it != std::prev(A.end())) ? (*std::next(forslr_it)) : ngr; #define forslr_init5(a, A, r, ngl, ngr) \ auto a = (*forslr_it); \ auto prev = (forslr_it != A.begin()) ? (*std::prev(forslr_it)) : ngl; \ auto next = (forslr_it != std::prev(A.end())) ? (*std::next(forslr_it)) : ngr; #define forslr_init6(a, A, l, r, ngl, ngr) \ auto a = (*forslr_it); \ auto prev = (forslr_it != A.begin()) ? (*std::prev(forslr_it)) : ngl; \ auto next = (forslr_it != std::prev(A.end())) ? (*std::next(forslr_it)) : ngr; #define forslr_init(...) \ over6(__VA_ARGS__, forslr_init6, forslr_init5, forslr_init4)(__VA_ARGS__); #define fora_2(a, A) for (auto &&a : A) #define fora_4(a, b, A, B) rep(fora_i, sz(A)) #define fora_6(a, b, c, A, B, C) rep(fora_i, sz(A)) #define fora_8(a, b, c, d, A, B, C, D) rep(fora_i, sz(A)) #define forr_2(a, A) rer(forr_i, sz(A) - 1) #define forr_4(a, b, A, B) rer(forr_i, sz(A) - 1) #define forr_6(a, b, c, A, B, C) rer(forr_i, sz(A) - 1) #define forr_8(a, b, c, d, A, B, C, D) rer(forr_i, sz(A) - 1) //↑@オーバーロード隔離 // rep系はインデックス、for系は中身 #define rep(...) over4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__) #define rer(...) over4(__VA_ARGS__, rer4, rer3, rer2, )(__VA_ARGS__) // char用のrep #define repc(i, m, n) for (char i = m, repc3lim = n; i < repc3lim; ++i) // i,j,k...をnまで見る #define reps(...) over5(__VA_ARGS__, reps5, reps4, reps3, reps2, )(__VA_ARGS__) #define repss(...) \ over10(__VA_ARGS__, repss10, a, repss8, a, repss6, a, repss4, a, \ repss2)(__VA_ARGS__) // vectorのindexを走査する // repv(i,j,vvi) #define repv(...) \ over6(__VA_ARGS__, repv_6, repv_5, repv_4, repv_3, repv_2, )(__VA_ARGS__) #define rerv(i, A) for (int i = sz(A) - 1; i >= 0; i--) // repvn(dp) nは次元 #define repv1(a) repv(i, a) #define repv2(a) repv(i, j, a) #define repv3(a) repv(i, j, k, a) #define repv4(a) repv(i, j, k, l, a) #define fora(...) \ over8(__VA_ARGS__, fora_8, a, fora_6, a, fora_4, a, fora_2)(__VA_ARGS__) #define forr(...) \ over8(__VA_ARGS__, forr_8, a, forr_6, a, forr_4, a, forr_2)(__VA_ARGS__) //#define forr(v, a) for(auto&& v : makeReversedRange(a)) //参照を取らない #define forv(a, b) for (auto a : to1d(b)) //インデックスを前後含めて走査 #define ring(i, s, len) \ for (int i = s, prev = (s == 0) ? len - 1 : s - 1, \ next = (s == len - 1) ? 0 : s + 1, cou = 0; \ cou < len; \ cou++, prev = i, i = next, next = (next == len - 1) ? 0 : next + 1) //値と前後を見る #define ringv(v, d) \ index_ = 0; \ for (auto prev = d[sz(d) - 1], next = (int)d.size() > 1 ? d[1] : d[0], \ v = d[0]; \ index_ < sz(d); index_++, prev = v, v = next, \ next = (index_ >= sz(d) - 1 ? d[0] : d[index_ + 1])) // 左右をnext prevで見る 0の左と nの右 #define forlr(v, d, banpei_l, banpei_r) rep(forlr_i, sz(d)) #define form(...) \ over5(__VA_ARGS__, form5, form4, form3, form2, form1)(__VA_ARGS__) #define fors(...) over4(__VA_ARGS__, fors4, fors3, fors2, fors1)(__VA_ARGS__) #define forslr(...) \ over6(__VA_ARGS__, forslr6, forslr5, forslr4, forslr3)(__VA_ARGS__) #define forp(k, v, st) \ for (auto &&forp_it = st.begin(); forp_it != st.end(); ++forp_it) //マクロ 定数 #define k3 1010 #define k4 10101 #define k5 101010 #define k6 1010101 #define k7 10101010 const ll inf = (ll)1e9 + 100; const ll linf = (ll)1e18 + 100; const dou dinf = (dou)linf * linf; const char infc = '{'; const string infs = "{"; const double PI = 3.1415926535897932384626433832795029L; //マクロ省略形 関数等 #define arsz(a) (sizeof(a) / sizeof(a[0])) #define sz(a) ((ll)(a).size()) #define mp make_pair #define pb pop_back #define pf push_front #define eb emplace_back #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() constexpr bool ev(ll a) { return !(a & 1); } constexpr bool od(ll a) { return (a & 1); } //@拡張系 こう出来るべきというもの //埋め込み 存在を意識せずに機能を増やされているもの namespace std { template <> class hash<std::pair<signed, signed>> { public: size_t operator()(const std::pair<signed, signed> &x) const { return hash<ll>()(((ll)x.first << 32) | x.second); } }; template <> class hash<std::pair<ll, ll>> { public : /*大きいllが渡されると、<<32でオーバーフローするがとりあえず問題ないと判断*/ size_t operator()(const std::pair<ll, ll> &x) const { return hash<ll>()(((ll)x.first << 32) | x.second); } }; } // namespace std // stream まとめ /*@formatter:on*/ istream &operator>>(istream &iss, P &a) { iss >> a.first >> a.second; return iss; } template <typename T> istream &operator>>(istream &iss, vector<T> &vec) { for (T &x : vec) iss >> x; return iss; } template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> p) { os << p.fi << " " << p.se; return os; } ostream &operator<<(ostream &os, T p) { os << p.f << " " << p.s << " " << p.t; return os; } ostream &operator<<(ostream &os, F p) { os << p.a << " " << p.b << " " << p.c << " " << p.d; return os; } template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) { for (ll i = 0; i < vec.size(); ++i) os << vec[i] << (i + 1 == vec.size() ? "" : " "); return os; } template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) { for (ll i = 0; i < vec.size(); ++i) { for (ll j = 0; j < vec[i].size(); ++j) { os << vec[i][j] << " "; } os << endl; } return os; } template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &m) { for (auto &&v : m) os << v; return os; } template <class T> ostream &operator<<(ostream &os, set<T> s) { fora(v, s) { os << v << " "; } return os; } template <class T> ostream &operator<<(ostream &os, deque<T> a) { fora(v, a) os << v << " "; return os; } ostream &operator<<(ostream &os, vector<vector<char>> &vec) { rep(h, sz(vec)) { rep(w, sz(vec[0])) { os << vec[h][w]; } os << endl; } return os; } // template<class T,class U>ostream &operator<<(ostream &os, vector<pair<T,U>>& // a) {fora(v,a)os<<v<<endl;return os;} /*@formatter:off*/ template <typename W, typename H> void resize(W &vec, const H head) { vec.resize(head); } template <typename W, typename H, typename... T> void resize(W &vec, const H &head, const T... tail) { vec.resize(head); for (auto &v : vec) resize(v, tail...); } // 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); }); } //削除した後のvectorを返す template <typename T, typename F> vector<T> erase_if2(vector<T> &v, F f) { vector<T> nv; rep(i, sz(v)) { if (!f(v[i])) { nv.push_back(v[i]); } } return nv; } template <typename T, typename F> vector<vector<T>> erase_if2(vector<vector<T>> &v, F f) { vector<vector<T>> res; rep(i, sz(v)) { res[i] = erase_if2(v[i], f); } return res; } // all_of(A, %2); // all_of(A, a, a%2); #define all_of__2(a, right) all_of2(a, lam(right)) #define all_of__3(a, v, siki) all_of2(a, [&](auto v) { return siki; }) #define all_of(...) over3(__VA_ARGS__, all_of__3, all_of__2)(__VA_ARGS__) #define all_of_f(a, f) all_of2(a, f) #define any_of__2(a, right) any_of2(a, lam(right)) #define any_of__3(a, v, siki) any_of2(a, [&](auto v) { return siki; }) #define any_of(...) over3(__VA_ARGS__, any_of__3, any_of__2)(__VA_ARGS__) #define any_of_f(a, f) any_of2(a, f) #define none_of__2(a, right) none_of2(a, lam(right)) #define none_of__3(a, v, siki) none_of2(a, [&](auto v) { return siki; }) #define none_of(...) over3(__VA_ARGS__, none_of__3, none_of__2)(__VA_ARGS__) #define none_of_f(a, f) none_of2(a, f) #define find_if__2(a, right) find_if2(a, lam(right)) #define find_if__3(a, v, siki) find_if2(a, [&](auto v) { return siki; }) #define find_if(...) over3(__VA_ARGS__, find_if__3, find_if__2)(__VA_ARGS__) #define find_if_f(a, f) find_if2(a, f) #define rfind_if__2(a, right) rfind_if2(a, lam(right)) #define rfind_if__3(a, v, siki) rfind_if2(a, [&](auto v) { return siki; }) #define rfind_if(...) over3(__VA_ARGS__, rfind_if__3, rfind_if__2)(__VA_ARGS__) #define rfind_if_f(a, f) rfind_if2(a, f) #define contains_if__2(a, right) contains_if2(a, lam(right)) #define contains_if__3(a, v, siki) contains_if2(a, [&](auto v) { return siki; }) #define contains_if(...) \ over3(__VA_ARGS__, contains_if__3, contains_if__2)(__VA_ARGS__) #define contains_if_f(a, f) contains_if2(a, f) #define count_if__2(a, right) count_if2(a, lam(right)) #define count_if__3(a, v, siki) count_if2(a, [&](auto v) { return siki; }) #define count_if(...) over3(__VA_ARGS__, count_if__3, count_if__2)(__VA_ARGS__) #define count_if_f(a, f) count_if2(a, f) #define for_each__2(a, right) \ do { \ fora(v, a) { v right; } \ } while (0) #define for_each__3(a, v, siki) \ do { \ fora(v, a) { siki; } \ } while (0) #define for_each(...) over3(__VA_ARGS__, for_each__3, for_each__2)(__VA_ARGS__) #define for_each_f(a, f) \ do { \ fora(v, a) { f(v); } \ } while (0) #define erase_if__2(a, right) erase_if2(a, lam(right)) #define erase_if__3(a, v, siki) erase_if2(a, [&](auto v) { return siki; }) #define erase_if(...) over3(__VA_ARGS__, erase_if__3, erase_if__2)(__VA_ARGS__) #define erase_if_f(a, f) erase_if2(a, f) #define entry_if__2(a, right) erase_if2(a, [&](auto v) { return !(v right); }) #define entry_if__3(a, v, siki) erase_if2(a, [&](auto v) { return !(siki); }) #define entry_if(...) over3(__VA_ARGS__, entry_if__3, entry_if__2)(__VA_ARGS__) #define entry_if_f(a, f) erase_if2(a, [&](auto v) { return !f(v); }) template <class T, class U, class W> void replace(vector<W> &a, T key, U v) { rep(i, sz(a)) if (a[i] == key) a[i] = v; } template <class T, class U, class W> void replace(vector<vector<W>> &A, T key, U v) { rep(i, sz(A)) replace(A[i], key, v); } void replace(str &a, char key, str v) { if (v == "") a.erase(remove(all(a), key), a.end()); } void replace(str &a, char key, char v) { replace(all(a), key, v); } // keyと同じかどうか01で置き換える template <class T, class U> void replace(vector<T> &a, U k) { rep(i, sz(a)) a[i] = a[i] == k; } template <class T, class U> void replace(vector<vector<T>> &a, U k) { rep(i, sz(a)) rep(j, sz(a[0])) a[i][j] = a[i][j] == k; } template <class T> void replace(T &a) { replace(a, '#'); } void replace(str &a, str key, str v) { stringstream t; ll kn = sz(key); std::string::size_type Pos(a.find(key)); ll l = 0; while (Pos != std::string::npos) { t << a.substr(l, Pos - l); t << v; l = Pos + kn; Pos = a.find(key, Pos + kn); } t << a.substr(l, sz(a) - l); a = t.str(); } template <class T> bool includes(vector<T> &a, vector<T> &b) { vi c = a; vi d = b; sort(all(c)); sort(all(d)); return includes(all(c), all(d)); } template <class T> bool is_permutation(vector<T> &a, vector<T> &b) { return is_permutation(all(a), all(b)); } template <class T> bool next_permutation(vector<T> &a) { return next_permutation(all(a)); } void iota(vector<ll> &ve, ll s, ll n) { ve.resize(n); iota(all(ve), s); } vi iota(ll s, ll len) { vi ve(len); iota(all(ve), s); return ve; } template <class A, class B> auto vtop(vector<A> &a, vector<B> &b) { assert(sz(a) == sz(b)); /*stringを0で初期化できない */ vector<pair<A, B>> res; rep(i, sz(a)) res.eb(a[i], b[i]); return res; } template <class A, class B> void ptov(vector<pair<A, B>> &p, vector<A> &a, vector<B> &b) { a.resize(sz(p)), b.resize(sz(p)); rep(i, sz(p)) a[i] = p[i].fi, b[i] = p[i].se; } template <class A, class B, class C> auto vtot(vector<A> &a, vector<B> &b, vector<C> &c) { assert(sz(a) == sz(b) && sz(b) == sz(c)); vector<T2<A, B, C>> res; rep(i, sz(a)) res.eb(a[i], b[i], c[i]); return res; } template <class A, class B, class C, class D> auto vtof(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { assert(sz(a) == sz(b) && sz(b) == sz(c) && sz(c) == sz(d)); vector<F2<A, B, C, D>> res; rep(i, sz(a)) res.eb(a[i], b[i], c[i], d[i]); return res; } enum pcomparator { fisi, fisd, fdsi, fdsd, sifi, sifd, sdfi, sdfd }; enum tcomparator { fisiti, fisitd, fisdti, fisdtd, fdsiti, fdsitd, fdsdti, fdsdtd, fitisi, fitisd, fitdsi, fitdsd, fdtisi, fdtisd, fdtdsi, fdtdsd, sifiti, sifitd, sifdti, sifdtd, sdfiti, sdfitd, sdfdti, sdfdtd, sitifi, sitifd, sitdfi, sitdfd, sdtifi, sdtifd, sdtdfi, sdfdfd, tifisi, tifisd, tifdsi, tifdsd, tdfisi, tdfisd, tdfdsi, tdfdsd, tisifi, tisifd, tisdfi, tisdfd, tdsifi, tdsifd, tdsdfi, tdsdfd }; template <class A, class B> void sort(vector<pair<A, B>> &a, pcomparator type) { typedef pair<A, B> U; if (type == fisi) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se < r.se; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se > r.se; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se < r.se; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se > r.se; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi < r.fi; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi > r.fi; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi < r.fi; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi > r.fi; }); }; template <class U> void sort(vector<U> &a, pcomparator type) { if (type == fisi) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f > r.f; }); }; template <class A, class B, class C, class D> void sort(vector<F2<A, B, C, D>> &a, pcomparator type) { typedef F2<A, B, C, D> U; if (type == fisi) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a > r.a; }); }; template <class U> void sort(vector<U> &a, tcomparator type) { if (type == 0) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t < r.t; }); else if (type == 1) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t > r.t; }); else if (type == 2) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t < r.t; }); else if (type == 3) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t > r.t; }); else if (type == 4) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t < r.t; }); else if (type == 5) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t > r.t; }); else if (type == 6) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t < r.t; }); else if (type == 7) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t > r.t; }); else if (type == 8) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s < r.s; }); else if (type == 9) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s > r.s; }); else if (type == 10) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s < r.s; }); else if (type == 11) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s > r.s; }); else if (type == 12) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s < r.s; }); else if (type == 13) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s > r.s; }); else if (type == 14) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s < r.s; }); else if (type == 15) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s > r.s; }); else if (type == 16) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t < r.t; }); else if (type == 17) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t > r.t; }); else if (type == 18) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t < r.t; }); else if (type == 19) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t > r.t; }); else if (type == 20) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t < r.t; }); else if (type == 21) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t > r.t; }); else if (type == 22) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t < r.t; }); else if (type == 23) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t > r.t; }); else if (type == 24) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f < r.f; }); else if (type == 25) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f > r.f; }); else if (type == 26) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f < r.f; }); else if (type == 27) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f > r.f; }); else if (type == 28) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f < r.f; }); else if (type == 29) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f > r.f; }); else if (type == 30) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f < r.f; }); else if (type == 31) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f > r.f; }); else if (type == 32) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == 33) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == 34) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == 35) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == 36) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == 37) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == 38) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == 39) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == 40) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == 41) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == 42) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == 43) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f > r.f; }); else if (type == 44) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == 45) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == 46) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == 47) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f > r.f; }); } template <class A, class B, class C, class D> void sort(vector<F2<A, B, C, D>> &a, tcomparator type) { typedef F2<A, B, C, D> U; if (type == 0) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c < r.c; }); else if (type == 1) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c > r.c; }); else if (type == 2) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c < r.c; }); else if (type == 3) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c > r.c; }); else if (type == 4) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c < r.c; }); else if (type == 5) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c > r.c; }); else if (type == 6) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c < r.c; }); else if (type == 7) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c > r.c; }); else if (type == 8) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b < r.b; }); else if (type == 9) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b > r.b; }); else if (type == 10) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b < r.b; }); else if (type == 11) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b > r.b; }); else if (type == 12) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b < r.b; }); else if (type == 13) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b > r.b; }); else if (type == 14) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b < r.b; }); else if (type == 15) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b > r.b; }); else if (type == 16) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c < r.c; }); else if (type == 17) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c > r.c; }); else if (type == 18) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c < r.c; }); else if (type == 19) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c > r.c; }); else if (type == 20) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c < r.c; }); else if (type == 21) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c > r.c; }); else if (type == 22) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c < r.c; }); else if (type == 23) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c > r.c; }); else if (type == 24) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a < r.a; }); else if (type == 25) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a > r.a; }); else if (type == 26) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a < r.a; }); else if (type == 27) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a > r.a; }); else if (type == 28) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a < r.a; }); else if (type == 29) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a > r.a; }); else if (type == 30) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a < r.a; }); else if (type == 31) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a > r.a; }); else if (type == 32) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == 33) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == 34) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == 35) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == 36) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == 37) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == 38) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == 39) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == 40) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == 41) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == 42) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == 43) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a > r.a; }); else if (type == 44) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == 45) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == 46) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == 47) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a > r.a; }); } void sort(string &a) { sort(all(a)); } void sort(int &a, int &b) { if (a > b) swap(a, b); } void sort(int &a, int &b, int &c) { sort(a, b); sort(a, c); sort(b, c); } void rsort(int &a, int &b) { if (a < b) swap(a, b); } void rsort(int &a, int &b, int &c) { rsort(a, b); rsort(a, c); rsort(b, c); } template <class T> void sort(vector<T> &a) { sort(all(a)); } // P l, P rで f(P) の形で渡す template <class U, class F> void sort(vector<U> &a, F f) { sort(all(a), [&](U l, U r) { return f(l) < f(r); }); }; template <class T> void rsort(vector<T> &a) { sort(all(a), greater<T>()); }; template <class U, class F> void rsort(vector<U> &a, F f) { sort(all(a), [&](U l, U r) { return f(l) > f(r); }); }; // F = T<T> //例えばreturn p.fi + p.se; template <class A, class B> void sortp(vector<A> &a, vector<B> &b) { auto c = vtop(a, b); sort(c); rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se; } template <class A, class B, class F> void sortp(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); sort(c, f); rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se; } template <class A, class B> void rsortp(vector<A> &a, vector<B> &b) { auto c = vtop(a, b); rsort(c); rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second; } template <class A, class B, class F> void rsortp(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); rsort(c, f); rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second; } template <class A, class B, class C> void sortt(vector<A> &a, vector<B> &b, vector<C> &c) { auto d = vtot(a, b, c); sort(d); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class F> void sortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); sort(d, f); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C> void rsortt(vector<A> &a, vector<B> &b, vector<C> &c) { auto d = vtot(a, b, c); rsort(d); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class F> void rsortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); rsort(d, f); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class D> void sortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { auto e = vtof(a, b, c, d); sort(e); rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d; } template <class A, class B, class C, class D> void rsortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { auto e = vtof(a, b, c, d); rsort(e); rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d; } // sortindex 元のvectorはソートしない template <class T> vi sorti(vector<T> &a) { auto b = a; vi ind = iota(0, sz(a)); sortp(b, ind); return ind; } /*indexの分で型が変わるためpcomparatorが必要*/ template <class T> vi sorti(vector<T> &a, pcomparator f) { auto b = a; vi ind = iota(0, sz(a)); sortp(b, ind, f); return ind; } template <class T, class F> vi sorti(vector<T> &a, F f) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(a[x]) < f(a[y]); }); return ind; } template <class T> vi rsorti(vector<T> &a) { auto b = a; vi ind = iota(0, sz(a)); rsortp(b, ind); return ind; } template <class T, class F> vi rsorti(vector<T> &a, F f) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(a[x]) > f(a[y]); }); return ind; } template <class A, class B, class F> vi sortpi(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(c[x]) < f(c[y]); }); return ind; } template <class A, class B> vi sortpi(vector<A> &a, vector<B> &b, pcomparator f) { vi ind = iota(0, sz(a)); auto c = a; auto d = b; sortt(c, d, ind, f); return ind; } template <class A, class B> vi sortpi(vector<A> &a, vector<B> &b) { return sortpi(a, b, fisi); }; template <class A, class B, class F> vi rsortpi(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(c[x]) > f(c[y]); }); return ind; } template <class A, class B> vi rsortpi(vector<A> &a, vector<B> &b) { return sortpi(a, b, fdsd); }; template <class A, class B, class C, class F> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(d[x]) < f(d[y]); }); return ind; } template <class A, class B, class C> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, pcomparator f) { vi ind = iota(0, sz(a)); auto d = vtof(a, b, c, ind); sort(d, f); rep(i, sz(a)) ind[i] = d[i].d; return ind; } template <class A, class B, class C> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { if (a[x] == a[y]) { if (b[x] == b[y]) return c[x] < c[y]; else return b[x] < b[y]; } else { return a[x] < a[y]; } }); return ind; } template <class A, class B, class C, class F> vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(d[x]) > f(d[y]); }); return ind; } template <class A, class B, class C> vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { if (a[x] == a[y]) { if (b[x] == b[y]) return c[x] > c[y]; else return b[x] > b[y]; } else { return a[x] > a[y]; } }); return ind; } template <class T> void sort2(vector<vector<T>> &a) { for (ll i = 0, n = a.size(); i < n; ++i) sort(a[i]); } template <class T> void rsort2(vector<vector<T>> &a) { for (ll i = 0, n = a.size(); i < n; ++i) rsort(a[i]); } template <class... T, class U> auto sorted(U head, T... a) { sort(head, a...); return head; } 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 W, typename T> void fill(W &xx, const T vall) { xx = vall; } template <typename W, typename T> void fill(vector<W> &vecc, const T vall) { for (auto &&vx : vecc) fill(vx, vall); } template <typename W, typename T> void fill(vector<W> &xx, ll len, const T v) { rep(i, len) xx[i] = v; } template <typename W, typename T> void fill(vector<vector<W>> &xx, int sh, int th, int sw, int tw, T v) { rep(h, sh, th) rep(w, sw, tw) xx[h][w] = v; } template <class T, class U> void fill(vector<T> &a, vi &ind, U val) { fora(v, ind) a[v] = val; } template <class W, class T> void fill(mvec<W> &xx, const T v) { fora(x, xx) fill(x, v); } template <typename A, size_t N> A sum(A (&a)[N]) { A res = 0; rep(i, N) res += a[i]; return res; } template <typename A, size_t N, size_t O> A sum(A (&a)[N][O]) { A res = 0; rep(i, N) rep(j, O) res += a[i][j]; return res; } template <typename A, size_t N, size_t O, size_t P> A sum(A (&a)[N][O][P]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) res += a[i][j][k]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A sum(A (&a)[N][O][P][Q]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) res += a[i][j][k][l]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A sum(A (&a)[N][O][P][Q][R]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) res += a[i][j][k][l][m]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A sum(A (&a)[N][O][P][Q][R][S]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) rep(n, S) res += a[i][j][k][l][m][n]; return res; } //@汎用便利関数 入力 ll in() { ll ret; cin >> ret; return ret; } string sin() { string ret; cin >> ret; return ret; } template <class T> void in(T &head) { cin >> head; } template <class T, class... U> void in(T &head, U &...tail) { cin >> head; in(tail...); } #define din1(a) \ ll a; \ cin >> a #define din2(a, b) \ ll a, b; \ cin >> a >> b #define din3(a, b, c) \ ll a, b, c; \ cin >> a >> b >> c #define din4(a, b, c, d) \ ll a, b, c, d; \ cin >> a >> b >> c >> d #define din5(a, b, c, d, e) \ ll a, b, c, d, e; \ cin >> a >> b >> c >> d >> e #define din6(a, b, c, d, e, f) \ ll a, b, c, d, e, f; \ cin >> a >> b >> c >> d >> e >> f #define din(...) \ over6(__VA_ARGS__, din6, din5, din4, din3, din2, din1)(__VA_ARGS__) #define dins1(a) \ str a; \ cin >> a #define dins2(a, b) \ str a, b; \ cin >> a >> b #define dins3(a, b, c) \ str a, b, c; \ cin >> a >> b >> c #define dins4(a, b, c, d) \ str a, b, c, d; \ cin >> a >> b >> c >> d #define dins5(a, b, c, d, e) \ str a, b, c, d, e; \ cin >> a >> b >> c >> d >> e #define dins6(a, b, c, d, e, f) \ str a, b, c, d, e, f; \ cin >> a >> b >> c >> d >> e >> f #define dins(...) \ over6(__VA_ARGS__, dins6, dins5, dins4, dins3, dins2, dins1)(__VA_ARGS__) #define din1d(a) \ din1(a); \ a-- #define din2d(a, b) \ din2(a, b); \ a--, b-- #define din3d(a, b, c) \ din3(a, b, c); \ a--, b--, c-- #define din4d(a, b, c, d) \ din4(a, b, c, d); \ a--, b--, c--, d-- #define dind(...) over4(__VA_ARGS__, din4d, din3d, din2d, din1d)(__VA_ARGS__) template <class T> void out2(T &&head) { cout << head; } template <class T, class... U> void out2(T &&head, U &&...tail) { cout << head << " "; out2(tail...); } template <class T, class... U> void out(T &&head, U &&...tail) { cout << head << " "; out2(tail...); cout << "" << endl; } template <class T> void out(T &&head) { cout << head << endl; } void out() { cout << "" << endl; } #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; } /*@formatter:on*/ 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]; if (typeid(a[i]) == typeid(P)) { ss << endl; } else { ss << " "; } } return ss.str(); } /*@formatter:off*/ 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(); } 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 = 12; H = min(H, sz(a)); rep(i, H) { ss << endl; ss << out_m2(a[i], W, U, i); } return ss.str(); } template <class T, size_t N> string out_m2(T (&a)[N]) { vector<T> b; resize(b, N); rep(i, N) { b[i] = a[i]; } return out_m2(b); } template <class T, size_t N, size_t M> string out_m2(T (&a)[N][M]) { vector<vector<T>> b; resize(b, N, M); rep(i, N) { rep(j, M) { b[i][j] = a[i][j]; } } return out_m2(b); } template <class T, size_t N, size_t M, size_t O> string out_m2(T (&a)[N][M][O]) { vector<vector<vector<T>>> b; resize(b, N, M, O); rep(i, N) { rep(j, M) { rep(k, O) { b[i][j][k] = a[i][j][k]; } } } return out_m2(b); } string out_m2(int a) { stringstream ss; ss << a; return ss.str(); } /*@formatter:on*/ template <class T> string out_m2(mvec<mvec<T>> &a, ll H = inf, ll W = inf, int key = inf) { H = min({H, sz(a), 6ll}); W = min({W, sz(a[0]), 6ll}); stringstream ss; ss << endl; // if (key == inf)ss << " *|"; else ss << " " << key << "|"; if (key == inf) ss << " *|"; else { ss << std::right << std::setw(2) << key; ss << "|"; } rep(w, -W, W) ss << std::right << std::setw(4) << w; ss << "" << endl; rep(w, W * 8 + 3) ss << "_"; ss << "" << endl; rep(h, -H, H) { ss << std::right << std::setw(2) << h << "|"; int NW = min(sz(a[h]), 6ll); rep(w, -NW, NW) { if (abs(a[h][w]) == linf) ss << " e" << ""; else ss << std::right << std::setw(4) << a[h][w]; } ss << "" << endl; } return ss.str(); } /*@formatter:on*/ template <class T> string out_m2(mvec<mvec<mvec<T>>> &a, ll H = inf, ll W = inf, ll U = inf) { stringstream ss; if (H == inf) H = 6; H = min(H, sz(a)); rep(i, -H, H) { ss << endl; ss << out_m2(a[i], W, U, i); } return ss.str(); } /*@formatter:off*/ template <class T> string out_m2(T &a) { stringstream ss; ss << a; return ss.str(); } /*@formatter:on*/ 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(); } /*@formatter:off*/ 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(); } 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 template <class T> void outl(vector<T> &a, int n = inf) { rep(i, min(n, sz(a))) cout << a[i] << endl; } //テーブルをスペースなしで出力 template <class T> void outt(vector<vector<T>> &a) { rep(i, sz(a)) { rep(j, sz(a[i])) { cout << a[i][j]; } cout << endl; } } // int型をbit表記で出力 void outb(int a) { cout << bitset<20>(a) << endl; } template <class T> void na(vector<T> &a, ll n) { a.resize(n); rep(i, n) cin >> a[i]; } #define dna(a, n) \ vi a(n); \ rep(dnai, n) cin >> a[dnai]; #define dnad(a, n) \ vi a(n); \ rep(dnai, n) cin >> a[dnai], a[dnai]--; template <class T> void nao(vector<T> &a, ll n) { a.resize(n + 1); a[0] = 0; rep(i, n) cin >> a[i + 1]; } template <class T> void naod(vector<T> &a, ll n) { a.resize(n + 1); a[0] = 0; rep(i, n) cin >> a[i + 1], a[i + 1]--; } template <class T> void nad(vector<T> &a, ll n) { a.resize(n); rep(i, n) cin >> a[i], a[i]--; } template <class T, class U> void na2(vector<T> &a, vector<U> &b, ll n) { a.resize(n); b.resize(n); rep(i, n) cin >> a[i] >> b[i]; } #define dna2(a, b, n) \ vi a(n), b(n); \ rep(dna2i, n) cin >> a[dna2i] >> b[dna2i]; template <class T, class U> void nao2(vector<T> &a, vector<U> &b, ll n) { a.resize(n + 1); b.resize(n + 1); a[0] = b[0] = 0; rep(i, n) cin >> a[i + 1] >> b[i + 1]; } #define dna2d(a, b, n) \ vi a(n), b(n); \ rep(dna2di, n) { \ cin >> a[dna2di] >> b[dna2di]; \ a[dna2di]--, b[dna2di]--; \ } template <class T, class U> void na2d(vector<T> &a, vector<U> &b, ll n) { a.resize(n); b.resize(n); rep(i, n) cin >> a[i] >> b[i], a[i]--, b[i]--; } template <class T, class U, class W> void na3(vector<T> &a, vector<U> &b, vector<W> &c, ll n) { a.resize(n); b.resize(n); c.resize(n); rep(i, n) cin >> a[i] >> b[i] >> c[i]; } #define dna3(a, b, c, n) \ vi a(n), b(n), c(n); \ rep(dna3i, n) cin >> a[dna3i] >> b[dna3i] >> c[dna3i]; template <class T, class U, class W> void na3d(vector<T> &a, vector<U> &b, vector<W> &c, ll 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 dna3d(a, b, c, n) \ vi a(n), b(n), c(n); \ rep(dna3di, n) { \ cin >> a[dna3di] >> b[dna3di] >> c[dna3di]; \ a[dna3di]--, b[dna3di]--, c[dna3di]--; \ } template <class T, class U, class W, class X> void na4(vector<T> &a, vector<U> &b, vector<W> &c, vector<X> &d, ll n) { a.resize(n); b.resize(n); c.resize(n); d.resize(n); rep(i, n) cin >> a[i] >> b[i] >> c[i] >> d[i]; } #define dna4(a, b, c, d, n) \ vi a(n), b(n), c(n), d(n); \ rep(dna4i, n) cin >> a[dna4i] >> b[dna4i] >> c[dna4i] >> d[dna4i]; #define dna4d(a, b, c, d, n) \ vi a(n), b(n), c(n), d(n); \ rep(dna4i, n) cin >> a[dna4i] >> b[dna4i] >> c[dna4i] >> d[dna4i], \ --a[dna4i], --b[dna4i], --c[dna4i], --d[dna4i]; #define nt(a, h, w) \ resize(a, h, w); \ rep(nthi, h) rep(ntwi, w) cin >> a[nthi][ntwi]; #define ntd(a, h, w) \ resize(a, h, w); \ rep(ntdhi, h) rep(ntdwi, w) cin >> a[ntdhi][ntdwi], a[ntdhi][ntdwi]--; #define ntp(a, h, w) \ resize(a, h + 2, w + 2); \ fill(a, '#'); \ rep(ntphi, 1, h + 1) rep(ntpwi, 1, w + 1) cin >> a[ntphi][ntpwi]; //デバッグ #define sp << " " << #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__) #ifdef _DEBUG #define deb(...) \ cerr << over10(__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' //@formatter:off //よく使うクラス、構造体 // graphでredefineしているため、書き換えたら向こうも書き換える struct unionfind { vector<ll> par; vector<ll> siz; vector<ll> es; ll n, trees; //連結グループの数(親の種類) unionfind(ll n) : n(n), trees(n) { par.resize(n); siz.resize(n); es.resize(n); for (ll i = 0; i < n; i++) { par[i] = i; siz[i] = 1; } } ll root(ll x) { if (par[x] == x) { return x; } else { return par[x] = root(par[x]); } } ll operator()(ll x) { return root(x); } bool unite(ll x, ll y) { x = root(x); y = root(y); es[x]++; if (x == y) return false; if (siz[x] > siz[y]) swap(x, y); trees--; par[x] = y; siz[y] += siz[x]; es[y] += es[x]; return true; } bool same(ll x, ll y) { return root(x) == root(y); } ll size(ll x) { return siz[root(x)]; } ll esize(ll x) { return es[root(x)]; } vi sizes() { vi cou(n); vi ret; ret.reserve(n); rep(i, n) { cou[root(i)]++; } rep(i, n) { if (cou[i]) ret.push_back(cou[i]); } return ret; } //つながりを無向グラフと見なし、xが閉路に含まれるか判定 bool close(ll x) { return esize(x) >= size(x); } vector<vi> sets() { vi ind(n, -1); ll i = 0; vvi(res, trees); rep(j, n) { ll r = root(j); if (ind[r] == -1) ind[r] = i++; res[ind[r]].push_back(j); } rep(i, trees) { ll r = root(res[i][0]); if (res[i][0] == r) continue; rep(j, 1, sz(res[i])) { if (res[i][j] == r) { swap(res[i][0], res[i][j]); break; } } } return res; } }; //@formatter:off using bll = __int128; using u32 = unsigned; using u64 = unsigned long long; using u128 = __uint128_t; std::ostream &operator<<(std::ostream &dest, __int128_t value) { std::ostream::sentry s(dest); if (s) { __uint128_t tmp = value < 0 ? -value : value; char buffer[128]; char *d = std::end(buffer); do { --d; *d = "0123456789"[tmp % 10]; tmp /= 10; } while (tmp != 0); if (value < 0) { --d; *d = '-'; } ll len = std::end(buffer) - d; if (dest.rdbuf()->sputn(d, len) != len) { dest.setstate(std::ios_base::badbit); } } return dest; } //__int128 toi128(string &s) { __int128 ret = 0; for (ll i = 0; i < //s.length(); ++i) if ('0' <= s[i] && s[i] <= '9') ret = 10 * //ret + s[i] - '0'; return ret;} //エラー 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 re() { assert(0 == 1); exit(0); } void tle() { while (inf) cout << inf << endl; } //便利関数 //テスト用 #define rand xor128_ unsigned long xor128_(void) { static unsigned long x = 123456789, y = 362436069, z = 521288629, w = 88675123; unsigned long t; t = (x ^ (x << 11)); x = y; y = z; z = w; return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8))); } char ranc() { return (char)('a' + rand() % 26); } ll rand(ll min, ll max) { assert(min <= max); if (min >= 0 && max >= 0) { return rand() % (max + 1 - min) + min; } else if (max < 0) { return -rand(-max, -min); } else { if (rand() % 2) { return rand(0, max); } else { return -rand(0, -min); } } } vi ranv(ll n, ll min, ll max) { vi v(n); rep(i, n) v[i] = rand(min, max); return v; } str ransu(ll n) { str s; rep(i, n) s += (char)rand('A', 'Z'); return s; } str ransl(ll n) { str s; rep(i, n) s += (char)rand('a', 'z'); return s; } //単調増加 vi ranvinc(ll n, ll min, ll max) { vi v(n); bool bad = 1; while (bad) { bad = 0; v.resize(n); rep(i, n) { if (i && min > max - v[i - 1]) { bad = 1; break; } if (i) v[i] = v[i - 1] + rand(min, max - v[i - 1]); else v[i] = rand(min, max); } } return v; } //便利 汎用 void ranvlr(ll n, ll min, ll max, vi &l, vi &r) { l.resize(n); r.resize(n); rep(i, n) { l[i] = rand(min, max); r[i] = l[i] + rand(0, max - l[i]); } } template <class T> vector<pair<T, int>> run_length(vector<T> &a) { vector<pair<T, int>> 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 <class F> ll mgr(ll ok, ll ng, F f) { bool han = true; if (ok < ng) while (ng - ok > 1) { ll mid = (ok + ng) >> 1; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; deb(mid, han); } else while (ok - ng > 1) { ll mid = (ok + ng) >> 1; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; deb(mid, han); } return ok; } template <class F> dou mgrd(dou ok, dou ng, F f, int kai = 100) { bool han = true; if (ok < ng) rep(i, kai) { dou mid = (ok + ng) / 2; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; deb(mid, han); } else rep(i, kai) { dou mid = (ok + ng) / 2; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; deb(mid, han); } return ok; } // strを整数として比較 string smax(str &a, str b) { if (sz(a) < sz(b)) { return b; } else if (sz(a) > sz(b)) { return a; } else if (a < b) return b; else return a; } // strを整数として比較 string smin(str &a, str b) { if (sz(a) > sz(b)) { return b; } else if (sz(a) < sz(b)) { return a; } else if (a > b) return b; else return a; } //エラー-1 template <typename W, typename T> ll find(vector<W> &a, int l, const T key) { rep(i, l, sz(a)) if (a[i] == key) return i; return -1; } template <typename W, typename T> ll find(vector<W> &a, const T key) { rep(i, sz(a)) if (a[i] == key) return i; return -1; } template <typename W, typename T> P find(vector<vector<W>> &a, const T key) { rep(i, sz(a)) rep(j, sz(a[0])) if (a[i][j] == key) return mp(i, j); return mp(-1, -1); } template <typename W, typename U> T find(vector<vector<vector<W>>> &a, const U key) { rep(i, sz(a)) rep(j, sz(a[0])) rep(k, sz(a[0][0])) if (a[i][j][k] == key) return mt(i, j, k); return mt(-1, -1, -1); } // stringも書く int find(string &s, const string key) { int klen = sz(key); rep(i, sz(s) - klen + 1) { if (s[i] != key[0]) continue; if (s.substr(i, klen) == key) { return i; } } return -1; } int find(string &s, int l, const string key) { int klen = sz(key); rep(i, l, sz(s) - klen + 1) { if (s[i] != key[0]) continue; if (s.substr(i, klen) == key) { return i; } } return -1; } int find(string &s, const char key) { rep(i, sz(s)) { if (s[i] == key) return i; } return -1; } int find(string &s, int l, const char key) { rep(i, l, sz(s)) { if (s[i] == key) return i; } return -1; } template <typename W, typename T> ll count2(W &a, const T k) { return a == k; } template <typename W, typename T> ll count2(vector<W> &a, const T k) { ll ret = 0; fora(v, a) ret += count2(v, k); return ret; } template <typename W, typename T> ll count(vector<W> &a, const T k) { ll ret = 0; fora(v, a) ret += count2(v, k); return ret; } vi count(vi &a) { int ma = 0; fora(v, a) { if (ma < v) ma = v; } vi res(ma + 1); fora(v, a) { res[v]++; } return res; } ll count(str &a, str k) { ll ret = 0, len = k.length(); auto pos = a.find(k); while (pos != string::npos) pos = a.find(k, pos + len), ++ret; return ret; } vi count(str &a) { vi cou(26); char c = 'a'; if ('A' <= a[0] && a[0] <= 'Z') c = 'A'; rep(i, sz(a))++ cou[a[i] - c]; return cou; } #define couif count_if // algorythm 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)); } template <class U> void rev(vector<vector<U>> &a) { vector<vector<U>> b(sz(a[0]), vector<U>(sz(a))); rep(h, sz(a)) rep(w, sz(a[0])) b[w][h] = a[h][w]; a = b; } void rev(string &a) { reverse(all(a)); } constexpr ll p10[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000ll, 100000000000ll, 1000000000000ll, 10000000000000ll, 100000000000000ll, 1000000000000000ll, 10000000000000000ll, 100000000000000000ll, 1000000000000000000ll}; ll get(ll a, ll keta) { return (a / (ll)pow(10, keta)) % 10; } // 0は0桁 ll keta(ll v) { if (v < p10[9]) { if (v < p10[4]) { if (v < p10[2]) { if (v < p10[1]) { if (v < p10[0]) return 0; else return 1; } else return 2; } else { if (v < p10[3]) return 3; else return 4; } } else { if (v < p10[7]) { if (v < p10[5]) return 5; else if (v < p10[6]) return 6; else return 7; } else { if (v < p10[8]) return 8; else return 9; } } } else { if (v < p10[13]) { if (v < p10[11]) { if (v < p10[10]) return 10; else return 11; } else { if (v < p10[12]) return 12; else return 13; } } else { if (v < p10[15]) { if (v < p10[14]) return 14; else return 15; } else { if (v < p10[17]) { if (v < p10[16]) return 16; else return 17; } else { if (v < p10[18]) return 18; else return 19; } } } } } ll dsum(ll v, ll sin = 10) { ll ret = 0; for (; v; v /= sin) ret += v % sin; return ret; } ll mask10(ll v) { return p10[v] - 1; } //変換系 //[v] := iとなるようなvectorを返す //存在しない物は-1 template <class T> auto keys(T &a) { vector<decltype((a.begin())->fi)> res; for (auto &&k : a) res.push_back(k.fi); return res; } template <class T> auto values(T &a) { vector<decltype((a.begin())->se)> res; for (auto &&k : a) res.push_back(k.se); return res; } template <class T, class U> bool chma(T &a, const U &b) { if (a < b) { a = b; return true; } return false; } template <class T, class U> bool chmi(T &a, const U &b) { if (b < a) { a = b; return true; } return false; } template <class T> constexpr T min(T a, signed b) { return a < b ? a : b; } template <class T> constexpr T max(T a, signed b) { return a < b ? b : a; } template <class T> constexpr T min(T a, T b, T c) { return a >= b ? b >= c ? c : b : a >= c ? c : a; } template <class T> constexpr T max(T a, T b, T c) { return a <= b ? b <= c ? c : b : a <= c ? c : a; } template <class T> T min(vector<T> &a) { return *min_element(all(a)); } template <class T> T mini(vector<T> &a) { return min_element(all(a)) - a.begin(); } template <class T> T min(vector<T> &a, ll n) { return *min_element(a.begin(), a.begin() + min(n, sz(a))); } template <class T> T min(vector<T> &a, ll s, ll n) { return *min_element(a.begin() + s, a.begin() + min(n, sz(a))); } template <class T> T max(vector<T> &a) { return *max_element(all(a)); } template <class T, class U> T max(vector<T> &a, vector<U> &b) { return max(*max_element(all(a)), *max_element(all(b))); } template <class T> T maxi(vector<T> &a) { return max_element(all(a)) - a.begin(); } template <class T> T max(vector<T> &a, ll n) { return *max_element(a.begin(), a.begin() + min(n, sz(a))); } template <class T> T max(vector<T> &a, ll s, ll n) { return *max_element(a.begin() + s, a.begin() + min(n, sz(a))); } template <typename A, size_t N> A max(A (&a)[N]) { A res = a[0]; rep(i, N) res = max(res, a[i]); return res; } template <typename A, size_t N, size_t O> A max(A (&a)[N][O]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P> A max(A (&a)[N][O][P]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A max(A (&a)[N][O][P][Q], const T &v) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A max(A (&a)[N][O][P][Q][R]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A max(A (&a)[N][O][P][Q][R][S]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N> A min(A (&a)[N]) { A res = a[0]; rep(i, N) res = min(res, a[i]); return res; } template <typename A, size_t N, size_t O> A min(A (&a)[N][O]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P> A min(A (&a)[N][O][P]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A min(A (&a)[N][O][P][Q], const T &v) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A min(A (&a)[N][O][P][Q][R]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A min(A (&a)[N][O][P][Q][R][S]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <class T> T sum(vector<T> &v, ll s, ll t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += v[i]; return ret; } template <class T> T sum(vector<T> &v, ll t = inf) { return sum(v, 0, t); } template <class T> T sum(vector<vector<T>> &v, int s, int t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<T>> &v, int t = inf) { return sum(v, 0, t); } template <class T> T sum(vector<vector<vector<T>>> &v, int s, int t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<T>>> &v, int t = inf) { return sum(v, 0, t); } template <class T> T sum(vector<vector<vector<vector<T>>>> &v, int s, int t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<vector<T>>>> &v, int t = inf) { return sum(v, 0, t); } template <class T> T sum(vector<vector<vector<vector<vector<T>>>>> &v, int s, int t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<vector<vector<T>>>>> &v, int t = inf) { return sum(v, 0, t); } template <class T> auto sum(priority_queue<T, vector<T>, greater<T>> &r) { auto q = r; T ret = 0; while (sz(q)) { ret += q.top(); q.pop(); } return ret; } template <class T> auto sum(priority_queue<T> &r) { auto q = r; T ret = 0; while (sz(q)) { ret += q.top(); q.pop(); } return ret; } // template<class T, class U, class... W> auto sumn(vector<T> &v, U head, W... // tail) { auto ret = sum(v[0], tail...); rep(i, 1, min(sz(v), head))ret += // sum(v[i], tail...); return ret;} vi v_i(vi &a) { int n = max(a) + 1; vi ret(n, -1); rep(i, sz(a)) { ret[a[i]] = i; } return ret; } void clear(PQ &q) { q = PQ(); } void clear(priority_queue<int> &q) { q = priority_queue<int>(); } template <class T> void clear(queue<T> &q) { while (q.size()) q.pop(); } template <class T> T *negarr(ll size) { T *body = (T *)malloc((size * 2 + 1) * sizeof(T)); return body + size; } template <class T> T *negarr2(ll h, ll w) { double **dummy1 = new double *[2 * h + 1]; double *dummy2 = new double[(2 * h + 1) * (2 * w + 1)]; dummy1[0] = dummy2 + w; for (ll i = 1; i <= 2 * h + 1; ++i) { dummy1[i] = dummy1[i - 1] + 2 * w + 1; } double **a = dummy1 + h; return a; } // 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> vector<T> ima(vector<T> &v) { vector<T> ret = v; rep(i, sz(ret) - 1) chma(ret[i + 1], ret[i]); return ret; } // template<class T> vector<T> rimi(vector<T> &v) { vector<T> ret = v; rer(i, // sz(ret) - 1, 1)chmi(ret[i - 1], ret[i]); return ret;} template<class T> // vector<T> rima(vector<T> &v) { vector<T> ret = v; rer(i, sz(ret) - 1, // 1)chma(ret[i - 1], ret[i]); return ret;} template <class T> struct ruiC { vector<T> rui; ruiC(vector<T> &ru) : rui(ru) {} /*先頭0*/ ruiC() : rui(1, 0) {} T operator()(ll l, ll r) { if (l > r) { cerr << "ruic "; deb(l, r); assert(0); } return rui[r] - rui[l]; } T operator()(int r) { return operator()(0, r); } /*ruiv[]をruic[]に変えた際意味が変わるのがまずいため()と統一*/ /*単体iを返す 累積でないことに注意(seg木との統一でこうしている)*/ // T operator[](ll i) { return rui[i + 1] - rui[i]; } T operator[](ll i) { return rui[i]; } /*0から順に追加される必要がある*/ void operator+=(T v) { rui.push_back(rui.back() + v); } void add(int i, T v) { if (sz(rui) - 1 != i) ole(); operator+=(v); } T back() { return rui.back(); } ll size() { return rui.size(); } auto begin() { return rui.begin(); } auto end() { return rui.end(); } }; template <class T> struct ruimax { template <typename Monoid> struct SegmentTree { /*pairで処理*/ int sz; vector<Monoid> seg; const Monoid M1 = mp(MIN(T), -1); Monoid f(Monoid a, Monoid b) { return max(a, b); } void build(vector<T> &a) { int n = sz(a); sz = 1; while (sz < n) sz <<= 1; seg.assign(2 * sz, M1); rep(i, n) { seg[i + sz] = mp(a[i], i); } for (int k = sz - 1; k > 0; k--) { seg[k] = f(seg[k << 1], seg[(k << 1) | 1]); } } Monoid query(int a, int b) { Monoid L = M1, R = M1; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) L = f(L, seg[a++]); if (b & 1) R = f(seg[--b], R); } return f(L, R); } Monoid operator[](const int &k) const { return seg[k + sz]; } }; private: vector<T> ve; SegmentTree<pair<T, int>> seg; vector<T> rv; vector<T> ri; bool build = false; public: int n; ruimax(vector<T> &a) : ve(a), n(sz(a)) { int index = -1; int ma = MIN(T); rv.resize(n + 1); ri.resize(n + 1); rv[0] = -linf; ri[0] = -1; rep(i, n) { if (chma(ma, a[i])) { index = i; } rv[i + 1] = ma; ri[i + 1] = index; } } T operator()(int l, int r) { if (!(l <= r && 0 <= l && r <= n)) { deb(l, r, n); assert(0); } if (l == 0) { return rv[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).first; } } T operator()(int r = inf) { return operator()(0, min(r, n)); } T operator[](int r) { return operator()(0, r); } T getv(int l, int r) { return operator()(l, r); } T geti(int l, int r) { assert(l <= r && 0 <= l && r <= n); if (l == 0) { return ri[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).second; } } T geti(int r = inf) { return geti(0, min(r, n)); }; T getv(int r = inf) { return getv(0, min(r, n)); }; auto begin() { return rv.begin(); } auto end() { return rv.end(); } }; template <class T> struct ruimin { template <typename Monoid> struct SegmentTree { /*pairで処理*/ int sz; vector<Monoid> seg; const Monoid M1 = mp(MAX(T), -1); Monoid f(Monoid a, Monoid b) { return min(a, b); } void build(vector<T> &a) { int n = sz(a); sz = 1; while (sz < n) sz <<= 1; seg.assign(2 * sz, M1); rep(i, n) { seg[i + sz] = mp(a[i], i); } for (int k = sz - 1; k > 0; k--) { seg[k] = f(seg[k << 1], seg[(k << 1) | 1]); } } Monoid query(int a, int b) { Monoid L = M1, R = M1; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) L = f(L, seg[a++]); if (b & 1) R = f(seg[--b], R); } return f(L, R); } Monoid operator[](const int &k) const { return seg[k + sz]; } }; private: vector<T> ve; SegmentTree<pair<T, int>> seg; vector<T> rv; vector<T> ri; bool build = false; int n; public: ruimin(vector<T> &a) : ve(a), n(sz(a)) { int index = -1; int mi = MAX(T); rv.resize(n + 1); ri.resize(n + 1); rv[0] = linf; ri[0] = -1; rep(i, n) { if (chmi(mi, a[i])) { index = i; } rv[i + 1] = mi; ri[i + 1] = index; } } T operator()(int l, int r) { assert(l <= r && 0 <= l && r <= n); if (l == 0) { return rv[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).first; } } T operator()(int r = inf) { return operator()(0, min(r, n)); } T operator[](int r) { return operator()(0, r); } T getv(int l, int r) { return operator()(l, r); } T geti(int l, int r) { assert(l <= r && 0 <= l && r <= n); if (l == 0) { return ri[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).second; } } T geti(int r = inf) { return geti(0, min(r, n)); }; T getv(int r = inf) { return getv(0, min(r, n)); }; auto begin() { return rv.begin(); } auto end() { return rv.end(); } }; 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() { return ruiC<T>(); } template <class T> ruiC<T> ruic(vector<T> &a) { vector<T> ret = ruiv(a); return ruiC<T>(ret); } vvi() ruib(vi &a) { vvi(res, 61, sz(a) + 1); rep(k, 61) { rep(i, sz(a)) { res[k][i + 1] = res[k][i] + ((a[i] >> k) & 1); } } return res; } vector<ruiC<int>> ruibc(vi &a) { vector<ruiC<int>> ret(61); vvi(res, 61, sz(a)); rep(k, 61) { rep(i, sz(a)) { res[k][i] = (a[i] >> k) & 1; } ret[k] = ruic(res[k]); } return 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); } // h query template <class T> vector<T> imoh(vector<vector<T>> &v, int w) { vector<T> ret(sz(v)); rep(h, sz(ret)) { ret[h] = v[h][w]; } rep(i, sz(ret) - 1) { ret[i + 1] += ret[i]; } return ret; } template <class T> vector<T> ruih(vector<vector<T>> &v, int w) { vector<T> ret(sz(v) + 1); rep(h, sz(v)) { ret[h + 1] = v[h][w]; } rep(i, sz(v)) { ret[i + 1] += ret[i]; } return ret; } template <class T> ruiC<T> ruihc(vector<vector<T>> &a, int w) { vector<T> ret = ruih(a, w); return ruiC<T>(ret); } // xor template <class T> struct ruixC { 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> struct rruiC { vector<T> rui; int n; rruiC(vector<T> &a) : n(sz(a)) { rui.resize(n + 1); rer(i, n - 1) { rui[i] = rui[i + 1] + a[i]; } } /*[r l)*/ T operator()(int r, int l) { r++; l++; assert(l <= r && l >= 0 && r <= n); return rui[l] - rui[r]; } T operator()(int l) { return operator()(n - 1, l); } T operator[](int i) { return operator()(i); } }; template <class T> ostream &operator<<(ostream &os, rruiC<T> a) { fora(v, a.rui) os << v << " "; return os; } #define rrui rruic template <class T> rruiC<T> rruic(vector<T> &a) { return rruiC<T>(a); } //掛け算 template <class T> struct ruimulC { vector<T> rv; int n; ruimulC(vector<T> &a) : rv(a), n(sz(a)) { rv.resize(n + 1); rv[0] = 1; rep(i, n) { rv[i + 1] = a[i] * rv[i]; } } ruimulC() : n(0) { rv.resize(n + 1); rv[0] = 1; } void operator+=(T v) { rv.push_back(rv.back() * v); n++; } T operator()(int l, int r) { assert(l <= r && 0 <= l && r <= n); return rv[r] / rv[l]; } T operator()(int r = inf) { return operator()(0, min(r, n)); } T operator[](int r) { return operator()(0, r); } auto begin() { return rv.begin(); } auto end() { return rv.end(); } }; template <class T> ruimulC<T> ruimul(vector<T> &a) { return ruimulC<T>(a); } template <class T> ruimulC<T> ruimul() { vector<T> a; return ruimulC<T>(a); } /*@formatter:off*/ 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> void inc(pair<T, U> &a, U v = 1) { a.first += v, a.second += v; } template <class T, class U> void inc(T &a, U v = 1) { a += v; } template <class T, class U> void inc(vector<T> &a, U v = 1) { for (auto &u : a) inc(u, v); } template <class T, class U> void dec(T &a, U v = 1) { a -= v; } template <class T, class U> void dec(vector<T> &a, U v = 1) { for (auto &u : a) dec(u, v); } template <class U> void dec(string &a, U v = 1) { for (auto &u : a) dec(u, v); } template <class T, class U, class W> void dec(vector<T> &a, vector<U> &b, W v = 1) { for (auto &u : a) dec(u, v); for (auto &u : b) dec(u, v); } template <class T, class U, class W> void dec(vector<T> &a, vector<U> &b, vector<W> &c) { for (auto &u : a) dec(u, 1); for (auto &u : b) dec(u, 1); for (auto &u : c) dec(u, 1); } bool ins(ll h, ll w, ll H, ll W) { return h >= 0 && w >= 0 && h < H && w < W; } bool ins(ll l, ll v, ll r) { return l <= v && v < r; } template <class T> bool ins(vector<T> &a, ll i, ll j = 0) { return ins(0, i, sz(a)) && ins(0, j, sz(a)); } #define inside ins ll u(ll a) { return a < 0 ? 0 : a; } template <class T> vector<T> u(vector<T> &a) { vector<T> ret = a; fora(v, ret) v = u(v); return ret; } //添え字を返す template <class F> ll goldd_l(ll left, ll right, F calc) { double GRATIO = 1.6180339887498948482045868343656; ll lm = left + (ll)((right - left) / (GRATIO + 1.0)); ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); ll fl = calc(lm); ll fr = calc(rm); while (right - left > 10) { if (fl < fr) { right = rm; rm = lm; fr = fl; lm = left + (ll)((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } ll minScore = MAX(ll); ll resIndex = left; for (ll i = left; i < right + 1; ++i) { ll score = calc(i); if (minScore > score) { minScore = score; resIndex = i; } } return resIndex; } template <class F> ll goldt_l(ll left, ll right, F calc) { double GRATIO = 1.6180339887498948482045868343656; ll lm = left + (ll)((right - left) / (GRATIO + 1.0)); ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); ll fl = calc(lm); ll fr = calc(rm); while (right - left > 10) { if (fl > fr) { right = rm; rm = lm; fr = fl; lm = left + (ll)((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } if (left > right) { ll l = left; left = right; right = l; } ll maxScore = MIN(ll); ll resIndex = left; for (ll i = left; i < right + 1; ++i) { ll score = calc(i); if (maxScore < score) { maxScore = score; resIndex = i; } } return resIndex; } /*loopは200にすればおそらく大丈夫 余裕なら300に*/ template <class F> dou goldd_d(dou left, dou right, F calc, ll loop = 200) { dou GRATIO = 1.6180339887498948482045868343656; dou lm = left + ((right - left) / (GRATIO + 1.0)); dou rm = lm + ((right - lm) / (GRATIO + 1.0)); dou fl = calc(lm); dou fr = calc(rm); /*200にすればおそらく大丈夫*/ /*余裕なら300に*/ ll k = 141; loop++; while (--loop) { if (fl < fr) { right = rm; rm = lm; fr = fl; lm = left + ((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + ((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } return left; } template <class F> dou goldt_d(dou left, dou right, F calc, ll loop = 200) { double GRATIO = 1.6180339887498948482045868343656; dou lm = left + ((right - left) / (GRATIO + 1.0)); dou rm = lm + ((right - lm) / (GRATIO + 1.0)); dou fl = calc(lm); dou fr = calc(rm); loop++; while (--loop) { if (fl > fr) { right = rm; rm = lm; fr = fl; lm = left + ((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + ((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } return left; } // l ~ rを複数の区間に分割し、極致を与えるiを返す time-20 msまで探索 template <class F> ll goldd_ls(ll l, ll r, F calc, ll time = 2000) { auto lim = milliseconds(time - 20); ll mini = 0, minv = MAX(ll); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/ ll nl = l; ll nr = l + haba; rep(i, k) { ll ni = goldd_l(nl, nr, calc); if (chmi(minv, calc(ni))) mini = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return mini; } template <class F> ll goldt_ls(ll l, ll r, F calc, ll time = 2000) { auto lim = milliseconds(time - 20); ll maxi = 0, maxv = MIN(ll); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/ ll nl = l; ll nr = l + haba; rep(i, k) { ll ni = goldt_l(nl, nr, calc); if (chma(maxv, calc(ni))) maxi = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return maxi; } template <class F> dou goldd_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を持つ*/ auto lim = milliseconds(time - 20); dou mini = 0, minv = MAX(dou); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); dou haba = (r - l) / k; dou nl = l; dou nr = l + haba; rep(i, k) { dou ni = goldd_d(nl, nr, calc); if (chmi(minv, calc(ni))) mini = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return mini; } template <class F> dou goldt_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を残している*/ auto lim = milliseconds(time - 20); dou maxi = 0, maxv = MIN(dou); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); dou haba = (r - l) / k; dou nl = l; dou nr = l + haba; rep(i, k) { dou ni = goldt_d(nl, nr, calc); if (chma(maxv, calc(ni))) maxi = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return maxi; } template <class T> T min(vector<vector<T>> &a) { T res = MAX(T); rep(i, a.size()) chmi(res, *min_element(all(a[i]))); return res; } template <class T> T max(vector<vector<T>> &a) { T res = MIN(T); rep(i, a.size()) chma(res, *max_element(all(a[i]))); return res; } constexpr bool bget(ll m, ll keta) { #ifdef _DEBUG assert(keta <= 62); //オーバーフロー 1^62までしか扱えない #endif return (m >> keta) & 1; } // bget(n)次元 // NならN-1まで vector<vi> bget2(vi &a, int keta_size) { vvi(res, keta_size, sz(a)); rep(k, keta_size) { rep(i, sz(a)) { res[k][i] = bget(a[i], k); } } return res; } vi bget1(vi &a, int keta) { vi res(sz(a)); rep(i, sz(a)) { res[i] = bget(a[i], keta); } return res; } ll bget(ll m, ll keta, ll sinsuu) { m /= (ll)pow(sinsuu, keta); return m % sinsuu; } constexpr ll bit(ll n) { #ifdef _DEBUG assert(n <= 62); //オーバーフロー 1^62までしか扱えない #endif return (1LL << (n)); } ll bit(ll n, ll sinsuu) { return (ll)pow(sinsuu, n); } ll mask(ll n) { return (1ll << n) - 1; } // aをbitに置きなおす //{0, 2} -> 101 ll biti(vi &a) { int m = 0; for (auto &&v : a) m |= bit(v); return m; } //{1, 1, 0} -> 011 ll bitb(vi &a) { int m = 0; rep(i, sz(a)) if (a[i]) m |= bit(i); return m; } #define bcou __builtin_popcountll //最下位ビット ll lbit(ll n) { return n & -n; } ll lbiti(ll n) { return log2(n & -n); } //最上位ビット ll hbit(ll n) { n |= (n >> 1); n |= (n >> 2); n |= (n >> 4); n |= (n >> 8); n |= (n >> 16); n |= (n >> 32); return n - (n >> 1); } ll hbiti(ll n) { return log2(hbit(n)); } ll hbitk(ll n) { ll k = 0; rer(i, 5) { ll a = k + (1ll << i); ll b = 1ll << a; if (b <= n) k += 1ll << i; } return k; } //初期化は0を渡す ll nextComb(ll &mask, ll n, ll r) { if (!mask) return mask = (1LL << r) - 1; ll x = mask & -mask; /*最下位の1*/ ll y = mask + x; /*連続した下の1を繰り上がらせる*/ ll res = ((mask & ~y) / x >> 1) | y; if (bget(res, n)) return mask = 0; else return mask = res; } // n桁以下でビットがr個立っているもののvectorを返す vi bitCombList(ll n, ll r) { vi res; ll m = 0; while (nextComb(m, n, r)) { res.push_back(m); } return res; } // masの立ってるindexを見る #define forbit(i, mas) \ for (int forbitj = lbit(mas), forbitm = mas, i = log2(forbitj); forbitm; \ forbitm = forbitj ? forbitm ^ forbitj : 0, forbitj = lbit(forbitm), \ i = log2(forbitj)) char itoal(ll i) { return 'a' + i; } char itoaL(ll i) { return 'A' + i; } ll altoi(char c) { if ('A' <= c && c <= 'Z') return c - 'A'; return c - 'a'; } ll ctoi(char c) { return c - '0'; } char itoc(ll i) { return i + '0'; } ll vtoi(vi &v) { ll res = 0; if (sz(v) > 18) { debugline("vtoi"); deb(sz(v)); ole(); } rep(i, sz(v)) { res *= 10; res += v[i]; } return res; } vi itov(ll i) { vi res; while (i) { res.push_back(i % 10); i /= 10; } rev(res); return res; } vi stov(string &a) { ll n = sz(a); vi ret(n); rep(i, n) { ret[i] = a[i] - '0'; } return ret; } //基準を満たさないものは0になる vi stov(string &a, char one) { ll n = sz(a); vi ret(n); rep(i, n) ret[i] = a[i] == one; return ret; } vector<vector<ll>> ctoi(vector<vector<char>> s, char c) { ll n = sz(s), m = sz(s[0]); vector<vector<ll>> res(n, vector<ll>(m)); rep(i, n) rep(j, m) res[i][j] = s[i][j] == c; return res; } #define unique(v) v.erase(unique(v.begin(), v.end()), v.end()); //[i] := vを返す // aは0~n-1で置き換えられる vi compress(vi &a) { vi b; ll len = a.size(); for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { a[i] = lower_bound(all(b), a[i]) - b.begin(); } ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, umap<ll, ll> &map) { vi b; ll len = a.size(); for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { ll v = a[i]; a[i] = lower_bound(all(b), a[i]) - b.begin(); map[v] = a[i]; } ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, vi &r) { vi b; ll len = a.size(); fora(v, a) b.push_back(v); fora(v, r) b.push_back(v); sort(b); unique(b); for (ll i = 0; i < len; ++i) a[i] = lower_bound(all(b), a[i]) - b.begin(); for (ll i = 0; i < sz(r); ++i) r[i] = lower_bound(all(b), r[i]) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, vi &r, vi &s) { vi b; ll len = a.size(); fora(v, a) b.push_back(v); fora(v, r) b.push_back(v); fora(v, s) b.push_back(v); sort(b); unique(b); for (ll i = 0; i < len; ++i) a[i] = lower_bound(all(b), a[i]) - b.begin(); for (ll i = 0; i < sz(r); ++i) r[i] = lower_bound(all(b), r[i]) - b.begin(); for (ll i = 0; i < sz(s); ++i) r[i] = lower_bound(all(b), s[i]) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vector<vi> &a) { vi b; fora(vv, a) fora(v, vv) b.push_back(v); sort(b); unique(b); fora(vv, a) fora(v, vv) v = lower_bound(all(b), v) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vector<vector<vi>> &a) { vi b; fora(vvv, a) fora(vv, vvv) fora(v, vv) b.push_back(v); sort(b); unique(b); fora(vvv, a) fora(vv, vvv) fora(v, vv) v = lower_bound(all(b), v) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } void compress(ll a[], ll len) { vi b; for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { a[i] = lower_bound(all(b), a[i]) - b.begin(); } } //要素が見つからなかったときに困る #define binarySearch(a, v) (binary_search(all(a), v)) #define lowerIndex(a, v) (lower_bound(all(a), v) - a.begin()) #define upperIndex(a, v) (upper_bound(all(a), v) - a.begin()) #define rlowerIndex(a, v) (upper_bound(all(a), v) - a.begin() - 1) #define rupperIndex(a, v) (lower_bound(all(a), v) - a.begin() - 1) template <class T, class U, class W> T lowerBound(vector<T> &a, U v, W banpei) { auto it = lower_bound(a.begin(), a.end(), v); if (it == a.end()) return banpei; else return *it; } template <class T, class U, class W> T lowerBound(ruiC<T> &a, U v, W banpei) { return lowerBound(a.rui, v, banpei); } template <class T, class U, class W> T upperBound(vector<T> &a, U v, W banpei) { auto it = upper_bound(a.begin(), a.end(), v); if (it == a.end()) return banpei; else return *it; } template <class T, class U, class W> T upperBound(ruiC<T> &a, U v, W banpei) { return upperBound(a.rui, v, banpei); } template <class T, class U, class W> T rlowerBound(vector<T> &a, U v, W banpei) { auto it = upper_bound(a.begin(), a.end(), v); if (it == a.begin()) return banpei; else { return *(--it); } } template <class T, class U, class W> T rlowerBound(ruiC<T> &a, U v, W banpei) { return rlowerBound(a.rui, v, banpei); } template <class T, class U, class W> T rupperBound(vector<T> &a, U v, W banpei) { auto it = lower_bound(a.begin(), a.end(), v); if (it == a.begin()) return banpei; else { return *(--it); } } template <class T, class U, class W> T rupperBound(ruiC<T> &a, U v, W banpei) { return rupperBound(a.rui, v, banpei); } #define next2(a) next(next(a)) #define prev2(a) prev(prev(a)) //狭義の単調増加列 長さを返す template <class T> int lis(vector<T> &a) { int n = sz(a); vi tail(n + 1, MAX(T)); rep(i, n) { int id = lowerIndex(tail, a[i]); /**/ tail[id] = a[i]; } return lowerIndex(tail, MAX(T)); } template <class T> int lis_eq(vector<T> &a) { int n = sz(a); vi tail(n + 1, MAX(T)); rep(i, n) { int id = upperIndex(tail, a[i]); /**/ tail[id] = a[i]; } return lowerIndex(tail, MAX(T)); } // iteratorを返す // valueが1以上の物を返す 0は見つけ次第削除 // vを減らす場合 (*it).se--でいい template <class T, class U, class V> auto lower_map(map<T, U> &m, V k) { auto ret = m.lower_bound(k); while (ret != m.end() && (*ret).second == 0) { ret = m.erase(ret); } return ret; } template <class T, class U, class V> auto upper_map(map<T, U> &m, V k) { auto ret = m.upper_bound(k); while (ret != m.end() && (*ret).second == 0) { ret = m.erase(ret); } return ret; } //存在しなければエラー template <class T, class U, class V> auto rlower_map(map<T, U> &m, V k) { auto ret = upper_map(m, k); assert(ret != m.begin()); ret--; while (1) { if ((*ret).second != 0) break; assert(ret != m.begin()); auto next = ret; --next; m.erase(ret); ret = next; } return ret; } template <class T, class U, class V> auto rupper_map(map<T, U> &m, V k) { auto ret = lower_map(m, k); assert(ret != m.begin()); ret--; while (1) { if ((*ret).second != 0) break; assert(ret != m.begin()); auto next = ret; --next; m.erase(ret); ret = next; } return ret; } template <class... T> void fin(T... s) { out(s...); exit(0); } //便利 数学 math // sub ⊂ top bool subset(int sub, int top) { return (sub & top) == sub; } //-180 ~ 180 degree double atand(double h, double w) { return atan2(h, w) / PI * 180; } //% -mの場合、最小の正の数を返す ll mod(ll a, ll m) { if (m < 0) m *= -1; return (a % m + m) % m; } ll pow(ll a) { return a * a; }; ll fact(ll v) { return v <= 1 ? 1 : v * fact(v - 1); } dou factd(int v) { static vd fact(2, 1); if (sz(fact) <= v) { rep(i, sz(fact), v + 1) { fact.push_back(fact.back() * i); } } return fact[v]; } ll comi(ll n, ll r) { assert(n < 100); static vvi(pas, 100, 100); if (pas[0][0]) return pas[n][r]; pas[0][0] = 1; rep(i, 1, 100) { pas[i][0] = 1; rep(j, 1, i + 1) pas[i][j] = pas[i - 1][j - 1] + pas[i - 1][j]; } return pas[n][r]; } double comd2(ll n, ll r) { static vvd(comb, 2020, 2020); if (comb[0][0] == 0) { comb[0][0] = 1; rep(i, 2000) { comb[i + 1][0] = 1; rep(j, 1, i + 2) { comb[i + 1][j] = comb[i][j] + comb[i][j - 1]; } } } return comb[n][r]; } double comd(int n, int r) { if (r < 0 || r > n) return 0; if (n < 2020) return comd2(n, r); static vd fact(2, 1); if (sz(fact) <= n) { rep(i, sz(fact), n + 1) { fact.push_back(fact.back() * i); } } return fact[n] / fact[n - r] / fact[r]; } ll gcd(ll a, ll b) { while (b) a %= b, swap(a, b); return abs(a); } ll gcd(vi b) { ll res = b[0]; rep(i, 1, sz(b)) res = gcd(b[i], res); return res; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll lcm(vi a) { ll res = a[0]; rep(i, 1, sz(a)) res = lcm(a[i], res); return res; } ll ceil(ll a, ll b) { if (b == 0) { debugline("ceil"); deb(a, b); ole(); return -1; } else if (a < 0) { return 0; } else { return (a + b - 1) / b; } } ll sig0(ll t) { return t <= 0 ? 0 : ((1 + t) * t) >> 1; } // ll sig(ll s, ll t) { return ((s + t) * (t - s + 1)) >> 1; } ll sig(ll s, ll t) { if (s > t) swap(s, t); return sig0(t - s) + s * (t - s + 1); } #define tousa_i tosa_i #define lower_tousa_i lower_tosa_i #define upper_tousa upper_tosa #define upper_tousa_i upper_tosa_i ll tosa_i(ll st, ll ad, ll v) { assert(((v - st) % ad) == 0); return (v - st) / ad; } ll tosa_s(ll st, ll ad, ll len) { return st * len + sig0(len - 1) * ad; } // ax + r (x は非負整数) で表せる整数のうち、v 以上となる最小の整数 ll lower_tosa(ll st, ll ad, ll v) { if (st >= v) return st; return (v - st + ad - 1) / ad * ad + st; } //第何項か ll lower_tosa_i(ll st, ll ad, ll v) { if (st >= v) return 0; return (v - st + ad - 1) / ad; } ll upper_tosa(ll st, ll ad, ll v) { return lower_tosa(st, ad, v + 1); } ll upper_tosa_i(ll st, ll ad, ll v) { return lower_tosa_i(st, ad, v + 1); } // v * v >= aとなる最小のvを返す 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); } /*@formatter:off*/ //機能拡張 #define dtie(a, b) \ int a, b; \ tie(a, b) template <class T, class U> string to_string(T a, U b) { string res = ""; res += a; res += b; return res; } template <class T, class U, class V> string to_string(T a, U b, V c) { string res = ""; res += a; res += b; res += c; return res; } template <class T, class U, class V, class W> string to_string(T a, U b, V c, W d) { string res = ""; res += a; res += b; res += c; res += d; return res; } template <class T, class U, class V, class W, class X> string to_string(T a, U b, V c, W d, X e) { string res = ""; res += a; res += b; res += c; res += d; res += e; return res; } constexpr int bsetlen = k5 * 2; // constexpr int bsetlen = 5050; #define bset bitset<bsetlen> bool operator<(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] < b[i]) return true; if (a[i] > b[i]) return false; } return false; } bool operator>(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] > b[i]) return true; if (a[i] < b[i]) return false; } return false; } bool operator<=(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] < b[i]) return true; if (a[i] > b[i]) return false; } return true; } bool operator>=(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] > b[i]) return true; if (a[i] < b[i]) return false; } return true; } string operator~(string &a) { string res = a; for (auto &&c : res) { if (c == '0') c = '1'; else if (c == '1') c = '0'; else { cerr << "cant ~" << a << "must bit" << endl; exit(0); } } return res; } ostream &operator<<(ostream &os, bset &a) { bitset<10> b; vi list; rep(i, bsetlen) { if (a[i]) list.push_back(i), b[i] = 1; } os << b << ", " << list; return os; } int hbiti(bset &a) { rer(i, bsetlen) { if (a[i]) return i; } return -1; } #define hk(a, b, c) (a <= b && b < c) // O(N/64) bset nap(bset &a, int v) { bset r = a | a << v; return r; } bset nap(bset &a, bset &v) { bset r = a; rep(i, bsetlen) { if (v[i]) r |= a << i; } return r; } template <class T> void seth(vector<vector<T>> &S, int w, vector<T> &v) { assert(sz(S) == sz(v)); assert(w < sz(S[0])); rep(h, sz(S)) { S[h][w] = v[h]; } } template <class T, class U> void operator+=(pair<T, U> &a, pair<T, U> &b) { a.fi += b.fi; a.se += b.se; } template <class T, class U> pair<T, U> operator+(pair<T, U> &a, pair<T, U> &b) { return pair<T, U>(a.fi + b.fi, a.se + b.se); } template <typename CharT, typename Traits, typename Alloc> basic_string<CharT, Traits, Alloc> operator+(const basic_string<CharT, Traits, Alloc> &lhs, const int rv) { basic_string<CharT, Traits, Alloc> str(lhs); str.append(to_string(rv)); return str; } template <typename CharT, typename Traits, typename Alloc> void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const int rv) { lhs += to_string(rv); } template <typename CharT, typename Traits, typename Alloc> basic_string<CharT, Traits, Alloc> operator+(const basic_string<CharT, Traits, Alloc> &lhs, const signed rv) { basic_string<CharT, Traits, Alloc> str(lhs); str.append(to_string(rv)); return str; } template <typename CharT, typename Traits, typename Alloc> void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const signed rv) { lhs += to_string(rv); } template <typename CharT, typename Traits, typename Alloc> void operator*=(basic_string<CharT, Traits, Alloc> &s, int num) { auto bek = s; s = ""; for (; num; num >>= 1) { if (num & 1) { s += bek; } bek += bek; } } template <class T, class U> void operator+=(queue<T> &a, U v) { a.push(v); } template <class T, class U> void operator+=(deque<T> &a, U v) { a.push_back(v); } template <class T> priority_queue<T, vector<T>, greater<T>> & operator+=(priority_queue<T, vector<T>, greater<T>> &a, vector<T> &v) { fora(d, v) a.push(d); return a; } template <class T, class U> priority_queue<T, vector<T>, greater<T>> & operator+=(priority_queue<T, vector<T>, greater<T>> &a, U v) { a.push(v); return a; } template <class T, class U> priority_queue<T> &operator+=(priority_queue<T> &a, U v) { a.push(v); return a; } template <class T> set<T> &operator+=(set<T> &a, vector<T> v) { fora(d, v) a.insert(d); return a; } template <class T, class U> auto operator+=(set<T> &a, U v) { return a.insert(v); } template <class T, class U> auto operator-=(set<T> &a, U v) { return a.erase(v); } template <class T, class U> auto operator+=(mset<T> &a, U v) { return a.insert(v); } template <class T, class U> set<T, greater<T>> &operator+=(set<T, greater<T>> &a, U v) { a.insert(v); return a; } template <class T, class U> vector<T> &operator+=(vector<T> &a, U v) { a.push_back(v); return a; } template <class T, class U> vector<T> operator+(vector<T> &a, U v) { vector<T> ret = a; ret += v; return ret; } template <class T, class U> vector<T> operator+(U v, vector<T> &a) { vector<T> ret = a; ret.insert(ret.begin(), v); return ret; } template <class T> vector<T> operator+(vector<T> a, vector<T> b) { vector<T> ret; ret = a; fora(v, b) ret += v; return ret; } template <class T> vector<T> &operator+=(vector<T> &a, vector<T> &b) { rep(i, sz(b)) { /*こうしないとa+=aで両辺が増え続けてバグる*/ a.push_back(b[i]); } return a; } template <class T, class U> map<T, U> &operator+=(map<T, U> &a, map<T, U> &b) { fora(bv, b) { a[bv.first] += bv.second; } return a; } template <class T> vector<T> &operator-=(vector<T> &a, vector<T> &b) { if (sz(a) != sz(b)) { debugline("vector<T> operator-="); deb(a); deb(b); exit(0); } rep(i, sz(a)) a[i] -= b[i]; return a; } template <class T> vector<T> operator-(vector<T> &a, vector<T> &b) { if (sz(a) != sz(b)) { debugline("vector<T> operator-"); deb(a); deb(b); ole(); } vector<T> res(sz(a)); rep(i, sz(a)) res[i] = a[i] - b[i]; return res; } template <class T, class U> void operator*=(vector<T> &a, U b) { vector<T> ta = a; rep(b - 1) { a += ta; } } template <typename T> void erase(vector<T> &v, unsigned ll i) { v.erase(v.begin() + i); } template <typename T> void erase(vector<T> &v, unsigned ll s, unsigned ll e) { v.erase(v.begin() + s, v.begin() + e); } template <class T, class U> void erase(map<T, U> &m, ll okl, ll ngr) { m.erase(m.lower_bound(okl), m.lower_bound(ngr)); } template <class T> void erase(set<T> &m, ll okl, ll ngr) { m.erase(m.lower_bound(okl), m.lower_bound(ngr)); } template <typename T> void erasen(vector<T> &v, unsigned ll s, unsigned ll n) { v.erase(v.begin() + s, v.begin() + s + n); } template <typename T, typename U> void insert(vector<T> &v, unsigned ll i, U t) { v.insert(v.begin() + i, t); } template <typename T, typename U> void push_front(vector<T> &v, U t) { v.insert(v.begin(), t); } template <typename T, typename U> void insert(vector<T> &v, unsigned ll i, vector<T> list) { for (auto &&va : list) v.insert(v.begin() + i++, va); } template <typename T> void insert(set<T> &v, vector<T> list) { for (auto &&va : list) v.insert(va); } vector<string> split(const string a, const char deli) { string b = a + deli; ll l = 0, r = 0, n = b.size(); vector<string> res; rep(i, n) { if (b[i] == deli) { r = i; if (l < r) res.push_back(b.substr(l, r - l)); l = i + 1; } } return res; } vector<string> split(const string a, const string deli) { vector<string> res; ll kn = sz(deli); std::string::size_type Pos(a.find(deli)); ll l = 0; while (Pos != std::string::npos) { if (Pos - l) res.push_back(a.substr(l, Pos - l)); l = Pos + kn; Pos = a.find(deli, Pos + kn); } if (sz(a) - l) res.push_back(a.substr(l, sz(a) - l)); return res; } 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 fAb(bool a) { if (a) cout << "Alice" << endl; else cout << "Bob"; } 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 <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)}; } //未分類 // 0,2,1 1番目と2番目の次元を入れ替える template <class T> auto irekae(vector<vector<vector<T>>> &A, int x, int y, int z) { #define irekae_resize_loop(a, b, c) \ resize(res, a, b, c); \ rep(i, a) rep(j, b) rep(k, c) vector<vector<vector<T>>> res; if (x == 0 && y == 1 && z == 2) { res = A; } else if (x == 0 && y == 2 && z == 1) { irekae_resize_loop(sz(A), sz(A[0][0]), sz(A[0])) { res[i][j][k] = A[i][k][j]; } } else if (x == 1 && y == 0 && z == 2) { irekae_resize_loop(sz(A[0]), sz(A), sz(A[0][0])) { res[i][j][k] = A[j][i][k]; } } else if (x == 1 && y == 2 && z == 0) { irekae_resize_loop(sz(A[0]), sz(A[0][0]), sz(A)) { res[i][j][k] = A[k][i][j]; } } else if (x == 2 && y == 0 && z == 1) { irekae_resize_loop(sz(A[0][0]), sz(A), sz(A[0])) { res[i][j][k] = A[j][k][i]; } } else if (x == 2 && y == 1 && z == 0) { irekae_resize_loop(sz(A[0][0]), sz(A[0]), sz(A)) { res[i][j][k] = A[k][j][i]; } } return res; #undef irekae_resize_loop } template <class T> auto irekae(vector<vector<T>> &A, int i = 1, int j = 0) { vvt(res, sz(A[0]), sz(A)); rep(i, sz(A)) { rep(j, sz(A[0])) { res[j][i] = A[i][j]; } } return res; } // tou分割する template <typename T> vector<vector<T>> cut(vector<T> &a, int tou = 2) { int N = sz(a); vector<vector<T>> res(tou); int hab = N / tou; vi lens(tou, hab); rep(i, N % tou) { lens[tou - 1 - i]++; } int l = 0; rep(i, tou) { int len = lens[i]; int r = l + len; res[i].resize(len); std::copy(a.begin() + l, a.begin() + r, res[i].begin()); l = r; } return res; } //長さn毎に分割する template <typename T> vector<vector<T>> cutn(vector<T> &a, int len) { int N = sz(a); vector<vector<T>> res(ceil(N, len)); vi lens(N / len, len); if (N % len) lens.push_back(N % len); int l = 0; rep(i, sz(lens)) { int len = lens[i]; int r = l + len; res[i].resize(len); std::copy(a.begin() + l, a.begin() + r, res[i].begin()); l = r; } return res; } vi inds_(vi &a) { int n = sz(a); vb was(n); vi res(n); rep(i, n) { assert(!was[a[i]]); res[a[i]] = i; was[a[i]] = true; } return res; } //@起動時 struct initon { initon() { cin.tie(0); ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(16); srand((unsigned)clock() + (unsigned)time(NULL)); }; } initonv; #define pre prev #define nex next // gra mll pr //上下左右 const string udlr = "udlr"; string UDLR = "UDLR"; // x4と連動 UDLR.find('U') := x4[0] //右、上が正 constexpr ll h4[] = {1, -1, 0, 0}; constexpr ll w4[] = {0, 0, -1, 1}; constexpr ll h8[] = {0, 1, 0, -1, -1, 1, 1, -1}; constexpr ll w8[] = {1, 0, -1, 0, 1, -1, 1, -1}; int mei_inc(int h, int w, int H, int W, int i) { while (++i < 4) { if (inside(h + h4[i], w + w4[i], H, W)) return i; } return i; } #define mei(nh, nw, h, w) \ for (int i = mei_inc(h, w, H, W, -1), nh = i < 4 ? h + h4[i] : 0, \ nw = i < 4 ? w + w4[i] : 0; \ i < 4; i = mei_inc(h, w, H, W, i), nh = h + h4[i], nw = w + w4[i]) //グラフ内で #undef getid //#define getidとしているため、ここを書き直したらgraphも書き直す #define getid_2(h, w) (h * W + w) #define getid_1(p) (p.first * W + p.second) #define getid(...) over2(__VA_ARGS__, getid_2, getid_1)(__VA_ARGS__) #define getp(id) mp(id / W, id % W) //#define set_shuffle() std::random_device seed_gen;std::mt19937 //engine(seed_gen()) #define shuffle(a) std::shuffle((a).begin(), (a).end(), //engine); 1980 開始からtime ms経っていたらtrue bool timeup(int time) { auto end_time = system_clock::now(); auto part = duration_cast<milliseconds>(end_time - start_time); auto lim = milliseconds(time); return part >= lim; } void set_time() { past_time = system_clock::now(); } // MS型(millisecqnds)で返る // set_timeをしてからの時間 auto calc_time_milli() { auto now = system_clock::now(); auto part = duration_cast<milliseconds>(now - past_time); return part; } auto calc_time_micro() { auto now = system_clock::now(); auto part = duration_cast<microseconds>(now - past_time); return part; } auto calc_time_nano() { auto now = system_clock::now(); auto part = duration_cast<nanoseconds>(now - past_time); return part; } bool calc_time(int zikan) { return calc_time_micro() >= microseconds(zikan); } using MS = std::chrono::microseconds; int div(microseconds a, microseconds b) { return a / b; } int div(nanoseconds a, nanoseconds b) { if (b < nanoseconds(1)) { return a / nanoseconds(1); } int v = a / b; return v; } // set_time(); // rep(i,lim)shori // lim*=time_nanbai(); // rep(i,lim)shoriと使う //全体でmilliかかっていいときにlimを何倍してもう一回できるかを返す int time_nanbai(int milli) { auto dec = duration_cast<nanoseconds>(past_time - start_time); auto part = calc_time_nano(); auto can_time = nanoseconds(milli * 1000 * 1000); can_time -= part; can_time -= dec; return div(can_time, part); } /*@formatter:on*/ // vectorで取れる要素数 // bool=> 1e9 * 8.32 // int => 1e8 * 2.6 // ll => 1e8 * 1.3 // 3次元以上取るとメモリがヤバい // static配列を使う vvc(ba); ll N, M, H, W; vi A, B, C; //遅い 文字列以外も扱えるはず template <class T = string> struct rolling_hash2 { //@formatter:off const int b[2] = {11731, 11839}; const int m[2] = {1000002571LL, 1000002631LL}; int n; T s; // stringやvector vector<long long> hash[2], pow[2]; rolling_hash2() {} rolling_hash2(T &S) : s(S) { n = (int)S.size(); for (int i = 0; i < 2; ++i) { hash[i].assign(n + 1, 0); pow[i].assign(n + 1, 1); for (int j = 0; j < n; ++j) { hash[i][j + 1] = (hash[i][j] * b[i] + S[j]) % m[i]; pow[i][j + 1] = pow[i][j] * b[i] % m[i]; } } } // get hash of S[left:right] ll get(int l, int r, int id) { ll res = hash[id][r] - hash[id][l] * pow[id][r - l] % m[id]; if (res < 0) res += m[id]; return res; } ll get(int l, int r) { return (get(l, r, 0) << 32) | get(l, r, 1); } ll operator()(int l, int r) { return get(l, r, 0); } ll operator()() { return get(0, n, 0); } // get lcp of S[a:] and S[b:] int lcp(int a, int b) { int len = min((int)s.size() - a, (int)s.size() - b); int low = -1, high = len + 1; while (high - low > 1) { int mid = (low + high) / 2; if (get(a, a + mid, 0) != get(b, b + mid, 0)) high = mid; else if (get(a, a + mid, 1) != get(b, b + mid, 1)) high = mid; else low = mid; } return low; } // get lcp of S[a:] and T[b:] int lcp(rolling_hash2 &t, int a, int b) { int len = min((int)s.size() - a, (int)s.size() - b); int low = -1, high = len + 1; while (high - low > 1) { int mid = (low + high) / 2; if (get(a, a + mid, 0) != get(b, b + mid, 0)) high = mid; else if (get(a, a + mid, 1) != get(b, b + mid, 1)) high = mid; else low = mid; } return low; } }; //早い struct rolling_hash { static constexpr int base1 = 1007, base2 = 2009; static constexpr int mod1 = 1000000007, mod2 = 1000000009; vector<long long> hash1, hash2, power1, power2; int n; rolling_hash(string &S) { n = (int)S.size(); hash1.assign(n + 1, 0); hash2.assign(n + 1, 0); power1.assign(n + 1, 1); power2.assign(n + 1, 1); for (int i = 0; i < n; ++i) { hash1[i + 1] = (hash1[i] * base1 + S[i]) % mod1; hash2[i + 1] = (hash2[i] * base2 + S[i]) % mod2; power1[i + 1] = (power1[i] * base1) % mod1; power2[i + 1] = (power2[i] * base2) % mod2; } } template <class T> rolling_hash(vector<T> &S) { n = (int)S.size(); hash1.assign(n + 1, 0); hash2.assign(n + 1, 0); power1.assign(n + 1, 1); power2.assign(n + 1, 1); for (int i = 0; i < n; ++i) { hash1[i + 1] = (hash1[i] * base1 + S[i]) % mod1; hash2[i + 1] = (hash2[i] * base2 + S[i]) % mod2; power1[i + 1] = (power1[i] * base1) % mod1; power2[i + 1] = (power2[i] * base2) % mod2; } } inline pair<int, int> get(int l, int r) { long long res1 = hash1[r] - hash1[l] * power1[r - l] % mod1; if (res1 < 0) res1 += mod1; long long res2 = hash2[r] - hash2[l] * power2[r - l] % mod2; if (res2 < 0) res2 += mod2; return {res1, res2}; } inline pair<int, int> operator()(int l, int r) { return get(l, r); } inline pair<int, int> operator()() { return get(0, n); } // get lcp of S[a:] and T[b:] //接頭語がどれだけ一致するか //範囲の重複を含む inline int lcp_dup(int a, int b) { int len = min((int)hash1.size() - a, (int)hash1.size() - b); int low = 0, high = len; while (high - low > 1) { int mid = (low + high) >> 1; if (get(a, a + mid) != get(b, b + mid)) high = mid; else low = mid; } return low; } //重複を認めない inline int lcp_not(int a, int b) { return min(lcp_dup(a, b), b - a); } }; // hashを共有したくない場合、中に書く const uint64_t base = rand((uint64_t)868491463, ((uint64_t)1 << 61) - 1 - 1); //安全で爆速なrollinghash class roll { static constexpr uint64_t mask30 = ((uint64_t)1 << 30) - 1; static constexpr uint64_t mask31 = ((uint64_t)1 << 31) - 1; static constexpr uint64_t mask61 = ((uint64_t)1 << 61) - 1; static constexpr uint64_t offset = mask61 * 7; static constexpr uint64_t mod(const uint64_t x) { const uint64_t y = (x >> 61) + (x & mask61); return y >= mask61 ? y - mask61 : y; } static constexpr uint64_t mul(const uint64_t x, const uint64_t y) { const uint64_t xh = x >> 31, xl = x & mask31; const uint64_t yh = y >> 31, yl = y & mask31; const uint64_t z = xl * yh + xh * yl; const uint64_t zh = z >> 30, zl = z & mask30; return xh * yh * 2 + zh + (zl << 31) + xl * yl; } const std::size_t siz; vector<uint64_t> pow, h; public: template <typename T> roll(T &A) : siz(sz(A)), pow(siz + 1, 1), h(siz + 1, 0) { for (std::size_t i = 1; i <= siz; i++) { pow[i] = mod(mul(pow[i - 1], base)), h[i] = mod(mul(h[i - 1], base) + A[i - 1]); } } uint64_t operator()(const std::size_t l, const std::size_t r) { return mod(h[r] + offset - mul(h[l], pow[r - l])); } uint64_t operator()(int r = inf) { return operator()(0, min(siz, r)); } // get lcp of S[a:] and T[b:] //接頭語がどれだけ一致するか //範囲の重複を含む int lcp_dup(int a, int b) { int len = min((int)siz - a, (int)siz - b); int low = 0, high = len; while (high - low > 1) { int mid = (low + high) >> 1; if (operator()(a, a + mid) != operator()(b, b + mid)) high = mid; else low = mid; } return low; } //重複を認めない int lcp_not(int a, int b) { return min(lcp_dup(a, b), b - a); } //@formatter:on }; //動かない場合roll2 // roll ro(list); void solve() { cin >> N; vi A(N), B(N), A2(N), B2(N); rep(i, 0, N) cin >> A[i]; rep(i, 0, N) cin >> B[i]; rep(i, 0, N) A2[i] = A[i] ^ A[(i + 1) % N]; rep(i, 0, N) B2[i] = B[i] ^ B[(i + 1) % N]; vector<int> v; rep(i, 0, N) v.push_back(A2[i]); rep(i, 0, N) v.push_back(A2[i]); rep(i, 0, N) v.push_back(B2[i]); roll rh(v); rep(k, 0, N) if (rh(k, k + N - 2) == rh(N * 2, N * 2 + N - 2)) { int x = A[k] ^ B[0]; out(k, x); } } auto my(ll n, vi &a) { return 0; } auto sister(ll n, vi &a) { ll ret = 0; return ret; } signed main() { solve(); #define arg n, a #ifdef _DEBUG bool bad = 0; for (ll i = 0, ok = 1; i < k5 && ok; ++i) { ll n = rand(1, 8); vi a = ranv(n, 1, 10); auto myres = my(arg); auto res = sister(arg); ok = myres == res; if (!ok) { out(arg); cerr << "AC : " << res << endl; cerr << "MY : " << myres << endl; bad = 1; break; } } if (!bad) { // cout << "完璧 : solveを書き直そう" << endl; // cout << " : そして、solve()を呼び出すのだ" << endl; // cout << " : cin>>n; na(a,n);も忘れるな" << endl; } #endif return 0; };
//#undef _DEBUG //#pragma GCC optimize("Ofast") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace std::chrono; #define int long long // todo 消したら動かない intの代わりにsignedを使う #define ll long long auto start_time = system_clock::now(); auto past_time = system_clock::now(); #define debugName(VariableName) #VariableName //最大引数がN #define over2(o1, o2, name, ...) name #define over3(o1, o2, o3, name, ...) name #define over4(o1, o2, o3, o4, name, ...) name #define over5(o1, o2, o3, o4, o5, name, ...) name #define over6(o1, o2, o3, o4, o5, o6, name, ...) name #define over7(o1, o2, o3, o4, o5, o6, o7, name, ...) name #define over8(o1, o2, o3, o4, o5, o6, o7, o8, name, ...) name #define over9(o1, o2, o3, o4, o5, o6, o7, o8, o9, name, ...) name #define over10(o1, o2, o3, o4, o5, o6, o7, o8, o9, o10, name, ...) name /*@formatter:off*/ //[-n, n)にアクセスできる //また、外部関数resizeに渡せる // sizeは[-n, n)でnを返す template <class T> class mvec { vector<T> v; int n; public: mvec() : n(0), v(0) {} mvec(int n) : n(n), v(n * 2) {} mvec(int n, T val) : n(n), v(n * 2, val) {} auto &operator[](int i) { return v[i + n]; } auto size() { return n; } void resize(int sn) { assert(n == 0); n = sn; v.resize(sn * 2); } auto begin() { return v.begin(); } auto rbegin() { return v.rbegin(); } auto end() { return v.end(); } auto rend() { return v.rend(); } }; //[]でboolは参照を返さないため特殊化が必要 template <> struct mvec<bool> { vector<bool> v; int n; mvec() : n(0), v(0) {} mvec(int n) : n(n), v(n * 2) {} mvec(int n, bool val) : n(n), v(n * 2, val) {} auto operator[](int i) { return v[i + n]; } auto size() { return v.size(); } void resize(int sn) { assert(n == 0); n = sn; v.resize(sn * 2); } auto begin() { return v.begin(); } auto rbegin() { return v.rbegin(); } auto end() { return v.end(); } auto rend() { return v.rend(); } }; template <class T> ostream &operator<<(ostream &os, mvec<T> &a) { int spa = 3; for (auto &&v : a) { spa = max(spa, (int)(to_string(v).size()) + 1); } int n = (int)a.size(); os << endl; for (int i = -n; i < n; i++) { int need = spa - ((int)to_string(i).size()); if (i == -n) { need -= min(need, spa - ((int)to_string(a[i]).size())); } while (need--) { os << " "; } os << i; } os << endl; int i = -n; for (auto &&v : a) { int need = spa - ((int)to_string(v).size()); if (i == -n) { need -= min(need, spa - ((int)to_string(i).size())); } while (need--) { os << " "; } os << v; i++; } return os; } #define mv mvec #define MV mvec using mvi = mvec<ll>; using mvb = mvec<bool>; using mvs = mvec<string>; using mvd = mvec<double>; using mvc = mvec<char>; #define mvvt0(t) mvec<mvec<t>> #define mvvt1(t, a) mvec<mvec<t>> a #define mvvt2(t, a, b) mvec<mvec<t>> a(b) #define mvvt3(t, a, b, c) mvec<mvec<t>> a(b, mvec<t>(c)) #define mvvt4(t, a, b, c, d) mvec<mvec<t>> a(b, mvec<t>(c, d)) #define mvvi(...) \ over4(__VA_ARGS__, mvvt4, mvvt3, mvvt2, mvvt1, mvvt0)(ll, __VA_ARGS__) template <typename T> mvec<T> make_mv(size_t a) { return mvec<T>(a); } template <typename T, typename... Ts> auto make_mv(size_t a, Ts... ts) { return mvec<decltype(make_mv<T>(ts...))>(a, make_mv<T>(ts...)); } #define mvni(name, ...) auto name = make_mv<ll>(__VA_ARGS__) #ifdef _DEBUG // https://marycore.jp/prog/cpp/class-extension-methods/ 違うかも template <class T, class A = std::allocator<T>> struct debtor : std::vector<T, A> { using std::vector<T, A>::vector; template <class U> int deb_v(U a, int v) { return v; } template <class U> int deb_v(debtor<U> &a, int v = 0) { cerr << a.size() << " "; return deb_v(a.at(0), v + 1); } template <class U> void deb_o(U a) { cerr << a << " "; } template <class U> void deb_o(debtor<U> &a) { for (int i = 0; i < min((int)a.size(), 15ll); i++) { deb_o(a[i]); } if ((int)a.size() > 15) { cerr << "..."; } cerr << endl; } typename std::vector<T>::reference operator[](typename std::vector<T>::size_type n) { if (n < 0 || n >= (int)this->size()) { int siz = (int)this->size(); cerr << "vector size = "; int dim = deb_v((*this)); cerr << endl; cerr << "out index at " << n << endl; cerr << endl; if (dim <= 2) { deb_o((*this)); } exit(0); } return this->at(n); } }; #define vector debtor #endif #ifdef _DEBUG //区間削除は出来ない template <class T> struct my_pbds_tree { set<T> s; auto begin() { return s.begin(); } auto end() { return s.end(); } auto rbegin() { return s.rbegin(); } auto rend() { return s.rend(); } auto empty() { return s.empty(); } auto size() { return s.size(); } void clear() { s.clear(); } template <class U> void insert(U v) { s.insert(v); } template <class U> void operator+=(U v) { insert(v); } template <class F> auto erase(F v) { return s.erase(v); } template <class U> auto find(U v) { return s.find(v); } template <class U> auto lower_bound(U v) { return s.lower_bound(v); } template <class U> auto upper_bound(U v) { return s.upper_bound(v); } auto find_by_order(ll k) { auto it = s.begin(); for (ll i = 0; i < k; i++) it++; return it; } auto order_of_key(ll v) { auto it = s.begin(); ll i = 0; for (; it != s.end() && *it < v; i++) it++; return i; } }; #define pbds(T) my_pbds_tree<T> // gp_hash_tableでcountを使えないようにするため template <class T, class U> struct my_unordered_map { unordered_map<T, U> m; my_unordered_map(){}; auto begin() { return m.begin(); } auto end() { return m.end(); } auto cbegin() { return m.cbegin(); } auto cend() { return m.cend(); } template <class V> auto erase(V v) { return m.erase(v); } void clear() { m.clear(); } /*countは gp_hash_tableに存在しない*/ /*!= m.end()*/ template <class V> auto find(V v) { return m.find(v); } template <class V> auto &operator[](V n) { return m[n]; } }; #define unordered_map my_unordered_map #define umapi unordered_map<ll, ll> #define umapp unordered_map<P, ll> #define umapu unordered_map<uint64_t, ll> #define umapip unordered_map<ll, P> #else // umapはunorderd_mapになる // umapiはgp_hash_table // find_by_order(k) k番目のイテレーター // order_of_key(k) k以上が前から何番目か #define pbds(U) \ __gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag, \ __gnu_pbds::tree_order_statistics_node_update> #define umapi __gnu_pbds::gp_hash_table<ll, ll, xorshift> #define umapp __gnu_pbds::gp_hash_table<P, ll, xorshift> #define umapu __gnu_pbds::gp_hash_table<uint64_t, ll, xorshift> #define umapip __gnu_pbds::gp_hash_table<ll, P, xorshift> #endif /*@formatter:on*/ struct xorshift { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } size_t operator()(std::pair<ll, ll> x) const { ll v = ((x.first) << 32) | x.second; static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(v + FIXED_RANDOM); } }; /*@formatter:off*/ template <class U, class L> void operator+=( __gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update> &s, L v) { s.insert(v); } //衝突対策 #define ws ws_ template <class A, class B, class C> struct T2 { A f; B s; C t; T2() { f = 0, s = 0, t = 0; } T2(A f, B s, C t) : f(f), s(s), t(t) {} bool operator<(const T2 &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 ?n s > r.s : t > r.t; 大きい順 */ } bool operator>(const T2 &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 T2 &r) const { return f == r.f && s == r.s && t == r.t; } bool operator!=(const T2 &r) const { return f != r.f || s != r.s || t != r.t; } }; template <class A, class B, class C, class D> struct F2 { A a; B b; C c; D d; F2() { a = 0, b = 0, c = 0, d = 0; } F2(A a, B b, C c, D d) : a(a), b(b), c(c), d(d) {} bool operator<(const F2 &r) const { return a != r.a ? a < r.a : b != r.b ? b < r.b : c != r.c ? c < r.c : d < r.d; /* return a != r.a ? a > r.a : b != r.b ? b > r.b : c != r.c ? c > r.c : d > r.d;*/ } bool operator>(const F2 &r) const { return a != r.a ? a > r.a : b != r.b ? b > r.b : c != r.c ? c > r.c : d > r.d; /* return a != r.a ? a < r.a : b != r.b ? b < r.b : c != r.c ? c < r.c : d < r.d;*/ } bool operator==(const F2 &r) const { return a == r.a && b == r.b && c == r.c && d == r.d; } bool operator!=(const F2 &r) const { return a != r.a || b != r.b || c != r.c || d != r.d; } ll operator[](ll i) { assert(i < 4); return i == 0 ? a : i == 1 ? b : i == 2 ? c : d; } }; typedef T2<ll, ll, ll> T; typedef F2<ll, ll, ll, ll> F; T mt(ll a, ll b, ll c) { return T(a, b, c); } F mf(ll a, ll b, ll c, ll d) { return F(a, b, c, d); } //@マクロ省略系 型,構造 #define double long double #define pow powl using dou = double; const double eps = 1e-9; //基本コメントアウト /* struct epsdou { double v; epsdou(double v = 0) : v(v) {} template<class T> epsdou &operator+=(T b) { v += (double) b; return (*this); } template<class T> epsdou &operator-=(T b) { v -= (double) b; return (*this); } template<class T> epsdou &operator*=(T b) { v *= (double) b; return (*this); } template<class T> epsdou &operator/=(T b) { v /= (double) b; return (*this); } epsdou operator+(epsdou b) { return v + (double) b; } epsdou operator-(epsdou b) { return v - (double) b; } epsdou operator*(epsdou b) { return v * (double) b; } epsdou operator/(epsdou b) { return v / (double) b; } epsdou operator-() const { return epsdou(-v); } template<class T> bool operator<(T b) { return v < (double) b; } template<class T> bool operator>(T b) { return v > (double) b; } template<class T> bool operator==(T b) { return fabs(v - (double) b) <= eps; } template<class T> bool operator<=(T b) { return v < (double) b || fabs(v - b) <= eps; } template<class T> bool operator>=(T b) { return v > (double) b || fabs(v - b) <= eps; } operator double() { return v; }};istream &operator>>(istream &iss, epsdou &a) { iss >> a.v; return iss;}ostream &operator<<(ostream &os, epsdou &a) { os << a.v; return os;} #define eps_conr_t(o) template<class T> epsdou operator o(T b, epsdou a){return a.v o (dou)b;} #define eps_conl_t(o) template<class T> epsdou operator o(epsdou a, T b){return a.v o (dou)b;} eps_conl_t(+)eps_conl_t(-)eps_conl_t(*)eps_conl_t(/)eps_conr_t(+)eps_conr_t(-)eps_conr_t(*)eps_conr_t(/) #undef double #define double epsdou */ #define ull unsigned long long using itn = int; using str = string; using bo = bool; #define au auto using P = pair<ll, ll>; using mvp = mvec<P>; using mvt = mvec<T>; #define MIN(a) numeric_limits<a>::min() #define MAX(a) numeric_limits<a>::max() #define fi first #define se second #define beg begin #define rbeg rbegin #define con continue #define bre break #define brk break #define is == #define el else #define elf else if #define upd update #define sstream stringstream #define maxq 1 #define minq -1 #define ZERO(a) memset(a, 0, sizeof(a)) #define MINUS(a) memset(a, 0xff, sizeof(a)) #define MALLOC(type, len) (type *)malloc((len) * sizeof(type)) #define lam(right) [&](auto &p) { return p right; } //マクロ省略系 コンテナ 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 V vector #define vvt0(t) vector<vector<t>> #define vvt1(t, a) vector<vector<t>> a #define vvt2(t, a, b) vector<vector<t>> a(b) #define vvt3(t, a, b, c) vector<vector<t>> a(b, vector<t>(c)) #define vvt4(t, a, b, c, d) vector<vector<t>> a(b, vector<t>(c, d)) #define vvi(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(ll, __VA_ARGS__) #define vvb(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(bool, __VA_ARGS__) #define vvs(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(string, __VA_ARGS__) #define vvd(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(double, __VA_ARGS__) #define vvc(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(char, __VA_ARGS__) #define vvp(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(P, __VA_ARGS__) #define vvt(...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(T, __VA_ARGS__) #define vv(type, ...) \ over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(type, __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 vn(type, name, ...) auto name = make_v<type>(__VA_ARGS__) #define PQ priority_queue<ll, vector<ll>, greater<ll>> #define tos to_string using mapi = map<ll, ll>; using mapp = map<P, ll>; using mapd = map<dou, ll>; using mapc = map<char, ll>; using maps = map<str, ll>; using seti = set<ll>; using setd = set<dou>; using setc = set<char>; using sets = set<str>; using qui = queue<ll>; #define uset unordered_set #define useti unordered_set<ll, xorshift> #define mset multiset #define mseti multiset<ll> #define umap unordered_map #define mmap multimap //任意のマクロサポート用 使う度に初期化する int index_, v1_, v2_, v3_; template <class T> struct pq { priority_queue<T, vector<T>, greater<T>> q; /*小さい順*/ T su = 0; void clear() { q = priority_queue<T, vector<T>, greater<T>>(); su = 0; } void operator+=(T v) { su += v; q.push(v); } T sum() { return su; } T top() { return q.top(); } void pop() { su -= q.top(); q.pop(); } T poll() { T ret = q.top(); su -= ret; q.pop(); return ret; } ll size() { return q.size(); } }; template <class T> struct pqg { priority_queue<T> q; /*大きい順*/ T su = 0; void clear() { q = priority_queue<T>(); su = 0; } void operator+=(T v) { su += v; q.push(v); } T sum() { return su; } T top() { return q.top(); } void pop() { su -= q.top(); q.pop(); } T poll() { T ret = q.top(); su -= ret; q.pop(); return ret; } ll size() { return q.size(); } }; #define pqi pq<ll> #define pqgi pqg<ll> //マクロ 繰り返し //↓@オーバーロード隔離 #define rep1(n) for (ll rep1i = 0, rep1lim = n; rep1i < rep1lim; ++rep1i) #define rep2(i, n) for (ll i = 0, rep2lim = n; i < rep2lim; ++i) #define rep3(i, m, n) for (ll i = m, rep3lim = n; i < rep3lim; ++i) #define rep4(i, m, n, ad) for (ll i = m, rep4lim = n; i < rep4lim; i += ad) //逆順 閉区間 #define rer2(i, n) for (ll i = n; i >= 0; i--) #define rer3(i, m, n) for (ll i = m, rer3lim = n; i >= rer3lim; i--) #define rer4(i, m, n, dec) for (ll i = m, rer4lim = n; i >= rer4lim; i -= dec) //ループを一つにまとめないとフォーマットで汚くなるため #define nex_ind1(i) i++ #define nex_ind2(i, j, J) \ i = (j + 1 == J) ? i + 1 : i, j = (j + 1 == J ? 0 : j + 1) #define nex_ind3(i, j, k, J, K) \ i = (j + 1 == J && k + 1 == K) ? i + 1 : i, \ j = (k + 1 == K) ? (j + 1 == J ? 0 : j + 1) : j, \ k = (k + 1 == K ? 0 : k + 1) #define nex_ind4(i, j, k, l, J, K, L) \ i = (j + 1 == J && k + 1 == K && l + 1 == L) ? i + 1 : i, \ j = (k + 1 == K && l + 1 == L) ? (j + 1 == J ? 0 : j + 1) : j, \ k = (l + 1 == L ? (k + 1 == K ? 0 : k + 1) : k), l = l + 1 == L ? 0 : l + 1 #define nex_ind5(i, j, k, l, m, J, K, L, M) \ i = (j + 1 == J && k + 1 == K && l + 1 == L && m + 1 == M) ? i + 1 : i, \ j = (k + 1 == K && l + 1 == L && m + 1 == M) ? (j + 1 == J ? 0 : j + 1) : j, \ k = (l + 1 == L && m + 1 == M ? (k + 1 == K ? 0 : k + 1) : k), \ l = m + 1 == M ? l + 1 == L ? 0 : l + 1 : l, m = m + 1 == M ? 0 : m + 1 #define repss2(i, I) for (int i = 0; i < I; i++) #define repss4(i, j, I, J) \ for (int i = (J ? 0 : I), j = 0; i < I; nex_ind2(i, j, J)) #define repss6(i, j, k, I, J, K) \ for (int i = (J && K ? 0 : I), j = 0, k = 0; i < I; nex_ind3(i, j, k, J, K)) #define repss8(i, j, k, l, I, J, K, L) \ for (int i = (J && K && L ? 0 : I), j = 0, k = 0, l = 0; i < I; \ nex_ind4(i, j, k, l, J, K, L)) #define repss10(i, j, k, l, m, I, J, K, L, M) \ for (int i = (J && K && L && M ? 0 : I), j = 0, k = 0, l = 0, m = 0; i < I; \ nex_ind5(i, j, k, l, m, J, K, L, M)) // i,j,k...をnまで見る #define reps2(i, n) repss2(i, n) #define reps3(i, j, n) repss4(i, j, n, n) #define reps4(i, j, k, n) repss6(i, j, k, n, n, n) #define reps5(i, j, k, l, n) repss8(i, j, k, l, n, n, n, n) template <class T> void nex_repv2(int &i, int &j, int &I, int &J, vector<vector<T>> &s) { while (1) { j++; if (j >= J) { j = 0; i++; if (i < I) { J = (int)s[i].size(); } } if (i >= I || J) return; } } template <class T> void nex_repv3(int &i, int &j, int &k, int &I, int &J, int &K, vector<vector<vector<T>>> &s) { while (1) { k++; if (k >= K) { k = 0; j++; if (j >= J) { j = 0; i++; if (i >= I) return; } } J = (int)s[i].size(); K = (int)s[i][j].size(); if (J && K) return; } } #define repv_2(i, a) repss2(i, sz(a)) //正方形である必要はない //直前を持つのとどっちが早いか #define repv_3(i, j, a) \ for (int I = (int)a.size(), J = (int)a[0].size(), i = 0, j = 0; i < I; \ nex_repv2(i, j, I, J, a)) //箱状になっている事が要求される つまり[i] 次元目の要素数は一定 #define repv_4(i, j, k, a) \ for (int I = (int)a.size(), J = (int)a[0].size(), K = (int)a[0][0].size(), \ i = 0, j = 0, k = 0; \ i < I; nex_repv3(i, j, k, I, J, K, a)) #define repv_5(i, j, k, l, a) \ repss8(i, j, k, l, sz(a), sz(a[0]), sz(a[0][0]), sz(a[0][0][0])) #define repv_6(i, j, k, l, m, a) \ repss10(i, j, k, l, m, sz(a), sz(a[0]), sz(a[0][0]), sz(a[0][0][0]), \ sz(a[0][0][0][0])) template <typename T> struct has_rbegin_rend { private: template <typename U> static auto check(U &&obj) -> decltype(std::rbegin(obj), std::rend(obj), std::true_type{}); static std::false_type check(...); public: static constexpr bool value = decltype(check(std::declval<T>()))::value; }; template <typename T> constexpr bool has_rbegin_rend_v = has_rbegin_rend<T>::value; template <typename Iterator> class Range { public: Range(Iterator &&begin, Iterator &&end) noexcept : m_begin(std::forward<Iterator>(begin)), m_end(std::forward<Iterator>(end)) {} Iterator begin() const noexcept { return m_begin; } Iterator end() const noexcept { return m_end; } private: const Iterator m_begin; const Iterator m_end; }; template <typename Iterator> static inline Range<Iterator> makeRange(Iterator &&begin, Iterator &&end) noexcept { return Range<Iterator>{std::forward<Iterator>(begin), std::forward<Iterator>(end)}; } template <typename T> static inline decltype(auto) makeReversedRange(const std::initializer_list<T> &iniList) noexcept { return makeRange(std::rbegin(iniList), std::rend(iniList)); } template <typename T, typename std::enable_if_t<has_rbegin_rend_v<T>, std::nullptr_t> = nullptr> static inline decltype(auto) makeReversedRange(T &&c) noexcept { return makeRange(std::rbegin(c), std::rend(c)); } /* rbegin(), rend()を持たないものはこっちに分岐させて,エラーメッセージを少なくする*/ template <typename T, typename std::enable_if<!has_rbegin_rend<T>::value, std::nullptr_t>::type = nullptr> static inline void makeReversedRange(T &&) noexcept { static_assert(has_rbegin_rend<T>::value, "Specified argument doesn't have reverse iterator."); } #define form1(st) \ for (auto &&form_it = st.begin(); form_it != st.end(); ++form_it) #define form3(k, v, st) \ for (auto &&form_it = st.begin(); form_it != st.end(); ++form_it) #define form4(k, v, st, r) \ for (auto &&form_it = st.begin(); form_it != st.end() && (*form_it).fi < r; \ ++form_it) #define form5(k, v, st, l, r) \ for (auto &&form_it = st.lower_bound(l); \ form_it != st.end() && (*form_it).fi < r; ++form_it) #define fors1(st) \ for (auto &&fors_it = st.begin(); fors_it != st.end(); ++fors_it) #define fors2(v, st) \ for (auto &&fors_it = st.begin(); fors_it != st.end(); ++fors_it) #define fors3(v, st, r) \ for (auto &&fors_it = st.begin(); fors_it != st.end() && (*fors_it) < r; \ ++fors_it) #define fors4(v, st, l, r) \ for (auto &&fors_it = st.lower_bound(l); \ fors_it != st.end() && (*fors_it) < r; ++fors_it) #define forslr3(st, a, b) \ for (auto &&forslr_it = st.begin(); forslr_it != st.end(); ++forslr_it) #define forslr4(v, st, a, b) \ for (auto &&forslr_it = st.begin(); forslr_it != st.end(); ++forslr_it) #define forslr5(v, st, r, a, b) \ for (auto &&forslr_it = st.begin(); \ forslr_it != st.end() && (*forslr_it) < r; ++forslr_it) #define forslr6(v, st, l, r, a, b) \ for (auto &&forslr_it = st.lower_bound(l); \ forslr_it != st.end() && (*forslr_it) < r; ++forslr_it) template <class U> vector<U> to1d(vector<U> &a) { return a; } template <class U> vector<U> to1d(V<V<U>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) res.push_back(a2); return res; } template <class U> vector<U> to1d(V<V<V<U>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) res.push_back(a3); return res; } template <class U> vector<U> to1d(V<V<V<V<U>>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) for (auto &&a4 : a3) res.push_back(a4); return res; } template <class U> vector<U> to1d(V<V<V<V<V<U>>>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) for (auto &&a4 : a3) for (auto &&a5 : a4) res.push_back(a5); return res; } template <class U> vector<U> to1d(V<V<V<V<V<V<U>>>>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) for (auto &&a4 : a3) for (auto &&a5 : a4) for (auto &&a6 : a5) res.push_back(a6); return res; } #define fora_init_2(a, A) ; #define fora_init_4(a, b, A, B) \ auto &&a = A[fora_i]; \ auto &&b = B[fora_i]; #define fora_init_6(a, b, c, A, B, C) \ auto &&a = A[fora_i]; \ auto &&b = B[fora_i]; \ auto &&c = C[fora_i]; #define fora_init_8(a, b, c, d, A, B, C, D) \ auto &&a = A[fora_i]; \ auto &&b = B[fora_i]; \ auto &&c = C[fora_i]; \ auto &&d = D[fora_i]; #define fora_init(...) \ over8(__VA_ARGS__, fora_init_8, a, fora_init_6, a, fora_init_4, a, \ fora_init_2)(__VA_ARGS__) #define forr_init_2(a, A) auto &&a = A[forr_i]; #define forr_init_4(a, b, A, B) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; #define forr_init_6(a, b, c, A, B, C) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; \ auto &&c = C[forr_i]; #define forr_init_8(a, b, c, d, A, B, C, D) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; \ auto &&c = C[forr_i]; \ auto &&d = D[forr_i]; #define forr_init(...) \ over8(__VA_ARGS__, forr_init_8, a, forr_init_6, a, forr_init_4, a, \ forr_init_2)(__VA_ARGS__) #define forp_init(k, v, ...) \ auto &&k = (*forp_it).fi; \ auto &&v = (*forp_it).se; #define form_init(k, v, ...) \ auto &&k = (*form_it).fi; \ auto &&v = (*form_it).se; #define fors_init(v, ...) auto &&v = (*fors_it); #define forlr_init(a, A, ngl, ngr) \ auto a = A[forlr_i]; \ auto prev = forlr_i ? A[forlr_i - 1] : ngl; \ auto next = forlr_i + 1 < rep2lim ? A[forlr_i + 1] : ngr; #define forslr_init4(a, A, ngl, ngr) \ auto a = (*forslr_it); \ auto prev = (forslr_it != A.begin()) ? (*std::prev(forslr_it)) : ngl; \ auto next = (forslr_it != std::prev(A.end())) ? (*std::next(forslr_it)) : ngr; #define forslr_init5(a, A, r, ngl, ngr) \ auto a = (*forslr_it); \ auto prev = (forslr_it != A.begin()) ? (*std::prev(forslr_it)) : ngl; \ auto next = (forslr_it != std::prev(A.end())) ? (*std::next(forslr_it)) : ngr; #define forslr_init6(a, A, l, r, ngl, ngr) \ auto a = (*forslr_it); \ auto prev = (forslr_it != A.begin()) ? (*std::prev(forslr_it)) : ngl; \ auto next = (forslr_it != std::prev(A.end())) ? (*std::next(forslr_it)) : ngr; #define forslr_init(...) \ over6(__VA_ARGS__, forslr_init6, forslr_init5, forslr_init4)(__VA_ARGS__); #define fora_2(a, A) for (auto &&a : A) #define fora_4(a, b, A, B) rep(fora_i, sz(A)) #define fora_6(a, b, c, A, B, C) rep(fora_i, sz(A)) #define fora_8(a, b, c, d, A, B, C, D) rep(fora_i, sz(A)) #define forr_2(a, A) rer(forr_i, sz(A) - 1) #define forr_4(a, b, A, B) rer(forr_i, sz(A) - 1) #define forr_6(a, b, c, A, B, C) rer(forr_i, sz(A) - 1) #define forr_8(a, b, c, d, A, B, C, D) rer(forr_i, sz(A) - 1) //↑@オーバーロード隔離 // rep系はインデックス、for系は中身 #define rep(...) over4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__) #define rer(...) over4(__VA_ARGS__, rer4, rer3, rer2, )(__VA_ARGS__) // char用のrep #define repc(i, m, n) for (char i = m, repc3lim = n; i < repc3lim; ++i) // i,j,k...をnまで見る #define reps(...) over5(__VA_ARGS__, reps5, reps4, reps3, reps2, )(__VA_ARGS__) #define repss(...) \ over10(__VA_ARGS__, repss10, a, repss8, a, repss6, a, repss4, a, \ repss2)(__VA_ARGS__) // vectorのindexを走査する // repv(i,j,vvi) #define repv(...) \ over6(__VA_ARGS__, repv_6, repv_5, repv_4, repv_3, repv_2, )(__VA_ARGS__) #define rerv(i, A) for (int i = sz(A) - 1; i >= 0; i--) // repvn(dp) nは次元 #define repv1(a) repv(i, a) #define repv2(a) repv(i, j, a) #define repv3(a) repv(i, j, k, a) #define repv4(a) repv(i, j, k, l, a) #define fora(...) \ over8(__VA_ARGS__, fora_8, a, fora_6, a, fora_4, a, fora_2)(__VA_ARGS__) #define forr(...) \ over8(__VA_ARGS__, forr_8, a, forr_6, a, forr_4, a, forr_2)(__VA_ARGS__) //#define forr(v, a) for(auto&& v : makeReversedRange(a)) //参照を取らない #define forv(a, b) for (auto a : to1d(b)) //インデックスを前後含めて走査 #define ring(i, s, len) \ for (int i = s, prev = (s == 0) ? len - 1 : s - 1, \ next = (s == len - 1) ? 0 : s + 1, cou = 0; \ cou < len; \ cou++, prev = i, i = next, next = (next == len - 1) ? 0 : next + 1) //値と前後を見る #define ringv(v, d) \ index_ = 0; \ for (auto prev = d[sz(d) - 1], next = (int)d.size() > 1 ? d[1] : d[0], \ v = d[0]; \ index_ < sz(d); index_++, prev = v, v = next, \ next = (index_ >= sz(d) - 1 ? d[0] : d[index_ + 1])) // 左右をnext prevで見る 0の左と nの右 #define forlr(v, d, banpei_l, banpei_r) rep(forlr_i, sz(d)) #define form(...) \ over5(__VA_ARGS__, form5, form4, form3, form2, form1)(__VA_ARGS__) #define fors(...) over4(__VA_ARGS__, fors4, fors3, fors2, fors1)(__VA_ARGS__) #define forslr(...) \ over6(__VA_ARGS__, forslr6, forslr5, forslr4, forslr3)(__VA_ARGS__) #define forp(k, v, st) \ for (auto &&forp_it = st.begin(); forp_it != st.end(); ++forp_it) //マクロ 定数 #define k3 1010 #define k4 10101 #define k5 101010 #define k6 1010101 #define k7 10101010 const ll inf = (ll)1e9 + 100; const ll linf = (ll)1e18 + 100; const dou dinf = (dou)linf * linf; const char infc = '{'; const string infs = "{"; const double PI = 3.1415926535897932384626433832795029L; //マクロ省略形 関数等 #define arsz(a) (sizeof(a) / sizeof(a[0])) #define sz(a) ((ll)(a).size()) #define mp make_pair #define pb pop_back #define pf push_front #define eb emplace_back #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() constexpr bool ev(ll a) { return !(a & 1); } constexpr bool od(ll a) { return (a & 1); } //@拡張系 こう出来るべきというもの //埋め込み 存在を意識せずに機能を増やされているもの namespace std { template <> class hash<std::pair<signed, signed>> { public: size_t operator()(const std::pair<signed, signed> &x) const { return hash<ll>()(((ll)x.first << 32) | x.second); } }; template <> class hash<std::pair<ll, ll>> { public : /*大きいllが渡されると、<<32でオーバーフローするがとりあえず問題ないと判断*/ size_t operator()(const std::pair<ll, ll> &x) const { return hash<ll>()(((ll)x.first << 32) | x.second); } }; } // namespace std // stream まとめ /*@formatter:on*/ istream &operator>>(istream &iss, P &a) { iss >> a.first >> a.second; return iss; } template <typename T> istream &operator>>(istream &iss, vector<T> &vec) { for (T &x : vec) iss >> x; return iss; } template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> p) { os << p.fi << " " << p.se; return os; } ostream &operator<<(ostream &os, T p) { os << p.f << " " << p.s << " " << p.t; return os; } ostream &operator<<(ostream &os, F p) { os << p.a << " " << p.b << " " << p.c << " " << p.d; return os; } template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) { for (ll i = 0; i < vec.size(); ++i) os << vec[i] << (i + 1 == vec.size() ? "" : " "); return os; } template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) { for (ll i = 0; i < vec.size(); ++i) { for (ll j = 0; j < vec[i].size(); ++j) { os << vec[i][j] << " "; } os << endl; } return os; } template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &m) { for (auto &&v : m) os << v; return os; } template <class T> ostream &operator<<(ostream &os, set<T> s) { fora(v, s) { os << v << " "; } return os; } template <class T> ostream &operator<<(ostream &os, deque<T> a) { fora(v, a) os << v << " "; return os; } ostream &operator<<(ostream &os, vector<vector<char>> &vec) { rep(h, sz(vec)) { rep(w, sz(vec[0])) { os << vec[h][w]; } os << endl; } return os; } // template<class T,class U>ostream &operator<<(ostream &os, vector<pair<T,U>>& // a) {fora(v,a)os<<v<<endl;return os;} /*@formatter:off*/ template <typename W, typename H> void resize(W &vec, const H head) { vec.resize(head); } template <typename W, typename H, typename... T> void resize(W &vec, const H &head, const T... tail) { vec.resize(head); for (auto &v : vec) resize(v, tail...); } // 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); }); } //削除した後のvectorを返す template <typename T, typename F> vector<T> erase_if2(vector<T> &v, F f) { vector<T> nv; rep(i, sz(v)) { if (!f(v[i])) { nv.push_back(v[i]); } } return nv; } template <typename T, typename F> vector<vector<T>> erase_if2(vector<vector<T>> &v, F f) { vector<vector<T>> res; rep(i, sz(v)) { res[i] = erase_if2(v[i], f); } return res; } // all_of(A, %2); // all_of(A, a, a%2); #define all_of__2(a, right) all_of2(a, lam(right)) #define all_of__3(a, v, siki) all_of2(a, [&](auto v) { return siki; }) #define all_of(...) over3(__VA_ARGS__, all_of__3, all_of__2)(__VA_ARGS__) #define all_of_f(a, f) all_of2(a, f) #define any_of__2(a, right) any_of2(a, lam(right)) #define any_of__3(a, v, siki) any_of2(a, [&](auto v) { return siki; }) #define any_of(...) over3(__VA_ARGS__, any_of__3, any_of__2)(__VA_ARGS__) #define any_of_f(a, f) any_of2(a, f) #define none_of__2(a, right) none_of2(a, lam(right)) #define none_of__3(a, v, siki) none_of2(a, [&](auto v) { return siki; }) #define none_of(...) over3(__VA_ARGS__, none_of__3, none_of__2)(__VA_ARGS__) #define none_of_f(a, f) none_of2(a, f) #define find_if__2(a, right) find_if2(a, lam(right)) #define find_if__3(a, v, siki) find_if2(a, [&](auto v) { return siki; }) #define find_if(...) over3(__VA_ARGS__, find_if__3, find_if__2)(__VA_ARGS__) #define find_if_f(a, f) find_if2(a, f) #define rfind_if__2(a, right) rfind_if2(a, lam(right)) #define rfind_if__3(a, v, siki) rfind_if2(a, [&](auto v) { return siki; }) #define rfind_if(...) over3(__VA_ARGS__, rfind_if__3, rfind_if__2)(__VA_ARGS__) #define rfind_if_f(a, f) rfind_if2(a, f) #define contains_if__2(a, right) contains_if2(a, lam(right)) #define contains_if__3(a, v, siki) contains_if2(a, [&](auto v) { return siki; }) #define contains_if(...) \ over3(__VA_ARGS__, contains_if__3, contains_if__2)(__VA_ARGS__) #define contains_if_f(a, f) contains_if2(a, f) #define count_if__2(a, right) count_if2(a, lam(right)) #define count_if__3(a, v, siki) count_if2(a, [&](auto v) { return siki; }) #define count_if(...) over3(__VA_ARGS__, count_if__3, count_if__2)(__VA_ARGS__) #define count_if_f(a, f) count_if2(a, f) #define for_each__2(a, right) \ do { \ fora(v, a) { v right; } \ } while (0) #define for_each__3(a, v, siki) \ do { \ fora(v, a) { siki; } \ } while (0) #define for_each(...) over3(__VA_ARGS__, for_each__3, for_each__2)(__VA_ARGS__) #define for_each_f(a, f) \ do { \ fora(v, a) { f(v); } \ } while (0) #define erase_if__2(a, right) erase_if2(a, lam(right)) #define erase_if__3(a, v, siki) erase_if2(a, [&](auto v) { return siki; }) #define erase_if(...) over3(__VA_ARGS__, erase_if__3, erase_if__2)(__VA_ARGS__) #define erase_if_f(a, f) erase_if2(a, f) #define entry_if__2(a, right) erase_if2(a, [&](auto v) { return !(v right); }) #define entry_if__3(a, v, siki) erase_if2(a, [&](auto v) { return !(siki); }) #define entry_if(...) over3(__VA_ARGS__, entry_if__3, entry_if__2)(__VA_ARGS__) #define entry_if_f(a, f) erase_if2(a, [&](auto v) { return !f(v); }) template <class T, class U, class W> void replace(vector<W> &a, T key, U v) { rep(i, sz(a)) if (a[i] == key) a[i] = v; } template <class T, class U, class W> void replace(vector<vector<W>> &A, T key, U v) { rep(i, sz(A)) replace(A[i], key, v); } void replace(str &a, char key, str v) { if (v == "") a.erase(remove(all(a), key), a.end()); } void replace(str &a, char key, char v) { replace(all(a), key, v); } // keyと同じかどうか01で置き換える template <class T, class U> void replace(vector<T> &a, U k) { rep(i, sz(a)) a[i] = a[i] == k; } template <class T, class U> void replace(vector<vector<T>> &a, U k) { rep(i, sz(a)) rep(j, sz(a[0])) a[i][j] = a[i][j] == k; } template <class T> void replace(T &a) { replace(a, '#'); } void replace(str &a, str key, str v) { stringstream t; ll kn = sz(key); std::string::size_type Pos(a.find(key)); ll l = 0; while (Pos != std::string::npos) { t << a.substr(l, Pos - l); t << v; l = Pos + kn; Pos = a.find(key, Pos + kn); } t << a.substr(l, sz(a) - l); a = t.str(); } template <class T> bool includes(vector<T> &a, vector<T> &b) { vi c = a; vi d = b; sort(all(c)); sort(all(d)); return includes(all(c), all(d)); } template <class T> bool is_permutation(vector<T> &a, vector<T> &b) { return is_permutation(all(a), all(b)); } template <class T> bool next_permutation(vector<T> &a) { return next_permutation(all(a)); } void iota(vector<ll> &ve, ll s, ll n) { ve.resize(n); iota(all(ve), s); } vi iota(ll s, ll len) { vi ve(len); iota(all(ve), s); return ve; } template <class A, class B> auto vtop(vector<A> &a, vector<B> &b) { assert(sz(a) == sz(b)); /*stringを0で初期化できない */ vector<pair<A, B>> res; rep(i, sz(a)) res.eb(a[i], b[i]); return res; } template <class A, class B> void ptov(vector<pair<A, B>> &p, vector<A> &a, vector<B> &b) { a.resize(sz(p)), b.resize(sz(p)); rep(i, sz(p)) a[i] = p[i].fi, b[i] = p[i].se; } template <class A, class B, class C> auto vtot(vector<A> &a, vector<B> &b, vector<C> &c) { assert(sz(a) == sz(b) && sz(b) == sz(c)); vector<T2<A, B, C>> res; rep(i, sz(a)) res.eb(a[i], b[i], c[i]); return res; } template <class A, class B, class C, class D> auto vtof(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { assert(sz(a) == sz(b) && sz(b) == sz(c) && sz(c) == sz(d)); vector<F2<A, B, C, D>> res; rep(i, sz(a)) res.eb(a[i], b[i], c[i], d[i]); return res; } enum pcomparator { fisi, fisd, fdsi, fdsd, sifi, sifd, sdfi, sdfd }; enum tcomparator { fisiti, fisitd, fisdti, fisdtd, fdsiti, fdsitd, fdsdti, fdsdtd, fitisi, fitisd, fitdsi, fitdsd, fdtisi, fdtisd, fdtdsi, fdtdsd, sifiti, sifitd, sifdti, sifdtd, sdfiti, sdfitd, sdfdti, sdfdtd, sitifi, sitifd, sitdfi, sitdfd, sdtifi, sdtifd, sdtdfi, sdfdfd, tifisi, tifisd, tifdsi, tifdsd, tdfisi, tdfisd, tdfdsi, tdfdsd, tisifi, tisifd, tisdfi, tisdfd, tdsifi, tdsifd, tdsdfi, tdsdfd }; template <class A, class B> void sort(vector<pair<A, B>> &a, pcomparator type) { typedef pair<A, B> U; if (type == fisi) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se < r.se; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se > r.se; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se < r.se; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se > r.se; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi < r.fi; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi > r.fi; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi < r.fi; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi > r.fi; }); }; template <class U> void sort(vector<U> &a, pcomparator type) { if (type == fisi) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f > r.f; }); }; template <class A, class B, class C, class D> void sort(vector<F2<A, B, C, D>> &a, pcomparator type) { typedef F2<A, B, C, D> U; if (type == fisi) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a > r.a; }); }; template <class U> void sort(vector<U> &a, tcomparator type) { if (type == 0) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t < r.t; }); else if (type == 1) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t > r.t; }); else if (type == 2) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t < r.t; }); else if (type == 3) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t > r.t; }); else if (type == 4) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t < r.t; }); else if (type == 5) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t > r.t; }); else if (type == 6) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t < r.t; }); else if (type == 7) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t > r.t; }); else if (type == 8) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s < r.s; }); else if (type == 9) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s > r.s; }); else if (type == 10) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s < r.s; }); else if (type == 11) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s > r.s; }); else if (type == 12) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s < r.s; }); else if (type == 13) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s > r.s; }); else if (type == 14) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s < r.s; }); else if (type == 15) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s > r.s; }); else if (type == 16) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t < r.t; }); else if (type == 17) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t > r.t; }); else if (type == 18) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t < r.t; }); else if (type == 19) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t > r.t; }); else if (type == 20) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t < r.t; }); else if (type == 21) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t > r.t; }); else if (type == 22) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t < r.t; }); else if (type == 23) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t > r.t; }); else if (type == 24) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f < r.f; }); else if (type == 25) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f > r.f; }); else if (type == 26) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f < r.f; }); else if (type == 27) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f > r.f; }); else if (type == 28) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f < r.f; }); else if (type == 29) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f > r.f; }); else if (type == 30) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f < r.f; }); else if (type == 31) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f > r.f; }); else if (type == 32) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == 33) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == 34) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == 35) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == 36) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == 37) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == 38) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == 39) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == 40) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == 41) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == 42) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == 43) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f > r.f; }); else if (type == 44) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == 45) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == 46) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == 47) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f > r.f; }); } template <class A, class B, class C, class D> void sort(vector<F2<A, B, C, D>> &a, tcomparator type) { typedef F2<A, B, C, D> U; if (type == 0) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c < r.c; }); else if (type == 1) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c > r.c; }); else if (type == 2) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c < r.c; }); else if (type == 3) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c > r.c; }); else if (type == 4) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c < r.c; }); else if (type == 5) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c > r.c; }); else if (type == 6) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c < r.c; }); else if (type == 7) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c > r.c; }); else if (type == 8) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b < r.b; }); else if (type == 9) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b > r.b; }); else if (type == 10) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b < r.b; }); else if (type == 11) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b > r.b; }); else if (type == 12) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b < r.b; }); else if (type == 13) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b > r.b; }); else if (type == 14) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b < r.b; }); else if (type == 15) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b > r.b; }); else if (type == 16) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c < r.c; }); else if (type == 17) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c > r.c; }); else if (type == 18) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c < r.c; }); else if (type == 19) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c > r.c; }); else if (type == 20) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c < r.c; }); else if (type == 21) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c > r.c; }); else if (type == 22) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c < r.c; }); else if (type == 23) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c > r.c; }); else if (type == 24) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a < r.a; }); else if (type == 25) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a > r.a; }); else if (type == 26) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a < r.a; }); else if (type == 27) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a > r.a; }); else if (type == 28) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a < r.a; }); else if (type == 29) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a > r.a; }); else if (type == 30) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a < r.a; }); else if (type == 31) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a > r.a; }); else if (type == 32) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == 33) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == 34) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == 35) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == 36) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == 37) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == 38) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == 39) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == 40) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == 41) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == 42) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == 43) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a > r.a; }); else if (type == 44) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == 45) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == 46) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == 47) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a > r.a; }); } void sort(string &a) { sort(all(a)); } void sort(int &a, int &b) { if (a > b) swap(a, b); } void sort(int &a, int &b, int &c) { sort(a, b); sort(a, c); sort(b, c); } void rsort(int &a, int &b) { if (a < b) swap(a, b); } void rsort(int &a, int &b, int &c) { rsort(a, b); rsort(a, c); rsort(b, c); } template <class T> void sort(vector<T> &a) { sort(all(a)); } // P l, P rで f(P) の形で渡す template <class U, class F> void sort(vector<U> &a, F f) { sort(all(a), [&](U l, U r) { return f(l) < f(r); }); }; template <class T> void rsort(vector<T> &a) { sort(all(a), greater<T>()); }; template <class U, class F> void rsort(vector<U> &a, F f) { sort(all(a), [&](U l, U r) { return f(l) > f(r); }); }; // F = T<T> //例えばreturn p.fi + p.se; template <class A, class B> void sortp(vector<A> &a, vector<B> &b) { auto c = vtop(a, b); sort(c); rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se; } template <class A, class B, class F> void sortp(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); sort(c, f); rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se; } template <class A, class B> void rsortp(vector<A> &a, vector<B> &b) { auto c = vtop(a, b); rsort(c); rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second; } template <class A, class B, class F> void rsortp(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); rsort(c, f); rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second; } template <class A, class B, class C> void sortt(vector<A> &a, vector<B> &b, vector<C> &c) { auto d = vtot(a, b, c); sort(d); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class F> void sortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); sort(d, f); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C> void rsortt(vector<A> &a, vector<B> &b, vector<C> &c) { auto d = vtot(a, b, c); rsort(d); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class F> void rsortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); rsort(d, f); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class D> void sortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { auto e = vtof(a, b, c, d); sort(e); rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d; } template <class A, class B, class C, class D> void rsortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { auto e = vtof(a, b, c, d); rsort(e); rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d; } // sortindex 元のvectorはソートしない template <class T> vi sorti(vector<T> &a) { auto b = a; vi ind = iota(0, sz(a)); sortp(b, ind); return ind; } /*indexの分で型が変わるためpcomparatorが必要*/ template <class T> vi sorti(vector<T> &a, pcomparator f) { auto b = a; vi ind = iota(0, sz(a)); sortp(b, ind, f); return ind; } template <class T, class F> vi sorti(vector<T> &a, F f) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(a[x]) < f(a[y]); }); return ind; } template <class T> vi rsorti(vector<T> &a) { auto b = a; vi ind = iota(0, sz(a)); rsortp(b, ind); return ind; } template <class T, class F> vi rsorti(vector<T> &a, F f) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(a[x]) > f(a[y]); }); return ind; } template <class A, class B, class F> vi sortpi(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(c[x]) < f(c[y]); }); return ind; } template <class A, class B> vi sortpi(vector<A> &a, vector<B> &b, pcomparator f) { vi ind = iota(0, sz(a)); auto c = a; auto d = b; sortt(c, d, ind, f); return ind; } template <class A, class B> vi sortpi(vector<A> &a, vector<B> &b) { return sortpi(a, b, fisi); }; template <class A, class B, class F> vi rsortpi(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(c[x]) > f(c[y]); }); return ind; } template <class A, class B> vi rsortpi(vector<A> &a, vector<B> &b) { return sortpi(a, b, fdsd); }; template <class A, class B, class C, class F> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(d[x]) < f(d[y]); }); return ind; } template <class A, class B, class C> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, pcomparator f) { vi ind = iota(0, sz(a)); auto d = vtof(a, b, c, ind); sort(d, f); rep(i, sz(a)) ind[i] = d[i].d; return ind; } template <class A, class B, class C> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { if (a[x] == a[y]) { if (b[x] == b[y]) return c[x] < c[y]; else return b[x] < b[y]; } else { return a[x] < a[y]; } }); return ind; } template <class A, class B, class C, class F> vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(d[x]) > f(d[y]); }); return ind; } template <class A, class B, class C> vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { if (a[x] == a[y]) { if (b[x] == b[y]) return c[x] > c[y]; else return b[x] > b[y]; } else { return a[x] > a[y]; } }); return ind; } template <class T> void sort2(vector<vector<T>> &a) { for (ll i = 0, n = a.size(); i < n; ++i) sort(a[i]); } template <class T> void rsort2(vector<vector<T>> &a) { for (ll i = 0, n = a.size(); i < n; ++i) rsort(a[i]); } template <class... T, class U> auto sorted(U head, T... a) { sort(head, a...); return head; } 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 W, typename T> void fill(W &xx, const T vall) { xx = vall; } template <typename W, typename T> void fill(vector<W> &vecc, const T vall) { for (auto &&vx : vecc) fill(vx, vall); } template <typename W, typename T> void fill(vector<W> &xx, ll len, const T v) { rep(i, len) xx[i] = v; } template <typename W, typename T> void fill(vector<vector<W>> &xx, int sh, int th, int sw, int tw, T v) { rep(h, sh, th) rep(w, sw, tw) xx[h][w] = v; } template <class T, class U> void fill(vector<T> &a, vi &ind, U val) { fora(v, ind) a[v] = val; } template <class W, class T> void fill(mvec<W> &xx, const T v) { fora(x, xx) fill(x, v); } template <typename A, size_t N> A sum(A (&a)[N]) { A res = 0; rep(i, N) res += a[i]; return res; } template <typename A, size_t N, size_t O> A sum(A (&a)[N][O]) { A res = 0; rep(i, N) rep(j, O) res += a[i][j]; return res; } template <typename A, size_t N, size_t O, size_t P> A sum(A (&a)[N][O][P]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) res += a[i][j][k]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A sum(A (&a)[N][O][P][Q]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) res += a[i][j][k][l]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A sum(A (&a)[N][O][P][Q][R]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) res += a[i][j][k][l][m]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A sum(A (&a)[N][O][P][Q][R][S]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) rep(n, S) res += a[i][j][k][l][m][n]; return res; } //@汎用便利関数 入力 ll in() { ll ret; cin >> ret; return ret; } string sin() { string ret; cin >> ret; return ret; } template <class T> void in(T &head) { cin >> head; } template <class T, class... U> void in(T &head, U &...tail) { cin >> head; in(tail...); } #define din1(a) \ ll a; \ cin >> a #define din2(a, b) \ ll a, b; \ cin >> a >> b #define din3(a, b, c) \ ll a, b, c; \ cin >> a >> b >> c #define din4(a, b, c, d) \ ll a, b, c, d; \ cin >> a >> b >> c >> d #define din5(a, b, c, d, e) \ ll a, b, c, d, e; \ cin >> a >> b >> c >> d >> e #define din6(a, b, c, d, e, f) \ ll a, b, c, d, e, f; \ cin >> a >> b >> c >> d >> e >> f #define din(...) \ over6(__VA_ARGS__, din6, din5, din4, din3, din2, din1)(__VA_ARGS__) #define dins1(a) \ str a; \ cin >> a #define dins2(a, b) \ str a, b; \ cin >> a >> b #define dins3(a, b, c) \ str a, b, c; \ cin >> a >> b >> c #define dins4(a, b, c, d) \ str a, b, c, d; \ cin >> a >> b >> c >> d #define dins5(a, b, c, d, e) \ str a, b, c, d, e; \ cin >> a >> b >> c >> d >> e #define dins6(a, b, c, d, e, f) \ str a, b, c, d, e, f; \ cin >> a >> b >> c >> d >> e >> f #define dins(...) \ over6(__VA_ARGS__, dins6, dins5, dins4, dins3, dins2, dins1)(__VA_ARGS__) #define din1d(a) \ din1(a); \ a-- #define din2d(a, b) \ din2(a, b); \ a--, b-- #define din3d(a, b, c) \ din3(a, b, c); \ a--, b--, c-- #define din4d(a, b, c, d) \ din4(a, b, c, d); \ a--, b--, c--, d-- #define dind(...) over4(__VA_ARGS__, din4d, din3d, din2d, din1d)(__VA_ARGS__) template <class T> void out2(T &&head) { cout << head; } template <class T, class... U> void out2(T &&head, U &&...tail) { cout << head << " "; out2(tail...); } template <class T, class... U> void out(T &&head, U &&...tail) { cout << head << " "; out2(tail...); cout << "" << endl; } template <class T> void out(T &&head) { cout << head << endl; } void out() { cout << "" << endl; } #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; } /*@formatter:on*/ 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]; if (typeid(a[i]) == typeid(P)) { ss << endl; } else { ss << " "; } } return ss.str(); } /*@formatter:off*/ 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(); } 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 = 12; H = min(H, sz(a)); rep(i, H) { ss << endl; ss << out_m2(a[i], W, U, i); } return ss.str(); } template <class T, size_t N> string out_m2(T (&a)[N]) { vector<T> b; resize(b, N); rep(i, N) { b[i] = a[i]; } return out_m2(b); } template <class T, size_t N, size_t M> string out_m2(T (&a)[N][M]) { vector<vector<T>> b; resize(b, N, M); rep(i, N) { rep(j, M) { b[i][j] = a[i][j]; } } return out_m2(b); } template <class T, size_t N, size_t M, size_t O> string out_m2(T (&a)[N][M][O]) { vector<vector<vector<T>>> b; resize(b, N, M, O); rep(i, N) { rep(j, M) { rep(k, O) { b[i][j][k] = a[i][j][k]; } } } return out_m2(b); } string out_m2(int a) { stringstream ss; ss << a; return ss.str(); } /*@formatter:on*/ template <class T> string out_m2(mvec<mvec<T>> &a, ll H = inf, ll W = inf, int key = inf) { H = min({H, sz(a), 6ll}); W = min({W, sz(a[0]), 6ll}); stringstream ss; ss << endl; // if (key == inf)ss << " *|"; else ss << " " << key << "|"; if (key == inf) ss << " *|"; else { ss << std::right << std::setw(2) << key; ss << "|"; } rep(w, -W, W) ss << std::right << std::setw(4) << w; ss << "" << endl; rep(w, W * 8 + 3) ss << "_"; ss << "" << endl; rep(h, -H, H) { ss << std::right << std::setw(2) << h << "|"; int NW = min(sz(a[h]), 6ll); rep(w, -NW, NW) { if (abs(a[h][w]) == linf) ss << " e" << ""; else ss << std::right << std::setw(4) << a[h][w]; } ss << "" << endl; } return ss.str(); } /*@formatter:on*/ template <class T> string out_m2(mvec<mvec<mvec<T>>> &a, ll H = inf, ll W = inf, ll U = inf) { stringstream ss; if (H == inf) H = 6; H = min(H, sz(a)); rep(i, -H, H) { ss << endl; ss << out_m2(a[i], W, U, i); } return ss.str(); } /*@formatter:off*/ template <class T> string out_m2(T &a) { stringstream ss; ss << a; return ss.str(); } /*@formatter:on*/ 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(); } /*@formatter:off*/ 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(); } 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 template <class T> void outl(vector<T> &a, int n = inf) { rep(i, min(n, sz(a))) cout << a[i] << endl; } //テーブルをスペースなしで出力 template <class T> void outt(vector<vector<T>> &a) { rep(i, sz(a)) { rep(j, sz(a[i])) { cout << a[i][j]; } cout << endl; } } // int型をbit表記で出力 void outb(int a) { cout << bitset<20>(a) << endl; } template <class T> void na(vector<T> &a, ll n) { a.resize(n); rep(i, n) cin >> a[i]; } #define dna(a, n) \ vi a(n); \ rep(dnai, n) cin >> a[dnai]; #define dnad(a, n) \ vi a(n); \ rep(dnai, n) cin >> a[dnai], a[dnai]--; template <class T> void nao(vector<T> &a, ll n) { a.resize(n + 1); a[0] = 0; rep(i, n) cin >> a[i + 1]; } template <class T> void naod(vector<T> &a, ll n) { a.resize(n + 1); a[0] = 0; rep(i, n) cin >> a[i + 1], a[i + 1]--; } template <class T> void nad(vector<T> &a, ll n) { a.resize(n); rep(i, n) cin >> a[i], a[i]--; } template <class T, class U> void na2(vector<T> &a, vector<U> &b, ll n) { a.resize(n); b.resize(n); rep(i, n) cin >> a[i] >> b[i]; } #define dna2(a, b, n) \ vi a(n), b(n); \ rep(dna2i, n) cin >> a[dna2i] >> b[dna2i]; template <class T, class U> void nao2(vector<T> &a, vector<U> &b, ll n) { a.resize(n + 1); b.resize(n + 1); a[0] = b[0] = 0; rep(i, n) cin >> a[i + 1] >> b[i + 1]; } #define dna2d(a, b, n) \ vi a(n), b(n); \ rep(dna2di, n) { \ cin >> a[dna2di] >> b[dna2di]; \ a[dna2di]--, b[dna2di]--; \ } template <class T, class U> void na2d(vector<T> &a, vector<U> &b, ll n) { a.resize(n); b.resize(n); rep(i, n) cin >> a[i] >> b[i], a[i]--, b[i]--; } template <class T, class U, class W> void na3(vector<T> &a, vector<U> &b, vector<W> &c, ll n) { a.resize(n); b.resize(n); c.resize(n); rep(i, n) cin >> a[i] >> b[i] >> c[i]; } #define dna3(a, b, c, n) \ vi a(n), b(n), c(n); \ rep(dna3i, n) cin >> a[dna3i] >> b[dna3i] >> c[dna3i]; template <class T, class U, class W> void na3d(vector<T> &a, vector<U> &b, vector<W> &c, ll 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 dna3d(a, b, c, n) \ vi a(n), b(n), c(n); \ rep(dna3di, n) { \ cin >> a[dna3di] >> b[dna3di] >> c[dna3di]; \ a[dna3di]--, b[dna3di]--, c[dna3di]--; \ } template <class T, class U, class W, class X> void na4(vector<T> &a, vector<U> &b, vector<W> &c, vector<X> &d, ll n) { a.resize(n); b.resize(n); c.resize(n); d.resize(n); rep(i, n) cin >> a[i] >> b[i] >> c[i] >> d[i]; } #define dna4(a, b, c, d, n) \ vi a(n), b(n), c(n), d(n); \ rep(dna4i, n) cin >> a[dna4i] >> b[dna4i] >> c[dna4i] >> d[dna4i]; #define dna4d(a, b, c, d, n) \ vi a(n), b(n), c(n), d(n); \ rep(dna4i, n) cin >> a[dna4i] >> b[dna4i] >> c[dna4i] >> d[dna4i], \ --a[dna4i], --b[dna4i], --c[dna4i], --d[dna4i]; #define nt(a, h, w) \ resize(a, h, w); \ rep(nthi, h) rep(ntwi, w) cin >> a[nthi][ntwi]; #define ntd(a, h, w) \ resize(a, h, w); \ rep(ntdhi, h) rep(ntdwi, w) cin >> a[ntdhi][ntdwi], a[ntdhi][ntdwi]--; #define ntp(a, h, w) \ resize(a, h + 2, w + 2); \ fill(a, '#'); \ rep(ntphi, 1, h + 1) rep(ntpwi, 1, w + 1) cin >> a[ntphi][ntpwi]; //デバッグ #define sp << " " << #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__) #ifdef _DEBUG #define deb(...) \ cerr << over10(__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' //@formatter:off //よく使うクラス、構造体 // graphでredefineしているため、書き換えたら向こうも書き換える struct unionfind { vector<ll> par; vector<ll> siz; vector<ll> es; ll n, trees; //連結グループの数(親の種類) unionfind(ll n) : n(n), trees(n) { par.resize(n); siz.resize(n); es.resize(n); for (ll i = 0; i < n; i++) { par[i] = i; siz[i] = 1; } } ll root(ll x) { if (par[x] == x) { return x; } else { return par[x] = root(par[x]); } } ll operator()(ll x) { return root(x); } bool unite(ll x, ll y) { x = root(x); y = root(y); es[x]++; if (x == y) return false; if (siz[x] > siz[y]) swap(x, y); trees--; par[x] = y; siz[y] += siz[x]; es[y] += es[x]; return true; } bool same(ll x, ll y) { return root(x) == root(y); } ll size(ll x) { return siz[root(x)]; } ll esize(ll x) { return es[root(x)]; } vi sizes() { vi cou(n); vi ret; ret.reserve(n); rep(i, n) { cou[root(i)]++; } rep(i, n) { if (cou[i]) ret.push_back(cou[i]); } return ret; } //つながりを無向グラフと見なし、xが閉路に含まれるか判定 bool close(ll x) { return esize(x) >= size(x); } vector<vi> sets() { vi ind(n, -1); ll i = 0; vvi(res, trees); rep(j, n) { ll r = root(j); if (ind[r] == -1) ind[r] = i++; res[ind[r]].push_back(j); } rep(i, trees) { ll r = root(res[i][0]); if (res[i][0] == r) continue; rep(j, 1, sz(res[i])) { if (res[i][j] == r) { swap(res[i][0], res[i][j]); break; } } } return res; } }; //@formatter:off using bll = __int128; using u32 = unsigned; using u64 = unsigned long long; using u128 = __uint128_t; std::ostream &operator<<(std::ostream &dest, __int128_t value) { std::ostream::sentry s(dest); if (s) { __uint128_t tmp = value < 0 ? -value : value; char buffer[128]; char *d = std::end(buffer); do { --d; *d = "0123456789"[tmp % 10]; tmp /= 10; } while (tmp != 0); if (value < 0) { --d; *d = '-'; } ll len = std::end(buffer) - d; if (dest.rdbuf()->sputn(d, len) != len) { dest.setstate(std::ios_base::badbit); } } return dest; } //__int128 toi128(string &s) { __int128 ret = 0; for (ll i = 0; i < //s.length(); ++i) if ('0' <= s[i] && s[i] <= '9') ret = 10 * //ret + s[i] - '0'; return ret;} //エラー 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 re() { assert(0 == 1); exit(0); } void tle() { while (inf) cout << inf << endl; } //便利関数 //テスト用 #define rand xor128_ unsigned long xor128_(void) { static unsigned long x = 123456789, y = 362436069, z = 521288629, w = 88675123; unsigned long t; t = (x ^ (x << 11)); x = y; y = z; z = w; return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8))); } char ranc() { return (char)('a' + rand() % 26); } ll rand(ll min, ll max) { assert(min <= max); if (min >= 0 && max >= 0) { return rand() % (max + 1 - min) + min; } else if (max < 0) { return -rand(-max, -min); } else { if (rand() % 2) { return rand(0, max); } else { return -rand(0, -min); } } } vi ranv(ll n, ll min, ll max) { vi v(n); rep(i, n) v[i] = rand(min, max); return v; } str ransu(ll n) { str s; rep(i, n) s += (char)rand('A', 'Z'); return s; } str ransl(ll n) { str s; rep(i, n) s += (char)rand('a', 'z'); return s; } //単調増加 vi ranvinc(ll n, ll min, ll max) { vi v(n); bool bad = 1; while (bad) { bad = 0; v.resize(n); rep(i, n) { if (i && min > max - v[i - 1]) { bad = 1; break; } if (i) v[i] = v[i - 1] + rand(min, max - v[i - 1]); else v[i] = rand(min, max); } } return v; } //便利 汎用 void ranvlr(ll n, ll min, ll max, vi &l, vi &r) { l.resize(n); r.resize(n); rep(i, n) { l[i] = rand(min, max); r[i] = l[i] + rand(0, max - l[i]); } } template <class T> vector<pair<T, int>> run_length(vector<T> &a) { vector<pair<T, int>> 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 <class F> ll mgr(ll ok, ll ng, F f) { bool han = true; if (ok < ng) while (ng - ok > 1) { ll mid = (ok + ng) >> 1; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; deb(mid, han); } else while (ok - ng > 1) { ll mid = (ok + ng) >> 1; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; deb(mid, han); } return ok; } template <class F> dou mgrd(dou ok, dou ng, F f, int kai = 100) { bool han = true; if (ok < ng) rep(i, kai) { dou mid = (ok + ng) / 2; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; deb(mid, han); } else rep(i, kai) { dou mid = (ok + ng) / 2; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; deb(mid, han); } return ok; } // strを整数として比較 string smax(str &a, str b) { if (sz(a) < sz(b)) { return b; } else if (sz(a) > sz(b)) { return a; } else if (a < b) return b; else return a; } // strを整数として比較 string smin(str &a, str b) { if (sz(a) > sz(b)) { return b; } else if (sz(a) < sz(b)) { return a; } else if (a > b) return b; else return a; } //エラー-1 template <typename W, typename T> ll find(vector<W> &a, int l, const T key) { rep(i, l, sz(a)) if (a[i] == key) return i; return -1; } template <typename W, typename T> ll find(vector<W> &a, const T key) { rep(i, sz(a)) if (a[i] == key) return i; return -1; } template <typename W, typename T> P find(vector<vector<W>> &a, const T key) { rep(i, sz(a)) rep(j, sz(a[0])) if (a[i][j] == key) return mp(i, j); return mp(-1, -1); } template <typename W, typename U> T find(vector<vector<vector<W>>> &a, const U key) { rep(i, sz(a)) rep(j, sz(a[0])) rep(k, sz(a[0][0])) if (a[i][j][k] == key) return mt(i, j, k); return mt(-1, -1, -1); } // stringも書く int find(string &s, const string key) { int klen = sz(key); rep(i, sz(s) - klen + 1) { if (s[i] != key[0]) continue; if (s.substr(i, klen) == key) { return i; } } return -1; } int find(string &s, int l, const string key) { int klen = sz(key); rep(i, l, sz(s) - klen + 1) { if (s[i] != key[0]) continue; if (s.substr(i, klen) == key) { return i; } } return -1; } int find(string &s, const char key) { rep(i, sz(s)) { if (s[i] == key) return i; } return -1; } int find(string &s, int l, const char key) { rep(i, l, sz(s)) { if (s[i] == key) return i; } return -1; } template <typename W, typename T> ll count2(W &a, const T k) { return a == k; } template <typename W, typename T> ll count2(vector<W> &a, const T k) { ll ret = 0; fora(v, a) ret += count2(v, k); return ret; } template <typename W, typename T> ll count(vector<W> &a, const T k) { ll ret = 0; fora(v, a) ret += count2(v, k); return ret; } vi count(vi &a) { int ma = 0; fora(v, a) { if (ma < v) ma = v; } vi res(ma + 1); fora(v, a) { res[v]++; } return res; } ll count(str &a, str k) { ll ret = 0, len = k.length(); auto pos = a.find(k); while (pos != string::npos) pos = a.find(k, pos + len), ++ret; return ret; } vi count(str &a) { vi cou(26); char c = 'a'; if ('A' <= a[0] && a[0] <= 'Z') c = 'A'; rep(i, sz(a))++ cou[a[i] - c]; return cou; } #define couif count_if // algorythm 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)); } template <class U> void rev(vector<vector<U>> &a) { vector<vector<U>> b(sz(a[0]), vector<U>(sz(a))); rep(h, sz(a)) rep(w, sz(a[0])) b[w][h] = a[h][w]; a = b; } void rev(string &a) { reverse(all(a)); } constexpr ll p10[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000ll, 100000000000ll, 1000000000000ll, 10000000000000ll, 100000000000000ll, 1000000000000000ll, 10000000000000000ll, 100000000000000000ll, 1000000000000000000ll}; ll get(ll a, ll keta) { return (a / (ll)pow(10, keta)) % 10; } // 0は0桁 ll keta(ll v) { if (v < p10[9]) { if (v < p10[4]) { if (v < p10[2]) { if (v < p10[1]) { if (v < p10[0]) return 0; else return 1; } else return 2; } else { if (v < p10[3]) return 3; else return 4; } } else { if (v < p10[7]) { if (v < p10[5]) return 5; else if (v < p10[6]) return 6; else return 7; } else { if (v < p10[8]) return 8; else return 9; } } } else { if (v < p10[13]) { if (v < p10[11]) { if (v < p10[10]) return 10; else return 11; } else { if (v < p10[12]) return 12; else return 13; } } else { if (v < p10[15]) { if (v < p10[14]) return 14; else return 15; } else { if (v < p10[17]) { if (v < p10[16]) return 16; else return 17; } else { if (v < p10[18]) return 18; else return 19; } } } } } ll dsum(ll v, ll sin = 10) { ll ret = 0; for (; v; v /= sin) ret += v % sin; return ret; } ll mask10(ll v) { return p10[v] - 1; } //変換系 //[v] := iとなるようなvectorを返す //存在しない物は-1 template <class T> auto keys(T &a) { vector<decltype((a.begin())->fi)> res; for (auto &&k : a) res.push_back(k.fi); return res; } template <class T> auto values(T &a) { vector<decltype((a.begin())->se)> res; for (auto &&k : a) res.push_back(k.se); return res; } template <class T, class U> bool chma(T &a, const U &b) { if (a < b) { a = b; return true; } return false; } template <class T, class U> bool chmi(T &a, const U &b) { if (b < a) { a = b; return true; } return false; } template <class T> constexpr T min(T a, signed b) { return a < b ? a : b; } template <class T> constexpr T max(T a, signed b) { return a < b ? b : a; } template <class T> constexpr T min(T a, T b, T c) { return a >= b ? b >= c ? c : b : a >= c ? c : a; } template <class T> constexpr T max(T a, T b, T c) { return a <= b ? b <= c ? c : b : a <= c ? c : a; } template <class T> T min(vector<T> &a) { return *min_element(all(a)); } template <class T> T mini(vector<T> &a) { return min_element(all(a)) - a.begin(); } template <class T> T min(vector<T> &a, ll n) { return *min_element(a.begin(), a.begin() + min(n, sz(a))); } template <class T> T min(vector<T> &a, ll s, ll n) { return *min_element(a.begin() + s, a.begin() + min(n, sz(a))); } template <class T> T max(vector<T> &a) { return *max_element(all(a)); } template <class T, class U> T max(vector<T> &a, vector<U> &b) { return max(*max_element(all(a)), *max_element(all(b))); } template <class T> T maxi(vector<T> &a) { return max_element(all(a)) - a.begin(); } template <class T> T max(vector<T> &a, ll n) { return *max_element(a.begin(), a.begin() + min(n, sz(a))); } template <class T> T max(vector<T> &a, ll s, ll n) { return *max_element(a.begin() + s, a.begin() + min(n, sz(a))); } template <typename A, size_t N> A max(A (&a)[N]) { A res = a[0]; rep(i, N) res = max(res, a[i]); return res; } template <typename A, size_t N, size_t O> A max(A (&a)[N][O]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P> A max(A (&a)[N][O][P]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A max(A (&a)[N][O][P][Q], const T &v) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A max(A (&a)[N][O][P][Q][R]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A max(A (&a)[N][O][P][Q][R][S]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N> A min(A (&a)[N]) { A res = a[0]; rep(i, N) res = min(res, a[i]); return res; } template <typename A, size_t N, size_t O> A min(A (&a)[N][O]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P> A min(A (&a)[N][O][P]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A min(A (&a)[N][O][P][Q], const T &v) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A min(A (&a)[N][O][P][Q][R]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A min(A (&a)[N][O][P][Q][R][S]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <class T> T sum(vector<T> &v, ll s, ll t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += v[i]; return ret; } template <class T> T sum(vector<T> &v, ll t = inf) { return sum(v, 0, t); } template <class T> T sum(vector<vector<T>> &v, int s, int t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<T>> &v, int t = inf) { return sum(v, 0, t); } template <class T> T sum(vector<vector<vector<T>>> &v, int s, int t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<T>>> &v, int t = inf) { return sum(v, 0, t); } template <class T> T sum(vector<vector<vector<vector<T>>>> &v, int s, int t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<vector<T>>>> &v, int t = inf) { return sum(v, 0, t); } template <class T> T sum(vector<vector<vector<vector<vector<T>>>>> &v, int s, int t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<vector<vector<T>>>>> &v, int t = inf) { return sum(v, 0, t); } template <class T> auto sum(priority_queue<T, vector<T>, greater<T>> &r) { auto q = r; T ret = 0; while (sz(q)) { ret += q.top(); q.pop(); } return ret; } template <class T> auto sum(priority_queue<T> &r) { auto q = r; T ret = 0; while (sz(q)) { ret += q.top(); q.pop(); } return ret; } // template<class T, class U, class... W> auto sumn(vector<T> &v, U head, W... // tail) { auto ret = sum(v[0], tail...); rep(i, 1, min(sz(v), head))ret += // sum(v[i], tail...); return ret;} vi v_i(vi &a) { int n = max(a) + 1; vi ret(n, -1); rep(i, sz(a)) { ret[a[i]] = i; } return ret; } void clear(PQ &q) { q = PQ(); } void clear(priority_queue<int> &q) { q = priority_queue<int>(); } template <class T> void clear(queue<T> &q) { while (q.size()) q.pop(); } template <class T> T *negarr(ll size) { T *body = (T *)malloc((size * 2 + 1) * sizeof(T)); return body + size; } template <class T> T *negarr2(ll h, ll w) { double **dummy1 = new double *[2 * h + 1]; double *dummy2 = new double[(2 * h + 1) * (2 * w + 1)]; dummy1[0] = dummy2 + w; for (ll i = 1; i <= 2 * h + 1; ++i) { dummy1[i] = dummy1[i - 1] + 2 * w + 1; } double **a = dummy1 + h; return a; } // 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> vector<T> ima(vector<T> &v) { vector<T> ret = v; rep(i, sz(ret) - 1) chma(ret[i + 1], ret[i]); return ret; } // template<class T> vector<T> rimi(vector<T> &v) { vector<T> ret = v; rer(i, // sz(ret) - 1, 1)chmi(ret[i - 1], ret[i]); return ret;} template<class T> // vector<T> rima(vector<T> &v) { vector<T> ret = v; rer(i, sz(ret) - 1, // 1)chma(ret[i - 1], ret[i]); return ret;} template <class T> struct ruiC { vector<T> rui; ruiC(vector<T> &ru) : rui(ru) {} /*先頭0*/ ruiC() : rui(1, 0) {} T operator()(ll l, ll r) { if (l > r) { cerr << "ruic "; deb(l, r); assert(0); } return rui[r] - rui[l]; } T operator()(int r) { return operator()(0, r); } /*ruiv[]をruic[]に変えた際意味が変わるのがまずいため()と統一*/ /*単体iを返す 累積でないことに注意(seg木との統一でこうしている)*/ // T operator[](ll i) { return rui[i + 1] - rui[i]; } T operator[](ll i) { return rui[i]; } /*0から順に追加される必要がある*/ void operator+=(T v) { rui.push_back(rui.back() + v); } void add(int i, T v) { if (sz(rui) - 1 != i) ole(); operator+=(v); } T back() { return rui.back(); } ll size() { return rui.size(); } auto begin() { return rui.begin(); } auto end() { return rui.end(); } }; template <class T> struct ruimax { template <typename Monoid> struct SegmentTree { /*pairで処理*/ int sz; vector<Monoid> seg; const Monoid M1 = mp(MIN(T), -1); Monoid f(Monoid a, Monoid b) { return max(a, b); } void build(vector<T> &a) { int n = sz(a); sz = 1; while (sz < n) sz <<= 1; seg.assign(2 * sz, M1); rep(i, n) { seg[i + sz] = mp(a[i], i); } for (int k = sz - 1; k > 0; k--) { seg[k] = f(seg[k << 1], seg[(k << 1) | 1]); } } Monoid query(int a, int b) { Monoid L = M1, R = M1; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) L = f(L, seg[a++]); if (b & 1) R = f(seg[--b], R); } return f(L, R); } Monoid operator[](const int &k) const { return seg[k + sz]; } }; private: vector<T> ve; SegmentTree<pair<T, int>> seg; vector<T> rv; vector<T> ri; bool build = false; public: int n; ruimax(vector<T> &a) : ve(a), n(sz(a)) { int index = -1; int ma = MIN(T); rv.resize(n + 1); ri.resize(n + 1); rv[0] = -linf; ri[0] = -1; rep(i, n) { if (chma(ma, a[i])) { index = i; } rv[i + 1] = ma; ri[i + 1] = index; } } T operator()(int l, int r) { if (!(l <= r && 0 <= l && r <= n)) { deb(l, r, n); assert(0); } if (l == 0) { return rv[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).first; } } T operator()(int r = inf) { return operator()(0, min(r, n)); } T operator[](int r) { return operator()(0, r); } T getv(int l, int r) { return operator()(l, r); } T geti(int l, int r) { assert(l <= r && 0 <= l && r <= n); if (l == 0) { return ri[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).second; } } T geti(int r = inf) { return geti(0, min(r, n)); }; T getv(int r = inf) { return getv(0, min(r, n)); }; auto begin() { return rv.begin(); } auto end() { return rv.end(); } }; template <class T> struct ruimin { template <typename Monoid> struct SegmentTree { /*pairで処理*/ int sz; vector<Monoid> seg; const Monoid M1 = mp(MAX(T), -1); Monoid f(Monoid a, Monoid b) { return min(a, b); } void build(vector<T> &a) { int n = sz(a); sz = 1; while (sz < n) sz <<= 1; seg.assign(2 * sz, M1); rep(i, n) { seg[i + sz] = mp(a[i], i); } for (int k = sz - 1; k > 0; k--) { seg[k] = f(seg[k << 1], seg[(k << 1) | 1]); } } Monoid query(int a, int b) { Monoid L = M1, R = M1; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) L = f(L, seg[a++]); if (b & 1) R = f(seg[--b], R); } return f(L, R); } Monoid operator[](const int &k) const { return seg[k + sz]; } }; private: vector<T> ve; SegmentTree<pair<T, int>> seg; vector<T> rv; vector<T> ri; bool build = false; int n; public: ruimin(vector<T> &a) : ve(a), n(sz(a)) { int index = -1; int mi = MAX(T); rv.resize(n + 1); ri.resize(n + 1); rv[0] = linf; ri[0] = -1; rep(i, n) { if (chmi(mi, a[i])) { index = i; } rv[i + 1] = mi; ri[i + 1] = index; } } T operator()(int l, int r) { assert(l <= r && 0 <= l && r <= n); if (l == 0) { return rv[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).first; } } T operator()(int r = inf) { return operator()(0, min(r, n)); } T operator[](int r) { return operator()(0, r); } T getv(int l, int r) { return operator()(l, r); } T geti(int l, int r) { assert(l <= r && 0 <= l && r <= n); if (l == 0) { return ri[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).second; } } T geti(int r = inf) { return geti(0, min(r, n)); }; T getv(int r = inf) { return getv(0, min(r, n)); }; auto begin() { return rv.begin(); } auto end() { return rv.end(); } }; 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() { return ruiC<T>(); } template <class T> ruiC<T> ruic(vector<T> &a) { vector<T> ret = ruiv(a); return ruiC<T>(ret); } vvi() ruib(vi &a) { vvi(res, 61, sz(a) + 1); rep(k, 61) { rep(i, sz(a)) { res[k][i + 1] = res[k][i] + ((a[i] >> k) & 1); } } return res; } vector<ruiC<int>> ruibc(vi &a) { vector<ruiC<int>> ret(61); vvi(res, 61, sz(a)); rep(k, 61) { rep(i, sz(a)) { res[k][i] = (a[i] >> k) & 1; } ret[k] = ruic(res[k]); } return 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); } // h query template <class T> vector<T> imoh(vector<vector<T>> &v, int w) { vector<T> ret(sz(v)); rep(h, sz(ret)) { ret[h] = v[h][w]; } rep(i, sz(ret) - 1) { ret[i + 1] += ret[i]; } return ret; } template <class T> vector<T> ruih(vector<vector<T>> &v, int w) { vector<T> ret(sz(v) + 1); rep(h, sz(v)) { ret[h + 1] = v[h][w]; } rep(i, sz(v)) { ret[i + 1] += ret[i]; } return ret; } template <class T> ruiC<T> ruihc(vector<vector<T>> &a, int w) { vector<T> ret = ruih(a, w); return ruiC<T>(ret); } // xor template <class T> struct ruixC { 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> struct rruiC { vector<T> rui; int n; rruiC(vector<T> &a) : n(sz(a)) { rui.resize(n + 1); rer(i, n - 1) { rui[i] = rui[i + 1] + a[i]; } } /*[r l)*/ T operator()(int r, int l) { r++; l++; assert(l <= r && l >= 0 && r <= n); return rui[l] - rui[r]; } T operator()(int l) { return operator()(n - 1, l); } T operator[](int i) { return operator()(i); } }; template <class T> ostream &operator<<(ostream &os, rruiC<T> a) { fora(v, a.rui) os << v << " "; return os; } #define rrui rruic template <class T> rruiC<T> rruic(vector<T> &a) { return rruiC<T>(a); } //掛け算 template <class T> struct ruimulC { vector<T> rv; int n; ruimulC(vector<T> &a) : rv(a), n(sz(a)) { rv.resize(n + 1); rv[0] = 1; rep(i, n) { rv[i + 1] = a[i] * rv[i]; } } ruimulC() : n(0) { rv.resize(n + 1); rv[0] = 1; } void operator+=(T v) { rv.push_back(rv.back() * v); n++; } T operator()(int l, int r) { assert(l <= r && 0 <= l && r <= n); return rv[r] / rv[l]; } T operator()(int r = inf) { return operator()(0, min(r, n)); } T operator[](int r) { return operator()(0, r); } auto begin() { return rv.begin(); } auto end() { return rv.end(); } }; template <class T> ruimulC<T> ruimul(vector<T> &a) { return ruimulC<T>(a); } template <class T> ruimulC<T> ruimul() { vector<T> a; return ruimulC<T>(a); } /*@formatter:off*/ 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> void inc(pair<T, U> &a, U v = 1) { a.first += v, a.second += v; } template <class T, class U> void inc(T &a, U v = 1) { a += v; } template <class T, class U> void inc(vector<T> &a, U v = 1) { for (auto &u : a) inc(u, v); } template <class T, class U> void dec(T &a, U v = 1) { a -= v; } template <class T, class U> void dec(vector<T> &a, U v = 1) { for (auto &u : a) dec(u, v); } template <class U> void dec(string &a, U v = 1) { for (auto &u : a) dec(u, v); } template <class T, class U, class W> void dec(vector<T> &a, vector<U> &b, W v = 1) { for (auto &u : a) dec(u, v); for (auto &u : b) dec(u, v); } template <class T, class U, class W> void dec(vector<T> &a, vector<U> &b, vector<W> &c) { for (auto &u : a) dec(u, 1); for (auto &u : b) dec(u, 1); for (auto &u : c) dec(u, 1); } bool ins(ll h, ll w, ll H, ll W) { return h >= 0 && w >= 0 && h < H && w < W; } bool ins(ll l, ll v, ll r) { return l <= v && v < r; } template <class T> bool ins(vector<T> &a, ll i, ll j = 0) { return ins(0, i, sz(a)) && ins(0, j, sz(a)); } #define inside ins ll u(ll a) { return a < 0 ? 0 : a; } template <class T> vector<T> u(vector<T> &a) { vector<T> ret = a; fora(v, ret) v = u(v); return ret; } //添え字を返す template <class F> ll goldd_l(ll left, ll right, F calc) { double GRATIO = 1.6180339887498948482045868343656; ll lm = left + (ll)((right - left) / (GRATIO + 1.0)); ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); ll fl = calc(lm); ll fr = calc(rm); while (right - left > 10) { if (fl < fr) { right = rm; rm = lm; fr = fl; lm = left + (ll)((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } ll minScore = MAX(ll); ll resIndex = left; for (ll i = left; i < right + 1; ++i) { ll score = calc(i); if (minScore > score) { minScore = score; resIndex = i; } } return resIndex; } template <class F> ll goldt_l(ll left, ll right, F calc) { double GRATIO = 1.6180339887498948482045868343656; ll lm = left + (ll)((right - left) / (GRATIO + 1.0)); ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); ll fl = calc(lm); ll fr = calc(rm); while (right - left > 10) { if (fl > fr) { right = rm; rm = lm; fr = fl; lm = left + (ll)((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } if (left > right) { ll l = left; left = right; right = l; } ll maxScore = MIN(ll); ll resIndex = left; for (ll i = left; i < right + 1; ++i) { ll score = calc(i); if (maxScore < score) { maxScore = score; resIndex = i; } } return resIndex; } /*loopは200にすればおそらく大丈夫 余裕なら300に*/ template <class F> dou goldd_d(dou left, dou right, F calc, ll loop = 200) { dou GRATIO = 1.6180339887498948482045868343656; dou lm = left + ((right - left) / (GRATIO + 1.0)); dou rm = lm + ((right - lm) / (GRATIO + 1.0)); dou fl = calc(lm); dou fr = calc(rm); /*200にすればおそらく大丈夫*/ /*余裕なら300に*/ ll k = 141; loop++; while (--loop) { if (fl < fr) { right = rm; rm = lm; fr = fl; lm = left + ((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + ((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } return left; } template <class F> dou goldt_d(dou left, dou right, F calc, ll loop = 200) { double GRATIO = 1.6180339887498948482045868343656; dou lm = left + ((right - left) / (GRATIO + 1.0)); dou rm = lm + ((right - lm) / (GRATIO + 1.0)); dou fl = calc(lm); dou fr = calc(rm); loop++; while (--loop) { if (fl > fr) { right = rm; rm = lm; fr = fl; lm = left + ((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + ((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } return left; } // l ~ rを複数の区間に分割し、極致を与えるiを返す time-20 msまで探索 template <class F> ll goldd_ls(ll l, ll r, F calc, ll time = 2000) { auto lim = milliseconds(time - 20); ll mini = 0, minv = MAX(ll); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/ ll nl = l; ll nr = l + haba; rep(i, k) { ll ni = goldd_l(nl, nr, calc); if (chmi(minv, calc(ni))) mini = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return mini; } template <class F> ll goldt_ls(ll l, ll r, F calc, ll time = 2000) { auto lim = milliseconds(time - 20); ll maxi = 0, maxv = MIN(ll); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/ ll nl = l; ll nr = l + haba; rep(i, k) { ll ni = goldt_l(nl, nr, calc); if (chma(maxv, calc(ni))) maxi = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return maxi; } template <class F> dou goldd_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を持つ*/ auto lim = milliseconds(time - 20); dou mini = 0, minv = MAX(dou); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); dou haba = (r - l) / k; dou nl = l; dou nr = l + haba; rep(i, k) { dou ni = goldd_d(nl, nr, calc); if (chmi(minv, calc(ni))) mini = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return mini; } template <class F> dou goldt_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を残している*/ auto lim = milliseconds(time - 20); dou maxi = 0, maxv = MIN(dou); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); dou haba = (r - l) / k; dou nl = l; dou nr = l + haba; rep(i, k) { dou ni = goldt_d(nl, nr, calc); if (chma(maxv, calc(ni))) maxi = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return maxi; } template <class T> T min(vector<vector<T>> &a) { T res = MAX(T); rep(i, a.size()) chmi(res, *min_element(all(a[i]))); return res; } template <class T> T max(vector<vector<T>> &a) { T res = MIN(T); rep(i, a.size()) chma(res, *max_element(all(a[i]))); return res; } constexpr bool bget(ll m, ll keta) { #ifdef _DEBUG assert(keta <= 62); //オーバーフロー 1^62までしか扱えない #endif return (m >> keta) & 1; } // bget(n)次元 // NならN-1まで vector<vi> bget2(vi &a, int keta_size) { vvi(res, keta_size, sz(a)); rep(k, keta_size) { rep(i, sz(a)) { res[k][i] = bget(a[i], k); } } return res; } vi bget1(vi &a, int keta) { vi res(sz(a)); rep(i, sz(a)) { res[i] = bget(a[i], keta); } return res; } ll bget(ll m, ll keta, ll sinsuu) { m /= (ll)pow(sinsuu, keta); return m % sinsuu; } constexpr ll bit(ll n) { #ifdef _DEBUG assert(n <= 62); //オーバーフロー 1^62までしか扱えない #endif return (1LL << (n)); } ll bit(ll n, ll sinsuu) { return (ll)pow(sinsuu, n); } ll mask(ll n) { return (1ll << n) - 1; } // aをbitに置きなおす //{0, 2} -> 101 ll biti(vi &a) { int m = 0; for (auto &&v : a) m |= bit(v); return m; } //{1, 1, 0} -> 011 ll bitb(vi &a) { int m = 0; rep(i, sz(a)) if (a[i]) m |= bit(i); return m; } #define bcou __builtin_popcountll //最下位ビット ll lbit(ll n) { return n & -n; } ll lbiti(ll n) { return log2(n & -n); } //最上位ビット ll hbit(ll n) { n |= (n >> 1); n |= (n >> 2); n |= (n >> 4); n |= (n >> 8); n |= (n >> 16); n |= (n >> 32); return n - (n >> 1); } ll hbiti(ll n) { return log2(hbit(n)); } ll hbitk(ll n) { ll k = 0; rer(i, 5) { ll a = k + (1ll << i); ll b = 1ll << a; if (b <= n) k += 1ll << i; } return k; } //初期化は0を渡す ll nextComb(ll &mask, ll n, ll r) { if (!mask) return mask = (1LL << r) - 1; ll x = mask & -mask; /*最下位の1*/ ll y = mask + x; /*連続した下の1を繰り上がらせる*/ ll res = ((mask & ~y) / x >> 1) | y; if (bget(res, n)) return mask = 0; else return mask = res; } // n桁以下でビットがr個立っているもののvectorを返す vi bitCombList(ll n, ll r) { vi res; ll m = 0; while (nextComb(m, n, r)) { res.push_back(m); } return res; } // masの立ってるindexを見る #define forbit(i, mas) \ for (int forbitj = lbit(mas), forbitm = mas, i = log2(forbitj); forbitm; \ forbitm = forbitj ? forbitm ^ forbitj : 0, forbitj = lbit(forbitm), \ i = log2(forbitj)) char itoal(ll i) { return 'a' + i; } char itoaL(ll i) { return 'A' + i; } ll altoi(char c) { if ('A' <= c && c <= 'Z') return c - 'A'; return c - 'a'; } ll ctoi(char c) { return c - '0'; } char itoc(ll i) { return i + '0'; } ll vtoi(vi &v) { ll res = 0; if (sz(v) > 18) { debugline("vtoi"); deb(sz(v)); ole(); } rep(i, sz(v)) { res *= 10; res += v[i]; } return res; } vi itov(ll i) { vi res; while (i) { res.push_back(i % 10); i /= 10; } rev(res); return res; } vi stov(string &a) { ll n = sz(a); vi ret(n); rep(i, n) { ret[i] = a[i] - '0'; } return ret; } //基準を満たさないものは0になる vi stov(string &a, char one) { ll n = sz(a); vi ret(n); rep(i, n) ret[i] = a[i] == one; return ret; } vector<vector<ll>> ctoi(vector<vector<char>> s, char c) { ll n = sz(s), m = sz(s[0]); vector<vector<ll>> res(n, vector<ll>(m)); rep(i, n) rep(j, m) res[i][j] = s[i][j] == c; return res; } #define unique(v) v.erase(unique(v.begin(), v.end()), v.end()); //[i] := vを返す // aは0~n-1で置き換えられる vi compress(vi &a) { vi b; ll len = a.size(); for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { a[i] = lower_bound(all(b), a[i]) - b.begin(); } ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, umap<ll, ll> &map) { vi b; ll len = a.size(); for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { ll v = a[i]; a[i] = lower_bound(all(b), a[i]) - b.begin(); map[v] = a[i]; } ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, vi &r) { vi b; ll len = a.size(); fora(v, a) b.push_back(v); fora(v, r) b.push_back(v); sort(b); unique(b); for (ll i = 0; i < len; ++i) a[i] = lower_bound(all(b), a[i]) - b.begin(); for (ll i = 0; i < sz(r); ++i) r[i] = lower_bound(all(b), r[i]) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, vi &r, vi &s) { vi b; ll len = a.size(); fora(v, a) b.push_back(v); fora(v, r) b.push_back(v); fora(v, s) b.push_back(v); sort(b); unique(b); for (ll i = 0; i < len; ++i) a[i] = lower_bound(all(b), a[i]) - b.begin(); for (ll i = 0; i < sz(r); ++i) r[i] = lower_bound(all(b), r[i]) - b.begin(); for (ll i = 0; i < sz(s); ++i) r[i] = lower_bound(all(b), s[i]) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vector<vi> &a) { vi b; fora(vv, a) fora(v, vv) b.push_back(v); sort(b); unique(b); fora(vv, a) fora(v, vv) v = lower_bound(all(b), v) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vector<vector<vi>> &a) { vi b; fora(vvv, a) fora(vv, vvv) fora(v, vv) b.push_back(v); sort(b); unique(b); fora(vvv, a) fora(vv, vvv) fora(v, vv) v = lower_bound(all(b), v) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } void compress(ll a[], ll len) { vi b; for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { a[i] = lower_bound(all(b), a[i]) - b.begin(); } } //要素が見つからなかったときに困る #define binarySearch(a, v) (binary_search(all(a), v)) #define lowerIndex(a, v) (lower_bound(all(a), v) - a.begin()) #define upperIndex(a, v) (upper_bound(all(a), v) - a.begin()) #define rlowerIndex(a, v) (upper_bound(all(a), v) - a.begin() - 1) #define rupperIndex(a, v) (lower_bound(all(a), v) - a.begin() - 1) template <class T, class U, class W> T lowerBound(vector<T> &a, U v, W banpei) { auto it = lower_bound(a.begin(), a.end(), v); if (it == a.end()) return banpei; else return *it; } template <class T, class U, class W> T lowerBound(ruiC<T> &a, U v, W banpei) { return lowerBound(a.rui, v, banpei); } template <class T, class U, class W> T upperBound(vector<T> &a, U v, W banpei) { auto it = upper_bound(a.begin(), a.end(), v); if (it == a.end()) return banpei; else return *it; } template <class T, class U, class W> T upperBound(ruiC<T> &a, U v, W banpei) { return upperBound(a.rui, v, banpei); } template <class T, class U, class W> T rlowerBound(vector<T> &a, U v, W banpei) { auto it = upper_bound(a.begin(), a.end(), v); if (it == a.begin()) return banpei; else { return *(--it); } } template <class T, class U, class W> T rlowerBound(ruiC<T> &a, U v, W banpei) { return rlowerBound(a.rui, v, banpei); } template <class T, class U, class W> T rupperBound(vector<T> &a, U v, W banpei) { auto it = lower_bound(a.begin(), a.end(), v); if (it == a.begin()) return banpei; else { return *(--it); } } template <class T, class U, class W> T rupperBound(ruiC<T> &a, U v, W banpei) { return rupperBound(a.rui, v, banpei); } #define next2(a) next(next(a)) #define prev2(a) prev(prev(a)) //狭義の単調増加列 長さを返す template <class T> int lis(vector<T> &a) { int n = sz(a); vi tail(n + 1, MAX(T)); rep(i, n) { int id = lowerIndex(tail, a[i]); /**/ tail[id] = a[i]; } return lowerIndex(tail, MAX(T)); } template <class T> int lis_eq(vector<T> &a) { int n = sz(a); vi tail(n + 1, MAX(T)); rep(i, n) { int id = upperIndex(tail, a[i]); /**/ tail[id] = a[i]; } return lowerIndex(tail, MAX(T)); } // iteratorを返す // valueが1以上の物を返す 0は見つけ次第削除 // vを減らす場合 (*it).se--でいい template <class T, class U, class V> auto lower_map(map<T, U> &m, V k) { auto ret = m.lower_bound(k); while (ret != m.end() && (*ret).second == 0) { ret = m.erase(ret); } return ret; } template <class T, class U, class V> auto upper_map(map<T, U> &m, V k) { auto ret = m.upper_bound(k); while (ret != m.end() && (*ret).second == 0) { ret = m.erase(ret); } return ret; } //存在しなければエラー template <class T, class U, class V> auto rlower_map(map<T, U> &m, V k) { auto ret = upper_map(m, k); assert(ret != m.begin()); ret--; while (1) { if ((*ret).second != 0) break; assert(ret != m.begin()); auto next = ret; --next; m.erase(ret); ret = next; } return ret; } template <class T, class U, class V> auto rupper_map(map<T, U> &m, V k) { auto ret = lower_map(m, k); assert(ret != m.begin()); ret--; while (1) { if ((*ret).second != 0) break; assert(ret != m.begin()); auto next = ret; --next; m.erase(ret); ret = next; } return ret; } template <class... T> void fin(T... s) { out(s...); exit(0); } //便利 数学 math // sub ⊂ top bool subset(int sub, int top) { return (sub & top) == sub; } //-180 ~ 180 degree double atand(double h, double w) { return atan2(h, w) / PI * 180; } //% -mの場合、最小の正の数を返す ll mod(ll a, ll m) { if (m < 0) m *= -1; return (a % m + m) % m; } ll pow(ll a) { return a * a; }; ll fact(ll v) { return v <= 1 ? 1 : v * fact(v - 1); } dou factd(int v) { static vd fact(2, 1); if (sz(fact) <= v) { rep(i, sz(fact), v + 1) { fact.push_back(fact.back() * i); } } return fact[v]; } ll comi(ll n, ll r) { assert(n < 100); static vvi(pas, 100, 100); if (pas[0][0]) return pas[n][r]; pas[0][0] = 1; rep(i, 1, 100) { pas[i][0] = 1; rep(j, 1, i + 1) pas[i][j] = pas[i - 1][j - 1] + pas[i - 1][j]; } return pas[n][r]; } double comd2(ll n, ll r) { static vvd(comb, 2020, 2020); if (comb[0][0] == 0) { comb[0][0] = 1; rep(i, 2000) { comb[i + 1][0] = 1; rep(j, 1, i + 2) { comb[i + 1][j] = comb[i][j] + comb[i][j - 1]; } } } return comb[n][r]; } double comd(int n, int r) { if (r < 0 || r > n) return 0; if (n < 2020) return comd2(n, r); static vd fact(2, 1); if (sz(fact) <= n) { rep(i, sz(fact), n + 1) { fact.push_back(fact.back() * i); } } return fact[n] / fact[n - r] / fact[r]; } ll gcd(ll a, ll b) { while (b) a %= b, swap(a, b); return abs(a); } ll gcd(vi b) { ll res = b[0]; rep(i, 1, sz(b)) res = gcd(b[i], res); return res; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll lcm(vi a) { ll res = a[0]; rep(i, 1, sz(a)) res = lcm(a[i], res); return res; } ll ceil(ll a, ll b) { if (b == 0) { debugline("ceil"); deb(a, b); ole(); return -1; } else if (a < 0) { return 0; } else { return (a + b - 1) / b; } } ll sig0(ll t) { return t <= 0 ? 0 : ((1 + t) * t) >> 1; } // ll sig(ll s, ll t) { return ((s + t) * (t - s + 1)) >> 1; } ll sig(ll s, ll t) { if (s > t) swap(s, t); return sig0(t - s) + s * (t - s + 1); } #define tousa_i tosa_i #define lower_tousa_i lower_tosa_i #define upper_tousa upper_tosa #define upper_tousa_i upper_tosa_i ll tosa_i(ll st, ll ad, ll v) { assert(((v - st) % ad) == 0); return (v - st) / ad; } ll tosa_s(ll st, ll ad, ll len) { return st * len + sig0(len - 1) * ad; } // ax + r (x は非負整数) で表せる整数のうち、v 以上となる最小の整数 ll lower_tosa(ll st, ll ad, ll v) { if (st >= v) return st; return (v - st + ad - 1) / ad * ad + st; } //第何項か ll lower_tosa_i(ll st, ll ad, ll v) { if (st >= v) return 0; return (v - st + ad - 1) / ad; } ll upper_tosa(ll st, ll ad, ll v) { return lower_tosa(st, ad, v + 1); } ll upper_tosa_i(ll st, ll ad, ll v) { return lower_tosa_i(st, ad, v + 1); } // v * v >= aとなる最小のvを返す 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); } /*@formatter:off*/ //機能拡張 #define dtie(a, b) \ int a, b; \ tie(a, b) template <class T, class U> string to_string(T a, U b) { string res = ""; res += a; res += b; return res; } template <class T, class U, class V> string to_string(T a, U b, V c) { string res = ""; res += a; res += b; res += c; return res; } template <class T, class U, class V, class W> string to_string(T a, U b, V c, W d) { string res = ""; res += a; res += b; res += c; res += d; return res; } template <class T, class U, class V, class W, class X> string to_string(T a, U b, V c, W d, X e) { string res = ""; res += a; res += b; res += c; res += d; res += e; return res; } constexpr int bsetlen = k5 * 2; // constexpr int bsetlen = 5050; #define bset bitset<bsetlen> bool operator<(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] < b[i]) return true; if (a[i] > b[i]) return false; } return false; } bool operator>(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] > b[i]) return true; if (a[i] < b[i]) return false; } return false; } bool operator<=(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] < b[i]) return true; if (a[i] > b[i]) return false; } return true; } bool operator>=(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] > b[i]) return true; if (a[i] < b[i]) return false; } return true; } string operator~(string &a) { string res = a; for (auto &&c : res) { if (c == '0') c = '1'; else if (c == '1') c = '0'; else { cerr << "cant ~" << a << "must bit" << endl; exit(0); } } return res; } ostream &operator<<(ostream &os, bset &a) { bitset<10> b; vi list; rep(i, bsetlen) { if (a[i]) list.push_back(i), b[i] = 1; } os << b << ", " << list; return os; } int hbiti(bset &a) { rer(i, bsetlen) { if (a[i]) return i; } return -1; } #define hk(a, b, c) (a <= b && b < c) // O(N/64) bset nap(bset &a, int v) { bset r = a | a << v; return r; } bset nap(bset &a, bset &v) { bset r = a; rep(i, bsetlen) { if (v[i]) r |= a << i; } return r; } template <class T> void seth(vector<vector<T>> &S, int w, vector<T> &v) { assert(sz(S) == sz(v)); assert(w < sz(S[0])); rep(h, sz(S)) { S[h][w] = v[h]; } } template <class T, class U> void operator+=(pair<T, U> &a, pair<T, U> &b) { a.fi += b.fi; a.se += b.se; } template <class T, class U> pair<T, U> operator+(pair<T, U> &a, pair<T, U> &b) { return pair<T, U>(a.fi + b.fi, a.se + b.se); } template <typename CharT, typename Traits, typename Alloc> basic_string<CharT, Traits, Alloc> operator+(const basic_string<CharT, Traits, Alloc> &lhs, const int rv) { basic_string<CharT, Traits, Alloc> str(lhs); str.append(to_string(rv)); return str; } template <typename CharT, typename Traits, typename Alloc> void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const int rv) { lhs += to_string(rv); } template <typename CharT, typename Traits, typename Alloc> basic_string<CharT, Traits, Alloc> operator+(const basic_string<CharT, Traits, Alloc> &lhs, const signed rv) { basic_string<CharT, Traits, Alloc> str(lhs); str.append(to_string(rv)); return str; } template <typename CharT, typename Traits, typename Alloc> void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const signed rv) { lhs += to_string(rv); } template <typename CharT, typename Traits, typename Alloc> void operator*=(basic_string<CharT, Traits, Alloc> &s, int num) { auto bek = s; s = ""; for (; num; num >>= 1) { if (num & 1) { s += bek; } bek += bek; } } template <class T, class U> void operator+=(queue<T> &a, U v) { a.push(v); } template <class T, class U> void operator+=(deque<T> &a, U v) { a.push_back(v); } template <class T> priority_queue<T, vector<T>, greater<T>> & operator+=(priority_queue<T, vector<T>, greater<T>> &a, vector<T> &v) { fora(d, v) a.push(d); return a; } template <class T, class U> priority_queue<T, vector<T>, greater<T>> & operator+=(priority_queue<T, vector<T>, greater<T>> &a, U v) { a.push(v); return a; } template <class T, class U> priority_queue<T> &operator+=(priority_queue<T> &a, U v) { a.push(v); return a; } template <class T> set<T> &operator+=(set<T> &a, vector<T> v) { fora(d, v) a.insert(d); return a; } template <class T, class U> auto operator+=(set<T> &a, U v) { return a.insert(v); } template <class T, class U> auto operator-=(set<T> &a, U v) { return a.erase(v); } template <class T, class U> auto operator+=(mset<T> &a, U v) { return a.insert(v); } template <class T, class U> set<T, greater<T>> &operator+=(set<T, greater<T>> &a, U v) { a.insert(v); return a; } template <class T, class U> vector<T> &operator+=(vector<T> &a, U v) { a.push_back(v); return a; } template <class T, class U> vector<T> operator+(vector<T> &a, U v) { vector<T> ret = a; ret += v; return ret; } template <class T, class U> vector<T> operator+(U v, vector<T> &a) { vector<T> ret = a; ret.insert(ret.begin(), v); return ret; } template <class T> vector<T> operator+(vector<T> a, vector<T> b) { vector<T> ret; ret = a; fora(v, b) ret += v; return ret; } template <class T> vector<T> &operator+=(vector<T> &a, vector<T> &b) { rep(i, sz(b)) { /*こうしないとa+=aで両辺が増え続けてバグる*/ a.push_back(b[i]); } return a; } template <class T, class U> map<T, U> &operator+=(map<T, U> &a, map<T, U> &b) { fora(bv, b) { a[bv.first] += bv.second; } return a; } template <class T> vector<T> &operator-=(vector<T> &a, vector<T> &b) { if (sz(a) != sz(b)) { debugline("vector<T> operator-="); deb(a); deb(b); exit(0); } rep(i, sz(a)) a[i] -= b[i]; return a; } template <class T> vector<T> operator-(vector<T> &a, vector<T> &b) { if (sz(a) != sz(b)) { debugline("vector<T> operator-"); deb(a); deb(b); ole(); } vector<T> res(sz(a)); rep(i, sz(a)) res[i] = a[i] - b[i]; return res; } template <class T, class U> void operator*=(vector<T> &a, U b) { vector<T> ta = a; rep(b - 1) { a += ta; } } template <typename T> void erase(vector<T> &v, unsigned ll i) { v.erase(v.begin() + i); } template <typename T> void erase(vector<T> &v, unsigned ll s, unsigned ll e) { v.erase(v.begin() + s, v.begin() + e); } template <class T, class U> void erase(map<T, U> &m, ll okl, ll ngr) { m.erase(m.lower_bound(okl), m.lower_bound(ngr)); } template <class T> void erase(set<T> &m, ll okl, ll ngr) { m.erase(m.lower_bound(okl), m.lower_bound(ngr)); } template <typename T> void erasen(vector<T> &v, unsigned ll s, unsigned ll n) { v.erase(v.begin() + s, v.begin() + s + n); } template <typename T, typename U> void insert(vector<T> &v, unsigned ll i, U t) { v.insert(v.begin() + i, t); } template <typename T, typename U> void push_front(vector<T> &v, U t) { v.insert(v.begin(), t); } template <typename T, typename U> void insert(vector<T> &v, unsigned ll i, vector<T> list) { for (auto &&va : list) v.insert(v.begin() + i++, va); } template <typename T> void insert(set<T> &v, vector<T> list) { for (auto &&va : list) v.insert(va); } vector<string> split(const string a, const char deli) { string b = a + deli; ll l = 0, r = 0, n = b.size(); vector<string> res; rep(i, n) { if (b[i] == deli) { r = i; if (l < r) res.push_back(b.substr(l, r - l)); l = i + 1; } } return res; } vector<string> split(const string a, const string deli) { vector<string> res; ll kn = sz(deli); std::string::size_type Pos(a.find(deli)); ll l = 0; while (Pos != std::string::npos) { if (Pos - l) res.push_back(a.substr(l, Pos - l)); l = Pos + kn; Pos = a.find(deli, Pos + kn); } if (sz(a) - l) res.push_back(a.substr(l, sz(a) - l)); return res; } 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 fAb(bool a) { if (a) cout << "Alice" << endl; else cout << "Bob"; } 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 <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)}; } //未分類 // 0,2,1 1番目と2番目の次元を入れ替える template <class T> auto irekae(vector<vector<vector<T>>> &A, int x, int y, int z) { #define irekae_resize_loop(a, b, c) \ resize(res, a, b, c); \ rep(i, a) rep(j, b) rep(k, c) vector<vector<vector<T>>> res; if (x == 0 && y == 1 && z == 2) { res = A; } else if (x == 0 && y == 2 && z == 1) { irekae_resize_loop(sz(A), sz(A[0][0]), sz(A[0])) { res[i][j][k] = A[i][k][j]; } } else if (x == 1 && y == 0 && z == 2) { irekae_resize_loop(sz(A[0]), sz(A), sz(A[0][0])) { res[i][j][k] = A[j][i][k]; } } else if (x == 1 && y == 2 && z == 0) { irekae_resize_loop(sz(A[0]), sz(A[0][0]), sz(A)) { res[i][j][k] = A[k][i][j]; } } else if (x == 2 && y == 0 && z == 1) { irekae_resize_loop(sz(A[0][0]), sz(A), sz(A[0])) { res[i][j][k] = A[j][k][i]; } } else if (x == 2 && y == 1 && z == 0) { irekae_resize_loop(sz(A[0][0]), sz(A[0]), sz(A)) { res[i][j][k] = A[k][j][i]; } } return res; #undef irekae_resize_loop } template <class T> auto irekae(vector<vector<T>> &A, int i = 1, int j = 0) { vvt(res, sz(A[0]), sz(A)); rep(i, sz(A)) { rep(j, sz(A[0])) { res[j][i] = A[i][j]; } } return res; } // tou分割する template <typename T> vector<vector<T>> cut(vector<T> &a, int tou = 2) { int N = sz(a); vector<vector<T>> res(tou); int hab = N / tou; vi lens(tou, hab); rep(i, N % tou) { lens[tou - 1 - i]++; } int l = 0; rep(i, tou) { int len = lens[i]; int r = l + len; res[i].resize(len); std::copy(a.begin() + l, a.begin() + r, res[i].begin()); l = r; } return res; } //長さn毎に分割する template <typename T> vector<vector<T>> cutn(vector<T> &a, int len) { int N = sz(a); vector<vector<T>> res(ceil(N, len)); vi lens(N / len, len); if (N % len) lens.push_back(N % len); int l = 0; rep(i, sz(lens)) { int len = lens[i]; int r = l + len; res[i].resize(len); std::copy(a.begin() + l, a.begin() + r, res[i].begin()); l = r; } return res; } vi inds_(vi &a) { int n = sz(a); vb was(n); vi res(n); rep(i, n) { assert(!was[a[i]]); res[a[i]] = i; was[a[i]] = true; } return res; } //@起動時 struct initon { initon() { cin.tie(0); ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(16); srand((unsigned)clock() + (unsigned)time(NULL)); }; } initonv; #define pre prev #define nex next // gra mll pr //上下左右 const string udlr = "udlr"; string UDLR = "UDLR"; // x4と連動 UDLR.find('U') := x4[0] //右、上が正 constexpr ll h4[] = {1, -1, 0, 0}; constexpr ll w4[] = {0, 0, -1, 1}; constexpr ll h8[] = {0, 1, 0, -1, -1, 1, 1, -1}; constexpr ll w8[] = {1, 0, -1, 0, 1, -1, 1, -1}; int mei_inc(int h, int w, int H, int W, int i) { while (++i < 4) { if (inside(h + h4[i], w + w4[i], H, W)) return i; } return i; } #define mei(nh, nw, h, w) \ for (int i = mei_inc(h, w, H, W, -1), nh = i < 4 ? h + h4[i] : 0, \ nw = i < 4 ? w + w4[i] : 0; \ i < 4; i = mei_inc(h, w, H, W, i), nh = h + h4[i], nw = w + w4[i]) //グラフ内で #undef getid //#define getidとしているため、ここを書き直したらgraphも書き直す #define getid_2(h, w) (h * W + w) #define getid_1(p) (p.first * W + p.second) #define getid(...) over2(__VA_ARGS__, getid_2, getid_1)(__VA_ARGS__) #define getp(id) mp(id / W, id % W) //#define set_shuffle() std::random_device seed_gen;std::mt19937 //engine(seed_gen()) #define shuffle(a) std::shuffle((a).begin(), (a).end(), //engine); 1980 開始からtime ms経っていたらtrue bool timeup(int time) { auto end_time = system_clock::now(); auto part = duration_cast<milliseconds>(end_time - start_time); auto lim = milliseconds(time); return part >= lim; } void set_time() { past_time = system_clock::now(); } // MS型(millisecqnds)で返る // set_timeをしてからの時間 auto calc_time_milli() { auto now = system_clock::now(); auto part = duration_cast<milliseconds>(now - past_time); return part; } auto calc_time_micro() { auto now = system_clock::now(); auto part = duration_cast<microseconds>(now - past_time); return part; } auto calc_time_nano() { auto now = system_clock::now(); auto part = duration_cast<nanoseconds>(now - past_time); return part; } bool calc_time(int zikan) { return calc_time_micro() >= microseconds(zikan); } using MS = std::chrono::microseconds; int div(microseconds a, microseconds b) { return a / b; } int div(nanoseconds a, nanoseconds b) { if (b < nanoseconds(1)) { return a / nanoseconds(1); } int v = a / b; return v; } // set_time(); // rep(i,lim)shori // lim*=time_nanbai(); // rep(i,lim)shoriと使う //全体でmilliかかっていいときにlimを何倍してもう一回できるかを返す int time_nanbai(int milli) { auto dec = duration_cast<nanoseconds>(past_time - start_time); auto part = calc_time_nano(); auto can_time = nanoseconds(milli * 1000 * 1000); can_time -= part; can_time -= dec; return div(can_time, part); } /*@formatter:on*/ // vectorで取れる要素数 // bool=> 1e9 * 8.32 // int => 1e8 * 2.6 // ll => 1e8 * 1.3 // 3次元以上取るとメモリがヤバい // static配列を使う vvc(ba); ll N, M, H, W; vi A, B, C; //遅い 文字列以外も扱えるはず template <class T = string> struct rolling_hash2 { //@formatter:off const int b[2] = {11731, 11839}; const int m[2] = {1000002571LL, 1000002631LL}; int n; T s; // stringやvector vector<long long> hash[2], pow[2]; rolling_hash2() {} rolling_hash2(T &S) : s(S) { n = (int)S.size(); for (int i = 0; i < 2; ++i) { hash[i].assign(n + 1, 0); pow[i].assign(n + 1, 1); for (int j = 0; j < n; ++j) { hash[i][j + 1] = (hash[i][j] * b[i] + S[j]) % m[i]; pow[i][j + 1] = pow[i][j] * b[i] % m[i]; } } } // get hash of S[left:right] ll get(int l, int r, int id) { ll res = hash[id][r] - hash[id][l] * pow[id][r - l] % m[id]; if (res < 0) res += m[id]; return res; } ll get(int l, int r) { return (get(l, r, 0) << 32) | get(l, r, 1); } ll operator()(int l, int r) { return get(l, r, 0); } ll operator()() { return get(0, n, 0); } // get lcp of S[a:] and S[b:] int lcp(int a, int b) { int len = min((int)s.size() - a, (int)s.size() - b); int low = -1, high = len + 1; while (high - low > 1) { int mid = (low + high) / 2; if (get(a, a + mid, 0) != get(b, b + mid, 0)) high = mid; else if (get(a, a + mid, 1) != get(b, b + mid, 1)) high = mid; else low = mid; } return low; } // get lcp of S[a:] and T[b:] int lcp(rolling_hash2 &t, int a, int b) { int len = min((int)s.size() - a, (int)s.size() - b); int low = -1, high = len + 1; while (high - low > 1) { int mid = (low + high) / 2; if (get(a, a + mid, 0) != get(b, b + mid, 0)) high = mid; else if (get(a, a + mid, 1) != get(b, b + mid, 1)) high = mid; else low = mid; } return low; } }; //早い struct rolling_hash { static constexpr int base1 = 1007, base2 = 2009; static constexpr int mod1 = 1000000007, mod2 = 1000000009; vector<long long> hash1, hash2, power1, power2; int n; rolling_hash(string &S) { n = (int)S.size(); hash1.assign(n + 1, 0); hash2.assign(n + 1, 0); power1.assign(n + 1, 1); power2.assign(n + 1, 1); for (int i = 0; i < n; ++i) { hash1[i + 1] = (hash1[i] * base1 + S[i]) % mod1; hash2[i + 1] = (hash2[i] * base2 + S[i]) % mod2; power1[i + 1] = (power1[i] * base1) % mod1; power2[i + 1] = (power2[i] * base2) % mod2; } } template <class T> rolling_hash(vector<T> &S) { n = (int)S.size(); hash1.assign(n + 1, 0); hash2.assign(n + 1, 0); power1.assign(n + 1, 1); power2.assign(n + 1, 1); for (int i = 0; i < n; ++i) { hash1[i + 1] = (hash1[i] * base1 + S[i]) % mod1; hash2[i + 1] = (hash2[i] * base2 + S[i]) % mod2; power1[i + 1] = (power1[i] * base1) % mod1; power2[i + 1] = (power2[i] * base2) % mod2; } } inline pair<int, int> get(int l, int r) { long long res1 = hash1[r] - hash1[l] * power1[r - l] % mod1; if (res1 < 0) res1 += mod1; long long res2 = hash2[r] - hash2[l] * power2[r - l] % mod2; if (res2 < 0) res2 += mod2; return {res1, res2}; } inline pair<int, int> operator()(int l, int r) { return get(l, r); } inline pair<int, int> operator()() { return get(0, n); } // get lcp of S[a:] and T[b:] //接頭語がどれだけ一致するか //範囲の重複を含む inline int lcp_dup(int a, int b) { int len = min((int)hash1.size() - a, (int)hash1.size() - b); int low = 0, high = len; while (high - low > 1) { int mid = (low + high) >> 1; if (get(a, a + mid) != get(b, b + mid)) high = mid; else low = mid; } return low; } //重複を認めない inline int lcp_not(int a, int b) { return min(lcp_dup(a, b), b - a); } }; // hashを共有したくない場合、中に書く const uint64_t base = rand((uint64_t)868491463, ((uint64_t)1 << 61) - 1 - 1); //安全で爆速なrollinghash class roll { static constexpr uint64_t mask30 = ((uint64_t)1 << 30) - 1; static constexpr uint64_t mask31 = ((uint64_t)1 << 31) - 1; static constexpr uint64_t mask61 = ((uint64_t)1 << 61) - 1; static constexpr uint64_t offset = mask61 * 7; static constexpr uint64_t mod(const uint64_t x) { const uint64_t y = (x >> 61) + (x & mask61); return y >= mask61 ? y - mask61 : y; } static constexpr uint64_t mul(const uint64_t x, const uint64_t y) { const uint64_t xh = x >> 31, xl = x & mask31; const uint64_t yh = y >> 31, yl = y & mask31; const uint64_t z = xl * yh + xh * yl; const uint64_t zh = z >> 30, zl = z & mask30; return xh * yh * 2 + zh + (zl << 31) + xl * yl; } const std::size_t siz; vector<uint64_t> pow, h; public: template <typename T> roll(T &A) : siz(sz(A)), pow(siz + 1, 1), h(siz + 1, 0) { for (std::size_t i = 1; i <= siz; i++) { pow[i] = mod(mul(pow[i - 1], base)), h[i] = mod(mul(h[i - 1], base) + A[i - 1]); } } uint64_t operator()(const std::size_t l, const std::size_t r) { return mod(h[r] + offset - mul(h[l], pow[r - l])); } uint64_t operator()(int r = inf) { return operator()(0, min(siz, r)); } // get lcp of S[a:] and T[b:] //接頭語がどれだけ一致するか //範囲の重複を含む int lcp_dup(int a, int b) { int len = min((int)siz - a, (int)siz - b); int low = 0, high = len; while (high - low > 1) { int mid = (low + high) >> 1; if (operator()(a, a + mid) != operator()(b, b + mid)) high = mid; else low = mid; } return low; } //重複を認めない int lcp_not(int a, int b) { return min(lcp_dup(a, b), b - a); } //@formatter:on }; //動かない場合roll2 // roll ro(list); void solve() { cin >> N; vi A(N), B(N), A2(N), B2(N); rep(i, 0, N) cin >> A[i]; rep(i, 0, N) cin >> B[i]; rep(i, 0, N) A2[i] = A[i] ^ A[(i + 1) % N]; rep(i, 0, N) B2[i] = B[i] ^ B[(i + 1) % N]; vector<int> v; rep(i, 0, N) v.push_back(A2[i]); rep(i, 0, N) v.push_back(A2[i]); rep(i, 0, N) v.push_back(B2[i]); roll rh(v); rep(k, 0, N) if (rh(k, k + N) == rh(N * 2, N * 2 + N)) { int x = A[k] ^ B[0]; out(k, x); } } auto my(ll n, vi &a) { return 0; } auto sister(ll n, vi &a) { ll ret = 0; return ret; } signed main() { solve(); #define arg n, a #ifdef _DEBUG bool bad = 0; for (ll i = 0, ok = 1; i < k5 && ok; ++i) { ll n = rand(1, 8); vi a = ranv(n, 1, 10); auto myres = my(arg); auto res = sister(arg); ok = myres == res; if (!ok) { out(arg); cerr << "AC : " << res << endl; cerr << "MY : " << myres << endl; bad = 1; break; } } if (!bad) { // cout << "完璧 : solveを書き直そう" << endl; // cout << " : そして、solve()を呼び出すのだ" << endl; // cout << " : cin>>n; na(a,n);も忘れるな" << endl; } #endif return 0; };
[ "expression.operation.binary.remove" ]
744,038
744,040
u986437843
cpp
p02816
//#undef _DEBUG //#pragma GCC optimize("Ofast") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace std::chrono; #define int long long // todo 消したら動かない intの代わりにsignedを使う #define ll long long auto start_time = system_clock::now(); auto past_time = system_clock::now(); #define debugName(VariableName) #VariableName /*@formatter:off*/ //[-n, n)にアクセスできる //また、外部関数resizeに渡せる // sizeは[-n, n)でnを返す template <class T> class mvec { vector<T> v; int n; public: mvec() : n(0), v(0) {} mvec(int n) : n(n), v(n * 2) {} mvec(int n, T val) : n(n), v(n * 2, val) {} auto &operator[](int i) { return v[i + n]; } auto size() { return n; } void resize(int sn) { assert(n == 0); n = sn; v.resize(sn * 2); } auto begin() { return v.begin(); } auto rbegin() { return v.rbegin(); } auto end() { return v.end(); } auto rend() { return v.rend(); } }; //[]でboolは参照を返さないため特殊化が必要 template <> struct mvec<bool> { vector<bool> v; int n; mvec() : n(0), v(0) {} mvec(int n) : n(n), v(n * 2) {} mvec(int n, bool val) : n(n), v(n * 2, val) {} auto operator[](int i) { return v[i + n]; } auto size() { return v.size(); } void resize(int sn) { assert(n == 0); n = sn; v.resize(sn * 2); } auto begin() { return v.begin(); } auto rbegin() { return v.rbegin(); } auto end() { return v.end(); } auto rend() { return v.rend(); } }; template <class T> ostream &operator<<(ostream &os, mvec<T> &a) { int spa = 3; for (auto &&v : a) { spa = max(spa, (int)(to_string(v).size()) + 1); } int n = (int)a.size(); os << endl; for (int i = -n; i < n; i++) { int need = spa - ((int)to_string(i).size()); if (i == -n) { need -= min(need, spa - ((int)to_string(a[i]).size())); } while (need--) { os << " "; } os << i; } os << endl; int i = -n; for (auto &&v : a) { int need = spa - ((int)to_string(v).size()); if (i == -n) { need -= min(need, spa - ((int)to_string(i).size())); } while (need--) { os << " "; } os << v; i++; } return os; } #define mv mvec #define MV mvec using mvi = mvec<ll>; using mvb = mvec<bool>; using mvs = mvec<string>; using mvd = mvec<double>; using mvc = mvec<char>; #define o_mvvt(o1, o2, o3, o4, name, ...) name #define mvvt0(t) mvec<mvec<t>> #define mvvt1(t, a) mvec<mvec<t>> a #define mvvt2(t, a, b) mvec<mvec<t>> a(b) #define mvvt3(t, a, b, c) mvec<mvec<t>> a(b, mvec<t>(c)) #define mvvt4(t, a, b, c, d) mvec<mvec<t>> a(b, mvec<t>(c, d)) #define mvvi(...) \ o_mvvt(__VA_ARGS__, mvvt4, mvvt3, mvvt2, mvvt1, mvvt0)(ll, __VA_ARGS__) template <typename T> mvec<T> make_mv(size_t a) { return mvec<T>(a); } template <typename T, typename... Ts> auto make_mv(size_t a, Ts... ts) { return mvec<decltype(make_mv<T>(ts...))>(a, make_mv<T>(ts...)); } #define mvni(name, ...) auto name = make_mv<ll>(__VA_ARGS__) #ifdef _DEBUG // https://marycore.jp/prog/cpp/class-extension-methods/ 違うかも template <class T, class A = std::allocator<T>> struct debtor : std::vector<T, A> { using std::vector<T, A>::vector; template <class U> int deb_v(U a, int v) { return v; } template <class U> int deb_v(debtor<U> &a, int v = 0) { cerr << a.size() << " "; return deb_v(a.at(0), v + 1); } template <class U> void deb_o(U a) { cerr << a << " "; } template <class U> void deb_o(debtor<U> &a) { for (int i = 0; i < min((int)a.size(), 15ll); i++) { deb_o(a[i]); } if ((int)a.size() > 15) { cerr << "..."; } cerr << endl; } typename std::vector<T>::reference operator[](typename std::vector<T>::size_type n) { if (n < 0 || n >= (int)this->size()) { int siz = (int)this->size(); cerr << "vector size = "; int dim = deb_v((*this)); cerr << endl; cerr << "out index at " << n << endl; cerr << endl; if (dim <= 2) { deb_o((*this)); } exit(0); } return this->at(n); } }; #define vector debtor #endif #ifdef _DEBUG //区間削除は出来ない template <class T> struct my_pbds_tree { set<T> s; auto begin() { return s.begin(); } auto end() { return s.end(); } auto rbegin() { return s.rbegin(); } auto rend() { return s.rend(); } auto empty() { return s.empty(); } auto size() { return s.size(); } void clear() { s.clear(); } template <class U> void insert(U v) { s.insert(v); } template <class U> void operator+=(U v) { insert(v); } template <class F> auto erase(F v) { return s.erase(v); } template <class U> auto find(U v) { return s.find(v); } template <class U> auto lower_bound(U v) { return s.lower_bound(v); } template <class U> auto upper_bound(U v) { return s.upper_bound(v); } auto find_by_order(ll k) { auto it = s.begin(); for (ll i = 0; i < k; i++) it++; return it; } auto order_of_key(ll v) { auto it = s.begin(); ll i = 0; for (; it != s.end() && *it < v; i++) it++; return i; } }; #define pbds(T) my_pbds_tree<T> // gp_hash_tableでcountを使えないようにするため template <class T, class U> struct my_unordered_map { unordered_map<T, U> m; my_unordered_map(){}; auto begin() { return m.begin(); } auto end() { return m.end(); } auto cbegin() { return m.cbegin(); } auto cend() { return m.cend(); } template <class V> auto erase(V v) { return m.erase(v); } void clear() { m.clear(); } /*countは gp_hash_tableに存在しない*/ /*!= m.end()*/ template <class V> auto find(V v) { return m.find(v); } template <class V> auto &operator[](V n) { return m[n]; } }; #define unordered_map my_unordered_map #define umapi unordered_map<ll, ll> #define umapp unordered_map<P, ll> #define umapip unordered_map<ll, P> #else // umapはunorderd_mapになる // umapiはgp_hash_table // find_by_order(k) k番目のイテレーター // order_of_key(k) k以上が前から何番目か #define pbds(U) \ __gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag, \ __gnu_pbds::tree_order_statistics_node_update> #define umapi __gnu_pbds::gp_hash_table<ll, ll, xorshift> #define umapp __gnu_pbds::gp_hash_table<P, ll, xorshift> #define umapip __gnu_pbds::gp_hash_table<ll, P, xorshift> #endif /*@formatter:on*/ struct xorshift { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } size_t operator()(std::pair<ll, ll> x) const { ll v = ((x.first) << 32) | x.second; static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(v + FIXED_RANDOM); } }; /*@formatter:off*/ template <class U, class L> void operator+=( __gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update> &s, L v) { s.insert(v); } //衝突対策 #define ws ws_ template <class A, class B, class C> struct T2 { A f; B s; C t; T2() { f = 0, s = 0, t = 0; } T2(A f, B s, C t) : f(f), s(s), t(t) {} bool operator<(const T2 &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 ?n s > r.s : t > r.t; 大きい順 */ } bool operator>(const T2 &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 T2 &r) const { return f == r.f && s == r.s && t == r.t; } bool operator!=(const T2 &r) const { return f != r.f || s != r.s || t != r.t; } }; template <class A, class B, class C, class D> struct F2 { A a; B b; C c; D d; F2() { a = 0, b = 0, c = 0, d = 0; } F2(A a, B b, C c, D d) : a(a), b(b), c(c), d(d) {} bool operator<(const F2 &r) const { return a != r.a ? a < r.a : b != r.b ? b < r.b : c != r.c ? c < r.c : d < r.d; /* return a != r.a ? a > r.a : b != r.b ? b > r.b : c != r.c ? c > r.c : d > r.d;*/ } bool operator>(const F2 &r) const { return a != r.a ? a > r.a : b != r.b ? b > r.b : c != r.c ? c > r.c : d > r.d; /* return a != r.a ? a < r.a : b != r.b ? b < r.b : c != r.c ? c < r.c : d < r.d;*/ } bool operator==(const F2 &r) const { return a == r.a && b == r.b && c == r.c && d == r.d; } bool operator!=(const F2 &r) const { return a != r.a || b != r.b || c != r.c || d != r.d; } ll operator[](ll i) { assert(i < 4); return i == 0 ? a : i == 1 ? b : i == 2 ? c : d; } }; typedef T2<ll, ll, ll> T; typedef F2<ll, ll, ll, ll> F; T mt(ll a, ll b, ll c) { return T(a, b, c); } F mf(ll a, ll b, ll c, ll d) { return F(a, b, c, d); } //@マクロ省略系 型,構造 #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>; using mvp = mvec<P>; using mvt = mvec<T>; #define MIN(a) numeric_limits<a>::min() #define MAX(a) numeric_limits<a>::max() #define fi first #define se second #define beg begin #define rbeg rbegin #define con continue #define bre break #define brk break #define is == #define el else #define elf else if #define upd update #define sstream stringstream #define maxq 1 #define minq -1 #define ZERO(a) memset(a, 0, sizeof(a)) #define MINUS(a) memset(a, 0xff, sizeof(a)) #define MALLOC(type, len) (type *)malloc((len) * sizeof(type)) #define lam(right) [&](auto &p) { return p right; } //マクロ省略系 コンテナ 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 V vector #define o_vvt(o1, o2, o3, o4, name, ...) name #define vvt0(t) vector<vector<t>> #define vvt1(t, a) vector<vector<t>> a #define vvt2(t, a, b) vector<vector<t>> a(b) #define vvt3(t, a, b, c) vector<vector<t>> a(b, vector<t>(c)) #define vvt4(t, a, b, c, d) vector<vector<t>> a(b, vector<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__) #define vv(type, ...) \ o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(type, __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 vn(type, name, ...) auto name = make_v<type>(__VA_ARGS__) #define PQ priority_queue<ll, vector<ll>, greater<ll>> #define tos to_string using mapi = map<ll, ll>; using mapp = map<P, ll>; using mapd = map<dou, ll>; using mapc = map<char, ll>; using maps = map<str, ll>; using seti = set<ll>; using setd = set<dou>; using setc = set<char>; using sets = set<str>; using qui = queue<ll>; #define uset unordered_set #define useti unordered_set<ll, xorshift> #define mset multiset #define mseti multiset<ll> #define umap unordered_map #define mmap multimap //任意のマクロサポート用 使う度に初期化する int index_, v1_, v2_, v3_; template <class T> struct pq { priority_queue<T, vector<T>, greater<T>> q; /*小さい順*/ T su = 0; void clear() { q = priority_queue<T, vector<T>, greater<T>>(); su = 0; } void operator+=(T v) { su += v; q.push(v); } T sum() { return su; } T top() { return q.top(); } void pop() { su -= q.top(); q.pop(); } T poll() { T ret = q.top(); su -= ret; q.pop(); return ret; } ll size() { return q.size(); } }; template <class T> struct pqg { priority_queue<T> q; /*大きい順*/ T su = 0; void clear() { q = priority_queue<T>(); su = 0; } void operator+=(T v) { su += v; q.push(v); } T sum() { return su; } T top() { return q.top(); } void pop() { su -= q.top(); q.pop(); } T poll() { T ret = q.top(); su -= ret; q.pop(); return ret; } ll size() { return q.size(); } }; #define pqi pq<ll> #define pqgi pqg<ll> //マクロ 繰り返し //↓@オーバーロード隔離 #define o_rep(o1, o2, o3, o4, name, ...) name #define rep1(n) for (ll rep1i = 0, rep1lim = n; rep1i < rep1lim; ++rep1i) #define rep2(i, n) for (ll i = 0, rep2lim = n; i < rep2lim; ++i) #define rep3(i, m, n) for (ll i = m, rep3lim = n; i < rep3lim; ++i) #define rep4(i, m, n, ad) for (ll i = m, rep4lim = n; i < rep4lim; i += ad) //逆順 閉区間 #define rer2(i, n) for (ll i = n; i >= 0; i--) #define rer3(i, m, n) for (ll i = m, rer3lim = n; i >= rer3lim; i--) #define rer4(i, m, n, dec) for (ll i = m, rer4lim = n; i >= rer4lim; i -= dec) //ループを一つにまとめないとフォーマットで汚くなるため #define nex_ind1(i) i++ #define nex_ind2(i, j, J) \ i = (j + 1 == J) ? i + 1 : i, j = (j + 1 == J ? 0 : j + 1) #define nex_ind3(i, j, k, J, K) \ i = (j + 1 == J && k + 1 == K) ? i + 1 : i, \ j = (k + 1 == K) ? (j + 1 == J ? 0 : j + 1) : j, \ k = (k + 1 == K ? 0 : k + 1) #define nex_ind4(i, j, k, l, J, K, L) \ i = (j + 1 == J && k + 1 == K && l + 1 == L) ? i + 1 : i, \ j = (k + 1 == K && l + 1 == L) ? (j + 1 == J ? 0 : j + 1) : j, \ k = (l + 1 == L ? (k + 1 == K ? 0 : k + 1) : k), l = l + 1 == L ? 0 : l + 1 #define nex_ind5(i, j, k, l, m, J, K, L, M) \ i = (j + 1 == J && k + 1 == K && l + 1 == L && m + 1 == M) ? i + 1 : i, \ j = (k + 1 == K && l + 1 == L && m + 1 == M) ? (j + 1 == J ? 0 : j + 1) : j, \ k = (l + 1 == L && m + 1 == M ? (k + 1 == K ? 0 : k + 1) : k), \ l = m + 1 == M ? l + 1 == L ? 0 : l + 1 : l, m = m + 1 == M ? 0 : m + 1 #define repss1(i, I) for (int i = 0; i < I; i++) #define repss2(i, j, I, J) \ for (int i = (J ? 0 : I), j = 0; i < I; nex_ind2(i, j, J)) #define repss3(i, j, k, I, J, K) \ for (int i = (J && K ? 0 : I), j = 0, k = 0; i < I; nex_ind3(i, j, k, J, K)) #define repss4(i, j, k, l, I, J, K, L) \ for (int i = (J && K && L ? 0 : I), j = 0, k = 0, l = 0; i < I; \ nex_ind4(i, j, k, l, J, K, L)) #define repss5(i, j, k, l, m, I, J, K, L, M) \ for (int i = (J && K && L && M ? 0 : I), j = 0, k = 0, l = 0, m = 0; i < I; \ nex_ind5(i, j, k, l, m, J, K, L, M)) #define o_repss(o1, o2, o3, o4, o5, o6, o7, o8, o9, o10, name, ...) name // i,j,k...をnまで見る #define reps1(i, n) repss1(i, n) #define reps2(i, j, n) repss2(i, j, n, n) #define reps3(i, j, k, n) repss3(i, j, k, n, n, n) #define reps4(i, j, k, l, n) repss4(i, j, k, l, n, n, n, n) #define o_reps(o1, o2, o3, o4, o5, name, ...) name template <class T> void nex_repv2(int &i, int &j, int &I, int &J, vector<vector<T>> &s) { while (1) { j++; if (j >= J) { j = 0; i++; if (i < I) { J = (int)s[i].size(); } } if (i >= I || J) return; } } template <class T> void nex_repv3(int &i, int &j, int &k, int &I, int &J, int &K, vector<vector<vector<T>>> &s) { while (1) { k++; if (k >= K) { k = 0; j++; if (j >= J) { j = 0; i++; if (i >= I) return; } } J = (int)s[i].size(); K = (int)s[i][j].size(); if (J && K) return; } } #define repv_1(i, a) repss1(i, sz(a)) //正方形である必要はない //直前を持つのとどっちが早いか #define repv_2(i, j, a) \ for (int I = (int)a.size(), J = (int)a[0].size(), i = 0, j = 0; i < I; \ nex_repv2(i, j, I, J, a)) //箱状になっている事が要求される つまり[i] 次元目の要素数は一定 #define repv_3(i, j, k, a) \ for (int I = (int)a.size(), J = (int)a[0].size(), K = (int)a[0][0].size(), \ i = 0, j = 0, k = 0; \ i < I; nex_repv3(i, j, k, I, J, K, a)) #define repv_4(i, j, k, l, a) \ repss4(i, j, k, l, sz(a), sz(a[0]), sz(a[0][0]), sz(a[0][0][0])) #define repv_5(i, j, k, l, m, a) \ repss5(i, j, k, l, m, sz(a), sz(a[0]), sz(a[0][0]), sz(a[0][0][0]), \ sz(a[0][0][0][0])) #define o_repv(o1, o2, o3, o4, o5, o6, name, ...) name template <typename T> struct has_rbegin_rend { private: template <typename U> static auto check(U &&obj) -> decltype(std::rbegin(obj), std::rend(obj), std::true_type{}); static std::false_type check(...); public: static constexpr bool value = decltype(check(std::declval<T>()))::value; }; template <typename T> constexpr bool has_rbegin_rend_v = has_rbegin_rend<T>::value; template <typename Iterator> class Range { public: Range(Iterator &&begin, Iterator &&end) noexcept : m_begin(std::forward<Iterator>(begin)), m_end(std::forward<Iterator>(end)) {} Iterator begin() const noexcept { return m_begin; } Iterator end() const noexcept { return m_end; } private: const Iterator m_begin; const Iterator m_end; }; template <typename Iterator> static inline Range<Iterator> makeRange(Iterator &&begin, Iterator &&end) noexcept { return Range<Iterator>{std::forward<Iterator>(begin), std::forward<Iterator>(end)}; } template <typename T> static inline decltype(auto) makeReversedRange(const std::initializer_list<T> &iniList) noexcept { return makeRange(std::rbegin(iniList), std::rend(iniList)); } template <typename T, typename std::enable_if_t<has_rbegin_rend_v<T>, std::nullptr_t> = nullptr> static inline decltype(auto) makeReversedRange(T &&c) noexcept { return makeRange(std::rbegin(c), std::rend(c)); } /* rbegin(), rend()を持たないものはこっちに分岐させて,エラーメッセージを少なくする*/ template <typename T, typename std::enable_if<!has_rbegin_rend<T>::value, std::nullptr_t>::type = nullptr> static inline void makeReversedRange(T &&) noexcept { static_assert(has_rbegin_rend<T>::value, "Specified argument doesn't have reverse iterator."); } #define form1(st) \ for (auto &&form_it = st.begin(); form_it != st.end(); ++form_it) #define form3(k, v, st) \ for (auto &&form_it = st.begin(); form_it != st.end(); ++form_it) #define form4(k, v, st, r) \ for (auto &&form_it = st.begin(); form_it != st.end() && (*form_it).fi < r; \ ++form_it) #define form5(k, v, st, l, r) \ for (auto &&form_it = st.lower_bound(l); \ form_it != st.end() && (*form_it).fi < r; ++form_it) #define o_form(a, b, c, d, e, name, ...) name #define fors1(st) \ for (auto &&fors_it = st.begin(); fors_it != st.end(); ++fors_it) #define fors2(v, st) \ for (auto &&fors_it = st.begin(); fors_it != st.end(); ++fors_it) #define fors3(v, st, r) \ for (auto &&fors_it = st.begin(); fors_it != st.end() && (*fors_it) < r; \ ++fors_it) #define fors4(v, st, l, r) \ for (auto &&fors_it = st.lower_bound(l); \ fors_it != st.end() && (*fors_it) < r; ++fors_it) #define o_fors(a, b, c, d, e, name, ...) name #define forslr1(st, a, b) \ for (auto &&forslr_it = st.begin(); forslr_it != st.end(); ++forslr_it) #define forslr2(v, st, a, b) \ for (auto &&forslr_it = st.begin(); forslr_it != st.end(); ++forslr_it) #define forslr3(v, st, r, a, b) \ for (auto &&forslr_it = st.begin(); \ forslr_it != st.end() && (*forslr_it) < r; ++forslr_it) #define forslr4(v, st, l, r, a, b) \ for (auto &&forslr_it = st.lower_bound(l); \ forslr_it != st.end() && (*forslr_it) < r; ++forslr_it) #define o_forslr(a, b, c, d, e, f, g, name, ...) name template <class U> vector<U> to1d(vector<U> &a) { return a; } template <class U> vector<U> to1d(V<V<U>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) res.push_back(a2); return res; } template <class U> vector<U> to1d(V<V<V<U>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) res.push_back(a3); return res; } template <class U> vector<U> to1d(V<V<V<V<U>>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) for (auto &&a4 : a3) res.push_back(a4); return res; } template <class U> vector<U> to1d(V<V<V<V<V<U>>>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) for (auto &&a4 : a3) for (auto &&a5 : a4) res.push_back(a5); return res; } template <class U> vector<U> to1d(V<V<V<V<V<V<U>>>>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) for (auto &&a4 : a3) for (auto &&a5 : a4) for (auto &&a6 : a5) res.push_back(a6); return res; } #define fora_init_2(a, A) ; #define fora_init_4(a, b, A, B) \ auto &&a = A[fora_i]; \ auto &&b = B[fora_i]; #define fora_init_6(a, b, c, A, B, C) \ auto &&a = A[fora_i]; \ auto &&b = B[fora_i]; \ auto &&c = C[fora_i]; #define fora_init_8(a, b, c, d, A, B, C, D) \ auto &&a = A[fora_i]; \ auto &&b = B[fora_i]; \ auto &&c = C[fora_i]; \ auto &&d = D[fora_i]; #define o_fora_init(a, b, c, d, e, f, g, h, name, ...) name #define fora_init(...) \ o_fora_init(__VA_ARGS__, fora_init_8, a, fora_init_6, a, fora_init_4, a, \ fora_init_2)(__VA_ARGS__) #define forr_init_2(a, A) auto &&a = A[forr_i]; #define forr_init_4(a, b, A, B) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; #define forr_init_6(a, b, c, A, B, C) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; \ auto &&c = C[forr_i]; #define forr_init_8(a, b, c, d, A, B, C, D) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; \ auto &&c = C[forr_i]; \ auto &&d = D[forr_i]; #define o_forr_init(a, b, c, d, e, f, g, h, name, ...) name #define forr_init(...) \ o_forr_init(__VA_ARGS__, forr_init_8, a, forr_init_6, a, forr_init_4, a, \ forr_init_2)(__VA_ARGS__) #define forp_init(k, v, ...) \ auto &&k = (*forp_it).fi; \ auto &&v = (*forp_it).se; #define form_init(k, v, ...) \ auto &&k = (*form_it).fi; \ auto &&v = (*form_it).se; #define fors_init(v, ...) auto &&v = (*fors_it); #define forlr_init(a, A, ngl, ngr) \ auto a = A[forlr_i]; \ auto prev = forlr_i ? A[forlr_i - 1] : ngl; \ auto next = forlr_i + 1 < rep2lim ? A[forlr_i + 1] : ngr; #define forslr_init4(a, A, ngl, ngr) \ auto a = (*forslr_it); \ auto prev = (forslr_it != A.begin()) ? (*std::prev(forslr_it)) : ngl; \ auto next = (forslr_it != std::prev(A.end())) ? (*std::next(forslr_it)) : ngr; #define forslr_init5(a, A, r, ngl, ngr) \ auto a = (*forslr_it); \ auto prev = (forslr_it != A.begin()) ? (*std::prev(forslr_it)) : ngl; \ auto next = (forslr_it != std::prev(A.end())) ? (*std::next(forslr_it)) : ngr; #define forslr_init6(a, A, l, r, ngl, ngr) \ auto a = (*forslr_it); \ auto prev = (forslr_it != A.begin()) ? (*std::prev(forslr_it)) : ngl; \ auto next = (forslr_it != std::prev(A.end())) ? (*std::next(forslr_it)) : ngr; #define o_forslr_init(a, b, c, d, e, f, name, ...) name #define forslr_init(...) \ o_forslr_init(__VA_ARGS__, forslr_init6, forslr_init5, \ forslr_init4)(__VA_ARGS__); #define fora_2(a, A) for (auto &&a : A) #define fora_4(a, b, A, B) rep(fora_i, sz(A)) #define fora_6(a, b, c, A, B, C) rep(fora_i, sz(A)) #define fora_8(a, b, c, d, A, B, C, D) rep(fora_i, sz(A)) #define o_fora(a, b, c, d, e, f, g, h, name, ...) name #define forr_2(a, A) rer(forr_i, sz(A) - 1) #define forr_4(a, b, A, B) rer(forr_i, sz(A) - 1) #define forr_6(a, b, c, A, B, C) rer(forr_i, sz(A) - 1) #define forr_8(a, b, c, d, A, B, C, D) rer(forr_i, sz(A) - 1) #define o_forr(a, b, c, d, e, f, g, h, name, ...) name //↑@オーバーロード隔離 // rep系はインデックス、for系は中身 #define rep(...) o_rep(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__) #define rer(...) o_rep(__VA_ARGS__, rer4, rer3, rer2, )(__VA_ARGS__) // char用のrep #define repc(i, m, n) for (char i = m, repc3lim = n; i < repc3lim; ++i) // i,j,k...をnまで見る #define reps(...) o_reps(__VA_ARGS__, reps4, reps3, reps2, reps1, )(__VA_ARGS__) #define repss(...) \ o_repss(__VA_ARGS__, repss5, a, repss4, a, repss3, a, repss2, a, \ repss1)(__VA_ARGS__) // vectorのindexを走査する // repv(i,j,vvi) #define repv(...) \ o_repv(__VA_ARGS__, repv_5, repv_4, repv_3, repv_2, repv_1, )(__VA_ARGS__) #define rerv(i, A) for (int i = sz(A) - 1; i >= 0; i--) // repvn(dp) nは次元 #define repv1(a) repv(i, a) #define repv2(a) repv(i, j, a) #define repv3(a) repv(i, j, k, a) #define repv4(a) repv(i, j, k, l, a) #define fora(...) \ o_fora(__VA_ARGS__, fora_8, a, fora_6, a, fora_4, a, fora_2)(__VA_ARGS__) #define forr(...) \ o_forr(__VA_ARGS__, forr_8, a, forr_6, a, forr_4, a, forr_2)(__VA_ARGS__) //#define forr(v, a) for(auto&& v : makeReversedRange(a)) //参照を取らない #define forv(a, b) for (auto a : to1d(b)) //インデックスを前後含めて走査 #define ring(i, s, len) \ for (int i = s, prev = (s == 0) ? len - 1 : s - 1, \ next = (s == len - 1) ? 0 : s + 1, cou = 0; \ cou < len; \ cou++, prev = i, i = next, next = (next == len - 1) ? 0 : next + 1) //値と前後を見る #define ringv(v, d) \ index_ = 0; \ for (auto prev = d[sz(d) - 1], next = (int)d.size() > 1 ? d[1] : d[0], \ v = d[0]; \ index_ < sz(d); index_++, prev = v, v = next, \ next = (index_ >= sz(d) - 1 ? d[0] : d[index_ + 1])) // 左右をnext prevで見る 0の左と nの右 #define forlr(v, d, banpei_l, banpei_r) rep(forlr_i, sz(d)) #define form(...) \ o_form(__VA_ARGS__, form5, form4, form3, form2, form1)(__VA_ARGS__) #define fors(...) \ o_fors(__VA_ARGS__, fors5, fors4, fors3, fors2, fors1)(__VA_ARGS__) #define forslr(...) \ o_forslr(__VA_ARGS__, forslr5, forslr4, forslr3, forslr2, \ forslr1)(__VA_ARGS__) #define forp(k, v, st) \ for (auto &&forp_it = st.begin(); forp_it != st.end(); ++forp_it) //マクロ 定数 #define k3 1010 #define k4 10101 #define k5 101010 #define k6 1010101 #define k7 10101010 const ll inf = (ll)1e9 + 100; const ll linf = (ll)1e18 + 100; const char infc = '{'; const string infs = "{"; const double eps = 1e-9; const double PI = 3.1415926535897932384626433832795029L; //マクロ省略形 関数等 #define arsz(a) (sizeof(a) / sizeof(a[0])) #define sz(a) ((ll)(a).size()) #define mp make_pair #define pb pop_back #define pf push_front #define eb emplace_back #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() constexpr bool ev(ll a) { return !(a & 1); } constexpr bool od(ll a) { return (a & 1); } //@拡張系 こう出来るべきというもの //埋め込み 存在を意識せずに機能を増やされているもの namespace std { template <> class hash<std::pair<signed, signed>> { public: size_t operator()(const std::pair<signed, signed> &x) const { return hash<ll>()(((ll)x.first << 32) | x.second); } }; template <> class hash<std::pair<ll, ll>> { public : /*大きいllが渡されると、<<32でオーバーフローするがとりあえず問題ないと判断*/ size_t operator()(const std::pair<ll, ll> &x) const { return hash<ll>()(((ll)x.first << 32) | x.second); } }; } // namespace std // stream まとめ /*@formatter:on*/ istream &operator>>(istream &iss, P &a) { iss >> a.first >> a.second; return iss; } template <typename T> istream &operator>>(istream &iss, vector<T> &vec) { for (T &x : vec) iss >> x; return iss; } template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> p) { os << p.fi << " " << p.se; return os; } ostream &operator<<(ostream &os, T p) { os << p.f << " " << p.s << " " << p.t; return os; } ostream &operator<<(ostream &os, F p) { os << p.a << " " << p.b << " " << p.c << " " << p.d; return os; } template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) { for (ll i = 0; i < vec.size(); ++i) os << vec[i] << (i + 1 == vec.size() ? "" : " "); return os; } template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) { for (ll i = 0; i < vec.size(); ++i) { for (ll j = 0; j < vec[i].size(); ++j) { os << vec[i][j] << " "; } os << endl; } return os; } template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &m) { for (auto &&v : m) os << v; return os; } template <class T> ostream &operator<<(ostream &os, set<T> s) { fora(v, s) { os << v << " "; } return os; } template <class T> ostream &operator<<(ostream &os, deque<T> a) { fora(v, a) os << v << " "; return os; } ostream &operator<<(ostream &os, vector<vector<char>> &vec) { rep(h, sz(vec)) { rep(w, sz(vec[0])) { os << vec[h][w]; } os << endl; } return os; } // template<class T,class U>ostream &operator<<(ostream &os, vector<pair<T,U>>& // a) {fora(v,a)os<<v<<endl;return os;} /*@formatter:off*/ template <typename W, typename H> void resize(W &vec, const H head) { vec.resize(head); } template <typename W, typename H, typename... T> void resize(W &vec, const H &head, const T... tail) { vec.resize(head); for (auto &v : vec) resize(v, tail...); } // 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) template <class T, class U> void replace(vector<T> &a, T key, U v) { replace(a.begin(), a.end(), key, v); } void replace(str &a, char key, str v) { if (v == "") a.erase(remove(all(a), key), a.end()); } void replace(str &a, char key, char v) { replace(all(a), key, v); } // keyと同じかどうか01で置き換える template <class T, class U> void replace(vector<T> &a, U k) { rep(i, sz(a)) a[i] = a[i] == k; } template <class T, class U> void replace(vector<vector<T>> &a, U k) { rep(i, sz(a)) rep(j, sz(a[0])) a[i][j] = a[i][j] == k; } template <class T> void replace(T &a) { replace(a, '#'); } void replace(str &a, str key, str v) { stringstream t; ll kn = sz(key); std::string::size_type Pos(a.find(key)); ll l = 0; while (Pos != std::string::npos) { t << a.substr(l, Pos - l); t << v; l = Pos + kn; Pos = a.find(key, Pos + kn); } t << a.substr(l, sz(a) - l); a = t.str(); } template <class T> bool includes(vector<T> &a, vector<T> &b) { vi c = a; vi d = b; sort(all(c)); sort(all(d)); return includes(all(c), all(d)); } template <class T> bool is_permutation(vector<T> &a, vector<T> &b) { return is_permutation(all(a), all(b)); } template <class T> bool next_permutation(vector<T> &a) { return next_permutation(all(a)); } void iota(vector<ll> &ve, ll s, ll n) { ve.resize(n); iota(all(ve), s); } vi iota(ll s, ll len) { vi ve(len); iota(all(ve), s); return ve; } template <class A, class B> auto vtop(vector<A> &a, vector<B> &b) { assert(sz(a) == sz(b)); /*stringを0で初期化できない */ vector<pair<A, B>> res; rep(i, sz(a)) res.eb(a[i], b[i]); return res; } template <class A, class B> void ptov(vector<pair<A, B>> &p, vector<A> &a, vector<B> &b) { a.resize(sz(p)), b.resize(sz(p)); rep(i, sz(p)) a[i] = p[i].fi, b[i] = p[i].se; } template <class A, class B, class C> auto vtot(vector<A> &a, vector<B> &b, vector<C> &c) { assert(sz(a) == sz(b) && sz(b) == sz(c)); vector<T2<A, B, C>> res; rep(i, sz(a)) res.eb(a[i], b[i], c[i]); return res; } template <class A, class B, class C, class D> auto vtof(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { assert(sz(a) == sz(b) && sz(b) == sz(c) && sz(c) == sz(d)); vector<F2<A, B, C, D>> res; rep(i, sz(a)) res.eb(a[i], b[i], c[i], d[i]); return res; } enum pcomparator { fisi, fisd, fdsi, fdsd, sifi, sifd, sdfi, sdfd }; enum tcomparator { fisiti, fisitd, fisdti, fisdtd, fdsiti, fdsitd, fdsdti, fdsdtd, fitisi, fitisd, fitdsi, fitdsd, fdtisi, fdtisd, fdtdsi, fdtdsd, sifiti, sifitd, sifdti, sifdtd, sdfiti, sdfitd, sdfdti, sdfdtd, sitifi, sitifd, sitdfi, sitdfd, sdtifi, sdtifd, sdtdfi, sdfdfd, tifisi, tifisd, tifdsi, tifdsd, tdfisi, tdfisd, tdfdsi, tdfdsd, tisifi, tisifd, tisdfi, tisdfd, tdsifi, tdsifd, tdsdfi, tdsdfd }; template <class A, class B> void sort(vector<pair<A, B>> &a, pcomparator type) { typedef pair<A, B> U; if (type == fisi) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se < r.se; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se > r.se; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se < r.se; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se > r.se; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi < r.fi; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi > r.fi; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi < r.fi; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi > r.fi; }); }; template <class U> void sort(vector<U> &a, pcomparator type) { if (type == fisi) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f > r.f; }); }; template <class A, class B, class C, class D> void sort(vector<F2<A, B, C, D>> &a, pcomparator type) { typedef F2<A, B, C, D> U; if (type == fisi) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a > r.a; }); }; template <class U> void sort(vector<U> &a, tcomparator type) { if (type == 0) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t < r.t; }); else if (type == 1) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t > r.t; }); else if (type == 2) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t < r.t; }); else if (type == 3) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t > r.t; }); else if (type == 4) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t < r.t; }); else if (type == 5) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t > r.t; }); else if (type == 6) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t < r.t; }); else if (type == 7) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t > r.t; }); else if (type == 8) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s < r.s; }); else if (type == 9) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s > r.s; }); else if (type == 10) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s < r.s; }); else if (type == 11) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s > r.s; }); else if (type == 12) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s < r.s; }); else if (type == 13) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s > r.s; }); else if (type == 14) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s < r.s; }); else if (type == 15) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s > r.s; }); else if (type == 16) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t < r.t; }); else if (type == 17) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t > r.t; }); else if (type == 18) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t < r.t; }); else if (type == 19) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t > r.t; }); else if (type == 20) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t < r.t; }); else if (type == 21) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t > r.t; }); else if (type == 22) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t < r.t; }); else if (type == 23) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t > r.t; }); else if (type == 24) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f < r.f; }); else if (type == 25) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f > r.f; }); else if (type == 26) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f < r.f; }); else if (type == 27) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f > r.f; }); else if (type == 28) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f < r.f; }); else if (type == 29) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f > r.f; }); else if (type == 30) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f < r.f; }); else if (type == 31) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f > r.f; }); else if (type == 32) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == 33) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == 34) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == 35) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == 36) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == 37) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == 38) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == 39) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == 40) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == 41) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == 42) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == 43) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f > r.f; }); else if (type == 44) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == 45) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == 46) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == 47) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f > r.f; }); } template <class A, class B, class C, class D> void sort(vector<F2<A, B, C, D>> &a, tcomparator type) { typedef F2<A, B, C, D> U; if (type == 0) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c < r.c; }); else if (type == 1) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c > r.c; }); else if (type == 2) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c < r.c; }); else if (type == 3) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c > r.c; }); else if (type == 4) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c < r.c; }); else if (type == 5) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c > r.c; }); else if (type == 6) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c < r.c; }); else if (type == 7) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c > r.c; }); else if (type == 8) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b < r.b; }); else if (type == 9) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b > r.b; }); else if (type == 10) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b < r.b; }); else if (type == 11) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b > r.b; }); else if (type == 12) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b < r.b; }); else if (type == 13) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b > r.b; }); else if (type == 14) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b < r.b; }); else if (type == 15) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b > r.b; }); else if (type == 16) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c < r.c; }); else if (type == 17) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c > r.c; }); else if (type == 18) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c < r.c; }); else if (type == 19) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c > r.c; }); else if (type == 20) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c < r.c; }); else if (type == 21) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c > r.c; }); else if (type == 22) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c < r.c; }); else if (type == 23) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c > r.c; }); else if (type == 24) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a < r.a; }); else if (type == 25) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a > r.a; }); else if (type == 26) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a < r.a; }); else if (type == 27) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a > r.a; }); else if (type == 28) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a < r.a; }); else if (type == 29) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a > r.a; }); else if (type == 30) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a < r.a; }); else if (type == 31) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a > r.a; }); else if (type == 32) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == 33) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == 34) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == 35) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == 36) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == 37) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == 38) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == 39) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == 40) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == 41) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == 42) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == 43) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a > r.a; }); else if (type == 44) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == 45) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == 46) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == 47) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a > r.a; }); } void sort(string &a) { sort(all(a)); } template <class T> void sort(vector<T> &a) { sort(all(a)); } // P l, P rで f(P) の形で渡す template <class U, class F> void sort(vector<U> &a, F f) { sort(all(a), [&](U l, U r) { return f(l) < f(r); }); }; template <class T> void rsort(vector<T> &a) { sort(all(a), greater<T>()); }; template <class U, class F> void rsort(vector<U> &a, F f) { sort(all(a), [&](U l, U r) { return f(l) > f(r); }); }; // F = T<T> //例えばreturn p.fi + p.se; template <class A, class B> void sortp(vector<A> &a, vector<B> &b) { auto c = vtop(a, b); sort(c); rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se; } template <class A, class B, class F> void sortp(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); sort(c, f); rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se; } template <class A, class B> void rsortp(vector<A> &a, vector<B> &b) { auto c = vtop(a, b); rsort(c); rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second; } template <class A, class B, class F> void rsortp(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); rsort(c, f); rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second; } template <class A, class B, class C> void sortt(vector<A> &a, vector<B> &b, vector<C> &c) { auto d = vtot(a, b, c); sort(d); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class F> void sortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); sort(d, f); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C> void rsortt(vector<A> &a, vector<B> &b, vector<C> &c) { auto d = vtot(a, b, c); rsort(d); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class F> void rsortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); rsort(d, f); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class D> void sortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { auto e = vtof(a, b, c, d); sort(e); rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d; } template <class A, class B, class C, class D> void rsortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { auto e = vtof(a, b, c, d); rsort(e); rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d; } // sortindex 元のvectorはソートしない template <class T> vi sorti(vector<T> &a) { auto b = a; vi ind = iota(0, sz(a)); sortp(b, ind); return ind; } /*indexの分で型が変わるためpcomparatorが必要*/ template <class T> vi sorti(vector<T> &a, pcomparator f) { auto b = a; vi ind = iota(0, sz(a)); sortp(b, ind, f); return ind; } template <class T, class F> vi sorti(vector<T> &a, F f) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(a[x]) < f(a[y]); }); return ind; } template <class T> vi rsorti(vector<T> &a) { auto b = a; vi ind = iota(0, sz(a)); rsortp(b, ind); return ind; } template <class T, class F> vi rsorti(vector<T> &a, F f) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(a[x]) > f(a[y]); }); return ind; } template <class A, class B, class F> vi sortpi(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(c[x]) < f(c[y]); }); return ind; } template <class A, class B> vi sortpi(vector<A> &a, vector<B> &b, pcomparator f) { vi ind = iota(0, sz(a)); auto c = a; auto d = b; sortt(c, d, ind, f); return ind; } template <class A, class B> vi sortpi(vector<A> &a, vector<B> &b) { return sortpi(a, b, fisi); }; template <class A, class B, class F> vi rsortpi(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(c[x]) > f(c[y]); }); return ind; } template <class A, class B> vi rsortpi(vector<A> &a, vector<B> &b) { return sortpi(a, b, fdsd); }; template <class A, class B, class C, class F> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(d[x]) < f(d[y]); }); return ind; } template <class A, class B, class C> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, pcomparator f) { vi ind = iota(0, sz(a)); auto d = vtof(a, b, c, ind); sort(d, f); rep(i, sz(a)) ind[i] = d[i].d; return ind; } template <class A, class B, class C> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { if (a[x] == a[y]) { if (b[x] == b[y]) return c[x] < c[y]; else return b[x] < b[y]; } else { return a[x] < a[y]; } }); return ind; } template <class A, class B, class C, class F> vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(d[x]) > f(d[y]); }); return ind; } template <class A, class B, class C> vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { if (a[x] == a[y]) { if (b[x] == b[y]) return c[x] > c[y]; else return b[x] > b[y]; } else { return a[x] > a[y]; } }); return ind; } template <class T> void sort2(vector<vector<T>> &a) { for (ll i = 0, n = a.size(); i < n; ++i) sort(a[i]); } template <class T> void rsort2(vector<vector<T>> &a) { for (ll 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 W, typename T> void fill(W &xx, const T vall) { xx = vall; } template <typename W, typename T> void fill(vector<W> &vecc, const T vall) { for (auto &&vx : vecc) fill(vx, vall); } template <typename W, typename T> void fill(vector<W> &xx, ll len, const T v) { rep(i, len) xx[i] = v; } template <typename W, typename T> void fill(vector<vector<W>> &xx, int sh, int th, int sw, int tw, T v) { rep(h, sh, th) rep(w, sw, tw) xx[h][w] = v; } template <class T, class U> void fill(vector<T> &a, vi &ind, U val) { fora(v, ind) a[v] = val; } template <class W, class T> void fill(mvec<W> &xx, const T v) { fora(x, xx) fill(x, v); } template <typename A, size_t N> A sum(A (&a)[N]) { A res = 0; rep(i, N) res += a[i]; return res; } template <typename A, size_t N, size_t O> A sum(A (&a)[N][O]) { A res = 0; rep(i, N) rep(j, O) res += a[i][j]; return res; } template <typename A, size_t N, size_t O, size_t P> A sum(A (&a)[N][O][P]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) res += a[i][j][k]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A sum(A (&a)[N][O][P][Q]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) res += a[i][j][k][l]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A sum(A (&a)[N][O][P][Q][R]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) res += a[i][j][k][l][m]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A sum(A (&a)[N][O][P][Q][R][S]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) rep(n, S) res += a[i][j][k][l][m][n]; return res; } //@汎用便利関数 入力 ll in() { ll ret; cin >> ret; return ret; } string sin() { string ret; cin >> ret; return ret; } template <class T> void in(T &head) { cin >> head; } template <class T, class... U> void in(T &head, U &...tail) { cin >> head; in(tail...); } #define o_din(o1, o2, o3, o4, o5, o6, name, ...) name #define din1(a) \ ll a; \ cin >> a #define din2(a, b) \ ll a, b; \ cin >> a >> b #define din3(a, b, c) \ ll a, b, c; \ cin >> a >> b >> c #define din4(a, b, c, d) \ ll a, b, c, d; \ cin >> a >> b >> c >> d #define din5(a, b, c, d, e) \ ll a, b, c, d, e; \ cin >> a >> b >> c >> d >> e #define din6(a, b, c, d, e, f) \ ll a, b, c, d, e, f; \ cin >> a >> b >> c >> d >> e >> f #define din(...) \ o_din(__VA_ARGS__, din6, din5, din4, din3, din2, din1)(__VA_ARGS__) #define o_dins(o1, o2, o3, o4, o5, o6, name, ...) name #define dins1(a) \ str a; \ cin >> a #define dins2(a, b) \ str a, b; \ cin >> a >> b #define dins3(a, b, c) \ str a, b, c; \ cin >> a >> b >> c #define dins4(a, b, c, d) \ str a, b, c, d; \ cin >> a >> b >> c >> d #define dins5(a, b, c, d, e) \ str a, b, c, d, e; \ cin >> a >> b >> c >> d >> e #define dins6(a, b, c, d, e, f) \ str a, b, c, d, e, f; \ cin >> a >> b >> c >> d >> e >> f #define dins(...) \ o_dins(__VA_ARGS__, dins6, dins5, dins4, dins3, dins2, dins1)(__VA_ARGS__) #define o_dind(o1, o2, o3, o4, name, ...) name #define din1d(a) \ din1(a); \ a-- #define din2d(a, b) \ din2(a, b); \ a--, b-- #define din3d(a, b, c) \ din3(a, b, c); \ a--, b--, c-- #define din4d(a, b, c, d) \ din4(a, b, c, d); \ a--, b--, c--, d-- #define dind(...) o_dind(__VA_ARGS__, din4d, din3d, din2d, din1d)(__VA_ARGS__) template <class T> void out2(T &&head) { cout << head; } template <class T, class... U> void out2(T &&head, U &&...tail) { cout << head << " "; out2(tail...); } template <class T, class... U> void out(T &&head, U &&...tail) { cout << head << " "; out2(tail...); cout << "" << endl; } template <class T> void out(T &&head) { cout << head << endl; } void out() { cout << "" << endl; } #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; } /*@formatter:on*/ 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]; if (typeid(a[i]) == typeid(P)) { ss << endl; } else { ss << " "; } } return ss.str(); } /*@formatter:off*/ 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(); } 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 = 12; H = min(H, sz(a)); rep(i, H) { ss << endl; ss << out_m2(a[i], W, U, i); } return ss.str(); } template <class T, size_t N> string out_m2(T (&a)[N]) { vector<T> b; resize(b, N); rep(i, N) { b[i] = a[i]; } return out_m2(b); } template <class T, size_t N, size_t M> string out_m2(T (&a)[N][M]) { vector<vector<T>> b; resize(b, N, M); rep(i, N) { rep(j, M) { b[i][j] = a[i][j]; } } return out_m2(b); } template <class T, size_t N, size_t M, size_t O> string out_m2(T (&a)[N][M][O]) { vector<vector<vector<T>>> b; resize(b, N, M, O); rep(i, N) { rep(j, M) { rep(k, O) { b[i][j][k] = a[i][j][k]; } } } return out_m2(b); } string out_m2(int a) { stringstream ss; ss << a; return ss.str(); } /*@formatter:on*/ template <class T> string out_m2(mvec<mvec<T>> &a, ll H = inf, ll W = inf, int key = inf) { H = min({H, sz(a), 6ll}); W = min({W, sz(a[0]), 6ll}); stringstream ss; ss << endl; // if (key == inf)ss << " *|"; else ss << " " << key << "|"; if (key == inf) ss << " *|"; else { ss << std::right << std::setw(2) << key; ss << "|"; } rep(w, -W, W) ss << std::right << std::setw(4) << w; ss << "" << endl; rep(w, W * 8 + 3) ss << "_"; ss << "" << endl; rep(h, -H, H) { ss << std::right << std::setw(2) << h << "|"; int NW = min(sz(a[h]), 6ll); rep(w, -NW, NW) { if (abs(a[h][w]) == linf) ss << " e" << ""; else ss << std::right << std::setw(4) << a[h][w]; } ss << "" << endl; } return ss.str(); } /*@formatter:on*/ template <class T> string out_m2(mvec<mvec<mvec<T>>> &a, ll H = inf, ll W = inf, ll U = inf) { stringstream ss; if (H == inf) H = 6; H = min(H, sz(a)); rep(i, -H, H) { ss << endl; ss << out_m2(a[i], W, U, i); } return ss.str(); } /*@formatter:off*/ template <class T> string out_m2(T &a) { stringstream ss; ss << a; return ss.str(); } /*@formatter:on*/ 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(); } /*@formatter:off*/ 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(); } 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 template <class T> void outl(vector<T> &a, int n = inf) { rep(i, min(n, sz(a))) cout << a[i] << endl; } //テーブルをスペースなしで出力 template <class T> void outt(vector<vector<T>> &a) { rep(i, sz(a)) { rep(j, sz(a[i])) { cout << a[i][j]; } cout << endl; } } // int型をbit表記で出力 void outb(int a) { cout << bitset<20>(a) << endl; } template <class T> void na(vector<T> &a, ll n) { a.resize(n); rep(i, n) cin >> a[i]; } #define dna(a, n) \ vi a(n); \ rep(dnai, n) cin >> a[dnai]; #define dnad(a, n) \ vi a(n); \ rep(dnai, n) cin >> a[dnai], a[dnai]--; template <class T> void nao(vector<T> &a, ll n) { a.resize(n + 1); a[0] = 0; rep(i, n) cin >> a[i + 1]; } template <class T> void naod(vector<T> &a, ll n) { a.resize(n + 1); a[0] = 0; rep(i, n) cin >> a[i + 1], a[i + 1]--; } template <class T> void nad(vector<T> &a, ll n) { a.resize(n); rep(i, n) cin >> a[i], a[i]--; } template <class T, class U> void na2(vector<T> &a, vector<U> &b, ll n) { a.resize(n); b.resize(n); rep(i, n) cin >> a[i] >> b[i]; } #define dna2(a, b, n) \ vi a(n), b(n); \ rep(dna2i, n) cin >> a[dna2i] >> b[dna2i]; template <class T, class U> void nao2(vector<T> &a, vector<U> &b, ll n) { a.resize(n + 1); b.resize(n + 1); a[0] = b[0] = 0; rep(i, n) cin >> a[i + 1] >> b[i + 1]; } #define dna2d(a, b, n) \ vi a(n), b(n); \ rep(dna2di, n) { \ cin >> a[dna2di] >> b[dna2di]; \ a[dna2di]--, b[dna2di]--; \ } template <class T, class U> void na2d(vector<T> &a, vector<U> &b, ll n) { a.resize(n); b.resize(n); rep(i, n) cin >> a[i] >> b[i], a[i]--, b[i]--; } template <class T, class U, class W> void na3(vector<T> &a, vector<U> &b, vector<W> &c, ll n) { a.resize(n); b.resize(n); c.resize(n); rep(i, n) cin >> a[i] >> b[i] >> c[i]; } #define dna3(a, b, c, n) \ vi a(n), b(n), c(n); \ rep(dna3i, n) cin >> a[dna3i] >> b[dna3i] >> c[dna3i]; template <class T, class U, class W> void na3d(vector<T> &a, vector<U> &b, vector<W> &c, ll 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 dna3d(a, b, c, n) \ vi a(n), b(n), c(n); \ rep(dna3di, n) { \ cin >> a[dna3di] >> b[dna3di] >> c[dna3di]; \ a[dna3di]--, b[dna3di]--, c[dna3di]--; \ } template <class T, class U, class W, class X> void na4(vector<T> &a, vector<U> &b, vector<W> &c, vector<X> &d, ll n) { a.resize(n); b.resize(n); c.resize(n); d.resize(n); rep(i, n) cin >> a[i] >> b[i] >> c[i] >> d[i]; } #define dna4(a, b, c, d, n) \ vi a(n), b(n), c(n), d(n); \ rep(dna4i, n) cin >> a[dna4i] >> b[dna4i] >> c[dna4i] >> d[dna4i]; #define dna4d(a, b, c, d, n) \ vi a(n), b(n), c(n), d(n); \ rep(dna4i, n) cin >> a[dna4i] >> b[dna4i] >> c[dna4i] >> d[dna4i], \ --a[dna4i], --b[dna4i], --c[dna4i], --d[dna4i]; #define nt(a, h, w) \ resize(a, h, w); \ rep(nthi, h) rep(ntwi, w) cin >> a[nthi][ntwi]; #define ntd(a, h, w) \ resize(a, h, w); \ rep(ntdhi, h) rep(ntdwi, w) cin >> a[ntdhi][ntdwi], a[ntdhi][ntdwi]--; #define ntp(a, h, w) \ resize(a, h + 2, w + 2); \ fill(a, '#'); \ rep(ntphi, 1, h + 1) rep(ntpwi, 1, w + 1) cin >> a[ntphi][ntpwi]; //デバッグ #define sp << " " << #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' //@formatter:off //よく使うクラス、構造体 // graphでredefineしているため、書き換えたら向こうも書き換える struct unionfind { vector<ll> par; vector<ll> siz; vector<ll> es; ll n, trees; //連結グループの数(親の種類) unionfind(ll n) : n(n), trees(n) { par.resize(n); siz.resize(n); es.resize(n); for (ll i = 0; i < n; i++) { par[i] = i; siz[i] = 1; } } ll root(ll x) { if (par[x] == x) { return x; } else { return par[x] = root(par[x]); } } ll operator()(ll x) { return root(x); } bool unite(ll x, ll y) { x = root(x); y = root(y); es[x]++; if (x == y) return false; if (siz[x] > siz[y]) swap(x, y); trees--; par[x] = y; siz[y] += siz[x]; es[y] += es[x]; return true; } bool same(ll x, ll y) { return root(x) == root(y); } ll size(ll x) { return siz[root(x)]; } ll esize(ll x) { return es[root(x)]; } vi sizes() { vi cou(n); vi ret; ret.reserve(n); rep(i, n) { cou[root(i)]++; } rep(i, n) { if (cou[i]) ret.push_back(cou[i]); } return ret; } //つながりを無向グラフと見なし、xが閉路に含まれるか判定 bool close(ll x) { return esize(x) >= size(x); } vector<vi> sets() { vi ind(n, -1); ll i = 0; vvi(res, trees); rep(j, n) { ll r = root(j); if (ind[r] == -1) ind[r] = i++; res[ind[r]].push_back(j); } rep(i, trees) { ll r = root(res[i][0]); if (res[i][0] == r) continue; rep(j, 1, sz(res[i])) { if (res[i][j] == r) { swap(res[i][0], res[i][j]); break; } } } return res; } }; //@formatter:off using bll = __int128; using u32 = unsigned; using u64 = unsigned long long; using u128 = __uint128_t; std::ostream &operator<<(std::ostream &dest, __int128_t value) { std::ostream::sentry s(dest); if (s) { __uint128_t tmp = value < 0 ? -value : value; char buffer[128]; char *d = std::end(buffer); do { --d; *d = "0123456789"[tmp % 10]; tmp /= 10; } while (tmp != 0); if (value < 0) { --d; *d = '-'; } ll len = std::end(buffer) - d; if (dest.rdbuf()->sputn(d, len) != len) { dest.setstate(std::ios_base::badbit); } } return dest; } //__int128 toi128(string &s) { __int128 ret = 0; for (ll i = 0; i < //s.length(); ++i) if ('0' <= s[i] && s[i] <= '9') ret = 10 * //ret + s[i] - '0'; return ret;} //エラー 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 re() { assert(0 == 1); exit(0); } void tle() { while (inf) cout << inf << endl; } //便利関数 //テスト用 #define rand xor128_ unsigned long xor128_(void) { static unsigned long x = 123456789, y = 362436069, z = 521288629, w = 88675123; unsigned long t; t = (x ^ (x << 11)); x = y; y = z; z = w; return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8))); } char ranc() { return (char)('a' + rand() % 26); } ll rand(ll min, ll max) { assert(min <= max); if (min >= 0 && max >= 0) { return rand() % (max + 1 - min) + min; } else if (max < 0) { return -rand(-max, -min); } else { if (rand() % 2) { return rand(0, max); } else { return -rand(0, -min); } } } vi ranv(ll n, ll min, ll max) { vi v(n); rep(i, n) v[i] = rand(min, max); return v; } str ransu(ll n) { str s; rep(i, n) s += (char)rand('A', 'Z'); return s; } str ransl(ll n) { str s; rep(i, n) s += (char)rand('a', 'z'); return s; } //単調増加 vi ranvinc(ll n, ll min, ll max) { vi v(n); bool bad = 1; while (bad) { bad = 0; v.resize(n); rep(i, n) { if (i && min > max - v[i - 1]) { bad = 1; break; } if (i) v[i] = v[i - 1] + rand(min, max - v[i - 1]); else v[i] = rand(min, max); } } return v; } //便利 汎用 void ranvlr(ll n, ll min, ll max, vi &l, vi &r) { l.resize(n); r.resize(n); rep(i, n) { l[i] = rand(min, max); r[i] = l[i] + rand(0, max - l[i]); } } template <class T> vector<pair<T, int>> run_length(vector<T> &a) { vector<pair<T, int>> 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 <class F> ll mgr(ll ok, ll ng, F f) { bool han = true; if (ok < ng) while (ng - ok > 1) { ll mid = (ok + ng) >> 1; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; deb(mid, han); } else while (ok - ng > 1) { ll mid = (ok + ng) >> 1; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; deb(mid, han); } return ok; } template <class F> dou mgrd(dou ok, dou ng, F f) { bool han = true; if (ok < ng) rep(i, 100) { dou mid = (ok + ng) / 2; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; if (i < 30) deb(mid, han); } else rep(i, 100) { dou mid = (ok + ng) / 2; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; if (i < 30) deb(mid, han); } return ok; } // strを整数として比較 string smax(str &a, str b) { if (sz(a) < sz(b)) { return b; } else if (sz(a) > sz(b)) { return a; } else if (a < b) return b; else return a; } // strを整数として比較 string smin(str &a, str b) { if (sz(a) > sz(b)) { return b; } else if (sz(a) < sz(b)) { return a; } else if (a > b) return b; else return a; } //エラー-1 template <typename W, typename T> ll find(vector<W> &a, int l, const T key) { rep(i, l, sz(a)) if (a[i] == key) return i; return -1; } template <typename W, typename T> ll find(vector<W> &a, const T key) { rep(i, sz(a)) if (a[i] == key) return i; return -1; } template <typename W, typename T> P find(vector<vector<W>> &a, const T key) { rep(i, sz(a)) rep(j, sz(a[0])) if (a[i][j] == key) return mp(i, j); return mp(-1, -1); } template <typename W, typename U> T find(vector<vector<vector<W>>> &a, const U key) { rep(i, sz(a)) rep(j, sz(a[0])) rep(k, sz(a[0][0])) if (a[i][j][k] == key) return mt(i, j, k); return mt(-1, -1, -1); } // stringも書く int find(string &s, const string key) { int klen = sz(key); rep(i, sz(s) - klen + 1) { if (s[i] != key[0]) continue; if (s.substr(i, klen) == key) { return i; } } return -1; } int find(string &s, int l, const string key) { int klen = sz(key); rep(i, l, sz(s) - klen + 1) { if (s[i] != key[0]) continue; if (s.substr(i, klen) == key) { return i; } } return -1; } int find(string &s, const char key) { rep(i, sz(s)) { if (s[i] == key) return i; } return -1; } int find(string &s, int l, const char key) { rep(i, l, sz(s)) { if (s[i] == key) return i; } return -1; } template <typename W, typename T> ll count2(W &a, const T k) { return a == k; } template <typename W, typename T> ll count2(vector<W> &a, const T k) { ll ret = 0; fora(v, a) ret += count2(v, k); return ret; } template <typename W, typename T> ll count(vector<W> &a, const T k) { ll ret = 0; fora(v, a) ret += count2(v, k); return ret; } vi count(vi &a) { int ma = 0; fora(v, a) { if (ma < v) ma = v; } vi res(ma + 1); fora(v, a) { res[v]++; } return res; } ll count(str &a, str k) { ll ret = 0, len = k.length(); auto pos = a.find(k); while (pos != string::npos) pos = a.find(k, pos + len), ++ret; return ret; } vi count(str &a) { vi cou(26); char c = 'a'; if ('A' <= a[0] && a[0] <= 'Z') c = 'A'; rep(i, sz(a))++ cou[a[i] - c]; return cou; } #define couif count_if // algorythm 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)); } template <class U> void rev(vector<vector<U>> &a) { vector<vector<U>> b(sz(a[0]), vector<U>(sz(a))); rep(h, sz(a)) rep(w, sz(a[0])) b[w][h] = a[h][w]; a = b; } void rev(string &a) { reverse(all(a)); } constexpr ll p10[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000ll, 100000000000ll, 1000000000000ll, 10000000000000ll, 100000000000000ll, 1000000000000000ll, 10000000000000000ll, 100000000000000000ll, 1000000000000000000ll}; ll get(ll a, ll keta) { return (a / (ll)pow(10, keta)) % 10; } // 0は0桁 ll keta(ll v) { if (v < p10[9]) { if (v < p10[4]) { if (v < p10[2]) { if (v < p10[1]) { if (v < p10[0]) return 0; else return 1; } else return 2; } else { if (v < p10[3]) return 3; else return 4; } } else { if (v < p10[7]) { if (v < p10[5]) return 5; else if (v < p10[6]) return 6; else return 7; } else { if (v < p10[8]) return 8; else return 9; } } } else { if (v < p10[13]) { if (v < p10[11]) { if (v < p10[10]) return 10; else return 11; } else { if (v < p10[12]) return 12; else return 13; } } else { if (v < p10[15]) { if (v < p10[14]) return 14; else return 15; } else { if (v < p10[17]) { if (v < p10[16]) return 16; else return 17; } else { if (v < p10[18]) return 18; else return 19; } } } } } ll dsum(ll v, ll sin = 10) { ll ret = 0; for (; v; v /= sin) ret += v % sin; return ret; } ll mask10(ll v) { return p10[v] - 1; } //変換系 //[v] := iとなるようなvectorを返す //存在しない物は-1 template <class T> auto keys(T &a) { vector<decltype((a.begin())->fi)> res; for (auto &&k : a) res.push_back(k.fi); return res; } template <class T> auto values(T &a) { vector<decltype((a.begin())->se)> res; for (auto &&k : a) res.push_back(k.se); return res; } template <class T, class U> bool chma(T &a, const U &b) { if (a < b) { a = b; return true; } return false; } template <class T, class U> bool chmi(T &a, const U &b) { if (b < a) { a = b; return true; } return false; } template <class T> T min(T a, signed b) { return a < b ? a : b; } template <class T> T max(T a, signed b) { return a < b ? b : a; } template <class T> T min(T a, T b, T c) { return a >= b ? b >= c ? c : b : a >= c ? c : a; } template <class T> T max(T a, T b, T c) { return a <= b ? b <= c ? c : b : a <= c ? c : a; } template <class T> T min(vector<T> &a) { return *min_element(all(a)); } template <class T> T mini(vector<T> &a) { return min_element(all(a)) - a.begin(); } template <class T> T min(vector<T> &a, ll n) { return *min_element(a.begin(), a.begin() + min(n, sz(a))); } template <class T> T min(vector<T> &a, ll s, ll n) { return *min_element(a.begin() + s, a.begin() + min(n, sz(a))); } template <class T> T max(vector<T> &a) { return *max_element(all(a)); } template <class T, class U> T max(vector<T> &a, vector<U> &b) { return max(*max_element(all(a)), *max_element(all(b))); } template <class T> T maxi(vector<T> &a) { return max_element(all(a)) - a.begin(); } template <class T> T max(vector<T> &a, ll n) { return *max_element(a.begin(), a.begin() + min(n, sz(a))); } template <class T> T max(vector<T> &a, ll s, ll n) { return *max_element(a.begin() + s, a.begin() + min(n, sz(a))); } template <typename A, size_t N> A max(A (&a)[N]) { A res = a[0]; rep(i, N) res = max(res, a[i]); return res; } template <typename A, size_t N, size_t O> A max(A (&a)[N][O]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P> A max(A (&a)[N][O][P]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A max(A (&a)[N][O][P][Q], const T &v) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A max(A (&a)[N][O][P][Q][R]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A max(A (&a)[N][O][P][Q][R][S]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N> A min(A (&a)[N]) { A res = a[0]; rep(i, N) res = min(res, a[i]); return res; } template <typename A, size_t N, size_t O> A min(A (&a)[N][O]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P> A min(A (&a)[N][O][P]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A min(A (&a)[N][O][P][Q], const T &v) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A min(A (&a)[N][O][P][Q][R]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A min(A (&a)[N][O][P][Q][R][S]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <class T> T sum(vector<T> &v, ll s, ll t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += v[i]; return ret; } template <class T> T sum(vector<T> &v, ll t = inf) { return sum(v, 0, t); } template <class T> T sum(vector<vector<T>> &v, int s, int t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<T>> &v, int t = inf) { return sum(v, 0, t); } template <class T> T sum(vector<vector<vector<T>>> &v, int s, int t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<T>>> &v, int t = inf) { return sum(v, 0, t); } template <class T> T sum(vector<vector<vector<vector<T>>>> &v, int s, int t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<vector<T>>>> &v, int t = inf) { return sum(v, 0, t); } template <class T> T sum(vector<vector<vector<vector<vector<T>>>>> &v, int s, int t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<vector<vector<T>>>>> &v, int t = inf) { return sum(v, 0, t); } template <class T> auto sum(priority_queue<T, vector<T>, greater<T>> &r) { auto q = r; T ret = 0; while (sz(q)) { ret += q.top(); q.pop(); } return ret; } template <class T> auto sum(priority_queue<T> &r) { auto q = r; T ret = 0; while (sz(q)) { ret += q.top(); q.pop(); } return ret; } // template<class T, class U, class... W> auto sumn(vector<T> &v, U head, W... // tail) { auto ret = sum(v[0], tail...); rep(i, 1, min(sz(v), head))ret += // sum(v[i], tail...); return ret;} vi v_i(vi &a) { int n = max(a) + 1; vi ret(n, -1); rep(i, sz(a)) { ret[a[i]] = i; } return ret; } void clear(PQ &q) { q = PQ(); } void clear(priority_queue<int> &q) { q = priority_queue<int>(); } template <class T> void clear(queue<T> &q) { while (q.size()) q.pop(); } template <class T> T *negarr(ll size) { T *body = (T *)malloc((size * 2 + 1) * sizeof(T)); return body + size; } template <class T> T *negarr2(ll h, ll w) { double **dummy1 = new double *[2 * h + 1]; double *dummy2 = new double[(2 * h + 1) * (2 * w + 1)]; dummy1[0] = dummy2 + w; for (ll i = 1; i <= 2 * h + 1; ++i) { dummy1[i] = dummy1[i - 1] + 2 * w + 1; } double **a = dummy1 + h; return a; } // 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> vector<T> ima(vector<T> &v) { vector<T> ret = v; rep(i, sz(ret) - 1) chma(ret[i + 1], ret[i]); return ret; } // template<class T> vector<T> rimi(vector<T> &v) { vector<T> ret = v; rer(i, // sz(ret) - 1, 1)chmi(ret[i - 1], ret[i]); return ret;} template<class T> // vector<T> rima(vector<T> &v) { vector<T> ret = v; rer(i, sz(ret) - 1, // 1)chma(ret[i - 1], ret[i]); return ret;} template <class T> struct ruiC { vector<T> rui; ruiC(vector<T> &ru) : rui(ru) {} /*先頭0*/ ruiC() : rui(1, 0) {} T operator()(ll l, ll r) { if (l > r) { cerr << "ruic "; deb(l, r); assert(0); } return rui[r] - rui[l]; } T operator()(int r) { return operator()(0, r); } /*ruiv[]をruic[]に変えた際意味が変わるのがまずいため()と統一*/ /*単体iを返す 累積でないことに注意(seg木との統一でこうしている)*/ // T operator[](ll i) { return rui[i + 1] - rui[i]; } T operator[](ll i) { return rui[i]; } /*0から順に追加される必要がある*/ void operator+=(T v) { rui.push_back(rui.back() + v); } void add(int i, T v) { if (sz(rui) - 1 != i) ole(); operator+=(v); } T back() { return rui.back(); } ll size() { return rui.size(); } auto begin() { return rui.begin(); } auto end() { return rui.end(); } }; template <class T> struct ruimax { template <typename Monoid> struct SegmentTree { /*pairで処理*/ int sz; vector<Monoid> seg; const Monoid M1 = mp(MIN(T), -1); Monoid f(Monoid a, Monoid b) { return max(a, b); } void build(vector<T> &a) { int n = sz(a); sz = 1; while (sz < n) sz <<= 1; seg.assign(2 * sz, M1); rep(i, n) { seg[i + sz] = mp(a[i], i); } for (int k = sz - 1; k > 0; k--) { seg[k] = f(seg[k << 1], seg[(k << 1) | 1]); } } Monoid query(int a, int b) { Monoid L = M1, R = M1; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) L = f(L, seg[a++]); if (b & 1) R = f(seg[--b], R); } return f(L, R); } Monoid operator[](const int &k) const { return seg[k + sz]; } }; private: vector<T> ve; SegmentTree<pair<T, int>> seg; vector<T> rv; vector<T> ri; bool build = false; public: int n; ruimax(vector<T> &a) : ve(a), n(sz(a)) { int index = -1; int ma = MIN(T); rv.resize(n + 1); ri.resize(n + 1); rv[0] = -linf; ri[0] = -1; rep(i, n) { if (chma(ma, a[i])) { index = i; } rv[i + 1] = ma; ri[i + 1] = index; } } T operator()(int l, int r) { if (!(l <= r && 0 <= l && r <= n)) { deb(l, r, n); assert(0); } if (l == 0) { return rv[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).first; } } T operator()(int r = inf) { return operator()(0, min(r, n)); } T operator[](int r) { return operator()(0, r); } T getv(int l, int r) { return operator()(l, r); } T geti(int l, int r) { assert(l <= r && 0 <= l && r <= n); if (l == 0) { return ri[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).second; } } T geti(int r = inf) { return geti(0, min(r, n)); }; T getv(int r = inf) { return getv(0, min(r, n)); }; auto begin() { return rv.begin(); } auto end() { return rv.end(); } }; template <class T> struct ruimin { template <typename Monoid> struct SegmentTree { /*pairで処理*/ int sz; vector<Monoid> seg; const Monoid M1 = mp(MAX(T), -1); Monoid f(Monoid a, Monoid b) { return min(a, b); } void build(vector<T> &a) { int n = sz(a); sz = 1; while (sz < n) sz <<= 1; seg.assign(2 * sz, M1); rep(i, n) { seg[i + sz] = mp(a[i], i); } for (int k = sz - 1; k > 0; k--) { seg[k] = f(seg[k << 1], seg[(k << 1) | 1]); } } Monoid query(int a, int b) { Monoid L = M1, R = M1; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) L = f(L, seg[a++]); if (b & 1) R = f(seg[--b], R); } return f(L, R); } Monoid operator[](const int &k) const { return seg[k + sz]; } }; private: vector<T> ve; SegmentTree<pair<T, int>> seg; vector<T> rv; vector<T> ri; bool build = false; int n; public: ruimin(vector<T> &a) : ve(a), n(sz(a)) { int index = -1; int mi = MAX(T); rv.resize(n + 1); ri.resize(n + 1); rv[0] = linf; ri[0] = -1; rep(i, n) { if (chmi(mi, a[i])) { index = i; } rv[i + 1] = mi; ri[i + 1] = index; } } T operator()(int l, int r) { assert(l <= r && 0 <= l && r <= n); if (l == 0) { return rv[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).first; } } T operator()(int r = inf) { return operator()(0, min(r, n)); } T operator[](int r) { return operator()(0, r); } T getv(int l, int r) { return operator()(l, r); } T geti(int l, int r) { assert(l <= r && 0 <= l && r <= n); if (l == 0) { return ri[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).second; } } T geti(int r = inf) { return geti(0, min(r, n)); }; T getv(int r = inf) { return getv(0, min(r, n)); }; auto begin() { return rv.begin(); } auto end() { return rv.end(); } }; 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() { return ruiC<T>(); } template <class T> ruiC<T> ruic(vector<T> &a) { vector<T> ret = ruiv(a); return ruiC<T>(ret); } vvi() ruib(vi &a) { vvi(res, 61, sz(a) + 1); rep(k, 61) { rep(i, sz(a)) { res[k][i + 1] = res[k][i] + ((a[i] >> k) & 1); } } return res; } vector<ruiC<int>> ruibc(vi &a) { vector<ruiC<int>> ret(61); vvi(res, 61, sz(a)); rep(k, 61) { rep(i, sz(a)) { res[k][i] = (a[i] >> k) & 1; } ret[k] = ruic(res[k]); } return 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); } // h query template <class T> vector<T> imoh(vector<vector<T>> &v, int w) { vector<T> ret(sz(v)); rep(h, sz(ret)) { ret[h] = v[h][w]; } rep(i, sz(ret) - 1) { ret[i + 1] += ret[i]; } return ret; } template <class T> vector<T> ruih(vector<vector<T>> &v, int w) { vector<T> ret(sz(v) + 1); rep(h, sz(v)) { ret[h + 1] = v[h][w]; } rep(i, sz(v)) { ret[i + 1] += ret[i]; } return ret; } template <class T> ruiC<T> ruihc(vector<vector<T>> &a, int w) { vector<T> ret = ruih(a, w); return ruiC<T>(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> struct rruiC { vector<T> rui; int n; rruiC(vector<T> &a) : n(sz(a)) { rui.resize(n + 1); rer(i, n - 1) { rui[i] = rui[i + 1] + a[i]; } } /*[r l)*/ T operator()(int r, int l) { r++; l++; assert(l <= r && l >= 0 && r <= n); return rui[l] - rui[r]; } T operator()(int l) { return operator()(n - 1, l); } T operator[](int i) { return operator()(i); } }; template <class T> ostream &operator<<(ostream &os, rruiC<T> a) { fora(v, a.rui) os << v << " "; return os; } #define rrui rruic template <class T> rruiC<T> rruic(vector<T> &a) { return rruiC<T>(a); } //掛け算 template <class T> struct ruimulC { vector<T> rv; int n; ruimulC(vector<T> &a) : rv(a), n(sz(a)) { rv.resize(n + 1); rv[0] = 1; rep(i, n) { rv[i + 1] = a[i] * rv[i]; } } ruimulC() : n(0) { rv.resize(n + 1); rv[0] = 1; } void operator+=(T v) { rv.push_back(rv.back() * v); n++; } T operator()(int l, int r) { assert(l <= r && 0 <= l && r <= n); return rv[r] / rv[l]; } T operator()(int r = inf) { return operator()(0, min(r, n)); } T operator[](int r) { return operator()(0, r); } auto begin() { return rv.begin(); } auto end() { return rv.end(); } }; template <class T> ruimulC<T> ruimul(vector<T> &a) { return ruimulC<T>(a); } template <class T> ruimulC<T> ruimul() { vector<T> a; return ruimulC<T>(a); } /*@formatter:off*/ 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> void inc(pair<T, U> &a, U v = 1) { a.first += v, a.second += v; } template <class T, class U> void inc(T &a, U v = 1) { a += v; } template <class T, class U> void inc(vector<T> &a, U v = 1) { for (auto &u : a) inc(u, v); } template <class T, class U> void dec(T &a, U v = 1) { a -= v; } template <class T, class U> void dec(vector<T> &a, U v = 1) { for (auto &u : a) dec(u, v); } template <class U> void dec(string &a, U v = 1) { for (auto &u : a) dec(u, v); } template <class T, class U, class W> void dec(vector<T> &a, vector<U> &b, W v = 1) { for (auto &u : a) dec(u, v); for (auto &u : b) dec(u, v); } template <class T, class U, class W> void dec(vector<T> &a, vector<U> &b, vector<W> &c) { for (auto &u : a) dec(u, 1); for (auto &u : b) dec(u, 1); for (auto &u : c) dec(u, 1); } bool ins(ll h, ll w, ll H, ll W) { return h >= 0 && w >= 0 && h < H && w < W; } bool ins(ll l, ll v, ll r) { return l <= v && v < r; } template <class T> bool ins(vector<T> &a, ll i, ll j = 0) { return ins(0, i, sz(a)) && ins(0, j, sz(a)); } #define inside ins 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; } //添え字を返す template <class F> ll goldd_l(ll left, ll right, F calc) { double GRATIO = 1.6180339887498948482045868343656; ll lm = left + (ll)((right - left) / (GRATIO + 1.0)); ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); ll fl = calc(lm); ll fr = calc(rm); while (right - left > 10) { if (fl < fr) { right = rm; rm = lm; fr = fl; lm = left + (ll)((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } ll minScore = MAX(ll); ll resIndex = left; for (ll i = left; i < right + 1; ++i) { ll score = calc(i); if (minScore > score) { minScore = score; resIndex = i; } } return resIndex; } template <class F> ll goldt_l(ll left, ll right, F calc) { double GRATIO = 1.6180339887498948482045868343656; ll lm = left + (ll)((right - left) / (GRATIO + 1.0)); ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); ll fl = calc(lm); ll fr = calc(rm); while (right - left > 10) { if (fl > fr) { right = rm; rm = lm; fr = fl; lm = left + (ll)((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } if (left > right) { ll l = left; left = right; right = l; } ll maxScore = MIN(ll); ll resIndex = left; for (ll i = left; i < right + 1; ++i) { ll score = calc(i); if (maxScore < score) { maxScore = score; resIndex = i; } } return resIndex; } /*loopは200にすればおそらく大丈夫 余裕なら300に*/ template <class F> dou goldd_d(dou left, dou right, F calc, ll loop = 200) { dou GRATIO = 1.6180339887498948482045868343656; dou lm = left + ((right - left) / (GRATIO + 1.0)); dou rm = lm + ((right - lm) / (GRATIO + 1.0)); dou fl = calc(lm); dou fr = calc(rm); /*200にすればおそらく大丈夫*/ /*余裕なら300に*/ ll k = 141; loop++; while (--loop) { if (fl < fr) { right = rm; rm = lm; fr = fl; lm = left + ((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + ((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } return left; } template <class F> dou goldt_d(dou left, dou right, F calc, ll loop = 200) { double GRATIO = 1.6180339887498948482045868343656; dou lm = left + ((right - left) / (GRATIO + 1.0)); dou rm = lm + ((right - lm) / (GRATIO + 1.0)); dou fl = calc(lm); dou fr = calc(rm); loop++; while (--loop) { if (fl > fr) { right = rm; rm = lm; fr = fl; lm = left + ((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + ((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } return left; } // l ~ rを複数の区間に分割し、極致を与えるiを返す time-20 msまで探索 /*@formatter:on*/ template <class F> ll goldd_ls(ll l, ll r, F calc, ll time = 2000) { auto lim = milliseconds(time - 20); ll mini = 0, minv = MAX(ll); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/ ll nl = l; ll nr = l + haba; rep(i, k) { ll ni = goldd_l(nl, nr, calc); if (chmi(minv, calc(ni))) mini = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return mini; } /*@formatter:off*/ template <class F> ll goldt_ls(ll l, ll r, F calc, ll time = 2000) { auto lim = milliseconds(time - 20); ll maxi = 0, maxv = MIN(ll); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/ ll nl = l; ll nr = l + haba; rep(i, k) { ll ni = goldt_l(nl, nr, calc); if (chma(maxv, calc(ni))) maxi = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return maxi; } template <class F> dou goldd_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を持つ*/ auto lim = milliseconds(time - 20); dou mini = 0, minv = MAX(dou); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); dou haba = (r - l) / k; dou nl = l; dou nr = l + haba; rep(i, k) { dou ni = goldd_d(nl, nr, calc); if (chmi(minv, calc(ni))) mini = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return mini; } template <class F> dou goldt_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を残している*/ auto lim = milliseconds(time - 20); dou maxi = 0, maxv = MIN(dou); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); dou haba = (r - l) / k; dou nl = l; dou nr = l + haba; rep(i, k) { dou ni = goldt_d(nl, nr, calc); if (chma(maxv, calc(ni))) maxi = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return maxi; } template <class T> T min(vector<vector<T>> &a) { T res = MAX(T); rep(i, a.size()) chmi(res, *min_element(all(a[i]))); return res; } template <class T> T max(vector<vector<T>> &a) { T res = MIN(T); rep(i, a.size()) chma(res, *max_element(all(a[i]))); return res; } constexpr bool bget(ll m, ll keta) { #ifdef _DEBUG assert(keta <= 62); //オーバーフロー 1^62までしか扱えない #endif return (m >> keta) & 1; } // bget(n)次元 // NならN-1まで vector<vi> bget2(vi &a, int keta_size) { vvi(res, keta_size, sz(a)); rep(k, keta_size) { rep(i, sz(a)) { res[k][i] = bget(a[i], k); } } return res; } vi bget1(vi &a, int keta) { vi res(sz(a)); rep(i, sz(a)) { res[i] = bget(a[i], keta); } return res; } ll bget(ll m, ll keta, ll sinsuu) { m /= (ll)pow(sinsuu, keta); return m % sinsuu; } constexpr ll bit(ll n) { #ifdef _DEBUG assert(n <= 62); //オーバーフロー 1^62までしか扱えない #endif return (1LL << (n)); } ll bit(ll n, ll sinsuu) { return (ll)pow(sinsuu, n); } ll mask(ll n) { return (1ll << n) - 1; } // aをbitに置きなおす //{0, 2} -> 101 ll biti(vi &a) { int m = 0; for (auto &&v : a) m |= bit(v); return m; } //{1, 1, 0} -> 011 ll bitb(vi &a) { int m = 0; rep(i, sz(a)) if (a[i]) m |= bit(i); return m; } #define bcou __builtin_popcountll //最下位ビット ll lbit(ll n) { return n & -n; } ll lbiti(ll n) { return log2(n & -n); } //最上位ビット ll hbit(ll n) { n |= (n >> 1); n |= (n >> 2); n |= (n >> 4); n |= (n >> 8); n |= (n >> 16); n |= (n >> 32); return n - (n >> 1); } ll hbiti(ll n) { return log2(hbit(n)); } ll hbitk(ll n) { ll k = 0; rer(i, 5) { ll a = k + (1ll << i); ll b = 1ll << a; if (b <= n) k += 1ll << i; } return k; } //初期化は0を渡す ll nextComb(ll &mask, ll n, ll r) { if (!mask) return mask = (1LL << r) - 1; ll x = mask & -mask; /*最下位の1*/ ll y = mask + x; /*連続した下の1を繰り上がらせる*/ ll res = ((mask & ~y) / x >> 1) | y; if (bget(res, n)) return mask = 0; else return mask = res; } // n桁以下でビットがr個立っているもののvectorを返す vi bitCombList(ll n, ll r) { vi res; ll m = 0; while (nextComb(m, n, r)) { res.push_back(m); } return res; } // masの立ってるindexを見る #define forbit(i, mas) \ for (int forbitj = lbit(mas), forbitm = mas, i = log2(forbitj); forbitm; \ forbitm = forbitj ? forbitm ^ forbitj : 0, forbitj = lbit(forbitm), \ i = log2(forbitj)) char itoal(ll i) { return 'a' + i; } char itoaL(ll i) { return 'A' + i; } ll altoi(char c) { if ('A' <= c && c <= 'Z') return c - 'A'; return c - 'a'; } ll ctoi(char c) { return c - '0'; } char itoc(ll i) { return i + '0'; } ll vtoi(vi &v) { ll res = 0; if (sz(v) > 18) { debugline("vtoi"); deb(sz(v)); ole(); } rep(i, sz(v)) { res *= 10; res += v[i]; } return res; } vi itov(ll i) { vi res; while (i) { res.push_back(i % 10); i /= 10; } rev(res); return res; } vi stov(string &a) { ll n = sz(a); vi ret(n); rep(i, n) { ret[i] = a[i] - '0'; } return ret; } //基準を満たさないものは0になる vi stov(string &a, char one) { ll n = sz(a); vi ret(n); rep(i, n) ret[i] = a[i] == one; return ret; } vector<vector<ll>> ctoi(vector<vector<char>> s, char c) { ll n = sz(s), m = sz(s[0]); vector<vector<ll>> res(n, vector<ll>(m)); rep(i, n) rep(j, m) res[i][j] = s[i][j] == c; return res; } #define unique(v) v.erase(unique(v.begin(), v.end()), v.end()); //[i] := vを返す // aは0~n-1で置き換えられる vi compress(vi &a) { vi b; ll len = a.size(); for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { a[i] = lower_bound(all(b), a[i]) - b.begin(); } ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, umap<ll, ll> &map) { vi b; ll len = a.size(); for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { ll v = a[i]; a[i] = lower_bound(all(b), a[i]) - b.begin(); map[v] = a[i]; } ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, vi &r) { vi b; ll len = a.size(); fora(v, a) b.push_back(v); fora(v, r) b.push_back(v); sort(b); unique(b); for (ll i = 0; i < len; ++i) a[i] = lower_bound(all(b), a[i]) - b.begin(); for (ll i = 0; i < sz(r); ++i) r[i] = lower_bound(all(b), r[i]) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, vi &r, vi &s) { vi b; ll len = a.size(); fora(v, a) b.push_back(v); fora(v, r) b.push_back(v); fora(v, s) b.push_back(v); sort(b); unique(b); for (ll i = 0; i < len; ++i) a[i] = lower_bound(all(b), a[i]) - b.begin(); for (ll i = 0; i < sz(r); ++i) r[i] = lower_bound(all(b), r[i]) - b.begin(); for (ll i = 0; i < sz(s); ++i) r[i] = lower_bound(all(b), s[i]) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vector<vi> &a) { vi b; fora(vv, a) fora(v, vv) b.push_back(v); sort(b); unique(b); fora(vv, a) fora(v, vv) v = lower_bound(all(b), v) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vector<vector<vi>> &a) { vi b; fora(vvv, a) fora(vv, vvv) fora(v, vv) b.push_back(v); sort(b); unique(b); fora(vvv, a) fora(vv, vvv) fora(v, vv) v = lower_bound(all(b), v) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } void compress(ll a[], ll len) { vi b; for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { a[i] = lower_bound(all(b), a[i]) - b.begin(); } } //要素が見つからなかったときに困る #define binarySearch(a, v) (binary_search(all(a), v)) #define lowerIndex(a, v) (lower_bound(all(a), v) - a.begin()) #define upperIndex(a, v) (upper_bound(all(a), v) - a.begin()) #define rlowerIndex(a, v) (upper_bound(all(a), v) - a.begin() - 1) #define rupperIndex(a, v) (lower_bound(all(a), v) - a.begin() - 1) template <class T, class U, class W> T lowerBound(vector<T> &a, U v, W banpei) { auto it = lower_bound(a.begin(), a.end(), v); if (it == a.end()) return banpei; else return *it; } template <class T, class U, class W> T lowerBound(ruiC<T> &a, U v, W banpei) { return lowerBound(a.rui, v, banpei); } template <class T, class U, class W> T upperBound(vector<T> &a, U v, W banpei) { auto it = upper_bound(a.begin(), a.end(), v); if (it == a.end()) return banpei; else return *it; } template <class T, class U, class W> T upperBound(ruiC<T> &a, U v, W banpei) { return upperBound(a.rui, v, banpei); } template <class T, class U, class W> T rlowerBound(vector<T> &a, U v, W banpei) { auto it = upper_bound(a.begin(), a.end(), v); if (it == a.begin()) return banpei; else { return *(--it); } } template <class T, class U, class W> T rlowerBound(ruiC<T> &a, U v, W banpei) { return rlowerBound(a.rui, v, banpei); } template <class T, class U, class W> T rupperBound(vector<T> &a, U v, W banpei) { auto it = lower_bound(a.begin(), a.end(), v); if (it == a.begin()) return banpei; else { return *(--it); } } template <class T, class U, class W> T rupperBound(ruiC<T> &a, U v, W banpei) { return rupperBound(a.rui, v, banpei); } #define next2(a) next(next(a)) #define prev2(a) prev(prev(a)) //狭義の単調増加列 長さを返す template <class T> int lis(vector<T> &a) { int n = sz(a); vi tail(n + 1, MAX(T)); rep(i, n) { int id = lowerIndex(tail, a[i]); /**/ tail[id] = a[i]; } return lowerIndex(tail, MAX(T)); } template <class T> int lis_eq(vector<T> &a) { int n = sz(a); vi tail(n + 1, MAX(T)); rep(i, n) { int id = upperIndex(tail, a[i]); /**/ tail[id] = a[i]; } return lowerIndex(tail, MAX(T)); } // iteratorを返す // valueが1以上の物を返す 0は見つけ次第削除 // vを減らす場合 (*it).se--でいい template <class T, class U, class V> auto lower_map(map<T, U> &m, V k) { auto ret = m.lower_bound(k); while (ret != m.end() && (*ret).second == 0) { ret = m.erase(ret); } return ret; } template <class T, class U, class V> auto upper_map(map<T, U> &m, V k) { auto ret = m.upper_bound(k); while (ret != m.end() && (*ret).second == 0) { ret = m.erase(ret); } return ret; } //存在しなければエラー template <class T, class U, class V> auto rlower_map(map<T, U> &m, V k) { auto ret = upper_map(m, k); assert(ret != m.begin()); ret--; while (1) { if ((*ret).second != 0) break; assert(ret != m.begin()); auto next = ret; --next; m.erase(ret); ret = next; } return ret; } template <class T, class U, class V> auto rupper_map(map<T, U> &m, V k) { auto ret = lower_map(m, k); assert(ret != m.begin()); ret--; while (1) { if ((*ret).second != 0) break; assert(ret != m.begin()); auto next = ret; --next; m.erase(ret); ret = next; } return ret; } template <class T> void fin(T s) { cout << s << endl, exit(0); } //便利 数学 math // sub ⊂ top bool subset(int sub, int top) { return (sub & top) == sub; } //-180 ~ 180 degree double atand(double h, double w) { return atan2(h, w) / PI * 180; } //% -mの場合、最小の正の数を返す ll mod(ll a, ll m) { if (m < 0) m *= -1; return (a % m + m) % m; } ll pow(ll a) { return a * a; }; ll fact(ll v) { return v <= 1 ? 1 : v * fact(v - 1); } dou factd(int v) { static vd fact(2, 1); if (sz(fact) <= v) { rep(i, sz(fact), v + 1) { fact.push_back(fact.back() * i); } } return fact[v]; } ll comi(ll n, ll r) { assert(n < 100); static vvi(pas, 100, 100); if (pas[0][0]) return pas[n][r]; pas[0][0] = 1; rep(i, 1, 100) { pas[i][0] = 1; rep(j, 1, i + 1) pas[i][j] = pas[i - 1][j - 1] + pas[i - 1][j]; } return pas[n][r]; } double comd2(ll n, ll r) { static vvd(comb, 2020, 2020); if (comb[0][0] == 0) { comb[0][0] = 1; rep(i, 2000) { comb[i + 1][0] = 1; rep(j, 1, i + 2) { comb[i + 1][j] = comb[i][j] + comb[i][j - 1]; } } } return comb[n][r]; } double comd(int n, int r) { if (r < 0 || r > n) return 0; if (n < 2020) return comd2(n, r); static vd fact(2, 1); if (sz(fact) <= n) { rep(i, sz(fact), n + 1) { fact.push_back(fact.back() * i); } } return fact[n] / fact[n - r] / fact[r]; } ll gcd(ll a, ll b) { while (b) a %= b, swap(a, b); return abs(a); } ll gcd(vi b) { ll res = b[0]; rep(i, 1, sz(b)) res = gcd(b[i], res); return res; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll lcm(vi a) { ll res = a[0]; rep(i, 1, sz(a)) res = lcm(a[i], res); return res; } ll ceil(ll a, ll b) { if (b == 0) { debugline("ceil"); deb(a, b); ole(); return -1; } else if (a < 0) { return 0; } else { return (a + b - 1) / b; } } ll sig0(ll t) { return t <= 0 ? 0 : ((1 + t) * t) >> 1; } // ll sig(ll s, ll t) { return ((s + t) * (t - s + 1)) >> 1; } ll sig(ll s, ll t) { if (s > t) swap(s, t); return sig0(t - s) + s * (t - s + 1); } #define tousa_i tosa_i #define lower_tousa_i lower_tosa_i #define upper_tousa upper_tosa #define upper_tousa_i upper_tosa_i ll tosa_i(ll st, ll ad, ll v) { assert(((v - st) % ad) == 0); return (v - st) / ad; } ll tosa_s(ll st, ll ad, ll len) { return st * len + sig0(len - 1) * ad; } // ax + r (x は非負整数) で表せる整数のうち、v 以上となる最小の整数 ll lower_tosa(ll st, ll ad, ll v) { if (st >= v) return st; return (v - st + ad - 1) / ad * ad + st; } //第何項か ll lower_tosa_i(ll st, ll ad, ll v) { if (st >= v) return 0; return (v - st + ad - 1) / ad; } ll upper_tosa(ll st, ll ad, ll v) { return lower_tosa(st, ad, v + 1); } ll upper_tosa_i(ll st, ll ad, ll v) { return lower_tosa_i(st, ad, v + 1); } // v * v >= aとなる最小のvを返す 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); } /*@formatter:off*/ //機能拡張 template <class T, class U> string to_string(T a, U b) { string res = ""; res += a; res += b; return res; } template <class T, class U, class V> string to_string(T a, U b, V c) { string res = ""; res += a; res += b; res += c; return res; } template <class T, class U, class V, class W> string to_string(T a, U b, V c, W d) { string res = ""; res += a; res += b; res += c; res += d; return res; } template <class T, class U, class V, class W, class X> string to_string(T a, U b, V c, W d, X e) { string res = ""; res += a; res += b; res += c; res += d; res += e; return res; } constexpr int bsetlen = k5 * 2; // constexpr int bsetlen = 5050; #define bset bitset<bsetlen> bool operator<(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] < b[i]) return true; if (a[i] > b[i]) return false; } return false; } bool operator>(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] > b[i]) return true; if (a[i] < b[i]) return false; } return false; } bool operator<=(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] < b[i]) return true; if (a[i] > b[i]) return false; } return true; } bool operator>=(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] > b[i]) return true; if (a[i] < b[i]) return false; } return true; } string operator~(string &a) { string res = a; for (auto &&c : res) { if (c == '0') c = '1'; else if (c == '1') c = '0'; else { cerr << "cant ~" << a << "must bit" << endl; exit(0); } } return res; } ostream &operator<<(ostream &os, bset &a) { bitset<10> b; vi list; rep(i, bsetlen) { if (a[i]) list.push_back(i), b[i] = 1; } os << b << ", " << list; return os; } int hbiti(bset &a) { rer(i, bsetlen) { if (a[i]) return i; } return -1; } #define hk(a, b, c) (a <= b && b < c) // O(N/64) bset nap(bset &a, int v) { bset r = a | a << v; return r; } bset nap(bset &a, bset &v) { bset r = a; rep(i, bsetlen) { if (v[i]) r |= a << i; } return r; } template <class T> void seth(vector<vector<T>> &S, int w, vector<T> &v) { assert(sz(S) == sz(v)); assert(w < sz(S[0])); rep(h, sz(S)) { S[h][w] = v[h]; } } template <class T, class U> void operator+=(pair<T, U> &a, pair<T, U> &b) { a.fi += b.fi; a.se += b.se; } template <class T, class U> pair<T, U> operator+(pair<T, U> &a, pair<T, U> &b) { return pair<T, U>(a.fi + b.fi, a.se + b.se); } template <typename CharT, typename Traits, typename Alloc> basic_string<CharT, Traits, Alloc> operator+(const basic_string<CharT, Traits, Alloc> &lhs, const int rv) { basic_string<CharT, Traits, Alloc> str(lhs); str.append(to_string(rv)); return str; } template <typename CharT, typename Traits, typename Alloc> void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const int rv) { lhs += to_string(rv); } template <typename CharT, typename Traits, typename Alloc> basic_string<CharT, Traits, Alloc> operator+(const basic_string<CharT, Traits, Alloc> &lhs, const signed rv) { basic_string<CharT, Traits, Alloc> str(lhs); str.append(to_string(rv)); return str; } template <typename CharT, typename Traits, typename Alloc> void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const signed rv) { lhs += to_string(rv); } template <typename CharT, typename Traits, typename Alloc> void operator*=(basic_string<CharT, Traits, Alloc> &s, int num) { auto bek = s; s = ""; for (; num; num >>= 1) { if (num & 1) { s += bek; } bek += bek; } } template <class T, class U> void operator+=(queue<T> &a, U v) { a.push(v); } template <class T, class U> void operator+=(deque<T> &a, U v) { a.push_back(v); } template <class T> priority_queue<T, vector<T>, greater<T>> & operator+=(priority_queue<T, vector<T>, greater<T>> &a, vector<T> &v) { fora(d, v) a.push(d); return a; } template <class T, class U> priority_queue<T, vector<T>, greater<T>> & operator+=(priority_queue<T, vector<T>, greater<T>> &a, U v) { a.push(v); return a; } template <class T, class U> priority_queue<T> &operator+=(priority_queue<T> &a, U v) { a.push(v); return a; } template <class T> set<T> &operator+=(set<T> &a, vector<T> v) { fora(d, v) a.insert(d); return a; } template <class T, class U> auto operator+=(set<T> &a, U v) { return a.insert(v); } template <class T, class U> auto operator-=(set<T> &a, U v) { return a.erase(v); } template <class T, class U> auto operator+=(mset<T> &a, U v) { return a.insert(v); } template <class T, class U> set<T, greater<T>> &operator+=(set<T, greater<T>> &a, U v) { a.insert(v); return a; } template <class T, class U> vector<T> &operator+=(vector<T> &a, U v) { a.push_back(v); return a; } template <class T, class U> vector<T> operator+(const vector<T> &a, U v) { vector<T> ret = a; ret += v; return ret; } template <class T, class U> vector<T> operator+(U v, const vector<T> &a) { vector<T> ret = a; ret.insert(ret.begin(), v); return ret; } template <class T> vector<T> operator+(vector<T> a, vector<T> b) { vector<T> ret; ret = a; fora(v, b) ret += v; return ret; } template <class T> vector<T> &operator+=(vector<T> &a, vector<T> &b) { rep(i, sz(b)) { /*こうしないとa+=aで両辺が増え続けてバグる*/ a.push_back(b[i]); } return a; } template <class T, class U> map<T, U> &operator+=(map<T, U> &a, map<T, U> &b) { fora(bv, b) { a[bv.first] += bv.second; } return a; } template <class T> vector<T> &operator-=(vector<T> &a, vector<T> &b) { if (sz(a) != sz(b)) { debugline("vector<T> operator-="); deb(a); deb(b); exit(0); } rep(i, sz(a)) a[i] -= b[i]; return a; } template <class T> vector<T> operator-(vector<T> &a, vector<T> &b) { if (sz(a) != sz(b)) { debugline("vector<T> operator-"); deb(a); deb(b); ole(); } vector<T> res(sz(a)); rep(i, sz(a)) res[i] = a[i] - b[i]; return res; } template <class T, class U> void operator*=(vector<T> &a, U b) { vector<T> ta = a; rep(b - 1) { a += ta; } } template <typename T> void erase(vector<T> &v, unsigned ll i) { v.erase(v.begin() + i); } template <typename T> void erase(vector<T> &v, unsigned ll s, unsigned ll e) { v.erase(v.begin() + s, v.begin() + e); } template <class T, class U> void erase(map<T, U> &m, ll okl, ll ngr) { m.erase(m.lower_bound(okl), m.lower_bound(ngr)); } template <class T> void erase(set<T> &m, ll okl, ll ngr) { m.erase(m.lower_bound(okl), m.lower_bound(ngr)); } template <typename T> void erasen(vector<T> &v, unsigned ll s, unsigned ll n) { v.erase(v.begin() + s, v.begin() + s + n); } template <typename T, typename U> void insert(vector<T> &v, unsigned ll i, U t) { v.insert(v.begin() + i, t); } template <typename T, typename U> void push_front(vector<T> &v, U t) { v.insert(v.begin(), t); } template <typename T, typename U> void insert(vector<T> &v, unsigned ll i, vector<T> list) { for (auto &&va : list) v.insert(v.begin() + i++, va); } template <typename T> void insert(set<T> &v, vector<T> list) { for (auto &&va : list) v.insert(va); } vector<string> split(const string a, const char deli) { string b = a + deli; ll l = 0, r = 0, n = b.size(); vector<string> res; rep(i, n) { if (b[i] == deli) { r = i; if (l < r) res.push_back(b.substr(l, r - l)); l = i + 1; } } return res; } vector<string> split(const string a, const string deli) { vector<string> res; ll kn = sz(deli); std::string::size_type Pos(a.find(deli)); ll l = 0; while (Pos != std::string::npos) { if (Pos - l) res.push_back(a.substr(l, Pos - l)); l = Pos + kn; Pos = a.find(deli, Pos + kn); } if (sz(a) - l) res.push_back(a.substr(l, sz(a) - l)); return res; } 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 fAb(bool a) { if (a) cout << "Alice" << endl; else cout << "Bob"; } 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 <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)}; } //未分類 // 0,2,1 1番目と2番目の次元を入れ替える template <class T> auto irekae(vector<vector<vector<T>>> &A, int x, int y, int z) { #define irekae_resize_loop(a, b, c) \ resize(res, a, b, c); \ rep(i, a) rep(j, b) rep(k, c) vector<vector<vector<T>>> res; if (x == 0 && y == 1 && z == 2) { res = A; } else if (x == 0 && y == 2 && z == 1) { irekae_resize_loop(sz(A), sz(A[0][0]), sz(A[0])) { res[i][j][k] = A[i][k][j]; } } else if (x == 1 && y == 0 && z == 2) { irekae_resize_loop(sz(A[0]), sz(A), sz(A[0][0])) { res[i][j][k] = A[j][i][k]; } } else if (x == 1 && y == 2 && z == 0) { irekae_resize_loop(sz(A[0]), sz(A[0][0]), sz(A)) { res[i][j][k] = A[k][i][j]; } } else if (x == 2 && y == 0 && z == 1) { irekae_resize_loop(sz(A[0][0]), sz(A), sz(A[0])) { res[i][j][k] = A[j][k][i]; } } else if (x == 2 && y == 1 && z == 0) { irekae_resize_loop(sz(A[0][0]), sz(A[0]), sz(A)) { res[i][j][k] = A[k][j][i]; } } return res; #undef irekae_resize_loop } template <class T> auto irekae(vector<vector<T>> &A, int i = 1, int j = 0) { vvt(res, sz(A[0]), sz(A)); rep(i, sz(A)) { rep(j, sz(A[0])) { res[j][i] = A[i][j]; } } return res; } // tou分割する template <typename T> vector<vector<T>> cut(vector<T> &a, int tou = 2) { int N = sz(a); vector<vector<T>> res(tou); int hab = N / tou; vi lens(tou, hab); rep(i, N % tou) { lens[tou - 1 - i]++; } int l = 0; rep(i, tou) { int len = lens[i]; int r = l + len; res[i].resize(len); std::copy(a.begin() + l, a.begin() + r, res[i].begin()); l = r; } return res; } //長さn毎に分割する template <typename T> vector<vector<T>> cutn(vector<T> &a, int len) { int N = sz(a); vector<vector<T>> res(ceil(N, len)); vi lens(N / len, len); if (N % len) lens.push_back(N % len); int l = 0; rep(i, sz(lens)) { int len = lens[i]; int r = l + len; res[i].resize(len); std::copy(a.begin() + l, a.begin() + r, res[i].begin()); l = r; } return res; } vi inds_(vi &a) { int n = sz(a); vb was(n); vi res(n); rep(i, n) { assert(!was[a[i]]); res[a[i]] = i; was[a[i]] = true; } return res; } //@起動時 struct initon { initon() { cin.tie(0); ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(16); srand((unsigned)clock() + (unsigned)time(NULL)); }; } initonv; //@formatter:on #define pre prev #define nex next // gra mll pr //上下左右 const string udlr = "udlr"; string UDLR = "UDLR"; // x4と連動 UDLR.find('U') := x4[0] //右、上が正 constexpr ll h4[] = {1, -1, 0, 0}; constexpr ll w4[] = {0, 0, -1, 1}; constexpr ll h8[] = {0, 1, 0, -1, -1, 1, 1, -1}; constexpr ll w8[] = {1, 0, -1, 0, 1, -1, 1, -1}; int mei_inc(int h, int w, int H, int W, int i) { while (++i < 4) { if (inside(h + h4[i], w + w4[i], H, W)) return i; } return i; } #define mei(nh, nw, h, w) \ for (int i = mei_inc(h, w, H, W, -1), nh = i < 4 ? h + h4[i] : 0, \ nw = i < 4 ? w + w4[i] : 0; \ i < 4; i = mei_inc(h, w, H, W, i), nh = h + h4[i], nw = w + w4[i]) //グラフ内で #undef getid //#define getidとしているため、ここを書き直したらgraphも書き直す #define getid_2(h, w) (h * W + w) #define getid_1(p) (p.first * W + p.second) #define o_getid(a, b, name, ...) name #define getid(...) o_getid(__VA_ARGS__, getid_2, getid_1)(__VA_ARGS__) #define getp(id) mp(id / W, id % W) #define set_shuffle() \ std::random_device seed_gen; \ std::mt19937 engine(seed_gen()) #define shuffle(a) std::shuffle((a).begin(), (a).end(), engine); // 1980 開始からtime ms経っていたらtrue bool timeup(int time) { auto end_time = system_clock::now(); auto part = duration_cast<milliseconds>(end_time - start_time); auto lim = milliseconds(time); return part >= lim; } void set_time() { past_time = system_clock::now(); } // MS型(millisecqnds)で返る // set_timeをしてからの時間 auto calc_time_milli() { auto now = system_clock::now(); auto part = duration_cast<milliseconds>(now - past_time); return part; } auto calc_time_micro() { auto now = system_clock::now(); auto part = duration_cast<microseconds>(now - past_time); return part; } auto calc_time_nano() { auto now = system_clock::now(); auto part = duration_cast<nanoseconds>(now - past_time); return part; } bool calc_time(int zikan) { return calc_time_micro() >= microseconds(zikan); } using MS = std::chrono::microseconds; int div(microseconds a, microseconds b) { return a / b; } int div(nanoseconds a, nanoseconds b) { if (b < nanoseconds(1)) { return a / nanoseconds(1); } int v = a / b; return v; } // set_time(); // rep(i,lim)shori // lim*=time_nanbai(); // rep(i,lim)shoriと使う //全体でmilliかかっていいときにlimを何倍してもう一回できるかを返す int time_nanbai(int milli) { auto dec = duration_cast<nanoseconds>(past_time - start_time); auto part = calc_time_nano(); auto can_time = nanoseconds(milli * 1000 * 1000); can_time -= part; can_time -= dec; return div(can_time, part); } // vectorで取れる要素数 // bool=> 1e9 * 8.32 // int => 1e8 * 2.6 // ll => 1e8 * 1.3 // 3次元以上取るとメモリがヤバい // static配列を使う vvc(ba); ll N, M, H, W; vi A, B, C; //遅い 文字列以外も扱えるはず template <class T = string> struct rolling_hash2 { //@formatter:off const int b[2] = {11731, 11839}; const int m[2] = {1000002571LL, 1000002631LL}; int n; T s; // stringやvector vector<long long> hash[2], pow[2]; rolling_hash2() {} rolling_hash2(T &S) : s(S) { n = (int)S.size(); for (int i = 0; i < 2; ++i) { hash[i].assign(n + 1, 0); pow[i].assign(n + 1, 1); for (int j = 0; j < n; ++j) { hash[i][j + 1] = (hash[i][j] * b[i] + S[j]) % m[i]; pow[i][j + 1] = pow[i][j] * b[i] % m[i]; } } } // get hash of S[left:right] ll get(int l, int r, int id) { ll res = hash[id][r] - hash[id][l] * pow[id][r - l] % m[id]; if (res < 0) res += m[id]; return res; } ll get(int l, int r) { return (get(l, r, 0) << 32) | get(l, r, 1); } ll operator()(int l, int r) { return get(l, r, 0); } ll operator()() { return get(0, n, 0); } // get lcp of S[a:] and S[b:] int lcp(int a, int b) { int len = min((int)s.size() - a, (int)s.size() - b); int low = -1, high = len + 1; while (high - low > 1) { int mid = (low + high) / 2; if (get(a, a + mid, 0) != get(b, b + mid, 0)) high = mid; else if (get(a, a + mid, 1) != get(b, b + mid, 1)) high = mid; else low = mid; } return low; } // get lcp of S[a:] and T[b:] int lcp(rolling_hash2 &t, int a, int b) { int len = min((int)s.size() - a, (int)s.size() - b); int low = -1, high = len + 1; while (high - low > 1) { int mid = (low + high) / 2; if (get(a, a + mid, 0) != get(b, b + mid, 0)) high = mid; else if (get(a, a + mid, 1) != get(b, b + mid, 1)) high = mid; else low = mid; } return low; } }; //早い struct rolling_hash { static constexpr int base1 = 1007, base2 = 2009; static constexpr int mod1 = 1000000007, mod2 = 1000000009; vector<long long> hash1, hash2, power1, power2; int n; rolling_hash(string &S) { n = (int)S.size(); hash1.assign(n + 1, 0); hash2.assign(n + 1, 0); power1.assign(n + 1, 1); power2.assign(n + 1, 1); for (int i = 0; i < n; ++i) { hash1[i + 1] = (hash1[i] * base1 + S[i]) % mod1; hash2[i + 1] = (hash2[i] * base2 + S[i]) % mod2; power1[i + 1] = (power1[i] * base1) % mod1; power2[i + 1] = (power2[i] * base2) % mod2; } } /*@formatter:on*/ template <class T> rolling_hash(vector<T> &S) { n = (int)S.size(); hash1.assign(n + 1, 0); hash2.assign(n + 1, 0); power1.assign(n + 1, 1); power2.assign(n + 1, 1); for (int i = 0; i < n; ++i) { hash1[i + 1] = (hash1[i] * base1 + S[i]) % mod1; hash2[i + 1] = (hash2[i] * base2 + S[i]) % mod2; power1[i + 1] = (power1[i] * base1) % mod1; power2[i + 1] = (power2[i] * base2) % mod2; } } /*@formatter:off*/ inline pair<int, int> get(int l, int r) { long long res1 = hash1[r] - hash1[l] * power1[r - l] % mod1; if (res1 < 0) res1 += mod1; long long res2 = hash2[r] - hash2[l] * power2[r - l] % mod2; if (res2 < 0) res2 += mod2; return {res1, res2}; } inline pair<int, int> operator()(int l, int r) { return get(l, r); } inline pair<int, int> operator()() { return get(0, n); } // get lcp of S[a:] and T[b:] //接頭語がどれだけ一致するか //範囲の重複を含む inline int lcp_dup(int a, int b) { int len = min((int)hash1.size() - a, (int)hash1.size() - b); int low = 0, high = len; while (high - low > 1) { int mid = (low + high) >> 1; if (get(a, a + mid) != get(b, b + mid)) high = mid; else low = mid; } return low; } //重複を認めない inline int lcp_not(int a, int b) { return min(lcp_dup(a, b), b - a); } //@formatter:on }; #define roll rolling_hash // roll ro(list); void solve() { in(N); na(A, N); na(B, N); if (all_of(A, == A[0]) && all_of(B, == B[0])) { rep(i, N) { out(i, A[0] ^ B[0]); } } vi callr(N); vi resr(N); auto DS = bget2(B, 31); rep(b, 31) { roll rb(DS[b]); auto hb = rb(0, N); rep(x, 2) { vi C = bget1(A, b); C *= 2; for_each(C, ^= x); roll ra(C); rep(r, N) { //簡易チェック rep(i, 3) { if ((C[r + i]) != DS[b][i]) goto end; } if (ra(r, r + N) == hb) { callr[r]++; resr[r] += bit(b) * x; } end:; } } } rep(r, N) { if (callr[r] == 31) { out(r, A[r] ^ B[0]); } } } auto my(ll n, vi &a) { return 0; } auto sister(ll n, vi &a) { ll ret = 0; return ret; } signed main() { solve(); #define arg n, a #ifdef _DEBUG bool bad = 0; for (ll i = 0, ok = 1; i < k5 && ok; ++i) { ll n = rand(1, 8); vi a = ranv(n, 1, 10); auto myres = my(arg); auto res = sister(arg); ok = myres == res; if (!ok) { out(arg); cerr << "AC : " << res << endl; cerr << "MY : " << myres << endl; bad = 1; break; } } if (!bad) { // cout << "完璧 : solveを書き直そう" << endl; // cout << " : そして、solve()を呼び出すのだ" << endl; // cout << " : cin>>n; na(a,n);も忘れるな" << endl; } #endif return 0; };
//#undef _DEBUG //#pragma GCC optimize("Ofast") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace std::chrono; #define int long long // todo 消したら動かない intの代わりにsignedを使う #define ll long long auto start_time = system_clock::now(); auto past_time = system_clock::now(); #define debugName(VariableName) #VariableName /*@formatter:off*/ //[-n, n)にアクセスできる //また、外部関数resizeに渡せる // sizeは[-n, n)でnを返す template <class T> class mvec { vector<T> v; int n; public: mvec() : n(0), v(0) {} mvec(int n) : n(n), v(n * 2) {} mvec(int n, T val) : n(n), v(n * 2, val) {} auto &operator[](int i) { return v[i + n]; } auto size() { return n; } void resize(int sn) { assert(n == 0); n = sn; v.resize(sn * 2); } auto begin() { return v.begin(); } auto rbegin() { return v.rbegin(); } auto end() { return v.end(); } auto rend() { return v.rend(); } }; //[]でboolは参照を返さないため特殊化が必要 template <> struct mvec<bool> { vector<bool> v; int n; mvec() : n(0), v(0) {} mvec(int n) : n(n), v(n * 2) {} mvec(int n, bool val) : n(n), v(n * 2, val) {} auto operator[](int i) { return v[i + n]; } auto size() { return v.size(); } void resize(int sn) { assert(n == 0); n = sn; v.resize(sn * 2); } auto begin() { return v.begin(); } auto rbegin() { return v.rbegin(); } auto end() { return v.end(); } auto rend() { return v.rend(); } }; template <class T> ostream &operator<<(ostream &os, mvec<T> &a) { int spa = 3; for (auto &&v : a) { spa = max(spa, (int)(to_string(v).size()) + 1); } int n = (int)a.size(); os << endl; for (int i = -n; i < n; i++) { int need = spa - ((int)to_string(i).size()); if (i == -n) { need -= min(need, spa - ((int)to_string(a[i]).size())); } while (need--) { os << " "; } os << i; } os << endl; int i = -n; for (auto &&v : a) { int need = spa - ((int)to_string(v).size()); if (i == -n) { need -= min(need, spa - ((int)to_string(i).size())); } while (need--) { os << " "; } os << v; i++; } return os; } #define mv mvec #define MV mvec using mvi = mvec<ll>; using mvb = mvec<bool>; using mvs = mvec<string>; using mvd = mvec<double>; using mvc = mvec<char>; #define o_mvvt(o1, o2, o3, o4, name, ...) name #define mvvt0(t) mvec<mvec<t>> #define mvvt1(t, a) mvec<mvec<t>> a #define mvvt2(t, a, b) mvec<mvec<t>> a(b) #define mvvt3(t, a, b, c) mvec<mvec<t>> a(b, mvec<t>(c)) #define mvvt4(t, a, b, c, d) mvec<mvec<t>> a(b, mvec<t>(c, d)) #define mvvi(...) \ o_mvvt(__VA_ARGS__, mvvt4, mvvt3, mvvt2, mvvt1, mvvt0)(ll, __VA_ARGS__) template <typename T> mvec<T> make_mv(size_t a) { return mvec<T>(a); } template <typename T, typename... Ts> auto make_mv(size_t a, Ts... ts) { return mvec<decltype(make_mv<T>(ts...))>(a, make_mv<T>(ts...)); } #define mvni(name, ...) auto name = make_mv<ll>(__VA_ARGS__) #ifdef _DEBUG // https://marycore.jp/prog/cpp/class-extension-methods/ 違うかも template <class T, class A = std::allocator<T>> struct debtor : std::vector<T, A> { using std::vector<T, A>::vector; template <class U> int deb_v(U a, int v) { return v; } template <class U> int deb_v(debtor<U> &a, int v = 0) { cerr << a.size() << " "; return deb_v(a.at(0), v + 1); } template <class U> void deb_o(U a) { cerr << a << " "; } template <class U> void deb_o(debtor<U> &a) { for (int i = 0; i < min((int)a.size(), 15ll); i++) { deb_o(a[i]); } if ((int)a.size() > 15) { cerr << "..."; } cerr << endl; } typename std::vector<T>::reference operator[](typename std::vector<T>::size_type n) { if (n < 0 || n >= (int)this->size()) { int siz = (int)this->size(); cerr << "vector size = "; int dim = deb_v((*this)); cerr << endl; cerr << "out index at " << n << endl; cerr << endl; if (dim <= 2) { deb_o((*this)); } exit(0); } return this->at(n); } }; #define vector debtor #endif #ifdef _DEBUG //区間削除は出来ない template <class T> struct my_pbds_tree { set<T> s; auto begin() { return s.begin(); } auto end() { return s.end(); } auto rbegin() { return s.rbegin(); } auto rend() { return s.rend(); } auto empty() { return s.empty(); } auto size() { return s.size(); } void clear() { s.clear(); } template <class U> void insert(U v) { s.insert(v); } template <class U> void operator+=(U v) { insert(v); } template <class F> auto erase(F v) { return s.erase(v); } template <class U> auto find(U v) { return s.find(v); } template <class U> auto lower_bound(U v) { return s.lower_bound(v); } template <class U> auto upper_bound(U v) { return s.upper_bound(v); } auto find_by_order(ll k) { auto it = s.begin(); for (ll i = 0; i < k; i++) it++; return it; } auto order_of_key(ll v) { auto it = s.begin(); ll i = 0; for (; it != s.end() && *it < v; i++) it++; return i; } }; #define pbds(T) my_pbds_tree<T> // gp_hash_tableでcountを使えないようにするため template <class T, class U> struct my_unordered_map { unordered_map<T, U> m; my_unordered_map(){}; auto begin() { return m.begin(); } auto end() { return m.end(); } auto cbegin() { return m.cbegin(); } auto cend() { return m.cend(); } template <class V> auto erase(V v) { return m.erase(v); } void clear() { m.clear(); } /*countは gp_hash_tableに存在しない*/ /*!= m.end()*/ template <class V> auto find(V v) { return m.find(v); } template <class V> auto &operator[](V n) { return m[n]; } }; #define unordered_map my_unordered_map #define umapi unordered_map<ll, ll> #define umapp unordered_map<P, ll> #define umapip unordered_map<ll, P> #else // umapはunorderd_mapになる // umapiはgp_hash_table // find_by_order(k) k番目のイテレーター // order_of_key(k) k以上が前から何番目か #define pbds(U) \ __gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag, \ __gnu_pbds::tree_order_statistics_node_update> #define umapi __gnu_pbds::gp_hash_table<ll, ll, xorshift> #define umapp __gnu_pbds::gp_hash_table<P, ll, xorshift> #define umapip __gnu_pbds::gp_hash_table<ll, P, xorshift> #endif /*@formatter:on*/ struct xorshift { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } size_t operator()(std::pair<ll, ll> x) const { ll v = ((x.first) << 32) | x.second; static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(v + FIXED_RANDOM); } }; /*@formatter:off*/ template <class U, class L> void operator+=( __gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update> &s, L v) { s.insert(v); } //衝突対策 #define ws ws_ template <class A, class B, class C> struct T2 { A f; B s; C t; T2() { f = 0, s = 0, t = 0; } T2(A f, B s, C t) : f(f), s(s), t(t) {} bool operator<(const T2 &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 ?n s > r.s : t > r.t; 大きい順 */ } bool operator>(const T2 &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 T2 &r) const { return f == r.f && s == r.s && t == r.t; } bool operator!=(const T2 &r) const { return f != r.f || s != r.s || t != r.t; } }; template <class A, class B, class C, class D> struct F2 { A a; B b; C c; D d; F2() { a = 0, b = 0, c = 0, d = 0; } F2(A a, B b, C c, D d) : a(a), b(b), c(c), d(d) {} bool operator<(const F2 &r) const { return a != r.a ? a < r.a : b != r.b ? b < r.b : c != r.c ? c < r.c : d < r.d; /* return a != r.a ? a > r.a : b != r.b ? b > r.b : c != r.c ? c > r.c : d > r.d;*/ } bool operator>(const F2 &r) const { return a != r.a ? a > r.a : b != r.b ? b > r.b : c != r.c ? c > r.c : d > r.d; /* return a != r.a ? a < r.a : b != r.b ? b < r.b : c != r.c ? c < r.c : d < r.d;*/ } bool operator==(const F2 &r) const { return a == r.a && b == r.b && c == r.c && d == r.d; } bool operator!=(const F2 &r) const { return a != r.a || b != r.b || c != r.c || d != r.d; } ll operator[](ll i) { assert(i < 4); return i == 0 ? a : i == 1 ? b : i == 2 ? c : d; } }; typedef T2<ll, ll, ll> T; typedef F2<ll, ll, ll, ll> F; T mt(ll a, ll b, ll c) { return T(a, b, c); } F mf(ll a, ll b, ll c, ll d) { return F(a, b, c, d); } //@マクロ省略系 型,構造 #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>; using mvp = mvec<P>; using mvt = mvec<T>; #define MIN(a) numeric_limits<a>::min() #define MAX(a) numeric_limits<a>::max() #define fi first #define se second #define beg begin #define rbeg rbegin #define con continue #define bre break #define brk break #define is == #define el else #define elf else if #define upd update #define sstream stringstream #define maxq 1 #define minq -1 #define ZERO(a) memset(a, 0, sizeof(a)) #define MINUS(a) memset(a, 0xff, sizeof(a)) #define MALLOC(type, len) (type *)malloc((len) * sizeof(type)) #define lam(right) [&](auto &p) { return p right; } //マクロ省略系 コンテナ 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 V vector #define o_vvt(o1, o2, o3, o4, name, ...) name #define vvt0(t) vector<vector<t>> #define vvt1(t, a) vector<vector<t>> a #define vvt2(t, a, b) vector<vector<t>> a(b) #define vvt3(t, a, b, c) vector<vector<t>> a(b, vector<t>(c)) #define vvt4(t, a, b, c, d) vector<vector<t>> a(b, vector<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__) #define vv(type, ...) \ o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(type, __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 vn(type, name, ...) auto name = make_v<type>(__VA_ARGS__) #define PQ priority_queue<ll, vector<ll>, greater<ll>> #define tos to_string using mapi = map<ll, ll>; using mapp = map<P, ll>; using mapd = map<dou, ll>; using mapc = map<char, ll>; using maps = map<str, ll>; using seti = set<ll>; using setd = set<dou>; using setc = set<char>; using sets = set<str>; using qui = queue<ll>; #define uset unordered_set #define useti unordered_set<ll, xorshift> #define mset multiset #define mseti multiset<ll> #define umap unordered_map #define mmap multimap //任意のマクロサポート用 使う度に初期化する int index_, v1_, v2_, v3_; template <class T> struct pq { priority_queue<T, vector<T>, greater<T>> q; /*小さい順*/ T su = 0; void clear() { q = priority_queue<T, vector<T>, greater<T>>(); su = 0; } void operator+=(T v) { su += v; q.push(v); } T sum() { return su; } T top() { return q.top(); } void pop() { su -= q.top(); q.pop(); } T poll() { T ret = q.top(); su -= ret; q.pop(); return ret; } ll size() { return q.size(); } }; template <class T> struct pqg { priority_queue<T> q; /*大きい順*/ T su = 0; void clear() { q = priority_queue<T>(); su = 0; } void operator+=(T v) { su += v; q.push(v); } T sum() { return su; } T top() { return q.top(); } void pop() { su -= q.top(); q.pop(); } T poll() { T ret = q.top(); su -= ret; q.pop(); return ret; } ll size() { return q.size(); } }; #define pqi pq<ll> #define pqgi pqg<ll> //マクロ 繰り返し //↓@オーバーロード隔離 #define o_rep(o1, o2, o3, o4, name, ...) name #define rep1(n) for (ll rep1i = 0, rep1lim = n; rep1i < rep1lim; ++rep1i) #define rep2(i, n) for (ll i = 0, rep2lim = n; i < rep2lim; ++i) #define rep3(i, m, n) for (ll i = m, rep3lim = n; i < rep3lim; ++i) #define rep4(i, m, n, ad) for (ll i = m, rep4lim = n; i < rep4lim; i += ad) //逆順 閉区間 #define rer2(i, n) for (ll i = n; i >= 0; i--) #define rer3(i, m, n) for (ll i = m, rer3lim = n; i >= rer3lim; i--) #define rer4(i, m, n, dec) for (ll i = m, rer4lim = n; i >= rer4lim; i -= dec) //ループを一つにまとめないとフォーマットで汚くなるため #define nex_ind1(i) i++ #define nex_ind2(i, j, J) \ i = (j + 1 == J) ? i + 1 : i, j = (j + 1 == J ? 0 : j + 1) #define nex_ind3(i, j, k, J, K) \ i = (j + 1 == J && k + 1 == K) ? i + 1 : i, \ j = (k + 1 == K) ? (j + 1 == J ? 0 : j + 1) : j, \ k = (k + 1 == K ? 0 : k + 1) #define nex_ind4(i, j, k, l, J, K, L) \ i = (j + 1 == J && k + 1 == K && l + 1 == L) ? i + 1 : i, \ j = (k + 1 == K && l + 1 == L) ? (j + 1 == J ? 0 : j + 1) : j, \ k = (l + 1 == L ? (k + 1 == K ? 0 : k + 1) : k), l = l + 1 == L ? 0 : l + 1 #define nex_ind5(i, j, k, l, m, J, K, L, M) \ i = (j + 1 == J && k + 1 == K && l + 1 == L && m + 1 == M) ? i + 1 : i, \ j = (k + 1 == K && l + 1 == L && m + 1 == M) ? (j + 1 == J ? 0 : j + 1) : j, \ k = (l + 1 == L && m + 1 == M ? (k + 1 == K ? 0 : k + 1) : k), \ l = m + 1 == M ? l + 1 == L ? 0 : l + 1 : l, m = m + 1 == M ? 0 : m + 1 #define repss1(i, I) for (int i = 0; i < I; i++) #define repss2(i, j, I, J) \ for (int i = (J ? 0 : I), j = 0; i < I; nex_ind2(i, j, J)) #define repss3(i, j, k, I, J, K) \ for (int i = (J && K ? 0 : I), j = 0, k = 0; i < I; nex_ind3(i, j, k, J, K)) #define repss4(i, j, k, l, I, J, K, L) \ for (int i = (J && K && L ? 0 : I), j = 0, k = 0, l = 0; i < I; \ nex_ind4(i, j, k, l, J, K, L)) #define repss5(i, j, k, l, m, I, J, K, L, M) \ for (int i = (J && K && L && M ? 0 : I), j = 0, k = 0, l = 0, m = 0; i < I; \ nex_ind5(i, j, k, l, m, J, K, L, M)) #define o_repss(o1, o2, o3, o4, o5, o6, o7, o8, o9, o10, name, ...) name // i,j,k...をnまで見る #define reps1(i, n) repss1(i, n) #define reps2(i, j, n) repss2(i, j, n, n) #define reps3(i, j, k, n) repss3(i, j, k, n, n, n) #define reps4(i, j, k, l, n) repss4(i, j, k, l, n, n, n, n) #define o_reps(o1, o2, o3, o4, o5, name, ...) name template <class T> void nex_repv2(int &i, int &j, int &I, int &J, vector<vector<T>> &s) { while (1) { j++; if (j >= J) { j = 0; i++; if (i < I) { J = (int)s[i].size(); } } if (i >= I || J) return; } } template <class T> void nex_repv3(int &i, int &j, int &k, int &I, int &J, int &K, vector<vector<vector<T>>> &s) { while (1) { k++; if (k >= K) { k = 0; j++; if (j >= J) { j = 0; i++; if (i >= I) return; } } J = (int)s[i].size(); K = (int)s[i][j].size(); if (J && K) return; } } #define repv_1(i, a) repss1(i, sz(a)) //正方形である必要はない //直前を持つのとどっちが早いか #define repv_2(i, j, a) \ for (int I = (int)a.size(), J = (int)a[0].size(), i = 0, j = 0; i < I; \ nex_repv2(i, j, I, J, a)) //箱状になっている事が要求される つまり[i] 次元目の要素数は一定 #define repv_3(i, j, k, a) \ for (int I = (int)a.size(), J = (int)a[0].size(), K = (int)a[0][0].size(), \ i = 0, j = 0, k = 0; \ i < I; nex_repv3(i, j, k, I, J, K, a)) #define repv_4(i, j, k, l, a) \ repss4(i, j, k, l, sz(a), sz(a[0]), sz(a[0][0]), sz(a[0][0][0])) #define repv_5(i, j, k, l, m, a) \ repss5(i, j, k, l, m, sz(a), sz(a[0]), sz(a[0][0]), sz(a[0][0][0]), \ sz(a[0][0][0][0])) #define o_repv(o1, o2, o3, o4, o5, o6, name, ...) name template <typename T> struct has_rbegin_rend { private: template <typename U> static auto check(U &&obj) -> decltype(std::rbegin(obj), std::rend(obj), std::true_type{}); static std::false_type check(...); public: static constexpr bool value = decltype(check(std::declval<T>()))::value; }; template <typename T> constexpr bool has_rbegin_rend_v = has_rbegin_rend<T>::value; template <typename Iterator> class Range { public: Range(Iterator &&begin, Iterator &&end) noexcept : m_begin(std::forward<Iterator>(begin)), m_end(std::forward<Iterator>(end)) {} Iterator begin() const noexcept { return m_begin; } Iterator end() const noexcept { return m_end; } private: const Iterator m_begin; const Iterator m_end; }; template <typename Iterator> static inline Range<Iterator> makeRange(Iterator &&begin, Iterator &&end) noexcept { return Range<Iterator>{std::forward<Iterator>(begin), std::forward<Iterator>(end)}; } template <typename T> static inline decltype(auto) makeReversedRange(const std::initializer_list<T> &iniList) noexcept { return makeRange(std::rbegin(iniList), std::rend(iniList)); } template <typename T, typename std::enable_if_t<has_rbegin_rend_v<T>, std::nullptr_t> = nullptr> static inline decltype(auto) makeReversedRange(T &&c) noexcept { return makeRange(std::rbegin(c), std::rend(c)); } /* rbegin(), rend()を持たないものはこっちに分岐させて,エラーメッセージを少なくする*/ template <typename T, typename std::enable_if<!has_rbegin_rend<T>::value, std::nullptr_t>::type = nullptr> static inline void makeReversedRange(T &&) noexcept { static_assert(has_rbegin_rend<T>::value, "Specified argument doesn't have reverse iterator."); } #define form1(st) \ for (auto &&form_it = st.begin(); form_it != st.end(); ++form_it) #define form3(k, v, st) \ for (auto &&form_it = st.begin(); form_it != st.end(); ++form_it) #define form4(k, v, st, r) \ for (auto &&form_it = st.begin(); form_it != st.end() && (*form_it).fi < r; \ ++form_it) #define form5(k, v, st, l, r) \ for (auto &&form_it = st.lower_bound(l); \ form_it != st.end() && (*form_it).fi < r; ++form_it) #define o_form(a, b, c, d, e, name, ...) name #define fors1(st) \ for (auto &&fors_it = st.begin(); fors_it != st.end(); ++fors_it) #define fors2(v, st) \ for (auto &&fors_it = st.begin(); fors_it != st.end(); ++fors_it) #define fors3(v, st, r) \ for (auto &&fors_it = st.begin(); fors_it != st.end() && (*fors_it) < r; \ ++fors_it) #define fors4(v, st, l, r) \ for (auto &&fors_it = st.lower_bound(l); \ fors_it != st.end() && (*fors_it) < r; ++fors_it) #define o_fors(a, b, c, d, e, name, ...) name #define forslr1(st, a, b) \ for (auto &&forslr_it = st.begin(); forslr_it != st.end(); ++forslr_it) #define forslr2(v, st, a, b) \ for (auto &&forslr_it = st.begin(); forslr_it != st.end(); ++forslr_it) #define forslr3(v, st, r, a, b) \ for (auto &&forslr_it = st.begin(); \ forslr_it != st.end() && (*forslr_it) < r; ++forslr_it) #define forslr4(v, st, l, r, a, b) \ for (auto &&forslr_it = st.lower_bound(l); \ forslr_it != st.end() && (*forslr_it) < r; ++forslr_it) #define o_forslr(a, b, c, d, e, f, g, name, ...) name template <class U> vector<U> to1d(vector<U> &a) { return a; } template <class U> vector<U> to1d(V<V<U>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) res.push_back(a2); return res; } template <class U> vector<U> to1d(V<V<V<U>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) res.push_back(a3); return res; } template <class U> vector<U> to1d(V<V<V<V<U>>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) for (auto &&a4 : a3) res.push_back(a4); return res; } template <class U> vector<U> to1d(V<V<V<V<V<U>>>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) for (auto &&a4 : a3) for (auto &&a5 : a4) res.push_back(a5); return res; } template <class U> vector<U> to1d(V<V<V<V<V<V<U>>>>>> &a) { vector<U> res; for (auto &&a1 : a) for (auto &&a2 : a1) for (auto &&a3 : a2) for (auto &&a4 : a3) for (auto &&a5 : a4) for (auto &&a6 : a5) res.push_back(a6); return res; } #define fora_init_2(a, A) ; #define fora_init_4(a, b, A, B) \ auto &&a = A[fora_i]; \ auto &&b = B[fora_i]; #define fora_init_6(a, b, c, A, B, C) \ auto &&a = A[fora_i]; \ auto &&b = B[fora_i]; \ auto &&c = C[fora_i]; #define fora_init_8(a, b, c, d, A, B, C, D) \ auto &&a = A[fora_i]; \ auto &&b = B[fora_i]; \ auto &&c = C[fora_i]; \ auto &&d = D[fora_i]; #define o_fora_init(a, b, c, d, e, f, g, h, name, ...) name #define fora_init(...) \ o_fora_init(__VA_ARGS__, fora_init_8, a, fora_init_6, a, fora_init_4, a, \ fora_init_2)(__VA_ARGS__) #define forr_init_2(a, A) auto &&a = A[forr_i]; #define forr_init_4(a, b, A, B) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; #define forr_init_6(a, b, c, A, B, C) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; \ auto &&c = C[forr_i]; #define forr_init_8(a, b, c, d, A, B, C, D) \ auto &&a = A[forr_i]; \ auto &&b = B[forr_i]; \ auto &&c = C[forr_i]; \ auto &&d = D[forr_i]; #define o_forr_init(a, b, c, d, e, f, g, h, name, ...) name #define forr_init(...) \ o_forr_init(__VA_ARGS__, forr_init_8, a, forr_init_6, a, forr_init_4, a, \ forr_init_2)(__VA_ARGS__) #define forp_init(k, v, ...) \ auto &&k = (*forp_it).fi; \ auto &&v = (*forp_it).se; #define form_init(k, v, ...) \ auto &&k = (*form_it).fi; \ auto &&v = (*form_it).se; #define fors_init(v, ...) auto &&v = (*fors_it); #define forlr_init(a, A, ngl, ngr) \ auto a = A[forlr_i]; \ auto prev = forlr_i ? A[forlr_i - 1] : ngl; \ auto next = forlr_i + 1 < rep2lim ? A[forlr_i + 1] : ngr; #define forslr_init4(a, A, ngl, ngr) \ auto a = (*forslr_it); \ auto prev = (forslr_it != A.begin()) ? (*std::prev(forslr_it)) : ngl; \ auto next = (forslr_it != std::prev(A.end())) ? (*std::next(forslr_it)) : ngr; #define forslr_init5(a, A, r, ngl, ngr) \ auto a = (*forslr_it); \ auto prev = (forslr_it != A.begin()) ? (*std::prev(forslr_it)) : ngl; \ auto next = (forslr_it != std::prev(A.end())) ? (*std::next(forslr_it)) : ngr; #define forslr_init6(a, A, l, r, ngl, ngr) \ auto a = (*forslr_it); \ auto prev = (forslr_it != A.begin()) ? (*std::prev(forslr_it)) : ngl; \ auto next = (forslr_it != std::prev(A.end())) ? (*std::next(forslr_it)) : ngr; #define o_forslr_init(a, b, c, d, e, f, name, ...) name #define forslr_init(...) \ o_forslr_init(__VA_ARGS__, forslr_init6, forslr_init5, \ forslr_init4)(__VA_ARGS__); #define fora_2(a, A) for (auto &&a : A) #define fora_4(a, b, A, B) rep(fora_i, sz(A)) #define fora_6(a, b, c, A, B, C) rep(fora_i, sz(A)) #define fora_8(a, b, c, d, A, B, C, D) rep(fora_i, sz(A)) #define o_fora(a, b, c, d, e, f, g, h, name, ...) name #define forr_2(a, A) rer(forr_i, sz(A) - 1) #define forr_4(a, b, A, B) rer(forr_i, sz(A) - 1) #define forr_6(a, b, c, A, B, C) rer(forr_i, sz(A) - 1) #define forr_8(a, b, c, d, A, B, C, D) rer(forr_i, sz(A) - 1) #define o_forr(a, b, c, d, e, f, g, h, name, ...) name //↑@オーバーロード隔離 // rep系はインデックス、for系は中身 #define rep(...) o_rep(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__) #define rer(...) o_rep(__VA_ARGS__, rer4, rer3, rer2, )(__VA_ARGS__) // char用のrep #define repc(i, m, n) for (char i = m, repc3lim = n; i < repc3lim; ++i) // i,j,k...をnまで見る #define reps(...) o_reps(__VA_ARGS__, reps4, reps3, reps2, reps1, )(__VA_ARGS__) #define repss(...) \ o_repss(__VA_ARGS__, repss5, a, repss4, a, repss3, a, repss2, a, \ repss1)(__VA_ARGS__) // vectorのindexを走査する // repv(i,j,vvi) #define repv(...) \ o_repv(__VA_ARGS__, repv_5, repv_4, repv_3, repv_2, repv_1, )(__VA_ARGS__) #define rerv(i, A) for (int i = sz(A) - 1; i >= 0; i--) // repvn(dp) nは次元 #define repv1(a) repv(i, a) #define repv2(a) repv(i, j, a) #define repv3(a) repv(i, j, k, a) #define repv4(a) repv(i, j, k, l, a) #define fora(...) \ o_fora(__VA_ARGS__, fora_8, a, fora_6, a, fora_4, a, fora_2)(__VA_ARGS__) #define forr(...) \ o_forr(__VA_ARGS__, forr_8, a, forr_6, a, forr_4, a, forr_2)(__VA_ARGS__) //#define forr(v, a) for(auto&& v : makeReversedRange(a)) //参照を取らない #define forv(a, b) for (auto a : to1d(b)) //インデックスを前後含めて走査 #define ring(i, s, len) \ for (int i = s, prev = (s == 0) ? len - 1 : s - 1, \ next = (s == len - 1) ? 0 : s + 1, cou = 0; \ cou < len; \ cou++, prev = i, i = next, next = (next == len - 1) ? 0 : next + 1) //値と前後を見る #define ringv(v, d) \ index_ = 0; \ for (auto prev = d[sz(d) - 1], next = (int)d.size() > 1 ? d[1] : d[0], \ v = d[0]; \ index_ < sz(d); index_++, prev = v, v = next, \ next = (index_ >= sz(d) - 1 ? d[0] : d[index_ + 1])) // 左右をnext prevで見る 0の左と nの右 #define forlr(v, d, banpei_l, banpei_r) rep(forlr_i, sz(d)) #define form(...) \ o_form(__VA_ARGS__, form5, form4, form3, form2, form1)(__VA_ARGS__) #define fors(...) \ o_fors(__VA_ARGS__, fors5, fors4, fors3, fors2, fors1)(__VA_ARGS__) #define forslr(...) \ o_forslr(__VA_ARGS__, forslr5, forslr4, forslr3, forslr2, \ forslr1)(__VA_ARGS__) #define forp(k, v, st) \ for (auto &&forp_it = st.begin(); forp_it != st.end(); ++forp_it) //マクロ 定数 #define k3 1010 #define k4 10101 #define k5 101010 #define k6 1010101 #define k7 10101010 const ll inf = (ll)1e9 + 100; const ll linf = (ll)1e18 + 100; const char infc = '{'; const string infs = "{"; const double eps = 1e-9; const double PI = 3.1415926535897932384626433832795029L; //マクロ省略形 関数等 #define arsz(a) (sizeof(a) / sizeof(a[0])) #define sz(a) ((ll)(a).size()) #define mp make_pair #define pb pop_back #define pf push_front #define eb emplace_back #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() constexpr bool ev(ll a) { return !(a & 1); } constexpr bool od(ll a) { return (a & 1); } //@拡張系 こう出来るべきというもの //埋め込み 存在を意識せずに機能を増やされているもの namespace std { template <> class hash<std::pair<signed, signed>> { public: size_t operator()(const std::pair<signed, signed> &x) const { return hash<ll>()(((ll)x.first << 32) | x.second); } }; template <> class hash<std::pair<ll, ll>> { public : /*大きいllが渡されると、<<32でオーバーフローするがとりあえず問題ないと判断*/ size_t operator()(const std::pair<ll, ll> &x) const { return hash<ll>()(((ll)x.first << 32) | x.second); } }; } // namespace std // stream まとめ /*@formatter:on*/ istream &operator>>(istream &iss, P &a) { iss >> a.first >> a.second; return iss; } template <typename T> istream &operator>>(istream &iss, vector<T> &vec) { for (T &x : vec) iss >> x; return iss; } template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> p) { os << p.fi << " " << p.se; return os; } ostream &operator<<(ostream &os, T p) { os << p.f << " " << p.s << " " << p.t; return os; } ostream &operator<<(ostream &os, F p) { os << p.a << " " << p.b << " " << p.c << " " << p.d; return os; } template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) { for (ll i = 0; i < vec.size(); ++i) os << vec[i] << (i + 1 == vec.size() ? "" : " "); return os; } template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) { for (ll i = 0; i < vec.size(); ++i) { for (ll j = 0; j < vec[i].size(); ++j) { os << vec[i][j] << " "; } os << endl; } return os; } template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &m) { for (auto &&v : m) os << v; return os; } template <class T> ostream &operator<<(ostream &os, set<T> s) { fora(v, s) { os << v << " "; } return os; } template <class T> ostream &operator<<(ostream &os, deque<T> a) { fora(v, a) os << v << " "; return os; } ostream &operator<<(ostream &os, vector<vector<char>> &vec) { rep(h, sz(vec)) { rep(w, sz(vec[0])) { os << vec[h][w]; } os << endl; } return os; } // template<class T,class U>ostream &operator<<(ostream &os, vector<pair<T,U>>& // a) {fora(v,a)os<<v<<endl;return os;} /*@formatter:off*/ template <typename W, typename H> void resize(W &vec, const H head) { vec.resize(head); } template <typename W, typename H, typename... T> void resize(W &vec, const H &head, const T... tail) { vec.resize(head); for (auto &v : vec) resize(v, tail...); } // 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) template <class T, class U> void replace(vector<T> &a, T key, U v) { replace(a.begin(), a.end(), key, v); } void replace(str &a, char key, str v) { if (v == "") a.erase(remove(all(a), key), a.end()); } void replace(str &a, char key, char v) { replace(all(a), key, v); } // keyと同じかどうか01で置き換える template <class T, class U> void replace(vector<T> &a, U k) { rep(i, sz(a)) a[i] = a[i] == k; } template <class T, class U> void replace(vector<vector<T>> &a, U k) { rep(i, sz(a)) rep(j, sz(a[0])) a[i][j] = a[i][j] == k; } template <class T> void replace(T &a) { replace(a, '#'); } void replace(str &a, str key, str v) { stringstream t; ll kn = sz(key); std::string::size_type Pos(a.find(key)); ll l = 0; while (Pos != std::string::npos) { t << a.substr(l, Pos - l); t << v; l = Pos + kn; Pos = a.find(key, Pos + kn); } t << a.substr(l, sz(a) - l); a = t.str(); } template <class T> bool includes(vector<T> &a, vector<T> &b) { vi c = a; vi d = b; sort(all(c)); sort(all(d)); return includes(all(c), all(d)); } template <class T> bool is_permutation(vector<T> &a, vector<T> &b) { return is_permutation(all(a), all(b)); } template <class T> bool next_permutation(vector<T> &a) { return next_permutation(all(a)); } void iota(vector<ll> &ve, ll s, ll n) { ve.resize(n); iota(all(ve), s); } vi iota(ll s, ll len) { vi ve(len); iota(all(ve), s); return ve; } template <class A, class B> auto vtop(vector<A> &a, vector<B> &b) { assert(sz(a) == sz(b)); /*stringを0で初期化できない */ vector<pair<A, B>> res; rep(i, sz(a)) res.eb(a[i], b[i]); return res; } template <class A, class B> void ptov(vector<pair<A, B>> &p, vector<A> &a, vector<B> &b) { a.resize(sz(p)), b.resize(sz(p)); rep(i, sz(p)) a[i] = p[i].fi, b[i] = p[i].se; } template <class A, class B, class C> auto vtot(vector<A> &a, vector<B> &b, vector<C> &c) { assert(sz(a) == sz(b) && sz(b) == sz(c)); vector<T2<A, B, C>> res; rep(i, sz(a)) res.eb(a[i], b[i], c[i]); return res; } template <class A, class B, class C, class D> auto vtof(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { assert(sz(a) == sz(b) && sz(b) == sz(c) && sz(c) == sz(d)); vector<F2<A, B, C, D>> res; rep(i, sz(a)) res.eb(a[i], b[i], c[i], d[i]); return res; } enum pcomparator { fisi, fisd, fdsi, fdsd, sifi, sifd, sdfi, sdfd }; enum tcomparator { fisiti, fisitd, fisdti, fisdtd, fdsiti, fdsitd, fdsdti, fdsdtd, fitisi, fitisd, fitdsi, fitdsd, fdtisi, fdtisd, fdtdsi, fdtdsd, sifiti, sifitd, sifdti, sifdtd, sdfiti, sdfitd, sdfdti, sdfdtd, sitifi, sitifd, sitdfi, sitdfd, sdtifi, sdtifd, sdtdfi, sdfdfd, tifisi, tifisd, tifdsi, tifdsd, tdfisi, tdfisd, tdfdsi, tdfdsd, tisifi, tisifd, tisdfi, tisdfd, tdsifi, tdsifd, tdsdfi, tdsdfd }; template <class A, class B> void sort(vector<pair<A, B>> &a, pcomparator type) { typedef pair<A, B> U; if (type == fisi) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se < r.se; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se > r.se; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se < r.se; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se > r.se; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi < r.fi; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi > r.fi; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi < r.fi; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi > r.fi; }); }; template <class U> void sort(vector<U> &a, pcomparator type) { if (type == fisi) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f > r.f; }); }; template <class A, class B, class C, class D> void sort(vector<F2<A, B, C, D>> &a, pcomparator type) { typedef F2<A, B, C, D> U; if (type == fisi) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a > r.a; }); }; template <class U> void sort(vector<U> &a, tcomparator type) { if (type == 0) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t < r.t; }); else if (type == 1) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t > r.t; }); else if (type == 2) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t < r.t; }); else if (type == 3) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t > r.t; }); else if (type == 4) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t < r.t; }); else if (type == 5) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t > r.t; }); else if (type == 6) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t < r.t; }); else if (type == 7) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t > r.t; }); else if (type == 8) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s < r.s; }); else if (type == 9) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s > r.s; }); else if (type == 10) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s < r.s; }); else if (type == 11) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s > r.s; }); else if (type == 12) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s < r.s; }); else if (type == 13) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s > r.s; }); else if (type == 14) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s < r.s; }); else if (type == 15) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s > r.s; }); else if (type == 16) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t < r.t; }); else if (type == 17) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t > r.t; }); else if (type == 18) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t < r.t; }); else if (type == 19) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t > r.t; }); else if (type == 20) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t < r.t; }); else if (type == 21) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t > r.t; }); else if (type == 22) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t < r.t; }); else if (type == 23) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t > r.t; }); else if (type == 24) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f < r.f; }); else if (type == 25) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f > r.f; }); else if (type == 26) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f < r.f; }); else if (type == 27) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f > r.f; }); else if (type == 28) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f < r.f; }); else if (type == 29) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f > r.f; }); else if (type == 30) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f < r.f; }); else if (type == 31) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f > r.f; }); else if (type == 32) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == 33) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == 34) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == 35) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == 36) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == 37) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == 38) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == 39) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == 40) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == 41) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == 42) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == 43) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f > r.f; }); else if (type == 44) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == 45) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == 46) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == 47) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f > r.f; }); } template <class A, class B, class C, class D> void sort(vector<F2<A, B, C, D>> &a, tcomparator type) { typedef F2<A, B, C, D> U; if (type == 0) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c < r.c; }); else if (type == 1) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c > r.c; }); else if (type == 2) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c < r.c; }); else if (type == 3) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c > r.c; }); else if (type == 4) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c < r.c; }); else if (type == 5) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c > r.c; }); else if (type == 6) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c < r.c; }); else if (type == 7) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c > r.c; }); else if (type == 8) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b < r.b; }); else if (type == 9) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b > r.b; }); else if (type == 10) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b < r.b; }); else if (type == 11) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b > r.b; }); else if (type == 12) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b < r.b; }); else if (type == 13) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b > r.b; }); else if (type == 14) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b < r.b; }); else if (type == 15) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b > r.b; }); else if (type == 16) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c < r.c; }); else if (type == 17) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c > r.c; }); else if (type == 18) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c < r.c; }); else if (type == 19) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c > r.c; }); else if (type == 20) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c < r.c; }); else if (type == 21) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c > r.c; }); else if (type == 22) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c < r.c; }); else if (type == 23) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c > r.c; }); else if (type == 24) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a < r.a; }); else if (type == 25) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a > r.a; }); else if (type == 26) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a < r.a; }); else if (type == 27) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a > r.a; }); else if (type == 28) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a < r.a; }); else if (type == 29) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a > r.a; }); else if (type == 30) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a < r.a; }); else if (type == 31) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a > r.a; }); else if (type == 32) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == 33) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == 34) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == 35) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == 36) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == 37) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == 38) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == 39) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == 40) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == 41) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == 42) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == 43) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a > r.a; }); else if (type == 44) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == 45) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == 46) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == 47) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a > r.a; }); } void sort(string &a) { sort(all(a)); } template <class T> void sort(vector<T> &a) { sort(all(a)); } // P l, P rで f(P) の形で渡す template <class U, class F> void sort(vector<U> &a, F f) { sort(all(a), [&](U l, U r) { return f(l) < f(r); }); }; template <class T> void rsort(vector<T> &a) { sort(all(a), greater<T>()); }; template <class U, class F> void rsort(vector<U> &a, F f) { sort(all(a), [&](U l, U r) { return f(l) > f(r); }); }; // F = T<T> //例えばreturn p.fi + p.se; template <class A, class B> void sortp(vector<A> &a, vector<B> &b) { auto c = vtop(a, b); sort(c); rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se; } template <class A, class B, class F> void sortp(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); sort(c, f); rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se; } template <class A, class B> void rsortp(vector<A> &a, vector<B> &b) { auto c = vtop(a, b); rsort(c); rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second; } template <class A, class B, class F> void rsortp(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); rsort(c, f); rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second; } template <class A, class B, class C> void sortt(vector<A> &a, vector<B> &b, vector<C> &c) { auto d = vtot(a, b, c); sort(d); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class F> void sortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); sort(d, f); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C> void rsortt(vector<A> &a, vector<B> &b, vector<C> &c) { auto d = vtot(a, b, c); rsort(d); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class F> void rsortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); rsort(d, f); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class D> void sortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { auto e = vtof(a, b, c, d); sort(e); rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d; } template <class A, class B, class C, class D> void rsortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { auto e = vtof(a, b, c, d); rsort(e); rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d; } // sortindex 元のvectorはソートしない template <class T> vi sorti(vector<T> &a) { auto b = a; vi ind = iota(0, sz(a)); sortp(b, ind); return ind; } /*indexの分で型が変わるためpcomparatorが必要*/ template <class T> vi sorti(vector<T> &a, pcomparator f) { auto b = a; vi ind = iota(0, sz(a)); sortp(b, ind, f); return ind; } template <class T, class F> vi sorti(vector<T> &a, F f) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(a[x]) < f(a[y]); }); return ind; } template <class T> vi rsorti(vector<T> &a) { auto b = a; vi ind = iota(0, sz(a)); rsortp(b, ind); return ind; } template <class T, class F> vi rsorti(vector<T> &a, F f) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(a[x]) > f(a[y]); }); return ind; } template <class A, class B, class F> vi sortpi(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(c[x]) < f(c[y]); }); return ind; } template <class A, class B> vi sortpi(vector<A> &a, vector<B> &b, pcomparator f) { vi ind = iota(0, sz(a)); auto c = a; auto d = b; sortt(c, d, ind, f); return ind; } template <class A, class B> vi sortpi(vector<A> &a, vector<B> &b) { return sortpi(a, b, fisi); }; template <class A, class B, class F> vi rsortpi(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(c[x]) > f(c[y]); }); return ind; } template <class A, class B> vi rsortpi(vector<A> &a, vector<B> &b) { return sortpi(a, b, fdsd); }; template <class A, class B, class C, class F> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(d[x]) < f(d[y]); }); return ind; } template <class A, class B, class C> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, pcomparator f) { vi ind = iota(0, sz(a)); auto d = vtof(a, b, c, ind); sort(d, f); rep(i, sz(a)) ind[i] = d[i].d; return ind; } template <class A, class B, class C> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { if (a[x] == a[y]) { if (b[x] == b[y]) return c[x] < c[y]; else return b[x] < b[y]; } else { return a[x] < a[y]; } }); return ind; } template <class A, class B, class C, class F> vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(d[x]) > f(d[y]); }); return ind; } template <class A, class B, class C> vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { if (a[x] == a[y]) { if (b[x] == b[y]) return c[x] > c[y]; else return b[x] > b[y]; } else { return a[x] > a[y]; } }); return ind; } template <class T> void sort2(vector<vector<T>> &a) { for (ll i = 0, n = a.size(); i < n; ++i) sort(a[i]); } template <class T> void rsort2(vector<vector<T>> &a) { for (ll 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 W, typename T> void fill(W &xx, const T vall) { xx = vall; } template <typename W, typename T> void fill(vector<W> &vecc, const T vall) { for (auto &&vx : vecc) fill(vx, vall); } template <typename W, typename T> void fill(vector<W> &xx, ll len, const T v) { rep(i, len) xx[i] = v; } template <typename W, typename T> void fill(vector<vector<W>> &xx, int sh, int th, int sw, int tw, T v) { rep(h, sh, th) rep(w, sw, tw) xx[h][w] = v; } template <class T, class U> void fill(vector<T> &a, vi &ind, U val) { fora(v, ind) a[v] = val; } template <class W, class T> void fill(mvec<W> &xx, const T v) { fora(x, xx) fill(x, v); } template <typename A, size_t N> A sum(A (&a)[N]) { A res = 0; rep(i, N) res += a[i]; return res; } template <typename A, size_t N, size_t O> A sum(A (&a)[N][O]) { A res = 0; rep(i, N) rep(j, O) res += a[i][j]; return res; } template <typename A, size_t N, size_t O, size_t P> A sum(A (&a)[N][O][P]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) res += a[i][j][k]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A sum(A (&a)[N][O][P][Q]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) res += a[i][j][k][l]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A sum(A (&a)[N][O][P][Q][R]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) res += a[i][j][k][l][m]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A sum(A (&a)[N][O][P][Q][R][S]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) rep(n, S) res += a[i][j][k][l][m][n]; return res; } //@汎用便利関数 入力 ll in() { ll ret; cin >> ret; return ret; } string sin() { string ret; cin >> ret; return ret; } template <class T> void in(T &head) { cin >> head; } template <class T, class... U> void in(T &head, U &...tail) { cin >> head; in(tail...); } #define o_din(o1, o2, o3, o4, o5, o6, name, ...) name #define din1(a) \ ll a; \ cin >> a #define din2(a, b) \ ll a, b; \ cin >> a >> b #define din3(a, b, c) \ ll a, b, c; \ cin >> a >> b >> c #define din4(a, b, c, d) \ ll a, b, c, d; \ cin >> a >> b >> c >> d #define din5(a, b, c, d, e) \ ll a, b, c, d, e; \ cin >> a >> b >> c >> d >> e #define din6(a, b, c, d, e, f) \ ll a, b, c, d, e, f; \ cin >> a >> b >> c >> d >> e >> f #define din(...) \ o_din(__VA_ARGS__, din6, din5, din4, din3, din2, din1)(__VA_ARGS__) #define o_dins(o1, o2, o3, o4, o5, o6, name, ...) name #define dins1(a) \ str a; \ cin >> a #define dins2(a, b) \ str a, b; \ cin >> a >> b #define dins3(a, b, c) \ str a, b, c; \ cin >> a >> b >> c #define dins4(a, b, c, d) \ str a, b, c, d; \ cin >> a >> b >> c >> d #define dins5(a, b, c, d, e) \ str a, b, c, d, e; \ cin >> a >> b >> c >> d >> e #define dins6(a, b, c, d, e, f) \ str a, b, c, d, e, f; \ cin >> a >> b >> c >> d >> e >> f #define dins(...) \ o_dins(__VA_ARGS__, dins6, dins5, dins4, dins3, dins2, dins1)(__VA_ARGS__) #define o_dind(o1, o2, o3, o4, name, ...) name #define din1d(a) \ din1(a); \ a-- #define din2d(a, b) \ din2(a, b); \ a--, b-- #define din3d(a, b, c) \ din3(a, b, c); \ a--, b--, c-- #define din4d(a, b, c, d) \ din4(a, b, c, d); \ a--, b--, c--, d-- #define dind(...) o_dind(__VA_ARGS__, din4d, din3d, din2d, din1d)(__VA_ARGS__) template <class T> void out2(T &&head) { cout << head; } template <class T, class... U> void out2(T &&head, U &&...tail) { cout << head << " "; out2(tail...); } template <class T, class... U> void out(T &&head, U &&...tail) { cout << head << " "; out2(tail...); cout << "" << endl; } template <class T> void out(T &&head) { cout << head << endl; } void out() { cout << "" << endl; } #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; } /*@formatter:on*/ 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]; if (typeid(a[i]) == typeid(P)) { ss << endl; } else { ss << " "; } } return ss.str(); } /*@formatter:off*/ 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(); } 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 = 12; H = min(H, sz(a)); rep(i, H) { ss << endl; ss << out_m2(a[i], W, U, i); } return ss.str(); } template <class T, size_t N> string out_m2(T (&a)[N]) { vector<T> b; resize(b, N); rep(i, N) { b[i] = a[i]; } return out_m2(b); } template <class T, size_t N, size_t M> string out_m2(T (&a)[N][M]) { vector<vector<T>> b; resize(b, N, M); rep(i, N) { rep(j, M) { b[i][j] = a[i][j]; } } return out_m2(b); } template <class T, size_t N, size_t M, size_t O> string out_m2(T (&a)[N][M][O]) { vector<vector<vector<T>>> b; resize(b, N, M, O); rep(i, N) { rep(j, M) { rep(k, O) { b[i][j][k] = a[i][j][k]; } } } return out_m2(b); } string out_m2(int a) { stringstream ss; ss << a; return ss.str(); } /*@formatter:on*/ template <class T> string out_m2(mvec<mvec<T>> &a, ll H = inf, ll W = inf, int key = inf) { H = min({H, sz(a), 6ll}); W = min({W, sz(a[0]), 6ll}); stringstream ss; ss << endl; // if (key == inf)ss << " *|"; else ss << " " << key << "|"; if (key == inf) ss << " *|"; else { ss << std::right << std::setw(2) << key; ss << "|"; } rep(w, -W, W) ss << std::right << std::setw(4) << w; ss << "" << endl; rep(w, W * 8 + 3) ss << "_"; ss << "" << endl; rep(h, -H, H) { ss << std::right << std::setw(2) << h << "|"; int NW = min(sz(a[h]), 6ll); rep(w, -NW, NW) { if (abs(a[h][w]) == linf) ss << " e" << ""; else ss << std::right << std::setw(4) << a[h][w]; } ss << "" << endl; } return ss.str(); } /*@formatter:on*/ template <class T> string out_m2(mvec<mvec<mvec<T>>> &a, ll H = inf, ll W = inf, ll U = inf) { stringstream ss; if (H == inf) H = 6; H = min(H, sz(a)); rep(i, -H, H) { ss << endl; ss << out_m2(a[i], W, U, i); } return ss.str(); } /*@formatter:off*/ template <class T> string out_m2(T &a) { stringstream ss; ss << a; return ss.str(); } /*@formatter:on*/ 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(); } /*@formatter:off*/ 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(); } 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 template <class T> void outl(vector<T> &a, int n = inf) { rep(i, min(n, sz(a))) cout << a[i] << endl; } //テーブルをスペースなしで出力 template <class T> void outt(vector<vector<T>> &a) { rep(i, sz(a)) { rep(j, sz(a[i])) { cout << a[i][j]; } cout << endl; } } // int型をbit表記で出力 void outb(int a) { cout << bitset<20>(a) << endl; } template <class T> void na(vector<T> &a, ll n) { a.resize(n); rep(i, n) cin >> a[i]; } #define dna(a, n) \ vi a(n); \ rep(dnai, n) cin >> a[dnai]; #define dnad(a, n) \ vi a(n); \ rep(dnai, n) cin >> a[dnai], a[dnai]--; template <class T> void nao(vector<T> &a, ll n) { a.resize(n + 1); a[0] = 0; rep(i, n) cin >> a[i + 1]; } template <class T> void naod(vector<T> &a, ll n) { a.resize(n + 1); a[0] = 0; rep(i, n) cin >> a[i + 1], a[i + 1]--; } template <class T> void nad(vector<T> &a, ll n) { a.resize(n); rep(i, n) cin >> a[i], a[i]--; } template <class T, class U> void na2(vector<T> &a, vector<U> &b, ll n) { a.resize(n); b.resize(n); rep(i, n) cin >> a[i] >> b[i]; } #define dna2(a, b, n) \ vi a(n), b(n); \ rep(dna2i, n) cin >> a[dna2i] >> b[dna2i]; template <class T, class U> void nao2(vector<T> &a, vector<U> &b, ll n) { a.resize(n + 1); b.resize(n + 1); a[0] = b[0] = 0; rep(i, n) cin >> a[i + 1] >> b[i + 1]; } #define dna2d(a, b, n) \ vi a(n), b(n); \ rep(dna2di, n) { \ cin >> a[dna2di] >> b[dna2di]; \ a[dna2di]--, b[dna2di]--; \ } template <class T, class U> void na2d(vector<T> &a, vector<U> &b, ll n) { a.resize(n); b.resize(n); rep(i, n) cin >> a[i] >> b[i], a[i]--, b[i]--; } template <class T, class U, class W> void na3(vector<T> &a, vector<U> &b, vector<W> &c, ll n) { a.resize(n); b.resize(n); c.resize(n); rep(i, n) cin >> a[i] >> b[i] >> c[i]; } #define dna3(a, b, c, n) \ vi a(n), b(n), c(n); \ rep(dna3i, n) cin >> a[dna3i] >> b[dna3i] >> c[dna3i]; template <class T, class U, class W> void na3d(vector<T> &a, vector<U> &b, vector<W> &c, ll 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 dna3d(a, b, c, n) \ vi a(n), b(n), c(n); \ rep(dna3di, n) { \ cin >> a[dna3di] >> b[dna3di] >> c[dna3di]; \ a[dna3di]--, b[dna3di]--, c[dna3di]--; \ } template <class T, class U, class W, class X> void na4(vector<T> &a, vector<U> &b, vector<W> &c, vector<X> &d, ll n) { a.resize(n); b.resize(n); c.resize(n); d.resize(n); rep(i, n) cin >> a[i] >> b[i] >> c[i] >> d[i]; } #define dna4(a, b, c, d, n) \ vi a(n), b(n), c(n), d(n); \ rep(dna4i, n) cin >> a[dna4i] >> b[dna4i] >> c[dna4i] >> d[dna4i]; #define dna4d(a, b, c, d, n) \ vi a(n), b(n), c(n), d(n); \ rep(dna4i, n) cin >> a[dna4i] >> b[dna4i] >> c[dna4i] >> d[dna4i], \ --a[dna4i], --b[dna4i], --c[dna4i], --d[dna4i]; #define nt(a, h, w) \ resize(a, h, w); \ rep(nthi, h) rep(ntwi, w) cin >> a[nthi][ntwi]; #define ntd(a, h, w) \ resize(a, h, w); \ rep(ntdhi, h) rep(ntdwi, w) cin >> a[ntdhi][ntdwi], a[ntdhi][ntdwi]--; #define ntp(a, h, w) \ resize(a, h + 2, w + 2); \ fill(a, '#'); \ rep(ntphi, 1, h + 1) rep(ntpwi, 1, w + 1) cin >> a[ntphi][ntpwi]; //デバッグ #define sp << " " << #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' //@formatter:off //よく使うクラス、構造体 // graphでredefineしているため、書き換えたら向こうも書き換える struct unionfind { vector<ll> par; vector<ll> siz; vector<ll> es; ll n, trees; //連結グループの数(親の種類) unionfind(ll n) : n(n), trees(n) { par.resize(n); siz.resize(n); es.resize(n); for (ll i = 0; i < n; i++) { par[i] = i; siz[i] = 1; } } ll root(ll x) { if (par[x] == x) { return x; } else { return par[x] = root(par[x]); } } ll operator()(ll x) { return root(x); } bool unite(ll x, ll y) { x = root(x); y = root(y); es[x]++; if (x == y) return false; if (siz[x] > siz[y]) swap(x, y); trees--; par[x] = y; siz[y] += siz[x]; es[y] += es[x]; return true; } bool same(ll x, ll y) { return root(x) == root(y); } ll size(ll x) { return siz[root(x)]; } ll esize(ll x) { return es[root(x)]; } vi sizes() { vi cou(n); vi ret; ret.reserve(n); rep(i, n) { cou[root(i)]++; } rep(i, n) { if (cou[i]) ret.push_back(cou[i]); } return ret; } //つながりを無向グラフと見なし、xが閉路に含まれるか判定 bool close(ll x) { return esize(x) >= size(x); } vector<vi> sets() { vi ind(n, -1); ll i = 0; vvi(res, trees); rep(j, n) { ll r = root(j); if (ind[r] == -1) ind[r] = i++; res[ind[r]].push_back(j); } rep(i, trees) { ll r = root(res[i][0]); if (res[i][0] == r) continue; rep(j, 1, sz(res[i])) { if (res[i][j] == r) { swap(res[i][0], res[i][j]); break; } } } return res; } }; //@formatter:off using bll = __int128; using u32 = unsigned; using u64 = unsigned long long; using u128 = __uint128_t; std::ostream &operator<<(std::ostream &dest, __int128_t value) { std::ostream::sentry s(dest); if (s) { __uint128_t tmp = value < 0 ? -value : value; char buffer[128]; char *d = std::end(buffer); do { --d; *d = "0123456789"[tmp % 10]; tmp /= 10; } while (tmp != 0); if (value < 0) { --d; *d = '-'; } ll len = std::end(buffer) - d; if (dest.rdbuf()->sputn(d, len) != len) { dest.setstate(std::ios_base::badbit); } } return dest; } //__int128 toi128(string &s) { __int128 ret = 0; for (ll i = 0; i < //s.length(); ++i) if ('0' <= s[i] && s[i] <= '9') ret = 10 * //ret + s[i] - '0'; return ret;} //エラー 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 re() { assert(0 == 1); exit(0); } void tle() { while (inf) cout << inf << endl; } //便利関数 //テスト用 #define rand xor128_ unsigned long xor128_(void) { static unsigned long x = 123456789, y = 362436069, z = 521288629, w = 88675123; unsigned long t; t = (x ^ (x << 11)); x = y; y = z; z = w; return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8))); } char ranc() { return (char)('a' + rand() % 26); } ll rand(ll min, ll max) { assert(min <= max); if (min >= 0 && max >= 0) { return rand() % (max + 1 - min) + min; } else if (max < 0) { return -rand(-max, -min); } else { if (rand() % 2) { return rand(0, max); } else { return -rand(0, -min); } } } vi ranv(ll n, ll min, ll max) { vi v(n); rep(i, n) v[i] = rand(min, max); return v; } str ransu(ll n) { str s; rep(i, n) s += (char)rand('A', 'Z'); return s; } str ransl(ll n) { str s; rep(i, n) s += (char)rand('a', 'z'); return s; } //単調増加 vi ranvinc(ll n, ll min, ll max) { vi v(n); bool bad = 1; while (bad) { bad = 0; v.resize(n); rep(i, n) { if (i && min > max - v[i - 1]) { bad = 1; break; } if (i) v[i] = v[i - 1] + rand(min, max - v[i - 1]); else v[i] = rand(min, max); } } return v; } //便利 汎用 void ranvlr(ll n, ll min, ll max, vi &l, vi &r) { l.resize(n); r.resize(n); rep(i, n) { l[i] = rand(min, max); r[i] = l[i] + rand(0, max - l[i]); } } template <class T> vector<pair<T, int>> run_length(vector<T> &a) { vector<pair<T, int>> 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 <class F> ll mgr(ll ok, ll ng, F f) { bool han = true; if (ok < ng) while (ng - ok > 1) { ll mid = (ok + ng) >> 1; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; deb(mid, han); } else while (ok - ng > 1) { ll mid = (ok + ng) >> 1; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; deb(mid, han); } return ok; } template <class F> dou mgrd(dou ok, dou ng, F f) { bool han = true; if (ok < ng) rep(i, 100) { dou mid = (ok + ng) / 2; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; if (i < 30) deb(mid, han); } else rep(i, 100) { dou mid = (ok + ng) / 2; if (f(mid)) ok = mid, han = true; else ng = mid, han = false; if (i < 30) deb(mid, han); } return ok; } // strを整数として比較 string smax(str &a, str b) { if (sz(a) < sz(b)) { return b; } else if (sz(a) > sz(b)) { return a; } else if (a < b) return b; else return a; } // strを整数として比較 string smin(str &a, str b) { if (sz(a) > sz(b)) { return b; } else if (sz(a) < sz(b)) { return a; } else if (a > b) return b; else return a; } //エラー-1 template <typename W, typename T> ll find(vector<W> &a, int l, const T key) { rep(i, l, sz(a)) if (a[i] == key) return i; return -1; } template <typename W, typename T> ll find(vector<W> &a, const T key) { rep(i, sz(a)) if (a[i] == key) return i; return -1; } template <typename W, typename T> P find(vector<vector<W>> &a, const T key) { rep(i, sz(a)) rep(j, sz(a[0])) if (a[i][j] == key) return mp(i, j); return mp(-1, -1); } template <typename W, typename U> T find(vector<vector<vector<W>>> &a, const U key) { rep(i, sz(a)) rep(j, sz(a[0])) rep(k, sz(a[0][0])) if (a[i][j][k] == key) return mt(i, j, k); return mt(-1, -1, -1); } // stringも書く int find(string &s, const string key) { int klen = sz(key); rep(i, sz(s) - klen + 1) { if (s[i] != key[0]) continue; if (s.substr(i, klen) == key) { return i; } } return -1; } int find(string &s, int l, const string key) { int klen = sz(key); rep(i, l, sz(s) - klen + 1) { if (s[i] != key[0]) continue; if (s.substr(i, klen) == key) { return i; } } return -1; } int find(string &s, const char key) { rep(i, sz(s)) { if (s[i] == key) return i; } return -1; } int find(string &s, int l, const char key) { rep(i, l, sz(s)) { if (s[i] == key) return i; } return -1; } template <typename W, typename T> ll count2(W &a, const T k) { return a == k; } template <typename W, typename T> ll count2(vector<W> &a, const T k) { ll ret = 0; fora(v, a) ret += count2(v, k); return ret; } template <typename W, typename T> ll count(vector<W> &a, const T k) { ll ret = 0; fora(v, a) ret += count2(v, k); return ret; } vi count(vi &a) { int ma = 0; fora(v, a) { if (ma < v) ma = v; } vi res(ma + 1); fora(v, a) { res[v]++; } return res; } ll count(str &a, str k) { ll ret = 0, len = k.length(); auto pos = a.find(k); while (pos != string::npos) pos = a.find(k, pos + len), ++ret; return ret; } vi count(str &a) { vi cou(26); char c = 'a'; if ('A' <= a[0] && a[0] <= 'Z') c = 'A'; rep(i, sz(a))++ cou[a[i] - c]; return cou; } #define couif count_if // algorythm 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)); } template <class U> void rev(vector<vector<U>> &a) { vector<vector<U>> b(sz(a[0]), vector<U>(sz(a))); rep(h, sz(a)) rep(w, sz(a[0])) b[w][h] = a[h][w]; a = b; } void rev(string &a) { reverse(all(a)); } constexpr ll p10[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000ll, 100000000000ll, 1000000000000ll, 10000000000000ll, 100000000000000ll, 1000000000000000ll, 10000000000000000ll, 100000000000000000ll, 1000000000000000000ll}; ll get(ll a, ll keta) { return (a / (ll)pow(10, keta)) % 10; } // 0は0桁 ll keta(ll v) { if (v < p10[9]) { if (v < p10[4]) { if (v < p10[2]) { if (v < p10[1]) { if (v < p10[0]) return 0; else return 1; } else return 2; } else { if (v < p10[3]) return 3; else return 4; } } else { if (v < p10[7]) { if (v < p10[5]) return 5; else if (v < p10[6]) return 6; else return 7; } else { if (v < p10[8]) return 8; else return 9; } } } else { if (v < p10[13]) { if (v < p10[11]) { if (v < p10[10]) return 10; else return 11; } else { if (v < p10[12]) return 12; else return 13; } } else { if (v < p10[15]) { if (v < p10[14]) return 14; else return 15; } else { if (v < p10[17]) { if (v < p10[16]) return 16; else return 17; } else { if (v < p10[18]) return 18; else return 19; } } } } } ll dsum(ll v, ll sin = 10) { ll ret = 0; for (; v; v /= sin) ret += v % sin; return ret; } ll mask10(ll v) { return p10[v] - 1; } //変換系 //[v] := iとなるようなvectorを返す //存在しない物は-1 template <class T> auto keys(T &a) { vector<decltype((a.begin())->fi)> res; for (auto &&k : a) res.push_back(k.fi); return res; } template <class T> auto values(T &a) { vector<decltype((a.begin())->se)> res; for (auto &&k : a) res.push_back(k.se); return res; } template <class T, class U> bool chma(T &a, const U &b) { if (a < b) { a = b; return true; } return false; } template <class T, class U> bool chmi(T &a, const U &b) { if (b < a) { a = b; return true; } return false; } template <class T> T min(T a, signed b) { return a < b ? a : b; } template <class T> T max(T a, signed b) { return a < b ? b : a; } template <class T> T min(T a, T b, T c) { return a >= b ? b >= c ? c : b : a >= c ? c : a; } template <class T> T max(T a, T b, T c) { return a <= b ? b <= c ? c : b : a <= c ? c : a; } template <class T> T min(vector<T> &a) { return *min_element(all(a)); } template <class T> T mini(vector<T> &a) { return min_element(all(a)) - a.begin(); } template <class T> T min(vector<T> &a, ll n) { return *min_element(a.begin(), a.begin() + min(n, sz(a))); } template <class T> T min(vector<T> &a, ll s, ll n) { return *min_element(a.begin() + s, a.begin() + min(n, sz(a))); } template <class T> T max(vector<T> &a) { return *max_element(all(a)); } template <class T, class U> T max(vector<T> &a, vector<U> &b) { return max(*max_element(all(a)), *max_element(all(b))); } template <class T> T maxi(vector<T> &a) { return max_element(all(a)) - a.begin(); } template <class T> T max(vector<T> &a, ll n) { return *max_element(a.begin(), a.begin() + min(n, sz(a))); } template <class T> T max(vector<T> &a, ll s, ll n) { return *max_element(a.begin() + s, a.begin() + min(n, sz(a))); } template <typename A, size_t N> A max(A (&a)[N]) { A res = a[0]; rep(i, N) res = max(res, a[i]); return res; } template <typename A, size_t N, size_t O> A max(A (&a)[N][O]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P> A max(A (&a)[N][O][P]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A max(A (&a)[N][O][P][Q], const T &v) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A max(A (&a)[N][O][P][Q][R]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A max(A (&a)[N][O][P][Q][R][S]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N> A min(A (&a)[N]) { A res = a[0]; rep(i, N) res = min(res, a[i]); return res; } template <typename A, size_t N, size_t O> A min(A (&a)[N][O]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P> A min(A (&a)[N][O][P]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A min(A (&a)[N][O][P][Q], const T &v) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A min(A (&a)[N][O][P][Q][R]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A min(A (&a)[N][O][P][Q][R][S]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <class T> T sum(vector<T> &v, ll s, ll t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += v[i]; return ret; } template <class T> T sum(vector<T> &v, ll t = inf) { return sum(v, 0, t); } template <class T> T sum(vector<vector<T>> &v, int s, int t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<T>> &v, int t = inf) { return sum(v, 0, t); } template <class T> T sum(vector<vector<vector<T>>> &v, int s, int t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<T>>> &v, int t = inf) { return sum(v, 0, t); } template <class T> T sum(vector<vector<vector<vector<T>>>> &v, int s, int t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<vector<T>>>> &v, int t = inf) { return sum(v, 0, t); } template <class T> T sum(vector<vector<vector<vector<vector<T>>>>> &v, int s, int t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<vector<vector<T>>>>> &v, int t = inf) { return sum(v, 0, t); } template <class T> auto sum(priority_queue<T, vector<T>, greater<T>> &r) { auto q = r; T ret = 0; while (sz(q)) { ret += q.top(); q.pop(); } return ret; } template <class T> auto sum(priority_queue<T> &r) { auto q = r; T ret = 0; while (sz(q)) { ret += q.top(); q.pop(); } return ret; } // template<class T, class U, class... W> auto sumn(vector<T> &v, U head, W... // tail) { auto ret = sum(v[0], tail...); rep(i, 1, min(sz(v), head))ret += // sum(v[i], tail...); return ret;} vi v_i(vi &a) { int n = max(a) + 1; vi ret(n, -1); rep(i, sz(a)) { ret[a[i]] = i; } return ret; } void clear(PQ &q) { q = PQ(); } void clear(priority_queue<int> &q) { q = priority_queue<int>(); } template <class T> void clear(queue<T> &q) { while (q.size()) q.pop(); } template <class T> T *negarr(ll size) { T *body = (T *)malloc((size * 2 + 1) * sizeof(T)); return body + size; } template <class T> T *negarr2(ll h, ll w) { double **dummy1 = new double *[2 * h + 1]; double *dummy2 = new double[(2 * h + 1) * (2 * w + 1)]; dummy1[0] = dummy2 + w; for (ll i = 1; i <= 2 * h + 1; ++i) { dummy1[i] = dummy1[i - 1] + 2 * w + 1; } double **a = dummy1 + h; return a; } // 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> vector<T> ima(vector<T> &v) { vector<T> ret = v; rep(i, sz(ret) - 1) chma(ret[i + 1], ret[i]); return ret; } // template<class T> vector<T> rimi(vector<T> &v) { vector<T> ret = v; rer(i, // sz(ret) - 1, 1)chmi(ret[i - 1], ret[i]); return ret;} template<class T> // vector<T> rima(vector<T> &v) { vector<T> ret = v; rer(i, sz(ret) - 1, // 1)chma(ret[i - 1], ret[i]); return ret;} template <class T> struct ruiC { vector<T> rui; ruiC(vector<T> &ru) : rui(ru) {} /*先頭0*/ ruiC() : rui(1, 0) {} T operator()(ll l, ll r) { if (l > r) { cerr << "ruic "; deb(l, r); assert(0); } return rui[r] - rui[l]; } T operator()(int r) { return operator()(0, r); } /*ruiv[]をruic[]に変えた際意味が変わるのがまずいため()と統一*/ /*単体iを返す 累積でないことに注意(seg木との統一でこうしている)*/ // T operator[](ll i) { return rui[i + 1] - rui[i]; } T operator[](ll i) { return rui[i]; } /*0から順に追加される必要がある*/ void operator+=(T v) { rui.push_back(rui.back() + v); } void add(int i, T v) { if (sz(rui) - 1 != i) ole(); operator+=(v); } T back() { return rui.back(); } ll size() { return rui.size(); } auto begin() { return rui.begin(); } auto end() { return rui.end(); } }; template <class T> struct ruimax { template <typename Monoid> struct SegmentTree { /*pairで処理*/ int sz; vector<Monoid> seg; const Monoid M1 = mp(MIN(T), -1); Monoid f(Monoid a, Monoid b) { return max(a, b); } void build(vector<T> &a) { int n = sz(a); sz = 1; while (sz < n) sz <<= 1; seg.assign(2 * sz, M1); rep(i, n) { seg[i + sz] = mp(a[i], i); } for (int k = sz - 1; k > 0; k--) { seg[k] = f(seg[k << 1], seg[(k << 1) | 1]); } } Monoid query(int a, int b) { Monoid L = M1, R = M1; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) L = f(L, seg[a++]); if (b & 1) R = f(seg[--b], R); } return f(L, R); } Monoid operator[](const int &k) const { return seg[k + sz]; } }; private: vector<T> ve; SegmentTree<pair<T, int>> seg; vector<T> rv; vector<T> ri; bool build = false; public: int n; ruimax(vector<T> &a) : ve(a), n(sz(a)) { int index = -1; int ma = MIN(T); rv.resize(n + 1); ri.resize(n + 1); rv[0] = -linf; ri[0] = -1; rep(i, n) { if (chma(ma, a[i])) { index = i; } rv[i + 1] = ma; ri[i + 1] = index; } } T operator()(int l, int r) { if (!(l <= r && 0 <= l && r <= n)) { deb(l, r, n); assert(0); } if (l == 0) { return rv[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).first; } } T operator()(int r = inf) { return operator()(0, min(r, n)); } T operator[](int r) { return operator()(0, r); } T getv(int l, int r) { return operator()(l, r); } T geti(int l, int r) { assert(l <= r && 0 <= l && r <= n); if (l == 0) { return ri[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).second; } } T geti(int r = inf) { return geti(0, min(r, n)); }; T getv(int r = inf) { return getv(0, min(r, n)); }; auto begin() { return rv.begin(); } auto end() { return rv.end(); } }; template <class T> struct ruimin { template <typename Monoid> struct SegmentTree { /*pairで処理*/ int sz; vector<Monoid> seg; const Monoid M1 = mp(MAX(T), -1); Monoid f(Monoid a, Monoid b) { return min(a, b); } void build(vector<T> &a) { int n = sz(a); sz = 1; while (sz < n) sz <<= 1; seg.assign(2 * sz, M1); rep(i, n) { seg[i + sz] = mp(a[i], i); } for (int k = sz - 1; k > 0; k--) { seg[k] = f(seg[k << 1], seg[(k << 1) | 1]); } } Monoid query(int a, int b) { Monoid L = M1, R = M1; for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if (a & 1) L = f(L, seg[a++]); if (b & 1) R = f(seg[--b], R); } return f(L, R); } Monoid operator[](const int &k) const { return seg[k + sz]; } }; private: vector<T> ve; SegmentTree<pair<T, int>> seg; vector<T> rv; vector<T> ri; bool build = false; int n; public: ruimin(vector<T> &a) : ve(a), n(sz(a)) { int index = -1; int mi = MAX(T); rv.resize(n + 1); ri.resize(n + 1); rv[0] = linf; ri[0] = -1; rep(i, n) { if (chmi(mi, a[i])) { index = i; } rv[i + 1] = mi; ri[i + 1] = index; } } T operator()(int l, int r) { assert(l <= r && 0 <= l && r <= n); if (l == 0) { return rv[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).first; } } T operator()(int r = inf) { return operator()(0, min(r, n)); } T operator[](int r) { return operator()(0, r); } T getv(int l, int r) { return operator()(l, r); } T geti(int l, int r) { assert(l <= r && 0 <= l && r <= n); if (l == 0) { return ri[r]; } else { if (!build) seg.build(ve), build = true; return seg.query(l, r).second; } } T geti(int r = inf) { return geti(0, min(r, n)); }; T getv(int r = inf) { return getv(0, min(r, n)); }; auto begin() { return rv.begin(); } auto end() { return rv.end(); } }; 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() { return ruiC<T>(); } template <class T> ruiC<T> ruic(vector<T> &a) { vector<T> ret = ruiv(a); return ruiC<T>(ret); } vvi() ruib(vi &a) { vvi(res, 61, sz(a) + 1); rep(k, 61) { rep(i, sz(a)) { res[k][i + 1] = res[k][i] + ((a[i] >> k) & 1); } } return res; } vector<ruiC<int>> ruibc(vi &a) { vector<ruiC<int>> ret(61); vvi(res, 61, sz(a)); rep(k, 61) { rep(i, sz(a)) { res[k][i] = (a[i] >> k) & 1; } ret[k] = ruic(res[k]); } return 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); } // h query template <class T> vector<T> imoh(vector<vector<T>> &v, int w) { vector<T> ret(sz(v)); rep(h, sz(ret)) { ret[h] = v[h][w]; } rep(i, sz(ret) - 1) { ret[i + 1] += ret[i]; } return ret; } template <class T> vector<T> ruih(vector<vector<T>> &v, int w) { vector<T> ret(sz(v) + 1); rep(h, sz(v)) { ret[h + 1] = v[h][w]; } rep(i, sz(v)) { ret[i + 1] += ret[i]; } return ret; } template <class T> ruiC<T> ruihc(vector<vector<T>> &a, int w) { vector<T> ret = ruih(a, w); return ruiC<T>(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> struct rruiC { vector<T> rui; int n; rruiC(vector<T> &a) : n(sz(a)) { rui.resize(n + 1); rer(i, n - 1) { rui[i] = rui[i + 1] + a[i]; } } /*[r l)*/ T operator()(int r, int l) { r++; l++; assert(l <= r && l >= 0 && r <= n); return rui[l] - rui[r]; } T operator()(int l) { return operator()(n - 1, l); } T operator[](int i) { return operator()(i); } }; template <class T> ostream &operator<<(ostream &os, rruiC<T> a) { fora(v, a.rui) os << v << " "; return os; } #define rrui rruic template <class T> rruiC<T> rruic(vector<T> &a) { return rruiC<T>(a); } //掛け算 template <class T> struct ruimulC { vector<T> rv; int n; ruimulC(vector<T> &a) : rv(a), n(sz(a)) { rv.resize(n + 1); rv[0] = 1; rep(i, n) { rv[i + 1] = a[i] * rv[i]; } } ruimulC() : n(0) { rv.resize(n + 1); rv[0] = 1; } void operator+=(T v) { rv.push_back(rv.back() * v); n++; } T operator()(int l, int r) { assert(l <= r && 0 <= l && r <= n); return rv[r] / rv[l]; } T operator()(int r = inf) { return operator()(0, min(r, n)); } T operator[](int r) { return operator()(0, r); } auto begin() { return rv.begin(); } auto end() { return rv.end(); } }; template <class T> ruimulC<T> ruimul(vector<T> &a) { return ruimulC<T>(a); } template <class T> ruimulC<T> ruimul() { vector<T> a; return ruimulC<T>(a); } /*@formatter:off*/ 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> void inc(pair<T, U> &a, U v = 1) { a.first += v, a.second += v; } template <class T, class U> void inc(T &a, U v = 1) { a += v; } template <class T, class U> void inc(vector<T> &a, U v = 1) { for (auto &u : a) inc(u, v); } template <class T, class U> void dec(T &a, U v = 1) { a -= v; } template <class T, class U> void dec(vector<T> &a, U v = 1) { for (auto &u : a) dec(u, v); } template <class U> void dec(string &a, U v = 1) { for (auto &u : a) dec(u, v); } template <class T, class U, class W> void dec(vector<T> &a, vector<U> &b, W v = 1) { for (auto &u : a) dec(u, v); for (auto &u : b) dec(u, v); } template <class T, class U, class W> void dec(vector<T> &a, vector<U> &b, vector<W> &c) { for (auto &u : a) dec(u, 1); for (auto &u : b) dec(u, 1); for (auto &u : c) dec(u, 1); } bool ins(ll h, ll w, ll H, ll W) { return h >= 0 && w >= 0 && h < H && w < W; } bool ins(ll l, ll v, ll r) { return l <= v && v < r; } template <class T> bool ins(vector<T> &a, ll i, ll j = 0) { return ins(0, i, sz(a)) && ins(0, j, sz(a)); } #define inside ins 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; } //添え字を返す template <class F> ll goldd_l(ll left, ll right, F calc) { double GRATIO = 1.6180339887498948482045868343656; ll lm = left + (ll)((right - left) / (GRATIO + 1.0)); ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); ll fl = calc(lm); ll fr = calc(rm); while (right - left > 10) { if (fl < fr) { right = rm; rm = lm; fr = fl; lm = left + (ll)((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } ll minScore = MAX(ll); ll resIndex = left; for (ll i = left; i < right + 1; ++i) { ll score = calc(i); if (minScore > score) { minScore = score; resIndex = i; } } return resIndex; } template <class F> ll goldt_l(ll left, ll right, F calc) { double GRATIO = 1.6180339887498948482045868343656; ll lm = left + (ll)((right - left) / (GRATIO + 1.0)); ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); ll fl = calc(lm); ll fr = calc(rm); while (right - left > 10) { if (fl > fr) { right = rm; rm = lm; fr = fl; lm = left + (ll)((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } if (left > right) { ll l = left; left = right; right = l; } ll maxScore = MIN(ll); ll resIndex = left; for (ll i = left; i < right + 1; ++i) { ll score = calc(i); if (maxScore < score) { maxScore = score; resIndex = i; } } return resIndex; } /*loopは200にすればおそらく大丈夫 余裕なら300に*/ template <class F> dou goldd_d(dou left, dou right, F calc, ll loop = 200) { dou GRATIO = 1.6180339887498948482045868343656; dou lm = left + ((right - left) / (GRATIO + 1.0)); dou rm = lm + ((right - lm) / (GRATIO + 1.0)); dou fl = calc(lm); dou fr = calc(rm); /*200にすればおそらく大丈夫*/ /*余裕なら300に*/ ll k = 141; loop++; while (--loop) { if (fl < fr) { right = rm; rm = lm; fr = fl; lm = left + ((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + ((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } return left; } template <class F> dou goldt_d(dou left, dou right, F calc, ll loop = 200) { double GRATIO = 1.6180339887498948482045868343656; dou lm = left + ((right - left) / (GRATIO + 1.0)); dou rm = lm + ((right - lm) / (GRATIO + 1.0)); dou fl = calc(lm); dou fr = calc(rm); loop++; while (--loop) { if (fl > fr) { right = rm; rm = lm; fr = fl; lm = left + ((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + ((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } return left; } // l ~ rを複数の区間に分割し、極致を与えるiを返す time-20 msまで探索 /*@formatter:on*/ template <class F> ll goldd_ls(ll l, ll r, F calc, ll time = 2000) { auto lim = milliseconds(time - 20); ll mini = 0, minv = MAX(ll); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/ ll nl = l; ll nr = l + haba; rep(i, k) { ll ni = goldd_l(nl, nr, calc); if (chmi(minv, calc(ni))) mini = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return mini; } /*@formatter:off*/ template <class F> ll goldt_ls(ll l, ll r, F calc, ll time = 2000) { auto lim = milliseconds(time - 20); ll maxi = 0, maxv = MIN(ll); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/ ll nl = l; ll nr = l + haba; rep(i, k) { ll ni = goldt_l(nl, nr, calc); if (chma(maxv, calc(ni))) maxi = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return maxi; } template <class F> dou goldd_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を持つ*/ auto lim = milliseconds(time - 20); dou mini = 0, minv = MAX(dou); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); dou haba = (r - l) / k; dou nl = l; dou nr = l + haba; rep(i, k) { dou ni = goldd_d(nl, nr, calc); if (chmi(minv, calc(ni))) mini = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return mini; } template <class F> dou goldt_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を残している*/ auto lim = milliseconds(time - 20); dou maxi = 0, maxv = MIN(dou); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); dou haba = (r - l) / k; dou nl = l; dou nr = l + haba; rep(i, k) { dou ni = goldt_d(nl, nr, calc); if (chma(maxv, calc(ni))) maxi = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return maxi; } template <class T> T min(vector<vector<T>> &a) { T res = MAX(T); rep(i, a.size()) chmi(res, *min_element(all(a[i]))); return res; } template <class T> T max(vector<vector<T>> &a) { T res = MIN(T); rep(i, a.size()) chma(res, *max_element(all(a[i]))); return res; } constexpr bool bget(ll m, ll keta) { #ifdef _DEBUG assert(keta <= 62); //オーバーフロー 1^62までしか扱えない #endif return (m >> keta) & 1; } // bget(n)次元 // NならN-1まで vector<vi> bget2(vi &a, int keta_size) { vvi(res, keta_size, sz(a)); rep(k, keta_size) { rep(i, sz(a)) { res[k][i] = bget(a[i], k); } } return res; } vi bget1(vi &a, int keta) { vi res(sz(a)); rep(i, sz(a)) { res[i] = bget(a[i], keta); } return res; } ll bget(ll m, ll keta, ll sinsuu) { m /= (ll)pow(sinsuu, keta); return m % sinsuu; } constexpr ll bit(ll n) { #ifdef _DEBUG assert(n <= 62); //オーバーフロー 1^62までしか扱えない #endif return (1LL << (n)); } ll bit(ll n, ll sinsuu) { return (ll)pow(sinsuu, n); } ll mask(ll n) { return (1ll << n) - 1; } // aをbitに置きなおす //{0, 2} -> 101 ll biti(vi &a) { int m = 0; for (auto &&v : a) m |= bit(v); return m; } //{1, 1, 0} -> 011 ll bitb(vi &a) { int m = 0; rep(i, sz(a)) if (a[i]) m |= bit(i); return m; } #define bcou __builtin_popcountll //最下位ビット ll lbit(ll n) { return n & -n; } ll lbiti(ll n) { return log2(n & -n); } //最上位ビット ll hbit(ll n) { n |= (n >> 1); n |= (n >> 2); n |= (n >> 4); n |= (n >> 8); n |= (n >> 16); n |= (n >> 32); return n - (n >> 1); } ll hbiti(ll n) { return log2(hbit(n)); } ll hbitk(ll n) { ll k = 0; rer(i, 5) { ll a = k + (1ll << i); ll b = 1ll << a; if (b <= n) k += 1ll << i; } return k; } //初期化は0を渡す ll nextComb(ll &mask, ll n, ll r) { if (!mask) return mask = (1LL << r) - 1; ll x = mask & -mask; /*最下位の1*/ ll y = mask + x; /*連続した下の1を繰り上がらせる*/ ll res = ((mask & ~y) / x >> 1) | y; if (bget(res, n)) return mask = 0; else return mask = res; } // n桁以下でビットがr個立っているもののvectorを返す vi bitCombList(ll n, ll r) { vi res; ll m = 0; while (nextComb(m, n, r)) { res.push_back(m); } return res; } // masの立ってるindexを見る #define forbit(i, mas) \ for (int forbitj = lbit(mas), forbitm = mas, i = log2(forbitj); forbitm; \ forbitm = forbitj ? forbitm ^ forbitj : 0, forbitj = lbit(forbitm), \ i = log2(forbitj)) char itoal(ll i) { return 'a' + i; } char itoaL(ll i) { return 'A' + i; } ll altoi(char c) { if ('A' <= c && c <= 'Z') return c - 'A'; return c - 'a'; } ll ctoi(char c) { return c - '0'; } char itoc(ll i) { return i + '0'; } ll vtoi(vi &v) { ll res = 0; if (sz(v) > 18) { debugline("vtoi"); deb(sz(v)); ole(); } rep(i, sz(v)) { res *= 10; res += v[i]; } return res; } vi itov(ll i) { vi res; while (i) { res.push_back(i % 10); i /= 10; } rev(res); return res; } vi stov(string &a) { ll n = sz(a); vi ret(n); rep(i, n) { ret[i] = a[i] - '0'; } return ret; } //基準を満たさないものは0になる vi stov(string &a, char one) { ll n = sz(a); vi ret(n); rep(i, n) ret[i] = a[i] == one; return ret; } vector<vector<ll>> ctoi(vector<vector<char>> s, char c) { ll n = sz(s), m = sz(s[0]); vector<vector<ll>> res(n, vector<ll>(m)); rep(i, n) rep(j, m) res[i][j] = s[i][j] == c; return res; } #define unique(v) v.erase(unique(v.begin(), v.end()), v.end()); //[i] := vを返す // aは0~n-1で置き換えられる vi compress(vi &a) { vi b; ll len = a.size(); for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { a[i] = lower_bound(all(b), a[i]) - b.begin(); } ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, umap<ll, ll> &map) { vi b; ll len = a.size(); for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { ll v = a[i]; a[i] = lower_bound(all(b), a[i]) - b.begin(); map[v] = a[i]; } ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, vi &r) { vi b; ll len = a.size(); fora(v, a) b.push_back(v); fora(v, r) b.push_back(v); sort(b); unique(b); for (ll i = 0; i < len; ++i) a[i] = lower_bound(all(b), a[i]) - b.begin(); for (ll i = 0; i < sz(r); ++i) r[i] = lower_bound(all(b), r[i]) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, vi &r, vi &s) { vi b; ll len = a.size(); fora(v, a) b.push_back(v); fora(v, r) b.push_back(v); fora(v, s) b.push_back(v); sort(b); unique(b); for (ll i = 0; i < len; ++i) a[i] = lower_bound(all(b), a[i]) - b.begin(); for (ll i = 0; i < sz(r); ++i) r[i] = lower_bound(all(b), r[i]) - b.begin(); for (ll i = 0; i < sz(s); ++i) r[i] = lower_bound(all(b), s[i]) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vector<vi> &a) { vi b; fora(vv, a) fora(v, vv) b.push_back(v); sort(b); unique(b); fora(vv, a) fora(v, vv) v = lower_bound(all(b), v) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vector<vector<vi>> &a) { vi b; fora(vvv, a) fora(vv, vvv) fora(v, vv) b.push_back(v); sort(b); unique(b); fora(vvv, a) fora(vv, vvv) fora(v, vv) v = lower_bound(all(b), v) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } void compress(ll a[], ll len) { vi b; for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { a[i] = lower_bound(all(b), a[i]) - b.begin(); } } //要素が見つからなかったときに困る #define binarySearch(a, v) (binary_search(all(a), v)) #define lowerIndex(a, v) (lower_bound(all(a), v) - a.begin()) #define upperIndex(a, v) (upper_bound(all(a), v) - a.begin()) #define rlowerIndex(a, v) (upper_bound(all(a), v) - a.begin() - 1) #define rupperIndex(a, v) (lower_bound(all(a), v) - a.begin() - 1) template <class T, class U, class W> T lowerBound(vector<T> &a, U v, W banpei) { auto it = lower_bound(a.begin(), a.end(), v); if (it == a.end()) return banpei; else return *it; } template <class T, class U, class W> T lowerBound(ruiC<T> &a, U v, W banpei) { return lowerBound(a.rui, v, banpei); } template <class T, class U, class W> T upperBound(vector<T> &a, U v, W banpei) { auto it = upper_bound(a.begin(), a.end(), v); if (it == a.end()) return banpei; else return *it; } template <class T, class U, class W> T upperBound(ruiC<T> &a, U v, W banpei) { return upperBound(a.rui, v, banpei); } template <class T, class U, class W> T rlowerBound(vector<T> &a, U v, W banpei) { auto it = upper_bound(a.begin(), a.end(), v); if (it == a.begin()) return banpei; else { return *(--it); } } template <class T, class U, class W> T rlowerBound(ruiC<T> &a, U v, W banpei) { return rlowerBound(a.rui, v, banpei); } template <class T, class U, class W> T rupperBound(vector<T> &a, U v, W banpei) { auto it = lower_bound(a.begin(), a.end(), v); if (it == a.begin()) return banpei; else { return *(--it); } } template <class T, class U, class W> T rupperBound(ruiC<T> &a, U v, W banpei) { return rupperBound(a.rui, v, banpei); } #define next2(a) next(next(a)) #define prev2(a) prev(prev(a)) //狭義の単調増加列 長さを返す template <class T> int lis(vector<T> &a) { int n = sz(a); vi tail(n + 1, MAX(T)); rep(i, n) { int id = lowerIndex(tail, a[i]); /**/ tail[id] = a[i]; } return lowerIndex(tail, MAX(T)); } template <class T> int lis_eq(vector<T> &a) { int n = sz(a); vi tail(n + 1, MAX(T)); rep(i, n) { int id = upperIndex(tail, a[i]); /**/ tail[id] = a[i]; } return lowerIndex(tail, MAX(T)); } // iteratorを返す // valueが1以上の物を返す 0は見つけ次第削除 // vを減らす場合 (*it).se--でいい template <class T, class U, class V> auto lower_map(map<T, U> &m, V k) { auto ret = m.lower_bound(k); while (ret != m.end() && (*ret).second == 0) { ret = m.erase(ret); } return ret; } template <class T, class U, class V> auto upper_map(map<T, U> &m, V k) { auto ret = m.upper_bound(k); while (ret != m.end() && (*ret).second == 0) { ret = m.erase(ret); } return ret; } //存在しなければエラー template <class T, class U, class V> auto rlower_map(map<T, U> &m, V k) { auto ret = upper_map(m, k); assert(ret != m.begin()); ret--; while (1) { if ((*ret).second != 0) break; assert(ret != m.begin()); auto next = ret; --next; m.erase(ret); ret = next; } return ret; } template <class T, class U, class V> auto rupper_map(map<T, U> &m, V k) { auto ret = lower_map(m, k); assert(ret != m.begin()); ret--; while (1) { if ((*ret).second != 0) break; assert(ret != m.begin()); auto next = ret; --next; m.erase(ret); ret = next; } return ret; } template <class T> void fin(T s) { cout << s << endl, exit(0); } //便利 数学 math // sub ⊂ top bool subset(int sub, int top) { return (sub & top) == sub; } //-180 ~ 180 degree double atand(double h, double w) { return atan2(h, w) / PI * 180; } //% -mの場合、最小の正の数を返す ll mod(ll a, ll m) { if (m < 0) m *= -1; return (a % m + m) % m; } ll pow(ll a) { return a * a; }; ll fact(ll v) { return v <= 1 ? 1 : v * fact(v - 1); } dou factd(int v) { static vd fact(2, 1); if (sz(fact) <= v) { rep(i, sz(fact), v + 1) { fact.push_back(fact.back() * i); } } return fact[v]; } ll comi(ll n, ll r) { assert(n < 100); static vvi(pas, 100, 100); if (pas[0][0]) return pas[n][r]; pas[0][0] = 1; rep(i, 1, 100) { pas[i][0] = 1; rep(j, 1, i + 1) pas[i][j] = pas[i - 1][j - 1] + pas[i - 1][j]; } return pas[n][r]; } double comd2(ll n, ll r) { static vvd(comb, 2020, 2020); if (comb[0][0] == 0) { comb[0][0] = 1; rep(i, 2000) { comb[i + 1][0] = 1; rep(j, 1, i + 2) { comb[i + 1][j] = comb[i][j] + comb[i][j - 1]; } } } return comb[n][r]; } double comd(int n, int r) { if (r < 0 || r > n) return 0; if (n < 2020) return comd2(n, r); static vd fact(2, 1); if (sz(fact) <= n) { rep(i, sz(fact), n + 1) { fact.push_back(fact.back() * i); } } return fact[n] / fact[n - r] / fact[r]; } ll gcd(ll a, ll b) { while (b) a %= b, swap(a, b); return abs(a); } ll gcd(vi b) { ll res = b[0]; rep(i, 1, sz(b)) res = gcd(b[i], res); return res; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll lcm(vi a) { ll res = a[0]; rep(i, 1, sz(a)) res = lcm(a[i], res); return res; } ll ceil(ll a, ll b) { if (b == 0) { debugline("ceil"); deb(a, b); ole(); return -1; } else if (a < 0) { return 0; } else { return (a + b - 1) / b; } } ll sig0(ll t) { return t <= 0 ? 0 : ((1 + t) * t) >> 1; } // ll sig(ll s, ll t) { return ((s + t) * (t - s + 1)) >> 1; } ll sig(ll s, ll t) { if (s > t) swap(s, t); return sig0(t - s) + s * (t - s + 1); } #define tousa_i tosa_i #define lower_tousa_i lower_tosa_i #define upper_tousa upper_tosa #define upper_tousa_i upper_tosa_i ll tosa_i(ll st, ll ad, ll v) { assert(((v - st) % ad) == 0); return (v - st) / ad; } ll tosa_s(ll st, ll ad, ll len) { return st * len + sig0(len - 1) * ad; } // ax + r (x は非負整数) で表せる整数のうち、v 以上となる最小の整数 ll lower_tosa(ll st, ll ad, ll v) { if (st >= v) return st; return (v - st + ad - 1) / ad * ad + st; } //第何項か ll lower_tosa_i(ll st, ll ad, ll v) { if (st >= v) return 0; return (v - st + ad - 1) / ad; } ll upper_tosa(ll st, ll ad, ll v) { return lower_tosa(st, ad, v + 1); } ll upper_tosa_i(ll st, ll ad, ll v) { return lower_tosa_i(st, ad, v + 1); } // v * v >= aとなる最小のvを返す 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); } /*@formatter:off*/ //機能拡張 template <class T, class U> string to_string(T a, U b) { string res = ""; res += a; res += b; return res; } template <class T, class U, class V> string to_string(T a, U b, V c) { string res = ""; res += a; res += b; res += c; return res; } template <class T, class U, class V, class W> string to_string(T a, U b, V c, W d) { string res = ""; res += a; res += b; res += c; res += d; return res; } template <class T, class U, class V, class W, class X> string to_string(T a, U b, V c, W d, X e) { string res = ""; res += a; res += b; res += c; res += d; res += e; return res; } constexpr int bsetlen = k5 * 2; // constexpr int bsetlen = 5050; #define bset bitset<bsetlen> bool operator<(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] < b[i]) return true; if (a[i] > b[i]) return false; } return false; } bool operator>(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] > b[i]) return true; if (a[i] < b[i]) return false; } return false; } bool operator<=(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] < b[i]) return true; if (a[i] > b[i]) return false; } return true; } bool operator>=(bitset<bsetlen> &a, bitset<bsetlen> &b) { rer(i, bsetlen - 1) { if (a[i] > b[i]) return true; if (a[i] < b[i]) return false; } return true; } string operator~(string &a) { string res = a; for (auto &&c : res) { if (c == '0') c = '1'; else if (c == '1') c = '0'; else { cerr << "cant ~" << a << "must bit" << endl; exit(0); } } return res; } ostream &operator<<(ostream &os, bset &a) { bitset<10> b; vi list; rep(i, bsetlen) { if (a[i]) list.push_back(i), b[i] = 1; } os << b << ", " << list; return os; } int hbiti(bset &a) { rer(i, bsetlen) { if (a[i]) return i; } return -1; } #define hk(a, b, c) (a <= b && b < c) // O(N/64) bset nap(bset &a, int v) { bset r = a | a << v; return r; } bset nap(bset &a, bset &v) { bset r = a; rep(i, bsetlen) { if (v[i]) r |= a << i; } return r; } template <class T> void seth(vector<vector<T>> &S, int w, vector<T> &v) { assert(sz(S) == sz(v)); assert(w < sz(S[0])); rep(h, sz(S)) { S[h][w] = v[h]; } } template <class T, class U> void operator+=(pair<T, U> &a, pair<T, U> &b) { a.fi += b.fi; a.se += b.se; } template <class T, class U> pair<T, U> operator+(pair<T, U> &a, pair<T, U> &b) { return pair<T, U>(a.fi + b.fi, a.se + b.se); } template <typename CharT, typename Traits, typename Alloc> basic_string<CharT, Traits, Alloc> operator+(const basic_string<CharT, Traits, Alloc> &lhs, const int rv) { basic_string<CharT, Traits, Alloc> str(lhs); str.append(to_string(rv)); return str; } template <typename CharT, typename Traits, typename Alloc> void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const int rv) { lhs += to_string(rv); } template <typename CharT, typename Traits, typename Alloc> basic_string<CharT, Traits, Alloc> operator+(const basic_string<CharT, Traits, Alloc> &lhs, const signed rv) { basic_string<CharT, Traits, Alloc> str(lhs); str.append(to_string(rv)); return str; } template <typename CharT, typename Traits, typename Alloc> void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const signed rv) { lhs += to_string(rv); } template <typename CharT, typename Traits, typename Alloc> void operator*=(basic_string<CharT, Traits, Alloc> &s, int num) { auto bek = s; s = ""; for (; num; num >>= 1) { if (num & 1) { s += bek; } bek += bek; } } template <class T, class U> void operator+=(queue<T> &a, U v) { a.push(v); } template <class T, class U> void operator+=(deque<T> &a, U v) { a.push_back(v); } template <class T> priority_queue<T, vector<T>, greater<T>> & operator+=(priority_queue<T, vector<T>, greater<T>> &a, vector<T> &v) { fora(d, v) a.push(d); return a; } template <class T, class U> priority_queue<T, vector<T>, greater<T>> & operator+=(priority_queue<T, vector<T>, greater<T>> &a, U v) { a.push(v); return a; } template <class T, class U> priority_queue<T> &operator+=(priority_queue<T> &a, U v) { a.push(v); return a; } template <class T> set<T> &operator+=(set<T> &a, vector<T> v) { fora(d, v) a.insert(d); return a; } template <class T, class U> auto operator+=(set<T> &a, U v) { return a.insert(v); } template <class T, class U> auto operator-=(set<T> &a, U v) { return a.erase(v); } template <class T, class U> auto operator+=(mset<T> &a, U v) { return a.insert(v); } template <class T, class U> set<T, greater<T>> &operator+=(set<T, greater<T>> &a, U v) { a.insert(v); return a; } template <class T, class U> vector<T> &operator+=(vector<T> &a, U v) { a.push_back(v); return a; } template <class T, class U> vector<T> operator+(const vector<T> &a, U v) { vector<T> ret = a; ret += v; return ret; } template <class T, class U> vector<T> operator+(U v, const vector<T> &a) { vector<T> ret = a; ret.insert(ret.begin(), v); return ret; } template <class T> vector<T> operator+(vector<T> a, vector<T> b) { vector<T> ret; ret = a; fora(v, b) ret += v; return ret; } template <class T> vector<T> &operator+=(vector<T> &a, vector<T> &b) { rep(i, sz(b)) { /*こうしないとa+=aで両辺が増え続けてバグる*/ a.push_back(b[i]); } return a; } template <class T, class U> map<T, U> &operator+=(map<T, U> &a, map<T, U> &b) { fora(bv, b) { a[bv.first] += bv.second; } return a; } template <class T> vector<T> &operator-=(vector<T> &a, vector<T> &b) { if (sz(a) != sz(b)) { debugline("vector<T> operator-="); deb(a); deb(b); exit(0); } rep(i, sz(a)) a[i] -= b[i]; return a; } template <class T> vector<T> operator-(vector<T> &a, vector<T> &b) { if (sz(a) != sz(b)) { debugline("vector<T> operator-"); deb(a); deb(b); ole(); } vector<T> res(sz(a)); rep(i, sz(a)) res[i] = a[i] - b[i]; return res; } template <class T, class U> void operator*=(vector<T> &a, U b) { vector<T> ta = a; rep(b - 1) { a += ta; } } template <typename T> void erase(vector<T> &v, unsigned ll i) { v.erase(v.begin() + i); } template <typename T> void erase(vector<T> &v, unsigned ll s, unsigned ll e) { v.erase(v.begin() + s, v.begin() + e); } template <class T, class U> void erase(map<T, U> &m, ll okl, ll ngr) { m.erase(m.lower_bound(okl), m.lower_bound(ngr)); } template <class T> void erase(set<T> &m, ll okl, ll ngr) { m.erase(m.lower_bound(okl), m.lower_bound(ngr)); } template <typename T> void erasen(vector<T> &v, unsigned ll s, unsigned ll n) { v.erase(v.begin() + s, v.begin() + s + n); } template <typename T, typename U> void insert(vector<T> &v, unsigned ll i, U t) { v.insert(v.begin() + i, t); } template <typename T, typename U> void push_front(vector<T> &v, U t) { v.insert(v.begin(), t); } template <typename T, typename U> void insert(vector<T> &v, unsigned ll i, vector<T> list) { for (auto &&va : list) v.insert(v.begin() + i++, va); } template <typename T> void insert(set<T> &v, vector<T> list) { for (auto &&va : list) v.insert(va); } vector<string> split(const string a, const char deli) { string b = a + deli; ll l = 0, r = 0, n = b.size(); vector<string> res; rep(i, n) { if (b[i] == deli) { r = i; if (l < r) res.push_back(b.substr(l, r - l)); l = i + 1; } } return res; } vector<string> split(const string a, const string deli) { vector<string> res; ll kn = sz(deli); std::string::size_type Pos(a.find(deli)); ll l = 0; while (Pos != std::string::npos) { if (Pos - l) res.push_back(a.substr(l, Pos - l)); l = Pos + kn; Pos = a.find(deli, Pos + kn); } if (sz(a) - l) res.push_back(a.substr(l, sz(a) - l)); return res; } 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 fAb(bool a) { if (a) cout << "Alice" << endl; else cout << "Bob"; } 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 <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)}; } //未分類 // 0,2,1 1番目と2番目の次元を入れ替える template <class T> auto irekae(vector<vector<vector<T>>> &A, int x, int y, int z) { #define irekae_resize_loop(a, b, c) \ resize(res, a, b, c); \ rep(i, a) rep(j, b) rep(k, c) vector<vector<vector<T>>> res; if (x == 0 && y == 1 && z == 2) { res = A; } else if (x == 0 && y == 2 && z == 1) { irekae_resize_loop(sz(A), sz(A[0][0]), sz(A[0])) { res[i][j][k] = A[i][k][j]; } } else if (x == 1 && y == 0 && z == 2) { irekae_resize_loop(sz(A[0]), sz(A), sz(A[0][0])) { res[i][j][k] = A[j][i][k]; } } else if (x == 1 && y == 2 && z == 0) { irekae_resize_loop(sz(A[0]), sz(A[0][0]), sz(A)) { res[i][j][k] = A[k][i][j]; } } else if (x == 2 && y == 0 && z == 1) { irekae_resize_loop(sz(A[0][0]), sz(A), sz(A[0])) { res[i][j][k] = A[j][k][i]; } } else if (x == 2 && y == 1 && z == 0) { irekae_resize_loop(sz(A[0][0]), sz(A[0]), sz(A)) { res[i][j][k] = A[k][j][i]; } } return res; #undef irekae_resize_loop } template <class T> auto irekae(vector<vector<T>> &A, int i = 1, int j = 0) { vvt(res, sz(A[0]), sz(A)); rep(i, sz(A)) { rep(j, sz(A[0])) { res[j][i] = A[i][j]; } } return res; } // tou分割する template <typename T> vector<vector<T>> cut(vector<T> &a, int tou = 2) { int N = sz(a); vector<vector<T>> res(tou); int hab = N / tou; vi lens(tou, hab); rep(i, N % tou) { lens[tou - 1 - i]++; } int l = 0; rep(i, tou) { int len = lens[i]; int r = l + len; res[i].resize(len); std::copy(a.begin() + l, a.begin() + r, res[i].begin()); l = r; } return res; } //長さn毎に分割する template <typename T> vector<vector<T>> cutn(vector<T> &a, int len) { int N = sz(a); vector<vector<T>> res(ceil(N, len)); vi lens(N / len, len); if (N % len) lens.push_back(N % len); int l = 0; rep(i, sz(lens)) { int len = lens[i]; int r = l + len; res[i].resize(len); std::copy(a.begin() + l, a.begin() + r, res[i].begin()); l = r; } return res; } vi inds_(vi &a) { int n = sz(a); vb was(n); vi res(n); rep(i, n) { assert(!was[a[i]]); res[a[i]] = i; was[a[i]] = true; } return res; } //@起動時 struct initon { initon() { cin.tie(0); ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(16); srand((unsigned)clock() + (unsigned)time(NULL)); }; } initonv; //@formatter:on #define pre prev #define nex next // gra mll pr //上下左右 const string udlr = "udlr"; string UDLR = "UDLR"; // x4と連動 UDLR.find('U') := x4[0] //右、上が正 constexpr ll h4[] = {1, -1, 0, 0}; constexpr ll w4[] = {0, 0, -1, 1}; constexpr ll h8[] = {0, 1, 0, -1, -1, 1, 1, -1}; constexpr ll w8[] = {1, 0, -1, 0, 1, -1, 1, -1}; int mei_inc(int h, int w, int H, int W, int i) { while (++i < 4) { if (inside(h + h4[i], w + w4[i], H, W)) return i; } return i; } #define mei(nh, nw, h, w) \ for (int i = mei_inc(h, w, H, W, -1), nh = i < 4 ? h + h4[i] : 0, \ nw = i < 4 ? w + w4[i] : 0; \ i < 4; i = mei_inc(h, w, H, W, i), nh = h + h4[i], nw = w + w4[i]) //グラフ内で #undef getid //#define getidとしているため、ここを書き直したらgraphも書き直す #define getid_2(h, w) (h * W + w) #define getid_1(p) (p.first * W + p.second) #define o_getid(a, b, name, ...) name #define getid(...) o_getid(__VA_ARGS__, getid_2, getid_1)(__VA_ARGS__) #define getp(id) mp(id / W, id % W) #define set_shuffle() \ std::random_device seed_gen; \ std::mt19937 engine(seed_gen()) #define shuffle(a) std::shuffle((a).begin(), (a).end(), engine); // 1980 開始からtime ms経っていたらtrue bool timeup(int time) { auto end_time = system_clock::now(); auto part = duration_cast<milliseconds>(end_time - start_time); auto lim = milliseconds(time); return part >= lim; } void set_time() { past_time = system_clock::now(); } // MS型(millisecqnds)で返る // set_timeをしてからの時間 auto calc_time_milli() { auto now = system_clock::now(); auto part = duration_cast<milliseconds>(now - past_time); return part; } auto calc_time_micro() { auto now = system_clock::now(); auto part = duration_cast<microseconds>(now - past_time); return part; } auto calc_time_nano() { auto now = system_clock::now(); auto part = duration_cast<nanoseconds>(now - past_time); return part; } bool calc_time(int zikan) { return calc_time_micro() >= microseconds(zikan); } using MS = std::chrono::microseconds; int div(microseconds a, microseconds b) { return a / b; } int div(nanoseconds a, nanoseconds b) { if (b < nanoseconds(1)) { return a / nanoseconds(1); } int v = a / b; return v; } // set_time(); // rep(i,lim)shori // lim*=time_nanbai(); // rep(i,lim)shoriと使う //全体でmilliかかっていいときにlimを何倍してもう一回できるかを返す int time_nanbai(int milli) { auto dec = duration_cast<nanoseconds>(past_time - start_time); auto part = calc_time_nano(); auto can_time = nanoseconds(milli * 1000 * 1000); can_time -= part; can_time -= dec; return div(can_time, part); } // vectorで取れる要素数 // bool=> 1e9 * 8.32 // int => 1e8 * 2.6 // ll => 1e8 * 1.3 // 3次元以上取るとメモリがヤバい // static配列を使う vvc(ba); ll N, M, H, W; vi A, B, C; //遅い 文字列以外も扱えるはず template <class T = string> struct rolling_hash2 { //@formatter:off const int b[2] = {11731, 11839}; const int m[2] = {1000002571LL, 1000002631LL}; int n; T s; // stringやvector vector<long long> hash[2], pow[2]; rolling_hash2() {} rolling_hash2(T &S) : s(S) { n = (int)S.size(); for (int i = 0; i < 2; ++i) { hash[i].assign(n + 1, 0); pow[i].assign(n + 1, 1); for (int j = 0; j < n; ++j) { hash[i][j + 1] = (hash[i][j] * b[i] + S[j]) % m[i]; pow[i][j + 1] = pow[i][j] * b[i] % m[i]; } } } // get hash of S[left:right] ll get(int l, int r, int id) { ll res = hash[id][r] - hash[id][l] * pow[id][r - l] % m[id]; if (res < 0) res += m[id]; return res; } ll get(int l, int r) { return (get(l, r, 0) << 32) | get(l, r, 1); } ll operator()(int l, int r) { return get(l, r, 0); } ll operator()() { return get(0, n, 0); } // get lcp of S[a:] and S[b:] int lcp(int a, int b) { int len = min((int)s.size() - a, (int)s.size() - b); int low = -1, high = len + 1; while (high - low > 1) { int mid = (low + high) / 2; if (get(a, a + mid, 0) != get(b, b + mid, 0)) high = mid; else if (get(a, a + mid, 1) != get(b, b + mid, 1)) high = mid; else low = mid; } return low; } // get lcp of S[a:] and T[b:] int lcp(rolling_hash2 &t, int a, int b) { int len = min((int)s.size() - a, (int)s.size() - b); int low = -1, high = len + 1; while (high - low > 1) { int mid = (low + high) / 2; if (get(a, a + mid, 0) != get(b, b + mid, 0)) high = mid; else if (get(a, a + mid, 1) != get(b, b + mid, 1)) high = mid; else low = mid; } return low; } }; //早い struct rolling_hash { static constexpr int base1 = 1007, base2 = 2009; static constexpr int mod1 = 1000000007, mod2 = 1000000009; vector<long long> hash1, hash2, power1, power2; int n; rolling_hash(string &S) { n = (int)S.size(); hash1.assign(n + 1, 0); hash2.assign(n + 1, 0); power1.assign(n + 1, 1); power2.assign(n + 1, 1); for (int i = 0; i < n; ++i) { hash1[i + 1] = (hash1[i] * base1 + S[i]) % mod1; hash2[i + 1] = (hash2[i] * base2 + S[i]) % mod2; power1[i + 1] = (power1[i] * base1) % mod1; power2[i + 1] = (power2[i] * base2) % mod2; } } /*@formatter:on*/ template <class T> rolling_hash(vector<T> &S) { n = (int)S.size(); hash1.assign(n + 1, 0); hash2.assign(n + 1, 0); power1.assign(n + 1, 1); power2.assign(n + 1, 1); for (int i = 0; i < n; ++i) { hash1[i + 1] = (hash1[i] * base1 + S[i]) % mod1; hash2[i + 1] = (hash2[i] * base2 + S[i]) % mod2; power1[i + 1] = (power1[i] * base1) % mod1; power2[i + 1] = (power2[i] * base2) % mod2; } } /*@formatter:off*/ inline pair<int, int> get(int l, int r) { long long res1 = hash1[r] - hash1[l] * power1[r - l] % mod1; if (res1 < 0) res1 += mod1; long long res2 = hash2[r] - hash2[l] * power2[r - l] % mod2; if (res2 < 0) res2 += mod2; return {res1, res2}; } inline pair<int, int> operator()(int l, int r) { return get(l, r); } inline pair<int, int> operator()() { return get(0, n); } // get lcp of S[a:] and T[b:] //接頭語がどれだけ一致するか //範囲の重複を含む inline int lcp_dup(int a, int b) { int len = min((int)hash1.size() - a, (int)hash1.size() - b); int low = 0, high = len; while (high - low > 1) { int mid = (low + high) >> 1; if (get(a, a + mid) != get(b, b + mid)) high = mid; else low = mid; } return low; } //重複を認めない inline int lcp_not(int a, int b) { return min(lcp_dup(a, b), b - a); } //@formatter:on }; #define roll rolling_hash // roll ro(list); void solve() { in(N); na(A, N); na(B, N); if (all_of(A, == A[0]) && all_of(B, == B[0])) { rep(i, N) { out(i, A[0] ^ B[0]); } return; } vi callr(N); vi resr(N); auto DS = bget2(B, 31); rep(b, 31) { roll rb(DS[b]); auto hb = rb(0, N); rep(x, 2) { vi C = bget1(A, b); C *= 2; for_each(C, ^= x); roll ra(C); rep(r, N) { //簡易チェック rep(i, 3) { if ((C[r + i]) != DS[b][i]) goto end; } if (ra(r, r + N) == hb) { callr[r]++; resr[r] += bit(b) * x; } end:; } } } rep(r, N) { if (callr[r] == 31) { out(r, A[r] ^ B[0]); } } } auto my(ll n, vi &a) { return 0; } auto sister(ll n, vi &a) { ll ret = 0; return ret; } signed main() { solve(); #define arg n, a #ifdef _DEBUG bool bad = 0; for (ll i = 0, ok = 1; i < k5 && ok; ++i) { ll n = rand(1, 8); vi a = ranv(n, 1, 10); auto myres = my(arg); auto res = sister(arg); ok = myres == res; if (!ok) { out(arg); cerr << "AC : " << res << endl; cerr << "MY : " << myres << endl; bad = 1; break; } } if (!bad) { // cout << "完璧 : solveを書き直そう" << endl; // cout << " : そして、solve()を呼び出すのだ" << endl; // cout << " : cin>>n; na(a,n);も忘れるな" << endl; } #endif return 0; };
[ "control_flow.return.add" ]
744,041
744,042
u986437843
cpp
p02913
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) #define sz(x) int(x.size()) #define show(x) \ { \ for (auto i : x) { \ cout << i << " "; \ } \ cout << endl; \ } using namespace std; using ll = long long; using P = pair<int, int>; int dp[5005][5005]; // グローバルで定義すると0で初期化される int main() { // dp[i][j] i文字目とj文字目から連続する文字列の長さ // 重なった分は最後に考慮、0-indexed int N; cin >> N; string S; cin >> S; for (int j = N - 1; j >= 0; j--) { for (int i = N - 1; i >= 0; i--) { if (S[i] != S[j]) dp[i][j] = 0; else dp[i][j] = dp[i + 1][j + 1] + 1; } } int ans = 0; for (int i = 0; i < N; i++) { for (int j = i + 1; j < N; j++) { int temp = min(dp[i][j], j - 1); ans = max(ans, temp); } } cout << ans << '\n'; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) #define sz(x) int(x.size()) #define show(x) \ { \ for (auto i : x) { \ cout << i << " "; \ } \ cout << endl; \ } using namespace std; using ll = long long; using P = pair<int, int>; int dp[5005][5005]; // グローバルで定義すると0で初期化される int main() { // dp[i][j] i文字目とj文字目から連続する文字列の長さ // 重なった分は最後に考慮、0-indexed int N; cin >> N; string S; cin >> S; for (int j = N - 1; j >= 0; j--) { for (int i = j; i >= 0; i--) { if (S[i] != S[j]) dp[i][j] = 0; else dp[i][j] = dp[i + 1][j + 1] + 1; } } int ans = 0; for (int i = 0; i < N; i++) { for (int j = i + 1; j < N; j++) { int temp = min(dp[i][j], j - i); ans = max(ans, temp); } } cout << ans << '\n'; return 0; }
[ "variable_declaration.value.change", "control_flow.loop.for.initializer.change", "expression.operation.binary.change", "expression.operation.binary.remove", "identifier.replace.add", "literal.replace.remove", "call.arguments.change" ]
744,122
744,123
u600402037
cpp
p02913
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) #define sz(x) int(x.size()) #define show(x) \ { \ for (auto i : x) { \ cout << i << " "; \ } \ cout << endl; \ } using namespace std; using ll = long long; using P = pair<int, int>; int dp[5005][5005]; // グローバルで定義すると0で初期化される int main() { // dp[i][j] i文字目とj文字目から連続する文字列の長さ // 重なった分は最後に考慮、0-indexed int N; cin >> N; string S; cin >> S; for (int j = N - 1; j >= 0; j--) { for (int i = j; i >= 0; i--) { if (S[i] != S[j]) dp[i][j] = 0; else dp[i][j] = dp[i + 1][j + 1] + 1; } } int ans = 0; for (int i = 0; i < N; i++) { for (int j = i + 1; j < N; j++) { int temp = min(dp[i][j], j - 1); ans = max(ans, temp); } } cout << ans << '\n'; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) #define sz(x) int(x.size()) #define show(x) \ { \ for (auto i : x) { \ cout << i << " "; \ } \ cout << endl; \ } using namespace std; using ll = long long; using P = pair<int, int>; int dp[5005][5005]; // グローバルで定義すると0で初期化される int main() { // dp[i][j] i文字目とj文字目から連続する文字列の長さ // 重なった分は最後に考慮、0-indexed int N; cin >> N; string S; cin >> S; for (int j = N - 1; j >= 0; j--) { for (int i = j; i >= 0; i--) { if (S[i] != S[j]) dp[i][j] = 0; else dp[i][j] = dp[i + 1][j + 1] + 1; } } int ans = 0; for (int i = 0; i < N; i++) { for (int j = i + 1; j < N; j++) { int temp = min(dp[i][j], j - i); ans = max(ans, temp); } } cout << ans << '\n'; return 0; }
[ "identifier.replace.add", "literal.replace.remove", "call.arguments.change", "expression.operation.binary.change" ]
744,124
744,123
u600402037
cpp
p02913
#include <algorithm> #include <cstdio> using namespace std; #define int long long #define uint unsigned int #define dotimes(i, n) for (int i = 0, i##max__ = (n); i < i##max__; i++) #define whole(x, f, ...) \ ([&](decltype((x)) c) { return (f)(begin(c), end(c), ##__VA_ARGS__); })(x) int rint() { int x; scanf("%lld", &x); return x; } void wint(int x) { printf("%lld\n", x); } template <typename T> int size(T const &c) { return static_cast<int>(c.size()); } template <typename T> bool maxs(T &a, T const &b) { return a < b ? a = b, true : false; } template <typename T> bool mins(T &a, T const &b) { return a > b ? a = b, true : false; } inline int lg(int x) { return 63 - __builtin_clzll(static_cast<unsigned int>(x)); } signed main() { const int N = rint(); char S[N + 1]; scanf("%s", S); int dp[N] = {}, r = 0; dotimes(i, (N + 1) / 2) { for (int j = N - 1; j >= i + 1; j--) { dp[j] = S[i] == S[j] ? dp[j - 1] + 1 : 0; maxs(r, j - max(j - dp[j], i)); } } wint(r); return 0; }
#include <algorithm> #include <cstdio> using namespace std; #define int long long #define uint unsigned int #define dotimes(i, n) for (int i = 0, i##max__ = (n); i < i##max__; i++) #define whole(x, f, ...) \ ([&](decltype((x)) c) { return (f)(begin(c), end(c), ##__VA_ARGS__); })(x) int rint() { int x; scanf("%lld", &x); return x; } void wint(int x) { printf("%lld\n", x); } template <typename T> int size(T const &c) { return static_cast<int>(c.size()); } template <typename T> bool maxs(T &a, T const &b) { return a < b ? a = b, true : false; } template <typename T> bool mins(T &a, T const &b) { return a > b ? a = b, true : false; } inline int lg(int x) { return 63 - __builtin_clzll(static_cast<unsigned int>(x)); } signed main() { const int N = rint(); char S[N + 1]; scanf("%s", S); int dp[N] = {}, r = 0; dotimes(i, N - 1) { for (int j = N - 1; j >= i + 1; j--) { dp[j] = S[i] == S[j] ? dp[j - 1] + 1 : 0; maxs(r, j - max(j - dp[j], i)); } } wint(r); return 0; }
[ "call.arguments.change", "misc.opposites", "expression.operator.arithmetic.change", "expression.operation.binary.change", "expression.operation.binary.remove" ]
744,125
744,126
u281670674
cpp
p02913
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) #define sz(x) int(x.size()) using namespace std; typedef long long ll; typedef pair<int, int> P; const ll INF = 1LL << 60; ll N; string S; vector<int> Zalgo(const string &S) { int N = (int)S.size(); vector<int> res(N); res[0] = N; int i = 1, j = 0; while (i < N) { while (i + j < N && S[j] == S[i + j]) ++j; res[i] = j; if (j == 0) { ++i; continue; } int k = 1; while (i + k < N && k + res[k] < j) res[i + k] = res[k], ++k; i += k, j -= k; } return res; } vector<long long> divisor(long long n) { vector<long long> res; for (long long i = 1LL; i * i <= n; ++i) { if (n % i == 0LL) { res.push_back(i); long long temp = n / i; if (i != temp) res.push_back(temp); } } sort(res.begin(), res.end()); return res; } vector<int> Z_algorithm(string S) { int c = 0, n = S.size(); //接頭辞の長さを保存する配列を返す。 // i = 1からやるとする。 // Z[1]には赤下線が存在しないので、「溢れるor共通部分がない」のところにまずいく。 //実装上、上のような挙動をしてもらうため、Z[0]=n;は[1, n - //1]まで計算が終わった後に入れる。 cは、赤下線の左端の位置。 vector<int> Z(n, 0); for (int i = 1; i < n; i++) { int l = i - c; //今着目してる部分が赤下線の左端から何個分離れているかをlに入れる。 if (i + Z[l] < c + Z[c]) { //この条件を満たすのは、「青下線が赤下線に収まる」。 //この時、すでに計算されてるなのでそれを流用する。 Z[i] = Z[l]; } else { //この条件を満たすのは、「赤下線から青下線が溢れる」or「赤下線と青下線の共通部分がない」。 int j = max(0, c + Z[c] - i); // c + Z[c] - i > 0の時、これは「溢れる」が該当する。 //溢れてるのなら、収まる分は計算せずに、溢れた分(j番目から)だけ愚直に突き合わせればよい。 //そもそも共通部分がないならば、全部突き合わせる。この時、式からj = //0;となるとわかる。 //愚直に突き合せてる部分 while (i + j < n && S[j] == S[i + j]) j++; Z[i] = j; //今の見てるiで、赤下線に完全に含まれなくなったので、今のiを赤下線の左端として、次のiをまた計算する。 c = i; } } //最後にこれを忘れずに Z[0] = n; return Z; } // ローリングハッシュ // 二分探索で LCP を求める機能つき struct RollingHash { static const int base1 = 1007, base2 = 2009; static const int mod1 = 1000000007, mod2 = 1000000009; vector<long long> hash1, hash2, power1, power2; // construct RollingHash(const string &S) { int n = (int)S.size(); hash1.assign(n + 1, 0); hash2.assign(n + 1, 0); power1.assign(n + 1, 1); power2.assign(n + 1, 1); for (int i = 0; i < n; ++i) { hash1[i + 1] = (hash1[i] * base1 + S[i]) % mod1; hash2[i + 1] = (hash2[i] * base2 + S[i]) % mod2; power1[i + 1] = (power1[i] * base1) % mod1; power2[i + 1] = (power2[i] * base2) % mod2; } } // get hash of S[left:right] inline pair<long long, long long> get(int l, int r) const { long long res1 = hash1[r] - hash1[l] * power1[r - l] % mod1; if (res1 < 0) res1 += mod1; long long res2 = hash2[r] - hash2[l] * power2[r - l] % mod2; if (res2 < 0) res2 += mod2; return {res1, res2}; } // get lcp of S[a:] and T[b:] inline int getLCP(int a, int b) const { int len = min((int)hash1.size() - a, (int)hash1.size() - b); int low = 0, high = len; while (high - low > 1) { int mid = (low + high) >> 1; if (get(a, a + mid) != get(b, b + mid)) high = mid; else low = mid; } return low; } }; int main() { cin >> N >> S; // vector<int> tmp = Z_algorithm(S); RollingHash rh(S); int ans = 0; for (int i = 0; i < N; i++) { for (int j = i + 1; j < N; j++) { int t = rh.getLCP(i, j); t = min(t, j - i + 1); ans = max(ans, t); } } cout << ans << endl; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) #define sz(x) int(x.size()) using namespace std; typedef long long ll; typedef pair<int, int> P; const ll INF = 1LL << 60; ll N; string S; vector<int> Zalgo(const string &S) { int N = (int)S.size(); vector<int> res(N); res[0] = N; int i = 1, j = 0; while (i < N) { while (i + j < N && S[j] == S[i + j]) ++j; res[i] = j; if (j == 0) { ++i; continue; } int k = 1; while (i + k < N && k + res[k] < j) res[i + k] = res[k], ++k; i += k, j -= k; } return res; } vector<long long> divisor(long long n) { vector<long long> res; for (long long i = 1LL; i * i <= n; ++i) { if (n % i == 0LL) { res.push_back(i); long long temp = n / i; if (i != temp) res.push_back(temp); } } sort(res.begin(), res.end()); return res; } vector<int> Z_algorithm(string S) { int c = 0, n = S.size(); //接頭辞の長さを保存する配列を返す。 // i = 1からやるとする。 // Z[1]には赤下線が存在しないので、「溢れるor共通部分がない」のところにまずいく。 //実装上、上のような挙動をしてもらうため、Z[0]=n;は[1, n - //1]まで計算が終わった後に入れる。 cは、赤下線の左端の位置。 vector<int> Z(n, 0); for (int i = 1; i < n; i++) { int l = i - c; //今着目してる部分が赤下線の左端から何個分離れているかをlに入れる。 if (i + Z[l] < c + Z[c]) { //この条件を満たすのは、「青下線が赤下線に収まる」。 //この時、すでに計算されてるなのでそれを流用する。 Z[i] = Z[l]; } else { //この条件を満たすのは、「赤下線から青下線が溢れる」or「赤下線と青下線の共通部分がない」。 int j = max(0, c + Z[c] - i); // c + Z[c] - i > 0の時、これは「溢れる」が該当する。 //溢れてるのなら、収まる分は計算せずに、溢れた分(j番目から)だけ愚直に突き合わせればよい。 //そもそも共通部分がないならば、全部突き合わせる。この時、式からj = //0;となるとわかる。 //愚直に突き合せてる部分 while (i + j < n && S[j] == S[i + j]) j++; Z[i] = j; //今の見てるiで、赤下線に完全に含まれなくなったので、今のiを赤下線の左端として、次のiをまた計算する。 c = i; } } //最後にこれを忘れずに Z[0] = n; return Z; } // ローリングハッシュ // 二分探索で LCP を求める機能つき struct RollingHash { static const int base1 = 1007, base2 = 2009; static const int mod1 = 1000000007, mod2 = 1000000009; vector<long long> hash1, hash2, power1, power2; // construct RollingHash(const string &S) { int n = (int)S.size(); hash1.assign(n + 1, 0); hash2.assign(n + 1, 0); power1.assign(n + 1, 1); power2.assign(n + 1, 1); for (int i = 0; i < n; ++i) { hash1[i + 1] = (hash1[i] * base1 + S[i]) % mod1; hash2[i + 1] = (hash2[i] * base2 + S[i]) % mod2; power1[i + 1] = (power1[i] * base1) % mod1; power2[i + 1] = (power2[i] * base2) % mod2; } } // get hash of S[left:right] inline pair<long long, long long> get(int l, int r) const { long long res1 = hash1[r] - hash1[l] * power1[r - l] % mod1; if (res1 < 0) res1 += mod1; long long res2 = hash2[r] - hash2[l] * power2[r - l] % mod2; if (res2 < 0) res2 += mod2; return {res1, res2}; } // get lcp of S[a:] and T[b:] inline int getLCP(int a, int b) const { int len = min((int)hash1.size() - a, (int)hash1.size() - b); int low = 0, high = len; while (high - low > 1) { int mid = (low + high) >> 1; if (get(a, a + mid) != get(b, b + mid)) high = mid; else low = mid; } return low; } }; int main() { cin >> N >> S; // vector<int> tmp = Z_algorithm(S); RollingHash rh(S); int ans = 0; for (int i = 0; i < N; i++) { for (int j = i + 1; j < N; j++) { int t = rh.getLCP(i, j); // cout << t << " " << j - i + 1 << endl; t = min(t, j - i); ans = max(ans, t); } } cout << ans << endl; }
[ "expression.operation.binary.remove" ]
744,131
744,132
u455366471
cpp
p02913
//#pragma GCC optimize("Ofast") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace std::chrono; #define int long long #define ll long long auto start_time = system_clock::now(); //@formatter:off #ifdef _DEBUG //区間削除は出来ない template <class T> struct my_pbds_tree { set<T> s; auto begin() { return s.begin(); } auto end() { return s.end(); } auto rbegin() { return s.rbegin(); } auto rend() { return s.rend(); } auto empty() { return s.empty(); } auto size() { return s.size(); } void clear() { s.clear(); } template <class U> void insert(U v) { s.insert(v); } template <class U> void operator+=(U v) { insert(v); } template <class F> auto erase(F v) { return s.erase(v); } template <class U> auto find(U v) { return s.find(v); } template <class U> auto lower_bound(U v) { return s.lower_bound(v); } template <class U> auto upper_bound(U v) { return s.upper_bound(v); } auto find_by_order(ll k) { auto it = s.begin(); for (ll i = 0; i < k; i++) it++; return it; } auto order_of_key(ll v) { auto it = s.begin(); ll i = 0; for (; it != s.end() && *it < v; i++) it++; return i; } }; #define pbds(T) my_pbds_tree<T> // gp_hash_tableでcountを使えないようにするため template <class T, class U> struct my_unordered_map { unordered_map<T, U> m; my_unordered_map(){}; auto begin() { return m.begin(); } auto end() { return m.end(); } auto cbegin() { return m.cbegin(); } auto cend() { return m.cend(); } template <class V> auto erase(V v) { return m.erase(v); } void clear() { m.clear(); } /*countは gp_hash_tableに存在しない*/ /*!= m.end()*/ template <class V> auto find(V v) { return m.find(v); } template <class V> auto &operator[](V n) { return m[n]; } }; #define unordered_map my_unordered_map #define umapi unordered_map<ll, ll> #define umapu unordered_map<uint64_t, ll> #define umapp unordered_map<P, ll> #else #define unordered_map __gnu_pbds::gp_hash_table // find_by_order(k) k番目のイテレーター // order_of_key(k) k以上が前から何番目か #define pbds(U) \ __gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag, \ __gnu_pbds::tree_order_statistics_node_update> #define umapi unordered_map<ll, ll, xorshift> #define umapp unordered_map<P, ll, xorshift> #define umapu unordered_map<uint64_t, ll, xorshift> #endif struct xorshift { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } size_t operator()(std::pair<ll, ll> x) const { ll v = ((x.first) << 32) | x.second; static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(v + FIXED_RANDOM); } }; template <class U, class L> void operator+=( __gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update> &s, L v) { s.insert(v); } //衝突対策 #define ws wszzzz template <class A, class B, class C> struct T2 { A f; B s; C t; T2() { f = 0, s = 0, t = 0; } T2(A f, B s, C t) : f(f), s(s), t(t) {} bool operator<(const T2 &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 ?n s > r.s : t > r.t; 大きい順 */ } bool operator>(const T2 &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 T2 &r) const { return f == r.f && s == r.s && t == r.t; } bool operator!=(const T2 &r) const { return f != r.f || s != r.s || t != r.t; } }; template <class A, class B, class C, class D> struct F2 { A a; B b; C c; D d; F2() { a = 0, b = 0, c = 0, d = 0; } F2(A a, B b, C c, D d) : a(a), b(b), c(c), d(d) {} bool operator<(const F2 &r) const { return a != r.a ? a < r.a : b != r.b ? b < r.b : c != r.c ? c < r.c : d < r.d; /* return a != r.a ? a > r.a : b != r.b ? b > r.b : c != r.c ? c > r.c : d > r.d;*/ } bool operator>(const F2 &r) const { return a != r.a ? a > r.a : b != r.b ? b > r.b : c != r.c ? c > r.c : d > r.d; /* return a != r.a ? a < r.a : b != r.b ? b < r.b : c != r.c ? c < r.c : d < r.d;*/ } bool operator==(const F2 &r) const { return a == r.a && b == r.b && c == r.c && d == r.d; } bool operator!=(const F2 &r) const { return a != r.a || b != r.b || c != r.c || d != r.d; } ll operator[](ll i) { assert(i < 4); return i == 0 ? a : i == 1 ? b : i == 2 ? c : d; } }; typedef T2<ll, ll, ll> T; typedef F2<ll, ll, ll, ll> F; T mt(ll a, ll b, ll c) { return T(a, b, c); } //@マクロ省略系 型,構造 #define double long double #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 beg begin #define rbeg rbegin #define con continue #define bre break #define brk break #define is == #define el else #define elf else if #define wh while #define upd update #define maxq 1 #define minq -1 #define ZERO(a) memset(a, 0, sizeof(a)) #define MINUS(a) memset(a, 0xff, sizeof(a)) #define MALLOC(type, len) (type *)malloc((len) * sizeof(type)) #define lam(right) [&](ll &p) { return p right; } //マクロ省略系 コンテナ 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 PQ priority_queue<ll, vector<ll>, greater<ll>> #define tos to_string using mapi = map<ll, ll>; using mapp = map<P, ll>; using mapd = map<dou, ll>; using mapc = map<char, ll>; using maps = map<str, ll>; using seti = set<ll>; using setd = set<dou>; using setc = set<char>; using sets = set<str>; using qui = queue<ll>; #define bset bitset #define uset unordered_set #define useti unordered_set<ll, ll, xorshift> #define mset multiset #define mseti multiset<ll> #define umap unordered_map #define mmap multimap template <class T> struct pq { priority_queue<T, vector<T>, greater<T>> q; /*小さい順*/ T su = 0; void clear() { q = priority_queue<T, vector<T>, greater<T>>(); su = 0; } void operator+=(T v) { su += v; q.push(v); } T sum() { return su; } T top() { return q.top(); } void pop() { su -= q.top(); q.pop(); } T poll() { T ret = q.top(); su -= ret; q.pop(); return ret; } ll size() { return q.size(); } }; template <class T> struct pqg { priority_queue<T> q; /*大きい順*/ T su = 0; void clear() { q = priority_queue<T>(); su = 0; } void operator+=(T v) { su += v; q.push(v); } T sum() { return su; } T top() { return q.top(); } void pop() { su -= q.top(); q.pop(); } T poll() { T ret = q.top(); su -= ret; q.pop(); return ret; } ll size() { return q.size(); } }; #define pqi pq<ll> #define pqgi pqg<ll> //マクロ 繰り返し #define o_rep(o1, o2, o3, o4, name, ...) name #define rep1(n) for (ll rep1i = 0, rep1lim = n; rep1i < rep1lim; ++rep1i) #define rep2(i, n) for (ll i = 0, rep2lim = n; i < rep2lim; ++i) #define rep3(i, m, n) for (ll i = m, rep3lim = n; i < rep3lim; ++i) #define rep4(i, m, n, ad) for (ll i = m, rep4lim = n; i < rep4lim; i += ad) #define rep(...) o_rep(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__) #define rer2(i, n) for (ll i = n; i >= 0; i--) #define rer3(i, m, n) for (ll i = m, rer3lim = n; i >= rer3lim; i--) #define rer4(i, m, n, dec) for (ll i = m, rer4lim = n; i >= rer4lim; i -= dec) #define rer(...) o_rep(__VA_ARGS__, rer4, rer3, rer2, )(__VA_ARGS__) #define reps2(i, j, n) \ for (ll i = 0, reps2lim = n; i < reps2lim; ++i) \ for (ll j = 0; j < reps2lim; ++j) #define reps3(i, j, k, n) \ for (ll i = 0, reps3lim = n; i < reps3lim; ++i) \ for (ll j = 0; j < reps3lim; ++j) \ for (ll k = 0; k < reps3lim; ++k) #define reps4(i, j, k, l, n) \ for (ll i = 0, reps4lim = n; i < reps4lim; ++i) \ for (ll j = 0; j < reps4lim; ++j) \ for (ll k = 0; k < reps4lim; ++k) \ for (ll l = 0; l < reps4lim; ++l) #define o_reps(o1, o2, o3, o4, o5, name, ...) name #define reps(...) o_reps(__VA_ARGS__, reps4, reps3, reps2, rep2, )(__VA_ARGS__) #define repss(i, j, k, a, b, c) \ for (ll i = 0; i < a; ++i) \ for (ll j = 0; j < b; ++j) \ for (ll k = 0; k < c; ++k) #define fora(a, b) for (auto &&a : b) //インデックスを前後含めて走査 #define fori(i, s, len) \ for (int i = s, prev = (s == 0) ? len - 1 : s - 1, \ next = (s == len - 1) ? 0 : s + 1, cou = 0; \ cou < len; \ cou++, prev = i, i = next, next = (next == len - 1) ? 0 : next + 1) #define form(st, l, r) \ for (auto &&it = st.lower_bound(l); it != st.end() && (*it).fi < r; ++it) #define forit(st, l, r) \ for (auto &&it = st.lower_bound(l); it != st.end() && (*it) < r;) //マクロ 定数 #define k3 1010 #define k4 10101 #define k5 101010 #define k6 1010101 #define k7 10101010 const ll inf = (ll)1e9 + 100; const ll linf = (ll)1e18 + 100; const char infc = '{'; const string infs = "{"; const double eps = 1e-9; const double PI = 3.1415926535897932384626433832795029L; //マクロ省略形 関数等 #define arsz(a) (sizeof(a) / sizeof(a[0])) #define sz(a) ((ll)(a).size()) #define mp make_pair #define pb pop_back #define pf push_front #define eb emplace_back #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() constexpr bool ev(ll a) { return !(a & 1); } constexpr bool od(ll a) { return (a & 1); } //@拡張系 こう出来るべきというもの //埋め込み 存在を意識せずに機能を増やされているもの //@formatter:on namespace std { template <> class hash<std::pair<signed, signed>> { public: size_t operator()(const std::pair<signed, signed> &x) const { return hash<ll>()(((ll)x.first << 32) | x.second); } }; template <> class hash<std::pair<ll, ll>> { public : /*大きいllが渡されると、<<32でオーバーフローするがとりあえず問題ないと判断*/ size_t operator()(const std::pair<ll, ll> &x) const { return hash<ll>()(((ll)x.first << 32) | x.second); } }; } // namespace std //@formatter:off // stream まとめ istream &operator>>(istream &iss, P &a) { iss >> a.first >> a.second; return iss; } template <typename T> istream &operator>>(istream &iss, vector<T> &vec) { for (T &x : vec) iss >> x; return iss; } template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> p) { os << p.fi << " " << p.se << endl; return os; } ostream &operator<<(ostream &os, T p) { os << p.f << " " << p.s << " " << p.t; return os; } ostream &operator<<(ostream &os, F p) { os << p.a << " " << p.b << " " << p.c << " " << p.d; return os; } template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) { for (ll i = 0; i < vec.size(); ++i) os << vec[i] << (i + 1 == vec.size() ? "" : " "); return os; } template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) { for (ll i = 0; i < vec.size(); ++i) { for (ll j = 0; j < vec[i].size(); ++j) { os << vec[i][j] << " "; } os << endl; } return os; } template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &m) { for (auto &&v : m) os << v; return os; } template <class T> ostream &operator<<(ostream &os, set<T> s) { fora(v, s) { os << v << " "; } return os; } template <class T> ostream &operator<<(ostream &os, deque<T> a) { fora(v, a) os << v << " "; return os; } 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); }); } #define all_of(a, right) all_of2(a, lam(right)) #define any_of(a, right) any_of2(a, lam(right)) #define none_of(a, right) none_of2(a, lam(right)) #define find_if(a, right) find_if2(a, lam(right)) #define rfind_if(a, right) rfind_if2(a, lam(right)) #define contains_if(a, right) contains_if2(a, lam(right)) #define count_if(a, right) count_if2(a, lam(right)) #define for_each(a, right) \ do { \ fora(v, a) { v right; } \ } while (0) template <class T, class U> void replace(vector<T> &a, T key, U v) { replace(a.begin(), a.end(), key, v); } void replace(str &a, char key, str v) { if (v == "") a.erase(remove(all(a), key), a.end()); } void replace(str &a, char key, char v) { replace(all(a), key, v); } // keyと同じかどうか01で置き換える template <class T, class U> void replace(vector<T> &a, U k) { rep(i, sz(a)) a[i] = a[i] == k; } template <class T, class U> void replace(vector<vector<T>> &a, U k) { rep(i, sz(a)) rep(j, sz(a[0])) a[i][j] = a[i][j] == k; } template <class T> void replace(T &a) { replace(a, '#'); } void replace(str &a, str key, str v) { stringstream t; ll kn = sz(key); std::string::size_type Pos(a.find(key)); ll l = 0; while (Pos != std::string::npos) { t << a.substr(l, Pos - l); t << v; l = Pos + kn; Pos = a.find(key, Pos + kn); } t << a.substr(l, sz(a) - l); a = t.str(); } template <class T> bool includes(vector<T> &a, vector<T> &b) { vi c = a; vi d = b; sort(all(c)); sort(all(d)); return includes(all(c), all(d)); } template <class T> bool is_permutation(vector<T> &a, vector<T> &b) { return is_permutation(all(a), all(b)); } template <class T> bool next_permutation(vector<T> &a) { return next_permutation(all(a)); } void iota(vector<ll> &ve, ll s, ll n) { ve.resize(n); iota(all(ve), s); } vi iota(ll s, ll len) { vi ve(len); iota(all(ve), s); return ve; } template <class A, class B> auto vtop(vector<A> &a, vector<B> &b) { assert(sz(a) == sz(b)); /*stringを0で初期化できない */ vector<pair<A, B>> res; rep(i, sz(a)) res.eb(a[i], b[i]); return res; } template <class A, class B> void ptov(vector<pair<A, B>> &p, vector<A> &a, vector<B> &b) { a.resize(sz(p)), b.resize(sz(p)); rep(i, sz(p)) a[i] = p[i].fi, b[i] = p[i].se; } template <class A, class B, class C> auto vtot(vector<A> &a, vector<B> &b, vector<C> &c) { assert(sz(a) == sz(b) && sz(b) == sz(c)); vector<T2<A, B, C>> res; rep(i, sz(a)) res.eb(a[i], b[i], c[i]); return res; } template <class A, class B, class C, class D> auto vtof(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { assert(sz(a) == sz(b) && sz(b) == sz(c) && sz(c) == sz(d)); vector<F2<A, B, C, D>> res; rep(i, sz(a)) res.eb(a[i], b[i], c[i], d[i]); return res; } enum pcomparator { fisi, fisd, fdsi, fdsd, sifi, sifd, sdfi, sdfd }; enum tcomparator { fisiti, fisitd, fisdti, fisdtd, fdsiti, fdsitd, fdsdti, fdsdtd, fitisi, fitisd, fitdsi, fitdsd, fdtisi, fdtisd, fdtdsi, fdtdsd, sifiti, sifitd, sifdti, sifdtd, sdfiti, sdfitd, sdfdti, sdfdtd, sitifi, sitifd, sitdfi, sitdfd, sdtifi, sdtifd, sdtdfi, sdfdfd, tifisi, tifisd, tifdsi, tifdsd, tdfisi, tdfisd, tdfdsi, tdfdsd, tisifi, tisifd, tisdfi, tisdfd, tdsifi, tdsifd, tdsdfi, tdsdfd }; template <class A, class B> void sort(vector<pair<A, B>> &a, pcomparator type) { typedef pair<A, B> U; if (type == fisi) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se < r.se; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se > r.se; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se < r.se; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se > r.se; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi < r.fi; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi > r.fi; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi < r.fi; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi > r.fi; }); }; template <class U> void sort(vector<U> &a, pcomparator type) { if (type == fisi) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f > r.f; }); }; template <class A, class B, class C, class D> void sort(vector<F2<A, B, C, D>> &a, pcomparator type) { typedef F2<A, B, C, D> U; if (type == fisi) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a > r.a; }); }; template <class U> void sort(vector<U> &a, tcomparator type) { if (type == 0) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t < r.t; }); else if (type == 1) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t > r.t; }); else if (type == 2) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t < r.t; }); else if (type == 3) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t > r.t; }); else if (type == 4) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t < r.t; }); else if (type == 5) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t > r.t; }); else if (type == 6) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t < r.t; }); else if (type == 7) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t > r.t; }); else if (type == 8) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s < r.s; }); else if (type == 9) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s > r.s; }); else if (type == 10) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s < r.s; }); else if (type == 11) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s > r.s; }); else if (type == 12) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s < r.s; }); else if (type == 13) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s > r.s; }); else if (type == 14) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s < r.s; }); else if (type == 15) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s > r.s; }); else if (type == 16) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t < r.t; }); else if (type == 17) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t > r.t; }); else if (type == 18) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t < r.t; }); else if (type == 19) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t > r.t; }); else if (type == 20) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t < r.t; }); else if (type == 21) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t > r.t; }); else if (type == 22) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t < r.t; }); else if (type == 23) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t > r.t; }); else if (type == 24) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f < r.f; }); else if (type == 25) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f > r.f; }); else if (type == 26) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f < r.f; }); else if (type == 27) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f > r.f; }); else if (type == 28) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f < r.f; }); else if (type == 29) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f > r.f; }); else if (type == 30) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f < r.f; }); else if (type == 31) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f > r.f; }); else if (type == 32) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == 33) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == 34) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == 35) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == 36) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == 37) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == 38) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == 39) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == 40) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == 41) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == 42) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == 43) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f > r.f; }); else if (type == 44) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == 45) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == 46) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == 47) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f > r.f; }); } template <class A, class B, class C, class D> void sort(vector<F2<A, B, C, D>> &a, tcomparator type) { typedef F2<A, B, C, D> U; if (type == 0) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c < r.c; }); else if (type == 1) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c > r.c; }); else if (type == 2) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c < r.c; }); else if (type == 3) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c > r.c; }); else if (type == 4) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c < r.c; }); else if (type == 5) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c > r.c; }); else if (type == 6) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c < r.c; }); else if (type == 7) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c > r.c; }); else if (type == 8) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b < r.b; }); else if (type == 9) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b > r.b; }); else if (type == 10) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b < r.b; }); else if (type == 11) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b > r.b; }); else if (type == 12) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b < r.b; }); else if (type == 13) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b > r.b; }); else if (type == 14) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b < r.b; }); else if (type == 15) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b > r.b; }); else if (type == 16) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c < r.c; }); else if (type == 17) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c > r.c; }); else if (type == 18) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c < r.c; }); else if (type == 19) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c > r.c; }); else if (type == 20) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c < r.c; }); else if (type == 21) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c > r.c; }); else if (type == 22) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c < r.c; }); else if (type == 23) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c > r.c; }); else if (type == 24) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a < r.a; }); else if (type == 25) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a > r.a; }); else if (type == 26) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a < r.a; }); else if (type == 27) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a > r.a; }); else if (type == 28) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a < r.a; }); else if (type == 29) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a > r.a; }); else if (type == 30) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a < r.a; }); else if (type == 31) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a > r.a; }); else if (type == 32) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == 33) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == 34) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == 35) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == 36) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == 37) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == 38) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == 39) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == 40) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == 41) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == 42) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == 43) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a > r.a; }); else if (type == 44) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == 45) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == 46) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == 47) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a > r.a; }); } void sort(string &a) { sort(all(a)); } template <class T> void sort(vector<T> &a) { sort(all(a)); } // P l, P rで f(P) の形で渡す template <class U, class F> void sort(vector<U> &a, F f) { sort(all(a), [&](U l, U r) { return f(l) < f(r); }); }; template <class T> void rsort(vector<T> &a) { sort(all(a), greater<T>()); }; template <class U, class F> void rsort(vector<U> &a, F f) { sort(all(a), [&](U l, U r) { return f(l) > f(r); }); }; // F = T<T> //例えばreturn p.fi + p.se; template <class A, class B> void sortp(vector<A> &a, vector<B> &b) { auto c = vtop(a, b); sort(c); rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se; } template <class A, class B, class F> void sortp(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); sort(c, f); rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se; } template <class A, class B> void rsortp(vector<A> &a, vector<B> &b) { auto c = vtop(a, b); rsort(c); rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second; } template <class A, class B, class F> void rsortp(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); rsort(c, f); rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second; } template <class A, class B, class C> void sortt(vector<A> &a, vector<B> &b, vector<C> &c) { auto d = vtot(a, b, c); sort(d); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class F> void sortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); sort(d, f); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C> void rsortt(vector<A> &a, vector<B> &b, vector<C> &c) { auto d = vtot(a, b, c); rsort(d); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class F> void rsortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); rsort(d, f); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class D> void sortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { auto e = vtof(a, b, c, d); sort(e); rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d; } template <class A, class B, class C, class D> void rsortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { auto e = vtof(a, b, c, d); rsort(e); rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d; } // sortindex 元のvectorはソートしない template <class T> vi sorti(vector<T> &a) { auto b = a; vi ind = iota(0, sz(a)); sortp(b, ind); return ind; } /*indexの分で型が変わるためpcomparatorが必要*/ template <class T> vi sorti(vector<T> &a, pcomparator f) { auto b = a; vi ind = iota(0, sz(a)); sortp(b, ind, f); return ind; } template <class T, class F> vi sorti(vector<T> &a, F f) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(a[x]) < f(a[y]); }); return ind; } template <class T> vi rsorti(vector<T> &a) { auto b = a; vi ind = iota(0, sz(a)); rsortp(b, ind); return ind; } template <class T, class F> vi rsorti(vector<T> &a, F f) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(a[x]) > f(a[y]); }); return ind; } template <class A, class B, class F> vi sortpi(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(c[x]) < f(c[y]); }); return ind; } template <class A, class B> vi sortpi(vector<A> &a, vector<B> &b, pcomparator f) { vi ind = iota(0, sz(a)); auto c = a; auto d = b; sortt(c, d, ind, f); return ind; } template <class A, class B> vi sortpi(vector<A> &a, vector<B> &b) { return sortpi(a, b, fisi); }; template <class A, class B, class F> vi rsortpi(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(c[x]) > f(c[y]); }); return ind; } template <class A, class B> vi rsortpi(vector<A> &a, vector<B> &b) { return sortpi(a, b, fdsd); }; template <class A, class B, class C, class F> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(d[x]) < f(d[y]); }); return ind; } template <class A, class B, class C> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, pcomparator f) { vi ind = iota(0, sz(a)); auto d = vtof(a, b, c, ind); sort(d, f); rep(i, sz(a)) ind[i] = d[i].d; return ind; } template <class A, class B, class C> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { if (a[x] == a[y]) { if (b[x] == b[y]) return c[x] < c[y]; else return b[x] < b[y]; } else { return a[x] < a[y]; } }); return ind; } template <class A, class B, class C, class F> vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(d[x]) > f(d[y]); }); return ind; } template <class A, class B, class C> vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { if (a[x] == a[y]) { if (b[x] == b[y]) return c[x] > c[y]; else return b[x] > b[y]; } else { return a[x] > a[y]; } }); return ind; } template <class T> void sort2(vector<vector<T>> &a) { for (ll i = 0, n = a.size(); i < n; ++i) sort(a[i]); } template <class T> void rsort2(vector<vector<T>> &a) { for (ll 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 W, typename T> void fill(W &xx, const T vall) { xx = vall; } template <typename W, typename T> void fill(vector<W> &vecc, const T vall) { for (auto &&vx : vecc) fill(vx, vall); } template <typename W, typename T> void fill(vector<W> &xx, const T v, ll len) { rep(i, len) xx[i] = v; } template <typename W, typename T> void fill(vector<vector<W>> &xx, const T v, ll lh, ll lw) { rep(i, lh) rep(j, lw) xx[i][j] = v; } template <class T, class U> void fill(vector<T> &a, U val, vi &ind) { fora(v, ind) a[v] = val; } template <typename A, size_t N> A sum(A (&a)[N]) { A res = 0; rep(i, N) res += a[i]; return res; } template <typename A, size_t N, size_t O> A sum(A (&a)[N][O]) { A res = 0; rep(i, N) rep(j, O) res += a[i][j]; return res; } template <typename A, size_t N, size_t O, size_t P> A sum(A (&a)[N][O][P]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) res += a[i][j][k]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A sum(A (&a)[N][O][P][Q]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) res += a[i][j][k][l]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A sum(A (&a)[N][O][P][Q][R]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) res += a[i][j][k][l][m]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A sum(A (&a)[N][O][P][Q][R][S]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) rep(n, S) res += a[i][j][k][l][m][n]; return res; } //@汎用便利関数 入力 ll in() { ll ret; cin >> ret; return ret; } string sin() { string ret; cin >> ret; return ret; } template <class T> void in(T &head) { cin >> head; } template <class T, class... U> void in(T &head, U &...tail) { cin >> head; in(tail...); } #define o_din(o1, o2, o3, o4, o5, o6, name, ...) name #define din1(a) \ ll a; \ cin >> a #define din2(a, b) \ ll a, b; \ cin >> a >> b #define din3(a, b, c) \ ll a, b, c; \ cin >> a >> b >> c #define din4(a, b, c, d) \ ll a, b, c, d; \ cin >> a >> b >> c >> d #define din5(a, b, c, d, e) \ ll a, b, c, d, e; \ cin >> a >> b >> c >> d >> e #define din6(a, b, c, d, e, f) \ ll a, b, c, d, e, f; \ cin >> a >> b >> c >> d >> e >> f #define din(...) \ o_din(__VA_ARGS__, din6, din5, din4, din3, din2, din1)(__VA_ARGS__) #define o_dind(o1, o2, o3, o4, name, ...) name #define din1d(a) \ din1(a); \ a-- #define din2d(a, b) \ din2(a, b); \ a--, b-- #define din3d(a, b, c) \ din3(a, b, c); \ a--, b--, c-- #define din4d(a, b, c, d) \ din4(a, b, c, d); \ a--, b--, c--, d-- #define dind(...) o_dind(__VA_ARGS__, din4d, din3d, din2d, din1d)(__VA_ARGS__) template <class T> void out2(T &&head) { cout << head; } template <class T, class... U> void out2(T &&head, U &&...tail) { cout << head << " "; out2(tail...); } template <class T, class... U> void out(T &&head, U &&...tail) { cout << head << " "; out2(tail...); cout << "" << endl; } template <class T> void out(T &&head) { cout << head << endl; } void out() { cout << "" << endl; } #ifdef _DEBUG 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 (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) { ; } #endif template <class T> void outl(vector<T> &a, int n = inf) { rep(i, min(n, sz(a))) cout << a[i] << endl; } template <class T> void na(vector<T> &a, ll n) { a.resize(n); rep(i, n) cin >> a[i]; } #define dna(a, n) \ vi a(n); \ rep(dnai, n) cin >> a[dnai]; template <class T> void nao(vector<T> &a, ll n) { a.resize(n + 1); a[0] = 0; rep(i, n) cin >> a[i + 1]; } template <class T> void naod(vector<T> &a, ll n) { a.resize(n + 1); a[0] = 0; rep(i, n) cin >> a[i + 1], a[i + 1]--; } template <class T> void nad(vector<T> &a, ll n) { a.resize(n); rep(i, n) cin >> a[i], a[i]--; } template <class T, class U> void na2(vector<T> &a, vector<U> &b, ll n) { a.resize(n); b.resize(n); rep(i, n) cin >> a[i] >> b[i]; } #define dna2(a, b, n) \ vi a(n), b(n); \ rep(dna2i, n) cin >> a[dna2i] >> b[dna2i]; template <class T, class U> void nao2(vector<T> &a, vector<U> &b, ll n) { a.resize(n + 1); b.resize(n + 1); a[0] = b[0] = 0; rep(i, n) cin >> a[i + 1] >> b[i + 1]; } #define dna2d(a, b, n) \ vi a(n), b(n); \ rep(dna2di, n) { \ cin >> a[dna2di] >> b[dna2di]; \ a[dna2di]--, b[dna2di]--; \ } template <class T, class U> void na2d(vector<T> &a, vector<U> &b, ll n) { a.resize(n); b.resize(n); rep(i, n) cin >> a[i] >> b[i], a[i]--, b[i]--; } template <class T, class U, class W> void na3(vector<T> &a, vector<U> &b, vector<W> &c, ll n) { a.resize(n); b.resize(n); c.resize(n); rep(i, n) cin >> a[i] >> b[i] >> c[i]; } #define dna3(a, b, c, n) \ vi a(n), b(n), c(n); \ rep(dna3i, n) cin >> a[dna3i] >> b[dna3i] >> c[dna3i]; template <class T, class U, class W> void na3d(vector<T> &a, vector<U> &b, vector<W> &c, ll 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 dna3d(a, b, c, n) \ vi a(n), b(n), c(n); \ rep(dna3di, n) { \ cin >> a[dna3di] >> b[dna3di] >> c[dna3di]; \ a[dna3di]--, b[dna3di]--, c[dna3di]--; \ } template <class T, class U, class W, class X> void na4(vector<T> &a, vector<U> &b, vector<W> &c, vector<X> &d, ll n) { a.resize(n); b.resize(n); c.resize(n); d.resize(n); rep(i, n) cin >> a[i] >> b[i] >> c[i] >> d[i]; } #define dna4(a, b, c, d, n) \ vi a(n), b(n), c(n), d(n); \ rep(dna4i, n) cin >> a[dna4i] >> b[dna4i] >> c[dna4i] >> d[dna4i]; #define dna4d(a, b, c, d, n) \ vi a(n), b(n), c(n), d(n); \ rep(dna4i, n) cin >> a[dna4i] >> b[dna4i] >> c[dna4i] >> d[dna4i], \ --a[dna4i], --b[dna4i], --c[dna4i], --d[dna4i]; #define nt(a, h, w) \ resize(a, h, w); \ rep(nthi, h) rep(ntwi, w) cin >> a[nthi][ntwi]; #define ntd(a, h, w) \ resize(a, h, w); \ rep(ntdhi, h) rep(ntdwi, w) cin >> a[ntdhi][ntdwi], a[ntdhi][ntdwi]--; #define ntp(a, h, w) \ resize(a, h + 2, w + 2); \ fill(a, '#'); \ rep(ntphi, 1, h + 1) rep(ntpwi, 1, w + 1) cin >> a[ntphi][ntpwi]; //デバッグ #define sp << " " << #define debugName(VariableName) #VariableName #define deb1(x) debugName(x) << " = " << out_m(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' //@formatter:off //よく使うクラス、構造体 struct unionfind { vector<ll> par; vector<ll> siz; vector<ll> es; ll n, trees; //連結グループの数(親の種類) unionfind(ll n) : n(n), trees(n) { par.resize(n); siz.resize(n); es.resize(n); for (ll i = 0; i < n; i++) { par[i] = i; siz[i] = 1; } } ll root(ll x) { if (par[x] == x) { return x; } else { return par[x] = root(par[x]); } } bool unite(ll x, ll y) { x = root(x); y = root(y); es[x]++; if (x == y) return false; if (siz[x] > siz[y]) swap(x, y); trees--; par[x] = y; siz[y] += siz[x]; es[y] += es[x]; return true; } bool same(ll x, ll y) { return root(x) == root(y); } ll size(ll x) { return siz[root(x)]; } ll esize(ll x) { return es[root(x)]; } vi sizes() { vi cou(n); vi ret; ret.reserve(n); rep(i, n) { cou[root(i)]++; } rep(i, n) { if (cou[i]) ret.push_back(cou[i]); } return ret; } //つながりを無向グラフと見なし、xが閉路に含まれるか判定 bool close(ll x) { return esize(x) >= size(x); } vec<vi> sets() { vi ind(n, -1); ll i = 0; vvi(res, trees); rep(j, n) { ll r = root(j); if (ind[r] == -1) ind[r] = i++; res[ind[r]].push_back(j); } rep(i, trees) { ll r = root(res[i][0]); if (res[i][0] == r) continue; rep(j, 1, sz(res[i])) { if (res[i][j] == r) { swap(res[i][0], res[i][j]); break; } } } return res; } }; //@formatter:off using bll = __int128; using u32 = unsigned; using u64 = unsigned long long; using u128 = __uint128_t; std::ostream &operator<<(std::ostream &dest, __int128_t value) { std::ostream::sentry s(dest); if (s) { __uint128_t tmp = value < 0 ? -value : value; char buffer[128]; char *d = std::end(buffer); do { --d; *d = "0123456789"[tmp % 10]; tmp /= 10; } while (tmp != 0); if (value < 0) { --d; *d = '-'; } ll len = std::end(buffer) - d; if (dest.rdbuf()->sputn(d, len) != len) { dest.setstate(std::ios_base::badbit); } } return dest; } //__int128 toi128(string &s) { __int128 ret = 0; for (ll i = 0; i < //s.length(); ++i) if ('0' <= s[i] && s[i] <= '9') ret = 10 * //ret + s[i] - '0'; return ret;} //エラー 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 re() { assert(0 == 1); exit(0); } void tle() { while (inf) cout << inf << endl; } //便利関数 //テスト用 char ranc() { return (char)('a' + rand() % 26); } ll rand(ll min, ll max) { assert(min <= max); if (min >= 0 && max >= 0) { return rand() % (max + 1 - min) + min; } else if (max < 0) { return -rand(-max, -min); } else { if (rand() % 2) { return rand(0, max); } else { return -rand(0, -min); } } } vi ranv(ll n, ll min, ll max) { vi v(n); rep(i, n) v[i] = rand(min, max); return v; } str ransu(ll n) { str s; rep(i, n) s += (char)rand('A', 'Z'); return s; } str ransl(ll n) { str s; rep(i, n) s += (char)rand('a', 'z'); return s; } //単調増加 vi ranvinc(ll n, ll min, ll max) { vi v(n); bool bad = 1; while (bad) { bad = 0; v.resize(n); rep(i, n) { if (i && min > max - v[i - 1]) { bad = 1; break; } if (i) v[i] = v[i - 1] + rand(min, max - v[i - 1]); else v[i] = rand(min, max); } } return v; } //便利 汎用 void ranvlr(ll n, ll min, ll max, vi &l, vi &r) { l.resize(n); r.resize(n); rep(i, n) { l[i] = rand(min, max); r[i] = l[i] + rand(0, max - l[i]); } } 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 <class F> ll mgr(ll ok, ll ng, F f) { if (ok < ng) while (ng - ok > 1) { ll mid = (ok + ng) / 2; if (f(mid)) ok = mid; else ng = mid; } else while (ok - ng > 1) { ll mid = (ok + ng) / 2; if (f(mid)) ok = mid; else ng = mid; } return ok; } // strを整数として比較 string smax(str &a, str b) { if (sz(a) < sz(b)) { return b; } else if (sz(a) > sz(b)) { return a; } else if (a < b) return b; else return a; } // strを整数として比較 string smin(str &a, str b) { if (sz(a) > sz(b)) { return b; } else if (sz(a) < sz(b)) { return a; } else if (a > b) return b; else return a; } template <typename W, typename T> ll find(vector<W> &a, const T key) { rep(i, sz(a)) if (a[i] == key) return i; return -1; } template <typename W, typename T> P find(vector<vector<W>> &a, const T key) { rep(i, sz(a)) rep(j, sz(a[0])) if (a[i][j] == key) return mp(i, j); return mp(-1, -1); } template <typename W, typename U> T find(vector<vector<vector<W>>> &a, const U key) { rep(i, sz(a)) rep(j, sz(a[0])) rep(k, sz(a[0][0])) if (a[i][j][k] == key) return mt(i, j, k); return mt(-1, -1, -1); } template <typename W, typename T> ll count2(W &a, const T k) { return a == k; } template <typename W, typename T> ll count2(vector<W> &a, const T k) { ll ret = 0; fora(v, a) ret += count2(v, k); return ret; } template <typename W, typename T> ll count(vector<W> &a, const T k) { ll ret = 0; fora(v, a) ret += count2(v, k); return ret; } ll count(str &a, str k) { ll ret = 0, len = k.length(); auto pos = a.find(k); while (pos != string::npos) pos = a.find(k, pos + len), ++ret; return ret; } vi count(str &a) { vi cou(26); char c = 'a'; if ('A' <= a[0] && a[0] <= 'Z') c = 'A'; rep(i, sz(a))++ cou[a[i] - c]; return cou; } #define couif count_if // algorythm 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)); } template <class U> void rev(vector<vector<U>> &a) { vector<vector<U>> b(sz(a[0]), vector<U>(sz(a))); rep(h, sz(a)) rep(w, sz(a[0])) b[w][h] = a[h][w]; a = b; } void rev(string &a) { reverse(all(a)); } constexpr ll p10[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000ll, 100000000000ll, 1000000000000ll, 10000000000000ll, 100000000000000ll, 1000000000000000ll, 10000000000000000ll, 100000000000000000ll, 1000000000000000000ll}; ll get(ll a, ll keta) { return (a / (ll)pow(10, keta)) % 10; } ll keta(ll v) { if (v < p10[9]) { if (v < p10[4]) { if (v < p10[2]) { if (v < p10[1]) return 1; else return 2; } else { if (v < p10[3]) return 3; else return 4; } } else { if (v < p10[7]) { if (v < p10[5]) return 5; else if (v < p10[6]) return 6; else return 7; } else { if (v < p10[8]) return 8; else return 9; } } } else { if (v < p10[13]) { if (v < p10[11]) { if (v < p10[10]) return 10; else return 11; } else { if (v < p10[12]) return 12; else return 13; } } else { if (v < p10[15]) { if (v < p10[14]) return 14; else if (v < p10[15]) return 15; else return 16; } else { if (v < p10[17]) return 17; else return 18; } } } } ll dsum(ll v, ll sin = 10) { ll ret = 0; for (; v; v /= sin) ret += v % sin; return ret; } struct sint { ll v; sint(ll v) : v(v) {} operator ll() { return v; } //下からi番目 ll operator[](ll i) { return (v / p10[i]) % 10; } ll back(ll i) { return operator[](i); } //上からi番目 ll top(ll i) { ll len = keta(v); return operator[](len - 1 - i); } //先頭からi番目にセット ll settop(ll i, ll k) { ll len = keta(v); return set(len - 1 - i, k); } ll set(ll i, ll k) { if (i < 0) return settop(abs(i) - 1, k); return v += p10[i] * (k - (v / p10[i]) % 10); } ll add(ll i, ll k = 1) { return v += p10[i] * k; } ll addtop(ll i, ll k = 1) { return v += p10[keta(v) - i - 1] * k; } ll dec(ll i, ll k = 1) { return v -= p10[i] * k; } ll dectop(ll i, ll k = 1) { return v -= p10[keta(v) - i - 1] * k; } #define op(t, o) \ template <class T> t operator o(T r) { return v o r; } op(ll, +=); op(ll, -=); op(ll, *=); op(ll, /=); op(ll, %=); op(ll, +); op(ll, -); op(ll, *); op(ll, /); op(ll, %); op(bool, ==); op(bool, !=); op(bool, <); op(bool, <=); op(bool, >); op(bool, >=); #undef op template <class T> ll operator<<=(T r) { return v *= p10[r]; } template <class T> ll operator<<(T r) { return v * p10[r]; } template <class T> ll operator>>=(T r) { return v /= p10[r]; } template <class T> ll operator>>(T r) { return v / p10[r]; } }; ll mask10(ll v) { return p10[v] - 1; } //変換系 template <class T> auto keys(T &a) { vector<decltype((a.begin())->fi)> res; for (auto &&k : a) res.push_back(k.fi); return res; } template <class T> auto values(T &a) { vector<decltype((a.begin())->se)> res; for (auto &&k : a) res.push_back(k.se); return res; } template <class T, class U> bool chma(T &a, const U &b) { if (a < b) { a = b; return true; } return false; } template <class T, class U> bool chmi(T &a, const U &b) { if (b < a) { a = b; return true; } return false; } template <class T> T min(T a, signed b) { return a < b ? a : b; } template <class T> T max(T a, signed b) { return a < b ? b : a; } template <class T> T min(T a, T b, T c) { return a >= b ? b >= c ? c : b : a >= c ? c : a; } template <class T> T max(T a, T b, T c) { return a <= b ? b <= c ? c : b : a <= c ? c : a; } template <class T> T min(vector<T> &a) { return *min_element(all(a)); } template <class T> T mini(vector<T> &a) { return min_element(all(a)) - a.begin(); } template <class T> T min(vector<T> &a, ll n) { return *min_element(a.begin(), a.begin() + min(n, sz(a))); } template <class T> T min(vector<T> &a, ll s, ll n) { return *min_element(a.begin() + s, a.begin() + min(n, sz(a))); } template <class T> T max(vector<T> &a) { return *max_element(all(a)); } template <class T, class U> T max(vector<T> &a, vector<U> &b) { return max(*max_element(all(a)), *max_element(all(b))); } template <class T> T maxi(vector<T> &a) { return max_element(all(a)) - a.begin(); } template <class T> T max(vector<T> &a, ll n) { return *max_element(a.begin(), a.begin() + min(n, sz(a))); } template <class T> T max(vector<T> &a, ll s, ll n) { return *max_element(a.begin() + s, a.begin() + min(n, sz(a))); } template <typename A, size_t N> A max(A (&a)[N]) { A res = a[0]; rep(i, N) res = max(res, a[i]); return res; } template <typename A, size_t N, size_t O> A max(A (&a)[N][O]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P> A max(A (&a)[N][O][P]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A max(A (&a)[N][O][P][Q], const T &v) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A max(A (&a)[N][O][P][Q][R]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A max(A (&a)[N][O][P][Q][R][S]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N> A min(A (&a)[N]) { A res = a[0]; rep(i, N) res = min(res, a[i]); return res; } template <typename A, size_t N, size_t O> A min(A (&a)[N][O]) { A res = min(a[0]); rep(i, N) res = min(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P> A min(A (&a)[N][O][P]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A min(A (&a)[N][O][P][Q], const T &v) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A min(A (&a)[N][O][P][Q][R]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A min(A (&a)[N][O][P][Q][R][S]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <class T> T sum(vector<T> &v, ll s, ll t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += v[i]; return ret; } template <class T> T sum(vector<T> &v, ll t = inf) { return sum(v, 0, t); } template <class T> T sum(vector<vector<T>> &v) { T ret = 0; rep(i, sz(v)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<T>>> &v) { T ret = 0; rep(i, sz(v)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<vector<T>>>> &v) { T ret = 0; rep(i, sz(v)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<vector<vector<T>>>>> &v) { T ret = 0; rep(i, sz(v)) ret += sum(v[i]); return ret; } template <class T> auto sum(priority_queue<T, vector<T>, greater<T>> &r) { auto q = r; T ret = 0; while (sz(q)) { ret += q.top(); q.pop(); } return ret; } template <class T> auto sum(priority_queue<T> &r) { auto q = r; T ret = 0; while (sz(q)) { ret += q.top(); q.pop(); } return ret; } // template<class T, class U, class... W> auto sumn(vector<T> &v, U head, W... // tail) { auto ret = sum(v[0], tail...); rep(i, 1, min(sz(v), head))ret += // sum(v[i], tail...); return ret;} void clear(PQ &q) { q = PQ(); } template <class T> void clear(queue<T> &q) { while (q.size()) q.pop(); } template <class T> T *negarr(ll size) { T *body = (T *)malloc((size * 2 + 1) * sizeof(T)); return body + size; } template <class T> T *negarr2(ll h, ll w) { double **dummy1 = new double *[2 * h + 1]; double *dummy2 = new double[(2 * h + 1) * (2 * w + 1)]; dummy1[0] = dummy2 + w; for (ll i = 1; i <= 2 * h + 1; ++i) { dummy1[i] = dummy1[i - 1] + 2 * w + 1; } double **a = dummy1 + h; return a; } // 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> void inc(T &a, U v = 1) { a += v; } template <class T, class U> void inc(vector<T> &a, U v = 1) { for (auto &u : a) inc(u, v); } template <class T, class U> void dec(T &a, U v = 1) { a -= v; } template <class T, class U> void dec(vector<T> &a, U v = 1) { for (auto &u : a) dec(u, v); } template <class U> void dec(string &a, U v = 1) { for (auto &u : a) dec(u, v); } template <class T> void dec(vector<T> &a) { for (auto &u : a) dec(u, 1); } template <class T, class U> void dec(vector<T> &a, vector<U> &b) { for (auto &u : a) dec(u, 1); for (auto &u : b) dec(u, 1); } template <class T, class U, class W> void dec(vector<T> &a, vector<U> &b, vector<W> &c) { for (auto &u : a) dec(u, 1); for (auto &u : b) dec(u, 1); for (auto &u : c) dec(u, 1); } bool ins(ll h, ll w, ll H, ll W) { return h >= 0 && w >= 0 && h < H && w < W; } bool ins(ll l, ll v, ll r) { return l <= v && v < r; } template <class T> bool ins(vector<T> &a, ll i, ll j = 0) { return ins(0, i, sz(a)) && ins(0, j, sz(a)); } 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() //添え字を返す template <class F> ll goldd_l(ll left, ll right, F calc) { double GRATIO = 1.6180339887498948482045868343656; ll lm = left + (ll)((right - left) / (GRATIO + 1.0)); ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); ll fl = calc(lm); ll fr = calc(rm); while (right - left > 10) { if (fl < fr) { right = rm; rm = lm; fr = fl; lm = left + (ll)((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } ll minScore = MAX(ll); ll resIndex = left; for (ll i = left; i < right + 1; ++i) { ll score = calc(i); if (minScore > score) { minScore = score; resIndex = i; } } return resIndex; } template <class F> ll goldt_l(ll left, ll right, F calc) { double GRATIO = 1.6180339887498948482045868343656; ll lm = left + (ll)((right - left) / (GRATIO + 1.0)); ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); ll fl = calc(lm); ll fr = calc(rm); while (right - left > 10) { if (fl > fr) { right = rm; rm = lm; fr = fl; lm = left + (ll)((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } if (left > right) { ll l = left; left = right; right = l; } ll maxScore = MIN(ll); ll resIndex = left; for (ll i = left; i < right + 1; ++i) { ll score = calc(i); if (maxScore < score) { maxScore = score; resIndex = i; } } return resIndex; } /*loopは200にすればおそらく大丈夫 余裕なら300に*/ template <class F> dou goldd_d(dou left, dou right, F calc, ll loop = 200) { dou GRATIO = 1.6180339887498948482045868343656; dou lm = left + ((right - left) / (GRATIO + 1.0)); dou rm = lm + ((right - lm) / (GRATIO + 1.0)); dou fl = calc(lm); dou fr = calc(rm); /*200にすればおそらく大丈夫*/ /*余裕なら300に*/ ll k = 141; loop++; while (--loop) { if (fl < fr) { right = rm; rm = lm; fr = fl; lm = left + ((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + ((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } return left; } template <class F> dou goldt_d(dou left, dou right, F calc, ll loop = 200) { double GRATIO = 1.6180339887498948482045868343656; dou lm = left + ((right - left) / (GRATIO + 1.0)); dou rm = lm + ((right - lm) / (GRATIO + 1.0)); dou fl = calc(lm); dou fr = calc(rm); loop++; while (--loop) { if (fl > fr) { right = rm; rm = lm; fr = fl; lm = left + ((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + ((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } return left; } // l ~ rを複数の区間に分割し、極致を与えるiを返す time-20 msまで探索 template <class F> ll goldd_ls(ll l, ll r, F calc, ll time = 2000) { auto lim = milliseconds(time - 20); ll mini = 0, minv = MAX(ll); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/ ll nl = l; ll nr = l + haba; rep(i, k) { ll ni = goldd_l(nl, nr, calc); if (chmi(minv, calc(ni))) mini = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return mini; } template <class F> ll goldt_ls(ll l, ll r, F calc, ll time = 2000) { auto lim = milliseconds(time - 20); ll maxi = 0, maxv = MIN(ll); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/ ll nl = l; ll nr = l + haba; rep(i, k) { ll ni = goldt_l(nl, nr, calc); if (chma(maxv, calc(ni))) maxi = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return maxi; } template <class F> dou goldd_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を持つ*/ auto lim = milliseconds(time - 20); dou mini = 0, minv = MAX(dou); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); dou haba = (r - l) / k; dou nl = l; dou nr = l + haba; rep(i, k) { dou ni = goldd_d(nl, nr, calc); if (chmi(minv, calc(ni))) mini = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return mini; } template <class F> dou goldt_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を残している*/ auto lim = milliseconds(time - 20); dou maxi = 0, maxv = MIN(dou); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); dou haba = (r - l) / k; dou nl = l; dou nr = l + haba; rep(i, k) { dou ni = goldt_d(nl, nr, calc); if (chma(maxv, calc(ni))) maxi = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return maxi; } template <class T> T min(vector<vector<T>> &a) { T res = MAX(T); rep(i, a.size()) chmi(res, *min_element(all(a[i]))); return res; } template <class T> T max(vector<vector<T>> &a) { T res = MIN(T); rep(i, a.size()) chma(res, *max_element(all(a[i]))); return res; } constexpr bool bget(ll m, ll keta) { return (m >> keta) & 1; } ll bget(ll m, ll keta, ll sinsuu) { m /= (ll)pow(sinsuu, keta); return m % sinsuu; } ll bit(ll n) { return (1LL << (n)); } ll bit(ll n, ll sinsuu) { return (ll)pow(sinsuu, n); } ll mask(ll n) { return (1ll << n) - 1; } #define bcou __builtin_popcountll //最下位ビット ll lbit(ll n) { return n & -n; } //最上位ビット ll hbit(ll n) { n |= (n >> 1); n |= (n >> 2); n |= (n >> 4); n |= (n >> 8); n |= (n >> 16); n |= (n >> 32); return n - (n >> 1); } ll hbitk(ll n) { ll k = 0; rer(i, 5) { ll a = k + (1ll << i); ll b = 1ll << a; if (b <= n) k += 1ll << i; } return k; } //初期化は0を渡す ll nextComb(ll &mask, ll n, ll r) { if (!mask) return mask = (1LL << r) - 1; ll x = mask & -mask; /*最下位の1*/ ll y = mask + x; /*連続した下の1を繰り上がらせる*/ ll res = ((mask & ~y) / x >> 1) | y; if (bget(res, n)) return mask = 0; else return mask = res; } // n桁以下でビットがr個立っているもののvectorを返す vi bitCombList(ll n, ll r) { vi res; ll m = 0; while (nextComb(m, n, r)) { res.push_back(m); } return res; } // masの立ってるindexを見る #define forbit(i, mas) \ for (int forbitj = lbit(mas), forbitm = mas, i = log2(forbitj); forbitm; \ forbitm = forbitj ? forbitm ^ forbitj : 0, forbitj = lbit(forbitm), \ i = log2(forbitj)) char itoal(ll i) { return 'a' + i; } char itoaL(ll i) { return 'A' + i; } ll altoi(char c) { if ('A' <= c && c <= 'Z') return c - 'A'; return c - 'a'; } ll ctoi(char c) { return c - '0'; } char itoc(ll i) { return i + '0'; } ll vtoi(vi &v) { ll res = 0; if (sz(v) > 18) { debugline("vtoi"); deb(sz(v)); ole(); } rep(i, sz(v)) { res *= 10; res += v[i]; } return res; } vi itov(ll i) { vi res; while (i) { res.push_back(i % 10); i /= 10; } rev(res); return res; } vi stov(string &a) { ll n = sz(a); vi ret(n); rep(i, n) { ret[i] = a[i] - '0'; } return ret; } //基準を満たさないものは0になる vi stov(string &a, char one) { ll n = sz(a); vi ret(n); rep(i, n) ret[i] = a[i] == one; return ret; } vector<vector<ll>> ctoi(vector<vector<char>> s, char c) { ll n = sz(s), m = sz(s[0]); vector<vector<ll>> res(n, vector<ll>(m)); rep(i, n) rep(j, m) res[i][j] = s[i][j] == c; return res; } #define unique(v) v.erase(unique(v.begin(), v.end()), v.end()); //[i] := i番として圧縮されたものを返す vi compress(vi &a) { vi b; ll len = a.size(); for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { a[i] = lower_bound(all(b), a[i]) - b.begin(); } ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, umap<ll, ll> &map) { vi b; ll len = a.size(); for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { ll v = a[i]; a[i] = lower_bound(all(b), a[i]) - b.begin(); map[v] = a[i]; } ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, vi &r) { vi b; ll len = a.size(); fora(v, a) b.push_back(v); fora(v, r) b.push_back(v); sort(b); unique(b); for (ll i = 0; i < len; ++i) a[i] = lower_bound(all(b), a[i]) - b.begin(); for (ll i = 0; i < sz(r); ++i) r[i] = lower_bound(all(b), r[i]) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, vi &r, vi &s) { vi b; ll len = a.size(); fora(v, a) b.push_back(v); fora(v, r) b.push_back(v); fora(v, s) b.push_back(v); sort(b); unique(b); for (ll i = 0; i < len; ++i) a[i] = lower_bound(all(b), a[i]) - b.begin(); for (ll i = 0; i < sz(r); ++i) r[i] = lower_bound(all(b), r[i]) - b.begin(); for (ll i = 0; i < sz(s); ++i) r[i] = lower_bound(all(b), s[i]) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vector<vi> &a) { vi b; fora(vv, a) fora(v, vv) b.push_back(v); sort(b); unique(b); fora(vv, a) fora(v, vv) v = lower_bound(all(b), v) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vector<vector<vi>> &a) { vi b; fora(vvv, a) fora(vv, vvv) fora(v, vv) b.push_back(v); sort(b); unique(b); fora(vvv, a) fora(vv, vvv) fora(v, vv) v = lower_bound(all(b), v) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } void compress(ll a[], ll len) { vi b; for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { a[i] = lower_bound(all(b), a[i]) - b.begin(); } } //要素が見つからなかったときに困る #define binarySearch(a, v) (binary_search(all(a), v)) #define lowerIndex(a, v) (lower_bound(all(a), v) - a.begin()) #define lowerBound(a, v) (*lower_bound(all(a), v)) #define upperIndex(a, v) (upper_bound(all(a), v) - a.begin()) #define upperBound(a, v) (*upper_bound(all(a), v)) #define rlowerIndex(a, v) (upper_bound(all(a), v) - a.begin() - 1) #define rlowerBound(a, v) *(--(upper_bound(all(a), v))) #define rupperIndex(a, v) (lower_bound(all(a), v) - a.begin() - 1) #define rupperBound(a, v) *(--(lower_bound(all(a), v))) #define next2(a) next(next(a)) #define prev2(a) prev(prev(a)) //狭義の単調増加列 長さを返す template <class T> int lis(vector<T> &a) { int n = sz(a); vi tail(n + 1, MAX(T)); rep(i, n) { int id = lowerIndex(tail, a[i]); /**/ tail[id] = a[i]; } return lowerIndex(tail, MAX(T)); } template <class T> int lis_eq(vector<T> &a) { int n = sz(a); vi tail(n + 1, MAX(T)); rep(i, n) { int id = upperIndex(tail, a[i]); /**/ tail[id] = a[i]; } return lowerIndex(tail, MAX(T)); } // iteratorを返す // valueが1以上の物を返す 0は見つけ次第削除 // vを減らす場合 (*it).se--でいい template <class T, class U, class V> auto lower_map(map<T, U> &m, V k) { auto ret = m.lower_bound(k); while (ret != m.end() && (*ret).second == 0) { ret = m.erase(ret); } return ret; } template <class T, class U, class V> auto upper_map(map<T, U> &m, V k) { auto ret = m.upper_bound(k); while (ret != m.end() && (*ret).second == 0) { ret = m.erase(ret); } return ret; } //存在しなければエラー template <class T, class U, class V> auto rlower_map(map<T, U> &m, V k) { auto ret = upper_map(m, k); assert(ret != m.begin()); ret--; while (1) { if ((*ret).second != 0) break; assert(ret != m.begin()); auto next = ret; --next; m.erase(ret); ret = next; } return ret; } template <class T, class U, class V> auto rupper_map(map<T, U> &m, V k) { auto ret = lower_map(m, k); assert(ret != m.begin()); ret--; while (1) { if ((*ret).second != 0) break; assert(ret != m.begin()); auto next = ret; --next; m.erase(ret); ret = next; } return ret; } template <class T> void fin(T s) { cout << s << endl, exit(0); } //便利 数学 math ll mod(ll a, ll m) { return (a % m + m) % m; } ll pow(ll a) { return a * a; }; ll fact(ll v) { return v <= 1 ? 1 : v * fact(v - 1); } dou factd(int v) { static vd fact(2, 1); if (sz(fact) <= v) { rep(i, sz(fact), v + 1) { fact.push_back(fact.back() * i); } } return fact[v]; } ll comi(ll n, ll r) { assert(n < 100); static vvi(pas, 100, 100); if (pas[0][0]) return pas[n][r]; pas[0][0] = 1; rep(i, 1, 100) { pas[i][0] = 1; rep(j, 1, i + 1) pas[i][j] = pas[i - 1][j - 1] + pas[i - 1][j]; } return pas[n][r]; } double comd2(ll n, ll r) { static vvd(comb, 2020, 2020); if (comb[0][0] == 0) { comb[0][0] = 1; rep(i, 2000) { comb[i + 1][0] = 1; rep(j, 1, i + 2) { comb[i + 1][j] = comb[i][j] + comb[i][j - 1]; } } } return comb[n][r]; } double comd(int n, int r) { if (r < 0 || r > n) return 0; if (n < 2020) return comd2(n, r); static vd fact(2, 1); if (sz(fact) <= n) { rep(i, sz(fact), n + 1) { fact.push_back(fact.back() * i); } } return fact[n] / fact[n - r] / fact[r]; } ll gcd(ll a, ll b) { while (b) a %= b, swap(a, b); return abs(a); } ll gcd(vi b) { ll res = b[0]; rep(i, 1, sz(b)) res = gcd(b[i], res); return res; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll lcm(vi a) { ll res = a[0]; rep(i, 1, sz(a)) res = lcm(a[i], res); return res; } ll ceil(ll a, ll b) { if (b == 0) { debugline("ceil"); deb(a, b); ole(); return -1; } else if (a < 0) { return 0; } else { return (a + b - 1) / b; } } ll lower_remi__bx_a(ll kei, ll rem, ll x) { if (rem >= x) return 0; return (x - rem + kei - 1) / kei; } ll lower_remv__bx_a(ll kei, ll rem, ll x) { if (rem >= x) return rem; return (x - rem + kei - 1) / kei * kei + rem; } ll upper_remi__bx_a(ll kei, ll rem, ll x) { if (rem > x) return 0; return (x - rem + kei) / kei; } ll upper_remv__bx_a(ll kei, ll rem, ll x) { if (rem > x) return rem; return (x - rem + kei) / kei * kei + rem; } // v * v >= aとなる最小のvを返す 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) >> 1; } ll sig(ll s, ll t) { return ((s + t) * (t - s + 1)) >> 1; } //機能拡張 template <class T, class U> void operator+=(pair<T, U> &a, pair<T, U> &b) { a.fi += b.fi; a.se += b.se; } template <class T, class U> pair<T, U> operator+(pair<T, U> &a, pair<T, U> &b) { return pair<T, U>(a.fi + b.fi, a.se + b.se); } template <typename CharT, typename Traits, typename Alloc> basic_string<CharT, Traits, Alloc> operator+(const basic_string<CharT, Traits, Alloc> &lhs, const int rv) { basic_string<CharT, Traits, Alloc> str(lhs); str.append(to_string(rv)); return str; } template <typename CharT, typename Traits, typename Alloc> void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const int rv) { lhs += to_string(rv); } template <typename CharT, typename Traits, typename Alloc> basic_string<CharT, Traits, Alloc> operator+(const basic_string<CharT, Traits, Alloc> &lhs, const signed rv) { basic_string<CharT, Traits, Alloc> str(lhs); str.append(to_string(rv)); return str; } template <typename CharT, typename Traits, typename Alloc> void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const signed rv) { lhs += to_string(rv); } template <class T, class U> void operator+=(queue<T> &a, U v) { a.push(v); } template <class T, class U> void operator+=(deque<T> &a, U v) { a.push_back(v); } template <class T> priority_queue<T, vector<T>, greater<T>> & operator+=(priority_queue<T, vector<T>, greater<T>> &a, vector<T> &v) { fora(d, v) a.push(d); return a; } template <class T, class U> priority_queue<T, vector<T>, greater<T>> & operator+=(priority_queue<T, vector<T>, greater<T>> &a, U v) { a.push(v); return a; } template <class T, class U> priority_queue<T> &operator+=(priority_queue<T> &a, U v) { a.push(v); return a; } template <class T> set<T> &operator+=(set<T> &a, vector<T> v) { fora(d, v) a.insert(d); return a; } template <class T, class U> auto operator+=(set<T> &a, U v) { return a.insert(v); } template <class T, class U> auto operator-=(set<T> &a, U v) { return a.erase(v); } template <class T, class U> auto operator+=(mset<T> &a, U v) { return a.insert(v); } template <class T, class U> set<T, greater<T>> &operator+=(set<T, greater<T>> &a, U v) { a.insert(v); return a; } template <class T, class U> vector<T> &operator+=(vector<T> &a, U v) { a.push_back(v); return a; } template <class T, class U> vector<T> operator+(const vector<T> &a, U v) { vector<T> ret = a; ret += v; return ret; } template <class T, class U> vector<T> operator+(U v, const vector<T> &a) { vector<T> ret = a; ret.insert(ret.begin(), v); return ret; } template <class T> vector<T> operator+(vector<T> a, vector<T> b) { vector<T> ret; ret = a; fora(v, b) ret += v; return ret; } template <class T> vector<T> &operator+=(vector<T> &a, vector<T> &b) { fora(v, b) a += v; return a; } template <class T> vector<T> &operator-=(vector<T> &a, vector<T> &b) { if (sz(a) != sz(b)) { debugline("vector<T> operator-="); deb(a); deb(b); exit(0); } rep(i, sz(a)) a[i] -= b[i]; return a; } template <class T> vector<T> operator-(vector<T> &a, vector<T> &b) { if (sz(a) != sz(b)) { debugline("vector<T> operator-"); deb(a); deb(b); ole(); } vector<T> res(sz(a)); rep(i, sz(a)) res[i] = a[i] - b[i]; return res; } template <class T, class U> vector<T> operator*(vector<T> &a, U b) { vector<T> ret; fora(v, a) ret += v * b; return ret; } template <class T, class U> vector<T> operator/(vector<T> &a, U b) { vector<T> ret; fora(v, a) ret += v / b; return ret; } template <class T, class U> vector<T> operator*=(vector<T> &a, U b) { fora(v, a) v *= b; return a; } template <class T, class U> vector<T> operator/=(vector<T> &a, U b) { fora(v, a) v /= b; return a; } template <typename T> void erase(vector<T> &v, unsigned ll i) { v.erase(v.begin() + i); } template <typename T> void erase(vector<T> &v, unsigned ll s, unsigned ll e) { v.erase(v.begin() + s, v.begin() + e); } template <class T, class U> void erase(map<T, U> &m, ll okl, ll ngr) { m.erase(m.lower_bound(okl), m.lower_bound(ngr)); } template <class T> void erase(set<T> &m, ll okl, ll ngr) { m.erase(m.lower_bound(okl), m.lower_bound(ngr)); } template <typename T> void erasen(vector<T> &v, unsigned ll s, unsigned ll n) { v.erase(v.begin() + s, v.begin() + s + n); } template <typename T, typename U> void insert(vector<T> &v, unsigned ll i, U t) { v.insert(v.begin() + i, t); } template <typename T, typename U> void push_front(vector<T> &v, U t) { v.insert(v.begin(), t); } template <typename T, typename U> void insert(vector<T> &v, unsigned ll i, vector<T> list) { for (auto &&va : list) v.insert(v.begin() + i++, va); } template <typename T> void insert(set<T> &v, vector<T> list) { for (auto &&va : list) v.insert(va); } vector<string> split(const string a, const char deli) { string b = a + deli; ll l = 0, r = 0, n = b.size(); vector<string> res; rep(i, n) { if (b[i] == deli) { r = i; if (l < r) res.push_back(b.substr(l, r - l)); l = i + 1; } } return res; } vector<string> split(const string a, const string deli) { vector<string> res; ll kn = sz(deli); std::string::size_type Pos(a.find(deli)); ll l = 0; while (Pos != std::string::npos) { if (Pos - l) res.push_back(a.substr(l, Pos - l)); l = Pos + kn; Pos = a.find(deli, Pos + kn); } if (sz(a) - l) res.push_back(a.substr(l, sz(a) - l)); return res; } 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 <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)}; } //@formatter:off template <typename T> T minv(T a, T m); template <typename T> T minv(T a); template <typename T> class Modular { public: using Type = typename decay<decltype(T::value)>::type; constexpr Modular() : value() {} template <typename U> Modular(const U &x) { value = normalize(x); } template <typename U> static Type normalize(const U &x) { Type v; if (-mod() <= x && x < mod()) v = static_cast<Type>(x); else v = static_cast<Type>(x % mod()); if (v < 0) v += mod(); return v; } const Type &operator()() const { return value; } template <typename U> explicit operator U() const { return static_cast<U>(value); } constexpr static Type mod() { return T::value; } Modular &operator+=(const Modular &other) { if ((value += other.value) >= mod()) value -= mod(); return *this; } Modular &operator-=(const Modular &other) { if ((value -= other.value) < 0) value += mod(); return *this; } template <typename U> Modular &operator+=(const U &other) { return *this += Modular(other); } template <typename U> Modular &operator-=(const U &other) { return *this -= Modular(other); } Modular &operator++() { return *this += 1; } Modular &operator--() { return *this -= 1; } Modular operator++(signed) { Modular result(*this); *this += 1; return result; } Modular operator--(signed) { Modular result(*this); *this -= 1; return result; } Modular operator-() const { return Modular(-value); } template <typename U = T> typename enable_if<is_same<typename Modular<U>::Type, signed>::value, Modular>::type & operator*=(const Modular &rhs) { #ifdef _WIN32 uint64_t x = static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value); uint32_t xh = static_cast<uint32_t>(x >> 32), xl = static_cast<uint32_t>(x), d, m; asm("divl %4; \n\t" : "=a"(d), "=d"(m) : "d"(xh), "a"(xl), "r"(mod())); value = m; #else value = normalize(static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value)); #endif return *this; } template <typename U = T> typename enable_if<is_same<typename Modular<U>::Type, int64_t>::value, Modular>::type & operator*=(const Modular &rhs) { int64_t q = static_cast<int64_t>(static_cast<double>(value) * rhs.value / mod()); value = normalize(value * rhs.value - q * mod()); return *this; } template <typename U = T> typename enable_if<!is_integral<typename Modular<U>::Type>::value, Modular>::type & operator*=(const Modular &rhs) { value = normalize(value * rhs.value); return *this; } Modular &operator/=(const Modular &other) { return *this *= Modular(minv(other.value)); } template <typename U> friend bool operator==(const Modular<U> &lhs, const Modular<U> &rhs); template <typename U> friend bool operator<(const Modular<U> &lhs, const Modular<U> &rhs); template <typename U> friend std::istream &operator>>(std::istream &stream, Modular<U> &number); operator int() { return value; } private: Type value; }; template <typename T> bool operator==(const Modular<T> &lhs, const Modular<T> &rhs) { return lhs.value == rhs.value; } template <typename T, typename U> bool operator==(const Modular<T> &lhs, U rhs) { return lhs == Modular<T>(rhs); } template <typename T, typename U> bool operator==(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) == rhs; } template <typename T> bool operator!=(const Modular<T> &lhs, const Modular<T> &rhs) { return !(lhs == rhs); } template <typename T, typename U> bool operator!=(const Modular<T> &lhs, U rhs) { return !(lhs == rhs); } template <typename T, typename U> bool operator!=(U lhs, const Modular<T> &rhs) { return !(lhs == rhs); } template <typename T> bool operator<(const Modular<T> &lhs, const Modular<T> &rhs) { return lhs.value < rhs.value; } template <typename T> Modular<T> operator+(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) += rhs; } template <typename T, typename U> Modular<T> operator+(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) += rhs; } template <typename T, typename U> Modular<T> operator+(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) += rhs; } template <typename T> Modular<T> operator-(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) -= rhs; } template <typename T, typename U> Modular<T> operator-(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) -= rhs; } template <typename T, typename U> Modular<T> operator-(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) -= rhs; } template <typename T> Modular<T> operator*(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) *= rhs; } template <typename T, typename U> Modular<T> operator*(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) *= rhs; } template <typename T, typename U> Modular<T> operator*(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) *= rhs; } template <typename T> Modular<T> operator/(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) /= rhs; } template <typename T, typename U> Modular<T> operator/(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) /= rhs; } template <typename T, typename U> Modular<T> operator/(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) /= rhs; } constexpr signed MOD = // 998244353; 1e9 + 7; // MOD using mint = Modular<std::integral_constant<decay<decltype(MOD)>::type, MOD>>; constexpr int mint_len = 1400001; vi fac, finv, inv; vi p2; mint com(int n, int r) { if (r < 0 || r > n) return 0; return mint(finv[r] * fac[n] % MOD * finv[n - r]); } mint pom(int n, int r) { /* if (!sz(fac)) com(0, -1);*/ if (r < 0 || r > n) return 0; return mint(fac[n] * finv[n - 1]); } mint npr(int n, int r) { /* if (!sz(fac)) com(0, -1);*/ if (r < 0 || r > n) return 0; return mint(fac[n] * finv[n - r]); } int nprin(int n, int r) { /* if (!sz(fac)) com(0, -1);*/ if (r < 0 || r > n) return 0; return fac[n] * finv[n - r] % MOD; } int icom(int n, int r) { const int NUM_ = 1400001; static ll fac[NUM_ + 1], finv[NUM_ + 1], inv[NUM_ + 1]; if (fac[0] == 0) { inv[1] = fac[0] = finv[0] = 1; for (int i = 2; i <= NUM_; ++i) inv[i] = inv[MOD % i] * (MOD - MOD / i) % MOD; for (int i = 1; i <= NUM_; ++i) fac[i] = fac[i - 1] * i % MOD, finv[i] = finv[i - 1] * inv[i] % MOD; } if (r < 0 || r > n) return 0; return ((finv[r] * fac[n] % MOD) * finv[n - r]) % MOD; } #define ncr com #define ncri icom // n個の場所にr個の物を置く mint nhr(int n, int r) { return com(n + r - 1, r); } mint hom(int n, int r) { return com(n + r - 1, r); } int nhri(int n, int r) { return icom(n + r - 1, r); } template <typename T> T minv(T a, T m) { T u = 0, v = 1; while (a != 0) { T t = m / a; m -= t * a; swap(a, m); u -= t * v; swap(u, v); } assert(m == 1); return u; } template <typename T> T minv(T a) { if (a < mint_len) return inv[a]; T u = 0, v = 1; T m = MOD; while (a != 0) { T t = m / a; m -= t * a; swap(a, m); u -= t * v; swap(u, v); } assert(m == 1); return u; } template <typename T, typename U> Modular<T> mpow(const Modular<T> &a, const U &b) { assert(b >= 0); int x = a(), res = 1; U p = b; while (p > 0) { if (p & 1) (res *= x) %= MOD; (x *= x) %= MOD; p >>= 1; } return res; } template <typename T, typename U, typename V> mint mpow(const T a, const U b, const V m = MOD) { assert(b >= 0); int x = a, res = 1; U p = b; while (p > 0) { if (p & 1) (res *= x) %= m; (x *= x) %= m; p >>= 1; } return res; } template <typename T, typename U> mint mpow(const T a, const U b) { assert(b >= 0); int x = a, res = 1; U p = b; while (p > 0) { if (p & 1) (res *= x) %= MOD; (x *= x) %= MOD; p >>= 1; } return res; } template <typename T, typename U, typename V> int mpowi(const T &a, const U &b, const V &m = MOD) { assert(b >= 0); int x = a, res = 1; U p = b; while (p > 0) { if (p & 1) (res *= x) %= m; (x *= x) %= m; p >>= 1; } return res; } template <typename T> string to_string(const Modular<T> &number) { return to_string(number()); } string yuri(const mint &a) { stringstream st; rep(i, 300) { rep(j, 300) { if ((mint)i / j == a) { st << i << " / " << j; return st.str(); } } } rep(i, 1000) { rep(j, 1000) { if ((mint)i / j == a) { st << i << " / " << j; return st.str(); } } } return st.str(); } template <typename T> std::ostream &operator<<(std::ostream &stream, const Modular<T> &number) { stream << number(); #ifdef _DEBUG // stream << " -> " << yuri(number); #endif return stream; } //@formatter:off template <typename T> std::istream &operator>>(std::istream &stream, Modular<T> &number) { typename common_type<typename Modular<T>::Type, int64_t>::type x; stream >> x; number.value = Modular<T>::normalize(x); return stream; } using PM = pair<mint, mint>; using vm = vector<mint>; using mapm = map<int, mint>; using umapm = umap<int, mint>; #define vvm(...) \ o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(mint, __VA_ARGS__) #define vnm(name, ...) auto name = make_v<mint>(__VA_ARGS__) //@formatter:on // nhr n個の場所にr個の物を分ける //@起動時 struct initon { initon() { cin.tie(0); ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(16); srand((unsigned)clock() + (unsigned)time(NULL)); }; } initonv; //@formatter:on // gra mll pr //上下左右 const string udlr = "udlr"; string UDLR = "UDLR"; // x4と連動 UDLR.find('U') := x4[0] //右、上が正 constexpr ll y4[] = {1, -1, 0, 0}; constexpr ll x4[] = {0, 0, -1, 1}; constexpr ll y8[] = {0, 1, 0, -1, -1, 1, 1, -1}; constexpr ll x8[] = {1, 0, -1, 0, 1, -1, 1, -1}; ll n, m, k, K, d, H, W, x, y, z, q, Q; ll cou; vi a, b, c; // vvi (s, 0, 0); vvc(ba, 0, 0); vp p; str s; //一番高速 // hashを共有しない class roll_fast_only { static constexpr uint64_t base = 5; //他のロリハとhashを共有したい場合 // const uint64_t base = rand((uint64_t) 2, mask61 - // 1);//共有したくない場合 static constexpr uint64_t mask30 = ((uint64_t)1 << 30) - 1; static constexpr uint64_t mask31 = ((uint64_t)1 << 31) - 1; static constexpr uint64_t mask61 = ((uint64_t)1 << 61) - 1; static constexpr uint64_t offset = mask61 * 7; static constexpr uint64_t mod(const uint64_t x) { const uint64_t y = (x >> 61) + (x & mask61); return y >= mask61 ? y - mask61 : y; } static constexpr uint64_t mul(const uint64_t x, const uint64_t y) { const uint64_t xh = x >> 31, xl = x & mask31; const uint64_t yh = y >> 31, yl = y & mask31; const uint64_t z = xl * yh + xh * yl; const uint64_t zh = z >> 30, zl = z & mask30; return xh * yh * 2 + zh + (zl << 31) + xl * yl; } const std::size_t siz; vector<uint64_t> pow, h; public: template <typename T> roll_fast_only(T &A) : siz(sz(A)), pow(siz + 1, 1), h(siz + 1, 0) { for (std::size_t i = 1; i <= siz; i++) { pow[i] = mod(mul(pow[i - 1], base)), h[i] = mod(mul(h[i - 1], base) + A[i - 1]); } } uint64_t operator()(const std::size_t l, const std::size_t r) { return mod(h[r] + offset - mul(h[l], pow[r - l])); } uint64_t operator()(int r = inf) { return operator()(0, min(siz, r)); } inline int lcp_dup(int a, int b) { int len = min((int)siz - a, (int)siz - b); int low = 0, high = len; while (high - low > 1) { int mid = (low + high) >> 1; if (operator()(a, a + mid) != operator()(b, b + mid)) high = mid; else low = mid; } return low; } inline int lcp_not(int a, int b) { return min(lcp_dup(a, b), b - a); } //@formatter:on }; //動かない場合roll_fast_only2 // roll_fast_only ro(list); // lcpならsa := saffix_arrayでO(1) 構成O(N log(n)^2) void solve() { in(n); in(s); roll_fast_only ro(s); rer(len, n - 1, 1) { // umapu m; unordered_map<uint64_t, int> m; rep(i, n - len + 1) { auto has = ro(i, i + len); if (m.find(has) != m.end()) { if (m[has] <= i) fin(len); } else m[has] = i + len; } } fin(0); } auto my(ll n, vi &a) { return 0; } auto sister(ll n, vi &a) { ll ret = 0; return ret; } signed main() { solve(); #define arg n, a #ifdef _DEBUG bool bad = 0; for (ll i = 0, ok = 1; i < k5 && ok; ++i) { ll n = rand(1, 8); vi a = ranv(n, 1, 10); auto myres = my(arg); auto res = sister(arg); ok = myres == res; if (!ok) { out(arg); cerr << "AC : " << res << endl; cerr << "MY : " << myres << endl; bad = 1; break; } } if (!bad) { // cout << "完璧 : solveを書き直そう" << endl; // cout << " : そして、solve()を呼び出すのだ" << endl; // cout << " : cin>>n; na(a,n);も忘れるな" << endl; } #endif return 0; };
//#pragma GCC optimize("Ofast") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace std::chrono; #define int long long #define ll long long auto start_time = system_clock::now(); //@formatter:off #ifdef _DEBUG //区間削除は出来ない template <class T> struct my_pbds_tree { set<T> s; auto begin() { return s.begin(); } auto end() { return s.end(); } auto rbegin() { return s.rbegin(); } auto rend() { return s.rend(); } auto empty() { return s.empty(); } auto size() { return s.size(); } void clear() { s.clear(); } template <class U> void insert(U v) { s.insert(v); } template <class U> void operator+=(U v) { insert(v); } template <class F> auto erase(F v) { return s.erase(v); } template <class U> auto find(U v) { return s.find(v); } template <class U> auto lower_bound(U v) { return s.lower_bound(v); } template <class U> auto upper_bound(U v) { return s.upper_bound(v); } auto find_by_order(ll k) { auto it = s.begin(); for (ll i = 0; i < k; i++) it++; return it; } auto order_of_key(ll v) { auto it = s.begin(); ll i = 0; for (; it != s.end() && *it < v; i++) it++; return i; } }; #define pbds(T) my_pbds_tree<T> // gp_hash_tableでcountを使えないようにするため template <class T, class U> struct my_unordered_map { unordered_map<T, U> m; my_unordered_map(){}; auto begin() { return m.begin(); } auto end() { return m.end(); } auto cbegin() { return m.cbegin(); } auto cend() { return m.cend(); } template <class V> auto erase(V v) { return m.erase(v); } void clear() { m.clear(); } /*countは gp_hash_tableに存在しない*/ /*!= m.end()*/ template <class V> auto find(V v) { return m.find(v); } template <class V> auto &operator[](V n) { return m[n]; } }; #define unordered_map my_unordered_map #define umapi unordered_map<ll, ll> #define umapu unordered_map<uint64_t, ll> #define umapp unordered_map<P, ll> #else #define unordered_map __gnu_pbds::gp_hash_table // find_by_order(k) k番目のイテレーター // order_of_key(k) k以上が前から何番目か #define pbds(U) \ __gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag, \ __gnu_pbds::tree_order_statistics_node_update> #define umapi unordered_map<ll, ll, xorshift> #define umapp unordered_map<P, ll, xorshift> #define umapu unordered_map<uint64_t, ll, xorshift> #endif struct xorshift { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } size_t operator()(std::pair<ll, ll> x) const { ll v = ((x.first) << 32) | x.second; static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(v + FIXED_RANDOM); } }; template <class U, class L> void operator+=( __gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update> &s, L v) { s.insert(v); } //衝突対策 #define ws wszzzz template <class A, class B, class C> struct T2 { A f; B s; C t; T2() { f = 0, s = 0, t = 0; } T2(A f, B s, C t) : f(f), s(s), t(t) {} bool operator<(const T2 &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 ?n s > r.s : t > r.t; 大きい順 */ } bool operator>(const T2 &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 T2 &r) const { return f == r.f && s == r.s && t == r.t; } bool operator!=(const T2 &r) const { return f != r.f || s != r.s || t != r.t; } }; template <class A, class B, class C, class D> struct F2 { A a; B b; C c; D d; F2() { a = 0, b = 0, c = 0, d = 0; } F2(A a, B b, C c, D d) : a(a), b(b), c(c), d(d) {} bool operator<(const F2 &r) const { return a != r.a ? a < r.a : b != r.b ? b < r.b : c != r.c ? c < r.c : d < r.d; /* return a != r.a ? a > r.a : b != r.b ? b > r.b : c != r.c ? c > r.c : d > r.d;*/ } bool operator>(const F2 &r) const { return a != r.a ? a > r.a : b != r.b ? b > r.b : c != r.c ? c > r.c : d > r.d; /* return a != r.a ? a < r.a : b != r.b ? b < r.b : c != r.c ? c < r.c : d < r.d;*/ } bool operator==(const F2 &r) const { return a == r.a && b == r.b && c == r.c && d == r.d; } bool operator!=(const F2 &r) const { return a != r.a || b != r.b || c != r.c || d != r.d; } ll operator[](ll i) { assert(i < 4); return i == 0 ? a : i == 1 ? b : i == 2 ? c : d; } }; typedef T2<ll, ll, ll> T; typedef F2<ll, ll, ll, ll> F; T mt(ll a, ll b, ll c) { return T(a, b, c); } //@マクロ省略系 型,構造 #define double long double #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 beg begin #define rbeg rbegin #define con continue #define bre break #define brk break #define is == #define el else #define elf else if #define wh while #define upd update #define maxq 1 #define minq -1 #define ZERO(a) memset(a, 0, sizeof(a)) #define MINUS(a) memset(a, 0xff, sizeof(a)) #define MALLOC(type, len) (type *)malloc((len) * sizeof(type)) #define lam(right) [&](ll &p) { return p right; } //マクロ省略系 コンテナ 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 PQ priority_queue<ll, vector<ll>, greater<ll>> #define tos to_string using mapi = map<ll, ll>; using mapp = map<P, ll>; using mapd = map<dou, ll>; using mapc = map<char, ll>; using maps = map<str, ll>; using seti = set<ll>; using setd = set<dou>; using setc = set<char>; using sets = set<str>; using qui = queue<ll>; #define bset bitset #define uset unordered_set #define useti unordered_set<ll, ll, xorshift> #define mset multiset #define mseti multiset<ll> #define umap unordered_map #define mmap multimap template <class T> struct pq { priority_queue<T, vector<T>, greater<T>> q; /*小さい順*/ T su = 0; void clear() { q = priority_queue<T, vector<T>, greater<T>>(); su = 0; } void operator+=(T v) { su += v; q.push(v); } T sum() { return su; } T top() { return q.top(); } void pop() { su -= q.top(); q.pop(); } T poll() { T ret = q.top(); su -= ret; q.pop(); return ret; } ll size() { return q.size(); } }; template <class T> struct pqg { priority_queue<T> q; /*大きい順*/ T su = 0; void clear() { q = priority_queue<T>(); su = 0; } void operator+=(T v) { su += v; q.push(v); } T sum() { return su; } T top() { return q.top(); } void pop() { su -= q.top(); q.pop(); } T poll() { T ret = q.top(); su -= ret; q.pop(); return ret; } ll size() { return q.size(); } }; #define pqi pq<ll> #define pqgi pqg<ll> //マクロ 繰り返し #define o_rep(o1, o2, o3, o4, name, ...) name #define rep1(n) for (ll rep1i = 0, rep1lim = n; rep1i < rep1lim; ++rep1i) #define rep2(i, n) for (ll i = 0, rep2lim = n; i < rep2lim; ++i) #define rep3(i, m, n) for (ll i = m, rep3lim = n; i < rep3lim; ++i) #define rep4(i, m, n, ad) for (ll i = m, rep4lim = n; i < rep4lim; i += ad) #define rep(...) o_rep(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__) #define rer2(i, n) for (ll i = n; i >= 0; i--) #define rer3(i, m, n) for (ll i = m, rer3lim = n; i >= rer3lim; i--) #define rer4(i, m, n, dec) for (ll i = m, rer4lim = n; i >= rer4lim; i -= dec) #define rer(...) o_rep(__VA_ARGS__, rer4, rer3, rer2, )(__VA_ARGS__) #define reps2(i, j, n) \ for (ll i = 0, reps2lim = n; i < reps2lim; ++i) \ for (ll j = 0; j < reps2lim; ++j) #define reps3(i, j, k, n) \ for (ll i = 0, reps3lim = n; i < reps3lim; ++i) \ for (ll j = 0; j < reps3lim; ++j) \ for (ll k = 0; k < reps3lim; ++k) #define reps4(i, j, k, l, n) \ for (ll i = 0, reps4lim = n; i < reps4lim; ++i) \ for (ll j = 0; j < reps4lim; ++j) \ for (ll k = 0; k < reps4lim; ++k) \ for (ll l = 0; l < reps4lim; ++l) #define o_reps(o1, o2, o3, o4, o5, name, ...) name #define reps(...) o_reps(__VA_ARGS__, reps4, reps3, reps2, rep2, )(__VA_ARGS__) #define repss(i, j, k, a, b, c) \ for (ll i = 0; i < a; ++i) \ for (ll j = 0; j < b; ++j) \ for (ll k = 0; k < c; ++k) #define fora(a, b) for (auto &&a : b) //インデックスを前後含めて走査 #define fori(i, s, len) \ for (int i = s, prev = (s == 0) ? len - 1 : s - 1, \ next = (s == len - 1) ? 0 : s + 1, cou = 0; \ cou < len; \ cou++, prev = i, i = next, next = (next == len - 1) ? 0 : next + 1) #define form(st, l, r) \ for (auto &&it = st.lower_bound(l); it != st.end() && (*it).fi < r; ++it) #define forit(st, l, r) \ for (auto &&it = st.lower_bound(l); it != st.end() && (*it) < r;) //マクロ 定数 #define k3 1010 #define k4 10101 #define k5 101010 #define k6 1010101 #define k7 10101010 const ll inf = (ll)1e9 + 100; const ll linf = (ll)1e18 + 100; const char infc = '{'; const string infs = "{"; const double eps = 1e-9; const double PI = 3.1415926535897932384626433832795029L; //マクロ省略形 関数等 #define arsz(a) (sizeof(a) / sizeof(a[0])) #define sz(a) ((ll)(a).size()) #define mp make_pair #define pb pop_back #define pf push_front #define eb emplace_back #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() constexpr bool ev(ll a) { return !(a & 1); } constexpr bool od(ll a) { return (a & 1); } //@拡張系 こう出来るべきというもの //埋め込み 存在を意識せずに機能を増やされているもの //@formatter:on namespace std { template <> class hash<std::pair<signed, signed>> { public: size_t operator()(const std::pair<signed, signed> &x) const { return hash<ll>()(((ll)x.first << 32) | x.second); } }; template <> class hash<std::pair<ll, ll>> { public : /*大きいllが渡されると、<<32でオーバーフローするがとりあえず問題ないと判断*/ size_t operator()(const std::pair<ll, ll> &x) const { return hash<ll>()(((ll)x.first << 32) | x.second); } }; } // namespace std //@formatter:off // stream まとめ istream &operator>>(istream &iss, P &a) { iss >> a.first >> a.second; return iss; } template <typename T> istream &operator>>(istream &iss, vector<T> &vec) { for (T &x : vec) iss >> x; return iss; } template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> p) { os << p.fi << " " << p.se << endl; return os; } ostream &operator<<(ostream &os, T p) { os << p.f << " " << p.s << " " << p.t; return os; } ostream &operator<<(ostream &os, F p) { os << p.a << " " << p.b << " " << p.c << " " << p.d; return os; } template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) { for (ll i = 0; i < vec.size(); ++i) os << vec[i] << (i + 1 == vec.size() ? "" : " "); return os; } template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) { for (ll i = 0; i < vec.size(); ++i) { for (ll j = 0; j < vec[i].size(); ++j) { os << vec[i][j] << " "; } os << endl; } return os; } template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &m) { for (auto &&v : m) os << v; return os; } template <class T> ostream &operator<<(ostream &os, set<T> s) { fora(v, s) { os << v << " "; } return os; } template <class T> ostream &operator<<(ostream &os, deque<T> a) { fora(v, a) os << v << " "; return os; } 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); }); } #define all_of(a, right) all_of2(a, lam(right)) #define any_of(a, right) any_of2(a, lam(right)) #define none_of(a, right) none_of2(a, lam(right)) #define find_if(a, right) find_if2(a, lam(right)) #define rfind_if(a, right) rfind_if2(a, lam(right)) #define contains_if(a, right) contains_if2(a, lam(right)) #define count_if(a, right) count_if2(a, lam(right)) #define for_each(a, right) \ do { \ fora(v, a) { v right; } \ } while (0) template <class T, class U> void replace(vector<T> &a, T key, U v) { replace(a.begin(), a.end(), key, v); } void replace(str &a, char key, str v) { if (v == "") a.erase(remove(all(a), key), a.end()); } void replace(str &a, char key, char v) { replace(all(a), key, v); } // keyと同じかどうか01で置き換える template <class T, class U> void replace(vector<T> &a, U k) { rep(i, sz(a)) a[i] = a[i] == k; } template <class T, class U> void replace(vector<vector<T>> &a, U k) { rep(i, sz(a)) rep(j, sz(a[0])) a[i][j] = a[i][j] == k; } template <class T> void replace(T &a) { replace(a, '#'); } void replace(str &a, str key, str v) { stringstream t; ll kn = sz(key); std::string::size_type Pos(a.find(key)); ll l = 0; while (Pos != std::string::npos) { t << a.substr(l, Pos - l); t << v; l = Pos + kn; Pos = a.find(key, Pos + kn); } t << a.substr(l, sz(a) - l); a = t.str(); } template <class T> bool includes(vector<T> &a, vector<T> &b) { vi c = a; vi d = b; sort(all(c)); sort(all(d)); return includes(all(c), all(d)); } template <class T> bool is_permutation(vector<T> &a, vector<T> &b) { return is_permutation(all(a), all(b)); } template <class T> bool next_permutation(vector<T> &a) { return next_permutation(all(a)); } void iota(vector<ll> &ve, ll s, ll n) { ve.resize(n); iota(all(ve), s); } vi iota(ll s, ll len) { vi ve(len); iota(all(ve), s); return ve; } template <class A, class B> auto vtop(vector<A> &a, vector<B> &b) { assert(sz(a) == sz(b)); /*stringを0で初期化できない */ vector<pair<A, B>> res; rep(i, sz(a)) res.eb(a[i], b[i]); return res; } template <class A, class B> void ptov(vector<pair<A, B>> &p, vector<A> &a, vector<B> &b) { a.resize(sz(p)), b.resize(sz(p)); rep(i, sz(p)) a[i] = p[i].fi, b[i] = p[i].se; } template <class A, class B, class C> auto vtot(vector<A> &a, vector<B> &b, vector<C> &c) { assert(sz(a) == sz(b) && sz(b) == sz(c)); vector<T2<A, B, C>> res; rep(i, sz(a)) res.eb(a[i], b[i], c[i]); return res; } template <class A, class B, class C, class D> auto vtof(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { assert(sz(a) == sz(b) && sz(b) == sz(c) && sz(c) == sz(d)); vector<F2<A, B, C, D>> res; rep(i, sz(a)) res.eb(a[i], b[i], c[i], d[i]); return res; } enum pcomparator { fisi, fisd, fdsi, fdsd, sifi, sifd, sdfi, sdfd }; enum tcomparator { fisiti, fisitd, fisdti, fisdtd, fdsiti, fdsitd, fdsdti, fdsdtd, fitisi, fitisd, fitdsi, fitdsd, fdtisi, fdtisd, fdtdsi, fdtdsd, sifiti, sifitd, sifdti, sifdtd, sdfiti, sdfitd, sdfdti, sdfdtd, sitifi, sitifd, sitdfi, sitdfd, sdtifi, sdtifd, sdtdfi, sdfdfd, tifisi, tifisd, tifdsi, tifdsd, tdfisi, tdfisd, tdfdsi, tdfdsd, tisifi, tisifd, tisdfi, tisdfd, tdsifi, tdsifd, tdsdfi, tdsdfd }; template <class A, class B> void sort(vector<pair<A, B>> &a, pcomparator type) { typedef pair<A, B> U; if (type == fisi) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se < r.se; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se > r.se; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se < r.se; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se > r.se; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi < r.fi; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi > r.fi; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi < r.fi; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi > r.fi; }); }; template <class U> void sort(vector<U> &a, pcomparator type) { if (type == fisi) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f > r.f; }); }; template <class A, class B, class C, class D> void sort(vector<F2<A, B, C, D>> &a, pcomparator type) { typedef F2<A, B, C, D> U; if (type == fisi) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a > r.a; }); }; template <class U> void sort(vector<U> &a, tcomparator type) { if (type == 0) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t < r.t; }); else if (type == 1) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t > r.t; }); else if (type == 2) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t < r.t; }); else if (type == 3) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t > r.t; }); else if (type == 4) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t < r.t; }); else if (type == 5) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t > r.t; }); else if (type == 6) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t < r.t; }); else if (type == 7) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t > r.t; }); else if (type == 8) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s < r.s; }); else if (type == 9) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s > r.s; }); else if (type == 10) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s < r.s; }); else if (type == 11) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s > r.s; }); else if (type == 12) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s < r.s; }); else if (type == 13) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s > r.s; }); else if (type == 14) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s < r.s; }); else if (type == 15) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s > r.s; }); else if (type == 16) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t < r.t; }); else if (type == 17) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t > r.t; }); else if (type == 18) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t < r.t; }); else if (type == 19) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t > r.t; }); else if (type == 20) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t < r.t; }); else if (type == 21) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t > r.t; }); else if (type == 22) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t < r.t; }); else if (type == 23) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t > r.t; }); else if (type == 24) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f < r.f; }); else if (type == 25) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f > r.f; }); else if (type == 26) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f < r.f; }); else if (type == 27) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f > r.f; }); else if (type == 28) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f < r.f; }); else if (type == 29) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f > r.f; }); else if (type == 30) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f < r.f; }); else if (type == 31) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f > r.f; }); else if (type == 32) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == 33) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == 34) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == 35) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == 36) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == 37) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == 38) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == 39) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == 40) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == 41) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == 42) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == 43) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f > r.f; }); else if (type == 44) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == 45) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == 46) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == 47) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f > r.f; }); } template <class A, class B, class C, class D> void sort(vector<F2<A, B, C, D>> &a, tcomparator type) { typedef F2<A, B, C, D> U; if (type == 0) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c < r.c; }); else if (type == 1) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c > r.c; }); else if (type == 2) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c < r.c; }); else if (type == 3) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c > r.c; }); else if (type == 4) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c < r.c; }); else if (type == 5) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c > r.c; }); else if (type == 6) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c < r.c; }); else if (type == 7) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c > r.c; }); else if (type == 8) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b < r.b; }); else if (type == 9) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b > r.b; }); else if (type == 10) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b < r.b; }); else if (type == 11) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b > r.b; }); else if (type == 12) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b < r.b; }); else if (type == 13) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b > r.b; }); else if (type == 14) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b < r.b; }); else if (type == 15) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b > r.b; }); else if (type == 16) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c < r.c; }); else if (type == 17) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c > r.c; }); else if (type == 18) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c < r.c; }); else if (type == 19) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c > r.c; }); else if (type == 20) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c < r.c; }); else if (type == 21) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c > r.c; }); else if (type == 22) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c < r.c; }); else if (type == 23) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c > r.c; }); else if (type == 24) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a < r.a; }); else if (type == 25) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a > r.a; }); else if (type == 26) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a < r.a; }); else if (type == 27) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a > r.a; }); else if (type == 28) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a < r.a; }); else if (type == 29) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a > r.a; }); else if (type == 30) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a < r.a; }); else if (type == 31) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a > r.a; }); else if (type == 32) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == 33) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == 34) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == 35) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == 36) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == 37) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == 38) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == 39) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == 40) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == 41) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == 42) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == 43) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a > r.a; }); else if (type == 44) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == 45) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == 46) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == 47) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a > r.a; }); } void sort(string &a) { sort(all(a)); } template <class T> void sort(vector<T> &a) { sort(all(a)); } // P l, P rで f(P) の形で渡す template <class U, class F> void sort(vector<U> &a, F f) { sort(all(a), [&](U l, U r) { return f(l) < f(r); }); }; template <class T> void rsort(vector<T> &a) { sort(all(a), greater<T>()); }; template <class U, class F> void rsort(vector<U> &a, F f) { sort(all(a), [&](U l, U r) { return f(l) > f(r); }); }; // F = T<T> //例えばreturn p.fi + p.se; template <class A, class B> void sortp(vector<A> &a, vector<B> &b) { auto c = vtop(a, b); sort(c); rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se; } template <class A, class B, class F> void sortp(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); sort(c, f); rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se; } template <class A, class B> void rsortp(vector<A> &a, vector<B> &b) { auto c = vtop(a, b); rsort(c); rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second; } template <class A, class B, class F> void rsortp(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); rsort(c, f); rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second; } template <class A, class B, class C> void sortt(vector<A> &a, vector<B> &b, vector<C> &c) { auto d = vtot(a, b, c); sort(d); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class F> void sortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); sort(d, f); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C> void rsortt(vector<A> &a, vector<B> &b, vector<C> &c) { auto d = vtot(a, b, c); rsort(d); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class F> void rsortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); rsort(d, f); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class D> void sortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { auto e = vtof(a, b, c, d); sort(e); rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d; } template <class A, class B, class C, class D> void rsortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { auto e = vtof(a, b, c, d); rsort(e); rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d; } // sortindex 元のvectorはソートしない template <class T> vi sorti(vector<T> &a) { auto b = a; vi ind = iota(0, sz(a)); sortp(b, ind); return ind; } /*indexの分で型が変わるためpcomparatorが必要*/ template <class T> vi sorti(vector<T> &a, pcomparator f) { auto b = a; vi ind = iota(0, sz(a)); sortp(b, ind, f); return ind; } template <class T, class F> vi sorti(vector<T> &a, F f) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(a[x]) < f(a[y]); }); return ind; } template <class T> vi rsorti(vector<T> &a) { auto b = a; vi ind = iota(0, sz(a)); rsortp(b, ind); return ind; } template <class T, class F> vi rsorti(vector<T> &a, F f) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(a[x]) > f(a[y]); }); return ind; } template <class A, class B, class F> vi sortpi(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(c[x]) < f(c[y]); }); return ind; } template <class A, class B> vi sortpi(vector<A> &a, vector<B> &b, pcomparator f) { vi ind = iota(0, sz(a)); auto c = a; auto d = b; sortt(c, d, ind, f); return ind; } template <class A, class B> vi sortpi(vector<A> &a, vector<B> &b) { return sortpi(a, b, fisi); }; template <class A, class B, class F> vi rsortpi(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(c[x]) > f(c[y]); }); return ind; } template <class A, class B> vi rsortpi(vector<A> &a, vector<B> &b) { return sortpi(a, b, fdsd); }; template <class A, class B, class C, class F> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(d[x]) < f(d[y]); }); return ind; } template <class A, class B, class C> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, pcomparator f) { vi ind = iota(0, sz(a)); auto d = vtof(a, b, c, ind); sort(d, f); rep(i, sz(a)) ind[i] = d[i].d; return ind; } template <class A, class B, class C> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { if (a[x] == a[y]) { if (b[x] == b[y]) return c[x] < c[y]; else return b[x] < b[y]; } else { return a[x] < a[y]; } }); return ind; } template <class A, class B, class C, class F> vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(d[x]) > f(d[y]); }); return ind; } template <class A, class B, class C> vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { if (a[x] == a[y]) { if (b[x] == b[y]) return c[x] > c[y]; else return b[x] > b[y]; } else { return a[x] > a[y]; } }); return ind; } template <class T> void sort2(vector<vector<T>> &a) { for (ll i = 0, n = a.size(); i < n; ++i) sort(a[i]); } template <class T> void rsort2(vector<vector<T>> &a) { for (ll 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 W, typename T> void fill(W &xx, const T vall) { xx = vall; } template <typename W, typename T> void fill(vector<W> &vecc, const T vall) { for (auto &&vx : vecc) fill(vx, vall); } template <typename W, typename T> void fill(vector<W> &xx, const T v, ll len) { rep(i, len) xx[i] = v; } template <typename W, typename T> void fill(vector<vector<W>> &xx, const T v, ll lh, ll lw) { rep(i, lh) rep(j, lw) xx[i][j] = v; } template <class T, class U> void fill(vector<T> &a, U val, vi &ind) { fora(v, ind) a[v] = val; } template <typename A, size_t N> A sum(A (&a)[N]) { A res = 0; rep(i, N) res += a[i]; return res; } template <typename A, size_t N, size_t O> A sum(A (&a)[N][O]) { A res = 0; rep(i, N) rep(j, O) res += a[i][j]; return res; } template <typename A, size_t N, size_t O, size_t P> A sum(A (&a)[N][O][P]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) res += a[i][j][k]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A sum(A (&a)[N][O][P][Q]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) res += a[i][j][k][l]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A sum(A (&a)[N][O][P][Q][R]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) res += a[i][j][k][l][m]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A sum(A (&a)[N][O][P][Q][R][S]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) rep(n, S) res += a[i][j][k][l][m][n]; return res; } //@汎用便利関数 入力 ll in() { ll ret; cin >> ret; return ret; } string sin() { string ret; cin >> ret; return ret; } template <class T> void in(T &head) { cin >> head; } template <class T, class... U> void in(T &head, U &...tail) { cin >> head; in(tail...); } #define o_din(o1, o2, o3, o4, o5, o6, name, ...) name #define din1(a) \ ll a; \ cin >> a #define din2(a, b) \ ll a, b; \ cin >> a >> b #define din3(a, b, c) \ ll a, b, c; \ cin >> a >> b >> c #define din4(a, b, c, d) \ ll a, b, c, d; \ cin >> a >> b >> c >> d #define din5(a, b, c, d, e) \ ll a, b, c, d, e; \ cin >> a >> b >> c >> d >> e #define din6(a, b, c, d, e, f) \ ll a, b, c, d, e, f; \ cin >> a >> b >> c >> d >> e >> f #define din(...) \ o_din(__VA_ARGS__, din6, din5, din4, din3, din2, din1)(__VA_ARGS__) #define o_dind(o1, o2, o3, o4, name, ...) name #define din1d(a) \ din1(a); \ a-- #define din2d(a, b) \ din2(a, b); \ a--, b-- #define din3d(a, b, c) \ din3(a, b, c); \ a--, b--, c-- #define din4d(a, b, c, d) \ din4(a, b, c, d); \ a--, b--, c--, d-- #define dind(...) o_dind(__VA_ARGS__, din4d, din3d, din2d, din1d)(__VA_ARGS__) template <class T> void out2(T &&head) { cout << head; } template <class T, class... U> void out2(T &&head, U &&...tail) { cout << head << " "; out2(tail...); } template <class T, class... U> void out(T &&head, U &&...tail) { cout << head << " "; out2(tail...); cout << "" << endl; } template <class T> void out(T &&head) { cout << head << endl; } void out() { cout << "" << endl; } #ifdef _DEBUG 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 (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) { ; } #endif template <class T> void outl(vector<T> &a, int n = inf) { rep(i, min(n, sz(a))) cout << a[i] << endl; } template <class T> void na(vector<T> &a, ll n) { a.resize(n); rep(i, n) cin >> a[i]; } #define dna(a, n) \ vi a(n); \ rep(dnai, n) cin >> a[dnai]; template <class T> void nao(vector<T> &a, ll n) { a.resize(n + 1); a[0] = 0; rep(i, n) cin >> a[i + 1]; } template <class T> void naod(vector<T> &a, ll n) { a.resize(n + 1); a[0] = 0; rep(i, n) cin >> a[i + 1], a[i + 1]--; } template <class T> void nad(vector<T> &a, ll n) { a.resize(n); rep(i, n) cin >> a[i], a[i]--; } template <class T, class U> void na2(vector<T> &a, vector<U> &b, ll n) { a.resize(n); b.resize(n); rep(i, n) cin >> a[i] >> b[i]; } #define dna2(a, b, n) \ vi a(n), b(n); \ rep(dna2i, n) cin >> a[dna2i] >> b[dna2i]; template <class T, class U> void nao2(vector<T> &a, vector<U> &b, ll n) { a.resize(n + 1); b.resize(n + 1); a[0] = b[0] = 0; rep(i, n) cin >> a[i + 1] >> b[i + 1]; } #define dna2d(a, b, n) \ vi a(n), b(n); \ rep(dna2di, n) { \ cin >> a[dna2di] >> b[dna2di]; \ a[dna2di]--, b[dna2di]--; \ } template <class T, class U> void na2d(vector<T> &a, vector<U> &b, ll n) { a.resize(n); b.resize(n); rep(i, n) cin >> a[i] >> b[i], a[i]--, b[i]--; } template <class T, class U, class W> void na3(vector<T> &a, vector<U> &b, vector<W> &c, ll n) { a.resize(n); b.resize(n); c.resize(n); rep(i, n) cin >> a[i] >> b[i] >> c[i]; } #define dna3(a, b, c, n) \ vi a(n), b(n), c(n); \ rep(dna3i, n) cin >> a[dna3i] >> b[dna3i] >> c[dna3i]; template <class T, class U, class W> void na3d(vector<T> &a, vector<U> &b, vector<W> &c, ll 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 dna3d(a, b, c, n) \ vi a(n), b(n), c(n); \ rep(dna3di, n) { \ cin >> a[dna3di] >> b[dna3di] >> c[dna3di]; \ a[dna3di]--, b[dna3di]--, c[dna3di]--; \ } template <class T, class U, class W, class X> void na4(vector<T> &a, vector<U> &b, vector<W> &c, vector<X> &d, ll n) { a.resize(n); b.resize(n); c.resize(n); d.resize(n); rep(i, n) cin >> a[i] >> b[i] >> c[i] >> d[i]; } #define dna4(a, b, c, d, n) \ vi a(n), b(n), c(n), d(n); \ rep(dna4i, n) cin >> a[dna4i] >> b[dna4i] >> c[dna4i] >> d[dna4i]; #define dna4d(a, b, c, d, n) \ vi a(n), b(n), c(n), d(n); \ rep(dna4i, n) cin >> a[dna4i] >> b[dna4i] >> c[dna4i] >> d[dna4i], \ --a[dna4i], --b[dna4i], --c[dna4i], --d[dna4i]; #define nt(a, h, w) \ resize(a, h, w); \ rep(nthi, h) rep(ntwi, w) cin >> a[nthi][ntwi]; #define ntd(a, h, w) \ resize(a, h, w); \ rep(ntdhi, h) rep(ntdwi, w) cin >> a[ntdhi][ntdwi], a[ntdhi][ntdwi]--; #define ntp(a, h, w) \ resize(a, h + 2, w + 2); \ fill(a, '#'); \ rep(ntphi, 1, h + 1) rep(ntpwi, 1, w + 1) cin >> a[ntphi][ntpwi]; //デバッグ #define sp << " " << #define debugName(VariableName) #VariableName #define deb1(x) debugName(x) << " = " << out_m(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' //@formatter:off //よく使うクラス、構造体 struct unionfind { vector<ll> par; vector<ll> siz; vector<ll> es; ll n, trees; //連結グループの数(親の種類) unionfind(ll n) : n(n), trees(n) { par.resize(n); siz.resize(n); es.resize(n); for (ll i = 0; i < n; i++) { par[i] = i; siz[i] = 1; } } ll root(ll x) { if (par[x] == x) { return x; } else { return par[x] = root(par[x]); } } bool unite(ll x, ll y) { x = root(x); y = root(y); es[x]++; if (x == y) return false; if (siz[x] > siz[y]) swap(x, y); trees--; par[x] = y; siz[y] += siz[x]; es[y] += es[x]; return true; } bool same(ll x, ll y) { return root(x) == root(y); } ll size(ll x) { return siz[root(x)]; } ll esize(ll x) { return es[root(x)]; } vi sizes() { vi cou(n); vi ret; ret.reserve(n); rep(i, n) { cou[root(i)]++; } rep(i, n) { if (cou[i]) ret.push_back(cou[i]); } return ret; } //つながりを無向グラフと見なし、xが閉路に含まれるか判定 bool close(ll x) { return esize(x) >= size(x); } vec<vi> sets() { vi ind(n, -1); ll i = 0; vvi(res, trees); rep(j, n) { ll r = root(j); if (ind[r] == -1) ind[r] = i++; res[ind[r]].push_back(j); } rep(i, trees) { ll r = root(res[i][0]); if (res[i][0] == r) continue; rep(j, 1, sz(res[i])) { if (res[i][j] == r) { swap(res[i][0], res[i][j]); break; } } } return res; } }; //@formatter:off using bll = __int128; using u32 = unsigned; using u64 = unsigned long long; using u128 = __uint128_t; std::ostream &operator<<(std::ostream &dest, __int128_t value) { std::ostream::sentry s(dest); if (s) { __uint128_t tmp = value < 0 ? -value : value; char buffer[128]; char *d = std::end(buffer); do { --d; *d = "0123456789"[tmp % 10]; tmp /= 10; } while (tmp != 0); if (value < 0) { --d; *d = '-'; } ll len = std::end(buffer) - d; if (dest.rdbuf()->sputn(d, len) != len) { dest.setstate(std::ios_base::badbit); } } return dest; } //__int128 toi128(string &s) { __int128 ret = 0; for (ll i = 0; i < //s.length(); ++i) if ('0' <= s[i] && s[i] <= '9') ret = 10 * //ret + s[i] - '0'; return ret;} //エラー 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 re() { assert(0 == 1); exit(0); } void tle() { while (inf) cout << inf << endl; } //便利関数 //テスト用 char ranc() { return (char)('a' + rand() % 26); } ll rand(ll min, ll max) { assert(min <= max); if (min >= 0 && max >= 0) { return rand() % (max + 1 - min) + min; } else if (max < 0) { return -rand(-max, -min); } else { if (rand() % 2) { return rand(0, max); } else { return -rand(0, -min); } } } vi ranv(ll n, ll min, ll max) { vi v(n); rep(i, n) v[i] = rand(min, max); return v; } str ransu(ll n) { str s; rep(i, n) s += (char)rand('A', 'Z'); return s; } str ransl(ll n) { str s; rep(i, n) s += (char)rand('a', 'z'); return s; } //単調増加 vi ranvinc(ll n, ll min, ll max) { vi v(n); bool bad = 1; while (bad) { bad = 0; v.resize(n); rep(i, n) { if (i && min > max - v[i - 1]) { bad = 1; break; } if (i) v[i] = v[i - 1] + rand(min, max - v[i - 1]); else v[i] = rand(min, max); } } return v; } //便利 汎用 void ranvlr(ll n, ll min, ll max, vi &l, vi &r) { l.resize(n); r.resize(n); rep(i, n) { l[i] = rand(min, max); r[i] = l[i] + rand(0, max - l[i]); } } 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 <class F> ll mgr(ll ok, ll ng, F f) { if (ok < ng) while (ng - ok > 1) { ll mid = (ok + ng) / 2; if (f(mid)) ok = mid; else ng = mid; } else while (ok - ng > 1) { ll mid = (ok + ng) / 2; if (f(mid)) ok = mid; else ng = mid; } return ok; } // strを整数として比較 string smax(str &a, str b) { if (sz(a) < sz(b)) { return b; } else if (sz(a) > sz(b)) { return a; } else if (a < b) return b; else return a; } // strを整数として比較 string smin(str &a, str b) { if (sz(a) > sz(b)) { return b; } else if (sz(a) < sz(b)) { return a; } else if (a > b) return b; else return a; } template <typename W, typename T> ll find(vector<W> &a, const T key) { rep(i, sz(a)) if (a[i] == key) return i; return -1; } template <typename W, typename T> P find(vector<vector<W>> &a, const T key) { rep(i, sz(a)) rep(j, sz(a[0])) if (a[i][j] == key) return mp(i, j); return mp(-1, -1); } template <typename W, typename U> T find(vector<vector<vector<W>>> &a, const U key) { rep(i, sz(a)) rep(j, sz(a[0])) rep(k, sz(a[0][0])) if (a[i][j][k] == key) return mt(i, j, k); return mt(-1, -1, -1); } template <typename W, typename T> ll count2(W &a, const T k) { return a == k; } template <typename W, typename T> ll count2(vector<W> &a, const T k) { ll ret = 0; fora(v, a) ret += count2(v, k); return ret; } template <typename W, typename T> ll count(vector<W> &a, const T k) { ll ret = 0; fora(v, a) ret += count2(v, k); return ret; } ll count(str &a, str k) { ll ret = 0, len = k.length(); auto pos = a.find(k); while (pos != string::npos) pos = a.find(k, pos + len), ++ret; return ret; } vi count(str &a) { vi cou(26); char c = 'a'; if ('A' <= a[0] && a[0] <= 'Z') c = 'A'; rep(i, sz(a))++ cou[a[i] - c]; return cou; } #define couif count_if // algorythm 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)); } template <class U> void rev(vector<vector<U>> &a) { vector<vector<U>> b(sz(a[0]), vector<U>(sz(a))); rep(h, sz(a)) rep(w, sz(a[0])) b[w][h] = a[h][w]; a = b; } void rev(string &a) { reverse(all(a)); } constexpr ll p10[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000ll, 100000000000ll, 1000000000000ll, 10000000000000ll, 100000000000000ll, 1000000000000000ll, 10000000000000000ll, 100000000000000000ll, 1000000000000000000ll}; ll get(ll a, ll keta) { return (a / (ll)pow(10, keta)) % 10; } ll keta(ll v) { if (v < p10[9]) { if (v < p10[4]) { if (v < p10[2]) { if (v < p10[1]) return 1; else return 2; } else { if (v < p10[3]) return 3; else return 4; } } else { if (v < p10[7]) { if (v < p10[5]) return 5; else if (v < p10[6]) return 6; else return 7; } else { if (v < p10[8]) return 8; else return 9; } } } else { if (v < p10[13]) { if (v < p10[11]) { if (v < p10[10]) return 10; else return 11; } else { if (v < p10[12]) return 12; else return 13; } } else { if (v < p10[15]) { if (v < p10[14]) return 14; else if (v < p10[15]) return 15; else return 16; } else { if (v < p10[17]) return 17; else return 18; } } } } ll dsum(ll v, ll sin = 10) { ll ret = 0; for (; v; v /= sin) ret += v % sin; return ret; } struct sint { ll v; sint(ll v) : v(v) {} operator ll() { return v; } //下からi番目 ll operator[](ll i) { return (v / p10[i]) % 10; } ll back(ll i) { return operator[](i); } //上からi番目 ll top(ll i) { ll len = keta(v); return operator[](len - 1 - i); } //先頭からi番目にセット ll settop(ll i, ll k) { ll len = keta(v); return set(len - 1 - i, k); } ll set(ll i, ll k) { if (i < 0) return settop(abs(i) - 1, k); return v += p10[i] * (k - (v / p10[i]) % 10); } ll add(ll i, ll k = 1) { return v += p10[i] * k; } ll addtop(ll i, ll k = 1) { return v += p10[keta(v) - i - 1] * k; } ll dec(ll i, ll k = 1) { return v -= p10[i] * k; } ll dectop(ll i, ll k = 1) { return v -= p10[keta(v) - i - 1] * k; } #define op(t, o) \ template <class T> t operator o(T r) { return v o r; } op(ll, +=); op(ll, -=); op(ll, *=); op(ll, /=); op(ll, %=); op(ll, +); op(ll, -); op(ll, *); op(ll, /); op(ll, %); op(bool, ==); op(bool, !=); op(bool, <); op(bool, <=); op(bool, >); op(bool, >=); #undef op template <class T> ll operator<<=(T r) { return v *= p10[r]; } template <class T> ll operator<<(T r) { return v * p10[r]; } template <class T> ll operator>>=(T r) { return v /= p10[r]; } template <class T> ll operator>>(T r) { return v / p10[r]; } }; ll mask10(ll v) { return p10[v] - 1; } //変換系 template <class T> auto keys(T &a) { vector<decltype((a.begin())->fi)> res; for (auto &&k : a) res.push_back(k.fi); return res; } template <class T> auto values(T &a) { vector<decltype((a.begin())->se)> res; for (auto &&k : a) res.push_back(k.se); return res; } template <class T, class U> bool chma(T &a, const U &b) { if (a < b) { a = b; return true; } return false; } template <class T, class U> bool chmi(T &a, const U &b) { if (b < a) { a = b; return true; } return false; } template <class T> T min(T a, signed b) { return a < b ? a : b; } template <class T> T max(T a, signed b) { return a < b ? b : a; } template <class T> T min(T a, T b, T c) { return a >= b ? b >= c ? c : b : a >= c ? c : a; } template <class T> T max(T a, T b, T c) { return a <= b ? b <= c ? c : b : a <= c ? c : a; } template <class T> T min(vector<T> &a) { return *min_element(all(a)); } template <class T> T mini(vector<T> &a) { return min_element(all(a)) - a.begin(); } template <class T> T min(vector<T> &a, ll n) { return *min_element(a.begin(), a.begin() + min(n, sz(a))); } template <class T> T min(vector<T> &a, ll s, ll n) { return *min_element(a.begin() + s, a.begin() + min(n, sz(a))); } template <class T> T max(vector<T> &a) { return *max_element(all(a)); } template <class T, class U> T max(vector<T> &a, vector<U> &b) { return max(*max_element(all(a)), *max_element(all(b))); } template <class T> T maxi(vector<T> &a) { return max_element(all(a)) - a.begin(); } template <class T> T max(vector<T> &a, ll n) { return *max_element(a.begin(), a.begin() + min(n, sz(a))); } template <class T> T max(vector<T> &a, ll s, ll n) { return *max_element(a.begin() + s, a.begin() + min(n, sz(a))); } template <typename A, size_t N> A max(A (&a)[N]) { A res = a[0]; rep(i, N) res = max(res, a[i]); return res; } template <typename A, size_t N, size_t O> A max(A (&a)[N][O]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P> A max(A (&a)[N][O][P]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A max(A (&a)[N][O][P][Q], const T &v) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A max(A (&a)[N][O][P][Q][R]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A max(A (&a)[N][O][P][Q][R][S]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N> A min(A (&a)[N]) { A res = a[0]; rep(i, N) res = min(res, a[i]); return res; } template <typename A, size_t N, size_t O> A min(A (&a)[N][O]) { A res = min(a[0]); rep(i, N) res = min(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P> A min(A (&a)[N][O][P]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A min(A (&a)[N][O][P][Q], const T &v) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A min(A (&a)[N][O][P][Q][R]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A min(A (&a)[N][O][P][Q][R][S]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <class T> T sum(vector<T> &v, ll s, ll t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += v[i]; return ret; } template <class T> T sum(vector<T> &v, ll t = inf) { return sum(v, 0, t); } template <class T> T sum(vector<vector<T>> &v) { T ret = 0; rep(i, sz(v)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<T>>> &v) { T ret = 0; rep(i, sz(v)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<vector<T>>>> &v) { T ret = 0; rep(i, sz(v)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<vector<vector<T>>>>> &v) { T ret = 0; rep(i, sz(v)) ret += sum(v[i]); return ret; } template <class T> auto sum(priority_queue<T, vector<T>, greater<T>> &r) { auto q = r; T ret = 0; while (sz(q)) { ret += q.top(); q.pop(); } return ret; } template <class T> auto sum(priority_queue<T> &r) { auto q = r; T ret = 0; while (sz(q)) { ret += q.top(); q.pop(); } return ret; } // template<class T, class U, class... W> auto sumn(vector<T> &v, U head, W... // tail) { auto ret = sum(v[0], tail...); rep(i, 1, min(sz(v), head))ret += // sum(v[i], tail...); return ret;} void clear(PQ &q) { q = PQ(); } template <class T> void clear(queue<T> &q) { while (q.size()) q.pop(); } template <class T> T *negarr(ll size) { T *body = (T *)malloc((size * 2 + 1) * sizeof(T)); return body + size; } template <class T> T *negarr2(ll h, ll w) { double **dummy1 = new double *[2 * h + 1]; double *dummy2 = new double[(2 * h + 1) * (2 * w + 1)]; dummy1[0] = dummy2 + w; for (ll i = 1; i <= 2 * h + 1; ++i) { dummy1[i] = dummy1[i - 1] + 2 * w + 1; } double **a = dummy1 + h; return a; } // 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> void inc(T &a, U v = 1) { a += v; } template <class T, class U> void inc(vector<T> &a, U v = 1) { for (auto &u : a) inc(u, v); } template <class T, class U> void dec(T &a, U v = 1) { a -= v; } template <class T, class U> void dec(vector<T> &a, U v = 1) { for (auto &u : a) dec(u, v); } template <class U> void dec(string &a, U v = 1) { for (auto &u : a) dec(u, v); } template <class T> void dec(vector<T> &a) { for (auto &u : a) dec(u, 1); } template <class T, class U> void dec(vector<T> &a, vector<U> &b) { for (auto &u : a) dec(u, 1); for (auto &u : b) dec(u, 1); } template <class T, class U, class W> void dec(vector<T> &a, vector<U> &b, vector<W> &c) { for (auto &u : a) dec(u, 1); for (auto &u : b) dec(u, 1); for (auto &u : c) dec(u, 1); } bool ins(ll h, ll w, ll H, ll W) { return h >= 0 && w >= 0 && h < H && w < W; } bool ins(ll l, ll v, ll r) { return l <= v && v < r; } template <class T> bool ins(vector<T> &a, ll i, ll j = 0) { return ins(0, i, sz(a)) && ins(0, j, sz(a)); } 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() //添え字を返す template <class F> ll goldd_l(ll left, ll right, F calc) { double GRATIO = 1.6180339887498948482045868343656; ll lm = left + (ll)((right - left) / (GRATIO + 1.0)); ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); ll fl = calc(lm); ll fr = calc(rm); while (right - left > 10) { if (fl < fr) { right = rm; rm = lm; fr = fl; lm = left + (ll)((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } ll minScore = MAX(ll); ll resIndex = left; for (ll i = left; i < right + 1; ++i) { ll score = calc(i); if (minScore > score) { minScore = score; resIndex = i; } } return resIndex; } template <class F> ll goldt_l(ll left, ll right, F calc) { double GRATIO = 1.6180339887498948482045868343656; ll lm = left + (ll)((right - left) / (GRATIO + 1.0)); ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); ll fl = calc(lm); ll fr = calc(rm); while (right - left > 10) { if (fl > fr) { right = rm; rm = lm; fr = fl; lm = left + (ll)((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } if (left > right) { ll l = left; left = right; right = l; } ll maxScore = MIN(ll); ll resIndex = left; for (ll i = left; i < right + 1; ++i) { ll score = calc(i); if (maxScore < score) { maxScore = score; resIndex = i; } } return resIndex; } /*loopは200にすればおそらく大丈夫 余裕なら300に*/ template <class F> dou goldd_d(dou left, dou right, F calc, ll loop = 200) { dou GRATIO = 1.6180339887498948482045868343656; dou lm = left + ((right - left) / (GRATIO + 1.0)); dou rm = lm + ((right - lm) / (GRATIO + 1.0)); dou fl = calc(lm); dou fr = calc(rm); /*200にすればおそらく大丈夫*/ /*余裕なら300に*/ ll k = 141; loop++; while (--loop) { if (fl < fr) { right = rm; rm = lm; fr = fl; lm = left + ((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + ((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } return left; } template <class F> dou goldt_d(dou left, dou right, F calc, ll loop = 200) { double GRATIO = 1.6180339887498948482045868343656; dou lm = left + ((right - left) / (GRATIO + 1.0)); dou rm = lm + ((right - lm) / (GRATIO + 1.0)); dou fl = calc(lm); dou fr = calc(rm); loop++; while (--loop) { if (fl > fr) { right = rm; rm = lm; fr = fl; lm = left + ((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + ((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } return left; } // l ~ rを複数の区間に分割し、極致を与えるiを返す time-20 msまで探索 template <class F> ll goldd_ls(ll l, ll r, F calc, ll time = 2000) { auto lim = milliseconds(time - 20); ll mini = 0, minv = MAX(ll); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/ ll nl = l; ll nr = l + haba; rep(i, k) { ll ni = goldd_l(nl, nr, calc); if (chmi(minv, calc(ni))) mini = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return mini; } template <class F> ll goldt_ls(ll l, ll r, F calc, ll time = 2000) { auto lim = milliseconds(time - 20); ll maxi = 0, maxv = MIN(ll); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/ ll nl = l; ll nr = l + haba; rep(i, k) { ll ni = goldt_l(nl, nr, calc); if (chma(maxv, calc(ni))) maxi = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return maxi; } template <class F> dou goldd_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を持つ*/ auto lim = milliseconds(time - 20); dou mini = 0, minv = MAX(dou); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); dou haba = (r - l) / k; dou nl = l; dou nr = l + haba; rep(i, k) { dou ni = goldd_d(nl, nr, calc); if (chmi(minv, calc(ni))) mini = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return mini; } template <class F> dou goldt_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を残している*/ auto lim = milliseconds(time - 20); dou maxi = 0, maxv = MIN(dou); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); dou haba = (r - l) / k; dou nl = l; dou nr = l + haba; rep(i, k) { dou ni = goldt_d(nl, nr, calc); if (chma(maxv, calc(ni))) maxi = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return maxi; } template <class T> T min(vector<vector<T>> &a) { T res = MAX(T); rep(i, a.size()) chmi(res, *min_element(all(a[i]))); return res; } template <class T> T max(vector<vector<T>> &a) { T res = MIN(T); rep(i, a.size()) chma(res, *max_element(all(a[i]))); return res; } constexpr bool bget(ll m, ll keta) { return (m >> keta) & 1; } ll bget(ll m, ll keta, ll sinsuu) { m /= (ll)pow(sinsuu, keta); return m % sinsuu; } ll bit(ll n) { return (1LL << (n)); } ll bit(ll n, ll sinsuu) { return (ll)pow(sinsuu, n); } ll mask(ll n) { return (1ll << n) - 1; } #define bcou __builtin_popcountll //最下位ビット ll lbit(ll n) { return n & -n; } //最上位ビット ll hbit(ll n) { n |= (n >> 1); n |= (n >> 2); n |= (n >> 4); n |= (n >> 8); n |= (n >> 16); n |= (n >> 32); return n - (n >> 1); } ll hbitk(ll n) { ll k = 0; rer(i, 5) { ll a = k + (1ll << i); ll b = 1ll << a; if (b <= n) k += 1ll << i; } return k; } //初期化は0を渡す ll nextComb(ll &mask, ll n, ll r) { if (!mask) return mask = (1LL << r) - 1; ll x = mask & -mask; /*最下位の1*/ ll y = mask + x; /*連続した下の1を繰り上がらせる*/ ll res = ((mask & ~y) / x >> 1) | y; if (bget(res, n)) return mask = 0; else return mask = res; } // n桁以下でビットがr個立っているもののvectorを返す vi bitCombList(ll n, ll r) { vi res; ll m = 0; while (nextComb(m, n, r)) { res.push_back(m); } return res; } // masの立ってるindexを見る #define forbit(i, mas) \ for (int forbitj = lbit(mas), forbitm = mas, i = log2(forbitj); forbitm; \ forbitm = forbitj ? forbitm ^ forbitj : 0, forbitj = lbit(forbitm), \ i = log2(forbitj)) char itoal(ll i) { return 'a' + i; } char itoaL(ll i) { return 'A' + i; } ll altoi(char c) { if ('A' <= c && c <= 'Z') return c - 'A'; return c - 'a'; } ll ctoi(char c) { return c - '0'; } char itoc(ll i) { return i + '0'; } ll vtoi(vi &v) { ll res = 0; if (sz(v) > 18) { debugline("vtoi"); deb(sz(v)); ole(); } rep(i, sz(v)) { res *= 10; res += v[i]; } return res; } vi itov(ll i) { vi res; while (i) { res.push_back(i % 10); i /= 10; } rev(res); return res; } vi stov(string &a) { ll n = sz(a); vi ret(n); rep(i, n) { ret[i] = a[i] - '0'; } return ret; } //基準を満たさないものは0になる vi stov(string &a, char one) { ll n = sz(a); vi ret(n); rep(i, n) ret[i] = a[i] == one; return ret; } vector<vector<ll>> ctoi(vector<vector<char>> s, char c) { ll n = sz(s), m = sz(s[0]); vector<vector<ll>> res(n, vector<ll>(m)); rep(i, n) rep(j, m) res[i][j] = s[i][j] == c; return res; } #define unique(v) v.erase(unique(v.begin(), v.end()), v.end()); //[i] := i番として圧縮されたものを返す vi compress(vi &a) { vi b; ll len = a.size(); for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { a[i] = lower_bound(all(b), a[i]) - b.begin(); } ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, umap<ll, ll> &map) { vi b; ll len = a.size(); for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { ll v = a[i]; a[i] = lower_bound(all(b), a[i]) - b.begin(); map[v] = a[i]; } ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, vi &r) { vi b; ll len = a.size(); fora(v, a) b.push_back(v); fora(v, r) b.push_back(v); sort(b); unique(b); for (ll i = 0; i < len; ++i) a[i] = lower_bound(all(b), a[i]) - b.begin(); for (ll i = 0; i < sz(r); ++i) r[i] = lower_bound(all(b), r[i]) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, vi &r, vi &s) { vi b; ll len = a.size(); fora(v, a) b.push_back(v); fora(v, r) b.push_back(v); fora(v, s) b.push_back(v); sort(b); unique(b); for (ll i = 0; i < len; ++i) a[i] = lower_bound(all(b), a[i]) - b.begin(); for (ll i = 0; i < sz(r); ++i) r[i] = lower_bound(all(b), r[i]) - b.begin(); for (ll i = 0; i < sz(s); ++i) r[i] = lower_bound(all(b), s[i]) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vector<vi> &a) { vi b; fora(vv, a) fora(v, vv) b.push_back(v); sort(b); unique(b); fora(vv, a) fora(v, vv) v = lower_bound(all(b), v) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vector<vector<vi>> &a) { vi b; fora(vvv, a) fora(vv, vvv) fora(v, vv) b.push_back(v); sort(b); unique(b); fora(vvv, a) fora(vv, vvv) fora(v, vv) v = lower_bound(all(b), v) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } void compress(ll a[], ll len) { vi b; for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { a[i] = lower_bound(all(b), a[i]) - b.begin(); } } //要素が見つからなかったときに困る #define binarySearch(a, v) (binary_search(all(a), v)) #define lowerIndex(a, v) (lower_bound(all(a), v) - a.begin()) #define lowerBound(a, v) (*lower_bound(all(a), v)) #define upperIndex(a, v) (upper_bound(all(a), v) - a.begin()) #define upperBound(a, v) (*upper_bound(all(a), v)) #define rlowerIndex(a, v) (upper_bound(all(a), v) - a.begin() - 1) #define rlowerBound(a, v) *(--(upper_bound(all(a), v))) #define rupperIndex(a, v) (lower_bound(all(a), v) - a.begin() - 1) #define rupperBound(a, v) *(--(lower_bound(all(a), v))) #define next2(a) next(next(a)) #define prev2(a) prev(prev(a)) //狭義の単調増加列 長さを返す template <class T> int lis(vector<T> &a) { int n = sz(a); vi tail(n + 1, MAX(T)); rep(i, n) { int id = lowerIndex(tail, a[i]); /**/ tail[id] = a[i]; } return lowerIndex(tail, MAX(T)); } template <class T> int lis_eq(vector<T> &a) { int n = sz(a); vi tail(n + 1, MAX(T)); rep(i, n) { int id = upperIndex(tail, a[i]); /**/ tail[id] = a[i]; } return lowerIndex(tail, MAX(T)); } // iteratorを返す // valueが1以上の物を返す 0は見つけ次第削除 // vを減らす場合 (*it).se--でいい template <class T, class U, class V> auto lower_map(map<T, U> &m, V k) { auto ret = m.lower_bound(k); while (ret != m.end() && (*ret).second == 0) { ret = m.erase(ret); } return ret; } template <class T, class U, class V> auto upper_map(map<T, U> &m, V k) { auto ret = m.upper_bound(k); while (ret != m.end() && (*ret).second == 0) { ret = m.erase(ret); } return ret; } //存在しなければエラー template <class T, class U, class V> auto rlower_map(map<T, U> &m, V k) { auto ret = upper_map(m, k); assert(ret != m.begin()); ret--; while (1) { if ((*ret).second != 0) break; assert(ret != m.begin()); auto next = ret; --next; m.erase(ret); ret = next; } return ret; } template <class T, class U, class V> auto rupper_map(map<T, U> &m, V k) { auto ret = lower_map(m, k); assert(ret != m.begin()); ret--; while (1) { if ((*ret).second != 0) break; assert(ret != m.begin()); auto next = ret; --next; m.erase(ret); ret = next; } return ret; } template <class T> void fin(T s) { cout << s << endl, exit(0); } //便利 数学 math ll mod(ll a, ll m) { return (a % m + m) % m; } ll pow(ll a) { return a * a; }; ll fact(ll v) { return v <= 1 ? 1 : v * fact(v - 1); } dou factd(int v) { static vd fact(2, 1); if (sz(fact) <= v) { rep(i, sz(fact), v + 1) { fact.push_back(fact.back() * i); } } return fact[v]; } ll comi(ll n, ll r) { assert(n < 100); static vvi(pas, 100, 100); if (pas[0][0]) return pas[n][r]; pas[0][0] = 1; rep(i, 1, 100) { pas[i][0] = 1; rep(j, 1, i + 1) pas[i][j] = pas[i - 1][j - 1] + pas[i - 1][j]; } return pas[n][r]; } double comd2(ll n, ll r) { static vvd(comb, 2020, 2020); if (comb[0][0] == 0) { comb[0][0] = 1; rep(i, 2000) { comb[i + 1][0] = 1; rep(j, 1, i + 2) { comb[i + 1][j] = comb[i][j] + comb[i][j - 1]; } } } return comb[n][r]; } double comd(int n, int r) { if (r < 0 || r > n) return 0; if (n < 2020) return comd2(n, r); static vd fact(2, 1); if (sz(fact) <= n) { rep(i, sz(fact), n + 1) { fact.push_back(fact.back() * i); } } return fact[n] / fact[n - r] / fact[r]; } ll gcd(ll a, ll b) { while (b) a %= b, swap(a, b); return abs(a); } ll gcd(vi b) { ll res = b[0]; rep(i, 1, sz(b)) res = gcd(b[i], res); return res; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll lcm(vi a) { ll res = a[0]; rep(i, 1, sz(a)) res = lcm(a[i], res); return res; } ll ceil(ll a, ll b) { if (b == 0) { debugline("ceil"); deb(a, b); ole(); return -1; } else if (a < 0) { return 0; } else { return (a + b - 1) / b; } } ll lower_remi__bx_a(ll kei, ll rem, ll x) { if (rem >= x) return 0; return (x - rem + kei - 1) / kei; } ll lower_remv__bx_a(ll kei, ll rem, ll x) { if (rem >= x) return rem; return (x - rem + kei - 1) / kei * kei + rem; } ll upper_remi__bx_a(ll kei, ll rem, ll x) { if (rem > x) return 0; return (x - rem + kei) / kei; } ll upper_remv__bx_a(ll kei, ll rem, ll x) { if (rem > x) return rem; return (x - rem + kei) / kei * kei + rem; } // v * v >= aとなる最小のvを返す 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) >> 1; } ll sig(ll s, ll t) { return ((s + t) * (t - s + 1)) >> 1; } //機能拡張 template <class T, class U> void operator+=(pair<T, U> &a, pair<T, U> &b) { a.fi += b.fi; a.se += b.se; } template <class T, class U> pair<T, U> operator+(pair<T, U> &a, pair<T, U> &b) { return pair<T, U>(a.fi + b.fi, a.se + b.se); } template <typename CharT, typename Traits, typename Alloc> basic_string<CharT, Traits, Alloc> operator+(const basic_string<CharT, Traits, Alloc> &lhs, const int rv) { basic_string<CharT, Traits, Alloc> str(lhs); str.append(to_string(rv)); return str; } template <typename CharT, typename Traits, typename Alloc> void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const int rv) { lhs += to_string(rv); } template <typename CharT, typename Traits, typename Alloc> basic_string<CharT, Traits, Alloc> operator+(const basic_string<CharT, Traits, Alloc> &lhs, const signed rv) { basic_string<CharT, Traits, Alloc> str(lhs); str.append(to_string(rv)); return str; } template <typename CharT, typename Traits, typename Alloc> void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const signed rv) { lhs += to_string(rv); } template <class T, class U> void operator+=(queue<T> &a, U v) { a.push(v); } template <class T, class U> void operator+=(deque<T> &a, U v) { a.push_back(v); } template <class T> priority_queue<T, vector<T>, greater<T>> & operator+=(priority_queue<T, vector<T>, greater<T>> &a, vector<T> &v) { fora(d, v) a.push(d); return a; } template <class T, class U> priority_queue<T, vector<T>, greater<T>> & operator+=(priority_queue<T, vector<T>, greater<T>> &a, U v) { a.push(v); return a; } template <class T, class U> priority_queue<T> &operator+=(priority_queue<T> &a, U v) { a.push(v); return a; } template <class T> set<T> &operator+=(set<T> &a, vector<T> v) { fora(d, v) a.insert(d); return a; } template <class T, class U> auto operator+=(set<T> &a, U v) { return a.insert(v); } template <class T, class U> auto operator-=(set<T> &a, U v) { return a.erase(v); } template <class T, class U> auto operator+=(mset<T> &a, U v) { return a.insert(v); } template <class T, class U> set<T, greater<T>> &operator+=(set<T, greater<T>> &a, U v) { a.insert(v); return a; } template <class T, class U> vector<T> &operator+=(vector<T> &a, U v) { a.push_back(v); return a; } template <class T, class U> vector<T> operator+(const vector<T> &a, U v) { vector<T> ret = a; ret += v; return ret; } template <class T, class U> vector<T> operator+(U v, const vector<T> &a) { vector<T> ret = a; ret.insert(ret.begin(), v); return ret; } template <class T> vector<T> operator+(vector<T> a, vector<T> b) { vector<T> ret; ret = a; fora(v, b) ret += v; return ret; } template <class T> vector<T> &operator+=(vector<T> &a, vector<T> &b) { fora(v, b) a += v; return a; } template <class T> vector<T> &operator-=(vector<T> &a, vector<T> &b) { if (sz(a) != sz(b)) { debugline("vector<T> operator-="); deb(a); deb(b); exit(0); } rep(i, sz(a)) a[i] -= b[i]; return a; } template <class T> vector<T> operator-(vector<T> &a, vector<T> &b) { if (sz(a) != sz(b)) { debugline("vector<T> operator-"); deb(a); deb(b); ole(); } vector<T> res(sz(a)); rep(i, sz(a)) res[i] = a[i] - b[i]; return res; } template <class T, class U> vector<T> operator*(vector<T> &a, U b) { vector<T> ret; fora(v, a) ret += v * b; return ret; } template <class T, class U> vector<T> operator/(vector<T> &a, U b) { vector<T> ret; fora(v, a) ret += v / b; return ret; } template <class T, class U> vector<T> operator*=(vector<T> &a, U b) { fora(v, a) v *= b; return a; } template <class T, class U> vector<T> operator/=(vector<T> &a, U b) { fora(v, a) v /= b; return a; } template <typename T> void erase(vector<T> &v, unsigned ll i) { v.erase(v.begin() + i); } template <typename T> void erase(vector<T> &v, unsigned ll s, unsigned ll e) { v.erase(v.begin() + s, v.begin() + e); } template <class T, class U> void erase(map<T, U> &m, ll okl, ll ngr) { m.erase(m.lower_bound(okl), m.lower_bound(ngr)); } template <class T> void erase(set<T> &m, ll okl, ll ngr) { m.erase(m.lower_bound(okl), m.lower_bound(ngr)); } template <typename T> void erasen(vector<T> &v, unsigned ll s, unsigned ll n) { v.erase(v.begin() + s, v.begin() + s + n); } template <typename T, typename U> void insert(vector<T> &v, unsigned ll i, U t) { v.insert(v.begin() + i, t); } template <typename T, typename U> void push_front(vector<T> &v, U t) { v.insert(v.begin(), t); } template <typename T, typename U> void insert(vector<T> &v, unsigned ll i, vector<T> list) { for (auto &&va : list) v.insert(v.begin() + i++, va); } template <typename T> void insert(set<T> &v, vector<T> list) { for (auto &&va : list) v.insert(va); } vector<string> split(const string a, const char deli) { string b = a + deli; ll l = 0, r = 0, n = b.size(); vector<string> res; rep(i, n) { if (b[i] == deli) { r = i; if (l < r) res.push_back(b.substr(l, r - l)); l = i + 1; } } return res; } vector<string> split(const string a, const string deli) { vector<string> res; ll kn = sz(deli); std::string::size_type Pos(a.find(deli)); ll l = 0; while (Pos != std::string::npos) { if (Pos - l) res.push_back(a.substr(l, Pos - l)); l = Pos + kn; Pos = a.find(deli, Pos + kn); } if (sz(a) - l) res.push_back(a.substr(l, sz(a) - l)); return res; } 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 <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)}; } //@formatter:off template <typename T> T minv(T a, T m); template <typename T> T minv(T a); template <typename T> class Modular { public: using Type = typename decay<decltype(T::value)>::type; constexpr Modular() : value() {} template <typename U> Modular(const U &x) { value = normalize(x); } template <typename U> static Type normalize(const U &x) { Type v; if (-mod() <= x && x < mod()) v = static_cast<Type>(x); else v = static_cast<Type>(x % mod()); if (v < 0) v += mod(); return v; } const Type &operator()() const { return value; } template <typename U> explicit operator U() const { return static_cast<U>(value); } constexpr static Type mod() { return T::value; } Modular &operator+=(const Modular &other) { if ((value += other.value) >= mod()) value -= mod(); return *this; } Modular &operator-=(const Modular &other) { if ((value -= other.value) < 0) value += mod(); return *this; } template <typename U> Modular &operator+=(const U &other) { return *this += Modular(other); } template <typename U> Modular &operator-=(const U &other) { return *this -= Modular(other); } Modular &operator++() { return *this += 1; } Modular &operator--() { return *this -= 1; } Modular operator++(signed) { Modular result(*this); *this += 1; return result; } Modular operator--(signed) { Modular result(*this); *this -= 1; return result; } Modular operator-() const { return Modular(-value); } template <typename U = T> typename enable_if<is_same<typename Modular<U>::Type, signed>::value, Modular>::type & operator*=(const Modular &rhs) { #ifdef _WIN32 uint64_t x = static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value); uint32_t xh = static_cast<uint32_t>(x >> 32), xl = static_cast<uint32_t>(x), d, m; asm("divl %4; \n\t" : "=a"(d), "=d"(m) : "d"(xh), "a"(xl), "r"(mod())); value = m; #else value = normalize(static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value)); #endif return *this; } template <typename U = T> typename enable_if<is_same<typename Modular<U>::Type, int64_t>::value, Modular>::type & operator*=(const Modular &rhs) { int64_t q = static_cast<int64_t>(static_cast<double>(value) * rhs.value / mod()); value = normalize(value * rhs.value - q * mod()); return *this; } template <typename U = T> typename enable_if<!is_integral<typename Modular<U>::Type>::value, Modular>::type & operator*=(const Modular &rhs) { value = normalize(value * rhs.value); return *this; } Modular &operator/=(const Modular &other) { return *this *= Modular(minv(other.value)); } template <typename U> friend bool operator==(const Modular<U> &lhs, const Modular<U> &rhs); template <typename U> friend bool operator<(const Modular<U> &lhs, const Modular<U> &rhs); template <typename U> friend std::istream &operator>>(std::istream &stream, Modular<U> &number); operator int() { return value; } private: Type value; }; template <typename T> bool operator==(const Modular<T> &lhs, const Modular<T> &rhs) { return lhs.value == rhs.value; } template <typename T, typename U> bool operator==(const Modular<T> &lhs, U rhs) { return lhs == Modular<T>(rhs); } template <typename T, typename U> bool operator==(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) == rhs; } template <typename T> bool operator!=(const Modular<T> &lhs, const Modular<T> &rhs) { return !(lhs == rhs); } template <typename T, typename U> bool operator!=(const Modular<T> &lhs, U rhs) { return !(lhs == rhs); } template <typename T, typename U> bool operator!=(U lhs, const Modular<T> &rhs) { return !(lhs == rhs); } template <typename T> bool operator<(const Modular<T> &lhs, const Modular<T> &rhs) { return lhs.value < rhs.value; } template <typename T> Modular<T> operator+(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) += rhs; } template <typename T, typename U> Modular<T> operator+(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) += rhs; } template <typename T, typename U> Modular<T> operator+(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) += rhs; } template <typename T> Modular<T> operator-(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) -= rhs; } template <typename T, typename U> Modular<T> operator-(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) -= rhs; } template <typename T, typename U> Modular<T> operator-(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) -= rhs; } template <typename T> Modular<T> operator*(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) *= rhs; } template <typename T, typename U> Modular<T> operator*(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) *= rhs; } template <typename T, typename U> Modular<T> operator*(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) *= rhs; } template <typename T> Modular<T> operator/(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) /= rhs; } template <typename T, typename U> Modular<T> operator/(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) /= rhs; } template <typename T, typename U> Modular<T> operator/(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) /= rhs; } constexpr signed MOD = // 998244353; 1e9 + 7; // MOD using mint = Modular<std::integral_constant<decay<decltype(MOD)>::type, MOD>>; constexpr int mint_len = 1400001; vi fac, finv, inv; vi p2; mint com(int n, int r) { if (r < 0 || r > n) return 0; return mint(finv[r] * fac[n] % MOD * finv[n - r]); } mint pom(int n, int r) { /* if (!sz(fac)) com(0, -1);*/ if (r < 0 || r > n) return 0; return mint(fac[n] * finv[n - 1]); } mint npr(int n, int r) { /* if (!sz(fac)) com(0, -1);*/ if (r < 0 || r > n) return 0; return mint(fac[n] * finv[n - r]); } int nprin(int n, int r) { /* if (!sz(fac)) com(0, -1);*/ if (r < 0 || r > n) return 0; return fac[n] * finv[n - r] % MOD; } int icom(int n, int r) { const int NUM_ = 1400001; static ll fac[NUM_ + 1], finv[NUM_ + 1], inv[NUM_ + 1]; if (fac[0] == 0) { inv[1] = fac[0] = finv[0] = 1; for (int i = 2; i <= NUM_; ++i) inv[i] = inv[MOD % i] * (MOD - MOD / i) % MOD; for (int i = 1; i <= NUM_; ++i) fac[i] = fac[i - 1] * i % MOD, finv[i] = finv[i - 1] * inv[i] % MOD; } if (r < 0 || r > n) return 0; return ((finv[r] * fac[n] % MOD) * finv[n - r]) % MOD; } #define ncr com #define ncri icom // n個の場所にr個の物を置く mint nhr(int n, int r) { return com(n + r - 1, r); } mint hom(int n, int r) { return com(n + r - 1, r); } int nhri(int n, int r) { return icom(n + r - 1, r); } template <typename T> T minv(T a, T m) { T u = 0, v = 1; while (a != 0) { T t = m / a; m -= t * a; swap(a, m); u -= t * v; swap(u, v); } assert(m == 1); return u; } template <typename T> T minv(T a) { if (a < mint_len) return inv[a]; T u = 0, v = 1; T m = MOD; while (a != 0) { T t = m / a; m -= t * a; swap(a, m); u -= t * v; swap(u, v); } assert(m == 1); return u; } template <typename T, typename U> Modular<T> mpow(const Modular<T> &a, const U &b) { assert(b >= 0); int x = a(), res = 1; U p = b; while (p > 0) { if (p & 1) (res *= x) %= MOD; (x *= x) %= MOD; p >>= 1; } return res; } template <typename T, typename U, typename V> mint mpow(const T a, const U b, const V m = MOD) { assert(b >= 0); int x = a, res = 1; U p = b; while (p > 0) { if (p & 1) (res *= x) %= m; (x *= x) %= m; p >>= 1; } return res; } template <typename T, typename U> mint mpow(const T a, const U b) { assert(b >= 0); int x = a, res = 1; U p = b; while (p > 0) { if (p & 1) (res *= x) %= MOD; (x *= x) %= MOD; p >>= 1; } return res; } template <typename T, typename U, typename V> int mpowi(const T &a, const U &b, const V &m = MOD) { assert(b >= 0); int x = a, res = 1; U p = b; while (p > 0) { if (p & 1) (res *= x) %= m; (x *= x) %= m; p >>= 1; } return res; } template <typename T> string to_string(const Modular<T> &number) { return to_string(number()); } string yuri(const mint &a) { stringstream st; rep(i, 300) { rep(j, 300) { if ((mint)i / j == a) { st << i << " / " << j; return st.str(); } } } rep(i, 1000) { rep(j, 1000) { if ((mint)i / j == a) { st << i << " / " << j; return st.str(); } } } return st.str(); } template <typename T> std::ostream &operator<<(std::ostream &stream, const Modular<T> &number) { stream << number(); #ifdef _DEBUG // stream << " -> " << yuri(number); #endif return stream; } //@formatter:off template <typename T> std::istream &operator>>(std::istream &stream, Modular<T> &number) { typename common_type<typename Modular<T>::Type, int64_t>::type x; stream >> x; number.value = Modular<T>::normalize(x); return stream; } using PM = pair<mint, mint>; using vm = vector<mint>; using mapm = map<int, mint>; using umapm = umap<int, mint>; #define vvm(...) \ o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(mint, __VA_ARGS__) #define vnm(name, ...) auto name = make_v<mint>(__VA_ARGS__) //@formatter:on // nhr n個の場所にr個の物を分ける //@起動時 struct initon { initon() { cin.tie(0); ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(16); srand((unsigned)clock() + (unsigned)time(NULL)); }; } initonv; //@formatter:on // gra mll pr //上下左右 const string udlr = "udlr"; string UDLR = "UDLR"; // x4と連動 UDLR.find('U') := x4[0] //右、上が正 constexpr ll y4[] = {1, -1, 0, 0}; constexpr ll x4[] = {0, 0, -1, 1}; constexpr ll y8[] = {0, 1, 0, -1, -1, 1, 1, -1}; constexpr ll x8[] = {1, 0, -1, 0, 1, -1, 1, -1}; ll n, m, k, K, d, H, W, x, y, z, q, Q; ll cou; vi a, b, c; // vvi (s, 0, 0); vvc(ba, 0, 0); vp p; str s; //一番高速 // hashを共有しない class roll_fast_only { static constexpr uint64_t base = 3628397923; //他のロリハとhashを共有したい場合 // const uint64_t base = rand((uint64_t) 2, mask61 - // 1);//共有したくない場合 static constexpr uint64_t mask30 = ((uint64_t)1 << 30) - 1; static constexpr uint64_t mask31 = ((uint64_t)1 << 31) - 1; static constexpr uint64_t mask61 = ((uint64_t)1 << 61) - 1; static constexpr uint64_t offset = mask61 * 7; static constexpr uint64_t mod(const uint64_t x) { const uint64_t y = (x >> 61) + (x & mask61); return y >= mask61 ? y - mask61 : y; } static constexpr uint64_t mul(const uint64_t x, const uint64_t y) { const uint64_t xh = x >> 31, xl = x & mask31; const uint64_t yh = y >> 31, yl = y & mask31; const uint64_t z = xl * yh + xh * yl; const uint64_t zh = z >> 30, zl = z & mask30; return xh * yh * 2 + zh + (zl << 31) + xl * yl; } const std::size_t siz; vector<uint64_t> pow, h; public: template <typename T> roll_fast_only(T &A) : siz(sz(A)), pow(siz + 1, 1), h(siz + 1, 0) { for (std::size_t i = 1; i <= siz; i++) { pow[i] = mod(mul(pow[i - 1], base)), h[i] = mod(mul(h[i - 1], base) + A[i - 1]); } } uint64_t operator()(const std::size_t l, const std::size_t r) { return mod(h[r] + offset - mul(h[l], pow[r - l])); } uint64_t operator()(int r = inf) { return operator()(0, min(siz, r)); } inline int lcp_dup(int a, int b) { int len = min((int)siz - a, (int)siz - b); int low = 0, high = len; while (high - low > 1) { int mid = (low + high) >> 1; if (operator()(a, a + mid) != operator()(b, b + mid)) high = mid; else low = mid; } return low; } inline int lcp_not(int a, int b) { return min(lcp_dup(a, b), b - a); } //@formatter:on }; //動かない場合roll_fast_only2 // roll_fast_only ro(list); // lcpならsa := saffix_arrayでO(1) 構成O(N log(n)^2) void solve() { in(n); in(s); roll_fast_only ro(s); rer(len, n - 1, 1) { // umapu m; unordered_map<uint64_t, int> m; rep(i, n - len + 1) { auto has = ro(i, i + len); if (m.find(has) != m.end()) { if (m[has] <= i) fin(len); } else m[has] = i + len; } } fin(0); } auto my(ll n, vi &a) { return 0; } auto sister(ll n, vi &a) { ll ret = 0; return ret; } signed main() { solve(); #define arg n, a #ifdef _DEBUG bool bad = 0; for (ll i = 0, ok = 1; i < k5 && ok; ++i) { ll n = rand(1, 8); vi a = ranv(n, 1, 10); auto myres = my(arg); auto res = sister(arg); ok = myres == res; if (!ok) { out(arg); cerr << "AC : " << res << endl; cerr << "MY : " << myres << endl; bad = 1; break; } } if (!bad) { // cout << "完璧 : solveを書き直そう" << endl; // cout << " : そして、solve()を呼び出すのだ" << endl; // cout << " : cin>>n; na(a,n);も忘れるな" << endl; } #endif return 0; };
[ "literal.number.change" ]
744,133
744,134
u986437843
cpp
p02913
//#pragma GCC optimize("Ofast") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace std::chrono; #define int long long #define ll long long auto start_time = system_clock::now(); //@formatter:off #ifdef _DEBUG //区間削除は出来ない template <class T> struct my_pbds_tree { set<T> s; auto begin() { return s.begin(); } auto end() { return s.end(); } auto rbegin() { return s.rbegin(); } auto rend() { return s.rend(); } auto empty() { return s.empty(); } auto size() { return s.size(); } void clear() { s.clear(); } template <class U> void insert(U v) { s.insert(v); } template <class U> void operator+=(U v) { insert(v); } template <class F> auto erase(F v) { return s.erase(v); } template <class U> auto find(U v) { return s.find(v); } template <class U> auto lower_bound(U v) { return s.lower_bound(v); } template <class U> auto upper_bound(U v) { return s.upper_bound(v); } auto find_by_order(ll k) { auto it = s.begin(); for (ll i = 0; i < k; i++) it++; return it; } auto order_of_key(ll v) { auto it = s.begin(); ll i = 0; for (; it != s.end() && *it < v; i++) it++; return i; } }; #define pbds(T) my_pbds_tree<T> // gp_hash_tableでcountを使えないようにするため template <class T, class U> struct my_unordered_map { unordered_map<T, U> m; my_unordered_map(){}; auto begin() { return m.begin(); } auto end() { return m.end(); } auto cbegin() { return m.cbegin(); } auto cend() { return m.cend(); } template <class V> auto erase(V v) { return m.erase(v); } void clear() { m.clear(); } /*countは gp_hash_tableに存在しない*/ /*!= m.end()*/ template <class V> auto find(V v) { return m.find(v); } template <class V> auto &operator[](V n) { return m[n]; } }; #define unordered_map my_unordered_map #define umapi unordered_map<ll, ll> #define umapu unordered_map<uint64_t, ll> #define umapp unordered_map<P, ll> #else #define unordered_map __gnu_pbds::gp_hash_table // find_by_order(k) k番目のイテレーター // order_of_key(k) k以上が前から何番目か #define pbds(U) \ __gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag, \ __gnu_pbds::tree_order_statistics_node_update> #define umapi unordered_map<ll, ll, xorshift> #define umapp unordered_map<P, ll, xorshift> #define umapu unordered_map<uint64_t, ll, xorshift> #endif struct xorshift { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } size_t operator()(std::pair<ll, ll> x) const { ll v = ((x.first) << 32) | x.second; static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(v + FIXED_RANDOM); } }; template <class U, class L> void operator+=( __gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update> &s, L v) { s.insert(v); } //衝突対策 #define ws wszzzz template <class A, class B, class C> struct T2 { A f; B s; C t; T2() { f = 0, s = 0, t = 0; } T2(A f, B s, C t) : f(f), s(s), t(t) {} bool operator<(const T2 &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 ?n s > r.s : t > r.t; 大きい順 */ } bool operator>(const T2 &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 T2 &r) const { return f == r.f && s == r.s && t == r.t; } bool operator!=(const T2 &r) const { return f != r.f || s != r.s || t != r.t; } }; template <class A, class B, class C, class D> struct F2 { A a; B b; C c; D d; F2() { a = 0, b = 0, c = 0, d = 0; } F2(A a, B b, C c, D d) : a(a), b(b), c(c), d(d) {} bool operator<(const F2 &r) const { return a != r.a ? a < r.a : b != r.b ? b < r.b : c != r.c ? c < r.c : d < r.d; /* return a != r.a ? a > r.a : b != r.b ? b > r.b : c != r.c ? c > r.c : d > r.d;*/ } bool operator>(const F2 &r) const { return a != r.a ? a > r.a : b != r.b ? b > r.b : c != r.c ? c > r.c : d > r.d; /* return a != r.a ? a < r.a : b != r.b ? b < r.b : c != r.c ? c < r.c : d < r.d;*/ } bool operator==(const F2 &r) const { return a == r.a && b == r.b && c == r.c && d == r.d; } bool operator!=(const F2 &r) const { return a != r.a || b != r.b || c != r.c || d != r.d; } ll operator[](ll i) { assert(i < 4); return i == 0 ? a : i == 1 ? b : i == 2 ? c : d; } }; typedef T2<ll, ll, ll> T; typedef F2<ll, ll, ll, ll> F; T mt(ll a, ll b, ll c) { return T(a, b, c); } //@マクロ省略系 型,構造 #define double long double #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 beg begin #define rbeg rbegin #define con continue #define bre break #define brk break #define is == #define el else #define elf else if #define wh while #define upd update #define maxq 1 #define minq -1 #define ZERO(a) memset(a, 0, sizeof(a)) #define MINUS(a) memset(a, 0xff, sizeof(a)) #define MALLOC(type, len) (type *)malloc((len) * sizeof(type)) #define lam(right) [&](ll &p) { return p right; } //マクロ省略系 コンテナ 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 PQ priority_queue<ll, vector<ll>, greater<ll>> #define tos to_string using mapi = map<ll, ll>; using mapp = map<P, ll>; using mapd = map<dou, ll>; using mapc = map<char, ll>; using maps = map<str, ll>; using seti = set<ll>; using setd = set<dou>; using setc = set<char>; using sets = set<str>; using qui = queue<ll>; #define bset bitset #define uset unordered_set #define useti unordered_set<ll, ll, xorshift> #define mset multiset #define mseti multiset<ll> #define umap unordered_map #define mmap multimap template <class T> struct pq { priority_queue<T, vector<T>, greater<T>> q; /*小さい順*/ T su = 0; void clear() { q = priority_queue<T, vector<T>, greater<T>>(); su = 0; } void operator+=(T v) { su += v; q.push(v); } T sum() { return su; } T top() { return q.top(); } void pop() { su -= q.top(); q.pop(); } T poll() { T ret = q.top(); su -= ret; q.pop(); return ret; } ll size() { return q.size(); } }; template <class T> struct pqg { priority_queue<T> q; /*大きい順*/ T su = 0; void clear() { q = priority_queue<T>(); su = 0; } void operator+=(T v) { su += v; q.push(v); } T sum() { return su; } T top() { return q.top(); } void pop() { su -= q.top(); q.pop(); } T poll() { T ret = q.top(); su -= ret; q.pop(); return ret; } ll size() { return q.size(); } }; #define pqi pq<ll> #define pqgi pqg<ll> //マクロ 繰り返し #define o_rep(o1, o2, o3, o4, name, ...) name #define rep1(n) for (ll rep1i = 0, rep1lim = n; rep1i < rep1lim; ++rep1i) #define rep2(i, n) for (ll i = 0, rep2lim = n; i < rep2lim; ++i) #define rep3(i, m, n) for (ll i = m, rep3lim = n; i < rep3lim; ++i) #define rep4(i, m, n, ad) for (ll i = m, rep4lim = n; i < rep4lim; i += ad) #define rep(...) o_rep(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__) #define rer2(i, n) for (ll i = n; i >= 0; i--) #define rer3(i, m, n) for (ll i = m, rer3lim = n; i >= rer3lim; i--) #define rer4(i, m, n, dec) for (ll i = m, rer4lim = n; i >= rer4lim; i -= dec) #define rer(...) o_rep(__VA_ARGS__, rer4, rer3, rer2, )(__VA_ARGS__) #define reps2(i, j, n) \ for (ll i = 0, reps2lim = n; i < reps2lim; ++i) \ for (ll j = 0; j < reps2lim; ++j) #define reps3(i, j, k, n) \ for (ll i = 0, reps3lim = n; i < reps3lim; ++i) \ for (ll j = 0; j < reps3lim; ++j) \ for (ll k = 0; k < reps3lim; ++k) #define reps4(i, j, k, l, n) \ for (ll i = 0, reps4lim = n; i < reps4lim; ++i) \ for (ll j = 0; j < reps4lim; ++j) \ for (ll k = 0; k < reps4lim; ++k) \ for (ll l = 0; l < reps4lim; ++l) #define o_reps(o1, o2, o3, o4, o5, name, ...) name #define reps(...) o_reps(__VA_ARGS__, reps4, reps3, reps2, rep2, )(__VA_ARGS__) #define repss(i, j, k, a, b, c) \ for (ll i = 0; i < a; ++i) \ for (ll j = 0; j < b; ++j) \ for (ll k = 0; k < c; ++k) #define fora(a, b) for (auto &&a : b) //インデックスを前後含めて走査 #define fori(i, s, len) \ for (int i = s, prev = (s == 0) ? len - 1 : s - 1, \ next = (s == len - 1) ? 0 : s + 1, cou = 0; \ cou < len; \ cou++, prev = i, i = next, next = (next == len - 1) ? 0 : next + 1) #define form(st, l, r) \ for (auto &&it = st.lower_bound(l); it != st.end() && (*it).fi < r; ++it) #define forit(st, l, r) \ for (auto &&it = st.lower_bound(l); it != st.end() && (*it) < r;) //マクロ 定数 #define k3 1010 #define k4 10101 #define k5 101010 #define k6 1010101 #define k7 10101010 const ll inf = (ll)1e9 + 100; const ll linf = (ll)1e18 + 100; const char infc = '{'; const string infs = "{"; const double eps = 1e-9; const double PI = 3.1415926535897932384626433832795029L; //マクロ省略形 関数等 #define arsz(a) (sizeof(a) / sizeof(a[0])) #define sz(a) ((ll)(a).size()) #define mp make_pair #define pb pop_back #define pf push_front #define eb emplace_back #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() constexpr bool ev(ll a) { return !(a & 1); } constexpr bool od(ll a) { return (a & 1); } //@拡張系 こう出来るべきというもの //埋め込み 存在を意識せずに機能を増やされているもの //@formatter:on namespace std { template <> class hash<std::pair<signed, signed>> { public: size_t operator()(const std::pair<signed, signed> &x) const { return hash<ll>()(((ll)x.first << 32) | x.second); } }; template <> class hash<std::pair<ll, ll>> { public : /*大きいllが渡されると、<<32でオーバーフローするがとりあえず問題ないと判断*/ size_t operator()(const std::pair<ll, ll> &x) const { return hash<ll>()(((ll)x.first << 32) | x.second); } }; } // namespace std //@formatter:off // stream まとめ istream &operator>>(istream &iss, P &a) { iss >> a.first >> a.second; return iss; } template <typename T> istream &operator>>(istream &iss, vector<T> &vec) { for (T &x : vec) iss >> x; return iss; } template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> p) { os << p.fi << " " << p.se << endl; return os; } ostream &operator<<(ostream &os, T p) { os << p.f << " " << p.s << " " << p.t; return os; } ostream &operator<<(ostream &os, F p) { os << p.a << " " << p.b << " " << p.c << " " << p.d; return os; } template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) { for (ll i = 0; i < vec.size(); ++i) os << vec[i] << (i + 1 == vec.size() ? "" : " "); return os; } template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) { for (ll i = 0; i < vec.size(); ++i) { for (ll j = 0; j < vec[i].size(); ++j) { os << vec[i][j] << " "; } os << endl; } return os; } template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &m) { for (auto &&v : m) os << v; return os; } template <class T> ostream &operator<<(ostream &os, set<T> s) { fora(v, s) { os << v << " "; } return os; } template <class T> ostream &operator<<(ostream &os, deque<T> a) { fora(v, a) os << v << " "; return os; } 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); }); } #define all_of(a, right) all_of2(a, lam(right)) #define any_of(a, right) any_of2(a, lam(right)) #define none_of(a, right) none_of2(a, lam(right)) #define find_if(a, right) find_if2(a, lam(right)) #define rfind_if(a, right) rfind_if2(a, lam(right)) #define contains_if(a, right) contains_if2(a, lam(right)) #define count_if(a, right) count_if2(a, lam(right)) #define for_each(a, right) \ do { \ fora(v, a) { v right; } \ } while (0) template <class T, class U> void replace(vector<T> &a, T key, U v) { replace(a.begin(), a.end(), key, v); } void replace(str &a, char key, str v) { if (v == "") a.erase(remove(all(a), key), a.end()); } void replace(str &a, char key, char v) { replace(all(a), key, v); } // keyと同じかどうか01で置き換える template <class T, class U> void replace(vector<T> &a, U k) { rep(i, sz(a)) a[i] = a[i] == k; } template <class T, class U> void replace(vector<vector<T>> &a, U k) { rep(i, sz(a)) rep(j, sz(a[0])) a[i][j] = a[i][j] == k; } template <class T> void replace(T &a) { replace(a, '#'); } void replace(str &a, str key, str v) { stringstream t; ll kn = sz(key); std::string::size_type Pos(a.find(key)); ll l = 0; while (Pos != std::string::npos) { t << a.substr(l, Pos - l); t << v; l = Pos + kn; Pos = a.find(key, Pos + kn); } t << a.substr(l, sz(a) - l); a = t.str(); } template <class T> bool includes(vector<T> &a, vector<T> &b) { vi c = a; vi d = b; sort(all(c)); sort(all(d)); return includes(all(c), all(d)); } template <class T> bool is_permutation(vector<T> &a, vector<T> &b) { return is_permutation(all(a), all(b)); } template <class T> bool next_permutation(vector<T> &a) { return next_permutation(all(a)); } void iota(vector<ll> &ve, ll s, ll n) { ve.resize(n); iota(all(ve), s); } vi iota(ll s, ll len) { vi ve(len); iota(all(ve), s); return ve; } template <class A, class B> auto vtop(vector<A> &a, vector<B> &b) { assert(sz(a) == sz(b)); /*stringを0で初期化できない */ vector<pair<A, B>> res; rep(i, sz(a)) res.eb(a[i], b[i]); return res; } template <class A, class B> void ptov(vector<pair<A, B>> &p, vector<A> &a, vector<B> &b) { a.resize(sz(p)), b.resize(sz(p)); rep(i, sz(p)) a[i] = p[i].fi, b[i] = p[i].se; } template <class A, class B, class C> auto vtot(vector<A> &a, vector<B> &b, vector<C> &c) { assert(sz(a) == sz(b) && sz(b) == sz(c)); vector<T2<A, B, C>> res; rep(i, sz(a)) res.eb(a[i], b[i], c[i]); return res; } template <class A, class B, class C, class D> auto vtof(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { assert(sz(a) == sz(b) && sz(b) == sz(c) && sz(c) == sz(d)); vector<F2<A, B, C, D>> res; rep(i, sz(a)) res.eb(a[i], b[i], c[i], d[i]); return res; } enum pcomparator { fisi, fisd, fdsi, fdsd, sifi, sifd, sdfi, sdfd }; enum tcomparator { fisiti, fisitd, fisdti, fisdtd, fdsiti, fdsitd, fdsdti, fdsdtd, fitisi, fitisd, fitdsi, fitdsd, fdtisi, fdtisd, fdtdsi, fdtdsd, sifiti, sifitd, sifdti, sifdtd, sdfiti, sdfitd, sdfdti, sdfdtd, sitifi, sitifd, sitdfi, sitdfd, sdtifi, sdtifd, sdtdfi, sdfdfd, tifisi, tifisd, tifdsi, tifdsd, tdfisi, tdfisd, tdfdsi, tdfdsd, tisifi, tisifd, tisdfi, tisdfd, tdsifi, tdsifd, tdsdfi, tdsdfd }; template <class A, class B> void sort(vector<pair<A, B>> &a, pcomparator type) { typedef pair<A, B> U; if (type == fisi) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se < r.se; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se > r.se; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se < r.se; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se > r.se; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi < r.fi; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi > r.fi; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi < r.fi; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi > r.fi; }); }; template <class U> void sort(vector<U> &a, pcomparator type) { if (type == fisi) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f > r.f; }); }; template <class A, class B, class C, class D> void sort(vector<F2<A, B, C, D>> &a, pcomparator type) { typedef F2<A, B, C, D> U; if (type == fisi) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a > r.a; }); }; template <class U> void sort(vector<U> &a, tcomparator type) { if (type == 0) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t < r.t; }); else if (type == 1) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t > r.t; }); else if (type == 2) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t < r.t; }); else if (type == 3) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t > r.t; }); else if (type == 4) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t < r.t; }); else if (type == 5) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t > r.t; }); else if (type == 6) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t < r.t; }); else if (type == 7) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t > r.t; }); else if (type == 8) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s < r.s; }); else if (type == 9) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s > r.s; }); else if (type == 10) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s < r.s; }); else if (type == 11) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s > r.s; }); else if (type == 12) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s < r.s; }); else if (type == 13) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s > r.s; }); else if (type == 14) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s < r.s; }); else if (type == 15) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s > r.s; }); else if (type == 16) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t < r.t; }); else if (type == 17) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t > r.t; }); else if (type == 18) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t < r.t; }); else if (type == 19) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t > r.t; }); else if (type == 20) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t < r.t; }); else if (type == 21) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t > r.t; }); else if (type == 22) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t < r.t; }); else if (type == 23) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t > r.t; }); else if (type == 24) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f < r.f; }); else if (type == 25) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f > r.f; }); else if (type == 26) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f < r.f; }); else if (type == 27) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f > r.f; }); else if (type == 28) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f < r.f; }); else if (type == 29) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f > r.f; }); else if (type == 30) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f < r.f; }); else if (type == 31) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f > r.f; }); else if (type == 32) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == 33) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == 34) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == 35) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == 36) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == 37) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == 38) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == 39) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == 40) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == 41) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == 42) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == 43) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f > r.f; }); else if (type == 44) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == 45) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == 46) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == 47) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f > r.f; }); } template <class A, class B, class C, class D> void sort(vector<F2<A, B, C, D>> &a, tcomparator type) { typedef F2<A, B, C, D> U; if (type == 0) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c < r.c; }); else if (type == 1) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c > r.c; }); else if (type == 2) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c < r.c; }); else if (type == 3) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c > r.c; }); else if (type == 4) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c < r.c; }); else if (type == 5) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c > r.c; }); else if (type == 6) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c < r.c; }); else if (type == 7) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c > r.c; }); else if (type == 8) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b < r.b; }); else if (type == 9) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b > r.b; }); else if (type == 10) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b < r.b; }); else if (type == 11) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b > r.b; }); else if (type == 12) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b < r.b; }); else if (type == 13) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b > r.b; }); else if (type == 14) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b < r.b; }); else if (type == 15) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b > r.b; }); else if (type == 16) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c < r.c; }); else if (type == 17) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c > r.c; }); else if (type == 18) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c < r.c; }); else if (type == 19) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c > r.c; }); else if (type == 20) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c < r.c; }); else if (type == 21) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c > r.c; }); else if (type == 22) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c < r.c; }); else if (type == 23) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c > r.c; }); else if (type == 24) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a < r.a; }); else if (type == 25) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a > r.a; }); else if (type == 26) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a < r.a; }); else if (type == 27) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a > r.a; }); else if (type == 28) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a < r.a; }); else if (type == 29) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a > r.a; }); else if (type == 30) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a < r.a; }); else if (type == 31) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a > r.a; }); else if (type == 32) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == 33) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == 34) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == 35) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == 36) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == 37) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == 38) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == 39) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == 40) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == 41) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == 42) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == 43) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a > r.a; }); else if (type == 44) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == 45) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == 46) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == 47) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a > r.a; }); } void sort(string &a) { sort(all(a)); } template <class T> void sort(vector<T> &a) { sort(all(a)); } // P l, P rで f(P) の形で渡す template <class U, class F> void sort(vector<U> &a, F f) { sort(all(a), [&](U l, U r) { return f(l) < f(r); }); }; template <class T> void rsort(vector<T> &a) { sort(all(a), greater<T>()); }; template <class U, class F> void rsort(vector<U> &a, F f) { sort(all(a), [&](U l, U r) { return f(l) > f(r); }); }; // F = T<T> //例えばreturn p.fi + p.se; template <class A, class B> void sortp(vector<A> &a, vector<B> &b) { auto c = vtop(a, b); sort(c); rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se; } template <class A, class B, class F> void sortp(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); sort(c, f); rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se; } template <class A, class B> void rsortp(vector<A> &a, vector<B> &b) { auto c = vtop(a, b); rsort(c); rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second; } template <class A, class B, class F> void rsortp(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); rsort(c, f); rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second; } template <class A, class B, class C> void sortt(vector<A> &a, vector<B> &b, vector<C> &c) { auto d = vtot(a, b, c); sort(d); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class F> void sortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); sort(d, f); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C> void rsortt(vector<A> &a, vector<B> &b, vector<C> &c) { auto d = vtot(a, b, c); rsort(d); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class F> void rsortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); rsort(d, f); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class D> void sortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { auto e = vtof(a, b, c, d); sort(e); rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d; } template <class A, class B, class C, class D> void rsortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { auto e = vtof(a, b, c, d); rsort(e); rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d; } // sortindex 元のvectorはソートしない template <class T> vi sorti(vector<T> &a) { auto b = a; vi ind = iota(0, sz(a)); sortp(b, ind); return ind; } /*indexの分で型が変わるためpcomparatorが必要*/ template <class T> vi sorti(vector<T> &a, pcomparator f) { auto b = a; vi ind = iota(0, sz(a)); sortp(b, ind, f); return ind; } template <class T, class F> vi sorti(vector<T> &a, F f) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(a[x]) < f(a[y]); }); return ind; } template <class T> vi rsorti(vector<T> &a) { auto b = a; vi ind = iota(0, sz(a)); rsortp(b, ind); return ind; } template <class T, class F> vi rsorti(vector<T> &a, F f) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(a[x]) > f(a[y]); }); return ind; } template <class A, class B, class F> vi sortpi(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(c[x]) < f(c[y]); }); return ind; } template <class A, class B> vi sortpi(vector<A> &a, vector<B> &b, pcomparator f) { vi ind = iota(0, sz(a)); auto c = a; auto d = b; sortt(c, d, ind, f); return ind; } template <class A, class B> vi sortpi(vector<A> &a, vector<B> &b) { return sortpi(a, b, fisi); }; template <class A, class B, class F> vi rsortpi(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(c[x]) > f(c[y]); }); return ind; } template <class A, class B> vi rsortpi(vector<A> &a, vector<B> &b) { return sortpi(a, b, fdsd); }; template <class A, class B, class C, class F> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(d[x]) < f(d[y]); }); return ind; } template <class A, class B, class C> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, pcomparator f) { vi ind = iota(0, sz(a)); auto d = vtof(a, b, c, ind); sort(d, f); rep(i, sz(a)) ind[i] = d[i].d; return ind; } template <class A, class B, class C> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { if (a[x] == a[y]) { if (b[x] == b[y]) return c[x] < c[y]; else return b[x] < b[y]; } else { return a[x] < a[y]; } }); return ind; } template <class A, class B, class C, class F> vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(d[x]) > f(d[y]); }); return ind; } template <class A, class B, class C> vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { if (a[x] == a[y]) { if (b[x] == b[y]) return c[x] > c[y]; else return b[x] > b[y]; } else { return a[x] > a[y]; } }); return ind; } template <class T> void sort2(vector<vector<T>> &a) { for (ll i = 0, n = a.size(); i < n; ++i) sort(a[i]); } template <class T> void rsort2(vector<vector<T>> &a) { for (ll 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 W, typename T> void fill(W &xx, const T vall) { xx = vall; } template <typename W, typename T> void fill(vector<W> &vecc, const T vall) { for (auto &&vx : vecc) fill(vx, vall); } template <typename W, typename T> void fill(vector<W> &xx, const T v, ll len) { rep(i, len) xx[i] = v; } template <typename W, typename T> void fill(vector<vector<W>> &xx, const T v, ll lh, ll lw) { rep(i, lh) rep(j, lw) xx[i][j] = v; } template <class T, class U> void fill(vector<T> &a, U val, vi &ind) { fora(v, ind) a[v] = val; } template <typename A, size_t N> A sum(A (&a)[N]) { A res = 0; rep(i, N) res += a[i]; return res; } template <typename A, size_t N, size_t O> A sum(A (&a)[N][O]) { A res = 0; rep(i, N) rep(j, O) res += a[i][j]; return res; } template <typename A, size_t N, size_t O, size_t P> A sum(A (&a)[N][O][P]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) res += a[i][j][k]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A sum(A (&a)[N][O][P][Q]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) res += a[i][j][k][l]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A sum(A (&a)[N][O][P][Q][R]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) res += a[i][j][k][l][m]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A sum(A (&a)[N][O][P][Q][R][S]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) rep(n, S) res += a[i][j][k][l][m][n]; return res; } //@汎用便利関数 入力 ll in() { ll ret; cin >> ret; return ret; } string sin() { string ret; cin >> ret; return ret; } template <class T> void in(T &head) { cin >> head; } template <class T, class... U> void in(T &head, U &...tail) { cin >> head; in(tail...); } #define o_din(o1, o2, o3, o4, o5, o6, name, ...) name #define din1(a) \ ll a; \ cin >> a #define din2(a, b) \ ll a, b; \ cin >> a >> b #define din3(a, b, c) \ ll a, b, c; \ cin >> a >> b >> c #define din4(a, b, c, d) \ ll a, b, c, d; \ cin >> a >> b >> c >> d #define din5(a, b, c, d, e) \ ll a, b, c, d, e; \ cin >> a >> b >> c >> d >> e #define din6(a, b, c, d, e, f) \ ll a, b, c, d, e, f; \ cin >> a >> b >> c >> d >> e >> f #define din(...) \ o_din(__VA_ARGS__, din6, din5, din4, din3, din2, din1)(__VA_ARGS__) #define o_dind(o1, o2, o3, o4, name, ...) name #define din1d(a) \ din1(a); \ a-- #define din2d(a, b) \ din2(a, b); \ a--, b-- #define din3d(a, b, c) \ din3(a, b, c); \ a--, b--, c-- #define din4d(a, b, c, d) \ din4(a, b, c, d); \ a--, b--, c--, d-- #define dind(...) o_dind(__VA_ARGS__, din4d, din3d, din2d, din1d)(__VA_ARGS__) template <class T> void out2(T &&head) { cout << head; } template <class T, class... U> void out2(T &&head, U &&...tail) { cout << head << " "; out2(tail...); } template <class T, class... U> void out(T &&head, U &&...tail) { cout << head << " "; out2(tail...); cout << "" << endl; } template <class T> void out(T &&head) { cout << head << endl; } void out() { cout << "" << endl; } #ifdef _DEBUG 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 (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) { ; } #endif template <class T> void outl(vector<T> &a, int n = inf) { rep(i, min(n, sz(a))) cout << a[i] << endl; } template <class T> void na(vector<T> &a, ll n) { a.resize(n); rep(i, n) cin >> a[i]; } #define dna(a, n) \ vi a(n); \ rep(dnai, n) cin >> a[dnai]; template <class T> void nao(vector<T> &a, ll n) { a.resize(n + 1); a[0] = 0; rep(i, n) cin >> a[i + 1]; } template <class T> void naod(vector<T> &a, ll n) { a.resize(n + 1); a[0] = 0; rep(i, n) cin >> a[i + 1], a[i + 1]--; } template <class T> void nad(vector<T> &a, ll n) { a.resize(n); rep(i, n) cin >> a[i], a[i]--; } template <class T, class U> void na2(vector<T> &a, vector<U> &b, ll n) { a.resize(n); b.resize(n); rep(i, n) cin >> a[i] >> b[i]; } #define dna2(a, b, n) \ vi a(n), b(n); \ rep(dna2i, n) cin >> a[dna2i] >> b[dna2i]; template <class T, class U> void nao2(vector<T> &a, vector<U> &b, ll n) { a.resize(n + 1); b.resize(n + 1); a[0] = b[0] = 0; rep(i, n) cin >> a[i + 1] >> b[i + 1]; } #define dna2d(a, b, n) \ vi a(n), b(n); \ rep(dna2di, n) { \ cin >> a[dna2di] >> b[dna2di]; \ a[dna2di]--, b[dna2di]--; \ } template <class T, class U> void na2d(vector<T> &a, vector<U> &b, ll n) { a.resize(n); b.resize(n); rep(i, n) cin >> a[i] >> b[i], a[i]--, b[i]--; } template <class T, class U, class W> void na3(vector<T> &a, vector<U> &b, vector<W> &c, ll n) { a.resize(n); b.resize(n); c.resize(n); rep(i, n) cin >> a[i] >> b[i] >> c[i]; } #define dna3(a, b, c, n) \ vi a(n), b(n), c(n); \ rep(dna3i, n) cin >> a[dna3i] >> b[dna3i] >> c[dna3i]; template <class T, class U, class W> void na3d(vector<T> &a, vector<U> &b, vector<W> &c, ll 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 dna3d(a, b, c, n) \ vi a(n), b(n), c(n); \ rep(dna3di, n) { \ cin >> a[dna3di] >> b[dna3di] >> c[dna3di]; \ a[dna3di]--, b[dna3di]--, c[dna3di]--; \ } template <class T, class U, class W, class X> void na4(vector<T> &a, vector<U> &b, vector<W> &c, vector<X> &d, ll n) { a.resize(n); b.resize(n); c.resize(n); d.resize(n); rep(i, n) cin >> a[i] >> b[i] >> c[i] >> d[i]; } #define dna4(a, b, c, d, n) \ vi a(n), b(n), c(n), d(n); \ rep(dna4i, n) cin >> a[dna4i] >> b[dna4i] >> c[dna4i] >> d[dna4i]; #define dna4d(a, b, c, d, n) \ vi a(n), b(n), c(n), d(n); \ rep(dna4i, n) cin >> a[dna4i] >> b[dna4i] >> c[dna4i] >> d[dna4i], \ --a[dna4i], --b[dna4i], --c[dna4i], --d[dna4i]; #define nt(a, h, w) \ resize(a, h, w); \ rep(nthi, h) rep(ntwi, w) cin >> a[nthi][ntwi]; #define ntd(a, h, w) \ resize(a, h, w); \ rep(ntdhi, h) rep(ntdwi, w) cin >> a[ntdhi][ntdwi], a[ntdhi][ntdwi]--; #define ntp(a, h, w) \ resize(a, h + 2, w + 2); \ fill(a, '#'); \ rep(ntphi, 1, h + 1) rep(ntpwi, 1, w + 1) cin >> a[ntphi][ntpwi]; //デバッグ #define sp << " " << #define debugName(VariableName) #VariableName #define deb1(x) debugName(x) << " = " << out_m(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' //@formatter:off //よく使うクラス、構造体 struct unionfind { vector<ll> par; vector<ll> siz; vector<ll> es; ll n, trees; //連結グループの数(親の種類) unionfind(ll n) : n(n), trees(n) { par.resize(n); siz.resize(n); es.resize(n); for (ll i = 0; i < n; i++) { par[i] = i; siz[i] = 1; } } ll root(ll x) { if (par[x] == x) { return x; } else { return par[x] = root(par[x]); } } bool unite(ll x, ll y) { x = root(x); y = root(y); es[x]++; if (x == y) return false; if (siz[x] > siz[y]) swap(x, y); trees--; par[x] = y; siz[y] += siz[x]; es[y] += es[x]; return true; } bool same(ll x, ll y) { return root(x) == root(y); } ll size(ll x) { return siz[root(x)]; } ll esize(ll x) { return es[root(x)]; } vi sizes() { vi cou(n); vi ret; ret.reserve(n); rep(i, n) { cou[root(i)]++; } rep(i, n) { if (cou[i]) ret.push_back(cou[i]); } return ret; } //つながりを無向グラフと見なし、xが閉路に含まれるか判定 bool close(ll x) { return esize(x) >= size(x); } vec<vi> sets() { vi ind(n, -1); ll i = 0; vvi(res, trees); rep(j, n) { ll r = root(j); if (ind[r] == -1) ind[r] = i++; res[ind[r]].push_back(j); } rep(i, trees) { ll r = root(res[i][0]); if (res[i][0] == r) continue; rep(j, 1, sz(res[i])) { if (res[i][j] == r) { swap(res[i][0], res[i][j]); break; } } } return res; } }; //@formatter:off using bll = __int128; using u32 = unsigned; using u64 = unsigned long long; using u128 = __uint128_t; std::ostream &operator<<(std::ostream &dest, __int128_t value) { std::ostream::sentry s(dest); if (s) { __uint128_t tmp = value < 0 ? -value : value; char buffer[128]; char *d = std::end(buffer); do { --d; *d = "0123456789"[tmp % 10]; tmp /= 10; } while (tmp != 0); if (value < 0) { --d; *d = '-'; } ll len = std::end(buffer) - d; if (dest.rdbuf()->sputn(d, len) != len) { dest.setstate(std::ios_base::badbit); } } return dest; } //__int128 toi128(string &s) { __int128 ret = 0; for (ll i = 0; i < //s.length(); ++i) if ('0' <= s[i] && s[i] <= '9') ret = 10 * //ret + s[i] - '0'; return ret;} //エラー 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 re() { assert(0 == 1); exit(0); } void tle() { while (inf) cout << inf << endl; } //便利関数 //テスト用 char ranc() { return (char)('a' + rand() % 26); } ll rand(ll min, ll max) { assert(min <= max); if (min >= 0 && max >= 0) { return rand() % (max + 1 - min) + min; } else if (max < 0) { return -rand(-max, -min); } else { if (rand() % 2) { return rand(0, max); } else { return -rand(0, -min); } } } vi ranv(ll n, ll min, ll max) { vi v(n); rep(i, n) v[i] = rand(min, max); return v; } str ransu(ll n) { str s; rep(i, n) s += (char)rand('A', 'Z'); return s; } str ransl(ll n) { str s; rep(i, n) s += (char)rand('a', 'z'); return s; } //単調増加 vi ranvinc(ll n, ll min, ll max) { vi v(n); bool bad = 1; while (bad) { bad = 0; v.resize(n); rep(i, n) { if (i && min > max - v[i - 1]) { bad = 1; break; } if (i) v[i] = v[i - 1] + rand(min, max - v[i - 1]); else v[i] = rand(min, max); } } return v; } //便利 汎用 void ranvlr(ll n, ll min, ll max, vi &l, vi &r) { l.resize(n); r.resize(n); rep(i, n) { l[i] = rand(min, max); r[i] = l[i] + rand(0, max - l[i]); } } 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 <class F> ll mgr(ll ok, ll ng, F f) { if (ok < ng) while (ng - ok > 1) { ll mid = (ok + ng) / 2; if (f(mid)) ok = mid; else ng = mid; } else while (ok - ng > 1) { ll mid = (ok + ng) / 2; if (f(mid)) ok = mid; else ng = mid; } return ok; } // strを整数として比較 string smax(str &a, str b) { if (sz(a) < sz(b)) { return b; } else if (sz(a) > sz(b)) { return a; } else if (a < b) return b; else return a; } // strを整数として比較 string smin(str &a, str b) { if (sz(a) > sz(b)) { return b; } else if (sz(a) < sz(b)) { return a; } else if (a > b) return b; else return a; } template <typename W, typename T> ll find(vector<W> &a, const T key) { rep(i, sz(a)) if (a[i] == key) return i; return -1; } template <typename W, typename T> P find(vector<vector<W>> &a, const T key) { rep(i, sz(a)) rep(j, sz(a[0])) if (a[i][j] == key) return mp(i, j); return mp(-1, -1); } template <typename W, typename U> T find(vector<vector<vector<W>>> &a, const U key) { rep(i, sz(a)) rep(j, sz(a[0])) rep(k, sz(a[0][0])) if (a[i][j][k] == key) return mt(i, j, k); return mt(-1, -1, -1); } template <typename W, typename T> ll count2(W &a, const T k) { return a == k; } template <typename W, typename T> ll count2(vector<W> &a, const T k) { ll ret = 0; fora(v, a) ret += count2(v, k); return ret; } template <typename W, typename T> ll count(vector<W> &a, const T k) { ll ret = 0; fora(v, a) ret += count2(v, k); return ret; } ll count(str &a, str k) { ll ret = 0, len = k.length(); auto pos = a.find(k); while (pos != string::npos) pos = a.find(k, pos + len), ++ret; return ret; } vi count(str &a) { vi cou(26); char c = 'a'; if ('A' <= a[0] && a[0] <= 'Z') c = 'A'; rep(i, sz(a))++ cou[a[i] - c]; return cou; } #define couif count_if // algorythm 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)); } template <class U> void rev(vector<vector<U>> &a) { vector<vector<U>> b(sz(a[0]), vector<U>(sz(a))); rep(h, sz(a)) rep(w, sz(a[0])) b[w][h] = a[h][w]; a = b; } void rev(string &a) { reverse(all(a)); } constexpr ll p10[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000ll, 100000000000ll, 1000000000000ll, 10000000000000ll, 100000000000000ll, 1000000000000000ll, 10000000000000000ll, 100000000000000000ll, 1000000000000000000ll}; ll get(ll a, ll keta) { return (a / (ll)pow(10, keta)) % 10; } ll keta(ll v) { if (v < p10[9]) { if (v < p10[4]) { if (v < p10[2]) { if (v < p10[1]) return 1; else return 2; } else { if (v < p10[3]) return 3; else return 4; } } else { if (v < p10[7]) { if (v < p10[5]) return 5; else if (v < p10[6]) return 6; else return 7; } else { if (v < p10[8]) return 8; else return 9; } } } else { if (v < p10[13]) { if (v < p10[11]) { if (v < p10[10]) return 10; else return 11; } else { if (v < p10[12]) return 12; else return 13; } } else { if (v < p10[15]) { if (v < p10[14]) return 14; else if (v < p10[15]) return 15; else return 16; } else { if (v < p10[17]) return 17; else return 18; } } } } ll dsum(ll v, ll sin = 10) { ll ret = 0; for (; v; v /= sin) ret += v % sin; return ret; } struct sint { ll v; sint(ll v) : v(v) {} operator ll() { return v; } //下からi番目 ll operator[](ll i) { return (v / p10[i]) % 10; } ll back(ll i) { return operator[](i); } //上からi番目 ll top(ll i) { ll len = keta(v); return operator[](len - 1 - i); } //先頭からi番目にセット ll settop(ll i, ll k) { ll len = keta(v); return set(len - 1 - i, k); } ll set(ll i, ll k) { if (i < 0) return settop(abs(i) - 1, k); return v += p10[i] * (k - (v / p10[i]) % 10); } ll add(ll i, ll k = 1) { return v += p10[i] * k; } ll addtop(ll i, ll k = 1) { return v += p10[keta(v) - i - 1] * k; } ll dec(ll i, ll k = 1) { return v -= p10[i] * k; } ll dectop(ll i, ll k = 1) { return v -= p10[keta(v) - i - 1] * k; } #define op(t, o) \ template <class T> t operator o(T r) { return v o r; } op(ll, +=); op(ll, -=); op(ll, *=); op(ll, /=); op(ll, %=); op(ll, +); op(ll, -); op(ll, *); op(ll, /); op(ll, %); op(bool, ==); op(bool, !=); op(bool, <); op(bool, <=); op(bool, >); op(bool, >=); #undef op template <class T> ll operator<<=(T r) { return v *= p10[r]; } template <class T> ll operator<<(T r) { return v * p10[r]; } template <class T> ll operator>>=(T r) { return v /= p10[r]; } template <class T> ll operator>>(T r) { return v / p10[r]; } }; ll mask10(ll v) { return p10[v] - 1; } //変換系 template <class T> auto keys(T &a) { vector<decltype((a.begin())->fi)> res; for (auto &&k : a) res.push_back(k.fi); return res; } template <class T> auto values(T &a) { vector<decltype((a.begin())->se)> res; for (auto &&k : a) res.push_back(k.se); return res; } template <class T, class U> bool chma(T &a, const U &b) { if (a < b) { a = b; return true; } return false; } template <class T, class U> bool chmi(T &a, const U &b) { if (b < a) { a = b; return true; } return false; } template <class T> T min(T a, signed b) { return a < b ? a : b; } template <class T> T max(T a, signed b) { return a < b ? b : a; } template <class T> T min(T a, T b, T c) { return a >= b ? b >= c ? c : b : a >= c ? c : a; } template <class T> T max(T a, T b, T c) { return a <= b ? b <= c ? c : b : a <= c ? c : a; } template <class T> T min(vector<T> &a) { return *min_element(all(a)); } template <class T> T mini(vector<T> &a) { return min_element(all(a)) - a.begin(); } template <class T> T min(vector<T> &a, ll n) { return *min_element(a.begin(), a.begin() + min(n, sz(a))); } template <class T> T min(vector<T> &a, ll s, ll n) { return *min_element(a.begin() + s, a.begin() + min(n, sz(a))); } template <class T> T max(vector<T> &a) { return *max_element(all(a)); } template <class T, class U> T max(vector<T> &a, vector<U> &b) { return max(*max_element(all(a)), *max_element(all(b))); } template <class T> T maxi(vector<T> &a) { return max_element(all(a)) - a.begin(); } template <class T> T max(vector<T> &a, ll n) { return *max_element(a.begin(), a.begin() + min(n, sz(a))); } template <class T> T max(vector<T> &a, ll s, ll n) { return *max_element(a.begin() + s, a.begin() + min(n, sz(a))); } template <typename A, size_t N> A max(A (&a)[N]) { A res = a[0]; rep(i, N) res = max(res, a[i]); return res; } template <typename A, size_t N, size_t O> A max(A (&a)[N][O]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P> A max(A (&a)[N][O][P]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A max(A (&a)[N][O][P][Q], const T &v) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A max(A (&a)[N][O][P][Q][R]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A max(A (&a)[N][O][P][Q][R][S]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N> A min(A (&a)[N]) { A res = a[0]; rep(i, N) res = min(res, a[i]); return res; } template <typename A, size_t N, size_t O> A min(A (&a)[N][O]) { A res = min(a[0]); rep(i, N) res = min(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P> A min(A (&a)[N][O][P]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A min(A (&a)[N][O][P][Q], const T &v) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A min(A (&a)[N][O][P][Q][R]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A min(A (&a)[N][O][P][Q][R][S]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <class T> T sum(vector<T> &v, ll s, ll t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += v[i]; return ret; } template <class T> T sum(vector<T> &v, ll t = inf) { return sum(v, 0, t); } template <class T> T sum(vector<vector<T>> &v) { T ret = 0; rep(i, sz(v)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<T>>> &v) { T ret = 0; rep(i, sz(v)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<vector<T>>>> &v) { T ret = 0; rep(i, sz(v)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<vector<vector<T>>>>> &v) { T ret = 0; rep(i, sz(v)) ret += sum(v[i]); return ret; } template <class T> auto sum(priority_queue<T, vector<T>, greater<T>> &r) { auto q = r; T ret = 0; while (sz(q)) { ret += q.top(); q.pop(); } return ret; } template <class T> auto sum(priority_queue<T> &r) { auto q = r; T ret = 0; while (sz(q)) { ret += q.top(); q.pop(); } return ret; } // template<class T, class U, class... W> auto sumn(vector<T> &v, U head, W... // tail) { auto ret = sum(v[0], tail...); rep(i, 1, min(sz(v), head))ret += // sum(v[i], tail...); return ret;} void clear(PQ &q) { q = PQ(); } template <class T> void clear(queue<T> &q) { while (q.size()) q.pop(); } template <class T> T *negarr(ll size) { T *body = (T *)malloc((size * 2 + 1) * sizeof(T)); return body + size; } template <class T> T *negarr2(ll h, ll w) { double **dummy1 = new double *[2 * h + 1]; double *dummy2 = new double[(2 * h + 1) * (2 * w + 1)]; dummy1[0] = dummy2 + w; for (ll i = 1; i <= 2 * h + 1; ++i) { dummy1[i] = dummy1[i - 1] + 2 * w + 1; } double **a = dummy1 + h; return a; } // 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> void inc(T &a, U v = 1) { a += v; } template <class T, class U> void inc(vector<T> &a, U v = 1) { for (auto &u : a) inc(u, v); } template <class T, class U> void dec(T &a, U v = 1) { a -= v; } template <class T, class U> void dec(vector<T> &a, U v = 1) { for (auto &u : a) dec(u, v); } template <class U> void dec(string &a, U v = 1) { for (auto &u : a) dec(u, v); } template <class T> void dec(vector<T> &a) { for (auto &u : a) dec(u, 1); } template <class T, class U> void dec(vector<T> &a, vector<U> &b) { for (auto &u : a) dec(u, 1); for (auto &u : b) dec(u, 1); } template <class T, class U, class W> void dec(vector<T> &a, vector<U> &b, vector<W> &c) { for (auto &u : a) dec(u, 1); for (auto &u : b) dec(u, 1); for (auto &u : c) dec(u, 1); } bool ins(ll h, ll w, ll H, ll W) { return h >= 0 && w >= 0 && h < H && w < W; } bool ins(ll l, ll v, ll r) { return l <= v && v < r; } template <class T> bool ins(vector<T> &a, ll i, ll j = 0) { return ins(0, i, sz(a)) && ins(0, j, sz(a)); } 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() //添え字を返す template <class F> ll goldd_l(ll left, ll right, F calc) { double GRATIO = 1.6180339887498948482045868343656; ll lm = left + (ll)((right - left) / (GRATIO + 1.0)); ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); ll fl = calc(lm); ll fr = calc(rm); while (right - left > 10) { if (fl < fr) { right = rm; rm = lm; fr = fl; lm = left + (ll)((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } ll minScore = MAX(ll); ll resIndex = left; for (ll i = left; i < right + 1; ++i) { ll score = calc(i); if (minScore > score) { minScore = score; resIndex = i; } } return resIndex; } template <class F> ll goldt_l(ll left, ll right, F calc) { double GRATIO = 1.6180339887498948482045868343656; ll lm = left + (ll)((right - left) / (GRATIO + 1.0)); ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); ll fl = calc(lm); ll fr = calc(rm); while (right - left > 10) { if (fl > fr) { right = rm; rm = lm; fr = fl; lm = left + (ll)((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } if (left > right) { ll l = left; left = right; right = l; } ll maxScore = MIN(ll); ll resIndex = left; for (ll i = left; i < right + 1; ++i) { ll score = calc(i); if (maxScore < score) { maxScore = score; resIndex = i; } } return resIndex; } /*loopは200にすればおそらく大丈夫 余裕なら300に*/ template <class F> dou goldd_d(dou left, dou right, F calc, ll loop = 200) { dou GRATIO = 1.6180339887498948482045868343656; dou lm = left + ((right - left) / (GRATIO + 1.0)); dou rm = lm + ((right - lm) / (GRATIO + 1.0)); dou fl = calc(lm); dou fr = calc(rm); /*200にすればおそらく大丈夫*/ /*余裕なら300に*/ ll k = 141; loop++; while (--loop) { if (fl < fr) { right = rm; rm = lm; fr = fl; lm = left + ((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + ((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } return left; } template <class F> dou goldt_d(dou left, dou right, F calc, ll loop = 200) { double GRATIO = 1.6180339887498948482045868343656; dou lm = left + ((right - left) / (GRATIO + 1.0)); dou rm = lm + ((right - lm) / (GRATIO + 1.0)); dou fl = calc(lm); dou fr = calc(rm); loop++; while (--loop) { if (fl > fr) { right = rm; rm = lm; fr = fl; lm = left + ((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + ((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } return left; } // l ~ rを複数の区間に分割し、極致を与えるiを返す time-20 msまで探索 template <class F> ll goldd_ls(ll l, ll r, F calc, ll time = 2000) { auto lim = milliseconds(time - 20); ll mini = 0, minv = MAX(ll); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/ ll nl = l; ll nr = l + haba; rep(i, k) { ll ni = goldd_l(nl, nr, calc); if (chmi(minv, calc(ni))) mini = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return mini; } template <class F> ll goldt_ls(ll l, ll r, F calc, ll time = 2000) { auto lim = milliseconds(time - 20); ll maxi = 0, maxv = MIN(ll); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/ ll nl = l; ll nr = l + haba; rep(i, k) { ll ni = goldt_l(nl, nr, calc); if (chma(maxv, calc(ni))) maxi = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return maxi; } template <class F> dou goldd_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を持つ*/ auto lim = milliseconds(time - 20); dou mini = 0, minv = MAX(dou); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); dou haba = (r - l) / k; dou nl = l; dou nr = l + haba; rep(i, k) { dou ni = goldd_d(nl, nr, calc); if (chmi(minv, calc(ni))) mini = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return mini; } template <class F> dou goldt_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を残している*/ auto lim = milliseconds(time - 20); dou maxi = 0, maxv = MIN(dou); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); dou haba = (r - l) / k; dou nl = l; dou nr = l + haba; rep(i, k) { dou ni = goldt_d(nl, nr, calc); if (chma(maxv, calc(ni))) maxi = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return maxi; } template <class T> T min(vector<vector<T>> &a) { T res = MAX(T); rep(i, a.size()) chmi(res, *min_element(all(a[i]))); return res; } template <class T> T max(vector<vector<T>> &a) { T res = MIN(T); rep(i, a.size()) chma(res, *max_element(all(a[i]))); return res; } constexpr bool bget(ll m, ll keta) { return (m >> keta) & 1; } ll bget(ll m, ll keta, ll sinsuu) { m /= (ll)pow(sinsuu, keta); return m % sinsuu; } ll bit(ll n) { return (1LL << (n)); } ll bit(ll n, ll sinsuu) { return (ll)pow(sinsuu, n); } ll mask(ll n) { return (1ll << n) - 1; } #define bcou __builtin_popcountll //最下位ビット ll lbit(ll n) { return n & -n; } //最上位ビット ll hbit(ll n) { n |= (n >> 1); n |= (n >> 2); n |= (n >> 4); n |= (n >> 8); n |= (n >> 16); n |= (n >> 32); return n - (n >> 1); } ll hbitk(ll n) { ll k = 0; rer(i, 5) { ll a = k + (1ll << i); ll b = 1ll << a; if (b <= n) k += 1ll << i; } return k; } //初期化は0を渡す ll nextComb(ll &mask, ll n, ll r) { if (!mask) return mask = (1LL << r) - 1; ll x = mask & -mask; /*最下位の1*/ ll y = mask + x; /*連続した下の1を繰り上がらせる*/ ll res = ((mask & ~y) / x >> 1) | y; if (bget(res, n)) return mask = 0; else return mask = res; } // n桁以下でビットがr個立っているもののvectorを返す vi bitCombList(ll n, ll r) { vi res; ll m = 0; while (nextComb(m, n, r)) { res.push_back(m); } return res; } // masの立ってるindexを見る #define forbit(i, mas) \ for (int forbitj = lbit(mas), forbitm = mas, i = log2(forbitj); forbitm; \ forbitm = forbitj ? forbitm ^ forbitj : 0, forbitj = lbit(forbitm), \ i = log2(forbitj)) char itoal(ll i) { return 'a' + i; } char itoaL(ll i) { return 'A' + i; } ll altoi(char c) { if ('A' <= c && c <= 'Z') return c - 'A'; return c - 'a'; } ll ctoi(char c) { return c - '0'; } char itoc(ll i) { return i + '0'; } ll vtoi(vi &v) { ll res = 0; if (sz(v) > 18) { debugline("vtoi"); deb(sz(v)); ole(); } rep(i, sz(v)) { res *= 10; res += v[i]; } return res; } vi itov(ll i) { vi res; while (i) { res.push_back(i % 10); i /= 10; } rev(res); return res; } vi stov(string &a) { ll n = sz(a); vi ret(n); rep(i, n) { ret[i] = a[i] - '0'; } return ret; } //基準を満たさないものは0になる vi stov(string &a, char one) { ll n = sz(a); vi ret(n); rep(i, n) ret[i] = a[i] == one; return ret; } vector<vector<ll>> ctoi(vector<vector<char>> s, char c) { ll n = sz(s), m = sz(s[0]); vector<vector<ll>> res(n, vector<ll>(m)); rep(i, n) rep(j, m) res[i][j] = s[i][j] == c; return res; } #define unique(v) v.erase(unique(v.begin(), v.end()), v.end()); //[i] := i番として圧縮されたものを返す vi compress(vi &a) { vi b; ll len = a.size(); for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { a[i] = lower_bound(all(b), a[i]) - b.begin(); } ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, umap<ll, ll> &map) { vi b; ll len = a.size(); for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { ll v = a[i]; a[i] = lower_bound(all(b), a[i]) - b.begin(); map[v] = a[i]; } ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, vi &r) { vi b; ll len = a.size(); fora(v, a) b.push_back(v); fora(v, r) b.push_back(v); sort(b); unique(b); for (ll i = 0; i < len; ++i) a[i] = lower_bound(all(b), a[i]) - b.begin(); for (ll i = 0; i < sz(r); ++i) r[i] = lower_bound(all(b), r[i]) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, vi &r, vi &s) { vi b; ll len = a.size(); fora(v, a) b.push_back(v); fora(v, r) b.push_back(v); fora(v, s) b.push_back(v); sort(b); unique(b); for (ll i = 0; i < len; ++i) a[i] = lower_bound(all(b), a[i]) - b.begin(); for (ll i = 0; i < sz(r); ++i) r[i] = lower_bound(all(b), r[i]) - b.begin(); for (ll i = 0; i < sz(s); ++i) r[i] = lower_bound(all(b), s[i]) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vector<vi> &a) { vi b; fora(vv, a) fora(v, vv) b.push_back(v); sort(b); unique(b); fora(vv, a) fora(v, vv) v = lower_bound(all(b), v) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vector<vector<vi>> &a) { vi b; fora(vvv, a) fora(vv, vvv) fora(v, vv) b.push_back(v); sort(b); unique(b); fora(vvv, a) fora(vv, vvv) fora(v, vv) v = lower_bound(all(b), v) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } void compress(ll a[], ll len) { vi b; for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { a[i] = lower_bound(all(b), a[i]) - b.begin(); } } //要素が見つからなかったときに困る #define binarySearch(a, v) (binary_search(all(a), v)) #define lowerIndex(a, v) (lower_bound(all(a), v) - a.begin()) #define lowerBound(a, v) (*lower_bound(all(a), v)) #define upperIndex(a, v) (upper_bound(all(a), v) - a.begin()) #define upperBound(a, v) (*upper_bound(all(a), v)) #define rlowerIndex(a, v) (upper_bound(all(a), v) - a.begin() - 1) #define rlowerBound(a, v) *(--(upper_bound(all(a), v))) #define rupperIndex(a, v) (lower_bound(all(a), v) - a.begin() - 1) #define rupperBound(a, v) *(--(lower_bound(all(a), v))) #define next2(a) next(next(a)) #define prev2(a) prev(prev(a)) //狭義の単調増加列 長さを返す template <class T> int lis(vector<T> &a) { int n = sz(a); vi tail(n + 1, MAX(T)); rep(i, n) { int id = lowerIndex(tail, a[i]); /**/ tail[id] = a[i]; } return lowerIndex(tail, MAX(T)); } template <class T> int lis_eq(vector<T> &a) { int n = sz(a); vi tail(n + 1, MAX(T)); rep(i, n) { int id = upperIndex(tail, a[i]); /**/ tail[id] = a[i]; } return lowerIndex(tail, MAX(T)); } // iteratorを返す // valueが1以上の物を返す 0は見つけ次第削除 // vを減らす場合 (*it).se--でいい template <class T, class U, class V> auto lower_map(map<T, U> &m, V k) { auto ret = m.lower_bound(k); while (ret != m.end() && (*ret).second == 0) { ret = m.erase(ret); } return ret; } template <class T, class U, class V> auto upper_map(map<T, U> &m, V k) { auto ret = m.upper_bound(k); while (ret != m.end() && (*ret).second == 0) { ret = m.erase(ret); } return ret; } //存在しなければエラー template <class T, class U, class V> auto rlower_map(map<T, U> &m, V k) { auto ret = upper_map(m, k); assert(ret != m.begin()); ret--; while (1) { if ((*ret).second != 0) break; assert(ret != m.begin()); auto next = ret; --next; m.erase(ret); ret = next; } return ret; } template <class T, class U, class V> auto rupper_map(map<T, U> &m, V k) { auto ret = lower_map(m, k); assert(ret != m.begin()); ret--; while (1) { if ((*ret).second != 0) break; assert(ret != m.begin()); auto next = ret; --next; m.erase(ret); ret = next; } return ret; } template <class T> void fin(T s) { cout << s << endl, exit(0); } //便利 数学 math ll mod(ll a, ll m) { return (a % m + m) % m; } ll pow(ll a) { return a * a; }; ll fact(ll v) { return v <= 1 ? 1 : v * fact(v - 1); } dou factd(int v) { static vd fact(2, 1); if (sz(fact) <= v) { rep(i, sz(fact), v + 1) { fact.push_back(fact.back() * i); } } return fact[v]; } ll comi(ll n, ll r) { assert(n < 100); static vvi(pas, 100, 100); if (pas[0][0]) return pas[n][r]; pas[0][0] = 1; rep(i, 1, 100) { pas[i][0] = 1; rep(j, 1, i + 1) pas[i][j] = pas[i - 1][j - 1] + pas[i - 1][j]; } return pas[n][r]; } double comd2(ll n, ll r) { static vvd(comb, 2020, 2020); if (comb[0][0] == 0) { comb[0][0] = 1; rep(i, 2000) { comb[i + 1][0] = 1; rep(j, 1, i + 2) { comb[i + 1][j] = comb[i][j] + comb[i][j - 1]; } } } return comb[n][r]; } double comd(int n, int r) { if (r < 0 || r > n) return 0; if (n < 2020) return comd2(n, r); static vd fact(2, 1); if (sz(fact) <= n) { rep(i, sz(fact), n + 1) { fact.push_back(fact.back() * i); } } return fact[n] / fact[n - r] / fact[r]; } ll gcd(ll a, ll b) { while (b) a %= b, swap(a, b); return abs(a); } ll gcd(vi b) { ll res = b[0]; rep(i, 1, sz(b)) res = gcd(b[i], res); return res; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll lcm(vi a) { ll res = a[0]; rep(i, 1, sz(a)) res = lcm(a[i], res); return res; } ll ceil(ll a, ll b) { if (b == 0) { debugline("ceil"); deb(a, b); ole(); return -1; } else if (a < 0) { return 0; } else { return (a + b - 1) / b; } } ll lower_remi__bx_a(ll kei, ll rem, ll x) { if (rem >= x) return 0; return (x - rem + kei - 1) / kei; } ll lower_remv__bx_a(ll kei, ll rem, ll x) { if (rem >= x) return rem; return (x - rem + kei - 1) / kei * kei + rem; } ll upper_remi__bx_a(ll kei, ll rem, ll x) { if (rem > x) return 0; return (x - rem + kei) / kei; } ll upper_remv__bx_a(ll kei, ll rem, ll x) { if (rem > x) return rem; return (x - rem + kei) / kei * kei + rem; } // v * v >= aとなる最小のvを返す 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) >> 1; } ll sig(ll s, ll t) { return ((s + t) * (t - s + 1)) >> 1; } //機能拡張 template <class T, class U> void operator+=(pair<T, U> &a, pair<T, U> &b) { a.fi += b.fi; a.se += b.se; } template <class T, class U> pair<T, U> operator+(pair<T, U> &a, pair<T, U> &b) { return pair<T, U>(a.fi + b.fi, a.se + b.se); } template <typename CharT, typename Traits, typename Alloc> basic_string<CharT, Traits, Alloc> operator+(const basic_string<CharT, Traits, Alloc> &lhs, const int rv) { basic_string<CharT, Traits, Alloc> str(lhs); str.append(to_string(rv)); return str; } template <typename CharT, typename Traits, typename Alloc> void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const int rv) { lhs += to_string(rv); } template <typename CharT, typename Traits, typename Alloc> basic_string<CharT, Traits, Alloc> operator+(const basic_string<CharT, Traits, Alloc> &lhs, const signed rv) { basic_string<CharT, Traits, Alloc> str(lhs); str.append(to_string(rv)); return str; } template <typename CharT, typename Traits, typename Alloc> void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const signed rv) { lhs += to_string(rv); } template <class T, class U> void operator+=(queue<T> &a, U v) { a.push(v); } template <class T, class U> void operator+=(deque<T> &a, U v) { a.push_back(v); } template <class T> priority_queue<T, vector<T>, greater<T>> & operator+=(priority_queue<T, vector<T>, greater<T>> &a, vector<T> &v) { fora(d, v) a.push(d); return a; } template <class T, class U> priority_queue<T, vector<T>, greater<T>> & operator+=(priority_queue<T, vector<T>, greater<T>> &a, U v) { a.push(v); return a; } template <class T, class U> priority_queue<T> &operator+=(priority_queue<T> &a, U v) { a.push(v); return a; } template <class T> set<T> &operator+=(set<T> &a, vector<T> v) { fora(d, v) a.insert(d); return a; } template <class T, class U> auto operator+=(set<T> &a, U v) { return a.insert(v); } template <class T, class U> auto operator-=(set<T> &a, U v) { return a.erase(v); } template <class T, class U> auto operator+=(mset<T> &a, U v) { return a.insert(v); } template <class T, class U> set<T, greater<T>> &operator+=(set<T, greater<T>> &a, U v) { a.insert(v); return a; } template <class T, class U> vector<T> &operator+=(vector<T> &a, U v) { a.push_back(v); return a; } template <class T, class U> vector<T> operator+(const vector<T> &a, U v) { vector<T> ret = a; ret += v; return ret; } template <class T, class U> vector<T> operator+(U v, const vector<T> &a) { vector<T> ret = a; ret.insert(ret.begin(), v); return ret; } template <class T> vector<T> operator+(vector<T> a, vector<T> b) { vector<T> ret; ret = a; fora(v, b) ret += v; return ret; } template <class T> vector<T> &operator+=(vector<T> &a, vector<T> &b) { fora(v, b) a += v; return a; } template <class T> vector<T> &operator-=(vector<T> &a, vector<T> &b) { if (sz(a) != sz(b)) { debugline("vector<T> operator-="); deb(a); deb(b); exit(0); } rep(i, sz(a)) a[i] -= b[i]; return a; } template <class T> vector<T> operator-(vector<T> &a, vector<T> &b) { if (sz(a) != sz(b)) { debugline("vector<T> operator-"); deb(a); deb(b); ole(); } vector<T> res(sz(a)); rep(i, sz(a)) res[i] = a[i] - b[i]; return res; } template <class T, class U> vector<T> operator*(vector<T> &a, U b) { vector<T> ret; fora(v, a) ret += v * b; return ret; } template <class T, class U> vector<T> operator/(vector<T> &a, U b) { vector<T> ret; fora(v, a) ret += v / b; return ret; } template <class T, class U> vector<T> operator*=(vector<T> &a, U b) { fora(v, a) v *= b; return a; } template <class T, class U> vector<T> operator/=(vector<T> &a, U b) { fora(v, a) v /= b; return a; } template <typename T> void erase(vector<T> &v, unsigned ll i) { v.erase(v.begin() + i); } template <typename T> void erase(vector<T> &v, unsigned ll s, unsigned ll e) { v.erase(v.begin() + s, v.begin() + e); } template <class T, class U> void erase(map<T, U> &m, ll okl, ll ngr) { m.erase(m.lower_bound(okl), m.lower_bound(ngr)); } template <class T> void erase(set<T> &m, ll okl, ll ngr) { m.erase(m.lower_bound(okl), m.lower_bound(ngr)); } template <typename T> void erasen(vector<T> &v, unsigned ll s, unsigned ll n) { v.erase(v.begin() + s, v.begin() + s + n); } template <typename T, typename U> void insert(vector<T> &v, unsigned ll i, U t) { v.insert(v.begin() + i, t); } template <typename T, typename U> void push_front(vector<T> &v, U t) { v.insert(v.begin(), t); } template <typename T, typename U> void insert(vector<T> &v, unsigned ll i, vector<T> list) { for (auto &&va : list) v.insert(v.begin() + i++, va); } template <typename T> void insert(set<T> &v, vector<T> list) { for (auto &&va : list) v.insert(va); } vector<string> split(const string a, const char deli) { string b = a + deli; ll l = 0, r = 0, n = b.size(); vector<string> res; rep(i, n) { if (b[i] == deli) { r = i; if (l < r) res.push_back(b.substr(l, r - l)); l = i + 1; } } return res; } vector<string> split(const string a, const string deli) { vector<string> res; ll kn = sz(deli); std::string::size_type Pos(a.find(deli)); ll l = 0; while (Pos != std::string::npos) { if (Pos - l) res.push_back(a.substr(l, Pos - l)); l = Pos + kn; Pos = a.find(deli, Pos + kn); } if (sz(a) - l) res.push_back(a.substr(l, sz(a) - l)); return res; } 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 <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)}; } //@formatter:off template <typename T> T minv(T a, T m); template <typename T> T minv(T a); template <typename T> class Modular { public: using Type = typename decay<decltype(T::value)>::type; constexpr Modular() : value() {} template <typename U> Modular(const U &x) { value = normalize(x); } template <typename U> static Type normalize(const U &x) { Type v; if (-mod() <= x && x < mod()) v = static_cast<Type>(x); else v = static_cast<Type>(x % mod()); if (v < 0) v += mod(); return v; } const Type &operator()() const { return value; } template <typename U> explicit operator U() const { return static_cast<U>(value); } constexpr static Type mod() { return T::value; } Modular &operator+=(const Modular &other) { if ((value += other.value) >= mod()) value -= mod(); return *this; } Modular &operator-=(const Modular &other) { if ((value -= other.value) < 0) value += mod(); return *this; } template <typename U> Modular &operator+=(const U &other) { return *this += Modular(other); } template <typename U> Modular &operator-=(const U &other) { return *this -= Modular(other); } Modular &operator++() { return *this += 1; } Modular &operator--() { return *this -= 1; } Modular operator++(signed) { Modular result(*this); *this += 1; return result; } Modular operator--(signed) { Modular result(*this); *this -= 1; return result; } Modular operator-() const { return Modular(-value); } template <typename U = T> typename enable_if<is_same<typename Modular<U>::Type, signed>::value, Modular>::type & operator*=(const Modular &rhs) { #ifdef _WIN32 uint64_t x = static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value); uint32_t xh = static_cast<uint32_t>(x >> 32), xl = static_cast<uint32_t>(x), d, m; asm("divl %4; \n\t" : "=a"(d), "=d"(m) : "d"(xh), "a"(xl), "r"(mod())); value = m; #else value = normalize(static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value)); #endif return *this; } template <typename U = T> typename enable_if<is_same<typename Modular<U>::Type, int64_t>::value, Modular>::type & operator*=(const Modular &rhs) { int64_t q = static_cast<int64_t>(static_cast<double>(value) * rhs.value / mod()); value = normalize(value * rhs.value - q * mod()); return *this; } template <typename U = T> typename enable_if<!is_integral<typename Modular<U>::Type>::value, Modular>::type & operator*=(const Modular &rhs) { value = normalize(value * rhs.value); return *this; } Modular &operator/=(const Modular &other) { return *this *= Modular(minv(other.value)); } template <typename U> friend bool operator==(const Modular<U> &lhs, const Modular<U> &rhs); template <typename U> friend bool operator<(const Modular<U> &lhs, const Modular<U> &rhs); template <typename U> friend std::istream &operator>>(std::istream &stream, Modular<U> &number); operator int() { return value; } private: Type value; }; template <typename T> bool operator==(const Modular<T> &lhs, const Modular<T> &rhs) { return lhs.value == rhs.value; } template <typename T, typename U> bool operator==(const Modular<T> &lhs, U rhs) { return lhs == Modular<T>(rhs); } template <typename T, typename U> bool operator==(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) == rhs; } template <typename T> bool operator!=(const Modular<T> &lhs, const Modular<T> &rhs) { return !(lhs == rhs); } template <typename T, typename U> bool operator!=(const Modular<T> &lhs, U rhs) { return !(lhs == rhs); } template <typename T, typename U> bool operator!=(U lhs, const Modular<T> &rhs) { return !(lhs == rhs); } template <typename T> bool operator<(const Modular<T> &lhs, const Modular<T> &rhs) { return lhs.value < rhs.value; } template <typename T> Modular<T> operator+(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) += rhs; } template <typename T, typename U> Modular<T> operator+(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) += rhs; } template <typename T, typename U> Modular<T> operator+(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) += rhs; } template <typename T> Modular<T> operator-(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) -= rhs; } template <typename T, typename U> Modular<T> operator-(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) -= rhs; } template <typename T, typename U> Modular<T> operator-(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) -= rhs; } template <typename T> Modular<T> operator*(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) *= rhs; } template <typename T, typename U> Modular<T> operator*(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) *= rhs; } template <typename T, typename U> Modular<T> operator*(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) *= rhs; } template <typename T> Modular<T> operator/(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) /= rhs; } template <typename T, typename U> Modular<T> operator/(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) /= rhs; } template <typename T, typename U> Modular<T> operator/(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) /= rhs; } constexpr signed MOD = // 998244353; 1e9 + 7; // MOD using mint = Modular<std::integral_constant<decay<decltype(MOD)>::type, MOD>>; constexpr int mint_len = 1400001; vi fac, finv, inv; vi p2; mint com(int n, int r) { if (r < 0 || r > n) return 0; return mint(finv[r] * fac[n] % MOD * finv[n - r]); } mint pom(int n, int r) { /* if (!sz(fac)) com(0, -1);*/ if (r < 0 || r > n) return 0; return mint(fac[n] * finv[n - 1]); } mint npr(int n, int r) { /* if (!sz(fac)) com(0, -1);*/ if (r < 0 || r > n) return 0; return mint(fac[n] * finv[n - r]); } int nprin(int n, int r) { /* if (!sz(fac)) com(0, -1);*/ if (r < 0 || r > n) return 0; return fac[n] * finv[n - r] % MOD; } int icom(int n, int r) { const int NUM_ = 1400001; static ll fac[NUM_ + 1], finv[NUM_ + 1], inv[NUM_ + 1]; if (fac[0] == 0) { inv[1] = fac[0] = finv[0] = 1; for (int i = 2; i <= NUM_; ++i) inv[i] = inv[MOD % i] * (MOD - MOD / i) % MOD; for (int i = 1; i <= NUM_; ++i) fac[i] = fac[i - 1] * i % MOD, finv[i] = finv[i - 1] * inv[i] % MOD; } if (r < 0 || r > n) return 0; return ((finv[r] * fac[n] % MOD) * finv[n - r]) % MOD; } #define ncr com #define ncri icom // n個の場所にr個の物を置く mint nhr(int n, int r) { return com(n + r - 1, r); } mint hom(int n, int r) { return com(n + r - 1, r); } int nhri(int n, int r) { return icom(n + r - 1, r); } template <typename T> T minv(T a, T m) { T u = 0, v = 1; while (a != 0) { T t = m / a; m -= t * a; swap(a, m); u -= t * v; swap(u, v); } assert(m == 1); return u; } template <typename T> T minv(T a) { if (a < mint_len) return inv[a]; T u = 0, v = 1; T m = MOD; while (a != 0) { T t = m / a; m -= t * a; swap(a, m); u -= t * v; swap(u, v); } assert(m == 1); return u; } template <typename T, typename U> Modular<T> mpow(const Modular<T> &a, const U &b) { assert(b >= 0); int x = a(), res = 1; U p = b; while (p > 0) { if (p & 1) (res *= x) %= MOD; (x *= x) %= MOD; p >>= 1; } return res; } template <typename T, typename U, typename V> mint mpow(const T a, const U b, const V m = MOD) { assert(b >= 0); int x = a, res = 1; U p = b; while (p > 0) { if (p & 1) (res *= x) %= m; (x *= x) %= m; p >>= 1; } return res; } template <typename T, typename U> mint mpow(const T a, const U b) { assert(b >= 0); int x = a, res = 1; U p = b; while (p > 0) { if (p & 1) (res *= x) %= MOD; (x *= x) %= MOD; p >>= 1; } return res; } template <typename T, typename U, typename V> int mpowi(const T &a, const U &b, const V &m = MOD) { assert(b >= 0); int x = a, res = 1; U p = b; while (p > 0) { if (p & 1) (res *= x) %= m; (x *= x) %= m; p >>= 1; } return res; } template <typename T> string to_string(const Modular<T> &number) { return to_string(number()); } string yuri(const mint &a) { stringstream st; rep(i, 300) { rep(j, 300) { if ((mint)i / j == a) { st << i << " / " << j; return st.str(); } } } rep(i, 1000) { rep(j, 1000) { if ((mint)i / j == a) { st << i << " / " << j; return st.str(); } } } return st.str(); } template <typename T> std::ostream &operator<<(std::ostream &stream, const Modular<T> &number) { stream << number(); #ifdef _DEBUG // stream << " -> " << yuri(number); #endif return stream; } //@formatter:off template <typename T> std::istream &operator>>(std::istream &stream, Modular<T> &number) { typename common_type<typename Modular<T>::Type, int64_t>::type x; stream >> x; number.value = Modular<T>::normalize(x); return stream; } using PM = pair<mint, mint>; using vm = vector<mint>; using mapm = map<int, mint>; using umapm = umap<int, mint>; #define vvm(...) \ o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(mint, __VA_ARGS__) #define vnm(name, ...) auto name = make_v<mint>(__VA_ARGS__) //@formatter:on // nhr n個の場所にr個の物を分ける //@起動時 struct initon { initon() { cin.tie(0); ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(16); srand((unsigned)clock() + (unsigned)time(NULL)); }; } initonv; //@formatter:on // gra mll pr //上下左右 const string udlr = "udlr"; string UDLR = "UDLR"; // x4と連動 UDLR.find('U') := x4[0] //右、上が正 constexpr ll y4[] = {1, -1, 0, 0}; constexpr ll x4[] = {0, 0, -1, 1}; constexpr ll y8[] = {0, 1, 0, -1, -1, 1, 1, -1}; constexpr ll x8[] = {1, 0, -1, 0, 1, -1, 1, -1}; ll n, m, k, K, d, H, W, x, y, z, q, Q; ll cou; vi a, b, c; // vvi (s, 0, 0); vvc(ba, 0, 0); vp p; str s; //一番高速 // hashを共有しない class roll_fast_only { static constexpr uint64_t base = 5; //他のロリハとhashを共有したい場合 // const uint64_t base = rand((uint64_t) 2, mask61 - // 1);//共有したくない場合 static constexpr uint64_t mask30 = ((uint64_t)1 << 30) - 1; static constexpr uint64_t mask31 = ((uint64_t)1 << 31) - 1; static constexpr uint64_t mask61 = ((uint64_t)1 << 61) - 1; static constexpr uint64_t offset = mask61 * 7; static constexpr uint64_t mod(const uint64_t x) { const uint64_t y = (x >> 61) + (x & mask61); return y >= mask61 ? y - mask61 : y; } static constexpr uint64_t mul(const uint64_t x, const uint64_t y) { const uint64_t xh = x >> 31, xl = x & mask31; const uint64_t yh = y >> 31, yl = y & mask31; const uint64_t z = xl * yh + xh * yl; const uint64_t zh = z >> 30, zl = z & mask30; return xh * yh * 2 + zh + (zl << 31) + xl * yl; } const std::size_t siz; vector<uint64_t> pow, h; public: template <typename T> roll_fast_only(T &A) : siz(sz(A)), pow(siz + 1, 1), h(siz + 1, 0) { for (std::size_t i = 1; i <= siz; i++) { pow[i] = mod(mul(pow[i - 1], base)), h[i] = mod(mul(h[i - 1], base) + A[i - 1]); } } uint64_t operator()(const std::size_t l, const std::size_t r) { return mod(h[r] + offset - mul(h[l], pow[r - l])); } uint64_t operator()(int r = inf) { return operator()(0, min(siz, r)); } inline int lcp_dup(int a, int b) { int len = min((int)siz - a, (int)siz - b); int low = 0, high = len; while (high - low > 1) { int mid = (low + high) >> 1; if (operator()(a, a + mid) != operator()(b, b + mid)) high = mid; else low = mid; } return low; } inline int lcp_not(int a, int b) { return min(lcp_dup(a, b), b - a); } //@formatter:on }; //動かない場合roll_fast_only2 // roll_fast_only ro(list); // lcpならsa := saffix_arrayでO(1) 構成O(N log(n)^2) void solve() { in(n); in(s); roll_fast_only ro(s); rer(len, n - 1, 1) { // umapu m; unordered_map<uint64_t, int> m; rep(i, n - len + 1) { auto has = ro(i, i + len); if (m.find(has) != m.end()) { if (m[has] <= i) fin(len); } else m[has] = i + len; } } fin(0); } auto my(ll n, vi &a) { return 0; } auto sister(ll n, vi &a) { ll ret = 0; return ret; } signed main() { solve(); #define arg n, a #ifdef _DEBUG bool bad = 0; for (ll i = 0, ok = 1; i < k5 && ok; ++i) { ll n = rand(1, 8); vi a = ranv(n, 1, 10); auto myres = my(arg); auto res = sister(arg); ok = myres == res; if (!ok) { out(arg); cerr << "AC : " << res << endl; cerr << "MY : " << myres << endl; bad = 1; break; } } if (!bad) { // cout << "完璧 : solveを書き直そう" << endl; // cout << " : そして、solve()を呼び出すのだ" << endl; // cout << " : cin>>n; na(a,n);も忘れるな" << endl; } #endif return 0; };
//#pragma GCC optimize("Ofast") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace std::chrono; #define int long long #define ll long long auto start_time = system_clock::now(); //@formatter:off #ifdef _DEBUG //区間削除は出来ない template <class T> struct my_pbds_tree { set<T> s; auto begin() { return s.begin(); } auto end() { return s.end(); } auto rbegin() { return s.rbegin(); } auto rend() { return s.rend(); } auto empty() { return s.empty(); } auto size() { return s.size(); } void clear() { s.clear(); } template <class U> void insert(U v) { s.insert(v); } template <class U> void operator+=(U v) { insert(v); } template <class F> auto erase(F v) { return s.erase(v); } template <class U> auto find(U v) { return s.find(v); } template <class U> auto lower_bound(U v) { return s.lower_bound(v); } template <class U> auto upper_bound(U v) { return s.upper_bound(v); } auto find_by_order(ll k) { auto it = s.begin(); for (ll i = 0; i < k; i++) it++; return it; } auto order_of_key(ll v) { auto it = s.begin(); ll i = 0; for (; it != s.end() && *it < v; i++) it++; return i; } }; #define pbds(T) my_pbds_tree<T> // gp_hash_tableでcountを使えないようにするため template <class T, class U> struct my_unordered_map { unordered_map<T, U> m; my_unordered_map(){}; auto begin() { return m.begin(); } auto end() { return m.end(); } auto cbegin() { return m.cbegin(); } auto cend() { return m.cend(); } template <class V> auto erase(V v) { return m.erase(v); } void clear() { m.clear(); } /*countは gp_hash_tableに存在しない*/ /*!= m.end()*/ template <class V> auto find(V v) { return m.find(v); } template <class V> auto &operator[](V n) { return m[n]; } }; #define unordered_map my_unordered_map #define umapi unordered_map<ll, ll> #define umapu unordered_map<uint64_t, ll> #define umapp unordered_map<P, ll> #else #define unordered_map __gnu_pbds::gp_hash_table // find_by_order(k) k番目のイテレーター // order_of_key(k) k以上が前から何番目か #define pbds(U) \ __gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag, \ __gnu_pbds::tree_order_statistics_node_update> #define umapi unordered_map<ll, ll, xorshift> #define umapp unordered_map<P, ll, xorshift> #define umapu unordered_map<uint64_t, ll, xorshift> #endif struct xorshift { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } size_t operator()(std::pair<ll, ll> x) const { ll v = ((x.first) << 32) | x.second; static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(v + FIXED_RANDOM); } }; template <class U, class L> void operator+=( __gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update> &s, L v) { s.insert(v); } //衝突対策 #define ws wszzzz template <class A, class B, class C> struct T2 { A f; B s; C t; T2() { f = 0, s = 0, t = 0; } T2(A f, B s, C t) : f(f), s(s), t(t) {} bool operator<(const T2 &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 ?n s > r.s : t > r.t; 大きい順 */ } bool operator>(const T2 &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 T2 &r) const { return f == r.f && s == r.s && t == r.t; } bool operator!=(const T2 &r) const { return f != r.f || s != r.s || t != r.t; } }; template <class A, class B, class C, class D> struct F2 { A a; B b; C c; D d; F2() { a = 0, b = 0, c = 0, d = 0; } F2(A a, B b, C c, D d) : a(a), b(b), c(c), d(d) {} bool operator<(const F2 &r) const { return a != r.a ? a < r.a : b != r.b ? b < r.b : c != r.c ? c < r.c : d < r.d; /* return a != r.a ? a > r.a : b != r.b ? b > r.b : c != r.c ? c > r.c : d > r.d;*/ } bool operator>(const F2 &r) const { return a != r.a ? a > r.a : b != r.b ? b > r.b : c != r.c ? c > r.c : d > r.d; /* return a != r.a ? a < r.a : b != r.b ? b < r.b : c != r.c ? c < r.c : d < r.d;*/ } bool operator==(const F2 &r) const { return a == r.a && b == r.b && c == r.c && d == r.d; } bool operator!=(const F2 &r) const { return a != r.a || b != r.b || c != r.c || d != r.d; } ll operator[](ll i) { assert(i < 4); return i == 0 ? a : i == 1 ? b : i == 2 ? c : d; } }; typedef T2<ll, ll, ll> T; typedef F2<ll, ll, ll, ll> F; T mt(ll a, ll b, ll c) { return T(a, b, c); } //@マクロ省略系 型,構造 #define double long double #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 beg begin #define rbeg rbegin #define con continue #define bre break #define brk break #define is == #define el else #define elf else if #define wh while #define upd update #define maxq 1 #define minq -1 #define ZERO(a) memset(a, 0, sizeof(a)) #define MINUS(a) memset(a, 0xff, sizeof(a)) #define MALLOC(type, len) (type *)malloc((len) * sizeof(type)) #define lam(right) [&](ll &p) { return p right; } //マクロ省略系 コンテナ 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 PQ priority_queue<ll, vector<ll>, greater<ll>> #define tos to_string using mapi = map<ll, ll>; using mapp = map<P, ll>; using mapd = map<dou, ll>; using mapc = map<char, ll>; using maps = map<str, ll>; using seti = set<ll>; using setd = set<dou>; using setc = set<char>; using sets = set<str>; using qui = queue<ll>; #define bset bitset #define uset unordered_set #define useti unordered_set<ll, ll, xorshift> #define mset multiset #define mseti multiset<ll> #define umap unordered_map #define mmap multimap template <class T> struct pq { priority_queue<T, vector<T>, greater<T>> q; /*小さい順*/ T su = 0; void clear() { q = priority_queue<T, vector<T>, greater<T>>(); su = 0; } void operator+=(T v) { su += v; q.push(v); } T sum() { return su; } T top() { return q.top(); } void pop() { su -= q.top(); q.pop(); } T poll() { T ret = q.top(); su -= ret; q.pop(); return ret; } ll size() { return q.size(); } }; template <class T> struct pqg { priority_queue<T> q; /*大きい順*/ T su = 0; void clear() { q = priority_queue<T>(); su = 0; } void operator+=(T v) { su += v; q.push(v); } T sum() { return su; } T top() { return q.top(); } void pop() { su -= q.top(); q.pop(); } T poll() { T ret = q.top(); su -= ret; q.pop(); return ret; } ll size() { return q.size(); } }; #define pqi pq<ll> #define pqgi pqg<ll> //マクロ 繰り返し #define o_rep(o1, o2, o3, o4, name, ...) name #define rep1(n) for (ll rep1i = 0, rep1lim = n; rep1i < rep1lim; ++rep1i) #define rep2(i, n) for (ll i = 0, rep2lim = n; i < rep2lim; ++i) #define rep3(i, m, n) for (ll i = m, rep3lim = n; i < rep3lim; ++i) #define rep4(i, m, n, ad) for (ll i = m, rep4lim = n; i < rep4lim; i += ad) #define rep(...) o_rep(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__) #define rer2(i, n) for (ll i = n; i >= 0; i--) #define rer3(i, m, n) for (ll i = m, rer3lim = n; i >= rer3lim; i--) #define rer4(i, m, n, dec) for (ll i = m, rer4lim = n; i >= rer4lim; i -= dec) #define rer(...) o_rep(__VA_ARGS__, rer4, rer3, rer2, )(__VA_ARGS__) #define reps2(i, j, n) \ for (ll i = 0, reps2lim = n; i < reps2lim; ++i) \ for (ll j = 0; j < reps2lim; ++j) #define reps3(i, j, k, n) \ for (ll i = 0, reps3lim = n; i < reps3lim; ++i) \ for (ll j = 0; j < reps3lim; ++j) \ for (ll k = 0; k < reps3lim; ++k) #define reps4(i, j, k, l, n) \ for (ll i = 0, reps4lim = n; i < reps4lim; ++i) \ for (ll j = 0; j < reps4lim; ++j) \ for (ll k = 0; k < reps4lim; ++k) \ for (ll l = 0; l < reps4lim; ++l) #define o_reps(o1, o2, o3, o4, o5, name, ...) name #define reps(...) o_reps(__VA_ARGS__, reps4, reps3, reps2, rep2, )(__VA_ARGS__) #define repss(i, j, k, a, b, c) \ for (ll i = 0; i < a; ++i) \ for (ll j = 0; j < b; ++j) \ for (ll k = 0; k < c; ++k) #define fora(a, b) for (auto &&a : b) //インデックスを前後含めて走査 #define fori(i, s, len) \ for (int i = s, prev = (s == 0) ? len - 1 : s - 1, \ next = (s == len - 1) ? 0 : s + 1, cou = 0; \ cou < len; \ cou++, prev = i, i = next, next = (next == len - 1) ? 0 : next + 1) #define form(st, l, r) \ for (auto &&it = st.lower_bound(l); it != st.end() && (*it).fi < r; ++it) #define forit(st, l, r) \ for (auto &&it = st.lower_bound(l); it != st.end() && (*it) < r;) //マクロ 定数 #define k3 1010 #define k4 10101 #define k5 101010 #define k6 1010101 #define k7 10101010 const ll inf = (ll)1e9 + 100; const ll linf = (ll)1e18 + 100; const char infc = '{'; const string infs = "{"; const double eps = 1e-9; const double PI = 3.1415926535897932384626433832795029L; //マクロ省略形 関数等 #define arsz(a) (sizeof(a) / sizeof(a[0])) #define sz(a) ((ll)(a).size()) #define mp make_pair #define pb pop_back #define pf push_front #define eb emplace_back #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() constexpr bool ev(ll a) { return !(a & 1); } constexpr bool od(ll a) { return (a & 1); } //@拡張系 こう出来るべきというもの //埋め込み 存在を意識せずに機能を増やされているもの //@formatter:on namespace std { template <> class hash<std::pair<signed, signed>> { public: size_t operator()(const std::pair<signed, signed> &x) const { return hash<ll>()(((ll)x.first << 32) | x.second); } }; template <> class hash<std::pair<ll, ll>> { public : /*大きいllが渡されると、<<32でオーバーフローするがとりあえず問題ないと判断*/ size_t operator()(const std::pair<ll, ll> &x) const { return hash<ll>()(((ll)x.first << 32) | x.second); } }; } // namespace std //@formatter:off // stream まとめ istream &operator>>(istream &iss, P &a) { iss >> a.first >> a.second; return iss; } template <typename T> istream &operator>>(istream &iss, vector<T> &vec) { for (T &x : vec) iss >> x; return iss; } template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> p) { os << p.fi << " " << p.se << endl; return os; } ostream &operator<<(ostream &os, T p) { os << p.f << " " << p.s << " " << p.t; return os; } ostream &operator<<(ostream &os, F p) { os << p.a << " " << p.b << " " << p.c << " " << p.d; return os; } template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) { for (ll i = 0; i < vec.size(); ++i) os << vec[i] << (i + 1 == vec.size() ? "" : " "); return os; } template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) { for (ll i = 0; i < vec.size(); ++i) { for (ll j = 0; j < vec[i].size(); ++j) { os << vec[i][j] << " "; } os << endl; } return os; } template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &m) { for (auto &&v : m) os << v; return os; } template <class T> ostream &operator<<(ostream &os, set<T> s) { fora(v, s) { os << v << " "; } return os; } template <class T> ostream &operator<<(ostream &os, deque<T> a) { fora(v, a) os << v << " "; return os; } 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); }); } #define all_of(a, right) all_of2(a, lam(right)) #define any_of(a, right) any_of2(a, lam(right)) #define none_of(a, right) none_of2(a, lam(right)) #define find_if(a, right) find_if2(a, lam(right)) #define rfind_if(a, right) rfind_if2(a, lam(right)) #define contains_if(a, right) contains_if2(a, lam(right)) #define count_if(a, right) count_if2(a, lam(right)) #define for_each(a, right) \ do { \ fora(v, a) { v right; } \ } while (0) template <class T, class U> void replace(vector<T> &a, T key, U v) { replace(a.begin(), a.end(), key, v); } void replace(str &a, char key, str v) { if (v == "") a.erase(remove(all(a), key), a.end()); } void replace(str &a, char key, char v) { replace(all(a), key, v); } // keyと同じかどうか01で置き換える template <class T, class U> void replace(vector<T> &a, U k) { rep(i, sz(a)) a[i] = a[i] == k; } template <class T, class U> void replace(vector<vector<T>> &a, U k) { rep(i, sz(a)) rep(j, sz(a[0])) a[i][j] = a[i][j] == k; } template <class T> void replace(T &a) { replace(a, '#'); } void replace(str &a, str key, str v) { stringstream t; ll kn = sz(key); std::string::size_type Pos(a.find(key)); ll l = 0; while (Pos != std::string::npos) { t << a.substr(l, Pos - l); t << v; l = Pos + kn; Pos = a.find(key, Pos + kn); } t << a.substr(l, sz(a) - l); a = t.str(); } template <class T> bool includes(vector<T> &a, vector<T> &b) { vi c = a; vi d = b; sort(all(c)); sort(all(d)); return includes(all(c), all(d)); } template <class T> bool is_permutation(vector<T> &a, vector<T> &b) { return is_permutation(all(a), all(b)); } template <class T> bool next_permutation(vector<T> &a) { return next_permutation(all(a)); } void iota(vector<ll> &ve, ll s, ll n) { ve.resize(n); iota(all(ve), s); } vi iota(ll s, ll len) { vi ve(len); iota(all(ve), s); return ve; } template <class A, class B> auto vtop(vector<A> &a, vector<B> &b) { assert(sz(a) == sz(b)); /*stringを0で初期化できない */ vector<pair<A, B>> res; rep(i, sz(a)) res.eb(a[i], b[i]); return res; } template <class A, class B> void ptov(vector<pair<A, B>> &p, vector<A> &a, vector<B> &b) { a.resize(sz(p)), b.resize(sz(p)); rep(i, sz(p)) a[i] = p[i].fi, b[i] = p[i].se; } template <class A, class B, class C> auto vtot(vector<A> &a, vector<B> &b, vector<C> &c) { assert(sz(a) == sz(b) && sz(b) == sz(c)); vector<T2<A, B, C>> res; rep(i, sz(a)) res.eb(a[i], b[i], c[i]); return res; } template <class A, class B, class C, class D> auto vtof(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { assert(sz(a) == sz(b) && sz(b) == sz(c) && sz(c) == sz(d)); vector<F2<A, B, C, D>> res; rep(i, sz(a)) res.eb(a[i], b[i], c[i], d[i]); return res; } enum pcomparator { fisi, fisd, fdsi, fdsd, sifi, sifd, sdfi, sdfd }; enum tcomparator { fisiti, fisitd, fisdti, fisdtd, fdsiti, fdsitd, fdsdti, fdsdtd, fitisi, fitisd, fitdsi, fitdsd, fdtisi, fdtisd, fdtdsi, fdtdsd, sifiti, sifitd, sifdti, sifdtd, sdfiti, sdfitd, sdfdti, sdfdtd, sitifi, sitifd, sitdfi, sitdfd, sdtifi, sdtifd, sdtdfi, sdfdfd, tifisi, tifisd, tifdsi, tifdsd, tdfisi, tdfisd, tdfdsi, tdfdsd, tisifi, tisifd, tisdfi, tisdfd, tdsifi, tdsifd, tdsdfi, tdsdfd }; template <class A, class B> void sort(vector<pair<A, B>> &a, pcomparator type) { typedef pair<A, B> U; if (type == fisi) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se < r.se; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se > r.se; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se < r.se; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se > r.se; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi < r.fi; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi > r.fi; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi < r.fi; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi > r.fi; }); }; template <class U> void sort(vector<U> &a, pcomparator type) { if (type == fisi) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f > r.f; }); }; template <class A, class B, class C, class D> void sort(vector<F2<A, B, C, D>> &a, pcomparator type) { typedef F2<A, B, C, D> U; if (type == fisi) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == fisd) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == fdsi) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == fdsd) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == sifi) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == sifd) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == sdfi) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == sdfd) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a > r.a; }); }; template <class U> void sort(vector<U> &a, tcomparator type) { if (type == 0) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t < r.t; }); else if (type == 1) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t > r.t; }); else if (type == 2) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t < r.t; }); else if (type == 3) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t > r.t; }); else if (type == 4) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t < r.t; }); else if (type == 5) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t > r.t; }); else if (type == 6) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t < r.t; }); else if (type == 7) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t > r.t; }); else if (type == 8) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s < r.s; }); else if (type == 9) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s > r.s; }); else if (type == 10) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s < r.s; }); else if (type == 11) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s > r.s; }); else if (type == 12) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s < r.s; }); else if (type == 13) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s > r.s; }); else if (type == 14) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s < r.s; }); else if (type == 15) sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s > r.s; }); else if (type == 16) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t < r.t; }); else if (type == 17) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t > r.t; }); else if (type == 18) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t < r.t; }); else if (type == 19) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t > r.t; }); else if (type == 20) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t < r.t; }); else if (type == 21) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t > r.t; }); else if (type == 22) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t < r.t; }); else if (type == 23) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t > r.t; }); else if (type == 24) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f < r.f; }); else if (type == 25) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f > r.f; }); else if (type == 26) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f < r.f; }); else if (type == 27) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f > r.f; }); else if (type == 28) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f < r.f; }); else if (type == 29) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f > r.f; }); else if (type == 30) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f < r.f; }); else if (type == 31) sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f > r.f; }); else if (type == 32) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == 33) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == 34) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == 35) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == 36) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s < r.s; }); else if (type == 37) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s > r.s; }); else if (type == 38) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s < r.s; }); else if (type == 39) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s > r.s; }); else if (type == 40) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == 41) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == 42) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == 43) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f > r.f; }); else if (type == 44) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f < r.f; }); else if (type == 45) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f > r.f; }); else if (type == 46) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f < r.f; }); else if (type == 47) sort(all(a), [&](U l, U r) { return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f > r.f; }); } template <class A, class B, class C, class D> void sort(vector<F2<A, B, C, D>> &a, tcomparator type) { typedef F2<A, B, C, D> U; if (type == 0) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c < r.c; }); else if (type == 1) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c > r.c; }); else if (type == 2) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c < r.c; }); else if (type == 3) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c > r.c; }); else if (type == 4) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c < r.c; }); else if (type == 5) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c > r.c; }); else if (type == 6) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c < r.c; }); else if (type == 7) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c > r.c; }); else if (type == 8) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b < r.b; }); else if (type == 9) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b > r.b; }); else if (type == 10) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b < r.b; }); else if (type == 11) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b > r.b; }); else if (type == 12) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b < r.b; }); else if (type == 13) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b > r.b; }); else if (type == 14) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b < r.b; }); else if (type == 15) sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b > r.b; }); else if (type == 16) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c < r.c; }); else if (type == 17) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c > r.c; }); else if (type == 18) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c < r.c; }); else if (type == 19) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c > r.c; }); else if (type == 20) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c < r.c; }); else if (type == 21) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c > r.c; }); else if (type == 22) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c < r.c; }); else if (type == 23) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c > r.c; }); else if (type == 24) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a < r.a; }); else if (type == 25) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a > r.a; }); else if (type == 26) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a < r.a; }); else if (type == 27) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a > r.a; }); else if (type == 28) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a < r.a; }); else if (type == 29) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a > r.a; }); else if (type == 30) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a < r.a; }); else if (type == 31) sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a > r.a; }); else if (type == 32) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == 33) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == 34) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == 35) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == 36) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b < r.b; }); else if (type == 37) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b > r.b; }); else if (type == 38) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b < r.b; }); else if (type == 39) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b > r.b; }); else if (type == 40) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == 41) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == 42) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == 43) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a > r.a; }); else if (type == 44) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a < r.a; }); else if (type == 45) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a > r.a; }); else if (type == 46) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a < r.a; }); else if (type == 47) sort(all(a), [&](U l, U r) { return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a > r.a; }); } void sort(string &a) { sort(all(a)); } template <class T> void sort(vector<T> &a) { sort(all(a)); } // P l, P rで f(P) の形で渡す template <class U, class F> void sort(vector<U> &a, F f) { sort(all(a), [&](U l, U r) { return f(l) < f(r); }); }; template <class T> void rsort(vector<T> &a) { sort(all(a), greater<T>()); }; template <class U, class F> void rsort(vector<U> &a, F f) { sort(all(a), [&](U l, U r) { return f(l) > f(r); }); }; // F = T<T> //例えばreturn p.fi + p.se; template <class A, class B> void sortp(vector<A> &a, vector<B> &b) { auto c = vtop(a, b); sort(c); rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se; } template <class A, class B, class F> void sortp(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); sort(c, f); rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se; } template <class A, class B> void rsortp(vector<A> &a, vector<B> &b) { auto c = vtop(a, b); rsort(c); rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second; } template <class A, class B, class F> void rsortp(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); rsort(c, f); rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second; } template <class A, class B, class C> void sortt(vector<A> &a, vector<B> &b, vector<C> &c) { auto d = vtot(a, b, c); sort(d); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class F> void sortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); sort(d, f); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C> void rsortt(vector<A> &a, vector<B> &b, vector<C> &c) { auto d = vtot(a, b, c); rsort(d); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class F> void rsortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); rsort(d, f); rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t; } template <class A, class B, class C, class D> void sortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { auto e = vtof(a, b, c, d); sort(e); rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d; } template <class A, class B, class C, class D> void rsortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) { auto e = vtof(a, b, c, d); rsort(e); rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d; } // sortindex 元のvectorはソートしない template <class T> vi sorti(vector<T> &a) { auto b = a; vi ind = iota(0, sz(a)); sortp(b, ind); return ind; } /*indexの分で型が変わるためpcomparatorが必要*/ template <class T> vi sorti(vector<T> &a, pcomparator f) { auto b = a; vi ind = iota(0, sz(a)); sortp(b, ind, f); return ind; } template <class T, class F> vi sorti(vector<T> &a, F f) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(a[x]) < f(a[y]); }); return ind; } template <class T> vi rsorti(vector<T> &a) { auto b = a; vi ind = iota(0, sz(a)); rsortp(b, ind); return ind; } template <class T, class F> vi rsorti(vector<T> &a, F f) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(a[x]) > f(a[y]); }); return ind; } template <class A, class B, class F> vi sortpi(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(c[x]) < f(c[y]); }); return ind; } template <class A, class B> vi sortpi(vector<A> &a, vector<B> &b, pcomparator f) { vi ind = iota(0, sz(a)); auto c = a; auto d = b; sortt(c, d, ind, f); return ind; } template <class A, class B> vi sortpi(vector<A> &a, vector<B> &b) { return sortpi(a, b, fisi); }; template <class A, class B, class F> vi rsortpi(vector<A> &a, vector<B> &b, F f) { auto c = vtop(a, b); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(c[x]) > f(c[y]); }); return ind; } template <class A, class B> vi rsortpi(vector<A> &a, vector<B> &b) { return sortpi(a, b, fdsd); }; template <class A, class B, class C, class F> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(d[x]) < f(d[y]); }); return ind; } template <class A, class B, class C> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, pcomparator f) { vi ind = iota(0, sz(a)); auto d = vtof(a, b, c, ind); sort(d, f); rep(i, sz(a)) ind[i] = d[i].d; return ind; } template <class A, class B, class C> vi sortti(vector<A> &a, vector<B> &b, vector<C> &c) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { if (a[x] == a[y]) { if (b[x] == b[y]) return c[x] < c[y]; else return b[x] < b[y]; } else { return a[x] < a[y]; } }); return ind; } template <class A, class B, class C, class F> vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) { auto d = vtot(a, b, c); vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { return f(d[x]) > f(d[y]); }); return ind; } template <class A, class B, class C> vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c) { vi ind = iota(0, sz(a)); sort(all(ind), [&](ll x, ll y) { if (a[x] == a[y]) { if (b[x] == b[y]) return c[x] > c[y]; else return b[x] > b[y]; } else { return a[x] > a[y]; } }); return ind; } template <class T> void sort2(vector<vector<T>> &a) { for (ll i = 0, n = a.size(); i < n; ++i) sort(a[i]); } template <class T> void rsort2(vector<vector<T>> &a) { for (ll 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 W, typename T> void fill(W &xx, const T vall) { xx = vall; } template <typename W, typename T> void fill(vector<W> &vecc, const T vall) { for (auto &&vx : vecc) fill(vx, vall); } template <typename W, typename T> void fill(vector<W> &xx, const T v, ll len) { rep(i, len) xx[i] = v; } template <typename W, typename T> void fill(vector<vector<W>> &xx, const T v, ll lh, ll lw) { rep(i, lh) rep(j, lw) xx[i][j] = v; } template <class T, class U> void fill(vector<T> &a, U val, vi &ind) { fora(v, ind) a[v] = val; } template <typename A, size_t N> A sum(A (&a)[N]) { A res = 0; rep(i, N) res += a[i]; return res; } template <typename A, size_t N, size_t O> A sum(A (&a)[N][O]) { A res = 0; rep(i, N) rep(j, O) res += a[i][j]; return res; } template <typename A, size_t N, size_t O, size_t P> A sum(A (&a)[N][O][P]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) res += a[i][j][k]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A sum(A (&a)[N][O][P][Q]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) res += a[i][j][k][l]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A sum(A (&a)[N][O][P][Q][R]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) res += a[i][j][k][l][m]; return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A sum(A (&a)[N][O][P][Q][R][S]) { A res = 0; rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) rep(n, S) res += a[i][j][k][l][m][n]; return res; } //@汎用便利関数 入力 ll in() { ll ret; cin >> ret; return ret; } string sin() { string ret; cin >> ret; return ret; } template <class T> void in(T &head) { cin >> head; } template <class T, class... U> void in(T &head, U &...tail) { cin >> head; in(tail...); } #define o_din(o1, o2, o3, o4, o5, o6, name, ...) name #define din1(a) \ ll a; \ cin >> a #define din2(a, b) \ ll a, b; \ cin >> a >> b #define din3(a, b, c) \ ll a, b, c; \ cin >> a >> b >> c #define din4(a, b, c, d) \ ll a, b, c, d; \ cin >> a >> b >> c >> d #define din5(a, b, c, d, e) \ ll a, b, c, d, e; \ cin >> a >> b >> c >> d >> e #define din6(a, b, c, d, e, f) \ ll a, b, c, d, e, f; \ cin >> a >> b >> c >> d >> e >> f #define din(...) \ o_din(__VA_ARGS__, din6, din5, din4, din3, din2, din1)(__VA_ARGS__) #define o_dind(o1, o2, o3, o4, name, ...) name #define din1d(a) \ din1(a); \ a-- #define din2d(a, b) \ din2(a, b); \ a--, b-- #define din3d(a, b, c) \ din3(a, b, c); \ a--, b--, c-- #define din4d(a, b, c, d) \ din4(a, b, c, d); \ a--, b--, c--, d-- #define dind(...) o_dind(__VA_ARGS__, din4d, din3d, din2d, din1d)(__VA_ARGS__) template <class T> void out2(T &&head) { cout << head; } template <class T, class... U> void out2(T &&head, U &&...tail) { cout << head << " "; out2(tail...); } template <class T, class... U> void out(T &&head, U &&...tail) { cout << head << " "; out2(tail...); cout << "" << endl; } template <class T> void out(T &&head) { cout << head << endl; } void out() { cout << "" << endl; } #ifdef _DEBUG 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 (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) { ; } #endif template <class T> void outl(vector<T> &a, int n = inf) { rep(i, min(n, sz(a))) cout << a[i] << endl; } template <class T> void na(vector<T> &a, ll n) { a.resize(n); rep(i, n) cin >> a[i]; } #define dna(a, n) \ vi a(n); \ rep(dnai, n) cin >> a[dnai]; template <class T> void nao(vector<T> &a, ll n) { a.resize(n + 1); a[0] = 0; rep(i, n) cin >> a[i + 1]; } template <class T> void naod(vector<T> &a, ll n) { a.resize(n + 1); a[0] = 0; rep(i, n) cin >> a[i + 1], a[i + 1]--; } template <class T> void nad(vector<T> &a, ll n) { a.resize(n); rep(i, n) cin >> a[i], a[i]--; } template <class T, class U> void na2(vector<T> &a, vector<U> &b, ll n) { a.resize(n); b.resize(n); rep(i, n) cin >> a[i] >> b[i]; } #define dna2(a, b, n) \ vi a(n), b(n); \ rep(dna2i, n) cin >> a[dna2i] >> b[dna2i]; template <class T, class U> void nao2(vector<T> &a, vector<U> &b, ll n) { a.resize(n + 1); b.resize(n + 1); a[0] = b[0] = 0; rep(i, n) cin >> a[i + 1] >> b[i + 1]; } #define dna2d(a, b, n) \ vi a(n), b(n); \ rep(dna2di, n) { \ cin >> a[dna2di] >> b[dna2di]; \ a[dna2di]--, b[dna2di]--; \ } template <class T, class U> void na2d(vector<T> &a, vector<U> &b, ll n) { a.resize(n); b.resize(n); rep(i, n) cin >> a[i] >> b[i], a[i]--, b[i]--; } template <class T, class U, class W> void na3(vector<T> &a, vector<U> &b, vector<W> &c, ll n) { a.resize(n); b.resize(n); c.resize(n); rep(i, n) cin >> a[i] >> b[i] >> c[i]; } #define dna3(a, b, c, n) \ vi a(n), b(n), c(n); \ rep(dna3i, n) cin >> a[dna3i] >> b[dna3i] >> c[dna3i]; template <class T, class U, class W> void na3d(vector<T> &a, vector<U> &b, vector<W> &c, ll 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 dna3d(a, b, c, n) \ vi a(n), b(n), c(n); \ rep(dna3di, n) { \ cin >> a[dna3di] >> b[dna3di] >> c[dna3di]; \ a[dna3di]--, b[dna3di]--, c[dna3di]--; \ } template <class T, class U, class W, class X> void na4(vector<T> &a, vector<U> &b, vector<W> &c, vector<X> &d, ll n) { a.resize(n); b.resize(n); c.resize(n); d.resize(n); rep(i, n) cin >> a[i] >> b[i] >> c[i] >> d[i]; } #define dna4(a, b, c, d, n) \ vi a(n), b(n), c(n), d(n); \ rep(dna4i, n) cin >> a[dna4i] >> b[dna4i] >> c[dna4i] >> d[dna4i]; #define dna4d(a, b, c, d, n) \ vi a(n), b(n), c(n), d(n); \ rep(dna4i, n) cin >> a[dna4i] >> b[dna4i] >> c[dna4i] >> d[dna4i], \ --a[dna4i], --b[dna4i], --c[dna4i], --d[dna4i]; #define nt(a, h, w) \ resize(a, h, w); \ rep(nthi, h) rep(ntwi, w) cin >> a[nthi][ntwi]; #define ntd(a, h, w) \ resize(a, h, w); \ rep(ntdhi, h) rep(ntdwi, w) cin >> a[ntdhi][ntdwi], a[ntdhi][ntdwi]--; #define ntp(a, h, w) \ resize(a, h + 2, w + 2); \ fill(a, '#'); \ rep(ntphi, 1, h + 1) rep(ntpwi, 1, w + 1) cin >> a[ntphi][ntpwi]; //デバッグ #define sp << " " << #define debugName(VariableName) #VariableName #define deb1(x) debugName(x) << " = " << out_m(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' //@formatter:off //よく使うクラス、構造体 struct unionfind { vector<ll> par; vector<ll> siz; vector<ll> es; ll n, trees; //連結グループの数(親の種類) unionfind(ll n) : n(n), trees(n) { par.resize(n); siz.resize(n); es.resize(n); for (ll i = 0; i < n; i++) { par[i] = i; siz[i] = 1; } } ll root(ll x) { if (par[x] == x) { return x; } else { return par[x] = root(par[x]); } } bool unite(ll x, ll y) { x = root(x); y = root(y); es[x]++; if (x == y) return false; if (siz[x] > siz[y]) swap(x, y); trees--; par[x] = y; siz[y] += siz[x]; es[y] += es[x]; return true; } bool same(ll x, ll y) { return root(x) == root(y); } ll size(ll x) { return siz[root(x)]; } ll esize(ll x) { return es[root(x)]; } vi sizes() { vi cou(n); vi ret; ret.reserve(n); rep(i, n) { cou[root(i)]++; } rep(i, n) { if (cou[i]) ret.push_back(cou[i]); } return ret; } //つながりを無向グラフと見なし、xが閉路に含まれるか判定 bool close(ll x) { return esize(x) >= size(x); } vec<vi> sets() { vi ind(n, -1); ll i = 0; vvi(res, trees); rep(j, n) { ll r = root(j); if (ind[r] == -1) ind[r] = i++; res[ind[r]].push_back(j); } rep(i, trees) { ll r = root(res[i][0]); if (res[i][0] == r) continue; rep(j, 1, sz(res[i])) { if (res[i][j] == r) { swap(res[i][0], res[i][j]); break; } } } return res; } }; //@formatter:off using bll = __int128; using u32 = unsigned; using u64 = unsigned long long; using u128 = __uint128_t; std::ostream &operator<<(std::ostream &dest, __int128_t value) { std::ostream::sentry s(dest); if (s) { __uint128_t tmp = value < 0 ? -value : value; char buffer[128]; char *d = std::end(buffer); do { --d; *d = "0123456789"[tmp % 10]; tmp /= 10; } while (tmp != 0); if (value < 0) { --d; *d = '-'; } ll len = std::end(buffer) - d; if (dest.rdbuf()->sputn(d, len) != len) { dest.setstate(std::ios_base::badbit); } } return dest; } //__int128 toi128(string &s) { __int128 ret = 0; for (ll i = 0; i < //s.length(); ++i) if ('0' <= s[i] && s[i] <= '9') ret = 10 * //ret + s[i] - '0'; return ret;} //エラー 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 re() { assert(0 == 1); exit(0); } void tle() { while (inf) cout << inf << endl; } //便利関数 //テスト用 char ranc() { return (char)('a' + rand() % 26); } ll rand(ll min, ll max) { assert(min <= max); if (min >= 0 && max >= 0) { return rand() % (max + 1 - min) + min; } else if (max < 0) { return -rand(-max, -min); } else { if (rand() % 2) { return rand(0, max); } else { return -rand(0, -min); } } } vi ranv(ll n, ll min, ll max) { vi v(n); rep(i, n) v[i] = rand(min, max); return v; } str ransu(ll n) { str s; rep(i, n) s += (char)rand('A', 'Z'); return s; } str ransl(ll n) { str s; rep(i, n) s += (char)rand('a', 'z'); return s; } //単調増加 vi ranvinc(ll n, ll min, ll max) { vi v(n); bool bad = 1; while (bad) { bad = 0; v.resize(n); rep(i, n) { if (i && min > max - v[i - 1]) { bad = 1; break; } if (i) v[i] = v[i - 1] + rand(min, max - v[i - 1]); else v[i] = rand(min, max); } } return v; } //便利 汎用 void ranvlr(ll n, ll min, ll max, vi &l, vi &r) { l.resize(n); r.resize(n); rep(i, n) { l[i] = rand(min, max); r[i] = l[i] + rand(0, max - l[i]); } } 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 <class F> ll mgr(ll ok, ll ng, F f) { if (ok < ng) while (ng - ok > 1) { ll mid = (ok + ng) / 2; if (f(mid)) ok = mid; else ng = mid; } else while (ok - ng > 1) { ll mid = (ok + ng) / 2; if (f(mid)) ok = mid; else ng = mid; } return ok; } // strを整数として比較 string smax(str &a, str b) { if (sz(a) < sz(b)) { return b; } else if (sz(a) > sz(b)) { return a; } else if (a < b) return b; else return a; } // strを整数として比較 string smin(str &a, str b) { if (sz(a) > sz(b)) { return b; } else if (sz(a) < sz(b)) { return a; } else if (a > b) return b; else return a; } template <typename W, typename T> ll find(vector<W> &a, const T key) { rep(i, sz(a)) if (a[i] == key) return i; return -1; } template <typename W, typename T> P find(vector<vector<W>> &a, const T key) { rep(i, sz(a)) rep(j, sz(a[0])) if (a[i][j] == key) return mp(i, j); return mp(-1, -1); } template <typename W, typename U> T find(vector<vector<vector<W>>> &a, const U key) { rep(i, sz(a)) rep(j, sz(a[0])) rep(k, sz(a[0][0])) if (a[i][j][k] == key) return mt(i, j, k); return mt(-1, -1, -1); } template <typename W, typename T> ll count2(W &a, const T k) { return a == k; } template <typename W, typename T> ll count2(vector<W> &a, const T k) { ll ret = 0; fora(v, a) ret += count2(v, k); return ret; } template <typename W, typename T> ll count(vector<W> &a, const T k) { ll ret = 0; fora(v, a) ret += count2(v, k); return ret; } ll count(str &a, str k) { ll ret = 0, len = k.length(); auto pos = a.find(k); while (pos != string::npos) pos = a.find(k, pos + len), ++ret; return ret; } vi count(str &a) { vi cou(26); char c = 'a'; if ('A' <= a[0] && a[0] <= 'Z') c = 'A'; rep(i, sz(a))++ cou[a[i] - c]; return cou; } #define couif count_if // algorythm 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)); } template <class U> void rev(vector<vector<U>> &a) { vector<vector<U>> b(sz(a[0]), vector<U>(sz(a))); rep(h, sz(a)) rep(w, sz(a[0])) b[w][h] = a[h][w]; a = b; } void rev(string &a) { reverse(all(a)); } constexpr ll p10[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000ll, 100000000000ll, 1000000000000ll, 10000000000000ll, 100000000000000ll, 1000000000000000ll, 10000000000000000ll, 100000000000000000ll, 1000000000000000000ll}; ll get(ll a, ll keta) { return (a / (ll)pow(10, keta)) % 10; } ll keta(ll v) { if (v < p10[9]) { if (v < p10[4]) { if (v < p10[2]) { if (v < p10[1]) return 1; else return 2; } else { if (v < p10[3]) return 3; else return 4; } } else { if (v < p10[7]) { if (v < p10[5]) return 5; else if (v < p10[6]) return 6; else return 7; } else { if (v < p10[8]) return 8; else return 9; } } } else { if (v < p10[13]) { if (v < p10[11]) { if (v < p10[10]) return 10; else return 11; } else { if (v < p10[12]) return 12; else return 13; } } else { if (v < p10[15]) { if (v < p10[14]) return 14; else if (v < p10[15]) return 15; else return 16; } else { if (v < p10[17]) return 17; else return 18; } } } } ll dsum(ll v, ll sin = 10) { ll ret = 0; for (; v; v /= sin) ret += v % sin; return ret; } struct sint { ll v; sint(ll v) : v(v) {} operator ll() { return v; } //下からi番目 ll operator[](ll i) { return (v / p10[i]) % 10; } ll back(ll i) { return operator[](i); } //上からi番目 ll top(ll i) { ll len = keta(v); return operator[](len - 1 - i); } //先頭からi番目にセット ll settop(ll i, ll k) { ll len = keta(v); return set(len - 1 - i, k); } ll set(ll i, ll k) { if (i < 0) return settop(abs(i) - 1, k); return v += p10[i] * (k - (v / p10[i]) % 10); } ll add(ll i, ll k = 1) { return v += p10[i] * k; } ll addtop(ll i, ll k = 1) { return v += p10[keta(v) - i - 1] * k; } ll dec(ll i, ll k = 1) { return v -= p10[i] * k; } ll dectop(ll i, ll k = 1) { return v -= p10[keta(v) - i - 1] * k; } #define op(t, o) \ template <class T> t operator o(T r) { return v o r; } op(ll, +=); op(ll, -=); op(ll, *=); op(ll, /=); op(ll, %=); op(ll, +); op(ll, -); op(ll, *); op(ll, /); op(ll, %); op(bool, ==); op(bool, !=); op(bool, <); op(bool, <=); op(bool, >); op(bool, >=); #undef op template <class T> ll operator<<=(T r) { return v *= p10[r]; } template <class T> ll operator<<(T r) { return v * p10[r]; } template <class T> ll operator>>=(T r) { return v /= p10[r]; } template <class T> ll operator>>(T r) { return v / p10[r]; } }; ll mask10(ll v) { return p10[v] - 1; } //変換系 template <class T> auto keys(T &a) { vector<decltype((a.begin())->fi)> res; for (auto &&k : a) res.push_back(k.fi); return res; } template <class T> auto values(T &a) { vector<decltype((a.begin())->se)> res; for (auto &&k : a) res.push_back(k.se); return res; } template <class T, class U> bool chma(T &a, const U &b) { if (a < b) { a = b; return true; } return false; } template <class T, class U> bool chmi(T &a, const U &b) { if (b < a) { a = b; return true; } return false; } template <class T> T min(T a, signed b) { return a < b ? a : b; } template <class T> T max(T a, signed b) { return a < b ? b : a; } template <class T> T min(T a, T b, T c) { return a >= b ? b >= c ? c : b : a >= c ? c : a; } template <class T> T max(T a, T b, T c) { return a <= b ? b <= c ? c : b : a <= c ? c : a; } template <class T> T min(vector<T> &a) { return *min_element(all(a)); } template <class T> T mini(vector<T> &a) { return min_element(all(a)) - a.begin(); } template <class T> T min(vector<T> &a, ll n) { return *min_element(a.begin(), a.begin() + min(n, sz(a))); } template <class T> T min(vector<T> &a, ll s, ll n) { return *min_element(a.begin() + s, a.begin() + min(n, sz(a))); } template <class T> T max(vector<T> &a) { return *max_element(all(a)); } template <class T, class U> T max(vector<T> &a, vector<U> &b) { return max(*max_element(all(a)), *max_element(all(b))); } template <class T> T maxi(vector<T> &a) { return max_element(all(a)) - a.begin(); } template <class T> T max(vector<T> &a, ll n) { return *max_element(a.begin(), a.begin() + min(n, sz(a))); } template <class T> T max(vector<T> &a, ll s, ll n) { return *max_element(a.begin() + s, a.begin() + min(n, sz(a))); } template <typename A, size_t N> A max(A (&a)[N]) { A res = a[0]; rep(i, N) res = max(res, a[i]); return res; } template <typename A, size_t N, size_t O> A max(A (&a)[N][O]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P> A max(A (&a)[N][O][P]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A max(A (&a)[N][O][P][Q], const T &v) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A max(A (&a)[N][O][P][Q][R]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A max(A (&a)[N][O][P][Q][R][S]) { A res = max(a[0]); rep(i, N) res = max(res, max(a[i])); return res; } template <typename A, size_t N> A min(A (&a)[N]) { A res = a[0]; rep(i, N) res = min(res, a[i]); return res; } template <typename A, size_t N, size_t O> A min(A (&a)[N][O]) { A res = min(a[0]); rep(i, N) res = min(res, max(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P> A min(A (&a)[N][O][P]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q> A min(A (&a)[N][O][P][Q], const T &v) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R> A min(A (&a)[N][O][P][Q][R]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R, size_t S> A min(A (&a)[N][O][P][Q][R][S]) { A res = min(a[0]); rep(i, N) res = min(res, min(a[i])); return res; } template <class T> T sum(vector<T> &v, ll s, ll t) { T ret = 0; rep(i, s, min(sz(v), t)) ret += v[i]; return ret; } template <class T> T sum(vector<T> &v, ll t = inf) { return sum(v, 0, t); } template <class T> T sum(vector<vector<T>> &v) { T ret = 0; rep(i, sz(v)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<T>>> &v) { T ret = 0; rep(i, sz(v)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<vector<T>>>> &v) { T ret = 0; rep(i, sz(v)) ret += sum(v[i]); return ret; } template <class T> T sum(vector<vector<vector<vector<vector<T>>>>> &v) { T ret = 0; rep(i, sz(v)) ret += sum(v[i]); return ret; } template <class T> auto sum(priority_queue<T, vector<T>, greater<T>> &r) { auto q = r; T ret = 0; while (sz(q)) { ret += q.top(); q.pop(); } return ret; } template <class T> auto sum(priority_queue<T> &r) { auto q = r; T ret = 0; while (sz(q)) { ret += q.top(); q.pop(); } return ret; } // template<class T, class U, class... W> auto sumn(vector<T> &v, U head, W... // tail) { auto ret = sum(v[0], tail...); rep(i, 1, min(sz(v), head))ret += // sum(v[i], tail...); return ret;} void clear(PQ &q) { q = PQ(); } template <class T> void clear(queue<T> &q) { while (q.size()) q.pop(); } template <class T> T *negarr(ll size) { T *body = (T *)malloc((size * 2 + 1) * sizeof(T)); return body + size; } template <class T> T *negarr2(ll h, ll w) { double **dummy1 = new double *[2 * h + 1]; double *dummy2 = new double[(2 * h + 1) * (2 * w + 1)]; dummy1[0] = dummy2 + w; for (ll i = 1; i <= 2 * h + 1; ++i) { dummy1[i] = dummy1[i - 1] + 2 * w + 1; } double **a = dummy1 + h; return a; } // 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> void inc(T &a, U v = 1) { a += v; } template <class T, class U> void inc(vector<T> &a, U v = 1) { for (auto &u : a) inc(u, v); } template <class T, class U> void dec(T &a, U v = 1) { a -= v; } template <class T, class U> void dec(vector<T> &a, U v = 1) { for (auto &u : a) dec(u, v); } template <class U> void dec(string &a, U v = 1) { for (auto &u : a) dec(u, v); } template <class T> void dec(vector<T> &a) { for (auto &u : a) dec(u, 1); } template <class T, class U> void dec(vector<T> &a, vector<U> &b) { for (auto &u : a) dec(u, 1); for (auto &u : b) dec(u, 1); } template <class T, class U, class W> void dec(vector<T> &a, vector<U> &b, vector<W> &c) { for (auto &u : a) dec(u, 1); for (auto &u : b) dec(u, 1); for (auto &u : c) dec(u, 1); } bool ins(ll h, ll w, ll H, ll W) { return h >= 0 && w >= 0 && h < H && w < W; } bool ins(ll l, ll v, ll r) { return l <= v && v < r; } template <class T> bool ins(vector<T> &a, ll i, ll j = 0) { return ins(0, i, sz(a)) && ins(0, j, sz(a)); } 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() //添え字を返す template <class F> ll goldd_l(ll left, ll right, F calc) { double GRATIO = 1.6180339887498948482045868343656; ll lm = left + (ll)((right - left) / (GRATIO + 1.0)); ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); ll fl = calc(lm); ll fr = calc(rm); while (right - left > 10) { if (fl < fr) { right = rm; rm = lm; fr = fl; lm = left + (ll)((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } ll minScore = MAX(ll); ll resIndex = left; for (ll i = left; i < right + 1; ++i) { ll score = calc(i); if (minScore > score) { minScore = score; resIndex = i; } } return resIndex; } template <class F> ll goldt_l(ll left, ll right, F calc) { double GRATIO = 1.6180339887498948482045868343656; ll lm = left + (ll)((right - left) / (GRATIO + 1.0)); ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); ll fl = calc(lm); ll fr = calc(rm); while (right - left > 10) { if (fl > fr) { right = rm; rm = lm; fr = fl; lm = left + (ll)((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + (ll)((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } if (left > right) { ll l = left; left = right; right = l; } ll maxScore = MIN(ll); ll resIndex = left; for (ll i = left; i < right + 1; ++i) { ll score = calc(i); if (maxScore < score) { maxScore = score; resIndex = i; } } return resIndex; } /*loopは200にすればおそらく大丈夫 余裕なら300に*/ template <class F> dou goldd_d(dou left, dou right, F calc, ll loop = 200) { dou GRATIO = 1.6180339887498948482045868343656; dou lm = left + ((right - left) / (GRATIO + 1.0)); dou rm = lm + ((right - lm) / (GRATIO + 1.0)); dou fl = calc(lm); dou fr = calc(rm); /*200にすればおそらく大丈夫*/ /*余裕なら300に*/ ll k = 141; loop++; while (--loop) { if (fl < fr) { right = rm; rm = lm; fr = fl; lm = left + ((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + ((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } return left; } template <class F> dou goldt_d(dou left, dou right, F calc, ll loop = 200) { double GRATIO = 1.6180339887498948482045868343656; dou lm = left + ((right - left) / (GRATIO + 1.0)); dou rm = lm + ((right - lm) / (GRATIO + 1.0)); dou fl = calc(lm); dou fr = calc(rm); loop++; while (--loop) { if (fl > fr) { right = rm; rm = lm; fr = fl; lm = left + ((right - left) / (GRATIO + 1.0)); fl = calc(lm); } else { left = lm; lm = rm; fl = fr; rm = lm + ((right - lm) / (GRATIO + 1.0)); fr = calc(rm); } } return left; } // l ~ rを複数の区間に分割し、極致を与えるiを返す time-20 msまで探索 template <class F> ll goldd_ls(ll l, ll r, F calc, ll time = 2000) { auto lim = milliseconds(time - 20); ll mini = 0, minv = MAX(ll); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/ ll nl = l; ll nr = l + haba; rep(i, k) { ll ni = goldd_l(nl, nr, calc); if (chmi(minv, calc(ni))) mini = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return mini; } template <class F> ll goldt_ls(ll l, ll r, F calc, ll time = 2000) { auto lim = milliseconds(time - 20); ll maxi = 0, maxv = MIN(ll); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/ ll nl = l; ll nr = l + haba; rep(i, k) { ll ni = goldt_l(nl, nr, calc); if (chma(maxv, calc(ni))) maxi = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return maxi; } template <class F> dou goldd_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を持つ*/ auto lim = milliseconds(time - 20); dou mini = 0, minv = MAX(dou); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); dou haba = (r - l) / k; dou nl = l; dou nr = l + haba; rep(i, k) { dou ni = goldd_d(nl, nr, calc); if (chmi(minv, calc(ni))) mini = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return mini; } template <class F> dou goldt_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を残している*/ auto lim = milliseconds(time - 20); dou maxi = 0, maxv = MIN(dou); /*区間をk分割する*/ rep(k, 1, inf) { auto s = system_clock::now(); dou haba = (r - l) / k; dou nl = l; dou nr = l + haba; rep(i, k) { dou ni = goldt_d(nl, nr, calc); if (chma(maxv, calc(ni))) maxi = ni; nl = nr; nr = nl + haba; } auto end = system_clock::now(); auto part = duration_cast<milliseconds>(end - s); auto elapsed = duration_cast<milliseconds>(end - start_time); if (elapsed + part * 2 >= lim) { break; } } return maxi; } template <class T> T min(vector<vector<T>> &a) { T res = MAX(T); rep(i, a.size()) chmi(res, *min_element(all(a[i]))); return res; } template <class T> T max(vector<vector<T>> &a) { T res = MIN(T); rep(i, a.size()) chma(res, *max_element(all(a[i]))); return res; } constexpr bool bget(ll m, ll keta) { return (m >> keta) & 1; } ll bget(ll m, ll keta, ll sinsuu) { m /= (ll)pow(sinsuu, keta); return m % sinsuu; } ll bit(ll n) { return (1LL << (n)); } ll bit(ll n, ll sinsuu) { return (ll)pow(sinsuu, n); } ll mask(ll n) { return (1ll << n) - 1; } #define bcou __builtin_popcountll //最下位ビット ll lbit(ll n) { return n & -n; } //最上位ビット ll hbit(ll n) { n |= (n >> 1); n |= (n >> 2); n |= (n >> 4); n |= (n >> 8); n |= (n >> 16); n |= (n >> 32); return n - (n >> 1); } ll hbitk(ll n) { ll k = 0; rer(i, 5) { ll a = k + (1ll << i); ll b = 1ll << a; if (b <= n) k += 1ll << i; } return k; } //初期化は0を渡す ll nextComb(ll &mask, ll n, ll r) { if (!mask) return mask = (1LL << r) - 1; ll x = mask & -mask; /*最下位の1*/ ll y = mask + x; /*連続した下の1を繰り上がらせる*/ ll res = ((mask & ~y) / x >> 1) | y; if (bget(res, n)) return mask = 0; else return mask = res; } // n桁以下でビットがr個立っているもののvectorを返す vi bitCombList(ll n, ll r) { vi res; ll m = 0; while (nextComb(m, n, r)) { res.push_back(m); } return res; } // masの立ってるindexを見る #define forbit(i, mas) \ for (int forbitj = lbit(mas), forbitm = mas, i = log2(forbitj); forbitm; \ forbitm = forbitj ? forbitm ^ forbitj : 0, forbitj = lbit(forbitm), \ i = log2(forbitj)) char itoal(ll i) { return 'a' + i; } char itoaL(ll i) { return 'A' + i; } ll altoi(char c) { if ('A' <= c && c <= 'Z') return c - 'A'; return c - 'a'; } ll ctoi(char c) { return c - '0'; } char itoc(ll i) { return i + '0'; } ll vtoi(vi &v) { ll res = 0; if (sz(v) > 18) { debugline("vtoi"); deb(sz(v)); ole(); } rep(i, sz(v)) { res *= 10; res += v[i]; } return res; } vi itov(ll i) { vi res; while (i) { res.push_back(i % 10); i /= 10; } rev(res); return res; } vi stov(string &a) { ll n = sz(a); vi ret(n); rep(i, n) { ret[i] = a[i] - '0'; } return ret; } //基準を満たさないものは0になる vi stov(string &a, char one) { ll n = sz(a); vi ret(n); rep(i, n) ret[i] = a[i] == one; return ret; } vector<vector<ll>> ctoi(vector<vector<char>> s, char c) { ll n = sz(s), m = sz(s[0]); vector<vector<ll>> res(n, vector<ll>(m)); rep(i, n) rep(j, m) res[i][j] = s[i][j] == c; return res; } #define unique(v) v.erase(unique(v.begin(), v.end()), v.end()); //[i] := i番として圧縮されたものを返す vi compress(vi &a) { vi b; ll len = a.size(); for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { a[i] = lower_bound(all(b), a[i]) - b.begin(); } ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, umap<ll, ll> &map) { vi b; ll len = a.size(); for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { ll v = a[i]; a[i] = lower_bound(all(b), a[i]) - b.begin(); map[v] = a[i]; } ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, vi &r) { vi b; ll len = a.size(); fora(v, a) b.push_back(v); fora(v, r) b.push_back(v); sort(b); unique(b); for (ll i = 0; i < len; ++i) a[i] = lower_bound(all(b), a[i]) - b.begin(); for (ll i = 0; i < sz(r); ++i) r[i] = lower_bound(all(b), r[i]) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vi &a, vi &r, vi &s) { vi b; ll len = a.size(); fora(v, a) b.push_back(v); fora(v, r) b.push_back(v); fora(v, s) b.push_back(v); sort(b); unique(b); for (ll i = 0; i < len; ++i) a[i] = lower_bound(all(b), a[i]) - b.begin(); for (ll i = 0; i < sz(r); ++i) r[i] = lower_bound(all(b), r[i]) - b.begin(); for (ll i = 0; i < sz(s); ++i) r[i] = lower_bound(all(b), s[i]) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vector<vi> &a) { vi b; fora(vv, a) fora(v, vv) b.push_back(v); sort(b); unique(b); fora(vv, a) fora(v, vv) v = lower_bound(all(b), v) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } vi compress(vector<vector<vi>> &a) { vi b; fora(vvv, a) fora(vv, vvv) fora(v, vv) b.push_back(v); sort(b); unique(b); fora(vvv, a) fora(vv, vvv) fora(v, vv) v = lower_bound(all(b), v) - b.begin(); ll blen = sz(b); vi ret(blen); rep(i, blen) { ret[i] = b[i]; } return ret; } void compress(ll a[], ll len) { vi b; for (ll i = 0; i < len; ++i) { b.push_back(a[i]); } sort(b); unique(b); for (ll i = 0; i < len; ++i) { a[i] = lower_bound(all(b), a[i]) - b.begin(); } } //要素が見つからなかったときに困る #define binarySearch(a, v) (binary_search(all(a), v)) #define lowerIndex(a, v) (lower_bound(all(a), v) - a.begin()) #define lowerBound(a, v) (*lower_bound(all(a), v)) #define upperIndex(a, v) (upper_bound(all(a), v) - a.begin()) #define upperBound(a, v) (*upper_bound(all(a), v)) #define rlowerIndex(a, v) (upper_bound(all(a), v) - a.begin() - 1) #define rlowerBound(a, v) *(--(upper_bound(all(a), v))) #define rupperIndex(a, v) (lower_bound(all(a), v) - a.begin() - 1) #define rupperBound(a, v) *(--(lower_bound(all(a), v))) #define next2(a) next(next(a)) #define prev2(a) prev(prev(a)) //狭義の単調増加列 長さを返す template <class T> int lis(vector<T> &a) { int n = sz(a); vi tail(n + 1, MAX(T)); rep(i, n) { int id = lowerIndex(tail, a[i]); /**/ tail[id] = a[i]; } return lowerIndex(tail, MAX(T)); } template <class T> int lis_eq(vector<T> &a) { int n = sz(a); vi tail(n + 1, MAX(T)); rep(i, n) { int id = upperIndex(tail, a[i]); /**/ tail[id] = a[i]; } return lowerIndex(tail, MAX(T)); } // iteratorを返す // valueが1以上の物を返す 0は見つけ次第削除 // vを減らす場合 (*it).se--でいい template <class T, class U, class V> auto lower_map(map<T, U> &m, V k) { auto ret = m.lower_bound(k); while (ret != m.end() && (*ret).second == 0) { ret = m.erase(ret); } return ret; } template <class T, class U, class V> auto upper_map(map<T, U> &m, V k) { auto ret = m.upper_bound(k); while (ret != m.end() && (*ret).second == 0) { ret = m.erase(ret); } return ret; } //存在しなければエラー template <class T, class U, class V> auto rlower_map(map<T, U> &m, V k) { auto ret = upper_map(m, k); assert(ret != m.begin()); ret--; while (1) { if ((*ret).second != 0) break; assert(ret != m.begin()); auto next = ret; --next; m.erase(ret); ret = next; } return ret; } template <class T, class U, class V> auto rupper_map(map<T, U> &m, V k) { auto ret = lower_map(m, k); assert(ret != m.begin()); ret--; while (1) { if ((*ret).second != 0) break; assert(ret != m.begin()); auto next = ret; --next; m.erase(ret); ret = next; } return ret; } template <class T> void fin(T s) { cout << s << endl, exit(0); } //便利 数学 math ll mod(ll a, ll m) { return (a % m + m) % m; } ll pow(ll a) { return a * a; }; ll fact(ll v) { return v <= 1 ? 1 : v * fact(v - 1); } dou factd(int v) { static vd fact(2, 1); if (sz(fact) <= v) { rep(i, sz(fact), v + 1) { fact.push_back(fact.back() * i); } } return fact[v]; } ll comi(ll n, ll r) { assert(n < 100); static vvi(pas, 100, 100); if (pas[0][0]) return pas[n][r]; pas[0][0] = 1; rep(i, 1, 100) { pas[i][0] = 1; rep(j, 1, i + 1) pas[i][j] = pas[i - 1][j - 1] + pas[i - 1][j]; } return pas[n][r]; } double comd2(ll n, ll r) { static vvd(comb, 2020, 2020); if (comb[0][0] == 0) { comb[0][0] = 1; rep(i, 2000) { comb[i + 1][0] = 1; rep(j, 1, i + 2) { comb[i + 1][j] = comb[i][j] + comb[i][j - 1]; } } } return comb[n][r]; } double comd(int n, int r) { if (r < 0 || r > n) return 0; if (n < 2020) return comd2(n, r); static vd fact(2, 1); if (sz(fact) <= n) { rep(i, sz(fact), n + 1) { fact.push_back(fact.back() * i); } } return fact[n] / fact[n - r] / fact[r]; } ll gcd(ll a, ll b) { while (b) a %= b, swap(a, b); return abs(a); } ll gcd(vi b) { ll res = b[0]; rep(i, 1, sz(b)) res = gcd(b[i], res); return res; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } ll lcm(vi a) { ll res = a[0]; rep(i, 1, sz(a)) res = lcm(a[i], res); return res; } ll ceil(ll a, ll b) { if (b == 0) { debugline("ceil"); deb(a, b); ole(); return -1; } else if (a < 0) { return 0; } else { return (a + b - 1) / b; } } ll lower_remi__bx_a(ll kei, ll rem, ll x) { if (rem >= x) return 0; return (x - rem + kei - 1) / kei; } ll lower_remv__bx_a(ll kei, ll rem, ll x) { if (rem >= x) return rem; return (x - rem + kei - 1) / kei * kei + rem; } ll upper_remi__bx_a(ll kei, ll rem, ll x) { if (rem > x) return 0; return (x - rem + kei) / kei; } ll upper_remv__bx_a(ll kei, ll rem, ll x) { if (rem > x) return rem; return (x - rem + kei) / kei * kei + rem; } // v * v >= aとなる最小のvを返す 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) >> 1; } ll sig(ll s, ll t) { return ((s + t) * (t - s + 1)) >> 1; } //機能拡張 template <class T, class U> void operator+=(pair<T, U> &a, pair<T, U> &b) { a.fi += b.fi; a.se += b.se; } template <class T, class U> pair<T, U> operator+(pair<T, U> &a, pair<T, U> &b) { return pair<T, U>(a.fi + b.fi, a.se + b.se); } template <typename CharT, typename Traits, typename Alloc> basic_string<CharT, Traits, Alloc> operator+(const basic_string<CharT, Traits, Alloc> &lhs, const int rv) { basic_string<CharT, Traits, Alloc> str(lhs); str.append(to_string(rv)); return str; } template <typename CharT, typename Traits, typename Alloc> void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const int rv) { lhs += to_string(rv); } template <typename CharT, typename Traits, typename Alloc> basic_string<CharT, Traits, Alloc> operator+(const basic_string<CharT, Traits, Alloc> &lhs, const signed rv) { basic_string<CharT, Traits, Alloc> str(lhs); str.append(to_string(rv)); return str; } template <typename CharT, typename Traits, typename Alloc> void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const signed rv) { lhs += to_string(rv); } template <class T, class U> void operator+=(queue<T> &a, U v) { a.push(v); } template <class T, class U> void operator+=(deque<T> &a, U v) { a.push_back(v); } template <class T> priority_queue<T, vector<T>, greater<T>> & operator+=(priority_queue<T, vector<T>, greater<T>> &a, vector<T> &v) { fora(d, v) a.push(d); return a; } template <class T, class U> priority_queue<T, vector<T>, greater<T>> & operator+=(priority_queue<T, vector<T>, greater<T>> &a, U v) { a.push(v); return a; } template <class T, class U> priority_queue<T> &operator+=(priority_queue<T> &a, U v) { a.push(v); return a; } template <class T> set<T> &operator+=(set<T> &a, vector<T> v) { fora(d, v) a.insert(d); return a; } template <class T, class U> auto operator+=(set<T> &a, U v) { return a.insert(v); } template <class T, class U> auto operator-=(set<T> &a, U v) { return a.erase(v); } template <class T, class U> auto operator+=(mset<T> &a, U v) { return a.insert(v); } template <class T, class U> set<T, greater<T>> &operator+=(set<T, greater<T>> &a, U v) { a.insert(v); return a; } template <class T, class U> vector<T> &operator+=(vector<T> &a, U v) { a.push_back(v); return a; } template <class T, class U> vector<T> operator+(const vector<T> &a, U v) { vector<T> ret = a; ret += v; return ret; } template <class T, class U> vector<T> operator+(U v, const vector<T> &a) { vector<T> ret = a; ret.insert(ret.begin(), v); return ret; } template <class T> vector<T> operator+(vector<T> a, vector<T> b) { vector<T> ret; ret = a; fora(v, b) ret += v; return ret; } template <class T> vector<T> &operator+=(vector<T> &a, vector<T> &b) { fora(v, b) a += v; return a; } template <class T> vector<T> &operator-=(vector<T> &a, vector<T> &b) { if (sz(a) != sz(b)) { debugline("vector<T> operator-="); deb(a); deb(b); exit(0); } rep(i, sz(a)) a[i] -= b[i]; return a; } template <class T> vector<T> operator-(vector<T> &a, vector<T> &b) { if (sz(a) != sz(b)) { debugline("vector<T> operator-"); deb(a); deb(b); ole(); } vector<T> res(sz(a)); rep(i, sz(a)) res[i] = a[i] - b[i]; return res; } template <class T, class U> vector<T> operator*(vector<T> &a, U b) { vector<T> ret; fora(v, a) ret += v * b; return ret; } template <class T, class U> vector<T> operator/(vector<T> &a, U b) { vector<T> ret; fora(v, a) ret += v / b; return ret; } template <class T, class U> vector<T> operator*=(vector<T> &a, U b) { fora(v, a) v *= b; return a; } template <class T, class U> vector<T> operator/=(vector<T> &a, U b) { fora(v, a) v /= b; return a; } template <typename T> void erase(vector<T> &v, unsigned ll i) { v.erase(v.begin() + i); } template <typename T> void erase(vector<T> &v, unsigned ll s, unsigned ll e) { v.erase(v.begin() + s, v.begin() + e); } template <class T, class U> void erase(map<T, U> &m, ll okl, ll ngr) { m.erase(m.lower_bound(okl), m.lower_bound(ngr)); } template <class T> void erase(set<T> &m, ll okl, ll ngr) { m.erase(m.lower_bound(okl), m.lower_bound(ngr)); } template <typename T> void erasen(vector<T> &v, unsigned ll s, unsigned ll n) { v.erase(v.begin() + s, v.begin() + s + n); } template <typename T, typename U> void insert(vector<T> &v, unsigned ll i, U t) { v.insert(v.begin() + i, t); } template <typename T, typename U> void push_front(vector<T> &v, U t) { v.insert(v.begin(), t); } template <typename T, typename U> void insert(vector<T> &v, unsigned ll i, vector<T> list) { for (auto &&va : list) v.insert(v.begin() + i++, va); } template <typename T> void insert(set<T> &v, vector<T> list) { for (auto &&va : list) v.insert(va); } vector<string> split(const string a, const char deli) { string b = a + deli; ll l = 0, r = 0, n = b.size(); vector<string> res; rep(i, n) { if (b[i] == deli) { r = i; if (l < r) res.push_back(b.substr(l, r - l)); l = i + 1; } } return res; } vector<string> split(const string a, const string deli) { vector<string> res; ll kn = sz(deli); std::string::size_type Pos(a.find(deli)); ll l = 0; while (Pos != std::string::npos) { if (Pos - l) res.push_back(a.substr(l, Pos - l)); l = Pos + kn; Pos = a.find(deli, Pos + kn); } if (sz(a) - l) res.push_back(a.substr(l, sz(a) - l)); return res; } 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 <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)}; } //@formatter:off template <typename T> T minv(T a, T m); template <typename T> T minv(T a); template <typename T> class Modular { public: using Type = typename decay<decltype(T::value)>::type; constexpr Modular() : value() {} template <typename U> Modular(const U &x) { value = normalize(x); } template <typename U> static Type normalize(const U &x) { Type v; if (-mod() <= x && x < mod()) v = static_cast<Type>(x); else v = static_cast<Type>(x % mod()); if (v < 0) v += mod(); return v; } const Type &operator()() const { return value; } template <typename U> explicit operator U() const { return static_cast<U>(value); } constexpr static Type mod() { return T::value; } Modular &operator+=(const Modular &other) { if ((value += other.value) >= mod()) value -= mod(); return *this; } Modular &operator-=(const Modular &other) { if ((value -= other.value) < 0) value += mod(); return *this; } template <typename U> Modular &operator+=(const U &other) { return *this += Modular(other); } template <typename U> Modular &operator-=(const U &other) { return *this -= Modular(other); } Modular &operator++() { return *this += 1; } Modular &operator--() { return *this -= 1; } Modular operator++(signed) { Modular result(*this); *this += 1; return result; } Modular operator--(signed) { Modular result(*this); *this -= 1; return result; } Modular operator-() const { return Modular(-value); } template <typename U = T> typename enable_if<is_same<typename Modular<U>::Type, signed>::value, Modular>::type & operator*=(const Modular &rhs) { #ifdef _WIN32 uint64_t x = static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value); uint32_t xh = static_cast<uint32_t>(x >> 32), xl = static_cast<uint32_t>(x), d, m; asm("divl %4; \n\t" : "=a"(d), "=d"(m) : "d"(xh), "a"(xl), "r"(mod())); value = m; #else value = normalize(static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value)); #endif return *this; } template <typename U = T> typename enable_if<is_same<typename Modular<U>::Type, int64_t>::value, Modular>::type & operator*=(const Modular &rhs) { int64_t q = static_cast<int64_t>(static_cast<double>(value) * rhs.value / mod()); value = normalize(value * rhs.value - q * mod()); return *this; } template <typename U = T> typename enable_if<!is_integral<typename Modular<U>::Type>::value, Modular>::type & operator*=(const Modular &rhs) { value = normalize(value * rhs.value); return *this; } Modular &operator/=(const Modular &other) { return *this *= Modular(minv(other.value)); } template <typename U> friend bool operator==(const Modular<U> &lhs, const Modular<U> &rhs); template <typename U> friend bool operator<(const Modular<U> &lhs, const Modular<U> &rhs); template <typename U> friend std::istream &operator>>(std::istream &stream, Modular<U> &number); operator int() { return value; } private: Type value; }; template <typename T> bool operator==(const Modular<T> &lhs, const Modular<T> &rhs) { return lhs.value == rhs.value; } template <typename T, typename U> bool operator==(const Modular<T> &lhs, U rhs) { return lhs == Modular<T>(rhs); } template <typename T, typename U> bool operator==(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) == rhs; } template <typename T> bool operator!=(const Modular<T> &lhs, const Modular<T> &rhs) { return !(lhs == rhs); } template <typename T, typename U> bool operator!=(const Modular<T> &lhs, U rhs) { return !(lhs == rhs); } template <typename T, typename U> bool operator!=(U lhs, const Modular<T> &rhs) { return !(lhs == rhs); } template <typename T> bool operator<(const Modular<T> &lhs, const Modular<T> &rhs) { return lhs.value < rhs.value; } template <typename T> Modular<T> operator+(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) += rhs; } template <typename T, typename U> Modular<T> operator+(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) += rhs; } template <typename T, typename U> Modular<T> operator+(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) += rhs; } template <typename T> Modular<T> operator-(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) -= rhs; } template <typename T, typename U> Modular<T> operator-(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) -= rhs; } template <typename T, typename U> Modular<T> operator-(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) -= rhs; } template <typename T> Modular<T> operator*(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) *= rhs; } template <typename T, typename U> Modular<T> operator*(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) *= rhs; } template <typename T, typename U> Modular<T> operator*(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) *= rhs; } template <typename T> Modular<T> operator/(const Modular<T> &lhs, const Modular<T> &rhs) { return Modular<T>(lhs) /= rhs; } template <typename T, typename U> Modular<T> operator/(const Modular<T> &lhs, U rhs) { return Modular<T>(lhs) /= rhs; } template <typename T, typename U> Modular<T> operator/(U lhs, const Modular<T> &rhs) { return Modular<T>(lhs) /= rhs; } constexpr signed MOD = // 998244353; 1e9 + 7; // MOD using mint = Modular<std::integral_constant<decay<decltype(MOD)>::type, MOD>>; constexpr int mint_len = 1400001; vi fac, finv, inv; vi p2; mint com(int n, int r) { if (r < 0 || r > n) return 0; return mint(finv[r] * fac[n] % MOD * finv[n - r]); } mint pom(int n, int r) { /* if (!sz(fac)) com(0, -1);*/ if (r < 0 || r > n) return 0; return mint(fac[n] * finv[n - 1]); } mint npr(int n, int r) { /* if (!sz(fac)) com(0, -1);*/ if (r < 0 || r > n) return 0; return mint(fac[n] * finv[n - r]); } int nprin(int n, int r) { /* if (!sz(fac)) com(0, -1);*/ if (r < 0 || r > n) return 0; return fac[n] * finv[n - r] % MOD; } int icom(int n, int r) { const int NUM_ = 1400001; static ll fac[NUM_ + 1], finv[NUM_ + 1], inv[NUM_ + 1]; if (fac[0] == 0) { inv[1] = fac[0] = finv[0] = 1; for (int i = 2; i <= NUM_; ++i) inv[i] = inv[MOD % i] * (MOD - MOD / i) % MOD; for (int i = 1; i <= NUM_; ++i) fac[i] = fac[i - 1] * i % MOD, finv[i] = finv[i - 1] * inv[i] % MOD; } if (r < 0 || r > n) return 0; return ((finv[r] * fac[n] % MOD) * finv[n - r]) % MOD; } #define ncr com #define ncri icom // n個の場所にr個の物を置く mint nhr(int n, int r) { return com(n + r - 1, r); } mint hom(int n, int r) { return com(n + r - 1, r); } int nhri(int n, int r) { return icom(n + r - 1, r); } template <typename T> T minv(T a, T m) { T u = 0, v = 1; while (a != 0) { T t = m / a; m -= t * a; swap(a, m); u -= t * v; swap(u, v); } assert(m == 1); return u; } template <typename T> T minv(T a) { if (a < mint_len) return inv[a]; T u = 0, v = 1; T m = MOD; while (a != 0) { T t = m / a; m -= t * a; swap(a, m); u -= t * v; swap(u, v); } assert(m == 1); return u; } template <typename T, typename U> Modular<T> mpow(const Modular<T> &a, const U &b) { assert(b >= 0); int x = a(), res = 1; U p = b; while (p > 0) { if (p & 1) (res *= x) %= MOD; (x *= x) %= MOD; p >>= 1; } return res; } template <typename T, typename U, typename V> mint mpow(const T a, const U b, const V m = MOD) { assert(b >= 0); int x = a, res = 1; U p = b; while (p > 0) { if (p & 1) (res *= x) %= m; (x *= x) %= m; p >>= 1; } return res; } template <typename T, typename U> mint mpow(const T a, const U b) { assert(b >= 0); int x = a, res = 1; U p = b; while (p > 0) { if (p & 1) (res *= x) %= MOD; (x *= x) %= MOD; p >>= 1; } return res; } template <typename T, typename U, typename V> int mpowi(const T &a, const U &b, const V &m = MOD) { assert(b >= 0); int x = a, res = 1; U p = b; while (p > 0) { if (p & 1) (res *= x) %= m; (x *= x) %= m; p >>= 1; } return res; } template <typename T> string to_string(const Modular<T> &number) { return to_string(number()); } string yuri(const mint &a) { stringstream st; rep(i, 300) { rep(j, 300) { if ((mint)i / j == a) { st << i << " / " << j; return st.str(); } } } rep(i, 1000) { rep(j, 1000) { if ((mint)i / j == a) { st << i << " / " << j; return st.str(); } } } return st.str(); } template <typename T> std::ostream &operator<<(std::ostream &stream, const Modular<T> &number) { stream << number(); #ifdef _DEBUG // stream << " -> " << yuri(number); #endif return stream; } //@formatter:off template <typename T> std::istream &operator>>(std::istream &stream, Modular<T> &number) { typename common_type<typename Modular<T>::Type, int64_t>::type x; stream >> x; number.value = Modular<T>::normalize(x); return stream; } using PM = pair<mint, mint>; using vm = vector<mint>; using mapm = map<int, mint>; using umapm = umap<int, mint>; #define vvm(...) \ o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(mint, __VA_ARGS__) #define vnm(name, ...) auto name = make_v<mint>(__VA_ARGS__) //@formatter:on // nhr n個の場所にr個の物を分ける //@起動時 struct initon { initon() { cin.tie(0); ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(16); srand((unsigned)clock() + (unsigned)time(NULL)); }; } initonv; //@formatter:on // gra mll pr //上下左右 const string udlr = "udlr"; string UDLR = "UDLR"; // x4と連動 UDLR.find('U') := x4[0] //右、上が正 constexpr ll y4[] = {1, -1, 0, 0}; constexpr ll x4[] = {0, 0, -1, 1}; constexpr ll y8[] = {0, 1, 0, -1, -1, 1, 1, -1}; constexpr ll x8[] = {1, 0, -1, 0, 1, -1, 1, -1}; ll n, m, k, K, d, H, W, x, y, z, q, Q; ll cou; vi a, b, c; // vvi (s, 0, 0); vvc(ba, 0, 0); vp p; str s; //一番高速 // hashを共有しない class roll_fast_only { static constexpr uint64_t base = 362923; //他のロリハとhashを共有したい場合 // const uint64_t base = rand((uint64_t) 2, mask61 - // 1);//共有したくない場合 static constexpr uint64_t mask30 = ((uint64_t)1 << 30) - 1; static constexpr uint64_t mask31 = ((uint64_t)1 << 31) - 1; static constexpr uint64_t mask61 = ((uint64_t)1 << 61) - 1; static constexpr uint64_t offset = mask61 * 7; static constexpr uint64_t mod(const uint64_t x) { const uint64_t y = (x >> 61) + (x & mask61); return y >= mask61 ? y - mask61 : y; } static constexpr uint64_t mul(const uint64_t x, const uint64_t y) { const uint64_t xh = x >> 31, xl = x & mask31; const uint64_t yh = y >> 31, yl = y & mask31; const uint64_t z = xl * yh + xh * yl; const uint64_t zh = z >> 30, zl = z & mask30; return xh * yh * 2 + zh + (zl << 31) + xl * yl; } const std::size_t siz; vector<uint64_t> pow, h; public: template <typename T> roll_fast_only(T &A) : siz(sz(A)), pow(siz + 1, 1), h(siz + 1, 0) { for (std::size_t i = 1; i <= siz; i++) { pow[i] = mod(mul(pow[i - 1], base)), h[i] = mod(mul(h[i - 1], base) + A[i - 1]); } } uint64_t operator()(const std::size_t l, const std::size_t r) { return mod(h[r] + offset - mul(h[l], pow[r - l])); } uint64_t operator()(int r = inf) { return operator()(0, min(siz, r)); } inline int lcp_dup(int a, int b) { int len = min((int)siz - a, (int)siz - b); int low = 0, high = len; while (high - low > 1) { int mid = (low + high) >> 1; if (operator()(a, a + mid) != operator()(b, b + mid)) high = mid; else low = mid; } return low; } inline int lcp_not(int a, int b) { return min(lcp_dup(a, b), b - a); } //@formatter:on }; //動かない場合roll_fast_only2 // roll_fast_only ro(list); // lcpならsa := saffix_arrayでO(1) 構成O(N log(n)^2) void solve() { in(n); in(s); roll_fast_only ro(s); rer(len, n - 1, 1) { // umapu m; unordered_map<uint64_t, int> m; rep(i, n - len + 1) { auto has = ro(i, i + len); if (m.find(has) != m.end()) { if (m[has] <= i) fin(len); } else m[has] = i + len; } } fin(0); } auto my(ll n, vi &a) { return 0; } auto sister(ll n, vi &a) { ll ret = 0; return ret; } signed main() { solve(); #define arg n, a #ifdef _DEBUG bool bad = 0; for (ll i = 0, ok = 1; i < k5 && ok; ++i) { ll n = rand(1, 8); vi a = ranv(n, 1, 10); auto myres = my(arg); auto res = sister(arg); ok = myres == res; if (!ok) { out(arg); cerr << "AC : " << res << endl; cerr << "MY : " << myres << endl; bad = 1; break; } } if (!bad) { // cout << "完璧 : solveを書き直そう" << endl; // cout << " : そして、solve()を呼び出すのだ" << endl; // cout << " : cin>>n; na(a,n);も忘れるな" << endl; } #endif return 0; };
[ "literal.number.change" ]
744,133
744,135
u986437843
cpp
p02913
#include <algorithm> #include <cmath> #include <functional> #include <iostream> #include <numeric> #include <string> #include <vector> #define REP(i, n) for (int i = 0; i < n; i++) #define REPR(i, n) for (int i = n; i >= 0; i--) #define FOR(i, m, n) for (int i = m; i < n; i++) #define INF 2e9 #define ALL(v) v.begin(), v.end() using namespace std; typedef long long ll; typedef pair<int, int> P; typedef vector<vector<int>> vvi; typedef vector<vector<ll>> vll; typedef pair<ll, ll> pll; const ll inf = 1LL << 60; int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); // cout << fixed << setprecision(15); int N; cin >> N; string S; cin >> S; vvi dp(N, vector<int>(N + 1, -1)); int maxi = 0; REP(i, N - 1) { FOR(k, i + 1, N) { if (dp[i][k] != -1) continue; int nowi = i; int nowk = k; int itr = 0; while (S[nowi] == S[nowk]) { nowi++; nowk++; itr++; if (nowk >= N || nowi >= k) break; } dp[i][k] = itr; maxi = max(maxi, itr); for (int j = 1; j <= itr; j++) { dp[i + j][k] = 0; } } } cout << maxi << endl; }
#include <algorithm> #include <cmath> #include <functional> #include <iostream> #include <numeric> #include <string> #include <vector> #define REP(i, n) for (int i = 0; i < n; i++) #define REPR(i, n) for (int i = n; i >= 0; i--) #define FOR(i, m, n) for (int i = m; i < n; i++) #define INF 2e9 #define ALL(v) v.begin(), v.end() using namespace std; typedef long long ll; typedef pair<int, int> P; typedef vector<vector<int>> vvi; typedef vector<vector<ll>> vll; typedef pair<ll, ll> pll; const ll inf = 1LL << 60; int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); // cout << fixed << setprecision(15); int N; cin >> N; string S; cin >> S; vvi dp(N, vector<int>(N + 1, -1)); int maxi = 0; REP(i, N - 1) { FOR(k, i + 1, N) { if (dp[i][k] != -1) continue; int nowi = i; int nowk = k; int itr = 0; while (S[nowi] == S[nowk]) { nowi++; nowk++; itr++; if (nowk >= N || nowi >= k) break; } dp[i][k] = itr; maxi = max(maxi, itr); for (int j = 1; j < itr; j++) { dp[i + j][k + j] = 0; } } } cout << maxi << endl; }
[ "expression.operator.compare.change", "control_flow.loop.for.condition.change", "expression.off_by_one", "expression.operation.binary.change", "assignment.change" ]
744,145
744,146
u211259428
cpp
p02913
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define rep(i, n) for (int i = 0; i < (n); ++i) #define rrep(i, n) for (int i = 1; i <= (n); ++i) #define drep(i, n) for (int i = (n)-1; i >= 0; --i) using namespace std; const int INF = 1001001001; const long long LINF = 1001002003004005006ll; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } typedef long long ll; typedef pair<int, int> P; int dp[5005][5005]; int main() { int n; cin >> n; string s; cin >> s; dp[0][0] = 0; rrep(i, n) { rrep(j, n) { if (i <= j) continue; if (s[i] == s[j]) dp[i][j] = dp[i - 1][j - 1] + 1; } } int ans = 0; rrep(i, n) { rrep(j, n) { if (i <= j) continue; chmax(ans, min(dp[i][j], i - j)); } } cout << ans << endl; }
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define rep(i, n) for (int i = 0; i < (n); ++i) #define rrep(i, n) for (int i = 1; i <= (n); ++i) #define drep(i, n) for (int i = (n)-1; i >= 0; --i) using namespace std; const int INF = 1001001001; const long long LINF = 1001002003004005006ll; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } typedef long long ll; typedef pair<int, int> P; int dp[5005][5005]; int main() { int n; cin >> n; string s; cin >> s; dp[0][0] = 0; rrep(i, n) { rrep(j, n) { if (i <= j) continue; if (s[i - 1] == s[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1; } } int ans = 0; rrep(i, n) { rrep(j, n) { if (i <= j) continue; chmax(ans, min(dp[i][j], i - j)); } } cout << ans << endl; }
[ "control_flow.branch.if.condition.change" ]
744,147
744,148
u639032323
cpp
p02913
#include <bits/stdc++.h> using namespace std; inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } typedef long long ll; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<long long> VL; typedef vector<vector<long long>> VVL; typedef vector<string> VS; typedef pair<int, int> P; typedef tuple<int, int, int> tpl; #define ALL(a) (a).begin(), (a).end() #define SORT(c) sort((c).begin(), (c).end()) #define REVERSE(c) reverse((c).begin(), (c).end()) #define EXIST(m, v) (m).find((v)) != (m).end() #define LB(a, x) lower_bound((a).begin(), (a).end(), x) - (a).begin() #define UB(a, x) upper_bound((a).begin(), (a).end(), x) - (a).begin() #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) #define RFOR(i, a, b) for (int i = (a)-1; i >= (b); --i) #define RREP(i, n) RFOR(i, n, 0) #define en "\n" constexpr double EPS = 1e-9; constexpr double PI = 3.1415926535897932; constexpr int INF = 2147483647; constexpr long long LINF = 1LL << 60; constexpr long long MOD = 1000000007; // 998244353; #define dump(x) cerr << #x << " = " << (x) << endl; #define debug(x) \ cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } struct RollingHash { const long long MASK30 = (1LL << 30) - 1; const long long MASK31 = (1LL << 31) - 1; const long long MOD = (1LL << 61) - 1; const long long MASK61 = MOD; int N; vector<long long> hs, b; long long base; RollingHash(string &s, long long base_ = -1) { if (base_ == -1) { random_device rnd; // 非決定的な乱数生成器 mt19937 mt(rnd()); base = mt() + 1; } else { base = base_; } N = s.size(); hs.resize(N + 1); b.resize(N + 1); b[0] = 1; hs[0] = 0; for (int i = 0; i < N; i++) { b[i + 1] = Mul(b[i], base); hs[i + 1] = Mul(hs[i], base) + s[i]; if (hs[i + 1] >= MOD) hs[i + 1] -= MOD; } } long long get(int l, int r) { long long ret = hs[r] + MOD - Mul(hs[l], b[r - l]); if (ret >= MOD) ret -= MOD; return ret; } // mod 2^61-1を計算する関数 long long CalcMod(long long x) { long long xu = x >> 61; long long xd = x & MASK61; long long res = xu + xd; if (res >= MOD) res -= MOD; return res; } // a*b mod 2^61-1を返す関数(最後にModを取る) long long Mul(long long a, long long b) { long long au = a >> 31; long long ad = a & MASK31; long long bu = b >> 31; long long bd = b & MASK31; long long mid = ad * bu + au * bd; long long midu = mid >> 30; long long midd = mid & MASK30; return CalcMod(au * bu * 2 + midu + (midd << 31) + ad * bd); } }; void Main() { int N; cin >> N; string S; cin >> S; RollingHash rh(S); int l = 0, r = (N + 1) / 2; while (r - l > 1) { int k = (l + r) / 2; map<ll, VI> m; REP(i, N - k + 1) { ll hash = rh.get(i, i + k); m[hash].push_back(i); } bool flag = false; for (auto &p : m) { VI v = p.second; int K = v.size(); REP(i, K - 1) { if (v.back() - v[i] >= k) flag = true; } } if (flag) l = k; else r = k; } cout << l << en; return; } int main(void) { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); cout << fixed << setprecision(15); int t = 1; // cin>>t; REP(_, t) Main(); return 0; }
#include <bits/stdc++.h> using namespace std; inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } typedef long long ll; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<long long> VL; typedef vector<vector<long long>> VVL; typedef vector<string> VS; typedef pair<int, int> P; typedef tuple<int, int, int> tpl; #define ALL(a) (a).begin(), (a).end() #define SORT(c) sort((c).begin(), (c).end()) #define REVERSE(c) reverse((c).begin(), (c).end()) #define EXIST(m, v) (m).find((v)) != (m).end() #define LB(a, x) lower_bound((a).begin(), (a).end(), x) - (a).begin() #define UB(a, x) upper_bound((a).begin(), (a).end(), x) - (a).begin() #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i, 0, n) #define RFOR(i, a, b) for (int i = (a)-1; i >= (b); --i) #define RREP(i, n) RFOR(i, n, 0) #define en "\n" constexpr double EPS = 1e-9; constexpr double PI = 3.1415926535897932; constexpr int INF = 2147483647; constexpr long long LINF = 1LL << 60; constexpr long long MOD = 1000000007; // 998244353; #define dump(x) cerr << #x << " = " << (x) << endl; #define debug(x) \ cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } struct RollingHash { const long long MASK30 = (1LL << 30) - 1; const long long MASK31 = (1LL << 31) - 1; const long long MOD = (1LL << 61) - 1; const long long MASK61 = MOD; int N; vector<long long> hs, b; long long base; RollingHash(string &s, long long base_ = -1) { if (base_ == -1) { random_device rnd; // 非決定的な乱数生成器 mt19937 mt(rnd()); base = mt() + 1; } else { base = base_; } N = s.size(); hs.resize(N + 1); b.resize(N + 1); b[0] = 1; hs[0] = 0; for (int i = 0; i < N; i++) { b[i + 1] = Mul(b[i], base); hs[i + 1] = Mul(hs[i], base) + s[i]; if (hs[i + 1] >= MOD) hs[i + 1] -= MOD; } } long long get(int l, int r) { long long ret = hs[r] + MOD - Mul(hs[l], b[r - l]); if (ret >= MOD) ret -= MOD; return ret; } // mod 2^61-1を計算する関数 long long CalcMod(long long x) { long long xu = x >> 61; long long xd = x & MASK61; long long res = xu + xd; if (res >= MOD) res -= MOD; return res; } // a*b mod 2^61-1を返す関数(最後にModを取る) long long Mul(long long a, long long b) { long long au = a >> 31; long long ad = a & MASK31; long long bu = b >> 31; long long bd = b & MASK31; long long mid = ad * bu + au * bd; long long midu = mid >> 30; long long midd = mid & MASK30; return CalcMod(au * bu * 2 + midu + (midd << 31) + ad * bd); } }; void Main() { int N; cin >> N; string S; cin >> S; RollingHash rh(S); int l = 0, r = (N + 1) / 2 + 1; while (r - l > 1) { int k = (l + r) / 2; map<ll, VI> m; REP(i, N - k + 1) { ll hash = rh.get(i, i + k); m[hash].push_back(i); } bool flag = false; for (auto &p : m) { VI v = p.second; int K = v.size(); REP(i, K - 1) { if (v.back() - v[i] >= k) flag = true; } } if (flag) l = k; else r = k; } cout << l << en; return; } int main(void) { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); cout << fixed << setprecision(15); int t = 1; // cin>>t; REP(_, t) Main(); return 0; }
[ "assignment.change" ]
744,149
744,150
u934272719
cpp
p02913
#include <algorithm> #include <bits/stdc++.h> #include <cmath> #include <complex> #include <cstdio> #include <iostream> #include <map> #include <queue> #include <set> #include <utility> #include <vector> #define fir first #define sec second #define sz(s) (s).size() #define pb push_back #define get(n) scanf("%d", &n); #define gets(s) \ string s; \ cin >> (s); #define prfi(n) printf("%d", &n); #define prfd(n) printf("%lf", &n); #define All(s) (s).begin(), (s).end() #define rep(i, j) for (int(i) = 0; (i) < (j); (i)++) #define For(i, j, k) for (int(i) = (j); (i) < (k); (i)++) #define drep(i, j) for (int(i) = (j); (i) >= 0; (i)--) #define Ford(i, j, k) for (int(i) = (j); i >= (k); i--) #define fore(c, v) for (auto(c) : (v)) #define lp for (int __ = 0; __ < n; i++) #define mem(a, b) memset(a, b, sizeof(a)); #define dump(x) std::cout << #x << " = " << (x) << std::endl; #define debug(x) \ cout << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl; using ull = unsigned long long int; using ll = long long; using ld = long double; using pii = std::pair<int, int>; using pll = std::pair<ll, ll>; using vi = std::vector<int>; using vvi = std::vector<vi>; using vll = std::vector<ll>; using vvll = std::vector<vll>; using vd = std::vector<double>; using vvd = std::vector<vd>; using qi = std::queue<int>; using vpii = std::vector<std::pair<int, int>>; using vpll = std::vector<pll>; using namespace std; const int Mod = (1e9) + 7; const int INF = 1e9 + 19; const ll INFL = 1e18 + 19; const int dx[] = {-1, 0, 0, 1}; const int dy[] = {0, -1, 1, 0}; const int dx2[] = {-1, -1, -1, 0, 0, 0, 1, 1, 1}; const int dy2[] = {1, 0, -1, 1, 0, -1, 1, 0, -1}; const double EPS = 1e-10; //_____________________________________Templates_________________________________________// template <class T1, class T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; } template <class T1, class T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; } template <class T> inline void pri(T a) { cout << a << endl; } template <class Z> using vec = vector<Z>; template <class T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>; // mainly use for dynamic prog template <class T1, class T2> void update(T1 &a, T2 b) { a += b; if (a > Mod) a %= Mod; } inline void IN(void) { return; } template <typename First, typename... Rest> void IN(First &first, Rest &...rest) { cin >> first; IN(rest...); return; } inline void OUT(void) { cout << "\n"; return; } template <typename First, typename... Rest> void OUT(First first, Rest... rest) { cout << first << " "; OUT(rest...); return; } bool pairsort(pll pl, pll pr) { if (pl.first == pr.first) return pl.second > pr.second; return pl.first < pr.first; } int cntbit(ll a, int n, int j) { int res = 0; For(i, j, n) { if (a >> i & 1) { res++; } } return res; } vector<int> make_bit(int a) { vector<int> res; for (int i = 31; i >= 0; i--) if (a & (1 << i)) res.pb(i); return res; } bool stdbit(int a, int b) { return a & (1 << b); } int GCD(int a, int b) { if (b > a) return GCD(b, a); if (a % b == 0) return b; else return GCD(b, a % b); } int LCM(int a, int b) { return a * b / GCD(a, b); } int roundup(int a, int b) { if (a % b == 0) return a / b; else return (a + b) / b; } int rounddown(int a, int b) { if (a % b == 0) return a / b; else { return (a - b) / b; } } ll pow(ll a, ll n) { ll res = 1; while (n > 0) { if (n & 1) res *= a; a *= a; n = n >> 1; } return res; } ll GetDiviserCount(ll N) //約数の個数 { ll res = 1; For(i, 2, sqrt(N) + 1) { ll cnt = 0; while (N % i == 0) { cnt++; N /= i; } res *= (cnt + 1); if (N == 1) break; } if (N != 1) res *= 2; return res; } vll GetDivisor(ll N) //約数列挙 { vll res; for (ll i = 1; i * i <= N; i++) { if (N % i == 0) { res.pb(i); if (i * i != N) res.pb(N / i); } } sort(All(res)); return res; } struct Modint { using ll = long long int; ll x; Modint(ll x = 0) : x((x % Mod + Mod) % Mod) {} Modint operator-() { return Modint(-x); } Modint &operator+=(Modint a) { (x += a.x % Mod) % Mod; return *this; } Modint &operator-=(Modint a) { x = (x - a.x + Mod) % Mod; return *this; } Modint &operator*=(Modint a) { (x *= a.x) %= Mod; return *this; } Modint operator+(Modint a) { Modint b(*this); b += a; return b; } Modint operator-(Modint a) { Modint b(*this); b -= a; return b; } Modint operator*(Modint a) { Modint b(*this); b *= a; return b; } long long int EXTGCD(long long int a, long long int b, long long int &x, long long int &y) { if (b == 0) { x = 1; y = 0; return a; } long long int g = EXTGCD(b, a % b, y, x); y -= (a / b) * x; return g; } Modint inverse() { long long int a, b; EXTGCD(x, Mod, a, b); (a += Mod) %= Mod; return a; } Modint pow(ll a) { Modint res = 1; Modint b = x; while (a > 0) { if (a & 1) res *= b; a = a >> 1; b *= b; } return res; } friend ostream &operator<<(ostream &os, const Modint &a); }; ostream &operator<<(ostream &os, const Modint &a) { os << a.x; return os; } struct combination { vector<Modint> fact, ifact; combination(int n) : fact(n + 1), ifact(n + 1) { fact[0] = 1; for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * i; ifact[n] = fact[n].inverse(); for (int i = n; i >= 1; i--) ifact[i - 1] = ifact[i] * i; } Modint operator()(int n, int k) { return fact[n] * ifact[n - k] * ifact[k]; } }; using vm = vector<Modint>; using vvm = vector<vm>; template <class Z> struct MyMatrix { using mat = MyMatrix<Z>; vector<vector<Z>> m_dat; int m_h; int m_w; MyMatrix(int h, int w) : m_h(h), m_w(w), m_dat(h, vector<Z>(w)) {} vector<Z> &operator[](int idx) { return m_dat[idx]; } mat Multiple(mat &a) { mat C(m_h, a.m_w); for (int i = 0; i < m_h; i++) { for (int j = 0; j < a.m_w; j++) { for (int k = 0; k < m_w; k++) { C[i][j] += m_dat[i][k] * a[k][j]; } } } return C; } mat Pow(ll x) { mat B(m_h, m_w); for (int i = 0; i < m_h; i++) { B[i][i] = 1; } mat A = *this; while (x > 0) { if (x & 1) B = B.Multiple(A); A = A.Multiple(A); x = x >> 1; } return B; } // friend ostream& operator<<(ostream &os, const mat &A); }; /* template<class Z> ostream& operator<<(ostream &os, Matrix<Z>& A) { for(int i=0;i<A.m_h;i++) { for(int j=0;j<A.m_w;j++) { os << A[i][j] << " "; } os << endl; } return os; } */ template <class T> using mat = MyMatrix<T>; //_____________________ following sorce code_________________________// const int max_n = 3 * (1e5) + 1; const int max_m = 83 * (1e5) + 1; int n, m, k; ll N; int h, w; string S; int a, b, c; vi v; int ans; double x, y, z; vector<int> G[1010101]; const unsigned long long int B1 = 1000000007; const unsigned long long int B2 = 10000000007; bool RollingHash(string const &S, string const &T) { int slen = S.length(); int tlen = T.length(); if (tlen > slen) return false; unsigned long long int t1 = 1; unsigned long long int t2 = 1; for (int i = 0; i < tlen; i++) { t1 *= B1; t2 *= B2; } unsigned long long int sh1 = 0; unsigned long long int th1 = 0; unsigned long long int sh2 = 0; unsigned long long int th2 = 0; for (int i = 0; i < tlen; i++) { sh1 = sh1 * B1 + S[i]; th1 = th1 * B1 + T[i]; sh2 = sh2 * B2 + S[i]; th2 = th2 * B2 + T[i]; } for (int i = 0; i + tlen <= slen; i++) { if (sh1 == th1 and sh2 == th2) { bool _ok = true; for (int j = 0; j < tlen; j++) { if (S[j + i] != T[j]) _ok = false; } if (_ok) return true; } if (i + tlen < slen) { sh1 = sh1 * B1 + S[i + tlen] - (t1 * S[i]); sh2 = sh2 * B2 + S[i + tlen] - (t2 * S[i]); } } return false; } void solve() { IN(n, S); int ng = n + 1, ok = 0; while (ok + 1 < ng) { int mid = (ng + ok) / 2; bool match = false; for (int i = 0; i + 2 * mid < n; i++) { string t = {}; string s = {}; For(j, i, i + mid) t.push_back(S[j]); For(j, i + mid, n) s.push_back(S[j]); if (RollingHash(s, t)) { match = true; break; } } if (match) ok = mid; else ng = mid; } pri(ok); } signed main(int argc, char *argv[]) { cin.tie(0); ios::sync_with_stdio(false); int cases = 1; // IN(cases); while (cases--) solve(); // pri(ans); // for(auto c : ans){cout << c << endl;} // cout << fixed << setprecision(15) << ans << endl; return 0; }
#include <algorithm> #include <bits/stdc++.h> #include <cmath> #include <complex> #include <cstdio> #include <iostream> #include <map> #include <queue> #include <set> #include <utility> #include <vector> #define fir first #define sec second #define sz(s) (s).size() #define pb push_back #define get(n) scanf("%d", &n); #define gets(s) \ string s; \ cin >> (s); #define prfi(n) printf("%d", &n); #define prfd(n) printf("%lf", &n); #define All(s) (s).begin(), (s).end() #define rep(i, j) for (int(i) = 0; (i) < (j); (i)++) #define For(i, j, k) for (int(i) = (j); (i) < (k); (i)++) #define drep(i, j) for (int(i) = (j); (i) >= 0; (i)--) #define Ford(i, j, k) for (int(i) = (j); i >= (k); i--) #define fore(c, v) for (auto(c) : (v)) #define lp for (int __ = 0; __ < n; i++) #define mem(a, b) memset(a, b, sizeof(a)); #define dump(x) std::cout << #x << " = " << (x) << std::endl; #define debug(x) \ cout << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl; using ull = unsigned long long int; using ll = long long; using ld = long double; using pii = std::pair<int, int>; using pll = std::pair<ll, ll>; using vi = std::vector<int>; using vvi = std::vector<vi>; using vll = std::vector<ll>; using vvll = std::vector<vll>; using vd = std::vector<double>; using vvd = std::vector<vd>; using qi = std::queue<int>; using vpii = std::vector<std::pair<int, int>>; using vpll = std::vector<pll>; using namespace std; const int Mod = (1e9) + 7; const int INF = 1e9 + 19; const ll INFL = 1e18 + 19; const int dx[] = {-1, 0, 0, 1}; const int dy[] = {0, -1, 1, 0}; const int dx2[] = {-1, -1, -1, 0, 0, 0, 1, 1, 1}; const int dy2[] = {1, 0, -1, 1, 0, -1, 1, 0, -1}; const double EPS = 1e-10; //_____________________________________Templates_________________________________________// template <class T1, class T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; } template <class T1, class T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; } template <class T> inline void pri(T a) { cout << a << endl; } template <class Z> using vec = vector<Z>; template <class T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>; // mainly use for dynamic prog template <class T1, class T2> void update(T1 &a, T2 b) { a += b; if (a > Mod) a %= Mod; } inline void IN(void) { return; } template <typename First, typename... Rest> void IN(First &first, Rest &...rest) { cin >> first; IN(rest...); return; } inline void OUT(void) { cout << "\n"; return; } template <typename First, typename... Rest> void OUT(First first, Rest... rest) { cout << first << " "; OUT(rest...); return; } bool pairsort(pll pl, pll pr) { if (pl.first == pr.first) return pl.second > pr.second; return pl.first < pr.first; } int cntbit(ll a, int n, int j) { int res = 0; For(i, j, n) { if (a >> i & 1) { res++; } } return res; } vector<int> make_bit(int a) { vector<int> res; for (int i = 31; i >= 0; i--) if (a & (1 << i)) res.pb(i); return res; } bool stdbit(int a, int b) { return a & (1 << b); } int GCD(int a, int b) { if (b > a) return GCD(b, a); if (a % b == 0) return b; else return GCD(b, a % b); } int LCM(int a, int b) { return a * b / GCD(a, b); } int roundup(int a, int b) { if (a % b == 0) return a / b; else return (a + b) / b; } int rounddown(int a, int b) { if (a % b == 0) return a / b; else { return (a - b) / b; } } ll pow(ll a, ll n) { ll res = 1; while (n > 0) { if (n & 1) res *= a; a *= a; n = n >> 1; } return res; } ll GetDiviserCount(ll N) //約数の個数 { ll res = 1; For(i, 2, sqrt(N) + 1) { ll cnt = 0; while (N % i == 0) { cnt++; N /= i; } res *= (cnt + 1); if (N == 1) break; } if (N != 1) res *= 2; return res; } vll GetDivisor(ll N) //約数列挙 { vll res; for (ll i = 1; i * i <= N; i++) { if (N % i == 0) { res.pb(i); if (i * i != N) res.pb(N / i); } } sort(All(res)); return res; } struct Modint { using ll = long long int; ll x; Modint(ll x = 0) : x((x % Mod + Mod) % Mod) {} Modint operator-() { return Modint(-x); } Modint &operator+=(Modint a) { (x += a.x % Mod) % Mod; return *this; } Modint &operator-=(Modint a) { x = (x - a.x + Mod) % Mod; return *this; } Modint &operator*=(Modint a) { (x *= a.x) %= Mod; return *this; } Modint operator+(Modint a) { Modint b(*this); b += a; return b; } Modint operator-(Modint a) { Modint b(*this); b -= a; return b; } Modint operator*(Modint a) { Modint b(*this); b *= a; return b; } long long int EXTGCD(long long int a, long long int b, long long int &x, long long int &y) { if (b == 0) { x = 1; y = 0; return a; } long long int g = EXTGCD(b, a % b, y, x); y -= (a / b) * x; return g; } Modint inverse() { long long int a, b; EXTGCD(x, Mod, a, b); (a += Mod) %= Mod; return a; } Modint pow(ll a) { Modint res = 1; Modint b = x; while (a > 0) { if (a & 1) res *= b; a = a >> 1; b *= b; } return res; } friend ostream &operator<<(ostream &os, const Modint &a); }; ostream &operator<<(ostream &os, const Modint &a) { os << a.x; return os; } struct combination { vector<Modint> fact, ifact; combination(int n) : fact(n + 1), ifact(n + 1) { fact[0] = 1; for (int i = 1; i <= n; i++) fact[i] = fact[i - 1] * i; ifact[n] = fact[n].inverse(); for (int i = n; i >= 1; i--) ifact[i - 1] = ifact[i] * i; } Modint operator()(int n, int k) { return fact[n] * ifact[n - k] * ifact[k]; } }; using vm = vector<Modint>; using vvm = vector<vm>; template <class Z> struct MyMatrix { using mat = MyMatrix<Z>; vector<vector<Z>> m_dat; int m_h; int m_w; MyMatrix(int h, int w) : m_h(h), m_w(w), m_dat(h, vector<Z>(w)) {} vector<Z> &operator[](int idx) { return m_dat[idx]; } mat Multiple(mat &a) { mat C(m_h, a.m_w); for (int i = 0; i < m_h; i++) { for (int j = 0; j < a.m_w; j++) { for (int k = 0; k < m_w; k++) { C[i][j] += m_dat[i][k] * a[k][j]; } } } return C; } mat Pow(ll x) { mat B(m_h, m_w); for (int i = 0; i < m_h; i++) { B[i][i] = 1; } mat A = *this; while (x > 0) { if (x & 1) B = B.Multiple(A); A = A.Multiple(A); x = x >> 1; } return B; } // friend ostream& operator<<(ostream &os, const mat &A); }; /* template<class Z> ostream& operator<<(ostream &os, Matrix<Z>& A) { for(int i=0;i<A.m_h;i++) { for(int j=0;j<A.m_w;j++) { os << A[i][j] << " "; } os << endl; } return os; } */ template <class T> using mat = MyMatrix<T>; //_____________________ following sorce code_________________________// const int max_n = 3 * (1e5) + 1; const int max_m = 83 * (1e5) + 1; int n, m, k; ll N; int h, w; string S; int a, b, c; vi v; int ans; double x, y, z; vector<int> G[1010101]; const unsigned long long int B1 = 1000000007; const unsigned long long int B2 = 10000000007; bool RollingHash(string const &S, string const &T) { int slen = S.length(); int tlen = T.length(); if (tlen > slen) return false; unsigned long long int t1 = 1; unsigned long long int t2 = 1; for (int i = 0; i < tlen; i++) { t1 *= B1; t2 *= B2; } unsigned long long int sh1 = 0; unsigned long long int th1 = 0; unsigned long long int sh2 = 0; unsigned long long int th2 = 0; for (int i = 0; i < tlen; i++) { sh1 = sh1 * B1 + S[i]; th1 = th1 * B1 + T[i]; sh2 = sh2 * B2 + S[i]; th2 = th2 * B2 + T[i]; } for (int i = 0; i + tlen <= slen; i++) { if (sh1 == th1 && sh2 == th2) { bool _ok = true; for (int j = 0; j < tlen; j++) { if (S[j + i] != T[j]) _ok = false; } if (_ok) return true; } if (i + tlen < slen) { sh1 = (sh1 * B1) + S[i + tlen] - (t1 * S[i]); sh2 = (sh2 * B2) + S[i + tlen] - (t2 * S[i]); } } return false; } void solve() { IN(n, S); int ng = n + 1, ok = 0; while (ok + 1 < ng) { int mid = (ng + ok) / 2; bool match = false; for (int i = 0; i + 2 * mid <= n; i++) { string t = {}; string s = {}; For(j, i, i + mid) t.push_back(S[j]); For(j, i + mid, n) s.push_back(S[j]); if (RollingHash(s, t)) { match = true; break; } } if (match) ok = mid; else ng = mid; } pri(ok); } signed main(int argc, char *argv[]) { cin.tie(0); ios::sync_with_stdio(false); int cases = 1; // IN(cases); while (cases--) solve(); // pri(ans); // for(auto c : ans){cout << c << endl;} // cout << fixed << setprecision(15) << ans << endl; return 0; }
[ "control_flow.branch.if.condition.change", "expression.operator.compare.change", "control_flow.loop.condition.change" ]
744,154
744,155
u264405855
cpp
p02913
#include <bits/stdc++.h> using namespace std; vector<int> z_function(string s) { int n = (int)s.length(); vector<int> z(n); for (int i = 1, l = 0, r = 0; i < n; ++i) { if (i <= r) z[i] = min(r - i + 1, z[i - l]); while (i + z[i] < n && s[z[i]] == s[i + z[i]]) ++z[i]; if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1; } return z; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n; cin >> n; string s; cin >> s; int ans = 0; for (int i = 0; i < n; i++) { vector<int> z = z_function(s); for (int i = 1; i < n; i++) { ans = max(ans, min(i, z[i])); } s = s.substr(1); } cout << ans << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> z_function(string s) { int n = (int)s.length(); vector<int> z(n); for (int i = 1, l = 0, r = 0; i < n; ++i) { if (i <= r) z[i] = min(r - i + 1, z[i - l]); while (i + z[i] < n && s[z[i]] == s[i + z[i]]) ++z[i]; if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1; } return z; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n; cin >> n; string s; cin >> s; int ans = 0; for (int i = 0; i < n; i++) { vector<int> z = z_function(s); for (int i = 1; i < s.size(); i++) { ans = max(ans, min(i, z[i])); } s = s.substr(1); } cout << ans << "\n"; return 0; }
[ "control_flow.loop.for.condition.change", "expression.operation.binary.change", "call.add" ]
744,160
744,161
u845907943
cpp
p02913
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> #define ll long long #define ull unsigned long long using namespace std; void Zalgorithm(string s, vector<int> &Z, int i, int j) { while (i < s.size()) { while ((i + j < s.size()) && (s[j] == s[i + j])) { ++j; } Z[i] = j; if (j == 0) { ++i; continue; } int k = 1; while ((i + k < s.size()) && (k + Z[k] < j)) { Z[i + k] = Z[k]; ++k; } i += k; j -= k; } } int main() { int N; cin >> N; string S; cin >> S; int ans = 0; for (int ii = 0; ii < N - 1; ++ii) { int size = N - ii; vector<int> Z(size); Z[0] = size; string sub = S.substr(ii); Zalgorithm(sub, Z, ii + 1, 0); int val = 0; for (int jj = 0; jj < size; ++jj) { int tmp = min(jj, Z[jj]); val = max(val, tmp); } ans = max(ans, val); } cout << ans << "\n"; return 0; }
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> #define ll long long #define ull unsigned long long using namespace std; void Zalgorithm(string s, vector<int> &Z, int i, int j) { while (i < s.size()) { while ((i + j < s.size()) && (s[j] == s[i + j])) { ++j; } Z[i] = j; if (j == 0) { ++i; continue; } int k = 1; while ((i + k < s.size()) && (k + Z[k] < j)) { Z[i + k] = Z[k]; ++k; } i += k; j -= k; } } int main() { int N; cin >> N; string S; cin >> S; int ans = 0; for (int ii = 0; ii < N - 1; ++ii) { int size = N - ii; vector<int> Z(size); Z[0] = size; string sub = S.substr(ii); Zalgorithm(sub, Z, 1, 0); int val = 0; for (int jj = 0; jj < size; ++jj) { int tmp = min(jj, Z[jj]); val = max(val, tmp); } ans = max(ans, val); } cout << ans << "\n"; return 0; }
[ "expression.operation.binary.remove" ]
744,170
744,171
u762158020
cpp
p02913
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define REP(i, n) for (int i = 0; i < n; i++) #define REPi(i, a, b) for (int i = int(a); i < int(b); i++) #define MEMS(a, b) memset(a, b, sizeof(a)) #define mp make_pair template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } const ll MOD = 1e9 + 7; ll n, k; vector<int> order; vector<int> sa; vector<int> lcp; // (order[i], order[i + k])と(order[j], order[j + k])を比較. bool compare_sa(int i, int j) { if (order[i] != order[j]) return order[i] < order[j]; else { int ri = (i + k <= n) ? order[i + k] : -1; int rj = (j + k <= n) ? order[j + k] : -1; return ri < rj; } } //文字列Sの接尾辞配列を構築 (O(n*logn*logn)) void construct_sa(string S) { n = S.length(); sa.resize(n + 1); order.resize(n + 1); vector<ll> tmp(n + 1); //最初の1文字. ランクを文字コードで表現 for (int i = 0; i <= n; i++) { sa[i] = i; order[i] = (i < n) ? S[i] : -1; } // k文字についてソートされているところから、2k文字でソートする for (k = 1; k <= n; k *= 2) { sort(sa.begin(), sa.end(), compare_sa); // tmpに次のランクを格納. 辞書順にランク計算. tmp[sa[0]] = 0; for (int i = 1; i <= n; i++) { tmp[sa[i]] = tmp[sa[i - 1]] + (compare_sa(sa[i - 1], sa[i]) ? 1 : 0); } for (int i = 0; i <= n; i++) { order[i] = tmp[i]; } } } // 文字列Sとその接尾辞配列saを受け取り、高さ配列lcpを返す. (O(n)) void construct_lcp(string S) { n = S.length(); lcp.resize(n); order.resize(n + 1); for (int i = 0; i <= n; i++) order[sa[i]] = i; int h = 0; lcp[0] = 0; for (int i = 0; i < n; i++) { // 文字列中での位置iの接尾辞と、接尾辞配列中でその一つ前の設備時のLCPを求める. int j = sa[order[i] - 1]; // hを先頭の分1減らし、後ろが一致しているだけ増やす. if (h > 0) h--; for (; j + h < n && i + h < n; h++) { if (S[j + h] != S[i + h]) break; } lcp[order[i] - 1] = h; } } const int INF = 10000000; class SegmentTree { int n; vector<int> node; public: SegmentTree(vector<int> v) { int size = v.size(); n = 1; while (n < size) n *= 2; node.resize(2 * n - 1, INF); for (int i = 0; i < size; i++) { node[n - 1 + i] = v[i]; } for (int i = n - 2; i >= 0; i--) { node[i] = min(node[2 * i + 1], node[2 * i + 2]); } } void update(int idx, int val) { idx += n - 1; node[idx] = val; while (idx > 0) { idx = (idx - 1) / 2; node[idx] = min(node[2 * idx + 1], node[2 * idx + 2]); } } // return min value within [a,b) // call get_min(a, b); int get_min(int a, int b, int k = 0, int l = 0, int r = -1) { if (r < 0) r = n; if (b <= l || r <= a) return INF; if (a <= l && r <= b) return node[k]; int vl = get_min(a, b, 2 * k + 1, l, (l + r) / 2); int vr = get_min(a, b, 2 * k + 2, (l + r) / 2, r); return min(vl, vr); } }; int main() { ll N; cin >> N; string S; cin >> S; int ans = 0; construct_sa(S); construct_lcp(S); vector<int> tmp(N); REP(i, N) { tmp[i] = lcp[sa[i + 1]]; } SegmentTree stree(lcp); // SegmentTree stree(tmp); REP(i, N) { REPi(j, i + 1, N) { int x, y; // ll l = lcp[i]; // int l = stree.get_min(i, j+1); x = sa[i], y = sa[j]; if (x > y) swap(x, y); int l = stree.get_min(i, j); // printf("(%d, %d), (%d, %d): %d\n", i, j, x, y, l); // ll a = sa[i]; // ll b = sa[j]; // if(abs(b-a) < l) continue; // chmin(l, j-i); chmin(l, y - x); // if(y-x < l) continue; chmax(ans, l); } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define REP(i, n) for (int i = 0; i < n; i++) #define REPi(i, a, b) for (int i = int(a); i < int(b); i++) #define MEMS(a, b) memset(a, b, sizeof(a)) #define mp make_pair template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } const ll MOD = 1e9 + 7; ll n, k; vector<int> order; vector<int> sa; vector<int> lcp; // (order[i], order[i + k])と(order[j], order[j + k])を比較. bool compare_sa(int i, int j) { if (order[i] != order[j]) return order[i] < order[j]; else { int ri = (i + k <= n) ? order[i + k] : -1; int rj = (j + k <= n) ? order[j + k] : -1; return ri < rj; } } //文字列Sの接尾辞配列を構築 (O(n*logn*logn)) void construct_sa(string S) { n = S.length(); sa.resize(n + 1); order.resize(n + 1); vector<ll> tmp(n + 1); //最初の1文字. ランクを文字コードで表現 for (int i = 0; i <= n; i++) { sa[i] = i; order[i] = (i < n) ? S[i] : -1; } // k文字についてソートされているところから、2k文字でソートする for (k = 1; k <= n; k *= 2) { sort(sa.begin(), sa.end(), compare_sa); // tmpに次のランクを格納. 辞書順にランク計算. tmp[sa[0]] = 0; for (int i = 1; i <= n; i++) { tmp[sa[i]] = tmp[sa[i - 1]] + (compare_sa(sa[i - 1], sa[i]) ? 1 : 0); } for (int i = 0; i <= n; i++) { order[i] = tmp[i]; } } } // 文字列Sとその接尾辞配列saを受け取り、高さ配列lcpを返す. (O(n)) void construct_lcp(string S) { n = S.length(); lcp.resize(n); order.resize(n + 1); for (int i = 0; i <= n; i++) order[sa[i]] = i; int h = 0; lcp[0] = 0; for (int i = 0; i < n; i++) { // 文字列中での位置iの接尾辞と、接尾辞配列中でその一つ前の設備時のLCPを求める. int j = sa[order[i] - 1]; // hを先頭の分1減らし、後ろが一致しているだけ増やす. if (h > 0) h--; for (; j + h < n && i + h < n; h++) { if (S[j + h] != S[i + h]) break; } lcp[order[i] - 1] = h; } } const int INF = 10000000; class SegmentTree { int n; vector<int> node; public: SegmentTree(vector<int> v) { int size = v.size(); n = 1; while (n < size) n *= 2; node.resize(2 * n - 1, INF); for (int i = 0; i < size; i++) { node[n - 1 + i] = v[i]; } for (int i = n - 2; i >= 0; i--) { node[i] = min(node[2 * i + 1], node[2 * i + 2]); } } void update(int idx, int val) { idx += n - 1; node[idx] = val; while (idx > 0) { idx = (idx - 1) / 2; node[idx] = min(node[2 * idx + 1], node[2 * idx + 2]); } } // return min value within [a,b) // call get_min(a, b); int get_min(int a, int b, int k = 0, int l = 0, int r = -1) { if (r < 0) r = n; if (b <= l || r <= a) return INF; if (a <= l && r <= b) return node[k]; int vl = get_min(a, b, 2 * k + 1, l, (l + r) / 2); int vr = get_min(a, b, 2 * k + 2, (l + r) / 2, r); return min(vl, vr); } }; int main() { ll N; cin >> N; string S; cin >> S; int ans = 0; construct_sa(S); construct_lcp(S); vector<int> tmp(N); REP(i, N) { tmp[i] = lcp[sa[i + 1]]; } SegmentTree stree(lcp); // SegmentTree stree(tmp); REP(i, N) { REPi(j, i + 1, N + 1) { int x, y; // ll l = lcp[i]; // int l = stree.get_min(i, j+1); x = sa[i], y = sa[j]; if (x > y) swap(x, y); int l = stree.get_min(i, j); // printf("(%d, %d), (%d, %d): %d\n", i, j, x, y, l); // ll a = sa[i]; // ll b = sa[j]; // if(abs(b-a) < l) continue; // chmin(l, j-i); chmin(l, y - x); // if(y-x < l) continue; chmax(ans, l); } } cout << ans << endl; return 0; }
[ "expression.operation.binary.add" ]
744,176
744,177
u666394517
cpp
p02913
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> l_l; typedef pair<int, int> i_i; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } const ll INF = 1e18; const ll MOD = 998244353; const ll MX = 5001; int N, ans; string S; int z_function(string s) { int n = (int)s.size(); vector<int> z(n); int cm = 0; for (int i = 1, l = 0, r = 0; i < n; i++) { if (i <= r) z[i] = min(r - i + 1, z[i - l]); while (i + z[i] < n && s[z[i]] == s[i + z[i]]) z[i]++; if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1; chmax(cm, z[i]); } return cm; } void solve() { cin >> N >> S; for (int i = 1; i < N; i++) { chmax(ans, z_function(S.substr(i, N - i))); } cout << ans; } int main() { cin.sync_with_stdio(0); cin.tie(0); ll T = 1; // cin >> T; while (T--) solve(); return 0; // You should actually read the stuff at the bottom } /* Stuff to Look For * ----------------- * Int overflow, array bounds * Initializing all variables, avoid weird behavior * Edge cases(n = 0, n = 1) * Just return 0 after result */
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> l_l; typedef pair<int, int> i_i; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } const ll INF = 1e18; const ll MOD = 998244353; const ll MX = 5001; int N, ans; string S; int z_function(string s) { int n = (int)s.size(); vector<int> z(n); int cm = 0; for (int i = 1, l = 0, r = 0; i < n; i++) { if (i <= r) z[i] = min(r - i + 1, z[i - l]); while (i + z[i] < n && s[z[i]] == s[i + z[i]]) z[i]++; if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1; chmax(cm, min(z[i], i)); } return cm; } void solve() { cin >> N >> S; for (int i = 0; i < N; i++) { chmax(ans, z_function(S.substr(i, N - i))); } cout << ans; } int main() { cin.sync_with_stdio(0); cin.tie(0); ll T = 1; // cin >> T; while (T--) solve(); return 0; // You should actually read the stuff at the bottom } /* Stuff to Look For * ----------------- * Int overflow, array bounds * Initializing all variables, avoid weird behavior * Edge cases(n = 0, n = 1) * Just return 0 after result */
[ "call.add", "call.arguments.add", "literal.number.change", "variable_declaration.value.change", "control_flow.loop.for.initializer.change", "expression.off_by_one" ]
744,178
744,179
u736647947
cpp
p02913
#include <algorithm> #include <assert.h> #include <bitset> #include <cmath> #include <cstdlib> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<char> vc; typedef vector<string> vs; typedef vector<bool> vb; typedef vector<double> vd; typedef pair<ll, ll> P; typedef vector<P> vpl; typedef tuple<ll, ll, ll> tapu; #define rep(i, n) for (ll i = 0; i < (n); i++) #define REP(i, a, b) for (int i = (a); i < (b); i++) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() const int inf = 1 << 30; const ll linf = 1LL << 62; const int MAX = 510000; ll dy[8] = {0, 1, 0, -1, 1, -1, 1, -1}; ll dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; const double pi = acos(-1); const double eps = 1e-7; template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return true; } else return false; } template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return true; } else return false; } template <typename T1, typename T2> inline void print2(T1 a, T2 b) { cout << a << " " << b << "\n"; } template <typename T1, typename T2, typename T3> inline void print3(T1 a, T2 b, T3 c) { cout << a << " " << b << " " << c << "\n"; } const int mod = 1e9 + 7; // const int mod = 998244353; vector<ll> zalgo(string s) { ll n = s.size(); vector<ll> z(n, 0); z[0] = n; ll i = 1, j = 0; while (i < n) { while (i + j < n && s[j] == s[i + j]) j++; if (j == 0) { i++; continue; } z[i] = j; ll k = 1; if (k < j && k + z[k] < j) { z[i + k] = z[k]; k++; } i += k; j -= k; } return z; } int main() { ll n; cin >> n; string s; cin >> s; ll ans = 0; rep(i, n - 1) { vl z = zalgo(s.substr(i)); rep(j, n - i + 1) { chmax(ans, min(z[j], j)); } } cout << ans << endl; }
#include <algorithm> #include <assert.h> #include <bitset> #include <cmath> #include <cstdlib> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<char> vc; typedef vector<string> vs; typedef vector<bool> vb; typedef vector<double> vd; typedef pair<ll, ll> P; typedef vector<P> vpl; typedef tuple<ll, ll, ll> tapu; #define rep(i, n) for (ll i = 0; i < (n); i++) #define REP(i, a, b) for (int i = (a); i < (b); i++) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() const int inf = 1 << 30; const ll linf = 1LL << 62; const int MAX = 510000; ll dy[8] = {0, 1, 0, -1, 1, -1, 1, -1}; ll dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; const double pi = acos(-1); const double eps = 1e-7; template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return true; } else return false; } template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return true; } else return false; } template <typename T1, typename T2> inline void print2(T1 a, T2 b) { cout << a << " " << b << "\n"; } template <typename T1, typename T2, typename T3> inline void print3(T1 a, T2 b, T3 c) { cout << a << " " << b << " " << c << "\n"; } const int mod = 1e9 + 7; // const int mod = 998244353; vector<ll> zalgo(string s) { ll n = s.size(); vector<ll> z(n, 0); z[0] = n; ll i = 1, j = 0; while (i < n) { while (i + j < n && s[j] == s[i + j]) j++; if (j == 0) { i++; continue; } z[i] = j; ll k = 1; while (k < j && k + z[k] < j) { z[i + k] = z[k]; k++; } i += k; j -= k; } return z; } int main() { ll n; cin >> n; string s; cin >> s; ll ans = 0; rep(i, n - 1) { vl z = zalgo(s.substr(i)); rep(j, n - i) { chmax(ans, min(z[j], j)); } } cout << ans << endl; }
[ "control_flow.branch.while.replace.add", "control_flow.loop.if.replace.remove", "expression.operation.binary.remove" ]
744,180
744,181
u242679311
cpp
p02913
#include <algorithm> #include <bitset> #include <cmath> #include <cstdlib> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<char> vc; typedef vector<string> vs; typedef vector<bool> vb; typedef pair<ll, ll> P; typedef vector<P> vpl; #define rep(i, n) for (ll i = 0; i < (n); i++) #define REP(i, a, b) for (ll i = (a); i < (b); i++) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() const int inf = 1 << 30; const ll linf = 1LL << 62; const int MAX = 2100000; ll dy[8] = {0, 1, 0, -1, 1, -1, 1, -1}; ll dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; const double pi = acos(-1); template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; } template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; } template <typename T1, typename T2> inline void print2(T1 a, T2 b) { cout << a << " " << b << endl; } template <typename T1, typename T2, typename T3> inline void print3(T1 a, T2 b, T3 c) { cout << a << " " << b << " " << c << endl; } const int mod = 1e9 + 7; vector<ll> z_algorithm(const string &s) { vector<ll> prefix(s.size()); for (ll i = 1, j = 0; i < s.size(); i++) { if (i + prefix[i - j] < j + prefix[j]) { prefix[i] = prefix[i - j]; } else { ll k = max(0LL, j + prefix[j] - i); while (i + k < s.size() && s[k] == s[i + k]) ++k; prefix[i] = k; j = i; } } prefix[0] = (ll)s.size(); return prefix; } int main() { ll n; cin >> n; string s; cin >> s; s += s; ll ans = 0; rep(i, n) { string t = s.substr(i, n); auto z = z_algorithm(t); REP(j, 1, n - i) chmax(ans, min({z[j], n - i - j + 1, j})); } cout << ans << endl; }
#include <algorithm> #include <bitset> #include <cmath> #include <cstdlib> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; typedef long long ll; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<char> vc; typedef vector<string> vs; typedef vector<bool> vb; typedef pair<ll, ll> P; typedef vector<P> vpl; #define rep(i, n) for (ll i = 0; i < (n); i++) #define REP(i, a, b) for (ll i = (a); i < (b); i++) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() const int inf = 1 << 30; const ll linf = 1LL << 62; const int MAX = 2100000; ll dy[8] = {0, 1, 0, -1, 1, -1, 1, -1}; ll dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; const double pi = acos(-1); template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; } template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; } template <typename T1, typename T2> inline void print2(T1 a, T2 b) { cout << a << " " << b << endl; } template <typename T1, typename T2, typename T3> inline void print3(T1 a, T2 b, T3 c) { cout << a << " " << b << " " << c << endl; } const int mod = 1e9 + 7; vector<ll> z_algorithm(const string &s) { vector<ll> prefix(s.size()); for (ll i = 1, j = 0; i < s.size(); i++) { if (i + prefix[i - j] < j + prefix[j]) { prefix[i] = prefix[i - j]; } else { ll k = max(0LL, j + prefix[j] - i); while (i + k < s.size() && s[k] == s[i + k]) ++k; prefix[i] = k; j = i; } } prefix[0] = (ll)s.size(); return prefix; } int main() { ll n; cin >> n; string s; cin >> s; s += s; ll ans = 0; rep(i, n) { string t = s.substr(i, n); auto z = z_algorithm(t); REP(j, 1, n - i) chmax(ans, min({z[j], n - i - j, j})); } cout << ans << endl; }
[ "expression.operation.binary.remove" ]
744,182
744,183
u242679311
cpp
p02913
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <string.h> #include <unordered_map> #include <vector> using namespace std; #define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl template <class T> void printvec(const vector<T> &v) { for (auto x : v) { cout << x << " "; } cout << endl; } template <class T> void printtree(const vector<vector<T>> &tree) { for (long long i = 0; i < tree.size(); ++i) { cout << i + 1 << ": "; printvec(tree[i]); } } template <class T, class U> void printmap(const map<T, U> &mp) { for (auto x : mp) { cout << x.first << "=>" << x.second << endl; } } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } #define rep(i, n) for (ll i = 0; i < n; ++i) #define all(s) s.begin(), s.end() #define sz(x) (ll)(x).size() #define fr first #define sc second #define mp make_pair #define pb push_back #define eb emplace_back typedef long long ll; typedef unsigned long long ull; typedef pair<ll, ll> P; typedef tuple<ll, ll, ll> triple; typedef double D; typedef vector<ll> vl; typedef vector<P> vp; const ll INF = 1e9; const ll MOD = 1000000007; // 1e9 + 7 vector<ll> build_z(const string &s) { ll n = s.size(); vector<ll> z(n, 0); ll l = -1, r = -1; for (ll i = 1; i < n; ++i) { if (i <= r) { z[i] = min<ll>(r - i + 1, z[i - l]); } while (i + z[i] < n && s[i + z[i]] == s[z[i]]) { ++z[i]; } if (i + z[i] - 1 > r) { r = i + z[i] - 1; l = i; } } return z; } void search(const string &text, const string &pattern, vector<ll> &result) { vector<ll> z = build_z(pattern + "$" + text); ll p = pattern.length(); for (ll i = p + 1; i < z.size(); ++i) { if (z[i] == p) { result.push_back(i - p - 1); } } } // int main(int argc, char** argv) { // string s, t; // cin >> s; // cin >> t; // // vector<ll> matches; // search(s, t, matches); // // for (ll idx : matches) { // cout << idx << endl; // } // // cout << s << endl; // cout << t << endl; // } int main(int argc, char **argv) { cin.tie(NULL); cout.tie(NULL); ios_base::sync_with_stdio(false); // cout << setprecision(10) << fixed; ll n; cin >> n; string S; cin >> S; ll ans = 0; for (ll i = 1; i < n; ++i) { // prefix..[0..i), suffix..[i,n) string pre = S.substr(0, i); string suf = S.substr(i); string nexS = suf + "$" + pre; vector<ll> z = build_z(suf + "$" + pre); for (ll j = i + 1; j < n + 1; ++j) { chmax(ans, z[j]); } } cout << ans << endl; }
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <string.h> #include <unordered_map> #include <vector> using namespace std; #define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl template <class T> void printvec(const vector<T> &v) { for (auto x : v) { cout << x << " "; } cout << endl; } template <class T> void printtree(const vector<vector<T>> &tree) { for (long long i = 0; i < tree.size(); ++i) { cout << i + 1 << ": "; printvec(tree[i]); } } template <class T, class U> void printmap(const map<T, U> &mp) { for (auto x : mp) { cout << x.first << "=>" << x.second << endl; } } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } #define rep(i, n) for (ll i = 0; i < n; ++i) #define all(s) s.begin(), s.end() #define sz(x) (ll)(x).size() #define fr first #define sc second #define mp make_pair #define pb push_back #define eb emplace_back typedef long long ll; typedef unsigned long long ull; typedef pair<ll, ll> P; typedef tuple<ll, ll, ll> triple; typedef double D; typedef vector<ll> vl; typedef vector<P> vp; const ll INF = 1e9; const ll MOD = 1000000007; // 1e9 + 7 vector<ll> build_z(const string &s) { ll n = s.size(); vector<ll> z(n, 0); ll l = -1, r = -1; for (ll i = 1; i < n; ++i) { if (i <= r) { z[i] = min<ll>(r - i + 1, z[i - l]); } while (i + z[i] < n && s[i + z[i]] == s[z[i]]) { ++z[i]; } if (i + z[i] - 1 > r) { r = i + z[i] - 1; l = i; } } return z; } void search(const string &text, const string &pattern, vector<ll> &result) { vector<ll> z = build_z(pattern + "$" + text); ll p = pattern.length(); for (ll i = p + 1; i < z.size(); ++i) { if (z[i] == p) { result.push_back(i - p - 1); } } } // int main(int argc, char** argv) { // string s, t; // cin >> s; // cin >> t; // // vector<ll> matches; // search(s, t, matches); // // for (ll idx : matches) { // cout << idx << endl; // } // // cout << s << endl; // cout << t << endl; // } int main(int argc, char **argv) { cin.tie(NULL); cout.tie(NULL); ios_base::sync_with_stdio(false); // cout << setprecision(10) << fixed; ll n; cin >> n; string S; cin >> S; ll ans = 0; for (ll i = 1; i < n; ++i) { // prefix..[0..i), suffix..[i,n) string pre = S.substr(0, i); string suf = S.substr(i); string nexS = suf + "$" + pre; vector<ll> z = build_z(suf + "$" + pre); for (ll j = n + 1 - i; j < n + 1; ++j) { chmax(ans, z[j]); } } cout << ans << endl; }
[ "identifier.change", "control_flow.loop.for.initializer.change", "expression.operation.binary.change" ]
744,197
744,198
u328294851
cpp
p02913
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <deque> #include <fstream> #include <functional> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <time.h> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> #define popcount __builtin_popcount using namespace std; typedef long long int ll; typedef pair<int, int> P; template <ll mod> struct RollingHash { vector<ll> hash, power; RollingHash(const string &s, ll base = 10007) { int sz = s.size(); hash.resize(sz + 1); power.resize(sz + 1); power[0] = 1; for (int i = 0; i < sz; i++) { power[i + 1] = power[i] * base % mod; hash[i + 1] = (hash[i] * base + s[i]) % mod; } } ll get(int l, int r) const { // [l, r) ll ret = hash[r] - hash[l] * power[r - l] % mod + mod; if (ret >= mod) ret -= mod; return ret; } ll concat(ll h1, ll h2, int len2) const { // h1+h2 ll ret = h1 * power[len2] % mod + h2; if (ret >= mod) ret -= mod; return ret; } }; int main() { int n; cin >> n; string s; cin >> s; const ll MOD1 = 1e9 + 7; const ll MOD2 = 1e9 + 9; RollingHash<MOD1> rh1(s); RollingHash<MOD2> rh2(s); int ans = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { while (i + ans < j && j + ans < n) { ll h1 = rh1.get(i, i + ans + 1) - rh1.get(j, j + ans + 1), h2 = rh2.get(i, i + ans + 1) - rh2.get(j, j + ans + 1); if (h1 == 0 && h2 == 0) break; ans++; } } } cout << ans << endl; return 0; }
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <deque> #include <fstream> #include <functional> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <time.h> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> #define popcount __builtin_popcount using namespace std; typedef long long int ll; typedef pair<int, int> P; template <ll mod> struct RollingHash { vector<ll> hash, power; RollingHash(const string &s, ll base = 10007) { int sz = s.size(); hash.resize(sz + 1); power.resize(sz + 1); power[0] = 1; for (int i = 0; i < sz; i++) { power[i + 1] = power[i] * base % mod; hash[i + 1] = (hash[i] * base + s[i]) % mod; } } ll get(int l, int r) const { // [l, r) ll ret = hash[r] - hash[l] * power[r - l] % mod + mod; if (ret >= mod) ret -= mod; return ret; } ll concat(ll h1, ll h2, int len2) const { // h1+h2 ll ret = h1 * power[len2] % mod + h2; if (ret >= mod) ret -= mod; return ret; } }; int main() { int n; cin >> n; string s; cin >> s; const ll MOD1 = 1e9 + 7; const ll MOD2 = 1e9 + 9; RollingHash<MOD1> rh1(s); RollingHash<MOD2> rh2(s); int ans = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { while (i + ans < j && j + ans < n) { ll h1 = rh1.get(i, i + ans + 1) - rh1.get(j, j + ans + 1), h2 = rh2.get(i, i + ans + 1) - rh2.get(j, j + ans + 1); if (h1 != 0 || h2 != 0) break; ans++; } } } cout << ans << endl; return 0; }
[ "misc.opposites", "expression.operator.compare.change", "control_flow.branch.if.condition.change" ]
744,213
744,214
u930898631
cpp
p02913
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define rep2(i, x, n) for (int i = x; i < n; i++) int main() { int N; string S; cin >> N >> S; priority_queue<int> que; int A[N][N]; for (int i = N - 1; i >= 0; i--) { int j = i + 1; while (j < N) { int k = 0; while (j + k < N && S[i + k] == S[j + k]) { k++; } A[i][j] = k; if (k == 0) { j++; } else { rep2(l, 1, k) { if (l + A[i][i + l] >= k) { if (j + l < N) { A[i][j + l] = (k - l) + A[i + (k - l)][j + k]; } else { A[i][j + l] = k - l; } } else { A[i][j + l] = A[i][i + l]; } } j += k; } } } rep(i, N) { rep2(j, i + 1, N) { que.push(min(A[i][j], j - i)); } } cout << que.top() << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define rep2(i, x, n) for (int i = x; i < n; i++) int main() { int N; string S; cin >> N >> S; priority_queue<int> que; int A[N][N]; for (int i = N - 1; i >= 0; i--) { int j = i + 1; while (j < N) { int k = 0; while (j + k < N && S[i + k] == S[j + k]) { k++; } A[i][j] = k; if (k == 0) { j++; } else { rep2(l, 1, k) { if (l + A[i][i + l] >= k) { if (j + k < N) { A[i][j + l] = (k - l) + A[i + (k - l)][j + k]; } else { A[i][j + l] = k - l; } } else { A[i][j + l] = A[i][i + l]; } } j += k; } } } rep(i, N) { rep2(j, i + 1, N) { que.push(min(A[i][j], j - i)); } } cout << que.top() << endl; }
[ "control_flow.branch.if.condition.change" ]
744,225
744,226
u879436400
cpp
p02913
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define rep2(i, x, n) for (int i = x; i < n; i++) int main() { int N; string S; cin >> N >> S; priority_queue<int> que; int A[N][N]; for (int i = N - 1; i >= 0; i--) { int j = i + 1; while (j < N) { int k = 0; while (j + k < N && S[i + k] == S[j + k]) { k++; } A[i][j] = k; if (k == 0) { j++; } else { rep2(l, 1, k) { if (l + A[i][i + l] >= k) { if (j + k < N) { A[i][j + l] = (k - l) + A[i + (k - l)][j + k]; } else { A[i][j + l] = k - l; } } else { A[i][j + l] = A[i][i + l]; } } j += k + 1; } } } rep(i, N) { rep2(j, i + 1, N) { que.push(min(A[i][j], j - i)); } } cout << que.top() << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define rep2(i, x, n) for (int i = x; i < n; i++) int main() { int N; string S; cin >> N >> S; priority_queue<int> que; int A[N][N]; for (int i = N - 1; i >= 0; i--) { int j = i + 1; while (j < N) { int k = 0; while (j + k < N && S[i + k] == S[j + k]) { k++; } A[i][j] = k; if (k == 0) { j++; } else { rep2(l, 1, k) { if (l + A[i][i + l] >= k) { if (j + k < N) { A[i][j + l] = (k - l) + A[i + (k - l)][j + k]; } else { A[i][j + l] = k - l; } } else { A[i][j + l] = A[i][i + l]; } } j += k; } } } rep(i, N) { rep2(j, i + 1, N) { que.push(min(A[i][j], j - i)); } } cout << que.top() << endl; }
[ "expression.operation.binary.remove" ]
744,227
744,226
u879436400
cpp
p02913
#include <bits/stdc++.h> using namespace std; int main() { int N; string S; int result = 0; cin >> N; cin >> S; int A[N]; int c = N; for (int b = 0; b < c; b++) { for (int a = 0; a < c; a++) A[a] = 0; A[0] = N; int i = 1, j = 0; while (i < N) { while (i + j < N && S[j] == S[i + j]) ++j; // 右向きにjを伸ばしていく A[i] = j; if (result < j && i > j) result = j; if (j == 0) { ++i; continue; } int k = 1; while (i + k < N && k + A[k] < j) A[i + k] = A[k], ++k; // i~j間にkを走らせる i += k; j -= k; } S.erase(0, 1); N -= 1; } cout << result << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int N; string S; int result = 0; cin >> N; cin >> S; int A[N]; int c = N; for (int b = 0; b < c; b++) { for (int a = 0; a < c; a++) A[a] = 0; A[0] = N; int i = 1, j = 0; while (i < N) { while (i + j < N && S[j] == S[i + j]) ++j; // 右向きにjを伸ばしていく A[i] = j; if (result < j && i >= j) result = j; if (j == 0) { ++i; continue; } int k = 1; while (i + k < N && k + A[k] < j) A[i + k] = A[k], ++k; // i~j間にkを走らせる i += k; j -= k; } S.erase(0, 1); N -= 1; } cout << result << endl; }
[ "expression.operator.compare.change", "control_flow.branch.if.condition.change" ]
744,232
744,233
u073895786
cpp
p02913
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <stdio.h> #include <string> #include <time.h> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> #define all(a) a.begin(), a.end() #define rep(i, n) for (ll i = 0; i < (n); i++) #define pb push_back #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<ll, ll> P; typedef complex<ld> com; constexpr int inf = 1000000000; constexpr ll INF = 1000000000000000000; constexpr ld EPS = 1e-12; constexpr ld PI = 3.141592653589793238; template <class T, class U> inline bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; } template <class T, class U> inline bool chmin(T &a, const U &b) { if (a > b) { a = b; return true; } return false; } template <class T> class rollinghash { const ull mask30 = (1LL << 30) - 1; const ull mask31 = (1LL << 31) - 1; const ull mod61 = (1LL << 61) - 1; vector<ull> base; vector<vector<ull>> hashvalue, basepower; int length; ull multiply(ull a, ull b) { ull au = a >> 31; ull ad = a & mask31; ull bu = b >> 31; ull bd = b & mask31; ull mid = ad * bu + au * bd; ull midu = mid >> 30; ull midd = mid & mask30; return au * bu * 2 + midu + (midd << 31) + ad * bd; } ull modulo(ull a) { ull au = a >> 61; ull ad = a & mod61; ull res = au + ad; if (res >= mod61) res -= mod61; return res; } public: rollinghash(T s) { length = s.size(); base = {200004}; hashvalue = vector<vector<ull>>(length + 1, vector<ull>(1)); for (int j = 0; j < 1; j++) { for (int i = 0; i < length; i++) { hashvalue[i + 1][j] = modulo(multiply(hashvalue[i][j], base[j]) + s[i]); } } basepower = vector<vector<ull>>(length + 1, vector<ull>(1)); for (int j = 0; j < 1; j++) { basepower[0][j] = 1; for (int i = 1; i <= length; i++) { basepower[i][j] = modulo(multiply(basepower[i - 1][j], base[j])); } } } ull hash(ll l, ll r, ll i) { assert(l < r && 0 <= l && l < length && 0 < r && r <= length); return modulo(hashvalue[r][i] + mod61 * 3 - multiply(hashvalue[l][i], basepower[r - l][i])); } }; signed main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); int n; cin >> n; string s; cin >> s; rollinghash<string> rol(s); int ans = 0; rep(i, n) { for (int j = i + 1; j < n; j++) { if (ans != 0 && rol.hash(i, i + ans, 0) != rol.hash(j, j + ans, 0)) continue; int ok = ans, ng = j - i + 1; while (ng - ok > 1) { int mid = (ok + ng) / 2; bool flag = true; rep(k, 1) { if (j + mid > n || rol.hash(i, i + mid, k) != rol.hash(j, j + mid, k)) flag = false; } if (flag) ok = mid; else ng = mid; } chmax(ans, ok); } } cout << ans << '\n'; }
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <stdio.h> #include <string> #include <time.h> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> #define all(a) a.begin(), a.end() #define rep(i, n) for (ll i = 0; i < (n); i++) #define pb push_back #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<ll, ll> P; typedef complex<ld> com; constexpr int inf = 1000000000; constexpr ll INF = 1000000000000000000; constexpr ld EPS = 1e-12; constexpr ld PI = 3.141592653589793238; template <class T, class U> inline bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; } template <class T, class U> inline bool chmin(T &a, const U &b) { if (a > b) { a = b; return true; } return false; } template <class T> class rollinghash { const ull mask30 = (1LL << 30) - 1; const ull mask31 = (1LL << 31) - 1; const ull mod61 = (1LL << 61) - 1; vector<ull> base; vector<vector<ull>> hashvalue, basepower; int length; ull multiply(ull a, ull b) { ull au = a >> 31; ull ad = a & mask31; ull bu = b >> 31; ull bd = b & mask31; ull mid = ad * bu + au * bd; ull midu = mid >> 30; ull midd = mid & mask30; return au * bu * 2 + midu + (midd << 31) + ad * bd; } ull modulo(ull a) { ull au = a >> 61; ull ad = a & mod61; ull res = au + ad; if (res >= mod61) res -= mod61; return res; } public: rollinghash(T s) { length = s.size(); base = {200004}; hashvalue = vector<vector<ull>>(length + 1, vector<ull>(1)); for (int j = 0; j < 1; j++) { for (int i = 0; i < length; i++) { hashvalue[i + 1][j] = modulo(multiply(hashvalue[i][j], base[j]) + s[i]); } } basepower = vector<vector<ull>>(length + 1, vector<ull>(1)); for (int j = 0; j < 1; j++) { basepower[0][j] = 1; for (int i = 1; i <= length; i++) { basepower[i][j] = modulo(multiply(basepower[i - 1][j], base[j])); } } } ull hash(ll l, ll r, ll i) { assert(l < r && 0 <= l && l < length && 0 < r && r <= length); return modulo(hashvalue[r][i] + mod61 * 3 - multiply(hashvalue[l][i], basepower[r - l][i])); } }; signed main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); int n; cin >> n; string s; cin >> s; rollinghash<string> rol(s); int ans = 0; rep(i, n) { for (int j = i + 1; j < n; j++) { if (j + ans > n || ans != 0 && rol.hash(i, i + ans, 0) != rol.hash(j, j + ans, 0)) continue; int ok = ans, ng = j - i + 1; while (ng - ok > 1) { int mid = (ok + ng) / 2; bool flag = true; rep(k, 1) { if (j + mid > n || rol.hash(i, i + mid, k) != rol.hash(j, j + mid, k)) flag = false; } if (flag) ok = mid; else ng = mid; } chmax(ans, ok); } } cout << ans << '\n'; }
[ "control_flow.branch.if.condition.change", "control_flow.loop.for.condition.change" ]
744,234
744,235
u179970156
cpp
p02913
#define _CRT_SECURE_NO_WARNINGS #include <algorithm> #include <assert.h> #include <cmath> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <new> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <time.h> #include <vector> #define BIT(nr) (1ULL << (nr)) #define int long long #define ll long long #define double long double #define mod 1000000007 #define MAXN (int)1e+5 * 2 + 1 #define LL_MAX 9223372036854775807 //ない環境用 #define LL_HALFMAX 9223372036854775807 / 2 //ない環境用 #define MIN -(9223372036854775807 / 2) #define REP(i, a, n) for (int i = (a); i < (int)(n); i++) #define rep(i, n) REP(i, 0, n) #define FOR(it, c) \ for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it) #define ALLOF(c) (c).begin(), (c).end() #define REPS(i, x) for (int i = 1; i <= (int)(x); i++) #define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--) #define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--) #define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define mp make_pair template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; } template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; } using rh_type = unsigned long long; template <rh_type MOD = 0> struct rolling_hash { int n; rh_type base; std::vector<rh_type> pow, hash; rolling_hash() {} template <typename Array> rolling_hash(const Array &s, rh_type b_ = 10007) { set(s, b_); } template <typename Array> void set(const Array &s, rh_type b_ = 10007) { n = s.size(); base = b_; pow.resize(n + 1); pow[0] = 1; hash.resize(n + 1); hash[0] = 0; for (int i = 0; i < n; ++i) { if (MOD == 0) { pow[i + 1] = pow[i] * base; hash[i + 1] = s[i] + hash[i] * base; } else { pow[i + 1] = pow[i] * base % MOD; hash[i + 1] = (s[i] + hash[i] * base) % MOD; } } } rh_type get(int r) const { return hash[r]; } rh_type get(int l, int r) const { if (MOD == 0) { return get(r) - get(l) * pow[r - l]; } else { return (get(r) - get(l) * pow[r - l] % MOD + MOD) % MOD; } } }; // 1000000007, 1000000009, 1000000021, ... using RH = rolling_hash<10123457689>; using namespace std; // 汎用的な二分探索のテンプレ(めぐる式) int binary_search(function<bool(int)> isOk, int ng, int ok) { /* ok と ng のどちらが大きいかわからないことを考慮 */ while (abs(ok - ng) > 1) { int mid = (ok + ng) / 2; if (isOk(mid)) ok = mid; else ng = mid; } return ok; } signed main() { int N; cin >> N; string S; cin >> S; RH hs(S, 114514); // RH hs2(S, 1919810); auto isOk = [&](int len) -> bool { map<rh_type, vector<int>> ma; rep(i, N - len + 1) { rh_type hash = hs.get(i, i + len); ma[hash].push_back(i); } for (auto p : ma) { int f = p.second[0]; int l = p.second.back(); if (f + len <= l) { return true; } } return false; }; cout << binary_search(isOk, N, 0) << "\n"; return 0; }
#define _CRT_SECURE_NO_WARNINGS #include <algorithm> #include <assert.h> #include <cmath> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <new> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <time.h> #include <vector> #define BIT(nr) (1ULL << (nr)) #define int long long #define ll long long #define double long double #define mod 1000000007 #define MAXN (int)1e+5 * 2 + 1 #define LL_MAX 9223372036854775807 //ない環境用 #define LL_HALFMAX 9223372036854775807 / 2 //ない環境用 #define MIN -(9223372036854775807 / 2) #define REP(i, a, n) for (int i = (a); i < (int)(n); i++) #define rep(i, n) REP(i, 0, n) #define FOR(it, c) \ for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it) #define ALLOF(c) (c).begin(), (c).end() #define REPS(i, x) for (int i = 1; i <= (int)(x); i++) #define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--) #define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--) #define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define mp make_pair template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; } template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; } using rh_type = unsigned long long; template <rh_type MOD = 0> struct rolling_hash { int n; rh_type base; std::vector<rh_type> pow, hash; rolling_hash() {} template <typename Array> rolling_hash(const Array &s, rh_type b_ = 10007) { set(s, b_); } template <typename Array> void set(const Array &s, rh_type b_ = 10007) { n = s.size(); base = b_; pow.resize(n + 1); pow[0] = 1; hash.resize(n + 1); hash[0] = 0; for (int i = 0; i < n; ++i) { if (MOD == 0) { pow[i + 1] = pow[i] * base; hash[i + 1] = s[i] + hash[i] * base; } else { pow[i + 1] = pow[i] * base % MOD; hash[i + 1] = (s[i] + hash[i] * base) % MOD; } } } rh_type get(int r) const { return hash[r]; } rh_type get(int l, int r) const { if (MOD == 0) { return get(r) - get(l) * pow[r - l]; } else { return (get(r) - get(l) * pow[r - l] % MOD + MOD) % MOD; } } }; // 1000000007, 1000000009, 1000000021, ... using RH = rolling_hash<1000000007>; using namespace std; // 汎用的な二分探索のテンプレ(めぐる式) int binary_search(function<bool(int)> isOk, int ng, int ok) { /* ok と ng のどちらが大きいかわからないことを考慮 */ while (abs(ok - ng) > 1) { int mid = (ok + ng) / 2; if (isOk(mid)) ok = mid; else ng = mid; } return ok; } signed main() { int N; cin >> N; string S; cin >> S; RH hs(S); // RH hs2(S, 1919810); auto isOk = [&](int len) -> bool { map<rh_type, vector<int>> ma; rep(i, N - len + 1) { rh_type hash = hs.get(i, i + len); ma[hash].push_back(i); } for (auto p : ma) { int f = p.second[0]; int l = p.second.back(); if (f + len <= l) { return true; } } return false; }; cout << binary_search(isOk, N, 0) << "\n"; return 0; }
[ "literal.number.change", "call.arguments.change" ]
744,244
744,245
u783675879
cpp
p02913
#include <bits/stdc++.h> class rolling_hash { public: using u64 = std::uint_fast64_t; using size_type = std::uint_fast32_t; static constexpr u64 MOD = (1uL << 61) - 1; static constexpr u64 base = 20200213; std::string str; std::vector<u64> hash_, pow; private: static constexpr u64 mask30 = (1ul << 30) - 1; static constexpr u64 mask31 = (1ul << 31) - 1; u64 mul(u64 a, u64 b) const { u64 au = a >> 31; u64 ad = 1 & mask31; u64 bu = b >> 31; u64 bd = b & mask31; u64 mid = ad * bu + au * bd; u64 midu = mid >> 30; u64 midd = mid & mask30; return apply(au * bu * 2 + midu + (midd << 31) + ad * bd); } u64 apply(u64 val) const { val = (val & MOD) + (val >> 61); if (val >= MOD) val -= MOD; return val; } size_type xorshift(size_type x) const { x ^= x << 13; x ^= x >> 17; x ^= x << 5; return x; } public: rolling_hash(const rolling_hash &) = default; rolling_hash(rolling_hash &&) = default; rolling_hash() : str(){}; rolling_hash(const std::string &str) : str(str) { hash_.resize(str.size() + 1, 0); pow.resize(str.size() + 1, 1); for (size_type i = 0; i < str.size(); i++) { hash_[i + 1] = mul(hash_[i], base) + xorshift(str[i] + 1); pow[i + 1] = mul(pow[i], base); if (hash_[i + 1] >= MOD) hash_[i + 1] -= MOD; } } u64 hash() const { return hash_.back(); } u64 hash(size_type l, size_type r) const { u64 ret = MOD + hash_[r] - mul(hash_[l], pow[r - l]); return ret < MOD ? ret : ret - MOD; } size_type size() const { return str.size(); } }; int main() { int n; scanf("%d", &n); std::string s; std::cin >> s; int ans = 0; rolling_hash hash(s); for (int i = 0; i < s.size(); i++) { for (int j = i; j < s.size(); j++) { while (i + ans < j and j + ans < n) { if (hash.hash(i, i + ans + 1) != hash.hash(j, j + ans + 1)) break; ans++; } } } printf("%d\n", ans); return 0; }
#include <bits/stdc++.h> class rolling_hash { public: using u64 = std::uint_fast64_t; using size_type = std::uint_fast32_t; static constexpr u64 MOD = (1uL << 61) - 1; static constexpr u64 base = 20200213; std::string str; std::vector<u64> hash_, pow; private: static constexpr u64 mask30 = (1ul << 30) - 1; static constexpr u64 mask31 = (1ul << 31) - 1; u64 mul(u64 a, u64 b) const { u64 au = a >> 31; u64 ad = a & mask31; u64 bu = b >> 31; u64 bd = b & mask31; u64 mid = ad * bu + au * bd; u64 midu = mid >> 30; u64 midd = mid & mask30; return apply(au * bu * 2 + midu + (midd << 31) + ad * bd); } u64 apply(u64 val) const { val = (val & MOD) + (val >> 61); if (val >= MOD) val -= MOD; return val; } size_type xorshift(size_type x) const { x ^= x << 13; x ^= x >> 17; x ^= x << 5; return x; } public: rolling_hash(const rolling_hash &) = default; rolling_hash(rolling_hash &&) = default; rolling_hash() : str(){}; rolling_hash(const std::string &str) : str(str) { hash_.resize(str.size() + 1, 0); pow.resize(str.size() + 1, 1); for (size_type i = 0; i < str.size(); i++) { hash_[i + 1] = mul(hash_[i], base) + xorshift(str[i] + 1); pow[i + 1] = mul(pow[i], base); if (hash_[i + 1] >= MOD) hash_[i + 1] -= MOD; } } u64 hash() const { return hash_.back(); } u64 hash(size_type l, size_type r) const { u64 ret = MOD + hash_[r] - mul(hash_[l], pow[r - l]); return ret < MOD ? ret : ret - MOD; } size_type size() const { return str.size(); } }; int main() { int n; scanf("%d", &n); std::string s; std::cin >> s; int ans = 0; rolling_hash hash(s); for (int i = 0; i < s.size(); i++) { for (int j = i; j < s.size(); j++) { while (i + ans < j and j + ans < n) { if (hash.hash(i, i + ans + 1) != hash.hash(j, j + ans + 1)) break; ans++; } } } printf("%d\n", ans); return 0; }
[ "identifier.replace.add", "literal.replace.remove", "expression.operation.binary.change" ]
744,258
744,259
u424655672
cpp
p02945
#include <bits/stdc++.h> using namespace std; typedef long long LL; #define ALL(obj) (obj).begin(), (obj).end() #define REP(i, N) for (int i = 0; i < (N); ++i) int main() { int a, b, c, d, e, ans; cin >> a >> b; c = a + b; d = a * b; e = a - b; ans = max(b, c); ans = max(ans, d); cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long LL; #define ALL(obj) (obj).begin(), (obj).end() #define REP(i, N) for (int i = 0; i < (N); ++i) int main() { int a, b, c, d, e, ans; cin >> a >> b; c = a + b; d = a * b; e = a - b; ans = max(c, d); ans = max(ans, e); cout << ans << endl; }
[ "call.arguments.change", "call.arguments.add", "assignment.value.change", "identifier.change" ]
744,282
744,283
u654884616
cpp
p02945
#include <iostream> using namespace std; int main() { int a, b; cin >> a >> b; int s = a + b; if (a - b > s) s = a - b; if (a * b > s) s = s * b; cout << s << endl; return 0; }
#include <iostream> using namespace std; int main() { int a, b; cin >> a >> b; int s = a + b; if (a - b > s) s = a - b; if (a * b > s) s = a * b; cout << s << endl; return 0; }
[ "assignment.value.change", "identifier.change", "expression.operation.binary.change" ]
744,286
744,287
u808738521
cpp
p02945
#include <bits/stdc++.h> #include <cmath> #include <numeric> using namespace std; using ll = long long; #define rep(i, a, b) for (int i = (a); i < (b); ++i) // a ≦ i < b #define Rrep(i, a, b) \ for (int i = (a); i >= (b); --i) // reverse repeat. a から b まで減少. #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() //逆イテレータ #define RANGE(a, b, c) \ (a).begin() + b, (a).begin() + c // コンテナ a の 要素 b から c へのイテレータ // typedef typedef pair<int, int> PII; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<string> VS; typedef vector<PII> VP; int main() { int a, b; cin >> a >> b; cout << max(a + b, max(a - b, a + b)) << "\n"; return 0; }
#include <bits/stdc++.h> #include <cmath> #include <numeric> using namespace std; using ll = long long; #define rep(i, a, b) for (int i = (a); i < (b); ++i) // a ≦ i < b #define Rrep(i, a, b) \ for (int i = (a); i >= (b); --i) // reverse repeat. a から b まで減少. #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() //逆イテレータ #define RANGE(a, b, c) \ (a).begin() + b, (a).begin() + c // コンテナ a の 要素 b から c へのイテレータ // typedef typedef pair<int, int> PII; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<string> VS; typedef vector<PII> VP; int main() { int a, b; cin >> a >> b; cout << max({a + b, a - b, a * b}) << "\n"; return 0; }
[ "call.arguments.change", "call.remove", "expression.operator.arithmetic.change", "io.output.change" ]
744,288
744,289
u421426760
cpp
p02945
#include <bits/stdc++.h> #include <cmath> #include <numeric> using namespace std; using ll = long long; #define rep(i, a, b) for (int i = (a); i < (b); ++i) // a ≦ i < b #define Rrep(i, a, b) \ for (int i = (a); i >= (b); --i) // reverse repeat. a から b まで減少. #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() //逆イテレータ #define RANGE(a, b, c) \ (a).begin() + b, (a).begin() + c // コンテナ a の 要素 b から c へのイテレータ // typedef typedef pair<int, int> PII; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<string> VS; typedef vector<PII> VP; int main() { int a, b; cin >> a >> b; cout << max({a + b, a - b, a + b}) << "\n"; return 0; }
#include <bits/stdc++.h> #include <cmath> #include <numeric> using namespace std; using ll = long long; #define rep(i, a, b) for (int i = (a); i < (b); ++i) // a ≦ i < b #define Rrep(i, a, b) \ for (int i = (a); i >= (b); --i) // reverse repeat. a から b まで減少. #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() //逆イテレータ #define RANGE(a, b, c) \ (a).begin() + b, (a).begin() + c // コンテナ a の 要素 b から c へのイテレータ // typedef typedef pair<int, int> PII; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<string> VS; typedef vector<PII> VP; int main() { int a, b; cin >> a >> b; cout << max({a + b, a - b, a * b}) << "\n"; return 0; }
[ "expression.operator.arithmetic.change", "io.output.change" ]
744,290
744,289
u421426760
cpp
p02945
//#include <bits/stdc++.h> #include <algorithm> #include <iostream> #include <array> #include <bitset> #include <complex> #include <cstring> #include <deque> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <utility> #include <valarray> #include <vector> #include <cassert> #include <chrono> #include <cmath> #include <functional> #include <iomanip> #include <numeric> #include <random> #include <memory> using namespace std; #define int long long typedef long long ll; typedef unsigned long long ull; // typedef unsigned __int128 HASH; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<ull, ull> pullull; typedef pair<ll, int> plli; typedef pair<double, int> pdi; typedef pair<long double, int> pdbi; typedef pair<int, pii> pipii; typedef pair<ll, pll> plpll; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef vector<pii> vpii; typedef vector<vector<int>> mat; #define rep(i, n) for (int i = 0; i < (n); i++) #define rep2(i, a, b) for (int i = (a); i < (b); i++) #define rrep(i, n) for (int i = (n); i > 0; i--) #define rrep2(i, a, b) for (int i = (a); i > b; i--) #define pb push_back #define fi first #define se second #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() const ll hmod1 = 999999937; const ll hmod2 = 1000000000 + 9; const int INF = 1 << 30; const ll INFLL = 1LL << 62; const long double EPS = 1e-12; const ll mod = 1000000000 + 7; // const ll mod = 998244353; const int dx4[4] = {1, 0, -1, 0}; const int dy4[4] = {0, 1, 0, -1}; const int dx8[8] = {1, 1, 1, 0, 0, -1, -1, -1}; const int dy8[8] = {0, 1, -1, 1, -1, 0, 1, -1}; const long double pi = 3.141592653589793; #define addm(X, Y) (X) = ((X) + ((Y) % mod) + mod) % mod #define inside(y, x, h, w) \ (0 <= (y) && (y) < (h) && 0 <= (x) && (x) < (w)) ? true : false // debug #define DEBUG #define DUMPOUT cerr #ifdef DEBUG #define dump(...) \ DUMPOUT << #__VA_ARGS__ << " :[" << __FUNCTION__ << ":" << __LINE__ << "]" \ << endl; \ DUMPOUT << " "; \ dump_func(__VA_ARGS__) #else #define dump(...) #endif void dump_func() { DUMPOUT << endl; }; template <class Head, class... Tail> void dump_func(Head &&head, Tail &&...tail) { DUMPOUT << head; if (sizeof...(Tail) == 0) DUMPOUT << " "; else DUMPOUT << ", "; dump_func(std::move(tail)...); } // ostream template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) { os << "["; for (int i = 0; i < vec.size(); i++) os << vec[i] << (i + 1 == vec.size() ? "" : ", "); os << "]"; return os; } template <typename T, typename U> ostream &operator<<(ostream &os, pair<T, U> &pair_var) { os << "(" << pair_var.first << ", " << pair_var.second << ")"; return os; } template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &map_var) { os << "["; for (auto itr = map_var.begin(); itr != map_var.end(); itr++) { os << "(" << itr->first << ", " << itr->second << ")"; itr++; if (itr != map_var.end()) os << ", "; itr--; } os << "]"; return os; } template <typename T> ostream &operator<<(ostream &os, set<T> &set_var) { os << "["; for (auto itr = set_var.begin(); itr != set_var.end(); itr++) { os << *itr; ++itr; if (itr != set_var.end()) os << ", "; itr--; } os << "]"; return os; } int a, b; signed main() { cin.tie(0); ios::sync_with_stdio(false); cin >> a >> b; cout << max({a + b, a - b, a + b}) << endl; }
//#include <bits/stdc++.h> #include <algorithm> #include <iostream> #include <array> #include <bitset> #include <complex> #include <cstring> #include <deque> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <utility> #include <valarray> #include <vector> #include <cassert> #include <chrono> #include <cmath> #include <functional> #include <iomanip> #include <numeric> #include <random> #include <memory> using namespace std; #define int long long typedef long long ll; typedef unsigned long long ull; // typedef unsigned __int128 HASH; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<ull, ull> pullull; typedef pair<ll, int> plli; typedef pair<double, int> pdi; typedef pair<long double, int> pdbi; typedef pair<int, pii> pipii; typedef pair<ll, pll> plpll; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef vector<pii> vpii; typedef vector<vector<int>> mat; #define rep(i, n) for (int i = 0; i < (n); i++) #define rep2(i, a, b) for (int i = (a); i < (b); i++) #define rrep(i, n) for (int i = (n); i > 0; i--) #define rrep2(i, a, b) for (int i = (a); i > b; i--) #define pb push_back #define fi first #define se second #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() const ll hmod1 = 999999937; const ll hmod2 = 1000000000 + 9; const int INF = 1 << 30; const ll INFLL = 1LL << 62; const long double EPS = 1e-12; const ll mod = 1000000000 + 7; // const ll mod = 998244353; const int dx4[4] = {1, 0, -1, 0}; const int dy4[4] = {0, 1, 0, -1}; const int dx8[8] = {1, 1, 1, 0, 0, -1, -1, -1}; const int dy8[8] = {0, 1, -1, 1, -1, 0, 1, -1}; const long double pi = 3.141592653589793; #define addm(X, Y) (X) = ((X) + ((Y) % mod) + mod) % mod #define inside(y, x, h, w) \ (0 <= (y) && (y) < (h) && 0 <= (x) && (x) < (w)) ? true : false // debug #define DEBUG #define DUMPOUT cerr #ifdef DEBUG #define dump(...) \ DUMPOUT << #__VA_ARGS__ << " :[" << __FUNCTION__ << ":" << __LINE__ << "]" \ << endl; \ DUMPOUT << " "; \ dump_func(__VA_ARGS__) #else #define dump(...) #endif void dump_func() { DUMPOUT << endl; }; template <class Head, class... Tail> void dump_func(Head &&head, Tail &&...tail) { DUMPOUT << head; if (sizeof...(Tail) == 0) DUMPOUT << " "; else DUMPOUT << ", "; dump_func(std::move(tail)...); } // ostream template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) { os << "["; for (int i = 0; i < vec.size(); i++) os << vec[i] << (i + 1 == vec.size() ? "" : ", "); os << "]"; return os; } template <typename T, typename U> ostream &operator<<(ostream &os, pair<T, U> &pair_var) { os << "(" << pair_var.first << ", " << pair_var.second << ")"; return os; } template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &map_var) { os << "["; for (auto itr = map_var.begin(); itr != map_var.end(); itr++) { os << "(" << itr->first << ", " << itr->second << ")"; itr++; if (itr != map_var.end()) os << ", "; itr--; } os << "]"; return os; } template <typename T> ostream &operator<<(ostream &os, set<T> &set_var) { os << "["; for (auto itr = set_var.begin(); itr != set_var.end(); itr++) { os << *itr; ++itr; if (itr != set_var.end()) os << ", "; itr--; } os << "]"; return os; } int a, b; signed main() { cin.tie(0); ios::sync_with_stdio(false); cin >> a >> b; cout << max({a + b, a - b, a * b}) << endl; }
[ "expression.operator.arithmetic.change", "io.output.change" ]
744,293
744,294
u272028993
cpp
p02945
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; const ll NN = 1e6 + 10; ll a, b; int main() { cin >> a >> b; ll ma = 0; ma = max(ma, a + b); ma = max(ma, a - b); ma = max(ma, a * b); cout << ma << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; const ll NN = 1e6 + 10; ll a, b; int main() { cin >> a >> b; ll ma = -1e10; ma = max(ma, a + b); ma = max(ma, a - b); ma = max(ma, a * b); cout << ma << endl; return 0; }
[ "literal.number.change", "variable_declaration.value.change" ]
744,295
744,296
u682427554
cpp
p02945
#include <iostream> #include <stdio.h> using namespace std; int main() { int A, B; cin >> A >> B; int m = A + B; if (m > A - B) { m = A - B; } if (m > A * B) { m = A * B; } cout << m << endl; }
#include <iostream> #include <stdio.h> using namespace std; int main() { int A, B; cin >> A >> B; int m = A + B; if (m < A - B) { m = A - B; } if (m < A * B) { m = A * B; } cout << m << endl; }
[ "misc.opposites", "expression.operator.compare.change", "control_flow.branch.if.condition.change" ]
744,303
744,304
u937950774
cpp
p02945
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { ll A, B, a[3]; scanf("%lld %lld", &A, &B); a[0] = A - B; a[1] = A * B; a[2] = A + B; sort(a, a + 2); printf("%lld", a[2]); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { ll A, B, a[3]; scanf("%lld %lld", &A, &B); a[0] = A - B; a[1] = A * B; a[2] = A + B; sort(a, a + 3); printf("%lld", a[2]); return 0; }
[ "literal.number.change", "call.arguments.change", "expression.operation.binary.change" ]
744,330
744,331
u083496316
cpp
p02945
#include <bits/stdc++.h> using namespace std; int main() { int a, b, MX = -1000000; cin >> a >> b; max(MX, max(a - b, max(a * b, a + b))); cout << MX << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a, b, MX = -1e9; cin >> a >> b; MX = max(MX, max(a - b, max(a * b, a + b))); cout << MX << endl; return 0; }
[ "literal.number.change", "variable_declaration.value.change", "assignment.change" ]
744,347
744,348
u418568023
cpp
p02945
#include <bits/stdc++.h> using namespace std; int main() { int a, b, MX = -1e9; cin >> a >> b; max(MX, max(a - b, max(a * b, a + b))); cout << MX << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a, b, MX = -1e9; cin >> a >> b; MX = max(MX, max(a - b, max(a * b, a + b))); cout << MX << endl; return 0; }
[ "assignment.change" ]
744,350
744,348
u418568023
cpp
p02945
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; const int inf = 2147483647, dx[] = {-1, 0, 1, 0}, dy[] = {0, -1, 0, 1}; // 上 左 下 右 const int N = 100005, M = 1000005, K = 200005, mod = 1000000007; const long long llinf = 9223372036854775807ll; // int & long long int a, b; int main() { cin >> a >> b; cout << max(max(a + 1, a - b), a * b); return 0; }
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; const int inf = 2147483647, dx[] = {-1, 0, 1, 0}, dy[] = {0, -1, 0, 1}; // 上 左 下 右 const int N = 100005, M = 1000005, K = 200005, mod = 1000000007; const long long llinf = 9223372036854775807ll; // int & long long int a, b; int main() { cin >> a >> b; cout << max(max(a + b, a - b), a * b); return 0; }
[ "identifier.replace.add", "literal.replace.remove", "io.output.change" ]
744,358
744,359
u965118468
cpp
p02945
#include <bits/stdc++.h> using namespace std; int main() { int a, b; cin >> a >> b; cout << max((a + b, a - b), a * b); }
#include <bits/stdc++.h> using namespace std; int main() { int a, b; cin >> a >> b; cout << max(max(a + b, a - b), a * b); }
[ "call.add", "call.arguments.change" ]
744,370
744,371
u661467504
cpp
p02945
#include <assert.h> #include <math.h> #include <algorithm> #include <functional> #include <iostream> #include <numeric> #include <vector> template <typename T> void in(T &x) { std::cin >> x; } template <typename T> void out(const T H) { std::cout << (H); } template <typename T1, typename... T2> void out(const T1 H, const T2... T) { std::cout << (H) << " "; out(T...); } template <typename T1, typename T2> std::ostream &operator<<(std::ostream &os, const std::pair<T1, T2> &v) { return os << v.first << " " << v.second; } template <typename T> std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) { int n = (int)v.size(); for (int i = 0; i < n - 1; ++i) os << v[i] << ' '; return os << v[n - 1]; } template <typename T> void eout(const char *s, const T H) { std::cerr << s << "=" << H << "\n"; } template <typename T1, typename... T2> void eout(const char *s, const T1 H, const T2... T) { while (*s != ',') std::cerr << *s++; std::cerr << "=" << H << " | "; eout(++s, T...); } #define nl std::cout << "\n" #define err(args...) eout(#args, args) #define inn(x) \ int x; \ in(x) #define rep(i, n) for (int i = 0; i < (n); ++i) #define per(i, n) for (int i = (n)-1; i > -1; --i) #define repr(i, l, r) for (int i = (l); i < (r); ++i) #define sz(v) ((int)v.size()) #define all(v) (v).begin(), (v).end() #define lla(v) (v).rbegin(), (v).rend() #define no std::cout << "NO\n" #define yes std::cout << "YES\n" const int INF = 2147483647; const long long int LNF = 9223372036854775807LL; using ll = long long int; using vi = std::vector<int>; using vb = std::vector<bool>; using ii = std::pair<int, int>; using iii = std::pair<ii, int>; using vii = std::vector<ii>; const int N = 505050; const int MD = 1000 * 1000 * 1000 + 7; int solve(); int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(0); // std::cout.tie(0); // inn(t); repr(tc, 1, t + 1) solve(); return 0; } int solve() { inn(a); inn(b); out(std::min(a * b, std::min(a - b, a + b))); return 0; }
#include <assert.h> #include <math.h> #include <algorithm> #include <functional> #include <iostream> #include <numeric> #include <vector> template <typename T> void in(T &x) { std::cin >> x; } template <typename T> void out(const T H) { std::cout << (H); } template <typename T1, typename... T2> void out(const T1 H, const T2... T) { std::cout << (H) << " "; out(T...); } template <typename T1, typename T2> std::ostream &operator<<(std::ostream &os, const std::pair<T1, T2> &v) { return os << v.first << " " << v.second; } template <typename T> std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) { int n = (int)v.size(); for (int i = 0; i < n - 1; ++i) os << v[i] << ' '; return os << v[n - 1]; } template <typename T> void eout(const char *s, const T H) { std::cerr << s << "=" << H << "\n"; } template <typename T1, typename... T2> void eout(const char *s, const T1 H, const T2... T) { while (*s != ',') std::cerr << *s++; std::cerr << "=" << H << " | "; eout(++s, T...); } #define nl std::cout << "\n" #define err(args...) eout(#args, args) #define inn(x) \ int x; \ in(x) #define rep(i, n) for (int i = 0; i < (n); ++i) #define per(i, n) for (int i = (n)-1; i > -1; --i) #define repr(i, l, r) for (int i = (l); i < (r); ++i) #define sz(v) ((int)v.size()) #define all(v) (v).begin(), (v).end() #define lla(v) (v).rbegin(), (v).rend() #define no std::cout << "NO\n" #define yes std::cout << "YES\n" const int INF = 2147483647; const long long int LNF = 9223372036854775807LL; using ll = long long int; using vi = std::vector<int>; using vb = std::vector<bool>; using ii = std::pair<int, int>; using iii = std::pair<ii, int>; using vii = std::vector<ii>; const int N = 505050; const int MD = 1000 * 1000 * 1000 + 7; int solve(); int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(0); // std::cout.tie(0); // inn(t); repr(tc, 1, t + 1) solve(); return 0; } int solve() { inn(a); inn(b); out(std::max(a * b, std::max(a - b, a + b))); return 0; }
[ "misc.opposites", "identifier.change", "call.arguments.change" ]
744,380
744,381
u811925505
cpp
p02945
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define MOD (1000000007); int main() { ll a, b; cin >> a, b; ll ans = max({(a + b), (a - b), (a * b)}); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define MOD (1000000007); int main() { ll a, b; cin >> a >> b; ll ans = max({(a + b), (a - b), (a * b)}); cout << ans << endl; return 0; }
[]
744,388
744,389
u493491082
cpp
p02945
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <queue> #include <string> using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < (int)(n); i++) int gcd(int a, int b) { int t; while (b > 0) { t = b; b = a % b; a = t; } return a; } int lcm(int a, int b) { return a * b / gcd(a, b); } int main() { ll a, b, c, d, e, k, l, m, n, o = 0, p, q, r, x, y, z, max1 = 0, ans = 0; string s, t, u; cin >> a, b; cout << max(a + b, max(a - b, a * b)) << endl; }
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <queue> #include <string> using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < (int)(n); i++) int gcd(int a, int b) { int t; while (b > 0) { t = b; b = a % b; a = t; } return a; } int lcm(int a, int b) { return a * b / gcd(a, b); } int main() { ll a, b, c, d, e, k, l, m, n, o = 0, p, q, r, x, y, z, max1 = 0, ans = 0; string s, t, u; cin >> a >> b; cout << max(a + b, max(a - b, a * b)) << endl; }
[]
744,390
744,391
u953532675
cpp
p02945
// https://algospot.com/judge/problem/read/CLOCKSYNC #include <algorithm> #include <climits> #include <cstdio> #include <functional> #include <iostream> #include <map> #include <queue> #include <stack> #include <string> #include <utility> #include <vector> #define LF '\n'; #define MOD 1000000007 typedef unsigned long long ull; typedef long long ll; using namespace std; int main(void) { ios::sync_with_stdio(false); int a, b; cin >> a >> b; cout << max(a * b, max(a - b, a * b)); return 0; }
// https://algospot.com/judge/problem/read/CLOCKSYNC #include <algorithm> #include <climits> #include <cstdio> #include <functional> #include <iostream> #include <map> #include <queue> #include <stack> #include <string> #include <utility> #include <vector> #define LF '\n'; #define MOD 1000000007 typedef unsigned long long ull; typedef long long ll; using namespace std; int main(void) { ios::sync_with_stdio(false); int a, b; cin >> a >> b; cout << max(a + b, max(a - b, a * b)); return 0; }
[ "expression.operator.arithmetic.change", "io.output.change" ]
744,396
744,397
u888251784
cpp
p02945
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 10; int a, b; int main() { cin >> a >> b; cout << max({a + b, a - 1, a * b}); } /* Good Luck -Lucina */
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 10; int a, b; int main() { cin >> a >> b; cout << max({a + b, a - b, a * b}); } /* Good Luck -Lucina */
[ "identifier.replace.add", "literal.replace.remove", "io.output.change" ]
744,400
744,401
u047398677
cpp
p02945
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i) #define debug(x) cerr << #x << " = " << x << "\n" #define debugv(x) \ rep(i, x.size()) cerr << x[i] << (i == x.size() - 1 ? "\n" : " ") #define all(x) x.begin(), x.end() #define int long long using namespace std; typedef long long ll; typedef pair<int, int> pii; const int INF = ((1LL << 30) + (1LL << 29)); const int MOD = 1000000007; //============================= int N, M, K; string S, T; vector<int> A; vector<int> B; //============================= void input() { cin >> N >> M; } //============================= void func() {} //============================= void solve() { cout << max({M * N, M - N, M + N}) << endl; } //============================= signed main() { input(); solve(); return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i) #define debug(x) cerr << #x << " = " << x << "\n" #define debugv(x) \ rep(i, x.size()) cerr << x[i] << (i == x.size() - 1 ? "\n" : " ") #define all(x) x.begin(), x.end() #define int long long using namespace std; typedef long long ll; typedef pair<int, int> pii; const int INF = ((1LL << 30) + (1LL << 29)); const int MOD = 1000000007; //============================= int N, M, K; string S, T; vector<int> A; vector<int> B; //============================= void input() { cin >> N >> M; } //============================= void func() {} //============================= void solve() { cout << max({M * N, N - M, M + N}) << endl; } //============================= signed main() { input(); solve(); return 0; }
[ "expression.operation.binary.remove" ]
744,402
744,403
u265065462
cpp
p02945
#include <bits/stdc++.h> using namespace std; int main() { int A, B, a, b, c; cin >> A >> B; a = A + b; b = A - B; c = A * B; vector<int> AA(3); AA.at(0) = a; AA.at(1) = b; AA.at(2) = c; sort(AA.begin(), AA.end()); cout << AA.at(2) << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int A, B, a, b, c; cin >> A >> B; a = A + B; b = A - B; c = A * B; vector<int> AA(3); AA.at(0) = a; AA.at(1) = b; AA.at(2) = c; sort(AA.begin(), AA.end()); cout << AA.at(2) << endl; }
[ "assignment.value.change", "identifier.change", "expression.operation.binary.change" ]
744,412
744,413
u804980376
cpp
p02945
#include <bits/stdc++.h> using namespace std; int main() { int a, b, ret; cin >> a >> b; ret = a + b; ret = a - b > ret ? a - b : ret; ret = a * b > ret ? a - b : ret; cout << ret << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int a, b, ret; cin >> a >> b; ret = a + b; ret = a - b > ret ? a - b : ret; ret = a * b > ret ? a * b : ret; cout << ret << endl; }
[ "expression.operator.arithmetic.change", "expression.operation.binary.change" ]
744,414
744,415
u065089468
cpp
p02945
#include <algorithm> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <string> #include <utility> using namespace std; #define ll long long int main() { int A, B, max = 0; cin >> A >> B; if (A + B > max) max = A + B; if (A - B > max) max = A - B; if (A * B > max) max = A * B; cout << max << endl; return 0; }
#include <algorithm> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <string> #include <utility> using namespace std; #define ll long long int main() { int A, B, max = -200; cin >> A >> B; if (A + B > max) max = A + B; if (A - B > max) max = A - B; if (A * B > max) max = A * B; cout << max << endl; return 0; }
[ "literal.number.change", "variable_declaration.value.change" ]
744,428
744,429
u668443884
cpp
p02945
#include <bits/stdc++.h> using namespace std; int main() { int A, B; cin >> A >> B; int s; int d; int m; s = A + B; d = A - B; m = A * B; int max; if (s >= d) { max = s; if (m >= s) { max = m; } } else { max = d; if (m <= d) { max = m; } } cout << max << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int A, B; cin >> A >> B; int s; int d; int m; s = A + B; d = A - B; m = A * B; int max; if (s >= d) { max = s; if (m >= s) { max = m; } } else { max = d; if (m >= d) { max = m; } } cout << max << endl; }
[ "misc.opposites", "expression.operator.compare.change", "control_flow.branch.if.condition.change" ]
744,438
744,439
u566571310
cpp
p02945
#include <bits/stdc++.h> using namespace std; int main() { int A, B; cin >> A; cout << max(A + B, max(A - B, A * B)) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int A, B; cin >> A >> B; cout << max(A + B, max(A - B, A * B)) << endl; return 0; }
[ "expression.operation.binary.add" ]
744,442
744,443
u239087789
cpp
p02945
#include <bits/stdc++.h> using namespace std; #define speed \ ios_base::sync_with_stdio(0); \ cin.tie(NULL); \ cout.tie(NULL); #define test \ ll t; \ cin >> t; \ while (t--) #define ub(a, x) upper_bound(a.begin(), a.end(), x) - a.begin() #define lb(a, x) lower_bound(a.begin(), a.end(), x) - a.begin() #define ll long long #define endl "\n" #define all(v) v.begin(), v.end() #define ff first #define ss second #define pb push_back #define PI 3.1415926535897932384626 #define inf 1e18 #define forn(i, n) for (int i = 0; i < int(n); i++) #define MOD 1000000007 int main() { speed; ll a, b; cin >> a, b; cout << max(a - b, max(a + b, a * b)) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define speed \ ios_base::sync_with_stdio(0); \ cin.tie(NULL); \ cout.tie(NULL); #define test \ ll t; \ cin >> t; \ while (t--) #define ub(a, x) upper_bound(a.begin(), a.end(), x) - a.begin() #define lb(a, x) lower_bound(a.begin(), a.end(), x) - a.begin() #define ll long long #define endl "\n" #define all(v) v.begin(), v.end() #define ff first #define ss second #define pb push_back #define PI 3.1415926535897932384626 #define inf 1e18 #define forn(i, n) for (int i = 0; i < int(n); i++) #define MOD 1000000007 int main() { speed; ll a, b; cin >> a >> b; cout << max(a - b, max(a + b, a * b)) << endl; return 0; }
[]
744,454
744,455
u995696863
cpp
p02945
#include <algorithm> #include <bits/stdc++.h> #include <cmath> #include <cstdlib> using namespace std; int max(int x, int y) { return (x > y ? x : y); } int main() { int a, b; cin >> a >> b; int ans = max(a + b, a - b); ans = max(ans, a * b); return ans; }
#include <algorithm> #include <bits/stdc++.h> #include <cmath> #include <cstdlib> using namespace std; int max(int x, int y) { return (x > y ? x : y); } int main() { int a, b; cin >> a >> b; int ans = max(a + b, a - b); ans = max(ans, a * b); cout << ans; }
[ "io.output.change" ]
744,460
744,461
u098679988
cpp
p02945
#include <bits/stdc++.h> using namespace std; #define ll long long #define fi first #define se second #define pb push_back #define all(x) (x).begin(), (x).end() #define rep(i, a, b) for (ll i = a; i < b; i += 1) #define pp pair<ll, ll> #define sz(v) int(v.size()) #define hell (ll)1000000007 #define hell1 (ll)100000009 #define hell2 (ll)998244353 ll t = 1; void solve() { ll a, b; cin >> a >> b; cout << max(a - b, max(a - b, a * b)); } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); // cin>>t ; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define fi first #define se second #define pb push_back #define all(x) (x).begin(), (x).end() #define rep(i, a, b) for (ll i = a; i < b; i += 1) #define pp pair<ll, ll> #define sz(v) int(v.size()) #define hell (ll)1000000007 #define hell1 (ll)100000009 #define hell2 (ll)998244353 ll t = 1; void solve() { ll a, b; cin >> a >> b; cout << max(a + b, max(a - b, a * b)); } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); // cin>>t ; while (t--) { solve(); } return 0; }
[ "misc.opposites", "expression.operator.arithmetic.change", "io.output.change" ]
744,469
744,470
u889695981
cpp
p02945
#include <bits/stdc++.h> using namespace std; struct compare { bool operator()(const int &first, const int &second) { return first > second; } }; void solve() { long long a, b; cin >> a >> b; int x = a * b; int y = a + b; int z = a - b; int mx = (x, y); cout << max(z, mx) << endl; } int main() { int t; t = 1; while (t--) { solve(); } }
#include <bits/stdc++.h> using namespace std; struct compare { bool operator()(const int &first, const int &second) { return first > second; } }; void solve() { long long a, b; cin >> a >> b; int x = a * b; int y = a + b; int z = a - b; int mx = max(x, y); cout << max(z, mx) << endl; } int main() { int t; t = 1; while (t--) { solve(); } }
[ "call.add" ]
744,477
744,478
u466983830
cpp
p02945
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < n; ++i) #define REPP(i, n) for (int i = 1; i <= n; ++i) #define REPR(i, n) for (int i = n - 1; i >= 0; --i) #define FOR(i, m, n) for (int i = m; i < n; ++i) #define ALL(v) v.begin(), v.end() #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()); using namespace std; typedef long long ll; typedef pair<ll, ll> P; const ll MOD = 1000000007; const ll INF = 1000000000; int main() { int a, b; cin >> a >> b; int ans = max({a + b, a - b, a + b}); cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < n; ++i) #define REPP(i, n) for (int i = 1; i <= n; ++i) #define REPR(i, n) for (int i = n - 1; i >= 0; --i) #define FOR(i, m, n) for (int i = m; i < n; ++i) #define ALL(v) v.begin(), v.end() #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()); using namespace std; typedef long long ll; typedef pair<ll, ll> P; const ll MOD = 1000000007; const ll INF = 1000000000; int main() { int a, b; cin >> a >> b; int ans = max({a + b, a - b, a * b}); cout << ans << endl; return 0; }
[ "expression.operator.arithmetic.change", "call.arguments.change", "expression.operation.binary.change" ]
744,481
744,482
u881647420
cpp
p02945
#include <iostream> using namespace std; int main() { int A, B; cin >> A >> B; if (A > 0 && B > 0) { if (A + B > A * B) { cout << (A + B) << endl; } else { cout << (A * B) << endl; } } else if (A > 0 && B == 0) { cout << A << endl; } else if (A > 0 && B < 0) { cout << (A - B) << endl; } else if (A == 0 && B > 0) { cout << B << endl; } else if (A == 0 && B == 0) { cout << 0 << endl; } else if (A == 0 && B < 0) { cout << (A - B) << endl; } else if (A < 0 && B > 0) { cout << (B - A) << endl; } else if (A < 0 && B < 0) { cout << A << endl; } else if (A < 0 && B < 0) { if (A - B > A * B) { cout << (A - B) << endl; } else { cout << (A * B) << endl; } } return 0; }
#include <iostream> using namespace std; int main() { int A, B; cin >> A >> B; if (A > 0 && B > 0) { if (A + B > A * B) { cout << (A + B) << endl; } else { cout << (A * B) << endl; } } else if (A > 0 && B == 0) { cout << A << endl; } else if (A > 0 && B < 0) { cout << (A - B) << endl; } else if (A == 0 && B > 0) { cout << B << endl; } else if (A == 0 && B == 0) { cout << 0 << endl; } else if (A == 0 && B < 0) { cout << (A - B) << endl; } else if (A < 0 && B > 0) { cout << (A + B) << endl; } else if (A < 0 && B == 0) { cout << A << endl; } else if (A < 0 && B < 0) { if (A - B > A * B) { cout << (A - B) << endl; } else { cout << (A * B) << endl; } } return 0; }
[ "expression.operation.binary.remove", "expression.operator.compare.change", "control_flow.branch.if.condition.change" ]
744,497
744,498
u763012775
cpp
p02945
#include <stdio.h> int main() { int a, b; scanf("%d%d", &a, &b); int x, y, z; x = a + b; y = a - b; z = a * b; if (x >= y || x >= z) { printf("%d\n", x); } else if (y >= z || y >= x) { printf("%d\n", y); } else if (z >= x, z >= y) { printf("%d\n", z); } }
#include <stdio.h> int main() { int a, b; scanf("%d%d", &a, &b); int x, y, z; x = a + b; y = a - b; z = a * b; if (x >= y && x >= z) { printf("%d\n", x); } else if (y >= z && y >= x) { printf("%d\n", y); } else if (z >= x && z >= y) { printf("%d\n", z); } // printf("%d%d%d",x,y,z); }
[ "misc.opposites", "control_flow.branch.if.condition.change" ]
744,511
744,512
u937776252
cpp
p02945
#include <bits/stdc++.h> using namespace std; int main(void) { int max; int A, B; cin >> A >> B; max = A + B; if (max < A - B) { max = A - B; } if (max = A * B) { max = A * B; } cout << max; }
#include <bits/stdc++.h> using namespace std; int main(void) { int max; int A, B; cin >> A >> B; max = A + B; if (max < A - B) { max = A - B; } if (max < A * B) { max = A * B; } cout << max; }
[ "control_flow.branch.if.condition.change" ]
744,516
744,517
u454600293
cpp
p02945
#include <iostream> using namespace std; int main() { int a, b; cin >> a >> b; int maxAns = 0; int x = a + b, y = a - b, z = a * b; maxAns = max(x, y); maxAns = max(y, z); cout << maxAns << endl; }
#include <iostream> using namespace std; int main() { int a, b; cin >> a >> b; int maxAns = 0; int x = a + b, y = a - b, z = a * b; maxAns = max(x, y); maxAns = max(maxAns, z); cout << maxAns << endl; }
[ "assignment.value.change", "identifier.change", "call.arguments.change" ]
744,532
744,533
u185037583
cpp
p02945
#include <iostream> using namespace std; int main(void) { int A, B, C[3], max, i; cin >> A; cin >> B; C[0] = A + B; C[1] = A - B; C[2] = A * B; max = C[0]; for (i = 0; i <= 2; i++) { if (max > C[i]) { max = C[i]; } } cout << max << endl; return 0; }
#include <iostream> using namespace std; int main(void) { int A, B, C[3], max, i; cin >> A; cin >> B; C[0] = A + B; C[1] = A - B; C[2] = A * B; max = C[0]; for (i = 0; i <= 2; i++) { if (max < C[i]) { max = C[i]; } } cout << max << endl; return 0; }
[ "misc.opposites", "expression.operator.compare.change", "control_flow.branch.if.condition.change" ]
744,542
744,543
u566116575
cpp
p02946
#include <bits/stdc++.h> #define _GLIBCXX_DEBUG #define rep(i, n) for (int i = 0; i < (n); ++i) #define repi(i, a, b) for (int i = int(a); i < int(b); ++i) #define all(x) (x).begin(), (x).end() #define PI 3.14159265358979323846264338327950L using namespace std; typedef long long ll; typedef long double ld; int main() { int k, x; cin >> k >> x; repi(i, x - k + 1, x + k - 1) { cout << i << " "; } }
#include <bits/stdc++.h> #define _GLIBCXX_DEBUG #define rep(i, n) for (int i = 0; i < (n); ++i) #define repi(i, a, b) for (int i = int(a); i < int(b); ++i) #define all(x) (x).begin(), (x).end() #define PI 3.14159265358979323846264338327950L using namespace std; typedef long long ll; typedef long double ld; int main() { int k, x; cin >> k >> x; repi(i, x - k + 1, x + k) { cout << i << " "; } }
[ "expression.operation.binary.remove" ]
744,561
744,562
u151236434
cpp
p02946
#include <iostream> using namespace std; int main(void) { // Your code here! int k, x; cin >> k >> x; int max = x + (k - 1); int min = x - (k - 1); for (int i = min; i <= max; i++) { cout << i; } }
#include <iostream> using namespace std; int main(void) { // Your code here! int k, x; cin >> k >> x; int max = x + (k - 1); int min = x - (k - 1); for (int i = min; i <= max; i++) { cout << i << " "; } }
[ "io.output.change" ]
744,571
744,572
u178938628
cpp
p02946
#include <iostream> using namespace std; int main(void) { // Your code here! int a, b, k, j; cin >> a >> b; k = a + b - 1; j = b - a + 1; for (int z = j; z < k; z++) { if (z < 1000000 || z > -1000000) { cout << z << " "; } } cout << endl; }
#include <iostream> using namespace std; int main(void) { // Your code here! int a, b, k, j; cin >> a >> b; k = a + b; j = b - a + 1; for (int z = j; z < k; z++) { if (z < 1000000 || z > -1000000) { cout << z << " "; } } cout << endl; }
[ "expression.operation.binary.remove" ]
744,573
744,574
u783388585
cpp
p02946
#include <bits/stdc++.h> using namespace std; int main() { int k, x; scanf("%d %d", &k, &x); for (int i = x - k + 1; i < x; i++) { printf("%d", i); } for (int i = x; i < x + k; i++) { printf("%d", i); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int k, x; scanf("%d %d", &k, &x); for (int i = x - k + 1; i < x; i++) { printf("%d ", i); } for (int i = x; i < x + k; i++) { printf("%d ", i); } return 0; }
[ "literal.string.change", "call.arguments.change", "io.output.change" ]
744,577
744,578
u379682992
cpp
p02946
#include <bits/stdc++.h> using namespace std; int main() { long long t, j, p, r, i; cin >> t >> j; p = t - j + 1; if (p < -1000000) p = -1000000; r = t + j - 1; if (r > 1000000) r = 1000000; for (i = p; i <= r; i++) cout << i << " "; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long t, j, p, r, i; cin >> j >> t; p = t - j + 1; if (p < -1000000) p = -1000000; r = t + j - 1; if (r > 1000000) r = 1000000; for (i = p; i <= r; i++) cout << i << " "; return 0; }
[ "expression.operation.binary.remove" ]
744,605
744,606
u662180008
cpp
p02946
#include <bits/stdc++.h> using namespace std; int main() { int K, X; cin >> K >> X; for (int i = X - K + 1; i < X + K - 1; i++) { cout << i; if (i == X + K - 2) cout << endl; else cout << ' '; } }
#include <bits/stdc++.h> using namespace std; int main() { int K, X; cin >> K >> X; for (int i = X - K + 1; i <= X + K - 1; i++) { cout << i; if (i == X + K - 1) cout << endl; else cout << ' '; } }
[ "expression.operator.compare.change", "control_flow.loop.for.condition.change", "expression.off_by_one", "expression.operation.binary.change", "literal.number.change", "control_flow.branch.if.condition.change" ]
744,618
744,619
u306482197
cpp
p02946
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) int main() { int k, x; cin >> k >> x; rep(i, k * 2) { if (i != k * 2 - 1) cout << x - k + 1 + i << " "; else cout << x - k + 1 + i << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) int main() { int k, x; cin >> k >> x; rep(i, k * 2 - 1) { if (i != k * 2 - 2) cout << x - k + 1 + i << " "; else cout << x - k + 1 + i << endl; } return 0; }
[ "literal.number.change", "control_flow.branch.if.condition.change" ]
744,625
744,626
u442855610
cpp
p02946
#include <bits/stdc++.h> using namespace std; int main(void) { int k, x; cin >> k >> x; for (int i = x - k + 1; i < x + k - 1; i++) { cout << i << " "; } return 0; }
#include <bits/stdc++.h> using namespace std; int main(void) { int k, x; cin >> k >> x; for (int i = x - k + 1; i <= x + k - 1; i++) { cout << i << " "; } return 0; }
[ "expression.operator.compare.change", "control_flow.loop.for.condition.change", "expression.off_by_one", "expression.operation.binary.change" ]
744,627
744,628
u716903426
cpp
p02946
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); ++i) using P = pair<int, int>; using ll = long long; static const int INF = 1000000000; static const ll MOD = 1000000007; int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); int k, x; cin >> k >> x; int left = x - k - 1; int right = x + k - 1; for (int i = left; i <= right; ++i) { cout << i << " "; } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); ++i) using P = pair<int, int>; using ll = long long; static const int INF = 1000000000; static const ll MOD = 1000000007; int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); int k, x; cin >> k >> x; int left = x - (k - 1); int right = x + (k - 1); for (int i = left; i <= right; ++i) { cout << i << " "; } cout << endl; return 0; }
[]
744,643
744,644
u741815956
cpp
p02946
#include <bits/stdc++.h> using namespace std; int main() { int K, X; cin >> K >> X; for (long int i = (X - K); i < (X + K + 1); i++) { cout << i; if (i == X + K) { cout << endl; } else { cout << " "; } } }
#include <bits/stdc++.h> using namespace std; int main() { int K, X; cin >> K >> X; for (long int i = (X - K + 1); i < (X + K); i++) { cout << i; if (i == X + K - 1) { cout << endl; } else { cout << " "; } } }
[ "control_flow.loop.for.initializer.change", "control_flow.loop.for.condition.change", "expression.operation.binary.remove", "control_flow.branch.if.condition.change", "misc.off_by_one" ]
744,647
744,648
u436660228
cpp
p02946
#include <bits/stdc++.h> using namespace std; int main() { int K, X; cin >> K >> X; for (int i = 0; i < (2 * K - 1); i++) { cout << X + 1 - K + i << (i == 2 * K - 1 ? " " : ""); } cout << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int K, X; cin >> K >> X; for (int i = 0; i < (2 * K - 1); i++) { cout << X + 1 - K + i << (i != 2 * K - 1 ? " " : ""); } cout << endl; }
[ "misc.opposites", "expression.operator.compare.change", "control_flow.loop.for.condition.change", "io.output.change" ]
744,651
744,652
u669315378
cpp