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
p00935
C++
Memory Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef pair<int, int> P; typedef pair<ll, ll> Pll; typedef vector<int> Vi; // typedef tuple<int, int, int> T; #define FOR(i, s, x) for (int i = s; i < (int)(x); i++) #define REP(i, x) FOR(i, 0, x) #define ALL(c) c.begin(), c.end() #define DUMP(x) cerr << #x << " = " << (x) << endl const int dr[4] = {-1, 0, 1, 0}; const int dc[4] = {0, 1, 0, -1}; int main() { // use scanf in CodeForces! cin.tie(0); ios_base::sync_with_stdio(false); int N; cin >> N; vector<int> num(N); REP(i, N) cin >> num[i]; set<string> num_set; REP(i, N) { string t; for (int j = i; j < N; j++) { t.push_back(num[j] + '0'); num_set.insert(t); } } REP(i, 1e6) { if (num_set.find(to_string(i)) == num_set.end()) { cout << i << endl; break; } } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef pair<int, int> P; typedef pair<ll, ll> Pll; typedef vector<int> Vi; // typedef tuple<int, int, int> T; #define FOR(i, s, x) for (int i = s; i < (int)(x); i++) #define REP(i, x) FOR(i, 0, x) #define ALL(c) c.begin(), c.end() #define DUMP(x) cerr << #x << " = " << (x) << endl const int dr[4] = {-1, 0, 1, 0}; const int dc[4] = {0, 1, 0, -1}; int main() { // use scanf in CodeForces! cin.tie(0); ios_base::sync_with_stdio(false); int N; cin >> N; vector<int> num(N); REP(i, N) cin >> num[i]; set<string> num_set; REP(i, N) { string t; for (int j = i; j < N; j++) { if (j - i + 1 > 6) break; t.push_back(num[j] + '0'); num_set.insert(t); } } REP(i, 1e6) { if (num_set.find(to_string(i)) == num_set.end()) { cout << i << endl; break; } } return 0; }
insert
30
30
30
32
MLE
p00935
C++
Memory Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(n) begin(n), end(n) int a[1010]; int n; string solve() { set<string> S; for (int i = 0; i < n; i++) { string cur; for (int j = i; j < n; j++) { cur += (char)(a[j] + '0'); S.insert(cur); } } for (int i = 0; i < 1e9; i++) { string s = to_string(i); if (S.find(s) != S.end()) continue; return s; } return ""; } int main() { while (cin >> n) { rep(i, n) cin >> a[i]; cout << solve() << endl; } }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(n) begin(n), end(n) int a[1010]; int n; string solve() { set<string> S; for (int i = 0; i < n; i++) { string cur; for (int j = i; j < n; j++) { cur += (char)(a[j] + '0'); if (cur.size() > 10) continue; S.insert(cur); } } for (int i = 0; i < 1e9; i++) { string s = to_string(i); if (S.find(s) != S.end()) continue; return s; } return ""; } int main() { while (cin >> n) { rep(i, n) cin >> a[i]; cout << solve() << endl; } }
insert
14
14
14
16
MLE
p00935
C++
Runtime Error
#include <bits/stdc++.h> #define r(i, n) for (int i = 0; i < n; i++) using namespace std; bool p[1001]; int main() { int n, c; cin >> n; int a[n]; r(i, n) cin >> a[i]; r(i, n) p[a[i]] = true; if (n > 1) r(i, n - 1) { int s = a[i] * 10 + a[i + 1]; p[s] = true; } if (n > 2) r(i, n - 2) { int s = a[i] * 100 + a[i + 1] * 10 + a[i + 2]; p[s] = true; } if (n > 3) r(i, n - 3) { int s = a[i] * 1000 + a[i + 1] * 100 + a[i + 2] * 10 + a[i + 3]; p[s] = true; } c = 0; while (p[c]) c++; cout << c << endl; }
#include <bits/stdc++.h> #define r(i, n) for (int i = 0; i < n; i++) using namespace std; bool p[10000]; int main() { int n, c; cin >> n; int a[n]; r(i, n) cin >> a[i]; r(i, n) p[a[i]] = true; if (n > 1) r(i, n - 1) { int s = a[i] * 10 + a[i + 1]; p[s] = true; } if (n > 2) r(i, n - 2) { int s = a[i] * 100 + a[i + 1] * 10 + a[i + 2]; p[s] = true; } if (n > 3) r(i, n - 3) { int s = a[i] * 1000 + a[i + 1] * 100 + a[i + 2] * 10 + a[i + 3]; p[s] = true; } c = 0; while (p[c]) c++; cout << c << endl; }
replace
3
4
3
4
0
p00936
C++
Time Limit Exceeded
// aoj-VolumeICPCOOC2015-B / 2015-11-30 #include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <tuple> #include <vector> using namespace std; typedef long long ll; typedef pair<ll, ll> pll; typedef ll int__; #define rep(i, j) for (int__ i = 0; i < (int__)(j); i++) #define repeat(i, j, k) for (int__ i = (j); i < (int__)(k); i++) #define all(v) v.begin(), v.end() template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { rep(i, v.size()) os << v[i] << (i != v.size() - 1 ? " " : ""); return os; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { rep(i, v.size()) is >> v[i]; return is; } #ifdef DEBUG void debug() { cerr << " : Debug" << endl; } #endif template <class F, class... R> void debug(const F &car, const R &...cdr) { #ifdef DEBUG cerr << car << " "; debug(cdr...); #endif } const double EPS = 1e-8; struct Point { double x, y; Point() : x(0), y(0) {} Point(double x, double y) : x(x), y(y){}; bool operator==(const Point &a) const { return x == a.x && y == a.y; } Point operator-(const Point &p) const { return Point(x - p.x, y - p.y); } }; istream &operator>>(istream &is, Point &p) { return is >> p.x >> p.y; } ostream &operator<<(ostream &os, const Point &p) { return os << "(" << p.x << "," << p.y << ")"; } double dot(const Point &a, const Point &b) { return a.x * b.x + a.y * b.y; } double cross(const Point &a, const Point &b) { return a.x * b.y - a.y * b.x; } double norm(const Point &a) { return sqrt(dot(a, a)); } double distance(const Point &a, const Point &b) { return norm(a - b); } bool check(vector<Point> &P, vector<int> &R, Point &np, int nr) { rep(i, P.size()) { if (distance(P[i], np) - (R[i] + nr) < 0.0) return false; } return true; } bool solve() { int n; cin >> n; vector<int> R(n); cin >> R; vector<Point> P; for (int r : R) { double l = P.size() > 0 ? max(P.back().x, (double)r) : r; double h = 1e10; while (h - l > 1e-9) { double m = (l + h) / 2; Point p = Point(m, r); if (check(P, R, p, r)) h = m; else l = m; } P.push_back(Point(h, r)); } debug(P); double ans = 0; rep(i, P.size()) ans = max(ans, P[i].x + R[i]); printf("%.10f\n", ans); return false; } int main() { ios::sync_with_stdio(false); while (solve()) ; return 0; }
// aoj-VolumeICPCOOC2015-B / 2015-11-30 #include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstdlib> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <tuple> #include <vector> using namespace std; typedef long long ll; typedef pair<ll, ll> pll; typedef ll int__; #define rep(i, j) for (int__ i = 0; i < (int__)(j); i++) #define repeat(i, j, k) for (int__ i = (j); i < (int__)(k); i++) #define all(v) v.begin(), v.end() template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { rep(i, v.size()) os << v[i] << (i != v.size() - 1 ? " " : ""); return os; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { rep(i, v.size()) is >> v[i]; return is; } #ifdef DEBUG void debug() { cerr << " : Debug" << endl; } #endif template <class F, class... R> void debug(const F &car, const R &...cdr) { #ifdef DEBUG cerr << car << " "; debug(cdr...); #endif } const double EPS = 1e-8; struct Point { double x, y; Point() : x(0), y(0) {} Point(double x, double y) : x(x), y(y){}; bool operator==(const Point &a) const { return x == a.x && y == a.y; } Point operator-(const Point &p) const { return Point(x - p.x, y - p.y); } }; istream &operator>>(istream &is, Point &p) { return is >> p.x >> p.y; } ostream &operator<<(ostream &os, const Point &p) { return os << "(" << p.x << "," << p.y << ")"; } double dot(const Point &a, const Point &b) { return a.x * b.x + a.y * b.y; } double cross(const Point &a, const Point &b) { return a.x * b.y - a.y * b.x; } double norm(const Point &a) { return sqrt(dot(a, a)); } double distance(const Point &a, const Point &b) { return norm(a - b); } bool check(vector<Point> &P, vector<int> &R, Point &np, int nr) { rep(i, P.size()) { if (distance(P[i], np) - (R[i] + nr) < 0.0) return false; } return true; } bool solve() { int n; cin >> n; vector<int> R(n); cin >> R; vector<Point> P; for (int r : R) { double l = P.size() > 0 ? max(P.back().x, (double)r) : r; double h = 1e10; while (h - l > 1e-8) { double m = (l + h) / 2; Point p = Point(m, r); if (check(P, R, p, r)) h = m; else l = m; } P.push_back(Point(h, r)); } debug(P); double ans = 0; rep(i, P.size()) ans = max(ans, P[i].x + R[i]); printf("%.10f\n", ans); return false; } int main() { ios::sync_with_stdio(false); while (solve()) ; return 0; }
replace
78
79
78
79
TLE
p00936
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define DEBUG(x) cerr << #x << " = " << x << endl #define int long long signed main() { int N; cin >> N; vector<int> R(N); REP(i, N) cin >> R[i]; // double ans = 0.0; // ans += R[0] + R[N - 1]; // for(int i = 0; i < N - 1; ++i) { // int mx = max(R[i], R[i + 1]); // int mn = min(R[i], R[i + 1]); // ans += sqrt(max<double>(0.0, (R[i] + R[i + 1]) * (R[i] + R[i + 1]) - (mx // - mn) * (mx - mn))); // } // cout << ans << endl; vector<double> pos(N); double ans = R[0] * 2; pos[0] = R[0]; for (int i = 1; i < N; ++i) { pos[i] = R[i]; REP(j, i) { int mx = max(R[i], R[j]); int mn = min(R[i], R[j]); double update = pos[j] + sqrt(max<double>(0.0, (R[i] + R[j]) * (R[i] + R[j]) - (mx - mn) * (mx - mn))); // DEBUG(update); pos[i] = max(pos[i], update); } ans = max(ans, pos[i] + R[i]); cerr << "pos[" << i << "] = " << pos[i] << endl; } cout.setf(ios::fixed); cout.precision(10); cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < (int)(n); ++i) #define DEBUG(x) cerr << #x << " = " << x << endl #define int long long signed main() { int N; cin >> N; vector<int> R(N); REP(i, N) cin >> R[i]; // double ans = 0.0; // ans += R[0] + R[N - 1]; // for(int i = 0; i < N - 1; ++i) { // int mx = max(R[i], R[i + 1]); // int mn = min(R[i], R[i + 1]); // ans += sqrt(max<double>(0.0, (R[i] + R[i + 1]) * (R[i] + R[i + 1]) - (mx // - mn) * (mx - mn))); // } // cout << ans << endl; vector<double> pos(N); double ans = R[0] * 2; pos[0] = R[0]; for (int i = 1; i < N; ++i) { pos[i] = R[i]; REP(j, i) { int mx = max(R[i], R[j]); int mn = min(R[i], R[j]); double update = pos[j] + sqrt(max<double>(0.0, (R[i] + R[j]) * (R[i] + R[j]) - (mx - mn) * (mx - mn))); // DEBUG(update); pos[i] = max(pos[i], update); } ans = max(ans, pos[i] + R[i]); // cerr << "pos[" << i << "] = " << pos[i] << endl; } cout.setf(ios::fixed); cout.precision(10); cout << ans << endl; }
replace
34
35
34
35
0
pos[1] = 30
p00936
C++
Runtime Error
#include <cassert> #include <cmath> #include <iostream> #define rep(i, n) for (int i = 0; i < n; ++i) using namespace std; double r[1010], x[1010]; const double eps = 1e-11; const double inf = 1e9; bool cross(int m, double cx, double cy) { rep(i, m) { double dd = 0.0; dd += 1.0 * (x[i] - cx) * (x[i] - cx); dd += 1.0 * (r[i] - cy) * (r[i] - cy); double rr = 1.0 * (r[i] + r[m]) * (r[i] + r[m]); // cerr << rr << " " << dd << endl; if (rr > dd + eps) return true; } return false; } int main(void) { int n; cin >> n; rep(i, n) cin >> r[i]; rep(i, n) x[i] = inf; rep(i, n) { for (int j = -1; j < i; ++j) { double cx = r[i]; if (j >= 0) { double d = 1.0 * (r[i] + r[j]) * (r[i] + r[j]); d -= 1.0 * (r[i] - r[j]) * (r[i] - r[j]); cx = x[j] + sqrt(d); } if (cx < r[i]) continue; if (i > 0 && x[i - 1] >= cx) continue; if (cross(i, cx, r[i])) continue; x[i] = min(x[i], cx); } // cerr << i << " " << x[i] << " " << r[i] << endl; } double ans = 0.0; rep(i, n) ans = max(ans, x[i] + r[i]); cout.precision(20); assert(ans < inf); cout << fixed << ans << endl; return 0; }
#include <cassert> #include <cmath> #include <iostream> #define rep(i, n) for (int i = 0; i < n; ++i) using namespace std; double r[1010], x[1010]; const double eps = 1e-5; const double inf = 1e9; bool cross(int m, double cx, double cy) { rep(i, m) { double dd = 0.0; dd += 1.0 * (x[i] - cx) * (x[i] - cx); dd += 1.0 * (r[i] - cy) * (r[i] - cy); double rr = 1.0 * (r[i] + r[m]) * (r[i] + r[m]); // cerr << rr << " " << dd << endl; if (rr > dd + eps) return true; } return false; } int main(void) { int n; cin >> n; rep(i, n) cin >> r[i]; rep(i, n) x[i] = inf; rep(i, n) { for (int j = -1; j < i; ++j) { double cx = r[i]; if (j >= 0) { double d = 1.0 * (r[i] + r[j]) * (r[i] + r[j]); d -= 1.0 * (r[i] - r[j]) * (r[i] - r[j]); cx = x[j] + sqrt(d); } if (cx < r[i]) continue; if (i > 0 && x[i - 1] >= cx) continue; if (cross(i, cx, r[i])) continue; x[i] = min(x[i], cx); } // cerr << i << " " << x[i] << " " << r[i] << endl; } double ans = 0.0; rep(i, n) ans = max(ans, x[i] + r[i]); cout.precision(20); assert(ans < inf); cout << fixed << ans << endl; return 0; }
replace
8
9
8
9
0
p00936
C++
Time Limit Exceeded
#include <algorithm> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <vector> #define _USE_MATH_DEFINES #include <cassert> #include <cctype> #include <cfloat> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> using namespace std; #define EPS 1e-12 #define ull unsigned long long #define ll long long #define VI vector<ll> #define PII pair<ll, ll> #define VVI vector<vector<ll>> #define REP(i, n) for (int i = 0, _n = (n); (i) < (int)_n; ++i) #define RANGE(i, a, b) for (int i = (int)a, _b = (int)(b); (i) < _b; ++i) #define RANGE_R(i, a, b) for (int i = (int)b - 1, _a = (int)(a); (i) >= _a; --i) #define MIN_UPDATE(target, value) target = min(target, value) #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define ALL(c) (c).begin(), (c).end() #define ALLR(c) (c).rbegin(), (c).rend() #define PB push_back #define MP(a, b) make_pair(a, b) #define POPCOUNT(v) __builtin_popcountll((ll)(v)) #define IN_RANGE(v, a, b) ((a) <= (v) && (v) < (b)) #define CLEAR(table, v) memset(table, v, sizeof(table)); #define PRINT1(table, D0) \ REP(d0, D0) cout << table[d0] << " "; \ cout << "\n"; #define PRINT2(table, D0, D1) \ REP(d0, D0) { \ REP(d1, D1) cout << table[d0][d1] << " "; \ cout << "\n"; \ } #define PRINT3(table, D0, D1, D2) \ REP(d0, D0) { \ REP(d1, D1) { \ REP(d2, D2) cout << table[d0][d1][d2] << " "; \ cout << "\n"; \ } \ cout << "\n"; \ } template <typename T0, typename T1> std::ostream &operator<<(std::ostream &os, const map<T0, T1> &v) { for (typename map<T0, T1>::const_iterator p = v.begin(); p != v.end(); p++) { os << p->first << ": " << p->second << " "; } return os; } template <typename T0, typename T1> std::ostream &operator<<(std::ostream &os, const pair<T0, T1> &v) { os << v.first << ": " << v.second << " "; return os; } template <typename T> std::ostream &operator<<(std::ostream &os, const vector<T> &v) { for (int i = 0; i < (int)v.size(); i++) { os << v[i] << " "; } return os; } template <typename T> std::ostream &operator<<(std::ostream &os, const set<T> &v) { vector<T> tmp(v.begin(), v.end()); os << tmp; return os; } template <typename T> std::ostream &operator<<(std::ostream &os, const deque<T> &v) { vector<T> tmp(v.begin(), v.end()); os << tmp; return os; } template <typename T> std::ostream &operator<<(std::ostream &os, const vector<vector<T>> &v) { for (int i = 0; i < (int)v.size(); i++) { os << v[i] << endl; } return os; } /* */ int main() { ll N; while (1) { cin >> N; if (!N) break; VI w(N); REP(i, N) cin >> w[i]; double minX = 0, maxX = 0; vector<double> x(N + 1); REP(i, N) { REP(j, i) { double X = x[j] + sqrt(pow(w[j] + w[i], 2) - pow(fabs(w[j] - w[i]), 2)); x[i] = max(x[i], X); } minX = min(minX, x[i] - w[i]); maxX = max(maxX, x[i] + w[i]); } printf("%.10f\n", maxX - minX); } return 0; }
#include <algorithm> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <vector> #define _USE_MATH_DEFINES #include <cassert> #include <cctype> #include <cfloat> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> using namespace std; #define EPS 1e-12 #define ull unsigned long long #define ll long long #define VI vector<ll> #define PII pair<ll, ll> #define VVI vector<vector<ll>> #define REP(i, n) for (int i = 0, _n = (n); (i) < (int)_n; ++i) #define RANGE(i, a, b) for (int i = (int)a, _b = (int)(b); (i) < _b; ++i) #define RANGE_R(i, a, b) for (int i = (int)b - 1, _a = (int)(a); (i) >= _a; --i) #define MIN_UPDATE(target, value) target = min(target, value) #define FOR(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define ALL(c) (c).begin(), (c).end() #define ALLR(c) (c).rbegin(), (c).rend() #define PB push_back #define MP(a, b) make_pair(a, b) #define POPCOUNT(v) __builtin_popcountll((ll)(v)) #define IN_RANGE(v, a, b) ((a) <= (v) && (v) < (b)) #define CLEAR(table, v) memset(table, v, sizeof(table)); #define PRINT1(table, D0) \ REP(d0, D0) cout << table[d0] << " "; \ cout << "\n"; #define PRINT2(table, D0, D1) \ REP(d0, D0) { \ REP(d1, D1) cout << table[d0][d1] << " "; \ cout << "\n"; \ } #define PRINT3(table, D0, D1, D2) \ REP(d0, D0) { \ REP(d1, D1) { \ REP(d2, D2) cout << table[d0][d1][d2] << " "; \ cout << "\n"; \ } \ cout << "\n"; \ } template <typename T0, typename T1> std::ostream &operator<<(std::ostream &os, const map<T0, T1> &v) { for (typename map<T0, T1>::const_iterator p = v.begin(); p != v.end(); p++) { os << p->first << ": " << p->second << " "; } return os; } template <typename T0, typename T1> std::ostream &operator<<(std::ostream &os, const pair<T0, T1> &v) { os << v.first << ": " << v.second << " "; return os; } template <typename T> std::ostream &operator<<(std::ostream &os, const vector<T> &v) { for (int i = 0; i < (int)v.size(); i++) { os << v[i] << " "; } return os; } template <typename T> std::ostream &operator<<(std::ostream &os, const set<T> &v) { vector<T> tmp(v.begin(), v.end()); os << tmp; return os; } template <typename T> std::ostream &operator<<(std::ostream &os, const deque<T> &v) { vector<T> tmp(v.begin(), v.end()); os << tmp; return os; } template <typename T> std::ostream &operator<<(std::ostream &os, const vector<vector<T>> &v) { for (int i = 0; i < (int)v.size(); i++) { os << v[i] << endl; } return os; } /* */ int main() { ll N; while (1) { cin >> N; if (!N) break; VI w(N); REP(i, N) cin >> w[i]; double minX = 0, maxX = 0; vector<double> x(N + 1); REP(i, N) { REP(j, i) { double X = x[j] + sqrt(pow(w[j] + w[i], 2) - pow(fabs(w[j] - w[i]), 2)); x[i] = max(x[i], X); } minX = min(minX, x[i] - w[i]); maxX = max(maxX, x[i] + w[i]); } printf("%.10f\n", maxX - minX); break; } return 0; }
insert
117
117
117
118
TLE
p00937
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int n, m, a[3]; vector<int> rG[50]; int d[50], c[50]; bool dp[101][50][50]; void init(int si) { dp[0][si][si] = true; for (int i = 0; i <= 100; i++) { for (int j = 0; j < n; j++) { if (!dp[i][si][j]) continue; for (int k = 0; k < (int)rG[j].size(); k++) { int w = rG[j][k]; dp[i + 1][si][w] = true; } } } } void solve() { memset(c, 0, sizeof(c)); memset(d, -1, sizeof(d)); queue<int> Q; Q.push(n - 1); d[n - 1] = 0; while (!Q.empty()) { int pos = Q.front(); Q.pop(); if (pos == 0) { cout << d[pos] << endl; return; } for (int i = 0; i < 3; i++) { for (int to = 0; to < n; to++) { if (!dp[a[i]][pos][to]) continue; c[to] |= (1 << i); if (c[to] == 7 && d[to] == -1) { d[to] = d[pos] + 1; Q.push(to); } } } } cout << "IMPOSSIBLE" << endl; } int main() { cin >> n >> m; for (int i = 0; i < 3; i++) cin >> a[i]; for (int i = 0; i < m; i++) { int from, to; cin >> from >> to; from--, to--; rG[to].push_back(from); } for (int i = 0; i < n; i++) init(i); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; int n, m, a[3]; vector<int> rG[50]; int d[50], c[50]; bool dp[101][50][50]; void init(int si) { dp[0][si][si] = true; for (int i = 0; i < 100; i++) { for (int j = 0; j < n; j++) { if (!dp[i][si][j]) continue; for (int k = 0; k < (int)rG[j].size(); k++) { int w = rG[j][k]; dp[i + 1][si][w] = true; } } } } void solve() { memset(c, 0, sizeof(c)); memset(d, -1, sizeof(d)); queue<int> Q; Q.push(n - 1); d[n - 1] = 0; while (!Q.empty()) { int pos = Q.front(); Q.pop(); if (pos == 0) { cout << d[pos] << endl; return; } for (int i = 0; i < 3; i++) { for (int to = 0; to < n; to++) { if (!dp[a[i]][pos][to]) continue; c[to] |= (1 << i); if (c[to] == 7 && d[to] == -1) { d[to] = d[pos] + 1; Q.push(to); } } } } cout << "IMPOSSIBLE" << endl; } int main() { cin >> n >> m; for (int i = 0; i < 3; i++) cin >> a[i]; for (int i = 0; i < m; i++) { int from, to; cin >> from >> to; from--, to--; rG[to].push_back(from); } for (int i = 0; i < n; i++) init(i); solve(); return 0; }
replace
10
11
10
11
0
p00937
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using vi = vector<int>; using vvi = vector<vi>; #define rep(i, n) for (int i = 0; i < n; i++) #define all(a) a.begin(), a.end() vvi matmul(vvi a, vvi b) { int n = a.size(); vvi d(n, vi(n, 0)); rep(i, n) rep(j, n) rep(k, n) d[i][j] |= a[i][k] & b[k][j]; return d; } static map<int, vvi> memo; vvi matpow(vvi a, int p) { return memo[p] = memo.find(p) != memo.end() ? p % 2 ? matmul(matpow(a, p - 1), a) : matmul(matpow(a, p / 2), matpow(a, p / 2)) : memo[p]; } int main() { int n, m, a[3]; while (cin >> n >> m >> a[0] >> a[1] >> a[2]) { vvi z(n, vi(n, 0)), e = z; rep(_, m) { int p, q; cin >> p >> q; e[p - 1][q - 1] = 1; } memo.clear(); memo[1] = e; vector<vvi> d(3); rep(p, 3) d[p] = matpow(e, a[p]); vi g(n, 0), ng(n, 0); g[n - 1] = 1; rep(t, 1000) { bool next = 0; fill(all(ng), 0); rep(i, n) if (!g[i]) { bool ok = 1, r; rep(p, 3)[&] { r = 0; rep(j, n) r |= (g[j] and d[p][i][j]); ok &= r; } (); if (ok) ng[i] = next = 1; } if (!next) { cout << "IMPOSSIBLE" << endl; break; } else if (ng[0]) { cout << t + 1 << endl; break; } rep(i, n) g[i] |= ng[i]; } } return 0; }
#include <bits/stdc++.h> using namespace std; using vi = vector<int>; using vvi = vector<vi>; #define rep(i, n) for (int i = 0; i < n; i++) #define all(a) a.begin(), a.end() vvi matmul(vvi a, vvi b) { int n = a.size(); vvi d(n, vi(n, 0)); rep(i, n) rep(j, n) rep(k, n) d[i][j] |= a[i][k] & b[k][j]; return d; } static map<int, vvi> memo; vvi matpow(vvi a, int p) { return memo[p] = memo.find(p) == memo.end() ? p % 2 ? matmul(matpow(a, p - 1), a) : matmul(matpow(a, p / 2), matpow(a, p / 2)) : memo[p]; } int main() { int n, m, a[3]; while (cin >> n >> m >> a[0] >> a[1] >> a[2]) { vvi z(n, vi(n, 0)), e = z; rep(_, m) { int p, q; cin >> p >> q; e[p - 1][q - 1] = 1; } memo.clear(); memo[1] = e; vector<vvi> d(3); rep(p, 3) d[p] = matpow(e, a[p]); vi g(n, 0), ng(n, 0); g[n - 1] = 1; rep(t, 1000) { bool next = 0; fill(all(ng), 0); rep(i, n) if (!g[i]) { bool ok = 1, r; rep(p, 3)[&] { r = 0; rep(j, n) r |= (g[j] and d[p][i][j]); ok &= r; } (); if (ok) ng[i] = next = 1; } if (!next) { cout << "IMPOSSIBLE" << endl; break; } else if (ng[0]) { cout << t + 1 << endl; break; } rep(i, n) g[i] |= ng[i]; } } return 0; }
replace
16
17
16
17
-11
p00937
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <vector> using namespace std; // Brute Force Attack! int main(int argc, char *argv[]) { int n, m, a, b, c; cin >> n >> m >> a >> b >> c; vector<int> abc = {a, b, c}; sort(abc.begin(), abc.end()); vector<vector<bool>> step(n, vector<bool>(n, false)); for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; step[u - 1][v - 1] = true; } a = abc[0]; b = abc[1]; c = abc[2]; vector<vector<bool>> arch(n, vector<bool>(n)); vector<vector<bool>> brch(n, vector<bool>(n)); vector<vector<bool>> crch(n, vector<bool>(n)); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { arch[i][j] = i == j; brch[i][j] = i == j; crch[i][j] = i == j; } } auto mul = [](int n, vector<vector<bool>> &z, vector<vector<bool>> &y) { bool t[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { bool c = false; for (int k = 0; k < n; k++) { c = c || (z[i][k] && y[k][j]); } t[i][j] = c; } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { z[i][j] = t[i][j]; } } }; // c is the biggest while (c != 0) { if (a & 1) { // arch *= step mul(n, arch, step); } if (b & 1) { // brch *= step mul(n, brch, step); } if (c & 1) { // crch *= step mul(n, crch, step); } mul(n, step, step); a >>= 1; b >>= 1; c >>= 1; } // arch: a-step reachable // brch: b-step reachable // crch: c-step reachable vector<int> ok(n, -1); ok[n - 1] = 0; // the goal is ok auto reachable = [&ok, &n](const vector<vector<bool>> &rch, int i) { for (int j = 0; j < n; j++) { if (rch[i][j] && (ok[j] >= 0)) return true; } return false; }; auto search = [&]() { bool done = false; int ss = 0; while (!done) { ss++; done = true; vector<int> ok2(n, -1); for (int i = 0; i < n; i++) { // if i has a-reachable ok, b-reachable ok, and c-reachable ok vertives, // then i is also ok if (ok[i] < 0 && reachable(arch, i) && reachable(brch, i) && reachable(crch, i)) { ok2[i] = ss; if (i == 0) { ok[i] = ok2[i]; return; } done = false; } } for (int i = 0; i < n; i++) { ok[i] = ok2[i]; } } }; search(); if (ok[0] < 0) cout << "IMPOSSIBLE" << endl; else cout << ok[0] << endl; return 0; }
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <vector> using namespace std; // Brute Force Attack! int main(int argc, char *argv[]) { int n, m, a, b, c; cin >> n >> m >> a >> b >> c; vector<int> abc = {a, b, c}; sort(abc.begin(), abc.end()); vector<vector<bool>> step(n, vector<bool>(n, false)); for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; step[u - 1][v - 1] = true; } a = abc[0]; b = abc[1]; c = abc[2]; vector<vector<bool>> arch(n, vector<bool>(n)); vector<vector<bool>> brch(n, vector<bool>(n)); vector<vector<bool>> crch(n, vector<bool>(n)); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { arch[i][j] = i == j; brch[i][j] = i == j; crch[i][j] = i == j; } } auto mul = [](int n, vector<vector<bool>> &z, vector<vector<bool>> &y) { bool t[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { bool c = false; for (int k = 0; k < n; k++) { c = c || (z[i][k] && y[k][j]); } t[i][j] = c; } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { z[i][j] = t[i][j]; } } }; // c is the biggest while (c != 0) { if (a & 1) { // arch *= step mul(n, arch, step); } if (b & 1) { // brch *= step mul(n, brch, step); } if (c & 1) { // crch *= step mul(n, crch, step); } mul(n, step, step); a >>= 1; b >>= 1; c >>= 1; } // arch: a-step reachable // brch: b-step reachable // crch: c-step reachable vector<int> ok(n, -1); ok[n - 1] = 0; // the goal is ok auto reachable = [&ok, &n](const vector<vector<bool>> &rch, int i) { for (int j = 0; j < n; j++) { if (rch[i][j] && (ok[j] >= 0)) return true; } return false; }; auto search = [&]() { bool done = false; int ss = 0; while (!done) { ss++; done = true; vector<int> ok2(n, -1); for (int i = 0; i < n; i++) { // if i has a-reachable ok, b-reachable ok, and c-reachable ok vertives, // then i is also ok if (ok[i] < 0 && reachable(arch, i) && reachable(brch, i) && reachable(crch, i)) { ok2[i] = ss; if (i == 0) { ok[i] = ok2[i]; return; } done = false; } } for (int i = 0; i < n; i++) { ok[i] = ok2[i] >= 0 ? ok2[i] : ok[i]; } } }; search(); if (ok[0] < 0) cout << "IMPOSSIBLE" << endl; else cout << ok[0] << endl; return 0; }
replace
101
102
101
102
TLE
p00937
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)n; i++) #define all(c) (c).begin(), (c).end() #define mp make_pair #define pb push_back #define each(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++) #define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl using namespace std; typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pi; const int inf = (int)1e9; const double INF = 1e12, EPS = 1e-9; int n, m, a, b, c; bool can[101][40][40]; int dp[50]; int main() { cin >> n >> m >> a >> b >> c; rep(i, m) { int x, y; cin >> x >> y; x--; y--; can[0][x][y] = 1; } rep(it, 100) rep(k, n) rep(i, n) rep(j, n) { if (can[it][i][k] && can[0][k][j]) can[it + 1][i][j] = 1; } rep(it, 101) { rep(i, n - 1) { int x = inf, y = inf, z = inf; rep(j, n) { if (can[a - 1][i][j]) x = min(x, dp[j] + 1); if (can[b - 1][i][j]) y = min(y, dp[j] + 1); if (can[c - 1][i][j]) z = min(z, dp[j] + 1); } dp[i] = max({dp[i], x, y, z}); } } if (dp[0] >= n) cout << "IMPOSSIBLE" << endl; else cout << dp[0] << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)n; i++) #define all(c) (c).begin(), (c).end() #define mp make_pair #define pb push_back #define each(i, c) \ for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++) #define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl using namespace std; typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pi; const int inf = (int)1e9; const double INF = 1e12, EPS = 1e-9; int n, m, a, b, c; bool can[101][50][50]; int dp[50]; int main() { cin >> n >> m >> a >> b >> c; rep(i, m) { int x, y; cin >> x >> y; x--; y--; can[0][x][y] = 1; } rep(it, 100) rep(k, n) rep(i, n) rep(j, n) { if (can[it][i][k] && can[0][k][j]) can[it + 1][i][j] = 1; } rep(it, 101) { rep(i, n - 1) { int x = inf, y = inf, z = inf; rep(j, n) { if (can[a - 1][i][j]) x = min(x, dp[j] + 1); if (can[b - 1][i][j]) y = min(y, dp[j] + 1); if (can[c - 1][i][j]) z = min(z, dp[j] + 1); } dp[i] = max({dp[i], x, y, z}); } } if (dp[0] >= n) cout << "IMPOSSIBLE" << endl; else cout << dp[0] << endl; return 0; }
replace
19
20
19
20
0
p00938
C++
Runtime Error
#include "bits/stdc++.h" #include <unordered_map> #include <unordered_set> #pragma warning(disable : 4996) using namespace std; using ld = long double; template <class T> using Table = vector<vector<T>>; const ld eps = 1e-9; //// < "D:\D_Download\Visual Studio ///2015\Projects\programing_contest_c++\Debug\a.txt" #include <complex> typedef long double ld; typedef complex<ld> Point; #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define ALL(x) (x).begin(), (x).end() const ld pi = acos(-1.0); const ld dtop = pi / 180.; const ld ptod = 1. / dtop; namespace std { bool operator<(const Point &lhs, const Point &rhs) { if (lhs.real() < rhs.real() - eps) return true; if (lhs.real() > rhs.real() + eps) return false; return lhs.imag() < rhs.imag(); } } // namespace std Point input_point() { ld x, y; cin >> x >> y; return Point(x, y); } bool eq(const ld a, const ld b) { return (abs(a - b) < eps); } ld dot(const Point &a, const Point &b) { return real(conj(a) * b); } ld cross(const Point &a, const Point &b) { return imag(conj(a) * b); } class Line { public: Point a, b; Line() : a(Point(0, 0)), b(Point(0, 0)) {} Line(Point a, Point b) : a(a), b(b) {} Point operator[](const int _num) const { if (_num == 0) return a; else if (_num == 1) return b; else { assert(false); return Point(); } } }; class Circle { public: Point p; ld r; Circle() : p(Point(0, 0)), r(0) {} Circle(Point p, ld r) : p(p), r(r) {} }; // CCW int ccw(const Point &a, const Point &b, const Point &c) { const Point nb(b - a); const Point nc(c - a); if (cross(nb, nc) > eps) return 1; // a,b,c if (cross(nb, nc) < -eps) return -1; // a, if (dot(nb, nc) < 0) return 2; // if (norm(nb) < norm(nc)) return -2; // return 0; } bool isis_ll(const Line &l, const Line &m) { return !eq(cross(l.b - l.a, m.b - m.a), 0); } bool isis_ls(const Line &l, const Line &s) { return isis_ll(l, s) && (cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < eps); } bool isis_ss(const Line &s, const Line &t) { return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 && ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0; } bool isis_lp(const Line &l, const Point &p) { return (abs(cross(l.b - p, l.a - p)) < eps); } bool isis_sp(const Line &s, const Point &p) { return (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps); } Point proj(const Line &l, const Point &p) { ld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b); return l.a + t * (l.a - l.b); } Point is_ll(const Line &s, const Line &t) { Point sv = s.b - s.a, tv = t.b - t.a; assert(cross(sv, tv) != 0); return s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv); } vector<Point> is_ll2(const Line &s, const Line &t) { Point sv = s.b - s.a, tv = t.b - t.a; if (cross(sv, tv) != 0) return vector<Point>(1, is_ll(s, t)); else { vector<Point> ans; for (int k = 0; k < 2; ++k) { if (isis_sp(s, t[k]) && find(ans.begin(), ans.end(), t[k]) == ans.end()) ans.push_back(t[k]); if (isis_sp(t, s[k]) && find(ans.begin(), ans.end(), s[k]) == ans.end()) ans.push_back(s[k]); } return ans; } } Point is_ss(const Line &s, const Line &t) { if (isis_ss(s, t)) { for (int k = 0; k < 2; ++k) { for (int l = 0; l < 2; ++l) { if (s[k] == t[l]) return s[k]; } } return is_ll(s, t); } else { assert(false); return Point(0, 0); } } ld dist_lp(const Line &l, const Point &p) { return abs(p - proj(l, p)); } ld dist_ll(const Line &l, const Line &m) { return isis_ll(l, m) ? 0 : dist_lp(l, m.a); } ld dist_ls(const Line &l, const Line &s) { return isis_ls(l, s) ? 0 : min(dist_lp(l, s.a), dist_lp(l, s.b)); } ld dist_sp(const Line &s, const Point &p) { Point r = proj(s, p); return isis_sp(s, r) ? abs(r - p) : min(abs(s.a - p), abs(s.b - p)); } ld dist_ss(const Line &s, const Line &t) { if (isis_ss(s, t)) return 0; return min( {dist_sp(s, t.a), dist_sp(s, t.b), dist_sp(t, s.a), dist_sp(t, s.b)}); } Line bisection(const Line &s, const Line &t) { const Point laglanju(is_ll(s, t)); const Point avec = !(abs(laglanju - s[0]) < eps) ? s[0] - laglanju : s[1] - laglanju; const Point bvec = !(abs(laglanju - t[0]) < eps) ? t[0] - laglanju : t[1] - laglanju; return Line(laglanju, laglanju + (abs(bvec) * avec + abs(avec) * bvec) / (abs(avec) + abs(bvec))); } Point inner_center(const vector<Line> &ls) { vector<Point> vertics; for (int i = 0; i < static_cast<int>(ls.size()); ++i) { vertics.push_back(is_ll(ls[i], ls[(i + 1) % 3])); } if (vertics[0] == vertics[1] || vertics[1] == vertics[2] || vertics[2] == vertics[0]) return vertics[0]; Line bi1( bisection(Line(vertics[0], vertics[1]), Line(vertics[0], vertics[2]))); Line bi2( bisection(Line(vertics[1], vertics[2]), Line(vertics[1], vertics[0]))); if (bi1[0] == bi2[0]) return bi1[0]; else { return is_ll(bi1, bi2); } } vector<Point> ex_center(const vector<Line> &ls) { vector<Point> vertics; for (int i = 0; i < static_cast<int>(ls.size()); ++i) { vertics.push_back(is_ll(ls[i], ls[(i + 1) % 3])); } if (abs(vertics[0] - vertics[1]) < eps || abs(vertics[1] - vertics[2]) < eps || (abs(vertics[2] - vertics[0]) < eps)) return vector<Point>(); vector<Point> ecs; for (int i = 0; i < 3; ++i) { Line bi1( bisection(Line(vertics[i], vertics[i] * 2.0l - vertics[(i + 2) % 3]), Line(vertics[i], vertics[(i + 1) % 3]))); Line bi2(bisection(Line(vertics[(i + 1) % 3], vertics[(i + 1) % 3] * 2.0l - vertics[(i + 2) % 3]), Line(vertics[(i + 1) % 3], vertics[i]))); ecs.push_back(is_ll(bi1, bi2)); } return ecs; } vector<Point> same_dis(const vector<Line> &ls) { vector<Point> vertics; vertics.push_back(is_ll(ls[0], ls[2])); vertics.push_back(is_ll(ls[1], ls[2])); if (abs(vertics[0] - vertics[1]) < eps) return vector<Point>{vertics[0]}; Line bis(bisection(ls[0], ls[1])); vector<Point> ecs; Line abi(bisection(Line(vertics[0], vertics[1]), ls[0])); ecs.push_back(is_ll(bis, abi)); Line bbi(bisection(Line(vertics[0], 2.l * vertics[0] - vertics[1]), ls[0])); ecs.push_back(is_ll(bis, bbi)); return ecs; } vector<Point> is_cc(const Circle &c1, const Circle &c2) { vector<Point> res; ld d = abs(c1.p - c2.p); ld rc = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d); ld dfr = c1.r * c1.r - rc * rc; if (abs(dfr) < eps) dfr = 0.0; else if (dfr < 0.0) return res; // no intersection ld rs = sqrt(dfr); Point diff = (c2.p - c1.p) / d; res.push_back(c1.p + diff * Point(rc, rs)); if (dfr != 0.0) res.push_back(c1.p + diff * Point(rc, -rs)); return res; } int is_in_circle(const Circle &cir, const Point &p) { ld dis = abs(cir.p - p); if (dis > cir.r + eps) return 0; else if (dis < cir.r - eps) return 2; else return 1; } int circle_in_circle(const Circle &lc, const Circle &rc) { ld dis = abs(lc.p - rc.p); if (dis < rc.r - lc.r - eps) return 2; else if (dis > rc.r - lc.r + eps) return 0; else return 1; } vector<Point> is_lc(const Circle &c, const Line &l) { vector<Point> res; ld d = dist_lp(l, c.p); if (d < c.r + eps) { ld len = (d > c.r) ? 0.0 : sqrt(c.r * c.r - d * d); // safety; Point nor = (l.a - l.b) / abs(l.a - l.b); res.push_back(proj(l, c.p) + len * nor); res.push_back(proj(l, c.p) - len * nor); } return res; } vector<Point> is_sc(const Circle &c, const Line &l) { vector<Point> v = is_lc(c, l), res; for (Point p : v) if (isis_sp(l, p)) res.push_back(p); return res; } vector<Line> tangent_cp(const Circle &c, const Point &p) { vector<Line> ret; Point v = c.p - p; ld d = abs(v); ld l = sqrt(norm(v) - c.r * c.r); if (isnan(l)) { return ret; } Point v1 = v * Point(l / d, c.r / d); Point v2 = v * Point(l / d, -c.r / d); ret.push_back(Line(p, p + v1)); if (l < eps) return ret; ret.push_back(Line(p, p + v2)); return ret; } vector<Line> tangent_cc(const Circle &c1, const Circle &c2) { vector<Line> ret; if (abs(c1.p - c2.p) - (c1.r + c2.r) > -eps) { Point center = (c1.p * c2.r + c2.p * c1.r) / (c1.r + c2.r); ret = tangent_cp(c1, center); } if (abs(c1.r - c2.r) > eps) { Point out = (-c1.p * c2.r + c2.p * c1.r) / (c1.r - c2.r); vector<Line> nret = tangent_cp(c1, out); ret.insert(ret.end(), ALL(nret)); } else { Point v = c2.p - c1.p; v /= abs(v); Point q1 = c1.p + v * Point(0, 1) * c1.r; Point q2 = c1.p + v * Point(0, -1) * c1.r; ret.push_back(Line(q1, q1 + v)); ret.push_back(Line(q2, q2 + v)); } return ret; } ld two_circle_area(const Circle &l, const Circle &r) { ld dis = abs(l.p - r.p); if (dis > l.r + r.r) return 0; else if (dis + r.r < l.r) { return r.r * r.r * pi; } else if (dis + l.r < r.r) { return l.r * l.r * pi; } else { ld ans = (l.r) * (l.r) * acos((dis * dis + l.r * l.r - r.r * r.r) / (2 * dis * l.r)) + (r.r) * (r.r) * acos((dis * dis + r.r * r.r - l.r * l.r) / (2 * dis * r.r)) - sqrt(4 * dis * dis * l.r * l.r - (dis * dis + l.r * l.r - r.r * r.r) * (dis * dis + l.r * l.r - r.r * r.r)) / 2; return ans; } } typedef vector<Point> Polygon; ld area(const Polygon &p) { ld res = 0; int n = p.size(); REP(j, n) res += cross(p[j], p[(j + 1) % n]); return res / 2; } bool is_counter_clockwise(const Polygon &poly) { ld angle = 0; int n = poly.size(); REP(i, n) { Point a = poly[i], b = poly[(i + 1) % n], c = poly[(i + 2) % n]; angle += arg((c - b) / (b - a)); } return angle > eps; } int is_in_polygon(const Polygon &poly, const Point &p) { ld angle = 0; int n = poly.size(); REP(i, n) { Point a = poly[i], b = poly[(i + 1) % n]; if (isis_sp(Line(a, b), p)) return 1; angle += arg((b - p) / (a - p)); } return eq(angle, 0) ? 0 : 2; } enum { OUT, ON, IN }; int convex_contains(const Polygon &P, const Point &p) { const int n = P.size(); Point g = (P[0] + P[n / 3] + P[2 * n / 3]) / 3.0l; // inner-point int a = 0, b = n; while (a + 1 < b) { // invariant: c is in fan g-P[a]-P[b] int c = (a + b) / 2; if (cross(P[a] - g, P[c] - g) > 0) { // angle < 180 deg if (cross(P[a] - g, p - g) > 0 && cross(P[c] - g, p - g) < 0) b = c; else a = c; } else { if (cross(P[a] - g, p - g) < 0 && cross(P[c] - g, p - g) > 0) a = c; else b = c; } } b %= n; if (cross(P[a] - p, P[b] - p) < 0) return 0; if (cross(P[a] - p, P[b] - p) > 0) return 2; return 1; } Polygon convex_hull(vector<Point> ps) { int n = ps.size(); int k = 0; sort(ps.begin(), ps.end()); Polygon ch(2 * n); for (int i = 0; i < n; ch[k++] = ps[i++]) while (k >= 2 && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k; for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = ps[i--]) while (k >= t && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k; ch.resize(k - 1); return ch; } vector<Polygon> convex_cut(const Polygon &ps, const Line &l) { int n = ps.size(); Polygon Q; Polygon R; REP(i, n) { Point A = ps[i], B = ps[(i + 1) % n]; Line m = Line(A, B); if (ccw(l.a, l.b, A) != -1) Q.push_back(A); if (ccw(l.a, l.b, A) != 1) R.push_back(A); if (ccw(l.a, l.b, A) * ccw(l.a, l.b, B) < 0 && isis_ll(l, m)) { Q.push_back(is_ll(l, m)); R.push_back(is_ll(l, m)); } } const vector<Polygon> polys{Q, R}; return polys; } void add_point(vector<Point> &ps, const Point p) { for (Point q : ps) if (abs(q - p) < eps) return; ps.push_back(p); } typedef int Weight; struct Edge { int src, dst; Weight weight; Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {} }; typedef vector<Edge> Edges; typedef vector<Edges> Graph; void add_edge(Graph &g, const int from, const int to, const Weight weight) { g[from].push_back(Edge{from, to, weight}); } Graph segment_arrangement(const vector<Line> &s, const vector<Point> &p) { int n = p.size(), m = s.size(); Graph g(n); REP(i, m) { vector<pair<ld, int>> vec; REP(j, n) if (isis_sp(s[i], p[j])) vec.emplace_back(abs(s[i].a - p[j]), j); sort(ALL(vec)); REP(j, vec.size() - 1) { int from = vec[j].second, to = vec[j + 1].second; add_edge(g, from, to, static_cast<Weight>(abs(p[from] - p[to]))); } } return g; } Graph sennbunn_arrangement(const vector<Line> &s) { vector<Point> crss; for (int i = 0; i < static_cast<int>(s.size()); ++i) { for (int j = i + 1; j < static_cast<int>(s.size()); ++j) { if (isis_ss(s[i], s[j])) { crss.push_back(is_ll(s[i], s[j])); } } } for (int i = 0; i < static_cast<int>(s.size()); ++i) { crss.push_back(s[i][0]); crss.push_back(s[i][1]); } return segment_arrangement(s, crss); } // Graph circle_arrangement(const vector<Circle> &c, const vector<Point> &p) { // int n = p.size(), m = c.size(); // Graph g(n); // REP(i, m) { // vector<pair<ld, int>> vec; // REP(j, n) if (abs(abs(c[i].p - p[j]) - c[i].r) < eps) // vec.emplace_back(arg(c[i].p - p[j]), j); // sort(ALL(vec)); // REP(j, vec.size() - 1) { // int from = vec[j].second, to = vec[j + 1].second; // ld angle = vec[j + 1].first - vec[j].first; // add_edge(g, from, to, static_cast<Weight>(angle * //c[i].r)); // } // if (vec.size() >= 2) { // int from = vec.back().second, to = vec.front().first; // ld angle = vec.front().first - vec.back().first; // add_edge(g, from, to, static_cast<Weight>(angle * //c[i].r)); // } // } // return g; // } const int dx0[4] = {-1, 0, 1, 0}; const int dy0[4] = {0, 1, 0, -1}; const int dx[4] = {-1, -1, 1, 1}; const int dy[4] = {-1, 1, 1, -1}; int N, W, D; int getnum(int x, int y) { if (x == 0) { return y; } else if (y == D) { return D + x; } else if (x == W) { return 2 * D + W - y; } else { assert(!y); return 2 * D + 2 * W - x; } } bool isorder(const int a, const int b, const int c) { if (a < c) { return a <= b && b <= c; } else if (a > c) { return a <= b || b <= c; } else { return true; } } struct Compress { map<int, int> mp; map<int, int> revmp; Compress(vector<int> vs) { sort(vs.begin(), vs.end()); vs.erase(unique(vs.begin(), vs.end()), vs.end()); for (int i = 0; i < vs.size(); ++i) { mp[vs[i]] = i; revmp[i] = vs[i]; } } }; map<char, int> mp; int main() { mp['W'] = 0; mp['S'] = 1; mp['E'] = 2; mp['N'] = 3; cin >> N >> W >> D; if (W == 2) assert(false); vector<Line> ls; ls.push_back(Line(Point(0, 0), Point(0, D))); ls.push_back(Line(Point(0, D), Point(W, D))); ls.push_back(Line(Point(W, D), Point(W, 0))); ls.push_back(Line(Point(W, 0), Point(0, 0))); vector<pair<int, int>> ps; for (int i = 0; i < N; ++i) { int x, y; char f; cin >> x >> y >> f; y = D - y; Point p(x, y); int num1, num2; int betnum; { const int way = mp[f]; Line l(p, p + 1000000.l * Point(dx[way], dy[way])); for (int i = 0; i < 4; ++i) { if (isis_ss(l, ls[i])) { Point sect(is_ll(l, ls[i])); num1 = getnum(round(sect.real()), round(sect.imag())); break; } } } { const int way = (mp[f] + 1) % 4; Line l(p, p + 1000000.l * Point(dx[way], dy[way])); for (int i = 0; i < 4; ++i) { if (isis_ss(l, ls[i])) { Point sect(is_ll(l, ls[i])); num2 = getnum(round(sect.real()), round(sect.imag())); break; } } } { const int way = mp[f]; Line l(p, p + 1000000.l * Point(dx0[way], dy0[way])); for (int i = 0; i < 4; ++i) { if (isis_ss(l, ls[i])) { Point sect(is_ll(l, ls[i])); betnum = getnum(round(sect.real()), round(sect.imag())); break; } } } if (num1 < num2) { if (num1 < betnum && betnum < num2) { } else { swap(num1, num2); } } else { if (num2 < betnum && betnum < num1) { swap(num1, num2); } else { } } ps.emplace_back(num1, num2); } sort(ps.begin(), ps.end()); vector<int> v; for (auto p : ps) { v.emplace_back(p.first); v.emplace_back(p.second); } Compress comp(v); for (auto &p : ps) { p.first = comp.mp[p.first]; p.second = comp.mp[p.second]; } { bool a = is_sorted(ps.begin(), ps.end()); assert(a); } int _size = comp.mp.size(); int ans = 1e8; for (int start = 0; start < ps.size(); ++start) { int num = 1; const int anum = ps[start].first; queue<int> que; int amin = 1e8; for (int p = start; p < start + ps.size(); ++p) { const int from = ps[p % N].first; const int to = ps[p % N].second; if (isorder(from, anum, to)) continue; const int realfrom = from < anum ? from + _size : from; const int realto = to < anum ? to + _size : to; amin = min(amin, realto); if (amin < realfrom) { amin = realto; num++; } } if (amin != 1e8) num++; ans = min(ans, num); } cout << ans << endl; return 0; }
#include "bits/stdc++.h" #include <unordered_map> #include <unordered_set> #pragma warning(disable : 4996) using namespace std; using ld = long double; template <class T> using Table = vector<vector<T>>; const ld eps = 1e-9; //// < "D:\D_Download\Visual Studio ///2015\Projects\programing_contest_c++\Debug\a.txt" #include <complex> typedef long double ld; typedef complex<ld> Point; #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define ALL(x) (x).begin(), (x).end() const ld pi = acos(-1.0); const ld dtop = pi / 180.; const ld ptod = 1. / dtop; namespace std { bool operator<(const Point &lhs, const Point &rhs) { if (lhs.real() < rhs.real() - eps) return true; if (lhs.real() > rhs.real() + eps) return false; return lhs.imag() < rhs.imag(); } } // namespace std Point input_point() { ld x, y; cin >> x >> y; return Point(x, y); } bool eq(const ld a, const ld b) { return (abs(a - b) < eps); } ld dot(const Point &a, const Point &b) { return real(conj(a) * b); } ld cross(const Point &a, const Point &b) { return imag(conj(a) * b); } class Line { public: Point a, b; Line() : a(Point(0, 0)), b(Point(0, 0)) {} Line(Point a, Point b) : a(a), b(b) {} Point operator[](const int _num) const { if (_num == 0) return a; else if (_num == 1) return b; else { assert(false); return Point(); } } }; class Circle { public: Point p; ld r; Circle() : p(Point(0, 0)), r(0) {} Circle(Point p, ld r) : p(p), r(r) {} }; // CCW int ccw(const Point &a, const Point &b, const Point &c) { const Point nb(b - a); const Point nc(c - a); if (cross(nb, nc) > eps) return 1; // a,b,c if (cross(nb, nc) < -eps) return -1; // a, if (dot(nb, nc) < 0) return 2; // if (norm(nb) < norm(nc)) return -2; // return 0; } bool isis_ll(const Line &l, const Line &m) { return !eq(cross(l.b - l.a, m.b - m.a), 0); } bool isis_ls(const Line &l, const Line &s) { return isis_ll(l, s) && (cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < eps); } bool isis_ss(const Line &s, const Line &t) { return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 && ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0; } bool isis_lp(const Line &l, const Point &p) { return (abs(cross(l.b - p, l.a - p)) < eps); } bool isis_sp(const Line &s, const Point &p) { return (abs(s.a - p) + abs(s.b - p) - abs(s.b - s.a) < eps); } Point proj(const Line &l, const Point &p) { ld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b); return l.a + t * (l.a - l.b); } Point is_ll(const Line &s, const Line &t) { Point sv = s.b - s.a, tv = t.b - t.a; assert(cross(sv, tv) != 0); return s.a + sv * cross(tv, t.a - s.a) / cross(tv, sv); } vector<Point> is_ll2(const Line &s, const Line &t) { Point sv = s.b - s.a, tv = t.b - t.a; if (cross(sv, tv) != 0) return vector<Point>(1, is_ll(s, t)); else { vector<Point> ans; for (int k = 0; k < 2; ++k) { if (isis_sp(s, t[k]) && find(ans.begin(), ans.end(), t[k]) == ans.end()) ans.push_back(t[k]); if (isis_sp(t, s[k]) && find(ans.begin(), ans.end(), s[k]) == ans.end()) ans.push_back(s[k]); } return ans; } } Point is_ss(const Line &s, const Line &t) { if (isis_ss(s, t)) { for (int k = 0; k < 2; ++k) { for (int l = 0; l < 2; ++l) { if (s[k] == t[l]) return s[k]; } } return is_ll(s, t); } else { assert(false); return Point(0, 0); } } ld dist_lp(const Line &l, const Point &p) { return abs(p - proj(l, p)); } ld dist_ll(const Line &l, const Line &m) { return isis_ll(l, m) ? 0 : dist_lp(l, m.a); } ld dist_ls(const Line &l, const Line &s) { return isis_ls(l, s) ? 0 : min(dist_lp(l, s.a), dist_lp(l, s.b)); } ld dist_sp(const Line &s, const Point &p) { Point r = proj(s, p); return isis_sp(s, r) ? abs(r - p) : min(abs(s.a - p), abs(s.b - p)); } ld dist_ss(const Line &s, const Line &t) { if (isis_ss(s, t)) return 0; return min( {dist_sp(s, t.a), dist_sp(s, t.b), dist_sp(t, s.a), dist_sp(t, s.b)}); } Line bisection(const Line &s, const Line &t) { const Point laglanju(is_ll(s, t)); const Point avec = !(abs(laglanju - s[0]) < eps) ? s[0] - laglanju : s[1] - laglanju; const Point bvec = !(abs(laglanju - t[0]) < eps) ? t[0] - laglanju : t[1] - laglanju; return Line(laglanju, laglanju + (abs(bvec) * avec + abs(avec) * bvec) / (abs(avec) + abs(bvec))); } Point inner_center(const vector<Line> &ls) { vector<Point> vertics; for (int i = 0; i < static_cast<int>(ls.size()); ++i) { vertics.push_back(is_ll(ls[i], ls[(i + 1) % 3])); } if (vertics[0] == vertics[1] || vertics[1] == vertics[2] || vertics[2] == vertics[0]) return vertics[0]; Line bi1( bisection(Line(vertics[0], vertics[1]), Line(vertics[0], vertics[2]))); Line bi2( bisection(Line(vertics[1], vertics[2]), Line(vertics[1], vertics[0]))); if (bi1[0] == bi2[0]) return bi1[0]; else { return is_ll(bi1, bi2); } } vector<Point> ex_center(const vector<Line> &ls) { vector<Point> vertics; for (int i = 0; i < static_cast<int>(ls.size()); ++i) { vertics.push_back(is_ll(ls[i], ls[(i + 1) % 3])); } if (abs(vertics[0] - vertics[1]) < eps || abs(vertics[1] - vertics[2]) < eps || (abs(vertics[2] - vertics[0]) < eps)) return vector<Point>(); vector<Point> ecs; for (int i = 0; i < 3; ++i) { Line bi1( bisection(Line(vertics[i], vertics[i] * 2.0l - vertics[(i + 2) % 3]), Line(vertics[i], vertics[(i + 1) % 3]))); Line bi2(bisection(Line(vertics[(i + 1) % 3], vertics[(i + 1) % 3] * 2.0l - vertics[(i + 2) % 3]), Line(vertics[(i + 1) % 3], vertics[i]))); ecs.push_back(is_ll(bi1, bi2)); } return ecs; } vector<Point> same_dis(const vector<Line> &ls) { vector<Point> vertics; vertics.push_back(is_ll(ls[0], ls[2])); vertics.push_back(is_ll(ls[1], ls[2])); if (abs(vertics[0] - vertics[1]) < eps) return vector<Point>{vertics[0]}; Line bis(bisection(ls[0], ls[1])); vector<Point> ecs; Line abi(bisection(Line(vertics[0], vertics[1]), ls[0])); ecs.push_back(is_ll(bis, abi)); Line bbi(bisection(Line(vertics[0], 2.l * vertics[0] - vertics[1]), ls[0])); ecs.push_back(is_ll(bis, bbi)); return ecs; } vector<Point> is_cc(const Circle &c1, const Circle &c2) { vector<Point> res; ld d = abs(c1.p - c2.p); ld rc = (d * d + c1.r * c1.r - c2.r * c2.r) / (2 * d); ld dfr = c1.r * c1.r - rc * rc; if (abs(dfr) < eps) dfr = 0.0; else if (dfr < 0.0) return res; // no intersection ld rs = sqrt(dfr); Point diff = (c2.p - c1.p) / d; res.push_back(c1.p + diff * Point(rc, rs)); if (dfr != 0.0) res.push_back(c1.p + diff * Point(rc, -rs)); return res; } int is_in_circle(const Circle &cir, const Point &p) { ld dis = abs(cir.p - p); if (dis > cir.r + eps) return 0; else if (dis < cir.r - eps) return 2; else return 1; } int circle_in_circle(const Circle &lc, const Circle &rc) { ld dis = abs(lc.p - rc.p); if (dis < rc.r - lc.r - eps) return 2; else if (dis > rc.r - lc.r + eps) return 0; else return 1; } vector<Point> is_lc(const Circle &c, const Line &l) { vector<Point> res; ld d = dist_lp(l, c.p); if (d < c.r + eps) { ld len = (d > c.r) ? 0.0 : sqrt(c.r * c.r - d * d); // safety; Point nor = (l.a - l.b) / abs(l.a - l.b); res.push_back(proj(l, c.p) + len * nor); res.push_back(proj(l, c.p) - len * nor); } return res; } vector<Point> is_sc(const Circle &c, const Line &l) { vector<Point> v = is_lc(c, l), res; for (Point p : v) if (isis_sp(l, p)) res.push_back(p); return res; } vector<Line> tangent_cp(const Circle &c, const Point &p) { vector<Line> ret; Point v = c.p - p; ld d = abs(v); ld l = sqrt(norm(v) - c.r * c.r); if (isnan(l)) { return ret; } Point v1 = v * Point(l / d, c.r / d); Point v2 = v * Point(l / d, -c.r / d); ret.push_back(Line(p, p + v1)); if (l < eps) return ret; ret.push_back(Line(p, p + v2)); return ret; } vector<Line> tangent_cc(const Circle &c1, const Circle &c2) { vector<Line> ret; if (abs(c1.p - c2.p) - (c1.r + c2.r) > -eps) { Point center = (c1.p * c2.r + c2.p * c1.r) / (c1.r + c2.r); ret = tangent_cp(c1, center); } if (abs(c1.r - c2.r) > eps) { Point out = (-c1.p * c2.r + c2.p * c1.r) / (c1.r - c2.r); vector<Line> nret = tangent_cp(c1, out); ret.insert(ret.end(), ALL(nret)); } else { Point v = c2.p - c1.p; v /= abs(v); Point q1 = c1.p + v * Point(0, 1) * c1.r; Point q2 = c1.p + v * Point(0, -1) * c1.r; ret.push_back(Line(q1, q1 + v)); ret.push_back(Line(q2, q2 + v)); } return ret; } ld two_circle_area(const Circle &l, const Circle &r) { ld dis = abs(l.p - r.p); if (dis > l.r + r.r) return 0; else if (dis + r.r < l.r) { return r.r * r.r * pi; } else if (dis + l.r < r.r) { return l.r * l.r * pi; } else { ld ans = (l.r) * (l.r) * acos((dis * dis + l.r * l.r - r.r * r.r) / (2 * dis * l.r)) + (r.r) * (r.r) * acos((dis * dis + r.r * r.r - l.r * l.r) / (2 * dis * r.r)) - sqrt(4 * dis * dis * l.r * l.r - (dis * dis + l.r * l.r - r.r * r.r) * (dis * dis + l.r * l.r - r.r * r.r)) / 2; return ans; } } typedef vector<Point> Polygon; ld area(const Polygon &p) { ld res = 0; int n = p.size(); REP(j, n) res += cross(p[j], p[(j + 1) % n]); return res / 2; } bool is_counter_clockwise(const Polygon &poly) { ld angle = 0; int n = poly.size(); REP(i, n) { Point a = poly[i], b = poly[(i + 1) % n], c = poly[(i + 2) % n]; angle += arg((c - b) / (b - a)); } return angle > eps; } int is_in_polygon(const Polygon &poly, const Point &p) { ld angle = 0; int n = poly.size(); REP(i, n) { Point a = poly[i], b = poly[(i + 1) % n]; if (isis_sp(Line(a, b), p)) return 1; angle += arg((b - p) / (a - p)); } return eq(angle, 0) ? 0 : 2; } enum { OUT, ON, IN }; int convex_contains(const Polygon &P, const Point &p) { const int n = P.size(); Point g = (P[0] + P[n / 3] + P[2 * n / 3]) / 3.0l; // inner-point int a = 0, b = n; while (a + 1 < b) { // invariant: c is in fan g-P[a]-P[b] int c = (a + b) / 2; if (cross(P[a] - g, P[c] - g) > 0) { // angle < 180 deg if (cross(P[a] - g, p - g) > 0 && cross(P[c] - g, p - g) < 0) b = c; else a = c; } else { if (cross(P[a] - g, p - g) < 0 && cross(P[c] - g, p - g) > 0) a = c; else b = c; } } b %= n; if (cross(P[a] - p, P[b] - p) < 0) return 0; if (cross(P[a] - p, P[b] - p) > 0) return 2; return 1; } Polygon convex_hull(vector<Point> ps) { int n = ps.size(); int k = 0; sort(ps.begin(), ps.end()); Polygon ch(2 * n); for (int i = 0; i < n; ch[k++] = ps[i++]) while (k >= 2 && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k; for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = ps[i--]) while (k >= t && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) --k; ch.resize(k - 1); return ch; } vector<Polygon> convex_cut(const Polygon &ps, const Line &l) { int n = ps.size(); Polygon Q; Polygon R; REP(i, n) { Point A = ps[i], B = ps[(i + 1) % n]; Line m = Line(A, B); if (ccw(l.a, l.b, A) != -1) Q.push_back(A); if (ccw(l.a, l.b, A) != 1) R.push_back(A); if (ccw(l.a, l.b, A) * ccw(l.a, l.b, B) < 0 && isis_ll(l, m)) { Q.push_back(is_ll(l, m)); R.push_back(is_ll(l, m)); } } const vector<Polygon> polys{Q, R}; return polys; } void add_point(vector<Point> &ps, const Point p) { for (Point q : ps) if (abs(q - p) < eps) return; ps.push_back(p); } typedef int Weight; struct Edge { int src, dst; Weight weight; Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {} }; typedef vector<Edge> Edges; typedef vector<Edges> Graph; void add_edge(Graph &g, const int from, const int to, const Weight weight) { g[from].push_back(Edge{from, to, weight}); } Graph segment_arrangement(const vector<Line> &s, const vector<Point> &p) { int n = p.size(), m = s.size(); Graph g(n); REP(i, m) { vector<pair<ld, int>> vec; REP(j, n) if (isis_sp(s[i], p[j])) vec.emplace_back(abs(s[i].a - p[j]), j); sort(ALL(vec)); REP(j, vec.size() - 1) { int from = vec[j].second, to = vec[j + 1].second; add_edge(g, from, to, static_cast<Weight>(abs(p[from] - p[to]))); } } return g; } Graph sennbunn_arrangement(const vector<Line> &s) { vector<Point> crss; for (int i = 0; i < static_cast<int>(s.size()); ++i) { for (int j = i + 1; j < static_cast<int>(s.size()); ++j) { if (isis_ss(s[i], s[j])) { crss.push_back(is_ll(s[i], s[j])); } } } for (int i = 0; i < static_cast<int>(s.size()); ++i) { crss.push_back(s[i][0]); crss.push_back(s[i][1]); } return segment_arrangement(s, crss); } // Graph circle_arrangement(const vector<Circle> &c, const vector<Point> &p) { // int n = p.size(), m = c.size(); // Graph g(n); // REP(i, m) { // vector<pair<ld, int>> vec; // REP(j, n) if (abs(abs(c[i].p - p[j]) - c[i].r) < eps) // vec.emplace_back(arg(c[i].p - p[j]), j); // sort(ALL(vec)); // REP(j, vec.size() - 1) { // int from = vec[j].second, to = vec[j + 1].second; // ld angle = vec[j + 1].first - vec[j].first; // add_edge(g, from, to, static_cast<Weight>(angle * //c[i].r)); // } // if (vec.size() >= 2) { // int from = vec.back().second, to = vec.front().first; // ld angle = vec.front().first - vec.back().first; // add_edge(g, from, to, static_cast<Weight>(angle * //c[i].r)); // } // } // return g; // } const int dx0[4] = {-1, 0, 1, 0}; const int dy0[4] = {0, 1, 0, -1}; const int dx[4] = {-1, -1, 1, 1}; const int dy[4] = {-1, 1, 1, -1}; int N, W, D; int getnum(int x, int y) { if (x == 0) { return y; } else if (y == D) { return D + x; } else if (x == W) { return 2 * D + W - y; } else { assert(!y); return 2 * D + 2 * W - x; } } bool isorder(const int a, const int b, const int c) { if (a < c) { return a <= b && b <= c; } else if (a > c) { return a <= b || b <= c; } else { return true; } } struct Compress { map<int, int> mp; map<int, int> revmp; Compress(vector<int> vs) { sort(vs.begin(), vs.end()); vs.erase(unique(vs.begin(), vs.end()), vs.end()); for (int i = 0; i < vs.size(); ++i) { mp[vs[i]] = i; revmp[i] = vs[i]; } } }; map<char, int> mp; int main() { mp['W'] = 0; mp['S'] = 1; mp['E'] = 2; mp['N'] = 3; cin >> N >> W >> D; vector<Line> ls; ls.push_back(Line(Point(0, 0), Point(0, D))); ls.push_back(Line(Point(0, D), Point(W, D))); ls.push_back(Line(Point(W, D), Point(W, 0))); ls.push_back(Line(Point(W, 0), Point(0, 0))); vector<pair<int, int>> ps; for (int i = 0; i < N; ++i) { int x, y; char f; cin >> x >> y >> f; y = D - y; Point p(x, y); int num1, num2; int betnum; { const int way = mp[f]; Line l(p, p + 1000000.l * Point(dx[way], dy[way])); for (int i = 0; i < 4; ++i) { if (isis_ss(l, ls[i])) { Point sect(is_ll(l, ls[i])); num1 = getnum(round(sect.real()), round(sect.imag())); break; } } } { const int way = (mp[f] + 1) % 4; Line l(p, p + 1000000.l * Point(dx[way], dy[way])); for (int i = 0; i < 4; ++i) { if (isis_ss(l, ls[i])) { Point sect(is_ll(l, ls[i])); num2 = getnum(round(sect.real()), round(sect.imag())); break; } } } { const int way = mp[f]; Line l(p, p + 1000000.l * Point(dx0[way], dy0[way])); for (int i = 0; i < 4; ++i) { if (isis_ss(l, ls[i])) { Point sect(is_ll(l, ls[i])); betnum = getnum(round(sect.real()), round(sect.imag())); break; } } } if (num1 < num2) { if (num1 < betnum && betnum < num2) { } else { swap(num1, num2); } } else { if (num2 < betnum && betnum < num1) { swap(num1, num2); } else { } } ps.emplace_back(num1, num2); } sort(ps.begin(), ps.end()); vector<int> v; for (auto p : ps) { v.emplace_back(p.first); v.emplace_back(p.second); } Compress comp(v); for (auto &p : ps) { p.first = comp.mp[p.first]; p.second = comp.mp[p.second]; } { bool a = is_sorted(ps.begin(), ps.end()); assert(a); } int _size = comp.mp.size(); int ans = 1e8; for (int start = 0; start < ps.size(); ++start) { int num = 1; const int anum = ps[start].first; queue<int> que; int amin = 1e8; for (int p = start; p < start + ps.size(); ++p) { const int from = ps[p % N].first; const int to = ps[p % N].second; if (isorder(from, anum, to)) continue; const int realfrom = from < anum ? from + _size : from; const int realto = to < anum ? to + _size : to; amin = min(amin, realto); if (amin < realfrom) { amin = realto; num++; } } if (amin != 1e8) num++; ans = min(ans, num); } cout << ans << endl; return 0; }
delete
566
568
566
566
0
p00940
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; int N, M, K; int a[300]; int b[300][300]; int cnt[300]; int s[200000], t[200000]; int A[300]; int B[300][300]; bool check(int x) { for (int i = 0; i < M; i++) { A[i] = a[i]; } for (int i = 0; i < N; i++) { cnt[i] = 0; for (int j = 0; j < M; j++) { B[i][j] = b[i][j]; cnt[i] += b[i][j]; } } for (int i = 0; i < x; i++) { int p = s[i], q = t[i]; A[q]--; if (A[q] < 0) return false; B[p][q]--; cnt[p]--; if (cnt[p] == 0) { for (int j = 0; j < M; j++) A[j] += b[p][j]; } } bool update = true; while (update) { update = false; for (int i = 0; i < N; i++) { if (cnt[i] == 0) continue; bool flg = true; for (int j = 0; j < M; j++) if (A[j] < B[i][j]) flg = false; if (!flg) continue; cnt[i] = 0; for (int j = 0; j < M; j++) A[j] += (b[i][j] - B[i][j]); update = true; } } for (int i = 0; i < N; i++) { if (cnt[i] > 0) return false; assert(cnt[i] == 0); } return true; } int main() { scanf("%d %d %d", &N, &M, &K); for (int i = 0; i < M; i++) scanf("%d", &a[i]); for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) scanf("%d", &b[i][j]); for (int i = 0; i < K; i++) { scanf("%d %d", &s[i], &t[i]); s[i]--; t[i]--; } int left = 0, right = K, mid; while (left < right) { mid = (left + right) / 2; if (check(mid)) left = mid + 1; else right = mid; } if (check(K + 1)) left = -1; printf("%d\n", left); return 0; }
#include <bits/stdc++.h> using namespace std; int N, M, K; int a[300]; int b[300][300]; int cnt[300]; int s[200000], t[200000]; int A[300]; int B[300][300]; bool check(int x) { for (int i = 0; i < M; i++) { A[i] = a[i]; } for (int i = 0; i < N; i++) { cnt[i] = 0; for (int j = 0; j < M; j++) { B[i][j] = b[i][j]; cnt[i] += b[i][j]; } } for (int i = 0; i < x; i++) { int p = s[i], q = t[i]; A[q]--; if (A[q] < 0) return false; B[p][q]--; cnt[p]--; if (cnt[p] == 0) { for (int j = 0; j < M; j++) A[j] += b[p][j]; } } bool update = true; while (update) { update = false; for (int i = 0; i < N; i++) { if (cnt[i] == 0) continue; bool flg = true; for (int j = 0; j < M; j++) if (A[j] < B[i][j]) flg = false; if (!flg) continue; cnt[i] = 0; for (int j = 0; j < M; j++) A[j] += (b[i][j] - B[i][j]); update = true; } } for (int i = 0; i < N; i++) { if (cnt[i] > 0) return false; assert(cnt[i] == 0); } return true; } int main() { scanf("%d %d %d", &N, &M, &K); for (int i = 0; i < M; i++) scanf("%d", &a[i]); for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) scanf("%d", &b[i][j]); for (int i = 0; i < K; i++) { scanf("%d %d", &s[i], &t[i]); s[i]--; t[i]--; } int left = 0, right = K, mid; while (left < right) { mid = (left + right) / 2; if (check(mid)) left = mid + 1; else right = mid; } if (check(K)) left = -1; printf("%d\n", left); return 0; }
replace
89
90
89
90
0
p00946
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) #define MAX_N 40 #define INF 1000000000 #define MOD 1000000007 using namespace std; typedef long long ll; typedef pair<int, int> P; int n, m; vector<int> ans; bool saw[100001]; int main() { cin >> n >> m; rep(i, m) { int x; cin >> x; ans.push_back(x); } for (int i = ans.size() - 1; i >= 0; i--) { if (saw[ans[i]]) continue; cout << ans[i] << endl; saw[ans[i]] = true; } for (int i = 1; i <= n; i++) { if (saw[i]) continue; cout << i << endl; } }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) #define MAX_N 40 #define INF 1000000000 #define MOD 1000000007 using namespace std; typedef long long ll; typedef pair<int, int> P; int n, m; vector<int> ans; bool saw[200001]; int main() { cin >> n >> m; rep(i, m) { int x; cin >> x; ans.push_back(x); } for (int i = ans.size() - 1; i >= 0; i--) { if (saw[ans[i]]) continue; cout << ans[i] << endl; saw[ans[i]] = true; } for (int i = 1; i <= n; i++) { if (saw[i]) continue; cout << i << endl; } }
replace
11
12
11
12
0
p00946
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define rep(i, n) repl(i, 0, n) #define mp(a, b) make_pair(a, b) #define pb(a) push_back(a) #define all(x) (x).begin(), (x).end() #define dbg(x) cout << #x "=" << x << endl #define fi first #define se second #define INF 2147483600 int main() { int n, m; cin >> n >> m; vector<int> vec(n); rep(i, m) scanf("%d", &vec[i]); vector<bool> used(n + 1, false); for (int i = m - 1; i >= 0; i--) { if (!used[vec[i]]) printf("%d\n", vec[i]); used[vec[i]] = true; } for (int i = 1; i <= n; i++) if (!used[i]) { printf("%d\n", i); } return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; #define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define rep(i, n) repl(i, 0, n) #define mp(a, b) make_pair(a, b) #define pb(a) push_back(a) #define all(x) (x).begin(), (x).end() #define dbg(x) cout << #x "=" << x << endl #define fi first #define se second #define INF 2147483600 int main() { int n, m; cin >> n >> m; vector<int> vec(m); rep(i, m) scanf("%d", &vec[i]); vector<bool> used(n + 1, false); for (int i = m - 1; i >= 0; i--) { if (!used[vec[i]]) printf("%d\n", vec[i]); used[vec[i]] = true; } for (int i = 1; i <= n; i++) if (!used[i]) { printf("%d\n", i); } return 0; }
replace
29
30
29
30
0
p00946
C++
Runtime Error
#include <iostream> using namespace std; int n, m; int e[200000]; int main() { int i; cin >> n >> m; for (i = 0; i < m; i++) cin >> e[i]; bool used[100001] = {false}; for (i = m - 1; i >= 0; i--) { if (!used[e[i]]) { used[e[i]] = true; cout << e[i] << endl; } } for (i = 1; i <= n; i++) { if (!used[i]) cout << i << endl; } return 0; }
#include <iostream> using namespace std; int n, m; int e[200000]; int main() { int i; cin >> n >> m; for (i = 0; i < m; i++) cin >> e[i]; static bool used[200001] = {false}; for (i = m - 1; i >= 0; i--) { if (!used[e[i]]) { used[e[i]] = true; cout << e[i] << endl; } } for (i = 1; i <= n; i++) { if (!used[i]) cout << i << endl; } return 0; }
replace
13
14
13
14
0
p00946
C++
Runtime Error
#include "bits/stdc++.h" using namespace std; int main() { int n, m; cin >> n >> m; vector<int> used(n + 1, 0); vector<int> ans(m); for (int i = 0; i < m; i++) { int a; cin >> a; ans[i] = a; used[a] = 1; } vector<int> syu(m + 1, 0); for (int i = 0; i < m; i++) { if (syu[ans[m - i - 1]] != 1) { cout << ans[m - i - 1] << endl; syu[ans[m - i - 1]] = 1; } } for (int i = 1; i < n + 1; i++) { if (used[i] == 0) cout << i << endl; } return 0; }
#include "bits/stdc++.h" using namespace std; int main() { int n, m; cin >> n >> m; vector<int> used(n + 1, 0); vector<int> ans(m); for (int i = 0; i < m; i++) { int a; cin >> a; ans[i] = a; used[a] = 1; } vector<int> syu(n + 1, 0); for (int i = 0; i < m; i++) { if (syu[ans[m - i - 1]] != 1) { cout << ans[m - i - 1] << endl; syu[ans[m - i - 1]] = 1; } } for (int i = 1; i < n + 1; i++) { if (used[i] == 0) cout << i << endl; } return 0; }
replace
16
17
16
17
0
p00946
C++
Runtime Error
#include <algorithm> #include <climits> #include <cstdio> #include <cstring> #include <iostream> using namespace std; int N, M; //?度? n 的序列,有 m 个操作 int F[20500]; bool Jud[20500]; int main() { cin >> N >> M; memset(Jud, true, sizeof(Jud)); for (int i = 1; i <= M; i++) cin >> F[i]; for (int i = M; i >= 1; i--) if (Jud[F[i]] == true) { Jud[F[i]] = false; cout << F[i] << endl; } for (int i = 1; i <= N; i++) if (Jud[i] == true) cout << i << endl; return 0; }
#include <algorithm> #include <climits> #include <cstdio> #include <cstring> #include <iostream> using namespace std; int N, M; //?度? n 的序列,有 m 个操作 int F[205000]; bool Jud[205000]; int main() { cin >> N >> M; memset(Jud, true, sizeof(Jud)); for (int i = 1; i <= M; i++) cin >> F[i]; for (int i = M; i >= 1; i--) if (Jud[F[i]] == true) { Jud[F[i]] = false; cout << F[i] << endl; } for (int i = 1; i <= N; i++) if (Jud[i] == true) cout << i << endl; return 0; }
replace
8
10
8
10
0
p00946
C++
Runtime Error
#include <algorithm> #include <iostream> #include <vector> using namespace std; bool b[114514]; int n, m; vector<int> l; int main() { cin >> n >> m; for (int i = 1; i <= n; i++) b[i] = false; for (int i = 0; i < m; i++) { int a; cin >> a; l.push_back(a); } for (int i = 1; i <= m; i++) { if (!b[l[m - i]]) cout << l[m - i] << endl; b[l[m - i]] = true; } for (int i = 1; i <= n; i++) if (!b[i]) cout << i << endl; }
#include <algorithm> #include <iostream> #include <vector> using namespace std; bool b[229028]; int n, m; vector<int> l; int main() { cin >> n >> m; for (int i = 1; i <= n; i++) b[i] = false; for (int i = 0; i < m; i++) { int a; cin >> a; l.push_back(a); } for (int i = 1; i <= m; i++) { if (!b[l[m - i]]) cout << l[m - i] << endl; b[l[m - i]] = true; } for (int i = 1; i <= n; i++) if (!b[i]) cout << i << endl; }
replace
4
5
4
5
0
p00946
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <complex> #include <cstddef> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <fstream> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #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 RREP(i, a) for (int(i) = (a)-1; (i) >= 0; (i)--) #define FORR(i, a, b) for (int(i) = (a)-1; (i) >= (b); (i)--) #define DEBUG(C) cerr << #C << " = " << C << endl; using LL = long long; using VI = vector<int>; using VVI = vector<VI>; using VL = vector<LL>; using VVL = vector<VL>; using VD = vector<double>; using VVD = vector<VD>; using PII = pair<int, int>; using PDD = pair<double, double>; using PLL = pair<LL, LL>; using VPII = vector<PII>; template <typename T> using VT = vector<T>; #define ALL(a) begin((a)), end((a)) #define RALL(a) rbegin((a)), rend((a)) #define SORT(a) sort(ALL((a))) #define RSORT(a) sort(RALL((a))) #define REVERSE(a) reverse(ALL((a))) #define MP make_pair #define FORE(a, b) for (auto &&a : (b)) #define FIND(s, e) ((s).find(e) != (s).end()) #define EB emplace_back template <typename T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <typename T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } const int INF = 1e9; const int MOD = INF + 7; const LL LLINF = 1e18; const double EPS = 1e-9; FILE *fp; void init(); void last(); // #define MYLOCAL const int MAX = 100010; int n, m; bool used[MAX]; void init() { #ifdef MYLOCAL if ((fp = freopen("/Users/sekiya9311/Documents/comp_programming/" "comp_programming/in.txt", "r", stdin)) == NULL) { assert(false); } #endif memset(used, false, sizeof(used)); scanf("%d%d", &n, &m); stack<int> st; for (int i = n - 1; i >= 0; i--) { st.push(i + 1); } for (int i = 0; i < m; i++) { int e; scanf("%d", &e); st.push(e); } while (!st.empty()) { if (used[st.top()]) { st.pop(); continue; } used[st.top()] = true; printf("%d\n", st.top()); st.pop(); } last(); } void last() { #ifdef MYLOCAL fclose(fp); #endif exit(0); } void solve(int n) { last(); } signed main(void) { LL n, m, p, a, b, c, x, y, z, q; string s; bool f = false; init(); while (cin >> n, n) { solve(n); // return 0; } last(); }
#include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <complex> #include <cstddef> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <fstream> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #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 RREP(i, a) for (int(i) = (a)-1; (i) >= 0; (i)--) #define FORR(i, a, b) for (int(i) = (a)-1; (i) >= (b); (i)--) #define DEBUG(C) cerr << #C << " = " << C << endl; using LL = long long; using VI = vector<int>; using VVI = vector<VI>; using VL = vector<LL>; using VVL = vector<VL>; using VD = vector<double>; using VVD = vector<VD>; using PII = pair<int, int>; using PDD = pair<double, double>; using PLL = pair<LL, LL>; using VPII = vector<PII>; template <typename T> using VT = vector<T>; #define ALL(a) begin((a)), end((a)) #define RALL(a) rbegin((a)), rend((a)) #define SORT(a) sort(ALL((a))) #define RSORT(a) sort(RALL((a))) #define REVERSE(a) reverse(ALL((a))) #define MP make_pair #define FORE(a, b) for (auto &&a : (b)) #define FIND(s, e) ((s).find(e) != (s).end()) #define EB emplace_back template <typename T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <typename T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } const int INF = 1e9; const int MOD = INF + 7; const LL LLINF = 1e18; const double EPS = 1e-9; FILE *fp; void init(); void last(); // #define MYLOCAL const int MAX = 200010; int n, m; bool used[MAX]; void init() { #ifdef MYLOCAL if ((fp = freopen("/Users/sekiya9311/Documents/comp_programming/" "comp_programming/in.txt", "r", stdin)) == NULL) { assert(false); } #endif memset(used, false, sizeof(used)); scanf("%d%d", &n, &m); stack<int> st; for (int i = n - 1; i >= 0; i--) { st.push(i + 1); } for (int i = 0; i < m; i++) { int e; scanf("%d", &e); st.push(e); } while (!st.empty()) { if (used[st.top()]) { st.pop(); continue; } used[st.top()] = true; printf("%d\n", st.top()); st.pop(); } last(); } void last() { #ifdef MYLOCAL fclose(fp); #endif exit(0); } void solve(int n) { last(); } signed main(void) { LL n, m, p, a, b, c, x, y, z, q; string s; bool f = false; init(); while (cin >> n, n) { solve(n); // return 0; } last(); }
replace
81
82
81
82
0
p00948
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> using namespace std; struct Pos { int y, x; Pos() {} Pos(int y, int x) { this->y = y; this->x = x; } bool operator<(const Pos &r) const { return (y != r.y) ? y < r.y : x < r.x; } }; int n, m; Pos arm[100000]; int dp[2][100000]; //?§??????????(dir=0:???, dir=1:???), (?????£????????£???)??¢???????????????id, //??????????????????????????´???(0-indexed)????????? int dfs(int dir, int id) { int nextId; if (dir == 0) { nextId = upper_bound(arm, arm + m, Pos(arm[id].y - 1, arm[id].x)) - 1 - arm; if (nextId < 0 || arm[nextId].y != arm[id].y - 1) return arm[id].y; return dp[dir][id] = dfs(dir, nextId); } nextId = upper_bound(arm, arm + m, Pos(arm[id].y + 1, arm[id].x)) - 1 - arm; if (nextId < 0 || arm[nextId].y != arm[id].y + 1) return arm[id].y + 1; return dp[dir][id] = dfs(dir, nextId); } int main() { int i, j; cin >> n >> m; for (i = 0; i < m; i++) { cin >> arm[i].x >> arm[i].y; arm[i].y--; } sort(arm, arm + m); for (i = 0; i < 2; i++) for (j = 0; j < m; j++) dp[i][j] = -1; int id; for (i = 0; i < n; i++) { id = lower_bound(arm, arm + m, Pos(i, -1)) - 1 - arm; int low = ((id < 0 || arm[id].y != i - 1) ? i : dfs(0, id)); id = lower_bound(arm, arm + m, Pos(i + 1, -1)) - 1 - arm; int high = ((id < 0 || arm[id].y != i) ? i : dfs(1, id)); cout << high - low + 1; if (i + 1 < n) cout << " "; } cout << endl; return 0; }
#include <algorithm> #include <iostream> using namespace std; struct Pos { int y, x; Pos() {} Pos(int y, int x) { this->y = y; this->x = x; } bool operator<(const Pos &r) const { return (y != r.y) ? y < r.y : x < r.x; } }; int n, m; Pos arm[100000]; int dp[2][100000]; //?§??????????(dir=0:???, dir=1:???), (?????£????????£???)??¢???????????????id, //??????????????????????????´???(0-indexed)????????? int dfs(int dir, int id) { int nextId; if (dp[dir][id] != -1) { return dp[dir][id]; } if (dir == 0) { nextId = upper_bound(arm, arm + m, Pos(arm[id].y - 1, arm[id].x)) - 1 - arm; if (nextId < 0 || arm[nextId].y != arm[id].y - 1) return arm[id].y; return dp[dir][id] = dfs(dir, nextId); } nextId = upper_bound(arm, arm + m, Pos(arm[id].y + 1, arm[id].x)) - 1 - arm; if (nextId < 0 || arm[nextId].y != arm[id].y + 1) return arm[id].y + 1; return dp[dir][id] = dfs(dir, nextId); } int main() { int i, j; cin >> n >> m; for (i = 0; i < m; i++) { cin >> arm[i].x >> arm[i].y; arm[i].y--; } sort(arm, arm + m); for (i = 0; i < 2; i++) for (j = 0; j < m; j++) dp[i][j] = -1; int id; for (i = 0; i < n; i++) { id = lower_bound(arm, arm + m, Pos(i, -1)) - 1 - arm; int low = ((id < 0 || arm[id].y != i - 1) ? i : dfs(0, id)); id = lower_bound(arm, arm + m, Pos(i + 1, -1)) - 1 - arm; int high = ((id < 0 || arm[id].y != i) ? i : dfs(1, id)); cout << high - low + 1; if (i + 1 < n) cout << " "; } cout << endl; return 0; }
insert
22
22
22
26
TLE
p00949
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { string S, T; cin >> S >> T; if (S.length() > T.length()) { swap(S, T); } vector<vector<int>> memo(S.length() + 1, vector<int>(26)); for (int i = 0; i < S.length(); i++) { memo[i + 1] = memo[i]; memo[i + 1][S[i] - 'a']++; } vector<vector<int>> memo2(T.length() + 1, vector<int>(26)); for (int i = 0; i < T.length(); i++) { memo2[i + 1] = memo2[i]; memo2[i + 1][T[i] - 'a']++; } { bool f = false; for (int i = 0; i < 26; i++) { if (memo[S.length()][i] && memo2[T.length()][i]) { f = true; break; } } if (!f) { cout << 0 << endl; return 0; } } vector<int> now(26); int ans = 0; for (int i = 0; i < memo.size(); i++) { for (int j = memo.size() - 1; j > i + ans; j--) { for (int l = 0; l + j - i < memo2.size(); l++) { int m = l + j - i; bool f = true; for (int k = 0; k < 26; k++) { if (memo[j][k] - memo[i][k] - memo2[m][k] + memo2[l][k] != 0) { f = false; break; } } if (f) { ans = max(ans, j - i); break; } } } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int main() { string S, T; cin >> S >> T; if (S.length() > T.length()) { swap(S, T); } vector<vector<int>> memo(S.length() + 1, vector<int>(26)); for (int i = 0; i < S.length(); i++) { memo[i + 1] = memo[i]; memo[i + 1][S[i] - 'a']++; } vector<vector<int>> memo2(T.length() + 1, vector<int>(26)); for (int i = 0; i < T.length(); i++) { memo2[i + 1] = memo2[i]; memo2[i + 1][T[i] - 'a']++; } { bool f = false; for (int i = 0; i < 26; i++) { if (memo[S.length()][i] && memo2[T.length()][i]) { f = true; break; } } if (!f) { cout << 0 << endl; return 0; } } vector<int> now(26); int ans = 0; for (int len = S.length(); len >= 1; len--) { set<vector<int>> SVI; for (int i = 0; i + len < memo.size(); i++) { for (int k = 0; k < 26; k++) { now[k] = memo[i + len][k] - memo[i][k]; } SVI.insert(now); } for (int i = 0; i + len < memo2.size(); i++) { for (int k = 0; k < 26; k++) { now[k] = memo2[i + len][k] - memo2[i][k]; } if (SVI.count(now)) { cout << len << endl; return 0; } } } cout << ans << endl; }
replace
36
51
36
51
TLE
p00949
C++
Memory Limit Exceeded
#include <algorithm> #include <cstdio> #include <iostream> #include <map> #include <queue> #include <set> #include <vector> using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < (n); i++) const int INF = 1e9; set<vector<int>> m1; string s1, s2; vector<int> memo(26); int ans = 0; void hashh(void) { /* for (int i = 0; i < s1.size(); ++i){ rep(k, 26)memo[k] = 0; for (int j = i; j < s1.size(); ++j){ memo[s1[j] - 'a']++; m1.insert(memo); } rep(k, 26) memo[k] = 0; for (int l = 0; l < s2.size(); ++l){ rep(k, 26) memo[k] = 0; for (int m = l; m < s2.size(); ++m){ memo[s2[m] - 'a']++; if(m1.count(memo)){ ans = max(ans, m - l + 1); } } } m1.erase(m1.begin(), m1.end()); } */ for (int i = 1; i <= s1.size(); ++i) { //?????? rep(k, 26) memo[k] = 0; for (int j = 0; j < i; ++j) { memo[s1[j] - 'a']++; m1.insert(memo); } int l = 0; for (int r = i; r < s1.size(); ++r) { // printf("l %d r %d\n", l, r); memo[s1[r] - 'a']++; memo[s1[l] - 'a']--; m1.insert(memo); l++; } rep(k, 26) memo[k] = 0; for (int j = 0; j < i; ++j) { memo[s2[j] - 'a']++; } if (m1.count(memo)) { ans = max(ans, i); } l = 0; for (int r = i; r < s2.size(); ++r) { memo[s2[r] - 'a']++; memo[s2[l] - 'a']--; if (m1.count(memo)) { ans = max(ans, i); } l++; } } } int main(void) { cin >> s1 >> s2; if (s1.size() > s2.size()) { swap(s1, s2); } hashh(); printf("%d\n", ans); return 0; }
#include <algorithm> #include <cstdio> #include <iostream> #include <map> #include <queue> #include <set> #include <vector> using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < (n); i++) const int INF = 1e9; set<vector<int>> m1; string s1, s2; vector<int> memo(26); int ans = 0; void hashh(void) { /* for (int i = 0; i < s1.size(); ++i){ rep(k, 26)memo[k] = 0; for (int j = i; j < s1.size(); ++j){ memo[s1[j] - 'a']++; m1.insert(memo); } rep(k, 26) memo[k] = 0; for (int l = 0; l < s2.size(); ++l){ rep(k, 26) memo[k] = 0; for (int m = l; m < s2.size(); ++m){ memo[s2[m] - 'a']++; if(m1.count(memo)){ ans = max(ans, m - l + 1); } } } m1.erase(m1.begin(), m1.end()); } */ for (int i = 1; i <= s1.size(); ++i) { //?????? rep(k, 26) memo[k] = 0; for (int j = 0; j < i; ++j) { memo[s1[j] - 'a']++; m1.insert(memo); } int l = 0; for (int r = i; r < s1.size(); ++r) { // printf("l %d r %d\n", l, r); memo[s1[r] - 'a']++; memo[s1[l] - 'a']--; m1.insert(memo); l++; } rep(k, 26) memo[k] = 0; for (int j = 0; j < i; ++j) { memo[s2[j] - 'a']++; } if (m1.count(memo)) { ans = max(ans, i); } l = 0; for (int r = i; r < s2.size(); ++r) { memo[s2[r] - 'a']++; memo[s2[l] - 'a']--; if (m1.count(memo)) { ans = max(ans, i); } l++; } m1.erase(m1.begin(), m1.end()); } } int main(void) { cin >> s1 >> s2; if (s1.size() > s2.size()) { swap(s1, s2); } hashh(); printf("%d\n", ans); return 0; }
insert
71
71
71
72
MLE
p00950
C++
Runtime Error
#include <bits/stdc++.h> #define _overload(_1, _2, _3, name, ...) name #define _rep(i, n) _range(i, 0, n) #define _range(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) #define rep(...) _overload(__VA_ARGS__, _range, _rep, )(__VA_ARGS__) #define _rrep(i, n) _rrange(i, n, 0) #define _rrange(i, a, b) for (int i = (int)(a)-1; i >= (int)(b); --i) #define rrep(...) _overload(__VA_ARGS__, _rrange, _rrep, )(__VA_ARGS__) #define _all(arg) begin(arg), end(arg) #define uniq(arg) sort(_all(arg)), (arg).erase(unique(_all(arg)), end(arg)) #define getidx(ary, key) lower_bound(_all(ary), key) - begin(ary) #define clr(a, b) memset((a), (b), sizeof(a)) #define bit(n) (1LL << (n)) // #define DEBUG #ifdef DEBUG #define dump(...) fprintf(stderr, __VA_ARGS__) #else #define dump(...) #endif template <class T> bool chmax(T &a, const T &b) { return (a < b) ? (a = b, 1) : 0; } template <class T> bool chmin(T &a, const T &b) { return (b < a) ? (a = b, 1) : 0; } using namespace std; using ll = long long; using vi = vector<int>; using vll = vector<ll>; const double EPS = 1e-10; const double PI = acos(-1.0); const ll inf = 1LL << 62; const ll mod = 1000000007LL; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; ll extgcd(ll a, ll b, ll &x, ll &y) { x = 1, y = 0; ll g = a; if (b != 0) g = extgcd(b, a % b, y, x), y -= a / b * x; return g; } ll ADD(const ll &a, const ll &b, const ll &mod) { return (a + b) % mod; } ll SUB(const ll &a, const ll &b, const ll &mod) { return (a - b + mod) % mod; } ll MUL(const ll &a, const ll &b, const ll &mod) { return (1LL * a * b) % mod; } ll DIV(const ll &a, const ll &b, const ll &mod) { ll x, y; extgcd(b, mod, x, y); return MUL(a, (x + mod) % mod, mod); } random_device rd; mt19937 mt(rd()); uniform_int_distribution<int> dice(1, 6); uniform_real_distribution<double> score(0.0, 10.0); string f; int idx, len; int number(); int factor(); int term(); int expression(); int number() { char c = f[idx++]; if (c != '0' and c != '1') throw 1; if (c == '0') return 0; int ret = 1; while (idx < len and (f[idx] == '0' or f[idx] == '1')) { ret *= 2; char c = f[idx++]; if (c == '1') ret += 1; } return ret; } int factor() { int mcnt = 0; while (idx < len and f[idx] == '-') { mcnt++; idx++; } if (idx >= len) throw 1; int sign = (mcnt % 2 == 0 ? +1 : -1); if (f[idx] != '(') return sign * number(); idx++; int ret = sign * expression(); if (idx >= len or f[idx] != ')') throw 1; idx++; return ret; } int term() { int ret = factor(); while (idx < len and f[idx] == '*') { idx++; int rhs = factor(); ret *= rhs; } return ret; } int expression() { int ret = term(); while (idx < len and (f[idx] == '+' or f[idx] == '-')) { char op = f[idx++]; int rhs = term(); switch (op) { case '+': { ret += rhs; break; } case '-': { ret -= rhs; break; } } } return ret; } int main(void) { cin.tie(0); ios::sync_with_stdio(false); /* while(cin >> f){ len = f.size(), idx = 0; int ret = expression(); cerr << ret << ", idx = " << idx << ", len = " << len << endl; } return 0; //*/ string in; cin >> in; string T = "01+-*()="; sort(_all(T)); set<string> s; do { map<char, bool> used; map<char, char> c2t; int j = 0; f = in; rep(i, in.size()) { char c = in[i]; if (not isupper(c) and not islower(c)) continue; if (not used[c]) { used[c] = true; assert(j < T.size()); c2t[c] = T[j++]; } f[i] = c2t[c]; } string equation = f; string l, r; rep(i, in.size()) { if (f[i] == '=') { l = f.substr(0, i); r = f.substr(i + 1); break; } } if (l == "" or r == "") continue; try { idx = 0; f = l; len = f.size(); int lv = expression(); if (idx != len) continue; idx = 0; f = r; len = f.size(); int rv = expression(); if (idx != len) continue; if (lv != rv) continue; } catch (int e) { continue; } s.insert(equation); } while (next_permutation(_all(T))); // for(auto& e : s){ // cerr << e << endl; // } cout << s.size() << endl; return 0; }
#include <bits/stdc++.h> #define _overload(_1, _2, _3, name, ...) name #define _rep(i, n) _range(i, 0, n) #define _range(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) #define rep(...) _overload(__VA_ARGS__, _range, _rep, )(__VA_ARGS__) #define _rrep(i, n) _rrange(i, n, 0) #define _rrange(i, a, b) for (int i = (int)(a)-1; i >= (int)(b); --i) #define rrep(...) _overload(__VA_ARGS__, _rrange, _rrep, )(__VA_ARGS__) #define _all(arg) begin(arg), end(arg) #define uniq(arg) sort(_all(arg)), (arg).erase(unique(_all(arg)), end(arg)) #define getidx(ary, key) lower_bound(_all(ary), key) - begin(ary) #define clr(a, b) memset((a), (b), sizeof(a)) #define bit(n) (1LL << (n)) // #define DEBUG #ifdef DEBUG #define dump(...) fprintf(stderr, __VA_ARGS__) #else #define dump(...) #endif template <class T> bool chmax(T &a, const T &b) { return (a < b) ? (a = b, 1) : 0; } template <class T> bool chmin(T &a, const T &b) { return (b < a) ? (a = b, 1) : 0; } using namespace std; using ll = long long; using vi = vector<int>; using vll = vector<ll>; const double EPS = 1e-10; const double PI = acos(-1.0); const ll inf = 1LL << 62; const ll mod = 1000000007LL; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; ll extgcd(ll a, ll b, ll &x, ll &y) { x = 1, y = 0; ll g = a; if (b != 0) g = extgcd(b, a % b, y, x), y -= a / b * x; return g; } ll ADD(const ll &a, const ll &b, const ll &mod) { return (a + b) % mod; } ll SUB(const ll &a, const ll &b, const ll &mod) { return (a - b + mod) % mod; } ll MUL(const ll &a, const ll &b, const ll &mod) { return (1LL * a * b) % mod; } ll DIV(const ll &a, const ll &b, const ll &mod) { ll x, y; extgcd(b, mod, x, y); return MUL(a, (x + mod) % mod, mod); } random_device rd; mt19937 mt(rd()); uniform_int_distribution<int> dice(1, 6); uniform_real_distribution<double> score(0.0, 10.0); string f; int idx, len; int number(); int factor(); int term(); int expression(); int number() { char c = f[idx++]; if (c != '0' and c != '1') throw 1; if (c == '0') return 0; int ret = 1; while (idx < len and (f[idx] == '0' or f[idx] == '1')) { ret *= 2; char c = f[idx++]; if (c == '1') ret += 1; } return ret; } int factor() { int mcnt = 0; while (idx < len and f[idx] == '-') { mcnt++; idx++; } if (idx >= len) throw 1; int sign = (mcnt % 2 == 0 ? +1 : -1); if (f[idx] != '(') return sign * number(); idx++; int ret = sign * expression(); if (idx >= len or f[idx] != ')') throw 1; idx++; return ret; } int term() { int ret = factor(); while (idx < len and f[idx] == '*') { idx++; int rhs = factor(); ret *= rhs; } return ret; } int expression() { int ret = term(); while (idx < len and (f[idx] == '+' or f[idx] == '-')) { char op = f[idx++]; int rhs = term(); switch (op) { case '+': { ret += rhs; break; } case '-': { ret -= rhs; break; } } } return ret; } int main(void) { cin.tie(0); ios::sync_with_stdio(false); /* while(cin >> f){ len = f.size(), idx = 0; int ret = expression(); cerr << ret << ", idx = " << idx << ", len = " << len << endl; } return 0; //*/ string in; cin >> in; string T = "01+-*()="; sort(_all(T)); set<string> s; do { map<char, bool> used; map<char, char> c2t; int j = 0; f = in; rep(i, in.size()) { char c = in[i]; if (not isupper(c) and not islower(c)) continue; if (not used[c]) { used[c] = true; if (j >= T.size()) { cout << 0 << endl; return 0; } c2t[c] = T[j++]; } f[i] = c2t[c]; } string equation = f; string l, r; rep(i, in.size()) { if (f[i] == '=') { l = f.substr(0, i); r = f.substr(i + 1); break; } } if (l == "" or r == "") continue; try { idx = 0; f = l; len = f.size(); int lv = expression(); if (idx != len) continue; idx = 0; f = r; len = f.size(); int rv = expression(); if (idx != len) continue; if (lv != rv) continue; } catch (int e) { continue; } s.insert(equation); } while (next_permutation(_all(T))); // for(auto& e : s){ // cerr << e << endl; // } cout << s.size() << endl; return 0; }
replace
173
174
173
177
0
p00951
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using Int = long long; struct edge { int from, to; edge() {} edge(int from, int to) : from(from), to(to) {} }; const int MAX = 333; using BS = bitset<MAX>; vector<BS> G(MAX); vector<vector<int>> rG(MAX); vector<int> vis(MAX); void dfs(int v, int r) { if (vis[v]) return; vis[v] = 1; G[v] |= G[r]; for (int u : rG[v]) if (!vis[u]) dfs(u, r); }; signed main() { cin.tie(0); ios::sync_with_stdio(0); auto D = G; map<string, int> R; int sz = 0; { string p, q; cin >> p >> q; R[p] = sz++; R[q] = sz++; } int n; cin >> n; vector<vector<edge>> sG(n); vector<map<int, set<int>>> M(MAX); for (int i = 0; i < n; i++) { int m; cin >> m; for (int j = 0; j < m; j++) { string s, t; cin >> s >> t; if (!R.count(s)) R[s] = sz++; if (!R.count(t)) R[t] = sz++; sG[i].emplace_back(R[s], R[t]); M[R[s]][R[t]].insert(i); } } vector<int> used(n, 0); queue<edge> q; auto WF = [sz]() { for (int k = 0; k < sz; k++) for (int i = 0; i < sz; i++) for (int j = 0; j < sz; j++) G[i][j] = G[i][j] || (G[i][k] && G[k][j]); }; for (int i = 0; i < sz; i++) G[i][i] = 1; auto add_edge = [&](int from, int to) { rG[to].push_back(from); fill(vis.begin(), vis.end(), 0); dfs(to, to); }; auto add = [&](int x) { for (edge e : sG[x]) if (!D[e.from][e.to]) add_edge(e.from, e.to); }; q.emplace(0, 1); add_edge(0, 1); while (!q.empty()) { edge e = q.front(); q.pop(); if (D[e.from][e.to]) continue; D[e.from][e.to] = 1; for (int i : M[e.from][e.to]) { if (used[i]) continue; used[i] = 1; add(i); } if (q.empty()) { WF(); for (int i = 0; i < sz; i++) for (int j = 0; j < sz; j++) if (i != j && !D[i][j] && G[i][j]) q.emplace(i, j); } } for (int i = 0; i < sz; i++) G[i][i] = 0; WF(); for (int i = 0; i < sz; i++) { if (!G[i][i]) continue; cout << "No" << endl; return 0; } cout << "Yes" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using Int = long long; struct edge { int from, to; edge() {} edge(int from, int to) : from(from), to(to) {} }; const int MAX = 333; using BS = bitset<MAX>; vector<BS> G(MAX); vector<vector<int>> rG(MAX); vector<int> vis(MAX); void dfs(int v, int r) { if (vis[v]) return; vis[v] = 1; G[v] |= G[r]; for (int u : rG[v]) if (!vis[u]) dfs(u, r); }; signed main() { cin.tie(0); ios::sync_with_stdio(0); auto D = G; map<string, int> R; int sz = 0; { string p, q; cin >> p >> q; R[p] = sz++; R[q] = sz++; } int n; cin >> n; vector<vector<edge>> sG(n); vector<map<int, set<int>>> M(MAX); for (int i = 0; i < n; i++) { int m; cin >> m; for (int j = 0; j < m; j++) { string s, t; cin >> s >> t; if (!R.count(s)) R[s] = sz++; if (!R.count(t)) R[t] = sz++; sG[i].emplace_back(R[s], R[t]); M[R[s]][R[t]].insert(i); } } vector<int> used(n, 0); queue<edge> q; auto WF = [sz]() { for (int k = 0; k < sz; k++) for (int i = 0; i < sz; i++) for (int j = 0; j < sz; j++) G[i][j] = G[i][j] || (G[i][k] && G[k][j]); }; for (int i = 0; i < sz; i++) G[i][i] = 1; auto add_edge = [&](int from, int to) { rG[to].push_back(from); fill(vis.begin(), vis.end(), 0); dfs(to, to); }; auto add = [&](int x) { for (edge e : sG[x]) if (!D[e.from][e.to]) add_edge(e.from, e.to); }; q.emplace(0, 1); add_edge(0, 1); while (!q.empty()) { edge e = q.front(); q.pop(); if (D[e.from][e.to]) continue; D[e.from][e.to] = 1; for (int i : M[e.from][e.to]) { if (used[i]) continue; used[i] = 1; add(i); } if (q.empty()) { for (int i = 0; i < sz; i++) for (int j = 0; j < sz; j++) if (i != j && !D[i][j] && G[i][j]) q.emplace(i, j); } } for (int i = 0; i < sz; i++) G[i][i] = 0; WF(); for (int i = 0; i < sz; i++) { if (!G[i][i]) continue; cout << "No" << endl; return 0; } cout << "Yes" << endl; return 0; }
delete
98
99
98
98
TLE
p00952
C++
Runtime Error
#define _CRT_SECURE_NO_WARNINGS #include <algorithm> #include <array> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <string> #include <tuple> #include <unordered_set> #include <vector> using namespace std; #define INF (1 << 29) #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(v) v.begin(), v.end() #define uniq(v) v.erase(unique(all(v)), v.end()) #define MAXN 500000 bitset<MAXN + 1 + 1> b; bool full[MAXN + 1 + 1]; int lowest = -1; bool ok(int depth) { if (b[0]) return false; return depth > MAXN || !full[depth] || depth >= lowest; } void add(int depth) { lowest = max(lowest, depth); if (depth >= MAXN) return; while (1) { if (b[depth]) { b.reset(depth--); } else { b.set(depth); break; } } if (!b[lowest]) lowest = depth; if (depth == 0) return; while (full[depth - 1] && !full[depth] && b[depth]) { full[depth++] = true; } } int main() { ios::sync_with_stdio(0); cin.tie(0); full[0] = true; int n; cin >> n; rep(i, n) { int x; cin >> x; if (ok(x)) { add(x); cout << "Yes" << endl; } else { cout << "No" << endl; } } return 0; }
#define _CRT_SECURE_NO_WARNINGS #include <algorithm> #include <array> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <string> #include <tuple> #include <unordered_set> #include <vector> using namespace std; #define INF (1 << 29) #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(v) v.begin(), v.end() #define uniq(v) v.erase(unique(all(v)), v.end()) #define MAXN 500000 bitset<MAXN + 1 + 1> b; bool full[MAXN + 1 + 1]; int lowest = -1; bool ok(int depth) { if (b[0]) return false; return depth > MAXN || !full[depth] || depth >= lowest; } void add(int depth) { lowest = max(lowest, depth); if (depth >= MAXN) return; while (1) { if (b[depth]) { b.reset(depth--); } else { b.set(depth); break; } } if (lowest <= MAXN && !b[lowest]) lowest = depth; if (depth == 0) return; while (full[depth - 1] && !full[depth] && b[depth]) { full[depth++] = true; } } int main() { ios::sync_with_stdio(0); cin.tie(0); full[0] = true; int n; cin >> n; rep(i, n) { int x; cin >> x; if (ok(x)) { add(x); cout << "Yes" << endl; } else { cout << "No" << endl; } } return 0; }
replace
52
53
52
53
0
p00952
C++
Time Limit Exceeded
#include "bits/stdc++.h" using namespace std; int b[500000], p = 1, cnt = 0; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { int x; cin >> x; if (b[0] == 1 || x < p - 1) { cout << "No" << endl; continue; } if (p - 1 == x && cnt > x) { cout << "No" << endl; continue; } cnt++; cout << "Yes" << endl; if (x >= n) continue; b[x]++; int j = x; while (j > 0 && b[j] >= 2) b[j] -= 2, b[j - 1]++, j--, cnt--; p = 1; while (p < n && b[p] == 1) p++; } }
#include "bits/stdc++.h" using namespace std; int b[500000], p = 1, cnt = 0; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { int x; cin >> x; if (b[0] == 1 || x < p - 1) { cout << "No" << endl; continue; } if (p - 1 == x && cnt > x) { cout << "No" << endl; continue; } cnt++; cout << "Yes" << endl; if (x >= n) continue; b[x]++; while (x > 0 && b[x] >= 2) b[x] -= 2, b[x - 1]++, x--, cnt--; while (p < n && b[p] == 1) p++; } }
replace
22
26
22
24
TLE
p00953
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int INF = 1 << 29; struct UnionFind { vector<int> data; UnionFind(int sz) { data.assign(sz, -1); } int find(int k) { return (data[k] < 0 ? k : data[k] = find(data[k])); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (data[x] > data[y]) swap(x, y); data[x] += data[y]; data[y] = x; } }; vector<pair<int, int>> g[100001]; vector<int> leftt[100001], rightt[100001], just[100001]; vector<int> gg[100001]; int deg[100001]; int rec(int idx, int back = -1) { if (back != -1) { int tmp = max(leftt[idx][back], rightt[idx][back + 1]); if (tmp != INF) return (tmp); } if (rightt[idx][0] == INF) { for (int i = 0; i < g[idx].size(); i++) { int to, rev; tie(to, rev) = g[idx][i]; if (i == back || just[idx][i] != INF) continue; just[idx][i] = rec(to, rev) + 1; } for (int i = 0; i < g[idx].size(); i++) { leftt[idx][i + 1] = max(leftt[idx][i], just[idx][i]); } for (int i = (int)g[idx].size() - 1; i >= 0; i--) { rightt[idx][i] = max(rightt[idx][i + 1], just[idx][i]); } } if (back == -1) return (rightt[idx][0]); return (max(leftt[idx][back], rightt[idx][back + 1])); } int main() { int N, M; vector<pair<int, int>> edges, arcs; scanf("%d %d", &N, &M); UnionFind uf(N); for (int i = 0; i < M; i++) { int x, y, t; cin >> x >> y >> t; --x, --y; if (t == 1) { arcs.emplace_back(x, y); g[x].emplace_back(y, -1); } else { if (uf.find(x) == uf.find(y)) { cout << "Infinite" << endl; return (0); } uf.unite(x, y); edges.emplace_back(x, y); g[y].emplace_back(x, g[x].size()); g[x].emplace_back(y, g[y].size() - 1); } } { for (auto &e : arcs) { gg[uf.find(e.first)].push_back(uf.find(e.second)); ++deg[uf.find(e.second)]; } vector<int> order; for (int i = 0; i < N; i++) { if (deg[i] == 0) order.push_back(i); } for (int i : order) { for (auto &e : gg[i]) { if (--deg[e] == 0) order.push_back(e); } } if (order.size() != N) { cout << "Infinite" << endl; return (0); } } int ret = 0; for (int i = 0; i < N; i++) { int sz = g[i].size(); just[i].assign(sz, INF); leftt[i].assign(sz + 1, INF); rightt[i].assign(sz + 1, INF); leftt[i][0] = rightt[i][sz] = 0; } for (int i = 0; i < N; i++) ret = max(ret, rec(i)); cout << ret << endl; }
#include <bits/stdc++.h> using namespace std; const int INF = 1 << 29; struct UnionFind { vector<int> data; UnionFind(int sz) { data.assign(sz, -1); } int find(int k) { return (data[k] < 0 ? k : data[k] = find(data[k])); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (data[x] > data[y]) swap(x, y); data[x] += data[y]; data[y] = x; } }; vector<pair<int, int>> g[100001]; vector<int> leftt[100001], rightt[100001], just[100001]; vector<int> gg[100001]; int deg[100001]; int rec(int idx, int back = -1) { if (back != -1) { int tmp = max(leftt[idx][back], rightt[idx][back + 1]); if (tmp != INF) return (tmp); } if (rightt[idx][0] == INF) { for (int i = 0; i < g[idx].size(); i++) { int to, rev; tie(to, rev) = g[idx][i]; if (i == back || just[idx][i] != INF) continue; just[idx][i] = rec(to, rev) + 1; } for (int i = 0; i < g[idx].size(); i++) { leftt[idx][i + 1] = max(leftt[idx][i], just[idx][i]); } for (int i = (int)g[idx].size() - 1; i >= 0; i--) { rightt[idx][i] = max(rightt[idx][i + 1], just[idx][i]); } } if (back == -1) return (rightt[idx][0]); return (max(leftt[idx][back], rightt[idx][back + 1])); } int main() { int N, M; vector<pair<int, int>> edges, arcs; scanf("%d %d", &N, &M); UnionFind uf(N); for (int i = 0; i < M; i++) { int x, y, t; cin >> x >> y >> t; --x, --y; if (t == 1) { arcs.emplace_back(x, y); g[x].emplace_back(y, -1); } else { if (uf.find(x) == uf.find(y)) { cout << "Infinite" << endl; return (0); } uf.unite(x, y); edges.emplace_back(x, y); g[y].emplace_back(x, g[x].size()); g[x].emplace_back(y, g[y].size() - 1); } } { for (auto &e : arcs) { gg[uf.find(e.first)].push_back(uf.find(e.second)); ++deg[uf.find(e.second)]; } vector<int> order; for (int i = 0; i < N; i++) { if (deg[i] == 0) order.push_back(i); } for (int i = 0; i < order.size(); i++) { for (auto &e : gg[order[i]]) { if (--deg[e] == 0) order.push_back(e); } } if (order.size() != N) { cout << "Infinite" << endl; return (0); } } int ret = 0; for (int i = 0; i < N; i++) { int sz = g[i].size(); just[i].assign(sz, INF); leftt[i].assign(sz + 1, INF); rightt[i].assign(sz + 1, INF); leftt[i][0] = rightt[i][sz] = 0; } for (int i = 0; i < N; i++) ret = max(ret, rec(i)); cout << ret << endl; }
replace
94
96
94
96
0
p00957
C++
Runtime Error
#include <iostream> #include <stdlib.h> #define SHIRO 0 #define USUI 1 #define ATSUI 2 using namespace std; int l, k; double array[200]; void print(int *pole) { for (int i = 0; i < l; i++) printf("%d ", pole[i]); printf("\n"); } bool check(int *pole, int len) { // l ?????????????¶???????????????? if (l < len) return false; // ???????????\????????? if (pole[0] == SHIRO) return false; // ???????????\????????? if (pole[len - 1] == SHIRO) return false; return true; } /* copy array */ void copy(int *tmp, int *pole) { for (int i = 0; i < l; i++) tmp[i] = pole[i]; } double kuro(int *pole, int len); // ???????????????OK?????£????????° double shiro(int *pole, int len) { if (array[len] != -1) { // printf("%d\n", array[len]); return array[len]; } int tmp[l]; /* pole ???????????? */ double count = 0; //??????????????\?????? //????????\????????????????????§??????????????????????????? //?????????????????????????????§??????????????????????????????????????? copy(tmp, pole); tmp[len] = SHIRO; if (len + 1 <= l) { count = kuro(tmp, len + 1); } array[len] = count; return count; } // ???????????????OK?????£????????° double kuro(int *pole, int len) { int tmp[l]; /* pole ???????????? */ double count = 0; //??????????????\?????? copy(tmp, pole); tmp[len] = USUI; // check ??????????????????????????????????????°???????????? if (check(tmp, len + 1)) { // print(tmp); count += 1 + shiro(tmp, len + 1); } //??????????????\?????? copy(tmp, pole); for (int i = 0; i < k; i++) tmp[len + i] = ATSUI; if (check(tmp, len + k)) { // print(tmp); count += 1 + shiro(tmp, len + k); } return count; } int main(void) { cin >> l >> k; int pole[l]; int len = 0; /* len of disks */ for (int i = 0; i < l; i++) { pole[i] = -1; } for (int i = 0; i < 200; i++) array[i] = -1; // cout << kuro(pole, 0) << endl; printf("%.0lf\n", kuro(pole, 0)); return 0; }
#include <iostream> #include <stdlib.h> #define SHIRO 0 #define USUI 1 #define ATSUI 2 using namespace std; int l, k; double array[200]; void print(int *pole) { for (int i = 0; i < l; i++) printf("%d ", pole[i]); printf("\n"); } bool check(int *pole, int len) { // l ?????????????¶???????????????? if (l < len) return false; // ???????????\????????? if (pole[0] == SHIRO) return false; // ???????????\????????? if (pole[len - 1] == SHIRO) return false; return true; } /* copy array */ void copy(int *tmp, int *pole) { for (int i = 0; i < l; i++) tmp[i] = pole[i]; } double kuro(int *pole, int len); // ???????????????OK?????£????????° double shiro(int *pole, int len) { if (array[len] != -1) { // printf("%d\n", array[len]); return array[len]; } int tmp[l]; /* pole ???????????? */ double count = 0; //??????????????\?????? //????????\????????????????????§??????????????????????????? //?????????????????????????????§??????????????????????????????????????? copy(tmp, pole); tmp[len] = SHIRO; if (len + 1 <= l) { count = kuro(tmp, len + 1); } array[len] = count; return count; } // ???????????????OK?????£????????° double kuro(int *pole, int len) { int tmp[l]; /* pole ???????????? */ double count = 0; //??????????????\?????? copy(tmp, pole); tmp[len] = USUI; // check ??????????????????????????????????????°???????????? if (check(tmp, len + 1)) { // print(tmp); count += 1 + shiro(tmp, len + 1); } //??????????????\?????? copy(tmp, pole); if (len + k <= l) { for (int i = 0; i < k; i++) tmp[len + i] = ATSUI; if (check(tmp, len + k)) { // print(tmp); count += 1 + shiro(tmp, len + k); } } return count; } int main(void) { cin >> l >> k; int pole[l]; int len = 0; /* len of disks */ for (int i = 0; i < l; i++) { pole[i] = -1; } for (int i = 0; i < 200; i++) array[i] = -1; // cout << kuro(pole, 0) << endl; printf("%.0lf\n", kuro(pole, 0)); return 0; }
replace
70
75
70
77
0
p00958
C++
Memory Limit Exceeded
#include <bits/stdc++.h> using namespace std; using Int = long long; short dp[16][16][10][1 << 16]; template <typename T1, typename T2> void chmax(T1 &a, T2 b) { if (a < b) a = b; } signed main() { cin.tie(0); ios::sync_with_stdio(0); Int n; cin >> n; using P = pair<Int, Int>; vector<P> v; for (Int i = 0; i < n; i++) { Int x, y; cin >> x >> y; v.emplace_back(x, y); } vector<vector<P>> r(n, vector<P>(n)); for (Int i = 0; i < n; i++) { for (Int j = i + 1; j < n; j++) { if (v[i].first == v[j].first) { r[i][j] = P(0, 1); continue; } if (v[i].second == v[j].second) { r[i][j] = P(1, 0); continue; } Int dx = v[i].first - v[j].first; Int dy = v[i].second - v[j].second; if (dx < 0) { dx *= -1; dy *= -1; } Int k = __gcd(dx, abs(dy)); dx /= k; dy /= k; r[i][j] = P(dx, dy); // cout<<i<<" "<<j<<":"<<dx<<" "<<dy<<endl; } } memset(dp, -1, sizeof(dp)); // dp[0][0][0][0]=0; for (Int i = 0; i < n; i++) { for (Int j = i + 1; j < n; j++) { dp[i][j][1][(1 << i) | (1 << j)] = 0; } } Int ans = 0; for (Int b = 0; b < (1 << n); b++) { for (Int i = 0; i < n; i++) { for (Int j = i + 1; j < n; j++) { for (Int k = 0; k <= n / 2; k++) { if (dp[i][j][k][b] < 0) continue; // cout<<i<<" "<<j<<" "<<k<<" "<<b<<":"<<dp[i][j][k][b]<<endl; for (Int s = 0; s < n; s++) { for (Int t = s + 1; t < n; t++) { if ((b >> s) & 1) continue; if ((b >> t) & 1) continue; Int nb = b | (1 << s) | (1 << t); if (r[i][j] == r[s][t]) { chmax(dp[s][t][k + 1][nb], dp[i][j][k][b] + k); chmax(ans, dp[s][t][k + 1][nb]); } else { chmax(dp[s][t][1][nb], dp[i][j][k][b]); chmax(ans, dp[s][t][1][nb]); } } } } } } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using Int = long long; signed char dp[16][16][10][1 << 16]; template <typename T1, typename T2> void chmax(T1 &a, T2 b) { if (a < b) a = b; } signed main() { cin.tie(0); ios::sync_with_stdio(0); Int n; cin >> n; using P = pair<Int, Int>; vector<P> v; for (Int i = 0; i < n; i++) { Int x, y; cin >> x >> y; v.emplace_back(x, y); } vector<vector<P>> r(n, vector<P>(n)); for (Int i = 0; i < n; i++) { for (Int j = i + 1; j < n; j++) { if (v[i].first == v[j].first) { r[i][j] = P(0, 1); continue; } if (v[i].second == v[j].second) { r[i][j] = P(1, 0); continue; } Int dx = v[i].first - v[j].first; Int dy = v[i].second - v[j].second; if (dx < 0) { dx *= -1; dy *= -1; } Int k = __gcd(dx, abs(dy)); dx /= k; dy /= k; r[i][j] = P(dx, dy); // cout<<i<<" "<<j<<":"<<dx<<" "<<dy<<endl; } } memset(dp, -1, sizeof(dp)); // dp[0][0][0][0]=0; for (Int i = 0; i < n; i++) { for (Int j = i + 1; j < n; j++) { dp[i][j][1][(1 << i) | (1 << j)] = 0; } } Int ans = 0; for (Int b = 0; b < (1 << n); b++) { for (Int i = 0; i < n; i++) { for (Int j = i + 1; j < n; j++) { for (Int k = 0; k <= n / 2; k++) { if (dp[i][j][k][b] < 0) continue; // cout<<i<<" "<<j<<" "<<k<<" "<<b<<":"<<dp[i][j][k][b]<<endl; for (Int s = 0; s < n; s++) { for (Int t = s + 1; t < n; t++) { if ((b >> s) & 1) continue; if ((b >> t) & 1) continue; Int nb = b | (1 << s) | (1 << t); if (r[i][j] == r[s][t]) { chmax(dp[s][t][k + 1][nb], dp[i][j][k][b] + k); chmax(ans, dp[s][t][k + 1][nb]); } else { chmax(dp[s][t][1][nb], dp[i][j][k][b]); chmax(ans, dp[s][t][1][nb]); } } } } } } } cout << ans << endl; return 0; }
replace
3
4
3
4
MLE
p00959
C++
Runtime Error
#include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #include <complex> #include <cassert> #include <functional> typedef long long ll; using namespace std; #define debug(x) cerr << #x << " = " << (x) << endl; #define mod 1000000007 // 1e9+7(prime number) #define INF 1000000000 // 1e9 #define LLINF 2000000000000000000LL // 2e18 #define SIZE 100010 int main() { int n; ll t, h[SIZE]; scanf("%d%lld", &n, &t); ll start = 0; ll maxh = 0; ll sum = 0; ll l = 0; for (int i = 0; i < n; i++) { scanf("%lld", h + i); if (i > 0 && maxh <= h[i]) { debug(i); ll pos = (t - start) / h[l]; printf("%lld\n", max(pos + 1, 1LL)); ll subsum = 0; for (int j = l + 1; j < i; j++) { subsum += h[j]; while (t - start < subsum + pos * h[l]) pos--; printf("%lld\n", max(pos + 1, 1LL)); } l = i; start += sum; maxh = h[i]; sum = h[i]; } else { sum += h[i]; if (i == 0) maxh = h[0]; } } ll pos = (t - start) / h[l]; printf("%lld\n", max(pos + 1, 1LL)); ll subsum = 0; for (int j = l + 1; j < n; j++) { subsum += h[j]; while (t - start < subsum + pos * h[l]) pos--; printf("%lld\n", max(pos + 1, 1LL)); } return 0; }
#include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #include <complex> #include <cassert> #include <functional> typedef long long ll; using namespace std; #define debug(x) cerr << #x << " = " << (x) << endl; #define mod 1000000007 // 1e9+7(prime number) #define INF 1000000000 // 1e9 #define LLINF 2000000000000000000LL // 2e18 #define SIZE 100010 int main() { int n; ll t, h[SIZE]; scanf("%d%lld", &n, &t); ll start = 0; ll maxh = 0; ll sum = 0; ll l = 0; for (int i = 0; i < n; i++) { scanf("%lld", h + i); if (i > 0 && maxh <= h[i]) { ll pos = (t - start) / h[l]; printf("%lld\n", max(pos + 1, 1LL)); ll subsum = 0; for (int j = l + 1; j < i; j++) { subsum += h[j]; while (t - start < subsum + pos * h[l]) pos--; printf("%lld\n", max(pos + 1, 1LL)); } l = i; start += sum; maxh = h[i]; sum = h[i]; } else { sum += h[i]; if (i == 0) maxh = h[0]; } } ll pos = (t - start) / h[l]; printf("%lld\n", max(pos + 1, 1LL)); ll subsum = 0; for (int j = l + 1; j < n; j++) { subsum += h[j]; while (t - start < subsum + pos * h[l]) pos--; printf("%lld\n", max(pos + 1, 1LL)); } return 0; }
delete
46
47
46
46
0
i = 1
p00960
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using uint = unsigned int; using ll = long long; using ull = unsigned long long; constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); } template <class T> using V = vector<T>; template <class T> using VV = V<V<T>>; using D = double; const D PI = acos(D(-1)), EPS = 1e-10; struct Pt2 { D x, y; Pt2() {} Pt2(D _x, D _y) : x(_x), y(_y) {} Pt2 operator+(const Pt2 &r) const { return Pt2(x + r.x, y + r.y); } Pt2 operator-(const Pt2 &r) const { return Pt2(x - r.x, y - r.y); } Pt2 operator*(const Pt2 &r) const { return Pt2(x * r.x - y * r.y, x * r.y + y * r.x); } Pt2 &operator+=(const Pt2 &r) { return *this = *this + r; } Pt2 &operator-=(const Pt2 &r) { return *this = *this - r; } Pt2 &operator*=(const Pt2 &r) { return *this = *this * r; } Pt2 operator-() const { return Pt2(-x, -y); } D abs() const { return sqrt(x * x + y * y); } D rabs() const { return max(x, y); } // robust abs D arg() const { return atan2(y, x); } pair<D, D> to_pair() const { return make_pair(x, y); } static Pt2 polar(D le, D th) { return Pt2(le * cos(th), le * sin(th)); } }; template <class T> ostream &operator<<(ostream &os, const Pt2 &p) { os << "(" << p.x << ", " << p.y << ")"; return os; } using P = Pt2; int sgn(D a) { if (abs(a) <= EPS) return 0; return (a < 0) ? -1 : 1; } // relative sign int rsgn(D a, D f) { if (abs(a) <= f * EPS) return 0; return (a < 0) ? -1 : 1; } bool near(P a, P b) { return !sgn((a - b).abs()); } bool lessP(P l, P r) { if (sgn(r.x - l.x)) return l.x < r.x; if (sgn(r.y - l.y)) return l.y < r.y; return false; } D cross(P a, P b) { return a.x * b.y - a.y * b.x; } D dot(P a, P b) { return a.x * b.x + a.y * b.y; } // -2, -1, 0, 1, 2 : front, clock, on, cclock, back int ccw(P b, P c) { int s = rsgn(cross(b, c), b.rabs()); if (s) return s; if (!sgn(c.rabs()) || !sgn((c - b).rabs())) return 0; if (dot(b, c) < 0) return 2; if (dot(-b, c - b) < 0) return -2; return 0; } int ccw(P a, P b, P c) { return ccw(b - a, c - a); } using Pol = V<P>; Pol convex_up(const Pol &p, int l, int r, int e1 = -1, int e2 = -1) { Pol up; for (int i = l; i <= r; i++) { if (i == e1 || i == e2) continue; P d = p[i]; while (up.size() > 1 && ccw(up[up.size() - 2], up[up.size() - 1], d) == 1) up.pop_back(); up.push_back(d); } return up; } Pol convex_down(const Pol &p, int l, int r, int e1 = -1, int e2 = -1) { Pol down; for (int i = l; i <= r; i++) { if (i == e1 || i == e2) continue; P d = p[i]; while (down.size() > 1 && ccw(down[down.size() - 2], down[down.size() - 1], d) == -1) down.pop_back(); down.push_back(d); } return down; } D line_len(const Pol &p) { int n = int(p.size()); D sm = 0; for (int i = 0; i < n - 1; i++) { sm += (p[i + 1] - p[i]).abs(); } return sm; } bool operator<(const P &x, const P &y) { return lessP(x, y); } D solve(V<P> v, bool one = false) { int n = int(v.size()); map<P, int> mp; for (int i = 0; i < n; i++) mp[v[i]] = i; auto up = convex_up(v, 0, n - 1); auto down = convex_down(v, 0, n - 1); int un = int(up.size()), dn = int(down.size()); V<D> bufu, bufd; for (int i = 1; i < un - 1; i++) { D base = (up[i - 1] - up[i]).abs() + (up[i] - up[i + 1]).abs(); bufu.push_back( base - line_len(convex_up(v, mp[up[i - 1]], mp[up[i + 1]], mp[up[i]]))); } for (int i = 1; i < dn - 1; i++) { D base = (down[i - 1] - down[i]).abs() + (down[i] - down[i + 1]).abs(); bufd.push_back(base - line_len(convex_down(v, mp[down[i - 1]], mp[down[i + 1]], mp[down[i]]))); } D ans = -1e100; D uma = -1e100, dma = -1e100; for (int i = 1; i < un - 2; i++) { ans = max(ans, uma + bufu[i]); uma = max(uma, bufu[i - 1]); } for (int i = 1; i < dn - 2; i++) { ans = max(ans, dma + bufu[i]); dma = max(dma, bufu[i - 1]); } for (auto d : bufu) uma = max(uma, d); for (auto d : bufd) dma = max(dma, d); if (one) return max(uma, dma); ans = max(ans, uma + dma); for (int i = 1; i < un - 1; i++) { D base = (up[i - 1] - up[i]).abs() + (up[i] - up[i + 1]).abs(); auto npo = convex_up(v, mp[up[i - 1]], mp[up[i + 1]], mp[up[i]]); base -= line_len(npo); int m = int(npo.size()); for (int j = 1; j < m - 1; j++) { D base2 = (npo[j - 1] - npo[j]).abs() + (npo[j] - npo[j + 1]).abs(); ans = max(ans, base + base2 - line_len(convex_up(v, mp[npo[j - 1]], mp[npo[j + 1]], mp[npo[j]], mp[up[i]]))); } } for (int i = 1; i < dn - 1; i++) { D base = (down[i - 1] - down[i]).abs() + (down[i] - down[i + 1]).abs(); auto npo = convex_down(v, mp[down[i - 1]], mp[down[i + 1]], mp[down[i]]); base -= line_len(npo); int m = int(npo.size()); for (int j = 1; j < m - 1; j++) { D base2 = (npo[j - 1] - npo[j]).abs() + (npo[j] - npo[j + 1]).abs(); ans = max(ans, base + base2 - line_len(convex_down(v, mp[npo[j - 1]], mp[npo[j + 1]], mp[npo[j]], mp[down[i]]))); } } for (int i = 1; i < un - 2; i++) { D base = (up[i - 1] - up[i]).abs() + (up[i] - up[i + 1]).abs() + (up[i + 1] - up[i + 2]).abs(); ans = max(ans, base - line_len(convex_up(v, mp[up[i - 1]], mp[up[i + 2]], mp[up[i]], mp[up[i + 1]]))); } for (int i = 1; i < dn - 2; i++) { D base = (down[i - 1] - down[i]).abs() + (down[i] - down[i + 1]).abs() + (down[i + 1] - down[i + 2]).abs(); ans = max(ans, base - line_len(convex_down(v, mp[down[i - 1]], mp[down[i + 2]], mp[down[i]], mp[down[i + 1]]))); } return ans; } D naive(V<P> v) { int n = int(v.size()); auto up = convex_up(v, 0, n - 1); auto down = convex_down(v, 0, n - 1); return line_len(up) + line_len(down); } int main() { cin.tie(0); ios::sync_with_stdio(false); cout << setprecision(20); int n; cin >> n; V<P> v(n); for (int i = 0; i < n; i++) { D x, y; cin >> x >> y; v[i] = P(x, y); v[i] *= P::polar(1, 13.4514); } sort(begin(v), end(v)); D ma = -1; D base = naive(v); ma = max(ma, base - naive(V<P>(begin(v), end(v) - 2))); ma = max(ma, base - naive(V<P>(begin(v) + 1, end(v) - 1))); ma = max(ma, base - naive(V<P>(begin(v) + 2, end(v)))); ma = max(ma, base - naive(V<P>(begin(v), end(v) - 1)) + solve(V<P>(begin(v), end(v) - 1), true)); ma = max(ma, base - naive(V<P>(begin(v) + 1, end(v))) + solve(V<P>(begin(v) + 1, end(v)), true)); ma = max(ma, solve(v)); cout << ma << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using uint = unsigned int; using ll = long long; using ull = unsigned long long; constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); } template <class T> using V = vector<T>; template <class T> using VV = V<V<T>>; using D = double; const D PI = acos(D(-1)), EPS = 1e-10; struct Pt2 { D x, y; Pt2() {} Pt2(D _x, D _y) : x(_x), y(_y) {} Pt2 operator+(const Pt2 &r) const { return Pt2(x + r.x, y + r.y); } Pt2 operator-(const Pt2 &r) const { return Pt2(x - r.x, y - r.y); } Pt2 operator*(const Pt2 &r) const { return Pt2(x * r.x - y * r.y, x * r.y + y * r.x); } Pt2 &operator+=(const Pt2 &r) { return *this = *this + r; } Pt2 &operator-=(const Pt2 &r) { return *this = *this - r; } Pt2 &operator*=(const Pt2 &r) { return *this = *this * r; } Pt2 operator-() const { return Pt2(-x, -y); } D abs() const { return sqrt(x * x + y * y); } D rabs() const { return max(x, y); } // robust abs D arg() const { return atan2(y, x); } pair<D, D> to_pair() const { return make_pair(x, y); } static Pt2 polar(D le, D th) { return Pt2(le * cos(th), le * sin(th)); } }; template <class T> ostream &operator<<(ostream &os, const Pt2 &p) { os << "(" << p.x << ", " << p.y << ")"; return os; } using P = Pt2; int sgn(D a) { if (abs(a) <= EPS) return 0; return (a < 0) ? -1 : 1; } // relative sign int rsgn(D a, D f) { if (abs(a) <= f * EPS) return 0; return (a < 0) ? -1 : 1; } bool near(P a, P b) { return !sgn((a - b).abs()); } bool lessP(P l, P r) { if (sgn(r.x - l.x)) return l.x < r.x; if (sgn(r.y - l.y)) return l.y < r.y; return false; } D cross(P a, P b) { return a.x * b.y - a.y * b.x; } D dot(P a, P b) { return a.x * b.x + a.y * b.y; } // -2, -1, 0, 1, 2 : front, clock, on, cclock, back int ccw(P b, P c) { int s = rsgn(cross(b, c), b.rabs()); if (s) return s; if (!sgn(c.rabs()) || !sgn((c - b).rabs())) return 0; if (dot(b, c) < 0) return 2; if (dot(-b, c - b) < 0) return -2; return 0; } int ccw(P a, P b, P c) { return ccw(b - a, c - a); } using Pol = V<P>; Pol convex_up(const Pol &p, int l, int r, int e1 = -1, int e2 = -1) { Pol up; for (int i = l; i <= r; i++) { if (i == e1 || i == e2) continue; P d = p[i]; while (up.size() > 1 && ccw(up[up.size() - 2], up[up.size() - 1], d) == 1) up.pop_back(); up.push_back(d); } return up; } Pol convex_down(const Pol &p, int l, int r, int e1 = -1, int e2 = -1) { Pol down; for (int i = l; i <= r; i++) { if (i == e1 || i == e2) continue; P d = p[i]; while (down.size() > 1 && ccw(down[down.size() - 2], down[down.size() - 1], d) == -1) down.pop_back(); down.push_back(d); } return down; } D line_len(const Pol &p) { int n = int(p.size()); D sm = 0; for (int i = 0; i < n - 1; i++) { sm += (p[i + 1] - p[i]).abs(); } return sm; } bool operator<(const P &x, const P &y) { return lessP(x, y); } D solve(V<P> v, bool one = false) { int n = int(v.size()); map<P, int> mp; for (int i = 0; i < n; i++) mp[v[i]] = i; auto up = convex_up(v, 0, n - 1); auto down = convex_down(v, 0, n - 1); int un = int(up.size()), dn = int(down.size()); V<D> bufu, bufd; for (int i = 1; i < un - 1; i++) { D base = (up[i - 1] - up[i]).abs() + (up[i] - up[i + 1]).abs(); bufu.push_back( base - line_len(convex_up(v, mp[up[i - 1]], mp[up[i + 1]], mp[up[i]]))); } for (int i = 1; i < dn - 1; i++) { D base = (down[i - 1] - down[i]).abs() + (down[i] - down[i + 1]).abs(); bufd.push_back(base - line_len(convex_down(v, mp[down[i - 1]], mp[down[i + 1]], mp[down[i]]))); } D ans = -1e100; D uma = -1e100, dma = -1e100; for (int i = 1; i < un - 2; i++) { ans = max(ans, uma + bufu[i]); uma = max(uma, bufu[i - 1]); } for (int i = 1; i < dn - 2; i++) { ans = max(ans, dma + bufd[i]); dma = max(dma, bufd[i - 1]); } for (auto d : bufu) uma = max(uma, d); for (auto d : bufd) dma = max(dma, d); if (one) return max(uma, dma); ans = max(ans, uma + dma); for (int i = 1; i < un - 1; i++) { D base = (up[i - 1] - up[i]).abs() + (up[i] - up[i + 1]).abs(); auto npo = convex_up(v, mp[up[i - 1]], mp[up[i + 1]], mp[up[i]]); base -= line_len(npo); int m = int(npo.size()); for (int j = 1; j < m - 1; j++) { D base2 = (npo[j - 1] - npo[j]).abs() + (npo[j] - npo[j + 1]).abs(); ans = max(ans, base + base2 - line_len(convex_up(v, mp[npo[j - 1]], mp[npo[j + 1]], mp[npo[j]], mp[up[i]]))); } } for (int i = 1; i < dn - 1; i++) { D base = (down[i - 1] - down[i]).abs() + (down[i] - down[i + 1]).abs(); auto npo = convex_down(v, mp[down[i - 1]], mp[down[i + 1]], mp[down[i]]); base -= line_len(npo); int m = int(npo.size()); for (int j = 1; j < m - 1; j++) { D base2 = (npo[j - 1] - npo[j]).abs() + (npo[j] - npo[j + 1]).abs(); ans = max(ans, base + base2 - line_len(convex_down(v, mp[npo[j - 1]], mp[npo[j + 1]], mp[npo[j]], mp[down[i]]))); } } for (int i = 1; i < un - 2; i++) { D base = (up[i - 1] - up[i]).abs() + (up[i] - up[i + 1]).abs() + (up[i + 1] - up[i + 2]).abs(); ans = max(ans, base - line_len(convex_up(v, mp[up[i - 1]], mp[up[i + 2]], mp[up[i]], mp[up[i + 1]]))); } for (int i = 1; i < dn - 2; i++) { D base = (down[i - 1] - down[i]).abs() + (down[i] - down[i + 1]).abs() + (down[i + 1] - down[i + 2]).abs(); ans = max(ans, base - line_len(convex_down(v, mp[down[i - 1]], mp[down[i + 2]], mp[down[i]], mp[down[i + 1]]))); } return ans; } D naive(V<P> v) { int n = int(v.size()); auto up = convex_up(v, 0, n - 1); auto down = convex_down(v, 0, n - 1); return line_len(up) + line_len(down); } int main() { cin.tie(0); ios::sync_with_stdio(false); cout << setprecision(20); int n; cin >> n; V<P> v(n); for (int i = 0; i < n; i++) { D x, y; cin >> x >> y; v[i] = P(x, y); v[i] *= P::polar(1, 13.4514); } sort(begin(v), end(v)); D ma = -1; D base = naive(v); ma = max(ma, base - naive(V<P>(begin(v), end(v) - 2))); ma = max(ma, base - naive(V<P>(begin(v) + 1, end(v) - 1))); ma = max(ma, base - naive(V<P>(begin(v) + 2, end(v)))); ma = max(ma, base - naive(V<P>(begin(v), end(v) - 1)) + solve(V<P>(begin(v), end(v) - 1), true)); ma = max(ma, base - naive(V<P>(begin(v) + 1, end(v))) + solve(V<P>(begin(v) + 1, end(v)), true)); ma = max(ma, solve(v)); cout << ma << endl; return 0; }
replace
150
152
150
152
0
p00962
C++
Runtime Error
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; (i) < int(n); ++(i)) using ll = long long; using namespace std; constexpr ll inf = ll(1e18) + 9; vector<ll> dijkstra(vector<vector<pair<int, ll>>> const &g, int root) { vector<ll> dist(g.size(), inf); priority_queue<pair<ll, int>> que; dist[root] = 0; que.emplace(-dist[root], root); while (not que.empty()) { ll dist_i; int i; tie(dist_i, i) = que.top(); que.pop(); if (dist[i] < -dist_i) continue; for (auto it : g[i]) { int j; ll cost; tie(j, cost) = it; if (-dist_i + cost < dist[j]) { dist[j] = -dist_i + cost; que.emplace(dist_i - cost, j); } } } return dist; } pair<int, vector<int>> decompose_to_two_edge_connected_components(vector<vector<int>> const &g) { int n = g.size(); vector<int> imos(n); { // imos[i] == 0 iff the edge i -> parent is a bridge vector<char> used( n); // 0: unused ; 1: exists on stack ; 2: removed from stack function<void(int, int)> go = [&](int i, int parent) { used[i] = 1; for (int j : g[i]) if (j != parent) { if (used[j] == 0) { go(j, i); imos[i] += imos[j]; } else if (used[j] == 1) { imos[i] += 1; imos[j] -= 1; } } used[i] = 2; }; REP(i, n) if (used[i] == 0) { go(i, -1); } } int size = 0; vector<int> component_of(n, -1); { function<void(int)> go = [&](int i) { for (int j : g[i]) if (component_of[j] == -1) { component_of[j] = imos[j] == 0 ? size++ : component_of[i]; go(j); } }; REP(i, n) if (component_of[i] == -1) { component_of[i] = size++; go(i); } } return {size, move(component_of)}; } enum result_t { HAPPY, SOSO, SAD }; constexpr int start = 0; constexpr int goal = 1; int main() { // input int n, m; scanf("%d%d", &n, &m); vector<tuple<int, int, int>> edges(m); REP(i, m) { int a, b, c; scanf("%d%d%d", &a, &b, &c); --a; --b; edges[i] = make_tuple(a, b, c); } // solve vector<vector<pair<int, ll>>> g(n); vector<vector<pair<int, ll>>> rev_g(n); map<tuple<int, int, int>, int> count_edges; for (auto edge : edges) { int a, b, c; tie(a, b, c) = edge; g[a].emplace_back(b, c); rev_g[b].emplace_back(a, c); count_edges[edge] += 1; } auto dist = dijkstra(g, start); auto rev_dist = dijkstra(rev_g, goal); vector<vector<int>> h(n); REP(i, n) { for (auto edge : g[i]) { int j, cost; tie(j, cost) = edge; if (dist[i] + cost + rev_dist[j] == dist[goal]) { h[i].push_back(j); h[j].push_back(i); } } } auto component_of = decompose_to_two_edge_connected_components(h).second; REP(i, n) cerr << i + 1 << " " << component_of[i] << endl; // output for (auto edge : edges) { int a, b, c; tie(a, b, c) = edge; result_t result = dist[b] + c + rev_dist[a] < dist[goal] ? HAPPY : // dist[a] == inf or rev_dist[b] == inf ? SOSO : dist[b] + c + rev_dist[a] == dist[goal] ? SOSO : dist[a] + c + rev_dist[b] != dist[goal] ? SOSO : count_edges[edge] >= 2 ? SOSO : component_of[a] != component_of[b] ? SAD : SOSO; printf("%s\n", result == HAPPY ? "HAPPY" : result == SOSO ? "SOSO" : result == SAD ? "SAD" : ""); } return 0; }
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; (i) < int(n); ++(i)) using ll = long long; using namespace std; constexpr ll inf = ll(1e18) + 9; vector<ll> dijkstra(vector<vector<pair<int, ll>>> const &g, int root) { vector<ll> dist(g.size(), inf); priority_queue<pair<ll, int>> que; dist[root] = 0; que.emplace(-dist[root], root); while (not que.empty()) { ll dist_i; int i; tie(dist_i, i) = que.top(); que.pop(); if (dist[i] < -dist_i) continue; for (auto it : g[i]) { int j; ll cost; tie(j, cost) = it; if (-dist_i + cost < dist[j]) { dist[j] = -dist_i + cost; que.emplace(dist_i - cost, j); } } } return dist; } pair<int, vector<int>> decompose_to_two_edge_connected_components(vector<vector<int>> const &g) { int n = g.size(); vector<int> imos(n); { // imos[i] == 0 iff the edge i -> parent is a bridge vector<char> used( n); // 0: unused ; 1: exists on stack ; 2: removed from stack function<void(int, int)> go = [&](int i, int parent) { used[i] = 1; for (int j : g[i]) if (j != parent) { if (used[j] == 0) { go(j, i); imos[i] += imos[j]; } else if (used[j] == 1) { imos[i] += 1; imos[j] -= 1; } } used[i] = 2; }; REP(i, n) if (used[i] == 0) { go(i, -1); } } int size = 0; vector<int> component_of(n, -1); { function<void(int)> go = [&](int i) { for (int j : g[i]) if (component_of[j] == -1) { component_of[j] = imos[j] == 0 ? size++ : component_of[i]; go(j); } }; REP(i, n) if (component_of[i] == -1) { component_of[i] = size++; go(i); } } return {size, move(component_of)}; } enum result_t { HAPPY, SOSO, SAD }; constexpr int start = 0; constexpr int goal = 1; int main() { // input int n, m; scanf("%d%d", &n, &m); vector<tuple<int, int, int>> edges(m); REP(i, m) { int a, b, c; scanf("%d%d%d", &a, &b, &c); --a; --b; edges[i] = make_tuple(a, b, c); } // solve vector<vector<pair<int, ll>>> g(n); vector<vector<pair<int, ll>>> rev_g(n); map<tuple<int, int, int>, int> count_edges; for (auto edge : edges) { int a, b, c; tie(a, b, c) = edge; g[a].emplace_back(b, c); rev_g[b].emplace_back(a, c); count_edges[edge] += 1; } auto dist = dijkstra(g, start); auto rev_dist = dijkstra(rev_g, goal); vector<vector<int>> h(n); REP(i, n) { for (auto edge : g[i]) { int j, cost; tie(j, cost) = edge; if (dist[i] + cost + rev_dist[j] == dist[goal]) { h[i].push_back(j); h[j].push_back(i); } } } auto component_of = decompose_to_two_edge_connected_components(h).second; // output for (auto edge : edges) { int a, b, c; tie(a, b, c) = edge; result_t result = dist[b] + c + rev_dist[a] < dist[goal] ? HAPPY : // dist[a] == inf or rev_dist[b] == inf ? SOSO : dist[b] + c + rev_dist[a] == dist[goal] ? SOSO : dist[a] + c + rev_dist[b] != dist[goal] ? SOSO : count_edges[edge] >= 2 ? SOSO : component_of[a] != component_of[b] ? SAD : SOSO; printf("%s\n", result == HAPPY ? "HAPPY" : result == SOSO ? "SOSO" : result == SAD ? "SAD" : ""); } return 0; }
delete
113
114
113
113
0
1 0 2 3 3 1 4 2
p00965
C++
Runtime Error
#include <algorithm> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #define FRER() freopen("i.txt", "r", stdin); using namespace std; const int maxn = 100000 + 5; int l[maxn], r[maxn], ml[maxn] = {0}, mr[maxn] = {0}, sum[maxn] = {0}; int main() { int n, ans1 = 0, ans2 = 0; scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d %d", &l[i], &r[i]); ++sum[l[i]]; --sum[r[i]]; ++ml[l[i]]; ++mr[r[i]]; } for (int i = 1; i < maxn; ++i) { sum[i] += sum[i - 1]; ml[i] += ml[i - 1]; mr[i] += mr[i - 1]; ans2 = max(ans2, sum[i]); } for (int i = 0; i < n; ++i) { ans1 = max(ans1, ml[r[i] - 1] - mr[l[i]]); } printf("%d %d\n", ans1, ans2); return 0; }
#include <algorithm> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #define FRER() freopen("i.txt", "r", stdin); using namespace std; const int maxn = 200000 + 5; int l[maxn], r[maxn], ml[maxn] = {0}, mr[maxn] = {0}, sum[maxn] = {0}; int main() { int n, ans1 = 0, ans2 = 0; scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d %d", &l[i], &r[i]); ++sum[l[i]]; --sum[r[i]]; ++ml[l[i]]; ++mr[r[i]]; } for (int i = 1; i < maxn; ++i) { sum[i] += sum[i - 1]; ml[i] += ml[i - 1]; mr[i] += mr[i - 1]; ans2 = max(ans2, sum[i]); } for (int i = 0; i < n; ++i) { ans1 = max(ans1, ml[r[i] - 1] - mr[l[i]]); } printf("%d %d\n", ans1, ans2); return 0; }
replace
10
11
10
11
0
p00967
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; } struct UnionFindTree { vector<int> par, sz; void init() { rep(i, par.size()) par[i] = i, sz[i] = 1; } UnionFindTree(int n) { par.resize(n); sz.resize(n); for (int i = 0; i < n; i++) { par[i] = i; sz[i] = 1; } } int find(int x) { return x == par[x] ? x : par[x] = find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (sz[x] < sz[y]) swap(x, y); sz[x] += sz[y]; par[y] = x; } bool areSame(int x, int y) { return find(x) == find(y); } int size(int x) { return sz[find(x)]; } }; int N, M; int A[111111], B[111111]; vint G[111111]; bool mark[111111]; bool dfs(int v, int p) { int cnt = 0; for (auto u : G[v]) { if (u == p) continue; cnt += dfs(u, v); } if (cnt >= 2) mark[v] = true; return mark[v] || cnt; } int n, m; int id[111111]; int par[111111]; int parEdgeId[111111]; int aa[111111], bb[111111]; int dfs2(int v, int p, int a) { if (mark[v]) { id[v] = n++; par[id[v]] = a; if (a != -1) { aa[m] = a; bb[m] = id[v]; parEdgeId[id[v]] = m++; } a = v; } for (auto u : G[v]) { if (u == p) continue; dfs2(u, v, a); } } typedef bitset<100> mask; signed main() { cin >> N >> M; UnionFindTree uf(N); vpint es; rep(i, M) { int a, b; cin >> a >> b; a--; b--; A[i] = a; B[i] = b; if (uf.areSame(a, b)) { es.pb({a, b}); mark[A[i]] = true; mark[B[i]] = true; } else { uf.unite(a, b); G[a].pb(b); G[b].pb(a); } } dfs(0, -1); dfs2(0, -1, -1); rep(i, es.size()) { es[i].fi = id[es[i].fi]; es[i].se = id[es[i].se]; aa[m] = es[i].fi; bb[m] = es[i].se; m++; } mask st[20]; rep(i, es.size()) { int a = es[i].fi; int b = es[i].se; st[i][m - (int)es.size() + i] = 1; rep(latte, 2) { int v = a; while (par[v] != -1) { st[i][parEdgeId[v]] = 1 - st[i][parEdgeId[v]]; v = par[v]; } swap(a, b); } } rep(i, m) if (!(0 <= aa[i] && aa[i] < n) || !(0 <= bb[i] && bb[i] < n)) { for (;;) ; } int ans = 0; for (int i = 1; i < 1 << es.size(); i++) { UnionFindTree uf(n); mask x; rep(j, es.size()) if (i >> j & 1) x ^= st[j]; int latte; rep(j, m) if (x[j]) uf.unite(aa[j], bb[j]), latte = aa[j]; if (uf.size(latte) == x.count()) ans++; } cout << ans << endl; 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; } struct UnionFindTree { vector<int> par, sz; void init() { rep(i, par.size()) par[i] = i, sz[i] = 1; } UnionFindTree(int n) { par.resize(n); sz.resize(n); for (int i = 0; i < n; i++) { par[i] = i; sz[i] = 1; } } int find(int x) { return x == par[x] ? x : par[x] = find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (sz[x] < sz[y]) swap(x, y); sz[x] += sz[y]; par[y] = x; } bool areSame(int x, int y) { return find(x) == find(y); } int size(int x) { return sz[find(x)]; } }; int N, M; int A[111111], B[111111]; vint G[111111]; bool mark[111111]; bool dfs(int v, int p) { int cnt = 0; for (auto u : G[v]) { if (u == p) continue; cnt += dfs(u, v); } if (cnt >= 2) mark[v] = true; return mark[v] || cnt; } int n, m; int id[111111]; int par[111111]; int parEdgeId[111111]; int aa[111111], bb[111111]; int dfs2(int v, int p, int a) { if (mark[v]) { id[v] = n++; par[id[v]] = a; if (a != -1) { aa[m] = a; bb[m] = id[v]; parEdgeId[id[v]] = m++; } a = id[v]; } for (auto u : G[v]) { if (u == p) continue; dfs2(u, v, a); } } typedef bitset<100> mask; signed main() { cin >> N >> M; UnionFindTree uf(N); vpint es; rep(i, M) { int a, b; cin >> a >> b; a--; b--; A[i] = a; B[i] = b; if (uf.areSame(a, b)) { es.pb({a, b}); mark[A[i]] = true; mark[B[i]] = true; } else { uf.unite(a, b); G[a].pb(b); G[b].pb(a); } } dfs(0, -1); dfs2(0, -1, -1); rep(i, es.size()) { es[i].fi = id[es[i].fi]; es[i].se = id[es[i].se]; aa[m] = es[i].fi; bb[m] = es[i].se; m++; } mask st[20]; rep(i, es.size()) { int a = es[i].fi; int b = es[i].se; st[i][m - (int)es.size() + i] = 1; rep(latte, 2) { int v = a; while (par[v] != -1) { st[i][parEdgeId[v]] = 1 - st[i][parEdgeId[v]]; v = par[v]; } swap(a, b); } } rep(i, m) if (!(0 <= aa[i] && aa[i] < n) || !(0 <= bb[i] && bb[i] < n)) { for (;;) ; } int ans = 0; for (int i = 1; i < 1 << es.size(); i++) { UnionFindTree uf(n); mask x; rep(j, es.size()) if (i >> j & 1) x ^= st[j]; int latte; rep(j, m) if (x[j]) uf.unite(aa[j], bb[j]), latte = aa[j]; if (uf.size(latte) == x.count()) ans++; } cout << ans << endl; return 0; }
replace
82
83
82
83
TLE
p00967
C++
Runtime Error
#include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #include <complex> #include <cassert> #include <functional> typedef long long ll; using namespace std; #define debug(x) cerr << #x << " = " << (x) << endl; #define mod 1000000007 // 1e9+7(prime number) #define INF 1000000000 // 1e9 #define LLINF 2000000000000000000LL // 2e18 #define SIZE 100020 /* UnionFind */ struct UnionFind { vector<int> data, tree_size; UnionFind(int s) : data(s, -1), tree_size(s, 1) {} int root(int x) { if (data[x] == -1) return x; return data[x] = root(data[x]); } bool set(int x, int y) { x = root(x); y = root(y); if (x == y) return false; data[y] = x; tree_size[x] += tree_size[y]; tree_size[y] = 0; return true; } bool check(int x, int y) { x = root(x); y = root(y); return x == y; } int size(int x) { return tree_size[root(x)]; } }; vector<pair<int, int>> G[SIZE]; bool tree[SIZE]; int u[SIZE], v[SIZE]; int visited[SIZE]; int depth[SIZE]; bool dfs(int now, int back = -1, int d = 0) { if (visited[now]) return false; visited[now] = true; depth[now] = d; for (int i = 0; i < G[now].size(); i++) { if (G[now][i].first != back) tree[G[now][i].second] = dfs(G[now][i].first, now, d + 1); } return true; } vector<int> cprG[SIZE]; int cprID[SIZE]; int cprSize = 0; int compress(int now, int back = -1) { bool need = false; int res = -1; vector<int> child; for (int i = 0; i < G[now].size(); i++) { if (G[now][i].first != back && tree[G[now][i].second]) { int p = compress(G[now][i].first, now); if (p != -1) { child.push_back(p); } } need |= !tree[G[now][i].second]; } need |= back == -1; need |= child.size() >= 2; if (need) { for (int i = 0; i < child.size(); i++) { int p = now; int q = child[i]; cprG[p].push_back(q); cprG[q].push_back(p); cprSize++; } res = now; cprID[now] = cprSize++; } if (!need && child.size() == 1) { res = child[0]; } return res; } void cprdfs(int *sum, vector<pair<int, int>> &vec, int now, int back = -1) { for (int i = 0; i < cprG[now].size(); i++) { if (cprG[now][i] == back) continue; cprdfs(sum, vec, cprG[now][i], now); sum[cprID[now]] += sum[cprID[cprG[now][i]]]; if (sum[cprID[cprG[now][i]]] % 2) { vec.push_back({now, cprG[now][i]}); } } } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { scanf("%d%d", u + i, v + i); u[i]--; v[i]--; G[u[i]].push_back({v[i], i}); G[v[i]].push_back({u[i], i}); } dfs(0); compress(0); debug(cprSize); pair<int, int> addEdge[17]; int s = 0; for (int i = 0; i < m; i++) { if (!tree[i]) { if (depth[u[i]] > depth[v[i]]) swap(u[i], v[i]); addEdge[s++] = {u[i], v[i]}; } } int ans = 0; int sum[SIZE] = {}; int inedge[SIZE] = {}; for (int i = 1; i < (1 << s); i++) { vector<pair<int, int>> vec; for (int i = 0; i < cprSize; i++) { sum[i] = inedge[i] = 0; } for (int j = 0; j < s; j++) { if (i & (1 << j)) { sum[cprID[addEdge[j].first]]--; sum[cprID[addEdge[j].second]]++; vec.push_back({addEdge[j].first, addEdge[j].second}); } } cprdfs(sum, vec, 0); // debug(vec.size()); bool flag = true; UnionFind uf(cprSize); int group = 0; // 入次数2 for (int j = 0; j < vec.size(); j++) { inedge[cprID[vec[j].first]]++; inedge[cprID[vec[j].second]]++; group += uf.set(cprID[vec[j].first], cprID[vec[j].second]); } for (int j = 0; j < vec.size(); j++) { flag &= inedge[cprID[vec[j].first]] == 2; flag &= inedge[cprID[vec[j].second]] == 2; } // 連結 flag &= group == vec.size() - 1; ans += flag; } printf("%d\n", ans); return 0; }
#include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #include <complex> #include <cassert> #include <functional> typedef long long ll; using namespace std; #define debug(x) cerr << #x << " = " << (x) << endl; #define mod 1000000007 // 1e9+7(prime number) #define INF 1000000000 // 1e9 #define LLINF 2000000000000000000LL // 2e18 #define SIZE 100020 /* UnionFind */ struct UnionFind { vector<int> data, tree_size; UnionFind(int s) : data(s, -1), tree_size(s, 1) {} int root(int x) { if (data[x] == -1) return x; return data[x] = root(data[x]); } bool set(int x, int y) { x = root(x); y = root(y); if (x == y) return false; data[y] = x; tree_size[x] += tree_size[y]; tree_size[y] = 0; return true; } bool check(int x, int y) { x = root(x); y = root(y); return x == y; } int size(int x) { return tree_size[root(x)]; } }; vector<pair<int, int>> G[SIZE]; bool tree[SIZE]; int u[SIZE], v[SIZE]; int visited[SIZE]; int depth[SIZE]; bool dfs(int now, int back = -1, int d = 0) { if (visited[now]) return false; visited[now] = true; depth[now] = d; for (int i = 0; i < G[now].size(); i++) { if (G[now][i].first != back) tree[G[now][i].second] = dfs(G[now][i].first, now, d + 1); } return true; } vector<int> cprG[SIZE]; int cprID[SIZE]; int cprSize = 0; int compress(int now, int back = -1) { bool need = false; int res = -1; vector<int> child; for (int i = 0; i < G[now].size(); i++) { if (G[now][i].first != back && tree[G[now][i].second]) { int p = compress(G[now][i].first, now); if (p != -1) { child.push_back(p); } } need |= !tree[G[now][i].second]; } need |= back == -1; need |= child.size() >= 2; if (need) { for (int i = 0; i < child.size(); i++) { int p = now; int q = child[i]; cprG[p].push_back(q); cprG[q].push_back(p); cprSize++; } res = now; cprID[now] = cprSize++; } if (!need && child.size() == 1) { res = child[0]; } return res; } void cprdfs(int *sum, vector<pair<int, int>> &vec, int now, int back = -1) { for (int i = 0; i < cprG[now].size(); i++) { if (cprG[now][i] == back) continue; cprdfs(sum, vec, cprG[now][i], now); sum[cprID[now]] += sum[cprID[cprG[now][i]]]; if (sum[cprID[cprG[now][i]]] % 2) { vec.push_back({now, cprG[now][i]}); } } } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { scanf("%d%d", u + i, v + i); u[i]--; v[i]--; G[u[i]].push_back({v[i], i}); G[v[i]].push_back({u[i], i}); } dfs(0); compress(0); // debug(cprSize); pair<int, int> addEdge[17]; int s = 0; for (int i = 0; i < m; i++) { if (!tree[i]) { if (depth[u[i]] > depth[v[i]]) swap(u[i], v[i]); addEdge[s++] = {u[i], v[i]}; } } int ans = 0; int sum[SIZE] = {}; int inedge[SIZE] = {}; for (int i = 1; i < (1 << s); i++) { vector<pair<int, int>> vec; for (int i = 0; i < cprSize; i++) { sum[i] = inedge[i] = 0; } for (int j = 0; j < s; j++) { if (i & (1 << j)) { sum[cprID[addEdge[j].first]]--; sum[cprID[addEdge[j].second]]++; vec.push_back({addEdge[j].first, addEdge[j].second}); } } cprdfs(sum, vec, 0); // debug(vec.size()); bool flag = true; UnionFind uf(cprSize); int group = 0; // 入次数2 for (int j = 0; j < vec.size(); j++) { inedge[cprID[vec[j].first]]++; inedge[cprID[vec[j].second]]++; group += uf.set(cprID[vec[j].first], cprID[vec[j].second]); } for (int j = 0; j < vec.size(); j++) { flag &= inedge[cprID[vec[j].first]] == 2; flag &= inedge[cprID[vec[j].second]] == 2; } // 連結 flag &= group == vec.size() - 1; ans += flag; } printf("%d\n", ans); return 0; }
replace
163
164
163
164
0
cprSize = 5
p00991
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) #define MOD 100000007 using namespace std; typedef pair<int, int> P; int d[1000][1000], ans[1000][1000]; int dx[]{1, -1, 0, 0}, dy[]{0, 0, 1, -1}; int main() { int r, c, x1, y1, x2, y2; cin >> r >> c >> x1 >> y1 >> x2 >> y2; queue<P> que; memset(d, -1, sizeof(d)); ans[x1][y1] = 1; d[x1][y1] = 0; que.push(P(x1, y1)); while (!que.empty()) { P p = que.front(); que.pop(); rep(i, 4) { int nx = p.first + dx[i], ny = p.second + dy[i]; int a = 0; /* if(nx<0||nx>=r){ nx=(nx+r)%r;a++; } if(ny<0||ny>=c){ ny=(ny+c)%c;a++; } if(a==2)continue;*/ if (d[nx][ny] == -1) { d[nx][ny] = d[p.first][p.second] + 1; que.push(P(nx, ny)); } if (d[nx][ny] == d[p.first][p.second] + 1) (ans[nx][ny] += ans[p.first][p.second]) %= MOD; } } printf("%d\n", ans[x2][y2]); }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) #define MOD 100000007 using namespace std; typedef pair<int, int> P; int d[1000][1000], ans[1000][1000]; int dx[]{1, -1, 0, 0}, dy[]{0, 0, 1, -1}; int main() { int r, c, x1, y1, x2, y2; cin >> r >> c >> x1 >> y1 >> x2 >> y2; queue<P> que; memset(d, -1, sizeof(d)); ans[x1][y1] = 1; d[x1][y1] = 0; que.push(P(x1, y1)); while (!que.empty()) { P p = que.front(); que.pop(); rep(i, 4) { int nx = (p.first + dx[i] + r) % r, ny = (p.second + dy[i] + c) % c; if (d[nx][ny] == -1) { d[nx][ny] = d[p.first][p.second] + 1; que.push(P(nx, ny)); } if (d[nx][ny] == d[p.first][p.second] + 1) (ans[nx][ny] += ans[p.first][p.second]) %= MOD; } } printf("%d\n", ans[x2][y2]); }
replace
20
29
20
21
0
p00993
C++
Memory Limit Exceeded
#include <iostream> using namespace std; #define MAX_P 50000 int n; int dp[5010][MAX_P]; void fact(int n) { dp[0][0] = 1; for (int i = 1; i <= n; i++) { for (int j = 0; j < MAX_P; j++) { dp[i][j] += dp[i - 1][j] * i; if (dp[i][j] >= 10) { dp[i][j + 1] += dp[i][j] / 10; dp[i][j] %= 10; } } } dp[n][0] += 2; for (int j = 0; j < MAX_P; j++) { if (dp[n][j] >= 10) { dp[n][j + 1] += dp[n][j] / 10; dp[n][j] %= 10; } } } int main() { cin >> n; fact(n + 1); int ok = 0; for (int i = MAX_P - 1; i >= 0; i--) { if (dp[n + 1][i] >= 1) { ok = 1; } if (ok == 1) { cout << dp[n + 1][i]; } } cout << endl; for (int i = 2; i < n + 2; i++) { cout << i << endl; } return 0; }
#include <iostream> using namespace std; #define MAX_P 20000 int n; int dp[5010][MAX_P]; void fact(int n) { dp[0][0] = 1; for (int i = 1; i <= n; i++) { for (int j = 0; j < MAX_P; j++) { dp[i][j] += dp[i - 1][j] * i; if (dp[i][j] >= 10) { dp[i][j + 1] += dp[i][j] / 10; dp[i][j] %= 10; } } } dp[n][0] += 2; for (int j = 0; j < MAX_P; j++) { if (dp[n][j] >= 10) { dp[n][j + 1] += dp[n][j] / 10; dp[n][j] %= 10; } } } int main() { cin >> n; fact(n + 1); int ok = 0; for (int i = MAX_P - 1; i >= 0; i--) { if (dp[n + 1][i] >= 1) { ok = 1; } if (ok == 1) { cout << dp[n + 1][i]; } } cout << endl; for (int i = 2; i < n + 2; i++) { cout << i << endl; } return 0; }
replace
2
3
2
3
MLE
p00997
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define MAX 100000 class Union_Find { public: int par[MAX], rnk[MAX], sz[MAX], gnum; Union_Find(int N) { gnum = N; for (int i = 0; i < N; i++) { par[i] = i; rnk[i] = 0; sz[i] = 1; } } 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 (rnk[x] < rnk[y]) { par[x] = y; sz[y] += sz[x]; } else { par[y] = x; sz[x] += sz[y]; if (rnk[x] == rnk[y]) { rnk[x]++; } } gnum--; } bool same(int x, int y) { return (find(x) == find(y)); } int size(int x) { return sz[find(x)]; } int groups() { return gnum; } }; struct edge { int u, v, cost; }; bool comp(const edge &a, const edge &b) { return a.cost < b.cost; } using ll = long long; edge es[MAX]; ll solve(int N) { Union_Find uf(N); sort(es, es + N - 1, comp); ll res = 0; for (int i = 0; i < N - 1; i++) { edge &e = es[i]; if (!uf.same(e.u, e.v)) { res += (ll)uf.size(e.u) * uf.size(e.v) * e.cost; uf.unite(e.u, e.v); } } return res; } int main() { int N, a, b, c; cin >> N; for (int i = 0; i < N - 1; i++) { cin >> a >> b >> c; es[i] = {a, b, c}; } cout << solve(N) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define MAX 252521 class Union_Find { public: int par[MAX], rnk[MAX], sz[MAX], gnum; Union_Find(int N) { gnum = N; for (int i = 0; i < N; i++) { par[i] = i; rnk[i] = 0; sz[i] = 1; } } 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 (rnk[x] < rnk[y]) { par[x] = y; sz[y] += sz[x]; } else { par[y] = x; sz[x] += sz[y]; if (rnk[x] == rnk[y]) { rnk[x]++; } } gnum--; } bool same(int x, int y) { return (find(x) == find(y)); } int size(int x) { return sz[find(x)]; } int groups() { return gnum; } }; struct edge { int u, v, cost; }; bool comp(const edge &a, const edge &b) { return a.cost < b.cost; } using ll = long long; edge es[MAX]; ll solve(int N) { Union_Find uf(N); sort(es, es + N - 1, comp); ll res = 0; for (int i = 0; i < N - 1; i++) { edge &e = es[i]; if (!uf.same(e.u, e.v)) { res += (ll)uf.size(e.u) * uf.size(e.v) * e.cost; uf.unite(e.u, e.v); } } return res; } int main() { int N, a, b, c; cin >> N; for (int i = 0; i < N - 1; i++) { cin >> a >> b >> c; es[i] = {a, b, c}; } cout << solve(N) << endl; return 0; }
replace
4
5
4
5
0
p00997
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 = 1000000007; int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; struct UnionFind { vector<int> data; UnionFind(int N) : data(N, -1) {} bool uni(int x, int y) { x = root(x); y = root(y); if (x != y) { if (data[x] > data[y]) swap(x, y); data[x] += data[y]; data[y] = x; } return x != y; } bool find(int x, int y) { return root(x) == root(y); } int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); } int size(int x) { return -data[root(x)]; } }; struct edge { int src, dst, cost; edge(int s, int d, int c) : src(s), dst(d), cost(c) {} edge() {} bool operator<(const edge &e) const { return cost < e.cost; } }; int main() { int N; cin >> N; vector<edge> v(N - 1); REP(i, N - 1) { int a, b, c; cin >> a >> b >> c; v[i] = edge(a, b, c); } UnionFind uf(N); sort(v.begin(), v.end()); ll ans = 0; REP(i, N) { ans += (ll)uf.size(v[i].src) * uf.size(v[i].dst) * v[i].cost; uf.uni(v[i].src, v[i].dst); } cout << ans << 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 = 1000000007; int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; struct UnionFind { vector<int> data; UnionFind(int N) : data(N, -1) {} bool uni(int x, int y) { x = root(x); y = root(y); if (x != y) { if (data[x] > data[y]) swap(x, y); data[x] += data[y]; data[y] = x; } return x != y; } bool find(int x, int y) { return root(x) == root(y); } int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); } int size(int x) { return -data[root(x)]; } }; struct edge { int src, dst, cost; edge(int s, int d, int c) : src(s), dst(d), cost(c) {} edge() {} bool operator<(const edge &e) const { return cost < e.cost; } }; int main() { int N; cin >> N; vector<edge> v(N - 1); REP(i, N - 1) { int a, b, c; cin >> a >> b >> c; v[i] = edge(a, b, c); } UnionFind uf(N); sort(v.begin(), v.end()); ll ans = 0; REP(i, N - 1) { ans += (ll)uf.size(v[i].src) * uf.size(v[i].dst) * v[i].cost; uf.uni(v[i].src, v[i].dst); } cout << ans << endl; return 0; }
replace
76
77
76
77
0
p00998
C++
Time Limit Exceeded
#define _USE_MATH_DEFINES #include <algorithm> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <string> #include <time.h> #include <vector> using namespace std; #define rep(i, N) for (int i = 0; i < N; i++) #define pb push_back typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> i_i; typedef pair<ll, int> ll_i; typedef pair<int, ll> i_ll; typedef pair<double, int> d_i; typedef pair<ll, ll> ll_ll; typedef pair<double, double> d_d; struct edge { int v; ll w; }; const int MOD = 1000000007; ll _MOD = 1000000009; double EPS = 1e-10; int INF = INT_MAX / 2; struct Splay { Splay *lch, *rch; int d, val; Splay(int _val = 0) : lch(0), rch(0), d(1), val(_val) {} }; typedef pair<Splay *, Splay *> P; Splay *update(Splay *x) { x->d = x->lch->d + x->rch->d; x->val = min(x->lch->val, x->rch->val); return x; } Splay *rotr(Splay *x) { Splay *y = x->lch; x->lch = y->rch; y->rch = x; update(x); return update(y); } Splay *rotl(Splay *x) { Splay *y = x->rch; x->rch = y->lch; y->lch = x; update(x); return update(y); } /* void propagate(Splay* x) { x->lch->dval += x->dval; x->rch->dval += x->dval; x->dval = 0; } */ Splay *splay(Splay *x, int m) { if (m == 0 || m == x->d) return x; int d1 = x->lch->d; if (m < d1) { int d2 = x->lch->lch->d; if (m < d2) { x->lch->lch = splay(x->lch->lch, m); return rotr(rotr(x)); } else if (m > d2) { x->lch->rch = splay(x->lch->rch, m - d2); x->lch = rotl(x->lch); return rotr(x); } else { x->lch = splay(x->lch, m); return rotr(x); } } else if (m > d1) { int d2 = x->rch->lch->d; if (false && m < d1 + d2) { } else if (false && m > d1 + d2) { } else { x->rch = splay(x->rch, m - d1); return rotl(x); } } else return x; } Splay *merge(Splay *x, Splay *y) { if (!x) return y; if (!y) return x; Splay *z = new Splay(); z->lch = x; z->rch = y; return update(z); } Splay *merges(vector<Splay *> xs) { Splay *x = NULL; for (Splay *y : xs) x = merge(x, y); return x; } P split(Splay *x, int m) { if (m == 0) return P(NULL, x); if (m == x->d) return P(x, NULL); x = splay(x, m); P p(x->lch, x->rch); delete x; return p; } vector<Splay *> splits(Splay *x, vector<int> is) { reverse(is.begin(), is.end()); vector<Splay *> xs; for (int i : is) { P p = split(x, i); x = p.first; xs.pb(p.second); } xs.pb(x); reverse(xs.begin(), xs.end()); return xs; } int get_val(Splay *&x, int i) { P p = split(x, i); cout << p.second->d << endl; P q = split(p.second, 1); cout << q.first << endl; int val = q.first->val; x = merge(p.first, merge(q.first, q.second)); return val; } int main() { int N, Q; cin >> N >> Q; vector<int> a(N); rep(i, N) scanf("%d", &a[i]); Splay *x = NULL; rep(i, N) x = merge(x, new Splay(a[i])); while (Q--) { int q; scanf("%d", &q); if (q == 0) { int l, r; scanf("%d%d", &l, &r); r++; vector<Splay *> xs = splits(x, {l, r - 1, r}); swap(xs[1], xs[2]); x = merges(xs); } if (q == 1) { int l, r; scanf("%d%d", &l, &r); r++; vector<Splay *> xs = splits(x, {l, r}); int val = xs[1]->val; x = merges(xs); printf("%d\n", val); } if (q == 2) { int i, val; scanf("%d%d", &i, &val); vector<Splay *> xs = splits(x, {i, i + 1}); xs[1]->val = val; x = merges(xs); } } }
#define _USE_MATH_DEFINES #include <algorithm> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <string> #include <time.h> #include <vector> using namespace std; #define rep(i, N) for (int i = 0; i < N; i++) #define pb push_back typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> i_i; typedef pair<ll, int> ll_i; typedef pair<int, ll> i_ll; typedef pair<double, int> d_i; typedef pair<ll, ll> ll_ll; typedef pair<double, double> d_d; struct edge { int v; ll w; }; const int MOD = 1000000007; ll _MOD = 1000000009; double EPS = 1e-10; int INF = INT_MAX / 2; struct Splay { Splay *lch, *rch; int d, val; Splay(int _val = 0) : lch(0), rch(0), d(1), val(_val) {} }; typedef pair<Splay *, Splay *> P; Splay *update(Splay *x) { x->d = x->lch->d + x->rch->d; x->val = min(x->lch->val, x->rch->val); return x; } Splay *rotr(Splay *x) { Splay *y = x->lch; x->lch = y->rch; y->rch = x; update(x); return update(y); } Splay *rotl(Splay *x) { Splay *y = x->rch; x->rch = y->lch; y->lch = x; update(x); return update(y); } /* void propagate(Splay* x) { x->lch->dval += x->dval; x->rch->dval += x->dval; x->dval = 0; } */ Splay *splay(Splay *x, int m) { if (m == 0 || m == x->d) return x; int d1 = x->lch->d; if (m < d1) { int d2 = x->lch->lch->d; if (m < d2) { x->lch->lch = splay(x->lch->lch, m); return rotr(rotr(x)); } else if (m > d2) { x->lch->rch = splay(x->lch->rch, m - d2); x->lch = rotl(x->lch); return rotr(x); } else { x->lch = splay(x->lch, m); return rotr(x); } } else if (m > d1) { int d2 = x->rch->lch->d; if (m < d1 + d2) { x->rch->lch = splay(x->rch->lch, m - d1); x->rch = rotr(x->rch); return rotl(x); } else if (m > d1 + d2) { x->rch->rch = splay(x->rch->rch, m - d1 - d2); return rotl(rotl(x)); } else { x->rch = splay(x->rch, m - d1); return rotl(x); } } else return x; } Splay *merge(Splay *x, Splay *y) { if (!x) return y; if (!y) return x; Splay *z = new Splay(); z->lch = x; z->rch = y; return update(z); } Splay *merges(vector<Splay *> xs) { Splay *x = NULL; for (Splay *y : xs) x = merge(x, y); return x; } P split(Splay *x, int m) { if (m == 0) return P(NULL, x); if (m == x->d) return P(x, NULL); x = splay(x, m); P p(x->lch, x->rch); delete x; return p; } vector<Splay *> splits(Splay *x, vector<int> is) { reverse(is.begin(), is.end()); vector<Splay *> xs; for (int i : is) { P p = split(x, i); x = p.first; xs.pb(p.second); } xs.pb(x); reverse(xs.begin(), xs.end()); return xs; } int get_val(Splay *&x, int i) { P p = split(x, i); cout << p.second->d << endl; P q = split(p.second, 1); cout << q.first << endl; int val = q.first->val; x = merge(p.first, merge(q.first, q.second)); return val; } int main() { int N, Q; cin >> N >> Q; vector<int> a(N); rep(i, N) scanf("%d", &a[i]); Splay *x = NULL; rep(i, N) x = merge(x, new Splay(a[i])); while (Q--) { int q; scanf("%d", &q); if (q == 0) { int l, r; scanf("%d%d", &l, &r); r++; vector<Splay *> xs = splits(x, {l, r - 1, r}); swap(xs[1], xs[2]); x = merges(xs); } if (q == 1) { int l, r; scanf("%d%d", &l, &r); r++; vector<Splay *> xs = splits(x, {l, r}); int val = xs[1]->val; x = merges(xs); printf("%d\n", val); } if (q == 2) { int i, val; scanf("%d%d", &i, &val); vector<Splay *> xs = splits(x, {i, i + 1}); xs[1]->val = val; x = merges(xs); } } }
replace
99
103
99
106
TLE
p00998
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; template <typename T> constexpr T INF = numeric_limits<T>::max() / 10; template <typename Monoid> struct AccRandomizedBinarySearchTree { public: static constexpr int POOL_SIZE = 200000; private: struct Node { using BaseMonoid = Monoid; using T = typename BaseMonoid::T; using Ptr = Node *; using PtrPair = pair<Ptr, Ptr>; Node() : left{}, right{}, value{}, accum{}, size{1} {} Node(const T &value) : left{}, right{}, value{value}, accum{value}, size{1} {} Ptr left, right; T value; T accum; int size; static void serialize(Ptr a, vector<T> &v) { if (a == nullptr) { return; } else { serialize(a->left, v); v.push_back(a->value); serialize(a->right, v); } } static unsigned xor128() { static unsigned x = 123456789; static unsigned y = 362436069; static unsigned z = 521288629; static unsigned w = 88675123; const unsigned t = x ^ (x << 11); x = y; y = z; z = w; return w = (w ^ (w << 19)) ^ (t ^ (t >> 8)); } static unsigned getRand(const int maximum) { return xor128() % maximum; } static T acc(const T &a, const T &b) { static const BaseMonoid ACC{}; return ACC(a, b); } static int getSize(const Ptr a) { return (a == nullptr) ? 0 : a->size; } static T getAcc(const Ptr a) { return (a == nullptr) ? BaseMonoid::identity() : a->accum; } static Ptr update(Ptr a) { a->size = getSize(a->left) + getSize(a->right) + 1; a->accum = acc(acc(getAcc(a->left), getAcc(a->right)), a->value); return a; } static Ptr merge(Ptr a, Ptr b) { if (a == nullptr) { return b; } else if (b == nullptr) { return a; } const int asize = getSize(a); const int bsize = getSize(b); if (getRand(asize + bsize) < asize) { a->right = merge(a->right, b); return update(a); } else { b->left = merge(a, b->left); return update(b); } } static PtrPair split(Ptr a, const int k) // size = (k,n-k) { if (a == nullptr) { return make_pair(Ptr{}, Ptr{}); } if (k <= getSize(a->left)) { const PtrPair s = split(a->left, k); a->left = s.second; return make_pair(s.first, update(a)); } else { const PtrPair s = split(a->right, k - getSize(a->left) - 1); a->right = s.first; return make_pair(update(a), s.second); } } static Ptr insert(Ptr a, const int k, Ptr b) { const PtrPair s = split(a, k); return merge(merge(s.first, b), s.second); } static PtrPair erase(Ptr a, const int k) { const PtrPair sr = split(a, k + 1); const PtrPair sl = split(sr.first, k); return make_pair(merge(sl.first, sr.second), update(sl.second)); } }; using Ptr = typename Node::Ptr; using PtrPair = typename Node::PtrPair; int head = 0; static Node pool[POOL_SIZE]; Ptr alloc(const typename Node::T &x) { assert(head < POOL_SIZE); pool[head].value = x; pool[head].size = 1; pool[head].left = nullptr; pool[head].right = nullptr; pool[head].accum = x; head++; return pool + head - 1; } void garbage_collect() { vector<T> v; Node::serialize(root, v); head = 0; root = nullptr; rev_root = nullptr; for (int i = 0; i < v.size(); i++) { root = Node::insert(root, i, alloc(v[i])); if (use_reverse) { rev_root = Node::insert(rev_root, v.size() - i - 1, alloc(v[i])); } } } Ptr root; Ptr rev_root; const bool use_reverse; public: using BaseMonoid = Monoid; using T = typename Node::T; AccRandomizedBinarySearchTree(const bool reversible = true) : root{}, rev_root{}, use_reverse{reversible} {} int getSize() const { return Node::getSize(root); } void insert(const int k, const T &value) { const int s = Node::getSize(root); assert(0 <= k); assert(k <= s); if (head == POOL_SIZE) { garbage_collect(); } root = Node::insert(root, k, alloc(value)); if (use_reverse) { rev_root = Node::insert(rev_root, s - k, alloc(value)); } } void erase(const int k) { const int s = Node::getSize(root); assert(0 <= k); assert(k < s); Ptr p; tie(root, p) = Node::erase(root, k); if (p != nullptr) { p->left = Ptr{}; p->right = Ptr{}; } if (use_reverse) { tie(rev_root, p) = Node::erase(rev_root, s - k - 1); if (p != nullptr) { p->left = Ptr{}; p->right = Ptr{}; } } } T get(int k) const { assert(0 <= k); assert(k < Node::getSize(root)); Ptr v = root; while (v != nullptr) { const int s = Node::getSize(v->left); if (s > k) { v = v->left; } else if (s == k) { return v->value; } else { v = v->right; k -= s + 1; } } return v->value; } void set(const int k, const T &value) { const int s = Node::getSize(root); assert(0 <= k); assert(k < s); const PtrPair sr = Node::split(root, k + 1); const PtrPair sl = Node::split(sr.first, k); const Ptr lr = sl.second; lr->value = value; lr->accum = value; root = Node::merge(Node::merge(sl.first, lr), sr.second); if (use_reverse) { const PtrPair rev_sr = Node::split(rev_root, s - k); const PtrPair rev_sl = Node::split(rev_sr.first, s - k - 1); const Ptr rev_lr = rev_sl.second; rev_lr->value = value; rev_lr->accum = value; rev_root = Node::merge(Node::merge(rev_sl.first, rev_lr), rev_sr.second); } } T accumulate(const int l, const int r) // [l,r) { assert(0 <= l); assert(l < r); assert(r <= Node::getSize(root)); const PtrPair sr = Node::split(root, r); const PtrPair sl = Node::split(sr.first, l); const T ans = Node::getAcc(sl.second); root = Node::merge(Node::merge(sl.first, sl.second), sr.second); return ans; } void reverse(const int l, const int r) // [l,r) { assert(reverse); const int s = Node::getSize(root); assert(0 <= l); assert(l < r); assert(r <= s); const int width = r - l; if (width > 1) { const PtrPair sr = Node::split(root, r); const PtrPair sl = Node::split(sr.first, l); const PtrPair rev_sr = Node::split(rev_root, s - l); const PtrPair rev_sl = Node::split(rev_sr.first, s - r); root = Node::merge(Node::merge(sl.first, rev_sl.second), sr.second); rev_root = Node::merge(Node::merge(rev_sl.first, sl.second), rev_sr.second); } } void shiftRight(const int l, const int r, const int count) // [l,r) { const int s = Node::getSize(root); assert(0 <= l); assert(l < r); assert(r <= s); const int width = r - l; const int shift = count % width; if (shift > 0) { const PtrPair sr = Node::split(root, r); PtrPair sl = Node::split(sr.first, l); const PtrPair sm = Node::split(sl.second, width - shift); root = Node::merge(Node::merge(sl.first, sm.second), Node::merge(sm.first, sr.second)); if (use_reverse) { const PtrPair rev_sr = Node::split(rev_root, s - l); PtrPair rev_sl = Node::split(rev_sr.first, s - r); const PtrPair rev_sm = Node::split(rev_sl.second, shift); rev_root = Node::merge(Node::merge(rev_sl.first, rev_sm.second), Node::merge(rev_sm.first, rev_sr.second)); } } } void shiftLeft(const int l, const int r, const int count) // [l,r) { assert(0 <= l); assert(l < r); assert(r <= Node::getSize(root)); const int width = r - l; const int shift = count % width; if (shift > 0) { shiftRight(l, r, width - shift); } } }; template <typename T> typename AccRandomizedBinarySearchTree<T>::Node AccRandomizedBinarySearchTree< T>::pool[AccRandomizedBinarySearchTree::POOL_SIZE]; template <typename T> ostream &operator<<(ostream &os, const AccRandomizedBinarySearchTree<T> &rbst) { os << "["; for (int i = 0; i < rbst.getSize(); i++) { os << rbst.get(i) << ","; } os << "]" << endl; return os; } struct RBST_Monoid_Min { using T = int; T operator()(const T &a, const T &b) const { return min(a, b); } static constexpr T identity() { return INF<T>; } }; using RBST = AccRandomizedBinarySearchTree<RBST_Monoid_Min>; int main() { cin.tie(0); ios::sync_with_stdio(false); int N, Q; cin >> N >> Q; RBST rbst; for (int i = 0; i < N; i++) { int a; cin >> a; rbst.insert(i, a); } for (int q = 0; q < Q; q++) { int x, y, z; cin >> x >> y >> z; if (x == 0) { rbst.shiftRight(y, z + 1, 1); } else if (x == 1) { cout << rbst.accumulate(y, z + 1) << "\n"; } else { rbst.set(y, z); } } return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T> constexpr T INF = numeric_limits<T>::max() / 10; template <typename Monoid> struct AccRandomizedBinarySearchTree { public: static constexpr int POOL_SIZE = 200000; private: struct Node { using BaseMonoid = Monoid; using T = typename BaseMonoid::T; using Ptr = Node *; using PtrPair = pair<Ptr, Ptr>; Node() : left{}, right{}, value{}, accum{}, size{1} {} Node(const T &value) : left{}, right{}, value{value}, accum{value}, size{1} {} Ptr left, right; T value; T accum; int size; static void serialize(Ptr a, vector<T> &v) { if (a == nullptr) { return; } else { serialize(a->left, v); v.push_back(a->value); serialize(a->right, v); } } static unsigned xor128() { static unsigned x = 123456789; static unsigned y = 362436069; static unsigned z = 521288629; static unsigned w = 88675123; const unsigned t = x ^ (x << 11); x = y; y = z; z = w; return w = (w ^ (w << 19)) ^ (t ^ (t >> 8)); } static unsigned getRand(const int maximum) { return xor128() % maximum; } static T acc(const T &a, const T &b) { static const BaseMonoid ACC{}; return ACC(a, b); } static int getSize(const Ptr a) { return (a == nullptr) ? 0 : a->size; } static T getAcc(const Ptr a) { return (a == nullptr) ? BaseMonoid::identity() : a->accum; } static Ptr update(Ptr a) { a->size = getSize(a->left) + getSize(a->right) + 1; a->accum = acc(acc(getAcc(a->left), getAcc(a->right)), a->value); return a; } static Ptr merge(Ptr a, Ptr b) { if (a == nullptr) { return b; } else if (b == nullptr) { return a; } const int asize = getSize(a); const int bsize = getSize(b); if (getRand(asize + bsize) < asize) { a->right = merge(a->right, b); return update(a); } else { b->left = merge(a, b->left); return update(b); } } static PtrPair split(Ptr a, const int k) // size = (k,n-k) { if (a == nullptr) { return make_pair(Ptr{}, Ptr{}); } if (k <= getSize(a->left)) { const PtrPair s = split(a->left, k); a->left = s.second; return make_pair(s.first, update(a)); } else { const PtrPair s = split(a->right, k - getSize(a->left) - 1); a->right = s.first; return make_pair(update(a), s.second); } } static Ptr insert(Ptr a, const int k, Ptr b) { const PtrPair s = split(a, k); return merge(merge(s.first, b), s.second); } static PtrPair erase(Ptr a, const int k) { const PtrPair sr = split(a, k + 1); const PtrPair sl = split(sr.first, k); return make_pair(merge(sl.first, sr.second), update(sl.second)); } }; using Ptr = typename Node::Ptr; using PtrPair = typename Node::PtrPair; int head = 0; static Node pool[POOL_SIZE]; Ptr alloc(const typename Node::T &x) { assert(head < POOL_SIZE); pool[head].value = x; pool[head].size = 1; pool[head].left = nullptr; pool[head].right = nullptr; pool[head].accum = x; head++; return pool + head - 1; } void garbage_collect() { vector<T> v; Node::serialize(root, v); head = 0; root = nullptr; rev_root = nullptr; for (int i = 0; i < v.size(); i++) { root = Node::insert(root, i, alloc(v[i])); if (use_reverse) { rev_root = Node::insert(rev_root, v.size() - i - 1, alloc(v[i])); } } } Ptr root; Ptr rev_root; const bool use_reverse; public: using BaseMonoid = Monoid; using T = typename Node::T; AccRandomizedBinarySearchTree(const bool reversible = true) : root{}, rev_root{}, use_reverse{reversible} {} int getSize() const { return Node::getSize(root); } void insert(const int k, const T &value) { const int s = Node::getSize(root); assert(0 <= k); assert(k <= s); if (head == POOL_SIZE) { garbage_collect(); } root = Node::insert(root, k, alloc(value)); if (use_reverse) { rev_root = Node::insert(rev_root, s - k, alloc(value)); } } void erase(const int k) { const int s = Node::getSize(root); assert(0 <= k); assert(k < s); Ptr p; tie(root, p) = Node::erase(root, k); if (p != nullptr) { p->left = Ptr{}; p->right = Ptr{}; } if (use_reverse) { tie(rev_root, p) = Node::erase(rev_root, s - k - 1); if (p != nullptr) { p->left = Ptr{}; p->right = Ptr{}; } } } T get(int k) const { assert(0 <= k); assert(k < Node::getSize(root)); Ptr v = root; while (v != nullptr) { const int s = Node::getSize(v->left); if (s > k) { v = v->left; } else if (s == k) { return v->value; } else { v = v->right; k -= s + 1; } } return v->value; } void set(const int k, const T &value) { const int s = Node::getSize(root); assert(0 <= k); assert(k < s); const PtrPair sr = Node::split(root, k + 1); const PtrPair sl = Node::split(sr.first, k); const Ptr lr = sl.second; lr->value = value; lr->accum = value; root = Node::merge(Node::merge(sl.first, lr), sr.second); if (use_reverse) { const PtrPair rev_sr = Node::split(rev_root, s - k); const PtrPair rev_sl = Node::split(rev_sr.first, s - k - 1); const Ptr rev_lr = rev_sl.second; rev_lr->value = value; rev_lr->accum = value; rev_root = Node::merge(Node::merge(rev_sl.first, rev_lr), rev_sr.second); } } T accumulate(const int l, const int r) // [l,r) { assert(0 <= l); assert(l < r); assert(r <= Node::getSize(root)); const PtrPair sr = Node::split(root, r); const PtrPair sl = Node::split(sr.first, l); const T ans = Node::getAcc(sl.second); root = Node::merge(Node::merge(sl.first, sl.second), sr.second); return ans; } void reverse(const int l, const int r) // [l,r) { assert(reverse); const int s = Node::getSize(root); assert(0 <= l); assert(l < r); assert(r <= s); const int width = r - l; if (width > 1) { const PtrPair sr = Node::split(root, r); const PtrPair sl = Node::split(sr.first, l); const PtrPair rev_sr = Node::split(rev_root, s - l); const PtrPair rev_sl = Node::split(rev_sr.first, s - r); root = Node::merge(Node::merge(sl.first, rev_sl.second), sr.second); rev_root = Node::merge(Node::merge(rev_sl.first, sl.second), rev_sr.second); } } void shiftRight(const int l, const int r, const int count) // [l,r) { const int s = Node::getSize(root); assert(0 <= l); assert(l < r); assert(r <= s); const int width = r - l; const int shift = count % width; if (shift > 0) { const PtrPair sr = Node::split(root, r); PtrPair sl = Node::split(sr.first, l); const PtrPair sm = Node::split(sl.second, width - shift); root = Node::merge(Node::merge(sl.first, sm.second), Node::merge(sm.first, sr.second)); if (use_reverse) { const PtrPair rev_sr = Node::split(rev_root, s - l); PtrPair rev_sl = Node::split(rev_sr.first, s - r); const PtrPair rev_sm = Node::split(rev_sl.second, shift); rev_root = Node::merge(Node::merge(rev_sl.first, rev_sm.second), Node::merge(rev_sm.first, rev_sr.second)); } } } void shiftLeft(const int l, const int r, const int count) // [l,r) { assert(0 <= l); assert(l < r); assert(r <= Node::getSize(root)); const int width = r - l; const int shift = count % width; if (shift > 0) { shiftRight(l, r, width - shift); } } }; template <typename T> typename AccRandomizedBinarySearchTree<T>::Node AccRandomizedBinarySearchTree< T>::pool[AccRandomizedBinarySearchTree::POOL_SIZE]; template <typename T> ostream &operator<<(ostream &os, const AccRandomizedBinarySearchTree<T> &rbst) { os << "["; for (int i = 0; i < rbst.getSize(); i++) { os << rbst.get(i) << ","; } os << "]" << endl; return os; } struct RBST_Monoid_Min { using T = int; T operator()(const T &a, const T &b) const { return min(a, b); } static constexpr T identity() { return INF<T>; } }; using RBST = AccRandomizedBinarySearchTree<RBST_Monoid_Min>; int main() { cin.tie(0); ios::sync_with_stdio(false); int N, Q; cin >> N >> Q; RBST rbst(false); for (int i = 0; i < N; i++) { int a; cin >> a; rbst.insert(i, a); } for (int q = 0; q < Q; q++) { int x, y, z; cin >> x >> y >> z; if (x == 0) { rbst.shiftRight(y, z + 1, 1); } else if (x == 1) { cout << rbst.accumulate(y, z + 1) << "\n"; } else { rbst.set(y, z); } } return 0; }
replace
308
309
308
309
0
p00998
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; template <typename T> constexpr T INF = numeric_limits<T>::max() / 10; template <typename Monoid> struct AccRandomizedBinarySearchTree { public: static constexpr int POOL_SIZE = 200001; private: struct Node { using BaseMonoid = Monoid; using T = typename BaseMonoid::T; using Ptr = Node *; using PtrPair = pair<Ptr, Ptr>; Node() : left{}, right{}, value{}, accum{}, size{1} {} Node(const T &value) : left{}, right{}, value{value}, accum{value}, size{1} {} Ptr left, right; T value; T accum; int size; static void serialize(Ptr a, vector<T> &v) { if (a == nullptr) { return; } else { serialize(a->left, v); v.push_back(a->value); serialize(a->right, v); } } static unsigned xor128() { static unsigned x = 123456789; static unsigned y = 362436069; static unsigned z = 521288629; static unsigned w = 88675123; const unsigned t = x ^ (x << 11); x = y; y = z; z = w; return w = (w ^ (w << 19)) ^ (t ^ (t >> 8)); } static unsigned getRand(const int maximum) { return xor128() % maximum; } static T acc(const T &a, const T &b) { static const BaseMonoid ACC{}; return ACC(a, b); } static int getSize(const Ptr a) { return (a == nullptr) ? 0 : a->size; } static T getAcc(const Ptr a) { return (a == nullptr) ? BaseMonoid::identity() : a->accum; } static Ptr update(Ptr a) { a->size = getSize(a->left) + getSize(a->right) + 1; a->accum = acc(acc(getAcc(a->left), getAcc(a->right)), a->value); return a; } static Ptr merge(Ptr a, Ptr b) { if (a == nullptr) { return b; } else if (b == nullptr) { return a; } const int asize = getSize(a); const int bsize = getSize(b); if (getRand(asize + bsize) < asize) { a->right = merge(a->right, b); return update(a); } else { b->left = merge(a, b->left); return update(b); } } static PtrPair split(Ptr a, const int k) // size = (k,n-k) { if (a == nullptr) { return make_pair(Ptr{}, Ptr{}); } if (k <= getSize(a->left)) { const PtrPair s = split(a->left, k); a->left = s.second; return make_pair(s.first, update(a)); } else { const PtrPair s = split(a->right, k - getSize(a->left) - 1); a->right = s.first; return make_pair(update(a), s.second); } } static Ptr insert(Ptr a, const int k, Ptr b) { const PtrPair s = split(a, k); return merge(merge(s.first, b), s.second); } static PtrPair erase(Ptr a, const int k) { const PtrPair sr = split(a, k + 1); const PtrPair sl = split(sr.first, k); return make_pair(merge(sl.first, sr.second), update(sl.second)); } }; using Ptr = typename Node::Ptr; using PtrPair = typename Node::PtrPair; int head = 0; static Node pool[POOL_SIZE]; Ptr alloc(const typename Node::T &x) { assert(head < POOL_SIZE); pool[head].value = x; pool[head].size = 1; pool[head].left = nullptr; pool[head].right = nullptr; pool[head].accum = x; head++; return pool + head - 1; } void garbage_collect() { vector<T> v; Node::serialize(root, v); head = 0; root = nullptr; rev_root = nullptr; for (int i = 0; i < v.size(); i++) { root = Node::insert(root, i, alloc(v[i])); rev_root = Node::insert(rev_root, v.size() - i - 1, alloc(v[i])); } } Ptr root; Ptr rev_root; public: using BaseMonoid = Monoid; using T = typename Node::T; AccRandomizedBinarySearchTree() : root{}, rev_root{} {} int getSize() const { return Node::getSize(root); } void insert(const int k, const T &value) { const int s = Node::getSize(root); assert(0 <= k); assert(k <= s); if (head == POOL_SIZE) { garbage_collect(); } root = Node::insert(root, k, alloc(value)); rev_root = Node::insert(rev_root, s - k, alloc(value)); } void erase(const int k) { const int s = Node::getSize(root); assert(0 <= k); assert(k < s); Ptr p; tie(root, p) = Node::erase(root, k); if (p != nullptr) { p->left = Ptr{}; p->right = Ptr{}; } tie(rev_root, p) = Node::erase(rev_root, s - k - 1); if (p != nullptr) { p->left = Ptr{}; p->right = Ptr{}; } } T get(int k) const { assert(0 <= k); assert(k < Node::getSize(root)); Ptr v = root; while (v != nullptr) { const int s = Node::getSize(v->left); if (s > k) { v = v->left; } else if (s == k) { return v->value; } else { v = v->right; k -= s + 1; } } return v->value; } void set(const int k, const T &value) { const int s = Node::getSize(root); assert(0 <= k); assert(k < s); const PtrPair sr = Node::split(root, k + 1); const PtrPair sl = Node::split(sr.first, k); const Ptr lr = sl.second; lr->value = value; lr->accum = value; root = Node::merge(Node::merge(sl.first, lr), sr.second); const PtrPair rev_sr = Node::split(rev_root, s - k); const PtrPair rev_sl = Node::split(rev_sr.first, s - k - 1); const Ptr rev_lr = rev_sl.second; rev_lr->value = value; rev_lr->accum = value; rev_root = Node::merge(Node::merge(rev_sl.first, rev_lr), rev_sr.second); } T accumulate(const int l, const int r) // [l,r) { assert(0 <= l); assert(l < r); assert(r <= Node::getSize(root)); const PtrPair sr = Node::split(root, r); const PtrPair sl = Node::split(sr.first, l); const T ans = Node::getAcc(sl.second); root = Node::merge(Node::merge(sl.first, sl.second), sr.second); return ans; } void reverse(const int l, const int r) // [l,r) { const int s = Node::getSize(root); assert(0 <= l); assert(l < r); assert(r <= s); const int width = r - l; if (width > 1) { const PtrPair sr = Node::split(root, r); const PtrPair sl = Node::split(sr.first, l); const PtrPair rev_sr = Node::split(rev_root, s - l); const PtrPair rev_sl = Node::split(rev_sr.first, s - r); root = Node::merge(Node::merge(sl.first, rev_sl.second), sr.second); rev_root = Node::merge(Node::merge(rev_sl.first, sl.second), rev_sr.second); } } void shiftRight(const int l, const int r, const int count) // [l,r) { const int s = Node::getSize(root); assert(0 <= l); assert(l < r); assert(r <= s); const int width = r - l; const int shift = count % width; if (shift > 0) { const PtrPair sr = Node::split(root, r); PtrPair sl = Node::split(sr.first, l); const PtrPair sm = Node::split(sl.second, width - shift); const PtrPair rev_sr = Node::split(rev_root, s - l); PtrPair rev_sl = Node::split(rev_sr.first, s - r); const PtrPair rev_sm = Node::split(rev_sl.second, shift); root = Node::merge(Node::merge(sl.first, sm.second), Node::merge(sm.first, sr.second)); rev_root = Node::merge(Node::merge(rev_sl.first, rev_sm.second), Node::merge(rev_sm.first, rev_sr.second)); } } void shiftLeft(const int l, const int r, const int count) // [l,r) { assert(0 <= l); assert(l < r); assert(r <= Node::getSize(root)); const int width = r - l; const int shift = count % width; if (shift > 0) { shiftRight(l, r, width - shift); } } }; template <typename T> typename AccRandomizedBinarySearchTree<T>::Node AccRandomizedBinarySearchTree< T>::pool[AccRandomizedBinarySearchTree::POOL_SIZE]; template <typename T> ostream &operator<<(ostream &os, const AccRandomizedBinarySearchTree<T> &rbst) { os << "["; for (int i = 0; i < rbst.getSize(); i++) { os << rbst.get(i) << ","; } os << "]" << endl; return os; } struct RBST_Monoid_Min { using T = int; T operator()(const T &a, const T &b) const { return min(a, b); } static constexpr T identity() { return INF<T>; } }; using RBST = AccRandomizedBinarySearchTree<RBST_Monoid_Min>; int main() { cin.tie(0); ios::sync_with_stdio(false); int N, Q; cin >> N >> Q; RBST rbst; for (int i = 0; i < N; i++) { int a; cin >> a; rbst.insert(i, a); } for (int q = 0; q < Q; q++) { int x, y, z; cin >> x >> y >> z; if (x == 0) { rbst.shiftRight(y, z + 1, 1); } else if (x == 1) { cout << rbst.accumulate(y, z + 1) << "\n"; } else { rbst.set(y, z); } } return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T> constexpr T INF = numeric_limits<T>::max() / 10; template <typename Monoid> struct AccRandomizedBinarySearchTree { public: static constexpr int POOL_SIZE = 400000; private: struct Node { using BaseMonoid = Monoid; using T = typename BaseMonoid::T; using Ptr = Node *; using PtrPair = pair<Ptr, Ptr>; Node() : left{}, right{}, value{}, accum{}, size{1} {} Node(const T &value) : left{}, right{}, value{value}, accum{value}, size{1} {} Ptr left, right; T value; T accum; int size; static void serialize(Ptr a, vector<T> &v) { if (a == nullptr) { return; } else { serialize(a->left, v); v.push_back(a->value); serialize(a->right, v); } } static unsigned xor128() { static unsigned x = 123456789; static unsigned y = 362436069; static unsigned z = 521288629; static unsigned w = 88675123; const unsigned t = x ^ (x << 11); x = y; y = z; z = w; return w = (w ^ (w << 19)) ^ (t ^ (t >> 8)); } static unsigned getRand(const int maximum) { return xor128() % maximum; } static T acc(const T &a, const T &b) { static const BaseMonoid ACC{}; return ACC(a, b); } static int getSize(const Ptr a) { return (a == nullptr) ? 0 : a->size; } static T getAcc(const Ptr a) { return (a == nullptr) ? BaseMonoid::identity() : a->accum; } static Ptr update(Ptr a) { a->size = getSize(a->left) + getSize(a->right) + 1; a->accum = acc(acc(getAcc(a->left), getAcc(a->right)), a->value); return a; } static Ptr merge(Ptr a, Ptr b) { if (a == nullptr) { return b; } else if (b == nullptr) { return a; } const int asize = getSize(a); const int bsize = getSize(b); if (getRand(asize + bsize) < asize) { a->right = merge(a->right, b); return update(a); } else { b->left = merge(a, b->left); return update(b); } } static PtrPair split(Ptr a, const int k) // size = (k,n-k) { if (a == nullptr) { return make_pair(Ptr{}, Ptr{}); } if (k <= getSize(a->left)) { const PtrPair s = split(a->left, k); a->left = s.second; return make_pair(s.first, update(a)); } else { const PtrPair s = split(a->right, k - getSize(a->left) - 1); a->right = s.first; return make_pair(update(a), s.second); } } static Ptr insert(Ptr a, const int k, Ptr b) { const PtrPair s = split(a, k); return merge(merge(s.first, b), s.second); } static PtrPair erase(Ptr a, const int k) { const PtrPair sr = split(a, k + 1); const PtrPair sl = split(sr.first, k); return make_pair(merge(sl.first, sr.second), update(sl.second)); } }; using Ptr = typename Node::Ptr; using PtrPair = typename Node::PtrPair; int head = 0; static Node pool[POOL_SIZE]; Ptr alloc(const typename Node::T &x) { assert(head < POOL_SIZE); pool[head].value = x; pool[head].size = 1; pool[head].left = nullptr; pool[head].right = nullptr; pool[head].accum = x; head++; return pool + head - 1; } void garbage_collect() { vector<T> v; Node::serialize(root, v); head = 0; root = nullptr; rev_root = nullptr; for (int i = 0; i < v.size(); i++) { root = Node::insert(root, i, alloc(v[i])); rev_root = Node::insert(rev_root, v.size() - i - 1, alloc(v[i])); } } Ptr root; Ptr rev_root; public: using BaseMonoid = Monoid; using T = typename Node::T; AccRandomizedBinarySearchTree() : root{}, rev_root{} {} int getSize() const { return Node::getSize(root); } void insert(const int k, const T &value) { const int s = Node::getSize(root); assert(0 <= k); assert(k <= s); if (head == POOL_SIZE) { garbage_collect(); } root = Node::insert(root, k, alloc(value)); rev_root = Node::insert(rev_root, s - k, alloc(value)); } void erase(const int k) { const int s = Node::getSize(root); assert(0 <= k); assert(k < s); Ptr p; tie(root, p) = Node::erase(root, k); if (p != nullptr) { p->left = Ptr{}; p->right = Ptr{}; } tie(rev_root, p) = Node::erase(rev_root, s - k - 1); if (p != nullptr) { p->left = Ptr{}; p->right = Ptr{}; } } T get(int k) const { assert(0 <= k); assert(k < Node::getSize(root)); Ptr v = root; while (v != nullptr) { const int s = Node::getSize(v->left); if (s > k) { v = v->left; } else if (s == k) { return v->value; } else { v = v->right; k -= s + 1; } } return v->value; } void set(const int k, const T &value) { const int s = Node::getSize(root); assert(0 <= k); assert(k < s); const PtrPair sr = Node::split(root, k + 1); const PtrPair sl = Node::split(sr.first, k); const Ptr lr = sl.second; lr->value = value; lr->accum = value; root = Node::merge(Node::merge(sl.first, lr), sr.second); const PtrPair rev_sr = Node::split(rev_root, s - k); const PtrPair rev_sl = Node::split(rev_sr.first, s - k - 1); const Ptr rev_lr = rev_sl.second; rev_lr->value = value; rev_lr->accum = value; rev_root = Node::merge(Node::merge(rev_sl.first, rev_lr), rev_sr.second); } T accumulate(const int l, const int r) // [l,r) { assert(0 <= l); assert(l < r); assert(r <= Node::getSize(root)); const PtrPair sr = Node::split(root, r); const PtrPair sl = Node::split(sr.first, l); const T ans = Node::getAcc(sl.second); root = Node::merge(Node::merge(sl.first, sl.second), sr.second); return ans; } void reverse(const int l, const int r) // [l,r) { const int s = Node::getSize(root); assert(0 <= l); assert(l < r); assert(r <= s); const int width = r - l; if (width > 1) { const PtrPair sr = Node::split(root, r); const PtrPair sl = Node::split(sr.first, l); const PtrPair rev_sr = Node::split(rev_root, s - l); const PtrPair rev_sl = Node::split(rev_sr.first, s - r); root = Node::merge(Node::merge(sl.first, rev_sl.second), sr.second); rev_root = Node::merge(Node::merge(rev_sl.first, sl.second), rev_sr.second); } } void shiftRight(const int l, const int r, const int count) // [l,r) { const int s = Node::getSize(root); assert(0 <= l); assert(l < r); assert(r <= s); const int width = r - l; const int shift = count % width; if (shift > 0) { const PtrPair sr = Node::split(root, r); PtrPair sl = Node::split(sr.first, l); const PtrPair sm = Node::split(sl.second, width - shift); const PtrPair rev_sr = Node::split(rev_root, s - l); PtrPair rev_sl = Node::split(rev_sr.first, s - r); const PtrPair rev_sm = Node::split(rev_sl.second, shift); root = Node::merge(Node::merge(sl.first, sm.second), Node::merge(sm.first, sr.second)); rev_root = Node::merge(Node::merge(rev_sl.first, rev_sm.second), Node::merge(rev_sm.first, rev_sr.second)); } } void shiftLeft(const int l, const int r, const int count) // [l,r) { assert(0 <= l); assert(l < r); assert(r <= Node::getSize(root)); const int width = r - l; const int shift = count % width; if (shift > 0) { shiftRight(l, r, width - shift); } } }; template <typename T> typename AccRandomizedBinarySearchTree<T>::Node AccRandomizedBinarySearchTree< T>::pool[AccRandomizedBinarySearchTree::POOL_SIZE]; template <typename T> ostream &operator<<(ostream &os, const AccRandomizedBinarySearchTree<T> &rbst) { os << "["; for (int i = 0; i < rbst.getSize(); i++) { os << rbst.get(i) << ","; } os << "]" << endl; return os; } struct RBST_Monoid_Min { using T = int; T operator()(const T &a, const T &b) const { return min(a, b); } static constexpr T identity() { return INF<T>; } }; using RBST = AccRandomizedBinarySearchTree<RBST_Monoid_Min>; int main() { cin.tie(0); ios::sync_with_stdio(false); int N, Q; cin >> N >> Q; RBST rbst; for (int i = 0; i < N; i++) { int a; cin >> a; rbst.insert(i, a); } for (int q = 0; q < Q; q++) { int x, y, z; cin >> x >> y >> z; if (x == 0) { rbst.shiftRight(y, z + 1, 1); } else if (x == 1) { cout << rbst.accumulate(y, z + 1) << "\n"; } else { rbst.set(y, z); } } return 0; }
replace
5
6
5
6
0
p00998
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; template <class Monoid, class OperatorMonoid = Monoid> struct RandomizedBinarySearchTree { using F = function<Monoid(Monoid, Monoid)>; using G = function<Monoid(Monoid, OperatorMonoid)>; using H = function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>; using P = function<OperatorMonoid(OperatorMonoid, int)>; inline int xor128() { static int x = 123456789; static int y = 362436069; static int z = 521288629; static int w = 88675123; int t; t = x ^ (x << 11); x = y; y = z; z = w; return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); } struct Node { Node *l, *r; int cnt; Monoid key, sum; OperatorMonoid lazy; Node() {} Node(const Monoid &k, const OperatorMonoid &p) : cnt(1), key(k), sum(k), lazy(p), l(nullptr), r(nullptr) {} }; vector<Node> pool; int ptr; const Monoid M1; const OperatorMonoid OM0; F f; G g; H h; P p; RandomizedBinarySearchTree(int sz, const F &f, const Monoid &M1) : pool(sz), ptr(0), f(f), g(f), h(f), p(f), M1(M1), OM0(0) {} RandomizedBinarySearchTree(int sz, const F &f, const G &g, const H &h, const P &p, const Monoid &M1, const OperatorMonoid &OM0) : pool(sz), ptr(0), f(f), g(g), h(h), p(p), M1(M1), OM0(OM0) {} inline Node *alloc(const Monoid &key) { return &(pool[ptr++] = Node(key, OM0)); } virtual Node *clone(Node *t) { return t; } inline int count(const Node *t) { return t ? t->cnt : 0; } inline Monoid sum(const Node *t) { return t ? t->sum : M1; } inline Node *update(Node *t) { t->cnt = count(t->l) + count(t->r) + 1; t->sum = f(f(sum(t->l), sum(t->r)), t->key); return t; } Node *propagete(Node *t) { t = clone(t); if (t->lazy != OM0) { t->key = g(t->key, t->lazy); if (t->l) { t->l = clone(t->l); t->l->lazy = h(t->l->lazy, t->lazy); t->l->sum = f(t->l->sum, p(t->lazy, count(t->l))); } if (t->r) { t->r = clone(t->r); t->r->lazy = h(t->r->lazy, t->lazy); t->r->sum = f(t->r->sum, p(t->lazy, count(t->r))); } t->lazy = OM0; } return update(t); } Node *merge(Node *l, Node *r) { if (!l || !r) return l ? l : r; if (xor128() % (l->cnt + r->cnt) < l->cnt) { l = propagete(l); l->r = merge(l->r, r); return update(l); } else { r = propagete(r); r->l = merge(l, r->l); return update(r); } } pair<Node *, Node *> split(Node *t, int k) { if (!t) return {t, t}; t = propagete(t); if (k <= count(t->l)) { auto s = split(t->l, k); t->l = s.second; return {s.first, update(t)}; } else { auto s = split(t->r, k - count(t->l) - 1); t->r = s.first; return {update(t), s.second}; } } Node *build(int l, int r, const vector<Monoid> &v) { if (l + 1 >= r) return alloc(v[l]); return merge(build(l, (l + r) >> 1, v), build((l + r) >> 1, r, v)); } Node *build(const vector<Monoid> &v) { ptr = 0; return build(0, (int)v.size(), v); } void dump(Node *r, typename vector<Monoid>::iterator &it) { if (!r) return; r = propagete(r); dump(r->l, it); *it = r->key; dump(r->r, ++it); } vector<Monoid> dump(Node *r) { vector<Monoid> v((size_t)count(r)); auto it = begin(v); dump(r, it); return v; } string to_string(Node *r) { auto s = dump(r); string ret; for (int i = 0; i < s.size(); i++) ret += ", "; return (ret); } void insert(Node *&t, int k, const Monoid &v) { auto x = split(t, k); t = merge(merge(x.first, alloc(v)), x.second); } void erase(Node *&t, int k) { auto x = split(t, k); t = merge(x.first, split(x.second, 1).second); } Monoid query(Node *&t, int a, int b) { auto x = split(t, a); auto y = split(x.second, b - a); auto ret = sum(y.first); t = merge(x.first, merge(y.first, y.second)); return ret; } void set_propagate(Node *&t, int a, int b, OperatorMonoid &p) { auto x = split(t, a); auto y = split(x.second, b - a); y.first->lazy = h(y.first->lazy, p); t = merge(x.first, merge(propagete(y.first), y.second)); } }; const int INF = 1 << 30; int main() { int N, Q; scanf("%d %d", &N, &Q); vector<int> v(N); for (int i = 0; i < N; i++) scanf("%d", &v[i]); auto f = [](int a, int b) { return min(a, b); }; RandomizedBinarySearchTree<int> rbst(2 * N, f, INF); auto root = rbst.build(v); for (int i = 0; i < Q; i++) { int x, y, z; scanf("%d %d %d", &x, &y, &z); if (x == 0) { auto vv = rbst.query(root, z, z + 1); rbst.erase(root, z); rbst.insert(root, y, vv); } else if (x == 1) { printf("%d\n", rbst.query(root, y, z + 1)); } else if (x == 2) { rbst.erase(root, y); rbst.insert(root, y, z); } } }
#include <bits/stdc++.h> using namespace std; template <class Monoid, class OperatorMonoid = Monoid> struct RandomizedBinarySearchTree { using F = function<Monoid(Monoid, Monoid)>; using G = function<Monoid(Monoid, OperatorMonoid)>; using H = function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>; using P = function<OperatorMonoid(OperatorMonoid, int)>; inline int xor128() { static int x = 123456789; static int y = 362436069; static int z = 521288629; static int w = 88675123; int t; t = x ^ (x << 11); x = y; y = z; z = w; return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); } struct Node { Node *l, *r; int cnt; Monoid key, sum; OperatorMonoid lazy; Node() {} Node(const Monoid &k, const OperatorMonoid &p) : cnt(1), key(k), sum(k), lazy(p), l(nullptr), r(nullptr) {} }; vector<Node> pool; int ptr; const Monoid M1; const OperatorMonoid OM0; F f; G g; H h; P p; RandomizedBinarySearchTree(int sz, const F &f, const Monoid &M1) : pool(sz), ptr(0), f(f), g(f), h(f), p(f), M1(M1), OM0(0) {} RandomizedBinarySearchTree(int sz, const F &f, const G &g, const H &h, const P &p, const Monoid &M1, const OperatorMonoid &OM0) : pool(sz), ptr(0), f(f), g(g), h(h), p(p), M1(M1), OM0(OM0) {} inline Node *alloc(const Monoid &key) { return &(pool[ptr++] = Node(key, OM0)); } virtual Node *clone(Node *t) { return t; } inline int count(const Node *t) { return t ? t->cnt : 0; } inline Monoid sum(const Node *t) { return t ? t->sum : M1; } inline Node *update(Node *t) { t->cnt = count(t->l) + count(t->r) + 1; t->sum = f(f(sum(t->l), sum(t->r)), t->key); return t; } Node *propagete(Node *t) { t = clone(t); if (t->lazy != OM0) { t->key = g(t->key, t->lazy); if (t->l) { t->l = clone(t->l); t->l->lazy = h(t->l->lazy, t->lazy); t->l->sum = f(t->l->sum, p(t->lazy, count(t->l))); } if (t->r) { t->r = clone(t->r); t->r->lazy = h(t->r->lazy, t->lazy); t->r->sum = f(t->r->sum, p(t->lazy, count(t->r))); } t->lazy = OM0; } return update(t); } Node *merge(Node *l, Node *r) { if (!l || !r) return l ? l : r; if (xor128() % (l->cnt + r->cnt) < l->cnt) { l = propagete(l); l->r = merge(l->r, r); return update(l); } else { r = propagete(r); r->l = merge(l, r->l); return update(r); } } pair<Node *, Node *> split(Node *t, int k) { if (!t) return {t, t}; t = propagete(t); if (k <= count(t->l)) { auto s = split(t->l, k); t->l = s.second; return {s.first, update(t)}; } else { auto s = split(t->r, k - count(t->l) - 1); t->r = s.first; return {update(t), s.second}; } } Node *build(int l, int r, const vector<Monoid> &v) { if (l + 1 >= r) return alloc(v[l]); return merge(build(l, (l + r) >> 1, v), build((l + r) >> 1, r, v)); } Node *build(const vector<Monoid> &v) { ptr = 0; return build(0, (int)v.size(), v); } void dump(Node *r, typename vector<Monoid>::iterator &it) { if (!r) return; r = propagete(r); dump(r->l, it); *it = r->key; dump(r->r, ++it); } vector<Monoid> dump(Node *r) { vector<Monoid> v((size_t)count(r)); auto it = begin(v); dump(r, it); return v; } string to_string(Node *r) { auto s = dump(r); string ret; for (int i = 0; i < s.size(); i++) ret += ", "; return (ret); } void insert(Node *&t, int k, const Monoid &v) { auto x = split(t, k); t = merge(merge(x.first, alloc(v)), x.second); } void erase(Node *&t, int k) { auto x = split(t, k); t = merge(x.first, split(x.second, 1).second); } Monoid query(Node *&t, int a, int b) { auto x = split(t, a); auto y = split(x.second, b - a); auto ret = sum(y.first); t = merge(x.first, merge(y.first, y.second)); return ret; } void set_propagate(Node *&t, int a, int b, OperatorMonoid &p) { auto x = split(t, a); auto y = split(x.second, b - a); y.first->lazy = h(y.first->lazy, p); t = merge(x.first, merge(propagete(y.first), y.second)); } }; const int INF = 1 << 30; int main() { int N, Q; scanf("%d %d", &N, &Q); vector<int> v(N); for (int i = 0; i < N; i++) scanf("%d", &v[i]); auto f = [](int a, int b) { return min(a, b); }; RandomizedBinarySearchTree<int> rbst(N + Q, f, INF); auto root = rbst.build(v); for (int i = 0; i < Q; i++) { int x, y, z; scanf("%d %d %d", &x, &y, &z); if (x == 0) { auto vv = rbst.query(root, z, z + 1); rbst.erase(root, z); rbst.insert(root, y, vv); } else if (x == 1) { printf("%d\n", rbst.query(root, y, z + 1)); } else if (x == 2) { rbst.erase(root, y); rbst.insert(root, y, z); } } }
replace
190
191
190
191
0
p00998
C++
Time Limit Exceeded
#define NDEBUG 1 #include <bits/stdc++.h> using key_type = int; enum { L, R }; struct node { key_type key; std::array<node *, 2> ch; int size; int height; int min; static node *const nil; node() : node(-1) {} node(key_type key) : node(key, nil, nil, 1, 1, key) {} node(const key_type key, node *left, node *right, int size, int height, key_type min) : key(key), ch({{left, right}}), size(size), height(height), min(min) {} void *operator new(size_t) { static int p = 0; static node pool[1000000]; return pool + p++; } }; node *const node::nil = new node(key_type(), nullptr, nullptr, 0, 0, 1e9); node *const nil = node::nil; namespace trush { auto ___ = []() { return nil->ch = {{nil, nil}}; }; }; using np = node *; using cnp = const node *; // ok np update(np n) { n->size = n->ch[L]->size + 1 + n->ch[R]->size; n->height = std::max(n->ch[L]->height, n->ch[R]->height) + 1; n->min = std::min({n->ch[L]->min, n->key, n->ch[R]->min}); return n; } // ok template <int dir> np rotate(np n) { assert(n->ch[!dir] != nil); np root = n->ch[!dir]; n->ch[!dir] = root->ch[dir]; root->ch[dir] = n; update(n); update(root); return root; } // ok int bfactor(np n) { assert(n != nil); return n->ch[R]->height - n->ch[L]->height; } // ok np balance(np n) { assert(abs(bfactor(n)) <= 2); if (bfactor(n) == +2) { if (bfactor(n->ch[R]) < 0) n->ch[R] = rotate<R>(n->ch[R]); return rotate<L>(n); } else if (bfactor(n) == -2) { if (bfactor(n->ch[L]) > 0) n->ch[L] = rotate<L>(n->ch[L]); return rotate<R>(n); } else { return n; } } // ok np insert_at(np n, int k, key_type x) { assert(0 <= k && k <= n->size); if (n == nil) return new node(x); int sl = n->ch[L]->size; if (k <= sl) n->ch[L] = insert_at(n->ch[L], k, x); else n->ch[R] = insert_at(n->ch[R], k - sl - 1, x); return balance(update(n)); } // ok template <int dir> std::pair<np, np> remove_most(np n) { assert(n != nil); if (n->ch[dir] != nil) { np most; std::tie(n->ch[dir], most) = remove_most<dir>(n->ch[dir]); return {balance(update(n)), most}; } else { np res = n->ch[!dir]; n->ch[!dir] = nil; return {res, update(n)}; } } // ok std::pair<np, key_type> remove_at(np n, int k) { assert(n != nil); int sl = n->ch[L]->size; if (k < sl) { key_type most; std::tie(n->ch[L], most) = remove_at(n->ch[L], k); return {balance(update(n)), most}; } if (k == sl) { if (n->ch[R] == nil) { return {n->ch[L], n->key}; } else { np most; std::tie(n->ch[R], most) = remove_most<L>(n->ch[R]); most->ch = n->ch; // delete n; return {balance(update(most)), n->key}; } } else { key_type most; std::tie(n->ch[R], most) = remove_at(n->ch[R], k - sl - 1); return {balance(update(n)), most}; } } // ok np merge_with_root(np l, np root, np r) { // Members of `root` except root->key may not be valid for performance. if (abs(l->height - r->height) <= 1) { root->ch = {{l, r}}; return update(root); } else if (l->height > r->height) { l->ch[R] = merge_with_root(l->ch[R], root, r); return balance(update(l)); } else { r->ch[L] = merge_with_root(l, root, r->ch[L]); return balance(update(r)); } } // ok np merge(np l, np r) { if (l == nil) return r; if (r == nil) return l; np m; if (l->height > r->height) std::tie(r, m) = remove_most<L>(r); else std::tie(l, m) = remove_most<R>(l); return merge_with_root(l, m, r); } template <typename Iterator> np build(Iterator left, Iterator right) { int n = right - left; Iterator mid = left + n / 2; if (n == 0) return nil; np l = build(left, mid); np r = build(mid + 1, right); np m = new node(*mid); m->ch = {{l, r}}; return update(m); } // ok std::pair<np, np> split_at(np n, int k) { assert(0 <= k && k <= n->size); if (n == nil) return {nil, nil}; int sl = n->ch[L]->size; np l = n->ch[L]; np r = n->ch[R]; n->ch[L] = n->ch[R] = nil; // Members of node passed to `merge` must be valid, // but ones for `merge_with_root` doesn't have to. np nl, nr; if (k < sl) { std::tie(nl, nr) = split_at(l, k); return {nl, merge_with_root(nr, n, r)}; } else if (k == sl) { update(n); return {l, merge(n, r)}; } else { std::tie(nl, nr) = split_at(r, k - sl - 1); return {merge_with_root(l, n, nl), nr}; } } void update_at(np n, int k, key_type x) { assert(0 <= k && k <= n->size - 1); int sl = n->ch[L]->size; if (k < sl) { update_at(n->ch[L], k, x); } else if (k == sl) { n->key = x; } else { update_at(n->ch[R], k - sl - 1, x); } update(n); } key_type range_min(np n, int l, int r) { assert(n != nil); assert(0 <= l && l <= r); assert(l <= r && r <= n->size); if (l == 0 && r == n->size) { return n->min; } int sl = n->ch[L]->size; key_type res = 1e9; if (l < sl) { res = std::min(res, range_min(n->ch[L], l, std::min(r, sl))); } if (l <= sl && sl < r) { res = std::min(res, n->key); } if (sl + 1 < r) { res = std::min(res, range_min(n->ch[R], std::max(0, l - sl - 1), r - sl - 1)); } return res; } namespace test { int real_height(np n) { if (n == nil) return 0; return 1 + std::max(real_height(n->ch[L]), real_height(n->ch[R])); } int real_size(np n) { if (n == nil) return 0; return 1 + real_size(n->ch[L]) + real_size(n->ch[R]); } int real_min(np n) { if (n == nil) return 1e9; return std::min({real_min(n->ch[L]), n->key, real_min(n->ch[R])}); } bool verify(np n) { if (n == nil) { if (n->ch[L] != nil) return false; if (n->ch[R] != nil) return false; return true; } np l = n->ch[L]; np r = n->ch[R]; if (n->size != real_size(n)) return false; if (n->height != real_height(n)) return false; if (n->min != real_min(n)) return false; if (n->size != l->size + 1 + r->size) return false; if (n->height != std::max(l->height, r->height) + 1) return false; if (n->min != std::min({n->ch[L]->min, n->key, n->ch[R]->min})) return false; if (abs(bfactor(n)) >= 2) return false; return true; } } // namespace test void to_a(np n, std::vector<int> &v) { if (n == nil) return; to_a(n->ch[L], v); v.push_back(n->key); to_a(n->ch[R], v); } std::vector<int> to_a(np n) { std::vector<int> res; to_a(n, res); return res; } using namespace std; mt19937 mt(100); int randint(int l, int r) { uniform_int_distribution<int> dist(l, r); return dist(mt); } void show(const vector<int> &x) { for (auto &e : x) cout << e << ' '; cout << endl; } int main() { cin.tie(0); ios::sync_with_stdio(0); int n, q; while (cin >> n >> q) { vector<int> a(n); for (int i = 0; i < n; ++i) cin >> a[i]; np root = build(a.begin(), a.end()); for (int i = 0; i < q; ++i) { int t, x, y; cin >> t >> x >> y; if (t == 0) { int t; tie(root, t) = remove_at(root, y); root = insert_at(root, x, t); } else if (t == 1) { ++y; cout << range_min(root, x, y) << '\n'; // np l, m, r; // tie(m, r) = split_at(root, y); // tie(l, m) = split_at(m, x); // cout << m->min << '\n'; // root = merge(l, merge(m, r)); } else { update_at(root, x, y); } test::verify(root); } } // for (int _ = 0; _ < 10000; ++_) { // int N = randint(1, 2000); // np root = nil; // vector<int> v(N); // for (int i = 0; i < N; ++i) { // v[i] = randint(0, 100); // root = insert_at(root, i, v[i]); // } // int k = randint(0, N); // np l, r; // tie(l, r) = split_at(root, k); // test::verify(l); // test::verify(r); // vector<int> vl(v.begin(), v.begin() + k), vr(v.begin() + k, v.end()); // assert(to_a(l) == vl); // assert(to_a(r) == vr); // root = merge(l, r); // test::verify(root); // for (auto &x : vr) vl.push_back(x); // assert(to_a(root) == vl); // } // for (int _ = 0; _ < 10000; ++_) { // int N = randint(1, 2000); // np root = nil; // // vector<int> v; // for (int i = 0; i < N; ++i) { // if (root->size && randint(0, 3) == 0) { // int k = randint(0, root->size - 1); // // v.erase(v.begin() + k); // root = remove_at(root, k); // } else { // int k = randint(0, root->size); // int x = randint(0, 100); // // v.insert(v.begin() + k, x); // root = insert_at(root, k, x); // } // // vector<int> a = to_a(root); // // assert(verify(root)); // // assert(a == v); // } // assert(test::verify(root)); // // vector<int> a = to_a(root); // // assert(a == v); // } }
#define NDEBUG 1 #include <bits/stdc++.h> using key_type = int; enum { L, R }; struct node { key_type key; std::array<node *, 2> ch; int size; int height; int min; static node *const nil; node() : node(-1) {} node(key_type key) : node(key, nil, nil, 1, 1, key) {} node(const key_type key, node *left, node *right, int size, int height, key_type min) : key(key), ch({{left, right}}), size(size), height(height), min(min) {} void *operator new(size_t) { static int p = 0; static node pool[1000000]; return pool + p++; } }; node *const node::nil = new node(key_type(), nullptr, nullptr, 0, 0, 1e9); node *const nil = node::nil; namespace trush { auto ___ = []() { return nil->ch = {{nil, nil}}; }; }; using np = node *; using cnp = const node *; // ok np update(np n) { n->size = n->ch[L]->size + 1 + n->ch[R]->size; n->height = std::max(n->ch[L]->height, n->ch[R]->height) + 1; n->min = std::min({n->ch[L]->min, n->key, n->ch[R]->min}); return n; } // ok template <int dir> np rotate(np n) { assert(n->ch[!dir] != nil); np root = n->ch[!dir]; n->ch[!dir] = root->ch[dir]; root->ch[dir] = n; update(n); update(root); return root; } // ok int bfactor(np n) { assert(n != nil); return n->ch[R]->height - n->ch[L]->height; } // ok np balance(np n) { assert(abs(bfactor(n)) <= 2); if (bfactor(n) == +2) { if (bfactor(n->ch[R]) < 0) n->ch[R] = rotate<R>(n->ch[R]); return rotate<L>(n); } else if (bfactor(n) == -2) { if (bfactor(n->ch[L]) > 0) n->ch[L] = rotate<L>(n->ch[L]); return rotate<R>(n); } else { return n; } } // ok np insert_at(np n, int k, key_type x) { assert(0 <= k && k <= n->size); if (n == nil) return new node(x); int sl = n->ch[L]->size; if (k <= sl) n->ch[L] = insert_at(n->ch[L], k, x); else n->ch[R] = insert_at(n->ch[R], k - sl - 1, x); return balance(update(n)); } // ok template <int dir> std::pair<np, np> remove_most(np n) { assert(n != nil); if (n->ch[dir] != nil) { np most; std::tie(n->ch[dir], most) = remove_most<dir>(n->ch[dir]); return {balance(update(n)), most}; } else { np res = n->ch[!dir]; n->ch[!dir] = nil; return {res, update(n)}; } } // ok std::pair<np, key_type> remove_at(np n, int k) { assert(n != nil); int sl = n->ch[L]->size; if (k < sl) { key_type most; std::tie(n->ch[L], most) = remove_at(n->ch[L], k); return {balance(update(n)), most}; } if (k == sl) { if (n->ch[R] == nil) { return {n->ch[L], n->key}; } else { np most; std::tie(n->ch[R], most) = remove_most<L>(n->ch[R]); most->ch = n->ch; // delete n; return {balance(update(most)), n->key}; } } else { key_type most; std::tie(n->ch[R], most) = remove_at(n->ch[R], k - sl - 1); return {balance(update(n)), most}; } } // ok np merge_with_root(np l, np root, np r) { // Members of `root` except root->key may not be valid for performance. if (abs(l->height - r->height) <= 1) { root->ch = {{l, r}}; return update(root); } else if (l->height > r->height) { l->ch[R] = merge_with_root(l->ch[R], root, r); return balance(update(l)); } else { r->ch[L] = merge_with_root(l, root, r->ch[L]); return balance(update(r)); } } // ok np merge(np l, np r) { if (l == nil) return r; if (r == nil) return l; np m; if (l->height > r->height) std::tie(r, m) = remove_most<L>(r); else std::tie(l, m) = remove_most<R>(l); return merge_with_root(l, m, r); } template <typename Iterator> np build(Iterator left, Iterator right) { int n = right - left; Iterator mid = left + n / 2; if (n == 0) return nil; np l = build(left, mid); np r = build(mid + 1, right); np m = new node(*mid); m->ch = {{l, r}}; return update(m); } // ok std::pair<np, np> split_at(np n, int k) { assert(0 <= k && k <= n->size); if (n == nil) return {nil, nil}; int sl = n->ch[L]->size; np l = n->ch[L]; np r = n->ch[R]; n->ch[L] = n->ch[R] = nil; // Members of node passed to `merge` must be valid, // but ones for `merge_with_root` doesn't have to. np nl, nr; if (k < sl) { std::tie(nl, nr) = split_at(l, k); return {nl, merge_with_root(nr, n, r)}; } else if (k == sl) { update(n); return {l, merge(n, r)}; } else { std::tie(nl, nr) = split_at(r, k - sl - 1); return {merge_with_root(l, n, nl), nr}; } } void update_at(np n, int k, key_type x) { assert(0 <= k && k <= n->size - 1); int sl = n->ch[L]->size; if (k < sl) { update_at(n->ch[L], k, x); } else if (k == sl) { n->key = x; } else { update_at(n->ch[R], k - sl - 1, x); } update(n); } key_type range_min(np n, int l, int r) { assert(n != nil); assert(0 <= l && l <= r); assert(l <= r && r <= n->size); if (l == 0 && r == n->size) { return n->min; } int sl = n->ch[L]->size; key_type res = 1e9; if (l < sl) { res = std::min(res, range_min(n->ch[L], l, std::min(r, sl))); } if (l <= sl && sl < r) { res = std::min(res, n->key); } if (sl + 1 < r) { res = std::min(res, range_min(n->ch[R], std::max(0, l - sl - 1), r - sl - 1)); } return res; } namespace test { int real_height(np n) { if (n == nil) return 0; return 1 + std::max(real_height(n->ch[L]), real_height(n->ch[R])); } int real_size(np n) { if (n == nil) return 0; return 1 + real_size(n->ch[L]) + real_size(n->ch[R]); } int real_min(np n) { if (n == nil) return 1e9; return std::min({real_min(n->ch[L]), n->key, real_min(n->ch[R])}); } bool verify(np n) { if (n == nil) { if (n->ch[L] != nil) return false; if (n->ch[R] != nil) return false; return true; } np l = n->ch[L]; np r = n->ch[R]; if (n->size != real_size(n)) return false; if (n->height != real_height(n)) return false; if (n->min != real_min(n)) return false; if (n->size != l->size + 1 + r->size) return false; if (n->height != std::max(l->height, r->height) + 1) return false; if (n->min != std::min({n->ch[L]->min, n->key, n->ch[R]->min})) return false; if (abs(bfactor(n)) >= 2) return false; return true; } } // namespace test void to_a(np n, std::vector<int> &v) { if (n == nil) return; to_a(n->ch[L], v); v.push_back(n->key); to_a(n->ch[R], v); } std::vector<int> to_a(np n) { std::vector<int> res; to_a(n, res); return res; } using namespace std; mt19937 mt(100); int randint(int l, int r) { uniform_int_distribution<int> dist(l, r); return dist(mt); } void show(const vector<int> &x) { for (auto &e : x) cout << e << ' '; cout << endl; } int main() { cin.tie(0); ios::sync_with_stdio(0); int n, q; while (cin >> n >> q) { vector<int> a(n); for (int i = 0; i < n; ++i) cin >> a[i]; np root = build(a.begin(), a.end()); for (int i = 0; i < q; ++i) { int t, x, y; cin >> t >> x >> y; if (t == 0) { int t; tie(root, t) = remove_at(root, y); root = insert_at(root, x, t); } else if (t == 1) { ++y; cout << range_min(root, x, y) << '\n'; // np l, m, r; // tie(m, r) = split_at(root, y); // tie(l, m) = split_at(m, x); // cout << m->min << '\n'; // root = merge(l, merge(m, r)); } else { update_at(root, x, y); } // test::verify(root); } } // for (int _ = 0; _ < 10000; ++_) { // int N = randint(1, 2000); // np root = nil; // vector<int> v(N); // for (int i = 0; i < N; ++i) { // v[i] = randint(0, 100); // root = insert_at(root, i, v[i]); // } // int k = randint(0, N); // np l, r; // tie(l, r) = split_at(root, k); // test::verify(l); // test::verify(r); // vector<int> vl(v.begin(), v.begin() + k), vr(v.begin() + k, v.end()); // assert(to_a(l) == vl); // assert(to_a(r) == vr); // root = merge(l, r); // test::verify(root); // for (auto &x : vr) vl.push_back(x); // assert(to_a(root) == vl); // } // for (int _ = 0; _ < 10000; ++_) { // int N = randint(1, 2000); // np root = nil; // // vector<int> v; // for (int i = 0; i < N; ++i) { // if (root->size && randint(0, 3) == 0) { // int k = randint(0, root->size - 1); // // v.erase(v.begin() + k); // root = remove_at(root, k); // } else { // int k = randint(0, root->size); // int x = randint(0, 100); // // v.insert(v.begin() + k, x); // root = insert_at(root, k, x); // } // // vector<int> a = to_a(root); // // assert(verify(root)); // // assert(a == v); // } // assert(test::verify(root)); // // vector<int> a = to_a(root); // // assert(a == v); // } }
replace
342
343
342
343
TLE
p00998
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using int64 = long long; template <class T> struct ArrayPool { vector<T> pool; vector<T *> stock; int ptr; ArrayPool(int sz) : pool(sz), stock(sz) {} inline T *alloc() { return stock[--ptr]; } inline void free(T *t) { stock[ptr++] = t; } void clear() { ptr = (int)pool.size(); for (int i = 0; i < pool.size(); i++) stock[i] = &pool[i]; } }; template <class Monoid, class OperatorMonoid = Monoid> struct RedBlackTree { using F = function<Monoid(Monoid, Monoid)>; using G = function<Monoid(Monoid, OperatorMonoid)>; using H = function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>; using P = function<OperatorMonoid(OperatorMonoid, int)>; enum COLOR { BLACK, RED }; struct Node { Node *l, *r; COLOR color; int level, cnt; Monoid key, sum; OperatorMonoid lazy; Node() {} Node(const Monoid &k, const OperatorMonoid &p) : key(k), sum(k), l(nullptr), r(nullptr), color(BLACK), level(0), cnt(1), lazy(p) {} Node(Node *l, Node *r, const Monoid &k, const OperatorMonoid &p) : key(k), color(RED), l(l), r(r), lazy(p) {} }; ArrayPool<Node> pool; const Monoid M1; const OperatorMonoid OM0; const F f; const G g; const H h; const P p; RedBlackTree(int sz, const F &f, const Monoid &M1) : pool(sz), f(f), g(G()), h(H()), p(P()), M1(M1), OM0(OperatorMonoid()) {} RedBlackTree(int sz, const F &f, const G &g, const H &h, const P &p, const Monoid &M1, const OperatorMonoid &OM0) : pool(sz), f(f), g(g), h(h), p(p), M1(M1), OM0(OM0) {} inline Node *alloc(const Monoid &key) { return &(*pool.alloc() = Node(key, OM0)); } inline Node *alloc(Node *l, Node *r) { auto t = &(*pool.alloc() = Node(l, r, M1, OM0)); return update(t); } virtual Node *clone(Node *t) { return t; } inline int count(const Node *t) { return t ? t->cnt : 0; } inline Monoid sum(const Node *t) { return t ? t->sum : M1; } Node *update(Node *t) { t->cnt = count(t->l) + count(t->r) + (!t->l || !t->r); t->level = t->l ? t->l->level + (t->l->color == BLACK) : 0; t->sum = f(f(sum(t->l), t->key), sum(t->r)); return t; } Node *propagate(Node *t) { t = clone(t); if (t->lazy != OM0) { if (!t->l) { t->key = g(t->key, t->lazy); } else { if (t->l) { t->l = clone(t->l); t->l->lazy = h(t->l->lazy, t->lazy); t->l->sum = g(t->l->sum, p(t->lazy, count(t->l))); } if (t->r) { t->r = clone(t->r); t->r->lazy = h(t->r->lazy, t->lazy); t->r->sum = g(t->r->sum, p(t->lazy, count(t->r))); } } t->lazy = OM0; } return update(t); } Node *rotate(Node *t, bool b) { t = propagate(t); Node *s; if (b) { s = propagate(t->l); t->l = s->r; s->r = t; } else { s = propagate(t->r); t->r = s->l; s->l = t; } update(t); return update(s); } Node *submerge(Node *l, Node *r) { if (l->level < r->level) { r = propagate(r); Node *c = (r->l = submerge(l, r->l)); if (r->color == BLACK && c->color == RED && c->l && c->l->color == RED) { r->color = RED; c->color = BLACK; if (r->r->color == BLACK) return rotate(r, true); r->r->color = BLACK; } return update(r); } if (l->level > r->level) { l = propagate(l); Node *c = (l->r = submerge(l->r, r)); if (l->color == BLACK && c->color == RED && c->r && c->r->color == RED) { l->color = RED; c->color = BLACK; if (l->l->color == BLACK) return rotate(l, false); l->l->color = BLACK; } return update(l); } return alloc(l, r); } Node *merge(Node *l, Node *r) { if (!l || !r) return l ? l : r; Node *c = submerge(l, r); c->color = BLACK; return c; } pair<Node *, Node *> split(Node *t, int k) { if (!t) return {nullptr, nullptr}; t = propagate(t); if (k == 0) return {nullptr, t}; if (k >= count(t)) return {t, nullptr}; if (k < count(t->l)) { auto p = split(t->l, k); return {p.first, merge(p.second, t->r)}; } if (k > count(t->l)) { auto p = split(t->r, k - count(t->l)); return {merge(t->l, p.first), p.second}; } pair<Node *, Node *> ret = {t->l, t->r}; pool.free(t); return ret; } Node *build(int l, int r, const vector<Monoid> &v) { if (l + 1 >= r) return alloc(v[l]); return merge(build(l, (l + r) >> 1, v), build((l + r) >> 1, r, v)); } Node *build(const vector<Monoid> &v) { pool.clear(); return build(0, (int)v.size(), v); } void dump(Node *r, typename vector<Monoid>::iterator &it) { r = propagate(r); if (!r->l || !r->r) { *it++ = r->key; return; } dump(r->l, it); dump(r->r, it); } vector<Monoid> dump(Node *r) { vector<Monoid> v((size_t)count(r)); auto it = begin(v); dump(r, it); return v; } string to_string(Node *r) { auto s = dump(r); string ret; for (int i = 0; i < s.size(); i++) { ret += std::to_string(s[i]); ret += ", "; } return (ret); } void insert(Node *&t, int k, const Monoid &v) { auto x = split(t, k); t = merge(merge(x.first, alloc(v)), x.second); } void erase(Node *&t, int k) { auto x = split(t, k); auto y = split(x.second, 1); pool.free(y.first); t = merge(x.first, y.second); } Monoid query(Node *&t, int a, int b) { auto x = split(t, a); auto y = split(x.second, b - a); auto ret = sum(y.first); t = merge(x.first, merge(y.first, y.second)); return ret; } void set_propagate(Node *&t, int a, int b, const OperatorMonoid &p) { auto x = split(t, a); auto y = split(x.second, b - a); y.first->lazy = h(y.first->lazy, p); t = merge(x.first, merge(propagate(y.first), y.second)); } void set_element(Node *&t, int k, const Monoid &x) { if (!t->l) { t->key = t->sum = x; return; } t = propagate(t); if (k < count(t->l)) set_element(t->l, k, x); else set_element(t->r, k - count(t->l), x); t = update(t); } int size(Node *t) { return count(t); } bool empty(Node *t) { return !t; } Node *makeset() { return (nullptr); } }; using int64 = long long; int main() { int n, q; scanf("%d %d", &n, &q); vector<int> v(n); for (int i = 0; i < n; i++) scanf("%d", &v[i]); auto f = [](int a, int b) { return min(a, b); }; const int INF = 1 << 30; RedBlackTree<int, int> rbt(5400000, f, INF); auto root = rbt.build(v); for (int i = 0; i < q; i++) { int x, y, z; scanf("%d %d %d", &x, &y, &z); if (x == 0) { int v = rbt.query(root, z, z + 1); rbt.erase(root, z); rbt.insert(root, y, v); } else if (x == 1) { printf("%d\n", rbt.query(root, y, z + 1)); } else if (x == 2) { rbt.set_element(root, y, z); } if (rbt.pool.ptr < 50) root = rbt.build(rbt.dump(root)); } }
#include <bits/stdc++.h> using namespace std; using int64 = long long; template <class T> struct ArrayPool { vector<T> pool; vector<T *> stock; int ptr; ArrayPool(int sz) : pool(sz), stock(sz) {} inline T *alloc() { return stock[--ptr]; } inline void free(T *t) { stock[ptr++] = t; } void clear() { ptr = (int)pool.size(); for (int i = 0; i < pool.size(); i++) stock[i] = &pool[i]; } }; template <class Monoid, class OperatorMonoid = Monoid> struct RedBlackTree { using F = function<Monoid(Monoid, Monoid)>; using G = function<Monoid(Monoid, OperatorMonoid)>; using H = function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>; using P = function<OperatorMonoid(OperatorMonoid, int)>; enum COLOR { BLACK, RED }; struct Node { Node *l, *r; COLOR color; int level, cnt; Monoid key, sum; OperatorMonoid lazy; Node() {} Node(const Monoid &k, const OperatorMonoid &p) : key(k), sum(k), l(nullptr), r(nullptr), color(BLACK), level(0), cnt(1), lazy(p) {} Node(Node *l, Node *r, const Monoid &k, const OperatorMonoid &p) : key(k), color(RED), l(l), r(r), lazy(p) {} }; ArrayPool<Node> pool; const Monoid M1; const OperatorMonoid OM0; const F f; const G g; const H h; const P p; RedBlackTree(int sz, const F &f, const Monoid &M1) : pool(sz), f(f), g(G()), h(H()), p(P()), M1(M1), OM0(OperatorMonoid()) {} RedBlackTree(int sz, const F &f, const G &g, const H &h, const P &p, const Monoid &M1, const OperatorMonoid &OM0) : pool(sz), f(f), g(g), h(h), p(p), M1(M1), OM0(OM0) {} inline Node *alloc(const Monoid &key) { return &(*pool.alloc() = Node(key, OM0)); } inline Node *alloc(Node *l, Node *r) { auto t = &(*pool.alloc() = Node(l, r, M1, OM0)); return update(t); } virtual Node *clone(Node *t) { return t; } inline int count(const Node *t) { return t ? t->cnt : 0; } inline Monoid sum(const Node *t) { return t ? t->sum : M1; } Node *update(Node *t) { t->cnt = count(t->l) + count(t->r) + (!t->l || !t->r); t->level = t->l ? t->l->level + (t->l->color == BLACK) : 0; t->sum = f(f(sum(t->l), t->key), sum(t->r)); return t; } Node *propagate(Node *t) { t = clone(t); if (t->lazy != OM0) { if (!t->l) { t->key = g(t->key, t->lazy); } else { if (t->l) { t->l = clone(t->l); t->l->lazy = h(t->l->lazy, t->lazy); t->l->sum = g(t->l->sum, p(t->lazy, count(t->l))); } if (t->r) { t->r = clone(t->r); t->r->lazy = h(t->r->lazy, t->lazy); t->r->sum = g(t->r->sum, p(t->lazy, count(t->r))); } } t->lazy = OM0; } return update(t); } Node *rotate(Node *t, bool b) { t = propagate(t); Node *s; if (b) { s = propagate(t->l); t->l = s->r; s->r = t; } else { s = propagate(t->r); t->r = s->l; s->l = t; } update(t); return update(s); } Node *submerge(Node *l, Node *r) { if (l->level < r->level) { r = propagate(r); Node *c = (r->l = submerge(l, r->l)); if (r->color == BLACK && c->color == RED && c->l && c->l->color == RED) { r->color = RED; c->color = BLACK; if (r->r->color == BLACK) return rotate(r, true); r->r->color = BLACK; } return update(r); } if (l->level > r->level) { l = propagate(l); Node *c = (l->r = submerge(l->r, r)); if (l->color == BLACK && c->color == RED && c->r && c->r->color == RED) { l->color = RED; c->color = BLACK; if (l->l->color == BLACK) return rotate(l, false); l->l->color = BLACK; } return update(l); } return alloc(l, r); } Node *merge(Node *l, Node *r) { if (!l || !r) return l ? l : r; Node *c = submerge(l, r); c->color = BLACK; return c; } pair<Node *, Node *> split(Node *t, int k) { if (!t) return {nullptr, nullptr}; t = propagate(t); if (k == 0) return {nullptr, t}; if (k >= count(t)) return {t, nullptr}; if (k < count(t->l)) { auto p = split(t->l, k); return {p.first, merge(p.second, t->r)}; } if (k > count(t->l)) { auto p = split(t->r, k - count(t->l)); return {merge(t->l, p.first), p.second}; } pair<Node *, Node *> ret = {t->l, t->r}; pool.free(t); return ret; } Node *build(int l, int r, const vector<Monoid> &v) { if (l + 1 >= r) return alloc(v[l]); return merge(build(l, (l + r) >> 1, v), build((l + r) >> 1, r, v)); } Node *build(const vector<Monoid> &v) { pool.clear(); return build(0, (int)v.size(), v); } void dump(Node *r, typename vector<Monoid>::iterator &it) { r = propagate(r); if (!r->l || !r->r) { *it++ = r->key; return; } dump(r->l, it); dump(r->r, it); } vector<Monoid> dump(Node *r) { vector<Monoid> v((size_t)count(r)); auto it = begin(v); dump(r, it); return v; } string to_string(Node *r) { auto s = dump(r); string ret; for (int i = 0; i < s.size(); i++) { ret += std::to_string(s[i]); ret += ", "; } return (ret); } void insert(Node *&t, int k, const Monoid &v) { auto x = split(t, k); t = merge(merge(x.first, alloc(v)), x.second); } void erase(Node *&t, int k) { auto x = split(t, k); auto y = split(x.second, 1); pool.free(y.first); t = merge(x.first, y.second); } Monoid query(Node *&t, int a, int b) { auto x = split(t, a); auto y = split(x.second, b - a); auto ret = sum(y.first); t = merge(x.first, merge(y.first, y.second)); return ret; } void set_propagate(Node *&t, int a, int b, const OperatorMonoid &p) { auto x = split(t, a); auto y = split(x.second, b - a); y.first->lazy = h(y.first->lazy, p); t = merge(x.first, merge(propagate(y.first), y.second)); } void set_element(Node *&t, int k, const Monoid &x) { if (!t->l) { t->key = t->sum = x; return; } t = propagate(t); if (k < count(t->l)) set_element(t->l, k, x); else set_element(t->r, k - count(t->l), x); t = update(t); } int size(Node *t) { return count(t); } bool empty(Node *t) { return !t; } Node *makeset() { return (nullptr); } }; using int64 = long long; int main() { int n, q; scanf("%d %d", &n, &q); vector<int> v(n); for (int i = 0; i < n; i++) scanf("%d", &v[i]); auto f = [](int a, int b) { return min(a, b); }; const int INF = 1 << 30; RedBlackTree<int, int> rbt(5400000, f, INF); auto root = rbt.build(v); for (int i = 0; i < q; i++) { int x, y, z; scanf("%d %d %d", &x, &y, &z); if (x == 0) { int v = rbt.query(root, z, z + 1); rbt.erase(root, z); rbt.insert(root, y, v); } else if (x == 1) { printf("%d\n", rbt.query(root, y, z + 1)); } else if (x == 2) { rbt.set_element(root, y, z); } if (rbt.pool.ptr < 100) root = rbt.build(rbt.dump(root)); } }
replace
294
295
294
295
-6
terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc
p00998
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; const int INF = 1 << 30; inline int xor32() { static int y = 2463534242; y = y ^ (y << 13); y = y ^ (y >> 17); return (y = y ^ (y << 5)); } struct Node { int Value; int SubTreeSize; Node *Lch, *Rch; int RMQ; Node(int V) : Value(V), SubTreeSize(1), RMQ(V) { Lch = (Node *)NULL; Rch = (Node *)NULL; }; }; inline int Count(Node *t) { if (t == (Node *)NULL) return (0); else return (t->SubTreeSize); } inline int Sum(Node *t) { if (t == (Node *)NULL) return (INF); else return (t->RMQ); } inline Node *Update(Node *t) { t->SubTreeSize = Count(t->Lch) + Count(t->Rch) + 1; t->RMQ = min(Sum(t->Lch), min(Sum(t->Rch), t->Value)); return (t); } inline void Push(Node *t) {} inline Node *MakeRoot(int value) { return (new Node(value)); } Node *Merge(Node *l, Node *r) { Push(l), Push(r); if (l == (Node *)NULL) return (r); if (r == (Node *)NULL) return (l); int Left = l->SubTreeSize; int Right = r->SubTreeSize; if (xor32() % (Left + Right) < Left) { // ???(left)?????? l->Rch = Merge(l->Rch, r); return (Update(l)); } else { // ???(right)?????? r->Lch = Merge(l, r->Lch); return (Update(r)); } } pair<Node *, Node *> Split(Node *t, int k) // [0, k), [k, n) { if (t == (Node *)NULL) return (make_pair((Node *)NULL, (Node *)NULL)); Push(t); if (k <= Count(t->Lch)) { // ?????´????????? pair<Node *, Node *> s = Split(t->Lch, k); t->Lch = s.second; return (make_pair(s.first, Update(t))); } else { // ?????´????????? pair<Node *, Node *> s = Split(t->Rch, k - Count(t->Lch) - 1); t->Rch = s.first; return (make_pair(Update(t), s.second)); } } Node *Insert(Node *root, int pos, int value) { Node *p = MakeRoot(value); pair<Node *, Node *> s = Split(root, pos); return (Merge(Merge(s.first, p), s.second)); } Node *Erase(Node *root, int pos) { pair<Node *, Node *> s = Split(root, pos); pair<Node *, Node *> t = Split(s.second, 1); delete t.first; return (Merge(s.first, t.second)); } void Dump(Node *root) { if (root == (Node *)NULL) return; cout << "("; Dump(root->Lch); cout << "" << root->Value << ""; Dump(root->Rch); cout << ")"; } int main() { Node *root = NULL; int n, q, a, x, y, z; scanf("%d %d", &n, &q); for (int i = 0; i < n; i++) { scanf("%d", &a); root = Insert(root, i, a); } while (q--) { scanf("%d %d %d", &x, &y, &z); if (x == 0) { ++z; pair<Node *, Node *> a, b, c; c = Split(root, z); b = Split(c.first, z - 1); a = Split(b.first, y); root = Merge(Merge(Merge(a.first, b.second), a.second), c.second); } else if (x == 1) { ++z; pair<Node *, Node *> p, q; p = Split(root, y); q = Split(p.second, z - y); printf("%d\n", Sum(q.first)); root = Merge(p.first, Merge(q.first, q.second)); } else { root = Erase(root, y); root = Insert(root, y, z); } } }
#include <bits/stdc++.h> using namespace std; const int INF = 1 << 30; inline int xor32() { static int y = 2463534242; y = y ^ (y << 13); y = y ^ (y >> 17); return (y = y ^ (y << 5)); } struct Node { int Value; int SubTreeSize; Node *Lch, *Rch; int RMQ; Node(int V) : Value(V), SubTreeSize(1), RMQ(V) { Lch = (Node *)NULL; Rch = (Node *)NULL; }; }; inline int Count(Node *t) { if (t == (Node *)NULL) return (0); else return (t->SubTreeSize); } inline int Sum(Node *t) { if (t == (Node *)NULL) return (INF); else return (t->RMQ); } inline Node *Update(Node *t) { t->SubTreeSize = Count(t->Lch) + Count(t->Rch) + 1; t->RMQ = min(Sum(t->Lch), min(Sum(t->Rch), t->Value)); return (t); } inline void Push(Node *t) {} inline Node *MakeRoot(int value) { return (new Node(value)); } Node *Merge(Node *l, Node *r) { Push(l), Push(r); if (l == (Node *)NULL) return (r); if (r == (Node *)NULL) return (l); int Left = l->SubTreeSize; int Right = r->SubTreeSize; if (rand() % (Left + Right) < Left) { // ???(left)?????? l->Rch = Merge(l->Rch, r); return (Update(l)); } else { // ???(right)?????? r->Lch = Merge(l, r->Lch); return (Update(r)); } } pair<Node *, Node *> Split(Node *t, int k) // [0, k), [k, n) { if (t == (Node *)NULL) return (make_pair((Node *)NULL, (Node *)NULL)); Push(t); if (k <= Count(t->Lch)) { // ?????´????????? pair<Node *, Node *> s = Split(t->Lch, k); t->Lch = s.second; return (make_pair(s.first, Update(t))); } else { // ?????´????????? pair<Node *, Node *> s = Split(t->Rch, k - Count(t->Lch) - 1); t->Rch = s.first; return (make_pair(Update(t), s.second)); } } Node *Insert(Node *root, int pos, int value) { Node *p = MakeRoot(value); pair<Node *, Node *> s = Split(root, pos); return (Merge(Merge(s.first, p), s.second)); } Node *Erase(Node *root, int pos) { pair<Node *, Node *> s = Split(root, pos); pair<Node *, Node *> t = Split(s.second, 1); delete t.first; return (Merge(s.first, t.second)); } void Dump(Node *root) { if (root == (Node *)NULL) return; cout << "("; Dump(root->Lch); cout << "" << root->Value << ""; Dump(root->Rch); cout << ")"; } int main() { Node *root = NULL; int n, q, a, x, y, z; scanf("%d %d", &n, &q); for (int i = 0; i < n; i++) { scanf("%d", &a); root = Insert(root, i, a); } while (q--) { scanf("%d %d %d", &x, &y, &z); if (x == 0) { ++z; pair<Node *, Node *> a, b, c; c = Split(root, z); b = Split(c.first, z - 1); a = Split(b.first, y); root = Merge(Merge(Merge(a.first, b.second), a.second), c.second); } else if (x == 1) { ++z; pair<Node *, Node *> p, q; p = Split(root, y); q = Split(p.second, z - y); printf("%d\n", Sum(q.first)); root = Merge(p.first, Merge(q.first, q.second)); } else { root = Erase(root, y); root = Insert(root, y, z); } } }
replace
53
54
53
54
TLE
p00998
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; // RBST (平衡二分探索木) // データ型・最大値用の単位元を指定して使う // TODO: 使い方をまとめる (わすれるので) inline uint32_t xor128() { static uint32_t x = 123456789; static uint32_t y = 362436069; static uint32_t z = 521288629; static uint32_t w = 88675123; uint32_t t; t = x ^ (x << 11); x = y; y = z; z = w; return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); } namespace rbt { template <typename Type> struct node_t { Type val, sum, max_val, min_val; node_t<Type> *lch, *rch, *par; int cap; node_t(int val_) : val(val_), sum(val_), max_val(val_), min_val(val_), cap(1) { lch = rch = par = nullptr; } }; // 扱う要素の型と、max, min に用いる単位元 // (どれと max, min とっても変わらないやつ) template <typename Type, Type max_identity, Type min_identity> struct rbst { using RBST = rbst<Type, max_identity, min_identity>; private: size_t size_; node_t<Type> *root; // 根の情報 void eval(node_t<Type> *t) { root = t, size_ = t ? count(update(t)) : 0; } // 部分木の情報 int count(node_t<Type> *t) { return t ? t->cap : 0; } Type sum(node_t<Type> *t) { return t ? t->sum : 0; } Type max_val(node_t<Type> *t) { return t ? t->max_val : max_identity; } Type min_val(node_t<Type> *t) { return t ? t->min_val : min_identity; } // 部分木に関する情報の再計算 node_t<Type> *update(node_t<Type> *t, Type val = max_identity) { if (val != max_identity) t->val = val; t->cap = count(t->lch) + count(t->rch) + 1; t->sum = sum(t->lch) + sum(t->rch) + t->val; t->max_val = max({max_val(t->lch), max_val(t->rch), t->val}); t->min_val = min({min_val(t->lch), min_val(t->rch), t->val}); if (t->par) update(t->par); return t; } public: node_t<Type> *merge(node_t<Type> *l, node_t<Type> *r) { if (!l || !r) return !l ? r : l; uint32_t rnd = xor128(); double prob = rnd / UINT_MAX; int N = count(l), M = count(r); if (prob < (double)N / (N + M)) { if (l->rch) l->rch->par = nullptr; node_t<Type> *child = merge(l->rch, r); l->rch = child; if (child) child->par = l; return update(l); } else { if (r->lch) r->lch->par = nullptr; node_t<Type> *child = merge(l, r->lch); r->lch = child; if (child) child->par = r; return update(r); } } // 場所 k で split [0, k), [k, n) pair<node_t<Type> *, node_t<Type> *> split(node_t<Type> *t, int k) { if (t == nullptr) return make_pair(nullptr, nullptr); if (k <= count(t->lch)) { if (t->lch) t->lch->par = nullptr; pair<node_t<Type> *, node_t<Type> *> s = split(t->lch, k); t->lch = s.second; if (s.second) s.second->par = t; return make_pair(s.first, update(t)); } else { if (t->rch) t->rch->par = nullptr; pair<node_t<Type> *, node_t<Type> *> s = split(t->rch, k - count(t->lch) - 1); t->rch = s.first; if (s.first) s.first->par = t; return make_pair(update(t), s.second); } } node_t<Type> *insert(node_t<Type> *t, int k, Type val) { pair<node_t<Type> *, node_t<Type> *> lr = split(t, k); node_t<Type> *newElem = new node_t<Type>(val); node_t<Type> *res = merge(lr.first, newElem); res = merge(res, lr.second); return res; } node_t<Type> *erase(node_t<Type> *t, int k) { pair<node_t<Type> *, node_t<Type> *> lr2 = split(t, k + 1); t = lr2.first; pair<node_t<Type> *, node_t<Type> *> lr1 = split(t, k); node_t<Type> *res = merge(lr1.first, lr2.second); return res; } // k 番目の要素 (0-indexed) node_t<Type> *kth_node(node_t<Type> *t, int k) { assert(k < count(t)); if (count(t->lch) >= k + 1) { return kth_node(t->lch, k); } else if (count(t->lch) + 1 == k + 1) { return t; } else { return kth_node(t->rch, k - count(t->lch) - 1); } } Type kth_number(node_t<Type> *t, int k) { assert(k < size()); return kth_node(t, k)->val; } // lower bound (0-indexed) int lower_bound(node_t<Type> *t, Type val, int idx = 0) { if (t == nullptr) return idx; if (t->lch && t->lch->max_val >= val) { return lower_bound(t->lch, val, idx); } if (t->val >= val) { return idx + (count(t) - count(t->rch)) - 1; } if (t->rch && t->rch->max_val >= val) { return lower_bound(t->rch, val, idx + count(t->lch) + 1); } return idx + count(t); } // upper bound (0-indexed) int upper_bound(node_t<Type> *t, Type val, int idx = 0) { if (t == nullptr) return idx; if (t->lch && t->lch->max_val > val) { return upper_bound(t->lch, val, idx); } if (t->val > val) { return idx + (count(t) - count(t->rch)) - 1; } if (t->rch && t->rch->max_val > val) { return lower_bound(t->rch, val, idx + count(t->lch) + 1); } return idx + count(t); } int get_idx(node_t<Type> *t) { if (t->par == nullptr) return count(t->lch); else if (t->par->lch == t) { return get_idx(t->par) - count(t->rch) - 1; } else { return get_idx(t->par) + count(t->lch) + 1; } } // rmq を解く (minmax: 0 で最大、1 で最小) Type rmq_query(int a, int b, int l, int r, node_t<Type> *cur, int minmax) { Type identity = (minmax ? min_identity : max_identity); if (r <= a || b <= l || !cur) return identity; if (a <= l && r <= b) return (minmax ? cur->min_val : cur->max_val); // 答え (現在の頂点を入れることも) int idx = get_idx(cur), nl, nr; Type ans = (a <= idx && idx < b) ? cur->val : identity; auto get = [&](Type A, Type B) { return minmax ? min(A, B) : max(A, B); }; // 左の子へ (右端が縮む) if (cur->lch) { nl = l, nr = r - 1 - count(cur->rch); ans = get(ans, rmq_query(a, b, nl, nr, cur->lch, minmax)); } // 右の子へ (左端が縮む) if (cur->rch) { nl = l + 1 + count(cur->lch), nr = r; ans = get(ans, rmq_query(a, b, nl, nr, cur->rch, minmax)); } return ans; } rbst() { size_ = 0, root = nullptr; } rbst(node_t<Type> *r) { eval(r); } node_t<Type> *get_root() { return root; } size_t size() { return size_; } bool empty() { return size_ == 0; } void insert(Type val, int k) { eval(insert(root, k, val)); } void erase(int k) { eval(erase(root, k)); } pair<RBST, RBST> split(int k) { pair<node_t<Type> *, node_t<Type> *> lr = split(root, k); return make_pair(RBST(lr.first), RBST(lr.second)); } RBST merge(RBST l, RBST r) { eval(merge(l.get_root(), r.get_root())); return RBST(root); } node_t<Type> *kth_node(int k) { return kth_node(root, k); } Type kth_number(int k) { return kth_number(root, k); } int lower_bound(Type val) { return lower_bound(root, val); } int upper_bound(Type val) { return upper_bound(root, val); } void update(Type val, int k) { node_t<Type> *node = kth_node(k); update(node, val); } bool find(Type val) { int idx = lower_bound(val); if (idx >= size_) return false; return kth_number(idx) == val; } Type find_min(int l, int r) { return rmq_query(l, r, 0, size(), root, 1); } Type find_max(int l, int r) { return rmq_query(l, r, 0, size(), root, 0); } void dump_array() { for (int i = 0; i < size(); i++) { fprintf(stderr, "%d ", kth_number(i)); } fprintf(stderr, "\n"); } // みさわさんの根付き木と多分同じフォーマット? void dump_tree(node_t<Type> *t) { if (t == nullptr) return; if (t->lch) { fprintf(stderr, "("); dump_tree(t->lch); fprintf(stderr, ")"); } fprintf(stderr, "[%d", t->val); if (t->par) { fprintf(stderr, "<%d>", t->par->val); } else { fprintf(stderr, "<NA>"); } fprintf(stderr, "]"); if (t->rch) { fprintf(stderr, "("); dump_tree(t->rch); fprintf(stderr, ")"); } } }; // 重複を許さない全順序集合 template <typename Type, Type max_identity, Type min_identity> struct Set : public rbst<Type, max_identity, min_identity> { public: using PRBST = rbst<Type, max_identity, min_identity>; bool empty() { return PRBST::size() == 0; } void insert(Type val) { if (PRBST::find(val)) return; int idx = PRBST::lower_bound(val); PRBST::insert(val, idx); } void erase(Type val) { if (empty() || !PRBST::find(val)) return; int idx = PRBST::lower_bound(val); PRBST::erase(idx); } }; // TODO: multiset も書く } // namespace rbt using RB = rbt::rbst<int, -1, 1 << 28>; int main() { int N, Q; cin >> N >> Q; RB rb; for (int i = 0; i < N; i++) { int val; cin >> val; rb.insert(val, i); } for (int i = 0; i < Q; i++) { int x, y, z; cin >> x >> y >> z; if (x == 0) { RB rb1, rb2, rb3, rb4; tie(rb3, rb4) = rb.split(z + 1); tie(rb2, rb3) = rb3.split(z); tie(rb1, rb2) = rb2.split(y); rb = rb.merge(rb1, rb3); rb = rb.merge(rb, rb2); rb = rb.merge(rb, rb4); } if (x == 1) { cout << rb.find_min(y, z + 1) << endl; } if (x == 2) { rb.update(z, y); } // rb.dump_array(); } return 0; }
#include <bits/stdc++.h> using namespace std; // RBST (平衡二分探索木) // データ型・最大値用の単位元を指定して使う // TODO: 使い方をまとめる (わすれるので) inline uint32_t xor128() { static uint32_t x = 123456789; static uint32_t y = 362436069; static uint32_t z = 521288629; static uint32_t w = 88675123; uint32_t t; t = x ^ (x << 11); x = y; y = z; z = w; return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); } namespace rbt { template <typename Type> struct node_t { Type val, sum, max_val, min_val; node_t<Type> *lch, *rch, *par; int cap; node_t(int val_) : val(val_), sum(val_), max_val(val_), min_val(val_), cap(1) { lch = rch = par = nullptr; } }; // 扱う要素の型と、max, min に用いる単位元 // (どれと max, min とっても変わらないやつ) template <typename Type, Type max_identity, Type min_identity> struct rbst { using RBST = rbst<Type, max_identity, min_identity>; private: size_t size_; node_t<Type> *root; // 根の情報 void eval(node_t<Type> *t) { root = t, size_ = t ? count(update(t)) : 0; } // 部分木の情報 int count(node_t<Type> *t) { return t ? t->cap : 0; } Type sum(node_t<Type> *t) { return t ? t->sum : 0; } Type max_val(node_t<Type> *t) { return t ? t->max_val : max_identity; } Type min_val(node_t<Type> *t) { return t ? t->min_val : min_identity; } // 部分木に関する情報の再計算 node_t<Type> *update(node_t<Type> *t, Type val = max_identity) { if (val != max_identity) t->val = val; t->cap = count(t->lch) + count(t->rch) + 1; t->sum = sum(t->lch) + sum(t->rch) + t->val; t->max_val = max({max_val(t->lch), max_val(t->rch), t->val}); t->min_val = min({min_val(t->lch), min_val(t->rch), t->val}); if (t->par) update(t->par); return t; } public: node_t<Type> *merge(node_t<Type> *l, node_t<Type> *r) { if (!l || !r) return !l ? r : l; uint32_t rnd = xor128(); double prob = (double)rnd / UINT_MAX; // fprintf(stderr, "%.12f\n", prob); int N = count(l), M = count(r); if (prob < (double)N / (N + M)) { if (l->rch) l->rch->par = nullptr; node_t<Type> *child = merge(l->rch, r); l->rch = child; if (child) child->par = l; return update(l); } else { if (r->lch) r->lch->par = nullptr; node_t<Type> *child = merge(l, r->lch); r->lch = child; if (child) child->par = r; return update(r); } } // 場所 k で split [0, k), [k, n) pair<node_t<Type> *, node_t<Type> *> split(node_t<Type> *t, int k) { if (t == nullptr) return make_pair(nullptr, nullptr); if (k <= count(t->lch)) { if (t->lch) t->lch->par = nullptr; pair<node_t<Type> *, node_t<Type> *> s = split(t->lch, k); t->lch = s.second; if (s.second) s.second->par = t; return make_pair(s.first, update(t)); } else { if (t->rch) t->rch->par = nullptr; pair<node_t<Type> *, node_t<Type> *> s = split(t->rch, k - count(t->lch) - 1); t->rch = s.first; if (s.first) s.first->par = t; return make_pair(update(t), s.second); } } node_t<Type> *insert(node_t<Type> *t, int k, Type val) { pair<node_t<Type> *, node_t<Type> *> lr = split(t, k); node_t<Type> *newElem = new node_t<Type>(val); node_t<Type> *res = merge(lr.first, newElem); res = merge(res, lr.second); return res; } node_t<Type> *erase(node_t<Type> *t, int k) { pair<node_t<Type> *, node_t<Type> *> lr2 = split(t, k + 1); t = lr2.first; pair<node_t<Type> *, node_t<Type> *> lr1 = split(t, k); node_t<Type> *res = merge(lr1.first, lr2.second); return res; } // k 番目の要素 (0-indexed) node_t<Type> *kth_node(node_t<Type> *t, int k) { assert(k < count(t)); if (count(t->lch) >= k + 1) { return kth_node(t->lch, k); } else if (count(t->lch) + 1 == k + 1) { return t; } else { return kth_node(t->rch, k - count(t->lch) - 1); } } Type kth_number(node_t<Type> *t, int k) { assert(k < size()); return kth_node(t, k)->val; } // lower bound (0-indexed) int lower_bound(node_t<Type> *t, Type val, int idx = 0) { if (t == nullptr) return idx; if (t->lch && t->lch->max_val >= val) { return lower_bound(t->lch, val, idx); } if (t->val >= val) { return idx + (count(t) - count(t->rch)) - 1; } if (t->rch && t->rch->max_val >= val) { return lower_bound(t->rch, val, idx + count(t->lch) + 1); } return idx + count(t); } // upper bound (0-indexed) int upper_bound(node_t<Type> *t, Type val, int idx = 0) { if (t == nullptr) return idx; if (t->lch && t->lch->max_val > val) { return upper_bound(t->lch, val, idx); } if (t->val > val) { return idx + (count(t) - count(t->rch)) - 1; } if (t->rch && t->rch->max_val > val) { return lower_bound(t->rch, val, idx + count(t->lch) + 1); } return idx + count(t); } int get_idx(node_t<Type> *t) { if (t->par == nullptr) return count(t->lch); else if (t->par->lch == t) { return get_idx(t->par) - count(t->rch) - 1; } else { return get_idx(t->par) + count(t->lch) + 1; } } // rmq を解く (minmax: 0 で最大、1 で最小) Type rmq_query(int a, int b, int l, int r, node_t<Type> *cur, int minmax) { Type identity = (minmax ? min_identity : max_identity); if (r <= a || b <= l || !cur) return identity; if (a <= l && r <= b) return (minmax ? cur->min_val : cur->max_val); // 答え (現在の頂点を入れることも) int idx = get_idx(cur), nl, nr; Type ans = (a <= idx && idx < b) ? cur->val : identity; auto get = [&](Type A, Type B) { return minmax ? min(A, B) : max(A, B); }; // 左の子へ (右端が縮む) if (cur->lch) { nl = l, nr = r - 1 - count(cur->rch); ans = get(ans, rmq_query(a, b, nl, nr, cur->lch, minmax)); } // 右の子へ (左端が縮む) if (cur->rch) { nl = l + 1 + count(cur->lch), nr = r; ans = get(ans, rmq_query(a, b, nl, nr, cur->rch, minmax)); } return ans; } rbst() { size_ = 0, root = nullptr; } rbst(node_t<Type> *r) { eval(r); } node_t<Type> *get_root() { return root; } size_t size() { return size_; } bool empty() { return size_ == 0; } void insert(Type val, int k) { eval(insert(root, k, val)); } void erase(int k) { eval(erase(root, k)); } pair<RBST, RBST> split(int k) { pair<node_t<Type> *, node_t<Type> *> lr = split(root, k); return make_pair(RBST(lr.first), RBST(lr.second)); } RBST merge(RBST l, RBST r) { eval(merge(l.get_root(), r.get_root())); return RBST(root); } node_t<Type> *kth_node(int k) { return kth_node(root, k); } Type kth_number(int k) { return kth_number(root, k); } int lower_bound(Type val) { return lower_bound(root, val); } int upper_bound(Type val) { return upper_bound(root, val); } void update(Type val, int k) { node_t<Type> *node = kth_node(k); update(node, val); } bool find(Type val) { int idx = lower_bound(val); if (idx >= size_) return false; return kth_number(idx) == val; } Type find_min(int l, int r) { return rmq_query(l, r, 0, size(), root, 1); } Type find_max(int l, int r) { return rmq_query(l, r, 0, size(), root, 0); } void dump_array() { for (int i = 0; i < size(); i++) { fprintf(stderr, "%d ", kth_number(i)); } fprintf(stderr, "\n"); } // みさわさんの根付き木と多分同じフォーマット? void dump_tree(node_t<Type> *t) { if (t == nullptr) return; if (t->lch) { fprintf(stderr, "("); dump_tree(t->lch); fprintf(stderr, ")"); } fprintf(stderr, "[%d", t->val); if (t->par) { fprintf(stderr, "<%d>", t->par->val); } else { fprintf(stderr, "<NA>"); } fprintf(stderr, "]"); if (t->rch) { fprintf(stderr, "("); dump_tree(t->rch); fprintf(stderr, ")"); } } }; // 重複を許さない全順序集合 template <typename Type, Type max_identity, Type min_identity> struct Set : public rbst<Type, max_identity, min_identity> { public: using PRBST = rbst<Type, max_identity, min_identity>; bool empty() { return PRBST::size() == 0; } void insert(Type val) { if (PRBST::find(val)) return; int idx = PRBST::lower_bound(val); PRBST::insert(val, idx); } void erase(Type val) { if (empty() || !PRBST::find(val)) return; int idx = PRBST::lower_bound(val); PRBST::erase(idx); } }; // TODO: multiset も書く } // namespace rbt using RB = rbt::rbst<int, -1, 1 << 28>; int main() { int N, Q; cin >> N >> Q; RB rb; for (int i = 0; i < N; i++) { int val; cin >> val; rb.insert(val, i); } for (int i = 0; i < Q; i++) { int x, y, z; cin >> x >> y >> z; if (x == 0) { RB rb1, rb2, rb3, rb4; tie(rb3, rb4) = rb.split(z + 1); tie(rb2, rb3) = rb3.split(z); tie(rb1, rb2) = rb2.split(y); rb = rb.merge(rb1, rb3); rb = rb.merge(rb, rb2); rb = rb.merge(rb, rb4); } if (x == 1) { cout << rb.find_min(y, z + 1) << endl; } if (x == 2) { rb.update(z, y); } // rb.dump_array(); } return 0; }
replace
69
70
69
71
TLE
p00998
C++
Time Limit Exceeded
#include <algorithm> #include <cassert> #include <cstdio> using namespace std; const int INF = int(1.05e9); class Random { private: unsigned int x, y, z, w, t; public: unsigned int rand() { t = x ^ (x << 11); x = y; y = z; z = w; return w = ((w ^ (w >> 19)) ^ (t ^ (t >> 8))); } }; Random rnd; typedef unsigned int uint; template <class T> struct Node { T value, mini; Node *child[2]; uint priority; int size; Node() {} Node(T val, uint pri) : value(val), mini(val), priority(pri), size(1) { child[0] = child[1] = 0; } }; template <class T> inline Node<T> *allocateNode(T value, uint priority) { assert(0); return new Node<T>(value, priority); } template <class T> inline void freeNode(Node<T> *node) { assert(0); free(node); } template <class T> inline int getSize(Node<T> *r) { return !r ? 0 : r->size; } template <class T> inline Node<T> *getNode(Node<T> *r, int k) { int left = getSize(r->child[0]), which = (k > left); if (left == k) { return r; } return getNode(r->child[which], k - (which ? (left + 1) : 0)); } template <class T> inline Node<T> *update(Node<T> *r) { r->size = getSize(r->child[0]) + getSize(r->child[1]) + 1; r->mini = r->value; for (int i = 0; i < 2; ++i) if (r->child[i]) { r->mini = min(r->mini, r->child[i]->mini); } return r; } template <class T> inline Node<T> *updateValue(Node<T> *r, int k, T val) { int left = getSize(r->child[0]), which = (k > left); if (left == k) { r->value = val; return update(r); } if (r->child[which]) { r->child[which] = updateValue(r->child[which], k - (which ? (left + 1) : 0), val); } return update(r); } template <class T> inline T queryValue(Node<T> *r, int from, int to) { if (to <= from) { return INF; } if (to - from == getSize(r)) { return r->mini; } int left = getSize(r->child[0]); if (to <= left) { return queryValue(r->child[0], from, to); } if (left < from) { return queryValue(r->child[1], from - left - 1, to - left - 1); } return min(r->value, min(queryValue(r->child[0], from, left), queryValue(r->child[1], 0, to - left - 1))); } template <class T> inline Node<T> *rotate(Node<T> *r, int which) { Node<T> *opposite = r->child[which ^ 1]; r->child[which ^ 1] = opposite->child[which]; opposite->child[which] = r; update(r); update(opposite); return opposite; } /* template<class T> inline Node<T> *insert(Node<T> *r, int k, T value, uint priority) { if (!r) { assert(k == 0); return allocateNode(value, priority); } assert(0 <= k && k <= r->size); int left = getSize(r->child[0]), which = (k > left); r->child[which] = insert(r->child[which], k - (which ? (left + 1) : 0), value, priority); update(r); if (r->priority > r->child[which]->priority) { r = rotate(r, which^1); } return r; } template<class T> inline Node<T> *insert(Node<T> *r, int k, T value) { return insert(r, k, value, rnd.rand()); } template<class T> inline Node<T> *erase(Node<T> *r, int k) { assert(r); if (r->size == 1) { assert(k == 0); freeNode(r); return 0; } int left = getSize(r->child[0]), which = (k > left); if (left == k) { r = rotate(r, left > 0); return erase(r, k); } r->child[which] = erase(r->child[which], k - (which ? (left + 1) : 0)); update(r); return r; } */ template <class T> inline Node<T> *merge(Node<T> *left, Node<T> *right) { if (!left || !right) { return !left ? right : left; } if (left->priority > right->priority) { left->child[1] = merge(left->child[1], right); return update(left); } else { right->child[0] = merge(left, right->child[0]); return update(right); } } template <class T> inline pair<Node<T> *, Node<T> *> split(Node<T> *r, int k) { if (!r) { return pair<Node<T> *, Node<T> *>(0, 0); } const int left = getSize(r->child[0]); if (k <= left) { pair<Node<T> *, Node<T> *> ret = split(r->child[0], k); r->child[0] = ret.second; return make_pair(ret.first, update(r)); } else { pair<Node<T> *, Node<T> *> ret = split(r->child[1], k - (left + 1)); r->child[1] = ret.first; return make_pair(update(r), ret.second); } } template <class T> inline void freeAllNode(Node<T> *r) { if (r) { freeAllNode(r->child[0]); freeAllNode(r->child[1]); freeNode(r); } } template <class T> inline Node<T> *insert(Node<T> *r, int k, T value) { pair<Node<T> *, Node<T> *> sp = split(r, k); return merge(merge(sp.first, allocateNode(value, rnd.rand())), sp.second); } template <class T> inline Node<T> *shift(Node<T> *r, int y, int z) { pair<Node<T> *, Node<T> *> r1 = split(r, z + 1), r2 = split(r1.first, z), r3 = split(r2.first, y); return merge(merge(r3.first, r2.second), merge(r3.second, r1.second)); } const int MAX_NODE = 400000; Node<int> nodes__[MAX_NODE]; int freeNodeStack__[MAX_NODE], *lastStack__; void initNode(int n = MAX_NODE) { for (int i = 0; i < n; ++i) { freeNodeStack__[i] = n - 1 - i; } lastStack__ = freeNodeStack__ + n; } template <> inline Node<int> *allocateNode(int value, uint priority) { return &(nodes__[*--lastStack__] = Node<int>(value, priority)); } template <> inline void freeNode(Node<int> *node) { assert(node); if (node) { *lastStack__ = int(node - nodes__); ++lastStack__; } } int main() { initNode(); rnd.rand(); for (int n, q, x, y, z; scanf("%d%d", &n, &q) == 2;) { Node<int> *tp = 0; for (int i = 0; i < n; ++i) { int a; scanf("%d", &a); tp = insert(tp, i, a); } for (int _ = 0; _ < q; ++_) { scanf("%d%d%d", &x, &y, &z); if (x == 0) { /* int tmp = getNode(tp, z)->value; tp = erase(tp, z); tp = insert(tp, y, tmp); */ tp = shift(tp, y, z); } else if (x == 1) { printf("%d\n", queryValue(tp, y, z + 1)); } else if (x == 2) { tp = updateValue(tp, y, z); } } break; } return 0; }
#include <algorithm> #include <cassert> #include <cstdio> using namespace std; const int INF = int(1.05e9); class Random { private: unsigned int x, y, z, w, t; public: unsigned int rand() { t = x ^ (x << 11); x = y; y = z; z = w; return w = ((w ^ (w >> 19)) ^ (t ^ (t >> 8))); } Random() : x(123456789U), y(362436069U), z(521288629U), w(88675123U) { for (int _ = 0; _ < 538; ++_) { rand(); } } Random(unsigned int w_) : x(123456789U), y(362436069U), z(521288629U), w(w_) { for (int _ = 0; _ < 538; ++_) { rand(); } } }; Random rnd; typedef unsigned int uint; template <class T> struct Node { T value, mini; Node *child[2]; uint priority; int size; Node() {} Node(T val, uint pri) : value(val), mini(val), priority(pri), size(1) { child[0] = child[1] = 0; } }; template <class T> inline Node<T> *allocateNode(T value, uint priority) { assert(0); return new Node<T>(value, priority); } template <class T> inline void freeNode(Node<T> *node) { assert(0); free(node); } template <class T> inline int getSize(Node<T> *r) { return !r ? 0 : r->size; } template <class T> inline Node<T> *getNode(Node<T> *r, int k) { int left = getSize(r->child[0]), which = (k > left); if (left == k) { return r; } return getNode(r->child[which], k - (which ? (left + 1) : 0)); } template <class T> inline Node<T> *update(Node<T> *r) { r->size = getSize(r->child[0]) + getSize(r->child[1]) + 1; r->mini = r->value; for (int i = 0; i < 2; ++i) if (r->child[i]) { r->mini = min(r->mini, r->child[i]->mini); } return r; } template <class T> inline Node<T> *updateValue(Node<T> *r, int k, T val) { int left = getSize(r->child[0]), which = (k > left); if (left == k) { r->value = val; return update(r); } if (r->child[which]) { r->child[which] = updateValue(r->child[which], k - (which ? (left + 1) : 0), val); } return update(r); } template <class T> inline T queryValue(Node<T> *r, int from, int to) { if (to <= from) { return INF; } if (to - from == getSize(r)) { return r->mini; } int left = getSize(r->child[0]); if (to <= left) { return queryValue(r->child[0], from, to); } if (left < from) { return queryValue(r->child[1], from - left - 1, to - left - 1); } return min(r->value, min(queryValue(r->child[0], from, left), queryValue(r->child[1], 0, to - left - 1))); } template <class T> inline Node<T> *rotate(Node<T> *r, int which) { Node<T> *opposite = r->child[which ^ 1]; r->child[which ^ 1] = opposite->child[which]; opposite->child[which] = r; update(r); update(opposite); return opposite; } /* template<class T> inline Node<T> *insert(Node<T> *r, int k, T value, uint priority) { if (!r) { assert(k == 0); return allocateNode(value, priority); } assert(0 <= k && k <= r->size); int left = getSize(r->child[0]), which = (k > left); r->child[which] = insert(r->child[which], k - (which ? (left + 1) : 0), value, priority); update(r); if (r->priority > r->child[which]->priority) { r = rotate(r, which^1); } return r; } template<class T> inline Node<T> *insert(Node<T> *r, int k, T value) { return insert(r, k, value, rnd.rand()); } template<class T> inline Node<T> *erase(Node<T> *r, int k) { assert(r); if (r->size == 1) { assert(k == 0); freeNode(r); return 0; } int left = getSize(r->child[0]), which = (k > left); if (left == k) { r = rotate(r, left > 0); return erase(r, k); } r->child[which] = erase(r->child[which], k - (which ? (left + 1) : 0)); update(r); return r; } */ template <class T> inline Node<T> *merge(Node<T> *left, Node<T> *right) { if (!left || !right) { return !left ? right : left; } if (left->priority > right->priority) { left->child[1] = merge(left->child[1], right); return update(left); } else { right->child[0] = merge(left, right->child[0]); return update(right); } } template <class T> inline pair<Node<T> *, Node<T> *> split(Node<T> *r, int k) { if (!r) { return pair<Node<T> *, Node<T> *>(0, 0); } const int left = getSize(r->child[0]); if (k <= left) { pair<Node<T> *, Node<T> *> ret = split(r->child[0], k); r->child[0] = ret.second; return make_pair(ret.first, update(r)); } else { pair<Node<T> *, Node<T> *> ret = split(r->child[1], k - (left + 1)); r->child[1] = ret.first; return make_pair(update(r), ret.second); } } template <class T> inline void freeAllNode(Node<T> *r) { if (r) { freeAllNode(r->child[0]); freeAllNode(r->child[1]); freeNode(r); } } template <class T> inline Node<T> *insert(Node<T> *r, int k, T value) { pair<Node<T> *, Node<T> *> sp = split(r, k); return merge(merge(sp.first, allocateNode(value, rnd.rand())), sp.second); } template <class T> inline Node<T> *shift(Node<T> *r, int y, int z) { pair<Node<T> *, Node<T> *> r1 = split(r, z + 1), r2 = split(r1.first, z), r3 = split(r2.first, y); return merge(merge(r3.first, r2.second), merge(r3.second, r1.second)); } const int MAX_NODE = 400000; Node<int> nodes__[MAX_NODE]; int freeNodeStack__[MAX_NODE], *lastStack__; void initNode(int n = MAX_NODE) { for (int i = 0; i < n; ++i) { freeNodeStack__[i] = n - 1 - i; } lastStack__ = freeNodeStack__ + n; } template <> inline Node<int> *allocateNode(int value, uint priority) { return &(nodes__[*--lastStack__] = Node<int>(value, priority)); } template <> inline void freeNode(Node<int> *node) { assert(node); if (node) { *lastStack__ = int(node - nodes__); ++lastStack__; } } int main() { initNode(); rnd.rand(); for (int n, q, x, y, z; scanf("%d%d", &n, &q) == 2;) { Node<int> *tp = 0; for (int i = 0; i < n; ++i) { int a; scanf("%d", &a); tp = insert(tp, i, a); } for (int _ = 0; _ < q; ++_) { scanf("%d%d%d", &x, &y, &z); if (x == 0) { /* int tmp = getNode(tp, z)->value; tp = erase(tp, z); tp = insert(tp, y, tmp); */ tp = shift(tp, y, z); } else if (x == 1) { printf("%d\n", queryValue(tp, y, z + 1)); } else if (x == 2) { tp = updateValue(tp, y, z); } } break; } return 0; }
insert
18
18
18
30
TLE
p00998
C++
Time Limit Exceeded
#include <iostream> #include <random> #include <string> #include <vector> #define REP(i, n) for (int i = 0; i < (n); ++i) using namespace std; struct Myrand { random_device rd; mt19937 mt; Myrand() : rd(), mt(rd()) {} int operator()() { return mt(); } } my_rand; typedef int Data; const int INF = 1e8; struct RBST { Data value; RBST *left; RBST *right; int cnt; Data minv; RBST(Data v) : value(v), left(nullptr), right(nullptr), cnt(1), minv(v) {} }; int count(RBST *t) { return !t ? 0 : t->cnt; } Data minv(RBST *t) { return !t ? INF : t->minv; } RBST *update(RBST *t) { t->cnt = count(t->left) + count(t->right) + 1; t->minv = min(min(minv(t->left), minv(t->right)), t->value); return t; } RBST *merge(RBST *l, RBST *r) { if (!l || !r) return !l ? r : l; int cnt_sum = count(l) + count(r); // rand()?????¨?°????????????????????????????????£?????????£??????????????¨??????????????§???mt19937??¨????????????????£?????????? if ((my_rand() % cnt_sum) < count(l)) { l->right = merge(l->right, r); return update(l); } else { r->left = merge(l, r->left); return update(r); } } pair<RBST *, RBST *> split(RBST *t, int k) { if (!t) return make_pair(nullptr, nullptr); if (k <= count(t->left)) { auto s = split(t->left, k); t->left = s.second; return make_pair(s.first, update(t)); } else { auto s = split(t->right, k - count(t->left) - 1); t->right = s.first; return make_pair(update(t), s.second); } } RBST *insert(RBST *t, int k, Data v) { auto s = split(t, k); return merge(s.first, merge(new RBST(v), s.second)); } RBST *erase(RBST *t, int k) { auto s1 = split(t, k); auto s2 = split(s1.second, 1); delete s2.first; return merge(s1.first, s2.second); } int main() { srand(0); RBST *root = nullptr; int n, q; cin >> n >> q; REP(i, n) { int a; cin >> a; root = insert(root, i, a); } REP(i, q) { int x, y, z; cin >> x >> y >> z; switch (x) { case 0: { auto b = split(root, z + 1); auto f = split(b.first, y); auto s = split(f.second, z - y); RBST *ns = merge(s.second, s.first); RBST *nf = merge(f.first, ns); root = merge(nf, b.second); } break; case 1: { auto b = split(root, z + 1); auto f = split(b.first, y); cout << f.second->minv << endl; RBST *nf = merge(f.first, f.second); root = merge(nf, b.second); } break; case 2: { root = erase(root, y); root = insert(root, y, z); } break; } } return 0; }
#include <iostream> #include <random> #include <string> #include <vector> #define REP(i, n) for (int i = 0; i < (n); ++i) using namespace std; struct Myrand { uint64_t val; Myrand() : val(859238598) {} uint64_t operator()() { return val = val * val + 737598437; } } my_rand; typedef int Data; const int INF = 1e8; struct RBST { Data value; RBST *left; RBST *right; int cnt; Data minv; RBST(Data v) : value(v), left(nullptr), right(nullptr), cnt(1), minv(v) {} }; int count(RBST *t) { return !t ? 0 : t->cnt; } Data minv(RBST *t) { return !t ? INF : t->minv; } RBST *update(RBST *t) { t->cnt = count(t->left) + count(t->right) + 1; t->minv = min(min(minv(t->left), minv(t->right)), t->value); return t; } RBST *merge(RBST *l, RBST *r) { if (!l || !r) return !l ? r : l; int cnt_sum = count(l) + count(r); // rand()?????¨?°????????????????????????????????£?????????£??????????????¨??????????????§???mt19937??¨????????????????£?????????? if ((my_rand() % cnt_sum) < count(l)) { l->right = merge(l->right, r); return update(l); } else { r->left = merge(l, r->left); return update(r); } } pair<RBST *, RBST *> split(RBST *t, int k) { if (!t) return make_pair(nullptr, nullptr); if (k <= count(t->left)) { auto s = split(t->left, k); t->left = s.second; return make_pair(s.first, update(t)); } else { auto s = split(t->right, k - count(t->left) - 1); t->right = s.first; return make_pair(update(t), s.second); } } RBST *insert(RBST *t, int k, Data v) { auto s = split(t, k); return merge(s.first, merge(new RBST(v), s.second)); } RBST *erase(RBST *t, int k) { auto s1 = split(t, k); auto s2 = split(s1.second, 1); delete s2.first; return merge(s1.first, s2.second); } int main() { srand(0); RBST *root = nullptr; int n, q; cin >> n >> q; REP(i, n) { int a; cin >> a; root = insert(root, i, a); } REP(i, q) { int x, y, z; cin >> x >> y >> z; switch (x) { case 0: { auto b = split(root, z + 1); auto f = split(b.first, y); auto s = split(f.second, z - y); RBST *ns = merge(s.second, s.first); RBST *nf = merge(f.first, ns); root = merge(nf, b.second); } break; case 1: { auto b = split(root, z + 1); auto f = split(b.first, y); cout << f.second->minv << endl; RBST *nf = merge(f.first, f.second); root = merge(nf, b.second); } break; case 2: { root = erase(root, y); root = insert(root, y, z); } break; } } return 0; }
replace
10
14
10
13
TLE
p00998
C++
Time Limit Exceeded
#define _CRT_SECURE_NO_WARNINGS #include "bits/stdc++.h" using namespace std; #define int long long #define CHOOSE(a) CHOOSE2 a #define CHOOSE2(a0, a1, a2, a3, x, ...) x #define REP1(i, s, cond, cal) for (signed i = signed(s); i cond; i cal) #define REP2(i, s, n) REP1(i, s, < signed(n), ++) #define REP3(i, n) REP2(i, 0, n) #define rep(...) CHOOSE((__VA_ARGS__, REP1, REP2, REP3))(__VA_ARGS__) #define rrep(i, s) rep(i, s, >= 0, --) #define all(c) begin(c), end(c) template <typename T> bool maxup(T &a, T &&b) { if (a < b) { a = b; return true; }; } template <typename T> bool maxup(T &a, T &b) { if (a < b) { a = b; return true; }; } template <typename T> bool minup(T &a, T &&b) { if (a > b) { a = b; return true; }; } template <typename T> bool minup(T &a, T &b) { if (a > b) { a = b; return true; }; } #define X first #define Y second using VV = vector<vector<int>>; using V = vector<int>; using P = pair<int, int>; using IP = pair<int, P>; template <typename T> inline void input(vector<T> &v) { for (auto &x : v) cin >> x; } struct node_t { int val; int pri; int cnt; int min; node_t *lch, *rch; }; int count(node_t *t) { return !t ? 0 : t->cnt; } int min(node_t *t) { return !t ? (1 << 29) : t->min; } node_t *update(node_t *t) { t->cnt = count(t->lch) + count(t->rch) + 1; t->min = min(t->val, min(min(t->lch), min(t->rch))); return t; } node_t *merge(node_t *l, node_t *r) { if (!l || !r) return l ? l : r; if (l->pri > r->pri) { l->rch = merge(l->rch, r); return update(l); } else { r->lch = merge(l, r->lch); return update(r); } } pair<node_t *, node_t *> split(node_t *t, int k) { if (!t) return make_pair(nullptr, nullptr); if (k <= count(t->lch)) { auto s = split(t->lch, k); t->lch = s.second; return make_pair(s.first, update(t)); } else { auto s = split(t->rch, k - count(t->lch) - 1); t->rch = s.first; return make_pair(update(t), s.second); } } node_t *insert(node_t *t, int k, int v) { auto n = new node_t{v, rand(), 1, v}; if (!t) return n; auto x = split(t, k); return merge(merge(x.first, n), x.second); } node_t *erase(node_t *t, int k) { auto x = split(t, k); auto y = split(x.second, 1); return merge(x.first, y.second); } node_t *kth(node_t *t, int k) { if (!t || count(t->lch) == k) return t; return count(t->lch) > k ? kth(t->lch, k) : kth(t->rch, k - count(t->lch) - 1); } int min(node_t *t, int l, int r) { //[l, r) if (!t || (l == 0 && count(t) <= r)) return min(t); int lc = count(t->lch); int ret = min(t->lch, l, r); minup(ret, min(t->rch, l - lc - 1, r - lc - 1)); if (l <= lc && lc < r) { minup(ret, t->val); } return ret; } using Tree = node_t *; void get_all(node_t *t, vector<node_t *> &res) { if (!t) return; get_all(t->lch, res); res.push_back(t); get_all(t->rch, res); } vector<node_t *> get_all(node_t *t) { vector<node_t *> v; get_all(t, v); return v; } void dump(node_t *t) { return; if (!t) return; cout << "top: (" << t->val << ", " << t->min << "), count: " << count(t) << endl; for (auto x : get_all(t)) { cout << " " << (x->val) << " " << x->min << endl; } } signed main() { int n, q; scanf("%lld %lld", &n, &q); Tree t = nullptr; rep(i, n) { int a; scanf("%lld", &a); dump(t); t = insert(t, count(t), a); } dump(t); rep(i, q) { int x, y, z; scanf("%lld %lld %lld", &x, &y, &z); if (x == 0) { auto tmp = kth(t, z); t = erase(t, z); dump(t); t = insert(t, y, tmp->val); dump(t); } if (x == 1) { printf("%lld\n", min(t, y, z + 1)); } if (x == 2) { t = erase(t, y); t = insert(t, y, z); } dump(t); } // system("pause"); }
#define _CRT_SECURE_NO_WARNINGS #include "bits/stdc++.h" using namespace std; #define int long long #define CHOOSE(a) CHOOSE2 a #define CHOOSE2(a0, a1, a2, a3, x, ...) x #define REP1(i, s, cond, cal) for (signed i = signed(s); i cond; i cal) #define REP2(i, s, n) REP1(i, s, < signed(n), ++) #define REP3(i, n) REP2(i, 0, n) #define rep(...) CHOOSE((__VA_ARGS__, REP1, REP2, REP3))(__VA_ARGS__) #define rrep(i, s) rep(i, s, >= 0, --) #define all(c) begin(c), end(c) template <typename T> bool maxup(T &a, T &&b) { if (a < b) { a = b; return true; }; } template <typename T> bool maxup(T &a, T &b) { if (a < b) { a = b; return true; }; } template <typename T> bool minup(T &a, T &&b) { if (a > b) { a = b; return true; }; } template <typename T> bool minup(T &a, T &b) { if (a > b) { a = b; return true; }; } #define X first #define Y second using VV = vector<vector<int>>; using V = vector<int>; using P = pair<int, int>; using IP = pair<int, P>; template <typename T> inline void input(vector<T> &v) { for (auto &x : v) cin >> x; } struct node_t { int val; int pri; int cnt; int min; node_t *lch, *rch; }; int count(node_t *t) { return !t ? 0 : t->cnt; } int min(node_t *t) { return !t ? (1 << 29) : t->min; } node_t *update(node_t *t) { t->cnt = count(t->lch) + count(t->rch) + 1; t->min = min(t->val, min(min(t->lch), min(t->rch))); return t; } node_t *merge(node_t *l, node_t *r) { if (!l || !r) return l ? l : r; if (l->pri > r->pri) { l->rch = merge(l->rch, r); return update(l); } else { r->lch = merge(l, r->lch); return update(r); } } pair<node_t *, node_t *> split(node_t *t, int k) { if (!t) return make_pair(nullptr, nullptr); if (k <= count(t->lch)) { auto s = split(t->lch, k); t->lch = s.second; return make_pair(s.first, update(t)); } else { auto s = split(t->rch, k - count(t->lch) - 1); t->rch = s.first; return make_pair(update(t), s.second); } } node_t *insert(node_t *t, int k, int v) { auto n = new node_t{v, rand(), 1, v}; if (!t) return n; auto x = split(t, k); return merge(merge(x.first, n), x.second); } node_t *erase(node_t *t, int k) { auto x = split(t, k); auto y = split(x.second, 1); return merge(x.first, y.second); } node_t *kth(node_t *t, int k) { if (!t || count(t->lch) == k) return t; return count(t->lch) > k ? kth(t->lch, k) : kth(t->rch, k - count(t->lch) - 1); } int min(node_t *t, int l, int r) { //[l, r) if (!t || (l == 0 && count(t) <= r)) return min(t); int lc = count(t->lch); int ret = min(t->lch, l, r); minup(ret, min(t->rch, l - lc - 1, r - lc - 1)); if (l <= lc && lc < r) { minup(ret, t->val); } return ret; } using Tree = node_t *; void get_all(node_t *t, vector<node_t *> &res) { if (!t) return; get_all(t->lch, res); res.push_back(t); get_all(t->rch, res); } vector<node_t *> get_all(node_t *t) { vector<node_t *> v; get_all(t, v); return v; } void dump(node_t *t) { return; if (!t) return; cout << "top: (" << t->val << ", " << t->min << "), count: " << count(t) << endl; for (auto x : get_all(t)) { cout << " " << (x->val) << " " << x->min << endl; } } signed main() { int n, q; scanf("%lld %lld", &n, &q); Tree t = nullptr; rep(i, n) { int a; scanf("%lld", &a); dump(t); t = insert(t, count(t), a); } dump(t); rep(i, q) { int x, y, z; scanf("%lld %lld %lld", &x, &y, &z); if (x == 0) { auto tmp = kth(t, z); t = erase(t, z); dump(t); t = insert(t, y, tmp->val); dump(t); } if (x == 1) { auto a = split(t, z + 1); auto b = split(a.first, y); printf("%lld\n", b.second->min); t = merge(merge(b.first, b.second), a.second); // printf("%lld\n" , min(t, y, z + 1)); } if (x == 2) { t = erase(t, y); t = insert(t, y, z); } dump(t); } // system("pause"); }
replace
179
180
179
184
TLE
p00998
C++
Runtime Error
#include <bits/stdc++.h> #define FOR(i, a, b) for (int i = a; i < b; i++) #define REP(i, b) FOR(i, 0, b) #define MP make_pair using namespace std; struct Node { Node *l, *r; int d, s, mn; }; using NP = Node *; const int poolSize = 1000000; Node buf[poolSize]; stack<NP> pool; void InitPool() { REP(i, poolSize) pool.push(buf + i); } int Depth(NP x) { return x ? x->d : 0; } int Size(NP x) { return x ? x->s : 0; } const int inf = 1145141919; int Mini(NP x) { return x ? x->mn : inf; } void delNode(NP x) { pool.push(x); } NP newNode(NP l, NP r, int v = inf) { NP x = pool.top(); pool.pop(); *x = Node{l, r, max(Depth(l), Depth(r)) + 1, Size(l) + Size(r), min({v, Mini(l), Mini(r)})}; return x; } Node Merge1(NP a, NP b, NP c) { if (abs(max(Depth(a), Depth(b)) + 1 - Depth(c)) <= 1) return Node{newNode(a, b), c}; else if (abs(max(Depth(b), Depth(c)) + 1 - Depth(a)) <= 1) return Node{a, newNode(b, c)}; Node ret = Node{newNode(a, b->l), newNode(b->r, c)}; delNode(b); return ret; } Node Merge2(NP a, NP b) { if (Depth(a) == Depth(b)) return Node{a, b}; else if (Depth(a) > Depth(b)) { Node m = Merge2(a->r, b); Node ret = Merge1(a->l, m.l, m.r); delNode(a); return ret; } else { Node m = Merge2(a, b->l); Node ret = Merge1(m.l, m.r, b->r); delNode(b); return ret; } } NP Merge(NP a, NP b) { if (!a) return b; if (!b) return a; Node m = Merge2(a, b); return newNode(m.l, m.r); } Node Split(NP x, int k) { if (k <= 0) return Node{NULL, x}; if (Size(x) <= k) return Node{x, NULL}; if (k <= Size(x->l)) { Node s = Split(x->l, k); Node ret = Node{s.l, Merge(s.r, x->r)}; delNode(x); return ret; } else { Node s = Split(x->r, k - Size(x->l)); Node ret = Node{Merge(x->l, s.l), s.r}; delNode(x); return ret; } } void Update(NP x, int k, int v) { if (Size(x) == 1) { x->mn = v; return; } if (k < Size(x->l)) Update(x->l, k, v); else Update(x->r, k - Size(x->l), v); x->mn = min(Mini(x->l), Mini(x->r)); } int read() { int i; scanf("%d", &i); return i; } NP t[100000]; int main() { InitPool(); int n = read(), q = read(); REP(i, n) { t[i] = newNode(NULL, NULL, read()); t[i]->s = 1; } while (n > 1) { int k = n / 2; REP(i, k) t[i] = Merge(t[i * 2], t[i * 2 + 1]); if (k * 2 < n) t[k] = t[k * 2]; n = (n + 1) / 2; } NP root = t[0]; REP(_, q) { int x = read(), y = read(), z = read(); if (x == 0) { Node s1 = Split(root, y); Node s2 = Split(s1.r, z + 1 - y - 1); Node s3 = Split(s2.r, 1); root = Merge(Merge(s1.l, s3.l), Merge(s2.l, s3.r)); } else if (x == 1) { Node s1 = Split(root, y); Node s2 = Split(s1.r, z + 1 - y); printf("%d\n", Mini(s2.l)); root = Merge(s1.l, Merge(s2.l, s2.r)); } else if (x == 2) { Update(root, y, z); } } }
#include <bits/stdc++.h> #define FOR(i, a, b) for (int i = a; i < b; i++) #define REP(i, b) FOR(i, 0, b) #define MP make_pair using namespace std; struct Node { Node *l, *r; int d, s, mn; }; using NP = Node *; const int poolSize = 1000000; Node buf[poolSize]; stack<NP> pool; void InitPool() { REP(i, poolSize) pool.push(buf + i); } int Depth(NP x) { return x ? x->d : 0; } int Size(NP x) { return x ? x->s : 0; } const int inf = 1145141919; int Mini(NP x) { return x ? x->mn : inf; } void delNode(NP x) { pool.push(x); } NP newNode(NP l, NP r, int v = inf) { NP x = pool.top(); pool.pop(); *x = Node{l, r, max(Depth(l), Depth(r)) + 1, Size(l) + Size(r), min({v, Mini(l), Mini(r)})}; return x; } Node Merge1(NP a, NP b, NP c) { if (abs(max(Depth(a), Depth(b)) + 1 - Depth(c)) <= 1) return Node{newNode(a, b), c}; else if (abs(max(Depth(b), Depth(c)) + 1 - Depth(a)) <= 1) return Node{a, newNode(b, c)}; Node ret = Node{newNode(a, b->l), newNode(b->r, c)}; delNode(b); return ret; } Node Merge2(NP a, NP b) { if (Depth(a) == Depth(b)) return Node{a, b}; else if (Depth(a) > Depth(b)) { Node m = Merge2(a->r, b); Node ret = Merge1(a->l, m.l, m.r); delNode(a); return ret; } else { Node m = Merge2(a, b->l); Node ret = Merge1(m.l, m.r, b->r); delNode(b); return ret; } } NP Merge(NP a, NP b) { if (!a) return b; if (!b) return a; Node m = Merge2(a, b); return newNode(m.l, m.r); } Node Split(NP x, int k) { if (k <= 0) return Node{NULL, x}; if (Size(x) <= k) return Node{x, NULL}; if (k <= Size(x->l)) { Node s = Split(x->l, k); Node ret = Node{s.l, Merge(s.r, x->r)}; delNode(x); return ret; } else { Node s = Split(x->r, k - Size(x->l)); Node ret = Node{Merge(x->l, s.l), s.r}; delNode(x); return ret; } } void Update(NP x, int k, int v) { if (Size(x) == 1) { x->mn = v; return; } if (k < Size(x->l)) Update(x->l, k, v); else Update(x->r, k - Size(x->l), v); x->mn = min(Mini(x->l), Mini(x->r)); } int read() { int i; scanf("%d", &i); return i; } NP t[200000]; int main() { InitPool(); int n = read(), q = read(); REP(i, n) { t[i] = newNode(NULL, NULL, read()); t[i]->s = 1; } while (n > 1) { int k = n / 2; REP(i, k) t[i] = Merge(t[i * 2], t[i * 2 + 1]); if (k * 2 < n) t[k] = t[k * 2]; n = (n + 1) / 2; } NP root = t[0]; REP(_, q) { int x = read(), y = read(), z = read(); if (x == 0) { Node s1 = Split(root, y); Node s2 = Split(s1.r, z + 1 - y - 1); Node s3 = Split(s2.r, 1); root = Merge(Merge(s1.l, s3.l), Merge(s2.l, s3.r)); } else if (x == 1) { Node s1 = Split(root, y); Node s2 = Split(s1.r, z + 1 - y); printf("%d\n", Mini(s2.l)); root = Merge(s1.l, Merge(s2.l, s2.r)); } else if (x == 2) { Update(root, y, z); } } }
replace
112
113
112
113
0
p00998
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; typedef pair<int, int> P; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vll; #define pu push #define pb push_back #define mp make_pair #define eps 1e-9 #define INF 2000000000 #define sz(x) ((int)(x).size()) #define fi first #define sec second #define SORT(x) sort((x).begin(), (x).end()) #define all(x) (x).begin(), (x).end() #define EQ(a, b) (abs((a) - (b)) < eps) const int SQ = 1000; const int MAX_N = 200000; deque<int> a[MAX_N / SQ + 10]; multiset<int> Min[MAX_N / SQ + 10]; int N, Q; int main() { scanf("%d %d", &N, &Q); for (int i = 0; i < N; i++) { int x = i / SQ, k; scanf("%d", &k); a[x].pb(k); Min[x].insert(k); } for (int i = 0; i < Q; i++) { int type, l, r, pos, val; scanf("%d", &type); if (type == 0) { scanf("%d %d", &l, &r); int lx = l / SQ, ly = l % SQ, rx = r / SQ, ry = r % SQ; if (lx == rx) { int f = a[rx][ry]; a[rx].erase(a[rx].begin() + ry); a[rx].insert(a[rx].begin() + ly, f); } else { int f = a[rx][ry]; a[rx].erase(a[rx].begin() + ry); Min[rx].erase(Min[rx].find(f)); for (int j = lx; j < rx; j++) { int b = a[j].back(); a[j].pop_back(); Min[j].erase(Min[j].find(b)); a[j + 1].push_front(b); Min[j + 1].insert(b); } a[lx].insert(a[lx].begin() + ly, f); Min[lx].insert(f); } } else if (type == 1) { scanf("%d %d", &l, &r); int lx = l / SQ, ly = l % SQ, rx = r / SQ, ry = r % SQ; int ans = INF; if (lx == rx) { for (int j = ly; j <= ry; j++) ans = min(ans, a[lx][j]); } else { for (int j = ly; j < SQ; j++) ans = min(ans, a[lx][j]); for (int j = lx + 1; j < rx; j++) ans = min(ans, *Min[j].begin()); for (int j = 0; j <= ry; j++) ans = min(ans, a[rx][j]); } printf("%d\n", ans); } else { scanf("%d %d", &pos, &val); int px = pos / SQ, py = pos % SQ; int f = a[px][py]; Min[px].erase(Min[px].find(f)); Min[px].insert(val); a[px][py] = val; } } return 0; }
#include <algorithm> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <functional> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; typedef pair<int, int> P; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vll; #define pu push #define pb push_back #define mp make_pair #define eps 1e-9 #define INF 2000000000 #define sz(x) ((int)(x).size()) #define fi first #define sec second #define SORT(x) sort((x).begin(), (x).end()) #define all(x) (x).begin(), (x).end() #define EQ(a, b) (abs((a) - (b)) < eps) const int SQ = 10000; const int MAX_N = 200000; deque<int> a[MAX_N / SQ + 10]; multiset<int> Min[MAX_N / SQ + 10]; int N, Q; int main() { scanf("%d %d", &N, &Q); for (int i = 0; i < N; i++) { int x = i / SQ, k; scanf("%d", &k); a[x].pb(k); Min[x].insert(k); } for (int i = 0; i < Q; i++) { int type, l, r, pos, val; scanf("%d", &type); if (type == 0) { scanf("%d %d", &l, &r); int lx = l / SQ, ly = l % SQ, rx = r / SQ, ry = r % SQ; if (lx == rx) { int f = a[rx][ry]; a[rx].erase(a[rx].begin() + ry); a[rx].insert(a[rx].begin() + ly, f); } else { int f = a[rx][ry]; a[rx].erase(a[rx].begin() + ry); Min[rx].erase(Min[rx].find(f)); for (int j = lx; j < rx; j++) { int b = a[j].back(); a[j].pop_back(); Min[j].erase(Min[j].find(b)); a[j + 1].push_front(b); Min[j + 1].insert(b); } a[lx].insert(a[lx].begin() + ly, f); Min[lx].insert(f); } } else if (type == 1) { scanf("%d %d", &l, &r); int lx = l / SQ, ly = l % SQ, rx = r / SQ, ry = r % SQ; int ans = INF; if (lx == rx) { for (int j = ly; j <= ry; j++) ans = min(ans, a[lx][j]); } else { for (int j = ly; j < SQ; j++) ans = min(ans, a[lx][j]); for (int j = lx + 1; j < rx; j++) ans = min(ans, *Min[j].begin()); for (int j = 0; j <= ry; j++) ans = min(ans, a[rx][j]); } printf("%d\n", ans); } else { scanf("%d %d", &pos, &val); int px = pos / SQ, py = pos % SQ; int f = a[px][py]; Min[px].erase(Min[px].find(f)); Min[px].insert(val); a[px][py] = val; } } return 0; }
replace
31
32
31
32
TLE
p00998
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; const int PMAX = 350000; struct RMQ { using type = int; static type id() { return INT_MAX; } static type op(type l, type r) { return min(l, r); } }; template <typename M> class avl_tree { using T = typename M::type; struct node { T val, all; node *ch[2]; int dep, size; node() : node(M::id()) {} node(T v, node *l = nullptr, node *r = nullptr) : val(v), all(v), ch{l, r}, dep(1), size(1) {} void *operator new(size_t) { static int p = 0; static node pool[PMAX]; return pool + p++; } }; int depth(node *t) const { return t ? t->dep : 0; } int count(node *t) const { return t ? t->size : 0; } T que(node *t) const { return t ? t->all : M::id(); } node *update(node *t) { t->dep = max(depth(t->ch[0]), depth(t->ch[1])) + 1; t->size = count(t->ch[0]) + 1 + count(t->ch[1]); t->all = M::op(que(t->ch[0]), M::op(t->val, que(t->ch[1]))); return t; } node *rotate(node *t, int b) { node *s = t->ch[1 - b]; t->ch[1 - b] = s->ch[b]; s->ch[b] = t; update(t); return update(s); } node *fix(node *t) { if (!t) return t; if (depth(t->ch[0]) - depth(t->ch[1]) == 2) { if (depth(t->ch[0]->ch[1]) > depth(t->ch[0]->ch[0])) { t->ch[0] = rotate(t->ch[0], 0); } t = rotate(t, 1); } else if (depth(t->ch[0]) - depth(t->ch[1]) == -2) { if (depth(t->ch[1]->ch[0]) > depth(t->ch[1]->ch[1])) { t->ch[1] = rotate(t->ch[1], 1); } t = rotate(t, 0); } return t; } node *insert(node *t, int k, T v) { if (!t) return new node(v); int c = count(t->ch[0]); if (k <= c) t->ch[0] = insert(t->ch[0], k, v); else t->ch[1] = insert(t->ch[1], k - (c + 1), v); return fix(update(t)); } node *erase(node *t) { if (!t) return t; if (!t->ch[0] && !t->ch[1]) return nullptr; if (!t->ch[0] || !t->ch[1]) return t->ch[t->ch[0] == nullptr]; return fix( update(new node(find(t->ch[1], 0)->val, t->ch[0], erase(t->ch[1], 0)))); } node *erase(node *t, int k) { if (!t) return t; int c = count(t->ch[0]); if (k < c) { t->ch[0] = erase(t->ch[0], k); update(t); } else if (k > c) { t->ch[1] = erase(t->ch[1], k - (c + 1)); update(t); } else { t = erase(t); } return fix(t); } node *find(node *t, int k) const { if (!t) return t; int c = count(t->ch[0]); return k < c ? find(t->ch[0], k) : k == c ? t : find(t->ch[1], k - (c + 1)); } void update(node *t, int k, T val) { if (!t) return; int c = count(t->ch[0]); if (k == c) { t->val = val; } else if (k < c) { update(t->ch[0], k, val); } else { update(t->ch[1], k - (c + 1), val); } update(t); } T find(node *t, int l, int r) const { if (!t || r <= 0 || t->size <= l) return M::id(); if (l <= 0 && t->size <= r) return t->all; int c = count(t->ch[0]); return M::op(find(t->ch[0], l, r), M::op((l <= c && c < r ? t->val : M::id()), find(t->ch[1], l - (c + 1), r - (c + 1)))); } node *root; public: avl_tree() : root(nullptr) {} int size() const { return count(root); } void insert(int k, T b) { root = insert(root, k, b); } void erase(int k) { root = erase(root, k); } void update(int k, T val) { update(root, k, val); } T find(int k) const { return find(root, k)->val; } T find(int l, int r) const { return find(root, l, r); } }; int main() { ios::sync_with_stdio(false), cin.tie(0); int n, q; cin >> n >> q; avl_tree<RMQ> tree; for (int i = 0; i < n; i++) { int a; cin >> a; tree.insert(i, a); } while (q--) { int x, y, z; cin >> x >> y >> z; if (x == 0) { int val = tree.find(z); tree.erase(z); tree.insert(y, val); } else if (x == 1) { printf("%d\n", tree.find(y, z + 1)); } else { tree.update(y, z); } } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; const int PMAX = 400000; struct RMQ { using type = int; static type id() { return INT_MAX; } static type op(type l, type r) { return min(l, r); } }; template <typename M> class avl_tree { using T = typename M::type; struct node { T val, all; node *ch[2]; int dep, size; node() : node(M::id()) {} node(T v, node *l = nullptr, node *r = nullptr) : val(v), all(v), ch{l, r}, dep(1), size(1) {} void *operator new(size_t) { static int p = 0; static node pool[PMAX]; return pool + p++; } }; int depth(node *t) const { return t ? t->dep : 0; } int count(node *t) const { return t ? t->size : 0; } T que(node *t) const { return t ? t->all : M::id(); } node *update(node *t) { t->dep = max(depth(t->ch[0]), depth(t->ch[1])) + 1; t->size = count(t->ch[0]) + 1 + count(t->ch[1]); t->all = M::op(que(t->ch[0]), M::op(t->val, que(t->ch[1]))); return t; } node *rotate(node *t, int b) { node *s = t->ch[1 - b]; t->ch[1 - b] = s->ch[b]; s->ch[b] = t; update(t); return update(s); } node *fix(node *t) { if (!t) return t; if (depth(t->ch[0]) - depth(t->ch[1]) == 2) { if (depth(t->ch[0]->ch[1]) > depth(t->ch[0]->ch[0])) { t->ch[0] = rotate(t->ch[0], 0); } t = rotate(t, 1); } else if (depth(t->ch[0]) - depth(t->ch[1]) == -2) { if (depth(t->ch[1]->ch[0]) > depth(t->ch[1]->ch[1])) { t->ch[1] = rotate(t->ch[1], 1); } t = rotate(t, 0); } return t; } node *insert(node *t, int k, T v) { if (!t) return new node(v); int c = count(t->ch[0]); if (k <= c) t->ch[0] = insert(t->ch[0], k, v); else t->ch[1] = insert(t->ch[1], k - (c + 1), v); return fix(update(t)); } node *erase(node *t) { if (!t) return t; if (!t->ch[0] && !t->ch[1]) return nullptr; if (!t->ch[0] || !t->ch[1]) return t->ch[t->ch[0] == nullptr]; return fix( update(new node(find(t->ch[1], 0)->val, t->ch[0], erase(t->ch[1], 0)))); } node *erase(node *t, int k) { if (!t) return t; int c = count(t->ch[0]); if (k < c) { t->ch[0] = erase(t->ch[0], k); update(t); } else if (k > c) { t->ch[1] = erase(t->ch[1], k - (c + 1)); update(t); } else { t = erase(t); } return fix(t); } node *find(node *t, int k) const { if (!t) return t; int c = count(t->ch[0]); return k < c ? find(t->ch[0], k) : k == c ? t : find(t->ch[1], k - (c + 1)); } void update(node *t, int k, T val) { if (!t) return; int c = count(t->ch[0]); if (k == c) { t->val = val; } else if (k < c) { update(t->ch[0], k, val); } else { update(t->ch[1], k - (c + 1), val); } update(t); } T find(node *t, int l, int r) const { if (!t || r <= 0 || t->size <= l) return M::id(); if (l <= 0 && t->size <= r) return t->all; int c = count(t->ch[0]); return M::op(find(t->ch[0], l, r), M::op((l <= c && c < r ? t->val : M::id()), find(t->ch[1], l - (c + 1), r - (c + 1)))); } node *root; public: avl_tree() : root(nullptr) {} int size() const { return count(root); } void insert(int k, T b) { root = insert(root, k, b); } void erase(int k) { root = erase(root, k); } void update(int k, T val) { update(root, k, val); } T find(int k) const { return find(root, k)->val; } T find(int l, int r) const { return find(root, l, r); } }; int main() { ios::sync_with_stdio(false), cin.tie(0); int n, q; cin >> n >> q; avl_tree<RMQ> tree; for (int i = 0; i < n; i++) { int a; cin >> a; tree.insert(i, a); } while (q--) { int x, y, z; cin >> x >> y >> z; if (x == 0) { int val = tree.find(z); tree.erase(z); tree.insert(y, val); } else if (x == 1) { printf("%d\n", tree.find(y, z + 1)); } else { tree.update(y, z); } } return 0; }
replace
4
5
4
5
0
p00998
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using T = int; const T id = INT_MAX; T op(T l, T r) { return min(l, r); } enum COL { BLACK, RED }; struct node; node *update(node *t); struct node { T val, all; node *ch[2]; COL color; int level; int size; node() {} void init(T v) { val = v; all = v; color = BLACK; level = 0; size = 1; ch[0] = ch[1] = nullptr; } void init(node *l, node *r) { val = id; color = RED; ch[0] = l; ch[1] = r; update(this); } }; const int pmax = 5e5; node pool[pmax]; queue<int> deleted; int it = 0; int count(node *t) { return !t ? 0 : t->size; } T que(node *t) { return !t ? id : t->all; } node *update(node *t) { t->size = count(t->ch[0]) + count(t->ch[1]) + (!t->ch[0] || !t->ch[1]); t->all = op(que(t->ch[0]), op(t->val, que(t->ch[1]))); t->level = (t->ch[0] ? t->ch[0]->level + (t->ch[0]->color == BLACK) : 0); return t; } node *new_leaf(T v) { if (!deleted.empty()) { auto pos = deleted.front(); deleted.pop(); pool[pos].init(v); return pool + pos; } assert(it < pmax); pool[it].init(v); return pool + it++; } node *new_node(node *l, node *r) { if (!deleted.empty()) { auto pos = deleted.front(); deleted.pop(); pool[pos].init(l, r); return pool + pos; } assert(it < pmax); pool[it].init(l, r); return pool + it++; } void delete_node(node *t) { deleted.push(t - pool); } node *rotate(node *t, int b) { node *s = t->ch[1 - b]; t->ch[1 - b] = s->ch[b]; s->ch[b] = t; update(t); return update(s); } node *submerge(node *l, node *r) { if (l->level < r->level) { node *c = submerge(l, r->ch[0]); r->ch[0] = c; if (r->color == BLACK && c->color == RED && c->ch[0] && c->ch[0]->color == RED) { if (r->ch[1]->color == BLACK) { r->color = RED; c->color = BLACK; return rotate(r, 1); } c->color = BLACK; r->ch[1]->color = BLACK; r->color = RED; } return update(r); } if (l->level > r->level) { node *c = submerge(l->ch[1], r); l->ch[1] = c; if (l->color == BLACK && c->color == RED && c->ch[1] && c->ch[1]->color == RED) { if (l->ch[0]->color == BLACK) { l->color = RED; c->color = BLACK; return rotate(l, 0); } l->ch[0]->color = BLACK; c->color = BLACK; l->color = RED; } return update(l); } return new_node(l, r); } node *merge(node *l, node *r) { if (!l) return r; if (!r) return l; node *c = submerge(l, r); c->color = BLACK; return c; } pair<node *, node *> split(node *t, int k) { if (!t) return make_pair(nullptr, nullptr); if (k == 0) return make_pair(nullptr, t); if (k >= count(t)) return make_pair(t, nullptr); int c = count(t->ch[0]); node *l = t->ch[0], *r = t->ch[1]; delete_node(t); if (k < c) { pair<node *, node *> p = split(l, k); return make_pair(p.first, merge(p.second, r)); } if (k > c) { pair<node *, node *> p = split(r, k - c); return make_pair(merge(l, p.first), p.second); } return make_pair(l, r); } node *insert(node *t, int k, T v) { pair<node *, node *> s = split(t, k); return merge(merge(s.first, new_leaf(v)), s.second); } node *erase(node *t, int k) { pair<node *, node *> s1 = split(t, k), s2 = split(s1.second, 1); return merge(s1.first, s2.second); } T find(node *t, int l, int r) { if (!t) return id; if (r < 0 || l >= count(t)) return id; if (l <= 0 && r >= t->size) return t->all; int c = count(t->ch[0]), d = c + (!t->ch[0] || !t->ch[1]); return op(!t->ch[0] ? id : find(t->ch[0], l, r), op(l <= c && c < r ? t->val : id, !t->ch[1] ? id : find(t->ch[1], l - d, r - d))); } node *find(node *t, int k) { if (!t) return t; int c = count(t->ch[0]), b = (!t->ch[0] || !t->ch[1]); return c == 0 && b ? t : k < c ? find(t->ch[0], k) : find(t->ch[1], k - c - b); } node *build(const vector<int> &v, int lb, int ub) { if (ub - lb == 1) return new_leaf(v[lb]); int m = (lb + ub) >> 1; return merge(build(v, lb, m), build(v, m, ub)); } int main() { ios::sync_with_stdio(false), cin.tie(0); int n, q; cin >> n >> q; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } node *root = build(a, 0, n); while (q--) { int x, y, z; cin >> x >> y >> z; if (x == 0) { int val = find(root, z)->val; root = insert(erase(root, z), y, val); } else if (x == 1) { printf("%d\n", find(root, y, z + 1)); } else { root = erase(root, y); root = insert(root, y, z); } } return 0; }
#include <bits/stdc++.h> using namespace std; using T = int; const T id = INT_MAX; T op(T l, T r) { return min(l, r); } enum COL { BLACK, RED }; struct node; node *update(node *t); struct node { T val, all; node *ch[2]; COL color; int level; int size; node() {} void init(T v) { val = v; all = v; color = BLACK; level = 0; size = 1; ch[0] = ch[1] = nullptr; } void init(node *l, node *r) { val = id; color = RED; ch[0] = l; ch[1] = r; update(this); } }; const int pmax = 8e5; node pool[pmax]; queue<int> deleted; int it = 0; int count(node *t) { return !t ? 0 : t->size; } T que(node *t) { return !t ? id : t->all; } node *update(node *t) { t->size = count(t->ch[0]) + count(t->ch[1]) + (!t->ch[0] || !t->ch[1]); t->all = op(que(t->ch[0]), op(t->val, que(t->ch[1]))); t->level = (t->ch[0] ? t->ch[0]->level + (t->ch[0]->color == BLACK) : 0); return t; } node *new_leaf(T v) { if (!deleted.empty()) { auto pos = deleted.front(); deleted.pop(); pool[pos].init(v); return pool + pos; } assert(it < pmax); pool[it].init(v); return pool + it++; } node *new_node(node *l, node *r) { if (!deleted.empty()) { auto pos = deleted.front(); deleted.pop(); pool[pos].init(l, r); return pool + pos; } assert(it < pmax); pool[it].init(l, r); return pool + it++; } void delete_node(node *t) { deleted.push(t - pool); } node *rotate(node *t, int b) { node *s = t->ch[1 - b]; t->ch[1 - b] = s->ch[b]; s->ch[b] = t; update(t); return update(s); } node *submerge(node *l, node *r) { if (l->level < r->level) { node *c = submerge(l, r->ch[0]); r->ch[0] = c; if (r->color == BLACK && c->color == RED && c->ch[0] && c->ch[0]->color == RED) { if (r->ch[1]->color == BLACK) { r->color = RED; c->color = BLACK; return rotate(r, 1); } c->color = BLACK; r->ch[1]->color = BLACK; r->color = RED; } return update(r); } if (l->level > r->level) { node *c = submerge(l->ch[1], r); l->ch[1] = c; if (l->color == BLACK && c->color == RED && c->ch[1] && c->ch[1]->color == RED) { if (l->ch[0]->color == BLACK) { l->color = RED; c->color = BLACK; return rotate(l, 0); } l->ch[0]->color = BLACK; c->color = BLACK; l->color = RED; } return update(l); } return new_node(l, r); } node *merge(node *l, node *r) { if (!l) return r; if (!r) return l; node *c = submerge(l, r); c->color = BLACK; return c; } pair<node *, node *> split(node *t, int k) { if (!t) return make_pair(nullptr, nullptr); if (k == 0) return make_pair(nullptr, t); if (k >= count(t)) return make_pair(t, nullptr); int c = count(t->ch[0]); node *l = t->ch[0], *r = t->ch[1]; delete_node(t); if (k < c) { pair<node *, node *> p = split(l, k); return make_pair(p.first, merge(p.second, r)); } if (k > c) { pair<node *, node *> p = split(r, k - c); return make_pair(merge(l, p.first), p.second); } return make_pair(l, r); } node *insert(node *t, int k, T v) { pair<node *, node *> s = split(t, k); return merge(merge(s.first, new_leaf(v)), s.second); } node *erase(node *t, int k) { pair<node *, node *> s1 = split(t, k), s2 = split(s1.second, 1); return merge(s1.first, s2.second); } T find(node *t, int l, int r) { if (!t) return id; if (r < 0 || l >= count(t)) return id; if (l <= 0 && r >= t->size) return t->all; int c = count(t->ch[0]), d = c + (!t->ch[0] || !t->ch[1]); return op(!t->ch[0] ? id : find(t->ch[0], l, r), op(l <= c && c < r ? t->val : id, !t->ch[1] ? id : find(t->ch[1], l - d, r - d))); } node *find(node *t, int k) { if (!t) return t; int c = count(t->ch[0]), b = (!t->ch[0] || !t->ch[1]); return c == 0 && b ? t : k < c ? find(t->ch[0], k) : find(t->ch[1], k - c - b); } node *build(const vector<int> &v, int lb, int ub) { if (ub - lb == 1) return new_leaf(v[lb]); int m = (lb + ub) >> 1; return merge(build(v, lb, m), build(v, m, ub)); } int main() { ios::sync_with_stdio(false), cin.tie(0); int n, q; cin >> n >> q; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } node *root = build(a, 0, n); while (q--) { int x, y, z; cin >> x >> y >> z; if (x == 0) { int val = find(root, z)->val; root = insert(erase(root, z), y, val); } else if (x == 1) { printf("%d\n", find(root, y, z + 1)); } else { root = erase(root, y); root = insert(root, y, z); } } return 0; }
replace
35
36
35
36
0
p00998
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using T = int; const T id = INT_MAX; T op(T l, T r) { return min(l, r); } struct node; node *update(node *t); struct node { T val, all; node *ch[2]; int dep, size; node() {} void init(T v) { val = all = v; dep = size = 1; ch[0] = ch[1] = nullptr; } void init(node *l, node *r) { val = id; ch[0] = l, ch[1] = r; update(this); } }; const int pmax = 5e5; node pool[pmax]; queue<int> deleted; int it = 0; int depth(node *t) { return t ? t->dep : 0; } int count(node *t) { return t ? t->size : 0; } int que(node *t) { return t ? t->all : id; } node *new_leaf(T v) { if (!deleted.empty()) { auto pos = deleted.front(); deleted.pop(); pool[pos].init(v); return pool + pos; } assert(it < pmax); pool[it].init(v); return pool + it++; } node *new_node(node *l, node *r) { if (!deleted.empty()) { auto pos = deleted.front(); deleted.pop(); pool[pos].init(l, r); return pool + pos; } assert(it < pmax); pool[it].init(l, r); return pool + it++; } void delete_node(node *t) { deleted.push(t - pool); } node *update(node *t) { t->dep = max(depth(t->ch[0]), depth(t->ch[1])) + 1; t->all = op(que(t->ch[0]), op(t->val, que(t->ch[1]))); t->size = count(t->ch[0]) + count(t->ch[1]) + (!t->ch[0] || !t->ch[1]); return t; } node *rotate(node *t, int b) { node *s = t->ch[1 - b]; t->ch[1 - b] = s->ch[b]; s->ch[b] = t; t = update(t); s = update(s); return s; } node *fix(node *t) { if (!t) return t; int dd = depth(t->ch[0]) - depth(t->ch[1]); if (abs(dd) < 2) return t; int b = dd < 0; if (depth(t->ch[b]->ch[!b]) > depth(t->ch[b]->ch[b])) { t->ch[b] = rotate(t->ch[b], b); } return rotate(t, !b); } node *merge(node *l, node *r) { if (!l) return r; if (!r) return l; if (l->dep == r->dep) return new_node(l, r); if (l->dep < r->dep) { r->ch[0] = merge(l, r->ch[0]); return fix(update(r)); } l->ch[1] = merge(l->ch[1], r); return fix(update(l)); } pair<node *, node *> split(node *t, int k) { if (!t) return make_pair(nullptr, nullptr); if (k == 0) return make_pair(nullptr, t); if (k >= count(t)) return make_pair(t, nullptr); int c = count(t->ch[0]); node *l = t->ch[0], *r = t->ch[1]; delete_node(t); if (k < c) { pair<node *, node *> p = split(l, k); return make_pair(p.first, merge(p.second, r)); } if (k > c) { pair<node *, node *> p = split(r, k - c); return make_pair(merge(l, p.first), p.second); } return make_pair(l, r); } node *insert(node *t, int k, T v) { pair<node *, node *> s = split(t, k); return merge(merge(s.first, new_leaf(v)), s.second); } node *erase(node *t, int k) { pair<node *, node *> s1 = split(t, k), s2 = split(s1.second, 1); return merge(s1.first, s2.second); } node *find(node *t, int k) { if (!t) return t; int c = count(t->ch[0]), b = (!t->ch[0] || !t->ch[1]); return c == 0 && b ? t : k < c ? find(t->ch[0], k) : find(t->ch[1], k - c - b); } T find(node *t, int l, int r) { if (!t) return id; if (r < 0 || l >= count(t)) return id; if (l <= 0 && r >= t->size) return t->all; int c = count(t->ch[0]), d = c + (!t->ch[0] || !t->ch[1]); return op(!t->ch[0] ? id : find(t->ch[0], l, r), op(l <= c && c < r ? t->val : id, !t->ch[1] ? id : find(t->ch[1], l - d, r - d))); } void update(node *t, int k, T val) { if (!t) return; int c = count(t->ch[0]), b = (!t->ch[0] || !t->ch[1]); if (c == 0 && b) { t->val = val; } else if (k < c) { update(t->ch[0], k, val); } else { update(t->ch[1], k - c - b, val); } update(t); } node *build(const vector<T> &a, int lb, int ub) { if (ub - lb == 1) return new_leaf(a[lb]); int m = (lb + ub) >> 1; return merge(build(a, lb, m), build(a, m, ub)); } int main() { ios::sync_with_stdio(false), cin.tie(0); int n, q; cin >> n >> q; vector<T> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } node *root = build(a, 0, n); while (q--) { int x, y, z; cin >> x >> y >> z; if (x == 0) { int val = find(root, z)->val; root = insert(erase(root, z), y, val); } else if (x == 1) { printf("%d\n", find(root, y, z + 1)); } else { update(root, y, z); } } return 0; }
#include <bits/stdc++.h> using namespace std; using T = int; const T id = INT_MAX; T op(T l, T r) { return min(l, r); } struct node; node *update(node *t); struct node { T val, all; node *ch[2]; int dep, size; node() {} void init(T v) { val = all = v; dep = size = 1; ch[0] = ch[1] = nullptr; } void init(node *l, node *r) { val = id; ch[0] = l, ch[1] = r; update(this); } }; const int pmax = 8e5; node pool[pmax]; queue<int> deleted; int it = 0; int depth(node *t) { return t ? t->dep : 0; } int count(node *t) { return t ? t->size : 0; } int que(node *t) { return t ? t->all : id; } node *new_leaf(T v) { if (!deleted.empty()) { auto pos = deleted.front(); deleted.pop(); pool[pos].init(v); return pool + pos; } assert(it < pmax); pool[it].init(v); return pool + it++; } node *new_node(node *l, node *r) { if (!deleted.empty()) { auto pos = deleted.front(); deleted.pop(); pool[pos].init(l, r); return pool + pos; } assert(it < pmax); pool[it].init(l, r); return pool + it++; } void delete_node(node *t) { deleted.push(t - pool); } node *update(node *t) { t->dep = max(depth(t->ch[0]), depth(t->ch[1])) + 1; t->all = op(que(t->ch[0]), op(t->val, que(t->ch[1]))); t->size = count(t->ch[0]) + count(t->ch[1]) + (!t->ch[0] || !t->ch[1]); return t; } node *rotate(node *t, int b) { node *s = t->ch[1 - b]; t->ch[1 - b] = s->ch[b]; s->ch[b] = t; t = update(t); s = update(s); return s; } node *fix(node *t) { if (!t) return t; int dd = depth(t->ch[0]) - depth(t->ch[1]); if (abs(dd) < 2) return t; int b = dd < 0; if (depth(t->ch[b]->ch[!b]) > depth(t->ch[b]->ch[b])) { t->ch[b] = rotate(t->ch[b], b); } return rotate(t, !b); } node *merge(node *l, node *r) { if (!l) return r; if (!r) return l; if (l->dep == r->dep) return new_node(l, r); if (l->dep < r->dep) { r->ch[0] = merge(l, r->ch[0]); return fix(update(r)); } l->ch[1] = merge(l->ch[1], r); return fix(update(l)); } pair<node *, node *> split(node *t, int k) { if (!t) return make_pair(nullptr, nullptr); if (k == 0) return make_pair(nullptr, t); if (k >= count(t)) return make_pair(t, nullptr); int c = count(t->ch[0]); node *l = t->ch[0], *r = t->ch[1]; delete_node(t); if (k < c) { pair<node *, node *> p = split(l, k); return make_pair(p.first, merge(p.second, r)); } if (k > c) { pair<node *, node *> p = split(r, k - c); return make_pair(merge(l, p.first), p.second); } return make_pair(l, r); } node *insert(node *t, int k, T v) { pair<node *, node *> s = split(t, k); return merge(merge(s.first, new_leaf(v)), s.second); } node *erase(node *t, int k) { pair<node *, node *> s1 = split(t, k), s2 = split(s1.second, 1); return merge(s1.first, s2.second); } node *find(node *t, int k) { if (!t) return t; int c = count(t->ch[0]), b = (!t->ch[0] || !t->ch[1]); return c == 0 && b ? t : k < c ? find(t->ch[0], k) : find(t->ch[1], k - c - b); } T find(node *t, int l, int r) { if (!t) return id; if (r < 0 || l >= count(t)) return id; if (l <= 0 && r >= t->size) return t->all; int c = count(t->ch[0]), d = c + (!t->ch[0] || !t->ch[1]); return op(!t->ch[0] ? id : find(t->ch[0], l, r), op(l <= c && c < r ? t->val : id, !t->ch[1] ? id : find(t->ch[1], l - d, r - d))); } void update(node *t, int k, T val) { if (!t) return; int c = count(t->ch[0]), b = (!t->ch[0] || !t->ch[1]); if (c == 0 && b) { t->val = val; } else if (k < c) { update(t->ch[0], k, val); } else { update(t->ch[1], k - c - b, val); } update(t); } node *build(const vector<T> &a, int lb, int ub) { if (ub - lb == 1) return new_leaf(a[lb]); int m = (lb + ub) >> 1; return merge(build(a, lb, m), build(a, m, ub)); } int main() { ios::sync_with_stdio(false), cin.tie(0); int n, q; cin >> n >> q; vector<T> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } node *root = build(a, 0, n); while (q--) { int x, y, z; cin >> x >> y >> z; if (x == 0) { int val = find(root, z)->val; root = insert(erase(root, z), y, val); } else if (x == 1) { printf("%d\n", find(root, y, z + 1)); } else { update(root, y, z); } } return 0; }
replace
27
28
27
28
0
p00998
C++
Runtime Error
#include <algorithm> #include <cassert> #include <cstdio> #include <cstdlib> #include <ctime> #include <tuple> #include <utility> #include <vector> #define loop(i, a, b) for (int i = (a); i < int(b); i++) #define rep(i, n) loop(i, 0, n) using namespace std; int const inf = 1e9; #define np nullptr struct Node { int val; Node *ch[2]; int pri, cnt, sum; int min; Node(int v, int p) : val(v), pri(p), cnt(1), sum(v), min(v) { ch[0] = ch[1] = np; } // ~Node(){ // if(ch[0]) delete ch[0]; // if(ch[1]) delete ch[1]; // } void *operator new(size_t sz) { static int pos = 0; static char mem[200020 * sizeof(Node)]; Node *res = (Node *)(mem + pos); pos += sizeof(Node); return res; } }; int count(Node *t) { return t == np ? 0 : t->cnt; } int sum(Node *t) { return t == np ? 0 : t->sum; } int min(Node *t) { return t == np ? inf : t->min; } void show(Node *n, int ofs = 0) { if (n == nullptr) return; show(n->ch[0], ofs + 1); rep(i, ofs * 2) putchar(' '); printf("v:%d m:%d (%p)\n", n->val, n->min, n); show(n->ch[1], ofs + 1); } void update(Node *t) { t->cnt = count(t->ch[0]) + count(t->ch[1]) + 1; t->sum = sum(t->ch[0]) + sum(t->ch[1]) + t->val; t->min = min({min(t->ch[0]), min(t->ch[1]), t->val}); } Node *merge(Node *l, Node *r) { if (l == np || r == np) return l == np ? r : l; if (l->pri > r->pri) { l->ch[1] = merge(l->ch[1], r); update(l); return l; } else { r->ch[0] = merge(l, r->ch[0]); update(r); return r; } } pair<Node *, Node *> split(Node *t, int k) { if (t == np) return make_pair(np, np); if (k <= count(t->ch[0])) { auto s = split(t->ch[0], k); t->ch[0] = s.second; update(t); return make_pair(s.first, t); } else { k -= count(t->ch[0]) + 1; auto s = split(t->ch[1], k); t->ch[1] = s.first; update(t); return make_pair(t, s.second); } } Node *erase(Node *t, int k) { Node *l, *m, *r; tie(l, m) = split(t, k); tie(m, r) = split(m, 1); // delete m; return merge(l, r); } Node *at(Node *t, int k) { int lc = count(t->ch[0]); if (k < lc) return at(t->ch[0], k); else if (k == lc) return t; else return at(t->ch[1], k - lc - 1); } Node *insert(Node *t, int k, int v) { Node *l, *m = new Node(v, rand()), *r; tie(l, r) = split(t, k); return merge(merge(l, m), r); } int min(Node *t, int l, int r) { if (r - l <= 0) return inf; if (r - l == count(t)) return min(t); int s = count(t->ch[0]); if (l == s && r - s == 1) return t->val; int res = inf; if (l < s) res = min(res, min(t->ch[0], l, min(s, r))); if (l <= s && s < r) res = min(res, min(t, s, s + 1)); if (s + 1 < r) res = min(res, min(t->ch[1], max(0, l - s - 1), r - s - 1)); return res; } int get() { int x; scanf("%d", &x); return x; } void put(int x) { // printf("ans : %d\n",x); printf("%d\n", x); } int main() { #ifdef DEBUG freopen("in", "r", stdin); #endif srand(time(0)); int n = get(), q = get(); Node *root = np; rep(i, n) root = insert(root, i, get()); rep(i, q) { int op = get(); if (false) { } else if (op == 0) { int l = get(), r = get(); int x = at(root, r)->val; root = erase(root, r); root = insert(root, l, x); } else if (op == 1) { int l = get(), r = get(); r++; put(min(root, l, r)); } else if (op == 2) { int p = get(), v = get(); root = erase(root, p); root = insert(root, p, v); } } // delete root; }
#include <algorithm> #include <cassert> #include <cstdio> #include <cstdlib> #include <ctime> #include <tuple> #include <utility> #include <vector> #define loop(i, a, b) for (int i = (a); i < int(b); i++) #define rep(i, n) loop(i, 0, n) using namespace std; int const inf = 1e9; #define np nullptr struct Node { int val; Node *ch[2]; int pri, cnt, sum; int min; Node(int v, int p) : val(v), pri(p), cnt(1), sum(v), min(v) { ch[0] = ch[1] = np; } // ~Node(){ // if(ch[0]) delete ch[0]; // if(ch[1]) delete ch[1]; // } void *operator new(size_t sz) { static int pos = 0; static char mem[400020 * sizeof(Node)]; Node *res = (Node *)(mem + pos); pos += sizeof(Node); return res; } }; int count(Node *t) { return t == np ? 0 : t->cnt; } int sum(Node *t) { return t == np ? 0 : t->sum; } int min(Node *t) { return t == np ? inf : t->min; } void show(Node *n, int ofs = 0) { if (n == nullptr) return; show(n->ch[0], ofs + 1); rep(i, ofs * 2) putchar(' '); printf("v:%d m:%d (%p)\n", n->val, n->min, n); show(n->ch[1], ofs + 1); } void update(Node *t) { t->cnt = count(t->ch[0]) + count(t->ch[1]) + 1; t->sum = sum(t->ch[0]) + sum(t->ch[1]) + t->val; t->min = min({min(t->ch[0]), min(t->ch[1]), t->val}); } Node *merge(Node *l, Node *r) { if (l == np || r == np) return l == np ? r : l; if (l->pri > r->pri) { l->ch[1] = merge(l->ch[1], r); update(l); return l; } else { r->ch[0] = merge(l, r->ch[0]); update(r); return r; } } pair<Node *, Node *> split(Node *t, int k) { if (t == np) return make_pair(np, np); if (k <= count(t->ch[0])) { auto s = split(t->ch[0], k); t->ch[0] = s.second; update(t); return make_pair(s.first, t); } else { k -= count(t->ch[0]) + 1; auto s = split(t->ch[1], k); t->ch[1] = s.first; update(t); return make_pair(t, s.second); } } Node *erase(Node *t, int k) { Node *l, *m, *r; tie(l, m) = split(t, k); tie(m, r) = split(m, 1); // delete m; return merge(l, r); } Node *at(Node *t, int k) { int lc = count(t->ch[0]); if (k < lc) return at(t->ch[0], k); else if (k == lc) return t; else return at(t->ch[1], k - lc - 1); } Node *insert(Node *t, int k, int v) { Node *l, *m = new Node(v, rand()), *r; tie(l, r) = split(t, k); return merge(merge(l, m), r); } int min(Node *t, int l, int r) { if (r - l <= 0) return inf; if (r - l == count(t)) return min(t); int s = count(t->ch[0]); if (l == s && r - s == 1) return t->val; int res = inf; if (l < s) res = min(res, min(t->ch[0], l, min(s, r))); if (l <= s && s < r) res = min(res, min(t, s, s + 1)); if (s + 1 < r) res = min(res, min(t->ch[1], max(0, l - s - 1), r - s - 1)); return res; } int get() { int x; scanf("%d", &x); return x; } void put(int x) { // printf("ans : %d\n",x); printf("%d\n", x); } int main() { #ifdef DEBUG freopen("in", "r", stdin); #endif srand(time(0)); int n = get(), q = get(); Node *root = np; rep(i, n) root = insert(root, i, get()); rep(i, q) { int op = get(); if (false) { } else if (op == 0) { int l = get(), r = get(); int x = at(root, r)->val; root = erase(root, r); root = insert(root, l, x); } else if (op == 1) { int l = get(), r = get(); r++; put(min(root, l, r)); } else if (op == 2) { int p = get(), v = get(); root = erase(root, p); root = insert(root, p, v); } } // delete root; }
replace
30
31
30
31
0
p00998
C++
Runtime Error
#include <algorithm> #include <cassert> #include <cstdio> #include <cstdlib> #include <ctime> #include <tuple> #include <utility> #include <vector> #define loop(i, a, b) for (int i = (a); i < int(b); i++) #define rep(i, n) loop(i, 0, n) using namespace std; int const inf = 1e9; #define np (Node *)nullptr unsigned int xor128() { static unsigned int x = 123456789, y = 362436069, z = 521288629, w = time(0); unsigned long t = x ^ (x << 11); x = y; y = z; z = w; return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); } unsigned int xor128r(int n) { return xor128() % n; } double xor128d() { return xor128() * 0.00000000023283064365386962890625; } struct Node { int val; Node *ch[2]; int pri, cnt, sum_; int min_; Node(int v, int p) : val(v), pri(p), cnt(1), sum_(v), min_(v) { ch[0] = ch[1] = np; } // ~Node(){ // if(ch[0]) delete ch[0]; // if(ch[1]) delete ch[1]; // } void *operator new(size_t sz) { static int pos = 0; static char mem[400020 * sizeof(Node)]; Node *res = (Node *)(mem + pos); pos += sizeof(Node); return res; } static int count(Node *t) { return t == np ? 0 : t->cnt; } static int sum(Node *t) { return t == np ? 0 : t->sum_; } static int min(Node *t) { return t == np ? inf : t->min_; } void show(int ofs = 0) { if (ch[0]) ch[0]->show(ofs + 1); rep(i, ofs * 2) putchar(' '); printf("v:%d m:%d (%p)\n", val, min_, this); if (ch[1]) ch[1]->show(ofs + 1); } void update() { cnt = count(ch[0]) + count(ch[1]) + 1; sum_ = sum(ch[0]) + sum(ch[1]) + val; min_ = std::min({min(ch[0]), min(ch[1]), val}); } Node *merge(Node *r) { if (this == np || r == np) return this == np ? r : this; if (pri > r->pri) { ch[1] = ch[1]->merge(r); update(); return this; } else { r->ch[0] = merge(r->ch[0]); r->update(); return r; } } pair<Node *, Node *> split(int k) { int lc = count(ch[0]); if (k <= lc) { auto s = ch[0] ? ch[0]->split(k) : make_pair(np, np); ch[0] = s.second; update(); return make_pair(s.first, this); } else { auto s = ch[1] ? ch[1]->split(k - lc - 1) : make_pair(np, np); ch[1] = s.first; update(); return make_pair(this, s.second); } } Node *erase(int k) { Node *l, *m, *r; tie(l, m) = split(k); tie(m, r) = m->split(1); delete m; return l->merge(r); } Node *at(int k) { int lc = count(ch[0]); if (k < lc) return ch[0]->at(k); else if (k == lc) return this; else return ch[1]->at(k - lc - 1); } void set(int k, int x) { int lc = count(ch[0]); if (k < lc) ch[0]->set(k, x); else if (k == lc) val = x; else if (k > lc) ch[1]->set(k - lc - 1, x); update(); } Node *insert(int k, int v) { Node *l, *m = new Node(v, xor128()), *r; tie(l, r) = split(k); return l->merge(m)->merge(r); // merge(merge(l,m),r); } int min(int l, int r) { if (r - l <= 0) return inf; if (r - l == count(this)) return min(this); int lc = count(ch[0]); if (l == lc && r - lc == 1) return val; int res = inf; if (l < lc) res = std::min(res, ch[0]->min(l, std::min(lc, r))); if (l <= lc && lc < r) res = std::min(res, min(lc, lc + 1)); if (lc + 1 < r) res = std::min(res, ch[1]->min(max(0, l - lc - 1), r - lc - 1)); return res; } static int min(Node *t, int l, int r) { if (r - l <= 0) return inf; if (r - l == count(t)) return min(t); int lc = count(t->ch[0]); if (l == lc && r - lc == 1) return t->val; int res = inf; if (l < lc) res = std::min(res, min(t->ch[0], l, std::min(lc, r))); if (l <= lc && lc < r) res = std::min(res, min(t, lc, lc + 1)); if (lc + 1 < r) res = std::min(res, min(t->ch[1], max(0, l - lc - 1), r - lc - 1)); return res; } }; struct Treap { Node *root; int size; Treap() : root(nullptr), size(0) {} void shift(int l, int r) { int tmp = root->at(r)->val; root = root->erase(r); root = root->insert(l, tmp); } void set(int k, int x) { root->set(k, x); } int rangemin(int l, int r) { return Node::min(root, l, r); } void push_back(int x) { if (size == 0) root = new Node(x, xor128()), size++; else root = root->insert(size++, x); } }; int get() { int x; scanf("%d", &x); return x; } void put(int x) { // printf("ans : %d\n",x); printf("%d\n", x); } int main() { #ifdef DEBUG freopen("in", "r", stdin); #endif int n = get(), q = get(); Treap t; rep(i, n) t.push_back(get()); rep(i, q) { int op = get(); if (false) { } else if (op == 0) { int l = get(), r = get(); t.shift(l, r); } else if (op == 1) { int l = get(), r = get(); r++; put(t.rangemin(l, r)); } else if (op == 2) { int p = get(), v = get(); t.set(p, v); } } }
#include <algorithm> #include <cassert> #include <cstdio> #include <cstdlib> #include <ctime> #include <tuple> #include <utility> #include <vector> #define loop(i, a, b) for (int i = (a); i < int(b); i++) #define rep(i, n) loop(i, 0, n) using namespace std; int const inf = 1e9; #define np (Node *)nullptr unsigned int xor128() { static unsigned int x = 123456789, y = 362436069, z = 521288629, w = time(0); unsigned long t = x ^ (x << 11); x = y; y = z; z = w; return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); } unsigned int xor128r(int n) { return xor128() % n; } double xor128d() { return xor128() * 0.00000000023283064365386962890625; } struct Node { int val; Node *ch[2]; int pri, cnt, sum_; int min_; Node(int v, int p) : val(v), pri(p), cnt(1), sum_(v), min_(v) { ch[0] = ch[1] = np; } // ~Node(){ // if(ch[0]) delete ch[0]; // if(ch[1]) delete ch[1]; // } void *operator new(size_t sz) { static int pos = 0; static char mem[400020 * sizeof(Node)]; Node *res = (Node *)(mem + pos); pos += sizeof(Node); return res; } static int count(Node *t) { return t == np ? 0 : t->cnt; } static int sum(Node *t) { return t == np ? 0 : t->sum_; } static int min(Node *t) { return t == np ? inf : t->min_; } void show(int ofs = 0) { if (ch[0]) ch[0]->show(ofs + 1); rep(i, ofs * 2) putchar(' '); printf("v:%d m:%d (%p)\n", val, min_, this); if (ch[1]) ch[1]->show(ofs + 1); } void update() { cnt = count(ch[0]) + count(ch[1]) + 1; sum_ = sum(ch[0]) + sum(ch[1]) + val; min_ = std::min({min(ch[0]), min(ch[1]), val}); } Node *merge(Node *r) { if (this == np || r == np) return this == np ? r : this; if (pri > r->pri) { ch[1] = ch[1]->merge(r); update(); return this; } else { r->ch[0] = merge(r->ch[0]); r->update(); return r; } } pair<Node *, Node *> split(int k) { int lc = count(ch[0]); if (k <= lc) { auto s = ch[0] ? ch[0]->split(k) : make_pair(np, np); ch[0] = s.second; update(); return make_pair(s.first, this); } else { auto s = ch[1] ? ch[1]->split(k - lc - 1) : make_pair(np, np); ch[1] = s.first; update(); return make_pair(this, s.second); } } Node *erase(int k) { Node *l, *m, *r; tie(l, m) = split(k); tie(m, r) = m->split(1); // delete m; return l->merge(r); } Node *at(int k) { int lc = count(ch[0]); if (k < lc) return ch[0]->at(k); else if (k == lc) return this; else return ch[1]->at(k - lc - 1); } void set(int k, int x) { int lc = count(ch[0]); if (k < lc) ch[0]->set(k, x); else if (k == lc) val = x; else if (k > lc) ch[1]->set(k - lc - 1, x); update(); } Node *insert(int k, int v) { Node *l, *m = new Node(v, xor128()), *r; tie(l, r) = split(k); return l->merge(m)->merge(r); // merge(merge(l,m),r); } int min(int l, int r) { if (r - l <= 0) return inf; if (r - l == count(this)) return min(this); int lc = count(ch[0]); if (l == lc && r - lc == 1) return val; int res = inf; if (l < lc) res = std::min(res, ch[0]->min(l, std::min(lc, r))); if (l <= lc && lc < r) res = std::min(res, min(lc, lc + 1)); if (lc + 1 < r) res = std::min(res, ch[1]->min(max(0, l - lc - 1), r - lc - 1)); return res; } static int min(Node *t, int l, int r) { if (r - l <= 0) return inf; if (r - l == count(t)) return min(t); int lc = count(t->ch[0]); if (l == lc && r - lc == 1) return t->val; int res = inf; if (l < lc) res = std::min(res, min(t->ch[0], l, std::min(lc, r))); if (l <= lc && lc < r) res = std::min(res, min(t, lc, lc + 1)); if (lc + 1 < r) res = std::min(res, min(t->ch[1], max(0, l - lc - 1), r - lc - 1)); return res; } }; struct Treap { Node *root; int size; Treap() : root(nullptr), size(0) {} void shift(int l, int r) { int tmp = root->at(r)->val; root = root->erase(r); root = root->insert(l, tmp); } void set(int k, int x) { root->set(k, x); } int rangemin(int l, int r) { return Node::min(root, l, r); } void push_back(int x) { if (size == 0) root = new Node(x, xor128()), size++; else root = root->insert(size++, x); } }; int get() { int x; scanf("%d", &x); return x; } void put(int x) { // printf("ans : %d\n",x); printf("%d\n", x); } int main() { #ifdef DEBUG freopen("in", "r", stdin); #endif int n = get(), q = get(); Treap t; rep(i, n) t.push_back(get()); rep(i, q) { int op = get(); if (false) { } else if (op == 0) { int l = get(), r = get(); t.shift(l, r); } else if (op == 1) { int l = get(), r = get(); r++; put(t.rangemin(l, r)); } else if (op == 2) { int p = get(), v = get(); t.set(p, v); } } }
replace
99
100
99
100
-6
munmap_chunk(): invalid pointer
p00998
C++
Time Limit Exceeded
#include <algorithm> #include <cassert> #include <cstdio> #include <cstdlib> #include <ctime> #include <iostream> #include <tuple> #include <utility> #include <vector> #define loop(i, a, b) for (int i = (a); i < int(b); i++) #define rep(i, n) loop(i, 0, n) using namespace std; int const inf = 1e9; unsigned int xor128() { static unsigned int x = 123456789, y = 362436069, z = 521288629, w = time(0); unsigned long t = x ^ (x << 11); x = y; y = z; z = w; return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); } template <class T> struct Node { T val; Node *ch[2]; int pri, cnt; T min_; static Node *nil; Node(T v, int p) : val(v), pri(p), cnt(1), min_(v) { ch[0] = ch[1] = nil; } void *operator new(size_t) { static int pos = 0; static char mem[600020 * sizeof(Node)]; Node *res = (Node *)(mem + pos); pos += sizeof(Node); return res; } int size() { return this->cnt; } T min() { return this->min_; } T sum() { return this->sum_; } void show(int ofs = 0) { if (ch[0] != nil) ch[0]->show(ofs + 1); rep(i, ofs * 2) putchar(' '); printf("v:%d m:%d (%p)\n", val, min_, this); if (ch[1] != nil) ch[1]->show(ofs + 1); } void update() { cnt = ch[0]->size() + ch[1]->size() + 1; min_ = std::min({ch[0]->min(), ch[1]->min(), val}); } Node *merge(Node *r) { if (this == nil || r == nil) return this == nil ? r : this; if (pri > r->pri) { ch[1] = ch[1]->merge(r); update(); return this; } else { r->ch[0] = merge(r->ch[0]); r->update(); return r; } } pair<Node *, Node *> split(int k) { int lc = ch[0]->size(); if (k <= lc) { auto s = ch[0] != nil ? ch[0]->split(k) : make_pair(nil, nil); ch[0] = s.second; update(); return make_pair(s.first, this); } else { auto s = ch[1] != nil ? ch[1]->split(k - lc - 1) : make_pair(nil, nil); ch[1] = s.first; update(); return make_pair(this, s.second); } } Node *erase(int k) { Node *l, *m, *r; tie(l, m) = split(k); tie(m, r) = m->split(1); // delete m; return l->merge(r); } Node *at(int k) { int lc = ch[0]->size(); if (k < lc) return ch[0]->at(k); else if (k == lc) return this; else return ch[1]->at(k - lc - 1); } void set(int k, T x) { int lc = ch[0]->size(); if (k < lc) ch[0]->set(k, x); else if (k == lc) val = x; else if (k > lc) ch[1]->set(k - lc - 1, x); update(); } Node *insert(int k, T v) { Node *l, *m = new Node(v, xor128()), *r; tie(l, r) = split(k); return l->merge(m)->merge(r); } T min(int l, int r) { if (r - l <= 0) return inf; int lc = ch[0]->size(); T res = inf; if (l <= lc && lc < r) res = std::min(res, this->val); res = std::min(res, ch[0]->min(l, std::min(r, lc))); res = std::min(res, ch[1]->min(std::max(0, l - lc - 1), r - lc - 1)); return res; // if(r - l <= 0) return inf; // if(r - l == this->size()) return this->min(); // int lc = ch[0]->size(); // if(l == lc && r - lc == 1) return val; // T res = inf; // if(l < lc) res = std::min(res, ch[0]->min(l, std::min(lc, r))); // if(l <= lc && lc < r) res = std::min(res, min(lc, lc+1)); // if(lc + 1 < r) res = std::min(res, ch[1]->min(max(0, l-lc-1), r-lc-1)); // return res; } }; template <class T> Node<T> *Node<T>::nil = new Node<T>(0, 0); template <class T> struct Treap { Node<T> *root; int size; Treap() : root(Node<T>::nil), size(0) { root->nil->cnt = 0; root->nil->min_ = inf; } // ~Treap(){ // delete root; // } void shift(int l, int r) { T tmp = root->at(r - 1)->val; root = root->erase(r - 1); root = root->insert(l, tmp); } void set(int k, T x) { root->set(k, x); } T min(int l, int r) { return root->min(l, r); } void push_back(T x) { if (size == 0) root = new Node<T>(x, xor128()), size++; else root = root->insert(size++, x); } }; int main() { cin.tie(0); ios::sync_with_stdio(0); int n, q; cin >> n >> q; Treap<int> t; rep(i, n) { int x; cin >> x; t.push_back(x); } rep(i, q) { int op; cin >> op; if (false) { } else if (op == 0) { int l, r; cin >> l >> r; t.shift(l, r + 1); } else if (op == 1) { int l, r; cin >> l >> r; cout << t.min(l, r + 1) << '\n'; } else if (op == 2) { int p, v; cin >> p >> v; t.set(p, v); } } }
#include <algorithm> #include <cassert> #include <cstdio> #include <cstdlib> #include <ctime> #include <iostream> #include <tuple> #include <utility> #include <vector> #define loop(i, a, b) for (int i = (a); i < int(b); i++) #define rep(i, n) loop(i, 0, n) using namespace std; int const inf = 1e9; unsigned int xor128() { static unsigned int x = 123456789, y = 362436069, z = 521288629, w = time(0); unsigned long t = x ^ (x << 11); x = y; y = z; z = w; return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); } template <class T> struct Node { T val; Node *ch[2]; int pri, cnt; T min_; static Node *nil; Node(T v, int p) : val(v), pri(p), cnt(1), min_(v) { ch[0] = ch[1] = nil; } void *operator new(size_t) { static int pos = 0; static char mem[600020 * sizeof(Node)]; Node *res = (Node *)(mem + pos); pos += sizeof(Node); return res; } int size() { return this->cnt; } T min() { return this->min_; } T sum() { return this->sum_; } void show(int ofs = 0) { if (ch[0] != nil) ch[0]->show(ofs + 1); rep(i, ofs * 2) putchar(' '); printf("v:%d m:%d (%p)\n", val, min_, this); if (ch[1] != nil) ch[1]->show(ofs + 1); } void update() { cnt = ch[0]->size() + ch[1]->size() + 1; min_ = std::min({ch[0]->min(), ch[1]->min(), val}); } Node *merge(Node *r) { if (this == nil || r == nil) return this == nil ? r : this; if (pri > r->pri) { ch[1] = ch[1]->merge(r); update(); return this; } else { r->ch[0] = merge(r->ch[0]); r->update(); return r; } } pair<Node *, Node *> split(int k) { int lc = ch[0]->size(); if (k <= lc) { auto s = ch[0] != nil ? ch[0]->split(k) : make_pair(nil, nil); ch[0] = s.second; update(); return make_pair(s.first, this); } else { auto s = ch[1] != nil ? ch[1]->split(k - lc - 1) : make_pair(nil, nil); ch[1] = s.first; update(); return make_pair(this, s.second); } } Node *erase(int k) { Node *l, *m, *r; tie(l, m) = split(k); tie(m, r) = m->split(1); // delete m; return l->merge(r); } Node *at(int k) { int lc = ch[0]->size(); if (k < lc) return ch[0]->at(k); else if (k == lc) return this; else return ch[1]->at(k - lc - 1); } void set(int k, T x) { int lc = ch[0]->size(); if (k < lc) ch[0]->set(k, x); else if (k == lc) val = x; else if (k > lc) ch[1]->set(k - lc - 1, x); update(); } Node *insert(int k, T v) { Node *l, *m = new Node(v, xor128()), *r; tie(l, r) = split(k); return l->merge(m)->merge(r); } T min(int l, int r) { if (r - l <= 0) return inf; if (r - l == this->size()) return this->min(); int lc = ch[0]->size(); T res = inf; if (l <= lc && lc < r) res = std::min(res, this->val); res = std::min(res, ch[0]->min(l, std::min(r, lc))); res = std::min(res, ch[1]->min(std::max(0, l - lc - 1), r - lc - 1)); return res; // if(r - l <= 0) return inf; // if(r - l == this->size()) return this->min(); // int lc = ch[0]->size(); // if(l == lc && r - lc == 1) return val; // T res = inf; // if(l < lc) res = std::min(res, ch[0]->min(l, std::min(lc, r))); // if(l <= lc && lc < r) res = std::min(res, min(lc, lc+1)); // if(lc + 1 < r) res = std::min(res, ch[1]->min(max(0, l-lc-1), r-lc-1)); // return res; } }; template <class T> Node<T> *Node<T>::nil = new Node<T>(0, 0); template <class T> struct Treap { Node<T> *root; int size; Treap() : root(Node<T>::nil), size(0) { root->nil->cnt = 0; root->nil->min_ = inf; } // ~Treap(){ // delete root; // } void shift(int l, int r) { T tmp = root->at(r - 1)->val; root = root->erase(r - 1); root = root->insert(l, tmp); } void set(int k, T x) { root->set(k, x); } T min(int l, int r) { return root->min(l, r); } void push_back(T x) { if (size == 0) root = new Node<T>(x, xor128()), size++; else root = root->insert(size++, x); } }; int main() { cin.tie(0); ios::sync_with_stdio(0); int n, q; cin >> n >> q; Treap<int> t; rep(i, n) { int x; cin >> x; t.push_back(x); } rep(i, q) { int op; cin >> op; if (false) { } else if (op == 0) { int l, r; cin >> l >> r; t.shift(l, r + 1); } else if (op == 1) { int l, r; cin >> l >> r; cout << t.min(l, r + 1) << '\n'; } else if (op == 2) { int p, v; cin >> p >> v; t.set(p, v); } } }
insert
115
115
115
117
TLE
p00998
C++
Time Limit Exceeded
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cctype> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <forward_list> #include <functional> #include <initializer_list> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <memory> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using ll = long long; #define rep(j, k) for (int j = 0; j < (int)(k); j++) struct Random { uint32_t seed; Random(uint32_t s = 0) { seed = (uint32_t)time(0) + 1145141919; } uint32_t next() { static uint32_t x = 123456789, y = 362436069, z = 521288629, w = seed; uint32_t t = x ^ (x << 11); x = y; y = z; z = w; w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); return w; } int32_t nextInt(int32_t l, int32_t r) { return l + next() % (r - l + 1); } int32_t nextInt(int r) { return next() % r; } template <int mask> int32_t nextInt() { return next() & mask; } double nextDouble() { return next() * 0.00000000023283064365386962890625; } double nextDouble(double r) { return next() * 0.00000000023283064365386962890625 * r; } }; #define rand @ Random rng; namespace RBST { using Key = int; struct Node { Key key; Node *left, *right; int size; Key min; }; const Key INF = 1e9; using Np = Node *; using pair = std::pair<Np, Np>; const Np NIL = new Node{Key(), nullptr, nullptr, 0, INF}; int size(Np n) { return n->size; } Key min(Np n) { return n->min; } void update(Np n) { n->size = 1 + size(n->left) + size(n->right); n->min = std::min({min(n->left), n->key, min(n->right)}); } void assign(Np n, int k, Key x) { if (k < size(n->left)) assign(n->left, k, x); else if (size(n->left) == k) n->key = x; else assign(n->right, k - size(n->left) - 1, x); update(n); } Key min(Np n, int l, int r) { l = std::max(l, 0); r = std::min(r, size(n)); if (l >= r) return INF; if (l == 0 && r == size(n)) return min(n); Key res = INF; int sl = size(n->left); res = std::min(res, min(n->left, l, r)); res = std::min(res, min(n->right, l - sl - 1, r - sl - 1)); if (l <= sl && sl < r) res = std::min(res, n->key); return res; } Np merge(Np l, Np r) { if (l == NIL) return r; if (r == NIL) return l; int sl = size(l), sr = size(r); if (rng.next() % (sl + sr) >= sl) { l->right = merge(l->right, r); update(l); return l; } else { r->left = merge(l, r->left); update(r); return r; } } pair split(Np n, int k) { if (n == NIL) return {NIL, NIL}; if (k <= size(n->left)) { pair p = split(n->left, k); n->left = p.second; update(n); return {p.first, n}; } else { pair p = split(n->right, k - size(n->left) - 1); n->right = p.first; update(n); return {n, p.second}; } } Np shift(Np n, int l, int r) { Np a, b, c, d; std::tie(a, b) = split(n, l); std::tie(b, c) = split(b, r - l); std::tie(c, d) = split(c, 1); return merge(merge(a, c), merge(b, d)); } Np append(Np n, const Key &k) { static Node pool[200010]; static int p = 0; Np a = pool + p++; *a = {k, NIL, NIL, 1, k}; return merge(n, a); } }; // namespace RBST using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(0); int n, q; cin >> n >> q; RBST::Node *root = RBST::NIL; for (int i = 0; i < n; i++) { int a; cin >> a; root = RBST::append(root, a); } // Treap::show(root); for (int i = 0; i < q; i++) { int x, y, z; cin >> x >> y >> z; if (x == 0) { root = RBST::shift(root, y, z); } if (x == 1) { // puts("====="); // Treap::show(root); cout << RBST::min(root, y, z + 1) << '\n'; } if (x == 2) { RBST::assign(root, y, z); } } }
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cctype> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <forward_list> #include <functional> #include <initializer_list> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <memory> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using ll = long long; #define rep(j, k) for (int j = 0; j < (int)(k); j++) struct Random { uint32_t seed; Random(uint32_t s = 0) { seed = (uint32_t)time(0) + 1145141919; } uint32_t next() { static uint32_t x = 123456789, y = 362436069, z = 521288629, w = seed; uint32_t t = x ^ (x << 11); x = y; y = z; z = w; w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); return w; } int32_t nextInt(int32_t l, int32_t r) { return l + next() % (r - l + 1); } int32_t nextInt(int r) { return next() % r; } template <int mask> int32_t nextInt() { return next() & mask; } double nextDouble() { return next() * 0.00000000023283064365386962890625; } double nextDouble(double r) { return next() * 0.00000000023283064365386962890625 * r; } }; #define rand @ Random rng; namespace RBST { using Key = int; struct Node { Key key; Node *left, *right; int size; Key min; }; const Key INF = 1e9; using Np = Node *; using pair = std::pair<Np, Np>; const Np NIL = new Node{Key(), nullptr, nullptr, 0, INF}; int size(Np n) { return n->size; } Key min(Np n) { return n->min; } void update(Np n) { n->size = 1 + size(n->left) + size(n->right); n->min = std::min({min(n->left), n->key, min(n->right)}); } void assign(Np n, int k, Key x) { if (k < size(n->left)) assign(n->left, k, x); else if (size(n->left) == k) n->key = x; else assign(n->right, k - size(n->left) - 1, x); update(n); } Key min(Np n, int l, int r) { l = std::max(l, 0); r = std::min(r, size(n)); if (l >= r) return INF; if (l == 0 && r == size(n)) return min(n); Key res = INF; int sl = size(n->left); res = std::min(res, min(n->left, l, r)); res = std::min(res, min(n->right, l - sl - 1, r - sl - 1)); if (l <= sl && sl < r) res = std::min(res, n->key); return res; } Np merge(Np l, Np r) { if (l == NIL) return r; if (r == NIL) return l; int sl = size(l), sr = size(r); if (rng.next() % (sl + sr) < sl) { l->right = merge(l->right, r); update(l); return l; } else { r->left = merge(l, r->left); update(r); return r; } } pair split(Np n, int k) { if (n == NIL) return {NIL, NIL}; if (k <= size(n->left)) { pair p = split(n->left, k); n->left = p.second; update(n); return {p.first, n}; } else { pair p = split(n->right, k - size(n->left) - 1); n->right = p.first; update(n); return {n, p.second}; } } Np shift(Np n, int l, int r) { Np a, b, c, d; std::tie(a, b) = split(n, l); std::tie(b, c) = split(b, r - l); std::tie(c, d) = split(c, 1); return merge(merge(a, c), merge(b, d)); } Np append(Np n, const Key &k) { static Node pool[200010]; static int p = 0; Np a = pool + p++; *a = {k, NIL, NIL, 1, k}; return merge(n, a); } }; // namespace RBST using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(0); int n, q; cin >> n >> q; RBST::Node *root = RBST::NIL; for (int i = 0; i < n; i++) { int a; cin >> a; root = RBST::append(root, a); } // Treap::show(root); for (int i = 0; i < q; i++) { int x, y, z; cin >> x >> y >> z; if (x == 0) { root = RBST::shift(root, y, z); } if (x == 1) { // puts("====="); // Treap::show(root); cout << RBST::min(root, y, z + 1) << '\n'; } if (x == 2) { RBST::assign(root, y, z); } } }
replace
118
119
118
119
TLE
p00998
C++
Time Limit Exceeded
#include <algorithm> #include <cassert> #include <cctype> #include <ctime> #include <iostream> #include <queue> #include <string> #include <tuple> #include <vector> #define dump(x) std::cerr << __LINE__ << ":\t" #x " = " << x << std::endl using Key = int; const Key INF = 1e9; const int MAX_N = 1000010; // ?????\?????????° + EPS struct node { Key key; int priority; node *left, *right; int size; Key min; static node *const NIL; node() {} node(const Key &x) : node(x, xor128(), NIL, NIL, 1, x) {} node(const Key &key_, int priority_, node *left_, node *right_, int size_, const Key &min_) : key(key_), priority(priority_), left(left_), right(right_), size(size_), min(min_) {} void *operator new(std::size_t) { static node pool[MAX_N]; static int p = 0; return pool + p++; } static uint32_t xor128() { static uint32_t x = 123456789, y = 362436069, z = 521288629, w = time(0); uint32_t t = x ^ (x << 11); x = y; y = z; z = w; w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); return w % 100; } }; using np = node *; using cnp = const node *; using npair = std::pair<np, np>; node *const node::NIL = new node(Key(), -1, nullptr, nullptr, 0, INF); bool is_leaf(cnp n) { return n->left == node::NIL; } class treap { protected: np root; public: treap() : root(node::NIL) {} treap(np root_) : root(root_) {} // AOJ 1508 http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2099535#1 treap(Key *left, Key *right) : root(__make_tree(left, right)) { int n = right - left; std::vector<int> ps(n - 1); std::generate(ps.begin(), ps.end(), node::xor128); std::sort(ps.begin(), ps.end()); std::queue<np> que; que.push(root); while (que.size()) { np n = que.front(); que.pop(); if (is_leaf(n)) continue; n->priority = ps.back(); ps.pop_back(); que.push(n->left); que.push(n->right); } } protected: // AOJ 1508 http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2099535#1 np __make_tree(Key *left, Key *right) { int sz = right - left; Key *mid = left + sz / 2; if (sz == 0) { return node::NIL; } else if (sz == 1) { return new node(*mid, -1, node::NIL, node::NIL, 1, *mid); } else { np lc = __make_tree(left, mid); np rc = __make_tree(mid, right); return new node(-1, -1, lc, rc, sz, std::min(lc->min, rc->min)); } } public: int size() const { return root->size; } public: // AOJ 1508 http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2083288 void set(int k, const Key &x) { __set(root, k, x); } protected: // AOJ 1508 http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2083288 void __set(np n, int k, const Key &x) { if (n->size == 1) { n->key = x; } else if (k < n->left->size) __set(n->left, k, x); else __set(n->right, k - n->left->size, x); __update(n); } public: // AOJ 1508 http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2083288 Key get(int k) const { return __get(root, k); } protected: // AOJ 1508 http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2083288 Key __get(np n, int k) const { if (n->size == 1) { return n->key; } else if (k < n->left->size) return __get(n->left, k); else return __get(n->right, k - n->left->size); } public: // AOJ 1508 http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2083288 Key range_min(int l, int r) { return __range_min(root, l, r); } protected: // AOJ 1508 http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2083288 Key __range_min(cnp n, int l, int r) const { l = std::max(l, 0); r = std::min(r, n->size); // std::cout << n << ' ' << l << ' ' << r << std::endl; if (l >= r) return INF; if (l == 0 && r == n->size) return n->min; Key res = INF; int sl = n->left->size; res = std::min(res, __range_min(n->left, l, r)); res = std::min(res, __range_min(n->right, l - sl, r - sl)); return res; } public: void merge_from_left(treap &t) { root = __merge(t.root, root); } void merge_from_right(treap &t) { root = __merge(root, t.root); } protected: // yukicoder 449 http://yukicoder.me/submissions/131855 np __merge(np l, np r, int pri) const { if (is_leaf(l) && is_leaf(r)) return new node(-1, pri, l, r, 2, std::min(l->key, r->key)); if (is_leaf(l)) { r->left = __merge(l, r->left, pri); return __update(r); } if (is_leaf(r)) { l->right = __update(__merge(l->right, r, pri)); return __update(l); } if (pri > l->priority && pri > r->priority) { return new node(-1, pri, l, r, l->size + r->size, std::min(l->min, r->min)); } if (l->priority > r->priority) { l->right = __merge(l->right, r, pri); return __update(l); } { r->left = __merge(l, r->left, pri); return __update(r); } } np __merge(np l, np r) const { if (l == node::NIL) return r; if (r == node::NIL) return l; int pri = node::xor128(); return __merge(l, r, pri); } public: // yukicoder 449 http://yukicoder.me/submissions/131855 std::pair<treap, treap> split_at(int k) { np l, r; std::tie(l, r) = __split_at(root, k); return std::make_pair(treap(l), treap(r)); } protected: // yukicoder 449 http://yukicoder.me/submissions/131855 npair __split_at(np n, int k) const { if (n == node::NIL) return npair(node::NIL, node::NIL); else return __split_at_sub(n, k); } npair __split_at_sub(np n, int k) const { if (is_leaf(n)) return k == 0 ? npair(node::NIL, n) : npair(n, node::NIL); if (k == n->left->size) return npair(n->left, n->right); if (k < n->left->size) { npair p = __split_at_sub(n->left, k); n->left = p.second; return npair(p.first, __update(n)); } else { npair p = __split_at_sub(n->right, k - n->left->size); n->right = p.first; return npair(__update(n), p.second); } } protected: np __update(np n) const { n->size = n->left->size + n->right->size + (n->left == node::NIL); n->min = std::min( {n->left->min, n->right->min, (n->left == node::NIL ? n->key : INF)}); return n; } public: std::string to_string() const { std::string res; __to_string(root, res); return res; } protected: void __to_string(cnp n, std::string &res) const { res += "("; if (n != node::NIL) { __to_string(n->left, res); res += ")" + std::to_string(n->key) + "("; __to_string(n->right, res); } res += ")"; } public: void show() { return; puts("=== show ==="); if (root == node::NIL) { std::cout << "nil" << std::endl; } else { __show(root, 0); } } void show_array() { return; puts("=== show array ==="); if (root == node::NIL) { std::cout << "nil" << std::endl; } else { __show_array(root); std::cout << '\n'; } } void __show(np n, int ofs) { using std::cout; using std::endl; using std::string; assert((n->left == node::NIL && n->right == node::NIL) || (n->left != node::NIL && n->right != node::NIL)); if (is_leaf(n)) { // leaf printf("%s-- (%d %d %d)\n", string(ofs * 8, ' ').c_str(), n->priority, n->min, n->key); } else { __show(n->left, ofs + 1); printf("%s(%d %d %d) < \n", string(ofs * 8, ' ').c_str(), n->priority, n->min, n->key); __show(n->right, ofs + 1); } } void __show_array(np n) { using std::cout; using std::endl; using std::string; assert((n->left == node::NIL && n->right == node::NIL) || (n->left != node::NIL && n->right != node::NIL)); if (is_leaf(n)) { // leaf printf("%d ", n->key); } else { __show_array(n->left); __show_array(n->right); } } public: void shift(int l, int r) { // std::cout << "shift " << l << ' ' << r << std::endl; np a, b, c, d; std::tie(a, b) = __split_at(root, l); std::tie(b, c) = __split_at(b, r - l); // puts("cd"); // treap(c).show_array(); std::tie(c, d) = __split_at(c, 1); // puts("a"); // treap(a).show_array(); // puts("b"); // treap(b).show_array(); // puts("c"); // treap(c).show_array(); // puts("d"); // treap(d).show_array(); np ac = __merge(a, c); np bd = __merge(b, d); root = __merge(ac, bd); } }; using namespace std; using ll = long long; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(c) begin(c), end(c) using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(0); int n, q; cin >> n >> q; static int a[200010]; for (int i = 0; i < n; i++) cin >> a[i]; treap t(a, a + n); t.show(); t.show_array(); for (int i = 0; i < q; i++) { int x, y, z; cin >> x >> y >> z; // cout << "== query " << x << ' ' << y << ' ' << z << " ==" << endl; if (x == 0) { t.shift(y, z); } if (x == 1) { // puts("====="); // Treap::show(root); cout << t.range_min(y, z + 1) << '\n'; } if (x == 2) { t.set(y, z); } t.show(); t.show_array(); } }
#include <algorithm> #include <cassert> #include <cctype> #include <ctime> #include <iostream> #include <queue> #include <string> #include <tuple> #include <vector> #define dump(x) std::cerr << __LINE__ << ":\t" #x " = " << x << std::endl using Key = int; const Key INF = 1e9; const int MAX_N = 1000010; // ?????\?????????° + EPS struct node { Key key; int priority; node *left, *right; int size; Key min; static node *const NIL; node() {} node(const Key &x) : node(x, xor128(), NIL, NIL, 1, x) {} node(const Key &key_, int priority_, node *left_, node *right_, int size_, const Key &min_) : key(key_), priority(priority_), left(left_), right(right_), size(size_), min(min_) {} void *operator new(std::size_t) { static node pool[MAX_N]; static int p = 0; return pool + p++; } static uint32_t xor128() { static uint32_t x = 123456789, y = 362436069, z = 521288629, w = time(0); uint32_t t = x ^ (x << 11); x = y; y = z; z = w; w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); return w % 100; } }; using np = node *; using cnp = const node *; using npair = std::pair<np, np>; node *const node::NIL = new node(Key(), -1, nullptr, nullptr, 0, INF); bool is_leaf(cnp n) { return n->left == node::NIL; } class treap { protected: np root; public: treap() : root(node::NIL) {} treap(np root_) : root(root_) {} // AOJ 1508 http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2099535#1 treap(Key *left, Key *right) : root(__make_tree(left, right)) { int n = right - left; std::vector<int> ps(n - 1); std::generate(ps.begin(), ps.end(), node::xor128); std::sort(ps.begin(), ps.end()); std::queue<np> que; que.push(root); while (que.size()) { np n = que.front(); que.pop(); if (is_leaf(n)) continue; n->priority = ps.back(); ps.pop_back(); que.push(n->left); que.push(n->right); } } protected: // AOJ 1508 http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2099535#1 np __make_tree(Key *left, Key *right) { int sz = right - left; Key *mid = left + sz / 2; if (sz == 0) { return node::NIL; } else if (sz == 1) { return new node(*mid, -1, node::NIL, node::NIL, 1, *mid); } else { np lc = __make_tree(left, mid); np rc = __make_tree(mid, right); return new node(-1, -1, lc, rc, sz, std::min(lc->min, rc->min)); } } public: int size() const { return root->size; } public: // AOJ 1508 http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2083288 void set(int k, const Key &x) { __set(root, k, x); } protected: // AOJ 1508 http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2083288 void __set(np n, int k, const Key &x) { if (n->size == 1) { n->key = x; } else if (k < n->left->size) __set(n->left, k, x); else __set(n->right, k - n->left->size, x); __update(n); } public: // AOJ 1508 http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2083288 Key get(int k) const { return __get(root, k); } protected: // AOJ 1508 http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2083288 Key __get(np n, int k) const { if (n->size == 1) { return n->key; } else if (k < n->left->size) return __get(n->left, k); else return __get(n->right, k - n->left->size); } public: // AOJ 1508 http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2083288 Key range_min(int l, int r) { return __range_min(root, l, r); } protected: // AOJ 1508 http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2083288 Key __range_min(cnp n, int l, int r) const { l = std::max(l, 0); r = std::min(r, n->size); // std::cout << n << ' ' << l << ' ' << r << std::endl; if (l >= r) return INF; if (l == 0 && r == n->size) return n->min; Key res = INF; int sl = n->left->size; res = std::min(res, __range_min(n->left, l, r)); res = std::min(res, __range_min(n->right, l - sl, r - sl)); return res; } public: void merge_from_left(treap &t) { root = __merge(t.root, root); } void merge_from_right(treap &t) { root = __merge(root, t.root); } protected: // yukicoder 449 http://yukicoder.me/submissions/131855 np __merge(np l, np r, int pri) const { if (is_leaf(l) && is_leaf(r)) return new node(-1, pri, l, r, 2, std::min(l->key, r->key)); if (is_leaf(l)) { r->left = __merge(l, r->left, pri); return __update(r); } if (is_leaf(r)) { l->right = __update(__merge(l->right, r, pri)); return __update(l); } if (pri > l->priority && pri > r->priority) { return new node(-1, pri, l, r, l->size + r->size, std::min(l->min, r->min)); } if (pri < l->priority && pri < r->priority) { if (node::xor128() & 1) { l->right = __merge(l->right, r, pri); return __update(l); } else { r->left = __merge(l, r->left, pri); return __update(r); } } if (l->priority > r->priority) { l->right = __merge(l->right, r, pri); return __update(l); } { r->left = __merge(l, r->left, pri); return __update(r); } } np __merge(np l, np r) const { if (l == node::NIL) return r; if (r == node::NIL) return l; int pri = node::xor128(); return __merge(l, r, pri); } public: // yukicoder 449 http://yukicoder.me/submissions/131855 std::pair<treap, treap> split_at(int k) { np l, r; std::tie(l, r) = __split_at(root, k); return std::make_pair(treap(l), treap(r)); } protected: // yukicoder 449 http://yukicoder.me/submissions/131855 npair __split_at(np n, int k) const { if (n == node::NIL) return npair(node::NIL, node::NIL); else return __split_at_sub(n, k); } npair __split_at_sub(np n, int k) const { if (is_leaf(n)) return k == 0 ? npair(node::NIL, n) : npair(n, node::NIL); if (k == n->left->size) return npair(n->left, n->right); if (k < n->left->size) { npair p = __split_at_sub(n->left, k); n->left = p.second; return npair(p.first, __update(n)); } else { npair p = __split_at_sub(n->right, k - n->left->size); n->right = p.first; return npair(__update(n), p.second); } } protected: np __update(np n) const { n->size = n->left->size + n->right->size + (n->left == node::NIL); n->min = std::min( {n->left->min, n->right->min, (n->left == node::NIL ? n->key : INF)}); return n; } public: std::string to_string() const { std::string res; __to_string(root, res); return res; } protected: void __to_string(cnp n, std::string &res) const { res += "("; if (n != node::NIL) { __to_string(n->left, res); res += ")" + std::to_string(n->key) + "("; __to_string(n->right, res); } res += ")"; } public: void show() { return; puts("=== show ==="); if (root == node::NIL) { std::cout << "nil" << std::endl; } else { __show(root, 0); } } void show_array() { return; puts("=== show array ==="); if (root == node::NIL) { std::cout << "nil" << std::endl; } else { __show_array(root); std::cout << '\n'; } } void __show(np n, int ofs) { using std::cout; using std::endl; using std::string; assert((n->left == node::NIL && n->right == node::NIL) || (n->left != node::NIL && n->right != node::NIL)); if (is_leaf(n)) { // leaf printf("%s-- (%d %d %d)\n", string(ofs * 8, ' ').c_str(), n->priority, n->min, n->key); } else { __show(n->left, ofs + 1); printf("%s(%d %d %d) < \n", string(ofs * 8, ' ').c_str(), n->priority, n->min, n->key); __show(n->right, ofs + 1); } } void __show_array(np n) { using std::cout; using std::endl; using std::string; assert((n->left == node::NIL && n->right == node::NIL) || (n->left != node::NIL && n->right != node::NIL)); if (is_leaf(n)) { // leaf printf("%d ", n->key); } else { __show_array(n->left); __show_array(n->right); } } public: void shift(int l, int r) { // std::cout << "shift " << l << ' ' << r << std::endl; np a, b, c, d; std::tie(a, b) = __split_at(root, l); std::tie(b, c) = __split_at(b, r - l); // puts("cd"); // treap(c).show_array(); std::tie(c, d) = __split_at(c, 1); // puts("a"); // treap(a).show_array(); // puts("b"); // treap(b).show_array(); // puts("c"); // treap(c).show_array(); // puts("d"); // treap(d).show_array(); np ac = __merge(a, c); np bd = __merge(b, d); root = __merge(ac, bd); } }; using namespace std; using ll = long long; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(c) begin(c), end(c) using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(0); int n, q; cin >> n >> q; static int a[200010]; for (int i = 0; i < n; i++) cin >> a[i]; treap t(a, a + n); t.show(); t.show_array(); for (int i = 0; i < q; i++) { int x, y, z; cin >> x >> y >> z; // cout << "== query " << x << ' ' << y << ' ' << z << " ==" << endl; if (x == 0) { t.shift(y, z); } if (x == 1) { // puts("====="); // Treap::show(root); cout << t.range_min(y, z + 1) << '\n'; } if (x == 2) { t.set(y, z); } t.show(); t.show_array(); } }
insert
180
180
180
189
TLE
p00998
C++
Runtime Error
#include <algorithm> #include <cassert> #include <ctime> #include <iostream> #include <string> #include <tuple> #include <vector> #define dump(x) std::cerr << __LINE__ << ":\t" #x " = " << x << std::endl using key_t = int; const key_t INF = 1000000000; const int MAX_N = 1000010; // ?????\???????????° + EPS enum { L, R }; uint32_t xor128() { static uint32_t x = 123456789, y = 362436069, z = 521288629, w = time(0); uint32_t t = x ^ (x << 11); x = y; y = z; z = w; w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); return w; } struct node { key_t key; node *ch[2]; int size; key_t min; static node *const nil; node() {} node(const key_t &x) : key(x), size(1), min(x) {} node(node *left_, node *right_, int size_, const key_t &min_) : size(size_), min(min_) { ch[L] = left_; ch[R] = right_; } void *operator new(std::size_t) { static node pool[MAX_N]; static int p = 0; return pool + p++; } bool is_leaf() const { return size == 1; } }; using np = node *; using cnp = const node *; using npair = std::pair<np, np>; node *const node::nil = new node(nullptr, nullptr, 0, INF); class rbst { public: np root; rbst() : root(node::nil) {} rbst(np root_) : root(root_) {} rbst(key_t *left, key_t *right) : root(make_tree_rec(left, right)) {} np make_tree_rec(key_t *left, key_t *right) { int sz = right - left; key_t *mid = left + sz / 2; if (sz == 0) { return node::nil; } else if (sz == 1) { return new node(*mid); } else { np lc = make_tree_rec(left, mid); np rc = make_tree_rec(mid, right); return new node(lc, rc, sz, std::min(lc->min, rc->min)); } } int size() const { return root->size; } void set(int k, const key_t &x) { set(root, k, x); } void set(np n, int k, const key_t &x) { if (n->is_leaf()) { n->key = n->min = x; } else { if (k < n->ch[L]->size) set(n->ch[L], k, x); else set(n->ch[R], k - n->ch[L]->size, x); update(n); } } key_t get(np n, int k) const { if (n->size == 1) return n->key; else if (k < n->ch[L]->size) return get(n->ch[L], k); else return get(n->ch[R], k - n->ch[L]->size); } key_t range_min(int l, int r) { return range_min(root, l, r); } key_t range_min(cnp n, int l, int r) const { l = std::max(l, 0); r = std::min(r, n->size); if (l >= r) return INF; if (l == 0 && r == n->size) return n->min; key_t res = INF; int sl = n->ch[L]->size; res = std::min(res, range_min(n->ch[L], l, r)); res = std::min(res, range_min(n->ch[R], l - sl, r - sl)); return res; } np merge(np l, np r) const { if (l == node::nil) return r; if (r == node::nil) return l; return merge_sub(l, r); } np merge_sub(np l, np r) const { int sl = l->size, sr = r->size; int rand = xor128() % (sl + sr + 1); if (rand < sl) { if (l->is_leaf()) return new node(l, r, l->size + r->size, std::min(l->min, r->min)); l->ch[R] = merge_sub(l->ch[R], r); return update(l); } else if (rand == sl) { return new node(l, r, l->size + r->size, std::min(l->min, r->min)); } else { if (r->is_leaf()) return new node(l, r, l->size + r->size, std::min(l->min, r->min)); r->ch[L] = merge_sub(l, r->ch[L]); return update(r); } } std::pair<rbst, rbst> split(int k) { np l, r; std::tie(l, r) = split(root, k); return std::make_pair(rbst(l), rbst(r)); } npair split(np n, int k) const { if (n == node::nil) return npair(node::nil, node::nil); else return split_sub(n, k); } npair split_sub(np n, int k) const { if (n->is_leaf()) return k == 0 ? npair(node::nil, n) : npair(n, node::nil); if (k < n->ch[L]->size) { npair p = split_sub(n->ch[L], k); n->ch[L] = p.second; return npair(p.first, update(n)); } if (k == n->ch[L]->size) return npair(n->ch[L], n->ch[R]); npair p = split_sub(n->ch[R], k - n->ch[L]->size); n->ch[R] = p.first; return npair(update(n), p.second); } np insert(np n, int k, const key_t &x) { return n == node::nil ? new node(x) : insert_sub(n, k, x); } np insert_sub(np n, int k, const key_t &x) { if (n->is_leaf()) { return update(k == 0 ? new node(new node(x), n, 2, INF) : new node(n, new node(x), 2, INF)); } else { if (k < n->ch[L]->size) n->ch[L] = insert_sub(n->ch[L], k, x); else n->ch[R] = insert_sub(n->ch[R], k - n->ch[L]->size, x); n = update(n); int sl = n->ch[L]->size, sr = n->ch[R]->size; int rand = xor128() % (sl + sr + 1); return (rand < sl && sl != 1) ? rotate<R>(n) : (rand > sl && sr != 1) ? rotate<L>(n) : n; } } np erase(np n, int k) { return n->is_leaf() ? node::nil : erase_sub(n, k); } np erase_sub(np n, int k) { if (k == 0 && n->ch[L]->is_leaf()) { return n->ch[R]; } else if (k == n->size - 1 && n->ch[R]->is_leaf()) { return n->ch[L]; } else { if (k < n->ch[L]->size) n->ch[L] = erase_sub(n->ch[L], k); else n->ch[R] = erase_sub(n->ch[R], k - n->ch[L]->size); n = update(n); int sl = n->ch[L]->size, sr = n->ch[R]->size; int rand = xor128() % (sl + sr + 1); return (rand < sl && sl != 1) ? rotate<R>(n) : (rand > sl && sr != 1) ? rotate<L>(n) : n; } } template <int dir> np rotate(np n) { np par = n->ch[1 - dir]; n->ch[1 - dir] = par->ch[dir]; par->ch[dir] = update(n); return update(par); } np update(np n) const { assert(!n->is_leaf()); n->size = n->ch[L]->size + n->ch[R]->size; n->min = std::min(n->ch[L]->min, n->ch[R]->min); return n; } void shift(int l, int r) { np a, b, c, d; std::tie(a, b) = split(root, l); std::tie(b, c) = split(b, r - l); std::tie(c, d) = split(c, 1); np ac = merge(a, c); np bd = merge(b, d); root = merge(ac, bd); } void shift2(int l, int r) { key_t x = get(root, r); root = erase(root, r); root = insert(root, l, x); } void show_array() { show_array(root); std::cout << '\n'; } void show_array(cnp n) const { if (n->is_leaf()) { std::cout << n->key << ' '; std::cout << std::flush; } else { show_array(n->ch[L]); show_array(n->ch[R]); } } }; using namespace std; using ll = long long; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(c) begin(c), end(c) int main() { cin.tie(0); ios::sync_with_stdio(0); int n, q; cin >> n >> q; static int a[200010]; for (int i = 0; i < n; i++) cin >> a[i]; rbst t(a, a + n); rbst u(a, a + n); for (int i = 0; i < q; i++) { int x, y, z; cin >> x >> y >> z; if (x == 0) { // dump(y); // dump(z); // t.show_array(); t.shift(y, z); u.shift2(y, z); // t.show_array(); } if (x == 1) { int a = t.range_min(y, z + 1); int b = u.range_min(y, z + 1); if (a != b) throw 0; cout << a << '\n'; } if (x == 2) { t.set(y, z); u.set(y, z); } } }
#include <algorithm> #include <cassert> #include <ctime> #include <iostream> #include <string> #include <tuple> #include <vector> #define dump(x) std::cerr << __LINE__ << ":\t" #x " = " << x << std::endl using key_t = int; const key_t INF = 1000000000; const int MAX_N = 5000010; // ?????\???????????° + EPS enum { L, R }; uint32_t xor128() { static uint32_t x = 123456789, y = 362436069, z = 521288629, w = time(0); uint32_t t = x ^ (x << 11); x = y; y = z; z = w; w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); return w; } struct node { key_t key; node *ch[2]; int size; key_t min; static node *const nil; node() {} node(const key_t &x) : key(x), size(1), min(x) {} node(node *left_, node *right_, int size_, const key_t &min_) : size(size_), min(min_) { ch[L] = left_; ch[R] = right_; } void *operator new(std::size_t) { static node pool[MAX_N]; static int p = 0; return pool + p++; } bool is_leaf() const { return size == 1; } }; using np = node *; using cnp = const node *; using npair = std::pair<np, np>; node *const node::nil = new node(nullptr, nullptr, 0, INF); class rbst { public: np root; rbst() : root(node::nil) {} rbst(np root_) : root(root_) {} rbst(key_t *left, key_t *right) : root(make_tree_rec(left, right)) {} np make_tree_rec(key_t *left, key_t *right) { int sz = right - left; key_t *mid = left + sz / 2; if (sz == 0) { return node::nil; } else if (sz == 1) { return new node(*mid); } else { np lc = make_tree_rec(left, mid); np rc = make_tree_rec(mid, right); return new node(lc, rc, sz, std::min(lc->min, rc->min)); } } int size() const { return root->size; } void set(int k, const key_t &x) { set(root, k, x); } void set(np n, int k, const key_t &x) { if (n->is_leaf()) { n->key = n->min = x; } else { if (k < n->ch[L]->size) set(n->ch[L], k, x); else set(n->ch[R], k - n->ch[L]->size, x); update(n); } } key_t get(np n, int k) const { if (n->size == 1) return n->key; else if (k < n->ch[L]->size) return get(n->ch[L], k); else return get(n->ch[R], k - n->ch[L]->size); } key_t range_min(int l, int r) { return range_min(root, l, r); } key_t range_min(cnp n, int l, int r) const { l = std::max(l, 0); r = std::min(r, n->size); if (l >= r) return INF; if (l == 0 && r == n->size) return n->min; key_t res = INF; int sl = n->ch[L]->size; res = std::min(res, range_min(n->ch[L], l, r)); res = std::min(res, range_min(n->ch[R], l - sl, r - sl)); return res; } np merge(np l, np r) const { if (l == node::nil) return r; if (r == node::nil) return l; return merge_sub(l, r); } np merge_sub(np l, np r) const { int sl = l->size, sr = r->size; int rand = xor128() % (sl + sr + 1); if (rand < sl) { if (l->is_leaf()) return new node(l, r, l->size + r->size, std::min(l->min, r->min)); l->ch[R] = merge_sub(l->ch[R], r); return update(l); } else if (rand == sl) { return new node(l, r, l->size + r->size, std::min(l->min, r->min)); } else { if (r->is_leaf()) return new node(l, r, l->size + r->size, std::min(l->min, r->min)); r->ch[L] = merge_sub(l, r->ch[L]); return update(r); } } std::pair<rbst, rbst> split(int k) { np l, r; std::tie(l, r) = split(root, k); return std::make_pair(rbst(l), rbst(r)); } npair split(np n, int k) const { if (n == node::nil) return npair(node::nil, node::nil); else return split_sub(n, k); } npair split_sub(np n, int k) const { if (n->is_leaf()) return k == 0 ? npair(node::nil, n) : npair(n, node::nil); if (k < n->ch[L]->size) { npair p = split_sub(n->ch[L], k); n->ch[L] = p.second; return npair(p.first, update(n)); } if (k == n->ch[L]->size) return npair(n->ch[L], n->ch[R]); npair p = split_sub(n->ch[R], k - n->ch[L]->size); n->ch[R] = p.first; return npair(update(n), p.second); } np insert(np n, int k, const key_t &x) { return n == node::nil ? new node(x) : insert_sub(n, k, x); } np insert_sub(np n, int k, const key_t &x) { if (n->is_leaf()) { return update(k == 0 ? new node(new node(x), n, 2, INF) : new node(n, new node(x), 2, INF)); } else { if (k < n->ch[L]->size) n->ch[L] = insert_sub(n->ch[L], k, x); else n->ch[R] = insert_sub(n->ch[R], k - n->ch[L]->size, x); n = update(n); int sl = n->ch[L]->size, sr = n->ch[R]->size; int rand = xor128() % (sl + sr + 1); return (rand < sl && sl != 1) ? rotate<R>(n) : (rand > sl && sr != 1) ? rotate<L>(n) : n; } } np erase(np n, int k) { return n->is_leaf() ? node::nil : erase_sub(n, k); } np erase_sub(np n, int k) { if (k == 0 && n->ch[L]->is_leaf()) { return n->ch[R]; } else if (k == n->size - 1 && n->ch[R]->is_leaf()) { return n->ch[L]; } else { if (k < n->ch[L]->size) n->ch[L] = erase_sub(n->ch[L], k); else n->ch[R] = erase_sub(n->ch[R], k - n->ch[L]->size); n = update(n); int sl = n->ch[L]->size, sr = n->ch[R]->size; int rand = xor128() % (sl + sr + 1); return (rand < sl && sl != 1) ? rotate<R>(n) : (rand > sl && sr != 1) ? rotate<L>(n) : n; } } template <int dir> np rotate(np n) { np par = n->ch[1 - dir]; n->ch[1 - dir] = par->ch[dir]; par->ch[dir] = update(n); return update(par); } np update(np n) const { assert(!n->is_leaf()); n->size = n->ch[L]->size + n->ch[R]->size; n->min = std::min(n->ch[L]->min, n->ch[R]->min); return n; } void shift(int l, int r) { np a, b, c, d; std::tie(a, b) = split(root, l); std::tie(b, c) = split(b, r - l); std::tie(c, d) = split(c, 1); np ac = merge(a, c); np bd = merge(b, d); root = merge(ac, bd); } void shift2(int l, int r) { key_t x = get(root, r); root = erase(root, r); root = insert(root, l, x); } void show_array() { show_array(root); std::cout << '\n'; } void show_array(cnp n) const { if (n->is_leaf()) { std::cout << n->key << ' '; std::cout << std::flush; } else { show_array(n->ch[L]); show_array(n->ch[R]); } } }; using namespace std; using ll = long long; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(c) begin(c), end(c) int main() { cin.tie(0); ios::sync_with_stdio(0); int n, q; cin >> n >> q; static int a[200010]; for (int i = 0; i < n; i++) cin >> a[i]; rbst t(a, a + n); rbst u(a, a + n); for (int i = 0; i < q; i++) { int x, y, z; cin >> x >> y >> z; if (x == 0) { // dump(y); // dump(z); // t.show_array(); t.shift(y, z); u.shift2(y, z); // t.show_array(); } if (x == 1) { int a = t.range_min(y, z + 1); int b = u.range_min(y, z + 1); if (a != b) throw 0; cout << a << '\n'; } if (x == 2) { t.set(y, z); u.set(y, z); } } }
replace
10
11
10
11
0
p00998
C++
Runtime Error
#define NDEBUG 1 #include <bits/stdc++.h> using key_type = int; enum { L, R }; struct avl_node { key_type key; std::array<avl_node *, 2> ch; int size; int height; int min; static avl_node *const nil; avl_node() : avl_node(-1) {} avl_node(key_type key) : avl_node(key, nil, nil, 1, 1, key) {} avl_node(const key_type key, avl_node *left, avl_node *right, int size, int height, key_type min) : key(key), ch({{left, right}}), size(size), height(height), min(min) {} void *operator new(size_t) { static int p = 0; static avl_node pool[200010]; return pool + p++; } }; avl_node *const avl_node::nil = new avl_node(key_type(), nullptr, nullptr, 0, 0, 1e9); avl_node *const nil = avl_node::nil; namespace trush { auto ___ = []() { return nil->ch = {{nil, nil}}; }; }; using np = avl_node *; using cnp = const avl_node *; // ok np update(np n) { n->size = n->ch[L]->size + 1 + n->ch[R]->size; n->height = std::max(n->ch[L]->height, n->ch[R]->height) + 1; n->min = std::min({n->ch[L]->min, n->key, n->ch[R]->min}); return n; } // ok template <int dir> np rotate(np n) { assert(n->ch[!dir] != nil); np root = n->ch[!dir]; n->ch[!dir] = root->ch[dir]; root->ch[dir] = n; update(n); update(root); return root; } // ok int bfactor(np n) { assert(n != nil); return n->ch[R]->height - n->ch[L]->height; } // ok np balance(np n) { assert(abs(bfactor(n)) <= 2); if (bfactor(n) == +2) { if (bfactor(n->ch[R]) < 0) n->ch[R] = rotate<R>(n->ch[R]); return rotate<L>(n); } else if (bfactor(n) == -2) { if (bfactor(n->ch[L]) > 0) n->ch[L] = rotate<L>(n->ch[L]); return rotate<R>(n); } else { return n; } } // ok np insert_at(np n, int k, key_type x) { assert(0 <= k && k <= n->size); if (n == nil) return new avl_node(x); int sl = n->ch[L]->size; if (k <= sl) n->ch[L] = insert_at(n->ch[L], k, x); else n->ch[R] = insert_at(n->ch[R], k - sl - 1, x); return balance(update(n)); } // ok template <int dir> std::pair<np, np> remove_most(np n) { assert(n != nil); if (n->ch[dir] != nil) { np most; std::tie(n->ch[dir], most) = remove_most<dir>(n->ch[dir]); return {balance(update(n)), most}; } else { np res = n->ch[!dir]; n->ch[!dir] = nil; return {res, update(n)}; } } // ok std::pair<np, key_type> remove_at(np n, int k) { assert(n != nil); int sl = n->ch[L]->size; if (k < sl) { key_type most; std::tie(n->ch[L], most) = remove_at(n->ch[L], k); return {balance(update(n)), most}; } if (k == sl) { if (n->ch[R] == nil) { return {n->ch[L], n->key}; } else { np most; std::tie(n->ch[R], most) = remove_most<L>(n->ch[R]); most->ch = n->ch; // delete n; return {balance(update(most)), n->key}; } } else { key_type most; std::tie(n->ch[R], most) = remove_at(n->ch[R], k - sl - 1); return {balance(update(n)), most}; } } // ok np merge_with_root(np l, np root, np r) { // Members of `root` except root->key may not be valid for performance. if (abs(l->height - r->height) <= 1) { root->ch = {{l, r}}; return update(root); } else if (l->height > r->height) { l->ch[R] = merge_with_root(l->ch[R], root, r); return balance(update(l)); } else { r->ch[L] = merge_with_root(l, root, r->ch[L]); return balance(update(r)); } } // ok np merge(np l, np r) { if (l == nil) return r; if (r == nil) return l; np m; if (l->height > r->height) std::tie(r, m) = remove_most<L>(r); else std::tie(l, m) = remove_most<R>(l); return merge_with_root(l, m, r); } template <typename Iterator> np build(Iterator left, Iterator right) { int n = right - left; Iterator mid = left + n / 2; if (n == 0) return nil; np l = build(left, mid); np r = build(mid + 1, right); np m = new avl_node(*mid); m->ch = {{l, r}}; return update(m); } // ok std::pair<np, np> split_at(np n, int k) { assert(0 <= k && k <= n->size); if (n == nil) return {nil, nil}; int sl = n->ch[L]->size; np l = n->ch[L]; np r = n->ch[R]; n->ch[L] = n->ch[R] = nil; // Members of avl_node passed to `merge` must be valid, // but ones for `merge_with_root` doesn't have to. np nl, nr; if (k < sl) { std::tie(nl, nr) = split_at(l, k); return {nl, merge_with_root(nr, n, r)}; } else if (k == sl) { update(n); return {l, merge(n, r)}; } else { std::tie(nl, nr) = split_at(r, k - sl - 1); return {merge_with_root(l, n, nl), nr}; } } void update_at(np n, int k, key_type x) { assert(0 <= k && k <= n->size - 1); int sl = n->ch[L]->size; if (k < sl) { update_at(n->ch[L], k, x); } else if (k == sl) { n->key = x; } else { update_at(n->ch[R], k - sl - 1, x); } update(n); } key_type range_min(np n, int l, int r) { assert(n != nil); assert(0 <= l && l <= r); assert(l <= r && r <= n->size); if (l == 0 && r == n->size) { return n->min; } int sl = n->ch[L]->size; key_type res = 1e9; if (l < sl) { res = std::min(res, range_min(n->ch[L], l, std::min(r, sl))); } if (l <= sl && sl < r) { res = std::min(res, n->key); } if (sl + 1 < r) { res = std::min(res, range_min(n->ch[R], std::max(0, l - sl - 1), r - sl - 1)); } return res; } namespace test { int real_height(np n) { if (n == nil) return 0; return 1 + std::max(real_height(n->ch[L]), real_height(n->ch[R])); } int real_size(np n) { if (n == nil) return 0; return 1 + real_size(n->ch[L]) + real_size(n->ch[R]); } int real_min(np n) { if (n == nil) return 1e9; return std::min({real_min(n->ch[L]), n->key, real_min(n->ch[R])}); } bool verify(np n) { if (n == nil) { if (n->ch[L] != nil) return false; if (n->ch[R] != nil) return false; return true; } np l = n->ch[L]; np r = n->ch[R]; if (n->size != real_size(n)) return false; if (n->height != real_height(n)) return false; if (n->min != real_min(n)) return false; if (n->size != l->size + 1 + r->size) return false; if (n->height != std::max(l->height, r->height) + 1) return false; if (n->min != std::min({n->ch[L]->min, n->key, n->ch[R]->min})) return false; if (abs(bfactor(n)) >= 2) return false; return true; } } // namespace test void to_a(np n, std::vector<int> &v) { if (n == nil) return; to_a(n->ch[L], v); v.push_back(n->key); to_a(n->ch[R], v); } std::vector<int> to_a(np n) { std::vector<int> res; to_a(n, res); return res; } using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(0); int n, q; while (cin >> n >> q) { vector<int> a(n); for (int i = 0; i < n; ++i) cin >> a[i]; np root = build(a.begin(), a.end()); for (int i = 0; i < q; ++i) { int t, x, y; cin >> t >> x >> y; if (t == 0) { int t; tie(root, t) = remove_at(root, y); root = insert_at(root, x, t); } else if (t == 1) { ++y; cout << range_min(root, x, y) << '\n'; } else { update_at(root, x, y); } } } }
#define NDEBUG 1 #include <bits/stdc++.h> using key_type = int; enum { L, R }; struct avl_node { key_type key; std::array<avl_node *, 2> ch; int size; int height; int min; static avl_node *const nil; avl_node() : avl_node(-1) {} avl_node(key_type key) : avl_node(key, nil, nil, 1, 1, key) {} avl_node(const key_type key, avl_node *left, avl_node *right, int size, int height, key_type min) : key(key), ch({{left, right}}), size(size), height(height), min(min) {} void *operator new(size_t) { static int p = 0; static avl_node pool[400010]; return pool + p++; } }; avl_node *const avl_node::nil = new avl_node(key_type(), nullptr, nullptr, 0, 0, 1e9); avl_node *const nil = avl_node::nil; namespace trush { auto ___ = []() { return nil->ch = {{nil, nil}}; }; }; using np = avl_node *; using cnp = const avl_node *; // ok np update(np n) { n->size = n->ch[L]->size + 1 + n->ch[R]->size; n->height = std::max(n->ch[L]->height, n->ch[R]->height) + 1; n->min = std::min({n->ch[L]->min, n->key, n->ch[R]->min}); return n; } // ok template <int dir> np rotate(np n) { assert(n->ch[!dir] != nil); np root = n->ch[!dir]; n->ch[!dir] = root->ch[dir]; root->ch[dir] = n; update(n); update(root); return root; } // ok int bfactor(np n) { assert(n != nil); return n->ch[R]->height - n->ch[L]->height; } // ok np balance(np n) { assert(abs(bfactor(n)) <= 2); if (bfactor(n) == +2) { if (bfactor(n->ch[R]) < 0) n->ch[R] = rotate<R>(n->ch[R]); return rotate<L>(n); } else if (bfactor(n) == -2) { if (bfactor(n->ch[L]) > 0) n->ch[L] = rotate<L>(n->ch[L]); return rotate<R>(n); } else { return n; } } // ok np insert_at(np n, int k, key_type x) { assert(0 <= k && k <= n->size); if (n == nil) return new avl_node(x); int sl = n->ch[L]->size; if (k <= sl) n->ch[L] = insert_at(n->ch[L], k, x); else n->ch[R] = insert_at(n->ch[R], k - sl - 1, x); return balance(update(n)); } // ok template <int dir> std::pair<np, np> remove_most(np n) { assert(n != nil); if (n->ch[dir] != nil) { np most; std::tie(n->ch[dir], most) = remove_most<dir>(n->ch[dir]); return {balance(update(n)), most}; } else { np res = n->ch[!dir]; n->ch[!dir] = nil; return {res, update(n)}; } } // ok std::pair<np, key_type> remove_at(np n, int k) { assert(n != nil); int sl = n->ch[L]->size; if (k < sl) { key_type most; std::tie(n->ch[L], most) = remove_at(n->ch[L], k); return {balance(update(n)), most}; } if (k == sl) { if (n->ch[R] == nil) { return {n->ch[L], n->key}; } else { np most; std::tie(n->ch[R], most) = remove_most<L>(n->ch[R]); most->ch = n->ch; // delete n; return {balance(update(most)), n->key}; } } else { key_type most; std::tie(n->ch[R], most) = remove_at(n->ch[R], k - sl - 1); return {balance(update(n)), most}; } } // ok np merge_with_root(np l, np root, np r) { // Members of `root` except root->key may not be valid for performance. if (abs(l->height - r->height) <= 1) { root->ch = {{l, r}}; return update(root); } else if (l->height > r->height) { l->ch[R] = merge_with_root(l->ch[R], root, r); return balance(update(l)); } else { r->ch[L] = merge_with_root(l, root, r->ch[L]); return balance(update(r)); } } // ok np merge(np l, np r) { if (l == nil) return r; if (r == nil) return l; np m; if (l->height > r->height) std::tie(r, m) = remove_most<L>(r); else std::tie(l, m) = remove_most<R>(l); return merge_with_root(l, m, r); } template <typename Iterator> np build(Iterator left, Iterator right) { int n = right - left; Iterator mid = left + n / 2; if (n == 0) return nil; np l = build(left, mid); np r = build(mid + 1, right); np m = new avl_node(*mid); m->ch = {{l, r}}; return update(m); } // ok std::pair<np, np> split_at(np n, int k) { assert(0 <= k && k <= n->size); if (n == nil) return {nil, nil}; int sl = n->ch[L]->size; np l = n->ch[L]; np r = n->ch[R]; n->ch[L] = n->ch[R] = nil; // Members of avl_node passed to `merge` must be valid, // but ones for `merge_with_root` doesn't have to. np nl, nr; if (k < sl) { std::tie(nl, nr) = split_at(l, k); return {nl, merge_with_root(nr, n, r)}; } else if (k == sl) { update(n); return {l, merge(n, r)}; } else { std::tie(nl, nr) = split_at(r, k - sl - 1); return {merge_with_root(l, n, nl), nr}; } } void update_at(np n, int k, key_type x) { assert(0 <= k && k <= n->size - 1); int sl = n->ch[L]->size; if (k < sl) { update_at(n->ch[L], k, x); } else if (k == sl) { n->key = x; } else { update_at(n->ch[R], k - sl - 1, x); } update(n); } key_type range_min(np n, int l, int r) { assert(n != nil); assert(0 <= l && l <= r); assert(l <= r && r <= n->size); if (l == 0 && r == n->size) { return n->min; } int sl = n->ch[L]->size; key_type res = 1e9; if (l < sl) { res = std::min(res, range_min(n->ch[L], l, std::min(r, sl))); } if (l <= sl && sl < r) { res = std::min(res, n->key); } if (sl + 1 < r) { res = std::min(res, range_min(n->ch[R], std::max(0, l - sl - 1), r - sl - 1)); } return res; } namespace test { int real_height(np n) { if (n == nil) return 0; return 1 + std::max(real_height(n->ch[L]), real_height(n->ch[R])); } int real_size(np n) { if (n == nil) return 0; return 1 + real_size(n->ch[L]) + real_size(n->ch[R]); } int real_min(np n) { if (n == nil) return 1e9; return std::min({real_min(n->ch[L]), n->key, real_min(n->ch[R])}); } bool verify(np n) { if (n == nil) { if (n->ch[L] != nil) return false; if (n->ch[R] != nil) return false; return true; } np l = n->ch[L]; np r = n->ch[R]; if (n->size != real_size(n)) return false; if (n->height != real_height(n)) return false; if (n->min != real_min(n)) return false; if (n->size != l->size + 1 + r->size) return false; if (n->height != std::max(l->height, r->height) + 1) return false; if (n->min != std::min({n->ch[L]->min, n->key, n->ch[R]->min})) return false; if (abs(bfactor(n)) >= 2) return false; return true; } } // namespace test void to_a(np n, std::vector<int> &v) { if (n == nil) return; to_a(n->ch[L], v); v.push_back(n->key); to_a(n->ch[R], v); } std::vector<int> to_a(np n) { std::vector<int> res; to_a(n, res); return res; } using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(0); int n, q; while (cin >> n >> q) { vector<int> a(n); for (int i = 0; i < n; ++i) cin >> a[i]; np root = build(a.begin(), a.end()); for (int i = 0; i < q; ++i) { int t, x, y; cin >> t >> x >> y; if (t == 0) { int t; tie(root, t) = remove_at(root, y); root = insert_at(root, x, t); } else if (t == 1) { ++y; cout << range_min(root, x, y) << '\n'; } else { update_at(root, x, y); } } } }
replace
25
26
25
26
0
p00998
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using int64 = long long; template <class T> struct ArrayPool { vector<T> pool; vector<T *> stock; int ptr; ArrayPool(int sz) : pool(sz), stock(sz) {} inline T *alloc() { return stock[--ptr]; } inline void free(T *t) { stock[ptr++] = t; } void clear() { ptr = (int)pool.size(); for (int i = 0; i < pool.size(); i++) stock[i] = &pool[i]; } }; template <class Monoid, class OperatorMonoid = Monoid> struct RedBlackTree { using F = function<Monoid(Monoid, Monoid)>; using G = function<Monoid(Monoid, OperatorMonoid)>; using H = function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>; using P = function<OperatorMonoid(OperatorMonoid, int)>; enum COLOR { BLACK, RED }; struct Node { Node *l, *r; COLOR color; int level, cnt; Monoid key, sum; OperatorMonoid lazy; Node() {} Node(const Monoid &k, const OperatorMonoid &p) : key(k), sum(k), l(nullptr), r(nullptr), color(BLACK), level(0), cnt(1), lazy(p) {} Node(Node *l, Node *r, const Monoid &k, const OperatorMonoid &p) : key(k), color(RED), l(l), r(r), lazy(p) {} }; ArrayPool<Node> pool; const Monoid M1; const OperatorMonoid OM0; const F f; const G g; const H h; const P p; RedBlackTree(int sz, const F &f, const Monoid &M1) : pool(sz), f(f), g(G()), h(H()), p(P()), M1(M1), OM0(OperatorMonoid()) {} RedBlackTree(int sz, const F &f, const G &g, const H &h, const P &p, const Monoid &M1, const OperatorMonoid &OM0) : pool(sz), f(f), g(g), h(h), p(p), M1(M1), OM0(OM0) {} inline Node *alloc(const Monoid &key) { return &(*pool.alloc() = Node(key, OM0)); } inline Node *alloc(Node *l, Node *r) { auto t = &(*pool.alloc() = Node(l, r, M1, OM0)); return update(t); } virtual Node *clone(Node *t) { return t; } inline int count(const Node *t) { return t ? t->cnt : 0; } inline Monoid sum(const Node *t) { return t ? t->sum : M1; } Node *update(Node *t) { t->cnt = count(t->l) + count(t->r) + (!t->l || !t->r); t->level = t->l ? t->l->level + (t->l->color == BLACK) : 0; t->sum = f(f(sum(t->l), t->key), sum(t->r)); return t; } Node *propagate(Node *t) { t = clone(t); if (t->lazy != OM0) { if (!t->l) { t->key = g(t->key, t->lazy); } else { if (t->l) { t->l = clone(t->l); t->l->lazy = h(t->l->lazy, t->lazy); t->l->sum = g(t->l->sum, p(t->lazy, count(t->l))); } if (t->r) { t->r = clone(t->r); t->r->lazy = h(t->r->lazy, t->lazy); t->r->sum = g(t->r->sum, p(t->lazy, count(t->r))); } } t->lazy = OM0; } return update(t); } Node *rotate(Node *t, bool b) { t = propagate(t); Node *s; if (b) { s = propagate(t->l); t->l = s->r; s->r = t; } else { s = propagate(t->r); t->r = s->l; s->l = t; } update(t); return update(s); } Node *submerge(Node *l, Node *r) { if (l->level < r->level) { r = propagate(r); Node *c = (r->l = submerge(l, r->l)); if (r->color == BLACK && c->color == RED && c->l && c->l->color == RED) { r->color = RED; c->color = BLACK; if (r->r->color == BLACK) return rotate(r, true); r->r->color = BLACK; } return update(r); } if (l->level > r->level) { l = propagate(l); Node *c = (l->r = submerge(l->r, r)); if (l->color == BLACK && c->color == RED && c->r && c->r->color == RED) { l->color = RED; c->color = BLACK; if (l->l->color == BLACK) return rotate(l, false); l->l->color = BLACK; } return update(l); } return alloc(l, r); } Node *merge(Node *l, Node *r) { if (!l || !r) return l ? l : r; Node *c = submerge(l, r); c->color = BLACK; return c; } pair<Node *, Node *> split(Node *t, int k) { if (!t) return {nullptr, nullptr}; t = propagate(t); if (k == 0) return {nullptr, t}; if (k >= count(t)) return {t, nullptr}; if (k < count(t->l)) { auto p = split(t->l, k); return {p.first, merge(p.second, t->r)}; } if (k > count(t->l)) { auto p = split(t->r, k - count(t->l)); return {merge(t->l, p.first), p.second}; } pair<Node *, Node *> ret = {t->l, t->r}; pool.free(t); return ret; } Node *build(int l, int r, const vector<Monoid> &v) { if (l + 1 >= r) return alloc(v[l]); return merge(build(l, (l + r) >> 1, v), build((l + r) >> 1, r, v)); } Node *build(const vector<Monoid> &v) { pool.clear(); return build(0, (int)v.size(), v); } void dump(Node *r, typename vector<Monoid>::iterator &it) { r = propagate(r); if (!r->l || !r->r) { *it++ = r->key; return; } dump(r->l, it); dump(r->r, it); } vector<Monoid> dump(Node *r) { vector<Monoid> v((size_t)count(r)); auto it = begin(v); dump(r, it); return v; } string to_string(Node *r) { auto s = dump(r); string ret; for (int i = 0; i < s.size(); i++) { ret += std::to_string(s[i]); ret += ", "; } return (ret); } void insert(Node *&t, int k, const Monoid &v) { auto x = split(t, k); t = merge(merge(x.first, alloc(v)), x.second); } void erase(Node *&t, int k) { auto x = split(t, k); auto y = split(x.second, 1); pool.free(y.first); t = merge(x.first, y.second); } Monoid query(Node *&t, int a, int b) { auto x = split(t, a); auto y = split(x.second, b - a); auto ret = sum(y.first); t = merge(x.first, merge(y.first, y.second)); return ret; } void set_propagate(Node *&t, int a, int b, const OperatorMonoid &p) { auto x = split(t, a); auto y = split(x.second, b - a); y.first->lazy = h(y.first->lazy, p); t = merge(x.first, merge(propagate(y.first), y.second)); } void set_element(Node *&t, int k, const Monoid &x) { if (!t->l) { t->key = t->sum = x; return; } t = propagate(t); if (k < count(t->l)) set_element(t->l, k, x); else set_element(t->r, k - count(t->l), x); t = update(t); } int size(Node *t) { return count(t); } bool empty(Node *t) { return !t; } Node *makeset() { return (nullptr); } }; using int64 = long long; int main() { int n, q; scanf("%d %d", &n, &q); vector<int> v(n); for (int i = 0; i < n; i++) scanf("%d", &v[i]); auto f = [](int a, int b) { return min(a, b); }; const int INF = 1 << 30; const int LIM = 4000000; RedBlackTree<int, int> rbt(LIM + LIM / 2, f, INF); auto root = rbt.build(v); for (int i = 0; i < q; i++) { int x, y, z; scanf("%d %d %d", &x, &y, &z); if (x == 0) { int v = rbt.query(root, z, z + 1); rbt.erase(root, z); rbt.insert(root, y, v); } else if (x == 1) { printf("%d\n", rbt.query(root, y, z + 1)); } else if (x == 2) { rbt.set_element(root, y, z); } if (rbt.pool.ptr > LIM) root = rbt.build(rbt.dump(root)); } }
#include <bits/stdc++.h> using namespace std; using int64 = long long; template <class T> struct ArrayPool { vector<T> pool; vector<T *> stock; int ptr; ArrayPool(int sz) : pool(sz), stock(sz) {} inline T *alloc() { return stock[--ptr]; } inline void free(T *t) { stock[ptr++] = t; } void clear() { ptr = (int)pool.size(); for (int i = 0; i < pool.size(); i++) stock[i] = &pool[i]; } }; template <class Monoid, class OperatorMonoid = Monoid> struct RedBlackTree { using F = function<Monoid(Monoid, Monoid)>; using G = function<Monoid(Monoid, OperatorMonoid)>; using H = function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>; using P = function<OperatorMonoid(OperatorMonoid, int)>; enum COLOR { BLACK, RED }; struct Node { Node *l, *r; COLOR color; int level, cnt; Monoid key, sum; OperatorMonoid lazy; Node() {} Node(const Monoid &k, const OperatorMonoid &p) : key(k), sum(k), l(nullptr), r(nullptr), color(BLACK), level(0), cnt(1), lazy(p) {} Node(Node *l, Node *r, const Monoid &k, const OperatorMonoid &p) : key(k), color(RED), l(l), r(r), lazy(p) {} }; ArrayPool<Node> pool; const Monoid M1; const OperatorMonoid OM0; const F f; const G g; const H h; const P p; RedBlackTree(int sz, const F &f, const Monoid &M1) : pool(sz), f(f), g(G()), h(H()), p(P()), M1(M1), OM0(OperatorMonoid()) {} RedBlackTree(int sz, const F &f, const G &g, const H &h, const P &p, const Monoid &M1, const OperatorMonoid &OM0) : pool(sz), f(f), g(g), h(h), p(p), M1(M1), OM0(OM0) {} inline Node *alloc(const Monoid &key) { return &(*pool.alloc() = Node(key, OM0)); } inline Node *alloc(Node *l, Node *r) { auto t = &(*pool.alloc() = Node(l, r, M1, OM0)); return update(t); } virtual Node *clone(Node *t) { return t; } inline int count(const Node *t) { return t ? t->cnt : 0; } inline Monoid sum(const Node *t) { return t ? t->sum : M1; } Node *update(Node *t) { t->cnt = count(t->l) + count(t->r) + (!t->l || !t->r); t->level = t->l ? t->l->level + (t->l->color == BLACK) : 0; t->sum = f(f(sum(t->l), t->key), sum(t->r)); return t; } Node *propagate(Node *t) { t = clone(t); if (t->lazy != OM0) { if (!t->l) { t->key = g(t->key, t->lazy); } else { if (t->l) { t->l = clone(t->l); t->l->lazy = h(t->l->lazy, t->lazy); t->l->sum = g(t->l->sum, p(t->lazy, count(t->l))); } if (t->r) { t->r = clone(t->r); t->r->lazy = h(t->r->lazy, t->lazy); t->r->sum = g(t->r->sum, p(t->lazy, count(t->r))); } } t->lazy = OM0; } return update(t); } Node *rotate(Node *t, bool b) { t = propagate(t); Node *s; if (b) { s = propagate(t->l); t->l = s->r; s->r = t; } else { s = propagate(t->r); t->r = s->l; s->l = t; } update(t); return update(s); } Node *submerge(Node *l, Node *r) { if (l->level < r->level) { r = propagate(r); Node *c = (r->l = submerge(l, r->l)); if (r->color == BLACK && c->color == RED && c->l && c->l->color == RED) { r->color = RED; c->color = BLACK; if (r->r->color == BLACK) return rotate(r, true); r->r->color = BLACK; } return update(r); } if (l->level > r->level) { l = propagate(l); Node *c = (l->r = submerge(l->r, r)); if (l->color == BLACK && c->color == RED && c->r && c->r->color == RED) { l->color = RED; c->color = BLACK; if (l->l->color == BLACK) return rotate(l, false); l->l->color = BLACK; } return update(l); } return alloc(l, r); } Node *merge(Node *l, Node *r) { if (!l || !r) return l ? l : r; Node *c = submerge(l, r); c->color = BLACK; return c; } pair<Node *, Node *> split(Node *t, int k) { if (!t) return {nullptr, nullptr}; t = propagate(t); if (k == 0) return {nullptr, t}; if (k >= count(t)) return {t, nullptr}; if (k < count(t->l)) { auto p = split(t->l, k); return {p.first, merge(p.second, t->r)}; } if (k > count(t->l)) { auto p = split(t->r, k - count(t->l)); return {merge(t->l, p.first), p.second}; } pair<Node *, Node *> ret = {t->l, t->r}; pool.free(t); return ret; } Node *build(int l, int r, const vector<Monoid> &v) { if (l + 1 >= r) return alloc(v[l]); return merge(build(l, (l + r) >> 1, v), build((l + r) >> 1, r, v)); } Node *build(const vector<Monoid> &v) { pool.clear(); return build(0, (int)v.size(), v); } void dump(Node *r, typename vector<Monoid>::iterator &it) { r = propagate(r); if (!r->l || !r->r) { *it++ = r->key; return; } dump(r->l, it); dump(r->r, it); } vector<Monoid> dump(Node *r) { vector<Monoid> v((size_t)count(r)); auto it = begin(v); dump(r, it); return v; } string to_string(Node *r) { auto s = dump(r); string ret; for (int i = 0; i < s.size(); i++) { ret += std::to_string(s[i]); ret += ", "; } return (ret); } void insert(Node *&t, int k, const Monoid &v) { auto x = split(t, k); t = merge(merge(x.first, alloc(v)), x.second); } void erase(Node *&t, int k) { auto x = split(t, k); auto y = split(x.second, 1); pool.free(y.first); t = merge(x.first, y.second); } Monoid query(Node *&t, int a, int b) { auto x = split(t, a); auto y = split(x.second, b - a); auto ret = sum(y.first); t = merge(x.first, merge(y.first, y.second)); return ret; } void set_propagate(Node *&t, int a, int b, const OperatorMonoid &p) { auto x = split(t, a); auto y = split(x.second, b - a); y.first->lazy = h(y.first->lazy, p); t = merge(x.first, merge(propagate(y.first), y.second)); } void set_element(Node *&t, int k, const Monoid &x) { if (!t->l) { t->key = t->sum = x; return; } t = propagate(t); if (k < count(t->l)) set_element(t->l, k, x); else set_element(t->r, k - count(t->l), x); t = update(t); } int size(Node *t) { return count(t); } bool empty(Node *t) { return !t; } Node *makeset() { return (nullptr); } }; using int64 = long long; int main() { int n, q; scanf("%d %d", &n, &q); vector<int> v(n); for (int i = 0; i < n; i++) scanf("%d", &v[i]); auto f = [](int a, int b) { return min(a, b); }; const int INF = 1 << 30; const int LIM = 4000000; RedBlackTree<int, int> rbt(LIM + LIM / 2, f, INF); auto root = rbt.build(v); for (int i = 0; i < q; i++) { int x, y, z; scanf("%d %d %d", &x, &y, &z); if (x == 0) { int v = rbt.query(root, z, z + 1); rbt.erase(root, z); rbt.insert(root, y, v); } else if (x == 1) { printf("%d\n", rbt.query(root, y, z + 1)); } else if (x == 2) { rbt.set_element(root, y, z); } if (rbt.pool.ptr < LIM / 2) root = rbt.build(rbt.dump(root)); } }
replace
295
296
295
296
TLE
p01001
C++
Memory Limit Exceeded
#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; int tx[] = {0, 1, 0, -1}; int ty[] = {-1, 0, 1, 0}; static const double EPS = 1e-8; double dp[101][101][101][11] [5]; // dp[note_i][prev_combo][prev_score][prev_stability][prev_action] int main() { while (true) { // lt rt lk rk int stability[4][4]; for (int i = 0; i < 4; i++) { int count = 0; for (int j = 0; j < 4; j++) { scanf("%d", &stability[i][j]); if (i == 0 && stability[i][j] == -1) { count++; } } if (count >= 4) goto over; } int len; scanf("%d", &len); int notes[101]; // 0:none 1:t 2:k for (int note_i = 0; note_i < len; note_i++) { scanf("%d", &notes[note_i]); } int A, B; scanf("%d %d", &A, &B); memset(dp, 0, sizeof(dp)); for (int prev_combo = 10; prev_combo >= 0; prev_combo--) { for (int prev_action = 4; prev_action >= 0; prev_action--) { for (int prev_stability = 10; prev_stability >= 0; prev_stability--) { dp[len][prev_combo][100][prev_stability][prev_action] = 1.0; } } } // dp[note_i][prev_combo][prev_score][prev_stability][prev_action] for (int note_i = len - 1; note_i >= 0; note_i--) { if (notes[note_i] == 0) { for (int prev_score = 100; prev_score >= 0; prev_score--) { for (int prev_combo = 10; prev_combo >= 0; prev_combo--) { for (int prev_stability = 10; prev_stability >= 0; prev_stability--) { for (int prev_action = 4; prev_action >= 0; prev_action--) { dp[note_i][prev_combo][prev_score][prev_stability] [prev_action] = dp[note_i + 1][prev_combo][prev_score][10][4]; } } } } } else if (notes[note_i] > 0) { int offset = (notes[note_i] == 1 ? 0 : 2); for (int prev_score = 100; prev_score >= 0; prev_score--) { for (int prev_combo = 10; prev_combo >= 0; prev_combo--) { int added = (A + B * prev_combo) / 100; for (int prev_stability = 10; prev_stability >= 0; prev_stability--) { for (int prev_action = 3; prev_action >= 0; prev_action--) { dp[note_i][prev_combo][prev_score][prev_stability] [prev_action] = dp[note_i + 1][0][prev_score][10][4]; int st0 = max(0, (stability[prev_action][offset + 0] - 10) * 10 + prev_stability * 10); int st1 = max(0, (stability[prev_action][offset + 1] - 10) * 10 + prev_stability * 10); double precision0 = (double)st0 / 100.0; double precision1 = (double)st1 / 100.0; // bang dp[note_i][prev_combo][prev_score][prev_stability] [prev_action] = max( dp[note_i][prev_combo][prev_score][prev_stability] [prev_action], dp[note_i + 1][min(10, prev_combo + 1)] [min(100, prev_score + added)][st0 / 10][offset + 0] * precision0 + dp[note_i + 1][0][prev_score][st0 / 10][offset + 0] * (1.0 - precision0)); dp[note_i][prev_combo][prev_score][prev_stability] [prev_action] = max( dp[note_i][prev_combo][prev_score][prev_stability] [prev_action], dp[note_i + 1][min(10, prev_combo + 1)] [min(100, prev_score + added)][st1 / 10][offset + 1] * precision1 + dp[note_i + 1][0][prev_score][st1 / 10][offset + 1] * (1.0 - precision1)); } dp[note_i][prev_combo][prev_score][prev_stability][4] = dp[note_i + 1][0][prev_score][10][4]; dp[note_i][prev_combo][prev_score][prev_stability][4] = max(dp[note_i][prev_combo][prev_score][prev_stability][4], max(dp[note_i + 1][min(10, prev_combo + 1)] [min(100, prev_score + added)][10][offset + 0], dp[note_i + 1][min(10, prev_combo + 1)] [min(100, prev_score + added)][10][offset + 1])); } } } } } printf("%.6lf\n", dp[0][0][0][10][4]); } over:; }
#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; int tx[] = {0, 1, 0, -1}; int ty[] = {-1, 0, 1, 0}; static const double EPS = 1e-8; double dp[101][11][101][11] [5]; // dp[note_i][prev_combo][prev_score][prev_stability][prev_action] int main() { while (true) { // lt rt lk rk int stability[4][4]; for (int i = 0; i < 4; i++) { int count = 0; for (int j = 0; j < 4; j++) { scanf("%d", &stability[i][j]); if (i == 0 && stability[i][j] == -1) { count++; } } if (count >= 4) goto over; } int len; scanf("%d", &len); int notes[101]; // 0:none 1:t 2:k for (int note_i = 0; note_i < len; note_i++) { scanf("%d", &notes[note_i]); } int A, B; scanf("%d %d", &A, &B); memset(dp, 0, sizeof(dp)); for (int prev_combo = 10; prev_combo >= 0; prev_combo--) { for (int prev_action = 4; prev_action >= 0; prev_action--) { for (int prev_stability = 10; prev_stability >= 0; prev_stability--) { dp[len][prev_combo][100][prev_stability][prev_action] = 1.0; } } } // dp[note_i][prev_combo][prev_score][prev_stability][prev_action] for (int note_i = len - 1; note_i >= 0; note_i--) { if (notes[note_i] == 0) { for (int prev_score = 100; prev_score >= 0; prev_score--) { for (int prev_combo = 10; prev_combo >= 0; prev_combo--) { for (int prev_stability = 10; prev_stability >= 0; prev_stability--) { for (int prev_action = 4; prev_action >= 0; prev_action--) { dp[note_i][prev_combo][prev_score][prev_stability] [prev_action] = dp[note_i + 1][prev_combo][prev_score][10][4]; } } } } } else if (notes[note_i] > 0) { int offset = (notes[note_i] == 1 ? 0 : 2); for (int prev_score = 100; prev_score >= 0; prev_score--) { for (int prev_combo = 10; prev_combo >= 0; prev_combo--) { int added = (A + B * prev_combo) / 100; for (int prev_stability = 10; prev_stability >= 0; prev_stability--) { for (int prev_action = 3; prev_action >= 0; prev_action--) { dp[note_i][prev_combo][prev_score][prev_stability] [prev_action] = dp[note_i + 1][0][prev_score][10][4]; int st0 = max(0, (stability[prev_action][offset + 0] - 10) * 10 + prev_stability * 10); int st1 = max(0, (stability[prev_action][offset + 1] - 10) * 10 + prev_stability * 10); double precision0 = (double)st0 / 100.0; double precision1 = (double)st1 / 100.0; // bang dp[note_i][prev_combo][prev_score][prev_stability] [prev_action] = max( dp[note_i][prev_combo][prev_score][prev_stability] [prev_action], dp[note_i + 1][min(10, prev_combo + 1)] [min(100, prev_score + added)][st0 / 10][offset + 0] * precision0 + dp[note_i + 1][0][prev_score][st0 / 10][offset + 0] * (1.0 - precision0)); dp[note_i][prev_combo][prev_score][prev_stability] [prev_action] = max( dp[note_i][prev_combo][prev_score][prev_stability] [prev_action], dp[note_i + 1][min(10, prev_combo + 1)] [min(100, prev_score + added)][st1 / 10][offset + 1] * precision1 + dp[note_i + 1][0][prev_score][st1 / 10][offset + 1] * (1.0 - precision1)); } dp[note_i][prev_combo][prev_score][prev_stability][4] = dp[note_i + 1][0][prev_score][10][4]; dp[note_i][prev_combo][prev_score][prev_stability][4] = max(dp[note_i][prev_combo][prev_score][prev_stability][4], max(dp[note_i + 1][min(10, prev_combo + 1)] [min(100, prev_score + added)][10][offset + 0], dp[note_i + 1][min(10, prev_combo + 1)] [min(100, prev_score + added)][10][offset + 1])); } } } } } printf("%.6lf\n", dp[0][0][0][10][4]); } over:; }
replace
32
33
32
33
MLE
p01002
C++
Runtime Error
#include <iostream> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; int n; int a[5][5]; int s[5]; int dy[] = {-1, 0, 1, 0}, dx[] = {0, 1, 0, -1}; int check(void) { int res = 0; for (int d = 1;; d++) { bool dis[5][5], f = false; rep(i, 5) rep(j, 5) dis[i][j] = false; rep(i, 5) rep(j, 5) { if (a[i][j] == 0) continue; rep(k, 2) { int sy = i, sx = j; int c = 0; while (sy >= 0 && sx >= 0 && sy <= 4 && sx <= 4 && a[sy][sx] == a[i][j]) { c++; sy += dy[k]; sx += dx[k]; } if (c < 3) continue; f = true; sy = i; sx = j; while (sy >= 0 && sx >= 0 && sy <= 4 && sx <= 4 && a[sy][sx] == a[i][j]) { dis[sy][sx] = true; sy += dy[k]; sx += dx[k]; } } } if (!f) return res; rep(i, 5) rep(j, 5) { if (dis[i][j]) { res += d * s[a[i][j] - 1]; a[i][j] = 0; } } rep(i, 5) { for (int j = 4; j >= 0; j--) { if (a[j][i] != 0) { for (int k = 1; k <= 4 - j; k++) { if (a[j + k][i] == 0) swap(a[j + k][i], a[j + k - 1][i]); } } } } } } int move(int y, int x, int d) { int tmp[5][5]; rep(i, 5) rep(j, 5) tmp[i][j] = a[i][j]; int res = check(); rep(i, 5) rep(j, 5) a[i][j] = tmp[i][j]; if (d == n) return res; rep(k, 4) { int sy = y + dy[k], sx = x + dx[k]; if (sy < 0 || sy > 4 || sx < 0 || sy > 4) continue; swap(a[y][x], a[sy][sx]); res = max(res, move(sy, sx, d + 1)); swap(a[y][x], a[sy][sx]); } return res; } int solve() { int res = 0; rep(i, 5) rep(j, 5) res = max(res, move(i, j, 0)); return res; } int main() { while (cin >> n, n >= 0) { rep(i, 5) rep(j, 5) cin >> a[i][j]; rep(i, 5) cin >> s[i]; cout << solve() << endl; } }
#include <iostream> #define rep(i, n) for (int i = 0; i < n; i++) using namespace std; int n; int a[5][5]; int s[5]; int dy[] = {-1, 0, 1, 0}, dx[] = {0, 1, 0, -1}; int check(void) { int res = 0; for (int d = 1;; d++) { bool dis[5][5], f = false; rep(i, 5) rep(j, 5) dis[i][j] = false; rep(i, 5) rep(j, 5) { if (a[i][j] == 0) continue; rep(k, 2) { int sy = i, sx = j; int c = 0; while (sy >= 0 && sx >= 0 && sy <= 4 && sx <= 4 && a[sy][sx] == a[i][j]) { c++; sy += dy[k]; sx += dx[k]; } if (c < 3) continue; f = true; sy = i; sx = j; while (sy >= 0 && sx >= 0 && sy <= 4 && sx <= 4 && a[sy][sx] == a[i][j]) { dis[sy][sx] = true; sy += dy[k]; sx += dx[k]; } } } if (!f) return res; rep(i, 5) rep(j, 5) { if (dis[i][j]) { res += d * s[a[i][j] - 1]; a[i][j] = 0; } } rep(i, 5) { for (int j = 4; j >= 0; j--) { if (a[j][i] != 0) { for (int k = 1; k <= 4 - j; k++) { if (a[j + k][i] == 0) swap(a[j + k][i], a[j + k - 1][i]); } } } } } } int move(int y, int x, int d) { int tmp[5][5]; rep(i, 5) rep(j, 5) tmp[i][j] = a[i][j]; int res = check(); rep(i, 5) rep(j, 5) a[i][j] = tmp[i][j]; if (d == n) return res; rep(k, 4) { int sy = y + dy[k], sx = x + dx[k]; if (sy < 0 || sy > 4 || sx < 0 || sx > 4) continue; swap(a[y][x], a[sy][sx]); res = max(res, move(sy, sx, d + 1)); swap(a[y][x], a[sy][sx]); } return res; } int solve() { int res = 0; rep(i, 5) rep(j, 5) res = max(res, move(i, j, 0)); return res; } int main() { while (cin >> n, n >= 0) { rep(i, 5) rep(j, 5) cin >> a[i][j]; rep(i, 5) cin >> s[i]; cout << solve() << endl; } }
replace
75
76
75
76
0
p01006
C++
Runtime Error
#include <iostream> #include <string> using namespace std; string s[3] = {"ABC", "DEF", "GHI"}, pass; int vx[4] = {-1, 0, 1, 0}, vy[4] = {0, -1, 0, 1}; bool search(int cnt, int n) { if (cnt == pass.size() - 1) return true; bool ret = false; for (int i = 0; i < 4; ++i) { int nx = n / 3 + vx[i], ny = n % 3 + vy[i]; if (0 <= nx && nx <= 3 && 0 <= ny && ny <= 3) { if (pass[cnt + 1] == s[nx][ny]) { ret = search(cnt + 1, pass[cnt + 1] - 'A'); if (ret) return ret; } } } return false; } int main() { cin.tie(0); ios::sync_with_stdio(false); for (int i = 0; i < 1000; ++i) { cin >> pass; int num = pass[0] - 'A'; if (search(0, num)) cout << pass << endl; } return 0; }
#include <iostream> #include <string> using namespace std; string s[3] = {"ABC", "DEF", "GHI"}, pass; int vx[4] = {-1, 0, 1, 0}, vy[4] = {0, -1, 0, 1}; bool search(int cnt, int n) { if (cnt == pass.size() - 1) return true; bool ret = false; for (int i = 0; i < 4; ++i) { int nx = n / 3 + vx[i], ny = n % 3 + vy[i]; if (0 <= nx && nx <= 2 && 0 <= ny && ny <= 2) { if (pass[cnt + 1] == s[nx][ny]) { ret = search(cnt + 1, pass[cnt + 1] - 'A'); if (ret) return ret; } } } return false; } int main() { cin.tie(0); ios::sync_with_stdio(false); for (int i = 0; i < 1000; ++i) { cin >> pass; int num = pass[0] - 'A'; if (search(0, num)) cout << pass << endl; } return 0; }
replace
16
17
16
17
0
p01007
C++
Runtime Error
#include <algorithm> #include <cassert> #include <cstdint> #include <cstdio> #include <queue> #include <utility> #include <vector> using Matrix = std::vector<std::vector<int>>; void rotate(Matrix &a) { size_t r, c, size; int angle; scanf("%zu %zu %zu %d", &r, &c, &size, &angle); --r, --c; if (angle == 0 || angle == 360) return; // ??? Matrix b(size, std::vector<int>(size)); for (size_t i = 0; i < size; ++i) for (size_t j = 0; j < size; ++j) b[i][j] = a[r + i][c + j]; for (size_t i = 0; i < size; ++i) { for (size_t j = 0; j < size; ++j) { if (angle == 90) { a[r + i][c + j] = b[size - j - 1][i]; } else if (angle == 180) { a[r + i][c + j] = b[size - i - 1][size - j - 1]; } else if (angle == 270) { a[r + i][c + j] = b[j][size - i - 1]; } } } } void reversal(Matrix &a) { size_t r, c, size; scanf("%zu %zu %zu", &r, &c, &size); --r, --c; for (size_t i = 0; i < size; ++i) for (size_t j = 0; j < size; ++j) a[r + i][c + j] ^= 1; } void left_shift(Matrix &a) { size_t r; scanf("%zu", &r); --r; int tmp = a[r][0]; for (size_t j = 1; j < a.size(); ++j) a[r][j - 1] = a[r][j]; a[r].back() = tmp; } void right_shift(Matrix &a) { size_t r; scanf("%zu", &r); --r; int tmp = a[r].back(); for (size_t j = a.size(); j >= 1; --j) a[r][j] = a[r][j - 1]; a[r][0] = tmp; } const size_t di[] = {size_t(-1), 0, 1, 0}; const size_t dj[] = {0, size_t(-1), 0, 1}; void island_reversal(Matrix &a) { size_t r, c; scanf("%zu %zu", &r, &c); --r, --c; int par = a[r][c]; std::queue<std::pair<size_t, size_t>> q; q.emplace(r, c); while (!q.empty()) { size_t i = q.front().first, j = q.front().second; q.pop(); if (a[i][j] != par) continue; a[i][j] ^= 1; for (int k = 0; k < 4; ++k) { size_t ni = i + di[k], nj = j + dj[k]; if (!(ni < a.size() && nj < a.size())) continue; if (a[ni][nj] != par) continue; q.emplace(ni, nj); } } } int main() { size_t n, m; scanf("%zu %zu", &n, &m); std::vector<std::vector<int>> a(n, std::vector<int>(n)); for (size_t i = 0; i < n; ++i) { for (size_t j = 0; j < n; ++j) { scanf("%d", &a[i][j]); } } for (size_t i = 0; i < m; ++i) { int op; scanf("%d", &op); switch (op) { case 0: rotate(a); break; case 1: reversal(a); break; case 2: left_shift(a); break; case 3: right_shift(a); break; case 4: island_reversal(a); break; } // for (size_t i=0; i<n; ++i) // for (size_t j=0; j<n; ++j) // fprintf(stderr, "%d%c", a[i][j], j+1<n? ' ':'\n'); } for (size_t i = 0; i < n; ++i) for (size_t j = 0; j < n; ++j) printf("%d%c", a[i][j], j + 1 < n ? ' ' : '\n'); }
#include <algorithm> #include <cassert> #include <cstdint> #include <cstdio> #include <queue> #include <utility> #include <vector> using Matrix = std::vector<std::vector<int>>; void rotate(Matrix &a) { size_t r, c, size; int angle; scanf("%zu %zu %zu %d", &r, &c, &size, &angle); --r, --c; if (angle == 0 || angle == 360) return; // ??? Matrix b(size, std::vector<int>(size)); for (size_t i = 0; i < size; ++i) for (size_t j = 0; j < size; ++j) b[i][j] = a[r + i][c + j]; for (size_t i = 0; i < size; ++i) { for (size_t j = 0; j < size; ++j) { if (angle == 90) { a[r + i][c + j] = b[size - j - 1][i]; } else if (angle == 180) { a[r + i][c + j] = b[size - i - 1][size - j - 1]; } else if (angle == 270) { a[r + i][c + j] = b[j][size - i - 1]; } } } } void reversal(Matrix &a) { size_t r, c, size; scanf("%zu %zu %zu", &r, &c, &size); --r, --c; for (size_t i = 0; i < size; ++i) for (size_t j = 0; j < size; ++j) a[r + i][c + j] ^= 1; } void left_shift(Matrix &a) { size_t r; scanf("%zu", &r); --r; int tmp = a[r][0]; for (size_t j = 1; j < a.size(); ++j) a[r][j - 1] = a[r][j]; a[r].back() = tmp; } void right_shift(Matrix &a) { size_t r; scanf("%zu", &r); --r; int tmp = a[r].back(); for (size_t j = a.size() - 1; j >= 1; --j) a[r][j] = a[r][j - 1]; a[r][0] = tmp; } const size_t di[] = {size_t(-1), 0, 1, 0}; const size_t dj[] = {0, size_t(-1), 0, 1}; void island_reversal(Matrix &a) { size_t r, c; scanf("%zu %zu", &r, &c); --r, --c; int par = a[r][c]; std::queue<std::pair<size_t, size_t>> q; q.emplace(r, c); while (!q.empty()) { size_t i = q.front().first, j = q.front().second; q.pop(); if (a[i][j] != par) continue; a[i][j] ^= 1; for (int k = 0; k < 4; ++k) { size_t ni = i + di[k], nj = j + dj[k]; if (!(ni < a.size() && nj < a.size())) continue; if (a[ni][nj] != par) continue; q.emplace(ni, nj); } } } int main() { size_t n, m; scanf("%zu %zu", &n, &m); std::vector<std::vector<int>> a(n, std::vector<int>(n)); for (size_t i = 0; i < n; ++i) { for (size_t j = 0; j < n; ++j) { scanf("%d", &a[i][j]); } } for (size_t i = 0; i < m; ++i) { int op; scanf("%d", &op); switch (op) { case 0: rotate(a); break; case 1: reversal(a); break; case 2: left_shift(a); break; case 3: right_shift(a); break; case 4: island_reversal(a); break; } // for (size_t i=0; i<n; ++i) // for (size_t j=0; j<n; ++j) // fprintf(stderr, "%d%c", a[i][j], j+1<n? ' ':'\n'); } for (size_t i = 0; i < n; ++i) for (size_t j = 0; j < n; ++j) printf("%d%c", a[i][j], j + 1 < n ? ' ' : '\n'); }
replace
64
65
64
65
0
p01007
C++
Time Limit Exceeded
#include <stdio.h> #include <string.h> #define MAX_N 15 void rotate90(int r, int c, int size, int a[MAX_N][MAX_N]) { int b[MAX_N][MAX_N], i, j; for (i = 0; i < size; i++) { for (j = 0; j < size; j++) { b[r + i][c + j] = a[r + i][c + j]; } } for (i = 0; i < size; i++) { for (j = 0; j < size; j++) { b[r + i][c + j] = a[size + r - j - 1][c + i]; } } for (i = 0; i < size; i++) { for (j = 0; j < size; j++) { a[r + i][c + j] = b[r + i][c + j]; } } } void rotate(int r, int c, int size, int angle, int a[MAX_N][MAX_N]) { int i; for (i = 0; i < (angle / 90) % 4; i++) { rotate90(r, c, size, a); } } void reversal(int r, int c, int size, int a[MAX_N][MAX_N]) { int i, j; for (i = 0; i < size; i++) { for (j = 0; j < size; j++) { if (a[r + i][c + j] == 0) { a[r + i][c + j] = 1; } else { a[r + i][c + j] = 0; } } } } void left_shift(int r, int n, int a[MAX_N][MAX_N]) { int j, tmp; tmp = a[r][0]; for (j = 1; j < n; j++) { a[r][j - 1] = a[r][j]; } a[r][n - 1] = tmp; } void right_shift(int r, int n, int a[MAX_N][MAX_N]) { int j, tmp; tmp = a[r][n - 1]; for (j = n - 2; j >= 0; j--) { a[r][j + 1] = a[r][j]; } a[r][0] = tmp; } void island_reversal(int r, int c, int n, int a[MAX_N][MAX_N]) { int i, j; const int dy[4] = {-1, 0, 0, 1}, dx[4] = {0, -1, 1, 0}; int tmp = a[r][c]; a[r][c] = !a[r][c]; for (i = 0; i < 4; i++) { if (0 <= r + dy[i] && r + dy[i] < n && 0 <= c + dx[i] && c + dx[i] < n && tmp == a[r + dy[i]][c + dx[i]]) { island_reversal(r + dy[i], c + dx[i], n, a); } } } int main(void) { int a[MAX_N][MAX_N], i, j, n, m, ope, r, c, size, angle; scanf("%d%d", &n, &m); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { scanf("%d", &a[i][j]); } } for (i = 0; i < m; i++) { scanf("%d", &ope); switch (ope) { case 0: scanf("%d%d%d%d", &r, &c, &size, &angle); rotate(r - 1, c - 1, size, angle, a); break; case 1: scanf("%d%d%d", &r, &c, &size); reversal(r - 1, c - 1, size, a); break; case 2: scanf("%d", &r); left_shift(r - 1, n, a); break; case 3: scanf("%d", &r); right_shift(r - 1, n, a); break; case 4: scanf("%d%d", &r, &c); island_reversal(r - 1, c - 1, n, a); break; } } for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (j != 0) { printf(" "); } printf("%d", a[i][j]); } printf("\n"); } while (1) ; return 0; }
#include <stdio.h> #include <string.h> #define MAX_N 15 void rotate90(int r, int c, int size, int a[MAX_N][MAX_N]) { int b[MAX_N][MAX_N], i, j; for (i = 0; i < size; i++) { for (j = 0; j < size; j++) { b[r + i][c + j] = a[r + i][c + j]; } } for (i = 0; i < size; i++) { for (j = 0; j < size; j++) { b[r + i][c + j] = a[size + r - j - 1][c + i]; } } for (i = 0; i < size; i++) { for (j = 0; j < size; j++) { a[r + i][c + j] = b[r + i][c + j]; } } } void rotate(int r, int c, int size, int angle, int a[MAX_N][MAX_N]) { int i; for (i = 0; i < (angle / 90) % 4; i++) { rotate90(r, c, size, a); } } void reversal(int r, int c, int size, int a[MAX_N][MAX_N]) { int i, j; for (i = 0; i < size; i++) { for (j = 0; j < size; j++) { if (a[r + i][c + j] == 0) { a[r + i][c + j] = 1; } else { a[r + i][c + j] = 0; } } } } void left_shift(int r, int n, int a[MAX_N][MAX_N]) { int j, tmp; tmp = a[r][0]; for (j = 1; j < n; j++) { a[r][j - 1] = a[r][j]; } a[r][n - 1] = tmp; } void right_shift(int r, int n, int a[MAX_N][MAX_N]) { int j, tmp; tmp = a[r][n - 1]; for (j = n - 2; j >= 0; j--) { a[r][j + 1] = a[r][j]; } a[r][0] = tmp; } void island_reversal(int r, int c, int n, int a[MAX_N][MAX_N]) { int i, j; const int dy[4] = {-1, 0, 0, 1}, dx[4] = {0, -1, 1, 0}; int tmp = a[r][c]; a[r][c] = !a[r][c]; for (i = 0; i < 4; i++) { if (0 <= r + dy[i] && r + dy[i] < n && 0 <= c + dx[i] && c + dx[i] < n && tmp == a[r + dy[i]][c + dx[i]]) { island_reversal(r + dy[i], c + dx[i], n, a); } } } int main(void) { int a[MAX_N][MAX_N], i, j, n, m, ope, r, c, size, angle; scanf("%d%d", &n, &m); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { scanf("%d", &a[i][j]); } } for (i = 0; i < m; i++) { scanf("%d", &ope); switch (ope) { case 0: scanf("%d%d%d%d", &r, &c, &size, &angle); rotate(r - 1, c - 1, size, angle, a); break; case 1: scanf("%d%d%d", &r, &c, &size); reversal(r - 1, c - 1, size, a); break; case 2: scanf("%d", &r); left_shift(r - 1, n, a); break; case 3: scanf("%d", &r); right_shift(r - 1, n, a); break; case 4: scanf("%d%d", &r, &c); island_reversal(r - 1, c - 1, n, a); break; } } for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (j != 0) { printf(" "); } printf("%d", a[i][j]); } printf("\n"); } return 0; }
delete
125
128
125
125
TLE
p01012
C++
Time Limit Exceeded
#include "bits/stdc++.h" #include <unordered_map> #include <unordered_set> #pragma warning(disable : 4996) using namespace std; using ld = long double; const ld eps = 1e-8; //// < "d:\d_download\visual studio ///2015\projects\programing_contest_c++\debug\a.txt" > "d:\d_download\visual ///studio 2015\projects\programing_contest_c++\debug\b.txt" ld getans(ld l, ld r, int b, ld c) { if (b == 0 || abs(r - l) < eps) { return (1 - l) * (r - l); } ld aa = getans(l, (1 - c) * l + c * r, b - 1, c); ld ab = getans((1 - c) * l + c * r, r, b - 1, c); return aa + ab; } ld get() { int m, n, x; cin >> m >> n >> x; ld c = (ld(m)) / (m + n); x = min(x, 40); return getans(0, 1, x, c); } int main() { ld a = get(); ld b = get(); cout << setprecision(10) << fixed << a * b << endl; return 0; }
#include "bits/stdc++.h" #include <unordered_map> #include <unordered_set> #pragma warning(disable : 4996) using namespace std; using ld = long double; const ld eps = 1e-7; //// < "d:\d_download\visual studio ///2015\projects\programing_contest_c++\debug\a.txt" > "d:\d_download\visual ///studio 2015\projects\programing_contest_c++\debug\b.txt" ld getans(ld l, ld r, int b, ld c) { if (b == 0 || abs(r - l) < eps) { return (1 - l) * (r - l); } ld aa = getans(l, (1 - c) * l + c * r, b - 1, c); ld ab = getans((1 - c) * l + c * r, r, b - 1, c); return aa + ab; } ld get() { int m, n, x; cin >> m >> n >> x; ld c = (ld(m)) / (m + n); x = min(x, 40); return getans(0, 1, x, c); } int main() { ld a = get(); ld b = get(); cout << setprecision(10) << fixed << a * b << endl; return 0; }
replace
6
7
6
7
TLE
p01048
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define Int long long int #define REP(i, n) for (int i = 0; i < (n); i++) #define REAP(i, a, n) for (int i = (a); i < (n); i++) #define YES cout << "Yes" << endl #define NO cout << "No" << endl #define fr first #define sc second #define pb push_back #define All(v) v.begin(), v.end() int main() { int n; cin >> n; int num = 0; while (1) { num++; int cou = 0; REAP(i, 1, num + 1) { if (!num % i) cou++; } if (cou == n) { cout << num << endl; break; } } }
#include <bits/stdc++.h> using namespace std; #define Int long long int #define REP(i, n) for (int i = 0; i < (n); i++) #define REAP(i, a, n) for (int i = (a); i < (n); i++) #define YES cout << "Yes" << endl #define NO cout << "No" << endl #define fr first #define sc second #define pb push_back #define All(v) v.begin(), v.end() int main() { int n; cin >> n; int num = 0; while (1) { num++; int cou = 0; REAP(i, 1, num + 1) { if (num % i == 0) cou++; } if (cou == n) { cout << num << endl; break; } } }
replace
21
22
21
22
TLE
p01052
C++
Runtime Error
#include <iostream> #define N 101 #define INF (1e9) using namespace std; int n, a[N], b[N], ans, c; bool used[N]; int main() { cin >> n; for (int i = 0; i < n; i++) cin >> a[i] >> b[i]; for (int i = 1; i <= 31; i++) { c = INF; for (int j = 0; j < n; j++) if (!used[j] && a[j] <= i && i <= b[j] && b[j] <= b[c]) c = j; if (c != INF) used[c] = true; } for (int i = 0; i < n; i++) if (used[i]) ans++; cout << ans * 100 + (31 - ans) * 50 << endl; return 0; }
#include <iostream> #define N 101 #define INF (1e9) using namespace std; int n, a[N], b[N], ans, c; bool used[N]; int main() { cin >> n; for (int i = 0; i < n; i++) cin >> a[i] >> b[i]; for (int i = 1; i <= 31; i++) { c = INF; for (int j = 0; j < n; j++) if (!used[j] && a[j] <= i && i <= b[j]) { if (c == INF) c = j; else if (b[j] < b[c]) c = j; } if (c != INF) used[c] = true; } for (int i = 0; i < n; i++) if (used[i]) ans++; cout << ans * 100 + (31 - ans) * 50 << endl; return 0; }
replace
14
16
14
20
-11
p01057
C++
Memory Limit Exceeded
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <time.h> #include <vector> using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> P; typedef pair<int, P> P1; #define fr first #define sc second #define mp make_pair #define pb push_back #define rep(i, x) for (int i = 0; i < x; i++) #define rep1(i, x) for (int i = 1; i <= x; i++) #define rrep(i, x) for (int i = x - 1; i >= 0; i--) #define rrep1(i, x) for (int i = x; i > 0; i--) #define sor(v) sort(v.begin(), v.end()) #define rev(s) reverse(s.begin(), s.end()) #define lb(vec, a) lower_bound(vec.begin(), vec.end(), a) #define ub(vec, a) upper_bound(vec.begin(), vec.end(), a) #define uniq(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end()) #define mp1(a, b, c) P1(a, P(b, c)) const int INF = 1000000000; const int dir_4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; const int dir_8[8][2] = {{1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}}; struct node { char c; node *next, *down; int siz; }; const double p = 0.5; const int max_height = 10; node null; struct slis { int n; node head; void show() { node *h[max_height]; h[max_height - 1] = &head; rrep(i, max_height - 1) h[i] = (*h[i + 1]).down; rrep(i, max_height) { printf("%d:", i); while (h[i] != &null) { printf("%c,%d ", h[i]->c, h[i]->siz); h[i] = h[i]->next; } cout << endl; } } void show_() { node *id = &head; while (id->down != &null) id = id->down; while (id->next != &null) { id = id->next; printf("%c", id->c); } puts(""); } }; node *init() { // 0??????????????? node *h = new node; node *head[max_height]; head[max_height - 1] = h; rrep1(i, max_height - 1) { head[i]->c = '$'; head[i]->down = new node; head[i - 1] = head[i]->down; } head[0]->c = '$'; head[0]->down = &null; return h; } slis *mkslis(int n, char *c, slis *ret) { //???????´??????????????±?????????? vector<int> height; rep(i, n) { int h = 1; while (h < max_height && rand() % 100 < p * 100.0) h++; height.pb(h); } // 0??????????????? node *head[max_height]; head[max_height - 1] = init(); rrep1(i, max_height - 1) head[i - 1] = head[i]->down; /*node* h = new node; node* head[max_height]; head[max_height-1] = h; rrep1(i,max_height-1){ head[i]->c = '$'; head[i]->down = new node; head[i-1] = head[i]->down; } head[0]->c = '$'; head[0]->down = &null;*/ //??¬???????????? vector<node *> down; rep(i, n) down.pb(&null); rep(i, max_height) { node *id = head[i]; int loc = 0; rep(j, n) { if (i < height[j]) { (*id).next = new node; (*id).siz = j + 1 - loc; loc = j + 1; id = (*id).next; (*id).c = *(c + j); (*id).down = down[j]; down[j] = id; } } (*id).next = &null; (*id).siz = n - loc; } (*ret).n = n; (*ret).head = *head[max_height - 1]; return ret; } pair<slis *, slis *> split(slis *a, slis *b, int k) { // b???0??????????????? node *head[max_height]; head[max_height - 1] = init(); rrep1(i, max_height - 1) head[i - 1] = head[i]->down; // split node *id = &a->head; int loc = 0; int h = max_height - 1; while (1) { while (loc + id->siz <= k) { loc += id->siz; id = id->next; } head[h]->next = id->next; head[h]->siz = loc + id->siz - k; id->next = &null; id->siz = k - loc; if (id->down == &null) break; id = id->down; h--; } //?????? b->n = a->n - k; a->n = k; b->head = *head[max_height - 1]; return pair<slis *, slis *>(a, b); } slis *merge(slis *a, slis *b) { node *id0 = &a->head; node *id1 = &b->head; while (id0 != &null) { while (id0->next != &null) id0 = id0->next; id0->next = id1->next; id0->siz += id1->siz; id0 = id0->down; id1 = id1->down; } a->n += b->n; return a; } void updata(slis *lis, int k, char c) { node *id = &lis->head; int loc = 0; while (id != &null) { while (id->next != &null && loc + id->siz <= k) { loc += id->siz; id = id->next; } if (loc == k) id->c = c; id = id->down; } } int main() { static int n, q; static char c[2000010]; static slis *s[100010]; scanf("%d%d", &n, &q); rep(i, n) { scanf("%s", &c); int siz = 0; while (c[siz] != '\0') siz++; s[i] = mkslis(siz, c, new slis); rep(j, siz) c[j] = '\0'; } rep(i, q) { int t; scanf("%d", &t); if (t == 1) { int a, b, c, d; scanf("%d%d%d%d", &a, &b, &c, &d); pair<slis *, slis *> p = split(s[a - 1], new slis, b - 1); pair<slis *, slis *> q = split(s[c - 1], new slis, d - 1); s[a - 1] = merge(p.fr, q.sc); s[c - 1] = merge(q.fr, p.sc); } else { int a, b; char c_; scanf("%d%d %c", &a, &b, &c_); updata(s[a - 1], b, c_); } } rep(i, n) { s[i]->show_(); } /*int n; char c[30]; scanf("%d %s",&n,&c); slis *test = new slis; test = mkslis(n,c,test); test->show(); int k; scanf("%d",&k); pair<slis*,slis*> p = split(test,new slis,k); p.fr->show(); p.sc->show(); test = merge(p.fr,p.sc); test->show();*/ }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <time.h> #include <vector> using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> P; typedef pair<int, P> P1; #define fr first #define sc second #define mp make_pair #define pb push_back #define rep(i, x) for (int i = 0; i < x; i++) #define rep1(i, x) for (int i = 1; i <= x; i++) #define rrep(i, x) for (int i = x - 1; i >= 0; i--) #define rrep1(i, x) for (int i = x; i > 0; i--) #define sor(v) sort(v.begin(), v.end()) #define rev(s) reverse(s.begin(), s.end()) #define lb(vec, a) lower_bound(vec.begin(), vec.end(), a) #define ub(vec, a) upper_bound(vec.begin(), vec.end(), a) #define uniq(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end()) #define mp1(a, b, c) P1(a, P(b, c)) const int INF = 1000000000; const int dir_4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; const int dir_8[8][2] = {{1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}}; struct node { char c; node *next, *down; int siz; }; const double p = 0.25; const int max_height = 8; node null; struct slis { int n; node head; void show() { node *h[max_height]; h[max_height - 1] = &head; rrep(i, max_height - 1) h[i] = (*h[i + 1]).down; rrep(i, max_height) { printf("%d:", i); while (h[i] != &null) { printf("%c,%d ", h[i]->c, h[i]->siz); h[i] = h[i]->next; } cout << endl; } } void show_() { node *id = &head; while (id->down != &null) id = id->down; while (id->next != &null) { id = id->next; printf("%c", id->c); } puts(""); } }; node *init() { // 0??????????????? node *h = new node; node *head[max_height]; head[max_height - 1] = h; rrep1(i, max_height - 1) { head[i]->c = '$'; head[i]->down = new node; head[i - 1] = head[i]->down; } head[0]->c = '$'; head[0]->down = &null; return h; } slis *mkslis(int n, char *c, slis *ret) { //???????´??????????????±?????????? vector<int> height; rep(i, n) { int h = 1; while (h < max_height && rand() % 100 < p * 100.0) h++; height.pb(h); } // 0??????????????? node *head[max_height]; head[max_height - 1] = init(); rrep1(i, max_height - 1) head[i - 1] = head[i]->down; /*node* h = new node; node* head[max_height]; head[max_height-1] = h; rrep1(i,max_height-1){ head[i]->c = '$'; head[i]->down = new node; head[i-1] = head[i]->down; } head[0]->c = '$'; head[0]->down = &null;*/ //??¬???????????? vector<node *> down; rep(i, n) down.pb(&null); rep(i, max_height) { node *id = head[i]; int loc = 0; rep(j, n) { if (i < height[j]) { (*id).next = new node; (*id).siz = j + 1 - loc; loc = j + 1; id = (*id).next; (*id).c = *(c + j); (*id).down = down[j]; down[j] = id; } } (*id).next = &null; (*id).siz = n - loc; } (*ret).n = n; (*ret).head = *head[max_height - 1]; return ret; } pair<slis *, slis *> split(slis *a, slis *b, int k) { // b???0??????????????? node *head[max_height]; head[max_height - 1] = init(); rrep1(i, max_height - 1) head[i - 1] = head[i]->down; // split node *id = &a->head; int loc = 0; int h = max_height - 1; while (1) { while (loc + id->siz <= k) { loc += id->siz; id = id->next; } head[h]->next = id->next; head[h]->siz = loc + id->siz - k; id->next = &null; id->siz = k - loc; if (id->down == &null) break; id = id->down; h--; } //?????? b->n = a->n - k; a->n = k; b->head = *head[max_height - 1]; return pair<slis *, slis *>(a, b); } slis *merge(slis *a, slis *b) { node *id0 = &a->head; node *id1 = &b->head; while (id0 != &null) { while (id0->next != &null) id0 = id0->next; id0->next = id1->next; id0->siz += id1->siz; id0 = id0->down; id1 = id1->down; } a->n += b->n; return a; } void updata(slis *lis, int k, char c) { node *id = &lis->head; int loc = 0; while (id != &null) { while (id->next != &null && loc + id->siz <= k) { loc += id->siz; id = id->next; } if (loc == k) id->c = c; id = id->down; } } int main() { static int n, q; static char c[2000010]; static slis *s[100010]; scanf("%d%d", &n, &q); rep(i, n) { scanf("%s", &c); int siz = 0; while (c[siz] != '\0') siz++; s[i] = mkslis(siz, c, new slis); rep(j, siz) c[j] = '\0'; } rep(i, q) { int t; scanf("%d", &t); if (t == 1) { int a, b, c, d; scanf("%d%d%d%d", &a, &b, &c, &d); pair<slis *, slis *> p = split(s[a - 1], new slis, b - 1); pair<slis *, slis *> q = split(s[c - 1], new slis, d - 1); s[a - 1] = merge(p.fr, q.sc); s[c - 1] = merge(q.fr, p.sc); } else { int a, b; char c_; scanf("%d%d %c", &a, &b, &c_); updata(s[a - 1], b, c_); } } rep(i, n) { s[i]->show_(); } /*int n; char c[30]; scanf("%d %s",&n,&c); slis *test = new slis; test = mkslis(n,c,test); test->show(); int k; scanf("%d",&k); pair<slis*,slis*> p = split(test,new slis,k); p.fr->show(); p.sc->show(); test = merge(p.fr,p.sc); test->show();*/ }
replace
45
47
45
47
MLE
p01057
C++
Runtime Error
#include <algorithm> #include <array> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <unordered_map> #include <unordered_set> #include <valarray> #include <vector> using namespace std; typedef long long ll; struct NTree { // syakyou simasita // demo TLE sisou... struct Node; typedef char D; typedef Node *NP; static Node last_d; static NP last; struct Node { NP l, r; int sz; D v; Node(D v) : l(last), r(last), sz(1), v(v) {} Node() : l(nullptr), r(nullptr), sz(0) {} NP update() { sz = 1 + l->sz + r->sz; return this; } void set(int a, D x) { if (a < l->sz) { l->set(a, x); } else if (a == l->sz) { v = x; } else { r->set(a - (l->sz + 1), x); } } void allget(D d[]) { if (!sz) return; l->allget(d); d[l->sz] = v; r->allget(d + l->sz + 1); } } *n; static uint xor128() { static uint x = 123456789, y = 362436039, z = 525188629, w = 88675123; uint t; t = (x ^ (x << 11)); x = y; y = z; z = w; return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8))); } static NP merge(NP l, NP r) { if (!l->sz) return r; if (!r->sz) return l; if ((int)(xor128() % (l->sz + r->sz)) < l->sz) { l->r = merge(l->r, r); return l->update(); } else { r->l = merge(l, r->l); return r->update(); } } static pair<NP, NP> split(NP x, int k) { if (!x->sz) return {last, last}; if (k <= x->l->sz) { auto y = split(x->l, k); x->l = y.second; y.second = x->update(); return y; } else { auto y = split(x->r, k - x->l->sz - 1); x->r = y.first; y.first = x->update(); return y; } } static NP built(int sz, D d[]) { if (!sz) return last; int md = sz / 2; NP x = new Node(d[md]); x->l = built(md, d); x->r = built(sz - (md + 1), d + (md + 1)); return x->update(); } NTree() : n(last) {} NTree(NP n) : n(n) {} NTree(D d) : n(new Node(d)) {} NTree(int sz, D d[]) { n = built(sz, d); } void merge(NTree r) { n = merge(n, r.n); } NTree split(int k) { auto u = split(n, k); n = u.first; return NTree(u.second); } void set(int a, D x) { n->set(a, x); } void allget(D d[]) { n->allget(d); d[n->sz] = '\0'; } }; NTree::Node NTree::last_d = NTree::Node(); NTree::NP NTree::last = &last_d; const int MN = 100100; const int MS = 1000100; NTree tr[MN]; char buf[MS]; void deb(NTree t) { t.allget(buf); printf("d %s\n", buf); } int main() { int n, q; scanf("%d %d", &n, &q); for (int i = 0; i < n; i++) { scanf(" %s", buf); tr[i] = NTree(strlen(buf), buf); } for (int i = 0; i < q; i++) { int ty; scanf("%d", &ty); // printf("quey %d %d\n", i, ty); if (ty == 1) { int a, b, c, d; scanf("%d %d %d %d", &a, &b, &c, &d); a--; b--; c--; d--; // printf("%d %d %d %d\n", a, b, c, d); // deb(tr[a]); // deb(tr[c]); auto t1 = tr[a].split(b); auto t2 = tr[c].split(d); // deb(t1); // deb(t2); tr[a].merge(t2); tr[c].merge(t1); // deb(tr[a]); // deb(tr[c]); } else { int a, b; char c; scanf("%d %d %c", &a, &b, &c); a--; b--; // printf("type 2 %d %d %d\n", a, b, tr[a].n->sz); tr[a].set(b, c); } for (int i = 0; i < n; i++) { // tr[i].allget(buf); // printf("%s\n", buf); } // printf("\n"); // printf("\n"); } for (int i = 0; i < n; i++) { tr[i].allget(buf); printf("%s\n", buf); } return 0; }
#include <algorithm> #include <array> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <unordered_map> #include <unordered_set> #include <valarray> #include <vector> using namespace std; typedef long long ll; struct NTree { // syakyou simasita // demo TLE sisou... struct Node; typedef char D; typedef Node *NP; static Node last_d; static NP last; struct Node { NP l, r; int sz; D v; Node(D v) : l(last), r(last), sz(1), v(v) {} Node() : l(nullptr), r(nullptr), sz(0) {} NP update() { sz = 1 + l->sz + r->sz; return this; } void set(int a, D x) { if (a < l->sz) { l->set(a, x); } else if (a == l->sz) { v = x; } else { r->set(a - (l->sz + 1), x); } } void allget(D d[]) { if (!sz) return; l->allget(d); d[l->sz] = v; r->allget(d + l->sz + 1); } } *n; static uint xor128() { static uint x = 123456789, y = 362436039, z = 525188629, w = 88675123; uint t; t = (x ^ (x << 11)); x = y; y = z; z = w; return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8))); } static NP merge(NP l, NP r) { if (!l->sz) return r; if (!r->sz) return l; if ((int)(xor128() % (l->sz + r->sz)) < l->sz) { l->r = merge(l->r, r); return l->update(); } else { r->l = merge(l, r->l); return r->update(); } } static pair<NP, NP> split(NP x, int k) { if (!x->sz) return {last, last}; if (k <= x->l->sz) { auto y = split(x->l, k); x->l = y.second; y.second = x->update(); return y; } else { auto y = split(x->r, k - x->l->sz - 1); x->r = y.first; y.first = x->update(); return y; } } static NP built(int sz, D d[]) { if (!sz) return last; int md = sz / 2; NP x = new Node(d[md]); x->l = built(md, d); x->r = built(sz - (md + 1), d + (md + 1)); return x->update(); } NTree() : n(last) {} NTree(NP n) : n(n) {} NTree(D d) : n(new Node(d)) {} NTree(int sz, D d[]) { n = built(sz, d); } void merge(NTree r) { n = merge(n, r.n); } NTree split(int k) { auto u = split(n, k); n = u.first; return NTree(u.second); } void set(int a, D x) { n->set(a, x); } void allget(D d[]) { n->allget(d); d[n->sz] = '\0'; } }; NTree::Node NTree::last_d = NTree::Node(); NTree::NP NTree::last = &last_d; const int MN = 100100; const int MS = 2000100; NTree tr[MN]; char buf[MS]; void deb(NTree t) { t.allget(buf); printf("d %s\n", buf); } int main() { int n, q; scanf("%d %d", &n, &q); for (int i = 0; i < n; i++) { scanf(" %s", buf); tr[i] = NTree(strlen(buf), buf); } for (int i = 0; i < q; i++) { int ty; scanf("%d", &ty); // printf("quey %d %d\n", i, ty); if (ty == 1) { int a, b, c, d; scanf("%d %d %d %d", &a, &b, &c, &d); a--; b--; c--; d--; // printf("%d %d %d %d\n", a, b, c, d); // deb(tr[a]); // deb(tr[c]); auto t1 = tr[a].split(b); auto t2 = tr[c].split(d); // deb(t1); // deb(t2); tr[a].merge(t2); tr[c].merge(t1); // deb(tr[a]); // deb(tr[c]); } else { int a, b; char c; scanf("%d %d %c", &a, &b, &c); a--; b--; // printf("type 2 %d %d %d\n", a, b, tr[a].n->sz); tr[a].set(b, c); } for (int i = 0; i < n; i++) { // tr[i].allget(buf); // printf("%s\n", buf); } // printf("\n"); // printf("\n"); } for (int i = 0; i < n; i++) { tr[i].allget(buf); printf("%s\n", buf); } return 0; }
replace
128
129
128
129
0
p01057
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; namespace RBST { // root = insert(root,k,hoge) // ???????????????????????????????????¨????????§?????? struct T { T *l, *r; int size; char v; T() { l = r = NULL; size = 1; } T(T *a) { *this = *a; } T(char v) : v(v) { l = r = NULL; size = 1; } }; int stack_size; T *my_new(T *a) { return new T(a); } T *my_new(int v) { return new T(v); } class BST { public: static int size(T *c) { if (!c) return 0; else return c->size; } static T *update(T *c) { if (!c) return 0; c->size = size(c->l) + size(c->r) + 1; return c; } static T *merge(T *a, T *b, int perm = 0) { if (!a) return b; if (!b) return a; if (rand() % (size(a) + size(b)) < size(a)) { T *R = perm ? my_new(a) : a; R->r = merge(a->r, b, perm); return update(R); } else { T *R = perm ? my_new(b) : b; R->l = merge(a, b->l, perm); return update(R); } } // [0,k) [k,n) static pair<T *, T *> split(T *c, int k, int perm = 0) { if (!c) return {0, 0}; T *R = perm ? my_new(c) : c; if (k <= size(c->l)) { pair<T *, T *> s = split(c->l, k, perm); R->l = s.second; return make_pair(s.first, update(R)); } else { pair<T *, T *> s = split(c->r, k - size(c->l) - 1, perm); R->r = s.first; return make_pair(update(R), s.second); } } static T *insert(T *c, int k, T *newnode, int perm = 0) { // cout << s.first << " " << s.second << endl; pair<T *, T *> s = split(c, k, perm); return merge(merge(s.first, newnode, perm), s.second, perm); } static T *erase(T *c, int k, int perm = 0) { pair<T *, T *> s1 = split(c, k, perm); pair<T *, T *> s2 = split(s1.second, 1, perm); return merge(s1.first, s2.second, perm); } // ?????????NULL static T *newTree() { return 0; } static void view(T *c) { if (!c) return; view(c->l); printf("%c", c->v); view(c->r); } }; }; // namespace RBST using namespace RBST; char str[100010]; T *root[100010]; int main() { int N, Q; cin >> N >> Q; for (int i = 0; i < N; i++) { scanf("%s", str); int j = 0; while (str[j]) { root[i] = BST::insert(root[i], j, new T(str[j])); j++; } } for (int i = 0; i < Q; i++) { int t; scanf("%d", &t); if (t == 1) { int a, b, c, d; scanf("%d%d%d%d", &a, &b, &c, &d); --a; --b; --c; --d; auto s1 = BST::split(root[a], b); auto s2 = BST::split(root[c], d); root[c] = BST::merge(s2.first, s1.second); root[a] = BST::merge(s1.first, s2.second); } else { int a, b; char c[2]; scanf("%d%d%s", &a, &b, c); --a; --b; root[a] = BST::erase(root[a], b); // BST::view(root[a]);puts(""); // cout << "ins" << " " << b << " " << c << endl; root[a] = BST::insert(root[a], b, new T(c[0])); // BST::view(root[a]);puts(""); } } for (int i = 0; i < N; i++) { BST::view(root[i]); puts(""); } }
#include <bits/stdc++.h> using namespace std; namespace RBST { // root = insert(root,k,hoge) // ???????????????????????????????????¨????????§?????? struct T { T *l, *r; int size; char v; T() { l = r = NULL; size = 1; } T(T *a) { *this = *a; } T(char v) : v(v) { l = r = NULL; size = 1; } }; int stack_size; T *my_new(T *a) { return new T(a); } T *my_new(int v) { return new T(v); } class BST { public: static int size(T *c) { if (!c) return 0; else return c->size; } static T *update(T *c) { if (!c) return 0; c->size = size(c->l) + size(c->r) + 1; return c; } static T *merge(T *a, T *b, int perm = 0) { if (!a) return b; if (!b) return a; if (rand() % (size(a) + size(b)) < size(a)) { T *R = perm ? my_new(a) : a; R->r = merge(a->r, b, perm); return update(R); } else { T *R = perm ? my_new(b) : b; R->l = merge(a, b->l, perm); return update(R); } } // [0,k) [k,n) static pair<T *, T *> split(T *c, int k, int perm = 0) { if (!c) return {0, 0}; T *R = perm ? my_new(c) : c; if (k <= size(c->l)) { pair<T *, T *> s = split(c->l, k, perm); R->l = s.second; return make_pair(s.first, update(R)); } else { pair<T *, T *> s = split(c->r, k - size(c->l) - 1, perm); R->r = s.first; return make_pair(update(R), s.second); } } static T *insert(T *c, int k, T *newnode, int perm = 0) { // cout << s.first << " " << s.second << endl; pair<T *, T *> s = split(c, k, perm); return merge(merge(s.first, newnode, perm), s.second, perm); } static T *erase(T *c, int k, int perm = 0) { pair<T *, T *> s1 = split(c, k, perm); pair<T *, T *> s2 = split(s1.second, 1, perm); return merge(s1.first, s2.second, perm); } // ?????????NULL static T *newTree() { return 0; } static void view(T *c) { if (!c) return; view(c->l); printf("%c", c->v); view(c->r); } }; }; // namespace RBST using namespace RBST; char str[2000010]; T *root[100010]; int main() { int N, Q; cin >> N >> Q; for (int i = 0; i < N; i++) { scanf("%s", str); int j = 0; while (str[j]) { root[i] = BST::insert(root[i], j, new T(str[j])); j++; } } for (int i = 0; i < Q; i++) { int t; scanf("%d", &t); if (t == 1) { int a, b, c, d; scanf("%d%d%d%d", &a, &b, &c, &d); --a; --b; --c; --d; auto s1 = BST::split(root[a], b); auto s2 = BST::split(root[c], d); root[c] = BST::merge(s2.first, s1.second); root[a] = BST::merge(s1.first, s2.second); } else { int a, b; char c[2]; scanf("%d%d%s", &a, &b, c); --a; --b; root[a] = BST::erase(root[a], b); // BST::view(root[a]);puts(""); // cout << "ins" << " " << b << " " << c << endl; root[a] = BST::insert(root[a], b, new T(c[0])); // BST::view(root[a]);puts(""); } } for (int i = 0; i < N; i++) { BST::view(root[i]); puts(""); } }
replace
94
95
94
95
0
p01057
C++
Memory Limit Exceeded
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <time.h> #include <vector> using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> P; typedef pair<int, P> P1; #define fr first #define sc second #define mp make_pair #define pb push_back #define rep(i, x) for (int i = 0; i < x; i++) #define rep1(i, x) for (int i = 1; i <= x; i++) #define rrep(i, x) for (int i = x - 1; i >= 0; i--) #define rrep1(i, x) for (int i = x; i > 0; i--) #define sor(v) sort(v.begin(), v.end()) #define rev(s) reverse(s.begin(), s.end()) #define lb(vec, a) lower_bound(vec.begin(), vec.end(), a) #define ub(vec, a) upper_bound(vec.begin(), vec.end(), a) #define uniq(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end()) #define mp1(a, b, c) P1(a, P(b, c)) const int INF = 1000000000; const int dir_4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; const int dir_8[8][2] = {{1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}}; struct node { char c; node *next, *down; int siz; }; const double p = 0.37; const int max_height = 12; node null; struct slis { int n; node head; void show() { node *h[max_height]; h[max_height - 1] = &head; rrep(i, max_height - 1) h[i] = (*h[i + 1]).down; rrep(i, max_height) { printf("%d:", i); while (h[i] != &null) { printf("%c,%d ", h[i]->c, h[i]->siz); h[i] = h[i]->next; } cout << endl; } } void show_() { node *id = &head; while (id->down != &null) id = id->down; while (id->next != &null) { id = id->next; printf("%c", id->c); } puts(""); } }; node *init() { // 0??????????????? node *h = new node; node *head[max_height]; head[max_height - 1] = h; rrep1(i, max_height - 1) { head[i]->c = '$'; head[i]->down = new node; head[i - 1] = head[i]->down; } head[0]->c = '$'; head[0]->down = &null; return h; } slis *mkslis(int n, char *c, slis *ret) { //???????´??????????????±?????????? vector<int> height; rep(i, n) { int h = 1; while (h < max_height && rand() % 100 < p * 100.0) h++; height.pb(h); } // 0??????????????? node *head[max_height]; head[max_height - 1] = init(); rrep1(i, max_height - 1) head[i - 1] = head[i]->down; /*node* h = new node; node* head[max_height]; head[max_height-1] = h; rrep1(i,max_height-1){ head[i]->c = '$'; head[i]->down = new node; head[i-1] = head[i]->down; } head[0]->c = '$'; head[0]->down = &null;*/ //??¬???????????? vector<node *> down; rep(i, n) down.pb(&null); rep(i, max_height) { node *id = head[i]; int loc = 0; rep(j, n) { if (i < height[j]) { (*id).next = new node; (*id).siz = j + 1 - loc; loc = j + 1; id = (*id).next; (*id).c = *(c + j); (*id).down = down[j]; down[j] = id; } } (*id).next = &null; (*id).siz = n - loc; } (*ret).n = n; (*ret).head = *head[max_height - 1]; return ret; } pair<slis *, slis *> split(slis *a, slis *b, int k) { // b???0??????????????? node *head[max_height]; head[max_height - 1] = init(); rrep1(i, max_height - 1) head[i - 1] = head[i]->down; // split node *id = &a->head; int loc = 0; int h = max_height - 1; while (1) { while (loc + id->siz <= k) { loc += id->siz; id = id->next; } head[h]->next = id->next; head[h]->siz = loc + id->siz - k; id->next = &null; id->siz = k - loc; if (id->down == &null) break; id = id->down; h--; } //?????? b->n = a->n - k; a->n = k; b->head = *head[max_height - 1]; return pair<slis *, slis *>(a, b); } slis *merge(slis *a, slis *b) { node *id0 = &a->head; node *id1 = &b->head; while (id0 != &null) { while (id0->next != &null) id0 = id0->next; id0->next = id1->next; id0->siz += id1->siz; id0 = id0->down; id1 = id1->down; } a->n += b->n; return a; } void updata(slis *lis, int k, char c) { node *id = &lis->head; int loc = 0; while (id != &null) { while (id->next != &null && loc + id->siz <= k) { loc += id->siz; id = id->next; } if (loc == k) id->c = c; id = id->down; } } int main() { static int n, q; static char c[2000010]; static slis *s[100010]; scanf("%d%d", &n, &q); rep(i, n) { scanf("%s", &c); int siz = 0; while (c[siz] != '\0') siz++; s[i] = mkslis(siz, c, new slis); rep(j, siz) c[j] = '\0'; } rep(i, q) { int t; scanf("%d", &t); if (t == 1) { int a, b, c, d; scanf("%d%d%d%d", &a, &b, &c, &d); pair<slis *, slis *> p = split(s[a - 1], new slis, b - 1); pair<slis *, slis *> q = split(s[c - 1], new slis, d - 1); s[a - 1] = merge(p.fr, q.sc); s[c - 1] = merge(q.fr, p.sc); } else { int a, b; char c_; scanf("%d%d %c", &a, &b, &c_); updata(s[a - 1], b, c_); } } rep(i, n) { s[i]->show_(); } /*int n; char c[30]; scanf("%d %s",&n,&c); slis *test = new slis; test = mkslis(n,c,test); test->show(); int k; scanf("%d",&k); pair<slis*,slis*> p = split(test,new slis,k); p.fr->show(); p.sc->show(); test = merge(p.fr,p.sc); test->show();*/ }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <time.h> #include <vector> using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> P; typedef pair<int, P> P1; #define fr first #define sc second #define mp make_pair #define pb push_back #define rep(i, x) for (int i = 0; i < x; i++) #define rep1(i, x) for (int i = 1; i <= x; i++) #define rrep(i, x) for (int i = x - 1; i >= 0; i--) #define rrep1(i, x) for (int i = x; i > 0; i--) #define sor(v) sort(v.begin(), v.end()) #define rev(s) reverse(s.begin(), s.end()) #define lb(vec, a) lower_bound(vec.begin(), vec.end(), a) #define ub(vec, a) upper_bound(vec.begin(), vec.end(), a) #define uniq(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end()) #define mp1(a, b, c) P1(a, P(b, c)) const int INF = 1000000000; const int dir_4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; const int dir_8[8][2] = {{1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}}; struct node { char c; node *next, *down; int siz; }; const double p = 0.30; const int max_height = 10; node null; struct slis { int n; node head; void show() { node *h[max_height]; h[max_height - 1] = &head; rrep(i, max_height - 1) h[i] = (*h[i + 1]).down; rrep(i, max_height) { printf("%d:", i); while (h[i] != &null) { printf("%c,%d ", h[i]->c, h[i]->siz); h[i] = h[i]->next; } cout << endl; } } void show_() { node *id = &head; while (id->down != &null) id = id->down; while (id->next != &null) { id = id->next; printf("%c", id->c); } puts(""); } }; node *init() { // 0??????????????? node *h = new node; node *head[max_height]; head[max_height - 1] = h; rrep1(i, max_height - 1) { head[i]->c = '$'; head[i]->down = new node; head[i - 1] = head[i]->down; } head[0]->c = '$'; head[0]->down = &null; return h; } slis *mkslis(int n, char *c, slis *ret) { //???????´??????????????±?????????? vector<int> height; rep(i, n) { int h = 1; while (h < max_height && rand() % 100 < p * 100.0) h++; height.pb(h); } // 0??????????????? node *head[max_height]; head[max_height - 1] = init(); rrep1(i, max_height - 1) head[i - 1] = head[i]->down; /*node* h = new node; node* head[max_height]; head[max_height-1] = h; rrep1(i,max_height-1){ head[i]->c = '$'; head[i]->down = new node; head[i-1] = head[i]->down; } head[0]->c = '$'; head[0]->down = &null;*/ //??¬???????????? vector<node *> down; rep(i, n) down.pb(&null); rep(i, max_height) { node *id = head[i]; int loc = 0; rep(j, n) { if (i < height[j]) { (*id).next = new node; (*id).siz = j + 1 - loc; loc = j + 1; id = (*id).next; (*id).c = *(c + j); (*id).down = down[j]; down[j] = id; } } (*id).next = &null; (*id).siz = n - loc; } (*ret).n = n; (*ret).head = *head[max_height - 1]; return ret; } pair<slis *, slis *> split(slis *a, slis *b, int k) { // b???0??????????????? node *head[max_height]; head[max_height - 1] = init(); rrep1(i, max_height - 1) head[i - 1] = head[i]->down; // split node *id = &a->head; int loc = 0; int h = max_height - 1; while (1) { while (loc + id->siz <= k) { loc += id->siz; id = id->next; } head[h]->next = id->next; head[h]->siz = loc + id->siz - k; id->next = &null; id->siz = k - loc; if (id->down == &null) break; id = id->down; h--; } //?????? b->n = a->n - k; a->n = k; b->head = *head[max_height - 1]; return pair<slis *, slis *>(a, b); } slis *merge(slis *a, slis *b) { node *id0 = &a->head; node *id1 = &b->head; while (id0 != &null) { while (id0->next != &null) id0 = id0->next; id0->next = id1->next; id0->siz += id1->siz; id0 = id0->down; id1 = id1->down; } a->n += b->n; return a; } void updata(slis *lis, int k, char c) { node *id = &lis->head; int loc = 0; while (id != &null) { while (id->next != &null && loc + id->siz <= k) { loc += id->siz; id = id->next; } if (loc == k) id->c = c; id = id->down; } } int main() { static int n, q; static char c[2000010]; static slis *s[100010]; scanf("%d%d", &n, &q); rep(i, n) { scanf("%s", &c); int siz = 0; while (c[siz] != '\0') siz++; s[i] = mkslis(siz, c, new slis); rep(j, siz) c[j] = '\0'; } rep(i, q) { int t; scanf("%d", &t); if (t == 1) { int a, b, c, d; scanf("%d%d%d%d", &a, &b, &c, &d); pair<slis *, slis *> p = split(s[a - 1], new slis, b - 1); pair<slis *, slis *> q = split(s[c - 1], new slis, d - 1); s[a - 1] = merge(p.fr, q.sc); s[c - 1] = merge(q.fr, p.sc); } else { int a, b; char c_; scanf("%d%d %c", &a, &b, &c_); updata(s[a - 1], b, c_); } } rep(i, n) { s[i]->show_(); } /*int n; char c[30]; scanf("%d %s",&n,&c); slis *test = new slis; test = mkslis(n,c,test); test->show(); int k; scanf("%d",&k); pair<slis*,slis*> p = split(test,new slis,k); p.fr->show(); p.sc->show(); test = merge(p.fr,p.sc); test->show();*/ }
replace
45
47
45
47
MLE
p01057
C++
Runtime Error
#include <bits/stdc++.h> #include <ext/rope> #define REP(x, y, z) for (int x = y; x <= z; x++) #define FORD(x, y, z) for (int x = y; x >= z; x--) #define MSET(x, y) memset(x, y, sizeof(x)) #define FOR(x, y) for (__typeof(y.begin()) x = y.begin(); x != y.end(); x++) #define F first #define S second #define MP make_pair #define PB push_back #define SZ size() #define M 200005 void RI() {} template <typename... T> void RI(int &head, T &...tail) { scanf("%d", &head); RI(tail...); } using namespace std; using namespace __gnu_cxx; typedef long long LL; int n, q; rope<char> in[M], t1, t2; char buf[M * 2]; int main() { int a, b, c, d; int z; RI(n, q); REP(i, 1, n) { scanf("%s", buf); in[i] = buf; } while (q--) { RI(z); if (z == 1) { RI(a, b, c, d); t1 = in[a].substr(b - 1, in[a].length() - b + 1); t2 = in[c].substr(d - 1, in[c].length() - d + 1); in[a] = in[a].substr(0, b - 1) + t2; in[c] = in[c].substr(0, d - 1) + t1; } else { RI(a, b); scanf("%s", buf); t1 = buf; int len = in[a].length(); in[a] = in[a].substr(0, b - 1) + t1 + in[a].substr(b, len - b); } } REP(i, 1, n) printf("%s\n", in[i].c_str()); return 0; }
#include <bits/stdc++.h> #include <ext/rope> #define REP(x, y, z) for (int x = y; x <= z; x++) #define FORD(x, y, z) for (int x = y; x >= z; x--) #define MSET(x, y) memset(x, y, sizeof(x)) #define FOR(x, y) for (__typeof(y.begin()) x = y.begin(); x != y.end(); x++) #define F first #define S second #define MP make_pair #define PB push_back #define SZ size() #define M 500005 void RI() {} template <typename... T> void RI(int &head, T &...tail) { scanf("%d", &head); RI(tail...); } using namespace std; using namespace __gnu_cxx; typedef long long LL; int n, q; rope<char> in[M], t1, t2; char buf[M * 2]; int main() { int a, b, c, d; int z; RI(n, q); REP(i, 1, n) { scanf("%s", buf); in[i] = buf; } while (q--) { RI(z); if (z == 1) { RI(a, b, c, d); t1 = in[a].substr(b - 1, in[a].length() - b + 1); t2 = in[c].substr(d - 1, in[c].length() - d + 1); in[a] = in[a].substr(0, b - 1) + t2; in[c] = in[c].substr(0, d - 1) + t1; } else { RI(a, b); scanf("%s", buf); t1 = buf; int len = in[a].length(); in[a] = in[a].substr(0, b - 1) + t1 + in[a].substr(b, len - b); } } REP(i, 1, n) printf("%s\n", in[i].c_str()); return 0; }
replace
11
12
11
12
0
p01061
C++
Runtime Error
#include <algorithm> #include <fstream> #include <iomanip> #include <iostream> #include <vector> using namespace std; #define ALL(c) (c).begin(), (c).end() #define REP(i, n) for (ll i = 0; i < (n); ++i) using ll = long long; using vl = vector<ll>; struct union_find { vl v; union_find(ll n) : v(n, -1) {} ll find(ll x) { return (v[x] < 0) ? x : (v[x] = find(v[x])); } void unite(ll x, ll y) { x = find(x); y = find(y); if (x == y) return; if (-v[x] < -v[y]) swap(x, y); v[x] += v[y]; v[y] = x; } bool same(ll x, ll y) { return find(x) == find(y); } ll size(ll x) { return -v[find(x)]; } }; int main() { #ifdef _WIN32 ifstream cin("sample.in"); ofstream cout("sample.out"); #endif cout << fixed << setprecision(8); ll n, m; cin >> n >> m; vl a(n), b(n); REP(i, m) cin >> a[i] >> b[i]; union_find uf(n); REP(i, m) uf.unite(a[i] - 1, b[i] - 1); ll cntm = 0; ll cnts = 0; REP(i, n) { if (uf.size(i) == 1) { cntm++; } else { if (uf.find(i) == i) { cnts++; } } } cout << abs(cntm - cnts) << endl; }
#include <algorithm> #include <fstream> #include <iomanip> #include <iostream> #include <vector> using namespace std; #define ALL(c) (c).begin(), (c).end() #define REP(i, n) for (ll i = 0; i < (n); ++i) using ll = long long; using vl = vector<ll>; struct union_find { vl v; union_find(ll n) : v(n, -1) {} ll find(ll x) { return (v[x] < 0) ? x : (v[x] = find(v[x])); } void unite(ll x, ll y) { x = find(x); y = find(y); if (x == y) return; if (-v[x] < -v[y]) swap(x, y); v[x] += v[y]; v[y] = x; } bool same(ll x, ll y) { return find(x) == find(y); } ll size(ll x) { return -v[find(x)]; } }; int main() { #ifdef _WIN32 ifstream cin("sample.in"); ofstream cout("sample.out"); #endif cout << fixed << setprecision(8); ll n, m; cin >> n >> m; vl a(m), b(m); REP(i, m) cin >> a[i] >> b[i]; union_find uf(n); REP(i, m) uf.unite(a[i] - 1, b[i] - 1); ll cntm = 0; ll cnts = 0; REP(i, n) { if (uf.size(i) == 1) { cntm++; } else { if (uf.find(i) == i) { cnts++; } } } cout << abs(cntm - cnts) << endl; }
replace
39
40
39
40
0
p01061
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; struct UnionFind { vector<int> data; UnionFind(int size) { data.assign(size, -1); } int Find(int k) { if (data[k] < 0) return (k); return (data[k] = Find(data[k])); } void unite(int x, int y) { x = Find(x); y = Find(y); if (x == y) return; data[x] += data[y]; data[y] = x; } int Size(int k) { return (-data[Find(k)]); } }; int main() { int N, M; cin >> N >> M; UnionFind tree(N); for (int i = 0; i < M; i++) { int A, B; cin >> A >> B; --A, --B; tree.unite(A, B); } int mura = 0; int used[100] = {}; for (int i = 0; i < N; i++) { int K = tree.Find(i); if (used[K]++) continue; if (tree.Size(K) == 1) ++mura; else --mura; } cout << abs(mura) << endl; }
#include <bits/stdc++.h> using namespace std; struct UnionFind { vector<int> data; UnionFind(int size) { data.assign(size, -1); } int Find(int k) { if (data[k] < 0) return (k); return (data[k] = Find(data[k])); } void unite(int x, int y) { x = Find(x); y = Find(y); if (x == y) return; data[x] += data[y]; data[y] = x; } int Size(int k) { return (-data[Find(k)]); } }; int main() { int N, M; cin >> N >> M; UnionFind tree(N); for (int i = 0; i < M; i++) { int A, B; cin >> A >> B; --A, --B; tree.unite(A, B); } int mura = 0; int used[1000] = {}; for (int i = 0; i < N; i++) { int K = tree.Find(i); if (used[K]++) continue; if (tree.Size(K) == 1) ++mura; else --mura; } cout << abs(mura) << endl; }
replace
33
34
33
34
0
p01062
C++
Time Limit Exceeded
#include <algorithm> #include <cfloat> #include <cmath> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <string> #include <time.h> #include <vector> typedef long long int ll; typedef unsigned long long int ull; #define BIG_NUM 2000000000 #define MOD 1000000007 #define EPS 0.000000001 using namespace std; #define NUM 100000 int N, A, B, num_A, num_B; bool check[NUM]; vector<int> G[NUM]; bool recursive(int side_A, int side_B) { if (num_A == N / 2 && num_B == N / 2) return true; for (int i = 0; i < G[side_A].size(); i++) { if (check[G[side_A][i]]) continue; check[G[side_A][i]] = true; num_A++; for (int k = 0; k < G[side_B].size(); k++) { if (check[G[side_B][k]]) continue; check[G[side_B][k]] = true; num_B++; if (recursive(G[side_A][i], G[side_B][k])) return true; check[G[side_B][k]] = false; num_B--; } check[G[side_A][i]] = false; num_A--; } return false; } int main() { scanf("%d %d %d", &N, &A, &B); A--; B--; num_A = num_B = 1; for (int i = 0; i < N; i++) check[i] = false; check[A] = true; check[B] = true; int from, to; for (int loop = 0; loop < N - 1; loop++) { scanf("%d %d", &from, &to); from--; to--; G[from].push_back(to); G[to].push_back(from); } if (N % 2 != 0) { printf("No\n"); return 0; } if (recursive(A, B)) { printf("Yes\n"); } else { printf("No\n"); } }
#include <algorithm> #include <cfloat> #include <cmath> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <stdio.h> #include <string> #include <time.h> #include <vector> typedef long long int ll; typedef unsigned long long int ull; #define BIG_NUM 2000000000 #define MOD 1000000007 #define EPS 0.000000001 using namespace std; #define NUM 100000 int N, A, B, num_A, num_B; bool check[NUM]; vector<int> G[NUM]; bool recursive(int side_A, int side_B) { if (num_A == N / 2 && num_B == N / 2) return true; for (int i = 0; i < G[side_A].size(); i++) { if (check[G[side_A][i]]) continue; check[G[side_A][i]] = true; num_A++; for (int k = 0; k < G[side_B].size(); k++) { if (check[G[side_B][k]]) continue; check[G[side_B][k]] = true; num_B++; if (recursive(G[side_A][i], G[side_B][k])) return true; check[G[side_B][k]] = false; num_B--; } check[G[side_A][i]] = false; num_A--; } return false; } int main() { scanf("%d %d %d", &N, &A, &B); A--; B--; num_A = num_B = 1; for (int i = 0; i < N; i++) check[i] = false; check[A] = true; check[B] = true; int from, to; for (int loop = 0; loop < N - 1; loop++) { scanf("%d %d", &from, &to); from--; to--; G[from].push_back(to); G[to].push_back(from); } if (N % 2 != 0) { printf("No\n"); return 0; } int num_3 = 0; for (int i = 0; i < N; i++) { if (G[i].size() >= 4) { printf("No\n"); return 0; } if (G[i].size() == 3) { num_3++; if (num_3 > 2) { printf("No\n"); return 0; } } } if (recursive(A, B)) { printf("Yes\n"); } else { printf("No\n"); } }
insert
84
84
84
99
TLE
p01062
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <utility> #include <vector> #define loop(i, a, b) for (int i = a; i < b; i++) #define rep(i, a) loop(i, 0, a) #define pb push_back #define mp make_pair #define all(in) in.begin(), in.end() const double PI = acos(-1); const double EPS = 1e-10; const int inf = 1e8; using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> pii; vvi in; vector<bool> d; int n; int coa, cob; bool f(int a, int b) { if (coa == cob && coa == n / 2) return true; rep(i, in[a].size()) { if (d[in[a][i]]) continue; d[in[a][i]] = true; coa++; rep(j, in[b].size()) { if (d[in[b][j]]) continue; d[in[b][j]] = true; cob++; if (f(in[a][i], in[b][j])) return true; d[in[b][j]] = false; cob--; } d[in[a][i]] = false; coa--; } return false; } int main() { int a, b; coa = cob = 1; cin >> n >> a >> b; d = vector<bool>(n, false); a--; b--; d[a] = d[b] = true; vvi tmp(n); rep(i, n - 1) { int a, b; cin >> a >> b; a--; b--; tmp[a].pb(b); tmp[b].pb(a); } if (n % 2) { cout << "No" << endl; return 0; } in = tmp; cout << (f(a, b) ? "Yes" : "No") << endl; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <utility> #include <vector> #define loop(i, a, b) for (int i = a; i < b; i++) #define rep(i, a) loop(i, 0, a) #define pb push_back #define mp make_pair #define all(in) in.begin(), in.end() const double PI = acos(-1); const double EPS = 1e-10; const int inf = 1e8; using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int, int> pii; vvi in; vector<bool> d; int n; int coa, cob; bool f(int a, int b) { if (coa == cob && coa == n / 2) return true; rep(i, in[a].size()) { if (d[in[a][i]]) continue; d[in[a][i]] = true; coa++; rep(j, in[b].size()) { if (d[in[b][j]]) continue; d[in[b][j]] = true; cob++; if (f(in[a][i], in[b][j])) return true; d[in[b][j]] = false; cob--; } d[in[a][i]] = false; coa--; } return false; } int main() { int a, b; coa = cob = 1; cin >> n >> a >> b; d = vector<bool>(n, false); a--; b--; d[a] = d[b] = true; vvi tmp(n); rep(i, n - 1) { int a, b; cin >> a >> b; a--; b--; tmp[a].pb(b); tmp[b].pb(a); } if (n % 2) { cout << "No" << endl; return 0; } in = tmp; int q = 0; rep(i, n) if (in[i].size() > 3) q++; if (q) { cout << "No" << endl; return 0; } q = 0; rep(i, n) if (in[i].size() == 3) q++; if (q > 2) { cout << "No" << endl; return 0; } cout << (f(a, b) ? "Yes" : "No") << endl; }
insert
73
73
73
85
TLE
p01062
C++
Time Limit Exceeded
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair<int, int> pii; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define all(a) (a).begin(), (a).end() #define pb push_back #define INF 999999999 vector<int> G[100003]; int N; bool dfs(int pa, int pb, int number, bool used[100003]) { // cout<<pa<<" "<<pb<<" "<<number<<endl; bool res = false; if (number == N) return true; vector<int> next1, next2; rep(i, G[pa].size()) { int to = G[pa][i]; if (used[to]) continue; next1.pb(to); } rep(i, G[pb].size()) { int to = G[pb][i]; if (used[to]) continue; next2.pb(to); } rep(i, next1.size()) { rep(j, next2.size()) { if (next1[i] == next2[j]) continue; used[next1[i]] = true; used[next2[j]] = true; res |= dfs(next1[i], next2[j], number + 2, used); used[next1[i]] = false; used[next2[j]] = false; } } return res; } int main() { int u, v; cin >> N >> u >> v; rep(i, N - 1) { int a, b; cin >> a >> b; G[a].pb(b); G[b].pb(a); } bool used[100003] = {}; used[u] = used[v] = true; bool res = dfs(u, v, 2, used); if (res) cout << "Yes" << endl; else cout << "No" << endl; }
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair<int, int> pii; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define all(a) (a).begin(), (a).end() #define pb push_back #define INF 999999999 vector<int> G[100003]; int N; bool dfs(int pa, int pb, int number, bool used[100003]) { // cout<<pa<<" "<<pb<<" "<<number<<endl; bool res = false; if (number == N) return true; vector<int> next1, next2; rep(i, G[pa].size()) { int to = G[pa][i]; if (used[to]) continue; next1.pb(to); } rep(i, G[pb].size()) { int to = G[pb][i]; if (used[to]) continue; next2.pb(to); } rep(i, next1.size()) { rep(j, next2.size()) { if (next1[i] == next2[j]) continue; used[next1[i]] = true; used[next2[j]] = true; res |= dfs(next1[i], next2[j], number + 2, used); used[next1[i]] = false; used[next2[j]] = false; } } return res; } int main() { int u, v; cin >> N >> u >> v; rep(i, N - 1) { int a, b; cin >> a >> b; G[a].pb(b); G[b].pb(a); } rep(i, N + 1) { if (G[i].size() >= 4) { cout << "No" << endl; return 0; } } bool used[100003] = {}; used[u] = used[v] = true; bool res = dfs(u, v, 2, used); if (res) cout << "Yes" << endl; else cout << "No" << endl; }
insert
58
58
58
65
TLE
p01063
C++
Time Limit Exceeded
#include <algorithm> #include <cassert> #include <iostream> #include <tuple> #include <vector> using namespace std; using vi = vector<int>; using ll = long long; const ll inf = 1e9; inline int dist(int x1, int y1, int z1, int x2, int y2, int z2) { return abs(x1 - y1) + abs(y1 - y2) + abs(z1 - z2); } int dx[] = {0, 0, 0, 0, -1, 1, 0}; int dy[] = {0, 0, -1, 1, 0, 0, 0}; int dz[] = {-1, 1, 0, 0, 0, 0, 0}; int ans; int n; struct P { int x, y, z; P(int x, int y, int z) : x(x), y(y), z(z) {} P() {} P rotX(int dir) const { return dir ? P(x, n - z - 1, y) : P(x, z, n - y - 1); } P rotY(int dir) const { return dir ? P(z, y, n - x - 1) : P(n - z - 1, y, x); } P rotZ(int dir) const { return dir ? P(n - y - 1, x, z) : P(y, n - x - 1, z); } int dist(P const &p) const { return abs(x - p.x) + abs(y - p.y) + abs(z - p.z); } bool inner() const { return 0 <= x && 0 <= y && 0 <= z && x < n && y < n && z < n; } }; void dfs(const P &a, const P &b, int role) { // cout << " " << a.x << " " << a.y << " " << a.z << " " << role << endl; ans = min(role + a.dist(b), ans); if (role == 10) return; if (a.x != b.x) { dfs(a.rotX(0), b, role + 1); dfs(a.rotX(1), b, role + 1); } if (a.y != b.y) { dfs(a.rotY(0), b, role + 1); dfs(a.rotY(1), b, role + 1); } if (a.z != b.z) { dfs(a.rotZ(0), b, role + 1); dfs(a.rotZ(1), b, role + 1); } } int main() { while (cin >> n) { ans = 1e9; P a, b; cin >> a.x >> a.y >> a.z; cin >> b.x >> b.y >> b.z; for (int i = 0; i < 6; ++i) { P aa = a; aa.x += dx[i]; aa.y += dy[i]; aa.z += dz[i]; if (aa.inner()) { dfs(aa, b, 0); } } ++ans; dfs(a, b, 0); cout << ans << endl; } }
#include <algorithm> #include <cassert> #include <iostream> #include <tuple> #include <vector> using namespace std; using vi = vector<int>; using ll = long long; const ll inf = 1e9; inline int dist(int x1, int y1, int z1, int x2, int y2, int z2) { return abs(x1 - y1) + abs(y1 - y2) + abs(z1 - z2); } int dx[] = {0, 0, 0, 0, -1, 1, 0}; int dy[] = {0, 0, -1, 1, 0, 0, 0}; int dz[] = {-1, 1, 0, 0, 0, 0, 0}; int ans; int n; struct P { int x, y, z; P(int x, int y, int z) : x(x), y(y), z(z) {} P() {} P rotX(int dir) const { return dir ? P(x, n - z - 1, y) : P(x, z, n - y - 1); } P rotY(int dir) const { return dir ? P(z, y, n - x - 1) : P(n - z - 1, y, x); } P rotZ(int dir) const { return dir ? P(n - y - 1, x, z) : P(y, n - x - 1, z); } int dist(P const &p) const { return abs(x - p.x) + abs(y - p.y) + abs(z - p.z); } bool inner() const { return 0 <= x && 0 <= y && 0 <= z && x < n && y < n && z < n; } }; void dfs(const P &a, const P &b, int role) { // cout << " " << a.x << " " << a.y << " " << a.z << " " << role << endl; ans = min(role + a.dist(b), ans); if (role == 9) return; if (a.x != b.x) { dfs(a.rotX(0), b, role + 1); dfs(a.rotX(1), b, role + 1); } if (a.y != b.y) { dfs(a.rotY(0), b, role + 1); dfs(a.rotY(1), b, role + 1); } if (a.z != b.z) { dfs(a.rotZ(0), b, role + 1); dfs(a.rotZ(1), b, role + 1); } } int main() { while (cin >> n) { ans = 1e9; P a, b; cin >> a.x >> a.y >> a.z; cin >> b.x >> b.y >> b.z; for (int i = 0; i < 6; ++i) { P aa = a; aa.x += dx[i]; aa.y += dy[i]; aa.z += dz[i]; if (aa.inner()) { dfs(aa, b, 0); } } ++ans; dfs(a, b, 0); cout << ans << endl; } }
replace
47
48
47
48
TLE
p01064
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define _MACRO(_1, _2, _3, NAME, ...) NAME #define _repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define _rep(i, n) _repl(i, 0, n) #define rep(...) _MACRO(__VA_ARGS__, _repl, _rep)(__VA_ARGS__) #define mp make_pair #define pb push_back #define all(x) begin(x), end(x) #define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x)) #define fi first #define se second #define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__) void _dbg(string) { cerr << endl; } template <class H, class... T> void _dbg(string s, H h, T... t) { int l = s.find(','); cerr << s.substr(0, l) << " = " << h << ", "; _dbg(s.substr(l + 1), t...); } 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; } #define long long long // for codeforces int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; long a, d; int m; cin >> n >> a >> d >> m; vector<int> x(m), y(m), z(m); rep(i, m) cin >> x[i] >> y[i] >> z[i]; rep(i, m) y[i]--, z[i]--; int k; cin >> k; k--; for (int i = m - 1; i >= 0; i--) { if (x[i] == 0 && y[i] <= k && k <= z[i]) { k = y[i] + z[i] - k; } } long ans = a + d * k; rep(i, n) if (y[i] <= k && k <= z[i]) { if (x[i] == 0) { k = y[i] + z[i] - k; } else if (x[i] == 1) { ans++; } else if (x[i] == 2) { ans /= 2; } else assert(false); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define _MACRO(_1, _2, _3, NAME, ...) NAME #define _repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define _rep(i, n) _repl(i, 0, n) #define rep(...) _MACRO(__VA_ARGS__, _repl, _rep)(__VA_ARGS__) #define mp make_pair #define pb push_back #define all(x) begin(x), end(x) #define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x)) #define fi first #define se second #define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__) void _dbg(string) { cerr << endl; } template <class H, class... T> void _dbg(string s, H h, T... t) { int l = s.find(','); cerr << s.substr(0, l) << " = " << h << ", "; _dbg(s.substr(l + 1), t...); } 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; } #define long long long // for codeforces int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; long a, d; int m; cin >> n >> a >> d >> m; vector<int> x(m), y(m), z(m); rep(i, m) cin >> x[i] >> y[i] >> z[i]; rep(i, m) y[i]--, z[i]--; int k; cin >> k; k--; for (int i = m - 1; i >= 0; i--) { if (x[i] == 0 && y[i] <= k && k <= z[i]) { k = y[i] + z[i] - k; } } long ans = a + d * k; rep(i, m) if (y[i] <= k && k <= z[i]) { if (x[i] == 0) { k = y[i] + z[i] - k; } else if (x[i] == 1) { ans++; } else if (x[i] == 2) { ans /= 2; } else assert(false); } cout << ans << endl; return 0; }
replace
59
60
59
60
0
p01064
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <set> #include <string> #include <utility> #include <vector> #define loop(i, a, b) for (int i = a; i < b; i++) #define rep(i, a) loop(i, 0, a) #define pb push_back #define mp make_pair #define all(in) in.begin(), in.end() const double PI = acos(-1); const double EPS = 1e-10; const int inf = 1e8; using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; int main() { int n; cin >> n; int a, d; cin >> a >> d; int m; cin >> m; vvi in(n, vi(3)); rep(i, m) rep(j, 3) cin >> in[i][j]; int q; cin >> q; for (int i = m - 1; i >= 0; i--) if (in[i][0] == 0 && in[i][1] <= q && q <= in[i][2]) { q = in[i][2] - q + in[i][1]; } int out = a + d * (q - 1); rep(i, m) { if (in[i][0] == 1) { if (in[i][1] <= q && q <= in[i][2]) out++; } else if (in[i][0] == 2) { if (in[i][1] <= q && q <= in[i][2]) out /= 2; } else if (in[i][1] <= q && q <= in[i][2]) { q = in[i][2] - q + in[i][1]; } } cout << out << endl; }
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <set> #include <string> #include <utility> #include <vector> #define loop(i, a, b) for (int i = a; i < b; i++) #define rep(i, a) loop(i, 0, a) #define pb push_back #define mp make_pair #define all(in) in.begin(), in.end() const double PI = acos(-1); const double EPS = 1e-10; const int inf = 1e8; using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; int main() { int n; cin >> n; int a, d; cin >> a >> d; int m; cin >> m; vvi in(m, vi(3)); rep(i, m) rep(j, 3) cin >> in[i][j]; int q; cin >> q; for (int i = m - 1; i >= 0; i--) if (in[i][0] == 0 && in[i][1] <= q && q <= in[i][2]) { q = in[i][2] - q + in[i][1]; } int out = a + d * (q - 1); rep(i, m) { if (in[i][0] == 1) { if (in[i][1] <= q && q <= in[i][2]) out++; } else if (in[i][0] == 2) { if (in[i][1] <= q && q <= in[i][2]) out /= 2; } else if (in[i][1] <= q && q <= in[i][2]) { q = in[i][2] - q + in[i][1]; } } cout << out << endl; }
replace
29
30
29
30
0
p01065
C++
Runtime Error
#include <iostream> #include <vector> constexpr int MAXA = 300; constexpr int MAXB = 600; constexpr int OFFSET = 300; void chmax(int &a, int b) { a = (a < b ? b : a); } int main() { int n, x, y; std::cin >> n >> x >> y; std::vector<int> a(n), b(n), c(n), d(n); for (int i = 0; i < n; i++) { std::cin >> a[i] >> b[i] >> c[i] >> d[i]; } std::vector<std::vector<int>> dp(MAXA, std::vector<int>(MAXB, -1)); dp[x][y + OFFSET] = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < MAXA; j++) { for (int k = 0; k < MAXB; k++) { if (dp[j][k] == -1) { continue; } if (j - a[i] >= 0 && k + b[i] < MAXB) { chmax(dp[j - a[i]][k + b[i]], dp[j][k]); } if (k - c[i] >= 0) { chmax(dp[j][k - c[i]], dp[j][k] + d[i]); } } } } int ret = 0; for (int i = 0; i < MAXA; i++) { for (int j = OFFSET; j < MAXB; j++) { chmax(ret, dp[i][j]); } } std::cout << ret << std::endl; return 0; }
#include <iostream> #include <vector> constexpr int MAXA = 350; constexpr int MAXB = 700; constexpr int OFFSET = 350; void chmax(int &a, int b) { a = (a < b ? b : a); } int main() { int n, x, y; std::cin >> n >> x >> y; std::vector<int> a(n), b(n), c(n), d(n); for (int i = 0; i < n; i++) { std::cin >> a[i] >> b[i] >> c[i] >> d[i]; } std::vector<std::vector<int>> dp(MAXA, std::vector<int>(MAXB, -1)); dp[x][y + OFFSET] = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < MAXA; j++) { for (int k = 0; k < MAXB; k++) { if (dp[j][k] == -1) { continue; } if (j - a[i] >= 0 && k + b[i] < MAXB) { chmax(dp[j - a[i]][k + b[i]], dp[j][k]); } if (k - c[i] >= 0) { chmax(dp[j][k - c[i]], dp[j][k] + d[i]); } } } } int ret = 0; for (int i = 0; i < MAXA; i++) { for (int j = OFFSET; j < MAXB; j++) { chmax(ret, dp[i][j]); } } std::cout << ret << std::endl; return 0; }
replace
3
6
3
6
0
p01065
C++
Runtime Error
/* _ooOoo_ o8888888o 88" . "88 (| -_- |) O\ = /O ____/`---'\____ .' \\| |// `. / \\||| : |||// \ / _||||| -:- |||||- \ | | \\\ - /// | | | \_| ''\---/'' | | \ .-\__ `-` ___/-. / ___`. .' /--.--\ `. . __ ."" '< `.___\_<|>_/___.' >'"". | | : `- \`.;`\ _ /`;.`/ - ` : | | \ \ `-. \_ __\ /__ _/ .-` / / ======`-.____`-.___\_____/___.-`____.-'====== `=---=' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ pass System Test! */ #include <bits/stdc++.h> using namespace std; typedef long long ll; template <typename T> std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) { if (!v.empty()) { out << '['; std::copy(v.begin(), v.end(), std::ostream_iterator<T>(out, ", ")); out << "\b\b]"; } return out; } template <class T, class U> void chmin(T &t, U f) { if (t > f) t = f; } template <class T, class U> void chmax(T &t, U f) { if (t < f) t = f; } int main() { cin.tie(0); ios::sync_with_stdio(false); const int B = 350; int N, x, y; cin >> N >> x >> y; vector<int> a(N), b(N), c(N), d(N); for (int i = 0; i < N; ++i) { cin >> a[i] >> b[i] >> c[i] >> d[i]; } vector<vector<vector<int>>> dp( N + 1, vector<vector<int>>(305, vector<int>(B + 305, -1))); dp[0][x][y + B] = 0; for (int i = 0; i < N; ++i) { for (int j = 0; j < dp[i].size(); ++j) { for (int k = 0; k < dp[i][j].size(); ++k) { if (dp[i][j][k] < 0) continue; chmax(dp[i + 1][j][k], dp[i][j][k]); if (j >= a[i]) chmax(dp[i + 1][j - a[i]][k + b[i]], dp[i][j][k]); if (k >= c[i]) chmax(dp[i + 1][j][k - c[i]], dp[i][j][k] + d[i]); } } } int ma = 0; for (int j = 0; j < dp[N].size(); ++j) { for (int k = B; k < dp[N][j].size(); ++k) { chmax(ma, dp[N][j][k]); } } cout << ma << endl; }
/* _ooOoo_ o8888888o 88" . "88 (| -_- |) O\ = /O ____/`---'\____ .' \\| |// `. / \\||| : |||// \ / _||||| -:- |||||- \ | | \\\ - /// | | | \_| ''\---/'' | | \ .-\__ `-` ___/-. / ___`. .' /--.--\ `. . __ ."" '< `.___\_<|>_/___.' >'"". | | : `- \`.;`\ _ /`;.`/ - ` : | | \ \ `-. \_ __\ /__ _/ .-` / / ======`-.____`-.___\_____/___.-`____.-'====== `=---=' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ pass System Test! */ #include <bits/stdc++.h> using namespace std; typedef long long ll; template <typename T> std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) { if (!v.empty()) { out << '['; std::copy(v.begin(), v.end(), std::ostream_iterator<T>(out, ", ")); out << "\b\b]"; } return out; } template <class T, class U> void chmin(T &t, U f) { if (t > f) t = f; } template <class T, class U> void chmax(T &t, U f) { if (t < f) t = f; } int main() { cin.tie(0); ios::sync_with_stdio(false); const int B = 350; int N, x, y; cin >> N >> x >> y; vector<int> a(N), b(N), c(N), d(N); for (int i = 0; i < N; ++i) { cin >> a[i] >> b[i] >> c[i] >> d[i]; } vector<vector<vector<int>>> dp( N + 1, vector<vector<int>>(305, vector<int>(B + 605, -1))); dp[0][x][y + B] = 0; for (int i = 0; i < N; ++i) { for (int j = 0; j < dp[i].size(); ++j) { for (int k = 0; k < dp[i][j].size(); ++k) { if (dp[i][j][k] < 0) continue; chmax(dp[i + 1][j][k], dp[i][j][k]); if (j >= a[i]) chmax(dp[i + 1][j - a[i]][k + b[i]], dp[i][j][k]); if (k >= c[i]) chmax(dp[i + 1][j][k - c[i]], dp[i][j][k] + d[i]); } } } int ma = 0; for (int j = 0; j < dp[N].size(); ++j) { for (int k = B; k < dp[N][j].size(); ++k) { chmax(ma, dp[N][j][k]); } } cout << ma << endl; }
replace
58
59
58
59
0
p01065
C++
Runtime Error
#include <algorithm> #include <iostream> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } int n, x, y; int a[102], b[102], c[102], d[102]; // idx, umaka, fugashi int dp[102][302][700]; const int offset = 301; #define at(i, j, k) dp[i][j][k + offset] int dfs(int i, int j, int k) { if (i == n) return 0; int ret = dfs(i + 1, j, k); if (j >= a[i]) ret = max(ret, dfs(i + 1, j - a[i], k + b[i])); ret = max(ret, dfs(i + 1, j, k - c[i]) + d[i]); return ret; } int main() { cin >> n >> x >> y; rep(i, n) { cin >> a[i] >> b[i] >> c[i] >> d[i]; } rep(i, 102) rep(j, 303) rep(k, 302 * 2) dp[i][j][k] = -1e8; at(0, x, y) = 0; rep(i, n) rep(j, 302) for (int k = -301; k <= 301; k++) { // cout<<i<<", "<<j<<", "<<k<<endl; chmax(at(i + 1, j, k), at(i, j, k)); if (j - a[i] >= 0 && k + b[i] < 303 * 2) chmax(at(i + 1, j - a[i], k + b[i]), at(i, j, k)); if (k - c[i] + offset >= 0) chmax(at(i + 1, j, k - c[i]), at(i, j, k) + d[i]); } int ans = -1e8; rep(i, n + 1) rep(j, 302) rep(k, 302) chmax(ans, at(i, j, k)); cout << ans << endl; return 0; }
#include <algorithm> #include <iostream> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } int n, x, y; int a[102], b[102], c[102], d[102]; // idx, umaka, fugashi int dp[102][302][700]; const int offset = 301; #define at(i, j, k) dp[i][j][k + offset] int dfs(int i, int j, int k) { if (i == n) return 0; int ret = dfs(i + 1, j, k); if (j >= a[i]) ret = max(ret, dfs(i + 1, j - a[i], k + b[i])); ret = max(ret, dfs(i + 1, j, k - c[i]) + d[i]); return ret; } int main() { cin >> n >> x >> y; rep(i, n) { cin >> a[i] >> b[i] >> c[i] >> d[i]; } rep(i, 102) rep(j, 302) rep(k, 302 * 2) dp[i][j][k] = -1e8; at(0, x, y) = 0; rep(i, n) rep(j, 302) for (int k = -301; k <= 301; k++) { // cout<<i<<", "<<j<<", "<<k<<endl; chmax(at(i + 1, j, k), at(i, j, k)); if (j - a[i] >= 0 && k + b[i] < 303 * 2) chmax(at(i + 1, j - a[i], k + b[i]), at(i, j, k)); if (k - c[i] + offset >= 0) chmax(at(i + 1, j, k - c[i]), at(i, j, k) + d[i]); } int ans = -1e8; rep(i, n + 1) rep(j, 302) rep(k, 302) chmax(ans, at(i, j, k)); cout << ans << endl; return 0; }
replace
35
36
35
36
-11
p01069
C++
Time Limit Exceeded
#include <climits> #include <cmath> #include <complex> #include <iostream> #include <map> #include <vector> using namespace std; typedef pair<int, int> i_i; typedef long double D; typedef complex<D> P; typedef long long ll; D pi = acos(-1.0); void FFT(vector<P> &a, int N, double t) { for (int m = N; m >= 2; m /= 2) { int mh = m / 2; for (int i = 0; i < mh; i++) { P w = polar(1.0, t * i); for (int j = i; j < N; j += m) { int k = j + mh; P x = a[j] - a[k]; a[j] += a[k]; a[k] = w * x; } } t *= 2; } int i = 0; for (int j = 1; j <= N - 2; j++) { for (int k = N / 2; k > (i ^= k); k /= 2) ; if (j < i) swap(a[i], a[j]); } } vector<P> conv(vector<P> a, vector<P> b) { int n = a.size() + b.size(); int N = 1; while (N < n) N *= 2; a.resize(N); b.resize(N); FFT(a, N, 2 * pi / N); FFT(b, N, 2 * pi / N); for (int i = 0; i < N; i++) a[i] *= b[i]; FFT(a, N, -2 * pi / N); for (int i = 0; i < N; i++) a[i] /= N; return a; } vector<int> f(int N, vector<int> &a) { vector<int> sum(N + 1); for (int i = 0; i < N; i++) sum[i + 1] = sum[i] + a[i]; vector<P> p(200001), q = p; for (int i = 0; i <= N; i++) { p[sum[i]] += 1; q[200000 - sum[i]] += 1; } vector<P> r = conv(p, q); vector<int> ans(200001); for (int x = 1; x <= 200000; x++) ans[x] += (int)(r[200000 + x].real() + 0.5); return ans; } int main() { int N, M, Q; cin >> N >> M >> Q; vector<int> a(N), b(M); for (int i = 0; i < N; i++) scanf("%d", &a[i]); for (int j = 0; j < M; j++) scanf("%d", &b[j]); vector<int> A = f(N, a), B = f(M, b); vector<P> p(200001), q(200001); for (int x = 0; x <= 200000; x++) p[x] += A[x]; for (int x = 0; x <= 200000; x++) q[200000 - x] += B[x]; vector<P> r = conv(p, q); vector<ll> ans(200001); for (int x = 0; x <= 200000; x++) ans[x] += (ll)(r[200000 + x].real() + 0.5) + (ll)(r[200000 - x].real() + 0.5); ans[0] /= 2; while (Q--) { int c; scanf("%d", &c); printf("%lld\n", ans[c]); } }
#include <climits> #include <cmath> #include <complex> #include <iostream> #include <map> #include <vector> using namespace std; typedef pair<int, int> i_i; typedef double D; typedef complex<D> P; typedef long long ll; D pi = acos(-1.0); void FFT(vector<P> &a, int N, double t) { for (int m = N; m >= 2; m /= 2) { int mh = m / 2; for (int i = 0; i < mh; i++) { P w = polar(1.0, t * i); for (int j = i; j < N; j += m) { int k = j + mh; P x = a[j] - a[k]; a[j] += a[k]; a[k] = w * x; } } t *= 2; } int i = 0; for (int j = 1; j <= N - 2; j++) { for (int k = N / 2; k > (i ^= k); k /= 2) ; if (j < i) swap(a[i], a[j]); } } vector<P> conv(vector<P> a, vector<P> b) { int n = a.size() + b.size(); int N = 1; while (N < n) N *= 2; a.resize(N); b.resize(N); FFT(a, N, 2 * pi / N); FFT(b, N, 2 * pi / N); for (int i = 0; i < N; i++) a[i] *= b[i]; FFT(a, N, -2 * pi / N); for (int i = 0; i < N; i++) a[i] /= N; return a; } vector<int> f(int N, vector<int> &a) { vector<int> sum(N + 1); for (int i = 0; i < N; i++) sum[i + 1] = sum[i] + a[i]; vector<P> p(200001), q = p; for (int i = 0; i <= N; i++) { p[sum[i]] += 1; q[200000 - sum[i]] += 1; } vector<P> r = conv(p, q); vector<int> ans(200001); for (int x = 1; x <= 200000; x++) ans[x] += (int)(r[200000 + x].real() + 0.5); return ans; } int main() { int N, M, Q; cin >> N >> M >> Q; vector<int> a(N), b(M); for (int i = 0; i < N; i++) scanf("%d", &a[i]); for (int j = 0; j < M; j++) scanf("%d", &b[j]); vector<int> A = f(N, a), B = f(M, b); vector<P> p(200001), q(200001); for (int x = 0; x <= 200000; x++) p[x] += A[x]; for (int x = 0; x <= 200000; x++) q[200000 - x] += B[x]; vector<P> r = conv(p, q); vector<ll> ans(200001); for (int x = 0; x <= 200000; x++) ans[x] += (ll)(r[200000 + x].real() + 0.5) + (ll)(r[200000 - x].real() + 0.5); ans[0] /= 2; while (Q--) { int c; scanf("%d", &c); printf("%lld\n", ans[c]); } }
replace
9
10
9
10
TLE
p01075
C++
Time Limit Exceeded
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair<int, int> pii; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define all(a) (a).begin(), (a).end() #define vi vector<int> #define pb push_back #define INF 999999999 // #define INF (1LL<<59) struct edge { int to, cost; }; #define MAX_V 100000 vector<edge> G[MAX_V]; void dijkstra(int s, vector<int> &d) { priority_queue<pii, vector<pii>, greater<pii>> que; rep(i, d.size()) d[i] = INF; d[s] = 0; que.push(pii(0, s)); while (que.size()) { pii p = que.top(); que.pop(); int v = p.second; rep(i, G[v].size()) { edge e = G[v][i]; if (d[e.to] > e.cost && d[v] <= e.cost) { d[e.to] = e.cost; que.push(pii(d[e.to], e.to)); } } } } int main() { int n, m; cin >> n >> m; rep(i, m) { int a, b, c; cin >> a >> b >> c; a--, b--; G[a].pb(edge{b, c}); } vector<int> d(n); dijkstra(0, d); int ans = -1; rep(i, n) { for (auto &e : G[i]) { if (e.to == n - 1) { if (d[i] > e.cost) continue; ans = max(e.cost, ans); } } } cout << ans << endl; }
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair<int, int> pii; #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define all(a) (a).begin(), (a).end() #define vi vector<int> #define pb push_back #define INF 999999999 // #define INF (1LL<<59) struct edge { int to, cost; }; #define MAX_V 100000 vector<edge> G[MAX_V]; void dijkstra(int s, vector<int> &d) { priority_queue<pii, vector<pii>, greater<pii>> que; rep(i, d.size()) d[i] = INF; d[s] = 0; que.push(pii(0, s)); while (que.size()) { pii p = que.top(); que.pop(); int v = p.second; if (d[v] < p.first) continue; rep(i, G[v].size()) { edge e = G[v][i]; if (d[e.to] > e.cost && d[v] <= e.cost) { d[e.to] = e.cost; que.push(pii(d[e.to], e.to)); } } } } int main() { int n, m; cin >> n >> m; rep(i, m) { int a, b, c; cin >> a >> b >> c; a--, b--; G[a].pb(edge{b, c}); } vector<int> d(n); dijkstra(0, d); int ans = -1; rep(i, n) { for (auto &e : G[i]) { if (e.to == n - 1) { if (d[i] > e.cost) continue; ans = max(e.cost, ans); } } } cout << ans << endl; }
replace
29
30
29
31
TLE
p01075
C++
Time Limit Exceeded
#include "bits/stdc++.h" #include <unordered_map> #include <unordered_set> #pragma warning(disable : 4996) using namespace std; int main() { int N, M; cin >> N >> M; vector<vector<pair<int, int>>> edges(N); vector<int> memo(N - 1, 1e9 + 1e7); memo[0] = 0; for (int i = 0; i < M; ++i) { int a, b, c; cin >> a >> b >> c; a--; b--; edges[a].push_back(make_pair(b, c)); } priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> que; que.push(make_pair(0, 0)); while (!que.empty()) { auto atop(que.top()); que.pop(); const int now = atop.second; const int now_time = atop.first; for (auto e : edges[now]) { if (e.first == N - 1) continue; if (e.second >= now_time) { if (memo[e.first] > e.second) { que.push(make_pair(e.second, e.first)); memo[e.first] = e.second; } } } } int ans = -1; for (int i = 0; i < N - 1; ++i) { for (auto e : edges[i]) { if (e.first == N - 1 && memo[i] <= e.second) { ans = max(ans, e.second); } } } { cout << ans << endl; } return 0; }
#include "bits/stdc++.h" #include <unordered_map> #include <unordered_set> #pragma warning(disable : 4996) using namespace std; int main() { int N, M; cin >> N >> M; vector<vector<pair<int, int>>> edges(N); vector<int> memo(N - 1, 1e9 + 1e7); memo[0] = 0; for (int i = 0; i < M; ++i) { int a, b, c; cin >> a >> b >> c; a--; b--; edges[a].push_back(make_pair(b, c)); } priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> que; que.push(make_pair(0, 0)); while (!que.empty()) { auto atop(que.top()); que.pop(); const int now = atop.second; const int now_time = atop.first; if (memo[now] < now_time) continue; for (auto e : edges[now]) { if (e.first == N - 1) continue; if (e.second >= now_time) { if (memo[e.first] > e.second) { que.push(make_pair(e.second, e.first)); memo[e.first] = e.second; } } } } int ans = -1; for (int i = 0; i < N - 1; ++i) { for (auto e : edges[i]) { if (e.first == N - 1 && memo[i] <= e.second) { ans = max(ans, e.second); } } } { cout << ans << endl; } return 0; }
insert
30
30
30
32
TLE
p01080
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long #define REP(i, n) for (ll i = 0, max_i = (n); i < max_i; i++) #define REPI(i, a, b) for (ll i = (a), max_i = (b); i < max_i; i++) #define ALL(obj) (obj).begin(), (obj).end() #define RALL(obj) (obj).rbegin(), (obj).rend() #define fi first #define se second #define pb push_back #define debug(x) cerr << #x << ": " << (x) << endl #define int long long using namespace std; using II = pair<int, int>; using VII = vector<II>; using VVII = vector<VII>; using VI = vector<int>; using VVI = vector<VI>; using VVVI = vector<VVI>; template <class T = int> inline T in() { T x; cin >> x; return x; } template <class T = int> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <class T = int> inline bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } template <class T> ostream &operator<<(ostream &s, const vector<T> &d) { int n = d.size(); REP(i, n) s << d[i] << " "; return s; } template <class T> ostream &operator<<(ostream &s, const vector<vector<T>> &dd) { for (vector<T> d : dd) s << d << endl; return s; } // struct Fast { Fast() { cin.tie(0); ios::sync_with_stdio(false); } } fast; const int MOD = 1e9 + 7; signed main() { int N = in(); VVI G(N); REP(i, N - 1) { int u, v; cin >> u >> v; u--; v--; G[u].push_back(v); G[v].push_back(u); } VVII dist(N); // {len, v} VI far(N); function<void(int, int)> dfs1 = [&](int v, int prev) { for (int u : G[v]) if (u != prev) { dfs1(u, v); dist[v].push_back({far[u] + 1, u}); chmax(far[v], far[u] + 1); } }; dfs1(0, -1); sort(RALL(dist[0])); function<void(int, int)> dfs2 = [&](int v, int prev) { for (int u : G[v]) if (u != prev) { int tmp; if (dist[v][0].se == u) { tmp = dist[v].size() == 1 ? 0 : dist[v][1].fi; } else { tmp = dist[v][0].fi; } debug(tmp); dist[u].pb({tmp + 1, v}); sort(RALL(dist[u])); dfs2(u, v); } }; dfs2(0, -1); REP(i, N) { cout << 2 * (N - 1) - dist[i][0].fi << endl; } }
#include <bits/stdc++.h> #define ll long long #define REP(i, n) for (ll i = 0, max_i = (n); i < max_i; i++) #define REPI(i, a, b) for (ll i = (a), max_i = (b); i < max_i; i++) #define ALL(obj) (obj).begin(), (obj).end() #define RALL(obj) (obj).rbegin(), (obj).rend() #define fi first #define se second #define pb push_back #define debug(x) cerr << #x << ": " << (x) << endl #define int long long using namespace std; using II = pair<int, int>; using VII = vector<II>; using VVII = vector<VII>; using VI = vector<int>; using VVI = vector<VI>; using VVVI = vector<VVI>; template <class T = int> inline T in() { T x; cin >> x; return x; } template <class T = int> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <class T = int> inline bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } template <class T> ostream &operator<<(ostream &s, const vector<T> &d) { int n = d.size(); REP(i, n) s << d[i] << " "; return s; } template <class T> ostream &operator<<(ostream &s, const vector<vector<T>> &dd) { for (vector<T> d : dd) s << d << endl; return s; } // struct Fast { Fast() { cin.tie(0); ios::sync_with_stdio(false); } } fast; const int MOD = 1e9 + 7; signed main() { int N = in(); VVI G(N); REP(i, N - 1) { int u, v; cin >> u >> v; u--; v--; G[u].push_back(v); G[v].push_back(u); } VVII dist(N); // {len, v} VI far(N); function<void(int, int)> dfs1 = [&](int v, int prev) { for (int u : G[v]) if (u != prev) { dfs1(u, v); dist[v].push_back({far[u] + 1, u}); chmax(far[v], far[u] + 1); } }; dfs1(0, -1); sort(RALL(dist[0])); function<void(int, int)> dfs2 = [&](int v, int prev) { for (int u : G[v]) if (u != prev) { int tmp; if (dist[v][0].se == u) { tmp = dist[v].size() == 1 ? 0 : dist[v][1].fi; } else { tmp = dist[v][0].fi; } dist[u].pb({tmp + 1, v}); sort(RALL(dist[u])); dfs2(u, v); } }; dfs2(0, -1); REP(i, N) { cout << 2 * (N - 1) - dist[i][0].fi << endl; } }
delete
84
85
84
84
0
tmp: 0
p01081
C++
Runtime Error
#include <cstdio> #include <cstring> #include <deque> #include <iostream> const int N = 200000; int n; char string[N + 1]; std::deque<int> letters[26]; int count[N]; bool present[N]; void modify(int k, int v) { for (; k < n; k += ~k & k + 1) { count[k] += v; } } int query(int k) { int ret = 0; for (; k >= 0; k -= ~k & k + 1) { ret += count[k]; } return ret; } void remove(int i) { present[i] = false; modify(i, -1); } int main() { scanf("%s", string); n = strlen(string); for (int i = 0; i < n; ++i) { letters[(int)(string[i] -= 'a')].push_back(i); } int odd_count = 0; for (int i = 0; i < 26; ++i) { odd_count += letters[i].size() & 1; } if (odd_count != (n & 1)) { puts("-1"); return 0; } memset(present, true, sizeof(present)); for (int i = 0; i < n; ++i) { modify(i, 1); } long long answer = 0; for (int l = 0, r = n - 1; l < r;) { if (!present[l]) { l++; } else if (!present[r]) { r--; } else { int left_cost = query(n - 1) - query(letters[(int)string[l]].back()); int right_cost = query(letters[(int)string[r]].front()) - 1; answer += std::min(left_cost, right_cost); int token = left_cost < right_cost ? string[l] : string[r]; remove(letters[token].front()); remove(letters[token].back()); letters[token].pop_front(); letters[token].pop_back(); } } std::cout << answer << std::endl; return 0; }
#include <cstdio> #include <cstring> #include <deque> #include <iostream> const int N = 1000000; int n; char string[N + 1]; std::deque<int> letters[26]; int count[N]; bool present[N]; void modify(int k, int v) { for (; k < n; k += ~k & k + 1) { count[k] += v; } } int query(int k) { int ret = 0; for (; k >= 0; k -= ~k & k + 1) { ret += count[k]; } return ret; } void remove(int i) { present[i] = false; modify(i, -1); } int main() { scanf("%s", string); n = strlen(string); for (int i = 0; i < n; ++i) { letters[(int)(string[i] -= 'a')].push_back(i); } int odd_count = 0; for (int i = 0; i < 26; ++i) { odd_count += letters[i].size() & 1; } if (odd_count != (n & 1)) { puts("-1"); return 0; } memset(present, true, sizeof(present)); for (int i = 0; i < n; ++i) { modify(i, 1); } long long answer = 0; for (int l = 0, r = n - 1; l < r;) { if (!present[l]) { l++; } else if (!present[r]) { r--; } else { int left_cost = query(n - 1) - query(letters[(int)string[l]].back()); int right_cost = query(letters[(int)string[r]].front()) - 1; answer += std::min(left_cost, right_cost); int token = left_cost < right_cost ? string[l] : string[r]; remove(letters[token].front()); remove(letters[token].back()); letters[token].pop_front(); letters[token].pop_back(); } } std::cout << answer << std::endl; return 0; }
replace
5
6
5
6
0
p01085
C++
Runtime Error
#include <cstdio> int main() { while (1) { int m, nmin, nmax, p[201], maxg = 0, n; scanf("%d %d %d", &m, &nmin, &nmax); for (int i = 1; i < m; i++) { scanf("%d", &p[i]); } for (int j = nmin; j <= nmax; j++) { if (p[j] - p[j + 1] >= maxg) { maxg = p[j] - p[j + 1]; n = j; } } if (m == 0 && nmin == 0 && nmax == 0) { return 0; } else { printf("%d\n", n); } } }
#include <cstdio> int main() { while (1) { int m, nmin, nmax, p[201], maxg = 0, n; scanf("%d %d %d", &m, &nmin, &nmax); for (int i = 1; i <= m; i++) { scanf("%d", &p[i]); } for (int j = nmin; j <= nmax; j++) { if (p[j] - p[j + 1] >= maxg) { maxg = p[j] - p[j + 1]; n = j; } } if (m == 0 && nmin == 0 && nmax == 0) { return 0; } else { printf("%d\n", n); } } }
replace
6
7
6
7
TLE
p01085
C++
Runtime Error
// #include <bits/stdc++.h> #include <algorithm> #include <cstdio> #include <vector> #define ll long long #define all(a) (a).begin(), (a).end() int main(int argc, char const *argv[]) { while (1) { int m = 0, nmin = 0, nmax = 0; scanf("%d%d%d", &m, &nmin, &nmax); if (m == 0 && nmin == 0 && nmax == 0) { break; } std::vector<int> p(m); for (int i = 1; i <= m; i++) { scanf("%d", &p[i]); } int temp = 0, memo = 0, memoid = 0; for (int i = nmin; i <= nmax; i++) { temp = p[i] - p[i + 1]; memo = std::max(memo, temp); if (memo == temp) memoid = i; } p.erase(all(p)); printf("%d\n", memoid); } return 0; }
// #include <bits/stdc++.h> #include <algorithm> #include <cstdio> #include <vector> #define ll long long #define all(a) (a).begin(), (a).end() int main(int argc, char const *argv[]) { while (1) { int m = 0, nmin = 0, nmax = 0; scanf("%d%d%d", &m, &nmin, &nmax); if (m == 0 && nmin == 0 && nmax == 0) { break; } std::vector<int> p(m + 1); for (int i = 1; i <= m; i++) { scanf("%d", &p[i]); } int temp = 0, memo = 0, memoid = 0; for (int i = nmin; i <= nmax; i++) { temp = p[i] - p[i + 1]; memo = std::max(memo, temp); if (memo == temp) memoid = i; } p.erase(all(p)); printf("%d\n", memoid); } return 0; }
replace
14
15
14
15
0
p01086
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <numeric> #include <string> #include <vector> using namespace std; int main() { while (true) { int n; cin >> n; if (n == 0) { return 0; } string str; int nums[41]; for (int i = 0; i < n; i++) { cin >> str; nums[i] = str.size(); } int start = 0; int tail = 0; int sum = 0; while (true) { while (tail < n && sum < (5 + 7 + 5 + 7 + 7)) { tail++; sum = 0; for (int i = start; i < tail; i++) { sum += nums[i]; } } if (sum < (5 + 7 + 5 + 7 + 7)) { break; } else if (sum == (5 + 7 + 5 + 7 + 7)) { int ns[5] = {5, 7, 5, 7, 7}; int s = start; int i = 0; while (i < 5) { if (nums[s] == ns[i]) { i++; s++; } else if (nums[s] > ns[i]) { break; } else { ns[i] = ns[i] - nums[s]; s++; } } if (i == 5) { cout << start + 1 << endl; break; } } while (start < tail && sum > (5 + 7 + 5 + 7 + 7)) { start++; sum = 0; for (int i = start; i < tail; i++) { sum += nums[i]; } } } } return 0; }
#include <algorithm> #include <iostream> #include <numeric> #include <string> #include <vector> using namespace std; int main() { while (true) { int n; cin >> n; if (n == 0) { return 0; } string str; int nums[41]; for (int i = 0; i < n; i++) { cin >> str; nums[i] = str.size(); } int start = 0; int tail = 0; int sum = 0; while (true) { while (tail < n && sum < (5 + 7 + 5 + 7 + 7)) { tail++; sum = 0; for (int i = start; i < tail; i++) { sum += nums[i]; } } if (sum < (5 + 7 + 5 + 7 + 7)) { break; } else if (sum == (5 + 7 + 5 + 7 + 7)) { int ns[5] = {5, 7, 5, 7, 7}; int s = start; int i = 0; while (i < 5) { if (nums[s] == ns[i]) { i++; s++; } else if (nums[s] > ns[i]) { break; } else { ns[i] = ns[i] - nums[s]; s++; } } if (i == 5) { cout << start + 1 << endl; break; } } start++; sum = 0; for (int i = start; i < tail; i++) { sum += nums[i]; } } } return 0; }
replace
61
68
61
65
TLE
p01086
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; int tx[] = {0, 1, 0, -1}; int ty[] = {-1, 0, 1, 0}; int main() { int num_word; while (~scanf("%d", &num_word)) { if (num_word == 0) break; vector<string> words; for (int word_i = 0; word_i < num_word; word_i++) { string word; cin >> word; words.push_back(word); } int res = 0; for (int start = 0; start < num_word; start++) { bool has_end[50]; memset(has_end, 0, sizeof(has_end)); int pos = 0; for (int word_i = start; word_i < num_word; word_i++) { pos += words[word_i].size(); has_end[pos] = true; } if (has_end[5] && has_end[5 + 7] && has_end[5 + 7 + 5] && has_end[5 + 7 + 5 + 7] && has_end[5 + 7 + 5 + 7 + 7]) { res = start + 1; break; } } printf("%d\n", res); } }
#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; int tx[] = {0, 1, 0, -1}; int ty[] = {-1, 0, 1, 0}; int main() { int num_word; while (~scanf("%d", &num_word)) { if (num_word == 0) break; vector<string> words; for (int word_i = 0; word_i < num_word; word_i++) { string word; cin >> word; words.push_back(word); } int res = 0; for (int start = 0; start < num_word; start++) { bool has_end[256]; memset(has_end, 0, sizeof(has_end)); int pos = 0; for (int word_i = start; word_i < num_word; word_i++) { pos += words[word_i].size(); has_end[pos] = true; } if (has_end[5] && has_end[5 + 7] && has_end[5 + 7 + 5] && has_end[5 + 7 + 5 + 7] && has_end[5 + 7 + 5 + 7 + 7]) { res = start + 1; break; } } printf("%d\n", res); } }
replace
47
48
47
48
-6
*** stack smashing detected ***: terminated
p01086
C++
Runtime Error
#ifdef _MSVC_LANG #define _SCL_SECURE_NO_WARNINGS #pragma error(disable : 0325) // inline namespace Error #endif #include <algorithm> #include <array> #include <bitset> #include <cstdio> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <random> #include <string> #include <unordered_set> #include <vector> // C++ 11 前提 using Ull = unsigned long long; using Ll = long long; template <typename T> using Vec = std::vector<T>; template <typename T, size_t L> using Ary = std::array<T, L>; namespace { namespace meta { namespace coming { // c++ 17; template <class... _Types> using void_t = void; template <class _Ty, _Ty _Val> struct integral_constant { // convenient template for integral constant types static constexpr _Ty value = _Val; using value_type = _Ty; using type = integral_constant; constexpr operator value_type() const noexcept { // return stored value return (value); } constexpr value_type operator()() const noexcept { // return stored value return (value); } }; // c++ 17; template <bool _Val> using bool_constant = integral_constant<bool, _Val>; // c++ 14 template <bool _Test, class _Ty = void> using enable_if_t = typename std::enable_if<_Test, _Ty>::type; } // namespace coming namespace core { template <class, class = void> struct enable_std_begin_end : std::false_type {}; template <typename T> struct enable_std_begin_end< T, coming::void_t<decltype(std::begin(std::declval<const T &>()), std::end(std::declval<const T &>()))>> : std::true_type {}; // c++ 17 template <class, class = void> struct enable_adl_begin_end : std::false_type {}; template <typename T> struct enable_adl_begin_end< T, coming::void_t<decltype(begin(std::declval<const T &>()), end(std::declval<const T &>()))>> : std::true_type {}; } // namespace core template <typename T> struct is_range : coming::bool_constant<core::enable_std_begin_end<T>::value || core::enable_adl_begin_end<T>::value> { }; /// <summary> /// ある値が0以上か /// </summary> template <class T, T num, class = std::nullptr_t> struct is_positive : coming::bool_constant<(num >= 0)> {}; } // namespace meta inline namespace extension { inline namespace container_io { inline namespace in { /// <summary> /// for cin with STL container /// </summary> template <template <class...> class Container, class... Args, typename = meta::coming::enable_if_t< !std::is_same<Container<Args...>, std::string>::value>> std::istream &operator>>(std::istream &is, Container<Args...> &container) { for (auto &elem : container) is >> elem; return is; } /// <summary> /// for cin with std::arary /// </summary> template <class T, size_t L> std::istream &operator>>(std::istream &is, std::array<T, L> &container) { for (auto &elem : container) is >> elem; return is; } /// <summary> /// for cin with raw array /// </summary> template <class T, size_t L> std::istream &operator>>(std::istream &is, T (&ary)[L]) { for (int i{}; i < L; ++i) is >> ary[i]; return is; } } // namespace in inline namespace out { namespace settings { /// <summary> /// 要素間の区切り文字 /// </summary> const auto sepOfContainerElements{" "}; /// <summary> /// コンテナ間の区切り文字 /// </summary> const auto sepOfContainer{"\n"}; } // namespace settings /// <summary> /// for cout Container with STL container /// </summary> template <template <class...> class Container, class... Args, typename = meta::coming::enable_if_t< !std::is_same<Container<Args...>, std::string>::value>> std::ostream &operator<<(std::ostream &os, const Container<Args...> &container) { for (const auto &elem : container) os << elem << settings::sepOfContainerElements; return os; } /// <summary> /// for cout Container with std::array /// </summary> template <class T, size_t L> std::ostream &operator<<(std::ostream &os, const std::array<T, L> container) { for (const auto &elem : container) os << elem << settings::sepOfContainerElements; return os; } /// <summary> /// for cout Container with raw array /// </summary> template <class T, size_t L, typename = meta::coming::enable_if_t<!std::is_same<T, char>::value>> std::ostream &operator<<(std::ostream &os, const T (&ary)[L]) { for (const auto &elem : ary) os << elem << settings::sepOfContainerElements; return os; } /// <summary> /// for cout Container in Container with STL container /// </summary> template <template <class...> class Container1, class... Cont1Args, template <class...> class Container2, class... Cont2Args> std::ostream &operator<<( std::ostream &os, const Container1<Container2<Cont2Args...>, Cont1Args...> &container) { for (const auto &elem : container) os << elem << settings::sepOfContainer; return os; } /// <summary> /// for cout Container in Container with std::array /// </summary> template <template <class...> class Container, class... Args, size_t L> std::ostream &operator<<(std::ostream &os, const std::array<Container<Args...>, L> &container) { for (const auto &elem : container) os << elem << settings::sepOfContainer; return os; } /// <summary> /// for cout Container in Container with std::array /// </summary> template <template <class...> class Container, class... Args, size_t L, class T> std::ostream & operator<<(std::ostream &os, const Container<std::array<T, L>, Args...> &container) { for (const auto &elem : container) os << elem << settings::sepOfContainer; return os; } /// <summary> /// for cout Container in Container with std::array /// </summary> template <class T, size_t L1, size_t L2> std::ostream &operator<<(std::ostream &os, const std::array<std::array<T, L2>, L1> container) { for (const auto &elem : container) os << elem << settings::sepOfContainer; return os; } /// <summary> /// for cout raw ary in raw ary /// </summary> template <class T, size_t L1, size_t L2> std::ostream &operator<<(std::ostream &os, const T (&ary)[L1][L2]) { for (size_t i{}; i < L1; ++i) os << ary[i] << settings::sepOfContainer; return os; } } // namespace out } // namespace container_io template <typename T, size_t L, typename Function> void for_each(T (&ary)[L], Function lambda) { for (size_t i{}; i < L; ++i) lambda(static_cast<T &>(ary[i])); } } // namespace extension inline namespace util { template <typename T> void SetAll(T &container, typename T::value_type value = typename T::value_type{}) { for (typename T::reference content : container) content = value; } template <typename T, size_t L> void SetAll(T (&ary)[L], T value) { for (size_t i{}; i < L; ++i) ary[i] = value; } template <typename T, typename U, size_t L1, size_t L2> void SetAll(T (&ary)[L1][L2], U value) { for (size_t i{}; i < L1; ++i) SetAll(ary[i], value); } inline namespace ranges { namespace core { using namespace meta; /// <summary> /// 再帰上限回数 /// </summary> constexpr size_t recursion_limits{490}; /// <summary> /// 値が割り切れるか /// </summary> template <int num, int divide> struct is_divisible : coming::bool_constant<num % divide == 0> {}; /// <summary> /// 有限回数の再帰で終わるか /// </summary> template <int start, int end, int step> struct is_finite_recursion_depth : coming::bool_constant<(start < end) && (step > 0) || (end < start) && (step < 0)> {}; /// <summary> /// 再帰回数が上限を超えないか /// </summary> template <int start, int end, int step> struct is_valid_recursion_depth : coming::bool_constant<(end - start) / step <= recursion_limits> {}; /// <summary> /// 上記メタ関数の条件をすべて満たすか /// </summary> template <int start, int end, int step> struct is_valid_range : coming::bool_constant< (start == end) || is_finite_recursion_depth<start, end, step>::value && is_valid_recursion_depth<start, end, step>::value && is_divisible<end - start, step>::value> {}; /// <summary> /// 再帰を続けるか /// </summary> template <int start, int end, int step> struct is_count_up_or_down : coming::bool_constant<is_valid_range<start, end, step>::value && end != start> {}; ///// <summary> ///// 初期化子リストからstd::arrayをコンパイル時生成 ///// </summary> // template<typename T, typename ...Args> // constexpr std::array<T, sizeof...(Args)> make_array(Args&&... args) //{ // return std::array<T, sizeof...(Args)>{ static_cast<Args&&>(args)... }; // } /// <summary> /// 本体 /// </summary> // エラー処理 template <int start, int end, int step, class = void, int... Ary> struct Range_Impl { // static_assert(is_valid_range <start, end, step>::value, "[ ! ] // invalid teplate argments [ ! ] 値正しくないっぽいヨ!"); static_assert(is_divisible<end - start, step>::value, "[ ! ] (end - start) is indivisible by step number [ ! ] " "割り切れないヨ!stepの値確認して!"); static_assert(is_finite_recursion_depth<start, end, step>::value, "[ ! ] goes to infinite loop [ ! ] " "無限ループなっちゃうヨ!stepの符号逆じゃない?"); static_assert(is_valid_recursion_depth<start, end, step>::value, "[ ! ] recursion depth is out of maximum limit [ ! ] " "再帰回数が大きくなりすぎィ壊れちゃううう"); }; // 再帰 template <int start, int end, int step, int... Ary> struct Range_Impl< start, end, step, meta::coming::enable_if_t<is_count_up_or_down<start, end, step>::value>, Ary...> : Range_Impl<start + step, end, step, void, Ary..., start> {}; // 終端 template <int start, int end, int step, int... Ary> struct Range_Impl<start, end, step, meta::coming::enable_if_t<start == end>, Ary...> { // static constexpr auto value = make_array<int>(Ary ..., end); // static constexpr std::array<int, (sizeof... (Ary) + 1)> value = {Ary ..., // end}; static constexpr int value[(sizeof...(Ary) + 1)] = {Ary..., end}; }; } // namespace core template <size_t size, bool isReverse = false> struct Iterate { static constexpr auto in = isReverse ? core::Range_Impl<static_cast<int>(size - 1), 0, -1>::value : core::Range_Impl<0, static_cast<int>(size - 1), 1>::value; }; template <unsigned int upTo> struct CountUp { static constexpr auto to = core::Range_Impl<0, static_cast<int>(upTo), 1>::value; }; template <unsigned int downFrom> struct CountDown { static constexpr auto from = core::Range_Impl<static_cast<int>(downFrom), 0, -1>::value; }; template <int start, int containedEnd, int step = 0, typename = void> struct Range { static constexpr auto &in = core::Range_Impl<start, containedEnd, step>::value; }; template <int start, int containedEnd> struct Range<start, containedEnd, 0> { static constexpr auto &in = core::Range_Impl < start, containedEnd, meta::is_positive<int, containedEnd - start>::value ? 1 : -1 > ::value; }; } // namespace ranges } // namespace util inline namespace alg { bool IsPrime(const Ull n) { switch (n) { case 0: case 1: return false; case 2: return true; default: if (n % 2 == 0) return false; for (Ull i{3}; i * i <= n; i += 2) { if (n % i == 0) return false; } } return true; } // 最大公約数 Ull EuclideanAlg(Ull n1, Ull n2) { // if (n1 < 0 || n2 < 0) return 0; if (n2 > n1) std::swap(n1, n2); Ull r; while ((r = n1 % n2) != 0) { n1 = n2; n2 = r; } return n2; } // 最小公倍数 Ull LCM(const std::vector<Ull> &values) { if (values.size() == 0) return 0; if (values.size() == 1) return values[0]; if (values.size() == 2) return values[0] * values[1] / EuclideanAlg(values[0], values[1]); auto mid = values.cbegin() + values.size() / 2; return LCM(std::vector<Ull>{LCM(std::vector<Ull>(values.begin(), mid)), LCM(std::vector<Ull>(mid, values.end()))}); } } // namespace alg void Solve(); } // namespace #if true int main() { Solve(); return 0; } #endif namespace { bool isShortPhrase(const std::string &phrases) { constexpr Ary<char, 5> eachLength = {5, 7, 5, 7, 7}; int sum{}, current{}; for (int i = 0; i < phrases.size(); ++i) { sum += (int)phrases[i]; if (sum == eachLength[current]) { if (current == eachLength.size() - 1) return true; ++current; sum = 0; } } return false; } void Solve() { std::ofstream ofs("output.txt"); std::cout.rdbuf(ofs.rdbuf()); int length; while (std::cin >> length, length != 0) { std::string phrases(40, '\0'); for (int i{}; i < length; ++i) { std::string temp; std::cin >> temp; phrases[i] = temp.length(); } for (int i{}; i < length - 4; ++i) { if (isShortPhrase(phrases.substr(i, phrases.size() - i))) { std::cout << i + 1 << std::endl; break; } } } } } // namespace
#ifdef _MSVC_LANG #define _SCL_SECURE_NO_WARNINGS #pragma error(disable : 0325) // inline namespace Error #endif #include <algorithm> #include <array> #include <bitset> #include <cstdio> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <random> #include <string> #include <unordered_set> #include <vector> // C++ 11 前提 using Ull = unsigned long long; using Ll = long long; template <typename T> using Vec = std::vector<T>; template <typename T, size_t L> using Ary = std::array<T, L>; namespace { namespace meta { namespace coming { // c++ 17; template <class... _Types> using void_t = void; template <class _Ty, _Ty _Val> struct integral_constant { // convenient template for integral constant types static constexpr _Ty value = _Val; using value_type = _Ty; using type = integral_constant; constexpr operator value_type() const noexcept { // return stored value return (value); } constexpr value_type operator()() const noexcept { // return stored value return (value); } }; // c++ 17; template <bool _Val> using bool_constant = integral_constant<bool, _Val>; // c++ 14 template <bool _Test, class _Ty = void> using enable_if_t = typename std::enable_if<_Test, _Ty>::type; } // namespace coming namespace core { template <class, class = void> struct enable_std_begin_end : std::false_type {}; template <typename T> struct enable_std_begin_end< T, coming::void_t<decltype(std::begin(std::declval<const T &>()), std::end(std::declval<const T &>()))>> : std::true_type {}; // c++ 17 template <class, class = void> struct enable_adl_begin_end : std::false_type {}; template <typename T> struct enable_adl_begin_end< T, coming::void_t<decltype(begin(std::declval<const T &>()), end(std::declval<const T &>()))>> : std::true_type {}; } // namespace core template <typename T> struct is_range : coming::bool_constant<core::enable_std_begin_end<T>::value || core::enable_adl_begin_end<T>::value> { }; /// <summary> /// ある値が0以上か /// </summary> template <class T, T num, class = std::nullptr_t> struct is_positive : coming::bool_constant<(num >= 0)> {}; } // namespace meta inline namespace extension { inline namespace container_io { inline namespace in { /// <summary> /// for cin with STL container /// </summary> template <template <class...> class Container, class... Args, typename = meta::coming::enable_if_t< !std::is_same<Container<Args...>, std::string>::value>> std::istream &operator>>(std::istream &is, Container<Args...> &container) { for (auto &elem : container) is >> elem; return is; } /// <summary> /// for cin with std::arary /// </summary> template <class T, size_t L> std::istream &operator>>(std::istream &is, std::array<T, L> &container) { for (auto &elem : container) is >> elem; return is; } /// <summary> /// for cin with raw array /// </summary> template <class T, size_t L> std::istream &operator>>(std::istream &is, T (&ary)[L]) { for (int i{}; i < L; ++i) is >> ary[i]; return is; } } // namespace in inline namespace out { namespace settings { /// <summary> /// 要素間の区切り文字 /// </summary> const auto sepOfContainerElements{" "}; /// <summary> /// コンテナ間の区切り文字 /// </summary> const auto sepOfContainer{"\n"}; } // namespace settings /// <summary> /// for cout Container with STL container /// </summary> template <template <class...> class Container, class... Args, typename = meta::coming::enable_if_t< !std::is_same<Container<Args...>, std::string>::value>> std::ostream &operator<<(std::ostream &os, const Container<Args...> &container) { for (const auto &elem : container) os << elem << settings::sepOfContainerElements; return os; } /// <summary> /// for cout Container with std::array /// </summary> template <class T, size_t L> std::ostream &operator<<(std::ostream &os, const std::array<T, L> container) { for (const auto &elem : container) os << elem << settings::sepOfContainerElements; return os; } /// <summary> /// for cout Container with raw array /// </summary> template <class T, size_t L, typename = meta::coming::enable_if_t<!std::is_same<T, char>::value>> std::ostream &operator<<(std::ostream &os, const T (&ary)[L]) { for (const auto &elem : ary) os << elem << settings::sepOfContainerElements; return os; } /// <summary> /// for cout Container in Container with STL container /// </summary> template <template <class...> class Container1, class... Cont1Args, template <class...> class Container2, class... Cont2Args> std::ostream &operator<<( std::ostream &os, const Container1<Container2<Cont2Args...>, Cont1Args...> &container) { for (const auto &elem : container) os << elem << settings::sepOfContainer; return os; } /// <summary> /// for cout Container in Container with std::array /// </summary> template <template <class...> class Container, class... Args, size_t L> std::ostream &operator<<(std::ostream &os, const std::array<Container<Args...>, L> &container) { for (const auto &elem : container) os << elem << settings::sepOfContainer; return os; } /// <summary> /// for cout Container in Container with std::array /// </summary> template <template <class...> class Container, class... Args, size_t L, class T> std::ostream & operator<<(std::ostream &os, const Container<std::array<T, L>, Args...> &container) { for (const auto &elem : container) os << elem << settings::sepOfContainer; return os; } /// <summary> /// for cout Container in Container with std::array /// </summary> template <class T, size_t L1, size_t L2> std::ostream &operator<<(std::ostream &os, const std::array<std::array<T, L2>, L1> container) { for (const auto &elem : container) os << elem << settings::sepOfContainer; return os; } /// <summary> /// for cout raw ary in raw ary /// </summary> template <class T, size_t L1, size_t L2> std::ostream &operator<<(std::ostream &os, const T (&ary)[L1][L2]) { for (size_t i{}; i < L1; ++i) os << ary[i] << settings::sepOfContainer; return os; } } // namespace out } // namespace container_io template <typename T, size_t L, typename Function> void for_each(T (&ary)[L], Function lambda) { for (size_t i{}; i < L; ++i) lambda(static_cast<T &>(ary[i])); } } // namespace extension inline namespace util { template <typename T> void SetAll(T &container, typename T::value_type value = typename T::value_type{}) { for (typename T::reference content : container) content = value; } template <typename T, size_t L> void SetAll(T (&ary)[L], T value) { for (size_t i{}; i < L; ++i) ary[i] = value; } template <typename T, typename U, size_t L1, size_t L2> void SetAll(T (&ary)[L1][L2], U value) { for (size_t i{}; i < L1; ++i) SetAll(ary[i], value); } inline namespace ranges { namespace core { using namespace meta; /// <summary> /// 再帰上限回数 /// </summary> constexpr size_t recursion_limits{490}; /// <summary> /// 値が割り切れるか /// </summary> template <int num, int divide> struct is_divisible : coming::bool_constant<num % divide == 0> {}; /// <summary> /// 有限回数の再帰で終わるか /// </summary> template <int start, int end, int step> struct is_finite_recursion_depth : coming::bool_constant<(start < end) && (step > 0) || (end < start) && (step < 0)> {}; /// <summary> /// 再帰回数が上限を超えないか /// </summary> template <int start, int end, int step> struct is_valid_recursion_depth : coming::bool_constant<(end - start) / step <= recursion_limits> {}; /// <summary> /// 上記メタ関数の条件をすべて満たすか /// </summary> template <int start, int end, int step> struct is_valid_range : coming::bool_constant< (start == end) || is_finite_recursion_depth<start, end, step>::value && is_valid_recursion_depth<start, end, step>::value && is_divisible<end - start, step>::value> {}; /// <summary> /// 再帰を続けるか /// </summary> template <int start, int end, int step> struct is_count_up_or_down : coming::bool_constant<is_valid_range<start, end, step>::value && end != start> {}; ///// <summary> ///// 初期化子リストからstd::arrayをコンパイル時生成 ///// </summary> // template<typename T, typename ...Args> // constexpr std::array<T, sizeof...(Args)> make_array(Args&&... args) //{ // return std::array<T, sizeof...(Args)>{ static_cast<Args&&>(args)... }; // } /// <summary> /// 本体 /// </summary> // エラー処理 template <int start, int end, int step, class = void, int... Ary> struct Range_Impl { // static_assert(is_valid_range <start, end, step>::value, "[ ! ] // invalid teplate argments [ ! ] 値正しくないっぽいヨ!"); static_assert(is_divisible<end - start, step>::value, "[ ! ] (end - start) is indivisible by step number [ ! ] " "割り切れないヨ!stepの値確認して!"); static_assert(is_finite_recursion_depth<start, end, step>::value, "[ ! ] goes to infinite loop [ ! ] " "無限ループなっちゃうヨ!stepの符号逆じゃない?"); static_assert(is_valid_recursion_depth<start, end, step>::value, "[ ! ] recursion depth is out of maximum limit [ ! ] " "再帰回数が大きくなりすぎィ壊れちゃううう"); }; // 再帰 template <int start, int end, int step, int... Ary> struct Range_Impl< start, end, step, meta::coming::enable_if_t<is_count_up_or_down<start, end, step>::value>, Ary...> : Range_Impl<start + step, end, step, void, Ary..., start> {}; // 終端 template <int start, int end, int step, int... Ary> struct Range_Impl<start, end, step, meta::coming::enable_if_t<start == end>, Ary...> { // static constexpr auto value = make_array<int>(Ary ..., end); // static constexpr std::array<int, (sizeof... (Ary) + 1)> value = {Ary ..., // end}; static constexpr int value[(sizeof...(Ary) + 1)] = {Ary..., end}; }; } // namespace core template <size_t size, bool isReverse = false> struct Iterate { static constexpr auto in = isReverse ? core::Range_Impl<static_cast<int>(size - 1), 0, -1>::value : core::Range_Impl<0, static_cast<int>(size - 1), 1>::value; }; template <unsigned int upTo> struct CountUp { static constexpr auto to = core::Range_Impl<0, static_cast<int>(upTo), 1>::value; }; template <unsigned int downFrom> struct CountDown { static constexpr auto from = core::Range_Impl<static_cast<int>(downFrom), 0, -1>::value; }; template <int start, int containedEnd, int step = 0, typename = void> struct Range { static constexpr auto &in = core::Range_Impl<start, containedEnd, step>::value; }; template <int start, int containedEnd> struct Range<start, containedEnd, 0> { static constexpr auto &in = core::Range_Impl < start, containedEnd, meta::is_positive<int, containedEnd - start>::value ? 1 : -1 > ::value; }; } // namespace ranges } // namespace util inline namespace alg { bool IsPrime(const Ull n) { switch (n) { case 0: case 1: return false; case 2: return true; default: if (n % 2 == 0) return false; for (Ull i{3}; i * i <= n; i += 2) { if (n % i == 0) return false; } } return true; } // 最大公約数 Ull EuclideanAlg(Ull n1, Ull n2) { // if (n1 < 0 || n2 < 0) return 0; if (n2 > n1) std::swap(n1, n2); Ull r; while ((r = n1 % n2) != 0) { n1 = n2; n2 = r; } return n2; } // 最小公倍数 Ull LCM(const std::vector<Ull> &values) { if (values.size() == 0) return 0; if (values.size() == 1) return values[0]; if (values.size() == 2) return values[0] * values[1] / EuclideanAlg(values[0], values[1]); auto mid = values.cbegin() + values.size() / 2; return LCM(std::vector<Ull>{LCM(std::vector<Ull>(values.begin(), mid)), LCM(std::vector<Ull>(mid, values.end()))}); } } // namespace alg void Solve(); } // namespace #if true int main() { Solve(); return 0; } #endif namespace { bool isShortPhrase(const std::string &phrases) { constexpr Ary<char, 5> eachLength = {5, 7, 5, 7, 7}; int sum{}, current{}; for (int i = 0; i < phrases.size(); ++i) { sum += (int)phrases[i]; if (sum == eachLength[current]) { if (current == eachLength.size() - 1) return true; ++current; sum = 0; } } return false; } void Solve() { int length; while (std::cin >> length, length != 0) { std::string phrases(40, '\0'); for (int i{}; i < length; ++i) { std::string temp; std::cin >> temp; phrases[i] = temp.length(); } for (int i{}; i < length - 4; ++i) { if (isShortPhrase(phrases.substr(i, phrases.size() - i))) { std::cout << i + 1 << std::endl; break; } } } } } // namespace
delete
474
476
474
474
0
p01086
C++
Runtime Error
// // main.cpp // Short Phrase // // Created by x15071xx on 2017/06/18. // Copyright ?? 2017??´ AIT. All rights reserved. // #include <cstdio> #include <iostream> #include <string> #include <vector> using namespace std; int main(int argc, const char *argv[]) { while (1) { int n, i, j; vector<string> str; cin >> n; if (n == 0) { break; } string tmp; for (i = 0; i < n; i++) { cin >> tmp; str.push_back(tmp); } for (i = 0; i < n; i++) { int flag = 0; j = i; int numsum = 0; while (1) { if (flag == -1) { break; } if (flag == 5) { cout << i + 1 << endl; break; } numsum += str[i + j].length(); // if (i == 5) { // cout << numsum << str[i+j] <<endl; // } if (flag == 0 || flag == 2) { if (numsum > 5) { flag = -1; } else if (numsum < 5) { j++; } else if (numsum == 5) { flag++; numsum = 0; j++; } } else { if (numsum > 7) { flag = -1; } else if (numsum < 7) { j++; } else if (numsum == 7) { flag++; numsum = 0; j++; } } } if (flag == 5) { break; } } } return 0; }
// // main.cpp // Short Phrase // // Created by x15071xx on 2017/06/18. // Copyright ?? 2017??´ AIT. All rights reserved. // #include <cstdio> #include <iostream> #include <string> #include <vector> using namespace std; int main(int argc, const char *argv[]) { while (1) { int n, i, j; vector<string> str; cin >> n; if (n == 0) { break; } string tmp; for (i = 0; i < n; i++) { cin >> tmp; str.push_back(tmp); } for (i = 0; i < n; i++) { int flag = 0; j = 0; int numsum = 0; while (1) { if (flag == -1) { break; } if (flag == 5) { cout << i + 1 << endl; break; } numsum += str[i + j].length(); // if (i == 5) { // cout << numsum << str[i+j] <<endl; // } if (flag == 0 || flag == 2) { if (numsum > 5) { flag = -1; } else if (numsum < 5) { j++; } else if (numsum == 5) { flag++; numsum = 0; j++; } } else { if (numsum > 7) { flag = -1; } else if (numsum < 7) { j++; } else if (numsum == 7) { flag++; numsum = 0; j++; } } } if (flag == 5) { break; } } } return 0; }
replace
34
35
34
35
-11
p01086
C++
Time Limit Exceeded
#include <cstdio> #include <cstring> int n, len[40]; char word[11]; int solve(int pos) { int rest; rest = 5; while (rest > 0) { rest -= len[pos++]; if (rest < 0) return 0; } rest = 7; while (rest > 0) { rest -= len[pos++]; if (rest < 0) return 0; } rest = 5; while (rest > 0) { rest -= len[pos++]; if (rest < 0) return 0; } rest = 7; while (rest > 0) { rest -= len[pos++]; if (rest < 0) return 0; } rest = 7; while (rest > 0) { rest -= len[pos++]; if (rest < 0) return 0; } return 1; } int main() { while (1) { scanf("%d\n", &n); for (int i = 0; i < n; i++) { scanf("%s\n", word); len[i] = strlen(word); } for (int j = 0; j < n; j++) { if (solve(j)) { printf("%d\n", j + 1); break; } } } }
#include <cstdio> #include <cstring> int n, len[40]; char word[11]; int solve(int pos) { int rest; rest = 5; while (rest > 0) { rest -= len[pos++]; if (rest < 0) return 0; } rest = 7; while (rest > 0) { rest -= len[pos++]; if (rest < 0) return 0; } rest = 5; while (rest > 0) { rest -= len[pos++]; if (rest < 0) return 0; } rest = 7; while (rest > 0) { rest -= len[pos++]; if (rest < 0) return 0; } rest = 7; while (rest > 0) { rest -= len[pos++]; if (rest < 0) return 0; } return 1; } int main() { while (1) { scanf("%d\n", &n); if (n == 0) break; for (int i = 0; i < n; i++) { scanf("%s\n", word); len[i] = strlen(word); } for (int j = 0; j < n; j++) { if (solve(j)) { printf("%d\n", j + 1); break; } } } }
insert
44
44
44
46
TLE