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 |
|---|---|---|---|---|---|---|---|---|---|---|---|
p02832 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long
const int mod = 1000000007;
const int MOD = 998244353;
const ll INF = 1000000000000000000;
int A[100010];
int main() {
int N;
cin >> N;
for (int i = 0; i < N; i++)
cin >> A[i];
int ans = 0;
int num = 1;
for (int i = 0; i < N; i++) {
if (A[i] != num)
ans++;
else
num++;
}
if (ans == N)
cout << -1 << endl;
else
cout << ans << endl;
} | #include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long
const int mod = 1000000007;
const int MOD = 998244353;
const ll INF = 1000000000000000000;
int A[200010];
int main() {
int N;
cin >> N;
for (int i = 0; i < N; i++)
cin >> A[i];
int ans = 0;
int num = 1;
for (int i = 0; i < N; i++) {
if (A[i] != num)
ans++;
else
num++;
}
if (ans == N)
cout << -1 << endl;
else
cout << ans << endl;
} | replace | 17 | 18 | 17 | 18 | 0 | |
p02832 | C++ | Runtime Error | #include <iostream>
using namespace std;
int main() {
long N, a[20000];
long i, k = 1;
long count = 1;
cin >> N;
for (i = 1; i <= N; i++) {
cin >> a[i];
}
for (i = 1; i <= N; i++)
if (a[i] == count)
count++;
if (count == 1) {
cout << "-1" << endl;
return 0;
}
cout << N - count + 1 << endl;
return 0;
}
| #include <iostream>
using namespace std;
int main() {
long N, a[200000];
long i, k = 1;
long count = 1;
cin >> N;
for (i = 1; i <= N; i++) {
cin >> a[i];
}
for (i = 1; i <= N; i++)
if (a[i] == count)
count++;
if (count == 1) {
cout << "-1" << endl;
return 0;
}
cout << N - count + 1 << endl;
return 0;
}
| replace | 5 | 6 | 5 | 6 | 0 | |
p02832 | C++ | Runtime Error | // in the name of god
// if you read this code please search about imam hussain
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define endl "\n"
#define X first
#define Y second
#define pii pair<int, int>
#define migmig \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0)
#define read \
freopen("input.txt", "r", stdin); \
freopen("output.txt", "w", stdout)
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9;
const int del = 728729;
ll poww(ll a, ll b, ll md) {
return (!b ? 1
: (b & 1 ? a * poww(a * a % md, b / 2, md) % md
: poww(a * a % md, b / 2, md) % md));
}
ll n;
ll q[maxn];
ll nw = 1;
ll ted;
int main() {
migmig;
vector<int> v;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> q[i];
}
for (int i = 0; i < n; i++) {
if (q[i] == nw) {
nw++;
} else {
ted++;
}
}
if (nw == 1) {
cout << "-1";
} else {
cout << ted;
}
return 0;
}
| // in the name of god
// if you read this code please search about imam hussain
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define endl "\n"
#define X first
#define Y second
#define pii pair<int, int>
#define migmig \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0)
#define read \
freopen("input.txt", "r", stdin); \
freopen("output.txt", "w", stdout)
const int maxn = 2e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9;
const int del = 728729;
ll poww(ll a, ll b, ll md) {
return (!b ? 1
: (b & 1 ? a * poww(a * a % md, b / 2, md) % md
: poww(a * a % md, b / 2, md) % md));
}
ll n;
ll q[maxn];
ll nw = 1;
ll ted;
int main() {
migmig;
vector<int> v;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> q[i];
}
for (int i = 0; i < n; i++) {
if (q[i] == nw) {
nw++;
} else {
ted++;
}
}
if (nw == 1) {
cout << "-1";
} else {
cout << ted;
}
return 0;
}
| replace | 21 | 22 | 21 | 22 | 0 | |
p02832 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
int gcd(int x, int y) {
int t = 0;
t = (x + y) / 2;
if (x && y != t) {
if (x > y)
return gcd(x = x - x / y * y, y);
else if (x < y)
return gcd(x, y = y - y / x * x);
} else if (x > y)
return t = x;
else
return t = y;
}
int main() {
int n;
int a[n];
int j = 0;
bool ok = false;
cin >> n;
REP(i, n) { cin >> a[i]; }
for (int i = 0; i < n; i++) {
/* code */
if (a[i] == 1 + j) {
j++;
ok = true;
}
}
if (ok) {
cout << n - j << endl;
} else {
cout << -1 << endl;
}
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
int gcd(int x, int y) {
int t = 0;
t = (x + y) / 2;
if (x && y != t) {
if (x > y)
return gcd(x = x - x / y * y, y);
else if (x < y)
return gcd(x, y = y - y / x * x);
} else if (x > y)
return t = x;
else
return t = y;
}
int main() {
int n;
int a[1000000];
int j = 0;
bool ok = false;
cin >> n;
REP(i, n) { cin >> a[i]; }
for (int i = 0; i < n; i++) {
/* code */
if (a[i] == 1 + j) {
j++;
ok = true;
}
}
if (ok) {
cout << n - j << endl;
} else {
cout << -1 << endl;
}
}
| replace | 22 | 23 | 22 | 23 | -11 | |
p02832 | C++ | Runtime Error | #include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using pii = pair<int, int>;
using ll = long long;
using ld = long double;
#define pb push_back
#define mp make_pair
#define sc second
#define fr first
#define stpr setprecision
#define cYES cout << "YES" << endl
#define cNO cout << "NO" << endl
#define cYes cout << "Yes" << endl
#define cNo cout << "No" << endl
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define Rep(i, a, b) for (ll i = (a); i < (b); ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define rRep(i, a, b) for (int i = a; i >= b; i--)
#define crep(i) for (char i = 'a'; i <= 'z'; ++i)
#define psortsecond(A, N) \
sort(A, A + N, \
[](const pii &a, const pii &b) { return a.second < b.second; });
#define ALL(x) (x).begin(), (x).end()
#define debug(v) \
cout << #v << ":"; \
for (auto x : v) { \
cout << x << ' '; \
} \
cout << endl;
#define endl '\n'
int ctoi(const char c) {
if ('0' <= c && c <= '9')
return (c - '0');
return -1;
}
ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
constexpr ll MOD = 1000000007;
constexpr ll INF = 1000000011;
constexpr ll MOD2 = 998244353;
constexpr ll LINF = 1001002003004005006ll;
constexpr ld EPS = 10e-8;
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;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
for (auto &&x : v)
is >> x;
return is;
}
template <typename T, typename U>
istream &operator>>(istream &is, pair<T, U> &p) {
is >> p.first;
is >> p.second;
return is;
}
template <typename T, typename U>
ostream &operator>>(ostream &os, const pair<T, U> &p) {
os << p.first << ' ' << p.second;
return os;
}
template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
for (auto i = begin(v); i != end(v); ++i) {
if (i != begin(v))
os << ' ';
os << *i;
}
return os;
}
int main() {
ll N;
cin >> N;
ll A[100007];
ll K = 1;
rep(i, N) {
cin >> A[i];
if (A[i] == K) {
K++;
}
}
if (K == 1) {
cout << -1 << endl;
} else {
cout << N - K + 1 << endl;
}
} | #include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using pii = pair<int, int>;
using ll = long long;
using ld = long double;
#define pb push_back
#define mp make_pair
#define sc second
#define fr first
#define stpr setprecision
#define cYES cout << "YES" << endl
#define cNO cout << "NO" << endl
#define cYes cout << "Yes" << endl
#define cNo cout << "No" << endl
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define Rep(i, a, b) for (ll i = (a); i < (b); ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define rRep(i, a, b) for (int i = a; i >= b; i--)
#define crep(i) for (char i = 'a'; i <= 'z'; ++i)
#define psortsecond(A, N) \
sort(A, A + N, \
[](const pii &a, const pii &b) { return a.second < b.second; });
#define ALL(x) (x).begin(), (x).end()
#define debug(v) \
cout << #v << ":"; \
for (auto x : v) { \
cout << x << ' '; \
} \
cout << endl;
#define endl '\n'
int ctoi(const char c) {
if ('0' <= c && c <= '9')
return (c - '0');
return -1;
}
ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
constexpr ll MOD = 1000000007;
constexpr ll INF = 1000000011;
constexpr ll MOD2 = 998244353;
constexpr ll LINF = 1001002003004005006ll;
constexpr ld EPS = 10e-8;
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;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
for (auto &&x : v)
is >> x;
return is;
}
template <typename T, typename U>
istream &operator>>(istream &is, pair<T, U> &p) {
is >> p.first;
is >> p.second;
return is;
}
template <typename T, typename U>
ostream &operator>>(ostream &os, const pair<T, U> &p) {
os << p.first << ' ' << p.second;
return os;
}
template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
for (auto i = begin(v); i != end(v); ++i) {
if (i != begin(v))
os << ' ';
os << *i;
}
return os;
}
int main() {
ll N;
cin >> N;
ll A[200007];
ll K = 1;
rep(i, N) {
cin >> A[i];
if (A[i] == K) {
K++;
}
}
if (K == 1) {
cout << -1 << endl;
} else {
cout << N - K + 1 << endl;
}
} | replace | 99 | 100 | 99 | 100 | 0 | |
p02832 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#if MYDEBUG
#include "lib/cp_debug.hpp"
#else
#define DBG(...) ;
#endif
#if __cplusplus <= 201402L
template <typename T> T gcd(T a, T b) {
return ((a % b == 0) ? b : gcd(b, a % b));
}
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#endif
using LL = long long;
constexpr LL LINF = 334ll << 53;
constexpr int INF = 15 << 26;
constexpr LL MOD = 1E9 + 7;
namespace Problem {
using namespace std;
class Solver {
public:
int n;
vector<int> a, r, memo;
Solver(LL n) : n(n), a(n), r(n, -1), memo(200000, -1){};
void solve() {
bool ok = false;
for (int i = 0; i < n; ++i) {
cin >> a[i];
a[i]--;
if (a[i] == 0)
ok = true;
}
if (!ok) {
cout << -1 << endl;
return;
}
DBG(a)
for (int i = n - 1; i >= 0; --i) {
memo[a[i]] = i;
if (memo[a[i] + 1] >= 0) {
r[i] = memo[a[i] + 1];
}
}
DBG(r)
int p = 0;
while (a[p] != 0)
p++;
int len = 0;
DBG(p)
while (p >= 0) {
p = r[p];
len++;
}
cout << n - len << endl;
}
};
} // namespace Problem
int main() {
std::cin.tie(0);
std::ios_base::sync_with_stdio(false);
// std::cout << std::fixed << std::setprecision(12);
long long n = 0;
std::cin >> n;
Problem::Solver sol(n);
sol.solve();
return 0;
}
| #include <bits/stdc++.h>
#if MYDEBUG
#include "lib/cp_debug.hpp"
#else
#define DBG(...) ;
#endif
#if __cplusplus <= 201402L
template <typename T> T gcd(T a, T b) {
return ((a % b == 0) ? b : gcd(b, a % b));
}
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#endif
using LL = long long;
constexpr LL LINF = 334ll << 53;
constexpr int INF = 15 << 26;
constexpr LL MOD = 1E9 + 7;
namespace Problem {
using namespace std;
class Solver {
public:
int n;
vector<int> a, r, memo;
Solver(LL n) : n(n), a(n), r(n, -1), memo(200001, -1){};
void solve() {
bool ok = false;
for (int i = 0; i < n; ++i) {
cin >> a[i];
a[i]--;
if (a[i] == 0)
ok = true;
}
if (!ok) {
cout << -1 << endl;
return;
}
DBG(a)
for (int i = n - 1; i >= 0; --i) {
memo[a[i]] = i;
if (memo[a[i] + 1] >= 0) {
r[i] = memo[a[i] + 1];
}
}
DBG(r)
int p = 0;
while (a[p] != 0)
p++;
int len = 0;
DBG(p)
while (p >= 0) {
p = r[p];
len++;
}
cout << n - len << endl;
}
};
} // namespace Problem
int main() {
std::cin.tie(0);
std::ios_base::sync_with_stdio(false);
// std::cout << std::fixed << std::setprecision(12);
long long n = 0;
std::cin >> n;
Problem::Solver sol(n);
sol.solve();
return 0;
}
| replace | 25 | 26 | 25 | 26 | TLE | |
p02832 | C++ | Time Limit Exceeded | #include <iostream>
#include <vector>
int main() {
int n;
std::cin >> n;
std::vector<int> a(n);
for (int i = 0; i < n; i++) {
std::cin >> a.at(i);
}
int ans = 0;
int tmp = 1;
int size = a.size();
for (int i = 0; i < a.size(); i++) {
if (a.at(i) != tmp) {
a.erase(a.begin() + i);
i--;
ans++;
} else {
tmp++;
}
}
if (ans == size) {
ans = -1;
}
std::cout << ans << std::endl;
return 0;
}
| #include <iostream>
#include <vector>
int main() {
int n;
std::cin >> n;
std::vector<int> a(n);
for (int i = 0; i < n; i++) {
std::cin >> a.at(i);
}
int ans = 0;
int tmp = 1;
int size = a.size();
for (int i = 0; i < a.size(); i++) {
if (a.at(i) != tmp) {
ans++;
} else {
tmp++;
}
}
if (ans == size) {
ans = -1;
}
std::cout << ans << std::endl;
return 0;
}
| delete | 17 | 19 | 17 | 17 | TLE | |
p02832 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e5 + 10;
int a[N];
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n;
cin >> n;
int cnt = 1, ans = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
if (a[i] == cnt)
cnt++;
else
ans++;
}
if (ans == n)
cout << -1 << endl;
else
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 5e5 + 10;
int a[N];
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n;
cin >> n;
int cnt = 1, ans = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
if (a[i] == cnt)
cnt++;
else
ans++;
}
if (ans == n)
cout << -1 << endl;
else
cout << ans << endl;
} | replace | 5 | 6 | 5 | 6 | 0 | |
p02832 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int n, f = 1, sum, a[1001];
int main() {
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= n; i++)
if (a[i] == f)
f++;
else
sum++;
if (f == 1)
cout << -1;
else
cout << sum;
} | #include <bits/stdc++.h>
using namespace std;
int n, f = 1, sum, a[200001];
int main() {
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= n; i++)
if (a[i] == f)
f++;
else
sum++;
if (f == 1)
cout << -1;
else
cout << sum;
} | replace | 2 | 3 | 2 | 3 | 0 | |
p02832 | C++ | Runtime Error | #define DBG 0 // submit 0
#define ll long long
#if DBG
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>
#else
#include <bits/stdc++.h>
#endif
using namespace std;
int main() {
int a[100005], b[100005], c[100005];
// bool flag=false,d[100005];
int n, ans = 0;
// char s[105],t[105],u;
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
// for(int i=0;i<n;i++) cin >> t[i];
for (int i = 0; i < n; i++) {
if (a[i] == ans + 1)
ans++;
}
ans = n - ans;
if (ans == n)
ans = -1;
// cout << (!flag ? "Yes" : "No");
cout << ans;
// cout << s;
// printf("%.10f",ans_d);
cout << endl;
}
| #define DBG 0 // submit 0
#define ll long long
#if DBG
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>
#else
#include <bits/stdc++.h>
#endif
using namespace std;
int main() {
int a[200005];
// bool flag=false,d[100005];
int n, ans = 0;
// char s[105],t[105],u;
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
// for(int i=0;i<n;i++) cin >> t[i];
for (int i = 0; i < n; i++) {
if (a[i] == ans + 1)
ans++;
}
ans = n - ans;
if (ans == n)
ans = -1;
// cout << (!flag ? "Yes" : "No");
cout << ans;
// cout << s;
// printf("%.10f",ans_d);
cout << endl;
}
| replace | 21 | 22 | 21 | 22 | 0 | |
p02832 | C++ | Runtime Error | /*
Author: Nguyen Tan Bao
Status:
Idea:
*/
#include "bits/stdc++.h"
#define FI first
#define SE second
#define EPS 1e-9
#define ALL(a) a.begin(), a.end()
#define SZ(a) int((a).size())
#define MS(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORE(i, a, b) for (int i = (a); i >= (b); i--)
#define FORALL(it, a) \
for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
//__builtin_ffs(x) return 1 + index of least significant 1-bit of x
//__builtin_clz(x) return number of leading zeros of x
//__builtin_ctz(x) return number of trailing zeros of x
using namespace std;
using ll = long long;
using ld = double;
typedef pair<int, int> II;
typedef pair<II, ll> III;
typedef complex<ld> cd;
typedef vector<cd> vcd;
const ll MODBASE = 1000000007LL;
const int MAXN = 100010;
const int MAXM = 1000;
const int MAXK = 16;
const int MAXQ = 200010;
int n, a[MAXN];
int main() {
ios::sync_with_stdio(0);
cin.tie(nullptr);
cin >> n;
FOR(i, 1, n) cin >> a[i];
int now = 1, res = 0;
FOR(i, 1, n)
if (a[i] == now) {
now++;
} else
res++;
if (now == 1)
res = -1;
cout << res;
return 0;
} | /*
Author: Nguyen Tan Bao
Status:
Idea:
*/
#include "bits/stdc++.h"
#define FI first
#define SE second
#define EPS 1e-9
#define ALL(a) a.begin(), a.end()
#define SZ(a) int((a).size())
#define MS(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORE(i, a, b) for (int i = (a); i >= (b); i--)
#define FORALL(it, a) \
for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
//__builtin_ffs(x) return 1 + index of least significant 1-bit of x
//__builtin_clz(x) return number of leading zeros of x
//__builtin_ctz(x) return number of trailing zeros of x
using namespace std;
using ll = long long;
using ld = double;
typedef pair<int, int> II;
typedef pair<II, ll> III;
typedef complex<ld> cd;
typedef vector<cd> vcd;
const ll MODBASE = 1000000007LL;
const int MAXN = 200010;
const int MAXM = 1000;
const int MAXK = 16;
const int MAXQ = 200010;
int n, a[MAXN];
int main() {
ios::sync_with_stdio(0);
cin.tie(nullptr);
cin >> n;
FOR(i, 1, n) cin >> a[i];
int now = 1, res = 0;
FOR(i, 1, n)
if (a[i] == now) {
now++;
} else
res++;
if (now == 1)
res = -1;
cout << res;
return 0;
} | replace | 30 | 31 | 30 | 31 | 0 | |
p02832 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int cnt = 0;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a.at(i);
}
int i = 0;
while (i < n && a.at(i) != 0) {
if (a.at(i) == i + 1) {
i++;
continue;
}
if (a.at(i) != i + 1) {
a.erase(a.begin() + i);
a.push_back(0);
cnt++;
}
}
if (cnt == n) {
cout << "-1" << endl;
} else {
cout << cnt << endl;
}
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int cnt = 0;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a.at(i);
}
int ans = 1;
for (int i = 0; i < n; i++) {
if (a.at(i) == ans) {
ans++;
} else {
cnt++;
}
}
if (cnt == n) {
cout << "-1" << endl;
} else {
cout << cnt << endl;
}
}
| replace | 11 | 21 | 11 | 16 | TLE | |
p02832 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> A(N);
for (int i = 0; i < N; i++) {
cin >> A.at(i);
}
int C = 0;
int Size = N;
int Num = 1;
while (C < Size) {
if (Num == A.at(C)) {
Num++;
C++;
} else {
A.erase(A.begin() + C);
Size--;
}
}
if (Num > 1) {
cout << N - (Num - 1);
} else {
cout << -1;
}
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> A(N);
for (int i = 0; i < N; i++) {
cin >> A.at(i);
}
int C = 0;
int Size = N;
int Num = 1;
while (C < Size) {
if (Num == A.at(C)) {
Num++;
C++;
} else {
C++;
}
}
if (Num > 1) {
cout << N - (Num - 1);
} else {
cout << -1;
}
}
| replace | 18 | 20 | 18 | 19 | TLE | |
p02832 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
int n, a[20010] = {}, serch = 1, ans = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
if (a[i] == serch) {
serch++;
} else {
ans++;
}
}
if (serch == 1) {
cout << "-1" << endl;
} else {
cout << ans << endl;
}
return 0;
} | #include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
int n, a[200100] = {}, serch = 1, ans = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
if (a[i] == serch) {
serch++;
} else {
ans++;
}
}
if (serch == 1) {
cout << "-1" << endl;
} else {
cout << ans << endl;
}
return 0;
} | replace | 6 | 7 | 6 | 7 | 0 | |
p02832 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> a(N);
for (int i = 1; i <= N; i++) {
cin >> a[i];
}
if (N == 1) {
if (a[1] == 1) {
cout << 0 << endl;
} else {
cout << -1 << endl;
}
return 0;
}
int num = 1, ans = 0;
for (int i = 1; i <= N; i++) {
if (a[i] == num) {
num++;
} else {
ans++;
}
}
if (ans == N) {
cout << -1 << endl;
} else if (num > 1) {
cout << ans << endl;
} else {
cout << -1 << endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
int a[N];
for (int i = 1; i <= N; i++) {
cin >> a[i];
}
if (N == 1) {
if (a[1] == 1) {
cout << 0 << endl;
} else {
cout << -1 << endl;
}
return 0;
}
int num = 1, ans = 0;
for (int i = 1; i <= N; i++) {
if (a[i] == num) {
num++;
} else {
ans++;
}
}
if (ans == N) {
cout << -1 << endl;
} else if (num > 1) {
cout << ans << endl;
} else {
cout << -1 << endl;
}
return 0;
}
| replace | 7 | 8 | 7 | 8 | 0 | |
p02832 | 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 = 200005;
#define M 1000000007
#define double long double
#define BINF 10000000000000000
#define init(arr, val) memset(arr, val, sizeof(arr))
#define MAXN 15000001
#define deb(x) cout << #x << " " << x << "\n";
const int LG = 22;
multiset<int> s[N];
#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;
cin >> n;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
s[x].insert(i);
}
int ans = 0;
if (s[1].size() == 0) {
cout << -1 << endl;
return 0;
}
int ind = *s[1].begin();
int x = 2;
ans = 1;
while (1) {
auto it = s[x].upper_bound(ind);
if (it == s[x].end())
break;
ans++;
x++;
ind = *it;
}
cout << n - ans << endl;
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 = 200005;
#define M 1000000007
#define double long double
#define BINF 10000000000000000
#define init(arr, val) memset(arr, val, sizeof(arr))
#define MAXN 15000001
#define deb(x) cout << #x << " " << x << "\n";
const int LG = 22;
multiset<int> s[N];
#undef int
int main() {
#define int long long int
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
s[x].insert(i);
}
int ans = 0;
if (s[1].size() == 0) {
cout << -1 << endl;
return 0;
}
int ind = *s[1].begin();
int x = 2;
ans = 1;
while (1) {
auto it = s[x].upper_bound(ind);
if (it == s[x].end())
break;
ans++;
x++;
ind = *it;
}
cout << n - ans << endl;
return 0;
}
| delete | 27 | 32 | 27 | 27 | -11 | |
p02832 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using P = pair<int, int>;
using ll = long long;
int main() {
int n;
cin >> n;
vector<int> a;
rep(i, n) cin >> a[i];
int nxt = 1, ans = 0;
rep(i, n) {
if (a[i] == nxt) {
nxt++;
} else
ans++;
}
if (ans == n)
ans = -1;
cout << ans << endl;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using P = pair<int, int>;
using ll = long long;
int main() {
int n;
cin >> n;
vector<int> a(n);
rep(i, n) cin >> a[i];
int nxt = 1, ans = 0;
rep(i, n) {
if (a[i] == nxt) {
nxt++;
} else
ans++;
}
if (ans == n)
ans = -1;
cout << ans << endl;
} | replace | 9 | 10 | 9 | 10 | -11 | |
p02832 | C++ | Runtime Error |
#include <bits/stdc++.h>
#define ln '\n'
#define all(dat) dat.begin(), dat.end()
#define loop(i, to) for (int i = 0; i < to; ++i)
#define cont(i, to) for (int i = 1; i <= to; ++i)
#define circ(i, fm, to) for (int i = fm; i <= to; ++i)
#define foreach(i, dat) \
for (__typeof(dat.begin()) i = dat.begin(); i != dat.end(); ++i)
typedef long long num;
using namespace std;
const int nsz = 1e5;
int n, a[nsz + 5];
int main() {
ios::sync_with_stdio(0);
cin >> n;
cont(i, n) { cin >> a[i]; }
int cur = 0;
cont(i, n) {
if (a[i] == cur + 1) {
++cur;
}
}
if (cur == 0) {
cout << -1 << ln;
} else {
cout << n - cur << ln;
}
} |
#include <bits/stdc++.h>
#define ln '\n'
#define all(dat) dat.begin(), dat.end()
#define loop(i, to) for (int i = 0; i < to; ++i)
#define cont(i, to) for (int i = 1; i <= to; ++i)
#define circ(i, fm, to) for (int i = fm; i <= to; ++i)
#define foreach(i, dat) \
for (__typeof(dat.begin()) i = dat.begin(); i != dat.end(); ++i)
typedef long long num;
using namespace std;
const int nsz = 2e5;
int n, a[nsz + 5];
int main() {
ios::sync_with_stdio(0);
cin >> n;
cont(i, n) { cin >> a[i]; }
int cur = 0;
cont(i, n) {
if (a[i] == cur + 1) {
++cur;
}
}
if (cur == 0) {
cout << -1 << ln;
} else {
cout << n - cur << ln;
}
} | replace | 15 | 16 | 15 | 16 | 0 | |
p02832 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
long long int x = 1, res, m, n, a[100000], i, b[1000] = {0}, cnt = 0;
cin >> n;
for (i = 1; i <= n; i++) {
cin >> a[i];
if (a[i] == x) {
cnt++;
x++;
}
// b[a[i]]++;
}
if (cnt != 0)
cout << n - cnt << endl;
else
cout << -1 << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
long long int x = 1, res, m, n, a[200008], i, b[1000] = {0}, cnt = 0;
cin >> n;
for (i = 1; i <= n; i++) {
cin >> a[i];
if (a[i] == x) {
cnt++;
x++;
}
// b[a[i]]++;
}
if (cnt != 0)
cout << n - cnt << endl;
else
cout << -1 << endl;
}
| replace | 3 | 4 | 3 | 4 | 0 | |
p02832 | C++ | Runtime Error | #include <cstdio>
int main(int argc, const char *argv[]) {
int n;
scanf("%d", &n);
int a[20000];
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
int ans = 0, cur = 1;
for (int i = 0; i < n; i++) {
if (a[i] != cur) {
ans++;
} else {
cur++;
}
}
printf("%d\n", ans == n ? -1 : ans);
} | #include <cstdio>
int main(int argc, const char *argv[]) {
int n;
scanf("%d", &n);
int a[300000];
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
int ans = 0, cur = 1;
for (int i = 0; i < n; i++) {
if (a[i] != cur) {
ans++;
} else {
cur++;
}
}
printf("%d\n", ans == n ? -1 : ans);
}
| replace | 6 | 7 | 6 | 7 | 0 | |
p02832 | C++ | Time Limit Exceeded |
/*
𝓐𝓾𝓽𝓱𝓸𝓻:-
𝓢𝓪𝓾𝓻𝓪𝓫𝓱𝓼𝓱𝓪𝓭𝓸𝔀
------------------------------------------------------------------------------------------------------------------
██╗░░██╗░█████╗░██████╗░██████╗░ ░██╗░░░░░░░██╗░█████╗░██████╗░██╗░░██╗ ██████╗░███████╗░█████╗░████████╗░██████╗
██║░░██║██╔══██╗██╔══██╗██╔══██╗ ░██║░░██╗░░██║██╔══██╗██╔══██╗██║░██╔╝ ██╔══██╗██╔════╝██╔══██╗╚══██╔══╝██╔════╝
███████║███████║██████╔╝██║░░██║ ░╚██╗████╗██╔╝██║░░██║██████╔╝█████═╝░ ██████╦╝█████╗░░███████║░░░██║░░░╚█████╗░
██╔══██║██╔══██║██╔══██╗██║░░██║ ░░████╔═████║░██║░░██║██╔══██╗██╔═██╗░ ██╔══██╗██╔══╝░░██╔══██║░░░██║░░░░╚═══██╗
██║░░██║██║░░██║██║░░██║██████╔╝ ░░╚██╔╝░╚██╔╝░╚█████╔╝██║░░██║██║░╚██╗ ██████╦╝███████╗██║░░██║░░░██║░░░██████╔╝
╚═╝░░╚═╝╚═╝░░╚═╝╚═╝░░╚═╝╚═════╝░ ░░░╚═╝░░░╚═╝░░░╚════╝░╚═╝░░╚═╝╚═╝░░╚═╝ ╚═════╝░╚══════╝╚═╝░░╚═╝░░░╚═╝░░░╚═════╝░
████████╗░█████╗░██╗░░░░░███████╗███╗░░██╗████████╗ ░██╗░░░░░░░██╗██╗░░██╗███████╗███╗░░██╗
╚══██╔══╝██╔══██╗██║░░░░░██╔════╝████╗░██║╚══██╔══╝ ░██║░░██╗░░██║██║░░██║██╔════╝████╗░██║
░░░██║░░░███████║██║░░░░░█████╗░░██╔██╗██║░░░██║░░░ ░╚██╗████╗██╔╝███████║█████╗░░██╔██╗██║
░░░██║░░░██╔══██║██║░░░░░██╔══╝░░██║╚████║░░░██║░░░ ░░████╔═████║░██╔══██║██╔══╝░░██║╚████║
░░░██║░░░██║░░██║███████╗███████╗██║░╚███║░░░██║░░░ ░░╚██╔╝░╚██╔╝░██║░░██║███████╗██║░╚███║
░░░╚═╝░░░╚═╝░░╚═╝╚══════╝╚══════╝╚═╝░░╚══╝░░░╚═╝░░░ ░░░╚═╝░░░╚═╝░░╚═╝░░╚═╝╚══════╝╚═╝░░╚══╝
████████╗░█████╗░██╗░░░░░███████╗███╗░░██╗████████╗ ██████╗░░█████╗░███████╗░██████╗███╗░░██╗██╗████████╗
╚══██╔══╝██╔══██╗██║░░░░░██╔════╝████╗░██║╚══██╔══╝ ██╔══██╗██╔══██╗██╔════╝██╔════╝████╗░██║╚█║╚══██╔══╝
░░░██║░░░███████║██║░░░░░█████╗░░██╔██╗██║░░░██║░░░ ██║░░██║██║░░██║█████╗░░╚█████╗░██╔██╗██║░╚╝░░░██║░░░
░░░██║░░░██╔══██║██║░░░░░██╔══╝░░██║╚████║░░░██║░░░ ██║░░██║██║░░██║██╔══╝░░░╚═══██╗██║╚████║░░░░░░██║░░░
░░░██║░░░██║░░██║███████╗███████╗██║░╚███║░░░██║░░░ ██████╔╝╚█████╔╝███████╗██████╔╝██║░╚███║░░░░░░██║░░░
░░░╚═╝░░░╚═╝░░╚═╝╚══════╝╚══════╝╚═╝░░╚══╝░░░╚═╝░░░ ╚═════╝░░╚════╝░╚══════╝╚═════╝░╚═╝░░╚══╝░░░░░░╚═╝░░░
░██╗░░░░░░░██╗░█████╗░██████╗░██╗░░██╗ ██╗░░██╗░█████╗░██████╗░██████╗░
░██║░░██╗░░██║██╔══██╗██╔══██╗██║░██╔╝ ██║░░██║██╔══██╗██╔══██╗██╔══██╗
░╚██╗████╗██╔╝██║░░██║██████╔╝█████═╝░ ███████║███████║██████╔╝██║░░██║
░░████╔═████║░██║░░██║██╔══██╗██╔═██╗░ ██╔══██║██╔══██║██╔══██╗██║░░██║
░░╚██╔╝░╚██╔╝░╚█████╔╝██║░░██║██║░╚██╗ ██║░░██║██║░░██║██║░░██║██████╔╝
░░░╚═╝░░░╚═╝░░░╚════╝░╚═╝░░╚═╝╚═╝░░╚═╝ ╚═╝░░╚═╝╚═╝░░╚═╝╚═╝░░╚═╝╚═════╝░
------------------------------------------------------------------------
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define loop(i, n) for (i = 0; i < n; i++)
#define loopf(i, a, n) for (i = a; i < n; i++)
#define loopb(i, a, n) for (i = a; i >= n; i--)
#define fore(name) for (auto it = name.begin(); it != name.end(); it++)
#define w(t) \
int t; \
cin >> t; \
while (t--)
#define pb(x) push_back(x)
#define in(y) insert(y)
#define make(x, y) make_pair(x, y)
#define LOAD_FACTOR_set(name) \
name.reserve(32768); \
name.max_load_factor(0.25);
#define LOAD_FACTOR_map(name) \
name.reserve(1024); \
name.max_load_factor(0.25);
#define lcm(a, b) ((a * b)) / (__gcd(a, b))
#define int long long int
#define REDBULL \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
#define mod 1000000007
#define endl "\n"
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
#define ordered_justset \
tree<int, null_type, less<int>, rb_tree_tag, \
tree_order_statistics_node_update>
struct HASH {
size_t operator()(const pair<int, int> &x) const {
return ((int)x.first) ^ (((int)x.second) << 32);
}
};
int32_t main() {
REDBULL
int n;
cin >> n;
int input;
int i;
std::vector<int> v;
loop(i, n) {
cin >> input;
v.pb(input);
}
i = 1;
auto f = v.begin();
while (f != v.end()) {
if (*f != i) {
v.erase(f);
f = v.begin();
// cout<<*f<<endl;
// cout<<"ehre"<<endl;
} else {
// cout<<"amadr"<<endl;
i++;
f++;
}
}
i--;
// cout<<i<<endl;
if (i <= 0)
cout << -1 << endl;
else
cout << n - i << endl;
// cout<<i<<endl;
return 0;
}
|
/*
𝓐𝓾𝓽𝓱𝓸𝓻:-
𝓢𝓪𝓾𝓻𝓪𝓫𝓱𝓼𝓱𝓪𝓭𝓸𝔀
------------------------------------------------------------------------------------------------------------------
██╗░░██╗░█████╗░██████╗░██████╗░ ░██╗░░░░░░░██╗░█████╗░██████╗░██╗░░██╗ ██████╗░███████╗░█████╗░████████╗░██████╗
██║░░██║██╔══██╗██╔══██╗██╔══██╗ ░██║░░██╗░░██║██╔══██╗██╔══██╗██║░██╔╝ ██╔══██╗██╔════╝██╔══██╗╚══██╔══╝██╔════╝
███████║███████║██████╔╝██║░░██║ ░╚██╗████╗██╔╝██║░░██║██████╔╝█████═╝░ ██████╦╝█████╗░░███████║░░░██║░░░╚█████╗░
██╔══██║██╔══██║██╔══██╗██║░░██║ ░░████╔═████║░██║░░██║██╔══██╗██╔═██╗░ ██╔══██╗██╔══╝░░██╔══██║░░░██║░░░░╚═══██╗
██║░░██║██║░░██║██║░░██║██████╔╝ ░░╚██╔╝░╚██╔╝░╚█████╔╝██║░░██║██║░╚██╗ ██████╦╝███████╗██║░░██║░░░██║░░░██████╔╝
╚═╝░░╚═╝╚═╝░░╚═╝╚═╝░░╚═╝╚═════╝░ ░░░╚═╝░░░╚═╝░░░╚════╝░╚═╝░░╚═╝╚═╝░░╚═╝ ╚═════╝░╚══════╝╚═╝░░╚═╝░░░╚═╝░░░╚═════╝░
████████╗░█████╗░██╗░░░░░███████╗███╗░░██╗████████╗ ░██╗░░░░░░░██╗██╗░░██╗███████╗███╗░░██╗
╚══██╔══╝██╔══██╗██║░░░░░██╔════╝████╗░██║╚══██╔══╝ ░██║░░██╗░░██║██║░░██║██╔════╝████╗░██║
░░░██║░░░███████║██║░░░░░█████╗░░██╔██╗██║░░░██║░░░ ░╚██╗████╗██╔╝███████║█████╗░░██╔██╗██║
░░░██║░░░██╔══██║██║░░░░░██╔══╝░░██║╚████║░░░██║░░░ ░░████╔═████║░██╔══██║██╔══╝░░██║╚████║
░░░██║░░░██║░░██║███████╗███████╗██║░╚███║░░░██║░░░ ░░╚██╔╝░╚██╔╝░██║░░██║███████╗██║░╚███║
░░░╚═╝░░░╚═╝░░╚═╝╚══════╝╚══════╝╚═╝░░╚══╝░░░╚═╝░░░ ░░░╚═╝░░░╚═╝░░╚═╝░░╚═╝╚══════╝╚═╝░░╚══╝
████████╗░█████╗░██╗░░░░░███████╗███╗░░██╗████████╗ ██████╗░░█████╗░███████╗░██████╗███╗░░██╗██╗████████╗
╚══██╔══╝██╔══██╗██║░░░░░██╔════╝████╗░██║╚══██╔══╝ ██╔══██╗██╔══██╗██╔════╝██╔════╝████╗░██║╚█║╚══██╔══╝
░░░██║░░░███████║██║░░░░░█████╗░░██╔██╗██║░░░██║░░░ ██║░░██║██║░░██║█████╗░░╚█████╗░██╔██╗██║░╚╝░░░██║░░░
░░░██║░░░██╔══██║██║░░░░░██╔══╝░░██║╚████║░░░██║░░░ ██║░░██║██║░░██║██╔══╝░░░╚═══██╗██║╚████║░░░░░░██║░░░
░░░██║░░░██║░░██║███████╗███████╗██║░╚███║░░░██║░░░ ██████╔╝╚█████╔╝███████╗██████╔╝██║░╚███║░░░░░░██║░░░
░░░╚═╝░░░╚═╝░░╚═╝╚══════╝╚══════╝╚═╝░░╚══╝░░░╚═╝░░░ ╚═════╝░░╚════╝░╚══════╝╚═════╝░╚═╝░░╚══╝░░░░░░╚═╝░░░
░██╗░░░░░░░██╗░█████╗░██████╗░██╗░░██╗ ██╗░░██╗░█████╗░██████╗░██████╗░
░██║░░██╗░░██║██╔══██╗██╔══██╗██║░██╔╝ ██║░░██║██╔══██╗██╔══██╗██╔══██╗
░╚██╗████╗██╔╝██║░░██║██████╔╝█████═╝░ ███████║███████║██████╔╝██║░░██║
░░████╔═████║░██║░░██║██╔══██╗██╔═██╗░ ██╔══██║██╔══██║██╔══██╗██║░░██║
░░╚██╔╝░╚██╔╝░╚█████╔╝██║░░██║██║░╚██╗ ██║░░██║██║░░██║██║░░██║██████╔╝
░░░╚═╝░░░╚═╝░░░╚════╝░╚═╝░░╚═╝╚═╝░░╚═╝ ╚═╝░░╚═╝╚═╝░░╚═╝╚═╝░░╚═╝╚═════╝░
------------------------------------------------------------------------
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define loop(i, n) for (i = 0; i < n; i++)
#define loopf(i, a, n) for (i = a; i < n; i++)
#define loopb(i, a, n) for (i = a; i >= n; i--)
#define fore(name) for (auto it = name.begin(); it != name.end(); it++)
#define w(t) \
int t; \
cin >> t; \
while (t--)
#define pb(x) push_back(x)
#define in(y) insert(y)
#define make(x, y) make_pair(x, y)
#define LOAD_FACTOR_set(name) \
name.reserve(32768); \
name.max_load_factor(0.25);
#define LOAD_FACTOR_map(name) \
name.reserve(1024); \
name.max_load_factor(0.25);
#define lcm(a, b) ((a * b)) / (__gcd(a, b))
#define int long long int
#define REDBULL \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
#define mod 1000000007
#define endl "\n"
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
#define ordered_justset \
tree<int, null_type, less<int>, rb_tree_tag, \
tree_order_statistics_node_update>
struct HASH {
size_t operator()(const pair<int, int> &x) const {
return ((int)x.first) ^ (((int)x.second) << 32);
}
};
int32_t main() {
REDBULL
int n;
cin >> n;
int input;
int i;
std::vector<int> v;
loop(i, n) {
cin >> input;
v.pb(input);
}
i = 1;
auto f = v.begin();
while (f != v.end()) {
if (*f != i) {
f++;
} else {
// cout<<"amadr"<<endl;
i++;
f++;
}
}
i--;
// cout<<i<<endl;
if (i <= 0)
cout << -1 << endl;
else
cout << n - i << endl;
// cout<<i<<endl;
return 0;
}
| replace | 98 | 102 | 98 | 99 | TLE | |
p02832 | C++ | Runtime Error | // daddy's back in town, back to rolling the damn hash yoooo babeeeeees//
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define dbg(x) cout << #x << '=' << x << '\n';
#define ll long long
#define x first
#define y second
#define pi pair<int, int>
#define vi vector<int>
#define L nod << 1
#define R ((nod << 1) | 1)
const ll mod = 998244353;
#define int ll
int n, m, ans, cnt[100010];
int32_t main() {
ios_base ::sync_with_stdio(0);
cin.tie();
cout.tie();
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
cin >> n;
cnt[0] = 1;
for (int i = 1, x; i <= n; i++) {
cin >> x;
if (cnt[x - 1])
cnt[x] = 1;
}
for (int i = n; i >= 1; i--)
if (cnt[i])
return cout << n - i, 0;
cout << -1 << endl;
} | // daddy's back in town, back to rolling the damn hash yoooo babeeeeees//
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define dbg(x) cout << #x << '=' << x << '\n';
#define ll long long
#define x first
#define y second
#define pi pair<int, int>
#define vi vector<int>
#define L nod << 1
#define R ((nod << 1) | 1)
const ll mod = 998244353;
#define int ll
int n, m, ans, cnt[200010];
int32_t main() {
ios_base ::sync_with_stdio(0);
cin.tie();
cout.tie();
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
cin >> n;
cnt[0] = 1;
for (int i = 1, x; i <= n; i++) {
cin >> x;
if (cnt[x - 1])
cnt[x] = 1;
}
for (int i = n; i >= 1; i--)
if (cnt[i])
return cout << n - i, 0;
cout << -1 << endl;
} | replace | 14 | 15 | 14 | 15 | 0 | |
p02832 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define PB push_back
#define PII pair<int, int>
#define MP make_pair
#define all(x) x.begin(), x.end()
#define REP(x, y, z) for (int x = y; x <= z; x++)
#define REPP(x, y, z) for (int x = y; x >= z; x--)
#define F first
#define S second
#define MSET(x, y) memset(x, y, sizeof(x))
#define EB emplace_back
#define maxn 2005
// structure
// declaration
// functions
int main(void) {
#ifdef DBG
freopen("in.in", "r", stdin);
freopen("out.out", "w", stdout);
#endif
int n;
int arr[maxn];
// MSET(dp, 0);
scanf("%d", &n);
REP(i, 1, n) {
scanf("%d", &arr[i]);
// ans[i] = i;
}
int ans = 0;
int k = 1;
REP(i, 1, n) {
if (arr[i] == k) {
k++;
} else {
ans++;
}
}
if (ans == n)
printf("-1\n");
else
printf("%d\n", ans);
// REP(i, 1, n)
// {
// REP(j, 1, n)
// {
// if(arr[i] == ans[j])
// dp[i][j] = dp[i-1][j-1]+1;
// else
// {
// dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
// }
// }
// }
// printf("%d\n", n-dp[n][n]);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define PB push_back
#define PII pair<int, int>
#define MP make_pair
#define all(x) x.begin(), x.end()
#define REP(x, y, z) for (int x = y; x <= z; x++)
#define REPP(x, y, z) for (int x = y; x >= z; x--)
#define F first
#define S second
#define MSET(x, y) memset(x, y, sizeof(x))
#define EB emplace_back
#define maxn 200005
// structure
// declaration
// functions
int main(void) {
#ifdef DBG
freopen("in.in", "r", stdin);
freopen("out.out", "w", stdout);
#endif
int n;
int arr[maxn];
// MSET(dp, 0);
scanf("%d", &n);
REP(i, 1, n) {
scanf("%d", &arr[i]);
// ans[i] = i;
}
int ans = 0;
int k = 1;
REP(i, 1, n) {
if (arr[i] == k) {
k++;
} else {
ans++;
}
}
if (ans == n)
printf("-1\n");
else
printf("%d\n", ans);
// REP(i, 1, n)
// {
// REP(j, 1, n)
// {
// if(arr[i] == ans[j])
// dp[i][j] = dp[i-1][j-1]+1;
// else
// {
// dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
// }
// }
// }
// printf("%d\n", n-dp[n][n]);
return 0;
} | replace | 14 | 15 | 14 | 15 | 0 | |
p02832 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
ll num[20005];
int main() {
int n;
scanf("%d", &n);
ll ans = 0;
ll cnt = 1;
for (int i = 0; i < n; i++) {
scanf("%lld", &num[i]);
if (num[i] == cnt) {
cnt++;
} else {
ans++;
}
}
if (cnt == 1)
printf("-1");
else
printf("%lld", ans);
} | #include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
ll num[200005];
int main() {
int n;
scanf("%d", &n);
ll ans = 0;
ll cnt = 1;
for (int i = 0; i < n; i++) {
scanf("%lld", &num[i]);
if (num[i] == cnt) {
cnt++;
} else {
ans++;
}
}
if (cnt == 1)
printf("-1");
else
printf("%lld", ans);
} | replace | 5 | 6 | 5 | 6 | 0 | |
p02832 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
using namespace std;
long long n;
long long a[200010];
int now, ans, tmp1, tmp2;
vector<int> vans;
int saiki(int k) {
now = 1;
ans = k;
// if(n-k > tmp2) return 0;
FOR(i, k, n) {
if (a[i] != now && a[i] == 1) {
tmp1 = now;
tmp2 = ans;
saiki(i);
now = tmp1;
ans = tmp2;
ans++;
} else if (a[i] != now)
ans++;
else
now++;
}
vans.push_back(ans);
return 0;
}
int main() {
cin >> n;
REP(i, n) cin >> a[i];
saiki(0);
sort(vans.begin(), vans.end());
if (vans[0] == n)
vans[0] = -1;
cout << vans[0] << 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 - 1; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
using namespace std;
long long n;
long long a[200010];
int now, ans, tmp1, tmp2;
vector<int> vans;
int saiki(int k) {
now = 1;
ans = k;
// if(n-k > tmp2) return 0;
FOR(i, k, n) {
if (a[i] != now && a[i] == 1) {
tmp1 = now;
tmp2 = ans;
// saiki(i);
now = tmp1;
ans = tmp2;
ans++;
} else if (a[i] != now)
ans++;
else
now++;
}
vans.push_back(ans);
return 0;
}
int main() {
cin >> n;
REP(i, n) cin >> a[i];
saiki(0);
sort(vans.begin(), vans.end());
if (vans[0] == n)
vans[0] = -1;
cout << vans[0] << endl;
return 0;
} | replace | 19 | 20 | 19 | 20 | TLE | |
p02832 | Python | Time Limit Exceeded | N = int(input())
a = list(map(int, input().split()))
K = 0
ans = 0
for i in range(N):
k = a.pop(0)
if k == K + 1:
K = k
else:
ans += 1
if K == 0:
ans = -1
print(ans)
| N = int(input())
a = list(map(int, input().split()))
K = 0
ans = 0
for i in range(N):
k = a[i]
if k == K + 1:
K = k
else:
ans += 1
if K == 0:
ans = -1
print(ans)
| replace | 6 | 7 | 6 | 7 | TLE | |
p02832 | Python | Time Limit Exceeded | def t(a):
b = 0
n = 0
while n < len(a):
if a[n] == n + 1:
n += 1
else:
a.pop(n)
b += 1
return b if len(a) != 0 else -1
if __name__ == "__main__":
n = input()
a = list(map(int, input().split()))
print(t(a))
| def t(a):
b = [False] * len(a)
c = 1
for i, j in enumerate(a):
if j == c:
b[i] = True
c += 1
return b.count(False) if any(b) else -1
if __name__ == "__main__":
n = input()
a = list(map(int, input().split()))
print(t(a))
| replace | 1 | 10 | 1 | 9 | TLE | |
p02832 | Python | Time Limit Exceeded | N = int(input())
a = list(map(int, input().split()))
nm = 1
ans = 0
while a:
x = a.pop(0)
if x == nm:
nm += 1
else:
ans += 1
if ans == N:
print(-1)
else:
print(ans)
| N = int(input())
a = list(map(int, input().split()))
nm = 1
ans = 0
for i in range(N):
x = a[i]
if x == nm:
nm += 1
else:
ans += 1
if ans == N:
print(-1)
else:
print(ans)
| replace | 4 | 6 | 4 | 6 | TLE | |
p02832 | Python | Time Limit Exceeded | N = int(input())
a = list(map(int, input().split()))
left_max_num = 0
for i in range(1, N + 1):
if i in a:
left_max_num = i
a = a[a.index(i) + 1 :]
else:
break
if left_max_num != 0:
print(N - left_max_num)
else:
print(-1)
| N = int(input())
a = list(map(int, input().split()))
num = 0
for item in a:
if item == (num + 1):
num += 1
if num != 0:
print(N - num)
else:
print(-1)
| replace | 2 | 11 | 2 | 10 | TLE | |
p02832 | Python | Time Limit Exceeded | N = int(input())
a = list(map(int, input().split()))
ans = 0
i = 1
j = 0
while j < N:
if i not in a[j:]:
ans += N - j
break
nj = a[j:].index(i)
ans += nj - j
i += 1
j = nj + 1
if i == 1:
print(-1)
else:
print(ans)
| N = int(input())
a = list(map(int, input().split()))
ans = 0
x = 1
for i in a:
if i == x:
x += 1
else:
ans += 1
if x == 1:
print(-1)
else:
print(ans)
| replace | 3 | 14 | 3 | 10 | TLE | |
p02832 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all_map(itr, mp) for (auto itr = mp.begin(); itr != mp.end(); itr++)
#define ALL(a) (a).begin(), (a).end()
int main() {
ll n;
cin >> n;
vector<ll> a(n);
rep(i, n) cin >> a[i];
ll pos = 1;
rep(i, n) {
while (i + 1 != a[i]) {
a.erase(a.begin() + i);
if (a.empty()) {
cout << -1 << endl;
return 0;
}
}
if (i + 1 == a.size())
break;
}
cout << n - a.size() << endl;
} | #include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all_map(itr, mp) for (auto itr = mp.begin(); itr != mp.end(); itr++)
#define ALL(a) (a).begin(), (a).end()
int main() {
ll n;
cin >> n;
vector<ll> a(n);
rep(i, n) cin >> a[i];
ll pos = 0;
rep(i, n) if (a[i] == pos + 1) pos++;
if (pos == 0)
cout << -1 << endl;
else
cout << n - pos << endl;
} | replace | 23 | 36 | 23 | 30 | 0 | |
p02832 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define sint(a) scanf("%d", &a)
#define sint2(a, b) scanf("%d %d", &a, &b)
#define sll(a) scanf("%lld", &a)
#define sll2(a, b) scanf("%lld %lld", &a, &b)
const int maxn = 1e5 + 5;
int a[maxn], b[maxn];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++)
sint(a[i]);
int sum = 0;
int k = 1;
for (int i = 0; i < n; i++) {
if (a[i] == k)
k++;
else
sum++;
}
if (sum != n)
cout << sum << endl;
else
cout << -1 << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define sint(a) scanf("%d", &a)
#define sint2(a, b) scanf("%d %d", &a, &b)
#define sll(a) scanf("%lld", &a)
#define sll2(a, b) scanf("%lld %lld", &a, &b)
const int maxn = 1e6 + 5;
int a[maxn], b[maxn];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++)
sint(a[i]);
int sum = 0;
int k = 1;
for (int i = 0; i < n; i++) {
if (a[i] == k)
k++;
else
sum++;
}
if (sum != n)
cout << sum << endl;
else
cout << -1 << endl;
return 0;
}
| replace | 9 | 10 | 9 | 10 | 0 | |
p02832 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MOD 1000000007
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
if (a > b)
a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
if (a < b)
a = b;
}
ll helper(int cur, int tgt, int n, vector<vector<int>> &pos) {
if (cur > n)
return 0;
ll res = n - cur;
auto it = lower_bound(pos[tgt].begin(), pos[tgt].end(), cur);
while (it != pos[tgt].end()) {
res = min(res, *it - cur + helper(*it + 1, tgt + 1, n, pos));
it++;
}
return res;
}
int main() {
ll n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++)
cin >> a[i];
// check whether input array contains 1
bool flg = true;
for (int i = 0; i < n; i++)
if (a[i] == 1)
flg = false;
if (flg) {
cout << -1 << endl;
return 0;
}
vector<vector<int>> pos(n + 1);
for (int i = 0; i < n; i++)
pos[a[i]].push_back(i);
ll ans = helper(0, 1, n, pos);
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MOD 1000000007
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
if (a > b)
a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
if (a < b)
a = b;
}
ll helper(int cur, int tgt, int n, vector<vector<int>> &pos) {
if (cur >= n)
return 0;
if (tgt > n)
return 0;
ll res = n - cur;
auto it = lower_bound(pos[tgt].begin(), pos[tgt].end(), cur);
while (it != pos[tgt].end()) {
res = min(res, *it - cur + helper(*it + 1, tgt + 1, n, pos));
it++;
}
return res;
}
int main() {
ll n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++)
cin >> a[i];
// check whether input array contains 1
bool flg = true;
for (int i = 0; i < n; i++)
if (a[i] == 1)
flg = false;
if (flg) {
cout << -1 << endl;
return 0;
}
vector<vector<int>> pos(n + 1);
for (int i = 0; i < n; i++)
pos[a[i]].push_back(i);
ll ans = helper(0, 1, n, pos);
cout << ans << endl;
return 0;
} | replace | 15 | 16 | 15 | 18 | 0 | |
p02832 | C++ | Runtime Error | #include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
using ii = pair<ll, ll>;
ll a, b, c, i, j, l, m, n, ans, tmp, hoge, fuga, piyo;
unsigned long long k;
ll A[100000];
int main() {
cin >> n;
REP(i, n) cin >> A[i];
k = 1;
m = 0;
for (i = 0; i < n; i++) {
if (A[i] == k) {
k++;
} else {
m++;
}
}
if (k > 1 && m < n) {
printf("%llu", m);
} else {
printf("-1");
}
return 0;
} | #include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
using ii = pair<ll, ll>;
ll a, b, c, i, j, l, m, n, ans, tmp, hoge, fuga, piyo;
unsigned long long k;
ll A[200000];
int main() {
cin >> n;
REP(i, n) cin >> A[i];
k = 1;
m = 0;
for (i = 0; i < n; i++) {
if (A[i] == k) {
k++;
} else {
m++;
}
}
if (k > 1 && m < n) {
printf("%llu", m);
} else {
printf("-1");
}
return 0;
} | replace | 11 | 12 | 11 | 12 | 0 | |
p02832 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int f[1001];
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> f[i];
}
int ans = 0, sum = 1;
for (int i = 1; i <= n; i++) {
if (f[i] == sum) {
sum++;
ans++;
}
}
if (sum == 1) {
cout << -1;
} else
cout << n - ans;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int f[1000001];
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> f[i];
}
int ans = 0, sum = 1;
for (int i = 1; i <= n; i++) {
if (f[i] == sum) {
sum++;
ans++;
}
}
if (sum == 1) {
cout << -1;
} else
cout << n - ans;
return 0;
} | replace | 2 | 3 | 2 | 3 | 0 | |
p02833 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <tuple>
#include <vector>
#define PI 3.14159265359
#define INF 1e9
#define LINF 1e18
#define IMOD 1000000007
#define irep(i, n) for (int i = 0; i < n; i++)
#define irep2(i, a, n) for (int i = a; i <= n; i++)
#define lrep(i, n) for (long long i = 0; i < n; i++)
#define lrep2(i, a, n) for (long long i = a; i <= n; i++)
typedef long long ll;
typedef std::vector<char> v_char;
typedef std::vector<int> v_int;
typedef std::vector<std::vector<int>> v2_int;
typedef std::vector<ll> v_ll;
typedef std::vector<std::vector<ll>> v2_ll;
typedef std::vector<std::string> v_string;
typedef std::vector<std::vector<std::string>> v2_string;
typedef std::vector<bool> v_bool;
typedef std::vector<std::vector<bool>> v2_bool;
typedef std::pair<ll, ll> pll;
typedef std::pair<int, int> pii;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
using namespace std;
ll g(ll n, ll x) {
if (n == 1) {
return 0;
}
ll i = n / x;
return i + g(i, x);
}
ll g2(ll n, ll x) {
if (n % 2) {
return 0;
} else {
ll i = n / 2;
return 2 / x + g(i, x);
}
}
int main(void) {
ll n;
cin >> n;
// cout << min(g2(n, 2), g2(n, 5));
cout << g2(n, 5);
return 0;
}
/** atcodr **/
| #include <algorithm>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <tuple>
#include <vector>
#define PI 3.14159265359
#define INF 1e9
#define LINF 1e18
#define IMOD 1000000007
#define irep(i, n) for (int i = 0; i < n; i++)
#define irep2(i, a, n) for (int i = a; i <= n; i++)
#define lrep(i, n) for (long long i = 0; i < n; i++)
#define lrep2(i, a, n) for (long long i = a; i <= n; i++)
typedef long long ll;
typedef std::vector<char> v_char;
typedef std::vector<int> v_int;
typedef std::vector<std::vector<int>> v2_int;
typedef std::vector<ll> v_ll;
typedef std::vector<std::vector<ll>> v2_ll;
typedef std::vector<std::string> v_string;
typedef std::vector<std::vector<std::string>> v2_string;
typedef std::vector<bool> v_bool;
typedef std::vector<std::vector<bool>> v2_bool;
typedef std::pair<ll, ll> pll;
typedef std::pair<int, int> pii;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
using namespace std;
ll g(ll n, ll x) {
if (n == 0) {
return 0;
}
ll i = n / x;
return i + g(i, x);
}
ll g2(ll n, ll x) {
if (n % 2) {
return 0;
} else {
ll i = n / 2;
return 2 / x + g(i, x);
}
}
int main(void) {
ll n;
cin >> n;
// cout << min(g2(n, 2), g2(n, 5));
cout << g2(n, 5);
return 0;
}
/** atcodr **/
| replace | 40 | 41 | 40 | 41 | TLE | |
p02833 | C++ | Time Limit Exceeded | #include <algorithm>
#include <array>
#include <cassert>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define ll long long int
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repp(i, n) for (ll i = 1; i <= n; i++)
#define sort(v) sort((v).begin(), (v).end())
#define riverse(v) reverse((v).begin(), (v).end())
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
using vi = vector<int>;
using vs = vector<string>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
const ll MOD = 1e9 + 7;
vi alphabet = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
ll func(ll m) {
ll s = m / 10;
ll count5 = s;
while (s > 1) {
count5 += s / 5;
s = s / 5;
}
return count5;
}
int main() {
ll n;
cin >> n;
ll m = n;
ll count2 = 0;
ll count5 = 0;
ll count10 = 0;
if (n % 2 == 0) {
while (n % 2 == 0) {
count2 += n / 2;
n = n / 2;
}
}
if (m % 10 == 0 || m % 10 == 2 || m % 10 == 4 || m % 10 == 6 || m % 10 == 8) {
count5 += func(m);
} else if (m == 1 || m == 3) {
count5 = 0;
} else {
while (m % 5 != 0)
m -= 2;
ll k = m;
ll l = (m - 5) / 10;
count5 += l + 1;
m = m / 5;
while (m > 1) {
count5 += m / 5;
m = m / 5;
}
count5 -= func(k);
}
cout << min(count2, count5) << endl;
}
| #include <algorithm>
#include <array>
#include <cassert>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define ll long long int
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repp(i, n) for (ll i = 1; i <= n; i++)
#define sort(v) sort((v).begin(), (v).end())
#define riverse(v) reverse((v).begin(), (v).end())
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
using vi = vector<int>;
using vs = vector<string>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
const ll MOD = 1e9 + 7;
vi alphabet = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
ll func(ll m) {
ll s = m / 10;
ll count5 = s;
while (s > 1) {
count5 += s / 5;
s = s / 5;
}
return count5;
}
int main() {
ll n;
cin >> n;
ll m = n;
ll count2 = 0;
ll count5 = 0;
ll count10 = 0;
if (n % 2 == 0 && n > 0) {
while (n % 2 == 0) {
count2 += n / 2;
n = n / 2;
}
}
if (m % 10 == 0 || m % 10 == 2 || m % 10 == 4 || m % 10 == 6 || m % 10 == 8) {
count5 += func(m);
} else if (m == 1 || m == 3) {
count5 = 0;
} else {
while (m % 5 != 0)
m -= 2;
ll k = m;
ll l = (m - 5) / 10;
count5 += l + 1;
m = m / 5;
while (m > 1) {
count5 += m / 5;
m = m / 5;
}
count5 -= func(k);
}
cout << min(count2, count5) << endl;
}
| replace | 50 | 51 | 50 | 51 | TLE | |
p02833 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define REP(i, n) FOR(i, 0, n - 1)
#define NREP(i, n) FOR(i, 1, n)
using ll = long long;
using pii = pair<int, int>;
using piii = pair<pii, pii>;
const int dx[4] = {0, -1, 1, 0};
const int dy[4] = {-1, 0, 0, 1};
const int INF = 1e9 + 7;
int gcd(int x, int y) {
if (x < y)
swap(x, y);
if (y == 0)
return x;
return gcd(y, x % y);
}
template <class T1, class T2> void chmin(T1 &a, T2 b) {
if (a > b)
a = b;
}
template <class T1, class T2> void chmax(T1 &a, T2 b) {
if (a < b)
a = b;
}
template <class T> void Add(T &a, const T &b, const T &mod = 1000000007) {
int val = ((a % mod) + (b % mod)) % mod;
if (val < 0) {
val += mod;
}
a = val;
}
////////////////////////////////////////
int main() {
ll N;
cin >> N;
if (N % 2 == 1)
cout << 0 << endl;
else {
ll ans = 0;
ll n = N;
if (N % 10 == 0) {
while (N % 10 == 0) {
ans++;
N /= 5;
}
}
ll key = 10;
while (key <= n - 2) {
ans += (n - 2) / key;
key *= 5;
}
cout << ans << endl;
}
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define REP(i, n) FOR(i, 0, n - 1)
#define NREP(i, n) FOR(i, 1, n)
using ll = long long;
using pii = pair<int, int>;
using piii = pair<pii, pii>;
const int dx[4] = {0, -1, 1, 0};
const int dy[4] = {-1, 0, 0, 1};
const int INF = 1e9 + 7;
int gcd(int x, int y) {
if (x < y)
swap(x, y);
if (y == 0)
return x;
return gcd(y, x % y);
}
template <class T1, class T2> void chmin(T1 &a, T2 b) {
if (a > b)
a = b;
}
template <class T1, class T2> void chmax(T1 &a, T2 b) {
if (a < b)
a = b;
}
template <class T> void Add(T &a, const T &b, const T &mod = 1000000007) {
int val = ((a % mod) + (b % mod)) % mod;
if (val < 0) {
val += mod;
}
a = val;
}
////////////////////////////////////////
int main() {
ll N;
cin >> N;
if (N == 0) {
cout << 0 << endl;
return 0;
}
if (N % 2 == 1)
cout << 0 << endl;
else {
ll ans = 0;
ll n = N;
if (N % 10 == 0) {
while (N % 10 == 0) {
ans++;
N /= 5;
}
}
ll key = 10;
while (key <= n - 2) {
ans += (n - 2) / key;
key *= 5;
}
cout << ans << endl;
}
return 0;
} | insert | 56 | 56 | 56 | 60 | TLE | |
p02833 | C++ | Runtime Error | #include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = (0); i < (n); ++i)
#define rng(x) (x).begin(), (x).end()
#define rrng(x) (x).rbegin(), (x).rend()
#define show(x) cout << #x << " = " << (x) << endl;
#define show2(x, y) \
cout << #x << " = " << (x) << ", " << #y << " = " << (y) << endl;
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define bn(x) ((1 << x) - 1)
#define dup(x, y) (((x) + (y)-1) / (y))
#define uni(x) x.erase(unique(rng(x)), x.end())
#define SP << " " <<
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using pii = pair<int, int>;
using tiii = tuple<int, int, int>;
const int mod = 1000000007;
const double EPS = 1e-9;
const int INF = 1 << 30;
const ll INFLL = 1LL << 60;
// cout << fixed << setprecision(10);
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
ll g1(ll n, int p) {
if (n == 0)
return 0;
return n / p + g1(n / p, p);
}
ll g2(ll n, int p) {
if (n % 2 == 1)
return g1(n, p) - g2(n - 1, p);
ll res = g1(n / 2, p);
if (p == 2)
res += 2 / n;
return res;
}
int main() {
ll n;
cin >> n;
ll ans = min(g2(n, 2), g2(n, 5));
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = (0); i < (n); ++i)
#define rng(x) (x).begin(), (x).end()
#define rrng(x) (x).rbegin(), (x).rend()
#define show(x) cout << #x << " = " << (x) << endl;
#define show2(x, y) \
cout << #x << " = " << (x) << ", " << #y << " = " << (y) << endl;
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define bn(x) ((1 << x) - 1)
#define dup(x, y) (((x) + (y)-1) / (y))
#define uni(x) x.erase(unique(rng(x)), x.end())
#define SP << " " <<
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using pii = pair<int, int>;
using tiii = tuple<int, int, int>;
const int mod = 1000000007;
const double EPS = 1e-9;
const int INF = 1 << 30;
const ll INFLL = 1LL << 60;
// cout << fixed << setprecision(10);
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
ll g1(ll n, int p) {
if (n == 0)
return 0;
return n / p + g1(n / p, p);
}
ll g2(ll n, int p) {
if (n % 2 == 1)
return g1(n, p) - g2(n - 1, p);
ll res = g1(n / 2, p);
if (p == 2)
res += n / 2;
return res;
}
int main() {
ll n;
cin >> n;
ll ans = min(g2(n, 2), g2(n, 5));
cout << ans << endl;
return 0;
} | replace | 55 | 56 | 55 | 56 | 0 | |
p02833 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
int main() {
ll N;
cin >> N;
if (N % 2 == 1) {
puts(0);
return 0;
} else {
ll x = 5;
ll ans = 0;
while (N >= x) {
ans += N / x / 2;
x *= 5;
}
cout << ans << endl;
}
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
int main() {
ll N;
cin >> N;
if (N % 2 == 1) {
cout << 0 << endl;
return 0;
} else {
ll x = 5;
ll ans = 0;
while (N >= x) {
ans += N / x / 2;
x *= 5;
}
cout << ans << endl;
}
return 0;
} | replace | 9 | 10 | 9 | 10 | 0 | |
p02833 | C++ | Time Limit Exceeded | #include <iostream>
using namespace std;
long long int solve(long long int n) {
long long int ans = 0;
if (n % 2 != 0) {
return 0;
}
for (int i = 10; i <= n; i *= 5) {
ans += n / i;
}
return ans;
}
int main() {
long long int n, ans;
cin >> n;
ans = solve(n);
cout << ans << endl;
return 0;
} | #include <iostream>
using namespace std;
long long int solve(long long int n) {
long long int ans = 0;
if (n % 2 != 0) {
return 0;
}
for (long long int i = 10; i <= n; i *= 5) {
ans += n / i;
}
return ans;
}
int main() {
long long int n, ans;
cin >> n;
ans = solve(n);
cout << ans << endl;
return 0;
} | replace | 10 | 11 | 10 | 11 | TLE | |
p02833 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
long long n, i, nr, x, aux;
int main() {
cin >> n;
if (n % (long long)2 == 1) {
cout << 0;
return 1;
}
x = 2;
while (x * 5 <= n) {
x = x * 5;
nr = nr + n / x;
}
cout << nr;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
long long n, i, nr, x, aux;
int main() {
cin >> n;
if (n % 2 == 0)
for (i = 5; i <= n; i = i * 5)
nr = nr + n / (i * 2);
cout << nr;
return 0;
}
| replace | 5 | 14 | 5 | 8 | 0 | |
p02833 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef string str;
#define f(i, begin, end) \
for (auto i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); \
i += 1 - 2 * ((begin) > (end)))
#define trav(v) for (auto &x : v)
#define dis(v) trav(v) cout << x << " ";
#define deb(x) cerr << #x << ":" << x << "\n"
#define debv(v) trav(v) cerr << x << " ";
#define all(v) v.begin(), v.end()
#define nl cout << "\n"
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
ll t = 1;
// cin >> t;
while (t--) {
ll i, j, k;
ll n, ans = 0;
cin >> n;
k = 10;
if (!(n & 1))
while (n / k > 0) {
ans += (n / k);
k *= 5;
}
cout << ans << "\n";
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef string str;
#define f(i, begin, end) \
for (auto i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); \
i += 1 - 2 * ((begin) > (end)))
#define trav(v) for (auto &x : v)
#define dis(v) trav(v) cout << x << " ";
#define deb(x) cerr << #x << ":" << x << "\n"
#define debv(v) trav(v) cerr << x << " ";
#define all(v) v.begin(), v.end()
#define nl cout << "\n"
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
ll t = 1;
// cin >> t;
while (t--) {
ll i, j, k;
ll n, ans = 0;
cin >> n;
k = 10;
if (!(n & 1))
while (n / k > 0) {
ans += (n / k);
k *= 5;
}
cout << ans << "\n";
}
return 0;
} | delete | 18 | 22 | 18 | 18 | 0 | |
p02833 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define repr(i, n) for (int i = (n)-1; 0 <= i; --i)
#define each(e, v) for (auto &&e : (v))
#define all(v) begin(v), end(v)
#define dump(x) cerr << #x << ": " << (x) << endl
using namespace std;
using vint = vector<int>;
using ll = long long;
using vll = vector<ll>;
template <class T> void chmin(T &a, const T &b) { a = min(a, b); }
template <class T> void chmax(T &a, const T &b) { a = max(a, b); }
void yes() {
cout << "Yes" << endl;
exit(0);
}
void no() {
cout << "No" << endl;
exit(0);
}
ll cnt2(ll n) {
if (n % 2LL != 0)
return 0;
return (n / 2LL) + cnt2(n / 2LL);
}
ll cnt5(ll n) {
if (n < 5LL)
return 0;
return (n / 10LL) + cnt5(n / 5LL);
}
int main() {
ll n;
cin >> n;
cout << min(cnt2(n), cnt5(n)) << endl;
return 0;
}
| #include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define repr(i, n) for (int i = (n)-1; 0 <= i; --i)
#define each(e, v) for (auto &&e : (v))
#define all(v) begin(v), end(v)
#define dump(x) cerr << #x << ": " << (x) << endl
using namespace std;
using vint = vector<int>;
using ll = long long;
using vll = vector<ll>;
template <class T> void chmin(T &a, const T &b) { a = min(a, b); }
template <class T> void chmax(T &a, const T &b) { a = max(a, b); }
void yes() {
cout << "Yes" << endl;
exit(0);
}
void no() {
cout << "No" << endl;
exit(0);
}
ll cnt2(ll n) {
if (n == 0LL)
return 0;
if (n % 2LL != 0)
return 0;
return (n / 2LL) + cnt2(n / 2LL);
}
ll cnt5(ll n) {
if (n < 5LL)
return 0;
return (n / 10LL) + cnt5(n / 5LL);
}
int main() {
ll n;
cin >> n;
cout << min(cnt2(n), cnt5(n)) << endl;
return 0;
}
| insert | 23 | 23 | 23 | 25 | TLE | |
p02833 | C++ | Runtime Error | //
// Created by Mahmoud Rashad on 11/27/19.
//
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> ii;
long long count_P_in_nfact(long long n, long long p) {
long long res = 0;
while (n) {
n /= p;
res += n;
}
return res;
}
long long count_5_in_nfact(long long n, long long p) {
long long res = 0;
while (n) {
n /= p;
res += n / 2;
}
return res;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
// freopen("output.out", "w", stdout);
#endif
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0), cout.precision(10),
cout << fixed;
long long n;
cin >> n;
if (n & 1) {
cout << 0 << '\n';
return 0;
}
cout << min(count_5_in_nfact(n, 5), count_P_in_nfact(n, 2)) << '\n';
return 0;
} | //
// Created by Mahmoud Rashad on 11/27/19.
//
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> ii;
long long count_P_in_nfact(long long n, long long p) {
long long res = 0;
while (n) {
n /= p;
res += n;
}
return res;
}
long long count_5_in_nfact(long long n, long long p) {
long long res = 0;
while (n) {
n /= p;
res += n / 2;
}
return res;
}
int main() {
#ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.out", "w", stdout);
#endif
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0), cout.precision(10),
cout << fixed;
long long n;
cin >> n;
if (n & 1) {
cout << 0 << '\n';
return 0;
}
cout << min(count_5_in_nfact(n, 5), count_P_in_nfact(n, 2)) << '\n';
return 0;
} | replace | 29 | 30 | 29 | 30 | 0 | |
p02833 | C++ | Time Limit Exceeded | #include <iostream>
#include <vector>
using namespace std;
long cntfive(long n) {
long ans = 0;
while (n % 5 == 0) {
ans++;
n /= 5;
}
return ans;
}
int main(void) {
long n;
cin >> n;
long ans = 0;
if (n % 2 == 0) {
for (long i = n; i >= 10; i -= 2) {
ans += cntfive(i);
}
}
cout << ans << endl;
} | #include <iostream>
#include <vector>
using namespace std;
long cntfive(long n) {
long ans = 0;
while (n % 5 == 0) {
ans++;
n /= 5;
}
return ans;
}
int main(void) {
long n;
cin >> n;
long ans = 0;
if (n % 2 == 0) {
for (long i = 10; i <= n; i *= 5) {
ans += n / i;
}
}
cout << ans << endl;
} | replace | 16 | 18 | 16 | 18 | TLE | |
p02833 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
long long ans = 0;
int main() {
long long n;
cin >> n;
if (n % 2 != 0)
cout << "0";
else {
for (int i = 10; i <= n; i *= 5) {
ans += (n / i);
}
cout << ans;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
long long ans = 0;
int main() {
long long n;
cin >> n;
if (n % 2 != 0)
cout << "0";
else {
for (long long i = 10; i <= n; i *= 5) {
ans += (n / i);
}
cout << ans;
}
return 0;
} | replace | 9 | 10 | 9 | 10 | TLE | |
p02833 | Python | Runtime Error | import math
def main():
N = int(input())
if N % 2 == 0:
B = N // 2
n = math.floor(math.log(B) / math.log(5))
ans = 0
for i in range(1, n + 1):
ans += B // (5**i)
else:
ans = 0
print(int(ans))
main()
| import math
def main():
N = int(input())
if N == 0 or N == 1:
ans = 0
elif N % 2 == 0:
B = N // 2
n = math.floor(math.log(B) / math.log(5))
ans = 0
for i in range(1, n + 1):
ans += B // (5**i)
else:
ans = 0
print(int(ans))
main()
| replace | 6 | 7 | 6 | 10 | 0 | |
p02833 | Python | Runtime Error | import math
N = int(input())
ans = 0
if N % 2 != 0:
pass
else:
max_pow_five = int(math.log(N, 5))
for i in range(1, max_pow_five + 1):
ans += N // ((5**i) * 2)
print(ans)
| import math
N = int(input())
ans = 0
if N % 2 != 0 or N == 0:
pass
else:
max_pow_five = int(math.log(N, 5))
for i in range(1, max_pow_five + 1):
ans += N // ((5**i) * 2)
print(ans)
| replace | 6 | 7 | 6 | 7 | 0 | |
p02833 | Python | Runtime Error | import math
def run(N):
ans = 0
if N % 2 == 0:
for i in range(int(math.log(N, 5))):
ans += N // (10 * (5**i))
return ans
def main():
N = int(input())
print(run(N))
if __name__ == "__main__":
main()
| import math
def run(N):
if N == 0:
return 0
ans = 0
if N % 2 == 0:
for i in range(int(math.log(N, 5))):
ans += N // (10 * (5**i))
return ans
def main():
N = int(input())
print(run(N))
if __name__ == "__main__":
main()
| insert | 4 | 4 | 4 | 6 | 0 | |
p02833 | Python | Runtime Error | N = int(input())
if N % 2 == 1:
print(0)
else:
ans = 0
for i in range(1, 30):
ans += (N // 2) // (5**i)
print(ans)
| N = int(input())
if N % 2 == 1:
print(0)
else:
ans = 0
for i in range(1, 30):
ans += (N // 2) // (5**i)
print(ans)
| replace | 7 | 9 | 7 | 8 | 0 | |
p02833 | Python | Runtime Error | import math
N = int(input())
if N % 2 == 1:
print(0)
else:
div10 = N // 10
sum_val = div10
count_5 = math.floor(math.log(div10, 5))
for i in range(count_5):
sum_val += div10 // pow(5, i + 1)
print(sum_val)
| import math
N = int(input())
if N % 2 == 1:
print(0)
else:
div10 = N // 10
sum_val = div10
if sum_val > 0:
count_5 = math.floor(math.log(div10, 5))
for i in range(count_5):
sum_val += div10 // pow(5, i + 1)
print(sum_val)
| replace | 8 | 11 | 8 | 12 | 0 | |
p02833 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
long long a;
cin >> a;
if (a % 2 != 0) {
cout << 0 << endl;
} else {
long long c = 0;
long long d = 5;
long long j = a;
while (j / d > 0) {
if (j % (d * 2) == 0) {
c += j / (d * 2);
d *= 5;
j = a;
} else {
j -= 2;
}
}
cout << min(a / 2, c) << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
int main() {
long long a;
cin >> a;
if (a % 2 != 0) {
cout << 0 << endl;
} else {
long long c = 0;
long long d = 5;
while (a / d > 0) {
c += a / (d * 2);
d *= 5;
}
cout << min(a / 2, c) << endl;
}
} | replace | 11 | 20 | 11 | 14 | TLE | |
p02833 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
// ifstream cin("lineup.in");
// ofstream cout("lineup.out");
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
int main() {
IOS;
long long n;
cin >> n;
if (n % 2) {
cout << 0 << endl;
} else {
n /= 2;
long long ans = 0;
while (n != 1) {
ans += n / 5;
n /= 5;
}
cout << ans << endl;
}
}
| #include <bits/stdc++.h>
using namespace std;
// ifstream cin("lineup.in");
// ofstream cout("lineup.out");
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
int main() {
IOS;
long long n;
cin >> n;
if (n % 2) {
cout << 0 << endl;
} else {
n /= 2;
long long ans = 0;
while (n > 1) {
ans += n / 5;
n /= 5;
}
cout << ans << endl;
}
}
| replace | 15 | 16 | 15 | 16 | TLE | |
p02833 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Vec = vector<int>;
ll five_count(ll n) {
ll c = 0;
ll f = 5;
for (; f <= n; f *= 5) {
if (!(n % f))
c++;
else
break;
}
return (c);
}
int main(void) {
ll n;
cin >> n;
if (n % 2) {
cout << 0 << endl;
return (0);
}
n /= 2;
ll c = 0;
for (ll i = 1; i <= n; i++)
c += five_count(i);
cout << c << endl;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Vec = vector<int>;
ll five_count(ll n) {
ll c = 0;
ll f = 5;
for (; f <= n; f *= 5) {
if (!(n % f))
c++;
else
break;
}
return (c);
}
int main(void) {
ll n;
cin >> n;
if (n % 2) {
cout << 0 << endl;
return (0);
}
n /= 2;
ll c = 0;
ll five = 1;
while (n >= 5) {
c += n / 5;
n /= 5;
five++;
}
cout << c << endl;
}
| replace | 27 | 29 | 27 | 35 | TLE | |
p02833 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
ll f_2(ll n) {
if (n == 1)
return 0;
return f_2(n / 2) + n / 2;
}
ll f_5(ll n) {
if (n <= 1)
return 0;
return f_5(n / 5) + n / 5;
}
signed main(void) {
ll n, ans;
cin >> n;
ans = 0;
if (n % 2 == 0) {
ans = min(f_2(n), f_5(n / 2));
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
ll f_2(ll n) {
if (n == 1)
return 0;
return f_2(n / 2) + n / 2;
}
ll f_5(ll n) {
if (n <= 1)
return 0;
return f_5(n / 5) + n / 5;
}
signed main(void) {
ll n, ans;
cin >> n;
ans = 0;
if (n % 2 == 0 && n != 0) {
ans = min(f_2(n), f_5(n / 2));
}
cout << ans << endl;
return 0;
}
| replace | 21 | 22 | 21 | 22 | TLE | |
p02833 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define ll long long
#define all(x) (x).begin(), (x).end()
#define int long long
signed main() {
int n;
cin >> n;
// 奇数なら約数に2がないので0
if (n % 2 == 1) {
cout << 0 << endl;
return 0;
}
// 偶数なら明らかに約数は5の数が少ないので5の数を調べれば良い
int res = 0;
while (n > 10) {
n -= 10;
res++;
}
cout << res << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define ll long long
#define all(x) (x).begin(), (x).end()
#define int long long
signed main() {
int n;
cin >> n;
// 奇数なら約数に2がないので0
if (n % 2 == 1) {
cout << 0 << endl;
return 0;
}
// 偶数なら明らかに約数は5の数が少ないので5の数を調べれば良い
int res = 0;
int divide = 10;
while (divide <= n) {
res += n / divide;
divide *= 5;
}
cout << res << endl;
}
| replace | 18 | 21 | 18 | 22 | TLE | |
p02833 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back
template <class T> bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
ll ans = 0;
ll x = 5;
unsigned long long doublefactorial(long long a) {
while (x >= a) {
ans++;
x *= x;
}
if (a == 0 || a == 1)
return 1;
return a * doublefactorial(a - 2);
}
int main() {
ll n;
cin >> n;
ll x = 5;
ll sum = 0;
ll sum2 = 0;
if (n % 2 == 0) {
while (x <= n / 2) {
sum += n / 2 / x;
x *= 5;
}
cout << sum << endl;
return 0;
} else {
while (x <= n + 1) {
sum += (n + 1) / x;
x *= 5;
}
x = 5;
while (x <= n / 2) {
sum += n / 2 / x;
x *= 5;
}
cout << sum / sum2 << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back
template <class T> bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
ll ans = 0;
ll x = 5;
unsigned long long doublefactorial(long long a) {
while (x >= a) {
ans++;
x *= x;
}
if (a == 0 || a == 1)
return 1;
return a * doublefactorial(a - 2);
}
int main() {
ll n;
cin >> n;
ll x = 5;
ll sum = 0;
ll sum2 = 0;
if (n % 2 == 0) {
while (x <= n / 2) {
sum += n / 2 / x;
x *= 5;
}
cout << sum << endl;
return 0;
} else {
cout << 0 << endl;
return 0;
}
return 0;
} | replace | 59 | 69 | 59 | 61 | 0 | |
p02833 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;
using vvi = vector<vector<int>>;
using vvll = vector<vector<ll>>;
const ll mod = 5e18;
int main() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(15);
ll N;
cin >> N;
ll ans = 0;
if (N % 2) {
cout << ans << endl;
return 0;
}
ll jyuubai = 10;
ll now = N;
if (N == 1e18) {
while (jyuubai <= N) {
ans += N / jyuubai;
if (jyuubai == 1e18)
break;
jyuubai *= 5;
}
} else {
while (jyuubai <= N) {
ans += N / jyuubai;
if (jyuubai <= 1e17)
jyuubai *= 5;
}
}
cout << ans << endl;
return 0;
}
| #include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <string>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;
using vvi = vector<vector<int>>;
using vvll = vector<vector<ll>>;
const ll mod = 5e18;
int main() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(15);
ll N;
cin >> N;
ll ans = 0;
if (N % 2) {
cout << ans << endl;
return 0;
}
ll jyuubai = 10;
ll now = N;
if (N == 1e18) {
while (jyuubai <= N) {
ans += N / jyuubai;
if (jyuubai == 1e18)
break;
jyuubai *= 5;
}
} else {
while (jyuubai <= N) {
ans += N / jyuubai;
jyuubai *= 5;
}
}
cout << ans << endl;
return 0;
}
| replace | 46 | 48 | 46 | 47 | TLE | |
p02833 | C++ | Runtime Error | // C
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <unordered_map>
#include <unordered_set>
#endif
// ここまで標準include
using namespace std;
int main() {
long long n;
cin >> n;
long long ans = 0;
if (n % 2 == 1) {
cout << 0 << endl;
} else {
for (long long i = 0; i < 1000000; i++) {
long long l;
l = n / (10 * (long long)pow(5, i));
ans += l;
}
cout << ans << endl;
}
}
| // C
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <unordered_map>
#include <unordered_set>
#endif
// ここまで標準include
using namespace std;
int main() {
long long n;
cin >> n;
long long ans = 0;
if (n % 2 == 1) {
cout << 0 << endl;
} else {
for (long long i = 0; i < 1000000; i++) {
long long l;
l = n / (long long)(10 * pow(5, i));
ans += l;
}
cout << ans << endl;
}
}
| replace | 101 | 102 | 101 | 102 | -8 | |
p02833 | C++ | Time Limit Exceeded | #include <iostream>
using namespace std;
long long ans = 0;
long long a;
int main() {
cin >> a;
if (a % 2 == 1) {
cout << 0;
return 0;
} else {
while (a >= 0) {
a /= 10;
ans += a;
}
cout << ans;
return 0;
}
} | #include <iostream>
using namespace std;
long long ans = 0;
long long a;
int main() {
cin >> a;
if (a % 2 == 1) {
cout << 0;
return 0;
} else {
a /= 10;
ans += a;
while (a > 0) {
a /= 5;
ans += a;
}
cout << ans;
return 0;
}
} | replace | 10 | 12 | 10 | 14 | TLE | |
p02833 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define For(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) For(i, 0, n)
// Write From This Line
int main() {
ll n;
cin >> n;
if (n % 2) {
cout << 0 << endl;
return 0;
}
ll ans = 0;
ll five = 10;
while (five > 0) {
ans += (n / five);
five *= 5;
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define For(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) For(i, 0, n)
// Write From This Line
int main() {
ll n;
cin >> n;
if (n % 2) {
cout << 0 << endl;
return 0;
}
ll ans = 0;
n /= 2;
ll five = 5;
while (n) {
ans += n / five;
n /= 5;
}
cout << ans << endl;
}
| replace | 17 | 21 | 17 | 22 | TLE | |
p02833 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int64_t N, count;
cin >> N;
count = 0;
if (N % 2 != 0) {
cout << count << endl;
} else {
for (int i = 10; i <= N; i = i * 10) {
count += N / i;
}
cout << count << endl;
}
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
int64_t N, count;
cin >> N;
count = 0;
if (N % 2 != 0) {
cout << count << endl;
} else {
for (int64_t i = 5; i <= N; i = i * 5) {
count += N / (i * 2);
}
cout << count << endl;
}
}
| replace | 10 | 12 | 10 | 12 | 0 | |
p02833 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
long long n, res = 0;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
if (n % 2) {
cout << 0;
return 0;
}
n /= 2;
for (int i = 5; i <= n; i *= 5)
res += n / i;
cout << res;
}
| #include <bits/stdc++.h>
using namespace std;
long long n, res = 0;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
if (n % 2) {
cout << 0;
return 0;
}
n /= 2;
for (long long i = 5; i <= n; i *= 5)
res += n / i;
cout << res;
}
| replace | 16 | 17 | 16 | 17 | TLE | |
p02833 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll INF = 1LL << 60;
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
ll n;
cin >> n;
if (n % 2) {
cout << 0 << endl;
return 0;
}
n /= 2;
ll most = 1;
while (most <= n) {
most *= 5;
}
most /= 5;
ll res = 0;
while (most != 1) {
// cout << n/most << " " << n << " " << most << endl;
res += n / most;
most /= 5;
}
cout << res << endl;
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll INF = 1LL << 60;
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
ll n;
cin >> n;
if (n % 2 || n == 0) {
cout << 0 << endl;
return 0;
}
n /= 2;
ll most = 1;
while (most <= n) {
most *= 5;
}
most /= 5;
ll res = 0;
while (most != 1) {
// cout << n/most << " " << n << " " << most << endl;
res += n / most;
most /= 5;
}
cout << res << endl;
return 0;
} | replace | 14 | 15 | 14 | 15 | 0 | |
p02833 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define sort(A) sort(A.begin(), A.end())
#define reverse(A) reverse(A.begin(), A.end())
typedef long long ll;
typedef pair<int, int> P;
int main() {
ll n;
cin >> n;
if (n % 2 == 1) {
cout << 0 << endl;
return 0;
}
ll ans = 0;
for (int i = 1; pow(5, i) <= n; i++) {
long long l = (long double)ans + n / (pow((long double)5, i) * 2);
long long r =
(long double)ans + n / (long long)(pow((long double)5, i) * 2);
assert(l == r);
ans += n / (long long)(pow((long double)5, i) * 2);
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define sort(A) sort(A.begin(), A.end())
#define reverse(A) reverse(A.begin(), A.end())
typedef long long ll;
typedef pair<int, int> P;
int main() {
ll n;
cin >> n;
if (n % 2 == 1) {
cout << 0 << endl;
return 0;
}
ll ans = 0;
for (int i = 1; pow(5, i) <= n; i++) {
long long l = n / (pow((long double)5, i) * 2);
long long r = n / (long long)(pow((long double)5, i) * 2);
assert(l == r);
ans += n / (long long)(pow((long double)5, i) * 2);
}
cout << ans << endl;
}
| replace | 19 | 22 | 19 | 21 | 0 | |
p02833 | C++ | Time Limit Exceeded | // x<<y=x*2^y,x>>y=x/2^y
// 1<<31 gives overflow
// use ll() for using an integer in self-built fn
//(x&-x) bitwise and of (x and its 2's complement) returns (last set bit)
// eg if x=1010 then it will return 0010
// careful dont print empty container(run time error)
// v.erase O(n)
// use ("\n") instead of endl as endl flushes the output buffer
// every time so takes more time than \n (TLE)
// stoll() and to_string((less than 1e19))
// INT_MAX is approx 3e10
// For sets use set_name.lower_bound(x)(strictly O(logn))
// NOT lb(all(s),x)(O(logn) for random access (eg vector) but for sets it is
// O(n));
#include <bits/stdc++.h>
typedef long long int ll;
#define ull unsigned long long int
#define lld long double
#define endl "\n"
#define fi first
#define sec second
#define sp setprecision
#define lb lower_bound
#define ub upper_bound
#define For(i, a, b) for (long long int i = (a); i <= (b); i++)
#define Forr(i, a, b) for (long long int i = (a); i >= (b); i--)
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define vll vector<ll>
#define vlld vector<lld>
#define vi vector<int>
#define vch vector<char>
#define sll set<ll>
#define sch set<ch>
#define vpll vector<pair<ll, ll>>
#define vpii vector<pair<int, int>>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define mll map<ll, ll>
#define mcll map<char, ll>
#define sz(container) ll((container).size())
#define fill(a, b) memset(a, b, sizeof(a))
#define fast_io ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
using namespace std;
lld pi = 3.1415926535897932;
const ll MOD = 1e9 + 7;
const ll dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
const ll dxx[8] = {-1, -1, 0, 1, 1, 1, 0, -1},
dyy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
/**************************************************
//FENWICK TREE
ll bit[100005];
void update(ll idx, ll val, ll n)
{
for (; idx <= n; idx += idx&-idx)
bit[idx] += val;
}
ll query(ll idx)
{
ll sum = 0;
for (; idx>0; idx-=idx&-idx)
sum += bit[idx];
return sum;
}
*****************************************************/
ll lcm(ll a, ll b) {
ll g = __gcd(a, b);
return a / g * b;
}
ll binpow(ll a, ll b, ll m) {
a %= m;
ll res = 1;
while (b > 0) {
if (b & 1)
res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
bool isPrime(ll n) {
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
// This is checked so that we can skip
// middle five numbers in below loop
if (n % 2 == 0 || n % 3 == 0)
return false;
for (ll i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
/******************************************
ll phi(ll n)
{
ll result = n;
For(i,0,sqrt(n))
{
if (n % i == 0)
{
while (n % i == 0)
n /= i;
result -= result / i;
}
}
if (n > 1)
result -= result / n;
return result;
}
************************************/
/***************************************
//to check whether a string is whole number(without leading 0s)
bool isNumber(string s)
{
if(s=="0") return true;
if(s[0]=='0') return false;
For(i,0,sz(s)-1)
{
if (isdigit(s[i]) == false) return false;
}
return true;
}
******************************/
/******************************
queue<ll> q;
//p:parent d:depth
vll used(200002),p(200002),d(200002);
vector<vll>adj(200002);
void bfs(ll s)
{
q.push(s);
p[s] = -1;
used[s] = 1;
while (!q.empty())
{
ll v = q.front();
q.pop();
for (ll u : adj[v])
{
if (!used[u])
{
used[u] = 1;
q.push(u);
d[u] = d[v] + 1;
p[u] = v;
}
}
}
}
*****************************/
/***********************************
vector<vll >adj(100005);
vll subsz(100005);
void dfs(ll v,ll x)
{
subsz[v]=1;
for(ll u : adj[v])
{
if (u!=x) dfs(u,v),subsz[v]+=subsz[u];
}
}
************************************/
/**********************************
ll used[1005][1005];
char s[1005][1005];
void dfs(ll x,ll y)
{
used[x][y]=1;
For(i,0,3)
{
ll nx=x+dx[i],ny=y+dy[i];
if(used[nx][ny]==0&&s[nx][ny]!='#') dfs(nx,ny);
}
}
*********/
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
fast_io;
ll T = 1; // cin>>T;
For(i, 1, T) {
ll n;
cin >> n;
ll c5 = 0, c2 = 0;
for (ll j = n; j >= 1; j -= 2) {
ll temp = j;
while (temp % 2 == 0) {
temp /= 2;
c2++;
}
while (temp % 5 == 0) {
temp /= 5;
c5++;
}
}
cout << min(c5, c2);
}
} | // x<<y=x*2^y,x>>y=x/2^y
// 1<<31 gives overflow
// use ll() for using an integer in self-built fn
//(x&-x) bitwise and of (x and its 2's complement) returns (last set bit)
// eg if x=1010 then it will return 0010
// careful dont print empty container(run time error)
// v.erase O(n)
// use ("\n") instead of endl as endl flushes the output buffer
// every time so takes more time than \n (TLE)
// stoll() and to_string((less than 1e19))
// INT_MAX is approx 3e10
// For sets use set_name.lower_bound(x)(strictly O(logn))
// NOT lb(all(s),x)(O(logn) for random access (eg vector) but for sets it is
// O(n));
#include <bits/stdc++.h>
typedef long long int ll;
#define ull unsigned long long int
#define lld long double
#define endl "\n"
#define fi first
#define sec second
#define sp setprecision
#define lb lower_bound
#define ub upper_bound
#define For(i, a, b) for (long long int i = (a); i <= (b); i++)
#define Forr(i, a, b) for (long long int i = (a); i >= (b); i--)
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define vll vector<ll>
#define vlld vector<lld>
#define vi vector<int>
#define vch vector<char>
#define sll set<ll>
#define sch set<ch>
#define vpll vector<pair<ll, ll>>
#define vpii vector<pair<int, int>>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define mll map<ll, ll>
#define mcll map<char, ll>
#define sz(container) ll((container).size())
#define fill(a, b) memset(a, b, sizeof(a))
#define fast_io ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
using namespace std;
lld pi = 3.1415926535897932;
const ll MOD = 1e9 + 7;
const ll dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
const ll dxx[8] = {-1, -1, 0, 1, 1, 1, 0, -1},
dyy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
/**************************************************
//FENWICK TREE
ll bit[100005];
void update(ll idx, ll val, ll n)
{
for (; idx <= n; idx += idx&-idx)
bit[idx] += val;
}
ll query(ll idx)
{
ll sum = 0;
for (; idx>0; idx-=idx&-idx)
sum += bit[idx];
return sum;
}
*****************************************************/
ll lcm(ll a, ll b) {
ll g = __gcd(a, b);
return a / g * b;
}
ll binpow(ll a, ll b, ll m) {
a %= m;
ll res = 1;
while (b > 0) {
if (b & 1)
res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
bool isPrime(ll n) {
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
// This is checked so that we can skip
// middle five numbers in below loop
if (n % 2 == 0 || n % 3 == 0)
return false;
for (ll i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
/******************************************
ll phi(ll n)
{
ll result = n;
For(i,0,sqrt(n))
{
if (n % i == 0)
{
while (n % i == 0)
n /= i;
result -= result / i;
}
}
if (n > 1)
result -= result / n;
return result;
}
************************************/
/***************************************
//to check whether a string is whole number(without leading 0s)
bool isNumber(string s)
{
if(s=="0") return true;
if(s[0]=='0') return false;
For(i,0,sz(s)-1)
{
if (isdigit(s[i]) == false) return false;
}
return true;
}
******************************/
/******************************
queue<ll> q;
//p:parent d:depth
vll used(200002),p(200002),d(200002);
vector<vll>adj(200002);
void bfs(ll s)
{
q.push(s);
p[s] = -1;
used[s] = 1;
while (!q.empty())
{
ll v = q.front();
q.pop();
for (ll u : adj[v])
{
if (!used[u])
{
used[u] = 1;
q.push(u);
d[u] = d[v] + 1;
p[u] = v;
}
}
}
}
*****************************/
/***********************************
vector<vll >adj(100005);
vll subsz(100005);
void dfs(ll v,ll x)
{
subsz[v]=1;
for(ll u : adj[v])
{
if (u!=x) dfs(u,v),subsz[v]+=subsz[u];
}
}
************************************/
/**********************************
ll used[1005][1005];
char s[1005][1005];
void dfs(ll x,ll y)
{
used[x][y]=1;
For(i,0,3)
{
ll nx=x+dx[i],ny=y+dy[i];
if(used[nx][ny]==0&&s[nx][ny]!='#') dfs(nx,ny);
}
}
*********/
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
fast_io;
ll T = 1; // cin>>T;
For(i, 1, T) {
ll n;
cin >> n;
ll ans = 0;
ll temp = 5 * 2;
while (1) {
if ((n / temp) == 0)
break;
ans += (n / temp);
temp *= 5;
}
if (n % 2)
ans = 0;
cout << ans;
}
} | replace | 197 | 210 | 197 | 208 | TLE | |
p02834 | Python | Runtime Error | n, u, v = map(int, input().split())
# よりたかはしくんが早く辿り着ける葉のうち、青木君から最も遠いで
u -= 1
v -= 1
g = [[] for _ in range(n)]
for _ in range(n - 1):
a, b = map(lambda x: int(x) - 1, input().split())
g[a].append(b)
g[b].append(a)
INF = 1 << 30
t = [INF] * n
a = [INF] * n
t[u] = 0
a[v] = 0
def dfs(v, p, d):
for nv in g[v]:
if nv == p:
continue
if d[nv] == INF:
d[nv] = d[v] + 1
dfs(nv, v, d)
dfs(u, -1, t)
dfs(v, -1, a)
ans = 0
for i in range(n):
if t[i] < a[i]:
ans = max(ans, a[i] - 1)
print(ans)
| import sys
sys.setrecursionlimit(10**6)
n, u, v = map(int, input().split())
# よりたかはしくんが早く辿り着ける葉のうち、青木君から最も遠いで
u -= 1
v -= 1
g = [[] for _ in range(n)]
for _ in range(n - 1):
a, b = map(lambda x: int(x) - 1, input().split())
g[a].append(b)
g[b].append(a)
INF = 1 << 30
t = [INF] * n
a = [INF] * n
t[u] = 0
a[v] = 0
def dfs(v, p, d):
for nv in g[v]:
if nv == p:
continue
if d[nv] == INF:
d[nv] = d[v] + 1
dfs(nv, v, d)
dfs(u, -1, t)
dfs(v, -1, a)
ans = 0
for i in range(n):
if t[i] < a[i]:
ans = max(ans, a[i] - 1)
print(ans)
| insert | 0 | 0 | 0 | 4 | 0 | |
p02834 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const int MOD = 1e9 + 7;
vector<int> g[100100];
int dist[100100] = {};
int ans[100100] = {};
int tak, ao;
void dfs(int u, int pa, int d) {
for (auto v : g[u]) {
if (v == pa)
continue;
dfs(v, u, d + 1);
}
dist[u] = d;
}
void dfs2(int u, int pa, int d) {
for (auto v : g[u]) {
if (v == u)
continue;
if (d + 1 >= dist[v]) {
continue;
} else {
dfs2(v, u, d + 1);
}
}
ans[u] = dist[u];
}
int main() {
int N;
cin >> N >> tak >> ao;
tak--;
ao--;
for (int i = 0; i < N - 1; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
g[a].push_back(b);
g[b].push_back(a);
}
dfs(ao, -1, 0);
dfs2(tak, -1, 0);
int f_a = 0;
for (int i = 0; i < N; i++) {
f_a = max(ans[i], f_a);
}
cout << f_a - 1 << endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const int MOD = 1e9 + 7;
vector<int> g[100100];
int dist[100100] = {};
int ans[100100] = {};
int tak, ao;
void dfs(int u, int pa, int d) {
for (auto v : g[u]) {
if (v == pa)
continue;
dfs(v, u, d + 1);
}
dist[u] = d;
}
void dfs2(int u, int pa, int d) {
for (auto v : g[u]) {
if (v == pa)
continue;
if (d + 1 >= dist[v]) {
continue;
} else {
dfs2(v, u, d + 1);
}
}
ans[u] = dist[u];
}
int main() {
int N;
cin >> N >> tak >> ao;
tak--;
ao--;
for (int i = 0; i < N - 1; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
g[a].push_back(b);
g[b].push_back(a);
}
dfs(ao, -1, 0);
dfs2(tak, -1, 0);
int f_a = 0;
for (int i = 0; i < N; i++) {
f_a = max(ans[i], f_a);
}
cout << f_a - 1 << endl;
} | replace | 26 | 27 | 26 | 27 | TLE | |
p02834 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long
#define ff first
#define ss second
#define fast \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define pb push_back
#define mp make_pair
#define inf 2000000009
#define mod 1000000007
using namespace std;
vector<ll> adj[100009];
ll d[100009][2];
void fun(ll x, bool f) {
for (ll y : adj[x])
if (d[y][f] == -1) {
d[y][f] = d[x][f] + 1;
fun(y, f);
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
fast;
ll n, u, v;
cin >> n >> u >> v;
u--, v--;
ll x, y;
for (ll i = 0; i < n - 1; i++) {
ll x, y;
cin >> x >> y;
x--;
y--;
adj[x].pb(y);
adj[y].pb(x);
}
memset(d, -1, sizeof(d));
d[u][0] = d[v][1] = 0;
fun(u, 0);
fun(v, 1);
ll ans = 0;
for (ll i = 0; i < n; i++)
if (d[i][0] < d[i][1])
ans = max(ans, d[i][1]);
cout << ans - 1 << endl;
} | #include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long
#define ff first
#define ss second
#define fast \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define pb push_back
#define mp make_pair
#define inf 2000000009
#define mod 1000000007
using namespace std;
vector<ll> adj[100009];
ll d[100009][2];
void fun(ll x, bool f) {
for (ll y : adj[x])
if (d[y][f] == -1) {
d[y][f] = d[x][f] + 1;
fun(y, f);
}
}
int main() {
fast;
ll n, u, v;
cin >> n >> u >> v;
u--, v--;
ll x, y;
for (ll i = 0; i < n - 1; i++) {
ll x, y;
cin >> x >> y;
x--;
y--;
adj[x].pb(y);
adj[y].pb(x);
}
memset(d, -1, sizeof(d));
d[u][0] = d[v][1] = 0;
fun(u, 0);
fun(v, 1);
ll ans = 0;
for (ll i = 0; i < n; i++)
if (d[i][0] < d[i][1])
ans = max(ans, d[i][1]);
cout << ans - 1 << endl;
} | delete | 25 | 30 | 25 | 25 | -11 | |
p02834 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
#define INF (ll)1e18 + 50
#define MAXN (int)1e5 + 50
#define fi first
#define se second
#define MOD 1000000007
#define all(v) ((v).begin()), ((v).end())
#define A 911382323
#define B 972663749
vector<vector<int>> G;
int dist[101010][2];
void dfs(int cur, int pre, int d, int id) {
dist[cur][id] = d;
for (const auto next : G[cur]) {
if (next != pre) {
dfs(next, cur, d + 1, id);
}
}
}
int main() {
int n, u, v;
cin >> n >> u >> v;
u--;
v--;
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
--a, --b;
G[a].push_back(b);
G[b].push_back(a);
}
dfs(u, u, 0, 0);
dfs(v, v, 0, 1);
int ans = 0;
for (int i = 0; i < n; i++)
if (dist[i][0] < dist[i][1])
ans = max(ans, dist[i][1] - 1);
cout << ans;
} | #include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
#define INF (ll)1e18 + 50
#define MAXN (int)1e5 + 50
#define fi first
#define se second
#define MOD 1000000007
#define all(v) ((v).begin()), ((v).end())
#define A 911382323
#define B 972663749
vector<int> G[101010];
int dist[101010][2];
void dfs(int cur, int pre, int d, int id) {
dist[cur][id] = d;
for (const auto next : G[cur]) {
if (next != pre) {
dfs(next, cur, d + 1, id);
}
}
}
int main() {
int n, u, v;
cin >> n >> u >> v;
u--;
v--;
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
--a, --b;
G[a].push_back(b);
G[b].push_back(a);
}
dfs(u, u, 0, 0);
dfs(v, v, 0, 1);
int ans = 0;
for (int i = 0; i < n; i++)
if (dist[i][0] < dist[i][1])
ans = max(ans, dist[i][1] - 1);
cout << ans;
} | replace | 13 | 14 | 13 | 14 | -11 | |
p02834 | C++ | Time Limit Exceeded | #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define rep(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define rep1(i, n) for (ll(i) = 1; (i) <= (n); (i)++)
#define rrep(i, n) for (ll(i) = (n)-1; (i) >= 0; (i)--)
#define rrep1(i, n) for (ll(i) = (n); (i) >= 1; (i)--)
const ll INF = 1145141919;
const ll MOD = 1000000007;
template <class T> void chmax(T &a, const T &b) {
if (a < b) {
a = b;
}
}
template <class T> void chmin(T &a, const T &b) {
if (a > b) {
a = b;
}
}
ll N, u, v;
vector<ll> path[101010];
ll U[101010], V[101010], parent[101010];
ll V2[101010];
ll ha[101010];
void dep() {
rep1(i, N) U[i] = INF;
rep1(i, N) V[i] = INF;
rep1(i, N) parent[i] = -1;
queue<ll> Q;
priority_queue<P> PQ;
U[u] = 0;
Q.push(u);
for (; !Q.empty(); Q.pop()) {
ll idx = Q.front();
rep(i, path[idx].size()) {
ll to = path[idx][i];
if (U[to] < INF)
continue;
U[to] = U[idx] + 1;
Q.push(to);
}
}
V[v] = 0;
Q.push(v);
for (; !Q.empty(); Q.pop()) {
ll idx = Q.front();
ll flg = 1;
rep(i, path[idx].size()) {
ll to = path[idx][i];
if (V[to] < INF)
continue;
V[to] = V[idx] + 1;
Q.push(to);
parent[to] = idx;
flg = 0;
}
if (flg) {
PQ.push(P(V[idx], idx));
ha[idx] = 1;
}
}
for (; !PQ.empty();) {
ll d = PQ.top().first;
ll idx = PQ.top().second;
PQ.pop();
if (V2[idx])
continue;
V2[idx] = d;
ll to = parent[idx];
if (to < 0)
continue;
if (V2[to])
continue;
if (V[to] > V[idx])
continue;
PQ.push(P(V2[idx], to));
}
}
int main() {
cin >> N >> u >> v;
rep1(i, N - 1) {
ll a, b;
cin >> a >> b;
path[a].push_back(b);
path[b].push_back(a);
}
dep();
// rep1(i, N)cout << i << " : " << U[i] << ", " << V[i] << ", " << V2[i] <<
// endl;
ll cnt = 0;
ll idx = u;
for (;;) {
ll p = parent[idx];
if (ha[idx] && V[p] <= cnt)
break;
if (ha[idx] && V[p] <= cnt + 1) {
cnt++;
break;
}
if (ha[idx]) {
idx = p;
cnt++;
continue;
}
if (V[p] > cnt + 1) {
idx = p;
cnt++;
continue;
}
ll tmp = -1;
rep(i, path[idx].size()) {
ll to = path[idx][i];
if (V2[to] == V2[idx])
tmp = to;
}
idx = tmp;
cnt++;
}
cout << cnt << endl;
return 0;
} | #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define rep(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define rep1(i, n) for (ll(i) = 1; (i) <= (n); (i)++)
#define rrep(i, n) for (ll(i) = (n)-1; (i) >= 0; (i)--)
#define rrep1(i, n) for (ll(i) = (n); (i) >= 1; (i)--)
const ll INF = 1145141919;
const ll MOD = 1000000007;
template <class T> void chmax(T &a, const T &b) {
if (a < b) {
a = b;
}
}
template <class T> void chmin(T &a, const T &b) {
if (a > b) {
a = b;
}
}
ll N, u, v;
vector<ll> path[101010];
ll U[101010], V[101010], parent[101010];
ll V2[101010];
ll ha[101010];
void dep() {
rep1(i, N) U[i] = INF;
rep1(i, N) V[i] = INF;
rep1(i, N) parent[i] = -1;
queue<ll> Q;
priority_queue<P> PQ;
U[u] = 0;
Q.push(u);
for (; !Q.empty(); Q.pop()) {
ll idx = Q.front();
rep(i, path[idx].size()) {
ll to = path[idx][i];
if (U[to] < INF)
continue;
U[to] = U[idx] + 1;
Q.push(to);
}
}
V[v] = 0;
Q.push(v);
for (; !Q.empty(); Q.pop()) {
ll idx = Q.front();
ll flg = 1;
rep(i, path[idx].size()) {
ll to = path[idx][i];
if (V[to] < INF)
continue;
V[to] = V[idx] + 1;
Q.push(to);
parent[to] = idx;
flg = 0;
}
if (flg) {
PQ.push(P(V[idx], idx));
ha[idx] = 1;
}
}
for (; !PQ.empty();) {
ll d = PQ.top().first;
ll idx = PQ.top().second;
PQ.pop();
if (V2[idx])
continue;
V2[idx] = d;
ll to = parent[idx];
if (to < 0)
continue;
if (V2[to])
continue;
if (V[to] > V[idx])
continue;
PQ.push(P(V2[idx], to));
}
}
int main() {
cin >> N >> u >> v;
rep1(i, N - 1) {
ll a, b;
cin >> a >> b;
path[a].push_back(b);
path[b].push_back(a);
}
dep();
// rep1(i, N)cout << i << " : " << U[i] << ", " << V[i] << ", " << V2[i] <<
// endl;
ll cnt = 0;
ll idx = u;
for (;;) {
ll p = parent[idx];
if (ha[idx] && V[p] <= cnt)
break;
if (ha[idx] && V[p] <= cnt + 1) {
cnt++;
break;
}
if (ha[idx]) {
idx = p;
cnt++;
continue;
}
if (V[p] > cnt + 1) {
idx = p;
cnt++;
continue;
}
ll tmp = -1;
rep(i, path[idx].size()) {
ll to = path[idx][i];
if (V2[to] == V2[idx] && V[to] > V[idx])
tmp = to;
}
idx = tmp;
cnt++;
}
cout << cnt << endl;
return 0;
} | replace | 118 | 119 | 118 | 119 | TLE | |
p02834 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using Graph = vector<vector<int>>;
const int INF = 1e9 + 1;
const ll LLINF = 1e18;
const int MOD = 1e9 + 7;
int main() {
int N, u, v;
cin >> N >> u >> v;
u--, v--;
Graph G(N, vector<int>(N));
for (int i = 0; i < N - 1; ++i) {
int a, b;
cin >> a >> b;
a--, b--;
G[a].push_back(b);
G[b].push_back(a);
}
queue<int> que;
vector<int> dist1(N, -1);
que.push(u);
dist1[u] = 0;
while (!que.empty()) {
int k = que.front();
que.pop();
for (auto nk : G[k]) {
if (dist1[nk] != -1)
continue;
dist1[nk] = dist1[k] + 1;
que.push(nk);
}
}
vector<int> dist2(N, -1);
que.push(v);
dist2[v] = 0;
int mdist = 0, lv, dif = 0;
while (!que.empty()) {
int k = que.front();
que.pop();
for (auto nk : G[k]) {
if (dist2[nk] != -1)
continue;
dist2[nk] = dist2[k] + 1;
que.push(nk);
}
}
int ans = 0;
for (int i = 0; i < N; ++i)
if (dist1[i] < dist2[i])
ans = max(dist2[i] - 1, ans);
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using Graph = vector<vector<int>>;
const int INF = 1e9 + 1;
const ll LLINF = 1e18;
const int MOD = 1e9 + 7;
int main() {
int N, u, v;
cin >> N >> u >> v;
u--, v--;
Graph G(N);
for (int i = 0; i < N - 1; ++i) {
int a, b;
cin >> a >> b;
a--, b--;
G[a].push_back(b);
G[b].push_back(a);
}
queue<int> que;
vector<int> dist1(N, -1);
que.push(u);
dist1[u] = 0;
while (!que.empty()) {
int k = que.front();
que.pop();
for (auto nk : G[k]) {
if (dist1[nk] != -1)
continue;
dist1[nk] = dist1[k] + 1;
que.push(nk);
}
}
vector<int> dist2(N, -1);
que.push(v);
dist2[v] = 0;
int mdist = 0, lv, dif = 0;
while (!que.empty()) {
int k = que.front();
que.pop();
for (auto nk : G[k]) {
if (dist2[nk] != -1)
continue;
dist2[nk] = dist2[k] + 1;
que.push(nk);
}
}
int ans = 0;
for (int i = 0; i < N; ++i)
if (dist1[i] < dist2[i])
ans = max(dist2[i] - 1, ans);
cout << ans << endl;
return 0;
}
| replace | 14 | 15 | 14 | 15 | 0 | |
p02834 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define per(i, n) for (ll i = n - 1; i >= 0; i--)
#define REP(i, n) for (ll i = 1; i < n; i++)
#define PER(i, n) for (ll i = n; i >= 1; i--)
#define sz(x) int(x.size())
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define YesorNo(a) printf(a ? "Yes\n" : "No\n")
#define endl '\n'
#define fi first
#define se second
using ll = long long;
using P = pair<int, int>;
using Pl = pair<ll, ll>;
using vi = vector<int>;
using vii = vector<vi>;
using vl = vector<ll>;
using vll = vector<vl>;
using vs = vector<string>;
using vb = vector<bool>;
using vc = vector<char>;
using vcc = vector<vc>;
using vP = vector<P>;
using vPP = vector<vP>;
using vPl = vector<Pl>;
const int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
const int inf = (1 << 30) - 1;
const ll infll = (1LL << 62) - 1;
ll ceil(const ll &a, const ll &b) { return ((a) + (b)-1) / b; }
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;
}
void dfs(int v, vi &res, const vii tree) {
for (int nv : tree[v]) {
if (res[nv] != inf)
continue;
res[nv] = res[v] + 1;
dfs(nv, res, tree);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, u, v;
cin >> n >> u >> v;
u--;
v--;
vii tree(n);
rep(i, n - 1) {
int a, b;
cin >> a >> b;
a--;
b--;
tree[a].push_back(b);
tree[b].push_back(a);
}
vi t(n, inf), a(n, inf);
t[u] = 0, a[v] = 0;
dfs(u, t, tree);
dfs(v, a, tree);
int ans = 0;
rep(i, n) {
if (t[i] + 1 > a[i])
continue;
int d = a[i];
if (sz(tree[i]) == 1)
d--;
chmax(ans, d);
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define per(i, n) for (ll i = n - 1; i >= 0; i--)
#define REP(i, n) for (ll i = 1; i < n; i++)
#define PER(i, n) for (ll i = n; i >= 1; i--)
#define sz(x) int(x.size())
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define YesorNo(a) printf(a ? "Yes\n" : "No\n")
#define endl '\n'
#define fi first
#define se second
using ll = long long;
using P = pair<int, int>;
using Pl = pair<ll, ll>;
using vi = vector<int>;
using vii = vector<vi>;
using vl = vector<ll>;
using vll = vector<vl>;
using vs = vector<string>;
using vb = vector<bool>;
using vc = vector<char>;
using vcc = vector<vc>;
using vP = vector<P>;
using vPP = vector<vP>;
using vPl = vector<Pl>;
const int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
const int inf = (1 << 30) - 1;
const ll infll = (1LL << 62) - 1;
ll ceil(const ll &a, const ll &b) { return ((a) + (b)-1) / b; }
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;
}
void dfs(int v, vi &res, const vii &tree) {
for (int nv : tree[v]) {
if (res[nv] != inf)
continue;
res[nv] = res[v] + 1;
dfs(nv, res, tree);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, u, v;
cin >> n >> u >> v;
u--;
v--;
vii tree(n);
rep(i, n - 1) {
int a, b;
cin >> a >> b;
a--;
b--;
tree[a].push_back(b);
tree[b].push_back(a);
}
vi t(n, inf), a(n, inf);
t[u] = 0, a[v] = 0;
dfs(u, t, tree);
dfs(v, a, tree);
int ans = 0;
rep(i, n) {
if (t[i] + 1 > a[i])
continue;
int d = a[i];
if (sz(tree[i]) == 1)
d--;
chmax(ans, d);
}
cout << ans << endl;
} | replace | 48 | 49 | 48 | 49 | TLE | |
p02834 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
vector<int> to[10005];
int n;
vector<int> dist;
void dfs(int v, int d = 0, int p = -1) {
dist[v] = d;
for (int u : to[v]) {
if (u == p)
continue;
dfs(u, d + 1, v);
}
}
vector<int> calcDist(int s) {
dist = vector<int>(n);
dfs(s);
return dist;
}
int main() {
cin >> n;
int s, t;
cin >> s >> t;
--s;
--t;
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
--a;
--b;
to[a].push_back(b);
to[b].push_back(a);
}
vector<int> distS = calcDist(s);
vector<int> distT = calcDist(t);
int mx = 0;
for (int i = 0; i < n; i++) {
if (distS[i] < distT[i]) {
mx = max(mx, distT[i]);
}
}
int ans = mx - 1;
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
vector<int> to[100005];
int n;
vector<int> dist;
void dfs(int v, int d = 0, int p = -1) {
dist[v] = d;
for (int u : to[v]) {
if (u == p)
continue;
dfs(u, d + 1, v);
}
}
vector<int> calcDist(int s) {
dist = vector<int>(n);
dfs(s);
return dist;
}
int main() {
cin >> n;
int s, t;
cin >> s >> t;
--s;
--t;
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
--a;
--b;
to[a].push_back(b);
to[b].push_back(a);
}
vector<int> distS = calcDist(s);
vector<int> distT = calcDist(t);
int mx = 0;
for (int i = 0; i < n; i++) {
if (distS[i] < distT[i]) {
mx = max(mx, distT[i]);
}
}
int ans = mx - 1;
cout << ans << endl;
return 0;
}
| replace | 3 | 4 | 3 | 4 | 0 | |
p02834 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const ll maxs = 1e5;
#define pb push_back
ll n, a, b;
std::vector<ll> Tree[maxs];
ll dist[2][maxs];
void go(ll s, ll p, ll ind) {
dist[ind][s] = dist[ind][p] + 1;
for (auto d : Tree[s]) {
if (d != p)
go(d, s, ind);
}
}
int main() {
cin >> n >> a >> b;
for (ll i = 0; i < n - 1; i++) {
ll s, d;
cin >> s >> d;
Tree[s].pb(d);
Tree[d].pb(s);
}
dist[0][0] = dist[1][0] = -1;
go(a, 0, 0);
go(b, 0, 1);
ll ans = 0;
for (ll i = 1; i <= n; i++) {
if (dist[0][i] < dist[1][i]) {
ans = max(ans, dist[1][i] - 1);
}
}
std::cout << ans << std::endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const ll maxs = 1e5 + 5;
#define pb push_back
ll n, a, b;
std::vector<ll> Tree[maxs];
ll dist[2][maxs];
void go(ll s, ll p, ll ind) {
dist[ind][s] = dist[ind][p] + 1;
for (auto d : Tree[s]) {
if (d != p)
go(d, s, ind);
}
}
int main() {
cin >> n >> a >> b;
for (ll i = 0; i < n - 1; i++) {
ll s, d;
cin >> s >> d;
Tree[s].pb(d);
Tree[d].pb(s);
}
dist[0][0] = dist[1][0] = -1;
go(a, 0, 0);
go(b, 0, 1);
ll ans = 0;
for (ll i = 1; i <= n; i++) {
if (dist[0][i] < dist[1][i]) {
ans = max(ans, dist[1][i] - 1);
}
}
std::cout << ans << std::endl;
return 0;
} | replace | 4 | 5 | 4 | 5 | 0 | |
p02834 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 110;
const ll INF = 2e9 + 10;
struct edge {
int to, next;
} G[maxn << 1];
int tot, head[maxn];
void addEdge(int u, int v) {
G[tot] = {v, head[u]};
head[u] = tot++;
};
int n, s, t, vis[2][maxn];
void dfs(int u, int fa, int x) {
for (int i = head[u]; i != -1; i = G[i].next)
if (G[i].to != fa)
vis[x][G[i].to] = vis[x][u] + 1, dfs(G[i].to, u, x);
}
int main() {
#ifdef Irene
freopen("in.txt", "r", stdin);
#endif // Irene
ios_base::sync_with_stdio(false);
cin >> n >> s >> t;
int u, v;
for (int i = 1; i <= n; i++)
head[i] = -1;
for (int i = 1; i < n; i++) {
cin >> u >> v;
addEdge(u, v);
addEdge(v, u);
}
dfs(s, -1, 0);
dfs(t, -1, 1);
// for(int i = 1;i <= n;i++)cout << vis[0][i] << " " << vis[1][i] << "\n";
int ans = 0, mi = INF;
for (int i = 1; i <= n; i++) {
if (vis[0][i] < vis[1][i])
ans = max(ans, vis[1][i] - 1);
else if (vis[0][i] == vis[1][i])
mi = min(mi, vis[0][i]);
}
cout << max(ans, (mi == INF ? 0 : mi)) << "\n";
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 5;
const ll INF = 2e9 + 10;
struct edge {
int to, next;
} G[maxn << 1];
int tot, head[maxn];
void addEdge(int u, int v) {
G[tot] = {v, head[u]};
head[u] = tot++;
};
int n, s, t, vis[2][maxn];
void dfs(int u, int fa, int x) {
for (int i = head[u]; i != -1; i = G[i].next)
if (G[i].to != fa)
vis[x][G[i].to] = vis[x][u] + 1, dfs(G[i].to, u, x);
}
int main() {
#ifdef Irene
freopen("in.txt", "r", stdin);
#endif // Irene
ios_base::sync_with_stdio(false);
cin >> n >> s >> t;
int u, v;
for (int i = 1; i <= n; i++)
head[i] = -1;
for (int i = 1; i < n; i++) {
cin >> u >> v;
addEdge(u, v);
addEdge(v, u);
}
dfs(s, -1, 0);
dfs(t, -1, 1);
// for(int i = 1;i <= n;i++)cout << vis[0][i] << " " << vis[1][i] << "\n";
int ans = 0, mi = INF;
for (int i = 1; i <= n; i++) {
if (vis[0][i] < vis[1][i])
ans = max(ans, vis[1][i] - 1);
else if (vis[0][i] == vis[1][i])
mi = min(mi, vis[0][i]);
}
cout << max(ans, (mi == INF ? 0 : mi)) << "\n";
}
| replace | 3 | 4 | 3 | 4 | 0 | |
p02834 | C++ | Runtime Error | #include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <utility>
#include <vector>
#define fir first
#define sec second
#define sz(s) (s).size();
#define pb push_back
#define get(n) scanf("%d", &n);
#define gets(s) \
string s; \
cin >> (s);
#define prfi(n) printf("%d", &n);
#define prfd(n) printf("%lf", &n);
#define All(s) (s).begin(), (s).end()
#define rep(i, j, k) for (int(i) = (j); (i) <= (k); (i)++)
#define rep0(i, j) for (int(i) = 0; (i) < (j); (i)++)
#define repdown(i, j, k) for (int(i) = (j); (i) >= (k); (i)--)
#define repdown0(i, j) for (int(i) = (j); i >= 0; i--)
#define vfor(c, v) for (auto(c) : v)
#define dump(x) std::cout << #x << " = " << (x) << std::endl;
#define debug(x) \
cout << #x << " = " << (x) << " (L" << __LINE__ << ")" \
<< " " << __FILE__ << endl;
using ll = long long;
using pii = std::pair<int, int>;
using vi = std::vector<int>;
using vvi = std::vector<vi>;
using vll = std::vector<ll>;
using vvll = std::vector<vll>;
using vd = std::vector<double>;
using vvd = std::vector<vd>;
using qi = std::queue<int>;
using vpii = std::vector<std::pair<int, int>>;
using namespace std;
const int Mod = (1e9) + 7;
const int max_n = 3 * (1e5) + 1;
const int max_m = 83 * (1e5) + 1;
const int INF = 10241024;
const ll INFL = (1ll << 62) - 1;
const int dx[] = {-1, 0, 0, 1};
const int dy[] = {0, -1, 1, 0};
//_____________________________________Templates_________________________________________//
template <class T1, class T2> inline void chmin(T1 &a, T2 b) {
if (a > b)
a = b;
}
template <class T1, class T2> inline void chmax(T1 &a, T2 b) {
if (a < b)
a = b;
}
// mainly use for dynamic prog
template <class T1, class T2> void update(T1 &a, T2 b) {
a += b;
if (a > Mod)
a %= Mod;
}
inline void IN(void) { return; }
template <typename First, typename... Rest>
void IN(First &first, Rest &...rest) {
cin >> first;
IN(rest...);
return;
}
inline void OUT(void) {
cout << "\n";
return;
}
template <typename First, typename... Rest>
void OUT(First first, Rest... rest) {
cout << first << " ";
OUT(rest...);
return;
}
template <class T> struct segt {
int n;
T def;
vector<T> dat;
function<T(T, T)> operation;
function<T(T, T)> update;
segt(size_t _n, T _def, function<T(T, T)> _operation,
function<T(T, T)> _update)
: def(_def), operation(_operation), update(_update) {
n = 1;
while (n < _n) {
n *= 2;
}
dat = vector<T>(2 * n - 1, _def);
}
T _query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return def;
if (a <= r && l <= b)
return dat[k];
else {
T vl = _query(a, b, k * 2 + 1, l, (l + r) / 2);
T vr = _query(a, b, k * 2 + 2, (l + r) / 2, r);
return operation(vl, vr);
}
}
T query(int a, int b) { return _query(a, b, 0, 0, n); }
void change(int i, T value) {
i += n - 1;
dat[i] = update(dat[i], value);
while (i > 0) {
i = (i - 1) / 2;
dat[i] = operation(dat[i * 2 + 1], dat[i * 2 + 2]);
}
}
T operator[](int i) { return dat[i + n - 1]; }
};
//_____________________array calc____________________________________//
/*
vvi mul(vvi &A, vvi &B){
vvi C(A.size(), vi(B.size()))
rep(i,A.size()){
rep(j,B.size()){
rep(k,B[0].size()){
C[i][j] = (C[i][j] A[i][k] + B[k][j]) % Mod;
}
}
}
return C;
}
vvi pow(vvi A, ll n){
vvi B(A.size(), vi(A.size()));
rep(i=0;i<A.size();i++){
B[i][i] = 1;
}
while (n >0){
if (n & 1) B = mul(B, A);
A = mul(A, A);
n = n >> 1;
}
return B;
}
*/
//_____________________Bynary Indexed Tree __________________________//
/*
struct BIT{
#define bs (1 << 20)
int bit[bs];
int sum (int i){
int s = 0;
while(i > 0){
s += bit[i];
i -= i & -i;
}
return s;
}
void add(int i, int x){
while(i <= n){
bit[i] += x;
i += i & -i;
}
}
}
*/
//____________________RMQ____________________________//
/*struct RMQ{
#define s (1 << 20)
ll dat[s];
int n;
void init(){
memset(dat, -1e10, sizeof(dat));
}
void upd(int k, ll a){
k += s/2 - 1;
dat[k] = a;
while(k > 0){
k = (k -1) /2;
dat[k] = max(dat[k*2+1], dat[k*2+2]);
}
}
ll query(int a, int b, int k, int l, int r){
if(r <= a || b <= l) return -1e8;
if( a <= l && r <= b)return dat[k];
else
{
ll vl = query(a, b, k*2+1, l, (l+r)/2);
ll vr = query(a, b, k*2+2, (l+r)/2, r);
return max(vl, vr);
}
}
void Debug(int a){
a += s/2 - 1;
rep(i,s/2 - 1,a){
dump(dat[i]);
}
}
};
*/
//_____________________ following sorce code_________________________//
int n, m, k, x, y;
// vvll dp(400, vll(400));
vll dp(1 << 16, -1e18);
string s;
vvll v(16, vll(16));
vll cost(1 << 16);
vi dist;
vi dat[10005];
void dfs(int v, int u = -1, int score = 0) {
dist[v] = score;
for (auto c : dat[v]) {
if (c == u)
continue;
dfs(c, v, score + 1);
}
}
vi f(int x) {
dist = vi(n);
dfs(x);
return dist;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int u, v;
IN(n, u, v);
u--;
v--;
rep0(i, n - 1) {
int a, b;
IN(a, b);
a--;
b--;
dat[a].pb(b);
dat[b].pb(a);
}
vi distU = f(u);
vi distV = f(v);
int mx = 0;
rep0(i, n) {
if (distV[i] > distU[i]) {
chmax(mx, distV[i]);
}
}
mx--;
OUT(mx);
return 0;
} | #include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <utility>
#include <vector>
#define fir first
#define sec second
#define sz(s) (s).size();
#define pb push_back
#define get(n) scanf("%d", &n);
#define gets(s) \
string s; \
cin >> (s);
#define prfi(n) printf("%d", &n);
#define prfd(n) printf("%lf", &n);
#define All(s) (s).begin(), (s).end()
#define rep(i, j, k) for (int(i) = (j); (i) <= (k); (i)++)
#define rep0(i, j) for (int(i) = 0; (i) < (j); (i)++)
#define repdown(i, j, k) for (int(i) = (j); (i) >= (k); (i)--)
#define repdown0(i, j) for (int(i) = (j); i >= 0; i--)
#define vfor(c, v) for (auto(c) : v)
#define dump(x) std::cout << #x << " = " << (x) << std::endl;
#define debug(x) \
cout << #x << " = " << (x) << " (L" << __LINE__ << ")" \
<< " " << __FILE__ << endl;
using ll = long long;
using pii = std::pair<int, int>;
using vi = std::vector<int>;
using vvi = std::vector<vi>;
using vll = std::vector<ll>;
using vvll = std::vector<vll>;
using vd = std::vector<double>;
using vvd = std::vector<vd>;
using qi = std::queue<int>;
using vpii = std::vector<std::pair<int, int>>;
using namespace std;
const int Mod = (1e9) + 7;
const int max_n = 3 * (1e5) + 1;
const int max_m = 83 * (1e5) + 1;
const int INF = 10241024;
const ll INFL = (1ll << 62) - 1;
const int dx[] = {-1, 0, 0, 1};
const int dy[] = {0, -1, 1, 0};
//_____________________________________Templates_________________________________________//
template <class T1, class T2> inline void chmin(T1 &a, T2 b) {
if (a > b)
a = b;
}
template <class T1, class T2> inline void chmax(T1 &a, T2 b) {
if (a < b)
a = b;
}
// mainly use for dynamic prog
template <class T1, class T2> void update(T1 &a, T2 b) {
a += b;
if (a > Mod)
a %= Mod;
}
inline void IN(void) { return; }
template <typename First, typename... Rest>
void IN(First &first, Rest &...rest) {
cin >> first;
IN(rest...);
return;
}
inline void OUT(void) {
cout << "\n";
return;
}
template <typename First, typename... Rest>
void OUT(First first, Rest... rest) {
cout << first << " ";
OUT(rest...);
return;
}
template <class T> struct segt {
int n;
T def;
vector<T> dat;
function<T(T, T)> operation;
function<T(T, T)> update;
segt(size_t _n, T _def, function<T(T, T)> _operation,
function<T(T, T)> _update)
: def(_def), operation(_operation), update(_update) {
n = 1;
while (n < _n) {
n *= 2;
}
dat = vector<T>(2 * n - 1, _def);
}
T _query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l)
return def;
if (a <= r && l <= b)
return dat[k];
else {
T vl = _query(a, b, k * 2 + 1, l, (l + r) / 2);
T vr = _query(a, b, k * 2 + 2, (l + r) / 2, r);
return operation(vl, vr);
}
}
T query(int a, int b) { return _query(a, b, 0, 0, n); }
void change(int i, T value) {
i += n - 1;
dat[i] = update(dat[i], value);
while (i > 0) {
i = (i - 1) / 2;
dat[i] = operation(dat[i * 2 + 1], dat[i * 2 + 2]);
}
}
T operator[](int i) { return dat[i + n - 1]; }
};
//_____________________array calc____________________________________//
/*
vvi mul(vvi &A, vvi &B){
vvi C(A.size(), vi(B.size()))
rep(i,A.size()){
rep(j,B.size()){
rep(k,B[0].size()){
C[i][j] = (C[i][j] A[i][k] + B[k][j]) % Mod;
}
}
}
return C;
}
vvi pow(vvi A, ll n){
vvi B(A.size(), vi(A.size()));
rep(i=0;i<A.size();i++){
B[i][i] = 1;
}
while (n >0){
if (n & 1) B = mul(B, A);
A = mul(A, A);
n = n >> 1;
}
return B;
}
*/
//_____________________Bynary Indexed Tree __________________________//
/*
struct BIT{
#define bs (1 << 20)
int bit[bs];
int sum (int i){
int s = 0;
while(i > 0){
s += bit[i];
i -= i & -i;
}
return s;
}
void add(int i, int x){
while(i <= n){
bit[i] += x;
i += i & -i;
}
}
}
*/
//____________________RMQ____________________________//
/*struct RMQ{
#define s (1 << 20)
ll dat[s];
int n;
void init(){
memset(dat, -1e10, sizeof(dat));
}
void upd(int k, ll a){
k += s/2 - 1;
dat[k] = a;
while(k > 0){
k = (k -1) /2;
dat[k] = max(dat[k*2+1], dat[k*2+2]);
}
}
ll query(int a, int b, int k, int l, int r){
if(r <= a || b <= l) return -1e8;
if( a <= l && r <= b)return dat[k];
else
{
ll vl = query(a, b, k*2+1, l, (l+r)/2);
ll vr = query(a, b, k*2+2, (l+r)/2, r);
return max(vl, vr);
}
}
void Debug(int a){
a += s/2 - 1;
rep(i,s/2 - 1,a){
dump(dat[i]);
}
}
};
*/
//_____________________ following sorce code_________________________//
int n, m, k, x, y;
// vvll dp(400, vll(400));
vll dp(1 << 16, -1e18);
string s;
vvll v(16, vll(16));
vll cost(1 << 16);
vi dist;
vi dat[100005];
void dfs(int v, int u = -1, int score = 0) {
dist[v] = score;
for (auto c : dat[v]) {
if (c == u)
continue;
dfs(c, v, score + 1);
}
}
vi f(int x) {
dist = vi(n);
dfs(x);
return dist;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int u, v;
IN(n, u, v);
u--;
v--;
rep0(i, n - 1) {
int a, b;
IN(a, b);
a--;
b--;
dat[a].pb(b);
dat[b].pb(a);
}
vi distU = f(u);
vi distV = f(v);
int mx = 0;
rep0(i, n) {
if (distV[i] > distU[i]) {
chmax(mx, distV[i]);
}
}
mx--;
OUT(mx);
return 0;
} | replace | 234 | 235 | 234 | 235 | 0 | |
p02834 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <bitset>
using namespace std;
#define M 1000000007
#define pb push_back
#define f first
#define s second
#define rep(i, st, ed) for (int i = st; i < ed; i++)
#define repn(i, st, ed) for (int i = st; i <= ed; i++)
#define repb(i, ed, st) for (int i = ed; i >= st; i--)
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef unsigned long long int llt;
typedef long double ld;
const long double PI = 3.141592653589793238463;
const int N = 1010;
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;
}
// Main Code
vector<int> g[N];
bool visA[N];
bool visT[N];
ll distA[N];
ll distT[N];
// Normal DFS
void dfsA(int curr, int par, int dist) {
distA[curr] = dist;
visA[curr] = 1;
for (auto x : g[curr]) {
if (visA[x] == 0) {
dfsA(x, curr, dist + 1);
} else {
if (distA[x] > (distA[curr] + 1)) {
dfsA(x, curr, dist + 1);
}
}
}
}
void dfsT(int curr, int par, int dist) {
distT[curr] = dist;
visT[curr] = 1;
for (auto x : g[curr]) {
if (visT[x] == 0) {
dfsT(x, curr, dist + 1);
} else {
if (distT[x] > (distT[curr] + 1)) {
dfsT(x, curr, dist + 1);
}
}
}
}
int main() {
int n, u, v;
cin >> n >> u >> v;
rep(i, 1, n) {
int x, y;
cin >> x >> y;
g[x].pb(y);
g[y].pb(x);
}
dfsA(v, -1, 0);
dfsT(u, -1, 0);
ll ans = 0;
repn(i, 1, n) {
if (distA[i] > distT[i]) {
chmax(ans, distA[i] - 1);
}
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
#include <bitset>
using namespace std;
#define M 1000000007
#define pb push_back
#define f first
#define s second
#define rep(i, st, ed) for (int i = st; i < ed; i++)
#define repn(i, st, ed) for (int i = st; i <= ed; i++)
#define repb(i, ed, st) for (int i = ed; i >= st; i--)
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef unsigned long long int llt;
typedef long double ld;
const long double PI = 3.141592653589793238463;
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;
}
// Main Code
vector<int> g[N];
bool visA[N];
bool visT[N];
ll distA[N];
ll distT[N];
// Normal DFS
void dfsA(int curr, int par, int dist) {
distA[curr] = dist;
visA[curr] = 1;
for (auto x : g[curr]) {
if (visA[x] == 0) {
dfsA(x, curr, dist + 1);
} else {
if (distA[x] > (distA[curr] + 1)) {
dfsA(x, curr, dist + 1);
}
}
}
}
void dfsT(int curr, int par, int dist) {
distT[curr] = dist;
visT[curr] = 1;
for (auto x : g[curr]) {
if (visT[x] == 0) {
dfsT(x, curr, dist + 1);
} else {
if (distT[x] > (distT[curr] + 1)) {
dfsT(x, curr, dist + 1);
}
}
}
}
int main() {
int n, u, v;
cin >> n >> u >> v;
rep(i, 1, n) {
int x, y;
cin >> x >> y;
g[x].pb(y);
g[y].pb(x);
}
dfsA(v, -1, 0);
dfsT(u, -1, 0);
ll ans = 0;
repn(i, 1, n) {
if (distA[i] > distT[i]) {
chmax(ans, distA[i] - 1);
}
}
cout << ans << endl;
}
| replace | 18 | 19 | 18 | 19 | 0 | |
p02834 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(vec) vec.begin(), vec.end()
typedef long long ll;
const int MAX = 2e5;
const ll MOD = 1e9 + 7;
const int INF = 1 << 30;
int N;
vector<vector<int>> G;
vector<int> dist;
void dfs(int v, int d = 0, int p = -1) {
dist[v] = d;
for (int nv : G[v]) {
if (nv == p)
continue;
dfs(nv, d + 1, v);
}
}
vector<int> calcdist(int v) {
dist = vector<int>(N); // 初期化
dfs(v);
return dist;
}
int main() {
cin >> N;
int u, v;
cin >> u >> v;
u--;
v--;
G.resize(N, vector<int>(0));
rep(i, N) {
int a, b;
cin >> a >> b;
a--;
b--;
G[a].push_back(b);
G[b].push_back(a);
}
vector<int> distu = calcdist(u);
vector<int> distv = calcdist(v);
int mv = 0;
rep(i, N) {
if (distu[i] < distv[i]) {
mv = max(mv, distv[i]);
}
}
int ans = mv - 1;
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(vec) vec.begin(), vec.end()
typedef long long ll;
const int MAX = 2e5;
const ll MOD = 1e9 + 7;
const int INF = 1 << 30;
int N;
vector<vector<int>> G;
vector<int> dist;
void dfs(int v, int d = 0, int p = -1) {
dist[v] = d;
for (int nv : G[v]) {
if (nv == p)
continue;
dfs(nv, d + 1, v);
}
}
vector<int> calcdist(int v) {
dist = vector<int>(N); // 初期化
dfs(v);
return dist;
}
int main() {
cin >> N;
int u, v;
cin >> u >> v;
u--;
v--;
G.resize(N, vector<int>(0));
rep(i, N - 1) {
int a, b;
cin >> a >> b;
a--;
b--;
G[a].push_back(b);
G[b].push_back(a);
}
vector<int> distu = calcdist(u);
vector<int> distv = calcdist(v);
int mv = 0;
rep(i, N) {
if (distu[i] < distv[i]) {
mv = max(mv, distv[i]);
}
}
int ans = mv - 1;
cout << ans << endl;
} | replace | 49 | 50 | 49 | 50 | -11 | |
p02834 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
vector<bool> seen;
vector<int> U, V;
void dfs(Graph G, int v, int num) {
seen[v] = true;
U[v] = num;
num++;
for (int next_v : G[v]) {
if (seen[next_v])
continue;
dfs(G, next_v, num);
}
}
int main() {
int N, u, v;
cin >> N >> u >> v;
Graph G(N + 1);
U.assign(N + 1, 0);
V.assign(N + 1, 0);
for (int i = 1; i <= N; i++) {
int a, b;
cin >> a >> b;
G[a].push_back(b);
G[b].push_back(a);
}
int ans = 0;
seen.assign(N + 1, false);
int num = 0;
dfs(G, v, num);
num = 0, V = U;
seen.assign(N + 1, false);
dfs(G, u, num);
for (int i = 1; i <= N; i++) {
if (V[i] > U[i]) {
ans = max(ans, V[i]);
}
}
ans--;
cout << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
vector<bool> seen;
vector<int> U, V;
void dfs(Graph &G, int v, int num) {
seen[v] = true;
U[v] = num;
num++;
for (int next_v : G[v]) {
if (seen[next_v])
continue;
dfs(G, next_v, num);
}
}
int main() {
int N, u, v;
cin >> N >> u >> v;
Graph G(N + 1);
U.assign(N + 1, 0);
V.assign(N + 1, 0);
for (int i = 1; i <= N; i++) {
int a, b;
cin >> a >> b;
G[a].push_back(b);
G[b].push_back(a);
}
int ans = 0;
seen.assign(N + 1, false);
int num = 0;
dfs(G, v, num);
num = 0, V = U;
seen.assign(N + 1, false);
dfs(G, u, num);
for (int i = 1; i <= N; i++) {
if (V[i] > U[i]) {
ans = max(ans, V[i]);
}
}
ans--;
cout << ans << endl;
}
| replace | 5 | 6 | 5 | 6 | TLE | |
p02834 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using vvi = vector<vector<int>>;
#define repex(i, a, b, c) for (int i = a; i < b; i += c)
#define repx(i, a, b) repex(i, a, b, 1)
#define rep(i, n) repx(i, 0, n)
#define pb push_back
int du[101010], dv[101010], duv[101010]; // 距離を保存
// グラフを幅優先探索する.
// https://ja.wikipedia.org/wiki/幅優先探索
// ※bfsの動作確認用.
// @param G: グラフ.
// @param s: グラフの探索開始頂点.
// @param d: 探索開始地点からの距離.
// @return: 特に無し.
void bfs(vvi &G, int s, int *d) {
// 1. 空のキュー.
queue<int> q;
// 2. 訪問済みフラグ設定.
// ※スタート地点は, 距離ゼロを指定.
d[s] = 0;
// 3. 探索地点 s をキュー q に追加.
q.push(s);
while (!q.empty()) {
// 4. キューから取り出す.
int u = q.front();
q.pop();
// 5. 取り出した要素を処理.
for (auto &e : G[u]) {
// 6. 訪問済であれば, 処理をスキップ.
if (d[e] > 0)
continue;
if (d[e] == 0 && e != s)
d[e] = d[u] + 1, q.push(e);
}
}
return;
}
int main() {
// 1. 入力情報.
int N, u, v;
scanf("%d %d %d", &N, &u, &v);
u--, v--;
vvi G(N);
rep(i, N - 1) {
int a, b;
scanf("%d %d", &a, &b);
a--, b--;
G[a].pb(b);
G[b].pb(a);
}
// 2. 頂点 u, v から 各頂点までの距離を計算.
bfs(G, u, du);
bfs(G, v, dv);
// rep(i, N) printf("%d ", du[i]);
// puts("");
// rep(i, N) printf("%d ", dv[i]);
// puts("");
// 3. 頂点 u, v の 中間地点 m を 取得.
int m = -1, dm = du[v] / 2;
rep(i, N) {
if (du[i] == dm && dv[i] == dm) {
m = i;
break;
}
}
// 4. 頂点 m から 各頂点までの距離を計算.
bfs(G, m, duv);
// rep(i, N) printf("%d ", duv[i]);
// printf("\nm=%d\n", m);
// 5. 頂点 m から 最も遠い頂点 f を 取得.
// ※ 但し, 頂点 v よりも 頂点 u に近いものに限定.
int f = -1, df = 0;
rep(i, N) {
if (df < duv[i] && du[i] < dv[i])
f = i, df = max(df, duv[i]);
}
// 6. 出力.
printf("%d\n", duv[v] + duv[f] - 1);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using vvi = vector<vector<int>>;
#define repex(i, a, b, c) for (int i = a; i < b; i += c)
#define repx(i, a, b) repex(i, a, b, 1)
#define rep(i, n) repx(i, 0, n)
#define pb push_back
int du[101010], dv[101010], duv[101010]; // 距離を保存
// グラフを幅優先探索する.
// https://ja.wikipedia.org/wiki/幅優先探索
// ※bfsの動作確認用.
// @param G: グラフ.
// @param s: グラフの探索開始頂点.
// @param d: 探索開始地点からの距離.
// @return: 特に無し.
void bfs(vvi &G, int s, int *d) {
// 1. 空のキュー.
queue<int> q;
// 2. 訪問済みフラグ設定.
// ※スタート地点は, 距離ゼロを指定.
d[s] = 0;
// 3. 探索地点 s をキュー q に追加.
q.push(s);
while (!q.empty()) {
// 4. キューから取り出す.
int u = q.front();
q.pop();
// 5. 取り出した要素を処理.
for (auto &e : G[u]) {
// 6. 訪問済であれば, 処理をスキップ.
if (d[e] > 0)
continue;
if (d[e] == 0 && e != s)
d[e] = d[u] + 1, q.push(e);
}
}
return;
}
int main() {
// 1. 入力情報.
int N, u, v;
scanf("%d %d %d", &N, &u, &v);
u--, v--;
vvi G(N);
rep(i, N - 1) {
int a, b;
scanf("%d %d", &a, &b);
a--, b--;
G[a].pb(b);
G[b].pb(a);
}
// 2. 頂点 u, v から 各頂点までの距離を計算.
bfs(G, u, du);
bfs(G, v, dv);
// rep(i, N) printf("%d ", du[i]);
// puts("");
// rep(i, N) printf("%d ", dv[i]);
// puts("");
// 3. 頂点 u, v の 中間地点 m を 取得.
int m = -1, dm = du[v] / 2;
rep(i, N) {
if (du[v] % 2 == 0) {
if (du[i] == dm && dv[i] == dm) {
m = i;
break;
}
} else {
if (du[i] == dm && dv[i] == dm + 1) {
m = i;
break;
}
}
}
// 4. 頂点 m から 各頂点までの距離を計算.
bfs(G, m, duv);
// rep(i, N) printf("%d ", duv[i]);
// printf("\nm=%d\n", m);
// 5. 頂点 m から 最も遠い頂点 f を 取得.
// ※ 但し, 頂点 v よりも 頂点 u に近いものに限定.
int f = -1, df = 0;
rep(i, N) {
if (df < duv[i] && du[i] < dv[i])
f = i, df = max(df, duv[i]);
}
// 6. 出力.
printf("%d\n", duv[v] + duv[f] - 1);
return 0;
} | replace | 72 | 75 | 72 | 82 | 0 | |
p02834 | C++ | Runtime Error | #include "bits/stdc++.h"
using namespace std;
vector<int> G[10500];
int dist[2][10500] = {};
int ans = 0;
void DFS(int node, int parent, int man) {
for (auto it : G[node]) {
if (it != parent) {
dist[man][it] = dist[man][node] + 1;
DFS(it, node, man);
}
}
}
int main() {
int n, v, u;
cin >> n >> u >> v;
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
G[a].push_back(b);
G[b].push_back(a);
}
DFS(u, -1, 0);
DFS(v, -1, 1);
for (int i = 1; i <= n; i++) {
if (dist[0][i] < dist[1][i]) {
ans = max(ans, dist[1][i] - 1);
}
}
cout << ans << endl;
return 0;
}
| #include "bits/stdc++.h"
using namespace std;
vector<int> G[105000];
int dist[2][100500] = {};
int ans = 0;
void DFS(int node, int parent, int man) {
for (auto it : G[node]) {
if (it != parent) {
dist[man][it] = dist[man][node] + 1;
DFS(it, node, man);
}
}
}
int main() {
int n, v, u;
cin >> n >> u >> v;
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
G[a].push_back(b);
G[b].push_back(a);
}
DFS(u, -1, 0);
DFS(v, -1, 1);
for (int i = 1; i <= n; i++) {
if (dist[0][i] < dist[1][i]) {
ans = max(ans, dist[1][i] - 1);
}
}
cout << ans << endl;
return 0;
}
| replace | 3 | 5 | 3 | 5 | 0 | |
p02834 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define vout(x) rep(i, x.size()) cout << x[i] << " "
template <class T> bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
using namespace std;
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int, int>;
const int inf = 1e9;
const ll inf_l = 1e18;
const int MAX = 1e5;
void dfs(const vvint g, vint &dis, int v, int p = -1) {
for (int n_v : g[v]) {
if (n_v == p)
continue;
dis[n_v] = dis[v] + 1;
dfs(g, dis, n_v, v);
}
}
int main() {
int n, u, v;
cin >> n >> u >> v;
u--;
v--;
vvint g(n);
rep(i, n - 1) {
int a, b;
cin >> a >> b;
a--;
b--;
g[a].push_back(b);
g[b].push_back(a);
}
vint dis_u(n, inf);
vint dis_v(n, inf);
dis_u[u] = 0;
dis_v[v] = 0;
dfs(g, dis_u, u);
dfs(g, dis_v, v);
int ans = 0;
rep(i, n) {
if (dis_u[i] < dis_v[i]) {
ans = max(ans, dis_v[i] - 1);
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define vout(x) rep(i, x.size()) cout << x[i] << " "
template <class T> bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
using namespace std;
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int, int>;
const int inf = 1e9;
const ll inf_l = 1e18;
const int MAX = 1e5;
void dfs(const vvint &g, vint &dis, int v, int p = -1) {
for (int n_v : g[v]) {
if (n_v == p)
continue;
dis[n_v] = dis[v] + 1;
dfs(g, dis, n_v, v);
}
}
int main() {
int n, u, v;
cin >> n >> u >> v;
u--;
v--;
vvint g(n);
rep(i, n - 1) {
int a, b;
cin >> a >> b;
a--;
b--;
g[a].push_back(b);
g[b].push_back(a);
}
vint dis_u(n, inf);
vint dis_v(n, inf);
dis_u[u] = 0;
dis_v[v] = 0;
dfs(g, dis_u, u);
dfs(g, dis_v, v);
int ans = 0;
rep(i, n) {
if (dis_u[i] < dis_v[i]) {
ans = max(ans, dis_v[i] - 1);
}
}
cout << ans << endl;
} | replace | 31 | 32 | 31 | 32 | TLE | |
p02834 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <iostream>
using namespace std;
vector<vector<int>> g;
vector<int> used;
void dfs(int cur, int d, vector<int> &h) {
h[cur] = d;
used[cur] = 1;
for (auto x : g[cur]) {
if (!used[x]) {
dfs(x, d + 1, h);
}
}
}
int main() {
// your code goes here
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, u, v;
cin >> n >> u >> v;
u--;
v--;
vector<int> hu(n), hv(n);
g.resize(n);
used.resize(n);
for (int i = 0; i < n; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
g[a].push_back(b);
g[b].push_back(a);
}
// fill(used.begin(),used.end(),0);
dfs(u, 0, hu);
fill(used.begin(), used.end(), 0);
dfs(v, 0, hv);
int ans = 0;
for (int i = 0; i < n; i++) {
if (hv[i] > hu[i]) {
ans = max(ans, hv[i] - 1);
}
}
cout << ans;
return 0;
}
| #include <bits/stdc++.h>
#include <iostream>
using namespace std;
vector<vector<int>> g;
vector<int> used;
void dfs(int cur, int d, vector<int> &h) {
h[cur] = d;
used[cur] = 1;
for (auto x : g[cur]) {
if (!used[x]) {
dfs(x, d + 1, h);
}
}
}
int main() {
// your code goes here
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, u, v;
cin >> n >> u >> v;
u--;
v--;
vector<int> hu(n), hv(n);
g.resize(n);
used.resize(n);
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
g[a].push_back(b);
g[b].push_back(a);
}
// fill(used.begin(),used.end(),0);
dfs(u, 0, hu);
fill(used.begin(), used.end(), 0);
dfs(v, 0, hv);
int ans = 0;
for (int i = 0; i < n; i++) {
if (hv[i] > hu[i]) {
ans = max(ans, hv[i] - 1);
}
}
cout << ans;
return 0;
}
| replace | 27 | 28 | 27 | 28 | 0 | |
p02834 | Python | Time Limit Exceeded | def p_f():
from sys import stdin
input = stdin.readline
N, u, v = map(int, input().split())
edges = [list(map(int, input().split())) for i in range(N - 1)]
if u == v:
print(0)
exit()
def BFS(K, edges, N):
roots = [[] for i in range(N)]
for a, b in edges:
roots[a - 1] += [(b - 1, 1)]
roots[b - 1] += [(a - 1, 1)]
dist = [-1] * N
stack = []
stack.append(K)
dist[K] = 0
while stack:
label = stack.pop(-1)
for i, c in roots[label]:
if dist[i] == -1:
dist[i] = dist[label] + c
stack += [i]
return dist
dist_v = BFS(v - 1, edges, N)
dist_u = BFS(u - 1, edges, N)
# print(dist_v)
# print(dist_u)
maxIndex = [i for i, x in enumerate(dist_v) if x == max(dist_v)]
for i in maxIndex:
if max(dist_v) > dist_u[i]:
print(max(dist_v) - 1)
exit()
if dist_v[u - 1] > max(dist_v) - dist_v[u - 1]:
print(max(dist_v) - 1)
else:
print(dist_v[u - 1] - 1)
if __name__ == "__main__":
p_f()
| def p_f():
from sys import stdin
input = stdin.readline
N, u, v = map(int, input().split())
edges = [list(map(int, input().split())) for i in range(N - 1)]
if u == v:
print(0)
exit()
def BFS(K, edges, N):
roots = [[] for i in range(N)]
for a, b in edges:
roots[a - 1] += [(b - 1, 1)]
roots[b - 1] += [(a - 1, 1)]
dist = [-1] * N
stack = []
stack.append(K)
dist[K] = 0
while stack:
label = stack.pop(-1)
for i, c in roots[label]:
if dist[i] == -1:
dist[i] = dist[label] + c
stack += [i]
return dist
dist_v = BFS(v - 1, edges, N)
dist_u = BFS(u - 1, edges, N)
ans = 0
for du, dv in zip(dist_u, dist_v):
if du < dv and ans < dv - 1:
ans = dv - 1
print(ans)
if __name__ == "__main__":
p_f()
| replace | 29 | 40 | 29 | 34 | TLE | |
p02834 | C++ | Runtime Error | #include <iostream>
#include <vector>
using namespace std;
vector<vector<int>> c(10010, vector<int>(0));
int N;
// 既に探索した部分をboolの配列で表現したところREとなった
// 上記boolをやめて,一つ前のpositionをppで把握→ppを飛ばすとしても提出結果に変わりなし
void DFS(int p, int ans, int pp, vector<int> &d) {
d.at(p) = ans;
for (int i : c.at(p)) {
if (i == pp) {
continue;
}
DFS(i, ans + 1, p, d);
}
return;
}
vector<int> dist(int p) {
vector<int> d(N);
DFS(p, 0, -1, d);
return d;
}
int main() {
int T, A, ans = 0;
cin >> N >> T >> A;
T--;
A--;
int s, t;
for (int i = 0; i < N - 1; i++) {
cin >> s >> t;
s--;
t--;
c.at(s).push_back(t);
c.at(t).push_back(s);
}
vector<int> Td(N), Ad(N);
Td = dist(T);
Ad = dist(A);
for (int i = 0; i < N; i++) {
if (Td[i] < Ad[i]) {
ans = max(ans, Ad[i] - 1);
}
}
cout << ans;
} | #include <iostream>
#include <vector>
using namespace std;
vector<vector<int>> c(100100, vector<int>(0));
int N;
// 既に探索した部分をboolの配列で表現したところREとなった
// 上記boolをやめて,一つ前のpositionをppで把握→ppを飛ばすとしても提出結果に変わりなし
void DFS(int p, int ans, int pp, vector<int> &d) {
d.at(p) = ans;
for (int i : c.at(p)) {
if (i == pp) {
continue;
}
DFS(i, ans + 1, p, d);
}
return;
}
vector<int> dist(int p) {
vector<int> d(N);
DFS(p, 0, -1, d);
return d;
}
int main() {
int T, A, ans = 0;
cin >> N >> T >> A;
T--;
A--;
int s, t;
for (int i = 0; i < N - 1; i++) {
cin >> s >> t;
s--;
t--;
c.at(s).push_back(t);
c.at(t).push_back(s);
}
vector<int> Td(N), Ad(N);
Td = dist(T);
Ad = dist(A);
for (int i = 0; i < N; i++) {
if (Td[i] < Ad[i]) {
ans = max(ans, Ad[i] - 1);
}
}
cout << ans;
} | replace | 3 | 4 | 3 | 4 | 0 | |
p02834 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define bit(x) (1L << (x))
using ll = long long;
using namespace std;
template <typename T> vector<T> make_v(size_t a, T b) {
return vector<T>(a, b);
}
template <typename... Ts> auto make_v(size_t a, Ts... ts) {
return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}
struct node {
int aoki;
vector<int> neighbor;
bool isLeaf() const { return neighbor.size() == 1; }
};
using graph = vector<node>;
void aoki(int s, graph &g) {
function<void(int, int)> rec = [&](int v, int bv) {
for (int u : g[v].neighbor) {
if (u != bv) {
g[u].aoki = g[v].aoki + 1;
rec(u, v);
}
}
};
g[s].aoki = 0;
rec(s, -1);
}
int f(int n) {
static map<int, int> mp;
if (mp[n]) {
return mp[n];
}
if (n == 1) {
return 0;
} else if (n == 2) {
return 1;
} else {
return mp[n] = 2 + f(n - 2);
}
}
int takahashi(int s, graph &g) {
function<int(int, int, int)> rec = [&](int v, int bv, int step) {
int ret = 0;
if (g[v].isLeaf()) {
ret = step + f(g[v].aoki - step);
}
for (int u : g[v].neighbor) {
if (u != bv && g[u].aoki - step > 0) {
ret = max(ret, rec(u, v, step + 1));
}
}
return ret;
};
return rec(s, -1, 0);
}
int main() {
int n, u, v;
cin >> n >> u >> v;
--u;
--v;
graph g(n);
REP(i, n - 1) {
int a, b;
cin >> a >> b;
--a;
--b;
g[a].neighbor.push_back(b);
g[b].neighbor.push_back(a);
}
aoki(v, g);
cout << takahashi(u, g) << endl;
return 0;
}
| #include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define bit(x) (1L << (x))
using ll = long long;
using namespace std;
template <typename T> vector<T> make_v(size_t a, T b) {
return vector<T>(a, b);
}
template <typename... Ts> auto make_v(size_t a, Ts... ts) {
return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}
struct node {
int aoki;
vector<int> neighbor;
bool isLeaf() const { return neighbor.size() == 1; }
};
using graph = vector<node>;
void aoki(int s, graph &g) {
function<void(int, int)> rec = [&](int v, int bv) {
for (int u : g[v].neighbor) {
if (u != bv) {
g[u].aoki = g[v].aoki + 1;
rec(u, v);
}
}
};
g[s].aoki = 0;
rec(s, -1);
}
int f(int n) { return n - 1; }
int takahashi(int s, graph &g) {
function<int(int, int, int)> rec = [&](int v, int bv, int step) {
int ret = 0;
if (g[v].isLeaf()) {
ret = step + f(g[v].aoki - step);
}
for (int u : g[v].neighbor) {
if (u != bv && g[u].aoki - step > 0) {
ret = max(ret, rec(u, v, step + 1));
}
}
return ret;
};
return rec(s, -1, 0);
}
int main() {
int n, u, v;
cin >> n >> u >> v;
--u;
--v;
graph g(n);
REP(i, n - 1) {
int a, b;
cin >> a >> b;
--a;
--b;
g[a].neighbor.push_back(b);
g[b].neighbor.push_back(a);
}
aoki(v, g);
cout << takahashi(u, g) << endl;
return 0;
}
| replace | 39 | 53 | 39 | 40 | TLE | |
p02834 | Python | Runtime Error | # 解説を見た
def solve(N, t, a, g) -> int:
def calc_dist(init):
dist = [-1] * N
dist[init] = 0
def _calc_dist(curr):
for next_ in g[curr]:
if dist[next_] != -1:
continue
dist[next_] = dist[curr] + 1
_calc_dist(next_)
_calc_dist(init)
return dist
dist_a = calc_dist(a)
dist_t = calc_dist(t)
return max(dist_a[v] for v in range(N) if dist_t[v] < dist_a[v]) - 1
if __name__ == "__main__":
N, t, a = map(int, input().split())
t -= 1
a -= 1
g = tuple(set() for _ in range(N))
for _ in range(N - 1):
u, v = (int(x) - 1 for x in input().split())
g[u].add(v)
g[v].add(u)
print(solve(N, t, a, g))
# 青木君からの距離 > 高橋君からの距離 となる頂点Xが目的地の候補
# 青木君と高橋君の共通祖先からの距離は等しいので、
# 共通祖先への距離は、 青木君 > 高橋君 となり、高橋君は必ずXに到達できる
# Xに着いた高橋君は、手前の頂点とXとを行ったり来たりする
# a-t->at-: aが一歩進んだときに高橋君に会う(-は無人のマス目)
# at: tが手前の頂点に移動したときに青木君に会う
# いずれも手前の頂点まで青木君が移動したときにゲームが終わる
# どのXを選んでも、Xの手前でゲームが終わるので、青木君から最も遠いXを選ぶのが最善
# 頂点X: 追いつめられる位置=葉
| # 解説を見た
def solve(N, t, a, g) -> int:
import sys
sys.setrecursionlimit(10**7)
def calc_dist(init):
dist = [-1] * N
dist[init] = 0
def _calc_dist(curr):
for next_ in g[curr]:
if dist[next_] != -1:
continue
dist[next_] = dist[curr] + 1
_calc_dist(next_)
_calc_dist(init)
return dist
dist_a = calc_dist(a)
dist_t = calc_dist(t)
return max(dist_a[v] for v in range(N) if dist_t[v] < dist_a[v]) - 1
if __name__ == "__main__":
N, t, a = map(int, input().split())
t -= 1
a -= 1
g = tuple(set() for _ in range(N))
for _ in range(N - 1):
u, v = (int(x) - 1 for x in input().split())
g[u].add(v)
g[v].add(u)
print(solve(N, t, a, g))
# 青木君からの距離 > 高橋君からの距離 となる頂点Xが目的地の候補
# 青木君と高橋君の共通祖先からの距離は等しいので、
# 共通祖先への距離は、 青木君 > 高橋君 となり、高橋君は必ずXに到達できる
# Xに着いた高橋君は、手前の頂点とXとを行ったり来たりする
# a-t->at-: aが一歩進んだときに高橋君に会う(-は無人のマス目)
# at: tが手前の頂点に移動したときに青木君に会う
# いずれも手前の頂点まで青木君が移動したときにゲームが終わる
# どのXを選んでも、Xの手前でゲームが終わるので、青木君から最も遠いXを選ぶのが最善
# 頂点X: 追いつめられる位置=葉
| insert | 4 | 4 | 4 | 8 | 0 | |
p02834 | Python | Runtime Error | from collections import deque
def nearlist(N, LIST): # 隣接リスト
NEAR = [set() for _ in range(N)]
for a, b in LIST:
NEAR[a - 1].add(b - 1)
NEAR[b - 1].add(a - 1)
return NEAR
def bfs(NEAR, S, N): # 幅優先探索 # キュー
dist = [-1 for _ in range(N)] # 前処理
dist[S] = 0
que, frag = deque([S]), set([S])
while len(que) > 0:
q = que.popleft()
for i in NEAR[q]: # 移動先の候補
if i in frag: # 処理済みか否か
continue
dist[i] = dist[q] + 1
que.append(i), frag.add(i)
return dist
n, u, v = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n - 1)]
near = nearlist(n, ab)
tkdist, akdist = bfs(near, u - 1, n), bfs(near, v - 1, n)
node = [i for i in range(n) if tkdist[i] <= akdist[i]]
ans = max(akdist[i] for i in node)
print(ans[0] - 1)
| from collections import deque
def nearlist(N, LIST): # 隣接リスト
NEAR = [set() for _ in range(N)]
for a, b in LIST:
NEAR[a - 1].add(b - 1)
NEAR[b - 1].add(a - 1)
return NEAR
def bfs(NEAR, S, N): # 幅優先探索 # キュー
dist = [-1 for _ in range(N)] # 前処理
dist[S] = 0
que, frag = deque([S]), set([S])
while len(que) > 0:
q = que.popleft()
for i in NEAR[q]: # 移動先の候補
if i in frag: # 処理済みか否か
continue
dist[i] = dist[q] + 1
que.append(i), frag.add(i)
return dist
n, u, v = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n - 1)]
near = nearlist(n, ab)
tkdist, akdist = bfs(near, u - 1, n), bfs(near, v - 1, n)
node = [i for i in range(n) if tkdist[i] <= akdist[i]]
ans = max(akdist[i] for i in node)
print(ans - 1)
| replace | 34 | 35 | 34 | 35 | TypeError: 'int' object is not subscriptable | Traceback (most recent call last):
File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02834/Python/s347111194.py", line 35, in <module>
print(ans[0] - 1)
TypeError: 'int' object is not subscriptable
|
p02834 | C++ | Runtime Error | // #undef _DEBUG
// #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace std::chrono;
#define int long long
#define ll long long
auto start_time = system_clock::now();
#define debugName(VariableName) #VariableName
/*@formatter:off*/
//[-n, n)にアクセスできる
// また、外部関数resizeに渡せる
template <class T> struct mvec {
vector<T> v;
int n;
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 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(); }
};
//[]で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(); }
};
// todo?
template <class T> ostream &operator<<(ostream &os, mvec<T> &a) { 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>;
#ifdef _DEBUG
// https://marycore.jp/prog/cpp/class-extension-methods/ 違うかも
template <class T, class A = std::allocator<T>>
struct debtor : std::vector<T, A> {
using std::vector<T, A>::vector;
template <class U> int deb_v(U a, int v) { return v; }
template <class U> int deb_v(debtor<U> &a, int v = 0) {
cerr << a.size() << " ";
return deb_v(a.at(0), v + 1);
}
template <class U> void deb_o(U a) { cerr << a << " "; }
template <class U> void deb_o(debtor<U> &a) {
for (int i = 0; i < min((int)a.size(), 15ll); i++) {
deb_o(a[i]);
}
if ((int)a.size() > 15) {
cerr << "...";
}
cerr << endl;
}
typename std::vector<T>::reference
operator[](typename std::vector<T>::size_type n) {
if (n < 0 || n >= (int)this->size()) {
int siz = (int)this->size();
cerr << "vector size = ";
int dim = deb_v((*this));
cerr << endl;
cerr << "out index at " << n << endl;
cerr << endl;
if (dim <= 2) {
deb_o((*this));
}
exit(0);
}
return this->at(n);
}
};
#define vector debtor
#endif
#ifdef _DEBUG
// 区間削除は出来ない
template <class T> struct my_pbds_tree {
set<T> s;
auto begin() { return s.begin(); }
auto end() { return s.end(); }
auto rbegin() { return s.rbegin(); }
auto rend() { return s.rend(); }
auto empty() { return s.empty(); }
auto size() { return s.size(); }
void clear() { s.clear(); }
template <class U> void insert(U v) { s.insert(v); }
template <class U> void operator+=(U v) { insert(v); }
template <class F> auto erase(F v) { return s.erase(v); }
template <class U> auto find(U v) { return s.find(v); }
template <class U> auto lower_bound(U v) { return s.lower_bound(v); }
template <class U> auto upper_bound(U v) { return s.upper_bound(v); }
auto find_by_order(ll k) {
auto it = s.begin();
for (ll i = 0; i < k; i++)
it++;
return it;
}
auto order_of_key(ll v) {
auto it = s.begin();
ll i = 0;
for (; it != s.end() && *it < v; i++)
it++;
return i;
}
};
#define pbds(T) my_pbds_tree<T>
// gp_hash_tableでcountを使えないようにするため
template <class T, class U> struct my_unordered_map {
unordered_map<T, U> m;
my_unordered_map(){};
auto begin() { return m.begin(); }
auto end() { return m.end(); }
auto cbegin() { return m.cbegin(); }
auto cend() { return m.cend(); }
template <class V> auto erase(V v) { return m.erase(v); }
void clear() {
m.clear();
} /*countは gp_hash_tableに存在しない*/ /*!= m.end()*/
template <class V> auto find(V v) { return m.find(v); }
template <class V> auto &operator[](V n) { return m[n]; }
};
#define unordered_map my_unordered_map
#define umapi unordered_map<ll, ll>
#define umapp unordered_map<P, ll>
#define umapip unordered_map<ll, P>
#else
// umapはunorderd_mapになる
// umapiはgp_hash_table
// find_by_order(k) k番目のイテレーター
// order_of_key(k) k以上が前から何番目か
#define pbds(U) \
__gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag, \
__gnu_pbds::tree_order_statistics_node_update>
#define umapi __gnu_pbds::gp_hash_table<ll, ll, xorshift>
#define umapp __gnu_pbds::gp_hash_table<P, ll, xorshift>
#define umapip __gnu_pbds::gp_hash_table<ll, P, xorshift>
#endif
struct xorshift {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
size_t operator()(std::pair<ll, ll> x) const {
ll v = ((x.first) << 32) | x.second;
static const uint64_t FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(v + FIXED_RANDOM);
}
};
template <class U, class L>
void operator+=(
__gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag,
__gnu_pbds::tree_order_statistics_node_update> &s,
L v) {
s.insert(v);
}
// 衝突対策
#define ws ws_
template <class A, class B, class C> struct T2 {
A f;
B s;
C t;
T2() { f = 0, s = 0, t = 0; }
T2(A f, B s, C t) : f(f), s(s), t(t) {}
bool operator<(const T2 &r) const {
return f != r.f ? f < r.f
: s != r.s ? s < r.s
: t < r.t; /*return f != r.f ? f > r.f : s != r.s ?n s >
r.s : t > r.t; 大きい順 */
}
bool operator>(const T2 &r) const {
return f != r.f ? f > r.f
: s != r.s ? s > r.s
: t > r.t; /*return f != r.f ? f > r.f : s != r.s ? s >
r.s : t > r.t; 小さい順 */
}
bool operator==(const T2 &r) const {
return f == r.f && s == r.s && t == r.t;
}
bool operator!=(const T2 &r) const {
return f != r.f || s != r.s || t != r.t;
}
};
template <class A, class B, class C, class D> struct F2 {
A a;
B b;
C c;
D d;
F2() { a = 0, b = 0, c = 0, d = 0; }
F2(A a, B b, C c, D d) : a(a), b(b), c(c), d(d) {}
bool operator<(const F2 &r) const {
return a != r.a ? a < r.a
: b != r.b ? b < r.b
: c != r.c ? c < r.c
: d < r.d; /* return a != r.a ? a > r.a : b != r.b ? b
> r.b : c != r.c ? c > r.c : d > r.d;*/
}
bool operator>(const F2 &r) const {
return a != r.a ? a > r.a
: b != r.b ? b > r.b
: c != r.c ? c > r.c
: d > r.d; /* return a != r.a ? a < r.a : b != r.b
? b < r.b : c != r.c ? c < r.c : d < r.d;*/
}
bool operator==(const F2 &r) const {
return a == r.a && b == r.b && c == r.c && d == r.d;
}
bool operator!=(const F2 &r) const {
return a != r.a || b != r.b || c != r.c || d != r.d;
}
ll operator[](ll i) {
assert(i < 4);
return i == 0 ? a : i == 1 ? b : i == 2 ? c : d;
}
};
typedef T2<ll, ll, ll> T;
typedef F2<ll, ll, ll, ll> F;
T mt(ll a, ll b, ll c) { return T(a, b, c); }
F mf(ll a, ll b, ll c, ll d) { return F(a, b, c, d); }
//@マクロ省略系 型,構造
#define double long double
#define pow powl
#define ull unsigned long long
using dou = double;
using itn = int;
using str = string;
using bo = bool;
#define au auto
using P = pair<ll, ll>;
using mvp = mvec<P>;
using mvt = mvec<T>;
#define fi first
#define se second
#define beg begin
#define rbeg rbegin
#define con continue
#define bre break
#define brk break
#define is ==
#define el else
#define elf else if
#define wh while
#define upd update
#define sstream stringstream
#define maxq 1
#define minq -1
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
#define MALLOC(type, len) (type *)malloc((len) * sizeof(type))
#define lam(right) [&](auto &p) { return p right; }
// マクロ省略系 コンテナ
using vi = vector<ll>;
using vb = vector<bool>;
using vs = vector<string>;
using vd = vector<double>;
using vc = vector<char>;
using vp = vector<P>;
using vt = vector<T>;
#define V vector
#define o_vvt(o1, o2, o3, o4, name, ...) name
#define vvt0(t) vector<vector<t>>
#define vvt1(t, a) vector<vector<t>> a
#define vvt2(t, a, b) vector<vector<t>> a(b)
#define vvt3(t, a, b, c) vector<vector<t>> a(b, vector<t>(c))
#define vvt4(t, a, b, c, d) vector<vector<t>> a(b, vector<t>(c, d))
#define vvi(...) \
o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(ll, __VA_ARGS__)
#define vvb(...) \
o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(bool, __VA_ARGS__)
#define vvs(...) \
o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(string, __VA_ARGS__)
#define vvd(...) \
o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(double, __VA_ARGS__)
#define vvc(...) \
o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(char, __VA_ARGS__)
#define vvp(...) \
o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(P, __VA_ARGS__)
#define vvt(...) \
o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(T, __VA_ARGS__)
#define vv(type, ...) \
o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(type, __VA_ARGS__)
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
#define vni(name, ...) auto name = make_v<ll>(__VA_ARGS__)
#define vnb(name, ...) auto name = make_v<bool>(__VA_ARGS__)
#define vns(name, ...) auto name = make_v<string>(__VA_ARGS__)
#define vnd(name, ...) auto name = make_v<double>(__VA_ARGS__)
#define vnc(name, ...) auto name = make_v<char>(__VA_ARGS__)
#define vnp(name, ...) auto name = make_v<P>(__VA_ARGS__)
#define vn(type, name, ...) auto name = make_v<type>(__VA_ARGS__)
#define PQ priority_queue<ll, vector<ll>, greater<ll>>
#define tos to_string
using mapi = map<ll, ll>;
using mapp = map<P, ll>;
using mapd = map<dou, ll>;
using mapc = map<char, ll>;
using maps = map<str, ll>;
using seti = set<ll>;
using setd = set<dou>;
using setc = set<char>;
using sets = set<str>;
using qui = queue<ll>;
#define uset unordered_set
#define useti unordered_set<ll, xorshift>
#define mset multiset
#define mseti multiset<ll>
#define umap unordered_map
#define mmap multimap
// 任意のマクロサポート用 使う度に初期化する
int index_, v1_, v2_, v3_;
template <class T> struct pq {
priority_queue<T, vector<T>, greater<T>> q; /*小さい順*/
T su = 0;
void clear() {
q = priority_queue<T, vector<T>, greater<T>>();
su = 0;
}
void operator+=(T v) {
su += v;
q.push(v);
}
T sum() { return su; }
T top() { return q.top(); }
void pop() {
su -= q.top();
q.pop();
}
T poll() {
T ret = q.top();
su -= ret;
q.pop();
return ret;
}
ll size() { return q.size(); }
};
template <class T> struct pqg {
priority_queue<T> q; /*大きい順*/
T su = 0;
void clear() {
q = priority_queue<T>();
su = 0;
}
void operator+=(T v) {
su += v;
q.push(v);
}
T sum() { return su; }
T top() { return q.top(); }
void pop() {
su -= q.top();
q.pop();
}
T poll() {
T ret = q.top();
su -= ret;
q.pop();
return ret;
}
ll size() { return q.size(); }
};
#define pqi pq<ll>
#define pqgi pqg<ll>
// マクロ 繰り返し
// ↓@オーバーロード隔離
#define o_rep(o1, o2, o3, o4, name, ...) name
#define rep1(n) for (ll rep1i = 0, rep1lim = n; rep1i < rep1lim; ++rep1i)
#define rep2(i, n) for (ll i = 0, rep2lim = n; i < rep2lim; ++i)
#define rep3(i, m, n) for (ll i = m, rep3lim = n; i < rep3lim; ++i)
#define rep4(i, m, n, ad) for (ll i = m, rep4lim = n; i < rep4lim; i += ad)
// 逆順 閉区間
#define rer2(i, n) for (ll i = n; i >= 0; i--)
#define rer3(i, m, n) for (ll i = m, rer3lim = n; i >= rer3lim; i--)
#define rer4(i, m, n, dec) for (ll i = m, rer4lim = n; i >= rer4lim; i -= dec)
// ループを一つにまとめないとフォーマットで汚くなるため
#define nex_ind1(i) i++
#define nex_ind2(i, j, J) \
i = (j + 1 == J) ? i + 1 : i, j = (j + 1 == J ? 0 : j + 1)
#define nex_ind3(i, j, k, J, K) \
i = (j + 1 == J && k + 1 == K) ? i + 1 : i, \
j = (k + 1 == K) ? (j + 1 == J ? 0 : j + 1) : j, \
k = (k + 1 == K ? 0 : k + 1)
#define nex_ind4(i, j, k, l, J, K, L) \
i = (j + 1 == J && k + 1 == K && l + 1 == L) ? i + 1 : i, \
j = (k + 1 == K && l + 1 == L) ? (j + 1 == J ? 0 : j + 1) : j, \
k = (l + 1 == L ? (k + 1 == K ? 0 : k + 1) : k), l = l + 1 == L ? 0 : l + 1
#define nex_ind5(i, j, k, l, m, J, K, L, M) \
i = (j + 1 == J && k + 1 == K && l + 1 == L && m + 1 == M) ? i + 1 : i, \
j = (k + 1 == K && l + 1 == L && m + 1 == M) ? (j + 1 == J ? 0 : j + 1) : j, \
k = (l + 1 == L && m + 1 == M ? (k + 1 == K ? 0 : k + 1) : k), \
l = m + 1 == M ? l + 1 == L ? 0 : l + 1 : l, m = m + 1 == M ? 0 : m + 1
#define repss1(i, I) for (int i = 0; i < I; i++)
#define repss2(i, j, I, J) \
for (int i = (J ? 0 : I), j = 0; i < I; nex_ind2(i, j, J))
#define repss3(i, j, k, I, J, K) \
for (int i = (J && K ? 0 : I), j = 0, k = 0; i < I; nex_ind3(i, j, k, J, K))
#define repss4(i, j, k, l, I, J, K, L) \
for (int i = (J && K && L ? 0 : I), j = 0, k = 0, l = 0; i < I; \
nex_ind4(i, j, k, l, J, K, L))
#define repss5(i, j, k, l, m, I, J, K, L, M) \
for (int i = (J && K && L && M ? 0 : I), j = 0, k = 0, l = 0, m = 0; i < I; \
nex_ind5(i, j, k, l, m, J, K, L, M))
#define o_repss(o1, o2, o3, o4, o5, o6, o7, o8, o9, o10, name, ...) name
// i,j,k...をnまで見る
#define reps1(i, n) repss1(i, n)
#define reps2(i, j, n) repss2(i, j, n, n)
#define reps3(i, j, k, n) repss3(i, j, k, n, n, n)
#define reps4(i, j, k, l, n) repss4(i, j, k, l, n, n, n, n)
#define o_reps(o1, o2, o3, o4, o5, name, ...) name
template <class T>
void nex_repv2(int &i, int &j, int &I, int &J, vector<vector<T>> &s) {
while (1) {
j++;
if (j >= J) {
j = 0;
i++;
if (i < I) {
J = (int)s[i].size();
}
}
if (i >= I || J)
return;
}
}
template <class T>
void nex_repv3(int &i, int &j, int &k, int &I, int &J, int &K,
vector<vector<vector<T>>> &s) {
while (1) {
k++;
if (k >= K) {
k = 0;
j++;
if (j >= J) {
j = 0;
i++;
if (i >= I)
return;
}
}
J = (int)s[i].size();
K = (int)s[i][j].size();
if (J && K)
return;
}
}
#define repv_1(i, a) repss1(i, sz(a))
// 正方形である必要はない
// 直前を持つのとどっちが早いか
#define repv_2(i, j, a) \
for (int I = (int)a.size(), J = (int)a[0].size(), i = 0, j = 0; i < I; \
nex_repv2(i, j, I, J, a))
// 箱状になっている事が要求される つまり[i] 次元目の要素数は一定
#define repv_3(i, j, k, a) \
for (int I = (int)a.size(), J = (int)a[0].size(), K = (int)a[0][0].size(), \
i = 0, j = 0, k = 0; \
i < I; nex_repv3(i, j, k, I, J, K, a))
#define repv_4(i, j, k, l, a) \
repss4(i, j, k, l, sz(a), sz(a[0]), sz(a[0][0]), sz(a[0][0][0]))
#define repv_5(i, j, k, l, m, a) \
repss5(i, j, k, l, m, sz(a), sz(a[0]), sz(a[0][0]), sz(a[0][0][0]), \
sz(a[0][0][0][0]))
#define o_repv(o1, o2, o3, o4, o5, o6, name, ...) name
template <typename T> struct has_rbegin_rend {
private:
template <typename U>
static auto check(U &&obj)
-> decltype(std::rbegin(obj), std::rend(obj), std::true_type{});
static std::false_type check(...);
public:
static constexpr bool value = decltype(check(std::declval<T>()))::value;
};
template <typename T>
constexpr bool has_rbegin_rend_v = has_rbegin_rend<T>::value;
template <typename Iterator> class Range {
public:
Range(Iterator &&begin, Iterator &&end) noexcept
: m_begin(std::forward<Iterator>(begin)),
m_end(std::forward<Iterator>(end)) {}
Iterator begin() const noexcept { return m_begin; }
Iterator end() const noexcept { return m_end; }
private:
const Iterator m_begin;
const Iterator m_end;
};
template <typename Iterator>
static inline Range<Iterator> makeRange(Iterator &&begin,
Iterator &&end) noexcept {
return Range<Iterator>{std::forward<Iterator>(begin),
std::forward<Iterator>(end)};
}
template <typename T>
static inline decltype(auto)
makeReversedRange(const std::initializer_list<T> &iniList) noexcept {
return makeRange(std::rbegin(iniList), std::rend(iniList));
}
template <typename T, typename std::enable_if_t<has_rbegin_rend_v<T>,
std::nullptr_t> = nullptr>
static inline decltype(auto) makeReversedRange(T &&c) noexcept {
return makeRange(std::rbegin(c), std::rend(c));
} /* rbegin(),
rend()を持たないものはこっちに分岐させて,エラーメッセージを少なくする*/
template <typename T, typename std::enable_if<!has_rbegin_rend<T>::value,
std::nullptr_t>::type = nullptr>
static inline void makeReversedRange(T &&) noexcept {
static_assert(has_rbegin_rend<T>::value,
"Specified argument doesn't have reverse iterator.");
}
#define form1(st) \
decltype(st)::key_type k; \
decltype(st)::mapped_type v; \
for (auto &&it = st.begin(); \
it != st.end() && (k = (*it).fi, v = (*it).se, true); ++it)
#define form3(k, v, st) \
decltype(st)::key_type k; \
decltype(st)::mapped_type v; \
for (auto &&it = st.begin(); \
it != st.end() && (k = (*it).fi, v = (*it).se, true); ++it)
#define form4(k, v, st, r) \
decltype(st)::key_type k; \
decltype(st)::mapped_type v; \
for (auto &&it = st.begin(); \
it != st.end() && (k = (*it).fi, v = (*it).se, (*it).fi < r); ++it)
#define form5(k, v, st, l, r) \
decltype(st)::key_type k; \
decltype(st)::mapped_type v; \
for (auto &&it = st.lower_bound(l); \
it != st.end() && (k = (*it).fi, v = (*it).se, (*it).fi < r); ++it)
#define o_form(a, b, c, d, e, name, ...) name
template <class U> vector<U> to1d(vector<U> &a) { return a; }
template <class U> vector<U> to1d(V<V<U>> &a) {
vector<U> res;
for (auto &&a1 : a)
for (auto &&a2 : a1)
res.push_back(a2);
return res;
}
template <class U> vector<U> to1d(V<V<V<U>>> &a) {
vector<U> res;
for (auto &&a1 : a)
for (auto &&a2 : a1)
for (auto &&a3 : a2)
res.push_back(a3);
return res;
}
template <class U> vector<U> to1d(V<V<V<V<U>>>> &a) {
vector<U> res;
for (auto &&a1 : a)
for (auto &&a2 : a1)
for (auto &&a3 : a2)
for (auto &&a4 : a3)
res.push_back(a4);
return res;
}
template <class U> vector<U> to1d(V<V<V<V<V<U>>>>> &a) {
vector<U> res;
for (auto &&a1 : a)
for (auto &&a2 : a1)
for (auto &&a3 : a2)
for (auto &&a4 : a3)
for (auto &&a5 : a4)
res.push_back(a5);
return res;
}
template <class U> vector<U> to1d(V<V<V<V<V<V<U>>>>>> &a) {
vector<U> res;
for (auto &&a1 : a)
for (auto &&a2 : a1)
for (auto &&a3 : a2)
for (auto &&a4 : a3)
for (auto &&a5 : a4)
for (auto &&a6 : a5)
res.push_back(a6);
return res;
}
// ↑@オーバーロード隔離
// rep系はインデックス、for系は中身
#define rep(...) o_rep(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rer(...) o_rep(__VA_ARGS__, rer4, rer3, rer2, )(__VA_ARGS__)
// char用のrep
#define repc(i, m, n) for (char i = m, repc3lim = n; i < repc3lim; ++i)
// i,j,k...をnまで見る
#define reps(...) o_reps(__VA_ARGS__, reps4, reps3, reps2, reps1, )(__VA_ARGS__)
#define repss(...) \
o_repss(__VA_ARGS__, repss5, a, repss4, a, repss3, a, repss2, a, \
repss1)(__VA_ARGS__)
// vectorのindexを走査する
// repv(i,j,vvi)
#define repv(...) \
o_repv(__VA_ARGS__, repv_5, repv_4, repv_3, repv_2, repv_1, )(__VA_ARGS__)
// 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(a, b) for (auto &&a : b)
#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]))
// 0の左と nの右
#define forlr(v, d, lng, rng) \
v1_ = lng, v2_ = rng; \
for (auto prev = v1_, 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 ? v2_ : d[index_ + 1]))
#define form(...) \
o_form(__VA_ARGS__, form5, form4, form3, form2, form1)(__VA_ARGS__)
#define forp(k, v, m) \
decltype(m)::value_type::first_type k; \
decltype(m)::value_type::second_type v; \
for (auto &&it = m.begin(); \
it != m.end() && (k = (*it).fi, v = (*it).se, true); ++it)
// マクロ 定数
#define k3 1010
#define k4 10101
#define k5 101010
#define k6 1010101
#define k7 10101010
const ll inf = (ll)1e9 + 100;
const ll linf = (ll)1e18 + 100;
const char infc = '{';
const string infs = "{";
const double eps = 1e-9;
const double PI = 3.1415926535897932384626433832795029L;
// マクロ省略形 関数等
#define arsz(a) (sizeof(a) / sizeof(a[0]))
#define sz(a) ((ll)(a).size())
#define mp make_pair
#define pb pop_back
#define pf push_front
#define eb emplace_back
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
constexpr bool ev(ll a) { return !(a & 1); }
constexpr bool od(ll a) { return (a & 1); }
//@拡張系 こう出来るべきというもの
// 埋め込み 存在を意識せずに機能を増やされているもの
namespace std {
template <> class hash<std::pair<signed, signed>> {
public:
size_t operator()(const std::pair<signed, signed> &x) const {
return hash<ll>()(((ll)x.first << 32) | x.second);
}
};
template <> class hash<std::pair<ll, ll>> {
public
: /*大きいllが渡されると、<<32でオーバーフローするがとりあえず問題ないと判断*/
size_t operator()(const std::pair<ll, ll> &x) const {
return hash<ll>()(((ll)x.first << 32) | x.second);
}
};
} // namespace std
// stream まとめ
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, typename U>
ostream &operator<<(ostream &os, vector<pair<T, U>> &vec) {
for (ll i = 0; i < vec.size(); ++i) {
os << vec[i];
if (i != vec.size() - 1)
os << endl;
}
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 <typename W, typename H> void resize(W &vec, const H head) {
vec.resize(head);
}
template <typename W, typename H, typename... T>
void resize(W &vec, const H &head, const T... tail) {
vec.resize(head);
for (auto &v : vec)
resize(v, tail...);
}
// template<typename W, typename H> void resize(vector<W> &vec, const H head) {
// vec.resize(head); } template<typename W, typename H, typename ... T> void
// resize(vector<W> &vec, const H &head, const T ... tail) {vec.resize(head);for
// (auto &v: vec)resize(v, tail...);}
template <typename T, typename F> bool all_of2(T &v, F f) { return f(v); }
template <typename T, typename F> bool all_of2(vector<T> &v, F f) {
rep(i, sz(v)) {
if (!all_of2(v[i], f))
return false;
}
return true;
}
template <typename T, typename F> bool any_of2(T &v, F f) { return f(v); }
template <typename T, typename F> bool any_of2(vector<T> &v, F f) {
rep(i, sz(v)) {
if (any_of2(v[i], f))
return true;
}
return false;
}
template <typename T, typename F> bool none_of2(T &v, F f) { return f(v); }
template <typename T, typename F> bool none_of2(vector<T> &v, F f) {
rep(i, sz(v)) {
if (none_of2(v[i], f))
return false;
}
return true;
}
template <typename T, typename F> bool find_if2(T &v, F f) { return f(v); }
template <typename T, typename F> ll find_if2(vector<T> &v, F f) {
rep(i, sz(v)) {
if (find_if2(v[i], f))
return i;
}
return sz(v);
}
template <typename T, typename F> bool rfind_if2(T &v, F f) { return f(v); }
template <typename T, typename F> ll rfind_if2(vector<T> &v, F f) {
rer(i, sz(v) - 1) {
if (rfind_if2(v[i], f))
return i;
}
return -1;
}
template <class T> bool contains(string &s, const T &v) {
return s.find(v) != string::npos;
}
template <typename T> bool contains(vector<T> &v, const T &val) {
return std::find(v.begin(), v.end(), val) != v.end();
}
template <typename T, typename F> bool contains_if2(vector<T> &v, F f) {
return find_if(v.begin(), v.end(), f) != v.end();
}
template <typename T, typename F> ll count_if2(T &v, F f) { return f(v); }
template <typename T, typename F> ll count_if2(vector<T> &vec, F f) {
ll ret = 0;
fora(v, vec) ret += count_if2(v, f);
return ret;
}
template <typename T, typename F> void for_each2(T &v, F f) { f(v); }
template <typename T, typename F> void for_each2(vector<T> &vec, F f) {
fora(v, vec) for_each2(v, f);
}
template <typename W> ll count_od(vector<W> &a) {
return count_if2(a, [](ll v) { return v & 1; });
}
template <typename W> ll count_ev(vector<W> &a) {
return count_if2(a, [](ll v) { return !(v & 1); });
}
// 削除された要素の数を返す
template <typename T, typename F> int erase_if2(vector<T> &v, F f) {
vector<T> nv;
int cou = 0;
rep(i, sz(v)) {
if (f(v[i])) {
cou++;
} else {
nv.push_back(v[i]);
}
}
v = nv;
return cou;
}
template <typename T, typename F> int erase_if2(vector<vector<T>> &v, F f) {
int cou = 0;
rep(i, sz(v)) { cou += erase_if2(v[i], f); }
return cou;
}
#define all_of(a, right) all_of2(a, lam(right))
#define all_of_f(a, f) all_of2(a, f)
#define any_of(a, right) any_of2(a, lam(right))
#define any_of_f(a, f) any_of2(a, f)
#define none_of(a, right) none_of2(a, lam(right))
#define none_of_f(a, f) none_of2(a, f)
#define find_if(a, right) find_if2(a, lam(right))
#define find_if_f(a, f) find_if2(a, f)
#define rfind_if(a, right) rfind_if2(a, lam(right))
#define rfind_if_f(a, f) rfind_if2(a, f)
#define contains_if(a, right) contains_if2(a, lam(right))
#define contains_if_f(a, f) contains_if2(a, f)
#define count_if(a, right) count_if2(a, lam(right))
#define count_if_f(a, f) count_if2(a, f)
#define for_each(a, right) \
do { \
fora(v, a) { v right; } \
} while (0)
#define for_each_f(a, f) \
do { \
fora(v, a) { f(v); } \
} while (0)
#define erase_if(a, right) erase_if2(a, lam(right))
#define erase_if_f(a, f) erase_if2(a, f)
template <class T, class U> void replace(vector<T> &a, T key, U v) {
replace(a.begin(), a.end(), key, v);
}
void replace(str &a, char key, str v) {
if (v == "")
a.erase(remove(all(a), key), a.end());
}
void replace(str &a, char key, char v) { replace(all(a), key, v); }
// keyと同じかどうか01で置き換える
template <class T, class U> void replace(vector<T> &a, U k) {
rep(i, sz(a)) a[i] = a[i] == k;
}
template <class T, class U> void replace(vector<vector<T>> &a, U k) {
rep(i, sz(a)) rep(j, sz(a[0])) a[i][j] = a[i][j] == k;
}
template <class T> void replace(T &a) { replace(a, '#'); }
void replace(str &a, str key, str v) {
stringstream t;
ll kn = sz(key);
std::string::size_type Pos(a.find(key));
ll l = 0;
while (Pos != std::string::npos) {
t << a.substr(l, Pos - l);
t << v;
l = Pos + kn;
Pos = a.find(key, Pos + kn);
}
t << a.substr(l, sz(a) - l);
a = t.str();
}
template <class T> bool includes(vector<T> &a, vector<T> &b) {
vi c = a;
vi d = b;
sort(all(c));
sort(all(d));
return includes(all(c), all(d));
}
template <class T> bool is_permutation(vector<T> &a, vector<T> &b) {
return is_permutation(all(a), all(b));
}
template <class T> bool next_permutation(vector<T> &a) {
return next_permutation(all(a));
}
void iota(vector<ll> &ve, ll s, ll n) {
ve.resize(n);
iota(all(ve), s);
}
vi iota(ll s, ll len) {
vi ve(len);
iota(all(ve), s);
return ve;
}
template <class A, class B> auto vtop(vector<A> &a, vector<B> &b) {
assert(sz(a) == sz(b)); /*stringを0で初期化できない */
vector<pair<A, B>> res;
rep(i, sz(a)) res.eb(a[i], b[i]);
return res;
}
template <class A, class B>
void ptov(vector<pair<A, B>> &p, vector<A> &a, vector<B> &b) {
a.resize(sz(p)), b.resize(sz(p));
rep(i, sz(p)) a[i] = p[i].fi, b[i] = p[i].se;
}
template <class A, class B, class C>
auto vtot(vector<A> &a, vector<B> &b, vector<C> &c) {
assert(sz(a) == sz(b) && sz(b) == sz(c));
vector<T2<A, B, C>> res;
rep(i, sz(a)) res.eb(a[i], b[i], c[i]);
return res;
}
template <class A, class B, class C, class D>
auto vtof(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) {
assert(sz(a) == sz(b) && sz(b) == sz(c) && sz(c) == sz(d));
vector<F2<A, B, C, D>> res;
rep(i, sz(a)) res.eb(a[i], b[i], c[i], d[i]);
return res;
}
enum pcomparator { fisi, fisd, fdsi, fdsd, sifi, sifd, sdfi, sdfd };
enum tcomparator {
fisiti,
fisitd,
fisdti,
fisdtd,
fdsiti,
fdsitd,
fdsdti,
fdsdtd,
fitisi,
fitisd,
fitdsi,
fitdsd,
fdtisi,
fdtisd,
fdtdsi,
fdtdsd,
sifiti,
sifitd,
sifdti,
sifdtd,
sdfiti,
sdfitd,
sdfdti,
sdfdtd,
sitifi,
sitifd,
sitdfi,
sitdfd,
sdtifi,
sdtifd,
sdtdfi,
sdfdfd,
tifisi,
tifisd,
tifdsi,
tifdsd,
tdfisi,
tdfisd,
tdfdsi,
tdfdsd,
tisifi,
tisifd,
tisdfi,
tisdfd,
tdsifi,
tdsifd,
tdsdfi,
tdsdfd
};
template <class A, class B> void sort(vector<pair<A, B>> &a, pcomparator type) {
typedef pair<A, B> U;
if (type == fisi)
sort(all(a),
[&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se < r.se; });
else if (type == fisd)
sort(all(a),
[&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se > r.se; });
else if (type == fdsi)
sort(all(a),
[&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se < r.se; });
else if (type == fdsd)
sort(all(a),
[&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se > r.se; });
else if (type == sifi)
sort(all(a),
[&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi < r.fi; });
else if (type == sifd)
sort(all(a),
[&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi > r.fi; });
else if (type == sdfi)
sort(all(a),
[&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi < r.fi; });
else if (type == sdfd)
sort(all(a),
[&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi > r.fi; });
};
template <class U> void sort(vector<U> &a, pcomparator type) {
if (type == fisi)
sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s < r.s; });
else if (type == fisd)
sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s > r.s; });
else if (type == fdsi)
sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s < r.s; });
else if (type == fdsd)
sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s > r.s; });
else if (type == sifi)
sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f < r.f; });
else if (type == sifd)
sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f > r.f; });
else if (type == sdfi)
sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f < r.f; });
else if (type == sdfd)
sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f > r.f; });
};
template <class A, class B, class C, class D>
void sort(vector<F2<A, B, C, D>> &a, pcomparator type) {
typedef F2<A, B, C, D> U;
if (type == fisi)
sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b < r.b; });
else if (type == fisd)
sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b > r.b; });
else if (type == fdsi)
sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b < r.b; });
else if (type == fdsd)
sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b > r.b; });
else if (type == sifi)
sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a < r.a; });
else if (type == sifd)
sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a > r.a; });
else if (type == sdfi)
sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a < r.a; });
else if (type == sdfd)
sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a > r.a; });
};
template <class U> void sort(vector<U> &a, tcomparator type) {
if (type == 0)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t < r.t;
});
else if (type == 1)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t > r.t;
});
else if (type == 2)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t < r.t;
});
else if (type == 3)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t > r.t;
});
else if (type == 4)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t < r.t;
});
else if (type == 5)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t > r.t;
});
else if (type == 6)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t < r.t;
});
else if (type == 7)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t > r.t;
});
else if (type == 8)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s < r.s;
});
else if (type == 9)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s > r.s;
});
else if (type == 10)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s < r.s;
});
else if (type == 11)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s > r.s;
});
else if (type == 12)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s < r.s;
});
else if (type == 13)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s > r.s;
});
else if (type == 14)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s < r.s;
});
else if (type == 15)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s > r.s;
});
else if (type == 16)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t < r.t;
});
else if (type == 17)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t > r.t;
});
else if (type == 18)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t < r.t;
});
else if (type == 19)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t > r.t;
});
else if (type == 20)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t < r.t;
});
else if (type == 21)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t > r.t;
});
else if (type == 22)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t < r.t;
});
else if (type == 23)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t > r.t;
});
else if (type == 24)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f < r.f;
});
else if (type == 25)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f > r.f;
});
else if (type == 26)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f < r.f;
});
else if (type == 27)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f > r.f;
});
else if (type == 28)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f < r.f;
});
else if (type == 29)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f > r.f;
});
else if (type == 30)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f < r.f;
});
else if (type == 31)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f > r.f;
});
else if (type == 32)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s < r.s;
});
else if (type == 33)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s > r.s;
});
else if (type == 34)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s < r.s;
});
else if (type == 35)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s > r.s;
});
else if (type == 36)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s < r.s;
});
else if (type == 37)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s > r.s;
});
else if (type == 38)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s < r.s;
});
else if (type == 39)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s > r.s;
});
else if (type == 40)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f < r.f;
});
else if (type == 41)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f > r.f;
});
else if (type == 42)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f < r.f;
});
else if (type == 43)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f > r.f;
});
else if (type == 44)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f < r.f;
});
else if (type == 45)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f > r.f;
});
else if (type == 46)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f < r.f;
});
else if (type == 47)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f > r.f;
});
}
template <class A, class B, class C, class D>
void sort(vector<F2<A, B, C, D>> &a, tcomparator type) {
typedef F2<A, B, C, D> U;
if (type == 0)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c < r.c;
});
else if (type == 1)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c > r.c;
});
else if (type == 2)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c < r.c;
});
else if (type == 3)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c > r.c;
});
else if (type == 4)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c < r.c;
});
else if (type == 5)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c > r.c;
});
else if (type == 6)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c < r.c;
});
else if (type == 7)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c > r.c;
});
else if (type == 8)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b < r.b;
});
else if (type == 9)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b > r.b;
});
else if (type == 10)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b < r.b;
});
else if (type == 11)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b > r.b;
});
else if (type == 12)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b < r.b;
});
else if (type == 13)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b > r.b;
});
else if (type == 14)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b < r.b;
});
else if (type == 15)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b > r.b;
});
else if (type == 16)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c < r.c;
});
else if (type == 17)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c > r.c;
});
else if (type == 18)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c < r.c;
});
else if (type == 19)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c > r.c;
});
else if (type == 20)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c < r.c;
});
else if (type == 21)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c > r.c;
});
else if (type == 22)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c < r.c;
});
else if (type == 23)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c > r.c;
});
else if (type == 24)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a < r.a;
});
else if (type == 25)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a > r.a;
});
else if (type == 26)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a < r.a;
});
else if (type == 27)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a > r.a;
});
else if (type == 28)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a < r.a;
});
else if (type == 29)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a > r.a;
});
else if (type == 30)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a < r.a;
});
else if (type == 31)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a > r.a;
});
else if (type == 32)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b < r.b;
});
else if (type == 33)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b > r.b;
});
else if (type == 34)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b < r.b;
});
else if (type == 35)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b > r.b;
});
else if (type == 36)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b < r.b;
});
else if (type == 37)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b > r.b;
});
else if (type == 38)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b < r.b;
});
else if (type == 39)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b > r.b;
});
else if (type == 40)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a < r.a;
});
else if (type == 41)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a > r.a;
});
else if (type == 42)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a < r.a;
});
else if (type == 43)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a > r.a;
});
else if (type == 44)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a < r.a;
});
else if (type == 45)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a > r.a;
});
else if (type == 46)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a < r.a;
});
else if (type == 47)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a > r.a;
});
}
void sort(string &a) { sort(all(a)); }
template <class T> void sort(vector<T> &a) { sort(all(a)); }
// P l, P rで f(P) の形で渡す
template <class U, class F> void sort(vector<U> &a, F f) {
sort(all(a), [&](U l, U r) { return f(l) < f(r); });
};
template <class T> void rsort(vector<T> &a) { sort(all(a), greater<T>()); };
template <class U, class F> void rsort(vector<U> &a, F f) {
sort(all(a), [&](U l, U r) { return f(l) > f(r); });
};
// F = T<T>
// 例えばreturn p.fi + p.se;
template <class A, class B> void sortp(vector<A> &a, vector<B> &b) {
auto c = vtop(a, b);
sort(c);
rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se;
}
template <class A, class B, class F>
void sortp(vector<A> &a, vector<B> &b, F f) {
auto c = vtop(a, b);
sort(c, f);
rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se;
}
template <class A, class B> void rsortp(vector<A> &a, vector<B> &b) {
auto c = vtop(a, b);
rsort(c);
rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second;
}
template <class A, class B, class F>
void rsortp(vector<A> &a, vector<B> &b, F f) {
auto c = vtop(a, b);
rsort(c, f);
rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second;
}
template <class A, class B, class C>
void sortt(vector<A> &a, vector<B> &b, vector<C> &c) {
auto d = vtot(a, b, c);
sort(d);
rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t;
}
template <class A, class B, class C, class F>
void sortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) {
auto d = vtot(a, b, c);
sort(d, f);
rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t;
}
template <class A, class B, class C>
void rsortt(vector<A> &a, vector<B> &b, vector<C> &c) {
auto d = vtot(a, b, c);
rsort(d);
rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t;
}
template <class A, class B, class C, class F>
void rsortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) {
auto d = vtot(a, b, c);
rsort(d, f);
rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t;
}
template <class A, class B, class C, class D>
void sortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) {
auto e = vtof(a, b, c, d);
sort(e);
rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d;
}
template <class A, class B, class C, class D>
void rsortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) {
auto e = vtof(a, b, c, d);
rsort(e);
rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d;
}
// sortindex 元のvectorはソートしない
template <class T> vi sorti(vector<T> &a) {
auto b = a;
vi ind = iota(0, sz(a));
sortp(b, ind);
return ind;
} /*indexの分で型が変わるためpcomparatorが必要*/
template <class T> vi sorti(vector<T> &a, pcomparator f) {
auto b = a;
vi ind = iota(0, sz(a));
sortp(b, ind, f);
return ind;
}
template <class T, class F> vi sorti(vector<T> &a, F f) {
vi ind = iota(0, sz(a));
sort(all(ind), [&](ll x, ll y) { return f(a[x]) < f(a[y]); });
return ind;
}
template <class T> vi rsorti(vector<T> &a) {
auto b = a;
vi ind = iota(0, sz(a));
rsortp(b, ind);
return ind;
}
template <class T, class F> vi rsorti(vector<T> &a, F f) {
vi ind = iota(0, sz(a));
sort(all(ind), [&](ll x, ll y) { return f(a[x]) > f(a[y]); });
return ind;
}
template <class A, class B, class F>
vi sortpi(vector<A> &a, vector<B> &b, F f) {
auto c = vtop(a, b);
vi ind = iota(0, sz(a));
sort(all(ind), [&](ll x, ll y) { return f(c[x]) < f(c[y]); });
return ind;
}
template <class A, class B>
vi sortpi(vector<A> &a, vector<B> &b, pcomparator f) {
vi ind = iota(0, sz(a));
auto c = a;
auto d = b;
sortt(c, d, ind, f);
return ind;
}
template <class A, class B> vi sortpi(vector<A> &a, vector<B> &b) {
return sortpi(a, b, fisi);
};
template <class A, class B, class F>
vi rsortpi(vector<A> &a, vector<B> &b, F f) {
auto c = vtop(a, b);
vi ind = iota(0, sz(a));
sort(all(ind), [&](ll x, ll y) { return f(c[x]) > f(c[y]); });
return ind;
}
template <class A, class B> vi rsortpi(vector<A> &a, vector<B> &b) {
return sortpi(a, b, fdsd);
};
template <class A, class B, class C, class F>
vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) {
auto d = vtot(a, b, c);
vi ind = iota(0, sz(a));
sort(all(ind), [&](ll x, ll y) { return f(d[x]) < f(d[y]); });
return ind;
}
template <class A, class B, class C>
vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, pcomparator f) {
vi ind = iota(0, sz(a));
auto d = vtof(a, b, c, ind);
sort(d, f);
rep(i, sz(a)) ind[i] = d[i].d;
return ind;
}
template <class A, class B, class C>
vi sortti(vector<A> &a, vector<B> &b, vector<C> &c) {
vi ind = iota(0, sz(a));
sort(all(ind), [&](ll x, ll y) {
if (a[x] == a[y]) {
if (b[x] == b[y])
return c[x] < c[y];
else
return b[x] < b[y];
} else {
return a[x] < a[y];
}
});
return ind;
}
template <class A, class B, class C, class F>
vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) {
auto d = vtot(a, b, c);
vi ind = iota(0, sz(a));
sort(all(ind), [&](ll x, ll y) { return f(d[x]) > f(d[y]); });
return ind;
}
template <class A, class B, class C>
vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c) {
vi ind = iota(0, sz(a));
sort(all(ind), [&](ll x, ll y) {
if (a[x] == a[y]) {
if (b[x] == b[y])
return c[x] > c[y];
else
return b[x] > b[y];
} else {
return a[x] > a[y];
}
});
return ind;
}
template <class T> void sort2(vector<vector<T>> &a) {
for (ll i = 0, n = a.size(); i < n; ++i)
sort(a[i]);
}
template <class T> void rsort2(vector<vector<T>> &a) {
for (ll i = 0, n = a.size(); i < n; ++i)
rsort(a[i]);
}
template <typename A, size_t N, typename T> void fill(A (&a)[N], const T &v) {
rep(i, N) a[i] = v;
}
template <typename A, size_t N, size_t O, typename T>
void fill(A (&a)[N][O], const T &v) {
rep(i, N) rep(j, O) a[i][j] = v;
}
template <typename A, size_t N, size_t O, size_t P, typename T>
void fill(A (&a)[N][O][P], const T &v) {
rep(i, N) rep(j, O) rep(k, P) a[i][j][k] = v;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, typename T>
void fill(A (&a)[N][O][P][Q], const T &v) {
rep(i, N) rep(j, O) rep(k, P) rep(l, Q) a[i][j][k][l] = v;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R,
typename T>
void fill(A (&a)[N][O][P][Q][R], const T &v) {
rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) a[i][j][k][l][m] = v;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R,
size_t S, typename T>
void fill(A (&a)[N][O][P][Q][R][S], const T &v) {
rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) rep(n, S)
a[i][j][k][l][m][n] = v;
}
template <typename W, typename T> void fill(W &xx, const T vall) { xx = vall; }
template <typename W, typename T> void fill(vector<W> &vecc, const T vall) {
for (auto &&vx : vecc)
fill(vx, vall);
}
template <typename W, typename T> void fill(vector<W> &xx, ll len, const T v) {
rep(i, len) xx[i] = v;
}
template <typename W, typename T>
void fill(vector<vector<W>> &xx, int sh, int th, int sw, int tw, T v) {
rep(h, sh, th) rep(w, sw, tw) xx[h][w] = v;
}
template <class T, class U> void fill(vector<T> &a, vi &ind, U val) {
fora(v, ind) a[v] = val;
}
template <typename A, size_t N> A sum(A (&a)[N]) {
A res = 0;
rep(i, N) res += a[i];
return res;
}
template <typename A, size_t N, size_t O> A sum(A (&a)[N][O]) {
A res = 0;
rep(i, N) rep(j, O) res += a[i][j];
return res;
}
template <typename A, size_t N, size_t O, size_t P> A sum(A (&a)[N][O][P]) {
A res = 0;
rep(i, N) rep(j, O) rep(k, P) res += a[i][j][k];
return res;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q>
A sum(A (&a)[N][O][P][Q]) {
A res = 0;
rep(i, N) rep(j, O) rep(k, P) rep(l, Q) res += a[i][j][k][l];
return res;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R>
A sum(A (&a)[N][O][P][Q][R]) {
A res = 0;
rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) res += a[i][j][k][l][m];
return res;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R,
size_t S>
A sum(A (&a)[N][O][P][Q][R][S]) {
A res = 0;
rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) rep(n, S) res +=
a[i][j][k][l][m][n];
return res;
}
//@汎用便利関数 入力
ll in() {
ll ret;
cin >> ret;
return ret;
}
string sin() {
string ret;
cin >> ret;
return ret;
}
template <class T> void in(T &head) { cin >> head; }
template <class T, class... U> void in(T &head, U &...tail) {
cin >> head;
in(tail...);
}
#define o_din(o1, o2, o3, o4, o5, o6, name, ...) name
#define din1(a) \
ll a; \
cin >> a
#define din2(a, b) \
ll a, b; \
cin >> a >> b
#define din3(a, b, c) \
ll a, b, c; \
cin >> a >> b >> c
#define din4(a, b, c, d) \
ll a, b, c, d; \
cin >> a >> b >> c >> d
#define din5(a, b, c, d, e) \
ll a, b, c, d, e; \
cin >> a >> b >> c >> d >> e
#define din6(a, b, c, d, e, f) \
ll a, b, c, d, e, f; \
cin >> a >> b >> c >> d >> e >> f
#define din(...) \
o_din(__VA_ARGS__, din6, din5, din4, din3, din2, din1)(__VA_ARGS__)
#define o_dins(o1, o2, o3, o4, o5, o6, name, ...) name
#define dins1(a) \
str a; \
cin >> a
#define dins2(a, b) \
str a, b; \
cin >> a >> b
#define dins3(a, b, c) \
str a, b, c; \
cin >> a >> b >> c
#define dins4(a, b, c, d) \
str a, b, c, d; \
cin >> a >> b >> c >> d
#define dins5(a, b, c, d, e) \
str a, b, c, d, e; \
cin >> a >> b >> c >> d >> e
#define dins6(a, b, c, d, e, f) \
str a, b, c, d, e, f; \
cin >> a >> b >> c >> d >> e >> f
#define dins(...) \
o_dins(__VA_ARGS__, dins6, dins5, dins4, dins3, dins2, dins1)(__VA_ARGS__)
#define o_dind(o1, o2, o3, o4, name, ...) name
#define din1d(a) \
din1(a); \
a--
#define din2d(a, b) \
din2(a, b); \
a--, b--
#define din3d(a, b, c) \
din3(a, b, c); \
a--, b--, c--
#define din4d(a, b, c, d) \
din4(a, b, c, d); \
a--, b--, c--, d--
#define dind(...) o_dind(__VA_ARGS__, din4d, din3d, din2d, din1d)(__VA_ARGS__)
template <class T> void out2(T &&head) { cout << head; }
template <class T, class... U> void out2(T &&head, U &&...tail) {
cout << head << " ";
out2(tail...);
}
template <class T, class... U> void out(T &&head, U &&...tail) {
cout << head << " ";
out2(tail...);
cout << "" << endl;
}
template <class T> void out(T &&head) { cout << head << endl; }
void out() { cout << "" << endl; }
#ifdef _DEBUG
template <class T> void err2(T &&head) { cerr << head; }
template <class T, class... U> void err2(T &&head, U &&...tail) {
cerr << head << " ";
err2(tail...);
}
template <class T, class... U> void err(T &&head, U &&...tail) {
cerr << head << " ";
err2(tail...);
cerr << "" << endl;
}
template <class T> void err(T &&head) { cerr << head << endl; }
void err() { cerr << "" << endl; }
template <class T> string out_m2(vector<T> &a, ll W = inf) {
stringstream ss;
if (W == inf)
W = min(sz(a), 12ll);
if (sz(a) == 0)
return ss.str();
rep(i, W) { ss << a[i] << " "; }
return ss.str();
}
template <class T>
string out_m2(vector<vector<T>> &a, ll H = inf, ll W = inf, int key = -1) {
H = min({H, sz(a), 12ll});
W = min({W, sz(a[0]), 12ll});
stringstream ss;
ss << endl;
if (key == -1)
ss << " *|";
else
ss << " " << key << "|";
rep(w, W) ss << std::right << std::setw(4) << w;
ss << "" << endl;
rep(w, W * 4 + 3) ss << "_";
ss << "" << endl;
rep(h, H) {
ss << std::right << std::setw(2) << h << "|";
rep(w, min(sz(a[h]), 12ll)) {
if (abs(a[h][w]) == linf)
ss << " e"
<< "";
else
ss << std::right << std::setw(4) << a[h][w];
}
ss << "" << endl;
}
return ss.str();
}
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();
}
template <class T> string out_m2(T &a) {
stringstream ss;
ss << a;
return ss.str();
}
template <class T> string out_m(vector<T> &a, ll W = inf) {
stringstream ss;
if (W == inf)
W = min(sz(a), 12ll);
if (sz(a) == 0)
return ss.str();
rep(i, W) { ss << a[i] << " "; }
ss << "" << endl;
return ss.str();
}
template <class T>
string out_m(vector<vector<T>> &a, ll H = inf, ll W = inf, int key = -1) {
H = min({H, sz(a), 12ll});
W = min({W, sz(a[0]), 12ll});
stringstream ss;
ss << endl;
if (key == -1)
ss << " *|";
else
ss << " " << key << "|";
rep(w, W) ss << std::right << std::setw(4) << w;
ss << "" << endl;
rep(w, W * 4 + 3) ss << "_";
ss << "" << endl;
rep(h, H) {
ss << std::right << std::setw(2) << h << "|";
rep(w, min(sz(a[h]), 12ll)) {
if (abs(a[h][w]) == linf)
ss << " e"
<< "";
else
ss << std::right << std::setw(4) << a[h][w];
}
ss << "" << endl;
}
ss << endl;
return ss.str();
}
template <class T>
string out_m(vector<vector<vector<T>>> &a, ll H = inf, ll W = inf, ll U = inf) {
stringstream ss;
if (H == inf)
H = 5;
H = min(H, sz(a));
rep(i, H) {
ss << endl;
ss << out_m(a[i], W, U, i);
}
ss << endl;
return ss.str();
}
string out_m(int a) {
stringstream ss;
ss << a << endl;
return ss.str();
}
template <class T> string out_m(T &a) {
stringstream ss;
ss << a << endl;
return ss.str();
}
template <class T> void outv(vector<T> &a, ll W = inf) {
cout << out_m(a, W) << endl;
}
template <class T>
void outv(vector<vector<T>> &a, ll H = linf, ll W = linf, int key = -1) {
cout << out_m(a, H, W, key) << endl;
}
template <class T>
void outv(vector<vector<vector<T>>> &a, ll H = linf, ll W = linf, ll U = linf) {
cout << out_m(a, H, W, U) << endl;
}
#else
template <class T> void outv(vector<T> &a, ll W = inf) {
rep(i, min(W, sz(a))) { cout << a[i] << " "; }
cout << "" << endl;
}
template <class T>
void outv(vector<vector<T>> &a, ll H = linf, ll W = linf, int key = -1) {
rep(i, min(H, sz(a))) { outv(a[i], W); }
}
template <class T>
void outv(vector<vector<vector<T>>> &a, ll H = linf, ll W = linf, ll U = linf) {
;
}
#define err(...) ;
#endif
template <class T> void outl(vector<T> &a, int n = inf) {
rep(i, min(n, sz(a))) cout << a[i] << endl;
}
// テーブルをスペースなしで出力
template <class T> void outt(vector<vector<T>> &a) {
rep(i, sz(a)) {
rep(j, sz(a[i])) { cout << a[i][j]; }
cout << endl;
}
}
// int型をbit表記で出力
void outb(int a) { cout << bitset<20>(a) << endl; }
template <class T> void na(vector<T> &a, ll n) {
a.resize(n);
rep(i, n) cin >> a[i];
}
#define dna(a, n) \
vi a(n); \
rep(dnai, n) cin >> a[dnai];
#define dnad(a, n) \
vi a(n); \
rep(dnai, n) cin >> a[dnai], a[dnai]--;
template <class T> void nao(vector<T> &a, ll n) {
a.resize(n + 1);
a[0] = 0;
rep(i, n) cin >> a[i + 1];
}
template <class T> void naod(vector<T> &a, ll n) {
a.resize(n + 1);
a[0] = 0;
rep(i, n) cin >> a[i + 1], a[i + 1]--;
}
template <class T> void nad(vector<T> &a, ll n) {
a.resize(n);
rep(i, n) cin >> a[i], a[i]--;
}
template <class T, class U> void na2(vector<T> &a, vector<U> &b, ll n) {
a.resize(n);
b.resize(n);
rep(i, n) cin >> a[i] >> b[i];
}
#define dna2(a, b, n) \
vi a(n), b(n); \
rep(dna2i, n) cin >> a[dna2i] >> b[dna2i];
template <class T, class U> void nao2(vector<T> &a, vector<U> &b, ll n) {
a.resize(n + 1);
b.resize(n + 1);
a[0] = b[0] = 0;
rep(i, n) cin >> a[i + 1] >> b[i + 1];
}
#define dna2d(a, b, n) \
vi a(n), b(n); \
rep(dna2di, n) { \
cin >> a[dna2di] >> b[dna2di]; \
a[dna2di]--, b[dna2di]--; \
}
template <class T, class U> void na2d(vector<T> &a, vector<U> &b, ll n) {
a.resize(n);
b.resize(n);
rep(i, n) cin >> a[i] >> b[i], a[i]--, b[i]--;
}
template <class T, class U, class W>
void na3(vector<T> &a, vector<U> &b, vector<W> &c, ll n) {
a.resize(n);
b.resize(n);
c.resize(n);
rep(i, n) cin >> a[i] >> b[i] >> c[i];
}
#define dna3(a, b, c, n) \
vi a(n), b(n), c(n); \
rep(dna3i, n) cin >> a[dna3i] >> b[dna3i] >> c[dna3i];
template <class T, class U, class W>
void na3d(vector<T> &a, vector<U> &b, vector<W> &c, ll n) {
a.resize(n);
b.resize(n);
c.resize(n);
rep(i, n) cin >> a[i] >> b[i] >> c[i], a[i]--, b[i]--, c[i]--;
}
#define dna3d(a, b, c, n) \
vi a(n), b(n), c(n); \
rep(dna3di, n) { \
cin >> a[dna3di] >> b[dna3di] >> c[dna3di]; \
a[dna3di]--, b[dna3di]--, c[dna3di]--; \
}
template <class T, class U, class W, class X>
void na4(vector<T> &a, vector<U> &b, vector<W> &c, vector<X> &d, ll n) {
a.resize(n);
b.resize(n);
c.resize(n);
d.resize(n);
rep(i, n) cin >> a[i] >> b[i] >> c[i] >> d[i];
}
#define dna4(a, b, c, d, n) \
vi a(n), b(n), c(n), d(n); \
rep(dna4i, n) cin >> a[dna4i] >> b[dna4i] >> c[dna4i] >> d[dna4i];
#define dna4d(a, b, c, d, n) \
vi a(n), b(n), c(n), d(n); \
rep(dna4i, n) cin >> a[dna4i] >> b[dna4i] >> c[dna4i] >> d[dna4i], \
--a[dna4i], --b[dna4i], --c[dna4i], --d[dna4i];
#define nt(a, h, w) \
resize(a, h, w); \
rep(nthi, h) rep(ntwi, w) cin >> a[nthi][ntwi];
#define ntd(a, h, w) \
resize(a, h, w); \
rep(ntdhi, h) rep(ntdwi, w) cin >> a[ntdhi][ntdwi], a[ntdhi][ntdwi]--;
#define ntp(a, h, w) \
resize(a, h + 2, w + 2); \
fill(a, '#'); \
rep(ntphi, 1, h + 1) rep(ntpwi, 1, w + 1) cin >> a[ntphi][ntpwi];
// デバッグ
#define sp << " " <<
#define deb1(x) debugName(x) << " = " << out_m2(x)
#define deb2(x, ...) deb1(x) << ", " << deb1(__VA_ARGS__)
#define deb3(x, ...) deb1(x) << ", " << deb2(__VA_ARGS__)
#define deb4(x, ...) deb1(x) << ", " << deb3(__VA_ARGS__)
#define deb5(x, ...) deb1(x) << ", " << deb4(__VA_ARGS__)
#define deb6(x, ...) deb1(x) << ", " << deb5(__VA_ARGS__)
#define deb7(x, ...) deb1(x) << ", " << deb6(__VA_ARGS__)
#define deb8(x, ...) deb1(x) << ", " << deb7(__VA_ARGS__)
#define deb9(x, ...) deb1(x) << ", " << deb8(__VA_ARGS__)
#define deb10(x, ...) deb1(x) << ", " << deb9(__VA_ARGS__)
#define o_ebug(o1, o2, o3, o4, o5, o6, o7, o8, o9, o10, name, ...) name
#ifdef _DEBUG
#define deb(...) \
cerr << o_ebug(__VA_ARGS__, deb10, deb9, deb8, deb7, deb6, deb5, deb4, deb3, \
deb2, deb1)(__VA_ARGS__) \
<< endl
#else
#define deb(...) ;
#endif
#define debugline(x) \
cerr << x << " " \
<< "(L:" << __LINE__ << ")" << '\n'
//@formatter:off
// よく使うクラス、構造体
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;
}
// 便利関数
// テスト用
char ranc() { return (char)('a' + rand() % 26); }
ll rand(ll min, ll max) {
assert(min <= max);
if (min >= 0 && max >= 0) {
return rand() % (max + 1 - min) + min;
} else if (max < 0) {
return -rand(-max, -min);
} else {
if (rand() % 2) {
return rand(0, max);
} else {
return -rand(0, -min);
}
}
}
vi ranv(ll n, ll min, ll max) {
vi v(n);
rep(i, n) v[i] = rand(min, max);
return v;
}
str ransu(ll n) {
str s;
rep(i, n) s += (char)rand('A', 'Z');
return s;
}
str ransl(ll n) {
str s;
rep(i, n) s += (char)rand('a', 'z');
return s;
}
// 単調増加
vi ranvinc(ll n, ll min, ll max) {
vi v(n);
bool bad = 1;
while (bad) {
bad = 0;
v.resize(n);
rep(i, n) {
if (i && min > max - v[i - 1]) {
bad = 1;
break;
}
if (i)
v[i] = v[i - 1] + rand(min, max - v[i - 1]);
else
v[i] = rand(min, max);
}
}
return v;
}
// 便利 汎用
void ranvlr(ll n, ll min, ll max, vi &l, vi &r) {
l.resize(n);
r.resize(n);
rep(i, n) {
l[i] = rand(min, max);
r[i] = l[i] + rand(0, max - l[i]);
}
}
vp run_length(vi &a) {
vp ret;
ret.eb(a[0], 1);
rep(i, 1, sz(a)) {
if (ret.back().fi == a[i]) {
ret.back().se++;
} else {
ret.eb(a[i], 1);
}
}
return ret;
}
vector<pair<char, ll>> run_length(string &a) {
vector<pair<char, ll>> ret;
ret.eb(a[0], 1);
rep(i, 1, sz(a)) {
if (ret.back().fi == a[i]) {
ret.back().se++;
} else {
ret.eb(a[i], 1);
}
}
return ret;
}
template <class F> ll mgr(ll ok, ll ng, F f) {
if (ok < ng)
while (ng - ok > 1) {
ll mid = (ok + ng) / 2;
if (f(mid))
ok = mid;
else
ng = mid;
}
else
while (ok - ng > 1) {
ll mid = (ok + ng) / 2;
if (f(mid))
ok = mid;
else
ng = mid;
}
return ok;
}
// strを整数として比較
string smax(str &a, str b) {
if (sz(a) < sz(b)) {
return b;
} else if (sz(a) > sz(b)) {
return a;
} else if (a < b)
return b;
else
return a;
}
// strを整数として比較
string smin(str &a, str b) {
if (sz(a) > sz(b)) {
return b;
} else if (sz(a) < sz(b)) {
return a;
} else if (a > b)
return b;
else
return a;
}
// エラー-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;
}
ll count(str &a, str k) {
ll ret = 0, len = k.length();
auto pos = a.find(k);
while (pos != string::npos)
pos = a.find(k, pos + len), ++ret;
return ret;
}
vi count(str &a) {
vi cou(26);
char c = 'a';
if ('A' <= a[0] && a[0] <= 'Z')
c = 'A';
rep(i, sz(a))++ cou[a[i] - c];
return cou;
}
#define couif count_if
// algorythm
ll rev(ll a) {
ll res = 0;
while (a) {
res *= 10;
res += a % 10;
a /= 10;
}
return res;
}
template <class T> void rev(vector<T> &a) { reverse(all(a)); }
template <class U> void rev(vector<vector<U>> &a) {
vector<vector<U>> b(sz(a[0]), vector<U>(sz(a)));
rep(h, sz(a)) rep(w, sz(a[0])) b[w][h] = a[h][w];
a = b;
}
void rev(string &a) { reverse(all(a)); }
constexpr ll p10[] = {1,
10,
100,
1000,
10000,
100000,
1000000,
10000000,
100000000,
1000000000,
10000000000ll,
100000000000ll,
1000000000000ll,
10000000000000ll,
100000000000000ll,
1000000000000000ll,
10000000000000000ll,
100000000000000000ll,
1000000000000000000ll};
ll get(ll a, ll keta) { return (a / (ll)pow(10, keta)) % 10; }
// 0は0桁
ll keta(ll v) {
if (v < p10[9]) {
if (v < p10[4]) {
if (v < p10[2]) {
if (v < p10[1]) {
if (v < p10[0])
return 0;
else
return 1;
} else
return 2;
} else {
if (v < p10[3])
return 3;
else
return 4;
}
} else {
if (v < p10[7]) {
if (v < p10[5])
return 5;
else if (v < p10[6])
return 6;
else
return 7;
} else {
if (v < p10[8])
return 8;
else
return 9;
}
}
} else {
if (v < p10[13]) {
if (v < p10[11]) {
if (v < p10[10])
return 10;
else
return 11;
} else {
if (v < p10[12])
return 12;
else
return 13;
}
} else {
if (v < p10[15]) {
if (v < p10[14])
return 14;
else
return 15;
} else {
if (v < p10[17]) {
if (v < p10[16])
return 16;
else
return 17;
} else {
if (v < p10[18])
return 18;
else
return 19;
}
}
}
}
}
ll dsum(ll v, ll sin = 10) {
ll ret = 0;
for (; v; v /= sin)
ret += v % sin;
return ret;
}
ll mask10(ll v) { return p10[v] - 1; }
// 変換系
//[v] := iとなるようなvectorを返す
// 存在しない物は-1
template <class T> auto keys(T &a) {
vector<decltype((a.begin())->fi)> res;
for (auto &&k : a)
res.push_back(k.fi);
return res;
}
template <class T> auto values(T &a) {
vector<decltype((a.begin())->se)> res;
for (auto &&k : a)
res.push_back(k.se);
return res;
}
template <class T, class U> bool chma(T &a, const U &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T, class U> bool chmi(T &a, const U &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template <class T> T min(T a, signed b) { return a < b ? a : b; }
template <class T> T max(T a, signed b) { return a < b ? b : a; }
template <class T> T min(T a, T b, T c) {
return a >= b ? b >= c ? c : b : a >= c ? c : a;
}
template <class T> T max(T a, T b, T c) {
return a <= b ? b <= c ? c : b : a <= c ? c : a;
}
template <class T> T min(vector<T> &a) { return *min_element(all(a)); }
template <class T> T mini(vector<T> &a) {
return min_element(all(a)) - a.begin();
}
template <class T> T min(vector<T> &a, ll n) {
return *min_element(a.begin(), a.begin() + min(n, sz(a)));
}
template <class T> T min(vector<T> &a, ll s, ll n) {
return *min_element(a.begin() + s, a.begin() + min(n, sz(a)));
}
template <class T> T max(vector<T> &a) { return *max_element(all(a)); }
template <class T, class U> T max(vector<T> &a, vector<U> &b) {
return max(*max_element(all(a)), *max_element(all(b)));
}
template <class T> T maxi(vector<T> &a) {
return max_element(all(a)) - a.begin();
}
template <class T> T max(vector<T> &a, ll n) {
return *max_element(a.begin(), a.begin() + min(n, sz(a)));
}
template <class T> T max(vector<T> &a, ll s, ll n) {
return *max_element(a.begin() + s, a.begin() + min(n, sz(a)));
}
template <typename A, size_t N> A max(A (&a)[N]) {
A res = a[0];
rep(i, N) res = max(res, a[i]);
return res;
}
template <typename A, size_t N, size_t O> A max(A (&a)[N][O]) {
A res = max(a[0]);
rep(i, N) res = max(res, max(a[i]));
return res;
}
template <typename A, size_t N, size_t O, size_t P> A max(A (&a)[N][O][P]) {
A res = max(a[0]);
rep(i, N) res = max(res, max(a[i]));
return res;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q>
A max(A (&a)[N][O][P][Q], const T &v) {
A res = max(a[0]);
rep(i, N) res = max(res, max(a[i]));
return res;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R>
A max(A (&a)[N][O][P][Q][R]) {
A res = max(a[0]);
rep(i, N) res = max(res, max(a[i]));
return res;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R,
size_t S>
A max(A (&a)[N][O][P][Q][R][S]) {
A res = max(a[0]);
rep(i, N) res = max(res, max(a[i]));
return res;
}
template <typename A, size_t N> A min(A (&a)[N]) {
A res = a[0];
rep(i, N) res = min(res, a[i]);
return res;
}
template <typename A, size_t N, size_t O> A min(A (&a)[N][O]) {
A res = min(a[0]);
rep(i, N) res = min(res, min(a[i]));
return res;
}
template <typename A, size_t N, size_t O, size_t P> A min(A (&a)[N][O][P]) {
A res = min(a[0]);
rep(i, N) res = min(res, min(a[i]));
return res;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q>
A min(A (&a)[N][O][P][Q], const T &v) {
A res = min(a[0]);
rep(i, N) res = min(res, min(a[i]));
return res;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R>
A min(A (&a)[N][O][P][Q][R]) {
A res = min(a[0]);
rep(i, N) res = min(res, min(a[i]));
return res;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R,
size_t S>
A min(A (&a)[N][O][P][Q][R][S]) {
A res = min(a[0]);
rep(i, N) res = min(res, min(a[i]));
return res;
}
template <class T> T sum(vector<T> &v, ll s, ll t) {
T ret = 0;
rep(i, s, min(sz(v), t)) ret += v[i];
return ret;
}
template <class T> T sum(vector<T> &v, ll t = inf) { return sum(v, 0, t); }
template <class T> T sum(vector<vector<T>> &v) {
T ret = 0;
rep(i, sz(v)) ret += sum(v[i]);
return ret;
}
template <class T> T sum(vector<vector<vector<T>>> &v) {
T ret = 0;
rep(i, sz(v)) ret += sum(v[i]);
return ret;
}
template <class T> T sum(vector<vector<vector<vector<T>>>> &v) {
T ret = 0;
rep(i, sz(v)) ret += sum(v[i]);
return ret;
}
template <class T> T sum(vector<vector<vector<vector<vector<T>>>>> &v) {
T ret = 0;
rep(i, sz(v)) ret += sum(v[i]);
return ret;
}
template <class T> auto sum(priority_queue<T, vector<T>, greater<T>> &r) {
auto q = r;
T ret = 0;
while (sz(q)) {
ret += q.top();
q.pop();
}
return ret;
}
template <class T> auto sum(priority_queue<T> &r) {
auto q = r;
T ret = 0;
while (sz(q)) {
ret += q.top();
q.pop();
}
return ret;
}
// template<class T, class U, class... W> auto sumn(vector<T> &v, U head, W...
// tail) { auto ret = sum(v[0], tail...); rep(i, 1, min(sz(v), head))ret
// += sum(v[i], tail...); return ret;}
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(); }
};
template <class T> struct rruic {
const T *rrui;
rruic(T *ru) : rrui(ru) {}
//[r , l) : [3 , -1)等
T operator()(ll r, ll l) {
assert(r >= l);
return rrui[l] - rrui[r];
}
T operator()(int l) { return rrui[l]; }
T operator[](ll i) { return rrui[i - 1] - rrui[i]; }
};
template <class T> ostream &operator<<(ostream &os, ruiC<T> a) {
fora(v, a.rui) os << v << " ";
return os;
}
template <class T> vector<T> ruiv(vector<T> &a) {
vector<T> ret(a.size() + 1);
rep(i, a.size()) ret[i + 1] = ret[i] + a[i];
return ret;
}
template <class T> ruiC<T> ruic() { return ruiC<T>(); }
template <class T> ruiC<T> ruic(vector<T> &a) {
vector<T> ret = ruiv(a);
return ruiC<T>(ret);
}
vvi() ruib(vi &a) {
vvi(res, 61, sz(a) + 1);
rep(k, 61) {
rep(i, sz(a)) { res[k][i + 1] = res[k][i] + ((a[i] >> k) & 1); }
}
return res;
}
vector<ruiC<int>> ruibc(vi &a) {
vector<ruiC<int>> ret(61);
vvi(res, 61, sz(a));
rep(k, 61) {
rep(i, sz(a)) { res[k][i] = (a[i] >> k) & 1; }
ret[k] = ruic(res[k]);
}
return ret;
}
vector<ll> ruiv(string &a) {
if (sz(a) == 0)
return vi(1);
ll dec = ('0' <= a[0] && a[0] <= '9') ? '0' : 0;
vector<ll> ret(a.size() + 1);
rep(i, a.size()) ret[i + 1] = ret[i] + a[i] - dec;
return ret;
}
ruiC<ll> ruic(string &a) {
vector<ll> ret = ruiv(a);
return ruiC<ll>(ret);
}
// kと同じものの数
template <class T, class U> vi ruiv(T &a, U k) {
vi ret(a.size() + 1);
rep(i, a.size()) ret[i + 1] = ret[i] + (a[i] == k);
return ret;
}
template <class T, class U> ruiC<ll> ruic(T &a, U k) {
vi ret = ruiv(a, k);
return ruiC<ll>(ret);
}
// h query
template <class T> vector<T> imoh(vector<vector<T>> &v, int w) {
vector<T> ret(sz(v));
rep(h, sz(ret)) { ret[h] = v[h][w]; }
rep(i, sz(ret) - 1) { ret[i + 1] += ret[i]; }
return ret;
}
template <class T> vector<T> ruih(vector<vector<T>> &v, int w) {
vector<T> ret(sz(v) + 1);
rep(h, sz(v)) { ret[h + 1] = v[h][w]; }
rep(i, sz(v)) { ret[i + 1] += ret[i]; }
return ret;
}
template <class T> ruiC<T> ruihc(vector<vector<T>> &a, int w) {
vector<T> ret = ruih(a, w);
return ruiC<T>(ret);
}
// xor
template <class T> struct ruixC {
const vector<T> rui;
ruixC(vector<T> &ru) : rui(ru) {}
T operator()(ll l, ll r) {
if (l > r) {
cerr << "ruiXc ";
deb(l, r);
assert(0);
}
return rui[r] ^ rui[l];
}
T operator[](ll i) { return rui[i]; }
T back() { return rui.back(); }
ll size() { return rui.size(); }
};
template <class T> vector<T> ruix(vector<T> &a) {
vector<T> ret(a.size() + 1);
rep(i, a.size()) ret[i + 1] = ret[i] ^ a[i];
return ret;
}
template <class T> ruixC<ll> ruixc(vector<T> &a) {
vi ret = ruix(a);
return ruixC<ll>(ret);
}
template <class T> vector<T> ruim(vector<T> &a) {
vector<T> res(a.size() + 1, 1);
rep(i, a.size()) res[i + 1] = res[i] * a[i];
return res;
}
// 漸化的に最小を1indexで持つ
template <class T> vector<T> ruimi(vector<T> &a) {
ll n = sz(a);
vector<T> ret(n + 1);
rep(i, 1, n) {
ret[i] = a[i - 1];
chmi(ret[i + 1], ret[i]);
}
return ret;
}
// template<class T> T *rrui(vector<T> &a) {
// 右から左にかけての半開区間 (-1 n-1]
template <class T> rruic<T> rrui(vector<T> &a) {
ll len = a.size();
T *body = (T *)malloc((len + 1) * sizeof(T));
T *res = body + 1;
rer(i, len - 1) res[i - 1] = res[i] + a[i];
return rruic<T>(res);
}
// 掛け算
template <class T> T *rruim(vector<T> &a) {
ll len = a.size();
T *body = (T *)malloc((len + 1) * sizeof(T));
T *res = body + 1;
res[len - 1] = 1;
rer(i, len - 1) res[i - 1] = res[i] * a[i];
return res;
}
template <class T, class U> void inc(T &a, U v = 1) { a += v; }
template <class T, class U> void inc(vector<T> &a, U v = 1) {
for (auto &u : a)
inc(u, v);
}
template <class T, class U> void dec(T &a, U v = 1) { a -= v; }
template <class T, class U> void dec(vector<T> &a, U v = 1) {
for (auto &u : a)
dec(u, v);
}
template <class U> void dec(string &a, U v = 1) {
for (auto &u : a)
dec(u, v);
}
template <class T> void dec(vector<T> &a) {
for (auto &u : a)
dec(u, 1);
}
template <class T, class U> void dec(vector<T> &a, vector<U> &b) {
for (auto &u : a)
dec(u, 1);
for (auto &u : b)
dec(u, 1);
}
template <class T, class U, class W>
void dec(vector<T> &a, vector<U> &b, vector<W> &c) {
for (auto &u : a)
dec(u, 1);
for (auto &u : b)
dec(u, 1);
for (auto &u : c)
dec(u, 1);
}
bool ins(ll h, ll w, ll H, ll W) { return h >= 0 && w >= 0 && h < H && w < W; }
bool ins(ll l, ll v, ll r) { return l <= v && v < r; }
template <class T> bool ins(vector<T> &a, ll i, ll j = 0) {
return ins(0, i, sz(a)) && ins(0, j, sz(a));
}
ll u(ll a) { return a < 0 ? 0 : a; }
template <class T> vector<T> u(const vector<T> &a) {
vector<T> ret = a;
fora(v, ret) v = u(v);
return ret;
}
#define MIN(a) numeric_limits<a>::min()
#define MAX(a) numeric_limits<a>::max()
// 添え字を返す
template <class F> ll goldd_l(ll left, ll right, F calc) {
double GRATIO = 1.6180339887498948482045868343656;
ll lm = left + (ll)((right - left) / (GRATIO + 1.0));
ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0));
ll fl = calc(lm);
ll fr = calc(rm);
while (right - left > 10) {
if (fl < fr) {
right = rm;
rm = lm;
fr = fl;
lm = left + (ll)((right - left) / (GRATIO + 1.0));
fl = calc(lm);
} else {
left = lm;
lm = rm;
fl = fr;
rm = lm + (ll)((right - lm) / (GRATIO + 1.0));
fr = calc(rm);
}
}
ll minScore = MAX(ll);
ll resIndex = left;
for (ll i = left; i < right + 1; ++i) {
ll score = calc(i);
if (minScore > score) {
minScore = score;
resIndex = i;
}
}
return resIndex;
}
template <class F> ll goldt_l(ll left, ll right, F calc) {
double GRATIO = 1.6180339887498948482045868343656;
ll lm = left + (ll)((right - left) / (GRATIO + 1.0));
ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0));
ll fl = calc(lm);
ll fr = calc(rm);
while (right - left > 10) {
if (fl > fr) {
right = rm;
rm = lm;
fr = fl;
lm = left + (ll)((right - left) / (GRATIO + 1.0));
fl = calc(lm);
} else {
left = lm;
lm = rm;
fl = fr;
rm = lm + (ll)((right - lm) / (GRATIO + 1.0));
fr = calc(rm);
}
}
if (left > right) {
ll l = left;
left = right;
right = l;
}
ll maxScore = MIN(ll);
ll resIndex = left;
for (ll i = left; i < right + 1; ++i) {
ll score = calc(i);
if (maxScore < score) {
maxScore = score;
resIndex = i;
}
}
return resIndex;
}
/*loopは200にすればおそらく大丈夫 余裕なら300に*/
template <class F> dou goldd_d(dou left, dou right, F calc, ll loop = 200) {
dou GRATIO = 1.6180339887498948482045868343656;
dou lm = left + ((right - left) / (GRATIO + 1.0));
dou rm = lm + ((right - lm) / (GRATIO + 1.0));
dou fl = calc(lm);
dou fr = calc(rm); /*200にすればおそらく大丈夫*/ /*余裕なら300に*/
ll k = 141;
loop++;
while (--loop) {
if (fl < fr) {
right = rm;
rm = lm;
fr = fl;
lm = left + ((right - left) / (GRATIO + 1.0));
fl = calc(lm);
} else {
left = lm;
lm = rm;
fl = fr;
rm = lm + ((right - lm) / (GRATIO + 1.0));
fr = calc(rm);
}
}
return left;
}
template <class F> dou goldt_d(dou left, dou right, F calc, ll loop = 200) {
double GRATIO = 1.6180339887498948482045868343656;
dou lm = left + ((right - left) / (GRATIO + 1.0));
dou rm = lm + ((right - lm) / (GRATIO + 1.0));
dou fl = calc(lm);
dou fr = calc(rm);
loop++;
while (--loop) {
if (fl > fr) {
right = rm;
rm = lm;
fr = fl;
lm = left + ((right - left) / (GRATIO + 1.0));
fl = calc(lm);
} else {
left = lm;
lm = rm;
fl = fr;
rm = lm + ((right - lm) / (GRATIO + 1.0));
fr = calc(rm);
}
}
return left;
}
// l ~ rを複数の区間に分割し、極致を与えるiを返す time-20 msまで探索
template <class F> ll goldd_ls(ll l, ll r, F calc, ll time = 2000) {
auto lim = milliseconds(time - 20);
ll mini = 0, minv = MAX(ll); /*区間をk分割する*/
rep(k, 1, inf) {
auto s = system_clock::now();
ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/
ll nl = l;
ll nr = l + haba;
rep(i, k) {
ll ni = goldd_l(nl, nr, calc);
if (chmi(minv, calc(ni)))
mini = ni;
nl = nr;
nr = nl + haba;
}
auto end = system_clock::now();
auto part = duration_cast<milliseconds>(end - s);
auto elapsed = duration_cast<milliseconds>(end - start_time);
if (elapsed + part * 2 >= lim) {
break;
}
}
return mini;
}
template <class F> ll goldt_ls(ll l, ll r, F calc, ll time = 2000) {
auto lim = milliseconds(time - 20);
ll maxi = 0, maxv = MIN(ll); /*区間をk分割する*/
rep(k, 1, inf) {
auto s = system_clock::now();
ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/
ll nl = l;
ll nr = l + haba;
rep(i, k) {
ll ni = goldt_l(nl, nr, calc);
if (chma(maxv, calc(ni)))
maxi = ni;
nl = nr;
nr = nl + haba;
}
auto end = system_clock::now();
auto part = duration_cast<milliseconds>(end - s);
auto elapsed = duration_cast<milliseconds>(end - start_time);
if (elapsed + part * 2 >= lim) {
break;
}
}
return maxi;
}
template <class F>
dou goldd_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を持つ*/
auto lim = milliseconds(time - 20);
dou mini = 0, minv = MAX(dou); /*区間をk分割する*/
rep(k, 1, inf) {
auto s = system_clock::now();
dou haba = (r - l) / k;
dou nl = l;
dou nr = l + haba;
rep(i, k) {
dou ni = goldd_d(nl, nr, calc);
if (chmi(minv, calc(ni)))
mini = ni;
nl = nr;
nr = nl + haba;
}
auto end = system_clock::now();
auto part = duration_cast<milliseconds>(end - s);
auto elapsed = duration_cast<milliseconds>(end - start_time);
if (elapsed + part * 2 >= lim) {
break;
}
}
return mini;
}
template <class F>
dou goldt_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を残している*/
auto lim = milliseconds(time - 20);
dou maxi = 0, maxv = MIN(dou); /*区間をk分割する*/
rep(k, 1, inf) {
auto s = system_clock::now();
dou haba = (r - l) / k;
dou nl = l;
dou nr = l + haba;
rep(i, k) {
dou ni = goldt_d(nl, nr, calc);
if (chma(maxv, calc(ni)))
maxi = ni;
nl = nr;
nr = nl + haba;
}
auto end = system_clock::now();
auto part = duration_cast<milliseconds>(end - s);
auto elapsed = duration_cast<milliseconds>(end - start_time);
if (elapsed + part * 2 >= lim) {
break;
}
}
return maxi;
}
template <class T> T min(vector<vector<T>> &a) {
T res = MAX(T);
rep(i, a.size()) chmi(res, *min_element(all(a[i])));
return res;
}
template <class T> T max(vector<vector<T>> &a) {
T res = MIN(T);
rep(i, a.size()) chma(res, *max_element(all(a[i])));
return res;
}
constexpr bool bget(ll m, ll keta) {
#ifdef _DEBUG
assert(keta <= 62); // オーバーフロー 1^62までしか扱えない
#endif
return (m >> keta) & 1;
}
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に置きなおす
ll bit(vi &a) {
int m = 0;
for (auto &&v : a)
m |= bit(v);
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 lowerBound(a, v) (*lower_bound(all(a), v))
#define upperIndex(a, v) (upper_bound(all(a), v) - a.begin())
#define upperBound(a, v) (*upper_bound(all(a), v))
#define rlowerIndex(a, v) (upper_bound(all(a), v) - a.begin() - 1)
#define rlowerBound(a, v) *(--(upper_bound(all(a), v)))
#define rupperIndex(a, v) (lower_bound(all(a), v) - a.begin() - 1)
#define rupperBound(a, v) *(--(lower_bound(all(a), v)))
#define next2(a) next(next(a))
#define prev2(a) prev(prev(a))
// 狭義の単調増加列 長さを返す
template <class T> int lis(vector<T> &a) {
int n = sz(a);
vi tail(n + 1, MAX(T));
rep(i, n) {
int id = lowerIndex(tail, a[i]); /**/
tail[id] = a[i];
}
return lowerIndex(tail, MAX(T));
}
template <class T> int lis_eq(vector<T> &a) {
int n = sz(a);
vi tail(n + 1, MAX(T));
rep(i, n) {
int id = upperIndex(tail, a[i]); /**/
tail[id] = a[i];
}
return lowerIndex(tail, MAX(T));
}
// iteratorを返す
// valueが1以上の物を返す 0は見つけ次第削除
// vを減らす場合 (*it).se--でいい
template <class T, class U, class V> auto lower_map(map<T, U> &m, V k) {
auto ret = m.lower_bound(k);
while (ret != m.end() && (*ret).second == 0) {
ret = m.erase(ret);
}
return ret;
}
template <class T, class U, class V> auto upper_map(map<T, U> &m, V k) {
auto ret = m.upper_bound(k);
while (ret != m.end() && (*ret).second == 0) {
ret = m.erase(ret);
}
return ret;
}
// 存在しなければエラー
template <class T, class U, class V> auto rlower_map(map<T, U> &m, V k) {
auto ret = upper_map(m, k);
assert(ret != m.begin());
ret--;
while (1) {
if ((*ret).second != 0)
break;
assert(ret != m.begin());
auto next = ret;
--next;
m.erase(ret);
ret = next;
}
return ret;
}
template <class T, class U, class V> auto rupper_map(map<T, U> &m, V k) {
auto ret = lower_map(m, k);
assert(ret != m.begin());
ret--;
while (1) {
if ((*ret).second != 0)
break;
assert(ret != m.begin());
auto next = ret;
--next;
m.erase(ret);
ret = next;
}
return ret;
}
template <class T> void fin(T s) { cout << s << endl, exit(0); }
// 便利 数学 math
// 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;
}
}
/*@formatter:on*/
ll tosa_i(ll st, ll ad, ll v) {
assert(((v - st) % ad) == 0);
return (v - st) / ad;
}
/*@formatter:off*/
// tood 係数が負の場合
// 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); }
ll sig(ll t) { return ((1 + t) * t) >> 1; }
ll sig(ll s, ll t) { return ((s + t) * (t - s + 1)) >> 1; }
// 機能拡張
template <class T, class U> string to_string(T a, U b) {
string res = "";
res += a;
res += b;
return res;
}
template <class T, class U, class V> string to_string(T a, U b, V c) {
string res = "";
res += a;
res += b;
res += c;
return res;
}
template <class T, class U, class V, class W>
string to_string(T a, U b, V c, W d) {
string res = "";
res += a;
res += b;
res += c;
res += d;
return res;
}
template <class T, class U, class V, class W, class X>
string to_string(T a, U b, V c, W d, X e) {
string res = "";
res += a;
res += b;
res += c;
res += d;
res += e;
return res;
}
constexpr int bsetlen = k5 * 2;
// constexpr int bsetlen = 5050;
#define bset bitset<bsetlen>
bool operator<(bitset<bsetlen> &a, bitset<bsetlen> &b) {
rer(i, bsetlen - 1) {
if (a[i] < b[i])
return true;
if (a[i] > b[i])
return false;
}
return false;
}
bool operator>(bitset<bsetlen> &a, bitset<bsetlen> &b) {
rer(i, bsetlen - 1) {
if (a[i] > b[i])
return true;
if (a[i] < b[i])
return false;
}
return false;
}
bool operator<=(bitset<bsetlen> &a, bitset<bsetlen> &b) {
rer(i, bsetlen - 1) {
if (a[i] < b[i])
return true;
if (a[i] > b[i])
return false;
}
return true;
}
bool operator>=(bitset<bsetlen> &a, bitset<bsetlen> &b) {
rer(i, bsetlen - 1) {
if (a[i] > b[i])
return true;
if (a[i] < b[i])
return false;
}
return true;
}
string operator~(string &a) {
string res = a;
for (auto &&c : res) {
if (c == '0')
c = '1';
else if (c == '1')
c = '0';
else {
cerr << "cant ~" << a << "must bit" << endl;
exit(0);
}
}
return res;
}
ostream &operator<<(ostream &os, bset &a) {
bitset<10> b;
vi list;
rep(i, bsetlen) {
if (a[i])
list.push_back(i), b[i] = 1;
}
os << b << ", " << list;
return os;
}
int hbiti(bset &a) {
rer(i, bsetlen) {
if (a[i])
return i;
}
return -1;
}
#define hk(a, b, c) (a <= b && b < c)
// O(N/64)
bset nap(bset &a, int v) {
bset r = a | a << v;
return r;
}
bset nap(bset &a, bset &v) {
bset r = a;
rep(i, bsetlen) {
if (v[i])
r |= a << i;
}
return r;
}
template <class T> void seth(vector<vector<T>> &S, int w, vector<T> &v) {
assert(sz(S) == sz(v));
assert(w < sz(S[0]));
rep(h, sz(S)) { S[h][w] = v[h]; }
}
template <class T, class U> void operator+=(pair<T, U> &a, pair<T, U> &b) {
a.fi += b.fi;
a.se += b.se;
}
template <class T, class U> pair<T, U> operator+(pair<T, U> &a, pair<T, U> &b) {
return pair<T, U>(a.fi + b.fi, a.se + b.se);
}
template <typename CharT, typename Traits, typename Alloc>
basic_string<CharT, Traits, Alloc>
operator+(const basic_string<CharT, Traits, Alloc> &lhs, const int rv) {
basic_string<CharT, Traits, Alloc> str(lhs);
str.append(to_string(rv));
return str;
}
template <typename CharT, typename Traits, typename Alloc>
void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const int rv) {
lhs += to_string(rv);
}
template <typename CharT, typename Traits, typename Alloc>
basic_string<CharT, Traits, Alloc>
operator+(const basic_string<CharT, Traits, Alloc> &lhs, const signed rv) {
basic_string<CharT, Traits, Alloc> str(lhs);
str.append(to_string(rv));
return str;
}
template <typename CharT, typename Traits, typename Alloc>
void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const signed rv) {
lhs += to_string(rv);
}
template <typename CharT, typename Traits, typename Alloc>
void operator*=(basic_string<CharT, Traits, Alloc> &s, int num) {
auto bek = s;
s = "";
for (; num; num >>= 1) {
if (num & 1) {
s += bek;
}
bek += bek;
}
}
template <class T, class U> void operator+=(queue<T> &a, U v) { a.push(v); }
template <class T, class U> void operator+=(deque<T> &a, U v) {
a.push_back(v);
}
template <class T>
priority_queue<T, vector<T>, greater<T>> &
operator+=(priority_queue<T, vector<T>, greater<T>> &a, vector<T> &v) {
fora(d, v) a.push(d);
return a;
}
template <class T, class U>
priority_queue<T, vector<T>, greater<T>> &
operator+=(priority_queue<T, vector<T>, greater<T>> &a, U v) {
a.push(v);
return a;
}
template <class T, class U>
priority_queue<T> &operator+=(priority_queue<T> &a, U v) {
a.push(v);
return a;
}
template <class T> set<T> &operator+=(set<T> &a, vector<T> v) {
fora(d, v) a.insert(d);
return a;
}
template <class T, class U> auto operator+=(set<T> &a, U v) {
return a.insert(v);
}
template <class T, class U> auto operator-=(set<T> &a, U v) {
return a.erase(v);
}
template <class T, class U> auto operator+=(mset<T> &a, U v) {
return a.insert(v);
}
template <class T, class U>
set<T, greater<T>> &operator+=(set<T, greater<T>> &a, U v) {
a.insert(v);
return a;
}
template <class T, class U> vector<T> &operator+=(vector<T> &a, U v) {
a.push_back(v);
return a;
}
template <class T, class U> vector<T> operator+(const vector<T> &a, U v) {
vector<T> ret = a;
ret += v;
return ret;
}
template <class T, class U> vector<T> operator+(U v, const vector<T> &a) {
vector<T> ret = a;
ret.insert(ret.begin(), v);
return ret;
}
template <class T> vector<T> operator+(vector<T> a, vector<T> b) {
vector<T> ret;
ret = a;
fora(v, b) ret += v;
return ret;
}
template <class T> vector<T> &operator+=(vector<T> &a, vector<T> &b) {
rep(i, sz(b)) { /*こうしないとa+=aで両辺が増え続けてバグる*/
a.push_back(b[i]);
}
return a;
}
template <class T, class U> map<T, U> &operator+=(map<T, U> &a, map<T, U> &b) {
fora(bv, b) { a[bv.first] += bv.second; }
return a;
}
template <class T> vector<T> &operator-=(vector<T> &a, vector<T> &b) {
if (sz(a) != sz(b)) {
debugline("vector<T> operator-=");
deb(a);
deb(b);
exit(0);
}
rep(i, sz(a)) a[i] -= b[i];
return a;
}
template <class T> vector<T> operator-(vector<T> &a, vector<T> &b) {
if (sz(a) != sz(b)) {
debugline("vector<T> operator-");
deb(a);
deb(b);
ole();
}
vector<T> res(sz(a));
rep(i, sz(a)) res[i] = a[i] - b[i];
return res;
}
template <class T, class U> void operator*=(vector<T> &a, U b) {
vector<T> ta = a;
rep(b - 1) { a += ta; }
}
template <typename T> void erase(vector<T> &v, unsigned ll i) {
v.erase(v.begin() + i);
}
template <typename T> void erase(vector<T> &v, unsigned ll s, unsigned ll e) {
v.erase(v.begin() + s, v.begin() + e);
}
template <class T, class U> void erase(map<T, U> &m, ll okl, ll ngr) {
m.erase(m.lower_bound(okl), m.lower_bound(ngr));
}
template <class T> void erase(set<T> &m, ll okl, ll ngr) {
m.erase(m.lower_bound(okl), m.lower_bound(ngr));
}
template <typename T> void erasen(vector<T> &v, unsigned ll s, unsigned ll n) {
v.erase(v.begin() + s, v.begin() + s + n);
}
template <typename T, typename U>
void insert(vector<T> &v, unsigned ll i, U t) {
v.insert(v.begin() + i, t);
}
template <typename T, typename U> void push_front(vector<T> &v, U t) {
v.insert(v.begin(), t);
}
template <typename T, typename U>
void insert(vector<T> &v, unsigned ll i, vector<T> list) {
for (auto &&va : list)
v.insert(v.begin() + i++, va);
}
template <typename T> void insert(set<T> &v, vector<T> list) {
for (auto &&va : list)
v.insert(va);
}
vector<string> split(const string a, const char deli) {
string b = a + deli;
ll l = 0, r = 0, n = b.size();
vector<string> res;
rep(i, n) {
if (b[i] == deli) {
r = i;
if (l < r)
res.push_back(b.substr(l, r - l));
l = i + 1;
}
}
return res;
}
vector<string> split(const string a, const string deli) {
vector<string> res;
ll kn = sz(deli);
std::string::size_type Pos(a.find(deli));
ll l = 0;
while (Pos != std::string::npos) {
if (Pos - l)
res.push_back(a.substr(l, Pos - l));
l = Pos + kn;
Pos = a.find(deli, Pos + kn);
}
if (sz(a) - l)
res.push_back(a.substr(l, sz(a) - l));
return res;
}
void yn(bool a) {
if (a)
cout << "yes" << endl;
else
cout << "no" << endl;
}
void Yn(bool a) {
if (a)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
void YN(bool a) {
if (a)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
void fyn(bool a) {
if (a)
cout << "yes" << endl;
else
cout << "no" << endl;
exit(0);
}
void fYn(bool a) {
if (a)
cout << "Yes" << endl;
else
cout << "No" << endl;
exit(0);
}
void fYN(bool a) {
if (a)
cout << "YES" << endl;
else
cout << "NO" << endl;
exit(0);
}
void 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)};
}
// 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;
}
//@起動時
struct initon {
initon() {
cin.tie(0);
ios::sync_with_stdio(false);
cout.setf(ios::fixed);
cout.precision(16);
srand((unsigned)clock() + (unsigned)time(NULL));
};
} initonv; //@formatter:on
#define pre prev
#define nex next
// gra mll pr
// 上下左右
const string udlr = "udlr";
string UDLR = "UDLR"; // x4と連動 UDLR.find('U') := x4[0]
// 右、上が正
constexpr ll y4[] = {1, -1, 0, 0};
constexpr ll x4[] = {0, 0, -1, 1};
constexpr ll y8[] = {0, 1, 0, -1, -1, 1, 1, -1};
constexpr ll x8[] = {1, 0, -1, 0, 1, -1, 1, -1};
// vectorで取れる要素数
// bool=> 1e9 * 8.32
// int => 1e8 * 2.6
// ll => 1e8 * 1.3
vvc(ba);
ll N, M, H, W;
vi A, B, C;
/*@formatter:off*/
#define forg(gi, ve) \
for (ll gi = 0, forglim = ve.size(), f, t, c; \
gi < forglim && (f = ve[gi].f, t = ve[gi].t, c = ve[gi].c, true); ++gi)
#define fort(gi, ve) \
for (ll gi = 0, f, t, c, 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);
}
};
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 N = g.n;
vi dis(N, linf);
dis[s] = 0;
deque<int> q;
q.push_back(s);
vb was(N);
while (!q.empty()) {
int f = q.front();
q.pop_front();
if (was[f])
continue;
was[f] = true;
fora(e, g[f]) {
if (dis[e.t] > dis[f] + e.c) {
if (e.c) {
dis[e.t] = dis[f] + 1;
q.push_back(e.t);
} else {
dis[e.t] = dis[f];
q.push_front(e.t);
}
}
}
}
return dis;
}
// dijkstra_cou<mint> : 数える型で書く return vp(dis,cou)
template <class COU, class T = int>
auto dijkstra_cou(const graph<T> &g, int s, int init_value) {
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(const 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(const vector<pair<signed, signed>> &array, OpFunc opfunc) {
signed n = array.size();
op = opfunc;
lg.assign(n + 1, 0);
for (signed i = 1; i <= n; i++) {
lg[i] = 31 - __builtin_clz(i);
}
table.assign(lg[n] + 1, array);
for (signed i = 1; i <= lg[n]; i++) {
for (signed j = 0; j < n; j++) {
if (j + (1 << (i - 1)) < n) {
table[i][j] = op(table[i - 1][j], table[i - 1][j + (1 << (i - 1))]);
} else {
table[i][j] = table[i - 1][j];
}
}
}
}
pair<signed, signed> query(signed l, signed r) {
assert(l < r);
return op(table[lg[r - l]][l], table[lg[r - l]][r - (1 << lg[r - l])]);
}
};
struct PMORMQ {
vector<signed> a;
SparseTable<MinOp<pair<signed, signed>>> sparse_table;
vector<vector<vector<signed>>> lookup_table;
vector<signed> block_type;
signed block_size, n_block;
void init(const vector<signed> &array) {
a = array;
signed n = a.size();
block_size = std::max(1, (31 - __builtin_clz(n)) / 2);
while (n % block_size != 0) {
a.push_back(a.back() + 1);
n++;
}
n_block = n / block_size;
vector<pair<signed, signed>> b(n_block, make_pair(INT_MAX, INT_MAX));
for (signed i = 0; i < n; i++) {
b[i / block_size] = min(b[i / block_size], make_pair(a[i], i));
}
sparse_table.init(b, MinOp<pair<signed, signed>>());
block_type.assign(n_block, 0);
for (signed i = 0; i < n_block; i++) {
signed cur = 0;
for (signed j = 0; j < block_size - 1; j++) {
signed ind = i * block_size + j;
if (a[ind] < a[ind + 1]) {
cur |= 1 << j;
}
}
block_type[i] = cur;
}
lookup_table.assign(
1 << (block_size - 1),
vector<vector<signed>>(block_size, vector<signed>(block_size + 1)));
for (signed i = 0; i < (1 << (block_size - 1)); i++) {
for (signed j = 0; j < block_size; j++) {
signed res = 0;
signed cur = 0;
signed pos = j;
for (signed k = j + 1; k <= block_size; k++) {
lookup_table[i][j][k] = pos;
if (i & (1 << (k - 1))) {
cur++;
} else {
cur--;
}
if (res > cur) {
pos = k;
res = cur;
}
}
}
}
}
signed query(signed l, signed r) {
assert(l < r);
signed lb = l / block_size;
signed rb = r / block_size;
if (lb == rb) {
return lb * block_size +
lookup_table[block_type[lb]][l % block_size][r % block_size];
}
signed pl = lb * block_size +
lookup_table[block_type[lb]][l % block_size][block_size];
signed pr =
rb * block_size + lookup_table[block_type[rb]][0][r % block_size];
signed pos = pl;
if (r % block_size > 0 && a[pl] > a[pr]) {
pos = pr;
}
if (lb + 1 == rb) {
return pos;
}
signed spv = sparse_table.query(lb + 1, rb).second;
if (a[pos] > a[spv]) {
return spv;
}
return pos;
}
};
template <class T = int> class tree : public undigraph<T> {
PMORMQ rmq;
int cnt;
vector<signed> id, in;
bool never = true;
bool never_hld = true;
void dfs(int x, int p, int d, int dis = 0) {
id[cnt] = x;
par_[x] = p;
rmq_dep.push_back(d);
disv[x] = dis;
in[x] = cnt++;
forg(gi, g[x]) {
if (t == p) {
continue;
}
dfs(t, x, d + 1, dis + c);
id[cnt] = x;
rmq_dep.push_back(d);
cnt++;
}
}
void precalc() {
never = false;
cnt = 0;
rmq_dep.clear();
disv.assign(n, 0);
in.assign(n, -1);
id.assign(2 * n - 1, -1);
par_.assign(n, -1);
dfs(root, -1, 0);
rmq.init(rmq_dep);
#ifdef _DEBUG
if (n >= 100)
return;
cerr << "---tree---" << endl;
rep(i, n) {
if (!(i == root || sz(g[i]) > 1))
continue;
cerr << i << " -> ";
vi ts;
forg(gi, g[i]) {
if (t != par_[i])
ts.push_back(t);
}
rep(i, sz(ts) - 1) cerr << ts[i] << ", ";
if (sz(ts))
cerr << ts.back() << endl;
}
cerr << endl;
#endif
}
int pos;
void hld_build() {
never_hld = false;
if (never)
precalc();
g.resize(n);
vid.resize(n, -1);
head.resize(n);
heavy.resize(n, -1);
depth.resize(n);
inv.resize(n);
subl.resize(n);
subr.resize(n);
dfs(root, -1);
t = 0;
dfs_hld(root);
#ifdef _DEBUG
if (n >= 100)
return;
cerr << "---hld_index---" << endl;
vi inds;
rep(i, n) if (sz(g[i])) inds.push_back(i);
rep(i, sz(inds)) {
str s = tos(bel(inds[i]));
cerr << std::right << std::setw(sz(s) + (i ? 1 : 0)) << inds[i];
}
cerr << endl;
rep(i, sz(inds)) { cerr << bel(inds[i]) << " "; }
cerr << endl << endl;
cerr << "---hld_edge_index---" << endl;
fora(e, edges) {
if (e.f <= e.t)
cerr << e.f << "-" << e.t << " " << bel(e) << endl;
}
cerr << endl << endl;
cerr << "!!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;
public:
using undigraph<T>::g;
using undigraph<T>::n;
using undigraph<T>::edges;
int root;
// 部分木の [左端、右端) 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_; }
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];
}
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;
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 par(int a){if (never)precalc(); return par[a];}
/*O(N) hldを使わず木を普通にたどる*/
void for_each_l(int u, int v, function<void(int)> fnode) {
int r = lca(u, v);
while (u != r) {
fnode(u);
u = par_[u];
}
while (v != r) {
fnode(v);
v = par_[v];
}
fnode(r);
}
void for_each_edge_l /*O(N) 頂点に対しての処理順が可換*/ (
int u, int v, function<void(edge<int> &)> fedge) {
int r = lca(u, v);
auto sub = [&](int u, int r) {
while (u != r) {
forg(gi, g[u]) {
if (t == par_[u]) {
fedge(g[u][gi]);
u = par_[u];
break;
}
}
}
};
sub(u, r);
sub(v, r);
}
// Fは半開 (u,v)は木の頂点
// 中ではhldの頂点を見るため、seg木のupdateはhldのindexで行なう
void for_each_ /*[l,r)*/ (int u, int v, const function<void(int, int)> &f) {
if (never_hld)
hld_build();
while (1) {
if (vid[u] > vid[v])
swap(u, v);
int l = max(vid[head[v]], vid[u]);
int r = vid[v] + 1;
f(l, r);
if (head[u] != head[v])
v = par_[head[v]];
else
break;
}
}
void for_each_edge /*[l,r) O(log(N)) 辺を頂点として扱っている
上と同じ感じで使える*/
(int u, int v, const function<void(int, int)> &f) {
if (never_hld)
hld_build();
while (1) {
if (vid[u] > vid[v])
swap(u, v);
if (head[u] != head[v]) {
int l = vid[head[v]];
int r = vid[v] + 1;
f(l, r);
v = par_[head[v]];
} else {
if (u != v) {
int l = vid[u] + 1;
int r = vid[v] + 1;
f(l, r);
}
break;
}
}
}
int bel(int v) { /*hld内での頂点番号を返す*/
if (never_hld)
hld_build();
return vid[v];
}
// 下の頂点に辺のクエリを持たせている
int bel(
int f,
int t) { /*辺のクエリを扱うときどの頂点に持たせればいいか(vidを返すのでそのままupd出来る)*/
if (never_hld)
hld_build();
return depth[f] > depth[t] ? vid[f] : vid[t];
}
int bel(
edge<T> &
e) { /*辺のクエリを扱うときどの頂点に持たせればいいか(vidを返すのでそのままupd出来る)*/
if (never_hld)
hld_build();
return depth[e.f] > depth[e.t] ? vid[e.f] : vid[e.t];
}
template <class... U> int operator()(U... args) { return bel(args...); }
// path l -> r += v
template <class S> void seg_add(S &seg, int lhei, int rhei, int v) {
for_each_(lhei, rhei, [&](int l, int r) { seg.add(l, r, v); });
}
template <class S> void seg_update(S &seg, int lhei, int rhei, int v) {
for_each_(lhei, rhei, [&](int l, int r) { seg.update(l, r, v); });
}
template <class S> 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) {
cout << "grid" << endl;
if (!(0 <= f && f < n && 0 <= t && t < n)) {
debugline("grid_k6 add");
deb(f, t, c);
ole();
}
g[f].emplace_back(f, t, c);
g[t].emplace_back(t, f, c);
// undigraphと違い、edgesを持たない
}
int getid(int h, int w) {
assert(ins(h, w, H, W));
return W * h + w;
}
int getid(P p) { return getid(p.first, p.second); }
P get2(int id) { return mp(id / W, id % W); }
P operator()(int id) { return get2(id); }
int operator()(int h, int w) { return getid(h, w); }
int operator()(P p) { return getid(p); }
// 辺は無い
grid_k6(int H, int W) : H(H), W(W), undigraph<T>(H * W) {}
grid_k6(vector<vector<char>> ba, char wall = '#')
: H(sz(ba)), W(sz(ba[0])), undigraph<T>(sz(ba) * sz(ba[0])) {
rep(h, H) {
rep(w, W) {
if (ba[h][w] == wall)
con;
int f = getid(h, w);
if (w + 1 < W && ba[h][w + 1] != wall) {
add(f, getid(h, w + 1));
}
if (h + 1 < H && ba[h + 1][w] != wall) {
add(f, getid(h + 1, w));
}
}
}
}
/*o -> o, o -> x, x-> x*/
grid_k6(vector<vector<char>> ba, int oo, int ox, int xo, int xx,
char wall = '#')
: H(sz(ba)), W(sz(ba[0])), undigraph<T>(sz(ba) * sz(ba[0])),
costs({oo, ox, xo, xx}), ba(ba), wall(wall) {
rep(h, H) {
rep(w, W) {
add(h, w, h + 1, w);
add(h, w, h - 1, w);
add(h, w, h, w + 1);
add(h, w, h, w - 1);
}
}
}
void add(int fh, int fw, int th, int tw) {
if (ins(fh, fw, H, W) && ins(th, tw, H, W)) {
int cm = 0;
if (ba[fh][fw] == wall) {
cm += 2;
}
if (ba[th][tw] == wall) {
cm++;
}
int f = getid(fh, fw);
int t = getid(th, tw);
g[f].emplace_back(f, t, costs[cm]);
}
}
void set_edges() {
rep(i, n) fora(e, g[i]) if (e.f < e.t) edges.push_back(e);
}
};
// 辺によりメモリを大量消費ためedgesを消している
// 頂点10^6でメモリを190MB(制限の8割)使う
// 左上から右下に移動できる
template <class T = int> class digrid_k6 : public digraph<T> {
public:
using digraph<T>::g;
using digraph<T>::n;
using digraph<T>::edges;
int H, W;
void add(int f, int t, T c = 1) {
if (!(0 <= f && f < n && 0 <= t && t < n)) {
debugline("digrid_k6 add");
deb(f, t, c);
ole();
}
g[f].emplace_back(f, t, c);
/*digraphと違いedgesを持たない*/
}
int getid(int h, int w) {
if (!ins(h, w, H, W))
return -1;
return W * h + w;
}
P get2(int id) { return mp(id / W, id % W); }
P operator()(int id) { return get2(id); }
int operator()(int h, int w) { return getid(h, w); }
digrid_k6(int H, int W) : H(H), W(W), digraph<T>(H * W) {}
digrid_k6(vector<vector<char>> ba, char wall = '#')
: H(sz(ba)), W(sz(ba[0])), digraph<T>(sz(ba) * sz(ba[0])) {
rep(h, H) {
rep(w, W) {
if (ba[h][w] == wall)
con;
int f = getid(h, w);
if (w + 1 < W && ba[h][w + 1] != wall) {
add(f, getid(h, w + 1));
}
if (h + 1 < H && ba[h + 1][w] != wall) {
add(f, getid(h + 1, w));
}
}
}
}
void add(int fh, int fw, int th, int tw) {
add(getid(fh, fw), getid(th, tw));
}
void set_edges() { rep(i, n) fora(e, g[i]) edges.push_back(e); }
};
// 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++;
}
};
//@出力
template <class T> ostream &operator<<(ostream &os, digraph<T> &g) {
os << endl << g.n << " " << sz(g.edges) << endl;
fore(gi, g.edges) { os << f << " " << t << " " << c << endl; }
return os;
}
template <class T> ostream &operator<<(ostream &os, undigraph<T> &g) {
os << endl << g.n << " " << sz(g.edges) << endl;
fore(gi, g.edges) {
if (f < t)
os << f << " " << t << " " << c << endl;
}
return os;
}
//@判定
template <class T> bool nibu(const 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 1;
}
// 二部グラフを色分けした際の頂点数を返す
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) { \
if (zhouken && f < t) { \
g2.add(f, t, c); \
} \
}
// 閉路がなければ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
P farthest(undigraph<> &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(undigraph<> &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];
}
int diameter(undigraph<> &E) {
vi d = diameters(E);
return max(d);
}
// i d
vp diameter_p(undigraph<> &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(D[0][i], v1.second);
else
res[i] = mp(D[1][i], v2.second);
}
return res;
}
//@列挙 取得
// 閉路がある時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;
}
//@実験
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*/
// todo rootを変えてもちゃんと動くか確認
// これらの具体的な説明、およびhldとその他で分ける
// vi par;
// vi vid, head, heavy, depth, inv, subl, subr;
void solve() {
din(N, b, r);
--b, --r;
tree<> g(2 * k5, r);
;
g.ing(N, N - 1);
int dis = g.dis(r, b);
int up = (dis - 1) / 2;
while (up--) {
b = g.par(b);
}
int far = 0;
auto chi = g.child_ex(b);
rep(i, N) {
if (chi[i])
chma(far, g.dis(b, i));
}
int res = (dis - 1) / 2 + far;
if ((dis % 2) == 0)
res++;
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;
}
#endif
return 0;
};
| // #undef _DEBUG
// #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace std::chrono;
#define int long long
#define ll long long
auto start_time = system_clock::now();
#define debugName(VariableName) #VariableName
/*@formatter:off*/
//[-n, n)にアクセスできる
// また、外部関数resizeに渡せる
template <class T> struct mvec {
vector<T> v;
int n;
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 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(); }
};
//[]で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(); }
};
// todo?
template <class T> ostream &operator<<(ostream &os, mvec<T> &a) { 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>;
#ifdef _DEBUG
// https://marycore.jp/prog/cpp/class-extension-methods/ 違うかも
template <class T, class A = std::allocator<T>>
struct debtor : std::vector<T, A> {
using std::vector<T, A>::vector;
template <class U> int deb_v(U a, int v) { return v; }
template <class U> int deb_v(debtor<U> &a, int v = 0) {
cerr << a.size() << " ";
return deb_v(a.at(0), v + 1);
}
template <class U> void deb_o(U a) { cerr << a << " "; }
template <class U> void deb_o(debtor<U> &a) {
for (int i = 0; i < min((int)a.size(), 15ll); i++) {
deb_o(a[i]);
}
if ((int)a.size() > 15) {
cerr << "...";
}
cerr << endl;
}
typename std::vector<T>::reference
operator[](typename std::vector<T>::size_type n) {
if (n < 0 || n >= (int)this->size()) {
int siz = (int)this->size();
cerr << "vector size = ";
int dim = deb_v((*this));
cerr << endl;
cerr << "out index at " << n << endl;
cerr << endl;
if (dim <= 2) {
deb_o((*this));
}
exit(0);
}
return this->at(n);
}
};
#define vector debtor
#endif
#ifdef _DEBUG
// 区間削除は出来ない
template <class T> struct my_pbds_tree {
set<T> s;
auto begin() { return s.begin(); }
auto end() { return s.end(); }
auto rbegin() { return s.rbegin(); }
auto rend() { return s.rend(); }
auto empty() { return s.empty(); }
auto size() { return s.size(); }
void clear() { s.clear(); }
template <class U> void insert(U v) { s.insert(v); }
template <class U> void operator+=(U v) { insert(v); }
template <class F> auto erase(F v) { return s.erase(v); }
template <class U> auto find(U v) { return s.find(v); }
template <class U> auto lower_bound(U v) { return s.lower_bound(v); }
template <class U> auto upper_bound(U v) { return s.upper_bound(v); }
auto find_by_order(ll k) {
auto it = s.begin();
for (ll i = 0; i < k; i++)
it++;
return it;
}
auto order_of_key(ll v) {
auto it = s.begin();
ll i = 0;
for (; it != s.end() && *it < v; i++)
it++;
return i;
}
};
#define pbds(T) my_pbds_tree<T>
// gp_hash_tableでcountを使えないようにするため
template <class T, class U> struct my_unordered_map {
unordered_map<T, U> m;
my_unordered_map(){};
auto begin() { return m.begin(); }
auto end() { return m.end(); }
auto cbegin() { return m.cbegin(); }
auto cend() { return m.cend(); }
template <class V> auto erase(V v) { return m.erase(v); }
void clear() {
m.clear();
} /*countは gp_hash_tableに存在しない*/ /*!= m.end()*/
template <class V> auto find(V v) { return m.find(v); }
template <class V> auto &operator[](V n) { return m[n]; }
};
#define unordered_map my_unordered_map
#define umapi unordered_map<ll, ll>
#define umapp unordered_map<P, ll>
#define umapip unordered_map<ll, P>
#else
// umapはunorderd_mapになる
// umapiはgp_hash_table
// find_by_order(k) k番目のイテレーター
// order_of_key(k) k以上が前から何番目か
#define pbds(U) \
__gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag, \
__gnu_pbds::tree_order_statistics_node_update>
#define umapi __gnu_pbds::gp_hash_table<ll, ll, xorshift>
#define umapp __gnu_pbds::gp_hash_table<P, ll, xorshift>
#define umapip __gnu_pbds::gp_hash_table<ll, P, xorshift>
#endif
struct xorshift {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
size_t operator()(std::pair<ll, ll> x) const {
ll v = ((x.first) << 32) | x.second;
static const uint64_t FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(v + FIXED_RANDOM);
}
};
template <class U, class L>
void operator+=(
__gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag,
__gnu_pbds::tree_order_statistics_node_update> &s,
L v) {
s.insert(v);
}
// 衝突対策
#define ws ws_
template <class A, class B, class C> struct T2 {
A f;
B s;
C t;
T2() { f = 0, s = 0, t = 0; }
T2(A f, B s, C t) : f(f), s(s), t(t) {}
bool operator<(const T2 &r) const {
return f != r.f ? f < r.f
: s != r.s ? s < r.s
: t < r.t; /*return f != r.f ? f > r.f : s != r.s ?n s >
r.s : t > r.t; 大きい順 */
}
bool operator>(const T2 &r) const {
return f != r.f ? f > r.f
: s != r.s ? s > r.s
: t > r.t; /*return f != r.f ? f > r.f : s != r.s ? s >
r.s : t > r.t; 小さい順 */
}
bool operator==(const T2 &r) const {
return f == r.f && s == r.s && t == r.t;
}
bool operator!=(const T2 &r) const {
return f != r.f || s != r.s || t != r.t;
}
};
template <class A, class B, class C, class D> struct F2 {
A a;
B b;
C c;
D d;
F2() { a = 0, b = 0, c = 0, d = 0; }
F2(A a, B b, C c, D d) : a(a), b(b), c(c), d(d) {}
bool operator<(const F2 &r) const {
return a != r.a ? a < r.a
: b != r.b ? b < r.b
: c != r.c ? c < r.c
: d < r.d; /* return a != r.a ? a > r.a : b != r.b ? b
> r.b : c != r.c ? c > r.c : d > r.d;*/
}
bool operator>(const F2 &r) const {
return a != r.a ? a > r.a
: b != r.b ? b > r.b
: c != r.c ? c > r.c
: d > r.d; /* return a != r.a ? a < r.a : b != r.b
? b < r.b : c != r.c ? c < r.c : d < r.d;*/
}
bool operator==(const F2 &r) const {
return a == r.a && b == r.b && c == r.c && d == r.d;
}
bool operator!=(const F2 &r) const {
return a != r.a || b != r.b || c != r.c || d != r.d;
}
ll operator[](ll i) {
assert(i < 4);
return i == 0 ? a : i == 1 ? b : i == 2 ? c : d;
}
};
typedef T2<ll, ll, ll> T;
typedef F2<ll, ll, ll, ll> F;
T mt(ll a, ll b, ll c) { return T(a, b, c); }
F mf(ll a, ll b, ll c, ll d) { return F(a, b, c, d); }
//@マクロ省略系 型,構造
#define double long double
#define pow powl
#define ull unsigned long long
using dou = double;
using itn = int;
using str = string;
using bo = bool;
#define au auto
using P = pair<ll, ll>;
using mvp = mvec<P>;
using mvt = mvec<T>;
#define fi first
#define se second
#define beg begin
#define rbeg rbegin
#define con continue
#define bre break
#define brk break
#define is ==
#define el else
#define elf else if
#define wh while
#define upd update
#define sstream stringstream
#define maxq 1
#define minq -1
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
#define MALLOC(type, len) (type *)malloc((len) * sizeof(type))
#define lam(right) [&](auto &p) { return p right; }
// マクロ省略系 コンテナ
using vi = vector<ll>;
using vb = vector<bool>;
using vs = vector<string>;
using vd = vector<double>;
using vc = vector<char>;
using vp = vector<P>;
using vt = vector<T>;
#define V vector
#define o_vvt(o1, o2, o3, o4, name, ...) name
#define vvt0(t) vector<vector<t>>
#define vvt1(t, a) vector<vector<t>> a
#define vvt2(t, a, b) vector<vector<t>> a(b)
#define vvt3(t, a, b, c) vector<vector<t>> a(b, vector<t>(c))
#define vvt4(t, a, b, c, d) vector<vector<t>> a(b, vector<t>(c, d))
#define vvi(...) \
o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(ll, __VA_ARGS__)
#define vvb(...) \
o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(bool, __VA_ARGS__)
#define vvs(...) \
o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(string, __VA_ARGS__)
#define vvd(...) \
o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(double, __VA_ARGS__)
#define vvc(...) \
o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(char, __VA_ARGS__)
#define vvp(...) \
o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(P, __VA_ARGS__)
#define vvt(...) \
o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(T, __VA_ARGS__)
#define vv(type, ...) \
o_vvt(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(type, __VA_ARGS__)
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
#define vni(name, ...) auto name = make_v<ll>(__VA_ARGS__)
#define vnb(name, ...) auto name = make_v<bool>(__VA_ARGS__)
#define vns(name, ...) auto name = make_v<string>(__VA_ARGS__)
#define vnd(name, ...) auto name = make_v<double>(__VA_ARGS__)
#define vnc(name, ...) auto name = make_v<char>(__VA_ARGS__)
#define vnp(name, ...) auto name = make_v<P>(__VA_ARGS__)
#define vn(type, name, ...) auto name = make_v<type>(__VA_ARGS__)
#define PQ priority_queue<ll, vector<ll>, greater<ll>>
#define tos to_string
using mapi = map<ll, ll>;
using mapp = map<P, ll>;
using mapd = map<dou, ll>;
using mapc = map<char, ll>;
using maps = map<str, ll>;
using seti = set<ll>;
using setd = set<dou>;
using setc = set<char>;
using sets = set<str>;
using qui = queue<ll>;
#define uset unordered_set
#define useti unordered_set<ll, xorshift>
#define mset multiset
#define mseti multiset<ll>
#define umap unordered_map
#define mmap multimap
// 任意のマクロサポート用 使う度に初期化する
int index_, v1_, v2_, v3_;
template <class T> struct pq {
priority_queue<T, vector<T>, greater<T>> q; /*小さい順*/
T su = 0;
void clear() {
q = priority_queue<T, vector<T>, greater<T>>();
su = 0;
}
void operator+=(T v) {
su += v;
q.push(v);
}
T sum() { return su; }
T top() { return q.top(); }
void pop() {
su -= q.top();
q.pop();
}
T poll() {
T ret = q.top();
su -= ret;
q.pop();
return ret;
}
ll size() { return q.size(); }
};
template <class T> struct pqg {
priority_queue<T> q; /*大きい順*/
T su = 0;
void clear() {
q = priority_queue<T>();
su = 0;
}
void operator+=(T v) {
su += v;
q.push(v);
}
T sum() { return su; }
T top() { return q.top(); }
void pop() {
su -= q.top();
q.pop();
}
T poll() {
T ret = q.top();
su -= ret;
q.pop();
return ret;
}
ll size() { return q.size(); }
};
#define pqi pq<ll>
#define pqgi pqg<ll>
// マクロ 繰り返し
// ↓@オーバーロード隔離
#define o_rep(o1, o2, o3, o4, name, ...) name
#define rep1(n) for (ll rep1i = 0, rep1lim = n; rep1i < rep1lim; ++rep1i)
#define rep2(i, n) for (ll i = 0, rep2lim = n; i < rep2lim; ++i)
#define rep3(i, m, n) for (ll i = m, rep3lim = n; i < rep3lim; ++i)
#define rep4(i, m, n, ad) for (ll i = m, rep4lim = n; i < rep4lim; i += ad)
// 逆順 閉区間
#define rer2(i, n) for (ll i = n; i >= 0; i--)
#define rer3(i, m, n) for (ll i = m, rer3lim = n; i >= rer3lim; i--)
#define rer4(i, m, n, dec) for (ll i = m, rer4lim = n; i >= rer4lim; i -= dec)
// ループを一つにまとめないとフォーマットで汚くなるため
#define nex_ind1(i) i++
#define nex_ind2(i, j, J) \
i = (j + 1 == J) ? i + 1 : i, j = (j + 1 == J ? 0 : j + 1)
#define nex_ind3(i, j, k, J, K) \
i = (j + 1 == J && k + 1 == K) ? i + 1 : i, \
j = (k + 1 == K) ? (j + 1 == J ? 0 : j + 1) : j, \
k = (k + 1 == K ? 0 : k + 1)
#define nex_ind4(i, j, k, l, J, K, L) \
i = (j + 1 == J && k + 1 == K && l + 1 == L) ? i + 1 : i, \
j = (k + 1 == K && l + 1 == L) ? (j + 1 == J ? 0 : j + 1) : j, \
k = (l + 1 == L ? (k + 1 == K ? 0 : k + 1) : k), l = l + 1 == L ? 0 : l + 1
#define nex_ind5(i, j, k, l, m, J, K, L, M) \
i = (j + 1 == J && k + 1 == K && l + 1 == L && m + 1 == M) ? i + 1 : i, \
j = (k + 1 == K && l + 1 == L && m + 1 == M) ? (j + 1 == J ? 0 : j + 1) : j, \
k = (l + 1 == L && m + 1 == M ? (k + 1 == K ? 0 : k + 1) : k), \
l = m + 1 == M ? l + 1 == L ? 0 : l + 1 : l, m = m + 1 == M ? 0 : m + 1
#define repss1(i, I) for (int i = 0; i < I; i++)
#define repss2(i, j, I, J) \
for (int i = (J ? 0 : I), j = 0; i < I; nex_ind2(i, j, J))
#define repss3(i, j, k, I, J, K) \
for (int i = (J && K ? 0 : I), j = 0, k = 0; i < I; nex_ind3(i, j, k, J, K))
#define repss4(i, j, k, l, I, J, K, L) \
for (int i = (J && K && L ? 0 : I), j = 0, k = 0, l = 0; i < I; \
nex_ind4(i, j, k, l, J, K, L))
#define repss5(i, j, k, l, m, I, J, K, L, M) \
for (int i = (J && K && L && M ? 0 : I), j = 0, k = 0, l = 0, m = 0; i < I; \
nex_ind5(i, j, k, l, m, J, K, L, M))
#define o_repss(o1, o2, o3, o4, o5, o6, o7, o8, o9, o10, name, ...) name
// i,j,k...をnまで見る
#define reps1(i, n) repss1(i, n)
#define reps2(i, j, n) repss2(i, j, n, n)
#define reps3(i, j, k, n) repss3(i, j, k, n, n, n)
#define reps4(i, j, k, l, n) repss4(i, j, k, l, n, n, n, n)
#define o_reps(o1, o2, o3, o4, o5, name, ...) name
template <class T>
void nex_repv2(int &i, int &j, int &I, int &J, vector<vector<T>> &s) {
while (1) {
j++;
if (j >= J) {
j = 0;
i++;
if (i < I) {
J = (int)s[i].size();
}
}
if (i >= I || J)
return;
}
}
template <class T>
void nex_repv3(int &i, int &j, int &k, int &I, int &J, int &K,
vector<vector<vector<T>>> &s) {
while (1) {
k++;
if (k >= K) {
k = 0;
j++;
if (j >= J) {
j = 0;
i++;
if (i >= I)
return;
}
}
J = (int)s[i].size();
K = (int)s[i][j].size();
if (J && K)
return;
}
}
#define repv_1(i, a) repss1(i, sz(a))
// 正方形である必要はない
// 直前を持つのとどっちが早いか
#define repv_2(i, j, a) \
for (int I = (int)a.size(), J = (int)a[0].size(), i = 0, j = 0; i < I; \
nex_repv2(i, j, I, J, a))
// 箱状になっている事が要求される つまり[i] 次元目の要素数は一定
#define repv_3(i, j, k, a) \
for (int I = (int)a.size(), J = (int)a[0].size(), K = (int)a[0][0].size(), \
i = 0, j = 0, k = 0; \
i < I; nex_repv3(i, j, k, I, J, K, a))
#define repv_4(i, j, k, l, a) \
repss4(i, j, k, l, sz(a), sz(a[0]), sz(a[0][0]), sz(a[0][0][0]))
#define repv_5(i, j, k, l, m, a) \
repss5(i, j, k, l, m, sz(a), sz(a[0]), sz(a[0][0]), sz(a[0][0][0]), \
sz(a[0][0][0][0]))
#define o_repv(o1, o2, o3, o4, o5, o6, name, ...) name
template <typename T> struct has_rbegin_rend {
private:
template <typename U>
static auto check(U &&obj)
-> decltype(std::rbegin(obj), std::rend(obj), std::true_type{});
static std::false_type check(...);
public:
static constexpr bool value = decltype(check(std::declval<T>()))::value;
};
template <typename T>
constexpr bool has_rbegin_rend_v = has_rbegin_rend<T>::value;
template <typename Iterator> class Range {
public:
Range(Iterator &&begin, Iterator &&end) noexcept
: m_begin(std::forward<Iterator>(begin)),
m_end(std::forward<Iterator>(end)) {}
Iterator begin() const noexcept { return m_begin; }
Iterator end() const noexcept { return m_end; }
private:
const Iterator m_begin;
const Iterator m_end;
};
template <typename Iterator>
static inline Range<Iterator> makeRange(Iterator &&begin,
Iterator &&end) noexcept {
return Range<Iterator>{std::forward<Iterator>(begin),
std::forward<Iterator>(end)};
}
template <typename T>
static inline decltype(auto)
makeReversedRange(const std::initializer_list<T> &iniList) noexcept {
return makeRange(std::rbegin(iniList), std::rend(iniList));
}
template <typename T, typename std::enable_if_t<has_rbegin_rend_v<T>,
std::nullptr_t> = nullptr>
static inline decltype(auto) makeReversedRange(T &&c) noexcept {
return makeRange(std::rbegin(c), std::rend(c));
} /* rbegin(),
rend()を持たないものはこっちに分岐させて,エラーメッセージを少なくする*/
template <typename T, typename std::enable_if<!has_rbegin_rend<T>::value,
std::nullptr_t>::type = nullptr>
static inline void makeReversedRange(T &&) noexcept {
static_assert(has_rbegin_rend<T>::value,
"Specified argument doesn't have reverse iterator.");
}
#define form1(st) \
decltype(st)::key_type k; \
decltype(st)::mapped_type v; \
for (auto &&it = st.begin(); \
it != st.end() && (k = (*it).fi, v = (*it).se, true); ++it)
#define form3(k, v, st) \
decltype(st)::key_type k; \
decltype(st)::mapped_type v; \
for (auto &&it = st.begin(); \
it != st.end() && (k = (*it).fi, v = (*it).se, true); ++it)
#define form4(k, v, st, r) \
decltype(st)::key_type k; \
decltype(st)::mapped_type v; \
for (auto &&it = st.begin(); \
it != st.end() && (k = (*it).fi, v = (*it).se, (*it).fi < r); ++it)
#define form5(k, v, st, l, r) \
decltype(st)::key_type k; \
decltype(st)::mapped_type v; \
for (auto &&it = st.lower_bound(l); \
it != st.end() && (k = (*it).fi, v = (*it).se, (*it).fi < r); ++it)
#define o_form(a, b, c, d, e, name, ...) name
template <class U> vector<U> to1d(vector<U> &a) { return a; }
template <class U> vector<U> to1d(V<V<U>> &a) {
vector<U> res;
for (auto &&a1 : a)
for (auto &&a2 : a1)
res.push_back(a2);
return res;
}
template <class U> vector<U> to1d(V<V<V<U>>> &a) {
vector<U> res;
for (auto &&a1 : a)
for (auto &&a2 : a1)
for (auto &&a3 : a2)
res.push_back(a3);
return res;
}
template <class U> vector<U> to1d(V<V<V<V<U>>>> &a) {
vector<U> res;
for (auto &&a1 : a)
for (auto &&a2 : a1)
for (auto &&a3 : a2)
for (auto &&a4 : a3)
res.push_back(a4);
return res;
}
template <class U> vector<U> to1d(V<V<V<V<V<U>>>>> &a) {
vector<U> res;
for (auto &&a1 : a)
for (auto &&a2 : a1)
for (auto &&a3 : a2)
for (auto &&a4 : a3)
for (auto &&a5 : a4)
res.push_back(a5);
return res;
}
template <class U> vector<U> to1d(V<V<V<V<V<V<U>>>>>> &a) {
vector<U> res;
for (auto &&a1 : a)
for (auto &&a2 : a1)
for (auto &&a3 : a2)
for (auto &&a4 : a3)
for (auto &&a5 : a4)
for (auto &&a6 : a5)
res.push_back(a6);
return res;
}
// ↑@オーバーロード隔離
// rep系はインデックス、for系は中身
#define rep(...) o_rep(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rer(...) o_rep(__VA_ARGS__, rer4, rer3, rer2, )(__VA_ARGS__)
// char用のrep
#define repc(i, m, n) for (char i = m, repc3lim = n; i < repc3lim; ++i)
// i,j,k...をnまで見る
#define reps(...) o_reps(__VA_ARGS__, reps4, reps3, reps2, reps1, )(__VA_ARGS__)
#define repss(...) \
o_repss(__VA_ARGS__, repss5, a, repss4, a, repss3, a, repss2, a, \
repss1)(__VA_ARGS__)
// vectorのindexを走査する
// repv(i,j,vvi)
#define repv(...) \
o_repv(__VA_ARGS__, repv_5, repv_4, repv_3, repv_2, repv_1, )(__VA_ARGS__)
// 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(a, b) for (auto &&a : b)
#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]))
// 0の左と nの右
#define forlr(v, d, lng, rng) \
v1_ = lng, v2_ = rng; \
for (auto prev = v1_, 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 ? v2_ : d[index_ + 1]))
#define form(...) \
o_form(__VA_ARGS__, form5, form4, form3, form2, form1)(__VA_ARGS__)
#define forp(k, v, m) \
decltype(m)::value_type::first_type k; \
decltype(m)::value_type::second_type v; \
for (auto &&it = m.begin(); \
it != m.end() && (k = (*it).fi, v = (*it).se, true); ++it)
// マクロ 定数
#define k3 1010
#define k4 10101
#define k5 101010
#define k6 1010101
#define k7 10101010
const ll inf = (ll)1e9 + 100;
const ll linf = (ll)1e18 + 100;
const char infc = '{';
const string infs = "{";
const double eps = 1e-9;
const double PI = 3.1415926535897932384626433832795029L;
// マクロ省略形 関数等
#define arsz(a) (sizeof(a) / sizeof(a[0]))
#define sz(a) ((ll)(a).size())
#define mp make_pair
#define pb pop_back
#define pf push_front
#define eb emplace_back
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
constexpr bool ev(ll a) { return !(a & 1); }
constexpr bool od(ll a) { return (a & 1); }
//@拡張系 こう出来るべきというもの
// 埋め込み 存在を意識せずに機能を増やされているもの
namespace std {
template <> class hash<std::pair<signed, signed>> {
public:
size_t operator()(const std::pair<signed, signed> &x) const {
return hash<ll>()(((ll)x.first << 32) | x.second);
}
};
template <> class hash<std::pair<ll, ll>> {
public
: /*大きいllが渡されると、<<32でオーバーフローするがとりあえず問題ないと判断*/
size_t operator()(const std::pair<ll, ll> &x) const {
return hash<ll>()(((ll)x.first << 32) | x.second);
}
};
} // namespace std
// stream まとめ
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, typename U>
ostream &operator<<(ostream &os, vector<pair<T, U>> &vec) {
for (ll i = 0; i < vec.size(); ++i) {
os << vec[i];
if (i != vec.size() - 1)
os << endl;
}
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 <typename W, typename H> void resize(W &vec, const H head) {
vec.resize(head);
}
template <typename W, typename H, typename... T>
void resize(W &vec, const H &head, const T... tail) {
vec.resize(head);
for (auto &v : vec)
resize(v, tail...);
}
// template<typename W, typename H> void resize(vector<W> &vec, const H head) {
// vec.resize(head); } template<typename W, typename H, typename ... T> void
// resize(vector<W> &vec, const H &head, const T ... tail) {vec.resize(head);for
// (auto &v: vec)resize(v, tail...);}
template <typename T, typename F> bool all_of2(T &v, F f) { return f(v); }
template <typename T, typename F> bool all_of2(vector<T> &v, F f) {
rep(i, sz(v)) {
if (!all_of2(v[i], f))
return false;
}
return true;
}
template <typename T, typename F> bool any_of2(T &v, F f) { return f(v); }
template <typename T, typename F> bool any_of2(vector<T> &v, F f) {
rep(i, sz(v)) {
if (any_of2(v[i], f))
return true;
}
return false;
}
template <typename T, typename F> bool none_of2(T &v, F f) { return f(v); }
template <typename T, typename F> bool none_of2(vector<T> &v, F f) {
rep(i, sz(v)) {
if (none_of2(v[i], f))
return false;
}
return true;
}
template <typename T, typename F> bool find_if2(T &v, F f) { return f(v); }
template <typename T, typename F> ll find_if2(vector<T> &v, F f) {
rep(i, sz(v)) {
if (find_if2(v[i], f))
return i;
}
return sz(v);
}
template <typename T, typename F> bool rfind_if2(T &v, F f) { return f(v); }
template <typename T, typename F> ll rfind_if2(vector<T> &v, F f) {
rer(i, sz(v) - 1) {
if (rfind_if2(v[i], f))
return i;
}
return -1;
}
template <class T> bool contains(string &s, const T &v) {
return s.find(v) != string::npos;
}
template <typename T> bool contains(vector<T> &v, const T &val) {
return std::find(v.begin(), v.end(), val) != v.end();
}
template <typename T, typename F> bool contains_if2(vector<T> &v, F f) {
return find_if(v.begin(), v.end(), f) != v.end();
}
template <typename T, typename F> ll count_if2(T &v, F f) { return f(v); }
template <typename T, typename F> ll count_if2(vector<T> &vec, F f) {
ll ret = 0;
fora(v, vec) ret += count_if2(v, f);
return ret;
}
template <typename T, typename F> void for_each2(T &v, F f) { f(v); }
template <typename T, typename F> void for_each2(vector<T> &vec, F f) {
fora(v, vec) for_each2(v, f);
}
template <typename W> ll count_od(vector<W> &a) {
return count_if2(a, [](ll v) { return v & 1; });
}
template <typename W> ll count_ev(vector<W> &a) {
return count_if2(a, [](ll v) { return !(v & 1); });
}
// 削除された要素の数を返す
template <typename T, typename F> int erase_if2(vector<T> &v, F f) {
vector<T> nv;
int cou = 0;
rep(i, sz(v)) {
if (f(v[i])) {
cou++;
} else {
nv.push_back(v[i]);
}
}
v = nv;
return cou;
}
template <typename T, typename F> int erase_if2(vector<vector<T>> &v, F f) {
int cou = 0;
rep(i, sz(v)) { cou += erase_if2(v[i], f); }
return cou;
}
#define all_of(a, right) all_of2(a, lam(right))
#define all_of_f(a, f) all_of2(a, f)
#define any_of(a, right) any_of2(a, lam(right))
#define any_of_f(a, f) any_of2(a, f)
#define none_of(a, right) none_of2(a, lam(right))
#define none_of_f(a, f) none_of2(a, f)
#define find_if(a, right) find_if2(a, lam(right))
#define find_if_f(a, f) find_if2(a, f)
#define rfind_if(a, right) rfind_if2(a, lam(right))
#define rfind_if_f(a, f) rfind_if2(a, f)
#define contains_if(a, right) contains_if2(a, lam(right))
#define contains_if_f(a, f) contains_if2(a, f)
#define count_if(a, right) count_if2(a, lam(right))
#define count_if_f(a, f) count_if2(a, f)
#define for_each(a, right) \
do { \
fora(v, a) { v right; } \
} while (0)
#define for_each_f(a, f) \
do { \
fora(v, a) { f(v); } \
} while (0)
#define erase_if(a, right) erase_if2(a, lam(right))
#define erase_if_f(a, f) erase_if2(a, f)
template <class T, class U> void replace(vector<T> &a, T key, U v) {
replace(a.begin(), a.end(), key, v);
}
void replace(str &a, char key, str v) {
if (v == "")
a.erase(remove(all(a), key), a.end());
}
void replace(str &a, char key, char v) { replace(all(a), key, v); }
// keyと同じかどうか01で置き換える
template <class T, class U> void replace(vector<T> &a, U k) {
rep(i, sz(a)) a[i] = a[i] == k;
}
template <class T, class U> void replace(vector<vector<T>> &a, U k) {
rep(i, sz(a)) rep(j, sz(a[0])) a[i][j] = a[i][j] == k;
}
template <class T> void replace(T &a) { replace(a, '#'); }
void replace(str &a, str key, str v) {
stringstream t;
ll kn = sz(key);
std::string::size_type Pos(a.find(key));
ll l = 0;
while (Pos != std::string::npos) {
t << a.substr(l, Pos - l);
t << v;
l = Pos + kn;
Pos = a.find(key, Pos + kn);
}
t << a.substr(l, sz(a) - l);
a = t.str();
}
template <class T> bool includes(vector<T> &a, vector<T> &b) {
vi c = a;
vi d = b;
sort(all(c));
sort(all(d));
return includes(all(c), all(d));
}
template <class T> bool is_permutation(vector<T> &a, vector<T> &b) {
return is_permutation(all(a), all(b));
}
template <class T> bool next_permutation(vector<T> &a) {
return next_permutation(all(a));
}
void iota(vector<ll> &ve, ll s, ll n) {
ve.resize(n);
iota(all(ve), s);
}
vi iota(ll s, ll len) {
vi ve(len);
iota(all(ve), s);
return ve;
}
template <class A, class B> auto vtop(vector<A> &a, vector<B> &b) {
assert(sz(a) == sz(b)); /*stringを0で初期化できない */
vector<pair<A, B>> res;
rep(i, sz(a)) res.eb(a[i], b[i]);
return res;
}
template <class A, class B>
void ptov(vector<pair<A, B>> &p, vector<A> &a, vector<B> &b) {
a.resize(sz(p)), b.resize(sz(p));
rep(i, sz(p)) a[i] = p[i].fi, b[i] = p[i].se;
}
template <class A, class B, class C>
auto vtot(vector<A> &a, vector<B> &b, vector<C> &c) {
assert(sz(a) == sz(b) && sz(b) == sz(c));
vector<T2<A, B, C>> res;
rep(i, sz(a)) res.eb(a[i], b[i], c[i]);
return res;
}
template <class A, class B, class C, class D>
auto vtof(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) {
assert(sz(a) == sz(b) && sz(b) == sz(c) && sz(c) == sz(d));
vector<F2<A, B, C, D>> res;
rep(i, sz(a)) res.eb(a[i], b[i], c[i], d[i]);
return res;
}
enum pcomparator { fisi, fisd, fdsi, fdsd, sifi, sifd, sdfi, sdfd };
enum tcomparator {
fisiti,
fisitd,
fisdti,
fisdtd,
fdsiti,
fdsitd,
fdsdti,
fdsdtd,
fitisi,
fitisd,
fitdsi,
fitdsd,
fdtisi,
fdtisd,
fdtdsi,
fdtdsd,
sifiti,
sifitd,
sifdti,
sifdtd,
sdfiti,
sdfitd,
sdfdti,
sdfdtd,
sitifi,
sitifd,
sitdfi,
sitdfd,
sdtifi,
sdtifd,
sdtdfi,
sdfdfd,
tifisi,
tifisd,
tifdsi,
tifdsd,
tdfisi,
tdfisd,
tdfdsi,
tdfdsd,
tisifi,
tisifd,
tisdfi,
tisdfd,
tdsifi,
tdsifd,
tdsdfi,
tdsdfd
};
template <class A, class B> void sort(vector<pair<A, B>> &a, pcomparator type) {
typedef pair<A, B> U;
if (type == fisi)
sort(all(a),
[&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se < r.se; });
else if (type == fisd)
sort(all(a),
[&](U l, U r) { return l.fi != r.fi ? l.fi < r.fi : l.se > r.se; });
else if (type == fdsi)
sort(all(a),
[&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se < r.se; });
else if (type == fdsd)
sort(all(a),
[&](U l, U r) { return l.fi != r.fi ? l.fi > r.fi : l.se > r.se; });
else if (type == sifi)
sort(all(a),
[&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi < r.fi; });
else if (type == sifd)
sort(all(a),
[&](U l, U r) { return l.se != r.se ? l.se < r.se : l.fi > r.fi; });
else if (type == sdfi)
sort(all(a),
[&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi < r.fi; });
else if (type == sdfd)
sort(all(a),
[&](U l, U r) { return l.se != r.se ? l.se > r.se : l.fi > r.fi; });
};
template <class U> void sort(vector<U> &a, pcomparator type) {
if (type == fisi)
sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s < r.s; });
else if (type == fisd)
sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f < r.f : l.s > r.s; });
else if (type == fdsi)
sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s < r.s; });
else if (type == fdsd)
sort(all(a), [&](U l, U r) { return l.f != r.f ? l.f > r.f : l.s > r.s; });
else if (type == sifi)
sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f < r.f; });
else if (type == sifd)
sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s < r.s : l.f > r.f; });
else if (type == sdfi)
sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f < r.f; });
else if (type == sdfd)
sort(all(a), [&](U l, U r) { return l.s != r.s ? l.s > r.s : l.f > r.f; });
};
template <class A, class B, class C, class D>
void sort(vector<F2<A, B, C, D>> &a, pcomparator type) {
typedef F2<A, B, C, D> U;
if (type == fisi)
sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b < r.b; });
else if (type == fisd)
sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a < r.a : l.b > r.b; });
else if (type == fdsi)
sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b < r.b; });
else if (type == fdsd)
sort(all(a), [&](U l, U r) { return l.a != r.a ? l.a > r.a : l.b > r.b; });
else if (type == sifi)
sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a < r.a; });
else if (type == sifd)
sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b < r.b : l.a > r.a; });
else if (type == sdfi)
sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a < r.a; });
else if (type == sdfd)
sort(all(a), [&](U l, U r) { return l.b != r.b ? l.b > r.b : l.a > r.a; });
};
template <class U> void sort(vector<U> &a, tcomparator type) {
if (type == 0)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t < r.t;
});
else if (type == 1)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s < r.s : l.t > r.t;
});
else if (type == 2)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t < r.t;
});
else if (type == 3)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f < r.f : l.s != r.s ? l.s > r.s : l.t > r.t;
});
else if (type == 4)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t < r.t;
});
else if (type == 5)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s < r.s : l.t > r.t;
});
else if (type == 6)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t < r.t;
});
else if (type == 7)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f > r.f : l.s != r.s ? l.s > r.s : l.t > r.t;
});
else if (type == 8)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s < r.s;
});
else if (type == 9)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t < r.t : l.s > r.s;
});
else if (type == 10)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s < r.s;
});
else if (type == 11)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f < r.f : l.t != r.t ? l.t > r.t : l.s > r.s;
});
else if (type == 12)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s < r.s;
});
else if (type == 13)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t < r.t : l.s > r.s;
});
else if (type == 14)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s < r.s;
});
else if (type == 15)
sort(all(a), [&](U l, U r) {
return l.f != r.f ? l.f > r.f : l.t != r.t ? l.t > r.t : l.s > r.s;
});
else if (type == 16)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t < r.t;
});
else if (type == 17)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f < r.f : l.t > r.t;
});
else if (type == 18)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t < r.t;
});
else if (type == 19)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s < r.s : l.f != r.f ? l.f > r.f : l.t > r.t;
});
else if (type == 20)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t < r.t;
});
else if (type == 21)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f < r.f : l.t > r.t;
});
else if (type == 22)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t < r.t;
});
else if (type == 23)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s > r.s : l.f != r.f ? l.f > r.f : l.t > r.t;
});
else if (type == 24)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f < r.f;
});
else if (type == 25)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t < r.t : l.f > r.f;
});
else if (type == 26)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f < r.f;
});
else if (type == 27)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s < r.s : l.t != r.t ? l.t > r.t : l.f > r.f;
});
else if (type == 28)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f < r.f;
});
else if (type == 29)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t < r.t : l.f > r.f;
});
else if (type == 30)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f < r.f;
});
else if (type == 31)
sort(all(a), [&](U l, U r) {
return l.s != r.s ? l.s > r.s : l.t != r.t ? l.t > r.t : l.f > r.f;
});
else if (type == 32)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s < r.s;
});
else if (type == 33)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f < r.f : l.s > r.s;
});
else if (type == 34)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s < r.s;
});
else if (type == 35)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t < r.t : l.f != r.f ? l.f > r.f : l.s > r.s;
});
else if (type == 36)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s < r.s;
});
else if (type == 37)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f < r.f : l.s > r.s;
});
else if (type == 38)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s < r.s;
});
else if (type == 39)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t > r.t : l.f != r.f ? l.f > r.f : l.s > r.s;
});
else if (type == 40)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f < r.f;
});
else if (type == 41)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s < r.s : l.f > r.f;
});
else if (type == 42)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f < r.f;
});
else if (type == 43)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t < r.t : l.s != r.s ? l.s > r.s : l.f > r.f;
});
else if (type == 44)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f < r.f;
});
else if (type == 45)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s < r.s : l.f > r.f;
});
else if (type == 46)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f < r.f;
});
else if (type == 47)
sort(all(a), [&](U l, U r) {
return l.t != r.t ? l.t > r.t : l.s != r.s ? l.s > r.s : l.f > r.f;
});
}
template <class A, class B, class C, class D>
void sort(vector<F2<A, B, C, D>> &a, tcomparator type) {
typedef F2<A, B, C, D> U;
if (type == 0)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c < r.c;
});
else if (type == 1)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b < r.b : l.c > r.c;
});
else if (type == 2)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c < r.c;
});
else if (type == 3)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a < r.a : l.b != r.b ? l.b > r.b : l.c > r.c;
});
else if (type == 4)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c < r.c;
});
else if (type == 5)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b < r.b : l.c > r.c;
});
else if (type == 6)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c < r.c;
});
else if (type == 7)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a > r.a : l.b != r.b ? l.b > r.b : l.c > r.c;
});
else if (type == 8)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b < r.b;
});
else if (type == 9)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c < r.c : l.b > r.b;
});
else if (type == 10)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b < r.b;
});
else if (type == 11)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a < r.a : l.c != r.c ? l.c > r.c : l.b > r.b;
});
else if (type == 12)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b < r.b;
});
else if (type == 13)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c < r.c : l.b > r.b;
});
else if (type == 14)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b < r.b;
});
else if (type == 15)
sort(all(a), [&](U l, U r) {
return l.a != r.a ? l.a > r.a : l.c != r.c ? l.c > r.c : l.b > r.b;
});
else if (type == 16)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c < r.c;
});
else if (type == 17)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a < r.a : l.c > r.c;
});
else if (type == 18)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c < r.c;
});
else if (type == 19)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b < r.b : l.a != r.a ? l.a > r.a : l.c > r.c;
});
else if (type == 20)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c < r.c;
});
else if (type == 21)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a < r.a : l.c > r.c;
});
else if (type == 22)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c < r.c;
});
else if (type == 23)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b > r.b : l.a != r.a ? l.a > r.a : l.c > r.c;
});
else if (type == 24)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a < r.a;
});
else if (type == 25)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c < r.c : l.a > r.a;
});
else if (type == 26)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a < r.a;
});
else if (type == 27)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b < r.b : l.c != r.c ? l.c > r.c : l.a > r.a;
});
else if (type == 28)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a < r.a;
});
else if (type == 29)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c < r.c : l.a > r.a;
});
else if (type == 30)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a < r.a;
});
else if (type == 31)
sort(all(a), [&](U l, U r) {
return l.b != r.b ? l.b > r.b : l.c != r.c ? l.c > r.c : l.a > r.a;
});
else if (type == 32)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b < r.b;
});
else if (type == 33)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a < r.a : l.b > r.b;
});
else if (type == 34)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b < r.b;
});
else if (type == 35)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c < r.c : l.a != r.a ? l.a > r.a : l.b > r.b;
});
else if (type == 36)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b < r.b;
});
else if (type == 37)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a < r.a : l.b > r.b;
});
else if (type == 38)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b < r.b;
});
else if (type == 39)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c > r.c : l.a != r.a ? l.a > r.a : l.b > r.b;
});
else if (type == 40)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a < r.a;
});
else if (type == 41)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b < r.b : l.a > r.a;
});
else if (type == 42)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a < r.a;
});
else if (type == 43)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c < r.c : l.b != r.b ? l.b > r.b : l.a > r.a;
});
else if (type == 44)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a < r.a;
});
else if (type == 45)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b < r.b : l.a > r.a;
});
else if (type == 46)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a < r.a;
});
else if (type == 47)
sort(all(a), [&](U l, U r) {
return l.c != r.c ? l.c > r.c : l.b != r.b ? l.b > r.b : l.a > r.a;
});
}
void sort(string &a) { sort(all(a)); }
template <class T> void sort(vector<T> &a) { sort(all(a)); }
// P l, P rで f(P) の形で渡す
template <class U, class F> void sort(vector<U> &a, F f) {
sort(all(a), [&](U l, U r) { return f(l) < f(r); });
};
template <class T> void rsort(vector<T> &a) { sort(all(a), greater<T>()); };
template <class U, class F> void rsort(vector<U> &a, F f) {
sort(all(a), [&](U l, U r) { return f(l) > f(r); });
};
// F = T<T>
// 例えばreturn p.fi + p.se;
template <class A, class B> void sortp(vector<A> &a, vector<B> &b) {
auto c = vtop(a, b);
sort(c);
rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se;
}
template <class A, class B, class F>
void sortp(vector<A> &a, vector<B> &b, F f) {
auto c = vtop(a, b);
sort(c, f);
rep(i, sz(a)) a[i] = c[i].fi, b[i] = c[i].se;
}
template <class A, class B> void rsortp(vector<A> &a, vector<B> &b) {
auto c = vtop(a, b);
rsort(c);
rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second;
}
template <class A, class B, class F>
void rsortp(vector<A> &a, vector<B> &b, F f) {
auto c = vtop(a, b);
rsort(c, f);
rep(i, sz(a)) a[i] = c[i].first, b[i] = c[i].second;
}
template <class A, class B, class C>
void sortt(vector<A> &a, vector<B> &b, vector<C> &c) {
auto d = vtot(a, b, c);
sort(d);
rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t;
}
template <class A, class B, class C, class F>
void sortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) {
auto d = vtot(a, b, c);
sort(d, f);
rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t;
}
template <class A, class B, class C>
void rsortt(vector<A> &a, vector<B> &b, vector<C> &c) {
auto d = vtot(a, b, c);
rsort(d);
rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t;
}
template <class A, class B, class C, class F>
void rsortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) {
auto d = vtot(a, b, c);
rsort(d, f);
rep(i, sz(a)) a[i] = d[i].f, b[i] = d[i].s, c[i] = d[i].t;
}
template <class A, class B, class C, class D>
void sortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) {
auto e = vtof(a, b, c, d);
sort(e);
rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d;
}
template <class A, class B, class C, class D>
void rsortf(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d) {
auto e = vtof(a, b, c, d);
rsort(e);
rep(i, sz(a)) a[i] = e[i].a, b[i] = e[i].b, c[i] = e[i].c, d[i] = e[i].d;
}
// sortindex 元のvectorはソートしない
template <class T> vi sorti(vector<T> &a) {
auto b = a;
vi ind = iota(0, sz(a));
sortp(b, ind);
return ind;
} /*indexの分で型が変わるためpcomparatorが必要*/
template <class T> vi sorti(vector<T> &a, pcomparator f) {
auto b = a;
vi ind = iota(0, sz(a));
sortp(b, ind, f);
return ind;
}
template <class T, class F> vi sorti(vector<T> &a, F f) {
vi ind = iota(0, sz(a));
sort(all(ind), [&](ll x, ll y) { return f(a[x]) < f(a[y]); });
return ind;
}
template <class T> vi rsorti(vector<T> &a) {
auto b = a;
vi ind = iota(0, sz(a));
rsortp(b, ind);
return ind;
}
template <class T, class F> vi rsorti(vector<T> &a, F f) {
vi ind = iota(0, sz(a));
sort(all(ind), [&](ll x, ll y) { return f(a[x]) > f(a[y]); });
return ind;
}
template <class A, class B, class F>
vi sortpi(vector<A> &a, vector<B> &b, F f) {
auto c = vtop(a, b);
vi ind = iota(0, sz(a));
sort(all(ind), [&](ll x, ll y) { return f(c[x]) < f(c[y]); });
return ind;
}
template <class A, class B>
vi sortpi(vector<A> &a, vector<B> &b, pcomparator f) {
vi ind = iota(0, sz(a));
auto c = a;
auto d = b;
sortt(c, d, ind, f);
return ind;
}
template <class A, class B> vi sortpi(vector<A> &a, vector<B> &b) {
return sortpi(a, b, fisi);
};
template <class A, class B, class F>
vi rsortpi(vector<A> &a, vector<B> &b, F f) {
auto c = vtop(a, b);
vi ind = iota(0, sz(a));
sort(all(ind), [&](ll x, ll y) { return f(c[x]) > f(c[y]); });
return ind;
}
template <class A, class B> vi rsortpi(vector<A> &a, vector<B> &b) {
return sortpi(a, b, fdsd);
};
template <class A, class B, class C, class F>
vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) {
auto d = vtot(a, b, c);
vi ind = iota(0, sz(a));
sort(all(ind), [&](ll x, ll y) { return f(d[x]) < f(d[y]); });
return ind;
}
template <class A, class B, class C>
vi sortti(vector<A> &a, vector<B> &b, vector<C> &c, pcomparator f) {
vi ind = iota(0, sz(a));
auto d = vtof(a, b, c, ind);
sort(d, f);
rep(i, sz(a)) ind[i] = d[i].d;
return ind;
}
template <class A, class B, class C>
vi sortti(vector<A> &a, vector<B> &b, vector<C> &c) {
vi ind = iota(0, sz(a));
sort(all(ind), [&](ll x, ll y) {
if (a[x] == a[y]) {
if (b[x] == b[y])
return c[x] < c[y];
else
return b[x] < b[y];
} else {
return a[x] < a[y];
}
});
return ind;
}
template <class A, class B, class C, class F>
vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c, F f) {
auto d = vtot(a, b, c);
vi ind = iota(0, sz(a));
sort(all(ind), [&](ll x, ll y) { return f(d[x]) > f(d[y]); });
return ind;
}
template <class A, class B, class C>
vi rsortti(vector<A> &a, vector<B> &b, vector<C> &c) {
vi ind = iota(0, sz(a));
sort(all(ind), [&](ll x, ll y) {
if (a[x] == a[y]) {
if (b[x] == b[y])
return c[x] > c[y];
else
return b[x] > b[y];
} else {
return a[x] > a[y];
}
});
return ind;
}
template <class T> void sort2(vector<vector<T>> &a) {
for (ll i = 0, n = a.size(); i < n; ++i)
sort(a[i]);
}
template <class T> void rsort2(vector<vector<T>> &a) {
for (ll i = 0, n = a.size(); i < n; ++i)
rsort(a[i]);
}
template <typename A, size_t N, typename T> void fill(A (&a)[N], const T &v) {
rep(i, N) a[i] = v;
}
template <typename A, size_t N, size_t O, typename T>
void fill(A (&a)[N][O], const T &v) {
rep(i, N) rep(j, O) a[i][j] = v;
}
template <typename A, size_t N, size_t O, size_t P, typename T>
void fill(A (&a)[N][O][P], const T &v) {
rep(i, N) rep(j, O) rep(k, P) a[i][j][k] = v;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, typename T>
void fill(A (&a)[N][O][P][Q], const T &v) {
rep(i, N) rep(j, O) rep(k, P) rep(l, Q) a[i][j][k][l] = v;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R,
typename T>
void fill(A (&a)[N][O][P][Q][R], const T &v) {
rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) a[i][j][k][l][m] = v;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R,
size_t S, typename T>
void fill(A (&a)[N][O][P][Q][R][S], const T &v) {
rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) rep(n, S)
a[i][j][k][l][m][n] = v;
}
template <typename W, typename T> void fill(W &xx, const T vall) { xx = vall; }
template <typename W, typename T> void fill(vector<W> &vecc, const T vall) {
for (auto &&vx : vecc)
fill(vx, vall);
}
template <typename W, typename T> void fill(vector<W> &xx, ll len, const T v) {
rep(i, len) xx[i] = v;
}
template <typename W, typename T>
void fill(vector<vector<W>> &xx, int sh, int th, int sw, int tw, T v) {
rep(h, sh, th) rep(w, sw, tw) xx[h][w] = v;
}
template <class T, class U> void fill(vector<T> &a, vi &ind, U val) {
fora(v, ind) a[v] = val;
}
template <typename A, size_t N> A sum(A (&a)[N]) {
A res = 0;
rep(i, N) res += a[i];
return res;
}
template <typename A, size_t N, size_t O> A sum(A (&a)[N][O]) {
A res = 0;
rep(i, N) rep(j, O) res += a[i][j];
return res;
}
template <typename A, size_t N, size_t O, size_t P> A sum(A (&a)[N][O][P]) {
A res = 0;
rep(i, N) rep(j, O) rep(k, P) res += a[i][j][k];
return res;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q>
A sum(A (&a)[N][O][P][Q]) {
A res = 0;
rep(i, N) rep(j, O) rep(k, P) rep(l, Q) res += a[i][j][k][l];
return res;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R>
A sum(A (&a)[N][O][P][Q][R]) {
A res = 0;
rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) res += a[i][j][k][l][m];
return res;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R,
size_t S>
A sum(A (&a)[N][O][P][Q][R][S]) {
A res = 0;
rep(i, N) rep(j, O) rep(k, P) rep(l, Q) rep(m, R) rep(n, S) res +=
a[i][j][k][l][m][n];
return res;
}
//@汎用便利関数 入力
ll in() {
ll ret;
cin >> ret;
return ret;
}
string sin() {
string ret;
cin >> ret;
return ret;
}
template <class T> void in(T &head) { cin >> head; }
template <class T, class... U> void in(T &head, U &...tail) {
cin >> head;
in(tail...);
}
#define o_din(o1, o2, o3, o4, o5, o6, name, ...) name
#define din1(a) \
ll a; \
cin >> a
#define din2(a, b) \
ll a, b; \
cin >> a >> b
#define din3(a, b, c) \
ll a, b, c; \
cin >> a >> b >> c
#define din4(a, b, c, d) \
ll a, b, c, d; \
cin >> a >> b >> c >> d
#define din5(a, b, c, d, e) \
ll a, b, c, d, e; \
cin >> a >> b >> c >> d >> e
#define din6(a, b, c, d, e, f) \
ll a, b, c, d, e, f; \
cin >> a >> b >> c >> d >> e >> f
#define din(...) \
o_din(__VA_ARGS__, din6, din5, din4, din3, din2, din1)(__VA_ARGS__)
#define o_dins(o1, o2, o3, o4, o5, o6, name, ...) name
#define dins1(a) \
str a; \
cin >> a
#define dins2(a, b) \
str a, b; \
cin >> a >> b
#define dins3(a, b, c) \
str a, b, c; \
cin >> a >> b >> c
#define dins4(a, b, c, d) \
str a, b, c, d; \
cin >> a >> b >> c >> d
#define dins5(a, b, c, d, e) \
str a, b, c, d, e; \
cin >> a >> b >> c >> d >> e
#define dins6(a, b, c, d, e, f) \
str a, b, c, d, e, f; \
cin >> a >> b >> c >> d >> e >> f
#define dins(...) \
o_dins(__VA_ARGS__, dins6, dins5, dins4, dins3, dins2, dins1)(__VA_ARGS__)
#define o_dind(o1, o2, o3, o4, name, ...) name
#define din1d(a) \
din1(a); \
a--
#define din2d(a, b) \
din2(a, b); \
a--, b--
#define din3d(a, b, c) \
din3(a, b, c); \
a--, b--, c--
#define din4d(a, b, c, d) \
din4(a, b, c, d); \
a--, b--, c--, d--
#define dind(...) o_dind(__VA_ARGS__, din4d, din3d, din2d, din1d)(__VA_ARGS__)
template <class T> void out2(T &&head) { cout << head; }
template <class T, class... U> void out2(T &&head, U &&...tail) {
cout << head << " ";
out2(tail...);
}
template <class T, class... U> void out(T &&head, U &&...tail) {
cout << head << " ";
out2(tail...);
cout << "" << endl;
}
template <class T> void out(T &&head) { cout << head << endl; }
void out() { cout << "" << endl; }
#ifdef _DEBUG
template <class T> void err2(T &&head) { cerr << head; }
template <class T, class... U> void err2(T &&head, U &&...tail) {
cerr << head << " ";
err2(tail...);
}
template <class T, class... U> void err(T &&head, U &&...tail) {
cerr << head << " ";
err2(tail...);
cerr << "" << endl;
}
template <class T> void err(T &&head) { cerr << head << endl; }
void err() { cerr << "" << endl; }
template <class T> string out_m2(vector<T> &a, ll W = inf) {
stringstream ss;
if (W == inf)
W = min(sz(a), 12ll);
if (sz(a) == 0)
return ss.str();
rep(i, W) { ss << a[i] << " "; }
return ss.str();
}
template <class T>
string out_m2(vector<vector<T>> &a, ll H = inf, ll W = inf, int key = -1) {
H = min({H, sz(a), 12ll});
W = min({W, sz(a[0]), 12ll});
stringstream ss;
ss << endl;
if (key == -1)
ss << " *|";
else
ss << " " << key << "|";
rep(w, W) ss << std::right << std::setw(4) << w;
ss << "" << endl;
rep(w, W * 4 + 3) ss << "_";
ss << "" << endl;
rep(h, H) {
ss << std::right << std::setw(2) << h << "|";
rep(w, min(sz(a[h]), 12ll)) {
if (abs(a[h][w]) == linf)
ss << " e"
<< "";
else
ss << std::right << std::setw(4) << a[h][w];
}
ss << "" << endl;
}
return ss.str();
}
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();
}
template <class T> string out_m2(T &a) {
stringstream ss;
ss << a;
return ss.str();
}
template <class T> string out_m(vector<T> &a, ll W = inf) {
stringstream ss;
if (W == inf)
W = min(sz(a), 12ll);
if (sz(a) == 0)
return ss.str();
rep(i, W) { ss << a[i] << " "; }
ss << "" << endl;
return ss.str();
}
template <class T>
string out_m(vector<vector<T>> &a, ll H = inf, ll W = inf, int key = -1) {
H = min({H, sz(a), 12ll});
W = min({W, sz(a[0]), 12ll});
stringstream ss;
ss << endl;
if (key == -1)
ss << " *|";
else
ss << " " << key << "|";
rep(w, W) ss << std::right << std::setw(4) << w;
ss << "" << endl;
rep(w, W * 4 + 3) ss << "_";
ss << "" << endl;
rep(h, H) {
ss << std::right << std::setw(2) << h << "|";
rep(w, min(sz(a[h]), 12ll)) {
if (abs(a[h][w]) == linf)
ss << " e"
<< "";
else
ss << std::right << std::setw(4) << a[h][w];
}
ss << "" << endl;
}
ss << endl;
return ss.str();
}
template <class T>
string out_m(vector<vector<vector<T>>> &a, ll H = inf, ll W = inf, ll U = inf) {
stringstream ss;
if (H == inf)
H = 5;
H = min(H, sz(a));
rep(i, H) {
ss << endl;
ss << out_m(a[i], W, U, i);
}
ss << endl;
return ss.str();
}
string out_m(int a) {
stringstream ss;
ss << a << endl;
return ss.str();
}
template <class T> string out_m(T &a) {
stringstream ss;
ss << a << endl;
return ss.str();
}
template <class T> void outv(vector<T> &a, ll W = inf) {
cout << out_m(a, W) << endl;
}
template <class T>
void outv(vector<vector<T>> &a, ll H = linf, ll W = linf, int key = -1) {
cout << out_m(a, H, W, key) << endl;
}
template <class T>
void outv(vector<vector<vector<T>>> &a, ll H = linf, ll W = linf, ll U = linf) {
cout << out_m(a, H, W, U) << endl;
}
#else
template <class T> void outv(vector<T> &a, ll W = inf) {
rep(i, min(W, sz(a))) { cout << a[i] << " "; }
cout << "" << endl;
}
template <class T>
void outv(vector<vector<T>> &a, ll H = linf, ll W = linf, int key = -1) {
rep(i, min(H, sz(a))) { outv(a[i], W); }
}
template <class T>
void outv(vector<vector<vector<T>>> &a, ll H = linf, ll W = linf, ll U = linf) {
;
}
#define err(...) ;
#endif
template <class T> void outl(vector<T> &a, int n = inf) {
rep(i, min(n, sz(a))) cout << a[i] << endl;
}
// テーブルをスペースなしで出力
template <class T> void outt(vector<vector<T>> &a) {
rep(i, sz(a)) {
rep(j, sz(a[i])) { cout << a[i][j]; }
cout << endl;
}
}
// int型をbit表記で出力
void outb(int a) { cout << bitset<20>(a) << endl; }
template <class T> void na(vector<T> &a, ll n) {
a.resize(n);
rep(i, n) cin >> a[i];
}
#define dna(a, n) \
vi a(n); \
rep(dnai, n) cin >> a[dnai];
#define dnad(a, n) \
vi a(n); \
rep(dnai, n) cin >> a[dnai], a[dnai]--;
template <class T> void nao(vector<T> &a, ll n) {
a.resize(n + 1);
a[0] = 0;
rep(i, n) cin >> a[i + 1];
}
template <class T> void naod(vector<T> &a, ll n) {
a.resize(n + 1);
a[0] = 0;
rep(i, n) cin >> a[i + 1], a[i + 1]--;
}
template <class T> void nad(vector<T> &a, ll n) {
a.resize(n);
rep(i, n) cin >> a[i], a[i]--;
}
template <class T, class U> void na2(vector<T> &a, vector<U> &b, ll n) {
a.resize(n);
b.resize(n);
rep(i, n) cin >> a[i] >> b[i];
}
#define dna2(a, b, n) \
vi a(n), b(n); \
rep(dna2i, n) cin >> a[dna2i] >> b[dna2i];
template <class T, class U> void nao2(vector<T> &a, vector<U> &b, ll n) {
a.resize(n + 1);
b.resize(n + 1);
a[0] = b[0] = 0;
rep(i, n) cin >> a[i + 1] >> b[i + 1];
}
#define dna2d(a, b, n) \
vi a(n), b(n); \
rep(dna2di, n) { \
cin >> a[dna2di] >> b[dna2di]; \
a[dna2di]--, b[dna2di]--; \
}
template <class T, class U> void na2d(vector<T> &a, vector<U> &b, ll n) {
a.resize(n);
b.resize(n);
rep(i, n) cin >> a[i] >> b[i], a[i]--, b[i]--;
}
template <class T, class U, class W>
void na3(vector<T> &a, vector<U> &b, vector<W> &c, ll n) {
a.resize(n);
b.resize(n);
c.resize(n);
rep(i, n) cin >> a[i] >> b[i] >> c[i];
}
#define dna3(a, b, c, n) \
vi a(n), b(n), c(n); \
rep(dna3i, n) cin >> a[dna3i] >> b[dna3i] >> c[dna3i];
template <class T, class U, class W>
void na3d(vector<T> &a, vector<U> &b, vector<W> &c, ll n) {
a.resize(n);
b.resize(n);
c.resize(n);
rep(i, n) cin >> a[i] >> b[i] >> c[i], a[i]--, b[i]--, c[i]--;
}
#define dna3d(a, b, c, n) \
vi a(n), b(n), c(n); \
rep(dna3di, n) { \
cin >> a[dna3di] >> b[dna3di] >> c[dna3di]; \
a[dna3di]--, b[dna3di]--, c[dna3di]--; \
}
template <class T, class U, class W, class X>
void na4(vector<T> &a, vector<U> &b, vector<W> &c, vector<X> &d, ll n) {
a.resize(n);
b.resize(n);
c.resize(n);
d.resize(n);
rep(i, n) cin >> a[i] >> b[i] >> c[i] >> d[i];
}
#define dna4(a, b, c, d, n) \
vi a(n), b(n), c(n), d(n); \
rep(dna4i, n) cin >> a[dna4i] >> b[dna4i] >> c[dna4i] >> d[dna4i];
#define dna4d(a, b, c, d, n) \
vi a(n), b(n), c(n), d(n); \
rep(dna4i, n) cin >> a[dna4i] >> b[dna4i] >> c[dna4i] >> d[dna4i], \
--a[dna4i], --b[dna4i], --c[dna4i], --d[dna4i];
#define nt(a, h, w) \
resize(a, h, w); \
rep(nthi, h) rep(ntwi, w) cin >> a[nthi][ntwi];
#define ntd(a, h, w) \
resize(a, h, w); \
rep(ntdhi, h) rep(ntdwi, w) cin >> a[ntdhi][ntdwi], a[ntdhi][ntdwi]--;
#define ntp(a, h, w) \
resize(a, h + 2, w + 2); \
fill(a, '#'); \
rep(ntphi, 1, h + 1) rep(ntpwi, 1, w + 1) cin >> a[ntphi][ntpwi];
// デバッグ
#define sp << " " <<
#define deb1(x) debugName(x) << " = " << out_m2(x)
#define deb2(x, ...) deb1(x) << ", " << deb1(__VA_ARGS__)
#define deb3(x, ...) deb1(x) << ", " << deb2(__VA_ARGS__)
#define deb4(x, ...) deb1(x) << ", " << deb3(__VA_ARGS__)
#define deb5(x, ...) deb1(x) << ", " << deb4(__VA_ARGS__)
#define deb6(x, ...) deb1(x) << ", " << deb5(__VA_ARGS__)
#define deb7(x, ...) deb1(x) << ", " << deb6(__VA_ARGS__)
#define deb8(x, ...) deb1(x) << ", " << deb7(__VA_ARGS__)
#define deb9(x, ...) deb1(x) << ", " << deb8(__VA_ARGS__)
#define deb10(x, ...) deb1(x) << ", " << deb9(__VA_ARGS__)
#define o_ebug(o1, o2, o3, o4, o5, o6, o7, o8, o9, o10, name, ...) name
#ifdef _DEBUG
#define deb(...) \
cerr << o_ebug(__VA_ARGS__, deb10, deb9, deb8, deb7, deb6, deb5, deb4, deb3, \
deb2, deb1)(__VA_ARGS__) \
<< endl
#else
#define deb(...) ;
#endif
#define debugline(x) \
cerr << x << " " \
<< "(L:" << __LINE__ << ")" << '\n'
//@formatter:off
// よく使うクラス、構造体
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;
}
// 便利関数
// テスト用
char ranc() { return (char)('a' + rand() % 26); }
ll rand(ll min, ll max) {
assert(min <= max);
if (min >= 0 && max >= 0) {
return rand() % (max + 1 - min) + min;
} else if (max < 0) {
return -rand(-max, -min);
} else {
if (rand() % 2) {
return rand(0, max);
} else {
return -rand(0, -min);
}
}
}
vi ranv(ll n, ll min, ll max) {
vi v(n);
rep(i, n) v[i] = rand(min, max);
return v;
}
str ransu(ll n) {
str s;
rep(i, n) s += (char)rand('A', 'Z');
return s;
}
str ransl(ll n) {
str s;
rep(i, n) s += (char)rand('a', 'z');
return s;
}
// 単調増加
vi ranvinc(ll n, ll min, ll max) {
vi v(n);
bool bad = 1;
while (bad) {
bad = 0;
v.resize(n);
rep(i, n) {
if (i && min > max - v[i - 1]) {
bad = 1;
break;
}
if (i)
v[i] = v[i - 1] + rand(min, max - v[i - 1]);
else
v[i] = rand(min, max);
}
}
return v;
}
// 便利 汎用
void ranvlr(ll n, ll min, ll max, vi &l, vi &r) {
l.resize(n);
r.resize(n);
rep(i, n) {
l[i] = rand(min, max);
r[i] = l[i] + rand(0, max - l[i]);
}
}
vp run_length(vi &a) {
vp ret;
ret.eb(a[0], 1);
rep(i, 1, sz(a)) {
if (ret.back().fi == a[i]) {
ret.back().se++;
} else {
ret.eb(a[i], 1);
}
}
return ret;
}
vector<pair<char, ll>> run_length(string &a) {
vector<pair<char, ll>> ret;
ret.eb(a[0], 1);
rep(i, 1, sz(a)) {
if (ret.back().fi == a[i]) {
ret.back().se++;
} else {
ret.eb(a[i], 1);
}
}
return ret;
}
template <class F> ll mgr(ll ok, ll ng, F f) {
if (ok < ng)
while (ng - ok > 1) {
ll mid = (ok + ng) / 2;
if (f(mid))
ok = mid;
else
ng = mid;
}
else
while (ok - ng > 1) {
ll mid = (ok + ng) / 2;
if (f(mid))
ok = mid;
else
ng = mid;
}
return ok;
}
// strを整数として比較
string smax(str &a, str b) {
if (sz(a) < sz(b)) {
return b;
} else if (sz(a) > sz(b)) {
return a;
} else if (a < b)
return b;
else
return a;
}
// strを整数として比較
string smin(str &a, str b) {
if (sz(a) > sz(b)) {
return b;
} else if (sz(a) < sz(b)) {
return a;
} else if (a > b)
return b;
else
return a;
}
// エラー-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;
}
ll count(str &a, str k) {
ll ret = 0, len = k.length();
auto pos = a.find(k);
while (pos != string::npos)
pos = a.find(k, pos + len), ++ret;
return ret;
}
vi count(str &a) {
vi cou(26);
char c = 'a';
if ('A' <= a[0] && a[0] <= 'Z')
c = 'A';
rep(i, sz(a))++ cou[a[i] - c];
return cou;
}
#define couif count_if
// algorythm
ll rev(ll a) {
ll res = 0;
while (a) {
res *= 10;
res += a % 10;
a /= 10;
}
return res;
}
template <class T> void rev(vector<T> &a) { reverse(all(a)); }
template <class U> void rev(vector<vector<U>> &a) {
vector<vector<U>> b(sz(a[0]), vector<U>(sz(a)));
rep(h, sz(a)) rep(w, sz(a[0])) b[w][h] = a[h][w];
a = b;
}
void rev(string &a) { reverse(all(a)); }
constexpr ll p10[] = {1,
10,
100,
1000,
10000,
100000,
1000000,
10000000,
100000000,
1000000000,
10000000000ll,
100000000000ll,
1000000000000ll,
10000000000000ll,
100000000000000ll,
1000000000000000ll,
10000000000000000ll,
100000000000000000ll,
1000000000000000000ll};
ll get(ll a, ll keta) { return (a / (ll)pow(10, keta)) % 10; }
// 0は0桁
ll keta(ll v) {
if (v < p10[9]) {
if (v < p10[4]) {
if (v < p10[2]) {
if (v < p10[1]) {
if (v < p10[0])
return 0;
else
return 1;
} else
return 2;
} else {
if (v < p10[3])
return 3;
else
return 4;
}
} else {
if (v < p10[7]) {
if (v < p10[5])
return 5;
else if (v < p10[6])
return 6;
else
return 7;
} else {
if (v < p10[8])
return 8;
else
return 9;
}
}
} else {
if (v < p10[13]) {
if (v < p10[11]) {
if (v < p10[10])
return 10;
else
return 11;
} else {
if (v < p10[12])
return 12;
else
return 13;
}
} else {
if (v < p10[15]) {
if (v < p10[14])
return 14;
else
return 15;
} else {
if (v < p10[17]) {
if (v < p10[16])
return 16;
else
return 17;
} else {
if (v < p10[18])
return 18;
else
return 19;
}
}
}
}
}
ll dsum(ll v, ll sin = 10) {
ll ret = 0;
for (; v; v /= sin)
ret += v % sin;
return ret;
}
ll mask10(ll v) { return p10[v] - 1; }
// 変換系
//[v] := iとなるようなvectorを返す
// 存在しない物は-1
template <class T> auto keys(T &a) {
vector<decltype((a.begin())->fi)> res;
for (auto &&k : a)
res.push_back(k.fi);
return res;
}
template <class T> auto values(T &a) {
vector<decltype((a.begin())->se)> res;
for (auto &&k : a)
res.push_back(k.se);
return res;
}
template <class T, class U> bool chma(T &a, const U &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T, class U> bool chmi(T &a, const U &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template <class T> T min(T a, signed b) { return a < b ? a : b; }
template <class T> T max(T a, signed b) { return a < b ? b : a; }
template <class T> T min(T a, T b, T c) {
return a >= b ? b >= c ? c : b : a >= c ? c : a;
}
template <class T> T max(T a, T b, T c) {
return a <= b ? b <= c ? c : b : a <= c ? c : a;
}
template <class T> T min(vector<T> &a) { return *min_element(all(a)); }
template <class T> T mini(vector<T> &a) {
return min_element(all(a)) - a.begin();
}
template <class T> T min(vector<T> &a, ll n) {
return *min_element(a.begin(), a.begin() + min(n, sz(a)));
}
template <class T> T min(vector<T> &a, ll s, ll n) {
return *min_element(a.begin() + s, a.begin() + min(n, sz(a)));
}
template <class T> T max(vector<T> &a) { return *max_element(all(a)); }
template <class T, class U> T max(vector<T> &a, vector<U> &b) {
return max(*max_element(all(a)), *max_element(all(b)));
}
template <class T> T maxi(vector<T> &a) {
return max_element(all(a)) - a.begin();
}
template <class T> T max(vector<T> &a, ll n) {
return *max_element(a.begin(), a.begin() + min(n, sz(a)));
}
template <class T> T max(vector<T> &a, ll s, ll n) {
return *max_element(a.begin() + s, a.begin() + min(n, sz(a)));
}
template <typename A, size_t N> A max(A (&a)[N]) {
A res = a[0];
rep(i, N) res = max(res, a[i]);
return res;
}
template <typename A, size_t N, size_t O> A max(A (&a)[N][O]) {
A res = max(a[0]);
rep(i, N) res = max(res, max(a[i]));
return res;
}
template <typename A, size_t N, size_t O, size_t P> A max(A (&a)[N][O][P]) {
A res = max(a[0]);
rep(i, N) res = max(res, max(a[i]));
return res;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q>
A max(A (&a)[N][O][P][Q], const T &v) {
A res = max(a[0]);
rep(i, N) res = max(res, max(a[i]));
return res;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R>
A max(A (&a)[N][O][P][Q][R]) {
A res = max(a[0]);
rep(i, N) res = max(res, max(a[i]));
return res;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R,
size_t S>
A max(A (&a)[N][O][P][Q][R][S]) {
A res = max(a[0]);
rep(i, N) res = max(res, max(a[i]));
return res;
}
template <typename A, size_t N> A min(A (&a)[N]) {
A res = a[0];
rep(i, N) res = min(res, a[i]);
return res;
}
template <typename A, size_t N, size_t O> A min(A (&a)[N][O]) {
A res = min(a[0]);
rep(i, N) res = min(res, min(a[i]));
return res;
}
template <typename A, size_t N, size_t O, size_t P> A min(A (&a)[N][O][P]) {
A res = min(a[0]);
rep(i, N) res = min(res, min(a[i]));
return res;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q>
A min(A (&a)[N][O][P][Q], const T &v) {
A res = min(a[0]);
rep(i, N) res = min(res, min(a[i]));
return res;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R>
A min(A (&a)[N][O][P][Q][R]) {
A res = min(a[0]);
rep(i, N) res = min(res, min(a[i]));
return res;
}
template <typename A, size_t N, size_t O, size_t P, size_t Q, size_t R,
size_t S>
A min(A (&a)[N][O][P][Q][R][S]) {
A res = min(a[0]);
rep(i, N) res = min(res, min(a[i]));
return res;
}
template <class T> T sum(vector<T> &v, ll s, ll t) {
T ret = 0;
rep(i, s, min(sz(v), t)) ret += v[i];
return ret;
}
template <class T> T sum(vector<T> &v, ll t = inf) { return sum(v, 0, t); }
template <class T> T sum(vector<vector<T>> &v) {
T ret = 0;
rep(i, sz(v)) ret += sum(v[i]);
return ret;
}
template <class T> T sum(vector<vector<vector<T>>> &v) {
T ret = 0;
rep(i, sz(v)) ret += sum(v[i]);
return ret;
}
template <class T> T sum(vector<vector<vector<vector<T>>>> &v) {
T ret = 0;
rep(i, sz(v)) ret += sum(v[i]);
return ret;
}
template <class T> T sum(vector<vector<vector<vector<vector<T>>>>> &v) {
T ret = 0;
rep(i, sz(v)) ret += sum(v[i]);
return ret;
}
template <class T> auto sum(priority_queue<T, vector<T>, greater<T>> &r) {
auto q = r;
T ret = 0;
while (sz(q)) {
ret += q.top();
q.pop();
}
return ret;
}
template <class T> auto sum(priority_queue<T> &r) {
auto q = r;
T ret = 0;
while (sz(q)) {
ret += q.top();
q.pop();
}
return ret;
}
// template<class T, class U, class... W> auto sumn(vector<T> &v, U head, W...
// tail) { auto ret = sum(v[0], tail...); rep(i, 1, min(sz(v), head))ret
// += sum(v[i], tail...); return ret;}
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(); }
};
template <class T> struct rruic {
const T *rrui;
rruic(T *ru) : rrui(ru) {}
//[r , l) : [3 , -1)等
T operator()(ll r, ll l) {
assert(r >= l);
return rrui[l] - rrui[r];
}
T operator()(int l) { return rrui[l]; }
T operator[](ll i) { return rrui[i - 1] - rrui[i]; }
};
template <class T> ostream &operator<<(ostream &os, ruiC<T> a) {
fora(v, a.rui) os << v << " ";
return os;
}
template <class T> vector<T> ruiv(vector<T> &a) {
vector<T> ret(a.size() + 1);
rep(i, a.size()) ret[i + 1] = ret[i] + a[i];
return ret;
}
template <class T> ruiC<T> ruic() { return ruiC<T>(); }
template <class T> ruiC<T> ruic(vector<T> &a) {
vector<T> ret = ruiv(a);
return ruiC<T>(ret);
}
vvi() ruib(vi &a) {
vvi(res, 61, sz(a) + 1);
rep(k, 61) {
rep(i, sz(a)) { res[k][i + 1] = res[k][i] + ((a[i] >> k) & 1); }
}
return res;
}
vector<ruiC<int>> ruibc(vi &a) {
vector<ruiC<int>> ret(61);
vvi(res, 61, sz(a));
rep(k, 61) {
rep(i, sz(a)) { res[k][i] = (a[i] >> k) & 1; }
ret[k] = ruic(res[k]);
}
return ret;
}
vector<ll> ruiv(string &a) {
if (sz(a) == 0)
return vi(1);
ll dec = ('0' <= a[0] && a[0] <= '9') ? '0' : 0;
vector<ll> ret(a.size() + 1);
rep(i, a.size()) ret[i + 1] = ret[i] + a[i] - dec;
return ret;
}
ruiC<ll> ruic(string &a) {
vector<ll> ret = ruiv(a);
return ruiC<ll>(ret);
}
// kと同じものの数
template <class T, class U> vi ruiv(T &a, U k) {
vi ret(a.size() + 1);
rep(i, a.size()) ret[i + 1] = ret[i] + (a[i] == k);
return ret;
}
template <class T, class U> ruiC<ll> ruic(T &a, U k) {
vi ret = ruiv(a, k);
return ruiC<ll>(ret);
}
// h query
template <class T> vector<T> imoh(vector<vector<T>> &v, int w) {
vector<T> ret(sz(v));
rep(h, sz(ret)) { ret[h] = v[h][w]; }
rep(i, sz(ret) - 1) { ret[i + 1] += ret[i]; }
return ret;
}
template <class T> vector<T> ruih(vector<vector<T>> &v, int w) {
vector<T> ret(sz(v) + 1);
rep(h, sz(v)) { ret[h + 1] = v[h][w]; }
rep(i, sz(v)) { ret[i + 1] += ret[i]; }
return ret;
}
template <class T> ruiC<T> ruihc(vector<vector<T>> &a, int w) {
vector<T> ret = ruih(a, w);
return ruiC<T>(ret);
}
// xor
template <class T> struct ruixC {
const vector<T> rui;
ruixC(vector<T> &ru) : rui(ru) {}
T operator()(ll l, ll r) {
if (l > r) {
cerr << "ruiXc ";
deb(l, r);
assert(0);
}
return rui[r] ^ rui[l];
}
T operator[](ll i) { return rui[i]; }
T back() { return rui.back(); }
ll size() { return rui.size(); }
};
template <class T> vector<T> ruix(vector<T> &a) {
vector<T> ret(a.size() + 1);
rep(i, a.size()) ret[i + 1] = ret[i] ^ a[i];
return ret;
}
template <class T> ruixC<ll> ruixc(vector<T> &a) {
vi ret = ruix(a);
return ruixC<ll>(ret);
}
template <class T> vector<T> ruim(vector<T> &a) {
vector<T> res(a.size() + 1, 1);
rep(i, a.size()) res[i + 1] = res[i] * a[i];
return res;
}
// 漸化的に最小を1indexで持つ
template <class T> vector<T> ruimi(vector<T> &a) {
ll n = sz(a);
vector<T> ret(n + 1);
rep(i, 1, n) {
ret[i] = a[i - 1];
chmi(ret[i + 1], ret[i]);
}
return ret;
}
// template<class T> T *rrui(vector<T> &a) {
// 右から左にかけての半開区間 (-1 n-1]
template <class T> rruic<T> rrui(vector<T> &a) {
ll len = a.size();
T *body = (T *)malloc((len + 1) * sizeof(T));
T *res = body + 1;
rer(i, len - 1) res[i - 1] = res[i] + a[i];
return rruic<T>(res);
}
// 掛け算
template <class T> T *rruim(vector<T> &a) {
ll len = a.size();
T *body = (T *)malloc((len + 1) * sizeof(T));
T *res = body + 1;
res[len - 1] = 1;
rer(i, len - 1) res[i - 1] = res[i] * a[i];
return res;
}
template <class T, class U> void inc(T &a, U v = 1) { a += v; }
template <class T, class U> void inc(vector<T> &a, U v = 1) {
for (auto &u : a)
inc(u, v);
}
template <class T, class U> void dec(T &a, U v = 1) { a -= v; }
template <class T, class U> void dec(vector<T> &a, U v = 1) {
for (auto &u : a)
dec(u, v);
}
template <class U> void dec(string &a, U v = 1) {
for (auto &u : a)
dec(u, v);
}
template <class T> void dec(vector<T> &a) {
for (auto &u : a)
dec(u, 1);
}
template <class T, class U> void dec(vector<T> &a, vector<U> &b) {
for (auto &u : a)
dec(u, 1);
for (auto &u : b)
dec(u, 1);
}
template <class T, class U, class W>
void dec(vector<T> &a, vector<U> &b, vector<W> &c) {
for (auto &u : a)
dec(u, 1);
for (auto &u : b)
dec(u, 1);
for (auto &u : c)
dec(u, 1);
}
bool ins(ll h, ll w, ll H, ll W) { return h >= 0 && w >= 0 && h < H && w < W; }
bool ins(ll l, ll v, ll r) { return l <= v && v < r; }
template <class T> bool ins(vector<T> &a, ll i, ll j = 0) {
return ins(0, i, sz(a)) && ins(0, j, sz(a));
}
ll u(ll a) { return a < 0 ? 0 : a; }
template <class T> vector<T> u(const vector<T> &a) {
vector<T> ret = a;
fora(v, ret) v = u(v);
return ret;
}
#define MIN(a) numeric_limits<a>::min()
#define MAX(a) numeric_limits<a>::max()
// 添え字を返す
template <class F> ll goldd_l(ll left, ll right, F calc) {
double GRATIO = 1.6180339887498948482045868343656;
ll lm = left + (ll)((right - left) / (GRATIO + 1.0));
ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0));
ll fl = calc(lm);
ll fr = calc(rm);
while (right - left > 10) {
if (fl < fr) {
right = rm;
rm = lm;
fr = fl;
lm = left + (ll)((right - left) / (GRATIO + 1.0));
fl = calc(lm);
} else {
left = lm;
lm = rm;
fl = fr;
rm = lm + (ll)((right - lm) / (GRATIO + 1.0));
fr = calc(rm);
}
}
ll minScore = MAX(ll);
ll resIndex = left;
for (ll i = left; i < right + 1; ++i) {
ll score = calc(i);
if (minScore > score) {
minScore = score;
resIndex = i;
}
}
return resIndex;
}
template <class F> ll goldt_l(ll left, ll right, F calc) {
double GRATIO = 1.6180339887498948482045868343656;
ll lm = left + (ll)((right - left) / (GRATIO + 1.0));
ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0));
ll fl = calc(lm);
ll fr = calc(rm);
while (right - left > 10) {
if (fl > fr) {
right = rm;
rm = lm;
fr = fl;
lm = left + (ll)((right - left) / (GRATIO + 1.0));
fl = calc(lm);
} else {
left = lm;
lm = rm;
fl = fr;
rm = lm + (ll)((right - lm) / (GRATIO + 1.0));
fr = calc(rm);
}
}
if (left > right) {
ll l = left;
left = right;
right = l;
}
ll maxScore = MIN(ll);
ll resIndex = left;
for (ll i = left; i < right + 1; ++i) {
ll score = calc(i);
if (maxScore < score) {
maxScore = score;
resIndex = i;
}
}
return resIndex;
}
/*loopは200にすればおそらく大丈夫 余裕なら300に*/
template <class F> dou goldd_d(dou left, dou right, F calc, ll loop = 200) {
dou GRATIO = 1.6180339887498948482045868343656;
dou lm = left + ((right - left) / (GRATIO + 1.0));
dou rm = lm + ((right - lm) / (GRATIO + 1.0));
dou fl = calc(lm);
dou fr = calc(rm); /*200にすればおそらく大丈夫*/ /*余裕なら300に*/
ll k = 141;
loop++;
while (--loop) {
if (fl < fr) {
right = rm;
rm = lm;
fr = fl;
lm = left + ((right - left) / (GRATIO + 1.0));
fl = calc(lm);
} else {
left = lm;
lm = rm;
fl = fr;
rm = lm + ((right - lm) / (GRATIO + 1.0));
fr = calc(rm);
}
}
return left;
}
template <class F> dou goldt_d(dou left, dou right, F calc, ll loop = 200) {
double GRATIO = 1.6180339887498948482045868343656;
dou lm = left + ((right - left) / (GRATIO + 1.0));
dou rm = lm + ((right - lm) / (GRATIO + 1.0));
dou fl = calc(lm);
dou fr = calc(rm);
loop++;
while (--loop) {
if (fl > fr) {
right = rm;
rm = lm;
fr = fl;
lm = left + ((right - left) / (GRATIO + 1.0));
fl = calc(lm);
} else {
left = lm;
lm = rm;
fl = fr;
rm = lm + ((right - lm) / (GRATIO + 1.0));
fr = calc(rm);
}
}
return left;
}
// l ~ rを複数の区間に分割し、極致を与えるiを返す time-20 msまで探索
template <class F> ll goldd_ls(ll l, ll r, F calc, ll time = 2000) {
auto lim = milliseconds(time - 20);
ll mini = 0, minv = MAX(ll); /*区間をk分割する*/
rep(k, 1, inf) {
auto s = system_clock::now();
ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/
ll nl = l;
ll nr = l + haba;
rep(i, k) {
ll ni = goldd_l(nl, nr, calc);
if (chmi(minv, calc(ni)))
mini = ni;
nl = nr;
nr = nl + haba;
}
auto end = system_clock::now();
auto part = duration_cast<milliseconds>(end - s);
auto elapsed = duration_cast<milliseconds>(end - start_time);
if (elapsed + part * 2 >= lim) {
break;
}
}
return mini;
}
template <class F> ll goldt_ls(ll l, ll r, F calc, ll time = 2000) {
auto lim = milliseconds(time - 20);
ll maxi = 0, maxv = MIN(ll); /*区間をk分割する*/
rep(k, 1, inf) {
auto s = system_clock::now();
ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/
ll nl = l;
ll nr = l + haba;
rep(i, k) {
ll ni = goldt_l(nl, nr, calc);
if (chma(maxv, calc(ni)))
maxi = ni;
nl = nr;
nr = nl + haba;
}
auto end = system_clock::now();
auto part = duration_cast<milliseconds>(end - s);
auto elapsed = duration_cast<milliseconds>(end - start_time);
if (elapsed + part * 2 >= lim) {
break;
}
}
return maxi;
}
template <class F>
dou goldd_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を持つ*/
auto lim = milliseconds(time - 20);
dou mini = 0, minv = MAX(dou); /*区間をk分割する*/
rep(k, 1, inf) {
auto s = system_clock::now();
dou haba = (r - l) / k;
dou nl = l;
dou nr = l + haba;
rep(i, k) {
dou ni = goldd_d(nl, nr, calc);
if (chmi(minv, calc(ni)))
mini = ni;
nl = nr;
nr = nl + haba;
}
auto end = system_clock::now();
auto part = duration_cast<milliseconds>(end - s);
auto elapsed = duration_cast<milliseconds>(end - start_time);
if (elapsed + part * 2 >= lim) {
break;
}
}
return mini;
}
template <class F>
dou goldt_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を残している*/
auto lim = milliseconds(time - 20);
dou maxi = 0, maxv = MIN(dou); /*区間をk分割する*/
rep(k, 1, inf) {
auto s = system_clock::now();
dou haba = (r - l) / k;
dou nl = l;
dou nr = l + haba;
rep(i, k) {
dou ni = goldt_d(nl, nr, calc);
if (chma(maxv, calc(ni)))
maxi = ni;
nl = nr;
nr = nl + haba;
}
auto end = system_clock::now();
auto part = duration_cast<milliseconds>(end - s);
auto elapsed = duration_cast<milliseconds>(end - start_time);
if (elapsed + part * 2 >= lim) {
break;
}
}
return maxi;
}
template <class T> T min(vector<vector<T>> &a) {
T res = MAX(T);
rep(i, a.size()) chmi(res, *min_element(all(a[i])));
return res;
}
template <class T> T max(vector<vector<T>> &a) {
T res = MIN(T);
rep(i, a.size()) chma(res, *max_element(all(a[i])));
return res;
}
constexpr bool bget(ll m, ll keta) {
#ifdef _DEBUG
assert(keta <= 62); // オーバーフロー 1^62までしか扱えない
#endif
return (m >> keta) & 1;
}
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に置きなおす
ll bit(vi &a) {
int m = 0;
for (auto &&v : a)
m |= bit(v);
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 lowerBound(a, v) (*lower_bound(all(a), v))
#define upperIndex(a, v) (upper_bound(all(a), v) - a.begin())
#define upperBound(a, v) (*upper_bound(all(a), v))
#define rlowerIndex(a, v) (upper_bound(all(a), v) - a.begin() - 1)
#define rlowerBound(a, v) *(--(upper_bound(all(a), v)))
#define rupperIndex(a, v) (lower_bound(all(a), v) - a.begin() - 1)
#define rupperBound(a, v) *(--(lower_bound(all(a), v)))
#define next2(a) next(next(a))
#define prev2(a) prev(prev(a))
// 狭義の単調増加列 長さを返す
template <class T> int lis(vector<T> &a) {
int n = sz(a);
vi tail(n + 1, MAX(T));
rep(i, n) {
int id = lowerIndex(tail, a[i]); /**/
tail[id] = a[i];
}
return lowerIndex(tail, MAX(T));
}
template <class T> int lis_eq(vector<T> &a) {
int n = sz(a);
vi tail(n + 1, MAX(T));
rep(i, n) {
int id = upperIndex(tail, a[i]); /**/
tail[id] = a[i];
}
return lowerIndex(tail, MAX(T));
}
// iteratorを返す
// valueが1以上の物を返す 0は見つけ次第削除
// vを減らす場合 (*it).se--でいい
template <class T, class U, class V> auto lower_map(map<T, U> &m, V k) {
auto ret = m.lower_bound(k);
while (ret != m.end() && (*ret).second == 0) {
ret = m.erase(ret);
}
return ret;
}
template <class T, class U, class V> auto upper_map(map<T, U> &m, V k) {
auto ret = m.upper_bound(k);
while (ret != m.end() && (*ret).second == 0) {
ret = m.erase(ret);
}
return ret;
}
// 存在しなければエラー
template <class T, class U, class V> auto rlower_map(map<T, U> &m, V k) {
auto ret = upper_map(m, k);
assert(ret != m.begin());
ret--;
while (1) {
if ((*ret).second != 0)
break;
assert(ret != m.begin());
auto next = ret;
--next;
m.erase(ret);
ret = next;
}
return ret;
}
template <class T, class U, class V> auto rupper_map(map<T, U> &m, V k) {
auto ret = lower_map(m, k);
assert(ret != m.begin());
ret--;
while (1) {
if ((*ret).second != 0)
break;
assert(ret != m.begin());
auto next = ret;
--next;
m.erase(ret);
ret = next;
}
return ret;
}
template <class T> void fin(T s) { cout << s << endl, exit(0); }
// 便利 数学 math
// 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;
}
}
/*@formatter:on*/
ll tosa_i(ll st, ll ad, ll v) {
assert(((v - st) % ad) == 0);
return (v - st) / ad;
}
/*@formatter:off*/
// tood 係数が負の場合
// 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); }
ll sig(ll t) { return ((1 + t) * t) >> 1; }
ll sig(ll s, ll t) { return ((s + t) * (t - s + 1)) >> 1; }
// 機能拡張
template <class T, class U> string to_string(T a, U b) {
string res = "";
res += a;
res += b;
return res;
}
template <class T, class U, class V> string to_string(T a, U b, V c) {
string res = "";
res += a;
res += b;
res += c;
return res;
}
template <class T, class U, class V, class W>
string to_string(T a, U b, V c, W d) {
string res = "";
res += a;
res += b;
res += c;
res += d;
return res;
}
template <class T, class U, class V, class W, class X>
string to_string(T a, U b, V c, W d, X e) {
string res = "";
res += a;
res += b;
res += c;
res += d;
res += e;
return res;
}
constexpr int bsetlen = k5 * 2;
// constexpr int bsetlen = 5050;
#define bset bitset<bsetlen>
bool operator<(bitset<bsetlen> &a, bitset<bsetlen> &b) {
rer(i, bsetlen - 1) {
if (a[i] < b[i])
return true;
if (a[i] > b[i])
return false;
}
return false;
}
bool operator>(bitset<bsetlen> &a, bitset<bsetlen> &b) {
rer(i, bsetlen - 1) {
if (a[i] > b[i])
return true;
if (a[i] < b[i])
return false;
}
return false;
}
bool operator<=(bitset<bsetlen> &a, bitset<bsetlen> &b) {
rer(i, bsetlen - 1) {
if (a[i] < b[i])
return true;
if (a[i] > b[i])
return false;
}
return true;
}
bool operator>=(bitset<bsetlen> &a, bitset<bsetlen> &b) {
rer(i, bsetlen - 1) {
if (a[i] > b[i])
return true;
if (a[i] < b[i])
return false;
}
return true;
}
string operator~(string &a) {
string res = a;
for (auto &&c : res) {
if (c == '0')
c = '1';
else if (c == '1')
c = '0';
else {
cerr << "cant ~" << a << "must bit" << endl;
exit(0);
}
}
return res;
}
ostream &operator<<(ostream &os, bset &a) {
bitset<10> b;
vi list;
rep(i, bsetlen) {
if (a[i])
list.push_back(i), b[i] = 1;
}
os << b << ", " << list;
return os;
}
int hbiti(bset &a) {
rer(i, bsetlen) {
if (a[i])
return i;
}
return -1;
}
#define hk(a, b, c) (a <= b && b < c)
// O(N/64)
bset nap(bset &a, int v) {
bset r = a | a << v;
return r;
}
bset nap(bset &a, bset &v) {
bset r = a;
rep(i, bsetlen) {
if (v[i])
r |= a << i;
}
return r;
}
template <class T> void seth(vector<vector<T>> &S, int w, vector<T> &v) {
assert(sz(S) == sz(v));
assert(w < sz(S[0]));
rep(h, sz(S)) { S[h][w] = v[h]; }
}
template <class T, class U> void operator+=(pair<T, U> &a, pair<T, U> &b) {
a.fi += b.fi;
a.se += b.se;
}
template <class T, class U> pair<T, U> operator+(pair<T, U> &a, pair<T, U> &b) {
return pair<T, U>(a.fi + b.fi, a.se + b.se);
}
template <typename CharT, typename Traits, typename Alloc>
basic_string<CharT, Traits, Alloc>
operator+(const basic_string<CharT, Traits, Alloc> &lhs, const int rv) {
basic_string<CharT, Traits, Alloc> str(lhs);
str.append(to_string(rv));
return str;
}
template <typename CharT, typename Traits, typename Alloc>
void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const int rv) {
lhs += to_string(rv);
}
template <typename CharT, typename Traits, typename Alloc>
basic_string<CharT, Traits, Alloc>
operator+(const basic_string<CharT, Traits, Alloc> &lhs, const signed rv) {
basic_string<CharT, Traits, Alloc> str(lhs);
str.append(to_string(rv));
return str;
}
template <typename CharT, typename Traits, typename Alloc>
void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const signed rv) {
lhs += to_string(rv);
}
template <typename CharT, typename Traits, typename Alloc>
void operator*=(basic_string<CharT, Traits, Alloc> &s, int num) {
auto bek = s;
s = "";
for (; num; num >>= 1) {
if (num & 1) {
s += bek;
}
bek += bek;
}
}
template <class T, class U> void operator+=(queue<T> &a, U v) { a.push(v); }
template <class T, class U> void operator+=(deque<T> &a, U v) {
a.push_back(v);
}
template <class T>
priority_queue<T, vector<T>, greater<T>> &
operator+=(priority_queue<T, vector<T>, greater<T>> &a, vector<T> &v) {
fora(d, v) a.push(d);
return a;
}
template <class T, class U>
priority_queue<T, vector<T>, greater<T>> &
operator+=(priority_queue<T, vector<T>, greater<T>> &a, U v) {
a.push(v);
return a;
}
template <class T, class U>
priority_queue<T> &operator+=(priority_queue<T> &a, U v) {
a.push(v);
return a;
}
template <class T> set<T> &operator+=(set<T> &a, vector<T> v) {
fora(d, v) a.insert(d);
return a;
}
template <class T, class U> auto operator+=(set<T> &a, U v) {
return a.insert(v);
}
template <class T, class U> auto operator-=(set<T> &a, U v) {
return a.erase(v);
}
template <class T, class U> auto operator+=(mset<T> &a, U v) {
return a.insert(v);
}
template <class T, class U>
set<T, greater<T>> &operator+=(set<T, greater<T>> &a, U v) {
a.insert(v);
return a;
}
template <class T, class U> vector<T> &operator+=(vector<T> &a, U v) {
a.push_back(v);
return a;
}
template <class T, class U> vector<T> operator+(const vector<T> &a, U v) {
vector<T> ret = a;
ret += v;
return ret;
}
template <class T, class U> vector<T> operator+(U v, const vector<T> &a) {
vector<T> ret = a;
ret.insert(ret.begin(), v);
return ret;
}
template <class T> vector<T> operator+(vector<T> a, vector<T> b) {
vector<T> ret;
ret = a;
fora(v, b) ret += v;
return ret;
}
template <class T> vector<T> &operator+=(vector<T> &a, vector<T> &b) {
rep(i, sz(b)) { /*こうしないとa+=aで両辺が増え続けてバグる*/
a.push_back(b[i]);
}
return a;
}
template <class T, class U> map<T, U> &operator+=(map<T, U> &a, map<T, U> &b) {
fora(bv, b) { a[bv.first] += bv.second; }
return a;
}
template <class T> vector<T> &operator-=(vector<T> &a, vector<T> &b) {
if (sz(a) != sz(b)) {
debugline("vector<T> operator-=");
deb(a);
deb(b);
exit(0);
}
rep(i, sz(a)) a[i] -= b[i];
return a;
}
template <class T> vector<T> operator-(vector<T> &a, vector<T> &b) {
if (sz(a) != sz(b)) {
debugline("vector<T> operator-");
deb(a);
deb(b);
ole();
}
vector<T> res(sz(a));
rep(i, sz(a)) res[i] = a[i] - b[i];
return res;
}
template <class T, class U> void operator*=(vector<T> &a, U b) {
vector<T> ta = a;
rep(b - 1) { a += ta; }
}
template <typename T> void erase(vector<T> &v, unsigned ll i) {
v.erase(v.begin() + i);
}
template <typename T> void erase(vector<T> &v, unsigned ll s, unsigned ll e) {
v.erase(v.begin() + s, v.begin() + e);
}
template <class T, class U> void erase(map<T, U> &m, ll okl, ll ngr) {
m.erase(m.lower_bound(okl), m.lower_bound(ngr));
}
template <class T> void erase(set<T> &m, ll okl, ll ngr) {
m.erase(m.lower_bound(okl), m.lower_bound(ngr));
}
template <typename T> void erasen(vector<T> &v, unsigned ll s, unsigned ll n) {
v.erase(v.begin() + s, v.begin() + s + n);
}
template <typename T, typename U>
void insert(vector<T> &v, unsigned ll i, U t) {
v.insert(v.begin() + i, t);
}
template <typename T, typename U> void push_front(vector<T> &v, U t) {
v.insert(v.begin(), t);
}
template <typename T, typename U>
void insert(vector<T> &v, unsigned ll i, vector<T> list) {
for (auto &&va : list)
v.insert(v.begin() + i++, va);
}
template <typename T> void insert(set<T> &v, vector<T> list) {
for (auto &&va : list)
v.insert(va);
}
vector<string> split(const string a, const char deli) {
string b = a + deli;
ll l = 0, r = 0, n = b.size();
vector<string> res;
rep(i, n) {
if (b[i] == deli) {
r = i;
if (l < r)
res.push_back(b.substr(l, r - l));
l = i + 1;
}
}
return res;
}
vector<string> split(const string a, const string deli) {
vector<string> res;
ll kn = sz(deli);
std::string::size_type Pos(a.find(deli));
ll l = 0;
while (Pos != std::string::npos) {
if (Pos - l)
res.push_back(a.substr(l, Pos - l));
l = Pos + kn;
Pos = a.find(deli, Pos + kn);
}
if (sz(a) - l)
res.push_back(a.substr(l, sz(a) - l));
return res;
}
void yn(bool a) {
if (a)
cout << "yes" << endl;
else
cout << "no" << endl;
}
void Yn(bool a) {
if (a)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
void YN(bool a) {
if (a)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
void fyn(bool a) {
if (a)
cout << "yes" << endl;
else
cout << "no" << endl;
exit(0);
}
void fYn(bool a) {
if (a)
cout << "Yes" << endl;
else
cout << "No" << endl;
exit(0);
}
void fYN(bool a) {
if (a)
cout << "YES" << endl;
else
cout << "NO" << endl;
exit(0);
}
void 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)};
}
// 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;
}
//@起動時
struct initon {
initon() {
cin.tie(0);
ios::sync_with_stdio(false);
cout.setf(ios::fixed);
cout.precision(16);
srand((unsigned)clock() + (unsigned)time(NULL));
};
} initonv; //@formatter:on
#define pre prev
#define nex next
// gra mll pr
// 上下左右
const string udlr = "udlr";
string UDLR = "UDLR"; // x4と連動 UDLR.find('U') := x4[0]
// 右、上が正
constexpr ll y4[] = {1, -1, 0, 0};
constexpr ll x4[] = {0, 0, -1, 1};
constexpr ll y8[] = {0, 1, 0, -1, -1, 1, 1, -1};
constexpr ll x8[] = {1, 0, -1, 0, 1, -1, 1, -1};
// vectorで取れる要素数
// bool=> 1e9 * 8.32
// int => 1e8 * 2.6
// ll => 1e8 * 1.3
vvc(ba);
ll N, M, H, W;
vi A, B, C;
/*@formatter:off*/
#define forg(gi, ve) \
for (ll gi = 0, forglim = ve.size(), f, t, c; \
gi < forglim && (f = ve[gi].f, t = ve[gi].t, c = ve[gi].c, true); ++gi)
#define fort(gi, ve) \
for (ll gi = 0, f, t, c, 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);
}
};
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 N = g.n;
vi dis(N, linf);
dis[s] = 0;
deque<int> q;
q.push_back(s);
vb was(N);
while (!q.empty()) {
int f = q.front();
q.pop_front();
if (was[f])
continue;
was[f] = true;
fora(e, g[f]) {
if (dis[e.t] > dis[f] + e.c) {
if (e.c) {
dis[e.t] = dis[f] + 1;
q.push_back(e.t);
} else {
dis[e.t] = dis[f];
q.push_front(e.t);
}
}
}
}
return dis;
}
// dijkstra_cou<mint> : 数える型で書く return vp(dis,cou)
template <class COU, class T = int>
auto dijkstra_cou(const graph<T> &g, int s, int init_value) {
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(const 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(const vector<pair<signed, signed>> &array, OpFunc opfunc) {
signed n = array.size();
op = opfunc;
lg.assign(n + 1, 0);
for (signed i = 1; i <= n; i++) {
lg[i] = 31 - __builtin_clz(i);
}
table.assign(lg[n] + 1, array);
for (signed i = 1; i <= lg[n]; i++) {
for (signed j = 0; j < n; j++) {
if (j + (1 << (i - 1)) < n) {
table[i][j] = op(table[i - 1][j], table[i - 1][j + (1 << (i - 1))]);
} else {
table[i][j] = table[i - 1][j];
}
}
}
}
pair<signed, signed> query(signed l, signed r) {
assert(l < r);
return op(table[lg[r - l]][l], table[lg[r - l]][r - (1 << lg[r - l])]);
}
};
struct PMORMQ {
vector<signed> a;
SparseTable<MinOp<pair<signed, signed>>> sparse_table;
vector<vector<vector<signed>>> lookup_table;
vector<signed> block_type;
signed block_size, n_block;
void init(const vector<signed> &array) {
a = array;
signed n = a.size();
block_size = std::max(1, (31 - __builtin_clz(n)) / 2);
while (n % block_size != 0) {
a.push_back(a.back() + 1);
n++;
}
n_block = n / block_size;
vector<pair<signed, signed>> b(n_block, make_pair(INT_MAX, INT_MAX));
for (signed i = 0; i < n; i++) {
b[i / block_size] = min(b[i / block_size], make_pair(a[i], i));
}
sparse_table.init(b, MinOp<pair<signed, signed>>());
block_type.assign(n_block, 0);
for (signed i = 0; i < n_block; i++) {
signed cur = 0;
for (signed j = 0; j < block_size - 1; j++) {
signed ind = i * block_size + j;
if (a[ind] < a[ind + 1]) {
cur |= 1 << j;
}
}
block_type[i] = cur;
}
lookup_table.assign(
1 << (block_size - 1),
vector<vector<signed>>(block_size, vector<signed>(block_size + 1)));
for (signed i = 0; i < (1 << (block_size - 1)); i++) {
for (signed j = 0; j < block_size; j++) {
signed res = 0;
signed cur = 0;
signed pos = j;
for (signed k = j + 1; k <= block_size; k++) {
lookup_table[i][j][k] = pos;
if (i & (1 << (k - 1))) {
cur++;
} else {
cur--;
}
if (res > cur) {
pos = k;
res = cur;
}
}
}
}
}
signed query(signed l, signed r) {
assert(l < r);
signed lb = l / block_size;
signed rb = r / block_size;
if (lb == rb) {
return lb * block_size +
lookup_table[block_type[lb]][l % block_size][r % block_size];
}
signed pl = lb * block_size +
lookup_table[block_type[lb]][l % block_size][block_size];
signed pr =
rb * block_size + lookup_table[block_type[rb]][0][r % block_size];
signed pos = pl;
if (r % block_size > 0 && a[pl] > a[pr]) {
pos = pr;
}
if (lb + 1 == rb) {
return pos;
}
signed spv = sparse_table.query(lb + 1, rb).second;
if (a[pos] > a[spv]) {
return spv;
}
return pos;
}
};
template <class T = int> class tree : public undigraph<T> {
PMORMQ rmq;
int cnt;
vector<signed> id, in;
bool never = true;
bool never_hld = true;
void dfs(int x, int p, int d, int dis = 0) {
id[cnt] = x;
par_[x] = p;
rmq_dep.push_back(d);
disv[x] = dis;
in[x] = cnt++;
forg(gi, g[x]) {
if (t == p) {
continue;
}
dfs(t, x, d + 1, dis + c);
id[cnt] = x;
rmq_dep.push_back(d);
cnt++;
}
}
void precalc() {
never = false;
cnt = 0;
rmq_dep.clear();
disv.assign(n, 0);
in.assign(n, -1);
id.assign(2 * n - 1, -1);
par_.assign(n, -1);
dfs(root, -1, 0);
rmq.init(rmq_dep);
#ifdef _DEBUG
if (n >= 100)
return;
cerr << "---tree---" << endl;
rep(i, n) {
if (!(i == root || sz(g[i]) > 1))
continue;
cerr << i << " -> ";
vi ts;
forg(gi, g[i]) {
if (t != par_[i])
ts.push_back(t);
}
rep(i, sz(ts) - 1) cerr << ts[i] << ", ";
if (sz(ts))
cerr << ts.back() << endl;
}
cerr << endl;
#endif
}
int pos;
void hld_build() {
never_hld = false;
if (never)
precalc();
g.resize(n);
vid.resize(n, -1);
head.resize(n);
heavy.resize(n, -1);
depth.resize(n);
inv.resize(n);
subl.resize(n);
subr.resize(n);
dfs(root, -1);
t = 0;
dfs_hld(root);
#ifdef _DEBUG
if (n >= 100)
return;
cerr << "---hld_index---" << endl;
vi inds;
rep(i, n) if (sz(g[i])) inds.push_back(i);
rep(i, sz(inds)) {
str s = tos(bel(inds[i]));
cerr << std::right << std::setw(sz(s) + (i ? 1 : 0)) << inds[i];
}
cerr << endl;
rep(i, sz(inds)) { cerr << bel(inds[i]) << " "; }
cerr << endl << endl;
cerr << "---hld_edge_index---" << endl;
fora(e, edges) {
if (e.f <= e.t)
cerr << e.f << "-" << e.t << " " << bel(e) << endl;
}
cerr << endl << endl;
cerr << "!!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;
public:
using undigraph<T>::g;
using undigraph<T>::n;
using undigraph<T>::edges;
int root;
// 部分木の [左端、右端) 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_; }
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];
}
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 par(int a){if (never)precalc(); return par[a];}
/*O(N) hldを使わず木を普通にたどる*/
void for_each_l(int u, int v, function<void(int)> fnode) {
int r = lca(u, v);
while (u != r) {
fnode(u);
u = par_[u];
}
while (v != r) {
fnode(v);
v = par_[v];
}
fnode(r);
}
void for_each_edge_l /*O(N) 頂点に対しての処理順が可換*/ (
int u, int v, function<void(edge<int> &)> fedge) {
int r = lca(u, v);
auto sub = [&](int u, int r) {
while (u != r) {
forg(gi, g[u]) {
if (t == par_[u]) {
fedge(g[u][gi]);
u = par_[u];
break;
}
}
}
};
sub(u, r);
sub(v, r);
}
// Fは半開 (u,v)は木の頂点
// 中ではhldの頂点を見るため、seg木のupdateはhldのindexで行なう
void for_each_ /*[l,r)*/ (int u, int v, const function<void(int, int)> &f) {
if (never_hld)
hld_build();
while (1) {
if (vid[u] > vid[v])
swap(u, v);
int l = max(vid[head[v]], vid[u]);
int r = vid[v] + 1;
f(l, r);
if (head[u] != head[v])
v = par_[head[v]];
else
break;
}
}
void for_each_edge /*[l,r) O(log(N)) 辺を頂点として扱っている
上と同じ感じで使える*/
(int u, int v, const function<void(int, int)> &f) {
if (never_hld)
hld_build();
while (1) {
if (vid[u] > vid[v])
swap(u, v);
if (head[u] != head[v]) {
int l = vid[head[v]];
int r = vid[v] + 1;
f(l, r);
v = par_[head[v]];
} else {
if (u != v) {
int l = vid[u] + 1;
int r = vid[v] + 1;
f(l, r);
}
break;
}
}
}
int bel(int v) { /*hld内での頂点番号を返す*/
if (never_hld)
hld_build();
return vid[v];
}
// 下の頂点に辺のクエリを持たせている
int bel(
int f,
int t) { /*辺のクエリを扱うときどの頂点に持たせればいいか(vidを返すのでそのままupd出来る)*/
if (never_hld)
hld_build();
return depth[f] > depth[t] ? vid[f] : vid[t];
}
int bel(
edge<T> &
e) { /*辺のクエリを扱うときどの頂点に持たせればいいか(vidを返すのでそのままupd出来る)*/
if (never_hld)
hld_build();
return depth[e.f] > depth[e.t] ? vid[e.f] : vid[e.t];
}
template <class... U> int operator()(U... args) { return bel(args...); }
// path l -> r += v
template <class S> void seg_add(S &seg, int lhei, int rhei, int v) {
for_each_(lhei, rhei, [&](int l, int r) { seg.add(l, r, v); });
}
template <class S> void seg_update(S &seg, int lhei, int rhei, int v) {
for_each_(lhei, rhei, [&](int l, int r) { seg.update(l, r, v); });
}
template <class S> 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) {
cout << "grid" << endl;
if (!(0 <= f && f < n && 0 <= t && t < n)) {
debugline("grid_k6 add");
deb(f, t, c);
ole();
}
g[f].emplace_back(f, t, c);
g[t].emplace_back(t, f, c);
// undigraphと違い、edgesを持たない
}
int getid(int h, int w) {
assert(ins(h, w, H, W));
return W * h + w;
}
int getid(P p) { return getid(p.first, p.second); }
P get2(int id) { return mp(id / W, id % W); }
P operator()(int id) { return get2(id); }
int operator()(int h, int w) { return getid(h, w); }
int operator()(P p) { return getid(p); }
// 辺は無い
grid_k6(int H, int W) : H(H), W(W), undigraph<T>(H * W) {}
grid_k6(vector<vector<char>> ba, char wall = '#')
: H(sz(ba)), W(sz(ba[0])), undigraph<T>(sz(ba) * sz(ba[0])) {
rep(h, H) {
rep(w, W) {
if (ba[h][w] == wall)
con;
int f = getid(h, w);
if (w + 1 < W && ba[h][w + 1] != wall) {
add(f, getid(h, w + 1));
}
if (h + 1 < H && ba[h + 1][w] != wall) {
add(f, getid(h + 1, w));
}
}
}
}
/*o -> o, o -> x, x-> x*/
grid_k6(vector<vector<char>> ba, int oo, int ox, int xo, int xx,
char wall = '#')
: H(sz(ba)), W(sz(ba[0])), undigraph<T>(sz(ba) * sz(ba[0])),
costs({oo, ox, xo, xx}), ba(ba), wall(wall) {
rep(h, H) {
rep(w, W) {
add(h, w, h + 1, w);
add(h, w, h - 1, w);
add(h, w, h, w + 1);
add(h, w, h, w - 1);
}
}
}
void add(int fh, int fw, int th, int tw) {
if (ins(fh, fw, H, W) && ins(th, tw, H, W)) {
int cm = 0;
if (ba[fh][fw] == wall) {
cm += 2;
}
if (ba[th][tw] == wall) {
cm++;
}
int f = getid(fh, fw);
int t = getid(th, tw);
g[f].emplace_back(f, t, costs[cm]);
}
}
void set_edges() {
rep(i, n) fora(e, g[i]) if (e.f < e.t) edges.push_back(e);
}
};
// 辺によりメモリを大量消費ためedgesを消している
// 頂点10^6でメモリを190MB(制限の8割)使う
// 左上から右下に移動できる
template <class T = int> class digrid_k6 : public digraph<T> {
public:
using digraph<T>::g;
using digraph<T>::n;
using digraph<T>::edges;
int H, W;
void add(int f, int t, T c = 1) {
if (!(0 <= f && f < n && 0 <= t && t < n)) {
debugline("digrid_k6 add");
deb(f, t, c);
ole();
}
g[f].emplace_back(f, t, c);
/*digraphと違いedgesを持たない*/
}
int getid(int h, int w) {
if (!ins(h, w, H, W))
return -1;
return W * h + w;
}
P get2(int id) { return mp(id / W, id % W); }
P operator()(int id) { return get2(id); }
int operator()(int h, int w) { return getid(h, w); }
digrid_k6(int H, int W) : H(H), W(W), digraph<T>(H * W) {}
digrid_k6(vector<vector<char>> ba, char wall = '#')
: H(sz(ba)), W(sz(ba[0])), digraph<T>(sz(ba) * sz(ba[0])) {
rep(h, H) {
rep(w, W) {
if (ba[h][w] == wall)
con;
int f = getid(h, w);
if (w + 1 < W && ba[h][w + 1] != wall) {
add(f, getid(h, w + 1));
}
if (h + 1 < H && ba[h + 1][w] != wall) {
add(f, getid(h + 1, w));
}
}
}
}
void add(int fh, int fw, int th, int tw) {
add(getid(fh, fw), getid(th, tw));
}
void set_edges() { rep(i, n) fora(e, g[i]) edges.push_back(e); }
};
// 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++;
}
};
//@出力
template <class T> ostream &operator<<(ostream &os, digraph<T> &g) {
os << endl << g.n << " " << sz(g.edges) << endl;
fore(gi, g.edges) { os << f << " " << t << " " << c << endl; }
return os;
}
template <class T> ostream &operator<<(ostream &os, undigraph<T> &g) {
os << endl << g.n << " " << sz(g.edges) << endl;
fore(gi, g.edges) {
if (f < t)
os << f << " " << t << " " << c << endl;
}
return os;
}
//@判定
template <class T> bool nibu(const 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 1;
}
// 二部グラフを色分けした際の頂点数を返す
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) { \
if (zhouken && f < t) { \
g2.add(f, t, c); \
} \
}
// 閉路がなければ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
P farthest(undigraph<> &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(undigraph<> &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];
}
int diameter(undigraph<> &E) {
vi d = diameters(E);
return max(d);
}
// i d
vp diameter_p(undigraph<> &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(D[0][i], v1.second);
else
res[i] = mp(D[1][i], v2.second);
}
return res;
}
//@列挙 取得
// 閉路がある時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;
}
//@実験
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*/
// todo rootを変えてもちゃんと動くか確認
// これらの具体的な説明、およびhldとその他で分ける
// vi par;
// vi vid, head, heavy, depth, inv, subl, subr;
void solve() {
din(N, b, r);
--b, --r;
tree<> g(2 * k5, r);
;
g.ing(N, N - 1);
int dis = g.dis(r, b);
int up = (dis - 1) / 2;
while (up--) {
b = g.par(b);
}
int far = 0;
auto chi = g.child_ex(b);
rep(i, N) {
if (chi[i])
chma(far, g.dis(b, i));
}
int res = (dis - 1) / 2 + far;
if ((dis % 2) == 0)
res++;
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;
}
#endif
return 0;
};
| replace | 5,032 | 5,033 | 5,032 | 5,033 | -11 | |
p02834 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <vector>
int parent[18][100000];
int depth[100000];
std::vector<int> edge[100000];
void dfs(int v, int p, int d) {
parent[0][v] = p;
depth[v] = d;
for (auto to : edge[v]) {
if (to != p)
dfs(to, v, d + 1);
}
}
int lca(int u, int v) {
if (depth[u] > depth[v])
std::swap(u, v);
for (int k = 0; k < 18; ++k) {
if ((depth[v] - depth[u]) >> k & 1)
v = parent[k][v];
}
if (u == v)
return u;
for (int k = 17; k >= 0; --k) {
if (parent[k][u] != parent[k][v]) {
u = parent[k][u];
v = parent[k][v];
}
}
return parent[0][u];
}
int main() {
int n, u, v;
scanf("%d%d%d", &n, &u, &v);
--u, --v;
for (int i = 0; i < n - 1; ++i) {
int a, b;
scanf("%d%d", &a, &b);
--a, --b;
edge[a].emplace_back(b);
edge[b].emplace_back(a);
}
dfs(v, -1, 0);
for (int k = 0; k < 18; ++k) {
for (int j = 0; j < n; ++j) {
if (parent[k][j] < 0)
parent[k + 1][j] = -1;
else
parent[k + 1][j] = parent[k][parent[k][j]];
}
}
std::vector<int> leaf;
std::vector<int> lcav;
for (int i = 0; i < n; ++i) {
if (edge[i].size() == 1) {
int ansc = lca(i, u);
if (ansc == v)
continue;
int tak_to_lca = depth[u] - depth[ansc];
int aok_to_lca = depth[ansc];
if (aok_to_lca <= tak_to_lca)
continue;
leaf.emplace_back(i);
lcav.emplace_back(ansc);
}
}
int ans = 0;
for (int i = 0; i < leaf.size(); ++i) {
int r = depth[u] - depth[lcav[i]];
r += depth[leaf[i]] - depth[lcav[i]];
r += (depth[leaf[i]] - r) - 1;
if (r > ans)
ans = r;
}
printf("%d\n", ans);
return 0;
} | #include <algorithm>
#include <cstdio>
#include <vector>
int parent[20][100000];
int depth[100000];
std::vector<int> edge[100000];
void dfs(int v, int p, int d) {
parent[0][v] = p;
depth[v] = d;
for (auto to : edge[v]) {
if (to != p)
dfs(to, v, d + 1);
}
}
int lca(int u, int v) {
if (depth[u] > depth[v])
std::swap(u, v);
for (int k = 0; k < 18; ++k) {
if ((depth[v] - depth[u]) >> k & 1)
v = parent[k][v];
}
if (u == v)
return u;
for (int k = 17; k >= 0; --k) {
if (parent[k][u] != parent[k][v]) {
u = parent[k][u];
v = parent[k][v];
}
}
return parent[0][u];
}
int main() {
int n, u, v;
scanf("%d%d%d", &n, &u, &v);
--u, --v;
for (int i = 0; i < n - 1; ++i) {
int a, b;
scanf("%d%d", &a, &b);
--a, --b;
edge[a].emplace_back(b);
edge[b].emplace_back(a);
}
dfs(v, -1, 0);
for (int k = 0; k < 18; ++k) {
for (int j = 0; j < n; ++j) {
if (parent[k][j] < 0)
parent[k + 1][j] = -1;
else
parent[k + 1][j] = parent[k][parent[k][j]];
}
}
std::vector<int> leaf;
std::vector<int> lcav;
for (int i = 0; i < n; ++i) {
if (edge[i].size() == 1) {
int ansc = lca(i, u);
if (ansc == v)
continue;
int tak_to_lca = depth[u] - depth[ansc];
int aok_to_lca = depth[ansc];
if (aok_to_lca <= tak_to_lca)
continue;
leaf.emplace_back(i);
lcav.emplace_back(ansc);
}
}
int ans = 0;
for (int i = 0; i < leaf.size(); ++i) {
int r = depth[u] - depth[lcav[i]];
r += depth[leaf[i]] - depth[lcav[i]];
r += (depth[leaf[i]] - r) - 1;
if (r > ans)
ans = r;
}
printf("%d\n", ans);
return 0;
}
| replace | 4 | 5 | 4 | 5 | 0 | |
p02834 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, a, b) for (int i = a; i < (int)b; ++i)
#define rrep(i, n) for (int i = ((int)n - 1); i >= 0; --i)
typedef long long ll;
typedef long double ld;
const int Inf = 1e9;
const double EPS = 1e-9;
const int MOD = 1e9 + 7;
int n;
vector<int> g[10005];
vector<int> dist;
void dfs(int s, int p, int d) {
dist[s] = d;
for (auto u : g[s]) {
if (u == p)
continue;
dfs(u, s, d + 1);
}
}
vector<int> calc(int s) {
dist = vector<int>(n);
dfs(s, -1, 0);
return dist;
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(0);
int u, v;
cin >> n >> u >> v;
--u, --v;
rep(i, n - 1) {
int a, b;
cin >> a >> b;
a--, b--;
g[a].push_back(b);
g[b].push_back(a);
}
vector<int> distT, distA;
distT = calc(u);
distA = calc(v);
int res = 0;
rep(i, n) {
if (distT[i] < distA[i])
res = max(res, distA[i] - 1);
}
cout << res << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, a, b) for (int i = a; i < (int)b; ++i)
#define rrep(i, n) for (int i = ((int)n - 1); i >= 0; --i)
typedef long long ll;
typedef long double ld;
const int Inf = 1e9;
const double EPS = 1e-9;
const int MOD = 1e9 + 7;
int n;
vector<int> g[100005];
vector<int> dist;
void dfs(int s, int p, int d) {
dist[s] = d;
for (auto u : g[s]) {
if (u == p)
continue;
dfs(u, s, d + 1);
}
}
vector<int> calc(int s) {
dist = vector<int>(n);
dfs(s, -1, 0);
return dist;
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(0);
int u, v;
cin >> n >> u >> v;
--u, --v;
rep(i, n - 1) {
int a, b;
cin >> a >> b;
a--, b--;
g[a].push_back(b);
g[b].push_back(a);
}
vector<int> distT, distA;
distT = calc(u);
distA = calc(v);
int res = 0;
rep(i, n) {
if (distT[i] < distA[i])
res = max(res, distA[i] - 1);
}
cout << res << endl;
return 0;
}
| replace | 17 | 18 | 17 | 18 | 0 | |
p02834 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using uint = unsigned int;
using pcc = pair<char, char>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;
using tuplis = array<ll, 3>;
template <class T> using pq = priority_queue<T, vector<T>, greater<T>>;
const ll LINF = 0x1fffffffffffffff;
const ll MINF = 0x7fffffffffff;
const int INF = 0x3fffffff;
const int MOD = 1000000007;
const int MODD = 998244353;
const ld DINF = numeric_limits<ld>::infinity();
const ld EPS = 1e-9;
const ld PI = 3.1415926535897932;
const ll four[] = {0, 1, 0, -1, 0};
const ll eight[] = {0, 1, 1, 0, -1, -1, 1, -1, 0};
#define overload4(_1, _2, _3, _4, name, ...) name
#define overload3(_1, _2, _3, name, ...) name
#define rep1(n) for (ll i = 0; i < n; ++i)
#define rep2(i, n) for (ll i = 0; i < n; ++i)
#define rep3(i, a, b) for (ll i = a; i < b; ++i)
#define rep4(i, a, b, c) for (ll i = a; i < b; i += c)
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep1(n) for (ll i = (n)-1; i >= 0; i--)
#define rrep2(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rrep3(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define rrep4(i, a, b, c) for (ll i = a + (b - a - 1) / c * c; i >= a; i -= c)
#define rrep(...) \
overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__)
#define each(i, a) for (auto &&i : a)
#define all1(i) begin(i), end(i)
#define all2(i, a) begin(i), begin(i) + a
#define all3(i, a, b) begin(i) + a, begin(i) + b
#define all(...) overload3(__VA_ARGS__, all3, all2, all1)(__VA_ARGS__)
#define rall1(i) (i).rbegin(), (i).rend()
#define rall2(i, k) (i).rbegin(), (i).rbegin() + k
#define rall3(i, a, b) (i).rbegin() + a, (i).rbegin() + b
#define rall(...) overload3(__VA_ARGS__, rall3, rall2, rall1)(__VA_ARGS__)
#define sum(...) accumulate(all(__VA_ARGS__), 0LL)
#define dsum(...) accumulate(all(__VA_ARGS__), 0.0L)
#define elif else if
#define unless(a) if (!(a))
#define mp make_pair
#define mt make_tuple
#define INT(...) \
int __VA_ARGS__; \
in(__VA_ARGS__)
#define LL(...) \
ll __VA_ARGS__; \
in(__VA_ARGS__)
#define ULL(...) \
ull __VA_ARGS__; \
in(__VA_ARGS__)
#define STR(...) \
string __VA_ARGS__; \
in(__VA_ARGS__)
#define CHR(...) \
char __VA_ARGS__; \
in(__VA_ARGS__)
#define DBL(...) \
double __VA_ARGS__; \
in(__VA_ARGS__)
#define LD(...) \
ld __VA_ARGS__; \
in(__VA_ARGS__)
#define vec(type, name, ...) vector<type> name(__VA_ARGS__)
#define VEC(type, name, size) \
vector<type> name(size); \
in(name)
#define vv(type, name, h, ...) \
vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define VV(type, name, h, w) \
vector<vector<type>> name(h, vector<type>(w)); \
in(name)
#define vvv(type, name, h, w, ...) \
vector<vector<vector<type>>> name( \
h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
template <class T> auto max(const T &a) { return *max_element(all(a)); }
template <class T> auto min(const T &a) { return *min_element(all(a)); }
ll gcd(ll a, ll b) {
while (b) {
ll c = b;
b = a % b;
a = c;
}
return a;
}
ll lcm(ll a, ll b) {
if (!a || !b)
return 0;
return a * b / gcd(a, b);
}
ll intpow(ll a, ll b) {
ll ans = 1;
while (b) {
if (b & 1)
ans *= a;
a *= a;
b /= 2;
}
return ans;
}
ll modpow(ll a, ll b, ll p) {
ll ans = 1;
while (b) {
if (b & 1)
(ans *= a) %= p;
(a *= a) %= p;
b /= 2;
}
return ans;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
vector<pll> factor(ull x) {
vector<pll> ans;
for (ll i = 2; i * i <= x; i++)
if (x % i == 0) {
ans.push_back({i, 1});
while ((x /= i) % i == 0)
ans.back().second++;
}
if (x != 1)
ans.push_back({x, 1});
return ans;
}
vector<ll> divisor(ull x) {
vector<ll> ans;
for (ll i = 1; i * i <= x; i++)
if (x % i == 0)
ans.push_back(i);
rrep(ans.size() - (ans.back() * ans.back() == x)) ans.push_back(x / ans[i]);
return ans;
}
template <class T> unordered_map<T, ll> press(vector<T> &a) {
auto b = a;
sort(all(b));
b.erase(unique(all(b)), b.end());
unordered_map<T, ll> ans;
rep(b.size()) ans[b[i]] = i;
each(i, a) i = ans[i];
return ans;
}
template <class T> map<T, ll> press_map(vector<T> &a) {
auto b = a;
sort(all(b));
b.erase(unique(all(b)), b.end());
map<T, ll> ans;
rep(b.size()) ans[b[i]] = i;
each(i, a) i = ans[i];
return ans;
}
int scan() { return getchar(); }
void scan(int &a) { scanf("%d", &a); }
void scan(unsigned &a) { scanf("%u", &a); }
void scan(long &a) { scanf("%ld", &a); }
void scan(long long &a) { scanf("%lld", &a); }
void scan(unsigned long long &a) { scanf("%llu", &a); }
void scan(char &a) {
do {
a = getchar();
} while (a == ' ' || a == '\n');
}
void scan(float &a) { scanf("%f", &a); }
void scan(double &a) { scanf("%lf", &a); }
void scan(long double &a) { scanf("%Lf", &a); }
void scan(vector<bool> &a) {
for (unsigned i = 0; i < a.size(); i++) {
int b;
scan(b);
a[i] = b;
}
}
void scan(char a[]) { scanf("%s", a); }
void scan(string &a) { cin >> a; }
template <class T> void scan(vector<T> &);
template <class T, size_t size> void scan(array<T, size> &);
template <class T, class L> void scan(pair<T, L> &);
template <class T, size_t size> void scan(T (&)[size]);
template <class T> void scan(vector<T> &a) {
for (auto &i : a)
scan(i);
}
template <class T> void scan(deque<T> &a) {
for (auto &i : a)
scan(i);
}
template <class T, size_t size> void scan(array<T, size> &a) {
for (auto &i : a)
scan(i);
}
template <class T, class L> void scan(pair<T, L> &p) {
scan(p.first);
scan(p.second);
}
template <class T, size_t size> void scan(T (&a)[size]) {
for (auto &i : a)
scan(i);
}
template <class T> void scan(T &a) { cin >> a; }
void in() {}
template <class Head, class... Tail> void in(Head &head, Tail &...tail) {
scan(head);
in(tail...);
}
void print() { putchar(' '); }
void print(bool a) { printf("%d", a); }
void print(int a) { printf("%d", a); }
void print(unsigned a) { printf("%u", a); }
void print(long a) { printf("%ld", a); }
void print(long long a) { printf("%lld", a); }
void print(unsigned long long a) { printf("%llu", a); }
void print(char a) { printf("%c", a); }
void print(char a[]) { printf("%s", a); }
void print(const char a[]) { printf("%s", a); }
void print(float a) { printf("%.15f", a); }
void print(double a) { printf("%.15f", a); }
void print(long double a) { printf("%.15Lf", a); }
void print(const string &a) {
for (auto &&i : a)
print(i);
}
template <class T> void print(const vector<T> &);
template <class T, size_t size> void print(const array<T, size> &);
template <class T, class L> void print(const pair<T, L> &p);
template <class T, size_t size> void print(const T (&)[size]);
template <class T> void print(const vector<T> &a) {
if (a.empty())
return;
print(a[0]);
for (auto i = a.begin(); ++i != a.end();) {
putchar(' ');
print(*i);
}
}
template <class T> void print(const deque<T> &a) {
if (a.empty())
return;
print(a[0]);
for (auto i = a.begin(); ++i != a.end();) {
putchar(' ');
print(*i);
}
}
template <class T, size_t size> void print(const array<T, size> &a) {
print(a[0]);
for (auto i = a.begin(); ++i != a.end();) {
putchar(' ');
print(*i);
}
}
template <class T, class L> void print(const pair<T, L> &p) {
print(p.first);
putchar(' ');
print(p.second);
}
template <class T, size_t size> void print(const T (&a)[size]) {
print(a[0]);
for (auto i = a; ++i != end(a);) {
putchar(' ');
print(*i);
}
}
template <class T> void print(const T &a) { cout << a; }
int out() {
putchar('\n');
return 0;
}
template <class T> int out(const T &t) {
print(t);
putchar('\n');
return 0;
}
template <class Head, class... Tail>
int out(const Head &head, const Tail &...tail) {
print(head);
putchar(' ');
out(tail...);
return 0;
}
#ifdef DEBUG
void err() { putchar('\n'); }
template <class T> void err(const T &t) {
print(t);
putchar('\n');
}
template <class Head, class... Tail>
void err(const Head &head, const Tail &...tail) {
print(head);
putchar(' ');
out(tail...);
}
#else
template <class... T> void err(const T &...) {}
#endif
int first(bool i = true) { return out(i ? "first" : "second"); }
int yes(bool i = true) { return out(i ? "yes" : "no"); }
int Yes(bool i = true) { return out(i ? "Yes" : "No"); }
int No() { return out("No"); }
int YES(bool i = true) { return out(i ? "YES" : "NO"); }
int NO() { return out("NO"); }
int Yay(bool i = true) { return out(i ? "Yay!" : ":("); }
int possible(bool i = true) { return out(i ? "possible" : "impossible"); }
int Possible(bool i = true) { return out(i ? "Possible" : "Impossible"); }
int POSSIBLE(bool i = true) { return out(i ? "POSSIBLE" : "IMPOSSIBLE"); }
void Case(ll i) { printf("Case #%lld: ", i); }
struct UnionFind {
vector<ll> data;
UnionFind(ll n) : data(n, -1) {}
bool unite(ll a, ll b) {
a = root(a);
b = root(b);
if (a == b)
return 1;
if (data[a] > data[b])
swap(a, b);
data[a] += data[b];
data[b] = a;
return 0;
}
bool find(ll a, ll b) { return root(a) == root(b); }
ll root(ll a) { return data[a] < 0 ? a : data[a] = root(data[a]); }
ll size(ll a) { return -data[root(a)]; }
ll operator[](ll a) { return root(a); }
};
signed main() {
LL(n, u, v);
u--;
v--;
vector<vector<ll>> g(n);
rep(n - 1) {
LL(a, b);
a--;
b--;
g[a].push_back(b);
g[b].push_back(a);
}
vector<ll> qa, qb;
vector<ll> costa(n, LINF), costb(n, LINF);
costa[u] = 0;
costb[v] = 0;
qa.push_back(u);
qb.push_back(v);
while (qa.size() || qb.size()) {
vector<ll> qa2, qb2;
each(at, qb) {
each(i, g[at]) if (chmin(costb[i], costb[at] + 1)) qb2.push_back(i);
}
each(at, qa) {
each(i, g[at]) if (costb[i] == LINF && chmin(costa[i], costa[at] + 1))
qa2.push_back(i);
}
}
ll ans = 0;
rep(n) if (costa[i] != LINF) chmax(ans, costb[i]);
out(ans - 1);
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using uint = unsigned int;
using pcc = pair<char, char>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<double, double>;
using tuplis = array<ll, 3>;
template <class T> using pq = priority_queue<T, vector<T>, greater<T>>;
const ll LINF = 0x1fffffffffffffff;
const ll MINF = 0x7fffffffffff;
const int INF = 0x3fffffff;
const int MOD = 1000000007;
const int MODD = 998244353;
const ld DINF = numeric_limits<ld>::infinity();
const ld EPS = 1e-9;
const ld PI = 3.1415926535897932;
const ll four[] = {0, 1, 0, -1, 0};
const ll eight[] = {0, 1, 1, 0, -1, -1, 1, -1, 0};
#define overload4(_1, _2, _3, _4, name, ...) name
#define overload3(_1, _2, _3, name, ...) name
#define rep1(n) for (ll i = 0; i < n; ++i)
#define rep2(i, n) for (ll i = 0; i < n; ++i)
#define rep3(i, a, b) for (ll i = a; i < b; ++i)
#define rep4(i, a, b, c) for (ll i = a; i < b; i += c)
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep1(n) for (ll i = (n)-1; i >= 0; i--)
#define rrep2(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rrep3(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define rrep4(i, a, b, c) for (ll i = a + (b - a - 1) / c * c; i >= a; i -= c)
#define rrep(...) \
overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__)
#define each(i, a) for (auto &&i : a)
#define all1(i) begin(i), end(i)
#define all2(i, a) begin(i), begin(i) + a
#define all3(i, a, b) begin(i) + a, begin(i) + b
#define all(...) overload3(__VA_ARGS__, all3, all2, all1)(__VA_ARGS__)
#define rall1(i) (i).rbegin(), (i).rend()
#define rall2(i, k) (i).rbegin(), (i).rbegin() + k
#define rall3(i, a, b) (i).rbegin() + a, (i).rbegin() + b
#define rall(...) overload3(__VA_ARGS__, rall3, rall2, rall1)(__VA_ARGS__)
#define sum(...) accumulate(all(__VA_ARGS__), 0LL)
#define dsum(...) accumulate(all(__VA_ARGS__), 0.0L)
#define elif else if
#define unless(a) if (!(a))
#define mp make_pair
#define mt make_tuple
#define INT(...) \
int __VA_ARGS__; \
in(__VA_ARGS__)
#define LL(...) \
ll __VA_ARGS__; \
in(__VA_ARGS__)
#define ULL(...) \
ull __VA_ARGS__; \
in(__VA_ARGS__)
#define STR(...) \
string __VA_ARGS__; \
in(__VA_ARGS__)
#define CHR(...) \
char __VA_ARGS__; \
in(__VA_ARGS__)
#define DBL(...) \
double __VA_ARGS__; \
in(__VA_ARGS__)
#define LD(...) \
ld __VA_ARGS__; \
in(__VA_ARGS__)
#define vec(type, name, ...) vector<type> name(__VA_ARGS__)
#define VEC(type, name, size) \
vector<type> name(size); \
in(name)
#define vv(type, name, h, ...) \
vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define VV(type, name, h, w) \
vector<vector<type>> name(h, vector<type>(w)); \
in(name)
#define vvv(type, name, h, w, ...) \
vector<vector<vector<type>>> name( \
h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
template <class T> auto max(const T &a) { return *max_element(all(a)); }
template <class T> auto min(const T &a) { return *min_element(all(a)); }
ll gcd(ll a, ll b) {
while (b) {
ll c = b;
b = a % b;
a = c;
}
return a;
}
ll lcm(ll a, ll b) {
if (!a || !b)
return 0;
return a * b / gcd(a, b);
}
ll intpow(ll a, ll b) {
ll ans = 1;
while (b) {
if (b & 1)
ans *= a;
a *= a;
b /= 2;
}
return ans;
}
ll modpow(ll a, ll b, ll p) {
ll ans = 1;
while (b) {
if (b & 1)
(ans *= a) %= p;
(a *= a) %= p;
b /= 2;
}
return ans;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
vector<pll> factor(ull x) {
vector<pll> ans;
for (ll i = 2; i * i <= x; i++)
if (x % i == 0) {
ans.push_back({i, 1});
while ((x /= i) % i == 0)
ans.back().second++;
}
if (x != 1)
ans.push_back({x, 1});
return ans;
}
vector<ll> divisor(ull x) {
vector<ll> ans;
for (ll i = 1; i * i <= x; i++)
if (x % i == 0)
ans.push_back(i);
rrep(ans.size() - (ans.back() * ans.back() == x)) ans.push_back(x / ans[i]);
return ans;
}
template <class T> unordered_map<T, ll> press(vector<T> &a) {
auto b = a;
sort(all(b));
b.erase(unique(all(b)), b.end());
unordered_map<T, ll> ans;
rep(b.size()) ans[b[i]] = i;
each(i, a) i = ans[i];
return ans;
}
template <class T> map<T, ll> press_map(vector<T> &a) {
auto b = a;
sort(all(b));
b.erase(unique(all(b)), b.end());
map<T, ll> ans;
rep(b.size()) ans[b[i]] = i;
each(i, a) i = ans[i];
return ans;
}
int scan() { return getchar(); }
void scan(int &a) { scanf("%d", &a); }
void scan(unsigned &a) { scanf("%u", &a); }
void scan(long &a) { scanf("%ld", &a); }
void scan(long long &a) { scanf("%lld", &a); }
void scan(unsigned long long &a) { scanf("%llu", &a); }
void scan(char &a) {
do {
a = getchar();
} while (a == ' ' || a == '\n');
}
void scan(float &a) { scanf("%f", &a); }
void scan(double &a) { scanf("%lf", &a); }
void scan(long double &a) { scanf("%Lf", &a); }
void scan(vector<bool> &a) {
for (unsigned i = 0; i < a.size(); i++) {
int b;
scan(b);
a[i] = b;
}
}
void scan(char a[]) { scanf("%s", a); }
void scan(string &a) { cin >> a; }
template <class T> void scan(vector<T> &);
template <class T, size_t size> void scan(array<T, size> &);
template <class T, class L> void scan(pair<T, L> &);
template <class T, size_t size> void scan(T (&)[size]);
template <class T> void scan(vector<T> &a) {
for (auto &i : a)
scan(i);
}
template <class T> void scan(deque<T> &a) {
for (auto &i : a)
scan(i);
}
template <class T, size_t size> void scan(array<T, size> &a) {
for (auto &i : a)
scan(i);
}
template <class T, class L> void scan(pair<T, L> &p) {
scan(p.first);
scan(p.second);
}
template <class T, size_t size> void scan(T (&a)[size]) {
for (auto &i : a)
scan(i);
}
template <class T> void scan(T &a) { cin >> a; }
void in() {}
template <class Head, class... Tail> void in(Head &head, Tail &...tail) {
scan(head);
in(tail...);
}
void print() { putchar(' '); }
void print(bool a) { printf("%d", a); }
void print(int a) { printf("%d", a); }
void print(unsigned a) { printf("%u", a); }
void print(long a) { printf("%ld", a); }
void print(long long a) { printf("%lld", a); }
void print(unsigned long long a) { printf("%llu", a); }
void print(char a) { printf("%c", a); }
void print(char a[]) { printf("%s", a); }
void print(const char a[]) { printf("%s", a); }
void print(float a) { printf("%.15f", a); }
void print(double a) { printf("%.15f", a); }
void print(long double a) { printf("%.15Lf", a); }
void print(const string &a) {
for (auto &&i : a)
print(i);
}
template <class T> void print(const vector<T> &);
template <class T, size_t size> void print(const array<T, size> &);
template <class T, class L> void print(const pair<T, L> &p);
template <class T, size_t size> void print(const T (&)[size]);
template <class T> void print(const vector<T> &a) {
if (a.empty())
return;
print(a[0]);
for (auto i = a.begin(); ++i != a.end();) {
putchar(' ');
print(*i);
}
}
template <class T> void print(const deque<T> &a) {
if (a.empty())
return;
print(a[0]);
for (auto i = a.begin(); ++i != a.end();) {
putchar(' ');
print(*i);
}
}
template <class T, size_t size> void print(const array<T, size> &a) {
print(a[0]);
for (auto i = a.begin(); ++i != a.end();) {
putchar(' ');
print(*i);
}
}
template <class T, class L> void print(const pair<T, L> &p) {
print(p.first);
putchar(' ');
print(p.second);
}
template <class T, size_t size> void print(const T (&a)[size]) {
print(a[0]);
for (auto i = a; ++i != end(a);) {
putchar(' ');
print(*i);
}
}
template <class T> void print(const T &a) { cout << a; }
int out() {
putchar('\n');
return 0;
}
template <class T> int out(const T &t) {
print(t);
putchar('\n');
return 0;
}
template <class Head, class... Tail>
int out(const Head &head, const Tail &...tail) {
print(head);
putchar(' ');
out(tail...);
return 0;
}
#ifdef DEBUG
void err() { putchar('\n'); }
template <class T> void err(const T &t) {
print(t);
putchar('\n');
}
template <class Head, class... Tail>
void err(const Head &head, const Tail &...tail) {
print(head);
putchar(' ');
out(tail...);
}
#else
template <class... T> void err(const T &...) {}
#endif
int first(bool i = true) { return out(i ? "first" : "second"); }
int yes(bool i = true) { return out(i ? "yes" : "no"); }
int Yes(bool i = true) { return out(i ? "Yes" : "No"); }
int No() { return out("No"); }
int YES(bool i = true) { return out(i ? "YES" : "NO"); }
int NO() { return out("NO"); }
int Yay(bool i = true) { return out(i ? "Yay!" : ":("); }
int possible(bool i = true) { return out(i ? "possible" : "impossible"); }
int Possible(bool i = true) { return out(i ? "Possible" : "Impossible"); }
int POSSIBLE(bool i = true) { return out(i ? "POSSIBLE" : "IMPOSSIBLE"); }
void Case(ll i) { printf("Case #%lld: ", i); }
struct UnionFind {
vector<ll> data;
UnionFind(ll n) : data(n, -1) {}
bool unite(ll a, ll b) {
a = root(a);
b = root(b);
if (a == b)
return 1;
if (data[a] > data[b])
swap(a, b);
data[a] += data[b];
data[b] = a;
return 0;
}
bool find(ll a, ll b) { return root(a) == root(b); }
ll root(ll a) { return data[a] < 0 ? a : data[a] = root(data[a]); }
ll size(ll a) { return -data[root(a)]; }
ll operator[](ll a) { return root(a); }
};
signed main() {
LL(n, u, v);
u--;
v--;
vector<vector<ll>> g(n);
rep(n - 1) {
LL(a, b);
a--;
b--;
g[a].push_back(b);
g[b].push_back(a);
}
vector<ll> qa, qb;
vector<ll> costa(n, LINF), costb(n, LINF);
costa[u] = 0;
costb[v] = 0;
qa.push_back(u);
qb.push_back(v);
while (qa.size() || qb.size()) {
vector<ll> qa2, qb2;
each(at, qb) {
each(i, g[at]) if (chmin(costb[i], costb[at] + 1)) qb2.push_back(i);
}
each(at, qa) {
each(i, g[at]) if (costb[i] == LINF && chmin(costa[i], costa[at] + 1))
qa2.push_back(i);
}
swap(qa, qa2);
swap(qb, qb2);
}
ll ans = 0;
rep(n) if (costa[i] != LINF) chmax(ans, costb[i]);
out(ans - 1);
}
| insert | 371 | 371 | 371 | 373 | TLE | |
p02834 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
vector<int> dists_from(int v);
void dfs(int v, int d, int p);
int n;
vector<int> N[20000];
vector<int> dists;
int main(void) {
int u, v;
cin >> n >> u >> v;
u--;
v--;
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
N[a].push_back(b);
N[b].push_back(a);
}
vector<int> dist_u = dists_from(u);
vector<int> dist_v = dists_from(v);
int d = 0;
for (int i = 0; i < n; i++) {
if (dist_u[i] < dist_v[i]) {
d = max(d, dist_v[i]);
}
}
cout << d - 1 << endl;
return 0;
}
vector<int> dists_from(int v) {
dists = vector<int>(n);
dfs(v, 0, -1);
return dists;
}
void dfs(int v, int d, int p) {
for (const auto &u : N[v]) {
if (u == p) {
continue;
}
dfs(u, d + 1, v);
}
dists[v] = d;
} | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
vector<int> dists_from(int v);
void dfs(int v, int d, int p);
int n;
vector<int> N[200000];
vector<int> dists;
int main(void) {
int u, v;
cin >> n >> u >> v;
u--;
v--;
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
a--;
b--;
N[a].push_back(b);
N[b].push_back(a);
}
vector<int> dist_u = dists_from(u);
vector<int> dist_v = dists_from(v);
int d = 0;
for (int i = 0; i < n; i++) {
if (dist_u[i] < dist_v[i]) {
d = max(d, dist_v[i]);
}
}
cout << d - 1 << endl;
return 0;
}
vector<int> dists_from(int v) {
dists = vector<int>(n);
dfs(v, 0, -1);
return dists;
}
void dfs(int v, int d, int p) {
for (const auto &u : N[v]) {
if (u == p) {
continue;
}
dfs(u, d + 1, v);
}
dists[v] = d;
} | replace | 9 | 10 | 9 | 10 | 0 | |
p02834 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define fast_io \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define MOD (long long int)(1e9 + 7)
#define ll long long int
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n) for (int i = 1; i <= (int)(n); i++)
#define REP(i, n) for (int i = n - 1; i >= 0; i--)
#define REPS(i, n) for (int i = n; i > 0; i--)
#define INF (int)(1123456789)
#define LINF (long long int)(112345678901234567)
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))
#define all(v) v.begin(), v.end()
const int N = (int)3e5;
ll mpow(ll a, ll b) {
if (b == 0)
return 1;
else if (b % 2 == 0) {
ll memo = mpow(a, b / 2);
return memo * memo % MOD;
} else
return mpow(a, b - 1) * a % MOD;
}
ll lpow(ll a, ll b) {
if (b == 0)
return 1;
else if (b % 2 == 0) {
ll memo = lpow(a, b / 2);
return memo * memo;
} else
return lpow(a, b - 1) * a;
}
ll gcd(ll a, ll b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
vector<ll> kaijo_memo;
ll kaijo(ll n) {
if (kaijo_memo.size() > n)
return kaijo_memo[n];
if (kaijo_memo.size() == 0)
kaijo_memo.push_back(1);
while (kaijo_memo.size() <= n)
kaijo_memo.push_back(kaijo_memo[kaijo_memo.size() - 1] * kaijo_memo.size() %
MOD);
return kaijo_memo[n];
}
vector<ll> gyaku_kaijo_memo;
ll gyaku_kaijo(ll n) {
if (gyaku_kaijo_memo.size() > n)
return gyaku_kaijo_memo[n];
if (gyaku_kaijo_memo.size() == 0)
gyaku_kaijo_memo.push_back(1);
while (gyaku_kaijo_memo.size() <= n)
gyaku_kaijo_memo.push_back(gyaku_kaijo_memo[gyaku_kaijo_memo.size() - 1] *
mpow(gyaku_kaijo_memo.size(), MOD - 2) % MOD);
return gyaku_kaijo_memo[n];
}
ll nCr(ll n, ll r) {
if (n == r)
return 1; // 0個の丸と-1個の棒みたいな時に時に効く?不安.
if (n < r || r < 0)
return 0;
ll ret = 1;
ret *= kaijo(n);
ret %= MOD;
ret *= gyaku_kaijo(r);
ret %= MOD;
ret *= gyaku_kaijo(n - r);
ret %= MOD;
return ret;
}
vector<int> G[(int)(1e5 + 3)];
bool oni[(int)(1e5 + 3)], nige[(int)(1e5 + 3)];
bool checkOni[(int)(1e5 + 3)], checkNige[(int)(1e5 + 3)];
int main(void) {
fast_io int u, v;
int n;
cin >> n >> u >> v;
u--;
v--;
rep(i, n - 1) {
int a, b;
cin >> a >> b;
a--;
b--;
G[a].push_back(b);
G[b].push_back(a);
}
rep(i, n) {
checkOni[i] = false;
checkNige[i] = false;
oni[i] = false;
nige[i] = false;
}
queue<int> A, B;
A.push(u);
B.push(v);
oni[v] = true;
ll countNige = 0;
if (G[u].size() >= 2) {
countNige++;
nige[u] = true;
}
ll ans = 0;
while (true) {
queue<int> neA, neB;
while (A.size() > 0) {
int a = A.front();
A.pop();
if (!nige[a] && a != u) {
continue;
}
rep(j, G[a].size()) {
int next = G[a][j];
if (!checkNige[next] && !oni[next] && G[next].size() >= 2) {
checkNige[next] = true;
nige[next] = true;
countNige++;
neA.push(next);
}
}
}
if (countNige == 0) {
break;
}
ans++;
while (B.size() > 0) {
int b = B.front();
B.pop();
rep(j, G[b].size()) {
int next = G[b][j];
if (!checkOni[next]) {
checkOni[next] = true;
oni[next] = true;
if (nige[next]) {
nige[next] = false;
countNige--;
}
neB.push(next);
}
}
}
A = neA;
B = neB;
if (countNige == 0) {
break;
}
}
cout << ans << endl;
return 0;
} | #include <algorithm>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define fast_io \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define MOD (long long int)(1e9 + 7)
#define ll long long int
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n) for (int i = 1; i <= (int)(n); i++)
#define REP(i, n) for (int i = n - 1; i >= 0; i--)
#define REPS(i, n) for (int i = n; i > 0; i--)
#define INF (int)(1123456789)
#define LINF (long long int)(112345678901234567)
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))
#define all(v) v.begin(), v.end()
const int N = (int)3e5;
ll mpow(ll a, ll b) {
if (b == 0)
return 1;
else if (b % 2 == 0) {
ll memo = mpow(a, b / 2);
return memo * memo % MOD;
} else
return mpow(a, b - 1) * a % MOD;
}
ll lpow(ll a, ll b) {
if (b == 0)
return 1;
else if (b % 2 == 0) {
ll memo = lpow(a, b / 2);
return memo * memo;
} else
return lpow(a, b - 1) * a;
}
ll gcd(ll a, ll b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
vector<ll> kaijo_memo;
ll kaijo(ll n) {
if (kaijo_memo.size() > n)
return kaijo_memo[n];
if (kaijo_memo.size() == 0)
kaijo_memo.push_back(1);
while (kaijo_memo.size() <= n)
kaijo_memo.push_back(kaijo_memo[kaijo_memo.size() - 1] * kaijo_memo.size() %
MOD);
return kaijo_memo[n];
}
vector<ll> gyaku_kaijo_memo;
ll gyaku_kaijo(ll n) {
if (gyaku_kaijo_memo.size() > n)
return gyaku_kaijo_memo[n];
if (gyaku_kaijo_memo.size() == 0)
gyaku_kaijo_memo.push_back(1);
while (gyaku_kaijo_memo.size() <= n)
gyaku_kaijo_memo.push_back(gyaku_kaijo_memo[gyaku_kaijo_memo.size() - 1] *
mpow(gyaku_kaijo_memo.size(), MOD - 2) % MOD);
return gyaku_kaijo_memo[n];
}
ll nCr(ll n, ll r) {
if (n == r)
return 1; // 0個の丸と-1個の棒みたいな時に時に効く?不安.
if (n < r || r < 0)
return 0;
ll ret = 1;
ret *= kaijo(n);
ret %= MOD;
ret *= gyaku_kaijo(r);
ret %= MOD;
ret *= gyaku_kaijo(n - r);
ret %= MOD;
return ret;
}
vector<int> G[(int)(1e5 + 3)];
bool oni[(int)(1e5 + 3)], nige[(int)(1e5 + 3)];
bool checkOni[(int)(1e5 + 3)], checkNige[(int)(1e5 + 3)];
int main(void) {
fast_io int u, v;
int n;
cin >> n >> u >> v;
u--;
v--;
rep(i, n - 1) {
int a, b;
cin >> a >> b;
a--;
b--;
G[a].push_back(b);
G[b].push_back(a);
}
rep(i, n) {
checkOni[i] = false;
checkNige[i] = false;
oni[i] = false;
nige[i] = false;
}
queue<int> A, B;
A.push(u);
B.push(v);
oni[v] = true;
checkOni[v] = true;
checkNige[u] = true;
ll countNige = 0;
if (G[u].size() >= 2) {
countNige++;
nige[u] = true;
}
ll ans = 0;
while (true) {
queue<int> neA, neB;
while (A.size() > 0) {
int a = A.front();
A.pop();
if (!nige[a] && a != u) {
continue;
}
rep(j, G[a].size()) {
int next = G[a][j];
if (!checkNige[next] && !oni[next] && G[next].size() >= 2) {
checkNige[next] = true;
nige[next] = true;
countNige++;
neA.push(next);
}
}
}
if (countNige == 0) {
break;
}
ans++;
while (B.size() > 0) {
int b = B.front();
B.pop();
rep(j, G[b].size()) {
int next = G[b][j];
if (!checkOni[next]) {
checkOni[next] = true;
oni[next] = true;
if (nige[next]) {
nige[next] = false;
countNige--;
}
neB.push(next);
}
}
}
A = neA;
B = neB;
if (countNige == 0) {
break;
}
}
cout << ans << endl;
return 0;
} | insert | 123 | 123 | 123 | 125 | TLE | |
p02834 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
// long long
using ll = long long;
// pair<int, int>
using PII = pair<int, int>;
// 最大値、mod
const int MOD = 1000000007;
const int mod = 1000000007;
const int INF = 1000000000;
const long long LINF = 1e18;
const int MAX = 510000;
// 出力系
#define print(x) cout << x << endl
#define prints(x) cout << fixed << setprecision(20) << x << endl
#define printc(x) cout << setw(2) << setfill('0') << x << endl;
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl
// 配列入力
vector<long long> vecin(ll n) {
vector<long long> res(n);
for (int i = 0; i < n; i++)
cin >> res[i];
return res;
}
// begin() end()
#define all(x) (x).begin(), (x).end()
// for
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define rrep(i, a, b) for (int i = (a); i > (b); i--)
#define rep(i, a, b) for (int i = (a); i < (b); i++)
// 最大公約数
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
// 最小公倍数
unsigned lcm(unsigned a, unsigned b) { return a / gcd(a, b) * b; }
// a = max(a, b), a = min(a, b)
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
// num ^ pow(mod取る)
ll pow_mod(ll num, ll pow, ll mod) {
ll prod = 1;
num %= mod;
while (pow > 0) {
if (pow & 1)
prod = prod * num % mod;
num = num * num % mod;
pow >>= 1;
}
return prod;
}
// 二項係数(MODとる、1 ≦ k ≦ n ≦ 10^7 程度)
// COMinit()
// COM(x, y)
// とコンビで使う
// テーブルを作る前処理
long long fac[MAX], finv[MAX], inv[MAX];
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
// 二項係数計算
long long COM(int n, int k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
// UnionFind
struct UnionFind {
vector<int> par;
vector<int> rank;
vector<ll> Size;
UnionFind(int n = 1) { init(n); }
void init(int n = 1) {
par.resize(n + 1);
rank.resize(n + 1);
Size.resize(n + 1);
for (int i = 0; i <= n; ++i)
par[i] = i, rank[i] = 0, Size[i] = 1;
}
int root(int x) {
if (par[x] == x) {
return x;
} else {
int r = root(par[x]);
return par[x] = r;
}
}
bool issame(int x, int y) { return root(x) == root(y); }
bool merge(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return false;
if (rank[x] < rank[y])
swap(x, y);
if (rank[x] == rank[y])
++rank[x];
par[y] = x;
Size[x] += Size[y];
return true;
}
ll size(int x) { return Size[root(x)]; }
};
// modint構造体
struct Mint {
int val;
Mint inv() const {
int tmp, a = val, b = mod, x = 1, y = 0;
while (b)
tmp = a / b, a -= tmp * b, swap(a, b), x -= tmp * y, swap(x, y);
return Mint(x);
}
public:
Mint() : val(0) {}
Mint(ll x) {
if ((val = x % mod) < 0)
val += mod;
}
Mint pow(ll t) {
Mint res = 1, b = *this;
while (t) {
if (t & 1)
res *= b;
b *= b;
t >>= 1;
}
return res;
}
Mint &operator+=(const Mint &x) {
if ((val += x.val) >= mod)
val -= mod;
return *this;
}
Mint &operator-=(const Mint &x) {
if ((val += mod - x.val) >= mod)
val -= mod;
return *this;
}
Mint &operator*=(const Mint &x) {
val = (ll)val * x.val % mod;
return *this;
}
Mint &operator/=(const Mint &x) { return *this *= x.inv(); }
bool operator==(const Mint &x) const { return val == x.val; }
bool operator!=(const Mint &x) const { return val != x.val; }
bool operator<(const Mint &x) const { return val < x.val; }
bool operator<=(const Mint &x) const { return val <= x.val; }
bool operator>(const Mint &x) const { return val > x.val; }
bool operator>=(const Mint &x) const { return val >= x.val; }
Mint operator+(const Mint &x) const { return Mint(*this) += x; }
Mint operator-(const Mint &x) const { return Mint(*this) -= x; }
Mint operator*(const Mint &x) const { return Mint(*this) *= x; }
Mint operator/(const Mint &x) const { return Mint(*this) /= x; }
};
struct factorial {
vector<Mint> Fact, Finv;
public:
// factorial fact(10000010);
// fact.nCr(a, b)
// 「fact」の部分は自由に名前変更可能
factorial(int maxx) {
Fact.resize(maxx + 1), Finv.resize(maxx + 1);
Fact[0] = Mint(1);
rep(i, 0, maxx) Fact[i + 1] = Fact[i] * (i + 1);
Finv[maxx] = Mint(1) / Fact[maxx];
rrep(i, maxx, 0) Finv[i - 1] = Finv[i] * i;
}
Mint fact(int n, bool inv = 0) {
if (inv)
return Finv[n];
else
return Fact[n];
}
Mint nPr(int n, int r) {
if (n < 0 || n < r || r < 0)
return Mint(0);
else
return Fact[n] * Finv[n - r];
}
Mint nCr(int n, int r) {
if (n < 0 || n < r || r < 0)
return Mint(0);
else
return Fact[n] * Finv[r] * Finv[n - r];
}
};
// 1 * 2 * 3 .... * n (mod)
ll modfact(ll n) {
if (n <= 1)
return 1;
return (n * modfact(n - 1)) % MOD;
}
// kが角度だった場合:cos(k * (PI / 180));
// const double PI = acos(-1);のまま使うと円周率(M_PIもあるよ)
const double PI = acos(-1);
// 多次元 vector 生成 例: auto dp = make_vec<long long>(N+1, 5, 5, 5);
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
// 素因数分解
vector<pair<long long, int>> factorize(long long n) {
vector<pair<long long, int>> res;
for (long long i = 2; i * i <= n; ++i) {
if (n % i)
continue;
res.emplace_back(i, 0);
while (n % i == 0) {
n /= i;
res.back().second++;
}
}
if (n != 1)
res.emplace_back(n, 1);
return res;
}
// 素数判定
bool primejudge(long long a) {
if (a <= 1)
return false;
for (long long i = 2; i * i <= a; i++) {
if (a % i == 0)
return false;
}
return true;
}
// 累積和
// vector<long long>sums(vector<int>n){
// vector<long long>res(n.size() + 1, 0);
// for(int i = 0; i < n.size(); i++) res[i + 1] = n[i] + res[i];
// return res;
// }
int dx[4] = {1, 0, 0, -1}, dy[4] = {0, 1, -1, 0};
void dfs(int np, int ap, vector<int> &dist, int d, vector<vector<int>> g) {
dist[np] = d;
for (auto x : g[np]) {
if (x == ap)
continue;
dfs(x, np, dist, d + 1, g);
}
}
int main() {
int n;
cin >> n;
int t, a;
cin >> t >> a;
t--;
a--;
vector<vector<int>> g(n);
REP(i, n - 1) {
int x, y;
cin >> x >> y;
x--;
y--;
g[x].push_back(y);
g[y].push_back(x);
}
vector<int> tdist(n), adist(n);
dfs(t, -1, tdist, 0, g);
dfs(a, -1, adist, 0, g);
int ans = -1;
REP(i, n) {
if (tdist[i] < adist[i])
chmax(ans, adist[i] - 1);
}
print(ans);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
// long long
using ll = long long;
// pair<int, int>
using PII = pair<int, int>;
// 最大値、mod
const int MOD = 1000000007;
const int mod = 1000000007;
const int INF = 1000000000;
const long long LINF = 1e18;
const int MAX = 510000;
// 出力系
#define print(x) cout << x << endl
#define prints(x) cout << fixed << setprecision(20) << x << endl
#define printc(x) cout << setw(2) << setfill('0') << x << endl;
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl
// 配列入力
vector<long long> vecin(ll n) {
vector<long long> res(n);
for (int i = 0; i < n; i++)
cin >> res[i];
return res;
}
// begin() end()
#define all(x) (x).begin(), (x).end()
// for
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define rrep(i, a, b) for (int i = (a); i > (b); i--)
#define rep(i, a, b) for (int i = (a); i < (b); i++)
// 最大公約数
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
// 最小公倍数
unsigned lcm(unsigned a, unsigned b) { return a / gcd(a, b) * b; }
// a = max(a, b), a = min(a, b)
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
// num ^ pow(mod取る)
ll pow_mod(ll num, ll pow, ll mod) {
ll prod = 1;
num %= mod;
while (pow > 0) {
if (pow & 1)
prod = prod * num % mod;
num = num * num % mod;
pow >>= 1;
}
return prod;
}
// 二項係数(MODとる、1 ≦ k ≦ n ≦ 10^7 程度)
// COMinit()
// COM(x, y)
// とコンビで使う
// テーブルを作る前処理
long long fac[MAX], finv[MAX], inv[MAX];
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
// 二項係数計算
long long COM(int n, int k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
// UnionFind
struct UnionFind {
vector<int> par;
vector<int> rank;
vector<ll> Size;
UnionFind(int n = 1) { init(n); }
void init(int n = 1) {
par.resize(n + 1);
rank.resize(n + 1);
Size.resize(n + 1);
for (int i = 0; i <= n; ++i)
par[i] = i, rank[i] = 0, Size[i] = 1;
}
int root(int x) {
if (par[x] == x) {
return x;
} else {
int r = root(par[x]);
return par[x] = r;
}
}
bool issame(int x, int y) { return root(x) == root(y); }
bool merge(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return false;
if (rank[x] < rank[y])
swap(x, y);
if (rank[x] == rank[y])
++rank[x];
par[y] = x;
Size[x] += Size[y];
return true;
}
ll size(int x) { return Size[root(x)]; }
};
// modint構造体
struct Mint {
int val;
Mint inv() const {
int tmp, a = val, b = mod, x = 1, y = 0;
while (b)
tmp = a / b, a -= tmp * b, swap(a, b), x -= tmp * y, swap(x, y);
return Mint(x);
}
public:
Mint() : val(0) {}
Mint(ll x) {
if ((val = x % mod) < 0)
val += mod;
}
Mint pow(ll t) {
Mint res = 1, b = *this;
while (t) {
if (t & 1)
res *= b;
b *= b;
t >>= 1;
}
return res;
}
Mint &operator+=(const Mint &x) {
if ((val += x.val) >= mod)
val -= mod;
return *this;
}
Mint &operator-=(const Mint &x) {
if ((val += mod - x.val) >= mod)
val -= mod;
return *this;
}
Mint &operator*=(const Mint &x) {
val = (ll)val * x.val % mod;
return *this;
}
Mint &operator/=(const Mint &x) { return *this *= x.inv(); }
bool operator==(const Mint &x) const { return val == x.val; }
bool operator!=(const Mint &x) const { return val != x.val; }
bool operator<(const Mint &x) const { return val < x.val; }
bool operator<=(const Mint &x) const { return val <= x.val; }
bool operator>(const Mint &x) const { return val > x.val; }
bool operator>=(const Mint &x) const { return val >= x.val; }
Mint operator+(const Mint &x) const { return Mint(*this) += x; }
Mint operator-(const Mint &x) const { return Mint(*this) -= x; }
Mint operator*(const Mint &x) const { return Mint(*this) *= x; }
Mint operator/(const Mint &x) const { return Mint(*this) /= x; }
};
struct factorial {
vector<Mint> Fact, Finv;
public:
// factorial fact(10000010);
// fact.nCr(a, b)
// 「fact」の部分は自由に名前変更可能
factorial(int maxx) {
Fact.resize(maxx + 1), Finv.resize(maxx + 1);
Fact[0] = Mint(1);
rep(i, 0, maxx) Fact[i + 1] = Fact[i] * (i + 1);
Finv[maxx] = Mint(1) / Fact[maxx];
rrep(i, maxx, 0) Finv[i - 1] = Finv[i] * i;
}
Mint fact(int n, bool inv = 0) {
if (inv)
return Finv[n];
else
return Fact[n];
}
Mint nPr(int n, int r) {
if (n < 0 || n < r || r < 0)
return Mint(0);
else
return Fact[n] * Finv[n - r];
}
Mint nCr(int n, int r) {
if (n < 0 || n < r || r < 0)
return Mint(0);
else
return Fact[n] * Finv[r] * Finv[n - r];
}
};
// 1 * 2 * 3 .... * n (mod)
ll modfact(ll n) {
if (n <= 1)
return 1;
return (n * modfact(n - 1)) % MOD;
}
// kが角度だった場合:cos(k * (PI / 180));
// const double PI = acos(-1);のまま使うと円周率(M_PIもあるよ)
const double PI = acos(-1);
// 多次元 vector 生成 例: auto dp = make_vec<long long>(N+1, 5, 5, 5);
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
// 素因数分解
vector<pair<long long, int>> factorize(long long n) {
vector<pair<long long, int>> res;
for (long long i = 2; i * i <= n; ++i) {
if (n % i)
continue;
res.emplace_back(i, 0);
while (n % i == 0) {
n /= i;
res.back().second++;
}
}
if (n != 1)
res.emplace_back(n, 1);
return res;
}
// 素数判定
bool primejudge(long long a) {
if (a <= 1)
return false;
for (long long i = 2; i * i <= a; i++) {
if (a % i == 0)
return false;
}
return true;
}
// 累積和
// vector<long long>sums(vector<int>n){
// vector<long long>res(n.size() + 1, 0);
// for(int i = 0; i < n.size(); i++) res[i + 1] = n[i] + res[i];
// return res;
// }
int dx[4] = {1, 0, 0, -1}, dy[4] = {0, 1, -1, 0};
void dfs(int np, int ap, vector<int> &dist, int d, vector<vector<int>> &g) {
dist[np] = d;
for (auto x : g[np]) {
if (x == ap)
continue;
dfs(x, np, dist, d + 1, g);
}
}
int main() {
int n;
cin >> n;
int t, a;
cin >> t >> a;
t--;
a--;
vector<vector<int>> g(n);
REP(i, n - 1) {
int x, y;
cin >> x >> y;
x--;
y--;
g[x].push_back(y);
g[y].push_back(x);
}
vector<int> tdist(n), adist(n);
dfs(t, -1, tdist, 0, g);
dfs(a, -1, adist, 0, g);
int ans = -1;
REP(i, n) {
if (tdist[i] < adist[i])
chmax(ans, adist[i] - 1);
}
print(ans);
return 0;
}
| replace | 285 | 286 | 285 | 286 | TLE | |
p02834 | C++ | Runtime Error | #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
// vector string deque break continue
#define forn(i, s, f) for (int i = (int)s; i < (int)f; i++)
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair<int, int>
#define fs first
#define sc second
#define pf push_front
#define pb push_back
#define pop_f pop_front
#define pop_b pop_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
vector<vector<int>> gr;
void dfs(int v, int p, int depth, vector<vector<int>> &levels) {
levels[depth].pb(v);
for (int u : gr[v])
if (u != p)
dfs(u, v, depth + 1, levels);
}
vector<vector<int>> a, b;
bool check(int m) {
set<int> x, y;
for (int i = m; i >= 0; i -= 2) {
for (int j : a[i])
x.insert(j);
for (int j : b[i])
y.insert(j);
}
for (int i : x)
if (y.find(i) == y.end())
return false;
return true;
}
bool check1(int m) {
set<int> x, y;
for (int i = m; i >= 0; i -= 2) {
for (int j : a[i])
x.insert(j);
for (int j : b[i - 1])
y.insert(j);
}
for (int i : x)
if (y.find(i) == y.end())
return false;
return true;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n, x, y;
cin >> n >> x >> y, x--, y--;
gr.resize(n);
forn(i, 1, n) {
int v, u;
cin >> v >> u, v--, u--;
gr[v].pb(u), gr[u].pb(v);
}
a.resize(n + 2), b.resize(n + 2);
dfs(x, -1, 0, a), dfs(y, -1, 0, b);
/*forn (i, 0, n) {
cout << i << ":";
for (int j : a[i])
cout << j << " ";
cout << "\n";
}
forn (i, 0, n) {
cout << i << ":";
for (int j : b[i])
cout << j << " ";
cout << "\n";
}*/
/*forn (i, 0, n - 1) {
cout << i << ": " << check(i) << "\n";
}
cout << "\n";
forn (i, 1, n)
cout << i << ": " << check1(i) << "\n";*/
int l = 0, r = n - 1;
while (r - l > 1) {
int m = (l + r) / 2;
if (check(m))
r = m;
else
l = m;
}
int ans;
if (check(r))
ans = r;
else
ans = 0;
// cout << ans << " ";
l = 1, r = n - 1;
while (r - l > 1) {
int m = (l + r) / 2;
if (check1(m))
r = m;
else
l = m;
}
if (check1(r))
ans = max(ans, r - 1);
cout << ans;
return 0;
} | #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
// vector string deque break continue
#define forn(i, s, f) for (int i = (int)s; i < (int)f; i++)
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair<int, int>
#define fs first
#define sc second
#define pf push_front
#define pb push_back
#define pop_f pop_front
#define pop_b pop_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
vector<vector<int>> gr;
void dfs(int v, int p, int depth, vector<vector<int>> &levels) {
levels[depth].pb(v);
for (int u : gr[v])
if (u != p)
dfs(u, v, depth + 1, levels);
}
vector<vector<int>> a, b;
bool check(int m) {
set<int> x, y;
for (int i = m; i >= 0; i -= 2) {
for (int j : a[i])
x.insert(j);
for (int j : b[i])
y.insert(j);
}
for (int i : x)
if (y.find(i) == y.end())
return false;
return true;
}
bool check1(int m) {
set<int> x, y;
for (int i = m; i >= 0; i -= 2) {
for (int j : a[i])
x.insert(j);
if (i - 1 >= 0)
for (int j : b[i - 1])
y.insert(j);
}
for (int i : x)
if (y.find(i) == y.end())
return false;
return true;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n, x, y;
cin >> n >> x >> y, x--, y--;
gr.resize(n);
forn(i, 1, n) {
int v, u;
cin >> v >> u, v--, u--;
gr[v].pb(u), gr[u].pb(v);
}
a.resize(n + 2), b.resize(n + 2);
dfs(x, -1, 0, a), dfs(y, -1, 0, b);
/*forn (i, 0, n) {
cout << i << ":";
for (int j : a[i])
cout << j << " ";
cout << "\n";
}
forn (i, 0, n) {
cout << i << ":";
for (int j : b[i])
cout << j << " ";
cout << "\n";
}*/
/*forn (i, 0, n - 1) {
cout << i << ": " << check(i) << "\n";
}
cout << "\n";
forn (i, 1, n)
cout << i << ": " << check1(i) << "\n";*/
int l = 0, r = n - 1;
while (r - l > 1) {
int m = (l + r) / 2;
if (check(m))
r = m;
else
l = m;
}
int ans;
if (check(r))
ans = r;
else
ans = 0;
// cout << ans << " ";
l = 1, r = n - 1;
while (r - l > 1) {
int m = (l + r) / 2;
if (check1(m))
r = m;
else
l = m;
}
if (check1(r))
ans = max(ans, r - 1);
cout << ans;
return 0;
} | replace | 52 | 54 | 52 | 55 | 0 | |
p02834 | C++ | Runtime Error | #include "bits/stdc++.h"
// Begin Header {{{
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (i64 i = 0, i##_limit = (n); i < i##_limit; ++i)
#define reps(i, s, t) for (i64 i = (s), i##_limit = (t); i <= i##_limit; ++i)
#define repr(i, s, t) for (i64 i = (s), i##_limit = (t); i >= i##_limit; --i)
#define var(Type, ...) \
Type __VA_ARGS__; \
input(__VA_ARGS__)
#ifndef DBG
#define trace(...)
#endif
using namespace std;
using i64 = int_fast64_t;
using pii = pair<i64, i64>;
template <class T, class U> inline bool chmax(T &a, const U &b) {
return b > a && (a = b, true);
}
template <class T, class U> inline bool chmin(T &a, const U &b) {
return b < a && (a = b, true);
}
inline i64 sigma(i64 n) { return (n * (n + 1) >> 1); }
inline i64 updiv(i64 a, i64 b) { return (a + b - 1) / b; }
inline i64 sqr(i64 n) { return n * n; }
inline string to_string(char c) { return string(1, c); }
constexpr int INF = 0x3f3f3f3f;
constexpr i64 LINF = 0x3f3f3f3f3f3f3f3fLL;
template <class T> inline vector<T> make_v(const T &initValue, size_t sz) {
return vector<T>(sz, initValue);
}
template <class T, class... Args>
inline auto make_v(const T &initValue, size_t sz, Args... args) {
return vector<decltype(make_v<T>(initValue, args...))>(
sz, make_v<T>(initValue, args...));
}
inline void input() {}
template <class Head, class... Tail>
inline void input(Head &head, Tail &...tail) {
cin >> head;
input(tail...);
}
inline void print() { cout << "\n"; }
template <class Head, class... Tail>
inline void print(Head &&head, Tail &&...tail) {
cout << head;
if (sizeof...(tail))
cout << ' ';
print(forward<Tail>(tail)...);
}
template <class T>
inline ostream &operator<<(ostream &out, const vector<T> &vec) {
static constexpr const char *delim[] = {" ", ""};
for (const auto &e : vec)
out << e << delim[&e == &vec.back()];
return out;
}
template <typename Func> struct FixPoint : Func {
inline constexpr FixPoint(Func &&f) noexcept : Func(forward<Func>(f)) {}
template <typename... Args>
inline decltype(auto) operator()(Args &&...args) const {
return Func::operator()(*this, forward<Args>(args)...);
}
};
template <typename Func> inline decltype(auto) makeFixPoint(Func &&f) {
return FixPoint<Func>{forward<Func>(f)};
}
// }}} End Header
// graph {{{
struct Edge { // {{{
int src, to;
int64_t cost = 0;
inline Edge() noexcept {}
inline Edge(int s, int t, int64_t c = 1) noexcept : src(s), to(t), cost(c) {}
inline bool operator<(const Edge &rhs) const noexcept {
return cost < rhs.cost;
}
inline bool operator>(const Edge &rhs) const noexcept { return rhs < *this; }
inline operator int() const noexcept { return to; }
inline Edge &operator=(int rhs_to) noexcept {
this->to = rhs_to;
return *this;
}
};
// }}}
class Graph : public vector<vector<Edge>> { // {{{
using super = vector<vector<Edge>>;
public:
Graph() noexcept {}
Graph(int V) noexcept : super(V) {}
Graph &addEdge(int from, int to, int64_t cost = 1) noexcept {
super::operator[](from).emplace_back(from, to, cost);
return *this;
}
Graph &addEdge(const Edge &e) noexcept {
return addEdge(e.src, e.to, e.cost);
}
friend ostream &operator<<(ostream &out, const Graph &g) {
for (int i = 0; i < g.size(); ++i) {
out << '[' << setw(2) << i << ']';
for (const auto &e : g[i]) {
out << ' ' << setw(2) << e.to;
}
out << '\n';
}
return out;
}
}; // }}}
// }}}
class DoublingLCA { // {{{
public:
const int V;
const int LOG_V;
Graph G;
vector<int> depth;
vector<int64_t> weightSum;
vector<vector<int>>
table; // table[k][v] = ノード v から根へ 2^k だけ登った先のノード番号
DoublingLCA() = delete;
// ノード番号 [0, n) を管理する
DoublingLCA(int n)
: V(n), LOG_V(32 - __builtin_clz(V)), // ceil(log2(V))
G(V), depth(V), weightSum(V), table(LOG_V, vector<int>(V, -1)) {}
// u-v間に無向辺を張る。重みは省略可
inline void addEdge(int u, int v, int64_t weight = 0) noexcept {
G.addEdge(u, v, weight);
G.addEdge(v, u, weight);
}
// 前計算
void build(int root) noexcept {
dfs(root, -1, 0, 0);
for (int k = 0; (k + 1) < LOG_V; ++k) {
for (int v = 0; v < V; ++v) {
if (table[k][v] == -1)
continue;
table[k + 1][v] = table[k][table[k][v]];
}
}
}
// u,v のLCAを求める
inline int lca(int u, int v) const noexcept {
if (depth[u] > depth[v])
swap(u, v);
for (int i = LOG_V - 1; i >= 0; --i) {
if ((depth[v] - depth[u]) & (1 << i))
v = table[i][v];
}
if (u == v)
return u;
for (int i = LOG_V - 1; i >= 0; --i) {
if (table[i][u] != table[i][v]) {
u = table[i][u];
v = table[i][v];
}
}
return table[0][u];
}
// v から根へ k だけ登った先のノードを求める
inline int climb(int v, int k) const noexcept {
int i = 0;
while (k > 0) {
if (k & 1)
v = table[i][v];
++i;
k >>= 1;
}
return v;
}
// u-v間のパスの重みの和を求める
inline int pathWeight(int u, int v) const noexcept {
return weightSum[u] + weightSum[v] - 2 * weightSum[lca(u, v)];
}
// u-v間のパスの「辺の本数」を求める
inline int pathLength(int u, int v) const noexcept {
return depth[u] + depth[v] - 2 * depth[lca(u, v)];
}
private:
void dfs(int v, int par, int dep, int64_t ws) noexcept {
depth[v] = dep;
weightSum[v] = ws;
table[0][v] = par;
for (const auto &e : G[v]) {
if (e.to != par)
dfs(e.to, v, dep + 1, ws + e.cost);
}
}
}; // }}}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
var(int, N, T, A);
vector<int> deg(N);
DoublingLCA G(N + 1);
rep(i, N - 1) {
var(int, a, b);
G.addEdge(a, b);
++deg[a];
++deg[b];
}
G.build(1);
int ans = 0;
reps(i, 1, N) {
if (deg[i] != 1)
continue;
if (G.pathLength(T, i) >= G.pathLength(A, i))
continue;
chmax(ans, G.pathLength(A, i) - 1);
}
print(ans);
return 0;
}
| #include "bits/stdc++.h"
// Begin Header {{{
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (i64 i = 0, i##_limit = (n); i < i##_limit; ++i)
#define reps(i, s, t) for (i64 i = (s), i##_limit = (t); i <= i##_limit; ++i)
#define repr(i, s, t) for (i64 i = (s), i##_limit = (t); i >= i##_limit; --i)
#define var(Type, ...) \
Type __VA_ARGS__; \
input(__VA_ARGS__)
#ifndef DBG
#define trace(...)
#endif
using namespace std;
using i64 = int_fast64_t;
using pii = pair<i64, i64>;
template <class T, class U> inline bool chmax(T &a, const U &b) {
return b > a && (a = b, true);
}
template <class T, class U> inline bool chmin(T &a, const U &b) {
return b < a && (a = b, true);
}
inline i64 sigma(i64 n) { return (n * (n + 1) >> 1); }
inline i64 updiv(i64 a, i64 b) { return (a + b - 1) / b; }
inline i64 sqr(i64 n) { return n * n; }
inline string to_string(char c) { return string(1, c); }
constexpr int INF = 0x3f3f3f3f;
constexpr i64 LINF = 0x3f3f3f3f3f3f3f3fLL;
template <class T> inline vector<T> make_v(const T &initValue, size_t sz) {
return vector<T>(sz, initValue);
}
template <class T, class... Args>
inline auto make_v(const T &initValue, size_t sz, Args... args) {
return vector<decltype(make_v<T>(initValue, args...))>(
sz, make_v<T>(initValue, args...));
}
inline void input() {}
template <class Head, class... Tail>
inline void input(Head &head, Tail &...tail) {
cin >> head;
input(tail...);
}
inline void print() { cout << "\n"; }
template <class Head, class... Tail>
inline void print(Head &&head, Tail &&...tail) {
cout << head;
if (sizeof...(tail))
cout << ' ';
print(forward<Tail>(tail)...);
}
template <class T>
inline ostream &operator<<(ostream &out, const vector<T> &vec) {
static constexpr const char *delim[] = {" ", ""};
for (const auto &e : vec)
out << e << delim[&e == &vec.back()];
return out;
}
template <typename Func> struct FixPoint : Func {
inline constexpr FixPoint(Func &&f) noexcept : Func(forward<Func>(f)) {}
template <typename... Args>
inline decltype(auto) operator()(Args &&...args) const {
return Func::operator()(*this, forward<Args>(args)...);
}
};
template <typename Func> inline decltype(auto) makeFixPoint(Func &&f) {
return FixPoint<Func>{forward<Func>(f)};
}
// }}} End Header
// graph {{{
struct Edge { // {{{
int src, to;
int64_t cost = 0;
inline Edge() noexcept {}
inline Edge(int s, int t, int64_t c = 1) noexcept : src(s), to(t), cost(c) {}
inline bool operator<(const Edge &rhs) const noexcept {
return cost < rhs.cost;
}
inline bool operator>(const Edge &rhs) const noexcept { return rhs < *this; }
inline operator int() const noexcept { return to; }
inline Edge &operator=(int rhs_to) noexcept {
this->to = rhs_to;
return *this;
}
};
// }}}
class Graph : public vector<vector<Edge>> { // {{{
using super = vector<vector<Edge>>;
public:
Graph() noexcept {}
Graph(int V) noexcept : super(V) {}
Graph &addEdge(int from, int to, int64_t cost = 1) noexcept {
super::operator[](from).emplace_back(from, to, cost);
return *this;
}
Graph &addEdge(const Edge &e) noexcept {
return addEdge(e.src, e.to, e.cost);
}
friend ostream &operator<<(ostream &out, const Graph &g) {
for (int i = 0; i < g.size(); ++i) {
out << '[' << setw(2) << i << ']';
for (const auto &e : g[i]) {
out << ' ' << setw(2) << e.to;
}
out << '\n';
}
return out;
}
}; // }}}
// }}}
class DoublingLCA { // {{{
public:
const int V;
const int LOG_V;
Graph G;
vector<int> depth;
vector<int64_t> weightSum;
vector<vector<int>>
table; // table[k][v] = ノード v から根へ 2^k だけ登った先のノード番号
DoublingLCA() = delete;
// ノード番号 [0, n) を管理する
DoublingLCA(int n)
: V(n), LOG_V(32 - __builtin_clz(V)), // ceil(log2(V))
G(V), depth(V), weightSum(V), table(LOG_V, vector<int>(V, -1)) {}
// u-v間に無向辺を張る。重みは省略可
inline void addEdge(int u, int v, int64_t weight = 0) noexcept {
G.addEdge(u, v, weight);
G.addEdge(v, u, weight);
}
// 前計算
void build(int root) noexcept {
dfs(root, -1, 0, 0);
for (int k = 0; (k + 1) < LOG_V; ++k) {
for (int v = 0; v < V; ++v) {
if (table[k][v] == -1)
continue;
table[k + 1][v] = table[k][table[k][v]];
}
}
}
// u,v のLCAを求める
inline int lca(int u, int v) const noexcept {
if (depth[u] > depth[v])
swap(u, v);
for (int i = LOG_V - 1; i >= 0; --i) {
if ((depth[v] - depth[u]) & (1 << i))
v = table[i][v];
}
if (u == v)
return u;
for (int i = LOG_V - 1; i >= 0; --i) {
if (table[i][u] != table[i][v]) {
u = table[i][u];
v = table[i][v];
}
}
return table[0][u];
}
// v から根へ k だけ登った先のノードを求める
inline int climb(int v, int k) const noexcept {
int i = 0;
while (k > 0) {
if (k & 1)
v = table[i][v];
++i;
k >>= 1;
}
return v;
}
// u-v間のパスの重みの和を求める
inline int pathWeight(int u, int v) const noexcept {
return weightSum[u] + weightSum[v] - 2 * weightSum[lca(u, v)];
}
// u-v間のパスの「辺の本数」を求める
inline int pathLength(int u, int v) const noexcept {
return depth[u] + depth[v] - 2 * depth[lca(u, v)];
}
private:
void dfs(int v, int par, int dep, int64_t ws) noexcept {
depth[v] = dep;
weightSum[v] = ws;
table[0][v] = par;
for (const auto &e : G[v]) {
if (e.to != par)
dfs(e.to, v, dep + 1, ws + e.cost);
}
}
}; // }}}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
var(int, N, T, A);
vector<int> deg(N + 5);
DoublingLCA G(N + 1);
rep(i, N - 1) {
var(int, a, b);
G.addEdge(a, b);
++deg[a];
++deg[b];
}
G.build(1);
int ans = 0;
reps(i, 1, N) {
if (deg[i] != 1)
continue;
if (G.pathLength(T, i) >= G.pathLength(A, i))
continue;
chmax(ans, G.pathLength(A, i) - 1);
}
print(ans);
return 0;
}
| replace | 221 | 222 | 221 | 222 | 0 | |
p02835 | Python | Runtime Error | A1 = input()
A2 = input()
A3 = input()
| a, b, c = map(int, input().split())
if a + b + c >= 22:
print("bust")
else:
print("win")
| replace | 0 | 3 | 0 | 5 | EOFError: EOF when reading a line | Traceback (most recent call last):
File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02835/Python/s614017612.py", line 2, in <module>
A2 = input()
EOFError: EOF when reading a line
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.