problem_id stringlengths 6 6 | language stringclasses 2 values | original_status stringclasses 3 values | original_src stringlengths 19 243k | changed_src stringlengths 19 243k | change stringclasses 3 values | i1 int64 0 8.44k | i2 int64 0 8.44k | j1 int64 0 8.44k | j2 int64 0 8.44k | error stringclasses 270 values | stderr stringlengths 0 226k |
|---|---|---|---|---|---|---|---|---|---|---|---|
p02794 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
long N;
cin >> N;
vector<vector<pair<long, long>>> A(N);
for (long i = 0; i < N - 1; i++) {
long a, b;
cin >> a >> b;
A[a - 1].push_back(pair<long, long>(b - 1, i));
A[b - 1].push_back(pair<long, long>(a - 1, i));
}
long M;
cin >> M;
vector<long> U(M), V(M);
for (long i = 0; i < M; i++)
cin >> U[i] >> V[i];
vector<bitset<50>> B(M);
for (long i = 0; i < M; i++) {
queue<long> q;
q.push(U[i] - 1);
vector<bitset<50>> P(N);
while (q.size()) {
long f = q.front();
q.pop();
for (auto n : A[f])
if (P[n.first].none() && n.first != U[i] - 1) {
P[n.first] = P[f];
P[n.first].set(n.second);
q.push(n.first);
}
}
B[i] = P[V[i] - 1];
}
long ans = 0;
for (long i = 0; i < (1 << M); i++) {
bitset<50> b(i), c;
for (long j = 0; j < M; j++)
if (b.test(j))
c |= B[j];
ans += (1 - ((long)b.count() % 2) * 2) * (1 << (N - 1 - c.count()));
}
cout << ans;
assert(ans >= 0);
} | #include <bits/stdc++.h>
using namespace std;
int main() {
long N;
cin >> N;
vector<vector<pair<long, long>>> A(N);
for (long i = 0; i < N - 1; i++) {
long a, b;
cin >> a >> b;
A[a - 1].push_back(pair<long, long>(b - 1, i));
A[b - 1].push_back(pair<long, long>(a - 1, i));
}
long M;
cin >> M;
vector<long> U(M), V(M);
for (long i = 0; i < M; i++)
cin >> U[i] >> V[i];
vector<bitset<50>> B(M);
for (long i = 0; i < M; i++) {
queue<long> q;
q.push(U[i] - 1);
vector<bitset<50>> P(N);
while (q.size()) {
long f = q.front();
q.pop();
for (auto n : A[f])
if (P[n.first].none() && n.first != U[i] - 1) {
P[n.first] = P[f];
P[n.first].set(n.second);
q.push(n.first);
}
}
B[i] = P[V[i] - 1];
}
long ans = 0;
for (long i = 0; i < (1 << M); i++) {
bitset<50> b(i), c;
for (long j = 0; j < M; j++)
if (b.test(j))
c |= B[j];
ans += (1L - ((long)b.count() % 2L) * 2L) * (1L << (N - 1L - c.count()));
assert(1L << (N - 1L - c.count()) > 0);
}
cout << ans;
assert(ans >= 0);
} | replace | 41 | 42 | 41 | 43 | 0 | |
p02794 | C++ | Runtime Error | #include <bits/stdc++.h>
#define watch(x) cout << (#x) << " is " << (x) << endl
typedef long long ll;
using namespace std;
int static fast = []() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
return 0;
}();
int main() {
int n, m, a, b, src, dst;
cin >> n;
unordered_map<int, vector<int>> e;
map<pair<int, int>, int> mm;
for (int i = 0; i < n - 1; i++) {
cin >> a >> b;
e[a].push_back(b);
e[b].push_back(a);
if (a > b)
swap(a, b);
mm[{a, b}] = i;
}
vector<ll> rules;
cin >> m;
for (int i = 0; i < m; i++) {
cin >> src >> dst;
vector<int> parent(n, 0);
vector<bool> visit(n, false);
visit[src] = true;
parent[src] = src;
vector<int> dq = {src};
while (!dq.empty()) {
vector<int> new_dq;
for (int node : dq) {
for (int neibor : e[node]) {
if (visit[neibor] == false) {
visit[neibor] = true;
parent[neibor] = node;
new_dq.push_back(neibor);
}
}
}
new_dq.swap(dq);
}
int cur = dst;
ll num = 0;
while (cur != src) {
a = cur;
b = parent[cur];
if (a > b)
swap(a, b);
num = num | (1LL << mm[{a, b}]);
cur = parent[cur];
}
rules.push_back(num);
}
// inclusive - exclusive
ll ans = 0;
for (int state = 0; state < (1 << m); state++) {
ll x = state, bit_cnt = 0;
while (x > 0) {
x -= x & (-x);
bit_cnt += 1;
}
ll mask = 0;
for (int j = 0; j < m; j++) {
if (state >> j & 1) {
mask = mask | rules[j];
}
}
x = mask;
ll bit_cnt2 = 0;
while (x > 0) {
x -= x & (-x);
bit_cnt2 += 1;
}
ll comb = 1LL << (n - 1 - bit_cnt2);
if (bit_cnt % 2 == 1)
ans -= comb;
else
ans += comb;
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
#define watch(x) cout << (#x) << " is " << (x) << endl
typedef long long ll;
using namespace std;
int static fast = []() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
return 0;
}();
int main() {
int n, m, a, b, src, dst;
cin >> n;
unordered_map<int, vector<int>> e;
map<pair<int, int>, int> mm;
for (int i = 0; i < n - 1; i++) {
cin >> a >> b;
e[a].push_back(b);
e[b].push_back(a);
if (a > b)
swap(a, b);
mm[{a, b}] = i;
}
vector<ll> rules;
cin >> m;
for (int i = 0; i < m; i++) {
cin >> src >> dst;
vector<int> parent(n + 1, 0);
vector<bool> visit(n + 1, false);
visit[src] = true;
parent[src] = src;
vector<int> dq = {src};
while (!dq.empty()) {
vector<int> new_dq;
for (int node : dq) {
for (int neibor : e[node]) {
if (visit[neibor] == false) {
visit[neibor] = true;
parent[neibor] = node;
new_dq.push_back(neibor);
}
}
}
new_dq.swap(dq);
}
int cur = dst;
ll num = 0;
while (cur != src) {
a = cur;
b = parent[cur];
if (a > b)
swap(a, b);
num = num | (1LL << mm[{a, b}]);
cur = parent[cur];
}
rules.push_back(num);
}
// inclusive - exclusive
ll ans = 0;
for (int state = 0; state < (1 << m); state++) {
ll x = state, bit_cnt = 0;
while (x > 0) {
x -= x & (-x);
bit_cnt += 1;
}
ll mask = 0;
for (int j = 0; j < m; j++) {
if (state >> j & 1) {
mask = mask | rules[j];
}
}
x = mask;
ll bit_cnt2 = 0;
while (x > 0) {
x -= x & (-x);
bit_cnt2 += 1;
}
ll comb = 1LL << (n - 1 - bit_cnt2);
if (bit_cnt % 2 == 1)
ans -= comb;
else
ans += comb;
}
cout << ans << endl;
return 0;
}
| replace | 28 | 30 | 28 | 30 | 0 | |
p02794 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
struct Edge {
int to, id;
Edge(int to, int id) : to(to), id(id) {}
};
vector<Edge> g[55];
vector<int> es;
bool dfs(int v, int tv, int p = -1) {
if (v == tv)
return true;
for (Edge e : g[v]) {
if (e.to == p)
continue;
if (dfs(e.to, tv, v)) {
es.push_back(e.id);
return true;
}
}
return false;
}
int main() {
int n, m;
cin >> n >> m;
rep(i, n - 1) {
int a, b;
cin >> a >> b;
a--, b--;
g[a].emplace_back(b, i);
g[b].emplace_back(a, i);
}
cin >> m;
vector<ll> eset(m);
rep(i, m) {
int a, b;
cin >> a >> b;
a--, b--;
es = vector<int>();
dfs(a, b);
for (int e : es) {
eset[i] |= 1ll << e;
}
}
ll ans = 0;
rep(i, 1 << m) {
ll mask = 0;
rep(j, m) {
if (i >> j & 1)
mask |= eset[j];
}
int white = __builtin_popcountll(mask);
ll now = 1ll << (n - 1 - white);
if (__builtin_popcountll(i) % 2 == 0)
ans += now;
else
ans -= now;
}
cout << ans << endl;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
struct Edge {
int to, id;
Edge(int to, int id) : to(to), id(id) {}
};
vector<Edge> g[55];
vector<int> es;
bool dfs(int v, int tv, int p = -1) {
if (v == tv)
return true;
for (Edge e : g[v]) {
if (e.to == p)
continue;
if (dfs(e.to, tv, v)) {
es.push_back(e.id);
return true;
}
}
return false;
}
int main() {
int n, m;
cin >> n;
rep(i, n - 1) {
int a, b;
cin >> a >> b;
a--, b--;
g[a].emplace_back(b, i);
g[b].emplace_back(a, i);
}
cin >> m;
vector<ll> eset(m);
rep(i, m) {
int a, b;
cin >> a >> b;
a--, b--;
es = vector<int>();
dfs(a, b);
for (int e : es) {
eset[i] |= 1ll << e;
}
}
ll ans = 0;
rep(i, 1 << m) {
ll mask = 0;
rep(j, m) {
if (i >> j & 1)
mask |= eset[j];
}
int white = __builtin_popcountll(mask);
ll now = 1ll << (n - 1 - white);
if (__builtin_popcountll(i) % 2 == 0)
ans += now;
else
ans -= now;
}
cout << ans << endl;
} | replace | 30 | 31 | 30 | 31 | 0 | |
p02794 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 50;
const int BIT = (1 << 20);
int n, m, d[N];
vector<int> g[N];
ll bit[BIT];
vector<int> v;
void dfs_pre(int fa, int u, int src, int dest, int idx) {
v.push_back(u);
if (u == dest) {
for (int i = 1; i < v.size(); i++) {
int t = v[i];
if (d[v[i]] < d[v[i - 1]])
t = v[i - 1];
bit[1 << idx] |= (1LL << t);
}
}
for (int v : g[u]) {
if (v == fa)
continue;
if (dest == -1)
d[v] = d[u] + 1;
dfs_pre(u, v, src, dest, idx);
}
v.pop_back();
}
int get_count(ll x) {
int t = 0;
while (x) {
if (x & 1)
t++;
x /= 2;
}
return t;
}
int main() {
cin >> n;
for (int i = 1, u, v; i < n; i++) {
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
cin >> m;
dfs_pre(-1, 1, 1, -1, 0);
for (int i = 0, u, v; i < m; i++) {
cin >> u >> v;
dfs_pre(-1, u, u, v, i);
}
ll ans = (1LL << (n - 1));
for (int i = 1; i < (1 << m); i++) {
bit[i] = bit[i & -i] | bit[i - (i & -i)];
int c = get_count(bit[i]);
ll res = (1LL << (n - 1 - c)) * ((get_count(i) & 1) ? 1 : -1);
ans -= res;
}
cout << ans << '\n';
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 52;
const int BIT = (1 << 20);
int n, m, d[N];
vector<int> g[N];
ll bit[BIT];
vector<int> v;
void dfs_pre(int fa, int u, int src, int dest, int idx) {
v.push_back(u);
if (u == dest) {
for (int i = 1; i < v.size(); i++) {
int t = v[i];
if (d[v[i]] < d[v[i - 1]])
t = v[i - 1];
bit[1 << idx] |= (1LL << t);
}
}
for (int v : g[u]) {
if (v == fa)
continue;
if (dest == -1)
d[v] = d[u] + 1;
dfs_pre(u, v, src, dest, idx);
}
v.pop_back();
}
int get_count(ll x) {
int t = 0;
while (x) {
if (x & 1)
t++;
x /= 2;
}
return t;
}
int main() {
cin >> n;
for (int i = 1, u, v; i < n; i++) {
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
cin >> m;
dfs_pre(-1, 1, 1, -1, 0);
for (int i = 0, u, v; i < m; i++) {
cin >> u >> v;
dfs_pre(-1, u, u, v, i);
}
ll ans = (1LL << (n - 1));
for (int i = 1; i < (1 << m); i++) {
bit[i] = bit[i & -i] | bit[i - (i & -i)];
int c = get_count(bit[i]);
ll res = (1LL << (n - 1 - c)) * ((get_count(i) & 1) ? 1 : -1);
ans -= res;
}
cout << ans << '\n';
return 0;
} | replace | 3 | 4 | 3 | 4 | 0 | |
p02794 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<int, int> Pi;
typedef vector<ll> Vec;
typedef vector<int> Vi;
typedef vector<string> Vs;
typedef vector<P> VP;
typedef vector<vector<ll>> VV;
typedef vector<vector<int>> VVi;
#define REP(i, a, b) for (ll i = (a); i < (b); i++)
#define PER(i, a, b) for (ll i = (a); i >= (b); i--)
#define rep(i, n) REP(i, 0, n)
#define per(i, n) PER(i, n, 0)
const ll INF = 1e18 + 18;
const ll MAX = 100005;
const ll MOD = 1000000007;
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl;
#define YES(n) cout << ((n) ? "YES" : "NO") << endl;
#define ALL(v) v.begin(), v.end()
#define rALL(v) v.rbegin(), v.rend()
#define pb(x) push_back(x)
#define mp(a, b) make_pair(a, b)
#define Each(a, b) for (auto &a : b)
#define REPM(i, mp) for (auto i = mp.begin(); i != mp.end(); ++i)
#define dbg(x_) cerr << #x_ << ":" << x_ << endl;
#define dbgmap(mp) \
cerr << #mp << ":" << endl; \
for (auto i = mp.begin(); i != mp.end(); ++i) { \
cerr << i->first << ":" << i->second << endl; \
}
#define dbgarr(n, m, arr) \
rep(i, n) { \
rep(j, m) { cerr << arr[i][j] << " "; } \
cerr << endl; \
}
#define dbgdp(n, arr) \
rep(i, n) { cerr << arr[i] << " "; } \
cerr << endl;
#define dbgmint(n, arr) \
rep(i, n) { cerr << arr[i].x << " "; } \
cerr << endl;
#define Uniq(v) v.erase(unique(v.begin(), v.end()), v.end())
#define fi first
#define se second
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &p) {
return s << "(" << p.first << ", " << p.second << ")";
}
template <typename T> istream &operator>>(istream &i, vector<T> &v) {
rep(j, v.size()) i >> v[j];
return i;
}
// vector
template <typename T> ostream &operator<<(ostream &s, const vector<T> &v) {
int len = v.size();
for (int i = 0; i < len; ++i) {
s << v[i];
if (i < len - 1)
s << " ";
}
return s;
}
// 2 dimentional vector
template <typename T>
ostream &operator<<(ostream &s, const vector<vector<T>> &vv) {
int len = vv.size();
for (int i = 0; i < len; ++i) {
s << vv[i] << endl;
}
return s;
}
// LCA
template <typename G> struct DoublingLowestCommonAncestor {
const int LOG;
vector<int> dep;
const G &g;
vector<vector<int>> table;
DoublingLowestCommonAncestor(const G &g)
: g(g), dep(g.size()), LOG(32 - __builtin_clz(g.size())) {
table.assign(LOG, vector<int>(g.size(), -1));
}
void dfs(int idx, int par, int d) {
table[0][idx] = par;
dep[idx] = d;
for (auto &to : g[idx]) {
if (to != par)
dfs(to, idx, d + 1);
}
}
void build(int root) {
dfs(root, -1, 0);
for (int k = 0; k + 1 < LOG; k++) {
for (int i = 0; i < table[k].size(); i++) {
if (table[k][i] == -1)
table[k + 1][i] = -1;
else
table[k + 1][i] = table[k][table[k][i]];
}
}
}
int query(int u, int v) {
if (dep[u] > dep[v])
swap(u, v);
for (int i = LOG - 1; i >= 0; i--) {
if (((dep[v] - dep[u]) >> i) & 1)
v = table[i][v];
}
if (u == v)
return u;
for (int i = LOG - 1; i >= 0; i--) {
if (table[i][u] != table[i][v]) {
u = table[i][u];
v = table[i][v];
}
}
return table[0][u];
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << std::setprecision(10);
ll n;
cin >> n;
VV g(n);
rep(i, n - 1) {
ll a, b;
cin >> a >> b;
a--;
b--;
g[a].pb(b);
g[b].pb(a);
}
// dbg(g);
vector<ll> dist(n);
auto dfs = [&](auto dfs, ll v, ll parent) -> void {
Each(x, g[v]) {
if (x == parent)
continue;
dist[x] = dist[v] + 1;
dfs(dfs, x, v);
}
};
ll m;
cin >> m;
Vec u(m);
Vec v(m);
rep(i, m) {
ll a, b;
cin >> a >> b;
a--;
b--;
u[i] = a;
v[i] = b;
}
DoublingLowestCommonAncestor<VV> LCA(g);
LCA.build(0);
ll ans = pow(2, n - 1);
Vec lca(m);
rep(i, m) { lca[i] = LCA.query(u[i], v[i]); }
REP(bit, 1, (1LL << m)) {
ll tmp = 0;
Vec add(n);
rep(i, m) {
if (bit & (1LL << i)) {
add[lca[i]] -= 2;
add[u[i]]++;
add[v[i]]++;
}
}
dbg(add) auto dfs = [&](auto dfs, ll v, ll parent) -> ll {
ll res = 0;
Each(x, g[v]) {
if (x == parent)
continue;
res += dfs(dfs, x, v);
add[v] += add[x];
}
return res + (add[v] > 0);
};
tmp = dfs(dfs, 0, -1);
ll sum = pow(2, n - 1 - tmp);
if (__builtin_popcount(bit) % 2 == 1) {
sum *= -1;
}
// dbg(sum);
ans += sum;
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<int, int> Pi;
typedef vector<ll> Vec;
typedef vector<int> Vi;
typedef vector<string> Vs;
typedef vector<P> VP;
typedef vector<vector<ll>> VV;
typedef vector<vector<int>> VVi;
#define REP(i, a, b) for (ll i = (a); i < (b); i++)
#define PER(i, a, b) for (ll i = (a); i >= (b); i--)
#define rep(i, n) REP(i, 0, n)
#define per(i, n) PER(i, n, 0)
const ll INF = 1e18 + 18;
const ll MAX = 100005;
const ll MOD = 1000000007;
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl;
#define YES(n) cout << ((n) ? "YES" : "NO") << endl;
#define ALL(v) v.begin(), v.end()
#define rALL(v) v.rbegin(), v.rend()
#define pb(x) push_back(x)
#define mp(a, b) make_pair(a, b)
#define Each(a, b) for (auto &a : b)
#define REPM(i, mp) for (auto i = mp.begin(); i != mp.end(); ++i)
#define dbg(x_) cerr << #x_ << ":" << x_ << endl;
#define dbgmap(mp) \
cerr << #mp << ":" << endl; \
for (auto i = mp.begin(); i != mp.end(); ++i) { \
cerr << i->first << ":" << i->second << endl; \
}
#define dbgarr(n, m, arr) \
rep(i, n) { \
rep(j, m) { cerr << arr[i][j] << " "; } \
cerr << endl; \
}
#define dbgdp(n, arr) \
rep(i, n) { cerr << arr[i] << " "; } \
cerr << endl;
#define dbgmint(n, arr) \
rep(i, n) { cerr << arr[i].x << " "; } \
cerr << endl;
#define Uniq(v) v.erase(unique(v.begin(), v.end()), v.end())
#define fi first
#define se second
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &s, const pair<T1, T2> &p) {
return s << "(" << p.first << ", " << p.second << ")";
}
template <typename T> istream &operator>>(istream &i, vector<T> &v) {
rep(j, v.size()) i >> v[j];
return i;
}
// vector
template <typename T> ostream &operator<<(ostream &s, const vector<T> &v) {
int len = v.size();
for (int i = 0; i < len; ++i) {
s << v[i];
if (i < len - 1)
s << " ";
}
return s;
}
// 2 dimentional vector
template <typename T>
ostream &operator<<(ostream &s, const vector<vector<T>> &vv) {
int len = vv.size();
for (int i = 0; i < len; ++i) {
s << vv[i] << endl;
}
return s;
}
// LCA
template <typename G> struct DoublingLowestCommonAncestor {
const int LOG;
vector<int> dep;
const G &g;
vector<vector<int>> table;
DoublingLowestCommonAncestor(const G &g)
: g(g), dep(g.size()), LOG(32 - __builtin_clz(g.size())) {
table.assign(LOG, vector<int>(g.size(), -1));
}
void dfs(int idx, int par, int d) {
table[0][idx] = par;
dep[idx] = d;
for (auto &to : g[idx]) {
if (to != par)
dfs(to, idx, d + 1);
}
}
void build(int root) {
dfs(root, -1, 0);
for (int k = 0; k + 1 < LOG; k++) {
for (int i = 0; i < table[k].size(); i++) {
if (table[k][i] == -1)
table[k + 1][i] = -1;
else
table[k + 1][i] = table[k][table[k][i]];
}
}
}
int query(int u, int v) {
if (dep[u] > dep[v])
swap(u, v);
for (int i = LOG - 1; i >= 0; i--) {
if (((dep[v] - dep[u]) >> i) & 1)
v = table[i][v];
}
if (u == v)
return u;
for (int i = LOG - 1; i >= 0; i--) {
if (table[i][u] != table[i][v]) {
u = table[i][u];
v = table[i][v];
}
}
return table[0][u];
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << std::setprecision(10);
ll n;
cin >> n;
VV g(n);
rep(i, n - 1) {
ll a, b;
cin >> a >> b;
a--;
b--;
g[a].pb(b);
g[b].pb(a);
}
// dbg(g);
vector<ll> dist(n);
auto dfs = [&](auto dfs, ll v, ll parent) -> void {
Each(x, g[v]) {
if (x == parent)
continue;
dist[x] = dist[v] + 1;
dfs(dfs, x, v);
}
};
ll m;
cin >> m;
Vec u(m);
Vec v(m);
rep(i, m) {
ll a, b;
cin >> a >> b;
a--;
b--;
u[i] = a;
v[i] = b;
}
DoublingLowestCommonAncestor<VV> LCA(g);
LCA.build(0);
ll ans = pow(2, n - 1);
Vec lca(m);
rep(i, m) { lca[i] = LCA.query(u[i], v[i]); }
REP(bit, 1, (1LL << m)) {
ll tmp = 0;
Vec add(n);
rep(i, m) {
if (bit & (1LL << i)) {
add[lca[i]] -= 2;
add[u[i]]++;
add[v[i]]++;
}
}
// dbg(add)
auto dfs = [&](auto dfs, ll v, ll parent) -> ll {
ll res = 0;
Each(x, g[v]) {
if (x == parent)
continue;
res += dfs(dfs, x, v);
add[v] += add[x];
}
return res + (add[v] > 0);
};
tmp = dfs(dfs, 0, -1);
ll sum = pow(2, n - 1 - tmp);
if (__builtin_popcount(bit) % 2 == 1) {
sum *= -1;
}
// dbg(sum);
ans += sum;
}
cout << ans << endl;
return 0;
}
| replace | 202 | 203 | 202 | 204 | TLE | |
p02794 | C++ | Runtime Error | #include <bits/stdc++.h>
#define R register
#define ll long long
#define sum(a, b, mod) (((a) + (b)) % mod)
const int MaxN = 5e5 + 10;
const ll mod = 1ll << 62;
struct edge {
int next, to;
};
edge e[MaxN << 1];
int n, m, cnt;
int head[MaxN], f[MaxN], u[MaxN], v[MaxN];
std::bitset<510> q[25], t[1 << 16];
ll fast_pow(ll a, ll b) {
ll res = 1;
while (b) {
if (b & 1)
res = (res * a) % mod;
a = (a * a) % mod, b >>= 1;
}
return res;
}
void add_edge(int u, int v) {
++cnt;
e[cnt].to = v;
e[cnt].next = head[u];
head[u] = cnt;
}
void dfs(int u, int fa) {
f[u] = fa;
for (int i = head[u]; i; i = e[i].next) {
int v = e[i].to;
if (v == fa)
continue;
dfs(v, u);
}
}
void make(int x, int y, int p) {
for (int i = x; i != 1; i = f[i])
q[p].set(i, 1);
int cur = y;
for (; cur != 1 && !q[p][cur]; cur = f[cur])
q[p].set(cur, 1);
for (; cur != 1; cur = f[cur])
q[p].set(cur, 0);
}
inline int read() {
int x = 0;
char ch = getchar();
while (ch > '9' || ch < '0')
ch = getchar();
while (ch <= '9' && ch >= '0')
x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
return x;
}
int main() {
ll ans = 0;
n = read();
for (int i = 1; i <= n - 1; i++) {
int u = read(), v = read();
add_edge(u, v), add_edge(v, u);
}
dfs(1, 0), m = read();
for (int i = 1; i <= m; i++) {
u[i] = read(), v[i] = read();
make(u[i], v[i], i);
}
for (int i = 0; i < (1 << m); i++) {
int cnt = 0, cur = 0;
for (int j = 0; j < m; j++)
if (i & (1 << j))
cnt++, cur = j + 1;
if (cnt)
t[i] = t[i ^ (1 << (cur - 1))] | q[cur];
if (cnt & 1)
ans = sum(ans, mod - fast_pow(2, n - 1 - t[i].count()), mod);
else
ans = sum(ans, fast_pow(2, n - 1 - t[i].count()), mod);
}
printf("%lld", ans);
return 0;
} | #include <bits/stdc++.h>
#define R register
#define ll long long
#define sum(a, b, mod) (((a) + (b)) % mod)
const int MaxN = 5e5 + 10;
const ll mod = 1ll << 62;
struct edge {
int next, to;
};
edge e[MaxN << 1];
int n, m, cnt;
int head[MaxN], f[MaxN], u[MaxN], v[MaxN];
std::bitset<510> q[25], t[1 << 20];
ll fast_pow(ll a, ll b) {
ll res = 1;
while (b) {
if (b & 1)
res = (res * a) % mod;
a = (a * a) % mod, b >>= 1;
}
return res;
}
void add_edge(int u, int v) {
++cnt;
e[cnt].to = v;
e[cnt].next = head[u];
head[u] = cnt;
}
void dfs(int u, int fa) {
f[u] = fa;
for (int i = head[u]; i; i = e[i].next) {
int v = e[i].to;
if (v == fa)
continue;
dfs(v, u);
}
}
void make(int x, int y, int p) {
for (int i = x; i != 1; i = f[i])
q[p].set(i, 1);
int cur = y;
for (; cur != 1 && !q[p][cur]; cur = f[cur])
q[p].set(cur, 1);
for (; cur != 1; cur = f[cur])
q[p].set(cur, 0);
}
inline int read() {
int x = 0;
char ch = getchar();
while (ch > '9' || ch < '0')
ch = getchar();
while (ch <= '9' && ch >= '0')
x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
return x;
}
int main() {
ll ans = 0;
n = read();
for (int i = 1; i <= n - 1; i++) {
int u = read(), v = read();
add_edge(u, v), add_edge(v, u);
}
dfs(1, 0), m = read();
for (int i = 1; i <= m; i++) {
u[i] = read(), v[i] = read();
make(u[i], v[i], i);
}
for (int i = 0; i < (1 << m); i++) {
int cnt = 0, cur = 0;
for (int j = 0; j < m; j++)
if (i & (1 << j))
cnt++, cur = j + 1;
if (cnt)
t[i] = t[i ^ (1 << (cur - 1))] | q[cur];
if (cnt & 1)
ans = sum(ans, mod - fast_pow(2, n - 1 - t[i].count()), mod);
else
ans = sum(ans, fast_pow(2, n - 1 - t[i].count()), mod);
}
printf("%lld", ans);
return 0;
} | replace | 16 | 17 | 16 | 17 | 0 | |
p02794 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define INF 1000000001
using ll = long long;
using Graph = vector<vector<int>>;
vector<pair<int, int>> ab;
map<pair<int, int>, int> m;
bool dfs(Graph &G, int v, int p, int g, ll &c) {
for (auto nv : G.at(v)) {
if (nv == p) {
continue;
}
if (nv == g) {
ll k = m[make_pair(min(v, nv), max(v, nv))];
c = 1ull << k;
return true;
}
if (dfs(G, nv, v, g, c)) {
ll k = m[make_pair(min(v, nv), max(v, nv))];
c += (1ull << k);
return true;
}
}
return false;
}
int main() {
int N;
cin >> N;
Graph G(N);
ab.resize(N - 1);
for (int i = 0; i < N - 1; i++) {
int x, y;
cin >> x >> y;
x--;
y--;
ab.at(i) = make_pair(min(x, y), max(x, y));
m[ab.at(i)] = i;
G.at(x).push_back(y);
G.at(y).push_back(x);
}
int M;
cin >> M;
vector<int> u(M), v(M);
for (int i = 0; i < M; i++) {
cin >> u.at(i) >> v.at(i);
u.at(i)--;
v.at(i)--;
}
vector<ll> constrain(M);
for (int i = 0; i < M; i++) {
dfs(G, u.at(i), -1, v.at(i), constrain.at(i));
}
ll ans = 1ull << ((ll)N - 1);
for (int j = 1; j < 1 << M; j++) {
ll bit = 0;
ll count = 0;
for (int i = 0; i < N; i++) {
if (((j >> i) & 1) == 1) {
count++;
bit |= constrain.at(i);
}
}
ll C = 0;
for (int i = 0; i < 64; i++) {
C += bit % 2;
bit >>= 1;
}
if (count % 2 == 0) {
ans += 1ull << (N - 1 - C);
} else {
ans -= 1ull << (N - 1 - C);
}
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define INF 1000000001
using ll = long long;
using Graph = vector<vector<int>>;
vector<pair<int, int>> ab;
map<pair<int, int>, int> m;
bool dfs(Graph &G, int v, int p, int g, ll &c) {
for (auto nv : G.at(v)) {
if (nv == p) {
continue;
}
if (nv == g) {
ll k = m[make_pair(min(v, nv), max(v, nv))];
c = 1ull << k;
return true;
}
if (dfs(G, nv, v, g, c)) {
ll k = m[make_pair(min(v, nv), max(v, nv))];
c += (1ull << k);
return true;
}
}
return false;
}
int main() {
int N;
cin >> N;
Graph G(N);
ab.resize(N - 1);
for (int i = 0; i < N - 1; i++) {
int x, y;
cin >> x >> y;
x--;
y--;
ab.at(i) = make_pair(min(x, y), max(x, y));
m[ab.at(i)] = i;
G.at(x).push_back(y);
G.at(y).push_back(x);
}
int M;
cin >> M;
vector<int> u(M), v(M);
for (int i = 0; i < M; i++) {
cin >> u.at(i) >> v.at(i);
u.at(i)--;
v.at(i)--;
}
vector<ll> constrain(M);
for (int i = 0; i < M; i++) {
dfs(G, u.at(i), -1, v.at(i), constrain.at(i));
}
ll ans = 1ull << ((ll)N - 1);
for (int j = 1; j < 1 << M; j++) {
ll bit = 0;
ll count = 0;
for (int i = 0; i < M; i++) {
if (((j >> i) & 1) == 1) {
count++;
bit |= constrain.at(i);
}
}
ll C = 0;
for (int i = 0; i < 64; i++) {
C += bit % 2;
bit >>= 1;
}
if (count % 2 == 0) {
ans += 1ull << (N - 1 - C);
} else {
ans -= 1ull << (N - 1 - C);
}
}
cout << ans << endl;
}
| replace | 61 | 62 | 61 | 62 | 0 | |
p02794 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef unsigned long ul;
typedef unsigned long long ull;
typedef long long ll;
typedef vector<ll> vint;
typedef vector<vector<ll>> vvint;
typedef vector<vector<vector<ll>>> vvvint;
typedef vector<string> vstring;
typedef vector<vector<string>> vvstring;
typedef vector<char> vchar;
typedef vector<vector<char>> vvchar;
typedef vector<long double> vdouble;
typedef vector<vector<long double>> vvdouble;
typedef vector<vector<vector<long double>>> vvvdouble;
typedef pair<ll, ll> pint;
typedef vector<pint> vpint;
typedef vector<bool> vbool;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repf(i, f, n) for (ll i = f; i < n; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define ALL(obj) (obj).begin(), (obj).end()
// #define LLONG_MAX 9223372036854775806
#define vmax(vec) *max_element(vec.begin(), vec.end())
#define vmin(vec) *min_element(vec.begin(), vec.end())
#define vsort(vec) sort(vec.begin(), vec.end())
#define vsortgr(vec) sort(vec.begin(), vec.end(), greater<ll>())
#define MOD 1000000007
// #define MOD 998244353
// #define MOD LLONG_MAX
const double PI = 3.14159265358979323846;
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;
}
int dy[] = {0, 0, 1, -1};
int dx[] = {
1,
-1,
0,
0,
};
template <typename T> struct edge {
int src, to;
T cost;
edge(int to, T cost) : src(-1), to(to), cost(cost) {}
edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
edge &operator=(const int &x) {
to = x;
return *this;
}
operator int() const { return to; }
};
template <typename T> using Edges = vector<edge<T>>;
template <typename T> using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
template <typename T> using Matrix = vector<vector<T>>;
ll gcd(ll a, ll b) {
if (a < b) {
a ^= b;
b ^= a;
a ^= b;
}
return b ? gcd(b, a % b) : a;
}
ll lcm(int a, ll b) { return a * b / gcd(a, b); }
// 繰り返し二乗法
ll power(ll a, ll b) {
if (a == 1)
return a;
// if(a==0)re
ll res = 1;
while (b > 0) {
if (b & 1)
res = res * a % MOD;
a = a * a % MOD;
b >>= 1;
}
return res;
}
const int MAX = 1000000;
ll fact[MAX], fact_inv[MAX];
void init_fact(ll n) {
fact[0] = 1;
// 階乗の計算
rep(i, n) fact[i + 1] = fact[i] * (i + 1) % MOD;
fact_inv[n] = power(fact[n], MOD - 2);
// 逆元の計算
for (ll i = n - 1; i >= 0; i--)
fact_inv[i] = fact_inv[i + 1] * (i + 1) % MOD;
}
ll comb(ll n, ll r) {
return (fact[n] * fact_inv[r]) % MOD * fact_inv[n - r] % MOD;
}
ll perm(ll n, ll r) { return (fact[n] * fact_inv[n - r]) % MOD; }
struct UnionFind {
vector<ll> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2
vector<ll> num;
// vector<ll> dist; // rootまでの距離
UnionFind(ll N) : par(N), num(N) { // 最初は全てが根であるとして初期化
for (ll i = 0; i < N; i++)
par[i] = i;
for (ll i = 0; i < N; i++)
num[i] = 1;
// for(ll i = 0; i < N; i++) dist[i] = 0;
}
ll root(ll x) { // データxが属する木の根を再帰で得る:root(x) = {xの木の根}
if (par[x] == x)
return x;
return par[x] = root(par[x]);
}
void unite(ll x, ll y) { // xとyの木を併合
ll rx = root(x); // xの根をrx
ll ry = root(y); // yの根をry
if (rx == ry)
return; // xとyの根が同じ(=同じ木にある)時はそのまま
par[rx] =
ry; // xとyの根が同じでない(=同じ木にない)時:xの根rxをyの根ryにつける
num[ry] = num[rx] + num[ry];
}
bool same(ll x, ll y) { // 2つのデータx, yが属する木が同じならtrueを返す
ll rx = root(x);
ll ry = root(y);
return rx == ry;
}
ll size(ll x) { return num[root(x)]; }
};
vint divisor(ll n) { // nの約数
vint ret;
for (ll i = 1; i * i <= n; i++) {
if (n % i == 0) {
ret.pb(i);
if (i * i != n)
ret.pb(n / i);
}
}
vsort(ret);
return ret;
}
ll get_digit_in(ll n, ll i) { // i桁目の数字を得る。
for (ll j = 0; j < i - 1; j++) {
n /= 10;
}
return n % 10;
}
ll get_digit(ll n) {
ll rtn = 0;
while (n > 0) {
n /= 10;
rtn++;
}
return rtn;
}
map<ll, ll> prime_factor(ll n) {
map<ll, ll> rtn;
for (ll i = 2; i * i <= n; i++) {
while (n % i == 0) {
rtn[i]++;
n /= i;
}
}
if (n != 1)
rtn[n] = 1;
return rtn;
}
// nPnの順列に対して処理を実行する
void foreach_permutation(ll n, function<void(ll *)> f) {
ll indexes[n];
for (ll i = 0; i < n; i++)
indexes[i] = i;
do {
f(indexes);
} while (std::next_permutation(indexes, indexes + n));
}
void recursive_comb(ll *indexes, ll s, ll rest, function<void(ll *)> f) {
if (rest == 0) {
f(indexes);
} else {
if (s < 0)
return;
recursive_comb(indexes, s - 1, rest, f);
indexes[rest - 1] = s;
recursive_comb(indexes, s - 1, rest - 1, f);
}
}
// nCkの組み合わせに対して処理を実行する
void foreach_comb(ll n, ll k, function<void(ll *)> f) {
ll indexes[k];
recursive_comb(indexes, n - 1, k, f);
}
// nPkの順列に対して処理を実行する
void foreach_permutation(ll n, ll k, function<void(ll *)> f) {
foreach_comb(n, k, [&](ll *c_indexes) {
foreach_permutation(k, [&](ll *p_indexes) {
ll indexes[k];
for (ll i = 0; i < k; i++) {
indexes[i] = c_indexes[p_indexes[i]];
}
f(indexes);
});
});
}
void printv(vint &V) {
for (auto e : V)
cout << e << " ";
cout << endl;
}
// map< int64_t, int > prime_factor(int64_t n) {
// map< int64_t, int > ret;
// for(int64_t i = 2; i * i <= n; i++) {
// while(n % i == 0) {
// ret[i]++;
// n /= i;
// }
// }
// if(n != 1) ret[n] = 1;
// return ret;
// }
template <int mod> struct ModInt {
int x;
ModInt() : x(0) {}
ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
ModInt &operator+=(const ModInt &p) {
if ((x += p.x) >= mod)
x -= mod;
return *this;
}
ModInt &operator-=(const ModInt &p) {
if ((x += mod - p.x) >= mod)
x -= mod;
return *this;
}
ModInt &operator*=(const ModInt &p) {
x = (int)(1LL * x * p.x % mod);
return *this;
}
ModInt &operator/=(const ModInt &p) {
*this *= p.inverse();
return *this;
}
ModInt operator-() const { return ModInt(-x); }
ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
bool operator==(const ModInt &p) const { return x == p.x; }
bool operator!=(const ModInt &p) const { return x != p.x; }
ModInt inverse() const {
int a = x, b = mod, u = 1, v = 0, t;
while (b > 0) {
t = a / b;
swap(a -= t * b, b);
swap(u -= t * v, v);
}
return ModInt(u);
}
ModInt pow(int64_t n) const {
ModInt ret(1), mul(x);
while (n > 0) {
if (n & 1)
ret *= mul;
mul *= mul;
n >>= 1;
}
return ret;
}
friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }
friend istream &operator>>(istream &is, ModInt &a) {
int64_t t;
is >> t;
a = ModInt<mod>(t);
return (is);
}
static int get_mod() { return mod; }
};
using modint = ModInt<(int)(1e9 + 7)>;
template <typename T> T mod_pow(T x, T n, const T &p) {
T ret = 1;
while (n > 0) {
if (n & 1)
(ret *= x) %= p;
(x *= x) %= p;
n >>= 1;
}
return ret;
}
vvint edges;
bool bfs(ll now, ll p, ll s, ll g, ll i, vvint &mask) {
if (now == g)
return true;
for (auto nxt : edges[now]) {
// cout<<
if (nxt == p)
continue;
if (bfs(nxt, now, s, g, i, mask)) {
// puts("hey");
// cout<<now<<":"<<nxt<<endl;
mask[now][nxt] += (1 << i);
mask[nxt][now] += (1 << i);
return true;
}
}
return false;
};
int main() {
cout << fixed << setprecision(10);
ll N;
cin >> N;
vint a(N - 1), b(N - 1);
rep(i, N - 1) {
cin >> a[i] >> b[i];
a[i]--;
b[i]--;
}
ll M;
cin >> M;
vint u(M), v(M);
rep(i, M) {
cin >> u[i] >> v[i];
u[i]--;
v[i]--;
}
edges.resize(N);
vvint mask(N, vint(N, 0));
// map<pint, ll> m;
rep(i, N - 1) {
edges[a[i]].push_back(b[i]);
edges[b[i]].push_back(a[i]);
// m[mp(a[i], b[i])] = i;
// m[mp(b[i], a[i])] = i;
}
rep(i, M) {
ll s, g;
s = u[i];
g = v[i];
bfs(s, -1, s, g, i, mask);
}
// rep(i,N){
// rep(j,N){
// cout<<mask[i][j]<<" ";
// }
// cout<<endl;
// }
ll dp[N][(1 << M)];
rep(i, N) {
// rep(j,2){
rep(k, (1 << M)) dp[i][k] = 0;
// }
}
return 0;
dp[0][0] = 1;
rep(i, N - 1) {
// cout<<mask[a[i]][b[i]]<<endl;
rep(j, (1 << M)) {
ll tmp = j | mask[a[i]][b[i]];
dp[i + 1][tmp] += dp[i][j]; // 黒
dp[i + 1][j] += dp[i][j]; // white
}
}
cout << dp[N - 1][(1 << M) - 1] << endl;
}
//
| #include <bits/stdc++.h>
using namespace std;
typedef unsigned long ul;
typedef unsigned long long ull;
typedef long long ll;
typedef vector<ll> vint;
typedef vector<vector<ll>> vvint;
typedef vector<vector<vector<ll>>> vvvint;
typedef vector<string> vstring;
typedef vector<vector<string>> vvstring;
typedef vector<char> vchar;
typedef vector<vector<char>> vvchar;
typedef vector<long double> vdouble;
typedef vector<vector<long double>> vvdouble;
typedef vector<vector<vector<long double>>> vvvdouble;
typedef pair<ll, ll> pint;
typedef vector<pint> vpint;
typedef vector<bool> vbool;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repf(i, f, n) for (ll i = f; i < n; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define ALL(obj) (obj).begin(), (obj).end()
// #define LLONG_MAX 9223372036854775806
#define vmax(vec) *max_element(vec.begin(), vec.end())
#define vmin(vec) *min_element(vec.begin(), vec.end())
#define vsort(vec) sort(vec.begin(), vec.end())
#define vsortgr(vec) sort(vec.begin(), vec.end(), greater<ll>())
#define MOD 1000000007
// #define MOD 998244353
// #define MOD LLONG_MAX
const double PI = 3.14159265358979323846;
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;
}
int dy[] = {0, 0, 1, -1};
int dx[] = {
1,
-1,
0,
0,
};
template <typename T> struct edge {
int src, to;
T cost;
edge(int to, T cost) : src(-1), to(to), cost(cost) {}
edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
edge &operator=(const int &x) {
to = x;
return *this;
}
operator int() const { return to; }
};
template <typename T> using Edges = vector<edge<T>>;
template <typename T> using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
template <typename T> using Matrix = vector<vector<T>>;
ll gcd(ll a, ll b) {
if (a < b) {
a ^= b;
b ^= a;
a ^= b;
}
return b ? gcd(b, a % b) : a;
}
ll lcm(int a, ll b) { return a * b / gcd(a, b); }
// 繰り返し二乗法
ll power(ll a, ll b) {
if (a == 1)
return a;
// if(a==0)re
ll res = 1;
while (b > 0) {
if (b & 1)
res = res * a % MOD;
a = a * a % MOD;
b >>= 1;
}
return res;
}
const int MAX = 1000000;
ll fact[MAX], fact_inv[MAX];
void init_fact(ll n) {
fact[0] = 1;
// 階乗の計算
rep(i, n) fact[i + 1] = fact[i] * (i + 1) % MOD;
fact_inv[n] = power(fact[n], MOD - 2);
// 逆元の計算
for (ll i = n - 1; i >= 0; i--)
fact_inv[i] = fact_inv[i + 1] * (i + 1) % MOD;
}
ll comb(ll n, ll r) {
return (fact[n] * fact_inv[r]) % MOD * fact_inv[n - r] % MOD;
}
ll perm(ll n, ll r) { return (fact[n] * fact_inv[n - r]) % MOD; }
struct UnionFind {
vector<ll> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2
vector<ll> num;
// vector<ll> dist; // rootまでの距離
UnionFind(ll N) : par(N), num(N) { // 最初は全てが根であるとして初期化
for (ll i = 0; i < N; i++)
par[i] = i;
for (ll i = 0; i < N; i++)
num[i] = 1;
// for(ll i = 0; i < N; i++) dist[i] = 0;
}
ll root(ll x) { // データxが属する木の根を再帰で得る:root(x) = {xの木の根}
if (par[x] == x)
return x;
return par[x] = root(par[x]);
}
void unite(ll x, ll y) { // xとyの木を併合
ll rx = root(x); // xの根をrx
ll ry = root(y); // yの根をry
if (rx == ry)
return; // xとyの根が同じ(=同じ木にある)時はそのまま
par[rx] =
ry; // xとyの根が同じでない(=同じ木にない)時:xの根rxをyの根ryにつける
num[ry] = num[rx] + num[ry];
}
bool same(ll x, ll y) { // 2つのデータx, yが属する木が同じならtrueを返す
ll rx = root(x);
ll ry = root(y);
return rx == ry;
}
ll size(ll x) { return num[root(x)]; }
};
vint divisor(ll n) { // nの約数
vint ret;
for (ll i = 1; i * i <= n; i++) {
if (n % i == 0) {
ret.pb(i);
if (i * i != n)
ret.pb(n / i);
}
}
vsort(ret);
return ret;
}
ll get_digit_in(ll n, ll i) { // i桁目の数字を得る。
for (ll j = 0; j < i - 1; j++) {
n /= 10;
}
return n % 10;
}
ll get_digit(ll n) {
ll rtn = 0;
while (n > 0) {
n /= 10;
rtn++;
}
return rtn;
}
map<ll, ll> prime_factor(ll n) {
map<ll, ll> rtn;
for (ll i = 2; i * i <= n; i++) {
while (n % i == 0) {
rtn[i]++;
n /= i;
}
}
if (n != 1)
rtn[n] = 1;
return rtn;
}
// nPnの順列に対して処理を実行する
void foreach_permutation(ll n, function<void(ll *)> f) {
ll indexes[n];
for (ll i = 0; i < n; i++)
indexes[i] = i;
do {
f(indexes);
} while (std::next_permutation(indexes, indexes + n));
}
void recursive_comb(ll *indexes, ll s, ll rest, function<void(ll *)> f) {
if (rest == 0) {
f(indexes);
} else {
if (s < 0)
return;
recursive_comb(indexes, s - 1, rest, f);
indexes[rest - 1] = s;
recursive_comb(indexes, s - 1, rest - 1, f);
}
}
// nCkの組み合わせに対して処理を実行する
void foreach_comb(ll n, ll k, function<void(ll *)> f) {
ll indexes[k];
recursive_comb(indexes, n - 1, k, f);
}
// nPkの順列に対して処理を実行する
void foreach_permutation(ll n, ll k, function<void(ll *)> f) {
foreach_comb(n, k, [&](ll *c_indexes) {
foreach_permutation(k, [&](ll *p_indexes) {
ll indexes[k];
for (ll i = 0; i < k; i++) {
indexes[i] = c_indexes[p_indexes[i]];
}
f(indexes);
});
});
}
void printv(vint &V) {
for (auto e : V)
cout << e << " ";
cout << endl;
}
// map< int64_t, int > prime_factor(int64_t n) {
// map< int64_t, int > ret;
// for(int64_t i = 2; i * i <= n; i++) {
// while(n % i == 0) {
// ret[i]++;
// n /= i;
// }
// }
// if(n != 1) ret[n] = 1;
// return ret;
// }
template <int mod> struct ModInt {
int x;
ModInt() : x(0) {}
ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
ModInt &operator+=(const ModInt &p) {
if ((x += p.x) >= mod)
x -= mod;
return *this;
}
ModInt &operator-=(const ModInt &p) {
if ((x += mod - p.x) >= mod)
x -= mod;
return *this;
}
ModInt &operator*=(const ModInt &p) {
x = (int)(1LL * x * p.x % mod);
return *this;
}
ModInt &operator/=(const ModInt &p) {
*this *= p.inverse();
return *this;
}
ModInt operator-() const { return ModInt(-x); }
ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
bool operator==(const ModInt &p) const { return x == p.x; }
bool operator!=(const ModInt &p) const { return x != p.x; }
ModInt inverse() const {
int a = x, b = mod, u = 1, v = 0, t;
while (b > 0) {
t = a / b;
swap(a -= t * b, b);
swap(u -= t * v, v);
}
return ModInt(u);
}
ModInt pow(int64_t n) const {
ModInt ret(1), mul(x);
while (n > 0) {
if (n & 1)
ret *= mul;
mul *= mul;
n >>= 1;
}
return ret;
}
friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }
friend istream &operator>>(istream &is, ModInt &a) {
int64_t t;
is >> t;
a = ModInt<mod>(t);
return (is);
}
static int get_mod() { return mod; }
};
using modint = ModInt<(int)(1e9 + 7)>;
template <typename T> T mod_pow(T x, T n, const T &p) {
T ret = 1;
while (n > 0) {
if (n & 1)
(ret *= x) %= p;
(x *= x) %= p;
n >>= 1;
}
return ret;
}
vvint edges;
bool bfs(ll now, ll p, ll s, ll g, ll i, vvint &mask) {
if (now == g)
return true;
for (auto nxt : edges[now]) {
// cout<<
if (nxt == p)
continue;
if (bfs(nxt, now, s, g, i, mask)) {
// puts("hey");
// cout<<now<<":"<<nxt<<endl;
mask[now][nxt] += (1 << i);
mask[nxt][now] += (1 << i);
return true;
}
}
return false;
};
int main() {
cout << fixed << setprecision(10);
ll N;
cin >> N;
vint a(N - 1), b(N - 1);
rep(i, N - 1) {
cin >> a[i] >> b[i];
a[i]--;
b[i]--;
}
ll M;
cin >> M;
vint u(M), v(M);
rep(i, M) {
cin >> u[i] >> v[i];
u[i]--;
v[i]--;
}
edges.resize(N);
vvint mask(N, vint(N, 0));
// map<pint, ll> m;
rep(i, N - 1) {
edges[a[i]].push_back(b[i]);
edges[b[i]].push_back(a[i]);
// m[mp(a[i], b[i])] = i;
// m[mp(b[i], a[i])] = i;
}
rep(i, M) {
ll s, g;
s = u[i];
g = v[i];
bfs(s, -1, s, g, i, mask);
}
// rep(i,N){
// rep(j,N){
// cout<<mask[i][j]<<" ";
// }
// cout<<endl;
// }
// dp[N][(1<<M)];
vvint dp(N, vint((1 << M), 0));
// rep(i,N){
// rep(k,(1<<M)) dp[i][k] = 0;
// }
dp[0][0] = 1;
rep(i, N - 1) {
// cout<<mask[a[i]][b[i]]<<endl;
rep(j, (1 << M)) {
ll tmp = j | mask[a[i]][b[i]];
dp[i + 1][tmp] += dp[i][j]; // 黒
dp[i + 1][j] += dp[i][j]; // white
}
}
cout << dp[N - 1][(1 << M) - 1] << endl;
}
//
| replace | 425 | 432 | 425 | 430 | 0 | |
p02794 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define REP(i, m, n) for (int i = (m); i < (int)(n); i++)
#define RREP(i, m, n) for (int i = (int)(n - 1); i >= m; i--)
#define rep(i, n) REP(i, 0, n)
#define rrep(i, n) RREP(i, 0, n)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define aut(r, v) __typeof(v) r = (v)
#define each(it, o) for (aut(it, (o).begin()); it != (o).end(); ++it)
#define reach(it, o) for (aut(it, (o).rbegin()); it != (o).rend(); ++it)
#define fi first
#define se second
#define debug(...) \
{ \
cerr << "[L" << __LINE__ << "] "; \
_debug(__VA_ARGS__); \
}
template <typename T1, typename T2>
ostream &operator<<(ostream &o, const pair<T1, T2> &p) {
return o << "(" << p.first << ", " << p.second << ")";
}
template <typename T> string join(const vector<T> &v, string del = ", ") {
stringstream s;
rep(i, v.size()) s << del << v[i];
return s.str().substr(del.size());
}
template <typename T> ostream &operator<<(ostream &o, const vector<T> &v) {
if (v.size())
o << "[" << join(v) << "]";
return o;
}
template <typename T>
ostream &operator<<(ostream &o, const vector<vector<T>> &vv) {
int l = vv.size();
if (l) {
o << endl;
rep(i, l) {
o << (i == 0 ? "[ " : ",\n ") << vv[i] << (i == l - 1 ? " ]" : "");
}
}
return o;
}
template <typename T> ostream &operator<<(ostream &o, const set<T> &st) {
vector<T> v(st.begin(), st.end());
o << "{ " << join(v) << " }";
return o;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &o, const map<T1, T2> &m) {
each(p, m) {
o << (p == m.begin() ? "{ " : ",\n ") << *p
<< (p == --m.end() ? " }" : "");
}
return o;
}
inline void _debug() { cerr << endl; }
template <class First, class... Rest>
void _debug(const First &first, const Rest &...rest) {
cerr << first << " ";
_debug(rest...);
}
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
const double PI = (1 * acos(0.0));
const double EPS = 1e-9;
const ll INF = 0x3f3f3f3f;
const ll INFL = 0x3f3f3f3f3f3f3f3fLL;
const ll mod = 1e9 + 7;
inline void finput(string filename) { freopen(filename.c_str(), "r", stdin); }
struct Edge {
int to, id;
Edge(int to, int id) : to(to), id(id) {}
};
int N, M;
vector<Edge> G[100];
vector<int> es;
bool dfs(int v, int tv, int p = -1) {
if (v == tv) {
es = vector<int>();
return true;
}
for (auto e : G[v]) {
if (e.to == p)
continue;
if (dfs(e.to, tv, v)) {
es.push_back(e.id);
return true;
}
}
return false;
}
int main() {
ios_base::sync_with_stdio(0);
finput("./input");
cin >> N;
rep(i, N - 1) {
int a, b;
cin >> a >> b;
a--;
b--;
G[a].emplace_back(b, i);
G[b].emplace_back(a, i);
}
cin >> M;
vector<ll> eset(M);
rep(i, M) {
int u, v;
cin >> u >> v;
u--;
v--;
dfs(u, v);
for (auto e : es) {
eset[i] |= 1ll << e;
}
}
ll ans = 0;
rep(i, 1 << M) {
ll mask = 0;
rep(j, M) {
if ((i >> j) & 1)
mask |= eset[j];
}
ll k = __builtin_popcountll(mask);
ll x = 1ll << (N - 1 - k);
if (__builtin_popcountll(i) & 1)
ans -= x;
else
ans += x;
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define REP(i, m, n) for (int i = (m); i < (int)(n); i++)
#define RREP(i, m, n) for (int i = (int)(n - 1); i >= m; i--)
#define rep(i, n) REP(i, 0, n)
#define rrep(i, n) RREP(i, 0, n)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define aut(r, v) __typeof(v) r = (v)
#define each(it, o) for (aut(it, (o).begin()); it != (o).end(); ++it)
#define reach(it, o) for (aut(it, (o).rbegin()); it != (o).rend(); ++it)
#define fi first
#define se second
#define debug(...) \
{ \
cerr << "[L" << __LINE__ << "] "; \
_debug(__VA_ARGS__); \
}
template <typename T1, typename T2>
ostream &operator<<(ostream &o, const pair<T1, T2> &p) {
return o << "(" << p.first << ", " << p.second << ")";
}
template <typename T> string join(const vector<T> &v, string del = ", ") {
stringstream s;
rep(i, v.size()) s << del << v[i];
return s.str().substr(del.size());
}
template <typename T> ostream &operator<<(ostream &o, const vector<T> &v) {
if (v.size())
o << "[" << join(v) << "]";
return o;
}
template <typename T>
ostream &operator<<(ostream &o, const vector<vector<T>> &vv) {
int l = vv.size();
if (l) {
o << endl;
rep(i, l) {
o << (i == 0 ? "[ " : ",\n ") << vv[i] << (i == l - 1 ? " ]" : "");
}
}
return o;
}
template <typename T> ostream &operator<<(ostream &o, const set<T> &st) {
vector<T> v(st.begin(), st.end());
o << "{ " << join(v) << " }";
return o;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &o, const map<T1, T2> &m) {
each(p, m) {
o << (p == m.begin() ? "{ " : ",\n ") << *p
<< (p == --m.end() ? " }" : "");
}
return o;
}
inline void _debug() { cerr << endl; }
template <class First, class... Rest>
void _debug(const First &first, const Rest &...rest) {
cerr << first << " ";
_debug(rest...);
}
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
const double PI = (1 * acos(0.0));
const double EPS = 1e-9;
const ll INF = 0x3f3f3f3f;
const ll INFL = 0x3f3f3f3f3f3f3f3fLL;
const ll mod = 1e9 + 7;
inline void finput(string filename) { freopen(filename.c_str(), "r", stdin); }
struct Edge {
int to, id;
Edge(int to, int id) : to(to), id(id) {}
};
int N, M;
vector<Edge> G[100];
vector<int> es;
bool dfs(int v, int tv, int p = -1) {
if (v == tv) {
es = vector<int>();
return true;
}
for (auto e : G[v]) {
if (e.to == p)
continue;
if (dfs(e.to, tv, v)) {
es.push_back(e.id);
return true;
}
}
return false;
}
int main() {
ios_base::sync_with_stdio(0);
// finput("./input");
cin >> N;
rep(i, N - 1) {
int a, b;
cin >> a >> b;
a--;
b--;
G[a].emplace_back(b, i);
G[b].emplace_back(a, i);
}
cin >> M;
vector<ll> eset(M);
rep(i, M) {
int u, v;
cin >> u >> v;
u--;
v--;
dfs(u, v);
for (auto e : es) {
eset[i] |= 1ll << e;
}
}
ll ans = 0;
rep(i, 1 << M) {
ll mask = 0;
rep(j, M) {
if ((i >> j) & 1)
mask |= eset[j];
}
ll k = __builtin_popcountll(mask);
ll x = 1ll << (N - 1 - k);
if (__builtin_popcountll(i) & 1)
ans -= x;
else
ans += x;
}
cout << ans << endl;
return 0;
} | replace | 107 | 108 | 107 | 108 | 0 | |
p02794 | C++ | Runtime Error | // Link : https://atcoder.jp/contests/abc152/tasks/abc152_f
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 51
#define M 8
vector<int> edge[N];
int a[N], b[N];
int anc[N];
int parEdge[N], depth[N];
void dfs(int node, int par) {
for (int i = 0; i < edge[node].size(); ++i) {
int eid = edge[node][i];
int ch = a[eid] ^ b[eid] ^ node;
if (ch == par) {
continue;
}
anc[ch] = node;
depth[ch] = depth[node] + 1;
parEdge[ch] = eid;
dfs(ch, node);
}
}
#define EDGE 20
int x[EDGE], y[EDGE];
bool isColored[EDGE];
void paint(int u, int v) {
if (depth[u] < depth[v]) {
swap(u, v);
}
int diff = depth[u] - depth[v];
for (int i = 0; i < diff; ++i) {
isColored[parEdge[u]] = true;
u = anc[u];
}
while (u != v) {
isColored[parEdge[u]] = true;
isColored[parEdge[v]] = true;
u = anc[u];
v = anc[v];
}
}
ll color(int bit, int n, int m) {
ll ret = 1;
memset(isColored, 0, sizeof(isColored));
for (int i = 0; i < m; ++i) {
if (bit & (1 << i)) {
paint(x[i], y[i]);
}
}
for (int i = 0; i < n - 1; ++i) {
if (!isColored[i]) {
ret = ret * 2;
}
}
return ret;
}
int bitCount(int x) {
int ret = 0;
while (x > 0) {
++ret;
x = x & (x - 1);
}
return ret;
}
void solve() {
int n;
scanf("%d ", &n);
ll ret = 1;
for (int i = 0; i < n - 1; ++i) {
scanf("%d %d ", &a[i], &b[i]);
edge[a[i]].push_back(i);
edge[b[i]].push_back(i);
ret *= 2;
}
dfs(1, 0);
int m;
scanf("%d ", &m);
for (int i = 0; i < m; ++i) {
scanf("%d %d ", &x[i], &y[i]);
paint(x[i], y[i]);
}
for (int i = 1; i < (1 << m); ++i) {
int bc = bitCount(i);
if (bc & 1) {
ret -= color(i, n, m);
} else {
ret += color(i, n, m);
}
}
printf("%lld\n", ret);
}
int main() {
// freopen("input.txt","r",stdin);
solve();
return 0;
}
| // Link : https://atcoder.jp/contests/abc152/tasks/abc152_f
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 51
#define M 8
vector<int> edge[N];
int a[N], b[N];
int anc[N];
int parEdge[N], depth[N];
void dfs(int node, int par) {
for (int i = 0; i < edge[node].size(); ++i) {
int eid = edge[node][i];
int ch = a[eid] ^ b[eid] ^ node;
if (ch == par) {
continue;
}
anc[ch] = node;
depth[ch] = depth[node] + 1;
parEdge[ch] = eid;
dfs(ch, node);
}
}
#define EDGE 20
int x[EDGE], y[EDGE];
bool isColored[N];
void paint(int u, int v) {
if (depth[u] < depth[v]) {
swap(u, v);
}
int diff = depth[u] - depth[v];
for (int i = 0; i < diff; ++i) {
isColored[parEdge[u]] = true;
u = anc[u];
}
while (u != v) {
isColored[parEdge[u]] = true;
isColored[parEdge[v]] = true;
u = anc[u];
v = anc[v];
}
}
ll color(int bit, int n, int m) {
ll ret = 1;
memset(isColored, 0, sizeof(isColored));
for (int i = 0; i < m; ++i) {
if (bit & (1 << i)) {
paint(x[i], y[i]);
}
}
for (int i = 0; i < n - 1; ++i) {
if (!isColored[i]) {
ret = ret * 2;
}
}
return ret;
}
int bitCount(int x) {
int ret = 0;
while (x > 0) {
++ret;
x = x & (x - 1);
}
return ret;
}
void solve() {
int n;
scanf("%d ", &n);
ll ret = 1;
for (int i = 0; i < n - 1; ++i) {
scanf("%d %d ", &a[i], &b[i]);
edge[a[i]].push_back(i);
edge[b[i]].push_back(i);
ret *= 2;
}
dfs(1, 0);
int m;
scanf("%d ", &m);
for (int i = 0; i < m; ++i) {
scanf("%d %d ", &x[i], &y[i]);
paint(x[i], y[i]);
}
for (int i = 1; i < (1 << m); ++i) {
int bc = bitCount(i);
if (bc & 1) {
ret -= color(i, n, m);
} else {
ret += color(i, n, m);
}
}
printf("%lld\n", ret);
}
int main() {
// freopen("input.txt","r",stdin);
solve();
return 0;
}
| replace | 30 | 31 | 30 | 31 | 0 | |
p02794 | C++ | Runtime Error | #include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
using namespace std;
typedef long long ll;
typedef double db;
typedef long double ldb;
const int inf = 1e9 + 10;
const int maxn = 55;
ll dp[maxn];
int xx[maxn], n, m;
vector<pair<int, int>> g[maxn];
bool dfs(int x, int fa, int ed, int id) {
if (x == ed)
return true;
for (int i = 0; i < g[x].size(); i++) {
int to = g[x][i].fst;
if (to != fa)
if (dfs(to, x, ed, id)) {
xx[g[x][i].snd] |= 1 << id;
return true;
}
}
return false;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n - 1; i++) {
int u, v;
scanf("%d%d", &u, &v);
u--;
v--;
g[u].pb(mp(v, i));
g[v].pb(mp(u, i));
}
scanf("%d", &m);
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d%d", &u, &v);
u--;
v--;
dfs(u, -1, v, i);
}
dp[0] = 1ll;
for (int i = 0; i < n - 1; i++)
for (int mask = (1 << m) - 1; mask >= 0; mask--)
dp[mask | xx[i]] += dp[mask];
printf("%lld\n", dp[(1 << m) - 1]);
return 0;
}
| #include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
using namespace std;
typedef long long ll;
typedef double db;
typedef long double ldb;
const int inf = 1e9 + 10;
const int maxn = 55;
ll dp[1 << 21];
int xx[maxn], n, m;
vector<pair<int, int>> g[maxn];
bool dfs(int x, int fa, int ed, int id) {
if (x == ed)
return true;
for (int i = 0; i < g[x].size(); i++) {
int to = g[x][i].fst;
if (to != fa)
if (dfs(to, x, ed, id)) {
xx[g[x][i].snd] |= 1 << id;
return true;
}
}
return false;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n - 1; i++) {
int u, v;
scanf("%d%d", &u, &v);
u--;
v--;
g[u].pb(mp(v, i));
g[v].pb(mp(u, i));
}
scanf("%d", &m);
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d%d", &u, &v);
u--;
v--;
dfs(u, -1, v, i);
}
dp[0] = 1ll;
for (int i = 0; i < n - 1; i++)
for (int mask = (1 << m) - 1; mask >= 0; mask--)
dp[mask | xx[i]] += dp[mask];
printf("%lld\n", dp[(1 << m) - 1]);
return 0;
}
| replace | 14 | 15 | 14 | 15 | 0 | |
p02794 | C++ | Runtime Error | #include <cstdio>
#include <cstring>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int, int> ii;
int n, m;
vector<ii> al[100];
ii path[100];
bool onpath[100][100];
int depth[100];
int parent[100];
int edge[100];
void dfs(int i, int p) {
for (auto &it : al[i]) {
if (it.first == p)
continue;
depth[it.first] = depth[i] + 1;
parent[it.first] = i;
edge[it.first] = it.second;
dfs(it.first, i);
}
}
void __path(int i, int j, int k) {
if (depth[i] < depth[j])
swap(i, j);
while (depth[i] != depth[j]) {
onpath[k][edge[i]] = true;
i = parent[i];
}
while (i != j) {
onpath[k][edge[i]] = true;
i = parent[i];
onpath[k][edge[j]] = true;
j = parent[j];
}
}
long long memo[2][150000];
int main() {
cin >> n;
int a, b;
for (int x = 0; x < n - 1; x++) {
cin >> a >> b;
al[a].push_back(ii(b, x));
al[b].push_back(ii(a, x));
}
cin >> m;
for (int x = 0; x < m; x++) {
cin >> path[x].first >> path[x].second;
}
dfs(1, -1);
for (int x = 0; x < m; x++) {
__path(path[x].first, path[x].second, x);
}
memo[0][0] = 1;
a = 0, b = 1;
int temp;
for (int x = 0; x < n - 1; x++) {
memset(memo[b], 0, sizeof(memo[b]));
for (int bm = 0; bm < (1 << m); bm++) {
memo[b][bm] += memo[a][bm];
temp = bm;
for (int y = 0; y < m; y++) {
if (onpath[y][x])
temp |= (1 << y);
}
memo[b][temp] += memo[a][bm];
}
swap(a, b);
}
printf("%lld\n", memo[a][(1 << m) - 1]);
} | #include <cstdio>
#include <cstring>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int, int> ii;
int n, m;
vector<ii> al[100];
ii path[100];
bool onpath[100][100];
int depth[100];
int parent[100];
int edge[100];
void dfs(int i, int p) {
for (auto &it : al[i]) {
if (it.first == p)
continue;
depth[it.first] = depth[i] + 1;
parent[it.first] = i;
edge[it.first] = it.second;
dfs(it.first, i);
}
}
void __path(int i, int j, int k) {
if (depth[i] < depth[j])
swap(i, j);
while (depth[i] != depth[j]) {
onpath[k][edge[i]] = true;
i = parent[i];
}
while (i != j) {
onpath[k][edge[i]] = true;
i = parent[i];
onpath[k][edge[j]] = true;
j = parent[j];
}
}
long long memo[2][1500000];
int main() {
cin >> n;
int a, b;
for (int x = 0; x < n - 1; x++) {
cin >> a >> b;
al[a].push_back(ii(b, x));
al[b].push_back(ii(a, x));
}
cin >> m;
for (int x = 0; x < m; x++) {
cin >> path[x].first >> path[x].second;
}
dfs(1, -1);
for (int x = 0; x < m; x++) {
__path(path[x].first, path[x].second, x);
}
memo[0][0] = 1;
a = 0, b = 1;
int temp;
for (int x = 0; x < n - 1; x++) {
memset(memo[b], 0, sizeof(memo[b]));
for (int bm = 0; bm < (1 << m); bm++) {
memo[b][bm] += memo[a][bm];
temp = bm;
for (int y = 0; y < m; y++) {
if (onpath[y][x])
temp |= (1 << y);
}
memo[b][temp] += memo[a][bm];
}
swap(a, b);
}
printf("%lld\n", memo[a][(1 << m) - 1]);
} | replace | 46 | 47 | 46 | 47 | 0 | |
p02794 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 55, M = 21;
int n;
vector<pair<int, int>> g[N];
int m;
int pt[M];
bool prec(int st, int en, int id, int par = -1) {
if (st == en)
return true;
for (auto p : g[st])
if (p.first != par) {
if (prec(p.first, en, id, st)) {
pt[p.second] |= 1 << id;
return true;
}
}
return false;
}
ll dp[N][1 << M];
ll f(int i, int mask) {
if (i == n - 1)
return mask == (1 << m) - 1;
ll &ans = dp[i][mask];
if (~ans)
return ans;
return ans = f(i + 1, mask) + f(i + 1, mask | pt[i]);
}
int main() {
memset(dp, -1, sizeof dp);
scanf("%d", &n);
for (int i = 0; i < n - 1; i++) {
int u, v;
scanf("%d %d", &u, &v);
g[u].emplace_back(v, i);
g[v].emplace_back(u, i);
}
scanf("%d", &m);
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d %d", &u, &v);
assert(prec(u, v, i));
}
printf("%lld\n", f(0, 0));
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 55, M = 21;
int n;
vector<pair<int, int>> g[N];
int m;
int pt[N];
bool prec(int st, int en, int id, int par = -1) {
if (st == en)
return true;
for (auto p : g[st])
if (p.first != par) {
if (prec(p.first, en, id, st)) {
pt[p.second] |= 1 << id;
return true;
}
}
return false;
}
ll dp[N][1 << M];
ll f(int i, int mask) {
if (i == n - 1)
return mask == (1 << m) - 1;
ll &ans = dp[i][mask];
if (~ans)
return ans;
return ans = f(i + 1, mask) + f(i + 1, mask | pt[i]);
}
int main() {
memset(dp, -1, sizeof dp);
scanf("%d", &n);
for (int i = 0; i < n - 1; i++) {
int u, v;
scanf("%d %d", &u, &v);
g[u].emplace_back(v, i);
g[v].emplace_back(u, i);
}
scanf("%d", &m);
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d %d", &u, &v);
assert(prec(u, v, i));
}
printf("%lld\n", f(0, 0));
}
| replace | 10 | 11 | 10 | 11 | -11 | |
p02794 | C++ | Runtime Error | #include <bits/stdc++.h>
int ri() {
int n;
scanf("%d", &n);
return n;
}
#define MOD 1000000007
template <int mod> struct ModInt {
int x;
ModInt() : x(0) {}
ModInt(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
ModInt &operator+=(const ModInt &p) {
if ((x += p.x) >= mod)
x -= mod;
return *this;
}
ModInt &operator-=(const ModInt &p) {
if ((x += mod - p.x) >= mod)
x -= mod;
return *this;
}
ModInt &operator*=(const ModInt &p) {
x = (int)(1LL * x * p.x % mod);
return *this;
}
ModInt &operator/=(const ModInt &p) {
*this *= p.inverse();
return *this;
}
ModInt &operator^=(long long p) {
ModInt res = 1;
for (; p; p >>= 1) {
if (p & 1)
res *= *this;
*this *= *this;
}
return *this = res;
}
ModInt operator-() const { return ModInt(-x); }
ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
ModInt operator^(long long p) const { return ModInt(*this) ^= p; }
bool operator==(const ModInt &p) const { return x == p.x; }
bool operator!=(const ModInt &p) const { return x != p.x; }
explicit operator int() const { return x; } // added by QCFium
ModInt operator=(const int p) {
x = p;
return ModInt(*this);
} // added by QCFium
ModInt inverse() const {
int a = x, b = mod, u = 1, v = 0, t;
while (b > 0) {
t = a / b;
a -= t * b;
std::swap(a, b);
u -= t * v;
std::swap(u, v);
}
return ModInt(u);
}
friend std::ostream &operator<<(std::ostream &os, const ModInt<mod> &p) {
return os << p.x;
}
friend std::istream &operator>>(std::istream &is, ModInt<mod> &a) {
long long x;
is >> x;
a = ModInt<mod>(x);
return (is);
}
};
typedef ModInt<MOD> mint;
struct MComb {
std::vector<mint> fact;
std::vector<mint> inversed;
MComb(int n) { // O(n+log(mod))
fact = std::vector<mint>(n + 1, 1);
for (int i = 1; i <= n; i++)
fact[i] = fact[i - 1] * mint(i);
inversed = std::vector<mint>(n + 1);
inversed[n] = fact[n] ^ (MOD - 2);
for (int i = n - 1; i >= 0; i--)
inversed[i] = inversed[i + 1] * mint(i + 1);
}
mint ncr(int n, int r) { return (fact[n] * inversed[r] * inversed[n - r]); }
mint npr(int n, int r) { return (fact[n] * inversed[n - r]); }
mint nhr(int n, int r) {
assert(n + r - 1 < (int)fact.size());
return ncr(n + r - 1, r);
}
};
int main() {
int n = ri();
std::vector<int> hen[n];
for (int i = 1; i < n; i++) {
int a = ri() - 1;
int b = ri() - 1;
hen[a].push_back(b);
hen[b].push_back(a);
}
int parent[n];
parent[0] = -1;
std::queue<int> que;
que.push(0);
while (que.size()) {
int i = que.front();
que.pop();
for (auto j : hen[i])
if (j != parent[i])
parent[j] = i, que.push(j);
}
int ok[n];
memset(ok, 0, sizeof(ok));
int m = ri();
for (int i = 0; i < m; i++) {
int x = ri() - 1;
int y = ri() - 1;
std::vector<int> r0, r1;
while (x != -1)
r0.push_back(x), x = parent[x];
while (y != -1)
r1.push_back(y), y = parent[y];
while (r0.size() && r1.size() && r0.back() == r1.back())
r0.pop_back(), r1.pop_back();
for (auto j : r0)
ok[j] |= 1 << i;
for (auto j : r1)
ok[j] |= 1 << i;
}
int64_t dp[2][1 << n];
dp[0][0] = 1;
for (int i = 0; i < n; i++) {
memset(dp[(i + 1) & 1], 0, sizeof(dp[(i + 1) & 1]));
for (int j = 0; j < 1 << m; j++) {
// use
if (i)
dp[(i + 1) & 1][j | ok[i]] += dp[i & 1][j];
// no use
dp[(i + 1) & 1][j] += dp[i & 1][j];
}
}
std::cout << dp[n & 1][(1 << m) - 1] << std::endl;
return 0;
}
| #include <bits/stdc++.h>
int ri() {
int n;
scanf("%d", &n);
return n;
}
#define MOD 1000000007
template <int mod> struct ModInt {
int x;
ModInt() : x(0) {}
ModInt(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
ModInt &operator+=(const ModInt &p) {
if ((x += p.x) >= mod)
x -= mod;
return *this;
}
ModInt &operator-=(const ModInt &p) {
if ((x += mod - p.x) >= mod)
x -= mod;
return *this;
}
ModInt &operator*=(const ModInt &p) {
x = (int)(1LL * x * p.x % mod);
return *this;
}
ModInt &operator/=(const ModInt &p) {
*this *= p.inverse();
return *this;
}
ModInt &operator^=(long long p) {
ModInt res = 1;
for (; p; p >>= 1) {
if (p & 1)
res *= *this;
*this *= *this;
}
return *this = res;
}
ModInt operator-() const { return ModInt(-x); }
ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
ModInt operator^(long long p) const { return ModInt(*this) ^= p; }
bool operator==(const ModInt &p) const { return x == p.x; }
bool operator!=(const ModInt &p) const { return x != p.x; }
explicit operator int() const { return x; } // added by QCFium
ModInt operator=(const int p) {
x = p;
return ModInt(*this);
} // added by QCFium
ModInt inverse() const {
int a = x, b = mod, u = 1, v = 0, t;
while (b > 0) {
t = a / b;
a -= t * b;
std::swap(a, b);
u -= t * v;
std::swap(u, v);
}
return ModInt(u);
}
friend std::ostream &operator<<(std::ostream &os, const ModInt<mod> &p) {
return os << p.x;
}
friend std::istream &operator>>(std::istream &is, ModInt<mod> &a) {
long long x;
is >> x;
a = ModInt<mod>(x);
return (is);
}
};
typedef ModInt<MOD> mint;
struct MComb {
std::vector<mint> fact;
std::vector<mint> inversed;
MComb(int n) { // O(n+log(mod))
fact = std::vector<mint>(n + 1, 1);
for (int i = 1; i <= n; i++)
fact[i] = fact[i - 1] * mint(i);
inversed = std::vector<mint>(n + 1);
inversed[n] = fact[n] ^ (MOD - 2);
for (int i = n - 1; i >= 0; i--)
inversed[i] = inversed[i + 1] * mint(i + 1);
}
mint ncr(int n, int r) { return (fact[n] * inversed[r] * inversed[n - r]); }
mint npr(int n, int r) { return (fact[n] * inversed[n - r]); }
mint nhr(int n, int r) {
assert(n + r - 1 < (int)fact.size());
return ncr(n + r - 1, r);
}
};
int main() {
int n = ri();
std::vector<int> hen[n];
for (int i = 1; i < n; i++) {
int a = ri() - 1;
int b = ri() - 1;
hen[a].push_back(b);
hen[b].push_back(a);
}
int parent[n];
parent[0] = -1;
std::queue<int> que;
que.push(0);
while (que.size()) {
int i = que.front();
que.pop();
for (auto j : hen[i])
if (j != parent[i])
parent[j] = i, que.push(j);
}
int ok[n];
memset(ok, 0, sizeof(ok));
int m = ri();
for (int i = 0; i < m; i++) {
int x = ri() - 1;
int y = ri() - 1;
std::vector<int> r0, r1;
while (x != -1)
r0.push_back(x), x = parent[x];
while (y != -1)
r1.push_back(y), y = parent[y];
while (r0.size() && r1.size() && r0.back() == r1.back())
r0.pop_back(), r1.pop_back();
for (auto j : r0)
ok[j] |= 1 << i;
for (auto j : r1)
ok[j] |= 1 << i;
}
int64_t dp[2][1 << m];
memset(dp, 0, sizeof(dp));
dp[0][0] = 1;
for (int i = 0; i < n; i++) {
memset(dp[(i + 1) & 1], 0, sizeof(dp[(i + 1) & 1]));
for (int j = 0; j < 1 << m; j++) {
// use
if (i)
dp[(i + 1) & 1][j | ok[i]] += dp[i & 1][j];
// no use
dp[(i + 1) & 1][j] += dp[i & 1][j];
}
}
std::cout << dp[n & 1][(1 << m) - 1] << std::endl;
return 0;
}
| replace | 135 | 136 | 135 | 137 | 0 | |
p02794 | C++ | Runtime Error | #include <bits/stdc++.h>
int ri() {
int n;
scanf("%d", &n);
return n;
}
#define MOD 1000000007
template <int mod> struct ModInt {
int x;
ModInt() : x(0) {}
ModInt(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
ModInt &operator+=(const ModInt &p) {
if ((x += p.x) >= mod)
x -= mod;
return *this;
}
ModInt &operator-=(const ModInt &p) {
if ((x += mod - p.x) >= mod)
x -= mod;
return *this;
}
ModInt &operator*=(const ModInt &p) {
x = (int)(1LL * x * p.x % mod);
return *this;
}
ModInt &operator/=(const ModInt &p) {
*this *= p.inverse();
return *this;
}
ModInt &operator^=(long long p) {
ModInt res = 1;
for (; p; p >>= 1) {
if (p & 1)
res *= *this;
*this *= *this;
}
return *this = res;
}
ModInt operator-() const { return ModInt(-x); }
ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
ModInt operator^(long long p) const { return ModInt(*this) ^= p; }
bool operator==(const ModInt &p) const { return x == p.x; }
bool operator!=(const ModInt &p) const { return x != p.x; }
explicit operator int() const { return x; } // added by QCFium
ModInt operator=(const int p) {
x = p;
return ModInt(*this);
} // added by QCFium
ModInt inverse() const {
int a = x, b = mod, u = 1, v = 0, t;
while (b > 0) {
t = a / b;
a -= t * b;
std::swap(a, b);
u -= t * v;
std::swap(u, v);
}
return ModInt(u);
}
friend std::ostream &operator<<(std::ostream &os, const ModInt<mod> &p) {
return os << p.x;
}
friend std::istream &operator>>(std::istream &is, ModInt<mod> &a) {
long long x;
is >> x;
a = ModInt<mod>(x);
return (is);
}
};
typedef ModInt<MOD> mint;
struct MComb {
std::vector<mint> fact;
std::vector<mint> inversed;
MComb(int n) { // O(n+log(mod))
fact = std::vector<mint>(n + 1, 1);
for (int i = 1; i <= n; i++)
fact[i] = fact[i - 1] * mint(i);
inversed = std::vector<mint>(n + 1);
inversed[n] = fact[n] ^ (MOD - 2);
for (int i = n - 1; i >= 0; i--)
inversed[i] = inversed[i + 1] * mint(i + 1);
}
mint ncr(int n, int r) { return (fact[n] * inversed[r] * inversed[n - r]); }
mint npr(int n, int r) { return (fact[n] * inversed[n - r]); }
mint nhr(int n, int r) {
assert(n + r - 1 < (int)fact.size());
return ncr(n + r - 1, r);
}
};
int main() {
int n = ri();
std::vector<int> hen[n];
for (int i = 1; i < n; i++) {
int a = ri() - 1;
int b = ri() - 1;
hen[a].push_back(b);
hen[b].push_back(a);
}
int parent[n];
parent[0] = -1;
std::queue<int> que;
que.push(0);
while (que.size()) {
int i = que.front();
que.pop();
for (auto j : hen[i])
if (j != parent[i])
parent[j] = i, que.push(j);
}
int ok[n];
memset(ok, 0, sizeof(ok));
int m = ri();
for (int i = 0; i < m; i++) {
int x = ri() - 1;
int y = ri() - 1;
std::vector<int> r0, r1;
while (x != -1)
r0.push_back(x), x = parent[x];
while (y != -1)
r1.push_back(y), y = parent[y];
while (r0.size() && r1.size() && r0.back() == r1.back())
r0.pop_back(), r1.pop_back();
for (auto j : r0)
ok[j] |= 1 << i;
for (auto j : r1)
ok[j] |= 1 << i;
}
int64_t dp[n + 1][1 << m];
memset(dp, 0, sizeof(dp));
dp[0][0] = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < 1 << m; j++) {
// use
if (i)
dp[i + 1][j | ok[i]] += dp[i][j];
// no use
dp[i + 1][j] += dp[i][j];
}
}
std::cout << dp[n][(1 << m) - 1] << std::endl;
return 0;
}
| #include <bits/stdc++.h>
int ri() {
int n;
scanf("%d", &n);
return n;
}
#define MOD 1000000007
template <int mod> struct ModInt {
int x;
ModInt() : x(0) {}
ModInt(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
ModInt &operator+=(const ModInt &p) {
if ((x += p.x) >= mod)
x -= mod;
return *this;
}
ModInt &operator-=(const ModInt &p) {
if ((x += mod - p.x) >= mod)
x -= mod;
return *this;
}
ModInt &operator*=(const ModInt &p) {
x = (int)(1LL * x * p.x % mod);
return *this;
}
ModInt &operator/=(const ModInt &p) {
*this *= p.inverse();
return *this;
}
ModInt &operator^=(long long p) {
ModInt res = 1;
for (; p; p >>= 1) {
if (p & 1)
res *= *this;
*this *= *this;
}
return *this = res;
}
ModInt operator-() const { return ModInt(-x); }
ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
ModInt operator^(long long p) const { return ModInt(*this) ^= p; }
bool operator==(const ModInt &p) const { return x == p.x; }
bool operator!=(const ModInt &p) const { return x != p.x; }
explicit operator int() const { return x; } // added by QCFium
ModInt operator=(const int p) {
x = p;
return ModInt(*this);
} // added by QCFium
ModInt inverse() const {
int a = x, b = mod, u = 1, v = 0, t;
while (b > 0) {
t = a / b;
a -= t * b;
std::swap(a, b);
u -= t * v;
std::swap(u, v);
}
return ModInt(u);
}
friend std::ostream &operator<<(std::ostream &os, const ModInt<mod> &p) {
return os << p.x;
}
friend std::istream &operator>>(std::istream &is, ModInt<mod> &a) {
long long x;
is >> x;
a = ModInt<mod>(x);
return (is);
}
};
typedef ModInt<MOD> mint;
struct MComb {
std::vector<mint> fact;
std::vector<mint> inversed;
MComb(int n) { // O(n+log(mod))
fact = std::vector<mint>(n + 1, 1);
for (int i = 1; i <= n; i++)
fact[i] = fact[i - 1] * mint(i);
inversed = std::vector<mint>(n + 1);
inversed[n] = fact[n] ^ (MOD - 2);
for (int i = n - 1; i >= 0; i--)
inversed[i] = inversed[i + 1] * mint(i + 1);
}
mint ncr(int n, int r) { return (fact[n] * inversed[r] * inversed[n - r]); }
mint npr(int n, int r) { return (fact[n] * inversed[n - r]); }
mint nhr(int n, int r) {
assert(n + r - 1 < (int)fact.size());
return ncr(n + r - 1, r);
}
};
int main() {
int n = ri();
std::vector<int> hen[n];
for (int i = 1; i < n; i++) {
int a = ri() - 1;
int b = ri() - 1;
hen[a].push_back(b);
hen[b].push_back(a);
}
int parent[n];
parent[0] = -1;
std::queue<int> que;
que.push(0);
while (que.size()) {
int i = que.front();
que.pop();
for (auto j : hen[i])
if (j != parent[i])
parent[j] = i, que.push(j);
}
int ok[n];
memset(ok, 0, sizeof(ok));
int m = ri();
for (int i = 0; i < m; i++) {
int x = ri() - 1;
int y = ri() - 1;
std::vector<int> r0, r1;
while (x != -1)
r0.push_back(x), x = parent[x];
while (y != -1)
r1.push_back(y), y = parent[y];
while (r0.size() && r1.size() && r0.back() == r1.back())
r0.pop_back(), r1.pop_back();
for (auto j : r0)
ok[j] |= 1 << i;
for (auto j : r1)
ok[j] |= 1 << i;
}
std::vector<std::vector<int64_t>> dp(n + 1, std::vector<int64_t>(1 << m));
dp[0][0] = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < 1 << m; j++) {
// use
if (i)
dp[i + 1][j | ok[i]] += dp[i][j];
// no use
dp[i + 1][j] += dp[i][j];
}
}
std::cout << dp[n][(1 << m) - 1] << std::endl;
return 0;
}
| replace | 135 | 137 | 135 | 136 | 0 | |
p02794 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
const int N = 50;
vector<int> edges[N];
int tin[N], tout[N], timer, lca[N][N], p[N], add[N];
int ql[N], qr[N];
int n, m;
void dfs(int v, int _p) {
tin[v] = timer++;
p[v] = _p;
for (int u : edges[v]) {
if (u == _p)
continue;
dfs(u, v);
}
tout[v] = timer++;
}
bool ancestor(int v, int u) { return tin[v] <= tin[u] && tout[u] <= tout[v]; }
int calcLCA(int v, int u) {
if (ancestor(v, u))
return v;
if (ancestor(u, v))
return u;
while (!ancestor(p[v], u))
v = p[v];
return p[v];
}
void prec() {
dfs(0, 0);
for (int v = 1; v <= n; v++) {
for (int u = 1; u <= n; u++) {
lca[v][u] = calcLCA(v, u);
}
}
}
int getcnt;
void dfsForC(int v, int p) {
for (int u : edges[v]) {
if (u == p)
continue;
dfsForC(u, v);
add[v] += add[u];
if (add[u] > 0)
getcnt--;
}
}
int calcMask(int mask) {
memset(add, 0, sizeof add);
for (int i = 0; i < m; i++) {
if (!(mask & (1 << i)))
continue;
int v = ql[i], u = qr[i], w = lca[v][u];
add[v]++, add[u]++, add[w] -= 2;
}
getcnt = n - 1;
dfsForC(0, 0);
return getcnt;
}
int getSign(int mask) {
int cnt = __builtin_popcount(mask);
if (cnt % 2 == 0)
return 1;
else
return -1;
}
void solve() {
cin >> n;
for (int i = 0; i < n - 1; i++) {
int v, u;
cin >> v >> u;
v--, u--;
edges[v].push_back(u);
edges[u].push_back(v);
}
cin >> m;
for (int i = 0; i < m; i++) {
cin >> ql[i] >> qr[i];
ql[i]--, qr[i]--;
}
prec();
long long ans = 0;
for (int mask = 0; mask < (1 << m); mask++) {
int sign = getSign(mask);
ans += 1LL * sign * (1LL << calcMask(mask));
}
cout << ans;
}
int main() {
ios::sync_with_stdio(NULL), cin.tie(0), cout.tie(0);
cout.setf(ios::fixed), cout.precision(20);
solve();
}
| #include <bits/stdc++.h>
using namespace std;
const int N = 50;
vector<int> edges[N];
int tin[N], tout[N], timer, lca[N][N], p[N], add[N];
int ql[N], qr[N];
int n, m;
void dfs(int v, int _p) {
tin[v] = timer++;
p[v] = _p;
for (int u : edges[v]) {
if (u == _p)
continue;
dfs(u, v);
}
tout[v] = timer++;
}
bool ancestor(int v, int u) { return tin[v] <= tin[u] && tout[u] <= tout[v]; }
int calcLCA(int v, int u) {
if (ancestor(v, u))
return v;
if (ancestor(u, v))
return u;
while (!ancestor(p[v], u))
v = p[v];
return p[v];
}
void prec() {
dfs(0, 0);
for (int v = 0; v < n; v++) {
for (int u = 0; u < n; u++) {
lca[v][u] = calcLCA(v, u);
}
}
}
int getcnt;
void dfsForC(int v, int p) {
for (int u : edges[v]) {
if (u == p)
continue;
dfsForC(u, v);
add[v] += add[u];
if (add[u] > 0)
getcnt--;
}
}
int calcMask(int mask) {
memset(add, 0, sizeof add);
for (int i = 0; i < m; i++) {
if (!(mask & (1 << i)))
continue;
int v = ql[i], u = qr[i], w = lca[v][u];
add[v]++, add[u]++, add[w] -= 2;
}
getcnt = n - 1;
dfsForC(0, 0);
return getcnt;
}
int getSign(int mask) {
int cnt = __builtin_popcount(mask);
if (cnt % 2 == 0)
return 1;
else
return -1;
}
void solve() {
cin >> n;
for (int i = 0; i < n - 1; i++) {
int v, u;
cin >> v >> u;
v--, u--;
edges[v].push_back(u);
edges[u].push_back(v);
}
cin >> m;
for (int i = 0; i < m; i++) {
cin >> ql[i] >> qr[i];
ql[i]--, qr[i]--;
}
prec();
long long ans = 0;
for (int mask = 0; mask < (1 << m); mask++) {
int sign = getSign(mask);
ans += 1LL * sign * (1LL << calcMask(mask));
}
cout << ans;
}
int main() {
ios::sync_with_stdio(NULL), cin.tie(0), cout.tie(0);
cout.setf(ios::fixed), cout.precision(20);
solve();
}
| replace | 35 | 37 | 35 | 37 | TLE | |
p02794 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i, s, e) for (i64(i) = (s); (i) < (e); (i)++)
#define rev(i, s, e) for (i64(i) = (s); (i)-- > (e);)
#define all(x) x.begin(), x.end()
template <class T>
static inline std::vector<T> ndvec(size_t &&n, T val) noexcept {
return std::vector<T>(n, std::forward<T>(val));
}
template <class... Tail>
static inline auto ndvec(size_t &&n, Tail &&...tail) noexcept {
return std::vector<decltype(ndvec(std::forward<Tail>(tail)...))>(
n, ndvec(std::forward<Tail>(tail)...));
}
vector<bitset<100>> p;
vector<vector<pair<i64, i64>>> G;
bool dfs(i64 v, i64 f, i64 g, i64 i) {
if (v == g) {
return true;
}
for (auto e : G[v]) {
i64 t = e.first;
i64 ei = e.second;
if (t == f)
continue;
if (dfs(t, v, g, i)) {
p[i] |= (1 << ei);
return true;
}
}
return false;
}
int main() {
i64 N;
cin >> N;
G.resize(N);
rep(i, 0, N - 1) {
i64 a, b;
cin >> a >> b;
a--;
b--;
G[a].push_back({b, i});
G[b].push_back({a, i});
}
i64 M;
cin >> M;
p.resize(M);
vector<i64> U(M), V(M);
rep(i, 0, M) {
cin >> U[i] >> V[i];
U[i]--;
V[i]--;
dfs(U[i], -1, V[i], i);
}
i64 ans = 0;
vector<i64> cnt(N);
for (auto s = 1; s < (1 << M); s++) {
bitset<100> bit;
rep(i, 0, M) {
if (s & (1 << i)) {
bit |= p[i];
}
}
if (__builtin_popcountll(s) & 1)
cnt[N - 1 - bit.count()]++;
else
cnt[N - 1 - bit.count()]--;
}
for (i64 i = 0; i < N; i++) {
ans += (1ll << i) * cnt[i];
}
cout << (1ll << (N - 1)) - ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i, s, e) for (i64(i) = (s); (i) < (e); (i)++)
#define rev(i, s, e) for (i64(i) = (s); (i)-- > (e);)
#define all(x) x.begin(), x.end()
template <class T>
static inline std::vector<T> ndvec(size_t &&n, T val) noexcept {
return std::vector<T>(n, std::forward<T>(val));
}
template <class... Tail>
static inline auto ndvec(size_t &&n, Tail &&...tail) noexcept {
return std::vector<decltype(ndvec(std::forward<Tail>(tail)...))>(
n, ndvec(std::forward<Tail>(tail)...));
}
vector<bitset<100>> p;
vector<vector<pair<i64, i64>>> G;
bool dfs(i64 v, i64 f, i64 g, i64 i) {
if (v == g) {
return true;
}
for (auto e : G[v]) {
i64 t = e.first;
i64 ei = e.second;
if (t == f)
continue;
if (dfs(t, v, g, i)) {
p[i] |= (1ll << ei);
return true;
}
}
return false;
}
int main() {
i64 N;
cin >> N;
G.resize(N);
rep(i, 0, N - 1) {
i64 a, b;
cin >> a >> b;
a--;
b--;
G[a].push_back({b, i});
G[b].push_back({a, i});
}
i64 M;
cin >> M;
p.resize(M);
vector<i64> U(M), V(M);
rep(i, 0, M) {
cin >> U[i] >> V[i];
U[i]--;
V[i]--;
dfs(U[i], -1, V[i], i);
}
i64 ans = 0;
vector<i64> cnt(N);
for (auto s = 1; s < (1 << M); s++) {
bitset<100> bit;
rep(i, 0, M) {
if (s & (1 << i)) {
bit |= p[i];
}
}
if (__builtin_popcountll(s) & 1)
cnt[N - 1 - bit.count()]++;
else
cnt[N - 1 - bit.count()]--;
}
for (i64 i = 0; i < N; i++) {
ans += (1ll << i) * cnt[i];
}
cout << (1ll << (N - 1)) - ans << endl;
}
| replace | 31 | 32 | 31 | 32 | 0 | |
p02794 | C++ | Runtime Error | #define HAVE_STRUCT_TIMESPEC
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> v[57];
long long dp[1 << 20];
int x[57];
bool dfs(int a, int b, int c, int d) {
if (a == c)
return 1;
for (auto it : v[a]) {
if (it.first != b && dfs(it.first, a, c, d)) {
x[it.second] ^= 1 << d;
return 1;
}
}
return 0;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
int a, b;
cin >> a >> b;
v[a].emplace_back(b, i);
v[b].emplace_back(a, i);
}
int m;
cin >> m;
for (int i = 0; i < m; ++i) {
int a, b;
cin >> a >> b;
dfs(a, 0, b, i);
}
dp[0] = 1;
for (int i = 1; i < n; ++i)
for (int j = (1 << m) - 1; j >= 0; --j)
dp[j | x[i]] += dp[j];
cout << dp[(1 << m) - 1];
return 0;
}
| #define HAVE_STRUCT_TIMESPEC
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> v[57];
long long dp[1 << 20];
int x[57];
bool dfs(int a, int b, int c, int d) {
if (a == c)
return 1;
for (auto it : v[a]) {
if (it.first != b && dfs(it.first, a, c, d)) {
x[it.second] ^= 1 << d;
return 1;
}
}
return 0;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
for (int i = 1; i < n; ++i) {
int a, b;
cin >> a >> b;
v[a].emplace_back(b, i);
v[b].emplace_back(a, i);
}
int m;
cin >> m;
for (int i = 0; i < m; ++i) {
int a, b;
cin >> a >> b;
dfs(a, 0, b, i);
}
dp[0] = 1;
for (int i = 1; i < n; ++i)
for (int j = (1 << m) - 1; j >= 0; --j)
dp[j | x[i]] += dp[j];
cout << dp[(1 << m) - 1];
return 0;
}
| replace | 23 | 24 | 23 | 24 | 0 | |
p02794 | C++ | Runtime Error | // #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
/*@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
string message;
// 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
// 区間削除は出来ない
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
#define endl '\n'
// 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 lam1(ret) [&](auto &v) { return ret; }
#define lam2(v, ret) [&](auto &v) { return ret; }
#define lam(...) over2(__VA_ARGS__, lam2, lam1)(__VA_ARGS__)
#define lamr(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(vector<vector<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(vector<vector<vector<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(vector<vector<vector<vector<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(vector<vector<vector<vector<vector<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(vector<vector<vector<vector<vector<vector<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_3(fora_i, a, A) auto &&a = A[fora_i];
#define fora_init_4(a, b, A, B) \
auto &&a = A[fora_i]; \
auto &&b = B[fora_i];
#define fora_init_5(fora_i, 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_7(fora_i, 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_9(fora_i, 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(...) \
over9(__VA_ARGS__, fora_init_9, fora_init_8, fora_init_7, fora_init_6, \
fora_init_5, fora_init_4, fora_init_3, fora_init_2)(__VA_ARGS__)
#define forr_init_2(a, A) auto &&a = A[forr_i];
#define forr_init_3(forr_i, 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_5(forr_i, 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_7(forr_i, 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_9(forr_i, 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(...) \
over9(__VA_ARGS__, forr_init_9, forr_init_8, forr_init_7, forr_init_6, \
forr_init_5, forr_init_4, forr_init_3, 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_3(fora_i, a, A) rep(fora_i, sz(A))
#define fora_4(a, b, A, B) rep(fora_i, sz(A))
#define fora_5(fora_i, 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_7(fora_i, 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 fora_9(fora_i, 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_3(forr_i, a, A) rer(forr_i, sz(A) - 1)
#define forr_4(a, b, A, B) rer(forr_i, sz(A) - 1)
#define forr_5(forr_i, 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_7(forr_i, 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 forr_9(forr_i, 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(...) \
over9(__VA_ARGS__, forr_9, fora_8, fora_7, fora_6, fora_5, fora_4, fora_3, \
fora_2)(__VA_ARGS__)
#define forr(...) \
over9(__VA_ARGS__, forr_9, forr_8, forr_7, forr_6, forr_5, forr_4, forr_3, \
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, lamr(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, lamr(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, lamr(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, lamr(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, lamr(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, lamr(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, lamr(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, lamr(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];
}
template <class T> void na(set<T> &a, ll n) { rep(i, n) a.insert(in()); }
#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> void nad(set<T> &a, ll n) { rep(i, n) a.insert(in() - 1); }
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];
}
template <class T, class U> void na2(set<T> &a, set<U> &b, ll n) {
rep(i, n) {
a.insert(in());
b.insert(in());
}
}
#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 deb_2(x, ...) deb1(x) << ", " << deb1(__VA_ARGS__)
#define deb_3(x, ...) deb1(x) << ", " << deb_2(__VA_ARGS__)
#define deb_4(x, ...) deb1(x) << ", " << deb_3(__VA_ARGS__)
#define deb5(x, ...) deb1(x) << ", " << deb_4(__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, deb_4, \
deb_3, deb_2, deb1)(__VA_ARGS__) \
<< endl
#define base_keta 8
void print_n_base(int x, int base) { cerr << bitset<base_keta>(x) << endl; }
template <class T> void print_n_base(vector<T> X, int base) {
cerr << endl;
for (auto &&x : X) {
print_n_base(x, base);
}
cerr << endl;
}
// n進数
#define deb2(x) \
cerr << debugName(x) << " = "; \
print_n_base(x, 2);
#define deb3(x) \
cerr << debugName(x) << " = "; \
print_n_base(x, 3);
#define deb4(x) \
cerr << debugName(x) << " = "; \
print_n_base(x, 4);
#else
#define deb(...) ;
#define deb2(...) ;
#define deb3(...) ;
#define deb4(...) ;
#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);
}
}
}
ll rand(ll max) { return rand(0, max); }
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};
// 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 getr(ll a, ll keta) { return (a / (ll)pow(10, keta)) % 10; }
// 上から何桁目か
ll getl(ll a, ll ket) {
int sketa = keta(a);
return getr(a, sketa - 1 - ket);
}
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;
}
#ifdef _DEBUG
auto pow(int a, int k) {
static bool was = 1;
if (was) {
message += "if integer use *powi* it's very fast\n";
}
was = 0;
return std::pow(a, k);
}
auto pow(signed a, int k) { return pow((int)a, k); }
auto pow(signed a, signed k) { return pow((int)a, (int)k); }
#endif
// 整数型のpow
int powi(int a, int k) {
if (a == 2)
return 1ll << k;
int res = 1;
int x = a;
while (k) {
res *= (k & 1) * x;
x *= x;
k >>= 1;
}
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 bit(vi &a) {
int m = 0;
for (auto &&v : a)
m |= bit(v);
return m;
}
//{1, 1, 0} -> 011
// bitsetに置き換える感覚 i が立っていたら i bit目を立てる
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;
#undef getid
/*@formatter:off*/
#define forg(gi, ve) \
for (ll gi = 0, forglim = ve.size(), f, t, c; \
gi < forglim && (f = ve[gi].f, t = ve[gi].t, c = ve[gi].c, true); ++gi)
#define fort(gi, ve) \
for (ll gi = 0, f, t, c, wasp = (p != -1 && ve[gi].t == p) ? 1 : 0; \
(wasp = wasp ? 1 \
: (p != -1 && ve[gi].t == p) ? 1 \
: 0, \
gi + wasp < ve.size()) && \
(tie(f, t, c) = \
wasp ? make_tuple(ve[gi + 1].f, ve[gi + 1].t, ve[gi + 1].c) \
: make_tuple(ve[gi].f, ve[gi].t, ve[gi].c), \
true); \
++gi)
#define fore(gi, ve) \
for (ll gi = 0, forglim = ve.size(), f, t, c; \
gi < forglim && (f = ve[gi].f, t = ve[gi].t, c = ve[gi].c, true); ++gi)
template <class T> struct edge_ {
int f, t;
T c;
edge_(int f, int t, T c = 1) : f(f), t(t), c(c) {}
bool operator<(const edge_ &b) const { return c < b.c; }
bool operator>(const edge_ &b) const { return c > b.c; }
};
template <class T> ostream &operator<<(ostream &os, edge_<T> &e) {
os << e.f << " " << e.t << " " << e.c;
return os;
}
template <typename T> class graph {
public:
vector<vector<edge_<T>>> g;
vector<edge_<T>> edges;
int n;
explicit graph(int n) : n(n) { g.resize(n); }
void clear() { g.clear(), edges.clear(); }
void resize(int n) {
this->n = n;
g.resize(n);
}
int size() { return n; }
vector<edge_<T>> &operator[](int i) { return g[i]; }
virtual void add(int f, int t, T c) = 0;
virtual void set_edges() = 0;
};
template <typename T = ll> class digraph : public graph<T> {
public:
using graph<T>::g;
using graph<T>::n;
using graph<T>::edges;
explicit digraph(int n) : graph<T>(n) {}
void add(int f, int t, T c = 1) {
if (!(0 <= f && f < n && 0 <= t && t < n)) {
debugline("digraph add");
deb(f, t, c);
ole();
}
g[f].emplace_back(f, t, c);
edges.emplace_back(f, t, c); // edgesを使わないなら消せる
}
void ing(int n, int m, int minus = 1) {
this->resize(n);
rep(i, m) {
int f, t;
cin >> f >> t;
f -= minus;
t -= minus;
add(f, t);
}
}
void ingc(int n, int m, int minus = 1) {
this->resize(n);
rep(i, m) {
int f, t, c;
cin >> f >> t >> c;
f -= minus;
t -= minus;
add(f, t, c);
}
}
void set_edges() override {
if (sz(edges))
return;
rep(i, n) fora(e, g[i]) edges.push_back(e);
}
};
template <class T = int> class undigraph : public graph<T> {
public:
using graph<T>::g;
using graph<T>::n;
using graph<T>::edges;
explicit undigraph(int n) : graph<T>(n) {}
// f < t
void add(int f, int t, T c = 1) {
if (!(0 <= f && f < n && 0 <= t && t < n)) {
debugline("undigraph add");
deb(f, t, c);
ole();
}
g[f].emplace_back(f, t, c);
g[t].emplace_back(t, f, c);
edges.emplace_back(f, t, c); // edgesを使わないなら消せる
edges.emplace_back(t, f, c);
}
void add(edge_<T> &e) {
int f = e.f, t = e.t;
T c = e.c;
add(f, t, c);
}
void ing(int n, int m, int minus = 1) {
this->resize(n);
rep(i, m) {
int f, t;
cin >> f >> t;
f -= minus;
t -= minus;
add(f, t);
}
}
void ingc(int n, int m, int minus = 1) {
this->resize(n);
rep(i, m) {
int f, t, c;
cin >> f >> t >> c;
f -= minus;
t -= minus;
add(f, t, c);
}
}
void set_edges() override {
if (sz(edges))
return;
rep(i, n) fora(e, g[i]) edges.push_back(e);
}
};
#define dijkstra_path dis_path
template <class T>
vi dis_path(digraph<T> &g, vector<T> &dis, int s, int t, int init_value) {
assert(dis[t] != init_value);
auto rg = rev(g);
int now = t;
vi path;
path.push_back(now);
T cost = 0;
while (now != s) {
rep(gi, sz(rg[now])) {
int m = rg[now][gi].t;
if (dis[m] == init_value)
continue;
if (dis[m] + rg[now][gi].c + cost == dis[t]) {
cost += rg[now][gi].c;
now = m;
break;
}
}
path.push_back(now);
}
rev(path);
return path;
}
template <class T>
vi dis_path(undigraph<T> &g, vector<T> &dis, int s, int t, int init_value) {
assert(dis[t] != init_value);
int now = t;
vi path;
path.push_back(now);
T cost = 0;
while (now != s) {
rep(gi, sz(g[now])) {
int m = g[now][gi].t;
if (dis[m] == init_value)
continue;
if (dis[m] + g[now][gi].c + cost == dis[t]) {
cost += g[now][gi].c;
now = m;
break;
}
}
path.push_back(now);
}
rev(path);
return path;
}
template <class T>
vi dis_path(digraph<T> &g, vector<vector<T>> &dis, int s, int t,
int init_value) {
return dis_path(g, dis[s], s, t, init_value);
}
template <class T>
vi dis_path(undigraph<T> &g, vector<vector<T>> &dis, int s, int t,
int init_value) {
return dis_path(g, dis[s], s, t, init_value);
}
// O(N^2)
template <class T> vector<T> dijkstra_mitu(graph<T> &g, int s, int init_value) {
if (!(0 <= s && s < g.n)) {
debugline("dijkstra_mitu");
deb(s, g.n);
ole();
}
int n = g.n;
int initValue = MAX(int);
vector<T> dis(n, initValue);
dis[s] = 0;
vb used(n);
while (true) {
int v = -1;
rep(u, n) {
if (!used[u] && (v == -1 || dis[u] < dis[v]))
v = u;
}
if (v == -1)
break;
if (dis[v] == initValue)
break;
used[v] = true;
rep(u, sz(g[v])) {
auto e = g[v][u];
dis[e.t] = min(dis[e.t], dis[v] + e.c);
}
} /*基本、たどり着かないなら-1*/
for (auto &&d : dis) {
if (d == initValue) {
d = init_value;
}
}
return dis;
}
template <typename T> struct radixheap {
vector<pair<u64, T>> v[65];
u64 size, last;
radixheap() : size(0), last(0) {}
bool empty() const { return size == 0; }
int getbit(int a) { return a ? 64 - __builtin_clzll(a) : 0; }
void push(u64 key, const T &value) {
++size;
v[getbit(key ^ last)].emplace_back(key, value);
}
pair<u64, T> pop() {
if (v[0].empty()) {
int idx = 1;
while (v[idx].empty())
++idx;
last = min_element(begin(v[idx]), end(v[idx]))->first;
for (auto &p : v[idx])
v[getbit(p.first ^ last)].emplace_back(p);
v[idx].clear();
}
--size;
auto ret = v[0].back();
v[0].pop_back();
return ret;
}
};
/*radix_heap こっちの方が早い*/
// O((N+M) log N)
vi dijkstra(graph<int> &g, int s, int init_value) {
if (!(0 <= s && s < g.n)) {
debugline("dijkstra");
deb(s, g.n);
ole();
} /*O((N+M) log N) vs O(N^2)*/
if ((g.n + sz(g.edges)) * log2(N) > g.n * g.n) {
return dijkstra_mitu(g, s, init_value);
}
int initValue = MAX(int);
vi dis(g.n, initValue);
radixheap<int> q;
dis[s] = 0;
q.push(0, s);
while (!q.empty()) {
int nowc, i;
tie(nowc, i) = q.pop();
if (dis[i] != nowc)
continue;
for (auto &&e : g.g[i]) {
int to = e.t;
int c = nowc + e.c;
if (dis[to] > c) {
dis[to] = c;
q.push(dis[to], to);
}
}
} /*基本、たどり着かないなら-1*/
for (auto &&d : dis)
if (d == initValue)
d = init_value;
return dis;
}
template <class T>
vector<T> dijkstra_normal(graph<T> &g, int s, int init_value) {
if (!(0 <= s && s < g.n)) {
debugline("dijkstra");
deb(s, g.n);
ole();
}
if ((g.n + sz(g.edges)) * 20 > g.n * g.n) {
return dijkstra_mitu(g, s, init_value);
}
T initValue = MAX(T);
vector<T> dis(g.n, initValue);
priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> q;
dis[s] = 0;
q.emplace(0, s);
while (q.size()) {
T nowc = q.top().fi;
int i = q.top().se;
q.pop();
if (dis[i] != nowc)
continue;
for (auto &&e : g.g[i]) {
int to = e.t;
T c = nowc + e.c;
if (dis[to] > c) {
dis[to] = c;
q.emplace(dis[to], to);
}
}
} /*基本、たどり着かないなら-1*/
for (auto &&d : dis)
if (d == initValue)
d = init_value;
return dis;
}
template <class T> vector<T> dijkstra_01(graph<T> &g, int s, int init_value) {
int N = g.n;
vi dis(N, linf);
dis[s] = 0;
deque<int> q;
q.push_back(s);
vb was(N);
while (!q.empty()) {
int f = q.front();
q.pop_front();
if (was[f])
continue;
was[f] = true;
fora(e, g[f]) {
if (dis[e.t] > dis[f] + e.c) {
if (e.c) {
dis[e.t] = dis[f] + 1;
q.push_back(e.t);
} else {
dis[e.t] = dis[f];
q.push_front(e.t);
}
}
}
}
rep(i, N) if (dis[i] == linf) dis[i] = init_value;
return dis;
}
// dijkstra_cou<mint> : 数える型で書く return vp(dis,cou)
template <class COU, class T = int>
auto dijkstra_cou(const graph<T> &g, int s, int init_value) {
if (!(0 <= s && s < g.n)) {
debugline("dijkstra");
deb(s, g.n);
ole();
}
err("count by type COU ");
err("int or mint");
T initValue = MAX(T);
vector<T> dis(g.n, initValue);
vector<COU> cou(g.n);
cou[s] = 1;
priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> q;
dis[s] = 0;
q.emplace(0, s);
while (q.size()) {
T nowc = q.top().fi;
int i = q.top().se;
q.pop();
if (dis[i] != nowc)
continue;
for (auto &&e : g.g[i]) {
int to = e.t;
T c = nowc + e.c;
if (dis[to] > c) {
dis[to] = c;
cou[to] = cou[e.f];
q.emplace(dis[to], to);
} else if (dis[to] == c) {
cou[to] += cou[e.f];
}
}
} /*基本、たどり着かないなら-1*/
for (auto &&d : dis)
if (d == initValue)
d = init_value;
return vtop(dis, cou);
}
// 密グラフの時、warshallに投げる
template <class T>
vector<vector<T>> dijkstra_all(const graph<T> &g, int init_value) {
int n = g.n;
assert(n < 1e4);
if (n * n < (n + sz(g.edges)) * 14) { /*O(N^3) vs O(N (N+M)log N)*/
return warshall(g, init_value);
}
vector<vector<T>> dis(n);
rep(i, n) { dis[i] = dijkstra(g, i, init_value); }
return dis;
}
// コストを無限に減らせる := -linf
// たどり着けない := linf
template <class T> vector<T> bell(graph<T> &g, int s) {
if (g.n >= 1e4) {
cout << "bell size too big" << endl;
exit(0);
}
vector<T> res(g.n, linf);
res[s] = 0;
vb can(g.n); /*頂点から行けない頂点を持つ、辺を消しておく */
fix([&](auto ds, int p, int i) -> void {
if (can[i])
return;
can[i] = true;
forg(gi, g[i]) if (t != p) ds(i, t);
})(-1, 0);
vector<edge_<T>> es;
fora(e, g.edges) {
if (can[e.f])
es += e;
}
rep(i, g.n) {
bool upd = false;
fora(e, es) {
if (res[e.f] != linf && res[e.t] > res[e.f] + e.c) {
upd = true;
res[e.t] = res[e.f] + e.c;
}
}
if (!upd)
break;
}
rep(i, g.n * 2) {
bool upd = 0;
fora(e, g.edges) {
if (res[e.f] != linf && res[e.t] != -linf && res[e.t] > res[e.f] + e.c) {
upd = 1;
res[e.t] = -linf;
}
}
if (!upd)
break;
}
return res;
}
// コストを無限に増やせる := linf
// たどり着けない := -linf
template <class T> vector<T> bell_far(graph<T> &g, int s) {
if (g.n >= 1e4) {
cout << "bell_far size too big" << endl;
exit(0);
}
vector<T> res(g.n, linf);
res[s] = 0;
vb can(g.n); /*頂点から行けない頂点を持つ、辺を消しておく*/
fix([&](auto ds, int p, int i) -> void {
if (can[i])
return;
can[i] = true;
forg(gi, g[i]) if (t != p) ds(i, t);
})(-1, 0);
vector<edge_<T>> es;
fora(e, g.edges) {
if (can[e.f])
es += e;
}
rep(i, g.n) {
bool upd = false;
fora(e, es) {
if (res[e.f] != linf && res[e.t] > res[e.f] - e.c) { /*-c*/
upd = true;
res[e.t] = res[e.f] - e.c; /*-c*/
}
}
if (!upd)
break;
}
rep(i, g.n * 2) {
bool upd = 0;
fora(e, g.edges) {
if (res[e.f] != linf && res[e.t] != -linf &&
res[e.t] > res[e.f] - e.c) { /*-c*/
upd = 1;
res[e.t] = -linf;
}
}
if (!upd)
break;
}
rep(i, g.n) res[i] *= -1;
return res;
}
template <class T> vector<vector<T>> warshall(graph<T> &g, int init_value) {
int n = g.n;
assert(n < 1e4);
vector<vector<T>> dis(n, vector<T>(n, linf));
rep(i, n) fora(e, g.g[i]) {
if (dis[e.f][e.t] > e.c) {
dis[e.f][e.t] = e.c;
}
}
rep(i, n) dis[i][i] = 0;
rep(k, n) rep(i, n) rep(j, n) {
if (dis[i][j] > dis[i][k] + dis[k][j]) {
dis[i][j] = dis[i][k] + dis[k][j];
}
}
rep(i, n) rep(j, n) if (dis[i][j] == linf) dis[i][j] = init_value;
return dis;
}
template <class T> class MinOp {
public:
T operator()(T a, T b) { return min(a, b); }
};
template <typename OpFunc> struct SparseTable {
OpFunc op;
signed size;
vector<signed> lg;
vector<vector<pair<signed, signed>>> table;
void init(vector<pair<signed, signed>> &array, OpFunc opfunc) {
signed n = array.size();
op = opfunc;
lg.assign(n + 1, 0);
for (signed i = 1; i <= n; i++) {
lg[i] = 31 - __builtin_clz(i);
}
table.assign(lg[n] + 1, array);
for (signed i = 1; i <= lg[n]; i++) {
for (signed j = 0; j < n; j++) {
if (j + (1 << (i - 1)) < n) {
table[i][j] = op(table[i - 1][j], table[i - 1][j + (1 << (i - 1))]);
} else {
table[i][j] = table[i - 1][j];
}
}
}
}
pair<signed, signed> query(signed l, signed r) {
assert(l < r);
return op(table[lg[r - l]][l], table[lg[r - l]][r - (1 << lg[r - l])]);
}
};
struct PMORMQ {
vector<signed> a;
SparseTable<MinOp<pair<signed, signed>>> sparse_table;
vector<vector<vector<signed>>> lookup_table;
vector<signed> block_type;
signed block_size, n_block;
void init(vector<signed> &array) {
a = array;
signed n = a.size();
block_size = std::max(1, (31 - __builtin_clz(n)) / 2);
while (n % block_size != 0) {
a.push_back(a.back() + 1);
n++;
}
n_block = n / block_size;
vector<pair<signed, signed>> b(n_block, make_pair(INT_MAX, INT_MAX));
for (signed i = 0; i < n; i++) {
b[i / block_size] = min(b[i / block_size], make_pair(a[i], i));
}
sparse_table.init(b, MinOp<pair<signed, signed>>());
block_type.assign(n_block, 0);
for (signed i = 0; i < n_block; i++) {
signed cur = 0;
for (signed j = 0; j < block_size - 1; j++) {
signed ind = i * block_size + j;
if (a[ind] < a[ind + 1]) {
cur |= 1 << j;
}
}
block_type[i] = cur;
}
lookup_table.assign(
1 << (block_size - 1),
vector<vector<signed>>(block_size, vector<signed>(block_size + 1)));
for (signed i = 0; i < (1 << (block_size - 1)); i++) {
for (signed j = 0; j < block_size; j++) {
signed res = 0;
signed cur = 0;
signed pos = j;
for (signed k = j + 1; k <= block_size; k++) {
lookup_table[i][j][k] = pos;
if (i & (1 << (k - 1))) {
cur++;
} else {
cur--;
}
if (res > cur) {
pos = k;
res = cur;
}
}
}
}
}
signed query(signed l, signed r) {
assert(l < r);
signed lb = l / block_size;
signed rb = r / block_size;
if (lb == rb) {
return lb * block_size +
lookup_table[block_type[lb]][l % block_size][r % block_size];
}
signed pl = lb * block_size +
lookup_table[block_type[lb]][l % block_size][block_size];
signed pr =
rb * block_size + lookup_table[block_type[rb]][0][r % block_size];
signed pos = pl;
if (r % block_size > 0 && a[pl] > a[pr]) {
pos = pr;
}
if (lb + 1 == rb) {
return pos;
}
signed spv = sparse_table.query(lb + 1, rb).second;
if (a[pos] > a[spv]) {
return spv;
}
return pos;
}
};
template <class T = int> class tree : public undigraph<T> {
PMORMQ rmq;
int cnt;
vector<signed> id, in;
bool never = true;
bool never_hld = true;
void dfs(int x, int p, int d, int dis = 0) {
id[cnt] = x;
par_[x] = p;
rmq_dep.push_back(d);
disv[x] = dis;
in[x] = cnt++;
forg(gi, g[x]) {
if (t == p) {
continue;
}
dfs(t, x, d + 1, dis + c);
id[cnt] = x;
rmq_dep.push_back(d);
cnt++;
}
}
void precalc() {
never = false;
cnt = 0;
rmq_dep.clear();
disv.assign(n, 0);
in.assign(n, -1);
id.assign(2 * n - 1, -1);
par_.assign(n, -1);
dfs(root, -1, 0);
rmq.init(rmq_dep);
#ifdef _DEBUG
if (n >= 100)
return;
cerr << "---tree---" << endl;
rep(i, n) {
if (!(i == root || sz(g[i]) > 1))
continue;
cerr << i << " -> ";
vi ts;
forg(gi, g[i]) {
if (t != par_[i])
ts.push_back(t);
}
rep(i, sz(ts) - 1) cerr << ts[i] << ", ";
if (sz(ts))
cerr << ts.back() << endl;
}
cerr << endl;
#endif
}
int pos;
void hld_build() {
never_hld = false;
if (never)
precalc();
g.resize(n);
vid.resize(n, -1);
head.resize(n);
heavy.resize(n, -1);
depth.resize(n);
inv.resize(n);
subl.resize(n);
subr.resize(n);
dfs(root, -1);
t = 0;
dfs_hld(root);
#ifdef _DEBUG
if (n >= 100)
return;
cerr << "---hld_index---" << endl;
vi inds;
rep(i, n) if (sz(g[i])) inds.push_back(i);
rep(i, sz(inds)) {
str s = tos(bel(inds[i]));
cerr << std::right << std::setw(sz(s) + (i ? 1 : 0)) << inds[i];
}
cerr << endl;
rep(i, sz(inds)) { cerr << bel(inds[i]) << " "; }
cerr << endl << endl;
cerr << "---hld_edge_index---" << endl;
fora(e, edges) {
if (e.f <= e.t)
cerr << e.f << "-" << e.t << " " << bel(e) << endl;
}
cerr << endl << endl;
cerr << "!!query!! edge or not edge carefull!!" << endl;
#endif
}
int dfs(int curr, int prev) {
int sub = 1, max_sub = 0;
heavy[curr] = -1;
forg(i, g[curr]) {
int next = t;
if (next != prev) {
depth[next] = depth[curr] + 1;
int sub_next = dfs(next, curr);
sub += sub_next;
if (max_sub < sub_next)
max_sub = sub_next, heavy[curr] = next;
}
}
return sub;
}
int t = 0;
#ifndef __CLION_IDE__
void dfs_hld(int v = 0) {
vid[v] = subl[v] = t;
t++;
inv[subl[v]] = v;
if (0 <= heavy[v]) {
head[heavy[v]] = head[v];
dfs_hld(heavy[v]);
}
forg(i, g[v]) if (depth[v] < depth[t]) if (t != heavy[v]) {
head[t] = t;
dfs_hld(t);
}
subr[v] = t;
}
#endif //__CLION_IDE__
vector<signed> rmq_dep;
vi par_, depth, disv;
vi childs;
vi par_id_; // 親への辺のidを持つ
vvi(ids_); // 隣接で辺のidを持つ
public:
using undigraph<T>::g;
using undigraph<T>::n;
using undigraph<T>::edges;
int root;
//(f,t)と(t,f)は同じidを持ち、[0,n-1]でadd順に割り振られる
// 部分木の [左端、右端) index
// 部分木の辺に加算する場合
// add(subl[i],subr[i],x)
// add(sub[i],sub[i+1],-x)
vector<int> vid, head, heavy, inv, subl, subr;
tree(int n_, int root = 0) : undigraph<T>(n_), root(root) { n = n_; }
void change_root(int roo) {
root = roo;
precalc();
}
int lca(int a, int b) {
if (never)
precalc();
int x = in[a];
int y = in[b];
if (x > y) {
swap(x, y);
}
int pos = rmq.query(x, y + 1);
return id[pos];
}
int dis(int a, int b) {
if (never)
precalc();
int x = in[a];
int y = in[b];
if (x > y) {
swap(x, y);
}
int pos = rmq.query(x, y + 1);
int p = id[pos];
return disv[a] + disv[b] - disv[p] * 2;
}
int dep(int a) {
if (never)
precalc();
return disv[a];
}
int par(int a) {
if (never)
precalc();
return par_[a];
}
bool isleaf(int i) {
if (never)
precalc();
return (sz(g[i]) == 1 && i != root);
}
vi child(int r) {
vi res;
res.push_back(r);
queue<int> q;
q.push(r);
while (!q.empty()) {
int i = q.front();
res.push_back(i);
q.pop();
forg(gi, g[i]) {
if (t != par(i))
q.push(t);
}
}
return res;
}
vb child_ex(int r) {
vb res(n);
res[r] = true;
queue<int> q;
q.push(r);
while (!q.empty()) {
int i = q.front();
res[i] = true;
q.pop();
forg(gi, g[i]) {
if (t != par(i))
q.push(t);
}
}
return res;
}
int dfs_count_subtree(int p, int i) {
childs[i] = 1;
fort(gi, g[i]) { childs[i] += dfs_count_subtree(i, t); }
return childs[i];
}
#define count_child count_subtree
int count_subtree(int f) {
if (sz(childs) == 0) {
if (never)
precalc();
childs.resize(n);
dfs_count_subtree(-1, root);
}
return childs[f];
}
// fからtの辺を切った時のtの大きさ
int count_subtree(int f, int t) {
if (par(f) == t) {
return n - count_subtree(f);
} else {
return count_subtree(t);
}
}
vi path(int a, int b) {
vi res;
for_each_l(a, b, [&](int i) { res.push_back(i); });
return res;
}
int par_id(int i) {
if (sz(par_id_) == 0) {
pathe(0, 0);
}
return par_id_[i];
}
int ids(int f, int i) {
if (sz(ids_) == 0) {
pathe(0, 0);
}
return ids_[f][i];
}
edge_<T> edge(int id) { return edges[id << 1]; }
// pathに含まれる辺のid達を返す
vi pathe(int a, int b) {
#ifdef _DEBUG
static bool was = 0;
if (!was)
message += "can't edges[id]. must edge(id)\n";
was = 1;
#endif
if (sz(par_id_) == 0) {
ids_.resize(n);
par_id_.resize(n);
rep(i, 0, sz(edges), 2) {
ids_[edges[i].f].emplace_back(i >> 1);
ids_[edges[i].t].emplace_back(i >> 1);
}
if (never)
precalc(); /*par_を呼ぶため*/
rep(i, n) {
int pa = par_[i];
forg(gi, g[i]) {
if (t == pa) {
par_id_[i] = ids_[i][gi];
}
}
}
}
int u = lca(a, b);
vi res;
if (a != u) {
do {
res.emplace_back(par_id_[a]);
a = par_[a];
} while (a != u);
}
vi rev;
if (b != u) {
do {
rev.emplace_back(par_id_[b]);
b = par_[b];
} while (b != u);
}
rer(i, sz(rev) - 1) { res.emplace_back(rev[i]); }
return res;
}
/*O(N) hldを使わず木を普通にたどる liteの意*/
void for_each_l(int u, int v, function<void(int)> fnode) {
int r = lca(u, v);
while (u != r) {
fnode(u);
u = par_[u];
}
fnode(r);
vi a;
while (v != r) {
a.push_back(v);
v = par_[v];
}
while (sz(a)) {
fnode(a.back());
a.pop_back();
}
}
void for_each_edge_l(int u, int v, function<void(edge_<int> &)> fedge) {
int r = lca(u, v);
while (u != r) {
forg(gi, g[u]) {
if (t == par_[u]) {
fedge(g[u][gi]);
u = par_[u];
break;
}
}
}
vector<edge_<int>> qs;
while (v != r) {
forg(gi, g[v]) {
if (t == par_[v]) {
qs.push_back(g[v][gi]);
v = par_[v];
break;
}
}
}
while (sz(qs)) {
fedge(qs.back());
qs.pop_back();
}
}
// Fは半開 (u,v)は木の頂点
// 中ではhldの頂点を見るため、seg木のupdateはhldのindexで行なう
void for_each_ /*[l,r)*/ (int u, int v, const function<void(int, int)> &f) {
if (never_hld)
hld_build();
while (1) {
if (vid[u] > vid[v])
swap(u, v);
int l = max(vid[head[v]], vid[u]);
int r = vid[v] + 1;
f(l, r);
if (head[u] != head[v])
v = par_[head[v]];
else
break;
}
}
void for_each_edge /*[l,r) O(log(N)) 辺を頂点として扱っている
上と同じ感じで使える*/
(int u, int v, const function<void(int, int)> &f) {
if (never_hld)
hld_build();
while (1) {
if (vid[u] > vid[v])
swap(u, v);
if (head[u] != head[v]) {
int l = vid[head[v]];
int r = vid[v] + 1;
f(l, r);
v = par_[head[v]];
} else {
if (u != v) {
int l = vid[u] + 1;
int r = vid[v] + 1;
f(l, r);
}
break;
}
}
}
int bel(int v) { /*hld内での頂点番号を返す*/
if (never_hld)
hld_build();
return vid[v];
}
// 下の頂点に辺のクエリを持たせている
int bel(
int f,
int t) { /*辺のクエリを扱うときどの頂点に持たせればいいか(vidを返すのでそのままupd出来る)*/
if (never_hld)
hld_build();
return depth[f] > depth[t] ? vid[f] : vid[t];
}
int bel(
edge_<T> &
e) { /*辺のクエリを扱うときどの頂点に持たせればいいか(vidを返すのでそのままupd出来る)*/
if (never_hld)
hld_build();
return depth[e.f] > depth[e.t] ? vid[e.f] : vid[e.t];
}
template <class... U> int operator()(U... args) { return bel(args...); }
// path l -> r += v
template <class S> void seg_add(S &seg, int lhei, int rhei, int v) {
for_each_(lhei, rhei, [&](int l, int r) { seg.add(l, r, v); });
}
template <class S> void seg_update(S &seg, int lhei, int rhei, int v) {
for_each_(lhei, rhei, [&](int l, int r) { seg.update(l, r, v); });
}
template <class S> T seg_get(S &seg, int lhei, int rhei) {
T res = seg.e;
for_each_(lhei, rhei,
[&](int l, int r) { res = seg.f(res, seg.get(l, r)); });
return res;
}
template <class S> void seg_add_edge(S &seg, int lhei, int rhei, int v) {
for_each_edge(lhei, rhei, [&](int l, int r) { seg.add(l, r, v); });
}
template <class S> void seg_update_edge(S &seg, int lhei, int rhei, int v) {
for_each_edge(lhei, rhei, [&](int l, int r) { seg.update(l, r, v); });
}
template <class S> T seg_get_edge(S &seg, int lhei, int rhei) {
T res = seg.e;
for_each_edge(lhei, rhei,
[&](int l, int r) { res = seg.f(res, seg.get(l, r)); });
return res;
}
// 単体 edgeは上で処理できる
template <class S> void seg_add(S &seg, int i, int v) { seg.add(bel(i), v); }
template <class S> void seg_update(S &seg, int i, int v) {
seg.update(bel(i), v);
}
template <class S> T seg_get(S &seg, int i) { return seg[i]; }
template <class S> void seg_del(S &seg, int i) { seg.del(bel(i)); }
// 部分木iに対するクエリ
template <class S> void seg_add_sub(S &seg, int i, int v) {
if (never_hld)
hld_build();
seg.add(subl[i], subr[i], v);
}
template <class S> void seg_update_sub(S &seg, int i, int v) {
if (never_hld)
hld_build();
seg.update(subl[i], subr[i], v);
}
template <class S> T seg_get_sub(S &seg, int i, int v) {
if (never_hld)
hld_build();
return seg.get(subl[i], subr[i], v);
}
template <class S> void seg_add_sub_edge(S &seg, int i, int v) {
if (never_hld)
hld_build(); /*iの上の辺が数えられてしまうため、i+1から*/
seg.add(subl[i] + 1, subr[i], v);
}
template <class S> void seg_update_sub_edge(S &seg, int i, int v) {
if (never_hld)
hld_build(); /*iの上の辺が数えられてしまうため、i+1から*/
seg.update(subl[i] + 1, subr[i], v);
}
template <class S> T seg_get_sub_edge(S &seg, int i, int v) {
if (never_hld)
hld_build(); /*iの上の辺が数えられてしまうため、i+1から*/
return seg.get(subl[i] + 1, subr[i], v);
}
};
// 辺が多いのでedgesを持たない
// cost oo, ox, xo, xx 渡す
template <class T = int> class grid_k6 : public undigraph<T> {
vi costs;
public:
using undigraph<T>::g;
using undigraph<T>::n;
using undigraph<T>::edges;
int H, W;
vector<vector<char>> ba;
char wall;
void add(int f, int t, T c = 1) {
if (!(0 <= f && f < n && 0 <= t && t < n)) {
debugline("grid_k6 add");
deb(f, t, c);
ole();
}
g[f].emplace_back(f, t, c);
g[t].emplace_back(t, f, c);
// undigraphと違い、edgesを持たない
}
int getid(int h, int w) {
assert(ins(h, w, H, W));
return W * h + w;
}
int getid(P p) { return getid(p.first, p.second); }
P get2(int id) { return mp(id / W, id % W); }
P operator()(int id) { return get2(id); }
int operator()(int h, int w) { return getid(h, w); }
int operator()(P p) { return getid(p); }
// 辺は無い
grid_k6(int H, int W) : H(H), W(W), undigraph<T>(H * W) {}
grid_k6(vector<vector<char>> ba, char wall = '#')
: H(sz(ba)), W(sz(ba[0])), undigraph<T>(sz(ba) * sz(ba[0])) {
rep(h, H) {
rep(w, W) {
if (ba[h][w] == wall)
con;
int f = getid(h, w);
if (w + 1 < W && ba[h][w + 1] != wall) {
add(f, getid(h, w + 1));
}
if (h + 1 < H && ba[h + 1][w] != wall) {
add(f, getid(h + 1, w));
}
}
}
}
/*o -> o, o -> x, x-> x*/
grid_k6(vector<vector<char>> ba, int oo, int ox, int xo, int xx,
char wall = '#')
: H(sz(ba)), W(sz(ba[0])), undigraph<T>(sz(ba) * sz(ba[0])),
costs({oo, ox, xo, xx}), ba(ba), wall(wall) {
rep(h, H) {
rep(w, W) {
add(h, w, h + 1, w);
add(h, w, h - 1, w);
add(h, w, h, w + 1);
add(h, w, h, w - 1);
}
}
}
void add(int fh, int fw, int th, int tw) {
if (ins(fh, fw, H, W) && ins(th, tw, H, W)) {
int cm = 0;
if (ba[fh][fw] == wall) {
cm += 2;
}
if (ba[th][tw] == wall) {
cm++;
}
int f = getid(fh, fw);
int t = getid(th, tw);
g[f].emplace_back(f, t, costs[cm]);
}
}
void set_edges() {
rep(i, n) fora(e, g[i]) if (e.f < e.t) edges.push_back(e);
}
};
// 辺によりメモリを大量消費ためedgesを消している
// 頂点10^6でメモリを190MB(制限の8割)使う
// 左上から右下に移動できる
template <class T = int> class digrid_k6 : public digraph<T> {
public:
using digraph<T>::g;
using digraph<T>::n;
using digraph<T>::edges;
int H, W;
void add(int f, int t, T c = 1) {
if (!(0 <= f && f < n && 0 <= t && t < n)) {
debugline("digrid_k6 add");
deb(f, t, c);
ole();
}
g[f].emplace_back(f, t, c);
/*digraphと違いedgesを持たない*/
}
int getid(int h, int w) {
if (!ins(h, w, H, W))
return -1;
return W * h + w;
}
P get2(int id) { return mp(id / W, id % W); }
P operator()(int id) { return get2(id); }
int operator()(int h, int w) { return getid(h, w); }
digrid_k6(int H, int W) : H(H), W(W), digraph<T>(H * W) {}
digrid_k6(vector<vector<char>> ba, char wall = '#')
: H(sz(ba)), W(sz(ba[0])), digraph<T>(sz(ba) * sz(ba[0])) {
rep(h, H) {
rep(w, W) {
if (ba[h][w] == wall)
con;
int f = getid(h, w);
if (w + 1 < W && ba[h][w + 1] != wall) {
add(f, getid(h, w + 1));
}
if (h + 1 < H && ba[h + 1][w] != wall) {
add(f, getid(h + 1, w));
}
}
}
}
void add(int fh, int fw, int th, int tw) {
add(getid(fh, fw), getid(th, tw));
}
void set_edges() { rep(i, n) fora(e, g[i]) edges.push_back(e); }
};
// edgesを持たない
// dijkstra(g,0)[t] とかける (t+n-1等とする必要はない)
template <class T = int> class segdi : public digraph<T> {
int getid(int k) {
if (k >= len * 2 - 1) {
return k;
} else if (k < len - 1) {
return k + len;
} else {
return k - len + 1;
}
}
void add(int f, int t, T c = 1) {
f = getid(f);
t = getid(t);
if (!(0 <= f && f < n && 0 <= t && t < n)) {
debugline("segdi add");
deb(f, t, c);
ole();
}
g[f].emplace_back(f, t, c); /*digraphと違いedgesを持たない*/
}
int mid;
int len;
public:
using digraph<T>::g;
using digraph<T>::n;
using digraph<T>::edges;
// 頂点が足りなくなったらresize
segdi(int n_) : digraph<T>(1) {
int nn = 1;
while (nn < n_)
nn *= 2;
n_ = nn;
len = n_;
this->resize(n_ + (n_ - 1) * 2 + n_);
mid = n_ + (n_ - 1) * 2;
int ad = len * 2 - 1;
rep(i, len - 1) {
add(i, i * 2 + 1, 0);
add(i, i * 2 + 2, 0);
if (i * 2 + 1 >= len - 1) {
add(i * 2 + 1, i + ad, 0);
add(i * 2 + 2, i + ad, 0);
} else {
add(i * 2 + 1 + ad, i + ad, 0);
add(i * 2 + 2 + ad, i + ad, 0);
}
}
}
void dfs(vi &list, int nl, int nr, int k, int l, int r) {
if (r <= nl || nr <= l)
return;
if (l <= nl && nr <= r) {
list += k;
} else {
dfs(list, nl, (nl + nr) / 2, k * 2 + 1, l, r);
dfs(list, (nl + nr) / 2, nr, k * 2 + 2, l, r);
}
}
void rekkyo(vi &list, int l, int r) {
l += len - 1;
r += len - 1;
while (l < r) {
if (!(l & 1)) {
list.push_back(l);
}
if (!(r & 1)) {
list.push_back(r - 1);
}
l >>= 1;
r = (r - 1) >> 1;
}
}
// 半開
void add(int fl, int fr, int tl, int tr, int cost) { /*fは下側*/
int ad = 2 * len - 1;
if (mid >= n) {
this->resize(n + 100);
}
{
int l = fl, r = fr;
l += len - 1;
r += len - 1;
while (l < r) {
if (!(l & 1)) {
if (l - len + 1 < 0)
add(l + ad, mid, cost);
else
add(l, mid, cost);
}
if (!(r & 1)) {
if (r - 1 - len + 1 < 0)
add(r - 1 + ad, mid, cost);
else
add(r - 1, mid, cost);
}
l >>= 1;
r = (r - 1) >> 1;
}
}
{
int l = tl, r = tr;
l += len - 1;
r += len - 1;
while (l < r) {
if (!(l & 1)) {
add(mid, l, 0);
}
if (!(r & 1)) {
add(mid, r - 1, 0);
}
l >>= 1;
r = (r - 1) >> 1;
}
}
mid++;
}
};
// edgesを持たない
// dijkstra(g,0)[t] とかける (t+n-1等とする必要はない)
template <class T = int> class segun : public undigraph<T> {
int getid(int k) {
if (k >= len * 2 - 1) {
return k;
} else if (k < len - 1) {
return k + len;
} else {
return k - len + 1;
}
}
void add(int f, int t, T c = 1) {
f = getid(f);
t = getid(t);
if (!(0 <= f && f < n && 0 <= t && t < n)) {
debugline("segun add");
deb(f, t, c);
ole();
}
g[f].emplace_back(f, t, c); /*digraphと違いedgesを持たない*/
g[t].emplace_back(t, f, c); /*digraphと違いedgesを持たない*/
}
int mid;
int len;
public:
using digraph<T>::g;
using digraph<T>::n;
using digraph<T>::edges;
// 頂点が足りなくなったらresize
segun(int n_) : digraph<T>(1) {
int nn = 1;
while (nn < n_)
nn *= 2;
n_ = nn;
len = n_;
this->resize(n_ + (n_ - 1) * 2 + n_);
mid = n_ + (n_ - 1) * 2;
int ad = len * 2 - 1;
rep(i, len - 1) {
add(i, i * 2 + 1, 0);
add(i, i * 2 + 2, 0);
if (i * 2 + 1 >= len - 1) {
add(i * 2 + 1, i + ad, 0);
add(i * 2 + 2, i + ad, 0);
} else {
add(i * 2 + 1 + ad, i + ad, 0);
add(i * 2 + 2 + ad, i + ad, 0);
}
}
}
void dfs(vi &list, int nl, int nr, int k, int l, int r) {
if (r <= nl || nr <= l)
return;
if (l <= nl && nr <= r) {
list += k;
} else {
dfs(list, nl, (nl + nr) / 2, k * 2 + 1, l, r);
dfs(list, (nl + nr) / 2, nr, k * 2 + 2, l, r);
}
}
void rekkyo(vi &list, int l, int r) {
l += len - 1;
r += len - 1;
while (l < r) {
if (!(l & 1)) {
list.push_back(l);
}
if (!(r & 1)) {
list.push_back(r - 1);
}
l >>= 1;
r = (r - 1) >> 1;
}
}
// 半開
void add(int fl, int fr, int tl, int tr, int cost) { /*fは下側*/
int ad = 2 * len - 1;
if (mid >= n) {
this->resize(n + 100);
}
{
int l = fl, r = fr;
l += len - 1;
r += len - 1;
while (l < r) {
if (!(l & 1)) {
if (l - len + 1 < 0)
add(l + ad, mid, cost);
else
add(l, mid, cost);
}
if (!(r & 1)) {
if (r - 1 - len + 1 < 0)
add(r - 1 + ad, mid, cost);
else
add(r - 1, mid, cost);
}
l >>= 1;
r = (r - 1) >> 1;
}
}
{
int l = tl, r = tr;
l += len - 1;
r += len - 1;
while (l < r) {
if (!(l & 1)) {
add(mid, l, 0);
}
if (!(r & 1)) {
add(mid, r - 1, 0);
}
l >>= 1;
r = (r - 1) >> 1;
}
}
mid++;
}
};
#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 unionfind unionfind_graph
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;
}
}
template <class T> unionfind(graph<T> &g) : n(g.n), trees(g.n) {
par.resize(n);
siz.resize(n);
es.resize(n);
for (ll i = 0; i < n; i++) {
par[i] = i;
siz[i] = 1;
}
add(g);
}
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;
}
template <class T> void add(graph<T> &g) {
fora(e, g.edges) { unite(e.f, e.t); }
}
template <class T> void unite(graph<T> &g) { add(g); }
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
//@出力
template <class T> ostream &operator<<(ostream &os, digraph<T> &g) {
os << endl << g.n << " " << sz(g.edges) << endl;
fore(gi, g.edges) { os << f << " " << t << " " << c << endl; }
return os;
}
template <class T> ostream &operator<<(ostream &os, undigraph<T> &g) {
os << endl << g.n << " " << sz(g.edges) / 2 << endl;
fore(gi, g.edges) {
if (f < t)
os << f << " " << t << " " << c << endl;
}
return os;
}
//@判定
template <class T> bool nibu(graph<T> &g) {
int size = 0;
rep(i, g.n) size += sz(g.g[i]);
if (size == 0)
return true;
unionfind uf(g.n * 2);
rep(i, g.n) fora(e, g.g[i]) uf.unite(e.f, e.t + g.n),
uf.unite(e.f + g.n, e.t);
rep(i, g.n) if (uf.same(i, i + g.n)) return 0;
return true;
}
// 頂点ではなく辺の数に依存した計算量 O(E)
template <class T> bool nibu_sub(graph<T> &g) {
umapi col; /*0なら無色 */
queue<int> q; /*色は01か11 */
fora(e, g.edegs) { /*fとtの色を持つか否かは同じ*/
if (col[e.f] == 0)
q.push(e.f);
while (!q.empty()) {
int f = q.front();
q.pop();
int fc = col[f];
forg(gi, g[f]) {
int &tc = col[t]; /*fcには色がある*/
if (fc == tc)
return false; /*違う色*/
if (tc)
continue; /*無色*/
tc = fc ^ 2;
q.push(tc);
}
}
}
return true;
}
// 二部グラフを色分けした際の頂点数を返す
template <class T> vp nibug(graph<T> &g) {
vp cg;
if (!nibu(g)) {
debugline("nibu");
ole();
}
int n = g.size();
vb was(n);
queue<P> q;
rep(i, n) {
if (was[i])
continue;
q.push(mp(i, 1));
was[i] = 1;
int red = 0;
int coun = 0;
while (q.size()) {
int now = q.front().fi;
int col = q.front().se;
red += col;
coun++;
q.pop();
forg(gi, g[now]) {
if (was[t])
continue;
q.push(mp(t, col ^ 1));
was[t] = 1;
}
}
cg.push_back(mp(red, coun - red));
}
return cg;
}
// 連結グラフが与えられる 閉路があるか
template <class T> bool close(undigraph<T> &g) {
int n = 0;
int e = 0;
rep(i, g.n) {
if (sz(g[i]))
n++;
forg(gi, g[i]) { e++; }
}
return (e >> 1) >= n;
}
template <class T> bool close(undigraph<T> &g, int v) {
unionfind uf(g.n);
rep(i, g.n) {
forg(gi, g[i]) {
if (f < t)
break;
if (f == t && f == v)
return true;
if (uf.same(f, v) && uf.same(t, v))
return true;
uf.unite(f, t);
}
}
return false;
}
template <class T> bool close(digraph<T> &g) {
vi res;
return topo(res, g);
}
//@変形
// 条件(f!=0等 f,t,cが使える)を満たすsubgraphをg2に代入
#define sub_di(g, g2, zhouken) \
{ \
g2.resize(g.n); \
fore(gi, g.edges) { \
if (zhouken) { \
g2.add(f, t, c); \
} \
} \
}
#define sub_un(g, g2, zhouken) \
{ \
g2.resize(g.n); \
fore(gi, g.edges) { \
bool ok = zhouken; /*片方がアウトなら駄目という扱い*/ \
swap(f, t); \
ok &= zhouken; \
if (ok && f < t) { \
g2.add(f, t, c); \
} \
} \
}
#define sub_tree sub_un
// 閉路がなければtrue
bool topo(vi &res, digraph<int> &g) {
int n = g.g.size();
vi nyu(n);
rep(i, n) for (auto &&e : g[i]) nyu[e.t]++;
queue<int> st;
rep(i, n) if (nyu[i] == 0) st.push(i);
while (st.size()) {
int v = st.front();
st.pop();
res.push_back(v);
fora(e, g[v]) if (--nyu[e.t] == 0) st.push(e.t);
}
return res.size() == n;
}
// 辞書順最小トポロジカルソート
bool topos(vi &res, digraph<int> &g) {
int n = g.g.size();
vi nyu(n);
rep(i, n) for (auto &&e : g[i]) nyu[e.t]++; /*小さい順*/
priority_queue<int, vector<int>, greater<int>> q;
rep(i, n) if (nyu[i] == 0) q.push(i);
while (q.size()) {
int i = q.top();
q.pop();
res.push_back(i);
fora(e, g[i]) if (--nyu[e.t] == 0) q.push(e.t);
}
return res.size() == n;
}
template <class T> digraph<T> rev(digraph<T> &g) {
digraph<T> r(g.n);
rep(i, g.n) {
forg(gi, g[i]) { r.add(t, f, c); }
}
return r;
}
// lc,rcは子を持つ中で一番左、右
//(g,ind,l,r)
template <class T> tree<T> get_bfs_tree(tree<T> &g, vi &ind, vi &l, vi &r) {
if (sz(ind)) {
cerr << "ind must be empty" << endl;
exit(0);
}
int N = sz(g);
ind.resize(N);
l.resize(N, inf);
r.resize(N, -1);
tree<T> h(N);
queue<P> q;
q.emplace(-1, 0);
int c = 0;
while (sz(q)) {
int p = q.front().first;
int i = q.front().second;
q.pop();
ind[i] = c;
if (~p)
chmi(l[ind[p]], c);
if (~p)
chma(r[ind[p]], c);
c++;
forg(gi, g[i]) {
if (t != p)
q.emplace(i, t);
}
}
fora(e, g.edges) {
if (e.f < e.t) {
h.add(ind[e.f], ind[e.t], e.c);
}
}
rep(i, N) {
if (l[i] == inf)
l[i] = -1;
}
return h;
}
// lc,rcは子を持つ中で一番左、右
// たとえばl[lc[x]は2段下の最左
//(g,ind,l,r,lc,rc)
template <class T>
tree<T> get_bfs_tree(tree<T> &g, vi &ind, vi &l, vi &r, vi &lc, vi &rc) {
if (sz(ind)) {
cerr << "ind must be empty" << endl;
exit(0);
}
int N = sz(g);
ind.resize(N);
l.resize(N, inf);
lc.resize(N, inf);
r.resize(N, -1);
rc.resize(N, -1);
tree<T> h(N);
queue<P> q;
q.emplace(-1, 0);
int c = 0;
while (sz(q)) {
int p = q.front().first;
int i = q.front().second;
q.pop();
ind[i] = c;
if (~p) {
chmi(l[ind[p]], c);
chma(r[ind[p]], c);
if (sz(g[i]) > 1) {
chmi(lc[ind[p]], c);
chma(rc[ind[p]], c);
}
}
c++;
forg(gi, g[i]) {
if (t != p)
q.emplace(i, t);
}
}
fora(e, g.edges) {
if (e.f < e.t) {
h.add(ind[e.f], ind[e.t], e.c);
}
}
rep(i, N) {
if (l[i] == inf)
l[i] = -1;
if (lc[i] == inf)
lc[i] = -1;
}
return h;
}
//@集計
template <class T> vi indegree(graph<T> &g) {
vi ret(g.size());
rep(i, g.size()) {
forg(gi, g[i]) { ret[t]++; }
}
return ret;
}
template <class T> vi outdegree(graph<T> &g) {
vi ret(g.size());
rep(i, g.size()) { ret[i] = g[i].size(); }
return ret;
}
#define kansetu articulation
// private
/*private*/ P farthest____(tree<> &E, int cur, int pre, int d, vi &D) {
D[cur] = d;
P r = {d, cur};
forg(gi, E[cur]) if (t != pre) {
P v = farthest____(E, t, cur, d + 1, D);
r = max(r, v);
}
return r;
}
// dagでなければ-1を返す
int diameter(digraph<> &g) {
vi per;
if (!topo(per, g))
return -1;
int n = g.n;
vi dp(n, 1);
fora(v, per) {
forg(gi, g[v]) { chma(dp[t], dp[f] + 1); }
}
return max(dp);
}
// iから最も離れた距離
vi diameters(tree<> &E) { /* diameter,center*/
vi D[3];
D[0].resize(E.size());
D[1].resize(E.size());
auto v1 = farthest____(E, 0, 0, 0, D[0]);
auto v2 = farthest____(E, v1.second, v1.second, 0, D[0]);
farthest____(E, v2.second, v2.second, 0, D[1]);
int i;
rep(i, D[0].size()) D[2].push_back(max(D[0][i], D[1][i]));
return D[2];
}
// iに対応するjと距離
vp diameters_p(tree<> &E) { /* diameter,center*/
vector<int> D[3];
D[0].resize(E.size());
D[1].resize(E.size());
auto v1 = farthest____(E, 0, 0, 0, D[0]);
auto v2 = farthest____(E, v1.second, v1.second, 0, D[0]);
farthest____(E, v2.second, v2.second, 0, D[1]);
int i;
vp res(E.size());
rep(i, D[0].size()) {
if (D[0][i] > D[1][i])
res[i] = mp(v1.second, D[0][i]);
else
res[i] = mp(v2.second, D[1][i]);
}
return res;
}
int diameter(tree<> &E) {
vi d = diameters(E);
return max(d);
}
// 最も離れた二点を返す
P diameter_p(tree<> &E) {
auto d = diameters_p(E);
int dis = -1;
int l = -1, r = -1;
rep(i, sz(d)) {
if (chma(dis, d[i].se)) {
l = i;
r = d[i].fi;
}
}
return mp(l, r);
}
//@列挙 取得
// 閉路がある時linfを返す
template <class T> int longest_path(digraph<T> &g) {
vi top;
if (!topo(top, g)) {
return linf;
}
int n = sz(top);
vi dp(n, 0);
for (auto &&i : top) {
forg(gi, g[i]) { chma(dp[t], dp[i] + 1); }
}
return max(dp);
}
template <class T> vi longest_path_v(digraph<T> &g) {
vi top;
if (!topo(top, g)) {
return vi();
}
int n = sz(top);
vi dp(n, 0);
vi pre(n, -1);
for (auto &&i : top) {
forg(gi, g[i]) {
if (chma(dp[t], dp[i] + 1)) {
pre[t] = i;
}
}
}
int s = std::max_element(dp.begin(), dp.end()) - dp.begin();
vi path;
while (s != -1) {
path.push_back(s);
s = pre[s];
}
std::reverse(path.begin(), path.end());
return path;
}
// 橋を列挙する (取り除くと連結でなくなる辺)
template <class T> vp bridge(graph<T> &G) {
static bool was;
vp brid;
vi articulation;
vi ord(G.n), low(G.n);
vb vis(G.n);
function<void(int, int, int)> dfs = [&](int v, int p, int k) {
vis[v] = true;
ord[v] = k++;
low[v] = ord[v];
bool isArticulation = false;
int ct = 0;
for (int i = 0; i < G[v].size(); i++) {
if (!vis[G[v][i].t]) {
ct++;
dfs(G[v][i].t, v, k);
low[v] = min(low[v], low[G[v][i].t]);
if (~p && ord[v] <= low[G[v][i].t])
isArticulation = true;
if (ord[v] < low[G[v][i].t])
brid.push_back(make_pair(min(v, G[v][i].t), max(v, G[v][i].t)));
} else if (G[v][i].t != p) {
low[v] = min(low[v], ord[G[v][i].t]);
}
}
if (p == -1 && ct > 1)
isArticulation = true;
if (isArticulation)
articulation.push_back(v);
};
int k = 0;
rep(i, G.n) {
if (!vis[i])
dfs(i, -1, k);
}
sort(brid.begin(), brid.end());
return brid;
}
// 間接点を列挙する (取り除くと連結でなくなる点)
template <class T> vi articulation(undigraph<T> &G) {
static bool was;
vp bridge;
vi arti;
vi ord(G.n), low(G.n);
vb vis(G.n);
function<void(int, int, int)> dfs = [&](int v, int p, int k) {
vis[v] = true;
ord[v] = k++;
low[v] = ord[v];
bool isArticulation = false;
int ct = 0;
for (int i = 0; i < G[v].size(); i++) {
if (!vis[G[v][i].t]) {
ct++;
dfs(G[v][i].t, v, k);
low[v] = min(low[v], low[G[v][i].t]);
if (~p && ord[v] <= low[G[v][i].t])
isArticulation = true;
if (ord[v] < low[G[v][i].t])
bridge.push_back(make_pair(min(v, G[v][i].t), max(v, G[v][i].t)));
} else if (G[v][i].t != p) {
low[v] = min(low[v], ord[G[v][i].t]);
}
}
if (p == -1 && ct > 1)
isArticulation = true;
if (isArticulation)
arti.push_back(v);
};
int k = 0;
rep(i, G.n) {
if (!vis[i])
dfs(i, -1, k);
}
sort(arti.begin(), arti.end());
return arti;
}
// 閉路パスを一つ返す
vi close_path(digraph<> &g) {
int n = g.n;
vi state(n);
vi path;
rep(i, n) if (!state[i]) {
if (fix([&](auto dfs, int v) -> bool {
if (state[v]) {
if (state[v] == 1) {
path.erase(path.begin(), find(path.begin(), path.end(), v));
return true;
}
return false;
}
path.push_back(v);
state[v] = 1;
forg(gi, g[v]) {
if (dfs(t))
return true;
}
state[v] = -1;
path.pop_back();
return false;
})(i)) {
return path;
}
}
return vi();
}
vi close_path_min(digraph<> &g) {
int n = g.n;
vvi(dis, n);
rep(i, n) dis[i] = dijkstra(g, i, linf);
int mind = linf;
int f = 0, t = 0;
rep(i, n) {
rep(j, n) {
if (i == j)
continue;
if (chmi(mind, dis[i][j] + dis[j][i])) {
f = i;
t = j;
}
}
}
vi path;
auto add = [&](int f, int t) {
int now = f;
while (now != t) {
rep(i, n) {
if (dis[now][i] == 1 && dis[f][i] + dis[i][t] == dis[f][t]) {
path.push_back(i);
now = i;
break;
}
}
}
};
add(f, t);
add(t, f);
return path;
}
// iを含む最短閉路 https://atcoder.jp/contests/abc022/tasks/abc022_c
/*閉路が1つしかない場合、その閉路に含まれる頂点を1としたvectorを返す*/;
template <class T> vi get_close1(digraph<T> &g) {
int n = g.n;
queue<int> q;
vi d = outdegree(g);
vi res(n, 1);
rep(i, n) {
if (d[i] == 0) {
q += i;
res[i] = 0;
}
}
auto rg = rev(g);
while (q.size()) {
auto now = q.front();
q.pop();
forg(gi, rg[now]) {
if (--d[t] == 0) {
q += t;
res[t] = 0;
}
}
}
return res;
}
//@アルゴリズム
template <class T> int krus(undigraph<T> &g) {
int res = 0;
unionfind uf(g.n);
if (sz(g.edges) == 0)
g.set_edges();
int i = 0;
auto E = g.edges;
sort(E);
fora(e, E) {
if (uf.unite(e.f, e.t)) {
res += e.c;
}
}
return res;
}
template <class T> vector<edge_<T>> krus_ed(undigraph<T> &g) {
unionfind uf(g.n);
if (sz(g.edges) == 0)
g.set_edges();
int i = 0;
auto E = g.edges;
sort(E);
vector<edge_<T>> res;
fora(e, E) {
if (uf.unite(e.f, e.t)) {
res.push_back(e);
}
}
return res;
}
template <class T> tree<T> krus_tr(undigraph<T> &g) {
tree<T> res(g.n);
unionfind uf(g.n);
if (sz(g.edges) == 0)
g.set_edges();
int i = 0;
auto E = g.edges;
sort(E);
fora(e, E) {
if (uf.unite(e.f, e.t)) {
res.add(e.f, e.t);
}
}
return res;
}
template <class T> int krus(vector<edge_<T>> &g) {
int n = 0;
fora(e, g) { chma(n, max(e.f, e.t) + 1); };
int res = 0;
unionfind uf(n);
auto E = g;
sort(E);
fora(e, E) {
if (uf.unite(e.f, e.t)) {
res += e.c;
}
}
return res;
}
template <class T> vector<edge_<T>> krus_ed(vector<edge_<T>> &g) {
int n = 0;
fora(e, g) { chma(n, max(e.f, e.t) + 1); };
vector<edge_<T>> res;
unionfind uf(n);
auto E = g;
sort(E);
fora(e, E) {
if (uf.unite(e.f, e.t)) {
res += e;
}
}
return res;
};
template <class T> vi krus_i(vector<edge_<T>> &g) {
int n = 0;
fora(e, g) { chma(n, max(e.f, e.t) + 1); };
vi res;
unionfind uf(n);
auto E = g;
sort(E);
int i = 0;
fora(e, E) {
if (uf.unite(e.f, e.t)) {
res += i;
}
i++;
}
return res;
}
//@実験
digraph<> rang_di(int n, int m, bool zibun = 0, bool taju = 0) {
umapp was;
digraph<> g(n);
was[mp(-1, -2)] = 1;
while (m) {
int f = -1, t = -2;
while (f < 0 || (!taju && was[mp(f, t)])) {
f = rand(0, n - 1);
t = rand(0, n - 1);
if (!zibun && f == t)
f = -1;
}
g.add(f, t);
was[mp(f, t)] = 1;
m--;
}
return g;
}
digraph<> perfect_di(int n, bool zibun = 0) {
digraph<> g(n);
rep(i, n) {
rep(j, n) {
if (!zibun && i == j)
con;
g.add(i, j);
}
}
return g;
}
undigraph<> rang_un(int n, int m, bool zibun = 0, bool taju = 0) {
umapp was;
undigraph<> g(n);
was[mp(-1, -2)] = 1;
while (m) {
int f = -1, t = -2;
while (f < 0 || (!taju && was[mp(min(f, t), max(f, t))])) {
f = rand(0, n - 1);
t = rand(0, n - 1);
if (!zibun && f == t)
f = -1;
}
g.add(f, t);
was[mp(min(f, t), max(f, t))] = 1;
m--;
}
return g;
}
undigraph<> perfect_un(int n, bool zibun = 0) {
undigraph<> g(n);
rep(i, n) {
rep(j, i, n) {
if (!zibun && i == j)
con;
g.add(i, j);
}
}
return g;
}
/*頂点数がkの木を一つ返す サイズが0の木が帰ったら終了*/
tree<int> next_tree(int k) {
assert(2 <= k && k < 11);
static str name;
static ifstream ina;
static int rem;
static vp edges;
static int pk = -1; /*前回見たk*/
if (pk != k) {
if (~pk)
ina.close();
edges.clear();
pk = k;
name = (k == 6) ? "C:\\Users\\kaout\\Desktop\\trees_sizek\\nazeka6.txt"
: "C:\\Users\\kaout\\Desktop\\trees_sizek\\tree_size" +
tos(k) + ".txt";
ina = ifstream(name);
rem = pow(k, k - 2); /*Cayleyの定理*/
rep(i, k) rep(j, i + 1, k) edges.emplace_back(i, j);
pk = k;
}
tree<int> g(k);
if (rem == 0) {
g.resize(0);
return g;
}
int m;
ina >> m;
while (m) {
int lb = lbit(m);
int id = log2(lb);
g.add(edges[id].first, edges[id].second);
m ^= lb;
}
rem--;
return g;
}
undigraph<int> next_undi(int k) {
assert(2 <= k && k < 9);
static str name;
static ifstream ina;
static int rem;
static vp edges;
static vi lims = {-1, -1, 1, 4, 38, 728, 26704, 1866256};
static int pk = -1; /*前回見たk*/
if (pk != k) {
if (~pk)
ina.close();
edges.clear();
pk = k;
name = (k == 6) ? "C:\\Users\\kaout\\Desktop\\undi_sizek\\roku.txt"
: "C:\\Users\\kaout\\Desktop\\undi_sizek\\undi_size" +
tos(k) + ".txt";
ina = ifstream(name);
rem = lims[k];
rep(i, k) rep(j, i + 1, k) edges.emplace_back(i, j);
pk = k;
}
undigraph<int> g(k);
if (rem == 0) {
g.resize(0);
return g;
}
int m;
ina >> m;
while (m) {
int lb = lbit(m);
int id = log2(lb);
g.add(edges[id].first, edges[id].second);
m ^= lb;
}
rem--;
return g;
}
vector<tree<int>> trees(int k) {
vector<tree<int>> res;
while (1) {
tree<int> g = next_tree(k);
if (sz(g) == 0)
break;
res.push_back(g);
}
return res;
}
vector<undigraph<int>> undis(int k) {
vector<undigraph<int>> res;
while (1) {
undigraph<int> g = next_undi(k);
if (sz(g) == 0)
break;
res.push_back(g);
}
return res;
}
template <class T> vector<vector<int>> table(graph<T> &g, int init_value) {
vvi(res, g.n, g.n, init_value);
rep(i, g.n) {
forg(gi, g[i]) { res[i][t] = c; }
}
return res;
}
// type,idが使いたい場合はgraty
/*@formatter:on*/
namespace mint_china { /*@formatter:off*/
ll minv(ll a, ll m) {
ll u = 0, v = 1;
while (a != 0) {
ll t = m / a;
m -= t * a;
swap(a, m);
u -= t * v;
swap(u, v);
}
assert(m == 1);
return u;
}
ll garner(const vector<ll> &x, vector<ll> mods, ll mod = 0) {
mods.emplace_back(mod);
int n = sz(x);
vector<ll> coeff; /* coeff[i]v_i*/
vector<ll> constants;
if (mod) {
coeff.assign(n + 1, 1);
constants.assign(n + 1, 0);
} else {
coeff.assign(n, 1);
constants.assign(n, 0);
}
int nj = n + !!mod;
for (size_t i = 0; i < n; i++) {
ll v = (x[i] - constants[i]) * minv(coeff[i], mods[i]) % mods[i];
if (v < 0)
v += mods[i];
for (size_t j = i + 1; j < nj; j++) {
constants[j] = (constants[j] + coeff[j] * v) % mods[j];
coeff[j] = (coeff[j] * mods[i]) % mods[j];
}
}
return constants.back();
}
bool to_disjoint(vi &vs, vi &ms) {
vector<int> primes;
int maxm = *max_element(ms.begin(), ms.end());
int lim = sqrt(maxm);
vb p(lim + 1);
for (int i = 2; i <= lim; i++) {
if (p[i])
continue;
primes.eb(i);
for (int j = i * i; j <= lim; j += i)
p[j] = true;
}
for (int m : ms) {
for (int p : primes)
while (m % p == 0)
m /= p;
if (m != 1)
primes.emplace_back(m);
}
int n = sz(ms);
for (int p : primes) {
vp ps;
for (int i = 0; i < n; i++) {
if (ms[i] % p == 0) {
int lg = 0;
while (ms[i] % p == 0)
ms[i] /= p, lg++;
ps.eb(lg, i);
}
}
if (ps.size() < 1)
continue;
sort(ps);
int base = vs[ps.back().second];
for (auto d : ps) {
if ((vs[d.second] - base) % p)
return false;
if (d.second == ps.back().second)
for (int i = 0; i < d.first; i++)
ms[d.second] *= p;
}
}
return true;
}
int extGCD(int a, int b, int &x0, int &x1) {
x0 = 1, x1 = 0;
int y0 = 0, y1 = 1;
while (b != 0) {
int q = a / b;
int r = a % b;
int x2 = x0 - q * x1;
int y2 = y0 - q * y1;
a = b;
b = r;
x0 = x1;
x1 = x2;
y0 = y1;
y1 = y2;
}
return a;
}
// 答えを x ≡ r (mod. M) として、{r, M} をリターン, 存在しない場合は {0, -1}
// をリターン
P ChineseRem(const vi &b, const vi &m) {
int r = 0, M = 1;
for (int i = 0; i < (int)b.size(); ++i) {
int p, q;
int d = extGCD(M, m[i], p, q); /* p is inv of M/d (mod. m[i]/d)*/
if ((b[i] - r) % d != 0)
return make_pair(0, -1);
long long tmp = (b[i] - r) / d * p % (m[i] / d);
r += M * tmp;
M *= m[i] / d;
}
return make_pair(mod(r, M), M);
}
int china_m_r_mod_dj(vi &ms, vi &rs, int mod = 0,
int disjoint = -3) { /*中国余剰定理*/
if (mod == 0) {
bool exist_non_zero = false;
for (int i = 0; i < sz(rs); ++i) {
if (rs[i])
exist_non_zero = true;
}
P res = ChineseRem(rs, ms);
if (res.second == -1)
return -1;
else if (exist_non_zero)
return res.first;
else
return res.second;
} else {
if (disjoint != -3 && !to_disjoint(rs, ms))
return -1;
int allzero = 1;
ll ans = 1;
int n = sz(ms);
rep(i, n) {
rs[i] = rs[i] % ms[i];
if (rs[i] != 0)
allzero = 0;
}
if (allzero) {
if (mod)
rep(i, n) ans = ans * ms[i] % mod;
else
rep(i, n) ans = ans * ms[i];
return ans;
}
return garner(rs, ms, mod);
}
}
// 基本chinaを使えばいい 答えが無ければ-
// m r
constexpr int MOD = 1000000007;
constexpr int MOE = 1000000009;
/*@formatter:on*/
struct mint {
int X[2];
mint() { X[0] = X[1] = 0; }
mint(int a) {
if (a < 0)
X[0] = (a % MOD) + MOD, X[1] = (a % MOE) + MOE;
else
X[0] = a % MOD, X[1] = a % MOE;
}
mint &operator+=(mint that) {
X[0] = (X[0] + that.X[0]) % MOD;
X[1] = (X[1] + that.X[1]) % MOE;
return *this;
}
mint &operator-=(mint that) {
X[0] = (X[0] + MOD - that.X[0]) % MOD;
X[1] = (X[1] + MOE - that.X[1]) % MOE;
return *this;
}
mint &operator*=(mint that) {
X[0] = (X[0] * that.X[0]) % MOD;
X[1] = (X[1] * that.X[1]) % MOE;
return *this;
}
mint &operator/=(mint that) { return *this *= that.inverse(); }
mint operator-() {
mint res = *this;
if (X[0])
res.X[0] = MOD - X[0];
if (X[1])
res.X[1] = MOE - X[1];
return res;
}
friend ostream &operator<<(ostream &out, mint m) { return out << (int)m; }
mint inverse() {
mint ret;
{
int a = X[0], b = MOD, u = 1, v = 0;
while (b) {
int t = a / b;
a -= t * b;
u -= t * v;
swap(a, b);
swap(u, v);
}
if (u < 0)
u = (u + MOD);
ret.X[0] = (u);
}
{
int a = X[1], b = MOE, u = 1, v = 0;
while (b) {
int t = a / b;
a -= t * b;
u -= t * v;
swap(a, b);
swap(u, v);
}
if (u < 0)
u = (u + MOE);
ret.X[1] = u;
}
return ret;
}
operator int() const {
vi ms = {MOD, MOE};
vi rs = {X[0], X[1]};
return china_m_r_mod_dj(ms, rs);
}
template <class T> mint &operator+=(T that) { return operator+=((mint)that); }
template <class T> mint &operator-=(T that) { return operator-=((mint)that); }
template <class T> mint &operator*=(T that) { return operator*=((mint)that); }
template <class T> mint &operator/=(T that) { return operator/=((mint)that); }
mint operator+(mint that) { return mint(*this) += that; }
mint operator-(mint that) { return mint(*this) -= that; }
mint operator*(mint that) { return mint(*this) *= that; }
mint operator/(mint that) { return mint(*this) /= that; }
template <class T> mint operator+(T that) {
return mint(*this) += (mint)that;
}
template <class T> mint operator-(T that) {
return mint(*this) -= (mint)that;
}
template <class T> mint operator*(T that) {
return mint(*this) *= (mint)that;
}
template <class T> mint operator/(T that) {
return mint(*this) /= (mint)that;
}
// bool operator==(mint that) const { return x == that.x && y ==
// that.y; }
// //重い
// bool operator==(signed that) const { return x == (mint) that; }
// bool operator!=(mint that) const { return !(x == that); }
// bool operator<(mint that) const { return (int) x < (int) that; }
// bool operator<=(mint that) const { return (int) x <= (int) that; }
// bool operator>(mint that) const { return (int) x > (int) that; }
// bool operator>=(mint that) const { return (int) x >= (int) that; }
};
istream &operator>>(istream &i, mint &a) {
int v;
i >> v;
a = mint(v);
return i;
}
typedef vector<mint> vm;
// vector<mint> fac, finv;
int mint_len = 1400000; // 既に変更されていたら変えない
mint fac[1400000], finv[1400000];
struct setmod {
setmod() {
// if (mint_len >= MOD - 1)mint_len = MOD - 1;
// if (mint_len >= MOE - 1)mint_len = MOE - 1;
// fac = vector<mint>(mint_len + 1);
// finv = vector<mint>(mint_len + 1);
fac[0] = 1;
rep(i, 1, mint_len + 1) { fac[i] = fac[i - 1] * i; }
finv[mint_len] = (mint)1 / fac[mint_len];
rer(i, mint_len, 1) { finv[i - 1] = finv[i] * i; }
}
} setmodv;
mint com(int a, int b) {
if (a < 0)
return 0;
if (b < 0 || b > a)
return 0;
return fac[a] * finv[a - b] * finv[b];
}
mint hom(int a, int b) { return com(a + b - 1, b); }
template <typename T, typename U> mint mpow(const T a, const U b) {
assert(b >= 0);
mint x = a, res = 1;
U p = b;
while (p > 0) {
if (p & 1)
(res *= x);
x *= x;
p >>= 1;
}
return res;
} /*@formatter:on*/
using PM = pair<mint, mint>;
using vm = vector<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__)
} // namespace mint_china
//!!!答えが負になってはいけない
using mint = mint_china::mint;
void solve() {
in(N);
tree<> g(2 * k5);
g.ing(N, N - 1);
in(M);
dna2d(U, V, M);
vi in(M);
fora(i, u, v, U, V) { /*@formatter:off*/
fora_init(i, u, v, U, V); /*@formatter:on*/
auto D = g.pathe(u, v);
in[i] = bit(D);
}
deb2(in);
mint res = 0;
rep(mas, bit(M)) {
int use = 0;
rep(i, M) {
if (bget(mas, i))
use |= in[i];
}
deb2(mas);
deb2(use);
int ziyu = N - 1;
ziyu -= bcou(use);
if ((bcou(mas) % 2) == 0) {
res += powi(2, ziyu);
} else {
res -= powi(2, ziyu);
}
}
out(res);
}
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;
}
if (sz(message)) {
cerr << "****************************" << endl;
cerr << message << endl;
cerr << "****************************" << 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
/*@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
string message;
// 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
// 区間削除は出来ない
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
#define endl '\n'
// 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 lam1(ret) [&](auto &v) { return ret; }
#define lam2(v, ret) [&](auto &v) { return ret; }
#define lam(...) over2(__VA_ARGS__, lam2, lam1)(__VA_ARGS__)
#define lamr(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(vector<vector<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(vector<vector<vector<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(vector<vector<vector<vector<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(vector<vector<vector<vector<vector<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(vector<vector<vector<vector<vector<vector<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_3(fora_i, a, A) auto &&a = A[fora_i];
#define fora_init_4(a, b, A, B) \
auto &&a = A[fora_i]; \
auto &&b = B[fora_i];
#define fora_init_5(fora_i, 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_7(fora_i, 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_9(fora_i, 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(...) \
over9(__VA_ARGS__, fora_init_9, fora_init_8, fora_init_7, fora_init_6, \
fora_init_5, fora_init_4, fora_init_3, fora_init_2)(__VA_ARGS__)
#define forr_init_2(a, A) auto &&a = A[forr_i];
#define forr_init_3(forr_i, 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_5(forr_i, 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_7(forr_i, 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_9(forr_i, 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(...) \
over9(__VA_ARGS__, forr_init_9, forr_init_8, forr_init_7, forr_init_6, \
forr_init_5, forr_init_4, forr_init_3, 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_3(fora_i, a, A) rep(fora_i, sz(A))
#define fora_4(a, b, A, B) rep(fora_i, sz(A))
#define fora_5(fora_i, 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_7(fora_i, 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 fora_9(fora_i, 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_3(forr_i, a, A) rer(forr_i, sz(A) - 1)
#define forr_4(a, b, A, B) rer(forr_i, sz(A) - 1)
#define forr_5(forr_i, 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_7(forr_i, 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 forr_9(forr_i, 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(...) \
over9(__VA_ARGS__, forr_9, fora_8, fora_7, fora_6, fora_5, fora_4, fora_3, \
fora_2)(__VA_ARGS__)
#define forr(...) \
over9(__VA_ARGS__, forr_9, forr_8, forr_7, forr_6, forr_5, forr_4, forr_3, \
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, lamr(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, lamr(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, lamr(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, lamr(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, lamr(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, lamr(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, lamr(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, lamr(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];
}
template <class T> void na(set<T> &a, ll n) { rep(i, n) a.insert(in()); }
#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> void nad(set<T> &a, ll n) { rep(i, n) a.insert(in() - 1); }
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];
}
template <class T, class U> void na2(set<T> &a, set<U> &b, ll n) {
rep(i, n) {
a.insert(in());
b.insert(in());
}
}
#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 deb_2(x, ...) deb1(x) << ", " << deb1(__VA_ARGS__)
#define deb_3(x, ...) deb1(x) << ", " << deb_2(__VA_ARGS__)
#define deb_4(x, ...) deb1(x) << ", " << deb_3(__VA_ARGS__)
#define deb5(x, ...) deb1(x) << ", " << deb_4(__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, deb_4, \
deb_3, deb_2, deb1)(__VA_ARGS__) \
<< endl
#define base_keta 8
void print_n_base(int x, int base) { cerr << bitset<base_keta>(x) << endl; }
template <class T> void print_n_base(vector<T> X, int base) {
cerr << endl;
for (auto &&x : X) {
print_n_base(x, base);
}
cerr << endl;
}
// n進数
#define deb2(x) \
cerr << debugName(x) << " = "; \
print_n_base(x, 2);
#define deb3(x) \
cerr << debugName(x) << " = "; \
print_n_base(x, 3);
#define deb4(x) \
cerr << debugName(x) << " = "; \
print_n_base(x, 4);
#else
#define deb(...) ;
#define deb2(...) ;
#define deb3(...) ;
#define deb4(...) ;
#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);
}
}
}
ll rand(ll max) { return rand(0, max); }
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};
// 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 getr(ll a, ll keta) { return (a / (ll)pow(10, keta)) % 10; }
// 上から何桁目か
ll getl(ll a, ll ket) {
int sketa = keta(a);
return getr(a, sketa - 1 - ket);
}
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;
}
#ifdef _DEBUG
auto pow(int a, int k) {
static bool was = 1;
if (was) {
message += "if integer use *powi* it's very fast\n";
}
was = 0;
return std::pow(a, k);
}
auto pow(signed a, int k) { return pow((int)a, k); }
auto pow(signed a, signed k) { return pow((int)a, (int)k); }
#endif
// 整数型のpow
int powi(int a, int k) {
if (a == 2)
return 1ll << k;
int res = 1;
int x = a;
while (k) {
res *= (k & 1) * x;
x *= x;
k >>= 1;
}
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 bit(vi &a) {
int m = 0;
for (auto &&v : a)
m |= bit(v);
return m;
}
//{1, 1, 0} -> 011
// bitsetに置き換える感覚 i が立っていたら i bit目を立てる
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;
#undef getid
/*@formatter:off*/
#define forg(gi, ve) \
for (ll gi = 0, forglim = ve.size(), f, t, c; \
gi < forglim && (f = ve[gi].f, t = ve[gi].t, c = ve[gi].c, true); ++gi)
#define fort(gi, ve) \
for (ll gi = 0, f, t, c, wasp = (p != -1 && ve[gi].t == p) ? 1 : 0; \
(wasp = wasp ? 1 \
: (p != -1 && ve[gi].t == p) ? 1 \
: 0, \
gi + wasp < ve.size()) && \
(tie(f, t, c) = \
wasp ? make_tuple(ve[gi + 1].f, ve[gi + 1].t, ve[gi + 1].c) \
: make_tuple(ve[gi].f, ve[gi].t, ve[gi].c), \
true); \
++gi)
#define fore(gi, ve) \
for (ll gi = 0, forglim = ve.size(), f, t, c; \
gi < forglim && (f = ve[gi].f, t = ve[gi].t, c = ve[gi].c, true); ++gi)
template <class T> struct edge_ {
int f, t;
T c;
edge_(int f, int t, T c = 1) : f(f), t(t), c(c) {}
bool operator<(const edge_ &b) const { return c < b.c; }
bool operator>(const edge_ &b) const { return c > b.c; }
};
template <class T> ostream &operator<<(ostream &os, edge_<T> &e) {
os << e.f << " " << e.t << " " << e.c;
return os;
}
template <typename T> class graph {
public:
vector<vector<edge_<T>>> g;
vector<edge_<T>> edges;
int n;
explicit graph(int n) : n(n) { g.resize(n); }
void clear() { g.clear(), edges.clear(); }
void resize(int n) {
this->n = n;
g.resize(n);
}
int size() { return n; }
vector<edge_<T>> &operator[](int i) { return g[i]; }
virtual void add(int f, int t, T c) = 0;
virtual void set_edges() = 0;
};
template <typename T = ll> class digraph : public graph<T> {
public:
using graph<T>::g;
using graph<T>::n;
using graph<T>::edges;
explicit digraph(int n) : graph<T>(n) {}
void add(int f, int t, T c = 1) {
if (!(0 <= f && f < n && 0 <= t && t < n)) {
debugline("digraph add");
deb(f, t, c);
ole();
}
g[f].emplace_back(f, t, c);
edges.emplace_back(f, t, c); // edgesを使わないなら消せる
}
void ing(int n, int m, int minus = 1) {
this->resize(n);
rep(i, m) {
int f, t;
cin >> f >> t;
f -= minus;
t -= minus;
add(f, t);
}
}
void ingc(int n, int m, int minus = 1) {
this->resize(n);
rep(i, m) {
int f, t, c;
cin >> f >> t >> c;
f -= minus;
t -= minus;
add(f, t, c);
}
}
void set_edges() override {
if (sz(edges))
return;
rep(i, n) fora(e, g[i]) edges.push_back(e);
}
};
template <class T = int> class undigraph : public graph<T> {
public:
using graph<T>::g;
using graph<T>::n;
using graph<T>::edges;
explicit undigraph(int n) : graph<T>(n) {}
// f < t
void add(int f, int t, T c = 1) {
if (!(0 <= f && f < n && 0 <= t && t < n)) {
debugline("undigraph add");
deb(f, t, c);
ole();
}
g[f].emplace_back(f, t, c);
g[t].emplace_back(t, f, c);
edges.emplace_back(f, t, c); // edgesを使わないなら消せる
edges.emplace_back(t, f, c);
}
void add(edge_<T> &e) {
int f = e.f, t = e.t;
T c = e.c;
add(f, t, c);
}
void ing(int n, int m, int minus = 1) {
this->resize(n);
rep(i, m) {
int f, t;
cin >> f >> t;
f -= minus;
t -= minus;
add(f, t);
}
}
void ingc(int n, int m, int minus = 1) {
this->resize(n);
rep(i, m) {
int f, t, c;
cin >> f >> t >> c;
f -= minus;
t -= minus;
add(f, t, c);
}
}
void set_edges() override {
if (sz(edges))
return;
rep(i, n) fora(e, g[i]) edges.push_back(e);
}
};
#define dijkstra_path dis_path
template <class T>
vi dis_path(digraph<T> &g, vector<T> &dis, int s, int t, int init_value) {
assert(dis[t] != init_value);
auto rg = rev(g);
int now = t;
vi path;
path.push_back(now);
T cost = 0;
while (now != s) {
rep(gi, sz(rg[now])) {
int m = rg[now][gi].t;
if (dis[m] == init_value)
continue;
if (dis[m] + rg[now][gi].c + cost == dis[t]) {
cost += rg[now][gi].c;
now = m;
break;
}
}
path.push_back(now);
}
rev(path);
return path;
}
template <class T>
vi dis_path(undigraph<T> &g, vector<T> &dis, int s, int t, int init_value) {
assert(dis[t] != init_value);
int now = t;
vi path;
path.push_back(now);
T cost = 0;
while (now != s) {
rep(gi, sz(g[now])) {
int m = g[now][gi].t;
if (dis[m] == init_value)
continue;
if (dis[m] + g[now][gi].c + cost == dis[t]) {
cost += g[now][gi].c;
now = m;
break;
}
}
path.push_back(now);
}
rev(path);
return path;
}
template <class T>
vi dis_path(digraph<T> &g, vector<vector<T>> &dis, int s, int t,
int init_value) {
return dis_path(g, dis[s], s, t, init_value);
}
template <class T>
vi dis_path(undigraph<T> &g, vector<vector<T>> &dis, int s, int t,
int init_value) {
return dis_path(g, dis[s], s, t, init_value);
}
// O(N^2)
template <class T> vector<T> dijkstra_mitu(graph<T> &g, int s, int init_value) {
if (!(0 <= s && s < g.n)) {
debugline("dijkstra_mitu");
deb(s, g.n);
ole();
}
int n = g.n;
int initValue = MAX(int);
vector<T> dis(n, initValue);
dis[s] = 0;
vb used(n);
while (true) {
int v = -1;
rep(u, n) {
if (!used[u] && (v == -1 || dis[u] < dis[v]))
v = u;
}
if (v == -1)
break;
if (dis[v] == initValue)
break;
used[v] = true;
rep(u, sz(g[v])) {
auto e = g[v][u];
dis[e.t] = min(dis[e.t], dis[v] + e.c);
}
} /*基本、たどり着かないなら-1*/
for (auto &&d : dis) {
if (d == initValue) {
d = init_value;
}
}
return dis;
}
template <typename T> struct radixheap {
vector<pair<u64, T>> v[65];
u64 size, last;
radixheap() : size(0), last(0) {}
bool empty() const { return size == 0; }
int getbit(int a) { return a ? 64 - __builtin_clzll(a) : 0; }
void push(u64 key, const T &value) {
++size;
v[getbit(key ^ last)].emplace_back(key, value);
}
pair<u64, T> pop() {
if (v[0].empty()) {
int idx = 1;
while (v[idx].empty())
++idx;
last = min_element(begin(v[idx]), end(v[idx]))->first;
for (auto &p : v[idx])
v[getbit(p.first ^ last)].emplace_back(p);
v[idx].clear();
}
--size;
auto ret = v[0].back();
v[0].pop_back();
return ret;
}
};
/*radix_heap こっちの方が早い*/
// O((N+M) log N)
vi dijkstra(graph<int> &g, int s, int init_value) {
if (!(0 <= s && s < g.n)) {
debugline("dijkstra");
deb(s, g.n);
ole();
} /*O((N+M) log N) vs O(N^2)*/
if ((g.n + sz(g.edges)) * log2(N) > g.n * g.n) {
return dijkstra_mitu(g, s, init_value);
}
int initValue = MAX(int);
vi dis(g.n, initValue);
radixheap<int> q;
dis[s] = 0;
q.push(0, s);
while (!q.empty()) {
int nowc, i;
tie(nowc, i) = q.pop();
if (dis[i] != nowc)
continue;
for (auto &&e : g.g[i]) {
int to = e.t;
int c = nowc + e.c;
if (dis[to] > c) {
dis[to] = c;
q.push(dis[to], to);
}
}
} /*基本、たどり着かないなら-1*/
for (auto &&d : dis)
if (d == initValue)
d = init_value;
return dis;
}
template <class T>
vector<T> dijkstra_normal(graph<T> &g, int s, int init_value) {
if (!(0 <= s && s < g.n)) {
debugline("dijkstra");
deb(s, g.n);
ole();
}
if ((g.n + sz(g.edges)) * 20 > g.n * g.n) {
return dijkstra_mitu(g, s, init_value);
}
T initValue = MAX(T);
vector<T> dis(g.n, initValue);
priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> q;
dis[s] = 0;
q.emplace(0, s);
while (q.size()) {
T nowc = q.top().fi;
int i = q.top().se;
q.pop();
if (dis[i] != nowc)
continue;
for (auto &&e : g.g[i]) {
int to = e.t;
T c = nowc + e.c;
if (dis[to] > c) {
dis[to] = c;
q.emplace(dis[to], to);
}
}
} /*基本、たどり着かないなら-1*/
for (auto &&d : dis)
if (d == initValue)
d = init_value;
return dis;
}
template <class T> vector<T> dijkstra_01(graph<T> &g, int s, int init_value) {
int N = g.n;
vi dis(N, linf);
dis[s] = 0;
deque<int> q;
q.push_back(s);
vb was(N);
while (!q.empty()) {
int f = q.front();
q.pop_front();
if (was[f])
continue;
was[f] = true;
fora(e, g[f]) {
if (dis[e.t] > dis[f] + e.c) {
if (e.c) {
dis[e.t] = dis[f] + 1;
q.push_back(e.t);
} else {
dis[e.t] = dis[f];
q.push_front(e.t);
}
}
}
}
rep(i, N) if (dis[i] == linf) dis[i] = init_value;
return dis;
}
// dijkstra_cou<mint> : 数える型で書く return vp(dis,cou)
template <class COU, class T = int>
auto dijkstra_cou(const graph<T> &g, int s, int init_value) {
if (!(0 <= s && s < g.n)) {
debugline("dijkstra");
deb(s, g.n);
ole();
}
err("count by type COU ");
err("int or mint");
T initValue = MAX(T);
vector<T> dis(g.n, initValue);
vector<COU> cou(g.n);
cou[s] = 1;
priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> q;
dis[s] = 0;
q.emplace(0, s);
while (q.size()) {
T nowc = q.top().fi;
int i = q.top().se;
q.pop();
if (dis[i] != nowc)
continue;
for (auto &&e : g.g[i]) {
int to = e.t;
T c = nowc + e.c;
if (dis[to] > c) {
dis[to] = c;
cou[to] = cou[e.f];
q.emplace(dis[to], to);
} else if (dis[to] == c) {
cou[to] += cou[e.f];
}
}
} /*基本、たどり着かないなら-1*/
for (auto &&d : dis)
if (d == initValue)
d = init_value;
return vtop(dis, cou);
}
// 密グラフの時、warshallに投げる
template <class T>
vector<vector<T>> dijkstra_all(const graph<T> &g, int init_value) {
int n = g.n;
assert(n < 1e4);
if (n * n < (n + sz(g.edges)) * 14) { /*O(N^3) vs O(N (N+M)log N)*/
return warshall(g, init_value);
}
vector<vector<T>> dis(n);
rep(i, n) { dis[i] = dijkstra(g, i, init_value); }
return dis;
}
// コストを無限に減らせる := -linf
// たどり着けない := linf
template <class T> vector<T> bell(graph<T> &g, int s) {
if (g.n >= 1e4) {
cout << "bell size too big" << endl;
exit(0);
}
vector<T> res(g.n, linf);
res[s] = 0;
vb can(g.n); /*頂点から行けない頂点を持つ、辺を消しておく */
fix([&](auto ds, int p, int i) -> void {
if (can[i])
return;
can[i] = true;
forg(gi, g[i]) if (t != p) ds(i, t);
})(-1, 0);
vector<edge_<T>> es;
fora(e, g.edges) {
if (can[e.f])
es += e;
}
rep(i, g.n) {
bool upd = false;
fora(e, es) {
if (res[e.f] != linf && res[e.t] > res[e.f] + e.c) {
upd = true;
res[e.t] = res[e.f] + e.c;
}
}
if (!upd)
break;
}
rep(i, g.n * 2) {
bool upd = 0;
fora(e, g.edges) {
if (res[e.f] != linf && res[e.t] != -linf && res[e.t] > res[e.f] + e.c) {
upd = 1;
res[e.t] = -linf;
}
}
if (!upd)
break;
}
return res;
}
// コストを無限に増やせる := linf
// たどり着けない := -linf
template <class T> vector<T> bell_far(graph<T> &g, int s) {
if (g.n >= 1e4) {
cout << "bell_far size too big" << endl;
exit(0);
}
vector<T> res(g.n, linf);
res[s] = 0;
vb can(g.n); /*頂点から行けない頂点を持つ、辺を消しておく*/
fix([&](auto ds, int p, int i) -> void {
if (can[i])
return;
can[i] = true;
forg(gi, g[i]) if (t != p) ds(i, t);
})(-1, 0);
vector<edge_<T>> es;
fora(e, g.edges) {
if (can[e.f])
es += e;
}
rep(i, g.n) {
bool upd = false;
fora(e, es) {
if (res[e.f] != linf && res[e.t] > res[e.f] - e.c) { /*-c*/
upd = true;
res[e.t] = res[e.f] - e.c; /*-c*/
}
}
if (!upd)
break;
}
rep(i, g.n * 2) {
bool upd = 0;
fora(e, g.edges) {
if (res[e.f] != linf && res[e.t] != -linf &&
res[e.t] > res[e.f] - e.c) { /*-c*/
upd = 1;
res[e.t] = -linf;
}
}
if (!upd)
break;
}
rep(i, g.n) res[i] *= -1;
return res;
}
template <class T> vector<vector<T>> warshall(graph<T> &g, int init_value) {
int n = g.n;
assert(n < 1e4);
vector<vector<T>> dis(n, vector<T>(n, linf));
rep(i, n) fora(e, g.g[i]) {
if (dis[e.f][e.t] > e.c) {
dis[e.f][e.t] = e.c;
}
}
rep(i, n) dis[i][i] = 0;
rep(k, n) rep(i, n) rep(j, n) {
if (dis[i][j] > dis[i][k] + dis[k][j]) {
dis[i][j] = dis[i][k] + dis[k][j];
}
}
rep(i, n) rep(j, n) if (dis[i][j] == linf) dis[i][j] = init_value;
return dis;
}
template <class T> class MinOp {
public:
T operator()(T a, T b) { return min(a, b); }
};
template <typename OpFunc> struct SparseTable {
OpFunc op;
signed size;
vector<signed> lg;
vector<vector<pair<signed, signed>>> table;
void init(vector<pair<signed, signed>> &array, OpFunc opfunc) {
signed n = array.size();
op = opfunc;
lg.assign(n + 1, 0);
for (signed i = 1; i <= n; i++) {
lg[i] = 31 - __builtin_clz(i);
}
table.assign(lg[n] + 1, array);
for (signed i = 1; i <= lg[n]; i++) {
for (signed j = 0; j < n; j++) {
if (j + (1 << (i - 1)) < n) {
table[i][j] = op(table[i - 1][j], table[i - 1][j + (1 << (i - 1))]);
} else {
table[i][j] = table[i - 1][j];
}
}
}
}
pair<signed, signed> query(signed l, signed r) {
assert(l < r);
return op(table[lg[r - l]][l], table[lg[r - l]][r - (1 << lg[r - l])]);
}
};
struct PMORMQ {
vector<signed> a;
SparseTable<MinOp<pair<signed, signed>>> sparse_table;
vector<vector<vector<signed>>> lookup_table;
vector<signed> block_type;
signed block_size, n_block;
void init(vector<signed> &array) {
a = array;
signed n = a.size();
block_size = std::max(1, (31 - __builtin_clz(n)) / 2);
while (n % block_size != 0) {
a.push_back(a.back() + 1);
n++;
}
n_block = n / block_size;
vector<pair<signed, signed>> b(n_block, make_pair(INT_MAX, INT_MAX));
for (signed i = 0; i < n; i++) {
b[i / block_size] = min(b[i / block_size], make_pair(a[i], i));
}
sparse_table.init(b, MinOp<pair<signed, signed>>());
block_type.assign(n_block, 0);
for (signed i = 0; i < n_block; i++) {
signed cur = 0;
for (signed j = 0; j < block_size - 1; j++) {
signed ind = i * block_size + j;
if (a[ind] < a[ind + 1]) {
cur |= 1 << j;
}
}
block_type[i] = cur;
}
lookup_table.assign(
1 << (block_size - 1),
vector<vector<signed>>(block_size, vector<signed>(block_size + 1)));
for (signed i = 0; i < (1 << (block_size - 1)); i++) {
for (signed j = 0; j < block_size; j++) {
signed res = 0;
signed cur = 0;
signed pos = j;
for (signed k = j + 1; k <= block_size; k++) {
lookup_table[i][j][k] = pos;
if (i & (1 << (k - 1))) {
cur++;
} else {
cur--;
}
if (res > cur) {
pos = k;
res = cur;
}
}
}
}
}
signed query(signed l, signed r) {
assert(l < r);
signed lb = l / block_size;
signed rb = r / block_size;
if (lb == rb) {
return lb * block_size +
lookup_table[block_type[lb]][l % block_size][r % block_size];
}
signed pl = lb * block_size +
lookup_table[block_type[lb]][l % block_size][block_size];
signed pr =
rb * block_size + lookup_table[block_type[rb]][0][r % block_size];
signed pos = pl;
if (r % block_size > 0 && a[pl] > a[pr]) {
pos = pr;
}
if (lb + 1 == rb) {
return pos;
}
signed spv = sparse_table.query(lb + 1, rb).second;
if (a[pos] > a[spv]) {
return spv;
}
return pos;
}
};
template <class T = int> class tree : public undigraph<T> {
PMORMQ rmq;
int cnt;
vector<signed> id, in;
bool never = true;
bool never_hld = true;
void dfs(int x, int p, int d, int dis = 0) {
id[cnt] = x;
par_[x] = p;
rmq_dep.push_back(d);
disv[x] = dis;
in[x] = cnt++;
forg(gi, g[x]) {
if (t == p) {
continue;
}
dfs(t, x, d + 1, dis + c);
id[cnt] = x;
rmq_dep.push_back(d);
cnt++;
}
}
void precalc() {
never = false;
cnt = 0;
rmq_dep.clear();
disv.assign(n, 0);
in.assign(n, -1);
id.assign(2 * n - 1, -1);
par_.assign(n, -1);
dfs(root, -1, 0);
rmq.init(rmq_dep);
#ifdef _DEBUG
if (n >= 100)
return;
cerr << "---tree---" << endl;
rep(i, n) {
if (!(i == root || sz(g[i]) > 1))
continue;
cerr << i << " -> ";
vi ts;
forg(gi, g[i]) {
if (t != par_[i])
ts.push_back(t);
}
rep(i, sz(ts) - 1) cerr << ts[i] << ", ";
if (sz(ts))
cerr << ts.back() << endl;
}
cerr << endl;
#endif
}
int pos;
void hld_build() {
never_hld = false;
if (never)
precalc();
g.resize(n);
vid.resize(n, -1);
head.resize(n);
heavy.resize(n, -1);
depth.resize(n);
inv.resize(n);
subl.resize(n);
subr.resize(n);
dfs(root, -1);
t = 0;
dfs_hld(root);
#ifdef _DEBUG
if (n >= 100)
return;
cerr << "---hld_index---" << endl;
vi inds;
rep(i, n) if (sz(g[i])) inds.push_back(i);
rep(i, sz(inds)) {
str s = tos(bel(inds[i]));
cerr << std::right << std::setw(sz(s) + (i ? 1 : 0)) << inds[i];
}
cerr << endl;
rep(i, sz(inds)) { cerr << bel(inds[i]) << " "; }
cerr << endl << endl;
cerr << "---hld_edge_index---" << endl;
fora(e, edges) {
if (e.f <= e.t)
cerr << e.f << "-" << e.t << " " << bel(e) << endl;
}
cerr << endl << endl;
cerr << "!!query!! edge or not edge carefull!!" << endl;
#endif
}
int dfs(int curr, int prev) {
int sub = 1, max_sub = 0;
heavy[curr] = -1;
forg(i, g[curr]) {
int next = t;
if (next != prev) {
depth[next] = depth[curr] + 1;
int sub_next = dfs(next, curr);
sub += sub_next;
if (max_sub < sub_next)
max_sub = sub_next, heavy[curr] = next;
}
}
return sub;
}
int t = 0;
#ifndef __CLION_IDE__
void dfs_hld(int v = 0) {
vid[v] = subl[v] = t;
t++;
inv[subl[v]] = v;
if (0 <= heavy[v]) {
head[heavy[v]] = head[v];
dfs_hld(heavy[v]);
}
forg(i, g[v]) if (depth[v] < depth[t]) if (t != heavy[v]) {
head[t] = t;
dfs_hld(t);
}
subr[v] = t;
}
#endif //__CLION_IDE__
vector<signed> rmq_dep;
vi par_, depth, disv;
vi childs;
vi par_id_; // 親への辺のidを持つ
vvi(ids_); // 隣接で辺のidを持つ
public:
using undigraph<T>::g;
using undigraph<T>::n;
using undigraph<T>::edges;
int root;
//(f,t)と(t,f)は同じidを持ち、[0,n-1]でadd順に割り振られる
// 部分木の [左端、右端) index
// 部分木の辺に加算する場合
// add(subl[i],subr[i],x)
// add(sub[i],sub[i+1],-x)
vector<int> vid, head, heavy, inv, subl, subr;
tree(int n_, int root = 0) : undigraph<T>(n_), root(root) { n = n_; }
void change_root(int roo) {
root = roo;
precalc();
}
int lca(int a, int b) {
if (never)
precalc();
int x = in[a];
int y = in[b];
if (x > y) {
swap(x, y);
}
int pos = rmq.query(x, y + 1);
return id[pos];
}
int dis(int a, int b) {
if (never)
precalc();
int x = in[a];
int y = in[b];
if (x > y) {
swap(x, y);
}
int pos = rmq.query(x, y + 1);
int p = id[pos];
return disv[a] + disv[b] - disv[p] * 2;
}
int dep(int a) {
if (never)
precalc();
return disv[a];
}
int par(int a) {
if (never)
precalc();
return par_[a];
}
bool isleaf(int i) {
if (never)
precalc();
return (sz(g[i]) == 1 && i != root);
}
vi child(int r) {
vi res;
res.push_back(r);
queue<int> q;
q.push(r);
while (!q.empty()) {
int i = q.front();
res.push_back(i);
q.pop();
forg(gi, g[i]) {
if (t != par(i))
q.push(t);
}
}
return res;
}
vb child_ex(int r) {
vb res(n);
res[r] = true;
queue<int> q;
q.push(r);
while (!q.empty()) {
int i = q.front();
res[i] = true;
q.pop();
forg(gi, g[i]) {
if (t != par(i))
q.push(t);
}
}
return res;
}
int dfs_count_subtree(int p, int i) {
childs[i] = 1;
fort(gi, g[i]) { childs[i] += dfs_count_subtree(i, t); }
return childs[i];
}
#define count_child count_subtree
int count_subtree(int f) {
if (sz(childs) == 0) {
if (never)
precalc();
childs.resize(n);
dfs_count_subtree(-1, root);
}
return childs[f];
}
// fからtの辺を切った時のtの大きさ
int count_subtree(int f, int t) {
if (par(f) == t) {
return n - count_subtree(f);
} else {
return count_subtree(t);
}
}
vi path(int a, int b) {
vi res;
for_each_l(a, b, [&](int i) { res.push_back(i); });
return res;
}
int par_id(int i) {
if (sz(par_id_) == 0) {
pathe(0, 0);
}
return par_id_[i];
}
int ids(int f, int i) {
if (sz(ids_) == 0) {
pathe(0, 0);
}
return ids_[f][i];
}
edge_<T> edge(int id) { return edges[id << 1]; }
// pathに含まれる辺のid達を返す
vi pathe(int a, int b) {
#ifdef _DEBUG
static bool was = 0;
if (!was)
message += "can't edges[id]. must edge(id)\n";
was = 1;
#endif
if (sz(par_id_) == 0) {
ids_.resize(n);
par_id_.resize(n);
rep(i, 0, sz(edges), 2) {
ids_[edges[i].f].emplace_back(i >> 1);
ids_[edges[i].t].emplace_back(i >> 1);
}
if (never)
precalc(); /*par_を呼ぶため*/
rep(i, n) {
int pa = par_[i];
forg(gi, g[i]) {
if (t == pa) {
par_id_[i] = ids_[i][gi];
}
}
}
}
int u = lca(a, b);
vi res;
if (a != u) {
do {
res.emplace_back(par_id_[a]);
a = par_[a];
} while (a != u);
}
vi rev;
if (b != u) {
do {
rev.emplace_back(par_id_[b]);
b = par_[b];
} while (b != u);
}
rer(i, sz(rev) - 1) { res.emplace_back(rev[i]); }
return res;
}
/*O(N) hldを使わず木を普通にたどる liteの意*/
void for_each_l(int u, int v, function<void(int)> fnode) {
int r = lca(u, v);
while (u != r) {
fnode(u);
u = par_[u];
}
fnode(r);
vi a;
while (v != r) {
a.push_back(v);
v = par_[v];
}
while (sz(a)) {
fnode(a.back());
a.pop_back();
}
}
void for_each_edge_l(int u, int v, function<void(edge_<int> &)> fedge) {
int r = lca(u, v);
while (u != r) {
forg(gi, g[u]) {
if (t == par_[u]) {
fedge(g[u][gi]);
u = par_[u];
break;
}
}
}
vector<edge_<int>> qs;
while (v != r) {
forg(gi, g[v]) {
if (t == par_[v]) {
qs.push_back(g[v][gi]);
v = par_[v];
break;
}
}
}
while (sz(qs)) {
fedge(qs.back());
qs.pop_back();
}
}
// Fは半開 (u,v)は木の頂点
// 中ではhldの頂点を見るため、seg木のupdateはhldのindexで行なう
void for_each_ /*[l,r)*/ (int u, int v, const function<void(int, int)> &f) {
if (never_hld)
hld_build();
while (1) {
if (vid[u] > vid[v])
swap(u, v);
int l = max(vid[head[v]], vid[u]);
int r = vid[v] + 1;
f(l, r);
if (head[u] != head[v])
v = par_[head[v]];
else
break;
}
}
void for_each_edge /*[l,r) O(log(N)) 辺を頂点として扱っている
上と同じ感じで使える*/
(int u, int v, const function<void(int, int)> &f) {
if (never_hld)
hld_build();
while (1) {
if (vid[u] > vid[v])
swap(u, v);
if (head[u] != head[v]) {
int l = vid[head[v]];
int r = vid[v] + 1;
f(l, r);
v = par_[head[v]];
} else {
if (u != v) {
int l = vid[u] + 1;
int r = vid[v] + 1;
f(l, r);
}
break;
}
}
}
int bel(int v) { /*hld内での頂点番号を返す*/
if (never_hld)
hld_build();
return vid[v];
}
// 下の頂点に辺のクエリを持たせている
int bel(
int f,
int t) { /*辺のクエリを扱うときどの頂点に持たせればいいか(vidを返すのでそのままupd出来る)*/
if (never_hld)
hld_build();
return depth[f] > depth[t] ? vid[f] : vid[t];
}
int bel(
edge_<T> &
e) { /*辺のクエリを扱うときどの頂点に持たせればいいか(vidを返すのでそのままupd出来る)*/
if (never_hld)
hld_build();
return depth[e.f] > depth[e.t] ? vid[e.f] : vid[e.t];
}
template <class... U> int operator()(U... args) { return bel(args...); }
// path l -> r += v
template <class S> void seg_add(S &seg, int lhei, int rhei, int v) {
for_each_(lhei, rhei, [&](int l, int r) { seg.add(l, r, v); });
}
template <class S> void seg_update(S &seg, int lhei, int rhei, int v) {
for_each_(lhei, rhei, [&](int l, int r) { seg.update(l, r, v); });
}
template <class S> T seg_get(S &seg, int lhei, int rhei) {
T res = seg.e;
for_each_(lhei, rhei,
[&](int l, int r) { res = seg.f(res, seg.get(l, r)); });
return res;
}
template <class S> void seg_add_edge(S &seg, int lhei, int rhei, int v) {
for_each_edge(lhei, rhei, [&](int l, int r) { seg.add(l, r, v); });
}
template <class S> void seg_update_edge(S &seg, int lhei, int rhei, int v) {
for_each_edge(lhei, rhei, [&](int l, int r) { seg.update(l, r, v); });
}
template <class S> T seg_get_edge(S &seg, int lhei, int rhei) {
T res = seg.e;
for_each_edge(lhei, rhei,
[&](int l, int r) { res = seg.f(res, seg.get(l, r)); });
return res;
}
// 単体 edgeは上で処理できる
template <class S> void seg_add(S &seg, int i, int v) { seg.add(bel(i), v); }
template <class S> void seg_update(S &seg, int i, int v) {
seg.update(bel(i), v);
}
template <class S> T seg_get(S &seg, int i) { return seg[i]; }
template <class S> void seg_del(S &seg, int i) { seg.del(bel(i)); }
// 部分木iに対するクエリ
template <class S> void seg_add_sub(S &seg, int i, int v) {
if (never_hld)
hld_build();
seg.add(subl[i], subr[i], v);
}
template <class S> void seg_update_sub(S &seg, int i, int v) {
if (never_hld)
hld_build();
seg.update(subl[i], subr[i], v);
}
template <class S> T seg_get_sub(S &seg, int i, int v) {
if (never_hld)
hld_build();
return seg.get(subl[i], subr[i], v);
}
template <class S> void seg_add_sub_edge(S &seg, int i, int v) {
if (never_hld)
hld_build(); /*iの上の辺が数えられてしまうため、i+1から*/
seg.add(subl[i] + 1, subr[i], v);
}
template <class S> void seg_update_sub_edge(S &seg, int i, int v) {
if (never_hld)
hld_build(); /*iの上の辺が数えられてしまうため、i+1から*/
seg.update(subl[i] + 1, subr[i], v);
}
template <class S> T seg_get_sub_edge(S &seg, int i, int v) {
if (never_hld)
hld_build(); /*iの上の辺が数えられてしまうため、i+1から*/
return seg.get(subl[i] + 1, subr[i], v);
}
};
// 辺が多いのでedgesを持たない
// cost oo, ox, xo, xx 渡す
template <class T = int> class grid_k6 : public undigraph<T> {
vi costs;
public:
using undigraph<T>::g;
using undigraph<T>::n;
using undigraph<T>::edges;
int H, W;
vector<vector<char>> ba;
char wall;
void add(int f, int t, T c = 1) {
if (!(0 <= f && f < n && 0 <= t && t < n)) {
debugline("grid_k6 add");
deb(f, t, c);
ole();
}
g[f].emplace_back(f, t, c);
g[t].emplace_back(t, f, c);
// undigraphと違い、edgesを持たない
}
int getid(int h, int w) {
assert(ins(h, w, H, W));
return W * h + w;
}
int getid(P p) { return getid(p.first, p.second); }
P get2(int id) { return mp(id / W, id % W); }
P operator()(int id) { return get2(id); }
int operator()(int h, int w) { return getid(h, w); }
int operator()(P p) { return getid(p); }
// 辺は無い
grid_k6(int H, int W) : H(H), W(W), undigraph<T>(H * W) {}
grid_k6(vector<vector<char>> ba, char wall = '#')
: H(sz(ba)), W(sz(ba[0])), undigraph<T>(sz(ba) * sz(ba[0])) {
rep(h, H) {
rep(w, W) {
if (ba[h][w] == wall)
con;
int f = getid(h, w);
if (w + 1 < W && ba[h][w + 1] != wall) {
add(f, getid(h, w + 1));
}
if (h + 1 < H && ba[h + 1][w] != wall) {
add(f, getid(h + 1, w));
}
}
}
}
/*o -> o, o -> x, x-> x*/
grid_k6(vector<vector<char>> ba, int oo, int ox, int xo, int xx,
char wall = '#')
: H(sz(ba)), W(sz(ba[0])), undigraph<T>(sz(ba) * sz(ba[0])),
costs({oo, ox, xo, xx}), ba(ba), wall(wall) {
rep(h, H) {
rep(w, W) {
add(h, w, h + 1, w);
add(h, w, h - 1, w);
add(h, w, h, w + 1);
add(h, w, h, w - 1);
}
}
}
void add(int fh, int fw, int th, int tw) {
if (ins(fh, fw, H, W) && ins(th, tw, H, W)) {
int cm = 0;
if (ba[fh][fw] == wall) {
cm += 2;
}
if (ba[th][tw] == wall) {
cm++;
}
int f = getid(fh, fw);
int t = getid(th, tw);
g[f].emplace_back(f, t, costs[cm]);
}
}
void set_edges() {
rep(i, n) fora(e, g[i]) if (e.f < e.t) edges.push_back(e);
}
};
// 辺によりメモリを大量消費ためedgesを消している
// 頂点10^6でメモリを190MB(制限の8割)使う
// 左上から右下に移動できる
template <class T = int> class digrid_k6 : public digraph<T> {
public:
using digraph<T>::g;
using digraph<T>::n;
using digraph<T>::edges;
int H, W;
void add(int f, int t, T c = 1) {
if (!(0 <= f && f < n && 0 <= t && t < n)) {
debugline("digrid_k6 add");
deb(f, t, c);
ole();
}
g[f].emplace_back(f, t, c);
/*digraphと違いedgesを持たない*/
}
int getid(int h, int w) {
if (!ins(h, w, H, W))
return -1;
return W * h + w;
}
P get2(int id) { return mp(id / W, id % W); }
P operator()(int id) { return get2(id); }
int operator()(int h, int w) { return getid(h, w); }
digrid_k6(int H, int W) : H(H), W(W), digraph<T>(H * W) {}
digrid_k6(vector<vector<char>> ba, char wall = '#')
: H(sz(ba)), W(sz(ba[0])), digraph<T>(sz(ba) * sz(ba[0])) {
rep(h, H) {
rep(w, W) {
if (ba[h][w] == wall)
con;
int f = getid(h, w);
if (w + 1 < W && ba[h][w + 1] != wall) {
add(f, getid(h, w + 1));
}
if (h + 1 < H && ba[h + 1][w] != wall) {
add(f, getid(h + 1, w));
}
}
}
}
void add(int fh, int fw, int th, int tw) {
add(getid(fh, fw), getid(th, tw));
}
void set_edges() { rep(i, n) fora(e, g[i]) edges.push_back(e); }
};
// edgesを持たない
// dijkstra(g,0)[t] とかける (t+n-1等とする必要はない)
template <class T = int> class segdi : public digraph<T> {
int getid(int k) {
if (k >= len * 2 - 1) {
return k;
} else if (k < len - 1) {
return k + len;
} else {
return k - len + 1;
}
}
void add(int f, int t, T c = 1) {
f = getid(f);
t = getid(t);
if (!(0 <= f && f < n && 0 <= t && t < n)) {
debugline("segdi add");
deb(f, t, c);
ole();
}
g[f].emplace_back(f, t, c); /*digraphと違いedgesを持たない*/
}
int mid;
int len;
public:
using digraph<T>::g;
using digraph<T>::n;
using digraph<T>::edges;
// 頂点が足りなくなったらresize
segdi(int n_) : digraph<T>(1) {
int nn = 1;
while (nn < n_)
nn *= 2;
n_ = nn;
len = n_;
this->resize(n_ + (n_ - 1) * 2 + n_);
mid = n_ + (n_ - 1) * 2;
int ad = len * 2 - 1;
rep(i, len - 1) {
add(i, i * 2 + 1, 0);
add(i, i * 2 + 2, 0);
if (i * 2 + 1 >= len - 1) {
add(i * 2 + 1, i + ad, 0);
add(i * 2 + 2, i + ad, 0);
} else {
add(i * 2 + 1 + ad, i + ad, 0);
add(i * 2 + 2 + ad, i + ad, 0);
}
}
}
void dfs(vi &list, int nl, int nr, int k, int l, int r) {
if (r <= nl || nr <= l)
return;
if (l <= nl && nr <= r) {
list += k;
} else {
dfs(list, nl, (nl + nr) / 2, k * 2 + 1, l, r);
dfs(list, (nl + nr) / 2, nr, k * 2 + 2, l, r);
}
}
void rekkyo(vi &list, int l, int r) {
l += len - 1;
r += len - 1;
while (l < r) {
if (!(l & 1)) {
list.push_back(l);
}
if (!(r & 1)) {
list.push_back(r - 1);
}
l >>= 1;
r = (r - 1) >> 1;
}
}
// 半開
void add(int fl, int fr, int tl, int tr, int cost) { /*fは下側*/
int ad = 2 * len - 1;
if (mid >= n) {
this->resize(n + 100);
}
{
int l = fl, r = fr;
l += len - 1;
r += len - 1;
while (l < r) {
if (!(l & 1)) {
if (l - len + 1 < 0)
add(l + ad, mid, cost);
else
add(l, mid, cost);
}
if (!(r & 1)) {
if (r - 1 - len + 1 < 0)
add(r - 1 + ad, mid, cost);
else
add(r - 1, mid, cost);
}
l >>= 1;
r = (r - 1) >> 1;
}
}
{
int l = tl, r = tr;
l += len - 1;
r += len - 1;
while (l < r) {
if (!(l & 1)) {
add(mid, l, 0);
}
if (!(r & 1)) {
add(mid, r - 1, 0);
}
l >>= 1;
r = (r - 1) >> 1;
}
}
mid++;
}
};
// edgesを持たない
// dijkstra(g,0)[t] とかける (t+n-1等とする必要はない)
template <class T = int> class segun : public undigraph<T> {
int getid(int k) {
if (k >= len * 2 - 1) {
return k;
} else if (k < len - 1) {
return k + len;
} else {
return k - len + 1;
}
}
void add(int f, int t, T c = 1) {
f = getid(f);
t = getid(t);
if (!(0 <= f && f < n && 0 <= t && t < n)) {
debugline("segun add");
deb(f, t, c);
ole();
}
g[f].emplace_back(f, t, c); /*digraphと違いedgesを持たない*/
g[t].emplace_back(t, f, c); /*digraphと違いedgesを持たない*/
}
int mid;
int len;
public:
using digraph<T>::g;
using digraph<T>::n;
using digraph<T>::edges;
// 頂点が足りなくなったらresize
segun(int n_) : digraph<T>(1) {
int nn = 1;
while (nn < n_)
nn *= 2;
n_ = nn;
len = n_;
this->resize(n_ + (n_ - 1) * 2 + n_);
mid = n_ + (n_ - 1) * 2;
int ad = len * 2 - 1;
rep(i, len - 1) {
add(i, i * 2 + 1, 0);
add(i, i * 2 + 2, 0);
if (i * 2 + 1 >= len - 1) {
add(i * 2 + 1, i + ad, 0);
add(i * 2 + 2, i + ad, 0);
} else {
add(i * 2 + 1 + ad, i + ad, 0);
add(i * 2 + 2 + ad, i + ad, 0);
}
}
}
void dfs(vi &list, int nl, int nr, int k, int l, int r) {
if (r <= nl || nr <= l)
return;
if (l <= nl && nr <= r) {
list += k;
} else {
dfs(list, nl, (nl + nr) / 2, k * 2 + 1, l, r);
dfs(list, (nl + nr) / 2, nr, k * 2 + 2, l, r);
}
}
void rekkyo(vi &list, int l, int r) {
l += len - 1;
r += len - 1;
while (l < r) {
if (!(l & 1)) {
list.push_back(l);
}
if (!(r & 1)) {
list.push_back(r - 1);
}
l >>= 1;
r = (r - 1) >> 1;
}
}
// 半開
void add(int fl, int fr, int tl, int tr, int cost) { /*fは下側*/
int ad = 2 * len - 1;
if (mid >= n) {
this->resize(n + 100);
}
{
int l = fl, r = fr;
l += len - 1;
r += len - 1;
while (l < r) {
if (!(l & 1)) {
if (l - len + 1 < 0)
add(l + ad, mid, cost);
else
add(l, mid, cost);
}
if (!(r & 1)) {
if (r - 1 - len + 1 < 0)
add(r - 1 + ad, mid, cost);
else
add(r - 1, mid, cost);
}
l >>= 1;
r = (r - 1) >> 1;
}
}
{
int l = tl, r = tr;
l += len - 1;
r += len - 1;
while (l < r) {
if (!(l & 1)) {
add(mid, l, 0);
}
if (!(r & 1)) {
add(mid, r - 1, 0);
}
l >>= 1;
r = (r - 1) >> 1;
}
}
mid++;
}
};
#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 unionfind unionfind_graph
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;
}
}
template <class T> unionfind(graph<T> &g) : n(g.n), trees(g.n) {
par.resize(n);
siz.resize(n);
es.resize(n);
for (ll i = 0; i < n; i++) {
par[i] = i;
siz[i] = 1;
}
add(g);
}
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;
}
template <class T> void add(graph<T> &g) {
fora(e, g.edges) { unite(e.f, e.t); }
}
template <class T> void unite(graph<T> &g) { add(g); }
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
//@出力
template <class T> ostream &operator<<(ostream &os, digraph<T> &g) {
os << endl << g.n << " " << sz(g.edges) << endl;
fore(gi, g.edges) { os << f << " " << t << " " << c << endl; }
return os;
}
template <class T> ostream &operator<<(ostream &os, undigraph<T> &g) {
os << endl << g.n << " " << sz(g.edges) / 2 << endl;
fore(gi, g.edges) {
if (f < t)
os << f << " " << t << " " << c << endl;
}
return os;
}
//@判定
template <class T> bool nibu(graph<T> &g) {
int size = 0;
rep(i, g.n) size += sz(g.g[i]);
if (size == 0)
return true;
unionfind uf(g.n * 2);
rep(i, g.n) fora(e, g.g[i]) uf.unite(e.f, e.t + g.n),
uf.unite(e.f + g.n, e.t);
rep(i, g.n) if (uf.same(i, i + g.n)) return 0;
return true;
}
// 頂点ではなく辺の数に依存した計算量 O(E)
template <class T> bool nibu_sub(graph<T> &g) {
umapi col; /*0なら無色 */
queue<int> q; /*色は01か11 */
fora(e, g.edegs) { /*fとtの色を持つか否かは同じ*/
if (col[e.f] == 0)
q.push(e.f);
while (!q.empty()) {
int f = q.front();
q.pop();
int fc = col[f];
forg(gi, g[f]) {
int &tc = col[t]; /*fcには色がある*/
if (fc == tc)
return false; /*違う色*/
if (tc)
continue; /*無色*/
tc = fc ^ 2;
q.push(tc);
}
}
}
return true;
}
// 二部グラフを色分けした際の頂点数を返す
template <class T> vp nibug(graph<T> &g) {
vp cg;
if (!nibu(g)) {
debugline("nibu");
ole();
}
int n = g.size();
vb was(n);
queue<P> q;
rep(i, n) {
if (was[i])
continue;
q.push(mp(i, 1));
was[i] = 1;
int red = 0;
int coun = 0;
while (q.size()) {
int now = q.front().fi;
int col = q.front().se;
red += col;
coun++;
q.pop();
forg(gi, g[now]) {
if (was[t])
continue;
q.push(mp(t, col ^ 1));
was[t] = 1;
}
}
cg.push_back(mp(red, coun - red));
}
return cg;
}
// 連結グラフが与えられる 閉路があるか
template <class T> bool close(undigraph<T> &g) {
int n = 0;
int e = 0;
rep(i, g.n) {
if (sz(g[i]))
n++;
forg(gi, g[i]) { e++; }
}
return (e >> 1) >= n;
}
template <class T> bool close(undigraph<T> &g, int v) {
unionfind uf(g.n);
rep(i, g.n) {
forg(gi, g[i]) {
if (f < t)
break;
if (f == t && f == v)
return true;
if (uf.same(f, v) && uf.same(t, v))
return true;
uf.unite(f, t);
}
}
return false;
}
template <class T> bool close(digraph<T> &g) {
vi res;
return topo(res, g);
}
//@変形
// 条件(f!=0等 f,t,cが使える)を満たすsubgraphをg2に代入
#define sub_di(g, g2, zhouken) \
{ \
g2.resize(g.n); \
fore(gi, g.edges) { \
if (zhouken) { \
g2.add(f, t, c); \
} \
} \
}
#define sub_un(g, g2, zhouken) \
{ \
g2.resize(g.n); \
fore(gi, g.edges) { \
bool ok = zhouken; /*片方がアウトなら駄目という扱い*/ \
swap(f, t); \
ok &= zhouken; \
if (ok && f < t) { \
g2.add(f, t, c); \
} \
} \
}
#define sub_tree sub_un
// 閉路がなければtrue
bool topo(vi &res, digraph<int> &g) {
int n = g.g.size();
vi nyu(n);
rep(i, n) for (auto &&e : g[i]) nyu[e.t]++;
queue<int> st;
rep(i, n) if (nyu[i] == 0) st.push(i);
while (st.size()) {
int v = st.front();
st.pop();
res.push_back(v);
fora(e, g[v]) if (--nyu[e.t] == 0) st.push(e.t);
}
return res.size() == n;
}
// 辞書順最小トポロジカルソート
bool topos(vi &res, digraph<int> &g) {
int n = g.g.size();
vi nyu(n);
rep(i, n) for (auto &&e : g[i]) nyu[e.t]++; /*小さい順*/
priority_queue<int, vector<int>, greater<int>> q;
rep(i, n) if (nyu[i] == 0) q.push(i);
while (q.size()) {
int i = q.top();
q.pop();
res.push_back(i);
fora(e, g[i]) if (--nyu[e.t] == 0) q.push(e.t);
}
return res.size() == n;
}
template <class T> digraph<T> rev(digraph<T> &g) {
digraph<T> r(g.n);
rep(i, g.n) {
forg(gi, g[i]) { r.add(t, f, c); }
}
return r;
}
// lc,rcは子を持つ中で一番左、右
//(g,ind,l,r)
template <class T> tree<T> get_bfs_tree(tree<T> &g, vi &ind, vi &l, vi &r) {
if (sz(ind)) {
cerr << "ind must be empty" << endl;
exit(0);
}
int N = sz(g);
ind.resize(N);
l.resize(N, inf);
r.resize(N, -1);
tree<T> h(N);
queue<P> q;
q.emplace(-1, 0);
int c = 0;
while (sz(q)) {
int p = q.front().first;
int i = q.front().second;
q.pop();
ind[i] = c;
if (~p)
chmi(l[ind[p]], c);
if (~p)
chma(r[ind[p]], c);
c++;
forg(gi, g[i]) {
if (t != p)
q.emplace(i, t);
}
}
fora(e, g.edges) {
if (e.f < e.t) {
h.add(ind[e.f], ind[e.t], e.c);
}
}
rep(i, N) {
if (l[i] == inf)
l[i] = -1;
}
return h;
}
// lc,rcは子を持つ中で一番左、右
// たとえばl[lc[x]は2段下の最左
//(g,ind,l,r,lc,rc)
template <class T>
tree<T> get_bfs_tree(tree<T> &g, vi &ind, vi &l, vi &r, vi &lc, vi &rc) {
if (sz(ind)) {
cerr << "ind must be empty" << endl;
exit(0);
}
int N = sz(g);
ind.resize(N);
l.resize(N, inf);
lc.resize(N, inf);
r.resize(N, -1);
rc.resize(N, -1);
tree<T> h(N);
queue<P> q;
q.emplace(-1, 0);
int c = 0;
while (sz(q)) {
int p = q.front().first;
int i = q.front().second;
q.pop();
ind[i] = c;
if (~p) {
chmi(l[ind[p]], c);
chma(r[ind[p]], c);
if (sz(g[i]) > 1) {
chmi(lc[ind[p]], c);
chma(rc[ind[p]], c);
}
}
c++;
forg(gi, g[i]) {
if (t != p)
q.emplace(i, t);
}
}
fora(e, g.edges) {
if (e.f < e.t) {
h.add(ind[e.f], ind[e.t], e.c);
}
}
rep(i, N) {
if (l[i] == inf)
l[i] = -1;
if (lc[i] == inf)
lc[i] = -1;
}
return h;
}
//@集計
template <class T> vi indegree(graph<T> &g) {
vi ret(g.size());
rep(i, g.size()) {
forg(gi, g[i]) { ret[t]++; }
}
return ret;
}
template <class T> vi outdegree(graph<T> &g) {
vi ret(g.size());
rep(i, g.size()) { ret[i] = g[i].size(); }
return ret;
}
#define kansetu articulation
// private
/*private*/ P farthest____(tree<> &E, int cur, int pre, int d, vi &D) {
D[cur] = d;
P r = {d, cur};
forg(gi, E[cur]) if (t != pre) {
P v = farthest____(E, t, cur, d + 1, D);
r = max(r, v);
}
return r;
}
// dagでなければ-1を返す
int diameter(digraph<> &g) {
vi per;
if (!topo(per, g))
return -1;
int n = g.n;
vi dp(n, 1);
fora(v, per) {
forg(gi, g[v]) { chma(dp[t], dp[f] + 1); }
}
return max(dp);
}
// iから最も離れた距離
vi diameters(tree<> &E) { /* diameter,center*/
vi D[3];
D[0].resize(E.size());
D[1].resize(E.size());
auto v1 = farthest____(E, 0, 0, 0, D[0]);
auto v2 = farthest____(E, v1.second, v1.second, 0, D[0]);
farthest____(E, v2.second, v2.second, 0, D[1]);
int i;
rep(i, D[0].size()) D[2].push_back(max(D[0][i], D[1][i]));
return D[2];
}
// iに対応するjと距離
vp diameters_p(tree<> &E) { /* diameter,center*/
vector<int> D[3];
D[0].resize(E.size());
D[1].resize(E.size());
auto v1 = farthest____(E, 0, 0, 0, D[0]);
auto v2 = farthest____(E, v1.second, v1.second, 0, D[0]);
farthest____(E, v2.second, v2.second, 0, D[1]);
int i;
vp res(E.size());
rep(i, D[0].size()) {
if (D[0][i] > D[1][i])
res[i] = mp(v1.second, D[0][i]);
else
res[i] = mp(v2.second, D[1][i]);
}
return res;
}
int diameter(tree<> &E) {
vi d = diameters(E);
return max(d);
}
// 最も離れた二点を返す
P diameter_p(tree<> &E) {
auto d = diameters_p(E);
int dis = -1;
int l = -1, r = -1;
rep(i, sz(d)) {
if (chma(dis, d[i].se)) {
l = i;
r = d[i].fi;
}
}
return mp(l, r);
}
//@列挙 取得
// 閉路がある時linfを返す
template <class T> int longest_path(digraph<T> &g) {
vi top;
if (!topo(top, g)) {
return linf;
}
int n = sz(top);
vi dp(n, 0);
for (auto &&i : top) {
forg(gi, g[i]) { chma(dp[t], dp[i] + 1); }
}
return max(dp);
}
template <class T> vi longest_path_v(digraph<T> &g) {
vi top;
if (!topo(top, g)) {
return vi();
}
int n = sz(top);
vi dp(n, 0);
vi pre(n, -1);
for (auto &&i : top) {
forg(gi, g[i]) {
if (chma(dp[t], dp[i] + 1)) {
pre[t] = i;
}
}
}
int s = std::max_element(dp.begin(), dp.end()) - dp.begin();
vi path;
while (s != -1) {
path.push_back(s);
s = pre[s];
}
std::reverse(path.begin(), path.end());
return path;
}
// 橋を列挙する (取り除くと連結でなくなる辺)
template <class T> vp bridge(graph<T> &G) {
static bool was;
vp brid;
vi articulation;
vi ord(G.n), low(G.n);
vb vis(G.n);
function<void(int, int, int)> dfs = [&](int v, int p, int k) {
vis[v] = true;
ord[v] = k++;
low[v] = ord[v];
bool isArticulation = false;
int ct = 0;
for (int i = 0; i < G[v].size(); i++) {
if (!vis[G[v][i].t]) {
ct++;
dfs(G[v][i].t, v, k);
low[v] = min(low[v], low[G[v][i].t]);
if (~p && ord[v] <= low[G[v][i].t])
isArticulation = true;
if (ord[v] < low[G[v][i].t])
brid.push_back(make_pair(min(v, G[v][i].t), max(v, G[v][i].t)));
} else if (G[v][i].t != p) {
low[v] = min(low[v], ord[G[v][i].t]);
}
}
if (p == -1 && ct > 1)
isArticulation = true;
if (isArticulation)
articulation.push_back(v);
};
int k = 0;
rep(i, G.n) {
if (!vis[i])
dfs(i, -1, k);
}
sort(brid.begin(), brid.end());
return brid;
}
// 間接点を列挙する (取り除くと連結でなくなる点)
template <class T> vi articulation(undigraph<T> &G) {
static bool was;
vp bridge;
vi arti;
vi ord(G.n), low(G.n);
vb vis(G.n);
function<void(int, int, int)> dfs = [&](int v, int p, int k) {
vis[v] = true;
ord[v] = k++;
low[v] = ord[v];
bool isArticulation = false;
int ct = 0;
for (int i = 0; i < G[v].size(); i++) {
if (!vis[G[v][i].t]) {
ct++;
dfs(G[v][i].t, v, k);
low[v] = min(low[v], low[G[v][i].t]);
if (~p && ord[v] <= low[G[v][i].t])
isArticulation = true;
if (ord[v] < low[G[v][i].t])
bridge.push_back(make_pair(min(v, G[v][i].t), max(v, G[v][i].t)));
} else if (G[v][i].t != p) {
low[v] = min(low[v], ord[G[v][i].t]);
}
}
if (p == -1 && ct > 1)
isArticulation = true;
if (isArticulation)
arti.push_back(v);
};
int k = 0;
rep(i, G.n) {
if (!vis[i])
dfs(i, -1, k);
}
sort(arti.begin(), arti.end());
return arti;
}
// 閉路パスを一つ返す
vi close_path(digraph<> &g) {
int n = g.n;
vi state(n);
vi path;
rep(i, n) if (!state[i]) {
if (fix([&](auto dfs, int v) -> bool {
if (state[v]) {
if (state[v] == 1) {
path.erase(path.begin(), find(path.begin(), path.end(), v));
return true;
}
return false;
}
path.push_back(v);
state[v] = 1;
forg(gi, g[v]) {
if (dfs(t))
return true;
}
state[v] = -1;
path.pop_back();
return false;
})(i)) {
return path;
}
}
return vi();
}
vi close_path_min(digraph<> &g) {
int n = g.n;
vvi(dis, n);
rep(i, n) dis[i] = dijkstra(g, i, linf);
int mind = linf;
int f = 0, t = 0;
rep(i, n) {
rep(j, n) {
if (i == j)
continue;
if (chmi(mind, dis[i][j] + dis[j][i])) {
f = i;
t = j;
}
}
}
vi path;
auto add = [&](int f, int t) {
int now = f;
while (now != t) {
rep(i, n) {
if (dis[now][i] == 1 && dis[f][i] + dis[i][t] == dis[f][t]) {
path.push_back(i);
now = i;
break;
}
}
}
};
add(f, t);
add(t, f);
return path;
}
// iを含む最短閉路 https://atcoder.jp/contests/abc022/tasks/abc022_c
/*閉路が1つしかない場合、その閉路に含まれる頂点を1としたvectorを返す*/;
template <class T> vi get_close1(digraph<T> &g) {
int n = g.n;
queue<int> q;
vi d = outdegree(g);
vi res(n, 1);
rep(i, n) {
if (d[i] == 0) {
q += i;
res[i] = 0;
}
}
auto rg = rev(g);
while (q.size()) {
auto now = q.front();
q.pop();
forg(gi, rg[now]) {
if (--d[t] == 0) {
q += t;
res[t] = 0;
}
}
}
return res;
}
//@アルゴリズム
template <class T> int krus(undigraph<T> &g) {
int res = 0;
unionfind uf(g.n);
if (sz(g.edges) == 0)
g.set_edges();
int i = 0;
auto E = g.edges;
sort(E);
fora(e, E) {
if (uf.unite(e.f, e.t)) {
res += e.c;
}
}
return res;
}
template <class T> vector<edge_<T>> krus_ed(undigraph<T> &g) {
unionfind uf(g.n);
if (sz(g.edges) == 0)
g.set_edges();
int i = 0;
auto E = g.edges;
sort(E);
vector<edge_<T>> res;
fora(e, E) {
if (uf.unite(e.f, e.t)) {
res.push_back(e);
}
}
return res;
}
template <class T> tree<T> krus_tr(undigraph<T> &g) {
tree<T> res(g.n);
unionfind uf(g.n);
if (sz(g.edges) == 0)
g.set_edges();
int i = 0;
auto E = g.edges;
sort(E);
fora(e, E) {
if (uf.unite(e.f, e.t)) {
res.add(e.f, e.t);
}
}
return res;
}
template <class T> int krus(vector<edge_<T>> &g) {
int n = 0;
fora(e, g) { chma(n, max(e.f, e.t) + 1); };
int res = 0;
unionfind uf(n);
auto E = g;
sort(E);
fora(e, E) {
if (uf.unite(e.f, e.t)) {
res += e.c;
}
}
return res;
}
template <class T> vector<edge_<T>> krus_ed(vector<edge_<T>> &g) {
int n = 0;
fora(e, g) { chma(n, max(e.f, e.t) + 1); };
vector<edge_<T>> res;
unionfind uf(n);
auto E = g;
sort(E);
fora(e, E) {
if (uf.unite(e.f, e.t)) {
res += e;
}
}
return res;
};
template <class T> vi krus_i(vector<edge_<T>> &g) {
int n = 0;
fora(e, g) { chma(n, max(e.f, e.t) + 1); };
vi res;
unionfind uf(n);
auto E = g;
sort(E);
int i = 0;
fora(e, E) {
if (uf.unite(e.f, e.t)) {
res += i;
}
i++;
}
return res;
}
//@実験
digraph<> rang_di(int n, int m, bool zibun = 0, bool taju = 0) {
umapp was;
digraph<> g(n);
was[mp(-1, -2)] = 1;
while (m) {
int f = -1, t = -2;
while (f < 0 || (!taju && was[mp(f, t)])) {
f = rand(0, n - 1);
t = rand(0, n - 1);
if (!zibun && f == t)
f = -1;
}
g.add(f, t);
was[mp(f, t)] = 1;
m--;
}
return g;
}
digraph<> perfect_di(int n, bool zibun = 0) {
digraph<> g(n);
rep(i, n) {
rep(j, n) {
if (!zibun && i == j)
con;
g.add(i, j);
}
}
return g;
}
undigraph<> rang_un(int n, int m, bool zibun = 0, bool taju = 0) {
umapp was;
undigraph<> g(n);
was[mp(-1, -2)] = 1;
while (m) {
int f = -1, t = -2;
while (f < 0 || (!taju && was[mp(min(f, t), max(f, t))])) {
f = rand(0, n - 1);
t = rand(0, n - 1);
if (!zibun && f == t)
f = -1;
}
g.add(f, t);
was[mp(min(f, t), max(f, t))] = 1;
m--;
}
return g;
}
undigraph<> perfect_un(int n, bool zibun = 0) {
undigraph<> g(n);
rep(i, n) {
rep(j, i, n) {
if (!zibun && i == j)
con;
g.add(i, j);
}
}
return g;
}
/*頂点数がkの木を一つ返す サイズが0の木が帰ったら終了*/
tree<int> next_tree(int k) {
assert(2 <= k && k < 11);
static str name;
static ifstream ina;
static int rem;
static vp edges;
static int pk = -1; /*前回見たk*/
if (pk != k) {
if (~pk)
ina.close();
edges.clear();
pk = k;
name = (k == 6) ? "C:\\Users\\kaout\\Desktop\\trees_sizek\\nazeka6.txt"
: "C:\\Users\\kaout\\Desktop\\trees_sizek\\tree_size" +
tos(k) + ".txt";
ina = ifstream(name);
rem = pow(k, k - 2); /*Cayleyの定理*/
rep(i, k) rep(j, i + 1, k) edges.emplace_back(i, j);
pk = k;
}
tree<int> g(k);
if (rem == 0) {
g.resize(0);
return g;
}
int m;
ina >> m;
while (m) {
int lb = lbit(m);
int id = log2(lb);
g.add(edges[id].first, edges[id].second);
m ^= lb;
}
rem--;
return g;
}
undigraph<int> next_undi(int k) {
assert(2 <= k && k < 9);
static str name;
static ifstream ina;
static int rem;
static vp edges;
static vi lims = {-1, -1, 1, 4, 38, 728, 26704, 1866256};
static int pk = -1; /*前回見たk*/
if (pk != k) {
if (~pk)
ina.close();
edges.clear();
pk = k;
name = (k == 6) ? "C:\\Users\\kaout\\Desktop\\undi_sizek\\roku.txt"
: "C:\\Users\\kaout\\Desktop\\undi_sizek\\undi_size" +
tos(k) + ".txt";
ina = ifstream(name);
rem = lims[k];
rep(i, k) rep(j, i + 1, k) edges.emplace_back(i, j);
pk = k;
}
undigraph<int> g(k);
if (rem == 0) {
g.resize(0);
return g;
}
int m;
ina >> m;
while (m) {
int lb = lbit(m);
int id = log2(lb);
g.add(edges[id].first, edges[id].second);
m ^= lb;
}
rem--;
return g;
}
vector<tree<int>> trees(int k) {
vector<tree<int>> res;
while (1) {
tree<int> g = next_tree(k);
if (sz(g) == 0)
break;
res.push_back(g);
}
return res;
}
vector<undigraph<int>> undis(int k) {
vector<undigraph<int>> res;
while (1) {
undigraph<int> g = next_undi(k);
if (sz(g) == 0)
break;
res.push_back(g);
}
return res;
}
template <class T> vector<vector<int>> table(graph<T> &g, int init_value) {
vvi(res, g.n, g.n, init_value);
rep(i, g.n) {
forg(gi, g[i]) { res[i][t] = c; }
}
return res;
}
// type,idが使いたい場合はgraty
/*@formatter:on*/
namespace mint_china { /*@formatter:off*/
ll minv(ll a, ll m) {
ll u = 0, v = 1;
while (a != 0) {
ll t = m / a;
m -= t * a;
swap(a, m);
u -= t * v;
swap(u, v);
}
assert(m == 1);
return u;
}
ll garner(const vector<ll> &x, vector<ll> mods, ll mod = 0) {
mods.emplace_back(mod);
int n = sz(x);
vector<ll> coeff; /* coeff[i]v_i*/
vector<ll> constants;
if (mod) {
coeff.assign(n + 1, 1);
constants.assign(n + 1, 0);
} else {
coeff.assign(n, 1);
constants.assign(n, 0);
}
int nj = n + !!mod;
for (size_t i = 0; i < n; i++) {
ll v = (x[i] - constants[i]) * minv(coeff[i], mods[i]) % mods[i];
if (v < 0)
v += mods[i];
for (size_t j = i + 1; j < nj; j++) {
constants[j] = (constants[j] + coeff[j] * v) % mods[j];
coeff[j] = (coeff[j] * mods[i]) % mods[j];
}
}
return constants.back();
}
bool to_disjoint(vi &vs, vi &ms) {
vector<int> primes;
int maxm = *max_element(ms.begin(), ms.end());
int lim = sqrt(maxm);
vb p(lim + 1);
for (int i = 2; i <= lim; i++) {
if (p[i])
continue;
primes.eb(i);
for (int j = i * i; j <= lim; j += i)
p[j] = true;
}
for (int m : ms) {
for (int p : primes)
while (m % p == 0)
m /= p;
if (m != 1)
primes.emplace_back(m);
}
int n = sz(ms);
for (int p : primes) {
vp ps;
for (int i = 0; i < n; i++) {
if (ms[i] % p == 0) {
int lg = 0;
while (ms[i] % p == 0)
ms[i] /= p, lg++;
ps.eb(lg, i);
}
}
if (ps.size() < 1)
continue;
sort(ps);
int base = vs[ps.back().second];
for (auto d : ps) {
if ((vs[d.second] - base) % p)
return false;
if (d.second == ps.back().second)
for (int i = 0; i < d.first; i++)
ms[d.second] *= p;
}
}
return true;
}
int extGCD(int a, int b, int &x0, int &x1) {
x0 = 1, x1 = 0;
int y0 = 0, y1 = 1;
while (b != 0) {
int q = a / b;
int r = a % b;
int x2 = x0 - q * x1;
int y2 = y0 - q * y1;
a = b;
b = r;
x0 = x1;
x1 = x2;
y0 = y1;
y1 = y2;
}
return a;
}
// 答えを x ≡ r (mod. M) として、{r, M} をリターン, 存在しない場合は {0, -1}
// をリターン
P ChineseRem(const vi &b, const vi &m) {
int r = 0, M = 1;
for (int i = 0; i < (int)b.size(); ++i) {
int p, q;
int d = extGCD(M, m[i], p, q); /* p is inv of M/d (mod. m[i]/d)*/
if ((b[i] - r) % d != 0)
return make_pair(0, -1);
long long tmp = (b[i] - r) / d * p % (m[i] / d);
r += M * tmp;
M *= m[i] / d;
}
return make_pair(mod(r, M), M);
}
int china_m_r_mod_dj(vi &ms, vi &rs, int mod = 0,
int disjoint = -3) { /*中国余剰定理*/
if (mod == 0) {
bool exist_non_zero = false;
for (int i = 0; i < sz(rs); ++i) {
if (rs[i])
exist_non_zero = true;
}
P res = ChineseRem(rs, ms);
if (res.second == -1)
return -1;
else if (exist_non_zero)
return res.first;
else
return res.second;
} else {
if (disjoint != -3 && !to_disjoint(rs, ms))
return -1;
int allzero = 1;
ll ans = 1;
int n = sz(ms);
rep(i, n) {
rs[i] = rs[i] % ms[i];
if (rs[i] != 0)
allzero = 0;
}
if (allzero) {
if (mod)
rep(i, n) ans = ans * ms[i] % mod;
else
rep(i, n) ans = ans * ms[i];
return ans;
}
return garner(rs, ms, mod);
}
}
// 基本chinaを使えばいい 答えが無ければ-
// m r
constexpr int MOD = 1000000007;
constexpr int MOE = 1000000009;
/*@formatter:on*/
struct mint {
int X[2];
mint() { X[0] = X[1] = 0; }
mint(int a) {
if (a < 0)
X[0] = (a % MOD) + MOD, X[1] = (a % MOE) + MOE;
else
X[0] = a % MOD, X[1] = a % MOE;
}
mint &operator+=(mint that) {
X[0] = (X[0] + that.X[0]) % MOD;
X[1] = (X[1] + that.X[1]) % MOE;
return *this;
}
mint &operator-=(mint that) {
X[0] = (X[0] + MOD - that.X[0]) % MOD;
X[1] = (X[1] + MOE - that.X[1]) % MOE;
return *this;
}
mint &operator*=(mint that) {
X[0] = (X[0] * that.X[0]) % MOD;
X[1] = (X[1] * that.X[1]) % MOE;
return *this;
}
mint &operator/=(mint that) { return *this *= that.inverse(); }
mint operator-() {
mint res = *this;
if (X[0])
res.X[0] = MOD - X[0];
if (X[1])
res.X[1] = MOE - X[1];
return res;
}
friend ostream &operator<<(ostream &out, mint m) { return out << (int)m; }
mint inverse() {
mint ret;
{
int a = X[0], b = MOD, u = 1, v = 0;
while (b) {
int t = a / b;
a -= t * b;
u -= t * v;
swap(a, b);
swap(u, v);
}
if (u < 0)
u = (u + MOD);
ret.X[0] = (u);
}
{
int a = X[1], b = MOE, u = 1, v = 0;
while (b) {
int t = a / b;
a -= t * b;
u -= t * v;
swap(a, b);
swap(u, v);
}
if (u < 0)
u = (u + MOE);
ret.X[1] = u;
}
return ret;
}
operator int() const {
vi ms = {MOD, MOE};
vi rs = {X[0], X[1]};
return china_m_r_mod_dj(ms, rs);
}
template <class T> mint &operator+=(T that) { return operator+=((mint)that); }
template <class T> mint &operator-=(T that) { return operator-=((mint)that); }
template <class T> mint &operator*=(T that) { return operator*=((mint)that); }
template <class T> mint &operator/=(T that) { return operator/=((mint)that); }
mint operator+(mint that) { return mint(*this) += that; }
mint operator-(mint that) { return mint(*this) -= that; }
mint operator*(mint that) { return mint(*this) *= that; }
mint operator/(mint that) { return mint(*this) /= that; }
template <class T> mint operator+(T that) {
return mint(*this) += (mint)that;
}
template <class T> mint operator-(T that) {
return mint(*this) -= (mint)that;
}
template <class T> mint operator*(T that) {
return mint(*this) *= (mint)that;
}
template <class T> mint operator/(T that) {
return mint(*this) /= (mint)that;
}
// bool operator==(mint that) const { return x == that.x && y ==
// that.y; }
// //重い
// bool operator==(signed that) const { return x == (mint) that; }
// bool operator!=(mint that) const { return !(x == that); }
// bool operator<(mint that) const { return (int) x < (int) that; }
// bool operator<=(mint that) const { return (int) x <= (int) that; }
// bool operator>(mint that) const { return (int) x > (int) that; }
// bool operator>=(mint that) const { return (int) x >= (int) that; }
};
istream &operator>>(istream &i, mint &a) {
int v;
i >> v;
a = mint(v);
return i;
}
typedef vector<mint> vm;
// vector<mint> fac, finv;
int mint_len = 1400000; // 既に変更されていたら変えない
mint fac[1400001], finv[1400001];
struct setmod {
setmod() {
// if (mint_len >= MOD - 1)mint_len = MOD - 1;
// if (mint_len >= MOE - 1)mint_len = MOE - 1;
// fac = vector<mint>(mint_len + 1);
// finv = vector<mint>(mint_len + 1);
fac[0] = 1;
rep(i, 1, mint_len + 1) { fac[i] = fac[i - 1] * i; }
finv[mint_len] = (mint)1 / fac[mint_len];
rer(i, mint_len, 1) { finv[i - 1] = finv[i] * i; }
}
} setmodv;
mint com(int a, int b) {
if (a < 0)
return 0;
if (b < 0 || b > a)
return 0;
return fac[a] * finv[a - b] * finv[b];
}
mint hom(int a, int b) { return com(a + b - 1, b); }
template <typename T, typename U> mint mpow(const T a, const U b) {
assert(b >= 0);
mint x = a, res = 1;
U p = b;
while (p > 0) {
if (p & 1)
(res *= x);
x *= x;
p >>= 1;
}
return res;
} /*@formatter:on*/
using PM = pair<mint, mint>;
using vm = vector<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__)
} // namespace mint_china
//!!!答えが負になってはいけない
using mint = mint_china::mint;
void solve() {
in(N);
tree<> g(2 * k5);
g.ing(N, N - 1);
in(M);
dna2d(U, V, M);
vi in(M);
fora(i, u, v, U, V) { /*@formatter:off*/
fora_init(i, u, v, U, V); /*@formatter:on*/
auto D = g.pathe(u, v);
in[i] = bit(D);
}
deb2(in);
mint res = 0;
rep(mas, bit(M)) {
int use = 0;
rep(i, M) {
if (bget(mas, i))
use |= in[i];
}
deb2(mas);
deb2(use);
int ziyu = N - 1;
ziyu -= bcou(use);
if ((bcou(mas) % 2) == 0) {
res += powi(2, ziyu);
} else {
res -= powi(2, ziyu);
}
}
out(res);
}
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;
}
if (sz(message)) {
cerr << "****************************" << endl;
cerr << message << endl;
cerr << "****************************" << endl;
}
#endif
return 0;
};
| replace | 7,721 | 7,722 | 7,721 | 7,722 | 0 | |
p02794 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define mp make_pair
#define fi first
#define se second
#define eb emplace_back
using namespace std;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 233;
vector<int> g[maxn];
int f[maxn], u[maxn], v[maxn], d[maxn];
ll p[100];
bool vis[maxn];
void dfs(int x, int fa) {
f[x] = fa;
for (int y : g[x]) {
if (y == fa)
continue;
d[y] = d[x] + 1;
dfs(y, x);
}
}
ll lca(int x, int y) {
ll s = 0;
if (d[x] < d[y])
swap(x, y);
// cout<<"!!!!!!!!"<<x<<" "<<y<<endl;
while (d[x] > d[y]) {
vis[x] = 1;
s ^= 1LL << x;
x = f[x];
}
if (x == y)
return s;
while (x != y) {
vis[x] = 1;
s ^= 1LL << x;
x = f[x];
vis[y] = 1;
s ^= 1LL << y;
y = f[y];
}
return s;
}
ll mask[maxn];
int main() {
int n;
cin >> n;
for (int i = 1; i < n; i++) {
int x, y;
scanf("%d%d", &x, &y);
g[x].pb(y);
g[y].pb(x);
}
d[1] = 1;
dfs(1, 0);
int m;
cin >> m;
for (int i = 0; i < m; i++) {
scanf("%d%d", &u[i], &v[i]);
mask[i] = lca(u[i], v[i]);
// cout<<"->"<<i<<" "<<mask[i]<<endl;
}
p[0] = 1;
for (int i = 1; i <= 50; i++)
p[i] = p[i - 1] * 2;
ll ans = 0;
for (int i = 0; i < (1 << m); i++) {
memset(vis, 0, sizeof(vis));
int op = 1;
ll cur = 0;
for (int j = 0; j < m; j++)
if (i & (1 << j)) {
op = -op;
cur |= mask[j];
}
int cnt = 0;
for (int j = 2; j <= n; j++)
if ((cur & (1LL << j)) == 0)
cnt++;
ans += op * p[cnt];
// cout<<"!"<<i<<" "<<cnt<<" "<<cur<<endl;
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define mp make_pair
#define fi first
#define se second
#define eb emplace_back
using namespace std;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 233;
vector<int> g[maxn];
int f[maxn], u[maxn], v[maxn], d[maxn];
ll p[100];
bool vis[100];
void dfs(int x, int fa) {
f[x] = fa;
for (int y : g[x]) {
if (y == fa)
continue;
d[y] = d[x] + 1;
dfs(y, x);
}
}
ll lca(int x, int y) {
ll s = 0;
if (d[x] < d[y])
swap(x, y);
// cout<<"!!!!!!!!"<<x<<" "<<y<<endl;
while (d[x] > d[y]) {
vis[x] = 1;
s ^= 1LL << x;
x = f[x];
}
if (x == y)
return s;
while (x != y) {
vis[x] = 1;
s ^= 1LL << x;
x = f[x];
vis[y] = 1;
s ^= 1LL << y;
y = f[y];
}
return s;
}
ll mask[maxn];
int main() {
int n;
cin >> n;
for (int i = 1; i < n; i++) {
int x, y;
scanf("%d%d", &x, &y);
g[x].pb(y);
g[y].pb(x);
}
d[1] = 1;
dfs(1, 0);
int m;
cin >> m;
for (int i = 0; i < m; i++) {
scanf("%d%d", &u[i], &v[i]);
mask[i] = lca(u[i], v[i]);
// cout<<"->"<<i<<" "<<mask[i]<<endl;
}
p[0] = 1;
for (int i = 1; i <= 50; i++)
p[i] = p[i - 1] * 2;
ll ans = 0;
for (int i = 0; i < (1 << m); i++) {
memset(vis, 0, sizeof(vis));
int op = 1;
ll cur = 0;
for (int j = 0; j < m; j++)
if (i & (1 << j)) {
op = -op;
cur |= mask[j];
}
int cnt = 0;
for (int j = 2; j <= n; j++)
if ((cur & (1LL << j)) == 0)
cnt++;
ans += op * p[cnt];
// cout<<"!"<<i<<" "<<cnt<<" "<<cur<<endl;
}
cout << ans << endl;
return 0;
}
| replace | 14 | 15 | 14 | 15 | TLE | |
p02794 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define ALL(x) x.begin(), x.end()
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
random_device rnd;
mt19937 mt(rnd());
using ll = long long;
using lld = long double;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using PII = pair<int, int>;
const int IINF = 1 << 28;
const ll INF = 1ll << 60;
const ll MOD = 1000000007;
map<int, vector<int>> g;
vector<vector<PII>> route;
vector<vector<vector<PII>>> path;
int n;
int m;
int tmpnum = 0;
int tmpsize = 0;
ll ans;
int start;
ll K;
map<int, bool> cc;
vector<vector<vector<PII>>> warshallfloyd() {
vector<vector<vector<PII>>> path(n, vector<vector<PII>>(n, vector<PII>(0)));
VVI dist(n, VI(n, IINF));
rep(i, n) {
for (auto &&j : g[i]) {
if (i < j) {
path[i][j].push_back({i, j});
path[j][i].push_back({i, j});
dist[i][j] = 1;
dist[j][i] = 1;
}
}
}
rep(k, n) rep(i, n) rep(j, n) {
if (dist[i][j] > dist[i][k] + dist[k][j]) {
dist[i][j] = dist[i][k] + dist[k][j];
path[i][j] = path[i][k];
for (auto x : path[k][j])
path[i][j].push_back(x);
path[j][i] = path[i][j];
}
}
return path;
}
ll powll(ll base, ll num) {
if (num == 0) {
return 1;
}
ll prev = powll(base, num / 2);
if (num % 2 == 0) {
return prev * prev;
} else {
return prev * prev * base;
}
}
void dfs(int cur, vector<PII> &marked, map<PII, bool> &skipped) {
if (cur == m) {
int num = 0;
// cerr << "*************" << endl;
rep(i, n) {
rep(j, i + 1, n) {
if (skipped[{i, j}]) {
num++;
// cerr << i << "," << j << " ";
}
}
}
// cerr << endl;
for (auto &&x : marked) {
// cerr << x.first << "," << x.second << " ";
}
// cerr << endl;
// cerr << n - 1 - marked.size() - num << endl;
assert(n - 1 >= marked.size() + num);
ans += K * powll(2, n - 1 - marked.size() - num - tmpsize);
return;
} else {
if (cc[cur]) {
dfs(cur + 1, marked, skipped);
return;
}
bool flag = false;
for (auto &&x : marked) {
if (find(ALL(route[cur]), x) != route[cur].end()) {
flag = true;
dfs(cur + 1, marked, skipped);
break;
}
}
vector<PII> pii;
if (flag == false) {
for (auto &&x : route[cur]) {
if (skipped[x])
continue;
marked.push_back(x);
dfs(cur + 1, marked, skipped);
marked.pop_back();
skipped[x] = true;
pii.push_back(x);
}
}
for (auto &&x : pii)
skipped[x] = false;
}
}
int main() {
ans = 0;
cin >> n;
rep(i, n - 1) {
int a, b;
cin >> a >> b;
a--;
b--;
g[a].push_back(b);
g[b].push_back(a);
}
auto path = warshallfloyd();
cin >> m;
vector<PII> cons(m);
rep(i, m) {
cin >> cons[i].first >> cons[i].second;
cons[i].first--;
cons[i].second--;
}
route.resize(m);
map<PII, int> num;
rep(i, m) {
start = i;
route[i] = path[cons[i].first][cons[i].second];
// sort(ALL(route[i]));
// cerr << route[i].size() << endl;
for (auto &&x : route[i]) {
num[x]++;
// cerr << x.first << "," << x.second << endl;
}
}
K = 1;
int wrapnum = 0;
int tmptmp = 0;
rep(i, m) {
cc[i] = true;
for (auto &&x : route[i]) {
chmax(tmptmp, (int)route[i].size());
if (num[x] > 1) {
wrapnum++;
cc[i] = false;
break;
}
}
if (cc[i]) {
K *= powll(2, route[i].size()) - 1;
tmpnum++;
tmpsize += route[i].size();
}
}
cerr << "calc"
<< " " << tmptmp << endl;
if (tmpnum != 0 && tmptmp < 15) {
rep(i, m) { random_shuffle(ALL(route[i])); }
}
map<PII, bool> skipped = {};
vector<PII> marked = {};
if (tmpnum == m) {
cerr << "tmp" << endl;
ans = K * powll(2, n - 1 - tmpsize);
} else {
dfs(0, marked, skipped);
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define ALL(x) x.begin(), x.end()
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
random_device rnd;
mt19937 mt(rnd());
using ll = long long;
using lld = long double;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using PII = pair<int, int>;
const int IINF = 1 << 28;
const ll INF = 1ll << 60;
const ll MOD = 1000000007;
map<int, vector<int>> g;
vector<vector<PII>> route;
vector<vector<vector<PII>>> path;
int n;
int m;
int tmpnum = 0;
int tmpsize = 0;
ll ans;
int start;
ll K;
map<int, bool> cc;
vector<vector<vector<PII>>> warshallfloyd() {
vector<vector<vector<PII>>> path(n, vector<vector<PII>>(n, vector<PII>(0)));
VVI dist(n, VI(n, IINF));
rep(i, n) {
for (auto &&j : g[i]) {
if (i < j) {
path[i][j].push_back({i, j});
path[j][i].push_back({i, j});
dist[i][j] = 1;
dist[j][i] = 1;
}
}
}
rep(k, n) rep(i, n) rep(j, n) {
if (dist[i][j] > dist[i][k] + dist[k][j]) {
dist[i][j] = dist[i][k] + dist[k][j];
path[i][j] = path[i][k];
for (auto x : path[k][j])
path[i][j].push_back(x);
path[j][i] = path[i][j];
}
}
return path;
}
ll powll(ll base, ll num) {
if (num == 0) {
return 1;
}
ll prev = powll(base, num / 2);
if (num % 2 == 0) {
return prev * prev;
} else {
return prev * prev * base;
}
}
void dfs(int cur, vector<PII> &marked, map<PII, bool> &skipped) {
if (cur == m) {
int num = 0;
// cerr << "*************" << endl;
rep(i, n) {
rep(j, i + 1, n) {
if (skipped[{i, j}]) {
num++;
// cerr << i << "," << j << " ";
}
}
}
// cerr << endl;
for (auto &&x : marked) {
// cerr << x.first << "," << x.second << " ";
}
// cerr << endl;
// cerr << n - 1 - marked.size() - num << endl;
assert(n - 1 >= marked.size() + num);
ans += K * powll(2, n - 1 - marked.size() - num - tmpsize);
return;
} else {
if (cc[cur]) {
dfs(cur + 1, marked, skipped);
return;
}
bool flag = false;
for (auto &&x : marked) {
if (find(ALL(route[cur]), x) != route[cur].end()) {
flag = true;
dfs(cur + 1, marked, skipped);
break;
}
}
vector<PII> pii;
if (flag == false) {
for (auto &&x : route[cur]) {
if (skipped[x])
continue;
marked.push_back(x);
dfs(cur + 1, marked, skipped);
marked.pop_back();
skipped[x] = true;
pii.push_back(x);
}
}
for (auto &&x : pii)
skipped[x] = false;
}
}
int main() {
ans = 0;
cin >> n;
rep(i, n - 1) {
int a, b;
cin >> a >> b;
a--;
b--;
g[a].push_back(b);
g[b].push_back(a);
}
auto path = warshallfloyd();
cin >> m;
vector<PII> cons(m);
rep(i, m) {
cin >> cons[i].first >> cons[i].second;
cons[i].first--;
cons[i].second--;
}
route.resize(m);
map<PII, int> num;
rep(i, m) {
start = i;
route[i] = path[cons[i].first][cons[i].second];
// sort(ALL(route[i]));
// cerr << route[i].size() << endl;
for (auto &&x : route[i]) {
num[x]++;
// cerr << x.first << "," << x.second << endl;
}
}
K = 1;
int wrapnum = 0;
int tmptmp = 0;
rep(i, m) {
cc[i] = true;
for (auto &&x : route[i]) {
chmax(tmptmp, (int)route[i].size());
if (num[x] > 1) {
wrapnum++;
cc[i] = false;
break;
}
}
if (cc[i]) {
K *= powll(2, route[i].size()) - 1;
tmpnum++;
tmpsize += route[i].size();
}
}
cerr << "calc" << tmpnum << " " << tmptmp << endl;
if (tmpnum != 0 || tmptmp < 15) {
rep(i, m) { random_shuffle(ALL(route[i])); }
}
map<PII, bool> skipped = {};
vector<PII> marked = {};
if (tmpnum == m) {
cerr << "tmp" << endl;
ans = K * powll(2, n - 1 - tmpsize);
} else {
dfs(0, marked, skipped);
}
cout << ans << endl;
return 0;
}
| replace | 175 | 178 | 175 | 177 | TLE | |
p02795 | C++ | Runtime Error | #include <algorithm>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
#define PB push_back
#define ll long long
#define ull unsigned long long
#define uint unsigned
#define PIE (3.14159265358979323846)
#define MOD 1000000007;
using namespace std;
void checkStream() {
#ifndef _DEBUFG
freopen("input.in", "r", stdin);
freopen("output.out", "w", stdout);
#endif
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
checkStream();
int r{}, c{}, n{};
cin >> r >> c >> n;
int step1{}, step2{};
int ttl{};
for (int i{}; i < r && ttl < n; ++i) {
ttl += c;
++step1;
}
ttl = 0;
for (int i{}; i < c && ttl < n; ++i) {
ttl += r;
++step2;
}
cout << min(step1, step2) << endl;
return 0;
}
| #include <algorithm>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
#define PB push_back
#define ll long long
#define ull unsigned long long
#define uint unsigned
#define PIE (3.14159265358979323846)
#define MOD 1000000007;
using namespace std;
void checkStream() {
#ifndef _DEBUFG
freopen("input.in", "r", stdin);
freopen("output.out", "w", stdout);
#endif
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// checkStream();
int r{}, c{}, n{};
cin >> r >> c >> n;
int step1{}, step2{};
int ttl{};
for (int i{}; i < r && ttl < n; ++i) {
ttl += c;
++step1;
}
ttl = 0;
for (int i{}; i < c && ttl < n; ++i) {
ttl += r;
++step2;
}
cout << min(step1, step2) << endl;
return 0;
}
| replace | 27 | 28 | 27 | 28 | 0 | |
p02795 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define PI pair<int, int>
#define ff first
#define ss second
#define boost \
ios_base::sync_with_stdio(false); \
cin.tie(NULL)
// #include "debug.cpp"
int32_t main() {
boost;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif // ONLINE_JUDGE
int a, b, n;
cin >> a >> b >> n;
if (a < b)
swap(a, b);
int ans = 0;
while (ans * a < n)
ans++;
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define PI pair<int, int>
#define ff first
#define ss second
#define boost \
ios_base::sync_with_stdio(false); \
cin.tie(NULL)
// #include "debug.cpp"
int32_t main() {
boost;
int a, b, n;
cin >> a >> b >> n;
if (a < b)
swap(a, b);
int ans = 0;
while (ans * a < n)
ans++;
cout << ans << endl;
} | delete | 13 | 17 | 13 | 13 | 0 | |
p02795 | C++ | Time Limit Exceeded | #include <stdio.h>
int main() {
int h, w, k, omen;
scanf("%d %d %d", &h, &w, &k);
if (h > w)
omen = h;
int count = 1;
for (int i = omen; i < k; i += omen) {
count++;
}
printf("%d\n", count);
return 0;
} | #include <stdio.h>
int main() {
int h, w, k, omen;
scanf("%d %d %d", &h, &w, &k);
if (h > w)
omen = h;
else
omen = w;
int count = 1;
for (int i = omen; i < k; i += omen) {
count++;
}
printf("%d\n", count);
return 0;
} | insert | 6 | 6 | 6 | 8 | TLE | |
p02795 | C++ | Runtime Error | #define __USE_MINGW_ANSI_STDIO
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef long long ll;
typedef long long ull;
typedef pair<int, int> ii;
#define all(v) ((v).begin()), ((v).end())
#define sz(v) ((int)((v).size()))
#define endl "\n"
#define fx(n) fixed << setprecision(n)
#define mk make_pair
void fast() {
ios::sync_with_stdio(NULL);
cout.tie(NULL);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("output.txt", "w", stdout);
freopen("input.txt", "r", stdin);
#endif
#ifdef ONLINE_JUDGE
/*freopen("output.txt", "w", stdout);
freopen("pyramid.in", "r", stdin);*/
#endif
}
const double pi = 2 * acos(0.0);
const ll oo = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int nn = 2e2 + 15;
int dx[8] = {-1, 0, 0, 1, 1, -1, 1, -1};
int dy[8] = {0, -1, 1, 0, 1, -1, -1, 1};
int main() {
fast();
ll a, b, c;
cin >> a >> b >> c;
cout << ceil(double(c) / max(a, b)) << endl;
return 0;
} | #define __USE_MINGW_ANSI_STDIO
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef long long ll;
typedef long long ull;
typedef pair<int, int> ii;
#define all(v) ((v).begin()), ((v).end())
#define sz(v) ((int)((v).size()))
#define endl "\n"
#define fx(n) fixed << setprecision(n)
#define mk make_pair
void fast() {
ios::sync_with_stdio(NULL);
cout.tie(NULL);
cin.tie(NULL);
/*#ifndef ONLINE_JUDGE
freopen("output.txt", "w", stdout);
freopen("input.txt", "r", stdin);
#endif
#ifdef ONLINE_JUDGE
/*freopen("output.txt", "w", stdout);
freopen("pyramid.in", "r", stdin);
#endif*/
}
const double pi = 2 * acos(0.0);
const ll oo = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int nn = 2e2 + 15;
int dx[8] = {-1, 0, 0, 1, 1, -1, 1, -1};
int dy[8] = {0, -1, 1, 0, 1, -1, -1, 1};
int main() {
fast();
ll a, b, c;
cin >> a >> b >> c;
cout << ceil(double(c) / max(a, b)) << endl;
return 0;
} | replace | 22 | 30 | 22 | 30 | 0 | |
p02795 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int m, h, w, n, ans = 0;
int main() {
cin >> h >> w >> n;
if (h < w)
swap(h, w);
if (n <= h) {
cout << "1";
return 0;
} else {
ans += n / h;
n %= h;
if (n % (h - ans) == 0)
m = n / (h - ans);
else
m = n / (h - ans) + 1;
}
cout << ans + m;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int m, h, w, n, ans = 0;
int main() {
cin >> h >> w >> n;
if (h < w)
swap(h, w);
if (n <= h) {
cout << "1";
return 0;
} else {
ans += n / h;
n %= h;
if (n == 0) {
cout << ans;
return 0;
}
if (n % (h - ans) == 0)
m = n / (h - ans);
else
m = n / (h - ans) + 1;
}
cout << ans + m;
return 0;
} | insert | 16 | 16 | 16 | 20 | 0 | |
p02795 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ll long long
#define mod 1000000007
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll int h, w, n, r, s, res = 0;
cin >> h >> w >> n;
r = max(h, w);
s = min(h, w);
while (n > 0) {
n -= r;
res++;
}
cout << res << endl;
} | #include <bits/stdc++.h>
#define ll long long
#define mod 1000000007
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll int h, w, n, r, s, res = 0;
cin >> h >> w >> n;
r = max(h, w);
s = min(h, w);
while (n > 0) {
n -= r;
res++;
}
cout << res << endl;
} | replace | 9 | 13 | 9 | 10 | 0 | |
p02795 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int H, W, N;
cin >> H >> W >> N;
cout << N / max(H, W) + N % (N / max(H, W) * max(H, W)) / min(H, W);
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
int H, W, N;
cin >> H >> W >> N;
if (N % (max(H, W)) == 0) {
cout << N / max(H, W);
} else {
cout << N / (max(H, W)) + 1;
}
}
| replace | 5 | 6 | 5 | 10 | 0 | |
p02795 | C++ | Runtime Error | #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define vi vector<int>
#define br cout << "\n";
#define all(x) (x).begin(), (x).end()
#define tr(c, i) for (auto i : c)
#define pii pair<int, int>
#define fast_io() \
ios_base::sync_with_stdio(false); \
cin.tie(nullptr)
#define pq \
priority_queue<pair<ll, pii>, vector<pair<ll, pii>>, greater<pair<ll, pii>>> \
p; // container adapter makes ascending q
#define er(x) cout << x << " "
#define err(x, y) cout << x << " " << y
using namespace std;
const ll MAX = 1000 * 1000;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};
const int MOD = 1000 * 1000 * 1000 + 7;
const int N = 301;
const int INF = INT_MAX;
const int M = 200005;
int main() {
ll h, w, n;
cin >> h >> w >> n;
if (h < w)
swap(h, w);
ll res = n / h;
n -= res * h;
w -= res;
res += n / w;
cout << res;
return 0;
}
| #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define vi vector<int>
#define br cout << "\n";
#define all(x) (x).begin(), (x).end()
#define tr(c, i) for (auto i : c)
#define pii pair<int, int>
#define fast_io() \
ios_base::sync_with_stdio(false); \
cin.tie(nullptr)
#define pq \
priority_queue<pair<ll, pii>, vector<pair<ll, pii>>, greater<pair<ll, pii>>> \
p; // container adapter makes ascending q
#define er(x) cout << x << " "
#define err(x, y) cout << x << " " << y
using namespace std;
const ll MAX = 1000 * 1000;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};
const int MOD = 1000 * 1000 * 1000 + 7;
const int N = 301;
const int INF = INT_MAX;
const int M = 200005;
int main() {
ll h, w, n;
cin >> h >> w >> n;
cout << min((n + h - 1) / h, (n + w - 1) / w);
return 0;
}
| replace | 33 | 42 | 33 | 34 | 0 | |
p02795 | C++ | Time Limit Exceeded | #include <cmath>
#include <iostream>
using namespace std;
int main() {
int h, w, n;
cin >> h;
cin >> w;
cin >> n;
int a = max(h, w);
int count = 0;
while (1) {
if (n >= a) {
n -= a;
count++;
}
if (n <= 0) {
break;
}
}
cout << count << endl;
return 0;
} | #include <cmath>
#include <iostream>
using namespace std;
int main() {
int h, w, n;
cin >> h;
cin >> w;
cin >> n;
int a = max(h, w);
int count = 0;
while (1) {
n -= a;
count++;
if (n <= 0) {
break;
}
}
cout << count << endl;
return 0;
} | replace | 15 | 20 | 15 | 17 | TLE | |
p02795 | C++ | Time Limit Exceeded | #include <iostream>
int main() {
int h;
int w;
int n;
int paintNum = 0;
std::cin >> h >> w >> n;
while (n > 0) {
if (w > h) {
paintNum++;
n -= w;
} else if (h > w) {
paintNum++;
n -= h;
}
}
std::cout << paintNum << std::endl;
return 0;
} | #include <iostream>
int main() {
int h;
int w;
int n;
int paintNum = 0;
std::cin >> h >> w >> n;
while (n > 0) {
if (w > h) {
paintNum++;
n -= w;
} else {
paintNum++;
n -= h;
}
}
std::cout << paintNum << std::endl;
return 0;
} | replace | 12 | 13 | 12 | 13 | TLE | |
p02795 | C++ | Time Limit Exceeded | #include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
using namespace std;
int main() {
int N, H, W, B;
cin >> H;
cin >> W;
cin >> N;
B = 0;
int i;
for (i = 0; B < N; i++) {
if (W > H) {
B += W;
}
if (H > W) {
B += H;
}
}
cout << i << endl;
return 0;
}
| #include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
using namespace std;
int main() {
int N, H, W, B;
cin >> H;
cin >> W;
cin >> N;
B = 0;
int i;
for (i = 0; B < N; i++) {
if (W > H) {
B += W;
} else {
B += H;
}
}
cout << i << endl;
return 0;
}
| replace | 19 | 21 | 19 | 20 | TLE | |
p02795 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int h, w, n;
cin >> h >> w >> n;
int a = max(h, w);
int k = n / a;
if (n % k != 0)
k++;
cout << k << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int h, w, n;
cin >> h >> w >> n;
int a = max(h, w);
if (n >= a) {
int k = n / a;
if (n % a != 0)
k++;
cout << k << endl;
} else
cout << 1 << endl;
} | replace | 6 | 10 | 6 | 13 | 0 | |
p02795 | C++ | Runtime Error |
/*
LANG:C++
PROB:
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
#include <iostream>
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;
typedef unsigned long long ui64;
template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }
const int mod = 1000000007;
i64 poww(i64 a, i64 b) {
a %= mod;
i64 res = 1;
while (b) {
if (b & 1)
res = res * a % mod;
a = a * a % mod;
b /= 2;
}
return res;
}
i64 inv(i64 a) { return poww(a, mod - 2); }
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
// ifstream fin(".in");
// ofstream fout(".in");
i64 a, b, c;
cin >> a >> b >> c;
a = max(a, b);
return (c + a - 1) / a;
return 0;
} |
/*
LANG:C++
PROB:
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
#include <iostream>
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;
typedef unsigned long long ui64;
template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }
const int mod = 1000000007;
i64 poww(i64 a, i64 b) {
a %= mod;
i64 res = 1;
while (b) {
if (b & 1)
res = res * a % mod;
a = a * a % mod;
b /= 2;
}
return res;
}
i64 inv(i64 a) { return poww(a, mod - 2); }
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
// ifstream fin(".in");
// ofstream fout(".in");
i64 a, b, c;
cin >> a >> b >> c;
a = max(a, b);
cout << (c + a - 1) / a;
return 0;
return 0;
}
| replace | 67 | 68 | 67 | 69 | 2 | |
p02795 | C++ | Runtime Error | #define ll long long
#define MOD 1000000007
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define set2d(array, val, m, n) memset(array, val, sizeof(array[0][0]) * m * n);
#include <bits/stdc++.h>
using namespace std;
ll gcd(ll a, ll b) {
if (!b)
return a;
return gcd(b, a % b);
}
ll power(ll x, ll y, ll p) {
ll res = 1;
x %= p;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
ll po(ll base, ll powerRaised) {
if (powerRaised != 0)
return (base * po(base, powerRaised - 1));
else
return 1;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif //*/
long double h, w, n;
cin >> h >> w >> n;
cout << ceil(n / max(h, w));
cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
return 0;
}
| #define ll long long
#define MOD 1000000007
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define set2d(array, val, m, n) memset(array, val, sizeof(array[0][0]) * m * n);
#include <bits/stdc++.h>
using namespace std;
ll gcd(ll a, ll b) {
if (!b)
return a;
return gcd(b, a % b);
}
ll power(ll x, ll y, ll p) {
ll res = 1;
x %= p;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
ll po(ll base, ll powerRaised) {
if (powerRaised != 0)
return (base * po(base, powerRaised - 1));
else
return 1;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif //*/
long double h, w, n;
cin >> h >> w >> n;
cout << ceil(n / max(h, w));
cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
return 0;
}
| replace | 35 | 39 | 35 | 39 | 0 |
Time elapsed: 29ms
|
p02796 | Python | Time Limit Exceeded | N = int(input())
R = sorted([list(map(int, input().split())) for i in range(N)])
T = []
for i in range(N):
T.append([R[i][0] + R[i][1], R[i][0] - R[i][1]])
T.sort()
while len(T) - 1 > 0:
t = T.pop(0)
i = 1
while len(T) and t[0] > T[0][1]:
N -= 1
i += 1
T.pop(0)
print(N)
| N = int(input())
X = [list(map(int, input().split())) for i in range(N)]
LR = [[x[0] - x[1], x[0] + x[1]] for x in X]
LR.sort(key=lambda x: x[1])
A = 1
right = LR[0][1]
for i in range(1, N):
if right <= LR[i][0]:
A += 1
right = LR[i][1]
print(A)
| replace | 1 | 14 | 1 | 12 | TLE | |
p02796 | Python | Time Limit Exceeded | #!/usr/bin/env python3
(n,), *r = [[*map(int, i.split())] for i in open(0)]
s = sorted([[x + l, x - l] for x, l in r])
c = 0
p = 0
while s:
c += 1
p = s[0][0]
s = [i for i in s if i[1] >= p]
print(c)
| #!/usr/bin/env python3
(n,), *r = [[*map(int, i.split())] for i in open(0)]
s = sorted([[x + l, x - l] for x, l in r])
c = 1
p = s[0][0]
for i in s[1:]:
if i[1] >= p:
c += 1
p = i[0]
print(c)
| replace | 3 | 9 | 3 | 9 | TLE | |
p02796 | C++ | Runtime Error | /**
* author : The San Cao (caothesan@gmail.com)
* created : 2020.02.08 10:32:46 +07
*/
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
const int maxn = 1e5 + 10;
int n;
int t[maxn];
vector<pair<int, int>> robots;
void update(int x, int v) {
for (; x < maxn; x += x & -x)
t[x] = max(t[x], v);
}
int get(int x) {
int res = 0;
for (; x > 0; x -= x & -x)
res = max(res, t[x]);
return res;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin >> n;
vector<int> c;
for (int i = 0; i < n; ++i) {
int x, l;
cin >> x >> l;
robots.emplace_back(x + l, x - l);
c.push_back(x + l);
c.push_back(x - l);
}
sort(c.begin(), c.end());
for (auto &it : robots) {
it.first = lower_bound(c.begin(), c.end(), it.first) - c.begin() + 1;
it.second = lower_bound(c.begin(), c.end(), it.second) - c.begin() + 1;
}
sort(robots.begin(), robots.end());
int answer = 0;
for (auto it : robots) {
int cur = get(it.second);
answer = max(answer, cur + 1);
update(it.first, cur + 1);
}
cout << answer << "\n";
return 0;
}
| /**
* author : The San Cao (caothesan@gmail.com)
* created : 2020.02.08 10:32:46 +07
*/
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
const int maxn = 2e5 + 10;
int n;
int t[maxn];
vector<pair<int, int>> robots;
void update(int x, int v) {
for (; x < maxn; x += x & -x)
t[x] = max(t[x], v);
}
int get(int x) {
int res = 0;
for (; x > 0; x -= x & -x)
res = max(res, t[x]);
return res;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin >> n;
vector<int> c;
for (int i = 0; i < n; ++i) {
int x, l;
cin >> x >> l;
robots.emplace_back(x + l, x - l);
c.push_back(x + l);
c.push_back(x - l);
}
sort(c.begin(), c.end());
for (auto &it : robots) {
it.first = lower_bound(c.begin(), c.end(), it.first) - c.begin() + 1;
it.second = lower_bound(c.begin(), c.end(), it.second) - c.begin() + 1;
}
sort(robots.begin(), robots.end());
int answer = 0;
for (auto it : robots) {
int cur = get(it.second);
answer = max(answer, cur + 1);
update(it.first, cur + 1);
}
cout << answer << "\n";
return 0;
}
| replace | 12 | 13 | 12 | 13 | 0 | |
p02796 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct player {
ll x;
ll b;
};
const int MAXN = 1100;
player A[MAXN];
bool player_compare(player p1, player p2) {
return (p1.x + p1.b) < (p2.x + p2.b);
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int x, b;
cin >> x >> b;
player p;
p.x = x;
p.b = b;
A[i] = p;
}
sort(A, A + n, player_compare);
int total = 1;
int i = 1;
int right = A[0].x + A[0].b;
while (i < n) {
if (A[i].x - A[i].b >= right) {
total++;
right = A[i].x + A[i].b;
}
i++;
}
cout << total << "\n";
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct player {
ll x;
ll b;
};
const int MAXN = 100100;
player A[MAXN];
bool player_compare(player p1, player p2) {
return (p1.x + p1.b) < (p2.x + p2.b);
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int x, b;
cin >> x >> b;
player p;
p.x = x;
p.b = b;
A[i] = p;
}
sort(A, A + n, player_compare);
int total = 1;
int i = 1;
int right = A[0].x + A[0].b;
while (i < n) {
if (A[i].x - A[i].b >= right) {
total++;
right = A[i].x + A[i].b;
}
i++;
}
cout << total << "\n";
}
| replace | 9 | 10 | 9 | 10 | 0 | |
p02796 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ll long long
#define mod 1000000007
using namespace std;
typedef pair<ll, ll> P;
const ll INF = 1e18;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll int n, i;
cin >> n;
ll int x[n], l[n];
for (i = 0; i < n; i++) {
cin >> x[i] >> l[i];
}
vector<P> v;
for (i = 0; i < n; i++) {
v.push_back(P(x[i] + l[i], x[i] - l[i]));
}
sort(v.begin(), v.end());
ll int ans = 0;
ll int at = -INF;
for (i = 0; i < n; i++) {
ll int left = v[i].second;
ll int right = v[i].first;
if (left >= at) {
ans++;
at = right;
}
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
#define ll long long
#define mod 1000000007
using namespace std;
typedef pair<ll, ll> P;
const ll INF = 1e18;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll int n, i;
cin >> n;
ll int x[n], l[n];
for (i = 0; i < n; i++) {
cin >> x[i] >> l[i];
}
vector<P> v;
for (i = 0; i < n; i++) {
v.push_back(P(x[i] + l[i], x[i] - l[i]));
}
sort(v.begin(), v.end());
ll int ans = 0;
ll int at = -INF;
for (i = 0; i < n; i++) {
ll int left = v[i].second;
ll int right = v[i].first;
if (left >= at) {
ans++;
at = right;
}
}
cout << ans << endl;
return 0;
} | delete | 11 | 15 | 11 | 11 | 0 | |
p02796 | C++ | Runtime Error | #include <algorithm>
#include <cstring>
#include <iostream>
#include <math.h>
#include <stdio.h>
using namespace std;
struct Meet {
int beg;
int end;
int num;
} meet[1000];
class setMeet {
public:
void init();
void solve();
private:
int n, ans;
};
void setMeet::init() {
int s, e;
cin >> n;
int i;
for (i = 0; i < n; ++i) {
cin >> s >> e;
int min, max;
max = s + e;
min = s - e;
meet[i].beg = min;
meet[i].end = max;
meet[i].num = i + 1;
}
}
bool cmp(Meet x, Meet y) {
if (x.end == y.end)
return x.beg > y.beg;
return x.end < y.end;
}
void setMeet::solve() {
sort(meet, meet + n, cmp);
int i;
ans = 1;
int last = meet[0].end;
for (i = 1; i < n; ++i) {
if (meet[i].beg >= last) {
ans++;
last = meet[i].end;
}
}
cout << ans << endl;
}
int main() {
setMeet sm;
sm.init();
sm.solve();
return 0;
} | #include <algorithm>
#include <cstring>
#include <iostream>
#include <math.h>
#include <stdio.h>
using namespace std;
struct Meet {
int beg;
int end;
int num;
} meet[100000];
class setMeet {
public:
void init();
void solve();
private:
int n, ans;
};
void setMeet::init() {
int s, e;
cin >> n;
int i;
for (i = 0; i < n; ++i) {
cin >> s >> e;
int min, max;
max = s + e;
min = s - e;
meet[i].beg = min;
meet[i].end = max;
meet[i].num = i + 1;
}
}
bool cmp(Meet x, Meet y) {
if (x.end == y.end)
return x.beg > y.beg;
return x.end < y.end;
}
void setMeet::solve() {
sort(meet, meet + n, cmp);
int i;
ans = 1;
int last = meet[0].end;
for (i = 1; i < n; ++i) {
if (meet[i].beg >= last) {
ans++;
last = meet[i].end;
}
}
cout << ans << endl;
}
int main() {
setMeet sm;
sm.init();
sm.solve();
return 0;
} | replace | 10 | 11 | 10 | 11 | 0 | |
p02796 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
struct Sect {
int L, R;
int x, len;
int size;
};
Sect sects[100010];
int n;
bool compare(const Sect &a, const Sect &b) {
if (a.R == b.R)
return a.L < b.L;
else
return a.R < b.R;
}
int calcStart(int startP) {
int count = 1;
int nextR = sects[startP].R;
for (int i = startP + 1; i < n; i++) {
if (nextR <= sects[i].L) {
count++;
nextR = sects[i].R;
}
}
return count;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d%d", &(sects[i].x), &(sects[i].len));
sects[i].L = sects[i].x - sects[i].len;
sects[i].R = sects[i].x + sects[i].len;
sects[i].size = sects[i].R - sects[i].L + 1;
}
sort(sects, sects + n, compare);
int maxNum = 0;
for (int i = 0; i < n; i++) {
int curNum = calcStart(i);
if (curNum > maxNum)
maxNum = curNum;
}
printf("%d\n", maxNum);
return 0;
}
| #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
struct Sect {
int L, R;
int x, len;
int size;
};
Sect sects[100010];
int n;
bool compare(const Sect &a, const Sect &b) {
if (a.R == b.R)
return a.L < b.L;
else
return a.R < b.R;
}
int calcStart(int startP) {
int count = 1;
int nextR = sects[startP].R;
for (int i = startP + 1; i < n; i++) {
if (nextR <= sects[i].L) {
count++;
nextR = sects[i].R;
}
}
return count;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d%d", &(sects[i].x), &(sects[i].len));
sects[i].L = sects[i].x - sects[i].len;
sects[i].R = sects[i].x + sects[i].len;
sects[i].size = sects[i].R - sects[i].L + 1;
}
sort(sects, sects + n, compare);
int maxNum = 0;
// for (int i = 0 ; i < n ; i++) {
int curNum = calcStart(0);
if (curNum > maxNum)
maxNum = curNum;
// }
printf("%d\n", maxNum);
return 0;
}
| replace | 54 | 59 | 54 | 59 | TLE | |
p02796 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
using namespace std;
struct node {
int s;
int f;
} a[1100];
int cmp(const node &x, const node &y) { return x.f < y.f; }
int n;
int main() {
scanf("%d", &n);
int x, y;
for (int i = 1; i <= n; i++) {
scanf("%d%d", &x, &y);
int t = x, tt = y;
x = t - tt;
y = t + tt;
a[i].s = x;
a[i].f = y;
}
sort(a + 1, a + n + 1, cmp);
int tot = 1;
int now = a[1].f;
for (int i = 2; i <= n; i++) {
if (a[i].s >= now) {
tot++;
now = a[i].f;
}
}
printf("%d", tot);
return 0;
}
| #include <algorithm>
#include <cstdio>
using namespace std;
struct node {
int s;
int f;
} a[110000];
int cmp(const node &x, const node &y) { return x.f < y.f; }
int n;
int main() {
scanf("%d", &n);
int x, y;
for (int i = 1; i <= n; i++) {
scanf("%d%d", &x, &y);
int t = x, tt = y;
x = t - tt;
y = t + tt;
a[i].s = x;
a[i].f = y;
}
sort(a + 1, a + n + 1, cmp);
int tot = 1;
int now = a[1].f;
for (int i = 2; i <= n; i++) {
if (a[i].s >= now) {
tot++;
now = a[i].f;
}
}
printf("%d", tot);
return 0;
}
| replace | 6 | 7 | 6 | 7 | 0 | |
p02796 | C++ | Runtime Error | #include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <math.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
int n, t = 0, ans = 0;
const int MAX_N = 10000;
pair<int, int> robo[MAX_N];
int main() {
cin >> n;
// vector<robop> robo(n);
int p1, p2;
for (int i = 0; i < n; i++) {
cin >> p1;
cin >> p2;
robo[i].first = p1 + p2;
robo[i].second = p1 - p2;
}
sort(robo, robo + n);
// cout << "-" << endl;
for (int i = 0; i < n; i++) {
int start = robo[i].second;
// cout << start << " " << t << " ";
if (start < 0)
start = 0;
// cout << start << " " << t <<endl;
if (t <= start && start != 0) {
t = robo[i].first;
ans++;
} else if (start == 0 && ans == 0) {
t = robo[i].first;
ans++;
}
}
cout << ans << endl;
}
| #include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <math.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
int n, t = 0, ans = 0;
const int MAX_N = 100000;
pair<int, int> robo[MAX_N];
int main() {
cin >> n;
// vector<robop> robo(n);
int p1, p2;
for (int i = 0; i < n; i++) {
cin >> p1;
cin >> p2;
robo[i].first = p1 + p2;
robo[i].second = p1 - p2;
}
sort(robo, robo + n);
// cout << "-" << endl;
for (int i = 0; i < n; i++) {
int start = robo[i].second;
// cout << start << " " << t << " ";
if (start < 0)
start = 0;
// cout << start << " " << t <<endl;
if (t <= start && start != 0) {
t = robo[i].first;
ans++;
} else if (start == 0 && ans == 0) {
t = robo[i].first;
ans++;
}
}
cout << ans << endl;
}
| replace | 12 | 13 | 12 | 13 | 0 | |
p02796 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ordered_set \
tree<int, null_type, less<int>, rb_tree_tag, \
tree_order_statistics_node_update>
// order_of_key (val): returns the no. of values less than val
// find_by_order (k): returns the kth largest element.(0-based)
#define ll long long int
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define pi pair<ll, ll>
#define pii pair<ll, pi>
#define ppi pair<pi, ll>
#define ppp pair<pi, pi>
#define ex(str, a, b) \
str.substr((int)(a), min((int)str.size() - a, (int)(b - a + 1)))
#define all(X) X.begin(), X.end()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// for pair comparison function(ascending order) use return (i1.ff < i2.ff);
/* string operations :
str.substr (x,y) : returns a substring str[x],str[x+1],...str[x+y-1]
__builtin_popcount(n) : no. of set bits in n.
*/
const int M = (1 << 20) + 5;
const int md = 1e9 + 7;
priority_queue<ll, vector<ll>, greater<ll>> pq;
ll pwr(ll a, ll n, ll m) {
ll p = 1;
while (n > 0) {
if (n % 2 == 1)
p = (p * a) % m;
a = (a * a) % m;
n = n / 2;
}
return p;
}
set<pi> st;
pi ar[M];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll n, i, ans = 1, v;
cin >> n;
for (i = 1; i <= n; ++i)
cin >> ar[i].ff >> ar[i].ss;
sort(ar + 1, ar + 1 + n);
for (i = 1; i <= n; ++i) {
auto it = st.lower_bound(pi(ar[i].ss - ar[i].ff, -5));
if (it == st.end())
v = 1;
else
v = (*it).ss + 1;
auto gt = st.lower_bound(pi(-ar[i].ss - ar[i].ff, -5));
if (gt == st.end())
st.insert(pi(-ar[i].ss - ar[i].ff, v));
else if ((*gt).ss < v) {
if ((*gt).ff == -ar[i].ss - ar[i].ff)
st.erase(gt);
st.insert(pi(-ar[i].ss - ar[i].ff, v));
auto vt = st.begin();
while ((*vt) != pi(-ar[i].ss - ar[i].ff, v)) {
st.erase(vt);
auto vt = st.begin();
}
}
ans = max(ans, v);
}
cout << ans << "\n";
}
| #include <bits/stdc++.h>
#include <chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ordered_set \
tree<int, null_type, less<int>, rb_tree_tag, \
tree_order_statistics_node_update>
// order_of_key (val): returns the no. of values less than val
// find_by_order (k): returns the kth largest element.(0-based)
#define ll long long int
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define pi pair<ll, ll>
#define pii pair<ll, pi>
#define ppi pair<pi, ll>
#define ppp pair<pi, pi>
#define ex(str, a, b) \
str.substr((int)(a), min((int)str.size() - a, (int)(b - a + 1)))
#define all(X) X.begin(), X.end()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// for pair comparison function(ascending order) use return (i1.ff < i2.ff);
/* string operations :
str.substr (x,y) : returns a substring str[x],str[x+1],...str[x+y-1]
__builtin_popcount(n) : no. of set bits in n.
*/
const int M = (1 << 20) + 5;
const int md = 1e9 + 7;
priority_queue<ll, vector<ll>, greater<ll>> pq;
ll pwr(ll a, ll n, ll m) {
ll p = 1;
while (n > 0) {
if (n % 2 == 1)
p = (p * a) % m;
a = (a * a) % m;
n = n / 2;
}
return p;
}
set<pi> st;
pi ar[M];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll n, i, ans = 1, v;
cin >> n;
for (i = 1; i <= n; ++i)
cin >> ar[i].ff >> ar[i].ss;
sort(ar + 1, ar + 1 + n);
for (i = 1; i <= n; ++i) {
auto it = st.lower_bound(pi(ar[i].ss - ar[i].ff, -5));
if (it == st.end())
v = 1;
else
v = (*it).ss + 1;
auto gt = st.lower_bound(pi(-ar[i].ss - ar[i].ff, -5));
if (gt == st.end())
st.insert(pi(-ar[i].ss - ar[i].ff, v));
else if ((*gt).ss < v) {
if ((*gt).ff == -ar[i].ss - ar[i].ff)
st.erase(gt);
st.insert(pi(-ar[i].ss - ar[i].ff, v));
auto vt = st.begin();
while ((*vt) != pi(-ar[i].ss - ar[i].ff, v)) {
st.erase(vt);
vt = st.begin();
}
}
ans = max(ans, v);
}
cout << ans << "\n";
}
| replace | 81 | 82 | 81 | 82 | 0 | |
p02796 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int f[N], n, ans = 1, b[N], tot; // f[i]表示i必选的最大价值.
struct node {
int x, y;
} a[N];
struct Tree {
int l, r, dat;
#define l(x) t[x].l
#define r(x) t[x].r
#define dat(x) t[x].dat
} t[N << 3];
inline bool cmp(node x, node y) { return x.x < y.x; }
inline int raw(int x) { return lower_bound(b + 1, b + tot + 1, x) - b; }
inline void build(int p, int l, int r) {
l(p) = l, r(p) = r;
if (l == r)
return;
int mid = l + r >> 1;
build(p << 1, l, mid);
build(p << 1 | 1, mid + 1, r);
}
inline void alter(int p, int x, int k) {
if (l(p) == r(p)) {
dat(p) = max(dat(p), k);
return;
}
int mid = l(p) + r(p) >> 1;
if (x <= mid)
alter(p << 1, x, k);
else
alter(p << 1 | 1, x, k);
dat(p) = max(dat(p << 1), dat(p << 1 | 1));
}
inline int ask(int p, int l, int r) {
if (l <= l(p) && r >= r(p))
return dat(p);
int mid = l(p) + r(p) >> 1;
int ans = 0;
if (l <= mid)
ans = max(ans, ask(p << 1, l, r));
if (r >= mid + 1)
ans = max(ans, ask(p << 1 | 1, l, r));
return ans;
}
int main() {
// freopen("1.in","r",stdin);
cin >> n;
for (int i = 1; i <= n; ++i) {
int x, l;
cin >> x >> l;
b[i << 1] = a[i].x = x - l;
b[i << 1 | 1] = a[i].y = x + l;
}
sort(b + 1, b + (n << 1) + 1);
tot = unique(b + 1, b + (n << 1) + 1) - b - 1;
build(1, 1, tot);
sort(a + 1, a + n + 1, cmp);
f[1] = 1;
alter(1, raw(a[1].y), 1);
for (int i = 2; i <= n; ++i) {
f[i] = ask(1, 1, raw(a[i].x)) + 1;
alter(1, raw(a[i].y), f[i]);
// for(int j=1;j<i;++j) if(a[j].y<=a[i].x) f[i]=max(f[i],f[j]);
// f[i]++;
ans = max(ans, f[i]);
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int f[N], n, ans = 1, b[N << 1], tot; // f[i]表示i必选的最大价值.
struct node {
int x, y;
} a[N];
struct Tree {
int l, r, dat;
#define l(x) t[x].l
#define r(x) t[x].r
#define dat(x) t[x].dat
} t[N << 3];
inline bool cmp(node x, node y) { return x.x < y.x; }
inline int raw(int x) { return lower_bound(b + 1, b + tot + 1, x) - b; }
inline void build(int p, int l, int r) {
l(p) = l, r(p) = r;
if (l == r)
return;
int mid = l + r >> 1;
build(p << 1, l, mid);
build(p << 1 | 1, mid + 1, r);
}
inline void alter(int p, int x, int k) {
if (l(p) == r(p)) {
dat(p) = max(dat(p), k);
return;
}
int mid = l(p) + r(p) >> 1;
if (x <= mid)
alter(p << 1, x, k);
else
alter(p << 1 | 1, x, k);
dat(p) = max(dat(p << 1), dat(p << 1 | 1));
}
inline int ask(int p, int l, int r) {
if (l <= l(p) && r >= r(p))
return dat(p);
int mid = l(p) + r(p) >> 1;
int ans = 0;
if (l <= mid)
ans = max(ans, ask(p << 1, l, r));
if (r >= mid + 1)
ans = max(ans, ask(p << 1 | 1, l, r));
return ans;
}
int main() {
// freopen("1.in","r",stdin);
cin >> n;
for (int i = 1; i <= n; ++i) {
int x, l;
cin >> x >> l;
b[i << 1] = a[i].x = x - l;
b[i << 1 | 1] = a[i].y = x + l;
}
sort(b + 1, b + (n << 1) + 1);
tot = unique(b + 1, b + (n << 1) + 1) - b - 1;
build(1, 1, tot);
sort(a + 1, a + n + 1, cmp);
f[1] = 1;
alter(1, raw(a[1].y), 1);
for (int i = 2; i <= n; ++i) {
f[i] = ask(1, 1, raw(a[i].x)) + 1;
alter(1, raw(a[i].y), f[i]);
// for(int j=1;j<i;++j) if(a[j].y<=a[i].x) f[i]=max(f[i],f[j]);
// f[i]++;
ans = max(ans, f[i]);
}
cout << ans << endl;
} | replace | 3 | 4 | 3 | 4 | 0 | |
p02796 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int qmax(int a, int b) { return (a > b) ? a : b; }
int n;
int l[100100], r[100100];
vector<int> robot[100100];
int dp[200200];
map<int, int> mp;
set<int> s;
int main() {
scanf("%d", &n);
rep(i, n) {
int x, L;
scanf("%d %d", &x, &L);
l[i] = x - L, r[i] = x + L;
s.insert(l[i]), s.insert(r[i]);
}
int cnt = 0;
for (set<int>::iterator it = s.begin(); it != s.end(); it++, cnt++) {
mp[*it] = cnt;
}
rep(i, n) {
l[i] = mp[l[i]];
r[i] = mp[r[i]];
robot[r[i]].push_back(i);
}
memset(dp, 0, sizeof(dp));
for (int i = 1; i < cnt; i++) {
dp[i] = dp[i - 1];
rep(j, robot[i].size()) { dp[i] = qmax(dp[i], dp[l[robot[i][j]]] + 1); }
}
cout << dp[cnt - 1] << endl;
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int qmax(int a, int b) { return (a > b) ? a : b; }
int n;
int l[100100], r[100100];
vector<int> robot[200200];
int dp[200200];
map<int, int> mp;
set<int> s;
int main() {
scanf("%d", &n);
rep(i, n) {
int x, L;
scanf("%d %d", &x, &L);
l[i] = x - L, r[i] = x + L;
s.insert(l[i]), s.insert(r[i]);
}
int cnt = 0;
for (set<int>::iterator it = s.begin(); it != s.end(); it++, cnt++) {
mp[*it] = cnt;
}
rep(i, n) {
l[i] = mp[l[i]];
r[i] = mp[r[i]];
robot[r[i]].push_back(i);
}
memset(dp, 0, sizeof(dp));
for (int i = 1; i < cnt; i++) {
dp[i] = dp[i - 1];
rep(j, robot[i].size()) { dp[i] = qmax(dp[i], dp[l[robot[i][j]]] + 1); }
}
cout << dp[cnt - 1] << endl;
return 0;
} | replace | 8 | 9 | 8 | 9 | 0 | |
p02796 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
inline int read() {
int n = 0;
bool b = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-')
b = 0;
c = getchar();
}
while (c >= '0' && c <= '9')
n = (n << 3) + (n << 1) + (c ^ 48), c = getchar();
return b ? n : -n;
}
struct dalao {
int left;
int right;
} a[50100];
bool juruo(dalao x, dalao y) { return x.right < y.right; }
int main() {
int n, s = 1, d;
cin >> n;
for (int i = 1; i <= n; i++) {
int t1 = read(), t2 = read();
a[i].left = t1 - t2, a[i].right = t1 + t2;
}
sort(a + 1, a + n + 1, juruo);
d = a[1].right;
for (int i = 2; i <= n; i++) {
if (a[i].left >= d) {
d = a[i].right;
s++;
}
}
cout << s;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
inline int read() {
int n = 0;
bool b = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-')
b = 0;
c = getchar();
}
while (c >= '0' && c <= '9')
n = (n << 3) + (n << 1) + (c ^ 48), c = getchar();
return b ? n : -n;
}
struct dalao {
int left;
int right;
} a[100100];
bool juruo(dalao x, dalao y) { return x.right < y.right; }
int main() {
int n, s = 1, d;
cin >> n;
for (int i = 1; i <= n; i++) {
int t1 = read(), t2 = read();
a[i].left = t1 - t2, a[i].right = t1 + t2;
}
sort(a + 1, a + n + 1, juruo);
d = a[1].right;
for (int i = 2; i <= n; i++) {
if (a[i].left >= d) {
d = a[i].right;
s++;
}
}
cout << s;
return 0;
}
| replace | 18 | 19 | 18 | 19 | 0 | |
p02796 | C++ | Time Limit Exceeded | /**
* Libraries
*/
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
/**
* macros
*/
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define repx(i, s, e, d) for (int i = s, i##_end = (e); i < i##_end; i += d)
#define repxr(i, s, e, d) for (int i = s, i##_end = (e); i > i##_end; i += d)
#define rept(n) \
for (int ___i___ = 0, i##_len = (n); ___i___ < i##_len; ++___i___)
#define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)
#define SZ(x) ((int)(x).size())
#define ZERO(a) memset(a, 0, sizeof(a))
#define BIT(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define CEIL(x, y) (((x) + (y)-1) / (y))
/**
* helpers
*/
typedef long long int lli;
typedef pair<int, int> ii;
#define int long long int
// struct S{
// bool operator<(const S& x) const {
// return ;
// }
// };
struct Robot {
int x, l;
int deleted;
inline int right() { return x + l; }
inline int left() { return x - l; }
bool operator<(const Robot &r) const { return this->x - this->l < r.x - r.l; }
};
signed main() {
int N;
cin >> N;
Robot robots[100000];
rep(i, N) {
int x, l;
cin >> x >> l;
robots[i].x = x;
robots[i].l = l;
robots[i].deleted = 0;
}
sort(robots, robots + N);
ii rL[100000];
rep(i, N) { rL[i] = ii(i, robots[i].left()); }
sort(rL, rL + N,
[](const ii &x, const ii &y) { return x.second < y.second; });
rep(i, N) {
if (robots[i].deleted == 1)
continue;
int rl = robots[i].left(), rr = robots[i].right();
auto li = lower_bound(rL, rL + N, ii(-1, rl), [](const ii &x, const ii &y) {
return x.second < y.second;
});
auto ri = lower_bound(rL, rL + N, ii(-1, rr), [](const ii &x, const ii &y) {
return x.second < y.second;
});
bool deleteMe = false;
for (auto it = li; it != ri; ++it) {
int j = it->first;
if (i == j)
continue;
if (robots[j].deleted == 1)
continue;
if (robots[j].right() < rr) {
deleteMe = true;
}
}
if (deleteMe) {
robots[i].deleted = 1;
continue;
}
for (auto it = li; it != ri; ++it) {
int j = it->first;
if (i == j)
continue;
if (robots[j].deleted == 1)
continue;
if (rr <= robots[j].right()) {
robots[j].deleted = 1;
}
}
}
int ans = 0;
rep(i, N) {
if (robots[i].deleted == 0)
ans++;
}
cout << ans << endl;
return 0;
}
| /**
* Libraries
*/
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
/**
* macros
*/
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define repx(i, s, e, d) for (int i = s, i##_end = (e); i < i##_end; i += d)
#define repxr(i, s, e, d) for (int i = s, i##_end = (e); i > i##_end; i += d)
#define rept(n) \
for (int ___i___ = 0, i##_len = (n); ___i___ < i##_len; ++___i___)
#define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)
#define SZ(x) ((int)(x).size())
#define ZERO(a) memset(a, 0, sizeof(a))
#define BIT(n) (1LL << (n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define CEIL(x, y) (((x) + (y)-1) / (y))
/**
* helpers
*/
typedef long long int lli;
typedef pair<int, int> ii;
#define int long long int
// struct S{
// bool operator<(const S& x) const {
// return ;
// }
// };
struct Robot {
int x, l;
int deleted;
inline int right() { return x + l; }
inline int left() { return x - l; }
bool operator<(const Robot &r) const { return this->x - this->l < r.x - r.l; }
};
signed main() {
int N;
cin >> N;
Robot robots[100000];
rep(i, N) {
int x, l;
cin >> x >> l;
robots[i].x = x;
robots[i].l = l;
robots[i].deleted = 0;
}
sort(robots, robots + N);
ii rL[100000];
rep(i, N) { rL[i] = ii(i, robots[i].left()); }
sort(rL, rL + N,
[](const ii &x, const ii &y) { return x.second < y.second; });
rep(i, N) {
if (robots[i].deleted == 1)
continue;
int rl = robots[i].left(), rr = robots[i].right();
auto li = lower_bound(rL, rL + N, ii(-1, rl), [](const ii &x, const ii &y) {
return x.second < y.second;
});
auto ri = lower_bound(rL, rL + N, ii(-1, rr), [](const ii &x, const ii &y) {
return x.second < y.second;
});
bool deleteMe = false;
for (auto it = li; it != ri; ++it) {
int j = it->first;
if (i == j)
continue;
if (robots[j].deleted == 1)
continue;
if (robots[j].right() < rr) {
deleteMe = true;
break;
}
}
if (deleteMe) {
robots[i].deleted = 1;
continue;
}
for (auto it = li; it != ri; ++it) {
int j = it->first;
if (i == j)
continue;
if (robots[j].deleted == 1)
continue;
if (rr <= robots[j].right()) {
robots[j].deleted = 1;
}
}
}
int ans = 0;
rep(i, N) {
if (robots[i].deleted == 0)
ans++;
}
cout << ans << endl;
return 0;
}
| insert | 94 | 94 | 94 | 95 | TLE | |
p02796 | C++ | Runtime Error | #include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define ss(x) (int)x.size()
#define pb push_back
#define ll long long
#define ld double
#define cat(x) cerr << #x << " = " << x << endl
#define FOR(i, n) for (int i = 0; i < n; ++i)
#define DOW(i, n) for (int i = n - 1; 0 <= i; --i)
#define boost cin.tie(0), ios_base::sync_with_stdio(0);
using namespace std;
const int nax = 1e5 + 111;
int n;
pair<int, int> t[nax];
vector<int> vec;
int pref[nax];
map<int, int> mapa;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d %d", &t[i].fi, &t[i].se);
mapa[t[i].fi] = t[i].se;
}
sort(t + 1, t + n + 1);
for (int i = 1; i <= n; ++i) {
vec.pb(t[i].fi);
vec.pb(t[i].fi + t[i].se);
if (t[i].fi - t[i].se >= 0)
vec.pb(t[i].fi - t[i].se);
}
sort(vec.begin(), vec.end());
vec.erase(unique(vec.begin(), vec.end()), vec.end());
for (int i = 0; i < ss(vec); ++i) {
if (i)
pref[i] = max(pref[i], pref[i - 1]);
if (mapa.find(vec[i]) == mapa.end())
continue;
int x = vec[i];
int l = mapa[x];
int prawo = (int)(lower_bound(vec.begin(), vec.end(), x + l) - vec.begin());
int lewo =
(int)(upper_bound(vec.begin(), vec.end(), x - l) - vec.begin()) - 1;
int ja = 1;
if (lewo >= 0)
ja += pref[lewo];
// cout << x << "" << ja << " " << lewo << " " << prawo << " " << i <<
//endl;
pref[prawo] = max(pref[prawo], ja);
}
printf("%d", pref[ss(vec) - 1]);
return 0;
}
| #include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define ss(x) (int)x.size()
#define pb push_back
#define ll long long
#define ld double
#define cat(x) cerr << #x << " = " << x << endl
#define FOR(i, n) for (int i = 0; i < n; ++i)
#define DOW(i, n) for (int i = n - 1; 0 <= i; --i)
#define boost cin.tie(0), ios_base::sync_with_stdio(0);
using namespace std;
const int nax = 4e5 + 111;
int n;
pair<int, int> t[nax];
vector<int> vec;
int pref[nax];
map<int, int> mapa;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d %d", &t[i].fi, &t[i].se);
mapa[t[i].fi] = t[i].se;
}
sort(t + 1, t + n + 1);
for (int i = 1; i <= n; ++i) {
vec.pb(t[i].fi);
vec.pb(t[i].fi + t[i].se);
if (t[i].fi - t[i].se >= 0)
vec.pb(t[i].fi - t[i].se);
}
sort(vec.begin(), vec.end());
vec.erase(unique(vec.begin(), vec.end()), vec.end());
for (int i = 0; i < ss(vec); ++i) {
if (i)
pref[i] = max(pref[i], pref[i - 1]);
if (mapa.find(vec[i]) == mapa.end())
continue;
int x = vec[i];
int l = mapa[x];
int prawo = (int)(lower_bound(vec.begin(), vec.end(), x + l) - vec.begin());
int lewo =
(int)(upper_bound(vec.begin(), vec.end(), x - l) - vec.begin()) - 1;
int ja = 1;
if (lewo >= 0)
ja += pref[lewo];
// cout << x << "" << ja << " " << lewo << " " << prawo << " " << i <<
//endl;
pref[prawo] = max(pref[prawo], ja);
}
printf("%d", pref[ss(vec) - 1]);
return 0;
}
| replace | 15 | 16 | 15 | 16 | 0 | |
p02796 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
using namespace std;
struct line {
int l, r;
} a[101];
bool operator<(const line &a, const line &b) { return a.r < b.r; }
int main() {
int n, tot = 1;
cin >> n;
for (int i = 1, x, L; i <= n; i++) {
cin >> x >> L;
a[i].l = x - L;
a[i].r = x + L;
}
sort(a + 1, a + n + 1);
int end = a[1].r;
for (int i = 2; i <= n; i++) {
if (a[i].l >= end) {
tot++;
end = a[i].r;
}
}
cout << tot << endl;
return 0;
} | #include <algorithm>
#include <iostream>
using namespace std;
struct line {
int l, r;
} a[100010];
bool operator<(const line &a, const line &b) { return a.r < b.r; }
int main() {
int n, tot = 1;
cin >> n;
for (int i = 1, x, L; i <= n; i++) {
cin >> x >> L;
a[i].l = x - L;
a[i].r = x + L;
}
sort(a + 1, a + n + 1);
int end = a[1].r;
for (int i = 2; i <= n; i++) {
if (a[i].l >= end) {
tot++;
end = a[i].r;
}
}
cout << tot << endl;
return 0;
} | replace | 6 | 7 | 6 | 7 | 0 | |
p02796 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
struct node {
int s, e;
} list[40010];
bool cmp(node x, node y) { return x.e < y.e; }
int main() {
int n;
while (~scanf("%d", &n)) {
int x, l;
for (int i = 0; i < n; i++) {
scanf("%d %d", &x, &l);
list[i].s = x - l;
list[i].e = x + l;
}
/* for(int i=0;i<n;i++){
cout<<list[i].s<<"*"<<list[i].e<<endl;
}*/
sort(list, list + n, cmp);
int ans = 1, s = list[0].e;
for (int i = 1; i < n; i++) {
if (list[i].s >= s) {
ans++;
s = list[i].e;
}
}
printf("%d\n", ans);
}
return 0;
} | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
struct node {
int s, e;
} list[100005];
bool cmp(node x, node y) { return x.e < y.e; }
int main() {
int n;
while (~scanf("%d", &n)) {
int x, l;
for (int i = 0; i < n; i++) {
scanf("%d %d", &x, &l);
list[i].s = x - l;
list[i].e = x + l;
}
/* for(int i=0;i<n;i++){
cout<<list[i].s<<"*"<<list[i].e<<endl;
}*/
sort(list, list + n, cmp);
int ans = 1, s = list[0].e;
for (int i = 1; i < n; i++) {
if (list[i].s >= s) {
ans++;
s = list[i].e;
}
}
printf("%d\n", ans);
}
return 0;
} | replace | 7 | 8 | 7 | 8 | 0 | |
p02796 | C++ | Runtime Error | #include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define MOD 1000000007
using namespace std;
bool compare(pair<int, int> a, pair<int, int> b) { return a.first < b.first; }
int main() {
int n;
cin >> n;
vector<pair<int, int>> arm(n);
vector<pair<int, int>> range(n);
for (int i = 0; i < n; i++) {
cin >> arm[i].first >> arm[i].second;
range[i].first = arm[i].first + arm[i].second;
range[i].second = arm[i].first - arm[i].second;
}
int ans = 1;
int g;
sort(all(range));
if (n == 1)
return 1;
g = range[0].first;
for (int i = 1; i < n; i++) {
if (g <= range[i].second) {
ans++;
g = range[i].first;
}
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define MOD 1000000007
using namespace std;
bool compare(pair<int, int> a, pair<int, int> b) { return a.first < b.first; }
int main() {
int n;
cin >> n;
vector<pair<int, int>> arm(n);
vector<pair<int, int>> range(n);
for (int i = 0; i < n; i++) {
cin >> arm[i].first >> arm[i].second;
range[i].first = arm[i].first + arm[i].second;
range[i].second = arm[i].first - arm[i].second;
}
int ans = 1;
int g;
sort(all(range));
if (n == 1) {
cout << 1 << endl;
return 0;
}
g = range[0].first;
for (int i = 1; i < n; i++) {
if (g <= range[i].second) {
ans++;
g = range[i].first;
}
}
cout << ans << endl;
return 0;
}
| replace | 25 | 27 | 25 | 29 | 0 | |
p02796 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, l, r) for (register int i = l; i <= r; i++)
#define per(i, r, l) for (register int i = r; i >= l; i--)
#define srep(i, l, r) for (register int i = l; i < r; i++)
#define sper(i, r, l) for (register int i = r; i > l; i--)
#define maxn 200020
#define ll long long int
using namespace std;
int n;
struct ele {
int id, l, r;
ele(int id, int l, int r) : id(id), l(l), r(r) {}
ele() {}
bool operator<(const ele b) const {
return (l == b.l) ? ((r == b.r) ? (id < b.id) : r < b.r) : l < b.l;
}
} p[maxn];
bool vis[maxn];
int dp[maxn];
int DP(int pos) {
if (pos == n + 1)
return 0;
if (vis[pos])
return dp[pos];
int &ans = dp[pos];
return ans = max(
DP(pos + 1),
1 + DP(lower_bound(p + 1, p + 1 + n, ele(-1, p[pos].r, p[pos].r)) -
p));
}
int main() {
// freopen("B.in", "r", stdin);
int x, y;
scanf("%d", &n);
rep(i, 1, n) scanf("%d%d", &x, &y), p[i] = ele(i, x - y, x + y);
sort(p + 1, p + 1 + n);
printf("%d", DP(1));
return 0;
} | #include <bits/stdc++.h>
#define rep(i, l, r) for (register int i = l; i <= r; i++)
#define per(i, r, l) for (register int i = r; i >= l; i--)
#define srep(i, l, r) for (register int i = l; i < r; i++)
#define sper(i, r, l) for (register int i = r; i > l; i--)
#define maxn 200020
#define ll long long int
using namespace std;
int n;
struct ele {
int id, l, r;
ele(int id, int l, int r) : id(id), l(l), r(r) {}
ele() {}
bool operator<(const ele b) const {
return (l == b.l) ? ((r == b.r) ? (id < b.id) : r < b.r) : l < b.l;
}
} p[maxn];
bool vis[maxn];
int dp[maxn];
int DP(int pos) {
if (pos == n + 1)
return 0;
if (vis[pos])
return dp[pos];
vis[pos] = 1;
int &ans = dp[pos];
return ans = max(
DP(pos + 1),
1 + DP(lower_bound(p + 1, p + 1 + n, ele(-1, p[pos].r, p[pos].r)) -
p));
}
int main() {
// freopen("B.in", "r", stdin);
int x, y;
scanf("%d", &n);
rep(i, 1, n) scanf("%d%d", &x, &y), p[i] = ele(i, x - y, x + y);
sort(p + 1, p + 1 + n);
printf("%d", DP(1));
return 0;
} | insert | 27 | 27 | 27 | 28 | TLE | |
p02796 | C++ | Runtime Error |
#include <algorithm>
#include <cstring>
#include <ctype.h>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
struct aera {
int start;
int end;
};
bool cmp(aera a, aera b) {
if (a.end != b.end)
return a.end < b.end;
else
return a.start > b.start;
}
int main() {
aera a1[10010];
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
a1[i].start = x - y;
a1[i].end = x + y;
}
sort(a1, a1 + n, cmp);
int i, count = 1;
int lastend = a1[0].end;
for (i = 0; i < n; i++) {
if (lastend <= a1[i].start) {
count++;
lastend = a1[i].end;
}
}
printf("%d\n", count);
return 0;
}
|
#include <algorithm>
#include <cstring>
#include <ctype.h>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
struct aera {
int start;
int end;
};
bool cmp(aera a, aera b) {
if (a.end != b.end)
return a.end < b.end;
else
return a.start > b.start;
}
int main() {
aera a1[100005];
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
a1[i].start = x - y;
a1[i].end = x + y;
}
sort(a1, a1 + n, cmp);
int i, count = 1;
int lastend = a1[0].end;
for (i = 0; i < n; i++) {
if (lastend <= a1[i].start) {
count++;
lastend = a1[i].end;
}
}
printf("%d\n", count);
return 0;
}
| replace | 26 | 27 | 26 | 27 | 0 | |
p02796 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
int n;
cin >> n;
vector<pair<int, int>> xl(n);
vector<pair<int, int>> state(n);
for (int i = 0; i < n; i++) {
cin >> xl[i].first >> xl[i].second;
state[i].first = xl[i].first + xl[i].second;
state[i].second = xl[i].first - xl[i].second;
}
int count = 0;
sort(state.begin(), state.end());
for (int i = 0; i < state.size(); i++) {
if (state[i + 1].second < state[i].first) {
count++;
state[i + 1] = state[i];
}
}
cout << n - count << endl;
return 0;
} | #include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
int n;
cin >> n;
vector<pair<int, int>> xl(n);
vector<pair<int, int>> state(n);
for (int i = 0; i < n; i++) {
cin >> xl[i].first >> xl[i].second;
state[i].first = xl[i].first + xl[i].second;
state[i].second = xl[i].first - xl[i].second;
}
int count = 0;
sort(state.begin(), state.end());
for (int i = 0; i < state.size() - 1; i++) {
if (state[i + 1].second < state[i].first) {
count++;
state[i + 1] = state[i];
}
}
cout << n - count << endl;
return 0;
} | replace | 20 | 21 | 20 | 21 | 0 | |
p02796 | C++ | Time Limit Exceeded | #include <cmath>
#include <iostream>
#include <algorithm>
#include <array>
#include <limits>
#include <queue>
#include <unordered_set>
#include <vector>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> x(n);
vector<int> l(n);
vector<tuple<int, int, int>> intervals(n);
for (int i = 0; i < n; ++i) {
cin >> x[i] >> l[i];
intervals[i] = make_tuple(i, x[i] - l[i], x[i] + l[i]);
}
sort(intervals.begin(), intervals.end(),
[](tuple<int, int, int> x1, tuple<int, int, int> x2) {
return get<2>(x1) < get<2>(x2);
});
int cnt = 0;
while (!intervals.empty()) {
auto interval = intervals[0];
cnt += 1;
std::vector<tuple<int, int, int>> new_intervals;
std::copy_if(intervals.begin(), intervals.end(),
std::back_inserter(new_intervals),
[interval](tuple<int, int, int> item) {
return get<1>(item) >= get<2>(interval);
});
intervals = new_intervals;
}
cout << cnt << endl;
}
| #include <cmath>
#include <iostream>
#include <algorithm>
#include <array>
#include <limits>
#include <queue>
#include <unordered_set>
#include <vector>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> x(n);
vector<int> l(n);
vector<tuple<int, int, int>> intervals(n);
for (int i = 0; i < n; ++i) {
cin >> x[i] >> l[i];
intervals[i] = make_tuple(i, x[i] - l[i], x[i] + l[i]);
}
sort(intervals.begin(), intervals.end(),
[](tuple<int, int, int> x1, tuple<int, int, int> x2) {
return get<2>(x1) < get<2>(x2);
});
int cnt = 0;
int idx = 0;
while (idx < n) {
auto interval = intervals[idx];
++idx;
++cnt;
for (; idx < n;) {
if (get<2>(interval) > get<1>(intervals[idx])) {
++idx;
} else {
break;
}
}
}
cout << cnt << endl;
}
| replace | 27 | 37 | 27 | 39 | TLE | |
p02797 | C++ | Runtime Error | #define ll long long
#define MOD 1000000007
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define set2d(array, val, m, n) memset(array, val, sizeof(array[0][0]) * m * n);
#include <bits/stdc++.h>
using namespace std;
ll gcd(ll a, ll b) {
if (!b)
return a;
return gcd(b, a % b);
}
ll power(ll x, ll y, ll p) {
ll res = 1;
x %= p;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
ll po(ll base, ll powerRaised) {
if (powerRaised != 0)
return (base * po(base, powerRaised - 1));
else
return 1;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif //*/
ll n, k, s;
cin >> n >> k >> s;
for (int i = 1; i <= k; i++)
cout << s << " ";
if (s == 1) {
for (int i = k + 1; i <= n; i++)
cout << "2 ";
return 0;
} else {
ll start = 2;
if (s == 2)
start = 3;
ll i = k;
while (i <= n) {
if (i == n)
break;
cout << start << " ";
i++;
if (i == n)
break;
cout << s - start + 1 << " ";
i++;
}
}
cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
return 0;
} | #define ll long long
#define MOD 1000000007
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define set2d(array, val, m, n) memset(array, val, sizeof(array[0][0]) * m * n);
#include <bits/stdc++.h>
using namespace std;
ll gcd(ll a, ll b) {
if (!b)
return a;
return gcd(b, a % b);
}
ll power(ll x, ll y, ll p) {
ll res = 1;
x %= p;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
ll po(ll base, ll powerRaised) {
if (powerRaised != 0)
return (base * po(base, powerRaised - 1));
else
return 1;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif //*/
ll n, k, s;
cin >> n >> k >> s;
for (int i = 1; i <= k; i++)
cout << s << " ";
if (s == 1) {
for (int i = k + 1; i <= n; i++)
cout << "2 ";
return 0;
} else {
ll start = 2;
if (s == 2)
start = 3;
ll i = k;
while (i <= n) {
if (i == n)
break;
cout << start << " ";
i++;
if (i == n)
break;
cout << s - start + 1 << " ";
i++;
}
}
cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
return 0;
} | replace | 35 | 39 | 35 | 39 | TLE | |
p02797 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <unistd.h>
using namespace std;
using i64 = std::int64_t;
namespace io {
constexpr int is = 1 << 17;
char ib[is], *ip = ib + is, it;
inline char getc() {
if (ip == ib + is) {
read(STDIN_FILENO, ib, is);
ip = ib;
}
return *ip++;
}
inline i64 scan() {
i64 r = 0;
if (ip + 16 > ib + is)
while ((it = getc()) & 16)
r = r * 10 + it - '0';
else
while ((it = *ip++) & 16)
r = r * 10 + it - '0';
return r;
}
constexpr int os = 1 << 17;
char ob[os], *op = ob, ot;
inline void putc(const char c) {
*op++ = c;
if (op == ob + os) {
write(STDOUT_FILENO, ob, os);
op = ob;
}
}
/* x shoud be greater than or equal to zero */
inline void print(i64 x) {
char d[20];
int i = 0;
while (true) {
d[i] = x % 10 + 48;
x /= 10;
++i;
if (x <= 0)
break;
}
while (i--) {
putc(d[i]);
}
}
inline void flush() { write(STDOUT_FILENO, ob, op - ob); }
} // namespace io
signed main() {
i64 n = io::scan();
i64 k = io::scan();
i64 s = io::scan();
for (int i = 0; i < n; i++) {
io::print(i % k ? 7 : s);
io::putc(' ');
}
io::flush();
} | #include <bits/stdc++.h>
#include <unistd.h>
using namespace std;
using i64 = std::int64_t;
namespace io {
constexpr int is = 1 << 17;
char ib[is], *ip = ib + is, it;
inline char getc() {
if (ip == ib + is) {
read(STDIN_FILENO, ib, is);
ip = ib;
}
return *ip++;
}
inline i64 scan() {
i64 r = 0;
if (ip + 16 > ib + is)
while ((it = getc()) & 16)
r = r * 10 + it - '0';
else
while ((it = *ip++) & 16)
r = r * 10 + it - '0';
return r;
}
constexpr int os = 1 << 17;
char ob[os], *op = ob, ot;
inline void putc(const char c) {
*op++ = c;
if (op == ob + os) {
write(STDOUT_FILENO, ob, os);
op = ob;
}
}
/* x shoud be greater than or equal to zero */
inline void print(i64 x) {
char d[20];
int i = 0;
while (true) {
d[i] = x % 10 + 48;
x /= 10;
++i;
if (x <= 0)
break;
}
while (i--) {
putc(d[i]);
}
}
inline void flush() { write(STDOUT_FILENO, ob, op - ob); }
} // namespace io
signed main() {
i64 n = io::scan();
i64 k = io::scan();
i64 s = io::scan();
for (int i = 0; i < n; i++) {
io::print(i < k ? s : 7);
io::putc(' ');
}
io::flush();
} | replace | 58 | 59 | 58 | 59 | 0 | |
p02797 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <unistd.h>
char buf[1000000];
signed main() {
int n, k;
char s[11];
scanf("%d %d %s", &n, &k, s);
char *p = buf;
size_t len = strlen(s);
s[len++] = ' ';
int i = 0;
for (; i < k; i++) {
strcpy(p, s);
p += len;
}
for (; i < n; i++) {
strcpy(p, "7 ");
p += 2;
}
write(STDOUT_FILENO, buf, p - buf);
}
| #include <bits/stdc++.h>
#include <unistd.h>
char buf[1000000];
signed main() {
int n, k;
char s[11];
scanf("%d %d %s", &n, &k, s);
char *p = buf;
size_t len = strlen(s);
s[len++] = ' ';
int i = 0;
for (; i < k; i++) {
strcpy(p, s);
p += len;
if (p - buf > 10000) {
write(STDOUT_FILENO, buf, p - buf);
p = buf;
}
}
for (; i < n; i++) {
strcpy(p, "7 ");
p += 2;
}
write(STDOUT_FILENO, buf, p - buf);
}
| insert | 15 | 15 | 15 | 19 | 0 | |
p02797 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define per(i, a, b) for (int i = b - 1; i >= a; i--)
#define ifor(n) for (int i = 0; i < n; i++)
#define jfor(n) for (int j = 0; j < n; j++)
#define Ifor(n) for (int i = 1; i < n; i++)
#define Jfor(n) for (int j = 1; j < n; j++)
const int inf = 0x3f3f3f3f;
int main() {
int n, k, s;
cin >> n >> k >> s;
n = n - k;
while (k--)
printf("%d ", s);
if (s > 100000)
s = 1;
else
s++;
n--;
while (n--)
printf("%d ", s);
cout << s << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define per(i, a, b) for (int i = b - 1; i >= a; i--)
#define ifor(n) for (int i = 0; i < n; i++)
#define jfor(n) for (int j = 0; j < n; j++)
#define Ifor(n) for (int i = 1; i < n; i++)
#define Jfor(n) for (int j = 1; j < n; j++)
const int inf = 0x3f3f3f3f;
int main() {
int n, k, s;
cin >> n >> k >> s;
ifor(k) cout << s << " ";
if (s < 1000000000) {
n = n - k;
ifor(n) cout << s + 1 << " ";
} else {
n = n - k;
ifor(n) cout << s - 1 << " ";
}
cout << endl;
} | replace | 14 | 26 | 14 | 23 | TLE | |
p02797 | C++ | Runtime Error | #include <bits/stdc++.h>
#define FIXED_FLOAT(x) std::fixed << std::setprecision(2) << (x)
#define what_is(x) cerr << #x << " is " << x << endl;
#define error(args...) \
{ \
string _s = #args; \
replace(_s.begin(), _s.end(), ',', ' '); \
stringstream _ss(_s); \
istream_iterator<string> _it(_ss); \
err(_it, args); \
}
#define rep(i, begin, end) \
for (__typeof(end) i = (begin) - ((begin) > (end)); \
i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define inf std::numeric_limits<int>::max();
#define highest_2_pow(n) \
__builtin_clzll((long long)0) - __builtin_clzll((long long)n) - 1
//-----------------------------------------------------------------------------------------------------
#define lli long long int
#define int long long
#define all(x) (x).begin(), (x).end()
#define uniq(v) (v).erase(unique(all(v)), (v).end())
#define sz(x) (int)((x).size())
#define fr first
#define sc second
#define pii pair<int, int>
#define vi vector<int>
#define vlli vector<long long int>
#define si set<int>
#define slli set<long long int>
#define mt make_tuple
#define mp make_pair
#define eb emplace_back
#define mem1(a) memset(a, -1, sizeof(a))
#define mem0(a) memset(a, 0, sizeof(a))
#define ppc __builtin_popcount
#define ppcll __builtin_popcountll
//---------------------------------------------------
using namespace std;
// GCD and LCM are available
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << endl;
err(++it, args...);
}
void faster() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
void solve() {
int n, k, s;
cin >> n >> k >> s;
vi v(n);
int arr[2] = {s / 2, s - (s / 2)};
rep(i, 0, k + 1) v[i] = arr[i % 2];
rep(i, k + 1, n) v[i] = arr[i % 2] + 2 + (arr[i % 2] + 2 == s);
for (auto e : v)
cout << e << " ";
cout << "\n";
}
signed main() {
faster();
int t = 1;
// cin>>t;
while (t--)
solve();
} | #include <bits/stdc++.h>
#define FIXED_FLOAT(x) std::fixed << std::setprecision(2) << (x)
#define what_is(x) cerr << #x << " is " << x << endl;
#define error(args...) \
{ \
string _s = #args; \
replace(_s.begin(), _s.end(), ',', ' '); \
stringstream _ss(_s); \
istream_iterator<string> _it(_ss); \
err(_it, args); \
}
#define rep(i, begin, end) \
for (__typeof(end) i = (begin) - ((begin) > (end)); \
i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define inf std::numeric_limits<int>::max();
#define highest_2_pow(n) \
__builtin_clzll((long long)0) - __builtin_clzll((long long)n) - 1
//-----------------------------------------------------------------------------------------------------
#define lli long long int
#define int long long
#define all(x) (x).begin(), (x).end()
#define uniq(v) (v).erase(unique(all(v)), (v).end())
#define sz(x) (int)((x).size())
#define fr first
#define sc second
#define pii pair<int, int>
#define vi vector<int>
#define vlli vector<long long int>
#define si set<int>
#define slli set<long long int>
#define mt make_tuple
#define mp make_pair
#define eb emplace_back
#define mem1(a) memset(a, -1, sizeof(a))
#define mem0(a) memset(a, 0, sizeof(a))
#define ppc __builtin_popcount
#define ppcll __builtin_popcountll
//---------------------------------------------------
using namespace std;
// GCD and LCM are available
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << endl;
err(++it, args...);
}
void faster() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
void solve() {
int n, k, s;
cin >> n >> k >> s;
vi v(n);
int arr[2] = {max(1ll, s / 2), s - (s / 2)};
if (k == n)
fill(all(v), s);
else {
rep(i, 0, s == 1 ? k : k + 1) v[i] = arr[i % 2];
rep(i, s == 1 ? k : k + 1, n) v[i] = arr[i % 2] + 2 + (arr[i % 2] + 2 == s);
}
for (auto e : v)
cout << e << " ";
cout << "\n";
}
signed main() {
faster();
int t = 1;
// cin>>t;
while (t--)
solve();
} | replace | 55 | 58 | 55 | 62 | 0 | |
p02797 | C++ | Runtime Error | #pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define int long long // 禁忌
using ll = long long;
using lld = long double;
#define P pair<ll, ll>
#define Vi vector<ll>
#define VVi vector<vector<ll>>
#define Vd vector<double>
#define Vb vector<bool>
#define Vs vector<string>
#define Vc vector<char>
#define M map<ll, ll>
#define S set<ll>
#define PQ priority_queue<ll>
#define PQG priority_queue < ll, V, greater<ll>
// 重み付き辺
template <typename T> struct edge {
int src, to;
T cost;
edge(int to, T cost) : src(-1), to(to), cost(cost) {}
edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
edge &operator=(const int &x) {
to = x;
return *this;
}
operator int() const { return to; }
};
// 重み付き辺集合
template <typename T> using Edges = vector<edge<T>>;
// 重み付きグラフ
template <typename T> using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = VVi; // 重みなしグラフ
// 距離行列
template <typename T> using Matrix = vector<vector<T>>;
const int MOD = 1000000007;
const int INF = 1061109567;
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define PER(i, n) for (int i = (n - 1); i >= 0; --i)
#define ALL(V) (V).begin(), (V).end()
#define SORT(V) sort(ALL(V)) // 小さい方からソート
#define REV(V) reverse(ALL(V)) // リバース
#define RSORT(V) \
SORT(V); \
REV(V) // 大きい方からソート
#define NEXP(V) next_permutation(ALL(V)) // 順列
#define pb(n) emplace_back(n)
#define popb pop_back()
#define endl '\n'
#define Endl '\n'
#define DUMP(x) cout << #x << " = " << (x) << endl
// UnionFind
class UnionFind {
public:
vector<int> par;
vector<int> siz;
UnionFind(int sz_) : par(sz_), siz(sz_, 1LL) {
for (int i = 0; i < sz_; ++i)
par[i] = i;
}
void init(ll sz_) {
par.resize(sz_);
siz.assign(sz_, 1LL);
for (int i = 0; i < sz_; ++i)
par[i] = i;
}
int root(int x) {
while (par[x] != x) {
x = par[x] = par[par[x]];
}
return x;
}
bool merge(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return false;
if (siz[x] < siz[y])
swap(x, y);
siz[x] += siz[y];
par[y] = x;
return true;
}
bool issame(int x, int y) { return root(x) == root(y); }
int size(int x) { return siz[root(x)]; }
};
// modint
template <int mod> struct ModInt {
int x;
ModInt() : x(0) {}
ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
ModInt &operator+=(const ModInt &p) {
if ((x += p.x) >= mod)
x -= mod;
return *this;
}
ModInt &operator-=(const ModInt &p) {
if ((x += mod - p.x) >= mod)
x -= mod;
return *this;
}
ModInt &operator*=(const ModInt &p) {
x = (int)(1LL * x * p.x % mod);
return *this;
}
ModInt &operator/=(const ModInt &p) {
*this *= p.inverse();
return *this;
}
ModInt operator-() const { return ModInt(-x); }
ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
bool operator==(const ModInt &p) const { return x == p.x; }
bool operator!=(const ModInt &p) const { return x != p.x; }
ModInt inverse() const {
int a = x, b = mod, u = 1, v = 0, t;
while (b > 0) {
t = a / b;
swap(a -= t * b, b);
swap(u -= t * v, v);
}
return ModInt(u);
}
ModInt pow(int64_t n) const {
ModInt ret(1), mul(x);
while (n > 0) {
if (n & 1)
ret *= mul;
mul *= mul;
n >>= 1;
}
return ret;
}
friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }
friend istream &operator>>(istream &is, ModInt &a) {
int64_t t;
is >> t;
a = ModInt<mod>(t);
return (is);
}
static int get_mod() { return mod; }
};
using modint = ModInt<MOD>;
// dijkstra O(E logV)
template <typename T> vector<T> dijkstra(WeightedGraph<T> &g, int s) {
const auto INF2 = numeric_limits<T>::max();
vector<T> dist(g.size(), INF2);
using Pi = pair<T, int>;
priority_queue<Pi, vector<Pi>, greater<Pi>> que;
dist[s] = 0;
que.emplace(dist[s], s);
while (!que.empty()) {
T cost;
int idx;
tie(cost, idx) = que.top();
que.pop();
if (dist[idx] < cost)
continue;
for (auto &e : g[idx]) {
auto next_cost = cost + e.cost;
if (dist[e.to] <= next_cost)
continue;
dist[e.to] = next_cost;
que.emplace(dist[e.to], e.to);
}
}
return dist;
}
// 負の辺を持つ単一始点最短路 O(VE)
template <typename T> vector<T> bellman_ford(Edges<T> &edges, int V, int s) {
const auto INF2 = numeric_limits<T>::max();
vector<T> dist(V, INF2);
dist[s] = 0;
REP(i, (V - 1)) {
for (auto &e : edges) {
if (dist[e.src] == INF2)
continue;
dist[e.to] = min(dist[e.to], dist[e.src] + e.cost);
}
}
for (auto &e : edges) {
if (dist[e.src] == INF2)
continue;
if (dist[e.src] + e.cost < dist[e.to])
return vector<T>();
}
return dist;
}
// 全点対間最短路 O(V^3)
template <typename T> void warshall_floyd(Matrix<T> &g, T lINF) {
ll n = g.size();
REP(k, n) {
REP(i, n) {
REP(j, n) {
if (g[i][k] == lINF || g[k][j] == lINF)
continue;
g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
}
}
}
return;
}
// 入出力高速化
void fast(void) {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
return;
}
// vectorの合計値を返します
ll vsum(Vi V) {
ll res = 0LL;
ll size = V.size();
REP(i, size) { res += V.at(i); }
return res;
}
// vectorの平均値を返します
lld vave(Vi V) {
lld size = V.size();
return (lld)vsum(V) / size;
}
// vectorの最大値を返します
ll vmax(Vi V) {
RSORT(V);
return V.at(0);
}
// vectorの最小値を返します
ll vmin(Vi V) {
SORT(V);
return V.at(0);
}
// 数値 b が a より大きく、 c より小さいことを判定します
template <typename T> bool mid1(T a, T b, T c) { return (a < b) && (b < c); }
// 数値 b が a以上 で、c以下 であることを判定します
template <typename T> bool mid2(T a, T b, T c) { return (a <= b) && (b <= c); }
// YES or NO
template <typename T> void YES(T n) { cout << ((n) ? "YES" : "NO") << endl; }
// Yes or No
template <typename T> void Yes(T n) { cout << ((n) ? "Yes" : "No") << endl; }
// yes or no
template <typename T> void yes(T n) { cout << ((n) ? "yes" : "no") << endl; }
// Yay! or :(
template <typename T> void Yay(T n) { cout << ((n) ? "Yay!" : ":(") << endl; }
// 標準入力します
inline void in(void) { return; }
template <typename First, typename... Rest>
void in(First &first, Rest &...rest) {
cin >> first;
in(rest...);
return;
}
// vectorを標準入力
template <typename T> void vin(T &V) {
ll size = V.size();
REP(i, size) { cin >> V[i]; }
return;
}
// 標準出力します
inline void out(void) {
cout << endl;
return;
}
template <typename T> void out(T only) {
cout << only << endl;
return;
}
template <typename First, typename... Rest>
void out(First first, Rest... rest) {
cout << first << " ";
out(rest...);
return;
}
// vectorを標準出力
template <typename T> void vout(T &V) {
ll size = V.size();
REP(i, size) { cout << V.at(i) << endl; }
}
// vectorを1行に標準出力
template <typename T> void vout2(T &V) {
ll size = V.size();
REP(i, size) { cout << V[i] << " \n"[i == (size - 1)]; }
}
// 最大公約数を求めます
int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; }
// 最小公倍数を求めます
int lcm(int a, int b) { return a * b / gcd(a, b); }
// 文字を全て大文字にします
string toStrUp(string str) {
char diff = 'A' - 'a';
REP(i, str.size()) str[i] += diff;
return str;
}
// 素因数分解します (O(sqrt(n)))
map<int, int> prime_factor(int n) {
map<int, int> ret;
for (int i = 2; i * i <= n; i++) {
while (n % i == 0) {
ret[i]++;
n /= i;
}
}
if (n != 1)
ret[n] = 1;
return ret;
}
// 素数判定 (O(sqrt(n)))
bool is_prime(int x) {
for (int i = 2; i * i <= x; i++) {
if (x % i == 0)
return false;
}
return true;
}
// 進数変換 (O(log n))
template <typename T> vector<T> convert_base(T x, T b) {
vector<T> ret;
T t = 1, k = abs(b);
while (x) {
ret.emplace_back((x * t) % k);
if (ret.back() < 0)
ret.back() += k;
x -= ret.back() * t;
x /= k;
t *= b / k;
}
if (ret.empty())
ret.emplace_back(0);
reverse(begin(ret), end(ret));
return ret;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
signed main() {
fast();
// 使えない変数名
// P, M, S, PQ, PQG
// ここから
int n, m, k;
int s;
in(n, k, s);
if (s == 1000000000) {
Vi ans(n, 1);
REP(i, k) { ans[i] = s; }
vout2(ans);
} else {
Vi ans(n, s + 1);
REP(i, k) { ans[i] = s; }
vout2(ans);
}
} | #include <bits/stdc++.h>
using namespace std;
#define int long long // 禁忌
using ll = long long;
using lld = long double;
#define P pair<ll, ll>
#define Vi vector<ll>
#define VVi vector<vector<ll>>
#define Vd vector<double>
#define Vb vector<bool>
#define Vs vector<string>
#define Vc vector<char>
#define M map<ll, ll>
#define S set<ll>
#define PQ priority_queue<ll>
#define PQG priority_queue < ll, V, greater<ll>
// 重み付き辺
template <typename T> struct edge {
int src, to;
T cost;
edge(int to, T cost) : src(-1), to(to), cost(cost) {}
edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
edge &operator=(const int &x) {
to = x;
return *this;
}
operator int() const { return to; }
};
// 重み付き辺集合
template <typename T> using Edges = vector<edge<T>>;
// 重み付きグラフ
template <typename T> using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = VVi; // 重みなしグラフ
// 距離行列
template <typename T> using Matrix = vector<vector<T>>;
const int MOD = 1000000007;
const int INF = 1061109567;
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define PER(i, n) for (int i = (n - 1); i >= 0; --i)
#define ALL(V) (V).begin(), (V).end()
#define SORT(V) sort(ALL(V)) // 小さい方からソート
#define REV(V) reverse(ALL(V)) // リバース
#define RSORT(V) \
SORT(V); \
REV(V) // 大きい方からソート
#define NEXP(V) next_permutation(ALL(V)) // 順列
#define pb(n) emplace_back(n)
#define popb pop_back()
#define endl '\n'
#define Endl '\n'
#define DUMP(x) cout << #x << " = " << (x) << endl
// UnionFind
class UnionFind {
public:
vector<int> par;
vector<int> siz;
UnionFind(int sz_) : par(sz_), siz(sz_, 1LL) {
for (int i = 0; i < sz_; ++i)
par[i] = i;
}
void init(ll sz_) {
par.resize(sz_);
siz.assign(sz_, 1LL);
for (int i = 0; i < sz_; ++i)
par[i] = i;
}
int root(int x) {
while (par[x] != x) {
x = par[x] = par[par[x]];
}
return x;
}
bool merge(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return false;
if (siz[x] < siz[y])
swap(x, y);
siz[x] += siz[y];
par[y] = x;
return true;
}
bool issame(int x, int y) { return root(x) == root(y); }
int size(int x) { return siz[root(x)]; }
};
// modint
template <int mod> struct ModInt {
int x;
ModInt() : x(0) {}
ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
ModInt &operator+=(const ModInt &p) {
if ((x += p.x) >= mod)
x -= mod;
return *this;
}
ModInt &operator-=(const ModInt &p) {
if ((x += mod - p.x) >= mod)
x -= mod;
return *this;
}
ModInt &operator*=(const ModInt &p) {
x = (int)(1LL * x * p.x % mod);
return *this;
}
ModInt &operator/=(const ModInt &p) {
*this *= p.inverse();
return *this;
}
ModInt operator-() const { return ModInt(-x); }
ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
bool operator==(const ModInt &p) const { return x == p.x; }
bool operator!=(const ModInt &p) const { return x != p.x; }
ModInt inverse() const {
int a = x, b = mod, u = 1, v = 0, t;
while (b > 0) {
t = a / b;
swap(a -= t * b, b);
swap(u -= t * v, v);
}
return ModInt(u);
}
ModInt pow(int64_t n) const {
ModInt ret(1), mul(x);
while (n > 0) {
if (n & 1)
ret *= mul;
mul *= mul;
n >>= 1;
}
return ret;
}
friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }
friend istream &operator>>(istream &is, ModInt &a) {
int64_t t;
is >> t;
a = ModInt<mod>(t);
return (is);
}
static int get_mod() { return mod; }
};
using modint = ModInt<MOD>;
// dijkstra O(E logV)
template <typename T> vector<T> dijkstra(WeightedGraph<T> &g, int s) {
const auto INF2 = numeric_limits<T>::max();
vector<T> dist(g.size(), INF2);
using Pi = pair<T, int>;
priority_queue<Pi, vector<Pi>, greater<Pi>> que;
dist[s] = 0;
que.emplace(dist[s], s);
while (!que.empty()) {
T cost;
int idx;
tie(cost, idx) = que.top();
que.pop();
if (dist[idx] < cost)
continue;
for (auto &e : g[idx]) {
auto next_cost = cost + e.cost;
if (dist[e.to] <= next_cost)
continue;
dist[e.to] = next_cost;
que.emplace(dist[e.to], e.to);
}
}
return dist;
}
// 負の辺を持つ単一始点最短路 O(VE)
template <typename T> vector<T> bellman_ford(Edges<T> &edges, int V, int s) {
const auto INF2 = numeric_limits<T>::max();
vector<T> dist(V, INF2);
dist[s] = 0;
REP(i, (V - 1)) {
for (auto &e : edges) {
if (dist[e.src] == INF2)
continue;
dist[e.to] = min(dist[e.to], dist[e.src] + e.cost);
}
}
for (auto &e : edges) {
if (dist[e.src] == INF2)
continue;
if (dist[e.src] + e.cost < dist[e.to])
return vector<T>();
}
return dist;
}
// 全点対間最短路 O(V^3)
template <typename T> void warshall_floyd(Matrix<T> &g, T lINF) {
ll n = g.size();
REP(k, n) {
REP(i, n) {
REP(j, n) {
if (g[i][k] == lINF || g[k][j] == lINF)
continue;
g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
}
}
}
return;
}
// 入出力高速化
void fast(void) {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
return;
}
// vectorの合計値を返します
ll vsum(Vi V) {
ll res = 0LL;
ll size = V.size();
REP(i, size) { res += V.at(i); }
return res;
}
// vectorの平均値を返します
lld vave(Vi V) {
lld size = V.size();
return (lld)vsum(V) / size;
}
// vectorの最大値を返します
ll vmax(Vi V) {
RSORT(V);
return V.at(0);
}
// vectorの最小値を返します
ll vmin(Vi V) {
SORT(V);
return V.at(0);
}
// 数値 b が a より大きく、 c より小さいことを判定します
template <typename T> bool mid1(T a, T b, T c) { return (a < b) && (b < c); }
// 数値 b が a以上 で、c以下 であることを判定します
template <typename T> bool mid2(T a, T b, T c) { return (a <= b) && (b <= c); }
// YES or NO
template <typename T> void YES(T n) { cout << ((n) ? "YES" : "NO") << endl; }
// Yes or No
template <typename T> void Yes(T n) { cout << ((n) ? "Yes" : "No") << endl; }
// yes or no
template <typename T> void yes(T n) { cout << ((n) ? "yes" : "no") << endl; }
// Yay! or :(
template <typename T> void Yay(T n) { cout << ((n) ? "Yay!" : ":(") << endl; }
// 標準入力します
inline void in(void) { return; }
template <typename First, typename... Rest>
void in(First &first, Rest &...rest) {
cin >> first;
in(rest...);
return;
}
// vectorを標準入力
template <typename T> void vin(T &V) {
ll size = V.size();
REP(i, size) { cin >> V[i]; }
return;
}
// 標準出力します
inline void out(void) {
cout << endl;
return;
}
template <typename T> void out(T only) {
cout << only << endl;
return;
}
template <typename First, typename... Rest>
void out(First first, Rest... rest) {
cout << first << " ";
out(rest...);
return;
}
// vectorを標準出力
template <typename T> void vout(T &V) {
ll size = V.size();
REP(i, size) { cout << V.at(i) << endl; }
}
// vectorを1行に標準出力
template <typename T> void vout2(T &V) {
ll size = V.size();
REP(i, size) { cout << V[i] << " \n"[i == (size - 1)]; }
}
// 最大公約数を求めます
int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; }
// 最小公倍数を求めます
int lcm(int a, int b) { return a * b / gcd(a, b); }
// 文字を全て大文字にします
string toStrUp(string str) {
char diff = 'A' - 'a';
REP(i, str.size()) str[i] += diff;
return str;
}
// 素因数分解します (O(sqrt(n)))
map<int, int> prime_factor(int n) {
map<int, int> ret;
for (int i = 2; i * i <= n; i++) {
while (n % i == 0) {
ret[i]++;
n /= i;
}
}
if (n != 1)
ret[n] = 1;
return ret;
}
// 素数判定 (O(sqrt(n)))
bool is_prime(int x) {
for (int i = 2; i * i <= x; i++) {
if (x % i == 0)
return false;
}
return true;
}
// 進数変換 (O(log n))
template <typename T> vector<T> convert_base(T x, T b) {
vector<T> ret;
T t = 1, k = abs(b);
while (x) {
ret.emplace_back((x * t) % k);
if (ret.back() < 0)
ret.back() += k;
x -= ret.back() * t;
x /= k;
t *= b / k;
}
if (ret.empty())
ret.emplace_back(0);
reverse(begin(ret), end(ret));
return ret;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
signed main() {
fast();
// 使えない変数名
// P, M, S, PQ, PQG
// ここから
int n, m, k;
int s;
in(n, k, s);
if (s == 1000000000) {
Vi ans(n, 1);
REP(i, k) { ans[i] = s; }
vout2(ans);
} else {
Vi ans(n, s + 1);
REP(i, k) { ans[i] = s; }
vout2(ans);
}
} | delete | 0 | 3 | 0 | 0 | 0 | |
p02797 | C++ | Runtime Error | #define __mode_debug__
/* ξ
ll
_ll_
/ ∞ \
│* A *│
│* C *│
│* 祈 *│
│* 願 *│
│* *│
 ̄ ̄ ̄ ̄ ̄ */
// C++14 (GCC 5.4.1)
#include <bits/stdc++.h>
using namespace std;
#ifdef __mode_debug__
#define DBG
#else
#define DBG if (false)
#endif
#define LLINF (1LL << 60)
using ll = long long;
#define mod (1e9 + 7)
#define whole(a) (a).begin(), (a).end()
#define rwhole(a) (a).rbegin(), (a).rend()
#define repd(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
#define rrepd(i, a, b) for (int i = (a); i >= (b); i--)
#define each(itr, ds) for (auto itr = (ds).begin(); itr != (ds).end(); itr++)
template <typename T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <typename T> inline string toString(const T &a) {
ostringstream oss;
oss << a;
return oss.str();
}
// DUMP
// ostream vector<T> , vector<vector<T>> , set<T> ,queue<T> ,
// priority_queue<T> , pair<T,U> , map<T,U>
template <class T> ostream &operator<<(ostream &os, vector<T> &vec) {
os << "{ ";
rep(i, vec.size()) os << ((i == 0) ? "" : ", ") << i << ":" << vec[i];
os << " }";
return os;
}
template <class T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) {
os << "{" << endl;
rep(i, vec.size()) os << "\t " << i << ":" << vec[i]
<< ((i == vec.size() - 1) ? "" : ",") << endl;
os << "\t }";
return os;
}
template <class T> ostream &operator<<(ostream &os, set<T> &p) {
os << "{ ";
each(itr, p) os << ((itr == p.begin()) ? "" : ", ") << *itr;
os << " }";
return os;
}
template <class T> ostream &operator<<(ostream &os, queue<T> &p) {
queue<T> q(p);
os << "[< " << (q.empty() ? " <]" : "");
while (!q.empty()) {
os << q.front();
q.pop();
os << (q.empty() ? " <]" : ", ");
}
return os;
}
template <class T> ostream &operator<<(ostream &os, priority_queue<T> &p) {
priority_queue<T> q(p);
os << "[< " << (q.empty() ? " <]" : "");
while (!q.empty()) {
os << q.top();
q.pop();
os << (q.empty() ? " <]" : ", ");
}
return os;
}
template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> &p) {
os << "(" << p.first << "," << p.second << ")";
return os;
}
template <class T, class U> ostream &operator<<(ostream &os, map<T, U> &p) {
os << "{ ";
each(itr, p) os << ((itr == p.begin()) ? "" : ", ") << itr->first << ":"
<< itr->second;
os << " }";
return os;
}
void dump_f(string &nm, char t) { cerr << endl; }
template <class T, class... Ts>
void dump_f(string &nm, char t, T &&x, Ts &&...ts) {
int l = nm.find_first_of(',');
string name(nm, 0, l), nx(nm, l + 1, -1);
if (t == '\n')
cerr << " " << name << "\t: " << x << t;
else
cerr << name << ": " << x << "," << t;
dump_f(nx, t, forward<Ts>(ts)...);
}
template <class... Ts> void dump_m(int &&ln, string &&nm, char t, Ts &&...ts) {
cerr << "[ln: " << ln << "]" << t;
dump_f(nm, t, forward<Ts>(ts)...);
}
#define dump(...) DBG dump_m(__LINE__, #__VA_ARGS__, '\n', __VA_ARGS__)
#define dl(...) DBG dump_m(__LINE__, #__VA_ARGS__, '\t', __VA_ARGS__)
#define lower_bound_idx(V, c) distance(V.begin(), lower_bound(whole(V), c))
int main() {
ios::sync_with_stdio(false); // stdoutとcoutの同期解除
cin.tie(nullptr); // cinとcoutの同期解除
ll N, K, S;
cin >> N >> K >> S;
vector<int> ans(N);
ll i = 0, cnt = K;
ll t = 0;
while (i < N - cnt) {
t = 0;
while (i < N - cnt && t < S - 1) {
ans[i] = 1;
i++;
t++;
}
if (i < N - cnt) {
ans[i] = S;
cnt--;
i++;
}
}
while (i < N) {
ans[i] = S;
i++;
}
rep(i, N) { cout << (i == 0 ? "" : " ") << ans[i]; }
cout << endl;
return (0);
} | #define __mode_debug__
/* ξ
ll
_ll_
/ ∞ \
│* A *│
│* C *│
│* 祈 *│
│* 願 *│
│* *│
 ̄ ̄ ̄ ̄ ̄ */
// C++14 (GCC 5.4.1)
#include <bits/stdc++.h>
using namespace std;
#ifdef __mode_debug__
#define DBG
#else
#define DBG if (false)
#endif
#define LLINF (1LL << 60)
using ll = long long;
#define mod (1e9 + 7)
#define whole(a) (a).begin(), (a).end()
#define rwhole(a) (a).rbegin(), (a).rend()
#define repd(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
#define rrepd(i, a, b) for (int i = (a); i >= (b); i--)
#define each(itr, ds) for (auto itr = (ds).begin(); itr != (ds).end(); itr++)
template <typename T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <typename T> inline string toString(const T &a) {
ostringstream oss;
oss << a;
return oss.str();
}
// DUMP
// ostream vector<T> , vector<vector<T>> , set<T> ,queue<T> ,
// priority_queue<T> , pair<T,U> , map<T,U>
template <class T> ostream &operator<<(ostream &os, vector<T> &vec) {
os << "{ ";
rep(i, vec.size()) os << ((i == 0) ? "" : ", ") << i << ":" << vec[i];
os << " }";
return os;
}
template <class T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) {
os << "{" << endl;
rep(i, vec.size()) os << "\t " << i << ":" << vec[i]
<< ((i == vec.size() - 1) ? "" : ",") << endl;
os << "\t }";
return os;
}
template <class T> ostream &operator<<(ostream &os, set<T> &p) {
os << "{ ";
each(itr, p) os << ((itr == p.begin()) ? "" : ", ") << *itr;
os << " }";
return os;
}
template <class T> ostream &operator<<(ostream &os, queue<T> &p) {
queue<T> q(p);
os << "[< " << (q.empty() ? " <]" : "");
while (!q.empty()) {
os << q.front();
q.pop();
os << (q.empty() ? " <]" : ", ");
}
return os;
}
template <class T> ostream &operator<<(ostream &os, priority_queue<T> &p) {
priority_queue<T> q(p);
os << "[< " << (q.empty() ? " <]" : "");
while (!q.empty()) {
os << q.top();
q.pop();
os << (q.empty() ? " <]" : ", ");
}
return os;
}
template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> &p) {
os << "(" << p.first << "," << p.second << ")";
return os;
}
template <class T, class U> ostream &operator<<(ostream &os, map<T, U> &p) {
os << "{ ";
each(itr, p) os << ((itr == p.begin()) ? "" : ", ") << itr->first << ":"
<< itr->second;
os << " }";
return os;
}
void dump_f(string &nm, char t) { cerr << endl; }
template <class T, class... Ts>
void dump_f(string &nm, char t, T &&x, Ts &&...ts) {
int l = nm.find_first_of(',');
string name(nm, 0, l), nx(nm, l + 1, -1);
if (t == '\n')
cerr << " " << name << "\t: " << x << t;
else
cerr << name << ": " << x << "," << t;
dump_f(nx, t, forward<Ts>(ts)...);
}
template <class... Ts> void dump_m(int &&ln, string &&nm, char t, Ts &&...ts) {
cerr << "[ln: " << ln << "]" << t;
dump_f(nm, t, forward<Ts>(ts)...);
}
#define dump(...) DBG dump_m(__LINE__, #__VA_ARGS__, '\n', __VA_ARGS__)
#define dl(...) DBG dump_m(__LINE__, #__VA_ARGS__, '\t', __VA_ARGS__)
#define lower_bound_idx(V, c) distance(V.begin(), lower_bound(whole(V), c))
int main() {
ios::sync_with_stdio(false); // stdoutとcoutの同期解除
cin.tie(nullptr); // cinとcoutの同期解除
ll N, K, S;
cin >> N >> K >> S;
vector<int> ans(N);
if (S == 1000000000LL) {
rep(i, K) { ans[i] = S; }
repd(i, K, N) { ans[i] = 1; }
} else {
rep(i, K) { ans[i] = S; }
repd(i, K, N) { ans[i] = S + 1; }
}
rep(i, N) { cout << (i == 0 ? "" : " ") << ans[i]; }
cout << endl;
return (0);
} | replace | 133 | 151 | 133 | 139 | 0 | |
p02797 | C++ | Runtime Error |
#include <bits/stdc++.h>
using namespace std;
#define inf INT_MAX
#define INF LLONG_MAX
#define ll long long
#define ull unsigned long long
#define M (int)(1e9 + 7)
#define P pair<int, int>
#define FOR(i, m, n) for (int i = (int)m; i < (int)n; i++)
#define RFOR(i, m, n) for (int i = (int)m; i >= (int)n; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n, 0)
#define all(a) a.begin(), a.end()
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};
#define F first
#define S second
#define PB push_back
#define EB emplace_back
#define int ll
#define vi vector<int>
#define IP pair<int, P>
#define PI pair<P, int>
#define PP pair<P, P>
#define Yes(f) \
{ cout << (f ? "Yes" : "No") << endl; }
#define YES(f) \
{ cout << (f ? "YES" : "NO") << endl; }
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
int n, k, s;
cin >> n >> k >> s;
rep(i, n) { cout << s / k + !!(s % (i + 1)) << ' '; }
cout << endl;
} |
#include <bits/stdc++.h>
using namespace std;
#define inf INT_MAX
#define INF LLONG_MAX
#define ll long long
#define ull unsigned long long
#define M (int)(1e9 + 7)
#define P pair<int, int>
#define FOR(i, m, n) for (int i = (int)m; i < (int)n; i++)
#define RFOR(i, m, n) for (int i = (int)m; i >= (int)n; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n, 0)
#define all(a) a.begin(), a.end()
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};
#define F first
#define S second
#define PB push_back
#define EB emplace_back
#define int ll
#define vi vector<int>
#define IP pair<int, P>
#define PI pair<P, int>
#define PP pair<P, P>
#define Yes(f) \
{ cout << (f ? "Yes" : "No") << endl; }
#define YES(f) \
{ cout << (f ? "YES" : "NO") << endl; }
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
int n, k, s;
cin >> n >> k >> s;
rep(i, n) {
if (i < k) {
cout << s << ' ';
} else {
cout << ((s + 1) % (int)(1e9)) << ' ';
}
}
cout << endl;
} | replace | 39 | 40 | 39 | 47 | 0 | |
p02797 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, k, s;
cin >> n >> k >> s;
// 4 2 3
// 1 1 1 1
// 5 3 100
// 100 100 100 1 1
vector<int> ans;
for (int i = 0; i < k; ++i) {
ans.push_back(s);
}
int tmp = s;
while (!(1 <= tmp && tmp <= 1e9) or tmp == s)
++tmp;
for (int i = k; i < n; ++i) {
ans.push_back(tmp);
}
cout << ans[0];
for (int i = 1; i < n; ++i) {
cout << " " << ans[i];
}
cout << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, k, s;
cin >> n >> k >> s;
// 4 2 3
// 1 1 1 1
// 5 3 100
// 100 100 100 1 1
vector<int> ans;
for (int i = 0; i < k; ++i) {
ans.push_back(s);
}
int tmp = s;
while (!(1 <= tmp && tmp <= 1e9) or tmp == s)
tmp = (tmp + 1) % int(1e9);
for (int i = k; i < n; ++i) {
ans.push_back(tmp);
}
cout << ans[0];
for (int i = 1; i < n; ++i) {
cout << " " << ans[i];
}
cout << endl;
return 0;
} | replace | 19 | 20 | 19 | 20 | TLE | |
p02797 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define M 1000000007
#define pb emplace_back
#define f first
#define s second
#define rep(i, st, ed) for (ll i = st; i < ed; ++i)
#define repn(i, st, ed) for (ll i = st; i <= ed; ++i)
#define repb(i, ed, st) for (ll i = ed; i >= st; --i)
#define all(v) v.begin(), v.end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define PI acosl(-1.0)
typedef long long ll;
typedef unsigned long long int llt;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<ll> vec;
typedef vector<pll> vecp;
typedef map<ll, ll> mpll;
const int N = 1e5 + 10;
const ll INF = 1LL << 60;
template <class T> void chmax(T &a, T b) {
if (a < b)
a = b;
}
template <class T> void chmin(T &a, T b) {
if (a > b)
a = b;
}
template <typename A, typename B> string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string &s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(vector<bool> v) {
bool first = true;
string res = "{";
for (int i = 0; i < static_cast<int>(v.size()); i++) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(v[i]);
}
res += "}";
return res;
}
template <size_t N> string to_string(bitset<N> v) {
string res = "";
for (size_t i = 0; i < N; i++) {
res += static_cast<char>('0' + v[i]);
}
return res;
}
template <typename A> string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
template <typename A, typename B> string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
cerr << to_string(H) << endl;
debug_out(T...);
}
#define LOCAL 3
#ifdef LOCAL
#define debug(...) \
cerr << "[" << #__VA_ARGS__ << "]:" << endl, debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
void solve() {
ll n, k, s;
cin >> n >> k >> s;
ll rem = n - k;
ll cnt = 0;
ll mm = rem / (s - 1);
while (mm--) {
rep(i, 0, s - 1) cout << 1 << " ";
cout << s << " ";
cnt++;
}
ll m = rem % (s - 1);
while (m--) {
cout << 1 << " ";
}
rep(i, 0, k - cnt) cout << s << " ";
}
int main() {
ll Tests = 1;
// cin>>Tests;
while (Tests--) {
solve();
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define M 1000000007
#define pb emplace_back
#define f first
#define s second
#define rep(i, st, ed) for (ll i = st; i < ed; ++i)
#define repn(i, st, ed) for (ll i = st; i <= ed; ++i)
#define repb(i, ed, st) for (ll i = ed; i >= st; --i)
#define all(v) v.begin(), v.end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define PI acosl(-1.0)
typedef long long ll;
typedef unsigned long long int llt;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<ll> vec;
typedef vector<pll> vecp;
typedef map<ll, ll> mpll;
const int N = 1e5 + 10;
const ll INF = 1LL << 60;
template <class T> void chmax(T &a, T b) {
if (a < b)
a = b;
}
template <class T> void chmin(T &a, T b) {
if (a > b)
a = b;
}
template <typename A, typename B> string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string &s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(vector<bool> v) {
bool first = true;
string res = "{";
for (int i = 0; i < static_cast<int>(v.size()); i++) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(v[i]);
}
res += "}";
return res;
}
template <size_t N> string to_string(bitset<N> v) {
string res = "";
for (size_t i = 0; i < N; i++) {
res += static_cast<char>('0' + v[i]);
}
return res;
}
template <typename A> string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
template <typename A, typename B> string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
cerr << to_string(H) << endl;
debug_out(T...);
}
#define LOCAL 3
#ifdef LOCAL
#define debug(...) \
cerr << "[" << #__VA_ARGS__ << "]:" << endl, debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
void solve() {
ll n, k, s;
cin >> n >> k >> s;
rep(i, 0, k) cout << s << " ";
if (s == 1e9)
s = 1;
else
s++;
rep(i, 0, n - k) cout << s << " ";
}
int main() {
ll Tests = 1;
// cin>>Tests;
while (Tests--) {
solve();
}
return 0;
} | replace | 118 | 131 | 118 | 124 | 0 | |
p02797 | Python | Runtime Error | def prime_decomposition(n):
ret = set()
x = n
divisor = 2
while x % divisor == 0:
x //= divisor
ret.add(divisor)
divisor = 3
while divisor * divisor <= n:
while x % divisor == 0:
x //= divisor
ret.add(divisor)
divisor += 2
if x > 1:
ret.add(x)
return ret
def main():
from functools import reduce
from operator import mul
n, k, s = map(int, input().split())
pd = prime_decomposition(s)
m = reduce(mul, pd) - 1 # 互いに素
ret = [s] * k + [m] * (n - k)
print(*ret)
if __name__ == "__main__":
main()
| def prime_decomposition(n):
ret = set()
x = n
divisor = 2
while x % divisor == 0:
x //= divisor
ret.add(divisor)
divisor = 3
while divisor * divisor <= n:
while x % divisor == 0:
x //= divisor
ret.add(divisor)
divisor += 2
if x > 1:
ret.add(x)
return ret
def main():
from functools import reduce
from operator import mul
n, k, s = map(int, input().split())
pd = prime_decomposition(s)
if not pd:
m = s + 1
else:
m = reduce(mul, pd) - 1 # 互いに素
ret = [s] * k + [m] * (n - k)
print(*ret)
if __name__ == "__main__":
main()
| replace | 30 | 31 | 30 | 34 | 0 | |
p02797 | Python | Runtime Error | from sys import stdin
N, K, S = [int(x) for x in stdin.readline().rstrip().split()]
ans = [S] * K
n = (N - K) // (S - 1)
ans.extend([S + 1] * (N - K))
print(*ans)
| from sys import stdin
N, K, S = [int(x) for x in stdin.readline().rstrip().split()]
ans = [S] * K
if S == 10**9:
ans.extend([1] * (N - K))
else:
ans.extend([S + 1] * (N - K))
print(*ans)
| replace | 5 | 7 | 5 | 9 | 0 | |
p02797 | Python | Runtime Error | N, K, S = map(int, input().split())
M = 1_000_000_000
_S = M - 1 if S == M else M
print(" ".join(list(map(str, [S] * K + [_S] * (N - K)))))
| N, K, S = map(int, input().split())
M = 1000000000
_S = M - 1 if S == M else M
print(" ".join(list(map(str, [S] * K + [_S] * (N - K)))))
| replace | 2 | 3 | 2 | 3 | 0 | |
p02797 | C++ | Runtime Error | #include <bits/stdc++.h>
#define pb push_back
#define INF (ll)1e9
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll n, k, s;
cin >> n >> k >> s;
vector<ll> ans;
ll left, right;
if (k == n) {
while (k--) {
ans.pb(s);
}
for (ll i = 0; i < n; i++) {
cout << ans[i] << " ";
}
} else if (s == 1) {
while (k--) {
ans.pb(1);
}
ll size = ans.size();
while (size != n) {
ans.pb(INF);
size++;
}
for (int i = 0; i < n; i++) {
cout << ans[i] << " ";
}
} else {
left = s / 2;
// cerr<<left;
right = s - left;
k--;
ans.pb(left);
ans.pb(right);
ll i = 0;
while (k--) {
if (i & 1) {
ans.pb(right);
} else
ans.pb(left);
i++;
}
ll p = ans.size();
ll MI = INF;
if (s == INF) {
MI--;
}
if (p < n) {
// ll size = ans.size();
// cerr<<"\n"<<INF-1<<"\n";
while (p != n) {
// cerr<<"\n\n";
ans.pb(MI);
p++;
}
}
for (i = 0; i < n; i++) {
cout << ans[i] << " ";
}
}
}
| #include <bits/stdc++.h>
#define pb push_back
#define INF (ll)1e9
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll n, k, s;
cin >> n >> k >> s;
vector<ll> ans;
ll left, right;
if (k == n) {
while (k--) {
ans.pb(s);
}
for (ll i = 0; i < n; i++) {
cout << ans[i] << " ";
}
} else if (s == 1) {
while (k--) {
ans.pb(1);
}
ll size = ans.size();
while (size != n) {
ans.pb(INF);
size++;
}
for (int i = 0; i < n; i++) {
cout << ans[i] << " ";
}
} else if (k == 0) {
ll MI = INF;
if (s == INF) {
MI--;
}
ans.resize(n, MI);
for (auto it : ans) {
cout << it << " ";
}
} else {
left = s / 2;
// cerr<<left;
right = s - left;
k--;
ans.pb(left);
ans.pb(right);
ll i = 0;
while (k--) {
if (i & 1) {
ans.pb(right);
} else
ans.pb(left);
i++;
}
ll p = ans.size();
ll MI = INF;
if (s == INF) {
MI--;
}
if (p < n) {
// ll size = ans.size();
// cerr<<"\n"<<INF-1<<"\n";
while (p != n) {
// cerr<<"\n\n";
ans.pb(MI);
p++;
}
}
for (i = 0; i < n; i++) {
cout << ans[i] << " ";
}
}
}
| insert | 31 | 31 | 31 | 40 | 0 | |
p02797 | C++ | Runtime Error | /**
* author : The San Cao (caothesan@gmail.com)
* created : 2020.02.08 11:01:47 +07
*/
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
int n, k, s;
cin >> n >> k >> s;
vector<int> result(k - 1, s);
int m = min(n - k + 1, s);
for (int i = 0; i < m - 1; ++i)
result.push_back(s / m);
result.push_back(s / m + s % m);
while (result.size() < n)
result.push_back(s + 1);
for (auto it : result)
cout << it << " ";
return 0;
}
| /**
* author : The San Cao (caothesan@gmail.com)
* created : 2020.02.08 11:01:47 +07
*/
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
int n, k, s;
cin >> n >> k >> s;
for (int i = 0; i < n; ++i)
cout << s + (i >= k) * (s == 1 ? 1 : -1) << " ";
return 0;
}
| replace | 17 | 26 | 17 | 19 | 0 | |
p02797 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define int long long int
mt19937 rng(std::chrono::duration_cast<std::chrono::nanoseconds>(
chrono::high_resolution_clock::now().time_since_epoch())
.count());
#define mp make_pair
#define pb push_back
#define F first
#define S second
const int N = 1000005;
#define M 1000000007
#define double long double
#define BINF 10000000000000
#define init(arr, val) memset(arr, val, sizeof(arr))
#define MAXN 15000001
#define deb(x) cout << #x << " " << x << "\n";
const int LG = 22;
/*
Created at 15:28
*/
#undef int
int main() {
#define int long long int
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("ip.txt", "r", stdin);
freopen("op.txt", "w", stdout);
#endif
int n, k, s;
cin >> n >> k >> s;
int a[n];
for (int i = 0; i < k; i++) {
a[i] = s;
}
int sum = 0;
for (int i = k; i < n; i++) {
sum++;
if (sum == s) {
a[i] = s + 1;
sum = 0;
continue;
} else
a[i] = 1;
}
for (int i = 0; i < n; i++)
cout << a[i] << ' ';
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long int
mt19937 rng(std::chrono::duration_cast<std::chrono::nanoseconds>(
chrono::high_resolution_clock::now().time_since_epoch())
.count());
#define mp make_pair
#define pb push_back
#define F first
#define S second
const int N = 1000005;
#define M 1000000007
#define double long double
#define BINF 10000000000000
#define init(arr, val) memset(arr, val, sizeof(arr))
#define MAXN 15000001
#define deb(x) cout << #x << " " << x << "\n";
const int LG = 22;
/*
Created at 15:28
*/
#undef int
int main() {
#define int long long int
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, k, s;
cin >> n >> k >> s;
int a[n];
for (int i = 0; i < k; i++) {
a[i] = s;
}
int sum = 0;
for (int i = k; i < n; i++) {
sum++;
if (sum == s) {
a[i] = s + 1;
sum = 0;
continue;
} else
a[i] = 1;
}
for (int i = 0; i < n; i++)
cout << a[i] << ' ';
return 0;
}
| delete | 30 | 35 | 30 | 30 | -11 | |
p02797 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll n, k, s;
cin >> n >> k >> s;
ll a[n];
for (int i = 0; i < k; i++) {
a[i] = s;
}
ll temp = s / (n - k);
if (temp > 1)
temp = 1;
else
temp = 10;
for (int i = k; i < n; i++) {
a[i] = temp;
}
for (int i = 0; i < n; i++) {
cout << a[i] << " ";
}
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll n, k, s;
cin >> n >> k >> s;
ll a[n];
for (int i = 0; i < k; i++) {
a[i] = s;
}
ll temp = 1000000000;
if (s == temp)
temp = 999999999;
for (int i = k; i < n; i++) {
a[i] = temp;
}
for (int i = 0; i < n; i++) {
cout << a[i] << " ";
}
} | replace | 10 | 15 | 10 | 14 | 0 | |
p02797 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
#define sz size()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(c) (c).begin(), (c).end()
#define rep(i, a, b) for (ll i = (a); i < (b); ++i)
#define per(i, a, b) for (ll i = b - 1LL; i >= (a); --i)
#define clr(a, b) memset((a), (b), sizeof(a))
#define ctos(c) string(1, c)
#define endl "\n"
#define print(x) cout << #x << " = " << x << endl;
#define MOD 1000000007
int main() {
ll n, k, s;
cin >> n >> k >> s;
vector<ll> ans;
if (s != 1000000000) {
rep(i, 0, k) { ans.pb(s); }
rep(i, 0, n - k) { ans.pb(1000000000); }
} else {
assert(false);
}
rep(i, 0, ans.sz) {
cout << ans[i];
if (i != ans.sz - 1)
cout << " ";
}
cout << endl;
return 0;
} | #include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
#define sz size()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(c) (c).begin(), (c).end()
#define rep(i, a, b) for (ll i = (a); i < (b); ++i)
#define per(i, a, b) for (ll i = b - 1LL; i >= (a); --i)
#define clr(a, b) memset((a), (b), sizeof(a))
#define ctos(c) string(1, c)
#define endl "\n"
#define print(x) cout << #x << " = " << x << endl;
#define MOD 1000000007
int main() {
ll n, k, s;
cin >> n >> k >> s;
vector<ll> ans;
if (s != 1000000000) {
rep(i, 0, k) { ans.pb(s); }
rep(i, 0, n - k) { ans.pb(1000000000); }
} else {
rep(i, 0, k) { ans.pb(1000000000); }
rep(i, 0, n - k) { ans.pb(1); }
}
rep(i, 0, ans.sz) {
cout << ans[i];
if (i != ans.sz - 1)
cout << " ";
}
cout << endl;
return 0;
} | replace | 48 | 49 | 48 | 50 | 0 | |
p02797 | C++ | Runtime Error | #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 equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const ll mod2 = 998244353;
const int INF = 1e9;
const long double EPS = 1e-10;
int main() {
int n, s, k;
cin >> n >> s >> k;
vector<int> a(n, INF);
if (s == INF) {
rep(i, n) a[i] = 1;
}
rep(i, k) a[i] = s;
rep(i, n) {
if (i)
cout << " ";
cout << a[i];
}
cout << endl;
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 equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const ll mod2 = 998244353;
const int INF = 1e9;
const long double EPS = 1e-10;
int main() {
int n, k, s;
cin >> n >> k >> s;
vector<int> a(n, INF);
if (s == INF) {
rep(i, n) a[i] = 1;
}
rep(i, k) a[i] = s;
rep(i, n) {
if (i)
cout << " ";
cout << a[i];
}
cout << endl;
return 0;
} | replace | 14 | 16 | 14 | 16 | 0 | |
p02797 | C++ | Runtime Error | #include <bits/stdc++.h>
#define For(i, a, b) for (int(i) = (a); (i) < (b); ++(i))
#define rFor(i, a, b) for (int(i) = (a)-1; (i) >= (b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<int, lint> pil;
typedef pair<lint, int> pli;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
constexpr lint mod = 1e9 + 7;
constexpr lint INF = mod * mod;
constexpr int MAX = 1000010;
int main() {
int n, K, S;
scanf("%d%d%d", &n, &K, &S);
vector<int> ans;
if (S == 1) {
rep(i, K) ans.push_back(1);
rep(i, n - K) ans.push_back(mod - 7);
} else {
ans.push_back(1);
rep(i, K) ans.push_back(i % 2 == 0 ? S - 1 : 1);
if (S != mod - 7)
rep(i, n - K - 1) ans.push_back(mod - 7);
else {
int dummy = (int)(ans[ans.size() - 1] == 1 ? mod - 9 : mod - 8);
rep(i, n - K - 1) ans.push_back(dummy);
}
}
assert(ans.size() == n);
for (int x : ans)
printf("%d ", x);
} | #include <bits/stdc++.h>
#define For(i, a, b) for (int(i) = (a); (i) < (b); ++(i))
#define rFor(i, a, b) for (int(i) = (a)-1; (i) >= (b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<int, lint> pil;
typedef pair<lint, int> pli;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
constexpr lint mod = 1e9 + 7;
constexpr lint INF = mod * mod;
constexpr int MAX = 1000010;
int main() {
int n, K, S;
scanf("%d%d%d", &n, &K, &S);
vector<int> ans;
rep(i, K) ans.push_back(S);
int dummy = (int)(S == mod - 7 ? 1 : mod - 7);
rep(i, n - K) ans.push_back(dummy);
for (int x : ans)
printf("%d ", x);
} | replace | 36 | 50 | 36 | 39 | 0 | |
p02797 | Python | Runtime Error | import sys
import numpy as np
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)
N, K, S = map(int, read().split())
S_half = [S // 2, S - S // 2]
ans = []
for _ in range(N // 2 + 1):
ans += S_half
ans = np.array(ans[:N])
i = 0
cnt = 0
for i in range(N):
if cnt == K:
ans[i:N] = S - 1
idx = i
break
if sum(ans[i : i + 2]) == S:
cnt += 1
continue
if ans[idx - 1 : idx + 1].sum() != S:
for s_ in S_half:
if ans[idx - 1] + s_ == S:
ans[idx] = s_
print(*ans)
| import sys
import numpy as np
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)
N, K, S = map(int, read().split())
if S != 1:
ans = [S - 1] * N
else:
ans = [S + 1] * N
for i in range(K):
ans[i] = S
print(*ans)
| replace | 10 | 31 | 10 | 16 | TLE | |
p02797 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
signed main(void) {
int n, m, k, s;
cin >> n >> k >> s;
vector<int> v;
rep(i, k) { v.push_back(s); }
m = n - k;
v.erase(v.begin());
rep(i, m) { v.push_back(1); }
v.push_back(s - m);
sort(v.begin(), v.end());
rep(i, v.size()) printf("%d%c", v[i], i == v.size() - 1 ? ' ' : '\n');
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
signed main(void) {
int n, m, k, s;
cin >> n >> k >> s;
vector<int> v;
rep(i, k) { v.push_back(s); }
if (s < 1000000000) {
rep(i, n - k) v.push_back(s + 1);
} else {
rep(i, n - k) v.push_back(1);
}
rep(i, v.size()) printf("%d%c", v[i], i == v.size() - 1 ? ' ' : '\n');
return 0;
}
| replace | 9 | 14 | 9 | 14 | 0 | |
p02798 | C++ | Time Limit Exceeded | #include <cassert>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <functional>
#include <bitset>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define TEST \
{ IS_TEST = true; }
#define fi first
#define se second
#define pb(x) \
{ push_back(x); }
using namespace std;
using ll = int_fast64_t;
using v_b = vector<bool>;
using v_ll = vector<ll>;
using str = string;
using v_str = vector<string>;
using p_ll = pair<ll, ll>;
using vv_b = vector<v_b>;
using vv_ll = vector<v_ll>;
using vp_ll = vector<p_ll>;
using vvv_ll = vector<vv_ll>;
using vvp_ll = vector<vp_ll>;
using ld = long double;
using v_ld = vector<ld>;
using vv_ld = vector<v_ld>;
bool IS_TEST = false;
ll ll_min64 = 1LL << 63;
ll ll_max64 = ~ll_min64;
ll ll_min32 = 1LL << 31;
ll ll_max32 = ~ll_min32;
ll MOD = 1000000007;
/*displaying functions for debug*/
template <class T> void show2(const T &x) { cout << x; }
template <class T1, class T2> void show2(const pair<T1, T2> &x) {
cout << "{" << show2(x.first) << "," << show2(x.second) << "}";
}
template <class T> void show(const T &x) {
if (!IS_TEST)
return;
show2(x);
cout << endl;
}
template <class T> void v_show(const T &v, ll n = -1) {
if (!IS_TEST)
return;
auto itr = v.begin();
ll m = n;
while (itr != v.end() && m != 0) {
show2(*itr);
cout << " ";
itr++;
m--;
}
cout << endl;
}
template <class T> void vv_show(const T &v, ll n = -1) {
if (!IS_TEST)
return;
cout << "--------------------------------\n";
auto itr = v.begin();
ll m = n;
while (itr != v.end() && m != 0) {
v_show(*itr, n);
itr++;
m--;
}
cout << "--------------------------------" << endl;
}
/*--------------------------------*/
/*loading integers*/
void load(ll &x1) { cin >> x1; }
void load(ll &x1, ll &x2) { cin >> x1 >> x2; }
void load(ll &x1, ll &x2, ll &x3) { cin >> x1 >> x2 >> x3; }
void load(ll &x1, ll &x2, ll &x3, ll &x4) { cin >> x1 >> x2 >> x3 >> x4; }
void v_load(ll n, v_ll &v1, ll head = 0, ll tail = 0, ll init = 0) {
ll m = n + head + tail;
v1.assign(m, init);
for (ll i = 0; i < n; i++) {
cin >> v1[i + head];
}
}
void v_load(ll n, v_ll &v1, v_ll &v2, ll head = 0, ll tail = 0, ll init = 0) {
ll m = n + head + tail;
v1.assign(m, init);
v2.assign(m, init);
for (ll i = 0; i < n; i++) {
cin >> v1[i + head] >> v2[i + head];
}
}
void v_load(ll n, v_ll &v1, v_ll &v2, v_ll &v3, ll head = 0, ll tail = 0,
ll init = 0) {
ll m = n + head + tail;
v1.assign(m, init);
v2.assign(m, init);
v3.assign(m, init);
for (ll i = 0; i < n; i++) {
cin >> v1[i + head] >> v2[i + head] >> v3[i + head];
}
}
void v_load(ll n, v_ll &v1, v_ll &v2, v_ll &v3, v_ll &v4, ll head = 0,
ll tail = 0, ll init = 0) {
ll m = n + head + tail;
v1.assign(m, init);
v2.assign(m, init);
v3.assign(m, init);
v4.assign(m, init);
for (ll i = 0; i < n; i++) {
cin >> v1[i + head] >> v2[i + head] >> v3[i + head] >> v4[i + head];
}
}
/*--------------------------------*/
v_ll local_sort(ll x1 = ll_max64, ll x2 = ll_max64, ll x3 = ll_max64,
ll x4 = ll_max64) {
v_ll x{x1, x2, x3, x4};
sort(x.begin(), x.end());
return x;
}
ll max(ll x, ll y) { return x > y ? x : y; }
ll max(v_ll::iterator b, v_ll::iterator e) {
ll ans = *b;
while (b < e) {
ans = max(ans, *b);
b++;
}
return ans;
}
ll argmax(v_ll::iterator b, v_ll::iterator e) {
ll ans = 0, cnt = 0, val = *b;
while (b < e) {
if (val < *b) {
ans = cnt;
val = *b;
}
cnt++;
b++;
}
return ans;
}
ll min(ll x, ll y) { return x < y ? x : y; }
ll min(v_ll::iterator b, v_ll::iterator e) {
ll ans = *b;
while (b < e) {
ans = min(ans, *b);
b++;
}
return ans;
}
ll argmin(v_ll::iterator b, v_ll::iterator e) {
ll ans = 0, cnt = 0, val = *b;
while (b < e) {
if (val > *b) {
ans = cnt;
val = *b;
}
cnt++;
b++;
}
return ans;
}
ll sum(v_ll::iterator b, v_ll::iterator e) {
ll ans = 0;
while (b < e) {
ans += *b;
b++;
}
return ans;
}
template <class T> void chmax(T &x, const T &y) {
if (x >= y)
return;
x = y;
}
template <class T> void chmin(T &x, const T &y) {
if (x <= y)
return;
x = y;
}
template <class T, class S> void chmax(T &x, S &xx, const T &y, const S &yy) {
if (x >= y)
return;
x = y;
xx = yy;
}
template <class T, class S> void chmin(T &x, S &xx, const T &y, const S &yy) {
if (x <= y)
return;
x = y;
xx = yy;
}
template <class T> void quit(T x) {
cout << x << endl;
exit(0);
}
ll rem(ll x, ll y) {
ll z = x % y;
return z >= 0 ? z : z + y;
}
// setprecision(digit)
ll N, M, K, H, W, ans;
v_ll A, B, C;
int main() {
load(N);
A.assign(N, 0);
B.assign(N, 0);
for (ll i = 0; i < N; i++) {
scanf("%lld", &A[i]);
}
for (ll i = 0; i < N; i++) {
scanf("%lld", &B[i]);
}
for (ll i = 1; i < N; i += 2) {
swap(A[i], B[i]);
}
v_ll S(N / 2, 0);
for (ll i = 0; i < N / 2; i++) {
S[i] = i;
}
bool flag = true;
ans = -1;
while (flag) {
v_ll T1(N - N / 2, 0);
v_ll T2(N / 2, 0);
ll j = 0, k = 0;
bool good = true;
for (ll i = 0; i < N; i++) {
if (k < N / 2 && i == S[k]) {
T2[k] = i;
k++;
} else {
T1[j] = i;
j++;
}
}
sort(T1.begin(), T1.end(), [&](const ll &x, const ll &y) {
if (A[x] < A[y])
return true;
if (A[x] == A[y] && x < y)
return true;
return false;
});
sort(T2.begin(), T2.end(), [&](const ll &x, const ll &y) {
if (B[x] < B[y])
return true;
if (B[x] == B[y] && x < y)
return true;
return false;
});
for (ll i = 0; i < T2.size(); i++) {
if (B[T2[i]] < A[T1[i]])
good = false;
}
for (ll i = 1; i < T1.size(); i++) {
if (A[T1[i]] < B[T2[i - 1]])
good = false;
}
v_ll U;
if (good) {
for (ll i = 0; i < T1.size(); i++) {
U.push_back(T1[i]);
if (i < T2.size()) {
U.push_back(T2[i]);
}
}
ll a = 0;
for (ll i = 0; i < N; i++) {
for (ll j = i + 1; j < N; j++) {
if (U[i] > U[j])
a++;
}
}
if (ans < 0 || a < ans)
ans = a;
}
ll i = 0;
for (i = N / 2 - 1; i >= 0; i--) {
S[i]++;
if (S[i] <= N - (N / 2) + i) {
i++;
break;
}
if (i == 0)
flag = false;
}
for (; i < N / 2; i++) {
if (i > 0)
S[i] = S[i - 1] + 1;
}
}
cout << ans << endl;
}
| #include <cassert>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <functional>
#include <bitset>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define TEST \
{ IS_TEST = true; }
#define fi first
#define se second
#define pb(x) \
{ push_back(x); }
using namespace std;
using ll = int_fast64_t;
using v_b = vector<bool>;
using v_ll = vector<ll>;
using str = string;
using v_str = vector<string>;
using p_ll = pair<ll, ll>;
using vv_b = vector<v_b>;
using vv_ll = vector<v_ll>;
using vp_ll = vector<p_ll>;
using vvv_ll = vector<vv_ll>;
using vvp_ll = vector<vp_ll>;
using ld = long double;
using v_ld = vector<ld>;
using vv_ld = vector<v_ld>;
bool IS_TEST = false;
ll ll_min64 = 1LL << 63;
ll ll_max64 = ~ll_min64;
ll ll_min32 = 1LL << 31;
ll ll_max32 = ~ll_min32;
ll MOD = 1000000007;
/*displaying functions for debug*/
template <class T> void show2(const T &x) { cout << x; }
template <class T1, class T2> void show2(const pair<T1, T2> &x) {
cout << "{" << show2(x.first) << "," << show2(x.second) << "}";
}
template <class T> void show(const T &x) {
if (!IS_TEST)
return;
show2(x);
cout << endl;
}
template <class T> void v_show(const T &v, ll n = -1) {
if (!IS_TEST)
return;
auto itr = v.begin();
ll m = n;
while (itr != v.end() && m != 0) {
show2(*itr);
cout << " ";
itr++;
m--;
}
cout << endl;
}
template <class T> void vv_show(const T &v, ll n = -1) {
if (!IS_TEST)
return;
cout << "--------------------------------\n";
auto itr = v.begin();
ll m = n;
while (itr != v.end() && m != 0) {
v_show(*itr, n);
itr++;
m--;
}
cout << "--------------------------------" << endl;
}
/*--------------------------------*/
/*loading integers*/
void load(ll &x1) { cin >> x1; }
void load(ll &x1, ll &x2) { cin >> x1 >> x2; }
void load(ll &x1, ll &x2, ll &x3) { cin >> x1 >> x2 >> x3; }
void load(ll &x1, ll &x2, ll &x3, ll &x4) { cin >> x1 >> x2 >> x3 >> x4; }
void v_load(ll n, v_ll &v1, ll head = 0, ll tail = 0, ll init = 0) {
ll m = n + head + tail;
v1.assign(m, init);
for (ll i = 0; i < n; i++) {
cin >> v1[i + head];
}
}
void v_load(ll n, v_ll &v1, v_ll &v2, ll head = 0, ll tail = 0, ll init = 0) {
ll m = n + head + tail;
v1.assign(m, init);
v2.assign(m, init);
for (ll i = 0; i < n; i++) {
cin >> v1[i + head] >> v2[i + head];
}
}
void v_load(ll n, v_ll &v1, v_ll &v2, v_ll &v3, ll head = 0, ll tail = 0,
ll init = 0) {
ll m = n + head + tail;
v1.assign(m, init);
v2.assign(m, init);
v3.assign(m, init);
for (ll i = 0; i < n; i++) {
cin >> v1[i + head] >> v2[i + head] >> v3[i + head];
}
}
void v_load(ll n, v_ll &v1, v_ll &v2, v_ll &v3, v_ll &v4, ll head = 0,
ll tail = 0, ll init = 0) {
ll m = n + head + tail;
v1.assign(m, init);
v2.assign(m, init);
v3.assign(m, init);
v4.assign(m, init);
for (ll i = 0; i < n; i++) {
cin >> v1[i + head] >> v2[i + head] >> v3[i + head] >> v4[i + head];
}
}
/*--------------------------------*/
v_ll local_sort(ll x1 = ll_max64, ll x2 = ll_max64, ll x3 = ll_max64,
ll x4 = ll_max64) {
v_ll x{x1, x2, x3, x4};
sort(x.begin(), x.end());
return x;
}
ll max(ll x, ll y) { return x > y ? x : y; }
ll max(v_ll::iterator b, v_ll::iterator e) {
ll ans = *b;
while (b < e) {
ans = max(ans, *b);
b++;
}
return ans;
}
ll argmax(v_ll::iterator b, v_ll::iterator e) {
ll ans = 0, cnt = 0, val = *b;
while (b < e) {
if (val < *b) {
ans = cnt;
val = *b;
}
cnt++;
b++;
}
return ans;
}
ll min(ll x, ll y) { return x < y ? x : y; }
ll min(v_ll::iterator b, v_ll::iterator e) {
ll ans = *b;
while (b < e) {
ans = min(ans, *b);
b++;
}
return ans;
}
ll argmin(v_ll::iterator b, v_ll::iterator e) {
ll ans = 0, cnt = 0, val = *b;
while (b < e) {
if (val > *b) {
ans = cnt;
val = *b;
}
cnt++;
b++;
}
return ans;
}
ll sum(v_ll::iterator b, v_ll::iterator e) {
ll ans = 0;
while (b < e) {
ans += *b;
b++;
}
return ans;
}
template <class T> void chmax(T &x, const T &y) {
if (x >= y)
return;
x = y;
}
template <class T> void chmin(T &x, const T &y) {
if (x <= y)
return;
x = y;
}
template <class T, class S> void chmax(T &x, S &xx, const T &y, const S &yy) {
if (x >= y)
return;
x = y;
xx = yy;
}
template <class T, class S> void chmin(T &x, S &xx, const T &y, const S &yy) {
if (x <= y)
return;
x = y;
xx = yy;
}
template <class T> void quit(T x) {
cout << x << endl;
exit(0);
}
ll rem(ll x, ll y) {
ll z = x % y;
return z >= 0 ? z : z + y;
}
// setprecision(digit)
ll N, M, K, H, W, ans;
v_ll A, B, C;
int main() {
load(N);
A.assign(N, 0);
B.assign(N, 0);
for (ll i = 0; i < N; i++) {
scanf("%lld", &A[i]);
}
for (ll i = 0; i < N; i++) {
scanf("%lld", &B[i]);
}
for (ll i = 1; i < N; i += 2) {
swap(A[i], B[i]);
}
v_ll S(N / 2, 0);
for (ll i = 0; i < N / 2; i++) {
S[i] = i;
}
bool flag = true;
ans = -1;
while (flag) {
v_ll T1(N - N / 2, 0);
v_ll T2(N / 2, 0);
ll j = 0, k = 0;
bool good = true;
for (ll i = 0; i < N; i++) {
if (k < N / 2 && i == S[k]) {
T2[k] = i;
k++;
} else {
T1[j] = i;
j++;
}
}
sort(T1.begin(), T1.end(), [&](const ll &x, const ll &y) {
if (A[x] < A[y])
return true;
if (A[x] == A[y] && x < y)
return true;
return false;
});
sort(T2.begin(), T2.end(), [&](const ll &x, const ll &y) {
if (B[x] < B[y])
return true;
if (B[x] == B[y] && x < y)
return true;
return false;
});
for (ll i = 0; i < T2.size(); i++) {
if (B[T2[i]] < A[T1[i]])
good = false;
}
for (ll i = 1; i < T1.size(); i++) {
if (A[T1[i]] < B[T2[i - 1]])
good = false;
}
v_ll U;
if (good) {
for (ll i = 0; i < T1.size(); i++) {
U.push_back(T1[i]);
if (i < T2.size()) {
U.push_back(T2[i]);
}
}
ll a = 0;
for (ll i = 0; i < N; i++) {
for (ll j = i + 1; j < N; j++) {
if (U[i] > U[j])
a++;
}
}
if (ans < 0 || a < ans)
ans = a;
if (ans == 0)
quit(0);
}
ll i = 0;
for (i = N / 2 - 1; i >= 0; i--) {
S[i]++;
if (S[i] <= N - (N / 2) + i) {
i++;
break;
}
if (i == 0)
flag = false;
}
for (; i < N / 2; i++) {
if (i > 0)
S[i] = S[i - 1] + 1;
}
}
cout << ans << endl;
}
| insert | 307 | 307 | 307 | 310 | TLE | |
p02798 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using lint = long long;
const lint mod = 1e9 + 7;
#define all(x) (x).begin(), (x).end()
#define bitcount(n) __builtin_popcountl((lint)(n))
#define fcout cout << fixed << setprecision(15)
#define highest(x) (63 - __builtin_clzl(x))
#define rep(i, n) for (int i = 0; i < n; i++)
const int inf9 = 1e9;
const lint inf18 = 1e18;
template <class T> inline void YES(T condition) {
if (condition)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
template <class T> inline void Yes(T condition) {
if (condition)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
template <class T = string, class U = char>
int character_count(T text, U character) {
int ans = 0;
for (U i : text) {
ans += (i == character);
}
return ans;
}
lint power(lint base, lint exponent, lint module) {
if (exponent % 2) {
return power(base, exponent - 1, module) * base % module;
} else if (exponent) {
lint root_ans = power(base, exponent / 2, module);
return root_ans * root_ans % module;
} else {
return 1;
}
}
struct position {
int y, x;
};
position mv[4] = {
{0, -1},
{1, 0},
{0, 1},
{-1, 0}}; // double euclidean(position first, position second){ return
// sqrt((second.x - first.x) * (second.x - first.x) + (second.y -
// first.y) * (second.y - first.y)); }
template <class T, class U> string to_string(pair<T, U> x) {
return to_string(x.first) + "," + to_string(x.second);
}
string to_string(string x) { return x; }
template <class itr> void array_output(itr start, itr goal) {
string ans;
for (auto i = start; i != goal; i++)
ans += to_string(*i) + " ";
if (!ans.empty())
ans.pop_back();
cout << ans << endl;
}
template <class itr> void cins(itr first, itr last) {
for (auto i = first; i != last; i++) {
cin >> (*i);
}
}
template <class T> T gcd(T a, T b) {
if (a && b) {
return gcd(min(a, b), max(a, b) % min(a, b));
} else {
return a;
}
}
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
struct combination {
vector<lint> fact, inv;
combination(int sz) : fact(sz + 1), inv(sz + 1) {
fact[0] = 1;
for (int i = 1; i <= sz; i++) {
fact[i] = fact[i - 1] * i % mod;
}
inv[sz] = power(fact[sz], mod - 2, mod);
for (int i = sz - 1; i >= 0; i--) {
inv[i] = inv[i + 1] * (i + 1) % mod;
}
}
lint P(int n, int r) {
if (r < 0 || n < r)
return 0;
return (fact[n] * inv[n - r] % mod);
}
lint C(int p, int q) {
if (q < 0 || p < q)
return 0;
return (fact[p] * inv[q] % mod * inv[p - q] % mod);
}
};
template <class itr> bool next_sequence(itr first, itr last, int max_bound) {
itr now = last;
while (now != first) {
now--;
(*now)++;
if ((*now) == max_bound) {
(*now) = 0;
} else {
return true;
}
}
return false;
}
template <class itr, class itr2>
bool next_sequence2(itr first, itr last, itr2 first2, itr2 last2) {
itr now = last;
itr2 now2 = last2;
while (now != first) {
now--, now2--;
(*now)++;
if ((*now) == (*now2)) {
(*now) = 0;
} else {
return true;
}
}
return false;
}
inline int at(lint i, int j) { return (i >> j) & 1; }
int N;
vector<int> num[2];
int dp[1 << 18][20];
int card(int already_bit, int now, int back) {
if (now == N) {
return 0;
} else if (dp[already_bit][back] != -1) {
return dp[already_bit][back];
}
int ans = inf9, cnt = 0;
for (int i = N - 1; i >= 0; i--) {
if (at(already_bit, i)) {
cnt++;
continue;
}
// cout << i << endl;
if (back < 18) {
int back_num = num[abs(back - now + 1) % 2][back];
int this_num = num[abs(i - now) % 2][i];
if (back_num > this_num) {
continue;
}
}
ans = min(ans, card(already_bit | (1 << i), now + 1, i) + cnt);
}
return ans;
}
int main() {
cin >> N;
num[0].resize(N);
num[1].resize(N);
cins(all(num[0]));
cins(all(num[1]));
memset(dp, -1, sizeof(dp));
int ans = card(0, 0, 18);
if (ans >= inf9) {
cout << -1 << endl;
} else {
cout << ans << endl;
}
}
| #include <bits/stdc++.h>
using namespace std;
using lint = long long;
const lint mod = 1e9 + 7;
#define all(x) (x).begin(), (x).end()
#define bitcount(n) __builtin_popcountl((lint)(n))
#define fcout cout << fixed << setprecision(15)
#define highest(x) (63 - __builtin_clzl(x))
#define rep(i, n) for (int i = 0; i < n; i++)
const int inf9 = 1e9;
const lint inf18 = 1e18;
template <class T> inline void YES(T condition) {
if (condition)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
template <class T> inline void Yes(T condition) {
if (condition)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
template <class T = string, class U = char>
int character_count(T text, U character) {
int ans = 0;
for (U i : text) {
ans += (i == character);
}
return ans;
}
lint power(lint base, lint exponent, lint module) {
if (exponent % 2) {
return power(base, exponent - 1, module) * base % module;
} else if (exponent) {
lint root_ans = power(base, exponent / 2, module);
return root_ans * root_ans % module;
} else {
return 1;
}
}
struct position {
int y, x;
};
position mv[4] = {
{0, -1},
{1, 0},
{0, 1},
{-1, 0}}; // double euclidean(position first, position second){ return
// sqrt((second.x - first.x) * (second.x - first.x) + (second.y -
// first.y) * (second.y - first.y)); }
template <class T, class U> string to_string(pair<T, U> x) {
return to_string(x.first) + "," + to_string(x.second);
}
string to_string(string x) { return x; }
template <class itr> void array_output(itr start, itr goal) {
string ans;
for (auto i = start; i != goal; i++)
ans += to_string(*i) + " ";
if (!ans.empty())
ans.pop_back();
cout << ans << endl;
}
template <class itr> void cins(itr first, itr last) {
for (auto i = first; i != last; i++) {
cin >> (*i);
}
}
template <class T> T gcd(T a, T b) {
if (a && b) {
return gcd(min(a, b), max(a, b) % min(a, b));
} else {
return a;
}
}
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
struct combination {
vector<lint> fact, inv;
combination(int sz) : fact(sz + 1), inv(sz + 1) {
fact[0] = 1;
for (int i = 1; i <= sz; i++) {
fact[i] = fact[i - 1] * i % mod;
}
inv[sz] = power(fact[sz], mod - 2, mod);
for (int i = sz - 1; i >= 0; i--) {
inv[i] = inv[i + 1] * (i + 1) % mod;
}
}
lint P(int n, int r) {
if (r < 0 || n < r)
return 0;
return (fact[n] * inv[n - r] % mod);
}
lint C(int p, int q) {
if (q < 0 || p < q)
return 0;
return (fact[p] * inv[q] % mod * inv[p - q] % mod);
}
};
template <class itr> bool next_sequence(itr first, itr last, int max_bound) {
itr now = last;
while (now != first) {
now--;
(*now)++;
if ((*now) == max_bound) {
(*now) = 0;
} else {
return true;
}
}
return false;
}
template <class itr, class itr2>
bool next_sequence2(itr first, itr last, itr2 first2, itr2 last2) {
itr now = last;
itr2 now2 = last2;
while (now != first) {
now--, now2--;
(*now)++;
if ((*now) == (*now2)) {
(*now) = 0;
} else {
return true;
}
}
return false;
}
inline int at(lint i, int j) { return (i >> j) & 1; }
int N;
vector<int> num[2];
int dp[1 << 18][20];
int card(int already_bit, int now, int back) {
if (now == N) {
return 0;
} else if (dp[already_bit][back] != -1) {
return dp[already_bit][back];
}
int ans = inf9, cnt = 0;
for (int i = N - 1; i >= 0; i--) {
if (at(already_bit, i)) {
cnt++;
continue;
}
// cout << i << endl;
if (back < 18) {
int back_num = num[abs(back - now + 1) % 2][back];
int this_num = num[abs(i - now) % 2][i];
if (back_num > this_num) {
continue;
}
}
ans = min(ans, card(already_bit | (1 << i), now + 1, i) + cnt);
}
return dp[already_bit][back] = ans;
}
int main() {
cin >> N;
num[0].resize(N);
num[1].resize(N);
cins(all(num[0]));
cins(all(num[1]));
memset(dp, -1, sizeof(dp));
int ans = card(0, 0, 18);
if (ans >= inf9) {
cout << -1 << endl;
} else {
cout << ans << endl;
}
}
| replace | 156 | 157 | 156 | 157 | TLE | |
p02798 | C++ | Runtime Error | /*** author: yuji9511 ***/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using lpair = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i, m, n) for (ll i = (m); i < (n); i++)
#define rrep(i, m, n) for (ll i = (m); i >= (n); i--)
#define printa(x, n) \
for (ll i = 0; i < n; i++) { \
cout << (x[i]) << " \n"[i == n - 1]; \
};
void print() {}
template <class H, class... T> void print(H &&h, T &&...t) {
cout << h << " \n"[sizeof...(t) == 0];
print(forward<T>(t)...);
}
struct BIT { // 1-indexed
private:
int n, n2;
vector<ll> bit;
public:
BIT(ll N) {
n = N;
bit.assign(n + 1, 0);
n2 = 1;
while (n2 * 2 <= n)
n2 *= 2;
}
ll sum(ll x) { //[1, x]
ll res = 0;
for (ll i = x; i > 0; i -= i & -i)
res += bit[i];
return res;
}
ll sum(ll lv, ll rv) { // [lv, rv)
return sum(rv - 1) - sum(lv);
}
void add(ll x, ll v) {
if (x == 0)
return;
for (ll i = x; i <= n; i += i & -i)
bit[i] += v;
}
ll lower_bound(ll w) {
if (w <= 0)
return 0;
ll x = 0;
for (ll k = n2; k > 0; k /= 2) {
if (x + k <= n && bit[x + k] < w) {
w -= bit[x + k];
x += k;
}
}
return x + 1;
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll N;
cin >> N;
ll A[20], B[20];
rep(i, 0, N) cin >> A[i];
rep(i, 0, N) cin >> B[i];
ll ans = INF;
rep(bit, 0, (1LL << N)) {
vector<ll> tt;
rep(i, 0, N) { // 0が上
tt.push_back((bit >> i) & 1);
}
ll cnt_even = 0, cnt_odd = 0;
rep(i, 0, N) {
if (tt[i] == 0) {
if (i % 2 == 0) {
cnt_even++;
} else {
cnt_odd++;
}
} else {
if (i % 2 == 0) {
cnt_odd++;
} else {
cnt_even++;
}
}
}
if (N % 2 == 0) {
if (cnt_odd != cnt_even)
continue;
} else {
if (cnt_even != cnt_odd + 1)
continue;
}
// printa(tt, N);
vector<lpair> odd, even;
rep(i, 0, N) {
if (tt[i] == 0 && i % 2 == 0) {
even.push_back({A[i], i});
} else if (tt[i] == 0 && i % 2 == 1) {
odd.push_back({A[i], i});
} else if (tt[i] == 1 && i % 2 == 0) {
odd.push_back({B[i], i});
} else {
even.push_back({B[i], i});
}
}
sort(even.begin(), even.end(), [](lpair l1, lpair l2) {
if (l1.first == l2.first) {
return l1.second < l2.second;
}
return l1.first < l2.first;
});
sort(odd.begin(), odd.end(), [](lpair l1, lpair l2) {
if (l1.first == l2.first) {
return l1.second < l2.second;
}
return l1.first < l2.first;
});
bool ok = true;
ll se = even.size(), so = odd.size();
// rep(i,0,se){
// print(even[i].first, even[i].second);
// }
// rep(i,0,so){
// print(odd[i].first, odd[i].second);
// }
rep(i, 0, se - 1) {
if (even[i].first > odd[i].first)
ok = false;
}
rep(i, 0, so - 1) {
if (odd[i].first > even[i + 1].first)
ok = false;
}
if (N % 2 == 0) {
if (even[se - 1].first > odd[so - 1].first)
ok = false;
}
if (N % 2 == 1) {
if (odd[so - 1].first > even[so].first)
ok = false;
}
// print(ok);
if (not ok)
continue;
vector<ll> v2;
rep(i, 0, so) {
v2.push_back(even[i].second);
v2.push_back(odd[i].second);
}
if (N % 2 == 1)
v2.push_back(even[se - 1].second);
// if(bit == 0) printa(v2, N);
BIT bb(20);
ll tentou = 0;
rep(i, 0, N) {
tentou += bb.sum(v2[i] + 1);
bb.add(v2[i] + 1, 1);
}
tentou = N * (N - 1) / 2 - tentou;
ans = min(ans, tentou);
}
if (ans == INF) {
print(-1);
} else {
print(ans);
}
} | /*** author: yuji9511 ***/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using lpair = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i, m, n) for (ll i = (m); i < (n); i++)
#define rrep(i, m, n) for (ll i = (m); i >= (n); i--)
#define printa(x, n) \
for (ll i = 0; i < n; i++) { \
cout << (x[i]) << " \n"[i == n - 1]; \
};
void print() {}
template <class H, class... T> void print(H &&h, T &&...t) {
cout << h << " \n"[sizeof...(t) == 0];
print(forward<T>(t)...);
}
struct BIT { // 1-indexed
private:
int n, n2;
vector<ll> bit;
public:
BIT(ll N) {
n = N;
bit.assign(n + 1, 0);
n2 = 1;
while (n2 * 2 <= n)
n2 *= 2;
}
ll sum(ll x) { //[1, x]
ll res = 0;
for (ll i = x; i > 0; i -= i & -i)
res += bit[i];
return res;
}
ll sum(ll lv, ll rv) { // [lv, rv)
return sum(rv - 1) - sum(lv);
}
void add(ll x, ll v) {
if (x == 0)
return;
for (ll i = x; i <= n; i += i & -i)
bit[i] += v;
}
ll lower_bound(ll w) {
if (w <= 0)
return 0;
ll x = 0;
for (ll k = n2; k > 0; k /= 2) {
if (x + k <= n && bit[x + k] < w) {
w -= bit[x + k];
x += k;
}
}
return x + 1;
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll N;
cin >> N;
ll A[20], B[20];
rep(i, 0, N) cin >> A[i];
rep(i, 0, N) cin >> B[i];
ll ans = INF;
if (N == 1) {
print(0);
return 0;
}
rep(bit, 0, (1LL << N)) {
vector<ll> tt;
rep(i, 0, N) { // 0が上
tt.push_back((bit >> i) & 1);
}
ll cnt_even = 0, cnt_odd = 0;
rep(i, 0, N) {
if (tt[i] == 0) {
if (i % 2 == 0) {
cnt_even++;
} else {
cnt_odd++;
}
} else {
if (i % 2 == 0) {
cnt_odd++;
} else {
cnt_even++;
}
}
}
if (N % 2 == 0) {
if (cnt_odd != cnt_even)
continue;
} else {
if (cnt_even != cnt_odd + 1)
continue;
}
// printa(tt, N);
vector<lpair> odd, even;
rep(i, 0, N) {
if (tt[i] == 0 && i % 2 == 0) {
even.push_back({A[i], i});
} else if (tt[i] == 0 && i % 2 == 1) {
odd.push_back({A[i], i});
} else if (tt[i] == 1 && i % 2 == 0) {
odd.push_back({B[i], i});
} else {
even.push_back({B[i], i});
}
}
sort(even.begin(), even.end(), [](lpair l1, lpair l2) {
if (l1.first == l2.first) {
return l1.second < l2.second;
}
return l1.first < l2.first;
});
sort(odd.begin(), odd.end(), [](lpair l1, lpair l2) {
if (l1.first == l2.first) {
return l1.second < l2.second;
}
return l1.first < l2.first;
});
bool ok = true;
ll se = even.size(), so = odd.size();
// rep(i,0,se){
// print(even[i].first, even[i].second);
// }
// rep(i,0,so){
// print(odd[i].first, odd[i].second);
// }
rep(i, 0, se - 1) {
if (even[i].first > odd[i].first)
ok = false;
}
rep(i, 0, so - 1) {
if (odd[i].first > even[i + 1].first)
ok = false;
}
if (N % 2 == 0) {
if (even[se - 1].first > odd[so - 1].first)
ok = false;
}
if (N % 2 == 1) {
if (odd[so - 1].first > even[so].first)
ok = false;
}
// print(ok);
if (not ok)
continue;
vector<ll> v2;
rep(i, 0, so) {
v2.push_back(even[i].second);
v2.push_back(odd[i].second);
}
if (N % 2 == 1)
v2.push_back(even[se - 1].second);
// if(bit == 0) printa(v2, N);
BIT bb(20);
ll tentou = 0;
rep(i, 0, N) {
tentou += bb.sum(v2[i] + 1);
bb.add(v2[i] + 1, 1);
}
tentou = N * (N - 1) / 2 - tentou;
ans = min(ans, tentou);
}
if (ans == INF) {
print(-1);
} else {
print(ans);
}
} | insert | 73 | 73 | 73 | 77 | 0 | |
p02798 | C++ | Runtime Error | #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
void cmin(int &lhs, int rhs) { lhs = min(lhs, rhs); }
const int INF = 1e9;
int main() {
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<vector<int>> dp(1 << n, vector<int>(n, INF));
dp[0][0] = 0;
auto build = [&](int cnt, int id) {
if (cnt < 0)
return 0;
else if ((cnt - id) % 2)
return b[id];
else
return a[id];
};
for (int bit = 0; bit < ((1 << n) - 1); bit++) {
int pc = __builtin_popcount(bit);
for (int i = 0; i < n; i++) {
int x = build(pc - 1, i);
int piyo = pc;
for (int j = 0; j < n; j++) {
if ((bit >> j) & 1) {
piyo--;
continue;
}
int y = build(pc, j);
if (x <= y) {
int nexBit = bit | (1 << j);
cmin(dp[nexBit][j], dp[bit][i] + piyo);
}
}
}
}
int ans = *min_element(dp[(1 << n) - 1].begin(), dp[(1 << n) - 1].end());
if (ans == INF) {
cout << -1 << endl;
} else {
assert(ans % 2 == 0);
cout << ans / 2 << endl;
}
return 0;
}
| #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
void cmin(int &lhs, int rhs) { lhs = min(lhs, rhs); }
const int INF = 1e9;
int main() {
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<vector<int>> dp(1 << n, vector<int>(n, INF));
dp[0][0] = 0;
auto build = [&](int cnt, int id) {
if (cnt < 0)
return 0;
else if ((cnt - id) % 2)
return b[id];
else
return a[id];
};
for (int bit = 0; bit < ((1 << n) - 1); bit++) {
int pc = __builtin_popcount(bit);
for (int i = 0; i < n; i++) {
int x = build(pc - 1, i);
int piyo = pc;
for (int j = 0; j < n; j++) {
if ((bit >> j) & 1) {
piyo--;
continue;
}
int y = build(pc, j);
if (x <= y) {
int nexBit = bit | (1 << j);
cmin(dp[nexBit][j], dp[bit][i] + piyo);
}
}
}
}
int ans = *min_element(dp[(1 << n) - 1].begin(), dp[(1 << n) - 1].end());
if (ans == INF) {
cout << -1 << endl;
} else {
cout << ans << endl;
}
return 0;
}
| replace | 74 | 76 | 74 | 75 | -6 | f1bfa46b-4be9-44f4-8984-40112c55b262.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02798/C++/s747951614.cpp:73: int main(): Assertion `ans%2==0' failed.
|
p02798 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
int n, a[2][20], ans = 1e9;
int main() {
scanf("%d", &n);
rep(o, 2) rep(i, n) scanf("%d", a[o] + i);
rep(bt, 1 << n) {
int rev[20] = {}, c[] = {-1, -2};
bool ok = true;
vector<int> v[51];
rep(i, n) v[a[bt >> i & 1][i]].push_back(i);
rep(i, 51) {
for (int t : v[i])
rev[c[abs(c[1] - t) % 2 == (bt >> t) % 2] += 2] = t + 1;
if (abs(c[0] - c[1]) > 1)
ok = false;
}
if (ok) {
constexpr int sz = 32;
int sm = 0;
vector<int> dat(sz + 1);
rep(i, n) {
for (int j = rev[i]; j; j -= j & -j)
sm += dat[j];
for (int j = rev[i]; j <= sz; j += j & -j)
dat[j]++;
}
sm = n * (n - 1) / 2 - sm;
ans = min(ans, sm);
}
}
printf("%d\n", ans == (int)1e9 ? -1 : ans);
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
int n, a[2][20], ans = 1e9;
int main() {
scanf("%d", &n);
rep(o, 2) rep(i, n) scanf("%d", a[o] + i);
rep(bt, 1 << n) {
int rev[40] = {}, c[] = {-2, -1};
bool ok = true;
vector<int> v[51];
rep(i, n) v[a[bt >> i & 1][i]].push_back(i);
rep(i, 51) {
for (int t : v[i])
rev[c[abs(c[1] - t) % 2 == (bt >> t) % 2] += 2] = t + 1;
if (abs(c[0] - c[1]) > 1)
ok = false;
}
if (ok) {
constexpr int sz = 32;
int sm = 0;
vector<int> dat(sz + 1);
rep(i, n) {
for (int j = rev[i]; j; j -= j & -j)
sm += dat[j];
for (int j = rev[i]; j <= sz; j += j & -j)
dat[j]++;
}
sm = n * (n - 1) / 2 - sm;
ans = min(ans, sm);
}
}
printf("%d\n", ans == (int)1e9 ? -1 : ans);
} | replace | 8 | 9 | 8 | 9 | 0 | |
p02798 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define long long long
using namespace std;
const int N = 1e6, inf = 1e9;
int aa[18 + 2], bb[18 + 2], dp[18 + 2][2][(1 << 18) + 2], n,
pre[18 + 2][(1 << 18) + 2];
inline int dfs(int last, int f, int msk) {
if (msk == (1 << n) - 1)
return 0;
if (dp[last][f][msk] != -1)
return dp[last][f][msk];
int l = aa[last];
if (f)
l = bb[last];
int ret = inf;
int cnt = __builtin_popcount(msk);
for (int i = 0; i < n; i++) {
if (msk & (1 << i))
continue;
int p = pre[i][msk] + i; // current position of i
int fl = (pre[i][msk] + p - cnt) % 2;
int now = aa[i];
if (fl)
now = bb[i];
if (now >= l)
ret = min(ret, p - cnt + dfs(i, fl, msk | (1 << i)));
}
return ret;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++)
cin >> aa[i];
for (int i = 0; i < n; i++)
cin >> bb[i];
// for(int i=0;i<n;i++)aa[i]=1+rand()%50;
// for(int i=0;i<n;i++)bb[i]=1+rand()%50;
for (int i = 0; i < n; i++) {
for (int msk = 0; msk < (1 << n); msk++) {
int cnt = 0;
if (msk & (1 << i))
continue;
for (int j = i + 1; j < n; j++)
cnt += (bool)(msk & (1 << j));
pre[i][msk] = cnt;
}
}
// cout<<pre[0][14]<<endl;
memset(dp, -1, sizeof(dp));
int mn = dfs(0, 0, (1 << 0));
for (int i = 1; i < n; i++)
mn = min(mn, i + dfs(i, i % 2, (1 << i)));
if (mn == inf)
mn = -1;
cout << mn << endl;
return 0;
} | #include <bits/stdc++.h>
#define long long long
using namespace std;
const int N = 1e6, inf = 1e9;
int aa[18 + 2], bb[18 + 2], dp[18 + 2][2][(1 << 18) + 2], n,
pre[18 + 2][(1 << 18) + 2];
inline int dfs(int last, int f, int msk) {
if (msk == (1 << n) - 1)
return 0;
if (dp[last][f][msk] != -1)
return dp[last][f][msk];
int l = aa[last];
if (f)
l = bb[last];
int ret = inf;
int cnt = __builtin_popcount(msk);
for (int i = 0; i < n; i++) {
if (msk & (1 << i))
continue;
int p = pre[i][msk] + i; // current position of i
int fl = (pre[i][msk] + p - cnt) % 2;
int now = aa[i];
if (fl)
now = bb[i];
if (now >= l)
ret = min(ret, p - cnt + dfs(i, fl, msk | (1 << i)));
}
return dp[last][f][msk] = ret;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++)
cin >> aa[i];
for (int i = 0; i < n; i++)
cin >> bb[i];
// for(int i=0;i<n;i++)aa[i]=1+rand()%50;
// for(int i=0;i<n;i++)bb[i]=1+rand()%50;
for (int i = 0; i < n; i++) {
for (int msk = 0; msk < (1 << n); msk++) {
int cnt = 0;
if (msk & (1 << i))
continue;
for (int j = i + 1; j < n; j++)
cnt += (bool)(msk & (1 << j));
pre[i][msk] = cnt;
}
}
// cout<<pre[0][14]<<endl;
memset(dp, -1, sizeof(dp));
int mn = dfs(0, 0, (1 << 0));
for (int i = 1; i < n; i++)
mn = min(mn, i + dfs(i, i % 2, (1 << i)));
if (mn == inf)
mn = -1;
cout << mn << endl;
return 0;
} | replace | 28 | 29 | 28 | 29 | TLE | |
p02798 | C++ | Time Limit Exceeded | #pragma GCC optimize("Ofast,inline,unroll-loops,fast-math")
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
int f(vector<int> v) {
int res = 0;
for (int i = 0; i < v.size(); i++) {
// cout << i << " ";
for (int j = i + 1; j < v.size(); j++) {
if (v[i] > v[j])
res++;
}
}
// cout << endl;
return res;
}
void dfs(const vector<pair<int, int>> &vp1, const vector<pair<int, int>> &vp2,
int index1, int index2, int last, vector<int> seq, vector<int> v,
int &res) {
if (vp1.size() - index1 + vp2.size() - index2 + seq.size() < vp1.size())
return;
if (seq.size() == vp1.size()) {
int r = f(seq);
if (res == -1 || r < res)
res = r;
return;
}
if (seq.size() % 2 == 0) {
while (index1 < vp1.size()) {
if (vp1[index1].first >= last && v[vp1[index1].second] == 0) {
seq.push_back(vp1[index1].second);
v[vp1[index1].second] = 1;
dfs(vp1, vp2, index1 + 1, index2, vp1[index1].first, seq, v, res);
seq.pop_back();
v[vp1[index1].second] = 0;
}
index1++;
}
} else {
while (index2 < vp2.size()) {
if (vp2[index2].first >= last && v[vp2[index2].second] == 0) {
seq.push_back(vp2[index2].second);
v[vp2[index2].second] = 1;
dfs(vp1, vp2, index1, index2 + 1, vp2[index2].first, seq, v, res);
seq.pop_back();
v[vp2[index2].second] = 0;
}
index2++;
}
}
}
int main(int argc, char *argv[]) {
int n;
cin >> n;
vector<pair<int, int>> vp1, vp2;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
if (i % 2 == 0)
vp1.push_back(make_pair(a, i));
else
vp2.push_back(make_pair(a, i));
}
for (int i = 0; i < n; i++) {
int b;
cin >> b;
if (i % 2 == 0)
vp2.push_back(make_pair(b, i));
else
vp1.push_back(make_pair(b, i));
}
sort(vp1.begin(), vp1.end());
sort(vp2.begin(), vp2.end());
int res = -1;
int index1 = 0, index2 = 0;
int last = 0;
vector<int> seq;
vector<int> v(n, 0);
dfs(vp1, vp2, index1, index2, last, seq, v, res);
cout << res << endl;
}
| #pragma GCC optimize("Ofast,inline,unroll-loops,fast-math")
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
int f(vector<int> v) {
int res = 0;
for (int i = 0; i < v.size(); i++) {
// cout << i << " ";
for (int j = i + 1; j < v.size(); j++) {
if (v[i] > v[j])
res++;
}
}
// cout << endl;
return res;
}
void dfs(const vector<pair<int, int>> &vp1, const vector<pair<int, int>> &vp2,
int index1, int index2, int last, vector<int> seq, vector<int> v,
int &res) {
if (vp1.size() - index1 + vp2.size() - index2 + seq.size() < vp1.size())
return;
if (seq.size() == vp1.size()) {
int r = f(seq);
if (res == -1 || r < res)
res = r;
return;
}
if (res != -1) {
int r = f(seq);
if (r >= res)
return;
}
if (seq.size() % 2 == 0) {
while (index1 < vp1.size()) {
if (vp1[index1].first >= last && v[vp1[index1].second] == 0) {
seq.push_back(vp1[index1].second);
v[vp1[index1].second] = 1;
dfs(vp1, vp2, index1 + 1, index2, vp1[index1].first, seq, v, res);
seq.pop_back();
v[vp1[index1].second] = 0;
}
index1++;
}
} else {
while (index2 < vp2.size()) {
if (vp2[index2].first >= last && v[vp2[index2].second] == 0) {
seq.push_back(vp2[index2].second);
v[vp2[index2].second] = 1;
dfs(vp1, vp2, index1, index2 + 1, vp2[index2].first, seq, v, res);
seq.pop_back();
v[vp2[index2].second] = 0;
}
index2++;
}
}
}
int main(int argc, char *argv[]) {
int n;
cin >> n;
vector<pair<int, int>> vp1, vp2;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
if (i % 2 == 0)
vp1.push_back(make_pair(a, i));
else
vp2.push_back(make_pair(a, i));
}
for (int i = 0; i < n; i++) {
int b;
cin >> b;
if (i % 2 == 0)
vp2.push_back(make_pair(b, i));
else
vp1.push_back(make_pair(b, i));
}
sort(vp1.begin(), vp1.end());
sort(vp2.begin(), vp2.end());
int res = -1;
int index1 = 0, index2 = 0;
int last = 0;
vector<int> seq;
vector<int> v(n, 0);
dfs(vp1, vp2, index1, index2, last, seq, v, res);
cout << res << endl;
}
| insert | 46 | 46 | 46 | 52 | TLE | |
p02798 | C++ | Time Limit Exceeded | #include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <complex>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <cassert>
#include <functional>
typedef long long ll;
using namespace std;
#ifndef LOCAL
#define debug(x) ;
#else
#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;
template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
out << "{" << p.first << ", " << p.second << "}";
return out;
}
template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) {
out << '{';
for (const T &item : v)
out << item << ", ";
out << "\b\b}";
return out;
}
#endif
#define mod 1000000007 // 1e9+7(prime number)
#define INF 1000000000 // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 20
int dp[1 << 18][50];
int main() {
int N, A[SIZE], B[SIZE];
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++) {
A[i]--;
B[i]--;
}
for (int i = 0; i < (1 << N); i++)
for (int j = 0; j < 50; j++)
dp[i][j] = INF;
dp[0][0] = 0;
for (int i = 0; i < (1 << N); i++) {
for (int k = 0; k < 50; k++) {
cerr << i << " " << k << " : " << dp[i][k] << endl;
if (dp[i][k] == INF)
continue;
for (int j = 0; j < N; j++) {
if (i & (1 << j))
continue;
int maskR = ((1 << N) - 1) - ((1 << (j + 1)) - 1);
int maskL = ((1 << j) - 1);
int cR = __builtin_popcount(i & maskR);
int cL = j - __builtin_popcount(i & maskL);
if ((cL + cR) % 2) {
if (k <= B[j]) {
dp[i | (1 << j)][B[j]] = min(dp[i | (1 << j)][B[j]], dp[i][k] + cL);
}
} else {
if (k <= A[j]) {
dp[i | (1 << j)][A[j]] = min(dp[i | (1 << j)][A[j]], dp[i][k] + cL);
}
}
}
}
}
int ans = INF;
for (int i = 0; i < 50; i++) {
ans = min(ans, dp[(1 << N) - 1][i]);
}
if (ans == INF)
ans = -1;
cout << ans << endl;
return 0;
}
| #include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <complex>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <cassert>
#include <functional>
typedef long long ll;
using namespace std;
#ifndef LOCAL
#define debug(x) ;
#else
#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;
template <typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
out << "{" << p.first << ", " << p.second << "}";
return out;
}
template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) {
out << '{';
for (const T &item : v)
out << item << ", ";
out << "\b\b}";
return out;
}
#endif
#define mod 1000000007 // 1e9+7(prime number)
#define INF 1000000000 // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 20
int dp[1 << 18][50];
int main() {
int N, A[SIZE], B[SIZE];
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++) {
A[i]--;
B[i]--;
}
for (int i = 0; i < (1 << N); i++)
for (int j = 0; j < 50; j++)
dp[i][j] = INF;
dp[0][0] = 0;
for (int i = 0; i < (1 << N); i++) {
for (int k = 0; k < 50; k++) {
if (dp[i][k] == INF)
continue;
for (int j = 0; j < N; j++) {
if (i & (1 << j))
continue;
int maskR = ((1 << N) - 1) - ((1 << (j + 1)) - 1);
int maskL = ((1 << j) - 1);
int cR = __builtin_popcount(i & maskR);
int cL = j - __builtin_popcount(i & maskL);
if ((cL + cR) % 2) {
if (k <= B[j]) {
dp[i | (1 << j)][B[j]] = min(dp[i | (1 << j)][B[j]], dp[i][k] + cL);
}
} else {
if (k <= A[j]) {
dp[i | (1 << j)][A[j]] = min(dp[i | (1 << j)][A[j]], dp[i][k] + cL);
}
}
}
}
}
int ans = INF;
for (int i = 0; i < 50; i++) {
ans = min(ans, dp[(1 << N) - 1][i]);
}
if (ans == INF)
ans = -1;
cout << ans << endl;
return 0;
}
| delete | 74 | 76 | 74 | 74 | TLE | |
p02798 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
vector<ll> A, B;
ll n;
ll cnt(vector<ll> P, ll x) {
vector<pair<ll, ll>> odd, even, U(n);
ll ret = 0;
for (ll i = 0; i < n; i++) {
// iが偶数番目でそのカードが表で終わると決めた or
// iが奇数番目でそのカードが裏で終わると決めた
// => ソートされると偶数番目で終わる
// (奇数回動くと裏で終わるため)
// 奇数番目で終わるときはその逆
if (i % 2 == (x >> i) % 2)
even.emplace_back(P[i], i);
else
odd.emplace_back(P[i], i);
}
// 偶数番目で終わるカードと奇数番目で終わるカードの枚数差が2枚以上はありえない.
if (abs((int)even.size() - (int)odd.size()) > 1)
return INF;
// それぞれカードの値でソート
sort(odd.begin(), odd.end(),
[](const pair<ll, ll> &a, const pair<ll, ll> &b) {
if (a.first == b.first)
return a.second < b.second;
return a.first < b.first;
});
sort(even.begin(), even.end(),
[](const pair<ll, ll> &a, const pair<ll, ll> &b) {
if (a.first == b.first)
return a.second < b.second;
return a.first < b.first;
});
// ソート後の数列を復元
for (ll i = 0; i < n; i++) {
if (i % 2 == 0)
U[i] = even[i / 2];
else
U[i] = odd[i / 2];
}
// ここで昇順になっていない場合はアウト
for (ll i = 0; i < n - 1; i++) {
if (U[i].first > U[i + 1].first)
return INF;
}
// もとの数列PからUになるためにどのくらい移動したか(転倒数)
for (ll i = 0; i < n - 1; i++) {
for (ll j = n - 1; j >= i + 1; j--) {
if (U[j].second < U[j - 1].second) {
swap(U[j], U[j - 1]);
ret++;
}
}
}
return ret;
}
ll solve(ll x) {
vector<ll> P(n);
for (ll i = 0; i < n; i++) {
if ((x >> i) % 2 == 0)
P[i] = A[i];
else
P[i] = B[i];
}
return cnt(P, x);
}
int main() {
ll m, i, j, k;
cin >> n;
A = vector<ll>(n);
B = vector<ll>(n);
for (i = 0; i < n; i++)
cin >> A[i];
for (i = 0; i < n; i++)
cin >> B[i];
ll ret = INF;
for (i = 0; i < (1ll << n); i++) {
ret = min(ret, solve(i));
}
if (ret == INF)
ret = -1;
cout << ret << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
vector<ll> A, B;
ll n;
ll cnt(vector<ll> P, ll x) {
vector<pair<ll, ll>> odd, even, U(n);
ll ret = 0;
for (ll i = 0; i < n; i++) {
// iが偶数番目でそのカードが表で終わると決めた or
// iが奇数番目でそのカードが裏で終わると決めた
// => ソートされると偶数番目で終わる
// (奇数回動くと裏で終わるため)
// 奇数番目で終わるときはその逆
if (i % 2 == (x >> i) % 2)
even.emplace_back(P[i], i);
else
odd.emplace_back(P[i], i);
}
// 偶数番目で終わるカードと奇数番目で終わるカードの枚数差が2枚以上はありえない.
if ((int)even.size() - (int)odd.size() > 1 ||
(int)even.size() - (int)odd.size() < 0)
return INF;
// それぞれカードの値でソート
sort(odd.begin(), odd.end(),
[](const pair<ll, ll> &a, const pair<ll, ll> &b) {
if (a.first == b.first)
return a.second < b.second;
return a.first < b.first;
});
sort(even.begin(), even.end(),
[](const pair<ll, ll> &a, const pair<ll, ll> &b) {
if (a.first == b.first)
return a.second < b.second;
return a.first < b.first;
});
// ソート後の数列を復元
for (ll i = 0; i < n; i++) {
if (i % 2 == 0)
U[i] = even[i / 2];
else
U[i] = odd[i / 2];
}
// ここで昇順になっていない場合はアウト
for (ll i = 0; i < n - 1; i++) {
if (U[i].first > U[i + 1].first)
return INF;
}
// もとの数列PからUになるためにどのくらい移動したか(転倒数)
for (ll i = 0; i < n - 1; i++) {
for (ll j = n - 1; j >= i + 1; j--) {
if (U[j].second < U[j - 1].second) {
swap(U[j], U[j - 1]);
ret++;
}
}
}
return ret;
}
ll solve(ll x) {
vector<ll> P(n);
for (ll i = 0; i < n; i++) {
if ((x >> i) % 2 == 0)
P[i] = A[i];
else
P[i] = B[i];
}
return cnt(P, x);
}
int main() {
ll m, i, j, k;
cin >> n;
A = vector<ll>(n);
B = vector<ll>(n);
for (i = 0; i < n; i++)
cin >> A[i];
for (i = 0; i < n; i++)
cin >> B[i];
ll ret = INF;
for (i = 0; i < (1ll << n); i++) {
ret = min(ret, solve(i));
}
if (ret == INF)
ret = -1;
cout << ret << endl;
return 0;
} | replace | 25 | 26 | 25 | 27 | 0 | |
p02798 | C++ | Time Limit Exceeded | #define _USE_MATH_DEFINES
#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int A[18];
int B[18];
int dp[1 << 18][18];
bool in_bit(int bit_set, int ind) {
if (((bit_set >> ind) & 1) == 1) {
return true;
}
return false;
}
int add_bit(int bit_set, int ind) { return bit_set ^ (1 << ind); }
int bit_set_size(int bit_set, int N) {
int ans = 0;
for (int i = 0; i < N; i++) {
if (((bit_set >> i) & 1) == 1) {
ans++;
}
}
return ans;
}
int get_swap(int bit_set, int ind, int N) {
int ans = 0;
for (int i = 0; i < N; i++) {
if (((bit_set >> i) & 1) == 1) {
if (i > ind) {
ans++;
}
}
}
return ans;
}
int get_card_num(int ind, int org_ind) {
if (ind % 2 == org_ind % 2) {
return A[org_ind];
}
return B[org_ind];
}
int main() {
int N;
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 < (1 << N); i++) {
for (int j = 0; j < N; j++) {
dp[i][j] = INT_MAX;
}
}
for (int i = 0; i < N; i++) {
dp[0][i] = 0;
}
for (int bit_set = 0; bit_set < (1 << N) - 1; bit_set++) {
for (int i = 0; i < N; i++) {
if (dp[bit_set][i] != INT_MAX) {
for (int j = 0; j < N; j++) {
if (!in_bit(bit_set, j)) {
int cur_ind = bit_set_size(bit_set, N) + 1;
int cur_card_num = get_card_num(cur_ind, j);
int prev_card_num = get_card_num(cur_ind - 1, i);
if (prev_card_num <= cur_card_num) {
int next_bit_set = add_bit(bit_set, i);
dp[next_bit_set][j] =
min(dp[next_bit_set][j],
dp[bit_set][i] + get_swap(next_bit_set, j, N));
}
}
}
}
}
}
int ans = INT_MAX;
for (int i = 0; i < N; i++) {
int tmp = ((1 << N) - 1) ^ (1 << i);
ans = min(ans, dp[tmp][i]);
}
if (ans == INT_MAX) {
cout << -1 << endl;
} else {
cout << ans << endl;
}
return 0;
}
| #define _USE_MATH_DEFINES
#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int A[18];
int B[18];
int dp[1 << 18][18];
bool in_bit(int bit_set, int ind) {
if (((bit_set >> ind) & 1) == 1) {
return true;
}
return false;
}
int add_bit(int bit_set, int ind) { return bit_set ^ (1 << ind); }
int bit_set_size(int bit_set, int N) {
int ans = 0;
for (int i = 0; i < N; i++) {
if (((bit_set >> i) & 1) == 1) {
ans++;
}
}
return ans;
}
int get_swap(int bit_set, int ind, int N) {
int ans = 0;
for (int i = 0; i < N; i++) {
if (((bit_set >> i) & 1) == 1) {
if (i > ind) {
ans++;
}
}
}
return ans;
}
int get_card_num(int ind, int org_ind) {
if (ind % 2 == org_ind % 2) {
return A[org_ind];
}
return B[org_ind];
}
int main() {
int N;
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 < (1 << N); i++) {
for (int j = 0; j < N; j++) {
dp[i][j] = INT_MAX;
}
}
for (int i = 0; i < N; i++) {
dp[0][i] = 0;
}
for (int bit_set = 0; bit_set < (1 << N) - 1; bit_set++) {
for (int i = 0; i < N; i++) {
if (dp[bit_set][i] != INT_MAX) {
for (int j = 0; j < N; j++) {
if (i == j) {
continue;
}
if (!in_bit(bit_set, j)) {
int cur_ind = bit_set_size(bit_set, N) + 1;
int cur_card_num = get_card_num(cur_ind, j);
int prev_card_num = get_card_num(cur_ind - 1, i);
if (prev_card_num <= cur_card_num) {
int next_bit_set = add_bit(bit_set, i);
dp[next_bit_set][j] =
min(dp[next_bit_set][j],
dp[bit_set][i] + get_swap(next_bit_set, j, N));
}
}
}
}
}
}
int ans = INT_MAX;
for (int i = 0; i < N; i++) {
int tmp = ((1 << N) - 1) ^ (1 << i);
ans = min(ans, dp[tmp][i]);
}
if (ans == INT_MAX) {
cout << -1 << endl;
} else {
cout << ans << endl;
}
return 0;
}
| insert | 79 | 79 | 79 | 82 | TLE | |
p02798 | C++ | Time Limit Exceeded | #define DEBUG 0
#include <bits/stdc++.h>
#define loop(n) \
for (lint ngtkana_is_a_genius = 0; ngtkana_is_a_genius < lint(n); \
ngtkana_is_a_genius++)
#define rep(i, begin, end) for (lint i = lint(begin); (i) < lint(end); i++)
#define all(v) v.begin(), v.end()
#define rand(l, r) std::uniform_int_distribution<>(l, r)(mt)
using lint = long long;
auto mt = std::mt19937_64(std::random_device{}());
auto cmn = [](auto &&a, auto b) {
if (a > b) {
a = b;
return true;
}
return false;
};
auto cmx = [](auto &&a, auto b) {
if (a < b) {
a = b;
return true;
}
return false;
};
void debug_impl() { std::cerr << std::endl; }
template <typename Head, typename... Tail>
void debug_impl(Head head, Tail... tail) {
std::cerr << " " << head;
debug_impl(tail...);
}
#if DEBUG
#define debug(...) \
do { \
std::cerr << std::boolalpha << "[" << #__VA_ARGS__ << "]:"; \
debug_impl(__VA_ARGS__); \
std::cerr << std::noboolalpha; \
} while (false)
#else
#define debug(...) \
{}
#endif
template <typename Container, typename Value = typename Container::value_type,
std::enable_if_t<!std::is_same<Container, std::string>::value,
std::nullptr_t> = nullptr>
std::istream &operator>>(std::istream &is, Container &v) {
for (auto &x : v) {
is >> x;
}
return is;
}
template <typename Container, typename Value = typename Container::value_type,
std::enable_if_t<!std::is_same<Container, std::string>::value,
std::nullptr_t> = nullptr>
std::ostream &operator<<(std::ostream &os, Container const &v) {
os << "{";
for (auto it = v.begin(); it != v.end(); it++) {
os << (it != v.begin() ? "," : "") << *it;
}
return os << "}";
}
template <class Value> bool bat(Value x, std::size_t pos) {
return (x >> pos) & Value{1};
}
template <typename T> auto make_vector_impl(size_t sz, T t) {
return std::vector<T>(sz, t);
}
template <size_t N, typename T, typename U,
std::enable_if_t<N == 1, std::nullptr_t> = nullptr>
auto make_vector(size_t sz, U u) {
return make_vector_impl(sz, T(u));
}
template <size_t N, typename T,
std::enable_if_t<N == 1, std::nullptr_t> = nullptr>
auto make_vector(size_t sz) {
return std::vector<T>(sz);
}
template <size_t N, typename T, typename... Args,
std::enable_if_t<N != 1, std::nullptr_t> = nullptr>
auto make_vector(size_t a, Args... args) {
return make_vector_impl(a, make_vector<N - 1, T>(args...));
}
template <typename T, typename Size_t> auto &at(T &t, Size_t i) {
return t.at(i);
}
template <typename T, typename Size_t, typename... Args>
auto &at(T &t, Size_t i, Args... args) {
return at(t.at(i), args...);
}
template <class Seg, class SegPtr> struct segment_tree_entry;
template <class Seg, class SegPtr, class Pointer, class Reference>
struct segment_tree_iterator;
template <typename Value, typename BinaryOp> class segment_tree {
public:
using value_type = Value;
using this_type = segment_tree<Value, BinaryOp>;
using entry_type = segment_tree_entry<this_type, this_type *>;
using const_entry_type = segment_tree_entry<this_type, this_type const *>;
using iterator =
segment_tree_iterator<this_type, this_type *, value_type *, value_type &>;
using const_iterator =
segment_tree_iterator<this_type, this_type const *, value_type const *,
value_type const &>;
friend class segment_tree_entry<this_type, this_type *>;
friend class segment_tree_entry<this_type, this_type const *>;
private:
int sz, n, N;
BinaryOp op;
Value id;
std::vector<Value> table;
auto &op_assign(Value &x, Value y) const { return x = op(x, y); }
void merge(int u) { table.at(u) = op(table.at(2 * u), table.at(2 * u + 1)); }
public:
segment_tree(int sz, BinaryOp op, Value id)
: sz(sz), n(std::pow(2, int(std::log2(2 * sz - 1)))), N(n * 2), op(op),
id(id), table(N, id) {}
iterator begin() { return iterator(this, 0); }
iterator end() { return iterator(this, sz); }
const_iterator begin() const { return const_iterator(this, 0); }
const_iterator end() const { return const_iterator(this, sz); }
auto at(std::size_t i) { return entry_type(this, i); }
auto at(std::size_t i) const { return table.at(n + i); }
auto &lazy_at(std::size_t i) { return table.at(n + i); }
auto &lazy_at(std::size_t i) const { return table.at(n + i); }
auto collect() const {
auto ret = std::vector<Value>(sz);
for (auto i = 0; i < sz; i++) {
ret.at(i) = at(i);
}
return ret;
}
auto query(int l, int r) const {
auto const dfs = [&](auto &&f, int l, int r, int k, int L, int R) -> Value {
return l <= L && R <= r ? table.at(k)
: R <= l || r <= L ? id
: op(f(f, l, r, 2 * k, L, (L + R) / 2),
f(f, l, r, 2 * k + 1, (L + R) / 2, R));
};
return dfs(dfs, l, r, 1, 0, n);
}
void build() {
for (auto i = n - 1; i > 0; i--)
merge(i);
}
};
template <class Seg, class SegPtr> class segment_tree_entry {
using value_type = typename Seg::value_type;
SegPtr seg;
std::size_t i;
auto &get() { return seg->table.at(seg->n + i); }
void build_oneline() {
for (int j = (i + seg->n) / 2; j > 0; j /= 2) {
seg->merge(j);
}
}
public:
segment_tree_entry(SegPtr seg, std::size_t i) : seg(seg), i(i) {}
operator value_type() { return seg->table.at(seg->n + i); }
void operator=(value_type x) {
get() = x;
build_oneline();
}
void operator++() {
get()++;
build_oneline();
}
void operator--() {
get()--;
build_oneline();
}
void operator+=(value_type x) {
get() += x;
build_oneline();
}
void operator-=(value_type x) {
get() -= x;
build_oneline();
}
void operator*=(value_type x) {
get() *= x;
build_oneline();
}
void operator/=(value_type x) {
get() /= x;
build_oneline();
}
};
template <class Seg, class SegPtr, class Pointer, class Reference>
class segment_tree_iterator {
using this_type = segment_tree_iterator<Seg, SegPtr, Pointer, Reference>;
using value_type = typename Seg::value_type;
using pointer = Pointer;
using reference = Reference;
template <class Seg_, class SegPtr_, class Pointer_, class Reference_>
friend bool
operator!=(segment_tree_iterator<Seg_, SegPtr_, Pointer_, Reference_>,
segment_tree_iterator<Seg_, SegPtr_, Pointer_, Reference_>);
SegPtr seg;
std::size_t i;
public:
segment_tree_iterator(SegPtr seg, std::size_t i) : seg(seg), i(i) {}
this_type &operator++() {
i++;
return *this;
}
this_type operator++(int) {
auto old = *this;
++i;
return old;
}
pointer operator->() { return &seg->at(i); }
value_type operator*() { return seg->at(i); }
};
template <class Seg, class SegPtr, class Pointer, class Reference>
bool operator!=(segment_tree_iterator<Seg, SegPtr, Pointer, Reference> a,
segment_tree_iterator<Seg, SegPtr, Pointer, Reference> b) {
return a.i != b.i;
}
template <typename Value, typename BinaryOp>
auto make_segment_tree(int sz, BinaryOp const &op, Value id) {
return segment_tree<Value, BinaryOp>(sz, op, id);
}
int main() {
std::cin.tie(0);
std::cin.sync_with_stdio(false);
int n;
std::cin >> n;
int N = 1 << n;
std::vector<int> a(n), b(n);
std::cin >> a >> b;
int max = 0;
for (int x : a)
cmx(max, x);
for (int x : b)
cmx(max, x);
int inf = std::numeric_limits<int>::max();
auto cal = [&](int bs) -> int {
std::vector<int> c(n);
rep(i, 0, n) { c.at(i) = (bat(bs, i) ? b : a).at(i); }
auto d = c;
std::sort(all(d));
debug(bs, c, d);
auto from = make_vector<2, std::vector<int>>(max + 1, 2);
rep(i, 0, n) {
int x = c.at(i);
from.at(x).at((i % 2) ^ bat(bs, i)).emplace_back(i);
}
auto to = make_vector<2, std::vector<int>>(max + 1, 2);
rep(i, 0, n) {
int x = d.at(i);
to.at(x).at(i % 2).emplace_back(i);
}
// #if DEBUG
// rep(i,0,max+1) {
// debug(i,from.at(i),to.at(i));
// }
// #endif
std::vector<int> p(n);
rep(i, 0, max + 1) rep(j, 0, 2) {
auto const &v0 = at(from, i, j);
auto const &v1 = at(to, i, j);
if (v0.size() != v1.size()) {
return inf;
}
// debug(v0,v1);
int sz = v0.size();
rep(k, 0, sz) { p.at(v0.at(k)) = v1.at(k); }
}
debug(p);
int ans = 0;
auto seg = make_segment_tree<int>(n, std::plus<>{}, 0);
for (int x : p) {
ans += seg.query(x, n);
seg.at(x) += 1;
}
return ans;
};
int ans = inf;
rep(bs, 0, N) { cmn(ans, cal(bs)); }
if (ans == inf)
ans = -1;
std::cout << ans << std::endl;
return 0;
}
/*
裏表を全探索です。ただし裏返っているものは偶数枚です。
どれがどこにいたのかを決めます。
各登場する数字に対し、今いる場所を見ます。そこの偶奇を見ます。
行き先は、裏返っていれば偶奇も替えたもので、そうでなければそのままのものです。
*/
| #define DEBUG 0
#include <bits/stdc++.h>
#define loop(n) \
for (lint ngtkana_is_a_genius = 0; ngtkana_is_a_genius < lint(n); \
ngtkana_is_a_genius++)
#define rep(i, begin, end) for (lint i = lint(begin); (i) < lint(end); i++)
#define all(v) v.begin(), v.end()
#define rand(l, r) std::uniform_int_distribution<>(l, r)(mt)
using lint = long long;
auto mt = std::mt19937_64(std::random_device{}());
auto cmn = [](auto &&a, auto b) {
if (a > b) {
a = b;
return true;
}
return false;
};
auto cmx = [](auto &&a, auto b) {
if (a < b) {
a = b;
return true;
}
return false;
};
void debug_impl() { std::cerr << std::endl; }
template <typename Head, typename... Tail>
void debug_impl(Head head, Tail... tail) {
std::cerr << " " << head;
debug_impl(tail...);
}
#if DEBUG
#define debug(...) \
do { \
std::cerr << std::boolalpha << "[" << #__VA_ARGS__ << "]:"; \
debug_impl(__VA_ARGS__); \
std::cerr << std::noboolalpha; \
} while (false)
#else
#define debug(...) \
{}
#endif
template <typename Container, typename Value = typename Container::value_type,
std::enable_if_t<!std::is_same<Container, std::string>::value,
std::nullptr_t> = nullptr>
std::istream &operator>>(std::istream &is, Container &v) {
for (auto &x : v) {
is >> x;
}
return is;
}
template <typename Container, typename Value = typename Container::value_type,
std::enable_if_t<!std::is_same<Container, std::string>::value,
std::nullptr_t> = nullptr>
std::ostream &operator<<(std::ostream &os, Container const &v) {
os << "{";
for (auto it = v.begin(); it != v.end(); it++) {
os << (it != v.begin() ? "," : "") << *it;
}
return os << "}";
}
template <class Value> bool bat(Value x, std::size_t pos) {
return (x >> pos) & Value{1};
}
template <typename T> auto make_vector_impl(size_t sz, T t) {
return std::vector<T>(sz, t);
}
template <size_t N, typename T, typename U,
std::enable_if_t<N == 1, std::nullptr_t> = nullptr>
auto make_vector(size_t sz, U u) {
return make_vector_impl(sz, T(u));
}
template <size_t N, typename T,
std::enable_if_t<N == 1, std::nullptr_t> = nullptr>
auto make_vector(size_t sz) {
return std::vector<T>(sz);
}
template <size_t N, typename T, typename... Args,
std::enable_if_t<N != 1, std::nullptr_t> = nullptr>
auto make_vector(size_t a, Args... args) {
return make_vector_impl(a, make_vector<N - 1, T>(args...));
}
template <typename T, typename Size_t> auto &at(T &t, Size_t i) {
return t.at(i);
}
template <typename T, typename Size_t, typename... Args>
auto &at(T &t, Size_t i, Args... args) {
return at(t.at(i), args...);
}
template <class Seg, class SegPtr> struct segment_tree_entry;
template <class Seg, class SegPtr, class Pointer, class Reference>
struct segment_tree_iterator;
template <typename Value, typename BinaryOp> class segment_tree {
public:
using value_type = Value;
using this_type = segment_tree<Value, BinaryOp>;
using entry_type = segment_tree_entry<this_type, this_type *>;
using const_entry_type = segment_tree_entry<this_type, this_type const *>;
using iterator =
segment_tree_iterator<this_type, this_type *, value_type *, value_type &>;
using const_iterator =
segment_tree_iterator<this_type, this_type const *, value_type const *,
value_type const &>;
friend class segment_tree_entry<this_type, this_type *>;
friend class segment_tree_entry<this_type, this_type const *>;
private:
int sz, n, N;
BinaryOp op;
Value id;
std::vector<Value> table;
auto &op_assign(Value &x, Value y) const { return x = op(x, y); }
void merge(int u) { table.at(u) = op(table.at(2 * u), table.at(2 * u + 1)); }
public:
segment_tree(int sz, BinaryOp op, Value id)
: sz(sz), n(std::pow(2, int(std::log2(2 * sz - 1)))), N(n * 2), op(op),
id(id), table(N, id) {}
iterator begin() { return iterator(this, 0); }
iterator end() { return iterator(this, sz); }
const_iterator begin() const { return const_iterator(this, 0); }
const_iterator end() const { return const_iterator(this, sz); }
auto at(std::size_t i) { return entry_type(this, i); }
auto at(std::size_t i) const { return table.at(n + i); }
auto &lazy_at(std::size_t i) { return table.at(n + i); }
auto &lazy_at(std::size_t i) const { return table.at(n + i); }
auto collect() const {
auto ret = std::vector<Value>(sz);
for (auto i = 0; i < sz; i++) {
ret.at(i) = at(i);
}
return ret;
}
auto query(int l, int r) const {
auto const dfs = [&](auto &&f, int l, int r, int k, int L, int R) -> Value {
return l <= L && R <= r ? table.at(k)
: R <= l || r <= L ? id
: op(f(f, l, r, 2 * k, L, (L + R) / 2),
f(f, l, r, 2 * k + 1, (L + R) / 2, R));
};
return dfs(dfs, l, r, 1, 0, n);
}
void build() {
for (auto i = n - 1; i > 0; i--)
merge(i);
}
};
template <class Seg, class SegPtr> class segment_tree_entry {
using value_type = typename Seg::value_type;
SegPtr seg;
std::size_t i;
auto &get() { return seg->table.at(seg->n + i); }
void build_oneline() {
for (int j = (i + seg->n) / 2; j > 0; j /= 2) {
seg->merge(j);
}
}
public:
segment_tree_entry(SegPtr seg, std::size_t i) : seg(seg), i(i) {}
operator value_type() { return seg->table.at(seg->n + i); }
void operator=(value_type x) {
get() = x;
build_oneline();
}
void operator++() {
get()++;
build_oneline();
}
void operator--() {
get()--;
build_oneline();
}
void operator+=(value_type x) {
get() += x;
build_oneline();
}
void operator-=(value_type x) {
get() -= x;
build_oneline();
}
void operator*=(value_type x) {
get() *= x;
build_oneline();
}
void operator/=(value_type x) {
get() /= x;
build_oneline();
}
};
template <class Seg, class SegPtr, class Pointer, class Reference>
class segment_tree_iterator {
using this_type = segment_tree_iterator<Seg, SegPtr, Pointer, Reference>;
using value_type = typename Seg::value_type;
using pointer = Pointer;
using reference = Reference;
template <class Seg_, class SegPtr_, class Pointer_, class Reference_>
friend bool
operator!=(segment_tree_iterator<Seg_, SegPtr_, Pointer_, Reference_>,
segment_tree_iterator<Seg_, SegPtr_, Pointer_, Reference_>);
SegPtr seg;
std::size_t i;
public:
segment_tree_iterator(SegPtr seg, std::size_t i) : seg(seg), i(i) {}
this_type &operator++() {
i++;
return *this;
}
this_type operator++(int) {
auto old = *this;
++i;
return old;
}
pointer operator->() { return &seg->at(i); }
value_type operator*() { return seg->at(i); }
};
template <class Seg, class SegPtr, class Pointer, class Reference>
bool operator!=(segment_tree_iterator<Seg, SegPtr, Pointer, Reference> a,
segment_tree_iterator<Seg, SegPtr, Pointer, Reference> b) {
return a.i != b.i;
}
template <typename Value, typename BinaryOp>
auto make_segment_tree(int sz, BinaryOp const &op, Value id) {
return segment_tree<Value, BinaryOp>(sz, op, id);
}
int main() {
std::cin.tie(0);
std::cin.sync_with_stdio(false);
int n;
std::cin >> n;
int N = 1 << n;
std::vector<int> a(n), b(n);
std::cin >> a >> b;
int max = 0;
for (int x : a)
cmx(max, x);
for (int x : b)
cmx(max, x);
int inf = std::numeric_limits<int>::max();
auto cal = [&](int bs) -> int {
std::vector<int> c(n);
rep(i, 0, n) { c.at(i) = (bat(bs, i) ? b : a).at(i); }
auto d = c;
std::sort(all(d));
debug(bs, c, d);
auto from = make_vector<2, std::vector<int>>(max + 1, 2);
rep(i, 0, n) {
int x = c.at(i);
from.at(x).at((i % 2) ^ bat(bs, i)).emplace_back(i);
}
auto to = make_vector<2, std::vector<int>>(max + 1, 2);
rep(i, 0, n) {
int x = d.at(i);
to.at(x).at(i % 2).emplace_back(i);
}
// #if DEBUG
// rep(i,0,max+1) {
// debug(i,from.at(i),to.at(i));
// }
// #endif
std::vector<int> p(n);
rep(i, 0, max + 1) rep(j, 0, 2) {
auto const &v0 = at(from, i, j);
auto const &v1 = at(to, i, j);
if (v0.size() != v1.size()) {
return inf;
}
// debug(v0,v1);
int sz = v0.size();
rep(k, 0, sz) { p.at(v0.at(k)) = v1.at(k); }
}
debug(p);
int ans = 0;
auto seg = make_segment_tree<int>(n, std::plus<>{}, 0);
for (int x : p) {
ans += seg.query(x, n);
seg.at(x) += 1;
}
return ans;
};
int ans = inf;
rep(bs, 0, N) {
if (__builtin_popcount(bs) % 2 == 1)
continue;
cmn(ans, cal(bs));
}
if (ans == inf)
ans = -1;
std::cout << ans << std::endl;
return 0;
}
/*
裏表を全探索です。ただし裏返っているものは偶数枚です。
どれがどこにいたのかを決めます。
各登場する数字に対し、今いる場所を見ます。そこの偶奇を見ます。
行き先は、裏返っていれば偶奇も替えたもので、そうでなければそのままのものです。
*/
| replace | 322 | 323 | 322 | 327 | TLE | |
p02798 | C++ | Runtime Error | #include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int n;
int a[51], b[51];
int dp[1 << 18][51];
int INF = 1 << 25;
int main(void) {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for (int i = 0; i < n; i++) {
scanf("%d", &b[i]);
}
for (int i = 0; i <= (1 << n); i++) {
for (int j = 0; j <= 50; j++) {
dp[i][j] = INF;
}
}
dp[0][0] = 0;
for (int i = 0; i < (1 << n); i++) {
int pos = __builtin_popcount(i);
for (int j = 0; j <= 50; j++) {
if (dp[i][j] == INF)
continue;
int t_pos = 0;
// printf("%d %d %d %d\n",i,j,dp[i][j],pos);
for (int k = 0; k < n; k++) {
if ((i >> k) & 1)
continue;
int flag = (k - pos + 100) % 2;
// printf("k=%d flag=%d t_pos=%d\n",k,flag,t_pos);
if (flag == 0) {
if (j <= a[k]) {
dp[i + (1 << k)][a[k]] =
min(dp[i + (1 << k)][a[k]], dp[i][j] + t_pos);
}
} else {
if (j <= b[k]) {
dp[i + (1 << k)][b[k]] =
min(dp[i + (1 << k)][b[k]], dp[i][j] + t_pos);
}
}
t_pos++;
}
}
}
int ans = INF;
for (int i = 0; i <= 50; i++) {
ans = min(ans, dp[(1 << n) - 1][i]);
}
printf("%d\n", ans == INF ? -1 : ans);
return 0;
}
| #include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int n;
int a[51], b[51];
int dp[1 << 18][51];
int INF = 1 << 25;
int main(void) {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for (int i = 0; i < n; i++) {
scanf("%d", &b[i]);
}
for (int i = 0; i < (1 << n); i++) {
for (int j = 0; j <= 50; j++) {
dp[i][j] = INF;
}
}
dp[0][0] = 0;
for (int i = 0; i < (1 << n); i++) {
int pos = __builtin_popcount(i);
for (int j = 0; j <= 50; j++) {
if (dp[i][j] == INF)
continue;
int t_pos = 0;
// printf("%d %d %d %d\n",i,j,dp[i][j],pos);
for (int k = 0; k < n; k++) {
if ((i >> k) & 1)
continue;
int flag = (k - pos + 100) % 2;
// printf("k=%d flag=%d t_pos=%d\n",k,flag,t_pos);
if (flag == 0) {
if (j <= a[k]) {
dp[i + (1 << k)][a[k]] =
min(dp[i + (1 << k)][a[k]], dp[i][j] + t_pos);
}
} else {
if (j <= b[k]) {
dp[i + (1 << k)][b[k]] =
min(dp[i + (1 << k)][b[k]], dp[i][j] + t_pos);
}
}
t_pos++;
}
}
}
int ans = INF;
for (int i = 0; i <= 50; i++) {
ans = min(ans, dp[(1 << n) - 1][i]);
}
printf("%d\n", ans == INF ? -1 : ans);
return 0;
}
| replace | 19 | 20 | 19 | 20 | 0 | |
p02798 | C++ | Runtime Error | #define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cmath>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#define endl "\n"
#define age first
#define pos second
#define Z third
#define mp make_pair
#define mt make_triple
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ld PI = 3.141592653589793;
const ld E = 2.71828182845904523;
const ll LLINF = 4000000009000000099;
const ll INF = 1009000099;
const ll MOD7 = 1000000007;
const ll MOD9 = 1000000009;
template <class A, class B, class C> struct triple {
A first;
B second;
C third;
triple(A, B, C);
triple();
};
template <class A, class B, class C> triple<A, B, C>::triple(A a, B b, C c) {
first = a;
second = b;
third = c;
}
template <class A, class B, class C> triple<A, B, C>::triple() {}
template <class A, class B, class C>
triple<A, B, C> make_triple(A a, B b, C c) {
triple<A, B, C> t(a, b, c);
return t;
}
template <class A, class B, class C>
bool operator<(triple<A, B, C> a, triple<A, B, C> b) {
if (a.first != b.first)
return a.first < b.first;
if (a.second != b.second)
return a.second < b.second;
if (a.third != b.third)
return a.third < b.third;
}
template <class T> T inline sqr(T a) { return a * a; }
inline ll bpow(ll a, ll d, ll mod) {
ll res = 1, p = a;
while (d != 0) {
if (d & 1) {
res *= p;
res %= mod;
}
p = (p * p) % mod;
d >>= 1;
}
return res;
}
ll bpow(ll a, ll d) {
if (d == 0)
return 1;
ll t = bpow(a, d / 2);
t = (t * t);
if (d & 1)
t = (t * a);
return t;
}
ll gcd(ll a, ll b) {
while (a && b)
if (a > b)
a %= b;
else
b %= a;
return a + b;
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll C(ll n, ll k) {
ll res = 1;
for (int i = 1; i <= k; ++i)
res = res * (n - k + i) / i;
return res;
}
inline ll C(ll n, ll k, ll mod) {
ll u = 1, d = 1;
for (int i = 2; i <= n; ++i)
u = (u * i) % mod;
int x = max(k, n - k), z = min(k, n - k);
for (int i = 2; i <= x; ++i) {
d = (d * i) % mod;
if (i <= z)
d = (d * i) % mod;
}
d = bpow(d, mod - 2, mod);
ll res = (u * d) % mod;
return res;
}
using namespace std;
void solve1() {
int n;
cin >> n;
vector<pair<int, int>> v(n);
for (int i = 0; i < n; ++i) {
cin >> v[i].first;
v[i].second = i;
}
sort(v.begin(), v.end());
vector<set<int>> cycles;
set<int> used;
for (int i = 0; i < n; ++i) {
set<int> s;
int idx = i;
while (used.find(v[idx].first) == used.end()) {
used.insert(v[idx].first);
s.insert(v[idx].first);
idx = v[idx].second;
}
if (s.size() > 1)
cycles.push_back(s);
}
int res = 0, glMin = v.front().first;
for (auto cycle : cycles) {
int sum = 0, locMin = *cycle.begin();
for (auto it = cycle.begin(); it != cycle.end(); ++it)
sum += *it;
res += min(sum + locMin * (cycle.size() - 2),
sum + locMin + glMin * (cycle.size() + 1));
}
cout << res << endl;
}
bool cmp(pair<ll, ll> a, pair<ll, ll> b) {
if (a.second != b.second)
return a.second < b.second;
return a.first > b.first;
}
bool doesIntersect(pair<ll, ll> a, pair<ll, ll> b) {
ll r = min(a.second, b.second), l = max(a.first, b.first);
return r > l;
}
ll value(const vector<vector<ll>> &v, int start, int finish) {
int shift = abs(start - finish);
return v[start][shift & 1];
}
int popcnt(int x) {
int cnt = 0;
while (x) {
cnt++;
x &= x - 1;
}
return cnt;
}
void solve() {
ll n;
cin >> n;
const ll MAX_N = 8;
vector<vector<ll>> v(n);
int sz = 2;
for (int j = 0; j < sz; ++j)
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
v[i].push_back(x);
}
int dp[1 << MAX_N][MAX_N];
for (int i = 0; i < (1 << MAX_N); ++i)
for (int j = 0; j < MAX_N; ++j)
dp[i][j] = -1;
for (int i = 0; i < n; ++i)
dp[(1 << i)][i] = i;
for (int i = 1; i < (1 << n); ++i) {
int cnt = popcnt(i);
int side_effect = cnt - 1;
for (int j = 0; j < n; ++j)
if ((i & (1 << j)) && dp[i][j] == -1) {
dp[i][j] = INF;
int umask = i ^ (1 << j);
int shift = abs(cnt - 1 - (j + side_effect));
for (int k = 0; k < n; ++k)
if ((umask & (1 << k)) &&
value(v, j, cnt - 1) >= value(v, k, cnt - 2))
dp[i][j] = min(dp[umask][k] + shift, dp[i][j]);
--side_effect;
}
}
int ans = INF;
for (int i = 0; i < n; ++i)
ans = min(ans, dp[(1 << n) - 1][i]);
if (ans < INF)
cout << ans;
else
cout << -1;
}
int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
#ifdef _DEBUG
freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif
solve();
return 0;
}
| #define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cmath>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#define endl "\n"
#define age first
#define pos second
#define Z third
#define mp make_pair
#define mt make_triple
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ld PI = 3.141592653589793;
const ld E = 2.71828182845904523;
const ll LLINF = 4000000009000000099;
const ll INF = 1009000099;
const ll MOD7 = 1000000007;
const ll MOD9 = 1000000009;
template <class A, class B, class C> struct triple {
A first;
B second;
C third;
triple(A, B, C);
triple();
};
template <class A, class B, class C> triple<A, B, C>::triple(A a, B b, C c) {
first = a;
second = b;
third = c;
}
template <class A, class B, class C> triple<A, B, C>::triple() {}
template <class A, class B, class C>
triple<A, B, C> make_triple(A a, B b, C c) {
triple<A, B, C> t(a, b, c);
return t;
}
template <class A, class B, class C>
bool operator<(triple<A, B, C> a, triple<A, B, C> b) {
if (a.first != b.first)
return a.first < b.first;
if (a.second != b.second)
return a.second < b.second;
if (a.third != b.third)
return a.third < b.third;
}
template <class T> T inline sqr(T a) { return a * a; }
inline ll bpow(ll a, ll d, ll mod) {
ll res = 1, p = a;
while (d != 0) {
if (d & 1) {
res *= p;
res %= mod;
}
p = (p * p) % mod;
d >>= 1;
}
return res;
}
ll bpow(ll a, ll d) {
if (d == 0)
return 1;
ll t = bpow(a, d / 2);
t = (t * t);
if (d & 1)
t = (t * a);
return t;
}
ll gcd(ll a, ll b) {
while (a && b)
if (a > b)
a %= b;
else
b %= a;
return a + b;
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll C(ll n, ll k) {
ll res = 1;
for (int i = 1; i <= k; ++i)
res = res * (n - k + i) / i;
return res;
}
inline ll C(ll n, ll k, ll mod) {
ll u = 1, d = 1;
for (int i = 2; i <= n; ++i)
u = (u * i) % mod;
int x = max(k, n - k), z = min(k, n - k);
for (int i = 2; i <= x; ++i) {
d = (d * i) % mod;
if (i <= z)
d = (d * i) % mod;
}
d = bpow(d, mod - 2, mod);
ll res = (u * d) % mod;
return res;
}
using namespace std;
void solve1() {
int n;
cin >> n;
vector<pair<int, int>> v(n);
for (int i = 0; i < n; ++i) {
cin >> v[i].first;
v[i].second = i;
}
sort(v.begin(), v.end());
vector<set<int>> cycles;
set<int> used;
for (int i = 0; i < n; ++i) {
set<int> s;
int idx = i;
while (used.find(v[idx].first) == used.end()) {
used.insert(v[idx].first);
s.insert(v[idx].first);
idx = v[idx].second;
}
if (s.size() > 1)
cycles.push_back(s);
}
int res = 0, glMin = v.front().first;
for (auto cycle : cycles) {
int sum = 0, locMin = *cycle.begin();
for (auto it = cycle.begin(); it != cycle.end(); ++it)
sum += *it;
res += min(sum + locMin * (cycle.size() - 2),
sum + locMin + glMin * (cycle.size() + 1));
}
cout << res << endl;
}
bool cmp(pair<ll, ll> a, pair<ll, ll> b) {
if (a.second != b.second)
return a.second < b.second;
return a.first > b.first;
}
bool doesIntersect(pair<ll, ll> a, pair<ll, ll> b) {
ll r = min(a.second, b.second), l = max(a.first, b.first);
return r > l;
}
ll value(const vector<vector<ll>> &v, int start, int finish) {
int shift = abs(start - finish);
return v[start][shift & 1];
}
int popcnt(int x) {
int cnt = 0;
while (x) {
cnt++;
x &= x - 1;
}
return cnt;
}
void solve() {
ll n;
cin >> n;
const ll MAX_N = 18;
vector<vector<ll>> v(n);
int sz = 2;
for (int j = 0; j < sz; ++j)
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
v[i].push_back(x);
}
int dp[1 << MAX_N][MAX_N];
for (int i = 0; i < (1 << MAX_N); ++i)
for (int j = 0; j < MAX_N; ++j)
dp[i][j] = -1;
for (int i = 0; i < n; ++i)
dp[(1 << i)][i] = i;
for (int i = 1; i < (1 << n); ++i) {
int cnt = popcnt(i);
int side_effect = cnt - 1;
for (int j = 0; j < n; ++j)
if ((i & (1 << j)) && dp[i][j] == -1) {
dp[i][j] = INF;
int umask = i ^ (1 << j);
int shift = abs(cnt - 1 - (j + side_effect));
for (int k = 0; k < n; ++k)
if ((umask & (1 << k)) &&
value(v, j, cnt - 1) >= value(v, k, cnt - 2))
dp[i][j] = min(dp[umask][k] + shift, dp[i][j]);
--side_effect;
}
}
int ans = INF;
for (int i = 0; i < n; ++i)
ans = min(ans, dp[(1 << n) - 1][i]);
if (ans < INF)
cout << ans;
else
cout << -1;
}
int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
#ifdef _DEBUG
freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif
solve();
return 0;
}
| replace | 187 | 188 | 187 | 188 | 0 | |
p02799 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define REV(i, n) for (ll(i) = (n)-1; (i) >= 0; --i)
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n) \
{ \
REP(WW, n) cerr << v[WW] << ' '; \
cerr << endl << endl; \
}
#define SHOW2d(v, WW, HH) \
{ \
REP(W_, WW) { \
REP(H_, HH) cerr << v[W_][H_] << ' '; \
cerr << endl; \
} \
cerr << endl; \
}
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 998244353
typedef long long ll;
typedef pair<ll, ll> P;
vector<vector<P>> v(111111);
ll d[111111];
ll c[222222];
ll color[111111];
ll used[111111];
int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
ll n, m;
cin >> n >> m;
REP(i, n) cin >> d[i];
REP(i, m) {
ll a, b;
cin >> a >> b;
a--;
b--;
v[a].EB(b, i);
v[b].EB(a, i);
}
queue<ll> q;
REP(i, n) {
REP(j, v[i].size()) {
int aite = v[i][j].FI;
int edge_id = v[i][j].SE;
if (d[i] == d[aite]) {
if (color[i] != 0 && color[aite] != 0)
continue;
if (color[i] == 0) {
if (color[aite] != 0)
color[i] = -color[aite];
else
color[i] = 1;
}
if (color[aite] == 0)
color[aite] = -color[i];
q.push(i);
q.push(aite);
c[edge_id] = d[i];
}
}
}
while (!q.empty()) {
int id = q.front();
q.pop();
// cout << "id " << id << endl;
if (used[id])
continue;
used[id] = true;
REP(i, v[id].size()) {
int aite = v[id][id].FI;
int edge_id = v[id][id].SE;
// cout << " " << aite << endl;
if (used[aite])
continue;
if (d[aite] <= d[id])
continue;
if (color[aite] != 0)
continue;
color[aite] = color[id];
c[edge_id] = d[aite] - d[id];
q.push(aite);
}
}
REP(i, n) {
if (!used[i]) {
cout << -1 << endl;
return 0;
}
}
REP(i, n) cout << (color[i] == 1 ? 'B' : 'W');
cout << endl;
REP(i, m) cout << (c[i] == 0 ? INF : c[i]) << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define REV(i, n) for (ll(i) = (n)-1; (i) >= 0; --i)
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n) \
{ \
REP(WW, n) cerr << v[WW] << ' '; \
cerr << endl << endl; \
}
#define SHOW2d(v, WW, HH) \
{ \
REP(W_, WW) { \
REP(H_, HH) cerr << v[W_][H_] << ' '; \
cerr << endl; \
} \
cerr << endl; \
}
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 998244353
typedef long long ll;
typedef pair<ll, ll> P;
vector<vector<P>> v(111111);
ll d[111111];
ll c[222222];
ll color[111111];
ll used[111111];
int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
ll n, m;
cin >> n >> m;
REP(i, n) cin >> d[i];
REP(i, m) {
ll a, b;
cin >> a >> b;
a--;
b--;
v[a].EB(b, i);
v[b].EB(a, i);
}
queue<ll> q;
REP(i, n) {
REP(j, v[i].size()) {
int aite = v[i][j].FI;
int edge_id = v[i][j].SE;
if (d[i] == d[aite]) {
if (color[i] != 0 && color[aite] != 0)
continue;
if (color[i] == 0) {
if (color[aite] != 0)
color[i] = -color[aite];
else
color[i] = 1;
}
if (color[aite] == 0)
color[aite] = -color[i];
q.push(i);
q.push(aite);
c[edge_id] = d[i];
}
}
}
while (!q.empty()) {
int id = q.front();
q.pop();
// cout << "id " << id << endl;
if (used[id])
continue;
used[id] = true;
REP(i, v[id].size()) {
int aite = v[id][i].FI;
int edge_id = v[id][i].SE;
// cout << " " << aite << endl;
if (used[aite])
continue;
if (d[aite] <= d[id])
continue;
if (color[aite] != 0)
continue;
color[aite] = color[id];
c[edge_id] = d[aite] - d[id];
q.push(aite);
}
}
REP(i, n) {
if (!used[i]) {
cout << -1 << endl;
return 0;
}
}
REP(i, n) cout << (color[i] == 1 ? 'B' : 'W');
cout << endl;
REP(i, m) cout << (c[i] == 0 ? INF : c[i]) << endl;
return 0;
}
| replace | 87 | 89 | 87 | 89 | 0 | |
p02799 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define sz(a) int(a.size())
const int N = 1e5 + 10;
int gi() {
int x = 0, o = 1;
char ch = getchar();
while ((ch < '0' || ch > '9') && ch != '-')
ch = getchar();
if (ch == '-')
o = -1, ch = getchar();
while (ch >= '0' && ch <= '9')
x = x * 10 + ch - '0', ch = getchar();
return x * o;
}
int n, to[N << 1], nxt[N << 1], d[N], ww[N << 1], tt = 1, col[N], h[N], m;
bool vis[N];
void adde(int u, int v) {
to[++tt] = v;
nxt[tt] = h[u];
h[u] = tt;
to[++tt] = u;
nxt[tt] = h[v];
h[v] = tt;
}
void dfs(int u, int c = 0) {
col[u] = c;
vis[u] = 1;
for (int i = h[u], v; v = to[i], i; i = nxt[i])
if (~ww[i] && !vis[v])
dfs(v, c ^ 1);
}
int main() {
memset(ww, -1, sizeof(ww));
cin >> n >> m;
for (int i = 1; i <= n; i++)
d[i] = gi();
for (int i = 1, u, v; i <= m; i++)
u = gi(), v = gi(), adde(u, v);
for (int u = 1; u <= n; u++) {
pii mn = mp(2e9, 0);
int e = 0;
for (int i = h[u], v; v = to[i], i; i = nxt[i])
if (mp(d[v], v) < mn)
mn = mp(d[v], v), e = i >> 1;
if (mn.fi > d[u])
return cout << -1, 0;
if (e)
ww[e << 1] = ww[e << 1 | 1] = d[u];
}
for (int i = 1; i <= n; i++)
if (!vis[i])
dfs(i);
for (int i = 1; i <= n; i++)
putchar(col[i] ? 'W' : 'B');
cout << '\n';
for (int i = 1; i <= m; i++) {
if (!~ww[i << 1])
ww[i << 1] = 1e9;
cout << ww[i << 1] << '\n';
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define sz(a) int(a.size())
const int N = 2e5 + 10;
int gi() {
int x = 0, o = 1;
char ch = getchar();
while ((ch < '0' || ch > '9') && ch != '-')
ch = getchar();
if (ch == '-')
o = -1, ch = getchar();
while (ch >= '0' && ch <= '9')
x = x * 10 + ch - '0', ch = getchar();
return x * o;
}
int n, to[N << 1], nxt[N << 1], d[N], ww[N << 1], tt = 1, col[N], h[N], m;
bool vis[N];
void adde(int u, int v) {
to[++tt] = v;
nxt[tt] = h[u];
h[u] = tt;
to[++tt] = u;
nxt[tt] = h[v];
h[v] = tt;
}
void dfs(int u, int c = 0) {
col[u] = c;
vis[u] = 1;
for (int i = h[u], v; v = to[i], i; i = nxt[i])
if (~ww[i] && !vis[v])
dfs(v, c ^ 1);
}
int main() {
memset(ww, -1, sizeof(ww));
cin >> n >> m;
for (int i = 1; i <= n; i++)
d[i] = gi();
for (int i = 1, u, v; i <= m; i++)
u = gi(), v = gi(), adde(u, v);
for (int u = 1; u <= n; u++) {
pii mn = mp(2e9, 0);
int e = 0;
for (int i = h[u], v; v = to[i], i; i = nxt[i])
if (mp(d[v], v) < mn)
mn = mp(d[v], v), e = i >> 1;
if (mn.fi > d[u])
return cout << -1, 0;
if (e)
ww[e << 1] = ww[e << 1 | 1] = d[u];
}
for (int i = 1; i <= n; i++)
if (!vis[i])
dfs(i);
for (int i = 1; i <= n; i++)
putchar(col[i] ? 'W' : 'B');
cout << '\n';
for (int i = 1; i <= m; i++) {
if (!~ww[i << 1])
ww[i << 1] = 1e9;
cout << ww[i << 1] << '\n';
}
return 0;
}
| replace | 10 | 11 | 10 | 11 | 0 | |
p02799 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcountll
#define INF 1e16
#define mod 1000000007
struct edge {
ll v, idx, w;
};
ll N, M;
vector<ll> D;
vector<edge> es[100010];
ll res[100010], resC[200010];
bool solved[100010];
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> N >> M;
D.resize(N);
rep(i, N) cin >> D[i];
rep(i, M) {
ll u, v;
cin >> u >> v;
u--;
v--;
if (D[u] < D[v] || (D[u] == D[v] && u < v))
swap(u, v);
es[u].push_back((edge){v, i, D[u]});
res[i] = D[u];
}
priority_queue<P, vector<P>, greater<P>> que;
rep(i, N) { que.push(P(D[i], i)); }
while (que.size()) {
P p = que.top();
que.pop();
ll v = p.se;
ll d = p.fi;
bool ok = true;
for (edge e : es[v]) {
if (!solved[e.v] && D[e.v] != d) {
ok = false;
}
}
if (!ok) {
cout << -1 << endl;
return 0;
}
if (es[v].size() == 0) {
continue;
}
bool col = false;
for (edge e : es[v]) {
solved[e.v] = true;
if (resC[e.v] == 0)
col = true;
}
resC[v] = col;
solved[v] = true;
}
rep(i, N) {
if (!solved[i]) {
cout << -1 << endl;
return 0;
}
}
rep(i, N) {
if (resC[i] == 0)
cout << "W";
else
cout << "B";
}
cout << "\n";
rep(i, M) { cout << res[i] << "\n"; }
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcountll
#define INF 1e16
#define mod 1000000007
struct edge {
ll v, idx, w;
};
ll N, M;
vector<ll> D;
vector<edge> es[200010];
ll res[200010], resC[200010];
bool solved[200010];
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> N >> M;
D.resize(N);
rep(i, N) cin >> D[i];
rep(i, M) {
ll u, v;
cin >> u >> v;
u--;
v--;
if (D[u] < D[v] || (D[u] == D[v] && u < v))
swap(u, v);
es[u].push_back((edge){v, i, D[u]});
res[i] = D[u];
}
priority_queue<P, vector<P>, greater<P>> que;
rep(i, N) { que.push(P(D[i], i)); }
while (que.size()) {
P p = que.top();
que.pop();
ll v = p.se;
ll d = p.fi;
bool ok = true;
for (edge e : es[v]) {
if (!solved[e.v] && D[e.v] != d) {
ok = false;
}
}
if (!ok) {
cout << -1 << endl;
return 0;
}
if (es[v].size() == 0) {
continue;
}
bool col = false;
for (edge e : es[v]) {
solved[e.v] = true;
if (resC[e.v] == 0)
col = true;
}
resC[v] = col;
solved[v] = true;
}
rep(i, N) {
if (!solved[i]) {
cout << -1 << endl;
return 0;
}
}
rep(i, N) {
if (resC[i] == 0)
cout << "W";
else
cout << "B";
}
cout << "\n";
rep(i, M) { cout << res[i] << "\n"; }
return 0;
}
| replace | 30 | 33 | 30 | 33 | 0 | |
p02799 | C++ | Runtime Error | // #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long int;
using int64 = long long int;
template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;
// Union-Find 木 (Verified: AtCoder Typical Contest 001 B)
struct UnionFind {
private:
const int n;
int size_;
vector<int> uf;
public:
// 初期化 UnionFind uni(n) のように宣言すれば良い
UnionFind(int _n) : n(_n), size_(_n), uf(_n, -1) {}
// find (木の根を求める)
int find(int x) { return (uf[x] < 0) ? x : uf[x] = find(uf[x]); }
// x と y が同じ集合に属するかどうか
bool same(int x, int y) { return find(x) == find(y); }
// x が属する集合の要素数
int size(int x) { return -uf[find(x)]; }
// 集合はいくつあるか
int size() { return size_; }
// x と y の属する集合を併合
bool unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return false;
size_--;
if (-uf[x] < -uf[y])
swap(x, y);
uf[x] += uf[y];
uf[y] = x;
return true;
}
};
int main() {
int N, M;
scanf("%d%d", &N, &M);
vector<ll> D(N);
for (int i = 0; i < N; i++)
scanf("%lld", &D[i]);
vector<vector<int>> G(N);
map<pair<int, int>, ll> edge_map;
for (int i = 0; i < M; i++) {
int u, v;
scanf("%d%d", &u, &v);
u--;
v--;
edge_map[make_pair(u, v)] = i;
edge_map[make_pair(v, u)] = i;
G[u].emplace_back(v);
G[v].emplace_back(u);
}
ll mi = *min_element(D.begin(), D.end());
if (count(D.begin(), D.end(), mi) < 2) {
cout << -1 << endl;
return 0;
}
vector<bool> used(N);
vector<ll> color(N, -1), ans(M, -1);
priority_queue<int, vector<int>, function<bool(int, int)>> que(
[&](int u, int v) { return D[u] > D[v]; });
UnionFind uf(N);
bool cond = true;
queue<int> q;
for (int i = 0; i < N; i++) {
if (D[i] != mi)
continue;
que.emplace(i);
int cnt = 0;
for (auto to : G[i]) {
cnt += (D[to] == mi);
if (D[to] == mi)
uf.unite(i, to);
}
cond &= (cnt > 0);
}
if (!cond) {
cout << -1 << endl;
return 0;
}
vector<int> rec(N);
for (int i = 0; i < N; i++) {
if (D[i] != mi)
continue;
if (rec[uf.find(i)])
continue;
rec[uf.find(i)] = true;
q.emplace(i);
}
while (q.size()) {
int cur = q.front();
q.pop();
if (color[cur] < 0)
color[cur] = 0;
for (auto to : G[cur]) {
if (color[to] >= 0)
continue;
color[to] = color[cur] ^ 1;
q.emplace(to);
}
}
while (que.size()) {
int cur = que.top();
que.pop();
for (auto to : G[cur]) {
if (color[to] >= 0) {
ans[edge_map[make_pair(cur, to)]] = max(D[cur], D[to]);
} else {
ans[edge_map[make_pair(cur, to)]] = max(D[cur], D[to]);
color[to] = color[cur] ^ 1;
if (!used[to]) {
used[to] = true;
que.emplace(to);
}
}
}
}
string color_str(N, '?');
for (int i = 0; i < N; i++) {
if (color[i] == 0)
color_str[i] = 'B';
else if (color[i] == 1)
color_str[i] = 'W';
else
assert(false);
// cerr << color_str[i];
}
// cerr << endl;
for (int i = 0; i < M; i++) {
assert(ans[i] > 0);
// cerr << ans[i] << endl;
}
bool ok = true;
for (int i = 0; i < N; i++) {
ll min_val = LONGINF, cur = i;
for (auto to : G[cur]) {
if (color[cur] == color[to])
continue;
chmin<ll>(min_val, ans[edge_map[make_pair(cur, to)]]);
}
ok &= (D[i] == min_val);
}
if (!ok) {
cout << -1 << endl;
return 0;
}
cout << color_str << endl;
for (int i = 0; i < M; i++) {
cout << ans[i] << endl;
}
return 0;
}
| // #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long int;
using int64 = long long int;
template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;
// Union-Find 木 (Verified: AtCoder Typical Contest 001 B)
struct UnionFind {
private:
const int n;
int size_;
vector<int> uf;
public:
// 初期化 UnionFind uni(n) のように宣言すれば良い
UnionFind(int _n) : n(_n), size_(_n), uf(_n, -1) {}
// find (木の根を求める)
int find(int x) { return (uf[x] < 0) ? x : uf[x] = find(uf[x]); }
// x と y が同じ集合に属するかどうか
bool same(int x, int y) { return find(x) == find(y); }
// x が属する集合の要素数
int size(int x) { return -uf[find(x)]; }
// 集合はいくつあるか
int size() { return size_; }
// x と y の属する集合を併合
bool unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y)
return false;
size_--;
if (-uf[x] < -uf[y])
swap(x, y);
uf[x] += uf[y];
uf[y] = x;
return true;
}
};
int main() {
int N, M;
scanf("%d%d", &N, &M);
vector<ll> D(N);
for (int i = 0; i < N; i++)
scanf("%lld", &D[i]);
vector<vector<int>> G(N);
map<pair<int, int>, ll> edge_map;
for (int i = 0; i < M; i++) {
int u, v;
scanf("%d%d", &u, &v);
u--;
v--;
edge_map[make_pair(u, v)] = i;
edge_map[make_pair(v, u)] = i;
G[u].emplace_back(v);
G[v].emplace_back(u);
}
ll mi = *min_element(D.begin(), D.end());
if (count(D.begin(), D.end(), mi) < 2) {
cout << -1 << endl;
return 0;
}
vector<bool> used(N);
vector<ll> color(N, -1), ans(M, -1);
priority_queue<int, vector<int>, function<bool(int, int)>> que(
[&](int u, int v) { return D[u] > D[v]; });
UnionFind uf(N);
bool cond = true;
queue<int> q;
for (int i = 0; i < N; i++) {
if (D[i] != mi)
continue;
que.emplace(i);
int cnt = 0;
for (auto to : G[i]) {
cnt += (D[to] == mi);
if (D[to] == mi)
uf.unite(i, to);
}
cond &= (cnt > 0);
}
if (!cond) {
cout << -1 << endl;
return 0;
}
vector<int> rec(N);
for (int i = 0; i < N; i++) {
if (D[i] != mi)
continue;
if (rec[uf.find(i)])
continue;
rec[uf.find(i)] = true;
q.emplace(i);
}
while (q.size()) {
int cur = q.front();
q.pop();
if (color[cur] < 0)
color[cur] = 0;
for (auto to : G[cur]) {
if (color[to] >= 0 or D[to] != mi)
continue;
color[to] = color[cur] ^ 1;
q.emplace(to);
}
}
while (que.size()) {
int cur = que.top();
que.pop();
for (auto to : G[cur]) {
if (color[to] >= 0) {
ans[edge_map[make_pair(cur, to)]] = max(D[cur], D[to]);
} else {
ans[edge_map[make_pair(cur, to)]] = max(D[cur], D[to]);
color[to] = color[cur] ^ 1;
if (!used[to]) {
used[to] = true;
que.emplace(to);
}
}
}
}
string color_str(N, '?');
for (int i = 0; i < N; i++) {
if (color[i] == 0)
color_str[i] = 'B';
else if (color[i] == 1)
color_str[i] = 'W';
else
assert(false);
// cerr << color_str[i];
}
// cerr << endl;
for (int i = 0; i < M; i++) {
assert(ans[i] > 0);
// cerr << ans[i] << endl;
}
bool ok = true;
for (int i = 0; i < N; i++) {
ll min_val = LONGINF, cur = i;
for (auto to : G[cur]) {
if (color[cur] == color[to])
continue;
chmin<ll>(min_val, ans[edge_map[make_pair(cur, to)]]);
}
ok &= (D[i] == min_val);
}
if (!ok) {
cout << -1 << endl;
return 0;
}
cout << color_str << endl;
for (int i = 0; i < M; i++) {
cout << ans[i] << endl;
}
return 0;
}
| replace | 141 | 142 | 141 | 142 | -6 | a361971e-bfaf-4f3f-81cf-c43749f0a1cf.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02799/C++/s352175599.cpp:161: int main(): Assertion `ans[i] > 0' failed.
|
p02799 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 100010;
int n, m;
int a[MAXN];
vector<int> g[MAXN];
pair<int, int> edges[MAXN];
string ansS;
map<pair<int, int>, int> ansC;
int visited[MAXN];
int dfs(int v, int cost, int color) {
ansS[v] = (color ? 'B' : 'W');
visited[v] = 1;
int found = 0;
for (int s : g[v])
if (!visited[s] && a[s] == a[v]) {
ansC[make_pair(s, v)] = ansC[make_pair(v, s)] = cost;
dfs(s, cost, 1 - color);
found = 1;
}
return found;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
vector<int> p;
cin >> n >> m;
for (int i = 0; i < n; ++i) {
cin >> a[i];
p.push_back(i);
}
ansS = string(n, '*');
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
--u;
--v;
g[u].push_back(v);
g[v].push_back(u);
edges[i] = {u, v};
}
sort(p.begin(), p.end(), [&](int x, int y) { return a[x] < a[y]; });
for (int i = 0; i < n; ++i) {
int v = p[i];
if (visited[v])
continue;
if (!dfs(v, a[v], 0)) {
ansS[v] = '*';
for (int s : g[v]) {
if (ansS[s] != '*') {
if (ansS[v] == '*') {
ansS[v] = (ansS[s] == 'B' ? 'W' : 'B');
ansC[make_pair(s, v)] = ansC[make_pair(v, s)] = a[v];
} else {
if (ansS[v] != ansS[s]) {
ansC[make_pair(s, v)] = ansC[make_pair(v, s)] = a[v];
} else {
ansC[make_pair(s, v)] = ansC[make_pair(v, s)] = a[v] - a[s];
}
}
}
}
if (ansS[v] == '*')
return cout << -1, 0;
}
}
cout << ansS << "\n";
for (int i = 0; i < m; ++i) {
// cout << edges[i] << endl;
if (!ansC.count(edges[i]))
cout << (int)1e9 << "\n";
else
cout << ansC[edges[i]] << "\n";
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
const int MAXN = 200010;
int n, m;
int a[MAXN];
vector<int> g[MAXN];
pair<int, int> edges[MAXN];
string ansS;
map<pair<int, int>, int> ansC;
int visited[MAXN];
int dfs(int v, int cost, int color) {
ansS[v] = (color ? 'B' : 'W');
visited[v] = 1;
int found = 0;
for (int s : g[v])
if (!visited[s] && a[s] == a[v]) {
ansC[make_pair(s, v)] = ansC[make_pair(v, s)] = cost;
dfs(s, cost, 1 - color);
found = 1;
}
return found;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
vector<int> p;
cin >> n >> m;
for (int i = 0; i < n; ++i) {
cin >> a[i];
p.push_back(i);
}
ansS = string(n, '*');
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
--u;
--v;
g[u].push_back(v);
g[v].push_back(u);
edges[i] = {u, v};
}
sort(p.begin(), p.end(), [&](int x, int y) { return a[x] < a[y]; });
for (int i = 0; i < n; ++i) {
int v = p[i];
if (visited[v])
continue;
if (!dfs(v, a[v], 0)) {
ansS[v] = '*';
for (int s : g[v]) {
if (ansS[s] != '*') {
if (ansS[v] == '*') {
ansS[v] = (ansS[s] == 'B' ? 'W' : 'B');
ansC[make_pair(s, v)] = ansC[make_pair(v, s)] = a[v];
} else {
if (ansS[v] != ansS[s]) {
ansC[make_pair(s, v)] = ansC[make_pair(v, s)] = a[v];
} else {
ansC[make_pair(s, v)] = ansC[make_pair(v, s)] = a[v] - a[s];
}
}
}
}
if (ansS[v] == '*')
return cout << -1, 0;
}
}
cout << ansS << "\n";
for (int i = 0; i < m; ++i) {
// cout << edges[i] << endl;
if (!ansC.count(edges[i]))
cout << (int)1e9 << "\n";
else
cout << ansC[edges[i]] << "\n";
}
return 0;
}
| replace | 4 | 5 | 4 | 5 | 0 | |
p02799 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
struct edge {
ll to;
ll cost, num;
edge() {}
edge(ll to, ll cost, ll num) : to(to), cost(cost), num(num){};
};
int main() {
ll n, m;
cin >> n >> m;
vector<vector<edge>> g(n);
vector<vector<edge>> gg(n);
vector<ll> v(n);
rep(i, n) { cin >> v[i]; }
rep(i, m) {
ll a, b;
cin >> a >> b;
g[a - 1].emplace_back(b - 1, -1, i);
g[b - 1].emplace_back(a - 1, -1, i);
}
rep(i, n) {
bool ok = 0;
for (auto r : g[i])
if (v[i] >= v[r.to])
ok = 1;
if (!ok) {
cout << -1 << endl;
return 0;
}
}
vector<bool> s(n, 0);
vector<bool> see(n, 0);
vector<ll> ans(n, 1000000000);
rep(i, n) {
if (see[i])
continue;
ll mi = 1LL << 40;
for (auto r : g[i])
mi = min(mi, v[r.to]);
for (auto r : g[i])
if (mi == v[r.to]) {
ans[r.num] = v[i];
gg[r.to].emplace_back(i, -1, i);
gg[i].emplace_back(r.to, -1, i);
break;
}
}
rep(i, n) {
if (see[i])
continue;
queue<ll> q;
q.push(i);
while (!q.empty()) {
ll p = q.front();
see[p] = 1;
q.pop();
for (auto r : gg[p]) {
if (!see[r.to]) {
s[r.to] = !s[p];
q.push(r.to);
}
}
}
}
string ss = "";
rep(i, n) {
if (s[i])
ss += 'B';
else
ss += 'W';
}
cout << ss << endl;
for (auto r : ans)
cout << r << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
struct edge {
ll to;
ll cost, num;
edge() {}
edge(ll to, ll cost, ll num) : to(to), cost(cost), num(num){};
};
int main() {
ll n, m;
cin >> n >> m;
vector<vector<edge>> g(n);
vector<vector<edge>> gg(n);
vector<ll> v(n);
rep(i, n) { cin >> v[i]; }
rep(i, m) {
ll a, b;
cin >> a >> b;
g[a - 1].emplace_back(b - 1, -1, i);
g[b - 1].emplace_back(a - 1, -1, i);
}
rep(i, n) {
bool ok = 0;
for (auto r : g[i])
if (v[i] >= v[r.to])
ok = 1;
if (!ok) {
cout << -1 << endl;
return 0;
}
}
vector<bool> s(n, 0);
vector<bool> see(n, 0);
vector<ll> ans(m, 1000000000);
rep(i, n) {
if (see[i])
continue;
ll mi = 1LL << 40;
for (auto r : g[i])
mi = min(mi, v[r.to]);
for (auto r : g[i])
if (mi == v[r.to]) {
ans[r.num] = v[i];
gg[r.to].emplace_back(i, -1, i);
gg[i].emplace_back(r.to, -1, i);
break;
}
}
rep(i, n) {
if (see[i])
continue;
queue<ll> q;
q.push(i);
while (!q.empty()) {
ll p = q.front();
see[p] = 1;
q.pop();
for (auto r : gg[p]) {
if (!see[r.to]) {
s[r.to] = !s[p];
q.push(r.to);
}
}
}
}
string ss = "";
rep(i, n) {
if (s[i])
ss += 'B';
else
ss += 'W';
}
cout << ss << endl;
for (auto r : ans)
cout << r << endl;
return 0;
} | replace | 35 | 36 | 35 | 36 | 0 | |
p02799 | C++ | Runtime Error | #include "bits/stdc++.h"
using namespace std;
#define FAST \
ios_base::sync_with_stdio(false); \
cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof((x)))
#define siz(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) (lower_bound(all(x), y) - x.begin())
#define ubd(x, y) (upper_bound(all(x), y) - x.begin())
mt19937
rng(chrono::steady_clock::now()
.time_since_epoch()
.count()); // can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) {
return rng() % (y + 1 - x) + x;
} // inclusivesss
string inline to_string(char c) {
string s(1, c);
return s;
}
template <typename T> inline T gcd(T a, T b) {
return a == 0 ? llabs(b) : gcd(b % a, a);
}
using ll = long long;
using ld = long double;
#define FOR(i, s, e) for (ll i = s; i <= ll(e); ++i)
#define DEC(i, s, e) for (ll i = s; i >= ll(e); --i)
using pi = pair<ll, ll>;
using spi = pair<ll, pi>;
using dpi = pair<pi, pi>;
#define LLINF ((long long)1e18)
#define INF int(1e9 + 1e6)
#define MAXN (100006)
ll n, m, A[MAXN], colour[MAXN], cost[MAXN];
bitset<MAXN> vis;
vector<pi> v[MAXN];
int main() {
FAST cin >> n >> m;
FOR(i, 1, n) cin >> A[i];
FOR(i, 1, m) {
ll a, b;
cin >> a >> b;
v[a].eb(b, i), v[b].eb(a, i);
}
FOR(i, 1, n) {
ll mi = LLINF;
for (auto j : v[i])
mi = min(mi, A[j.f]);
if (mi > A[i]) {
cout << "-1\n";
return 0;
}
}
vector<pi> D;
FOR(i, 1, n) D.eb(A[i], i);
sort(all(D), greater<>());
int co = 0;
while (D.size()) {
ll deal = D.back().f;
vector<int> cur;
cur.eb(D.back().s), D.pop_back();
while (D.size() && D.back().f == deal)
cur.eb(D.back().s), D.pop_back();
function<void(ll)> dfs = [&](ll x) {
if (vis[x])
return;
vis[x] = 1;
for (auto i : v[x])
if (cost[i.s] == 0) {
if (colour[i.f]) {
assert(vis[i.f]);
if (colour[i.f] == colour[x])
cost[i.s] = max(1ll, A[x] - A[i.f]);
else
cost[i.s] = A[x];
} else if (A[i.f] == deal) {
assert(!vis[i.f]); // since colour==0
colour[i.f] = 3 - colour[x];
cost[i.s] = A[x];
dfs(i.f);
}
}
};
for (auto &i : cur)
if (!vis[i])
colour[i] = (co & 1) + 1, dfs(i);
++co;
}
FOR(i, 1, n) if (colour[i] == 1) cout << 'W';
else cout << 'B';
cout << '\n';
FOR(i, 1, m) cout << cost[i] << '\n';
}
| #include "bits/stdc++.h"
using namespace std;
#define FAST \
ios_base::sync_with_stdio(false); \
cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof((x)))
#define siz(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) (lower_bound(all(x), y) - x.begin())
#define ubd(x, y) (upper_bound(all(x), y) - x.begin())
mt19937
rng(chrono::steady_clock::now()
.time_since_epoch()
.count()); // can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) {
return rng() % (y + 1 - x) + x;
} // inclusivesss
string inline to_string(char c) {
string s(1, c);
return s;
}
template <typename T> inline T gcd(T a, T b) {
return a == 0 ? llabs(b) : gcd(b % a, a);
}
using ll = long long;
using ld = long double;
#define FOR(i, s, e) for (ll i = s; i <= ll(e); ++i)
#define DEC(i, s, e) for (ll i = s; i >= ll(e); --i)
using pi = pair<ll, ll>;
using spi = pair<ll, pi>;
using dpi = pair<pi, pi>;
#define LLINF ((long long)1e18)
#define INF int(1e9 + 1e6)
#define MAXN (100006)
ll n, m, A[MAXN], colour[MAXN], cost[MAXN * 2];
bitset<MAXN> vis;
vector<pi> v[MAXN];
int main() {
FAST cin >> n >> m;
FOR(i, 1, n) cin >> A[i];
FOR(i, 1, m) {
ll a, b;
cin >> a >> b;
v[a].eb(b, i), v[b].eb(a, i);
}
FOR(i, 1, n) {
ll mi = LLINF;
for (auto j : v[i])
mi = min(mi, A[j.f]);
if (mi > A[i]) {
cout << "-1\n";
return 0;
}
}
vector<pi> D;
FOR(i, 1, n) D.eb(A[i], i);
sort(all(D), greater<>());
int co = 0;
while (D.size()) {
ll deal = D.back().f;
vector<int> cur;
cur.eb(D.back().s), D.pop_back();
while (D.size() && D.back().f == deal)
cur.eb(D.back().s), D.pop_back();
function<void(ll)> dfs = [&](ll x) {
if (vis[x])
return;
vis[x] = 1;
for (auto i : v[x])
if (cost[i.s] == 0) {
if (colour[i.f]) {
assert(vis[i.f]);
if (colour[i.f] == colour[x])
cost[i.s] = max(1ll, A[x] - A[i.f]);
else
cost[i.s] = A[x];
} else if (A[i.f] == deal) {
assert(!vis[i.f]); // since colour==0
colour[i.f] = 3 - colour[x];
cost[i.s] = A[x];
dfs(i.f);
}
}
};
for (auto &i : cur)
if (!vis[i])
colour[i] = (co & 1) + 1, dfs(i);
++co;
}
FOR(i, 1, n) if (colour[i] == 1) cout << 'W';
else cout << 'B';
cout << '\n';
FOR(i, 1, m) cout << cost[i] << '\n';
}
| replace | 43 | 44 | 43 | 44 | 0 | |
p02799 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
mt19937 mt(736);
class dsu {
vector<int> par, siz;
public:
dsu(int n) : par(n, -1), siz(n, 1) {}
int get_par(int v) { return par[v] == -1 ? v : get_par(par[v]); }
bool unite(int a, int b) {
a = get_par(a);
b = get_par(b);
if (a == b)
return false;
if (siz[a] < siz[b])
swap(a, b);
par[b] = a;
siz[a] += siz[b];
return true;
}
};
void dfs(const vector<vector<pair<int, int>>> &gr, vector<bool> &used,
vector<bool> &col, const unordered_set<int> &rev, int v, bool c) {
if (used[v])
return;
used[v] = true;
col[v] = c;
for (auto it : gr[v]) {
auto nc = c;
if (rev.count(it.second))
nc = !nc;
dfs(gr, used, col, rev, it.first, nc);
}
}
void solve(istream &cin = std::cin, ostream &cout = std::cout) {
int n, m;
cin >> n >> m;
vector<int> d(n);
for (auto &it : d)
cin >> it;
vector<pair<int, int>> edges;
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
edges.emplace_back(a, b);
}
vector<pair<int, int>> best(n, {-1, -1});
vector<int> c(m, (int)numeric_limits<int>::max());
for (int i = 0; i < m; i++) {
int a, b;
tie(a, b) = edges[i];
if (best[a].first == -1 ||
pair<int, int>{d[b], i} <
pair<int, int>{d[best[a].first], best[a].second})
best[a] = {b, i};
if (best[b].first == -1 ||
pair<int, int>{d[a], i} <
pair<int, int>{d[best[b].first], best[b].second})
best[b] = {a, i};
}
if (find(best.begin(), best.end(), pair<int, int>{-1, -1}) != best.end()) {
cout << -1 << endl;
return;
}
for (int i = 0; i < n; i++)
if (d[best[i].first] > d[i]) {
cout << -1 << endl;
cerr << "bad: " << i << endl;
return;
}
unordered_set<int> rev;
vector<vector<pair<int, int>>> gr(n);
for (auto &e : best) {
c[e.second] = abs(d[edges[e.second].first] - d[edges[e.second].second]);
if (c[e.second] == 0) {
c[e.second] = d[edges[e.second].first];
rev.insert(e.second);
}
gr[e.first].emplace_back(e.second, e.second);
gr[e.second].emplace_back(e.first, e.second);
}
vector<bool> used(n), col(n);
for (int i = 0; i < n; i++)
dfs(gr, used, col, rev, i, false);
assert(count(col.begin(), col.end(), false));
assert(count(col.begin(), col.end(), true));
for (auto it : col)
cout << (it ? 'B' : 'W');
cout << endl;
for (auto &it : c) {
it = min(it, (int)1e9);
cout << it << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed;
#ifdef LOCAL
auto st = clock();
ifstream fin("../input.txt");
solve(fin);
cout << "clock: " << (clock() - st) / (double)CLOCKS_PER_SEC << endl;
#else
solve();
#endif
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
mt19937 mt(736);
class dsu {
vector<int> par, siz;
public:
dsu(int n) : par(n, -1), siz(n, 1) {}
int get_par(int v) { return par[v] == -1 ? v : get_par(par[v]); }
bool unite(int a, int b) {
a = get_par(a);
b = get_par(b);
if (a == b)
return false;
if (siz[a] < siz[b])
swap(a, b);
par[b] = a;
siz[a] += siz[b];
return true;
}
};
void dfs(const vector<vector<pair<int, int>>> &gr, vector<bool> &used,
vector<bool> &col, const unordered_set<int> &rev, int v, bool c) {
if (used[v])
return;
used[v] = true;
col[v] = c;
for (auto it : gr[v]) {
auto nc = c;
if (rev.count(it.second))
nc = !nc;
dfs(gr, used, col, rev, it.first, nc);
}
}
void solve(istream &cin = std::cin, ostream &cout = std::cout) {
int n, m;
cin >> n >> m;
vector<int> d(n);
for (auto &it : d)
cin >> it;
vector<pair<int, int>> edges;
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
edges.emplace_back(a, b);
}
vector<pair<int, int>> best(n, {-1, -1});
vector<int> c(m, (int)numeric_limits<int>::max());
for (int i = 0; i < m; i++) {
int a, b;
tie(a, b) = edges[i];
if (best[a].first == -1 ||
pair<int, int>{d[b], i} <
pair<int, int>{d[best[a].first], best[a].second})
best[a] = {b, i};
if (best[b].first == -1 ||
pair<int, int>{d[a], i} <
pair<int, int>{d[best[b].first], best[b].second})
best[b] = {a, i};
}
if (find(best.begin(), best.end(), pair<int, int>{-1, -1}) != best.end()) {
cout << -1 << endl;
return;
}
for (int i = 0; i < n; i++)
if (d[best[i].first] > d[i]) {
cout << -1 << endl;
cerr << "bad: " << i << endl;
return;
}
unordered_set<int> rev;
vector<vector<pair<int, int>>> gr(n);
for (auto &e : best) {
c[e.second] = abs(d[edges[e.second].first] - d[edges[e.second].second]);
if (c[e.second] == 0) {
c[e.second] = d[edges[e.second].first];
rev.insert(e.second);
}
auto be = edges[e.second];
gr[be.first].emplace_back(be.second, e.second);
gr[be.second].emplace_back(be.first, e.second);
}
vector<bool> used(n), col(n);
for (int i = 0; i < n; i++)
dfs(gr, used, col, rev, i, false);
assert(count(col.begin(), col.end(), false));
assert(count(col.begin(), col.end(), true));
for (auto it : col)
cout << (it ? 'B' : 'W');
cout << endl;
for (auto &it : c) {
it = min(it, (int)1e9);
cout << it << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed;
#ifdef LOCAL
auto st = clock();
ifstream fin("../input.txt");
solve(fin);
cout << "clock: " << (clock() - st) / (double)CLOCKS_PER_SEC << endl;
#else
solve();
#endif
return 0;
}
| replace | 115 | 117 | 115 | 119 | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.