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
p01489
C++
Runtime Error
#include <bits/stdc++.h> #define int long long using namespace std; typedef vector<int> vec; typedef vector<vec> mat; const int M = 1000000007; mat mul(mat A, mat B) { mat C(A.size(), vec(B[0].size())); for (int i = 0; i < A.size(); i++) { for (int j = 0; j < B[0].size(); j++) { for (int k = 0; k < A[0].size(); k++) { C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % M; } } } return C; } mat matpow(mat A, int k) { mat B(A.size(), vec(A.size())); for (int i = 0; i < A.size(); i++) B[i][i] = 1; while (k) { if (k & 1) B = mul(B, A); A = mul(A, A); k >>= 1; } return B; } int sol(int n) { mat A(2, vec(2)); A[0][0] = 1; A[0][1] = 1; A[1][0] = 1; A[1][1] = 0; A = matpow(A, n); return A[1][0]; } void out(int x, int y) { cout << (sol(x + 1) * sol(y + 1)) % M << endl; exit(0); } signed main() { int K; cin >> K; int l = 0, r = (1e9) * 4; while (l < r) { int mid = (l + r) / 2; if (mid * (mid + 1) >= K) r = mid; else l = mid + 1; } l--; int cnt = 0; K -= l * (l + 1); int x = r * 2 - 1, p = 0; if (K > r) K -= r, x++, p++; int y = 1; while (x >= y) { cnt++; if (cnt == K) out(x, y); x -= 2; y += 2; } x += 2; y -= 2; // cout<<r<<endl; // cout<<x<<' '<<y<<endl; if (!p) { if ((x == r + 1 && y == r - 1)) x--, y++; else x++, y--; } else { if (x == y + x / 2) x++, y--; else x--, y++; } // cout<<x<<' '<<y<<endl; while (1) { cnt++; if (cnt >= 1000000000) exit(1); if (cnt == K) out(x, y); x += 2; y -= 2; } // cout<<K<<' '<<l<<endl; }
#include <bits/stdc++.h> #define int long long using namespace std; typedef vector<int> vec; typedef vector<vec> mat; const int M = 1000000007; mat mul(mat A, mat B) { mat C(A.size(), vec(B[0].size())); for (int i = 0; i < A.size(); i++) { for (int j = 0; j < B[0].size(); j++) { for (int k = 0; k < A[0].size(); k++) { C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % M; } } } return C; } mat matpow(mat A, int k) { mat B(A.size(), vec(A.size())); for (int i = 0; i < A.size(); i++) B[i][i] = 1; while (k) { if (k & 1) B = mul(B, A); A = mul(A, A); k >>= 1; } return B; } int sol(int n) { mat A(2, vec(2)); A[0][0] = 1; A[0][1] = 1; A[1][0] = 1; A[1][1] = 0; A = matpow(A, n); return A[1][0]; } void out(int x, int y) { cout << (sol(x + 1) * sol(y + 1)) % M << endl; exit(0); } signed main() { int K; cin >> K; int l = 0, r = (1e9) * 4; while (l < r) { int mid = (l + r) / 2; if (mid * (mid + 1) >= K) r = mid; else l = mid + 1; } l--; int cnt = 0; K -= l * (l + 1); int x = r * 2 - 1, p = 0; if (K > r) K -= r, x++, p++; int y = 1; while (x >= y) { cnt++; if (cnt == K) out(x, y); x -= 2; y += 2; } x += 2; y -= 2; // cout<<r<<endl; // cout<<x<<' '<<y<<endl; if (!p) { if ((x == r + 1 && y == r - 1)) x--, y++; else x++, y--; } else { if (x == y + x / 2) x++, y--; else x--, y++; } // cout<<x<<' '<<y<<endl; while (1) { cnt++; if (cnt >= 2000000000) exit(1); if (cnt == K) out(x, y); x += 2; y -= 2; } // cout<<K<<' '<<l<<endl; }
replace
90
91
90
91
0
p01490
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define int long long #define rep(i, n) for (int i = 0; i < (n); i++) #define pb push_back #define all(v) (v).begin(), (v).end() #define fi first #define se second typedef vector<int> vint; typedef pair<int, int> pint; typedef vector<pint> vpint; template <typename A, typename B> inline void chmin(A &a, B b) { if (a > b) a = b; } template <typename A, typename B> inline void chmax(A &a, B b) { if (a < b) a = b; } typedef long double D; const int LOOP = 200; const D PI = acos(-1); const D EPS = 1e-8; D calc(vint r) { int n = r.size(); D ret = 0.0; do { vint rr(n); rep(i, n) rr[i] = r[i] * r[(i + 1) % n]; int ei = LLONG_MAX; rep(i, n) chmin(ei, rr[i]); D lb = -ei; D ub = ei; rep(i, LOOP) { D mid = (lb + ub) / 2.0; D sum = 0.0; rep(j, n) sum += acos(mid / rr[j]); if (sum > 2 * PI) lb = mid; else ub = mid; } D sum = 0.0; rep(j, n) sum += acos(lb / rr[j]); if (abs(sum - 2 * PI) > EPS) { continue; //??????? } sum = 0.0; rep(i, n) sum += rr[i] * sin(acos(lb / rr[i])); chmax(ret, sum); } while (next_permutation(all(r))); return ret; } signed main() { int N; cin >> N; vint R(N); rep(i, N) cin >> R[i]; sort(all(R)); D ans = 0.0; rep(i, 1 << N) { int num = __builtin_popcount(i); if (num <= 2) continue; vint r; rep(j, N) if (i >> j & 1) r.pb(R[j]); chmax(ans, calc(r)); } printf("%.20Lf\n", ans / 2.0); return 0; }
#include <bits/stdc++.h> using namespace std; #define int long long #define rep(i, n) for (int i = 0; i < (n); i++) #define pb push_back #define all(v) (v).begin(), (v).end() #define fi first #define se second typedef vector<int> vint; typedef pair<int, int> pint; typedef vector<pint> vpint; template <typename A, typename B> inline void chmin(A &a, B b) { if (a > b) a = b; } template <typename A, typename B> inline void chmax(A &a, B b) { if (a < b) a = b; } typedef long double D; const int LOOP = 100; const D PI = acos(-1); const D EPS = 1e-8; D calc(vint r) { int n = r.size(); D ret = 0.0; do { vint rr(n); rep(i, n) rr[i] = r[i] * r[(i + 1) % n]; int ei = LLONG_MAX; rep(i, n) chmin(ei, rr[i]); D lb = -ei; D ub = ei; rep(i, LOOP) { D mid = (lb + ub) / 2.0; D sum = 0.0; rep(j, n) sum += acos(mid / rr[j]); if (sum > 2 * PI) lb = mid; else ub = mid; } D sum = 0.0; rep(j, n) sum += acos(lb / rr[j]); if (abs(sum - 2 * PI) > EPS) { continue; //??????? } sum = 0.0; rep(i, n) sum += rr[i] * sin(acos(lb / rr[i])); chmax(ret, sum); } while (next_permutation(all(r))); return ret; } signed main() { int N; cin >> N; vint R(N); rep(i, N) cin >> R[i]; sort(all(R)); D ans = 0.0; rep(i, 1 << N) { int num = __builtin_popcount(i); if (num <= 2) continue; vint r; rep(j, N) if (i >> j & 1) r.pb(R[j]); chmax(ans, calc(r)); } printf("%.20Lf\n", ans / 2.0); return 0; }
replace
25
26
25
26
TLE
p01490
C++
Time Limit Exceeded
#include <algorithm> #include <assert.h> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stdio.h> #include <string.h> #include <string> #include <vector> using namespace std; typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; static const double EPS = 1e-9; static const double PI = acos(-1.0); #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define FOR(i, s, n) for (int i = (s); i < (int)(n); i++) #define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++) #define FORIT(it, c) \ for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++) #define MEMSET(v, h) memset((v), h, sizeof(v)) int n; double rs[10]; int main() { while (scanf("%d", &n) > 0) { REP(i, n) { scanf("%lf", &rs[i]); } sort(rs, rs + n); double ans = 0.0; do { for (int last = 3; last <= n; last++) { double left = 0.0; double right = PI; REP(iter, 100) { double mid = (left + right) / 2.0; double lans = rs[0] * rs[1] * sin(mid) / 2.0; double sum = mid; for (int i = 1; i < last; i++) { double v = rs[0] * rs[1] / rs[i] / rs[(i + 1) % last] * cos(mid); if (v > 1) { v = 1; } if (v < -1) { v = -1; } double theta = acos(v); sum += theta; lans += rs[i] * rs[(i + 1) % last] * sin(theta) / 2.0; } if (sum > 2 * PI) { right = mid; } else { left = mid; } if (fabs(sum - 2 * PI) < EPS) { ans = max(ans, lans); } } } } while (next_permutation(rs, rs + n)); printf("%.8f\n", ans); } }
#include <algorithm> #include <assert.h> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stdio.h> #include <string.h> #include <string> #include <vector> using namespace std; typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; static const double EPS = 1e-9; static const double PI = acos(-1.0); #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define FOR(i, s, n) for (int i = (s); i < (int)(n); i++) #define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++) #define FORIT(it, c) \ for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++) #define MEMSET(v, h) memset((v), h, sizeof(v)) int n; double rs[10]; int main() { while (scanf("%d", &n) > 0) { REP(i, n) { scanf("%lf", &rs[i]); } sort(rs, rs + n); double ans = 0.0; do { for (int last = 3; last <= n; last++) { double left = 0.0; double right = PI; REP(iter, 50) { double mid = (left + right) / 2.0; double lans = rs[0] * rs[1] * sin(mid) / 2.0; double sum = mid; for (int i = 1; i < last; i++) { double v = rs[0] * rs[1] / rs[i] / rs[(i + 1) % last] * cos(mid); if (v > 1) { v = 1; } if (v < -1) { v = -1; } double theta = acos(v); sum += theta; lans += rs[i] * rs[(i + 1) % last] * sin(theta) / 2.0; } if (sum > 2 * PI) { right = mid; } else { left = mid; } if (fabs(sum - 2 * PI) < EPS) { ans = max(ans, lans); } } } } while (next_permutation(rs, rs + n)); printf("%.8f\n", ans); } }
replace
38
39
38
39
TLE
p01491
C++
Memory Limit Exceeded
#include <algorithm> #include <iostream> using namespace std; #define rep(i, n) for (ll i = 0; i < (n); ++i) typedef long long ll; ll a[2500000], b[2500000], sa[2500001], sb[2500001]; ll M, N, m0, md, n0, nd, ans = 1e18; ll val(ll i, ll j) { return (M - i) * (N - j) + sa[i] + sb[j]; } int main() { cin >> M >> N >> m0 >> md >> n0 >> nd; a[0] = m0; rep(i, M - 1) a[i + 1] = (a[i] * 58 + md) % (N + 1); b[0] = n0; rep(i, N - 1) b[i + 1] = (b[i] * 58 + nd) % (M + 1); sort(a, a + M); sort(b, b + N); rep(i, M) sa[i + 1] = sa[i] + a[i]; rep(i, N) sb[i + 1] = sb[i] + b[i]; ll j = N, now = sb[N]; rep(i, M + 1) { while (j > 0 && val(i, j) > val(i, j - 1)) { j--; } ans = min(ans, val(i, j)); } cout << ans << endl; }
#include <algorithm> #include <iostream> using namespace std; #define rep(i, n) for (ll i = 0; i < (n); ++i) typedef long long ll; int a[2500000], b[2500000]; ll M, N, m0, md, n0, nd, ans = 1e18, sa[2500001], sb[2500001]; ll val(ll i, ll j) { return (M - i) * (N - j) + sa[i] + sb[j]; } int main() { cin >> M >> N >> m0 >> md >> n0 >> nd; a[0] = m0; rep(i, M - 1) a[i + 1] = (a[i] * 58 + md) % (N + 1); b[0] = n0; rep(i, N - 1) b[i + 1] = (b[i] * 58 + nd) % (M + 1); sort(a, a + M); sort(b, b + N); rep(i, M) sa[i + 1] = sa[i] + a[i]; rep(i, N) sb[i + 1] = sb[i] + b[i]; ll j = N, now = sb[N]; rep(i, M + 1) { while (j > 0 && val(i, j) > val(i, j - 1)) { j--; } ans = min(ans, val(i, j)); } cout << ans << endl; }
replace
5
7
5
7
MLE
p01491
C++
Memory Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++) #define REP(i, n) FOR(i, 0, n) #define ALL(a) a.begin(), a.end() #define MS(m, v) memset(m, v, sizeof(m)) typedef long long ll; typedef long double ld; typedef vector<ll> vi; typedef vector<string> vs; typedef pair<int, int> pii; const int MOD = 1000000007; const int INF = MOD + 1; const ld EPS = 1e-12; template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); } template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); } /*--------------------template--------------------*/ ll m, n, m0, md, n0, nd; vi sum_m, sum_n; ll calc(int pm, int pn) { return sum_m[pm] + sum_n[pn] + (m - pm) * (n - pn); } int main() { cin.sync_with_stdio(false); cout << fixed << setprecision(10); cin >> m >> n >> m0 >> md >> n0 >> nd; vi v(m), w(n); v[0] = m0; REP(i, m - 1) v[i + 1] = (v[i] * 58 + md) % (n + 1); w[0] = n0; REP(i, n - 1) w[i + 1] = (w[i] * 58 + nd) % (m + 1); sort(ALL(v)); sort(ALL(w)); sum_m.resize(m + 1); sum_n.resize(n + 1); REP(i, m) sum_m[i + 1] = sum_m[i] + v[i]; REP(i, n) sum_n[i + 1] = sum_n[i] + w[i]; ll ans = LLONG_MAX; int pm = 0, pn = n; while (1) { ll tmp = calc(pm, pn); chmin(ans, tmp); while (1) { if (pn == 0) break; ll nx = calc(pm, pn - 1); if (tmp < nx) break; pn--; tmp = nx; chmin(ans, tmp); } pm++; if (pn < 0 || pm > m) break; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++) #define REP(i, n) FOR(i, 0, n) #define ALL(a) a.begin(), a.end() #define MS(m, v) memset(m, v, sizeof(m)) typedef long long ll; typedef long double ld; typedef vector<ll> vi; typedef vector<string> vs; typedef pair<int, int> pii; const int MOD = 1000000007; const int INF = MOD + 1; const ld EPS = 1e-12; template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); } template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); } /*--------------------template--------------------*/ ll m, n, m0, md, n0, nd; vi sum_m, sum_n; ll calc(int pm, int pn) { return sum_m[pm] + sum_n[pn] + (m - pm) * (n - pn); } int main() { cin.sync_with_stdio(false); cout << fixed << setprecision(10); cin >> m >> n >> m0 >> md >> n0 >> nd; vector<int> v(m), w(n); v[0] = m0; REP(i, m - 1) v[i + 1] = (v[i] * 58 + md) % (n + 1); w[0] = n0; REP(i, n - 1) w[i + 1] = (w[i] * 58 + nd) % (m + 1); sort(ALL(v)); sort(ALL(w)); sum_m.resize(m + 1); sum_n.resize(n + 1); REP(i, m) sum_m[i + 1] = sum_m[i] + v[i]; REP(i, n) sum_n[i + 1] = sum_n[i] + w[i]; ll ans = LLONG_MAX; int pm = 0, pn = n; while (1) { ll tmp = calc(pm, pn); chmin(ans, tmp); while (1) { if (pn == 0) break; ll nx = calc(pm, pn - 1); if (tmp < nx) break; pn--; tmp = nx; chmin(ans, tmp); } pm++; if (pn < 0 || pm > m) break; } cout << ans << endl; return 0; }
replace
28
29
28
29
MLE
p01493
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> #define X first #define Y second #define pb push_back #define rep(X, Y) for (int(X) = 0; (X) < (Y); ++(X)) #define rrep(X, Y) for (int(X) = (Y - 1); (X) >= 0; --(X)) #define repe(X, Y) for ((X) = 0; (X) < (Y); ++(X)) #define peat(X, Y) for (; (X) < (Y); ++(X)) #define all(X) (X).begin(), (X).end() #define rall(X) (X).rbegin(), (X).rend() using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; template <class T> ostream &operator<<(ostream &os, const vector<T> &t) { os << "{"; rep(i, t.size()) { os << t[i] << ","; } os << "}" << endl; return os; } template <class S, class T> ostream &operator<<(ostream &os, const pair<S, T> &t) { return os << "(" << t.first << "," << t.second << ")"; } typedef vector<string> Graph; int used[1123], v; int dfs(Graph &g, int p) { if (used[p]) return 0; used[p] = 1; int re = 1; rep(i, v) if (g[p][i] == 'Y') re += dfs(g, i); return re; } int memo[112][2][2][2][2]; int solve(int n, int edge, int even, int rem, int to) { // cout<<"("<<n<<","<<edge<<","<<even<<","<<rem<<","<<to<<")"<<endl; int re = 0; if (n == 2 && even == 0) return to == 1; if (n == 0 && even == 2) return to == 0; if (memo[n][edge][even][rem][to]) return memo[n][edge][even][rem][to] - 1; if (even == 1 && to == 0) { re = 1; } else if (!rem) { re = 1 - solve(n - 2 + 2 * even, edge + even, 1 - even, 1, 1 - to); } else { if (even) { re |= 1 - solve(n, 1 - edge, 0, 1, 1 - to); if (rem % 2) re |= 1 - solve(n, 1 - edge, 1, 0, 1 - to); } else { re |= 1 - solve(n - 2, edge, 1, 1, 1 - to); if (rem % 2) re |= 1 - solve(n - 2, edge, 1, 0, 1 - to); } } memo[n][edge][even][rem][to] = re + 1; // cout<<"("<<n<<","<<edge<<","<<even<<","<<rem<<","<<to<<"):"<<re<<endl; return re; } int main() { ios_base::sync_with_stdio(false); cout << fixed << setprecision(0); int i, j, k, e = 0; cin >> v; Graph strs(v); vector<int> ccs; rep(i, v) { cin >> strs[i]; e += count(all(strs[i]), 'Y'); } e /= 2; // cout<<e<<endl; rep(i, v) if (!used[i]) ccs.pb(dfs(strs, i)); int rem = 0; for (int cc : ccs) rem += (cc - 1) * cc / 2; rem -= e; // cout<<ccs<<endl; if (v % 2) { cout << (e % 2 == v % 4 / 2 ? "Hanako" : "Taro") << endl; } else { int oc = 0, ec = ccs.size(); rep(i, ccs.size()) oc += ccs[i] % 2; ec -= oc; // cout<<ec<<","<<oc<<endl; if (ec == 0) { /*if(ccs.size()==2){ cout<<"Hanako"<<endl; return 0; }*/ // cout<<"odd:"<<solve(oc,rem%2,0,rem>0,1)<<endl; // cout<<"even:"<<solve(oc,rem%2,0,rem>0,0)<<endl; cout << (e % 2 == v / 2 % 2 && solve(oc, rem % 2, 0, rem > 0, 1) || e % 2 != v / 2 % 2 && solve(oc, rem % 2, 0, rem > 0, 0) ? "Taro" : "Hanako") << endl; } else if (ec == 1) { int odd = 0; cout << (solve(oc, rem % 2, 1, rem > 0, 1) || e % 2 != v / 2 % 2 ? "Taro" : "Hanako") << endl; } else { cout << (e % 2 != v / 2 % 2 ? "Taro" : "Hanako") << endl; } } return 0; }
#include <algorithm> #include <bitset> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> #define X first #define Y second #define pb push_back #define rep(X, Y) for (int(X) = 0; (X) < (Y); ++(X)) #define rrep(X, Y) for (int(X) = (Y - 1); (X) >= 0; --(X)) #define repe(X, Y) for ((X) = 0; (X) < (Y); ++(X)) #define peat(X, Y) for (; (X) < (Y); ++(X)) #define all(X) (X).begin(), (X).end() #define rall(X) (X).rbegin(), (X).rend() using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; template <class T> ostream &operator<<(ostream &os, const vector<T> &t) { os << "{"; rep(i, t.size()) { os << t[i] << ","; } os << "}" << endl; return os; } template <class S, class T> ostream &operator<<(ostream &os, const pair<S, T> &t) { return os << "(" << t.first << "," << t.second << ")"; } typedef vector<string> Graph; int used[1123], v; int dfs(Graph &g, int p) { if (used[p]) return 0; used[p] = 1; int re = 1; rep(i, v) if (g[p][i] == 'Y') re += dfs(g, i); return re; } int memo[1124][2][2][2][2]; int solve(int n, int edge, int even, int rem, int to) { // cout<<"("<<n<<","<<edge<<","<<even<<","<<rem<<","<<to<<")"<<endl; int re = 0; if (n == 2 && even == 0) return to == 1; if (n == 0 && even == 2) return to == 0; if (memo[n][edge][even][rem][to]) return memo[n][edge][even][rem][to] - 1; if (even == 1 && to == 0) { re = 1; } else if (!rem) { re = 1 - solve(n - 2 + 2 * even, edge + even, 1 - even, 1, 1 - to); } else { if (even) { re |= 1 - solve(n, 1 - edge, 0, 1, 1 - to); if (rem % 2) re |= 1 - solve(n, 1 - edge, 1, 0, 1 - to); } else { re |= 1 - solve(n - 2, edge, 1, 1, 1 - to); if (rem % 2) re |= 1 - solve(n - 2, edge, 1, 0, 1 - to); } } memo[n][edge][even][rem][to] = re + 1; // cout<<"("<<n<<","<<edge<<","<<even<<","<<rem<<","<<to<<"):"<<re<<endl; return re; } int main() { ios_base::sync_with_stdio(false); cout << fixed << setprecision(0); int i, j, k, e = 0; cin >> v; Graph strs(v); vector<int> ccs; rep(i, v) { cin >> strs[i]; e += count(all(strs[i]), 'Y'); } e /= 2; // cout<<e<<endl; rep(i, v) if (!used[i]) ccs.pb(dfs(strs, i)); int rem = 0; for (int cc : ccs) rem += (cc - 1) * cc / 2; rem -= e; // cout<<ccs<<endl; if (v % 2) { cout << (e % 2 == v % 4 / 2 ? "Hanako" : "Taro") << endl; } else { int oc = 0, ec = ccs.size(); rep(i, ccs.size()) oc += ccs[i] % 2; ec -= oc; // cout<<ec<<","<<oc<<endl; if (ec == 0) { /*if(ccs.size()==2){ cout<<"Hanako"<<endl; return 0; }*/ // cout<<"odd:"<<solve(oc,rem%2,0,rem>0,1)<<endl; // cout<<"even:"<<solve(oc,rem%2,0,rem>0,0)<<endl; cout << (e % 2 == v / 2 % 2 && solve(oc, rem % 2, 0, rem > 0, 1) || e % 2 != v / 2 % 2 && solve(oc, rem % 2, 0, rem > 0, 0) ? "Taro" : "Hanako") << endl; } else if (ec == 1) { int odd = 0; cout << (solve(oc, rem % 2, 1, rem > 0, 1) || e % 2 != v / 2 % 2 ? "Taro" : "Hanako") << endl; } else { cout << (e % 2 != v / 2 % 2 ? "Taro" : "Hanako") << endl; } } return 0; }
replace
55
56
55
56
0
p01493
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++) #define REP(i, n) FOR(i, 0, n) #define ALL(a) a.begin(), a.end() #define MS(m, v) memset(m, v, sizeof(m)) typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef vector<string> vs; typedef pair<int, int> pii; const int MOD = 1000000007; const int INF = MOD + 1; const ld EPS = 1e-12; template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); } template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); } /*--------------------template--------------------*/ typedef int Weight; struct Edge { int from, to; Weight cost; bool operator<(const Edge &e) const { return cost < e.cost; } bool operator>(const Edge &e) const { return cost > e.cost; } }; typedef vector<Edge> Edges; typedef vector<Edges> Graph; typedef vector<Weight> Array; typedef vector<Array> Matrix; void add_edge(Graph &g, int from, int to, Weight cost) { g[from].push_back(Edge{from, to, cost}); } int dp[1111][1111][2]; int solve(int even, int odd, int edge) { if (even + odd == 1) return 1; if (dp[even][odd][edge] >= 0) return dp[even][odd][edge]; int res = 1; if (even >= 2) chmin(res, solve(even - 1, odd, 1 - edge)); if (odd >= 2) chmin(res, solve(even + 1, odd - 2, edge)); if (even >= 1 && odd >= 1) chmin(res, solve(even - 1, odd, 1 - edge)); if (edge == 1) chmin(res, solve(even, odd, 0)); return 1 - res; } int main() { cin.sync_with_stdio(false); cout << fixed << setprecision(10); int V; cin >> V; Graph g(V); REP(i, V) REP(j, V) { char a; cin >> a; if (a == 'Y') add_edge(g, i, j, 0); } vector<bool> vis(V); int even = 0, odd = 0, edge = 0; REP(i, V) { if (vis[i]) continue; vis[i] = true; vi v; v.push_back(i); queue<int> que; que.push(i); while (!que.empty()) { int t = que.front(); que.pop(); for (auto j : g[t]) { int nx = j.to; if (vis[nx]) continue; vis[nx] = true; v.push_back(nx); que.push(nx); } } if (v.size() % 2) odd++; else even++; int cnt = 0; REP(j, v.size()) { cnt += v.size() - g[v[j]].size() - 1; } cnt /= 2; if (cnt % 2) edge++; edge %= 2; } MS(dp, -1); int ans = solve(even, odd, edge); if (ans) cout << "Taro" << endl; else cout << "Hanako" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++) #define REP(i, n) FOR(i, 0, n) #define ALL(a) a.begin(), a.end() #define MS(m, v) memset(m, v, sizeof(m)) typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef vector<string> vs; typedef pair<int, int> pii; const int MOD = 1000000007; const int INF = MOD + 1; const ld EPS = 1e-12; template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); } template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); } /*--------------------template--------------------*/ typedef int Weight; struct Edge { int from, to; Weight cost; bool operator<(const Edge &e) const { return cost < e.cost; } bool operator>(const Edge &e) const { return cost > e.cost; } }; typedef vector<Edge> Edges; typedef vector<Edges> Graph; typedef vector<Weight> Array; typedef vector<Array> Matrix; void add_edge(Graph &g, int from, int to, Weight cost) { g[from].push_back(Edge{from, to, cost}); } int dp[1111][1111][2]; int solve(int even, int odd, int edge) { if (even + odd == 1) return 1; if (dp[even][odd][edge] >= 0) return dp[even][odd][edge]; int res = 1; if (even >= 2) chmin(res, solve(even - 1, odd, 1 - edge)); if (odd >= 2) chmin(res, solve(even + 1, odd - 2, edge)); if (even >= 1 && odd >= 1) chmin(res, solve(even - 1, odd, 1 - edge)); if (edge == 1) chmin(res, solve(even, odd, 0)); return dp[even][odd][edge] = 1 - res; } int main() { cin.sync_with_stdio(false); cout << fixed << setprecision(10); int V; cin >> V; Graph g(V); REP(i, V) REP(j, V) { char a; cin >> a; if (a == 'Y') add_edge(g, i, j, 0); } vector<bool> vis(V); int even = 0, odd = 0, edge = 0; REP(i, V) { if (vis[i]) continue; vis[i] = true; vi v; v.push_back(i); queue<int> que; que.push(i); while (!que.empty()) { int t = que.front(); que.pop(); for (auto j : g[t]) { int nx = j.to; if (vis[nx]) continue; vis[nx] = true; v.push_back(nx); que.push(nx); } } if (v.size() % 2) odd++; else even++; int cnt = 0; REP(j, v.size()) { cnt += v.size() - g[v[j]].size() - 1; } cnt /= 2; if (cnt % 2) edge++; edge %= 2; } MS(dp, -1); int ans = solve(even, odd, edge); if (ans) cout << "Taro" << endl; else cout << "Hanako" << endl; return 0; }
replace
50
51
50
51
TLE
p01493
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int N; bool visited[100]; char a[100][100]; int cnt = 0, k = 0; int cc; int t[2]; void dfs(int pos) { if (visited[pos]) return; visited[pos] = true; cc++; for (int i = 0; i < N; i++) if (a[pos][i] == 'Y') dfs(i); } int main() { cin >> N; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { cin >> a[i][j]; if (a[i][j] == 'Y') cnt++; } } for (int i = 0; i < N; i++) { if (visited[i]) continue; k++; cc = 0; dfs(i); t[cc % 2]++; } cnt /= 2; int total = N * (N - 1) / 2; if (N % 2 == 1 || t[1] % 2 == 1) { if ((total - cnt) % 2 == 1) cout << "Taro" << endl; else cout << "Hanako" << endl; return 0; } if (t[0] == 0 && t[1] == 2) { if ((total - cnt) % 2 == 0) cout << "Taro" << endl; else cout << "Hanako" << endl; return 0; } if (t[0] == 1 && t[1] == 2) { cout << "Taro" << endl; return 0; } if ((total - cnt) % 2 == 1) cout << "Taro" << endl; else cout << "Hanako" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int N; bool visited[1005]; char a[1005][1005]; int cnt = 0, k = 0; int cc; int t[2]; void dfs(int pos) { if (visited[pos]) return; visited[pos] = true; cc++; for (int i = 0; i < N; i++) if (a[pos][i] == 'Y') dfs(i); } int main() { cin >> N; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { cin >> a[i][j]; if (a[i][j] == 'Y') cnt++; } } for (int i = 0; i < N; i++) { if (visited[i]) continue; k++; cc = 0; dfs(i); t[cc % 2]++; } cnt /= 2; int total = N * (N - 1) / 2; if (N % 2 == 1 || t[1] % 2 == 1) { if ((total - cnt) % 2 == 1) cout << "Taro" << endl; else cout << "Hanako" << endl; return 0; } if (t[0] == 0 && t[1] == 2) { if ((total - cnt) % 2 == 0) cout << "Taro" << endl; else cout << "Hanako" << endl; return 0; } if (t[0] == 1 && t[1] == 2) { cout << "Taro" << endl; return 0; } if ((total - cnt) % 2 == 1) cout << "Taro" << endl; else cout << "Hanako" << endl; return 0; }
replace
4
6
4
6
0
p01493
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <complex> #include <cstdio> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long ll; // common #define BR "\n" #define ALL(c) (c).begin(), (c).end() #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define EACH(it, o) for (auto it = (o).begin(); it != (o).end(); ++it) #define IN(l, v, r) ((l) <= (v) && (v) < (r)) // config // #undef NDEBUG // #define INF 1<<30 // #define EPS 1e-8 // const ll MOD =100000007; // debug #ifdef NDEBUG #define DUMP(x) #define DUMPLN(x) #define DEBUG(x) #define DEBUGLN(x) #define LINE() #define LINELN() #define CHECK(exp, act) #define STOP(e) #else #define DUMP(x) cerr << #x << " = " << (x) #define DUMPLN(x) DUMP(x) << endl #define DEBUG(x) DUMP(x) << LINE() << " " << __FILE__ #define DEBUGLN(x) DEBUG(x) << endl #define LINE() cerr << " (L" << __LINE__ << ")" #define LINELN() LINE() << endl #define CHECK(exp, act) \ if (exp != act) { \ DUMPLN(exp); \ DEBUGLN(act); \ } #define STOP(e) \ CHECK(e, true); \ if (!(e)) \ exit(1); #endif // output template <typename T> ostream &operator<<(ostream &os, const vector<T> &as) { REP(i, as.size()) { if (i != 0) os << " "; os << as[i]; } return os; } template <typename T> ostream &operator<<(ostream &os, const vector<vector<T>> &as) { REP(i, as.size()) { if (i != 0) os << endl; os << as[i]; } return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &ss) { for (auto a : ss) { if (a != ss.begin()) os << " "; os << a; } return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << p.first << " " << p.second; return os; } template <typename K, typename V> ostream &operator<<(ostream &os, const map<K, V> &m) { bool isF = true; for (auto &p : m) { if (!isF) os << endl; os << p; isF = false; } return os; } template <typename T1> ostream &operator<<(ostream &os, const tuple<T1> &t) { os << get<0>(t); return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const tuple<T1, T2> &t) { os << get<0>(t) << " " << get<1>(t); return os; } template <typename T1, typename T2, typename T3> ostream &operator<<(ostream &os, const tuple<T1, T2, T3> &t) { os << get<0>(t) << " " << get<1>(t) << " " << get<2>(t); return os; } template <typename T1, typename T2, typename T3, typename T4> ostream &operator<<(ostream &os, const tuple<T1, T2, T3, T4> &t) { os << get<0>(t) << " " << get<1>(t) << " " << get<2>(t) << " " << get<3>(t); return os; } template <typename T1, typename T2, typename T3, typename T4, typename T5> ostream &operator<<(ostream &os, const tuple<T1, T2, T3, T4, T5> &t) { os << get<0>(t) << " " << get<1>(t) << " " << get<2>(t) << " " << get<3>(t) << " " << get<4>(t); return os; } template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6> ostream &operator<<(ostream &os, const tuple<T1, T2, T3, T4, T5, T6> &t) { os << get<0>(t) << " " << get<1>(t) << " " << get<2>(t) << " " << get<3>(t) << " " << get<4>(t) << " " << get<5>(t); return os; } template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7> ostream &operator<<(ostream &os, const tuple<T1, T2, T3, T4, T5, T6, T7> &t) { os << get<0>(t) << " " << get<1>(t) << " " << get<2>(t) << " " << get<3>(t) << " " << get<4>(t) << " " << get<5>(t) << " " << get<6>(t); return os; } template <typename T, typename V> inline T mod(T v, V MOD) { return (v % MOD + MOD) % MOD; } struct UnionFind { vector<int> par, rank, ss; int size; UnionFind(int n) { REP(i, n) par.push_back(i); rank = vector<int>(n); ss = vector<int>(n, 1); size = n; } int root(int x) { if (par[x] == x) return x; return par[x] = root(par[x]); } bool same(int x, int y) { return root(x) == root(y); } void unite(int x, int y) { x = root(x); y = root(y); if (x == y) return; if (rank[x] > rank[y]) swap(x, y); par[x] = y; ss[y] += ss[x]; if (rank[x] == rank[y]) rank[x]++; size--; } int getS(int x) { return ss[root(x)]; } }; ll MOD = 1000000007; static const int M = 1050; int dp[M][M][2]; // 偶数連結成分の数,奇数連結成分の数,連結成分を変えないで開いてる辺 // mod2 class Main { public: int dfs(int en, int on, int ec) { if (en + on == 1) return dp[en][on][ec] = 1; bool win = false; // edge if (ec > 0) win |= !dfs(en, on, 0); // e,e -> e if (en >= 2) win |= !dfs(en - 1, on, 1 - ec); // e,o -> o if (en >= 1 && on >= 1) win |= !dfs(en - 1, on, 1 - ec); // o,o -> e if (on >= 2) win |= !dfs(en + 1, on - 2, ec); return dp[en][on][ec] = win; } void run() { int N; cin >> N; vector<string> es(N); REP(i, N) cin >> es[i]; UnionFind uf(N); REP(y, N) REP(x, N) if (es[y][x] == 'Y') uf.unite(y, x); map<int, int> ge; REP(y, N) ge[uf.root(y)] = uf.getS(y) * (uf.getS(y) - 1) / 2; REP(y, N) for (int x = y + 1; x < N; x++) if (es[y][x] == 'Y') ge[uf.root(y)]--; int en = 0, on = 0, ec = 0; EACH(it, ge) { if (uf.getS(it->first) % 2) on++; else en++; ec += it->second; ec %= 2; } fill_n(dp[0][0], M * M * 2, -1); cout << (dfs(en, on, ec) ? "Taro" : "Hanako") << endl; } }; int main() { ios::sync_with_stdio(false); Main().run(); return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <complex> #include <cstdio> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long ll; // common #define BR "\n" #define ALL(c) (c).begin(), (c).end() #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define EACH(it, o) for (auto it = (o).begin(); it != (o).end(); ++it) #define IN(l, v, r) ((l) <= (v) && (v) < (r)) // config // #undef NDEBUG // #define INF 1<<30 // #define EPS 1e-8 // const ll MOD =100000007; // debug #ifdef NDEBUG #define DUMP(x) #define DUMPLN(x) #define DEBUG(x) #define DEBUGLN(x) #define LINE() #define LINELN() #define CHECK(exp, act) #define STOP(e) #else #define DUMP(x) cerr << #x << " = " << (x) #define DUMPLN(x) DUMP(x) << endl #define DEBUG(x) DUMP(x) << LINE() << " " << __FILE__ #define DEBUGLN(x) DEBUG(x) << endl #define LINE() cerr << " (L" << __LINE__ << ")" #define LINELN() LINE() << endl #define CHECK(exp, act) \ if (exp != act) { \ DUMPLN(exp); \ DEBUGLN(act); \ } #define STOP(e) \ CHECK(e, true); \ if (!(e)) \ exit(1); #endif // output template <typename T> ostream &operator<<(ostream &os, const vector<T> &as) { REP(i, as.size()) { if (i != 0) os << " "; os << as[i]; } return os; } template <typename T> ostream &operator<<(ostream &os, const vector<vector<T>> &as) { REP(i, as.size()) { if (i != 0) os << endl; os << as[i]; } return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &ss) { for (auto a : ss) { if (a != ss.begin()) os << " "; os << a; } return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << p.first << " " << p.second; return os; } template <typename K, typename V> ostream &operator<<(ostream &os, const map<K, V> &m) { bool isF = true; for (auto &p : m) { if (!isF) os << endl; os << p; isF = false; } return os; } template <typename T1> ostream &operator<<(ostream &os, const tuple<T1> &t) { os << get<0>(t); return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const tuple<T1, T2> &t) { os << get<0>(t) << " " << get<1>(t); return os; } template <typename T1, typename T2, typename T3> ostream &operator<<(ostream &os, const tuple<T1, T2, T3> &t) { os << get<0>(t) << " " << get<1>(t) << " " << get<2>(t); return os; } template <typename T1, typename T2, typename T3, typename T4> ostream &operator<<(ostream &os, const tuple<T1, T2, T3, T4> &t) { os << get<0>(t) << " " << get<1>(t) << " " << get<2>(t) << " " << get<3>(t); return os; } template <typename T1, typename T2, typename T3, typename T4, typename T5> ostream &operator<<(ostream &os, const tuple<T1, T2, T3, T4, T5> &t) { os << get<0>(t) << " " << get<1>(t) << " " << get<2>(t) << " " << get<3>(t) << " " << get<4>(t); return os; } template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6> ostream &operator<<(ostream &os, const tuple<T1, T2, T3, T4, T5, T6> &t) { os << get<0>(t) << " " << get<1>(t) << " " << get<2>(t) << " " << get<3>(t) << " " << get<4>(t) << " " << get<5>(t); return os; } template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7> ostream &operator<<(ostream &os, const tuple<T1, T2, T3, T4, T5, T6, T7> &t) { os << get<0>(t) << " " << get<1>(t) << " " << get<2>(t) << " " << get<3>(t) << " " << get<4>(t) << " " << get<5>(t) << " " << get<6>(t); return os; } template <typename T, typename V> inline T mod(T v, V MOD) { return (v % MOD + MOD) % MOD; } struct UnionFind { vector<int> par, rank, ss; int size; UnionFind(int n) { REP(i, n) par.push_back(i); rank = vector<int>(n); ss = vector<int>(n, 1); size = n; } int root(int x) { if (par[x] == x) return x; return par[x] = root(par[x]); } bool same(int x, int y) { return root(x) == root(y); } void unite(int x, int y) { x = root(x); y = root(y); if (x == y) return; if (rank[x] > rank[y]) swap(x, y); par[x] = y; ss[y] += ss[x]; if (rank[x] == rank[y]) rank[x]++; size--; } int getS(int x) { return ss[root(x)]; } }; ll MOD = 1000000007; static const int M = 1050; int dp[M][M][2]; // 偶数連結成分の数,奇数連結成分の数,連結成分を変えないで開いてる辺 // mod2 class Main { public: int dfs(int en, int on, int ec) { if (dp[en][on][ec] != -1) return dp[en][on][ec]; if (en + on == 1) return dp[en][on][ec] = 1; bool win = false; // edge if (ec > 0) win |= !dfs(en, on, 0); // e,e -> e if (en >= 2) win |= !dfs(en - 1, on, 1 - ec); // e,o -> o if (en >= 1 && on >= 1) win |= !dfs(en - 1, on, 1 - ec); // o,o -> e if (on >= 2) win |= !dfs(en + 1, on - 2, ec); return dp[en][on][ec] = win; } void run() { int N; cin >> N; vector<string> es(N); REP(i, N) cin >> es[i]; UnionFind uf(N); REP(y, N) REP(x, N) if (es[y][x] == 'Y') uf.unite(y, x); map<int, int> ge; REP(y, N) ge[uf.root(y)] = uf.getS(y) * (uf.getS(y) - 1) / 2; REP(y, N) for (int x = y + 1; x < N; x++) if (es[y][x] == 'Y') ge[uf.root(y)]--; int en = 0, on = 0, ec = 0; EACH(it, ge) { if (uf.getS(it->first) % 2) on++; else en++; ec += it->second; ec %= 2; } fill_n(dp[0][0], M * M * 2, -1); cout << (dfs(en, on, ec) ? "Taro" : "Hanako") << endl; } }; int main() { ios::sync_with_stdio(false); Main().run(); return 0; }
insert
190
190
190
192
TLE
p01493
C++
Memory Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, x, y) for (int i = (x); i < (y); ++i) #define debug(x) #x << "=" << (x) #ifdef DEBUG #define _GLIBCXX_DEBUG #define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl #else #define print(x) #endif const int inf = 1e9; const int64_t inf64 = 1e18; const double eps = 1e-9; template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "["; for (const auto &v : vec) { os << v << ","; } os << "]"; return os; } void solve() { int v; cin >> v; vector<vector<char>> a(v, vector<char>(v)); rep(i, 0, v) rep(j, 0, v) cin >> a[i][j]; int x = 0, y = 0, f = 0; { vector<bool> used(v); rep(i, 0, v) { if (used[i]) continue; used[i] = true; vector<int> vi; queue<int> que; que.push(i); while (!que.empty()) { int p = que.front(); que.pop(); vi.push_back(p); rep(j, 0, v) { if (used[j] or a[p][j] == 'N') continue; que.push(j); used[j] = true; } } if (vi.size() % 2 == 0) ++x; else ++y; int cnt = 0; rep(j, 0, vi.size()) rep(k, j + 1, vi.size()) if (a[vi[j]][vi[k]] == 'Y')++ cnt; f = (f + vi.size() * (vi.size() - 1) / 2 - cnt + 2) % 2; } } vector<vector<vector<bool>>> dp(v + 1), done(v + 1); rep(i, 0, v + 1) { dp[i].assign(v - i + 1, vector<bool>(2)); done[i].assign(v - i + 1, vector<bool>(2)); } function<bool(int, int, int)> rec = [&](int x, int y, int f) { if (done[x][y][f]) return dp[x][y][f]; done[x][y][f] = true; if ((x == 1 and y == 0) or (x == 0 and y == 1)) return dp[x][y][f] = true; bool res = false; if (f == 1) res |= !rec(x, y, 1 - f); if (x >= 2) res |= !rec(x - 1, y, 1 - f); if (y >= 2) res |= !rec(x + 1, y - 2, f); if (x >= 1 and y >= 1) res |= !rec(x - 1, y, 1 - f); return dp[x][y][f] = res; }; if (rec(x, y, f)) cout << "Taro" << endl; else cout << "Hanako" << endl; } int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(10); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, x, y) for (int i = (x); i < (y); ++i) #define debug(x) #x << "=" << (x) #ifdef DEBUG #define _GLIBCXX_DEBUG #define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl #else #define print(x) #endif const int inf = 1e9; const int64_t inf64 = 1e18; const double eps = 1e-9; template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "["; for (const auto &v : vec) { os << v << ","; } os << "]"; return os; } void solve() { int v; cin >> v; vector<vector<char>> a(v, vector<char>(v)); rep(i, 0, v) rep(j, 0, v) cin >> a[i][j]; int x = 0, y = 0, f = 0; { vector<bool> used(v); rep(i, 0, v) { if (used[i]) continue; used[i] = true; vector<int> vi; queue<int> que; que.push(i); while (!que.empty()) { int p = que.front(); que.pop(); vi.push_back(p); rep(j, 0, v) { if (used[j] or a[p][j] == 'N') continue; que.push(j); used[j] = true; } } if (vi.size() % 2 == 0) ++x; else ++y; int cnt = 0; rep(j, 0, vi.size()) rep(k, j + 1, vi.size()) if (a[vi[j]][vi[k]] == 'Y')++ cnt; f = (f + vi.size() * (vi.size() - 1) / 2 - cnt + 2) % 2; } } static bool dp[1001][1001][2] = {}, done[1001][1001][2] = {}; function<bool(int, int, int)> rec = [&](int x, int y, int f) { if (done[x][y][f]) return dp[x][y][f]; done[x][y][f] = true; if ((x == 1 and y == 0) or (x == 0 and y == 1)) return dp[x][y][f] = true; bool res = false; if (f == 1) res |= !rec(x, y, 1 - f); if (x >= 2) res |= !rec(x - 1, y, 1 - f); if (y >= 2) res |= !rec(x + 1, y - 2, f); if (x >= 1 and y >= 1) res |= !rec(x - 1, y, 1 - f); return dp[x][y][f] = res; }; if (rec(x, y, f)) cout << "Taro" << endl; else cout << "Hanako" << endl; } int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(10); solve(); return 0; }
replace
64
69
64
65
MLE
p01497
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep1(i, n) for (int i = 1; i <= (int)(n); i++) #define all(c) c.begin(), c.end() #define pb push_back #define fs first #define sc second #define show(x) cout << #x << " = " << x << endl #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<vi> vv; typedef pair<int, ll> P; int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; bool is(int x, int y) { return 0 <= x && x < 4 && 0 <= y && y < 4; } inline ll toll(vv b) { ll x = 0; rep(i, 4) rep(j, 4) x = x * 8 + b[i][j]; return x; } inline vv tovv(ll x) { vv b(4, vi(4, 0)); rep(i, 4) rep(j, 4) { b[3 - i][3 - j] = x % 8; x /= 8; } return b; } int main() { ll s = 0; rep(i, 4) rep(j, 4) { ll x; cin >> x; s = s * 8 + x; } queue<P> que; que.push(P(0, s)); while (!que.empty()) { P p = que.front(); que.pop(); if (p.fs >= 5) { puts("-1"); return 0; } vv b = tovv(p.sc); rep(i, 4) rep(j, 4) { vv nb = b; if (nb[i][j] == 0) continue; nb[i][j]++; if (nb[i][j] == 5) { vv waters; rep(d, 4) waters.pb(vi{i, j, d}); while (!waters.empty()) { vv nwaters; for (vi w : waters) { int x = w[0], y = w[1], d = w[2]; int nx = x + dx[d], ny = y + dy[d]; if (!is(nx, ny)) continue; if (nb[nx][ny] == 0) { nwaters.pb(vi{nx, ny, d}); continue; } else { nb[nx][ny]++; if (nb[nx][ny] == 5) { rep(di, 4) nwaters.pb(vi{nx, ny, di}); } } } waters = nwaters; rep(e, 4) rep(f, 4) if (nb[e][f] >= 5) nb[e][f] = 0; } } ll nx = toll(nb); if (nx == 0) { cout << p.fs + 1 << endl; return 0; } que.push(P(p.fs + 1, nx)); } } assert(0); }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep1(i, n) for (int i = 1; i <= (int)(n); i++) #define all(c) c.begin(), c.end() #define pb push_back #define fs first #define sc second #define show(x) cout << #x << " = " << x << endl #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<vi> vv; typedef pair<int, ll> P; int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; bool is(int x, int y) { return 0 <= x && x < 4 && 0 <= y && y < 4; } inline ll toll(vv b) { ll x = 0; rep(i, 4) rep(j, 4) x = x * 8 + b[i][j]; return x; } inline vv tovv(ll x) { vv b(4, vi(4, 0)); rep(i, 4) rep(j, 4) { b[3 - i][3 - j] = x % 8; x /= 8; } return b; } int main() { ll s = 0; rep(i, 4) rep(j, 4) { ll x; cin >> x; s = s * 8 + x; } if (s == 0) { puts("0"); return 0; } queue<P> que; que.push(P(0, s)); while (!que.empty()) { P p = que.front(); que.pop(); if (p.fs >= 5) { puts("-1"); return 0; } vv b = tovv(p.sc); rep(i, 4) rep(j, 4) { vv nb = b; if (nb[i][j] == 0) continue; nb[i][j]++; if (nb[i][j] == 5) { vv waters; rep(d, 4) waters.pb(vi{i, j, d}); while (!waters.empty()) { vv nwaters; for (vi w : waters) { int x = w[0], y = w[1], d = w[2]; int nx = x + dx[d], ny = y + dy[d]; if (!is(nx, ny)) continue; if (nb[nx][ny] == 0) { nwaters.pb(vi{nx, ny, d}); continue; } else { nb[nx][ny]++; if (nb[nx][ny] == 5) { rep(di, 4) nwaters.pb(vi{nx, ny, di}); } } } waters = nwaters; rep(e, 4) rep(f, 4) if (nb[e][f] >= 5) nb[e][f] = 0; } } ll nx = toll(nb); if (nx == 0) { cout << p.fs + 1 << endl; return 0; } que.push(P(p.fs + 1, nx)); } } assert(0); }
insert
36
36
36
40
0
p01498
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> P; #define each(i, a) for (auto &&i : a) #define FOR(i, a, b) for (ll i = (a), __last_##i = (b); i < __last_##i; i++) #define RFOR(i, a, b) for (ll i = (b)-1, __last_##i = (a); i >= __last_##i; i--) #define REP(i, n) FOR(i, 0, n) #define RREP(i, n) RFOR(i, 0, n) #define __GET_MACRO3(_1, _2, _3, NAME, ...) NAME #define rep(...) __GET_MACRO3(__VA_ARGS__, FOR, REP)(__VA_ARGS__) #define rrep(...) __GET_MACRO3(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__) #define pb push_back #define all(a) (a).begin(), (a).end() #define chmin(x, v) x = min(x, v) #define chmax(x, v) x = max(x, v) const ll linf = 1e18; const int inf = 1e9; const double eps = 1e-12; const double pi = acos(-1); template <typename T> istream &operator>>(istream &is, vector<T> &vec) { each(x, vec) is >> x; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { rep(i, vec.size()) { if (i) os << " "; os << vec[i]; } return os; } template <typename T> ostream &operator<<(ostream &os, const vector<vector<T>> &vec) { rep(i, vec.size()) { if (i) os << endl; os << vec[i]; } return os; } class UnionFind { vector<int> par, h; public: UnionFind(int size) { par.assign(size, 0); h.assign(size, 0); REP(i, size) par[i] = i; } void unite(int u, int v) { u = root(u), v = root(v); if (u == v) return; if (h[u] > h[v]) { par[v] = u; } else if (h[u] < h[v]) { par[u] = v; } else { ++h[u]; par[v] = u; } } bool isUnited(int u, int v) { return root(u) == root(v); } int root(int v) { if (par[v] == v) return v; return par[v] = root(par[v]); } }; int main() { ios::sync_with_stdio(false); cin.tie(0); ll n, h, w; cin >> n >> h >> w; vector<ll> X(n), Y(n); rep(i, n) { cin >> X[i] >> Y[i]; --X[i], --Y[i]; } vector<vector<ll>> g1(h), g2(w); rep(i, n) { g1[Y[i]].pb(i); g2[X[i]].pb(i); } UnionFind uf(n); rep(i, h) { rep(j, 1, g1[i].size()) { uf.unite(g1[i][j - 1], g1[i][j]); } } rep(i, w) { rep(j, 1, g2[i].size()) { uf.unite(g2[i][j - 1], g2[i][j]); } } set<ll> s; rep(i, n) s.insert(uf.root(i)); ll ans = linf; if (s.size() == 1) { ans = n - 1; } else { ans = n + s.size() - 1; set<ll> sl, sr, su, sd; rep(i, n) { ll id = uf.root(i); if (X[i] == 0) sl.insert(id); if (X[i] == w - 1) sr.insert(id); if (Y[i] == 0) su.insert(id); if (Y[i] == h - 1) sd.insert(id); } ans -= max(max(sl.size(), sr.size()), max(su.size(), sd.size())); } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> P; #define each(i, a) for (auto &&i : a) #define FOR(i, a, b) for (ll i = (a), __last_##i = (b); i < __last_##i; i++) #define RFOR(i, a, b) for (ll i = (b)-1, __last_##i = (a); i >= __last_##i; i--) #define REP(i, n) FOR(i, 0, n) #define RREP(i, n) RFOR(i, 0, n) #define __GET_MACRO3(_1, _2, _3, NAME, ...) NAME #define rep(...) __GET_MACRO3(__VA_ARGS__, FOR, REP)(__VA_ARGS__) #define rrep(...) __GET_MACRO3(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__) #define pb push_back #define all(a) (a).begin(), (a).end() #define chmin(x, v) x = min(x, v) #define chmax(x, v) x = max(x, v) const ll linf = 1e18; const int inf = 1e9; const double eps = 1e-12; const double pi = acos(-1); template <typename T> istream &operator>>(istream &is, vector<T> &vec) { each(x, vec) is >> x; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { rep(i, vec.size()) { if (i) os << " "; os << vec[i]; } return os; } template <typename T> ostream &operator<<(ostream &os, const vector<vector<T>> &vec) { rep(i, vec.size()) { if (i) os << endl; os << vec[i]; } return os; } class UnionFind { vector<int> par, h; public: UnionFind(int size) { par.assign(size, 0); h.assign(size, 0); REP(i, size) par[i] = i; } void unite(int u, int v) { u = root(u), v = root(v); if (u == v) return; if (h[u] > h[v]) { par[v] = u; } else if (h[u] < h[v]) { par[u] = v; } else { ++h[u]; par[v] = u; } } bool isUnited(int u, int v) { return root(u) == root(v); } int root(int v) { if (par[v] == v) return v; return par[v] = root(par[v]); } }; int main() { ios::sync_with_stdio(false); cin.tie(0); ll n, h, w; cin >> n >> w >> h; vector<ll> X(n), Y(n); rep(i, n) { cin >> X[i] >> Y[i]; --X[i], --Y[i]; } vector<vector<ll>> g1(h), g2(w); rep(i, n) { g1[Y[i]].pb(i); g2[X[i]].pb(i); } UnionFind uf(n); rep(i, h) { rep(j, 1, g1[i].size()) { uf.unite(g1[i][j - 1], g1[i][j]); } } rep(i, w) { rep(j, 1, g2[i].size()) { uf.unite(g2[i][j - 1], g2[i][j]); } } set<ll> s; rep(i, n) s.insert(uf.root(i)); ll ans = linf; if (s.size() == 1) { ans = n - 1; } else { ans = n + s.size() - 1; set<ll> sl, sr, su, sd; rep(i, n) { ll id = uf.root(i); if (X[i] == 0) sl.insert(id); if (X[i] == w - 1) sr.insert(id); if (Y[i] == 0) su.insert(id); if (Y[i] == h - 1) sd.insert(id); } ans -= max(max(sl.size(), sr.size()), max(su.size(), sd.size())); } cout << ans << endl; }
replace
80
81
80
81
0
p01498
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define FOR(i, k, n) for (int i = (k); i < (int)(n); i++) #define REP(i, n) FOR(i, 0, n) #define ALL(a) a.begin(), a.end() #define MS(m, v) memset(m, v, sizeof(m)) #define D10 fixed << setprecision(10) typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef vector<string> vs; typedef pair<int, int> pii; const int MOD = 1000000007; const int INF = MOD + 1; const ld EPS = 1e-12; template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); } template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); } /*--------------------template--------------------*/ struct union_find { vector<int> par; union_find(int n) { REP(i, n) par.push_back(i); } int find(int x) { return par[x] == x ? x : par[x] = find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; par[y] = x; if (find(x) != find(y)) par[find(y)] = x; } int count() { int cnt = 0; REP(i, par.size()) if (par[i] == i) cnt++; return cnt; } bool same(int x, int y) { return find(x) == find(y); } }; int main() { int n, w, h; cin >> n >> w >> h; vector<vi> x(h), y(w); union_find uf(n); REP(i, n) { int a, b; cin >> a >> b; a--; b--; x[a].push_back(i), y[b].push_back(i); } REP(i, h) REP(j, x[i].size() - 1) uf.unite(x[i][j], x[i][j + 1]); REP(i, w) REP(j, y[i].size() - 1) uf.unite(y[i][j], y[i][j + 1]); ll cnt = uf.count(); ll ans = n - cnt; if (cnt > 1) { ans += cnt; ans += cnt - 1; if (x[0].size() || x[h - 1].size() || y[0].size() || y[w - 1].size()) ans--; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define FOR(i, k, n) for (int i = (k); i < (int)(n); i++) #define REP(i, n) FOR(i, 0, n) #define ALL(a) a.begin(), a.end() #define MS(m, v) memset(m, v, sizeof(m)) #define D10 fixed << setprecision(10) typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef vector<string> vs; typedef pair<int, int> pii; const int MOD = 1000000007; const int INF = MOD + 1; const ld EPS = 1e-12; template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); } template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); } /*--------------------template--------------------*/ struct union_find { vector<int> par; union_find(int n) { REP(i, n) par.push_back(i); } int find(int x) { return par[x] == x ? x : par[x] = find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; par[y] = x; if (find(x) != find(y)) par[find(y)] = x; } int count() { int cnt = 0; REP(i, par.size()) if (par[i] == i) cnt++; return cnt; } bool same(int x, int y) { return find(x) == find(y); } }; int main() { int n, w, h; cin >> n >> w >> h; vector<vi> x(h), y(w); union_find uf(n); REP(i, n) { int a, b; cin >> b >> a; a--; b--; x[a].push_back(i), y[b].push_back(i); } REP(i, h) REP(j, x[i].size() - 1) uf.unite(x[i][j], x[i][j + 1]); REP(i, w) REP(j, y[i].size() - 1) uf.unite(y[i][j], y[i][j + 1]); ll cnt = uf.count(); ll ans = n - cnt; if (cnt > 1) { ans += cnt; ans += cnt - 1; if (x[0].size() || x[h - 1].size() || y[0].size() || y[w - 1].size()) ans--; } cout << ans << endl; return 0; }
replace
47
48
47
48
0
p01499
C++
Runtime Error
#include <algorithm> #include <assert.h> #include <deque> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stdio.h> #include <string.h> #include <string> #include <vector> using namespace std; typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; static const double EPS = 1e-9; static const double PI = acos(-1.0); #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define FOR(i, s, n) for (int i = (s); i < (int)(n); i++) #define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++) #define FORIT(it, c) \ for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++) #define MEMSET(v, h) memset((v), h, sizeof(v)) int n, t; int level[10010]; const ll MOD = 1e+9 + 7; int main() { while (scanf("%d %d", &n, &t) > 0) { REP(i, n) { scanf("%d", &level[i]); } sort(level, level + n); ll ans = 1; deque<int> prev; REP(i, n) { while (!prev.empty() && prev.front() < level[i] - t) { prev.pop_front(); } ans = (ans * (prev.size() + 1)) % MOD; prev.push_back(level[i]); } printf("%lld\n", ans); } }
#include <algorithm> #include <assert.h> #include <deque> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stdio.h> #include <string.h> #include <string> #include <vector> using namespace std; typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; static const double EPS = 1e-9; static const double PI = acos(-1.0); #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define FOR(i, s, n) for (int i = (s); i < (int)(n); i++) #define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++) #define FORIT(it, c) \ for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++) #define MEMSET(v, h) memset((v), h, sizeof(v)) int n, t; int level[100010]; const ll MOD = 1e+9 + 7; int main() { while (scanf("%d %d", &n, &t) > 0) { REP(i, n) { scanf("%d", &level[i]); } sort(level, level + n); ll ans = 1; deque<int> prev; REP(i, n) { while (!prev.empty() && prev.front() < level[i] - t) { prev.pop_front(); } ans = (ans * (prev.size() + 1)) % MOD; prev.push_back(level[i]); } printf("%lld\n", ans); } }
replace
28
29
28
29
0
p01499
C++
Runtime Error
#include <algorithm> #include <cassert> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < (int)n; i++) #define each(i, c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); i++) #define pb push_back #define mp make_pair #define all(c) c.begin(), c.end() #define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl typedef vector<int> vi; typedef pair<int, int> pi; typedef long long ll; const int inf = (int)1e9; const double EPS = 1e-9, INF = 1e12; const int mod = (int)1e9 + 7; int n, t, d[10000]; int main() { cin >> n >> t; rep(i, n) cin >> d[i]; sort(d, d + n); ll ans = 1; for (int i = 0, j = 0; j < n; j++) { while (i < j && d[i] + t < d[j]) i++; ans = ans * (j - i + 1) % mod; } cout << ans << endl; return 0; }
#include <algorithm> #include <cassert> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < (int)n; i++) #define each(i, c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); i++) #define pb push_back #define mp make_pair #define all(c) c.begin(), c.end() #define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl typedef vector<int> vi; typedef pair<int, int> pi; typedef long long ll; const int inf = (int)1e9; const double EPS = 1e-9, INF = 1e12; const int mod = (int)1e9 + 7; int n, t, d[100000]; int main() { cin >> n >> t; rep(i, n) cin >> d[i]; sort(d, d + n); ll ans = 1; for (int i = 0, j = 0; j < n; j++) { while (i < j && d[i] + t < d[j]) i++; ans = ans * (j - i + 1) % mod; } cout << ans << endl; return 0; }
replace
28
29
28
29
0
p01499
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <valarray> #include <vector> using namespace std; #define REP(i, n) for (int i = 0; i < (int)n; ++i) #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define ALL(c) (c).begin(), (c).end() #define chmax(a, b) (a < b ? (a = b, 1) : 0) #define chmin(a, b) (a > b ? (a = b, 1) : 0) #define valid(y, x, h, w) (0 <= y && y < h && 0 <= x && x < w) typedef long long ll; typedef pair<int, int> pii; const int INF = 1 << 29; const double PI = acos(-1); const double EPS = 1e-8; int a[10000]; int main() { int n, t; cin >> n >> t; REP(i, n) cin >> a[i]; sort(a, a + n); ll ans = 1; const ll mod = 1e9 + 7; REP(i, n) { ll num = upper_bound(a, a + n, a[i] + t) - (a + i); ans = ans * num % mod; } cout << ans << endl; }
#include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <valarray> #include <vector> using namespace std; #define REP(i, n) for (int i = 0; i < (int)n; ++i) #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define ALL(c) (c).begin(), (c).end() #define chmax(a, b) (a < b ? (a = b, 1) : 0) #define chmin(a, b) (a > b ? (a = b, 1) : 0) #define valid(y, x, h, w) (0 <= y && y < h && 0 <= x && x < w) typedef long long ll; typedef pair<int, int> pii; const int INF = 1 << 29; const double PI = acos(-1); const double EPS = 1e-8; int a[1000000]; int main() { int n, t; cin >> n >> t; REP(i, n) cin >> a[i]; sort(a, a + n); ll ans = 1; const ll mod = 1e9 + 7; REP(i, n) { ll num = upper_bound(a, a + n, a[i] + t) - (a + i); ans = ans * num % mod; } cout << ans << endl; }
replace
34
35
34
35
0
p01502
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < int(n); ++i) bool used[100]; long long c[101][101], R; int n; long long solve(int k, long long res) { if (res < 0) return 1e15; vector<pair<int, int>> next; rep(i, n) if (!used[i]) next.push_back(make_pair(c[k][i], i)); if (next.size() == 0u) return 0; sort(next.begin(), next.end()); rep(i, next.size()) { used[next[i].second] = true; res = min(solve(next[i].second, res - next[i].first) + next[i].first, res); used[next[i].second] = false; } return res; } int main() { cin >> n; rep(i, n) rep(j, n) cin >> c[i][j]; long long res = 0; rep(i, n) rep(j, i) { long long m = min(c[i][j], c[j][i]); res += m; c[i][j] -= m; c[j][i] -= m; } cout << solve(100, 1e15) + res << endl; return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < int(n); ++i) bool used[100]; long long c[101][101], R; int n; long long solve(int k, long long res) { if (res <= 0) return 1e15; vector<pair<int, int>> next; rep(i, n) if (!used[i]) next.push_back(make_pair(c[k][i], i)); if (next.size() == 0u) return 0; sort(next.begin(), next.end()); rep(i, next.size()) { used[next[i].second] = true; res = min(solve(next[i].second, res - next[i].first) + next[i].first, res); used[next[i].second] = false; } return res; } int main() { cin >> n; rep(i, n) rep(j, n) cin >> c[i][j]; long long res = 0; rep(i, n) rep(j, i) { long long m = min(c[i][j], c[j][i]); res += m; c[i][j] -= m; c[j][i] -= m; } cout << solve(100, 1e15) + res << endl; return 0; }
replace
13
14
13
14
TLE
p01504
C++
Runtime Error
#include <bits/stdc++.h> #define int long long #define N 100010 using namespace std; const int INF = 1LL << 55; const int mod = (1e9) + 7; const double EPS = 1e-8; const double PI = 6.0 * asin(0.5); template <class T> T Max(T &a, T b) { return a = max(a, b); } template <class T> T Min(T &a, T b) { return a = min(a, b); } #define rank Asdfiasofsad class UF { public: int V; vector<int> par, rank, base; UF() {} UF(int V) : V(V), par(V), rank(V, 0), base(V, 0) { for (int i = 0; i < V; i++) par[i] = i; } int find(int x) { assert(x < V); if (par[x] == x) return x; return par[x] = find(par[x]); } void unite(int x, int y) { x = find(x), y = find(y); if (x == y) return; if (rank[x] < rank[y]) par[x] = y, base[y] |= base[x]; else { par[y] = x; base[x] |= base[y]; if (rank[x] == rank[y]) rank[x]++; } } bool same(int x, int y) { return find(x) == find(y); } }; int n, k, t; typedef pair<int, int> P; typedef pair<int, P> PP; vector<int> T; vector<PP> edge; int solve() { sort(edge.begin(), edge.end()); int sum = 0; for (int i = 0; i < n - 1; i++) sum += -edge[i].first; UF uf(n); for (int i = 0; i < t; i++) uf.base[T[i]] = 1; int numBase = t, numNode = n; for (int i = 0; i < n - 1; i++) { int a = edge[i].second.first; int b = edge[i].second.second; int c = -edge[i].first; a = uf.find(a); b = uf.find(b); if (uf.same(a, b)) continue; if (numBase == k + 1 && uf.base[a] && uf.base[b]) continue; if (numNode == k + 1) continue; if (uf.base[a] && uf.base[b]) numBase--; numNode--; sum -= c; uf.unite(a, b); } return sum; } signed main() { int numCase = 0; while (1) { cin >> n >> t >> k; if (n == 0 && k == 0 && t == 0) break; cout << "Case " << ++numCase << ": "; edge.resize(n - 1); for (int i = 0; i < n - 1; i++) { int a, b, c; cin >> a >> b >> c; a--, b--; edge[i] = PP(-c, P(a, b)); } T.resize(t); for (int i = 0; i < t; i++) cin >> T[i]; cout << solve() << endl; } return 0; }
#include <bits/stdc++.h> #define int long long #define N 100010 using namespace std; const int INF = 1LL << 55; const int mod = (1e9) + 7; const double EPS = 1e-8; const double PI = 6.0 * asin(0.5); template <class T> T Max(T &a, T b) { return a = max(a, b); } template <class T> T Min(T &a, T b) { return a = min(a, b); } #define rank Asdfiasofsad class UF { public: int V; vector<int> par, rank, base; UF() {} UF(int V) : V(V), par(V), rank(V, 0), base(V, 0) { for (int i = 0; i < V; i++) par[i] = i; } int find(int x) { assert(x < V); if (par[x] == x) return x; return par[x] = find(par[x]); } void unite(int x, int y) { x = find(x), y = find(y); if (x == y) return; if (rank[x] < rank[y]) par[x] = y, base[y] |= base[x]; else { par[y] = x; base[x] |= base[y]; if (rank[x] == rank[y]) rank[x]++; } } bool same(int x, int y) { return find(x) == find(y); } }; int n, k, t; typedef pair<int, int> P; typedef pair<int, P> PP; vector<int> T; vector<PP> edge; int solve() { sort(edge.begin(), edge.end()); int sum = 0; for (int i = 0; i < n - 1; i++) sum += -edge[i].first; UF uf(n); for (int i = 0; i < t; i++) uf.base[T[i]] = 1; int numBase = t, numNode = n; for (int i = 0; i < n - 1; i++) { int a = edge[i].second.first; int b = edge[i].second.second; int c = -edge[i].first; a = uf.find(a); b = uf.find(b); if (uf.same(a, b)) continue; if (numBase == k + 1 && uf.base[a] && uf.base[b]) continue; if (numNode == k + 1) continue; if (uf.base[a] && uf.base[b]) numBase--; numNode--; sum -= c; uf.unite(a, b); } return sum; } signed main() { int numCase = 0; while (1) { cin >> n >> t >> k; if (n == 0 && k == 0 && t == 0) break; cout << "Case " << ++numCase << ": "; edge.resize(n - 1); for (int i = 0; i < n - 1; i++) { int a, b, c; cin >> a >> b >> c; a--, b--; edge[i] = PP(-c, P(a, b)); } T.resize(t); for (int i = 0; i < t; i++) cin >> T[i], T[i]--; cout << solve() << endl; } return 0; }
replace
101
102
101
102
0
p01504
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; struct unionfind { vector<int> parent; vector<int> size; void init(int n) { size.clear(); parent.clear(); size.resize(n); parent.resize(n); for (int i = 0; i < n; i++) { parent[i] = i; size[i] = 1; } } int find(int x) { if (parent[x] == x) return x; else parent[x] = find(parent[x]); } bool same(int x, int y) { return (find(x) == find(y)); } int getSize(int x) { return size[find(x)]; } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; parent[x] = y; size[y] += size[x]; } }; struct edge { int from, to, cost; bool operator<(const edge &e) const { return cost < e.cost; } }; int n, t, k; unionfind uf; int main() { for (int tc = 1;; tc++) { cin >> n >> t >> k; if (n == 0 && t == 0 && k == 0) break; vector<edge> E; uf.init(n); for (int i = 0; i < n - 1; i++) { int a, b, c; cin >> a >> b >> c; a--, b--; E.push_back((edge){a, b, -c}); uf.unite(a, b); } assert(uf.getSize(0) == n); for (int i = 0; i < t; i++) { int a; cin >> a; a--; E.push_back((edge){a, n, -20000}); } sort(E.begin(), E.end()); uf.init(n + 1); vector<edge> tmp; for (int i = 0; i < (int)E.size(); i++) { edge e = E[i]; if (uf.same(e.from, e.to)) { tmp.push_back(e); } else { uf.unite(e.from, e.to); } } reverse(tmp.begin(), tmp.end()); int ans = 0; for (int i = 0; i < min(k, (int)tmp.size()); i++) { ans -= tmp[i].cost; } cout << "Case " << tc << ": " << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; struct unionfind { vector<int> parent; vector<int> size; void init(int n) { size.clear(); parent.clear(); size.resize(n); parent.resize(n); for (int i = 0; i < n; i++) { parent[i] = i; size[i] = 1; } } int find(int x) { if (parent[x] == x) return x; else return parent[x] = find(parent[x]); } bool same(int x, int y) { return (find(x) == find(y)); } int getSize(int x) { return size[find(x)]; } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; parent[x] = y; size[y] += size[x]; } }; struct edge { int from, to, cost; bool operator<(const edge &e) const { return cost < e.cost; } }; int n, t, k; unionfind uf; int main() { for (int tc = 1;; tc++) { cin >> n >> t >> k; if (n == 0 && t == 0 && k == 0) break; vector<edge> E; uf.init(n); for (int i = 0; i < n - 1; i++) { int a, b, c; cin >> a >> b >> c; a--, b--; E.push_back((edge){a, b, -c}); uf.unite(a, b); } assert(uf.getSize(0) == n); for (int i = 0; i < t; i++) { int a; cin >> a; a--; E.push_back((edge){a, n, -20000}); } sort(E.begin(), E.end()); uf.init(n + 1); vector<edge> tmp; for (int i = 0; i < (int)E.size(); i++) { edge e = E[i]; if (uf.same(e.from, e.to)) { tmp.push_back(e); } else { uf.unite(e.from, e.to); } } reverse(tmp.begin(), tmp.end()); int ans = 0; for (int i = 0; i < min(k, (int)tmp.size()); i++) { ans -= tmp[i].cost; } cout << "Case " << tc << ": " << ans << endl; } return 0; }
replace
23
24
23
24
-11
p01504
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define INF 1000000007 using namespace std; typedef long long ll; typedef pair<int, int> P; struct diameter { static const int MAX = 10000; vector<int> G[MAX]; int n; int d[MAX]; bool vis[MAX]; int d2[2][MAX]; void bfs(int s) { fill(d, d + n, INF); queue<int> que; que.push(s); d[s] = 0; int v; while (que.size()) { v = que.front(); que.pop(); for (int i = 0; i < G[v].size(); i++) { int e = G[v][i]; if (d[e] == INF) { d[e] = d[v] + 1; que.push(e); } } } } P diameter_points(int s = 0) { bfs(s); int maxv = 0; int tgt = 0; for (int i = 0; i < n; i++) { if (d[i] == INF) continue; if (maxv < d[i]) { maxv = d[i]; tgt = i; } } bfs(tgt); maxv = 0; int v = 0; for (int i = 0; i < n; i++) { if (d[i] == INF) continue; if (maxv < d[i]) { maxv = d[i]; v = i; } } return P(tgt, v); } vector<int> middle_points() { P p = diameter_points(); bfs(p.first); for (int i = 0; i < n; i++) { d2[0][i] = d[i]; } bfs(p.second); for (int i = 0; i < n; i++) { d2[1][i] = d[i]; } vector<int> cid; if (d2[0][p.second] % 2 == 0) { for (int i = 0; i < n; i++) { if (d2[0][p.second] == d2[0][i] * 2 && d2[1][p.first] == d2[1][i] * 2) { cid.push_back(i); } } } else { for (int i = 0; i < n; i++) { if (d2[0][p.second] == (d2[0][i] + d2[1][i]) && abs(d2[0][i] - d2[1][i]) == 1) { cid.push_back(i); } } } return cid; } }; struct edge { int t, c; edge() {} edge(int tt, int cc) { t = tt; c = cc; } }; diameter di; vector<edge> G[10001]; int n, t, k; bool flag[10001]; int siz[10001]; int *dp[10001][2]; int tmp[2][10001]; int dfs(int v, int p) { int ans = 1; for (int i = 0; i < G[v].size(); i++) { int nv = G[v][i].t; if (nv != p) { ans += dfs(nv, v); } } return (siz[v] = ans); } void func(int v, int p) { free(dp[v][0]); free(dp[v][1]); dp[v][0] = new int[siz[v] + 1]; dp[v][1] = new int[siz[v] + 1]; if (flag[v]) { dp[v][0][1] = INF; dp[v][1][1] = 0; } else { dp[v][1][1] = INF; dp[v][0][1] = 0; } int ns = 1; for (int i = 0; i < G[v].size(); i++) { int nv = G[v][i].t; int nc = G[v][i].c; if (nv != p) { func(nv, v); for (int j = 0; j <= ns + siz[nv]; j++) { tmp[0][j] = INF; tmp[1][j] = INF; } for (int i = 1; i <= ns; i++) { for (int j = 0; j <= siz[nv]; j++) { tmp[0][i + j] = min(tmp[0][i + j], dp[v][0][i] + dp[nv][1][j] + nc); tmp[1][i + j] = min(tmp[1][i + j], dp[v][1][i] + dp[nv][1][j] + nc); tmp[0][i + j - 1] = min(tmp[0][i + j - 1], dp[v][0][i] + dp[nv][0][j]); tmp[1][i + j - 1] = min(tmp[1][i + j - 1], dp[v][1][i] + dp[nv][0][j]); tmp[1][i + j - 1] = min(tmp[1][i + j - 1], dp[v][0][i] + dp[nv][1][j]); tmp[1][i + j - 1] = min(tmp[1][i + j - 1], dp[v][1][i] + dp[nv][1][j]); } } for (int j = 0; j <= ns + siz[nv]; j++) { dp[v][0][j] = tmp[0][j]; dp[v][1][j] = tmp[1][j]; } ns += siz[nv]; } } } void solve() { int root = di.middle_points()[0]; dfs(root, -1); func(root, -1); int ans = INF; for (int i = k + 1; i <= n; i++) { ans = min(ans, dp[root][1][i]); } printf("%d\n", ans); } int main(void) { int casenum = 1; while (1) { scanf("%d%d%d", &n, &t, &k); if (n == 0) break; di.n = n; memset(flag, false, sizeof(flag)); memset(siz, false, sizeof(siz)); for (int i = 0; i < n; i++) { G[i].clear(); di.G[i].clear(); } for (int i = 0; i < n - 1; i++) { int a, b, c; scanf("%d%d%d", &a, &b, &c); a--; b--; G[a].push_back(edge(b, c)); G[b].push_back(edge(a, c)); di.G[a].push_back(b); di.G[b].push_back(a); } for (int i = 0; i < t; i++) { int a; scanf("%d", &a); a--; flag[a] = true; } printf("Case %d: ", casenum++); solve(); } return 0; }
#include <bits/stdc++.h> #define INF 1000000007 using namespace std; typedef long long ll; typedef pair<int, int> P; struct diameter { static const int MAX = 10000; vector<int> G[MAX]; int n; int d[MAX]; bool vis[MAX]; int d2[2][MAX]; void bfs(int s) { fill(d, d + n, INF); queue<int> que; que.push(s); d[s] = 0; int v; while (que.size()) { v = que.front(); que.pop(); for (int i = 0; i < G[v].size(); i++) { int e = G[v][i]; if (d[e] == INF) { d[e] = d[v] + 1; que.push(e); } } } } P diameter_points(int s = 0) { bfs(s); int maxv = 0; int tgt = 0; for (int i = 0; i < n; i++) { if (d[i] == INF) continue; if (maxv < d[i]) { maxv = d[i]; tgt = i; } } bfs(tgt); maxv = 0; int v = 0; for (int i = 0; i < n; i++) { if (d[i] == INF) continue; if (maxv < d[i]) { maxv = d[i]; v = i; } } return P(tgt, v); } vector<int> middle_points() { P p = diameter_points(); bfs(p.first); for (int i = 0; i < n; i++) { d2[0][i] = d[i]; } bfs(p.second); for (int i = 0; i < n; i++) { d2[1][i] = d[i]; } vector<int> cid; if (d2[0][p.second] % 2 == 0) { for (int i = 0; i < n; i++) { if (d2[0][p.second] == d2[0][i] * 2 && d2[1][p.first] == d2[1][i] * 2) { cid.push_back(i); } } } else { for (int i = 0; i < n; i++) { if (d2[0][p.second] == (d2[0][i] + d2[1][i]) && abs(d2[0][i] - d2[1][i]) == 1) { cid.push_back(i); } } } return cid; } }; struct edge { int t, c; edge() {} edge(int tt, int cc) { t = tt; c = cc; } }; diameter di; vector<edge> G[10001]; int n, t, k; bool flag[10001]; int siz[10001]; int *dp[10001][2]; int tmp[2][10001]; int dfs(int v, int p) { int ans = 1; for (int i = 0; i < G[v].size(); i++) { int nv = G[v][i].t; if (nv != p) { ans += dfs(nv, v); } } return (siz[v] = ans); } void func(int v, int p) { free(dp[v][0]); free(dp[v][1]); dp[v][0] = new int[siz[v] + 1]; dp[v][1] = new int[siz[v] + 1]; if (flag[v]) { dp[v][0][1] = INF; dp[v][1][1] = 0; } else { dp[v][1][1] = INF; dp[v][0][1] = 0; } int ns = 1; for (int i = 0; i < G[v].size(); i++) { int nv = G[v][i].t; int nc = G[v][i].c; if (nv != p) { func(nv, v); for (int j = 0; j <= ns + siz[nv]; j++) { tmp[0][j] = INF; tmp[1][j] = INF; } for (int i = 1; i <= ns; i++) { for (int j = 1; j <= siz[nv]; j++) { int l = i + j; tmp[0][l] = min(tmp[0][i + j], dp[v][0][i] + dp[nv][1][j] + nc); tmp[1][l] = min(tmp[1][i + j], dp[v][1][i] + dp[nv][1][j] + nc); tmp[0][l - 1] = min(tmp[0][i + j - 1], dp[v][0][i] + dp[nv][0][j]); tmp[1][l - 1] = min(tmp[1][i + j - 1], dp[v][1][i] + dp[nv][0][j]); tmp[1][l - 1] = min(tmp[1][i + j - 1], dp[v][0][i] + dp[nv][1][j]); tmp[1][l - 1] = min(tmp[1][i + j - 1], dp[v][1][i] + dp[nv][1][j]); } } for (int j = 1; j <= ns + siz[nv]; j++) { dp[v][0][j] = tmp[0][j]; dp[v][1][j] = tmp[1][j]; } ns += siz[nv]; } } } void solve() { int root = di.middle_points()[0]; dfs(root, -1); func(root, -1); int ans = INF; for (int i = k + 1; i <= n; i++) { ans = min(ans, dp[root][1][i]); } printf("%d\n", ans); } int main(void) { int casenum = 1; while (1) { scanf("%d%d%d", &n, &t, &k); if (n == 0) break; di.n = n; memset(flag, false, sizeof(flag)); memset(siz, false, sizeof(siz)); for (int i = 0; i < n; i++) { G[i].clear(); di.G[i].clear(); } for (int i = 0; i < n - 1; i++) { int a, b, c; scanf("%d%d%d", &a, &b, &c); a--; b--; G[a].push_back(edge(b, c)); G[b].push_back(edge(a, c)); di.G[a].push_back(b); di.G[b].push_back(a); } for (int i = 0; i < t; i++) { int a; scanf("%d", &a); a--; flag[a] = true; } printf("Case %d: ", casenum++); solve(); } return 0; }
replace
137
151
137
148
TLE
p01505
C++
Time Limit Exceeded
#include <algorithm> #include <assert.h> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stdio.h> #include <string.h> #include <string> #include <vector> using namespace std; typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; static const double EPS = 1e-9; static const double PI = acos(-1.0); #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define FOR(i, s, n) for (int i = (s); i < (int)(n); i++) #define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++) #define FORIT(it, c) \ for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++) #define MEMSET(v, h) memset((v), h, sizeof(v)) const int HALF = 20; const ll BASE = 1e+9 + 7; int n; int N; int posX[15]; int posY[15]; int dist[15][15]; const int dx[4] = {-1, 0, 0, 1}; const int dy[4] = {-1, -1, 1, 1}; inline int Encode(int x, int y) { assert(x <= y); return y * (y + 1) / 2 + x; } inline void Decode(int code, int &x, int &y) { assert(0 <= code && code < 15); x = posX[code]; y = posY[code]; } struct State { char cost; char a; char cards[15]; int Get(int x, int y) { return cards[Encode(x, y)]; } void Set(int x, int y, int v) { cards[Encode(x, y)] = v; } void Swap(int x1, int y1, int x2, int y2) { swap(cards[Encode(x1, y1)], cards[Encode(x2, y2)]); } void Holl(int &rx, int &ry) { REP(i, N) { if (cards[i] == 1) { rx = posX[i]; ry = posY[i]; return; } } assert(false); } int HollX() { int x, y; Holl(x, y); return x; } int HollY() { int x, y; Holl(x, y); return y; } void CalcA() { int rev[16]; REP(i, N) { rev[(int)cards[i] - 1] = i; } a = 0; FOR(i, 1, N) { a += dist[rev[i]][i]; } FOR(i, 3, N) { if (cards[i] == i + 1) { continue; } int fx = posX[i]; int fy = posY[i]; REP(dir, 4) { int nx = fx + dx[dir]; int ny = fy + dy[dir]; if (nx < 0 || nx > ny || ny < 0 || ny >= n) { continue; } if (cards[Encode(nx, ny)] != Encode(nx, ny) + 1) { goto next; } } a += 6; next:; } if (a != 0 && n == 5) { if (cards[0] == 1 && cards[1] == 2 && cards[2] == 3) { a += 6; } if ((cards[0] == 1 || cards[1] == 1 || cards[2] == 1) && (cards[3] == 4 && cards[4] == 5 && cards[5] == 6) && (cards[6] != 7 || cards[7] != 8 || cards[8] != 9 || cards[9] != 10 || cards[11] != 12 || cards[12] != 13 || cards[13] != 14)) { a += 6; } if ((cards[0] == 1 || cards[1] == 1 || cards[2] == 1 || cards[3] == 1 || cards[4] == 1 || cards[5] == 1) && (cards[6] == 7 || cards[7] == 8 || cards[8] == 9) && (cards[9] != 10 || cards[11] != 12 || cards[12] != 13 || cards[13] != 14)) { a += 6; } } } ull Hash() { ll ret = 0; REP(i, N) { ret = ret * BASE + cards[i]; } return ret; } bool operator<(const State &rhs) const { return cost + a > rhs.cost + rhs.a; } }; map<ull, char> half[6]; void CalcHalfSide(int m) { n = m; N = n * (n + 1) / 2; State ini; ini.cost = 0; REP(i, N) { ini.cards[i] = i + 1; } queue<State> que; que.push(ini); half[m][ini.Hash()] = 0; while (!que.empty()) { State s = que.front(); que.pop(); if (s.cost == HALF) { continue; } int hx, hy; s.Holl(hx, hy); REP(dir, 4) { int nx = hx + dx[dir]; int ny = hy + dy[dir]; if (nx < 0 || nx > ny || ny < 0 || ny >= n) { continue; } State ns = s; ns.Swap(hx, hy, nx, ny); ns.cost++; ull h = ns.Hash(); if (half[m].count(h) && half[m][h] <= ns.cost) { continue; } half[m][h] = ns.cost; que.push(ns); } } } int main() { REP(y, 5) { REP(x, y + 1) { posX[Encode(x, y)] = x; posY[Encode(x, y)] = y; } } FOREQ(i, 1, 5) { CalcHalfSide(i); } int test_case = 0; while (scanf("%d", &n), n) { test_case++; N = n * (n + 1) / 2; // calc dist MEMSET(dist, 0x0f); REP(i, N) { dist[i][i] = 0; } REP(i, N) { int fx = posX[i]; int fy = posY[i]; REP(dir, 4) { int nx = fx + dx[dir]; int ny = fy + dy[dir]; if (nx < 0 || nx > ny || ny < 0 || ny >= n) { continue; } dist[i][Encode(nx, ny)] = 1; } } REP(k, N) REP(i, N) REP(j, N) { dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); } // Get Input State ini; ini.cost = 0; REP(y, n) { REP(x, y + 1) { int v; scanf("%d", &v); ini.Set(x, y, v); } } if (ini.Get(0, n - 1) != Encode(0, n - 1) + 1) { ini.Swap(0, n - 1, 0, n - 2); ini.cost++; } if (ini.Get(n - 1, n - 1) != Encode(n - 1, n - 1) + 1) { ini.Swap(n - 1, n - 1, n - 2, n - 2); ini.cost++; } ini.CalcA(); int x, y; ini.Holl(x, y); // A* map<ull, int> dists; priority_queue<State> que; que.push(ini); dists[ini.Hash()] = 0; int ans = 45; while (!que.empty()) { State s = que.top(); que.pop(); if (s.a == 0) { ans = s.cost; break; } if (half[n].count(s.Hash())) { ans = min(ans, s.cost + half[n][s.Hash()]); // cout << "OK:" << " " << ans << endl; } int hx, hy; s.Holl(hx, hy); REP(dir, 4) { int nx = hx + dx[dir]; int ny = hy + dy[dir]; if (nx < 0 || nx > ny || ny < 0 || ny >= n) { continue; } State ns = s; ns.Swap(hx, hy, nx, ny); ns.cost++; ns.CalcA(); if (ns.cost > ans - HALF) { continue; } if (ns.cost + ns.a > ans) { continue; } ull h = ns.Hash(); if (dists.count(h) && dists[h] <= ns.cost) { continue; } dists[h] = ns.cost; que.push(ns); } } printf("Case %d: %d\n", test_case, ans); } }
#include <algorithm> #include <assert.h> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stdio.h> #include <string.h> #include <string> #include <vector> using namespace std; typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; static const double EPS = 1e-9; static const double PI = acos(-1.0); #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define FOR(i, s, n) for (int i = (s); i < (int)(n); i++) #define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++) #define FORIT(it, c) \ for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++) #define MEMSET(v, h) memset((v), h, sizeof(v)) const int HALF = 22; const ll BASE = 1e+9 + 7; int n; int N; int posX[15]; int posY[15]; int dist[15][15]; const int dx[4] = {-1, 0, 0, 1}; const int dy[4] = {-1, -1, 1, 1}; inline int Encode(int x, int y) { assert(x <= y); return y * (y + 1) / 2 + x; } inline void Decode(int code, int &x, int &y) { assert(0 <= code && code < 15); x = posX[code]; y = posY[code]; } struct State { char cost; char a; char cards[15]; int Get(int x, int y) { return cards[Encode(x, y)]; } void Set(int x, int y, int v) { cards[Encode(x, y)] = v; } void Swap(int x1, int y1, int x2, int y2) { swap(cards[Encode(x1, y1)], cards[Encode(x2, y2)]); } void Holl(int &rx, int &ry) { REP(i, N) { if (cards[i] == 1) { rx = posX[i]; ry = posY[i]; return; } } assert(false); } int HollX() { int x, y; Holl(x, y); return x; } int HollY() { int x, y; Holl(x, y); return y; } void CalcA() { int rev[16]; REP(i, N) { rev[(int)cards[i] - 1] = i; } a = 0; FOR(i, 1, N) { a += dist[rev[i]][i]; } FOR(i, 3, N) { if (cards[i] == i + 1) { continue; } int fx = posX[i]; int fy = posY[i]; REP(dir, 4) { int nx = fx + dx[dir]; int ny = fy + dy[dir]; if (nx < 0 || nx > ny || ny < 0 || ny >= n) { continue; } if (cards[Encode(nx, ny)] != Encode(nx, ny) + 1) { goto next; } } a += 6; next:; } if (a != 0 && n == 5) { if (cards[0] == 1 && cards[1] == 2 && cards[2] == 3) { a += 6; } if ((cards[0] == 1 || cards[1] == 1 || cards[2] == 1) && (cards[3] == 4 && cards[4] == 5 && cards[5] == 6) && (cards[6] != 7 || cards[7] != 8 || cards[8] != 9 || cards[9] != 10 || cards[11] != 12 || cards[12] != 13 || cards[13] != 14)) { a += 6; } if ((cards[0] == 1 || cards[1] == 1 || cards[2] == 1 || cards[3] == 1 || cards[4] == 1 || cards[5] == 1) && (cards[6] == 7 || cards[7] == 8 || cards[8] == 9) && (cards[9] != 10 || cards[11] != 12 || cards[12] != 13 || cards[13] != 14)) { a += 6; } } } ull Hash() { ll ret = 0; REP(i, N) { ret = ret * BASE + cards[i]; } return ret; } bool operator<(const State &rhs) const { return cost + a > rhs.cost + rhs.a; } }; map<ull, char> half[6]; void CalcHalfSide(int m) { n = m; N = n * (n + 1) / 2; State ini; ini.cost = 0; REP(i, N) { ini.cards[i] = i + 1; } queue<State> que; que.push(ini); half[m][ini.Hash()] = 0; while (!que.empty()) { State s = que.front(); que.pop(); if (s.cost == HALF) { continue; } int hx, hy; s.Holl(hx, hy); REP(dir, 4) { int nx = hx + dx[dir]; int ny = hy + dy[dir]; if (nx < 0 || nx > ny || ny < 0 || ny >= n) { continue; } State ns = s; ns.Swap(hx, hy, nx, ny); ns.cost++; ull h = ns.Hash(); if (half[m].count(h) && half[m][h] <= ns.cost) { continue; } half[m][h] = ns.cost; que.push(ns); } } } int main() { REP(y, 5) { REP(x, y + 1) { posX[Encode(x, y)] = x; posY[Encode(x, y)] = y; } } FOREQ(i, 1, 5) { CalcHalfSide(i); } int test_case = 0; while (scanf("%d", &n), n) { test_case++; N = n * (n + 1) / 2; // calc dist MEMSET(dist, 0x0f); REP(i, N) { dist[i][i] = 0; } REP(i, N) { int fx = posX[i]; int fy = posY[i]; REP(dir, 4) { int nx = fx + dx[dir]; int ny = fy + dy[dir]; if (nx < 0 || nx > ny || ny < 0 || ny >= n) { continue; } dist[i][Encode(nx, ny)] = 1; } } REP(k, N) REP(i, N) REP(j, N) { dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); } // Get Input State ini; ini.cost = 0; REP(y, n) { REP(x, y + 1) { int v; scanf("%d", &v); ini.Set(x, y, v); } } if (ini.Get(0, n - 1) != Encode(0, n - 1) + 1) { ini.Swap(0, n - 1, 0, n - 2); ini.cost++; } if (ini.Get(n - 1, n - 1) != Encode(n - 1, n - 1) + 1) { ini.Swap(n - 1, n - 1, n - 2, n - 2); ini.cost++; } ini.CalcA(); int x, y; ini.Holl(x, y); // A* map<ull, int> dists; priority_queue<State> que; que.push(ini); dists[ini.Hash()] = 0; int ans = 45; while (!que.empty()) { State s = que.top(); que.pop(); if (s.a == 0) { ans = s.cost; break; } if (half[n].count(s.Hash())) { ans = min(ans, s.cost + half[n][s.Hash()]); // cout << "OK:" << " " << ans << endl; } int hx, hy; s.Holl(hx, hy); REP(dir, 4) { int nx = hx + dx[dir]; int ny = hy + dy[dir]; if (nx < 0 || nx > ny || ny < 0 || ny >= n) { continue; } State ns = s; ns.Swap(hx, hy, nx, ny); ns.cost++; ns.CalcA(); if (ns.cost > ans - HALF) { continue; } if (ns.cost + ns.a > ans) { continue; } ull h = ns.Hash(); if (dists.count(h) && dists[h] <= ns.cost) { continue; } dists[h] = ns.cost; que.push(ns); } } printf("Case %d: %d\n", test_case, ans); } }
replace
26
27
26
27
TLE
p01505
C++
Memory Limit Exceeded
#include <algorithm> #include <assert.h> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stdio.h> #include <string.h> #include <string> #include <vector> using namespace std; typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; static const double EPS = 1e-9; static const double PI = acos(-1.0); #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define FOR(i, s, n) for (int i = (s); i < (int)(n); i++) #define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++) #define FORIT(it, c) \ for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++) #define MEMSET(v, h) memset((v), h, sizeof(v)) const int HALF = 23; const ll BASE = 1e+9 + 7; int n; int N; int posX[15]; int posY[15]; int dist[15][15]; const int dx[4] = {-1, 0, 0, 1}; const int dy[4] = {-1, -1, 1, 1}; inline int Encode(int x, int y) { assert(x <= y); return y * (y + 1) / 2 + x; } inline void Decode(int code, int &x, int &y) { assert(0 <= code && code < 15); x = posX[code]; y = posY[code]; } struct State { char cost; char a; char cards[15]; int Get(int x, int y) { return cards[Encode(x, y)]; } void Set(int x, int y, int v) { cards[Encode(x, y)] = v; } void Swap(int x1, int y1, int x2, int y2) { swap(cards[Encode(x1, y1)], cards[Encode(x2, y2)]); } void Holl(int &rx, int &ry) { REP(i, N) { if (cards[i] == 1) { rx = posX[i]; ry = posY[i]; return; } } assert(false); } int HollX() { int x, y; Holl(x, y); return x; } int HollY() { int x, y; Holl(x, y); return y; } void CalcA() { int rev[16]; REP(i, N) { rev[(int)cards[i] - 1] = i; } a = 0; FOR(i, 1, N) { a += dist[rev[i]][i]; } FOR(i, 3, N) { if (cards[i] == i + 1) { continue; } int fx = posX[i]; int fy = posY[i]; REP(dir, 4) { int nx = fx + dx[dir]; int ny = fy + dy[dir]; if (nx < 0 || nx > ny || ny < 0 || ny >= n) { continue; } if (cards[Encode(nx, ny)] != Encode(nx, ny) + 1) { goto next; } } a += 6; next:; } if (a != 0 && n == 5) { if (cards[0] == 1 && cards[1] == 2 && cards[2] == 3) { a += 6; } if ((cards[0] == 1 || cards[1] == 1 || cards[2] == 1) && (cards[3] == 4 && cards[4] == 5 && cards[5] == 6) && (cards[6] != 7 || cards[7] != 8 || cards[8] != 9 || cards[9] != 10 || cards[11] != 12 || cards[12] != 13 || cards[13] != 14)) { a += 6; } if ((cards[0] == 1 || cards[1] == 1 || cards[2] == 1 || cards[3] == 1 || cards[4] == 1 || cards[5] == 1) && (cards[6] == 7 || cards[7] == 8 || cards[8] == 9) && (cards[9] != 10 || cards[11] != 12 || cards[12] != 13 || cards[13] != 14)) { a += 6; } } } ull Hash() { ll ret = 0; REP(i, N) { ret = ret * BASE + cards[i]; } return ret; } bool operator<(const State &rhs) const { return cost + a > rhs.cost + rhs.a; } }; map<ull, char> half[6]; void CalcHalfSide(int m) { n = m; N = n * (n + 1) / 2; State ini; ini.cost = 0; REP(i, N) { ini.cards[i] = i + 1; } queue<State> que; que.push(ini); half[m][ini.Hash()] = 0; while (!que.empty()) { State s = que.front(); que.pop(); if (s.cost == HALF) { continue; } int hx, hy; s.Holl(hx, hy); REP(dir, 4) { int nx = hx + dx[dir]; int ny = hy + dy[dir]; if (nx < 0 || nx > ny || ny < 0 || ny >= n) { continue; } State ns = s; ns.Swap(hx, hy, nx, ny); ns.cost++; ull h = ns.Hash(); if (half[m].count(h) && half[m][h] <= ns.cost) { continue; } half[m][h] = ns.cost; que.push(ns); } } } int main() { REP(y, 5) { REP(x, y + 1) { posX[Encode(x, y)] = x; posY[Encode(x, y)] = y; } } FOREQ(i, 1, 5) { CalcHalfSide(i); } int test_case = 0; while (scanf("%d", &n), n) { test_case++; N = n * (n + 1) / 2; // calc dist MEMSET(dist, 0x0f); REP(i, N) { dist[i][i] = 0; } REP(i, N) { int fx = posX[i]; int fy = posY[i]; REP(dir, 4) { int nx = fx + dx[dir]; int ny = fy + dy[dir]; if (nx < 0 || nx > ny || ny < 0 || ny >= n) { continue; } dist[i][Encode(nx, ny)] = 1; } } REP(k, N) REP(i, N) REP(j, N) { dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); } // Get Input State ini; ini.cost = 0; REP(y, n) { REP(x, y + 1) { int v; scanf("%d", &v); ini.Set(x, y, v); } } if (ini.Get(0, n - 1) != Encode(0, n - 1) + 1) { ini.Swap(0, n - 1, 0, n - 2); ini.cost++; } if (ini.Get(n - 1, n - 1) != Encode(n - 1, n - 1) + 1) { ini.Swap(n - 1, n - 1, n - 2, n - 2); ini.cost++; } ini.CalcA(); int x, y; ini.Holl(x, y); // A* map<ull, int> dists; priority_queue<State> que; que.push(ini); dists[ini.Hash()] = 0; int ans = 45; while (!que.empty()) { State s = que.top(); que.pop(); if (s.a == 0) { ans = s.cost; break; } if (half[n].count(s.Hash())) { ans = min(ans, s.cost + half[n][s.Hash()]); // cout << "OK:" << " " << ans << endl; continue; } int hx, hy; s.Holl(hx, hy); REP(dir, 4) { int nx = hx + dx[dir]; int ny = hy + dy[dir]; if (nx < 0 || nx > ny || ny < 0 || ny >= n) { continue; } State ns = s; ns.Swap(hx, hy, nx, ny); ns.cost++; ns.CalcA(); if (ns.cost > ans - HALF) { continue; } if (ns.cost + ns.a > ans) { continue; } ull h = ns.Hash(); if (dists.count(h) && dists[h] <= ns.cost) { continue; } dists[h] = ns.cost; que.push(ns); } } printf("Case %d: %d\n", test_case, ans); } }
#include <algorithm> #include <assert.h> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stdio.h> #include <string.h> #include <string> #include <vector> using namespace std; typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; static const double EPS = 1e-9; static const double PI = acos(-1.0); #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define FOR(i, s, n) for (int i = (s); i < (int)(n); i++) #define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++) #define FORIT(it, c) \ for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++) #define MEMSET(v, h) memset((v), h, sizeof(v)) const int HALF = 21; const ll BASE = 1e+9 + 7; int n; int N; int posX[15]; int posY[15]; int dist[15][15]; const int dx[4] = {-1, 0, 0, 1}; const int dy[4] = {-1, -1, 1, 1}; inline int Encode(int x, int y) { assert(x <= y); return y * (y + 1) / 2 + x; } inline void Decode(int code, int &x, int &y) { assert(0 <= code && code < 15); x = posX[code]; y = posY[code]; } struct State { char cost; char a; char cards[15]; int Get(int x, int y) { return cards[Encode(x, y)]; } void Set(int x, int y, int v) { cards[Encode(x, y)] = v; } void Swap(int x1, int y1, int x2, int y2) { swap(cards[Encode(x1, y1)], cards[Encode(x2, y2)]); } void Holl(int &rx, int &ry) { REP(i, N) { if (cards[i] == 1) { rx = posX[i]; ry = posY[i]; return; } } assert(false); } int HollX() { int x, y; Holl(x, y); return x; } int HollY() { int x, y; Holl(x, y); return y; } void CalcA() { int rev[16]; REP(i, N) { rev[(int)cards[i] - 1] = i; } a = 0; FOR(i, 1, N) { a += dist[rev[i]][i]; } FOR(i, 3, N) { if (cards[i] == i + 1) { continue; } int fx = posX[i]; int fy = posY[i]; REP(dir, 4) { int nx = fx + dx[dir]; int ny = fy + dy[dir]; if (nx < 0 || nx > ny || ny < 0 || ny >= n) { continue; } if (cards[Encode(nx, ny)] != Encode(nx, ny) + 1) { goto next; } } a += 6; next:; } if (a != 0 && n == 5) { if (cards[0] == 1 && cards[1] == 2 && cards[2] == 3) { a += 6; } if ((cards[0] == 1 || cards[1] == 1 || cards[2] == 1) && (cards[3] == 4 && cards[4] == 5 && cards[5] == 6) && (cards[6] != 7 || cards[7] != 8 || cards[8] != 9 || cards[9] != 10 || cards[11] != 12 || cards[12] != 13 || cards[13] != 14)) { a += 6; } if ((cards[0] == 1 || cards[1] == 1 || cards[2] == 1 || cards[3] == 1 || cards[4] == 1 || cards[5] == 1) && (cards[6] == 7 || cards[7] == 8 || cards[8] == 9) && (cards[9] != 10 || cards[11] != 12 || cards[12] != 13 || cards[13] != 14)) { a += 6; } } } ull Hash() { ll ret = 0; REP(i, N) { ret = ret * BASE + cards[i]; } return ret; } bool operator<(const State &rhs) const { return cost + a > rhs.cost + rhs.a; } }; map<ull, char> half[6]; void CalcHalfSide(int m) { n = m; N = n * (n + 1) / 2; State ini; ini.cost = 0; REP(i, N) { ini.cards[i] = i + 1; } queue<State> que; que.push(ini); half[m][ini.Hash()] = 0; while (!que.empty()) { State s = que.front(); que.pop(); if (s.cost == HALF) { continue; } int hx, hy; s.Holl(hx, hy); REP(dir, 4) { int nx = hx + dx[dir]; int ny = hy + dy[dir]; if (nx < 0 || nx > ny || ny < 0 || ny >= n) { continue; } State ns = s; ns.Swap(hx, hy, nx, ny); ns.cost++; ull h = ns.Hash(); if (half[m].count(h) && half[m][h] <= ns.cost) { continue; } half[m][h] = ns.cost; que.push(ns); } } } int main() { REP(y, 5) { REP(x, y + 1) { posX[Encode(x, y)] = x; posY[Encode(x, y)] = y; } } FOREQ(i, 1, 5) { CalcHalfSide(i); } int test_case = 0; while (scanf("%d", &n), n) { test_case++; N = n * (n + 1) / 2; // calc dist MEMSET(dist, 0x0f); REP(i, N) { dist[i][i] = 0; } REP(i, N) { int fx = posX[i]; int fy = posY[i]; REP(dir, 4) { int nx = fx + dx[dir]; int ny = fy + dy[dir]; if (nx < 0 || nx > ny || ny < 0 || ny >= n) { continue; } dist[i][Encode(nx, ny)] = 1; } } REP(k, N) REP(i, N) REP(j, N) { dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); } // Get Input State ini; ini.cost = 0; REP(y, n) { REP(x, y + 1) { int v; scanf("%d", &v); ini.Set(x, y, v); } } if (ini.Get(0, n - 1) != Encode(0, n - 1) + 1) { ini.Swap(0, n - 1, 0, n - 2); ini.cost++; } if (ini.Get(n - 1, n - 1) != Encode(n - 1, n - 1) + 1) { ini.Swap(n - 1, n - 1, n - 2, n - 2); ini.cost++; } ini.CalcA(); int x, y; ini.Holl(x, y); // A* map<ull, int> dists; priority_queue<State> que; que.push(ini); dists[ini.Hash()] = 0; int ans = 45; while (!que.empty()) { State s = que.top(); que.pop(); if (s.a == 0) { ans = s.cost; break; } if (half[n].count(s.Hash())) { ans = min(ans, s.cost + half[n][s.Hash()]); // cout << "OK:" << " " << ans << endl; continue; } int hx, hy; s.Holl(hx, hy); REP(dir, 4) { int nx = hx + dx[dir]; int ny = hy + dy[dir]; if (nx < 0 || nx > ny || ny < 0 || ny >= n) { continue; } State ns = s; ns.Swap(hx, hy, nx, ny); ns.cost++; ns.CalcA(); if (ns.cost > ans - HALF) { continue; } if (ns.cost + ns.a > ans) { continue; } ull h = ns.Hash(); if (dists.count(h) && dists[h] <= ns.cost) { continue; } dists[h] = ns.cost; que.push(ns); } } printf("Case %d: %d\n", test_case, ans); } }
replace
26
27
26
27
MLE
p01507
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep1(i, n) for (int i = 1; i <= (int)(n); i++) #define all(c) c.begin(), c.end() #define pb push_back #define fs first #define sc second #define show(x) cout << #x << " = " << x << endl #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) using namespace std; /* use under a[i][j]!=0 -> abs(i-j)<=B (??????+-B?????????????????????????????????) ??????????????????swap?????±?????§,-B~+2B?????§???3B+1??????vi??????????????£?????? ?????????0????????????B-th value(0-indexed) ???a[i][i]?????????????????????????????? time O(NB^2) space O(NB) */ typedef long long ll; ll mod = 1e9 + 7; typedef vector<ll> vi; typedef vector<vi> bmat; ll ex(ll x, ll p) { ll a = 1; while (p) { if (p % 2) a = a * x % mod; x = x * x % mod; p /= 2; } return a; } ll inv(ll a) { return ex(a, mod - 2); } ll det(bmat a) { int N = a.size(); int B = a[0].size() / 3; ll ans = 1; rep(i, N) { if (a[i][B] == 0) { for (int j = i + 1; j < N; j++) { if (B - (j - i) < 0) break; if (a[j][B - (j - i)] != 0) { int d = j - i; for (int k = d; k <= 2 * B + d; k++) { swap(a[i][k], a[j][k - d]); } ans *= -1; break; } } if (a[i][B] == 0) return 0; } for (int j = i + 1; j < N; j++) { if (B - (j - i) < 0) break; int d = j - i; ll c = a[j][B - d] * inv(a[i][B]) % mod; for (int k = B; k <= 3 * B; k++) { a[j][k - d] = (a[j][k - d] - c * a[i][k]) % mod; } } ans = ans * a[i][B] % mod; } if (ans < 0) ans += mod; return ans; } int H, W; int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; string s[500]; bool is(int x, int y) { return 0 <= x && x < H && 0 <= y && y < W && s[x][y] == '.'; } int n[500][15]; int main() { int tt = 0; while (true) { tt++; cin >> H >> W; if (H == 0) break; rep(i, H) cin >> s[i]; int N = 0; rep(i, H) rep(j, W) { if (s[i][j] == '#') n[i][j] = -1; else n[i][j] = N++; } bmat a(N - 1, vi(3 * W + 1, 0)); rep(i, H) rep(j, W) { if (!is(i, j)) continue; if (n[i][j] == N - 1) break; rep(d, 4) { int x = i + dx[d], y = j + dy[d]; if (!is(x, y)) continue; if (n[x][y] != N - 1) a[n[i][j]][n[x][y] - n[i][j] + W]++; a[n[i][j]][W]++; } } cout << "Case " << tt << ": " << det(a) << endl; } }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep1(i, n) for (int i = 1; i <= (int)(n); i++) #define all(c) c.begin(), c.end() #define pb push_back #define fs first #define sc second #define show(x) cout << #x << " = " << x << endl #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) using namespace std; /* use under a[i][j]!=0 -> abs(i-j)<=B (??????+-B?????????????????????????????????) ??????????????????swap?????±?????§,-B~+2B?????§???3B+1??????vi??????????????£?????? ?????????0????????????B-th value(0-indexed) ???a[i][i]?????????????????????????????? time O(NB^2) space O(NB) */ typedef long long ll; ll mod = 1e9 + 7; typedef vector<ll> vi; typedef vector<vi> bmat; ll ex(ll x, ll p) { ll a = 1; while (p) { if (p % 2) a = a * x % mod; x = x * x % mod; p /= 2; } return a; } ll inv(ll a) { return ex(a, mod - 2); } ll det(bmat a) { int N = a.size(); if (N == 0) return 1; int B = a[0].size() / 3; ll ans = 1; rep(i, N) { if (a[i][B] == 0) { for (int j = i + 1; j < N; j++) { if (B - (j - i) < 0) break; if (a[j][B - (j - i)] != 0) { int d = j - i; for (int k = d; k <= 2 * B + d; k++) { swap(a[i][k], a[j][k - d]); } ans *= -1; break; } } if (a[i][B] == 0) return 0; } for (int j = i + 1; j < N; j++) { if (B - (j - i) < 0) break; int d = j - i; ll c = a[j][B - d] * inv(a[i][B]) % mod; for (int k = B; k <= 3 * B; k++) { a[j][k - d] = (a[j][k - d] - c * a[i][k]) % mod; } } ans = ans * a[i][B] % mod; } if (ans < 0) ans += mod; return ans; } int H, W; int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; string s[500]; bool is(int x, int y) { return 0 <= x && x < H && 0 <= y && y < W && s[x][y] == '.'; } int n[500][15]; int main() { int tt = 0; while (true) { tt++; cin >> H >> W; if (H == 0) break; rep(i, H) cin >> s[i]; int N = 0; rep(i, H) rep(j, W) { if (s[i][j] == '#') n[i][j] = -1; else n[i][j] = N++; } bmat a(N - 1, vi(3 * W + 1, 0)); rep(i, H) rep(j, W) { if (!is(i, j)) continue; if (n[i][j] == N - 1) break; rep(d, 4) { int x = i + dx[d], y = j + dy[d]; if (!is(x, y)) continue; if (n[x][y] != N - 1) a[n[i][j]][n[x][y] - n[i][j] + W]++; a[n[i][j]][W]++; } } cout << "Case " << tt << ": " << det(a) << endl; } }
insert
35
35
35
37
0
p01508
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define sgn(v) (abs((v)) < eps ? 0 : ((v) < 0 ? -1 : 1)) #define sqr(v) ((v) * (v)) #define mb make_pair #define debug 0 using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> PII; typedef pair<int, LL> PIL; typedef pair<LL, int> PLI; typedef pair<LL, LL> PLL; const double eps = 1e-8; const double pi = acos(-1); const int maxn = 101; struct Point { double x, y; Point() {} Point(double X, double Y) { x = X; y = Y; } bool operator<(const Point &p) const { if (sgn(x - p.x)) return x < p.x; else return y < p.y; } bool operator==(const Point &p) const { return sgn(x - p.x) == 0 && sgn(y - p.y) == 0; } Point operator-(const Point &p) { return Point(x - p.x, y - p.y); } Point operator+(const Point &p) { return Point(x + p.x, y + p.y); } double cross(const Point &p) const { return x * p.y - y * p.x; } double len() { return sqrt(sqr(x) + sqr(y)); } void unit() { double tmp = len(); x /= tmp; y /= tmp; x *= 0.0001; y *= 0.0001; } } pt[maxn]; struct Segment { Point st, ed; }; double cross(const Point &p, const Point &q) { return p.x * q.y - p.y * q.x; } double dot(const Point &p, const Point &q) { return p.x * q.x + p.y * q.y; } Point Insect(Segment &p, Segment &q) { double u = cross(p.ed - p.st, q.st - p.st), v = cross(p.st - p.ed, q.ed - p.st); return Point((q.st.x * v + q.ed.x * u) / (u + v), (q.st.y * v + q.ed.y * u) / (u + v)); } vector<Point> tp, sp; int n, cas; int check(Point P, int deb) { int con = 0; Segment now, tmp; Point cp; now.st = P; con = 0; now.ed.x = P.x + 10000; 19950527; now.ed.y = P.y + 10000; 27051995; for (int i = 0; i < n; ++i) { tmp.st = pt[i]; tmp.ed = pt[(i + 1) % n]; if (sgn((tmp.ed - tmp.st).cross(now.ed - now.st))) { cp = Insect(tmp, now); if (cp == tmp.ed || cp == tmp.st) while (1) ; if (sgn(dot(tmp.ed - cp, cp - tmp.st)) > 0 && sgn(dot(now.ed - cp, cp - now.st)) > 0) ++con; } } return con % 2; } double check(int i, int j) { Segment now, tmp; Point cp; tp.clear(); now.st = pt[i]; now.ed = pt[j]; for (i = 0; i < n; ++i) { tmp.st = pt[i]; tmp.ed = pt[(i + 1) % n]; if (sgn((tmp.ed - tmp.st).cross(now.ed - now.st))) { cp = Insect(tmp, now); if (sgn(dot(tmp.ed - cp, cp - tmp.st)) > 0) tp.push_back(cp); else if (sgn(dot(tmp.ed - cp, cp - tmp.st)) == 0) // endpoints { if (cp == tmp.st) j = i; else j = i + 1; if (cross(pt[(j - 1 + n) % n] - now.st, now.ed - now.st) * cross(pt[(j + 1) % n] - now.st, now.ed - now.st) < 0) tp.push_back(cp); } } else if (sgn((tmp.st - now.st).cross(now.ed - now.st)) == 0 && sgn((tmp.ed - now.st).cross(now.ed - now.st)) == 0) { Point vec = tmp.ed - tmp.st; vec.unit(); if (cross(pt[(i - 1 + n) % n] - now.st, now.ed - now.st) * cross(pt[(i + 2) % n] - now.st, now.ed - now.st) < 0) { if (!check(tmp.st - vec, 0)) tp.push_back(tmp.st); if (!check(tmp.ed + vec, 0)) tp.push_back(tmp.ed); } } } sort(tp.begin(), tp.end()); int n = tp.size(); sp.clear(); for (i = 0; i < n; ++i) if (i == 0 || (!(tp[i] == tp[i - 1]))) sp.push_back(tp[i]); double ans = 0; for (i = 0; i < sp.size(); i += 2) ans = max(ans, (sp[i + 1] - sp[i]).len()); return ans; } int main() { int i, j; cas = 0; while (scanf("%d", &n) != EOF) { if (n == 0) break; double len = 0; for (i = 0; i < n; ++i) scanf("%lf%lf", &pt[i].x, &pt[i].y); for (i = 0; i < n; ++i) len = max(len, (pt[i] - pt[(i + 1) % n]).len()); for (i = 0; i < n; ++i) for (j = i + 1; j < n; ++j) len = max(len, check(i, j)); printf("Case %d: %.10f\n", ++cas, len); // cout <<len*len<<endl; } return 0; }
#include <algorithm> #include <bitset> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define sgn(v) (abs((v)) < eps ? 0 : ((v) < 0 ? -1 : 1)) #define sqr(v) ((v) * (v)) #define mb make_pair #define debug 0 using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> PII; typedef pair<int, LL> PIL; typedef pair<LL, int> PLI; typedef pair<LL, LL> PLL; const double eps = 1e-8; const double pi = acos(-1); const int maxn = 101; struct Point { double x, y; Point() {} Point(double X, double Y) { x = X; y = Y; } bool operator<(const Point &p) const { if (sgn(x - p.x)) return x < p.x; else return y < p.y; } bool operator==(const Point &p) const { return sgn(x - p.x) == 0 && sgn(y - p.y) == 0; } Point operator-(const Point &p) { return Point(x - p.x, y - p.y); } Point operator+(const Point &p) { return Point(x + p.x, y + p.y); } double cross(const Point &p) const { return x * p.y - y * p.x; } double len() { return sqrt(sqr(x) + sqr(y)); } void unit() { double tmp = len(); x /= tmp; y /= tmp; x *= 0.0001; y *= 0.0001; } } pt[maxn]; struct Segment { Point st, ed; }; double cross(const Point &p, const Point &q) { return p.x * q.y - p.y * q.x; } double dot(const Point &p, const Point &q) { return p.x * q.x + p.y * q.y; } Point Insect(Segment &p, Segment &q) { double u = cross(p.ed - p.st, q.st - p.st), v = cross(p.st - p.ed, q.ed - p.st); return Point((q.st.x * v + q.ed.x * u) / (u + v), (q.st.y * v + q.ed.y * u) / (u + v)); } vector<Point> tp, sp; int n, cas; int check(Point P, int deb) { int con = 0; Segment now, tmp; Point cp; now.st = P; con = 0; now.ed.x = P.x + 19950527; now.ed.y = P.y + 27051995; for (int i = 0; i < n; ++i) { tmp.st = pt[i]; tmp.ed = pt[(i + 1) % n]; if (sgn((tmp.ed - tmp.st).cross(now.ed - now.st))) { cp = Insect(tmp, now); if (cp == tmp.ed || cp == tmp.st) while (1) ; if (sgn(dot(tmp.ed - cp, cp - tmp.st)) > 0 && sgn(dot(now.ed - cp, cp - now.st)) > 0) ++con; } } return con % 2; } double check(int i, int j) { Segment now, tmp; Point cp; tp.clear(); now.st = pt[i]; now.ed = pt[j]; for (i = 0; i < n; ++i) { tmp.st = pt[i]; tmp.ed = pt[(i + 1) % n]; if (sgn((tmp.ed - tmp.st).cross(now.ed - now.st))) { cp = Insect(tmp, now); if (sgn(dot(tmp.ed - cp, cp - tmp.st)) > 0) tp.push_back(cp); else if (sgn(dot(tmp.ed - cp, cp - tmp.st)) == 0) // endpoints { if (cp == tmp.st) j = i; else j = i + 1; if (cross(pt[(j - 1 + n) % n] - now.st, now.ed - now.st) * cross(pt[(j + 1) % n] - now.st, now.ed - now.st) < 0) tp.push_back(cp); } } else if (sgn((tmp.st - now.st).cross(now.ed - now.st)) == 0 && sgn((tmp.ed - now.st).cross(now.ed - now.st)) == 0) { Point vec = tmp.ed - tmp.st; vec.unit(); if (cross(pt[(i - 1 + n) % n] - now.st, now.ed - now.st) * cross(pt[(i + 2) % n] - now.st, now.ed - now.st) < 0) { if (!check(tmp.st - vec, 0)) tp.push_back(tmp.st); if (!check(tmp.ed + vec, 0)) tp.push_back(tmp.ed); } } } sort(tp.begin(), tp.end()); int n = tp.size(); sp.clear(); for (i = 0; i < n; ++i) if (i == 0 || (!(tp[i] == tp[i - 1]))) sp.push_back(tp[i]); double ans = 0; for (i = 0; i < sp.size(); i += 2) ans = max(ans, (sp[i + 1] - sp[i]).len()); return ans; } int main() { int i, j; cas = 0; while (scanf("%d", &n) != EOF) { if (n == 0) break; double len = 0; for (i = 0; i < n; ++i) scanf("%lf%lf", &pt[i].x, &pt[i].y); for (i = 0; i < n; ++i) len = max(len, (pt[i] - pt[(i + 1) % n]).len()); for (i = 0; i < n; ++i) for (j = i + 1; j < n; ++j) len = max(len, check(i, j)); printf("Case %d: %.10f\n", ++cas, len); // cout <<len*len<<endl; } return 0; }
replace
76
80
76
78
TLE
p01511
C++
Runtime Error
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i) #define REP(i, n) FOR(i, 0, n) #define FORIT(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) template <class T> void debug(T begin, T end) { for (T i = begin; i != end; ++i) cerr << *i << " "; cerr << endl; } inline bool valid(int x, int y, int W, int H) { return (x >= 0 && y >= 0 && x < W && y < H); } typedef long long ll; const int INF = 100000000; const double EPS = 1e-8; const int MOD = 1000000009; int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; typedef vector<ll> Array; typedef vector<Array> Matrix; // 行列の掛け算 O(N * M * S) Matrix mul(const Matrix &a, const Matrix &b) { int N = a.size(), M = b[0].size(), S = a[0].size(); assert(S == b.size()); Matrix c(N, Array(M)); for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { for (int k = 0; k < S; k++) { c[i][j] += a[i][k] * b[k][j]; c[i][j] %= MOD; } } } return c; } // 正方行列の累乗 O(N^3 * logn) Matrix pow(Matrix a, long long b) { int N = a.size(); Matrix c(N, Array(N)); REP(i, N) c[i][i] = 1; while (b > 0) { if (b & 1) c = mul(c, a); a = mul(a, a); b >>= 1; } return c; } void print(Matrix m) { REP(i, m.size()) { REP(j, m[i].size()) cout << m[i][j] << " "; cout << endl; } } int solve(ll W, ll H, vector<ll> ys, map<ll, vector<ll>> xmp) { Matrix T(W, Array(W)); REP(i, W) REP(j, W) if (abs(i - j) <= 1) T[i][j] = 1; Matrix A(W, Array(W)); REP(i, W) REP(j, W) A[i][j] = (i == j) ? 1 : 0; for (int i = 1; i < ys.size(); i++) { ll b = (ys[i] - ys[i - 1]); A = mul(pow(T, b), A); // Erase Matrix M(W, Array(W)); REP(j, W) M[j][j] = 1; REP(j, xmp[ys[i]].size()) { ll x = xmp[ys[i]][j]; M[x][x] = 0; } A = mul(M, A); // print(A); // cout << endl; } return A[W - 1][0]; } int solve2(ll W, ll H, int N, vector<ll> x, vector<ll> y) { if (H > 10000) assert(false); if (H > 10000) return -1; ll dp[10000][75] = {}; // memset(dp, 0, sizeof(dp)); bool bad[10000][75] = {}; // memset(bad, 0, sizeof(bad)); REP(i, N) bad[y[i]][x[i]] = true; dp[0][0] = 1; for (int y = 0; y + 1 < H; y++) { for (int x = 0; x < W; x++) { for (int dx = -1; dx <= 1; dx++) { if (x + dx >= 0 && x + dx < W && !bad[y + 1][x + dx]) { dp[y + 1][x + dx] += dp[y][x]; dp[y + 1][x + dx] %= MOD; } } } // cout << y + 1 << ": "; REP(x, W) cout << dp[y + 1][x] << " "; cout << // endl; } return dp[H - 1][W - 1]; } int main() { ll W, H; int N; int C = 1; while (cin >> W >> H >> N && W) { vector<ll> ys; map<ll, vector<ll>> xmp; ys.push_back(0); ys.push_back(H - 1); vector<ll> x(N), y(N); REP(i, N) { cin >> x[i] >> y[i]; x[i]--; y[i]--; ys.push_back(y[i]); xmp[y[i]].push_back(x[i]); } sort(ys.begin(), ys.end()); ys.erase(unique(ys.begin(), ys.end()), ys.end()); printf("Case %d: ", C++); // assert(solve(W, H, ys, xmp) == solve2(W, H, N, x, y)); cout << solve2(W, H, N, x, y) << endl; // cout << solve(W, H, ys, xmp) << endl; } return 0; }
#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i) #define REP(i, n) FOR(i, 0, n) #define FORIT(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) template <class T> void debug(T begin, T end) { for (T i = begin; i != end; ++i) cerr << *i << " "; cerr << endl; } inline bool valid(int x, int y, int W, int H) { return (x >= 0 && y >= 0 && x < W && y < H); } typedef long long ll; const int INF = 100000000; const double EPS = 1e-8; const int MOD = 1000000009; int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; typedef vector<ll> Array; typedef vector<Array> Matrix; // 行列の掛け算 O(N * M * S) Matrix mul(const Matrix &a, const Matrix &b) { int N = a.size(), M = b[0].size(), S = a[0].size(); assert(S == b.size()); Matrix c(N, Array(M)); for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { for (int k = 0; k < S; k++) { c[i][j] += a[i][k] * b[k][j]; c[i][j] %= MOD; } } } return c; } // 正方行列の累乗 O(N^3 * logn) Matrix pow(Matrix a, long long b) { int N = a.size(); Matrix c(N, Array(N)); REP(i, N) c[i][i] = 1; while (b > 0) { if (b & 1) c = mul(c, a); a = mul(a, a); b >>= 1; } return c; } void print(Matrix m) { REP(i, m.size()) { REP(j, m[i].size()) cout << m[i][j] << " "; cout << endl; } } int solve(ll W, ll H, vector<ll> ys, map<ll, vector<ll>> xmp) { Matrix T(W, Array(W)); REP(i, W) REP(j, W) if (abs(i - j) <= 1) T[i][j] = 1; Matrix A(W, Array(W)); REP(i, W) REP(j, W) A[i][j] = (i == j) ? 1 : 0; for (int i = 1; i < ys.size(); i++) { ll b = (ys[i] - ys[i - 1]); A = mul(pow(T, b), A); // Erase Matrix M(W, Array(W)); REP(j, W) M[j][j] = 1; REP(j, xmp[ys[i]].size()) { ll x = xmp[ys[i]][j]; M[x][x] = 0; } A = mul(M, A); // print(A); // cout << endl; } return A[W - 1][0]; } int solve2(ll W, ll H, int N, vector<ll> x, vector<ll> y) { if (H > 10000) assert(false); if (H > 10000) return -1; ll dp[10000][75] = {}; // memset(dp, 0, sizeof(dp)); bool bad[10000][75] = {}; // memset(bad, 0, sizeof(bad)); REP(i, N) bad[y[i]][x[i]] = true; dp[0][0] = 1; for (int y = 0; y + 1 < H; y++) { for (int x = 0; x < W; x++) { for (int dx = -1; dx <= 1; dx++) { if (x + dx >= 0 && x + dx < W && !bad[y + 1][x + dx]) { dp[y + 1][x + dx] += dp[y][x]; dp[y + 1][x + dx] %= MOD; } } } // cout << y + 1 << ": "; REP(x, W) cout << dp[y + 1][x] << " "; cout << // endl; } return dp[H - 1][W - 1]; } int main() { ll W, H; int N; int C = 1; while (cin >> W >> H >> N && W) { vector<ll> ys; map<ll, vector<ll>> xmp; ys.push_back(0); ys.push_back(H - 1); vector<ll> x(N), y(N); REP(i, N) { cin >> x[i] >> y[i]; x[i]--; y[i]--; ys.push_back(y[i]); xmp[y[i]].push_back(x[i]); } sort(ys.begin(), ys.end()); ys.erase(unique(ys.begin(), ys.end()), ys.end()); printf("Case %d: ", C++); // assert(solve(W, H, ys, xmp) == solve2(W, H, N, x, y)); // cout << solve2(W, H, N, x, y) << endl; cout << solve(W, H, ys, xmp) << endl; } return 0; }
replace
143
145
143
145
0
p01511
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using Int = long long; 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; } // BEGIN CUT HERE template <size_t N, typename R> struct SquareMatrix { typedef array<R, N> arr; typedef array<arr, N> mat; mat dat; SquareMatrix() { for (size_t i = 0; i < N; i++) for (size_t j = 0; j < N; j++) dat[i][j] = R::add_identity(); } SquareMatrix &operator=(const SquareMatrix &a) { dat = a.dat; return (*this); } bool operator==(const SquareMatrix &a) const { return dat == a.dat; } size_t size() const { return N; }; arr &operator[](size_t k) { return dat[k]; }; const arr &operator[](size_t k) const { return dat[k]; }; static SquareMatrix add_identity() { return SquareMatrix(); } static SquareMatrix mul_identity() { SquareMatrix res; for (size_t i = 0; i < N; i++) res[i][i] = R::mul_identity(); return res; } SquareMatrix operator*(const SquareMatrix &B) const { SquareMatrix res; for (size_t i = 0; i < N; i++) for (size_t j = 0; j < N; j++) for (size_t k = 0; k < N; k++) res[i][j] = res[i][j] + (dat[i][k] * B[k][j]); return res; } SquareMatrix operator+(const SquareMatrix &B) const { SquareMatrix res; for (size_t i = 0; i < N; i++) for (size_t j = 0; j < N; j++) res[i][j] = dat[i][j] + B[i][j]; return res; } SquareMatrix pow(long long n) const { SquareMatrix a = *this, res = mul_identity(); while (n) { if (n & 1) res = res * a; a = a * a; n >>= 1; } return res; } }; // END CUT HERE template <typename T, T MOD = 1000000007> struct Mint { T v; Mint() : v(0) {} Mint(signed v) : v(v) {} Mint(long long t) { v = t % MOD; if (v < 0) v += MOD; } Mint pow(long long k) { Mint res(1), tmp(v); while (k) { if (k & 1) res *= tmp; tmp *= tmp; k >>= 1; } return res; } static Mint add_identity() { return Mint(0); } static Mint mul_identity() { return Mint(1); } Mint inv() { return pow(MOD - 2); } Mint &operator+=(Mint a) { v += a.v; if (v >= MOD) v -= MOD; return *this; } Mint &operator-=(Mint a) { v += MOD - a.v; if (v >= MOD) v -= MOD; return *this; } Mint &operator*=(Mint a) { v = 1LL * v * a.v % MOD; return *this; } Mint &operator/=(Mint a) { return (*this) *= a.inv(); } Mint operator+(Mint a) const { return Mint(v) += a; }; Mint operator-(Mint a) const { return Mint(v) -= a; }; Mint operator*(Mint a) const { return Mint(v) *= a; }; Mint operator/(Mint a) const { return Mint(v) /= a; }; Mint operator-() { return v ? MOD - v : v; } bool operator==(const Mint a) const { return v == a.v; } bool operator!=(const Mint a) const { return v != a.v; } bool operator<(const Mint a) const { return v < a.v; } }; struct FastIO { FastIO() { cin.tie(0); ios::sync_with_stdio(0); } } fastio_beet; template <typename T> vector<T> compress(vector<T> v) { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); return v; } template <typename T> map<T, Int> dict(const vector<T> &v) { map<T, Int> res; for (Int i = 0; i < (Int)v.size(); i++) res[v[i]] = i; return res; } template <typename T, typename F> struct SegmentTree { Int n; F f; T ti; vector<T> dat; SegmentTree(){}; SegmentTree(F f, T ti) : f(f), ti(ti) {} void init(Int n_) { n = 1; while (n < n_) n <<= 1; dat.assign(n << 1, ti); } void build(const vector<T> &v) { Int n_ = v.size(); init(n_); for (Int i = 0; i < n_; i++) dat[n + i] = v[i]; for (Int i = n - 1; i; i--) dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]); } void set_val(Int k, T x) { dat[k += n] = x; while (k >>= 1) dat[k] = f(dat[(k << 1) | 0], dat[(k << 1) | 1]); } T query(Int a, Int b) { T vl = ti, vr = ti; for (Int l = a + n, r = b + n; l < r; l >>= 1, r >>= 1) { if (l & 1) vl = f(vl, dat[l++]); if (r & 1) vr = f(dat[--r], vr); } return f(vl, vr); } }; // INSERT ABOVE HERE signed YAHOO2019_FINAL_D() { using M = Mint<Int>; using MM = SquareMatrix<2, M>; Int n, q; cin >> n >> q; vector<Int> ts(q), ls(q), rs(q), ps(q); for (Int i = 0; i < q; i++) { cin >> ts[i]; if (ts[i] == 1) cin >> ps[i]; if (ts[i] == 2) cin >> ls[i] >> rs[i]; } vector<Int> vs; for (Int i = 0; i < q; i++) { if (ts[i] == 1) { vs.emplace_back(ps[i]); vs.emplace_back(ps[i] + 1); vs.emplace_back(ps[i] + 2); vs.emplace_back(ps[i] + 3); } if (ts[i] == 2) { vs.emplace_back(ls[i]); vs.emplace_back(ls[i] + 1); vs.emplace_back(rs[i]); vs.emplace_back(rs[i] + 1); } } vs.emplace_back(0); vs.emplace_back(n + 1); vs = compress(vs); auto ms = dict(vs); MM A; A[0][0] = M(1); A[0][1] = M(1); A[1][0] = M(1); A[1][1] = M(0); vector<MM> vt(vs.size() - 1, A); for (Int i = 0; i + 1 < (Int)vs.size(); i++) { vt[i] = A.pow(vs[i + 1] - vs[i]); } MM I = MM::mul_identity(); auto f = [&](MM a, MM b) { return b * a; }; SegmentTree<MM, decltype(f)> seg(f, I); seg.build(vt); vector<Int> used(vs.size(), 0); for (Int i = 0; i < q; i++) { if (ts[i] == 1) { Int k = ms[ps[i]]; used[k] ^= 1; MM B; for (Int j = k; j <= k + 2; j++) { if (used[j]) { B[0][0] = M(0); B[0][1] = M(0); B[1][0] = M(1); B[1][1] = M(0); } else { B[0][0] = M(!used[j - 1]); B[0][1] = M(!used[j - 2]); B[1][0] = M(1); B[1][1] = M(0); } seg.set_val(j, B); } } if (ts[i] == 2) { Int l = ms[ls[i]], r = ms[rs[i]]; if (used[l]) { cout << 0 << "\n"; continue; } auto B = seg.query(l + 1, r + 1); cout << B[0][0].v << "\n"; } } cout << flush; return 0; } /* verified on 2019/02/23 https://atcoder.jp/contests/yahoo-procon2019-final/tasks/yahoo_procon2019_final_d */ signed DDCC2019_FINAL_D() { string s; cin >> s; struct M { uint32_t v; M() { *this = add_identity(); } M(uint32_t v) : v(v) {} M operator+(const M &a) const { return M(v + a.v); } M operator*(const M &a) const { return M(v * a.v); } static M add_identity() { return M(0); } static M mul_identity() { return M(1); } }; using SM = SquareMatrix<6, M>; auto f = [](SM a, SM b) { return a * b; }; SM ti = SM::mul_identity(); SegmentTree<SM, decltype(f)> seg(f, ti); vector<SM> vt(s.size(), ti); for (int i = 0; i < (int)s.size(); i++) { if (s[i] == 'D') vt[i][0][1] = 1; if (s[i] == 'I') vt[i][1][2] = 1; if (s[i] == 'S') vt[i][2][3] = 1; if (s[i] == 'C') vt[i][3][4] = 1; if (s[i] == 'O') vt[i][4][5] = 1; } seg.build(vt); int q; cin >> q; for (int i = 0; i < q; i++) { int l, r; cin >> l >> r; l--; cout << seg.query(l, r)[0][5].v << "\n"; } cout << flush; return 0; } /* verified on 2019/02/23 https://atcoder.jp/contests/ddcc2019-final/tasks/ddcc2019_final_d */ signed AOJ_2397() { const int MAX = 80; const int MOD = 1e9 + 9; using M = Mint<int, MOD>; using SM = SquareMatrix<MAX, M>; using ll = long long; ll w, h, n; ll cnt = 0; while (cin >> w >> h >> n, w) { vector<ll> x(n), y(n); for (int i = 0; i < n; i++) cin >> x[i] >> y[i]; { using P = pair<ll, ll>; vector<P> vp; for (int i = 0; i < n; i++) vp.emplace_back(y[i], x[i]); sort(vp.begin(), vp.end()); for (int i = 0; i < n; i++) tie(y[i], x[i]) = vp[i]; } SM b; for (int i = 0; i < w; i++) { b[i][i] = M(1); if (i - 1 >= 0) b[i][i - 1] = M(1); if (i + 1 < w) b[i][i + 1] = M(1); } ll d = 1; SM res = SM::mul_identity(); for (int k = 0; k < n; k++) { if (y[k] == d) continue; res = b.pow(y[k] - d - 1) * res; int j = k; SM c(b); while (j < n && y[k] == y[j]) { for (int i = 0; i < w; i++) c[x[j] - 1][i] = 0; j++; } res = c * res; d = y[k]; } res = b.pow(h - d) * res; cout << "Case " << ++cnt << ": " << res[w - 1][0].v << endl; } return 0; } /* verified on 2019/04/11 http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2397 */ signed AOJ_2432() { const int MAX = 160; const int INF = 1e8; struct M { int v; M() { *this = add_identity(); } M(int v) : v(v) {} M operator+(const M &a) const { return M(max(v, a.v)); } M operator*(const M &a) const { return M(v + a.v); } static M add_identity() { return M(-INF); } static M mul_identity() { return M(0); } }; using SM = SquareMatrix<MAX, M>; int n, m, k; cin >> n >> m >> k; vector<SM> vs(1), ws(1); for (int i = 0; i < n; i++) { vs[0][i][i] = 0; ws[0][i][i] = 0; } for (int i = 0; i < m; i++) { int a, b, c; cin >> a >> b >> c; chmax(vs[0][a][b].v, c); } for (int t = 0; t < 20; t++) { SM tv = vs[t] * vs[t]; SM tw = vs[t] * ws[t]; vs.emplace_back(tv); ws.emplace_back(tw); } auto len = [&](SM &sm) -> int { int res = 0; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) chmax(res, sm[i][j].v); return res; }; if (len(vs.back()) < k) { cout << -1 << endl; return 0; } int ans = 0; SM res(ws[0]); for (int t = 20; t >= 0; t--) { SM tmp = res * ws[t]; SM nxt = res * vs[t]; if (len(tmp) < k) { res.dat = nxt.dat; ans += 1 << t; } } cout << ans << endl; if (ans > 100) return 0; int dp[MAX][MAX]; int pr[MAX][MAX]; for (int i = 0; i < MAX; i++) { for (int j = 0; j < MAX; j++) { dp[i][j] = -INF; pr[i][j] = -1; } } for (int v = 0; v < n; v++) dp[0][v] = 0; for (int i = 0; i < ans; i++) { for (int v = 0; v < n; v++) { for (int u = 0; u < n; u++) { if (dp[i + 1][u] < dp[i][v] + vs[0][v][u].v) { dp[i + 1][u] = dp[i][v] + vs[0][v][u].v; pr[i + 1][u] = v; } } } } int pos = 0; for (int v = 0; v < n; v++) if (dp[ans][v] > dp[ans][pos]) pos = v; vector<int> vx; for (int i = ans; i >= 0; i--) { vx.emplace_back(pos); pos = pr[i][pos]; } reverse(vx.begin(), vx.end()); for (int i = 0; i <= ans; i++) { if (i) cout << " "; cout << vx[i]; } cout << endl; return 0; } /* verified on 2019/04/11 http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2432 */ signed main() { // YAHOO2019_FINAL_D(); // DDCC2019_FINAL_D(); AOJ_2397(); // AOJ_2432(); return 0; }
#include <bits/stdc++.h> using namespace std; using Int = long long; 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; } // BEGIN CUT HERE template <size_t N, typename R> struct SquareMatrix { typedef array<R, N> arr; typedef array<arr, N> mat; mat dat; SquareMatrix() { for (size_t i = 0; i < N; i++) for (size_t j = 0; j < N; j++) dat[i][j] = R::add_identity(); } SquareMatrix &operator=(const SquareMatrix &a) { dat = a.dat; return (*this); } bool operator==(const SquareMatrix &a) const { return dat == a.dat; } size_t size() const { return N; }; arr &operator[](size_t k) { return dat[k]; }; const arr &operator[](size_t k) const { return dat[k]; }; static SquareMatrix add_identity() { return SquareMatrix(); } static SquareMatrix mul_identity() { SquareMatrix res; for (size_t i = 0; i < N; i++) res[i][i] = R::mul_identity(); return res; } SquareMatrix operator*(const SquareMatrix &B) const { SquareMatrix res; for (size_t i = 0; i < N; i++) for (size_t j = 0; j < N; j++) for (size_t k = 0; k < N; k++) res[i][j] = res[i][j] + (dat[i][k] * B[k][j]); return res; } SquareMatrix operator+(const SquareMatrix &B) const { SquareMatrix res; for (size_t i = 0; i < N; i++) for (size_t j = 0; j < N; j++) res[i][j] = dat[i][j] + B[i][j]; return res; } SquareMatrix pow(long long n) const { SquareMatrix a = *this, res = mul_identity(); while (n) { if (n & 1) res = res * a; a = a * a; n >>= 1; } return res; } }; // END CUT HERE template <typename T, T MOD = 1000000007> struct Mint { T v; Mint() : v(0) {} Mint(signed v) : v(v) {} Mint(long long t) { v = t % MOD; if (v < 0) v += MOD; } Mint pow(long long k) { Mint res(1), tmp(v); while (k) { if (k & 1) res *= tmp; tmp *= tmp; k >>= 1; } return res; } static Mint add_identity() { return Mint(0); } static Mint mul_identity() { return Mint(1); } Mint inv() { return pow(MOD - 2); } Mint &operator+=(Mint a) { v += a.v; if (v >= MOD) v -= MOD; return *this; } Mint &operator-=(Mint a) { v += MOD - a.v; if (v >= MOD) v -= MOD; return *this; } Mint &operator*=(Mint a) { v = 1LL * v * a.v % MOD; return *this; } Mint &operator/=(Mint a) { return (*this) *= a.inv(); } Mint operator+(Mint a) const { return Mint(v) += a; }; Mint operator-(Mint a) const { return Mint(v) -= a; }; Mint operator*(Mint a) const { return Mint(v) *= a; }; Mint operator/(Mint a) const { return Mint(v) /= a; }; Mint operator-() { return v ? MOD - v : v; } bool operator==(const Mint a) const { return v == a.v; } bool operator!=(const Mint a) const { return v != a.v; } bool operator<(const Mint a) const { return v < a.v; } }; struct FastIO { FastIO() { cin.tie(0); ios::sync_with_stdio(0); } } fastio_beet; template <typename T> vector<T> compress(vector<T> v) { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); return v; } template <typename T> map<T, Int> dict(const vector<T> &v) { map<T, Int> res; for (Int i = 0; i < (Int)v.size(); i++) res[v[i]] = i; return res; } template <typename T, typename F> struct SegmentTree { Int n; F f; T ti; vector<T> dat; SegmentTree(){}; SegmentTree(F f, T ti) : f(f), ti(ti) {} void init(Int n_) { n = 1; while (n < n_) n <<= 1; dat.assign(n << 1, ti); } void build(const vector<T> &v) { Int n_ = v.size(); init(n_); for (Int i = 0; i < n_; i++) dat[n + i] = v[i]; for (Int i = n - 1; i; i--) dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]); } void set_val(Int k, T x) { dat[k += n] = x; while (k >>= 1) dat[k] = f(dat[(k << 1) | 0], dat[(k << 1) | 1]); } T query(Int a, Int b) { T vl = ti, vr = ti; for (Int l = a + n, r = b + n; l < r; l >>= 1, r >>= 1) { if (l & 1) vl = f(vl, dat[l++]); if (r & 1) vr = f(dat[--r], vr); } return f(vl, vr); } }; // INSERT ABOVE HERE signed YAHOO2019_FINAL_D() { using M = Mint<Int>; using MM = SquareMatrix<2, M>; Int n, q; cin >> n >> q; vector<Int> ts(q), ls(q), rs(q), ps(q); for (Int i = 0; i < q; i++) { cin >> ts[i]; if (ts[i] == 1) cin >> ps[i]; if (ts[i] == 2) cin >> ls[i] >> rs[i]; } vector<Int> vs; for (Int i = 0; i < q; i++) { if (ts[i] == 1) { vs.emplace_back(ps[i]); vs.emplace_back(ps[i] + 1); vs.emplace_back(ps[i] + 2); vs.emplace_back(ps[i] + 3); } if (ts[i] == 2) { vs.emplace_back(ls[i]); vs.emplace_back(ls[i] + 1); vs.emplace_back(rs[i]); vs.emplace_back(rs[i] + 1); } } vs.emplace_back(0); vs.emplace_back(n + 1); vs = compress(vs); auto ms = dict(vs); MM A; A[0][0] = M(1); A[0][1] = M(1); A[1][0] = M(1); A[1][1] = M(0); vector<MM> vt(vs.size() - 1, A); for (Int i = 0; i + 1 < (Int)vs.size(); i++) { vt[i] = A.pow(vs[i + 1] - vs[i]); } MM I = MM::mul_identity(); auto f = [&](MM a, MM b) { return b * a; }; SegmentTree<MM, decltype(f)> seg(f, I); seg.build(vt); vector<Int> used(vs.size(), 0); for (Int i = 0; i < q; i++) { if (ts[i] == 1) { Int k = ms[ps[i]]; used[k] ^= 1; MM B; for (Int j = k; j <= k + 2; j++) { if (used[j]) { B[0][0] = M(0); B[0][1] = M(0); B[1][0] = M(1); B[1][1] = M(0); } else { B[0][0] = M(!used[j - 1]); B[0][1] = M(!used[j - 2]); B[1][0] = M(1); B[1][1] = M(0); } seg.set_val(j, B); } } if (ts[i] == 2) { Int l = ms[ls[i]], r = ms[rs[i]]; if (used[l]) { cout << 0 << "\n"; continue; } auto B = seg.query(l + 1, r + 1); cout << B[0][0].v << "\n"; } } cout << flush; return 0; } /* verified on 2019/02/23 https://atcoder.jp/contests/yahoo-procon2019-final/tasks/yahoo_procon2019_final_d */ signed DDCC2019_FINAL_D() { string s; cin >> s; struct M { uint32_t v; M() { *this = add_identity(); } M(uint32_t v) : v(v) {} M operator+(const M &a) const { return M(v + a.v); } M operator*(const M &a) const { return M(v * a.v); } static M add_identity() { return M(0); } static M mul_identity() { return M(1); } }; using SM = SquareMatrix<6, M>; auto f = [](SM a, SM b) { return a * b; }; SM ti = SM::mul_identity(); SegmentTree<SM, decltype(f)> seg(f, ti); vector<SM> vt(s.size(), ti); for (int i = 0; i < (int)s.size(); i++) { if (s[i] == 'D') vt[i][0][1] = 1; if (s[i] == 'I') vt[i][1][2] = 1; if (s[i] == 'S') vt[i][2][3] = 1; if (s[i] == 'C') vt[i][3][4] = 1; if (s[i] == 'O') vt[i][4][5] = 1; } seg.build(vt); int q; cin >> q; for (int i = 0; i < q; i++) { int l, r; cin >> l >> r; l--; cout << seg.query(l, r)[0][5].v << "\n"; } cout << flush; return 0; } /* verified on 2019/02/23 https://atcoder.jp/contests/ddcc2019-final/tasks/ddcc2019_final_d */ signed AOJ_2397() { const int MAX = 75; const int MOD = 1e9 + 9; using M = Mint<int, MOD>; using SM = SquareMatrix<MAX, M>; using ll = long long; ll w, h, n; ll cnt = 0; while (cin >> w >> h >> n, w) { vector<ll> x(n), y(n); for (int i = 0; i < n; i++) cin >> x[i] >> y[i]; { using P = pair<ll, ll>; vector<P> vp; for (int i = 0; i < n; i++) vp.emplace_back(y[i], x[i]); sort(vp.begin(), vp.end()); for (int i = 0; i < n; i++) tie(y[i], x[i]) = vp[i]; } SM b; for (int i = 0; i < w; i++) { b[i][i] = M(1); if (i - 1 >= 0) b[i][i - 1] = M(1); if (i + 1 < w) b[i][i + 1] = M(1); } ll d = 1; SM res = SM::mul_identity(); for (int k = 0; k < n; k++) { if (y[k] == d) continue; res = b.pow(y[k] - d - 1) * res; int j = k; SM c(b); while (j < n && y[k] == y[j]) { for (int i = 0; i < w; i++) c[x[j] - 1][i] = 0; j++; } res = c * res; d = y[k]; } res = b.pow(h - d) * res; cout << "Case " << ++cnt << ": " << res[w - 1][0].v << endl; } return 0; } /* verified on 2019/04/11 http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2397 */ signed AOJ_2432() { const int MAX = 160; const int INF = 1e8; struct M { int v; M() { *this = add_identity(); } M(int v) : v(v) {} M operator+(const M &a) const { return M(max(v, a.v)); } M operator*(const M &a) const { return M(v + a.v); } static M add_identity() { return M(-INF); } static M mul_identity() { return M(0); } }; using SM = SquareMatrix<MAX, M>; int n, m, k; cin >> n >> m >> k; vector<SM> vs(1), ws(1); for (int i = 0; i < n; i++) { vs[0][i][i] = 0; ws[0][i][i] = 0; } for (int i = 0; i < m; i++) { int a, b, c; cin >> a >> b >> c; chmax(vs[0][a][b].v, c); } for (int t = 0; t < 20; t++) { SM tv = vs[t] * vs[t]; SM tw = vs[t] * ws[t]; vs.emplace_back(tv); ws.emplace_back(tw); } auto len = [&](SM &sm) -> int { int res = 0; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) chmax(res, sm[i][j].v); return res; }; if (len(vs.back()) < k) { cout << -1 << endl; return 0; } int ans = 0; SM res(ws[0]); for (int t = 20; t >= 0; t--) { SM tmp = res * ws[t]; SM nxt = res * vs[t]; if (len(tmp) < k) { res.dat = nxt.dat; ans += 1 << t; } } cout << ans << endl; if (ans > 100) return 0; int dp[MAX][MAX]; int pr[MAX][MAX]; for (int i = 0; i < MAX; i++) { for (int j = 0; j < MAX; j++) { dp[i][j] = -INF; pr[i][j] = -1; } } for (int v = 0; v < n; v++) dp[0][v] = 0; for (int i = 0; i < ans; i++) { for (int v = 0; v < n; v++) { for (int u = 0; u < n; u++) { if (dp[i + 1][u] < dp[i][v] + vs[0][v][u].v) { dp[i + 1][u] = dp[i][v] + vs[0][v][u].v; pr[i + 1][u] = v; } } } } int pos = 0; for (int v = 0; v < n; v++) if (dp[ans][v] > dp[ans][pos]) pos = v; vector<int> vx; for (int i = ans; i >= 0; i--) { vx.emplace_back(pos); pos = pr[i][pos]; } reverse(vx.begin(), vx.end()); for (int i = 0; i <= ans; i++) { if (i) cout << " "; cout << vx[i]; } cout << endl; return 0; } /* verified on 2019/04/11 http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2432 */ signed main() { // YAHOO2019_FINAL_D(); // DDCC2019_FINAL_D(); AOJ_2397(); // AOJ_2432(); return 0; }
replace
326
327
326
327
TLE
p01511
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <fstream> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define REP(i, n) for (int i = 0; i < n; ++i) #define FOR(i, a, b) for (int i = a; i <= b; ++i) #define FORR(i, a, b) for (int i = a; i >= b; --i) typedef long long ll; typedef vector<int> VI; typedef vector<ll> VL; typedef vector<VI> VVI; typedef pair<int, int> P; typedef pair<ll, ll> PL; const ll MOD = 1e9 + 9; VL matvec(vector<VL> A, VL x) { int n = x.size(); VL y(n); REP(i, n) REP(j, n) { y[i] = (y[i] + A[i][j] * x[j]) % MOD; } return y; } vector<VL> matprod(vector<VL> A, vector<VL> B) { int n = A.size(); vector<VL> C(n, VL(n)); REP(i, n) REP(j, n) REP(k, n) { C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % MOD; } return C; } vector<VL> matpow(vector<VL> A, ll p) { int n = A.size(); vector<VL> B(n, VL(n)); REP(i, n) B[i][i] = 1; while (p) { if (p & 1) B = matprod(B, A); A = matprod(A, A); p /= 2; } return B; } int main() { int w, n, t = 0; ll h; while (cin >> w >> h >> n && w) { t++; printf("Case %d: ", t); vector<PL> p(n); REP(i, n) { cin >> p[i].second >> p[i].first; p[i].first--; p[i].second--; } sort(p.begin(), p.end()); VL a; a.push_back(0); REP(i, n) { if (a[a.size() - 1] != p[i].first) a.push_back(p[i].first); } a.push_back(h - 1); int m = a.size(); VL x(w); x[0] = 1; REP(i, m - 1) { vector<VL> A(w, VL(w)); REP(k, w) { A[k][k] = 1; A[k][min(w - 1, k + 1)] = 1; A[min(w - 1, k + 1)][k] = 1; } A = matpow(A, a[i + 1] - a[i]); x = matvec(A, x); REP(j, m) { if (p[j].first == a[i + 1]) x[p[j].second] = 0; } } cout << x[w - 1] << endl; } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <fstream> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define REP(i, n) for (int i = 0; i < n; ++i) #define FOR(i, a, b) for (int i = a; i <= b; ++i) #define FORR(i, a, b) for (int i = a; i >= b; --i) typedef long long ll; typedef vector<int> VI; typedef vector<ll> VL; typedef vector<VI> VVI; typedef pair<int, int> P; typedef pair<ll, ll> PL; const ll MOD = 1e9 + 9; VL matvec(vector<VL> A, VL x) { int n = x.size(); VL y(n); REP(i, n) REP(j, n) { y[i] = (y[i] + A[i][j] * x[j]) % MOD; } return y; } vector<VL> matprod(vector<VL> A, vector<VL> B) { int n = A.size(); vector<VL> C(n, VL(n)); REP(i, n) REP(j, n) REP(k, n) { C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % MOD; } return C; } vector<VL> matpow(vector<VL> A, ll p) { int n = A.size(); vector<VL> B(n, VL(n)); REP(i, n) B[i][i] = 1; while (p) { if (p & 1) B = matprod(B, A); A = matprod(A, A); p /= 2; } return B; } int main() { int w, n, t = 0; ll h; while (cin >> w >> h >> n && w) { t++; printf("Case %d: ", t); vector<PL> p(n); REP(i, n) { cin >> p[i].second >> p[i].first; p[i].first--; p[i].second--; } sort(p.begin(), p.end()); VL a; a.push_back(0); REP(i, n) { if (a[a.size() - 1] != p[i].first) a.push_back(p[i].first); } a.push_back(h - 1); int m = a.size(); VL x(w); x[0] = 1; REP(i, m - 1) { vector<VL> A(w, VL(w)); REP(k, w) { A[k][k] = 1; A[k][min(w - 1, k + 1)] = 1; A[min(w - 1, k + 1)][k] = 1; } A = matpow(A, a[i + 1] - a[i]); x = matvec(A, x); REP(j, n) { if (p[j].first == a[i + 1]) x[p[j].second] = 0; } } cout << x[w - 1] << endl; } return 0; }
replace
89
90
89
90
0
p01511
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define int long long #define r(i, n) for (int i = 0; i < n; i++) using namespace std; typedef pair<int, int> P; typedef vector<vector<int>> mat; int h, w, n; inline void mat_mul(mat &A, mat &B, int M = 1000000009) { mat ret(w, vector<int>(w)); for (int i = 0; i < w; ++i) { for (int j = 0; j < w; ++j) { ret[i][j] = 0; for (int k = 0; k < w; ++k) { ret[i][j] += (A[i][k] * B[k][j]) % M; ret[i][j] %= M; } } } A = ret; } mat mat_pow(mat &A, int N, int M = 1000000009) { mat ret(A.size(), vector<int>(A.size(), 0)), t = A; for (int i = 0; i < A.size(); ++i) ret[i][i] = 1; int k = 0; while (N > 0) { if (N & 1) { mat_mul(ret, t, M); } mat_mul(t, t, M); N >>= 1; } return ret; } mat XXX; void init() { mat res(w, vector<int>(w, 0)); for (int i = 0; i < w; i++) { for (int j = 0; j < w; j++) { if (i == j) { if (i) res[i - 1][j] = 1; res[i][j] = 1; if (i != w - 1) res[i + 1][j] = 1; } } } XXX = res; } inline mat make_mat(vector<int> &V) { mat res = XXX; for (int i = 0; i < V.size(); i++) { for (int j = 0; j < w; j++) { res[j][V[i]] = 0; } } return res; } signed main() { init(); int cnt = 0; while (cin >> w >> h >> n, w) { init(); cout << "Case " << (++cnt) << ": "; vector<P> v; vector<int> v1; r(i, n) { int x, y; cin >> x >> y; x--; y--; if (y == 0) v1.push_back(x); else v.push_back(P(y, x)); } v.push_back(P(1e18 + 1, 0)); int y = 0; sort(v.begin(), v.end()); mat A(w, vector<int>(w, 0)); A[0][0] = 1; r(i, v1.size()) A[v1[i]][v1[i]] = 0; for (int i = 0; i < v.size(); i++) { int s = v[i].first; int c = s - y; vector<int> vs, kara; vs.push_back(v[i].second); while (v[i].first == v[i + 1].first) { vs.push_back(v[i + 1].second); i++; } mat B = make_mat(vs); mat C = make_mat(kara); if (s == 1e18 + 1) { c = (h - 1) - y; if (c) { C = mat_pow(C, c); mat_mul(A, C); } break; } if (c > 1) { C = mat_pow(C, c - 1); mat_mul(A, C); } mat_mul(A, B); y = s; } int ans = 0; r(i, w) ans = (ans + A[i][w - 1]) % 1000000009; cout << ans << endl; } }
#include <bits/stdc++.h> #define int long long #define r(i, n) for (int i = 0; i < n; i++) using namespace std; typedef pair<int, int> P; typedef vector<vector<int>> mat; int h, w, n; inline void mat_mul(mat &A, mat &B, int M = 1000000009) { mat ret(w, vector<int>(w)); for (int i = 0; i < w; ++i) { for (int j = 0; j < w; ++j) { ret[i][j] = 0; for (int k = 0; k < w; ++k) { ret[i][j] += (A[i][k] * B[k][j]) % M; ret[i][j] %= M; } } } A = ret; } inline mat mat_pow(mat &A, int N, int M = 1000000009) { mat ret(A.size(), vector<int>(A.size(), 0)), t = A; for (int i = 0; i < A.size(); ++i) ret[i][i] = 1; int k = 0; while (N > 0) { if (N & 1) { mat_mul(ret, t, M); } mat_mul(t, t, M); N >>= 1; } return ret; } mat XXX; void init() { mat res(w, vector<int>(w, 0)); for (int i = 0; i < w; i++) { for (int j = 0; j < w; j++) { if (i == j) { if (i) res[i - 1][j] = 1; res[i][j] = 1; if (i != w - 1) res[i + 1][j] = 1; } } } XXX = res; } inline mat make_mat(vector<int> &V) { mat res = XXX; for (int i = 0; i < V.size(); i++) { for (int j = 0; j < w; j++) { res[j][V[i]] = 0; } } return res; } signed main() { init(); int cnt = 0; while (cin >> w >> h >> n, w) { init(); cout << "Case " << (++cnt) << ": "; vector<P> v; vector<int> v1; r(i, n) { int x, y; cin >> x >> y; x--; y--; if (y == 0) v1.push_back(x); else v.push_back(P(y, x)); } v.push_back(P(1e18 + 1, 0)); int y = 0; sort(v.begin(), v.end()); mat A(w, vector<int>(w, 0)); A[0][0] = 1; r(i, v1.size()) A[v1[i]][v1[i]] = 0; for (int i = 0; i < v.size(); i++) { int s = v[i].first; int c = s - y; vector<int> vs, kara; vs.push_back(v[i].second); while (v[i].first == v[i + 1].first) { vs.push_back(v[i + 1].second); i++; } mat B = make_mat(vs); mat C = make_mat(kara); if (s == 1e18 + 1) { c = (h - 1) - y; if (c) { C = mat_pow(C, c); mat_mul(A, C); } break; } if (c > 1) { C = mat_pow(C, c - 1); mat_mul(A, C); } mat_mul(A, B); y = s; } int ans = 0; r(i, w) ans = (ans + A[i][w - 1]) % 1000000009; cout << ans << endl; } }
replace
21
22
21
22
TLE
p01511
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define int long long #define r(i, n) for (int i = 0; i < n; i++) using namespace std; typedef pair<int, int> P; typedef vector<vector<int>> mat; int h, w, n; inline void mat_mul(mat &A, mat &B, int M = 1000000009) { mat ret(w, vector<int>(w)); for (int i = 0; i < w; ++i) { for (int j = 0; j < w; ++j) { ret[i][j] = 0; for (int k = 0; k < w; ++k) { ret[i][j] += (A[i][k] * B[k][j]) % M; ret[i][j] %= M; } } } A = ret; } void mat_pow(mat &A, int N, int M = 1000000009) { mat ret(A.size(), vector<int>(A.size(), 0)), t = A; for (int i = 0; i < A.size(); ++i) ret[i][i] = 1; int k = 0; while (N > 0) { if (N & 1) { mat_mul(ret, t, M); } mat_mul(t, t, M); N >>= 1; } A = ret; } mat XXX; void init() { mat res(w, vector<int>(w, 0)); for (int i = 0; i < w; i++) { for (int j = 0; j < w; j++) { if (i == j) { if (i) res[i - 1][j] = 1; res[i][j] = 1; if (i != w - 1) res[i + 1][j] = 1; } } } XXX = res; } inline mat make_mat(vector<int> &V) { mat res = XXX; for (int i = 0; i < V.size(); i++) { for (int j = 0; j < w; j++) { res[j][V[i]] = 0; } } return res; } signed main() { init(); int cnt = 0; while (cin >> w >> h >> n, w) { init(); cout << "Case " << (++cnt) << ": "; vector<P> v; vector<int> v1; r(i, n) { int x, y; cin >> x >> y; x--; y--; if (y == 0) v1.push_back(x); else v.push_back(P(y, x)); } v.push_back(P(1e18 + 1, 0)); int y = 0; sort(v.begin(), v.end()); mat A(w, vector<int>(w, 0)); A[0][0] = 1; r(i, v1.size()) A[v1[i]][v1[i]] = 0; for (int i = 0; i < v.size(); i++) { int s = v[i].first; int c = s - y; vector<int> vs, kara; vs.push_back(v[i].second); while (v[i].first == v[i + 1].first) { vs.push_back(v[i + 1].second); i++; } mat B = make_mat(vs); mat C = make_mat(kara); if (s == 1e18 + 1) { c = (h - 1) - y; if (c) { mat_pow(C, c); mat_mul(A, C); } break; } if (c > 1) { mat_pow(C, c - 1); mat_mul(A, C); } mat_mul(A, B); y = s; } int ans = 0; r(i, w) ans = (ans + A[i][w - 1]) % 1000000009; cout << ans << endl; } }
#include <bits/stdc++.h> #define int long long #define r(i, n) for (int i = 0; i < n; i++) using namespace std; typedef pair<int, int> P; typedef vector<vector<int>> mat; int h, w, n; inline void mat_mul(mat &A, mat &B, int M = 1000000009) { mat ret(w, vector<int>(w)); for (int i = 0; i < w; ++i) { for (int j = 0; j < w; ++j) { ret[i][j] = 0; for (int k = 0; k < w; ++k) { ret[i][j] += (A[i][k] * B[k][j]) % M; ret[i][j] %= M; } } } A = ret; } inline void mat_pow(mat &A, int N, int M = 1000000009) { mat ret(A.size(), vector<int>(A.size(), 0)), t = A; for (int i = 0; i < A.size(); ++i) ret[i][i] = 1; int k = 0; while (N > 0) { if (N & 1) { mat_mul(ret, t, M); } mat_mul(t, t, M); N >>= 1; } A = ret; } mat XXX; void init() { mat res(w, vector<int>(w, 0)); for (int i = 0; i < w; i++) { for (int j = 0; j < w; j++) { if (i == j) { if (i) res[i - 1][j] = 1; res[i][j] = 1; if (i != w - 1) res[i + 1][j] = 1; } } } XXX = res; } inline mat make_mat(vector<int> &V) { mat res = XXX; for (int i = 0; i < V.size(); i++) { for (int j = 0; j < w; j++) { res[j][V[i]] = 0; } } return res; } signed main() { init(); int cnt = 0; while (cin >> w >> h >> n, w) { init(); cout << "Case " << (++cnt) << ": "; vector<P> v; vector<int> v1; r(i, n) { int x, y; cin >> x >> y; x--; y--; if (y == 0) v1.push_back(x); else v.push_back(P(y, x)); } v.push_back(P(1e18 + 1, 0)); int y = 0; sort(v.begin(), v.end()); mat A(w, vector<int>(w, 0)); A[0][0] = 1; r(i, v1.size()) A[v1[i]][v1[i]] = 0; for (int i = 0; i < v.size(); i++) { int s = v[i].first; int c = s - y; vector<int> vs, kara; vs.push_back(v[i].second); while (v[i].first == v[i + 1].first) { vs.push_back(v[i + 1].second); i++; } mat B = make_mat(vs); mat C = make_mat(kara); if (s == 1e18 + 1) { c = (h - 1) - y; if (c) { mat_pow(C, c); mat_mul(A, C); } break; } if (c > 1) { mat_pow(C, c - 1); mat_mul(A, C); } mat_mul(A, B); y = s; } int ans = 0; r(i, w) ans = (ans + A[i][w - 1]) % 1000000009; cout << ans << endl; } }
replace
21
22
21
22
TLE
p01520
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, a, n) for (int i = a; i < n; i++) #define repb(i, a, b) for (int i = a; i >= b; i--) #define all(a) a.begin(), a.end() #define o(a) cout << a << endl #define int long long #define fi first #define se second using namespace std; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> pii; signed main() { int n, t, e; cin >> n >> t >> e; vi x(n); rep(i, 0, n) cin >> x[i]; int ans = -1; rep(i, 0, n) { for (int j = 1;; j++) { if (t - e <= j * x[i] && j * x[i] <= t + e) { ans = i + 1; i = n; } if (j * x[i] > t + e) break; } } cout << ans << endl; }
#include <bits/stdc++.h> #define rep(i, a, n) for (int i = a; i < n; i++) #define repb(i, a, b) for (int i = a; i >= b; i--) #define all(a) a.begin(), a.end() #define o(a) cout << a << endl #define int long long #define fi first #define se second using namespace std; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> pii; signed main() { int n, t, e; cin >> n >> t >> e; vi x(n); rep(i, 0, n) cin >> x[i]; int ans = -1; rep(i, 0, n) { for (int j = 1;; j++) { if (t - e <= j * x[i] && j * x[i] <= t + e) { ans = i + 1; i = n; break; } if (j * x[i] > t + e) break; } } cout << ans << endl; }
insert
24
24
24
25
TLE
p01522
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) #define repc(i, s, e) for (int i = (s); i < (e); i++) #define pb(n) push_back((n)) #define mp(n, m) make_pair((n), (m)) #define all(r) r.begin(), r.end() #define fi first #define se second typedef long long ll; typedef vector<int> vi; typedef vector<vi> vii; typedef vector<ll> vl; typedef vector<vl> vll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int INF = 10000000; const int MAX_V = 1000; int par_uft[MAX_V]; int rank_uft[MAX_V]; void init_uft(int n) { int i; for (i = 0; i < n; i++) { par_uft[i] = i; rank_uft[i] = 0; } } int find_uft(int x) { if (par_uft[x] == x) { return x; } else { return par_uft[x] = find_uft(par_uft[x]); } } void unite_uft(int x, int y) { x = find_uft(x); y = find_uft(y); if (x == y) return; if (rank_uft[x] < rank_uft[y]) { par_uft[x] = y; } else { par_uft[y] = x; if (rank_uft[x] == rank_uft[y]) { rank_uft[x]++; } } } bool same_uft(int x, int y) { return find_uft(x) == find_uft(y); } int main() { int n, k; cin >> n >> k; init_uft(n + 1); int m, t, s; for (int i = 0; i < k; i++) { cin >> m >> s; for (int j = 0; j < m - 1; j++) { cin >> t; unite_uft(s, t); } } int cnt = 0; cin >> m; vi v(n + 1, 0); for (int i = 0; i < n; i++) { cin >> s >> t; if (same_uft(s, t)) { if (!v[s]) { v[s] = 1; cnt++; } if (!v[t]) { v[t] = 1; cnt++; } } } cout << cnt << endl; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) #define repc(i, s, e) for (int i = (s); i < (e); i++) #define pb(n) push_back((n)) #define mp(n, m) make_pair((n), (m)) #define all(r) r.begin(), r.end() #define fi first #define se second typedef long long ll; typedef vector<int> vi; typedef vector<vi> vii; typedef vector<ll> vl; typedef vector<vl> vll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int INF = 10000000; const int MAX_V = 1000; int par_uft[MAX_V]; int rank_uft[MAX_V]; void init_uft(int n) { int i; for (i = 0; i < n; i++) { par_uft[i] = i; rank_uft[i] = 0; } } int find_uft(int x) { if (par_uft[x] == x) { return x; } else { return par_uft[x] = find_uft(par_uft[x]); } } void unite_uft(int x, int y) { x = find_uft(x); y = find_uft(y); if (x == y) return; if (rank_uft[x] < rank_uft[y]) { par_uft[x] = y; } else { par_uft[y] = x; if (rank_uft[x] == rank_uft[y]) { rank_uft[x]++; } } } bool same_uft(int x, int y) { return find_uft(x) == find_uft(y); } int main() { int n, k; cin >> n >> k; init_uft(n + 1); int m, t, s; for (int i = 0; i < k; i++) { cin >> m >> s; for (int j = 0; j < m - 1; j++) { cin >> t; unite_uft(s, t); } } int cnt = 0; cin >> m; vi v(n + 1, 0); for (int i = 0; i < m; i++) { cin >> s >> t; if (same_uft(s, t)) { if (!v[s]) { v[s] = 1; cnt++; } if (!v[t]) { v[t] = 1; cnt++; } } } cout << cnt << endl; }
replace
90
91
90
91
0
p01522
C++
Runtime Error
#include <iostream> #include <set> using namespace std; int main() { int n, k, r; int m[50], p[50], q[50]; int bunny[50][50]; int count = 0; set<int> all, selected; cin >> n >> k; for (int i = 0; i < k; ++i) { cin >> m[i]; for (int j = 0; j < m[i]; ++j) { cin >> bunny[i][j]; } } cin >> r; for (int i = 0; i < r; ++i) { cin >> p[i] >> q[i]; } for (int i = 0; i < k; ++i) { all.clear(); selected.clear(); for (int j = 0; j < m[i]; ++j) { all.insert(bunny[i][j]); } for (int j = 0; j < r; ++j) { if (all.count(p[j]) * all.count(q[j]) > 0) { selected.insert(p[j]); selected.insert(q[j]); } } count += selected.size(); } cout << count << endl; return 0; }
#include <iostream> #include <set> using namespace std; int main() { int n, k, r; int m[50], p[5000], q[5000]; int bunny[50][50]; int count = 0; set<int> all, selected; cin >> n >> k; for (int i = 0; i < k; ++i) { cin >> m[i]; for (int j = 0; j < m[i]; ++j) { cin >> bunny[i][j]; } } cin >> r; for (int i = 0; i < r; ++i) { cin >> p[i] >> q[i]; } for (int i = 0; i < k; ++i) { all.clear(); selected.clear(); for (int j = 0; j < m[i]; ++j) { all.insert(bunny[i][j]); } for (int j = 0; j < r; ++j) { if (all.count(p[j]) * all.count(q[j]) > 0) { selected.insert(p[j]); selected.insert(q[j]); } } count += selected.size(); } cout << count << endl; return 0; }
replace
7
8
7
8
0
p01523
C++
Time Limit Exceeded
#include <stdio.h> int n, m, a[100], b[100]; int main() { scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) scanf("%d%d", &a[i], &b[i]); int l = 1, c = 0; while (l <= n) { int ptr = -1; for (int j = 0; j < m; j++) { if (a[j] <= l) ptr = j; } if (ptr == -1) { printf("Impossible\n"); return 0; } l = b[ptr] + 1; c++; } printf("%d\n", c); return 0; }
#include <stdio.h> int n, m, a[100], b[100]; int main() { scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) scanf("%d%d", &a[i], &b[i]); int l = 1, c = 0; while (l <= n) { int ptr = -1; for (int j = 0; j < m; j++) { if (a[j] <= l && b[j] >= l && b[ptr] < b[j]) ptr = j; } if (ptr == -1) { printf("Impossible\n"); return 0; } l = b[ptr] + 1; c++; } printf("%d\n", c); return 0; }
replace
10
11
10
11
TLE
p01523
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <fstream> #include <iomanip> #include <iostream> #include <iterator> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> // #include <utility> // #include <memory> // #include <functional> // #include <deque> // #include <cctype> // #include <ctime> // #include <numeric> // #include <list> // #include <iomanip> // #if __cplusplus >= 201103L // #include <array> // #include <tuple> // #include <initializer_list> // #include <forward_list> // // #define cauto const auto& // #else // #endif using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vint; typedef vector<vector<int>> vvint; typedef vector<long long> vll, vLL; typedef vector<vector<long long>> vvll, vvLL; #define VV(T) vector<vector<T>> template <class T> void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) { v.assign(a, vector<T>(b, t)); } template <class F, class T> void convert(const F &f, T &t) { stringstream ss; ss << f; ss >> t; } #undef _P #define _P(...) (void)printf(__VA_ARGS__) #define reep(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) reep((i), 0, (n)) #define ALL(v) (v).begin(), (v).end() #define PB push_back #define F first #define S second #define mkp make_pair #define RALL(v) (v).rbegin(), (v).rend() #define DEBUG #ifdef DEBUG #define dump(x) cout << #x << " = " << (x) << endl; #define debug(x) \ cout << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl; #else #define dump(x) #define debug(x) #endif #define LDcout(x, n) fixed << setprecision(n) << x #define MOD 1000000007LL #define EPS 1e-8 static const int INF = 1 << 24; void mainmain() { int n, m; cin >> n >> m; vint v(n, 0); vector<pii> w(m); rep(i, m) { cin >> w[i].F >> w[i].S; w[i].F--; w[i].S--; reep(j, w[i].F, w[i].S + 1) { v[j]++; } } rep(i, n) { if (v[i] == 0) { cout << "Impossible" << endl; return; } } int ans = 0; int pos = 0; while (pos < n) { int t = 0; ans++; rep(i, n) { if (w[i].F <= pos && pos <= w[i].S) { t = max(t, w[i].S); } } pos = t + 1; } cout << ans << endl; } signed main() { ios_base::sync_with_stdio(false); cout << fixed << setprecision(0); mainmain(); }
#include <algorithm> #include <bitset> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <fstream> #include <iomanip> #include <iostream> #include <iterator> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> // #include <utility> // #include <memory> // #include <functional> // #include <deque> // #include <cctype> // #include <ctime> // #include <numeric> // #include <list> // #include <iomanip> // #if __cplusplus >= 201103L // #include <array> // #include <tuple> // #include <initializer_list> // #include <forward_list> // // #define cauto const auto& // #else // #endif using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vint; typedef vector<vector<int>> vvint; typedef vector<long long> vll, vLL; typedef vector<vector<long long>> vvll, vvLL; #define VV(T) vector<vector<T>> template <class T> void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) { v.assign(a, vector<T>(b, t)); } template <class F, class T> void convert(const F &f, T &t) { stringstream ss; ss << f; ss >> t; } #undef _P #define _P(...) (void)printf(__VA_ARGS__) #define reep(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) reep((i), 0, (n)) #define ALL(v) (v).begin(), (v).end() #define PB push_back #define F first #define S second #define mkp make_pair #define RALL(v) (v).rbegin(), (v).rend() #define DEBUG #ifdef DEBUG #define dump(x) cout << #x << " = " << (x) << endl; #define debug(x) \ cout << #x << " = " << (x) << " (L" << __LINE__ << ")" \ << " " << __FILE__ << endl; #else #define dump(x) #define debug(x) #endif #define LDcout(x, n) fixed << setprecision(n) << x #define MOD 1000000007LL #define EPS 1e-8 static const int INF = 1 << 24; void mainmain() { int n, m; cin >> n >> m; vint v(n, 0); vector<pii> w(m); rep(i, m) { cin >> w[i].F >> w[i].S; w[i].F--; w[i].S--; reep(j, w[i].F, w[i].S + 1) { v[j]++; } } rep(i, n) { if (v[i] == 0) { cout << "Impossible" << endl; return; } } int ans = 0; int pos = 0; while (pos < n) { int t = 0; ans++; rep(i, m) { if (w[i].F <= pos && pos <= w[i].S) { t = max(t, w[i].S); } } pos = t + 1; } cout << ans << endl; } signed main() { ios_base::sync_with_stdio(false); cout << fixed << setprecision(0); mainmain(); }
replace
115
116
115
116
TLE
p01525
C++
Runtime Error
#define _USE_MATH_DEFINES #define INF 0x3f3f3f3f #include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <iostream> #include <limits> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> using namespace std; typedef long long ll; typedef pair<int, int> P; typedef pair<int, P> PP; static const double EPS = 1e-8; static const ll MAX = 1e15; static const int tx[] = {0, 1, 0, -1}; static const int ty[] = {-1, 0, 1, 0}; ll recover_log[10001]; ll level_log[3652426]; struct Service { public: ll lower_bound; int type; int speed_up_duration; int service_idx; Service(ll _l, int _t, int _s, int _si) : lower_bound(_l), type(_t), speed_up_duration(_s), service_idx(_si) {} bool operator<(const Service &s) const { return lower_bound < s.lower_bound; } bool operator>(const Service &s) const { return lower_bound > s.lower_bound; } bool operator<(const ll _v) const { return lower_bound < _v; } bool operator>(const ll _v) const { return lower_bound > _v; } ll compute_recover(ll passed_days) { if (type == 0) { return 1; } else if (type == 1) { return passed_days; } else if (type == 2) { return passed_days * passed_days; } } ll compute_recover(ll first, ll last) { last = min((ll)speed_up_duration, last); first = min((ll)speed_up_duration + 1, first); ll minus = 0; if (first > 0) { if (type == 0) { minus = first - 1; } else if (type == 1) { minus = (first - 1) * (1 + (first - 1)) / 2; } else if (type == 2) { minus = (first - 1) * (first) * (2 * (first - 1) + 1) / 6; } } if (type == 0) { return last - minus; } else if (type == 1) { return last * (1 + last) / 2 - minus; } else if (type == 2) { return last * (last + 1) * (2 * last + 1) / 6 - minus; } } }; int main() { int total_services; int seek_duration; while (~scanf("%d %d", &total_services, &seek_duration)) { vector<Service> services; for (int service_idx = 0; service_idx < total_services; service_idx++) { ll lower_bound; int type; int speed_up_duration; scanf("%lld %d %d", &lower_bound, &type, &speed_up_duration); services.push_back( Service(lower_bound, type, speed_up_duration, service_idx)); } sort(services.begin(), services.end()); ll recover_level = 0; memset(recover_log, -1, sizeof(recover_log)); memset(level_log, -1, sizeof(level_log)); list<Service> provider; int next_target = 0; for (int day = 0; day <= 3652425; day++) { ll add_recover_level = 0; if (!provider.empty()) { for (list<Service>::iterator it = provider.begin(); it != provider.end();) { ll passed_days = day - recover_log[it->service_idx]; if (passed_days > it->speed_up_duration) { list<Service>::iterator next = provider.erase(it); if (next == provider.end()) break; it = next; } else { add_recover_level += it->compute_recover(passed_days); it++; if (it == provider.end()) break; } } } for (int i = next_target; i < total_services; i++) { if (services[i].lower_bound > recover_level + add_recover_level) break; if (recover_log[i] != -1) break; recover_log[i] = day; provider.push_back(services[i]); next_target = i + 1; } level_log[day] = recover_level + add_recover_level; recover_level += add_recover_level + 1; } for (int service_idx = 0; service_idx < total_services; service_idx++) { if (recover_log[service_idx] == -1) { printf("Many years later\n"); } else { printf("%lld\n", recover_log[service_idx]); } } for (int seek_idx = 0; seek_idx < seek_duration; seek_idx++) { int day; scanf("%d", &day); printf("%lld\n", level_log[day]); } } }
#define _USE_MATH_DEFINES #define INF 0x3f3f3f3f #include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <iostream> #include <limits> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> using namespace std; typedef long long ll; typedef pair<int, int> P; typedef pair<int, P> PP; static const double EPS = 1e-8; static const ll MAX = 1e15; static const int tx[] = {0, 1, 0, -1}; static const int ty[] = {-1, 0, 1, 0}; ll recover_log[100001]; ll level_log[3652426]; struct Service { public: ll lower_bound; int type; int speed_up_duration; int service_idx; Service(ll _l, int _t, int _s, int _si) : lower_bound(_l), type(_t), speed_up_duration(_s), service_idx(_si) {} bool operator<(const Service &s) const { return lower_bound < s.lower_bound; } bool operator>(const Service &s) const { return lower_bound > s.lower_bound; } bool operator<(const ll _v) const { return lower_bound < _v; } bool operator>(const ll _v) const { return lower_bound > _v; } ll compute_recover(ll passed_days) { if (type == 0) { return 1; } else if (type == 1) { return passed_days; } else if (type == 2) { return passed_days * passed_days; } } ll compute_recover(ll first, ll last) { last = min((ll)speed_up_duration, last); first = min((ll)speed_up_duration + 1, first); ll minus = 0; if (first > 0) { if (type == 0) { minus = first - 1; } else if (type == 1) { minus = (first - 1) * (1 + (first - 1)) / 2; } else if (type == 2) { minus = (first - 1) * (first) * (2 * (first - 1) + 1) / 6; } } if (type == 0) { return last - minus; } else if (type == 1) { return last * (1 + last) / 2 - minus; } else if (type == 2) { return last * (last + 1) * (2 * last + 1) / 6 - minus; } } }; int main() { int total_services; int seek_duration; while (~scanf("%d %d", &total_services, &seek_duration)) { vector<Service> services; for (int service_idx = 0; service_idx < total_services; service_idx++) { ll lower_bound; int type; int speed_up_duration; scanf("%lld %d %d", &lower_bound, &type, &speed_up_duration); services.push_back( Service(lower_bound, type, speed_up_duration, service_idx)); } sort(services.begin(), services.end()); ll recover_level = 0; memset(recover_log, -1, sizeof(recover_log)); memset(level_log, -1, sizeof(level_log)); list<Service> provider; int next_target = 0; for (int day = 0; day <= 3652425; day++) { ll add_recover_level = 0; if (!provider.empty()) { for (list<Service>::iterator it = provider.begin(); it != provider.end();) { ll passed_days = day - recover_log[it->service_idx]; if (passed_days > it->speed_up_duration) { list<Service>::iterator next = provider.erase(it); if (next == provider.end()) break; it = next; } else { add_recover_level += it->compute_recover(passed_days); it++; if (it == provider.end()) break; } } } for (int i = next_target; i < total_services; i++) { if (services[i].lower_bound > recover_level + add_recover_level) break; if (recover_log[i] != -1) break; recover_log[i] = day; provider.push_back(services[i]); next_target = i + 1; } level_log[day] = recover_level + add_recover_level; recover_level += add_recover_level + 1; } for (int service_idx = 0; service_idx < total_services; service_idx++) { if (recover_log[service_idx] == -1) { printf("Many years later\n"); } else { printf("%lld\n", recover_log[service_idx]); } } for (int seek_idx = 0; seek_idx < seek_duration; seek_idx++) { int day; scanf("%d", &day); printf("%lld\n", level_log[day]); } } }
replace
34
35
34
35
0
p01526
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define EPS (1e-8) #define N_MAX 200005 using namespace std; typedef double ll; int N; ll R, x[N_MAX], y[N_MAX]; bool used[N_MAX]; struct node { vector<ll> Point; int l, r; node() {} node(vector<ll> a) { Point = a, l = r = -1; } }; const int K = 2; // K:テヲツャツ。テ・ツ?εヲツ閉ー int D; // D:テ」ツ?敕」ツ?ョテヲツ卍づ」ツ?ォテ」ツつステ」ツδシテ」ツδ暗」ツ?凖」ツつ凝・ツ淞コテヲツコツ姪」ツ?ョテヲツャツ。テ・ツ?? vector<vector<ll>> P; // テ」ツ?敕」ツつ古」ツ?榲」ツつ古」ツ?ョティツヲツ?ァツエツ?」ツ?ッK+1テ・ツ??Kテ・ツ?凝」ツ?ョテヲツャツ。テ・ツ?ε」ツ?ョテァツ閉ェテ・ツ渉キテッツシツ凝、ツサツ佚」ツ?妥」ツ?淌」ツ??umber) vector<node> G; bool compare(vector<ll> A, vector<ll> B) { return A[D] < B[D]; } int make_tree(int depth, int l, int r) { if (l > r) return -1; D = depth % K; sort(P.begin() + l, P.begin() + r + 1, compare); int m = (l + r) / 2, res = G.size(); G.push_back(node(P[m])); int vl = make_tree(depth + 1, l, m - 1); int vr = make_tree(depth + 1, m + 1, r); G[res].l = vl; G[res].r = vr; return res; } vector<int> num; // searchテ」ツ?ァティツヲツ凝」ツ?、テ」ツ?妥」ツつ凝」ツ?禿」ツ?ィテ」ツ?古」ツ?ァテ」ツ?催」ツ?淌ァツつケテ」ツ?ョnumberテ」ツつ津」ツ??」ツつ古」ツつ? void search(int x, int depth, vector<ll> &L, vector<ll> &R) { bool f = true; for (int i = 0; i < K; i++) if (G[x].Point[i] < L[i] || R[i] < G[x].Point[i]) f = false; if (f) num.push_back(G[x].Point[K]); D = depth % K; if (G[x].Point[D] <= R[D] && G[x].r != -1) search(G[x].r, depth + 1, L, R); D = depth % K; if (L[D] <= G[x].Point[D] && G[x].l != -1) search(G[x].l, depth + 1, L, R); } int main() { cin >> N >> R; P.resize(N); for (int i = 0; i < N; i++) { scanf("%lf %lf", &x[i], &y[i]); P[i].resize(K + 1); P[i][K] = i; P[i][0] = x[i], P[i][1] = y[i]; } make_tree(0, 0, P.size() - 1); int ans = 0; for (int i = 0; i < N; i++) { if (used[i]) continue; vector<ll> L2(K), R2(K); L2[0] = x[i] - R - EPS; R2[0] = x[i] + R + EPS; L2[1] = y[i] - R - EPS; R2[1] = y[i] + R + EPS; num.clear(); search(0, 0, L2, R2); for (int j = 0; j < num.size(); j++) used[num[j]] = 1; ans++; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define EPS (1e-8) #define N_MAX 200005 using namespace std; typedef double ll; int N; ll R, x[N_MAX], y[N_MAX]; bool used[N_MAX]; struct node { vector<ll> Point; int l, r; node() {} node(vector<ll> a) { Point = a, l = r = -1; } }; const int K = 2; // K:テヲツャツ。テ・ツ?εヲツ閉ー int D; // D:テ」ツ?敕」ツ?ョテヲツ卍づ」ツ?ォテ」ツつステ」ツδシテ」ツδ暗」ツ?凖」ツつ凝・ツ淞コテヲツコツ姪」ツ?ョテヲツャツ。テ・ツ?? vector<vector<ll>> P; // テ」ツ?敕」ツつ古」ツ?榲」ツつ古」ツ?ョティツヲツ?ァツエツ?」ツ?ッK+1テ・ツ??Kテ・ツ?凝」ツ?ョテヲツャツ。テ・ツ?ε」ツ?ョテァツ閉ェテ・ツ渉キテッツシツ凝、ツサツ佚」ツ?妥」ツ?淌」ツ??umber) vector<node> G; bool compare(const vector<ll> &A, const vector<ll> &B) { return A[D] < B[D]; } int make_tree(int depth, int l, int r) { if (l > r) return -1; D = depth % K; sort(P.begin() + l, P.begin() + r + 1, compare); int m = (l + r) / 2, res = G.size(); G.push_back(node(P[m])); int vl = make_tree(depth + 1, l, m - 1); int vr = make_tree(depth + 1, m + 1, r); G[res].l = vl; G[res].r = vr; return res; } vector<int> num; // searchテ」ツ?ァティツヲツ凝」ツ?、テ」ツ?妥」ツつ凝」ツ?禿」ツ?ィテ」ツ?古」ツ?ァテ」ツ?催」ツ?淌ァツつケテ」ツ?ョnumberテ」ツつ津」ツ??」ツつ古」ツつ? void search(int x, int depth, vector<ll> &L, vector<ll> &R) { bool f = true; for (int i = 0; i < K; i++) if (G[x].Point[i] < L[i] || R[i] < G[x].Point[i]) f = false; if (f) num.push_back(G[x].Point[K]); D = depth % K; if (G[x].Point[D] <= R[D] && G[x].r != -1) search(G[x].r, depth + 1, L, R); D = depth % K; if (L[D] <= G[x].Point[D] && G[x].l != -1) search(G[x].l, depth + 1, L, R); } int main() { cin >> N >> R; P.resize(N); for (int i = 0; i < N; i++) { scanf("%lf %lf", &x[i], &y[i]); P[i].resize(K + 1); P[i][K] = i; P[i][0] = x[i], P[i][1] = y[i]; } make_tree(0, 0, P.size() - 1); int ans = 0; for (int i = 0; i < N; i++) { if (used[i]) continue; vector<ll> L2(K), R2(K); L2[0] = x[i] - R - EPS; R2[0] = x[i] + R + EPS; L2[1] = y[i] - R - EPS; R2[1] = y[i] + R + EPS; num.clear(); search(0, 0, L2, R2); for (int j = 0; j < num.size(); j++) used[num[j]] = 1; ans++; } cout << ans << endl; return 0; }
replace
23
24
23
24
TLE
p01529
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long int ll; int main() { ll N; cin >> N; vector<ll> w(N), sum(N, 0); for (ll i = 0; i < N; i++) { cin >> w[i]; sum[i + 1] = sum[i] + w[i]; } vector<vector<ll>> dp(N, vector<ll>(N, -1)), mo(N, vector<ll>(N)); for (ll i = 0; i < N; i++) { dp[i][i] = 0; mo[i][i] = i; } for (ll i = 0; i + 1 < N; i++) { dp[i][i + 1] = sum[i + 2] - sum[i]; mo[i][i + 1] = i; } for (ll l = 2; l < N; l++) { for (ll i = 0; i + l < N; i++) { for (ll k = mo[i][i + l - 1]; k <= mo[i + 1][i + l]; k++) { if (dp[i][i + l] == -1 || dp[i][i + l] > dp[i][k] + dp[k + 1][i + l]) { dp[i][i + l] = dp[i][k] + dp[k + 1][i + l]; mo[i][i + l] = k; } } dp[i][i + l] += sum[i + l + 1] - sum[i]; } } cout << dp[0][N - 1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; int main() { ll N; cin >> N; vector<ll> w(N), sum(N + 1, 0); for (ll i = 0; i < N; i++) { cin >> w[i]; sum[i + 1] = sum[i] + w[i]; } vector<vector<ll>> dp(N, vector<ll>(N, -1)), mo(N, vector<ll>(N)); for (ll i = 0; i < N; i++) { dp[i][i] = 0; mo[i][i] = i; } for (ll i = 0; i + 1 < N; i++) { dp[i][i + 1] = sum[i + 2] - sum[i]; mo[i][i + 1] = i; } for (ll l = 2; l < N; l++) { for (ll i = 0; i + l < N; i++) { for (ll k = mo[i][i + l - 1]; k <= mo[i + 1][i + l]; k++) { if (dp[i][i + l] == -1 || dp[i][i + l] > dp[i][k] + dp[k + 1][i + l]) { dp[i][i + l] = dp[i][k] + dp[k + 1][i + l]; mo[i][i + l] = k; } } dp[i][i + l] += sum[i + l + 1] - sum[i]; } } cout << dp[0][N - 1] << endl; return 0; }
replace
7
8
7
8
-6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)
p01529
C++
Runtime Error
#include <algorithm> #include <cstdio> #define MAX_N (100) using namespace std; typedef long long lint; int main() { int N; lint w[MAX_N], rSum[MAX_N]; static lint dp[MAX_N][MAX_N]; static int pos[MAX_N][MAX_N]; scanf("%d", &N); for (int i = 0; i < N; i++) { scanf("%lld", w + i); rSum[i] = i ? rSum[i - 1] + w[i] : w[i]; } for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { dp[i][j] = (lint)1e16; } dp[i][i] = 0; pos[i][i] = i; } for (int w = 1; w < N; w++) { for (int i = 0, j; (j = i + w) < N; i++) { for (int k = pos[i][j - 1]; k <= pos[i + 1][j]; k++) { lint nc = dp[i][k] + dp[k + 1][j] + rSum[j] - (i ? rSum[i - 1] : 0); if (nc < dp[i][j]) { dp[i][j] = nc; pos[i][j] = k; } } } } printf("%lld\n", dp[0][N - 1]); return (0); }
#include <algorithm> #include <cstdio> #define MAX_N (4000) using namespace std; typedef long long lint; int main() { int N; lint w[MAX_N], rSum[MAX_N]; static lint dp[MAX_N][MAX_N]; static int pos[MAX_N][MAX_N]; scanf("%d", &N); for (int i = 0; i < N; i++) { scanf("%lld", w + i); rSum[i] = i ? rSum[i - 1] + w[i] : w[i]; } for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { dp[i][j] = (lint)1e16; } dp[i][i] = 0; pos[i][i] = i; } for (int w = 1; w < N; w++) { for (int i = 0, j; (j = i + w) < N; i++) { for (int k = pos[i][j - 1]; k <= pos[i + 1][j]; k++) { lint nc = dp[i][k] + dp[k + 1][j] + rSum[j] - (i ? rSum[i - 1] : 0); if (nc < dp[i][j]) { dp[i][j] = nc; pos[i][j] = k; } } } } printf("%lld\n", dp[0][N - 1]); return (0); }
replace
3
4
3
4
0
p01529
C++
Runtime Error
#include <algorithm> #include <iostream> #include <vector> #define INF (1LL << 61) using namespace std; typedef unsigned long long ull; int main(void) { int n; cin >> n; vector<ull> cost(n); for (int i = 0; i < n; i++) { cin >> cost[i]; if (i > 0) cost[i] += cost[i - 1]; } static ull dp[4000][4000], k[4000][4000]; fill(dp[0], dp[n + 1], INF); fill(k[0], k[n + 1], INF); for (int i = 0; i < n; i++) { dp[i][i] = 0; k[i][i] = i; } for (int w = 1; w <= n; w++) { for (int i = 0, j = i + w; j < n; i++, j++) { for (int r = k[i][j - 1]; r <= k[i + 1][j]; r++) { ull c = dp[i][r] + dp[r + 1][j] + cost[j] - ((i > 0) ? cost[i - 1] : 0); if (dp[i][j] > c) dp[i][j] = c, k[i][j] = r; } } } cout << dp[0][n - 1] << endl; return 0; }
#include <algorithm> #include <iostream> #include <vector> #define INF (1LL << 61) using namespace std; typedef unsigned long long ull; int main(void) { int n; cin >> n; vector<ull> cost(n); for (int i = 0; i < n; i++) { cin >> cost[i]; if (i > 0) cost[i] += cost[i - 1]; } static ull dp[4001][4001], k[4001][4001]; fill(dp[0], dp[4001], INF); for (int i = 0; i < n; i++) { dp[i][i] = 0; k[i][i] = i; } for (int w = 1; w <= n; w++) { for (int i = 0, j = i + w; j < n; i++, j++) { for (int r = k[i][j - 1]; r <= k[i + 1][j]; r++) { ull c = dp[i][r] + dp[r + 1][j] + cost[j] - ((i > 0) ? cost[i - 1] : 0); if (dp[i][j] > c) dp[i][j] = c, k[i][j] = r; } } } cout << dp[0][n - 1] << endl; return 0; }
replace
21
24
21
23
-11
p01529
C++
Time Limit Exceeded
#include <algorithm> #include <cstdio> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <utility> #include <vector> #define ll long long int #define pb push_back #define mk make_pair #define pq priority_queue using namespace std; typedef pair<int, int> P; typedef pair<ll, int> Pl; const int inf = 1e9; const ll linf = 1LL << 50; int n; ll W[4001]; ll dp[4001][4001]; ll sum[4001]; int at[4001][4001]; int main(int argc, char const *argv[]) { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%lld", W + i); if (i == 0) sum[i] = W[i]; else sum[i] = sum[i - 1] + W[i]; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { at[i][j] = i; dp[i][j] = linf; } dp[i][i] = 0; } for (int w = 1; w < n; w++) { for (int i = 0; i + w < n; i++) { for (int k = max(at[i][i + w - 1], i); k < i + w; k++) { if (dp[i][i + w] > dp[i][k] + dp[k + 1][i + w]) { dp[i][i + w] = dp[i][k] + dp[k + 1][i + w]; at[i][i + w] = k; } else if (dp[i][i + w] < dp[i][k] + dp[k + 1][i + w]) { // break; } } if (i != 0) dp[i][i + w] += sum[i + w] - sum[i - 1]; else dp[i][i + w] += sum[i + w]; } } cout << dp[0][n - 1] << endl; return 0; }
#include <algorithm> #include <cstdio> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <utility> #include <vector> #define ll long long int #define pb push_back #define mk make_pair #define pq priority_queue using namespace std; typedef pair<int, int> P; typedef pair<ll, int> Pl; const int inf = 1e9; const ll linf = 1LL << 50; int n; ll W[4001]; ll dp[4001][4001]; ll sum[4001]; int at[4001][4001]; int main(int argc, char const *argv[]) { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%lld", W + i); if (i == 0) sum[i] = W[i]; else sum[i] = sum[i - 1] + W[i]; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { at[i][j] = i; dp[i][j] = linf; } dp[i][i] = 0; } for (int w = 1; w < n; w++) { for (int i = 0; i + w < n; i++) { for (int k = max(at[i][i + w - 1], i); k <= min(i + w - 1, at[i + 1][i + w]); k++) { if (dp[i][i + w] > dp[i][k] + dp[k + 1][i + w]) { dp[i][i + w] = dp[i][k] + dp[k + 1][i + w]; at[i][i + w] = k; } else if (dp[i][i + w] < dp[i][k] + dp[k + 1][i + w]) { // break; } } if (i != 0) dp[i][i + w] += sum[i + w] - sum[i - 1]; else dp[i][i + w] += sum[i + w]; } } cout << dp[0][n - 1] << endl; return 0; }
replace
45
46
45
47
TLE
p01529
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define DUMP(x) cerr << #x << "=" << x << endl #define DUMP2(x, y) \ cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl #define BINARY(x) static_cast<bitset<16>>(x) #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++) #define in_range(x, y, w, h) \ (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) && (int)(y) < (int)(h)) #define ALL(a) (a).begin(), (a).end() typedef long long ll; const ll INF = 1e18; typedef pair<int, int> PII; int dx[4] = {0, -1, 1, 0}, dy[4] = {-1, 0, 0, 1}; int N; vector<ll> W; // dp[i][j] := ??¨?????¨[i, j]????????????????????????????°? // dp[i][j] = min(dp[i][k] + dp[k+1][j] + w[i][j]) // sum[i][j] = w_i + ... + w_j ll dp[3010][3010]; ll p[3010][3010]; ll sum[3010]; ll cost(int i, int j) { return sum[j + 1] - sum[i]; } int main() { cin >> N; W.resize(N); rep(i, N) cin >> W[i]; sum[0] = 0LL; for (int i = 0; i < N; i++) { sum[i + 1] = sum[i] + W[i]; } rep(i, N) rep(j, N) dp[i][j] = (i == j ? 0LL : INF); rep(i, N) p[i][i] = i; REP(l, 1, N) { rep(i, N - l) { int j = i + l; int k0 = p[i][j - 1], k1 = p[i + 1][j]; REP(k, k0, k1 + 1) { ll tmp = dp[i][k] + dp[k + 1][j] + cost(i, j); if (tmp < dp[i][j]) { dp[i][j] = tmp; p[i][j] = k; } } } } cout << dp[0][N - 1] << endl; }
#include <bits/stdc++.h> using namespace std; #define DUMP(x) cerr << #x << "=" << x << endl #define DUMP2(x, y) \ cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl #define BINARY(x) static_cast<bitset<16>>(x) #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++) #define in_range(x, y, w, h) \ (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) && (int)(y) < (int)(h)) #define ALL(a) (a).begin(), (a).end() typedef long long ll; const ll INF = 1e18; typedef pair<int, int> PII; int dx[4] = {0, -1, 1, 0}, dy[4] = {-1, 0, 0, 1}; int N; vector<ll> W; // dp[i][j] := ??¨?????¨[i, j]????????????????????????????°? // dp[i][j] = min(dp[i][k] + dp[k+1][j] + w[i][j]) // sum[i][j] = w_i + ... + w_j ll dp[4010][4010]; ll p[4010][4010]; ll sum[4010]; ll cost(int i, int j) { return sum[j + 1] - sum[i]; } int main() { cin >> N; W.resize(N); rep(i, N) cin >> W[i]; sum[0] = 0LL; for (int i = 0; i < N; i++) { sum[i + 1] = sum[i] + W[i]; } rep(i, N) rep(j, N) dp[i][j] = (i == j ? 0LL : INF); rep(i, N) p[i][i] = i; REP(l, 1, N) { rep(i, N - l) { int j = i + l; int k0 = p[i][j - 1], k1 = p[i + 1][j]; REP(k, k0, k1 + 1) { ll tmp = dp[i][k] + dp[k + 1][j] + cost(i, j); if (tmp < dp[i][j]) { dp[i][j] = tmp; p[i][j] = k; } } } } cout << dp[0][N - 1] << endl; }
replace
26
29
26
29
-11
p01532
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> using namespace std; typedef pair<int, int> P; const int MAX = 100; P data[MAX]; int N, T, H, L; int mon; void solve() { int player = -1; while (1) { player++; if (player == N) player = 0; if (data[player].first > 0) { mon += 10; T++; data[player].first--; if (L < T) { cout << player + 1 << endl; break; } } else if (data[player].second > 0) { mon += 100; H++; data[player].second--; } else { cout << player + 1 << endl; break; } if (mon >= 90) { mon -= 90; T -= 9; data[player].first += 9; if (T < 0) { cout << player + 1 << endl; break; } } } } void input() { cin >> N >> T >> H >> L; for (int i = 0; i < N; i++) cin >> data[i].first >> data[i].second; } int main() { input(); solve(); return 0; }
#include <algorithm> #include <iostream> using namespace std; typedef pair<int, int> P; const int MAX = 100; P data[MAX]; int N, T, H, L; int mon; void solve() { int player = -1; while (1) { player++; if (player == N) player = 0; if (data[player].first > 0) { mon += 10; T++; data[player].first--; if (L < T) { cout << player + 1 << endl; break; } } else if (data[player].second > 0) { mon += 100; H++; data[player].second--; } else { cout << player + 1 << endl; break; } if (mon >= 90) { mon -= 90; T -= mon / 10; data[player].first += mon / 10; mon = 0; if (T < 0) { cout << player + 1 << endl; break; } } } } void input() { cin >> N >> T >> H >> L; for (int i = 0; i < N; i++) cin >> data[i].first >> data[i].second; } int main() { input(); solve(); return 0; }
replace
37
39
37
40
TLE
p01534
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> using namespace std; int n, m, a[55], b[55], c[55], dp[55][55][55][55]; bool vis[55][55][55][55]; int rec(int pos, int rem, int lv2, int lv1) { if (pos == n) return 0; if (vis[pos][rem][lv2][lv1]) return dp[pos][rem][lv2][lv1]; int ret = -999999999; for (int i = 0; i <= rem; i++) { int res = rec(pos + 1, rem - i, i, lv2); int cs = c[pos]; for (int j = 1; j <= 8; j++) { for (int k = 0; k <= j; k++) { int l = j - k; if (k <= lv2 && l <= lv1) { cs = max(cs, a[pos] * k + b[pos] * l); } } } ret = max(ret, res + cs); } return dp[pos][rem][lv2][lv1] = ret; } int main() { cin >> n >> m; for (int i = 0; i < n; i++) cin >> a[i] >> b[i] >> c[i]; int ret = -999999999; for (int i = 0; i <= m; i++) ret = max(ret, rec(0, m - i, i, 0)); cout << ret << endl; return 0; }
#include <algorithm> #include <iostream> using namespace std; int n, m, a[55], b[55], c[55], dp[55][55][55][55]; bool vis[55][55][55][55]; int rec(int pos, int rem, int lv2, int lv1) { if (pos == n) return 0; if (vis[pos][rem][lv2][lv1]) return dp[pos][rem][lv2][lv1]; int ret = -999999999; for (int i = 0; i <= rem; i++) { int res = rec(pos + 1, rem - i, i, lv2); int cs = c[pos]; for (int j = 1; j <= 8; j++) { for (int k = 0; k <= j; k++) { int l = j - k; if (k <= lv2 && l <= lv1) { cs = max(cs, a[pos] * k + b[pos] * l); } } } ret = max(ret, res + cs); } vis[pos][rem][lv2][lv1] = true; return dp[pos][rem][lv2][lv1] = ret; } int main() { cin >> n >> m; for (int i = 0; i < n; i++) cin >> a[i] >> b[i] >> c[i]; int ret = -999999999; for (int i = 0; i <= m; i++) ret = max(ret, rec(0, m - i, i, 0)); cout << ret << endl; return 0; }
insert
24
24
24
25
TLE
p01534
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <string> #include <vector> using namespace std; using ll = long long; #define rep(i, n) for (int i = 0; i < (int)n; i++) int n, m; int x[51], y[51], z[51]; int dp1[51][51][51]; int dp2[51][51][51]; const int INF = 1e9; int cost(int a, int b, int c) { if (dp1[a][b][c] != -INF) return dp1[a][b][c]; int res = -INF; rep(i, b + 1) rep(j, c + 1) { if (i == 0 && j == 0) res = max(res, z[a]); else if (i + j <= 8) res = max(res, i * x[a] + j * y[a]); } return dp1[a][b][c] = res; } int dfs(int a, int b, int c) { if (a == n) return 0; // if (dp2[a][b][c] != -INF) return dp2[a][b][c]; // int res = dfs(a + 1, b, 0) + z[a]; int res = -INF; rep(i, b + 1) { int s = -INF; rep(j, i + 1) { rep(k, c + 1) { if (k == 0 && j == 0) s = max(z[a], s); else if (j + k <= 8) s = max(s, j * x[a] + k * y[a]); } } res = max(res, dfs(a + 1, b - i, i) + s); } // rep(i, b + 1) { res = max(res, dfs(a + 1, b - i, i) + cost(a, i, c)); } return dp2[a][b][c] = res; } int main() { rep(i, 51) rep(j, 51) rep(k, 51) dp1[i][j][k] = -INF; rep(i, 51) rep(j, 51) rep(k, 51) dp2[i][j][k] = -INF; cin >> n >> m; rep(i, n) cin >> x[i] >> y[i] >> z[i]; cout << dfs(0, m, 0) << endl; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <string> #include <vector> using namespace std; using ll = long long; #define rep(i, n) for (int i = 0; i < (int)n; i++) int n, m; int x[51], y[51], z[51]; int dp1[51][51][51]; int dp2[51][51][51]; const int INF = 1e9; int cost(int a, int b, int c) { if (dp1[a][b][c] != -INF) return dp1[a][b][c]; int res = -INF; rep(i, b + 1) rep(j, c + 1) { if (i == 0 && j == 0) res = max(res, z[a]); else if (i + j <= 8) res = max(res, i * x[a] + j * y[a]); } return dp1[a][b][c] = res; } int dfs(int a, int b, int c) { if (a == n) return 0; if (dp2[a][b][c] != -INF) return dp2[a][b][c]; // int res = dfs(a + 1, b, 0) + z[a]; int res = -INF; rep(i, b + 1) { int s = -INF; rep(j, i + 1) { rep(k, c + 1) { if (k == 0 && j == 0) s = max(z[a], s); else if (j + k <= 8) s = max(s, j * x[a] + k * y[a]); } } res = max(res, dfs(a + 1, b - i, i) + s); } // rep(i, b + 1) { res = max(res, dfs(a + 1, b - i, i) + cost(a, i, c)); } return dp2[a][b][c] = res; } int main() { rep(i, 51) rep(j, 51) rep(k, 51) dp1[i][j][k] = -INF; rep(i, 51) rep(j, 51) rep(k, 51) dp2[i][j][k] = -INF; cin >> n >> m; rep(i, n) cin >> x[i] >> y[i] >> z[i]; cout << dfs(0, m, 0) << endl; }
replace
37
38
37
39
TLE
p01535
C++
Runtime Error
#include <assert.h> #include <iostream> #include <map> #include <stdio.h> #include <stdlib.h> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) struct Node; struct Fun; int h, w, cr, cc; char scr[512][512]; Node *lnk[512][512]; Fun *evt[512][512]; map<string, vector<pair<string, Fun *>>> scrp; map<string, Node *> dmls; map<string, Fun *> funs; Node *act; void newline() { cr++; cc = 0; } void draw(const string &s, Node *hl, Fun *fn) { rep(i, s.size()) { if (0 <= cr && cr < h && 0 <= cc && cc < w) { scr[cr][cc] = s[i]; lnk[cr][cc] = hl; evt[cr][cc] = fn; cc++; } if (cc == w) newline(); } } struct Node { string tag; string text; vector<Node *> cs; bool visible; Node(const string &tag) : tag(tag), visible(true) {} void dump() const { dump(0); } void dump(int dep) const { rep(_, dep) cerr << ' '; cerr << '<' << tag << '>'; cerr << " visi = " << visible; cerr << " text = " << text << endl; rep(i, cs.size()) cs[i]->dump(dep + 2); rep(_, dep) cerr << ' '; cerr << "</" << tag << '>' << endl; ; } void render() const { if (!visible) return; if (tag == "$text") draw(text, 0, 0); else if (tag == "script") ; else if (tag == "link") { assert(cs.size() == 1 && cs[0]->tag == "$text"); assert(dmls[cs[0]->text]); draw(cs[0]->text, dmls[cs[0]->text], 0); } else if (tag == "button") { assert(cs.size() == 1 && cs[0]->tag == "$text"); assert(funs[cs[0]->text]); draw(cs[0]->text, 0, funs[cs[0]->text]); } else if (tag == "br") newline(); else { rep(i, cs.size()) cs[i]->render(); } } void init() { visible = true; if (tag == "script") { assert(cs.size() == 1 && cs[0]->tag == "$text"); string file = cs[0]->text; vector<pair<string, Fun *>> fs = scrp[file]; rep(i, fs.size()) { funs[fs[i].first] = fs[i].second; } } rep(i, cs.size()) cs[i]->init(); } void apply(const vector<string> &vs, int k, bool visi) { if (vs[k] == tag) { if (k == (int)vs.size() - 1) { visible = visi; } else { k++; } } rep(i, cs.size()) cs[i]->apply(vs, k, visi); } }; struct Fun { vector<pair<vector<string>, bool>> asn; void exec() { rep(i, asn.size()) { act->apply(asn[i].first, 0, asn[i].second); } } }; vector<string> lex_dml(const string &s) { vector<string> ts; int pos = 0; rep(i, s.size()) { if (s[i] == '<') { ts.push_back(s.substr(pos, i - pos)); pos = i; } else if (s[i] == '>') { ts.push_back(s.substr(pos, i + 1 - pos)); pos = i + 1; } } ts.push_back(s.substr(pos)); return ts; } bool isbegin(const string &t) { if (t.size() < 3) return false; if (t[0] != '<' || t[t.size() - 1] != '>') return false; for (int i = 1; i < (int)t.size() - 1; i++) { assert(t[i] != ' '); if (!islower(t[i]) && !isupper(t[i])) return false; } return true; } bool isend(const string &t) { if (t.size() < 4) return false; if (t[0] != '<' || t[1] != '/' || t[t.size() - 1] != '>') return false; for (int i = 2; i < (int)t.size() - 1; i++) { assert(t[i] != ' '); if (!islower(t[i]) && !isupper(t[i])) return false; } return true; } Node *parse_dml(const vector<string> &ts) { Node *root = new Node("$root"); vector<Node *> stk; stk.push_back(root); rep(i, ts.size()) { if (ts[i].size() == 0) continue; if (isbegin(ts[i])) { Node *node = new Node(ts[i].substr(1, ts[i].size() - 2)); stk.back()->cs.push_back(node); if (ts[i] != "<br>") stk.push_back(node); } else if (isend(ts[i])) { assert(stk.back()->tag == ts[i].substr(2, ts[i].size() - 3)); stk.pop_back(); } else { Node *node = new Node("$text"); node->text = ts[i]; stk.back()->cs.push_back(node); } } assert(stk.size() == 1); return root; } vector<string> parse_prop(const string &s) { vector<string> ps; int pos = 0; rep(i, s.size()) { if (s[i] == '.') { ps.push_back(s.substr(pos, i - pos)); pos = i + 1; } } assert(s.substr(pos) == "visible"); return ps; } string _s; unsigned _ix; vector<pair<vector<string>, bool>> parse_expr() { unsigned pos = _ix; vector<string> props; vector<bool> rev; while (_s[_ix] != ';') { if (_s[_ix] == '!' || _s[_ix] == '=') { props.push_back(_s.substr(pos, _ix - pos)); if (_s[_ix] == '!') { _ix += 2; pos = _ix; rev.push_back(true); } else { _ix += 1; pos = _ix; rev.push_back(false); } } else { _ix++; } } string val = _s.substr(pos, _ix - pos); bool cur = val == "true"; vector<pair<vector<string>, bool>> rs; for (int i = (int)props.size() - 1; i >= 0; i--) { if (rev[i]) cur = !cur; // cerr << props[i] << " = " << cur << endl; rs.push_back(make_pair(parse_prop(props[i]), cur)); } _ix++; return rs; } pair<string, Fun *> parse_fun() { Fun *fun = new Fun(); const unsigned st = _ix; while (_s[_ix] != '{') _ix++; const string id = _s.substr(st, _ix - st); _ix++; // cerr << id << endl; while (_s[_ix] != '}') { vector<pair<vector<string>, bool>> es(parse_expr()); rep(i, es.size()) fun->asn.push_back(es[i]); } _ix++; return make_pair(id, fun); } vector<pair<string, Fun *>> parse_ds(const string &s) { _s = s; _ix = 0; vector<pair<string, Fun *>> fs; while (_ix < _s.size()) { fs.push_back(parse_fun()); } return fs; } void render(Node *file) { rep(i, h) rep(j, w) { scr[i][j] = '.'; lnk[i][j] = 0; evt[i][j] = 0; } cr = cc = 0; file->render(); act = file; } void click(int x, int y) { if (lnk[y][x]) { funs.clear(); lnk[y][x]->init(); render(lnk[y][x]); } else if (evt[y][x]) { evt[y][x]->exec(); render(act); } } void getl(string &s) { getline(cin, s); assert(s.size() == 0 || s[s.size() - 1] != '\r'); } int main() { string s; getl(s); const int n = atoi(s.c_str()); rep(_, n) { getl(s); if (s.substr(s.size() - 4) == ".dml") { const string file = s.substr(0, s.size() - 4); ; // cerr << file << endl; getl(s); vector<string> ts = lex_dml(s); // rep (i, ts.size()) cerr << i << ": " << ts[i] << endl; Node *root = parse_dml(ts); // root->dump(); dmls[file] = root; } else if (s.substr(s.size() - 3) == ".ds") { const string file = s.substr(0, s.size() - 3); ; // cerr << file << endl; getl(s); vector<pair<string, Fun *>> fs = parse_ds(s); scrp[file] = fs; } else assert(false); } getl(s); const int m = atoi(s.c_str()); rep(_, m) { int K; char buf[32]; getl(s); sscanf(s.c_str(), "%d %d %d %s", &w, &h, &K, buf); dmls[buf]->init(); render(dmls[buf]); rep(_, K) { int x, y; getl(s); sscanf(s.c_str(), "%d%d", &x, &y); click(x, y); } rep(i, h) { rep(j, w) putchar(scr[i][j]); putchar('\n'); } } return 0; }
#include <assert.h> #include <iostream> #include <map> #include <stdio.h> #include <stdlib.h> #include <vector> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) struct Node; struct Fun; int h, w, cr, cc; char scr[512][512]; Node *lnk[512][512]; Fun *evt[512][512]; map<string, vector<pair<string, Fun *>>> scrp; map<string, Node *> dmls; map<string, Fun *> funs; Node *act; void newline() { cr++; cc = 0; } void draw(const string &s, Node *hl, Fun *fn) { rep(i, s.size()) { if (0 <= cr && cr < h && 0 <= cc && cc < w) { scr[cr][cc] = s[i]; lnk[cr][cc] = hl; evt[cr][cc] = fn; cc++; } if (cc == w) newline(); } } struct Node { string tag; string text; vector<Node *> cs; bool visible; Node(const string &tag) : tag(tag), visible(true) {} void dump() const { dump(0); } void dump(int dep) const { rep(_, dep) cerr << ' '; cerr << '<' << tag << '>'; cerr << " visi = " << visible; cerr << " text = " << text << endl; rep(i, cs.size()) cs[i]->dump(dep + 2); rep(_, dep) cerr << ' '; cerr << "</" << tag << '>' << endl; ; } void render() const { if (!visible) return; if (tag == "$text") draw(text, 0, 0); else if (tag == "script") ; else if (tag == "link") { assert(cs.size() == 1 && cs[0]->tag == "$text"); assert(dmls[cs[0]->text]); draw(cs[0]->text, dmls[cs[0]->text], 0); } else if (tag == "button") { assert(cs.size() == 1 && cs[0]->tag == "$text"); // assert(funs[cs[0]->text]); draw(cs[0]->text, 0, funs[cs[0]->text]); } else if (tag == "br") newline(); else { rep(i, cs.size()) cs[i]->render(); } } void init() { visible = true; if (tag == "script") { assert(cs.size() == 1 && cs[0]->tag == "$text"); string file = cs[0]->text; vector<pair<string, Fun *>> fs = scrp[file]; rep(i, fs.size()) { funs[fs[i].first] = fs[i].second; } } rep(i, cs.size()) cs[i]->init(); } void apply(const vector<string> &vs, int k, bool visi) { if (vs[k] == tag) { if (k == (int)vs.size() - 1) { visible = visi; } else { k++; } } rep(i, cs.size()) cs[i]->apply(vs, k, visi); } }; struct Fun { vector<pair<vector<string>, bool>> asn; void exec() { rep(i, asn.size()) { act->apply(asn[i].first, 0, asn[i].second); } } }; vector<string> lex_dml(const string &s) { vector<string> ts; int pos = 0; rep(i, s.size()) { if (s[i] == '<') { ts.push_back(s.substr(pos, i - pos)); pos = i; } else if (s[i] == '>') { ts.push_back(s.substr(pos, i + 1 - pos)); pos = i + 1; } } ts.push_back(s.substr(pos)); return ts; } bool isbegin(const string &t) { if (t.size() < 3) return false; if (t[0] != '<' || t[t.size() - 1] != '>') return false; for (int i = 1; i < (int)t.size() - 1; i++) { assert(t[i] != ' '); if (!islower(t[i]) && !isupper(t[i])) return false; } return true; } bool isend(const string &t) { if (t.size() < 4) return false; if (t[0] != '<' || t[1] != '/' || t[t.size() - 1] != '>') return false; for (int i = 2; i < (int)t.size() - 1; i++) { assert(t[i] != ' '); if (!islower(t[i]) && !isupper(t[i])) return false; } return true; } Node *parse_dml(const vector<string> &ts) { Node *root = new Node("$root"); vector<Node *> stk; stk.push_back(root); rep(i, ts.size()) { if (ts[i].size() == 0) continue; if (isbegin(ts[i])) { Node *node = new Node(ts[i].substr(1, ts[i].size() - 2)); stk.back()->cs.push_back(node); if (ts[i] != "<br>") stk.push_back(node); } else if (isend(ts[i])) { assert(stk.back()->tag == ts[i].substr(2, ts[i].size() - 3)); stk.pop_back(); } else { Node *node = new Node("$text"); node->text = ts[i]; stk.back()->cs.push_back(node); } } assert(stk.size() == 1); return root; } vector<string> parse_prop(const string &s) { vector<string> ps; int pos = 0; rep(i, s.size()) { if (s[i] == '.') { ps.push_back(s.substr(pos, i - pos)); pos = i + 1; } } assert(s.substr(pos) == "visible"); return ps; } string _s; unsigned _ix; vector<pair<vector<string>, bool>> parse_expr() { unsigned pos = _ix; vector<string> props; vector<bool> rev; while (_s[_ix] != ';') { if (_s[_ix] == '!' || _s[_ix] == '=') { props.push_back(_s.substr(pos, _ix - pos)); if (_s[_ix] == '!') { _ix += 2; pos = _ix; rev.push_back(true); } else { _ix += 1; pos = _ix; rev.push_back(false); } } else { _ix++; } } string val = _s.substr(pos, _ix - pos); bool cur = val == "true"; vector<pair<vector<string>, bool>> rs; for (int i = (int)props.size() - 1; i >= 0; i--) { if (rev[i]) cur = !cur; // cerr << props[i] << " = " << cur << endl; rs.push_back(make_pair(parse_prop(props[i]), cur)); } _ix++; return rs; } pair<string, Fun *> parse_fun() { Fun *fun = new Fun(); const unsigned st = _ix; while (_s[_ix] != '{') _ix++; const string id = _s.substr(st, _ix - st); _ix++; // cerr << id << endl; while (_s[_ix] != '}') { vector<pair<vector<string>, bool>> es(parse_expr()); rep(i, es.size()) fun->asn.push_back(es[i]); } _ix++; return make_pair(id, fun); } vector<pair<string, Fun *>> parse_ds(const string &s) { _s = s; _ix = 0; vector<pair<string, Fun *>> fs; while (_ix < _s.size()) { fs.push_back(parse_fun()); } return fs; } void render(Node *file) { rep(i, h) rep(j, w) { scr[i][j] = '.'; lnk[i][j] = 0; evt[i][j] = 0; } cr = cc = 0; file->render(); act = file; } void click(int x, int y) { if (lnk[y][x]) { funs.clear(); lnk[y][x]->init(); render(lnk[y][x]); } else if (evt[y][x]) { evt[y][x]->exec(); render(act); } } void getl(string &s) { getline(cin, s); assert(s.size() == 0 || s[s.size() - 1] != '\r'); } int main() { string s; getl(s); const int n = atoi(s.c_str()); rep(_, n) { getl(s); if (s.substr(s.size() - 4) == ".dml") { const string file = s.substr(0, s.size() - 4); ; // cerr << file << endl; getl(s); vector<string> ts = lex_dml(s); // rep (i, ts.size()) cerr << i << ": " << ts[i] << endl; Node *root = parse_dml(ts); // root->dump(); dmls[file] = root; } else if (s.substr(s.size() - 3) == ".ds") { const string file = s.substr(0, s.size() - 3); ; // cerr << file << endl; getl(s); vector<pair<string, Fun *>> fs = parse_ds(s); scrp[file] = fs; } else assert(false); } getl(s); const int m = atoi(s.c_str()); rep(_, m) { int K; char buf[32]; getl(s); sscanf(s.c_str(), "%d %d %d %s", &w, &h, &K, buf); dmls[buf]->init(); render(dmls[buf]); rep(_, K) { int x, y; getl(s); sscanf(s.c_str(), "%d%d", &x, &y); click(x, y); } rep(i, h) { rep(j, w) putchar(scr[i][j]); putchar('\n'); } } return 0; }
replace
71
72
71
72
0
p01538
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define iota(i, n, b, s) \ for (int i = int(b); i != int((b) + (s) * (n)); i += (s)) #define range(i, n, m) \ iota(i, (((n) > (m)) ? ((n) - (m)) : ((m) - (n))), (n), ((n) > (m) ? -1 : 1)) #define rep(i, n) iota(i, (n), 0, 1) #define loop for (;;) #define INF (1e9) #define EPS (1e-9) #define cons(a, b) (make_pair(a, b)) #define car(a) (a.first) #define cdr(a) (a.second) #define cadr(a) (car(cdr(a))) #define cddr(a) (cdr(cdr(a))) #define all(a) a.begin(), a.end() #define trace(var) cerr << ">>> " << #var << " = " << var << endl; typedef long long Integer; typedef double Real; typedef vector<int> vi; typedef vector<string> vs; typedef map<string, int> Dictionary; const Real PI = acos(-1); template <class S, class T> ostream &operator<<(ostream &os, pair<S, T> p) { os << '(' << car(p) << ", " << cdr(p) << ')'; return os; } template <class T> ostream &operator<<(ostream &os, vector<T> v) { if (v.size() == 0) { os << "(empty)"; return os; } os << v[0]; for (int i = 1, len = v.size(); i < len; ++i) os << ' ' << v[i]; return os; } int dx[] = {-1, 0, 1, 0}; int dy[] = {0, -1, 0, 1}; bool contains(vector<int> &vs, int x) { for (int v : vs) { if (v == x) return true; } return false; } int main() { int n; cin >> n; while (n--) { int x; cin >> x; int cx = 0; vector<int> history; while (x > 9) { int nx = 0; for (int d = 10;; d *= 10) { int z = x / d; int y = x % d; if (z == 0) break; nx = max(nx, z * y); } x = nx; trace(x); ++cx; } cout << cx << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define iota(i, n, b, s) \ for (int i = int(b); i != int((b) + (s) * (n)); i += (s)) #define range(i, n, m) \ iota(i, (((n) > (m)) ? ((n) - (m)) : ((m) - (n))), (n), ((n) > (m) ? -1 : 1)) #define rep(i, n) iota(i, (n), 0, 1) #define loop for (;;) #define INF (1e9) #define EPS (1e-9) #define cons(a, b) (make_pair(a, b)) #define car(a) (a.first) #define cdr(a) (a.second) #define cadr(a) (car(cdr(a))) #define cddr(a) (cdr(cdr(a))) #define all(a) a.begin(), a.end() #define trace(var) cerr << ">>> " << #var << " = " << var << endl; typedef long long Integer; typedef double Real; typedef vector<int> vi; typedef vector<string> vs; typedef map<string, int> Dictionary; const Real PI = acos(-1); template <class S, class T> ostream &operator<<(ostream &os, pair<S, T> p) { os << '(' << car(p) << ", " << cdr(p) << ')'; return os; } template <class T> ostream &operator<<(ostream &os, vector<T> v) { if (v.size() == 0) { os << "(empty)"; return os; } os << v[0]; for (int i = 1, len = v.size(); i < len; ++i) os << ' ' << v[i]; return os; } int dx[] = {-1, 0, 1, 0}; int dy[] = {0, -1, 0, 1}; bool contains(vector<int> &vs, int x) { for (int v : vs) { if (v == x) return true; } return false; } int main() { int n; cin >> n; while (n--) { int x; cin >> x; int cx = 0; vector<int> history; while (x > 9) { int nx = 0; for (int d = 10;; d *= 10) { int z = x / d; int y = x % d; if (z == 0) break; nx = max(nx, z * y); } x = nx; ++cx; } cout << cx << endl; } return 0; }
delete
74
75
74
74
0
>>> x = 81 >>> x = 8 >>> x = 36 >>> x = 18 >>> x = 8
p01538
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> #define INF 100000000 #define EPS 1e-9 using namespace std; typedef long long ll; vector<int> ans_list; bool dp[1000000]; int main() { int Q; cin >> Q; memset(dp, 0, sizeof(dp)); while (Q) { Q--; int N; cin >> N; int ans = 0; memset(dp, 0, sizeof(dp)); while (N / 10 > 0) { if (dp[N]) { ans = -1; break; } int tmp = 0; for (int i = 1; N / pow(10, i) > 0; i++) { tmp = max(tmp, (N / (int)pow(10, i)) * (N % (int)pow(10, i))); } dp[N] = false; N = tmp; ans++; } ans_list.push_back(ans); } for (int i = 0; i < ans_list.size(); i++) { cout << ans_list[i] << endl; } return 0; }
#include <algorithm> #include <bitset> #include <cctype> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> #define INF 100000000 #define EPS 1e-9 using namespace std; typedef long long ll; vector<int> ans_list; bool dp[1000001]; int main() { int Q; cin >> Q; memset(dp, 0, sizeof(dp)); while (Q) { Q--; int N; cin >> N; int ans = 0; memset(dp, 0, sizeof(dp)); while (N / 10 > 0) { if (dp[N]) { ans = -1; break; } int tmp = 0; for (int i = 1; N / pow(10, i) > 0; i++) { tmp = max(tmp, (N / (int)pow(10, i)) * (N % (int)pow(10, i))); } dp[N] = false; N = tmp; ans++; } ans_list.push_back(ans); } for (int i = 0; i < ans_list.size(); i++) { cout << ans_list[i] << endl; } return 0; }
replace
34
35
34
35
0
p01538
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; int inf = 1000000000; int main(void) { int q, n; cin >> q; for (int i = 0; i < q; i++) { cin >> n; int cnt = 0; while (n >= 10) { int tmp = -1; string ns = to_string(n); for (int i = 0; i < ns.size() - 1; i++) { cerr << ns.substr(0, i + 1) << " " << ns.substr(i + 1) << endl; tmp = max(tmp, stoi(ns.substr(0, i + 1)) * stoi(ns.substr(i + 1))); } // cerr << tmp << endl; n = tmp; cnt++; } cout << cnt << endl; } return 0; } // EOF
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; int inf = 1000000000; int main(void) { int q, n; cin >> q; for (int i = 0; i < q; i++) { cin >> n; int cnt = 0; while (n >= 10) { int tmp = -1; string ns = to_string(n); for (int i = 0; i < ns.size() - 1; i++) { // cerr << ns.substr(0, i+1) << " " << ns.substr(i+1) << endl; tmp = max(tmp, stoi(ns.substr(0, i + 1)) * stoi(ns.substr(i + 1))); } // cerr << tmp << endl; n = tmp; cnt++; } cout << cnt << endl; } return 0; } // EOF
replace
26
27
26
27
0
9 9 8 1 1 23 12 3 3 6 1 8
p01539
C++
Runtime Error
#include <algorithm> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <fstream> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toStr(T x) { ostringstream sout; sout << x; return sout.str(); } typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<string> vs; typedef pair<int, int> pii; typedef long long ll; #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define FOR(i, a, b) for (int i = (a); i <= (b); ++i) #define REP(i, n) FOR(i, 0, (n)-1) const double EPS = 1e-12; const double PI = acos(-1.0); const int INF = INT_MAX / 10; int dx[7] = {0, 1, 1, 0, -1, -1, 0}; int dy[2][7] = {{1, 0, -1, -1, -1, 0, 0}, {1, 1, 0, -1, 0, 1, 0}}; struct state { int x, y, t, c; state(int x, int y, int t, int c) : x(x), y(y), t(t), c(c){}; bool operator<(const state &o) const { return o.c < c; } }; int main() { vvi field(200 + 1, vi(200 + 1)); int sx, sy, gx, gy, n, x, y, lx, ly; cin >> sx >> sy >> gx >> gy >> n; REP(i, n) { cin >> x >> y; field[y + 100][x + 100] = 1; } cin >> lx >> ly; priority_queue<state> Q; Q.push(state(sx + 100, sy + 100, 0, 0)); vector<vvi> cost(200 + 1, vvi(200 + 1, vi(6, INF))); cost[sy][sx][0] = 0; while (!Q.empty()) { state st = Q.top(); Q.pop(); if (st.x == gx + 100 && st.y == gy + 100) { cout << st.c << endl; return 0; } REP(d, 7) { int nx = st.x + dx[d]; int ny = st.y + dy[st.x % 2][d]; if (nx < 0 || lx < abs(nx - 100) || ny < 0 || ly < abs(ny - 100) || field[ny][nx]) { continue; } int nc = st.c; if (abs((st.x - 100) * (st.y - 100) * st.t) % 6 != d) { nc++; } if (nc < cost[ny][nx][(st.t + 1) % 6]) { cost[ny][nx][(st.t + 1) % 6] = nc; Q.push(state(nx, ny, st.t + 1, nc)); } } } cout << -1 << endl; return 0; }
#include <algorithm> #include <cctype> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <fstream> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toStr(T x) { ostringstream sout; sout << x; return sout.str(); } typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<string> vs; typedef pair<int, int> pii; typedef long long ll; #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define FOR(i, a, b) for (int i = (a); i <= (b); ++i) #define REP(i, n) FOR(i, 0, (n)-1) const double EPS = 1e-12; const double PI = acos(-1.0); const int INF = INT_MAX / 10; int dx[7] = {0, 1, 1, 0, -1, -1, 0}; int dy[2][7] = {{1, 0, -1, -1, -1, 0, 0}, {1, 1, 0, -1, 0, 1, 0}}; struct state { int x, y, t, c; state(int x, int y, int t, int c) : x(x), y(y), t(t), c(c){}; bool operator<(const state &o) const { return o.c < c; } }; int main() { vvi field(200 + 1, vi(200 + 1)); int sx, sy, gx, gy, n, x, y, lx, ly; cin >> sx >> sy >> gx >> gy >> n; REP(i, n) { cin >> x >> y; field[y + 100][x + 100] = 1; } cin >> lx >> ly; priority_queue<state> Q; Q.push(state(sx + 100, sy + 100, 0, 0)); vector<vvi> cost(200 + 1, vvi(200 + 1, vi(6, INF))); cost[sy + 100][sx + 100][0] = 0; while (!Q.empty()) { state st = Q.top(); Q.pop(); if (st.x == gx + 100 && st.y == gy + 100) { cout << st.c << endl; return 0; } REP(d, 7) { int nx = st.x + dx[d]; int ny = st.y + dy[st.x % 2][d]; if (nx < 0 || lx < abs(nx - 100) || ny < 0 || ly < abs(ny - 100) || field[ny][nx]) { continue; } int nc = st.c; if (abs((st.x - 100) * (st.y - 100) * st.t) % 6 != d) { nc++; } if (nc < cost[ny][nx][(st.t + 1) % 6]) { cost[ny][nx][(st.t + 1) % 6] = nc; Q.push(state(nx, ny, st.t + 1, nc)); } } } cout << -1 << endl; return 0; }
replace
66
67
66
67
0
p01539
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long #define INF 1000000005 #define MOD 1000000007 #define EPS 1e-10 #define rep(i, n) for (int i = 0; i < (int)n; ++i) #define each(a, b) for (auto(a) : (b)) #define all(v) (v).begin(), (v).end() #define fi first #define se second #define pb push_back #define show(x) cout << #x << " = " << (x) << endl #define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl #define svec(v) \ cout << #v << ":"; \ rep(i, v.size()) cout << " " << v[i]; \ cout << endl #define sset(s) \ cout << #s << ":"; \ each(i, s) cout << " " << i; \ cout << endl using namespace std; typedef pair<int, int> P; const int MAX_N = 102; const int dx[] = {0, 1, 1, 0, -1, -1, 0}; const int dy1[] = {1, 0, -1, -1, -1, 0, 0}; const int dy2[] = {1, 1, 0, -1, 0, 1, 0}; const int dt[] = {1, 1, 1, 1, 1, 1, 1}; struct edge { int to; int cost; }; struct pos { int x, y, t; bool operator<(const pos &another) const { if (x != another.x) { return x < another.x; } else if (y != another.y) { return y < another.y; } else { return t < another.t; } } }; int d[6 * MAX_N * MAX_N]; vector<edge> G[6 * MAX_N * MAX_N]; map<P, int> mp; map<pos, int> zip; pos trans[6 * MAX_N * MAX_N]; void dijkstra(int s) { priority_queue<P, vector<P>, greater<P>> que; d[s] = 0; que.push(P(0, s)); while (!que.empty()) { P p = que.top(); que.pop(); int v = p.second; if (d[v] < p.first) continue; vector<edge>::iterator it = G[v].begin(); rep(i, G[v].size()) { if (d[G[v][i].to] > d[v] + G[v][i].cost) { d[G[v][i].to] = d[v] + G[v][i].cost; que.push(P(d[G[v][i].to], G[v][i].to)); } } } } int main() { int sx, sy, gx, gy; cin >> sx >> sy >> gx >> gy; int n; cin >> n; rep(i, n) { int x, y; cin >> x >> y; mp[P(x, y)]++; } int lx, ly; cin >> lx >> ly; int cnt = 0; for (int i = -lx; i <= lx; i++) { for (int j = -ly; j <= ly; j++) { if (mp[P(i, j)] == 1) { continue; } rep(k, 6) { trans[cnt] = (pos){i, j, k}; zip[(pos){i, j, k}] = cnt++; } } } rep(i, cnt) { int x = trans[i].x; int y = trans[i].y; int t = trans[i].t; int ord = abs(x * y * t) % 6; rep(j, 7) { int nx, ny, nt; if (x % 2 == 0) { nx = x + dx[j]; ny = y + dy1[j]; nt = (t + dt[j]) % 6; } else { nx = x + dx[j]; ny = y + dy2[j]; nt = (t + dt[j]) % 6; } if (mp[P(nx, ny)] == 1) { continue; } if (-lx <= nx && nx <= lx && -ly <= ny && ny <= ly) { G[i].push_back((edge){zip[(pos){nx, ny, nt}], (j == ord ? 0 : 1)}); } } } rep(i, cnt) { d[i] = INF; } dijkstra(zip[(pos){sx, sy, 0}]); int ans = INF; rep(i, 6) { ans = min(ans, d[zip[(pos){gx, gy, i}]]); } if (ans == INF) { cout << "-1\n"; } else { cout << ans << "\n"; } }
#include <bits/stdc++.h> #define ll long long #define INF 1000000005 #define MOD 1000000007 #define EPS 1e-10 #define rep(i, n) for (int i = 0; i < (int)n; ++i) #define each(a, b) for (auto(a) : (b)) #define all(v) (v).begin(), (v).end() #define fi first #define se second #define pb push_back #define show(x) cout << #x << " = " << (x) << endl #define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl #define svec(v) \ cout << #v << ":"; \ rep(i, v.size()) cout << " " << v[i]; \ cout << endl #define sset(s) \ cout << #s << ":"; \ each(i, s) cout << " " << i; \ cout << endl using namespace std; typedef pair<int, int> P; const int MAX_N = 200; const int dx[] = {0, 1, 1, 0, -1, -1, 0}; const int dy1[] = {1, 0, -1, -1, -1, 0, 0}; const int dy2[] = {1, 1, 0, -1, 0, 1, 0}; const int dt[] = {1, 1, 1, 1, 1, 1, 1}; struct edge { int to; int cost; }; struct pos { int x, y, t; bool operator<(const pos &another) const { if (x != another.x) { return x < another.x; } else if (y != another.y) { return y < another.y; } else { return t < another.t; } } }; int d[6 * MAX_N * MAX_N]; vector<edge> G[6 * MAX_N * MAX_N]; map<P, int> mp; map<pos, int> zip; pos trans[6 * MAX_N * MAX_N]; void dijkstra(int s) { priority_queue<P, vector<P>, greater<P>> que; d[s] = 0; que.push(P(0, s)); while (!que.empty()) { P p = que.top(); que.pop(); int v = p.second; if (d[v] < p.first) continue; vector<edge>::iterator it = G[v].begin(); rep(i, G[v].size()) { if (d[G[v][i].to] > d[v] + G[v][i].cost) { d[G[v][i].to] = d[v] + G[v][i].cost; que.push(P(d[G[v][i].to], G[v][i].to)); } } } } int main() { int sx, sy, gx, gy; cin >> sx >> sy >> gx >> gy; int n; cin >> n; rep(i, n) { int x, y; cin >> x >> y; mp[P(x, y)]++; } int lx, ly; cin >> lx >> ly; int cnt = 0; for (int i = -lx; i <= lx; i++) { for (int j = -ly; j <= ly; j++) { if (mp[P(i, j)] == 1) { continue; } rep(k, 6) { trans[cnt] = (pos){i, j, k}; zip[(pos){i, j, k}] = cnt++; } } } rep(i, cnt) { int x = trans[i].x; int y = trans[i].y; int t = trans[i].t; int ord = abs(x * y * t) % 6; rep(j, 7) { int nx, ny, nt; if (x % 2 == 0) { nx = x + dx[j]; ny = y + dy1[j]; nt = (t + dt[j]) % 6; } else { nx = x + dx[j]; ny = y + dy2[j]; nt = (t + dt[j]) % 6; } if (mp[P(nx, ny)] == 1) { continue; } if (-lx <= nx && nx <= lx && -ly <= ny && ny <= ly) { G[i].push_back((edge){zip[(pos){nx, ny, nt}], (j == ord ? 0 : 1)}); } } } rep(i, cnt) { d[i] = INF; } dijkstra(zip[(pos){sx, sy, 0}]); int ans = INF; rep(i, 6) { ans = min(ans, d[zip[(pos){gx, gy, i}]]); } if (ans == INF) { cout << "-1\n"; } else { cout << ans << "\n"; } }
replace
26
27
26
27
0
p01539
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = a; i < b; i++) typedef long long ll; #define INF 1LL << 60 struct Comp { bool operator()(pair<int, ll> a, pair<int, ll> b) { return a.second > b.second; } }; int sx, sy, gx, gy; int lx, ly; bool box[300][300]; ll dist[300][300][6]; bool done[300][300][6]; int dx[2][7] = {{0, 1, 1, 0, -1, -1, 0}, {0, 1, 1, 0, -1, -1, 0}}; int dy[2][7] = {{1, 0, -1, -1, -1, 0, 0}, {1, 1, 0, -1, 0, 1, 0}}; ll solve() { rep(i, 0, ly * ly + 1) rep(j, 0, lx * lx + 1) rep(t, 0, 6) dist[i][j][t] = INF; rep(i, 0, ly * ly + 1) rep(j, 0, lx * lx + 1) rep(t, 0, 6) done[i][j][t] = false; priority_queue<pair<int, ll>, vector<pair<int, ll>>, Comp> que; que.push(make_pair(sy * 1000000 + sx * 1000, 0)); dist[sy][sx][0] = 0; while (!que.empty()) { auto q = que.top(); que.pop(); int y = q.first / 1000000; int x = (q.first % 1000000) / 1000; int t = q.first % 1000; if (y == gy && x == gx) return q.second; if (done[y][x][t]) continue; done[y][x][t] = false; int type = (x - lx + 1000) % 2; rep(i, 0, 7) { int yy = y + dy[type][i]; int xx = x + dx[type][i]; if (yy < 0 || 2 * ly < yy) continue; if (xx < 0 || 2 * lx < xx) continue; if (box[yy][xx]) continue; ll nc = q.second; int tmp = abs((x - lx) * (y - ly) * t) % 6; if (i != tmp) nc++; int tt = (t + 1) % 6; if (nc < dist[yy][xx][tt]) { dist[yy][xx][tt] = nc; que.push(make_pair(yy * 1000000 + xx * 1000 + tt, nc)); } } } return -1; } int bx[1010]; int by[1010]; int main() { cin.tie(0); ios::sync_with_stdio(false); while (cin >> sx >> sy >> gx >> gy) { int n; cin >> n; rep(i, 0, n) cin >> bx[i] >> by[i]; cin >> lx >> ly; sx += lx; sy += ly; gx += lx; gy += ly; rep(i, 0, 300) rep(j, 0, 300) box[i][j] = false; rep(i, 0, n) box[by[i] + ly][bx[i] + lx] = true; cout << solve() << endl; } }
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = a; i < b; i++) typedef long long ll; #define INF 1LL << 60 struct Comp { bool operator()(pair<int, ll> a, pair<int, ll> b) { return a.second > b.second; } }; int sx, sy, gx, gy; int lx, ly; bool box[300][300]; ll dist[300][300][6]; bool done[300][300][6]; int dx[2][7] = {{0, 1, 1, 0, -1, -1, 0}, {0, 1, 1, 0, -1, -1, 0}}; int dy[2][7] = {{1, 0, -1, -1, -1, 0, 0}, {1, 1, 0, -1, 0, 1, 0}}; ll solve() { rep(i, 0, ly * 2 + 1) rep(j, 0, lx * 2 + 1) rep(t, 0, 6) dist[i][j][t] = INF; rep(i, 0, ly * 2 + 1) rep(j, 0, lx * 2 + 1) rep(t, 0, 6) done[i][j][t] = false; priority_queue<pair<int, ll>, vector<pair<int, ll>>, Comp> que; que.push(make_pair(sy * 1000000 + sx * 1000, 0)); dist[sy][sx][0] = 0; while (!que.empty()) { auto q = que.top(); que.pop(); int y = q.first / 1000000; int x = (q.first % 1000000) / 1000; int t = q.first % 1000; if (y == gy && x == gx) return q.second; if (done[y][x][t]) continue; done[y][x][t] = false; int type = (x - lx + 1000) % 2; rep(i, 0, 7) { int yy = y + dy[type][i]; int xx = x + dx[type][i]; if (yy < 0 || 2 * ly < yy) continue; if (xx < 0 || 2 * lx < xx) continue; if (box[yy][xx]) continue; ll nc = q.second; int tmp = abs((x - lx) * (y - ly) * t) % 6; if (i != tmp) nc++; int tt = (t + 1) % 6; if (nc < dist[yy][xx][tt]) { dist[yy][xx][tt] = nc; que.push(make_pair(yy * 1000000 + xx * 1000 + tt, nc)); } } } return -1; } int bx[1010]; int by[1010]; int main() { cin.tie(0); ios::sync_with_stdio(false); while (cin >> sx >> sy >> gx >> gy) { int n; cin >> n; rep(i, 0, n) cin >> bx[i] >> by[i]; cin >> lx >> ly; sx += lx; sy += ly; gx += lx; gy += ly; rep(i, 0, 300) rep(j, 0, 300) box[i][j] = false; rep(i, 0, n) box[by[i] + ly][bx[i] + lx] = true; cout << solve() << endl; } }
replace
23
26
23
25
0
p01539
C++
Runtime Error
// ?????¬???????????¬?????? #include <algorithm> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++) #define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++) #define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--) #define int long long int template <typename T> void chmax(T &a, T b) { a = max(a, b); } template <typename T> void chmin(T &a, T b) { a = min(a, b); } template <typename T> void chadd(T &a, T b) { a = a + b; } typedef pair<int, int> pii; typedef long long ll; // x coordinate mod 2 = 0 int dx1[] = {0, 1, 1, 0, -1, -1}; int dy1[] = {1, 0, -1, -1, -1, 0}; // mod 2 = 1 int dx2[] = {0, 1, 1, 0, -1, -1}; int dy2[] = {1, 1, 0, -1, 0, 1}; constexpr ll INF = 1001001001001001LL; constexpr ll MOD = 1000000007LL; // ??§?¨? (x, y), ?????? z (mod 6) ????????????????°???? int rec[210][210][6]; int sx, sy, gx, gy; int N, X[210], Y[210]; bool exist[210][210]; int lx, ly; struct Elem { int x, y, t; }; signed main() { rep(i, 0, 210) rep(j, 0, 210) rep(k, 0, 6) rec[i][j][k] = INF; cin >> sx >> sy >> gx >> gy; cin >> N; rep(i, 0, N) cin >> X[i] >> Y[i]; cin >> lx >> ly; sx += lx, sy += ly, gx += lx, gy += ly; rep(i, 0, N) { exist[X[i] + lx][Y[i] + ly] = true; } rec[sx][sy][0] = 0; queue<Elem> q; q.push(Elem{sx, sy, 0}); while (!q.empty()) { Elem cur = q.front(); q.pop(); // move int ax = abs(cur.x - lx), ay = abs(cur.y - ly); int dir = (ax * ay * (cur.t)) % 6; rep(i, 0, 6) { int nx, ny; if (ax % 2) nx = cur.x + dx2[i], ny = cur.y + dy2[i]; else nx = cur.x + dx1[i], ny = cur.y + dy1[i]; if (nx < 0 || nx > 2 * lx || ny < 0 || ny > 2 * ly) continue; if (exist[nx][ny]) continue; if (rec[nx][ny][(cur.t + 1) % 6] > rec[cur.x][cur.y][cur.t] + (dir != i)) { rec[nx][ny][(cur.t + 1) % 6] = rec[cur.x][cur.y][cur.t] + (dir != i); /* if(nx-lx >= 0 && ny-ly >= 0) printf("(%lld, %lld) -> (%lld, %lld), time = %lld, cost = %lld\n", cur.x-lx, cur.y-ly, nx-lx, ny-ly, cur.t+1, rec[nx][ny][(cur.t+1)%6]); */ q.push(Elem{nx, ny, (cur.t + 1) % 6}); } } // stay if (rec[cur.x][cur.y][(cur.t + 1) % 6] > rec[cur.x][cur.y][cur.t] + 1) { rec[cur.x][cur.y][(cur.t + 1) % 6] = rec[cur.x][cur.y][cur.t] + 1; q.push(Elem{cur.x, cur.y, (cur.t + 1) % 6}); } } int ans = INF; rep(i, 0, 6) chmin(ans, rec[gx][gy][i]); if (ans == INF) cout << -1 << endl; else cout << ans << endl; return 0; }
// ?????¬???????????¬?????? #include <algorithm> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++) #define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++) #define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--) #define int long long int template <typename T> void chmax(T &a, T b) { a = max(a, b); } template <typename T> void chmin(T &a, T b) { a = min(a, b); } template <typename T> void chadd(T &a, T b) { a = a + b; } typedef pair<int, int> pii; typedef long long ll; // x coordinate mod 2 = 0 int dx1[] = {0, 1, 1, 0, -1, -1}; int dy1[] = {1, 0, -1, -1, -1, 0}; // mod 2 = 1 int dx2[] = {0, 1, 1, 0, -1, -1}; int dy2[] = {1, 1, 0, -1, 0, 1}; constexpr ll INF = 1001001001001001LL; constexpr ll MOD = 1000000007LL; // ??§?¨? (x, y), ?????? z (mod 6) ????????????????°???? int rec[210][210][6]; int sx, sy, gx, gy; int N, X[1010], Y[1010]; bool exist[210][210]; int lx, ly; struct Elem { int x, y, t; }; signed main() { rep(i, 0, 210) rep(j, 0, 210) rep(k, 0, 6) rec[i][j][k] = INF; cin >> sx >> sy >> gx >> gy; cin >> N; rep(i, 0, N) cin >> X[i] >> Y[i]; cin >> lx >> ly; sx += lx, sy += ly, gx += lx, gy += ly; rep(i, 0, N) { exist[X[i] + lx][Y[i] + ly] = true; } rec[sx][sy][0] = 0; queue<Elem> q; q.push(Elem{sx, sy, 0}); while (!q.empty()) { Elem cur = q.front(); q.pop(); // move int ax = abs(cur.x - lx), ay = abs(cur.y - ly); int dir = (ax * ay * (cur.t)) % 6; rep(i, 0, 6) { int nx, ny; if (ax % 2) nx = cur.x + dx2[i], ny = cur.y + dy2[i]; else nx = cur.x + dx1[i], ny = cur.y + dy1[i]; if (nx < 0 || nx > 2 * lx || ny < 0 || ny > 2 * ly) continue; if (exist[nx][ny]) continue; if (rec[nx][ny][(cur.t + 1) % 6] > rec[cur.x][cur.y][cur.t] + (dir != i)) { rec[nx][ny][(cur.t + 1) % 6] = rec[cur.x][cur.y][cur.t] + (dir != i); /* if(nx-lx >= 0 && ny-ly >= 0) printf("(%lld, %lld) -> (%lld, %lld), time = %lld, cost = %lld\n", cur.x-lx, cur.y-ly, nx-lx, ny-ly, cur.t+1, rec[nx][ny][(cur.t+1)%6]); */ q.push(Elem{nx, ny, (cur.t + 1) % 6}); } } // stay if (rec[cur.x][cur.y][(cur.t + 1) % 6] > rec[cur.x][cur.y][cur.t] + 1) { rec[cur.x][cur.y][(cur.t + 1) % 6] = rec[cur.x][cur.y][cur.t] + 1; q.push(Elem{cur.x, cur.y, (cur.t + 1) % 6}); } } int ans = INF; rep(i, 0, 6) chmin(ans, rec[gx][gy][i]); if (ans == INF) cout << -1 << endl; else cout << ans << endl; return 0; }
replace
53
54
53
54
0
p01539
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define LOG(...) fprintf(stderr, __VA_ARGS__) // #define LOG(...) #define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) #define RFOR(i, a, b) for (int i = (int)(b - 1); i >= (int)(a); --i) #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define RREP(i, n) for (int i = (int)(n - 1); i >= 0; --i) #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define EXIST(s, e) ((s).find(e) != (s).end()) #define SORT(c) sort(ALL(c)) #define RSORT(c) sort(RALL(c)) #define SZ(a) ((int)(a).size()) #define BIT(x, i) (((x) >> (i)) & 1) #define SQ(x) ((x) * (x)) typedef long long ll; typedef unsigned long long ull; typedef vector<bool> vb; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<vb> vvb; typedef vector<vi> vvi; typedef vector<vll> vvll; typedef vector<char> vc; typedef vector<vc> vvc; typedef vector<string> vs; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define INF 100000000 struct P { int x, y, t, ignore; bool operator>(const P &p) const { return ignore > p.ignore; } }; const int dx[] = {0, 1, 1, 0, -1, -1, 0}, dy[][7] = {{1, 0, -1, -1, -1, 0, 0}, {1, 1, 0, -1, 0, 1, 0}}; int G[201][201][6]; bool obstacle[201][201]; int main() { int ix, iy, ox, oy; cin >> ix >> iy >> ox >> oy; ix += 100; iy += 100; ox += 100; oy += 100; int n; cin >> n; REP(i, n) { int x, y; cin >> x >> y; x += 100; y += 100; obstacle[y][x] = true; } int W, H; cin >> W >> H; W += 100; H += 100; fill_n((int *)G, 201 * 201 * 6, INF); priority_queue<P, vector<P>, greater<P>> que; que.push({ix, iy, 0, 0}); G[iy][ix][0] = 0; while (!que.empty()) { P p = que.top(); que.pop(); if (p.x == ox && p.y == oy) { cout << p.ignore << endl; return 0; } int order = abs((p.x - 100) * (p.y - 100) * p.t) % 6; REP(i, 7) { int sx = p.x + dx[i]; int sy = p.y + dy[abs(p.x - 100) % 2][i]; if (-W <= sx - 100 && sx - 100 <= W && -H <= sy - 100 && sy - 100 <= H) { if (!obstacle[sy][sx]) { int t = (p.t + 1) % 6; int ignore = p.ignore + (i == order ? 0 : 1); if (G[sy][sx][t] > ignore) { G[sy][sx][t] = ignore; que.push({sx, sy, p.t + 1, ignore}); } } } } } cout << -1 << endl; }
#include <bits/stdc++.h> using namespace std; #define LOG(...) fprintf(stderr, __VA_ARGS__) // #define LOG(...) #define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) #define RFOR(i, a, b) for (int i = (int)(b - 1); i >= (int)(a); --i) #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define RREP(i, n) for (int i = (int)(n - 1); i >= 0; --i) #define ALL(a) (a).begin(), (a).end() #define RALL(a) (a).rbegin(), (a).rend() #define EXIST(s, e) ((s).find(e) != (s).end()) #define SORT(c) sort(ALL(c)) #define RSORT(c) sort(RALL(c)) #define SZ(a) ((int)(a).size()) #define BIT(x, i) (((x) >> (i)) & 1) #define SQ(x) ((x) * (x)) typedef long long ll; typedef unsigned long long ull; typedef vector<bool> vb; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<vb> vvb; typedef vector<vi> vvi; typedef vector<vll> vvll; typedef vector<char> vc; typedef vector<vc> vvc; typedef vector<string> vs; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define INF 100000000 struct P { int x, y, t, ignore; bool operator>(const P &p) const { return ignore > p.ignore; } }; const int dx[] = {0, 1, 1, 0, -1, -1, 0}, dy[][7] = {{1, 0, -1, -1, -1, 0, 0}, {1, 1, 0, -1, 0, 1, 0}}; int G[201][201][6]; bool obstacle[201][201]; int main() { int ix, iy, ox, oy; cin >> ix >> iy >> ox >> oy; ix += 100; iy += 100; ox += 100; oy += 100; int n; cin >> n; REP(i, n) { int x, y; cin >> x >> y; x += 100; y += 100; obstacle[y][x] = true; } int W, H; cin >> W >> H; fill_n((int *)G, 201 * 201 * 6, INF); priority_queue<P, vector<P>, greater<P>> que; que.push({ix, iy, 0, 0}); G[iy][ix][0] = 0; while (!que.empty()) { P p = que.top(); que.pop(); if (p.x == ox && p.y == oy) { cout << p.ignore << endl; return 0; } int order = abs((p.x - 100) * (p.y - 100) * p.t) % 6; REP(i, 7) { int sx = p.x + dx[i]; int sy = p.y + dy[abs(p.x - 100) % 2][i]; if (-W <= sx - 100 && sx - 100 <= W && -H <= sy - 100 && sy - 100 <= H) { if (!obstacle[sy][sx]) { int t = (p.t + 1) % 6; int ignore = p.ignore + (i == order ? 0 : 1); if (G[sy][sx][t] > ignore) { G[sy][sx][t] = ignore; que.push({sx, sy, p.t + 1, ignore}); } } } } } cout << -1 << endl; }
delete
64
66
64
64
0
p01539
C++
Runtime Error
#include <bits/stdc++.h> #define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i) #define REP(i, n) FOR(i, 0, n) #define ALL(x) begin(x), end(x) using namespace std; struct S { int x, y, t; }; bool operator==(S l, S r) { return l.x == r.x && l.y == r.y && l.t == r.t; } int to_i(const S &s) { return s.x + 100 + (s.y + 100) * 201 + s.t * 40401; } namespace std { template <> struct hash<S> { size_t operator()(const S &s) const { hash<int> h; return h(s.x + h(s.y + h(s.t))); } }; } // namespace std constexpr int INF = 1000000001; class Weight { public: Weight() : w(INF) {} Weight(int w) : w(w) {} operator int() const { return w; } operator int() { return w; } private: int w; }; using Array = unordered_map<S, Weight>; struct Edge { S dest; Weight weight; }; using Edges = vector<Edge>; using Graph = vector<Edges>; void add_edge(Graph &g, S src, S dest, Weight weight) { g[to_i(src)].push_back((Edge){dest, weight}); } int di[6] = {0, 1, 1, 0, -1, -1}; int dj[2][6] = {{1, 0, -1, -1, -1, 0}, {1, 1, 0, -1, 0, 1}}; void bfs01(Graph &g, Array &d, S s) { d[s] = 0; using P = pair<Weight, S>; deque<P> que; que.push_back(P(0, s)); while (!que.empty()) { P top = que.front(); que.pop_front(); Weight dist = top.first; S v = top.second; if (d[v] < dist) continue; REP(i, g[to_i(v)].size()) { Edge e = g[to_i(v)][i]; if (d[e.dest] > d[v] + e.weight) { d[e.dest] = d[v] + e.weight; if (e.weight) que.push_back(P(d[e.dest], e.dest)); else que.push_front(P(d[e.dest], e.dest)); } } } } int main() { int sx, sy, gx, gy; cin >> sx >> sy >> gx >> gy; int n; cin >> n; set<pair<int, int>> sf; REP(i, n) { int x, y; cin >> x >> y; sf.emplace(x, y); } int lx, ly; cin >> lx >> ly; Graph g; FOR(i, -lx, lx + 1) FOR(j, -ly, ly + 1) REP(k, 6) { S s = {i, j, k}; int dir = abs(i * j * k) % 6; int ni = i + di[dir]; int nj = j + dj[abs(i) % 2][dir]; if (abs(ni) > lx || abs(nj) > ly) continue; if (sf.count({ni, nj})) continue; S t = {ni, nj, (k + 1) % 6}; add_edge(g, s, t, 0); } FOR(i, -lx, lx + 1) FOR(j, -ly, ly + 1) REP(k, 6) { S s = {i, j, k}; REP(l, 6) { int ni = i + di[l]; int nj = j + dj[abs(i) % 2][l]; if (abs(ni) > lx || abs(nj) > ly) continue; if (sf.count({ni, nj})) continue; S t = {ni, nj, (k + 1) % 6}; add_edge(g, s, t, 1); } S t = {i, j, (k + 1) % 6}; add_edge(g, s, t, 1); } Array d; S s = {sx, sy, 0}; bfs01(g, d, s); Weight res; REP(i, 6) { S g = {gx, gy, i}; res = min(res, d[g]); } if (res == INF) { cout << -1 << endl; } else { cout << res << endl; } return 0; }
#include <bits/stdc++.h> #define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i) #define REP(i, n) FOR(i, 0, n) #define ALL(x) begin(x), end(x) using namespace std; struct S { int x, y, t; }; bool operator==(S l, S r) { return l.x == r.x && l.y == r.y && l.t == r.t; } int to_i(const S &s) { return s.x + 100 + (s.y + 100) * 201 + s.t * 40401; } namespace std { template <> struct hash<S> { size_t operator()(const S &s) const { hash<int> h; return h(s.x + h(s.y + h(s.t))); } }; } // namespace std constexpr int INF = 1000000001; class Weight { public: Weight() : w(INF) {} Weight(int w) : w(w) {} operator int() const { return w; } operator int() { return w; } private: int w; }; using Array = unordered_map<S, Weight>; struct Edge { S dest; Weight weight; }; using Edges = vector<Edge>; using Graph = vector<Edges>; void add_edge(Graph &g, S src, S dest, Weight weight) { g[to_i(src)].push_back((Edge){dest, weight}); } int di[6] = {0, 1, 1, 0, -1, -1}; int dj[2][6] = {{1, 0, -1, -1, -1, 0}, {1, 1, 0, -1, 0, 1}}; void bfs01(Graph &g, Array &d, S s) { d[s] = 0; using P = pair<Weight, S>; deque<P> que; que.push_back(P(0, s)); while (!que.empty()) { P top = que.front(); que.pop_front(); Weight dist = top.first; S v = top.second; if (d[v] < dist) continue; REP(i, g[to_i(v)].size()) { Edge e = g[to_i(v)][i]; if (d[e.dest] > d[v] + e.weight) { d[e.dest] = d[v] + e.weight; if (e.weight) que.push_back(P(d[e.dest], e.dest)); else que.push_front(P(d[e.dest], e.dest)); } } } } int main() { int sx, sy, gx, gy; cin >> sx >> sy >> gx >> gy; int n; cin >> n; set<pair<int, int>> sf; REP(i, n) { int x, y; cin >> x >> y; sf.emplace(x, y); } int lx, ly; cin >> lx >> ly; Graph g(250000); FOR(i, -lx, lx + 1) FOR(j, -ly, ly + 1) REP(k, 6) { S s = {i, j, k}; int dir = abs(i * j * k) % 6; int ni = i + di[dir]; int nj = j + dj[abs(i) % 2][dir]; if (abs(ni) > lx || abs(nj) > ly) continue; if (sf.count({ni, nj})) continue; S t = {ni, nj, (k + 1) % 6}; add_edge(g, s, t, 0); } FOR(i, -lx, lx + 1) FOR(j, -ly, ly + 1) REP(k, 6) { S s = {i, j, k}; REP(l, 6) { int ni = i + di[l]; int nj = j + dj[abs(i) % 2][l]; if (abs(ni) > lx || abs(nj) > ly) continue; if (sf.count({ni, nj})) continue; S t = {ni, nj, (k + 1) % 6}; add_edge(g, s, t, 1); } S t = {i, j, (k + 1) % 6}; add_edge(g, s, t, 1); } Array d; S s = {sx, sy, 0}; bfs01(g, d, s); Weight res; REP(i, 6) { S g = {gx, gy, i}; res = min(res, d[g]); } if (res == INF) { cout << -1 << endl; } else { cout << res << endl; } return 0; }
replace
84
85
84
85
-11
p01540
C++
Runtime Error
#include <algorithm> #include <iostream> #include <vector> using namespace std; int map[5001][5001] = {0}; int main() { long long int n, m, input_x, input_y; cin >> n >> m; vector<long long int> x, y, x_s, y_s; x_s.push_back(-10000000000); x_s.push_back(10000000000); y_s.push_back(-10000000000); y_s.push_back(10000000000); for (long long int i = 0; i < n; i++) { cin >> input_x >> input_y; x.push_back(input_x); y.push_back(input_y); x_s.push_back(input_x); y_s.push_back(input_y); } sort(x_s.begin(), x_s.end()); x_s.erase(unique(x_s.begin(), x_s.end()), x_s.end()); sort(y_s.begin(), y_s.end()); y_s.erase(unique(y_s.begin(), y_s.end()), y_s.end()); vector<long long int>::iterator it; for (long long int i = 0; i < n; i++) { it = lower_bound(x_s.begin(), x_s.end(), x[i]); x[i] = distance(x_s.begin(), it); it = lower_bound(y_s.begin(), y_s.end(), y[i]); y[i] = distance(y_s.begin(), it); map[x[i]][y[i]]++; } for (long long int i = 0; i < n + 2; i++) { long long int k = 0; for (long long int j = 0; j < n + 2; j++) { map[j][i] += k; k = map[j][i]; if (i > 0) { map[j][i] += map[j][i - 1]; } } } /* for ( long long int i = 0; i < n+2; i++ ) { for ( long long int j = 0; j < n+2; j++ ) { cout << map[j][i] << " "; } cout << endl; } */ long long int x1, x2, y1, y2; for (long long int i = 0; i < m; i++) { cin >> x1 >> y1 >> x2 >> y2; it = lower_bound(x_s.begin(), x_s.end(), x1); x1 = distance(x_s.begin(), it) - 1; it = lower_bound(y_s.begin(), y_s.end(), y1); y1 = distance(y_s.begin(), it) - 1; it = upper_bound(x_s.begin(), x_s.end(), x2); x2 = distance(x_s.begin(), it) - 1; it = upper_bound(y_s.begin(), y_s.end(), y2); y2 = distance(y_s.begin(), it) - 1; long long int ans = map[x2][y2]; if (x1 >= 0) ans -= map[x1][y2]; if (y1 >= 0) ans -= map[x2][y1]; if (x1 >= 0 && y1 >= 0) ans += map[x1][y1]; // cout << x1 << " " << y1 << " " << x2 << " " << y2 << endl; cout << ans << endl; } return 0; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; int map[5003][5003] = {0}; int main() { long long int n, m, input_x, input_y; cin >> n >> m; vector<long long int> x, y, x_s, y_s; x_s.push_back(-10000000000); x_s.push_back(10000000000); y_s.push_back(-10000000000); y_s.push_back(10000000000); for (long long int i = 0; i < n; i++) { cin >> input_x >> input_y; x.push_back(input_x); y.push_back(input_y); x_s.push_back(input_x); y_s.push_back(input_y); } sort(x_s.begin(), x_s.end()); x_s.erase(unique(x_s.begin(), x_s.end()), x_s.end()); sort(y_s.begin(), y_s.end()); y_s.erase(unique(y_s.begin(), y_s.end()), y_s.end()); vector<long long int>::iterator it; for (long long int i = 0; i < n; i++) { it = lower_bound(x_s.begin(), x_s.end(), x[i]); x[i] = distance(x_s.begin(), it); it = lower_bound(y_s.begin(), y_s.end(), y[i]); y[i] = distance(y_s.begin(), it); map[x[i]][y[i]]++; } for (long long int i = 0; i < n + 2; i++) { long long int k = 0; for (long long int j = 0; j < n + 2; j++) { map[j][i] += k; k = map[j][i]; if (i > 0) { map[j][i] += map[j][i - 1]; } } } /* for ( long long int i = 0; i < n+2; i++ ) { for ( long long int j = 0; j < n+2; j++ ) { cout << map[j][i] << " "; } cout << endl; } */ long long int x1, x2, y1, y2; for (long long int i = 0; i < m; i++) { cin >> x1 >> y1 >> x2 >> y2; it = lower_bound(x_s.begin(), x_s.end(), x1); x1 = distance(x_s.begin(), it) - 1; it = lower_bound(y_s.begin(), y_s.end(), y1); y1 = distance(y_s.begin(), it) - 1; it = upper_bound(x_s.begin(), x_s.end(), x2); x2 = distance(x_s.begin(), it) - 1; it = upper_bound(y_s.begin(), y_s.end(), y2); y2 = distance(y_s.begin(), it) - 1; long long int ans = map[x2][y2]; if (x1 >= 0) ans -= map[x1][y2]; if (y1 >= 0) ans -= map[x2][y1]; if (x1 >= 0 && y1 >= 0) ans += map[x1][y1]; // cout << x1 << " " << y1 << " " << x2 << " " << y2 << endl; cout << ans << endl; } return 0; }
replace
5
6
5
6
127
/tmp/d54a2bfc-2a72-4aec-bbd4-6c4a1a530ed4.out: error while loading shared libraries: libc.so.6: failed to map segment from shared object
p01540
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef vector<int> VI; #define pb push_back #define all(vec) (vec).begin(), (vec).end() const int INF = 2000000000; int x[5000], y[5000]; int sx[500000], sy[500000], ex[500000], ey[500000]; short num[5000][5000]; int find(VI &zip, int key, int type = 0) { int l = 0, r = zip.size() - 1; while (true) { if (l > r) return type ? l : r; int m = (l + r) / 2; if (zip[m] == key) return m; if (zip[m] < key) l = m + 1; else r = m - 1; } } int main() { VI zip_x; zip_x.pb(-INF); zip_x.pb(INF); VI zip_y; zip_y.pb(-INF); zip_y.pb(INF); int n, m; scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) { scanf("%d %d", &x[i], &y[i]); zip_x.pb(x[i]); zip_y.pb(y[i]); } for (int i = 0; i < m; i++) { scanf("%d %d %d %d", &sx[i], &sy[i], &ex[i], &ey[i]); // zip_x.pb(sx[i]); // zip_y.pb(sy[i]); // zip_x.pb(ex[i]); // zip_y.pb(ey[i]); } // 座標圧縮 sort(all(zip_x)); zip_x.erase(unique(all(zip_x)), zip_x.end()); sort(all(zip_y)); zip_y.erase(unique(all(zip_y)), zip_y.end()); // 圧縮後の座標を受け取る for (int i = 0; i < n; i++) { x[i] = find(zip_x, x[i]); y[i] = find(zip_y, y[i]); } for (int i = 0; i < m; i++) { sx[i] = find(zip_x, sx[i], 1); sy[i] = find(zip_y, sy[i], 1); ex[i] = find(zip_x, ex[i]); ey[i] = find(zip_y, ey[i]); } // 累積和 for (int i = 0; i < n; i++) { num[x[i]][y[i]]++; } for (int i = 1; i < zip_x.size(); i++) { for (int j = 1; j < zip_y.size(); j++) { num[i][j] += num[i - 1][j]; num[i][j] += num[i][j - 1]; num[i][j] -= num[i - 1][j - 1]; } } /* puts(""); for (int i = 0; i < zip_x.size(); i++){ for (int j = 0; j < zip_y.size(); j++){ printf("%d ", num[i][j]); } puts(""); } puts(""); */ for (int i = 0; i < m; i++) { int res = num[ex[i]][ey[i]]; res -= num[sx[i] - 1][ey[i]]; res -= num[ex[i]][sy[i] - 1]; res += num[sx[i] - 1][sy[i] - 1]; printf("%d\n", res); } return 0; }
#include <bits/stdc++.h> using namespace std; typedef vector<int> VI; #define pb push_back #define all(vec) (vec).begin(), (vec).end() const int INF = 2000000000; int x[5000], y[5000]; int sx[500000], sy[500000], ex[500000], ey[500000]; short num[5050][5050]; int find(VI &zip, int key, int type = 0) { int l = 0, r = zip.size() - 1; while (true) { if (l > r) return type ? l : r; int m = (l + r) / 2; if (zip[m] == key) return m; if (zip[m] < key) l = m + 1; else r = m - 1; } } int main() { VI zip_x; zip_x.pb(-INF); zip_x.pb(INF); VI zip_y; zip_y.pb(-INF); zip_y.pb(INF); int n, m; scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) { scanf("%d %d", &x[i], &y[i]); zip_x.pb(x[i]); zip_y.pb(y[i]); } for (int i = 0; i < m; i++) { scanf("%d %d %d %d", &sx[i], &sy[i], &ex[i], &ey[i]); // zip_x.pb(sx[i]); // zip_y.pb(sy[i]); // zip_x.pb(ex[i]); // zip_y.pb(ey[i]); } // 座標圧縮 sort(all(zip_x)); zip_x.erase(unique(all(zip_x)), zip_x.end()); sort(all(zip_y)); zip_y.erase(unique(all(zip_y)), zip_y.end()); // 圧縮後の座標を受け取る for (int i = 0; i < n; i++) { x[i] = find(zip_x, x[i]); y[i] = find(zip_y, y[i]); } for (int i = 0; i < m; i++) { sx[i] = find(zip_x, sx[i], 1); sy[i] = find(zip_y, sy[i], 1); ex[i] = find(zip_x, ex[i]); ey[i] = find(zip_y, ey[i]); } // 累積和 for (int i = 0; i < n; i++) { num[x[i]][y[i]]++; } for (int i = 1; i < zip_x.size(); i++) { for (int j = 1; j < zip_y.size(); j++) { num[i][j] += num[i - 1][j]; num[i][j] += num[i][j - 1]; num[i][j] -= num[i - 1][j - 1]; } } /* puts(""); for (int i = 0; i < zip_x.size(); i++){ for (int j = 0; j < zip_y.size(); j++){ printf("%d ", num[i][j]); } puts(""); } puts(""); */ for (int i = 0; i < m; i++) { int res = num[ex[i]][ey[i]]; res -= num[sx[i] - 1][ey[i]]; res -= num[ex[i]][sy[i] - 1]; res += num[sx[i] - 1][sy[i] - 1]; printf("%d\n", res); } return 0; }
replace
11
12
11
12
0
p01540
C++
Runtime Error
#include <bits/stdc++.h> #define fi first #define se second #define show(x) cerr << #x << "=" << x << "\n" typedef long long ll; using namespace std; // const ll MOD=(ll)1e9+7; // const ll inf=(ll)1e14; const int dy[] = {1, 0, -1}; const int dx[] = {1, 0, -1}; int n, m, h, w; vector<int> x, y, xx, yy; int imos[5001][5001]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.precision(10); cout << fixed; #ifdef LOCAL_DEFINE FILE *stream1; // FILE *stream2; stream1 = freopen("in", "r", stdin); // stream2=freopen("out","w",stdout); if (stream1 == NULL) return 0; // if(stream2==NULL)return 0; #endif cin >> n >> m; for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; xx.push_back(a); x.push_back(a); yy.push_back(b); y.push_back(b); } x.push_back(-1000000001); y.push_back(-1000000001); x.push_back(1000000001); y.push_back(1000000001); sort(x.begin(), x.end()); sort(y.begin(), y.end()); x.erase(unique(x.begin(), x.end()), x.end()); y.erase(unique(y.begin(), y.end()), y.end()); for (int i = 0; i < n; i++) { xx[i] = lower_bound(x.begin(), x.end(), xx[i]) - x.begin(); yy[i] = lower_bound(y.begin(), y.end(), yy[i]) - y.begin(); } h = (int)y.size(); w = (int)x.size(); for (int i = 0; i < n; i++) { imos[yy[i]][xx[i]]++; } for (int i = 0; i < h; i++) for (int j = 1; j < w; j++) imos[i][j] += imos[i][j - 1]; for (int i = 1; i < h; i++) for (int j = 0; j < w; j++) imos[i][j] += imos[i - 1][j]; for (int i = 0; i < m; i++) { int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; auto xa = lower_bound(x.begin(), x.end(), x1); if (*xa >= x1) xa--; auto xb = lower_bound(x.begin(), x.end(), x2); if (*xb > x2) xb--; auto ya = lower_bound(y.begin(), y.end(), y1); if (*ya >= y1) ya--; auto yb = lower_bound(y.begin(), y.end(), y2); if (*yb > y2) yb--; x1 = xa - x.begin(); x2 = xb - x.begin(); y1 = ya - y.begin(); y2 = yb - y.begin(); cout << imos[y2][x2] - imos[y2][x1] - imos[y1][x2] + imos[y1][x1] << endl; } #ifdef LOCAL_DEFINE cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s.\n"; fclose(stream1); // fclose(stream2); #endif return 0; }
#include <bits/stdc++.h> #define fi first #define se second #define show(x) cerr << #x << "=" << x << "\n" typedef long long ll; using namespace std; // const ll MOD=(ll)1e9+7; // const ll inf=(ll)1e14; const int dy[] = {1, 0, -1}; const int dx[] = {1, 0, -1}; int n, m, h, w; vector<int> x, y, xx, yy; int imos[5011][5011]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.precision(10); cout << fixed; #ifdef LOCAL_DEFINE FILE *stream1; // FILE *stream2; stream1 = freopen("in", "r", stdin); // stream2=freopen("out","w",stdout); if (stream1 == NULL) return 0; // if(stream2==NULL)return 0; #endif cin >> n >> m; for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; xx.push_back(a); x.push_back(a); yy.push_back(b); y.push_back(b); } x.push_back(-1000000001); y.push_back(-1000000001); x.push_back(1000000001); y.push_back(1000000001); sort(x.begin(), x.end()); sort(y.begin(), y.end()); x.erase(unique(x.begin(), x.end()), x.end()); y.erase(unique(y.begin(), y.end()), y.end()); for (int i = 0; i < n; i++) { xx[i] = lower_bound(x.begin(), x.end(), xx[i]) - x.begin(); yy[i] = lower_bound(y.begin(), y.end(), yy[i]) - y.begin(); } h = (int)y.size(); w = (int)x.size(); for (int i = 0; i < n; i++) { imos[yy[i]][xx[i]]++; } for (int i = 0; i < h; i++) for (int j = 1; j < w; j++) imos[i][j] += imos[i][j - 1]; for (int i = 1; i < h; i++) for (int j = 0; j < w; j++) imos[i][j] += imos[i - 1][j]; for (int i = 0; i < m; i++) { int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; auto xa = lower_bound(x.begin(), x.end(), x1); if (*xa >= x1) xa--; auto xb = lower_bound(x.begin(), x.end(), x2); if (*xb > x2) xb--; auto ya = lower_bound(y.begin(), y.end(), y1); if (*ya >= y1) ya--; auto yb = lower_bound(y.begin(), y.end(), y2); if (*yb > y2) yb--; x1 = xa - x.begin(); x2 = xb - x.begin(); y1 = ya - y.begin(); y2 = yb - y.begin(); cout << imos[y2][x2] - imos[y2][x1] - imos[y1][x2] + imos[y1][x1] << endl; } #ifdef LOCAL_DEFINE cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s.\n"; fclose(stream1); // fclose(stream2); #endif return 0; }
replace
12
13
12
13
127
/tmp/6ed1569d-9e14-41da-9f60-4e7704f21ebf.out: error while loading shared libraries: libc.so.6: failed to map segment from shared object
p01540
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; int sum[5000][5000]; int X[5000], Y[5000]; int X1[500000], X2[500000], Y1[500000], Y2[500000]; int n, m; int compress(int *x, int *X1, int *X2) { vector<int> v; rep(i, n) v.push_back(x[i]); sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); rep(i, m) { X1[i] = lower_bound(v.begin(), v.end(), X1[i]) - v.begin(); X2[i] = upper_bound(v.begin(), v.end(), X2[i]) - v.begin() - 1; } rep(i, n) { x[i] = lower_bound(v.begin(), v.end(), x[i]) - v.begin(); } return (int)v.size(); } signed main() { scanf("%d%d", &n, &m); rep(i, n) scanf("%d%d", &X[i], &Y[i]); rep(i, m) scanf("%d%d%d%d", &X1[i], &Y1[i], &X2[i], &Y2[i]); int h = compress(X, X1, X2), w = compress(Y, Y1, Y2); rep(i, n) sum[X[i]][Y[i]]++; rep(i, h + 1) for (int j = 1; j <= w; j++) { sum[i][j] += sum[i][j - 1]; } for (int i = 1; i <= h; i++) rep(j, w + 1) { sum[i][j] += sum[i - 1][j]; } rep(i, m) { if (X2[i] < 0 || Y2[i] < 0) { puts("0"); continue; } int ans = sum[X2[i]][Y2[i]]; if (X1[i] > 0) ans -= sum[X1[i] - 1][Y2[i]]; if (Y1[i] > 0) ans -= sum[X2[i]][Y1[i] - 1]; if (X1[i] > 0 && Y1[i] > 0) ans += sum[X1[i] - 1][Y1[i] - 1]; printf("%d\n", ans); } }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; int sum[6000][6000]; int X[6000], Y[6000]; int X1[600000], X2[600000], Y1[600000], Y2[600000]; int n, m; int compress(int *x, int *X1, int *X2) { vector<int> v; rep(i, n) v.push_back(x[i]); sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); rep(i, m) { X1[i] = lower_bound(v.begin(), v.end(), X1[i]) - v.begin(); X2[i] = upper_bound(v.begin(), v.end(), X2[i]) - v.begin() - 1; } rep(i, n) { x[i] = lower_bound(v.begin(), v.end(), x[i]) - v.begin(); } return (int)v.size(); } signed main() { scanf("%d%d", &n, &m); rep(i, n) scanf("%d%d", &X[i], &Y[i]); rep(i, m) scanf("%d%d%d%d", &X1[i], &Y1[i], &X2[i], &Y2[i]); int h = compress(X, X1, X2), w = compress(Y, Y1, Y2); rep(i, n) sum[X[i]][Y[i]]++; rep(i, h + 1) for (int j = 1; j <= w; j++) { sum[i][j] += sum[i][j - 1]; } for (int i = 1; i <= h; i++) rep(j, w + 1) { sum[i][j] += sum[i - 1][j]; } rep(i, m) { if (X2[i] < 0 || Y2[i] < 0) { puts("0"); continue; } int ans = sum[X2[i]][Y2[i]]; if (X1[i] > 0) ans -= sum[X1[i] - 1][Y2[i]]; if (Y1[i] > 0) ans -= sum[X2[i]][Y1[i] - 1]; if (X1[i] > 0 && Y1[i] > 0) ans += sum[X1[i] - 1][Y1[i] - 1]; printf("%d\n", ans); } }
replace
4
7
4
7
-11
p01540
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cmath> #include <cstdio> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; using ll = long long; using ull = unsigned long long; using pii = pair<int, int>; #define dump(o) \ cerr << #o; \ for (auto e : (o)) \ cerr << " " << e; \ cerr << endl; #define MOD 1000000007 #define INF 0x3f3f3f3f #define INFL 0x3f3f3f3f3f3f3f3fLL // pair????????? template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << "(" << p.first << "," << p.second << ")"; return os; } //??§?¨???§??? // v: ??§???????????§?¨??????? ???????????? template <typename T> void compress(vector<T> &v) { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); } //??§?¨???§???(map) // v: ??§???????????§?¨??????? ???????????? //?????????: zip[??§???????????§?¨?]:??§???????????§?¨? //(zip.size()<=10^5?????????????????????) template <typename T> map<T, T> compressMap(vector<T> &v) { map<T, T> zip; sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); for (T i = 0; i < v.size(); i++) zip[v[i]] = i; return zip; } //??§???????????§?¨? // index(??§???????????§?¨?, ??§???????????§?¨??????????(??????) ) template <typename T> int index(T i, vector<T> v) { return lower_bound(v.begin(), v.end(), i) - v.begin(); } #define MAX 5001 // Sample2 // https://gyazo.com/39c67d1abe7a6a686acb52285f8a0bc9 int main() { // 2?¬?????´??????? static int cumsum[MAX][MAX] = {}; int xs[MAX], ys[MAX]; int n, m; cin >> n >> m; vector<int> x(n + 1), y(n + 1); for (int i = 0; i < n; i++) { cin >> xs[i] >> ys[i]; x[i] = xs[i]; y[i] = ys[i]; } compress(x); compress(y); // map<int, int> zipx(compressMap(x)); // map<int, int> zipy(compressMap(y)); // dump(x); // dump(zipx); // dump(y); // dump(zipy); for (int i = 0; i < n; i++) { int vx = index(xs[i], x); int vy = index(ys[i], y); cumsum[vx + 1][vy + 1]++; } for (int vy = 0; vy < y.size(); vy++) for (int vx = 0; vx < x.size(); vx++) cumsum[vx + 1][vy + 1] += cumsum[vx + 1][vy] + cumsum[vx][vy + 1] - cumsum[vx][vy]; for (int i = 0; i < m; i++) { int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; x1 = index(x1, x); x2 = index(x2 + 1, x); y1 = index(y1, y); y2 = index(y2 + 1, y); cout << cumsum[x2][y2] - cumsum[x2][y1] - cumsum[x1][y2] + cumsum[x1][y1] << endl; } return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cmath> #include <cstdio> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; using ll = long long; using ull = unsigned long long; using pii = pair<int, int>; #define dump(o) \ cerr << #o; \ for (auto e : (o)) \ cerr << " " << e; \ cerr << endl; #define MOD 1000000007 #define INF 0x3f3f3f3f #define INFL 0x3f3f3f3f3f3f3f3fLL // pair????????? template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << "(" << p.first << "," << p.second << ")"; return os; } //??§?¨???§??? // v: ??§???????????§?¨??????? ???????????? template <typename T> void compress(vector<T> &v) { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); } //??§?¨???§???(map) // v: ??§???????????§?¨??????? ???????????? //?????????: zip[??§???????????§?¨?]:??§???????????§?¨? //(zip.size()<=10^5?????????????????????) template <typename T> map<T, T> compressMap(vector<T> &v) { map<T, T> zip; sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); for (T i = 0; i < v.size(); i++) zip[v[i]] = i; return zip; } //??§???????????§?¨? // index(??§???????????§?¨?, ??§???????????§?¨??????????(??????) ) template <typename T> int index(T i, vector<T> v) { return lower_bound(v.begin(), v.end(), i) - v.begin(); } #define MAX 5100 // Sample2 // https://gyazo.com/39c67d1abe7a6a686acb52285f8a0bc9 int main() { // 2?¬?????´??????? static int cumsum[MAX][MAX] = {}; int xs[MAX], ys[MAX]; int n, m; cin >> n >> m; vector<int> x(n + 1), y(n + 1); for (int i = 0; i < n; i++) { cin >> xs[i] >> ys[i]; x[i] = xs[i]; y[i] = ys[i]; } compress(x); compress(y); // map<int, int> zipx(compressMap(x)); // map<int, int> zipy(compressMap(y)); // dump(x); // dump(zipx); // dump(y); // dump(zipy); for (int i = 0; i < n; i++) { int vx = index(xs[i], x); int vy = index(ys[i], y); cumsum[vx + 1][vy + 1]++; } for (int vy = 0; vy < y.size(); vy++) for (int vx = 0; vx < x.size(); vx++) cumsum[vx + 1][vy + 1] += cumsum[vx + 1][vy] + cumsum[vx][vy + 1] - cumsum[vx][vy]; for (int i = 0; i < m; i++) { int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; x1 = index(x1, x); x2 = index(x2 + 1, x); y1 = index(y1, y); y2 = index(y2 + 1, y); cout << cumsum[x2][y2] - cumsum[x2][y1] - cumsum[x1][y2] + cumsum[x1][y1] << endl; } return 0; }
replace
68
69
68
69
127
/tmp/675f7277-fc01-4638-8efe-0ac8a7d7c658.out: error while loading shared libraries: libc.so.6: failed to map segment from shared object
p01540
C++
Runtime Error
#include <algorithm> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define fst first #define scd second #define PB push_back #define MP make_pair #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define omajinai \ ios::sync_with_stdio(false); \ cin.tie(0) #define rep(i, x) for (int i = 0; i < (int)(x); ++i) #define rep1(i, x) for (int i = 1; i <= (int)(x); ++i) using ll = long long; using ld = long double; using vi = vector<int>; using vvi = vector<vi>; using pii = pair<int, int>; using vpii = vector<pii>; template <class T, class U> ostream &operator<<(ostream &os, const pair<T, U> p) { os << "(" << p.fst << ", " << p.scd << ")"; return os; } template <class T> ostream &operator<<(ostream &os, const vector<T> v) { rep(i, v.size()) { if (i) os << ", "; os << v[i]; } return os; } template <typename T> T &max(T &a, T &b) { if (a >= b) return a; return b; } template <typename T> T &min(T &a, T &b) { if (a < b) return a; return b; } template <typename T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <typename T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } constexpr int inf = 1e9; constexpr ll linf = 3e18; constexpr double eps = 1e-9; int n, m; int x[5005], y[5005]; vi compressed_x, compressed_y; int a[5005][5005]; int main() { cin >> n >> m; rep(i, n) { cin >> x[i] >> y[i]; compressed_x.PB(x[i]); compressed_y.PB(y[i]); } sort(all(compressed_x)); sort(all(compressed_y)); compressed_x.erase(unique(all(compressed_x)), end(compressed_x)); compressed_y.erase(unique(all(compressed_y)), end(compressed_y)); rep(i, n) { int xidx = lower_bound(all(compressed_x), x[i]) - begin(compressed_x); int yidx = lower_bound(all(compressed_y), y[i]) - begin(compressed_y); a[yidx][xidx]++; } for (int i = 0; i < compressed_y.size() + 1; ++i) { for (int j = 0; j < compressed_x.size() + 1; ++j) { if (i - 1 >= 0) a[i][j] += a[i - 1][j]; } } for (int i = 0; i < compressed_x.size() + 1; ++i) { for (int j = 0; j < compressed_y.size() + 1; ++j) { if (i - 1 >= 0) a[j][i] += a[j][i - 1]; } } compressed_x.PB(inf); compressed_y.PB(inf); rep(i, m) { int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; x1 = lower_bound(all(compressed_x), x1) - begin(compressed_x); x2 = upper_bound(all(compressed_x), x2) - begin(compressed_x) - 1; y1 = lower_bound(all(compressed_y), y1) - begin(compressed_y); y2 = upper_bound(all(compressed_y), y2) - begin(compressed_y) - 1; int ans = 0; ans += a[y2][x2]; if (y1 - 1 >= 0) ans -= a[y1 - 1][x2]; if (x1 - 1 >= 0) ans -= a[y2][x1 - 1]; if (y1 - 1 >= 0 && x1 - 1 >= 0) ans += a[y1 - 1][x1 - 1]; cout << ans << endl; } }
#include <algorithm> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define fst first #define scd second #define PB push_back #define MP make_pair #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define omajinai \ ios::sync_with_stdio(false); \ cin.tie(0) #define rep(i, x) for (int i = 0; i < (int)(x); ++i) #define rep1(i, x) for (int i = 1; i <= (int)(x); ++i) using ll = long long; using ld = long double; using vi = vector<int>; using vvi = vector<vi>; using pii = pair<int, int>; using vpii = vector<pii>; template <class T, class U> ostream &operator<<(ostream &os, const pair<T, U> p) { os << "(" << p.fst << ", " << p.scd << ")"; return os; } template <class T> ostream &operator<<(ostream &os, const vector<T> v) { rep(i, v.size()) { if (i) os << ", "; os << v[i]; } return os; } template <typename T> T &max(T &a, T &b) { if (a >= b) return a; return b; } template <typename T> T &min(T &a, T &b) { if (a < b) return a; return b; } template <typename T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <typename T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } constexpr int inf = 1e9; constexpr ll linf = 3e18; constexpr double eps = 1e-9; int n, m; int x[5005], y[5005]; vi compressed_x, compressed_y; int a[5005][5005]; int main() { cin >> n >> m; rep(i, n) { cin >> x[i] >> y[i]; compressed_x.PB(x[i]); compressed_y.PB(y[i]); } sort(all(compressed_x)); sort(all(compressed_y)); compressed_x.erase(unique(all(compressed_x)), end(compressed_x)); compressed_y.erase(unique(all(compressed_y)), end(compressed_y)); rep(i, n) { int xidx = lower_bound(all(compressed_x), x[i]) - begin(compressed_x); int yidx = lower_bound(all(compressed_y), y[i]) - begin(compressed_y); a[yidx][xidx]++; } for (int i = 0; i < compressed_y.size() + 1; ++i) { for (int j = 0; j < compressed_x.size() + 1; ++j) { if (i - 1 >= 0) a[i][j] += a[i - 1][j]; } } for (int i = 0; i < compressed_x.size() + 1; ++i) { for (int j = 0; j < compressed_y.size() + 1; ++j) { if (i - 1 >= 0) a[j][i] += a[j][i - 1]; } } compressed_x.PB(inf); compressed_y.PB(inf); rep(i, m) { int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; x1 = lower_bound(all(compressed_x), x1) - begin(compressed_x); x2 = upper_bound(all(compressed_x), x2) - begin(compressed_x) - 1; y1 = lower_bound(all(compressed_y), y1) - begin(compressed_y); y2 = upper_bound(all(compressed_y), y2) - begin(compressed_y) - 1; if (x2 < 0 || y2 < 0) { cout << 0 << endl; continue; } int ans = 0; ans += a[y2][x2]; if (y1 - 1 >= 0) ans -= a[y1 - 1][x2]; if (x1 - 1 >= 0) ans -= a[y2][x1 - 1]; if (y1 - 1 >= 0 && x1 - 1 >= 0) ans += a[y1 - 1][x1 - 1]; cout << ans << endl; } }
insert
136
136
136
141
127
/tmp/3d92c6e2-6fa2-407c-b0a8-80758dd77cb8.out: error while loading shared libraries: libc.so.6: failed to map segment from shared object
p01540
C++
Memory Limit Exceeded
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> using namespace std; typedef long long ll; int N, M; ll fie[5111][5111]; ll rui[5111][5111]; ll xy[2][5001]; ll X[5001], Y[5001]; int W, H; int compress(int id, ll *x) { vector<ll> xs; for (int i = 0; i < N; i++) { xs.push_back(xy[id][i]); } sort(xs.begin(), xs.end()); xs.erase(unique(xs.begin(), xs.end()), xs.end()); for (int i = 0; i < N; i++) { ll a = find(xs.begin(), xs.end(), xy[id][i]) - xs.begin(); a++; x[a] = xy[id][i]; xy[id][i] = a; } x[0] = -10000000000; return (int)xs.size(); } int nibun(ll a, ll *b, int wh, bool f) { /* cout << a << " :: wh = " << wh << endl; for(int i=0;i<=W;i++) cout << b[i] << " "; cout << endl; */ int res = lower_bound(b, b + wh + 1, a) - b; // cout << res << endl; if (b[res] > a && !f) res--; return res; } void view() { for (int y = 0; y <= H + 1; y++) { for (int x = 0; x <= W + 1; x++) { cout << rui[x][y] << " "; } cout << endl; } } int main() { cin >> N >> M; for (int i = 0; i < N; i++) { cin >> xy[0][i] >> xy[1][i]; } W = compress(0, X); H = compress(1, Y); /* cout << "W = " << W << endl; for(int i=1;i<=W;i++) cout << X[i] << " "; cout << endl; cout << "H = " << H << endl; for(int i=1;i<=H;i++) cout << Y[i] << " "; cout << endl; */ for (int i = 0; i < N; i++) { fie[xy[0][i]][xy[1][i]]++; } for (int y = 1; y <= H + 1; y++) { for (int x = 1; x <= W + 1; x++) { rui[x][y] = rui[x - 1][y] + fie[x][y]; } } for (int y = 1; y <= H + 1; y++) { for (int x = 1; x <= W + 1; x++) { rui[x][y] += rui[x][y - 1]; } } for (int i = 0; i < M; i++) { ll x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; x1 = nibun(x1, X, W, true); y1 = nibun(y1, Y, H, true); x2 = nibun(x2, X, W, false); y2 = nibun(y2, Y, H, false); /* cout << x1 << " " << y1 << " " << x2 << " " << y2 << endl; cout << rui[x2][y2] << " - " << rui[x2][y1-1] << " - " << rui[x1-1][y2] << " + " << rui[x1][y1] << endl; */ int res = rui[x2][y2] - rui[x2][y1 - 1] - rui[x1 - 1][y2] + rui[x1 - 1][y1 - 1]; cout << res << endl; } }
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> using namespace std; typedef long long ll; int N, M; int fie[5005][5005]; int rui[5005][5005]; ll xy[2][5001]; ll X[5001], Y[5001]; int W, H; int compress(int id, ll *x) { vector<ll> xs; for (int i = 0; i < N; i++) { xs.push_back(xy[id][i]); } sort(xs.begin(), xs.end()); xs.erase(unique(xs.begin(), xs.end()), xs.end()); for (int i = 0; i < N; i++) { ll a = find(xs.begin(), xs.end(), xy[id][i]) - xs.begin(); a++; x[a] = xy[id][i]; xy[id][i] = a; } x[0] = -10000000000; return (int)xs.size(); } int nibun(ll a, ll *b, int wh, bool f) { /* cout << a << " :: wh = " << wh << endl; for(int i=0;i<=W;i++) cout << b[i] << " "; cout << endl; */ int res = lower_bound(b, b + wh + 1, a) - b; // cout << res << endl; if (b[res] > a && !f) res--; return res; } void view() { for (int y = 0; y <= H + 1; y++) { for (int x = 0; x <= W + 1; x++) { cout << rui[x][y] << " "; } cout << endl; } } int main() { cin >> N >> M; for (int i = 0; i < N; i++) { cin >> xy[0][i] >> xy[1][i]; } W = compress(0, X); H = compress(1, Y); /* cout << "W = " << W << endl; for(int i=1;i<=W;i++) cout << X[i] << " "; cout << endl; cout << "H = " << H << endl; for(int i=1;i<=H;i++) cout << Y[i] << " "; cout << endl; */ for (int i = 0; i < N; i++) { fie[xy[0][i]][xy[1][i]]++; } for (int y = 1; y <= H + 1; y++) { for (int x = 1; x <= W + 1; x++) { rui[x][y] = rui[x - 1][y] + fie[x][y]; } } for (int y = 1; y <= H + 1; y++) { for (int x = 1; x <= W + 1; x++) { rui[x][y] += rui[x][y - 1]; } } for (int i = 0; i < M; i++) { ll x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; x1 = nibun(x1, X, W, true); y1 = nibun(y1, Y, H, true); x2 = nibun(x2, X, W, false); y2 = nibun(y2, Y, H, false); /* cout << x1 << " " << y1 << " " << x2 << " " << y2 << endl; cout << rui[x2][y2] << " - " << rui[x2][y1-1] << " - " << rui[x1-1][y2] << " + " << rui[x1][y1] << endl; */ int res = rui[x2][y2] - rui[x2][y1 - 1] - rui[x1 - 1][y2] + rui[x1 - 1][y1 - 1]; cout << res << endl; } }
replace
14
16
14
16
MLE
p01540
C++
Memory Limit Exceeded
#include <algorithm> #include <bitset> #include <ciso646> #include <cmath> #include <cstdio> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <vector> using namespace std; inline bool cheak(int x, int y, int xMax, int yMax) { return x >= 0 && y >= 0 && xMax > x && yMax > y; } inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } template <class T> inline T sqr(T x) { return x * x; } typedef pair<int, int> P; typedef tuple<int, int, int> T; typedef long long ll; typedef unsigned long long ull; #define For(i, a, b) for (int(i) = (a); i < (b); (i)++) #define rep(i, n) For(i, 0, n) #define clr(a) memset((a), 0, sizeof(a)) #define mclr(a) memset((a), -1, sizeof(a)) #define all(a) (a).begin(), (a).end() #define sz(a) (sizeof(a)) #define Fill(a, v) fill((int *)a, (int *)(a + (sz(a) / sz(*(a)))), v) const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1}, dy[8] = {0, -1, 0, 1, -1, 1, -1, 1}; const int mod = 1000000007; const int INF = 1e9 + 2; vector<vector<short>> d; int main() { int n, m; cin >> n >> m; vector<int> ix(n), iy(n); //??§?¨???§?????¨???????????? vector<int> ax, ay; map<int, int> mx, my; rep(i, n) { cin >> ix[i] >> iy[i]; rep(j, 3) { ax.push_back(ix[i] + j - 1); ay.push_back(iy[i] + j - 1); } } ax.push_back(-INF - 1); ay.push_back(-INF - 1); ax.push_back(INF + 1); ay.push_back(INF + 1); sort(all(ax)); sort(all(ay)); rep(i, ax.size()) { mx[ax[i]] = i; } rep(i, ay.size()) { my[ay[i]] = i; } // d[x][y] 0 - x 0 - y ??????????????????????±????????????? d.resize(ax.size() + 1, vector<short>(ay.size() + 1, 0)); rep(i, n) { d[mx[ix[i]]][my[iy[i]]]++; } For(x, 1, ax.size() + 1) For(y, 1, ay.size() + 1) { d[x][y] = d[x - 1][y] + d[x][y - 1] - d[x - 1][y - 1] + d[x][y]; } rep(i, m) { int qx1, qy1, qx2, qy2; cin >> qx1 >> qy1 >> qx2 >> qy2; int rx, ry, lx, ly; qx1--; qy1--; if (mx.count(qx1)) { lx = mx[qx1]; } else { mx[qx1] = -1; auto p = mx.find(qx1); lx = prev(p)->second; mx.erase(qx1); } if (my.count(qy1)) { ly = my[qy1]; } else { my[qy1] = -1; auto p = my.find(qy1); ly = prev(p)->second; my.erase(qy1); } if (mx.count(qx2)) { rx = mx[qx2]; } else { mx[qx2] = -1; auto p = mx.find(qx2); rx = next(p)->second; mx.erase(qx2); } if (my.count(qy2)) { ry = my[qy2]; } else { my[qy2] = -1; auto p = my.find(qy2); ry = next(p)->second; my.erase(qy2); } cout << d[rx][ry] + d[lx][ly] - d[lx][ry] - d[rx][ly] << endl; } return 0; }
#include <algorithm> #include <bitset> #include <ciso646> #include <cmath> #include <cstdio> #include <cstring> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <vector> using namespace std; inline bool cheak(int x, int y, int xMax, int yMax) { return x >= 0 && y >= 0 && xMax > x && yMax > y; } inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } template <class T> inline T sqr(T x) { return x * x; } typedef pair<int, int> P; typedef tuple<int, int, int> T; typedef long long ll; typedef unsigned long long ull; #define For(i, a, b) for (int(i) = (a); i < (b); (i)++) #define rep(i, n) For(i, 0, n) #define clr(a) memset((a), 0, sizeof(a)) #define mclr(a) memset((a), -1, sizeof(a)) #define all(a) (a).begin(), (a).end() #define sz(a) (sizeof(a)) #define Fill(a, v) fill((int *)a, (int *)(a + (sz(a) / sz(*(a)))), v) const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1}, dy[8] = {0, -1, 0, 1, -1, 1, -1, 1}; const int mod = 1000000007; const int INF = 1e9 + 2; vector<vector<short>> d; int main() { int n, m; cin >> n >> m; vector<int> ix(n), iy(n); //??§?¨???§?????¨???????????? vector<int> ax, ay; map<int, int> mx, my; rep(i, n) { cin >> ix[i] >> iy[i]; rep(j, 2) { ax.push_back(ix[i] + j - 1); ay.push_back(iy[i] + j - 1); } } ax.push_back(-INF - 1); ay.push_back(-INF - 1); ax.push_back(INF + 1); ay.push_back(INF + 1); sort(all(ax)); sort(all(ay)); rep(i, ax.size()) { mx[ax[i]] = i; } rep(i, ay.size()) { my[ay[i]] = i; } // d[x][y] 0 - x 0 - y ??????????????????????±????????????? d.resize(ax.size() + 1, vector<short>(ay.size() + 1, 0)); rep(i, n) { d[mx[ix[i]]][my[iy[i]]]++; } For(x, 1, ax.size() + 1) For(y, 1, ay.size() + 1) { d[x][y] = d[x - 1][y] + d[x][y - 1] - d[x - 1][y - 1] + d[x][y]; } rep(i, m) { int qx1, qy1, qx2, qy2; cin >> qx1 >> qy1 >> qx2 >> qy2; int rx, ry, lx, ly; qx1--; qy1--; if (mx.count(qx1)) { lx = mx[qx1]; } else { mx[qx1] = -1; auto p = mx.find(qx1); lx = prev(p)->second; mx.erase(qx1); } if (my.count(qy1)) { ly = my[qy1]; } else { my[qy1] = -1; auto p = my.find(qy1); ly = prev(p)->second; my.erase(qy1); } if (mx.count(qx2)) { rx = mx[qx2]; } else { mx[qx2] = -1; auto p = mx.find(qx2); rx = next(p)->second; mx.erase(qx2); } if (my.count(qy2)) { ry = my[qy2]; } else { my[qy2] = -1; auto p = my.find(qy2); ry = next(p)->second; my.erase(qy2); } cout << d[rx][ry] + d[lx][ly] - d[lx][ry] - d[rx][ly] << endl; } return 0; }
replace
69
70
69
70
MLE
p01540
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; typedef pair<int, int> P; P p[5000]; vector<int> dat[10000]; void init(int k, int l, int r) { if (r - l == 1) { dat[k].push_back(p[l].second); return; } int lb = k * 2 + 1, rb = k * 2 + 2; init(lb, l, (l + r) / 2); init(rb, (l + r) / 2, r); dat[k].resize(r - l); merge(dat[lb].begin(), dat[lb].end(), dat[rb].begin(), dat[rb].end(), dat[k].begin()); } int query(int a, int b, int c, int d, int k, int l, int r) { if (b <= l || r <= a) return 0; if (a <= l && r <= b) { int u = upper_bound(dat[k].begin(), dat[k].end(), d) - dat[k].begin(); int v = upper_bound(dat[k].begin(), dat[k].end(), c - 1) - dat[k].begin(); return u - v; } int lb = query(a, b, c, d, k * 2 + 1, l, (l + r) / 2); int rb = query(a, b, c, d, k * 2 + 2, (l + r) / 2, r); return lb + rb; } int main() { int n, q; scanf("%d%d", &n, &q); rep(i, n) scanf("%d%d", &p[i].first, &p[i].second); sort(p, p + n); init(0, 0, n); rep(i, q) { int sx, tx, sy, ty; scanf("%d%d%d%d", &sx, &sy, &tx, &ty); int a = lower_bound(p, p + n, P(sx, INT_MIN)) - p; int b = lower_bound(p, p + n, P(tx, INT_MAX)) - p; printf("%d\n", query(a, b, sy, ty, 0, 0, n)); } }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; typedef pair<int, int> P; P p[5000]; vector<int> dat[50000]; void init(int k, int l, int r) { if (r - l == 1) { dat[k].push_back(p[l].second); return; } int lb = k * 2 + 1, rb = k * 2 + 2; init(lb, l, (l + r) / 2); init(rb, (l + r) / 2, r); dat[k].resize(r - l); merge(dat[lb].begin(), dat[lb].end(), dat[rb].begin(), dat[rb].end(), dat[k].begin()); } int query(int a, int b, int c, int d, int k, int l, int r) { if (b <= l || r <= a) return 0; if (a <= l && r <= b) { int u = upper_bound(dat[k].begin(), dat[k].end(), d) - dat[k].begin(); int v = upper_bound(dat[k].begin(), dat[k].end(), c - 1) - dat[k].begin(); return u - v; } int lb = query(a, b, c, d, k * 2 + 1, l, (l + r) / 2); int rb = query(a, b, c, d, k * 2 + 2, (l + r) / 2, r); return lb + rb; } int main() { int n, q; scanf("%d%d", &n, &q); rep(i, n) scanf("%d%d", &p[i].first, &p[i].second); sort(p, p + n); init(0, 0, n); rep(i, q) { int sx, tx, sy, ty; scanf("%d%d%d%d", &sx, &sy, &tx, &ty); int a = lower_bound(p, p + n, P(sx, INT_MIN)) - p; int b = lower_bound(p, p + n, P(tx, INT_MAX)) - p; printf("%d\n", query(a, b, sy, ty, 0, 0, n)); } }
replace
6
7
6
7
0
p01540
C++
Runtime Error
#include <algorithm> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++) #define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++) #define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--) #define int long long int template <typename T> void chmax(T &a, T b) { a = max(a, b); } template <typename T> void chmin(T &a, T b) { a = min(a, b); } template <typename T> void chadd(T &a, T b) { a = a + b; } typedef pair<int, int> pii; typedef long long ll; int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; constexpr ll INF = 1001001001001001LL; constexpr ll MOD = 1000000007LL; map<int, int> mx, my; vector<int> vx, vy; int N, M, x[1010], y[1010]; int board[1010][1010]; signed main() { cin >> N >> M; rep(i, 0, N) { cin >> x[i] >> y[i]; if (!mx.count(x[i])) vx.push_back(x[i]); if (!my.count(y[i])) vy.push_back(y[i]); mx[x[i]]; my[y[i]]; } sort(vx.begin(), vx.end()); sort(vy.begin(), vy.end()); vx.push_back(INF); vy.push_back(INF); // compression int k = 1; for (auto &e : mx) { e.second = k++; // printf("mx: %lld -> %lld\n", e.first, e.second); } k = 1; for (auto &e : my) { e.second = k++; // printf("my: %lld -> %lld\n", e.first, e.second); } // accumulate rep(i, 0, N) { // printf("treasure exist: (%lld, %lld)\n", mx[x[i]], my[y[i]]); board[mx[x[i]]][my[y[i]]]++; } rep(i, 0, N + 1) rep(j, 0, N + 1) board[i + 1][j + 1] += board[i + 1][j]; rep(j, 0, N + 1) rep(i, 0, N + 1) board[i + 1][j + 1] += board[i][j + 1]; // solve rep(i, 0, M) { int xl, yl, xr, yr; cin >> xl >> yl >> xr >> yr; // 1-indexed xl = lower_bound(vx.begin(), vx.end(), xl) - vx.begin(); yl = lower_bound(vy.begin(), vy.end(), yl) - vy.begin(); xr = upper_bound(vx.begin(), vx.end(), xr) - vx.begin(); yr = upper_bound(vy.begin(), vy.end(), yr) - vy.begin(); // printf("xl = %lld, yl = %lld, xr = %lld, yr = %lld\n", xl, yl, xr, yr); int ans = board[xr][yr] - board[xr][yl] - board[xl][yr] + board[xl][yl]; cout << ans << endl; } return 0; }
#include <algorithm> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; #define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++) #define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++) #define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--) #define int long long int template <typename T> void chmax(T &a, T b) { a = max(a, b); } template <typename T> void chmin(T &a, T b) { a = min(a, b); } template <typename T> void chadd(T &a, T b) { a = a + b; } typedef pair<int, int> pii; typedef long long ll; int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; constexpr ll INF = 1001001001001001LL; constexpr ll MOD = 1000000007LL; map<int, int> mx, my; vector<int> vx, vy; int N, M, x[5010], y[5010]; int board[5010][5010]; signed main() { cin >> N >> M; rep(i, 0, N) { cin >> x[i] >> y[i]; if (!mx.count(x[i])) vx.push_back(x[i]); if (!my.count(y[i])) vy.push_back(y[i]); mx[x[i]]; my[y[i]]; } sort(vx.begin(), vx.end()); sort(vy.begin(), vy.end()); vx.push_back(INF); vy.push_back(INF); // compression int k = 1; for (auto &e : mx) { e.second = k++; // printf("mx: %lld -> %lld\n", e.first, e.second); } k = 1; for (auto &e : my) { e.second = k++; // printf("my: %lld -> %lld\n", e.first, e.second); } // accumulate rep(i, 0, N) { // printf("treasure exist: (%lld, %lld)\n", mx[x[i]], my[y[i]]); board[mx[x[i]]][my[y[i]]]++; } rep(i, 0, N + 1) rep(j, 0, N + 1) board[i + 1][j + 1] += board[i + 1][j]; rep(j, 0, N + 1) rep(i, 0, N + 1) board[i + 1][j + 1] += board[i][j + 1]; // solve rep(i, 0, M) { int xl, yl, xr, yr; cin >> xl >> yl >> xr >> yr; // 1-indexed xl = lower_bound(vx.begin(), vx.end(), xl) - vx.begin(); yl = lower_bound(vy.begin(), vy.end(), yl) - vy.begin(); xr = upper_bound(vx.begin(), vx.end(), xr) - vx.begin(); yr = upper_bound(vy.begin(), vy.end(), yr) - vy.begin(); // printf("xl = %lld, yl = %lld, xr = %lld, yr = %lld\n", xl, yl, xr, yr); int ans = board[xr][yr] - board[xr][yl] - board[xl][yr] + board[xl][yl]; cout << ans << endl; } return 0; }
replace
44
46
44
46
0
p01540
C++
Runtime Error
#include <bits/stdc++.h> // #define int long long using namespace std; using pii = pair<int, int>; #define REPF(i, a, n) for (int i = (a); i < (int)(n); ++i) #define REP(i, n) REPF(i, 0, n) #define ALL(c) begin(c), end(c) int sum[5100][5100]; signed main() { int n, m; cin >> n >> m; vector<int> x(n), y(n); vector<pii> p(n); REP(i, n) { cin >> p[i].first >> p[i].second; x[i] = p[i].first; y[i] = p[i].second; } sort(ALL(x)); x.erase(unique(ALL(x)), end(x)); sort(ALL(y)); y.erase(unique(ALL(y)), end(y)); auto get_x = [&](int i) { return distance(begin(x), lower_bound(ALL(x), i)); }; auto get_y = [&](int i) { return distance(begin(y), lower_bound(ALL(y), i)); }; REP(i, n) { int xx = get_x(p[i].first); int yy = get_y(p[i].second); sum[xx][yy]++; } REP(i, 5100) REPF(j, 1, 5100) sum[i][j] += sum[i][j - 1]; REP(j, 5100) REPF(i, 1, 5100) sum[i][j] += sum[i - 1][j]; REP(i, m) { int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; int xx1 = get_x(x1); int yy1 = get_y(y1); int xx2 = get_x(x2); int yy2 = get_y(y2); if (find(ALL(x), x2) == end(x)) xx2--; if (find(ALL(y), y2) == end(y)) yy2--; int ans = sum[xx2][yy2]; if (xx1 > 0) { ans -= sum[xx1 - 1][yy2]; } if (yy1 > 0) { ans -= sum[xx2][yy1 - 1]; } if (xx1 > 0 && yy1 > 0) { ans += sum[xx1 - 1][yy1 - 1]; } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> // #define int long long using namespace std; using pii = pair<int, int>; #define REPF(i, a, n) for (int i = (a); i < (int)(n); ++i) #define REP(i, n) REPF(i, 0, n) #define ALL(c) begin(c), end(c) int sum[5100][5100]; signed main() { int n, m; cin >> n >> m; vector<int> x(n), y(n); vector<pii> p(n); REP(i, n) { cin >> p[i].first >> p[i].second; x[i] = p[i].first; y[i] = p[i].second; } sort(ALL(x)); x.erase(unique(ALL(x)), end(x)); sort(ALL(y)); y.erase(unique(ALL(y)), end(y)); auto get_x = [&](int i) { return distance(begin(x), lower_bound(ALL(x), i)); }; auto get_y = [&](int i) { return distance(begin(y), lower_bound(ALL(y), i)); }; REP(i, n) { int xx = get_x(p[i].first); int yy = get_y(p[i].second); sum[xx][yy]++; } REP(i, 5100) REPF(j, 1, 5100) sum[i][j] += sum[i][j - 1]; REP(j, 5100) REPF(i, 1, 5100) sum[i][j] += sum[i - 1][j]; REP(i, m) { int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; int xx1 = get_x(x1); int yy1 = get_y(y1); int xx2 = get_x(x2); int yy2 = get_y(y2); if (find(ALL(x), x2) == end(x)) xx2--; if (find(ALL(y), y2) == end(y)) yy2--; if (xx2 < xx1 || yy2 < yy1) { cout << 0 << endl; continue; } int ans = sum[xx2][yy2]; if (xx1 > 0) { ans -= sum[xx1 - 1][yy2]; } if (yy1 > 0) { ans -= sum[xx2][yy1 - 1]; } if (xx1 > 0 && yy1 > 0) { ans += sum[xx1 - 1][yy1 - 1]; } cout << ans << endl; } return 0; }
insert
51
51
51
56
127
/tmp/83b59fe4-28eb-4052-a34c-2ec923aa4c05.out: error while loading shared libraries: libstdc++.so.6: failed to map segment from shared object
p01540
C++
Memory Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll segN = (1LL << 32); vector<ll> G; struct data { vector<ll> *v; bool flg; data *ch[2]; void init() { v = new vector<ll>; flg = false; ch[0] = (data *)malloc(sizeof(data)); ch[1] = (data *)malloc(sizeof(data)); ch[0]->ch[0] = NULL; ch[1]->ch[0] = NULL; } void add(ll i, ll x, ll l, ll r) { // cout<<i<<' '<<x<<' '<<l<<' '<<r<<endl; ll m = (l + r) / 2; if (ch[0] == NULL) init(); if (i < l || r <= i) return; v->push_back(x); if (r - l == 1) return; ch[0]->add(i, x, l, m); ch[1]->add(i, x, m, r); } void add(ll i, ll x) { add(i, x, 0, segN); } ll sum(ll a, ll b, ll c, ll d, ll l, ll r) { ll m = (l + r) / 2; if (ch[0] == NULL) init(); if (b <= l || r <= a) return 0; if (a <= l && r <= b) { if (!flg) sort(v->begin(), v->end()); flg = true; vector<ll>::iterator A, B; A = lower_bound(v->begin(), v->end(), c); B = upper_bound(v->begin(), v->end(), d); return B - A; } ll res = 0; res += ch[0]->sum(a, b, c, d, l, m); res += ch[1]->sum(a, b, c, d, m, r); return res; } ll sum(ll a, ll b, ll c, ll d) { return sum(a, b, c, d, 0, segN); } }; data T; int main() { ll d = 1e9; int n, m; int ax, ay, bx, by; T.init(); scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) { scanf("%d %d", &ax, &ay); T.add(ax + d, ay); } for (int i = 0; i < m; i++) { scanf("%d %d %d %d", &ax, &ay, &bx, &by); cout << T.sum(ax + d, bx + d + 1, ay, by) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll segN = (1LL << 32); vector<ll> G; struct data { vector<ll> *v; bool flg; data *ch[2]; void init() { v = new vector<ll>; flg = false; ch[0] = (data *)malloc(sizeof(data)); ch[1] = (data *)malloc(sizeof(data)); ch[0]->ch[0] = NULL; ch[1]->ch[0] = NULL; } void add(ll i, ll x, ll l, ll r) { // cout<<i<<' '<<x<<' '<<l<<' '<<r<<endl; ll m = (l + r) / 2; if (ch[0] == NULL) init(); if (i < l || r <= i) return; v->push_back(x); if (r - l == 1) return; ch[0]->add(i, x, l, m); ch[1]->add(i, x, m, r); } void add(ll i, ll x) { add(i, x, 0, segN); } ll sum(ll a, ll b, ll c, ll d, ll l, ll r) { ll m = (l + r) / 2; if (ch[0] == NULL) return 0; if (b <= l || r <= a) return 0; if (a <= l && r <= b) { if (!flg) sort(v->begin(), v->end()); flg = true; vector<ll>::iterator A, B; A = lower_bound(v->begin(), v->end(), c); B = upper_bound(v->begin(), v->end(), d); return B - A; } ll res = 0; res += ch[0]->sum(a, b, c, d, l, m); res += ch[1]->sum(a, b, c, d, m, r); return res; } ll sum(ll a, ll b, ll c, ll d) { return sum(a, b, c, d, 0, segN); } }; data T; int main() { ll d = 1e9; int n, m; int ax, ay, bx, by; T.init(); scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) { scanf("%d %d", &ax, &ay); T.add(ax + d, ay); } for (int i = 0; i < m; i++) { scanf("%d %d %d %d", &ax, &ay, &bx, &by); cout << T.sum(ax + d, bx + d + 1, ay, by) << endl; } return 0; }
replace
39
40
39
40
MLE
p01540
C++
Memory Limit Exceeded
#include <algorithm> #include <bits/stdc++.h> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <math.h> #include <queue> #include <string> #include <vector> // #include <boost/multiprecision/cpp_int.hpp> #include <functional> #define int long long #define inf 1000000007 #define pa pair<int, int> #define ll long long #define pal pair<double, pa> #define ppap pair<pa, int> #define ssa pair<string, int> #define mp make_pair #define pb push_back #define EPS (1e-10) #define equals(a, b) (fabs((a) - (b)) < EPS) int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; using namespace std; class pas { public: int x, y, h; pas(int x = 0, int y = 0, int h = 0) : x(x), y(y), h(h) {} bool operator<(const pas &p) const { return x != p.x ? x < p.x : y < p.y; } bool operator==(const pas &p) const { return abs(x - p.x) == 0 && abs(y - p.y) == 0; } }; class pa2 { public: int x, y; pa2(int x = 0, int y = 0) : x(x), y(y) {} pa2 operator+(pa2 p) { return pa2(x + p.x, y + p.y); } bool operator<(const pa2 &p) const { return x != p.x ? x < p.x : y < p.y; } bool operator==(const pa2 &p) const { return abs(x - p.x) == 0 && abs(y - p.y) == 0; } }; #define ppa pair<int, pas> class Point { public: double x, y; Point(double x = 0, double y = 0) : x(x), y(y) {} Point operator+(Point p) { return Point(x + p.x, y + p.y); } Point operator-(Point p) { return Point(x - p.x, y - p.y); } Point operator*(double a) { return Point(x * a, y * a); } Point operator/(double a) { return Point(x / a, y / a); } double absv() { return sqrt(norm()); } double norm() { return x * x + y * y; } bool operator<(const Point &p) const { return x != p.x ? x < p.x : y < p.y; } bool operator==(const Point &p) const { return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS; } }; typedef Point Vector; #define pl pair<int, pas> struct Segment { Point p1, p2; }; double dot(Vector a, Vector b) { return a.x * b.x + a.y * b.y; } double cross(Vector a, Vector b) { return a.x * b.y - a.y * b.x; } bool parareru(Point a, Point b, Point c, Point d) { // if(abs(cross(a-b,d-c))<EPS)cout<<"dd "<<cross(a-b,d-c)<<endl; return abs(cross(a - b, d - c)) < EPS; } double distance_ls_p(Point a, Point b, Point c) { if (dot(b - a, c - a) < EPS) return (c - a).absv(); if (dot(a - b, c - b) < EPS) return (c - b).absv(); return abs(cross(b - a, c - a)) / (b - a).absv(); } bool is_intersected_ls(Segment a, Segment b) { if (a.p1 == b.p1 || a.p2 == b.p1 || a.p1 == b.p2 || a.p2 == b.p2) return false; if (parareru((a.p2), (a.p1), (a.p1), (b.p2)) && parareru((a.p2), (a.p1), (a.p1), (b.p1))) { // cout<<"sss"<<endl; if (dot(a.p1 - b.p1, a.p1 - b.p2) < EPS) return true; if (dot(a.p2 - b.p1, a.p2 - b.p2) < EPS) return true; if (dot(a.p1 - b.p1, a.p2 - b.p1) < EPS) return true; if (dot(a.p1 - b.p2, a.p2 - b.p2) < EPS) return true; return false; } else return (cross(a.p2 - a.p1, b.p1 - a.p1) * cross(a.p2 - a.p1, b.p2 - a.p1) < EPS) && (cross(b.p2 - b.p1, a.p1 - b.p1) * cross(b.p2 - b.p1, a.p2 - b.p1) < EPS); } double segment_dis(Segment a, Segment b) { if (is_intersected_ls(a, b)) return 0; double r = distance_ls_p(a.p1, a.p2, b.p1); r = min(r, distance_ls_p(a.p1, a.p2, b.p2)); r = min(r, distance_ls_p(b.p1, b.p2, a.p2)); r = min(r, distance_ls_p(b.p1, b.p2, a.p1)); return r; } Point intersection_ls(Segment a, Segment b) { Point ba = b.p2 - b.p1; double d1 = abs(cross(ba, a.p1 - b.p1)); double d2 = abs(cross(ba, a.p2 - b.p1)); double t = d1 / (d1 + d2); return a.p1 + (a.p2 - a.p1) * t; } string itos(int i) { ostringstream s; s << i; return s.str(); } int gcd(int v, int b) { if (v > b) return gcd(b, v); if (v == b) return b; if (b % v == 0) return v; return gcd(v, b % v); } double distans(double x1, double y1, double x2, double y2) { double rr = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); return sqrt(rr); } // int pr[2000010]; // int inv[2000010]; /* int beki(int wa,int rr,int warukazu){ if(rr==0) return 1ll; if(rr==1) return wa%warukazu; if(rr%2==1) return (beki(wa,rr-1,warukazu)*wa)%warukazu; int zx=beki(wa,rr/2,warukazu); return (zx*zx)%warukazu; } int comb(int nn,int rr){ int r=pr[nn]*inv[rr]; r%=inf; r*=inv[nn-rr]; r%=inf; return r; } void gya(int ert){ pr[0]=1; for(int i=1;i<ert;i++){ pr[i]=(pr[i-1]*i)%inf; } for(int i=0;i<ert;i++) inv[i]=beki(pr[i],inf-2,inf); } */ // sort(ve.begin(),ve.end(),greater<int>()); //----------------kokomade tenpure------------ // vector<double> ans(100000000),ans2(100000000); int xx[5500], yy[5500]; int dp[6000][6000] = {0}; int dp2[6000][6000] = {0}; set<int> sex, sey; map<int, int> maxx, may; vector<int> vex, vey; signed main() { int n, m; cin >> n >> m; for (int i = 0; i < n; i++) { cin >> xx[i] >> yy[i]; sex.insert(xx[i]); sey.insert(yy[i]); } int cnt = 1; for (auto it = sex.begin(); it != sex.end(); it++) { maxx[*it] = cnt; cnt++; vex.pb(*it); } cnt = 1; for (auto it = sey.begin(); it != sey.end(); it++) { may[*it] = cnt; cnt++; vey.pb(*it); } for (int i = 0; i < n; i++) { dp[maxx[xx[i]]][may[yy[i]]]++; // cout<<maxx[xx[i]]<<" "<<may[yy[i]]<<endl; } for (int i = 1; i < 6000; i++) for (int j = 1; j < 6000; j++) { dp2[i][j] = dp2[i - 1][j] + dp2[i][j - 1] - dp2[i - 1][j - 1] + dp[i][j]; } for (int i = 0; i < m; i++) { int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; x1--, y1--; auto itx1 = upper_bound(vex.begin(), vex.end(), x1) - vex.begin(); auto itx2 = upper_bound(vex.begin(), vex.end(), x2) - vex.begin(); auto ity1 = upper_bound(vey.begin(), vey.end(), y1) - vey.begin(); auto ity2 = upper_bound(vey.begin(), vey.end(), y2) - vey.begin(); // cout<<itx1<<" "<<ity1<<" "<<itx2<<" "<<ity2<<endl; cout << dp2[itx2][ity2] - dp2[itx1][ity2] - dp2[itx2][ity1] + dp2[itx1][ity1] << endl; } return 0; }
#include <algorithm> #include <bits/stdc++.h> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <math.h> #include <queue> #include <string> #include <vector> // #include <boost/multiprecision/cpp_int.hpp> #include <functional> // #define int long long #define inf 1000000007 #define pa pair<int, int> #define ll long long #define pal pair<double, pa> #define ppap pair<pa, int> #define ssa pair<string, int> #define mp make_pair #define pb push_back #define EPS (1e-10) #define equals(a, b) (fabs((a) - (b)) < EPS) int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; using namespace std; class pas { public: int x, y, h; pas(int x = 0, int y = 0, int h = 0) : x(x), y(y), h(h) {} bool operator<(const pas &p) const { return x != p.x ? x < p.x : y < p.y; } bool operator==(const pas &p) const { return abs(x - p.x) == 0 && abs(y - p.y) == 0; } }; class pa2 { public: int x, y; pa2(int x = 0, int y = 0) : x(x), y(y) {} pa2 operator+(pa2 p) { return pa2(x + p.x, y + p.y); } bool operator<(const pa2 &p) const { return x != p.x ? x < p.x : y < p.y; } bool operator==(const pa2 &p) const { return abs(x - p.x) == 0 && abs(y - p.y) == 0; } }; #define ppa pair<int, pas> class Point { public: double x, y; Point(double x = 0, double y = 0) : x(x), y(y) {} Point operator+(Point p) { return Point(x + p.x, y + p.y); } Point operator-(Point p) { return Point(x - p.x, y - p.y); } Point operator*(double a) { return Point(x * a, y * a); } Point operator/(double a) { return Point(x / a, y / a); } double absv() { return sqrt(norm()); } double norm() { return x * x + y * y; } bool operator<(const Point &p) const { return x != p.x ? x < p.x : y < p.y; } bool operator==(const Point &p) const { return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS; } }; typedef Point Vector; #define pl pair<int, pas> struct Segment { Point p1, p2; }; double dot(Vector a, Vector b) { return a.x * b.x + a.y * b.y; } double cross(Vector a, Vector b) { return a.x * b.y - a.y * b.x; } bool parareru(Point a, Point b, Point c, Point d) { // if(abs(cross(a-b,d-c))<EPS)cout<<"dd "<<cross(a-b,d-c)<<endl; return abs(cross(a - b, d - c)) < EPS; } double distance_ls_p(Point a, Point b, Point c) { if (dot(b - a, c - a) < EPS) return (c - a).absv(); if (dot(a - b, c - b) < EPS) return (c - b).absv(); return abs(cross(b - a, c - a)) / (b - a).absv(); } bool is_intersected_ls(Segment a, Segment b) { if (a.p1 == b.p1 || a.p2 == b.p1 || a.p1 == b.p2 || a.p2 == b.p2) return false; if (parareru((a.p2), (a.p1), (a.p1), (b.p2)) && parareru((a.p2), (a.p1), (a.p1), (b.p1))) { // cout<<"sss"<<endl; if (dot(a.p1 - b.p1, a.p1 - b.p2) < EPS) return true; if (dot(a.p2 - b.p1, a.p2 - b.p2) < EPS) return true; if (dot(a.p1 - b.p1, a.p2 - b.p1) < EPS) return true; if (dot(a.p1 - b.p2, a.p2 - b.p2) < EPS) return true; return false; } else return (cross(a.p2 - a.p1, b.p1 - a.p1) * cross(a.p2 - a.p1, b.p2 - a.p1) < EPS) && (cross(b.p2 - b.p1, a.p1 - b.p1) * cross(b.p2 - b.p1, a.p2 - b.p1) < EPS); } double segment_dis(Segment a, Segment b) { if (is_intersected_ls(a, b)) return 0; double r = distance_ls_p(a.p1, a.p2, b.p1); r = min(r, distance_ls_p(a.p1, a.p2, b.p2)); r = min(r, distance_ls_p(b.p1, b.p2, a.p2)); r = min(r, distance_ls_p(b.p1, b.p2, a.p1)); return r; } Point intersection_ls(Segment a, Segment b) { Point ba = b.p2 - b.p1; double d1 = abs(cross(ba, a.p1 - b.p1)); double d2 = abs(cross(ba, a.p2 - b.p1)); double t = d1 / (d1 + d2); return a.p1 + (a.p2 - a.p1) * t; } string itos(int i) { ostringstream s; s << i; return s.str(); } int gcd(int v, int b) { if (v > b) return gcd(b, v); if (v == b) return b; if (b % v == 0) return v; return gcd(v, b % v); } double distans(double x1, double y1, double x2, double y2) { double rr = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); return sqrt(rr); } // int pr[2000010]; // int inv[2000010]; /* int beki(int wa,int rr,int warukazu){ if(rr==0) return 1ll; if(rr==1) return wa%warukazu; if(rr%2==1) return (beki(wa,rr-1,warukazu)*wa)%warukazu; int zx=beki(wa,rr/2,warukazu); return (zx*zx)%warukazu; } int comb(int nn,int rr){ int r=pr[nn]*inv[rr]; r%=inf; r*=inv[nn-rr]; r%=inf; return r; } void gya(int ert){ pr[0]=1; for(int i=1;i<ert;i++){ pr[i]=(pr[i-1]*i)%inf; } for(int i=0;i<ert;i++) inv[i]=beki(pr[i],inf-2,inf); } */ // sort(ve.begin(),ve.end(),greater<int>()); //----------------kokomade tenpure------------ // vector<double> ans(100000000),ans2(100000000); int xx[5500], yy[5500]; int dp[6000][6000] = {0}; int dp2[6000][6000] = {0}; set<int> sex, sey; map<int, int> maxx, may; vector<int> vex, vey; signed main() { int n, m; cin >> n >> m; for (int i = 0; i < n; i++) { cin >> xx[i] >> yy[i]; sex.insert(xx[i]); sey.insert(yy[i]); } int cnt = 1; for (auto it = sex.begin(); it != sex.end(); it++) { maxx[*it] = cnt; cnt++; vex.pb(*it); } cnt = 1; for (auto it = sey.begin(); it != sey.end(); it++) { may[*it] = cnt; cnt++; vey.pb(*it); } for (int i = 0; i < n; i++) { dp[maxx[xx[i]]][may[yy[i]]]++; // cout<<maxx[xx[i]]<<" "<<may[yy[i]]<<endl; } for (int i = 1; i < 6000; i++) for (int j = 1; j < 6000; j++) { dp2[i][j] = dp2[i - 1][j] + dp2[i][j - 1] - dp2[i - 1][j - 1] + dp[i][j]; } for (int i = 0; i < m; i++) { int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; x1--, y1--; auto itx1 = upper_bound(vex.begin(), vex.end(), x1) - vex.begin(); auto itx2 = upper_bound(vex.begin(), vex.end(), x2) - vex.begin(); auto ity1 = upper_bound(vey.begin(), vey.end(), y1) - vey.begin(); auto ity2 = upper_bound(vey.begin(), vey.end(), y2) - vey.begin(); // cout<<itx1<<" "<<ity1<<" "<<itx2<<" "<<ity2<<endl; cout << dp2[itx2][ity2] - dp2[itx1][ity2] - dp2[itx2][ity1] + dp2[itx1][ity1] << endl; } return 0; }
replace
12
13
12
13
MLE
p01540
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int mp[5005][5005], n, m; vector<int> x, y; // O(n * log n ) n=v.size() vector<int> compress(vector<int> v) { // v = {4,6,7,1,1,6} // -> res={1,2,3,0,0,2} vector<int> copy = v; sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); vector<int> res; for (int i = 0; i < (int)copy.size(); i++) { int num = lower_bound(v.begin(), v.end(), copy[i]) - v.begin(); res.push_back(num); } return res; } int sum(int sx, int sy, int gx, int gy) { sx--, sy--; int res = mp[gx][gy]; if (sx >= 0) res -= mp[sx][gy]; if (sy >= 0) res -= mp[gx][sy]; if (sx >= 0 && sy >= 0) res += mp[sx][sy]; return res; } int main() { cin >> n >> m; x.resize(n), y.resize(n); for (int i = 0; i < n; i++) cin >> x[i] >> y[i]; vector<int> X, Y; X = compress(x), Y = compress(y); for (int i = 0; i < n; i++) mp[X[i]][Y[i]]++; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) mp[i][j + 1] += mp[i][j]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) mp[j + 1][i] += mp[j][i]; sort(x.begin(), x.end()); sort(y.begin(), y.end()); x.erase(unique(x.begin(), x.end()), x.end()); y.erase(unique(y.begin(), y.end()), y.end()); while (m--) { int a, b, c, d; cin >> a >> b >> c >> d; int sx = lower_bound(x.begin(), x.end(), a) - x.begin(); int sy = lower_bound(y.begin(), y.end(), b) - y.begin(); int gx = upper_bound(x.begin(), x.end(), c) - x.begin() - 1; int gy = upper_bound(y.begin(), y.end(), d) - y.begin() - 1; cout << sum(sx, sy, gx, gy) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int mp[5005][5005], n, m; vector<int> x, y; // O(n * log n ) n=v.size() vector<int> compress(vector<int> v) { // v = {4,6,7,1,1,6} // -> res={1,2,3,0,0,2} vector<int> copy = v; sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); vector<int> res; for (int i = 0; i < (int)copy.size(); i++) { int num = lower_bound(v.begin(), v.end(), copy[i]) - v.begin(); res.push_back(num); } return res; } int sum(int sx, int sy, int gx, int gy) { sx--, sy--; if (gx < 0 || gy < 0) return 0; int res = mp[gx][gy]; if (sx >= 0) res -= mp[sx][gy]; if (sy >= 0) res -= mp[gx][sy]; if (sx >= 0 && sy >= 0) res += mp[sx][sy]; return res; } int main() { cin >> n >> m; x.resize(n), y.resize(n); for (int i = 0; i < n; i++) cin >> x[i] >> y[i]; vector<int> X, Y; X = compress(x), Y = compress(y); for (int i = 0; i < n; i++) mp[X[i]][Y[i]]++; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) mp[i][j + 1] += mp[i][j]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) mp[j + 1][i] += mp[j][i]; sort(x.begin(), x.end()); sort(y.begin(), y.end()); x.erase(unique(x.begin(), x.end()), x.end()); y.erase(unique(y.begin(), y.end()), y.end()); while (m--) { int a, b, c, d; cin >> a >> b >> c >> d; int sx = lower_bound(x.begin(), x.end(), a) - x.begin(); int sy = lower_bound(y.begin(), y.end(), b) - y.begin(); int gx = upper_bound(x.begin(), x.end(), c) - x.begin() - 1; int gy = upper_bound(y.begin(), y.end(), d) - y.begin() - 1; cout << sum(sx, sy, gx, gy) << endl; } return 0; }
insert
25
25
25
27
127
/tmp/b1377cd3-fdb6-485d-924d-3b07c602f107.out: error while loading shared libraries: libc.so.6: failed to map segment from shared object
p01542
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cctype> #include <complex> #include <cstring> #include <iomanip> #include <iostream> #include <limits.h> #include <map> #include <math.h> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <string> #include <vector> #define VARIABLE(x) cerr << #x << "=" << x << endl #define BINARY(x) static_cast<bitset<16>>(x); #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define REP(i, m, n) for (int i = m; i < (int)(n); i++) #define if_range(x, y, w, h) \ if (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) && \ (int)(y) < (int)(h)) const int INF = 1000000000; const double EPS = 1e-8; const double PI = 3.14159; int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0}; using namespace std; // typedef pair<int, int> P; struct P { int x, y; P(int x, int y) : x(x), y(y) {} P() {} }; /** Prbolem2428 : Lost Number **/ int p; int N; string c = "01+-*()"; string S; int number(string s), expression(string s), factor(string s), term(string s); bool err; void valid(int n) { if (n < 0 || 1023 < n) err = true; } int factor(string s) { if (s[p] == '(') { p++; if (s[p] == '+' || s[p] == '-' || s[p] == '*' || s[p] == ')') err = true; int res = expression(s); if (s[p] != ')') err = true; p++; valid(res); return res; } else if (s[p] == ')') { err = true; return -1; } else if (isdigit(s[p])) { return number(s); } else { err = true; } } int term(string s) { int ret = factor(s); while (s[p] == '*') { p++; ret *= factor(s); valid(ret); } valid(ret); return ret; } int expression(string s) { int res = term(s); for (;;) { if (s[p] == '+') { p++; res += term(s); } else if (s[p] == '-') { p++; res -= term(s); } else { break; } valid(res); } valid(res); return res; } int number(string s) { int res = 0; while (isdigit(s[p])) { res *= 2; res += s[p++] - '0'; } valid(res); return res; } bool isOpe(char c) { return (c == '+' || c == '-' || c == '*'); } bool check(string s) { vector<int> ope; if (s[N - 1] == '+' || s[N - 1] == '-' || s[N - 1] == '*' || s[N - 1] == '(') return false; rep(i, s.size() - 1) { if (isOpe(s[i]) && isOpe(s[i + 1])) return false; } int d = -1; rep(i, s.size()) { if (s[i] == '(') { d++; if (i == s.size() - 1 || s[i + 1] == '+' || s[i + 1] == '-' || s[i + 1] == '*' || s[i + 1] == ')') return false; if (i != 0 && !isOpe(s[i - 1]) && s[i - 1] != '(') return false; ope.push_back(0); } else if (s[i] == ')') { if (d < 0 || !ope[d]) return false; ope.erase(ope.begin() + d); d--; } else if (s[i] == '+' || s[i] == '-' || s[i] == '*') { if (d >= 0) ope[d] = 1; } } if (d != -1) return false; return ope.end() == find(ope.begin(), ope.end(), 0); } int solve(int n, string s) { if (n == N) { p = 0; err = false; if (!check(s)) err = 1; int res = expression(s); if (res >= 1024) res = -1; cerr << "Persing : " << s << endl; cerr << " res : " << (err ? -1 : res) << endl; return err ? -1 : res; } int res = -1; if (S[n] != '.') res = solve(n + 1, s + S[n]); else rep(i, 7) { // if (n==N-1 && i>1) continue; res = max(res, solve(n + 1, s + c[i])); } return res; } int main() { cin >> S; N = (int)S.size(); int res = solve(0, ""); cout << max(-1, res) << endl; }
#include <algorithm> #include <bitset> #include <cctype> #include <complex> #include <cstring> #include <iomanip> #include <iostream> #include <limits.h> #include <map> #include <math.h> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <string> #include <vector> #define VARIABLE(x) cerr << #x << "=" << x << endl #define BINARY(x) static_cast<bitset<16>>(x); #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define REP(i, m, n) for (int i = m; i < (int)(n); i++) #define if_range(x, y, w, h) \ if (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) && \ (int)(y) < (int)(h)) const int INF = 1000000000; const double EPS = 1e-8; const double PI = 3.14159; int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0}; using namespace std; // typedef pair<int, int> P; struct P { int x, y; P(int x, int y) : x(x), y(y) {} P() {} }; /** Prbolem2428 : Lost Number **/ int p; int N; string c = "01+-*()"; string S; int number(string s), expression(string s), factor(string s), term(string s); bool err; void valid(int n) { if (n < 0 || 1023 < n) err = true; } int factor(string s) { if (s[p] == '(') { p++; if (s[p] == '+' || s[p] == '-' || s[p] == '*' || s[p] == ')') err = true; int res = expression(s); if (s[p] != ')') err = true; p++; valid(res); return res; } else if (s[p] == ')') { err = true; return -1; } else if (isdigit(s[p])) { return number(s); } else { err = true; } } int term(string s) { int ret = factor(s); while (s[p] == '*') { p++; ret *= factor(s); valid(ret); } valid(ret); return ret; } int expression(string s) { int res = term(s); for (;;) { if (s[p] == '+') { p++; res += term(s); } else if (s[p] == '-') { p++; res -= term(s); } else { break; } valid(res); } valid(res); return res; } int number(string s) { int res = 0; while (isdigit(s[p])) { res *= 2; res += s[p++] - '0'; } valid(res); return res; } bool isOpe(char c) { return (c == '+' || c == '-' || c == '*'); } bool check(string s) { vector<int> ope; if (s[N - 1] == '+' || s[N - 1] == '-' || s[N - 1] == '*' || s[N - 1] == '(') return false; rep(i, s.size() - 1) { if (isOpe(s[i]) && isOpe(s[i + 1])) return false; } int d = -1; rep(i, s.size()) { if (s[i] == '(') { d++; if (i == s.size() - 1 || s[i + 1] == '+' || s[i + 1] == '-' || s[i + 1] == '*' || s[i + 1] == ')') return false; if (i != 0 && !isOpe(s[i - 1]) && s[i - 1] != '(') return false; ope.push_back(0); } else if (s[i] == ')') { if (d < 0 || !ope[d]) return false; ope.erase(ope.begin() + d); d--; } else if (s[i] == '+' || s[i] == '-' || s[i] == '*') { if (d >= 0) ope[d] = 1; } } if (d != -1) return false; return ope.end() == find(ope.begin(), ope.end(), 0); } int solve(int n, string s) { if (n == N) { p = 0; err = false; if (!check(s)) err = 1; int res = expression(s); if (res >= 1024) res = -1; // cerr << "Persing : " << s << endl; // cerr << " res : " << (err?-1:res) << endl; return err ? -1 : res; } int res = -1; if (S[n] != '.') res = solve(n + 1, s + S[n]); else rep(i, 7) { // if (n==N-1 && i>1) continue; res = max(res, solve(n + 1, s + c[i])); } return res; } int main() { cin >> S; N = (int)S.size(); int res = solve(0, ""); cout << max(-1, res) << endl; }
replace
172
174
172
174
0
Persing : 000 res : 0
p01543
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <ctime> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <tuple> #include <typeinfo> #include <vector> using namespace std; typedef long long ll; const int MAX_V = 110; const int INT_INF = 1 << 30; typedef pair<int, int> P; typedef tuple<int, int, int> T; int G[MAX_V + 1][MAX_V + 1]; int h[MAX_V * 2 + 2], dist[MAX_V * 2 + 2]; int prevv[MAX_V * 2 + 2]; bool used[MAX_V * 2 + 2]; bool flow[MAX_V + 1][MAX_V + 1] = {}; int bit_matching_cost(int l, int r) { int min_f = 0; for (int i = 0; i < l; i++) { for (int j = 0; j < r; j++) { min_f = min(min_f, G[i][j]); } } for (int i = 0; i < l; i++) { for (int j = 0; j < r; j++) { G[i][j] -= min_f; } } for (int i = 0; i < l; i++) { G[i][r] = 0; flow[i][r] = true; } for (int i = 0; i < r; i++) { G[l][i] = 0; flow[l][i] = true; } G[l][r] = INT_INF; flow[l][r] = false; int res = 0; fill_n(h, l + r + 2, 0); while (true) { fill_n(dist, l + r + 2, INT_INF); dist[l + r + 1] = 0; fill_n(used, l + r + 2, false); while (true) { int v = -1; for (int i = 0; i < l + r + 2; i++) { if (!used[i] && (v == -1 || dist[i] < dist[v])) { v = i; } } if (v == -1) break; if (v < l + 1) { used[v] = true; for (int i = l + 1; i < l + r + 2; i++) { if (!flow[v][i - (l + 1)] && dist[i] > dist[v] + G[v][i - (l + 1)] + h[v] - h[i]) { dist[i] = dist[v] + G[v][i = (l + 1)] + h[v] - h[i]; prevv[i] = v; } } } else { used[v] = true; for (int i = 0; i < l + 1; i++) { if (flow[i][v - (l + 1)] && dist[i] > dist[v] - G[i][v - (l + 1)] + h[v] - h[i]) { dist[i] = dist[v] - G[i][v - (l + 1)] + h[v] - h[i]; prevv[i] = v - (l + 1); } } } } if (dist[l] >= INT_INF) { break; } for (int v = 0; v < l + r + 2; v++) { h[v] += dist[v]; } res += h[l]; res += min_f; bool flag = true; for (int v = l; !(v == r && !flag); v = (flag) ? prevv[v] : prevv[v + l + 1], flag = !flag) { if (flag) { flow[v][prevv[v]] = false; } else { flow[prevv[v + l + 1]][v] = true; } } } return res; } int cost[MAX_V][MAX_V]; const int MAX_N = 105; int N; int W[MAX_N][MAX_N], E[MAX_N][MAX_N]; bool F[MAX_N][MAX_N], F2[MAX_N][MAX_N]; int main(int argc, char *argv[]) { cin >> N; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { scanf("%d", &(W[i][j])); } } for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { scanf("%d", &(E[i][j])); } } for (int i = 0; i < N; i++) { string s; cin >> s; for (int j = 0; j < N; j++) { F[i][j] = (s[j] == 'o'); } } int f = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (F[i][j]) { G[i][j] = -E[i][j]; f += E[i][j]; } else { G[i][j] = W[i][j]; } } } f += bit_matching_cost(N, N); int c = 0; vector<T> q; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (F[i][j] != flow[i][j]) { c++; q.push_back(T(i, j, F[i][j])); } } } printf("%d\n%d\n", f, c); for (T t : q) { printf("%d %d %s\n", get<0>(t) + 1, get<1>(t) + 1, get<2>(t) ? "erase" : "write"); } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <ctime> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <tuple> #include <typeinfo> #include <vector> using namespace std; typedef long long ll; const int MAX_V = 110; const int INT_INF = 1 << 30; typedef pair<int, int> P; typedef tuple<int, int, int> T; int G[MAX_V + 1][MAX_V + 1]; int h[MAX_V * 2 + 2], dist[MAX_V * 2 + 2]; int prevv[MAX_V * 2 + 2]; bool used[MAX_V * 2 + 2]; bool flow[MAX_V + 1][MAX_V + 1] = {}; int bit_matching_cost(int l, int r) { int min_f = 0; for (int i = 0; i < l; i++) { for (int j = 0; j < r; j++) { min_f = min(min_f, G[i][j]); } } for (int i = 0; i < l; i++) { for (int j = 0; j < r; j++) { G[i][j] -= min_f; } } for (int i = 0; i < l; i++) { G[i][r] = 0; flow[i][r] = true; } for (int i = 0; i < r; i++) { G[l][i] = 0; flow[l][i] = true; } G[l][r] = INT_INF; flow[l][r] = false; int res = 0; fill_n(h, l + r + 2, 0); while (true) { fill_n(dist, l + r + 2, INT_INF); dist[l + r + 1] = 0; fill_n(used, l + r + 2, false); while (true) { int v = -1; for (int i = 0; i < l + r + 2; i++) { if (!used[i] && (v == -1 || dist[i] < dist[v])) { v = i; } } if (v == -1) break; if (v < l + 1) { used[v] = true; for (int i = l + 1; i < l + r + 2; i++) { if (!flow[v][i - (l + 1)] && dist[i] > dist[v] + G[v][i - (l + 1)] + h[v] - h[i]) { dist[i] = dist[v] + G[v][i - (l + 1)] + h[v] - h[i]; prevv[i] = v; } } } else { used[v] = true; for (int i = 0; i < l + 1; i++) { if (flow[i][v - (l + 1)] && dist[i] > dist[v] - G[i][v - (l + 1)] + h[v] - h[i]) { dist[i] = dist[v] - G[i][v - (l + 1)] + h[v] - h[i]; prevv[i] = v - (l + 1); } } } } if (dist[l] >= INT_INF) { break; } for (int v = 0; v < l + r + 2; v++) { h[v] += dist[v]; } res += h[l]; res += min_f; bool flag = true; for (int v = l; !(v == r && !flag); v = (flag) ? prevv[v] : prevv[v + l + 1], flag = !flag) { if (flag) { flow[v][prevv[v]] = false; } else { flow[prevv[v + l + 1]][v] = true; } } } return res; } int cost[MAX_V][MAX_V]; const int MAX_N = 105; int N; int W[MAX_N][MAX_N], E[MAX_N][MAX_N]; bool F[MAX_N][MAX_N], F2[MAX_N][MAX_N]; int main(int argc, char *argv[]) { cin >> N; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { scanf("%d", &(W[i][j])); } } for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { scanf("%d", &(E[i][j])); } } for (int i = 0; i < N; i++) { string s; cin >> s; for (int j = 0; j < N; j++) { F[i][j] = (s[j] == 'o'); } } int f = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (F[i][j]) { G[i][j] = -E[i][j]; f += E[i][j]; } else { G[i][j] = W[i][j]; } } } f += bit_matching_cost(N, N); int c = 0; vector<T> q; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (F[i][j] != flow[i][j]) { c++; q.push_back(T(i, j, F[i][j])); } } } printf("%d\n%d\n", f, c); for (T t : q) { printf("%d %d %s\n", get<0>(t) + 1, get<1>(t) + 1, get<2>(t) ? "erase" : "write"); } return 0; }
replace
71
72
71
72
TLE
p01543
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <ctime> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <tuple> #include <typeinfo> #include <vector> using namespace std; typedef long long ll; const int MAX_V = 110; const int INT_INF = 1 << 30; typedef pair<int, int> P; typedef tuple<int, int, int> T; int G[MAX_V + 1][MAX_V + 1]; int h[MAX_V * 2 + 2], dist[MAX_V * 2 + 2]; int prevv[MAX_V * 2 + 2]; bool used[MAX_V * 2 + 2]; bool flow[MAX_V + 1][MAX_V + 1] = {}; int bit_matching_cost(int l, int r) { int min_f = 0; for (int i = 0; i < l; i++) { for (int j = 0; j < r; j++) { min_f = min(min_f, G[i][j]); } } for (int i = 0; i < l; i++) { for (int j = 0; j < r; j++) { G[i][j] -= min_f; } } for (int i = 0; i < l; i++) { G[i][r] = 0; flow[i][r] = true; } for (int i = 0; i < r; i++) { G[l][i] = 0; flow[l][i] = true; } G[l][r] = INT_INF; flow[l][r] = false; int res = 0; fill_n(h, l + r + 2, 0); while (true) { fill_n(dist, l + r + 2, INT_INF); dist[l + r + 1] = 0; fill_n(used, l + r + 2, false); while (true) { int v = -1; for (int i = 0; i < l + r + 2; i++) { if (!used[i] && (v == -1 || dist[i] < dist[v])) { v = i; } } if (v == -1) break; if (v < l + 1) { used[v] = true; for (int i = l + 1; i < l + r + 2; i++) { if (!flow[v][i - (l + 1)] && dist[i] > dist[v] + G[v][i - (l + 1)] + h[v] - h[i]) { dist[i] = dist[v] + G[v][i - (l + 1)] + h[v] - h[i]; prevv[i] = v; } } } else { used[v] = true; for (int i = 0; i < l + 1; i++) { if (flow[i][v - (l + 1)] && dist[i] > dist[v] - G[i][v - (l + 1)] + h[v] - h[i]) { dist[i] = dist[v] - G[i][v - (l + 1)] + h[v] - h[i]; prevv[i] = v; } } } } if (dist[l] >= INT_INF) { break; } for (int v = 0; v < l + r + 2; v++) { h[v] += dist[v]; } res += h[l]; res += min_f; for (int v = l; v != l + r + 1; v = prevv[v]) { if (v < l + 1) { flow[v][prevv[v] - (l + 1)] = false; } else { flow[prevv[v + l + 1]][v - (l + 1)] = true; } } } return res; } int cost[MAX_V][MAX_V]; const int MAX_N = 105; int N; int W[MAX_N][MAX_N], E[MAX_N][MAX_N]; bool F[MAX_N][MAX_N], F2[MAX_N][MAX_N]; int main(int argc, char *argv[]) { cin >> N; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { scanf("%d", &(W[i][j])); } } for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { scanf("%d", &(E[i][j])); } } for (int i = 0; i < N; i++) { string s; cin >> s; for (int j = 0; j < N; j++) { F[i][j] = (s[j] == 'o'); } } int f = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (F[i][j]) { G[i][j] = -E[i][j]; f += E[i][j]; } else { G[i][j] = W[i][j]; } } } f += bit_matching_cost(N, N); int c = 0; vector<T> q; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (F[i][j] != flow[i][j]) { c++; q.push_back(T(i, j, F[i][j])); } } } printf("%d\n%d\n", f, c); for (T t : q) { printf("%d %d %s\n", get<0>(t) + 1, get<1>(t) + 1, get<2>(t) ? "erase" : "write"); } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <ctime> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <tuple> #include <typeinfo> #include <vector> using namespace std; typedef long long ll; const int MAX_V = 110; const int INT_INF = 1 << 30; typedef pair<int, int> P; typedef tuple<int, int, int> T; int G[MAX_V + 1][MAX_V + 1]; int h[MAX_V * 2 + 2], dist[MAX_V * 2 + 2]; int prevv[MAX_V * 2 + 2]; bool used[MAX_V * 2 + 2]; bool flow[MAX_V + 1][MAX_V + 1] = {}; int bit_matching_cost(int l, int r) { int min_f = 0; for (int i = 0; i < l; i++) { for (int j = 0; j < r; j++) { min_f = min(min_f, G[i][j]); } } for (int i = 0; i < l; i++) { for (int j = 0; j < r; j++) { G[i][j] -= min_f; } } for (int i = 0; i < l; i++) { G[i][r] = 0; flow[i][r] = true; } for (int i = 0; i < r; i++) { G[l][i] = 0; flow[l][i] = true; } G[l][r] = INT_INF; flow[l][r] = false; int res = 0; fill_n(h, l + r + 2, 0); while (true) { fill_n(dist, l + r + 2, INT_INF); dist[l + r + 1] = 0; fill_n(used, l + r + 2, false); while (true) { int v = -1; for (int i = 0; i < l + r + 2; i++) { if (!used[i] && (v == -1 || dist[i] < dist[v])) { v = i; } } if (v == -1) break; if (v < l + 1) { used[v] = true; for (int i = l + 1; i < l + r + 2; i++) { if (!flow[v][i - (l + 1)] && dist[i] > dist[v] + G[v][i - (l + 1)] + h[v] - h[i]) { dist[i] = dist[v] + G[v][i - (l + 1)] + h[v] - h[i]; prevv[i] = v; } } } else { used[v] = true; for (int i = 0; i < l + 1; i++) { if (flow[i][v - (l + 1)] && dist[i] > dist[v] - G[i][v - (l + 1)] + h[v] - h[i]) { dist[i] = dist[v] - G[i][v - (l + 1)] + h[v] - h[i]; prevv[i] = v; } } } } if (dist[l] >= INT_INF) { break; } for (int v = 0; v < l + r + 2; v++) { h[v] += dist[v]; } res += h[l]; res += min_f; for (int v = l; v != l + r + 1; v = prevv[v]) { if (v < l + 1) { flow[v][prevv[v] - (l + 1)] = false; } else { flow[prevv[v]][v - (l + 1)] = true; } } } return res; } int cost[MAX_V][MAX_V]; const int MAX_N = 105; int N; int W[MAX_N][MAX_N], E[MAX_N][MAX_N]; bool F[MAX_N][MAX_N], F2[MAX_N][MAX_N]; int main(int argc, char *argv[]) { cin >> N; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { scanf("%d", &(W[i][j])); } } for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { scanf("%d", &(E[i][j])); } } for (int i = 0; i < N; i++) { string s; cin >> s; for (int j = 0; j < N; j++) { F[i][j] = (s[j] == 'o'); } } int f = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (F[i][j]) { G[i][j] = -E[i][j]; f += E[i][j]; } else { G[i][j] = W[i][j]; } } } f += bit_matching_cost(N, N); int c = 0; vector<T> q; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (F[i][j] != flow[i][j]) { c++; q.push_back(T(i, j, F[i][j])); } } } printf("%d\n%d\n", f, c); for (T t : q) { printf("%d %d %s\n", get<0>(t) + 1, get<1>(t) + 1, get<2>(t) ? "erase" : "write"); } return 0; }
replace
98
99
98
99
TLE
p01543
C++
Runtime Error
#include <bits/stdc++.h> #define INF 1000000000 #define MAX_V 100 #define MAX_E 1000 using namespace std; typedef pair<int, int> P; struct edge { int to, cap, cost, rev; edge(int to_, int cap_, int cost_, int rev_) : to(to_), cap(cap_), cost(cost_), rev(rev_) {} }; int V; vector<edge> G[MAX_V]; int h[MAX_V]; int dist[MAX_V]; int prevv[MAX_V], preve[MAX_V]; void AddEdge(int from, int to, int cap, int cost) { G[from].push_back(edge(to, cap, cost, G[to].size())); G[to].push_back(edge(from, 0, -cost, G[from].size() - 1)); } int MinimumCostFlow(int s, int t, int f) { int res = 0; fill(h, h + V, 0); while (f > 0) { priority_queue<P, vector<P>, greater<P>> que; fill(dist, dist + V, INF); dist[s] = 0; que.push(P(0, s)); while (!que.empty()) { P p = que.top(); que.pop(); int v = p.second; if (dist[v] < p.first) continue; for (size_t i = 0; i < G[v].size(); i++) { edge &e = G[v][i]; if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) { dist[e.to] = dist[v] + e.cost + h[v] - h[e.to]; prevv[e.to] = v; preve[e.to] = i; que.push(P(dist[e.to], e.to)); } } } if (dist[t] == INF) return -1; for (int v = 0; v < V; v++) h[v] += dist[v]; int d = f; for (int v = t; v != s; v = prevv[v]) { d = min(d, G[prevv[v]][preve[v]].cap); } f -= d; res += d * h[t]; for (int v = t; v != s; v = prevv[v]) { edge &e = G[prevv[v]][preve[v]]; e.cap -= d; G[v][e.rev].cap += d; } } return res; } int main() { int n; cin >> n; V = n * 2 + 2; vector<vector<int>> W(n, vector<int>(n)), E(n, vector<int>(n)); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> W[i][j]; } AddEdge(n * 2, i, 1, 0); AddEdge(n + i, n * 2 + 1, 1, 0); } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> E[i][j]; } } vector<string> s(n); int def = 0; for (int i = 0; i < n; i++) { cin >> s[i]; for (int j = 0; j < n; j++) { if (s[i][j] == '.') { AddEdge(i, n + j, 1, W[i][j]); } else { AddEdge(i, n + j, 1, -E[i][j]); def += E[i][j]; } } } cout << MinimumCostFlow(n * 2, n * 2 + 1, n) + def << endl; int res = 0; for (int i = 0; i < n; i++) { for (auto &e : G[i]) { if (e.cap == 1 && e.to - n < n && s[i][e.to - n] != '.') { res++; } else if (e.cap == 0 && e.to - n < n && s[i][e.to - n] != 'o') { res++; } } } cout << res << endl; for (int i = 0; i < n; i++) { for (auto &e : G[i]) { if (e.cap == 1 && e.to - n < n && s[i][e.to - n] != '.') { cout << i + 1 << ' ' << e.to - n + 1 << " erase" << endl; } else if (e.cap == 0 && e.to - n < n && s[i][e.to - n] != 'o') { cout << i + 1 << ' ' << e.to - n + 1 << " write" << endl; } } } return 0; }
#include <bits/stdc++.h> #define INF 1000000000 #define MAX_V 202 using namespace std; typedef pair<int, int> P; struct edge { int to, cap, cost, rev; edge(int to_, int cap_, int cost_, int rev_) : to(to_), cap(cap_), cost(cost_), rev(rev_) {} }; int V; vector<edge> G[MAX_V]; int h[MAX_V]; int dist[MAX_V]; int prevv[MAX_V], preve[MAX_V]; void AddEdge(int from, int to, int cap, int cost) { G[from].push_back(edge(to, cap, cost, G[to].size())); G[to].push_back(edge(from, 0, -cost, G[from].size() - 1)); } int MinimumCostFlow(int s, int t, int f) { int res = 0; fill(h, h + V, 0); while (f > 0) { priority_queue<P, vector<P>, greater<P>> que; fill(dist, dist + V, INF); dist[s] = 0; que.push(P(0, s)); while (!que.empty()) { P p = que.top(); que.pop(); int v = p.second; if (dist[v] < p.first) continue; for (size_t i = 0; i < G[v].size(); i++) { edge &e = G[v][i]; if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) { dist[e.to] = dist[v] + e.cost + h[v] - h[e.to]; prevv[e.to] = v; preve[e.to] = i; que.push(P(dist[e.to], e.to)); } } } if (dist[t] == INF) return -1; for (int v = 0; v < V; v++) h[v] += dist[v]; int d = f; for (int v = t; v != s; v = prevv[v]) { d = min(d, G[prevv[v]][preve[v]].cap); } f -= d; res += d * h[t]; for (int v = t; v != s; v = prevv[v]) { edge &e = G[prevv[v]][preve[v]]; e.cap -= d; G[v][e.rev].cap += d; } } return res; } int main() { int n; cin >> n; V = n * 2 + 2; vector<vector<int>> W(n, vector<int>(n)), E(n, vector<int>(n)); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> W[i][j]; } AddEdge(n * 2, i, 1, 0); AddEdge(n + i, n * 2 + 1, 1, 0); } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> E[i][j]; } } vector<string> s(n); int def = 0; for (int i = 0; i < n; i++) { cin >> s[i]; for (int j = 0; j < n; j++) { if (s[i][j] == '.') { AddEdge(i, n + j, 1, W[i][j]); } else { AddEdge(i, n + j, 1, -E[i][j]); def += E[i][j]; } } } cout << MinimumCostFlow(n * 2, n * 2 + 1, n) + def << endl; int res = 0; for (int i = 0; i < n; i++) { for (auto &e : G[i]) { if (e.cap == 1 && e.to - n < n && s[i][e.to - n] != '.') { res++; } else if (e.cap == 0 && e.to - n < n && s[i][e.to - n] != 'o') { res++; } } } cout << res << endl; for (int i = 0; i < n; i++) { for (auto &e : G[i]) { if (e.cap == 1 && e.to - n < n && s[i][e.to - n] != '.') { cout << i + 1 << ' ' << e.to - n + 1 << " erase" << endl; } else if (e.cap == 0 && e.to - n < n && s[i][e.to - n] != 'o') { cout << i + 1 << ' ' << e.to - n + 1 << " write" << endl; } } } return 0; }
replace
2
4
2
3
0
p01544
C++
Runtime Error
#include <algorithm> #include <cassert> #include <cstdio> #include <vector> using namespace std; const long long inf = 1e13; struct RMQ { int N; long long dat[8192]; void init(int N_) { N = 1; while (N < N_) N *= 2; for (int i = 1; i <= N * 2 - 1; i++) dat[i] = -inf; } void update(int i, long long x) { i += N; while (i > 0) { dat[i] = max(dat[i], x); i /= 2; } } long long get(int i) { //[0,i) if (i == 0) return -inf; int le = 0, ri = N; int k = 1; long long res = -inf; while (ri - le > 1) { int mid = (le + ri) / 2; if (mid <= i) { res = max(res, dat[k * 2]); k = k * 2 + 1; le = mid; } else { k = k * 2; ri = mid; } } if (ri == i) res = max(res, dat[k]); return res; } }; RMQ rmq; long long sum[4040]; long long dp[4040][4040]; vector<long long> vals; int N; long long a[4040]; vector<int> C; int main() { scanf("%lld", &N); for (int i = 0; i < N; i++) scanf("%lld", a + i); sum[0] = a[0]; for (int i = 1; i < N; i++) sum[i] = sum[i - 1] + a[i]; for (int i = 0; i < 4040; i++) for (int j = 0; j < 4040; j++) { dp[i][j] = inf; } for (int i = 0; i < N; i++) dp[i][1] = sum[i]; for (int k = 2; k <= N; k++) { vals.clear(); for (int i = 0; i < N; i++) { if (dp[i][k - 1] < inf) vals.push_back(dp[i][k - 1] + sum[i]); } sort(vals.begin(), vals.end()); vals.erase(unique(vals.begin(), vals.end()), vals.end()); rmq.init(vals.size()); for (int j = 0; j < N; j++) { int id = distance(vals.begin(), lower_bound(vals.begin(), vals.end(), sum[j])); long long ma = rmq.get(id); if (ma > -inf) { dp[j][k] = min(dp[j][k], sum[j] - ma); } id = distance(vals.begin(), lower_bound(vals.begin(), vals.end(), dp[j][k - 1] + sum[j])); rmq.update(id, sum[j]); } } int ans = 0; for (int i = 1; i <= N; i++) { if (dp[N - 1][i] < inf) { ans = i; } } printf("%d\n", ans); int cur = N - 1; for (int i = ans; i >= 2; i--) { bool flg = false; for (int j = cur - 1; j >= 0; j--) { if (dp[j][i - 1] < sum[cur] - sum[j] && sum[cur] - sum[j] == dp[cur][i]) { C.push_back(j + 1); cur = j; flg = true; break; } } assert(flg); } reverse(C.begin(), C.end()); for (int i = 0; i < C.size(); i++) printf("%d%c", C[i], i == C.size() - 1 ? '\n' : ' '); if (C.size() == 0) printf("\n"); // assert(C.size()>=ans-1); /* long long prev=-inf; for(int i=0;i<(int)C.size();i++){ int le=i==0?0:C[i-1]; //int ri=i==C.size()-1?N-1:C[i+1]-1; int ri=C[i]; long long sum=0; for(int j=le;j<ri;j++) sum+=a[j]; // assert(prev<sum); prev=sum; } long long xx=0; if(C.size()>0){ for(int i=C[C.size()-1];i<N;i++) xx+=a[i]; // assert(prev<xx); }*/ return 0; }
#include <algorithm> #include <cassert> #include <cstdio> #include <vector> using namespace std; const long long inf = 1e13; struct RMQ { int N; long long dat[8192]; void init(int N_) { N = 1; while (N < N_) N *= 2; for (int i = 1; i <= N * 2 - 1; i++) dat[i] = -inf; } void update(int i, long long x) { i += N; while (i > 0) { dat[i] = max(dat[i], x); i /= 2; } } long long get(int i) { //[0,i) if (i == 0) return -inf; int le = 0, ri = N; int k = 1; long long res = -inf; while (ri - le > 1) { int mid = (le + ri) / 2; if (mid <= i) { res = max(res, dat[k * 2]); k = k * 2 + 1; le = mid; } else { k = k * 2; ri = mid; } } if (ri == i) res = max(res, dat[k]); return res; } }; RMQ rmq; long long sum[4040]; long long dp[4040][4040]; vector<long long> vals; int N; long long a[4040]; vector<int> C; int main() { scanf("%lld", &N); for (int i = 0; i < N; i++) scanf("%lld", a + i); sum[0] = a[0]; for (int i = 1; i < N; i++) sum[i] = sum[i - 1] + a[i]; for (int i = 0; i < 4040; i++) for (int j = 0; j < 4040; j++) { dp[i][j] = inf; } for (int i = 0; i < N; i++) dp[i][1] = sum[i]; for (int k = 2; k <= N; k++) { vals.clear(); for (int i = 0; i < N; i++) { if (dp[i][k - 1] < inf) vals.push_back(dp[i][k - 1] + sum[i]); } sort(vals.begin(), vals.end()); vals.erase(unique(vals.begin(), vals.end()), vals.end()); rmq.init(vals.size()); for (int j = 0; j < N; j++) { int id = distance(vals.begin(), lower_bound(vals.begin(), vals.end(), sum[j])); long long ma = rmq.get(id); if (ma > -inf) { dp[j][k] = min(dp[j][k], sum[j] - ma); } if (dp[j][k - 1] < inf) { id = distance(vals.begin(), lower_bound(vals.begin(), vals.end(), dp[j][k - 1] + sum[j])); rmq.update(id, sum[j]); } } } int ans = 0; for (int i = 1; i <= N; i++) { if (dp[N - 1][i] < inf) { ans = i; } } printf("%d\n", ans); int cur = N - 1; for (int i = ans; i >= 2; i--) { bool flg = false; for (int j = cur - 1; j >= 0; j--) { if (dp[j][i - 1] < sum[cur] - sum[j] && sum[cur] - sum[j] == dp[cur][i]) { C.push_back(j + 1); cur = j; flg = true; break; } } assert(flg); } reverse(C.begin(), C.end()); for (int i = 0; i < C.size(); i++) printf("%d%c", C[i], i == C.size() - 1 ? '\n' : ' '); if (C.size() == 0) printf("\n"); // assert(C.size()>=ans-1); /* long long prev=-inf; for(int i=0;i<(int)C.size();i++){ int le=i==0?0:C[i-1]; //int ri=i==C.size()-1?N-1:C[i+1]-1; int ri=C[i]; long long sum=0; for(int j=le;j<ri;j++) sum+=a[j]; // assert(prev<sum); prev=sum; } long long xx=0; if(C.size()>0){ for(int i=C[C.size()-1];i<N;i++) xx+=a[i]; // assert(prev<xx); }*/ return 0; }
replace
90
93
90
95
-11
p01544
C++
Runtime Error
#include <algorithm> #include <array> #include <cmath> #include <complex> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <map> #include <memory> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <valarray> #include <vector> // cin.sync_with_stdio(false); // streambuf using namespace std; typedef long long ll; typedef double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<double, double> pdd; using vi = vector<int>; using vll = vector<ll>; using vpii = vector<pii>; using vpll = vector<pll>; using ti3 = tuple<int, int, int>; using vti3 = vector<ti3>; template <class T, int s> using va = vector<array<T, s>>; template <class T, class T2> using umap = unordered_map<T, T2>; template <class T> using uset = unordered_set<T>; template <class T, class S> void cmin(T &a, const S &b) { if (a > b) a = b; } template <class T, class S> void cmax(T &a, const S &b) { if (a < b) a = b; } #define ALL(a) a.begin(), a.end() #define rep(i, a) for (int i = 0; i < a; i++) #define rep1(i, a) for (int i = 1; i <= a; i++) #define rrep(i, a) for (int i = (a)-1; i >= 0; i--) #define rrep1(i, a) for (int i = a; i; i--) #define repi(i, a, b) for (int i = a; i < b; i++) // const ll mod = 1000000007; template <class T> using heap = priority_queue<T, vector<T>, greater<T>>; template <class T> using pque = priority_queue<T, vector<T>, function<T(T, T)>>; template <class T> inline void hash_combine(size_t &seed, const T &v) { hash<T> hasher; seed ^= hasher(v) + 0x9e3779b97f4a7c15 + (seed << 6) + (seed >> 2); } namespace std { template <typename S, typename T> struct hash<pair<S, T>> { inline size_t operator()(const pair<S, T> &v) const { size_t seed = 0; hash_combine(seed, v.first); hash_combine(seed, v.second); return seed; } }; // Recursive template code derived from Matthieu M. template <class Tuple, size_t Index = std::tuple_size<Tuple>::value - 1> struct HashValueImpl { static void apply(size_t &seed, Tuple const &tuple) { HashValueImpl<Tuple, Index - 1>::apply(seed, tuple); hash_combine(seed, std::get<Index>(tuple)); } }; template <class Tuple> struct HashValueImpl<Tuple, 0> { static void apply(size_t &seed, Tuple const &tuple) { hash_combine(seed, std::get<0>(tuple)); } }; template <typename... TT> struct hash<std::tuple<TT...>> { size_t operator()(std::tuple<TT...> const &tt) const { size_t seed = 0; HashValueImpl<std::tuple<TT...>>::apply(seed, tt); return seed; } }; } // namespace std template <class T> int id(vector<T> &a, T b) { return lower_bound(ALL(a), b) - a.begin(); } ll pow(ll base, ll i, ll mod) { ll a = 1; while (i) { if (i & 1) { a *= base; a %= mod; } base *= base; base %= mod; i /= 2; } return a; } ll gcd(ll a, ll b) { while (b) { ll c = a % b; a = b; b = c; } return a; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } #ifdef _MSC_VER #include <intrin.h> #endif int popcnt(unsigned long long a) { #ifndef _MSC_VER return __builtin_popcountll(a); #elif defined _WIN64 return _mm_popcnt_u64(a); #else return _mm_popcnt_u32(a >> 32) + _mm_popcnt_u32(a); a = (a & 0x5555555555555555) + (a >> 1 & 0x5555555555555555); a = (a & 0x3333333333333333) + (a >> 2 & 0x3333333333333333); a = (a & 0x0f0f0f0f0f0f0f0f) + (a >> 4 & 0x0f0f0f0f0f0f0f0f); a = (a & 0x00ff00ff00ff00ff) + (a >> 8 & 0x00ff00ff00ff00ff); a = (a & 0x0000ffff0000ffff) + (a >> 16 & 0x0000ffff0000ffff); return (a & 0xffffffff) + (a >> 32); #endif } int BitScanF(unsigned long long a) { #ifndef _MSC_VER return __builtin_ctzll(a); #elif defined _WIN64 unsigned long ret; _BitScanForward64(&ret, a); return a; #else unsigned long ret; if (!(unsigned long)a) { _BitScanForward(&ret, a); ret += 32; } else _BitScanForward(&ret, (unsigned long)a); return ret; #endif } int BitScanR(unsigned long long a) { #ifndef _MSC_VER return 63 - __builtin_clzll(a); #elif defined _WIN64 unsigned long ret; _BitScanReverse64(&ret, a); return a; #else unsigned long ret; if (a >> 32) { _BitScanReverse(&ret, a); ret += 32; } else _BitScanReverse(&ret, (unsigned long)a); return ret; #endif } template <class T> class matrix { public: vector<valarray<T>> obj; pair<int, int> s; public: matrix(pair<int, int> size, T e = 0) : matrix(size.first, size.second, e) {} matrix(int n, int m = -1, T e = 0) : obj(n, valarray<T>(e, m == -1 ? n : m)), s(n, m == -1 ? n : m) {} static matrix e(int n) { matrix a = (n); for (int i = 0; i < n; i++) a[i][i] = 1; return a; } matrix &operator+=(const matrix &p) { if (s != p.s) throw runtime_error("matrix error"); for (int i = 0; i < s.first; i++) for (int j = 0; j < s.second; j++) obj[i][j] += p.obj[i][j]; return *this; } matrix operator+(const matrix &p) { matrix res(*this); return res += p; } matrix &operator-=(const matrix &p) { if (s != p.s) throw runtime_error("matrix error"); for (int i = 0; i < s.first; i++) for (int j = 0; j < s.second; j++) obj[i][j] -= p.obj[i][j]; return *this; } matrix operator-(const matrix &p) { matrix res(*this); return res -= p; } matrix &operator*=(T p) { for (auto &a : obj) for (auto &b : a) b *= p; return *this; } matrix operator*(T p) { matrix res(*this); return res *= p; } matrix operator*(const matrix &p) { if (s.second != p.s.first) throw runtime_error("matrix error"); matrix ret(s.first, p.s.second); for (int i = 0; i < s.first; i++) for (int j = 0; j < s.second; j++) ret[i] += obj[i][j] * p.obj[j]; return ret; } matrix &operator*=(const matrix &p) { return *this = *this * p; } bool operator==(const matrix &p) { if (s != p.s) return 0; for (int i = 0; i < s.first; i++) for (int j; j < s.second; j++) if (obj[i][j] != p.obj[i][j]) return 0; return 1; } pair<int, int> size() const { return s; } matrix &mod(T m) { for (auto &a : obj) for (auto &b : a) b %= m; return *this; } valarray<T> &operator[](int t) { return obj[t]; } void gauss() { if (size().first + 1 != size().second) return; rep(i, size().first) { int p = i; repi(j, i, size().first) if (abs(obj[j][i]) > abs(obj[p][i])) p = j; swap(obj[i], obj[p]); if (abs(obj[i][i]) < 1e-8) return; // contniue; repi(j, i + 1, size().second) obj[i][j] /= obj[i][i]; rep(j, size().first) { if (i != j) { repi(k, i + 1, size().second) obj[j][k] -= obj[j][i] * obj[i][k]; } } } } }; template <class T> std::pair<matrix<T>, vector<int>> LU_decomposition(matrix<T> a) { if (a.size().first != a.size().second) throw runtime_error("matrix error"); std::vector<int> pi(a.size().first); std::iota(ALL(pi), 0); valarray<T> tmp(a.size().first); for (int i = 0; i < a.size().first; i++) { // int pivot = i; // T max = abs(a[i][i]); // for (int j = i + 1; j < a.size().first; j++) { // if (max < abs(a[j][i])) { // max = abs(a[j][i]); // pivot = j; // } // } // std::swap(i, pivot); // pi.push_back(pivot); std::slice slice(i + 1, a.size().first - i - 1, 1); for (int j = i + 1; j < a.size().first; j++) { tmp[slice] = a[i][slice]; tmp *= a[j][i] / a[i][i]; a[j][slice] -= tmp[slice]; a[j][i] = a[j][i] / a[i][i]; } } return std::make_pair(std::move(a), std::move(pi)); } template <class T> matrix<T> LU_solve(pair<matrix<T>, std::vector<int>> a, matrix<T> b) { auto pi = std::move(a.second); auto A = std::move(a.first); if (A.size().first != A.size().second || A.size().first != b.size().first) throw runtime_error("matrix error"); for (int i = 0; i < A.size().first; i++) { std::swap(b[i], b[pi[i]]); } for (int i = 0; i < A.size().first; i++) { for (int j = 0; j < i; j++) b[i] -= A[i][j] * b[j]; } for (int i = A.size().first - 1; i >= 0; i--) { for (int j = i + 1; j < A.size().first; j++) b[i] -= A[i][j] * b[j]; b[i] /= A[i][i]; } return b; } template <class T> inline matrix<T> pow(matrix<T> &base, unsigned long long exp) { auto base_(base); if (base_.size().first != base_.size().second) throw runtime_error("matrix error"); matrix<T> res = matrix<T>::e(base_.size().first); for (;;) { if (exp & 1) res *= base_; if (res.obj[0].size() > 100) { int a = 0; } if (!(exp /= 2)) break; base_ *= base_; } return res; } template <class T> inline matrix<T> modpow(matrix<T> &base, unsigned long long exp, ll m) { auto base_(base); if (base.size().first != base_.size().second) throw runtime_error("matrix error"); matrix<T> res = matrix<T>::e(base_.size().first); for (;;) { if (exp & 1) (res *= base_).mod(m); if (res.obj[0].size() > 100) { int a = 0; } if (!(exp /= 2)) break; (base_ *= base_).mod(m); } return res; } class unionfind { vector<int> par, rank, size_; // 速度ではなくメモリ効率を考えるならrankのかわりにsizeを使う public: unionfind(int n) : par(n), rank(n), size_(n, 1) { iota(ALL(par), 0); } int find(int x) { if (par[x] == x) return x; return par[x] = find(par[x]); } void unite(int x, int y) { x = find(x), y = find(y); if (x == y) return; if (rank[x] < rank[y]) swap(x, y); par[y] = x; size_[x] += size_[y]; if (rank[x] == rank[y]) rank[x]++; } bool same(int x, int y) { return find(x) == find(y); } int size(int x) { return size_[find(x)]; } }; // end of lib // template<class S=void,int ptr_num, class T = char>class trie { // umap<T, trie<S, ptr_num, T> next; // public: // S key; // trie<S, ptr_num, T>* ptr[ptr_num] = {}; // trie(S &&data) :key(data) {} // trie(const S &data) :key(data) {} // void add(T x,S data) { // if (!next.find(x))next.insert(x, data); // } // trie& operator[](T x) { // return next[x]; // } // bool find(T x) { // retun next.find(x); // } // }; // template<class T=char>class AhoCorasick { // trie<pair<bool,int>, 2, T> tree; // AhoCorasick(vector<string> p) { // int num = 0; // vector<decltype(&tree)> que(p.size(),&tree); // for (int i = 0;; i++) { // bool end = 1; // int i = 0; // for (auto a : p) { // if (i >= a.size())break; // end = ;0 // que[i] = (*que[i])[a[i]]; // i++; // } // if (end)break; // } // } // }; template <class T, class Func = function<T(T, T)>> class segtree { vector<T> obj; int offset; Func updater; T e; int bufsize(int num) { int i = 1; for (; num > i; i <<= 1) ; offset = i - 1; return (i << 1) - 1; } T query(int a, int b, int k, int l, int r) { if (r <= a || b <= l) return e; if (a <= l && r <= b) return obj[k]; else return updater(query(a, b, k * 2 + 1, l, (l + r) / 2), query(a, b, k * 2 + 2, (l + r) / 2, r)); } public: T query(int a, int b) { //[a,b) return query(a, b, 0, 0, offset + 1); } void updateall(int l = 0, int r = -1) { if (r < 0) r = offset + 1; l += offset, r += offset; if (l == 0) return; do { l = l - 1 >> 1, r = r - 1 >> 1; for (int i = l; i < r; i++) obj[i] = updater(obj[i * 2 + 1], obj[i * 2 + 2]); } while (l); } void update(int k, const T &a) { k += offset; obj[k] = a; while (k) { k = k - 1 >> 1; obj[k] = updater(obj[k * 2 + 1], obj[k * 2 + 2]); } } segtree(int n, T e, const Func &updater = Func()) : obj(bufsize(n), e), e(e), updater(updater) {} segtree(vector<T> &vec, T e, const Func &updater = Func()) : obj(bufsize(vec.size()), e), e(e), updater(updater) { copy(vec.begin(), vec.end(), obj.begin() + offset); updateall(); } T &operator[](int n) { return obj[n + offset]; } }; template <class T = int> class BIT { // 多次元BITはループをネストすればいいらしい。 vector<T> bit; public: BIT(int n) : bit(n + 1, 0) {} void add(int i, T x) { i++; while (i <= bit.size()) { bit[i - 1] += x; i += i & -i; } } T sum(int i) { T s = 0; i++; while (i) { s += bit[i - 1]; i &= i - 1; } return s; } }; template <class T> class rangeadd { BIT<T> b0, b1; int n; rangeadd(int n) : b0(n), b1(n), n(n) {} void add(int l, int r, T x) { //[l,r) b0.add(l, -x(l - 1)); b1.add(l, x); b0.add(r, x * r); b1.add(r, -x); } T sum(int i) { return b0.sum(i) + b1.sum(i) * i; } }; class Flow { int V; struct edge { int to, cap, rev, cost, add; }; vector<vector<edge>> G; vector<int> level, iter, h, dist, prevv, preve; void bfs(int s) { fill(level.begin(), level.end(), -1); queue<int> que; level[s] = 0; que.push(s); while (!que.empty()) { int v = que.front(); que.pop(); for (int i = 0; i < G[v].size(); i++) { edge &e = G[v][i]; if (e.cap > 0 && level[e.to] < 0) { level[e.to] = level[v] + 1; que.push(e.to); } } } } int dfs(int v, int t, int f) { if (v == t) return f; for (int &i = iter[v]; i < G[v].size(); i++) { edge &e = G[v][i]; if (e.cap > 0 && level[v] < level[e.to]) { int d = dfs(e.to, t, min(f, e.cap)); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } int res = 0; int flow = 0; int s, t; int max_flow(int s, int t, int fmax = numeric_limits<int>::max()) { level.resize(V); iter.resize(V); for (;;) { bfs(s); if (level[t] < 0) return flow; fill(iter.begin(), iter.end(), 0); int f; while ((f = dfs(s, t, fmax)) > 0) { flow += f; fmax -= f; } if (fmax == 0) return flow; } } typedef pair<int, int> P; int min_cost_flow(int s, int t, int f) { h.resize(V); dist.resize(V); prevv.resize(V); preve.resize(V); fill(h.begin(), h.end(), 0); while (f > 0) { priority_queue<P, vector<P>, greater<P>> que; fill(dist.begin(), dist.end(), numeric_limits<int>::max()); dist[s] = 0; que.push({0, s}); while (!que.empty()) { P p = que.top(); que.pop(); int v = p.second; if (dist[v] < p.first) continue; for (int i = 0; i < G[v].size(); i++) { edge &e = G[v][i]; if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) { dist[e.to] = dist[v] + e.cost + h[v] - h[e.to]; prevv[e.to] = v; preve[e.to] = i; que.push({dist[e.to], e.to}); } } } if (dist[t] == numeric_limits<int>::max()) { return -1; } for (int v = 0; v < V; v++) h[v] += dist[v]; int d = f; for (int v = t; v != s; v = prevv[v]) { d = min(d, G[prevv[v]][preve[v]].cap); } f -= d; res += d * h[t]; for (int v = t; v != s; v = prevv[v]) { edge &e = G[prevv[v]][preve[v]]; e.cap -= d; G[v][e.rev].cap += d; } } return res; } public: Flow(int size, int s, int t) : G(size + 1), V(size + 1), s(s), t(t) {} void add_edge(int from, int to, int cap, int cost = 0) { G[from].push_back(edge{to, cap, (int)G[to].size(), cost, 1}); G[to].push_back(edge{from, 0, (int)G[from].size() - 1, -cost, 0}); } void remove_edge_max(int from, int to, int cap, int cost = 0) { for (auto &x : G[from]) { if (!x.add || x.to != to || x.cap + G[to][x.rev].cap != cap || x.cost != cost) continue; int prev = flow; int cap = G[to][x.rev].cap; G[to][x.rev].cap = 0; x.cap = 0; cap -= max_flow(from, to, cap) - prev; max_flow(t, to, cap); max_flow(from, s, cap); flow = prev - cap; break; } } void remove_edge_cost(int from, int to, int cap, int cost = 0) { for (auto &x : G[from]) { if (!x.add || x.to != to || x.cap + G[to][x.rev].cap != cap || x.cost != cost) continue; cost += G[to][x.rev].cap * G[to][x.rev].cost; int cap = G[to][x.rev].cap; G[to][x.rev].cap = 0; x.cap = 0; min_cost_flow(from, to, cap); break; } } int max_flow() { return max_flow(s, t); } int min_cost_flow(int f) { return min_cost_flow(s, t, f); } }; int main() { int n; cin >> n; vll a(n), sum(n + 1); rep(i, n) cin >> a[i]; sum[0] = 0; rep(i, n) sum[i + 1] = sum[i] + a[i]; vector<segtree<short>> dp; vector<vll> id_list(n + 1); id_list[0].push_back(-1e18); rep(i, n) { rep(j, i + 1) id_list[i + 1].push_back(sum[i + 1] - sum[j]); sort(ALL(id_list[i + 1])); id_list[i + 1].erase(unique(ALL(id_list[i + 1])), id_list[i + 1].end()); } rep(i, n + 1) dp.emplace_back( n, -1e9, (const short &(*)(const short &, const short &)) & std::max<short>); dp[0].update(0, 0); vector<vector<short>> trace(n + 1, vector<short>(n + 1)); rep(i, n) { rep(j, i + 1) { ll now = sum[i + 1] - sum[j]; int x = dp[j].query(0, id(id_list[j], now)); int pos = id(id_list[i + 1], now); if (x >= dp[i + 1][pos]) dp[i + 1].update(pos, x + 1), trace[i + 1][pos] = j; } } int m = -1e9, pos = n, now = n; ll prev_sum = 1e18; vi ans; while (now != 0) { m = -1e9; rep(i, id(id_list[now], prev_sum)) { if (m < dp[now][i]) { m = dp[now][i]; pos = trace[now][i]; } } prev_sum = sum[now] - sum[pos]; ans.push_back(pos); now = pos; } cout << ans.size() << endl; ans.pop_back(); reverse(ALL(ans)); rep(i, ans.size()) cout << ans[i] << " \n"[i == ans.size() - 1]; } // template<class T, class map = std::unordered_map<T,unique_ptr<node>>> /*class AhoCorasick { struct node { map<char,unique_ptr<node>> next; node* fail = nullptr, *match_list = nullptr; std::vector<int> match; }root; int pattern; //template<class string> AhoCorasick(std::vector<string> &vs) :pattern(vs.size()) { root.fail = &root; for (int i = 0; i < vs.size(); i++) { node* now = &root; for (auto c : vs[i]) { if (!now->next[c])now->next[c]=make_unique<node>(); now = now->next[c].get(); } now->match.push_back(i); } std::queue<node*> que; que.push(&root); while (!que.empty()) { auto now = que.front(); que.pop(); for (auto &next : now->next) { if (!next.second)continue; if (now->fail->next.count(next.first))next.second->fail = now->fail->next[next.first].get(); else next.second->fail = now->fail->fail; //next.second->match.insert(next.second->match.end(), next.second->fail->match.begin(), next.second->fail->match.end()); if (next.second->fail->match.empty())next.second->match_list = next.second->fail->match_list; else next.second->match_list = next.second->fail; que.push(next.second.get()); } } } auto match_n(string str) { vector<int> num(pattern); } auto match_list(string str) { vector<pair<int, int>> list; auto now = &root; for (int i = 0; i < str.size(); i++) { if (now->next.count(str[i]))now = now->next[str[i]].get(); else now = now->fail; auto match = now->match_list; do { match } } } };*/
#include <algorithm> #include <array> #include <cmath> #include <complex> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <map> #include <memory> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <valarray> #include <vector> // cin.sync_with_stdio(false); // streambuf using namespace std; typedef long long ll; typedef double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<double, double> pdd; using vi = vector<int>; using vll = vector<ll>; using vpii = vector<pii>; using vpll = vector<pll>; using ti3 = tuple<int, int, int>; using vti3 = vector<ti3>; template <class T, int s> using va = vector<array<T, s>>; template <class T, class T2> using umap = unordered_map<T, T2>; template <class T> using uset = unordered_set<T>; template <class T, class S> void cmin(T &a, const S &b) { if (a > b) a = b; } template <class T, class S> void cmax(T &a, const S &b) { if (a < b) a = b; } #define ALL(a) a.begin(), a.end() #define rep(i, a) for (int i = 0; i < a; i++) #define rep1(i, a) for (int i = 1; i <= a; i++) #define rrep(i, a) for (int i = (a)-1; i >= 0; i--) #define rrep1(i, a) for (int i = a; i; i--) #define repi(i, a, b) for (int i = a; i < b; i++) // const ll mod = 1000000007; template <class T> using heap = priority_queue<T, vector<T>, greater<T>>; template <class T> using pque = priority_queue<T, vector<T>, function<T(T, T)>>; template <class T> inline void hash_combine(size_t &seed, const T &v) { hash<T> hasher; seed ^= hasher(v) + 0x9e3779b97f4a7c15 + (seed << 6) + (seed >> 2); } namespace std { template <typename S, typename T> struct hash<pair<S, T>> { inline size_t operator()(const pair<S, T> &v) const { size_t seed = 0; hash_combine(seed, v.first); hash_combine(seed, v.second); return seed; } }; // Recursive template code derived from Matthieu M. template <class Tuple, size_t Index = std::tuple_size<Tuple>::value - 1> struct HashValueImpl { static void apply(size_t &seed, Tuple const &tuple) { HashValueImpl<Tuple, Index - 1>::apply(seed, tuple); hash_combine(seed, std::get<Index>(tuple)); } }; template <class Tuple> struct HashValueImpl<Tuple, 0> { static void apply(size_t &seed, Tuple const &tuple) { hash_combine(seed, std::get<0>(tuple)); } }; template <typename... TT> struct hash<std::tuple<TT...>> { size_t operator()(std::tuple<TT...> const &tt) const { size_t seed = 0; HashValueImpl<std::tuple<TT...>>::apply(seed, tt); return seed; } }; } // namespace std template <class T> int id(vector<T> &a, T b) { return lower_bound(ALL(a), b) - a.begin(); } ll pow(ll base, ll i, ll mod) { ll a = 1; while (i) { if (i & 1) { a *= base; a %= mod; } base *= base; base %= mod; i /= 2; } return a; } ll gcd(ll a, ll b) { while (b) { ll c = a % b; a = b; b = c; } return a; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } #ifdef _MSC_VER #include <intrin.h> #endif int popcnt(unsigned long long a) { #ifndef _MSC_VER return __builtin_popcountll(a); #elif defined _WIN64 return _mm_popcnt_u64(a); #else return _mm_popcnt_u32(a >> 32) + _mm_popcnt_u32(a); a = (a & 0x5555555555555555) + (a >> 1 & 0x5555555555555555); a = (a & 0x3333333333333333) + (a >> 2 & 0x3333333333333333); a = (a & 0x0f0f0f0f0f0f0f0f) + (a >> 4 & 0x0f0f0f0f0f0f0f0f); a = (a & 0x00ff00ff00ff00ff) + (a >> 8 & 0x00ff00ff00ff00ff); a = (a & 0x0000ffff0000ffff) + (a >> 16 & 0x0000ffff0000ffff); return (a & 0xffffffff) + (a >> 32); #endif } int BitScanF(unsigned long long a) { #ifndef _MSC_VER return __builtin_ctzll(a); #elif defined _WIN64 unsigned long ret; _BitScanForward64(&ret, a); return a; #else unsigned long ret; if (!(unsigned long)a) { _BitScanForward(&ret, a); ret += 32; } else _BitScanForward(&ret, (unsigned long)a); return ret; #endif } int BitScanR(unsigned long long a) { #ifndef _MSC_VER return 63 - __builtin_clzll(a); #elif defined _WIN64 unsigned long ret; _BitScanReverse64(&ret, a); return a; #else unsigned long ret; if (a >> 32) { _BitScanReverse(&ret, a); ret += 32; } else _BitScanReverse(&ret, (unsigned long)a); return ret; #endif } template <class T> class matrix { public: vector<valarray<T>> obj; pair<int, int> s; public: matrix(pair<int, int> size, T e = 0) : matrix(size.first, size.second, e) {} matrix(int n, int m = -1, T e = 0) : obj(n, valarray<T>(e, m == -1 ? n : m)), s(n, m == -1 ? n : m) {} static matrix e(int n) { matrix a = (n); for (int i = 0; i < n; i++) a[i][i] = 1; return a; } matrix &operator+=(const matrix &p) { if (s != p.s) throw runtime_error("matrix error"); for (int i = 0; i < s.first; i++) for (int j = 0; j < s.second; j++) obj[i][j] += p.obj[i][j]; return *this; } matrix operator+(const matrix &p) { matrix res(*this); return res += p; } matrix &operator-=(const matrix &p) { if (s != p.s) throw runtime_error("matrix error"); for (int i = 0; i < s.first; i++) for (int j = 0; j < s.second; j++) obj[i][j] -= p.obj[i][j]; return *this; } matrix operator-(const matrix &p) { matrix res(*this); return res -= p; } matrix &operator*=(T p) { for (auto &a : obj) for (auto &b : a) b *= p; return *this; } matrix operator*(T p) { matrix res(*this); return res *= p; } matrix operator*(const matrix &p) { if (s.second != p.s.first) throw runtime_error("matrix error"); matrix ret(s.first, p.s.second); for (int i = 0; i < s.first; i++) for (int j = 0; j < s.second; j++) ret[i] += obj[i][j] * p.obj[j]; return ret; } matrix &operator*=(const matrix &p) { return *this = *this * p; } bool operator==(const matrix &p) { if (s != p.s) return 0; for (int i = 0; i < s.first; i++) for (int j; j < s.second; j++) if (obj[i][j] != p.obj[i][j]) return 0; return 1; } pair<int, int> size() const { return s; } matrix &mod(T m) { for (auto &a : obj) for (auto &b : a) b %= m; return *this; } valarray<T> &operator[](int t) { return obj[t]; } void gauss() { if (size().first + 1 != size().second) return; rep(i, size().first) { int p = i; repi(j, i, size().first) if (abs(obj[j][i]) > abs(obj[p][i])) p = j; swap(obj[i], obj[p]); if (abs(obj[i][i]) < 1e-8) return; // contniue; repi(j, i + 1, size().second) obj[i][j] /= obj[i][i]; rep(j, size().first) { if (i != j) { repi(k, i + 1, size().second) obj[j][k] -= obj[j][i] * obj[i][k]; } } } } }; template <class T> std::pair<matrix<T>, vector<int>> LU_decomposition(matrix<T> a) { if (a.size().first != a.size().second) throw runtime_error("matrix error"); std::vector<int> pi(a.size().first); std::iota(ALL(pi), 0); valarray<T> tmp(a.size().first); for (int i = 0; i < a.size().first; i++) { // int pivot = i; // T max = abs(a[i][i]); // for (int j = i + 1; j < a.size().first; j++) { // if (max < abs(a[j][i])) { // max = abs(a[j][i]); // pivot = j; // } // } // std::swap(i, pivot); // pi.push_back(pivot); std::slice slice(i + 1, a.size().first - i - 1, 1); for (int j = i + 1; j < a.size().first; j++) { tmp[slice] = a[i][slice]; tmp *= a[j][i] / a[i][i]; a[j][slice] -= tmp[slice]; a[j][i] = a[j][i] / a[i][i]; } } return std::make_pair(std::move(a), std::move(pi)); } template <class T> matrix<T> LU_solve(pair<matrix<T>, std::vector<int>> a, matrix<T> b) { auto pi = std::move(a.second); auto A = std::move(a.first); if (A.size().first != A.size().second || A.size().first != b.size().first) throw runtime_error("matrix error"); for (int i = 0; i < A.size().first; i++) { std::swap(b[i], b[pi[i]]); } for (int i = 0; i < A.size().first; i++) { for (int j = 0; j < i; j++) b[i] -= A[i][j] * b[j]; } for (int i = A.size().first - 1; i >= 0; i--) { for (int j = i + 1; j < A.size().first; j++) b[i] -= A[i][j] * b[j]; b[i] /= A[i][i]; } return b; } template <class T> inline matrix<T> pow(matrix<T> &base, unsigned long long exp) { auto base_(base); if (base_.size().first != base_.size().second) throw runtime_error("matrix error"); matrix<T> res = matrix<T>::e(base_.size().first); for (;;) { if (exp & 1) res *= base_; if (res.obj[0].size() > 100) { int a = 0; } if (!(exp /= 2)) break; base_ *= base_; } return res; } template <class T> inline matrix<T> modpow(matrix<T> &base, unsigned long long exp, ll m) { auto base_(base); if (base.size().first != base_.size().second) throw runtime_error("matrix error"); matrix<T> res = matrix<T>::e(base_.size().first); for (;;) { if (exp & 1) (res *= base_).mod(m); if (res.obj[0].size() > 100) { int a = 0; } if (!(exp /= 2)) break; (base_ *= base_).mod(m); } return res; } class unionfind { vector<int> par, rank, size_; // 速度ではなくメモリ効率を考えるならrankのかわりにsizeを使う public: unionfind(int n) : par(n), rank(n), size_(n, 1) { iota(ALL(par), 0); } int find(int x) { if (par[x] == x) return x; return par[x] = find(par[x]); } void unite(int x, int y) { x = find(x), y = find(y); if (x == y) return; if (rank[x] < rank[y]) swap(x, y); par[y] = x; size_[x] += size_[y]; if (rank[x] == rank[y]) rank[x]++; } bool same(int x, int y) { return find(x) == find(y); } int size(int x) { return size_[find(x)]; } }; // end of lib // template<class S=void,int ptr_num, class T = char>class trie { // umap<T, trie<S, ptr_num, T> next; // public: // S key; // trie<S, ptr_num, T>* ptr[ptr_num] = {}; // trie(S &&data) :key(data) {} // trie(const S &data) :key(data) {} // void add(T x,S data) { // if (!next.find(x))next.insert(x, data); // } // trie& operator[](T x) { // return next[x]; // } // bool find(T x) { // retun next.find(x); // } // }; // template<class T=char>class AhoCorasick { // trie<pair<bool,int>, 2, T> tree; // AhoCorasick(vector<string> p) { // int num = 0; // vector<decltype(&tree)> que(p.size(),&tree); // for (int i = 0;; i++) { // bool end = 1; // int i = 0; // for (auto a : p) { // if (i >= a.size())break; // end = ;0 // que[i] = (*que[i])[a[i]]; // i++; // } // if (end)break; // } // } // }; template <class T, class Func = function<T(T, T)>> class segtree { vector<T> obj; int offset; Func updater; T e; int bufsize(int num) { int i = 1; for (; num > i; i <<= 1) ; offset = i - 1; return (i << 1) - 1; } T query(int a, int b, int k, int l, int r) { if (r <= a || b <= l) return e; if (a <= l && r <= b) return obj[k]; else return updater(query(a, b, k * 2 + 1, l, (l + r) / 2), query(a, b, k * 2 + 2, (l + r) / 2, r)); } public: T query(int a, int b) { //[a,b) return query(a, b, 0, 0, offset + 1); } void updateall(int l = 0, int r = -1) { if (r < 0) r = offset + 1; l += offset, r += offset; if (l == 0) return; do { l = l - 1 >> 1, r = r - 1 >> 1; for (int i = l; i < r; i++) obj[i] = updater(obj[i * 2 + 1], obj[i * 2 + 2]); } while (l); } void update(int k, const T &a) { k += offset; obj[k] = a; while (k) { k = k - 1 >> 1; obj[k] = updater(obj[k * 2 + 1], obj[k * 2 + 2]); } } segtree(int n, T e, const Func &updater = Func()) : obj(bufsize(n), e), e(e), updater(updater) {} segtree(vector<T> &vec, T e, const Func &updater = Func()) : obj(bufsize(vec.size()), e), e(e), updater(updater) { copy(vec.begin(), vec.end(), obj.begin() + offset); updateall(); } T &operator[](int n) { return obj[n + offset]; } }; template <class T = int> class BIT { // 多次元BITはループをネストすればいいらしい。 vector<T> bit; public: BIT(int n) : bit(n + 1, 0) {} void add(int i, T x) { i++; while (i <= bit.size()) { bit[i - 1] += x; i += i & -i; } } T sum(int i) { T s = 0; i++; while (i) { s += bit[i - 1]; i &= i - 1; } return s; } }; template <class T> class rangeadd { BIT<T> b0, b1; int n; rangeadd(int n) : b0(n), b1(n), n(n) {} void add(int l, int r, T x) { //[l,r) b0.add(l, -x(l - 1)); b1.add(l, x); b0.add(r, x * r); b1.add(r, -x); } T sum(int i) { return b0.sum(i) + b1.sum(i) * i; } }; class Flow { int V; struct edge { int to, cap, rev, cost, add; }; vector<vector<edge>> G; vector<int> level, iter, h, dist, prevv, preve; void bfs(int s) { fill(level.begin(), level.end(), -1); queue<int> que; level[s] = 0; que.push(s); while (!que.empty()) { int v = que.front(); que.pop(); for (int i = 0; i < G[v].size(); i++) { edge &e = G[v][i]; if (e.cap > 0 && level[e.to] < 0) { level[e.to] = level[v] + 1; que.push(e.to); } } } } int dfs(int v, int t, int f) { if (v == t) return f; for (int &i = iter[v]; i < G[v].size(); i++) { edge &e = G[v][i]; if (e.cap > 0 && level[v] < level[e.to]) { int d = dfs(e.to, t, min(f, e.cap)); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } int res = 0; int flow = 0; int s, t; int max_flow(int s, int t, int fmax = numeric_limits<int>::max()) { level.resize(V); iter.resize(V); for (;;) { bfs(s); if (level[t] < 0) return flow; fill(iter.begin(), iter.end(), 0); int f; while ((f = dfs(s, t, fmax)) > 0) { flow += f; fmax -= f; } if (fmax == 0) return flow; } } typedef pair<int, int> P; int min_cost_flow(int s, int t, int f) { h.resize(V); dist.resize(V); prevv.resize(V); preve.resize(V); fill(h.begin(), h.end(), 0); while (f > 0) { priority_queue<P, vector<P>, greater<P>> que; fill(dist.begin(), dist.end(), numeric_limits<int>::max()); dist[s] = 0; que.push({0, s}); while (!que.empty()) { P p = que.top(); que.pop(); int v = p.second; if (dist[v] < p.first) continue; for (int i = 0; i < G[v].size(); i++) { edge &e = G[v][i]; if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) { dist[e.to] = dist[v] + e.cost + h[v] - h[e.to]; prevv[e.to] = v; preve[e.to] = i; que.push({dist[e.to], e.to}); } } } if (dist[t] == numeric_limits<int>::max()) { return -1; } for (int v = 0; v < V; v++) h[v] += dist[v]; int d = f; for (int v = t; v != s; v = prevv[v]) { d = min(d, G[prevv[v]][preve[v]].cap); } f -= d; res += d * h[t]; for (int v = t; v != s; v = prevv[v]) { edge &e = G[prevv[v]][preve[v]]; e.cap -= d; G[v][e.rev].cap += d; } } return res; } public: Flow(int size, int s, int t) : G(size + 1), V(size + 1), s(s), t(t) {} void add_edge(int from, int to, int cap, int cost = 0) { G[from].push_back(edge{to, cap, (int)G[to].size(), cost, 1}); G[to].push_back(edge{from, 0, (int)G[from].size() - 1, -cost, 0}); } void remove_edge_max(int from, int to, int cap, int cost = 0) { for (auto &x : G[from]) { if (!x.add || x.to != to || x.cap + G[to][x.rev].cap != cap || x.cost != cost) continue; int prev = flow; int cap = G[to][x.rev].cap; G[to][x.rev].cap = 0; x.cap = 0; cap -= max_flow(from, to, cap) - prev; max_flow(t, to, cap); max_flow(from, s, cap); flow = prev - cap; break; } } void remove_edge_cost(int from, int to, int cap, int cost = 0) { for (auto &x : G[from]) { if (!x.add || x.to != to || x.cap + G[to][x.rev].cap != cap || x.cost != cost) continue; cost += G[to][x.rev].cap * G[to][x.rev].cost; int cap = G[to][x.rev].cap; G[to][x.rev].cap = 0; x.cap = 0; min_cost_flow(from, to, cap); break; } } int max_flow() { return max_flow(s, t); } int min_cost_flow(int f) { return min_cost_flow(s, t, f); } }; int main() { int n; cin >> n; vll a(n), sum(n + 1); rep(i, n) cin >> a[i]; sum[0] = 0; rep(i, n) sum[i + 1] = sum[i] + a[i]; vector<segtree<short>> dp; vector<vll> id_list(n + 1); id_list[0].push_back(-1e18); rep(i, n) { rep(j, i + 1) id_list[i + 1].push_back(sum[i + 1] - sum[j]); sort(ALL(id_list[i + 1])); id_list[i + 1].erase(unique(ALL(id_list[i + 1])), id_list[i + 1].end()); } rep(i, n + 1) dp.emplace_back( n, -10000, (const short &(*)(const short &, const short &)) & std::max<short>); dp[0].update(0, 0); vector<vector<short>> trace(n + 1, vector<short>(n + 1)); rep(i, n) { rep(j, i + 1) { ll now = sum[i + 1] - sum[j]; int x = dp[j].query(0, id(id_list[j], now)); int pos = id(id_list[i + 1], now); if (x >= dp[i + 1][pos]) dp[i + 1].update(pos, x + 1), trace[i + 1][pos] = j; } } int m = -1e9, pos = n, now = n; ll prev_sum = 1e18; vi ans; while (now != 0) { m = -1e9; rep(i, id(id_list[now], prev_sum)) { if (m < dp[now][i]) { m = dp[now][i]; pos = trace[now][i]; } } prev_sum = sum[now] - sum[pos]; ans.push_back(pos); now = pos; } cout << ans.size() << endl; ans.pop_back(); reverse(ALL(ans)); rep(i, ans.size()) cout << ans[i] << " \n"[i == ans.size() - 1]; } // template<class T, class map = std::unordered_map<T,unique_ptr<node>>> /*class AhoCorasick { struct node { map<char,unique_ptr<node>> next; node* fail = nullptr, *match_list = nullptr; std::vector<int> match; }root; int pattern; //template<class string> AhoCorasick(std::vector<string> &vs) :pattern(vs.size()) { root.fail = &root; for (int i = 0; i < vs.size(); i++) { node* now = &root; for (auto c : vs[i]) { if (!now->next[c])now->next[c]=make_unique<node>(); now = now->next[c].get(); } now->match.push_back(i); } std::queue<node*> que; que.push(&root); while (!que.empty()) { auto now = que.front(); que.pop(); for (auto &next : now->next) { if (!next.second)continue; if (now->fail->next.count(next.first))next.second->fail = now->fail->next[next.first].get(); else next.second->fail = now->fail->fail; //next.second->match.insert(next.second->match.end(), next.second->fail->match.begin(), next.second->fail->match.end()); if (next.second->fail->match.empty())next.second->match_list = next.second->fail->match_list; else next.second->match_list = next.second->fail; que.push(next.second.get()); } } } auto match_n(string str) { vector<int> num(pattern); } auto match_list(string str) { vector<pair<int, int>> list; auto now = &root; for (int i = 0; i < str.size(); i++) { if (now->next.count(str[i]))now = now->next[str[i]].get(); else now = now->fail; auto match = now->match_list; do { match } } } };*/
replace
659
660
659
660
0
p01544
C++
Time Limit Exceeded
#include <algorithm> #include <cstdio> using namespace std; int n, c, e, ans[4009], pre[4009], dp[4009], ps[4009][4009], l[8000009], r[8000009], p[8000009]; long long s[4009]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lld", &s[i]), s[i] += s[i - 1]; for (int i = 0; i < n; i++) { for (int j = i + 1; j <= n; j++) { l[c] = i, r[c] = j, p[c] = c, c++; } } sort(p, p + c, [](int i, int j) { return s[r[i]] - s[l[i]] != s[r[j]] - s[l[j]] ? s[r[i]] - s[l[i]] < s[r[j]] - s[l[j]] : i > j; }); fill(dp + 1, dp + n + 1, -(1 << 30)); for (int i = 0; i < c; i++) { if (dp[r[p[i]]] < dp[l[p[i]]] + 1) { dp[r[p[i]]] = dp[l[p[i]]] + 1; ps[r[p[i]]][l[p[i]]] = pre[l[p[i]]]; pre[r[p[i]]] = l[p[i]]; } } printf("%d\n", dp[n]); int p1 = n, p2 = pre[n]; while (p2 != 0) { ans[e++] = p2; swap(p1, p2); p2 = ps[p2][p1]; } for (int i = e - 1; i >= 0; i--) printf(i == 0 ? "%d\n" : "%d ", ans[i]); return 0; }
#include <algorithm> #include <cstdio> using namespace std; int n, c, e, ans[4009], pre[4009], dp[4009], ps[4009][4009], l[8002009], r[8002009], p[8002009]; long long s[4009]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lld", &s[i]), s[i] += s[i - 1]; for (int i = 0; i < n; i++) { for (int j = i + 1; j <= n; j++) { l[c] = i, r[c] = j, p[c] = c, c++; } } sort(p, p + c, [](int i, int j) { return s[r[i]] - s[l[i]] != s[r[j]] - s[l[j]] ? s[r[i]] - s[l[i]] < s[r[j]] - s[l[j]] : i > j; }); fill(dp + 1, dp + n + 1, -(1 << 30)); for (int i = 0; i < c; i++) { if (dp[r[p[i]]] < dp[l[p[i]]] + 1) { dp[r[p[i]]] = dp[l[p[i]]] + 1; ps[r[p[i]]][l[p[i]]] = pre[l[p[i]]]; pre[r[p[i]]] = l[p[i]]; } } printf("%d\n", dp[n]); int p1 = n, p2 = pre[n]; while (p2 != 0) { ans[e++] = p2; swap(p1, p2); p2 = ps[p2][p1]; } for (int i = e - 1; i >= 0; i--) printf(i == 0 ? "%d\n" : "%d ", ans[i]); return 0; }
replace
3
5
3
5
TLE
p01544
C++
Memory Limit Exceeded
#include <algorithm> #include <cstdio> #include <iostream> #include <queue> #include <vector> using namespace std; typedef long long ll; typedef pair<ll, int> P; #define rep(i, x) for (int i = 0; i < x; i++) #define rep1(i, x) for (int i = 1; i <= x; i++) #define pb push_back #define sor(vec) sort(vec.begin(), vec.end()) #define lb(vec, a) lower_bound(vec.begin(), vec.end(), a) #define ub(vec, a) upper_bound(vec.begin(), vec.end(), a) #define fr first #define sc second const ll INF = 1000000000000000000; struct bit { int siz; P s[8200]; void init() { siz = 1 << 13; rep1(i, siz) { s[i] = P(-INF, 0); } } void updata(int a, P x) { while (a <= siz) { if (s[a].fr < x.fr) s[a] = x; a += a & -a; } } P query(int a) { P ret = P(-INF, 0); if (a == 0) return ret; while (a > 0) { if (ret.fr < s[a].fr) ret = s[a]; a -= a & -a; } return ret; } } bit; int main() { int n; ll a[4002]; scanf("%d", &n); rep1(i, n) scanf("%lld", &a[i]); ll sum[4002]; static P dp[4002][4002]; sum[0] = 0; rep1(i, n) sum[i] = sum[i - 1] + a[i]; rep1(i, n) dp[i][1] = P(sum[i], 0); for (int j = 1; j <= n; j++) { vector<ll> Z; for (int i = 1; i <= n; i++) { Z.pb(sum[i] + dp[i][j].fr); } sor(Z); bit.init(); for (int i = 1; i <= n; i++) { dp[i][j + 1] = bit.query(lb(Z, sum[i]) - Z.begin()); if (dp[i][j + 1].fr == -INF) dp[i][j + 1].fr = INF; else dp[i][j + 1].fr = sum[i] - dp[i][j + 1].fr; bit.updata((lb(Z, sum[i] + dp[i][j].fr) - Z.begin()) + 1, P(sum[i], i)); } } for (int j = n; j >= 1; j--) { if (dp[n][j].fr != INF) { vector<int> ans; int loc = n; while (1) { loc = dp[loc][j].sc; j--; if (loc == 0) break; ans.pb(loc); } printf("%d\n", (int)ans.size() + 1); reverse(ans.begin(), ans.end()); rep(i, ans.size()) { printf("%d%c", ans[i], (i == (int)ans.size() - 1) ? '\n' : ' '); } if (ans.size() == 0) puts(""); return 0; } } }
#include <algorithm> #include <cstdio> #include <iostream> #include <queue> #include <vector> using namespace std; typedef long long ll; typedef pair<ll, int> P; #define rep(i, x) for (int i = 0; i < x; i++) #define rep1(i, x) for (int i = 1; i <= x; i++) #define pb push_back #define sor(vec) sort(vec.begin(), vec.end()) #define lb(vec, a) lower_bound(vec.begin(), vec.end(), a) #define ub(vec, a) upper_bound(vec.begin(), vec.end(), a) #define fr first #define sc second const ll INF = 1000000000000000000; struct bit { int siz; P s[8200]; void init() { siz = 1 << 13; rep1(i, siz) { s[i] = P(-INF, 0); } } void updata(int a, P x) { while (a <= siz) { if (s[a].fr < x.fr) s[a] = x; a += a & -a; } } P query(int a) { P ret = P(-INF, 0); if (a == 0) return ret; while (a > 0) { if (ret.fr < s[a].fr) ret = s[a]; a -= a & -a; } return ret; } } bit; int main() { int n; ll a[4002]; scanf("%d", &n); rep1(i, n) scanf("%lld", &a[i]); ll sum[4002]; static P dp[4002][4002]; sum[0] = 0; rep1(i, n) sum[i] = sum[i - 1] + a[i]; rep1(i, n) dp[i][1] = P(sum[i], 0); for (int j = 1; j <= n; j++) { vector<ll> Z; for (int i = 1; i <= n; i++) { Z.pb(sum[i] + dp[i][j].fr); } sor(Z); bit.init(); for (int i = j; i <= n; i++) { dp[i][j + 1] = bit.query(lb(Z, sum[i]) - Z.begin()); if (dp[i][j + 1].fr == -INF) dp[i][j + 1].fr = INF; else dp[i][j + 1].fr = sum[i] - dp[i][j + 1].fr; bit.updata((lb(Z, sum[i] + dp[i][j].fr) - Z.begin()) + 1, P(sum[i], i)); } } for (int j = n; j >= 1; j--) { if (dp[n][j].fr != INF) { vector<int> ans; int loc = n; while (1) { loc = dp[loc][j].sc; j--; if (loc == 0) break; ans.pb(loc); } printf("%d\n", (int)ans.size() + 1); reverse(ans.begin(), ans.end()); rep(i, ans.size()) { printf("%d%c", ans[i], (i == (int)ans.size() - 1) ? '\n' : ' '); } if (ans.size() == 0) puts(""); return 0; } } }
replace
66
67
66
67
MLE
p01545
C++
Runtime Error
#include <bits/stdc++.h> #define int long long #define PB push_back #define FI first #define SE second using namespace std; static const int MAX_N = 1 << 17; static const int inf = 1ll << 6; typedef pair<int, int> PII; int N; vector<PII> V; class Segment { public: int dp[MAX_N]; int n; void init(int n_) { n = 1; while (n < n_) n *= 2; for (int i = 0; i < 2 * n - 1; ++i) dp[i] = -inf; } void update(int k, int a) { k += n - 1; dp[k] = a; while (k > 0) { k = (k - 1) / 2; dp[k] = max(dp[2 * k + 1], dp[2 * k + 2]); } } int query(int a, int b, int k, int l, int r) { if (r <= a || b <= l) return -inf; if (a <= l && r <= b) return dp[k]; else { int vl = query(a, b, 2 * k + 1, l, (l + r) / 2); int vr = query(a, b, 2 * k + 2, (l + r) / 2, r); return max(vl, vr); } } }; int res; signed main() { cin >> N; Segment seg; seg.init(N); int MAX = N * (N + 1) / 2; for (int i = 0; i < N; ++i) { int a; cin >> a; V.PB(PII(a, -i)); } sort(V.begin(), V.end()); for (int i = 0; i < N; ++i) { int j = -V[i].SE; seg.update(j, V[i].FI); seg.update( j, max(seg.dp[j + seg.n - 1], seg.query(0, j, 0, 0, seg.n) + V[i].FI)); } cout << MAX - seg.dp[0] << endl; }
#include <bits/stdc++.h> #define int long long #define PB push_back #define FI first #define SE second using namespace std; static const int MAX_N = 1 << 17; static const int inf = 1ll << 6; typedef pair<int, int> PII; int N; vector<PII> V; class Segment { public: int dp[2 * MAX_N]; int n; void init(int n_) { n = 1; while (n < n_) n *= 2; for (int i = 0; i < 2 * n - 1; ++i) dp[i] = -inf; } void update(int k, int a) { k += n - 1; dp[k] = a; while (k > 0) { k = (k - 1) / 2; dp[k] = max(dp[2 * k + 1], dp[2 * k + 2]); } } int query(int a, int b, int k, int l, int r) { if (r <= a || b <= l) return -inf; if (a <= l && r <= b) return dp[k]; else { int vl = query(a, b, 2 * k + 1, l, (l + r) / 2); int vr = query(a, b, 2 * k + 2, (l + r) / 2, r); return max(vl, vr); } } }; int res; signed main() { cin >> N; Segment seg; seg.init(N); int MAX = N * (N + 1) / 2; for (int i = 0; i < N; ++i) { int a; cin >> a; V.PB(PII(a, -i)); } sort(V.begin(), V.end()); for (int i = 0; i < N; ++i) { int j = -V[i].SE; seg.update(j, V[i].FI); seg.update( j, max(seg.dp[j + seg.n - 1], seg.query(0, j, 0, 0, seg.n) + V[i].FI)); } cout << MAX - seg.dp[0] << endl; }
replace
15
16
15
16
0
p01545
C++
Runtime Error
#include <algorithm> #include <climits> #include <iostream> #include <vector> const int MAX_N = 100000; typedef long long ll; using namespace std; ll n, dat[2 * MAX_N - 1]; void init(int n_) { n = 1; while (n < n_) n *= 2; } void update(int k, ll a) { k += n - 1; dat[k] = a; while (k > 0) { k = (k - 1) / 2; dat[k] = max(dat[2 * k + 1], dat[2 * k + 2]); } } ll query(int a, int b, int k, int l, int r) { if (r <= a || b <= l) return 0; 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); } } int main(void) { ll in, m, ans = 0; cin >> m; init(m); for (int i = 0; i < m; i++) { cin >> in; update(in, in + query(1, in + 1, 0, 0, n)); // ans=max(ans,dat[in+n-1]); } cout << m * (m + 1) / 2 - query(1, m + 1, 0, 0, n) << endl; return 0; }
#include <algorithm> #include <climits> #include <iostream> #include <vector> const int MAX_N = 1000000; typedef long long ll; using namespace std; ll n, dat[2 * MAX_N - 1]; void init(int n_) { n = 1; while (n < n_) n *= 2; } void update(int k, ll a) { k += n - 1; dat[k] = a; while (k > 0) { k = (k - 1) / 2; dat[k] = max(dat[2 * k + 1], dat[2 * k + 2]); } } ll query(int a, int b, int k, int l, int r) { if (r <= a || b <= l) return 0; 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); } } int main(void) { ll in, m, ans = 0; cin >> m; init(m); for (int i = 0; i < m; i++) { cin >> in; update(in, in + query(1, in + 1, 0, 0, n)); // ans=max(ans,dat[in+n-1]); } cout << m * (m + 1) / 2 - query(1, m + 1, 0, 0, n) << endl; return 0; }
replace
5
6
5
6
0
p01545
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <complex> #include <cstdio> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; // common typedef int i32; typedef long long i64, ll; typedef long double ld; #define BR "\n" #define ALL(c) (c).begin(), (c).end() #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define FOR(i, l, r) for (int i = (int)l; i < (int)(r); ++i) #define EACH(it, o) for (auto it = (o).begin(); it != (o).end(); ++it) #define IN(l, v, r) ((l) <= (v) && (v) < (r)) // config // #define NDEBUG // #define INF 1<<30 // #define EPS 1e-8 // const ll MOD =100000007; // debug #ifdef NDEBUG #define DUMP(x) #define DUMPLN(x) #define DEBUG(x) #define DEBUGLN(x) #define LINE() #define LINELN() #define CHECK(exp, act) #define STOP(e) #else #define DUMP(x) cerr << #x << " = " << (x) #define DUMPLN(x) DUMP(x) << endl #define DEBUG(x) DUMP(x) << LINE() << " " << __FILE__ #define DEBUGLN(x) DEBUG(x) << endl #define LINE() cerr << " (L" << __LINE__ << ")" #define LINELN() LINE() << endl #define CHECK(exp, act) \ if (exp != act) { \ DUMPLN(exp); \ DEBUGLN(act); \ } #define STOP(e) \ CHECK(e, true); \ if (!(e)) \ exit(1); #endif template <class T> inline string toString(const vector<T> &x) { stringstream ss; REP(i, x.size()) { if (i != 0) ss << " "; ss << x[i]; } return ss.str(); } template <class T> inline string toString(const vector<vector<T>> &map) { stringstream ss; REP(i, map.size()) { if (i != 0) ss << BR; ss << toString(map[i]); } return ss.str(); } template <class K, class V> string toString(map<K, V> &x) { string res; stringstream ss; for (auto &p : x) ss << p.first << ":" << p.second << " "; return ss.str(); } string BITtoString(int bit) { stringstream ss; while (bit != 0) { ss << (bit % 2); bit /= 2; } string res = ss.str(); reverse(ALL(res)); return res; } template <typename T, typename V> inline T pmod(T v, V MOD) { return (v % MOD + MOD) % MOD; } #define nextInt(n) scanf("%d", &n) #define defInt(n) \ int n; \ nextInt(n) #define nextLong(n) scanf("%lld", &n) #define defLong(n) \ ll n; \ nextLong(n) #define nextDouble(n) scanf("%lf", &n) namespace SegmentTrees { ll INF = 1LL << 58; template <typename T> class RMQ { private: int Pow2Fit(int n) { int d = 1; while ((d << 1) <= n) d <<= 1; return d; } public: vector<T> dat; int n, size; RMQ(int _n) { n = Pow2Fit(_n) << 1; size = 2 * n - 1; dat = vector<T>(size, 0); } void set(int v, T a) { v += n - 1; dat[v] = a; while (v > 0) { int parent = v = (v - 1) / 2; int chl = parent * 2 + 1, chr = parent * 2 + 2; dat[parent] = max(dat[chl], dat[chr]); } } T get(int v) { // v return dat[v + n - 1]; } T query(int a, int b) { //[a,b) return query(0, a, b, 0, n); } private: T query(int v, int a, int b, int l, int r) { //[a,b) if (r <= a || b <= l) return 0; // out range if (a <= l && r <= b) return dat[v]; T vl = query(v * 2 + 1, a, b, l, (l + r) / 2), vr = query(v * 2 + 2, a, b, (l + r) / 2, r); return max(vl, vr); } }; } // namespace SegmentTrees using namespace SegmentTrees; class Main { public: void run() { // ifstream cin("in"); // ofstream cout( "out" ); while (true) { int N; nextInt(N); if (N == 0) break; RMQ<ll> rmq(N); vector<int> as(N); REP(i, N) nextInt(as[i]), as[i]; REP(i, N) { rmq.set(as[i] - 1, rmq.query(0, as[i]) + as[i]); } ll sum = 0; REP(i, N) sum += (i + 1); cout << sum - rmq.query(0, N) << endl; } } }; int main() { cout << fixed << setprecision(13); ios::sync_with_stdio(false); Main().run(); return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <complex> #include <cstdio> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; // common typedef int i32; typedef long long i64, ll; typedef long double ld; #define BR "\n" #define ALL(c) (c).begin(), (c).end() #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define FOR(i, l, r) for (int i = (int)l; i < (int)(r); ++i) #define EACH(it, o) for (auto it = (o).begin(); it != (o).end(); ++it) #define IN(l, v, r) ((l) <= (v) && (v) < (r)) // config // #define NDEBUG // #define INF 1<<30 // #define EPS 1e-8 // const ll MOD =100000007; // debug #ifdef NDEBUG #define DUMP(x) #define DUMPLN(x) #define DEBUG(x) #define DEBUGLN(x) #define LINE() #define LINELN() #define CHECK(exp, act) #define STOP(e) #else #define DUMP(x) cerr << #x << " = " << (x) #define DUMPLN(x) DUMP(x) << endl #define DEBUG(x) DUMP(x) << LINE() << " " << __FILE__ #define DEBUGLN(x) DEBUG(x) << endl #define LINE() cerr << " (L" << __LINE__ << ")" #define LINELN() LINE() << endl #define CHECK(exp, act) \ if (exp != act) { \ DUMPLN(exp); \ DEBUGLN(act); \ } #define STOP(e) \ CHECK(e, true); \ if (!(e)) \ exit(1); #endif template <class T> inline string toString(const vector<T> &x) { stringstream ss; REP(i, x.size()) { if (i != 0) ss << " "; ss << x[i]; } return ss.str(); } template <class T> inline string toString(const vector<vector<T>> &map) { stringstream ss; REP(i, map.size()) { if (i != 0) ss << BR; ss << toString(map[i]); } return ss.str(); } template <class K, class V> string toString(map<K, V> &x) { string res; stringstream ss; for (auto &p : x) ss << p.first << ":" << p.second << " "; return ss.str(); } string BITtoString(int bit) { stringstream ss; while (bit != 0) { ss << (bit % 2); bit /= 2; } string res = ss.str(); reverse(ALL(res)); return res; } template <typename T, typename V> inline T pmod(T v, V MOD) { return (v % MOD + MOD) % MOD; } #define nextInt(n) scanf("%d", &n) #define defInt(n) \ int n; \ nextInt(n) #define nextLong(n) scanf("%lld", &n) #define defLong(n) \ ll n; \ nextLong(n) #define nextDouble(n) scanf("%lf", &n) namespace SegmentTrees { ll INF = 1LL << 58; template <typename T> class RMQ { private: int Pow2Fit(int n) { int d = 1; while ((d << 1) <= n) d <<= 1; return d; } public: vector<T> dat; int n, size; RMQ(int _n) { n = Pow2Fit(_n) << 1; size = 2 * n - 1; dat = vector<T>(size, 0); } void set(int v, T a) { v += n - 1; dat[v] = a; while (v > 0) { int parent = v = (v - 1) / 2; int chl = parent * 2 + 1, chr = parent * 2 + 2; dat[parent] = max(dat[chl], dat[chr]); } } T get(int v) { // v return dat[v + n - 1]; } T query(int a, int b) { //[a,b) return query(0, a, b, 0, n); } private: T query(int v, int a, int b, int l, int r) { //[a,b) if (r <= a || b <= l) return 0; // out range if (a <= l && r <= b) return dat[v]; T vl = query(v * 2 + 1, a, b, l, (l + r) / 2), vr = query(v * 2 + 2, a, b, (l + r) / 2, r); return max(vl, vr); } }; } // namespace SegmentTrees using namespace SegmentTrees; class Main { public: void run() { // ifstream cin("in"); // ofstream cout( "out" ); int N; nextInt(N); RMQ<ll> rmq(N); vector<int> as(N); REP(i, N) nextInt(as[i]), as[i]; REP(i, N) { rmq.set(as[i] - 1, rmq.query(0, as[i]) + as[i]); } ll sum = 0; REP(i, N) sum += (i + 1); cout << sum - rmq.query(0, N) << endl; } }; int main() { cout << fixed << setprecision(13); ios::sync_with_stdio(false); Main().run(); return 0; }
replace
175
188
175
184
TLE
p01545
C++
Runtime Error
#include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> #define REP(i, s, n) for (int i = s; i < n; i++) #define rep(i, n) REP(i, 0, n) #define INF 1 << 27 #define all(n) n.begin(), n.end() #define insert(a, b, c, d) PP(P(a, b), P(c, d)) #define F first #define S second #define pb push_back #define MAX_N 100100 #define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); ++i) using namespace std; typedef pair<int, int> P; typedef pair<P, P> PP; typedef long long ll; template <class T> class RMQ_segment { private: int n; T dat[2 * MAX_N - 1]; public: void init(int n_) { n = 1; while (n < n_) n *= 2; for (int i = 0; i < 2 * n - 1; i++) dat[i] = 0; } void update(int k, T a) { k += n - 1; dat[k] = a; while (k > 0) { k = (k - 1) / 2; dat[k] = max(dat[k * 2 + 1], dat[k * 2 + 2]); } } T query(int a, int b, int k, int l, int r) { if (r <= a || b <= l) return 0; else if (a <= l && r <= b) return dat[k]; 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 max(vl, vr); } T _query(int a, int b) { // nの範囲でミスしないように極力こっちを利用 return query(a, b, 0, 0, n); } }; int x[1000010]; RMQ_segment<ll> rmq; int main() { int n; cin >> n; rmq.init(n + 1); rep(i, n) cin >> x[i]; ll mex = 0; ll sum = 0; for (int i = 0; i < n; i++) { sum += x[i]; ll premex = rmq._query(0, x[i]) + x[i]; rmq.update(x[i], premex); mex = max(mex, premex); } cout << sum - mex << endl; return 0; }
#include <algorithm> #include <cassert> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> #define REP(i, s, n) for (int i = s; i < n; i++) #define rep(i, n) REP(i, 0, n) #define INF 1 << 27 #define all(n) n.begin(), n.end() #define insert(a, b, c, d) PP(P(a, b), P(c, d)) #define F first #define S second #define pb push_back #define MAX_N (1 << 18) #define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); ++i) using namespace std; typedef pair<int, int> P; typedef pair<P, P> PP; typedef long long ll; template <class T> class RMQ_segment { private: int n; T dat[2 * MAX_N - 1]; public: void init(int n_) { n = 1; while (n < n_) n *= 2; for (int i = 0; i < 2 * n - 1; i++) dat[i] = 0; } void update(int k, T a) { k += n - 1; dat[k] = a; while (k > 0) { k = (k - 1) / 2; dat[k] = max(dat[k * 2 + 1], dat[k * 2 + 2]); } } T query(int a, int b, int k, int l, int r) { if (r <= a || b <= l) return 0; else if (a <= l && r <= b) return dat[k]; 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 max(vl, vr); } T _query(int a, int b) { // nの範囲でミスしないように極力こっちを利用 return query(a, b, 0, 0, n); } }; int x[1000010]; RMQ_segment<ll> rmq; int main() { int n; cin >> n; rmq.init(n + 1); rep(i, n) cin >> x[i]; ll mex = 0; ll sum = 0; for (int i = 0; i < n; i++) { sum += x[i]; ll premex = rmq._query(0, x[i]) + x[i]; rmq.update(x[i], premex); mex = max(mex, premex); } cout << sum - mex << endl; return 0; }
replace
24
25
24
25
0
p01546
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using int64 = long long; const int64 LINF = 1LL << 60; const int INF = 1 << 30; int V, E, K; int64 g[31][150][150]; int main() { fill_n(**g, 31 * 150 * 150, -LINF); cin >> V >> E >> K; for (int i = 0; i < E; i++) { g[0][i][i] = 0; } for (int i = 0; i < E; i++) { int a, b; int64 c; cin >> a >> b >> c; g[0][a][b] = max(g[0][a][b], c); } for (int i = 0; i < 30; i++) { for (int j = 0; j < V; j++) { for (int k = 0; k < V; k++) { for (int l = 0; l < V; l++) { g[i + 1][k][l] = max(g[i + 1][k][l], g[i][k][j] + g[i][j][l]); } } } } auto check = [&](int bit) { vector<int64> dp(V); for (int k = 30; k >= 0; k--) { if ((bit >> k) & 1) { vector<int64> dp2(V); for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { dp2[j] = max(dp2[j], dp[i] + g[k][i][j]); } } dp.swap(dp2); } } return *max_element(begin(dp), end(dp)) >= K; }; int ok = INF, ng = 0; while (ok - ng > 1) { int mid = (ok + ng) / 2; if (check(mid)) ok = mid; else ng = mid; } if (ok == INF) { cout << -1 << endl; } else { cout << ok << endl; if (ok <= 100) { int64 dp[101][150] = {{}}, pv[101][150]; for (int i = 0; i < ok; i++) { for (int j = 0; j < V; j++) { for (int k = 0; k < V; k++) { if (dp[i][j] + g[0][j][k] > dp[i + 1][k]) { dp[i + 1][k] = dp[i][j] + g[0][j][k]; pv[i + 1][k] = j; } } } } int high = 00; for (int i = 0; i < V; i++) { if (dp[ok][i] >= dp[ok][high]) high = i; } vector<int> route; for (int i = ok; i >= 0; i--) { route.push_back(high); high = pv[i][high]; } reverse(begin(route), end(route)); for (int i = 0; i < route.size(); i++) { if (i) cout << " "; cout << route[i]; } cout << endl; } } }
#include <bits/stdc++.h> using namespace std; using int64 = long long; const int64 LINF = 1LL << 60; const int INF = 1 << 30; int V, E, K; int64 g[31][150][150]; int main() { fill_n(**g, 31 * 150 * 150, -LINF); cin >> V >> E >> K; for (int i = 0; i < V; i++) { g[0][i][i] = 0; } for (int i = 0; i < E; i++) { int a, b; int64 c; cin >> a >> b >> c; g[0][a][b] = max(g[0][a][b], c); } for (int i = 0; i < 30; i++) { for (int j = 0; j < V; j++) { for (int k = 0; k < V; k++) { for (int l = 0; l < V; l++) { g[i + 1][k][l] = max(g[i + 1][k][l], g[i][k][j] + g[i][j][l]); } } } } auto check = [&](int bit) { vector<int64> dp(V); for (int k = 30; k >= 0; k--) { if ((bit >> k) & 1) { vector<int64> dp2(V); for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { dp2[j] = max(dp2[j], dp[i] + g[k][i][j]); } } dp.swap(dp2); } } return *max_element(begin(dp), end(dp)) >= K; }; int ok = INF, ng = 0; while (ok - ng > 1) { int mid = (ok + ng) / 2; if (check(mid)) ok = mid; else ng = mid; } if (ok == INF) { cout << -1 << endl; } else { cout << ok << endl; if (ok <= 100) { int64 dp[101][150] = {{}}, pv[101][150]; for (int i = 0; i < ok; i++) { for (int j = 0; j < V; j++) { for (int k = 0; k < V; k++) { if (dp[i][j] + g[0][j][k] > dp[i + 1][k]) { dp[i + 1][k] = dp[i][j] + g[0][j][k]; pv[i + 1][k] = j; } } } } int high = 00; for (int i = 0; i < V; i++) { if (dp[ok][i] >= dp[ok][high]) high = i; } vector<int> route; for (int i = ok; i >= 0; i--) { route.push_back(high); high = pv[i][high]; } reverse(begin(route), end(route)); for (int i = 0; i < route.size(); i++) { if (i) cout << " "; cout << route[i]; } cout << endl; } } }
replace
15
16
15
16
0
p01547
C++
Runtime Error
#include <algorithm> #include <cfloat> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <utility> #include <vector> using namespace std; #define EPS 1e-7 typedef complex<double> P; typedef const P &rP; typedef vector<P> vP; typedef vector<int> vint; struct segment { P a, b; segment() {} segment(rP a, rP b) : a(a), b(b) {} }; struct coninfo { int bnum, pnum; double arg; coninfo(int p) : pnum(p), bnum(-1), arg(0.0) {} bool operator<(const coninfo &c) const { return arg < c.arg; } }; inline bool eq(double a, double b) { return abs(a - b) < EPS; } namespace std { bool operator<(rP a, rP b) { if (!eq(real(a), real(b))) { return real(a) < real(b); } if (!eq(imag(a), imag(b))) { return imag(a) < imag(b); } return false; } } // namespace std double dot(rP va, rP vb) { return real(va) * real(vb) + imag(va) * imag(vb); } double cross(rP va, rP vb) { return real(va) * imag(vb) - imag(va) * real(vb); } int ccw(rP a, P b, P c) { b -= a; c -= a; if (cross(b, c) > 0) return +1; // counter clockwise if (cross(b, c) < 0) return -1; // clockwise if (dot(b, c) < 0) return +2; // c--a--b on line if (norm(b) < norm(c)) return -2; // a--b--c on line return 0; } P intrSS(const segment &s, const segment &t) { P dSab = s.b - s.a; P dTba = t.a - t.b; P dSTa = t.a - s.a; double D = cross(dSab, dTba); if (abs(D) > EPS) { double p = cross(dSTa, dTba) / D; double q = cross(dSab, dSTa) / D; if (-EPS < p && p < 1.0 + EPS && -EPS < q && q < 1.0 + EPS) { return s.a + p * dSab; } } return P(DBL_MAX, DBL_MAX); } bool intersectSP(const segment &s, rP p) { return eq(abs(s.a - p) + abs(s.b - p), abs(s.a - s.b)); } bool merge_if_able(segment &s, const segment &t) { if (abs(cross(s.b - s.a, t.b - t.a)) > EPS) return false; if (ccw(s.a, t.a, s.b) == +1 || ccw(s.a, t.a, s.b) == -1) return false; // not on the same line if (ccw(s.a, s.b, t.a) == -2 || ccw(t.a, t.b, s.a) == -2) return false; // separated s = segment(min(s.a, t.a), max(s.b, t.b)); return true; } void merge_segments(vector<segment> &segs) { for (int i = 0; i < segs.size(); ++i) if (segs[i].b < segs[i].a) swap(segs[i].b, segs[i].a); for (int i = 0; i < segs.size();) { int inc = 1; for (int j = i + 1; j < segs.size(); ++j) { if (merge_if_able(segs[i], segs[j])) { segs[j--] = segs.back(); segs.pop_back(); inc = 0; } } i += inc; } } void segarrange(const vector<segment> &ss, vector<P> &ps, vector<vector<coninfo>> &ret) { for (int i = 0; i < ss.size(); ++i) { // O(n^2) ps.push_back(ss[i].a); ps.push_back(ss[i].b); for (int j = i + 1; j < ss.size(); ++j) { P intr = intrSS(ss[i], ss[j]); if (real(intr) < DBL_MAX) { ps.push_back(intr); } } } sort(ps.begin(), ps.end()); ps.erase(unique(ps.begin(), ps.end()), ps.end()); vector<vector<coninfo>> g(ps.size()); for (int i = 0; i < ss.size(); ++i) { vector<pair<double, int>> list; for (int j = 0; j < ps.size(); ++j) if (intersectSP(ss[i], ps[j])) list.push_back(make_pair(norm(ss[i].a - ps[j]), j)); sort(list.begin(), list.end()); for (int j = 0; j + 1 < list.size(); ++j) { int a = list[j].second, b = list[j + 1].second; g[a].push_back(coninfo(b)); g[b].push_back(coninfo(a)); } } g.swap(ret); } double distance_ls_p(rP a, rP b, rP c) { if (dot(b - a, c - a) <= 0.0) return abs(c - a); if (dot(a - b, c - b) <= 0.0) return abs(c - b); return abs(cross(b - a, c - a)) / abs(b - a); } bool contains(const vector<P> &plg, rP pt) { int cnt = 0; double y = imag(pt); for (int i = 0; i + 1 < plg.size(); ++i) { int j = i + 1; if (distance_ls_p(plg[i], plg[j], pt) < EPS) { return true; } double dyi = imag(plg[i]) - y; double dyj = y - imag(plg[j]); double tx = (dyi * real(plg[j]) + dyj * real(plg[i])) / (dyi + dyj); if (imag(plg[i]) >= y && imag(plg[j]) < y) { if (tx < real(pt)) ++cnt; } else if (imag(plg[i]) < y && imag(plg[j]) >= y) { if (tx < real(pt)) ++cnt; } } return (cnt % 2 != 0); } P inner(const vector<P> &plg) { int n = plg.size() - 1; for (int i = 0;; ++i) { int j = (i + 1) % n; int k = (i + 2) % n; P cen = (plg[i] + plg[j] + plg[k]) / 3.0; if (contains(plg, cen)) return cen; } } vector<vector<P>> plgs; vector<P> pnts; vector<vector<P>> blocks; vector<vector<coninfo>> cons; vector<vint> WF; int out; void prepare() { vector<segment> segs; segs.reserve(35); blocks.reserve(1000); for (int i = 0; i < plgs.size(); ++i) { for (int j = 1; j < plgs[i].size(); ++j) { segs.push_back(segment(plgs[i][j - 1], plgs[i][j])); } if (i > 0) { segs.push_back(segment(plgs[i - 1][0], plgs[i][0])); } } merge_segments(segs); segarrange(segs, pnts, cons); for (int i = 0; i < cons.size(); ++i) { for (int j = 0; j < cons[i].size(); ++j) { cons[i][j].arg = arg(pnts[cons[i][j].pnum] - pnts[i]); } sort(cons[i].begin(), cons[i].end()); } for (int i = 0; i < cons.size(); ++i) { for (int j = 0; j < cons[i].size(); ++j) { if (cons[i][j].bnum != -1) continue; int p = i, idx = j; vector<P> blk; while (1) { blk.push_back(pnts[p]); int nxtp = cons[p][idx].pnum; int nxti; for (nxti = 0; cons[nxtp][nxti].pnum != p; ++nxti) ; --nxti; if (nxti < 0) nxti += cons[nxtp].size(); if (cons[nxtp][nxti].bnum != -1) { break; } cons[nxtp][nxti].bnum = blocks.size(); p = nxtp; idx = nxti; } double S = 0.0; for (int k = 1; k < blk.size(); ++k) { S += cross(blk[k - 1], blk[k]); } if (S < -EPS) { out = blocks.size(); } blocks.push_back(blk); } } vint gas(blocks.size()); for (int i = 0; i < blocks.size(); ++i) { if (i == out) continue; P cen = inner(blocks[i]); for (int j = 0; j < plgs.size(); ++j) { if (contains(plgs[j], cen)) { ++gas[i]; } } } WF.assign(blocks.size(), vint(blocks.size(), 1000000000)); for (int i = 0; i < blocks.size(); ++i) { WF[i][i] = 0; } for (int i = 0; i < cons.size(); ++i) { int k = cons[i].size() - 1; for (int j = 0; j < cons[i].size(); ++j) { int a = cons[i][k].bnum, b = cons[i][j].bnum; WF[a][b] = WF[b][a] = abs(gas[b] - gas[a]); k = j; } } for (int k = 0; k < blocks.size(); ++k) for (int i = 0; i < blocks.size(); ++i) for (int j = 0; j < i; ++j) { WF[i][j] = WF[j][i] = min(WF[i][j], WF[i][k] + WF[k][j]); } } int blocknumber(rP p) { for (int i = 0; i < blocks.size(); ++i) { if (i != out && contains(blocks[i], p)) { return i; } } return out; } int query(P pfrom, P pto) { int from = blocknumber(pfrom), to = blocknumber(pto); return WF[from][to]; } int main() { int n, m, q, x, y, x2, y2; scanf("%d", &n); plgs.resize(n); for (int i = 0; i < n; ++i) { scanf("%d", &m); plgs[i].resize(m + 1); for (int j = 0; j < m; ++j) { scanf("%d%d", &x, &y); plgs[i][j] = P(x, y); } plgs[i][m] = plgs[i][0]; } prepare(); scanf("%d", &q); for (int i = 0; i < q; ++i) { scanf("%d%d%d%d", &x, &y, &x2, &y2); printf("%d\n", query(P(x, y), P(x2, y2))); } }
#include <algorithm> #include <cfloat> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <utility> #include <vector> using namespace std; #define EPS 1e-9 typedef complex<double> P; typedef const P &rP; typedef vector<P> vP; typedef vector<int> vint; struct segment { P a, b; segment() {} segment(rP a, rP b) : a(a), b(b) {} }; struct coninfo { int bnum, pnum; double arg; coninfo(int p) : pnum(p), bnum(-1), arg(0.0) {} bool operator<(const coninfo &c) const { return arg < c.arg; } }; inline bool eq(double a, double b) { return abs(a - b) < EPS; } namespace std { bool operator<(rP a, rP b) { if (!eq(real(a), real(b))) { return real(a) < real(b); } if (!eq(imag(a), imag(b))) { return imag(a) < imag(b); } return false; } } // namespace std double dot(rP va, rP vb) { return real(va) * real(vb) + imag(va) * imag(vb); } double cross(rP va, rP vb) { return real(va) * imag(vb) - imag(va) * real(vb); } int ccw(rP a, P b, P c) { b -= a; c -= a; if (cross(b, c) > 0) return +1; // counter clockwise if (cross(b, c) < 0) return -1; // clockwise if (dot(b, c) < 0) return +2; // c--a--b on line if (norm(b) < norm(c)) return -2; // a--b--c on line return 0; } P intrSS(const segment &s, const segment &t) { P dSab = s.b - s.a; P dTba = t.a - t.b; P dSTa = t.a - s.a; double D = cross(dSab, dTba); if (abs(D) > EPS) { double p = cross(dSTa, dTba) / D; double q = cross(dSab, dSTa) / D; if (-EPS < p && p < 1.0 + EPS && -EPS < q && q < 1.0 + EPS) { return s.a + p * dSab; } } return P(DBL_MAX, DBL_MAX); } bool intersectSP(const segment &s, rP p) { return eq(abs(s.a - p) + abs(s.b - p), abs(s.a - s.b)); } bool merge_if_able(segment &s, const segment &t) { if (abs(cross(s.b - s.a, t.b - t.a)) > EPS) return false; if (ccw(s.a, t.a, s.b) == +1 || ccw(s.a, t.a, s.b) == -1) return false; // not on the same line if (ccw(s.a, s.b, t.a) == -2 || ccw(t.a, t.b, s.a) == -2) return false; // separated s = segment(min(s.a, t.a), max(s.b, t.b)); return true; } void merge_segments(vector<segment> &segs) { for (int i = 0; i < segs.size(); ++i) if (segs[i].b < segs[i].a) swap(segs[i].b, segs[i].a); for (int i = 0; i < segs.size();) { int inc = 1; for (int j = i + 1; j < segs.size(); ++j) { if (merge_if_able(segs[i], segs[j])) { segs[j--] = segs.back(); segs.pop_back(); inc = 0; } } i += inc; } } void segarrange(const vector<segment> &ss, vector<P> &ps, vector<vector<coninfo>> &ret) { for (int i = 0; i < ss.size(); ++i) { // O(n^2) ps.push_back(ss[i].a); ps.push_back(ss[i].b); for (int j = i + 1; j < ss.size(); ++j) { P intr = intrSS(ss[i], ss[j]); if (real(intr) < DBL_MAX) { ps.push_back(intr); } } } sort(ps.begin(), ps.end()); ps.erase(unique(ps.begin(), ps.end()), ps.end()); vector<vector<coninfo>> g(ps.size()); for (int i = 0; i < ss.size(); ++i) { vector<pair<double, int>> list; for (int j = 0; j < ps.size(); ++j) if (intersectSP(ss[i], ps[j])) list.push_back(make_pair(norm(ss[i].a - ps[j]), j)); sort(list.begin(), list.end()); for (int j = 0; j + 1 < list.size(); ++j) { int a = list[j].second, b = list[j + 1].second; g[a].push_back(coninfo(b)); g[b].push_back(coninfo(a)); } } g.swap(ret); } double distance_ls_p(rP a, rP b, rP c) { if (dot(b - a, c - a) <= 0.0) return abs(c - a); if (dot(a - b, c - b) <= 0.0) return abs(c - b); return abs(cross(b - a, c - a)) / abs(b - a); } bool contains(const vector<P> &plg, rP pt) { int cnt = 0; double y = imag(pt); for (int i = 0; i + 1 < plg.size(); ++i) { int j = i + 1; if (distance_ls_p(plg[i], plg[j], pt) < EPS) { return true; } double dyi = imag(plg[i]) - y; double dyj = y - imag(plg[j]); double tx = (dyi * real(plg[j]) + dyj * real(plg[i])) / (dyi + dyj); if (imag(plg[i]) >= y && imag(plg[j]) < y) { if (tx < real(pt)) ++cnt; } else if (imag(plg[i]) < y && imag(plg[j]) >= y) { if (tx < real(pt)) ++cnt; } } return (cnt % 2 != 0); } P inner(const vector<P> &plg) { int n = plg.size() - 1; for (int i = 0;; ++i) { int j = (i + 1) % n; int k = (i + 2) % n; P cen = (plg[i] + plg[j] + plg[k]) / 3.0; if (contains(plg, cen)) return cen; } } vector<vector<P>> plgs; vector<P> pnts; vector<vector<P>> blocks; vector<vector<coninfo>> cons; vector<vint> WF; int out; void prepare() { vector<segment> segs; segs.reserve(35); blocks.reserve(1000); for (int i = 0; i < plgs.size(); ++i) { for (int j = 1; j < plgs[i].size(); ++j) { segs.push_back(segment(plgs[i][j - 1], plgs[i][j])); } if (i > 0) { segs.push_back(segment(plgs[i - 1][0], plgs[i][0])); } } merge_segments(segs); segarrange(segs, pnts, cons); for (int i = 0; i < cons.size(); ++i) { for (int j = 0; j < cons[i].size(); ++j) { cons[i][j].arg = arg(pnts[cons[i][j].pnum] - pnts[i]); } sort(cons[i].begin(), cons[i].end()); } for (int i = 0; i < cons.size(); ++i) { for (int j = 0; j < cons[i].size(); ++j) { if (cons[i][j].bnum != -1) continue; int p = i, idx = j; vector<P> blk; while (1) { blk.push_back(pnts[p]); int nxtp = cons[p][idx].pnum; int nxti; for (nxti = 0; cons[nxtp][nxti].pnum != p; ++nxti) ; --nxti; if (nxti < 0) nxti += cons[nxtp].size(); if (cons[nxtp][nxti].bnum != -1) { break; } cons[nxtp][nxti].bnum = blocks.size(); p = nxtp; idx = nxti; } double S = 0.0; for (int k = 1; k < blk.size(); ++k) { S += cross(blk[k - 1], blk[k]); } if (S < -EPS) { out = blocks.size(); } blocks.push_back(blk); } } vint gas(blocks.size()); for (int i = 0; i < blocks.size(); ++i) { if (i == out) continue; P cen = inner(blocks[i]); for (int j = 0; j < plgs.size(); ++j) { if (contains(plgs[j], cen)) { ++gas[i]; } } } WF.assign(blocks.size(), vint(blocks.size(), 1000000000)); for (int i = 0; i < blocks.size(); ++i) { WF[i][i] = 0; } for (int i = 0; i < cons.size(); ++i) { int k = cons[i].size() - 1; for (int j = 0; j < cons[i].size(); ++j) { int a = cons[i][k].bnum, b = cons[i][j].bnum; WF[a][b] = WF[b][a] = abs(gas[b] - gas[a]); k = j; } } for (int k = 0; k < blocks.size(); ++k) for (int i = 0; i < blocks.size(); ++i) for (int j = 0; j < i; ++j) { WF[i][j] = WF[j][i] = min(WF[i][j], WF[i][k] + WF[k][j]); } } int blocknumber(rP p) { for (int i = 0; i < blocks.size(); ++i) { if (i != out && contains(blocks[i], p)) { return i; } } return out; } int query(P pfrom, P pto) { int from = blocknumber(pfrom), to = blocknumber(pto); return WF[from][to]; } int main() { int n, m, q, x, y, x2, y2; scanf("%d", &n); plgs.resize(n); for (int i = 0; i < n; ++i) { scanf("%d", &m); plgs[i].resize(m + 1); for (int j = 0; j < m; ++j) { scanf("%d%d", &x, &y); plgs[i][j] = P(x, y); } plgs[i][m] = plgs[i][0]; } prepare(); scanf("%d", &q); for (int i = 0; i < q; ++i) { scanf("%d%d%d%d", &x, &y, &x2, &y2); printf("%d\n", query(P(x, y), P(x2, y2))); } }
replace
10
11
10
11
0
p01547
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define MAX 1000 #define inf 1 << 29 #define linf 1e16 #define eps (1e-8) #define mod 1000000007 #define pi acos(-1) #define phi (1.0 + sqrt(5)) / 2.0 #define f first #define s second #define mp make_pair #define pb push_back #define all(a) (a).begin(), (a).end() #define pd(a) printf("%.10f\n", (double)(a)) #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define RFOR(i, a, b) for (int i = (a)-1; (b) <= i; i--) #define equals(a, b) (fabs((a) - (b)) < eps) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<int, double> pid; typedef pair<double, int> pdi; typedef vector<int> vi; typedef vector<pii> vpi; const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1}; const int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1}; class Point { public: double x, y; Point(double x = 0, double y = 0) : x(x), y(y) {} Point operator+(Point p) { return Point(x + p.x, y + p.y); } Point operator-(Point p) { return Point(x - p.x, y - p.y); } Point operator*(double k) { return Point(x * k, y * k); } Point operator/(double k) { return Point(x / k, y / k); } bool operator<(Point p) const { return x != p.x ? x < p.x : y < p.y; } bool operator==(Point p) const { return equals(x, p.x) && equals(y, p.y); } double abs() { return sqrt(norm()); } double norm() { return (x * x + y * y); } }; typedef Point Vector; typedef vector<Point> Polygon; class Segment { public: Point p1, p2; Segment(Point p1 = Point(), Point p2 = Point()) : p1(p1), p2(p2) {} }; typedef Segment Line; double norm(Vector a) { return (a.x * a.x + a.y * a.y); } double abs(Vector a) { return sqrt(norm(a)); } double dot(Vector a, Vector b) { return (a.x * b.x + a.y * b.y); } double cross(Vector a, Vector b) { return (a.x * b.y - a.y * b.x); } int ccw(Point p0, Point p1, Point p2) { Vector a = p1 - p0; Vector b = p2 - p0; if (cross(a, b) > eps) return 1; if (cross(a, b) < -eps) return -1; if (dot(a, b) < -eps) return 2; if (a.norm() < b.norm()) return -2; return 0; } bool intersect(Point p1, Point p2, Point p3, Point p4) { return (ccw(p1, p2, p3) * ccw(p1, p2, p4) <= 0 && ccw(p3, p4, p1) * ccw(p3, p4, p2) <= 0); } bool intersect(Segment s1, Segment s2) { return intersect(s1.p1, s1.p2, s2.p1, s2.p2); } bool intersectLS(Line L, Segment s) { return cross(L.p2 - L.p1, s.p1 - L.p1) * cross(L.p2 - L.p1, s.p2 - L.p1) < -eps; } Point getCrossPointLL(Line a, Line b) { double A = cross(a.p2 - a.p1, b.p2 - b.p1); double B = cross(a.p2 - a.p1, a.p2 - b.p1); if (abs(A) < eps || abs(B) < eps) return b.p1; return b.p1 + (b.p2 - b.p1) * (B / A); } double getPolygonArea(Polygon p) { double area = 0.0; int n = p.size(); for (int i = 0; i < n; i++) area += cross(p[i % n], p[(i + 1) % n]); return area / 2; } int contains(Polygon g, Point p) { int n = g.size(); bool x = false; for (int i = 0; i < n; i++) { Vector a = g[i] - p, b = g[(i + 1) % n] - p; if (abs(cross(a, b)) < eps && dot(a, b) < eps) return 1; if (a.y > b.y) swap(a, b); if (a.y < eps && eps < b.y && cross(a, b) > eps) x = !x; } if (x) return 2; return 0; } bool contains(Polygon a, Polygon b) { Point m; FOR(i, 0, b.size()) m = m + b[i]; m = m / b.size(); if (contains(a, m) == 0) return false; return true; } Polygon convex_cut(Polygon p, Line l) { Polygon ans; for (int i = 0; i < p.size(); i++) { Point A = p[i], B = p[(i + 1) % p.size()]; if (ccw(l.p1, l.p2, A) != -1) ans.push_back(A); if (intersectLS(l, Segment(A, B))) ans.push_back(getCrossPointLL(l, Segment(A, B))); } return ans; } vector<Polygon> cut(Polygon p, Line L) { vector<Polygon> res; Polygon a = convex_cut(p, L); Polygon b = convex_cut(p, Line(L.p2, L.p1)); if (2 < a.size() && eps < getPolygonArea(a)) res.pb(a); if (2 < b.size() && eps < getPolygonArea(b)) res.pb(b); return res; } typedef vector<vector<int>> Graph; Graph SegmentArrangement(vector<Segment> v, vector<Point> &ps) { for (int i = 0; i < v.size(); i++) { ps.push_back(v[i].p1); ps.push_back(v[i].p2); } sort(ps.begin(), ps.end()); // ps.erase(unique(ps.begin(),ps.end()),ps.end()); Graph g(ps.size()); for (int i = 0; i < v.size(); i++) { vector<pair<double, int>> list; for (int j = 0; j < ps.size(); j++) if (ccw(v[i].p1, v[i].p2, ps[j]) == 0) list.push_back(mp(norm(v[i].p1 - ps[j]), j)); sort(list.begin(), list.end()); for (int j = 0; j < list.size() - 1; j++) { int a = list[j].s, b = list[j + 1].s; g[a].push_back(b); } } return g; } vector<Point> getTriangle(Point a, Point b, Point c) { vector<Point> t(3); t[0] = a; t[1] = b; t[2] = c; return t; } bool ok(int a, int b, int c, Polygon p) { if (ccw(p[a], p[b], p[c]) == -1) return false; Polygon t = getTriangle(p[a], p[b], p[c]); for (int i = 0; i < p.size(); i++) { if (i == a || i == b || i == c) continue; if (contains(t, p[i]) != 0) return false; } return true; } vector<Polygon> triangulate(Polygon p) { int n = p.size(), i = 0; vector<Polygon> res; vector<int> L, R; for (int i = 0; i < n; i++) { L.push_back((i - 1 + n) % n); R.push_back((i + 1 + n) % n); } while (res.size() < n - 2) { i = R[i]; if (ok(L[i], i, R[i], p)) { res.push_back(getTriangle(p[L[i]], p[i], p[R[i]])); L[R[i]] = L[i]; R[L[i]] = R[i]; } } return res; } bool same(Polygon a, Polygon b) { if (!(equals(getPolygonArea(a), getPolygonArea(b)))) return false; if (a.size() != b.size()) return false; sort(all(a)); sort(all(b)); FOR(i, 0, a.size()) if (!(a[i] == b[i])) return false; return true; } bool same(Segment a, Segment b) { if (a.p1 == b.p1 && a.p2 == b.p2) return true; if (a.p1 == b.p2 && a.p2 == b.p1) return true; return false; } void Print(Polygon p) { cout << endl; FOR(i, 0, p.size()) cout << p[i].x << " " << p[i].y << endl; } vector<Polygon> unique(vector<Polygon> vp) { vector<Polygon> res; FOR(i, 0, vp.size()) { bool flag = true; FOR(j, i + 1, vp.size()) { if (same(vp[i], vp[j])) flag = false; } if (flag) res.pb(vp[i]); } return res; } vector<Segment> unique(vector<Segment> v) { vector<Segment> res; FOR(i, 0, v.size()) { bool flag = true; FOR(j, i + 1, v.size()) if (v[i].p1 == v[j].p1 && v[i].p2 == v[j].p2) flag = false; if (flag) res.pb(v[i]); } return res; } int n; vector<Polygon> P; vector<Polygon> ev; int deg[MAX] = {}; bool isout[MAX] = {}; vector<pii> g[MAX]; vector<Segment> vvs; vector<Point> vp; bool used[MAX] = {}; Graph e; vector<Segment> vs; vector<vector<Polygon>> t; Polygon rec(int index) { Polygon res; if (used[index]) return res; used[index] = true; // cout<<e[index].size()<<endl; if (e[index].size() == 0) return res; res = rec(e[index][0]); res.pb(vp[index]); return res; } vector<Polygon> makePolygon() { vector<Polygon> res; e = SegmentArrangement(vvs, vp); FOR(i, 0, vp.size()) { if (used[i]) continue; Polygon p = rec(i); if (getPolygonArea(p) > eps) res.pb(p); } return res; } void add_edge(int from, int to, int cost) { g[from].pb(mp(to, cost)); g[to].pb(mp(from, cost)); } bool check(Segment s) { Point m = s.p1 + (s.p2 - s.p1) / 2.0; int c = 0; FOR(i, 0, ev.size()) c += contains(ev[i], m); return c == 1; } bool check(Polygon p) { FOR(i, 0, p.size()) { Segment s(p[i], p[(i + 1) % p.size()]); if (check(s)) return true; } return false; } bool check(Polygon a, Polygon b) { FOR(i, 0, a.size()) { Segment s1(a[i], a[(i + 1) % a.size()]); FOR(j, 0, b.size()) { Segment s2(b[j], b[(j + 1) % b.size()]); if (same(s1, s2)) return true; } } return false; } void make() { FOR(i, 0, n) t.pb(triangulate(P[i])); FOR(i, 0, t.size()) { FOR(j, 0, t[i].size()) { Polygon p = t[i][j]; FOR(k, 0, p.size()) vs.pb(Segment(p[k], p[(k + 1) % p.size()])); } } FOR(i, 0, t.size()) { queue<Polygon> cand; FOR(j, 0, t[i].size()) cand.push(t[i][j]); FOR(j, 0, vs.size()) { queue<Polygon> tmp; while (cand.size()) { Polygon p = cand.front(); cand.pop(); vector<Polygon> pp = cut(p, vs[j]); FOR(l, 0, pp.size()) tmp.push(pp[l]); } while (tmp.size()) { cand.push(tmp.front()); tmp.pop(); } } while (!cand.empty()) { ev.pb(cand.front()); cand.pop(); } } ev = unique(ev); FOR(i, 0, ev.size()) { Polygon p = ev[i]; FOR(j, 0, p.size()) { Segment s(p[j], p[(j + 1) % p.size()]); if (check(s)) vvs.pb(s); } } FOR(i, 0, vvs.size()) { FOR(j, 0, vvs.size()) { if (i == j) continue; if (same(vvs[i], vvs[j])) { cout << vvs[i].p1.x << " " << vvs[i].p1.y << " "; cout << vvs[i].p2.x << " " << vvs[i].p2.y << endl; } } } // vvs = unique(vvs); vector<Polygon> res = makePolygon(); FOR(i, 0, res.size()) ev.pb(res[i]); FOR(i, 0, ev.size()) { bool flag = true; Polygon p = ev[i]; FOR(j, 0, n) if (contains(P[j], p)) deg[i]++; if (check(ev[i])) isout[i] = true; } FOR(i, 0, ev.size()) { FOR(j, 0, ev.size()) { if (check(ev[i], ev[j])) add_edge(i, j, abs(deg[i] - deg[j])); else if (isout[i] && isout[j]) add_edge(i, j, deg[i] + deg[j]); } } } int dijkstra(Point a, Point b) { vector<int> s; set<int> t; FOR(i, 0, ev.size()) { if (contains(ev[i], a)) s.pb(i); if (contains(ev[i], b)) t.insert(i); } if (s.empty() && t.empty()) return 0; if (s.empty()) { s.pb(ev.size()); FOR(i, 0, ev.size()) if (isout[i]) add_edge(i, ev.size(), deg[i]); } if (t.empty()) { t.insert(ev.size() + 1); FOR(i, 0, ev.size()) if (isout[i]) add_edge(i, ev.size() + 1, deg[i]); } int d[MAX]; fill(d, d + MAX, inf); priority_queue<pii, vector<pii>, greater<pii>> pq; FOR(i, 0, s.size()) { pq.push(mp(0, s[i])); d[s[i]] = 0; } while (pq.size()) { pii u = pq.top(); pq.pop(); if (d[u.s] < u.f) continue; if (t.find(u.s) != t.end()) return u.f; FOR(i, 0, g[u.s].size()) { int next = g[u.s][i].f; int cost = d[u.s] + g[u.s][i].s; if (cost < d[next]) { d[next] = cost; pq.push(mp(cost, next)); } } } return -1; } int main() { int m, x, y, a, b, c, d; cin >> n; FOR(i, 0, n) { cin >> m; Polygon p; FOR(j, 0, m) { cin >> x >> y; p.pb(Point(x, y)); } if (getPolygonArea(p) < eps) reverse(all(p)); P.pb(p); } make(); cin >> m; FOR(i, 0, m) { cin >> a >> b >> c >> d; cout << dijkstra(Point(a, b), Point(c, d)) << endl; } return 0; }
#include <bits/stdc++.h> #define MAX 1000 #define inf 1 << 29 #define linf 1e16 #define eps (1e-8) #define mod 1000000007 #define pi acos(-1) #define phi (1.0 + sqrt(5)) / 2.0 #define f first #define s second #define mp make_pair #define pb push_back #define all(a) (a).begin(), (a).end() #define pd(a) printf("%.10f\n", (double)(a)) #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define RFOR(i, a, b) for (int i = (a)-1; (b) <= i; i--) #define equals(a, b) (fabs((a) - (b)) < eps) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<int, double> pid; typedef pair<double, int> pdi; typedef vector<int> vi; typedef vector<pii> vpi; const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1}; const int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1}; class Point { public: double x, y; Point(double x = 0, double y = 0) : x(x), y(y) {} Point operator+(Point p) { return Point(x + p.x, y + p.y); } Point operator-(Point p) { return Point(x - p.x, y - p.y); } Point operator*(double k) { return Point(x * k, y * k); } Point operator/(double k) { return Point(x / k, y / k); } bool operator<(Point p) const { return x != p.x ? x < p.x : y < p.y; } bool operator==(Point p) const { return equals(x, p.x) && equals(y, p.y); } double abs() { return sqrt(norm()); } double norm() { return (x * x + y * y); } }; typedef Point Vector; typedef vector<Point> Polygon; class Segment { public: Point p1, p2; Segment(Point p1 = Point(), Point p2 = Point()) : p1(p1), p2(p2) {} }; typedef Segment Line; double norm(Vector a) { return (a.x * a.x + a.y * a.y); } double abs(Vector a) { return sqrt(norm(a)); } double dot(Vector a, Vector b) { return (a.x * b.x + a.y * b.y); } double cross(Vector a, Vector b) { return (a.x * b.y - a.y * b.x); } int ccw(Point p0, Point p1, Point p2) { Vector a = p1 - p0; Vector b = p2 - p0; if (cross(a, b) > eps) return 1; if (cross(a, b) < -eps) return -1; if (dot(a, b) < -eps) return 2; if (a.norm() < b.norm()) return -2; return 0; } bool intersect(Point p1, Point p2, Point p3, Point p4) { return (ccw(p1, p2, p3) * ccw(p1, p2, p4) <= 0 && ccw(p3, p4, p1) * ccw(p3, p4, p2) <= 0); } bool intersect(Segment s1, Segment s2) { return intersect(s1.p1, s1.p2, s2.p1, s2.p2); } bool intersectLS(Line L, Segment s) { return cross(L.p2 - L.p1, s.p1 - L.p1) * cross(L.p2 - L.p1, s.p2 - L.p1) < -eps; } Point getCrossPointLL(Line a, Line b) { double A = cross(a.p2 - a.p1, b.p2 - b.p1); double B = cross(a.p2 - a.p1, a.p2 - b.p1); if (abs(A) < eps || abs(B) < eps) return b.p1; return b.p1 + (b.p2 - b.p1) * (B / A); } double getPolygonArea(Polygon p) { double area = 0.0; int n = p.size(); for (int i = 0; i < n; i++) area += cross(p[i % n], p[(i + 1) % n]); return area / 2; } int contains(Polygon g, Point p) { int n = g.size(); bool x = false; for (int i = 0; i < n; i++) { Vector a = g[i] - p, b = g[(i + 1) % n] - p; if (abs(cross(a, b)) < eps && dot(a, b) < eps) return 1; if (a.y > b.y) swap(a, b); if (a.y < eps && eps < b.y && cross(a, b) > eps) x = !x; } if (x) return 2; return 0; } bool contains(Polygon a, Polygon b) { Point m; FOR(i, 0, b.size()) m = m + b[i]; m = m / b.size(); if (contains(a, m) == 0) return false; return true; } Polygon convex_cut(Polygon p, Line l) { Polygon ans; for (int i = 0; i < p.size(); i++) { Point A = p[i], B = p[(i + 1) % p.size()]; if (ccw(l.p1, l.p2, A) != -1) ans.push_back(A); if (intersectLS(l, Segment(A, B))) ans.push_back(getCrossPointLL(l, Segment(A, B))); } return ans; } vector<Polygon> cut(Polygon p, Line L) { vector<Polygon> res; Polygon a = convex_cut(p, L); Polygon b = convex_cut(p, Line(L.p2, L.p1)); if (2 < a.size() && eps < getPolygonArea(a)) res.pb(a); if (2 < b.size() && eps < getPolygonArea(b)) res.pb(b); return res; } typedef vector<vector<int>> Graph; Graph SegmentArrangement(vector<Segment> v, vector<Point> &ps) { for (int i = 0; i < v.size(); i++) { ps.push_back(v[i].p1); ps.push_back(v[i].p2); } sort(ps.begin(), ps.end()); // ps.erase(unique(ps.begin(),ps.end()),ps.end()); Graph g(ps.size()); for (int i = 0; i < v.size(); i++) { vector<pair<double, int>> list; for (int j = 0; j < ps.size(); j++) if (ccw(v[i].p1, v[i].p2, ps[j]) == 0) list.push_back(mp(norm(v[i].p1 - ps[j]), j)); sort(list.begin(), list.end()); for (int j = 0; j < list.size() - 1; j++) { int a = list[j].s, b = list[j + 1].s; g[a].push_back(b); } } return g; } vector<Point> getTriangle(Point a, Point b, Point c) { vector<Point> t(3); t[0] = a; t[1] = b; t[2] = c; return t; } bool ok(int a, int b, int c, Polygon p) { if (ccw(p[a], p[b], p[c]) < 0) return false; Polygon t = getTriangle(p[a], p[b], p[c]); for (int i = 0; i < p.size(); i++) { if (i == a || i == b || i == c) continue; if (contains(t, p[i]) != 0) return false; } return true; } vector<Polygon> triangulate(Polygon p) { int n = p.size(), i = 0; vector<Polygon> res; vector<int> L, R; for (int i = 0; i < n; i++) { L.push_back((i - 1 + n) % n); R.push_back((i + 1 + n) % n); } while (res.size() < n - 2) { i = R[i]; if (ok(L[i], i, R[i], p)) { res.push_back(getTriangle(p[L[i]], p[i], p[R[i]])); L[R[i]] = L[i]; R[L[i]] = R[i]; } } return res; } bool same(Polygon a, Polygon b) { if (!(equals(getPolygonArea(a), getPolygonArea(b)))) return false; if (a.size() != b.size()) return false; sort(all(a)); sort(all(b)); FOR(i, 0, a.size()) if (!(a[i] == b[i])) return false; return true; } bool same(Segment a, Segment b) { if (a.p1 == b.p1 && a.p2 == b.p2) return true; if (a.p1 == b.p2 && a.p2 == b.p1) return true; return false; } void Print(Polygon p) { cout << endl; FOR(i, 0, p.size()) cout << p[i].x << " " << p[i].y << endl; } vector<Polygon> unique(vector<Polygon> vp) { vector<Polygon> res; FOR(i, 0, vp.size()) { bool flag = true; FOR(j, i + 1, vp.size()) { if (same(vp[i], vp[j])) flag = false; } if (flag) res.pb(vp[i]); } return res; } vector<Segment> unique(vector<Segment> v) { vector<Segment> res; FOR(i, 0, v.size()) { bool flag = true; FOR(j, i + 1, v.size()) if (v[i].p1 == v[j].p1 && v[i].p2 == v[j].p2) flag = false; if (flag) res.pb(v[i]); } return res; } int n; vector<Polygon> P; vector<Polygon> ev; int deg[MAX] = {}; bool isout[MAX] = {}; vector<pii> g[MAX]; vector<Segment> vvs; vector<Point> vp; bool used[MAX] = {}; Graph e; vector<Segment> vs; vector<vector<Polygon>> t; Polygon rec(int index) { Polygon res; if (used[index]) return res; used[index] = true; // cout<<e[index].size()<<endl; if (e[index].size() == 0) return res; res = rec(e[index][0]); res.pb(vp[index]); return res; } vector<Polygon> makePolygon() { vector<Polygon> res; e = SegmentArrangement(vvs, vp); FOR(i, 0, vp.size()) { if (used[i]) continue; Polygon p = rec(i); if (getPolygonArea(p) > eps) res.pb(p); } return res; } void add_edge(int from, int to, int cost) { g[from].pb(mp(to, cost)); g[to].pb(mp(from, cost)); } bool check(Segment s) { Point m = s.p1 + (s.p2 - s.p1) / 2.0; int c = 0; FOR(i, 0, ev.size()) c += contains(ev[i], m); return c == 1; } bool check(Polygon p) { FOR(i, 0, p.size()) { Segment s(p[i], p[(i + 1) % p.size()]); if (check(s)) return true; } return false; } bool check(Polygon a, Polygon b) { FOR(i, 0, a.size()) { Segment s1(a[i], a[(i + 1) % a.size()]); FOR(j, 0, b.size()) { Segment s2(b[j], b[(j + 1) % b.size()]); if (same(s1, s2)) return true; } } return false; } void make() { FOR(i, 0, n) t.pb(triangulate(P[i])); FOR(i, 0, t.size()) { FOR(j, 0, t[i].size()) { Polygon p = t[i][j]; FOR(k, 0, p.size()) vs.pb(Segment(p[k], p[(k + 1) % p.size()])); } } FOR(i, 0, t.size()) { queue<Polygon> cand; FOR(j, 0, t[i].size()) cand.push(t[i][j]); FOR(j, 0, vs.size()) { queue<Polygon> tmp; while (cand.size()) { Polygon p = cand.front(); cand.pop(); vector<Polygon> pp = cut(p, vs[j]); FOR(l, 0, pp.size()) tmp.push(pp[l]); } while (tmp.size()) { cand.push(tmp.front()); tmp.pop(); } } while (!cand.empty()) { ev.pb(cand.front()); cand.pop(); } } ev = unique(ev); FOR(i, 0, ev.size()) { Polygon p = ev[i]; FOR(j, 0, p.size()) { Segment s(p[j], p[(j + 1) % p.size()]); if (check(s)) vvs.pb(s); } } FOR(i, 0, vvs.size()) { FOR(j, 0, vvs.size()) { if (i == j) continue; if (same(vvs[i], vvs[j])) { cout << vvs[i].p1.x << " " << vvs[i].p1.y << " "; cout << vvs[i].p2.x << " " << vvs[i].p2.y << endl; } } } // vvs = unique(vvs); vector<Polygon> res = makePolygon(); FOR(i, 0, res.size()) ev.pb(res[i]); FOR(i, 0, ev.size()) { bool flag = true; Polygon p = ev[i]; FOR(j, 0, n) if (contains(P[j], p)) deg[i]++; if (check(ev[i])) isout[i] = true; } FOR(i, 0, ev.size()) { FOR(j, 0, ev.size()) { if (check(ev[i], ev[j])) add_edge(i, j, abs(deg[i] - deg[j])); else if (isout[i] && isout[j]) add_edge(i, j, deg[i] + deg[j]); } } } int dijkstra(Point a, Point b) { vector<int> s; set<int> t; FOR(i, 0, ev.size()) { if (contains(ev[i], a)) s.pb(i); if (contains(ev[i], b)) t.insert(i); } if (s.empty() && t.empty()) return 0; if (s.empty()) { s.pb(ev.size()); FOR(i, 0, ev.size()) if (isout[i]) add_edge(i, ev.size(), deg[i]); } if (t.empty()) { t.insert(ev.size() + 1); FOR(i, 0, ev.size()) if (isout[i]) add_edge(i, ev.size() + 1, deg[i]); } int d[MAX]; fill(d, d + MAX, inf); priority_queue<pii, vector<pii>, greater<pii>> pq; FOR(i, 0, s.size()) { pq.push(mp(0, s[i])); d[s[i]] = 0; } while (pq.size()) { pii u = pq.top(); pq.pop(); if (d[u.s] < u.f) continue; if (t.find(u.s) != t.end()) return u.f; FOR(i, 0, g[u.s].size()) { int next = g[u.s][i].f; int cost = d[u.s] + g[u.s][i].s; if (cost < d[next]) { d[next] = cost; pq.push(mp(cost, next)); } } } return -1; } int main() { int m, x, y, a, b, c, d; cin >> n; FOR(i, 0, n) { cin >> m; Polygon p; FOR(j, 0, m) { cin >> x >> y; p.pb(Point(x, y)); } if (getPolygonArea(p) < eps) reverse(all(p)); P.pb(p); } make(); cin >> m; FOR(i, 0, m) { cin >> a >> b >> c >> d; cout << dijkstra(Point(a, b), Point(c, d)) << endl; } return 0; }
replace
184
185
184
185
TLE
p01549
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef unsigned char uc; int main() { int m; cin >> m; int l[m], r[m]; map<string, int> ms; for (int i = 0; i < m; i++) { string s; cin >> s >> l[i] >> r[i]; ms[s] = i; } int n; cin >> n; string ss[n]; for (int i = 0; i < n; i++) cin >> ss[i]; bool f = 0; time_t start = clock(); srand((unsigned)time(NULL)); while ((double)(clock() - start) / CLOCKS_PER_SEC < 1.7 && !f) { stack<uc> sc; for (int i = 0; !f && i < n; i++) { // cout<<i<<":"<<ss[i]<<endl; if (ms.find(ss[i]) != ms.end()) { int j = ms[ss[i]], k = (l[j] + (rand() % (r[j] - l[j]))); // cout<<ss[i]<<":"<<k<<endl; sc.push(k); } else if (isdigit(ss[i][0])) { uc a = 0; for (int j = 0; j < (int)ss[i].size(); j++) a = (uc)(a * 10 + ss[i][j] - '0'); sc.push(a); } else { uc a, b; b = sc.top(); sc.pop(); a = sc.top(); sc.pop(); if (ss[i] == "+") sc.push(a + b); if (ss[i] == "-") sc.push(a - b); if (ss[i] == "*") sc.push(a * b); if (ss[i] == "/") { if (b) sc.push(a / b); else { f = 1; break; } } } } } cout << (!f ? "correct" : "error") << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef unsigned char uc; int main() { int m; cin >> m; int l[m], r[m]; map<string, int> ms; for (int i = 0; i < m; i++) { string s; cin >> s >> l[i] >> r[i]; ms[s] = i; } int n; cin >> n; string ss[n]; for (int i = 0; i < n; i++) cin >> ss[i]; bool f = 0; time_t start = clock(); srand((unsigned)time(NULL)); while ((double)(clock() - start) / CLOCKS_PER_SEC < 1.7 && !f) { stack<uc> sc; for (int i = 0; !f && i < n; i++) { // cout<<i<<":"<<ss[i]<<endl; if (ms.find(ss[i]) != ms.end()) { int j = ms[ss[i]], k = l[j]; if (r[j] != l[j]) k += (rand() % (r[j] - l[j])); // cout<<ss[i]<<":"<<k<<endl; sc.push(k); } else if (isdigit(ss[i][0])) { uc a = 0; for (int j = 0; j < (int)ss[i].size(); j++) a = (uc)(a * 10 + ss[i][j] - '0'); sc.push(a); } else { uc a, b; b = sc.top(); sc.pop(); a = sc.top(); sc.pop(); if (ss[i] == "+") sc.push(a + b); if (ss[i] == "-") sc.push(a - b); if (ss[i] == "*") sc.push(a * b); if (ss[i] == "/") { if (b) sc.push(a / b); else { f = 1; break; } } } } } cout << (!f ? "correct" : "error") << endl; return 0; }
replace
26
27
26
29
0
p01549
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> P; int main() { int n, m, mod = 256; cin >> n; map<string, P> ma; for (int i = 0; i < n; i++) { string s; P p; cin >> s >> p.first >> p.second; ma[s] = p; } queue<vector<int>> que; cin >> m; for (int i = 0; i < m; i++) { string s; cin >> s; vector<int> a(mod); fill(a.begin(), a.end(), 0); if (s[0] == '+') a[0] = 2; else if (s[0] == '-') a[0] = 3; else if (s[0] == '*') a[0] = 4; else if (s[0] == '/') a[0] = 5; else if (isdigit(s[0])) { stringstream ss; ss << s; int x; ss >> x; a[x % mod] = 1; } else { for (int i = ma[s].first; i <= ma[s].second; i++) a[i] = 1; } que.push(a); } stack<vector<int>> s; while (!que.empty()) { vector<int> c = que.front(); que.pop(); if (c[0] >= 2) { vector<int> a, b, d(mod); fill(d.begin(), d.end(), 0); b = s.top(); s.pop(); a = s.top(); s.pop(); for (int i = 0; i < mod; i++) { for (int j = 0; j < mod; j++) { if (!a[i] || !b[j]) continue; if (c[0] == 2) d[(i + j) % mod] = 1; if (c[0] == 3) d[(i - j) % mod] = 1; if (c[0] == 4) d[(i * j) % mod] = 1; if (c[0] == 5) { if (j == 0) { cout << "error" << endl; return 0; } else d[(i / j) % mod] = 1; } } } s.push(d); } else s.push(c); } cout << "correct" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> P; int main() { int n, m, mod = 256; cin >> n; map<string, P> ma; for (int i = 0; i < n; i++) { string s; P p; cin >> s >> p.first >> p.second; ma[s] = p; } queue<vector<int>> que; cin >> m; for (int i = 0; i < m; i++) { string s; cin >> s; vector<int> a(mod); fill(a.begin(), a.end(), 0); if (s[0] == '+') a[0] = 2; else if (s[0] == '-') a[0] = 3; else if (s[0] == '*') a[0] = 4; else if (s[0] == '/') a[0] = 5; else if (isdigit(s[0])) { stringstream ss; ss << s; int x; ss >> x; a[x % mod] = 1; } else { for (int i = ma[s].first; i <= ma[s].second; i++) a[i] = 1; } que.push(a); } stack<vector<int>> s; while (!que.empty()) { vector<int> c = que.front(); que.pop(); if (c[0] >= 2) { vector<int> a, b, d(mod); fill(d.begin(), d.end(), 0); b = s.top(); s.pop(); a = s.top(); s.pop(); for (int i = 0; i < mod; i++) { for (int j = 0; j < mod; j++) { if (!a[i] || !b[j]) continue; if (c[0] == 2) d[(i + j) % mod] = 1; if (c[0] == 3) d[(i - j + mod) % mod] = 1; if (c[0] == 4) d[(i * j) % mod] = 1; if (c[0] == 5) { if (j == 0) { cout << "error" << endl; return 0; } else d[(i / j) % mod] = 1; } } } s.push(d); } else s.push(c); } cout << "correct" << endl; return 0; }
replace
58
59
58
59
0
p01550
C++
Runtime Error
#include <iostream> #include <string> #include <vector> using namespace std; long long n, a[209], sz[209], ret, mod = 1000000007; long long dp[209][209][209], power[5] = {1, 10, 100, 1000, 10000}; long long solve(vector<long long> E, long long num, bool iszero) { for (int i = 0; i <= E.size(); i++) { for (int j = 0; j <= i; j++) { for (int k = 0; k <= i - j; k++) dp[i][j][k] = 0; } } dp[0][0][0] = 1; for (int k = 0; k < E.size(); k++) { for (int i = 0; i <= k; i++) { for (int j = 0; j <= k - i; j++) { if (dp[k][i][j] == 0) continue; long long X = i + 1, Y = j + 1; dp[k + 1][i + 1][j] += dp[k][i][j] * X; dp[k + 1][i][j + 1] += dp[k][i][j] * Y * power[sz[E[k]]]; dp[k + 1][i][j] += dp[k][i][j]; } } for (int i = 0; i <= k + 1; i++) { for (int j = 0; j <= k + 1 - i; k++) dp[k + 1][i][j] %= mod; } } long long Z = 0; for (int j = 0; j <= E.size(); j++) { for (int k = 0; k <= E.size() - j; k++) { if (j == 0 && num == 0) continue; Z += dp[E.size()][j][k]; Z %= mod; if (iszero == true) { Z += dp[E.size()][j][k] * j; Z %= mod; Z += dp[E.size()][j][k] * (k + 1) * 10LL; Z %= mod; } } } return Z; } int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; sz[i] = to_string(a[i]).size(); } for (int i = 1; i <= n; i++) { vector<long long> X; bool OK = false; for (int j = 1; j <= n; j++) { if (i != j) { if (a[j] != 0) X.push_back(j); else OK = true; } } long long G = solve(X, a[i], OK); ret += a[i] * G; ret %= mod; } cout << ret << endl; return 0; }
#include <iostream> #include <string> #include <vector> using namespace std; long long n, a[209], sz[209], ret, mod = 1000000007; long long dp[209][209][209], power[5] = {1, 10, 100, 1000, 10000}; long long solve(vector<long long> E, long long num, bool iszero) { for (int i = 0; i <= E.size(); i++) { for (int j = 0; j <= i; j++) { for (int k = 0; k <= i - j; k++) dp[i][j][k] = 0; } } dp[0][0][0] = 1; for (int k = 0; k < E.size(); k++) { for (int i = 0; i <= k; i++) { for (int j = 0; j <= k - i; j++) { if (dp[k][i][j] == 0) continue; long long X = i + 1, Y = j + 1; dp[k + 1][i + 1][j] += dp[k][i][j] * X; dp[k + 1][i][j + 1] += dp[k][i][j] * Y * power[sz[E[k]]]; dp[k + 1][i][j] += dp[k][i][j]; } } for (int i = 0; i <= k + 1; i++) { for (int j = 0; j <= k + 1 - i; j++) dp[k + 1][i][j] %= mod; } } long long Z = 0; for (int j = 0; j <= E.size(); j++) { for (int k = 0; k <= E.size() - j; k++) { if (j == 0 && num == 0) continue; Z += dp[E.size()][j][k]; Z %= mod; if (iszero == true) { Z += dp[E.size()][j][k] * j; Z %= mod; Z += dp[E.size()][j][k] * (k + 1) * 10LL; Z %= mod; } } } return Z; } int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; sz[i] = to_string(a[i]).size(); } for (int i = 1; i <= n; i++) { vector<long long> X; bool OK = false; for (int j = 1; j <= n; j++) { if (i != j) { if (a[j] != 0) X.push_back(j); else OK = true; } } long long G = solve(X, a[i], OK); ret += a[i] * G; ret %= mod; } cout << ret << endl; return 0; }
replace
28
29
28
29
-11
p01551
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define FOR(i, k, n) for (int i = (k); i < (n); i++) #define REP(i, n) FOR(i, 0, n) #define ALL(a) a.begin(), a.end() #define MS(m, v) memset(m, v, sizeof(m)) #define D10 fixed << setprecision(10) typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef vector<string> vs; typedef pair<int, int> pii; const int MOD = 1000000007; const int INF = MOD + 1; const ld EPS = 1e-12; template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); } template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); } /*--------------------template--------------------*/ int A, T, G, C, m; int dp[51][51][51][51]; map<string, int> mp; vector<vs> v; vs tar; ll solve(int i, int a, int t, int g, int c) { ll res = 0; if (i == tar.size()) { if (a == A && t == T && g == G && c == C) return 1; else return 0; } if (dp[a][t][g][c] >= 0) return dp[a][t][g][c]; REP(j, tar[i].size()) { if (tar[i][j] == 'A') res += solve(i + 1, a + 1, t, g, c); if (tar[i][j] == 'T') res += solve(i + 1, a, t + 1, g, c); if (tar[i][j] == 'G') res += solve(i + 1, a, t, g + 1, c); if (tar[i][j] == 'C') res += solve(i + 1, a, t, g, c + 1); res %= MOD; } return dp[a][t][g][c] = res % MOD; } bool f; vs search(int i) { vs res; REP(j, v[i].size()) { if (v[i][j][0] == '[') res.push_back(v[i][j].substr(1, v[i][j].size() - 2)); else { int nx = mp[v[i][j]]; vs tmp = search(nx); if (tmp == vs{"-1"} || res.size() + tmp.size() > A + T + G + C) return vs{"-1"}; res.insert(res.end(), ALL(tmp)); } } return res; } int main() { MS(dp, -1); cin >> A >> T >> G >> C >> m; string s; int tmp = -1; v.resize(m); while (cin >> s) { if (s.back() == ':') { tmp++; s.pop_back(); mp[s] = tmp; } else { v[tmp].push_back(s); } } f = true; tar = search(0); if (!f) { puts("0"); return 0; } cout << solve(0, 0, 0, 0, 0) % MOD << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define FOR(i, k, n) for (int i = (k); i < (n); i++) #define REP(i, n) FOR(i, 0, n) #define ALL(a) a.begin(), a.end() #define MS(m, v) memset(m, v, sizeof(m)) #define D10 fixed << setprecision(10) typedef long long ll; typedef long double ld; typedef vector<int> vi; typedef vector<string> vs; typedef pair<int, int> pii; const int MOD = 1000000007; const int INF = MOD + 1; const ld EPS = 1e-12; template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); } template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); } /*--------------------template--------------------*/ int A, T, G, C, m; int dp[51][51][51][51]; map<string, int> mp; vector<vs> v; vs tar; ll solve(int i, int a, int t, int g, int c) { ll res = 0; if (a > A || t > T || g > G || c > C) return 0; if (i == tar.size()) { if (a == A && t == T && g == G && c == C) return 1; else return 0; } if (dp[a][t][g][c] >= 0) return dp[a][t][g][c]; REP(j, tar[i].size()) { if (tar[i][j] == 'A') res += solve(i + 1, a + 1, t, g, c); if (tar[i][j] == 'T') res += solve(i + 1, a, t + 1, g, c); if (tar[i][j] == 'G') res += solve(i + 1, a, t, g + 1, c); if (tar[i][j] == 'C') res += solve(i + 1, a, t, g, c + 1); res %= MOD; } return dp[a][t][g][c] = res % MOD; } bool f; vs search(int i) { vs res; REP(j, v[i].size()) { if (v[i][j][0] == '[') res.push_back(v[i][j].substr(1, v[i][j].size() - 2)); else { int nx = mp[v[i][j]]; vs tmp = search(nx); if (tmp == vs{"-1"} || res.size() + tmp.size() > A + T + G + C) return vs{"-1"}; res.insert(res.end(), ALL(tmp)); } } return res; } int main() { MS(dp, -1); cin >> A >> T >> G >> C >> m; string s; int tmp = -1; v.resize(m); while (cin >> s) { if (s.back() == ':') { tmp++; s.pop_back(); mp[s] = tmp; } else { v[tmp].push_back(s); } } f = true; tar = search(0); if (!f) { puts("0"); return 0; } cout << solve(0, 0, 0, 0, 0) % MOD << endl; return 0; }
insert
30
30
30
32
0
p01551
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const string buff = "ATGC"; const int mod = 1e9 + 7; vector<pair<int, int>> toto[50]; vector<int> order; int dp2[52][52][52]; int dp3[52][52][52]; void dfs(int idx) { for (auto &to : toto[idx]) { if (to.first) dfs(to.second); else order.push_back(to.second); } } int main() { int Na, Nt, Ng, Nc, M; string s[50]; cin >> Na >> Nt >> Ng >> Nc; int sum = Na + Nt + Ng + Nc; cin >> M; cin.ignore(); for (int i = 0; i < M; i++) getline(cin, s[i]); map<string, int> convert; for (int i = M - 1; i >= 0; i--) { convert[s[i].substr(0, s[i].find(':'))] = i; } int dp[50] = {}; for (int i = M - 1; i >= 0; i--) { string ss = s[i].substr(s[i].find(':') + 2); vector<string> splits; string curr; for (int j = 0; j <= ss.size(); j++) { if (j == ss.size() || ss[j] == ' ') splits.push_back(curr), curr = ""; else curr += ss[j]; } for (auto &t : splits) { if (t[0] == '[') { int mask = 0; for (int j = 0; j < buff.size(); j++) { if (t.find(buff[j]) != string::npos) { mask |= 1 << j; } } toto[i].emplace_back(0, mask); dp[i] = min(sum + 1, dp[i] + 1); } else { toto[i].emplace_back(1, convert[t]); dp[i] = min(sum + 1, dp[i] + dp[convert[t]]); } } } if (dp[0] != sum) { cout << 0 << endl; } else { dfs(0); dp2[0][0][0] = 1; int sz = 0; for (int bit : order) { memset(dp3, 0, sizeof(dp3)); for (int i = 0; i <= sz; i++) { for (int j = 0; j <= sz - i; j++) { for (int k = 0; k <= sz - i - j; k++) { if ((bit >> 0) & 1) (dp3[i + 1][j][k] += dp2[i][j][k]) %= mod; if ((bit >> 1) & 1) (dp3[i][j + 1][k] += dp2[i][j][k]) %= mod; if ((bit >> 2) & 1) (dp3[i][j][k + 1] += dp2[i][j][k]) %= mod; if ((bit >> 3) & 1) (dp3[i][j][k] += dp2[i][j][k]) %= mod; } } } ++sz; swap(dp2, dp3); } cout << dp2[Na][Nt][Ng] << endl; } }
#include <bits/stdc++.h> using namespace std; const string buff = "ATGC"; const int mod = 1e9 + 7; vector<pair<int, int>> toto[50]; vector<int> order; int dp2[52][52][52]; int dp3[52][52][52]; void dfs(int idx) { for (auto &to : toto[idx]) { if (to.first) dfs(to.second); else order.push_back(to.second); } } int main() { int Na, Nt, Ng, Nc, M; string s[50]; cin >> Na >> Nt >> Ng >> Nc; int sum = Na + Nt + Ng + Nc; cin >> M; cin.ignore(); for (int i = 0; i < M; i++) getline(cin, s[i]); map<string, int> convert; for (int i = M - 1; i >= 0; i--) { convert[s[i].substr(0, s[i].find(':'))] = i; } int dp[50] = {}; for (int i = M - 1; i >= 0; i--) { string ss = s[i].substr(s[i].find(':') + 2); vector<string> splits; string curr; for (int j = 0; j <= ss.size(); j++) { if (j == ss.size() || ss[j] == ' ') splits.push_back(curr), curr = ""; else curr += ss[j]; } for (auto &t : splits) { if (t[0] == '[') { int mask = 0; for (int j = 0; j < buff.size(); j++) { if (t.find(buff[j]) != string::npos) { mask |= 1 << j; } } toto[i].emplace_back(0, mask); dp[i] = min(sum + 1, dp[i] + 1); } else { toto[i].emplace_back(1, convert[t]); dp[i] = min(sum + 1, dp[i] + dp[convert[t]]); } } } if (dp[0] != sum) { cout << 0 << endl; } else { dfs(0); dp2[0][0][0] = 1; int sz = 0; for (int bit : order) { memset(dp3, 0, sizeof(dp3)); for (int i = 0; i <= min(sz, 50); i++) { for (int j = 0; j <= min(sz - i, 50); j++) { for (int k = 0; k <= min(sz - i - j, 50); k++) { if ((bit >> 0) & 1) (dp3[i + 1][j][k] += dp2[i][j][k]) %= mod; if ((bit >> 1) & 1) (dp3[i][j + 1][k] += dp2[i][j][k]) %= mod; if ((bit >> 2) & 1) (dp3[i][j][k + 1] += dp2[i][j][k]) %= mod; if ((bit >> 3) & 1) (dp3[i][j][k] += dp2[i][j][k]) %= mod; } } } ++sz; swap(dp2, dp3); } cout << dp2[Na][Nt][Ng] << endl; } }
replace
75
78
75
78
0
p01551
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <iostream> #include <map> #include <string> #include <utility> #include <vector> using namespace std; #define MOD 1000000007 int nsum; int rest(int x, int i, int k) { if (k == 3) { return nsum - (x & 077) - (x >> 6 & 077) - (x >> 12 & 077) - i; } return (x >> (k * 6)) & 077; } int main() { int na, nt, ng, nc, m; cin >> na >> nt >> ng >> nc >> m; nsum = na + nt + ng + nc; cin.ignore(); vector<string> vs(m); for (int i = 0; i < m; ++i) { getline(cin, vs[i]); } map<string, int> nametbl; char buf[32]; vector<vector<char>> seq(m); int d = 0; char chtbl['T' + 1] = {}; chtbl['A'] = 1; chtbl['T'] = 2; chtbl['G'] = 4; chtbl['C'] = 8; for (int i = m - 1; i >= 0; --i) { const char *ptr = vs[i].c_str(); sscanf(ptr, "%[^:]:%n", buf, &d); ptr += d; nametbl.insert(make_pair(buf, i)); while (*ptr != '\0') { if (sscanf(ptr, " %s%n", buf, &d) < 1) break; ptr += d; if (*buf == '[') { char t = 0; for (int j = 1; buf[j] != ']'; ++j) { t |= chtbl[buf[j]]; } seq[i].push_back(t); } else { int j = nametbl[buf]; seq[i].insert(seq[i].end(), seq[j].begin(), seq[j].end()); } if (seq[i].size() > nsum) break; } } if (seq[0].size() > nsum) { throw 1; puts("0"); return 0; } int last = 1 << 18; vector<vector<int>> dp(seq[0].size() + 1, vector<int>(last)); dp[0][na | nt << 6 | ng << 12] = 1; for (int i = 0; i < seq[0].size(); ++i) { for (int S = 0; S < last; ++S) { if (dp[i][S] == 0) continue; for (int j = 0; j < 4; ++j) { if (!(seq[0][i] & 1 << j)) continue; int c = rest(S, i, j); if (c > 0) { int t = S - ((1 << (j * 6)) & 0777777); dp[i + 1][t] = (dp[i + 1][t] + dp[i][S]) % MOD; } } } } int ans = 0; for (int S = 0; S < last; ++S) { ans = (ans + dp.back()[S]) % MOD; } printf("%d\n", ans); }
#include <algorithm> #include <cstdio> #include <iostream> #include <map> #include <string> #include <utility> #include <vector> using namespace std; #define MOD 1000000007 int nsum; int rest(int x, int i, int k) { if (k == 3) { return nsum - (x & 077) - (x >> 6 & 077) - (x >> 12 & 077) - i; } return (x >> (k * 6)) & 077; } int main() { int na, nt, ng, nc, m; cin >> na >> nt >> ng >> nc >> m; nsum = na + nt + ng + nc; cin.ignore(); vector<string> vs(m); for (int i = 0; i < m; ++i) { getline(cin, vs[i]); } map<string, int> nametbl; char buf[32]; vector<vector<char>> seq(m); int d = 0; char chtbl['T' + 1] = {}; chtbl['A'] = 1; chtbl['T'] = 2; chtbl['G'] = 4; chtbl['C'] = 8; for (int i = m - 1; i >= 0; --i) { const char *ptr = vs[i].c_str(); sscanf(ptr, "%[^:]:%n", buf, &d); ptr += d; nametbl.insert(make_pair(buf, i)); while (*ptr != '\0') { if (sscanf(ptr, " %s%n", buf, &d) < 1) break; ptr += d; if (*buf == '[') { char t = 0; for (int j = 1; buf[j] != ']'; ++j) { t |= chtbl[buf[j]]; } seq[i].push_back(t); } else { int j = nametbl[buf]; seq[i].insert(seq[i].end(), seq[j].begin(), seq[j].end()); } if (seq[i].size() > nsum) break; } } if (seq[0].size() != nsum) { puts("0"); return 0; } int last = 1 << 18; vector<vector<int>> dp(seq[0].size() + 1, vector<int>(last)); dp[0][na | nt << 6 | ng << 12] = 1; for (int i = 0; i < seq[0].size(); ++i) { for (int S = 0; S < last; ++S) { if (dp[i][S] == 0) continue; for (int j = 0; j < 4; ++j) { if (!(seq[0][i] & 1 << j)) continue; int c = rest(S, i, j); if (c > 0) { int t = S - ((1 << (j * 6)) & 0777777); dp[i + 1][t] = (dp[i + 1][t] + dp[i][S]) % MOD; } } } } int ans = 0; for (int S = 0; S < last; ++S) { ans = (ans + dp.back()[S]) % MOD; } printf("%d\n", ans); }
replace
72
74
72
73
0
p01551
C++
Runtime Error
#include <algorithm> #include <assert.h> #include <bitset> #include <complex> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i) #define rep(i, n) REP(i, 0, n) using ll = long long; const int inf = 1e9 + 7; const ll longinf = 1LL << 60; const ll mod = 1e9 + 7; string dp[55]; string s[55]; map<string, int> mp; bool ischar(char c) { if ('a' <= c && c <= 'z') return true; if ('A' <= c && c <= 'Z') return true; if (c == '[' || c == ']' || c == ':' || c == '*') return true; return false; } string rec(int idx) { if (dp[idx] != "") return dp[idx]; string ret = ""; int i = 1; while (ischar(s[idx][i])) ++i; while (1) { ++i; if (s[idx][i] == '*') break; if (s[idx][i] == '[') { char cur = 0; while (ischar(s[idx][i])) { if (s[idx][i] == 'A') cur += 8; if (s[idx][i] == 'T') cur += 4; if (s[idx][i] == 'G') cur += 2; if (s[idx][i] == 'C') cur += 1; ++i; } ret += cur; } else { string nxt = ""; while (ischar(s[idx][i])) { nxt += s[idx][i]; ++i; } ret += rec(mp[nxt]); } if (ret.size() > 300) { ret.resize(300); return dp[idx] = ret; } } return dp[idx] = ret; } ll dpp[55][55][55][55]; int main() { int a, g, c, t; cin >> a >> g >> c >> t; int n; scanf("%d ", &n); rep(i, n) { getline(cin, s[i]); } rep(i, n) { s[i] += " *"; string na = ""; int j = 0; while (s[i][j] != ':') { na += s[i][j++]; } mp[na] = i; } string res = rec(0); if (res.size() != a + g + c + t) { cout << 0 << endl; return 0; } dpp[0][0][0][0] = 1; n = res.size(); rep(i, 55) rep(j, 55) rep(k, 55) rep(l, 55) { dpp[i][j][k][l] %= mod; if (i + j + k + l >= n) break; int cur = i + j + k + l; cur = res[cur]; if (cur & 8) dpp[i + 1][j][k][l] += dpp[i][j][k][l]; if (cur & 4) dpp[i][j + 1][k][l] += dpp[i][j][k][l]; if (cur & 2) dpp[i][j][k + 1][l] += dpp[i][j][k][l]; if (cur & 1) dpp[i][j][k][l + 1] += dpp[i][j][k][l]; } cout << dpp[a][g][c][t] << endl; return 0; }
#include <algorithm> #include <assert.h> #include <bitset> #include <complex> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i) #define rep(i, n) REP(i, 0, n) using ll = long long; const int inf = 1e9 + 7; const ll longinf = 1LL << 60; const ll mod = 1e9 + 7; string dp[55]; string s[55]; map<string, int> mp; bool ischar(char c) { if ('a' <= c && c <= 'z') return true; if ('A' <= c && c <= 'Z') return true; if (c == '[' || c == ']' || c == ':' || c == '*') return true; return false; } string rec(int idx) { if (dp[idx] != "") return dp[idx]; string ret = ""; int i = 1; while (ischar(s[idx][i])) ++i; while (1) { ++i; if (s[idx][i] == '*') break; if (s[idx][i] == '[') { char cur = 0; while (ischar(s[idx][i])) { if (s[idx][i] == 'A') cur += 8; if (s[idx][i] == 'T') cur += 4; if (s[idx][i] == 'G') cur += 2; if (s[idx][i] == 'C') cur += 1; ++i; } ret += cur; } else { string nxt = ""; while (ischar(s[idx][i])) { nxt += s[idx][i]; ++i; } ret += rec(mp[nxt]); } if (ret.size() > 300) { ret.resize(300); return dp[idx] = ret; } } return dp[idx] = ret; } ll dpp[55][55][55][55]; int main() { int a, g, c, t; cin >> a >> g >> c >> t; int n; scanf("%d ", &n); rep(i, n) { getline(cin, s[i]); } rep(i, n) { s[i] += " *"; string na = ""; int j = 0; while (s[i][j] != ':') { na += s[i][j++]; } mp[na] = i; } string res = rec(0); if (res.size() != a + g + c + t) { cout << 0 << endl; return 0; } dpp[0][0][0][0] = 1; n = res.size(); rep(i, 51) rep(j, 51) rep(k, 51) rep(l, 51) { dpp[i][j][k][l] %= mod; if (i + j + k + l >= n) break; int cur = i + j + k + l; cur = res[cur]; if (cur & 8) dpp[i + 1][j][k][l] += dpp[i][j][k][l]; if (cur & 4) dpp[i][j + 1][k][l] += dpp[i][j][k][l]; if (cur & 2) dpp[i][j][k + 1][l] += dpp[i][j][k][l]; if (cur & 1) dpp[i][j][k][l + 1] += dpp[i][j][k][l]; } cout << dpp[a][g][c][t] << endl; return 0; }
replace
101
102
101
102
0
p01551
C++
Memory Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i)) #define all(x) (x).begin(), (x).end() #define pb push_back #define fi first #define se second #define dbg(x) cout << #x " = " << ((x)) << endl template <class T, class U> ostream &operator<<(ostream &o, const pair<T, U> &p) { o << "(" << p.fi << "," << p.se << ")"; return o; } template <class T> ostream &operator<<(ostream &o, const vector<T> &v) { o << "["; for (T t : v) { o << t << ","; } o << "]"; return o; } vector<string> split(const string &str, char sep) { vector<string> v; stringstream ss(str + sep); string buffer; while (getline(ss, buffer, sep)) v.pb(buffer); return v; } int Na, Nt, Ng, Nc; int m; const int N = 52; const ll mod = 1e9 + 7; vector<string> x; int X; ll dp[N][N][N][N]; ll dfs(int a, int t, int g, int c) { if (a > Na || t > Nt || g > Ng || c > Nc) return 0; int idx = a + t + g + c; if (idx == X) { return (a == Na && t == Nt && g == Ng && c == Nc); } if (dp[a][t][g][c] >= 0) return dp[a][t][g][c]; string f = x[idx]; ll ret = 0; rep(i, f.size()) { if (f[i] == 'A') ret += dfs(a + 1, t, g, c); else if (f[i] == 'T') ret += dfs(a, t + 1, g, c); else if (f[i] == 'G') ret += dfs(a, t, g + 1, c); else if (f[i] == 'C') ret += dfs(a, t, g, c + 1); ret %= mod; } return dp[a][t][g][c] = ret; } int main() { cin >> Na >> Nt >> Ng >> Nc >> m; cin.ignore(); map<string, int> t2id; vector<vector<string>> v(m); rep(i, m) { string s; getline(cin, s); vector<string> vs = split(s, ' '); string t = vs[0]; t = t.substr(0, t.size() - 1); t2id[t] = i; for (int j = 1; j < vs.size(); ++j) v[i].pb(vs[j]); } for (int i = m - 1; i >= 0; --i) { vector<string> new_v; rep(j, v[i].size()) { if (v[i][j][0] == '[') new_v.pb(v[i][j].substr()); else { int idx = t2id[v[i][j]]; for (string a : v[idx]) new_v.pb(a); } } v[i] = new_v; } x = v[0]; X = x.size(); memset(dp, -1, sizeof(dp)); cout << dfs(0, 0, 0, 0) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i)) #define all(x) (x).begin(), (x).end() #define pb push_back #define fi first #define se second #define dbg(x) cout << #x " = " << ((x)) << endl template <class T, class U> ostream &operator<<(ostream &o, const pair<T, U> &p) { o << "(" << p.fi << "," << p.se << ")"; return o; } template <class T> ostream &operator<<(ostream &o, const vector<T> &v) { o << "["; for (T t : v) { o << t << ","; } o << "]"; return o; } vector<string> split(const string &str, char sep) { vector<string> v; stringstream ss(str + sep); string buffer; while (getline(ss, buffer, sep)) v.pb(buffer); return v; } int Na, Nt, Ng, Nc; int m; const int N = 52; const ll mod = 1e9 + 7; vector<string> x; int X; ll dp[N][N][N][N]; ll dfs(int a, int t, int g, int c) { if (a > Na || t > Nt || g > Ng || c > Nc) return 0; int idx = a + t + g + c; if (idx == X) { return (a == Na && t == Nt && g == Ng && c == Nc); } if (dp[a][t][g][c] >= 0) return dp[a][t][g][c]; string f = x[idx]; ll ret = 0; rep(i, f.size()) { if (f[i] == 'A') ret += dfs(a + 1, t, g, c); else if (f[i] == 'T') ret += dfs(a, t + 1, g, c); else if (f[i] == 'G') ret += dfs(a, t, g + 1, c); else if (f[i] == 'C') ret += dfs(a, t, g, c + 1); ret %= mod; } return dp[a][t][g][c] = ret; } int main() { cin >> Na >> Nt >> Ng >> Nc >> m; cin.ignore(); map<string, int> t2id; vector<vector<string>> v(m); rep(i, m) { string s; getline(cin, s); vector<string> vs = split(s, ' '); string t = vs[0]; t = t.substr(0, t.size() - 1); t2id[t] = i; for (int j = 1; j < vs.size(); ++j) v[i].pb(vs[j]); } // precalc for length vector<ll> L(m); for (int i = m - 1; i >= 0; --i) { rep(j, v[i].size()) { if (v[i][j][0] == '[') ++L[i]; else { int idx = t2id[v[i][j]]; L[i] += L[idx]; } } } // Over Length if (L[0] > Na + Nt + Ng + Nc) { cout << 0 << endl; return 0; } // calc DP for (int i = m - 1; i >= 0; --i) { vector<string> new_v; rep(j, v[i].size()) { if (v[i][j][0] == '[') new_v.pb(v[i][j].substr()); else { int idx = t2id[v[i][j]]; for (string a : v[idx]) new_v.pb(a); } } v[i] = new_v; } x = v[0]; X = x.size(); memset(dp, -1, sizeof(dp)); cout << dfs(0, 0, 0, 0) << endl; return 0; }
insert
89
89
89
109
MLE
p01551
C++
Memory Limit Exceeded
#include <iostream> #include <string> #include <vector> using namespace std; #define MAX_N 100 #define MOD 1000000007 int A, B, C, D, N; int dp1[MAX_N]; string S, U[MAX_N]; vector<string> T[MAX_N]; vector<string> W[MAX_N]; int X[200][4]; char DNA[5] = "ATGC"; int dp[300][70][70][70]; int solve() { for (int i = N - 1; i >= 0; i--) { for (int j = 0; j < T[i].size(); j++) { if (T[i][j][0] - 'A' >= 26) { for (int k = 0; k < N; k++) { if (T[i][j] == U[k]) { for (int l = 0; l < W[k].size(); l++) { W[i].push_back(W[k][l]); } } } } else { W[i].push_back(T[i][j]); } } } for (int i = 0; i < W[0].size(); i++) { for (int j = 0; j < W[0][i].size(); j++) { for (int k = 0; k < 4; k++) { if (DNA[k] == W[0][i][j]) { X[i][k] = 1; } } } } dp[0][0][0][0] = 1; for (int i = 0; i < W[0].size(); i++) { for (int j = 0; j <= A; j++) { for (int k = 0; k <= B; k++) { for (int l = 0; l <= C; l++) { if (X[i][0] == 1) { dp[i + 1][j + 1][k][l] += dp[i][j][k][l]; dp[i + 1][j + 1][k][l] %= MOD; } if (X[i][1] == 1) { dp[i + 1][j][k + 1][l] += dp[i][j][k][l]; dp[i + 1][j][k + 1][l] %= MOD; } if (X[i][2] == 1) { dp[i + 1][j][k][l + 1] += dp[i][j][k][l]; dp[i + 1][j][k][l + 1] %= MOD; } if (X[i][3] == 1) { dp[i + 1][j][k][l] += dp[i][j][k][l]; dp[i + 1][j][k][l] %= MOD; } } } } } return dp[W[0].size()][A][B][C]; } int main() { cin >> A >> B >> C >> D >> N; for (int i = 0; i < N; i++) { S = ""; while (S == "") { getline(cin, S); } S += ' '; string V = ""; for (int j = 0; j < S.size(); j++) { if (S[j] == ':') { U[i] = V; V = ""; j++; } else if (S[j] == ']' || S[j] == ' ') { if (S[j - 1] != ']') { T[i].push_back(V); V = ""; } } else if (S[j] != '[') { V += S[j]; } } } for (int i = N - 1; i >= 0; i--) { int score = 0; for (int j = 0; j < T[i].size(); j++) { if (T[i][j][0] - 'A' >= 26) { for (int k = 0; k < N; k++) { if (T[i][j] == U[k]) { score += dp1[k]; } } } else { score += 1; } } if (score >= 2000) { dp1[0] = 2000; break; } dp1[i] = score; } if (dp1[0] != A + B + C + D) { cout << "0" << endl; goto E; } cout << solve() << endl; E:; return 0; }
#include <iostream> #include <string> #include <vector> using namespace std; #define MAX_N 100 #define MOD 1000000007 int A, B, C, D, N; int dp1[MAX_N]; string S, U[MAX_N]; vector<string> T[MAX_N]; vector<string> W[MAX_N]; int X[200][4]; char DNA[5] = "ATGC"; int dp[216][54][54][54]; int solve() { for (int i = N - 1; i >= 0; i--) { for (int j = 0; j < T[i].size(); j++) { if (T[i][j][0] - 'A' >= 26) { for (int k = 0; k < N; k++) { if (T[i][j] == U[k]) { for (int l = 0; l < W[k].size(); l++) { W[i].push_back(W[k][l]); } } } } else { W[i].push_back(T[i][j]); } } } for (int i = 0; i < W[0].size(); i++) { for (int j = 0; j < W[0][i].size(); j++) { for (int k = 0; k < 4; k++) { if (DNA[k] == W[0][i][j]) { X[i][k] = 1; } } } } dp[0][0][0][0] = 1; for (int i = 0; i < W[0].size(); i++) { for (int j = 0; j <= A; j++) { for (int k = 0; k <= B; k++) { for (int l = 0; l <= C; l++) { if (X[i][0] == 1) { dp[i + 1][j + 1][k][l] += dp[i][j][k][l]; dp[i + 1][j + 1][k][l] %= MOD; } if (X[i][1] == 1) { dp[i + 1][j][k + 1][l] += dp[i][j][k][l]; dp[i + 1][j][k + 1][l] %= MOD; } if (X[i][2] == 1) { dp[i + 1][j][k][l + 1] += dp[i][j][k][l]; dp[i + 1][j][k][l + 1] %= MOD; } if (X[i][3] == 1) { dp[i + 1][j][k][l] += dp[i][j][k][l]; dp[i + 1][j][k][l] %= MOD; } } } } } return dp[W[0].size()][A][B][C]; } int main() { cin >> A >> B >> C >> D >> N; for (int i = 0; i < N; i++) { S = ""; while (S == "") { getline(cin, S); } S += ' '; string V = ""; for (int j = 0; j < S.size(); j++) { if (S[j] == ':') { U[i] = V; V = ""; j++; } else if (S[j] == ']' || S[j] == ' ') { if (S[j - 1] != ']') { T[i].push_back(V); V = ""; } } else if (S[j] != '[') { V += S[j]; } } } for (int i = N - 1; i >= 0; i--) { int score = 0; for (int j = 0; j < T[i].size(); j++) { if (T[i][j][0] - 'A' >= 26) { for (int k = 0; k < N; k++) { if (T[i][j] == U[k]) { score += dp1[k]; } } } else { score += 1; } } if (score >= 2000) { dp1[0] = 2000; break; } dp1[i] = score; } if (dp1[0] != A + B + C + D) { cout << "0" << endl; goto E; } cout << solve() << endl; E:; return 0; }
replace
15
16
15
16
MLE
p01551
C++
Runtime Error
#include <algorithm> #include <bits/stdc++.h> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <math.h> #include <queue> #include <string> #include <vector> // #include <boost/multiprecision/cpp_int.hpp> #include <functional> // #define int long long #define inf 1000000007 #define pa pair<int, int> #define ll long long #define pal pair<double, int> #define ppa pair<pa, int> #define ppap pair<int, pa> #define ssa pair<string, int> #define mp make_pair #define pb push_back #define EPS (1e-10) #define equals(a, b) (fabs((a) - (b)) < EPS) using namespace std; // priority_queue<int, vector<int>, greater<int> > que; class Point { public: double x, y; Point(double x = 0, double y = 0) : x(x), y(y) {} Point operator+(Point p) { return Point(x + p.x, y + p.y); } Point operator-(Point p) { return Point(x - p.x, y - p.y); } Point operator*(double a) { return Point(x * a, y * a); } Point operator/(double a) { return Point(x / a, y / a); } double absv() { return sqrt(norm()); } double norm() { return x * x + y * y; } bool operator<(const Point &p) const { return x != p.x ? x < p.x : y < p.y; } bool operator==(const Point &p) const { return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS; } }; typedef Point Vector; struct Segment { Point p1, p2; }; double hen(Vector a) { if (fabs(a.x) < EPS && a.y > 0) return acos(0); else if (fabs(a.x) < EPS && a.y < 0) return 3 * acos(0); else if (fabs(a.y) < EPS && a.x < 0) return 2 * acos(0); else if (fabs(a.y) < EPS && a.x > 0) return 0.0; else if (a.y > 0) return acos(a.x / a.absv()); else return 2 * acos(0) + acos(-a.x / a.absv()); } string itos(int i) { ostringstream s; s << i; return s.str(); } int gcd(int v, int b) { if (v > b) return gcd(b, v); if (v == b) return b; if (b % v == 0) return v; return gcd(v, b % v); } double dot(Vector a, Vector b) { return a.x * b.x + a.y * b.y; } double cross(Vector a, Vector b) { return a.x * b.y - a.y * b.x; } double distans(double x1, double y1, double x2, double y2) { double rr = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); return sqrt(rr); } /*} int pr[100010]; //int inv[100010]; int beki(int wa,int rr){ if(rr==0) return 1ll; if(rr==1) return wa; if(rr%2==1) return (beki(wa,rr-1)*wa)%inf; int zx=beki(wa,rr/2); return (zx*zx)%inf; } void gya(){ pr[0]=1; for(int i=1;i<100010;i++){ pr[i]=(pr[i-1]*i)%inf; } for(int i=0;i<100010;i++) inv[i]=beki(pr[i],inf-2); } */ //----------------kokomade tenpure------------ int na, nt, ng, nc, m; string s[200]; map<string, vector<int>> ma; int dp[210][51][51][51] = {0}; int ch(string s) { // cout<<s<<endl; int l = s.length(); int cv[4] = {0}; for (int i = 0; i < l; i++) { if (s[i] == 'A') cv[0] = 1; if (s[i] == 'T') cv[1] = 1; if (s[i] == 'C') cv[2] = 1; if (s[i] == 'G') cv[3] = 1; } return cv[0] * 8 + cv[1] * 4 + cv[2] * 2 + cv[3]; } string t, t2; vector<int> as; signed main() { cin >> na >> nt >> ng >> nc >> m; getline(cin, s[0]); for (int i = 1; i <= m; i++) { getline(cin, s[i]); s[i] += " "; } // s[m] +=" "; for (int i = m; i >= 1; i--) { as.clear(); vector<int> z; for (int j = 0; j < s[i].length() - 1; j++) if (s[i][j] == ' ') z.pb(j); z.pb(s[i].length() - 1); t = s[i].substr(0, z[0] - 1); // cout<<"t "<<t<<endl; for (int j = 1; j < z.size(); j++) { t2 = s[i].substr(z[j - 1] + 1, z[j] - z[j - 1] - 1); // cout<<"t2 "<<t2<<endl; if (t2[0] == '[' && t2[t2.length() - 1] == ']') { as.pb(ch(t2)); } else { // cout<<"sss"<<ma["a"].size()<<endl; vector<int> y = ma[t2]; for (int ii = 0; ii < y.size(); ii++) { as.pb(y[ii]); } } // cout<<"assize "<<as.size()<<endl; } // cout<<t<<" "<<as.size()<<endl; ma[t] = as; } // cout<<as.size()<<endl; if (as.size() != na + nt + nc + ng) { cout << 0 << endl; return 0; } dp[0][0][0][0] = 1; int r[4] = {0}; for (int i = 0; i < as.size(); i++) { if (as[i] % 2 == 0) r[3] = 0; else r[3] = 1; as[i] /= 2; if (as[i] % 2 == 0) r[2] = 0; else r[2] = 1; as[i] /= 2; if (as[i] % 2 == 0) r[1] = 0; else r[1] = 1; as[i] /= 2; if (as[i] % 2 == 0) r[0] = 0; else r[0] = 1; // for(int j=0;j<4;j++) cout<<r[j]<<" "; // cout<<endl; for (int aa = 0; aa <= 50; aa++) for (int tt = 0; tt <= 50; tt++) for (int cc = 0; cc <= 50; cc++) { if (dp[i][aa][tt][cc] == 0) continue; for (int j = 0; j < 4; j++) { if (r[j] == 0) continue; if (j == 0 && aa + 1 <= 50) { dp[i + 1][aa + 1][tt][cc] += dp[i][aa][tt][cc]; dp[i + 1][aa + 1][tt][cc] %= inf; } if (j == 1 && tt + 1 <= 50) { dp[i + 1][aa][tt + 1][cc] += dp[i][aa][tt][cc]; dp[i + 1][aa][tt + 1][cc] %= inf; } if (j == 2 && cc + 1 <= 50) { dp[i + 1][aa][tt][cc + 1] += dp[i][aa][tt][cc]; dp[i + 1][aa][tt][cc + 1] %= inf; } if (j == 3) { dp[i + 1][aa][tt][cc] += dp[i][aa][tt][cc]; dp[i + 1][aa][tt][cc] %= inf; } } } } cout << dp[as.size()][na][nt][nc] << endl; return 0; }
#include <algorithm> #include <bits/stdc++.h> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <math.h> #include <queue> #include <string> #include <vector> // #include <boost/multiprecision/cpp_int.hpp> #include <functional> // #define int long long #define inf 1000000007 #define pa pair<int, int> #define ll long long #define pal pair<double, int> #define ppa pair<pa, int> #define ppap pair<int, pa> #define ssa pair<string, int> #define mp make_pair #define pb push_back #define EPS (1e-10) #define equals(a, b) (fabs((a) - (b)) < EPS) using namespace std; // priority_queue<int, vector<int>, greater<int> > que; class Point { public: double x, y; Point(double x = 0, double y = 0) : x(x), y(y) {} Point operator+(Point p) { return Point(x + p.x, y + p.y); } Point operator-(Point p) { return Point(x - p.x, y - p.y); } Point operator*(double a) { return Point(x * a, y * a); } Point operator/(double a) { return Point(x / a, y / a); } double absv() { return sqrt(norm()); } double norm() { return x * x + y * y; } bool operator<(const Point &p) const { return x != p.x ? x < p.x : y < p.y; } bool operator==(const Point &p) const { return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS; } }; typedef Point Vector; struct Segment { Point p1, p2; }; double hen(Vector a) { if (fabs(a.x) < EPS && a.y > 0) return acos(0); else if (fabs(a.x) < EPS && a.y < 0) return 3 * acos(0); else if (fabs(a.y) < EPS && a.x < 0) return 2 * acos(0); else if (fabs(a.y) < EPS && a.x > 0) return 0.0; else if (a.y > 0) return acos(a.x / a.absv()); else return 2 * acos(0) + acos(-a.x / a.absv()); } string itos(int i) { ostringstream s; s << i; return s.str(); } int gcd(int v, int b) { if (v > b) return gcd(b, v); if (v == b) return b; if (b % v == 0) return v; return gcd(v, b % v); } double dot(Vector a, Vector b) { return a.x * b.x + a.y * b.y; } double cross(Vector a, Vector b) { return a.x * b.y - a.y * b.x; } double distans(double x1, double y1, double x2, double y2) { double rr = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); return sqrt(rr); } /*} int pr[100010]; //int inv[100010]; int beki(int wa,int rr){ if(rr==0) return 1ll; if(rr==1) return wa; if(rr%2==1) return (beki(wa,rr-1)*wa)%inf; int zx=beki(wa,rr/2); return (zx*zx)%inf; } void gya(){ pr[0]=1; for(int i=1;i<100010;i++){ pr[i]=(pr[i-1]*i)%inf; } for(int i=0;i<100010;i++) inv[i]=beki(pr[i],inf-2); } */ //----------------kokomade tenpure------------ int na, nt, ng, nc, m; string s[200]; map<string, vector<int>> ma; int dp[210][51][51][51] = {0}; int ch(string s) { // cout<<s<<endl; int l = s.length(); int cv[4] = {0}; for (int i = 0; i < l; i++) { if (s[i] == 'A') cv[0] = 1; if (s[i] == 'T') cv[1] = 1; if (s[i] == 'C') cv[2] = 1; if (s[i] == 'G') cv[3] = 1; } return cv[0] * 8 + cv[1] * 4 + cv[2] * 2 + cv[3]; } string t, t2; vector<int> as; signed main() { cin >> na >> nt >> ng >> nc >> m; getline(cin, s[0]); for (int i = 1; i <= m; i++) { getline(cin, s[i]); s[i] += " "; } // s[m] +=" "; for (int i = m; i >= 1; i--) { as.clear(); vector<int> z; for (int j = 0; j < s[i].length() - 1; j++) if (s[i][j] == ' ') z.pb(j); z.pb(s[i].length() - 1); t = s[i].substr(0, z[0] - 1); // cout<<"t "<<t<<endl; for (int j = 1; j < z.size(); j++) { t2 = s[i].substr(z[j - 1] + 1, z[j] - z[j - 1] - 1); // cout<<"t2 "<<t2<<endl; if (t2[0] == '[' && t2[t2.length() - 1] == ']') { as.pb(ch(t2)); } else { // cout<<"sss"<<ma["a"].size()<<endl; vector<int> y = ma[t2]; for (int ii = 0; ii < y.size(); ii++) { as.pb(y[ii]); } } // cout<<"assize "<<as.size()<<endl; } // cout<<t<<" "<<as.size()<<endl; ma[t] = as; if (as.size() > 200) { cout << 0 << endl; return 0; } } // cout<<as.size()<<endl; if (as.size() != na + nt + nc + ng) { cout << 0 << endl; return 0; } dp[0][0][0][0] = 1; int r[4] = {0}; for (int i = 0; i < as.size(); i++) { if (as[i] % 2 == 0) r[3] = 0; else r[3] = 1; as[i] /= 2; if (as[i] % 2 == 0) r[2] = 0; else r[2] = 1; as[i] /= 2; if (as[i] % 2 == 0) r[1] = 0; else r[1] = 1; as[i] /= 2; if (as[i] % 2 == 0) r[0] = 0; else r[0] = 1; // for(int j=0;j<4;j++) cout<<r[j]<<" "; // cout<<endl; for (int aa = 0; aa <= 50; aa++) for (int tt = 0; tt <= 50; tt++) for (int cc = 0; cc <= 50; cc++) { if (dp[i][aa][tt][cc] == 0) continue; for (int j = 0; j < 4; j++) { if (r[j] == 0) continue; if (j == 0 && aa + 1 <= 50) { dp[i + 1][aa + 1][tt][cc] += dp[i][aa][tt][cc]; dp[i + 1][aa + 1][tt][cc] %= inf; } if (j == 1 && tt + 1 <= 50) { dp[i + 1][aa][tt + 1][cc] += dp[i][aa][tt][cc]; dp[i + 1][aa][tt + 1][cc] %= inf; } if (j == 2 && cc + 1 <= 50) { dp[i + 1][aa][tt][cc + 1] += dp[i][aa][tt][cc]; dp[i + 1][aa][tt][cc + 1] %= inf; } if (j == 3) { dp[i + 1][aa][tt][cc] += dp[i][aa][tt][cc]; dp[i + 1][aa][tt][cc] %= inf; } } } } cout << dp[as.size()][na][nt][nc] << endl; return 0; }
insert
176
176
176
180
-11
p01551
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep1(i, n) for (int i = 1; i <= (int)(n); i++) #define all(c) c.begin(), c.end() #define pb push_back #define fs first #define sc second #define show(x) cout << #x << " = " << x << endl #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) using namespace std; int mod = 1e9 + 7; void add(int &x, int y) { x += y; if (x >= mod) x -= mod; } int A, B, C, D, N; map<string, int> mp; string nam[50]; vector<string> vst[50]; int num[50][16]; int dp[51][52][52][52]; int main() { cin >> A >> B >> C >> D >> N; { string st; int I = -1; while (cin >> st) { if (st.back() == ':') { I++; nam[I] = st.substr(0, st.size() - 1); mp[nam[I]] = I; } else { vst[I].pb(st); } } } for (int i = N - 1; i >= 0; i--) { for (string st : vst[i]) { if (st[0] == '[') { int bit = 0; for (char ch : st) { if (ch == 'A') bit |= 1; if (ch == 'T') bit |= 2; if (ch == 'G') bit |= 4; if (ch == 'C') bit |= 8; } num[i][bit]++; } else { int id = mp[st]; rep(j, 16) num[i][j] += num[id][j]; } } } int S = 0; rep(i, 16) S += num[0][i]; if (S != A + B + C + D) { puts("0"); return 0; } vector<int> bs; rep(i, 16) rep(j, num[0][i]) bs.pb(i); dp[0][0][0][0] = 1; rep(i, S) { rep(a, A + 1) rep(b, B + 1) rep(c, C + 1) { int d = i - a - b - c; if (d < 0 || d > D) continue; int x = bs[i]; rep(j, 4) { if ((x >> j) & 1) add(dp[i + 1][a + (j == 0)][b + (j == 1)][c + (j == 2)], dp[i][a][b][c]); } } } cout << dp[S][A][B][C] << endl; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep1(i, n) for (int i = 1; i <= (int)(n); i++) #define all(c) c.begin(), c.end() #define pb push_back #define fs first #define sc second #define show(x) cout << #x << " = " << x << endl #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) using namespace std; int mod = 1e9 + 7; void add(int &x, int y) { x += y; if (x >= mod) x -= mod; } int A, B, C, D, N; map<string, int> mp; string nam[50]; vector<string> vst[50]; int num[50][16]; int dp[201][52][52][52]; int main() { cin >> A >> B >> C >> D >> N; { string st; int I = -1; while (cin >> st) { if (st.back() == ':') { I++; nam[I] = st.substr(0, st.size() - 1); mp[nam[I]] = I; } else { vst[I].pb(st); } } } for (int i = N - 1; i >= 0; i--) { for (string st : vst[i]) { if (st[0] == '[') { int bit = 0; for (char ch : st) { if (ch == 'A') bit |= 1; if (ch == 'T') bit |= 2; if (ch == 'G') bit |= 4; if (ch == 'C') bit |= 8; } num[i][bit]++; } else { int id = mp[st]; rep(j, 16) num[i][j] += num[id][j]; } } } int S = 0; rep(i, 16) S += num[0][i]; if (S != A + B + C + D) { puts("0"); return 0; } vector<int> bs; rep(i, 16) rep(j, num[0][i]) bs.pb(i); dp[0][0][0][0] = 1; rep(i, S) { rep(a, A + 1) rep(b, B + 1) rep(c, C + 1) { int d = i - a - b - c; if (d < 0 || d > D) continue; int x = bs[i]; rep(j, 4) { if ((x >> j) & 1) add(dp[i + 1][a + (j == 0)][b + (j == 1)][c + (j == 2)], dp[i][a][b][c]); } } } cout << dp[S][A][B][C] << endl; }
replace
23
24
23
24
0
p01552
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define repi(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, a) repi(i, 0, a) #define repd(i, a, b) for (int i = (a); i >= (b); --i) #define repit(i, a) \ for (__typeof((a).begin()) i = (a).begin(); i != (a).end(); ++i) #define all(u) (u).begin(), (u).end() #define rall(u) (u).rbegin(), (u).rend() #define UNIQUE(u) (u).erase(unique(all(u)), (u).end()) #define pb push_back #define mp make_pair #define INF 1e9 #define EPS 1e-9 #define PI acos(-1.0) using namespace std; typedef long long ll; typedef vector<int> vi; string prop[32]; int main() { rep(i, 32) prop[i] = ""; string in; cin >> in; int sz = 0; rep(i, in.size()) { if (in[i] == '.') sz++; else prop[sz - 1] += in[i]; } int pos = 0; int depth = 0; int prev = 0; bool flag = false; int indents[32]; cin.ignore(); while (getline(cin, in)) { if (in.size() == 0) break; int cur = 0; while (in[cur] == ' ') cur++; string tag = "", str = ""; int t = cur; while (in[t] != ':') tag += in[t++]; t += 2; while (t < in.size()) str += in[t++]; if (cur > prev) { depth++; indents[depth] = cur; } if (cur < prev) { while (indents[depth] > cur) depth--; } if (depth < pos) { pos = depth; } if (depth == pos && prop[pos] == tag) { pos++; if (pos == sz) { if (str.size() == 0) cout << "object\n"; else cout << "string \"" << str << "\"\n"; return 0; } } prev = cur; } cout << "no such property\n"; return 0; }
#include <algorithm> #include <bitset> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #define repi(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, a) repi(i, 0, a) #define repd(i, a, b) for (int i = (a); i >= (b); --i) #define repit(i, a) \ for (__typeof((a).begin()) i = (a).begin(); i != (a).end(); ++i) #define all(u) (u).begin(), (u).end() #define rall(u) (u).rbegin(), (u).rend() #define UNIQUE(u) (u).erase(unique(all(u)), (u).end()) #define pb push_back #define mp make_pair #define INF 1e9 #define EPS 1e-9 #define PI acos(-1.0) using namespace std; typedef long long ll; typedef vector<int> vi; string prop[32]; int main() { rep(i, 32) prop[i] = ""; string in; cin >> in; int sz = 0; rep(i, in.size()) { if (in[i] == '.') sz++; else prop[sz - 1] += in[i]; } int pos = 0; int depth = 0; int prev = 0; bool flag = false; int indents[50000] = {0}; cin.ignore(); while (getline(cin, in)) { if (in.size() == 0) break; int cur = 0; while (in[cur] == ' ') cur++; string tag = "", str = ""; int t = cur; while (in[t] != ':') tag += in[t++]; t += 2; while (t < in.size()) str += in[t++]; if (cur > prev) { depth++; indents[depth] = cur; } if (cur < prev) { while (indents[depth] > cur) depth--; } if (depth < pos) { pos = depth; } if (depth == pos && prop[pos] == tag) { pos++; if (pos == sz) { if (str.size() == 0) cout << "object\n"; else cout << "string \"" << str << "\"\n"; return 0; } } prev = cur; } cout << "no such property\n"; return 0; }
replace
52
53
52
53
0
p01553
C++
Runtime Error
#include <iostream> #include <vector> using namespace std; #define MOD 1000000007 typedef long long ll; ll dp[201][201][201] = {0}; // dp[n][rest U][rest empty box] = ??´????????° int main() { int N; cin >> N; vector<int> v; for (int i = 0; i < N; ++i) { char c; cin >> c; if (c != '-') { v.push_back(c); } } N = v.size(); if (v[0] == 'D') { cout << 0 << endl; return 0; } dp[0][1][1] = 1; // v[0] = 'U' for (int i = 0; i < N - 1; ++i) { for (int j = 0; j < N; ++j) { for (int k = 0; k < N; ++k) { if (v[i + 1] == 'U') { dp[i + 1][j + 1][k + 1] = (dp[i + 1][j + 1][k + 1] + dp[i][j][k]) % MOD; if (j >= 1) { dp[i + 1][j][k] = (dp[i + 1][j][k] + j * dp[i][j][k]) % MOD; } } else { // D if (k >= 1) { // if (dp[i][j][k]) cout << i << " " << j << " " << k << //" : " << dp[i][j][k] << endl; dp[i + 1][j][k] = (dp[i + 1][j][k] + k * dp[i][j][k]) % MOD; if (j >= 1) { dp[i + 1][j - 1][k - 1] = (dp[i + 1][j - 1][k - 1] + j * k * dp[i][j][k]) % MOD; } } } } } } cout << dp[N - 1][0][0] << endl; }
#include <iostream> #include <vector> using namespace std; #define MOD 1000000007 typedef long long ll; ll dp[201][201][201] = {0}; // dp[n][rest U][rest empty box] = ??´????????° int main() { int N; cin >> N; vector<int> v; for (int i = 0; i < N; ++i) { char c; cin >> c; if (c != '-') { v.push_back(c); } } N = v.size(); if (N == 0) { cout << 1 << endl; return 0; } if (v[0] == 'D') { cout << 0 << endl; return 0; } dp[0][1][1] = 1; // v[0] = 'U' for (int i = 0; i < N - 1; ++i) { for (int j = 0; j < N; ++j) { for (int k = 0; k < N; ++k) { if (v[i + 1] == 'U') { dp[i + 1][j + 1][k + 1] = (dp[i + 1][j + 1][k + 1] + dp[i][j][k]) % MOD; if (j >= 1) { dp[i + 1][j][k] = (dp[i + 1][j][k] + j * dp[i][j][k]) % MOD; } } else { // D if (k >= 1) { // if (dp[i][j][k]) cout << i << " " << j << " " << k << //" : " << dp[i][j][k] << endl; dp[i + 1][j][k] = (dp[i + 1][j][k] + k * dp[i][j][k]) % MOD; if (j >= 1) { dp[i + 1][j - 1][k - 1] = (dp[i + 1][j - 1][k - 1] + j * k * dp[i][j][k]) % MOD; } } } } } } cout << dp[N - 1][0][0] << endl; }
insert
24
24
24
28
0
p01553
C++
Runtime Error
#include <cstring> #include <iostream> using namespace std; typedef long long ll; ll dp[128][128][128]; int main() { int n; int const mod = 1000000007; while (cin >> n && n) { memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; for (int k = 0; k < n; k++) { char c; cin >> c; if (c == '-') { for (int x = 0; x <= k; x++) { for (int y = 0; y <= k; y++) { dp[k + 1][x][y] += dp[k][x][y]; } } } if (c == 'D') { for (int x = 1; x <= k; x++) { for (int y = 1; y <= k; y++) { dp[k + 1][x - 1][y - 1] += dp[k][x][y] * x * y; dp[k + 1][x - 1][y - 1] %= mod; } } for (int x = 0; x <= k; x++) { for (int y = 0; y <= k; y++) { dp[k + 1][x][y] += dp[k][x][y] * y; dp[k + 1][x][y] %= mod; } } } if (c == 'U') { for (int x = 0; x <= k; x++) { for (int y = 0; y <= k; y++) { dp[k + 1][x][y] += dp[k][x][y] * x; dp[k + 1][x][y] %= mod; } } for (int x = 0; x <= k; x++) { for (int y = 0; y <= k; y++) { dp[k + 1][x + 1][y + 1] += dp[k][x][y]; dp[k + 1][x + 1][y + 1] %= mod; } } } } // for(int i=0;i<=n;i++){ // for(int j=0;j<=n;j++){ // cout<<dp[n][i][j]<<" "; // if(j==n)cout<<endl; // } // } cout << dp[n][0][0] << endl; } }
#include <cstring> #include <iostream> using namespace std; typedef long long ll; ll dp[256][256][256]; int main() { int n; int const mod = 1000000007; while (cin >> n && n) { memset(dp, 0, sizeof(dp)); dp[0][0][0] = 1; for (int k = 0; k < n; k++) { char c; cin >> c; if (c == '-') { for (int x = 0; x <= k; x++) { for (int y = 0; y <= k; y++) { dp[k + 1][x][y] += dp[k][x][y]; } } } if (c == 'D') { for (int x = 1; x <= k; x++) { for (int y = 1; y <= k; y++) { dp[k + 1][x - 1][y - 1] += dp[k][x][y] * x * y; dp[k + 1][x - 1][y - 1] %= mod; } } for (int x = 0; x <= k; x++) { for (int y = 0; y <= k; y++) { dp[k + 1][x][y] += dp[k][x][y] * y; dp[k + 1][x][y] %= mod; } } } if (c == 'U') { for (int x = 0; x <= k; x++) { for (int y = 0; y <= k; y++) { dp[k + 1][x][y] += dp[k][x][y] * x; dp[k + 1][x][y] %= mod; } } for (int x = 0; x <= k; x++) { for (int y = 0; y <= k; y++) { dp[k + 1][x + 1][y + 1] += dp[k][x][y]; dp[k + 1][x + 1][y + 1] %= mod; } } } } // for(int i=0;i<=n;i++){ // for(int j=0;j<=n;j++){ // cout<<dp[n][i][j]<<" "; // if(j==n)cout<<endl; // } // } cout << dp[n][0][0] << endl; } }
replace
4
5
4
5
0
p01553
C++
Time Limit Exceeded
#include <iostream> using namespace std; typedef long long ll; int main() { ll dp[420] = {}, *dp0 = dp + 1, *dp1 = dp + 221; dp1[0] = 1; int n; cin >> n; while (n--) { char c; cin >> c; if (c == '-') continue; for (int j = 0; j < 210; j++) { dp0[j] = (j * dp1[j] + ((c == 'U') ? dp1[j - 1] : (j + 1) * (j + 1) * dp1[j + 1])) % (ll)(1e9 + 7); } swap(dp0, dp1); } cout << dp1[0] << endl; return 0; }
#include <iostream> using namespace std; typedef long long ll; int main() { ll dp[700] = {}, *dp0 = dp + 100, *dp1 = dp + 400; dp1[0] = 1; int n; cin >> n; while (n--) { char c; cin >> c; if (c == '-') continue; for (int j = 0; j < 210; j++) { dp0[j] = (j * dp1[j] + ((c == 'U') ? dp1[j - 1] : (j + 1) * (j + 1) * dp1[j + 1])) % (ll)(1e9 + 7); } swap(dp0, dp1); } cout << dp1[0] << endl; return 0; }
replace
6
7
6
7
TLE
p01554
C++
Runtime Error
#include <algorithm> #include <cmath> #include <complex> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define ll long long #define vvi vector<vector<int>> #define All(X) X.begin(), X.end() #define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define pb push_back ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } int main() { int n, m; bool open = true; cin >> n; string tou[200]; REP(i, n) { cin >> tou[i]; } cin >> m; string kita; REP(i, m) { cin >> kita; REP(j, n) { if (kita == tou[j]) { if (open) { cout << "Opened by " << kita << endl; open = (not open); break; } else { cout << "Closed by " << kita << endl; open = (not open); break; } } if (j == n - 1) cout << "Unknown " << kita << endl; } } }
#include <algorithm> #include <cmath> #include <complex> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define ll long long #define vvi vector<vector<int>> #define All(X) X.begin(), X.end() #define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define pb push_back ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } int main() { int n, m; bool open = true; cin >> n; string tou[500]; REP(i, n) { cin >> tou[i]; } cin >> m; string kita; REP(i, m) { cin >> kita; REP(j, n) { if (kita == tou[j]) { if (open) { cout << "Opened by " << kita << endl; open = (not open); break; } else { cout << "Closed by " << kita << endl; open = (not open); break; } } if (j == n - 1) cout << "Unknown " << kita << endl; } } }
replace
28
29
28
29
0
p01554
C++
Runtime Error
#include <cstdio> #include <iostream> #include <string> using namespace std; int main() { int n; cin >> n; string str[100]; for (int i = 0; i < n; i++) { cin >> str[i]; } int m; cin >> m; int cnt = 0; for (int j = 0; j < m; j++) { string str2; cin >> str2; for (int k = 0; k < n; k++) { if (str[k] == str2) { cnt++; if (cnt % 2 == 1) cout << "Opened by " << str2 << endl; else cout << "Closed by " << str2 << endl; break; } if (k == n - 1) { cout << "Unknown " << str2 << endl; } } } return 0; }
#include <cstdio> #include <iostream> #include <string> using namespace std; int main() { int n; cin >> n; string str[256]; for (int i = 0; i < n; i++) { cin >> str[i]; } int m; cin >> m; int cnt = 0; for (int j = 0; j < m; j++) { string str2; cin >> str2; for (int k = 0; k < n; k++) { if (str[k] == str2) { cnt++; if (cnt % 2 == 1) cout << "Opened by " << str2 << endl; else cout << "Closed by " << str2 << endl; break; } if (k == n - 1) { cout << "Unknown " << str2 << endl; } } } return 0; }
replace
9
10
9
10
0