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
p03182
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef struct line { int lp, rp, value; bool operator<(const struct line &n) const { return rp < n.rp; } } LINE, *PLINE; LINE arr[200000]; long long linetree[1 << 19], lazy[1 << 19]; long long query(int index, int lb, int rb, int lq, int rq) { if (lb == lq && rb == rq) return linetree[index]; long long mid = (lb + rb) >> 1; if (lazy[index] != 0) { linetree[index << 1] += lazy[index]; linetree[index << 1 | 1] += lazy[index]; lazy[index << 1] += lazy[index]; lazy[index << 1 | 1] += lazy[index]; lazy[index] = 0; } if (rq <= mid) { return query(index << 1, lb, mid, lq, rq); } else if (lq > mid) { return query(index << 1 | 1, mid + 1, rb, lq, rq); } else { return max(query(index << 1, lb, mid, lq, mid), query(index << 1 | 1, mid + 1, rb, mid + 1, rq)); } } void update(int index, int lb, int rb, int lq, int rq, long long value) { if (lb == lq && rb == rq) { linetree[index] += value; lazy[index] += value; return; } long long mid = (lb + rb) >> 1; if (lazy[index] != 0) { linetree[index << 1] += lazy[index]; linetree[index << 1 | 1] += lazy[index]; lazy[index << 1] += lazy[index]; lazy[index << 1 | 1] += lazy[index]; lazy[index] = 0; } if (rq <= mid) { update(index << 1, lb, mid, lq, rq, value); } else if (lq > mid) { update(index << 1 | 1, mid + 1, rb, lq, rq, value); } else { update(index << 1, lb, mid, lq, mid, value); update(index << 1 | 1, mid + 1, rb, mid + 1, rq, value); } linetree[index] = max(linetree[index << 1], linetree[index << 1 | 1]); } int main() { int i, j, k, cnt = 0; int n, m; scanf("%d %d", &n, &m); for (i = 0; i < m; i++) { scanf("%d %d %d", &arr[i].lp, &arr[i].rp, &arr[i].value); } sort(arr, arr + m); for (i = 0; i <= n; i++) { update(1, 0, n, i, i, query(1, 0, n, 0, i - 1)); while (arr[cnt].rp == i) { update(1, 0, n, arr[cnt].lp, arr[cnt].rp, arr[cnt].value); cnt++; } } printf("%lld", linetree[1]); }
#include <bits/stdc++.h> using namespace std; typedef struct line { int lp, rp, value; bool operator<(const struct line &n) const { return rp < n.rp; } } LINE, *PLINE; LINE arr[200000]; long long linetree[1 << 19], lazy[1 << 19]; long long query(int index, int lb, int rb, int lq, int rq) { if (lb == lq && rb == rq) return linetree[index]; long long mid = (lb + rb) >> 1; if (lazy[index] != 0) { linetree[index << 1] += lazy[index]; linetree[index << 1 | 1] += lazy[index]; lazy[index << 1] += lazy[index]; lazy[index << 1 | 1] += lazy[index]; lazy[index] = 0; } if (rq <= mid) { return query(index << 1, lb, mid, lq, rq); } else if (lq > mid) { return query(index << 1 | 1, mid + 1, rb, lq, rq); } else { return max(query(index << 1, lb, mid, lq, mid), query(index << 1 | 1, mid + 1, rb, mid + 1, rq)); } } void update(int index, int lb, int rb, int lq, int rq, long long value) { if (lb == lq && rb == rq) { linetree[index] += value; lazy[index] += value; return; } long long mid = (lb + rb) >> 1; if (lazy[index] != 0) { linetree[index << 1] += lazy[index]; linetree[index << 1 | 1] += lazy[index]; lazy[index << 1] += lazy[index]; lazy[index << 1 | 1] += lazy[index]; lazy[index] = 0; } if (rq <= mid) { update(index << 1, lb, mid, lq, rq, value); } else if (lq > mid) { update(index << 1 | 1, mid + 1, rb, lq, rq, value); } else { update(index << 1, lb, mid, lq, mid, value); update(index << 1 | 1, mid + 1, rb, mid + 1, rq, value); } linetree[index] = max(linetree[index << 1], linetree[index << 1 | 1]); } int main() { int i, j, k, cnt = 0; int n, m; scanf("%d %d", &n, &m); for (i = 0; i < m; i++) { scanf("%d %d %d", &arr[i].lp, &arr[i].rp, &arr[i].value); } sort(arr, arr + m); for (i = 1; i <= n; i++) { update(1, 0, n, i, i, query(1, 0, n, 0, i - 1)); while (arr[cnt].rp == i) { update(1, 0, n, arr[cnt].lp, arr[cnt].rp, arr[cnt].value); cnt++; } } printf("%lld", linetree[1]); }
replace
60
61
60
61
-11
p03182
C++
Runtime Error
#include <bits/stdc++.h> // using namespace std; #pragma GCC target("avx") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #define rep(i, j, n) for (ll i = (ll)(j); i < (ll)(n); i++) #define REP(i, j, n) for (ll i = (ll)(j); i <= (ll)(n); i++) #define per(i, j, n) for (ll i = (ll)(j); (ll)(n) <= i; i--) #define ll long long #define ALL(a) (a).begin(), (a).end() #define disup(A, key) distance(A.begin(), upper_bound(ALL(A), (ll)(key))) #define dislow(A, key) distance(A.begin(), lower_bound(ALL(A), (ll)(key))) #define pb emplace_back #define mp std::make_pair // #define endl "\n" // using std::endl; using std::cin; using std::cout; using std::lower_bound; using std::string; using std::upper_bound; using std::vector; using vi = vector<ll>; using vii = vector<vi>; using pii = std::pair<ll, ll>; // constexpr ll MOD = 1e9 + 7; // constexpr ll MOD=998244353; // constexpr ll MOD=10000000; // constexpr ll MOD=1e4; constexpr ll MAX = 3e6; constexpr ll inf = (1ll << 60); template <class T> class prique : public std::priority_queue<T, std::vector<T>, std::greater<T>> { }; template <typename T> struct Segment_tree { ll N; T mem; vector<T> node; Segment_tree(vector<T> &X, T m) : mem(m) { ll sz = X.size(); N = 1; while (N < sz) N *= 2; node.resize(2 * N - 1, mem); rep(i, 0, sz) node[N - 1 + i] = X[i]; per(i, N - 2, 0) { node[i] = Compare(node[i * 2 + 1], node[i * 2 + 2]); } } T Compare(T &A, T &B) { return std::min(A, B); } void update(ll X, T val) { X += N - 1; node[X] = val; while (X > 0) { X = (X - 1) / 2; node[X] = Compare(node[X * 2 + 1], node[X * 2 + 2]); } } T Query(ll a, ll b, ll now, ll l, ll r) { //[a,b),[l,r) if (r < 0) r = N; if (r <= a || b <= l) return mem; if (a <= l && r <= b) return node[now]; auto vl = Query(a, b, now * 2 + 1, l, (l + r) / 2), vr = Query(a, b, now * 2 + 2, (l + r) / 2, r); return Compare(vl, vr); } }; struct Tree { int N; vii dp; vi dist; Tree(vii edge) { N = edge.size(); dp.resize(N); dist.resize(N, -1); for (int i = 0; i < N; i++) dp[i].resize(30); dist[0] = dp[0][0] = 0; std::queue<int> que; que.push(0); while (!que.empty()) { int now = que.front(); que.pop(); for (int i = 0; i < edge[now].size(); i++) { int next = edge[now][i]; if (dist[next] == -1) { dist[next] = dist[now] + 1; que.push(next); dp[next][0] = now; } } } for (int i = 1; i < 30; i++) { for (int j = 0; j < N; j++) dp[j][i] = dp[dp[j][i - 1]][i - 1]; } } int LCA(int X, int Y) { if (dist[X] < dist[Y]) std::swap(X, Y); { int Z = dist[X] - dist[Y]; for (int i = 0; i < 30; i++) { if (Z & (1 << i)) { X = dp[X][i]; } } } if (X == Y) return X; for (int i = 29; i >= 0; i--) { if (dp[X][i] != dp[Y][i]) { X = dp[X][i]; Y = dp[Y][i]; } } return dp[X][0]; } }; struct Binary_indexed_tree { int N; vi bit; Binary_indexed_tree(int n) : N(n) { bit.resize(N + 1, 0); } void add(int x, ll a) { for (x; x <= N; x += (x & -x)) bit[x] += a; } ll sum(int x) { ll ret = 0; for (x; x > 0; x -= (x & -x)) ret += bit[x]; return ret; } ll lower_bound(ll X) { if (sum(N) < X) return -1; ll ret = 0, memo = 1, sum = 0; while (memo * 2 <= N) memo *= 2; while (memo > 0) { if (memo + ret <= N && sum + bit[memo + ret] < X) { sum += bit[memo + ret]; ret += memo; } memo /= 2; } return ret + 1; } }; struct Union_Find { ll N; vi par; vi siz; Union_Find(int n) : N(n) { par.resize(N); siz.resize(N, 1); rep(i, 0, N) par[i] = i; } ll root(ll X) { if (par[X] == X) return X; return par[X] = root(par[X]); } bool same(ll X, ll Y) { return root(X) == root(Y); } void unite(ll X, ll Y) { X = root(X); Y = root(Y); if (X == Y) return; par[X] = Y; siz[Y] += siz[X]; siz[X] = 0; } ll size(ll X) { return siz[root(X)]; } }; long long modpow(long long a, long long n, long long mod) { long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } vi fac, finv, inv; void COMinit() { fac.resize(MAX); finv.resize(MAX); inv.resize(MAX); fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } ll COM(ll n, ll r) { if (n < r || n < 0 || r < 0) return 0; return fac[n] * finv[r] % MOD * finv[n - r] % MOD; } void comp(vi &A) { std::map<ll, ll> memo; rep(i, 0, A.size()) memo[A[i]] = 0; ll cnt = 0; for (auto &p : memo) p.second = cnt++; rep(i, 0, A.size()) A[i] = memo[A[i]]; } void update(ll right, ll plus, ll N, ll m, vi &dp, vi &max, vi &sum) { rep(i, 0, right / m) { max[i] += plus; sum[i] += plus; } max[right / m] = -inf; REP(i, right / m * m, right) dp[i] += plus; rep(i, right / m * m, std::min(N + 1, right / m * m + m)) { max[i / m] = std::max(max[i / m], dp[i] + sum[i / m]); } } ll Query(ll right, ll N, ll m, vi &dp, vi &max, vi &sum) { ll ret = -inf; rep(i, 0, right / m) ret = std::max(ret, max[i]); REP(i, right / m * m, right) ret = std::max(ret, dp[i] + sum[i / m]); return ret; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); ll N, M; cin >> N >> M; vector<vector<pii>> memo(N + 2); rep(i, 0, M) { ll X, Y, Z; cin >> X >> Y >> Z; memo[X].pb(mp(X - 1, Z)); memo[Y + 1].pb(mp(X - 1, -Z)); } vi dp(N + 1, -inf); dp[0] = 0; ll m = sqrt(N); vi max(m * 3 + 1, -inf), sum(m * 3 + 1); max[0] = 0; m /= 2; ll ans = 0; REP(i, 1, N) { rep(j, 0, memo[i].size()) { update(memo[i][j].first, memo[i][j].second, N, m, dp, max, sum); } dp[i] = Query(i - 1, N, m, dp, max, sum); max[i / m] = std::max(dp[i], max[i / m]); ans = std::max(ans, dp[i]); } cout << ans << endl; }
#include <bits/stdc++.h> // using namespace std; #pragma GCC target("avx") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #define rep(i, j, n) for (ll i = (ll)(j); i < (ll)(n); i++) #define REP(i, j, n) for (ll i = (ll)(j); i <= (ll)(n); i++) #define per(i, j, n) for (ll i = (ll)(j); (ll)(n) <= i; i--) #define ll long long #define ALL(a) (a).begin(), (a).end() #define disup(A, key) distance(A.begin(), upper_bound(ALL(A), (ll)(key))) #define dislow(A, key) distance(A.begin(), lower_bound(ALL(A), (ll)(key))) #define pb emplace_back #define mp std::make_pair // #define endl "\n" // using std::endl; using std::cin; using std::cout; using std::lower_bound; using std::string; using std::upper_bound; using std::vector; using vi = vector<ll>; using vii = vector<vi>; using pii = std::pair<ll, ll>; // constexpr ll MOD = 1e9 + 7; // constexpr ll MOD=998244353; // constexpr ll MOD=10000000; // constexpr ll MOD=1e4; constexpr ll MAX = 3e6; constexpr ll inf = (1ll << 60); template <class T> class prique : public std::priority_queue<T, std::vector<T>, std::greater<T>> { }; template <typename T> struct Segment_tree { ll N; T mem; vector<T> node; Segment_tree(vector<T> &X, T m) : mem(m) { ll sz = X.size(); N = 1; while (N < sz) N *= 2; node.resize(2 * N - 1, mem); rep(i, 0, sz) node[N - 1 + i] = X[i]; per(i, N - 2, 0) { node[i] = Compare(node[i * 2 + 1], node[i * 2 + 2]); } } T Compare(T &A, T &B) { return std::min(A, B); } void update(ll X, T val) { X += N - 1; node[X] = val; while (X > 0) { X = (X - 1) / 2; node[X] = Compare(node[X * 2 + 1], node[X * 2 + 2]); } } T Query(ll a, ll b, ll now, ll l, ll r) { //[a,b),[l,r) if (r < 0) r = N; if (r <= a || b <= l) return mem; if (a <= l && r <= b) return node[now]; auto vl = Query(a, b, now * 2 + 1, l, (l + r) / 2), vr = Query(a, b, now * 2 + 2, (l + r) / 2, r); return Compare(vl, vr); } }; struct Tree { int N; vii dp; vi dist; Tree(vii edge) { N = edge.size(); dp.resize(N); dist.resize(N, -1); for (int i = 0; i < N; i++) dp[i].resize(30); dist[0] = dp[0][0] = 0; std::queue<int> que; que.push(0); while (!que.empty()) { int now = que.front(); que.pop(); for (int i = 0; i < edge[now].size(); i++) { int next = edge[now][i]; if (dist[next] == -1) { dist[next] = dist[now] + 1; que.push(next); dp[next][0] = now; } } } for (int i = 1; i < 30; i++) { for (int j = 0; j < N; j++) dp[j][i] = dp[dp[j][i - 1]][i - 1]; } } int LCA(int X, int Y) { if (dist[X] < dist[Y]) std::swap(X, Y); { int Z = dist[X] - dist[Y]; for (int i = 0; i < 30; i++) { if (Z & (1 << i)) { X = dp[X][i]; } } } if (X == Y) return X; for (int i = 29; i >= 0; i--) { if (dp[X][i] != dp[Y][i]) { X = dp[X][i]; Y = dp[Y][i]; } } return dp[X][0]; } }; struct Binary_indexed_tree { int N; vi bit; Binary_indexed_tree(int n) : N(n) { bit.resize(N + 1, 0); } void add(int x, ll a) { for (x; x <= N; x += (x & -x)) bit[x] += a; } ll sum(int x) { ll ret = 0; for (x; x > 0; x -= (x & -x)) ret += bit[x]; return ret; } ll lower_bound(ll X) { if (sum(N) < X) return -1; ll ret = 0, memo = 1, sum = 0; while (memo * 2 <= N) memo *= 2; while (memo > 0) { if (memo + ret <= N && sum + bit[memo + ret] < X) { sum += bit[memo + ret]; ret += memo; } memo /= 2; } return ret + 1; } }; struct Union_Find { ll N; vi par; vi siz; Union_Find(int n) : N(n) { par.resize(N); siz.resize(N, 1); rep(i, 0, N) par[i] = i; } ll root(ll X) { if (par[X] == X) return X; return par[X] = root(par[X]); } bool same(ll X, ll Y) { return root(X) == root(Y); } void unite(ll X, ll Y) { X = root(X); Y = root(Y); if (X == Y) return; par[X] = Y; siz[Y] += siz[X]; siz[X] = 0; } ll size(ll X) { return siz[root(X)]; } }; long long modpow(long long a, long long n, long long mod) { long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } vi fac, finv, inv; void COMinit() { fac.resize(MAX); finv.resize(MAX); inv.resize(MAX); fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } ll COM(ll n, ll r) { if (n < r || n < 0 || r < 0) return 0; return fac[n] * finv[r] % MOD * finv[n - r] % MOD; } void comp(vi &A) { std::map<ll, ll> memo; rep(i, 0, A.size()) memo[A[i]] = 0; ll cnt = 0; for (auto &p : memo) p.second = cnt++; rep(i, 0, A.size()) A[i] = memo[A[i]]; } void update(ll right, ll plus, ll N, ll m, vi &dp, vi &max, vi &sum) { rep(i, 0, right / m) { max[i] += plus; sum[i] += plus; } max[right / m] = -inf; REP(i, right / m * m, right) dp[i] += plus; rep(i, right / m * m, std::min(N + 1, right / m * m + m)) { max[i / m] = std::max(max[i / m], dp[i] + sum[i / m]); } } ll Query(ll right, ll N, ll m, vi &dp, vi &max, vi &sum) { ll ret = -inf; rep(i, 0, right / m) ret = std::max(ret, max[i]); REP(i, right / m * m, right) ret = std::max(ret, dp[i] + sum[i / m]); return ret; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); ll N, M; cin >> N >> M; vector<vector<pii>> memo(N + 2); rep(i, 0, M) { ll X, Y, Z; cin >> X >> Y >> Z; memo[X].pb(mp(X - 1, Z)); memo[Y + 1].pb(mp(X - 1, -Z)); } vi dp(N + 1, -inf); dp[0] = 0; ll m = sqrt(N); vi max(m * 3 + 1, -inf), sum(m * 3 + 1); max[0] = 0; m /= 2; if (m == 0) m++; ll ans = 0; REP(i, 1, N) { rep(j, 0, memo[i].size()) { update(memo[i][j].first, memo[i][j].second, N, m, dp, max, sum); } dp[i] = Query(i - 1, N, m, dp, max, sum); max[i / m] = std::max(dp[i], max[i / m]); ans = std::max(ans, dp[i]); } cout << ans << endl; }
insert
251
251
251
253
0
p03182
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define TemplateVersion "3.7.1" // Useful Marcos //====================START===================== // Compile use C++11 and above #ifdef LOCAL #define debug(args...) \ do { \ string _s = #args; \ replace(_s.begin(), _s.end(), ',', ' '); \ stringstream _ss(_s); \ istream_iterator<string> _it(_ss); \ err(_it, args); \ } while (0) void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << endl; err(++it, args...); } #define MSG cout << "Finished" << endl #else #define debug(args...) #define MSG #endif #if __cplusplus >= 201703L template <typename... Args> void readln(Args &...args) { ((cin >> args), ...); } template <typename... Args> void writeln(Args... args) { ((cout << args << " "), ...); cout << endl; } #elif __cplusplus >= 201103L void readln() {} template <typename T, typename... Args> void readln(T &a, Args &...args) { cin >> a; readln(args...); } void writeln() { cout << endl; } template <typename T, typename... Args> void writeln(T a, Args... args) { cout << a << " "; writeln(args...); } #endif #if __cplusplus >= 201103L #define FOR(_i, _begin, _end) for (auto _i = _begin; _i < _end; _i++) #define FORR(_i, _begin, _end) for (auto _i = _begin; _i > _end; _i--) #else #define FOR(_i, _begin, _end) for (int _i = (int)_begin; _i < (int)_end; _i++) #define FORR(_i, _begin, _end) for (int _i = (int)_begin; _i > (int)_end; _i--) #define nullptr NULL #endif #if __cplusplus >= 201103L #define VIS(_kind, _name, _size) \ vector<_kind> _name(_size); \ for (auto &i : _name) \ cin >> i; #else #define VIS(_kind, _name, _size) \ vector<_kind> _name; \ _name.resize(_size); \ for (int i = 0; i < _size; i++) \ cin >> _name[i]; #endif // alias #define mp make_pair #define pb push_back #define eb emplace_back #define all(x) (x).begin(), (x).end() #define clr(x) memset((x), 0, sizeof(x)) #define infty(x) memset((x), 0x3f, sizeof(x)) #define tcase() \ int T; \ cin >> T; \ FOR(kase, 1, T + 1) // Swap max/min template <typename T> bool smax(T &a, const T &b) { if (a > b) return false; a = b; return true; } template <typename T> bool smin(T &a, const T &b) { if (a < b) return false; a = b; return true; } // ceil divide template <typename T> T cd(T a, T b) { return (a + b - 1) / b; } // min exchange template <typename T> bool se(T &a, T &b) { if (a < b) return false; swap(a, b); return true; } // A better MAX choice const int INF = 0x3f3f3f3f; const long long INFLL = 0x3f3f3f3f3f3f3f3fLL; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<ll> vll; typedef set<int> si; typedef vector<string> cb; //====================END===================== // Constants here const int MAXN = 2e5 + 10; const ll MIN = -3e14; struct Line { int l, r; ll a; } ls[MAXN]; ll seg[MAXN >> 2], flag[MAXN >> 2]; void pushup(int pos) { seg[pos] = max(seg[pos << 1], seg[pos << 1 | 1]); } void pushdown(int pos) { if (flag[pos] != 0) { flag[pos << 1] += flag[pos]; flag[pos << 1 | 1] += flag[pos]; seg[pos << 1] += flag[pos]; seg[pos << 1 | 1] += flag[pos]; flag[pos] = 0; } } void update(int ul, int ur, ll val, int l, int r, int cur) { if (ul <= l && r <= ur) { seg[cur] += val; flag[cur] += val; return; } int m = ((r - l) >> 1) + l; pushdown(cur); if (ul <= m) { update(ul, ur, val, l, m, cur << 1); } if (ur > m) { update(ul, ur, val, m + 1, r, cur << 1 | 1); } pushup(cur); } ll query(int ql, int qr, int l, int r, int cur) { if (ql <= l && r <= qr) { return seg[cur]; } int m = ((r - l) >> 1) + l; pushdown(cur); ll ans = MIN; if (ql <= m) { smax(ans, query(ql, qr, l, m, cur << 1)); } if (qr > m) { smax(ans, query(ql, qr, m + 1, r, cur << 1 | 1)); } return ans; } // Pre-Build Function inline void build() {} // Actual Solver inline void solve() { map<int, vector<int>> s, e; int N, M; readln(N, M); N++; for (int i = 0; i < M; i++) { readln(ls[i].l, ls[i].r, ls[i].a); ls[i].l++, ls[i].r++; s[ls[i].l].pb(i); e[ls[i].r].pb(i); } for (int i = 2; i <= N; i++) { for (auto p : s[i]) { update(1, ls[p].l - 1, ls[p].a, 1, N, 1); } ll tmp = query(1, i - 1, 1, N, 1); update(i, i, tmp, 1, N, 1); for (auto p : e[i]) { update(1, ls[p].l - 1, -ls[p].a, 1, N, 1); } } writeln(max(0LL, query(1, N, 1, N, 1))); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); #ifdef LOCAL clock_t _begin = clock(); #endif build(); solve(); #ifdef LOCAL cerr << "Time elapsed: " << (double)(clock() - _begin) * 1000 / CLOCKS_PER_SEC << "ms." << endl; #endif return 0; }
#include <bits/stdc++.h> using namespace std; #define TemplateVersion "3.7.1" // Useful Marcos //====================START===================== // Compile use C++11 and above #ifdef LOCAL #define debug(args...) \ do { \ string _s = #args; \ replace(_s.begin(), _s.end(), ',', ' '); \ stringstream _ss(_s); \ istream_iterator<string> _it(_ss); \ err(_it, args); \ } while (0) void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << endl; err(++it, args...); } #define MSG cout << "Finished" << endl #else #define debug(args...) #define MSG #endif #if __cplusplus >= 201703L template <typename... Args> void readln(Args &...args) { ((cin >> args), ...); } template <typename... Args> void writeln(Args... args) { ((cout << args << " "), ...); cout << endl; } #elif __cplusplus >= 201103L void readln() {} template <typename T, typename... Args> void readln(T &a, Args &...args) { cin >> a; readln(args...); } void writeln() { cout << endl; } template <typename T, typename... Args> void writeln(T a, Args... args) { cout << a << " "; writeln(args...); } #endif #if __cplusplus >= 201103L #define FOR(_i, _begin, _end) for (auto _i = _begin; _i < _end; _i++) #define FORR(_i, _begin, _end) for (auto _i = _begin; _i > _end; _i--) #else #define FOR(_i, _begin, _end) for (int _i = (int)_begin; _i < (int)_end; _i++) #define FORR(_i, _begin, _end) for (int _i = (int)_begin; _i > (int)_end; _i--) #define nullptr NULL #endif #if __cplusplus >= 201103L #define VIS(_kind, _name, _size) \ vector<_kind> _name(_size); \ for (auto &i : _name) \ cin >> i; #else #define VIS(_kind, _name, _size) \ vector<_kind> _name; \ _name.resize(_size); \ for (int i = 0; i < _size; i++) \ cin >> _name[i]; #endif // alias #define mp make_pair #define pb push_back #define eb emplace_back #define all(x) (x).begin(), (x).end() #define clr(x) memset((x), 0, sizeof(x)) #define infty(x) memset((x), 0x3f, sizeof(x)) #define tcase() \ int T; \ cin >> T; \ FOR(kase, 1, T + 1) // Swap max/min template <typename T> bool smax(T &a, const T &b) { if (a > b) return false; a = b; return true; } template <typename T> bool smin(T &a, const T &b) { if (a < b) return false; a = b; return true; } // ceil divide template <typename T> T cd(T a, T b) { return (a + b - 1) / b; } // min exchange template <typename T> bool se(T &a, T &b) { if (a < b) return false; swap(a, b); return true; } // A better MAX choice const int INF = 0x3f3f3f3f; const long long INFLL = 0x3f3f3f3f3f3f3f3fLL; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<ll> vll; typedef set<int> si; typedef vector<string> cb; //====================END===================== // Constants here const int MAXN = 2e5 + 10; const ll MIN = -3e14; struct Line { int l, r; ll a; } ls[MAXN]; ll seg[MAXN << 2], flag[MAXN << 2]; void pushup(int pos) { seg[pos] = max(seg[pos << 1], seg[pos << 1 | 1]); } void pushdown(int pos) { if (flag[pos] != 0) { flag[pos << 1] += flag[pos]; flag[pos << 1 | 1] += flag[pos]; seg[pos << 1] += flag[pos]; seg[pos << 1 | 1] += flag[pos]; flag[pos] = 0; } } void update(int ul, int ur, ll val, int l, int r, int cur) { if (ul <= l && r <= ur) { seg[cur] += val; flag[cur] += val; return; } int m = ((r - l) >> 1) + l; pushdown(cur); if (ul <= m) { update(ul, ur, val, l, m, cur << 1); } if (ur > m) { update(ul, ur, val, m + 1, r, cur << 1 | 1); } pushup(cur); } ll query(int ql, int qr, int l, int r, int cur) { if (ql <= l && r <= qr) { return seg[cur]; } int m = ((r - l) >> 1) + l; pushdown(cur); ll ans = MIN; if (ql <= m) { smax(ans, query(ql, qr, l, m, cur << 1)); } if (qr > m) { smax(ans, query(ql, qr, m + 1, r, cur << 1 | 1)); } return ans; } // Pre-Build Function inline void build() {} // Actual Solver inline void solve() { map<int, vector<int>> s, e; int N, M; readln(N, M); N++; for (int i = 0; i < M; i++) { readln(ls[i].l, ls[i].r, ls[i].a); ls[i].l++, ls[i].r++; s[ls[i].l].pb(i); e[ls[i].r].pb(i); } for (int i = 2; i <= N; i++) { for (auto p : s[i]) { update(1, ls[p].l - 1, ls[p].a, 1, N, 1); } ll tmp = query(1, i - 1, 1, N, 1); update(i, i, tmp, 1, N, 1); for (auto p : e[i]) { update(1, ls[p].l - 1, -ls[p].a, 1, N, 1); } } writeln(max(0LL, query(1, N, 1, N, 1))); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); #ifdef LOCAL clock_t _begin = clock(); #endif build(); solve(); #ifdef LOCAL cerr << "Time elapsed: " << (double)(clock() - _begin) * 1000 / CLOCKS_PER_SEC << "ms." << endl; #endif return 0; }
replace
122
123
122
123
0
p03182
C++
Runtime Error
#include <algorithm> #include <chrono> #include <iostream> #include <random> #define rep(i, x) for (int i = 0; i < x; i++) using namespace std; typedef long long ll; ll MOD = (ll)1e9 + 7; const int N = 1e5 + 5, inf = 1e9 + 5; ll add(ll x, ll y) { x += y; if (x >= MOD) return x - MOD; return x; } ll sub(ll x, ll y) { x -= y; if (x < 0) return x + MOD; return x; } ll mult(ll x, ll y) { return (x * y) % MOD; } mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); struct Node { ll val, lazy; void add(ll x) { val += x; lazy += x; } }; vector<Node> st(4 * N, {0, 0}); void add(int node, int l, int r, int ql, int qr, ll x) { if (l > qr || r < ql) return; if (ql <= l && r <= qr) { st[node].add(x); } else { st[node * 2].add(st[node].lazy); st[node * 2 + 1].add(st[node].lazy); st[node].lazy = 0; add(node * 2, l, (l + r) / 2, ql, qr, x); add(node * 2 + 1, (l + r) / 2 + 1, r, ql, qr, x); st[node].val = max(st[node * 2].val, st[node * 2 + 1].val); } } ll query(int node, int l, int r, int ql, int qr) { if (l > qr || r < ql) return -inf; if (ql <= l && r <= qr) { return st[node].val; } else { st[node * 2].add(st[node].lazy); st[node * 2 + 1].add(st[node].lazy); st[node].lazy = 0; ll q1 = query(node * 2, l, (l + r) / 2, ql, qr); ll q2 = query(node * 2 + 1, (l + r) / 2 + 1, r, ql, qr); return max(q1, q2); } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; vector<ll> l(m), r(m), a(m), dp(n + 1, -inf); vector<vector<ll>> starting(n + 1, vector<ll>()), ending(n + 1, vector<ll>()); rep(i, m) { cin >> l[i] >> r[i] >> a[i]; starting[l[i]].push_back(i); ending[r[i]].push_back(i); } dp[0] = 0; /* for (int k = 0; k < m; ++k) if (l[k] == 0) dp[0] += a[k]; for (int i = 1; i < n; ++i) { for (int j = 0; j < i; ++j) { ll sum = 0; for (int k = 0; k < m; ++k) if (j < l[k] && l[k] <= i && i <= r[k]) sum += a[k]; dp[i] = max(dp[i], dp[j]+sum); } } */ for (int i = 1; i <= n; ++i) { for (ll x : starting[i]) add(1, 0, n, 0, i - 1, a[x]); dp[i] = query(1, 0, n, 0, i - 1); for (ll x : ending[i]) add(1, 0, n, 0, l[x] - 1, -a[x]); add(1, 0, n, i, i, dp[i]); } cout << *max_element(begin(dp), end(dp)) << endl; }
#include <algorithm> #include <chrono> #include <iostream> #include <random> #define rep(i, x) for (int i = 0; i < x; i++) using namespace std; typedef long long ll; ll MOD = (ll)1e9 + 7; const ll N = 2e5 + 5, inf = 1e18 + 5; ll add(ll x, ll y) { x += y; if (x >= MOD) return x - MOD; return x; } ll sub(ll x, ll y) { x -= y; if (x < 0) return x + MOD; return x; } ll mult(ll x, ll y) { return (x * y) % MOD; } mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); struct Node { ll val, lazy; void add(ll x) { val += x; lazy += x; } }; vector<Node> st(4 * N, {0, 0}); void add(int node, int l, int r, int ql, int qr, ll x) { if (l > qr || r < ql) return; if (ql <= l && r <= qr) { st[node].add(x); } else { st[node * 2].add(st[node].lazy); st[node * 2 + 1].add(st[node].lazy); st[node].lazy = 0; add(node * 2, l, (l + r) / 2, ql, qr, x); add(node * 2 + 1, (l + r) / 2 + 1, r, ql, qr, x); st[node].val = max(st[node * 2].val, st[node * 2 + 1].val); } } ll query(int node, int l, int r, int ql, int qr) { if (l > qr || r < ql) return -inf; if (ql <= l && r <= qr) { return st[node].val; } else { st[node * 2].add(st[node].lazy); st[node * 2 + 1].add(st[node].lazy); st[node].lazy = 0; ll q1 = query(node * 2, l, (l + r) / 2, ql, qr); ll q2 = query(node * 2 + 1, (l + r) / 2 + 1, r, ql, qr); return max(q1, q2); } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; vector<ll> l(m), r(m), a(m), dp(n + 1, -inf); vector<vector<ll>> starting(n + 1, vector<ll>()), ending(n + 1, vector<ll>()); rep(i, m) { cin >> l[i] >> r[i] >> a[i]; starting[l[i]].push_back(i); ending[r[i]].push_back(i); } dp[0] = 0; /* for (int k = 0; k < m; ++k) if (l[k] == 0) dp[0] += a[k]; for (int i = 1; i < n; ++i) { for (int j = 0; j < i; ++j) { ll sum = 0; for (int k = 0; k < m; ++k) if (j < l[k] && l[k] <= i && i <= r[k]) sum += a[k]; dp[i] = max(dp[i], dp[j]+sum); } } */ for (int i = 1; i <= n; ++i) { for (ll x : starting[i]) add(1, 0, n, 0, i - 1, a[x]); dp[i] = query(1, 0, n, 0, i - 1); for (ll x : ending[i]) add(1, 0, n, 0, l[x] - 1, -a[x]); add(1, 0, n, i, i, dp[i]); } cout << *max_element(begin(dp), end(dp)) << endl; }
replace
8
9
8
9
0
p03182
C++
Runtime Error
// #pragma GCC optimize("Ofast") // #pragma GCC target("avx") // #pragma GCC optimize ("unroll-loops") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; #define fi first #define se second #define pb push_back #define sz(x) ((int)x.size()) #define all(x) (x).begin(), (x).end() #define re return #define mp make_pair #define sqrt(x) sqrt(abs(x)) #define y0 y3451 #define y1 y4562 #define j0 j25624 #define j1 j45624 #define makeunique(x) sort(all(x)), (x).resize(unique(all(x)) - (x).begin()) typedef pair<int, int> ii; typedef long long ll; typedef unsigned long long ull; typedef double D; typedef long double ld; typedef unsigned int uint; typedef vector<string> vs; typedef vector<int> vi; typedef vector<ii> vii; typedef vector<vi> vvi; template <class T> using _tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; template <class T> T abs(T x) { re x > 0 ? x : -x; } template <class T> T sqr(T x) { re x *x; } template <class T> T gcd(T a, T b) { re a ? gcd(b % a, a) : b; } template <class T> int sgn(T x) { re x > 0 ? 1 : (x < 0 ? -1 : 0); } #define filename "" const D pi = acos(-1.); const int N = 1e5 + 20; const ll inf = 1e18 + 7; int n, m; ll t[4 * N], how[4 * N]; void push(int v) { if (how[v] == 0) re; t[2 * v + 1] += how[v]; t[2 * v + 2] += how[v]; how[2 * v + 1] += how[v]; how[2 * v + 2] += how[v]; how[v] = 0; } void modify(int v, int tl, int tr, int l, int r, ll x) { if (tl > r || l > tr) re; if (l <= tl && tr <= r) { how[v] += x; t[v] += x; re; } push(v); int c = (tl + tr) >> 1; modify(2 * v + 1, tl, c, l, r, x); modify(2 * v + 2, c + 1, tr, l, r, x); t[v] = max(t[2 * v + 1], t[2 * v + 2]); } ll get(int v, int tl, int tr, int l, int r) { if (tl > r || l > tr || l > r) re - inf; if (l <= tl && tr <= r) re t[v]; push(v); int c = (tl + tr) >> 1; re max(get(2 * v + 1, tl, c, l, r), get(2 * v + 2, c + 1, tr, l, r)); } int a[N]; ii seg[N]; vi b[N], e[N]; ll dp[N]; int main() { cin >> n >> m; for (int i = 0; i < m; i++) { int l, r, z; scanf("%d%d%d", &l, &r, &z); l--; r--; a[i] = z; seg[i] = {l, r}; b[l].pb(i); e[r].pb(i); } ll cur = 0; for (int i = 0; i < n; i++) { for (auto x : b[i]) cur += a[x]; dp[i] = cur; for (auto x : b[i]) modify(0, 0, n - 1, seg[x].fi, seg[x].se, -a[x]); ll ans = get(0, 0, n - 1, 0, i - 1); ans = max((ll)0, ans); dp[i] = ans + cur; modify(0, 0, n - 1, i, i, dp[i]); for (auto x : e[i]) modify(0, 0, n - 1, seg[x].fi, seg[x].se, a[x]), cur -= a[x]; } cout << *max_element(dp, dp + n + 1) << endl; }
// #pragma GCC optimize("Ofast") // #pragma GCC target("avx") // #pragma GCC optimize ("unroll-loops") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; #define fi first #define se second #define pb push_back #define sz(x) ((int)x.size()) #define all(x) (x).begin(), (x).end() #define re return #define mp make_pair #define sqrt(x) sqrt(abs(x)) #define y0 y3451 #define y1 y4562 #define j0 j25624 #define j1 j45624 #define makeunique(x) sort(all(x)), (x).resize(unique(all(x)) - (x).begin()) typedef pair<int, int> ii; typedef long long ll; typedef unsigned long long ull; typedef double D; typedef long double ld; typedef unsigned int uint; typedef vector<string> vs; typedef vector<int> vi; typedef vector<ii> vii; typedef vector<vi> vvi; template <class T> using _tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; template <class T> T abs(T x) { re x > 0 ? x : -x; } template <class T> T sqr(T x) { re x *x; } template <class T> T gcd(T a, T b) { re a ? gcd(b % a, a) : b; } template <class T> int sgn(T x) { re x > 0 ? 1 : (x < 0 ? -1 : 0); } #define filename "" const D pi = acos(-1.); const int N = 3e5 + 20; const ll inf = 1e18 + 7; int n, m; ll t[4 * N], how[4 * N]; void push(int v) { if (how[v] == 0) re; t[2 * v + 1] += how[v]; t[2 * v + 2] += how[v]; how[2 * v + 1] += how[v]; how[2 * v + 2] += how[v]; how[v] = 0; } void modify(int v, int tl, int tr, int l, int r, ll x) { if (tl > r || l > tr) re; if (l <= tl && tr <= r) { how[v] += x; t[v] += x; re; } push(v); int c = (tl + tr) >> 1; modify(2 * v + 1, tl, c, l, r, x); modify(2 * v + 2, c + 1, tr, l, r, x); t[v] = max(t[2 * v + 1], t[2 * v + 2]); } ll get(int v, int tl, int tr, int l, int r) { if (tl > r || l > tr || l > r) re - inf; if (l <= tl && tr <= r) re t[v]; push(v); int c = (tl + tr) >> 1; re max(get(2 * v + 1, tl, c, l, r), get(2 * v + 2, c + 1, tr, l, r)); } int a[N]; ii seg[N]; vi b[N], e[N]; ll dp[N]; int main() { cin >> n >> m; for (int i = 0; i < m; i++) { int l, r, z; scanf("%d%d%d", &l, &r, &z); l--; r--; a[i] = z; seg[i] = {l, r}; b[l].pb(i); e[r].pb(i); } ll cur = 0; for (int i = 0; i < n; i++) { for (auto x : b[i]) cur += a[x]; dp[i] = cur; for (auto x : b[i]) modify(0, 0, n - 1, seg[x].fi, seg[x].se, -a[x]); ll ans = get(0, 0, n - 1, 0, i - 1); ans = max((ll)0, ans); dp[i] = ans + cur; modify(0, 0, n - 1, i, i, dp[i]); for (auto x : e[i]) modify(0, 0, n - 1, seg[x].fi, seg[x].se, a[x]), cur -= a[x]; } cout << *max_element(dp, dp + n + 1) << endl; }
replace
46
47
46
47
0
p03182
C++
Runtime Error
#include <bits/stdc++.h> #define _ \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define int long long #define pb push_back #define sz(i) (int)(i.size()) #define F first #define S second #define LL long double #define P pair<int, int> #ifdef ONLINE_JUDGE #define endl '\n' #endif const int inf = 0x3f3f3f3f3f3f3f3LL; const int mod = (int)1e9 + 7; using namespace std; #ifndef ONLINE_JUDGE #define tr(...) \ { \ __l(__LINE__); \ __f(#__VA_ARGS__, __VA_ARGS__); \ } inline void __l(int line) { cerr << "#" << line << ": "; } template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cerr << name << " : " << arg1 << endl; } template <typename Arg1, typename... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } #else #define tr(...) #endif template <class T> void smin(T &a, T val) { if (a > val) a = val; } template <class T> void smax(T &a, T val) { if (a < val) a = val; } template <typename T> inline std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) { bool first = true; os << "["; for (auto i : v) { if (!first) os << ", "; os << i; first = false; } return os << "]"; } const int N = 2 * (int)1e5 + 10; // make sure N size is correctttt!!!!!!!!! // Segment tree operations: Range update(Lazy propagation) and Range Query const int MAX = 1e5 + 5; const int LIM = 3e5 + 5; // equals 2 * 2^ceil(log2(n)) int a[MAX]; int seg[LIM]; int lazy[LIM]; bool push[LIM]; // Complexity: O(1) // Stores what info. segment[i..j] should store int combine(int &a, int &b) { return max(a, b); } // Lazy propagation void propagate(int t, int i, int j) { if (push[t]) { seg[t] = seg[t] + lazy[t]; if (i != j) { push[t * 2] = true; push[t * 2 + 1] = true; lazy[t * 2] = lazy[t * 2] + lazy[t]; lazy[t * 2 + 1] = lazy[t * 2 + 1] + lazy[t]; } push[t] = false; lazy[t] = 0; } } // Complexity: O(n) void build(int t, int i, int j) { push[t] = false; lazy[t] = 0; if (i == j) { // base case : leaf node information to be stored here seg[t] = a[i]; return; } int mid = (i + j) / 2; build(t * 2, i, mid); build(t * 2 + 1, mid + 1, j); seg[t] = combine(seg[2 * t], seg[2 * t + 1]); } // Complexity: O(log n) void update(int t, int i, int j, int l, int r, int x) { propagate(t, i, j); if (i > r || j < l) { return; } if (l <= i && j <= r) { // base case : leaf node information to be stored here lazy[t] += x; push[t] = true; propagate(t, i, j); return; } int mid = (i + j) / 2; update(t * 2, i, mid, l, r, x); update(t * 2 + 1, mid + 1, j, l, r, x); seg[t] = combine(seg[2 * t], seg[2 * t + 1]); } // Complexity: O(log n) int query(int t, int i, int j, int l, int r) { propagate(t, i, j); if (i > r || j < l) { // base case: result of out-of-bound query return 0; } if (l <= i && j <= r) { return seg[t]; } int mid = (i + j) / 2; if (l <= mid) { if (r <= mid) { return query(t * 2, i, mid, l, r); } else { int a = query(t * 2, i, mid, l, r); int b = query(t * 2 + 1, mid + 1, j, l, r); return combine(a, b); } } else { return query(t * 2 + 1, mid + 1, j, l, r); } } int n, m, dp[N]; vector<P> L[N], R[N]; int32_t main() { _ cin >> n >> m; for (int i = 0; i < m; ++i) { int l, r, x; cin >> l >> r >> x; L[l].pb({l - 1, x}); R[r].pb({l - 1, x}); } // void update(int t, int i, int j, int l, int r, int x) { // int query(int t, int i, int j, int l, int r) { int ans = 0; for (int i = 1; i <= n; ++i) { for (auto it : L[i]) update(1, 0, n, 0, it.F, it.S); int tmp_ans = query(1, 0, n, 0, i - 1); smax(ans, tmp_ans); update(1, 0, n, i, i, tmp_ans); for (auto it : R[i]) update(1, 0, n, 0, it.F, -it.S); } cout << ans; // make sure N size is correct!!!!!!!!! return 0; }
#include <bits/stdc++.h> #define _ \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define int long long #define pb push_back #define sz(i) (int)(i.size()) #define F first #define S second #define LL long double #define P pair<int, int> #ifdef ONLINE_JUDGE #define endl '\n' #endif const int inf = 0x3f3f3f3f3f3f3f3LL; const int mod = (int)1e9 + 7; using namespace std; #ifndef ONLINE_JUDGE #define tr(...) \ { \ __l(__LINE__); \ __f(#__VA_ARGS__, __VA_ARGS__); \ } inline void __l(int line) { cerr << "#" << line << ": "; } template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cerr << name << " : " << arg1 << endl; } template <typename Arg1, typename... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } #else #define tr(...) #endif template <class T> void smin(T &a, T val) { if (a > val) a = val; } template <class T> void smax(T &a, T val) { if (a < val) a = val; } template <typename T> inline std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) { bool first = true; os << "["; for (auto i : v) { if (!first) os << ", "; os << i; first = false; } return os << "]"; } const int N = 2 * (int)1e5 + 10; // make sure N size is correctttt!!!!!!!!! // Segment tree operations: Range update(Lazy propagation) and Range Query const int MAX = 2e5 + 5; const int LIM = 7.5 * 1e5 + 5; // equals 2 * 2^ceil(log2(n)) int a[MAX]; int seg[LIM]; int lazy[LIM]; bool push[LIM]; // Complexity: O(1) // Stores what info. segment[i..j] should store int combine(int &a, int &b) { return max(a, b); } // Lazy propagation void propagate(int t, int i, int j) { if (push[t]) { seg[t] = seg[t] + lazy[t]; if (i != j) { push[t * 2] = true; push[t * 2 + 1] = true; lazy[t * 2] = lazy[t * 2] + lazy[t]; lazy[t * 2 + 1] = lazy[t * 2 + 1] + lazy[t]; } push[t] = false; lazy[t] = 0; } } // Complexity: O(n) void build(int t, int i, int j) { push[t] = false; lazy[t] = 0; if (i == j) { // base case : leaf node information to be stored here seg[t] = a[i]; return; } int mid = (i + j) / 2; build(t * 2, i, mid); build(t * 2 + 1, mid + 1, j); seg[t] = combine(seg[2 * t], seg[2 * t + 1]); } // Complexity: O(log n) void update(int t, int i, int j, int l, int r, int x) { propagate(t, i, j); if (i > r || j < l) { return; } if (l <= i && j <= r) { // base case : leaf node information to be stored here lazy[t] += x; push[t] = true; propagate(t, i, j); return; } int mid = (i + j) / 2; update(t * 2, i, mid, l, r, x); update(t * 2 + 1, mid + 1, j, l, r, x); seg[t] = combine(seg[2 * t], seg[2 * t + 1]); } // Complexity: O(log n) int query(int t, int i, int j, int l, int r) { propagate(t, i, j); if (i > r || j < l) { // base case: result of out-of-bound query return 0; } if (l <= i && j <= r) { return seg[t]; } int mid = (i + j) / 2; if (l <= mid) { if (r <= mid) { return query(t * 2, i, mid, l, r); } else { int a = query(t * 2, i, mid, l, r); int b = query(t * 2 + 1, mid + 1, j, l, r); return combine(a, b); } } else { return query(t * 2 + 1, mid + 1, j, l, r); } } int n, m, dp[N]; vector<P> L[N], R[N]; int32_t main() { _ cin >> n >> m; for (int i = 0; i < m; ++i) { int l, r, x; cin >> l >> r >> x; L[l].pb({l - 1, x}); R[r].pb({l - 1, x}); } // void update(int t, int i, int j, int l, int r, int x) { // int query(int t, int i, int j, int l, int r) { int ans = 0; for (int i = 1; i <= n; ++i) { for (auto it : L[i]) update(1, 0, n, 0, it.F, it.S); int tmp_ans = query(1, 0, n, 0, i - 1); smax(ans, tmp_ans); update(1, 0, n, i, i, tmp_ans); for (auto it : R[i]) update(1, 0, n, 0, it.F, -it.S); } cout << ans; // make sure N size is correct!!!!!!!!! return 0; }
replace
62
64
62
64
0
p03182
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define int long long const int MAXN = (4); struct Intervalle { int maximum, ajout; }; struct Event { int moment, debut, fin, valeur; bool operator<(const Event &autre) const { return moment < autre.moment; } }; int taille; Intervalle arbre[MAXN * 2]; void update(int noeud) { arbre[noeud].maximum += arbre[noeud].ajout; if (noeud < MAXN) { arbre[noeud * 2].ajout += arbre[noeud].ajout; arbre[noeud * 2 + 1].ajout += arbre[noeud].ajout; } arbre[noeud].ajout = 0; } void merge(int noeud) { arbre[noeud].maximum = max(arbre[noeud * 2].maximum, arbre[noeud * 2 + 1].maximum); } void ajouter(int noeud, int debut, int fin, int debutCible, int finCible, int ajout) { // cout<<debut<<" "<<fin<<" "<<noeud<<" "<<debutCible<<" "<<finCible<<endl; update(noeud); if (debutCible <= debut && fin <= finCible) { arbre[noeud].ajout += ajout; update(noeud); } else if (!(fin < debutCible || debut > finCible)) { ajouter(noeud * 2, debut, (debut + fin) / 2, debutCible, finCible, ajout); ajouter(noeud * 2 + 1, (debut + fin) / 2 + 1, fin, debutCible, finCible, ajout); merge(noeud); } } int recupeMax(int noeud, int debut, int fin, int debutCible, int finCible) { update(noeud); if (debutCible <= debut && fin <= finCible) { return arbre[noeud].maximum; } else if (!(fin < debutCible || debut > finCible)) { int g = recupeMax(noeud * 2, debut, (debut + fin) / 2, debutCible, finCible); int d = recupeMax(noeud * 2 + 1, (debut + fin) / 2 + 1, fin, debutCible, finCible); merge(noeud); return max(g, d); } else { return -1e18; } } int dp[MAXN]; int32_t main() { int nbSegments; cin >> taille >> nbSegments; Event events[nbSegments * 2]; for (int i = 0; i < nbSegments; i++) { int debut, fin, v; cin >> debut >> fin >> v; events[i * 2] = {debut, debut, fin, -v}; events[i * 2 + 1] = {fin + 1, debut, fin, v}; } int nbEvents = nbSegments * 2; sort(events, events + nbEvents); int totalActu = 0; int in = 0; int leMax = 0; for (int i = 1; i <= taille; i++) { while (in < nbEvents && events[in].moment == i) { totalActu -= events[in].valeur; ajouter(1, 0, MAXN - 1, events[in].debut, events[in].fin, events[in].valeur); in++; } dp[i] = recupeMax(1, 0, MAXN - 1, 0, i - 1) + totalActu; ajouter(1, 0, MAXN - 1, i, i, dp[i]); leMax = max(leMax, dp[i]); } cout << leMax << endl; }
#include <bits/stdc++.h> using namespace std; #define int long long const int MAXN = (1 << 18); struct Intervalle { int maximum, ajout; }; struct Event { int moment, debut, fin, valeur; bool operator<(const Event &autre) const { return moment < autre.moment; } }; int taille; Intervalle arbre[MAXN * 2]; void update(int noeud) { arbre[noeud].maximum += arbre[noeud].ajout; if (noeud < MAXN) { arbre[noeud * 2].ajout += arbre[noeud].ajout; arbre[noeud * 2 + 1].ajout += arbre[noeud].ajout; } arbre[noeud].ajout = 0; } void merge(int noeud) { arbre[noeud].maximum = max(arbre[noeud * 2].maximum, arbre[noeud * 2 + 1].maximum); } void ajouter(int noeud, int debut, int fin, int debutCible, int finCible, int ajout) { // cout<<debut<<" "<<fin<<" "<<noeud<<" "<<debutCible<<" "<<finCible<<endl; update(noeud); if (debutCible <= debut && fin <= finCible) { arbre[noeud].ajout += ajout; update(noeud); } else if (!(fin < debutCible || debut > finCible)) { ajouter(noeud * 2, debut, (debut + fin) / 2, debutCible, finCible, ajout); ajouter(noeud * 2 + 1, (debut + fin) / 2 + 1, fin, debutCible, finCible, ajout); merge(noeud); } } int recupeMax(int noeud, int debut, int fin, int debutCible, int finCible) { update(noeud); if (debutCible <= debut && fin <= finCible) { return arbre[noeud].maximum; } else if (!(fin < debutCible || debut > finCible)) { int g = recupeMax(noeud * 2, debut, (debut + fin) / 2, debutCible, finCible); int d = recupeMax(noeud * 2 + 1, (debut + fin) / 2 + 1, fin, debutCible, finCible); merge(noeud); return max(g, d); } else { return -1e18; } } int dp[MAXN]; int32_t main() { int nbSegments; cin >> taille >> nbSegments; Event events[nbSegments * 2]; for (int i = 0; i < nbSegments; i++) { int debut, fin, v; cin >> debut >> fin >> v; events[i * 2] = {debut, debut, fin, -v}; events[i * 2 + 1] = {fin + 1, debut, fin, v}; } int nbEvents = nbSegments * 2; sort(events, events + nbEvents); int totalActu = 0; int in = 0; int leMax = 0; for (int i = 1; i <= taille; i++) { while (in < nbEvents && events[in].moment == i) { totalActu -= events[in].valeur; ajouter(1, 0, MAXN - 1, events[in].debut, events[in].fin, events[in].valeur); in++; } dp[i] = recupeMax(1, 0, MAXN - 1, 0, i - 1) + totalActu; ajouter(1, 0, MAXN - 1, i, i, dp[i]); leMax = max(leMax, dp[i]); } cout << leMax << endl; }
replace
3
4
3
4
0
p03182
C++
Runtime Error
#include <bits/stdc++.h> #define FI first #define SE second #define EPS 1e-9 #define ALL(a) a.begin(), a.end() #define SZ(a) int((a).size()) #define MS(s, n) memset(s, n, sizeof(s)) #define FOR(i, a, b) for (int i = (a); i <= (b); i++) #define FORE(i, a, b) for (int i = (a); i >= (b); i--) #define FORALL(it, a) \ for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++) #define WHATIS(x) cout << #x << " is " << x << endl; #define ERROR(args...) \ { \ string _s = #args; \ replace(_s.begin(), _s.end(), ',', ' '); \ stringstream _ss(_s); \ istream_iterator<string> _it(_ss); \ err(_it, args); \ } //__builtin_ffs(x) return 1 + index of least significant 1-bit of x //__builtin_clz(x) return number of leading zeros of x //__builtin_ctz(x) return number of trailing zeros of x using namespace std; using ll = long long; using ld = double; typedef pair<int, int> II; typedef pair<II, int> III; typedef complex<ld> cd; typedef vector<cd> vcd; void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cout << *it << " = " << a << endl; err(++it, args...); } const ll MODBASE = 1000000007LL; const int MAXN = 200010; const int MAXM = 1000010; const int MAXK = 110; const int MAXQ = 200010; int n, m; ll Max[MAXN], lazy[MAXN]; vector<II> a[MAXN], b[MAXN]; void lazyupd(int k, int l, int r) { if (lazy[k] == 0) return; Max[k] += lazy[k]; if (l < r) { lazy[k * 2] += lazy[k]; lazy[k * 2 + 1] += lazy[k]; } lazy[k] = 0; } void update(int k, int l, int r, int u, int v, ll gt) { lazyupd(k, l, r); if (r < u || v < l || l > r) return; if (u <= l && r <= v) { lazy[k] = gt; lazyupd(k, l, r); return; } int m = (l + r) >> 1; update(k * 2, l, m, u, v, gt); update(k * 2 + 1, m + 1, r, u, v, gt); Max[k] = max(Max[k * 2], Max[k * 2 + 1]); } ll get(int k, int l, int r, int u, int v) { lazyupd(k, l, r); if (r < u || v < l || l > r) return -1e18; if (u <= l && r <= v) return Max[k]; int m = (l + r) >> 1; return max(get(k * 2, l, m, u, v), get(k * 2 + 1, m + 1, r, u, v)); } int main() { ios::sync_with_stdio(0); cin.tie(nullptr); cin >> n >> m; FOR(i, 1, m) { int u, v, c; cin >> u >> v >> c; a[v].emplace_back(II(u, c)); b[u].emplace_back(II(v, c)); } ll res = 0; FOR(i, 1, n) { FOR(j, 0, SZ(b[i]) - 1) { int c = b[i][j].SE; update(1, 0, n, 0, i - 1, c); } FOR(j, 0, SZ(a[i - 1]) - 1) { int u = a[i - 1][j].FI; int c = a[i - 1][j].SE; update(1, 0, n, 0, u - 1, -c); } ll kq = get(1, 0, n, 0, i - 1); res = max(res, kq); update(1, 0, n, i, i, kq); } cout << res; return 0; }
#include <bits/stdc++.h> #define FI first #define SE second #define EPS 1e-9 #define ALL(a) a.begin(), a.end() #define SZ(a) int((a).size()) #define MS(s, n) memset(s, n, sizeof(s)) #define FOR(i, a, b) for (int i = (a); i <= (b); i++) #define FORE(i, a, b) for (int i = (a); i >= (b); i--) #define FORALL(it, a) \ for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++) #define WHATIS(x) cout << #x << " is " << x << endl; #define ERROR(args...) \ { \ string _s = #args; \ replace(_s.begin(), _s.end(), ',', ' '); \ stringstream _ss(_s); \ istream_iterator<string> _it(_ss); \ err(_it, args); \ } //__builtin_ffs(x) return 1 + index of least significant 1-bit of x //__builtin_clz(x) return number of leading zeros of x //__builtin_ctz(x) return number of trailing zeros of x using namespace std; using ll = long long; using ld = double; typedef pair<int, int> II; typedef pair<II, int> III; typedef complex<ld> cd; typedef vector<cd> vcd; void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cout << *it << " = " << a << endl; err(++it, args...); } const ll MODBASE = 1000000007LL; const int MAXN = 200010; const int MAXM = 1000010; const int MAXK = 110; const int MAXQ = 200010; int n, m; ll Max[8 * MAXN], lazy[8 * MAXN]; vector<II> a[MAXN], b[MAXN]; void lazyupd(int k, int l, int r) { if (lazy[k] == 0) return; Max[k] += lazy[k]; if (l < r) { lazy[k * 2] += lazy[k]; lazy[k * 2 + 1] += lazy[k]; } lazy[k] = 0; } void update(int k, int l, int r, int u, int v, ll gt) { lazyupd(k, l, r); if (r < u || v < l || l > r) return; if (u <= l && r <= v) { lazy[k] = gt; lazyupd(k, l, r); return; } int m = (l + r) >> 1; update(k * 2, l, m, u, v, gt); update(k * 2 + 1, m + 1, r, u, v, gt); Max[k] = max(Max[k * 2], Max[k * 2 + 1]); } ll get(int k, int l, int r, int u, int v) { lazyupd(k, l, r); if (r < u || v < l || l > r) return -1e18; if (u <= l && r <= v) return Max[k]; int m = (l + r) >> 1; return max(get(k * 2, l, m, u, v), get(k * 2 + 1, m + 1, r, u, v)); } int main() { ios::sync_with_stdio(0); cin.tie(nullptr); cin >> n >> m; FOR(i, 1, m) { int u, v, c; cin >> u >> v >> c; a[v].emplace_back(II(u, c)); b[u].emplace_back(II(v, c)); } ll res = 0; FOR(i, 1, n) { FOR(j, 0, SZ(b[i]) - 1) { int c = b[i][j].SE; update(1, 0, n, 0, i - 1, c); } FOR(j, 0, SZ(a[i - 1]) - 1) { int u = a[i - 1][j].FI; int c = a[i - 1][j].SE; update(1, 0, n, 0, u - 1, -c); } ll kq = get(1, 0, n, 0, i - 1); res = max(res, kq); update(1, 0, n, i, i, kq); } cout << res; return 0; }
replace
46
47
46
47
0
p03182
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using lint = long long int; using pint = pair<int, int>; using plint = pair<lint, lint>; struct fast_ios { fast_ios() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; #define FOR(i, begin, end) \ for (int i = (begin), i##_end_ = (end); i < i##_end_; i++) #define IFOR(i, begin, end) \ for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--) #define REP(i, n) FOR(i, 0, n) #define IREP(i, n) IFOR(i, 0, n) template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; } template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ","; os << "]"; return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_set<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) { os << "(" << pa.first << "," << pa.second << ")"; return os; } template <typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp) { os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; } template <typename TK, typename TV> ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp) { os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; } template <typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); } template <typename T, typename... Args> void ndarray(vector<T> &vec, int len, Args... args) { vec.resize(len); for (auto &v : vec) ndarray(v, args...); } template <typename T> void Max(T &m, const T q) { if (m < q) m = q; } template <typename T> void Min(T &m, const T q) { if (m > q) m = q; } template <typename T1, typename T2> pair<T1, T2> operator+(pair<T1, T2> &l, pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); } template <typename T1, typename T2> pair<T1, T2> operator-(pair<T1, T2> &l, pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); } #define dbg(x) \ cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl; template <typename VAL, typename DVAL> struct LazySegTree { int N; int head; vector<VAL> val; vector<DVAL> dval; VAL I_val; DVAL I_dval; using vv2v = function<VAL(const VAL &, const VAL &)>; using d2d = function<void(DVAL &, const DVAL &)>; using d2v = function<void(VAL &, const DVAL &)>; vv2v merge_val; d2d add_dval; d2v refl_dval; LazySegTree(vector<VAL> &val_init, VAL val_default, DVAL dval_default, vv2v f, d2d dadd, d2v dreflect) : N(val_init.size()), I_val(val_default), I_dval(dval_default), merge_val(f), add_dval(dadd), refl_dval(dreflect) { int N_tmp = 1; while (N_tmp < N) N_tmp <<= 1; val = vector<VAL>(N_tmp * 2, I_val); dval = vector<DVAL>(N_tmp * 2, I_dval); head = N_tmp - 1; for (int i = 0; i < N; i++) val[head + i] = val_init[i]; for (int pos = head - 1; pos >= 0; pos--) val[pos] = merge_val(val[pos * 2 + 1], val[pos * 2 + 2]); } void resolve_dval(int pos, int l, int r) { // posで遅延を解消して子孫に押し付ける refl_dval(val[pos], dval[pos]); if (pos < head) { add_dval(dval[pos * 2 + 1], dval[pos]); add_dval(dval[pos * 2 + 2], dval[pos]); } dval[pos] = I_dval; } void update(int begin, int end, DVAL dval_q) { update(begin, end, dval_q, 0, 0, head + 1); } void update(int begin, int end, DVAL dval_q, int pos, int l, int r) { if (begin <= l && r <= end) { // 担当区間全部使う add_dval(dval[pos], dval_q); resolve_dval(pos, l, r); } else if (begin < r && l < end) { // 少なくともどこかで交差 resolve_dval(pos, l, r); update(begin, end, dval_q, pos * 2 + 1, l, (l + r) / 2); update(begin, end, dval_q, pos * 2 + 2, (l + r) / 2, r); val[pos] = merge_val(val[pos * 2 + 1], val[pos * 2 + 2]); } else resolve_dval(pos, l, r); } VAL getvalue(int begin, int end) { return getvalue(begin, end, 0, 0, head + 1); } VAL getvalue(int begin, int end, int pos, int l, int r) { resolve_dval(pos, l, r); if (begin <= l && r <= end) return val[pos]; else if (begin < r && l < end) { VAL vl = getvalue(begin, end, pos * 2 + 1, l, (l + r) / 2); VAL vr = getvalue(begin, end, pos * 2 + 2, (l + r) / 2, r); val[pos] = merge_val(val[pos * 2 + 1], val[pos * 2 + 2]); return merge_val(vl, vr); } else return I_val; } }; constexpr int Nmax = 100001; vector<pint> rla[Nmax]; int main() { int N, M; cin >> N >> M; REP(_, M) { int l, r, a; cin >> l >> r >> a; rla[r].push_back(pint(l, a)); } vector<lint> tmp(N + 1, 0); LazySegTree<lint, lint> dp( tmp, 0, 0, [](lint a, lint b) { return max(a, b); }, // 本情報のmerge [](lint &a, lint b) { a += b; }, // 遅延情報の子孫への伝播 [](lint &a, lint b) { a += b; }); // 遅延情報の本情報への反映 FOR(r, 1, N + 1) { dp.update(r, r + 1, dp.getvalue(1, r)); for (auto pa : rla[r]) { int l = pa.first; lint a = pa.second; dp.update(l, r + 1, a); } } cout << dp.getvalue(1, N + 1) << endl; }
#include <bits/stdc++.h> using namespace std; using lint = long long int; using pint = pair<int, int>; using plint = pair<lint, lint>; struct fast_ios { fast_ios() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; #define FOR(i, begin, end) \ for (int i = (begin), i##_end_ = (end); i < i##_end_; i++) #define IFOR(i, begin, end) \ for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--) #define REP(i, n) FOR(i, 0, n) #define IREP(i, n) IFOR(i, 0, n) template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; } template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ","; os << "]"; return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_set<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) { os << "(" << pa.first << "," << pa.second << ")"; return os; } template <typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp) { os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; } template <typename TK, typename TV> ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp) { os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; } template <typename T> void ndarray(vector<T> &vec, int len) { vec.resize(len); } template <typename T, typename... Args> void ndarray(vector<T> &vec, int len, Args... args) { vec.resize(len); for (auto &v : vec) ndarray(v, args...); } template <typename T> void Max(T &m, const T q) { if (m < q) m = q; } template <typename T> void Min(T &m, const T q) { if (m > q) m = q; } template <typename T1, typename T2> pair<T1, T2> operator+(pair<T1, T2> &l, pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); } template <typename T1, typename T2> pair<T1, T2> operator-(pair<T1, T2> &l, pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); } #define dbg(x) \ cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl; template <typename VAL, typename DVAL> struct LazySegTree { int N; int head; vector<VAL> val; vector<DVAL> dval; VAL I_val; DVAL I_dval; using vv2v = function<VAL(const VAL &, const VAL &)>; using d2d = function<void(DVAL &, const DVAL &)>; using d2v = function<void(VAL &, const DVAL &)>; vv2v merge_val; d2d add_dval; d2v refl_dval; LazySegTree(vector<VAL> &val_init, VAL val_default, DVAL dval_default, vv2v f, d2d dadd, d2v dreflect) : N(val_init.size()), I_val(val_default), I_dval(dval_default), merge_val(f), add_dval(dadd), refl_dval(dreflect) { int N_tmp = 1; while (N_tmp < N) N_tmp <<= 1; val = vector<VAL>(N_tmp * 2, I_val); dval = vector<DVAL>(N_tmp * 2, I_dval); head = N_tmp - 1; for (int i = 0; i < N; i++) val[head + i] = val_init[i]; for (int pos = head - 1; pos >= 0; pos--) val[pos] = merge_val(val[pos * 2 + 1], val[pos * 2 + 2]); } void resolve_dval(int pos, int l, int r) { // posで遅延を解消して子孫に押し付ける refl_dval(val[pos], dval[pos]); if (pos < head) { add_dval(dval[pos * 2 + 1], dval[pos]); add_dval(dval[pos * 2 + 2], dval[pos]); } dval[pos] = I_dval; } void update(int begin, int end, DVAL dval_q) { update(begin, end, dval_q, 0, 0, head + 1); } void update(int begin, int end, DVAL dval_q, int pos, int l, int r) { if (begin <= l && r <= end) { // 担当区間全部使う add_dval(dval[pos], dval_q); resolve_dval(pos, l, r); } else if (begin < r && l < end) { // 少なくともどこかで交差 resolve_dval(pos, l, r); update(begin, end, dval_q, pos * 2 + 1, l, (l + r) / 2); update(begin, end, dval_q, pos * 2 + 2, (l + r) / 2, r); val[pos] = merge_val(val[pos * 2 + 1], val[pos * 2 + 2]); } else resolve_dval(pos, l, r); } VAL getvalue(int begin, int end) { return getvalue(begin, end, 0, 0, head + 1); } VAL getvalue(int begin, int end, int pos, int l, int r) { resolve_dval(pos, l, r); if (begin <= l && r <= end) return val[pos]; else if (begin < r && l < end) { VAL vl = getvalue(begin, end, pos * 2 + 1, l, (l + r) / 2); VAL vr = getvalue(begin, end, pos * 2 + 2, (l + r) / 2, r); val[pos] = merge_val(val[pos * 2 + 1], val[pos * 2 + 2]); return merge_val(vl, vr); } else return I_val; } }; constexpr int Nmax = 200001; vector<pint> rla[Nmax]; int main() { int N, M; cin >> N >> M; REP(_, M) { int l, r, a; cin >> l >> r >> a; rla[r].push_back(pint(l, a)); } vector<lint> tmp(N + 1, 0); LazySegTree<lint, lint> dp( tmp, 0, 0, [](lint a, lint b) { return max(a, b); }, // 本情報のmerge [](lint &a, lint b) { a += b; }, // 遅延情報の子孫への伝播 [](lint &a, lint b) { a += b; }); // 遅延情報の本情報への反映 FOR(r, 1, N + 1) { dp.update(r, r + 1, dp.getvalue(1, r)); for (auto pa : rla[r]) { int l = pa.first; lint a = pa.second; dp.update(l, r + 1, a); } } cout << dp.getvalue(1, N + 1) << endl; }
replace
189
190
189
190
0
p03182
C++
Runtime Error
#include "bits/stdc++.h" #define int long long using namespace std; const int TREE_SZ = 1 << 19; vector<int> mx(TREE_SZ, -2e18); vector<int> buf(TREE_SZ); int upd(int v, int l, int r) { mx[v] += buf[v]; if (l < r - 1) { buf[2 * v + 1] += buf[v]; buf[2 * v + 2] += buf[v]; } buf[v] = 0; } void add_seg(int v, int l, int r, int L, int R, int x) { upd(v, l, r); if (r <= L || R <= l) { return; } if (L <= l && r <= R) { buf[v] += x; upd(v, l, r); return; } int m = (l + r) / 2; add_seg(2 * v + 1, l, m, L, R, x); add_seg(2 * v + 2, m, r, L, R, x); mx[v] = max(mx[2 * v + 1], mx[2 * v + 2]); } void set_elem(int v, int l, int r, int i, int x) { upd(v, l, r); if (l == r - 1) { mx[v] = x; return; } int m = (l + r) / 2; if (i < m) { set_elem(2 * v + 1, l, m, i, x); upd(2 * v + 2, m, r); } else { upd(2 * v + 1, l, m); set_elem(2 * v + 2, m, r, i, x); } mx[v] = max(mx[2 * v + 1], mx[2 * v + 2]); } int32_t main() { int n, m; cin >> n >> m; vector<int> l(n, 0); vector<vector<pair<int, int>>> r(n + 1); for (int i = 0; i < m; i++) { int L, R, a; cin >> L >> R >> a; L--; l[L] += a; r[R].push_back({L, a}); } int cur_max = 0; set_elem(0, -1, n, -1, 0); for (int i = 0; i < n; i++) { add_seg(0, -1, n, -1, i, l[i]); for (auto x : r[i]) { add_seg(0, -1, n, -1, x.first, -x.second); } cur_max = max(cur_max, mx[0]); set_elem(0, -1, n, i, mx[0]); } cout << cur_max << endl; }
#include "bits/stdc++.h" #define int long long using namespace std; const int TREE_SZ = 1 << 19; vector<int> mx(TREE_SZ, -2e18); vector<int> buf(TREE_SZ); void upd(int v, int l, int r) { mx[v] += buf[v]; if (l < r - 1) { buf[2 * v + 1] += buf[v]; buf[2 * v + 2] += buf[v]; } buf[v] = 0; } void add_seg(int v, int l, int r, int L, int R, int x) { upd(v, l, r); if (r <= L || R <= l) { return; } if (L <= l && r <= R) { buf[v] += x; upd(v, l, r); return; } int m = (l + r) / 2; add_seg(2 * v + 1, l, m, L, R, x); add_seg(2 * v + 2, m, r, L, R, x); mx[v] = max(mx[2 * v + 1], mx[2 * v + 2]); } void set_elem(int v, int l, int r, int i, int x) { upd(v, l, r); if (l == r - 1) { mx[v] = x; return; } int m = (l + r) / 2; if (i < m) { set_elem(2 * v + 1, l, m, i, x); upd(2 * v + 2, m, r); } else { upd(2 * v + 1, l, m); set_elem(2 * v + 2, m, r, i, x); } mx[v] = max(mx[2 * v + 1], mx[2 * v + 2]); } int32_t main() { int n, m; cin >> n >> m; vector<int> l(n, 0); vector<vector<pair<int, int>>> r(n + 1); for (int i = 0; i < m; i++) { int L, R, a; cin >> L >> R >> a; L--; l[L] += a; r[R].push_back({L, a}); } int cur_max = 0; set_elem(0, -1, n, -1, 0); for (int i = 0; i < n; i++) { add_seg(0, -1, n, -1, i, l[i]); for (auto x : r[i]) { add_seg(0, -1, n, -1, x.first, -x.second); } cur_max = max(cur_max, mx[0]); set_elem(0, -1, n, i, mx[0]); } cout << cur_max << endl; }
replace
9
10
9
10
0
p03182
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define pb push_back #define fi first #define se second #define pq priority_queue #define mp make_pair #define pii pair<int, int> #define mod 998244353 int lowbit(int x) { return x & (-x); } int n, m; vector<pii> p[200010]; long long tree[400040]; long long tag[400040]; void update(int cur, int l, int r, int ll, int rr, long long c) { if (ll <= l && rr >= r) { tree[cur] += c; tag[cur] += c; return; } int mid = (l + r) >> 1; if (ll <= mid) update(cur * 2, l, mid, ll, rr, c); if (rr > mid) update(cur * 2 + 1, mid + 1, r, ll, rr, c); tree[cur] = max(tree[cur * 2], tree[cur * 2 + 1]) + tag[cur]; } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { int l, r, a; scanf("%d%d%d", &l, &r, &a); p[r].pb(mp(l, a)); } for (int i = 1; i <= n; i++) { update(1, 1, n, i, i, tree[1]); for (int j = 0; j < p[i].size(); j++) update(1, 1, n, p[i][j].fi, i, p[i][j].se); } cout << max(0ll, tree[1]); return 0; }
#include <bits/stdc++.h> using namespace std; #define pb push_back #define fi first #define se second #define pq priority_queue #define mp make_pair #define pii pair<int, int> #define mod 998244353 int lowbit(int x) { return x & (-x); } int n, m; vector<pii> p[200010]; long long tree[800040]; long long tag[800040]; void update(int cur, int l, int r, int ll, int rr, long long c) { if (ll <= l && rr >= r) { tree[cur] += c; tag[cur] += c; return; } int mid = (l + r) >> 1; if (ll <= mid) update(cur * 2, l, mid, ll, rr, c); if (rr > mid) update(cur * 2 + 1, mid + 1, r, ll, rr, c); tree[cur] = max(tree[cur * 2], tree[cur * 2 + 1]) + tag[cur]; } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { int l, r, a; scanf("%d%d%d", &l, &r, &a); p[r].pb(mp(l, a)); } for (int i = 1; i <= n; i++) { update(1, 1, n, i, i, tree[1]); for (int j = 0; j < p[i].size(); j++) update(1, 1, n, p[i][j].fi, i, p[i][j].se); } cout << max(0ll, tree[1]); return 0; }
replace
15
17
15
17
0
p03182
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define X first #define Y second #define pb push_back #define mp make_pair #define all(a) a.begin(), a.end() #define rep(i, st, n) for (int i = (st); i < (n); ++i) #define repinv(i, st, n) for (int i = ((n)-1); i >= (st); --i) #define MEM(a, b) memset(a, b, sizeof(a)); #define debug(x) std::cout << #x << ": " << x << endl #define fastIO() \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0) #define fileIO(in, out) \ freopen(in, "r", stdin); \ freopen(out, "w", stdout) typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<ll> vll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef long double ld; // mt19937 mrand(random_device{}()); const ll mod = 1e9 + 7; // int rnd(int x) { return mrand() % x;} ll powmod(ll a, ll b) { ll res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } pii operator+(const pii &x, const pii &y) { return mp(x.X + y.X, x.Y + y.Y); } pii operator-(const pii &x, const pii &y) { return mp(x.X - y.X, x.Y - y.Y); } // INT_MAX, ULLONG_MAX, LLONG_MAX or numeric_limits<int>::min() inline ll read() { char ch = getchar(); ll x = 0, f = 0; while (ch < '0' || ch > '9') f |= ch == '-', ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return f ? -x : x; } //------------------------------------------------------------------------// int T; const int maxn = 2e5 + 7; ll ls[maxn]; vector<pair<ll, int>> rs[maxn]; // I think can do compression if n is 1e9, m is 1e5 (by unique and reassigning // li, ri) // T should be comparable and have +=, judge nullity // modify LL min to wanted min template <class T> struct segment_tree { int n, N; vector<T> a; vector<T> lazy; segment_tree(int n) : a(n * 2 + 5, 0), lazy(n * 2 + 5, 0), n(n), N(n * 2 + 5) { } // remember this write way void push(int root) { if (lazy[root]) { int lc = root << 1, rc = (root << 1) ^ 1; if (lc < N) lazy[lc] += lazy[root]; if (rc < N) lazy[rc] += lazy[root]; a[root] += lazy[root]; lazy[root] = 0; } } T pull(int root) { push(root); int lc = root << 1, rc = (root << 1) ^ 1; if (lc > N && rc > N) return a[root]; T &ret = a[root]; ret = LLONG_MIN; // this should if (lc < N) push(lc); if (rc < N) push(rc); if (lc < N) ret = max(ret, a[lc]); if (rc < N) ret = max(ret, a[rc]); return ret; } void add(int x, int y, T val, int l, int r, int root) { if (l > r) return; if (x <= l && r <= y) { lazy[root] += val; return; } push(root); int M = (l + r) >> 1; int lc = root << 1, rc = (root << 1) ^ 1; if (x <= M) add(x, y, val, l, M, lc); if (y > M) add(x, y, val, M + 1, r, rc); pull(root); } T query(int x, int y, int l, int r, int root) { if (l > r) return LLONG_MIN; push(root); if (x <= l && r <= y) return a[root]; int M = (l + r) >> 1; int lc = root << 1, rc = (root << 1) ^ 1; T ret = LLONG_MIN; if (x <= M) ret = max(ret, query(x, y, l, M, lc)); if (y > M) ret = max(ret, query(x, y, M + 1, r, rc)); return ret; } void print(int l, int r, int root) // for debug seg { push(root); cout << "Node " << root << " range is " << l << ' ' << r << " val is " << a[root] + lazy[root] << endl; int M = (l + r) >> 1; int lc = root << 1, rc = (root << 1) ^ 1; if (lc < N) print(l, M, lc); if (rc < N) print(M + 1, r, rc); } }; int n, m; void solve() { cin >> n >> m; ll li, ri, si; memset(ls, 0, sizeof(ls)); rep(i, 0, m) { cin >> li >> ri >> si; ls[li] += si; rs[ri].pb(mp(si, li)); } // build(0, n); segment_tree<ll> seg(n); ll ans = 0; // min ans = 0; rep(i, 1, n + 1) { ll dpi = max(0ll, seg.query(1, max(1, i - 1), 1, n, 1)); seg.add(i, i, dpi, 1, n, 1); for (pair<ll, int> &ri : rs[i]) seg.add(ri.Y, i, ri.X, 1, n, 1); /* if(ls[i]) seg.add(0, max(1, i-1), ls[i], 0, n, 1); seg.print(0, n, 1); ll cur = seg.query(0, i, 1, n, 1); debug(i); debug(cur); ans = max(ans, seg.query(1, i, 1, n, 1)); for(pair<ll, int>& ri : rs[i]) seg.add(1, max(1, ri.Y-1), -ri.X, 1, n, 1);*/ } // seg.print(1, n, 1); ans = max(ans, seg.query(1, n, 1, n, 1)); cout << ans << endl; return; } signed main() { fastIO(); T = 1; // cin >> T; //this while (T--) solve(); return 0; } /* stuck: * do observations and see properties * binary search? DP? DS? flow? * be organized before upload: * int overflow, array bounds * special cases (min, max, special) */
#include <bits/stdc++.h> using namespace std; #define X first #define Y second #define pb push_back #define mp make_pair #define all(a) a.begin(), a.end() #define rep(i, st, n) for (int i = (st); i < (n); ++i) #define repinv(i, st, n) for (int i = ((n)-1); i >= (st); --i) #define MEM(a, b) memset(a, b, sizeof(a)); #define debug(x) std::cout << #x << ": " << x << endl #define fastIO() \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0) #define fileIO(in, out) \ freopen(in, "r", stdin); \ freopen(out, "w", stdout) typedef long long ll; typedef unsigned long long ull; typedef vector<int> vi; typedef vector<ll> vll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef long double ld; // mt19937 mrand(random_device{}()); const ll mod = 1e9 + 7; // int rnd(int x) { return mrand() % x;} ll powmod(ll a, ll b) { ll res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } pii operator+(const pii &x, const pii &y) { return mp(x.X + y.X, x.Y + y.Y); } pii operator-(const pii &x, const pii &y) { return mp(x.X - y.X, x.Y - y.Y); } // INT_MAX, ULLONG_MAX, LLONG_MAX or numeric_limits<int>::min() inline ll read() { char ch = getchar(); ll x = 0, f = 0; while (ch < '0' || ch > '9') f |= ch == '-', ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return f ? -x : x; } //------------------------------------------------------------------------// int T; const int maxn = 2e5 + 7; ll ls[maxn]; vector<pair<ll, int>> rs[maxn]; // I think can do compression if n is 1e9, m is 1e5 (by unique and reassigning // li, ri) // T should be comparable and have +=, judge nullity // modify LL min to wanted min template <class T> struct segment_tree { int n, N; vector<T> a; vector<T> lazy; segment_tree(int n) : a(n * 8 + 5, 0), lazy(n * 8 + 5, 0), n(n), N(n * 8 + 5) { } // remember this write way void push(int root) { if (lazy[root]) { int lc = root << 1, rc = (root << 1) ^ 1; if (lc < N) lazy[lc] += lazy[root]; if (rc < N) lazy[rc] += lazy[root]; a[root] += lazy[root]; lazy[root] = 0; } } T pull(int root) { push(root); int lc = root << 1, rc = (root << 1) ^ 1; if (lc > N && rc > N) return a[root]; T &ret = a[root]; ret = LLONG_MIN; // this should if (lc < N) push(lc); if (rc < N) push(rc); if (lc < N) ret = max(ret, a[lc]); if (rc < N) ret = max(ret, a[rc]); return ret; } void add(int x, int y, T val, int l, int r, int root) { if (l > r) return; if (x <= l && r <= y) { lazy[root] += val; return; } push(root); int M = (l + r) >> 1; int lc = root << 1, rc = (root << 1) ^ 1; if (x <= M) add(x, y, val, l, M, lc); if (y > M) add(x, y, val, M + 1, r, rc); pull(root); } T query(int x, int y, int l, int r, int root) { if (l > r) return LLONG_MIN; push(root); if (x <= l && r <= y) return a[root]; int M = (l + r) >> 1; int lc = root << 1, rc = (root << 1) ^ 1; T ret = LLONG_MIN; if (x <= M) ret = max(ret, query(x, y, l, M, lc)); if (y > M) ret = max(ret, query(x, y, M + 1, r, rc)); return ret; } void print(int l, int r, int root) // for debug seg { push(root); cout << "Node " << root << " range is " << l << ' ' << r << " val is " << a[root] + lazy[root] << endl; int M = (l + r) >> 1; int lc = root << 1, rc = (root << 1) ^ 1; if (lc < N) print(l, M, lc); if (rc < N) print(M + 1, r, rc); } }; int n, m; void solve() { cin >> n >> m; ll li, ri, si; memset(ls, 0, sizeof(ls)); rep(i, 0, m) { cin >> li >> ri >> si; ls[li] += si; rs[ri].pb(mp(si, li)); } // build(0, n); segment_tree<ll> seg(n); ll ans = 0; // min ans = 0; rep(i, 1, n + 1) { ll dpi = max(0ll, seg.query(1, max(1, i - 1), 1, n, 1)); seg.add(i, i, dpi, 1, n, 1); for (pair<ll, int> &ri : rs[i]) seg.add(ri.Y, i, ri.X, 1, n, 1); /* if(ls[i]) seg.add(0, max(1, i-1), ls[i], 0, n, 1); seg.print(0, n, 1); ll cur = seg.query(0, i, 1, n, 1); debug(i); debug(cur); ans = max(ans, seg.query(1, i, 1, n, 1)); for(pair<ll, int>& ri : rs[i]) seg.add(1, max(1, ri.Y-1), -ri.X, 1, n, 1);*/ } // seg.print(1, n, 1); ans = max(ans, seg.query(1, n, 1, n, 1)); cout << ans << endl; return; } signed main() { fastIO(); T = 1; // cin >> T; //this while (T--) solve(); return 0; } /* stuck: * do observations and see properties * binary search? DP? DS? flow? * be organized before upload: * int overflow, array bounds * special cases (min, max, special) */
replace
76
77
76
77
0
p03182
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef vector<int> VI; typedef vector<VI> VVI; typedef long long LL; typedef pair<int, int> PI; typedef pair<LL, LL> PLL; typedef unsigned long long ULL; typedef pair<double, double> PD; #define FOR(x, b, e) for (int x = b; x <= (e); x++) #define FORD(x, b, e) for (int x = b; x >= (e); x--) #define REP(x, n) for (int x = 0; x < (n); ++x) #define ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int)(x).size()) #define PB push_back #define IN insert #define ST first #define ND second #define INF 2000000011 #define MOD 1000000007 #define MAXS 200010 #define MAXT 524300 PLL tree[MAXT]; //{max,nakl} vector<PLL> zb[MAXS]; vector<LL> mk[MAXS]; LL wyn = 0; int n, m; void upd(int v, int a, int b, int l, int r, LL val) { if (a > r || b < l || v >= MAXT) return; if (l <= a && b <= r) { tree[v].ST += val; tree[v].ND += val; return; } tree[v * 2].ND += tree[v].ND; tree[v * 2 + 1].ND += tree[v].ND; tree[v * 2].ST += tree[v].ND; tree[v * 2 + 1].ST += tree[v].ND; tree[v].ND = 0; int sr = (a + b - 1) / 2; upd(v * 2, a, sr, l, r, val); upd(v * 2 + 1, sr + 1, b, l, r, val); tree[v].ST = max(tree[2 * v + 1].ST, tree[2 * v].ST); } LL query(int v, int a, int b, int l, int r) { if (a > r || b < l || v >= MAXT) return LONG_LONG_MIN; if (l <= a && b <= r) return tree[v].ST; tree[v * 2].ND += tree[v].ND; tree[v * 2 + 1].ND += tree[v].ND; tree[v * 2].ST += tree[v].ND; tree[v * 2 + 1].ST += tree[v].ND; tree[v].ND = 0; int sr = (a + b - 1) / 2; return max(query(v * 2, a, sr, l, r), query(v * 2 + 1, sr + 1, b, l, r)); } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> m; int szer = 1; while (szer <= n) szer *= 2; szer *= 2; REP(I, m) { int t1, t2, a1; cin >> t1 >> t2 >> a1; zb[t2].PB({t1, a1}); mk[t1].PB(a1); } LL wyn = 0; FOR(i, 1, n) { for (auto x : mk[i]) upd(1, 0, szer - 1, 0, i - 1, x); LL val = query(1, 0, szer - 1, 0, i - 1); wyn = max(wyn, val); upd(1, 0, szer - 1, i, i, val); for (auto x : zb[i]) upd(1, 0, szer - 1, 0, x.ST - 1, -x.ND); } cout << tree[1].ST; return 0; }
#include <bits/stdc++.h> using namespace std; typedef vector<int> VI; typedef vector<VI> VVI; typedef long long LL; typedef pair<int, int> PI; typedef pair<LL, LL> PLL; typedef unsigned long long ULL; typedef pair<double, double> PD; #define FOR(x, b, e) for (int x = b; x <= (e); x++) #define FORD(x, b, e) for (int x = b; x >= (e); x--) #define REP(x, n) for (int x = 0; x < (n); ++x) #define ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int)(x).size()) #define PB push_back #define IN insert #define ST first #define ND second #define INF 2000000011 #define MOD 1000000007 #define MAXS 2000100 #define MAXT 5243000 PLL tree[MAXT]; //{max,nakl} vector<PLL> zb[MAXS]; vector<LL> mk[MAXS]; LL wyn = 0; int n, m; void upd(int v, int a, int b, int l, int r, LL val) { if (a > r || b < l || v >= MAXT) return; if (l <= a && b <= r) { tree[v].ST += val; tree[v].ND += val; return; } tree[v * 2].ND += tree[v].ND; tree[v * 2 + 1].ND += tree[v].ND; tree[v * 2].ST += tree[v].ND; tree[v * 2 + 1].ST += tree[v].ND; tree[v].ND = 0; int sr = (a + b - 1) / 2; upd(v * 2, a, sr, l, r, val); upd(v * 2 + 1, sr + 1, b, l, r, val); tree[v].ST = max(tree[2 * v + 1].ST, tree[2 * v].ST); } LL query(int v, int a, int b, int l, int r) { if (a > r || b < l || v >= MAXT) return LONG_LONG_MIN; if (l <= a && b <= r) return tree[v].ST; tree[v * 2].ND += tree[v].ND; tree[v * 2 + 1].ND += tree[v].ND; tree[v * 2].ST += tree[v].ND; tree[v * 2 + 1].ST += tree[v].ND; tree[v].ND = 0; int sr = (a + b - 1) / 2; return max(query(v * 2, a, sr, l, r), query(v * 2 + 1, sr + 1, b, l, r)); } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> m; int szer = 1; while (szer <= n) szer *= 2; szer *= 2; REP(I, m) { int t1, t2, a1; cin >> t1 >> t2 >> a1; zb[t2].PB({t1, a1}); mk[t1].PB(a1); } LL wyn = 0; FOR(i, 1, n) { for (auto x : mk[i]) upd(1, 0, szer - 1, 0, i - 1, x); LL val = query(1, 0, szer - 1, 0, i - 1); wyn = max(wyn, val); upd(1, 0, szer - 1, i, i, val); for (auto x : zb[i]) upd(1, 0, szer - 1, 0, x.ST - 1, -x.ND); } cout << tree[1].ST; return 0; }
replace
24
26
24
26
0
p03182
C++
Time Limit Exceeded
/* 数据不清空,爆零两行泪。 多测不读完,爆零两行泪。 边界不特判,爆零两行泪。 贪心不证明,爆零两行泪。 D P 顺序错,爆零两行泪。 大小少等号,爆零两行泪。 变量不统一,爆零两行泪。 越界不判断,爆零两行泪。 调试不注释,爆零两行泪。 溢出不 l l,爆零两行泪。 */ #include <bits/stdc++.h> using namespace std; #define int long long #define pb push_back #define mp make_pair #define X first #define Y second const int inf = 0x3f3f3f3f3f3f3f3f; const int N = 200000; int n, m; vector<pair<int, int>> rg[N + 1]; struct segtree { struct node { int l, r, mx, lz; } nd[N << 2]; #define l(p) nd[p].l #define r(p) nd[p].r #define mx(p) nd[p].mx #define lz(p) nd[p].lz void bld(int l = 1, int r = n, int p = 1) { l(p) = l; r(p) = r; mx(p) = lz(p) = 0; if (l == r) return; int mid = l + r >> 1; bld(l, mid, p << 1); bld(mid + 1, r, p << 1 | 1); } void init() { bld(); } void sprdwn(int p) { if (lz(p)) { mx(p << 1) += lz(p); mx(p << 1 | 1) += lz(p); lz(p << 1) += lz(p); lz(p << 1 | 1) += lz(p); lz(p) = 0; } } void sprup(int p) { mx(p) = max(mx(p << 1), mx(p << 1 | 1)); } void _add(int l, int r, int v, int p = 1) { if (l <= l(p) && r >= r(p)) { mx(p) += v; lz(p) += v; return; } sprdwn(p); int mid = l(p) + r(p) >> 1; if (l <= mid) _add(l, r, v, p << 1); if (r > mid) _add(l, r, v, p << 1 | 1); sprup(p); } void add(int l, int r, int v) { if (l <= r) add(l, r, v); } int _mx() { return mx(1); } } segt; int dp[N + 1][2]; signed main() { cin >> n >> m; while (m--) { int l, r, a; scanf("%lld%lld%lld", &l, &r, &a); rg[r].pb(mp(l, a)); } segt.init(); for (int i = 1; i <= n; i++) { vector<pair<int, int>> &v = rg[i]; v.pb(mp(i, inf)); sort(v.begin(), v.end()); int sum = 0; for (int j = 0; j + 1 < v.size(); j++) { sum += v[j].Y; segt.add(v[j].X, v[j + 1].X - 1, sum); } dp[i][1] = max(dp[i - 1][0], dp[i - 1][1]) + sum; dp[i][0] = segt._mx(); segt.add(i, i, dp[i][1]); } cout << max(dp[n][0], dp[n][1]); return 0; } /*1 5 3 1 3 10 2 4 -10 3 5 10 */
/* 数据不清空,爆零两行泪。 多测不读完,爆零两行泪。 边界不特判,爆零两行泪。 贪心不证明,爆零两行泪。 D P 顺序错,爆零两行泪。 大小少等号,爆零两行泪。 变量不统一,爆零两行泪。 越界不判断,爆零两行泪。 调试不注释,爆零两行泪。 溢出不 l l,爆零两行泪。 */ #include <bits/stdc++.h> using namespace std; #define int long long #define pb push_back #define mp make_pair #define X first #define Y second const int inf = 0x3f3f3f3f3f3f3f3f; const int N = 200000; int n, m; vector<pair<int, int>> rg[N + 1]; struct segtree { struct node { int l, r, mx, lz; } nd[N << 2]; #define l(p) nd[p].l #define r(p) nd[p].r #define mx(p) nd[p].mx #define lz(p) nd[p].lz void bld(int l = 1, int r = n, int p = 1) { l(p) = l; r(p) = r; mx(p) = lz(p) = 0; if (l == r) return; int mid = l + r >> 1; bld(l, mid, p << 1); bld(mid + 1, r, p << 1 | 1); } void init() { bld(); } void sprdwn(int p) { if (lz(p)) { mx(p << 1) += lz(p); mx(p << 1 | 1) += lz(p); lz(p << 1) += lz(p); lz(p << 1 | 1) += lz(p); lz(p) = 0; } } void sprup(int p) { mx(p) = max(mx(p << 1), mx(p << 1 | 1)); } void _add(int l, int r, int v, int p = 1) { if (l <= l(p) && r >= r(p)) { mx(p) += v; lz(p) += v; return; } sprdwn(p); int mid = l(p) + r(p) >> 1; if (l <= mid) _add(l, r, v, p << 1); if (r > mid) _add(l, r, v, p << 1 | 1); sprup(p); } void add(int l, int r, int v) { if (l <= r) _add(l, r, v); } int _mx() { return mx(1); } } segt; int dp[N + 1][2]; signed main() { cin >> n >> m; while (m--) { int l, r, a; scanf("%lld%lld%lld", &l, &r, &a); rg[r].pb(mp(l, a)); } segt.init(); for (int i = 1; i <= n; i++) { vector<pair<int, int>> &v = rg[i]; v.pb(mp(i, inf)); sort(v.begin(), v.end()); int sum = 0; for (int j = 0; j + 1 < v.size(); j++) { sum += v[j].Y; segt.add(v[j].X, v[j + 1].X - 1, sum); } dp[i][1] = max(dp[i - 1][0], dp[i - 1][1]) + sum; dp[i][0] = segt._mx(); segt.add(i, i, dp[i][1]); } cout << max(dp[n][0], dp[n][1]); return 0; } /*1 5 3 1 3 10 2 4 -10 3 5 10 */
replace
68
69
68
69
TLE
p03183
C++
Memory Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> P; typedef pair<ll, ll> l_l; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<P> vp; typedef vector<l_l> vpl; typedef vector<string> vs; typedef pair<l_l, ll> lll; #define pb push_back #define sz(x) (int)(x).size() #define fi first #define se second #define rep(i, n) for (int i = 0; i < (n); i++) #define rrep(i, n) for (int i = 1; i <= (n); i++) const int INF = 1001001000; const int mINF = -1001001000; const ll LINF = 10100100100100100; const ll dx[4] = {1, -1, 0, 0}; const ll dy[4] = {0, 0, 1, -1}; template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } ll n; ll w[1100], s[1100], v[1100]; ll dp[1100][220000]; int main() { cin >> n; rep(i, n) cin >> w[i] >> s[i] >> v[i]; vl block; rep(i, n) block.pb(i); sort(block.begin(), block.end(), [&](int x, int y) { return s[x] + w[x] < s[y] + w[y]; }); for (int i = 1; i <= n; i++) { ll ind = block[i - 1]; for (int j = 0; j < 220000; j++) { dp[i][j] = dp[i - 1][j]; if (j - w[ind] <= s[ind] && j - w[ind] >= 0) { dp[i][j] = max(dp[i][j], dp[i - 1][j - w[ind]] + v[ind]); } } } ll ans = 0; rep(i, 220000) chmax(ans, dp[n][i]); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> P; typedef pair<ll, ll> l_l; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<P> vp; typedef vector<l_l> vpl; typedef vector<string> vs; typedef pair<l_l, ll> lll; #define pb push_back #define sz(x) (int)(x).size() #define fi first #define se second #define rep(i, n) for (int i = 0; i < (n); i++) #define rrep(i, n) for (int i = 1; i <= (n); i++) const int INF = 1001001000; const int mINF = -1001001000; const ll LINF = 10100100100100100; const ll dx[4] = {1, -1, 0, 0}; const ll dy[4] = {0, 0, 1, -1}; template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } ll n; ll w[1100], s[1100], v[1100]; ll dp[1100][22000]; int main() { cin >> n; rep(i, n) cin >> w[i] >> s[i] >> v[i]; vl block; rep(i, n) block.pb(i); sort(block.begin(), block.end(), [&](int x, int y) { return s[x] + w[x] < s[y] + w[y]; }); for (int i = 1; i <= n; i++) { ll ind = block[i - 1]; for (int j = 0; j < 220000; j++) { dp[i][j] = dp[i - 1][j]; if (j - w[ind] <= s[ind] && j - w[ind] >= 0) { dp[i][j] = max(dp[i][j], dp[i - 1][j - w[ind]] + v[ind]); } } } ll ans = 0; rep(i, 220000) chmax(ans, dp[n][i]); cout << ans << endl; return 0; }
replace
39
40
39
40
MLE
p03183
C++
Runtime Error
#include <bits/stdc++.h> #define x first #define y second #define y1 Y1 #define y2 Y2 #define mp make_pair #define pb push_back using namespace std; typedef long long LL; typedef pair<int, int> pii; template <typename T> inline int Chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; } template <typename T> inline int Chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; } template <typename T> T read() { T sum = 0, fl = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') fl = -1; for (; isdigit(ch); ch = getchar()) sum = (sum << 3) + (sum << 1) + ch - '0'; return sum * fl; } inline void proc_status() { ifstream t("/proc/self/status"); cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>()) << endl; } const int Maxn = 1e3 + 100; int N; LL Dp[10000 + 100]; struct info { int w, s, v; } A[Maxn]; inline int cmp(info a, info b) { return a.s + a.w < b.s + b.w; } int Max; inline void Solve() { sort(A + 1, A + N + 1, cmp); LL ans = 0; for (int i = 1; i <= N; ++i) for (int j = A[i].s; j >= 0; --j) { Chkmax(Dp[j + A[i].w], Dp[j] + A[i].v); Chkmax(ans, Dp[j + A[i].w]); } cout << ans << endl; } inline void Input() { N = read<int>(); for (int i = 1; i <= N; ++i) A[i].w = read<int>(), A[i].s = read<int>(), A[i].v = read<int>(), Chkmax(Max, A[i].s); } int main() { #ifdef hk_cnyali freopen("X.in", "r", stdin); freopen("X.out", "w", stdout); #endif Input(); Solve(); return 0; }
#include <bits/stdc++.h> #define x first #define y second #define y1 Y1 #define y2 Y2 #define mp make_pair #define pb push_back using namespace std; typedef long long LL; typedef pair<int, int> pii; template <typename T> inline int Chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; } template <typename T> inline int Chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; } template <typename T> T read() { T sum = 0, fl = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') fl = -1; for (; isdigit(ch); ch = getchar()) sum = (sum << 3) + (sum << 1) + ch - '0'; return sum * fl; } inline void proc_status() { ifstream t("/proc/self/status"); cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>()) << endl; } const int Maxn = 1e3 + 100; int N; LL Dp[20000 + 100]; struct info { int w, s, v; } A[Maxn]; inline int cmp(info a, info b) { return a.s + a.w < b.s + b.w; } int Max; inline void Solve() { sort(A + 1, A + N + 1, cmp); LL ans = 0; for (int i = 1; i <= N; ++i) for (int j = A[i].s; j >= 0; --j) { Chkmax(Dp[j + A[i].w], Dp[j] + A[i].v); Chkmax(ans, Dp[j + A[i].w]); } cout << ans << endl; } inline void Input() { N = read<int>(); for (int i = 1; i <= N; ++i) A[i].w = read<int>(), A[i].s = read<int>(), A[i].v = read<int>(), Chkmax(Max, A[i].s); } int main() { #ifdef hk_cnyali freopen("X.in", "r", stdin); freopen("X.out", "w", stdout); #endif Input(); Solve(); return 0; }
replace
40
41
40
41
0
p03183
C++
Runtime Error
/* ___mennat khoday ra azz o va jall ke taatash mojebe ghorbat ast o be shokr andarash mazide nemat. ___har nafasi ke foro miravad momedde hayatast o chon bar miayad mofarrahe zat. ___pas dar har nafasi 2 nemat mojod ast o bar har nematy shokry vajeb. ___az dast o zabane ke barayad ? ___kaz ohdeye shokrash be dar ayad ! ___man be to dami gharar natvanam kard ___va ehsane to ra shomar natvanam kard #################### ___gar bar tane man zaban shavad har moyee ___yek shokre to az hezar natvanam kard */ #include <bits/stdc++.h> #define pb push_back #define SALI main #define left(x) ((x)*2) #define right(x) (((x)*2) + 1) #define fuck(x) cout << #x << " : " << x << endl; #define findLowVec(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin()) #define findUpVec(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin()) #define findLowArr(a, n, x) (lower_bound(a, a + n, x) - a) #define findUpArr(a, n, x) (upper_bound(a, a + n, x) - a) #define int long long using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef long double ld; template <class A, class B> ostream &operator<<(ostream &out, const pair<A, B> &p) { return out << "(" << p.first << ", " << p.second << ")"; } template <class A> ostream &operator<<(ostream &out, const vector<A> &v) { out << "["; for (int i = 0; i < v.size(); i++) { if (i) out << ", "; out << v[i]; } return out << "]"; } template <class A, class B> ostream &operator<<(ostream &out, const set<A, B> &s) { out << "["; for (auto i = s.begin(); i != s.end(); i++) { out << ", "; out << *i; } return out << "]"; } template <class A> ostream &operator<<(ostream &out, const set<A> &s) { out << "["; for (auto i = s.begin(); i != s.end(); i++) { out << ", "; out << *i; } return out << "]"; } const int N = 1e4 + 10; int dp[N]; struct stone { int s; int w; int v; }; bool cmp(stone a, stone b) { return a.s + a.w < b.s + b.w; } int32_t SALI() { int n; cin >> n; stone a[n]; for (int i = 0; i < n; i++) cin >> a[i].w >> a[i].s >> a[i].v; memset(dp, -1, sizeof(dp)); dp[0] = 0; sort(a, a + n, cmp); for (int i = 0; i < n; i++) { for (int j = N - 1; j >= 0; j--) { if (dp[j] < 0) continue; if (j <= a[i].s) { dp[j + a[i].w] = max(dp[j + a[i].w], dp[j] + a[i].v); } } } int ans = 0; for (int i = 0; i < N; i++) ans = max(ans, dp[i]); cout << ans; } /**< WRITEN BY ALI ADELKHAH */
/* ___mennat khoday ra azz o va jall ke taatash mojebe ghorbat ast o be shokr andarash mazide nemat. ___har nafasi ke foro miravad momedde hayatast o chon bar miayad mofarrahe zat. ___pas dar har nafasi 2 nemat mojod ast o bar har nematy shokry vajeb. ___az dast o zabane ke barayad ? ___kaz ohdeye shokrash be dar ayad ! ___man be to dami gharar natvanam kard ___va ehsane to ra shomar natvanam kard #################### ___gar bar tane man zaban shavad har moyee ___yek shokre to az hezar natvanam kard */ #include <bits/stdc++.h> #define pb push_back #define SALI main #define left(x) ((x)*2) #define right(x) (((x)*2) + 1) #define fuck(x) cout << #x << " : " << x << endl; #define findLowVec(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin()) #define findUpVec(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin()) #define findLowArr(a, n, x) (lower_bound(a, a + n, x) - a) #define findUpArr(a, n, x) (upper_bound(a, a + n, x) - a) #define int long long using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef long double ld; template <class A, class B> ostream &operator<<(ostream &out, const pair<A, B> &p) { return out << "(" << p.first << ", " << p.second << ")"; } template <class A> ostream &operator<<(ostream &out, const vector<A> &v) { out << "["; for (int i = 0; i < v.size(); i++) { if (i) out << ", "; out << v[i]; } return out << "]"; } template <class A, class B> ostream &operator<<(ostream &out, const set<A, B> &s) { out << "["; for (auto i = s.begin(); i != s.end(); i++) { out << ", "; out << *i; } return out << "]"; } template <class A> ostream &operator<<(ostream &out, const set<A> &s) { out << "["; for (auto i = s.begin(); i != s.end(); i++) { out << ", "; out << *i; } return out << "]"; } const int N = 2e4 + 10; int dp[N]; struct stone { int s; int w; int v; }; bool cmp(stone a, stone b) { return a.s + a.w < b.s + b.w; } int32_t SALI() { int n; cin >> n; stone a[n]; for (int i = 0; i < n; i++) cin >> a[i].w >> a[i].s >> a[i].v; memset(dp, -1, sizeof(dp)); dp[0] = 0; sort(a, a + n, cmp); for (int i = 0; i < n; i++) { for (int j = N - 1; j >= 0; j--) { if (dp[j] < 0) continue; if (j <= a[i].s) { dp[j + a[i].w] = max(dp[j + a[i].w], dp[j] + a[i].v); } } } int ans = 0; for (int i = 0; i < N; i++) ans = max(ans, dp[i]); cout << ans; } /**< WRITEN BY ALI ADELKHAH */
replace
71
72
71
72
0
p03183
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; struct trio { int wt, sz, v; bool operator<(const trio &t) const { return sz + wt < t.sz + t.wt; } }; int N; trio arr[1005]; long long dp[10005]; int main() { cin >> N; for (int i = 1; i <= N; i++) { cin >> arr[i].wt >> arr[i].sz >> arr[i].v; } sort(arr + 1, arr + N + 1); long long ans = 0; for (int i = 1; i <= N; i++) { for (int j = arr[i].sz; j >= 0; j--) { dp[j + arr[i].wt] = max(dp[j + arr[i].wt], dp[j] + arr[i].v); ans = max(dp[j + arr[i].wt], ans); } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; struct trio { int wt, sz, v; bool operator<(const trio &t) const { return sz + wt < t.sz + t.wt; } }; int N; trio arr[1005]; long long dp[30005]; int main() { cin >> N; for (int i = 1; i <= N; i++) { cin >> arr[i].wt >> arr[i].sz >> arr[i].v; } sort(arr + 1, arr + N + 1); long long ans = 0; for (int i = 1; i <= N; i++) { for (int j = arr[i].sz; j >= 0; j--) { dp[j + arr[i].wt] = max(dp[j + arr[i].wt], dp[j] + arr[i].v); ans = max(dp[j + arr[i].wt], ans); } } cout << ans << endl; }
replace
11
12
11
12
0
p03183
C++
Runtime Error
#include <bits/stdc++.h> typedef long long ll; #define ff first #define ss second #define pb push_back #define mp make_pair using namespace std; const int N = 1e4 + 69; const ll INF = 1e18; int n; ll answer; ll best[N]; bool fix[N]; vector<pair<int, int>> P; struct bla { int w, s, v; }; bla A[N]; bool cmp(bla A, bla B) { int cnt_A = A.s - B.w; int cnt_B = B.s - A.w; if (cnt_A == cnt_B) { return A.s < B.s; } return cnt_A < cnt_B; } int main() { ios::sync_with_stdio(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> A[i].w >> A[i].s >> A[i].v; } sort(A + 1, A + 1 + n, cmp); fix[0] = 1; cout << endl; for (int i = 1; i <= n; i++) { ll answer_i = 0; for (int j = 0; j <= A[i].s; j++) { answer_i = max(answer_i, best[j]); } answer_i += A[i].v; answer = max(answer, answer_i); for (int j = A[i].s; j >= 0; j--) { if (fix[j]) { int to = j + A[i].w; best[to] = max(best[to], best[j] + A[i].v); fix[to] = 1; } } } cout << answer << endl; }
#include <bits/stdc++.h> typedef long long ll; #define ff first #define ss second #define pb push_back #define mp make_pair using namespace std; const int N = 1e4 + 69; const ll INF = 1e18; int n; ll answer; ll best[N]; bool fix[N]; vector<pair<int, int>> P; struct bla { int w, s, v; }; bla A[N]; bool cmp(bla A, bla B) { int cnt_A = A.s - B.w; int cnt_B = B.s - A.w; if (cnt_A == cnt_B) { return A.s < B.s; } return cnt_A < cnt_B; } int main() { ios::sync_with_stdio(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> A[i].w >> A[i].s >> A[i].v; } sort(A + 1, A + 1 + n, cmp); fix[0] = 1; cout << endl; for (int i = 1; i <= n; i++) { ll answer_i = 0; for (int j = 0; j <= A[i].s; j++) { answer_i = max(answer_i, best[j]); } answer_i += A[i].v; answer = max(answer, answer_i); for (int j = A[i].s; j >= 0; j--) { if (fix[j]) { int to = j + A[i].w; if (to > 1e4) continue; best[to] = max(best[to], best[j] + A[i].v); fix[to] = 1; } } } cout << answer << endl; }
insert
61
61
61
63
0
p03183
C++
Runtime Error
// khodaya khodet komak kon #include <bits/stdc++.h> #define F first #define S second #define pb push_back #define all(x) x.begin(), x.end() #pragma GCC optimise("ofast") #pragma GCC optimise("unroll-loops") using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; const int N = 100 + 10, W = 20010; const ll MOD = 1000000000 + 7; const ll INF = 1000000000000000000; const ll LOG = 25; int n, a[N], w[N], s[N], v[N], ind[N]; ll dp[N][W], ps[N][W]; bool cmp(int x, int y) { return w[x] + s[x] < s[y] + w[y]; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> w[i] >> s[i] >> v[i]; ind[i] = i; } sort(ind + 1, ind + n + 1, cmp); for (int i = 1; i <= n; i++) { // cout << ind[i] << '\n'; for (int j = 0; j < w[ind[i]]; j++) dp[i][j] = dp[i - 1][j]; for (int j = w[ind[i]]; j <= min(W - 1, s[ind[i]] + w[ind[i]]); j++) { // cout << i << ' ' << j << ' ' << v[ind[i]] << '\n'; dp[i][j] = max(dp[i - 1][j], ps[i - 1][j - w[ind[i]]] + v[ind[i]]); } for (int j = min(W - 1, s[ind[i]] + w[ind[i]]) + 1; j < W; j++) dp[i][j] = dp[i - 1][j]; ps[i][0] = dp[i][0]; for (int j = 1; j < W; j++) ps[i][j] = max(dp[i][j], ps[i][j - 1]); } // cout << ps[1][2] << '\n'; // cout << dp[2][4] << '\n'; cout << ps[n][W - 1]; return 0; }
// khodaya khodet komak kon #include <bits/stdc++.h> #define F first #define S second #define pb push_back #define all(x) x.begin(), x.end() #pragma GCC optimise("ofast") #pragma GCC optimise("unroll-loops") using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; const int N = 1000 + 10, W = 20010; const ll MOD = 1000000000 + 7; const ll INF = 1000000000000000000; const ll LOG = 25; int n, a[N], w[N], s[N], v[N], ind[N]; ll dp[N][W], ps[N][W]; bool cmp(int x, int y) { return w[x] + s[x] < s[y] + w[y]; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> w[i] >> s[i] >> v[i]; ind[i] = i; } sort(ind + 1, ind + n + 1, cmp); for (int i = 1; i <= n; i++) { // cout << ind[i] << '\n'; for (int j = 0; j < w[ind[i]]; j++) dp[i][j] = dp[i - 1][j]; for (int j = w[ind[i]]; j <= min(W - 1, s[ind[i]] + w[ind[i]]); j++) { // cout << i << ' ' << j << ' ' << v[ind[i]] << '\n'; dp[i][j] = max(dp[i - 1][j], ps[i - 1][j - w[ind[i]]] + v[ind[i]]); } for (int j = min(W - 1, s[ind[i]] + w[ind[i]]) + 1; j < W; j++) dp[i][j] = dp[i - 1][j]; ps[i][0] = dp[i][0]; for (int j = 1; j < W; j++) ps[i][j] = max(dp[i][j], ps[i][j - 1]); } // cout << ps[1][2] << '\n'; // cout << dp[2][4] << '\n'; cout << ps[n][W - 1]; return 0; }
replace
16
17
16
17
0
p03183
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < (n); ++i) #define rep(i, a, b) for (int i = a; i < (b); ++i) #define p(s) std::cout << s; #define pl(s) std::cout << s << endl; #define printIf(j, s1, s2) cout << (j ? s1 : s2) << endl; #define YES(j) cout << (j ? "YES" : "NO") << endl; #define Yes(j) std::cout << (j ? "Yes" : "No") << endl; #define yes(j) std::cout << (j ? "yes" : "no") << endl; #define all(v) v.begin(), v.end() #define showVector(v) \ REP(i, v.size()) { \ p(v[i]); \ p(" ") \ } \ pl("") template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } typedef long long int ll; typedef pair<ll, ll> P_ii; typedef pair<double, double> P_dd; const ll longinf = 1LL << 60; struct LazySegmentTree { private: int n; vector<ll> node, lazy; public: LazySegmentTree(int sz, ll init = 0) { n = 1; while (n < sz) n *= 2; node.resize(2 * n - 1, init); lazy.resize(2 * n - 1, 0); } void eval(int k, int l, int r) { if (lazy[k] != 0) node[k] += lazy[k]; if (r - l > 1) { lazy[2 * k + 1] += lazy[k]; lazy[2 * k + 2] += lazy[k]; } lazy[k] = 0; } //[a,b)にxを加算 void add(int a, int b, ll x, int k = 0, int l = 0, int r = -1) { if (r < 0) r = n; eval(k, l, r); if (r <= a || b <= l) return; if (a <= l && r <= b) { lazy[k] += x; eval(k, l, r); } else { add(a, b, x, 2 * k + 1, l, (l + r) / 2); add(a, b, x, 2 * k + 2, (l + r) / 2, r); node[k] = max(node[2 * k + 1], node[2 * k + 2]); } } //[a,b)でのmaxを返す ll get(int a, int b, int k = 0, int l = 0, int r = -1) { if (r < 0) r = n; eval(k, l, r); if (r <= a || b <= l) return 0; if (a <= l && r <= b) return node[k]; ll xl = get(a, b, 2 * k + 1, l, (l + r) / 2); ll xr = get(a, b, 2 * k + 2, (l + r) / 2, r); return max(xl, xr); } }; int main() { int N; cin >> N; vector<ll> w(N), s(N), v(N); REP(i, N) cin >> w[i] >> s[i] >> v[i]; vector<int> ord(N); iota(all(ord), 0); sort(all(ord), [&](int x, int y) { return min(s[x], s[y] - w[x]) > min(s[y], s[x] - w[y]); }); vector<ll> dp(20202, -1); dp[0] = 0; for (int i : ord) { for (int j = 20200; j >= 0; j--) { if (dp[j] == -1) continue; chmax(dp[j + w[i]], dp[j] + v[i]); } } cout << *max_element(all(dp)) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < (n); ++i) #define rep(i, a, b) for (int i = a; i < (b); ++i) #define p(s) std::cout << s; #define pl(s) std::cout << s << endl; #define printIf(j, s1, s2) cout << (j ? s1 : s2) << endl; #define YES(j) cout << (j ? "YES" : "NO") << endl; #define Yes(j) std::cout << (j ? "Yes" : "No") << endl; #define yes(j) std::cout << (j ? "yes" : "no") << endl; #define all(v) v.begin(), v.end() #define showVector(v) \ REP(i, v.size()) { \ p(v[i]); \ p(" ") \ } \ pl("") template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } typedef long long int ll; typedef pair<ll, ll> P_ii; typedef pair<double, double> P_dd; const ll longinf = 1LL << 60; struct LazySegmentTree { private: int n; vector<ll> node, lazy; public: LazySegmentTree(int sz, ll init = 0) { n = 1; while (n < sz) n *= 2; node.resize(2 * n - 1, init); lazy.resize(2 * n - 1, 0); } void eval(int k, int l, int r) { if (lazy[k] != 0) node[k] += lazy[k]; if (r - l > 1) { lazy[2 * k + 1] += lazy[k]; lazy[2 * k + 2] += lazy[k]; } lazy[k] = 0; } //[a,b)にxを加算 void add(int a, int b, ll x, int k = 0, int l = 0, int r = -1) { if (r < 0) r = n; eval(k, l, r); if (r <= a || b <= l) return; if (a <= l && r <= b) { lazy[k] += x; eval(k, l, r); } else { add(a, b, x, 2 * k + 1, l, (l + r) / 2); add(a, b, x, 2 * k + 2, (l + r) / 2, r); node[k] = max(node[2 * k + 1], node[2 * k + 2]); } } //[a,b)でのmaxを返す ll get(int a, int b, int k = 0, int l = 0, int r = -1) { if (r < 0) r = n; eval(k, l, r); if (r <= a || b <= l) return 0; if (a <= l && r <= b) return node[k]; ll xl = get(a, b, 2 * k + 1, l, (l + r) / 2); ll xr = get(a, b, 2 * k + 2, (l + r) / 2, r); return max(xl, xr); } }; int main() { int N; cin >> N; vector<ll> w(N), s(N), v(N); REP(i, N) cin >> w[i] >> s[i] >> v[i]; vector<int> ord(N); iota(all(ord), 0); sort(all(ord), [&](int x, int y) { return min(s[x], s[y] - w[x]) > min(s[y], s[x] - w[y]); }); vector<ll> dp(20202, -1); dp[0] = 0; for (int i : ord) { for (int j = 20200; j >= 0; j--) { if (dp[j] == -1) continue; if (j <= s[i]) chmax(dp[j + w[i]], dp[j] + v[i]); } } cout << *max_element(all(dp)) << endl; return 0; }
replace
112
113
112
114
0
p03183
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define lli long long int using namespace std; #define mod 1000000007 #define MOD 1000000037 #define mod1 998244353 #define fastio \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define INF 1e18 int main() { fastio; int n; cin >> n; vector<pair<pair<int, int>, int>> v(n); for (int i = 0; i < n; i++) cin >> v[i].first.first >> v[i].first.second >> v[i].second; vector<pair<int, int>> sv(n); for (int i = 0; i < n; i++) { sv[i].first = v[i].first.first + v[i].first.second; sv[i].second = i; } sort(sv.begin(), sv.end()); int l = 1e5 - 1; vector<vector<lli>> dp(n + 1, vector<lli>(1e5, 0)); // lli dp[n+1][l+1]; for (int i = 0; i <= l; i++) { for (int j = 0; j <= n; j++) { if (i == 0 or j == 0) { dp[j][i] = 0; continue; } int ind = sv[j - 1].second; int w = v[ind].first.first, st = v[ind].first.second; dp[j][i] = max(dp[j - 1][i], dp[j][i - 1]); lli a = 0; if (w <= i and i - w <= st) a = v[ind].second * 1LL + dp[j - 1][i - w]; dp[j][i] = max(dp[j][i], a); } } // for(int i=0;i<=l;i++) // { // for(int j=0;j<=n;j++) // cout << dp[j][i] << " "; // cout << endl; // } lli ans = 0; for (int i = 1; i <= n; i++) ans = max(ans, dp[i][l]); cout << ans << endl; }
#include <bits/stdc++.h> #define lli long long int using namespace std; #define mod 1000000007 #define MOD 1000000037 #define mod1 998244353 #define fastio \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define INF 1e18 int main() { fastio; int n; cin >> n; vector<pair<pair<int, int>, int>> v(n); for (int i = 0; i < n; i++) cin >> v[i].first.first >> v[i].first.second >> v[i].second; vector<pair<int, int>> sv(n); for (int i = 0; i < n; i++) { sv[i].first = v[i].first.first + v[i].first.second; sv[i].second = i; } sort(sv.begin(), sv.end()); int l = 2e4; vector<vector<lli>> dp(n + 1, vector<lli>(2e4 + 1, 0)); // lli dp[n+1][l+1]; for (int i = 0; i <= l; i++) { for (int j = 0; j <= n; j++) { if (i == 0 or j == 0) { dp[j][i] = 0; continue; } int ind = sv[j - 1].second; int w = v[ind].first.first, st = v[ind].first.second; dp[j][i] = max(dp[j - 1][i], dp[j][i - 1]); lli a = 0; if (w <= i and i - w <= st) a = v[ind].second * 1LL + dp[j - 1][i - w]; dp[j][i] = max(dp[j][i], a); } } // for(int i=0;i<=l;i++) // { // for(int j=0;j<=n;j++) // cout << dp[j][i] << " "; // cout << endl; // } lli ans = 0; for (int i = 1; i <= n; i++) ans = max(ans, dp[i][l]); cout << ans << endl; }
replace
25
27
25
27
TLE
p03183
C++
Runtime Error
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define int long long #define endl '\n' #define pb push_back #define fi first #define se second #define all(c) (c).begin(), (c).end() typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef vector<int> vi; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define TRACE #ifndef ONLINE_JUDGE #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cerr << name << " : " << arg1 << endl; } template <typename Arg1, typename... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } #else #define trace(...) #endif const ll inf = 2e18; const int mod = 1e9 + 7; const int N = 2e4 + 10; int dp[N]; bool cmp(const pair<pii, int> &a, const pair<pii, int> &b) { return a.fi.fi + a.fi.se <= b.fi.fi + b.fi.se; } signed main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n; cin >> n; pair<pii, int> a[n]; for (int i = 0; i < n; i++) { cin >> a[i].fi.fi >> a[i].fi.se >> a[i].se; } sort(a, a + n, cmp); memset(dp, -1, sizeof(dp)); dp[0] = 0; int ans = 0; for (int i = 0; i < n; i++) { for (int j = a[i].fi.se; j >= 0; j--) { if (dp[j] != -1) { dp[j + a[i].fi.fi] = max(dp[j + a[i].fi.fi], dp[j] + a[i].se); ans = max(ans, dp[j + a[i].fi.fi]); } } } cout << ans; return 0; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; #define int long long #define endl '\n' #define pb push_back #define fi first #define se second #define all(c) (c).begin(), (c).end() typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef vector<int> vi; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define TRACE #ifndef ONLINE_JUDGE #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char *name, Arg1 &&arg1) { cerr << name << " : " << arg1 << endl; } template <typename Arg1, typename... Args> void __f(const char *names, Arg1 &&arg1, Args &&...args) { const char *comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } #else #define trace(...) #endif const ll inf = 2e18; const int mod = 1e9 + 7; const int N = 2e4 + 10; int dp[N]; bool cmp(const pair<pii, int> &a, const pair<pii, int> &b) { return a.fi.fi + a.fi.se < b.fi.fi + b.fi.se; } signed main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n; cin >> n; pair<pii, int> a[n]; for (int i = 0; i < n; i++) { cin >> a[i].fi.fi >> a[i].fi.se >> a[i].se; } sort(a, a + n, cmp); memset(dp, -1, sizeof(dp)); dp[0] = 0; int ans = 0; for (int i = 0; i < n; i++) { for (int j = a[i].fi.se; j >= 0; j--) { if (dp[j] != -1) { dp[j + a[i].fi.fi] = max(dp[j + a[i].fi.fi], dp[j] + a[i].se); ans = max(ans, dp[j + a[i].fi.fi]); } } } cout << ans; return 0; }
replace
43
44
43
44
0
p03183
C++
Runtime Error
#include <bits/stdc++.h> #include <iostream> using namespace std; // State // dp[i][j]: The maximum value we can get by using blocks from // 0...i and sum of blocks in the tower = j // Initialization // dp[0][W[0]] = V[0] // Transitions // dp[i][j] = max(V[j]+dp[i-W[0]][j-1], dp[i][j-1]) // Order of evaluation // Rowise or Columnwise // Answer: argmax(dp[0...W_max][N]) struct B { long long w; long long s; long long v; }; vector<vector<long long>> dp(10004, vector<long long>(1004, 0)); bool compare(B b1, B b2) { return (b1.w + b1.s <= b2.w + b2.s); } int main() { long long N; cin >> N; vector<B> boxes(N); for (long long i = 0; i < N; i++) { long long w, s, v; cin >> w >> s >> v; boxes[i].w = w; boxes[i].s = s; boxes[i].v = v; } sort(boxes.begin(), boxes.end(), compare); long long W_max = 20004; dp[boxes[0].w][0] = boxes[0].v; for (int i = 0; i < W_max; i++) { if (i >= boxes[0].w) { dp[i][0] = boxes[0].v; } } for (long long j = 1; j < N; j++) { for (long long i = 0; i < W_max; i++) { if (i - boxes[j].w >= 0 && boxes[j].s + boxes[j].w - i >= 0) { dp[i][j] = max(dp[i - boxes[j].w][j - 1] + boxes[j].v, dp[i][j - 1]); } else { dp[i][j] = dp[i][j - 1]; } } } long long result = 0; for (long long i = 0; i < W_max; i++) { if (result < dp[i][N - 1]) result = dp[i][N - 1]; } cout << result << endl; }
#include <bits/stdc++.h> #include <iostream> using namespace std; // State // dp[i][j]: The maximum value we can get by using blocks from // 0...i and sum of blocks in the tower = j // Initialization // dp[0][W[0]] = V[0] // Transitions // dp[i][j] = max(V[j]+dp[i-W[0]][j-1], dp[i][j-1]) // Order of evaluation // Rowise or Columnwise // Answer: argmax(dp[0...W_max][N]) struct B { long long w; long long s; long long v; }; vector<vector<long long>> dp(20004, vector<long long>(1004, 0)); bool compare(B b1, B b2) { return (b1.w + b1.s <= b2.w + b2.s); } int main() { long long N; cin >> N; vector<B> boxes(N); for (long long i = 0; i < N; i++) { long long w, s, v; cin >> w >> s >> v; boxes[i].w = w; boxes[i].s = s; boxes[i].v = v; } sort(boxes.begin(), boxes.end(), compare); long long W_max = 20004; dp[boxes[0].w][0] = boxes[0].v; for (int i = 0; i < W_max; i++) { if (i >= boxes[0].w) { dp[i][0] = boxes[0].v; } } for (long long j = 1; j < N; j++) { for (long long i = 0; i < W_max; i++) { if (i - boxes[j].w >= 0 && boxes[j].s + boxes[j].w - i >= 0) { dp[i][j] = max(dp[i - boxes[j].w][j - 1] + boxes[j].v, dp[i][j - 1]); } else { dp[i][j] = dp[i][j - 1]; } } } long long result = 0; for (long long i = 0; i < W_max; i++) { if (result < dp[i][N - 1]) result = dp[i][N - 1]; } cout << result << endl; }
replace
25
26
25
26
-11
p03183
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define mod 1000000007 #define pb push_back #define mp make_pair #define fi first #define sec second typedef long long ll; typedef long double ld; #define pii pair<ll, ll> #define pic pair<ll, char> #define vi vector<ll> #define vvi vector<vi> #define vpi vector<pii> #define vvpi vector<vpi> #define st set<ll, greater<ll>> #define mst multiset<ll, greater<ll>> #define fast \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0) #define mapp unordered_map<ll, ll> #define ppii pair<pii, pii> #define test(t) \ ll t; \ cin >> t; \ while (t--) #define inarr(n) \ ll arr[n]; \ for (ll i = 0; i < n; i++) \ cin >> arr[i]; #define ingra(m) \ vvi arr(100005); \ for (ll i = 0; i < m; i++) { \ ll u, v; \ cin >> u >> v; \ arr[u].pb(v); \ arr[v].pb(u); \ } #define mem(a, b) memset(a, b, sizeof(a)) #define inn freopen("input.txt", "r", stdin) #define outt freopen("output.txt", "w", stdout) #define all(arr) arr.begin(), arr.end() ll power(ll x, ll y) { ll res = 1; while (y) { if (y & 1) res = (res * x) % mod; y = y / 2, x = (x * x) % mod; } return res % mod; } pair<ll, pair<ll, ll>> arr[1000]; ll dp[1000][10001]; ll n; bool cmp(pair<ll, pair<ll, ll>> a, pair<ll, pair<ll, ll>> b) { if (a.fi + a.sec.fi < b.fi + b.sec.fi) return true; return false; } ll func(ll index, ll done) { if (index == n) return 0; ll &ret = dp[index][done]; if (ret != -1) return ret; ret = 0; if (done <= arr[index].fi) ret = arr[index].sec.sec + func(index + 1, done + arr[index].sec.fi); ret = max(ret, func(index + 1, done)); return ret; } signed main() { fast; mem(dp, -1); cin >> n; ll i; for (i = 0; i < n; i++) cin >> arr[i].sec.fi >> arr[i].fi >> arr[i].sec.sec; sort(arr, arr + n, cmp); cout << func(0, 0); }
#include <bits/stdc++.h> using namespace std; #define mod 1000000007 #define pb push_back #define mp make_pair #define fi first #define sec second typedef long long ll; typedef long double ld; #define pii pair<ll, ll> #define pic pair<ll, char> #define vi vector<ll> #define vvi vector<vi> #define vpi vector<pii> #define vvpi vector<vpi> #define st set<ll, greater<ll>> #define mst multiset<ll, greater<ll>> #define fast \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0) #define mapp unordered_map<ll, ll> #define ppii pair<pii, pii> #define test(t) \ ll t; \ cin >> t; \ while (t--) #define inarr(n) \ ll arr[n]; \ for (ll i = 0; i < n; i++) \ cin >> arr[i]; #define ingra(m) \ vvi arr(100005); \ for (ll i = 0; i < m; i++) { \ ll u, v; \ cin >> u >> v; \ arr[u].pb(v); \ arr[v].pb(u); \ } #define mem(a, b) memset(a, b, sizeof(a)) #define inn freopen("input.txt", "r", stdin) #define outt freopen("output.txt", "w", stdout) #define all(arr) arr.begin(), arr.end() ll power(ll x, ll y) { ll res = 1; while (y) { if (y & 1) res = (res * x) % mod; y = y / 2, x = (x * x) % mod; } return res % mod; } pair<ll, pair<ll, ll>> arr[1000]; ll dp[1000][10001]; ll n; bool cmp(pair<ll, pair<ll, ll>> a, pair<ll, pair<ll, ll>> b) { if (a.fi + a.sec.fi < b.fi + b.sec.fi) return true; return false; } ll func(ll index, ll done) { if (done > 1e4) return 0; if (index == n) return 0; ll &ret = dp[index][done]; if (ret != -1) return ret; ret = 0; if (done <= arr[index].fi) ret = arr[index].sec.sec + func(index + 1, done + arr[index].sec.fi); ret = max(ret, func(index + 1, done)); return ret; } signed main() { fast; mem(dp, -1); cin >> n; ll i; for (i = 0; i < n; i++) cin >> arr[i].sec.fi >> arr[i].fi >> arr[i].sec.sec; sort(arr, arr + n, cmp); cout << func(0, 0); }
insert
61
61
61
63
0
p03183
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define M 1000000007 #define F first #define S second #define mp make_pair #define pb push_back #define gcd __gcd #define in(a) scanf("%d", &a) #define in2(a, b) scanf("%d%d", &a, &b) #define in3(a, b, c) scanf("%d%d%d", &a, &b, &c) #define read(v, i, n) \ for (i = 0; i < n; i++) \ in(v[i]) #define twod(mat, i, j, n, m) \ rep(i, n) { rep(j, m) in(mat[i][j]); } #define sc(ch) scanf("%c", &ch) #define sstr(str) scanf("%s", str) #define pr(n) printf("%d ", n) #define out(n) printf("%d\n", n) #define inl(a) cin >> a #define prl(a) cout << a << " " #define outl(a) cout << a << endl #define yes printf("YES\n") #define no printf("NO\n") #define lin printf("\n") #define dbg(v, i, n) \ for (i = 0; i < n; i++) \ pr(v[i]); \ lin #define ck printf("continue\n") #define all(vec) vec.begin(), vec.end() #define asc(vec) sort(vec.begin(), vec.end()) #define lower(v, k) lower_bound(v.begin(), v.end(), k) - v.begin() #define upper(v, k) upper_bound(v.begin(), v.end(), k) - v.begin() #define tf(mytuple) get<0>(mytuple) #define ts(mytuple) get<1>(mytuple) #define tt(mytuple) get<2>(mytuple) #define tddd tuple<double, double, double> #define ii pair<int, int> #define vi vector<int> #define vii vector<pair<int, int>> #define vvi vector<vector<int>> #define viii vector<pair<pair<int, int>, int>> #define vvii vector<vector<pair<int, int>>> #define lp(i, a, b) for (i = a; i < b; i++) #define rep(i, n) for (i = 0; i < n; i++) #define N 10005 typedef long long int ll; ll dp[N]; bool cmp(pair<ii, int> A, pair<ii, int> B) { if (min(A.F.S, B.F.S - A.F.F) != min(B.F.S, A.F.S - B.F.F)) return min(A.F.S, B.F.S - A.F.F) > min(B.F.S, A.F.S - B.F.F); return true; } int main() { int i, j, n, w, c, v; int C = 0; in(n); viii ad(n); rep(i, n) { in3(w, c, v); C = max(C, c); ad[i] = {{w, c}, v}; } sort(all(ad), cmp); ll x, ans = 0; rep(i, n) { w = ad[i].F.F, c = ad[i].F.S, v = ad[i].S; x = 0; rep(j, c + 1) x = max(x, dp[j]); ans = max(ans, x + v); for (j = min(C, c + w); j >= w; j--) dp[j] = max(dp[j], v + dp[j - w]); } outl(ans); }
#include <bits/stdc++.h> using namespace std; #define M 1000000007 #define F first #define S second #define mp make_pair #define pb push_back #define gcd __gcd #define in(a) scanf("%d", &a) #define in2(a, b) scanf("%d%d", &a, &b) #define in3(a, b, c) scanf("%d%d%d", &a, &b, &c) #define read(v, i, n) \ for (i = 0; i < n; i++) \ in(v[i]) #define twod(mat, i, j, n, m) \ rep(i, n) { rep(j, m) in(mat[i][j]); } #define sc(ch) scanf("%c", &ch) #define sstr(str) scanf("%s", str) #define pr(n) printf("%d ", n) #define out(n) printf("%d\n", n) #define inl(a) cin >> a #define prl(a) cout << a << " " #define outl(a) cout << a << endl #define yes printf("YES\n") #define no printf("NO\n") #define lin printf("\n") #define dbg(v, i, n) \ for (i = 0; i < n; i++) \ pr(v[i]); \ lin #define ck printf("continue\n") #define all(vec) vec.begin(), vec.end() #define asc(vec) sort(vec.begin(), vec.end()) #define lower(v, k) lower_bound(v.begin(), v.end(), k) - v.begin() #define upper(v, k) upper_bound(v.begin(), v.end(), k) - v.begin() #define tf(mytuple) get<0>(mytuple) #define ts(mytuple) get<1>(mytuple) #define tt(mytuple) get<2>(mytuple) #define tddd tuple<double, double, double> #define ii pair<int, int> #define vi vector<int> #define vii vector<pair<int, int>> #define vvi vector<vector<int>> #define viii vector<pair<pair<int, int>, int>> #define vvii vector<vector<pair<int, int>>> #define lp(i, a, b) for (i = a; i < b; i++) #define rep(i, n) for (i = 0; i < n; i++) #define N 10005 typedef long long int ll; ll dp[N]; bool cmp(pair<ii, int> A, pair<ii, int> B) { if (min(A.F.S, B.F.S - A.F.F) != min(B.F.S, A.F.S - B.F.F)) return (min(A.F.S, B.F.S - A.F.F) > min(B.F.S, A.F.S - B.F.F)); return false; } int main() { int i, j, n, w, c, v; int C = 0; in(n); viii ad(n); rep(i, n) { in3(w, c, v); C = max(C, c); ad[i] = {{w, c}, v}; } sort(all(ad), cmp); ll x, ans = 0; rep(i, n) { w = ad[i].F.F, c = ad[i].F.S, v = ad[i].S; x = 0; rep(j, c + 1) x = max(x, dp[j]); ans = max(ans, x + v); for (j = min(C, c + w); j >= w; j--) dp[j] = max(dp[j], v + dp[j - w]); } outl(ans); }
replace
52
54
52
54
0
p03183
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define i_7 (ll)(1E9 + 7) #define i_5 (ll)(1E9 + 5) ll mod(ll a) { ll c = a % i_7; if (c >= 0) return c; else return c + i_7; } typedef pair<int, int> i_i; typedef pair<ll, ll> l_l; ll inf = (ll)1E12; #define rep(i, l, r) for (ll i = l; i <= r; i++) #define pb push_back ll max(ll a, ll b) { if (a < b) return b; else return a; } ll min(ll a, ll b) { if (a > b) return b; else return a; } void Max(ll *pos, ll val) { *pos = max(*pos, val); } // Max(&dp[i][j],dp[i-1][j]); void Min(ll *pos, ll val) { *pos = min(*pos, val); } void Add(ll *pos, ll val) { *pos = mod(*pos + val); } const long double EPS = 1E-8; //////////////////////////////////////// struct tower { ll w, s, v; }; bool comp(tower t1, tower t2) { if (t1.w + t1.s < t2.w + t2.s) return true; else return false; } int main() { ll n; cin >> n; tower t[n]; rep(i, 0, n - 1) { cin >> t[i].w >> t[i].s >> t[i].v; } sort(t, t + n, comp); ll N = (ll)200001; ll dp[n + 1][N]; memset(dp, 0, sizeof(dp)); rep(i, 0, n - 1) { rep(j, 0, N - 1) { dp[i + 1][j] = dp[i][j]; if (0 <= j - t[i].w && j - t[i].w <= t[i].s) Max(&dp[i + 1][j], dp[i][j - t[i].w] + t[i].v); } } /*rep(i,0,n){ rep(j,0,20){ cout<<dp[i][j]<<" "; }cout<<endl; }*/ ll ans = 0; rep(i, 0, N - 1) Max(&ans, dp[n][i]); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define i_7 (ll)(1E9 + 7) #define i_5 (ll)(1E9 + 5) ll mod(ll a) { ll c = a % i_7; if (c >= 0) return c; else return c + i_7; } typedef pair<int, int> i_i; typedef pair<ll, ll> l_l; ll inf = (ll)1E12; #define rep(i, l, r) for (ll i = l; i <= r; i++) #define pb push_back ll max(ll a, ll b) { if (a < b) return b; else return a; } ll min(ll a, ll b) { if (a > b) return b; else return a; } void Max(ll *pos, ll val) { *pos = max(*pos, val); } // Max(&dp[i][j],dp[i-1][j]); void Min(ll *pos, ll val) { *pos = min(*pos, val); } void Add(ll *pos, ll val) { *pos = mod(*pos + val); } const long double EPS = 1E-8; //////////////////////////////////////// struct tower { ll w, s, v; }; bool comp(tower t1, tower t2) { if (t1.w + t1.s < t2.w + t2.s) return true; else return false; } int main() { ll n; cin >> n; tower t[n]; rep(i, 0, n - 1) { cin >> t[i].w >> t[i].s >> t[i].v; } sort(t, t + n, comp); ll N = (ll)2E4 + 1; ll dp[n + 1][N]; memset(dp, 0, sizeof(dp)); rep(i, 0, n - 1) { rep(j, 0, N - 1) { dp[i + 1][j] = dp[i][j]; if (0 <= j - t[i].w && j - t[i].w <= t[i].s) Max(&dp[i + 1][j], dp[i][j - t[i].w] + t[i].v); } } /*rep(i,0,n){ rep(j,0,20){ cout<<dp[i][j]<<" "; }cout<<endl; }*/ ll ans = 0; rep(i, 0, N - 1) Max(&ans, dp[n][i]); cout << ans << endl; return 0; }
replace
53
54
53
54
0
p03183
C++
Memory Limit Exceeded
#include <bits/stdc++.h> #define ll long long #define all(a) (a).begin(), (a).end() #pragma GCC optimize("O3") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #define sz() size() #define fr first #define sc second #define pb push_back #define er erase #define in insert #define pi pair<int, int> #define pii pair<pair<int, int>, int> #define mp make_pair #define int long long #define rc(s) return cout << s, 0 #define rcc(s) cout << s, exit(0) using namespace std; const int mod = 1e9 + 7; const int modx = 998244353; const int per = 666013; int n, dp[1005][200100]; struct block { int x, y, z; bool operator<(block &blok) { return x + y < blok.x + blok.y; } } bloc[1005]; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cerr.tie(0); cout.tie(0); srand(chrono::steady_clock::now().time_since_epoch().count()); cin >> n; for (int i = 1; i <= n; i++) { cin >> bloc[i].x >> bloc[i].y >> bloc[i].z; } sort(bloc + 1, bloc + n + 1); for (int i = 1; i <= n; i++) { for (int w = 0; w <= 20005; w++) { dp[i][w] = dp[i - 1][w]; if (w - bloc[i].x >= 0 && w - bloc[i].x <= bloc[i].y) dp[i][w] = max(dp[i][w], dp[i - 1][w - bloc[i].x] + bloc[i].z); } } int res = 0LL; for (int i = 0LL; i <= 20005LL; i++) res = max(res, dp[n][i]); cout << res; }
#include <bits/stdc++.h> #define ll long long #define all(a) (a).begin(), (a).end() #pragma GCC optimize("O3") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #define sz() size() #define fr first #define sc second #define pb push_back #define er erase #define in insert #define pi pair<int, int> #define pii pair<pair<int, int>, int> #define mp make_pair #define int long long #define rc(s) return cout << s, 0 #define rcc(s) cout << s, exit(0) using namespace std; const int mod = 1e9 + 7; const int modx = 998244353; const int per = 666013; int n, dp[1005][20100]; struct block { int x, y, z; bool operator<(block &blok) { return x + y < blok.x + blok.y; } } bloc[1005]; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cerr.tie(0); cout.tie(0); srand(chrono::steady_clock::now().time_since_epoch().count()); cin >> n; for (int i = 1; i <= n; i++) { cin >> bloc[i].x >> bloc[i].y >> bloc[i].z; } sort(bloc + 1, bloc + n + 1); for (int i = 1; i <= n; i++) { for (int w = 0; w <= 20005; w++) { dp[i][w] = dp[i - 1][w]; if (w - bloc[i].x >= 0 && w - bloc[i].x <= bloc[i].y) dp[i][w] = max(dp[i][w], dp[i - 1][w - bloc[i].x] + bloc[i].z); } } int res = 0LL; for (int i = 0LL; i <= 20005LL; i++) res = max(res, dp[n][i]); cout << res; }
replace
24
25
24
25
MLE
p03183
C++
Runtime Error
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> // #include<bits/extc++.h> using namespace std; // using namespace __gnu_pbds; // typedef // tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> // set_t; #define mp(a, b) make_pair((a), (b)) #define pii pair<int, int> #define pll pair<LL, LL> #define pdd pair<double, double> #define pb push_back #define x first #define y second #define sqr(x) ((x) * (x)) #define EPS 1e-6 #define MEM(x) memset(x, 0, sizeof(x)) #define MEMS(x) memset(x, -1, sizeof(x)) #define pi acos(-1) #define index Index #define Line pll typedef long long LL; int main() { int n; scanf("%d", &n); pair<int, pii> p[1005]; for (int i = 0; i < n; i++) { int a, b, c; scanf("%d %d %d", &a, &b, &c); p[i] = mp(a + b, mp(a, c)); } sort(p, p + n); LL dp[10005]; MEM(dp); LL ans = 0; for (int i = 0; i < n; i++) { for (int j = p[i].x - p[i].y.x; j >= 0; j--) { dp[j + p[i].y.x] = max(dp[j + p[i].y.x], dp[j] + p[i].y.y); ans = max(ans, dp[j + p[i].y.x]); } } printf("%lld\n", ans); }
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> // #include<bits/extc++.h> using namespace std; // using namespace __gnu_pbds; // typedef // tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> // set_t; #define mp(a, b) make_pair((a), (b)) #define pii pair<int, int> #define pll pair<LL, LL> #define pdd pair<double, double> #define pb push_back #define x first #define y second #define sqr(x) ((x) * (x)) #define EPS 1e-6 #define MEM(x) memset(x, 0, sizeof(x)) #define MEMS(x) memset(x, -1, sizeof(x)) #define pi acos(-1) #define index Index #define Line pll typedef long long LL; int main() { int n; scanf("%d", &n); pair<int, pii> p[1005]; for (int i = 0; i < n; i++) { int a, b, c; scanf("%d %d %d", &a, &b, &c); p[i] = mp(a + b, mp(a, c)); } sort(p, p + n); LL dp[20005]; MEM(dp); LL ans = 0; for (int i = 0; i < n; i++) { for (int j = p[i].x - p[i].y.x; j >= 0; j--) { dp[j + p[i].y.x] = max(dp[j + p[i].y.x], dp[j] + p[i].y.y); ans = max(ans, dp[j + p[i].y.x]); } } printf("%lld\n", ans); }
replace
33
34
33
34
0
p03183
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long int struct Block { ll s, w, v; bool operator<(const Block &b1) { ll s1 = s, w1 = w, s2 = b1.s, w2 = b1.w; return s1 + w1 <= s2 + w2; } }; ll tower(Block arr[], int n) { // Suppose if we have 3 blocks with (weight, solidness, value) as: // (1, 2, 1) // (1, 3, 1) // (1, 1, 1) // If we try to find the best subsequence having this order itself // then we will be able to get the maximum value as 2 only // Although the maximum value can be 3 // by taking all the three in the order: 3, 1, 2 // Hence to avoid checking of all the possible permutations, // we need to sort them in some order // Now we need to decide the comparator function for sorting // Suppose we have 2 blocks: 1 and 2 // and they should be such that 1 should be above 2 // Then a good (or strong) comparator function will ensure // that 1 comes before 2 in the array always // For 2 to be below 1, this condition must be satisfied: s2 >= w1 // Also notice that for 1 to be below 2: s1 >= w2 but we don't want this // So we also want to satisfy: s1 < w2 // So that both conditions are satisfied simultaneously: // 1) 2 is below 1 // 2) 1 is never below 2 // Both conditions must be satisfied to avoid any contradiction // Hence there are 2 conditions: s2 >= w1 and s1 < w2 // s2 >= w1 and w2 > s1 // On combining these 2 conditions: s2+w2 >= w1+s1 OR s1+w1 <= s2+w2 // This single condition will ensure that 2 comes below 1 OR // 2 comes after 1 in the array AND also there is no contradiction // I have already defined the comparator in the structure definition itself sort(arr, arr + n); // Now we apply Dynamic Programming: // dp[i][j] stores the maximum sum of value of some subsequence // considered till i-th element and having the sum of weights as 'j' // So whenever a new element comes, we check for which maximum // value in dp[i-1][], // the solidness of that new element is greater than or equal to // the value in dp[i-1][] (sum of some weights) and then update // the dp value corresponding to the new sum of weights if better // The first dimension of dp[][] will be the no. of elements // Now we can take the second dimension to be the sum of all weights // but notice that the maximum allowed sum of weights // will be limited by the solidness of the bottom most block or // the last element which we pick from the array, i.e. // the maximum possible sum of weights for any subsequence will // be at max (maxS + maxW) // where maxS is the max sum of weights of all the blocks except the // bottom most block and maxW is the max weight of the bottom most block // Hence we can take the second dimension of size (maxS + maxW) int maxS = 0, maxW = 0; for (int i = 0; i < n; i++) { if (arr[i].s > maxS) maxS = arr[i].s; if (arr[i].w > maxW) maxW = arr[i].w; } vector<vector<ll>> dp(n + 1, vector<ll>(maxS + maxW + 1, 0)); for (int i = 1; i <= n; i++) { int w = arr[i - 1].w, s = arr[i - 1].s, v = arr[i - 1].v; // We need to go to all the values of previous row // to also copy them to the current row for (int j = 0; j <= maxS + maxW; j++) { dp[i][j] = max(dp[i][j], dp[i - 1][j]); if (s >= j) dp[i][j + w] = max(dp[i - 1][j + w], dp[i - 1][j] + v); } } // The maximum value in dp[n][] is our answer ll maxSumOfValue = 0; for (int i = 0; i <= maxS + maxW; i++) if (dp[n][i] > maxSumOfValue) maxSumOfValue = dp[n][i]; return maxSumOfValue; } ll towerOptimized(Block arr[], int n) { // Instead of maintaining a 2D array, // we can maintain a rolling 1D array like in knapsack sort(arr, arr + n); int maxS = 0, maxW = 0; for (int i = 0; i < n; i++) { if (arr[i].s > maxS) maxS = arr[i].s; if (arr[i].w > maxW) maxW = arr[i].w; } vector<ll> dp(maxS + maxW + 1, 0); for (int i = 1; i <= n; i++) { int w = arr[i - 1].w, s = arr[i - 1].s, v = arr[i - 1].v; for (int j = s; j >= 0; j--) dp[j + w] = max(dp[j + w], dp[j] + v); } ll maxSumOfValue = 0; for (int i = 0; i <= maxS + maxW; i++) if (dp[i] > maxSumOfValue) maxSumOfValue = dp[i]; return maxSumOfValue; } int main() { int n; cin >> n; Block arr[n]; for (int i = 0; i < n; i++) cin >> arr[i].w >> arr[i].s >> arr[i].v; // ll ans = tower(arr, n); ll ans = towerOptimized(arr, n); cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long int struct Block { ll s, w, v; bool operator<(const Block &b2) { ll s1 = s, w1 = w, s2 = b2.s, w2 = b2.w; return s1 + w1 < s2 + w2; } }; ll tower(Block arr[], int n) { // Suppose if we have 3 blocks with (weight, solidness, value) as: // (1, 2, 1) // (1, 3, 1) // (1, 1, 1) // If we try to find the best subsequence having this order itself // then we will be able to get the maximum value as 2 only // Although the maximum value can be 3 // by taking all the three in the order: 3, 1, 2 // Hence to avoid checking of all the possible permutations, // we need to sort them in some order // Now we need to decide the comparator function for sorting // Suppose we have 2 blocks: 1 and 2 // and they should be such that 1 should be above 2 // Then a good (or strong) comparator function will ensure // that 1 comes before 2 in the array always // For 2 to be below 1, this condition must be satisfied: s2 >= w1 // Also notice that for 1 to be below 2: s1 >= w2 but we don't want this // So we also want to satisfy: s1 < w2 // So that both conditions are satisfied simultaneously: // 1) 2 is below 1 // 2) 1 is never below 2 // Both conditions must be satisfied to avoid any contradiction // Hence there are 2 conditions: s2 >= w1 and s1 < w2 // s2 >= w1 and w2 > s1 // On combining these 2 conditions: s2+w2 >= w1+s1 OR s1+w1 <= s2+w2 // This single condition will ensure that 2 comes below 1 OR // 2 comes after 1 in the array AND also there is no contradiction // I have already defined the comparator in the structure definition itself sort(arr, arr + n); // Now we apply Dynamic Programming: // dp[i][j] stores the maximum sum of value of some subsequence // considered till i-th element and having the sum of weights as 'j' // So whenever a new element comes, we check for which maximum // value in dp[i-1][], // the solidness of that new element is greater than or equal to // the value in dp[i-1][] (sum of some weights) and then update // the dp value corresponding to the new sum of weights if better // The first dimension of dp[][] will be the no. of elements // Now we can take the second dimension to be the sum of all weights // but notice that the maximum allowed sum of weights // will be limited by the solidness of the bottom most block or // the last element which we pick from the array, i.e. // the maximum possible sum of weights for any subsequence will // be at max (maxS + maxW) // where maxS is the max sum of weights of all the blocks except the // bottom most block and maxW is the max weight of the bottom most block // Hence we can take the second dimension of size (maxS + maxW) int maxS = 0, maxW = 0; for (int i = 0; i < n; i++) { if (arr[i].s > maxS) maxS = arr[i].s; if (arr[i].w > maxW) maxW = arr[i].w; } vector<vector<ll>> dp(n + 1, vector<ll>(maxS + maxW + 1, 0)); for (int i = 1; i <= n; i++) { int w = arr[i - 1].w, s = arr[i - 1].s, v = arr[i - 1].v; // We need to go to all the values of previous row // to also copy them to the current row for (int j = 0; j <= maxS + maxW; j++) { dp[i][j] = max(dp[i][j], dp[i - 1][j]); if (s >= j) dp[i][j + w] = max(dp[i - 1][j + w], dp[i - 1][j] + v); } } // The maximum value in dp[n][] is our answer ll maxSumOfValue = 0; for (int i = 0; i <= maxS + maxW; i++) if (dp[n][i] > maxSumOfValue) maxSumOfValue = dp[n][i]; return maxSumOfValue; } ll towerOptimized(Block arr[], int n) { // Instead of maintaining a 2D array, // we can maintain a rolling 1D array like in knapsack sort(arr, arr + n); int maxS = 0, maxW = 0; for (int i = 0; i < n; i++) { if (arr[i].s > maxS) maxS = arr[i].s; if (arr[i].w > maxW) maxW = arr[i].w; } vector<ll> dp(maxS + maxW + 1, 0); for (int i = 1; i <= n; i++) { int w = arr[i - 1].w, s = arr[i - 1].s, v = arr[i - 1].v; for (int j = s; j >= 0; j--) dp[j + w] = max(dp[j + w], dp[j] + v); } ll maxSumOfValue = 0; for (int i = 0; i <= maxS + maxW; i++) if (dp[i] > maxSumOfValue) maxSumOfValue = dp[i]; return maxSumOfValue; } int main() { int n; cin >> n; Block arr[n]; for (int i = 0; i < n; i++) cin >> arr[i].w >> arr[i].s >> arr[i].v; // ll ans = tower(arr, n); ll ans = towerOptimized(arr, n); cout << ans; return 0; }
replace
6
9
6
9
0
p03183
C++
Runtime Error
#include <bits/stdc++.h> #define int long long #define maxc 20005 #define maxn 105 using namespace std; struct Pt { int w, s, v; void RD() { cin >> w >> s >> v; } bool operator<(const Pt &a) const { return w + s < a.w + a.s; } }; int dp[maxc] = {}; Pt arr[maxn]; int n; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n; for (int i = 1; i <= n; i++) { arr[i].RD(); } sort(arr + 1, arr + n + 1); for (int i = 1; i <= n; i++) { int mx = arr[i].s + arr[i].w; for (int j = mx; j >= arr[i].w; j--) { dp[j] = max(dp[j], dp[j - arr[i].w] + arr[i].v); } } cout << *max_element(dp, dp + maxc) << '\n'; return 0; }
#include <bits/stdc++.h> #define int long long #define maxc 20005 #define maxn 1005 using namespace std; struct Pt { int w, s, v; void RD() { cin >> w >> s >> v; } bool operator<(const Pt &a) const { return w + s < a.w + a.s; } }; int dp[maxc] = {}; Pt arr[maxn]; int n; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n; for (int i = 1; i <= n; i++) { arr[i].RD(); } sort(arr + 1, arr + n + 1); for (int i = 1; i <= n; i++) { int mx = arr[i].s + arr[i].w; for (int j = mx; j >= arr[i].w; j--) { dp[j] = max(dp[j], dp[j - arr[i].w] + arr[i].v); } } cout << *max_element(dp, dp + maxc) << '\n'; return 0; }
replace
3
4
3
4
0
p03183
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < (n); ++i) #define rep(i, a, b) for (int i = a; i < (b); ++i) #define p(s) std::cout << s; #define pl(s) std::cout << s << endl; #define printIf(j, s1, s2) cout << (j ? s1 : s2) << endl; #define YES(j) cout << (j ? "YES" : "NO") << endl; #define Yes(j) std::cout << (j ? "Yes" : "No") << endl; #define yes(j) std::cout << (j ? "yes" : "no") << endl; #define all(v) v.begin(), v.end() #define showVector(v) \ REP(i, v.size()) { \ p(v[i]); \ p(" ") \ } \ pl("") template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } typedef long long int ll; typedef pair<ll, ll> P_ii; typedef pair<double, double> P_dd; const ll longinf = 1LL << 60; struct LazySegmentTree { private: int n; vector<ll> node, lazy; public: LazySegmentTree(int sz, ll init = 0) { n = 1; while (n < sz) n *= 2; node.resize(2 * n - 1, init); lazy.resize(2 * n - 1, 0); } void eval(int k, int l, int r) { if (lazy[k] != 0) node[k] += lazy[k]; if (r - l > 1) { lazy[2 * k + 1] += lazy[k]; lazy[2 * k + 2] += lazy[k]; } lazy[k] = 0; } //[a,b)にxを加算 void add(int a, int b, ll x, int k = 0, int l = 0, int r = -1) { if (r < 0) r = n; eval(k, l, r); if (r <= a || b <= l) return; if (a <= l && r <= b) { lazy[k] += x; eval(k, l, r); } else { add(a, b, x, 2 * k + 1, l, (l + r) / 2); add(a, b, x, 2 * k + 2, (l + r) / 2, r); node[k] = max(node[2 * k + 1], node[2 * k + 2]); } } //[a,b)でのmaxを返す ll get(int a, int b, int k = 0, int l = 0, int r = -1) { if (r < 0) r = n; eval(k, l, r); if (r <= a || b <= l) return 0; if (a <= l && r <= b) return node[k]; ll xl = get(a, b, 2 * k + 1, l, (l + r) / 2); ll xr = get(a, b, 2 * k + 2, (l + r) / 2, r); return max(xl, xr); } }; int main() { int N; cin >> N; vector<ll> w(N), s(N), v(N); REP(i, N) cin >> w[i] >> s[i] >> v[i]; vector<int> ord(N); iota(all(ord), 0); sort(all(ord), [&](int x, int y) { return min(s[x], s[y] - w[x]) > min(s[y], s[x] - w[y]); }); // dp[i] := 上から順に重量iまで積んだ時の最大価値 vector<ll> dp((int)1e4 + 5, -1); dp[0] = 0; for (int i : ord) { for (int j = (int)1e4; j >= 0; j--) { if (dp[j] == -1) continue; // 上にjの重量を載せるには、丈夫さがそれ以上になる必要がある if (j <= s[i]) chmax(dp[j + w[i]], dp[j] + v[i]); } } cout << *max_element(all(dp)) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < (n); ++i) #define rep(i, a, b) for (int i = a; i < (b); ++i) #define p(s) std::cout << s; #define pl(s) std::cout << s << endl; #define printIf(j, s1, s2) cout << (j ? s1 : s2) << endl; #define YES(j) cout << (j ? "YES" : "NO") << endl; #define Yes(j) std::cout << (j ? "Yes" : "No") << endl; #define yes(j) std::cout << (j ? "yes" : "no") << endl; #define all(v) v.begin(), v.end() #define showVector(v) \ REP(i, v.size()) { \ p(v[i]); \ p(" ") \ } \ pl("") template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } typedef long long int ll; typedef pair<ll, ll> P_ii; typedef pair<double, double> P_dd; const ll longinf = 1LL << 60; struct LazySegmentTree { private: int n; vector<ll> node, lazy; public: LazySegmentTree(int sz, ll init = 0) { n = 1; while (n < sz) n *= 2; node.resize(2 * n - 1, init); lazy.resize(2 * n - 1, 0); } void eval(int k, int l, int r) { if (lazy[k] != 0) node[k] += lazy[k]; if (r - l > 1) { lazy[2 * k + 1] += lazy[k]; lazy[2 * k + 2] += lazy[k]; } lazy[k] = 0; } //[a,b)にxを加算 void add(int a, int b, ll x, int k = 0, int l = 0, int r = -1) { if (r < 0) r = n; eval(k, l, r); if (r <= a || b <= l) return; if (a <= l && r <= b) { lazy[k] += x; eval(k, l, r); } else { add(a, b, x, 2 * k + 1, l, (l + r) / 2); add(a, b, x, 2 * k + 2, (l + r) / 2, r); node[k] = max(node[2 * k + 1], node[2 * k + 2]); } } //[a,b)でのmaxを返す ll get(int a, int b, int k = 0, int l = 0, int r = -1) { if (r < 0) r = n; eval(k, l, r); if (r <= a || b <= l) return 0; if (a <= l && r <= b) return node[k]; ll xl = get(a, b, 2 * k + 1, l, (l + r) / 2); ll xr = get(a, b, 2 * k + 2, (l + r) / 2, r); return max(xl, xr); } }; int main() { int N; cin >> N; vector<ll> w(N), s(N), v(N); REP(i, N) cin >> w[i] >> s[i] >> v[i]; vector<int> ord(N); iota(all(ord), 0); sort(all(ord), [&](int x, int y) { return min(s[x], s[y] - w[x]) > min(s[y], s[x] - w[y]); }); // dp[i] := 上から順に重量iまで積んだ時の最大価値 vector<ll> dp((int)2e4 + 5, -1); dp[0] = 0; for (int i : ord) { for (int j = (int)1e4; j >= 0; j--) { if (dp[j] == -1) continue; // 上にjの重量を載せるには、丈夫さがそれ以上になる必要がある if (j <= s[i]) chmax(dp[j + w[i]], dp[j] + v[i]); } } cout << *max_element(all(dp)) << endl; return 0; }
replace
106
107
106
107
0
p03183
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, m, n) for (int(i) = (int)(m); i < (int)(n); i++) #define rep2(i, m, n) for (int(i) = (int)(n)-1; i >= (int)(m); i--) #define REP(i, n) rep(i, 0, n) #define REP2(i, n) rep2(i, 0, n) #define FOR(i, c) \ for (decltype((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define ll long long #define ull unsigned long long #define all(hoge) (hoge).begin(), (hoge).end() #define en '\n' template <class T> using vec = vector<T>; template <class T> using vvec = vector<vec<T>>; typedef pair<ll, ll> P; constexpr long long INF = 1LL << 60; constexpr int INF_INT = 1 << 25; constexpr long long MOD = (ll)1e9 + 7; typedef vector<ll> Array; typedef vector<Array> Matrix; constexpr int loose = 0; constexpr int tight = 1; template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } struct Edge { ll to, cap, rev; Edge(ll _to, ll _cap, ll _rev) { to = _to; cap = _cap; rev = _rev; } }; typedef vector<Edge> Edges; typedef vector<Edges> Graph; void add_edge(Graph &G, ll from, ll to, ll cap, bool revFlag, ll revCap) { G[from].push_back(Edge(to, cap, (ll)G[to].size())); if (revFlag) G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1)); } void solve() { ll n; cin >> n; Array w(n), s(n), v(n); REP(i, n) cin >> w[i] >> s[i] >> v[i]; Array o(n); REP(i, n) o[i] = i; sort(all(o), [&](ll i, ll j) { return min(s[i] - w[j], s[j]) < min(s[j] - w[i], s[i]); }); ll sn = 10101; Matrix dp(n + 1, Array(sn, -1)); dp[0][0] = 0; REP(i, n) { REP(j, sn) { if (dp[i][j] == -1) continue; if (j <= s[o[i]]) chmax(dp[i + 1][j + w[o[i]]], dp[i][j] + v[o[i]]); // のっける chmax(dp[i + 1][j], dp[i][j]); // のっけない } } ll ans = 0; REP(i, sn) chmax(ans, dp[n][i]); cout << ans << en; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, m, n) for (int(i) = (int)(m); i < (int)(n); i++) #define rep2(i, m, n) for (int(i) = (int)(n)-1; i >= (int)(m); i--) #define REP(i, n) rep(i, 0, n) #define REP2(i, n) rep2(i, 0, n) #define FOR(i, c) \ for (decltype((c).begin()) i = (c).begin(); i != (c).end(); ++i) #define ll long long #define ull unsigned long long #define all(hoge) (hoge).begin(), (hoge).end() #define en '\n' template <class T> using vec = vector<T>; template <class T> using vvec = vector<vec<T>>; typedef pair<ll, ll> P; constexpr long long INF = 1LL << 60; constexpr int INF_INT = 1 << 25; constexpr long long MOD = (ll)1e9 + 7; typedef vector<ll> Array; typedef vector<Array> Matrix; constexpr int loose = 0; constexpr int tight = 1; template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } struct Edge { ll to, cap, rev; Edge(ll _to, ll _cap, ll _rev) { to = _to; cap = _cap; rev = _rev; } }; typedef vector<Edge> Edges; typedef vector<Edges> Graph; void add_edge(Graph &G, ll from, ll to, ll cap, bool revFlag, ll revCap) { G[from].push_back(Edge(to, cap, (ll)G[to].size())); if (revFlag) G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1)); } void solve() { ll n; cin >> n; Array w(n), s(n), v(n); REP(i, n) cin >> w[i] >> s[i] >> v[i]; Array o(n); REP(i, n) o[i] = i; sort(all(o), [&](ll i, ll j) { return min(s[i] - w[j], s[j]) < min(s[j] - w[i], s[i]); }); ll sn = 20101; Matrix dp(n + 1, Array(sn, -1)); dp[0][0] = 0; REP(i, n) { REP(j, sn) { if (dp[i][j] == -1) continue; if (j <= s[o[i]]) chmax(dp[i + 1][j + w[o[i]]], dp[i][j] + v[o[i]]); // のっける chmax(dp[i + 1][j], dp[i][j]); // のっけない } } ll ans = 0; REP(i, sn) chmax(ans, dp[n][i]); cout << ans << en; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); solve(); return 0; }
replace
68
69
68
69
0
p03183
C++
Runtime Error
#include <bits/stdc++.h> #define f first #define s second #define fore(i, a, b) for (int i = (a), ThxMK = (b); i < ThxMK; ++i) #define pb push_back #define all(s) begin(s), end(s) #define _ \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define sz(s) int(s.size()) #define ENDL '\n' using namespace std; typedef long double ld; typedef long long lli; typedef pair<lli, lli> ii; typedef vector<int> vi; typedef vector<lli> vl; typedef vector<ii> vii; typedef vector<vi> vvi; typedef vector<vl> vvl; #define deb(x) cout << #x ": " << (x) << endl; const int N = 1e4 + 5; const int INF = 1e9; const lli MOD = 1e9 + 7; vi primes(1, 2); vector<vector<ld>> nk; void sieve() { vi m(N + 1, 0); for (int i = 4; i < N; i += 2) m[i] = 1; for (int i = 3; i * i <= N; i += 2) if (!m[i]) for (int j = i * i; j < N; j += i) m[j] = 1; for (int i = 3; i < N; i += 2) if (!m[i]) primes.pb(i); } void pascal() { nk = vector<vector<ld>>(N, vector<ld>(N, 0.0)); fore(i, 0, N) nk[i][0] = nk[i][i] = 1.0; fore(i, 1, N) fore(j, 1, i) nk[i][j] = nk[i - 1][j - 1] + nk[i - 1][j]; } lli gcd(lli a, lli b) { return (b ? gcd(b, a % b) : a); } lli lcm(lli a, lli b) { if (a < b) swap(a, b); lli c = gcd(a, b); a /= c; return a * b; } int popcount(lli x) { return __builtin_popcountll(x); } lli poww(lli a, lli b) { lli res = 1; while (b) { if (b & 1) res = res * a; a = a * a; b /= 2; } return res; } lli powm(lli a, lli b) { lli res = 1; while (b) { if (b & 1) res = (res * a) % MOD; a = (a * a) % MOD; b /= 2; } return res; } bool isPrime(lli x) { if (x == 1) return 0; for (auto i : primes) { if (i * i > x) return 1; if (x % i == 0) return 0; } return 1; } lli SUM(lli a, lli b, lli mod = MOD) { return (a + b + mod) % mod; } lli PROD(lli a, lli b, lli mod = MOD) { a %= mod; b %= mod; return a * b % mod; } int dx[4] = {-1, 0, 0, 1}, dy[4] = {0, 1, -1, 0}; // ---- コーディングはここから! ('-')7 struct S { lli w, s, v; bool operator<(const S &n) const { return ii{w + s, v} < ii{n.w + n.s, n.v}; } }; void solve() { int n; cin >> n; vector<S> v(n); for (auto &[a, b, c] : v) cin >> a >> b >> c; sort(all(v)); // for(auto &[a,b,c]:v)cout<<a<<b<<c<<ENDL; vvl dp(n, vl(N, -1)); auto f = [&](auto f, int u, lli sum) -> lli { if (u == n) return 0; if (dp[u][sum] != -1) return dp[u][sum]; lli res = f(f, u + 1, sum); if (v[u].s >= sum) res = max(res, f(f, u + 1, sum + v[u].w) + v[u].v); return dp[u][sum] = res; }; cout << f(f, 0, 0) << ENDL; } int main() { _ // int t; cin>>t; fore(tt,0,t) solve(); }
#include <bits/stdc++.h> #define f first #define s second #define fore(i, a, b) for (int i = (a), ThxMK = (b); i < ThxMK; ++i) #define pb push_back #define all(s) begin(s), end(s) #define _ \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define sz(s) int(s.size()) #define ENDL '\n' using namespace std; typedef long double ld; typedef long long lli; typedef pair<lli, lli> ii; typedef vector<int> vi; typedef vector<lli> vl; typedef vector<ii> vii; typedef vector<vi> vvi; typedef vector<vl> vvl; #define deb(x) cout << #x ": " << (x) << endl; const int N = 1e4 + 5; const int INF = 1e9; const lli MOD = 1e9 + 7; vi primes(1, 2); vector<vector<ld>> nk; void sieve() { vi m(N + 1, 0); for (int i = 4; i < N; i += 2) m[i] = 1; for (int i = 3; i * i <= N; i += 2) if (!m[i]) for (int j = i * i; j < N; j += i) m[j] = 1; for (int i = 3; i < N; i += 2) if (!m[i]) primes.pb(i); } void pascal() { nk = vector<vector<ld>>(N, vector<ld>(N, 0.0)); fore(i, 0, N) nk[i][0] = nk[i][i] = 1.0; fore(i, 1, N) fore(j, 1, i) nk[i][j] = nk[i - 1][j - 1] + nk[i - 1][j]; } lli gcd(lli a, lli b) { return (b ? gcd(b, a % b) : a); } lli lcm(lli a, lli b) { if (a < b) swap(a, b); lli c = gcd(a, b); a /= c; return a * b; } int popcount(lli x) { return __builtin_popcountll(x); } lli poww(lli a, lli b) { lli res = 1; while (b) { if (b & 1) res = res * a; a = a * a; b /= 2; } return res; } lli powm(lli a, lli b) { lli res = 1; while (b) { if (b & 1) res = (res * a) % MOD; a = (a * a) % MOD; b /= 2; } return res; } bool isPrime(lli x) { if (x == 1) return 0; for (auto i : primes) { if (i * i > x) return 1; if (x % i == 0) return 0; } return 1; } lli SUM(lli a, lli b, lli mod = MOD) { return (a + b + mod) % mod; } lli PROD(lli a, lli b, lli mod = MOD) { a %= mod; b %= mod; return a * b % mod; } int dx[4] = {-1, 0, 0, 1}, dy[4] = {0, 1, -1, 0}; // ---- コーディングはここから! ('-')7 struct S { lli w, s, v; bool operator<(const S &n) const { return ii{w + s, v} < ii{n.w + n.s, n.v}; } }; void solve() { int n; cin >> n; vector<S> v(n); for (auto &[a, b, c] : v) cin >> a >> b >> c; sort(all(v)); // for(auto &[a,b,c]:v)cout<<a<<b<<c<<ENDL; vvl dp(n, vl(N, -1)); auto f = [&](auto f, int u, lli sum) -> lli { if (u == n or sum >= N) return 0; if (dp[u][sum] != -1) return dp[u][sum]; lli res = f(f, u + 1, sum); if (v[u].s >= sum) res = max(res, f(f, u + 1, sum + v[u].w) + v[u].v); return dp[u][sum] = res; }; cout << f(f, 0, 0) << ENDL; } int main() { _ // int t; cin>>t; fore(tt,0,t) solve(); }
replace
117
118
117
118
0
p03183
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int N = 1e3 + 5, MAX_S = 2e4 + 5; int n; long long dp[N][MAX_S], ans; struct Block { int w, s, v; Block(int w = 0, int s = 0, int v = 0) : w(w), s(s), v(v) {} bool operator<(const Block &rhs) const { return w + s == rhs.w + rhs.s ? s < rhs.s : w + s < rhs.w + rhs.s; } } block[N]; int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { int w, s, v; scanf("%d %d %d", &w, &s, &v); block[i] = Block(w, s, v); } sort(block + 1, block + n + 1); memset(dp, -1, sizeof dp); dp[0][0] = 0; for (int i = 1; i <= n; ++i) { for (int j = 0; j < MAX_S; ++j) { dp[i][j] = max(dp[i][j], dp[i - 1][j]); if (dp[i - 1][j - block[i].w] != -1 && j - block[i].w >= 0 && j - block[i].w <= block[i].s) dp[i][j] = max(dp[i][j], dp[i - 1][j - block[i].w] + block[i].v); ans = max(ans, dp[i][j]); } } printf("%lld", ans); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e3 + 5, MAX_S = 2e4 + 5; int n; long long dp[N][MAX_S], ans; struct Block { int w, s, v; Block(int w = 0, int s = 0, int v = 0) : w(w), s(s), v(v) {} bool operator<(const Block &rhs) const { return w + s == rhs.w + rhs.s ? s < rhs.s : w + s < rhs.w + rhs.s; } } block[N]; int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { int w, s, v; scanf("%d %d %d", &w, &s, &v); block[i] = Block(w, s, v); } sort(block + 1, block + n + 1); memset(dp, -1, sizeof dp); dp[0][0] = 0; for (int i = 1; i <= n; ++i) { for (int j = 0; j < MAX_S; ++j) { dp[i][j] = max(dp[i][j], dp[i - 1][j]); if (j - block[i].w >= 0 && j - block[i].w <= block[i].s && dp[i - 1][j - block[i].w] != -1) dp[i][j] = max(dp[i][j], dp[i - 1][j - block[i].w] + block[i].v); ans = max(ans, dp[i][j]); } } printf("%lld", ans); return 0; }
replace
30
32
30
32
-11
p03183
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define i_7 (ll)(1E9 + 7) #define i_5 (ll)(1E9 + 5) ll mod(ll a) { ll c = a % i_7; if (c >= 0) return c; else return c + i_7; } typedef pair<int, int> i_i; typedef pair<ll, ll> l_l; ll inf = (ll)1E12; #define rep(i, l, r) for (ll i = l; i <= r; i++) #define pb push_back ll max(ll a, ll b) { if (a < b) return b; else return a; } ll min(ll a, ll b) { if (a > b) return b; else return a; } void Max(ll *pos, ll val) { *pos = max(*pos, val); } // Max(&dp[i][j],dp[i-1][j]); void Min(ll *pos, ll val) { *pos = min(*pos, val); } void Add(ll *pos, ll val) { *pos = mod(*pos + val); } const long double EPS = 1E-8; //////////////////////////////////////// struct tower { ll w, s, v; }; bool comp(tower t1, tower t2) { if (t1.w + t1.s < t2.w + t2.s) return true; else return false; } int main() { ll n; cin >> n; tower t[n]; rep(i, 0, n - 1) { cin >> t[i].w >> t[i].s >> t[i].v; } sort(t, t + n, comp); ll N = (ll)1E5 + 5; ll dp[n + 1][N]; memset(dp, 0, sizeof(dp)); rep(i, 0, n - 1) { rep(j, 0, N - 1) { dp[i + 1][j] = dp[i][j]; if (0 <= j - t[i].w && j - t[i].w <= t[i].s) Max(&dp[i + 1][j], dp[i][j - t[i].w] + t[i].v); } } /*rep(i,0,n){ rep(j,0,20){ cout<<dp[i][j]<<" "; }cout<<endl; }*/ ll ans = 0; rep(i, 0, N - 1) Max(&ans, dp[n][i]); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define i_7 (ll)(1E9 + 7) #define i_5 (ll)(1E9 + 5) ll mod(ll a) { ll c = a % i_7; if (c >= 0) return c; else return c + i_7; } typedef pair<int, int> i_i; typedef pair<ll, ll> l_l; ll inf = (ll)1E12; #define rep(i, l, r) for (ll i = l; i <= r; i++) #define pb push_back ll max(ll a, ll b) { if (a < b) return b; else return a; } ll min(ll a, ll b) { if (a > b) return b; else return a; } void Max(ll *pos, ll val) { *pos = max(*pos, val); } // Max(&dp[i][j],dp[i-1][j]); void Min(ll *pos, ll val) { *pos = min(*pos, val); } void Add(ll *pos, ll val) { *pos = mod(*pos + val); } const long double EPS = 1E-8; //////////////////////////////////////// struct tower { ll w, s, v; }; bool comp(tower t1, tower t2) { if (t1.w + t1.s < t2.w + t2.s) return true; else return false; } int main() { ll n; cin >> n; tower t[n]; rep(i, 0, n - 1) { cin >> t[i].w >> t[i].s >> t[i].v; } sort(t, t + n, comp); ll N = 30005; ll dp[n + 1][N]; memset(dp, 0, sizeof(dp)); rep(i, 0, n - 1) { rep(j, 0, N - 1) { dp[i + 1][j] = dp[i][j]; if (0 <= j - t[i].w && j - t[i].w <= t[i].s) Max(&dp[i + 1][j], dp[i][j - t[i].w] + t[i].v); } } /*rep(i,0,n){ rep(j,0,20){ cout<<dp[i][j]<<" "; }cout<<endl; }*/ ll ans = 0; rep(i, 0, N - 1) Max(&ans, dp[n][i]); cout << ans << endl; return 0; }
replace
53
54
53
54
0
p03183
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #if __has_include(<boost/multiprecision/cpp_int.hpp>) #include <boost/multiprecision/cpp_int.hpp> #include <boost/multiprecision/cpp_dec_float.hpp> using bll = boost::multiprecision::cpp_int; using bdouble = boost::multiprecision::number<boost::multiprecision::cpp_dec_float<100>>; using namespace boost::multiprecision; #endif #if __has_include(<boost/stacktrace.hpp>) #define BOOST_STACKTRACE_USE_ADDR2LINE #define BOOST_STACKTRACE_ADDR2LINE_LOCATION \ / usr / local / opt / binutils / bin / addr2line #define _GNU_SOURCE 1 #include <boost/stacktrace.hpp> #endif #ifdef LOCAL_TEST namespace std { template <typename T> class dvector : public std::vector<T> { public: dvector() : std::vector<T>() {} explicit dvector(size_t n, const T &value = T()) : std::vector<T>(n, value) {} dvector(const std::vector<T> &v) : std::vector<T>(v) {} dvector(const std::initializer_list<T> il) : std::vector<T>(il) {} dvector(const std::string::iterator first, const std::string::iterator last) : std::vector<T>(first, last) {} dvector(const typename std::vector<T>::iterator first, const typename std::vector<T>::iterator last) : std::vector<T>(first, last) {} dvector(const typename std::vector<T>::reverse_iterator first, const typename std::vector<T>::reverse_iterator last) : std::vector<T>(first, last) {} dvector(const typename std::vector<T>::const_iterator first, const typename std::vector<T>::const_iterator last) : std::vector<T>(first, last) {} dvector(const typename std::vector<T>::const_reverse_iterator first, const typename std::vector<T>::const_reverse_iterator last) : std::vector<T>(first, last) {} T &operator[](size_t n) { try { return this->at(n); } catch (const std::exception &e) { std::cerr << boost::stacktrace::stacktrace() << '\n'; return this->at(n); } } const T &operator[](size_t n) const { try { return this->at(n); } catch (const std::exception &e) { std::cerr << boost::stacktrace::stacktrace() << '\n'; return this->at(n); } } }; } // namespace std class dbool { private: bool boolvalue; public: dbool() : boolvalue(false) {} dbool(bool b) : boolvalue(b) {} operator bool &() { return boolvalue; } operator const bool &() const { return boolvalue; } }; #define vector dvector #define bool dbool #endif #ifdef LOCAL_DEV template <typename T> std::ostream &operator<<(std::ostream &s, const std::vector<T> &v) { for (size_t i = 0; i < v.size(); ++i) { s << v[i]; if (i < v.size() - 1) s << "\t"; } return s; } template <typename T> std::ostream &operator<<(std::ostream &s, const std::vector<std::vector<T>> &vv) { s << "\\\n"; for (size_t i = 0; i < vv.size(); ++i) { s << vv[i] << "\n"; } return s; } template <typename T> std::ostream &operator<<(std::ostream &s, const std::deque<T> &v) { for (size_t i = 0; i < v.size(); ++i) { s << v[i]; if (i < v.size() - 1) s << "\t"; } return s; } template <typename T> std::ostream &operator<<(std::ostream &s, const std::set<T> &se) { s << "{ "; for (auto itr = se.begin(); itr != se.end(); ++itr) { s << (*itr) << "\t"; } s << "}"; return s; } template <typename T> std::ostream &operator<<(std::ostream &s, const std::multiset<T> &se) { s << "{ "; for (auto itr = se.begin(); itr != se.end(); ++itr) { s << (*itr) << "\t"; } s << "}"; return s; } template <typename T, size_t N> std::ostream &operator<<(std::ostream &s, const std::array<T, N> &a) { s << "{ "; for (size_t i = 0; i < N; ++i) { s << a[i] << "\t"; } s << "}"; return s; } template <typename T1, typename T2> std::ostream &operator<<(std::ostream &s, const std::map<T1, T2> &m) { s << "{\n"; for (auto itr = m.begin(); itr != m.end(); ++itr) { s << "\t" << (*itr).first << " : " << (*itr).second << "\n"; } s << "}"; return s; } template <typename T1, typename T2> std::ostream &operator<<(std::ostream &s, const std::pair<T1, T2> &p) { return s << "(" << p.first << ", " << p.second << ")"; } class SIGFPE_exception : std::exception {}; class SIGSEGV_exception : std::exception {}; void catch_SIGFPE([[maybe_unused]] int e) { std::cerr << boost::stacktrace::stacktrace() << '\n'; throw SIGFPE_exception(); } void catch_SIGSEGV([[maybe_unused]] int e) { std::cerr << boost::stacktrace::stacktrace() << '\n'; throw SIGSEGV_exception(); } signed convertedmain(); signed main() { signal(SIGFPE, catch_SIGFPE); signal(SIGSEGV, catch_SIGSEGV); return convertedmain(); } #define main() convertedmain() void debug_impl() { std::cerr << '\n'; } template <typename Head, typename... Tail> void debug_impl(Head head, Tail... tail) { std::cerr << " " << head << (sizeof...(tail) ? "," : ""); debug_impl(tail...); } #define debug(...) \ do { \ std::cerr << "(" << #__VA_ARGS__ << ") ="; \ debug_impl(__VA_ARGS__); \ } while (false) #else #define debug(...) \ do { \ } while (false) #endif // #define int long long using ll = long long; // constexpr int INF = (ll)1e9 + 7;//INT_MAX=(1<<31)-1=2147483647 constexpr ll INF = (ll)1e18; //(1LL<<63)-1=9223372036854775807 constexpr ll MOD = (ll)1e9 + 7; constexpr double EPS = 1e-9; constexpr ll dx[4] = {1, 0, -1, 0}; constexpr ll dy[4] = {0, 1, 0, -1}; constexpr ll dx8[8] = {1, 0, -1, 0, 1, 1, -1, -1}; constexpr ll dy8[8] = {0, 1, 0, -1, 1, -1, 1, -1}; #define rep(i, n) for (ll i = 0, i##_length = (n); i < i##_length; ++i) #define repeq(i, n) for (ll i = 1, i##_length = (n); i <= i##_length; ++i) #define rrep(i, n) for (ll i = (n)-1; i >= 0; --i) #define rrepeq(i, n) for (ll i = (n); i >= 1; --i) #define all(v) (v).begin(), (v).end() #define rall(v) (v).rbegin(), (v).rend() void p() { std::cout << '\n'; } template <typename Head, typename... Tail> void p(Head head, Tail... tail) { std::cout << head << (sizeof...(tail) ? " " : ""); p(tail...); } template <typename T> inline void pv(std::vector<T> &v) { for (ll i = 0, N = v.size(); i < N; i++) std::cout << v[i] << " \n"[i == N - 1]; } template <typename T> inline bool chmax(T &a, T b) { return a < b && (a = b, true); } template <typename T> inline bool chmin(T &a, T b) { return a > b && (a = b, true); } template <typename T> inline void uniq(std::vector<T> &v) { v.erase(std::unique(v.begin(), v.end()), v.end()); } /*-----8<-----template-----8<-----*/ /*-----8<-----library-----8<-----*/ constexpr ll N_MAX = 1000; constexpr ll S_I_MAX = 10000; constexpr ll V_I_MAX = 1000000000; ll N; void solve() { scanf("%lld", &N); vector<array<ll, 3>> a(N); rep(i, N) scanf("%lld%lld%lld", &a[i][0], &a[i][1], &a[i][2]); sort(a.begin(), a.end(), [](const array<ll, 3> &x, const array<ll, 3> &y) { return x[0] + x[1] < y[0] + y[1]; }); ll maxweight = 0; rep(i, N) maxweight += a[i][0]; // w,s,v vector<vector<ll>> dp(N + 1, vector<ll>(maxweight + 1, 0)); rep(i, N) rep(w, maxweight + 1) { chmax(dp[i + 1][w], dp[i][w]); if (w - a[i][0] >= 0 && w - a[i][0] <= a[i][1]) chmax(dp[i + 1][w], dp[i][w - a[i][0]] + a[i][2]); } debug(a); debug(dp); ll ans = 0; rep(i, maxweight + 1) chmax(ans, dp[N][i]); printf("%lld\n", ans); } signed main() { solve(); return 0; }
#include <bits/stdc++.h> using namespace std; #if __has_include(<boost/multiprecision/cpp_int.hpp>) #include <boost/multiprecision/cpp_int.hpp> #include <boost/multiprecision/cpp_dec_float.hpp> using bll = boost::multiprecision::cpp_int; using bdouble = boost::multiprecision::number<boost::multiprecision::cpp_dec_float<100>>; using namespace boost::multiprecision; #endif #if __has_include(<boost/stacktrace.hpp>) #define BOOST_STACKTRACE_USE_ADDR2LINE #define BOOST_STACKTRACE_ADDR2LINE_LOCATION \ / usr / local / opt / binutils / bin / addr2line #define _GNU_SOURCE 1 #include <boost/stacktrace.hpp> #endif #ifdef LOCAL_TEST namespace std { template <typename T> class dvector : public std::vector<T> { public: dvector() : std::vector<T>() {} explicit dvector(size_t n, const T &value = T()) : std::vector<T>(n, value) {} dvector(const std::vector<T> &v) : std::vector<T>(v) {} dvector(const std::initializer_list<T> il) : std::vector<T>(il) {} dvector(const std::string::iterator first, const std::string::iterator last) : std::vector<T>(first, last) {} dvector(const typename std::vector<T>::iterator first, const typename std::vector<T>::iterator last) : std::vector<T>(first, last) {} dvector(const typename std::vector<T>::reverse_iterator first, const typename std::vector<T>::reverse_iterator last) : std::vector<T>(first, last) {} dvector(const typename std::vector<T>::const_iterator first, const typename std::vector<T>::const_iterator last) : std::vector<T>(first, last) {} dvector(const typename std::vector<T>::const_reverse_iterator first, const typename std::vector<T>::const_reverse_iterator last) : std::vector<T>(first, last) {} T &operator[](size_t n) { try { return this->at(n); } catch (const std::exception &e) { std::cerr << boost::stacktrace::stacktrace() << '\n'; return this->at(n); } } const T &operator[](size_t n) const { try { return this->at(n); } catch (const std::exception &e) { std::cerr << boost::stacktrace::stacktrace() << '\n'; return this->at(n); } } }; } // namespace std class dbool { private: bool boolvalue; public: dbool() : boolvalue(false) {} dbool(bool b) : boolvalue(b) {} operator bool &() { return boolvalue; } operator const bool &() const { return boolvalue; } }; #define vector dvector #define bool dbool #endif #ifdef LOCAL_DEV template <typename T> std::ostream &operator<<(std::ostream &s, const std::vector<T> &v) { for (size_t i = 0; i < v.size(); ++i) { s << v[i]; if (i < v.size() - 1) s << "\t"; } return s; } template <typename T> std::ostream &operator<<(std::ostream &s, const std::vector<std::vector<T>> &vv) { s << "\\\n"; for (size_t i = 0; i < vv.size(); ++i) { s << vv[i] << "\n"; } return s; } template <typename T> std::ostream &operator<<(std::ostream &s, const std::deque<T> &v) { for (size_t i = 0; i < v.size(); ++i) { s << v[i]; if (i < v.size() - 1) s << "\t"; } return s; } template <typename T> std::ostream &operator<<(std::ostream &s, const std::set<T> &se) { s << "{ "; for (auto itr = se.begin(); itr != se.end(); ++itr) { s << (*itr) << "\t"; } s << "}"; return s; } template <typename T> std::ostream &operator<<(std::ostream &s, const std::multiset<T> &se) { s << "{ "; for (auto itr = se.begin(); itr != se.end(); ++itr) { s << (*itr) << "\t"; } s << "}"; return s; } template <typename T, size_t N> std::ostream &operator<<(std::ostream &s, const std::array<T, N> &a) { s << "{ "; for (size_t i = 0; i < N; ++i) { s << a[i] << "\t"; } s << "}"; return s; } template <typename T1, typename T2> std::ostream &operator<<(std::ostream &s, const std::map<T1, T2> &m) { s << "{\n"; for (auto itr = m.begin(); itr != m.end(); ++itr) { s << "\t" << (*itr).first << " : " << (*itr).second << "\n"; } s << "}"; return s; } template <typename T1, typename T2> std::ostream &operator<<(std::ostream &s, const std::pair<T1, T2> &p) { return s << "(" << p.first << ", " << p.second << ")"; } class SIGFPE_exception : std::exception {}; class SIGSEGV_exception : std::exception {}; void catch_SIGFPE([[maybe_unused]] int e) { std::cerr << boost::stacktrace::stacktrace() << '\n'; throw SIGFPE_exception(); } void catch_SIGSEGV([[maybe_unused]] int e) { std::cerr << boost::stacktrace::stacktrace() << '\n'; throw SIGSEGV_exception(); } signed convertedmain(); signed main() { signal(SIGFPE, catch_SIGFPE); signal(SIGSEGV, catch_SIGSEGV); return convertedmain(); } #define main() convertedmain() void debug_impl() { std::cerr << '\n'; } template <typename Head, typename... Tail> void debug_impl(Head head, Tail... tail) { std::cerr << " " << head << (sizeof...(tail) ? "," : ""); debug_impl(tail...); } #define debug(...) \ do { \ std::cerr << "(" << #__VA_ARGS__ << ") ="; \ debug_impl(__VA_ARGS__); \ } while (false) #else #define debug(...) \ do { \ } while (false) #endif // #define int long long using ll = long long; // constexpr int INF = (ll)1e9 + 7;//INT_MAX=(1<<31)-1=2147483647 constexpr ll INF = (ll)1e18; //(1LL<<63)-1=9223372036854775807 constexpr ll MOD = (ll)1e9 + 7; constexpr double EPS = 1e-9; constexpr ll dx[4] = {1, 0, -1, 0}; constexpr ll dy[4] = {0, 1, 0, -1}; constexpr ll dx8[8] = {1, 0, -1, 0, 1, 1, -1, -1}; constexpr ll dy8[8] = {0, 1, 0, -1, 1, -1, 1, -1}; #define rep(i, n) for (ll i = 0, i##_length = (n); i < i##_length; ++i) #define repeq(i, n) for (ll i = 1, i##_length = (n); i <= i##_length; ++i) #define rrep(i, n) for (ll i = (n)-1; i >= 0; --i) #define rrepeq(i, n) for (ll i = (n); i >= 1; --i) #define all(v) (v).begin(), (v).end() #define rall(v) (v).rbegin(), (v).rend() void p() { std::cout << '\n'; } template <typename Head, typename... Tail> void p(Head head, Tail... tail) { std::cout << head << (sizeof...(tail) ? " " : ""); p(tail...); } template <typename T> inline void pv(std::vector<T> &v) { for (ll i = 0, N = v.size(); i < N; i++) std::cout << v[i] << " \n"[i == N - 1]; } template <typename T> inline bool chmax(T &a, T b) { return a < b && (a = b, true); } template <typename T> inline bool chmin(T &a, T b) { return a > b && (a = b, true); } template <typename T> inline void uniq(std::vector<T> &v) { v.erase(std::unique(v.begin(), v.end()), v.end()); } /*-----8<-----template-----8<-----*/ /*-----8<-----library-----8<-----*/ constexpr ll N_MAX = 1000; constexpr ll S_I_MAX = 10000; constexpr ll V_I_MAX = 1000000000; ll N; void solve() { scanf("%lld", &N); vector<array<ll, 3>> a(N); rep(i, N) scanf("%lld%lld%lld", &a[i][0], &a[i][1], &a[i][2]); sort(a.begin(), a.end(), [](const array<ll, 3> &x, const array<ll, 3> &y) { return x[0] + x[1] < y[0] + y[1]; }); ll s = 0, t = 0; rep(i, N) { chmax(s, a[i][0]); chmax(t, a[i][1]); } ll maxweight = s + t; // w,s,v vector<vector<ll>> dp(N + 1, vector<ll>(maxweight + 1, 0)); rep(i, N) rep(w, maxweight + 1) { chmax(dp[i + 1][w], dp[i][w]); if (w - a[i][0] >= 0 && w - a[i][0] <= a[i][1]) chmax(dp[i + 1][w], dp[i][w - a[i][0]] + a[i][2]); } debug(a); debug(dp); ll ans = 0; rep(i, maxweight + 1) chmax(ans, dp[N][i]); printf("%lld\n", ans); } signed main() { solve(); return 0; }
replace
224
226
224
230
0
p03183
C++
Runtime Error
#include <algorithm> #include <assert.h> #include <bitset> #include <chrono> #include <cmath> #include <complex> #include <cstdlib> #include <cstring> #include <deque> #include <iomanip> #include <iostream> #include <iterator> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <stdio.h> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define pb push_back #define fi first #define se second #define all(x) (x).begin(), (x).end() #define eb emplace_back #define el '\n' typedef long long ll; typedef pair<ll, ll> pll; typedef pair<int, int> pii; typedef vector<ll> vll; typedef vector<int> vi; typedef vector<pii> vpi; typedef vector<pll> vpll; typedef vector<vi> vvi; typedef vector<vll> vvll; typedef vector<bool> vb; // const int mod=1000000007; // const int mod=998244353; struct node { ll s, w, v; bool inline operator<(const node &a) const { return this->s + this->w < a.s + a.w; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll t, n, m, k, q, x, a, b, c, y, l, r; cin >> n; vector<node> v(n); for (int i = 0; i < n; i++) { cin >> v[i].w >> v[i].s >> v[i].v; } sort(all(v)); m = 20000; vll dp(m, 0); for (int i = 0; i < n; i++) { for (int j = (int)v[i].s; j >= 0; j--) { assert(j + v[i].w < m); dp[j + v[i].w] = max(dp[j + v[i].w], dp[j] + v[i].v); } } cout << *max_element(all(dp)) << el; return 0; }
#include <algorithm> #include <assert.h> #include <bitset> #include <chrono> #include <cmath> #include <complex> #include <cstdlib> #include <cstring> #include <deque> #include <iomanip> #include <iostream> #include <iterator> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <stdio.h> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> using namespace std; #define pb push_back #define fi first #define se second #define all(x) (x).begin(), (x).end() #define eb emplace_back #define el '\n' typedef long long ll; typedef pair<ll, ll> pll; typedef pair<int, int> pii; typedef vector<ll> vll; typedef vector<int> vi; typedef vector<pii> vpi; typedef vector<pll> vpll; typedef vector<vi> vvi; typedef vector<vll> vvll; typedef vector<bool> vb; // const int mod=1000000007; // const int mod=998244353; struct node { ll s, w, v; bool inline operator<(const node &a) const { return this->s + this->w < a.s + a.w; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll t, n, m, k, q, x, a, b, c, y, l, r; cin >> n; vector<node> v(n); for (int i = 0; i < n; i++) { cin >> v[i].w >> v[i].s >> v[i].v; } sort(all(v)); m = 30000; vll dp(m, 0); for (int i = 0; i < n; i++) { for (int j = (int)v[i].s; j >= 0; j--) { assert(j + v[i].w < m); dp[j + v[i].w] = max(dp[j + v[i].w], dp[j] + v[i].v); } } cout << *max_element(all(dp)) << el; return 0; }
replace
65
66
65
66
0
p03183
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define ll long long const ll inf = 1e18L + 5; struct block { int w, s; ll v; }; int main() { int n; cin >> n; vector<block> vec(n); int max_w = 0; for (int i = 0; i < n; i++) { cin >> vec[i].w >> vec[i].s >> vec[i].v; max_w += vec[i].w; } sort(vec.begin(), vec.end(), [](block a, block b) { return a.w + a.s < b.w + b.s; }); // dp[i] max value with total weight i so far vector<ll> dp(max_w + 5); // dp[n-1]=vec[n-1].v; for (int i = 0; i < n; i++) { for (int j = max_w - vec[i].w; j >= 0; j--) { if (j <= vec[i].s) { dp[j + vec[i].w] = max(dp[j + vec[i].w], dp[j] + vec[i].v); } } } ll ans = -1; for (int j = max_w; j >= 0; j--) { ans = max(ans, dp[j]); } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; #define ll long long const ll inf = 1e18L + 5; struct block { int w, s; ll v; }; int main() { int n; cin >> n; vector<block> vec(n); int max_w = 0; for (int i = 0; i < n; i++) { cin >> vec[i].w >> vec[i].s >> vec[i].v; max_w += vec[i].w; } sort(vec.begin(), vec.end(), [](block a, block b) { return a.w + a.s < b.w + b.s; }); // dp[i] max value with total weight i so far vector<ll> dp(max_w + 5); // dp[n-1]=vec[n-1].v; for (int i = 0; i < n; i++) { for (int j = min(vec[i].s, max_w - vec[i].w); j >= 0; j--) { // if(j<=vec[i].s) dp[j + vec[i].w] = max(dp[j + vec[i].w], dp[j] + vec[i].v); } } ll ans = -1; for (int j = max_w; j >= 0; j--) { ans = max(ans, dp[j]); } cout << ans << endl; }
replace
23
27
23
27
TLE
p03183
C++
Runtime Error
// Author : Rifayat Samee (Sanzee) // Problem : // Algorithm: /* */ #include <bits/stdc++.h> using namespace std; typedef long long ll; #define maxval 10005 struct Node { int w; int s; ll v; bool operator<(const Node &b) const { return b.w + b.s > w + s; } }; int main() { // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); int n; scanf("%d", &n); vector<Node> B(n); for (int i = 0; i < n; i++) { scanf("%d %d %d", &B[i].w, &B[i].s, &B[i].v); } sort(B.begin(), B.end()); vector<ll> P(maxval + 1, 0); for (int i = 0; i < n; i++) { vector<ll> dp(maxval + 1); dp = P; for (int w = 0; w <= B[i].s; w++) { dp[w + B[i].w] = max(P[w + B[i].w], P[w] + B[i].v); } P = dp; } ll res = 0; for (int w = 0; w <= maxval; w++) res = max(res, P[w]); printf("%lld\n", res); return 0; }
// Author : Rifayat Samee (Sanzee) // Problem : // Algorithm: /* */ #include <bits/stdc++.h> using namespace std; typedef long long ll; #define maxval 2 * 10005 struct Node { int w; int s; ll v; bool operator<(const Node &b) const { return b.w + b.s > w + s; } }; int main() { // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); int n; scanf("%d", &n); vector<Node> B(n); for (int i = 0; i < n; i++) { scanf("%d %d %d", &B[i].w, &B[i].s, &B[i].v); } sort(B.begin(), B.end()); vector<ll> P(maxval + 1, 0); for (int i = 0; i < n; i++) { vector<ll> dp(maxval + 1); dp = P; for (int w = 0; w <= B[i].s; w++) { dp[w + B[i].w] = max(P[w + B[i].w], P[w] + B[i].v); } P = dp; } ll res = 0; for (int w = 0; w <= maxval; w++) res = max(res, P[w]); printf("%lld\n", res); return 0; }
replace
10
11
10
11
0
p03183
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define NDEBUG #include <cassert> typedef long long ll; typedef long double Double; typedef unsigned long long ull; typedef pair<int, int> ii; typedef pair<ll, ll> llll; typedef pair<double, double> dd; typedef vector<int> vi; typedef vector<vector<int>> vvi; typedef vector<ii> vii; typedef vector<vector<ii>> vvii; typedef vector<ll> vll; typedef vector<vector<ll>> vvll; typedef vector<llll> vllll; typedef vector<bool> vb; typedef vector<string> vs; typedef vector<double> vd; typedef vector<long double> vD; #define sz(a) int((a).size()) #define pb push_back #define eb emplace_back #define FOR(var, from, to) for (int var = (from); var <= (to); ++var) #define rep(var, n) for (int var = 0; var < (n); ++var) #define rep1(var, n) for (int var = 1; var <= (n); ++var) #define repC2(vari, varj, n) \ for (int vari = 0; vari < (n)-1; ++vari) \ for (int varj = vari + 1; varj < (n); ++varj) #define repC3(vari, varj, vark, n) \ for (int vari = 0; vari < (n)-2; ++vari) \ for (int varj = vari + 1; varj < (n)-1; ++varj) \ for (int vark = varj + 1; vark < (n); ++vark) #define ALL(c) (c).begin(), (c).end() #define RALL(c) (c).rbegin(), (c).rend() #define tr(i, c) for (auto i = (c).begin(); i != (c).end(); ++i) #define found(s, e) ((s).find(e) != (s).end()) #define mset(arr, val) memset(arr, val, sizeof(arr)) #define mid(x, y) ((x) + ((y) - (x)) / 2) #define IN(x, a, b) ((a) <= (x) && (x) <= (b)) #define cons make_pair template <class T> inline void amin(T &a, T const &b) { a = min(a, b); } template <class T> inline void amax(T &a, T const &b) { a = max(a, b); } template <typename X, typename T> auto vectors(X x, T a) { return vector<T>(x, a); } template <typename X, typename Y, typename Z, typename... Zs> auto vectors(X x, Y y, Z z, Zs... zs) { auto cont = vectors(y, z, zs...); return vector<decltype(cont)>(x, cont); } ll gcd(ll a, ll b) { while (a) swap(a, b %= a); return b; } const ll MOD = 1000000007LL; ll ADD(ll x, ll y) { return (x + y) % MOD; } ll SUB(ll x, ll y) { return (x - y + MOD) % MOD; } ll MUL(ll x, ll y) { return x * y % MOD; } ll POW(ll x, ll e) { ll v = 1; for (; e; x = MUL(x, x), e >>= 1) if (e & 1) v = MUL(v, x); return v; } ll DIV(ll x, ll y) { /*assert(y%MOD!=0);*/ return MUL(x, POW(y, MOD - 2)); } #define INTSPACE 12 char _buf[INTSPACE * 1000000 + 3]; int loadint() { if (fgets(_buf, INTSPACE + 3, stdin) == NULL) return 0; return atoi(_buf); } int loadvec(vector<int> &v, int N = -1) { if (N == 0) { v.clear(); return 0; } if (N == -1) { N = loadint(); if (N == 0) return 0; } int bufsize = INTSPACE * N + 3; if (fgets(_buf, bufsize, stdin) == NULL) return 0; v.resize(N); int i = 0; bool last = false; for (char *p = &_buf[0];;) { char *q = p; while (*q > ' ') ++q; if (*q == 0x0D || *q == 0x0A) last = true; *q = 0; v[i++] = atoi(p); if (last || i == N) break; p = q + 1; } return i; } void read_cr() { fgets(_buf, 256, stdin); } #define WMAX 10000 ll solve(int N, vi &w, vi &s, vi &v) { vector<pair<int, ii>> x(N); rep(i, N) x[i] = pair<int, ii>(s[i] + w[i], ii(w[i], v[i])); sort(ALL(x)); vector<vll> dp(2, vll(WMAX + 1, -1)); dp[0][0] = 0; rep(i, N) { dp[1 - i % 2].assign(WMAX + 1, -1); int cap = x[i].first, wa = x[i].second.first, va = x[i].second.second; for (int ws = 0; ws <= WMAX; ++ws) { dp[1 - i % 2][ws] = max(dp[1 - i % 2][ws], dp[i % 2][ws]); if (ws + wa <= cap) { dp[1 - i % 2][ws + wa] = max(dp[1 - i % 2][ws], dp[i % 2][ws] + va); } } } ll ans = 0; rep(i, WMAX + 1) ans = max(ans, dp[N % 2][i]); return ans; } int main() { int N; scanf("%d", &N); vi w(N), s(N), v(N); rep(i, N) { scanf("%d%d%d", &w[i], &s[i], &v[i]); } cout << solve(N, w, s, v) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define NDEBUG #include <cassert> typedef long long ll; typedef long double Double; typedef unsigned long long ull; typedef pair<int, int> ii; typedef pair<ll, ll> llll; typedef pair<double, double> dd; typedef vector<int> vi; typedef vector<vector<int>> vvi; typedef vector<ii> vii; typedef vector<vector<ii>> vvii; typedef vector<ll> vll; typedef vector<vector<ll>> vvll; typedef vector<llll> vllll; typedef vector<bool> vb; typedef vector<string> vs; typedef vector<double> vd; typedef vector<long double> vD; #define sz(a) int((a).size()) #define pb push_back #define eb emplace_back #define FOR(var, from, to) for (int var = (from); var <= (to); ++var) #define rep(var, n) for (int var = 0; var < (n); ++var) #define rep1(var, n) for (int var = 1; var <= (n); ++var) #define repC2(vari, varj, n) \ for (int vari = 0; vari < (n)-1; ++vari) \ for (int varj = vari + 1; varj < (n); ++varj) #define repC3(vari, varj, vark, n) \ for (int vari = 0; vari < (n)-2; ++vari) \ for (int varj = vari + 1; varj < (n)-1; ++varj) \ for (int vark = varj + 1; vark < (n); ++vark) #define ALL(c) (c).begin(), (c).end() #define RALL(c) (c).rbegin(), (c).rend() #define tr(i, c) for (auto i = (c).begin(); i != (c).end(); ++i) #define found(s, e) ((s).find(e) != (s).end()) #define mset(arr, val) memset(arr, val, sizeof(arr)) #define mid(x, y) ((x) + ((y) - (x)) / 2) #define IN(x, a, b) ((a) <= (x) && (x) <= (b)) #define cons make_pair template <class T> inline void amin(T &a, T const &b) { a = min(a, b); } template <class T> inline void amax(T &a, T const &b) { a = max(a, b); } template <typename X, typename T> auto vectors(X x, T a) { return vector<T>(x, a); } template <typename X, typename Y, typename Z, typename... Zs> auto vectors(X x, Y y, Z z, Zs... zs) { auto cont = vectors(y, z, zs...); return vector<decltype(cont)>(x, cont); } ll gcd(ll a, ll b) { while (a) swap(a, b %= a); return b; } const ll MOD = 1000000007LL; ll ADD(ll x, ll y) { return (x + y) % MOD; } ll SUB(ll x, ll y) { return (x - y + MOD) % MOD; } ll MUL(ll x, ll y) { return x * y % MOD; } ll POW(ll x, ll e) { ll v = 1; for (; e; x = MUL(x, x), e >>= 1) if (e & 1) v = MUL(v, x); return v; } ll DIV(ll x, ll y) { /*assert(y%MOD!=0);*/ return MUL(x, POW(y, MOD - 2)); } #define INTSPACE 12 char _buf[INTSPACE * 1000000 + 3]; int loadint() { if (fgets(_buf, INTSPACE + 3, stdin) == NULL) return 0; return atoi(_buf); } int loadvec(vector<int> &v, int N = -1) { if (N == 0) { v.clear(); return 0; } if (N == -1) { N = loadint(); if (N == 0) return 0; } int bufsize = INTSPACE * N + 3; if (fgets(_buf, bufsize, stdin) == NULL) return 0; v.resize(N); int i = 0; bool last = false; for (char *p = &_buf[0];;) { char *q = p; while (*q > ' ') ++q; if (*q == 0x0D || *q == 0x0A) last = true; *q = 0; v[i++] = atoi(p); if (last || i == N) break; p = q + 1; } return i; } void read_cr() { fgets(_buf, 256, stdin); } #define WMAX 20000 ll solve(int N, vi &w, vi &s, vi &v) { vector<pair<int, ii>> x(N); rep(i, N) x[i] = pair<int, ii>(s[i] + w[i], ii(w[i], v[i])); sort(ALL(x)); vector<vll> dp(2, vll(WMAX + 1, -1)); dp[0][0] = 0; rep(i, N) { dp[1 - i % 2].assign(WMAX + 1, -1); int cap = x[i].first, wa = x[i].second.first, va = x[i].second.second; for (int ws = 0; ws <= WMAX; ++ws) { dp[1 - i % 2][ws] = max(dp[1 - i % 2][ws], dp[i % 2][ws]); if (ws + wa <= cap) { dp[1 - i % 2][ws + wa] = max(dp[1 - i % 2][ws], dp[i % 2][ws] + va); } } } ll ans = 0; rep(i, WMAX + 1) ans = max(ans, dp[N % 2][i]); return ans; } int main() { int N; scanf("%d", &N); vi w(N), s(N), v(N); rep(i, N) { scanf("%d%d%d", &w[i], &s[i], &v[i]); } cout << solve(N, w, s, v) << endl; return 0; }
replace
122
123
122
123
0
p03183
C++
Runtime Error
// thuanqvbn03 #include <bits/stdc++.h> using namespace std; struct Block { int w, s, v; bool operator<(const Block data) const { return (w + s <= data.w + data.s); } }; int n; Block a[1005]; long long dp[1005][20005]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i].w >> a[i].s >> a[i].v; } sort(a + 1, a + n + 1); int m = a[n].s * 2; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { dp[i][j] = dp[i - 1][j]; if (j >= a[i].w && j - a[i].w <= a[i].s) { dp[i][j] = max(dp[i][j], dp[i - 1][j - a[i].w] + a[i].v); } } } long long ans = 0; for (int i = 1; i <= m; i++) { ans = max(ans, dp[n][i]); } cout << ans; return 0; }
// thuanqvbn03 #include <bits/stdc++.h> using namespace std; struct Block { int w, s, v; bool operator<(const Block data) const { return (w + s != data.w + data.s ? w + s < data.w + data.s : s < data.s); } }; int n; Block a[1005]; long long dp[1005][20005]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i].w >> a[i].s >> a[i].v; } sort(a + 1, a + n + 1); int m = a[n].s * 2; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { dp[i][j] = dp[i - 1][j]; if (j >= a[i].w && j - a[i].w <= a[i].s) { dp[i][j] = max(dp[i][j], dp[i - 1][j - a[i].w] + a[i].v); } } } long long ans = 0; for (int i = 1; i <= m; i++) { ans = max(ans, dp[n][i]); } cout << ans; return 0; }
replace
7
8
7
10
-11
p03183
C++
Runtime Error
#include "bits/stdc++.h" using namespace std; #define FAST \ ios_base::sync_with_stdio(false); \ cin.tie(0); #define LLINF (long long)1e18 // 1234567890987654321 #define INF 1234567890 #define pb push_back #define ins insert #define f first #define s second #define db 0 #define EPS (1e-7) // 0.0000001 the value #define PI (acos(-1)) #define MAXN 300006 #define MAXK 26 #define MAXX 15000006 #define ll long long int #define ld long double #define rep0(kk, l1, l2) for (ll kk = l1; kk < l2; kk++) #define rep1(kk, l1, l2) for (ll kk = l1; kk <= l2; kk++) #define foritr(itr, A) \ for (set<ll>::iterator itr = A.begin(); itr != A.end(); itr++) mt19937 rng(chrono::steady_clock::now() .time_since_epoch() .count()); // can be used by calling rng() or shuffle(A, A+n, rng) #define FOR(ii, ss, ee) for (ll ii = ss; ii < ee; ii++) #define cr(x) cerr << #x << " = " << x << "\n"; #define crA(x, A) cerr << #x << " = " << A[x] << "\n"; #define spacing \ if (db) \ cout << " "; #define mmst(x, v) memset((x), v, sizeof((x))); #define bg(ms) (*ms.begin()) #define ed(ms) (*prev(ms.end(), 1)) #define addedge(a, b, c, v) \ v[(a)].pb(pi((b), (c))); \ v[(b)].pb(pi((a), (c))) #define ph push #define btinpct(x) __builtin_popcountll(x) #define p2(x) (1LL << (x)) #define all(x) (x).begin(), (x).end() #define lbd(x, y) lower_bound(all(x), y) #define ubd(x, y) upper_bound(all(x), y) typedef pair<ll, ll> pi; typedef pair<ll, pi> spi; typedef pair<pi, pi> dpi; inline ll rand(ll x, ll y) { ++y; return (rng() % (y - x)) + x; } // inclusivesss ll n, dp[10006]; struct item { ll w, s, v; bool operator<(const item &e) const { return s + w > e.s + e.w; } } A[1006]; int main() { cin >> n; FOR(i, 0, n) cin >> A[i].w >> A[i].s >> A[i].v; sort(A, A + n); for (ll i = 0; i < n; i++) { ll w = A[i].w, s = A[i].s, v = A[i].v; // cout << s << ' ' << w << ' ' << v << '\n'; for (ll i = 0; i <= s; i++) { dp[i] = max(dp[i], dp[i + w] + v); } } cout << *max_element(dp, dp + 10006) << '\n'; }
#include "bits/stdc++.h" using namespace std; #define FAST \ ios_base::sync_with_stdio(false); \ cin.tie(0); #define LLINF (long long)1e18 // 1234567890987654321 #define INF 1234567890 #define pb push_back #define ins insert #define f first #define s second #define db 0 #define EPS (1e-7) // 0.0000001 the value #define PI (acos(-1)) #define MAXN 300006 #define MAXK 26 #define MAXX 15000006 #define ll long long int #define ld long double #define rep0(kk, l1, l2) for (ll kk = l1; kk < l2; kk++) #define rep1(kk, l1, l2) for (ll kk = l1; kk <= l2; kk++) #define foritr(itr, A) \ for (set<ll>::iterator itr = A.begin(); itr != A.end(); itr++) mt19937 rng(chrono::steady_clock::now() .time_since_epoch() .count()); // can be used by calling rng() or shuffle(A, A+n, rng) #define FOR(ii, ss, ee) for (ll ii = ss; ii < ee; ii++) #define cr(x) cerr << #x << " = " << x << "\n"; #define crA(x, A) cerr << #x << " = " << A[x] << "\n"; #define spacing \ if (db) \ cout << " "; #define mmst(x, v) memset((x), v, sizeof((x))); #define bg(ms) (*ms.begin()) #define ed(ms) (*prev(ms.end(), 1)) #define addedge(a, b, c, v) \ v[(a)].pb(pi((b), (c))); \ v[(b)].pb(pi((a), (c))) #define ph push #define btinpct(x) __builtin_popcountll(x) #define p2(x) (1LL << (x)) #define all(x) (x).begin(), (x).end() #define lbd(x, y) lower_bound(all(x), y) #define ubd(x, y) upper_bound(all(x), y) typedef pair<ll, ll> pi; typedef pair<ll, pi> spi; typedef pair<pi, pi> dpi; inline ll rand(ll x, ll y) { ++y; return (rng() % (y - x)) + x; } // inclusivesss ll n, dp[10006 * 3]; struct item { ll w, s, v; bool operator<(const item &e) const { return s + w > e.s + e.w; } } A[1006]; int main() { cin >> n; FOR(i, 0, n) cin >> A[i].w >> A[i].s >> A[i].v; sort(A, A + n); for (ll i = 0; i < n; i++) { ll w = A[i].w, s = A[i].s, v = A[i].v; // cout << s << ' ' << w << ' ' << v << '\n'; for (ll i = 0; i <= s; i++) { dp[i] = max(dp[i], dp[i + w] + v); } } cout << *max_element(dp, dp + 10006) << '\n'; }
replace
52
53
52
53
0
p03184
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstring> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <vector> #define debug(x) cerr << #x << " = " << x << endl using namespace std; typedef long long ll; typedef long double ld; typedef pair<ll, ll> pll; typedef pair<int, int> pii; const int maxN = 1e5 + 5; const ll INF = 1e18; const ll MOD = 1e9 + 7; ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); } ll sq(ll x) { return (x * x) % MOD; } ll modP(ll a, ll b) { return (!b ? 1 : (sq(modP(a, b / 2)) * (b % 2 ? a : 1)) % MOD); } ll fact[maxN], invfact[maxN]; void initFact() { fact[0] = 1; for (ll i = 1; i < maxN; i++) { fact[i] = (fact[i - 1] * i) % MOD; } invfact[maxN - 1] = modP(fact[maxN - 1], MOD - 2); for (ll i = maxN - 2; i > -1; i--) { invfact[i] = (invfact[i + 1] * (i + 1)) % MOD; } } ll choose(ll n, ll k) { return (((fact[n] * invfact[k]) % MOD) * invfact[n - k]) % MOD; } ll dp[maxN]; pll P[maxN]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); initFact(); ll h, w, n; cin >> h >> w >> n; for (ll i = 0; i < n; i++) { cin >> P[i].first >> P[i].second; } P[n] = {h, w}; sort(P, P + n + 1); ll x = P[0].first; ll y = P[0].second; dp[0] = choose(x + y - 2, max(x - 1, y - 1)); for (ll i = 0; i <= n; i++) { ll x = P[i].first; ll y = P[i].second; dp[i] = choose(x + y - 2, max(x - 1, y - 1)); for (ll j = 0; j < i; j++) { if (P[i].second < P[j].second) continue; ll x = P[i].first - P[j].first; ll y = P[i].second - P[j].second; dp[i] -= dp[j] * choose(x + y, max(x, y)); dp[i] = (dp[i] + MOD) % MOD; } } while (dp[n] < 0) dp[n] += MOD; cout << dp[n] % MOD << endl; return 0; }
#include <algorithm> #include <cmath> #include <cstring> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <vector> #define debug(x) cerr << #x << " = " << x << endl using namespace std; typedef long long ll; typedef long double ld; typedef pair<ll, ll> pll; typedef pair<int, int> pii; const int maxN = 2e5 + 5; const ll INF = 1e18; const ll MOD = 1e9 + 7; ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); } ll sq(ll x) { return (x * x) % MOD; } ll modP(ll a, ll b) { return (!b ? 1 : (sq(modP(a, b / 2)) * (b % 2 ? a : 1)) % MOD); } ll fact[maxN], invfact[maxN]; void initFact() { fact[0] = 1; for (ll i = 1; i < maxN; i++) { fact[i] = (fact[i - 1] * i) % MOD; } invfact[maxN - 1] = modP(fact[maxN - 1], MOD - 2); for (ll i = maxN - 2; i > -1; i--) { invfact[i] = (invfact[i + 1] * (i + 1)) % MOD; } } ll choose(ll n, ll k) { return (((fact[n] * invfact[k]) % MOD) * invfact[n - k]) % MOD; } ll dp[maxN]; pll P[maxN]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); initFact(); ll h, w, n; cin >> h >> w >> n; for (ll i = 0; i < n; i++) { cin >> P[i].first >> P[i].second; } P[n] = {h, w}; sort(P, P + n + 1); ll x = P[0].first; ll y = P[0].second; dp[0] = choose(x + y - 2, max(x - 1, y - 1)); for (ll i = 0; i <= n; i++) { ll x = P[i].first; ll y = P[i].second; dp[i] = choose(x + y - 2, max(x - 1, y - 1)); for (ll j = 0; j < i; j++) { if (P[i].second < P[j].second) continue; ll x = P[i].first - P[j].first; ll y = P[i].second - P[j].second; dp[i] -= dp[j] * choose(x + y, max(x, y)); dp[i] = (dp[i] + MOD) % MOD; } } while (dp[n] < 0) dp[n] += MOD; cout << dp[n] % MOD << endl; return 0; }
replace
18
19
18
19
0
p03184
C++
Runtime Error
#include <algorithm> #include <bitset> #include <cmath> #include <complex> #include <cstring> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <regex> #include <set> #include <stack> #include <string> #include <vector> const int MOD = 1e9 + 7; const int iINF = 2147483647 / 2; const long long int llINF = 9223372036854775807 / 2; using namespace std; using ll = long long int; using vl = vector<ll>; using vvl = vector<vector<ll>>; using vvvl = vector<vector<vector<ll>>>; typedef pair<ll, ll> pll; bool paircomp(const pll &a, const pll &b) { if (a.first == b.first) return a.second < b.second; return a.first < b.first; } #define REP(i, n) for (ll i = 0; i < (n); i++) #define RREP(i, n) for (ll i = (n)-1; i >= 0; i--) #define FOR(i, a, b) for (ll i = (a); i < (b); i++) #define AUTO(i, m) for (auto &i : m) #define ALL(a) (a).begin(), (a).end() #define MAX(vec) *std::max_element(vec.begin(), vec.end()) #define MIN(vec) *std::min_element(vec.begin(), vec.end()) #define ARGMAX(vec) \ std::distance(vec.begin(), std::max_element(vec.begin(), vec.end())) #define ARGMIN(vec) \ std::distance(vec.begin(), std::min_element(vec.begin(), vec.end())) #define REV(T) greater<T>() #define PQ(T) priority_queue<T, vector<T>, greater<T>> #define VVL(a, b, c) vector<vector<ll>>(a, vector<ll>(b, c)) #define VV(T, a, b, c) vector<vector<T>>(a, vector<T>(b, c)) #define VVVL(a, b, c, d) \ vector<vector<vector<ll>>>(a, vector<vector<ll>>(b, vector<ll>(c, d))) #define VVV(T, a, b, c, d) \ vector<vector<vector<T>>>(a, vector<vector<T>>(b, vector<T>(c, d))) #define SP(a) setprecision(a) #define SQRT(a) sqrt((long double)(a)) #define DPOW(a, b) pow((long double)(a), (long double)(b)) #define UNIQUE(vec) \ do { \ sort(ALL((vec))); \ (vec).erase(std::unique(ALL((vec))), (vec).end()); \ } while (0) ll MODPOW(ll n, ll m) { if (m == 0) { return 1; } else if (m % 2 == 0) { ll tmp = MODPOW(n, m / 2); return (tmp * tmp) % MOD; } else { return (n * MODPOW(n, m - 1)) % MOD; } } ll MODDIV(ll a, ll b) { return a * MODPOW(b, MOD - 2) % MOD; } int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll H, W, N; cin >> H >> W >> N; vl r(N + 2), c(N + 2); r[0] = 1; c[0] = 1; REP(i, N) cin >> r[i + 1] >> c[i + 1]; r[N + 1] = H; c[N + 1] = W; ll start = 0; ll goal = N + 1; vvl reach(N + 2); vl fact(H + W + 1, 0); fact[0] = 1; REP(i, H + W + 1) fact[i + 1] = ((i + 1) * fact[i]) % MOD; REP(i, N + 2) REP(j, N + 2) { if (i == j) continue; if (r[i] <= r[j] && c[i] <= c[j]) reach[i].push_back(j); } vector<pll> order; REP(i, N + 2) order.emplace_back(r[i] + c[i], i); sort(ALL(order)); vvl dp = VVL(N + 2, 2, 0); dp[0][0] = 1; REP(i, N + 2) { ll u = order[i].second; AUTO(v, reach[u]) { ll rdif = r[v] - r[u]; ll cdif = c[v] - c[u]; ll mag = MODDIV(fact[rdif + cdif], (fact[rdif] * fact[cdif]) % MOD); REP(p, 2) { ll base = dp[u][p]; if (base == 0) continue; dp[v][1 - p] = (dp[v][1 - p] + (dp[u][p] * mag) % MOD) % MOD; } } } ll ans = (MOD + dp[goal][1] - dp[goal][0]) % MOD; cout << ans << endl; return 0; }
#include <algorithm> #include <bitset> #include <cmath> #include <complex> #include <cstring> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <regex> #include <set> #include <stack> #include <string> #include <vector> const int MOD = 1e9 + 7; const int iINF = 2147483647 / 2; const long long int llINF = 9223372036854775807 / 2; using namespace std; using ll = long long int; using vl = vector<ll>; using vvl = vector<vector<ll>>; using vvvl = vector<vector<vector<ll>>>; typedef pair<ll, ll> pll; bool paircomp(const pll &a, const pll &b) { if (a.first == b.first) return a.second < b.second; return a.first < b.first; } #define REP(i, n) for (ll i = 0; i < (n); i++) #define RREP(i, n) for (ll i = (n)-1; i >= 0; i--) #define FOR(i, a, b) for (ll i = (a); i < (b); i++) #define AUTO(i, m) for (auto &i : m) #define ALL(a) (a).begin(), (a).end() #define MAX(vec) *std::max_element(vec.begin(), vec.end()) #define MIN(vec) *std::min_element(vec.begin(), vec.end()) #define ARGMAX(vec) \ std::distance(vec.begin(), std::max_element(vec.begin(), vec.end())) #define ARGMIN(vec) \ std::distance(vec.begin(), std::min_element(vec.begin(), vec.end())) #define REV(T) greater<T>() #define PQ(T) priority_queue<T, vector<T>, greater<T>> #define VVL(a, b, c) vector<vector<ll>>(a, vector<ll>(b, c)) #define VV(T, a, b, c) vector<vector<T>>(a, vector<T>(b, c)) #define VVVL(a, b, c, d) \ vector<vector<vector<ll>>>(a, vector<vector<ll>>(b, vector<ll>(c, d))) #define VVV(T, a, b, c, d) \ vector<vector<vector<T>>>(a, vector<vector<T>>(b, vector<T>(c, d))) #define SP(a) setprecision(a) #define SQRT(a) sqrt((long double)(a)) #define DPOW(a, b) pow((long double)(a), (long double)(b)) #define UNIQUE(vec) \ do { \ sort(ALL((vec))); \ (vec).erase(std::unique(ALL((vec))), (vec).end()); \ } while (0) ll MODPOW(ll n, ll m) { if (m == 0) { return 1; } else if (m % 2 == 0) { ll tmp = MODPOW(n, m / 2); return (tmp * tmp) % MOD; } else { return (n * MODPOW(n, m - 1)) % MOD; } } ll MODDIV(ll a, ll b) { return a * MODPOW(b, MOD - 2) % MOD; } int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll H, W, N; cin >> H >> W >> N; vl r(N + 2), c(N + 2); r[0] = 1; c[0] = 1; REP(i, N) cin >> r[i + 1] >> c[i + 1]; r[N + 1] = H; c[N + 1] = W; ll start = 0; ll goal = N + 1; vvl reach(N + 2); vl fact(H + W + 1, 0); fact[0] = 1; REP(i, H + W) fact[i + 1] = ((i + 1) * fact[i]) % MOD; REP(i, N + 2) REP(j, N + 2) { if (i == j) continue; if (r[i] <= r[j] && c[i] <= c[j]) reach[i].push_back(j); } vector<pll> order; REP(i, N + 2) order.emplace_back(r[i] + c[i], i); sort(ALL(order)); vvl dp = VVL(N + 2, 2, 0); dp[0][0] = 1; REP(i, N + 2) { ll u = order[i].second; AUTO(v, reach[u]) { ll rdif = r[v] - r[u]; ll cdif = c[v] - c[u]; ll mag = MODDIV(fact[rdif + cdif], (fact[rdif] * fact[cdif]) % MOD); REP(p, 2) { ll base = dp[u][p]; if (base == 0) continue; dp[v][1 - p] = (dp[v][1 - p] + (dp[u][p] * mag) % MOD) % MOD; } } } ll ans = (MOD + dp[goal][1] - dp[goal][0]) % MOD; cout << ans << endl; return 0; }
replace
94
95
94
95
0
p03184
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long int #define lld long double #define pb push_back #define pii pair<ll, ll> #define mi map<ll, ll> #define vec vector<ll> #define all(a) (a).begin(), (a).end() #define F first #define S second #define mod 1000000007 #define nmod -1000000007 #define rep(i, a, b) for (ll i = a; i < b; i++) #define repi(i, a, b) for (ll i = a; i <= b; i++) #define per(i, a, b) for (ll i = a; i >= b; i--) #define mp make_pair #define mit map<ll, ll>::iterator #define sit set<ll>::iterator #define pit pair<ll, ll>::iterator #define xxx exit(0); #define tr(container, it) \ for (__typeof(container.begin()) it = container.begin(); \ it != container.end(); it++) using namespace std; ll xo(ll x, ll y) { return (x | y) & (~x | ~y); } ll bin_Expo(ll x, ll n) { if (x == 0) return 0; if (n == 0) return 1; else if (n % 2 == 0) // n is even return bin_Expo(x * x, n / 2); else // n is odd return x * bin_Expo(x * x, (n - 1) / 2); } ll mod_Expo(ll x, ll n, ll M) { if (x == 0) return 0; if (n == 0) return 1; else if (n % 2 == 0) // n is even return mod_Expo((x * x) % M, n / 2, M); else // n is odd return (x * mod_Expo((x * x) % M, (n - 1) / 2, M)) % M; } bool prime_check(ll x) { bool prime = (x >= 2); for (ll i = 2; i * i <= x; i++) { if (x % i == 0) { prime = false; break; } } return prime; } ll logg(ll base, ll x) { return (ll)(log(x) / log(base)); } ll h, w, n, dp[100005], f[200005], fc[200005]; ll ways(ll x, ll y) { if (x < 0 || y < 0) return 0; return (((f[x + y] * fc[x]) % mod) * fc[y]) % mod; } bool cc(pii &a, pii &b) { return (a.F + a.S) < (b.F + b.S); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int TESTS = 1; // cin>>TESTS; while (TESTS--) { ll i, l, j, x1, y1, x, y, q, h; cin >> h >> w >> n; vector<pii> z(n + 1); rep(i, 0, n) { cin >> x1 >> y1; z[i] = {x1, y1}; } z[n] = {mod, mod}; sort(all(z), cc); z[n] = {h, w}; f[0] = 1; fc[0] = 1; dp[0] = 0; repi(i, 1, 200004) { f[i] = (f[i - 1] * i) % mod; fc[i] = mod_Expo(f[i], mod - 2, mod); dp[i] = 0; } repi(i, 0, n) { dp[i] = ways(z[i].F - 1, z[i].S - 1); rep(j, 0, i) { if (z[i].F >= z[j].F && z[i].S >= z[j].S) dp[i] = (dp[i] - ((ways(z[i].F - z[j].F, z[i].S - z[j].S) * dp[j]) % mod) + mod) % mod; } } cout << dp[n]; } return 0; }
#include <bits/stdc++.h> #define ll long long int #define lld long double #define pb push_back #define pii pair<ll, ll> #define mi map<ll, ll> #define vec vector<ll> #define all(a) (a).begin(), (a).end() #define F first #define S second #define mod 1000000007 #define nmod -1000000007 #define rep(i, a, b) for (ll i = a; i < b; i++) #define repi(i, a, b) for (ll i = a; i <= b; i++) #define per(i, a, b) for (ll i = a; i >= b; i--) #define mp make_pair #define mit map<ll, ll>::iterator #define sit set<ll>::iterator #define pit pair<ll, ll>::iterator #define xxx exit(0); #define tr(container, it) \ for (__typeof(container.begin()) it = container.begin(); \ it != container.end(); it++) using namespace std; ll xo(ll x, ll y) { return (x | y) & (~x | ~y); } ll bin_Expo(ll x, ll n) { if (x == 0) return 0; if (n == 0) return 1; else if (n % 2 == 0) // n is even return bin_Expo(x * x, n / 2); else // n is odd return x * bin_Expo(x * x, (n - 1) / 2); } ll mod_Expo(ll x, ll n, ll M) { if (x == 0) return 0; if (n == 0) return 1; else if (n % 2 == 0) // n is even return mod_Expo((x * x) % M, n / 2, M); else // n is odd return (x * mod_Expo((x * x) % M, (n - 1) / 2, M)) % M; } bool prime_check(ll x) { bool prime = (x >= 2); for (ll i = 2; i * i <= x; i++) { if (x % i == 0) { prime = false; break; } } return prime; } ll logg(ll base, ll x) { return (ll)(log(x) / log(base)); } ll h, w, n, dp[200005], f[200005], fc[200005]; ll ways(ll x, ll y) { if (x < 0 || y < 0) return 0; return (((f[x + y] * fc[x]) % mod) * fc[y]) % mod; } bool cc(pii &a, pii &b) { return (a.F + a.S) < (b.F + b.S); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int TESTS = 1; // cin>>TESTS; while (TESTS--) { ll i, l, j, x1, y1, x, y, q, h; cin >> h >> w >> n; vector<pii> z(n + 1); rep(i, 0, n) { cin >> x1 >> y1; z[i] = {x1, y1}; } z[n] = {mod, mod}; sort(all(z), cc); z[n] = {h, w}; f[0] = 1; fc[0] = 1; dp[0] = 0; repi(i, 1, 200004) { f[i] = (f[i - 1] * i) % mod; fc[i] = mod_Expo(f[i], mod - 2, mod); dp[i] = 0; } repi(i, 0, n) { dp[i] = ways(z[i].F - 1, z[i].S - 1); rep(j, 0, i) { if (z[i].F >= z[j].F && z[i].S >= z[j].S) dp[i] = (dp[i] - ((ways(z[i].F - z[j].F, z[i].S - z[j].S) * dp[j]) % mod) + mod) % mod; } } cout << dp[n]; } return 0; }
replace
56
57
56
57
0
p03184
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (ll i = 0; i < n; ++i) using namespace std; typedef long long ll; typedef pair<ll, ll> P; const ll INF = 1e18; // auto mod int // https://youtu.be/L8grWxBlIZ4?t=9858 // https://youtu.be/ERZuLAxZffQ?t=4807 : optimize // https://youtu.be/8uowVvQ_-Mo?t=1329 : division const int mod = 1000000007; struct mint { ll x; // typedef long long ll; mint(ll x = 0) : x((x % mod + mod) % mod) {} mint operator-() const { return mint(-x); } mint &operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint &operator-=(const mint a) { if ((x += mod - a.x) >= mod) x -= mod; return *this; } mint &operator*=(const mint a) { (x *= a.x) %= mod; return *this; } mint operator+(const mint a) const { return mint(*this) += a; } mint operator-(const mint a) const { return mint(*this) -= a; } mint operator*(const mint a) const { return mint(*this) *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime mod mint inv() const { return pow(mod - 2); } mint &operator/=(const mint a) { return *this *= a.inv(); } mint operator/(const mint a) const { return mint(*this) /= a; } }; istream &operator>>(istream &is, const mint &a) { return is >> a.x; } ostream &operator<<(ostream &os, const mint &a) { return os << a.x; } // combination mod prime // https://www.youtube.com/watch?v=8uowVvQ_-Mo&feature=youtu.be&t=1619 struct combination { vector<mint> fact, ifact; combination(int n) : fact(n + 1), ifact(n + 1) { assert(n < mod); fact[0] = 1; for (int i = 1; i <= n; ++i) fact[i] = fact[i - 1] * i; ifact[n] = fact[n].inv(); for (int i = n; i >= 1; --i) ifact[i - 1] = ifact[i] * i; } mint operator()(int n, int k) { if (k < 0 || k > n) return 0; return fact[n] * ifact[k] * ifact[n - k]; } } comb(100005); // nPk mint f(int n, int k) { if (n < 0) return 0; // nPk = nCk * k! mint res = comb(n, k); res *= comb.fact[k]; return res; } // 組み合わせの数nCrを計算 int calcNumOfCombination(int n, int r) { int num = 1; for (int i = 1; i <= r; i++) { num = num * (n - i + 1) / i; } return num; } int main() { ll h, w, n; cin >> h >> w >> n; vector<P> g(n); rep(i, n) { ll r1, c1; cin >> r1 >> c1; r1--; c1--; g[i] = make_pair(r1, c1); } sort(g.begin(), g.end()); mint dp[n + 5]; // mint test=comb(6,2); // cout<<test; rep(i, n) { // cout<<"i"<<i<<endl; ll r1 = g[i].first; ll c1 = g[i].second; // cout<<"r"<<r1<<"c"<<c1<<endl; mint now = comb(r1 + c1, r1); // cout<<now<<endl; rep(j, n) { ll r2 = g[j].first; ll c2 = g[j].second; if (r2 > r1 or c2 > c1) continue; mint temp = dp[j] * comb((r1 - r2 + c1 - c2), r1 - r2); now -= temp; } // cout<<now<<endl; dp[i] = now; } mint ans = comb(h + w - 2, w - 1); // cout<<ans<<endl; rep(i, n) { ll hnow = h - 1 - g[i].first; ll cnow = w - 1 - g[i].second; mint temp = comb(hnow + cnow, hnow); temp *= dp[i]; ans -= temp; } cout << ans << endl; }
#include <bits/stdc++.h> #define rep(i, n) for (ll i = 0; i < n; ++i) using namespace std; typedef long long ll; typedef pair<ll, ll> P; const ll INF = 1e18; // auto mod int // https://youtu.be/L8grWxBlIZ4?t=9858 // https://youtu.be/ERZuLAxZffQ?t=4807 : optimize // https://youtu.be/8uowVvQ_-Mo?t=1329 : division const int mod = 1000000007; struct mint { ll x; // typedef long long ll; mint(ll x = 0) : x((x % mod + mod) % mod) {} mint operator-() const { return mint(-x); } mint &operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint &operator-=(const mint a) { if ((x += mod - a.x) >= mod) x -= mod; return *this; } mint &operator*=(const mint a) { (x *= a.x) %= mod; return *this; } mint operator+(const mint a) const { return mint(*this) += a; } mint operator-(const mint a) const { return mint(*this) -= a; } mint operator*(const mint a) const { return mint(*this) *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime mod mint inv() const { return pow(mod - 2); } mint &operator/=(const mint a) { return *this *= a.inv(); } mint operator/(const mint a) const { return mint(*this) /= a; } }; istream &operator>>(istream &is, const mint &a) { return is >> a.x; } ostream &operator<<(ostream &os, const mint &a) { return os << a.x; } // combination mod prime // https://www.youtube.com/watch?v=8uowVvQ_-Mo&feature=youtu.be&t=1619 struct combination { vector<mint> fact, ifact; combination(int n) : fact(n + 1), ifact(n + 1) { assert(n < mod); fact[0] = 1; for (int i = 1; i <= n; ++i) fact[i] = fact[i - 1] * i; ifact[n] = fact[n].inv(); for (int i = n; i >= 1; --i) ifact[i - 1] = ifact[i] * i; } mint operator()(int n, int k) { if (k < 0 || k > n) return 0; return fact[n] * ifact[k] * ifact[n - k]; } } comb(200005); // nPk mint f(int n, int k) { if (n < 0) return 0; // nPk = nCk * k! mint res = comb(n, k); res *= comb.fact[k]; return res; } // 組み合わせの数nCrを計算 int calcNumOfCombination(int n, int r) { int num = 1; for (int i = 1; i <= r; i++) { num = num * (n - i + 1) / i; } return num; } int main() { ll h, w, n; cin >> h >> w >> n; vector<P> g(n); rep(i, n) { ll r1, c1; cin >> r1 >> c1; r1--; c1--; g[i] = make_pair(r1, c1); } sort(g.begin(), g.end()); mint dp[n + 5]; // mint test=comb(6,2); // cout<<test; rep(i, n) { // cout<<"i"<<i<<endl; ll r1 = g[i].first; ll c1 = g[i].second; // cout<<"r"<<r1<<"c"<<c1<<endl; mint now = comb(r1 + c1, r1); // cout<<now<<endl; rep(j, n) { ll r2 = g[j].first; ll c2 = g[j].second; if (r2 > r1 or c2 > c1) continue; mint temp = dp[j] * comb((r1 - r2 + c1 - c2), r1 - r2); now -= temp; } // cout<<now<<endl; dp[i] = now; } mint ans = comb(h + w - 2, w - 1); // cout<<ans<<endl; rep(i, n) { ll hnow = h - 1 - g[i].first; ll cnow = w - 1 - g[i].second; mint temp = comb(hnow + cnow, hnow); temp *= dp[i]; ans -= temp; } cout << ans << endl; }
replace
71
72
71
72
0
p03184
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long #define pb push_back #define mp make_pair using namespace std; const ll mod = 1e9 + 7; const int maxv = 200100; const int maxn = 3100; ll fact[maxv], ifact[maxv]; ll power(ll x, ll y) { if (y == 0) return 1LL; else if (y == 1) return 1LL * x % mod; else { if (y % 2 == 0) { ll half = power(x, y / 2); return (half * half) % mod; } else return (x * power(x, y - 1)) % mod; } } void preprocess() { fact[0] = 1LL; ifact[0] = power(fact[0], mod - 2); for (ll i = 1LL; i < maxv; i++) { fact[i] = fact[i - 1] * i; fact[i] %= mod; ifact[i] = power(fact[i], mod - 2); } } ll ncr(ll x, ll y) { return 1LL * ((fact[x] * ifact[y]) % mod * ifact[x - y]) % mod; } int n, m, k; vector<pair<int, int>> points; ll cntunique[maxn]; int main() { preprocess(); cin >> n >> m >> k; int x, y; for (int i = 0; i < k; i++) { cin >> x >> y; points.pb(mp(x, y)); } sort(points.begin(), points.end()); ll result = ncr(n + m - 2, n - 1); for (int i = 0; i < k; i++) { cntunique[i] = ncr(points[i].first + points[i].second - 2, points[i].first - 1); for (int j = 0; j < k; j++) { if (points[j].first <= points[i].first && points[j].second <= points[j].second && i != j) { int diffx = points[i].first - points[j].first; int diffy = points[i].second - points[j].second; cntunique[i] = (cntunique[i] - (cntunique[j] * ncr(diffx + diffy, diffy)) % mod + mod) % mod; cntunique[i] %= mod; } } int diffx = n - points[i].first; int diffy = m - points[i].second; result = (result - (cntunique[i] * ncr(diffx + diffy, diffy)) % mod + mod) % mod; result %= mod; } cout << result << "\n"; return 0; }
#include <bits/stdc++.h> #define ll long long #define pb push_back #define mp make_pair using namespace std; const ll mod = 1e9 + 7; const int maxv = 200100; const int maxn = 3100; ll fact[maxv], ifact[maxv]; ll power(ll x, ll y) { if (y == 0) return 1LL; else if (y == 1) return 1LL * x % mod; else { if (y % 2 == 0) { ll half = power(x, y / 2); return (half * half) % mod; } else return (x * power(x, y - 1)) % mod; } } void preprocess() { fact[0] = 1LL; ifact[0] = power(fact[0], mod - 2); for (ll i = 1LL; i < maxv; i++) { fact[i] = fact[i - 1] * i; fact[i] %= mod; ifact[i] = power(fact[i], mod - 2); } } ll ncr(ll x, ll y) { return 1LL * ((fact[x] * ifact[y]) % mod * ifact[x - y]) % mod; } int n, m, k; vector<pair<int, int>> points; ll cntunique[maxn]; int main() { preprocess(); cin >> n >> m >> k; int x, y; for (int i = 0; i < k; i++) { cin >> x >> y; points.pb(mp(x, y)); } sort(points.begin(), points.end()); ll result = ncr(n + m - 2, n - 1); for (int i = 0; i < k; i++) { cntunique[i] = ncr(points[i].first + points[i].second - 2, points[i].first - 1); for (int j = 0; j < k; j++) { if (points[j].first <= points[i].first && points[j].second <= points[i].second && i != j) { int diffx = points[i].first - points[j].first; int diffy = points[i].second - points[j].second; cntunique[i] = (cntunique[i] - (cntunique[j] * ncr(diffx + diffy, diffy)) % mod + mod) % mod; cntunique[i] %= mod; } } int diffx = n - points[i].first; int diffy = m - points[i].second; result = (result - (cntunique[i] * ncr(diffx + diffy, diffy)) % mod + mod) % mod; result %= mod; } cout << result << "\n"; return 0; }
replace
58
59
58
59
0
p03184
C++
Time Limit Exceeded
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cmath> #include <cstdint> #include <cstdio> #include <cstring> #include <deque> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <tuple> #include <utility> #include <vector> using namespace std; using i64 = int64_t; using i32 = int32_t; template <class T, class U> void init_n(vector<T> &v, size_t n, U x) { v = vector<T>(n, x); } template <class T> void init_n(vector<T> &v, size_t n) { init_n(v, n, T()); } template <class T> void read_n(vector<T> &v, size_t n, size_t o = 0) { v = vector<T>(n + o); for (size_t i = o; i < n + o; ++i) cin >> v[i]; } template <class T> void read_n(T a[], size_t n, size_t o = 0) { for (size_t i = o; i < n + o; ++i) cin >> a[i]; } template <class T> T gabs(const T &x) { return max(x, -x); } #define abs gabs const i64 mod = 1e9 + 7; i64 fact_memo[200001]; i64 fact(i64 x) { if (x == 0) return 1; if (fact_memo[x]) return fact_memo[x]; return fact_memo[x] = x * fact(x - 1) % mod; } i64 ipow(i64 a, i64 b) { return (b & 1 ? a : 1) * (b > 1 ? ipow(a * a % mod, b >> 1) : 1) % mod; } i64 inv(i64 x) { return ipow(x, mod - 2); } i64 fact_inv(i64 x) { return inv(fact(x)); } i64 perm(i64 a, i64 b) { return fact(a) * fact_inv(a - b) % mod; } i64 conb(i64 a, i64 b) { return fact(a) * fact_inv(b) % mod * fact_inv(a - b) % mod; } i64 h, w, n; using P = pair<i64, i64>; vector<P> ps; int main() { cin >> h >> w >> n; init_n(ps, n + 1); for (i64 i = 0; i < n; ++i) { cin >> ps[i].first >> ps[i].second; } ps[n] = {h, w}; sort(begin(ps), end(ps)); vector<i64> dp(n + 1); for (i64 i = 0; i <= n; ++i) { i64 r, c; tie(r, c) = ps[i]; dp[i] = conb(r + c - 2, r - 1); for (i64 j = 0; j < i; ++j) { i64 r2, c2; tie(r2, c2) = ps[j]; if (r2 <= r && c2 <= c) { dp[i] += mod - dp[j] * conb(r - r2 + c - c2, c - c2) % mod; } } dp[i] %= mod; // fprintf(stderr, "%lld: (%lld, %lld)\t%lld\n", i, r, c, dp[i]); } cout << dp[n] << endl; return 0; }
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cmath> #include <cstdint> #include <cstdio> #include <cstring> #include <deque> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <tuple> #include <utility> #include <vector> using namespace std; using i64 = int64_t; using i32 = int32_t; template <class T, class U> void init_n(vector<T> &v, size_t n, U x) { v = vector<T>(n, x); } template <class T> void init_n(vector<T> &v, size_t n) { init_n(v, n, T()); } template <class T> void read_n(vector<T> &v, size_t n, size_t o = 0) { v = vector<T>(n + o); for (size_t i = o; i < n + o; ++i) cin >> v[i]; } template <class T> void read_n(T a[], size_t n, size_t o = 0) { for (size_t i = o; i < n + o; ++i) cin >> a[i]; } template <class T> T gabs(const T &x) { return max(x, -x); } #define abs gabs const i64 mod = 1e9 + 7; i64 fact_memo[200001]; i64 fact(i64 x) { if (x == 0) return 1; if (fact_memo[x]) return fact_memo[x]; return fact_memo[x] = x * fact(x - 1) % mod; } i64 ipow(i64 a, i64 b) { return (b & 1 ? a : 1) * (b > 1 ? ipow(a * a % mod, b >> 1) : 1) % mod; } i64 inv(i64 x) { return ipow(x, mod - 2); } i64 fact_inv_memo[200001]; i64 fact_inv(i64 x) { if (fact_inv_memo[x]) return fact_inv_memo[x]; return fact_inv_memo[x] = inv(fact(x)); } i64 perm(i64 a, i64 b) { return fact(a) * fact_inv(a - b) % mod; } i64 conb(i64 a, i64 b) { return fact(a) * fact_inv(b) % mod * fact_inv(a - b) % mod; } i64 h, w, n; using P = pair<i64, i64>; vector<P> ps; int main() { cin >> h >> w >> n; init_n(ps, n + 1); for (i64 i = 0; i < n; ++i) { cin >> ps[i].first >> ps[i].second; } ps[n] = {h, w}; sort(begin(ps), end(ps)); vector<i64> dp(n + 1); for (i64 i = 0; i <= n; ++i) { i64 r, c; tie(r, c) = ps[i]; dp[i] = conb(r + c - 2, r - 1); for (i64 j = 0; j < i; ++j) { i64 r2, c2; tie(r2, c2) = ps[j]; if (r2 <= r && c2 <= c) { dp[i] += mod - dp[j] * conb(r - r2 + c - c2, c - c2) % mod; } } dp[i] %= mod; // fprintf(stderr, "%lld: (%lld, %lld)\t%lld\n", i, r, c, dp[i]); } cout << dp[n] << endl; return 0; }
replace
51
52
51
57
TLE
p03184
C++
Runtime Error
#include <bits/stdc++.h> #define ll long long using namespace std; const int mod = 1e9 + 7; struct node { ll x, y; }; node w[3030]; ll n, m, t, f[3030][2], ny[200020], jc[200020]; bool cmp(node a1, node b1) { if (a1.x == b1.x) return a1.y < b1.y; return a1.x < b1.x; } ll ksm(ll d, ll z) { ll base = d, res = 1; while (z) { if (z & 1) res = (res * base) % mod; base = (base * base) % mod; z >>= 1; } return res; } ll c(ll n1, ll m1) { return (jc[n1] * ny[n1 - m1] % mod * ny[m1] % mod) % mod; } int main() { jc[0] = ny[0] = 1; for (int i = 1; i <= 200010; i++) jc[i] = (jc[i - 1] * i) % mod; for (int i = 1; i <= 200010; i++) ny[i] = ksm(jc[i], mod - 2); cin >> n >> m >> t; for (int i = 0; i < t; i++) cin >> w[i].x >> w[i].y; w[t].x = w[t].y = 1; w[t + 1].x = n, w[t + 1].y = m; sort(w, w + t + 2, cmp); f[0][0] = 1; for (int i = 0; i <= t + 1; i++) { for (int j = 0; j < i; j++) { ll dx = w[i].x - w[j].x, dy = w[i].y - w[j].y; f[i][0] = (f[i][0] + (f[j][1] * c(dx + dy, dy)) % mod) % mod; f[i][1] = (f[i][1] + (f[j][0] * c(dx + dy, dy)) % mod) % mod; } } cout << (f[t + 1][1] + mod - f[t + 1][0]) % mod << endl; return 0; }
#include <bits/stdc++.h> #define ll long long using namespace std; const int mod = 1e9 + 7; struct node { ll x, y; }; node w[3030]; ll n, m, t, f[3030][2], ny[200020], jc[200020]; bool cmp(node a1, node b1) { if (a1.x == b1.x) return a1.y < b1.y; return a1.x < b1.x; } ll ksm(ll d, ll z) { ll base = d, res = 1; while (z) { if (z & 1) res = (res * base) % mod; base = (base * base) % mod; z >>= 1; } return res; } ll c(ll n1, ll m1) { return (jc[n1] * ny[n1 - m1] % mod * ny[m1] % mod) % mod; } int main() { jc[0] = ny[0] = 1; for (int i = 1; i <= 200010; i++) jc[i] = (jc[i - 1] * i) % mod; for (int i = 1; i <= 200010; i++) ny[i] = ksm(jc[i], mod - 2); cin >> n >> m >> t; for (int i = 0; i < t; i++) cin >> w[i].x >> w[i].y; w[t].x = w[t].y = 1; w[t + 1].x = n, w[t + 1].y = m; sort(w, w + t + 2, cmp); f[0][0] = 1; for (int i = 0; i <= t + 1; i++) { for (int j = 0; j < i; j++) { if (w[i].y >= w[j].y) { ll dx = w[i].x - w[j].x, dy = w[i].y - w[j].y; f[i][0] = (f[i][0] + (f[j][1] * c(dx + dy, dy)) % mod) % mod; f[i][1] = (f[i][1] + (f[j][0] * c(dx + dy, dy)) % mod) % mod; } } } cout << (f[t + 1][1] + mod - f[t + 1][0]) % mod << endl; return 0; }
replace
40
43
40
45
0
p03184
C++
Runtime Error
/* Akshat Sharma @akshat98 */ #include <bits/stdc++.h> #include <iostream> #define IOS ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0) #define mp make_pair #define pb push_back #define F first #define S second #define ll long long #define MOD 1000000007 #define MAX 1000000001 #define vi vector<int> #define vl vector<ll> #define vpi vector<pair<int, int>> #define vpl vector<pair<ll, ll>> #define rep(i, a, b) for (i = a; i < b; i++) #define reqp(i, a, b) for (i = a; i <= b; i++) #define ren for (i = 0; i < n; i++) #define ALL(a) (a).begin(), (a).end() using namespace std; typedef pair<int, int> pii; typedef pair<ll, ll> pll; ll fact[200005]{}, ifact[200005]{}; ll mod(ll a, ll m = MOD) { a %= m; if (a < 0) a += m; return a; } ll power(ll a, ll b, ll m = MOD) { ll res = 1; while (b) { if (b % 2) res = mod(1ll * res * a), b--; else a = mod(1ll * a * a), b /= 2; } return res; } ll ncr(ll n, ll r) { ll res = mod(ifact[n - r] * ifact[r]); res = mod(res * fact[n]); return res; } ll calc(ll x1, ll y1, ll x2, ll y2) { return ncr(x2 - x1 + y2 - y1, x2 - x1); } int main() { #ifndef ONLINE_JUDGE freopen("input_file.txt", "r", stdin); // freopen("output_file.txt", "w", stdout); #endif ll i, j, k, l, n, test_case, m, r, H, W; test_case = 1; // cin>>test_case; fact[0] = fact[1] = ifact[0] = ifact[1] = 1; rep(i, 2, 200005) { fact[i] = mod(1ll * i * fact[i - 1]); ifact[i] = power(fact[i], MOD - 2); } while (test_case--) { cin >> H >> W >> n; vl dp(n); vpi vec(n); ren cin >> vec[i].F >> vec[i].S; sort(ALL(vec), [](const pii &a, const pii &b) { return ((a.F < b.F) || ((a.F == b.F) && (a.S < b.S))); }); ll tans = 0; rep(i, 0, n) { ll temp = 0; rep(j, 0, i) { if ((vec[j].F <= vec[i].F) && (vec[j].S <= vec[i].S)) { temp = mod(temp + mod(dp[j] * calc(vec[j].F, vec[j].S, vec[i].F, vec[i].S))); } } dp[i] = mod(calc(1, 1, vec[i].F, vec[i].S) - temp); tans = mod(tans + mod(dp[i] * calc(vec[i].F, vec[i].S, H, W))); } cout << mod(calc(1, 1, H, W) - tans) << "\n"; } return 0; }
/* Akshat Sharma @akshat98 */ #include <bits/stdc++.h> #include <iostream> #define IOS ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0) #define mp make_pair #define pb push_back #define F first #define S second #define ll long long #define MOD 1000000007 #define MAX 1000000001 #define vi vector<int> #define vl vector<ll> #define vpi vector<pair<int, int>> #define vpl vector<pair<ll, ll>> #define rep(i, a, b) for (i = a; i < b; i++) #define reqp(i, a, b) for (i = a; i <= b; i++) #define ren for (i = 0; i < n; i++) #define ALL(a) (a).begin(), (a).end() using namespace std; typedef pair<int, int> pii; typedef pair<ll, ll> pll; ll fact[200005]{}, ifact[200005]{}; ll mod(ll a, ll m = MOD) { a %= m; if (a < 0) a += m; return a; } ll power(ll a, ll b, ll m = MOD) { ll res = 1; while (b) { if (b % 2) res = mod(1ll * res * a), b--; else a = mod(1ll * a * a), b /= 2; } return res; } ll ncr(ll n, ll r) { ll res = mod(ifact[n - r] * ifact[r]); res = mod(res * fact[n]); return res; } ll calc(ll x1, ll y1, ll x2, ll y2) { return ncr(x2 - x1 + y2 - y1, x2 - x1); } int main() { #ifndef ONLINE_JUDGE // freopen("input_file.txt", "r", stdin); // freopen("output_file.txt", "w", stdout); #endif ll i, j, k, l, n, test_case, m, r, H, W; test_case = 1; // cin>>test_case; fact[0] = fact[1] = ifact[0] = ifact[1] = 1; rep(i, 2, 200005) { fact[i] = mod(1ll * i * fact[i - 1]); ifact[i] = power(fact[i], MOD - 2); } while (test_case--) { cin >> H >> W >> n; vl dp(n); vpi vec(n); ren cin >> vec[i].F >> vec[i].S; sort(ALL(vec), [](const pii &a, const pii &b) { return ((a.F < b.F) || ((a.F == b.F) && (a.S < b.S))); }); ll tans = 0; rep(i, 0, n) { ll temp = 0; rep(j, 0, i) { if ((vec[j].F <= vec[i].F) && (vec[j].S <= vec[i].S)) { temp = mod(temp + mod(dp[j] * calc(vec[j].F, vec[j].S, vec[i].F, vec[i].S))); } } dp[i] = mod(calc(1, 1, vec[i].F, vec[i].S) - temp); tans = mod(tans + mod(dp[i] * calc(vec[i].F, vec[i].S, H, W))); } cout << mod(calc(1, 1, H, W) - tans) << "\n"; } return 0; }
replace
52
53
52
53
-6
terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc
p03184
C++
Runtime Error
#pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include <bits/stdc++.h> using namespace std; #define fo(i, a, b) for (int i = a; i <= b; i++) #define _fo(i, a, b) for (int i = a; i >= b; i--) #define foa(i, a) for (auto &i : a) #define sz(a) ((int)a.size()) #define all(a) begin(a), end(a) #define fi first #define se second #define pb(x) push_back(x) #define mk(x, y) make_pair(x, y) typedef int64_t ll; typedef pair<ll, ll> pll; typedef vector<ll> vl; typedef pair<int, int> pii; typedef vector<int> vi; const int N = 2e3 + 5; const int MAX = 2e5 + 5; const int MOD = 1e9 + 7; const ll INF = INT_MAX; const ll _INF = INT_MIN; pii bcell[N]; ll total[N]; ll fac[MAX], infac[MAX]; ll add(ll a, ll b) { return (a + b) % MOD; } ll sub(ll a, ll b) { return (a - b + MOD) % MOD; } ll mul(ll a, ll b) { return (a * b) % MOD; } ll inv(ll val) { ll curr = 1, temp = MOD - 2; while (temp > 0) { if (temp & 1) curr = mul(curr, val); val = mul(val, val); temp >>= 1; } return curr; } void precompute(ll mx) { fac[0] = 1; fo(i, 1, mx) fac[i] = mul(i, fac[i - 1]); infac[mx] = inv(fac[mx]); _fo(i, mx - 1, 0) infac[i] = mul(i + 1, infac[i + 1]); } ll nCr(int a, int b) { return mul(mul(fac[a], infac[a - b]), infac[b]); } ll solve(int n) { fo(i, 1, n) { ll curr = nCr(bcell[i].fi + bcell[i].se - 2, bcell[i].fi - 1), invalid = 0; _fo(j, i - 1, 1) { if (bcell[i].se >= bcell[j].se) { int dx = bcell[i].fi - bcell[j].fi, dy = bcell[i].se - bcell[j].se; invalid = add(invalid, mul(total[j], nCr(dx + dy, dx))); } } total[i] = sub(curr, invalid); } return total[n]; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int h, w, n; cin >> h >> w >> n; fo(i, 1, n) cin >> bcell[i].fi >> bcell[i].se; precompute(h + w); sort(bcell + 1, bcell + n + 1); bcell[++n] = {h, w}; cout << solve(n); }
#pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include <bits/stdc++.h> using namespace std; #define fo(i, a, b) for (int i = a; i <= b; i++) #define _fo(i, a, b) for (int i = a; i >= b; i--) #define foa(i, a) for (auto &i : a) #define sz(a) ((int)a.size()) #define all(a) begin(a), end(a) #define fi first #define se second #define pb(x) push_back(x) #define mk(x, y) make_pair(x, y) typedef int64_t ll; typedef pair<ll, ll> pll; typedef vector<ll> vl; typedef pair<int, int> pii; typedef vector<int> vi; const int N = 3e3 + 5; const int MAX = 2e5 + 5; const int MOD = 1e9 + 7; const ll INF = INT_MAX; const ll _INF = INT_MIN; pii bcell[N]; ll total[N]; ll fac[MAX], infac[MAX]; ll add(ll a, ll b) { return (a + b) % MOD; } ll sub(ll a, ll b) { return (a - b + MOD) % MOD; } ll mul(ll a, ll b) { return (a * b) % MOD; } ll inv(ll val) { ll curr = 1, temp = MOD - 2; while (temp > 0) { if (temp & 1) curr = mul(curr, val); val = mul(val, val); temp >>= 1; } return curr; } void precompute(ll mx) { fac[0] = 1; fo(i, 1, mx) fac[i] = mul(i, fac[i - 1]); infac[mx] = inv(fac[mx]); _fo(i, mx - 1, 0) infac[i] = mul(i + 1, infac[i + 1]); } ll nCr(int a, int b) { return mul(mul(fac[a], infac[a - b]), infac[b]); } ll solve(int n) { fo(i, 1, n) { ll curr = nCr(bcell[i].fi + bcell[i].se - 2, bcell[i].fi - 1), invalid = 0; _fo(j, i - 1, 1) { if (bcell[i].se >= bcell[j].se) { int dx = bcell[i].fi - bcell[j].fi, dy = bcell[i].se - bcell[j].se; invalid = add(invalid, mul(total[j], nCr(dx + dy, dx))); } } total[i] = sub(curr, invalid); } return total[n]; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int h, w, n; cin >> h >> w >> n; fo(i, 1, n) cin >> bcell[i].fi >> bcell[i].se; precompute(h + w); sort(bcell + 1, bcell + n + 1); bcell[++n] = {h, w}; cout << solve(n); }
replace
24
25
24
25
0
p03184
C++
Runtime Error
// In The Name of Allah #include <bits/stdc++.h> using namespace std; typedef long long ll; #define X first #define Y second const int N = 3e1 + 10; const int M = 2e5 + 10; const ll mod = 1e9 + 7; pair<ll, ll> a[N]; ll dp[N], fact[M], inv[M]; ll Pow(ll a, ll b) { if (b == 0) return 1; ll res = Pow(a, b / 2); res = res * res % mod; if (b % 2) res = res * a % mod; return res; } ll C(ll n, ll m) { if (n < m || m < 0) return 0; return fact[n] * inv[m] % mod * inv[n - m] % mod; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll n, h, w; cin >> h >> w >> n; for (int i = 0; i < n; i++) cin >> a[i].first >> a[i].second; a[n] = {h, w}; sort(a, a + n + 1); fact[0] = 1; for (ll i = 1; i < M; i++) fact[i] = fact[i - 1] * i % mod; for (ll i = 0; i < M; i++) inv[i] = Pow(fact[i], mod - 2); for (int i = 0; i <= n; i++) { dp[i] = C(a[i].X + a[i].Y - 2, a[i].X - 1); for (int j = 0; j < i; j++) dp[i] = (dp[i] - dp[j] * C(a[i].X + a[i].Y - a[j].X - a[j].Y, a[i].X - a[j].X) % mod + mod) % mod; } cout << dp[n]; return 0; }
// In The Name of Allah #include <bits/stdc++.h> using namespace std; typedef long long ll; #define X first #define Y second const int N = 3e3 + 10; const int M = 2e5 + 10; const ll mod = 1e9 + 7; pair<ll, ll> a[N]; ll dp[N], fact[M], inv[M]; ll Pow(ll a, ll b) { if (b == 0) return 1; ll res = Pow(a, b / 2); res = res * res % mod; if (b % 2) res = res * a % mod; return res; } ll C(ll n, ll m) { if (n < m || m < 0) return 0; return fact[n] * inv[m] % mod * inv[n - m] % mod; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll n, h, w; cin >> h >> w >> n; for (int i = 0; i < n; i++) cin >> a[i].first >> a[i].second; a[n] = {h, w}; sort(a, a + n + 1); fact[0] = 1; for (ll i = 1; i < M; i++) fact[i] = fact[i - 1] * i % mod; for (ll i = 0; i < M; i++) inv[i] = Pow(fact[i], mod - 2); for (int i = 0; i <= n; i++) { dp[i] = C(a[i].X + a[i].Y - 2, a[i].X - 1); for (int j = 0; j < i; j++) dp[i] = (dp[i] - dp[j] * C(a[i].X + a[i].Y - a[j].X - a[j].Y, a[i].X - a[j].X) % mod + mod) % mod; } cout << dp[n]; return 0; }
replace
10
11
10
11
0
p03184
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define taskname "CNT" const int MOD = 1e9 + 7; typedef long long LL; const int lim2 = 2e6 + 1; LL fact[lim2], ifact[lim2]; void computeFactorials(int N); LL powerMod(LL a, LL n, LL m); LL combination(int N, int K); LL countPath(int m, int n); const int maxK = 1005; LL F[maxK]; struct TPoint { int x, y; bool operator<(TPoint a) const { return (x + y) < (a.x + a.y); } }; TPoint P[maxK]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int N, M, K; cin >> N >> M >> K; computeFactorials(M + N); for (int i = 1; i <= K; i++) cin >> P[i].x >> P[i].y; P[++K].x = N; P[K].y = M; // P[0].x = P[0].y = 1; sort(P + 1, P + 1 + K); F[1] = countPath(P[1].x, P[1].y); for (int i = 2; i <= K; i++) { F[i] = countPath(P[i].x, P[i].y); for (int j = 1; j < i; j++) { if (P[j].x > P[i].x or P[j].y > P[i].y) continue; F[i] -= F[j] * countPath(P[i].x - P[j].x + 1, P[i].y - P[j].y + 1); F[i] %= MOD; if (F[i] < 0) F[i] += MOD; } } cout << F[K]; } LL powerMod(LL a, LL n, LL m) { if (n == 0) return 1; if (n == 1) return (a % m); LL t = powerMod(a, n / 2, m); t = (t * t) % m; if (n % 2) t = (t * a) % m; return t; } void computeFactorials(int N) { fact[0] = 1; for (int i = 1; i <= N; i++) fact[i] = (i * fact[i - 1]) % MOD; ifact[N] = powerMod(fact[N], MOD - 2, MOD); for (int i = N - 1; i >= 0; i--) ifact[i] = (ifact[i + 1] * (i + 1)) % MOD; } LL combination(int N, int K) { return (((fact[N] * ifact[K]) % MOD) * ifact[N - K]) % MOD; } LL countPath(int m, int n) { return combination(m + n - 2, m - 1); }
#include <bits/stdc++.h> using namespace std; #define taskname "CNT" const int MOD = 1e9 + 7; typedef long long LL; const int lim2 = 2e6 + 1; LL fact[lim2], ifact[lim2]; void computeFactorials(int N); LL powerMod(LL a, LL n, LL m); LL combination(int N, int K); LL countPath(int m, int n); const int maxK = 2e5; LL F[maxK]; struct TPoint { int x, y; bool operator<(TPoint a) const { return (x + y) < (a.x + a.y); } }; TPoint P[maxK]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int N, M, K; cin >> N >> M >> K; computeFactorials(M + N); for (int i = 1; i <= K; i++) cin >> P[i].x >> P[i].y; P[++K].x = N; P[K].y = M; // P[0].x = P[0].y = 1; sort(P + 1, P + 1 + K); F[1] = countPath(P[1].x, P[1].y); for (int i = 2; i <= K; i++) { F[i] = countPath(P[i].x, P[i].y); for (int j = 1; j < i; j++) { if (P[j].x > P[i].x or P[j].y > P[i].y) continue; F[i] -= F[j] * countPath(P[i].x - P[j].x + 1, P[i].y - P[j].y + 1); F[i] %= MOD; if (F[i] < 0) F[i] += MOD; } } cout << F[K]; } LL powerMod(LL a, LL n, LL m) { if (n == 0) return 1; if (n == 1) return (a % m); LL t = powerMod(a, n / 2, m); t = (t * t) % m; if (n % 2) t = (t * a) % m; return t; } void computeFactorials(int N) { fact[0] = 1; for (int i = 1; i <= N; i++) fact[i] = (i * fact[i - 1]) % MOD; ifact[N] = powerMod(fact[N], MOD - 2, MOD); for (int i = N - 1; i >= 0; i--) ifact[i] = (ifact[i + 1] * (i + 1)) % MOD; } LL combination(int N, int K) { return (((fact[N] * ifact[K]) % MOD) * ifact[N - K]) % MOD; } LL countPath(int m, int n) { return combination(m + n - 2, m - 1); }
replace
14
15
14
15
0
p03184
C++
Runtime Error
#include "bits/stdc++.h" #define int long long using namespace std; const int mod = 1e9 + 7; int qpow(int a, int power) { if (power == 0) { return 1; } int b = qpow(a, power / 2); if (power % 2 == 0) { return (b * b) % mod; } else { return (((b * b) % mod) * a) % mod; } } int inv(int a) { return qpow(a, mod - 2); } vector<int> f(2e5 + 10), inv_f(2e5 + 10); int c(int n, int k) { return f[n] * inv_f[k] % mod * inv_f[n - k] % mod; } int32_t main() { ios_base::sync_with_stdio(0); f[0] = 1; for (int i = 1; i < f.size(); i++) { f[i] = i * f[i - 1] % mod; } for (int i = 0; i < f.size(); i++) { inv_f[i] = inv(f[i]); } int h, w, n; cin >> h >> w >> n; vector<pair<int, int>> obst(n); for (int i = 0; i < n; i++) { cin >> obst[i].first >> obst[i].second; obst[i].first--; obst[i].second--; } obst.push_back({h - 1, w - 1}); n++; sort(obst.begin(), obst.end()); vector<int> dp(n); for (int i = 0; i < n; i++) { int x = obst[i].first, y = obst[i].second; dp[i] = c(x + y, x); for (int j = 0; j < i; j++) { int x1 = obst[j].first, y1 = obst[j].second; dp[i] = (dp[i] - dp[j] * c(x - x1 + y - y1, x - x1) % mod + mod) % mod; } // cerr << dp[i] << endl; } cout << dp.back() << endl; }
#include "bits/stdc++.h" #define int long long using namespace std; const int mod = 1e9 + 7; int qpow(int a, int power) { if (power == 0) { return 1; } int b = qpow(a, power / 2); if (power % 2 == 0) { return (b * b) % mod; } else { return (((b * b) % mod) * a) % mod; } } int inv(int a) { return qpow(a, mod - 2); } vector<int> f(2e5 + 10), inv_f(2e5 + 10); int c(int n, int k) { return f[n] * inv_f[k] % mod * inv_f[n - k] % mod; } int32_t main() { ios_base::sync_with_stdio(0); f[0] = 1; for (int i = 1; i < f.size(); i++) { f[i] = i * f[i - 1] % mod; } for (int i = 0; i < f.size(); i++) { inv_f[i] = inv(f[i]); } int h, w, n; cin >> h >> w >> n; vector<pair<int, int>> obst(n); for (int i = 0; i < n; i++) { cin >> obst[i].first >> obst[i].second; obst[i].first--; obst[i].second--; } obst.push_back({h - 1, w - 1}); n++; sort(obst.begin(), obst.end()); vector<int> dp(n); for (int i = 0; i < n; i++) { int x = obst[i].first, y = obst[i].second; dp[i] = c(x + y, x); for (int j = 0; j < i; j++) { int x1 = obst[j].first, y1 = obst[j].second; if (x1 <= x && y1 <= y) { dp[i] = (dp[i] - dp[j] * c(x - x1 + y - y1, x - x1) % mod + mod) % mod; } } // cerr << dp[i] << endl; } cout << dp.back() << endl; }
replace
52
53
52
55
0
p03184
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < ((int)(n)); i++) #define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++) #define irep(i, n) for (int i = ((int)(n)-1); i >= 0; i--) #define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--) typedef long long ll; typedef pair<ll, ll> mp; /* 除DP dp[i]=|初めて通った壁がi番目| dp[i]=|(1,1) \to w_i| - \sum^{i-1}_{j=0}|w_j \to w_i|dp[j] */ struct pos { ll x, y; }; bool operator<(const pos &lhs, const pos &rhs) { return lhs.x + lhs.y < rhs.x + rhs.y; } struct Factorial { long long N, mod; vector<long long> fact, inv; long long mod_pow(long long x, long long n) { long long res = 1; while (n) { if (n & 1) (res *= x) %= mod; (x *= x) %= mod; n >>= 1; } return res; } long long Inverse(long long n) { return mod_pow(n, mod - 2); } Factorial() {} Factorial(long long sn, long long smod) { N = sn; mod = smod; fact.reserve(sn + 1); inv.reserve(sn + 1); // 初期化 fact[0] = 1; for (int i = 1; i <= sn; i++) { fact[i] = (fact[i - 1] * i) % mod; } inv[sn] = Inverse(fact[sn]); for (int i = sn; i > 0; i--) { inv[i - 1] = (inv[i] * i) % mod; } } long long Fact(long long n) { if (n < 0) return 0; return fact[n]; } long long Inv(long long n) { if (n < 0) return 0; return inv[n]; } long long Combination(long long n, long long r) { if (n < r) return 0; if (r < 0) return 0; if (r == 0) return 1; long long ans = 1; ans = (ans * Fact(n)) % mod; ans = (ans * Inv(r)) % mod; ans = (ans * Inv(n - r)) % mod; return ans; } }; ll H, W, n, dp[2010] = {}, ans = 0, mod = 1e9 + 7; pos w[3010]; int main(void) { cin >> H >> W >> n; rep(i, n) { ll x, y; cin >> y >> x; w[i] = {x, y}; } w[n] = {W, H}; // Factorial f(200010, mod); sort(w, w + n + 1); reg(i, 0, n) { ll sum = 0; rep(j, i) { if (w[i].x >= w[j].x && w[i].y >= w[j].y) { sum = (sum + (dp[j] * f.Combination(w[i].x - w[j].x + w[i].y - w[j].y, w[i].x - w[j].x)) % mod) % mod; } } dp[i] = (f.Combination(w[i].x + w[i].y - 2, w[i].x - 1) + mod - sum) % mod; } cout << dp[n] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < ((int)(n)); i++) #define reg(i, a, b) for (int i = ((int)(a)); i <= ((int)(b)); i++) #define irep(i, n) for (int i = ((int)(n)-1); i >= 0; i--) #define ireg(i, a, b) for (int i = ((int)(b)); i >= ((int)(a)); i--) typedef long long ll; typedef pair<ll, ll> mp; /* 除DP dp[i]=|初めて通った壁がi番目| dp[i]=|(1,1) \to w_i| - \sum^{i-1}_{j=0}|w_j \to w_i|dp[j] */ struct pos { ll x, y; }; bool operator<(const pos &lhs, const pos &rhs) { return lhs.x + lhs.y < rhs.x + rhs.y; } struct Factorial { long long N, mod; vector<long long> fact, inv; long long mod_pow(long long x, long long n) { long long res = 1; while (n) { if (n & 1) (res *= x) %= mod; (x *= x) %= mod; n >>= 1; } return res; } long long Inverse(long long n) { return mod_pow(n, mod - 2); } Factorial() {} Factorial(long long sn, long long smod) { N = sn; mod = smod; fact.reserve(sn + 1); inv.reserve(sn + 1); // 初期化 fact[0] = 1; for (int i = 1; i <= sn; i++) { fact[i] = (fact[i - 1] * i) % mod; } inv[sn] = Inverse(fact[sn]); for (int i = sn; i > 0; i--) { inv[i - 1] = (inv[i] * i) % mod; } } long long Fact(long long n) { if (n < 0) return 0; return fact[n]; } long long Inv(long long n) { if (n < 0) return 0; return inv[n]; } long long Combination(long long n, long long r) { if (n < r) return 0; if (r < 0) return 0; if (r == 0) return 1; long long ans = 1; ans = (ans * Fact(n)) % mod; ans = (ans * Inv(r)) % mod; ans = (ans * Inv(n - r)) % mod; return ans; } }; ll H, W, n, dp[3010] = {}, ans = 0, mod = 1e9 + 7; pos w[3010]; int main(void) { cin >> H >> W >> n; rep(i, n) { ll x, y; cin >> y >> x; w[i] = {x, y}; } w[n] = {W, H}; // Factorial f(200010, mod); sort(w, w + n + 1); reg(i, 0, n) { ll sum = 0; rep(j, i) { if (w[i].x >= w[j].x && w[i].y >= w[j].y) { sum = (sum + (dp[j] * f.Combination(w[i].x - w[j].x + w[i].y - w[j].y, w[i].x - w[j].x)) % mod) % mod; } } dp[i] = (f.Combination(w[i].x + w[i].y - 2, w[i].x - 1) + mod - sum) % mod; } cout << dp[n] << endl; return 0; }
replace
77
78
77
78
0
p03184
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long LL; #define MAXN 200001 #define MOD 1000000007 int H, W, N; pair<int, int> bl[2001]; int dp[2001]; // x must be relatively prime to p int fact[MAXN], inv[MAXN], ifact[MAXN]; // x must be relatively prime to p template <typename T, typename T1> T mod(T x, T1 p) { x %= p; if (x < 0) x += p; return x; } // x must be relatively prime to p template <typename T> T inverse(T x, T p) { x = mod(x, p); if (x == 1) return x; return mod((1LL * (-p / x) * (inv[p % x] % p)), p); // Since inverse of p % x is already calculated. } int NcR(int n, int r) { int ret = (1LL * ifact[n - r] * ifact[r]) % MOD; ret = (1LL * ret * fact[n]) % MOD; return ret; } int main(int argc, char **argv) { #ifdef OJ freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); #endif std::ios::sync_with_stdio(false); fact[0] = 1; for (int i = 1; i < MAXN; i++) { fact[i] = (1LL * fact[i - 1] * i) % MOD; } ifact[0] = 1; for (int i = 1; i < MAXN; i++) { inv[i] = inverse(i, MOD); ifact[i] = (1LL * ifact[i - 1] * inv[i]) % MOD; } cin >> H >> W >> N; for (int i = 0; i < N; i++) cin >> bl[i].first >> bl[i].second; bl[N].first = H; bl[N].second = W; N++; sort(bl, bl + N); for (int i = 0; i < N; i++) { int x = bl[i].first; int y = bl[i].second; dp[i] = NcR(x + y - 2, x - 1); for (int j = 0; j < i; j++) { if (bl[j].second <= y) { int dx = x - bl[j].first; int dy = y - bl[j].second; dp[i] -= (1LL * dp[j] * NcR(dx + dy, dx)) % MOD; if (dp[i] < 0) dp[i] += MOD; } } } cout << dp[N - 1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long LL; #define MAXN 200001 #define MOD 1000000007 int H, W, N; pair<int, int> bl[3001]; int dp[3001]; // x must be relatively prime to p int fact[MAXN], inv[MAXN], ifact[MAXN]; // x must be relatively prime to p template <typename T, typename T1> T mod(T x, T1 p) { x %= p; if (x < 0) x += p; return x; } // x must be relatively prime to p template <typename T> T inverse(T x, T p) { x = mod(x, p); if (x == 1) return x; return mod((1LL * (-p / x) * (inv[p % x] % p)), p); // Since inverse of p % x is already calculated. } int NcR(int n, int r) { int ret = (1LL * ifact[n - r] * ifact[r]) % MOD; ret = (1LL * ret * fact[n]) % MOD; return ret; } int main(int argc, char **argv) { #ifdef OJ freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); #endif std::ios::sync_with_stdio(false); fact[0] = 1; for (int i = 1; i < MAXN; i++) { fact[i] = (1LL * fact[i - 1] * i) % MOD; } ifact[0] = 1; for (int i = 1; i < MAXN; i++) { inv[i] = inverse(i, MOD); ifact[i] = (1LL * ifact[i - 1] * inv[i]) % MOD; } cin >> H >> W >> N; for (int i = 0; i < N; i++) cin >> bl[i].first >> bl[i].second; bl[N].first = H; bl[N].second = W; N++; sort(bl, bl + N); for (int i = 0; i < N; i++) { int x = bl[i].first; int y = bl[i].second; dp[i] = NcR(x + y - 2, x - 1); for (int j = 0; j < i; j++) { if (bl[j].second <= y) { int dx = x - bl[j].first; int dy = y - bl[j].second; dp[i] -= (1LL * dp[j] * NcR(dx + dy, dx)) % MOD; if (dp[i] < 0) dp[i] += MOD; } } } cout << dp[N - 1] << endl; return 0; }
replace
9
11
9
11
0
p03184
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define mp make_pair #define pb push_back #define rep(i, n) for (int i = 0; i < n; i++) #define rep1(i, n) for (int i = 1; i <= (int)(n); i++) template <int mod> struct ModInt { int x; ModInt() : x(0) {} ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {} ModInt &operator+=(const ModInt &p) { if ((x += p.x) >= mod) x -= mod; return *this; } ModInt &operator-=(const ModInt &p) { if ((x += mod - p.x) >= mod) x -= mod; return *this; } ModInt &operator*=(const ModInt &p) { x = (int)(1LL * x * p.x % mod); return *this; } ModInt &operator/=(const ModInt &p) { *this *= p.inverse(); return *this; } ModInt operator-() const { return ModInt(-x); } ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; } ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; } ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; } ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; } bool operator==(const ModInt &p) const { return x == p.x; } bool operator!=(const ModInt &p) const { return x != p.x; } ModInt inverse() const { int a = x, b = mod, u = 1, v = 0, t; while (b > 0) { t = a / b; swap(a -= t * b, b); swap(u -= t * v, v); } return ModInt(u); } ModInt pow(int64_t n) const { ModInt ret(1), mul(x); while (n > 0) { if (n & 1) ret *= mul; mul *= mul; n >>= 1; } return ret; } friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; } friend istream &operator>>(istream &is, ModInt &a) { int64_t t; is >> t; a = ModInt<mod>(t); return (is); } static int get_mod() { return mod; } }; const int mod = 1e9 + 7; using modint = ModInt<mod>; int main() { ios::sync_with_stdio(false); cin.tie(0); int h, w, n; cin >> h >> w >> n; vector<pair<int, int>> cor(n); rep(i, n) { int r, c; cin >> r >> c; --r; --c; cor[i] = mp(r, c); } sort(cor.begin(), cor.end()); const int n_max = h + w + 10; vector<modint> kai(n_max); kai[0] = 1; rep1(i, n_max - 1) kai[i] = kai[i - 1] * ((modint)i); vector<vector<modint>> dp(n, vector<modint>(2)); rep(i, n) dp[i][0] = 0; rep(i, n) { int r = cor[i].first; int c = cor[i].second; dp[i][1] = kai[r + c] / kai[r] / kai[c]; } modint ans = kai[h + w - 2] / kai[h - 1] / kai[w - 1]; rep(i, n) { int ci = cor[i].second; int ri = cor[i].first; rep(j, i) { int cj = cor[j].second; if (ci >= cj) { int rj = cor[j].first; dp[i][0] += dp[j][1] * kai[ri - rj + ci - cj] / kai[ri - rj] / kai[ci - cj]; dp[i][1] += dp[j][0] * kai[ri - rj + ci - cj] / kai[ri - rj] / kai[ci - cj]; } } ans += (dp[i][0] - dp[i][1]) * kai[h - 1 - ri + w - 1 - ci] / kai[h - 1 - ri] / kai[w - 1 - ci]; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define mp make_pair #define pb push_back #define rep(i, n) for (int i = 0; i < n; i++) #define rep1(i, n) for (int i = 1; i <= (int)(n); i++) template <int mod> struct ModInt { int x; ModInt() : x(0) {} ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {} ModInt &operator+=(const ModInt &p) { if ((x += p.x) >= mod) x -= mod; return *this; } ModInt &operator-=(const ModInt &p) { if ((x += mod - p.x) >= mod) x -= mod; return *this; } ModInt &operator*=(const ModInt &p) { x = (int)(1LL * x * p.x % mod); return *this; } ModInt &operator/=(const ModInt &p) { *this *= p.inverse(); return *this; } ModInt operator-() const { return ModInt(-x); } ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; } ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; } ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; } ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; } bool operator==(const ModInt &p) const { return x == p.x; } bool operator!=(const ModInt &p) const { return x != p.x; } ModInt inverse() const { int a = x, b = mod, u = 1, v = 0, t; while (b > 0) { t = a / b; swap(a -= t * b, b); swap(u -= t * v, v); } return ModInt(u); } ModInt pow(int64_t n) const { ModInt ret(1), mul(x); while (n > 0) { if (n & 1) ret *= mul; mul *= mul; n >>= 1; } return ret; } friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; } friend istream &operator>>(istream &is, ModInt &a) { int64_t t; is >> t; a = ModInt<mod>(t); return (is); } static int get_mod() { return mod; } }; const int mod = 1e9 + 7; using modint = ModInt<mod>; int main() { ios::sync_with_stdio(false); cin.tie(0); int h, w, n; cin >> h >> w >> n; vector<pair<int, int>> cor(n); rep(i, n) { int r, c; cin >> r >> c; --r; --c; cor[i] = mp(r, c); } sort(cor.begin(), cor.end()); const int n_max = h + w + 10; vector<modint> kai(n_max); kai[0] = 1; rep1(i, n_max - 1) kai[i] = kai[i - 1] * ((modint)i); vector<vector<modint>> dp(n, vector<modint>(2)); rep(i, n) dp[i][0] = 0; rep(i, n) { int r = cor[i].first; int c = cor[i].second; dp[i][1] = kai[r + c] / kai[r] / kai[c]; } modint ans = kai[h + w - 2] / kai[h - 1] / kai[w - 1]; rep(i, n) { int ci = cor[i].second; int ri = cor[i].first; rep(j, i) { int cj = cor[j].second; if (ci >= cj) { int rj = cor[j].first; modint fac = kai[ri - rj + ci - cj] / kai[ri - rj] / kai[ci - cj]; dp[i][0] += dp[j][1] * fac; dp[i][1] += dp[j][0] * fac; } } ans += (dp[i][0] - dp[i][1]) * kai[h - 1 - ri + w - 1 - ci] / kai[h - 1 - ri] / kai[w - 1 - ci]; } cout << ans << endl; return 0; }
replace
120
124
120
123
TLE
p03184
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define fastio \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define pi 3.1415926535897 #define ll long long int #define ld long double #define ff first #define ss second #define mp make_pair #define pb push_back #define lb lower_bound #define ub upper_bound #define mod 1000000007 #define mod1 998244353 #define inf 1000000000000000000 #define inf1 1000000000 #define endl '\n' bool comp(pair<pair<ll, ll>, ll> &a, pair<pair<ll, ll>, ll> &b) { if (a.ff.ff < b.ff.ff) return true; else if (a.ff.ff == b.ff.ff) return a.ff.ss < b.ff.ss; else return false; } ll fact[200005]; pair<pair<ll, ll>, ll> str[3005]; ll power(ll a, ll b) { if (a == 1) return 1; if (b == 0) return 1; if (b == 1) return a % mod; ll c = power(a, b / 2); ll d = (c * c) % mod; if (b % 2 == 1) d = (d * a) % mod; return d; } ll cal(ll x, ll y) { ll a = x + y - 2; ll b = x - 1; ll c = y - 1; ll ans = fact[a]; ans = (ans * power(fact[b], mod - 2)) % mod; ans = (ans * power(fact[c], mod - 2)) % mod; return ans; } int main() { fastio; #ifdef APNA_IO freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout); #endif ll h, w, n; cin >> h >> w >> n; fact[0] = 1; for (ll i = 1; i < 200005; i++) fact[i] = (fact[i - 1] * i) % mod; ll ans = cal(h, w); for (ll i = 0; i < n; i++) { ll a, b; cin >> a >> b; str[i] = mp(mp(a, b), cal(a, b)); } sort(str, str + n, comp); for (ll i = 0; i < n; i++) { for (ll j = i + 1; j < n; j++) { if (str[i].ff.ff <= str[j].ff.ff && str[i].ff.ss <= str[j].ff.ss) { str[j].ss = (str[j].ss - (str[i].ss * cal(str[j].ff.ff - str[i].ff.ff + 1, str[j].ff.ss - str[i].ff.ss + 1)) % mod) % mod; } } } for (ll i = 0; i < n; i++) ans = (ans - (str[i].ss * cal(h - str[i].ff.ff + 1, w - str[i].ff.ss + 1)) % mod) % mod; ans = ans % mod; ans = (ans + mod) % mod; cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define fastio \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL); #define pi 3.1415926535897 #define ll long long int #define ld long double #define ff first #define ss second #define mp make_pair #define pb push_back #define lb lower_bound #define ub upper_bound #define mod 1000000007 #define mod1 998244353 #define inf 1000000000000000000 #define inf1 1000000000 #define endl '\n' bool comp(pair<pair<ll, ll>, ll> &a, pair<pair<ll, ll>, ll> &b) { if (a.ff.ff < b.ff.ff) return true; else if (a.ff.ff == b.ff.ff) return a.ff.ss < b.ff.ss; else return false; } ll fact[200005]; pair<pair<ll, ll>, ll> str[3005]; ll power(ll x, ll y) { ll res = 1; while (y > 0) { if (y & 1) res = res * x; res %= mod; y = y >> 1; x = x * x; x %= mod; } return res; } ll cal(ll x, ll y) { ll a = x + y - 2; ll b = x - 1; ll c = y - 1; ll ans = fact[a]; ans = (ans * power(fact[b], mod - 2)) % mod; ans = (ans * power(fact[c], mod - 2)) % mod; return ans; } int main() { fastio; #ifdef APNA_IO freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout); #endif ll h, w, n; cin >> h >> w >> n; fact[0] = 1; for (ll i = 1; i < 200005; i++) fact[i] = (fact[i - 1] * i) % mod; ll ans = cal(h, w); for (ll i = 0; i < n; i++) { ll a, b; cin >> a >> b; str[i] = mp(mp(a, b), cal(a, b)); } sort(str, str + n, comp); for (ll i = 0; i < n; i++) { for (ll j = i + 1; j < n; j++) { if (str[i].ff.ff <= str[j].ff.ff && str[i].ff.ss <= str[j].ff.ss) { str[j].ss = (str[j].ss - (str[i].ss * cal(str[j].ff.ff - str[i].ff.ff + 1, str[j].ff.ss - str[i].ff.ss + 1)) % mod) % mod; } } } for (ll i = 0; i < n; i++) ans = (ans - (str[i].ss * cal(h - str[i].ff.ff + 1, w - str[i].ff.ss + 1)) % mod) % mod; ans = ans % mod; ans = (ans + mod) % mod; cout << ans << endl; return 0; }
replace
30
42
30
41
TLE
p03184
C++
Time Limit Exceeded
#include <algorithm> #include <fstream> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <set> #include <stdio.h> #include <string> #include <utility> #include <vector> using namespace std; long long P = 1000000007; struct N { int i, j; long long v; }; vector<N> ns; vector<vector<int>> p; int h, w, n; class C { public: int n; vector<long long> fac, inv, facInv; long long power(long long e, long long x) { if (x == 0) return 1; if (x == 1) return e; if (x % 2 == 0) return power((e * e) % P, x / 2); return (e * power(e, x - 1)) % P; } C(int n_) { n = n_; fac.resize(n + 1); inv.resize(n + 1); facInv.resize(n + 1); fac[0] = fac[1] = 1; for (int i = 2; i <= n; i++) fac[i] = (i * fac[i - 1]) % P; inv[0] = inv[1] = 1; for (int i = 2; i <= n; i++) inv[i] = power(i, P - 2); facInv[0] = facInv[1] = 1; for (int i = 2; i <= n; i++) facInv[i] = (inv[i] * facInv[i - 1]) % P; } long long comb(int N, int K) { if (N < K || K < 0 || N < 0) return 0; if (N == 0 || K == 0 || K == N) return 1; return ((fac[N] * facInv[K]) % P * facInv[N - K]) % P; } long long hcomb(int N, int K) { if (N == 0 && K == 0) return 1; return comb(N + K - 1, K); } long long mul(long long a, long long b) { return (a * b) % P; } long long add(long long a, long long b) { return (a + b) % P; } long long div(long long a, long long b) { return (a * power(b, P - 2)) % P; } }; C cmp(202020); int main() { cin >> h >> w >> n; ns.resize(n + 2); p.resize(n + 2); ns[0] = N{0, 0, 1}; for (int i = 1; i <= n; i++) { cin >> ns[i].i >> ns[i].j; ns[i].i--; ns[i].j--; ns[i].v = 0; } ns[n + 1] = N{h - 1, w - 1, 0}; sort(ns.begin(), ns.end(), [](const N &l, const N &r) { if (l.i == r.i) return l.j < r.j; return l.i < r.i; }); for (int i = 0; i < n + 2; i++) for (int j = i + 1; j < n + 2; j++) { if (ns[i].i <= ns[j].i && ns[i].j <= ns[j].j) p[i].push_back(j); } for (int i = 0; i <= n; i++) { for (auto &j : p[i]) { int h = (ns[j].i - ns[i].i); int w = (ns[j].j - ns[i].j); long long diff = (ns[i].v * cmp.div(cmp.fac[h + w], cmp.mul(cmp.fac[h], cmp.fac[w]))) % P; if (i != 0) { ns[j].v += P - diff; } else { ns[j].v += diff; } ns[j].v %= P; } } cout << ns[n + 1].v << endl; return 0; }
#include <algorithm> #include <fstream> #include <iomanip> #include <iostream> #include <map> #include <math.h> #include <set> #include <stdio.h> #include <string> #include <utility> #include <vector> using namespace std; long long P = 1000000007; struct N { int i, j; long long v; }; vector<N> ns; vector<vector<int>> p; int h, w, n; class C { public: int n; vector<long long> fac, inv, facInv; long long power(long long e, long long x) { if (x == 0) return 1; if (x == 1) return e; if (x % 2 == 0) return power((e * e) % P, x / 2); return (e * power(e, x - 1)) % P; } C(int n_) { n = n_; fac.resize(n + 1); inv.resize(n + 1); facInv.resize(n + 1); fac[0] = fac[1] = 1; for (int i = 2; i <= n; i++) fac[i] = (i * fac[i - 1]) % P; inv[0] = inv[1] = 1; for (int i = 2; i <= n; i++) inv[i] = power(i, P - 2); facInv[0] = facInv[1] = 1; for (int i = 2; i <= n; i++) facInv[i] = (inv[i] * facInv[i - 1]) % P; } long long comb(int N, int K) { if (N < K || K < 0 || N < 0) return 0; if (N == 0 || K == 0 || K == N) return 1; return ((fac[N] * facInv[K]) % P * facInv[N - K]) % P; } long long hcomb(int N, int K) { if (N == 0 && K == 0) return 1; return comb(N + K - 1, K); } long long mul(long long a, long long b) { return (a * b) % P; } long long add(long long a, long long b) { return (a + b) % P; } long long div(long long a, long long b) { return (a * power(b, P - 2)) % P; } }; C cmp(202020); int main() { cin >> h >> w >> n; ns.resize(n + 2); p.resize(n + 2); ns[0] = N{0, 0, 1}; for (int i = 1; i <= n; i++) { cin >> ns[i].i >> ns[i].j; ns[i].i--; ns[i].j--; ns[i].v = 0; } ns[n + 1] = N{h - 1, w - 1, 0}; sort(ns.begin(), ns.end(), [](const N &l, const N &r) { if (l.i == r.i) return l.j < r.j; return l.i < r.i; }); for (int i = 0; i < n + 2; i++) for (int j = i + 1; j < n + 2; j++) { if (ns[i].i <= ns[j].i && ns[i].j <= ns[j].j) p[i].push_back(j); } for (int i = 0; i <= n; i++) { for (auto &j : p[i]) { int h = (ns[j].i - ns[i].i); int w = (ns[j].j - ns[i].j); long long diff = (ns[i].v * cmp.mul(cmp.fac[h + w], cmp.mul(cmp.facInv[h], cmp.facInv[w]))) % P; if (i != 0) { ns[j].v += P - diff; } else { ns[j].v += diff; } ns[j].v %= P; } } cout << ns[n + 1].v << endl; return 0; }
replace
96
97
96
98
TLE
p03184
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define MAXN 100005 typedef long long ll; const ll mod = 1e9 + 7; ll fac[MAXN], ifac[MAXN]; ll fastpow(ll a, ll b) { ll res = 1; while (b) { if (b & 1) (res *= a) %= mod; (a *= a) %= mod; b >>= 1; } return res; } ll C(int m, int n) { return fac[m] * ifac[n] % mod * ifac[m - n] % mod; } struct T { int x, y; } a[MAXN]; bool cmp(T a, T b) { return a.x < b.x || (a.x == b.x && a.y < b.y); } ll ans[MAXN]; int main() { fac[0] = ifac[0] = 1; for (int i = 1; i < MAXN; i++) fac[i] = fac[i - 1] * i % mod, ifac[i] = ifac[i - 1] * fastpow(i, mod - 2) % mod; int h, w, n; cin >> h >> w >> n; for (int i = 1; i <= n; i++) scanf("%d%d", &a[i].x, &a[i].y); sort(a + 1, a + n + 1, cmp); ++n; a[n].x = h; a[n].y = w; for (int i = 1; i <= n; i++) ans[i] = C(a[i].x + a[i].y - 2, a[i].x - 1); for (int i = 1; i <= n; i++) for (int j = i + 1; j <= n; j++) if (a[i].x <= a[j].x && a[i].y <= a[j].y) { ll x = a[j].x - a[i].x + 1, y = a[j].y - a[i].y + 1; ans[j] = (ans[j] - C(x + y - 2, x - 1) * ans[i] % mod + mod) % mod; } cout << ans[n] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define MAXN 200005 typedef long long ll; const ll mod = 1e9 + 7; ll fac[MAXN], ifac[MAXN]; ll fastpow(ll a, ll b) { ll res = 1; while (b) { if (b & 1) (res *= a) %= mod; (a *= a) %= mod; b >>= 1; } return res; } ll C(int m, int n) { return fac[m] * ifac[n] % mod * ifac[m - n] % mod; } struct T { int x, y; } a[MAXN]; bool cmp(T a, T b) { return a.x < b.x || (a.x == b.x && a.y < b.y); } ll ans[MAXN]; int main() { fac[0] = ifac[0] = 1; for (int i = 1; i < MAXN; i++) fac[i] = fac[i - 1] * i % mod, ifac[i] = ifac[i - 1] * fastpow(i, mod - 2) % mod; int h, w, n; cin >> h >> w >> n; for (int i = 1; i <= n; i++) scanf("%d%d", &a[i].x, &a[i].y); sort(a + 1, a + n + 1, cmp); ++n; a[n].x = h; a[n].y = w; for (int i = 1; i <= n; i++) ans[i] = C(a[i].x + a[i].y - 2, a[i].x - 1); for (int i = 1; i <= n; i++) for (int j = i + 1; j <= n; j++) if (a[i].x <= a[j].x && a[i].y <= a[j].y) { ll x = a[j].x - a[i].x + 1, y = a[j].y - a[i].y + 1; ans[j] = (ans[j] - C(x + y - 2, x - 1) * ans[i] % mod + mod) % mod; } cout << ans[n] << endl; return 0; }
replace
2
3
2
3
0
p03184
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define F first #define S second using namespace std; typedef long long ll; typedef pair<ll, ll> pll; const ll MOD = 1e9 + 7; ll H, W, N; vector<ll> fact, finv; vector<pll> walls; vector<vector<ll>> dp; ll modpow(ll a, ll b) { ll result = 1; for (; b; b /= 2) { if (b % 2) result = result * a % MOD; a = a * a % MOD; } return result; } ll choose(ll n, ll k) { return fact[n] * finv[k] % MOD * finv[n - k] % MOD; } ll ways(int i, int j) { ll dr = walls[j].F - walls[i].F; ll dc = walls[j].S - walls[i].S; return dr < 0 || dc < 0 ? 0 : choose(dr + dc, dc); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> H >> W >> N; fact = {1}; finv = {1}; for (ll n = 1; n <= H + W - 2; n++) { fact.push_back(fact.back() * n % MOD); finv.push_back(modpow(fact.back(), MOD - 2)); } walls.resize(N); for (int i = 0; i < N; i++) cin >> walls[i].F >> walls[i].S; walls.push_back({1, 1}); walls.push_back({H, W}); sort(walls.begin(), walls.end()); int n = walls.size(); dp.assign(n, vector<ll>(n)); for (int i = 0; i < n - 1; i++) dp[i][i + 1] = ways(i, i + 1); for (int w = 3; w <= n; w++) { for (int l = 0, r = w - 1; r < n; l++, r++) { dp[l][r] = ways(l, r); for (int m = l + 1; m < r; m++) { dp[l][r] -= dp[l][m] * ways(m, r) % MOD; if (dp[l][r] < 0) dp[l][r] += MOD; } } } cout << dp[0][n - 1] << endl; }
#include <bits/stdc++.h> #define F first #define S second using namespace std; typedef long long ll; typedef pair<ll, ll> pll; const ll MOD = 1e9 + 7; ll H, W, N; vector<ll> fact, finv; vector<pll> walls; vector<vector<ll>> dp; ll modpow(ll a, ll b) { ll result = 1; for (; b; b /= 2) { if (b % 2) result = result * a % MOD; a = a * a % MOD; } return result; } ll choose(ll n, ll k) { return fact[n] * finv[k] % MOD * finv[n - k] % MOD; } ll ways(int i, int j) { ll dr = walls[j].F - walls[i].F; ll dc = walls[j].S - walls[i].S; return dr < 0 || dc < 0 ? 0 : choose(dr + dc, dc); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> H >> W >> N; fact = {1}; finv = {1}; for (ll n = 1; n <= H + W - 2; n++) { fact.push_back(fact.back() * n % MOD); finv.push_back(modpow(fact.back(), MOD - 2)); } walls.resize(N); for (int i = 0; i < N; i++) cin >> walls[i].F >> walls[i].S; walls.push_back({1, 1}); walls.push_back({H, W}); sort(walls.begin(), walls.end()); int n = walls.size(); dp.assign(n, vector<ll>(n)); for (int j = 1; j < n; j++) { dp[0][j] = ways(0, j); for (int i = 1; i < j; i++) { dp[0][j] -= dp[0][i] * ways(i, j) % MOD; if (dp[0][j] < 0) dp[0][j] += MOD; } } cout << dp[0][n - 1] << endl; }
replace
55
65
55
61
TLE
p03184
C++
Runtime Error
/* _____ _ _ _ _ |_ _| |__ ___ / \ _ __ ___| |__ _ _| | | | | '_ \ / _ \ / _ \ | '_ \/ __| '_ \| | | | | | | | | | | __// ___ \| | | \__ \ | | | |_| | | |_| |_| |_|\___/_/ \_\_| |_|___/_| |_|\__,_|_| */ #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define ll long long #define pb push_back #define ppb pop_back #define endl '\n' #define mii map<ll, ll> #define msi map<string, ll> #define mis map<ll, string> #define rep(i, a, b) for (ll i = a; i < b; i++) #define repr(i, a, b) for (ll i = b - 1; i >= a; i--) #define trav(a, x) for (auto &a : x) #define pii pair<ll, ll> #define vi vector<ll> #define vii vector<pair<ll, ll>> #define vs vector<string> #define all(a) (a).begin(), (a).end() #define F first #define S second #define sz(x) (ll) x.size() #define hell 1000000007 #define lbnd lower_bound #define ubnd upper_bound #define max(a, b) (a > b ? a : b) #define min(a, b) (a < b ? a : b) /* For Debugging */ #define DEBUG cerr << "\n>>>I'm Here<<<\n" << endl; #define display(x) \ trav(a, x) cout << a << " "; \ cout << endl; #define what_is(x) cerr << #x << " is " << x << endl; std::mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count()); #define ordered_set \ tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> #define TIME \ cerr << "\nTime elapsed: " << setprecision(5) \ << 1000.0 * clock() / CLOCKS_PER_SEC << "ms\n"; #define DECIMAL(n) \ cout << fixed; \ cout << setprecision(n); #define FAST \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); using namespace __gnu_pbds; using namespace std; #define PI 3.141592653589793 #define N 200005 ll dp[1005]; ll fact[N], modi[N]; ll add(ll a, ll b, ll mod = hell) { return (a + b) % mod; } ll sub(ll a, ll b, ll mod = hell) { return (a - b + mod) % mod; } ll mul(ll a, ll b, ll mod = hell) { return (a * b) % mod; } ll expo(ll base, ll exponent, ll mod) { // return base^exponent modulo modulus ll ans = 1; while (exponent != 0) { if ((exponent & 1) == 1) { ans = ans * base; ans = ans % mod; } base = base * base; base %= mod; exponent >>= 1; } return ans % mod; } ll modii(ll num) { return expo(num, hell - 2, hell); } void init() { fact[0] = 1; rep(i, 1, N) fact[i] = (fact[i - 1] * i) % hell; rep(i, 0, N) modi[i] = modii(fact[i]); } ll fun(ll x1, ll y1, ll x2, ll y2) { ll x = abs(x1 - x2), y = abs(y1 - y2); // cout<<x<<" - "<<y<<" "<<x+y<<endl; return mul(fact[x + y], mul(modi[x], modi[y])); } void solve() { ll h, w, n; cin >> h >> w >> n; vii v(n); rep(i, 0, n) { cin >> v[i].F >> v[i].S; } v.pb({h, w}); sort(all(v)); n++; rep(i, 0, n) dp[i] = fun(1, 1, v[i].F, v[i].S); // rep(i,0,n) // { // cout<<v[i].F<<" "<<v[i].S<<" "<<dp[i]<<"\n"; // } // cout<<endl; rep(i, 0, n) { rep(j, i + 1, n) { if (v[i].F > v[j].F || v[i].S > v[j].S) continue; dp[j] = sub(dp[j], mul(dp[i], fun(v[i].F, v[i].S, v[j].F, v[j].S))); } } cout << dp[n - 1] << endl; return; } int main() { init(); FAST int TESTS = 1; // cin>>TESTS; while (TESTS--) { solve(); } TIME return 0; }
/* _____ _ _ _ _ |_ _| |__ ___ / \ _ __ ___| |__ _ _| | | | | '_ \ / _ \ / _ \ | '_ \/ __| '_ \| | | | | | | | | | | __// ___ \| | | \__ \ | | | |_| | | |_| |_| |_|\___/_/ \_\_| |_|___/_| |_|\__,_|_| */ #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define ll long long #define pb push_back #define ppb pop_back #define endl '\n' #define mii map<ll, ll> #define msi map<string, ll> #define mis map<ll, string> #define rep(i, a, b) for (ll i = a; i < b; i++) #define repr(i, a, b) for (ll i = b - 1; i >= a; i--) #define trav(a, x) for (auto &a : x) #define pii pair<ll, ll> #define vi vector<ll> #define vii vector<pair<ll, ll>> #define vs vector<string> #define all(a) (a).begin(), (a).end() #define F first #define S second #define sz(x) (ll) x.size() #define hell 1000000007 #define lbnd lower_bound #define ubnd upper_bound #define max(a, b) (a > b ? a : b) #define min(a, b) (a < b ? a : b) /* For Debugging */ #define DEBUG cerr << "\n>>>I'm Here<<<\n" << endl; #define display(x) \ trav(a, x) cout << a << " "; \ cout << endl; #define what_is(x) cerr << #x << " is " << x << endl; std::mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count()); #define ordered_set \ tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> #define TIME \ cerr << "\nTime elapsed: " << setprecision(5) \ << 1000.0 * clock() / CLOCKS_PER_SEC << "ms\n"; #define DECIMAL(n) \ cout << fixed; \ cout << setprecision(n); #define FAST \ ios_base::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0); using namespace __gnu_pbds; using namespace std; #define PI 3.141592653589793 #define N 200005 ll dp[3005]; ll fact[N], modi[N]; ll add(ll a, ll b, ll mod = hell) { return (a + b) % mod; } ll sub(ll a, ll b, ll mod = hell) { return (a - b + mod) % mod; } ll mul(ll a, ll b, ll mod = hell) { return (a * b) % mod; } ll expo(ll base, ll exponent, ll mod) { // return base^exponent modulo modulus ll ans = 1; while (exponent != 0) { if ((exponent & 1) == 1) { ans = ans * base; ans = ans % mod; } base = base * base; base %= mod; exponent >>= 1; } return ans % mod; } ll modii(ll num) { return expo(num, hell - 2, hell); } void init() { fact[0] = 1; rep(i, 1, N) fact[i] = (fact[i - 1] * i) % hell; rep(i, 0, N) modi[i] = modii(fact[i]); } ll fun(ll x1, ll y1, ll x2, ll y2) { ll x = abs(x1 - x2), y = abs(y1 - y2); // cout<<x<<" - "<<y<<" "<<x+y<<endl; return mul(fact[x + y], mul(modi[x], modi[y])); } void solve() { ll h, w, n; cin >> h >> w >> n; vii v(n); rep(i, 0, n) { cin >> v[i].F >> v[i].S; } v.pb({h, w}); sort(all(v)); n++; rep(i, 0, n) dp[i] = fun(1, 1, v[i].F, v[i].S); // rep(i,0,n) // { // cout<<v[i].F<<" "<<v[i].S<<" "<<dp[i]<<"\n"; // } // cout<<endl; rep(i, 0, n) { rep(j, i + 1, n) { if (v[i].F > v[j].F || v[i].S > v[j].S) continue; dp[j] = sub(dp[j], mul(dp[i], fun(v[i].F, v[i].S, v[j].F, v[j].S))); } } cout << dp[n - 1] << endl; return; } int main() { init(); FAST int TESTS = 1; // cin>>TESTS; while (TESTS--) { solve(); } TIME return 0; }
replace
60
61
60
61
0
Time elapsed: 211.89ms
p03184
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, a, n) for (int i = a; i < n; i++) #define per(i, a, n) for (int i = n - 1; i >= a; i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define fi first #define se second typedef vector<int> VI; typedef long long ll; typedef pair<int, int> PII; const ll mod = 1000000007; ll powmod(ll a, ll b) { ll res = 1; a %= mod; for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } // head PII p[2010]; ll dp[2010], f[201000]; int h, w, n; ll go(ll x, ll y) { assert(x >= 0 && y >= 0); return f[x + y] * powmod(f[x] * f[y], mod - 2) % mod; } int main() { scanf("%d%d%d", &h, &w, &n); f[0] = 1; rep(i, 1, 200001) f[i] = f[i - 1] * i % mod; rep(i, 0, n) scanf("%d%d", &p[i].fi, &p[i].se); p[n] = mp(h, w); p[n + 1] = mp(1, 1); n += 2; sort(p, p + n); dp[0] = 1; rep(i, 1, n) { dp[i] = go(p[i].fi - 1, p[i].se - 1); rep(j, 1, i) if (p[j].fi <= p[i].fi && p[j].se <= p[i].se) dp[i] = (dp[i] - go(p[i].fi - p[j].fi, p[i].se - p[j].se) * dp[j]) % mod; } (dp[n - 1] += mod) %= mod; printf("%I64d\n", dp[n - 1]); }
#include <bits/stdc++.h> using namespace std; #define rep(i, a, n) for (int i = a; i < n; i++) #define per(i, a, n) for (int i = n - 1; i >= a; i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define fi first #define se second typedef vector<int> VI; typedef long long ll; typedef pair<int, int> PII; const ll mod = 1000000007; ll powmod(ll a, ll b) { ll res = 1; a %= mod; for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } // head PII p[3010]; ll dp[3010], f[201000]; int h, w, n; ll go(ll x, ll y) { assert(x >= 0 && y >= 0); return f[x + y] * powmod(f[x] * f[y], mod - 2) % mod; } int main() { scanf("%d%d%d", &h, &w, &n); f[0] = 1; rep(i, 1, 200001) f[i] = f[i - 1] * i % mod; rep(i, 0, n) scanf("%d%d", &p[i].fi, &p[i].se); p[n] = mp(h, w); p[n + 1] = mp(1, 1); n += 2; sort(p, p + n); dp[0] = 1; rep(i, 1, n) { dp[i] = go(p[i].fi - 1, p[i].se - 1); rep(j, 1, i) if (p[j].fi <= p[i].fi && p[j].se <= p[i].se) dp[i] = (dp[i] - go(p[i].fi - p[j].fi, p[i].se - p[j].se) * dp[j]) % mod; } (dp[n - 1] += mod) %= mod; printf("%I64d\n", dp[n - 1]); }
replace
26
28
26
28
0
p03184
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ff first #define ss second typedef long long ll; typedef long double ld; typedef pair<int, int> pi; typedef pair<long long, long long> pl; const int MOD = 1e9 + 7; const ll INF = 1e18; const double EPS = 1e-6; const int MAX_H = 1e5 + 5; const int MAX_N = 1e3 + 5; ll fastExpo(int base, int e) { if (e == 0) return 1; if (e == 1) return base; ll ans = fastExpo(base, e / 2); ans = ans * ans % MOD; if (e % 2 == 1) ans = ans * base % MOD; return ans; } namespace modOp { int add(int a, int b, int mod = MOD) { return (a + b) % mod; } int sub(int a, int b, int mod = MOD) { return (a - b + mod) % mod; } int mul(int a, int b, int mod = MOD) { return (ll)a * b % mod; } int divide(int a, int b, int mod = MOD) { return (ll)a * fastExpo(b, mod - 2) % mod; } } // namespace modOp using namespace modOp; int H, W, N; pi walls[MAX_N]; int dp[MAX_N]; ll fac[2 * MAX_H], inv[2 * MAX_H]; int f(int a, int b) { a += b; if (a < 0 || b < 0 || b > a) return 0; return mul(fac[a], mul(inv[b], inv[a - b])); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); fac[0] = inv[0] = 1; for (int i = 1; i < 2 * MAX_H; ++i) { fac[i] = mul(fac[i - 1], i); inv[i] = divide(1, fac[i]); } cin >> H >> W >> N; for (int i = 0; i < N; ++i) { cin >> walls[i].ff >> walls[i].ss; } walls[N] = {H, W}; sort(walls, walls + N + 1, [](pi a, pi b) { return a.ff + a.ss < b.ff + b.ss; }); for (int i = 0; i <= N; ++i) { dp[i] = f(walls[i].ff - 1, walls[i].ss - 1); for (int j = 0; j < i; ++j) { if (walls[i].ff >= walls[j].ff && walls[i].ss >= walls[j].ss) { dp[i] = sub(dp[i], mul(dp[j], f(walls[i].ff - walls[j].ff, walls[i].ss - walls[j].ss))); } } } cout << dp[N] << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; #define ff first #define ss second typedef long long ll; typedef long double ld; typedef pair<int, int> pi; typedef pair<long long, long long> pl; const int MOD = 1e9 + 7; const ll INF = 1e18; const double EPS = 1e-6; const int MAX_H = 1e5 + 5; const int MAX_N = 3e3 + 5; ll fastExpo(int base, int e) { if (e == 0) return 1; if (e == 1) return base; ll ans = fastExpo(base, e / 2); ans = ans * ans % MOD; if (e % 2 == 1) ans = ans * base % MOD; return ans; } namespace modOp { int add(int a, int b, int mod = MOD) { return (a + b) % mod; } int sub(int a, int b, int mod = MOD) { return (a - b + mod) % mod; } int mul(int a, int b, int mod = MOD) { return (ll)a * b % mod; } int divide(int a, int b, int mod = MOD) { return (ll)a * fastExpo(b, mod - 2) % mod; } } // namespace modOp using namespace modOp; int H, W, N; pi walls[MAX_N]; int dp[MAX_N]; ll fac[2 * MAX_H], inv[2 * MAX_H]; int f(int a, int b) { a += b; if (a < 0 || b < 0 || b > a) return 0; return mul(fac[a], mul(inv[b], inv[a - b])); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); fac[0] = inv[0] = 1; for (int i = 1; i < 2 * MAX_H; ++i) { fac[i] = mul(fac[i - 1], i); inv[i] = divide(1, fac[i]); } cin >> H >> W >> N; for (int i = 0; i < N; ++i) { cin >> walls[i].ff >> walls[i].ss; } walls[N] = {H, W}; sort(walls, walls + N + 1, [](pi a, pi b) { return a.ff + a.ss < b.ff + b.ss; }); for (int i = 0; i <= N; ++i) { dp[i] = f(walls[i].ff - 1, walls[i].ss - 1); for (int j = 0; j < i; ++j) { if (walls[i].ff >= walls[j].ff && walls[i].ss >= walls[j].ss) { dp[i] = sub(dp[i], mul(dp[j], f(walls[i].ff - walls[j].ff, walls[i].ss - walls[j].ss))); } } } cout << dp[N] << '\n'; return 0; }
replace
16
17
16
17
0
p03184
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define what_is(x) cerr << #x << " is " << x << endl; #define IOS \ ios::sync_with_stdio(false); \ cin.tie(0); typedef long long ll; const int N = 3e3 + 5; const int INF = 1e9; const int MOD = 1e9 + 7; int main() { IOS freopen("input.txt", "r", stdin); int h, w, n; cin >> h >> w >> n; vector<pair<int, int>> bl(n); // block for (int i = 0; i < n; i++) { cin >> bl[i].first >> bl[i].second; bl[i].first--; // size ak bl[i].second--; } bl.emplace_back(h - 1, w - 1); n++; sort(bl.begin(), bl.end()); vector<ll> inv(h + w + 1); // inverse: phan so vector<ll> fact(h + w + 1); vector<ll> fact_inv(h + w + 1); inv[1] = 1; for (int i = 2; i < h + w + 1; i++) { inv[i] = MOD - (MOD / i) * inv[MOD % i] % MOD; // 1/n } fact[0] = 1; // n! fact_inv[0] = 1; // 1/n! for (int i = 1; i < h + w + 1; i++) { fact[i] = fact[i - 1] * i % MOD; fact_inv[i] = fact_inv[i - 1] * inv[i] % MOD; } auto comb = [&](int n, int k) { if (n < 0 || k < 0) return 0LL; if (n < k) return 0LL; return fact[n] * fact_inv[n - k] % MOD * fact_inv[k] % MOD; }; auto f = [&](int x, int y) { return comb(x + y, y); }; vector<ll> dp(n); for (int i = 0; i < n; i++) { dp[i] = f(bl[i].first, bl[i].second); for (int j = 0; j < i; j++) { if (bl[i].second >= bl[j].second) { dp[i] = (dp[i] - dp[j] * f(bl[i].first - bl[j].first, bl[i].second - bl[j].second) % MOD + MOD) % MOD; } } } cout << dp[n - 1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define what_is(x) cerr << #x << " is " << x << endl; #define IOS \ ios::sync_with_stdio(false); \ cin.tie(0); typedef long long ll; const int N = 3e3 + 5; const int INF = 1e9; const int MOD = 1e9 + 7; int main() { IOS // freopen("input.txt", "r", stdin); int h, w, n; cin >> h >> w >> n; vector<pair<int, int>> bl(n); // block for (int i = 0; i < n; i++) { cin >> bl[i].first >> bl[i].second; bl[i].first--; // size ak bl[i].second--; } bl.emplace_back(h - 1, w - 1); n++; sort(bl.begin(), bl.end()); vector<ll> inv(h + w + 1); // inverse: phan so vector<ll> fact(h + w + 1); vector<ll> fact_inv(h + w + 1); inv[1] = 1; for (int i = 2; i < h + w + 1; i++) { inv[i] = MOD - (MOD / i) * inv[MOD % i] % MOD; // 1/n } fact[0] = 1; // n! fact_inv[0] = 1; // 1/n! for (int i = 1; i < h + w + 1; i++) { fact[i] = fact[i - 1] * i % MOD; fact_inv[i] = fact_inv[i - 1] * inv[i] % MOD; } auto comb = [&](int n, int k) { if (n < 0 || k < 0) return 0LL; if (n < k) return 0LL; return fact[n] * fact_inv[n - k] % MOD * fact_inv[k] % MOD; }; auto f = [&](int x, int y) { return comb(x + y, y); }; vector<ll> dp(n); for (int i = 0; i < n; i++) { dp[i] = f(bl[i].first, bl[i].second); for (int j = 0; j < i; j++) { if (bl[i].second >= bl[j].second) { dp[i] = (dp[i] - dp[j] * f(bl[i].first - bl[j].first, bl[i].second - bl[j].second) % MOD + MOD) % MOD; } } } cout << dp[n - 1] << endl; return 0; }
replace
12
14
12
16
0
p03184
C++
Runtime Error
#define _USE_MATH_DEFINES #include "bits/stdc++.h" using namespace std; #define FOR(i, j, k) for (int(i) = (j); (i) < (int)(k); ++(i)) #define rep(i, j) FOR(i, 0, j) #define each(x, y) for (auto &(x) : (y)) #define mp make_pair #define MT make_tuple #define all(x) (x).begin(), (x).end() #define debug(x) cout << #x << ": " << (x) << endl #define smax(x, y) (x) = max((x), (y)) #define smin(x, y) (x) = min((x), (y)) #define MEM(x, y) memset((x), (y), sizeof(x)) #define sz(x) (int)(x).size() #define RT return using ll = long long; using pii = pair<int, int>; using vi = vector<int>; using vll = vector<ll>; template <int MOD> class ModInt { public: ModInt() : value(0) {} ModInt(long long val) : value((int)(val < 0 ? MOD + val % MOD : val % MOD)) {} ModInt &operator+=(ModInt that) { value = value + that.value; if (value >= MOD) value -= MOD; return *this; } ModInt &operator-=(ModInt that) { value -= that.value; if (value < 0) value += MOD; return *this; } ModInt &operator*=(ModInt that) { value = (int)((long long)value * that.value % MOD); return *this; } ModInt &operator/=(ModInt that) { return *this *= that.inverse(); } ModInt operator+(ModInt that) const { return ModInt(*this) += that; } ModInt operator-(ModInt that) const { return ModInt(*this) -= that; } ModInt operator*(ModInt that) const { return ModInt(*this) *= that; } ModInt operator/(ModInt that) const { return ModInt(*this) /= that; } ModInt pow(long long k) const { ModInt n = *this, res = 1; while (k) { if (k & 1) res *= n; n *= n; k >>= 1; } return res; } ModInt inverse() const { long long a = value, b = MOD, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } return ModInt(u); } int toi() const { return value; } private: int value; }; typedef ModInt<1000000007> mint; ostream &operator<<(ostream &os, const mint &x) { os << x.toi(); return os; } template <class S, class T> void psort(vector<S> &u, vector<T> &v, bool isGreater = false) { int n = (int)u.size(); vector<pair<S, T>> vecP(n); for (int i = 0; i < n; ++i) { vecP[i].first = u[i]; vecP[i].second = v[i]; } if (isGreater) { sort(vecP.rbegin(), vecP.rend()); } else { sort(vecP.begin(), vecP.end()); } for (int i = 0; i < n; ++i) { u[i] = vecP[i].first; v[i] = vecP[i].second; } } namespace comb { const int N = 200005; mint fact[N]; mint rev[N]; void init() { fact[0] = 1; for (int i = 1; i < N; ++i) fact[i] = fact[i - 1] * i; for (int i = 0; i < N; ++i) rev[i] = fact[i].inverse(); } mint C(int n, int r) { if (n < r) return 0; return fact[n] * rev[r] * rev[n - r]; } mint H(int n, int r) { return C(n + r - 1, r); } mint P(int n, int r) { assert(n >= r); return fact[n] * rev[n - r]; } mint catalan(int n) { return fact[2 * n] * rev[n + 1] * rev[n]; } } // namespace comb void solve() { comb::init(); int H, W, N; cin >> H >> W >> N; vi R(N + 2), C(N + 2); rep(i, N) { cin >> R[i + 1] >> C[i + 1]; } R[0] = C[0] = 1; R[N + 1] = H; C[N + 1] = W; psort(R, C); static mint dp[1003][2]; dp[0][1] = 1; rep(i, N + 1) { for (int j = i + 1; j <= N + 1; ++j) { int h = R[j] - R[i]; int w = C[j] - C[i]; if (h >= 0 && w >= 0) { rep(k, 2) { dp[j][k ^ 1] += dp[i][k] * comb::C(h + w, h); } } } } mint ans = dp[N + 1][0] - dp[N + 1][1]; cout << ans << endl; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(15); solve(); return 0; }
#define _USE_MATH_DEFINES #include "bits/stdc++.h" using namespace std; #define FOR(i, j, k) for (int(i) = (j); (i) < (int)(k); ++(i)) #define rep(i, j) FOR(i, 0, j) #define each(x, y) for (auto &(x) : (y)) #define mp make_pair #define MT make_tuple #define all(x) (x).begin(), (x).end() #define debug(x) cout << #x << ": " << (x) << endl #define smax(x, y) (x) = max((x), (y)) #define smin(x, y) (x) = min((x), (y)) #define MEM(x, y) memset((x), (y), sizeof(x)) #define sz(x) (int)(x).size() #define RT return using ll = long long; using pii = pair<int, int>; using vi = vector<int>; using vll = vector<ll>; template <int MOD> class ModInt { public: ModInt() : value(0) {} ModInt(long long val) : value((int)(val < 0 ? MOD + val % MOD : val % MOD)) {} ModInt &operator+=(ModInt that) { value = value + that.value; if (value >= MOD) value -= MOD; return *this; } ModInt &operator-=(ModInt that) { value -= that.value; if (value < 0) value += MOD; return *this; } ModInt &operator*=(ModInt that) { value = (int)((long long)value * that.value % MOD); return *this; } ModInt &operator/=(ModInt that) { return *this *= that.inverse(); } ModInt operator+(ModInt that) const { return ModInt(*this) += that; } ModInt operator-(ModInt that) const { return ModInt(*this) -= that; } ModInt operator*(ModInt that) const { return ModInt(*this) *= that; } ModInt operator/(ModInt that) const { return ModInt(*this) /= that; } ModInt pow(long long k) const { ModInt n = *this, res = 1; while (k) { if (k & 1) res *= n; n *= n; k >>= 1; } return res; } ModInt inverse() const { long long a = value, b = MOD, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } return ModInt(u); } int toi() const { return value; } private: int value; }; typedef ModInt<1000000007> mint; ostream &operator<<(ostream &os, const mint &x) { os << x.toi(); return os; } template <class S, class T> void psort(vector<S> &u, vector<T> &v, bool isGreater = false) { int n = (int)u.size(); vector<pair<S, T>> vecP(n); for (int i = 0; i < n; ++i) { vecP[i].first = u[i]; vecP[i].second = v[i]; } if (isGreater) { sort(vecP.rbegin(), vecP.rend()); } else { sort(vecP.begin(), vecP.end()); } for (int i = 0; i < n; ++i) { u[i] = vecP[i].first; v[i] = vecP[i].second; } } namespace comb { const int N = 200005; mint fact[N]; mint rev[N]; void init() { fact[0] = 1; for (int i = 1; i < N; ++i) fact[i] = fact[i - 1] * i; for (int i = 0; i < N; ++i) rev[i] = fact[i].inverse(); } mint C(int n, int r) { if (n < r) return 0; return fact[n] * rev[r] * rev[n - r]; } mint H(int n, int r) { return C(n + r - 1, r); } mint P(int n, int r) { assert(n >= r); return fact[n] * rev[n - r]; } mint catalan(int n) { return fact[2 * n] * rev[n + 1] * rev[n]; } } // namespace comb void solve() { comb::init(); int H, W, N; cin >> H >> W >> N; vi R(N + 2), C(N + 2); rep(i, N) { cin >> R[i + 1] >> C[i + 1]; } R[0] = C[0] = 1; R[N + 1] = H; C[N + 1] = W; psort(R, C); static mint dp[3003][2]; dp[0][1] = 1; rep(i, N + 1) { for (int j = i + 1; j <= N + 1; ++j) { int h = R[j] - R[i]; int w = C[j] - C[i]; if (h >= 0 && w >= 0) { rep(k, 2) { dp[j][k ^ 1] += dp[i][k] * comb::C(h + w, h); } } } } mint ans = dp[N + 1][0] - dp[N + 1][1]; cout << ans << endl; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(15); solve(); return 0; }
replace
140
141
140
141
0
p03184
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<ll> vi; typedef vector<vi> vvi; #define ff first #define ss second #define pb push_back #define all(s) (s).begin(), (s).end() const ll mod = 1e9 + 7; ll h, w, x; vector<pair<ll, ll>> arr; vi ans, fact; ll binexp(ll a) { ll res = 1, y = mod - 2; while (y > 0) { if (y & 1) res = ((res * a) % mod + mod) % mod; y >>= 1; a = ((a * a) % mod + mod) % mod; } return res % mod; } ll num(ll x1, ll y1, ll x2, ll y2) { ll x = fact[x2 - x1 + y2 - y1]; ll y = binexp(((fact[x2 - x1] * fact[y2 - y1]) % mod + mod) % mod); return ((x * y) % mod + mod) % mod; } void solve() { for (int i = 2; i <= h + w + 1; i++) fact[i] = ((fact[i - 1] * i) % mod + mod) % mod; for (int i = 0; i <= x; i++) { ll ans_i = num(1, 1, arr[i].ff, arr[i].ss); ll ans_t = 0; for (int j = 0; j < i; j++) { if (arr[j].ff <= arr[i].ff && arr[j].ss <= arr[i].ss) { ans_t += (ans[j] * num(arr[j].ff, arr[j].ss, arr[i].ff, arr[i].ss) + mod) % mod; ans_t %= mod; } } ans[i] = (ans_i - ans_t) % mod + mod; ans[i] %= mod; } } signed main() { ios_base::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr); cin >> h >> w >> x; arr.resize(x); ans.resize(x + 1); fact.resize(h + w + 1); fact[0] = fact[1] = 1; for (int i = 0; i < x; i++) { ll a, b; cin >> a >> b; arr[i] = make_pair(a, b); } sort(all(arr)); arr.pb({h, w}); solve(); cout << ans[x] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<ll> vi; typedef vector<vi> vvi; #define ff first #define ss second #define pb push_back #define all(s) (s).begin(), (s).end() const ll mod = 1e9 + 7; ll h, w, x; vector<pair<ll, ll>> arr; vi ans, fact; ll binexp(ll a) { ll res = 1, y = mod - 2; while (y > 0) { if (y & 1) res = ((res * a) % mod + mod) % mod; y >>= 1; a = ((a * a) % mod + mod) % mod; } return res % mod; } ll num(ll x1, ll y1, ll x2, ll y2) { ll x = fact[x2 - x1 + y2 - y1]; ll y = binexp(((fact[x2 - x1] * fact[y2 - y1]) % mod + mod) % mod); return ((x * y) % mod + mod) % mod; } void solve() { for (int i = 2; i <= h + w; i++) fact[i] = ((fact[i - 1] * i) % mod + mod) % mod; for (int i = 0; i <= x; i++) { ll ans_i = num(1, 1, arr[i].ff, arr[i].ss); ll ans_t = 0; for (int j = 0; j < i; j++) { if (arr[j].ff <= arr[i].ff && arr[j].ss <= arr[i].ss) { ans_t += (ans[j] * num(arr[j].ff, arr[j].ss, arr[i].ff, arr[i].ss) + mod) % mod; ans_t %= mod; } } ans[i] = (ans_i - ans_t) % mod + mod; ans[i] %= mod; } } signed main() { ios_base::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr); cin >> h >> w >> x; arr.resize(x); ans.resize(x + 1); fact.resize(h + w + 1); fact[0] = fact[1] = 1; for (int i = 0; i < x; i++) { ll a, b; cin >> a >> b; arr[i] = make_pair(a, b); } sort(all(arr)); arr.pb({h, w}); solve(); cout << ans[x] << endl; return 0; }
replace
33
34
33
34
0
p03184
C++
Runtime Error
#include <bits/stdc++.h> #define MOD 1000000007 using namespace std; typedef struct point { int x, y; bool operator<(const struct point &t) const { return x == t.x ? y < t.y : x < t.x; } } POINT, *PPOINT; int w, h; POINT arr[30002]; long long fac[200010], rev[200010], f[30002]; void init() { int i; fac[0] = fac[1] = rev[0] = rev[1] = 1; for (i = 2; i <= h + w - 2; i++) { fac[i] = fac[i - 1] * i % MOD; rev[i] = (MOD - MOD / i) * rev[MOD % i] % MOD; } for (i = 2; i <= h + w - 2; i++) rev[i] = rev[i - 1] * rev[i] % MOD; } int main() { int i, j, n; scanf("%d %d %d", &h, &w, &n); for (i = 0; i < n; i++) scanf("%d %d", &arr[i].x, &arr[i].y); arr[n].x = h; arr[n].y = w; sort(arr, arr + n + 1); init(); for (i = 0; i <= n; i++) { f[i] = fac[arr[i].x + arr[i].y - 2] * rev[arr[i].x - 1] % MOD * rev[arr[i].y - 1] % MOD; // printf("%d %d\n",i,f[i]); for (j = 0; j < i; j++) { f[i] = (f[i] - f[j] * fac[arr[i].x - arr[j].x - arr[j].y + arr[i].y] % MOD * rev[arr[i].x - arr[j].x] % MOD * rev[arr[i].y - arr[j].y] % MOD); } f[i] = (f[i] + MOD) % MOD; } printf("%lld\n", (f[n] + MOD) % MOD); return 0; }
#include <bits/stdc++.h> #define MOD 1000000007 using namespace std; typedef struct point { int x, y; bool operator<(const struct point &t) const { return x == t.x ? y < t.y : x < t.x; } } POINT, *PPOINT; int w, h; POINT arr[200002]; long long fac[200010], rev[200010], f[200002]; void init() { int i; fac[0] = fac[1] = rev[0] = rev[1] = 1; for (i = 2; i <= h + w - 2; i++) { fac[i] = fac[i - 1] * i % MOD; rev[i] = (MOD - MOD / i) * rev[MOD % i] % MOD; } for (i = 2; i <= h + w - 2; i++) rev[i] = rev[i - 1] * rev[i] % MOD; } int main() { int i, j, n; scanf("%d %d %d", &h, &w, &n); for (i = 0; i < n; i++) scanf("%d %d", &arr[i].x, &arr[i].y); arr[n].x = h; arr[n].y = w; sort(arr, arr + n + 1); init(); for (i = 0; i <= n; i++) { f[i] = fac[arr[i].x + arr[i].y - 2] * rev[arr[i].x - 1] % MOD * rev[arr[i].y - 1] % MOD; // printf("%d %d\n",i,f[i]); for (j = 0; j < i; j++) { f[i] = (f[i] - f[j] * fac[arr[i].x - arr[j].x - arr[j].y + arr[i].y] % MOD * rev[arr[i].x - arr[j].x] % MOD * rev[arr[i].y - arr[j].y] % MOD); } f[i] = (f[i] + MOD) % MOD; } printf("%lld\n", (f[n] + MOD) % MOD); return 0; }
replace
10
12
10
12
0
p03184
C++
Runtime Error
#include <bits/stdc++.h> #define ff first #define ss second #define pb push_back #define mp make_pair #define len(s) s.length() #define forp(i, a, b) for (i = a; i <= b; i++) #define FOR(tt, a, b) for (int tt = a; tt <= b; tt++) #define rep(i, n) for (i = 0; i < n; i++) #define ren(i, n) for (i = n - 1; i >= 0; i--) #define forn(i, a, b) for (i = a; i >= b; i--) #define all(v) v.begin(), v.end() #define b(v) v.begin() #define e(v) v.end() #define mem(n, m) memset(n, m, sizeof(n)) #define lb lower_bound #define ub upper_bound #define pii pair<int, int> #define pll pair<long long, long long> #define vii vector<int> #define vll vector<long long> #define gl(cin, s) getline(cin, s); #define bitc(n) __builtin_popcountll(n) #define present(s, x) (s.find(x) != s.end()) #define cpresent(s, x) (find(all(s), x) != s.end()) #define tr(container, it) \ for (__typeof(container.begin()) it = container.begin(); \ it != container.end(); it++) #define boost ios_base::sync_with_stdio(0) #define MOD 1000000007 #define EPSILON 1e-9 #define PI 3.14159265358979323846 #define SIZE 200001 typedef long long ll; typedef unsigned long long ull; typedef long double ldo; typedef double db; using namespace std; pair<int, int> a[2001]; ll path[2001]; /* Factorial */ long long fact[SIZE]; void f() // Perfect { fact[0] = 1; for (long long i = 1; i < SIZE; i++) { fact[i] = fact[i - 1] * i; fact[i] %= MOD; } } /* Multiplying 2 Long long Numbers */ inline long long mulmod(long long a, long long b, long long mod) { a %= mod; long long res = 0LL; while (b) { if (b % 2) { res = (res + a) % mod; } a = (a * 2) % mod; b /= 2; } return res; } /*Function For Calculating Exponent*/ inline long long expo(long long a, long long b, long long mod) { a %= mod; long long res = 1LL; while (b) { if (b % 2) { // res = mulmod(res,a,mod); res = (res * a) % mod; } // a = mulmod(a,a,mod); a = (a * a) % mod; b /= 2; } return res; } /*Division */ inline long long mod_div(long long a, long long b, long long mod) { return (a * expo(b, mod - 2, mod)) % mod; } int main() { /* #ifndef ONLINE_JUDGE freopen(fi, "r", stdin); #endif */ // freopen("route.in","r",stdin); // freopen("route.out","w",stdout); // cin.ignore(); // cin.clear(); boost; // cin.tie(0); // cout<<"Case #"<<tt<<": "; f(); int i, n, m, p, x, y, j; int T; T = 1; while (T--) { cin >> n >> m >> p; swap(n, m); forp(i, 1, p) { cin >> x >> y; swap(x, y); a[i] = mp(x, y); } sort(a + 1, a + 1 + p); ll sum = 0; forp(i, 1, p) { x = a[i].ff; y = a[i].ss; ll val = mod_div(fact[abs(x - 1) + abs(y - 1)], (fact[abs(x - 1)] * fact[abs(y - 1)]) % MOD, MOD); forp(j, 1, i - 1) { if (a[j].ff <= x and a[j].ss <= y) { ll val2 = mod_div( fact[abs(x - a[j].ff) + abs(y - a[j].ss)], (fact[abs(x - a[j].ff)] * fact[abs(y - a[j].ss)]) % MOD, MOD); val2 = (val2 * path[j]) % MOD; val -= val2; while (val < 0) val += MOD; } } path[i] = val; // cout<<val<<endl; val = (val * mod_div(fact[abs(x - n) + abs(y - m)], (fact[abs(x - n)] * fact[abs(y - m)]) % MOD, MOD)) % MOD; sum = (sum + val) % MOD; } ll FANS = mod_div(fact[n + m - 2], (fact[n - 1] * fact[m - 1]) % MOD, MOD); FANS -= sum; while (FANS < 0) FANS += MOD; cout << FANS << endl; } return 0; }
#include <bits/stdc++.h> #define ff first #define ss second #define pb push_back #define mp make_pair #define len(s) s.length() #define forp(i, a, b) for (i = a; i <= b; i++) #define FOR(tt, a, b) for (int tt = a; tt <= b; tt++) #define rep(i, n) for (i = 0; i < n; i++) #define ren(i, n) for (i = n - 1; i >= 0; i--) #define forn(i, a, b) for (i = a; i >= b; i--) #define all(v) v.begin(), v.end() #define b(v) v.begin() #define e(v) v.end() #define mem(n, m) memset(n, m, sizeof(n)) #define lb lower_bound #define ub upper_bound #define pii pair<int, int> #define pll pair<long long, long long> #define vii vector<int> #define vll vector<long long> #define gl(cin, s) getline(cin, s); #define bitc(n) __builtin_popcountll(n) #define present(s, x) (s.find(x) != s.end()) #define cpresent(s, x) (find(all(s), x) != s.end()) #define tr(container, it) \ for (__typeof(container.begin()) it = container.begin(); \ it != container.end(); it++) #define boost ios_base::sync_with_stdio(0) #define MOD 1000000007 #define EPSILON 1e-9 #define PI 3.14159265358979323846 #define SIZE 200001 typedef long long ll; typedef unsigned long long ull; typedef long double ldo; typedef double db; using namespace std; pair<int, int> a[3001]; ll path[3001]; /* Factorial */ long long fact[SIZE]; void f() // Perfect { fact[0] = 1; for (long long i = 1; i < SIZE; i++) { fact[i] = fact[i - 1] * i; fact[i] %= MOD; } } /* Multiplying 2 Long long Numbers */ inline long long mulmod(long long a, long long b, long long mod) { a %= mod; long long res = 0LL; while (b) { if (b % 2) { res = (res + a) % mod; } a = (a * 2) % mod; b /= 2; } return res; } /*Function For Calculating Exponent*/ inline long long expo(long long a, long long b, long long mod) { a %= mod; long long res = 1LL; while (b) { if (b % 2) { // res = mulmod(res,a,mod); res = (res * a) % mod; } // a = mulmod(a,a,mod); a = (a * a) % mod; b /= 2; } return res; } /*Division */ inline long long mod_div(long long a, long long b, long long mod) { return (a * expo(b, mod - 2, mod)) % mod; } int main() { /* #ifndef ONLINE_JUDGE freopen(fi, "r", stdin); #endif */ // freopen("route.in","r",stdin); // freopen("route.out","w",stdout); // cin.ignore(); // cin.clear(); boost; // cin.tie(0); // cout<<"Case #"<<tt<<": "; f(); int i, n, m, p, x, y, j; int T; T = 1; while (T--) { cin >> n >> m >> p; swap(n, m); forp(i, 1, p) { cin >> x >> y; swap(x, y); a[i] = mp(x, y); } sort(a + 1, a + 1 + p); ll sum = 0; forp(i, 1, p) { x = a[i].ff; y = a[i].ss; ll val = mod_div(fact[abs(x - 1) + abs(y - 1)], (fact[abs(x - 1)] * fact[abs(y - 1)]) % MOD, MOD); forp(j, 1, i - 1) { if (a[j].ff <= x and a[j].ss <= y) { ll val2 = mod_div( fact[abs(x - a[j].ff) + abs(y - a[j].ss)], (fact[abs(x - a[j].ff)] * fact[abs(y - a[j].ss)]) % MOD, MOD); val2 = (val2 * path[j]) % MOD; val -= val2; while (val < 0) val += MOD; } } path[i] = val; // cout<<val<<endl; val = (val * mod_div(fact[abs(x - n) + abs(y - m)], (fact[abs(x - n)] * fact[abs(y - m)]) % MOD, MOD)) % MOD; sum = (sum + val) % MOD; } ll FANS = mod_div(fact[n + m - 2], (fact[n - 1] * fact[m - 1]) % MOD, MOD); FANS -= sum; while (FANS < 0) FANS += MOD; cout << FANS << endl; } return 0; }
replace
41
43
41
43
0
p03184
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; template <typename T> void read(T &t) { t = 0; char ch = getchar(); int f = 1; while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } do { (t *= 10) += ch - '0'; ch = getchar(); } while ('0' <= ch && ch <= '9'); t *= f; } typedef long long ll; const ll mod = (1e9) + 7; const int maxn = 2010; const int maxm = (2e5) + 10; int n, h, w, x[maxn], y[maxn]; ll dp[maxn]; struct node { int x, y; } D[maxn]; ll iv[maxm], ivjc[maxm], jc[maxm]; bool cmp(node a, node b) { if (a.x == b.x) return a.y < b.y; return a.x < b.x; } ll C(int x, int y) { x += y; return jc[x] * ivjc[y] % mod * ivjc[x - y] % mod; } void update(ll &x, ll y) { x += y; if (x >= mod) x -= mod; } int main() { // freopen("1.txt","r",stdin); jc[0] = iv[0] = ivjc[0] = 1; jc[1] = iv[1] = ivjc[1] = 1; for (int i = 2; i < maxm; i++) { jc[i] = jc[i - 1] * i % mod; iv[i] = (mod - mod / i) * iv[mod % i] % mod; ivjc[i] = ivjc[i - 1] * iv[i] % mod; } read(h); read(w); read(n); for (int i = 1; i <= n; i++) read(x[i]), read(y[i]); n += 2; x[n - 1] = 1; y[n - 1] = 1; x[n] = h; y[n] = w; for (int i = 1; i <= n; i++) D[i] = (node){x[i], y[i]}; sort(D + 1, D + n + 1, cmp); for (int i = 1; i <= n; i++) x[i] = D[i].x, y[i] = D[i].y; dp[1] = 1; for (int i = 1; i <= n; i++) { for (int j = 1; j < i; j++) if (y[j] <= y[i]) update(dp[i], mod - dp[j] * C(x[i] - x[j], y[i] - y[j]) % mod); } printf("%lld\n", (mod - dp[n]) % mod); return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T> void read(T &t) { t = 0; char ch = getchar(); int f = 1; while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } do { (t *= 10) += ch - '0'; ch = getchar(); } while ('0' <= ch && ch <= '9'); t *= f; } typedef long long ll; const ll mod = (1e9) + 7; const int maxn = 3010; const int maxm = (2e5) + 10; int n, h, w, x[maxn], y[maxn]; ll dp[maxn]; struct node { int x, y; } D[maxn]; ll iv[maxm], ivjc[maxm], jc[maxm]; bool cmp(node a, node b) { if (a.x == b.x) return a.y < b.y; return a.x < b.x; } ll C(int x, int y) { x += y; return jc[x] * ivjc[y] % mod * ivjc[x - y] % mod; } void update(ll &x, ll y) { x += y; if (x >= mod) x -= mod; } int main() { // freopen("1.txt","r",stdin); jc[0] = iv[0] = ivjc[0] = 1; jc[1] = iv[1] = ivjc[1] = 1; for (int i = 2; i < maxm; i++) { jc[i] = jc[i - 1] * i % mod; iv[i] = (mod - mod / i) * iv[mod % i] % mod; ivjc[i] = ivjc[i - 1] * iv[i] % mod; } read(h); read(w); read(n); for (int i = 1; i <= n; i++) read(x[i]), read(y[i]); n += 2; x[n - 1] = 1; y[n - 1] = 1; x[n] = h; y[n] = w; for (int i = 1; i <= n; i++) D[i] = (node){x[i], y[i]}; sort(D + 1, D + n + 1, cmp); for (int i = 1; i <= n; i++) x[i] = D[i].x, y[i] = D[i].y; dp[1] = 1; for (int i = 1; i <= n; i++) { for (int j = 1; j < i; j++) if (y[j] <= y[i]) update(dp[i], mod - dp[j] * C(x[i] - x[j], y[i] - y[j]) % mod); } printf("%lld\n", (mod - dp[n]) % mod); return 0; }
replace
19
20
19
20
0
p03184
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int MOD = 1e9 + 7; int dp[2001]; int fact[200001]; int tcaf[200001]; int pow1(int n, int p) { ll cur, res; cur = n; res = 1; while (p) { if (p & 1) { res *= cur; res %= MOD; } p /= 2; cur *= cur; cur %= MOD; } return res; } void init() { fact[0] = tcaf[0] = 1; for (int i = 1; i < 200001; i++) { fact[i] = 1LL * fact[i - 1] * i % MOD; tcaf[i] = pow1(fact[i], MOD - 2); } } int ways(pair<int, int> p1, pair<int, int> p2) { int n, m; n = p2.first - p1.first; m = p2.second - p1.second; if (n < 0 || m < 0) return 0; return 1LL * fact[n + m] * tcaf[n] % MOD * tcaf[m] % MOD; } int main() { // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); init(); int h, w, n; cin >> h >> w >> n; dp[0] = 1; vector<pair<int, int>> points(n + 2); points[n + 1] = {h - 1, w - 1}; for (int i = 1; i <= n; i++) { cin >> points[i].first >> points[i].second; points[i].first--; points[i].second--; } sort(points.begin(), points.end()); for (int i = 1; i <= n + 1; i++) { dp[i] = ways(points[0], points[i]); for (int j = 1; j < i; j++) { dp[i] -= 1LL * dp[j] * ways(points[j], points[i]) % MOD; dp[i] %= MOD; } dp[i] = (dp[i] + MOD) % MOD; } cout << dp[n + 1]; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int MOD = 1e9 + 7; int dp[3001]; int fact[200001]; int tcaf[200001]; int pow1(int n, int p) { ll cur, res; cur = n; res = 1; while (p) { if (p & 1) { res *= cur; res %= MOD; } p /= 2; cur *= cur; cur %= MOD; } return res; } void init() { fact[0] = tcaf[0] = 1; for (int i = 1; i < 200001; i++) { fact[i] = 1LL * fact[i - 1] * i % MOD; tcaf[i] = pow1(fact[i], MOD - 2); } } int ways(pair<int, int> p1, pair<int, int> p2) { int n, m; n = p2.first - p1.first; m = p2.second - p1.second; if (n < 0 || m < 0) return 0; return 1LL * fact[n + m] * tcaf[n] % MOD * tcaf[m] % MOD; } int main() { // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); init(); int h, w, n; cin >> h >> w >> n; dp[0] = 1; vector<pair<int, int>> points(n + 2); points[n + 1] = {h - 1, w - 1}; for (int i = 1; i <= n; i++) { cin >> points[i].first >> points[i].second; points[i].first--; points[i].second--; } sort(points.begin(), points.end()); for (int i = 1; i <= n + 1; i++) { dp[i] = ways(points[0], points[i]); for (int j = 1; j < i; j++) { dp[i] -= 1LL * dp[j] * ways(points[j], points[i]) % MOD; dp[i] %= MOD; } dp[i] = (dp[i] + MOD) % MOD; } cout << dp[n + 1]; return 0; }
replace
5
6
5
6
0
p03184
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define LL long long #define pii pair<int, int> #define pll pair<ll, ll> #define pil pair<int, ll> #define pli pair<ll, int> #define vi vector<int> #define vl vector<ll> #define vii vector<pii> #define vll vector<pll> #define vil vector<pil> #define vli vector<pli> #define pb push_back #define ppb pop_back #define mp make_pair #define ff first #define ss second #define sz(x) (int)x.size() #define TN typename #define all(v) v.begin(), v.end() #define fill(a, b) memset(a, b, sizeof(a)) #define endl '\n' template <TN T> T gcd(T a, T b) { return !b ? a : gcd(b, a % b); } template <TN T> T lcm(T a, T b) { return a * (b / gcd(a, b)); } template <TN T> T sqr(T a) { return a * a; } template <TN T> T cube(T a) { return a * a * a; } template <TN T> inline void smin(T &a, T b) { a = a < b ? a : b; } template <TN T> inline void smax(T &a, T b) { a = a > b ? a : b; } template <TN T> inline void Int(T &n) { n = 0; int f = 1; register int ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = -1; for (; isdigit(ch); ch = getchar()) n = (n << 3) + (n << 1) + ch - '0'; n = n * f; } #define error(args...) \ { \ vector<string> _v = split(#args, ','); \ err(_v.begin(), args); \ cout << endl; \ } vector<string> split(const string &s, char c) { vector<string> v; stringstream ss(s); string x; while (getline(ss, x, c)) v.emplace_back(x); return move(v); } void err(vector<string>::iterator it) {} template <typename T, typename... Args> void err(vector<string>::iterator it, T a, Args... args) { cout << it->substr((*it)[0] == ' ', it->length()) << " = " << a << ", "; err(++it, args...); } inline int in() { int n; scanf("%d", &n); return n; } inline ll Lin() { ll n; scanf("%lld", &n); return n; } inline double Din() { double n; scanf("%lf", &n); return n; } inline int add(int a, int b, int mod) { a += b; return a >= mod ? a - mod : a; } inline int sub(int a, int b, int mod) { a -= b; return a < 0 ? a + mod : a; } inline int mul(int a, int b, int mod) { return (ll)a * b % mod; } const int mod = (int)1e9 + 7; const ll Mod = (ll)1e9 + 7; inline int add(int a, int b) { a += b; return a >= mod ? a - mod : a; } inline int sub(int a, int b) { a -= b; return a < 0 ? a + mod : a; } inline int mul(int a, int b) { return (ll)a * b % mod; } const int inf = (int)2e9 + 5; const ll Inf = (ll)1e18 + 5; const int N = 2e5 + 5; int fac[N], inv_fac[N]; int bigmod(int a, int b) { if (b == 0) return 1; int x = bigmod(a, b / 2); x = mul(x, x); if (b & 1) x = mul(x, a); return x; } int path(int n, int m) { return mul(fac[n + m], mul(inv_fac[n], inv_fac[m])); } int solve() { int n = in(), m = in(); int k = in(); vii a(k); for (int i = 0; i < k; i++) { a[i].ff = in(), a[i].ss = in(); } sort(all(a), [&](pii x, pii y) { return x.ff + x.ss < y.ff + y.ss; }); a.pb({n, m}); vi dp(k + 1, 0); for (int i = 0; i <= k; i++) { int tem = path(a[i].ff - 1, a[i].ss - 1); for (int j = 0; j < i; j++) { tem = sub(tem, mul(dp[j], path(a[i].ff - a[j].ff, a[i].ss - a[j].ss))); } dp[i] = tem; } printf("%d\n", dp[k]); return 0; } int main() { fac[0] = inv_fac[0] = 1; for (int i = 1; i < N; i++) { fac[i] = mul(fac[i - 1], i); inv_fac[i] = bigmod(fac[i], mod - 2); } int test = 1, tc = 0; // scanf("%d", &test); while (test--) { // printf("Case %d: ", ++tc); solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define LL long long #define pii pair<int, int> #define pll pair<ll, ll> #define pil pair<int, ll> #define pli pair<ll, int> #define vi vector<int> #define vl vector<ll> #define vii vector<pii> #define vll vector<pll> #define vil vector<pil> #define vli vector<pli> #define pb push_back #define ppb pop_back #define mp make_pair #define ff first #define ss second #define sz(x) (int)x.size() #define TN typename #define all(v) v.begin(), v.end() #define fill(a, b) memset(a, b, sizeof(a)) #define endl '\n' template <TN T> T gcd(T a, T b) { return !b ? a : gcd(b, a % b); } template <TN T> T lcm(T a, T b) { return a * (b / gcd(a, b)); } template <TN T> T sqr(T a) { return a * a; } template <TN T> T cube(T a) { return a * a * a; } template <TN T> inline void smin(T &a, T b) { a = a < b ? a : b; } template <TN T> inline void smax(T &a, T b) { a = a > b ? a : b; } template <TN T> inline void Int(T &n) { n = 0; int f = 1; register int ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = -1; for (; isdigit(ch); ch = getchar()) n = (n << 3) + (n << 1) + ch - '0'; n = n * f; } #define error(args...) \ { \ vector<string> _v = split(#args, ','); \ err(_v.begin(), args); \ cout << endl; \ } vector<string> split(const string &s, char c) { vector<string> v; stringstream ss(s); string x; while (getline(ss, x, c)) v.emplace_back(x); return move(v); } void err(vector<string>::iterator it) {} template <typename T, typename... Args> void err(vector<string>::iterator it, T a, Args... args) { cout << it->substr((*it)[0] == ' ', it->length()) << " = " << a << ", "; err(++it, args...); } inline int in() { int n; scanf("%d", &n); return n; } inline ll Lin() { ll n; scanf("%lld", &n); return n; } inline double Din() { double n; scanf("%lf", &n); return n; } inline int add(int a, int b, int mod) { a += b; return a >= mod ? a - mod : a; } inline int sub(int a, int b, int mod) { a -= b; return a < 0 ? a + mod : a; } inline int mul(int a, int b, int mod) { return (ll)a * b % mod; } const int mod = (int)1e9 + 7; const ll Mod = (ll)1e9 + 7; inline int add(int a, int b) { a += b; return a >= mod ? a - mod : a; } inline int sub(int a, int b) { a -= b; return a < 0 ? a + mod : a; } inline int mul(int a, int b) { return (ll)a * b % mod; } const int inf = (int)2e9 + 5; const ll Inf = (ll)1e18 + 5; const int N = 2e5 + 5; int fac[N], inv_fac[N]; int bigmod(int a, int b) { if (b == 0) return 1; int x = bigmod(a, b / 2); x = mul(x, x); if (b & 1) x = mul(x, a); return x; } int path(int n, int m) { return mul(fac[n + m], mul(inv_fac[n], inv_fac[m])); } int solve() { int n = in(), m = in(); int k = in(); vii a(k); for (int i = 0; i < k; i++) { a[i].ff = in(), a[i].ss = in(); } sort(all(a), [&](pii x, pii y) { return x.ff + x.ss < y.ff + y.ss; }); a.pb({n, m}); vi dp(k + 1, 0); for (int i = 0; i <= k; i++) { int tem = path(a[i].ff - 1, a[i].ss - 1); for (int j = 0; j < i; j++) { if (a[j].ff <= a[i].ff && a[j].ss <= a[i].ss) { tem = sub(tem, mul(dp[j], path(a[i].ff - a[j].ff, a[i].ss - a[j].ss))); } } dp[i] = tem; } printf("%d\n", dp[k]); return 0; } int main() { fac[0] = inv_fac[0] = 1; for (int i = 1; i < N; i++) { fac[i] = mul(fac[i - 1], i); inv_fac[i] = bigmod(fac[i], mod - 2); } int test = 1, tc = 0; // scanf("%d", &test); while (test--) { // printf("Case %d: ", ++tc); solve(); } return 0; }
replace
138
139
138
141
0
p03185
C++
Runtime Error
// in the name of Allah #include <bits/stdc++.h> using namespace std; #define endl '\n' #define pb push_back #define ll long long #define double long double #define sz(x) ((int)(x.size())) #define fr first #define se second #define inf 1e18 #define max_n 123456 #define mod 1000000007 ll add(ll a, ll b) { a += b; if (a >= mod) return a - mod; return a; } ll mul(ll a, ll b) { a *= b; if (a >= mod) return a % mod; return a; } ll power(ll a, ll b) { ll p = 1; while (b) { if (b & 1) p = mul(p, a); a = mul(a, a); b /= 2; } return p; } // -----------> copied code <------------ const ll is_query = -(1LL << 62); struct Line { ll m, b; mutable function<const Line *()> succ; bool operator<(const Line &rhs) const { if (rhs.b != is_query) return m < rhs.m; const Line *s = succ(); if (!s) return 0; ll x = rhs.m; return b - s->b < (s->m - m) * x; } }; struct HullDynamic : public multiset<Line> { // will maintain upper hull for maximum bool bad(iterator y) { auto z = next(y); if (y == begin()) { if (z == end()) return 0; return y->m == z->m && y->b <= z->b; } auto x = prev(y); if (z == end()) return y->m == x->m && y->b <= x->b; return (double)(x->b - y->b) * (z->m - y->m) >= (double)(y->b - z->b) * (y->m - x->m); } void insert_line(ll m, ll b) { auto y = insert({m, b}); y->succ = [=] { return next(y) == end() ? 0 : &*next(y); }; if (bad(y)) { erase(y); return; } while (next(y) != end() && bad(next(y))) erase(next(y)); while (y != begin() && bad(prev(y))) erase(prev(y)); } ll eval(ll x) { auto l = *lower_bound((Line){x, is_query}); return l.m * x + l.b; } }; // -----------> end of copied code <------------ ll n, c; ll h[123456]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> c; for (int i = 0; i < n; i++) cin >> h[i]; ll res = (h[1] - h[0]) * (h[1] - h[0]) + c; if (n == 2) { cout << res << endl; return 0; } HullDynamic cht; cht.insert_line(2 * h[0], -(c + h[0] * h[0])); cht.insert_line(2 * h[1], -(res + c + h[1] * h[1])); for (int i = 2; i < n; i++) { res = h[i] * h[i] - cht.eval(h[i]); cht.insert_line(2 * h[i], -(res + c + h[i] * h[i])); } cout << res << endl; return 0; }
// in the name of Allah #include <bits/stdc++.h> using namespace std; #define endl '\n' #define pb push_back #define ll long long #define double long double #define sz(x) ((int)(x.size())) #define fr first #define se second #define inf 1e18 #define max_n 123456 #define mod 1000000007 ll add(ll a, ll b) { a += b; if (a >= mod) return a - mod; return a; } ll mul(ll a, ll b) { a *= b; if (a >= mod) return a % mod; return a; } ll power(ll a, ll b) { ll p = 1; while (b) { if (b & 1) p = mul(p, a); a = mul(a, a); b /= 2; } return p; } // -----------> copied code <------------ const ll is_query = -(1LL << 62); struct Line { ll m, b; mutable function<const Line *()> succ; bool operator<(const Line &rhs) const { if (rhs.b != is_query) return m < rhs.m; const Line *s = succ(); if (!s) return 0; ll x = rhs.m; return b - s->b < (s->m - m) * x; } }; struct HullDynamic : public multiset<Line> { // will maintain upper hull for maximum bool bad(iterator y) { auto z = next(y); if (y == begin()) { if (z == end()) return 0; return y->m == z->m && y->b <= z->b; } auto x = prev(y); if (z == end()) return y->m == x->m && y->b <= x->b; return (double)(x->b - y->b) * (z->m - y->m) >= (double)(y->b - z->b) * (y->m - x->m); } void insert_line(ll m, ll b) { auto y = insert({m, b}); y->succ = [=] { return next(y) == end() ? 0 : &*next(y); }; if (bad(y)) { erase(y); return; } while (next(y) != end() && bad(next(y))) erase(next(y)); while (y != begin() && bad(prev(y))) erase(prev(y)); } ll eval(ll x) { auto l = *lower_bound((Line){x, is_query}); return l.m * x + l.b; } }; // -----------> end of copied code <------------ ll n, c; ll h[223456]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> c; for (int i = 0; i < n; i++) cin >> h[i]; ll res = (h[1] - h[0]) * (h[1] - h[0]) + c; if (n == 2) { cout << res << endl; return 0; } HullDynamic cht; cht.insert_line(2 * h[0], -(c + h[0] * h[0])); cht.insert_line(2 * h[1], -(res + c + h[1] * h[1])); for (int i = 2; i < n; i++) { res = h[i] * h[i] - cht.eval(h[i]); cht.insert_line(2 * h[i], -(res + c + h[i] * h[i])); } cout << res << endl; return 0; }
replace
97
98
97
98
0
p03185
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using i64 = int64_t; #define rep(i, x, y) \ for (i64 i = i64(x), i##_max_for_repmacro = i64(y); \ i < i##_max_for_repmacro; ++i) #define debug(x) #x << "=" << (x) #ifdef DEBUG #define _GLIBCXX_DEBUG #define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl #else #define print(x) #endif const int inf = 1.01e9; const i64 inf64 = 4.01e18; const double eps = 1e-9; template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << "(" << p.first << ", " << p.second << ")"; return os; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "["; for (const auto &v : vec) { os << v << ","; } os << "]"; return os; } template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <typename T, size_t size> void fill(T (&ary)[size], const T &val) { fill_n((T *)ary, size, val); } /* 直線集合L={f_i = a_i*x+b_i}と与えられたクエリxに対してmin(f_i(x))を求めることができる. この実装では追加される直線の傾きが単調非増加である場合のみ対応している. add: O(N) min(クエリが単調非減少の場合): O(Q) min(一般の場合): O(QlogN) N := 直線の個数 Q := クエリの個数 verified by https://beta.atcoder.jp/contests/colopl2018-final-open/submissions/3644416 TODO: 最大値クエリの場合を検証する */ template <typename T> struct convex_hull_trick { static bool ge(T a, T b) { return a >= b; } static bool le(T a, T b) { return a <= b; } bool is_monotonicx; int head; function<bool(T, T)> comp; vector<pair<T, T>> lines; // is_monotonic := クエリは単調非減少か? // min_or_max := trueならば最小値を求める, falseならば最大値を求める convex_hull_trick(bool is_monotonicx, bool min_or_max) : is_monotonicx(is_monotonicx), head(0) { comp = min_or_max ? ge : le; } bool need(pair<T, T> &l, pair<T, T> &m, pair<T, T> &n) { return (n.second - m.second) * (m.first - l.first) < (m.second - l.second) * (n.first - m.first); } // 直線f(x)=ax+bを追加する. // aは単調非増加である必要がある void add(T a, T b) { auto line = make_pair(a, b); while (lines.size() >= 2 and !need(*(lines.end() - 2), lines.back(), line)) { lines.pop_back(); } lines.push_back(line); } T f(int i, T x) { return lines[i].first * x + lines[i].second; } // f_i(x)の最小値(or最大値) T min(T x) { if (is_monotonicx) { while (lines.size() - head >= 2 and comp(f(head, x), f(head + 1, x))) ++head; return f(head, x); } else { int lb = 0, ub = lines.size(); while (ub - lb > 1) { int mid = (lb + ub) / 2; (mid == 0 or comp(f(mid - 1, x), f(mid, x)) ? lb : ub) = mid; } rep(i, 0, lines.size()) assert(comp(f(i, x), f(lb, x))); return f(lb, x); } } }; void solve() { // const i64 mod = 1'000'000'007; i64 N, C; cin >> N >> C; vector<i64> h(N); rep(i, 0, N) { cin >> h[i]; } const i64 maxn = 200000; static i64 dp[maxn]; fill(dp, maxn); convex_hull_trick<i64> cht(false, false); dp[0] = 0; cht.add(2 * h[0], -h[0] * h[0] - dp[0]); rep(i, 1, N) { dp[i] = h[i] * h[i] + C - cht.min(h[i]); cht.add(2 * h[i], -h[i] * h[i] - dp[i]); } cout << dp[N - 1] << endl; } int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(16); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; using i64 = int64_t; #define rep(i, x, y) \ for (i64 i = i64(x), i##_max_for_repmacro = i64(y); \ i < i##_max_for_repmacro; ++i) #define debug(x) #x << "=" << (x) #ifdef DEBUG #define _GLIBCXX_DEBUG #define print(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl #else #define print(x) #endif const int inf = 1.01e9; const i64 inf64 = 4.01e18; const double eps = 1e-9; template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << "(" << p.first << ", " << p.second << ")"; return os; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "["; for (const auto &v : vec) { os << v << ","; } os << "]"; return os; } template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <typename T, size_t size> void fill(T (&ary)[size], const T &val) { fill_n((T *)ary, size, val); } /* 直線集合L={f_i = a_i*x+b_i}と与えられたクエリxに対してmin(f_i(x))を求めることができる. この実装では追加される直線の傾きが単調非増加である場合のみ対応している. add: O(N) min(クエリが単調非減少の場合): O(Q) min(一般の場合): O(QlogN) N := 直線の個数 Q := クエリの個数 verified by https://beta.atcoder.jp/contests/colopl2018-final-open/submissions/3644416 TODO: 最大値クエリの場合を検証する */ template <typename T> struct convex_hull_trick { static bool ge(T a, T b) { return a >= b; } static bool le(T a, T b) { return a <= b; } bool is_monotonicx; int head; function<bool(T, T)> comp; vector<pair<T, T>> lines; // is_monotonic := クエリは単調非減少か? // min_or_max := trueならば最小値を求める, falseならば最大値を求める convex_hull_trick(bool is_monotonicx, bool min_or_max) : is_monotonicx(is_monotonicx), head(0) { comp = min_or_max ? ge : le; } bool need(pair<T, T> &l, pair<T, T> &m, pair<T, T> &n) { return (n.second - m.second) * (m.first - l.first) < (m.second - l.second) * (n.first - m.first); } // 直線f(x)=ax+bを追加する. // aは単調非増加である必要がある void add(T a, T b) { auto line = make_pair(a, b); while (lines.size() >= 2 and !need(*(lines.end() - 2), lines.back(), line)) { lines.pop_back(); } lines.push_back(line); } T f(int i, T x) { return lines[i].first * x + lines[i].second; } // f_i(x)の最小値(or最大値) T min(T x) { if (is_monotonicx) { while (lines.size() - head >= 2 and comp(f(head, x), f(head + 1, x))) ++head; return f(head, x); } else { int lb = 0, ub = lines.size(); while (ub - lb > 1) { int mid = (lb + ub) / 2; (mid == 0 or comp(f(mid - 1, x), f(mid, x)) ? lb : ub) = mid; } rep(i, 0, lines.size()) assert(comp(f(i, x), f(lb, x))); return f(lb, x); } } }; void solve() { // const i64 mod = 1'000'000'007; i64 N, C; cin >> N >> C; vector<i64> h(N); rep(i, 0, N) { cin >> h[i]; } const i64 maxn = 200000; static i64 dp[maxn]; fill(dp, maxn); convex_hull_trick<i64> cht(true, false); dp[0] = 0; cht.add(2 * h[0], -h[0] * h[0] - dp[0]); rep(i, 1, N) { dp[i] = h[i] * h[i] + C - cht.min(h[i]); cht.add(2 * h[i], -h[i] * h[i] - dp[i]); } cout << dp[N - 1] << endl; } int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(16); solve(); return 0; }
replace
139
140
139
140
TLE
p03185
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; mt19937_64 llrand(random_device{}()); #define st first #define nd second #define mp make_pair #define pb push_back #define cl(x, v) memset((x), (v), sizeof(x)) #define db(x) cerr << #x << " == " << x << endl #define dbs(x) cerr << x << endl #define _ << ", " << #define gcd(x, y) __gcd((x), (y)) typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<int, pii> piii; typedef pair<ll, ll> pll; typedef pair<ll, pll> plll; typedef vector<int> vi; typedef vector<vi> vii; const ld EPS = 1e-9, PI = acos(-1.); const ll LINF = 0x3f3f3f3f3f3f3f3f, LMOD = 1011112131415161719ll; const int INF = 0x3f3f3f3f, MOD = 1e9 + 7; const int N = 1e5 + 5; typedef long long type; struct line { type b, m; }; int nh, pos; line hull[N]; bool check(line s, line t, line u) { return (s.b - t.b) * (u.m - s.m) < (s.b - u.b) * (t.m - s.m); } void update(line s) { if (nh == 1 and hull[nh - 1].b == s.b) nh--; if (nh > 0 and hull[nh - 1].m >= s.m) return; while (nh >= 2 and !check(hull[nh - 2], hull[nh - 1], s)) nh--; pos = min(pos, nh); hull[nh++] = s; } type eval(int id, type x) { return hull[id].b + hull[id].m * x; } type query(type x) { // Attention!!! while (pos + 1 < nh and eval(pos, x) < eval(pos + 1, x)) pos++; return eval(pos, x); } ll n, c, h[N], dp[N]; int main() { scanf("%lld%lld", &n, &c); for (int i = 0; i < n; i++) { scanf("%lld", &h[i]); ll q = -query(h[i]) + h[i] * h[i]; if (i > 0) dp[i] = -query(h[i]) + h[i] * h[i]; line l; l.b = -(h[i] * h[i] + c + dp[i]); l.m = -(-2 * h[i]); update(l); } printf("%lld\n", dp[n - 1]); return 0; }
#include <bits/stdc++.h> using namespace std; mt19937_64 llrand(random_device{}()); #define st first #define nd second #define mp make_pair #define pb push_back #define cl(x, v) memset((x), (v), sizeof(x)) #define db(x) cerr << #x << " == " << x << endl #define dbs(x) cerr << x << endl #define _ << ", " << #define gcd(x, y) __gcd((x), (y)) typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<int, pii> piii; typedef pair<ll, ll> pll; typedef pair<ll, pll> plll; typedef vector<int> vi; typedef vector<vi> vii; const ld EPS = 1e-9, PI = acos(-1.); const ll LINF = 0x3f3f3f3f3f3f3f3f, LMOD = 1011112131415161719ll; const int INF = 0x3f3f3f3f, MOD = 1e9 + 7; const int N = 2e5 + 5; typedef long long type; struct line { type b, m; }; int nh, pos; line hull[N]; bool check(line s, line t, line u) { return (s.b - t.b) * (u.m - s.m) < (s.b - u.b) * (t.m - s.m); } void update(line s) { if (nh == 1 and hull[nh - 1].b == s.b) nh--; if (nh > 0 and hull[nh - 1].m >= s.m) return; while (nh >= 2 and !check(hull[nh - 2], hull[nh - 1], s)) nh--; pos = min(pos, nh); hull[nh++] = s; } type eval(int id, type x) { return hull[id].b + hull[id].m * x; } type query(type x) { // Attention!!! while (pos + 1 < nh and eval(pos, x) < eval(pos + 1, x)) pos++; return eval(pos, x); } ll n, c, h[N], dp[N]; int main() { scanf("%lld%lld", &n, &c); for (int i = 0; i < n; i++) { scanf("%lld", &h[i]); ll q = -query(h[i]) + h[i] * h[i]; if (i > 0) dp[i] = -query(h[i]) + h[i] * h[i]; line l; l.b = -(h[i] * h[i] + c + dp[i]); l.m = -(-2 * h[i]); update(l); } printf("%lld\n", dp[n - 1]); return 0; }
replace
27
28
27
28
0
p03185
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> #define REP(i, n) for (ll i = 0; i < (ll)n; i++) #define INF 1000000000000000 using namespace std; typedef long long ll; typedef double db; typedef string str; typedef pair<ll, ll> p; ll n, c; vector<ll> h; vector<p> lines; vector<ll> dp; bool check(p p1, p p2, p p3) { return (p2.second - p1.second) * (p3.first - p2.first) > (p3.second - p2.second) * (p2.first - p1.first); } ll get_y(p line, ll x) { return line.first * x + line.second; } void add(p line) { if (lines.size() >= 2) { ll t = lines.size() - 1; while (!check(lines[t - 1], lines[t], line)) { lines.pop_back(); t--; } } lines.push_back(line); } ll query(ll x) { ll l = -1, r = lines.size() - 1, mid; while (r - l > 1) { mid = (l + r) / 2; if (get_y(lines[mid], x) >= get_y(lines[mid + 1], x)) { l = mid; } else { r = mid; } } return get_y(lines[r], x); } int main() { cin >> n >> c; REP(i, n) { ll tmp; cin >> tmp; h.push_back(tmp); } ll dp; add(p(-2 * h[0], c + h[0] * h[0])); for (ll i = 1; i < n; i++) { dp = query(h[i]) + h[i] * h[i]; add(p(-2 * h[i], h[i] * h[i] + c + dp)); } cout << dp << endl; return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> #include <map> #include <queue> #include <set> #include <string> #include <vector> #define REP(i, n) for (ll i = 0; i < (ll)n; i++) #define INF 1000000000000000 using namespace std; typedef long long ll; typedef double db; typedef string str; typedef pair<ll, ll> p; ll n, c; vector<ll> h; vector<p> lines; vector<ll> dp; bool check(p p1, p p2, p p3) { return (p2.second - p1.second) * (p3.first - p2.first) > (p3.second - p2.second) * (p2.first - p1.first); } ll get_y(p line, ll x) { return line.first * x + line.second; } void add(p line) { if (lines.size() >= 2) { ll t = lines.size() - 1; while (t - 1 >= 0 && !check(lines[t - 1], lines[t], line)) { lines.pop_back(); t--; } } lines.push_back(line); } ll query(ll x) { ll l = -1, r = lines.size() - 1, mid; while (r - l > 1) { mid = (l + r) / 2; if (get_y(lines[mid], x) >= get_y(lines[mid + 1], x)) { l = mid; } else { r = mid; } } return get_y(lines[r], x); } int main() { cin >> n >> c; REP(i, n) { ll tmp; cin >> tmp; h.push_back(tmp); } ll dp; add(p(-2 * h[0], c + h[0] * h[0])); for (ll i = 1; i < n; i++) { dp = query(h[i]) + h[i] * h[i]; add(p(-2 * h[i], h[i] * h[i] + c + dp)); } cout << dp << endl; return 0; }
replace
32
33
32
33
0
p03185
C++
Runtime Error
#pragma optimization_level 3 #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") // #pragma GCC target("avx,avx2,fma") #include <bits/stdc++.h> #define F first #define S second #define vec vector #define pb push_back #define pll pair<ll, ll> #define pdd pair<ld, ld> #define pq priority_queue #define umap unordered_map #define uset unordered_set #define pii pair<int, int> #define pnn pair<Node *, Node *> #define uid uniform_int_distribution #define FILE \ ifstream in("input.txt"); \ ofstream out("output.txt"); #define fast \ cin.tie(0); \ cout.tie(0); \ cin.sync_with_stdio(0); \ cout.sync_with_stdio(0); using namespace std; typedef string str; typedef long long ll; typedef long double ld; typedef unsigned int uint; typedef unsigned long long ull; mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); ld inf = 1e10; struct CHT { vec<pdd> line; vec<pdd> gr; CHT() { line.pb({0, 0}); gr.pb({-inf, inf}); } ld gval(ld X) { int l = -1, r = (int)(gr.size()) - 1; for (; l + 1 < r;) { int md = (l + r) / 2; if (X > gr[md].S) l = md; else r = md; } /*for(int q=0; q<gr.size(); q++){ if(gr[q].F<=X && X<=gr[q].S){ l = q; } }*/ assert(gr[r].F <= X && X <= gr[r].S); return line[r].F * X + line[r].S; } void add_line(ld K, ld B) { int l = -1, r = (int)(gr.size()) - 1; for (; l + 1 < r;) { int md = (l + r) / 2; ld px = (B - line[md].S) / (line[md].F - K); if (px > gr[md].S) l = md; else r = md; } ld px = (B - line[r].S) / (line[r].F - K); /*for(int q=0; q<line.size(); q++){ ld X = (B-line[l].S) / (line[l].F-K); if(gr[q].F<=X && X<=gr[q].S) l = q; } cout<<"NEED: "<<px<<endl; for(pdd p : line) cout<<"{"<<p.F<<" "<<p.S<<"} "; cout<<endl; for(int q=0; q<line.size(); q++) cout<<(B-line[q].S)/(line[q].F-K)<<" "; cout<<endl; for(pdd p : gr) cout<<"{"<<p.F<<" "<<p.S<<"} "; cout<<endl; cout<<"RES: "<<r+1<<endl<<endl;*/ assert(gr[r].F <= px && px <= gr[r].S); for (; line.size() > r + 1;) { line.pop_back(); gr.pop_back(); } gr[r].S = px; line.pb({K, B}); gr.pb({px, inf}); } }; ll a, c; vec<ll> m, dp; int main() { fast; cout.precision(2); cout << fixed; cin >> a >> c; m = vec<ll>(a); for (int q = 0; q < a; q++) cin >> m[q]; dp = vec<ll>(a); CHT cht; cht.add_line(-2 * m[0], m[0] * m[0] + dp[0]); for (int q = 1; q < a; q++) { ll mn = 1e18; ld res = cht.gval(m[q]); // cout<<"VAL: "<<res<<endl; mn = res; /*for(int w=0; w<q; w++){ ll tyt = -2*m[w]*m[q] + (dp[w]+m[w]*m[w]); mn = min(mn, tyt); }*/ dp[q] = mn + m[q] * m[q] + c; cht.add_line(-2 * m[q], m[q] * m[q] + dp[q]); } // for(ll i : dp) cout<<i<<" "; cout<<endl; cout << dp[a - 1]; }
#pragma optimization_level 3 #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") // #pragma GCC target("avx,avx2,fma") #include <bits/stdc++.h> #define F first #define S second #define vec vector #define pb push_back #define pll pair<ll, ll> #define pdd pair<ld, ld> #define pq priority_queue #define umap unordered_map #define uset unordered_set #define pii pair<int, int> #define pnn pair<Node *, Node *> #define uid uniform_int_distribution #define FILE \ ifstream in("input.txt"); \ ofstream out("output.txt"); #define fast \ cin.tie(0); \ cout.tie(0); \ cin.sync_with_stdio(0); \ cout.sync_with_stdio(0); using namespace std; typedef string str; typedef long long ll; typedef long double ld; typedef unsigned int uint; typedef unsigned long long ull; mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); ld inf = 1e15; struct CHT { vec<pdd> line; vec<pdd> gr; CHT() { line.pb({0, 0}); gr.pb({-inf, inf}); } ld gval(ld X) { int l = -1, r = (int)(gr.size()) - 1; for (; l + 1 < r;) { int md = (l + r) / 2; if (X > gr[md].S) l = md; else r = md; } /*for(int q=0; q<gr.size(); q++){ if(gr[q].F<=X && X<=gr[q].S){ l = q; } }*/ assert(gr[r].F <= X && X <= gr[r].S); return line[r].F * X + line[r].S; } void add_line(ld K, ld B) { int l = -1, r = (int)(gr.size()) - 1; for (; l + 1 < r;) { int md = (l + r) / 2; ld px = (B - line[md].S) / (line[md].F - K); if (px > gr[md].S) l = md; else r = md; } ld px = (B - line[r].S) / (line[r].F - K); /*for(int q=0; q<line.size(); q++){ ld X = (B-line[l].S) / (line[l].F-K); if(gr[q].F<=X && X<=gr[q].S) l = q; } cout<<"NEED: "<<px<<endl; for(pdd p : line) cout<<"{"<<p.F<<" "<<p.S<<"} "; cout<<endl; for(int q=0; q<line.size(); q++) cout<<(B-line[q].S)/(line[q].F-K)<<" "; cout<<endl; for(pdd p : gr) cout<<"{"<<p.F<<" "<<p.S<<"} "; cout<<endl; cout<<"RES: "<<r+1<<endl<<endl;*/ assert(gr[r].F <= px && px <= gr[r].S); for (; line.size() > r + 1;) { line.pop_back(); gr.pop_back(); } gr[r].S = px; line.pb({K, B}); gr.pb({px, inf}); } }; ll a, c; vec<ll> m, dp; int main() { fast; cout.precision(2); cout << fixed; cin >> a >> c; m = vec<ll>(a); for (int q = 0; q < a; q++) cin >> m[q]; dp = vec<ll>(a); CHT cht; cht.add_line(-2 * m[0], m[0] * m[0] + dp[0]); for (int q = 1; q < a; q++) { ll mn = 1e18; ld res = cht.gval(m[q]); // cout<<"VAL: "<<res<<endl; mn = res; /*for(int w=0; w<q; w++){ ll tyt = -2*m[w]*m[q] + (dp[w]+m[w]*m[w]); mn = min(mn, tyt); }*/ dp[q] = mn + m[q] * m[q] + c; cht.add_line(-2 * m[q], m[q] * m[q] + dp[q]); } // for(ll i : dp) cout<<i<<" "; cout<<endl; cout << dp[a - 1]; }
replace
33
34
33
34
0
p03185
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long int; constexpr ll M = 1e9 + 7; inline ll sum_1(const ll n) { return ((n * (n + 1)) / 2) % M; } inline ll sum_2(const ll n) { return ((n * (n + 1) * (2 * n + 1)) / 6) % M; } inline ll sum_3(const ll n) { return (((n * (n + 1)) / 2) % M * ((n * (n + 1)) / 2) % M) % M; } inline ll sum_1(const ll i, const ll j) { return (sum_1(j) - sum_1(i - 1) + M) % M; } inline ll sum_2(const ll i, const ll j) { return (sum_2(j) - sum_2(i - 1) + M) % M; } inline ll sum_3(const ll i, const ll j) { return (sum_3(j) - sum_3(i - 1) + M) % M; } /* clang-format off */ namespace myDebug { template <class T> auto is_printable(T *v) -> decltype(cout << *v, 0); template <class T> char is_printable(...); template <class T> struct range{T begin, end;}; class Debug { class DebugImpl { public: ~DebugImpl() { cout << endl; } #define enable_cond(cmp) enable_if<sizeof(is_printable<T>(nullptr)) cmp 1, DebugImpl &>::type template <class T> typename enable_cond(!=) operator<<(const T v) { cout << v; return *this; } template <class T> typename enable_cond(==) operator<<(const T v) { return *this << range<decltype(begin(v))>({begin(v), end(v)}); } template <class T, class U> DebugImpl& operator<<(const pair<T, U> p) { return *this << "(" << p.first << ", " << p.second << ")"; } template <class T> DebugImpl& operator<<(const range<T> v) { for (auto it = v.begin; it != v.end; ++it) { *this << *it << " "; } return *this << "\n"; } #ifdef enable_cond #undef enable_cond #endif } os; public: #ifdef D_LOCAL template<class T> Debug& operator<<(const T v) { os << v; cout << endl; return *this; } #else template <class T> Debug& operator<<(const T &) { return *this; } #endif }; #define px(...) #__VA_ARGS__":" << (__VA_ARGS__) }; // namespace myDebug /* clang-format on */ myDebug::Debug debug; ll pow_f(ll b, ll e, ll M = ::M) { ll ans = 1; b = b % M; while (e) { if (e & 1) { ans = (ans * b) % M; } b = (b * b) % M; e >>= 1; } return ans; } ll nCr(ll n, ll r) { if (r > n || n < 0 || r < 0) { return 0; } if (n == r || r == 0) { return 1; } r = min(r, n - r); ll ans = 1; for (ll i = 1; i <= r; ++i) { ans = (ans * (n - i + 1)) / (i); } return ans; } ll nCr(tuple<ll, ll> t) { auto n = get<0>(t); auto k = get<1>(t); return nCr(n + k - 1, k); } ll modinv(ll x, ll M = ::M) { return pow_f(x, M - 2, M); } struct line { ll m, c; ll operator()(ll x) { return m * x + c; } ll intersectX(line &ot) { return (ot.c - c) / (m - ot.m); } friend ostream &operator<<(ostream &os, const line &l) { os << "[" << l.m << "x + " << l.c << "]"; return os; } }; // int128 bool isGreaterEq(pair<line, line> p, ll x) { ll pa = p.first.c - p.second.c; ll pb = p.second.m - p.first.m; if (pb < 0) { pa = -pa; pb = -pb; } return pa <= x * pb; } bool isGreaterEq(pair<line, line> p, pair<line, line> q) { __int128 pa = ((__int128)p.first.c - (__int128)p.second.c); __int128 pb = ((__int128)p.second.m - (__int128)p.first.m); __int128 qa = ((__int128)q.first.c - (__int128)q.second.c); __int128 qb = ((__int128)q.second.m - (__int128)q.first.m); if (pb < 0) { pa = -pa; pb = -pb; } if (qb < 0) { qa = -qa; qb = -qb; } return pa * qb <= qa * pb; } void solve() { ll n, c; cin >> n >> c; vector<ll> h(n); for (auto &i : h) { cin >> i; } /** * * dp[i] = min j (dp[j] + (h[i] - h[j])**2 + C) * = dp[j] + hi**2 + hj**2 - 2hihj + C * = hi**2 + C + (dp[j] + h[j]**2) - 2hj*hi */ vector<ll> dp(n); dp[0] = 0; vector<line> cvxHull; cvxHull.reserve(n); cvxHull.push_back({-2 * h[0], dp[0] + h[0] * h[0]}); for (ll i = 1; i < n; ++i) { // Calc Dp[i] // cvxHull already present evaluate @ hi ll x = h[i]; ll lo = 0; ll hi = cvxHull.size() - 1; ll mid = lo + (hi - lo) / 2; ll ans = cvxHull[mid](x); bool lb = true, up = true; while (lo <= hi) { mid = lo + (hi - lo) / 2; lb = (mid - 1 >= 0) ? isGreaterEq({cvxHull[mid], cvxHull[mid - 1]}, x) : true; // Greater than lower intersection up = (mid + 1 < cvxHull.size()) ? !isGreaterEq({cvxHull[mid], cvxHull[mid + 1]}, x) : true; // Less than upper intersection if (lb && up) { ans = cvxHull[mid](x); break; } else if (!lb && up) { hi = mid - 1; } else if (lb && !up) { lo = mid + 1; } else { assert(false); } } dp[i] = x * x + c + ans; line l{-2 * h[i], dp[i] + h[i] * h[i]}; // Delete lines after intersection (greater and equal) for (int j = cvxHull.size() - 1; j > 0; --j) { auto lLast = cvxHull[j]; auto lPrev = cvxHull[j - 1]; auto ltemp = l; if (isGreaterEq({lLast, ltemp}, {lLast, lPrev})) { cvxHull.pop_back(); } } // Insert line cvxHull.push_back(l); } cout << dp[n - 1] << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #ifdef D_LOCAL freopen("in.in", "r", stdin); freopen("out.out", "w", stdout); #endif solve(); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long int; constexpr ll M = 1e9 + 7; inline ll sum_1(const ll n) { return ((n * (n + 1)) / 2) % M; } inline ll sum_2(const ll n) { return ((n * (n + 1) * (2 * n + 1)) / 6) % M; } inline ll sum_3(const ll n) { return (((n * (n + 1)) / 2) % M * ((n * (n + 1)) / 2) % M) % M; } inline ll sum_1(const ll i, const ll j) { return (sum_1(j) - sum_1(i - 1) + M) % M; } inline ll sum_2(const ll i, const ll j) { return (sum_2(j) - sum_2(i - 1) + M) % M; } inline ll sum_3(const ll i, const ll j) { return (sum_3(j) - sum_3(i - 1) + M) % M; } /* clang-format off */ namespace myDebug { template <class T> auto is_printable(T *v) -> decltype(cout << *v, 0); template <class T> char is_printable(...); template <class T> struct range{T begin, end;}; class Debug { class DebugImpl { public: ~DebugImpl() { cout << endl; } #define enable_cond(cmp) enable_if<sizeof(is_printable<T>(nullptr)) cmp 1, DebugImpl &>::type template <class T> typename enable_cond(!=) operator<<(const T v) { cout << v; return *this; } template <class T> typename enable_cond(==) operator<<(const T v) { return *this << range<decltype(begin(v))>({begin(v), end(v)}); } template <class T, class U> DebugImpl& operator<<(const pair<T, U> p) { return *this << "(" << p.first << ", " << p.second << ")"; } template <class T> DebugImpl& operator<<(const range<T> v) { for (auto it = v.begin; it != v.end; ++it) { *this << *it << " "; } return *this << "\n"; } #ifdef enable_cond #undef enable_cond #endif } os; public: #ifdef D_LOCAL template<class T> Debug& operator<<(const T v) { os << v; cout << endl; return *this; } #else template <class T> Debug& operator<<(const T &) { return *this; } #endif }; #define px(...) #__VA_ARGS__":" << (__VA_ARGS__) }; // namespace myDebug /* clang-format on */ myDebug::Debug debug; ll pow_f(ll b, ll e, ll M = ::M) { ll ans = 1; b = b % M; while (e) { if (e & 1) { ans = (ans * b) % M; } b = (b * b) % M; e >>= 1; } return ans; } ll nCr(ll n, ll r) { if (r > n || n < 0 || r < 0) { return 0; } if (n == r || r == 0) { return 1; } r = min(r, n - r); ll ans = 1; for (ll i = 1; i <= r; ++i) { ans = (ans * (n - i + 1)) / (i); } return ans; } ll nCr(tuple<ll, ll> t) { auto n = get<0>(t); auto k = get<1>(t); return nCr(n + k - 1, k); } ll modinv(ll x, ll M = ::M) { return pow_f(x, M - 2, M); } struct line { ll m, c; ll operator()(ll x) { return m * x + c; } ll intersectX(line &ot) { return (ot.c - c) / (m - ot.m); } friend ostream &operator<<(ostream &os, const line &l) { os << "[" << l.m << "x + " << l.c << "]"; return os; } }; // int128 bool isGreaterEq(pair<line, line> p, ll x) { ll pa = p.first.c - p.second.c; ll pb = p.second.m - p.first.m; if (pb < 0) { pa = -pa; pb = -pb; } return pa <= x * pb; } bool isGreaterEq(pair<line, line> p, pair<line, line> q) { __int128 pa = ((__int128)p.first.c - (__int128)p.second.c); __int128 pb = ((__int128)p.second.m - (__int128)p.first.m); __int128 qa = ((__int128)q.first.c - (__int128)q.second.c); __int128 qb = ((__int128)q.second.m - (__int128)q.first.m); if (pb < 0) { pa = -pa; pb = -pb; } if (qb < 0) { qa = -qa; qb = -qb; } return pa * qb <= qa * pb; } void solve() { ll n, c; cin >> n >> c; vector<ll> h(n); for (auto &i : h) { cin >> i; } /** * * dp[i] = min j (dp[j] + (h[i] - h[j])**2 + C) * = dp[j] + hi**2 + hj**2 - 2hihj + C * = hi**2 + C + (dp[j] + h[j]**2) - 2hj*hi */ vector<ll> dp(n); dp[0] = 0; vector<line> cvxHull; cvxHull.reserve(n); cvxHull.push_back({-2 * h[0], dp[0] + h[0] * h[0]}); for (ll i = 1; i < n; ++i) { // Calc Dp[i] // cvxHull already present evaluate @ hi ll x = h[i]; ll lo = 0; ll hi = cvxHull.size() - 1; ll mid = lo + (hi - lo) / 2; ll ans = cvxHull[mid](x); bool lb = true, up = true; while (lo <= hi) { mid = lo + (hi - lo) / 2; lb = (mid - 1 >= 0) ? isGreaterEq({cvxHull[mid], cvxHull[mid - 1]}, x) : true; // Greater than lower intersection up = (mid + 1 < cvxHull.size()) ? !isGreaterEq({cvxHull[mid], cvxHull[mid + 1]}, x) : true; // Less than upper intersection if (lb && up) { ans = cvxHull[mid](x); break; } else if (!lb && up) { hi = mid - 1; } else if (lb && !up) { lo = mid + 1; } else { assert(false); } } dp[i] = x * x + c + ans; line l{-2 * h[i], dp[i] + h[i] * h[i]}; // Delete lines after intersection (greater and equal) for (int j = cvxHull.size() - 1; j > 0; --j) { auto lLast = cvxHull[j]; auto lPrev = cvxHull[j - 1]; auto ltemp = l; if (isGreaterEq({lLast, ltemp}, {lLast, lPrev})) { cvxHull.pop_back(); } else { break; } } // Insert line cvxHull.push_back(l); } cout << dp[n - 1] << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #ifdef D_LOCAL freopen("in.in", "r", stdin); freopen("out.out", "w", stdout); #endif solve(); return 0; }
insert
205
205
205
207
TLE
p03185
C++
Time Limit Exceeded
#include <cassert> #include <iostream> #include <map> #include <vector> template <typename T> class LineSet { private: std::map<T, T> L; // 傾き→切片 T max_a, min_a; public: LineSet(T ai) : L(), max_a(-ai), min_a(ai) {} void push(T a, T b) { // push ax + b min_a = std::min(min_a, a); max_a = std::max(max_a, a); if (L.empty()) { L[a] = b; return; } if (L.size() == 1) { if (L.find(a) != L.end()) L[a] = std::min(L[a], b); else L[a] = b; return; } if (a == min_a or a == max_a) { if (L.find(a) != L.end()) L[a] = std::min(L[a], b); else L[a] = b; } if (L.find(a) == L.end()) { auto ll = L.lower_bound(a); auto lr = prev(ll); if ((a - lr->first) * (b - ll->second) >= (ll->first - a) * (lr->second - b)) return; L[a] = b; } else { L[a] = std::min(L[a], b); } if (L.find(a) == L.end() or L[a] != b) { return; } while (1) { auto ll = L.find(a); if (ll == L.begin()) break; auto lm = prev(ll); if (lm == L.begin()) break; auto lr = prev(lm); if ((lm->first - lr->first) * (lm->second - ll->second) < (ll->first - lm->first) * (lr->second - lm->second)) break; L.erase(lm); } while (1) { auto lr = L.find(a); auto lm = next(lr); if (lm == L.end()) break; auto ll = next(lm); if (ll == L.end()) break; if ((lm->first - lr->first) * (lm->second - ll->second) < (ll->first - lm->first) * (lr->second - lm->second)) break; L.erase(lm); } } T get(T x) { T ans = 2e18; for (auto [a, b] : L) { ans = std::min(ans, a * x + b); } return ans; if (L.size() == 1) { auto [a, b] = *L.begin(); return a * x + b; } T la = min_a, ra = max_a; while (ra - la > 1) { T m = (la + ra) / 2; auto r = L.upper_bound(m); assert(r != L.begin()); assert(r != L.end()); auto l = prev(r); if (l->first * x + l->second < r->first * x + r->second) { ra = l->first; } else { la = r->first; } } auto [a, b] = *L.lower_bound(la); T ret = a * x + b; auto itr = L.lower_bound(la); if (itr != L.end() and next(itr) != L.end()) { auto nex = next(itr); ret = std::min(ret, nex->first * x + nex->second); } if (itr != L.begin()) { auto pre = prev(itr); ret = std::min(ret, pre->first * x + pre->second); } return ret; } }; #include <iostream> #include <vector> using namespace std; int main() { long long N, C; cin >> N >> C; vector<long long> H(N); LineSet<long long> cht(1e9); for (int i = 0; i < N; ++i) { cin >> H[i]; } vector<long long> dp(N); cht.push(-2 * H[0], H[0] * H[0]); for (int i = 1; i < N; ++i) { dp[i] = cht.get(H[i]) + H[i] * H[i] + C; cht.push(-2 * H[i], dp[i] + H[i] * H[i]); } cout << dp.back() << endl; }
#include <cassert> #include <iostream> #include <map> #include <vector> template <typename T> class LineSet { private: std::map<T, T> L; // 傾き→切片 T max_a, min_a; public: LineSet(T ai) : L(), max_a(-ai), min_a(ai) {} void push(T a, T b) { // push ax + b min_a = std::min(min_a, a); max_a = std::max(max_a, a); if (L.empty()) { L[a] = b; return; } if (L.size() == 1) { if (L.find(a) != L.end()) L[a] = std::min(L[a], b); else L[a] = b; return; } if (a == min_a or a == max_a) { if (L.find(a) != L.end()) L[a] = std::min(L[a], b); else L[a] = b; } if (L.find(a) == L.end()) { auto ll = L.lower_bound(a); auto lr = prev(ll); if ((a - lr->first) * (b - ll->second) >= (ll->first - a) * (lr->second - b)) return; L[a] = b; } else { L[a] = std::min(L[a], b); } if (L.find(a) == L.end() or L[a] != b) { return; } while (1) { auto ll = L.find(a); if (ll == L.begin()) break; auto lm = prev(ll); if (lm == L.begin()) break; auto lr = prev(lm); if ((lm->first - lr->first) * (lm->second - ll->second) < (ll->first - lm->first) * (lr->second - lm->second)) break; L.erase(lm); } while (1) { auto lr = L.find(a); auto lm = next(lr); if (lm == L.end()) break; auto ll = next(lm); if (ll == L.end()) break; if ((lm->first - lr->first) * (lm->second - ll->second) < (ll->first - lm->first) * (lr->second - lm->second)) break; L.erase(lm); } } T get(T x) { // T ans = 2e18; // for(auto [a, b] : L){ // ans = std::min(ans,a*x+b); // } // return ans; if (L.size() == 1) { auto [a, b] = *L.begin(); return a * x + b; } T la = min_a, ra = max_a; while (ra - la > 1) { T m = (la + ra) / 2; auto r = L.upper_bound(m); assert(r != L.begin()); assert(r != L.end()); auto l = prev(r); if (l->first * x + l->second < r->first * x + r->second) { ra = l->first; } else { la = r->first; } } auto [a, b] = *L.lower_bound(la); T ret = a * x + b; auto itr = L.lower_bound(la); if (itr != L.end() and next(itr) != L.end()) { auto nex = next(itr); ret = std::min(ret, nex->first * x + nex->second); } if (itr != L.begin()) { auto pre = prev(itr); ret = std::min(ret, pre->first * x + pre->second); } return ret; } }; #include <iostream> #include <vector> using namespace std; int main() { long long N, C; cin >> N >> C; vector<long long> H(N); LineSet<long long> cht(1e9); for (int i = 0; i < N; ++i) { cin >> H[i]; } vector<long long> dp(N); cht.push(-2 * H[0], H[0] * H[0]); for (int i = 1; i < N; ++i) { dp[i] = cht.get(H[i]) + H[i] * H[i] + C; cht.push(-2 * H[i], dp[i] + H[i] * H[i]); } cout << dp.back() << endl; }
replace
79
84
79
84
TLE
p03185
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; bool query = 0; struct line { long long m, b; mutable function<const line *()> succ; bool operator<(const line &rhs) const { if (!query) return m < rhs.m; const line *s = succ(); return s ? (b - s->b) < rhs.m * (s->m - m) : 0; } }; struct hull : multiset<line> { bool bad(iterator y) { auto z = next(y); if (y == begin()) { if (z == end()) return 0; return (y->m == z->m) && (y->b <= z->b); } auto x = prev(y); if (z == end()) return (y->m == x->m) && (y->b <= x->b); return (1.0) * (x->b - y->b) * (z->m - y->m) >= (1.0) * (y->b - z->b) * (y->m - x->m); } void add(long long m, long long b) { auto it = insert({-m, -b}); it->succ = [=] { return next(it) == end() ? 0 : &*next(it); }; if (bad(it)) { erase(it); return; } while (next(it) != end() && bad(next(it))) erase(next(it)); while (it != begin() && bad(prev(it))) erase(prev(it)); } long long eval(long long x) { query = 1; auto l = *lower_bound({x, 0}); query = 0; return -(l.m * x + l.b); } }; #define MAX_N 100005 int n; long long c, s[MAX_N]; hull h; int main() { scanf("%d%lld", &n, &c); for (int i = 0; i < n; i++) scanf("%lld", &s[i]); vector<long long> dp(n); h.add(-2 * s[0], s[0] * s[0]); for (int i = 1; i < n; i++) { dp[i] = s[i] * s[i] + c + h.eval(s[i]); h.add(-2 * s[i], s[i] * s[i] + dp[i]); } printf("%lld\n", dp[n - 1]); }
#include <bits/stdc++.h> using namespace std; bool query = 0; struct line { long long m, b; mutable function<const line *()> succ; bool operator<(const line &rhs) const { if (!query) return m < rhs.m; const line *s = succ(); return s ? (b - s->b) < rhs.m * (s->m - m) : 0; } }; struct hull : multiset<line> { bool bad(iterator y) { auto z = next(y); if (y == begin()) { if (z == end()) return 0; return (y->m == z->m) && (y->b <= z->b); } auto x = prev(y); if (z == end()) return (y->m == x->m) && (y->b <= x->b); return (1.0) * (x->b - y->b) * (z->m - y->m) >= (1.0) * (y->b - z->b) * (y->m - x->m); } void add(long long m, long long b) { auto it = insert({-m, -b}); it->succ = [=] { return next(it) == end() ? 0 : &*next(it); }; if (bad(it)) { erase(it); return; } while (next(it) != end() && bad(next(it))) erase(next(it)); while (it != begin() && bad(prev(it))) erase(prev(it)); } long long eval(long long x) { query = 1; auto l = *lower_bound({x, 0}); query = 0; return -(l.m * x + l.b); } }; #define MAX_N 200005 int n; long long c, s[MAX_N]; hull h; int main() { scanf("%d%lld", &n, &c); for (int i = 0; i < n; i++) scanf("%lld", &s[i]); vector<long long> dp(n); h.add(-2 * s[0], s[0] * s[0]); for (int i = 1; i < n; i++) { dp[i] = s[i] * s[i] + c + h.eval(s[i]); h.add(-2 * s[i], s[i] * s[i] + dp[i]); } printf("%lld\n", dp[n - 1]); }
replace
48
49
48
49
0
p03185
C++
Runtime Error
#include <bits/stdc++.h> #define pb push_back #define f first #define s second #define mp make_pair #define inlld(x) scanf("%lld", &x) #define ind(x) scanf("%d", &x) #define inlld2(x, y) scanf("%lld%lld", &x, &y) #define inlld3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z) #define ind2(x, y) scanf("%d%d", &x, &y) #define ind3(x, y, z) scanf("%d%d%d", &x, &y, &z) const int N = 1e5 + 5; const int MOD = 1e9 + 7; typedef long long ll; typedef long double ld; using namespace std; struct line { ll sl, cn; line(ll s, ll c) { sl = s; cn = c; } }; // VERY IMPORTANT, CHANGE IF NEEDED: // GETTING LINES IN DECREASING ORDER OF SLOPE // CHANGE CHECK CONDITION FOR THE REVERSE struct CHT { vector<line> lines; int pointer; void set_pointer() { pointer = 0; } bool check() { int x = lines.size(); line lt = lines[x - 3], rt = lines[x - 1], mid = lines[x - 2]; // CHANGE FOLLOWING CONDITION IF NEEDED return (rt.cn - lt.cn) * (lt.sl - mid.sl) < (mid.cn - lt.cn) * (lt.sl - rt.sl); } void add(line x) { lines.pb(x); while (lines.size() >= 3 && check()) lines.erase(lines.end() - 2); } ll query(ll x) { if (pointer >= lines.size()) pointer = lines.size() - 1; // CHANGE FOLLOWING CONDITION IF NEEDED while (pointer < lines.size() - 1 && lines[pointer + 1].sl * x + lines[pointer + 1].cn < lines[pointer].sl * x + lines[pointer].cn) pointer++; return lines[pointer].sl * x + lines[pointer].cn; } }; CHT cht; ll dp[N], h[N], C, n; int main() { inlld2(n, C); for (ll a = 1; a <= n; a++) { dp[a] = 1e18; inlld(h[a]); } dp[1] = 0; cht.add(line(-2 * h[1], dp[1] + h[1] * h[1])); for (ll a = 2; a <= n; a++) { dp[a] = min(dp[a], cht.query(h[a]) + h[a] * h[a] + C); cht.add(line(-2 * h[a], h[a] * h[a] + dp[a])); } printf("%lld\n", dp[n]); return 0; }
#include <bits/stdc++.h> #define pb push_back #define f first #define s second #define mp make_pair #define inlld(x) scanf("%lld", &x) #define ind(x) scanf("%d", &x) #define inlld2(x, y) scanf("%lld%lld", &x, &y) #define inlld3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z) #define ind2(x, y) scanf("%d%d", &x, &y) #define ind3(x, y, z) scanf("%d%d%d", &x, &y, &z) const int N = 2e5 + 5; const int MOD = 1e9 + 7; typedef long long ll; typedef long double ld; using namespace std; struct line { ll sl, cn; line(ll s, ll c) { sl = s; cn = c; } }; // VERY IMPORTANT, CHANGE IF NEEDED: // GETTING LINES IN DECREASING ORDER OF SLOPE // CHANGE CHECK CONDITION FOR THE REVERSE struct CHT { vector<line> lines; int pointer; void set_pointer() { pointer = 0; } bool check() { int x = lines.size(); line lt = lines[x - 3], rt = lines[x - 1], mid = lines[x - 2]; // CHANGE FOLLOWING CONDITION IF NEEDED return (rt.cn - lt.cn) * (lt.sl - mid.sl) < (mid.cn - lt.cn) * (lt.sl - rt.sl); } void add(line x) { lines.pb(x); while (lines.size() >= 3 && check()) lines.erase(lines.end() - 2); } ll query(ll x) { if (pointer >= lines.size()) pointer = lines.size() - 1; // CHANGE FOLLOWING CONDITION IF NEEDED while (pointer < lines.size() - 1 && lines[pointer + 1].sl * x + lines[pointer + 1].cn < lines[pointer].sl * x + lines[pointer].cn) pointer++; return lines[pointer].sl * x + lines[pointer].cn; } }; CHT cht; ll dp[N], h[N], C, n; int main() { inlld2(n, C); for (ll a = 1; a <= n; a++) { dp[a] = 1e18; inlld(h[a]); } dp[1] = 0; cht.add(line(-2 * h[1], dp[1] + h[1] * h[1])); for (ll a = 2; a <= n; a++) { dp[a] = min(dp[a], cht.query(h[a]) + h[a] * h[a] + C); cht.add(line(-2 * h[a], h[a] * h[a] + dp[a])); } printf("%lld\n", dp[n]); return 0; }
replace
12
13
12
13
0
p03185
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; bool Q; struct Line { mutable ll k, m, p; bool operator<(const Line &o) const { return Q ? p < o.p : k < o.k; } }; struct LineContainer : multiset<Line> { const ll inf = LLONG_MAX; ll div(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); } bool isect(iterator x, iterator y) { if (y == end()) { x->p = inf; return false; } if (x->k == y->k) x->p = x->m > y->m ? inf : -inf; else x->p = div(y->m - x->m, x->k - y->k); return x->p >= y->p; } void add(ll k, ll m) { auto z = insert({k, m, 0}), y = z++, x = y; while (isect(y, z)) z = erase(z); if (x != begin() && isect(--x, y)) isect(x, y = erase(y)); while ((y = x) != begin() && (--x)->p >= y->p) isect(x, erase(y)); } ll query(ll x) { assert(!empty()); Q = 1; auto l = *lower_bound({0, 0, x}); Q = 0; return l.k * x + l.m; } }; const int MAXN = 1e5 + 5; ll dp[MAXN], h[MAXN]; int main() { LineContainer cht; int n; ll c; cin >> n >> c; for (int i = 1; i <= n; i++) cin >> h[i]; dp[1] = 0; cht.add(2ll * h[1], -h[1] * h[1]); for (int m = 2; m <= n; m++) { dp[m] = h[m] * h[m] + c - cht.query(h[m]); cht.add(2ll * h[m], -h[m] * h[m] - dp[m]); } cout << dp[n] << endl; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; bool Q; struct Line { mutable ll k, m, p; bool operator<(const Line &o) const { return Q ? p < o.p : k < o.k; } }; struct LineContainer : multiset<Line> { const ll inf = LLONG_MAX; ll div(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); } bool isect(iterator x, iterator y) { if (y == end()) { x->p = inf; return false; } if (x->k == y->k) x->p = x->m > y->m ? inf : -inf; else x->p = div(y->m - x->m, x->k - y->k); return x->p >= y->p; } void add(ll k, ll m) { auto z = insert({k, m, 0}), y = z++, x = y; while (isect(y, z)) z = erase(z); if (x != begin() && isect(--x, y)) isect(x, y = erase(y)); while ((y = x) != begin() && (--x)->p >= y->p) isect(x, erase(y)); } ll query(ll x) { assert(!empty()); Q = 1; auto l = *lower_bound({0, 0, x}); Q = 0; return l.k * x + l.m; } }; const int MAXN = 2e5 + 5; ll dp[MAXN], h[MAXN]; int main() { LineContainer cht; int n; ll c; cin >> n >> c; for (int i = 1; i <= n; i++) cin >> h[i]; dp[1] = 0; cht.add(2ll * h[1], -h[1] * h[1]); for (int m = 2; m <= n; m++) { dp[m] = h[m] * h[m] + c - cht.query(h[m]); cht.add(2ll * h[m], -h[m] * h[m] - dp[m]); } cout << dp[n] << endl; }
replace
45
46
45
46
0
p03185
C++
Runtime Error
#include <bits/stdc++.h> #define MAXN 2001000 #define lc (x << 1) #define rc (x << 1 | 1) #define pii pair<int, int> #define pb push_back typedef long long ll; using namespace std; ll n, C, h[MAXN], dp[MAXN]; int vis[MAXN << 1]; struct line { double k, b; line(double _k = 0, double _b = 0) { k = _k; b = _b; } double get(double x) { return k * x + b; } } c[MAXN << 1]; void modify(int x, int l, int r, line v) { if (!vis[x]) { vis[x] = 1; c[x] = v; return; } if (c[x].get(l) > v.get(l) && c[x].get(r) > v.get(r)) return; if (c[x].get(l) < v.get(l) && c[x].get(r) < v.get(r)) { c[x] = v; return; } int m = (l + r) >> 1; if (c[x].get(l) < v.get(l)) swap(c[x], v); if (c[x].get(m) > v.get(m)) modify(rc, m + 1, r, v); else { swap(c[x], v); modify(lc, l, m, v); } } double get(int x, int l, int r, int pos) { if (l == r) return c[x].get(l); int m = (l + r) >> 1; double ans = c[x].get(pos); if (pos <= m) ans = max(ans, get(lc, l, m, pos)); else ans = max(ans, get(rc, m + 1, r, pos)); return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> C; for (int i = 0; i < n; i++) { cin >> h[i]; if (i > 0) dp[i] = -get(1, 1, 1e6 + 10, h[i]) + h[i] * h[i] + C; line l = line(2 * h[i], -(dp[i] + h[i] * h[i])); modify(1, 1, 1e6 + 10, l); // cout << dp[i] << " "; } cout << dp[n - 1]; }
#include <bits/stdc++.h> #define MAXN 3001000 #define lc (x << 1) #define rc (x << 1 | 1) #define pii pair<int, int> #define pb push_back typedef long long ll; using namespace std; ll n, C, h[MAXN], dp[MAXN]; int vis[MAXN << 1]; struct line { double k, b; line(double _k = 0, double _b = 0) { k = _k; b = _b; } double get(double x) { return k * x + b; } } c[MAXN << 1]; void modify(int x, int l, int r, line v) { if (!vis[x]) { vis[x] = 1; c[x] = v; return; } if (c[x].get(l) > v.get(l) && c[x].get(r) > v.get(r)) return; if (c[x].get(l) < v.get(l) && c[x].get(r) < v.get(r)) { c[x] = v; return; } int m = (l + r) >> 1; if (c[x].get(l) < v.get(l)) swap(c[x], v); if (c[x].get(m) > v.get(m)) modify(rc, m + 1, r, v); else { swap(c[x], v); modify(lc, l, m, v); } } double get(int x, int l, int r, int pos) { if (l == r) return c[x].get(l); int m = (l + r) >> 1; double ans = c[x].get(pos); if (pos <= m) ans = max(ans, get(lc, l, m, pos)); else ans = max(ans, get(rc, m + 1, r, pos)); return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> C; for (int i = 0; i < n; i++) { cin >> h[i]; if (i > 0) dp[i] = -get(1, 1, 1e6 + 10, h[i]) + h[i] * h[i] + C; line l = line(2 * h[i], -(dp[i] + h[i] * h[i])); modify(1, 1, 1e6 + 10, l); // cout << dp[i] << " "; } cout << dp[n - 1]; }
replace
1
2
1
2
-11
p03185
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int maxn = 50005; typedef long long ll; struct line { ll m, n; line(ll _m = 0, ll _n = 0) { m = _m, n = _n; } } A[maxn]; ll get_y(const line &a, const int &x) { return a.m * x + a.n; } bool ok(const line &a, const line &b, const line &c) { return (a.m - b.m) * (c.n - a.n) <= (a.m - c.m) * (b.n - a.n); } int l, r; void add(const line f) { while (l < r && ok(A[r - 1], A[r], f)) { r--; } r++; A[r] = f; } line query(const int x) { while (l < r && get_y(A[l], x) > get_y(A[l + 1], x)) l++; return A[l]; } ll sq(int x) { return (ll)x * x; } int h[maxn]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); // freopen(".in","r",stdin); int N; ll C; cin >> N >> C; for (int i = 1; i <= N; i++) { cin >> h[i]; } A[1] = line(-2 * h[1], sq(h[1])); l = r = 1; ll mx = 0; for (int i = 2; i <= N; i++) { ll q = get_y(query(h[i]), h[i]); mx = q + C + sq(h[i]); add(line(-2 * h[i], mx + sq(h[i]))); } cout << mx; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; typedef long long ll; struct line { ll m, n; line(ll _m = 0, ll _n = 0) { m = _m, n = _n; } } A[maxn]; ll get_y(const line &a, const int &x) { return a.m * x + a.n; } bool ok(const line &a, const line &b, const line &c) { return (a.m - b.m) * (c.n - a.n) <= (a.m - c.m) * (b.n - a.n); } int l, r; void add(const line f) { while (l < r && ok(A[r - 1], A[r], f)) { r--; } r++; A[r] = f; } line query(const int x) { while (l < r && get_y(A[l], x) > get_y(A[l + 1], x)) l++; return A[l]; } ll sq(int x) { return (ll)x * x; } int h[maxn]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); // freopen(".in","r",stdin); int N; ll C; cin >> N >> C; for (int i = 1; i <= N; i++) { cin >> h[i]; } A[1] = line(-2 * h[1], sq(h[1])); l = r = 1; ll mx = 0; for (int i = 2; i <= N; i++) { ll q = get_y(query(h[i]), h[i]); mx = q + C + sq(h[i]); add(line(-2 * h[i], mx + sq(h[i]))); } cout << mx; return 0; }
replace
2
3
2
3
0
p03185
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; template <class T, class U> void maximize(T &x, U y) { if (x < y) x = y; } template <class T, class U> void minimize(T &x, U y) { if (x > y) x = y; } template <class T> T Abs(T x) { return (x < (T)0 ? -x : x); } #define llong long long #define MASK(i) ((1LL) << (i)) #define BIT(x, i) (((x) >> (i)) & 1) #define all(c) (c).begin(), (c).end() #define fn "test" /// FILE_NAME_HERE /*----------END_OF_TEMPLATE----------*/ namespace task { const int N = 2e5 + 5; int h[N]; llong f[N]; int n; llong c; struct Line { public: llong m, b; public: Line(llong _m, llong _b) { this->m = _m; this->b = _b; } llong evalute(int x) const { return m * (llong)x + b; } }; class ConvexHullTrick { public: vector<Line> hull; public: bool bad(Line la, Line lb, Line lc) { return (lc.b - la.b) * (la.m - lb.m) <= (la.m - lc.m) * (lb.b - la.b); } void addLine(Line l) { while (hull.size() >= 2 and bad(hull[hull.size() - 2], hull.back(), l)) { hull.pop_back(); } hull.emplace_back(l); } llong getMin(int x) { int l = 0, r = hull.size() - 1; while (l < r) { int mid = (l + r) >> 1; if (hull[mid].evalute(x) < hull[mid + 1].evalute(x)) { r = mid; } else { l = mid + 1; } } return min(hull[l].evalute(x), hull[r].evalute(x)); } }; void solve() { cin >> n >> c; for (int i = 1; i <= n; ++i) { cin >> h[i]; } ConvexHullTrick *cht = new ConvexHullTrick(); cht->addLine(Line(-2 * h[1], 1LL * h[1] * 1LL * h[1])); for (int i = 2; i <= n; ++i) { f[i] = 1LL * h[i] * 1LL * h[i] + c + cht->getMin(h[i]); cht->addLine(Line(-2 * h[i], f[i] + 1LL * h[i] * 1LL * h[i])); } cout << f[n]; } } // namespace task int main(void) { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE freopen(fn ".inp", "r", stdin); // freopen(fn".out","w",stdout); #endif // ONLINE_JUDGE task::solve(); }
#include <bits/stdc++.h> using namespace std; template <class T, class U> void maximize(T &x, U y) { if (x < y) x = y; } template <class T, class U> void minimize(T &x, U y) { if (x > y) x = y; } template <class T> T Abs(T x) { return (x < (T)0 ? -x : x); } #define llong long long #define MASK(i) ((1LL) << (i)) #define BIT(x, i) (((x) >> (i)) & 1) #define all(c) (c).begin(), (c).end() #define fn "test" /// FILE_NAME_HERE /*----------END_OF_TEMPLATE----------*/ namespace task { const int N = 2e5 + 5; int h[N]; llong f[N]; int n; llong c; struct Line { public: llong m, b; public: Line(llong _m, llong _b) { this->m = _m; this->b = _b; } llong evalute(int x) const { return m * (llong)x + b; } }; class ConvexHullTrick { public: vector<Line> hull; public: bool bad(Line la, Line lb, Line lc) { return (lc.b - la.b) * (la.m - lb.m) <= (la.m - lc.m) * (lb.b - la.b); } void addLine(Line l) { while (hull.size() >= 2 and bad(hull[hull.size() - 2], hull.back(), l)) { hull.pop_back(); } hull.emplace_back(l); } llong getMin(int x) { int l = 0, r = hull.size() - 1; while (l < r) { int mid = (l + r) >> 1; if (hull[mid].evalute(x) < hull[mid + 1].evalute(x)) { r = mid; } else { l = mid + 1; } } return min(hull[l].evalute(x), hull[r].evalute(x)); } }; void solve() { cin >> n >> c; for (int i = 1; i <= n; ++i) { cin >> h[i]; } ConvexHullTrick *cht = new ConvexHullTrick(); cht->addLine(Line(-2 * h[1], 1LL * h[1] * 1LL * h[1])); for (int i = 2; i <= n; ++i) { f[i] = 1LL * h[i] * 1LL * h[i] + c + cht->getMin(h[i]); cht->addLine(Line(-2 * h[i], f[i] + 1LL * h[i] * 1LL * h[i])); } cout << f[n]; } } // namespace task int main(void) { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #ifndef ONLINE_JUDGE // freopen(fn".inp","r",stdin); // freopen(fn".out","w",stdout); #endif // ONLINE_JUDGE task::solve(); }
replace
90
91
90
91
0
p03185
C++
Runtime Error
#include <bits/stdc++.h> #define DIM 100007 typedef long long ll; typedef long double ld; using namespace std; struct ev { ll k, b; }; ll n, i, j, m, nlines; ev lines[DIM]; ll a[DIM]; ll benz[DIM]; ll dp[DIM]; ll check(ev l1, ev l2, ev l3) { return (ld)(l3.b - l1.b) / (ld)(l1.k - l3.k) >= (ld)(l3.b - l2.b) / (ld)(l2.k - l3.k); } void add(ll k, ll b) { ev l = {k, b}; while (nlines > 1 && check(lines[nlines - 1], lines[nlines], l)) nlines--; nlines++; lines[nlines] = l; } ll f(ll num, ll x) { return lines[num].k * x + lines[num].b; } ll getm(ll x) { ll l = 0, r = nlines, m; while (r - l > 1) { m = (l + r) / 2; if (f(m, x) < f(m + 1, x)) r = m; else l = m; } return f(r, x); } int main() { ll sss; cin >> n >> sss; for (i = 1; i <= n; i++) cin >> a[i]; add(-2 * a[1], a[1] * a[1]); for (i = 2; i <= n; i++) { dp[i] = getm(a[i]) + a[i] * a[i] + sss; add(-2 * a[i], dp[i] + a[i] * a[i]); } cout << dp[n] << endl; return 0; }
#include <bits/stdc++.h> #define DIM 200007 typedef long long ll; typedef long double ld; using namespace std; struct ev { ll k, b; }; ll n, i, j, m, nlines; ev lines[DIM]; ll a[DIM]; ll benz[DIM]; ll dp[DIM]; ll check(ev l1, ev l2, ev l3) { return (ld)(l3.b - l1.b) / (ld)(l1.k - l3.k) >= (ld)(l3.b - l2.b) / (ld)(l2.k - l3.k); } void add(ll k, ll b) { ev l = {k, b}; while (nlines > 1 && check(lines[nlines - 1], lines[nlines], l)) nlines--; nlines++; lines[nlines] = l; } ll f(ll num, ll x) { return lines[num].k * x + lines[num].b; } ll getm(ll x) { ll l = 0, r = nlines, m; while (r - l > 1) { m = (l + r) / 2; if (f(m, x) < f(m + 1, x)) r = m; else l = m; } return f(r, x); } int main() { ll sss; cin >> n >> sss; for (i = 1; i <= n; i++) cin >> a[i]; add(-2 * a[1], a[1] * a[1]); for (i = 2; i <= n; i++) { dp[i] = getm(a[i]) + a[i] * a[i] + sss; add(-2 * a[i], dp[i] + a[i] * a[i]); } cout << dp[n] << endl; return 0; }
replace
2
3
2
3
0
p03185
C++
Runtime Error
#include <bits/stdc++.h> typedef long long ll; using namespace std; typedef pair<ll, ll> pii; #define endl '\n' #define F first #define S second const int MAX_N = 2e5 + 5; int n; ll c; ll h[MAX_N]; ll dp[MAX_N]; set<int> st; priority_queue<pii, vector<pii>, greater<pii>> pq; ll step(int x, int y) { return dp[x] + (h[y] - h[x]) * (h[y] - h[x]) + c; } ll calc_limit(int x, int y) { return (dp[y] + h[y] * h[y] - dp[x] - h[x] * h[x] - 1) / (2 * (h[y] - h[x])) + 1ll; } int main() { scanf("%d%lld", &n, &c); for (int i = 0; i < n; i++) scanf("%lld", &h[i]); st.insert(0); for (int i = 1; i < n; i++) { while (!pq.empty() && pq.top().F <= h[i]) { auto now = pq.top(); pq.pop(); auto it1 = st.find(now.S), it2 = next(it1); if (step((*it1), i) < step((*it2), i)) continue; if (it1 != st.begin()) { auto it0 = prev(it1); pq.push({calc_limit((*it0), (*it2)), (*it0)}); } st.erase(it1); } dp[i] = step((*st.begin()), i); pq.push({calc_limit((*st.rbegin()), i), (*st.rbegin())}); st.insert(i); } printf("%lld\n", dp[n - 1]); return 0; }
#include <bits/stdc++.h> typedef long long ll; using namespace std; typedef pair<ll, ll> pii; #define endl '\n' #define F first #define S second const int MAX_N = 2e5 + 5; int n; ll c; ll h[MAX_N]; ll dp[MAX_N]; set<int> st; priority_queue<pii, vector<pii>, greater<pii>> pq; ll step(int x, int y) { return dp[x] + (h[y] - h[x]) * (h[y] - h[x]) + c; } ll calc_limit(int x, int y) { return (dp[y] + h[y] * h[y] - dp[x] - h[x] * h[x] - 1) / (2 * (h[y] - h[x])) + 1ll; } int main() { scanf("%d%lld", &n, &c); for (int i = 0; i < n; i++) scanf("%lld", &h[i]); st.insert(0); for (int i = 1; i < n; i++) { while (!pq.empty() && pq.top().F <= h[i]) { auto now = pq.top(); pq.pop(); auto it1 = st.find(now.S); if (it1 == st.end()) continue; auto it2 = next(it1); if (it2 == st.end()) continue; if (step((*it1), i) < step((*it2), i)) continue; if (it1 != st.begin()) { auto it0 = prev(it1); pq.push({calc_limit((*it0), (*it2)), (*it0)}); } st.erase(it1); } dp[i] = step((*st.begin()), i); pq.push({calc_limit((*st.rbegin()), i), (*st.rbegin())}); st.insert(i); } printf("%lld\n", dp[n - 1]); return 0; }
replace
31
32
31
37
0
p03185
C++
Runtime Error
#include <bits/stdc++.h> #define int long long using namespace std; const int M = 1 << 20, N = 2 * M, INF = 1e18; int n, val[N], h[N], a[N], b[N], d[N], f[N]; void insert(int i, int A, int B) { int deb[] = {A * d[i] + B, a[i] * d[i] + b[i]}, fin[] = {A * f[i] + B, a[i] * f[i] + b[i]}; if (deb[0] <= deb[1] && fin[0] <= fin[1]) return; if (deb[1] <= deb[0] && fin[1] <= fin[0]) { a[i] = A, b[i] = B; return; } int m = (d[i] + f[i]) / 2, med[] = {A * m + B, a[i] * m + b[i]}; if (med[0] > med[1]) { swap(a[i], A); swap(b[i], B); swap(deb[0], deb[1]); } if (deb[0] > deb[1]) insert(i * 2, A, B); else insert(i * 2 + 1, A, B); } int getMax(int x) { int i = x + M, maxi = -INF; while (i > 0) { maxi = max(maxi, a[i] * x + b[i]); i /= 2; } return maxi; } signed main() { /* freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); */ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); for (int i = M; i < N; i++) f[i] = 1 + (d[i] = i - M); for (int i = M - 1; i > -1; i--) { d[i] = d[i * 2]; f[i] = f[i * 2 + 1]; } for (int i = 0; i < N; i++) b[i] = -INF; int A, B, C; /*cin >> n >> A >> B >> C; n++; for(int i = 1; i < n; i++) cin >> h[i]; for(int i = 1; i < n; i++) h[i] += h[i-1];*/ A = -1, B = 0; cin >> n >> C; C = -C; for (int i = 0; i < n; i++) cin >> h[i]; for (int i = 0, j = 1; j < n; i++, j++) { insert(1, -2 * h[i] * A, A * h[i] * h[i] - B * h[i] + C + val[i]); val[j] = getMax(h[j]) + A * h[j] * h[j] + B * h[j]; } cout << -val[n - 1]; }
#include <bits/stdc++.h> #define int long long using namespace std; const int M = 1 << 20, N = 2 * M, INF = 1e18; int n, val[N], h[N], a[N], b[N], d[N], f[N]; void insert(int i, int A, int B) { if (i >= N) return; int deb[] = {A * d[i] + B, a[i] * d[i] + b[i]}, fin[] = {A * f[i] + B, a[i] * f[i] + b[i]}; if (deb[0] <= deb[1] && fin[0] <= fin[1]) return; if (deb[1] <= deb[0] && fin[1] <= fin[0]) { a[i] = A, b[i] = B; return; } int m = (d[i] + f[i]) / 2, med[] = {A * m + B, a[i] * m + b[i]}; if (med[0] > med[1]) { swap(a[i], A); swap(b[i], B); swap(deb[0], deb[1]); } if (deb[0] > deb[1]) insert(i * 2, A, B); else insert(i * 2 + 1, A, B); } int getMax(int x) { int i = x + M, maxi = -INF; while (i > 0) { maxi = max(maxi, a[i] * x + b[i]); i /= 2; } return maxi; } signed main() { /* freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); */ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); for (int i = M; i < N; i++) f[i] = 1 + (d[i] = i - M); for (int i = M - 1; i > -1; i--) { d[i] = d[i * 2]; f[i] = f[i * 2 + 1]; } for (int i = 0; i < N; i++) b[i] = -INF; int A, B, C; /*cin >> n >> A >> B >> C; n++; for(int i = 1; i < n; i++) cin >> h[i]; for(int i = 1; i < n; i++) h[i] += h[i-1];*/ A = -1, B = 0; cin >> n >> C; C = -C; for (int i = 0; i < n; i++) cin >> h[i]; for (int i = 0, j = 1; j < n; i++, j++) { insert(1, -2 * h[i] * A, A * h[i] * h[i] - B * h[i] + C + val[i]); val[j] = getMax(h[j]) + A * h[j] * h[j] + B * h[j]; } cout << -val[n - 1]; }
insert
9
9
9
11
127
/tmp/c7844e89-b358-4ea4-a405-933a2ee32b08.out: error while loading shared libraries: libc.so.6: failed to map segment from shared object
p03185
C++
Runtime Error
#pragma GCC optimize("-O3") #include <bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; // #include <boost/multiprecision/cpp_int.hpp> // using namespace boost::multiprecision; using namespace std; #define all(c) (c).begin(), (c).end() #define endl "\n" #define ff first #define ss second #define allr(c) (c).rbegin(), (c).rend() #define ifr(i, begin, end) \ for (__typeof(end) i = (begin) - ((begin) > (end)); \ i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end))) #define pof pop_front #define pob pop_back #define pb emplace_back #define pf emplace_front #define fstm(m, n, r) \ m.reserve(n); \ m.max_load_factor(r) #define mp make_pair #define mt make_tuple #define inf LLONG_MAX #define os \ tree<int, null_type, less<int>, rb_tree_tag, \ tree_order_statistics_node_update> // order_of_key (k) : Number of items strictly smaller than k . // find_by_order(k) : K-th element in a set (counting from zero). const double PI = acos(-1); typedef complex<double> cd; typedef long long ll; ll gcd() { return 0ll; } template <typename T, typename... Args> T gcd(T a, Args... args) { return __gcd(a, (__typeof(a))gcd(args...)); } typedef map<ll, ll> mll; typedef map<string, ll> msll; typedef unordered_map<ll, ll> umap; typedef vector<ll> vll; typedef pair<ll, ll> pll; typedef long double ld; #define mod 1000000007 #define N 1000050 struct Point { ll x, y; Point(ll a = 0, ll b = 0) { x = a; y = b; } }; struct LineContainer { vector<Point> line; LineContainer() { line.resize(4 * N + 5, Point(0, inf)); } ll dot(Point a, Point b) { return (a.x * b.x + a.y * b.y); } ll f(Point a, ll x) { return dot(a, Point(x, 1)); } void add(Point nw, ll l = 0, ll r = N, ll pos = 1) { ll m = (l + r) / 2; bool lef = f(nw, l) < f(line[pos], l); bool mid = f(nw, m) < f(line[pos], m); if (mid) swap(nw, line[pos]); if (r - l == 1) return; if (lef != mid) add(nw, l, m, 2 * pos); else add(nw, m, r, 2 * pos + 1); } ll query(ll x, ll l = 0, ll r = N, ll pos = 1) { ll m = (l + r) / 2; if (r - l == 1) return f(line[pos], x); if (x < m) return min(f(line[pos], x), query(x, l, m, 2 * pos)); return min(f(line[pos], x), query(x, m, r, 2 * pos + 1)); } }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); #endif int n; ll c; cin >> n >> c; ll a[n]; ifr(i, 0, n) cin >> a[i]; LineContainer li; ll dp[n]; dp[0] = 0; li.add({-2 * a[0], a[0] * a[0]}); ifr(i, 1, n) { dp[i] = c + a[i] * a[i] + li.query(a[i]); li.add({-2 * a[i], a[i] * a[i] + dp[i]}); } cout << dp[n - 1] << endl; return 0; }
#pragma GCC optimize("-O3") #include <bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; // #include <boost/multiprecision/cpp_int.hpp> // using namespace boost::multiprecision; using namespace std; #define all(c) (c).begin(), (c).end() #define endl "\n" #define ff first #define ss second #define allr(c) (c).rbegin(), (c).rend() #define ifr(i, begin, end) \ for (__typeof(end) i = (begin) - ((begin) > (end)); \ i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end))) #define pof pop_front #define pob pop_back #define pb emplace_back #define pf emplace_front #define fstm(m, n, r) \ m.reserve(n); \ m.max_load_factor(r) #define mp make_pair #define mt make_tuple #define inf LLONG_MAX #define os \ tree<int, null_type, less<int>, rb_tree_tag, \ tree_order_statistics_node_update> // order_of_key (k) : Number of items strictly smaller than k . // find_by_order(k) : K-th element in a set (counting from zero). const double PI = acos(-1); typedef complex<double> cd; typedef long long ll; ll gcd() { return 0ll; } template <typename T, typename... Args> T gcd(T a, Args... args) { return __gcd(a, (__typeof(a))gcd(args...)); } typedef map<ll, ll> mll; typedef map<string, ll> msll; typedef unordered_map<ll, ll> umap; typedef vector<ll> vll; typedef pair<ll, ll> pll; typedef long double ld; #define mod 1000000007 #define N 1000050 struct Point { ll x, y; Point(ll a = 0, ll b = 0) { x = a; y = b; } }; struct LineContainer { vector<Point> line; LineContainer() { line.resize(4 * N + 5, Point(0, inf)); } ll dot(Point a, Point b) { return (a.x * b.x + a.y * b.y); } ll f(Point a, ll x) { return dot(a, Point(x, 1)); } void add(Point nw, ll l = 0, ll r = N, ll pos = 1) { ll m = (l + r) / 2; bool lef = f(nw, l) < f(line[pos], l); bool mid = f(nw, m) < f(line[pos], m); if (mid) swap(nw, line[pos]); if (r - l == 1) return; if (lef != mid) add(nw, l, m, 2 * pos); else add(nw, m, r, 2 * pos + 1); } ll query(ll x, ll l = 0, ll r = N, ll pos = 1) { ll m = (l + r) / 2; if (r - l == 1) return f(line[pos], x); if (x < m) return min(f(line[pos], x), query(x, l, m, 2 * pos)); return min(f(line[pos], x), query(x, m, r, 2 * pos + 1)); } }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; ll c; cin >> n >> c; ll a[n]; ifr(i, 0, n) cin >> a[i]; LineContainer li; ll dp[n]; dp[0] = 0; li.add({-2 * a[0], a[0] * a[0]}); ifr(i, 1, n) { dp[i] = c + a[i] * a[i] + li.query(a[i]); li.add({-2 * a[i], a[i] * a[i] + dp[i]}); } cout << dp[n - 1] << endl; return 0; }
replace
88
91
88
89
0
p03185
C++
Runtime Error
#include <bits/stdc++.h> #define inf 100000000000000LL #define ll long long #define mx 100007 using namespace std; vector<vector<ll>> tree(4LL * mx, {0LL, inf}); ll f(vector<ll> line, int x) { return line[0] * x + line[1]; } void insert(vector<ll> line, int lo = 1, int hi = mx, int i = 1) { int m = (lo + hi) / 2; bool left = f(line, lo) < f(tree[i], lo); bool mid = f(line, m) < f(tree[i], m); if (mid) swap(tree[i], line); if (hi - lo == 1) return; else if (left != mid) insert(line, lo, m, 2 * i); else insert(line, m, hi, 2 * i + 1); } ll query(int x, int lo = 1, int hi = mx, int i = 1) { int m = (lo + hi) / 2; ll curr = f(tree[i], x); if (hi - lo == 1) return curr; if (x < m) return min(curr, query(x, lo, m, 2 * i)); else return min(curr, query(x, m, hi, 2 * i + 1)); } ll dp[mx]; int main() { ll n, c; cin >> n >> c; ll h[n + 5]; for (int i = 1; i <= n; i++) { cin >> h[i]; if (i > 1) dp[i] = h[i] * h[i] + c + query(h[i]); insert({-2 * h[i], h[i] * h[i] + dp[i]}); } cout << dp[n] << "\n"; }
#include <bits/stdc++.h> #define inf 100000000000000LL #define ll long long #define mx 1000007 using namespace std; vector<vector<ll>> tree(4LL * mx, {0LL, inf}); ll f(vector<ll> line, int x) { return line[0] * x + line[1]; } void insert(vector<ll> line, int lo = 1, int hi = mx, int i = 1) { int m = (lo + hi) / 2; bool left = f(line, lo) < f(tree[i], lo); bool mid = f(line, m) < f(tree[i], m); if (mid) swap(tree[i], line); if (hi - lo == 1) return; else if (left != mid) insert(line, lo, m, 2 * i); else insert(line, m, hi, 2 * i + 1); } ll query(int x, int lo = 1, int hi = mx, int i = 1) { int m = (lo + hi) / 2; ll curr = f(tree[i], x); if (hi - lo == 1) return curr; if (x < m) return min(curr, query(x, lo, m, 2 * i)); else return min(curr, query(x, m, hi, 2 * i + 1)); } ll dp[mx]; int main() { ll n, c; cin >> n >> c; ll h[n + 5]; for (int i = 1; i <= n; i++) { cin >> h[i]; if (i > 1) dp[i] = h[i] * h[i] + c + query(h[i]); insert({-2 * h[i], h[i] * h[i] + dp[i]}); } cout << dp[n] << "\n"; }
replace
3
4
3
4
0
p03185
C++
Runtime Error
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair<ll, ll> pll; #define F first #define S second #define pb push_back #define mp make_pair #define all(X) (X).begin(), (X).end() #define REP(i, x, y) for (ll i = x; i <= y; ++i) vector<string> vec_splitter(string s) { for (char &c : s) c = c == ',' ? ' ' : c; stringstream ss; ss << s; vector<string> res; for (string z; ss >> z; res.push_back(z)) ; return res; } void debug_out(vector<string> args, int idx) { cerr << endl; } template <typename Head, typename... Tail> void debug_out(vector<string> args, int idx, Head H, Tail... T) { if (idx > 0) cerr << ", "; stringstream ss; ss << H; cerr << args[idx] << " = " << ss.str(); debug_out(args, idx + 1, T...); } #define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __VA_ARGS__) void localTest() { #ifndef ONLINE_JUDGE freopen("inp", "r", stdin); freopen("out", "w", stdout); #endif } const ll N = 2e5 + 5, inf = 1e15, M = 1e6 + 5, MX = 1e6; ll dp[N], n, b, A[N]; struct line { ll m, c; }; vector<line> T(4 * M, (line){0LL, inf}); ll getY(line l, ll x) { return l.m * x + l.c; } void addLine(ll node, ll lo, ll hi, line l) { if (lo > hi) return; ll mid = (lo + hi) >> 1LL; bool leftVal = getY(l, lo) < getY(T[node], lo); bool rightVal = getY(l, mid + 1) < getY(T[node], mid + 1); if (rightVal) swap(T[node], l); if (lo == hi) return; if (leftVal != rightVal) addLine(node << 1, lo, mid, l); else addLine(node << 1 | 1, mid + 1, hi, l); return; } ll query(ll node, ll lo, ll hi, ll x) { ll ans = getY(T[node], x); if (lo >= hi) return ans; ll mid = (lo + hi) >> 1LL; if (x <= mid) return min(ans, query(node << 1, lo, mid, x)); else return min(ans, query(node << 1 | 1, mid + 1, hi, x)); } int main() { localTest(); ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> b; for (ll i = 1; i <= n; ++i) { cin >> A[i]; if (i > 1) dp[i] = b + A[i] * A[i] + query(1, 1, MX, A[i]); addLine(1, 1, MX, (line){-2 * A[i], dp[i] + A[i] * A[i]}); } cout << dp[n] << "\n"; return 0; }
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair<ll, ll> pll; #define F first #define S second #define pb push_back #define mp make_pair #define all(X) (X).begin(), (X).end() #define REP(i, x, y) for (ll i = x; i <= y; ++i) vector<string> vec_splitter(string s) { for (char &c : s) c = c == ',' ? ' ' : c; stringstream ss; ss << s; vector<string> res; for (string z; ss >> z; res.push_back(z)) ; return res; } void debug_out(vector<string> args, int idx) { cerr << endl; } template <typename Head, typename... Tail> void debug_out(vector<string> args, int idx, Head H, Tail... T) { if (idx > 0) cerr << ", "; stringstream ss; ss << H; cerr << args[idx] << " = " << ss.str(); debug_out(args, idx + 1, T...); } #define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __VA_ARGS__) void localTest() { #ifndef ONLINE_JUDGE freopen("inp", "r", stdin); freopen("out", "w", stdout); #endif } const ll N = 2e5 + 5, inf = 1e15, M = 1e6 + 5, MX = 1e6; ll dp[N], n, b, A[N]; struct line { ll m, c; }; vector<line> T(4 * M, (line){0LL, inf}); ll getY(line l, ll x) { return l.m * x + l.c; } void addLine(ll node, ll lo, ll hi, line l) { if (lo > hi) return; ll mid = (lo + hi) >> 1LL; bool leftVal = getY(l, lo) < getY(T[node], lo); bool rightVal = getY(l, mid + 1) < getY(T[node], mid + 1); if (rightVal) swap(T[node], l); if (lo == hi) return; if (leftVal != rightVal) addLine(node << 1, lo, mid, l); else addLine(node << 1 | 1, mid + 1, hi, l); return; } ll query(ll node, ll lo, ll hi, ll x) { ll ans = getY(T[node], x); if (lo >= hi) return ans; ll mid = (lo + hi) >> 1LL; if (x <= mid) return min(ans, query(node << 1, lo, mid, x)); else return min(ans, query(node << 1 | 1, mid + 1, hi, x)); } int main() { // localTest(); ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> b; for (ll i = 1; i <= n; ++i) { cin >> A[i]; if (i > 1) dp[i] = b + A[i] * A[i] + query(1, 1, MX, A[i]); addLine(1, 1, MX, (line){-2 * A[i], dp[i] + A[i] * A[i]}); } cout << dp[n] << "\n"; return 0; }
replace
72
73
72
73
0
p03185
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define ll long long typedef complex<ll> reta; const ll INF = 1e18 + 7; const int MAXN = 2e5 + 7, MAXX = 1e6 + 7; int n, node_counter = 0, raiz, fe[4 * MAXN], fd[4 * MAXN]; reta f[4 * MAXN], inicial; ll h[MAXN], c, dp[MAXN]; inline int create() { node_counter++; fe[node_counter] = -1; fd[node_counter] = -1; f[node_counter] = inicial; return node_counter; } inline ll calc(reta r, ll val) { return r.real() * val + r.imag(); } void upt(int node, int l, int r, reta nova) { if (l > r) return; int mid = (l + r) / 2; if (calc(nova, mid) >= calc(f[node], mid)) { if (fd[node] == -1) fd[node] = create(); upt(fd[node], mid + 1, r, nova); } else { swap(f[node], nova); if (nova == inicial) return; if (fe[node] == -1) fe[node] = create(); upt(fe[node], l, mid, nova); } } ll query(int node, int l, int r, ll x) { if (node == -1) return INF; ll v_no = calc(f[node], x); int mid = (l + r) / 2; if (x <= mid) return min(v_no, query(fe[node], l, mid, x)); return min(v_no, query(fd[node], mid + 1, r, x)); } int main() { inicial.real(0); inicial.imag(INF); raiz = create(); scanf("%d %lld", &n, &c); scanf("%lld", &h[1]); for (int i = 2; i <= n; i++) { reta ri; ri.real(-2 * h[i - 1]); ri.imag(dp[i - 1] + h[i - 1] * h[i - 1]); upt(raiz, 1, MAXX, ri); scanf("%lld", &h[i]); dp[i] = c + h[i] * h[i] + query(raiz, 1, MAXX, h[i]); } printf("%lld\n", dp[n]); }
#include <bits/stdc++.h> using namespace std; #define ll long long typedef complex<ll> reta; const ll INF = 1e18 + 7; const int MAXN = 2e5 + 7, MAXX = 1e6 + 7; int n, node_counter = 0, raiz, fe[4 * MAXN], fd[4 * MAXN]; reta f[4 * MAXN], inicial; ll h[MAXN], c, dp[MAXN]; inline int create() { node_counter++; fe[node_counter] = -1; fd[node_counter] = -1; f[node_counter] = inicial; return node_counter; } inline ll calc(reta r, ll val) { return r.real() * val + r.imag(); } void upt(int node, int l, int r, reta nova) { if (l > r) return; int mid = (l + r) / 2; if (calc(nova, mid) >= calc(f[node], mid)) { if (fd[node] == -1) fd[node] = create(); upt(fd[node], mid + 1, r, nova); } else { swap(f[node], nova); if (nova == inicial || l == r) return; if (fe[node] == -1) fe[node] = create(); upt(fe[node], l, mid, nova); } } ll query(int node, int l, int r, ll x) { if (node == -1) return INF; ll v_no = calc(f[node], x); int mid = (l + r) / 2; if (x <= mid) return min(v_no, query(fe[node], l, mid, x)); return min(v_no, query(fd[node], mid + 1, r, x)); } int main() { inicial.real(0); inicial.imag(INF); raiz = create(); scanf("%d %lld", &n, &c); scanf("%lld", &h[1]); for (int i = 2; i <= n; i++) { reta ri; ri.real(-2 * h[i - 1]); ri.imag(dp[i - 1] + h[i - 1] * h[i - 1]); upt(raiz, 1, MAXX, ri); scanf("%lld", &h[i]); dp[i] = c + h[i] * h[i] + query(raiz, 1, MAXX, h[i]); } printf("%lld\n", dp[n]); }
replace
28
29
28
29
TLE
p03185
C++
Runtime Error
#include <bits/stdc++.h> #define int long long using namespace std; const int M = 1 << 20, N = 2 * M, INF = 1e18; struct Tree { int a[N], b[N], d[N], f[N]; void init() { for (int i = M; i < N; i++) f[i] = 1 + (d[i] = i - M); for (int i = M - 1; i > -1; i--) { d[i] = d[i * 2]; f[i] = f[i * 2 + 1]; } for (int i = 0; i < N; i++) { a[i] = 0; b[i] = INF; } } void insert(int i, int A, int B) { int deb[] = {d[i] * A + B, d[i] * a[i] + b[i]}, fin[] = {f[i] * A + B, f[i] * a[i] + b[i]}; // cout << A << ' ' << B << ' ' << d[i] << ' ' << f[i] << ' ' << deb[0] << ' // ' << deb[1] << ' ' << fin[0] << ' ' << fin[1] << '\n'; if (deb[0] <= deb[1] && fin[0] <= fin[1]) { a[i] = A; b[i] = B; } else if (!(deb[1] <= deb[0] && fin[1] <= fin[0])) { int m = (d[i] + f[i]) / 2, med[] = {m * A + B, m * a[i] + b[i]}; if (med[0] < med[1]) { swap(A, a[i]); swap(B, b[i]); swap(deb[0], deb[1]); } if (deb[0] < deb[1]) insert(i * 2, A, B); else insert(i * 2 + 1, A, B); } } int getMin(int i, int x) { if (i == 0) return INF; return min(getMin(i / 2, x), a[i] * x + b[i]); } }; int n, c, h[M], cost[M]; signed main() { /* freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); */ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); Tree tree; tree.init(); cin >> n >> c; for (int i = 0; i < n; i++) cin >> h[i]; for (int i = 0, j = 1; j < n; i++, j++) { tree.insert(1, -(h[i] * 2), c + cost[i] + h[i] * h[i]); // cout << -(h[i]*2) << ' ' << c + cost[i] + h[i] * h[i] << ' ' << cost[i] // << '\n'; cost[j] = tree.getMin(h[j] + M, h[j]) + h[j] * h[j]; // cout << h[j] << ' ' << tree.getMin(h[j] + M, h[j]) << ' ' << cost[j] << // '\n' << '\n'; } cout << cost[n - 1] << '\n'; }
#include <bits/stdc++.h> #define int long long using namespace std; const int M = 1 << 20, N = 2 * M, INF = 1e18; struct Tree { int a[N], b[N], d[N], f[N]; void init() { for (int i = M; i < N; i++) f[i] = 1 + (d[i] = i - M); for (int i = M - 1; i > -1; i--) { d[i] = d[i * 2]; f[i] = f[i * 2 + 1]; } for (int i = 0; i < N; i++) { a[i] = 0; b[i] = INF; } } void insert(int i, int A, int B) { if (i >= N) return; int deb[] = {d[i] * A + B, d[i] * a[i] + b[i]}, fin[] = {f[i] * A + B, f[i] * a[i] + b[i]}; // cout << A << ' ' << B << ' ' << d[i] << ' ' << f[i] << ' ' << deb[0] << ' // ' << deb[1] << ' ' << fin[0] << ' ' << fin[1] << '\n'; if (deb[0] <= deb[1] && fin[0] <= fin[1]) { a[i] = A; b[i] = B; } else if (!(deb[1] <= deb[0] && fin[1] <= fin[0])) { int m = (d[i] + f[i]) / 2, med[] = {m * A + B, m * a[i] + b[i]}; if (med[0] < med[1]) { swap(A, a[i]); swap(B, b[i]); swap(deb[0], deb[1]); } if (deb[0] < deb[1]) insert(i * 2, A, B); else insert(i * 2 + 1, A, B); } } int getMin(int i, int x) { if (i == 0) return INF; return min(getMin(i / 2, x), a[i] * x + b[i]); } }; int n, c, h[M], cost[M]; signed main() { /* freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); */ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); Tree tree; tree.init(); cin >> n >> c; for (int i = 0; i < n; i++) cin >> h[i]; for (int i = 0, j = 1; j < n; i++, j++) { tree.insert(1, -(h[i] * 2), c + cost[i] + h[i] * h[i]); // cout << -(h[i]*2) << ' ' << c + cost[i] + h[i] * h[i] << ' ' << cost[i] // << '\n'; cost[j] = tree.getMin(h[j] + M, h[j]) + h[j] * h[j]; // cout << h[j] << ' ' << tree.getMin(h[j] + M, h[j]) << ' ' << cost[j] << // '\n' << '\n'; } cout << cost[n - 1] << '\n'; }
insert
23
23
23
25
-11
p03185
C++
Runtime Error
#include "bits/stdc++.h" using namespace std; #define mod 1000000007 #define dom 998244353 #define all(c) begin(c), end(c) #define rep(i, n) repi(i, 0, n) #define repi(i, a, b) for (int i = (a); i < (b); i++) #define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--) template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <typename T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; } #define long long long #define List vector #define var auto #define Count size() #define Length size() int dd[] = {0, 1, 0, -1, 0}; // →↓←↑ struct LiChaoTree { struct Line { long a, b; Line(long a, long b) { this->a = a; this->b = b; } long f(long x) { return a * x + b; } }; int n; vector<Line> tr; Line ex = Line(0, LLONG_MAX); vector<long> X; // 動的CHT, M=(x座標の最大値) LiChaoTree(int M) { M++; n = 1; while (n < M) n <<= 1; tr.clear(); tr.resize((n << 1) - 1, ex); X.clear(); X.resize(n); for (int i = 0; i < n; i++) X[i] = i; } // クエリ先読み動的CHT Mが巨大なときに用いる // M=(x座標の最大値) // Q=(ソート済みであるx座標のクエリ配列) LiChaoTree(long M, vector<long> Q) { int n_ = Q.size(); n = 1; while (n < n_) n <<= 1; tr.clear(); X.clear(); X.resize(n); for (int i = 0; i < n_; i++) X[i] = Q[i]; for (int i = n_; i < n; i++) X[i] = M << 1; } // 座標xにおけるy座標の最小値 // j=(そのx座標のインデックス long query(int j) { long x = X[j]; int i = j + n - 1; long ret = tr[i].f(x); while (i > 0) { i = i - 1 >> 1; ret = min(ret, tr[i].f(x)); } return ret; } // 直線 y=ax+b を追加 void add(long a, long b) { add(0, 0, n, Line(a, b)); } void add(int k, int l, int r, Line f) { long lx = X[l]; long rx = X[r - 1]; bool left = f.f(lx) < tr[k].f(lx); bool right = f.f(rx) < tr[k].f(rx); if (!left && !right) return; if (left && right) { tr[k] = f; return; } int m = l + r >> 1; long mx = X[m]; bool mid = f.f(mx) < tr[k].f(mx); if (mid) { Line tmp = f; f = tr[k]; tr[k] = tmp; } if (left != mid) add(k << 1 | 1, l, l + r >> 1, f); else add(k + 1 << 1, l + r >> 1, r, f); } }; // 以下の漸化式は // dp[i]=min_j (H[j]-H[i])^2 + dp[j] + C (j=[0,i-1]) // min_j (-2H[j] *H[i] +(H[j]^2+dp[j])) + (H[i]^2+C) // と変形できるのでCHTで高速化できる。 void solve() { int N; cin >> N; long C; cin >> C; vector<long> dp(N); vector<long> H(N); rep(i, N) { cin >> H[i]; } LiChaoTree CHT((int)1e6, H); CHT.add(-2 * H[0], H[0] * H[0]); repi(j, 1, N) { dp[j] = H[j] * H[j] + C + CHT.query(j); CHT.add(-2 * H[j], dp[j] + H[j] * H[j]); } cout << dp[N - 1] << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); solve(); return 0; }
#include "bits/stdc++.h" using namespace std; #define mod 1000000007 #define dom 998244353 #define all(c) begin(c), end(c) #define rep(i, n) repi(i, 0, n) #define repi(i, a, b) for (int i = (a); i < (b); i++) #define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--) template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <typename T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; } #define long long long #define List vector #define var auto #define Count size() #define Length size() int dd[] = {0, 1, 0, -1, 0}; // →↓←↑ struct LiChaoTree { struct Line { long a, b; Line(long a, long b) { this->a = a; this->b = b; } long f(long x) { return a * x + b; } }; int n; vector<Line> tr; Line ex = Line(0, LLONG_MAX); vector<long> X; // 動的CHT, M=(x座標の最大値) LiChaoTree(int M) { M++; n = 1; while (n < M) n <<= 1; tr.clear(); tr.resize((n << 1) - 1, ex); X.clear(); X.resize(n); for (int i = 0; i < n; i++) X[i] = i; } // クエリ先読み動的CHT Mが巨大なときに用いる // M=(x座標の最大値) // Q=(ソート済みであるx座標のクエリ配列) LiChaoTree(long M, vector<long> Q) { int n_ = Q.size(); n = 1; while (n < n_) n <<= 1; tr.clear(); tr.resize((n << 1) - 1, ex); X.clear(); X.resize(n); for (int i = 0; i < n_; i++) X[i] = Q[i]; for (int i = n_; i < n; i++) X[i] = M << 1; } // 座標xにおけるy座標の最小値 // j=(そのx座標のインデックス long query(int j) { long x = X[j]; int i = j + n - 1; long ret = tr[i].f(x); while (i > 0) { i = i - 1 >> 1; ret = min(ret, tr[i].f(x)); } return ret; } // 直線 y=ax+b を追加 void add(long a, long b) { add(0, 0, n, Line(a, b)); } void add(int k, int l, int r, Line f) { long lx = X[l]; long rx = X[r - 1]; bool left = f.f(lx) < tr[k].f(lx); bool right = f.f(rx) < tr[k].f(rx); if (!left && !right) return; if (left && right) { tr[k] = f; return; } int m = l + r >> 1; long mx = X[m]; bool mid = f.f(mx) < tr[k].f(mx); if (mid) { Line tmp = f; f = tr[k]; tr[k] = tmp; } if (left != mid) add(k << 1 | 1, l, l + r >> 1, f); else add(k + 1 << 1, l + r >> 1, r, f); } }; // 以下の漸化式は // dp[i]=min_j (H[j]-H[i])^2 + dp[j] + C (j=[0,i-1]) // min_j (-2H[j] *H[i] +(H[j]^2+dp[j])) + (H[i]^2+C) // と変形できるのでCHTで高速化できる。 void solve() { int N; cin >> N; long C; cin >> C; vector<long> dp(N); vector<long> H(N); rep(i, N) { cin >> H[i]; } LiChaoTree CHT((int)1e6, H); CHT.add(-2 * H[0], H[0] * H[0]); repi(j, 1, N) { dp[j] = H[j] * H[j] + C + CHT.query(j); CHT.add(-2 * H[j], dp[j] + H[j] * H[j]); } cout << dp[N - 1] << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); solve(); return 0; }
insert
65
65
65
66
-11
p03185
C++
Runtime Error
#include <bits/stdc++.h> #define LL long long #define PII pair<int, int> using namespace std; const int MAXN = 100005; const LL MOD = 998244353; LL N, C, s, t, h[MAXN], dp[MAXN]; int deq[MAXN]; bool useless(int a, int b, int c) { LL m1 = -2 * h[a], b1 = dp[a] + h[a] * h[a]; LL m2 = -2 * h[b], b2 = dp[b] + h[b] * h[b]; LL m3 = -2 * h[c], b3 = dp[c] + h[c] * h[c]; if ((b2 - b3) / (m3 - m2) < (b1 - b2) / (m2 - m1)) return true; if ((b2 - b3) / (m3 - m2) > (b1 - b2) / (m2 - m1)) return false; return (b2 - b3) % (m3 - m2) * (m2 - m1) <= (b1 - b2) % (m2 - m1) * (m3 - m2); } LL f(int j, LL x) { return dp[j] + h[j] * h[j] - 2 * h[j] * x; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> N >> C; for (int i = 1; i <= N; i++) cin >> h[i]; dp[1] = 0; for (int i = 2; i <= N; i++) { while (t - s >= 2 && useless(deq[t - 2], deq[t - 1], i - 1)) t--; deq[t++] = i - 1; while (t - s >= 2 && f(deq[s], h[i]) >= f(deq[s + 1], h[i])) s++; dp[i] = f(deq[s], h[i]) + h[i] * h[i] + C; } cout << dp[N] << "\n"; }
#include <bits/stdc++.h> #define LL long long #define PII pair<int, int> using namespace std; const int MAXN = 200005; const LL MOD = 998244353; LL N, C, s, t, h[MAXN], dp[MAXN]; int deq[MAXN]; bool useless(int a, int b, int c) { LL m1 = -2 * h[a], b1 = dp[a] + h[a] * h[a]; LL m2 = -2 * h[b], b2 = dp[b] + h[b] * h[b]; LL m3 = -2 * h[c], b3 = dp[c] + h[c] * h[c]; if ((b2 - b3) / (m3 - m2) < (b1 - b2) / (m2 - m1)) return true; if ((b2 - b3) / (m3 - m2) > (b1 - b2) / (m2 - m1)) return false; return (b2 - b3) % (m3 - m2) * (m2 - m1) <= (b1 - b2) % (m2 - m1) * (m3 - m2); } LL f(int j, LL x) { return dp[j] + h[j] * h[j] - 2 * h[j] * x; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> N >> C; for (int i = 1; i <= N; i++) cin >> h[i]; dp[1] = 0; for (int i = 2; i <= N; i++) { while (t - s >= 2 && useless(deq[t - 2], deq[t - 1], i - 1)) t--; deq[t++] = i - 1; while (t - s >= 2 && f(deq[s], h[i]) >= f(deq[s + 1], h[i])) s++; dp[i] = f(deq[s], h[i]) + h[i] * h[i] + C; } cout << dp[N] << "\n"; }
replace
4
5
4
5
0
p03185
C++
Runtime Error
#include <algorithm> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define mod 1000000007 #define FOR(x, to) for (int x = 0; x < (to); x++) #define FORR(x, arr) for (auto &x : arr) #define ALL(a) (a.begin()), (a.end()) #define ZERO(a) memset(a, 0, sizeof(a)) #define MINUS(a) memset(a, 0xff, sizeof(a)) #define long long long inline int rei() { int x; cin >> x; return x; } inline long rel() { long x; cin >> x; return x; } inline string res() { string x; cin >> x; return x; } //------------------------------------------------------- struct ConvexHullTrick { pair<long, long> Point[20100]; int p; ConvexHullTrick() : p(0) {} // aは単調増加でないといけない void Add(long a, long b) { while (p >= 2 && !Need(Point[p - 2], Point[p - 1], {a, b})) { p--; } Point[p] = {a, b}; p++; } // 1つはAddしていないといけない long Query(long x) { return Calc(x, findMax(x)); } // 最初のクエリの前にpを減らす必要がある void Start() { p--; } // 2分探索で探索 int findMax(long x) { int bf = 0; int bl = p - 1; while (bf != bl) { int bc = (bf + bl) / 2; if (bc == p || Calc(x, bc) >= Calc(x, bc + 1)) { bl = bc; } else { bf = bc + 1; } } return bf; } long Calc(long x, int i) { return Point[i].first * x + Point[i].second; } // p1,p2,p3があるときp2は必要か bool Need(pair<long, long> p1, pair<long, long> p2, pair<long, long> p3) { return (p2.first - p1.first) * (p2.second - p3.second) > (p3.first - p2.first) * (p1.second - p2.second); } void Free() { p = 0; } }; long H[200000]; void Calc() { int N = rei(); long C = rel(); for (int i = 0; i < N; i++) { H[i] = rei(); } ConvexHullTrick CHT; CHT.Add(2 * H[0], -H[0] * H[0] - C); for (int i = 1; i < N - 1; i++) { long t = H[i] * H[i] - CHT.Query(H[i]); CHT.Add(2 * H[i], -t - C - H[i] * H[i]); } long ans = H[N - 1] * H[N - 1] - CHT.Query(H[N - 1]); cout << ans << endl; } int main(int argc, char **argv) { ios::sync_with_stdio(false), cin.tie(0); cout.tie(0); Calc(); return 0; }
#include <algorithm> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> using namespace std; #define mod 1000000007 #define FOR(x, to) for (int x = 0; x < (to); x++) #define FORR(x, arr) for (auto &x : arr) #define ALL(a) (a.begin()), (a.end()) #define ZERO(a) memset(a, 0, sizeof(a)) #define MINUS(a) memset(a, 0xff, sizeof(a)) #define long long long inline int rei() { int x; cin >> x; return x; } inline long rel() { long x; cin >> x; return x; } inline string res() { string x; cin >> x; return x; } //------------------------------------------------------- struct ConvexHullTrick { pair<long, long> Point[201000]; int p; ConvexHullTrick() : p(0) {} // aは単調増加でないといけない void Add(long a, long b) { while (p >= 2 && !Need(Point[p - 2], Point[p - 1], {a, b})) { p--; } Point[p] = {a, b}; p++; } // 1つはAddしていないといけない long Query(long x) { return Calc(x, findMax(x)); } // 最初のクエリの前にpを減らす必要がある void Start() { p--; } // 2分探索で探索 int findMax(long x) { int bf = 0; int bl = p - 1; while (bf != bl) { int bc = (bf + bl) / 2; if (bc == p || Calc(x, bc) >= Calc(x, bc + 1)) { bl = bc; } else { bf = bc + 1; } } return bf; } long Calc(long x, int i) { return Point[i].first * x + Point[i].second; } // p1,p2,p3があるときp2は必要か bool Need(pair<long, long> p1, pair<long, long> p2, pair<long, long> p3) { return (p2.first - p1.first) * (p2.second - p3.second) > (p3.first - p2.first) * (p1.second - p2.second); } void Free() { p = 0; } }; long H[200000]; void Calc() { int N = rei(); long C = rel(); for (int i = 0; i < N; i++) { H[i] = rei(); } ConvexHullTrick CHT; CHT.Add(2 * H[0], -H[0] * H[0] - C); for (int i = 1; i < N - 1; i++) { long t = H[i] * H[i] - CHT.Query(H[i]); CHT.Add(2 * H[i], -t - C - H[i] * H[i]); } long ans = H[N - 1] * H[N - 1] - CHT.Query(H[N - 1]); cout << ans << endl; } int main(int argc, char **argv) { ios::sync_with_stdio(false), cin.tie(0); cout.tie(0); Calc(); return 0; }
replace
41
42
41
42
0
p03186
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define REP(i, a, b) for (int i = (a); i < (b); i++) #define rep(i, n) REP(i, 0, (n)) typedef long long ll; using namespace std; int main() { int a, b, c; cin >> a >> b >> c; int ans = 0; bool health = true; while (true) { if (health) { if (c) { ans++; c--; health = false; } else if (b) { b--; ans++; } else if (a) { a--; } else { break; } } else { if (b) { ans++; b--; } else if (a) { a--; } else { break; } health = true; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #define REP(i, a, b) for (int i = (a); i < (b); i++) #define rep(i, n) REP(i, 0, (n)) typedef long long ll; using namespace std; int main() { int a, b, c; cin >> a >> b >> c; int ans = 0; if (a + b >= c - 1) { ans = b + c; } else { ans = a + b + b; if (b != 0 || c != 0) ans++; } cout << ans << endl; return 0; }
replace
10
36
10
16
TLE
p03186
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int gcd(int x, int y) { if (y == 0) return x; return gcd(y, x % y); } int lcm(int x, int y) { return x * y / gcd(x, y); } int ctoi(char c) { if (c >= '0' && c <= '9') { return c - '0'; } return 0; } int main() { int A, B, C, ans = 0, f = 0; cin >> A >> B >> C; while (1) { if (C == 0) { ans += B; break; } if (f == 0) { if (C > 0) { C--; ans++; f = 1; } } else { if (B > 0) { B--; ans++; } else if (A > 0) { A--; } else { break; } f = 0; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int gcd(int x, int y) { if (y == 0) return x; return gcd(y, x % y); } int lcm(int x, int y) { return x * y / gcd(x, y); } int ctoi(char c) { if (c >= '0' && c <= '9') { return c - '0'; } return 0; } int main() { int A, B, C, ans = 0, f = 0; cin >> A >> B >> C; ans = min(A + B + 1, C) + B; cout << ans << endl; return 0; }
replace
24
49
24
25
TLE
p03186
C++
Time Limit Exceeded
#include <iostream> using namespace std; int main(void) { // Your code here! int a, b, c; std::cin >> a >> b >> c; bool x = true; int count = 0; while (true) { if (x && c != 0) { c--; count++; x = false; continue; } if (b != 0) { b--; count++; x = true; continue; } if (a != 0) { a--; x = true; continue; } break; } std::cout << count << std::endl; }
#include <iostream> using namespace std; int main(void) { // Your code here! int a, b, c; std::cin >> a >> b >> c; bool x = true; int count = 0; while (true) { if (x && c != 0) { c--; count++; x = false; continue; } if (b != 0) { b--; count++; x = true; continue; } if (b == 0 && c == 0) { break; } if (a != 0) { a--; x = true; continue; } break; } std::cout << count << std::endl; }
insert
21
21
21
24
TLE