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
p02647
C++
Time Limit Exceeded
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; #define int long long #define rep(i, n) for (int i = 0; i < n; i++) #define rep2(i, s, f) for (int i = s; i < f; i++) #define INF 1000000000000000000 #define MOD 1000000007 #define endl "\n" #define fcout cout << fixed << setprecision(15) typedef pair<int, int> P; // struct edge { int to, cost; }; int dx[8] = {1, 0, 0, -1, 1, 1, -1, -1}; int dy[8] = {0, -1, 1, 0, -1, 1, -1, 1}; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } bool prime(int n) { for (int i = 2; i <= sqrt(n); i++) { if (n % i == 0) return false; } return n != 1; } int gcd(int x, int y) { if (y == 0) return x; return gcd(y, x % y); } int lcm(int x, int y) { return x / gcd(x, y) * y; } int mod_pow(int n, int p, int m) { if (p == 0) return 1; if (p % 2 == 0) { int t = mod_pow(n, p / 2, m); return (t * t) % m; } return n * mod_pow(n, p - 1, m) % m; } int extGCD(int a, int b, int &x, int &y) { if (b == 0) { x = 1; y = 0; return a; } long long d = extGCD(b, a % b, y, x); y -= a / b * x; return d; } int modinv(int a, int m) { int b = m, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } int modinv2(int a, int mod) { return mod_pow(a, mod - 2, mod); } int digit(int x) { int cnt = 0; while (x > 0) { cnt += x % 10; x /= 10; } return cnt; } int read() { int tmp; cin >> tmp; return tmp; } int xor128() { static int x = 123456789, y = 362436069, z = 521288629, w = 88675123; int t = (x xor (x << 11)); x = y; y = z; z = w; return (w = (w xor (w >> 19)) xor (t xor (t >> 8))); } map<int, int> factaring(int x) { map<int, int> ans; int now = 2; while (now * now <= x) { if (x % now == 0) { x /= now; ans[now]++; } else now++; } if (x != 1) ans[x]++; return ans; } int comb(int n, int k, int mod) { if (n < k) return 0; if (n < 0 || k < 0) return 0; k = min(k, n - k); int x = 1, y = 1; rep(i, k) { y *= i + 1; y %= mod; } for (int i = n - k + 1; i <= n; i++) { x *= i; x %= mod; } return x * modinv(y, mod) % mod; } signed main() { // srand((unsigned)time(NULL)); cin.tie(nullptr); ios::sync_with_stdio(false); int n, k; int a[200005]; cin >> n >> k; rep(i, n) { cin >> a[i]; } rep(i, k) { int b[200005]; rep(j, n) b[j] = 0; rep(j, n) { for (int l = max((int)0, j - a[j]); l <= min(n - 1, j + a[j]); l++) { b[l]++; } } rep(j, n) a[j] = b[j]; bool fin = true; rep(j, n) { if (a[j] != n) fin = false; } if (fin) break; } rep(i, n) { cout << a[i] << " "; } cout << endl; return 0; }
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; #define int long long #define rep(i, n) for (int i = 0; i < n; i++) #define rep2(i, s, f) for (int i = s; i < f; i++) #define INF 1000000000000000000 #define MOD 1000000007 #define endl "\n" #define fcout cout << fixed << setprecision(15) typedef pair<int, int> P; // struct edge { int to, cost; }; int dx[8] = {1, 0, 0, -1, 1, 1, -1, -1}; int dy[8] = {0, -1, 1, 0, -1, 1, -1, 1}; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } bool prime(int n) { for (int i = 2; i <= sqrt(n); i++) { if (n % i == 0) return false; } return n != 1; } int gcd(int x, int y) { if (y == 0) return x; return gcd(y, x % y); } int lcm(int x, int y) { return x / gcd(x, y) * y; } int mod_pow(int n, int p, int m) { if (p == 0) return 1; if (p % 2 == 0) { int t = mod_pow(n, p / 2, m); return (t * t) % m; } return n * mod_pow(n, p - 1, m) % m; } int extGCD(int a, int b, int &x, int &y) { if (b == 0) { x = 1; y = 0; return a; } long long d = extGCD(b, a % b, y, x); y -= a / b * x; return d; } int modinv(int a, int m) { int b = m, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } int modinv2(int a, int mod) { return mod_pow(a, mod - 2, mod); } int digit(int x) { int cnt = 0; while (x > 0) { cnt += x % 10; x /= 10; } return cnt; } int read() { int tmp; cin >> tmp; return tmp; } int xor128() { static int x = 123456789, y = 362436069, z = 521288629, w = 88675123; int t = (x xor (x << 11)); x = y; y = z; z = w; return (w = (w xor (w >> 19)) xor (t xor (t >> 8))); } map<int, int> factaring(int x) { map<int, int> ans; int now = 2; while (now * now <= x) { if (x % now == 0) { x /= now; ans[now]++; } else now++; } if (x != 1) ans[x]++; return ans; } int comb(int n, int k, int mod) { if (n < k) return 0; if (n < 0 || k < 0) return 0; k = min(k, n - k); int x = 1, y = 1; rep(i, k) { y *= i + 1; y %= mod; } for (int i = n - k + 1; i <= n; i++) { x *= i; x %= mod; } return x * modinv(y, mod) % mod; } signed main() { // srand((unsigned)time(NULL)); cin.tie(nullptr); ios::sync_with_stdio(false); int n, k; int a[200005]; cin >> n >> k; rep(i, n) { cin >> a[i]; } rep(i, k) { int b[200005]; rep(j, n) b[j] = 0; rep(j, n) { b[max((int)0, j - a[j])]++; b[min(n - 1, j + a[j]) + 1]--; } rep(j, n) { if (j) b[j] += b[j - 1]; } rep(j, n) a[j] = b[j]; bool fin = true; rep(j, n) { if (a[j] != n) fin = false; } if (fin) break; } rep(i, n) { cout << a[i] << " "; } cout << endl; return 0; }
replace
146
149
146
152
TLE
p02647
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <string> #include <vector> using namespace std; using ll = long long; ll N, K; vector<ll> A; vector<ll> B; int main() { cin >> N >> K; A.resize(N); B.resize(N + 1); for (int i = 0; i < N; i++) { cin >> A[i]; } ll logN = log2(N) + 100000; for (ll i = 0; i < min(K, logN); i++) { fill(B.begin(), B.end(), 0LL); for (ll j = 0; j < N; j++) { B[max(j - A[j], 0LL)]++; if (j + A[j] < N - 1) B[j + A[j] + 1]--; } for (ll j = 0; j < N; j++) { A[j] = B[j]; B[j + 1] += B[j]; } } for (ll i = 0; i < N; i++) { cout << B[i]; if (i == N - 1) { cout << endl; } else { cout << " "; } } return 0; }
#include <algorithm> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <string> #include <vector> using namespace std; using ll = long long; ll N, K; vector<ll> A; vector<ll> B; int main() { cin >> N >> K; A.resize(N); B.resize(N + 1); for (int i = 0; i < N; i++) { cin >> A[i]; } ll logN = log2(N) + 100; for (ll i = 0; i < min(K, logN); i++) { fill(B.begin(), B.end(), 0LL); for (ll j = 0; j < N; j++) { B[max(j - A[j], 0LL)]++; if (j + A[j] < N - 1) B[j + A[j] + 1]--; } for (ll j = 0; j < N; j++) { A[j] = B[j]; B[j + 1] += B[j]; } } for (ll i = 0; i < N; i++) { cout << B[i]; if (i == N - 1) { cout << endl; } else { cout << " "; } } return 0; }
replace
25
26
25
26
TLE
p02647
C++
Runtime Error
#include <stdio.h> int x[200010], s[200010]; int main() { freopen("input.txt", "r", stdin); int a, b; scanf("%d%d", &a, &b); for (int i = 1; i <= a; i++) scanf("%d", &x[i]); for (int j = 1; j <= 50 && j <= b; j++) { for (int i = 1; i <= a; i++) { int L = i - x[i]; int R = i + x[i]; if (L < 1) L = 1; if (R > a) R = a; s[L]++, s[R + 1]--; } for (int i = 1; i <= a; i++) x[i] = x[i - 1] + s[i]; for (int i = 1; i <= a + 1; i++) s[i] = 0; } for (int i = 1; i <= a; i++) printf("%d ", x[i]); }
#include <stdio.h> int x[200010], s[200010]; int main() { int a, b; scanf("%d%d", &a, &b); for (int i = 1; i <= a; i++) scanf("%d", &x[i]); for (int j = 1; j <= 50 && j <= b; j++) { for (int i = 1; i <= a; i++) { int L = i - x[i]; int R = i + x[i]; if (L < 1) L = 1; if (R > a) R = a; s[L]++, s[R + 1]--; } for (int i = 1; i <= a; i++) x[i] = x[i - 1] + s[i]; for (int i = 1; i <= a + 1; i++) s[i] = 0; } for (int i = 1; i <= a; i++) printf("%d ", x[i]); }
delete
4
5
4
4
0
p02647
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 10; int N, K; int A[MAXN]; int sum[MAXN]; int n_count = 0; void do_operation() { int cost = 0; for (int i = 1; i <= N; ++i) sum[i] = 0; for (int i = 1; i <= N; ++i) { sum[max(1, i - A[i])]++; sum[min(N + 1, i + A[i] + 1)]--; } n_count = 0; for (int i = 1; i <= N; ++i) { cost += sum[i]; A[i] = cost; if (cost == N) ++n_count; } } void solve() { for (int i = 1; i <= N; ++i) if (A[i] == N) n_count += 1; for (int k = 1; k <= K; ++k) { if (n_count == N) break; do_operation(); } } int main() { scanf("%d %d", &N, &K); for (int i = 1; i <= N; ++i) scanf("%d", &A[i]); solve(); for (int i = 1; i <= N; ++i) printf("%d ", A[i]); printf("\n"); return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 10; int N, K; int A[MAXN]; int sum[MAXN]; int n_count = 0; void do_operation() { int cost = 0; for (int i = 1; i <= N; ++i) sum[i] = 0; for (int i = 1; i <= N; ++i) { sum[max(1, i - A[i])]++; sum[min(N + 1, i + A[i] + 1)]--; } n_count = 0; for (int i = 1; i <= N; ++i) { cost += sum[i]; A[i] = cost; if (cost == N) ++n_count; } } void solve() { for (int i = 1; i <= N; ++i) if (A[i] == N) n_count += 1; for (int k = 1; k <= K; ++k) { if (n_count == N) break; do_operation(); } } int main() { scanf("%d %d", &N, &K); for (int i = 1; i <= N; ++i) scanf("%d", &A[i]); solve(); for (int i = 1; i <= N; ++i) printf("%d ", A[i]); printf("\n"); return 0; }
replace
3
4
3
4
0
p02647
C++
Time Limit Exceeded
#include <bits/stdc++.h> #include <cctype> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) #define all(v) (v).begin(), (v).end() typedef long long int ll; #define pi 3.1415926535897932384 #define E9 1000000000 #define eps 1e-4 #define pii pair<int, int> int main() { int N, K; cin >> N >> K; int a[N]; rep(i, N) cin >> a[i]; rep(j, K) { vector<int> b(N + 1, 0); rep(i, N) { if (i - a[i] < 0) b[0]++; else b[i - a[i]]++; if (i + a[i] + 1 <= N) b[i + a[i] + 1]--; else b[N]--; } rep(i, N) { if (i == 0) a[i] = b[i]; else a[i] = a[i - 1] + b[i]; } } rep(i, N) { if (i > 0) cout << " "; cout << a[i]; } cout << endl; // cout << fixed << setprecision(10); return 0; }
#include <bits/stdc++.h> #include <cctype> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) #define all(v) (v).begin(), (v).end() typedef long long int ll; #define pi 3.1415926535897932384 #define E9 1000000000 #define eps 1e-4 #define pii pair<int, int> int main() { int N, K; cin >> N >> K; int a[N]; rep(i, N) cin >> a[i]; rep(j, min(K, 1000)) { vector<int> b(N + 1, 0); rep(i, N) { if (i - a[i] < 0) b[0]++; else b[i - a[i]]++; if (i + a[i] + 1 <= N) b[i + a[i] + 1]--; else b[N]--; } rep(i, N) { if (i == 0) a[i] = b[i]; else a[i] = a[i - 1] + b[i]; } } rep(i, N) { if (i > 0) cout << " "; cout << a[i]; } cout << endl; // cout << fixed << setprecision(10); return 0; }
replace
17
18
17
18
TLE
p02647
C++
Runtime Error
#include "bits/stdc++.h" using namespace std; #define int int64_t int ar[(int)2e5 + 10]; int br[(int)2e5 + 10]; int32_t main() { ios::sync_with_stdio(false); cin.tie(NULL); int count = 0; int n, k; cin >> n >> k; int mini = n; for (int i = 1; i <= n; i++) { cin >> ar[i]; mini = min(mini, ar[i]); } while (k > 0 and mini < n) { k--; count++; for (int i = 1; i <= n; i++) { int lw = max(i - ar[i], (int)1); int up = min(i + ar[i] + 1, n + 1); br[lw] += 1; br[up] -= 1; } mini = n; for (int i = 1; i <= n; i++) { br[i] += br[i - 1]; ar[i] = br[i]; mini = min(mini, ar[i]); } for (int i = 1; i <= 10000; i++) br[i] = 0; } for (int i = 1; i <= n; i++) cout << ar[i] << " "; cout << "\n"; return 0; }
#include "bits/stdc++.h" using namespace std; #define int int64_t int ar[(int)2e5 + 10]; int br[(int)2e5 + 10]; int32_t main() { ios::sync_with_stdio(false); cin.tie(NULL); int count = 0; int n, k; cin >> n >> k; int mini = n; for (int i = 1; i <= n; i++) { cin >> ar[i]; mini = min(mini, ar[i]); } while (k > 0 and mini < n) { k--; count++; for (int i = 1; i <= n; i++) { int lw = max(i - ar[i], (int)1); int up = min(i + ar[i] + 1, n + 1); br[lw] += 1; br[up] -= 1; } mini = n; for (int i = 1; i <= n; i++) { br[i] += br[i - 1]; ar[i] = br[i]; mini = min(mini, ar[i]); } for (int i = 1; i <= n + 1; i++) br[i] = 0; } for (int i = 1; i <= n; i++) cout << ar[i] << " "; cout << "\n"; return 0; }
replace
31
32
31
32
0
p02647
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, n) for (ll i = 0; i < n; ++i) typedef long long ll; using namespace std; const int INF = 1e9; int main() { int n, k; cin >> n >> k; vector<ll> a(n); rep(i, n) cin >> a[i]; vector<ll> imos(n + 1); rep(_, k) { imos.assign(n + 1, 0); rep(i, n) { if (i - a[i] <= 0) imos[0]++; else imos[i - a[i]]++; if (i + a[i] + 1 <= n) imos[i + a[i] + 1]--; else imos[n]--; } rep(i, n) { imos[i + 1] += imos[i]; } rep(i, n) a[i] = imos[i]; } rep(i, n) cout << imos[i] << " "; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (ll i = 0; i < n; ++i) typedef long long ll; using namespace std; const int INF = 1e9; int main() { int n, k; cin >> n >> k; vector<ll> a(n); rep(i, n) cin >> a[i]; vector<ll> imos(n + 1); rep(_, min(k, 100)) { imos.assign(n + 1, 0); rep(i, n) { if (i - a[i] <= 0) imos[0]++; else imos[i - a[i]]++; if (i + a[i] + 1 <= n) imos[i + a[i] + 1]--; else imos[n]--; } rep(i, n) { imos[i + 1] += imos[i]; } rep(i, n) a[i] = imos[i]; } rep(i, n) cout << imos[i] << " "; return 0; }
replace
14
15
14
15
TLE
p02647
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define ll long long #define VI vector<int> #define VLL vector<ll> #define PII pair<int, int> #define mp make_pair #define pb push_back #define PI acos(-1) #define ld long double const int MxN = 100001; #define FOR(i, a, b) for (int i = a; i < b; ++i) #define RFOR(i, a, b) for (int i = a; i >= b; --i) /* VARIABLES */ int n, k; VI a(MxN); /* END OF VARIABLES */ int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> k; FOR(i, 0, n) cin >> a[i]; if (k >= 3 * log2((ld)n)) { FOR(i, 0, n) cout << n << ' '; return 0; } FOR(i, 0, k) { VI tmp(n + 1); FOR(j, 0, n) { int l = max(0, j - a[j]); int r = min(n - 1, j + a[j]); tmp[l]++; if (r + 1 < n) tmp[r + 1]--; } FOR(j, 1, n) { tmp[j] += tmp[j - 1]; } FOR(j, 0, n) { a[j] = tmp[j]; } } FOR(i, 0, n) cout << a[i] << " "; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long #define VI vector<int> #define VLL vector<ll> #define PII pair<int, int> #define mp make_pair #define pb push_back #define PI acos(-1) #define ld long double const int MxN = 200001; #define FOR(i, a, b) for (int i = a; i < b; ++i) #define RFOR(i, a, b) for (int i = a; i >= b; --i) /* VARIABLES */ int n, k; VI a(MxN); /* END OF VARIABLES */ int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> k; FOR(i, 0, n) cin >> a[i]; if (k >= 3 * log2((ld)n)) { FOR(i, 0, n) cout << n << ' '; return 0; } FOR(i, 0, k) { VI tmp(n + 1); FOR(j, 0, n) { int l = max(0, j - a[j]); int r = min(n - 1, j + a[j]); tmp[l]++; if (r + 1 < n) tmp[r + 1]--; } FOR(j, 1, n) { tmp[j] += tmp[j - 1]; } FOR(j, 0, n) { a[j] = tmp[j]; } } FOR(i, 0, n) cout << a[i] << " "; return 0; }
replace
11
12
11
12
0
p02647
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <numeric> #include <string> #include <tuple> #include <utility> #include <vector> #define REP(i, a, b) for (int i = int(a); i < int(b); i++) using namespace std; using ll = long long int; using P = pair<ll, ll>; // clang-format off #ifdef _DEBUG_ #define dump(...) do{ cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; PPPPP(__VA_ARGS__); cerr << endl; } while(false) template<typename T> void PPPPP(T t) { cerr << t; } template<typename T, typename... S> void PPPPP(T t, S... s) { cerr << t << ", "; PPPPP(s...); } #else #define dump(...) do{ } while(false) #endif template<typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); } template<typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); } template<typename T> bool chmin(T &a, T b) { if (a > b) {a = b; return true; } return false; } template<typename T> bool chmax(T &a, T b) { if (a < b) {a = b; return true; } return false; } template<typename T> void print(T a) { cout << a << endl; } template<typename T, typename... Ts> void print(T a, Ts... ts) { cout << a << ' '; print(ts...); } template<typename T> istream &operator,(istream &in, T &t) { return in >> t; } // clang-format on int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n, k; cin, n, k; vector<int> a(n); REP(i, 0, n) { cin, a[i]; } REP(t, 0, k) { vector<int> imos(n, 0); REP(i, 0, n) { int l = max(0, i - a[i]); int r = min(n, i + a[i] + 1); imos[l]++; if (r != n) { imos[r]--; } } REP(i, 0, n - 1) { imos[i + 1] += imos[i]; } a = move(imos); } REP(i, 0, n) { cout << a[i] << " \n"[i + 1 == n]; } return 0; }
#include <algorithm> #include <iostream> #include <numeric> #include <string> #include <tuple> #include <utility> #include <vector> #define REP(i, a, b) for (int i = int(a); i < int(b); i++) using namespace std; using ll = long long int; using P = pair<ll, ll>; // clang-format off #ifdef _DEBUG_ #define dump(...) do{ cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; PPPPP(__VA_ARGS__); cerr << endl; } while(false) template<typename T> void PPPPP(T t) { cerr << t; } template<typename T, typename... S> void PPPPP(T t, S... s) { cerr << t << ", "; PPPPP(s...); } #else #define dump(...) do{ } while(false) #endif template<typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); } template<typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); } template<typename T> bool chmin(T &a, T b) { if (a > b) {a = b; return true; } return false; } template<typename T> bool chmax(T &a, T b) { if (a < b) {a = b; return true; } return false; } template<typename T> void print(T a) { cout << a << endl; } template<typename T, typename... Ts> void print(T a, Ts... ts) { cout << a << ' '; print(ts...); } template<typename T> istream &operator,(istream &in, T &t) { return in >> t; } // clang-format on int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n, k; cin, n, k; vector<int> a(n); REP(i, 0, n) { cin, a[i]; } REP(t, 0, k) { vector<int> imos(n, 0); REP(i, 0, n) { int l = max(0, i - a[i]); int r = min(n, i + a[i] + 1); imos[l]++; if (r != n) { imos[r]--; } } REP(i, 0, n - 1) { imos[i + 1] += imos[i]; } a = move(imos); bool ok = true; REP(i, 0, n) { if (a[i] != n) ok = false; } if (ok) { dump(t); break; } } REP(i, 0, n) { cout << a[i] << " \n"[i + 1 == n]; } return 0; }
insert
49
49
49
58
TLE
p02647
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } int it = max(k, (int)floor(log(k))); for (int i = 0; i < it; i++) { vector<int> b(n + 1); for (int j = 0; j < n; j++) { int l = max(0, j - a[j]); int r = min(n - 1, j + a[j]); b[l]++; b[r + 1]--; } for (int j = 0; j < n; j++) { b[j + 1] += b[j]; a[j] = b[j]; } } for (int i = 0; i < n; i++) { cout << a[i] << ' '; } cout << '\n'; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } k = min(k, 100); for (int i = 0; i < k; i++) { vector<int> b(n + 1); for (int j = 0; j < n; j++) { int l = max(0, j - a[j]); int r = min(n - 1, j + a[j]); b[l]++; b[r + 1]--; } for (int j = 0; j < n; j++) { b[j + 1] += b[j]; a[j] = b[j]; } } for (int i = 0; i < n; i++) { cout << a[i] << ' '; } cout << '\n'; return 0; }
replace
12
14
12
14
TLE
p02647
C++
Time Limit Exceeded
// #include<cstdio> // #include<cassert> // #include<iostream> // #include<cstring> // #include<algorithm> #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define MAX ((int)1e6 + 5) #define MAXL ((ll)1e18 + 5) #define MAX_X ((ll)1e5 + 2) #define pi (2.0 * acos(0)) #define M ((int)1e6 + 7) #define MOD ((int)1e9 + 7) #define NN ((int)1e6 + 7) #define N ((int)2e5 + 7) #define eps (0) #define fastio ios_base::sync_with_stdio(false), cin.tie(NULL) #define logn 29 #define endl "\n" #define mp make_pair #define BUCK 105 #define LEF (idx << 1) #define RIG ((idx << 1) | 1) // #define int ll using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef unsigned long long ull; /*fast io ios_base::sync_with_stdio(false); cin.tie(NULL); */ typedef tree<pair<ll, pair<ll, ll>>, null_type, less<pair<ll, pair<ll, ll>>>, rb_tree_tag, tree_order_statistics_node_update> o_set; /// o_set s; /// s.order_of_key(k) : Number of items strictly smaller than k . /// *(s.find_by_order(k)) : K-th element in a set (counting from zero). int arr[N], tmp[N]; int main() { fastio; int n, k; cin >> n >> k; for (int i = 1; i <= n; i++) cin >> arr[i]; for (int i = 1; i <= k; i++) { bool flag = 1; for (int i = 1; i <= n; i++) { if (arr[i] < n) { flag = 0; break; } } memset(tmp, 0, sizeof tmp); for (int i = 1; i <= n; i++) { int lef = max(1, i - arr[i]), rig = min(n + 1, i + arr[i] + 1); tmp[lef]++; tmp[rig]--; } for (int i = 1; i <= n; i++) tmp[i] += tmp[i - 1]; for (int i = 1; i <= n; i++) arr[i] = tmp[i]; } for (int i = 1; i <= n; i++) cout << arr[i] << " "; return 0; }
// #include<cstdio> // #include<cassert> // #include<iostream> // #include<cstring> // #include<algorithm> #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define MAX ((int)1e6 + 5) #define MAXL ((ll)1e18 + 5) #define MAX_X ((ll)1e5 + 2) #define pi (2.0 * acos(0)) #define M ((int)1e6 + 7) #define MOD ((int)1e9 + 7) #define NN ((int)1e6 + 7) #define N ((int)2e5 + 7) #define eps (0) #define fastio ios_base::sync_with_stdio(false), cin.tie(NULL) #define logn 29 #define endl "\n" #define mp make_pair #define BUCK 105 #define LEF (idx << 1) #define RIG ((idx << 1) | 1) // #define int ll using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef unsigned long long ull; /*fast io ios_base::sync_with_stdio(false); cin.tie(NULL); */ typedef tree<pair<ll, pair<ll, ll>>, null_type, less<pair<ll, pair<ll, ll>>>, rb_tree_tag, tree_order_statistics_node_update> o_set; /// o_set s; /// s.order_of_key(k) : Number of items strictly smaller than k . /// *(s.find_by_order(k)) : K-th element in a set (counting from zero). int arr[N], tmp[N]; int main() { fastio; int n, k; cin >> n >> k; for (int i = 1; i <= n; i++) cin >> arr[i]; for (int i = 1; i <= k; i++) { bool flag = 1; for (int i = 1; i <= n; i++) { if (arr[i] < n) { flag = 0; break; } } if (flag) break; memset(tmp, 0, sizeof tmp); for (int i = 1; i <= n; i++) { int lef = max(1, i - arr[i]), rig = min(n + 1, i + arr[i] + 1); tmp[lef]++; tmp[rig]--; } for (int i = 1; i <= n; i++) tmp[i] += tmp[i - 1]; for (int i = 1; i <= n; i++) arr[i] = tmp[i]; } for (int i = 1; i <= n; i++) cout << arr[i] << " "; return 0; }
insert
60
60
60
62
TLE
p02647
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define int long long void solve() { int n, m, k, i, j, count = 0, sum = 0, maxi = 0, mini = 1e18, flag = 0; cin >> n >> k; vector<int> a(n + 1); for (i = 1; i <= n; i++) { cin >> a[i]; } for (i = 1; i <= k; i++) { vector<int> freq(n + 1, 0); for (j = 1; j <= n; j++) { int l = max((long long)1, j - a[j]); int r = min(n, j + a[j]); freq[l]++; if (r + 1 <= n) freq[r + 1]--; } for (j = 2; j <= n; j++) { freq[j] += freq[j - 1]; } for (j = 1; j <= n; j++) { if (freq[j] != n) { flag = 1; break; } } a = freq; if (flag == 0) break; } for (i = 1; i <= n; i++) { cout << a[i] << " "; } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; // cin>>t; while (t--) { solve(); } }
#include <bits/stdc++.h> using namespace std; #define int long long void solve() { int n, m, k, i, j, count = 0, sum = 0, maxi = 0, mini = 1e18, flag = 0; cin >> n >> k; vector<int> a(n + 1); for (i = 1; i <= n; i++) { cin >> a[i]; } for (i = 1; i <= k; i++) { vector<int> freq(n + 1, 0); flag = 0; for (j = 1; j <= n; j++) { int l = max((long long)1, j - a[j]); int r = min(n, j + a[j]); freq[l]++; if (r + 1 <= n) freq[r + 1]--; } for (j = 2; j <= n; j++) { freq[j] += freq[j - 1]; } for (j = 1; j <= n; j++) { if (freq[j] != n) { flag = 1; break; } } a = freq; if (flag == 0) break; } for (i = 1; i <= n; i++) { cout << a[i] << " "; } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; // cin>>t; while (t--) { solve(); } }
insert
15
15
15
16
TLE
p02647
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; ++i) using ll = long long; const ll MOD = 1000000007; // 998244353; int dp[200001][500]; void solve() { int n, k; cin >> n >> k; rep(i, n) cin >> dp[0][i]; int j = 0; while (k--) { rep(i, n) { dp[j + 1][max(i - dp[j][i], 0)]++; dp[j + 1][min(i + dp[j][i] + 1, n)]--; } rep(i, n) { dp[j + 1][i + 1] += dp[j + 1][i]; } j++; if (dp[j][0] == n && dp[j][n - 1] == n) break; } rep(i, n) cout << dp[j][i] << " "; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); solve(); }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; ++i) using ll = long long; const ll MOD = 1000000007; // 998244353; int dp[400][200001]; void solve() { int n, k; cin >> n >> k; rep(i, n) cin >> dp[0][i]; int j = 0; while (k--) { rep(i, n) { dp[j + 1][max(i - dp[j][i], 0)]++; dp[j + 1][min(i + dp[j][i] + 1, n)]--; } rep(i, n) { dp[j + 1][i + 1] += dp[j + 1][i]; } j++; if (dp[j][0] == n && dp[j][n - 1] == n) break; } rep(i, n) cout << dp[j][i] << " "; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); solve(); }
replace
5
6
5
6
-11
p02647
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; // #define int long long #define rep(i, n) for (long long i = (long long)(0); i < (long long)(n); ++i) #define reps(i, n) for (long long i = (long long)(1); i <= (long long)(n); ++i) #define rrep(i, n) for (long long i = ((long long)(n)-1); i >= 0; i--) #define rreps(i, n) for (long long i = ((long long)(n)); i > 0; i--) #define irep(i, m, n) \ for (long long i = (long long)(m); i < (long long)(n); ++i) #define ireps(i, m, n) \ for (long long i = (long long)(m); i <= (long long)(n); ++i) #define SORT(v, n) sort(v, v + n); #define REVERSE(v, n) reverse(v, v + n); #define vsort(v) sort(v.begin(), v.end()); #define all(v) v.begin(), v.end() #define mp(n, m) make_pair(n, m); #define cout(d) cout << d << endl; #define coutd(d) cout << std::setprecision(10) << d << endl; #define cinline(n) getline(cin, n); #define replace_all(s, b, a) replace(s.begin(), s.end(), b, a); #define PI (acos(-1)) #define FILL(v, n, x) fill(v, v + n, x); #define sz(x) long long(x.size()) using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using vvll = vector<vll>; using pii = pair<int, int>; using pll = pair<ll, ll>; using vs = vector<string>; using vpll = vector<pair<ll, ll>>; using vtp = vector<tuple<ll, ll, ll>>; using vb = vector<bool>; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } const ll INF = 1e9; const ll MOD = 1e9 + 7; const ll LINF = 1e18; signed main() { cin.tie(0); ios::sync_with_stdio(false); ll n, k; cin >> n >> k; vll a(n); rep(i, n) cin >> a[i]; rep(_, k) { // imos vll l(n + 1); rep(i, n) { l[i]++; ll r = min(n + 1, i + a[i] + 1); l[r]--; } rep(i, n) l[i + 1] += l[i]; vll r(n + 1); rrep(i, n) { r[i + 1]++; ll l = max(0LL, i + 1 - a[i] - 1); r[l]--; } rrep(i, n) r[i] += r[i + 1]; // rep(i,n+1) cout<<l[i]<<' '; // cout<<endl; // rep(i,n+1) cout<<r[i]<<" "; // cout<<endl; rep(i, n) { a[i] = l[i] + r[i + 1] - 1; } bool flg = true; rep(i, n) { if (a[i] != n) flg = false; } if (flg) { rep(i, n) cout << a[i] << endl; return 0; } } rep(i, n) cout << a[i] << endl; }
#include <bits/stdc++.h> using namespace std; // #define int long long #define rep(i, n) for (long long i = (long long)(0); i < (long long)(n); ++i) #define reps(i, n) for (long long i = (long long)(1); i <= (long long)(n); ++i) #define rrep(i, n) for (long long i = ((long long)(n)-1); i >= 0; i--) #define rreps(i, n) for (long long i = ((long long)(n)); i > 0; i--) #define irep(i, m, n) \ for (long long i = (long long)(m); i < (long long)(n); ++i) #define ireps(i, m, n) \ for (long long i = (long long)(m); i <= (long long)(n); ++i) #define SORT(v, n) sort(v, v + n); #define REVERSE(v, n) reverse(v, v + n); #define vsort(v) sort(v.begin(), v.end()); #define all(v) v.begin(), v.end() #define mp(n, m) make_pair(n, m); #define cout(d) cout << d << endl; #define coutd(d) cout << std::setprecision(10) << d << endl; #define cinline(n) getline(cin, n); #define replace_all(s, b, a) replace(s.begin(), s.end(), b, a); #define PI (acos(-1)) #define FILL(v, n, x) fill(v, v + n, x); #define sz(x) long long(x.size()) using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using vvll = vector<vll>; using pii = pair<int, int>; using pll = pair<ll, ll>; using vs = vector<string>; using vpll = vector<pair<ll, ll>>; using vtp = vector<tuple<ll, ll, ll>>; using vb = vector<bool>; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } const ll INF = 1e9; const ll MOD = 1e9 + 7; const ll LINF = 1e18; signed main() { cin.tie(0); ios::sync_with_stdio(false); ll n, k; cin >> n >> k; vll a(n); rep(i, n) cin >> a[i]; rep(_, k) { // imos vll l(n + 1); rep(i, n) { l[i]++; ll r = min(n, i + a[i] + 1); l[r]--; } rep(i, n) l[i + 1] += l[i]; vll r(n + 1); rrep(i, n) { r[i + 1]++; ll l = max(0LL, i + 1 - a[i] - 1); r[l]--; } rrep(i, n) r[i] += r[i + 1]; // rep(i,n+1) cout<<l[i]<<' '; // cout<<endl; // rep(i,n+1) cout<<r[i]<<" "; // cout<<endl; rep(i, n) { a[i] = l[i] + r[i + 1] - 1; } bool flg = true; rep(i, n) { if (a[i] != n) flg = false; } if (flg) { rep(i, n) cout << a[i] << endl; return 0; } } rep(i, n) cout << a[i] << endl; }
replace
69
70
69
70
0
p02647
C++
Time Limit Exceeded
// while (clock()<=69*CLOCKS_PER_SEC) // #pragma comment(linker, "/stack:200000000") // #pragma GCC optimize("O3") // #pragma GCC optimize("Ofast") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") // #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define pb push_back #define SZ(x) ((int)(x).size()) #define ALL(x) x.begin(), x.end() #define all(x) x.begin(), x.end() #define fi first #define se second #define _upgrade \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define erase_duplicates(x) \ sort(all(x)); \ (x).resize(distance((x).begin(), unique(all(x)))); using namespace std; using namespace __gnu_pbds; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; // X.find_by_order(k); - zwraca iterator na k-ty element (numeracja od zerowego) // X.order_of_key(k); - zwraca liczbę elementów ostro mniejszych niż k typedef long long LL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; typedef vector<PII> VPII; typedef vector<PLL> VPLL; typedef vector<LL> VLL; typedef vector<int> VI; typedef vector<string> VS; typedef vector<char> VC; typedef long double LD; typedef pair<LD, LD> PLD; typedef vector<LD> VLD; typedef vector<PLD> VPLD; template <class TH> void _dbg(const char *sdbg, TH h) { cerr << sdbg << " = " << h << endl; } template <class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while (*sdbg != ',') cerr << *sdbg++; cerr << " = " << h << ", "; _dbg(sdbg + 1, a...); } #ifdef LOCAL #define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define dbg(...) #define cerr \ if (0) \ cout #endif const int maxn = (1e6) + 7; const int maxk = 20; const int inf = (1e9) + 7; const LL LLinf = ((LL)1e18) + 7LL; const LD eps = 1e-9; const LL mod = 1e9 + 7; // ***************************** CODE ***************************** // LL tab[maxn]; VI rob(VI V) { for (int i = 0; i < SZ(V); i++) tab[i] = 0; for (int i = 0; i < SZ(V); i++) { tab[max(0, i - V[i])]++; tab[i + V[i] + 1]--; } for (int i = 0; i < SZ(V); i++) { if (i > 0) tab[i] += tab[i - 1]; V[i] = tab[i]; } return V; } int main() { _upgrade int n, k; cin >> n >> k; VI V; for (int i = 0; i < n; i++) { int a; cin >> a; V.pb(a); } while (k--) { bool ok = 0; for (auto s : V) if (s < SZ(V)) ok = 1; if (ok) V = rob(V); } for (auto s : V) cout << s << " "; return 0; }
// while (clock()<=69*CLOCKS_PER_SEC) // #pragma comment(linker, "/stack:200000000") // #pragma GCC optimize("O3") // #pragma GCC optimize("Ofast") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") // #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define pb push_back #define SZ(x) ((int)(x).size()) #define ALL(x) x.begin(), x.end() #define all(x) x.begin(), x.end() #define fi first #define se second #define _upgrade \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define erase_duplicates(x) \ sort(all(x)); \ (x).resize(distance((x).begin(), unique(all(x)))); using namespace std; using namespace __gnu_pbds; template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; // X.find_by_order(k); - zwraca iterator na k-ty element (numeracja od zerowego) // X.order_of_key(k); - zwraca liczbę elementów ostro mniejszych niż k typedef long long LL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; typedef vector<PII> VPII; typedef vector<PLL> VPLL; typedef vector<LL> VLL; typedef vector<int> VI; typedef vector<string> VS; typedef vector<char> VC; typedef long double LD; typedef pair<LD, LD> PLD; typedef vector<LD> VLD; typedef vector<PLD> VPLD; template <class TH> void _dbg(const char *sdbg, TH h) { cerr << sdbg << " = " << h << endl; } template <class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while (*sdbg != ',') cerr << *sdbg++; cerr << " = " << h << ", "; _dbg(sdbg + 1, a...); } #ifdef LOCAL #define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define dbg(...) #define cerr \ if (0) \ cout #endif const int maxn = (1e6) + 7; const int maxk = 20; const int inf = (1e9) + 7; const LL LLinf = ((LL)1e18) + 7LL; const LD eps = 1e-9; const LL mod = 1e9 + 7; // ***************************** CODE ***************************** // LL tab[maxn]; VI rob(VI V) { for (int i = 0; i < SZ(V); i++) tab[i] = 0; for (int i = 0; i < SZ(V); i++) { tab[max(0, i - V[i])]++; tab[i + V[i] + 1]--; } for (int i = 0; i < SZ(V); i++) { if (i > 0) tab[i] += tab[i - 1]; V[i] = tab[i]; } return V; } int main() { _upgrade int n, k; cin >> n >> k; VI V; for (int i = 0; i < n; i++) { int a; cin >> a; V.pb(a); } while (k--) { bool ok = 0; for (auto s : V) if (s < SZ(V)) ok = 1; if (ok) V = rob(V); else break; } for (auto s : V) cout << s << " "; return 0; }
insert
108
108
108
110
TLE
p02647
C++
Time Limit Exceeded
#include <algorithm> #include <bits/stdc++.h> #include <bitset> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <vector> using namespace std; typedef long long ll; ll n, k; ll a[200005]; ll b[200005] = {0}; int main(void) { scanf("%lld%lld", &n, &k); for (ll i = 0; i < n; i++) { scanf("%lld", &a[i]); } ll i1 = 0; while (true) { ll now = 0; memset(b, 0, sizeof(b)); for (ll i = 0; i < n; i++) { if (i - a[i] < 0) b[0]++; else b[i - a[i]]++; if (i + a[i] < n) b[i + a[i] + 1]--; } for (ll i = 1; i < n; i++) b[i] += b[i - 1]; for (ll i = 0; i < n; i++) if (b[i] > n) now++; if (now == n) break; i1++; if (i1 == k) break; for (ll i = 0; i < n; i++) a[i] = b[i]; } if (i1 == k) { for (ll i = 0; i < n; i++) { printf("%lld ", b[i]); } } else { for (ll i = 0; i < n; i++) { printf("%lld ", n); } } }
#include <algorithm> #include <bits/stdc++.h> #include <bitset> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <map> #include <queue> #include <set> #include <vector> using namespace std; typedef long long ll; ll n, k; ll a[200005]; ll b[200005] = {0}; int main(void) { scanf("%lld%lld", &n, &k); for (ll i = 0; i < n; i++) { scanf("%lld", &a[i]); } ll i1 = 0; while (true) { ll now = 0; memset(b, 0, sizeof(b)); for (ll i = 0; i < n; i++) { if (i - a[i] < 0) b[0]++; else b[i - a[i]]++; if (i + a[i] < n) b[i + a[i] + 1]--; } for (ll i = 1; i < n; i++) b[i] += b[i - 1]; for (ll i = 0; i < n; i++) if (b[i] >= n) now++; if (now == n) break; i1++; if (i1 == k) break; for (ll i = 0; i < n; i++) a[i] = b[i]; } if (i1 == k) { for (ll i = 0; i < n; i++) { printf("%lld ", b[i]); } } else { for (ll i = 0; i < n; i++) { printf("%lld ", n); } } }
replace
36
37
36
37
TLE
p02647
C++
Runtime Error
#include <bits/stdc++.h> #define taskname "" #define pb push_back #define eb emplace_back #define fi first #define se second #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define for0(i, n) for (int i = 0; i < (int)(n); ++i) #define for1(i, n) for (int i = 1; i <= (int)(n); ++i) #define ford(i, n) for (int i = (int)(n)-1; i >= 0; --i) #define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i) using namespace std; typedef long long ll; typedef long double ld; typedef complex<ld> cd; typedef vector<cd> vcd; typedef vector<int> vi; template <class T> using v2d = vector<vector<T>>; template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; } template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; } mt19937 rng(chrono::system_clock::now().time_since_epoch().count()); const int maxN = 1e5 + 10; int n, k; ll a[maxN], b[maxN]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> k; for1(i, n) { cin >> a[i]; } uin(k, 20000000 / n); while (k--) { fill(b + 1, b + n + 1, 0); for1(i, n) { int l = max(i - a[i], 1LL), r = min(i + a[i], (ll)n); ++b[l]; if (r < n) { --b[r + 1]; } } for1(i, n) { b[i] += b[i - 1]; a[i] = b[i]; } } for1(i, n) { cout << a[i] << ' '; } return 0; }
#include <bits/stdc++.h> #define taskname "" #define pb push_back #define eb emplace_back #define fi first #define se second #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define for0(i, n) for (int i = 0; i < (int)(n); ++i) #define for1(i, n) for (int i = 1; i <= (int)(n); ++i) #define ford(i, n) for (int i = (int)(n)-1; i >= 0; --i) #define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i) using namespace std; typedef long long ll; typedef long double ld; typedef complex<ld> cd; typedef vector<cd> vcd; typedef vector<int> vi; template <class T> using v2d = vector<vector<T>>; template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; } template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; } mt19937 rng(chrono::system_clock::now().time_since_epoch().count()); const int maxN = 2e5 + 10; int n, k; ll a[maxN], b[maxN]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> k; for1(i, n) { cin >> a[i]; } uin(k, 20000000 / n); while (k--) { fill(b + 1, b + n + 1, 0); for1(i, n) { int l = max(i - a[i], 1LL), r = min(i + a[i], (ll)n); ++b[l]; if (r < n) { --b[r + 1]; } } for1(i, n) { b[i] += b[i - 1]; a[i] = b[i]; } } for1(i, n) { cout << a[i] << ' '; } return 0; }
replace
28
29
28
29
0
p02647
C++
Runtime Error
#include <bits/stdc++.h> #define int long long using namespace std; int a[100010], c[100010]; int max(int a, int b) { return a > b ? a : b; } inline int read() { int X = 0, w = 0; char ch = 0; while (!isdigit(ch)) { w |= ch == '-'; ch = getchar(); } while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar(); return w ? -X : X; } inline void write(int x) { if (x < 0) putchar('-'), x = -x; if (x > 9) write(x / 10); putchar(x % 10 + '0'); } inline void writeln(int x) { if (x < 0) putchar('-'), x = -x; if (x > 9) write(x / 10); putchar(x % 10 + '0'); puts(""); } signed main() { int n = read(), k = read(); for (int i = 1; i <= n; i++) a[i] = read(); for (int j = 1; j <= k; j++) { memset(c, 0, sizeof(c)); for (int i = 1; i <= n; i++) { c[max(1, i - a[i])]++; c[min(n + 1, i + a[i] + 1)]--; } int s = 0, f = 1; for (int i = 1; i <= n; i++) { a[i] = s += c[i]; if (a[i] < n) f = 0; } if (f) break; } for (int i = 1; i <= n; i++) write(a[i]), cout << " "; puts(""); return 0; }
#include <bits/stdc++.h> #define int long long using namespace std; int a[200010], c[200010]; int max(int a, int b) { return a > b ? a : b; } inline int read() { int X = 0, w = 0; char ch = 0; while (!isdigit(ch)) { w |= ch == '-'; ch = getchar(); } while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar(); return w ? -X : X; } inline void write(int x) { if (x < 0) putchar('-'), x = -x; if (x > 9) write(x / 10); putchar(x % 10 + '0'); } inline void writeln(int x) { if (x < 0) putchar('-'), x = -x; if (x > 9) write(x / 10); putchar(x % 10 + '0'); puts(""); } signed main() { int n = read(), k = read(); for (int i = 1; i <= n; i++) a[i] = read(); for (int j = 1; j <= k; j++) { memset(c, 0, sizeof(c)); for (int i = 1; i <= n; i++) { c[max(1, i - a[i])]++; c[min(n + 1, i + a[i] + 1)]--; } int s = 0, f = 1; for (int i = 1; i <= n; i++) { a[i] = s += c[i]; if (a[i] < n) f = 0; } if (f) break; } for (int i = 1; i <= n; i++) write(a[i]), cout << " "; puts(""); return 0; }
replace
4
5
4
5
0
p02647
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> intensity(n); for (int i = 0; i < n; i++) { cin >> intensity[i]; } for (int j = 0; j < k; j++) { vector<int> power(n + 1); for (int i = 0; i < n; i++) { int start = i - intensity[i]; int end = i + intensity[i] + 1; if (start < 0) start = 0; if (end >= n) end = n; power[start] += 1; power[end] -= 1; } long long current = 0; for (int i = 0; i < n; i++) { current += power[i]; intensity[i] = current; } } for (int x : intensity) { cout << x << " "; } }
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> intensity(n); for (int i = 0; i < n; i++) { cin >> intensity[i]; } for (int j = 0; j < min(k, 50); j++) { vector<int> power(n + 1); for (int i = 0; i < n; i++) { int start = i - intensity[i]; int end = i + intensity[i] + 1; if (start < 0) start = 0; if (end >= n) end = n; power[start] += 1; power[end] -= 1; } long long current = 0; for (int i = 0; i < n; i++) { current += power[i]; intensity[i] = current; } } for (int x : intensity) { cout << x << " "; } }
replace
11
12
11
12
TLE
p02647
C++
Runtime Error
#include <algorithm> #include <cmath> #include <cstring> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #define bug(x) cout << "zdongdebug1: " << x << endl; #define bug2(x, y) cout << "zdongdebug2: " << x << " " << y << endl; #define bug3(x, y, z) \ cout << "zdongdebug3: " << x << " " << y << " " << z << endl; using namespace std; typedef long long ll; const int maxn = 100005; const int mod = 1000000007; int n, m; int a[maxn], b[maxn]; bool f() { bool sign = 0; for (int i = 1; i <= n; i++) { b[i] = 0; if (a[i] < n) sign = 1; } if (!sign) { return true; } for (int i = 1; i <= n; i++) { int l = i - a[i]; int r = i + a[i] + 1; l = max(l, 1); r = min(r, n + 1); b[l]++; b[r]--; a[i] = 0; } for (int i = 1; i <= n; i++) { a[i] = a[i - 1] + b[i]; } return false; } int main() { #ifdef suiyuan2009 freopen("/Users/suiyuan2009/CLionProjects/icpc/input.txt", "r", stdin); // freopen("/Users/suiyuan2009/CLionProjects/icpc/output.txt", "w", stdout); #endif cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= m; i++) { if (f()) { break; } } for (int i = 1; i < n; i++) cout << a[i] << " "; cout << a[n] << endl; return 0; }
#include <algorithm> #include <cmath> #include <cstring> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #define bug(x) cout << "zdongdebug1: " << x << endl; #define bug2(x, y) cout << "zdongdebug2: " << x << " " << y << endl; #define bug3(x, y, z) \ cout << "zdongdebug3: " << x << " " << y << " " << z << endl; using namespace std; typedef long long ll; const int maxn = 200005; const int mod = 1000000007; int n, m; int a[maxn], b[maxn]; bool f() { bool sign = 0; for (int i = 1; i <= n; i++) { b[i] = 0; if (a[i] < n) sign = 1; } if (!sign) { return true; } for (int i = 1; i <= n; i++) { int l = i - a[i]; int r = i + a[i] + 1; l = max(l, 1); r = min(r, n + 1); b[l]++; b[r]--; a[i] = 0; } for (int i = 1; i <= n; i++) { a[i] = a[i - 1] + b[i]; } return false; } int main() { #ifdef suiyuan2009 freopen("/Users/suiyuan2009/CLionProjects/icpc/input.txt", "r", stdin); // freopen("/Users/suiyuan2009/CLionProjects/icpc/output.txt", "w", stdout); #endif cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= m; i++) { if (f()) { break; } } for (int i = 1; i < n; i++) cout << a[i] << " "; cout << a[n] << endl; return 0; }
replace
20
21
20
21
0
p02647
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define int long long #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define reps(i, n) for (int i = 1; i <= (int)(n); i++) #define all(x) (x).begin(), (x).end() #define uniq(x) (x).erase(unique(all(x)), (x).end())) #define bit(n) (1LL << (n)) #define cdiv(a, b) (((a)-1) / (b) + 1) #define dump(x) cerr << #x " = " << (x) << endl using vint = vector<int>; using vvint = vector<vint>; using pint = pair<int, int>; using vpint = vector<pint>; template <typename T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>; constexpr double PI = 3.1415926535897932384626433832795028; constexpr int DY[8] = {0, 1, 0, -1, 1, 1, -1, -1}; constexpr int DX[8] = {1, 0, -1, 0, 1, -1, -1, 1}; int gcd(int a, int b) { while (b) { swap(a %= b, b); } return a; } int lcm(int a, int b) { return a / gcd(a, b) * b; } template <typename T> void fin(T mes) { cout << mes << endl; exit(0); } template <typename T, typename U> bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; } template <typename T, typename U> bool chmin(T &a, const U &b) { if (b < a) { a = b; return true; } return false; } template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &rhs) { os << "(" << rhs.first << ", " << rhs.second << ")"; return os; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &rhs) { os << "{"; for (auto itr = rhs.begin(); itr != rhs.end(); itr++) { os << *itr << (next(itr) != rhs.end() ? ", " : ""); } os << "}"; return os; } struct setup { static constexpr int PREC = 20; setup() { cout << fixed << setprecision(PREC); cerr << fixed << setprecision(PREC); }; } setup; template <typename M> struct fenwick_tree { using T = typename M::T; int n; std::vector<T> data; fenwick_tree(int n) : n(n), data(n + 1, M::id()) {} void add(int i, const T &x) { assert(0 <= i && i < n); for (i++; i <= n; i += i & -i) { data[i] = M::op(data[i], x); } } T get_sum(int i) const { assert(0 < i && i <= n); T ret = M::id(); for (; i > 0; i -= i & -i) { ret = M::op(ret, data[i]); } return ret; } T get_sum(int l, int r) const { assert(0 <= l && l < n); assert(0 < r && r <= n); return M::op(get_sum(r), M::inv(get_sum(l))); } }; template <typename M> struct fenwick_tree_range { using T = typename M::T; fenwick_tree<M> ft; fenwick_tree_range(int n) : ft(n + 1) {} void add(int l, int r, const T &x) { ft.add(l, x), ft.add(r, -x); } T operator[](int i) const { return ft.get_sum(i + 1); } }; struct rsq { using T = int; static T id() { return 0; } static T op(const T &a, const T &b) { return a + b; } static T inv(const T &a) { return -a; } }; signed main() { int N, K; cin >> N >> K; vint A(N); rep(i, N) { cin >> A[i]; } rep(i, min(1000LL, K)) { fenwick_tree_range<rsq> B(N); rep(j, N) { B.add(max(0LL, j - A[j]), min(N, j + A[j] + 1), 1); } rep(j, N) { A[j] = B[j]; } } rep(i, N) { cout << A[i] << endl; } }
#include <bits/stdc++.h> using namespace std; #define int long long #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define reps(i, n) for (int i = 1; i <= (int)(n); i++) #define all(x) (x).begin(), (x).end() #define uniq(x) (x).erase(unique(all(x)), (x).end())) #define bit(n) (1LL << (n)) #define cdiv(a, b) (((a)-1) / (b) + 1) #define dump(x) cerr << #x " = " << (x) << endl using vint = vector<int>; using vvint = vector<vint>; using pint = pair<int, int>; using vpint = vector<pint>; template <typename T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>; constexpr double PI = 3.1415926535897932384626433832795028; constexpr int DY[8] = {0, 1, 0, -1, 1, 1, -1, -1}; constexpr int DX[8] = {1, 0, -1, 0, 1, -1, -1, 1}; int gcd(int a, int b) { while (b) { swap(a %= b, b); } return a; } int lcm(int a, int b) { return a / gcd(a, b) * b; } template <typename T> void fin(T mes) { cout << mes << endl; exit(0); } template <typename T, typename U> bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; } template <typename T, typename U> bool chmin(T &a, const U &b) { if (b < a) { a = b; return true; } return false; } template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &rhs) { os << "(" << rhs.first << ", " << rhs.second << ")"; return os; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &rhs) { os << "{"; for (auto itr = rhs.begin(); itr != rhs.end(); itr++) { os << *itr << (next(itr) != rhs.end() ? ", " : ""); } os << "}"; return os; } struct setup { static constexpr int PREC = 20; setup() { cout << fixed << setprecision(PREC); cerr << fixed << setprecision(PREC); }; } setup; template <typename M> struct fenwick_tree { using T = typename M::T; int n; std::vector<T> data; fenwick_tree(int n) : n(n), data(n + 1, M::id()) {} void add(int i, const T &x) { assert(0 <= i && i < n); for (i++; i <= n; i += i & -i) { data[i] = M::op(data[i], x); } } T get_sum(int i) const { assert(0 < i && i <= n); T ret = M::id(); for (; i > 0; i -= i & -i) { ret = M::op(ret, data[i]); } return ret; } T get_sum(int l, int r) const { assert(0 <= l && l < n); assert(0 < r && r <= n); return M::op(get_sum(r), M::inv(get_sum(l))); } }; template <typename M> struct fenwick_tree_range { using T = typename M::T; fenwick_tree<M> ft; fenwick_tree_range(int n) : ft(n + 1) {} void add(int l, int r, const T &x) { ft.add(l, x), ft.add(r, -x); } T operator[](int i) const { return ft.get_sum(i + 1); } }; struct rsq { using T = int; static T id() { return 0; } static T op(const T &a, const T &b) { return a + b; } static T inv(const T &a) { return -a; } }; signed main() { int N, K; cin >> N >> K; vint A(N); rep(i, N) { cin >> A[i]; } rep(i, min(100LL, K)) { fenwick_tree_range<rsq> B(N); rep(j, N) { B.add(max(0LL, j - A[j]), min(N, j + A[j] + 1), 1); } rep(j, N) { A[j] = B[j]; } } rep(i, N) { cout << A[i] << endl; } }
replace
112
113
112
113
TLE
p02647
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) #define repI(i, d, n) for (int i = (d); i < (n); ++i) #define reps1(i, n) for (int i = 1; i < (n); ++i) using namespace std; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } using ll = long long; using P = pair<int, int>; using Pl = pair<ll, ll>; using M = map<int, int>; using Ml = map<ll, ll>; ll const INF = 1ll << 61; double pi = 3.141592653589793238; int main() { ll n, k; cin >> n >> k; vector<ll> a(n); rep(i, n) cin >> a[i]; vector<ll> table(n + 1, 0); rep(j, k) { rep(i, n + 1) table[i] = 0; rep(i, n) { table[max(0ll, i - a[i])]++; table[min(n, i + a[i] + 1)]--; } rep(i, n) { if (0 < i) table[i] += table[i - 1]; } rep(i, n) a[i] = table[i]; } rep(i, n) cout << a[i] << " "; cout << endl; return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) #define repI(i, d, n) for (int i = (d); i < (n); ++i) #define reps1(i, n) for (int i = 1; i < (n); ++i) using namespace std; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } using ll = long long; using P = pair<int, int>; using Pl = pair<ll, ll>; using M = map<int, int>; using Ml = map<ll, ll>; ll const INF = 1ll << 61; double pi = 3.141592653589793238; int main() { ll n, k; cin >> n >> k; vector<ll> a(n); rep(i, n) cin >> a[i]; vector<ll> table(n + 1, 0); rep(j, k) { rep(i, n + 1) table[i] = 0; rep(i, n) { table[max(0ll, i - a[i])]++; table[min(n, i + a[i] + 1)]--; } rep(i, n) { if (0 < i) table[i] += table[i - 1]; } rep(i, n) a[i] = table[i]; bool flag = true; rep(i, n) { if (a[i] != n) { flag = false; break; } } if (flag) break; } rep(i, n) cout << a[i] << " "; cout << endl; return 0; }
insert
49
49
49
59
TLE
p02647
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { int N, K; cin >> N >> K; if (log10(N) * 8 - 2 <= K) { for (int i = 0; i < N; i++) { cout << N; if (i == N - 1) cout << endl; else cout << ' '; } return 0; } vector<vector<int>> A(K + 1, vector<int>(N, 0)); for (int &i : A[0]) cin >> i; for (int i = 0; i < K; i++) { for (int k = 0; k < N; k++) { int l = min(N - 1, k + A[i][k]); for (int j = max(0, k - A[i][k]); j <= l; j++) { A[i + 1][j]++; } } } for (int i = 0; i < N; i++) { cout << A[K][i]; if (i == N - 1) cout << endl; else cout << ' '; } }
#include <bits/stdc++.h> using namespace std; int main() { int N, K; cin >> N >> K; if (log10(N) * 8 - 2 <= K) { for (int i = 0; i < N; i++) { cout << N; if (i == N - 1) cout << endl; else cout << ' '; } return 0; } vector<vector<int>> A(K + 1, vector<int>(N, 0)); for (int &i : A[0]) cin >> i; for (int i = 0; i < K; i++) { for (int j = 0; j < N; j++) { A[i + 1][max(0, j - A[i][j])]++; A[i + 1][min(N - 1, j + A[i][j]) + 1]--; } for (int j = 1; j < N; j++) { A[i + 1][j] += A[i + 1][j - 1]; } } for (int i = 0; i < N; i++) { cout << A[K][i]; if (i == N - 1) cout << endl; else cout << ' '; } }
replace
20
25
20
26
TLE
p02647
C++
Time Limit Exceeded
#define _CRT_SECURE_NO_WARNINGS #include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cstring> #include <float.h> #include <fstream> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <string> #include <time.h> #include <utility> #include <vector> using namespace std; long long gcd(long long a, long long b) { if (b == 0) return a; else return gcd(b, a % b); } template <typename Monoid, typename OperatorMonoid = Monoid> struct LazySegmentTree { using F = function<Monoid(Monoid, Monoid)>; using G = function<Monoid(Monoid, OperatorMonoid)>; using H = function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>; using P = function<OperatorMonoid(OperatorMonoid, int)>; int sz; vector<Monoid> data; vector<OperatorMonoid> lazy; const F f; const G g; const H h; const P p; const Monoid M1; const OperatorMonoid OM0; LazySegmentTree(int n, const F f, const G g, const H h, const P p, const Monoid &M1, const OperatorMonoid OM0) : f(f), g(g), h(h), p(p), M1(M1), OM0(OM0) { sz = 1; while (sz < n) sz <<= 1; data.assign(2 * sz, M1); lazy.assign(2 * sz, OM0); } void set(int k, const Monoid &x) { data[k + sz] = x; } void build() { for (int k = sz - 1; k > 0; k--) { data[k] = f(data[2 * k + 0], data[2 * k + 1]); } } void propagate(int k, int len) { if (lazy[k] != OM0) { if (k < sz) { lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]); lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]); } data[k] = g(data[k], p(lazy[k], len)); lazy[k] = OM0; } } Monoid update(int a, int b, const OperatorMonoid &x, int k, int l, int r) { propagate(k, r - l); if (r <= a || b <= l) { return data[k]; } else if (a <= l && r <= b) { lazy[k] = h(lazy[k], x); propagate(k, r - l); return data[k]; } else { return data[k] = f(update(a, b, x, 2 * k + 0, l, (l + r) >> 1), update(a, b, x, 2 * k + 1, (l + r) >> 1, r)); } } Monoid update(int a, int b, const OperatorMonoid &x) { return update(a, b, x, 1, 0, sz); } Monoid query(int a, int b, int k, int l, int r) { propagate(k, r - l); if (r <= a || b <= l) { return M1; } else if (a <= l && r <= b) { return data[k]; } else { return f(query(a, b, 2 * k + 0, l, (l + r) >> 1), query(a, b, 2 * k + 1, (l + r) >> 1, r)); } } Monoid query(int a, int b) { return query(a, b, 1, 0, sz); } Monoid operator[](const int &k) { return query(k, k + 1); } }; map<int64_t, int> prime_factor(int64_t n) { map<int64_t, int> ret; for (int64_t i = 2; i * i <= n; i++) { while (n % i == 0) { ret[i]++; n /= i; } } if (n != 1) ret[n] = 1; return ret; } long long MOD = 998244353; const int MAX = 510000; long long fac[MAX], finv[MAX], inv[MAX]; // テーブルを作る前処理 void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 long long COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } template <typename T> struct BinaryIndexedTree { vector<T> data; BinaryIndexedTree(int sz) { data.assign(++sz, 0); } T sum(int k) { T ret = 0; for (++k; k > 0; k -= k & -k) ret += data[k]; return (ret); } void add(int k, T x) { for (++k; k < data.size(); k += k & -k) data[k] += x; } }; typedef pair<int, int> p; typedef pair<int, p> pai; signed main() { cin.tie(0); ios::sync_with_stdio(false); int n, k; cin >> n >> k; int a[200004] = {0}; int memo[200004] = {0}; if (k >= 50) { for (int i = 1; i <= n; i++) { cout << n << " "; } } for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int t = 0; t < k; t++) { for (int i = 1; i <= n; i++) { memo[i] = 0; } for (int i = 1; i <= n; i++) { memo[max(i - a[i], 1)] += 1; memo[min(i + a[i] + 1, n + 1)] -= 1; } int now = 0; for (int i = 1; i <= n; i++) { now += memo[i]; a[i] = now; } } for (int i = 1; i <= n; i++) { cout << a[i] << " "; } }
#define _CRT_SECURE_NO_WARNINGS #include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cstring> #include <float.h> #include <fstream> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <string> #include <time.h> #include <utility> #include <vector> using namespace std; long long gcd(long long a, long long b) { if (b == 0) return a; else return gcd(b, a % b); } template <typename Monoid, typename OperatorMonoid = Monoid> struct LazySegmentTree { using F = function<Monoid(Monoid, Monoid)>; using G = function<Monoid(Monoid, OperatorMonoid)>; using H = function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>; using P = function<OperatorMonoid(OperatorMonoid, int)>; int sz; vector<Monoid> data; vector<OperatorMonoid> lazy; const F f; const G g; const H h; const P p; const Monoid M1; const OperatorMonoid OM0; LazySegmentTree(int n, const F f, const G g, const H h, const P p, const Monoid &M1, const OperatorMonoid OM0) : f(f), g(g), h(h), p(p), M1(M1), OM0(OM0) { sz = 1; while (sz < n) sz <<= 1; data.assign(2 * sz, M1); lazy.assign(2 * sz, OM0); } void set(int k, const Monoid &x) { data[k + sz] = x; } void build() { for (int k = sz - 1; k > 0; k--) { data[k] = f(data[2 * k + 0], data[2 * k + 1]); } } void propagate(int k, int len) { if (lazy[k] != OM0) { if (k < sz) { lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]); lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]); } data[k] = g(data[k], p(lazy[k], len)); lazy[k] = OM0; } } Monoid update(int a, int b, const OperatorMonoid &x, int k, int l, int r) { propagate(k, r - l); if (r <= a || b <= l) { return data[k]; } else if (a <= l && r <= b) { lazy[k] = h(lazy[k], x); propagate(k, r - l); return data[k]; } else { return data[k] = f(update(a, b, x, 2 * k + 0, l, (l + r) >> 1), update(a, b, x, 2 * k + 1, (l + r) >> 1, r)); } } Monoid update(int a, int b, const OperatorMonoid &x) { return update(a, b, x, 1, 0, sz); } Monoid query(int a, int b, int k, int l, int r) { propagate(k, r - l); if (r <= a || b <= l) { return M1; } else if (a <= l && r <= b) { return data[k]; } else { return f(query(a, b, 2 * k + 0, l, (l + r) >> 1), query(a, b, 2 * k + 1, (l + r) >> 1, r)); } } Monoid query(int a, int b) { return query(a, b, 1, 0, sz); } Monoid operator[](const int &k) { return query(k, k + 1); } }; map<int64_t, int> prime_factor(int64_t n) { map<int64_t, int> ret; for (int64_t i = 2; i * i <= n; i++) { while (n % i == 0) { ret[i]++; n /= i; } } if (n != 1) ret[n] = 1; return ret; } long long MOD = 998244353; const int MAX = 510000; long long fac[MAX], finv[MAX], inv[MAX]; // テーブルを作る前処理 void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 long long COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } template <typename T> struct BinaryIndexedTree { vector<T> data; BinaryIndexedTree(int sz) { data.assign(++sz, 0); } T sum(int k) { T ret = 0; for (++k; k > 0; k -= k & -k) ret += data[k]; return (ret); } void add(int k, T x) { for (++k; k < data.size(); k += k & -k) data[k] += x; } }; typedef pair<int, int> p; typedef pair<int, p> pai; signed main() { cin.tie(0); ios::sync_with_stdio(false); int n, k; cin >> n >> k; int a[200004] = {0}; int memo[200004] = {0}; if (k >= 50) { for (int i = 1; i <= n; i++) { cout << n << " "; } return 0; } for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int t = 0; t < k; t++) { for (int i = 1; i <= n; i++) { memo[i] = 0; } for (int i = 1; i <= n; i++) { memo[max(i - a[i], 1)] += 1; memo[min(i + a[i] + 1, n + 1)] -= 1; } int now = 0; for (int i = 1; i <= n; i++) { now += memo[i]; a[i] = now; } } for (int i = 1; i <= n; i++) { cout << a[i] << " "; } }
insert
178
178
178
179
TLE
p02647
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) #define all(a) (a).begin(), (a).end() using namespace std; using ll = long long int; using vec = vector<int>; using P = pair<int, int>; const int INF = 1e9 + 7; int main() { int n, k; cin >> n >> k; vec a(n); rep(i, n) { cin >> a[i]; } rep(ki, k) { vec b(n); rep(i, n) { int l = max(0, i - a[i]); int r = min(n, i + a[i] + 1); b[l]++; if (r < n) b[r]--; } bool f = true; rep(i, n) { b[i + 1] += b[i]; } if (a == b) break; a = b; } rep(i, n) { cout << a[i] << endl; } }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); ++i) #define all(a) (a).begin(), (a).end() using namespace std; using ll = long long int; using vec = vector<int>; using P = pair<int, int>; const int INF = 1e9 + 7; int main() { int n, k; cin >> n >> k; vec a(n); rep(i, n) { cin >> a[i]; } rep(ki, k) { vec b(n); rep(i, n) { int l = max(0, i - a[i]); int r = min(n, i + a[i] + 1); b[l]++; if (r < n) b[r]--; } bool f = true; rep(i, n - 1) { b[i + 1] += b[i]; } if (a == b) break; a = b; } rep(i, n) { cout << a[i] << endl; } }
replace
25
26
25
26
0
p02647
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <cstdlib> #include <iomanip> #include <iostream> #include <iterator> #include <map> #include <math.h> #include <numeric> #include <set> #include <string> #include <utility> #include <vector> using namespace std; typedef long long ll; int main() { ll N, K; cin >> N >> K; ll A[N]; ll area[N + 1]; // N + 1は無視 ll areaSum; for (ll i = 0; i < N; i++) { cin >> A[i]; } for (ll rep = 0; rep < K; rep++) { for (ll i = 0; i < N; i++) { area[i] = 0; } for (ll i = 0; i < N; i++) { area[max(i - A[i], (ll)0)]++; area[min(i + A[i] + 1, N)]--; } areaSum = 0; for (ll i = 0; i < N; i++) { areaSum += area[i]; A[i] = areaSum; } } for (ll i = 0; i < N; i++) { if (i != 0) { cout << " "; } cout << A[i]; } cout << endl; return 0; }
#include <algorithm> #include <cmath> #include <cstdlib> #include <iomanip> #include <iostream> #include <iterator> #include <map> #include <math.h> #include <numeric> #include <set> #include <string> #include <utility> #include <vector> using namespace std; typedef long long ll; int main() { ll N, K; cin >> N >> K; ll A[N]; ll area[N + 1]; // N + 1は無視 ll areaSum; for (ll i = 0; i < N; i++) { cin >> A[i]; } ll tryMin = 1000; ll tryMinN = 1; while (tryMinN <= N) { tryMinN *= 2; tryMin++; } tryMin = min(tryMin, K); for (ll rep = 0; rep < tryMin; rep++) { for (ll i = 0; i < N; i++) { area[i] = 0; } for (ll i = 0; i < N; i++) { area[max(i - A[i], (ll)0)]++; area[min(i + A[i] + 1, N)]--; } areaSum = 0; for (ll i = 0; i < N; i++) { areaSum += area[i]; A[i] = areaSum; } } for (ll i = 0; i < N; i++) { if (i != 0) { cout << " "; } cout << A[i]; } cout << endl; return 0; }
replace
27
28
27
35
TLE
p02647
C++
Time Limit Exceeded
#define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; typedef long long int lli; typedef unsigned long long int ulli; #define vec(s) vector<s>; #define vvec(s) vector<vector<s>>; typedef vector<lli> vi; typedef vector<vi> vvi; typedef pair<lli, lli> pii; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define YN(x) cout << (bool x ? "Yes" : "No") << endl; #define out(s) cout << s << endl; #define pb(s) push_back(s); #define sp " "; #define INF 10000000000 #define all(s) s.begin(), s.end() void vout(vi v) { for (int i = 0; i < v.size(); i++) cout << v.at(i) << endl; } int main() { lli n, k; cin >> n >> k; vi ans(n + 2, 0), cnt(n + 2, 0), meme(n + 2, 0); rep(i, n) cin >> ans[i + 1]; rep(i, k) { swap(ans, cnt); ans = meme; for (int j = 1; j <= n; j++) { (j - cnt[j] > 0 ? ans[j - cnt[j]] += 1 : ans[0] += 1); (j + cnt[j] + 1 < n + 1 ? ans[j + cnt[j] + 1] -= 1 : ans[n + 1] -= 1); } for (int j = 1; j <= n; j++) { ans[j] = ans[j] + ans[j - 1]; } } for (int i = 1; i <= n; i++) { cout << ans[i]; if (i != n) cout << " "; else cout << endl; } }
#define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; typedef long long int lli; typedef unsigned long long int ulli; #define vec(s) vector<s>; #define vvec(s) vector<vector<s>>; typedef vector<lli> vi; typedef vector<vi> vvi; typedef pair<lli, lli> pii; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define YN(x) cout << (bool x ? "Yes" : "No") << endl; #define out(s) cout << s << endl; #define pb(s) push_back(s); #define sp " "; #define INF 10000000000 #define all(s) s.begin(), s.end() void vout(vi v) { for (int i = 0; i < v.size(); i++) cout << v.at(i) << endl; } int main() { lli n, k; cin >> n >> k; vi ans(n + 2, 0), cnt(n + 2, 0), meme(n + 2, 0); rep(i, n) cin >> ans[i + 1]; rep(i, min(k, (lli)41)) { swap(ans, cnt); ans = meme; for (int j = 1; j <= n; j++) { (j - cnt[j] > 0 ? ans[j - cnt[j]] += 1 : ans[0] += 1); (j + cnt[j] + 1 < n + 1 ? ans[j + cnt[j] + 1] -= 1 : ans[n + 1] -= 1); } for (int j = 1; j <= n; j++) { ans[j] = ans[j] + ans[j - 1]; } } for (int i = 1; i <= n; i++) { cout << ans[i]; if (i != n) cout << " "; else cout << endl; } }
replace
27
28
27
28
TLE
p02647
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #ifdef LOCAL_DEBUG #include "LOCAL_DEBUG.hpp" #endif #define int long long const int INF = 1LL << 60; template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); } template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) { return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...)); } template <class T, class V> typename enable_if<is_class<T>::value == 0>::type fill(T &t, const V &v) { t = v; } template <class T, class V> typename enable_if<is_class<T>::value != 0>::type fill(T &t, const V &v) { for (auto &e : t) fill(e, v); } // auto v = make_vec<int>(h, w); // fill(v, 0); signed main() { int n, k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } auto dp = make_vec<int>(111, n + 1); dp[0] = a; for (int i = 0; i < 100; i++) { for (int j = 0; j < n; j++) { dp[i + 1][max(0LL, j - dp[i][j])]++; dp[i + 1][min(n, j + dp[i][j] + 1)]--; } for (int j = 0; j < n - 1; j++) { dp[i + 1][j + 1] += dp[i + 1][j]; } } for (int i = 0; i < n; i++) { cout << dp[k][i] << " "; } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #ifdef LOCAL_DEBUG #include "LOCAL_DEBUG.hpp" #endif #define int long long const int INF = 1LL << 60; template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); } template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) { return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...)); } template <class T, class V> typename enable_if<is_class<T>::value == 0>::type fill(T &t, const V &v) { t = v; } template <class T, class V> typename enable_if<is_class<T>::value != 0>::type fill(T &t, const V &v) { for (auto &e : t) fill(e, v); } // auto v = make_vec<int>(h, w); // fill(v, 0); signed main() { int n, k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } auto dp = make_vec<int>(111, n + 1); dp[0] = a; for (int i = 0; i < 100; i++) { for (int j = 0; j < n; j++) { dp[i + 1][max(0LL, j - dp[i][j])]++; dp[i + 1][min(n, j + dp[i][j] + 1)]--; } for (int j = 0; j < n - 1; j++) { dp[i + 1][j + 1] += dp[i + 1][j]; } } k = min(100LL, k); for (int i = 0; i < n; i++) { cout << dp[k][i] << " "; } cout << endl; return 0; }
insert
45
45
45
46
0
p02647
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 optimize("Ofast,no-stack-protector,unroll-loops,fast-math") using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; const int N = 200000 + 10; const ll MOD = 1000000000 + 7; const ll INF = 1000000010; const ll LOG = 25; int k, n, a[N], ps[N]; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= min(100, k); i++) { memset(ps, 0, sizeof ps); for (int j = 1; j <= n; j++) { // cout << j - a[i] << '\n'; ps[j - a[j]]++; ps[j + a[j] + 1]--; } for (int j = 1; j <= n; j++) ps[j] += ps[j - 1]; for (int j = 1; j <= n; j++) a[j] = ps[j]; } for (int i = 1; i <= n; i++) cout << a[i] << ' '; 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 optimize("Ofast,no-stack-protector,unroll-loops,fast-math") using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; const int N = 200000 + 10; const ll MOD = 1000000000 + 7; const ll INF = 1000000010; const ll LOG = 25; int k, n, a[N], ps[N]; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= min(100, k); i++) { memset(ps, 0, sizeof ps); for (int j = 1; j <= n; j++) { // cout << j - a[i] << '\n'; ps[max(0, j - a[j])]++; ps[min(n + 1, j + a[j] + 1)]--; } for (int j = 1; j <= n; j++) ps[j] += ps[j - 1]; for (int j = 1; j <= n; j++) a[j] = ps[j]; } for (int i = 1; i <= n; i++) cout << a[i] << ' '; return 0; }
replace
33
35
33
35
0
p02647
C++
Runtime Error
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; using ll = long long; using P = pair<int, int>; int main() { int n, k; cin >> n >> k; vector<int> a(n); rep(i, n) cin >> a[i]; rep(ki, k) { vector<int> b(n + 1); rep(i, n) { int l = max(0, i - a[i]); int r = min(i + a[i] + 1, n + 1); b[l]++; b[r]--; } rep(i, n) b[i + 1] += b[i]; b.pop_back(); if (a == b) break; a = b; } rep(i, n) { cout << a[i] << endl; } }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; using ll = long long; using P = pair<int, int>; int main() { int n, k; cin >> n >> k; vector<int> a(n); rep(i, n) cin >> a[i]; rep(ki, k) { vector<int> b(n + 1); rep(i, n) { int l = max(0, i - a[i]); int r = min(i + a[i] + 1, n); b[l]++; b[r]--; } rep(i, n) b[i + 1] += b[i]; b.pop_back(); if (a == b) break; a = b; } rep(i, n) { cout << a[i] << endl; } }
replace
16
17
16
17
0
p02647
C++
Runtime Error
#pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> typedef long long ll; typedef long double ld; using namespace std; #define F first #define S second #define pb push_back #define all(x) (x).begin(), (x).end() #define SZ(x) (int)(x).size() // #define int ll const int N = 1e5 + 10; int a[N], b[N]; void add(int l, int r) { b[l]++; b[r + 1]--; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, k; cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> b[i]; } for (int iter = 1; iter <= k; iter++) { for (int i = 1; i <= n; i++) { a[i] = b[i]; b[i] = 0; } for (int i = 1; i <= n; i++) { add(max(1, i - a[i]), min(n, i + a[i])); } bool ok = true; for (int i = 1; i <= n; i++) { b[i] += b[i - 1]; if (b[i] != n) { ok = false; } } if (ok) { break; } } for (int i = 1; i <= n; i++) { cout << b[i] << ' '; } cout << '\n'; }
#pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> typedef long long ll; typedef long double ld; using namespace std; #define F first #define S second #define pb push_back #define all(x) (x).begin(), (x).end() #define SZ(x) (int)(x).size() // #define int ll const int N = 2e5 + 10; int a[N], b[N]; void add(int l, int r) { b[l]++; b[r + 1]--; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, k; cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> b[i]; } for (int iter = 1; iter <= k; iter++) { for (int i = 1; i <= n; i++) { a[i] = b[i]; b[i] = 0; } for (int i = 1; i <= n; i++) { add(max(1, i - a[i]), min(n, i + a[i])); } bool ok = true; for (int i = 1; i <= n; i++) { b[i] += b[i - 1]; if (b[i] != n) { ok = false; } } if (ok) { break; } } for (int i = 1; i <= n; i++) { cout << b[i] << ' '; } cout << '\n'; }
replace
13
14
13
14
0
p02647
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<ll> vll; typedef vector<vector<ll>> vvll; typedef vector<vector<vector<ll>>> vvvll; typedef vector<bool> vb; typedef vector<vector<bool>> vvb; typedef vector<vector<vector<bool>>> vvvb; typedef pair<ll, ll> pll; typedef vector<pll> vpll; typedef vector<vpll> vvpll; typedef vector<double> vd; typedef vector<vd> vdd; #define FOR(i, x, y) for (ll i = (ll)x; i < (ll)y; ++i) #define REP(i, y) FOR(i, 0, y) #define RFOR(i, x, y) for (ll i = (ll)x; i >= (ll)y; --i) #define RREP(i, x) RFOR(i, x, 0) #define ALL(a) a.begin(), a.end() #define pb push_back #define debug_print(x...) \ cerr << "line " << __LINE__ << " : "; \ debug_print_in(x); template <typename First> void debug_print_in(First first) { cerr << first << endl; return; } template <typename First, typename... Rest> void debug_print_in(First first, Rest... rest) { cerr << first << " "; debug_print_in(rest...); return; } void IN(void) { return; } template <typename First, typename... Rest> void IN(First &first, Rest &...rest) { cin >> first; IN(rest...); return; } template <typename First> void OUT(First first) { cout << first << endl; return; } template <typename First, typename... Rest> void OUT(First first, Rest... rest) { cout << first << " "; OUT(rest...); return; } template <class t, class u> void chmax(t &a, u b) { if (a < b) a = b; }; template <class t, class u> void chmin(t &a, u b) { if (a > b) a = b; }; int popcount(int t) { return __builtin_popcount(t); } // GCC int popcount(ll t) { return __builtin_popcountll(t); } // GCC template <typename T> void vec_print(vector<T> VEC) { REP(i, VEC.size()) { cerr << VEC[i] << " "; } cerr << endl; }; template <typename T> void mat_print(vector<vector<T>> MAT){ REP(i, MAT.size()){REP(j, MAT[i].size()){cerr << MAT[i][j] << " "; } cerr << endl; } } ; constexpr int INF = (1 << 30); constexpr ll INFLL = 1LL << 62; constexpr long double EPS = 1e-12; constexpr ll MOD = (ll)((1E+9) + 7); template <typename DATA> class Segment_Tree { private: ll n; ll n_; using F = function<DATA(DATA, DATA)>; const F f; // query function const DATA init; public: vector<DATA> dat; vector<DATA> lazy1; // 遅延評価 - http://tsutaj.hatenablog.com/entry/2017/03/30/224339 vector<DATA> lazy2; // 遅延評価 - http://tsutaj.hatenablog.com/entry/2017/03/30/224339 vector<bool> lazy1_flag; vector<bool> lazy2_flag; Segment_Tree(ll n__, const F &f, DATA init) : f(f), init(init) { // For simply, the number of component is set to be power of 2 n = 1; n_ = n__; while (n < n_) n *= 2; dat.resize(2 * n - 1, init); lazy1.resize(2 * n - 1, init); lazy2.resize(2 * n - 1, init); lazy1_flag.resize(2 * n - 1, false); lazy2_flag.resize(2 * n - 1, false); return; }; ~Segment_Tree(void) { return; }; void zero(void) { for (ll i = 0; i < 2 * n - 1; ++i) { dat[i] = init; lazy1[i] = init; lazy2[i] = init; lazy1_flag[i] = false; lazy2_flag[i] = false; } return; } void eval1(ll k, ll l, ll r) { if (!lazy1_flag[k]) return; if (l >= r) return; lazy1_flag[k] = false; dat[k] = lazy1[k]; if (r - l > 1) { lazy1[2 * k + 1] = lazy1[k]; lazy1[2 * k + 2] = lazy1[k]; lazy1_flag[2 * k + 1] = true; lazy1_flag[2 * k + 2] = true; } lazy1[k] = init; } DATA eval2_2(DATA tmp, ll n) { DATA ans = tmp; n--; while (n > 0) { if (n & 1) ans = f(ans, tmp); tmp = f(tmp, tmp); n >>= 1; } return ans; } void eval2(ll k, ll l, ll r) { if (!lazy2_flag[k]) return; if (l >= r) return; lazy2_flag[k] = false; dat[k] = f(dat[k], eval2_2(lazy2[k], r - l)); if (r - l > 1) { lazy2[2 * k + 1] = f(lazy2[2 * k + 1], lazy2[k]); lazy2[2 * k + 2] = f(lazy2[2 * k + 2], lazy2[k]); lazy2_flag[2 * k + 1] = true; lazy2_flag[2 * k + 2] = true; } lazy2[k] = init; } // k-th value is changed to a void update1(ll k, DATA a) { k += n - 1; dat[k] = a; while (k > 0) { k = (k - 1) / 2; dat[k] = f(dat[k * 2 + 1], dat[k * 2 + 2]); } return; }; // k-th value is operated of a void update2(ll k, DATA a) { k += n - 1; dat[k] = f(dat[k], a); while (k > 0) { k = (k - 1) / 2; dat[k] = f(dat[k * 2 + 1], dat[k * 2 + 2]); } return; }; //[a,b)の範囲の値をxに変える void rangeupdate1(ll a, ll b, DATA x, ll k, ll l, ll r) { eval1(k, l, r); if (r <= a || b <= l) return; if (a <= l && r <= b) { lazy1[k] = x; lazy1_flag[k] = true; // eval1(k,l,r); } else { rangeupdate1(a, b, x, 2 * k + 1, l, (l + r) / 2); rangeupdate1(a, b, x, 2 * k + 2, (l + r) / 2, r); dat[k] = f(dat[2 * k + 1], dat[2 * k + 2]); } return; } void rangeupdate1(ll a, ll b, DATA x) { rangeupdate1(a, b, x, 0, 0, n); } //[a,b)の範囲に対してxを作用させる void rangeupdate2(ll a, ll b, DATA x, ll k, ll l, ll r) { eval2(k, l, r); if (r <= a || b <= l) return; if (a <= l && r <= b) { lazy2[k] = f(lazy2[k], x); lazy2_flag[k] = true; eval2(k, l, r); } else { rangeupdate2(a, b, x, 2 * k + 1, l, (l + r) / 2); rangeupdate2(a, b, x, 2 * k + 2, (l + r) / 2, r); dat[k] = f(dat[2 * k + 1], dat[2 * k + 2]); } return; } void rangeupdate2(ll a, ll b, DATA x) { rangeupdate2(a, b, x, 0, 0, n); } // search the minimum of [a, b) // Other values are used for simply // From outside, call query(a, b, 0, 0, n) // k is the node index, // temporary block is [l, r) DATA query(ll a, ll b, ll k, ll l, ll r) { // if there are no overlap between [a, b) and [l, r) if (r <= a || b <= l) return init; eval1(k, l, r); eval2(k, l, r); // if [a, b) contains [l, r), return the value of this node if (a <= l && r <= b) return dat[k]; DATA vl = query(a, b, k * 2 + 1, l, (l + r) / 2); DATA vr = query(a, b, k * 2 + 2, (l + r) / 2, r); return f(vl, vr); }; // search the minimum of [a, b) DATA query(ll a, ll b) { if (a < 0 || n_ < b || a == b) { fprintf(stderr, "Segment_Tree Search Space Error\n"); fprintf(stderr, "Desired:\n"); fprintf(stderr, "%lld <= %lld < %lld <= %lld\n", 0, a, b, n_); exit(0); } return query(a, b, 0, 0, n); }; }; int main() { cin.tie(0); // cut the cin and cout (default, std::flush is performed after // std::cin) ios::sync_with_stdio( false); // cut the iostream and stdio (DON'T endl; BUT "\n";) ll N, K; IN(N, K); vll A(N); REP(i, N) { IN(A[i]); } vll A_new(N); auto f = [](ll a, ll b) { return a + b; }; Segment_Tree<ll> que(N, f, 0); if (K > sqrt(N) * 2 + 10) { REP(i, N) { A[i] = N; } } else { REP(k, K) { bool N_flag = true; REP(i, N) A_new[i] = 0; REP(i, N) { if (A[i] < N) { N_flag = false; } ll start = max(i - A[i], (ll)0); ll end = min(i + A[i] + 1, N); A_new[start]++; A_new[end]--; } if (N_flag) { REP(i, N) { A[i] = N; } break; } REP(i, N - 1) { A[i] = A_new[i]; A_new[i + 1] += A_new[i]; } A[N - 1] = A_new[N - 1]; } } REP(i, N) { printf("%lld ", A[i]); } printf("\n"); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<ll> vll; typedef vector<vector<ll>> vvll; typedef vector<vector<vector<ll>>> vvvll; typedef vector<bool> vb; typedef vector<vector<bool>> vvb; typedef vector<vector<vector<bool>>> vvvb; typedef pair<ll, ll> pll; typedef vector<pll> vpll; typedef vector<vpll> vvpll; typedef vector<double> vd; typedef vector<vd> vdd; #define FOR(i, x, y) for (ll i = (ll)x; i < (ll)y; ++i) #define REP(i, y) FOR(i, 0, y) #define RFOR(i, x, y) for (ll i = (ll)x; i >= (ll)y; --i) #define RREP(i, x) RFOR(i, x, 0) #define ALL(a) a.begin(), a.end() #define pb push_back #define debug_print(x...) \ cerr << "line " << __LINE__ << " : "; \ debug_print_in(x); template <typename First> void debug_print_in(First first) { cerr << first << endl; return; } template <typename First, typename... Rest> void debug_print_in(First first, Rest... rest) { cerr << first << " "; debug_print_in(rest...); return; } void IN(void) { return; } template <typename First, typename... Rest> void IN(First &first, Rest &...rest) { cin >> first; IN(rest...); return; } template <typename First> void OUT(First first) { cout << first << endl; return; } template <typename First, typename... Rest> void OUT(First first, Rest... rest) { cout << first << " "; OUT(rest...); return; } template <class t, class u> void chmax(t &a, u b) { if (a < b) a = b; }; template <class t, class u> void chmin(t &a, u b) { if (a > b) a = b; }; int popcount(int t) { return __builtin_popcount(t); } // GCC int popcount(ll t) { return __builtin_popcountll(t); } // GCC template <typename T> void vec_print(vector<T> VEC) { REP(i, VEC.size()) { cerr << VEC[i] << " "; } cerr << endl; }; template <typename T> void mat_print(vector<vector<T>> MAT){ REP(i, MAT.size()){REP(j, MAT[i].size()){cerr << MAT[i][j] << " "; } cerr << endl; } } ; constexpr int INF = (1 << 30); constexpr ll INFLL = 1LL << 62; constexpr long double EPS = 1e-12; constexpr ll MOD = (ll)((1E+9) + 7); template <typename DATA> class Segment_Tree { private: ll n; ll n_; using F = function<DATA(DATA, DATA)>; const F f; // query function const DATA init; public: vector<DATA> dat; vector<DATA> lazy1; // 遅延評価 - http://tsutaj.hatenablog.com/entry/2017/03/30/224339 vector<DATA> lazy2; // 遅延評価 - http://tsutaj.hatenablog.com/entry/2017/03/30/224339 vector<bool> lazy1_flag; vector<bool> lazy2_flag; Segment_Tree(ll n__, const F &f, DATA init) : f(f), init(init) { // For simply, the number of component is set to be power of 2 n = 1; n_ = n__; while (n < n_) n *= 2; dat.resize(2 * n - 1, init); lazy1.resize(2 * n - 1, init); lazy2.resize(2 * n - 1, init); lazy1_flag.resize(2 * n - 1, false); lazy2_flag.resize(2 * n - 1, false); return; }; ~Segment_Tree(void) { return; }; void zero(void) { for (ll i = 0; i < 2 * n - 1; ++i) { dat[i] = init; lazy1[i] = init; lazy2[i] = init; lazy1_flag[i] = false; lazy2_flag[i] = false; } return; } void eval1(ll k, ll l, ll r) { if (!lazy1_flag[k]) return; if (l >= r) return; lazy1_flag[k] = false; dat[k] = lazy1[k]; if (r - l > 1) { lazy1[2 * k + 1] = lazy1[k]; lazy1[2 * k + 2] = lazy1[k]; lazy1_flag[2 * k + 1] = true; lazy1_flag[2 * k + 2] = true; } lazy1[k] = init; } DATA eval2_2(DATA tmp, ll n) { DATA ans = tmp; n--; while (n > 0) { if (n & 1) ans = f(ans, tmp); tmp = f(tmp, tmp); n >>= 1; } return ans; } void eval2(ll k, ll l, ll r) { if (!lazy2_flag[k]) return; if (l >= r) return; lazy2_flag[k] = false; dat[k] = f(dat[k], eval2_2(lazy2[k], r - l)); if (r - l > 1) { lazy2[2 * k + 1] = f(lazy2[2 * k + 1], lazy2[k]); lazy2[2 * k + 2] = f(lazy2[2 * k + 2], lazy2[k]); lazy2_flag[2 * k + 1] = true; lazy2_flag[2 * k + 2] = true; } lazy2[k] = init; } // k-th value is changed to a void update1(ll k, DATA a) { k += n - 1; dat[k] = a; while (k > 0) { k = (k - 1) / 2; dat[k] = f(dat[k * 2 + 1], dat[k * 2 + 2]); } return; }; // k-th value is operated of a void update2(ll k, DATA a) { k += n - 1; dat[k] = f(dat[k], a); while (k > 0) { k = (k - 1) / 2; dat[k] = f(dat[k * 2 + 1], dat[k * 2 + 2]); } return; }; //[a,b)の範囲の値をxに変える void rangeupdate1(ll a, ll b, DATA x, ll k, ll l, ll r) { eval1(k, l, r); if (r <= a || b <= l) return; if (a <= l && r <= b) { lazy1[k] = x; lazy1_flag[k] = true; // eval1(k,l,r); } else { rangeupdate1(a, b, x, 2 * k + 1, l, (l + r) / 2); rangeupdate1(a, b, x, 2 * k + 2, (l + r) / 2, r); dat[k] = f(dat[2 * k + 1], dat[2 * k + 2]); } return; } void rangeupdate1(ll a, ll b, DATA x) { rangeupdate1(a, b, x, 0, 0, n); } //[a,b)の範囲に対してxを作用させる void rangeupdate2(ll a, ll b, DATA x, ll k, ll l, ll r) { eval2(k, l, r); if (r <= a || b <= l) return; if (a <= l && r <= b) { lazy2[k] = f(lazy2[k], x); lazy2_flag[k] = true; eval2(k, l, r); } else { rangeupdate2(a, b, x, 2 * k + 1, l, (l + r) / 2); rangeupdate2(a, b, x, 2 * k + 2, (l + r) / 2, r); dat[k] = f(dat[2 * k + 1], dat[2 * k + 2]); } return; } void rangeupdate2(ll a, ll b, DATA x) { rangeupdate2(a, b, x, 0, 0, n); } // search the minimum of [a, b) // Other values are used for simply // From outside, call query(a, b, 0, 0, n) // k is the node index, // temporary block is [l, r) DATA query(ll a, ll b, ll k, ll l, ll r) { // if there are no overlap between [a, b) and [l, r) if (r <= a || b <= l) return init; eval1(k, l, r); eval2(k, l, r); // if [a, b) contains [l, r), return the value of this node if (a <= l && r <= b) return dat[k]; DATA vl = query(a, b, k * 2 + 1, l, (l + r) / 2); DATA vr = query(a, b, k * 2 + 2, (l + r) / 2, r); return f(vl, vr); }; // search the minimum of [a, b) DATA query(ll a, ll b) { if (a < 0 || n_ < b || a == b) { fprintf(stderr, "Segment_Tree Search Space Error\n"); fprintf(stderr, "Desired:\n"); fprintf(stderr, "%lld <= %lld < %lld <= %lld\n", 0, a, b, n_); exit(0); } return query(a, b, 0, 0, n); }; }; int main() { cin.tie(0); // cut the cin and cout (default, std::flush is performed after // std::cin) ios::sync_with_stdio( false); // cut the iostream and stdio (DON'T endl; BUT "\n";) ll N, K; IN(N, K); vll A(N); REP(i, N) { IN(A[i]); } vll A_new(N + 1); auto f = [](ll a, ll b) { return a + b; }; Segment_Tree<ll> que(N, f, 0); if (K > sqrt(N) * 2 + 10) { REP(i, N) { A[i] = N; } } else { REP(k, K) { bool N_flag = true; REP(i, N) A_new[i] = 0; REP(i, N) { if (A[i] < N) { N_flag = false; } ll start = max(i - A[i], (ll)0); ll end = min(i + A[i] + 1, N); A_new[start]++; A_new[end]--; } if (N_flag) { REP(i, N) { A[i] = N; } break; } REP(i, N - 1) { A[i] = A_new[i]; A_new[i + 1] += A_new[i]; } A[N - 1] = A_new[N - 1]; } } REP(i, N) { printf("%lld ", A[i]); } printf("\n"); return 0; }
replace
295
296
295
296
-6
munmap_chunk(): invalid pointer
p02647
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; int n, k, a[maxn], d[maxn]; int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); while (k--) { for (int i = 0; i <= n + 1; i++) d[i] = 0; for (int i = 1; i <= n; i++) { int l = max(i - a[i], 1), r = min(i + a[i], n); d[l]++, d[r + 1]--; } for (int i = 1; i <= n; i++) d[i] += d[i - 1]; for (int i = 1; i <= n; i++) a[i] = d[i]; } for (int i = 1; i <= n; i++) printf("%d%s", a[i], i == n ? "\n" : " "); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; int n, k, a[maxn], d[maxn]; int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); while (k--) { bool f = true; for (int i = 1; i <= n; i++) if (a[i] != n) f = false; if (f) break; for (int i = 0; i <= n + 1; i++) d[i] = 0; for (int i = 1; i <= n; i++) { int l = max(i - a[i], 1), r = min(i + a[i], n); d[l]++, d[r + 1]--; } for (int i = 1; i <= n; i++) d[i] += d[i - 1]; for (int i = 1; i <= n; i++) a[i] = d[i]; } for (int i = 1; i <= n; i++) printf("%d%s", a[i], i == n ? "\n" : " "); return 0; }
insert
9
9
9
15
TLE
p02647
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { long long num, k; long long tmp1, tmp2; long long Min = 0; cin >> num >> k; // vector<long long> vec(num,0); vector<long long> d(num, 0); for (int i = 0; i < num; i++) cin >> d.at(i); for (int i = 0; i < k; i++) { vector<long long> vec(num + 1, 0); for (int j = 0; j < num; j++) { tmp1 = max(Min, (j + 1) - d.at(j) - 1); tmp2 = min(num, (j + 1) + d.at(j)); vec.at(tmp1)++; vec.at(tmp2)--; } for (int j = 0; j < num; j++) { vec.at(j + 1) += vec.at(j); } d = vec; } for (int i = 0; i < num; i++) { cout << d.at(i) << " "; } }
#include <bits/stdc++.h> using namespace std; int main() { long long num, k; long long tmp1, tmp2; long long Min = 0; cin >> num >> k; // vector<long long> vec(num,0); vector<long long> d(num, 0); for (int i = 0; i < num; i++) cin >> d.at(i); for (int i = 0; i < k; i++) { vector<long long> vec(num + 1, 0); for (int j = 0; j < num; j++) { tmp1 = max(Min, (j + 1) - d.at(j) - 1); tmp2 = min(num, (j + 1) + d.at(j)); vec.at(tmp1)++; vec.at(tmp2)--; } for (int j = 0; j < num; j++) { vec.at(j + 1) += vec.at(j); } vec.pop_back(); if (d == vec) break; d = vec; } for (int i = 0; i < num; i++) { cout << d.at(i) << " "; } }
insert
28
28
28
32
TLE
p02647
C++
Time Limit Exceeded
#define rep(i, n) for (int i = 0; i < (int)(n); i++) #include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> v, vmin, vmax; rep(i, n) { int a; cin >> a; v.push_back(a); } vector<int> org(n, 0); vector<int> s = v; if (k < 1000000) { rep(j, k) { v = s; s = org; vmin.clear(); vmax.clear(); rep(i, n) { vmin.push_back(i - v[i]); vmax.push_back(i + v[i]); } vmin.push_back(1000000); vmax.push_back(1000000); sort(vmin.begin(), vmin.end()); sort(vmax.begin(), vmax.end()); // if(vmin[n-1]<0 &&vmax[n-1]>n-1 ){ // rep(i,n){ // s[i] += (n-1)* (k-j); // } // break; // } int value = 0; int i = 0, mini = 0, maxi = 0; // i = max(0,vmin[0]); // s = org; while (i < n) { if (i >= vmin[mini]) { value++; mini++; } else if (i > vmax[maxi]) { value--; maxi++; } else { s[i] = value; i++; } // cout << i <<" " << value<< " " << mini << " " <<maxi << endl; } } } else { rep(i, n) { s[i] = n; } } rep(i, n) { cout << s[i] << " "; } cout << endl; }
#define rep(i, n) for (int i = 0; i < (int)(n); i++) #include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> v, vmin, vmax; rep(i, n) { int a; cin >> a; v.push_back(a); } vector<int> org(n, 0); vector<int> s = v; if (k < 100) { rep(j, k) { v = s; s = org; vmin.clear(); vmax.clear(); rep(i, n) { vmin.push_back(i - v[i]); vmax.push_back(i + v[i]); } vmin.push_back(1000000); vmax.push_back(1000000); sort(vmin.begin(), vmin.end()); sort(vmax.begin(), vmax.end()); // if(vmin[n-1]<0 &&vmax[n-1]>n-1 ){ // rep(i,n){ // s[i] += (n-1)* (k-j); // } // break; // } int value = 0; int i = 0, mini = 0, maxi = 0; // i = max(0,vmin[0]); // s = org; while (i < n) { if (i >= vmin[mini]) { value++; mini++; } else if (i > vmax[maxi]) { value--; maxi++; } else { s[i] = value; i++; } // cout << i <<" " << value<< " " << mini << " " <<maxi << endl; } } } else { rep(i, n) { s[i] = n; } } rep(i, n) { cout << s[i] << " "; } cout << endl; }
replace
19
20
19
20
TLE
p02647
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define _GLIBCXX_DEBUG #define rep(i, n) for (int i = 0; i < (n); ++i) #define rep1(i, n) for (int i = 1; i < (n); ++i) using namespace std; typedef long long ll; #define SEG_LEN (1 << 18) int seg[SEG_LEN * 2]; // one point long long int get(int ind) { ind += SEG_LEN; long long int sum = 0; sum += seg[ind]; while (true) { ind /= 2; if (ind == 0) break; sum += seg[ind]; } return sum; } // range void add(int l, int r, int x) { l += SEG_LEN; r += SEG_LEN; while (l < r) { if (l % 2 == 1) { seg[l] += x; l++; } l /= 2; if (r % 2 == 1) { seg[r - 1] += x; r--; } r /= 2; } } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); ll n, k; cin >> n >> k; vector<ll> a(n); rep(i, n) cin >> a[i]; rep(i, n) { add(i, i + 1, a[i]); } rep(c, 200) { if (c == k) break; vector<pair<ll, ll>> addd(n); rep(i, n) { int l = max(i - get(i), 0ll); int r = min(i + get(i), n - 1); addd[i] = make_pair(l, r); } vector<int> tmp(n); rep(i, n) tmp[i] = get(i); rep(i, n) { int l = addd[i].first; int r = addd[i].second; add(l, r + 1, 1); } rep(i, n) { add(i, i + 1, -tmp[i]); } } rep(j, n - 1) { cout << get(j) << " "; } cout << get(n - 1) << endl; return 0; }
#include <bits/stdc++.h> #define _GLIBCXX_DEBUG #define rep(i, n) for (int i = 0; i < (n); ++i) #define rep1(i, n) for (int i = 1; i < (n); ++i) using namespace std; typedef long long ll; #define SEG_LEN (1 << 18) int seg[SEG_LEN * 2]; // one point long long int get(int ind) { ind += SEG_LEN; long long int sum = 0; sum += seg[ind]; while (true) { ind /= 2; if (ind == 0) break; sum += seg[ind]; } return sum; } // range void add(int l, int r, int x) { l += SEG_LEN; r += SEG_LEN; while (l < r) { if (l % 2 == 1) { seg[l] += x; l++; } l /= 2; if (r % 2 == 1) { seg[r - 1] += x; r--; } r /= 2; } } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); ll n, k; cin >> n >> k; vector<ll> a(n); rep(i, n) cin >> a[i]; rep(i, n) { add(i, i + 1, a[i]); } rep(c, 200) { if (c == k) break; vector<pair<ll, ll>> addd(n); rep(i, n) { int l = max(i - get(i), 0ll); int r = min(i + get(i), n - 1); addd[i] = make_pair(l, r); } vector<int> tmp(n); rep(i, n) tmp[i] = get(i); rep(i, n) { int l = addd[i].first; int r = addd[i].second; add(l, r + 1, 1); } rep(i, n) { add(i, i + 1, -tmp[i]); } bool flg = true; rep(i, n) { if (get(i) != n) { flg = false; break; } } if (flg) break; } rep(j, n - 1) { cout << get(j) << " "; } cout << get(n - 1) << endl; return 0; }
insert
75
75
75
85
TLE
p02647
C++
Time Limit Exceeded
// teja349 #include <algorithm> #include <bits/stdc++.h> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <iomanip> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> // setbase - cout << setbase (16); cout << 100 << endl; Prints 64 // setfill - cout << setfill ('x') << setw (5); cout << 77 << endl; prints // xxx77 setprecision - cout << setprecision (14) << f << endl; Prints x.xxxx // cout.precision(x) cout<<fixed<<val; // prints x digits after decimal in val using namespace std; using namespace __gnu_pbds; #define f(i, a, b) for (i = a; i < b; i++) #define rep(i, n) f(i, 0, n) #define fd(i, a, b) for (i = a; i >= b; i--) #define pb push_back #define mp make_pair #define vi vector<int> #define vl vector<ll> #define ss second #define ff first #define ll long long #define pii pair<int, int> #define pll pair<ll, ll> #define sz(a) a.size() #define inf (1000 * 1000 * 1000 + 5) #define all(a) a.begin(), a.end() #define tri pair<int, pii> #define vii vector<pii> #define vll vector<pll> #define viii vector<tri> #define mod (1000 * 1000 * 1000 + 7) #define pqueue priority_queue<int> #define pdqueue priority_queue<int, vi, greater<int>> #define flush fflush(stdout) #define primeDEN 727999983 mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); // find_by_order() // order_of_key typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; int b[212345], a[212345]; int main() { std::ios::sync_with_stdio(false); cin.tie(NULL); int n, k; cin >> n >> k; int i, j; rep(i, n) { cin >> a[i]; } rep(i, k) { int flag = 0; rep(j, n) { b[j] = 0; if (a[j] != n) { flag = 1; // break; } } if (flag = 0) break; rep(j, n) { int lef = j - a[j]; int rig = j + a[j]; rig = min(rig, n - 1); rig++; lef = max(lef, 0); b[lef]++; b[rig]--; } a[0] = b[0]; f(j, 1, n) { b[j] += b[j - 1]; a[j] = b[j]; } } rep(i, n) { cout << a[i] << " "; } cout << endl; return 0; }
// teja349 #include <algorithm> #include <bits/stdc++.h> #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <iomanip> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> // setbase - cout << setbase (16); cout << 100 << endl; Prints 64 // setfill - cout << setfill ('x') << setw (5); cout << 77 << endl; prints // xxx77 setprecision - cout << setprecision (14) << f << endl; Prints x.xxxx // cout.precision(x) cout<<fixed<<val; // prints x digits after decimal in val using namespace std; using namespace __gnu_pbds; #define f(i, a, b) for (i = a; i < b; i++) #define rep(i, n) f(i, 0, n) #define fd(i, a, b) for (i = a; i >= b; i--) #define pb push_back #define mp make_pair #define vi vector<int> #define vl vector<ll> #define ss second #define ff first #define ll long long #define pii pair<int, int> #define pll pair<ll, ll> #define sz(a) a.size() #define inf (1000 * 1000 * 1000 + 5) #define all(a) a.begin(), a.end() #define tri pair<int, pii> #define vii vector<pii> #define vll vector<pll> #define viii vector<tri> #define mod (1000 * 1000 * 1000 + 7) #define pqueue priority_queue<int> #define pdqueue priority_queue<int, vi, greater<int>> #define flush fflush(stdout) #define primeDEN 727999983 mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); // find_by_order() // order_of_key typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; int b[212345], a[212345]; int main() { std::ios::sync_with_stdio(false); cin.tie(NULL); int n, k; cin >> n >> k; int i, j; rep(i, n) { cin >> a[i]; } rep(i, k) { int flag = 0; rep(j, n) { b[j] = 0; if (a[j] != n) { flag = 1; // break; } } if (flag == 0) break; rep(j, n) { int lef = j - a[j]; int rig = j + a[j]; rig = min(rig, n - 1); rig++; lef = max(lef, 0); b[lef]++; b[rig]--; } a[0] = b[0]; f(j, 1, n) { b[j] += b[j - 1]; a[j] = b[j]; } } rep(i, n) { cout << a[i] << " "; } cout << endl; return 0; }
replace
73
74
73
74
TLE
p02647
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long int ll; ll N1097 = 1000000007LL; ll N, K; ll A[1000000]; ll d[1000000]; int main() { cin >> N >> K; for (ll i = 0; i < N; i++) { scanf("%ld", &A[i]); } // for (ll l = 0; l < K; l++) { for (ll i = 0; i <= N; i++) { d[i] = 0LL; } for (ll i = 0; i < N; i++) { ll a = A[i]; d[max(0LL, i - a)] += 1; d[min(N, i + a + 1)] -= 1; } ll tmp = 0; for (ll i = 0; i < N; i++) { tmp += d[i]; A[i] = tmp; } } for (ll i = 0; i < N; i++) { printf("%d ", A[i]); } }
#include <bits/stdc++.h> using namespace std; typedef long long int ll; ll N1097 = 1000000007LL; ll N, K; ll A[1000000]; ll d[1000000]; int main() { cin >> N >> K; for (ll i = 0; i < N; i++) { scanf("%ld", &A[i]); } // for (ll l = 0; l < K; l++) { for (ll i = 0; i <= N; i++) { d[i] = 0LL; } for (ll i = 0; i < N; i++) { ll a = A[i]; d[max(0LL, i - a)] += 1; d[min(N, i + a + 1)] -= 1; } ll tmp = 0; for (ll i = 0; i < N; i++) { tmp += d[i]; A[i] = tmp; } int f = 1; for (ll i = 0; i < N; i++) { if (A[i] != N) { f = 0; break; } } if (f) { break; } } for (ll i = 0; i < N; i++) { printf("%d ", A[i]); } }
insert
29
29
29
39
TLE
p02647
C++
Runtime Error
// includes #include <bits/stdc++.h> using namespace std; // macros #define pb emplace_back #define mk make_pair #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) FOR(i, 0, n) #define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--) #define irep(itr, st) for (auto itr = (st).begin(); itr != (st).end(); ++itr) #define irrep(itr, st) for (auto itr = (st).rbegin(); itr != (st).rend(); ++itr) #define whole(x) (x).begin(), (x).end() #define sz(x) ((int)(x).size()) #define bit(n) (1LL << (n)) #define F first #define S second // functions template <typename T> void unique(T &c) { c.erase(std::unique(c.begin(), c.end()), c.end()); } template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } 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) { for (int i = 0; i < vec.size(); i++) { os << vec[i]; if (i + 1 != vec.size()) os << " "; } return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &st) { for (auto itr = st.begin(); itr != st.end(); ++itr) { os << *itr; auto titr = itr; if (++titr != st.end()) os << " "; } return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_set<T> &st) { for (auto itr = st.begin(); itr != st.end(); ++itr) { os << *itr; auto titr = itr; if (++titr != st.end()) os << " "; } return os; } template <typename T> ostream &operator<<(ostream &os, const multiset<T> &st) { for (auto itr = st.begin(); itr != st.end(); ++itr) { os << *itr; auto titr = itr; if (++titr != st.end()) os << " "; } return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &st) { for (auto itr = st.begin(); itr != st.end(); ++itr) { os << *itr; auto titr = itr; if (++titr != st.end()) os << " "; } return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << "(" << p.first << ", " << p.second << ")"; return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const map<T1, T2> &mp) { for (auto itr = mp.begin(); itr != mp.end(); ++itr) { os << "(" << itr->first << ", " << itr->second << ")"; auto titr = itr; if (++titr != mp.end()) os << " "; } return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const unordered_map<T1, T2> &mp) { for (auto itr = mp.begin(); itr != mp.end(); ++itr) { os << "(" << itr->first << ", " << itr->second << ")"; auto titr = itr; if (++titr != mp.end()) os << " "; } return os; } // types using ll = long long int; using P = pair<int, int>; // constants const int inf = 1e9; const ll linf = 1LL << 60; const double EPS = 1e-10; const int mod = 1000000007; const int dx[4] = {-1, 0, 1, 0}; const int dy[4] = {0, -1, 0, 1}; // io struct fast_io { fast_io() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(20); } } fast_io_; int main(int argc, char const *argv[]) { int n, k; cin >> n >> k; vector<ll> a(n); cin >> a; vector<ll> v(n, 0); rep(l, k) { rep(i, n) { int l = i - a[i]; v[max(0, l)]++; int r = i + a[i] + 1; if (r < n) v[r]--; } bool same = true; ; FOR(i, 1, n + 1) v[i] += v[i - 1]; rep(i, n) { if (a[i] != n) same = false; } swap(a, v); if (same) break; v.assign(n, 0); } cout << a << endl; return 0; }
// includes #include <bits/stdc++.h> using namespace std; // macros #define pb emplace_back #define mk make_pair #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define rep(i, n) FOR(i, 0, n) #define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--) #define irep(itr, st) for (auto itr = (st).begin(); itr != (st).end(); ++itr) #define irrep(itr, st) for (auto itr = (st).rbegin(); itr != (st).rend(); ++itr) #define whole(x) (x).begin(), (x).end() #define sz(x) ((int)(x).size()) #define bit(n) (1LL << (n)) #define F first #define S second // functions template <typename T> void unique(T &c) { c.erase(std::unique(c.begin(), c.end()), c.end()); } template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } 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) { for (int i = 0; i < vec.size(); i++) { os << vec[i]; if (i + 1 != vec.size()) os << " "; } return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &st) { for (auto itr = st.begin(); itr != st.end(); ++itr) { os << *itr; auto titr = itr; if (++titr != st.end()) os << " "; } return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_set<T> &st) { for (auto itr = st.begin(); itr != st.end(); ++itr) { os << *itr; auto titr = itr; if (++titr != st.end()) os << " "; } return os; } template <typename T> ostream &operator<<(ostream &os, const multiset<T> &st) { for (auto itr = st.begin(); itr != st.end(); ++itr) { os << *itr; auto titr = itr; if (++titr != st.end()) os << " "; } return os; } template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &st) { for (auto itr = st.begin(); itr != st.end(); ++itr) { os << *itr; auto titr = itr; if (++titr != st.end()) os << " "; } return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << "(" << p.first << ", " << p.second << ")"; return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const map<T1, T2> &mp) { for (auto itr = mp.begin(); itr != mp.end(); ++itr) { os << "(" << itr->first << ", " << itr->second << ")"; auto titr = itr; if (++titr != mp.end()) os << " "; } return os; } template <typename T1, typename T2> ostream &operator<<(ostream &os, const unordered_map<T1, T2> &mp) { for (auto itr = mp.begin(); itr != mp.end(); ++itr) { os << "(" << itr->first << ", " << itr->second << ")"; auto titr = itr; if (++titr != mp.end()) os << " "; } return os; } // types using ll = long long int; using P = pair<int, int>; // constants const int inf = 1e9; const ll linf = 1LL << 60; const double EPS = 1e-10; const int mod = 1000000007; const int dx[4] = {-1, 0, 1, 0}; const int dy[4] = {0, -1, 0, 1}; // io struct fast_io { fast_io() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(20); } } fast_io_; int main(int argc, char const *argv[]) { int n, k; cin >> n >> k; vector<ll> a(n); cin >> a; vector<ll> v(n, 0); rep(l, k) { rep(i, n) { int l = i - a[i]; v[max(0, l)]++; int r = i + a[i] + 1; if (r < n) v[r]--; } bool same = true; ; FOR(i, 1, n) v[i] += v[i - 1]; rep(i, n) { if (a[i] != n) same = false; } swap(a, v); if (same) break; v.assign(n, 0); } cout << a << endl; return 0; }
replace
146
147
146
147
0
p02647
C++
Runtime Error
#include <bits/stdc++.h> using namespace std::literals::string_literals; using i64 = std::int_fast64_t; using std::cerr; using std::cin; using std::cout; using std::endl; template <typename T> std::vector<T> make_v(size_t a) { return std::vector<T>(a); } template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) { return std::vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); } #define NDEBUG std::vector<int> next(std::vector<int> v) { std::vector<int> vec(v.size() + 1); for (int i = 0; i < (int)v.size(); i++) { int L = std::max(0, i - v[i]); int R = std::min((int)v.size() - 1, i + v[i]); vec[L]++; vec[R + 1]--; } for (int i = 0; i < (int)v.size() - 1; i++) vec[i + 1] += vec[i]; return vec; }; int main() { int n, k; scanf("%d%d", &n, &k); std::vector<int> a(n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); #ifndef NDEBUG cout << 0 << ": "; for (auto v : a) cout << v << " "; cout << endl; for (int i = 0; i < k; i++) { a = next(a); cout << i + 1 << ": "; for (auto v : a) cout << v << " "; cout << endl; } #endif for (int i = 0; i < k; i++) { auto v = next(a); if (v == a) break; a.swap(v); } for (auto v : a) cout << v << " "; cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std::literals::string_literals; using i64 = std::int_fast64_t; using std::cerr; using std::cin; using std::cout; using std::endl; template <typename T> std::vector<T> make_v(size_t a) { return std::vector<T>(a); } template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) { return std::vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...)); } #define NDEBUG std::vector<int> next(std::vector<int> v) { std::vector<int> vec(v.size() + 1); for (int i = 0; i < (int)v.size(); i++) { int L = std::max(0, i - v[i]); int R = std::min((int)v.size() - 1, i + v[i]); vec[L]++; vec[R + 1]--; } for (int i = 0; i < (int)v.size() - 1; i++) vec[i + 1] += vec[i]; vec.pop_back(); return vec; }; int main() { int n, k; scanf("%d%d", &n, &k); std::vector<int> a(n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); #ifndef NDEBUG cout << 0 << ": "; for (auto v : a) cout << v << " "; cout << endl; for (int i = 0; i < k; i++) { a = next(a); cout << i + 1 << ": "; for (auto v : a) cout << v << " "; cout << endl; } #endif for (int i = 0; i < k; i++) { auto v = next(a); if (v == a) break; a.swap(v); } for (auto v : a) cout << v << " "; cout << endl; return 0; }
insert
29
29
29
30
0
p02647
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define all(v) v.begin(), v.end() int main() { int n, k; cin >> n >> k; vector<int> A(n); rep(i, n) cin >> A[i]; if (n == 1) { cout << min(n, k) << endl; } else { for (int l = 1; l <= min(k, 50); l++) { vector<int> B(n, 0); for (int i = 0; i < n; i++) { int l = max(0, i - A[i]); int r = min(i + A[i], n - 1); B[l]++; if (B[r + 1] < n) { B[r + 1]--; } } for (int i = 1; i < n; i++) B[i] += B[i - 1]; // B complete rep(i, n) { A[i] = B[i]; // new A } } } rep(i, n) { if (i == n - 1) { if (A[i] > n) { cout << n << endl; } else { cout << A[i] << endl; } } else { if (A[i] > n) { cout << n << endl; } else { cout << A[i] << " "; } } } }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define all(v) v.begin(), v.end() int main() { int n, k; cin >> n >> k; vector<int> A(n); rep(i, n) cin >> A[i]; if (n == 1) { cout << min(n, k) << endl; } else { for (int l = 1; l <= min(k, 50); l++) { vector<int> B(n, 0); for (int i = 0; i < n; i++) { int l = max(0, i - A[i]); int r = min(i + A[i], n - 1); B[l]++; if (r + 1 < n) { B[r + 1]--; } } for (int i = 1; i < n; i++) B[i] += B[i - 1]; // B complete rep(i, n) { A[i] = B[i]; // new A } } } rep(i, n) { if (i == n - 1) { if (A[i] > n) { cout << n << endl; } else { cout << A[i] << endl; } } else { if (A[i] > n) { cout << n << endl; } else { cout << A[i] << " "; } } } }
replace
22
23
22
23
0
p02647
C++
Time Limit Exceeded
#include <algorithm> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> #define INF 2e9 #define rep(i, n, m) for (int i = n; i < m; i++) using namespace std; int main() { int n, k; cin >> n >> k; vector<int> a(n), b(n + 1, 0); rep(i, 0, n) cin >> a[i]; while (k) { rep(i, 0, n) { b[max(0, i - a[i])]++; b[min(n, i + a[i] + 1)]--; } rep(i, 0, n) { b[i + 1] += b[i]; a[i] = b[i]; b[i] = 0; } k--; } rep(i, 0, n) cout << a[i] << " "; cout << endl; return 0; }
#include <algorithm> #include <iostream> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> #define INF 2e9 #define rep(i, n, m) for (int i = n; i < m; i++) using namespace std; int main() { int n, k; cin >> n >> k; vector<int> a(n), b(n + 1, 0); rep(i, 0, n) cin >> a[i]; if (k > 500) k = 500; while (k) { rep(i, 0, n) { b[max(0, i - a[i])]++; b[min(n, i + a[i] + 1)]--; } rep(i, 0, n) { b[i + 1] += b[i]; a[i] = b[i]; b[i] = 0; } k--; } rep(i, 0, n) cout << a[i] << " "; cout << endl; return 0; }
insert
22
22
22
24
TLE
p02648
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define FOR(i, n, m) for (int i = (int)(n); i <= (int)(m); i++) #define RFOR(i, n, m) for (int i = (int)(n); i >= (int)(m); i--) #define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++) #define RITR(x, c) for (__typeof(c.rbegin()) x = c.rbegin(); x != c.rend(); x++) #define setp(n) fixed << setprecision(n) template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } #define ll long long #define vll vector<ll> #define vi vector<int> #define pll pair<ll, ll> #define pi pair<int, int> #define all(a) (a.begin()), (a.end()) #define rall(a) (a.rbegin()), (a.rend()) #define fi first #define se second #define pb push_back #define mp make_pair #define ins insert using namespace std; const int INF = 1e9; const int MAX_K = 1; struct P { int v, w; P() {} P(int v, int w) : v(v), w(w) {} }; bool operator<(const P &a, const P &b) { return a.w < b.w; } P operator+(const P &a, const P &b) { return {a.v + b.v, a.w + b.w}; } ostream &operator<<(ostream &os, const P &a) { return os << "(" << a.w << "," << a.v << ")"; } vector<P> S[MAX_K]; //------------------------------------------------- int main(void) { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; vector<P> nodes(N); rep(i, N) { int v, w; cin >> v >> w; nodes[i] = P(v, w); } S[0].pb(P(0, 0)); int K = min(MAX_K, N + 1); FOR(i, 1, K - 1) { int p = i / 2; for (P e : S[p]) { S[i].pb(e); S[i].pb(e + nodes[i - 1]); } sort(all(S[i])); int M = S[i].size(); int m = 1; FOR(j, 1, M - 1) { if (S[i][m - 1].v < S[i][j].v) { S[i][m++] = S[i][j]; } } S[i].erase(S[i].begin() + m, S[i].end()); } // for(P e:nodes){ // cout<<e<<" "; // }cout<<endl; // rep(v,K){ // for(P e:S[v]){ // cout<<e<<" "; // }cout<<endl; // } int Q; cin >> Q; while (Q--) { int v, L; cin >> v >> L; vector<P> ps; ps.pb(P(0, 0)); while (v >= K) { int M = ps.size(); rep(i, M) { ps.pb(ps[i] + nodes[v - 1]); } v >>= 1; } int ans = 0; for (P e : ps) { if (L < e.w) continue; auto itr = upper_bound(all(S[v]), P(0, L - e.w)); itr--; chmax(ans, e.v + itr->v); } cout << ans << "\n"; } return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define FOR(i, n, m) for (int i = (int)(n); i <= (int)(m); i++) #define RFOR(i, n, m) for (int i = (int)(n); i >= (int)(m); i--) #define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++) #define RITR(x, c) for (__typeof(c.rbegin()) x = c.rbegin(); x != c.rend(); x++) #define setp(n) fixed << setprecision(n) template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } #define ll long long #define vll vector<ll> #define vi vector<int> #define pll pair<ll, ll> #define pi pair<int, int> #define all(a) (a.begin()), (a.end()) #define rall(a) (a.rbegin()), (a.rend()) #define fi first #define se second #define pb push_back #define mp make_pair #define ins insert using namespace std; const int INF = 1e9; const int MAX_K = 1 << 10; struct P { int v, w; P() {} P(int v, int w) : v(v), w(w) {} }; bool operator<(const P &a, const P &b) { return a.w < b.w; } P operator+(const P &a, const P &b) { return {a.v + b.v, a.w + b.w}; } ostream &operator<<(ostream &os, const P &a) { return os << "(" << a.w << "," << a.v << ")"; } vector<P> S[MAX_K]; //------------------------------------------------- int main(void) { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; vector<P> nodes(N); rep(i, N) { int v, w; cin >> v >> w; nodes[i] = P(v, w); } S[0].pb(P(0, 0)); int K = min(MAX_K, N + 1); FOR(i, 1, K - 1) { int p = i / 2; for (P e : S[p]) { S[i].pb(e); S[i].pb(e + nodes[i - 1]); } sort(all(S[i])); int M = S[i].size(); int m = 1; FOR(j, 1, M - 1) { if (S[i][m - 1].v < S[i][j].v) { S[i][m++] = S[i][j]; } } S[i].erase(S[i].begin() + m, S[i].end()); } // for(P e:nodes){ // cout<<e<<" "; // }cout<<endl; // rep(v,K){ // for(P e:S[v]){ // cout<<e<<" "; // }cout<<endl; // } int Q; cin >> Q; while (Q--) { int v, L; cin >> v >> L; vector<P> ps; ps.pb(P(0, 0)); while (v >= K) { int M = ps.size(); rep(i, M) { ps.pb(ps[i] + nodes[v - 1]); } v >>= 1; } int ans = 0; for (P e : ps) { if (L < e.w) continue; auto itr = upper_bound(all(S[v]), P(0, L - e.w)); itr--; chmax(ans, e.v + itr->v); } cout << ans << "\n"; } return 0; }
replace
41
42
41
42
TLE
p02648
C++
Time Limit Exceeded
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <cstdio> #include <deque> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #define REP(i, N) for (int i = 0; i < (int)N; i++) #define FOR(i, a, b) for (int i = a; i < (int)b; i++) #define ALL(x) (x).begin(), (x).end() #define INF (1 << 30) #define LLINF (1LL << 62) #define DEBUG(...) debug(__LINE__, ":" __VA_ARGS__) using namespace std; constexpr int MOD = 1000000007; using ll = long long; using Pii = pair<int, int>; using Pll = pair<ll, ll>; inline int popcount(ll x) { return __builtin_popcountll(x); } inline int div2num(ll x) { return __builtin_ctzll(x); } inline bool bit(ll x, int b) { return (x >> b) & 1; } template <class T> string to_string(T s); template <class S, class T> string to_string(pair<S, T> p); string to_string(string s) { return s; } string to_string(const char s[]) { return to_string(string(s)); } template <class T> string to_string(T v) { if (v.empty()) return "{}"; string ret = "{"; for (auto x : v) ret += to_string(x) + ","; ret.back() = '}'; return ret; } template <class S, class T> string to_string(pair<S, T> p) { return "{" + to_string(p.first) + ":" + to_string(p.second) + "}"; } void debug() { cerr << endl; } template <class Head, class... Tail> void debug(Head head, Tail... tail) { cerr << to_string(head) << " "; debug(tail...); } int main() { int N; cin >> N; vector<int> V(N + 1), W(N + 1); REP(i, N) cin >> V[i + 1] >> W[i + 1]; int Q; cin >> Q; constexpr int b = 2; vector<pair<int, int>> l1, l2, tmp; while (Q--) { int v, L; cin >> v >> L; l1.clear(); l2.clear(); auto gen = [&](vector<pair<int, int>> &l, int thr) { l.emplace_back(0, 0); while (v >= thr) { int p1 = 0, p2 = 0; while (p1 < l.size() || p2 < l.size()) { if (p1 < l.size() && l[p1].first < l[p2].first + W[v]) { if (tmp.empty() || tmp.back().second < l[p1].second) tmp.emplace_back(l[p1]); p1++; } else { if (tmp.empty() || tmp.back().second < l[p2].second + V[v]) tmp.emplace_back(l[p2].first + W[v], l[p2].second + V[v]); p2++; } } v /= 2; swap(l, tmp); tmp.clear(); } }; gen(l1, 1 << b); gen(l2, 1); int p1 = 0, p2 = 0; while (p2 + 1 < l2.size() && l1[p1].first + l2[p2 + 1].first <= L) p2++; int ans = l1[p1].second + l2[p2].second; while (++p1 < l1.size() && l1[p1].first <= L) { while (l1[p1].first + l2[p2].first > L) p2--; ans = max(ans, l1[p1].second + l2[p2].second); } cout << ans << "\n"; } return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <cstdio> #include <deque> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #define REP(i, N) for (int i = 0; i < (int)N; i++) #define FOR(i, a, b) for (int i = a; i < (int)b; i++) #define ALL(x) (x).begin(), (x).end() #define INF (1 << 30) #define LLINF (1LL << 62) #define DEBUG(...) debug(__LINE__, ":" __VA_ARGS__) using namespace std; constexpr int MOD = 1000000007; using ll = long long; using Pii = pair<int, int>; using Pll = pair<ll, ll>; inline int popcount(ll x) { return __builtin_popcountll(x); } inline int div2num(ll x) { return __builtin_ctzll(x); } inline bool bit(ll x, int b) { return (x >> b) & 1; } template <class T> string to_string(T s); template <class S, class T> string to_string(pair<S, T> p); string to_string(string s) { return s; } string to_string(const char s[]) { return to_string(string(s)); } template <class T> string to_string(T v) { if (v.empty()) return "{}"; string ret = "{"; for (auto x : v) ret += to_string(x) + ","; ret.back() = '}'; return ret; } template <class S, class T> string to_string(pair<S, T> p) { return "{" + to_string(p.first) + ":" + to_string(p.second) + "}"; } void debug() { cerr << endl; } template <class Head, class... Tail> void debug(Head head, Tail... tail) { cerr << to_string(head) << " "; debug(tail...); } int main() { int N; cin >> N; vector<int> V(N + 1), W(N + 1); REP(i, N) cin >> V[i + 1] >> W[i + 1]; int Q; cin >> Q; constexpr int b = 9; vector<pair<int, int>> l1, l2, tmp; while (Q--) { int v, L; cin >> v >> L; l1.clear(); l2.clear(); auto gen = [&](vector<pair<int, int>> &l, int thr) { l.emplace_back(0, 0); while (v >= thr) { int p1 = 0, p2 = 0; while (p1 < l.size() || p2 < l.size()) { if (p1 < l.size() && l[p1].first < l[p2].first + W[v]) { if (tmp.empty() || tmp.back().second < l[p1].second) tmp.emplace_back(l[p1]); p1++; } else { if (tmp.empty() || tmp.back().second < l[p2].second + V[v]) tmp.emplace_back(l[p2].first + W[v], l[p2].second + V[v]); p2++; } } v /= 2; swap(l, tmp); tmp.clear(); } }; gen(l1, 1 << b); gen(l2, 1); int p1 = 0, p2 = 0; while (p2 + 1 < l2.size() && l1[p1].first + l2[p2 + 1].first <= L) p2++; int ans = l1[p1].second + l2[p2].second; while (++p1 < l1.size() && l1[p1].first <= L) { while (l1[p1].first + l2[p2].first > L) p2--; ans = max(ans, l1[p1].second + l2[p2].second); } cout << ans << "\n"; } return 0; }
replace
68
69
68
69
TLE
p02648
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; const int maxN = (1 << 18) + 10; const int MAX = 1e5; typedef long long ll; int n, q; #define pii pair<int, int> pii a[maxN]; #define V first #define W second ll pre[maxN]; ll dp[512][MAX + 1]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // freopen("abc.inp", "r", stdin); freopen("abc.out", "w", stdout); cin >> n; for (int i = 1; i <= n; ++i) { cin >> a[i].V >> a[i].W; } const int limit = 512; for (int j = 0; j <= MAX; ++j) dp[1][j] = (a[1].W > j ? 0 : a[1].V); for (int i = 2; i < min(limit, n + 1); ++i) { int par = i / 2; for (int j = 0; j <= MAX; ++j) { dp[i][j] = dp[par][j]; if (j >= a[i].W) dp[i][j] = max(dp[i][j], dp[par][j - a[i].W] + a[i].V); } } cin >> q; while (q--) { int x, L; cin >> x >> L; vector<int> node; while (x >= limit) { node.push_back(x); x /= 2; } ll ans = 0; int sz = node.size(); for (int i = 0; i < (1 << sz); ++i) { ll Le = 0; int Ri = 0; for (int j = 0; j < sz; j) if ((i >> j) & 1) { Le = Le + a[node[j]].V; Ri = Ri + a[node[j]].W; } if (Ri <= L) ans = max(ans, dp[x][L - Ri] + Le); } cout << ans << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxN = (1 << 18) + 10; const int MAX = 1e5; typedef long long ll; int n, q; #define pii pair<int, int> pii a[maxN]; #define V first #define W second ll pre[maxN]; ll dp[512][MAX + 1]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // freopen("abc.inp", "r", stdin); freopen("abc.out", "w", stdout); cin >> n; for (int i = 1; i <= n; ++i) { cin >> a[i].V >> a[i].W; } const int limit = 512; for (int j = 0; j <= MAX; ++j) dp[1][j] = (a[1].W > j ? 0 : a[1].V); for (int i = 2; i < min(limit, n + 1); ++i) { int par = i / 2; for (int j = 0; j <= MAX; ++j) { dp[i][j] = dp[par][j]; if (j >= a[i].W) dp[i][j] = max(dp[i][j], dp[par][j - a[i].W] + a[i].V); } } cin >> q; while (q--) { int x, L; cin >> x >> L; vector<int> node; while (x >= limit) { node.push_back(x); x /= 2; } ll ans = 0; int sz = node.size(); for (int i = 0; i < (1 << sz); ++i) { ll Le = 0; int Ri = 0; for (int j = 0; j < sz; j++) if ((i >> j) & 1) { Le = Le + a[node[j]].V; Ri = Ri + a[node[j]].W; } if (Ri <= L) ans = max(ans, dp[x][L - Ri] + Le); } cout << ans << '\n'; } return 0; }
replace
53
54
53
54
TLE
p02648
C++
Runtime Error
#include <algorithm> #include <cfloat> #include <complex> #include <functional> #include <iostream> #include <limits.h> #include <map> #include <math.h> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <string> #include <sys/time.h> #include <unordered_map> #include <vector> #define fs first #define sc second #define endl "\n" using namespace std; typedef long long ll; typedef unsigned int uint; typedef pair<int, int> P; int main() { int n; cin >> n; vector<int> v(n + 1), w(n + 1); for (int i = 1; i <= n; i++) { cin >> v[i] >> w[i]; } int depth = 1; for (int i = 1; i < 30; i++) { if ((1 << i) - 1 >= n) { depth = i; break; } } int d2 = depth / 2; int num1 = (1 << d2) - 1; int wMax = 100000; vector<vector<int>> dp(num1 + 1, vector<int>(wMax + 1, 0)); vector<bool> state(n, true); for (int i = 1; i <= num1; i++) { state[i] = false; } dp[1][w[1]] = v[1]; for (int i = 2; i <= num1; i++) { int bef = i / 2; for (int j = 0; j <= wMax; j++) { dp[i][j] = max(dp[i][j], dp[bef][j]); if (j + w[i] <= wMax) { dp[i][j + w[i]] = max(dp[i][j + w[i]], dp[bef][j] + v[i]); } } } // i番目までの頂点で、重さj以下のうち最大の価値がdp[i][j] for (int i = 1; i <= num1; i++) { for (int j = 0; j < wMax; j++) { dp[i][j + 1] = max(dp[i][j + 1], dp[i][j]); } } int q; cin >> q; for (int l = 0; l < q; l++) { int x, L; cin >> x >> L; vector<P> wvList; wvList.emplace_back(P(0, 0)); while (state[x]) { vector<P> wvList_tmp; for (auto wv : wvList) { if (wv.fs + w[x] <= L) { wvList_tmp.emplace_back(P(wv.fs + w[x], wv.sc + v[x])); } } x /= 2; for (auto wv : wvList_tmp) { wvList.emplace_back(wv); } } int res = 0; for (auto wv : wvList) { int remain = L - wv.fs; res = max(res, wv.sc + dp[x][remain]); } cout << res << endl; } return 0; }
#include <algorithm> #include <cfloat> #include <complex> #include <functional> #include <iostream> #include <limits.h> #include <map> #include <math.h> #include <numeric> #include <queue> #include <random> #include <set> #include <stack> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <string> #include <sys/time.h> #include <unordered_map> #include <vector> #define fs first #define sc second #define endl "\n" using namespace std; typedef long long ll; typedef unsigned int uint; typedef pair<int, int> P; int main() { int n; cin >> n; vector<int> v(n + 1), w(n + 1); for (int i = 1; i <= n; i++) { cin >> v[i] >> w[i]; } if (n == 1) { int q; cin >> q; for (int i = 0; i < q; i++) { int x, L; cin >> x >> L; if (w[x] <= L) cout << v[x] << endl; else cout << 0 << endl; } return 0; } int depth = 1; for (int i = 1; i < 30; i++) { if ((1 << i) - 1 >= n) { depth = i; break; } } int d2 = depth / 2; int num1 = (1 << d2) - 1; int wMax = 100000; vector<vector<int>> dp(num1 + 1, vector<int>(wMax + 1, 0)); vector<bool> state(n, true); for (int i = 1; i <= num1; i++) { state[i] = false; } dp[1][w[1]] = v[1]; for (int i = 2; i <= num1; i++) { int bef = i / 2; for (int j = 0; j <= wMax; j++) { dp[i][j] = max(dp[i][j], dp[bef][j]); if (j + w[i] <= wMax) { dp[i][j + w[i]] = max(dp[i][j + w[i]], dp[bef][j] + v[i]); } } } // i番目までの頂点で、重さj以下のうち最大の価値がdp[i][j] for (int i = 1; i <= num1; i++) { for (int j = 0; j < wMax; j++) { dp[i][j + 1] = max(dp[i][j + 1], dp[i][j]); } } int q; cin >> q; for (int l = 0; l < q; l++) { int x, L; cin >> x >> L; vector<P> wvList; wvList.emplace_back(P(0, 0)); while (state[x]) { vector<P> wvList_tmp; for (auto wv : wvList) { if (wv.fs + w[x] <= L) { wvList_tmp.emplace_back(P(wv.fs + w[x], wv.sc + v[x])); } } x /= 2; for (auto wv : wvList_tmp) { wvList.emplace_back(wv); } } int res = 0; for (auto wv : wvList) { int remain = L - wv.fs; res = max(res, wv.sc + dp[x][remain]); } cout << res << endl; } return 0; }
insert
37
37
37
50
0
p02648
C++
Time Limit Exceeded
#include <algorithm> #include <chrono> #include <deque> #include <fstream> #include <functional> #include <iostream> #include <iterator> #include <limits> #include <map> #include <math.h> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #define forr(i, start, count) for (int i = (start); i < (start) + (count); ++i) #define set_map_includes(set, elt) (set.find((elt)) != set.end()) #define readint(i) \ int i; \ cin >> i #define readll(i) \ ll i; \ cin >> i #define readdouble(i) \ double i; \ cin >> i #define readstring(s) \ string s; \ cin >> s typedef long long ll; using namespace std; template <class T> T fastpower(T a, long long n) { // n must be >= 1 if (n % 2 == 0) { T halfpower = fastpower<T>(a, n / 2); return halfpower * halfpower; } else { if (n == 1) { return a; } else { T halfpower = fastpower<T>(a, n / 2); return (halfpower * halfpower) * a; } } }; int main() { cout.precision(17); ll modd = 1000 * 1000 * 1000 + 7; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); uniform_int_distribution<int> rand_gen( 0, modd); // rand_gen(rng) gets the rand no ll infinit = 10000000000000000; // auto start = chrono::steady_clock::now(); // readint(test_cases); int test_cases = 1; int lmax = 100001; forr(t, 1, test_cases) { readll(n); vector<int> val; vector<int> wei; forr(i, 0, n) { readint(vv); readint(ww); val.push_back(vv); wei.push_back(ww); } readint(q); vector<pair<ll, int>> qu; forr(i, 0, q) { readll(vv); readint(lll); qu.push_back(make_pair(vv, lll)); ll curr = vv; } vector<vector<ll>> memo(513, vector<ll>(lmax, 0)); forr(w, 0, lmax) { memo[1][w] = (w < wei[0] ? 0 : val[0]); } forr(v_, 1, 512) { if ((v_ > 1) && (v_ <= n)) { ll v_par = v_ / 2; forr(w, 0, lmax) { memo[v_][w] = (w < wei[v_ - 1] ? memo[v_par][w] : max(memo[v_par][w], memo[v_par][w - wei[v_ - 1]] + val[v_ - 1])); } } } for (auto q : qu) { vector<ll> verts; ll curr = q.first; while (curr > 512) { verts.push_back(curr); curr /= 2; } if (verts.empty()) { cout << memo[curr][q.second] << endl; } else { ll w_max = q.second; ll max_val = 0; forr(i, 0, fastpower<ll>(2, verts.size())) { int kk = i; ll total_w = 0, total_val = 0, i_ = 0; while (kk > 0) { if (kk % 2 == 1) { total_w += wei[verts[i_] - 1]; total_val += val[verts[i_] - 1]; } kk /= 2; i_++; } if (w_max >= total_w) { max_val = max(max_val, total_val + memo[curr][w_max - total_w]); } } cout << max_val << endl; } } } // auto end = chrono::steady_clock::now(); // cout << chrono::duration_cast<chrono::milliseconds>(end - start).count() // << endl; return 0; }
#include <algorithm> #include <chrono> #include <deque> #include <fstream> #include <functional> #include <iostream> #include <iterator> #include <limits> #include <map> #include <math.h> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #define forr(i, start, count) for (int i = (start); i < (start) + (count); ++i) #define set_map_includes(set, elt) (set.find((elt)) != set.end()) #define readint(i) \ int i; \ cin >> i #define readll(i) \ ll i; \ cin >> i #define readdouble(i) \ double i; \ cin >> i #define readstring(s) \ string s; \ cin >> s typedef long long ll; using namespace std; template <class T> T fastpower(T a, long long n) { // n must be >= 1 if (n % 2 == 0) { T halfpower = fastpower<T>(a, n / 2); return halfpower * halfpower; } else { if (n == 1) { return a; } else { T halfpower = fastpower<T>(a, n / 2); return (halfpower * halfpower) * a; } } }; int main() { cout.precision(17); ll modd = 1000 * 1000 * 1000 + 7; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); uniform_int_distribution<int> rand_gen( 0, modd); // rand_gen(rng) gets the rand no ll infinit = 10000000000000000; // auto start = chrono::steady_clock::now(); // readint(test_cases); int test_cases = 1; int lmax = 100001; forr(t, 1, test_cases) { readll(n); vector<int> val; vector<int> wei; forr(i, 0, n) { readint(vv); readint(ww); val.push_back(vv); wei.push_back(ww); } readint(q); vector<pair<ll, int>> qu; forr(i, 0, q) { readll(vv); readint(lll); qu.push_back(make_pair(vv, lll)); ll curr = vv; } vector<vector<ll>> memo(513, vector<ll>(lmax, 0)); forr(w, 0, lmax) { memo[1][w] = (w < wei[0] ? 0 : val[0]); } forr(v_, 1, 512) { if ((v_ > 1) && (v_ <= n)) { ll v_par = v_ / 2; forr(w, 0, lmax) { memo[v_][w] = (w < wei[v_ - 1] ? memo[v_par][w] : max(memo[v_par][w], memo[v_par][w - wei[v_ - 1]] + val[v_ - 1])); } } } for (auto q : qu) { vector<ll> verts; ll curr = q.first; while (curr > 512) { verts.push_back(curr); curr /= 2; } if (verts.empty()) { cout << memo[curr][q.second] << endl; } else { ll w_max = q.second; ll max_val = 0; int ppp = fastpower<ll>(2, verts.size()); forr(i, 0, ppp) { int kk = i; ll total_w = 0, total_val = 0, i_ = 0; while (kk > 0) { if (kk % 2 == 1) { total_w += wei[verts[i_] - 1]; total_val += val[verts[i_] - 1]; } kk /= 2; i_++; } if (w_max >= total_w) { max_val = max(max_val, total_val + memo[curr][w_max - total_w]); } } cout << max_val << endl; } } } // auto end = chrono::steady_clock::now(); // cout << chrono::duration_cast<chrono::milliseconds>(end - start).count() // << endl; return 0; }
replace
111
112
111
113
TLE
p02648
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define REP(i, m, n) for (int i = (m); i < (int)(n); i++) #define RREP(i, m, n) for (int i = (int)(n - 1); i >= m; i--) #define rep(i, n) REP(i, 0, n) #define rrep(i, n) RREP(i, 0, n) #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define fi first #define se second #define debug(...) \ { \ cerr << "[L" << __LINE__ << "] "; \ _debug(__VA_ARGS__); \ } template <typename T> string join(const vector<T> &v, string del = ", ") { stringstream s; for (auto x : v) s << del << x; return s.str().substr(del.size()); } template <typename T> ostream &operator<<(ostream &o, const vector<T> &v) { if (v.size()) o << "[" << join(v) << "]"; return o; } template <typename T> ostream &operator<<(ostream &o, const vector<vector<T>> &vv) { int l = vv.size(); if (l) { o << endl; rep(i, l) o << (i == 0 ? "[ " : ",\n ") << vv[i] << (i == l - 1 ? " ]" : ""); } return o; } template <typename T1, typename T2> ostream &operator<<(ostream &o, const map<T1, T2> &mp) { for (auto p : mp) { o << p.fi << ": " << p.se << ", "; } return o; } inline void _debug() { cerr << endl; } template <class First, class... Rest> void _debug(const First &first, const Rest &...rest) { cerr << first << " "; _debug(rest...); } typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<vl> vvl; const double PI = (1 * acos(0.0)); const double EPS = 1e-9; const int INF = 0x3f3f3f3f; const ll INFL = 0x3f3f3f3f3f3f3f3fLL; const ll mod = 1e9 + 7; inline void finput(string filename) { freopen(filename.c_str(), "r", stdin); } pii t[2 << 19]; int main() { ios_base::sync_with_stdio(0); // finput("./input"); int n; cin >> n; rep(i, n) { int v, w; cin >> v >> w; t[i] = pii(v, w); } int L = 100010; int K = min(n + 1, 1 << 9); vvl dp(K + 1, vl(L)); REP(i, 1, K) { int v = t[i - 1].fi, w = t[i - 1].se; int par = i >> 1; rep(j, L) { dp[i][j] = dp[par][j]; if (j - w >= 0) dp[i][j] = max(dp[i][j], dp[par][j - w] + v); } } int q; cin >> q; rep(_, q) { ll u, l; cin >> u >> l; ll ans = 0; if (u < K) { ans = dp[u][l]; } else { vi vs; while (u >= K) { vs.push_back(u); u >>= 1; } int d = vs.size(); rep(i, 1 << d) { ll v = 0, w = 0; rep(j, d) { if (i >> j & 1) { v += t[vs[j] - 1].fi; w += t[vs[j] - 1].se; } } if (l - w >= 0) ans = max(ans, dp[u][l - w] + v); } } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; #define REP(i, m, n) for (int i = (m); i < (int)(n); i++) #define RREP(i, m, n) for (int i = (int)(n - 1); i >= m; i--) #define rep(i, n) REP(i, 0, n) #define rrep(i, n) RREP(i, 0, n) #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define fi first #define se second #define debug(...) \ { \ cerr << "[L" << __LINE__ << "] "; \ _debug(__VA_ARGS__); \ } template <typename T> string join(const vector<T> &v, string del = ", ") { stringstream s; for (auto x : v) s << del << x; return s.str().substr(del.size()); } template <typename T> ostream &operator<<(ostream &o, const vector<T> &v) { if (v.size()) o << "[" << join(v) << "]"; return o; } template <typename T> ostream &operator<<(ostream &o, const vector<vector<T>> &vv) { int l = vv.size(); if (l) { o << endl; rep(i, l) o << (i == 0 ? "[ " : ",\n ") << vv[i] << (i == l - 1 ? " ]" : ""); } return o; } template <typename T1, typename T2> ostream &operator<<(ostream &o, const map<T1, T2> &mp) { for (auto p : mp) { o << p.fi << ": " << p.se << ", "; } return o; } inline void _debug() { cerr << endl; } template <class First, class... Rest> void _debug(const First &first, const Rest &...rest) { cerr << first << " "; _debug(rest...); } typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<vl> vvl; const double PI = (1 * acos(0.0)); const double EPS = 1e-9; const int INF = 0x3f3f3f3f; const ll INFL = 0x3f3f3f3f3f3f3f3fLL; const ll mod = 1e9 + 7; inline void finput(string filename) { freopen(filename.c_str(), "r", stdin); } pii t[2 << 19]; int main() { ios_base::sync_with_stdio(0); // finput("./input"); int n; cin >> n; rep(i, n) { int v, w; cin >> v >> w; t[i] = pii(v, w); } int L = 100010; int K = min(n + 1, 1 << 10); vvl dp(K + 1, vl(L)); REP(i, 1, K) { int v = t[i - 1].fi, w = t[i - 1].se; int par = i >> 1; rep(j, L) { dp[i][j] = dp[par][j]; if (j - w >= 0) dp[i][j] = max(dp[i][j], dp[par][j - w] + v); } } int q; cin >> q; rep(_, q) { ll u, l; cin >> u >> l; ll ans = 0; if (u < K) { ans = dp[u][l]; } else { vi vs; while (u >= K) { vs.push_back(u); u >>= 1; } int d = vs.size(); rep(i, 1 << d) { ll v = 0, w = 0; rep(j, d) { if (i >> j & 1) { v += t[vs[j] - 1].fi; w += t[vs[j] - 1].se; } } if (l - w >= 0) ans = max(ans, dp[u][l - w] + v); } } cout << ans << endl; } return 0; }
replace
81
82
81
82
TLE
p02648
C++
Runtime Error
#define _CRT_SECURE_NO_WARNINGS #pragma target("avx") #pragma optimize("O3") #pragma optimize("unroll-loops") #include <algorithm> #include <bitset> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <ctime> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <memory> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> #define rep(i, n) for (int i = 0; i < (lint)(n); i++) #define REP(i, n) for (int i = 1; i <= (lint)(n); i++) #define all(V) V.begin(), V.end() typedef long long lint; typedef unsigned long long ulint; typedef std::pair<lint, lint> P; constexpr int INF = INT_MAX / 2; constexpr lint LINF = LLONG_MAX / 2; constexpr double eps = DBL_EPSILON; constexpr double PI = 3.141592653589793238462643383279; template <class T> class prique : public std::priority_queue<T, std::vector<T>, std::greater<T>> { }; template <class T, class U> inline bool chmax(T &lhs, const U &rhs) { if (lhs < rhs) { lhs = rhs; return 1; } return 0; } template <class T, class U> inline bool chmin(T &lhs, const U &rhs) { if (lhs > rhs) { lhs = rhs; return 1; } return 0; } inline lint gcd(lint a, lint b) { while (b) { lint c = a; a = b; b = c % b; } return a; } inline lint lcm(lint a, lint b) { return a / gcd(a, b) * b; } bool isprime(lint n) { if (n == 1) return false; for (int i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } template <typename T> T mypow(T a, unsigned int b) { if (!b) return T(1); if (b & 1) return mypow(a, b - 1) * a; T memo = mypow(a, b >> 1); return memo * memo; } lint modpow(lint a, lint b, lint m) { if (!b) return 1; if (b & 1) return modpow(a, b - 1, m) * a % m; lint memo = modpow(a, b >> 1, m); return memo * memo % m; } template <typename T> void printArray(std::vector<T> &vec) { rep(i, vec.size() - 1) std::cout << vec[i] << " "; std::cout << vec.back() << std::endl; } template <typename T> void printArray(T l, T r) { T rprev = r; rprev--; for (T i = l; i != rprev; i++) { std::cout << *i << " "; } std::cout << *rprev << std::endl; } int n, q, v[300010], w[300010], l, mid; int table[1100][100010]; int ans; void dfs(int node, int sum, int value) { if (node < (1 << (mid + 1))) { chmax(ans, table[node - (1 << mid)][l - sum] + value); return; } if (sum + w[node] <= l) dfs(node / 2, sum + w[node], value + v[node]); dfs(node / 2, sum, value); } void dfs2(int node, int sum, int value) { if (node == 0) { chmax(ans, value); return; } if (sum + w[node] <= l) dfs(node / 2, sum + w[node], value + v[node]); dfs(node / 2, sum, value); } int main() { std::cin >> n; REP(i, n) std::cin >> v[i] >> w[i]; mid = log2(n + 1) / 2; for (int i = 1 << mid; i < (1 << (mid + 1)); i++) { int m = i - (1 << mid), node = i; while (node) { for (int j = 100000 - w[node]; j >= 0; j--) { chmax(table[m][j + w[node]], table[m][j] + v[node]); } node /= 2; } rep(j, 100000) chmax(table[m][j + 1], table[m][j]); } std::cin >> q; rep(i, q) { int u; std::cin >> u >> l; ans = 0; if (u < (1 << mid)) dfs2(u, 0, 0); else dfs(u, 0, 0); std::cout << ans << std::endl; } return 0; }
#define _CRT_SECURE_NO_WARNINGS #pragma target("avx") #pragma optimize("O3") #pragma optimize("unroll-loops") #include <algorithm> #include <bitset> #include <cassert> #include <cfloat> #include <climits> #include <cmath> #include <complex> #include <ctime> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <memory> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> #define rep(i, n) for (int i = 0; i < (lint)(n); i++) #define REP(i, n) for (int i = 1; i <= (lint)(n); i++) #define all(V) V.begin(), V.end() typedef long long lint; typedef unsigned long long ulint; typedef std::pair<lint, lint> P; constexpr int INF = INT_MAX / 2; constexpr lint LINF = LLONG_MAX / 2; constexpr double eps = DBL_EPSILON; constexpr double PI = 3.141592653589793238462643383279; template <class T> class prique : public std::priority_queue<T, std::vector<T>, std::greater<T>> { }; template <class T, class U> inline bool chmax(T &lhs, const U &rhs) { if (lhs < rhs) { lhs = rhs; return 1; } return 0; } template <class T, class U> inline bool chmin(T &lhs, const U &rhs) { if (lhs > rhs) { lhs = rhs; return 1; } return 0; } inline lint gcd(lint a, lint b) { while (b) { lint c = a; a = b; b = c % b; } return a; } inline lint lcm(lint a, lint b) { return a / gcd(a, b) * b; } bool isprime(lint n) { if (n == 1) return false; for (int i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } template <typename T> T mypow(T a, unsigned int b) { if (!b) return T(1); if (b & 1) return mypow(a, b - 1) * a; T memo = mypow(a, b >> 1); return memo * memo; } lint modpow(lint a, lint b, lint m) { if (!b) return 1; if (b & 1) return modpow(a, b - 1, m) * a % m; lint memo = modpow(a, b >> 1, m); return memo * memo % m; } template <typename T> void printArray(std::vector<T> &vec) { rep(i, vec.size() - 1) std::cout << vec[i] << " "; std::cout << vec.back() << std::endl; } template <typename T> void printArray(T l, T r) { T rprev = r; rprev--; for (T i = l; i != rprev; i++) { std::cout << *i << " "; } std::cout << *rprev << std::endl; } int n, q, v[300010], w[300010], l, mid; int table[1100][100010]; int ans; void dfs(int node, int sum, int value) { if (node < (1 << (mid + 1))) { chmax(ans, table[node - (1 << mid)][l - sum] + value); return; } if (sum + w[node] <= l) dfs(node / 2, sum + w[node], value + v[node]); dfs(node / 2, sum, value); } void dfs2(int node, int sum, int value) { if (node == 0) { chmax(ans, value); return; } if (sum + w[node] <= l) dfs2(node / 2, sum + w[node], value + v[node]); dfs2(node / 2, sum, value); } int main() { std::cin >> n; REP(i, n) std::cin >> v[i] >> w[i]; mid = log2(n + 1) / 2; for (int i = 1 << mid; i < (1 << (mid + 1)); i++) { int m = i - (1 << mid), node = i; while (node) { for (int j = 100000 - w[node]; j >= 0; j--) { chmax(table[m][j + w[node]], table[m][j] + v[node]); } node /= 2; } rep(j, 100000) chmax(table[m][j + 1], table[m][j]); } std::cin >> q; rep(i, q) { int u; std::cin >> u >> l; ans = 0; if (u < (1 << mid)) dfs2(u, 0, 0); else dfs(u, 0, 0); std::cout << ans << std::endl; } return 0; }
replace
121
123
121
123
-11
p02648
C++
Time Limit Exceeded
#include <iostream> #include <vector> using namespace std; using VP = vector<pair<int, int>>; void merge(const VP &v1, const VP &v2, VP &result) { auto it1 = v1.begin(), it2 = v2.begin(); int vmax = 0; while (it1 != v1.end() || it2 != v2.end()) { if (it1 == v1.end()) { result.push_back(*it2); it2++; } else if (it2 == v2.end()) { result.push_back(*it1); it1++; } else if (it1->first == it2->first) { result.push_back(max(*it1, *it2)); it1++; it2++; } else if (it1->first < it2->first) { result.push_back(*it1); it1++; } else { result.push_back(*it2); it2++; } vmax = max(vmax, result.back().second); result.back().second = vmax; } } vector<pair<int, int>> buf(1000); void update(const VP &prev, VP &result, int v, int w) { buf.clear(); for (auto p : prev) { int j = p.first + w; if (j <= 1e5) { buf.push_back({j, p.second + v}); } else break; } merge(prev, buf, result); } int main() { int n; cin >> n; int v[n + 1], w[n + 1]; for (int i = 1; i <= n; i++) cin >> v[i] >> w[i]; const int S = 2; vector<pair<int, int>> dp[S]; dp[1].push_back({0, 0}); dp[1].push_back({w[1], v[1]}); for (int i = 2; i <= min(n, S - 1); i++) { update(dp[i / 2], dp[i], v[i], w[i]); } // for (int i = 1; i <= min(n, S-1); i++) { // cerr << "i = " << i << " size = " << dp[i].size() << endl; // for (auto x : dp[i]) cerr << x.first << " " << x.second << endl; // } int q; cin >> q; while (q--) { int i, l; cin >> i >> l; vector<pair<int, int>> temp, save; temp.push_back({0, 0}); while (i >= S) { swap(save, temp); temp.clear(); update(save, temp, v[i], w[i]); i /= 2; } // cerr << "q = " << i << " " << "size = " << temp.size() << endl; // for (auto x : temp) cerr << x.first << " " << x.second << endl; int ans = 0; auto it1 = dp[i].begin(); auto it2 = temp.rbegin(); while (it1 != dp[i].end() && it2 != temp.rend()) { while (it2 != temp.rend() && it1->first + it2->first > l) it2++; if (it2 != temp.rend()) { ans = max(ans, it1->second + it2->second); } it1++; } cout << ans << endl; } }
#include <iostream> #include <vector> using namespace std; using VP = vector<pair<int, int>>; void merge(const VP &v1, const VP &v2, VP &result) { auto it1 = v1.begin(), it2 = v2.begin(); int vmax = 0; while (it1 != v1.end() || it2 != v2.end()) { if (it1 == v1.end()) { result.push_back(*it2); it2++; } else if (it2 == v2.end()) { result.push_back(*it1); it1++; } else if (it1->first == it2->first) { result.push_back(max(*it1, *it2)); it1++; it2++; } else if (it1->first < it2->first) { result.push_back(*it1); it1++; } else { result.push_back(*it2); it2++; } vmax = max(vmax, result.back().second); result.back().second = vmax; } } vector<pair<int, int>> buf(1000); void update(const VP &prev, VP &result, int v, int w) { buf.clear(); for (auto p : prev) { int j = p.first + w; if (j <= 1e5) { buf.push_back({j, p.second + v}); } else break; } merge(prev, buf, result); } int main() { int n; cin >> n; int v[n + 1], w[n + 1]; for (int i = 1; i <= n; i++) cin >> v[i] >> w[i]; const int S = 1024; vector<pair<int, int>> dp[S]; dp[1].push_back({0, 0}); dp[1].push_back({w[1], v[1]}); for (int i = 2; i <= min(n, S - 1); i++) { update(dp[i / 2], dp[i], v[i], w[i]); } // for (int i = 1; i <= min(n, S-1); i++) { // cerr << "i = " << i << " size = " << dp[i].size() << endl; // for (auto x : dp[i]) cerr << x.first << " " << x.second << endl; // } int q; cin >> q; while (q--) { int i, l; cin >> i >> l; vector<pair<int, int>> temp, save; temp.push_back({0, 0}); while (i >= S) { swap(save, temp); temp.clear(); update(save, temp, v[i], w[i]); i /= 2; } // cerr << "q = " << i << " " << "size = " << temp.size() << endl; // for (auto x : temp) cerr << x.first << " " << x.second << endl; int ans = 0; auto it1 = dp[i].begin(); auto it2 = temp.rbegin(); while (it1 != dp[i].end() && it2 != temp.rend()) { while (it2 != temp.rend() && it1->first + it2->first > l) it2++; if (it2 != temp.rend()) { ans = max(ans, it1->second + it2->second); } it1++; } cout << ans << endl; } }
replace
53
54
53
54
TLE
p02648
C++
Runtime Error
#include <algorithm> #include <ctime> #include <iostream> #include <utility> #include <vector> using namespace std; int main() { int N; cin >> N; vector<int> V(N + 1); vector<int> W(N + 1); for (int i = 1; i <= N; i++) cin >> V[i] >> W[i]; int Q; cin >> Q; vector<int> v(Q); vector<int> L(Q); for (int i = 0; i < Q; i++) cin >> v[i] >> L[i]; int D = 0; while ((1 << D) - 1 < N) D++; int TD = D / 2; vector<vector<int>> T(1 << TD, vector<int>(100001)); for (int i = W[1]; i <= 100000; i++) T[1][i] = V[1]; for (int i = 2; i < 1 << TD; i++) for (int j = 0; j <= 100000; j++) { T[i][j] = T[i / 2][j]; if (j >= W[i]) T[i][j] = max(T[i][j], T[i / 2][j - W[i]] + V[i]); } vector<int> W2; vector<int> V2; for (int q = 0; q < Q; q++) { int t = v[q]; W2.clear(); V2.clear(); while (t >= 1 << TD) { W2.push_back(W[t]); V2.push_back(V[t]); t /= 2; } int ans = 0; for (int b = 0; b < 1 << (int)W2.size(); b++) { int w = 0; int v = 0; for (int i = 0; i < (int)W2.size(); i++) if (b >> i & 1) { w += W2[i]; v += V2[i]; } if (w <= L[q]) ans = max(ans, v + T[t][L[q] - w]); } cout << ans << endl; } }
#include <algorithm> #include <ctime> #include <iostream> #include <utility> #include <vector> using namespace std; int main() { int N; cin >> N; vector<int> V(N + 1); vector<int> W(N + 1); for (int i = 1; i <= N; i++) cin >> V[i] >> W[i]; int Q; cin >> Q; vector<int> v(Q); vector<int> L(Q); for (int i = 0; i < Q; i++) cin >> v[i] >> L[i]; int D = 0; while ((1 << D) - 1 < N) D++; int TD = D / 2; if (TD == 0) TD = 1; vector<vector<int>> T(1 << TD, vector<int>(100001)); for (int i = W[1]; i <= 100000; i++) T[1][i] = V[1]; for (int i = 2; i < 1 << TD; i++) for (int j = 0; j <= 100000; j++) { T[i][j] = T[i / 2][j]; if (j >= W[i]) T[i][j] = max(T[i][j], T[i / 2][j - W[i]] + V[i]); } vector<int> W2; vector<int> V2; for (int q = 0; q < Q; q++) { int t = v[q]; W2.clear(); V2.clear(); while (t >= 1 << TD) { W2.push_back(W[t]); V2.push_back(V[t]); t /= 2; } int ans = 0; for (int b = 0; b < 1 << (int)W2.size(); b++) { int w = 0; int v = 0; for (int i = 0; i < (int)W2.size(); i++) if (b >> i & 1) { w += W2[i]; v += V2[i]; } if (w <= L[q]) ans = max(ans, v + T[t][L[q] - w]); } cout << ans << endl; } }
insert
26
26
26
28
0
p02648
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define ris return *this #define tmplt template <class T #define dbgo debug &operator<< tmplt > struct rge { T b, e; }; tmplt > rge<T> range(T i, T j) { return rge<T>{i, j}; } struct debug { #ifdef LOCAL ~debug() { cerr << endl; } tmplt > dbgo(T x) { cerr << boolalpha << x; ris; } tmplt, class C > dbgo(pair<T, C> x) { ris << "(" << x.first << ", " << x.second << ")"; } tmplt > dbgo(rge<T> x) { *this << "["; for (auto it = x.b; it != x.e; it++) { *this << ", " + 2 * (it == x.b) << *it; } ris << "]"; } tmplt > dbgo(vector<T> x) { ris << range(x.begin(), x.end()); } #else tmplt > dbgo(const T &) { ris; } #endif }; #define nav(...) << "[ " << #__VA_ARGS__ ": " << (__VA_ARGS__) << " ] " using ll = long long; #define forn(i, n) for (int i = 0; i < int(n); i++) #define ford(i, n) for (int i = n - 1; i >= 0; i--) template <typename T> void min_self(T &a, T b) { a = min(a, b); } template <typename T> void max_self(T &a, T b) { a = max(a, b); } void rec(vector<pair<ll, ll>> &path, vector<pair<ll, ll>> &ans, int i, int n, ll w = 0, ll v = 0) { if (i == n) { ans.emplace_back(w, v); } else { rec(path, ans, i + 1, n, w + path[i].first, v + path[i].second); rec(path, ans, i + 1, n, w, v); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<ll> value(n + 1), weight(n + 1); for (int i = 1; i <= n; i++) { cin >> value[i] >> weight[i]; } int q; cin >> q; vector<vector<pair<int, int>>> queries(n + 1); vector<ll> ret(q); for (int i = 0; i < q; i++) { int u, lim; cin >> u >> lim; queries[u].emplace_back(lim, i); } for (int u = 1; u <= n; u++) { vector<pair<ll, ll>> path, left, right; int v = u; while (v > 0) { path.emplace_back(weight[v], value[v]); v /= 2; } int m = path.size(); rec(path, left, 0, m / 2); rec(path, right, m / 2, m); sort(right.begin(), right.end()); int sz = right.size(); for (int i = 1; i < sz; i++) { max_self(right[i].second, right[i - 1].second); } sz = left.size(); for (const pair<int, int> &pp : queries[u]) { int L = pp.first, k = pp.second; for (int i = 0; i < sz; i++) { ll used = left[i].first; ll cur = left[i].second; const ll inf = 1e18; int j = upper_bound(right.begin(), right.end(), pair<ll, ll>{L - used, inf}) - right.begin(); --j; if (j < 0) { continue; } max_self(ret[k], cur + right[j].second); } } } for (int i = 0; i < q; i++) { cout << ret[i] << '\n'; } } // Author: blondie
#include <bits/stdc++.h> using namespace std; #define ris return *this #define tmplt template <class T #define dbgo debug &operator<< tmplt > struct rge { T b, e; }; tmplt > rge<T> range(T i, T j) { return rge<T>{i, j}; } struct debug { #ifdef LOCAL ~debug() { cerr << endl; } tmplt > dbgo(T x) { cerr << boolalpha << x; ris; } tmplt, class C > dbgo(pair<T, C> x) { ris << "(" << x.first << ", " << x.second << ")"; } tmplt > dbgo(rge<T> x) { *this << "["; for (auto it = x.b; it != x.e; it++) { *this << ", " + 2 * (it == x.b) << *it; } ris << "]"; } tmplt > dbgo(vector<T> x) { ris << range(x.begin(), x.end()); } #else tmplt > dbgo(const T &) { ris; } #endif }; #define nav(...) << "[ " << #__VA_ARGS__ ": " << (__VA_ARGS__) << " ] " using ll = long long; #define forn(i, n) for (int i = 0; i < int(n); i++) #define ford(i, n) for (int i = n - 1; i >= 0; i--) template <typename T> void min_self(T &a, T b) { a = min(a, b); } template <typename T> void max_self(T &a, T b) { a = max(a, b); } void rec(vector<pair<ll, ll>> &path, vector<pair<ll, ll>> &ans, int i, int n, ll w = 0, ll v = 0) { if (i == n) { ans.emplace_back(w, v); } else { rec(path, ans, i + 1, n, w + path[i].first, v + path[i].second); rec(path, ans, i + 1, n, w, v); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<ll> value(n + 1), weight(n + 1); for (int i = 1; i <= n; i++) { cin >> value[i] >> weight[i]; } int q; cin >> q; vector<vector<pair<int, int>>> queries(n + 1); vector<ll> ret(q); for (int i = 0; i < q; i++) { int u, lim; cin >> u >> lim; queries[u].emplace_back(lim, i); } for (int u = 1; u <= n; u++) { if (queries[u].empty()) { continue; } vector<pair<ll, ll>> path, left, right; int v = u; while (v > 0) { path.emplace_back(weight[v], value[v]); v /= 2; } int m = path.size(); rec(path, left, 0, m / 2); rec(path, right, m / 2, m); sort(right.begin(), right.end()); int sz = right.size(); for (int i = 1; i < sz; i++) { max_self(right[i].second, right[i - 1].second); } sz = left.size(); for (const pair<int, int> &pp : queries[u]) { int L = pp.first, k = pp.second; for (int i = 0; i < sz; i++) { ll used = left[i].first; ll cur = left[i].second; const ll inf = 1e18; int j = upper_bound(right.begin(), right.end(), pair<ll, ll>{L - used, inf}) - right.begin(); --j; if (j < 0) { continue; } max_self(ret[k], cur + right[j].second); } } } for (int i = 0; i < q; i++) { cout << ret[i] << '\n'; } } // Author: blondie
insert
71
71
71
74
TLE
p02648
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) #define ALL(v) (v).begin(), (v).end() using ll = long long; using P = pair<int, int>; constexpr int INF = 1e9; constexpr long long LINF = 1e18; constexpr long long MOD = 1e9 + 7; #define MAX 100000 ll dp[(1 << 9) + 1][2][MAX + 1]; signed main() { int n; cin >> n; int d = 0; while ((1 << d) <= n) { d++; } int V[n + 1], W[n + 1]; for (int i = 1; i <= n; i++) { cin >> V[i] >> W[i]; } int q; cin >> q; int v[q], l[q]; rep(i, q) { cin >> v[i] >> l[i]; } int h = d / 2; int depth[1 << h]; for (int i = 1; i < (1 << h); i++) { int j = 0; int cur = i; while (cur > 0) { for (int k = 0; k <= MAX; k++) { dp[i][(j + 1) % 2][k] = max(dp[i][(j + 1) % 2][k], dp[i][j % 2][k]); if (k - W[cur] < 0) continue; dp[i][(j + 1) % 2][k] = max(dp[i][(j + 1) % 2][k], dp[i][j % 2][k - W[cur]] + V[cur]); } j++; cur /= 2; } depth[i] = j % 2; } int vt[10], wt[10]; rep(i, q) { if (v[i] < (1 << h)) { cout << dp[v[i]][depth[v[i]]][l[i]] << endl; } else { int cur = v[i]; int sz = 0; while ((1 << h) <= cur) { vt[sz] = V[cur]; wt[sz] = W[cur]; cur /= 2; sz++; } ll ans = 0; for (int bit = 0; bit < (1 << sz); bit++) { ll V = 0, W = l[i]; rep(i, sz) { if (bit & (1 << i)) { V += vt[i]; W -= wt[i]; } } if (W < 0) continue; ans = max(ans, dp[cur][h % 2][W] + V); } cout << ans << endl; } } return 0; }
#pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) #define ALL(v) (v).begin(), (v).end() using ll = long long; using P = pair<int, int>; constexpr int INF = 1e9; constexpr long long LINF = 1e18; constexpr long long MOD = 1e9 + 7; #define MAX 100000 ll dp[(1 << 9) + 1][2][MAX + 1]; signed main() { int n; cin >> n; int d = 0; while ((1 << d) <= n) { d++; } int V[n + 1], W[n + 1]; for (int i = 1; i <= n; i++) { cin >> V[i] >> W[i]; } int q; cin >> q; int v[q], l[q]; rep(i, q) { cin >> v[i] >> l[i]; } int h = d / 2; int depth[1 << h]; for (int i = 1; i < (1 << h); i++) { int j = 0; int cur = i; while (cur > 0) { for (int k = 0; k <= MAX; k++) { dp[i][(j + 1) % 2][k] = max(dp[i][(j + 1) % 2][k], dp[i][j % 2][k]); if (k - W[cur] < 0) continue; dp[i][(j + 1) % 2][k] = max(dp[i][(j + 1) % 2][k], dp[i][j % 2][k - W[cur]] + V[cur]); } j++; cur /= 2; } depth[i] = j % 2; } int vt[10], wt[10]; rep(i, q) { if (v[i] < (1 << h)) { cout << dp[v[i]][depth[v[i]]][l[i]] << endl; } else { int cur = v[i]; int sz = 0; while ((1 << h) <= cur) { vt[sz] = V[cur]; wt[sz] = W[cur]; cur /= 2; sz++; } ll ans = 0; for (int bit = 0; bit < (1 << sz); bit++) { ll V = 0, W = l[i]; rep(i, sz) { if (bit & (1 << i)) { V += vt[i]; W -= wt[i]; } } if (W < 0) continue; ans = max(ans, dp[cur][h % 2][W] + V); } cout << ans << endl; } } return 0; }
insert
0
0
0
3
TLE
p02648
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define rep2(i, m, n) for (int i = (m); i < (n); ++i) #define rep(i, n) rep2(i, 0, n) #define all(a) (a).begin(), (a).end() struct fast_ios { fast_ios() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; template <typename T> inline int sz(T &x) { return x.size(); } template <typename T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <typename T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } const int INF = 1 << 30; struct item { int v, w; bool operator<(const item &r) const { return w < r.w; } void operator+=(const item &r) { w += r.w, v += r.v; } }; vector<item> enum_powerset(const vector<item> &is) { vector<item> a = {{0, 0}}; vector<item> c; c.reserve(1 << sz(is)); for (auto &i : is) { auto b = a; for (auto &e : b) e += i; merge(all(a), all(b), back_inserter(c)); swap(a, c); c.clear(); } return a; } int solve(const vector<item> &a, const vector<item> &b, int W) { int ans = 0; int vbmax = -INF; auto itr = b.begin(); for (auto &[va, wa] : a) { while (itr != b.end() && wa + itr->w <= W) { chmax(vbmax, itr->v); ++itr; } chmax(ans, va + vbmax); } return ans; } int main() { int n; cin >> n; vector<item> is(n); for (auto &[v, w] : is) cin >> v >> w; is.insert(is.begin(), {0, 0}); const int m = 10; int M = 1 << m; chmin(M, n + 1); vector<vector<item>> pre(M); pre[0] = {{0, 0}}; rep2(i, 1, M) { auto a = pre[i / 2]; for (auto &e : a) e += is[i]; merge(all(pre[i / 2]), all(a), back_inserter(pre[i])); } rep(i, M) reverse(all(pre[i])); int q; cin >> q; rep(_, q) { int v, l; cin >> v >> l; vector<item> qi; while (v > M) { qi.push_back(is[v]); v /= 2; } cout << solve(pre[v], enum_powerset(qi), l) << '\n'; } }
#include <bits/stdc++.h> using namespace std; #define rep2(i, m, n) for (int i = (m); i < (n); ++i) #define rep(i, n) rep2(i, 0, n) #define all(a) (a).begin(), (a).end() struct fast_ios { fast_ios() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; template <typename T> inline int sz(T &x) { return x.size(); } template <typename T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <typename T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } const int INF = 1 << 30; struct item { int v, w; bool operator<(const item &r) const { return w < r.w; } void operator+=(const item &r) { w += r.w, v += r.v; } }; vector<item> enum_powerset(const vector<item> &is) { vector<item> a = {{0, 0}}; vector<item> c; c.reserve(1 << sz(is)); for (auto &i : is) { auto b = a; for (auto &e : b) e += i; merge(all(a), all(b), back_inserter(c)); swap(a, c); c.clear(); } return a; } int solve(const vector<item> &a, const vector<item> &b, int W) { int ans = 0; int vbmax = -INF; auto itr = b.begin(); for (auto &[va, wa] : a) { while (itr != b.end() && wa + itr->w <= W) { chmax(vbmax, itr->v); ++itr; } chmax(ans, va + vbmax); } return ans; } int main() { int n; cin >> n; vector<item> is(n); for (auto &[v, w] : is) cin >> v >> w; is.insert(is.begin(), {0, 0}); const int m = 10; int M = 1 << m; chmin(M, n + 1); vector<vector<item>> pre(M); pre[0] = {{0, 0}}; rep2(i, 1, M) { auto a = pre[i / 2]; for (auto &e : a) e += is[i]; merge(all(pre[i / 2]), all(a), back_inserter(pre[i])); } rep(i, M) reverse(all(pre[i])); int q; cin >> q; rep(_, q) { int v, l; cin >> v >> l; vector<item> qi; while (v >= M) { qi.push_back(is[v]); v /= 2; } cout << solve(pre[v], enum_powerset(qi), l) << '\n'; } }
replace
91
92
91
92
0
p02648
C++
Time Limit Exceeded
#include <bits/stdc++.h> // ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); // clock_t start=clock();clock_t // end=clock();cout<<(double)(end-start)/CLOCKS_PER_SEC<<endl; using namespace std; typedef long long ll; typedef unsigned long long ull; typedef unsigned int ui; typedef pair<int, int> pii; typedef pair<pii, int> ppii; typedef pair<int, pii> pipi; typedef pair<ll, ll> pll; typedef pair<pll, ll> ppll; typedef pair<ll, pll> plpl; typedef pair<pii, pii> pippi; typedef tuple<ll, ll, ll> tl; typedef pair<double, double> pdd; typedef vector<vector<ll>> mat; ll mod = 1000000007; ll mod2 = 998244353; ll mod3 = 1000003; ll mod4 = 998244853; ll mod5 = 1000000009; ll inf = 1LL << 61; int iinf = 1 << 30; double pi = 3.141592653589793238462643383279L; double eps = 1e-8; #define rep(i, m, n) for (ll i = m; i < n; i++) #define rrep(i, n, m) for (ll i = n; i >= m; i--) #define srep(itr, st) for (auto itr = st.begin(); itr != st.end(); itr++) #define mrep(itr, mp) for (auto &itr : mp) #define Max(a, b) a = max(a, b) #define Min(a, b) a = min(a, b) // #define endl "\n" int dh[4] = {1, -1, 0, 0}; int dw[4] = {0, 0, 1, -1}; int ddh[8] = {-1, -1, -1, 0, 0, 1, 1, 1}; int ddw[8] = {-1, 0, 1, -1, 1, -1, 0, 1}; struct custom_hash { static uint64_t splitmix64(uint64_t x) { // http://xorshift.di.unimi.it/splitmix64.c x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; #define umh unordered_map<int, int, custom_hash> ll gcd(ll a, ll b) { if (a < b) swap(a, b); if (b == 0) return a; if (a % b == 0) return b; return gcd(b, a % b); } ll lcm(ll a, ll b) { ll c = gcd(a, b); return a * b / c; } ll Pow(ll n, ll k) { if (k < 0) return 0; ll ret = 1; ll now = n; while (k > 0) { if (k & 1) ret *= now; now *= now; k /= 2; } return ret; } ll beki(ll n, ll k, ll md) { ll ret = 1; ll now = n; now %= md; while (k > 0) { if (k % 2 == 1) { ret *= now; ret %= md; } now *= now; now %= md; k /= 2; } return ret; } ll gyaku(ll n, ll md) { return beki(n, md - 2, md); } ll popcount(ll n) { ll ret = 0; ll u = n; while (u > 0) { ret += u % 2; u /= 2; } return ret; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; ll v[n], w[n]; rep(i, 0, n) cin >> v[i] >> w[i]; if (n == 1) { ll q; cin >> q; rep(i, 0, q) { ll a, b; cin >> a >> b; if (b >= w[0]) cout << v[0] << endl; else cout << 0 << endl; } return 0; } vector<pll> vv[n]; vv[0].push_back({0, 0}); vv[1].push_back({0, 0}); vv[1].push_back({w[0], v[0]}); vector<pll> t; ll jou = pow(n, 0.75); rep(i, 2, jou + 1) { rep(j, 0, vv[i / 2].size()) { pll p = vv[i / 2][j]; vv[i].push_back(p); vv[i].push_back({p.first + w[i - 1], p.second + v[i - 1]}); } sort(vv[i].begin(), vv[i].end()); } ll q; cin >> q; vector<pll> ww; vector<pll> ne; rep(i, 0, q) { ll now, ma; cin >> now >> ma; ww.clear(); ww.push_back({0, 0}); while (now > jou) { ne.clear(); rep(j, 0, ww.size()) { ne.push_back(ww[j]); ne.push_back({ww[j].first + w[now - 1], ww[j].second + v[now - 1]}); } swap(ne, ww); now /= 2; } sort(ww.begin(), ww.end(), greater<pll>()); ll num = 0; ll maa = -inf; ll ans = -inf; rep(j, 0, ww.size()) { ll sa = ma - ww[j].first; while (num < vv[now].size() && vv[now][num].first <= sa) { Max(maa, vv[now][num].second); num++; } Max(ans, ww[j].second + maa); } cout << ans << endl; } }
#include <bits/stdc++.h> // ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); // clock_t start=clock();clock_t // end=clock();cout<<(double)(end-start)/CLOCKS_PER_SEC<<endl; using namespace std; typedef long long ll; typedef unsigned long long ull; typedef unsigned int ui; typedef pair<int, int> pii; typedef pair<pii, int> ppii; typedef pair<int, pii> pipi; typedef pair<ll, ll> pll; typedef pair<pll, ll> ppll; typedef pair<ll, pll> plpl; typedef pair<pii, pii> pippi; typedef tuple<ll, ll, ll> tl; typedef pair<double, double> pdd; typedef vector<vector<ll>> mat; ll mod = 1000000007; ll mod2 = 998244353; ll mod3 = 1000003; ll mod4 = 998244853; ll mod5 = 1000000009; ll inf = 1LL << 61; int iinf = 1 << 30; double pi = 3.141592653589793238462643383279L; double eps = 1e-8; #define rep(i, m, n) for (ll i = m; i < n; i++) #define rrep(i, n, m) for (ll i = n; i >= m; i--) #define srep(itr, st) for (auto itr = st.begin(); itr != st.end(); itr++) #define mrep(itr, mp) for (auto &itr : mp) #define Max(a, b) a = max(a, b) #define Min(a, b) a = min(a, b) // #define endl "\n" int dh[4] = {1, -1, 0, 0}; int dw[4] = {0, 0, 1, -1}; int ddh[8] = {-1, -1, -1, 0, 0, 1, 1, 1}; int ddw[8] = {-1, 0, 1, -1, 1, -1, 0, 1}; struct custom_hash { static uint64_t splitmix64(uint64_t x) { // http://xorshift.di.unimi.it/splitmix64.c x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; #define umh unordered_map<int, int, custom_hash> ll gcd(ll a, ll b) { if (a < b) swap(a, b); if (b == 0) return a; if (a % b == 0) return b; return gcd(b, a % b); } ll lcm(ll a, ll b) { ll c = gcd(a, b); return a * b / c; } ll Pow(ll n, ll k) { if (k < 0) return 0; ll ret = 1; ll now = n; while (k > 0) { if (k & 1) ret *= now; now *= now; k /= 2; } return ret; } ll beki(ll n, ll k, ll md) { ll ret = 1; ll now = n; now %= md; while (k > 0) { if (k % 2 == 1) { ret *= now; ret %= md; } now *= now; now %= md; k /= 2; } return ret; } ll gyaku(ll n, ll md) { return beki(n, md - 2, md); } ll popcount(ll n) { ll ret = 0; ll u = n; while (u > 0) { ret += u % 2; u /= 2; } return ret; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; ll v[n], w[n]; rep(i, 0, n) cin >> v[i] >> w[i]; if (n == 1) { ll q; cin >> q; rep(i, 0, q) { ll a, b; cin >> a >> b; if (b >= w[0]) cout << v[0] << endl; else cout << 0 << endl; } return 0; } vector<pll> vv[n]; vv[0].push_back({0, 0}); vv[1].push_back({0, 0}); vv[1].push_back({w[0], v[0]}); vector<pll> t; ll jou = pow(n, 0.6); rep(i, 2, jou + 1) { rep(j, 0, vv[i / 2].size()) { pll p = vv[i / 2][j]; vv[i].push_back(p); vv[i].push_back({p.first + w[i - 1], p.second + v[i - 1]}); } sort(vv[i].begin(), vv[i].end()); } ll q; cin >> q; vector<pll> ww; vector<pll> ne; rep(i, 0, q) { ll now, ma; cin >> now >> ma; ww.clear(); ww.push_back({0, 0}); while (now > jou) { ne.clear(); rep(j, 0, ww.size()) { ne.push_back(ww[j]); ne.push_back({ww[j].first + w[now - 1], ww[j].second + v[now - 1]}); } swap(ne, ww); now /= 2; } sort(ww.begin(), ww.end(), greater<pll>()); ll num = 0; ll maa = -inf; ll ans = -inf; rep(j, 0, ww.size()) { ll sa = ma - ww[j].first; while (num < vv[now].size() && vv[now][num].first <= sa) { Max(maa, vv[now][num].second); num++; } Max(ans, ww[j].second + maa); } cout << ans << endl; } }
replace
132
133
132
133
TLE
p02648
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rng(i, a, b) for (int i = int(a); i < int(b); i++) #define rep(i, b) rng(i, 0, b) #define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--) #define per(i, b) gnr(i, 0, b) #define pb push_back #define eb emplace_back #define bg begin() #define ed end() #define all(x) x.bg, x.ed const ll MOD = 1e9 + 7; int main() { int n; cin >> n; vector<pair<int, int>> nodes(n + 1), queries; rep(i, n) { int v, w; cin >> v >> w; nodes[i + 1] = {v, w}; } int q; cin >> q; int lmax = 0; rep(i, q) { int v, l; cin >> v >> l; lmax = max(l, lmax); queries.emplace_back(v, l); } const int n1 = (1 << 9) - 1; vector<vector<int>> dps(n1 + 1, vector<int>(lmax + 1, 0)); rng(i, 1, min(n1, n) + 1) { int v = nodes[i].first; int w = nodes[i].second; rep(k, lmax + 1) { dps[i][k] = dps[i / 2][k]; if (k >= w) dps[i][k] = max(dps[i][k], dps[i / 2][k - w] + v); } } vector<int> ans(q, 0); rep(i, q) { int v = queries[i].first; int l = queries[i].second; if (n1 < v) { int lv = v, cnt = 0; while (lv > n1) { lv /= 2; cnt++; } rep(j, 1 << cnt) { int b = j, ltv = v; int tv = 0, tw = 0; rep(k, cnt) { if (b & 1) { tv += nodes[ltv].first; tw += nodes[ltv].second; } b >>= 1; ltv /= 2; } if (tw <= l) ans[i] = max(ans[i], tv + dps[lv][l - tw]); } } else { ans[i] = dps[v][l]; } } for (int a : ans) cout << a << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rng(i, a, b) for (int i = int(a); i < int(b); i++) #define rep(i, b) rng(i, 0, b) #define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--) #define per(i, b) gnr(i, 0, b) #define pb push_back #define eb emplace_back #define bg begin() #define ed end() #define all(x) x.bg, x.ed const ll MOD = 1e9 + 7; int main() { int n; cin >> n; vector<pair<int, int>> nodes(n + 1), queries; rep(i, n) { int v, w; cin >> v >> w; nodes[i + 1] = {v, w}; } int q; cin >> q; int lmax = 0; rep(i, q) { int v, l; cin >> v >> l; lmax = max(l, lmax); queries.emplace_back(v, l); } const int n1 = (1 << 10) - 1; vector<vector<int>> dps(n1 + 1, vector<int>(lmax + 1, 0)); rng(i, 1, min(n1, n) + 1) { int v = nodes[i].first; int w = nodes[i].second; rep(k, lmax + 1) { dps[i][k] = dps[i / 2][k]; if (k >= w) dps[i][k] = max(dps[i][k], dps[i / 2][k - w] + v); } } vector<int> ans(q, 0); rep(i, q) { int v = queries[i].first; int l = queries[i].second; if (n1 < v) { int lv = v, cnt = 0; while (lv > n1) { lv /= 2; cnt++; } rep(j, 1 << cnt) { int b = j, ltv = v; int tv = 0, tw = 0; rep(k, cnt) { if (b & 1) { tv += nodes[ltv].first; tw += nodes[ltv].second; } b >>= 1; ltv /= 2; } if (tw <= l) ans[i] = max(ans[i], tv + dps[lv][l - tw]); } } else { ans[i] = dps[v][l]; } } for (int a : ans) cout << a << endl; return 0; }
replace
34
35
34
35
TLE
p02648
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define fi first #define se second #define rep(i, n) for (int i = 0; i < (n); ++i) #define drep(i, n) for (int i = (n)-1; i >= 0; --i) #define srep(i, s, t) for (int i = s; i < t; ++i) #define rng(a) a.begin(), a.end() #define sz(x) (int)(x).size() #define uni(x) x.erase(unique(rng(x)), x.end()) #define show(x) cout << #x << " = " << x << endl; #define PQ(T) priority_queue<T, v(T), greater<T>> #define newline puts("") #define v(T) vector<T> #define vv(T) v(v(T)) #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) #define EPS (1e-10) #define equals(a, b) (fabs((a) - (b)) < EPS) using namespace std; typedef long long int ll; typedef pair<int, int> P; typedef set<int> S; typedef queue<int> Q; typedef queue<P> QP; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<P> vp; const ll LINF = 1001002003004005006ll; const int INF = 1001001001; const int MOD = 1000000007; int N; vi tv, tw; int q; vp qq; int border; v(vl) dp; int depth(int k) { if (k == 0) return 1; return depth((k + 1) / 2 - 1) + 1; } int main() { cin >> N; tv.resize(N); tw.resize(N); rep(i, N) { cin >> tv[i] >> tw[i]; } int L = 0; cin >> q; qq.resize(q); rep(qi, q) { cin >> qq[qi].first >> qq[qi].second; chmax(L, qq[qi].second + 1); } const int X = 9; border = min(1 << (X + 1) - 1, N); dp.assign(border, vl(L, 0)); rep(i, L) { if (i - tw[0] < 0) continue; dp[0][i] = tv[0]; } srep(i, 1, border) { int p = (i + 1) / 2 - 1; dp[i] = dp[p]; drep(j, L) { if (j - tw[i] < 0) continue; chmax(dp[i][j], dp[i][j - tw[i]] + tv[i]); } } rep(qi, q) { int k = depth(qq[qi].first - 1); if (k <= X) { cout << dp[qq[qi].first - 1][qq[qi].second] << endl; continue; } ll ans = 0; rep(b, 1 << (k - X)) { ll sw = 0, sv = 0; int now = qq[qi].first - 1; rep(i, k - X) { if ((b >> i) & 1) { sw += tw[now]; sv += tv[now]; } now = (now + 1) / 2 - 1; } if (sw > qq[qi].second) continue; int rem = qq[qi].second - sw; chmax(ans, sv + dp[now][rem]); } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> #define fi first #define se second #define rep(i, n) for (int i = 0; i < (n); ++i) #define drep(i, n) for (int i = (n)-1; i >= 0; --i) #define srep(i, s, t) for (int i = s; i < t; ++i) #define rng(a) a.begin(), a.end() #define sz(x) (int)(x).size() #define uni(x) x.erase(unique(rng(x)), x.end()) #define show(x) cout << #x << " = " << x << endl; #define PQ(T) priority_queue<T, v(T), greater<T>> #define newline puts("") #define v(T) vector<T> #define vv(T) v(v(T)) #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) #define EPS (1e-10) #define equals(a, b) (fabs((a) - (b)) < EPS) using namespace std; typedef long long int ll; typedef pair<int, int> P; typedef set<int> S; typedef queue<int> Q; typedef queue<P> QP; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vl; typedef vector<P> vp; const ll LINF = 1001002003004005006ll; const int INF = 1001001001; const int MOD = 1000000007; int N; vi tv, tw; int q; vp qq; int border; v(vl) dp; int depth(int k) { if (k == 0) return 1; return depth((k + 1) / 2 - 1) + 1; } int main() { cin >> N; tv.resize(N); tw.resize(N); rep(i, N) { cin >> tv[i] >> tw[i]; } int L = 0; cin >> q; qq.resize(q); rep(qi, q) { cin >> qq[qi].first >> qq[qi].second; chmax(L, qq[qi].second + 1); } const int X = 10; border = min(1 << (X + 1) - 1, N); dp.assign(border, vl(L, 0)); rep(i, L) { if (i - tw[0] < 0) continue; dp[0][i] = tv[0]; } srep(i, 1, border) { int p = (i + 1) / 2 - 1; dp[i] = dp[p]; drep(j, L) { if (j - tw[i] < 0) continue; chmax(dp[i][j], dp[i][j - tw[i]] + tv[i]); } } rep(qi, q) { int k = depth(qq[qi].first - 1); if (k <= X) { cout << dp[qq[qi].first - 1][qq[qi].second] << endl; continue; } ll ans = 0; rep(b, 1 << (k - X)) { ll sw = 0, sv = 0; int now = qq[qi].first - 1; rep(i, k - X) { if ((b >> i) & 1) { sw += tw[now]; sv += tv[now]; } now = (now + 1) / 2 - 1; } if (sw > qq[qi].second) continue; int rem = qq[qi].second - sw; chmax(ans, sv + dp[now][rem]); } cout << ans << endl; } return 0; }
replace
57
58
57
58
TLE
p02648
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; #define sz(x) (int)((x).size()) #define forn(i, n) for (int i = 0; i < n; i++) #define forse(i, s, e) for (int i = s; i < e; i++) #define pb push_back #define fi first #define se second #define all(vec) vec.begin(), vec.end() #define time() ((double)clock() / CLOCKS_PER_SEC) typedef unsigned long long ull; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; // Debug string to_string(const string &s) { return '"' + s + '"'; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.fi) + ", " + to_string(p.se) + ")"; } template <typename A> string to_string(A v) { if (v.begin() == v.end()) return ""; return accumulate( ++v.begin(), v.end(), "{" + to_string(*v.begin()), [](const string &a, auto b) { return a + ", " + to_string(b); }) + "}"; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) false #endif double PI = 3.141592653589793; const int mx = 1 << 18, mvn = 1e5; map<int, int> proc[mx]; int va[mx], w[mx]; void solve() { int n, q; cin >> n; int bs = 9; forse(i, 1, n + 1) cin >> va[i] >> w[i]; forse(i, 1, n + 1) { if (i >= (1 << bs)) break; map<int, int> cur; if (i == 1) { cur[0] = 0; cur[w[i]] = va[i]; proc[i] = cur; continue; } cur = proc[i / 2]; for (auto v : proc[i / 2]) { int vn = v.fi + w[i], val = v.se + va[i]; if (vn <= mvn) cur[vn] = max(cur[vn], val); } int bval = -1; for (auto v : cur) { if (v.se <= bval) continue; bval = v.se; proc[i][v.fi] = v.se; } } // forse(i,1,n+1){ // debug(i,proc[i]); // } cin >> q; while (q--) { int v, l; cin >> v >> l; int pr = v, out = 0; vector<pii> pro; while (pr >= (1 << bs)) { pro.pb({w[pr], va[pr]}); pr /= 2; } forn(ind, (1 << sz(pro))) { int vsm = 0, wsm = 0; forn(j, sz(pro)) { if (ind & (1 << j)) { vsm += pro[j].se; wsm += pro[j].fi; } } if (l < wsm) continue; out = max(out, vsm + (--proc[pr].upper_bound(l - wsm))->se); } cout << out << '\n'; } } int main() { ios::sync_with_stdio(0); cin.tie(0); int T = 1; // cin >> T; forse(c, 1, T + 1) { // cout << "Case #" << cid << ": "; solve(); } }
#include <bits/stdc++.h> using namespace std; #define sz(x) (int)((x).size()) #define forn(i, n) for (int i = 0; i < n; i++) #define forse(i, s, e) for (int i = s; i < e; i++) #define pb push_back #define fi first #define se second #define all(vec) vec.begin(), vec.end() #define time() ((double)clock() / CLOCKS_PER_SEC) typedef unsigned long long ull; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; // Debug string to_string(const string &s) { return '"' + s + '"'; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.fi) + ", " + to_string(p.se) + ")"; } template <typename A> string to_string(A v) { if (v.begin() == v.end()) return ""; return accumulate( ++v.begin(), v.end(), "{" + to_string(*v.begin()), [](const string &a, auto b) { return a + ", " + to_string(b); }) + "}"; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) false #endif double PI = 3.141592653589793; const int mx = 1 << 18, mvn = 1e5; map<int, int> proc[mx]; int va[mx], w[mx]; void solve() { int n, q; cin >> n; int bs = 10; forse(i, 1, n + 1) cin >> va[i] >> w[i]; forse(i, 1, n + 1) { if (i >= (1 << bs)) break; map<int, int> cur; if (i == 1) { cur[0] = 0; cur[w[i]] = va[i]; proc[i] = cur; continue; } cur = proc[i / 2]; for (auto v : proc[i / 2]) { int vn = v.fi + w[i], val = v.se + va[i]; if (vn <= mvn) cur[vn] = max(cur[vn], val); } int bval = -1; for (auto v : cur) { if (v.se <= bval) continue; bval = v.se; proc[i][v.fi] = v.se; } } // forse(i,1,n+1){ // debug(i,proc[i]); // } cin >> q; while (q--) { int v, l; cin >> v >> l; int pr = v, out = 0; vector<pii> pro; while (pr >= (1 << bs)) { pro.pb({w[pr], va[pr]}); pr /= 2; } forn(ind, (1 << sz(pro))) { int vsm = 0, wsm = 0; forn(j, sz(pro)) { if (ind & (1 << j)) { vsm += pro[j].se; wsm += pro[j].fi; } } if (l < wsm) continue; out = max(out, vsm + (--proc[pr].upper_bound(l - wsm))->se); } cout << out << '\n'; } } int main() { ios::sync_with_stdio(0); cin.tie(0); int T = 1; // cin >> T; forse(c, 1, T + 1) { // cout << "Case #" << cid << ": "; solve(); } }
replace
53
54
53
54
TLE
p02648
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; using Graph = vector<vector<int>>; using WGraph = vector<vector<pair<int, ll>>>; template <class T> inline bool chmax(T &a, const T &b) { if (b > a) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; } constexpr int dx[4] = {-1, 0, 0, 1}, dy[4] = {0, -1, 1, 0}; constexpr int INF = 1e9; constexpr int MOD = 1e9 + 7; constexpr long long LINF = 1e18; constexpr double EPS = 1e-10; constexpr double PI = M_PI; void solve() { int n; cin >> n; vector<int> v(n + 1), w(n + 1); for (int i = 1; i < n + 1; ++i) cin >> v[i] >> w[i]; int m = 1; while (m < n / 2) { m *= 2; } assert(m < (1 << 9)); assert(m < n); vector<vector<int>> dp(m, vector<int>(100001)); for (int i = 1; i < m; ++i) { int par = i / 2; for (int j = 0; j < 100001; ++j) { dp[i][j] = dp[par][j]; if (j >= w[i]) chmax(dp[i][j], dp[par][j - w[i]] + v[i]); } } int q; cin >> q; for (int i = 0; i < q; ++i) { int V, l; cin >> V >> l; vector<int> vec; while (V >= m) { vec.push_back(V); V /= 2; } int nn = (int)vec.size(); int res = 0; for (int bits = 0; bits < (1 << nn); ++bits) { int cv = 0, cw = 0; for (int j = 0; j < nn; ++j) { if (bits & (1 << j)) { cv += v[vec[j]]; cw += w[vec[j]]; } } if (l - cw >= 0) chmax(res, cv + dp[V][l - cw]); } cout << res << '\n'; } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(20); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using Graph = vector<vector<int>>; using WGraph = vector<vector<pair<int, ll>>>; template <class T> inline bool chmax(T &a, const T &b) { if (b > a) { a = b; return true; } return false; } template <class T> inline bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; } constexpr int dx[4] = {-1, 0, 0, 1}, dy[4] = {0, -1, 1, 0}; constexpr int INF = 1e9; constexpr int MOD = 1e9 + 7; constexpr long long LINF = 1e18; constexpr double EPS = 1e-10; constexpr double PI = M_PI; void solve() { int n; cin >> n; vector<int> v(n + 1), w(n + 1); for (int i = 1; i < n + 1; ++i) cin >> v[i] >> w[i]; int m = min((1 << 9), n + 1); vector<vector<int>> dp(m, vector<int>(100001)); for (int i = 1; i < m; ++i) { int par = i / 2; for (int j = 0; j < 100001; ++j) { dp[i][j] = dp[par][j]; if (j >= w[i]) chmax(dp[i][j], dp[par][j - w[i]] + v[i]); } } int q; cin >> q; for (int i = 0; i < q; ++i) { int V, l; cin >> V >> l; vector<int> vec; while (V >= m) { vec.push_back(V); V /= 2; } int nn = (int)vec.size(); int res = 0; for (int bits = 0; bits < (1 << nn); ++bits) { int cv = 0, cw = 0; for (int j = 0; j < nn; ++j) { if (bits & (1 << j)) { cv += v[vec[j]]; cw += w[vec[j]]; } } if (l - cw >= 0) chmax(res, cv + dp[V][l - cw]); } cout << res << '\n'; } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(20); solve(); return 0; }
replace
36
42
36
37
0
p02648
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 ll long long #define ull unsigned long long #define db long double #define pb push_back #define ppb pop_back #define F first #define S second #define mp make_pair #define all(x) (x).begin(), (x).end() mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); typedef array<ll, 2> pii; typedef vector<int> vi; typedef tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; const int K = 3; const int N = (1 << 18) + 123, M = 1e5 + 123; int n, val[N], w[N], q, h[N], b[N]; int mv[(1 << 9) + 1], mw[(1 << 9) + 1]; int cnt, ind[N], opt[(1 << 9) + 1][M]; int main() { ios_base::sync_with_stdio(false), cin.tie(NULL); #ifdef LOCAL freopen("input.txt", "r", stdin); #endif for (int i = 1; i < (1 << 9); i++) { for (int j = 0; j < 9; j++) { if (i >> j & 1) { b[i] = j; break; } } } cin >> n; for (int i = 1; i <= n; i++) { cin >> val[i] >> w[i]; } vi vec; for (int i = 1; i <= n; i++) { h[i] = h[i / 2] + 1; if (h[i] == K) { int v = i; vec.clear(); while (v) { vec.pb(v); v /= 2; } int s = vec.size(); ind[i] = cnt++; int k = ind[i]; for (int j = 0; j < (1 << s); j++) { if (j > 0) { int el = vec[b[j]]; mw[j] = mw[j ^ (1 << b[j])] + w[el]; mv[j] = mv[j ^ (1 << b[j])] + val[el]; } if (mw[j] < M) { opt[k][mw[j]] = max(opt[k][mw[j]], mv[j]); } } for (int j = 1; j < M; j++) { opt[k][j] = max(opt[k][j], opt[k][j - 1]); } } } cin >> q; while (q--) { int V, L; cin >> V >> L; if (h[V] <= K) { vec.clear(); while (V) { vec.pb(V); V /= 2; } int s = vec.size(), res = 0; for (int i = 0; i < (1 << s); i++) { if (i > 0) { int el = vec[b[i]]; mw[i] = mw[i ^ (1 << b[i])] + w[el]; mv[i] = mv[i ^ (1 << b[i])] + val[el]; } if (mw[i] <= L) { res = max(res, mv[i]); } } cout << res << '\n'; } else { vec.clear(); while (h[V] > K) { vec.pb(V); V /= 2; } int k = ind[V]; int s = vec.size(), res = 0; for (int i = 0; i < (1 << s); i++) { if (i > 0) { int el = vec[b[i]]; mw[i] = mw[i ^ (1 << b[i])] + w[el]; mv[i] = mv[i ^ (1 << b[i])] + val[el]; } if (mw[i] <= L) { res = max(res, mv[i] + opt[k][L - mw[i]]); } } cout << res << '\n'; } } }
#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 ll long long #define ull unsigned long long #define db long double #define pb push_back #define ppb pop_back #define F first #define S second #define mp make_pair #define all(x) (x).begin(), (x).end() mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); typedef array<ll, 2> pii; typedef vector<int> vi; typedef tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; const int K = 9; const int N = (1 << 18) + 123, M = 1e5 + 123; int n, val[N], w[N], q, h[N], b[N]; int mv[(1 << 9) + 1], mw[(1 << 9) + 1]; int cnt, ind[N], opt[(1 << 9) + 1][M]; int main() { ios_base::sync_with_stdio(false), cin.tie(NULL); #ifdef LOCAL freopen("input.txt", "r", stdin); #endif for (int i = 1; i < (1 << 9); i++) { for (int j = 0; j < 9; j++) { if (i >> j & 1) { b[i] = j; break; } } } cin >> n; for (int i = 1; i <= n; i++) { cin >> val[i] >> w[i]; } vi vec; for (int i = 1; i <= n; i++) { h[i] = h[i / 2] + 1; if (h[i] == K) { int v = i; vec.clear(); while (v) { vec.pb(v); v /= 2; } int s = vec.size(); ind[i] = cnt++; int k = ind[i]; for (int j = 0; j < (1 << s); j++) { if (j > 0) { int el = vec[b[j]]; mw[j] = mw[j ^ (1 << b[j])] + w[el]; mv[j] = mv[j ^ (1 << b[j])] + val[el]; } if (mw[j] < M) { opt[k][mw[j]] = max(opt[k][mw[j]], mv[j]); } } for (int j = 1; j < M; j++) { opt[k][j] = max(opt[k][j], opt[k][j - 1]); } } } cin >> q; while (q--) { int V, L; cin >> V >> L; if (h[V] <= K) { vec.clear(); while (V) { vec.pb(V); V /= 2; } int s = vec.size(), res = 0; for (int i = 0; i < (1 << s); i++) { if (i > 0) { int el = vec[b[i]]; mw[i] = mw[i ^ (1 << b[i])] + w[el]; mv[i] = mv[i ^ (1 << b[i])] + val[el]; } if (mw[i] <= L) { res = max(res, mv[i]); } } cout << res << '\n'; } else { vec.clear(); while (h[V] > K) { vec.pb(V); V /= 2; } int k = ind[V]; int s = vec.size(), res = 0; for (int i = 0; i < (1 << s); i++) { if (i > 0) { int el = vec[b[i]]; mw[i] = mw[i ^ (1 << b[i])] + w[el]; mv[i] = mv[i ^ (1 << b[i])] + val[el]; } if (mw[i] <= L) { res = max(res, mv[i] + opt[k][L - mw[i]]); } } cout << res << '\n'; } } }
replace
24
25
24
25
-11
p02648
C++
Time Limit Exceeded
#define _CRT_SECURE_NO_WARNINGS #include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cstring> #include <float.h> #include <fstream> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <string> #include <time.h> #include <utility> #include <vector> using namespace std; long long gcd(long long a, long long b) { if (b == 0) return a; else return gcd(b, a % b); } template <typename Monoid, typename OperatorMonoid = Monoid> struct LazySegmentTree { using F = function<Monoid(Monoid, Monoid)>; using G = function<Monoid(Monoid, OperatorMonoid)>; using H = function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>; using P = function<OperatorMonoid(OperatorMonoid, int)>; int sz; vector<Monoid> data; vector<OperatorMonoid> lazy; const F f; const G g; const H h; const P p; const Monoid M1; const OperatorMonoid OM0; LazySegmentTree(int n, const F f, const G g, const H h, const P p, const Monoid &M1, const OperatorMonoid OM0) : f(f), g(g), h(h), p(p), M1(M1), OM0(OM0) { sz = 1; while (sz < n) sz <<= 1; data.assign(2 * sz, M1); lazy.assign(2 * sz, OM0); } void set(int k, const Monoid &x) { data[k + sz] = x; } void build() { for (int k = sz - 1; k > 0; k--) { data[k] = f(data[2 * k + 0], data[2 * k + 1]); } } void propagate(int k, int len) { if (lazy[k] != OM0) { if (k < sz) { lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]); lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]); } data[k] = g(data[k], p(lazy[k], len)); lazy[k] = OM0; } } Monoid update(int a, int b, const OperatorMonoid &x, int k, int l, int r) { propagate(k, r - l); if (r <= a || b <= l) { return data[k]; } else if (a <= l && r <= b) { lazy[k] = h(lazy[k], x); propagate(k, r - l); return data[k]; } else { return data[k] = f(update(a, b, x, 2 * k + 0, l, (l + r) >> 1), update(a, b, x, 2 * k + 1, (l + r) >> 1, r)); } } Monoid update(int a, int b, const OperatorMonoid &x) { return update(a, b, x, 1, 0, sz); } Monoid query(int a, int b, int k, int l, int r) { propagate(k, r - l); if (r <= a || b <= l) { return M1; } else if (a <= l && r <= b) { return data[k]; } else { return f(query(a, b, 2 * k + 0, l, (l + r) >> 1), query(a, b, 2 * k + 1, (l + r) >> 1, r)); } } Monoid query(int a, int b) { return query(a, b, 1, 0, sz); } Monoid operator[](const int &k) { return query(k, k + 1); } }; map<int64_t, int> prime_factor(int64_t n) { map<int64_t, int> ret; for (int64_t i = 2; i * i <= n; i++) { while (n % i == 0) { ret[i]++; n /= i; } } if (n != 1) ret[n] = 1; return ret; } long long MOD = 998244353; const int MAX = 510000; long long fac[MAX], finv[MAX], inv[MAX]; // テーブルを作る前処理 void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 long long COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } template <typename T> struct BinaryIndexedTree { vector<T> data; BinaryIndexedTree(int sz) { data.assign(++sz, 0); } T sum(int k) { T ret = 0; for (++k; k > 0; k -= k & -k) ret += data[k]; return (ret); } void add(int k, T x) { for (++k; k < data.size(); k += k & -k) data[k] += x; } }; typedef pair<int, int> p; signed main() { cin.tie(0); ios::sync_with_stdio(false); int maxc = 13; int n; cin >> n; int v[300004]; int w[300004]; for (int i = 1; i <= n; i++) { cin >> v[i] >> w[i]; } map<int, int> ma[3000004]; // i番目の頂点の重さx以下のときの最大価値 ma[1][0] = 0; ma[1][w[1]] = v[1]; for (int i = 2; i <= min(n, (1 << maxc) - 1); i++) { int from = i / 2; for (auto itr = ma[from].begin(); itr != ma[from].end(); itr++) { int weight = itr->first; int value = itr->second; if (ma[i].find(weight) != ma[i].end()) { ma[i][weight] = max(ma[i][weight], value); } else { ma[i][weight] = value; } if (ma[i].find(weight + w[i]) != ma[i].end()) { ma[i][weight + w[i]] = max(ma[i][weight + w[i]], value + v[i]); } else { ma[i][weight + w[i]] = value + v[i]; } } int now = 0; for (auto itr = ma[i].begin(); itr != ma[i].end(); itr++) { now = max(now, itr->second); itr->second = now; } } int q; cin >> q; while (q--) { int vertex, l; cin >> vertex >> l; if (vertex < (1 << maxc)) { auto itr = ma[vertex].upper_bound(l); itr--; cout << (itr->second); } else { vector<p> vec; // 重さ、価値 int vv = vertex; while (vv >= (1 << maxc)) { vec.push_back(p(w[vv], v[vv])); vv /= 2; } int ans = 0; for (int i = 0; i < (1 << vec.size()); i++) { int weight = 0; int value = 0; for (int x = 0; x < vec.size(); x++) { if ((i >> x) & 1) { weight += vec[x].first; value += vec[x].second; } } if (weight <= l) { auto itr = ma[vv].upper_bound(l - weight); itr--; ans = max(ans, value + itr->second); } } cout << ans; } cout << "\n"; } }
#define _CRT_SECURE_NO_WARNINGS #include <algorithm> #include <bitset> #include <cassert> #include <climits> #include <cstring> #include <float.h> #include <fstream> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdio.h> #include <string> #include <time.h> #include <utility> #include <vector> using namespace std; long long gcd(long long a, long long b) { if (b == 0) return a; else return gcd(b, a % b); } template <typename Monoid, typename OperatorMonoid = Monoid> struct LazySegmentTree { using F = function<Monoid(Monoid, Monoid)>; using G = function<Monoid(Monoid, OperatorMonoid)>; using H = function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>; using P = function<OperatorMonoid(OperatorMonoid, int)>; int sz; vector<Monoid> data; vector<OperatorMonoid> lazy; const F f; const G g; const H h; const P p; const Monoid M1; const OperatorMonoid OM0; LazySegmentTree(int n, const F f, const G g, const H h, const P p, const Monoid &M1, const OperatorMonoid OM0) : f(f), g(g), h(h), p(p), M1(M1), OM0(OM0) { sz = 1; while (sz < n) sz <<= 1; data.assign(2 * sz, M1); lazy.assign(2 * sz, OM0); } void set(int k, const Monoid &x) { data[k + sz] = x; } void build() { for (int k = sz - 1; k > 0; k--) { data[k] = f(data[2 * k + 0], data[2 * k + 1]); } } void propagate(int k, int len) { if (lazy[k] != OM0) { if (k < sz) { lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]); lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]); } data[k] = g(data[k], p(lazy[k], len)); lazy[k] = OM0; } } Monoid update(int a, int b, const OperatorMonoid &x, int k, int l, int r) { propagate(k, r - l); if (r <= a || b <= l) { return data[k]; } else if (a <= l && r <= b) { lazy[k] = h(lazy[k], x); propagate(k, r - l); return data[k]; } else { return data[k] = f(update(a, b, x, 2 * k + 0, l, (l + r) >> 1), update(a, b, x, 2 * k + 1, (l + r) >> 1, r)); } } Monoid update(int a, int b, const OperatorMonoid &x) { return update(a, b, x, 1, 0, sz); } Monoid query(int a, int b, int k, int l, int r) { propagate(k, r - l); if (r <= a || b <= l) { return M1; } else if (a <= l && r <= b) { return data[k]; } else { return f(query(a, b, 2 * k + 0, l, (l + r) >> 1), query(a, b, 2 * k + 1, (l + r) >> 1, r)); } } Monoid query(int a, int b) { return query(a, b, 1, 0, sz); } Monoid operator[](const int &k) { return query(k, k + 1); } }; map<int64_t, int> prime_factor(int64_t n) { map<int64_t, int> ret; for (int64_t i = 2; i * i <= n; i++) { while (n % i == 0) { ret[i]++; n /= i; } } if (n != 1) ret[n] = 1; return ret; } long long MOD = 998244353; const int MAX = 510000; long long fac[MAX], finv[MAX], inv[MAX]; // テーブルを作る前処理 void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % MOD; inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD; finv[i] = finv[i - 1] * inv[i] % MOD; } } // 二項係数計算 long long COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } template <typename T> struct BinaryIndexedTree { vector<T> data; BinaryIndexedTree(int sz) { data.assign(++sz, 0); } T sum(int k) { T ret = 0; for (++k; k > 0; k -= k & -k) ret += data[k]; return (ret); } void add(int k, T x) { for (++k; k < data.size(); k += k & -k) data[k] += x; } }; typedef pair<int, int> p; signed main() { cin.tie(0); ios::sync_with_stdio(false); int maxc = 11; int n; cin >> n; int v[300004]; int w[300004]; for (int i = 1; i <= n; i++) { cin >> v[i] >> w[i]; } map<int, int> ma[3000004]; // i番目の頂点の重さx以下のときの最大価値 ma[1][0] = 0; ma[1][w[1]] = v[1]; for (int i = 2; i <= min(n, (1 << maxc) - 1); i++) { int from = i / 2; for (auto itr = ma[from].begin(); itr != ma[from].end(); itr++) { int weight = itr->first; int value = itr->second; if (ma[i].find(weight) != ma[i].end()) { ma[i][weight] = max(ma[i][weight], value); } else { ma[i][weight] = value; } if (ma[i].find(weight + w[i]) != ma[i].end()) { ma[i][weight + w[i]] = max(ma[i][weight + w[i]], value + v[i]); } else { ma[i][weight + w[i]] = value + v[i]; } } int now = 0; for (auto itr = ma[i].begin(); itr != ma[i].end(); itr++) { now = max(now, itr->second); itr->second = now; } } int q; cin >> q; while (q--) { int vertex, l; cin >> vertex >> l; if (vertex < (1 << maxc)) { auto itr = ma[vertex].upper_bound(l); itr--; cout << (itr->second); } else { vector<p> vec; // 重さ、価値 int vv = vertex; while (vv >= (1 << maxc)) { vec.push_back(p(w[vv], v[vv])); vv /= 2; } int ans = 0; for (int i = 0; i < (1 << vec.size()); i++) { int weight = 0; int value = 0; for (int x = 0; x < vec.size(); x++) { if ((i >> x) & 1) { weight += vec[x].first; value += vec[x].second; } } if (weight <= l) { auto itr = ma[vv].upper_bound(l - weight); itr--; ans = max(ans, value + itr->second); } } cout << ans; } cout << "\n"; } }
replace
168
169
168
169
TLE
p02648
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; using uint = unsigned int; #define rep(i, n) for (int i = 0; i < int(n); i++) #define rep1(i, n) for (int i = 1; i <= int(n); i++) #define per(i, n) for (int i = int(n) - 1; i >= 0; i--) #define per1(i, n) for (int i = int(n); i > 0; i--) #define all(c) c.begin(), c.end() #define si(x) int(x.size()) #define pb emplace_back #define fs first #define sc second template <class T> using V = vector<T>; template <class T> using VV = vector<vector<T>>; template <class T, class U> void chmax(T &x, U y) { if (x < y) x = y; } template <class T, class U> void chmin(T &x, U y) { if (y < x) x = y; } template <class T> void mkuni(V<T> &v) { sort(all(v)); v.erase(unique(all(v)), v.end()); } template <class S, class T> ostream &operator<<(ostream &o, const pair<S, T> &p) { return o << "(" << p.fs << "," << p.sc << ")"; } template <class T> ostream &operator<<(ostream &o, const vector<T> &vc) { o << "{"; for (const T &v : vc) o << v << ","; o << "}"; return o; } constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); } #ifdef LOCAL #define show(x) \ cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl void dmpr(ostream &os) { os << endl; } template <class T, class... Args> void dmpr(ostream &os, const T &t, const Args &...args) { os << t << " ~ "; dmpr(os, args...); } #define shows(...) \ cerr << "LINE" << __LINE__ << " : "; \ dmpr(cerr, ##__VA_ARGS__) #define dump(x) \ cerr << "LINE" << __LINE__ << " : " << #x << " = {"; \ for (auto v : x) \ cerr << v << ","; \ cerr << "}" << endl; #else #define show(x) void(0) #define dump(x) void(0) #define shows(...) void(0) #endif int main() { cin.tie(0); ios::sync_with_stdio(false); // DON'T USE scanf/printf/puts !! cout << fixed << setprecision(20); int N; cin >> N; V<int> p(N), w(N); rep(i, N) cin >> p[i] >> w[i]; int Q; cin >> Q; using P = pair<int, int>; VV<P> v2Wq(N); rep(q, Q) { int v, W; cin >> v >> W; v--; v2Wq[v].pb(W, q); } V<int> dep(N); dep[0] = 1; V<int> root(N); VV<P> wps(N); rep1(i, N - 1) dep[i] = dep[(i - 1) / 2] + 1; V<int> ans(Q); rep(i, N) { if (dep[i] <= 12) { V<int> vs; { int v = i; rep(k, dep[i]) { vs.pb(v); v = (v - 1) / 2; } } V<P> ps; rep(s, 1 << dep[i]) { int pp = 0, ww = 0; rep(j, dep[i]) if (s & 1 << j) ww += w[vs[j]], pp += p[vs[j]]; ps.pb(P(ww, pp)); } sort(all(ps), [&](P l, P r) { return P(l.fs, -l.sc) < P(r.fs, -r.sc); }); { int mx = -1; for (P p : ps) { if (p.sc > mx) { mx = p.sc; wps[i].pb(p); } } } for (const auto [W, q] : v2Wq[i]) { int idx = lower_bound(all(wps[i]), P(W + 1, -1)) - wps[i].begin() - 1; ans[q] = wps[i][idx].sc; } } else { root[i] = root[(i - 1) / 2]; V<int> vs; { int v = i; while (v != root[i]) { vs.pb(v); v = (v - 1) / 2; } } rep(s, 1 << si(vs)) { int pp = 0, ww = 0; rep(j, dep[i]) if (s & 1 << j) ww += w[vs[j]], pp += p[vs[j]]; for (const auto [W, q] : v2Wq[i]) { int idx = lower_bound(all(wps[root[i]]), P(W - ww + 1, -1)) - wps[root[i]].begin() - 1; if (idx == -1) continue; chmax(ans[q], pp + wps[root[i]][idx].sc); } } } } rep(i, Q) cout << ans[i] << '\n'; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using uint = unsigned int; #define rep(i, n) for (int i = 0; i < int(n); i++) #define rep1(i, n) for (int i = 1; i <= int(n); i++) #define per(i, n) for (int i = int(n) - 1; i >= 0; i--) #define per1(i, n) for (int i = int(n); i > 0; i--) #define all(c) c.begin(), c.end() #define si(x) int(x.size()) #define pb emplace_back #define fs first #define sc second template <class T> using V = vector<T>; template <class T> using VV = vector<vector<T>>; template <class T, class U> void chmax(T &x, U y) { if (x < y) x = y; } template <class T, class U> void chmin(T &x, U y) { if (y < x) x = y; } template <class T> void mkuni(V<T> &v) { sort(all(v)); v.erase(unique(all(v)), v.end()); } template <class S, class T> ostream &operator<<(ostream &o, const pair<S, T> &p) { return o << "(" << p.fs << "," << p.sc << ")"; } template <class T> ostream &operator<<(ostream &o, const vector<T> &vc) { o << "{"; for (const T &v : vc) o << v << ","; o << "}"; return o; } constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); } #ifdef LOCAL #define show(x) \ cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl void dmpr(ostream &os) { os << endl; } template <class T, class... Args> void dmpr(ostream &os, const T &t, const Args &...args) { os << t << " ~ "; dmpr(os, args...); } #define shows(...) \ cerr << "LINE" << __LINE__ << " : "; \ dmpr(cerr, ##__VA_ARGS__) #define dump(x) \ cerr << "LINE" << __LINE__ << " : " << #x << " = {"; \ for (auto v : x) \ cerr << v << ","; \ cerr << "}" << endl; #else #define show(x) void(0) #define dump(x) void(0) #define shows(...) void(0) #endif int main() { cin.tie(0); ios::sync_with_stdio(false); // DON'T USE scanf/printf/puts !! cout << fixed << setprecision(20); int N; cin >> N; V<int> p(N), w(N); rep(i, N) cin >> p[i] >> w[i]; int Q; cin >> Q; using P = pair<int, int>; VV<P> v2Wq(N); rep(q, Q) { int v, W; cin >> v >> W; v--; v2Wq[v].pb(W, q); } V<int> dep(N); dep[0] = 1; V<int> root(N); VV<P> wps(N); rep1(i, N - 1) dep[i] = dep[(i - 1) / 2] + 1; V<int> ans(Q); rep(i, N) { if (dep[i] <= 12) { root[i] = i; V<int> vs; { int v = i; rep(k, dep[i]) { vs.pb(v); v = (v - 1) / 2; } } V<P> ps; rep(s, 1 << dep[i]) { int pp = 0, ww = 0; rep(j, dep[i]) if (s & 1 << j) ww += w[vs[j]], pp += p[vs[j]]; ps.pb(P(ww, pp)); } sort(all(ps), [&](P l, P r) { return P(l.fs, -l.sc) < P(r.fs, -r.sc); }); { int mx = -1; for (P p : ps) { if (p.sc > mx) { mx = p.sc; wps[i].pb(p); } } } for (const auto [W, q] : v2Wq[i]) { int idx = lower_bound(all(wps[i]), P(W + 1, -1)) - wps[i].begin() - 1; ans[q] = wps[i][idx].sc; } } else { root[i] = root[(i - 1) / 2]; V<int> vs; { int v = i; while (v != root[i]) { vs.pb(v); v = (v - 1) / 2; } } rep(s, 1 << si(vs)) { int pp = 0, ww = 0; rep(j, dep[i]) if (s & 1 << j) ww += w[vs[j]], pp += p[vs[j]]; for (const auto [W, q] : v2Wq[i]) { int idx = lower_bound(all(wps[root[i]]), P(W - ww + 1, -1)) - wps[root[i]].begin() - 1; if (idx == -1) continue; chmax(ans[q], pp + wps[root[i]][idx].sc); } } } } rep(i, Q) cout << ans[i] << '\n'; }
insert
90
90
90
91
TLE
p02648
C++
Time Limit Exceeded
// #pragma GCC optimize ("-O3") #include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <regex> #include <set> #include <sstream> #include <stack> #include <string> #include <time.h> #include <type_traits> #include <typeinfo> #include <unordered_map> #include <vector> #ifdef _MSC_VER #include <intrin.h> #define popcnt __popcnt64 // # define __builtin_popcount __popcnt #else #define popcnt __builtin_popcountll #endif // #include "boost/variant.hpp" using namespace std; typedef long long ll; constexpr ll MOD = 1000000007LL; constexpr ll INF = 1LL << 60; #define rep(i, N, M) for (ll i = N, i##_len = (M); i < i##_len; ++i) #define rep_skip(i, N, M, ...) \ for (ll i = N, i##_len = (M); i < i##_len; i += (skip)) #define rrep(i, N, M) for (ll i = (M)-1, i##_len = (N - 1); i > i##_len; --i) #define repbit(bit, N, DIG) rep(bit, (N), (1LL << (DIG))) #define pb push_back #define fir first #define sec second #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define perm(c) \ sort(all(c)); \ for (bool c##perm = 1; c##perm; \ c##perm = next_permutation( \ all(c))) // perm(c){write(c)} writes all permutation of c constexpr ll dceil(ll x, ll y) { if (y < 0) { x *= -1; y *= -1; }; return x > 0 ? (x + y - 1) / y : x / y; } // ceil for x/y constexpr ll dfloor(ll x, ll y) { if (y < 0) { x *= -1; y *= -1; }; return x > 0 ? x / y : -dceil((-x), y); } // floor for x/y typedef pair<double, double> pd; typedef pair<ll, ll> pll; typedef vector<ll> vll; typedef vector<vll> vvll; typedef vector<pll> vpll; typedef vector<bool> vb; typedef vector<vb> vvb; typedef vector<string> vs; template <typename T> using pq_greater = priority_queue<T, vector<T>, greater<T>>; template <typename T> using vpt = vector<complex<T>>; template <int n> struct tll_impl { using type = decltype(tuple_cat(tuple<ll>(), declval<typename tll_impl<n - 1>::type>())); }; template <> struct tll_impl<1> { using type = tuple<ll>; }; template <int n> using tll = typename tll_impl<n>::type; template <class T> constexpr ll SZ(T &v) { return static_cast<ll>(v.size()); }; template <int n, typename T> struct vec_t_impl { using type = vector<typename vec_t_impl<n - 1, T>::type>; }; template <typename T> struct vec_t_impl<1, T> { using type = vector<T>; }; template <int n, typename T> using vec_t = typename vec_t_impl<n, T>::type; // check static_assert(is_same<vec_t<3, ll>, vector<vector<vector<ll>>>>::value, ""); // decompose vector into basetype and dimension. template <typename T> struct vec_dec { static constexpr int dim = 0; using type = T; }; template <typename T> struct vec_dec<vector<T>> { static constexpr int dim = vec_dec<T>::dim + 1; using type = typename vec_dec<T>::type; }; static_assert(is_same<typename vec_dec<vec_t<3, ll>>::type, ll>::value, ""); static_assert(vec_dec<vec_t<3, ll>>::dim == 3, ""); template <typename T = ll> vector<T> makev(size_t a) { return vector<T>(a); } template <typename T = ll, typename... Ts> auto makev(size_t a, Ts... ts) { return vector<decltype(makev<T>(ts...))>(a, makev<T>(ts...)); } // ex: auto dp = makev<ll>(4,5) => vector<vector<ll>> dp(4,vector<ll>(5)); template <typename T> struct is_vector : std::false_type {}; // check if T is vector template <typename T> struct is_vector<vector<T>> : std::true_type {}; static_assert(is_vector<vector<ll>>::value == true && is_vector<ll>::value == false, ""); // check if T is vector template <typename T> struct is_pair : std::false_type {}; template <typename T, typename S> struct is_pair<pair<T, S>> : std::true_type {}; static_assert(is_pair<pll>::value == true && is_pair<ll>::value == false, ""); template <typename T, typename V, typename enable_if<!is_vector<T>::value, nullptr_t>::type = nullptr> void fill_v(T &t, const V &v) { t = v; } template <typename T, typename V, typename enable_if<is_vector<T>::value, nullptr_t>::type = nullptr> void fill_v(T &t, const V &v) { for (auto &&x : t) fill_v(x, v); } // ex: fill_v(dp, INF); namespace std { template <class T> bool operator<(const complex<T> &a, const complex<T> &b) { return a.real() == b.real() ? a.imag() < b.imag() : a.real() < b.real(); } }; // namespace std template <typename T, typename S> istream &operator>>(istream &istr, pair<T, S> &x) { return istr >> x.first >> x.second; } template <typename T> istream &operator>>(istream &istr, vector<T> &x) { rep(i, 0, x.size()) istr >> x[i]; return istr; } template <typename T> istream &operator>>(istream &istr, complex<T> &x) { T r, i; istr >> r >> i; x.real(r); x.imag(i); return istr; } template <typename T, typename Delim_t = string, typename enable_if<!is_vector<T>::value, nullptr_t>::type = nullptr> void write(T &x, Delim_t delim = " ") { cout << x << delim; } template <typename T, typename Delim_t = string, typename enable_if<is_vector<T>::value, nullptr_t>::type = nullptr> void write(T &x, Delim_t delim = " ") { rep(i, 0, x.size()) write(x[i], (i == (x.size() - 1) ? "" : delim)); cout << '\n'; } template <typename T> void chmin(T &a, T b) { if (a > b) a = b; } template <typename T> void chmax(T &a, T b) { if (a < b) a = b; } vll seq(ll i, ll j) { vll res(j - i); rep(k, i, j) res[k] = i + k; return res; } constexpr ll POW_0(ll x, ll y) { if (y == 0) return 1; if (y == 1) return x; if (y == 2) return x * x; if (y % 2 == 0) return POW_0(POW_0(x, y / 2), 2LL); return ((POW_0(POW_0(x, y / 2), 2LL)) * (x)); } constexpr ll POW(ll x, ll y, ll mod = 0) { if (mod == 0) return POW_0(x, y); if (y == 0) return 1; if (y == 1) return x % mod; if (y == 2) return x * x % mod; if (y % 2 == 0) return POW(POW(x, y / 2, mod), 2LL, mod) % mod; return ((POW(POW(x, y / 2, mod), 2LL, mod)) * (x % mod)) % mod; } template <typename Inputs, typename Functor, typename T = typename Inputs::value_type> void sort_by(Inputs &inputs, Functor f) { std::sort(std::begin(inputs), std::end(inputs), [&f](const T &lhs, const T &rhs) { return f(lhs) < f(rhs); }); } template <int pos, typename Inputs, typename T = typename Inputs::value_type> void sort_by(Inputs &inputs) { std::sort( std::begin(inputs), std::end(inputs), [](const T &lhs, const T &rhs) { return get<pos>(lhs) < get<pos>(rhs); }); } template <typename Inputs, typename Functor, typename T = typename Inputs::value_type> void stable_sort_by(Inputs &inputs, Functor f) { std::stable_sort( std::begin(inputs), std::end(inputs), [&f](const T &lhs, const T &rhs) { return f(lhs) < f(rhs); }); } template <typename Inputs> void sort_uniq(Inputs &inputs) { sort(all(inputs)); inputs.erase(unique(all(inputs)), inputs.end()); } vector<string> split(const string &s, char delim) { vector<string> elems; stringstream ss(s); string item; if (s.size() > 0 && s.front() == delim) { elems.push_back(""); } while (getline(ss, item, delim)) { if (!item.empty()) { elems.push_back(item); } } if (s.size() > 0 && s.back() == delim) { elems.push_back(""); } return elems; } template <class T> map<T, ll> inv_map(vector<T> &x) { map<T, ll> res; rep(i, 0, x.size()) { res[x[i]] = i; } return res; } template <class K, class V> map<V, K> inv_map(map<K, V> &m) { map<V, K> res; for (const auto &x : m) { res[x.second] = x.first; } return res; } template <class T, class val_t = typename T::value_type, enable_if_t<!is_same<T, set<val_t>>::value> * = nullptr> constexpr bool exist(const T &container, val_t val) { return find(all(container), val) != container.end(); } template <class T, class val_t = typename T::value_type, enable_if_t<is_same<T, set<val_t>>::value> * = nullptr> constexpr bool exist(const T &container, val_t val) { return container.find(val) != container.end(); } // inner prod: |a||b|cos(theta) template <class T> T dot(complex<T> a, complex<T> b) { return a.real() * b.real() + a.imag() * b.imag(); } // outer prod |a||b|sin(theta) template <class T> T cross(complex<T> a, complex<T> b) { return a.real() * b.imag() - a.imag() * b.real(); } ll div_ferm(ll val, ll b, ll mod) { return (val * POW(b, mod - 2, mod)) % mod; } // === Modint === // static uint_fast64_t runtime_modulus = MOD; template <ll modulus = MOD> class modint { public: ll val; constexpr modint() : val(0) {} constexpr modint(ll x) : val((x %= mod()) < 0 ? x + mod() : x) {} constexpr modint(ll x, ll modulus_) { set_modulo(modulus_); val = (x %= mod()) < 0 ? x + mod() : x; } template <class Ret = ll &> static auto modulo() -> std::enable_if_t<(modulus <= 0), Ret> { static ll runtime_modulus = numeric_limits<ll>::max(); return runtime_modulus; // singleton technique } template <class Ret = const ll> static auto mod() -> std::enable_if_t<(modulus <= 0), Ret> { return modulo(); } template <class Ret = const ll> static constexpr auto mod() -> std::enable_if_t<(modulus > 0), Ret> { return modulus; } template <ll modulus_ = modulus, enable_if_t<(modulus_ <= 0), nullptr_t> = nullptr> static void set_modulo(ll mod) { modulo() = mod; } void reset_modulo(ll modulus_) { modulo() = modulus_; val %= mod(); } constexpr modint inv() { return pow(mod() - 2); } constexpr ll value() const noexcept { return val; } constexpr modint operator+(const modint rhs) const noexcept { return modint(*this) += rhs; } constexpr modint operator-(const modint rhs) const noexcept { return modint(*this) -= rhs; } constexpr modint operator*(const modint rhs) const noexcept { return modint(*this) *= rhs; } constexpr modint operator/(const modint rhs) const noexcept { return modint(*this) /= rhs; } modint &operator+=(const modint rhs) noexcept { val += rhs.val; if (val >= mod()) { val -= mod(); } return *this; } modint &operator-=(const modint rhs) noexcept { if (val < rhs.val) { val += mod(); } val -= rhs.val; return *this; } modint &operator*=(const modint rhs) noexcept { val = val * rhs.val % mod(); return *this; } modint &operator/=(modint rhs) noexcept { ll exp = mod() - 2; while (exp) { if (exp % 2) { *this *= rhs; } rhs *= rhs; exp /= 2; } return *this; } modint &operator++() noexcept { return *this += modint(1); } modint operator++(int) noexcept { modint t = *this; *this += modint(1); return t; } modint &operator--() noexcept { return *this -= modint(1); } modint operator--(int) noexcept { modint t = *this; *this -= modint(1); return t; } constexpr modint operator-() { return val ? mod() - val : val; } constexpr bool operator==(const modint rhs) const noexcept { return val == rhs.value(); } constexpr bool operator!=(const modint rhs) const noexcept { return val != rhs.value(); } constexpr bool operator<(const modint rhs) const noexcept { return val < rhs.value(); } static constexpr modint zero() { return modint(0); } static constexpr modint unit() { return modint(1); } modint pow(long long k) const { modint v = *this; modint res(1), tmp(v); while (k) { if (k & 1) res *= tmp; tmp *= tmp; k >>= 1; } return res; } ll log(modint b) { modint val = *this; const ll sq = 40000; map<modint, ll> dp; // dp.reserve(sq); modint res(1); for (ll r = 0; r < sq; r++) { if (!dp.count(res)) dp[res] = r; res *= val; } modint p = val.inv().pow(sq); res = b; for (ll q = 0; q <= mod() / sq + 1; q++) { if (dp.count(res)) { ll idx = q * sq + dp[res]; if (idx > 0) return idx; } res *= p; } return INF; } friend ostream &operator<<(ostream &o, const modint<modulus> &t) { o << t.value(); return o; } friend istream &operator>>(istream &in, modint<modulus> &t) { ll x; in >> x; t = modint<modulus>(x); return in; } friend modint<modulus> POW(modint<modulus> x, ll n) { return modint<modulus>(POW(x.value(), n, mod())); } }; // user defined literal modint<MOD> operator"" _mod(unsigned long long x) { return modint<MOD>(x); } template <class T = modint<>> class Combination { // this calculates combination (nCk). // Constructor runs in O(MAX). // get(n,k) returns nCk in O(1). ll N_MAX; vector<T> fac, finv; public: Combination(ll N_MAX = 210000) : N_MAX(N_MAX), fac(N_MAX), finv(N_MAX) { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; pre_process(2, N_MAX); } T operator()(ll n, ll k) { // choose k from n if (N_MAX < n) pre_process(N_MAX, n); if (0 <= n && n < k) return 0; if (k == 0) return 1; if (n < 0) return operator()(-n + k - 1, k) * (k % 2 ? -1 : 0); return fac[n] * (finv[k] * finv[n - k]); } T H(ll n, ll k) { // 1) 区間[0, k) を(空を許して)n個に分割する場合の数 // 2) n個の中からk個を重複を許して選ぶ return operator()(n + k - 1, k); } T P(ll n, ll k) { // n (n-1) ... (n-k+1) if (N_MAX < n) pre_process(N_MAX, n); return (n < k || n < 0) ? T(0) : fac[n] * finv[n - k]; } T Fac(ll n) { return P(n, n); } T FacInv(ll n) { if (N_MAX < n) pre_process(N_MAX, n); return n < 0 ? T(0) : finv[n]; } private: void pre_process(ll m, ll n) { if (N_MAX < n) { fac.resize(n); finv.resize(n); } rep(i, m, n) { fac[i] = fac[i - 1] * i; finv[i] = finv[i - 1] / i; } } }; ll choose(int n, int r) { // O(r) for small n ll acc = 1; rep(i, 0, r) acc = acc * (n - i) / (i + 1); return acc; } ll gcd(ll val, ll b) { if (val < 0) val *= -1; if (b < 0) b *= -1; if (val == 0) return b; if (b == 0) return val; if (val % b == 0) return b; else return gcd(b, val % b); } vll divisor(ll n) { // returns common divisors in O(sqrt(min(n,m))) vll res; for (ll i = 1; i * i <= n; i++) { if (n % i == 0) { res.push_back(i); res.push_back(n / i); } if (i * i == n) res.push_back(i); } sort(res.begin(), res.end()); return res; } vll divisor(ll n, ll m) { // returns common divisors in O(sqrt(min(n,m))) if (n > m) swap(n, m); vll res; for (ll i = 1; i * i <= n; i++) { if (n % i == 0) { if (m % i == 0) res.push_back(i); if (m % (n / i) == 0) res.push_back(n / i); } if (i * i == n && m % i == 0) res.push_back(i); } sort(res.begin(), res.end()); return res; } vpll prime_factorize(ll n) { // returns prime factorization of n in O(sprt(n)) vector<pll> res; for (ll p = 2; p * p <= n; ++p) { if (n % p != 0) continue; ll num = 0; while (n % p == 0) { ++num; n /= p; } res.push_back({p, num}); } if (n != 1) res.push_back(make_pair(n, 1)); return res; } ll eulers_phi(ll n) { // returns the number of coprime integers in O(sqrt(n) + n^(1.4/log log n)) [ // the number of prime divisors <= O(log(n^(1.4/log log n) ) ] ll res = 0; auto primes = prime_factorize(n); ll psize = primes.size(); rep(bit, 1, 1LL << psize) { ll bitsize = popcnt(bit); ll arg = (bitsize % 2 ? 1 : -1); ll base = 1; rep(i, 0, primes.size()) { ll p = primes[i].first; if (bit & (1LL << i)) base *= p; } res += arg * (n / base); } return n - res; } // ビット bit に i 番目のフラグが立っているかどうか if (bit & (1 << i)) // ビット bit に i 番目のフラグが消えているかどうか if (!(bit & (1 << i))) // ビット bit に i 番目のフラグを立てる bit| = (1 << i) // ビット bit に i 番目のフラグを消す bit &= ~(1 << i) // ビット bit に何個のフラグが立っているか __builtin_popcount(bit) // ビット bit に i 番目のフラグを立てたもの bit|(1 << i) // ビット bit に i 番目のフラグを消したもの bit & ~(1 << i) template <ll bit = 2LL> ll at_bit(ll n, ll i) { return n / POW(bit, i) % bit; } template <> ll at_bit<2>(ll n, ll i) { return (n >> i) % 2LL; } template <ll bit> ll get_bit(ll i) { return POW(bit, i); } template <> ll get_bit<2>(ll i) { return 1LL << i; } template <ll bit = 2> ll get_max_bit(ll n) { ll tmp = bit; ll at = 0; while (tmp <= n) { at++; tmp *= bit; } return at; } template <> ll get_max_bit<2>(ll n) { ll tmp = 2; ll at = 0; while (tmp <= n) { at++; tmp <<= 1; } return at; } ll check_bit(ll N, int POS) { return (N & (1LL << POS)); } // Function to return maximum XOR subset in set by gaussian elimination ll max_subset_xor(vector<ll> &v) { int n = v.size(); int ind = 0; // Array index for (int bit = 61; bit >= 0; bit--) { int x = ind; while (x < n && check_bit(v[x], bit) == 0) x++; if (x == n) continue; // skip if there is no number below ind where current bit is 1 swap(v[ind], v[x]); for (int j = 0; j < n; j++) { if (j != ind && check_bit(v[j], bit)) v[j] ^= v[ind]; } ind++; } return accumulate(all(v), 0LL, [](ll x, ll y) { return x ^ y; }); } int main() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(12); ll n; cin >> n; vll v(n), w(n); rep(i, 0, n) { cin >> v[i] >> w[i]; } auto par = [&](ll i) { return (i + 1) / 2 - 1; }; ll q; cin >> q; ll max_dp = 1ll << 9; ll L_max = POW(10, 5) + 10; vvll dp(max_dp, vll(L_max)); rep(i, 0, min(max_dp, n)) { rep(ww, 0, L_max) { if (i == 0) { dp[i][ww] = (ww < w[i]) ? 0 : v[i]; } else { if (ww >= w[i]) dp[i][ww] = max(dp[par(i)][ww], dp[par(i)][ww - w[i]] + v[i]); else { dp[i][ww] = dp[par(i)][ww]; } } } } function<ll(ll, ll)> calc = [&](ll i, ll ww) { if (i < max_dp) { return dp[i][ww]; } if (ww >= w[i]) { return max(calc(par(i), ww), calc(par(i), ww - w[i]) + v[i]); } else { return calc(par(i), ww); } }; rep(i, 0, q) { ll v, l; cin >> v >> l; v--; cout << calc(v, l) << endl; } return 0; }
// #pragma GCC optimize ("-O3") #include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cmath> #include <complex> #include <cstdio> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <regex> #include <set> #include <sstream> #include <stack> #include <string> #include <time.h> #include <type_traits> #include <typeinfo> #include <unordered_map> #include <vector> #ifdef _MSC_VER #include <intrin.h> #define popcnt __popcnt64 // # define __builtin_popcount __popcnt #else #define popcnt __builtin_popcountll #endif // #include "boost/variant.hpp" using namespace std; typedef long long ll; constexpr ll MOD = 1000000007LL; constexpr ll INF = 1LL << 60; #define rep(i, N, M) for (ll i = N, i##_len = (M); i < i##_len; ++i) #define rep_skip(i, N, M, ...) \ for (ll i = N, i##_len = (M); i < i##_len; i += (skip)) #define rrep(i, N, M) for (ll i = (M)-1, i##_len = (N - 1); i > i##_len; --i) #define repbit(bit, N, DIG) rep(bit, (N), (1LL << (DIG))) #define pb push_back #define fir first #define sec second #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define perm(c) \ sort(all(c)); \ for (bool c##perm = 1; c##perm; \ c##perm = next_permutation( \ all(c))) // perm(c){write(c)} writes all permutation of c constexpr ll dceil(ll x, ll y) { if (y < 0) { x *= -1; y *= -1; }; return x > 0 ? (x + y - 1) / y : x / y; } // ceil for x/y constexpr ll dfloor(ll x, ll y) { if (y < 0) { x *= -1; y *= -1; }; return x > 0 ? x / y : -dceil((-x), y); } // floor for x/y typedef pair<double, double> pd; typedef pair<ll, ll> pll; typedef vector<ll> vll; typedef vector<vll> vvll; typedef vector<pll> vpll; typedef vector<bool> vb; typedef vector<vb> vvb; typedef vector<string> vs; template <typename T> using pq_greater = priority_queue<T, vector<T>, greater<T>>; template <typename T> using vpt = vector<complex<T>>; template <int n> struct tll_impl { using type = decltype(tuple_cat(tuple<ll>(), declval<typename tll_impl<n - 1>::type>())); }; template <> struct tll_impl<1> { using type = tuple<ll>; }; template <int n> using tll = typename tll_impl<n>::type; template <class T> constexpr ll SZ(T &v) { return static_cast<ll>(v.size()); }; template <int n, typename T> struct vec_t_impl { using type = vector<typename vec_t_impl<n - 1, T>::type>; }; template <typename T> struct vec_t_impl<1, T> { using type = vector<T>; }; template <int n, typename T> using vec_t = typename vec_t_impl<n, T>::type; // check static_assert(is_same<vec_t<3, ll>, vector<vector<vector<ll>>>>::value, ""); // decompose vector into basetype and dimension. template <typename T> struct vec_dec { static constexpr int dim = 0; using type = T; }; template <typename T> struct vec_dec<vector<T>> { static constexpr int dim = vec_dec<T>::dim + 1; using type = typename vec_dec<T>::type; }; static_assert(is_same<typename vec_dec<vec_t<3, ll>>::type, ll>::value, ""); static_assert(vec_dec<vec_t<3, ll>>::dim == 3, ""); template <typename T = ll> vector<T> makev(size_t a) { return vector<T>(a); } template <typename T = ll, typename... Ts> auto makev(size_t a, Ts... ts) { return vector<decltype(makev<T>(ts...))>(a, makev<T>(ts...)); } // ex: auto dp = makev<ll>(4,5) => vector<vector<ll>> dp(4,vector<ll>(5)); template <typename T> struct is_vector : std::false_type {}; // check if T is vector template <typename T> struct is_vector<vector<T>> : std::true_type {}; static_assert(is_vector<vector<ll>>::value == true && is_vector<ll>::value == false, ""); // check if T is vector template <typename T> struct is_pair : std::false_type {}; template <typename T, typename S> struct is_pair<pair<T, S>> : std::true_type {}; static_assert(is_pair<pll>::value == true && is_pair<ll>::value == false, ""); template <typename T, typename V, typename enable_if<!is_vector<T>::value, nullptr_t>::type = nullptr> void fill_v(T &t, const V &v) { t = v; } template <typename T, typename V, typename enable_if<is_vector<T>::value, nullptr_t>::type = nullptr> void fill_v(T &t, const V &v) { for (auto &&x : t) fill_v(x, v); } // ex: fill_v(dp, INF); namespace std { template <class T> bool operator<(const complex<T> &a, const complex<T> &b) { return a.real() == b.real() ? a.imag() < b.imag() : a.real() < b.real(); } }; // namespace std template <typename T, typename S> istream &operator>>(istream &istr, pair<T, S> &x) { return istr >> x.first >> x.second; } template <typename T> istream &operator>>(istream &istr, vector<T> &x) { rep(i, 0, x.size()) istr >> x[i]; return istr; } template <typename T> istream &operator>>(istream &istr, complex<T> &x) { T r, i; istr >> r >> i; x.real(r); x.imag(i); return istr; } template <typename T, typename Delim_t = string, typename enable_if<!is_vector<T>::value, nullptr_t>::type = nullptr> void write(T &x, Delim_t delim = " ") { cout << x << delim; } template <typename T, typename Delim_t = string, typename enable_if<is_vector<T>::value, nullptr_t>::type = nullptr> void write(T &x, Delim_t delim = " ") { rep(i, 0, x.size()) write(x[i], (i == (x.size() - 1) ? "" : delim)); cout << '\n'; } template <typename T> void chmin(T &a, T b) { if (a > b) a = b; } template <typename T> void chmax(T &a, T b) { if (a < b) a = b; } vll seq(ll i, ll j) { vll res(j - i); rep(k, i, j) res[k] = i + k; return res; } constexpr ll POW_0(ll x, ll y) { if (y == 0) return 1; if (y == 1) return x; if (y == 2) return x * x; if (y % 2 == 0) return POW_0(POW_0(x, y / 2), 2LL); return ((POW_0(POW_0(x, y / 2), 2LL)) * (x)); } constexpr ll POW(ll x, ll y, ll mod = 0) { if (mod == 0) return POW_0(x, y); if (y == 0) return 1; if (y == 1) return x % mod; if (y == 2) return x * x % mod; if (y % 2 == 0) return POW(POW(x, y / 2, mod), 2LL, mod) % mod; return ((POW(POW(x, y / 2, mod), 2LL, mod)) * (x % mod)) % mod; } template <typename Inputs, typename Functor, typename T = typename Inputs::value_type> void sort_by(Inputs &inputs, Functor f) { std::sort(std::begin(inputs), std::end(inputs), [&f](const T &lhs, const T &rhs) { return f(lhs) < f(rhs); }); } template <int pos, typename Inputs, typename T = typename Inputs::value_type> void sort_by(Inputs &inputs) { std::sort( std::begin(inputs), std::end(inputs), [](const T &lhs, const T &rhs) { return get<pos>(lhs) < get<pos>(rhs); }); } template <typename Inputs, typename Functor, typename T = typename Inputs::value_type> void stable_sort_by(Inputs &inputs, Functor f) { std::stable_sort( std::begin(inputs), std::end(inputs), [&f](const T &lhs, const T &rhs) { return f(lhs) < f(rhs); }); } template <typename Inputs> void sort_uniq(Inputs &inputs) { sort(all(inputs)); inputs.erase(unique(all(inputs)), inputs.end()); } vector<string> split(const string &s, char delim) { vector<string> elems; stringstream ss(s); string item; if (s.size() > 0 && s.front() == delim) { elems.push_back(""); } while (getline(ss, item, delim)) { if (!item.empty()) { elems.push_back(item); } } if (s.size() > 0 && s.back() == delim) { elems.push_back(""); } return elems; } template <class T> map<T, ll> inv_map(vector<T> &x) { map<T, ll> res; rep(i, 0, x.size()) { res[x[i]] = i; } return res; } template <class K, class V> map<V, K> inv_map(map<K, V> &m) { map<V, K> res; for (const auto &x : m) { res[x.second] = x.first; } return res; } template <class T, class val_t = typename T::value_type, enable_if_t<!is_same<T, set<val_t>>::value> * = nullptr> constexpr bool exist(const T &container, val_t val) { return find(all(container), val) != container.end(); } template <class T, class val_t = typename T::value_type, enable_if_t<is_same<T, set<val_t>>::value> * = nullptr> constexpr bool exist(const T &container, val_t val) { return container.find(val) != container.end(); } // inner prod: |a||b|cos(theta) template <class T> T dot(complex<T> a, complex<T> b) { return a.real() * b.real() + a.imag() * b.imag(); } // outer prod |a||b|sin(theta) template <class T> T cross(complex<T> a, complex<T> b) { return a.real() * b.imag() - a.imag() * b.real(); } ll div_ferm(ll val, ll b, ll mod) { return (val * POW(b, mod - 2, mod)) % mod; } // === Modint === // static uint_fast64_t runtime_modulus = MOD; template <ll modulus = MOD> class modint { public: ll val; constexpr modint() : val(0) {} constexpr modint(ll x) : val((x %= mod()) < 0 ? x + mod() : x) {} constexpr modint(ll x, ll modulus_) { set_modulo(modulus_); val = (x %= mod()) < 0 ? x + mod() : x; } template <class Ret = ll &> static auto modulo() -> std::enable_if_t<(modulus <= 0), Ret> { static ll runtime_modulus = numeric_limits<ll>::max(); return runtime_modulus; // singleton technique } template <class Ret = const ll> static auto mod() -> std::enable_if_t<(modulus <= 0), Ret> { return modulo(); } template <class Ret = const ll> static constexpr auto mod() -> std::enable_if_t<(modulus > 0), Ret> { return modulus; } template <ll modulus_ = modulus, enable_if_t<(modulus_ <= 0), nullptr_t> = nullptr> static void set_modulo(ll mod) { modulo() = mod; } void reset_modulo(ll modulus_) { modulo() = modulus_; val %= mod(); } constexpr modint inv() { return pow(mod() - 2); } constexpr ll value() const noexcept { return val; } constexpr modint operator+(const modint rhs) const noexcept { return modint(*this) += rhs; } constexpr modint operator-(const modint rhs) const noexcept { return modint(*this) -= rhs; } constexpr modint operator*(const modint rhs) const noexcept { return modint(*this) *= rhs; } constexpr modint operator/(const modint rhs) const noexcept { return modint(*this) /= rhs; } modint &operator+=(const modint rhs) noexcept { val += rhs.val; if (val >= mod()) { val -= mod(); } return *this; } modint &operator-=(const modint rhs) noexcept { if (val < rhs.val) { val += mod(); } val -= rhs.val; return *this; } modint &operator*=(const modint rhs) noexcept { val = val * rhs.val % mod(); return *this; } modint &operator/=(modint rhs) noexcept { ll exp = mod() - 2; while (exp) { if (exp % 2) { *this *= rhs; } rhs *= rhs; exp /= 2; } return *this; } modint &operator++() noexcept { return *this += modint(1); } modint operator++(int) noexcept { modint t = *this; *this += modint(1); return t; } modint &operator--() noexcept { return *this -= modint(1); } modint operator--(int) noexcept { modint t = *this; *this -= modint(1); return t; } constexpr modint operator-() { return val ? mod() - val : val; } constexpr bool operator==(const modint rhs) const noexcept { return val == rhs.value(); } constexpr bool operator!=(const modint rhs) const noexcept { return val != rhs.value(); } constexpr bool operator<(const modint rhs) const noexcept { return val < rhs.value(); } static constexpr modint zero() { return modint(0); } static constexpr modint unit() { return modint(1); } modint pow(long long k) const { modint v = *this; modint res(1), tmp(v); while (k) { if (k & 1) res *= tmp; tmp *= tmp; k >>= 1; } return res; } ll log(modint b) { modint val = *this; const ll sq = 40000; map<modint, ll> dp; // dp.reserve(sq); modint res(1); for (ll r = 0; r < sq; r++) { if (!dp.count(res)) dp[res] = r; res *= val; } modint p = val.inv().pow(sq); res = b; for (ll q = 0; q <= mod() / sq + 1; q++) { if (dp.count(res)) { ll idx = q * sq + dp[res]; if (idx > 0) return idx; } res *= p; } return INF; } friend ostream &operator<<(ostream &o, const modint<modulus> &t) { o << t.value(); return o; } friend istream &operator>>(istream &in, modint<modulus> &t) { ll x; in >> x; t = modint<modulus>(x); return in; } friend modint<modulus> POW(modint<modulus> x, ll n) { return modint<modulus>(POW(x.value(), n, mod())); } }; // user defined literal modint<MOD> operator"" _mod(unsigned long long x) { return modint<MOD>(x); } template <class T = modint<>> class Combination { // this calculates combination (nCk). // Constructor runs in O(MAX). // get(n,k) returns nCk in O(1). ll N_MAX; vector<T> fac, finv; public: Combination(ll N_MAX = 210000) : N_MAX(N_MAX), fac(N_MAX), finv(N_MAX) { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; pre_process(2, N_MAX); } T operator()(ll n, ll k) { // choose k from n if (N_MAX < n) pre_process(N_MAX, n); if (0 <= n && n < k) return 0; if (k == 0) return 1; if (n < 0) return operator()(-n + k - 1, k) * (k % 2 ? -1 : 0); return fac[n] * (finv[k] * finv[n - k]); } T H(ll n, ll k) { // 1) 区間[0, k) を(空を許して)n個に分割する場合の数 // 2) n個の中からk個を重複を許して選ぶ return operator()(n + k - 1, k); } T P(ll n, ll k) { // n (n-1) ... (n-k+1) if (N_MAX < n) pre_process(N_MAX, n); return (n < k || n < 0) ? T(0) : fac[n] * finv[n - k]; } T Fac(ll n) { return P(n, n); } T FacInv(ll n) { if (N_MAX < n) pre_process(N_MAX, n); return n < 0 ? T(0) : finv[n]; } private: void pre_process(ll m, ll n) { if (N_MAX < n) { fac.resize(n); finv.resize(n); } rep(i, m, n) { fac[i] = fac[i - 1] * i; finv[i] = finv[i - 1] / i; } } }; ll choose(int n, int r) { // O(r) for small n ll acc = 1; rep(i, 0, r) acc = acc * (n - i) / (i + 1); return acc; } ll gcd(ll val, ll b) { if (val < 0) val *= -1; if (b < 0) b *= -1; if (val == 0) return b; if (b == 0) return val; if (val % b == 0) return b; else return gcd(b, val % b); } vll divisor(ll n) { // returns common divisors in O(sqrt(min(n,m))) vll res; for (ll i = 1; i * i <= n; i++) { if (n % i == 0) { res.push_back(i); res.push_back(n / i); } if (i * i == n) res.push_back(i); } sort(res.begin(), res.end()); return res; } vll divisor(ll n, ll m) { // returns common divisors in O(sqrt(min(n,m))) if (n > m) swap(n, m); vll res; for (ll i = 1; i * i <= n; i++) { if (n % i == 0) { if (m % i == 0) res.push_back(i); if (m % (n / i) == 0) res.push_back(n / i); } if (i * i == n && m % i == 0) res.push_back(i); } sort(res.begin(), res.end()); return res; } vpll prime_factorize(ll n) { // returns prime factorization of n in O(sprt(n)) vector<pll> res; for (ll p = 2; p * p <= n; ++p) { if (n % p != 0) continue; ll num = 0; while (n % p == 0) { ++num; n /= p; } res.push_back({p, num}); } if (n != 1) res.push_back(make_pair(n, 1)); return res; } ll eulers_phi(ll n) { // returns the number of coprime integers in O(sqrt(n) + n^(1.4/log log n)) [ // the number of prime divisors <= O(log(n^(1.4/log log n) ) ] ll res = 0; auto primes = prime_factorize(n); ll psize = primes.size(); rep(bit, 1, 1LL << psize) { ll bitsize = popcnt(bit); ll arg = (bitsize % 2 ? 1 : -1); ll base = 1; rep(i, 0, primes.size()) { ll p = primes[i].first; if (bit & (1LL << i)) base *= p; } res += arg * (n / base); } return n - res; } // ビット bit に i 番目のフラグが立っているかどうか if (bit & (1 << i)) // ビット bit に i 番目のフラグが消えているかどうか if (!(bit & (1 << i))) // ビット bit に i 番目のフラグを立てる bit| = (1 << i) // ビット bit に i 番目のフラグを消す bit &= ~(1 << i) // ビット bit に何個のフラグが立っているか __builtin_popcount(bit) // ビット bit に i 番目のフラグを立てたもの bit|(1 << i) // ビット bit に i 番目のフラグを消したもの bit & ~(1 << i) template <ll bit = 2LL> ll at_bit(ll n, ll i) { return n / POW(bit, i) % bit; } template <> ll at_bit<2>(ll n, ll i) { return (n >> i) % 2LL; } template <ll bit> ll get_bit(ll i) { return POW(bit, i); } template <> ll get_bit<2>(ll i) { return 1LL << i; } template <ll bit = 2> ll get_max_bit(ll n) { ll tmp = bit; ll at = 0; while (tmp <= n) { at++; tmp *= bit; } return at; } template <> ll get_max_bit<2>(ll n) { ll tmp = 2; ll at = 0; while (tmp <= n) { at++; tmp <<= 1; } return at; } ll check_bit(ll N, int POS) { return (N & (1LL << POS)); } // Function to return maximum XOR subset in set by gaussian elimination ll max_subset_xor(vector<ll> &v) { int n = v.size(); int ind = 0; // Array index for (int bit = 61; bit >= 0; bit--) { int x = ind; while (x < n && check_bit(v[x], bit) == 0) x++; if (x == n) continue; // skip if there is no number below ind where current bit is 1 swap(v[ind], v[x]); for (int j = 0; j < n; j++) { if (j != ind && check_bit(v[j], bit)) v[j] ^= v[ind]; } ind++; } return accumulate(all(v), 0LL, [](ll x, ll y) { return x ^ y; }); } int main() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(12); ll n; cin >> n; vll v(n), w(n); rep(i, 0, n) { cin >> v[i] >> w[i]; } auto par = [&](ll i) { return (i + 1) / 2 - 1; }; ll q; cin >> q; ll max_dp = 1ll << 10; ll L_max = POW(10, 5) + 10; vvll dp(max_dp, vll(L_max)); rep(i, 0, min(max_dp, n)) { rep(ww, 0, L_max) { if (i == 0) { dp[i][ww] = (ww < w[i]) ? 0 : v[i]; } else { if (ww >= w[i]) dp[i][ww] = max(dp[par(i)][ww], dp[par(i)][ww - w[i]] + v[i]); else { dp[i][ww] = dp[par(i)][ww]; } } } } function<ll(ll, ll)> calc = [&](ll i, ll ww) { if (i < max_dp) { return dp[i][ww]; } if (ww >= w[i]) { return max(calc(par(i), ww), calc(par(i), ww - w[i]) + v[i]); } else { return calc(par(i), ww); } }; rep(i, 0, q) { ll v, l; cin >> v >> l; v--; cout << calc(v, l) << endl; } return 0; }
replace
680
681
680
681
TLE
p02648
C++
Memory Limit Exceeded
#include "bits/stdc++.h" using namespace std; #define REP(i, n) for (ll i = 0; i < n; i++) #define ll long long #define MOD 1000000007LL #define llMAX 9223372036854775807LL #define llMIN -9223372036854775808LL using vi = vector<ll>; // llの1次元の型に vi という別名をつける using vvi = vector<vi>; // llの2次元の型に vvi という別名をつける ll n; ll M = 1e5 + 1; // bit全探索 vi bt; void setbit(ll i) { bt.clear(); for (ll tmp = 0; i > 0; i /= 2) { if (i % 2 == 1) bt.push_back(tmp); tmp++; } } int main() { ll ans = 0, q; cin >> n; n++; ll nlg = ceil(log2(n)); vi V(n), W(n); REP(i, n - 1) cin >> V[i + 1] >> W[i + 1]; cin >> q; vi u(q), L(q); REP(i, q) cin >> u[i] >> L[i]; // 上半分 ll nhi = nlg - (nlg / 2); if (nhi == 9) nhi = 11; vvi data(1LL << nhi, vi(M)); // N * M の2次元配列 for (ll i = 1; i < (1LL << nhi); i++) { for (ll j = 0; j < M; j++) { if ((j - W[i]) >= 0) { data[i][j] = max(data[i / 2][j - W[i]] + V[i], data[i / 2][j]); } else { data[i][j] = data[i / 2][j]; } } } ll uiv[10]; ll wuiv = 0; // 下半分 for (ll loop = 0; loop < q; loop++) { ll uidx = u[loop]; if (uidx < (1LL << nhi)) { cout << data[uidx][L[loop]] << endl; } else { // 上+下 ll maxval = 0; // vi uiv; ll nn = 0; wuiv = 0; for (;; uidx /= 2) { if (uidx < (1LL << nhi)) break; uiv[wuiv] = uidx; wuiv++; nn++; } ll bn = 1 << nn; for (ll i = 0; i < bn; i++) { setbit(i); ll tmpw = 0; ll tmpv = 0; /* for(ll j=0;j<nn;j++){ if (i&(1<<j)){ tmpv+=V[uiv[j]]; tmpw+=W[uiv[j]]; } } */ for (auto j : bt) { tmpv += V[uiv[j]]; tmpw += W[uiv[j]]; } if (tmpw <= L[loop]) { maxval = max(data[uidx][L[loop] - tmpw] + tmpv, maxval); } } cout << maxval << endl; } } return 0; }
#include "bits/stdc++.h" using namespace std; #define REP(i, n) for (ll i = 0; i < n; i++) #define ll long long #define MOD 1000000007LL #define llMAX 9223372036854775807LL #define llMIN -9223372036854775808LL using vi = vector<ll>; // llの1次元の型に vi という別名をつける using vvi = vector<vi>; // llの2次元の型に vvi という別名をつける ll n; ll M = 1e5 + 1; // bit全探索 vi bt; void setbit(ll i) { bt.clear(); for (ll tmp = 0; i > 0; i /= 2) { if (i % 2 == 1) bt.push_back(tmp); tmp++; } } int main() { ll ans = 0, q; cin >> n; n++; ll nlg = ceil(log2(n)); vi V(n), W(n); REP(i, n - 1) cin >> V[i + 1] >> W[i + 1]; cin >> q; vi u(q), L(q); REP(i, q) cin >> u[i] >> L[i]; // 上半分 ll nhi = nlg - (nlg / 2); if (nhi == 9) nhi = 10; vvi data(1LL << nhi, vi(M)); // N * M の2次元配列 for (ll i = 1; i < (1LL << nhi); i++) { for (ll j = 0; j < M; j++) { if ((j - W[i]) >= 0) { data[i][j] = max(data[i / 2][j - W[i]] + V[i], data[i / 2][j]); } else { data[i][j] = data[i / 2][j]; } } } ll uiv[10]; ll wuiv = 0; // 下半分 for (ll loop = 0; loop < q; loop++) { ll uidx = u[loop]; if (uidx < (1LL << nhi)) { cout << data[uidx][L[loop]] << endl; } else { // 上+下 ll maxval = 0; // vi uiv; ll nn = 0; wuiv = 0; for (;; uidx /= 2) { if (uidx < (1LL << nhi)) break; uiv[wuiv] = uidx; wuiv++; nn++; } ll bn = 1 << nn; for (ll i = 0; i < bn; i++) { setbit(i); ll tmpw = 0; ll tmpv = 0; /* for(ll j=0;j<nn;j++){ if (i&(1<<j)){ tmpv+=V[uiv[j]]; tmpw+=W[uiv[j]]; } } */ for (auto j : bt) { tmpv += V[uiv[j]]; tmpw += W[uiv[j]]; } if (tmpw <= L[loop]) { maxval = max(data[uidx][L[loop] - tmpw] + tmpv, maxval); } } cout << maxval << endl; } } return 0; }
replace
39
40
39
40
MLE
p02648
C++
Runtime Error
#include <bits/stdc++.h> #define REP(i, x, y) for (ll i = x; i <= y; i++) #define BIT(t) (1ll << t) #define PER(i, y, x) for (ll i = y; i >= x; i--) #define vll vector<ll> #define vvll vector<vector<ll>> #define pll pair<ll, ll> #define SIZE(v) ll(v.size()) #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()); using namespace std; typedef long long ll; // ios::sync_with_stdio(false); // cin.tie(nullptr); ll f(ll n, ll halnum, vector<map<ll, ll>> &vw, vll &v, vll &w, ll nod, ll cw) { if (cw < 0) { return -1e18; } if (cw == 0) { return 0; } if (nod <= halnum) { auto tmp = vw[nod].upper_bound(cw); tmp--; return tmp->second; } return max(f(n, halnum, vw, v, w, nod / 2, cw), f(n, halnum, vw, v, w, nod / 2, cw - w[nod]) + v[nod]); } int main() { ll n; cin >> n; ll dep = 1; while (pow(2, dep) - 1 < n) { dep++; } vll w(n + 1), v(n + 1); REP(i, 1, n) { cin >> v[i] >> w[i]; } ll q; cin >> q; vll qv(n + 1), ql(n + 1); REP(i, 1, q) { cin >> qv[i] >> ql[i]; } ll hal = (dep + 1) / 2; ll halnum = pow(2, hal) - 1; vector<map<ll, ll>> vw(halnum + 1); vw[0].insert({0, 0}); REP(i, 1, halnum) { ll bef = i / 2; for (auto x : vw[bef]) { vw[i].insert(x); } for (auto x : vw[bef]) { ll befw = x.first; ll befv = x.second; ll curw = befw + w[i]; ll curv = befv + v[i]; if (vw[i].find(curw) == vw[i].end()) { vw[i][curw] = curv; } else { vw[i][curw] = max(vw[i][curw], curv); } } // REP(i,1,halnum){ // cout << i << " "; // for(auto x:vw[i]){ // cout << "("<<x.first << ", " << x.second << "), " ; // } // cout << endl; // } vector<ll> era; auto cur = vw[i].begin(); ll befwei = -1; ll befval = -1; while (cur != vw[i].end()) { ll curwei = cur->first; ll curval = cur->second; if (befval >= curval) { // vw[i].erase(cur); era.push_back(curwei); // cout << befval << " !!! " << curwei << endl; } else { befval = curval; befwei = curwei; } cur++; } for (auto cur : era) { vw[i].erase(cur); } } // REP(i,1,halnum){ // cout << i << " "; // for(auto x:vw[i]){ // cout << "("<<x.first << ", " << x.second << "), " ; // } // cout << endl; // } vll ans(q + 2); REP(i, 1, q) { ans[i] = f(n, halnum, vw, v, w, qv[i], ql[i]); } REP(i, 1, q) { cout << ans[i] << endl; } }
#include <bits/stdc++.h> #define REP(i, x, y) for (ll i = x; i <= y; i++) #define BIT(t) (1ll << t) #define PER(i, y, x) for (ll i = y; i >= x; i--) #define vll vector<ll> #define vvll vector<vector<ll>> #define pll pair<ll, ll> #define SIZE(v) ll(v.size()) #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()); using namespace std; typedef long long ll; // ios::sync_with_stdio(false); // cin.tie(nullptr); ll f(ll n, ll halnum, vector<map<ll, ll>> &vw, vll &v, vll &w, ll nod, ll cw) { if (cw < 0) { return -1e18; } if (cw == 0) { return 0; } if (nod <= halnum) { auto tmp = vw[nod].upper_bound(cw); tmp--; return tmp->second; } return max(f(n, halnum, vw, v, w, nod / 2, cw), f(n, halnum, vw, v, w, nod / 2, cw - w[nod]) + v[nod]); } int main() { ll n; cin >> n; ll dep = 1; while (pow(2, dep) - 1 < n) { dep++; } vll w(n + 1), v(n + 1); REP(i, 1, n) { cin >> v[i] >> w[i]; } ll q; cin >> q; vll qv(q + 1), ql(q + 1); REP(i, 1, q) { cin >> qv[i] >> ql[i]; } ll hal = (dep + 1) / 2; ll halnum = pow(2, hal) - 1; vector<map<ll, ll>> vw(halnum + 1); vw[0].insert({0, 0}); REP(i, 1, halnum) { ll bef = i / 2; for (auto x : vw[bef]) { vw[i].insert(x); } for (auto x : vw[bef]) { ll befw = x.first; ll befv = x.second; ll curw = befw + w[i]; ll curv = befv + v[i]; if (vw[i].find(curw) == vw[i].end()) { vw[i][curw] = curv; } else { vw[i][curw] = max(vw[i][curw], curv); } } // REP(i,1,halnum){ // cout << i << " "; // for(auto x:vw[i]){ // cout << "("<<x.first << ", " << x.second << "), " ; // } // cout << endl; // } vector<ll> era; auto cur = vw[i].begin(); ll befwei = -1; ll befval = -1; while (cur != vw[i].end()) { ll curwei = cur->first; ll curval = cur->second; if (befval >= curval) { // vw[i].erase(cur); era.push_back(curwei); // cout << befval << " !!! " << curwei << endl; } else { befval = curval; befwei = curwei; } cur++; } for (auto cur : era) { vw[i].erase(cur); } } // REP(i,1,halnum){ // cout << i << " "; // for(auto x:vw[i]){ // cout << "("<<x.first << ", " << x.second << "), " ; // } // cout << endl; // } vll ans(q + 2); REP(i, 1, q) { ans[i] = f(n, halnum, vw, v, w, qv[i], ql[i]); } REP(i, 1, q) { cout << ans[i] << endl; } }
replace
41
42
41
42
0
p02648
C++
Runtime Error
// Template #include <bits/stdc++.h> #define rep_override(x, y, z, name, ...) name #define rep2(i, n) for (int i = 0; i < (n); ++i) #define rep3(i, l, r) for (int i = (l); i < (r); ++i) #define rep(...) rep_override(__VA_ARGS__, rep3, rep2)(__VA_ARGS__) #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() using namespace std; using ll = long long; constexpr int inf = 1001001001; constexpr ll INF = 3003003003003003003; template <typename T> inline bool chmin(T &x, const T &y) { if (x > y) { x = y; return 1; } return 0; } template <typename T> inline bool chmax(T &x, const T &y) { if (x < y) { x = y; return 1; } return 0; } struct IOSET { IOSET() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(10); } } ioset; // --------------------------------------------------------------------- using pii = pair<int, int>; vector<pii> mymerge(vector<pii> x, vector<pii> y) { vector<pii> ans; auto iterx = x.begin(), itery = y.begin(); while (iterx != x.end() || itery != y.end()) { if (iterx == x.end()) { ans.push_back(*itery); ++itery; continue; } if (itery == y.end()) { ans.push_back(*iterx); ++iterx; continue; } if (*iterx < *itery) { ans.push_back(*iterx); ++iterx; } else { ans.push_back(*itery); ++itery; } } return ans; } int main() { int n; cin >> n; vector<int> v(n + 1, 0), w(n + 1, 0); rep(i, n) cin >> v[i + 1] >> w[i + 1]; vector<int> h(n + 1, 0); rep(i, 2, n + 1) h[i] = h[i / 2] + 1; int k = n / 2; vector<vector<pii>> vec(k); vec[0] = {pii(0, 0)}; rep(i, 1, k) { vector<pii> vec_ = vec[i / 2]; for (pii &p : vec_) { p.first += w[i]; p.second += v[i]; } vec[i] = mymerge(vec[i / 2], vec_); } rep(i, 1, k) rep(j, 1, sz(vec[i])) chmax(vec[i][j].second, vec[i][j - 1].second); int q; cin >> q; rep(_, q) { int u, l; cin >> u >> l; if (u < k) { int ans = 0; for (pii p : vec[u]) { if (p.first <= l) chmax(ans, p.second); } cout << ans << "\n"; continue; } vector<int> vs; while (u >= k) { vs.push_back(u); u /= 2; } reverse(all(vs)); vector<vector<pii>> vec2(sz(vs) + 1); vec2[0] = {pii(0, 0)}; rep(i, 1, sz(vs) + 1) { vector<pii> vec2_ = vec2[i - 1]; for (pii &p : vec2_) { p.first += w[vs[i - 1]]; p.second += v[vs[i - 1]]; } vec2[i] = mymerge(vec2[i - 1], vec2_); } rep(i, 1, sz(vec2[sz(vs)])) chmax(vec2[sz(vs)][i].second, vec2[sz(vs)][i - 1].second); int now = sz(vec2[sz(vs)]) - 1; int ans = 0; rep(i, sz(vec[u])) { while (now >= 0) { if (vec[u][i].first + vec2[sz(vs)][now].first <= l) { chmax(ans, vec[u][i].second + vec2[sz(vs)][now].second); break; } --now; } } cout << ans << "\n"; } return 0; }
// Template #include <bits/stdc++.h> #define rep_override(x, y, z, name, ...) name #define rep2(i, n) for (int i = 0; i < (n); ++i) #define rep3(i, l, r) for (int i = (l); i < (r); ++i) #define rep(...) rep_override(__VA_ARGS__, rep3, rep2)(__VA_ARGS__) #define all(x) (x).begin(), (x).end() #define sz(x) (int)(x).size() using namespace std; using ll = long long; constexpr int inf = 1001001001; constexpr ll INF = 3003003003003003003; template <typename T> inline bool chmin(T &x, const T &y) { if (x > y) { x = y; return 1; } return 0; } template <typename T> inline bool chmax(T &x, const T &y) { if (x < y) { x = y; return 1; } return 0; } struct IOSET { IOSET() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(10); } } ioset; // --------------------------------------------------------------------- using pii = pair<int, int>; vector<pii> mymerge(vector<pii> x, vector<pii> y) { vector<pii> ans; auto iterx = x.begin(), itery = y.begin(); while (iterx != x.end() || itery != y.end()) { if (iterx == x.end()) { ans.push_back(*itery); ++itery; continue; } if (itery == y.end()) { ans.push_back(*iterx); ++iterx; continue; } if (*iterx < *itery) { ans.push_back(*iterx); ++iterx; } else { ans.push_back(*itery); ++itery; } } return ans; } int main() { int n; cin >> n; vector<int> v(n + 1, 0), w(n + 1, 0); rep(i, n) cin >> v[i + 1] >> w[i + 1]; vector<int> h(n + 1, 0); rep(i, 2, n + 1) h[i] = h[i / 2] + 1; int k = max(1 << (h[n] / 2), 1); vector<vector<pii>> vec(k); vec[0] = {pii(0, 0)}; rep(i, 1, k) { vector<pii> vec_ = vec[i / 2]; for (pii &p : vec_) { p.first += w[i]; p.second += v[i]; } vec[i] = mymerge(vec[i / 2], vec_); } rep(i, 1, k) rep(j, 1, sz(vec[i])) chmax(vec[i][j].second, vec[i][j - 1].second); int q; cin >> q; rep(_, q) { int u, l; cin >> u >> l; if (u < k) { int ans = 0; for (pii p : vec[u]) { if (p.first <= l) chmax(ans, p.second); } cout << ans << "\n"; continue; } vector<int> vs; while (u >= k) { vs.push_back(u); u /= 2; } reverse(all(vs)); vector<vector<pii>> vec2(sz(vs) + 1); vec2[0] = {pii(0, 0)}; rep(i, 1, sz(vs) + 1) { vector<pii> vec2_ = vec2[i - 1]; for (pii &p : vec2_) { p.first += w[vs[i - 1]]; p.second += v[vs[i - 1]]; } vec2[i] = mymerge(vec2[i - 1], vec2_); } rep(i, 1, sz(vec2[sz(vs)])) chmax(vec2[sz(vs)][i].second, vec2[sz(vs)][i - 1].second); int now = sz(vec2[sz(vs)]) - 1; int ans = 0; rep(i, sz(vec[u])) { while (now >= 0) { if (vec[u][i].first + vec2[sz(vs)][now].first <= l) { chmax(ans, vec[u][i].second + vec2[sz(vs)][now].second); break; } --now; } } cout << ans << "\n"; } return 0; }
replace
71
72
71
72
0
p02648
C++
Time Limit Exceeded
#include <algorithm> #include <cmath> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> #define _repargs(_1, _2, _3, name, ...) name #define _rep(i, n) repi(i, 0, n) #define repi(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) #define rep(...) _repargs(__VA_ARGS__, repi, _rep, )(__VA_ARGS__) #define all(x) (x).begin(), (x).end() #define mod 1000000007 #define inf 2000000007 #define mp make_pair #define pb push_back typedef long long ll; using namespace std; template <typename T> inline void output(T a, int p = 0) { if (p) cout << fixed << setprecision(p) << a << "\n"; else cout << a << "\n"; } // end of template template <typename T> inline void voutput(T &v) { rep(i, v.size()) { if (i) cout << " " << v[i]; else cout << v[i]; } cout << endl; } const int H_DEPTH = 9; // 9 int main() { int N; cin >> N; vector<int> V(N), W(N); rep(i, N) cin >> V[i] >> W[i]; int Q; cin >> Q; vector<int> vtx(Q), wgt(Q); rep(i, Q) { cin >> vtx[i] >> wgt[i]; vtx[i]--; } if (N < (1 << H_DEPTH)) { // brute-force rep(i, Q) { vector<int> A; while (vtx[i] > 0) { A.pb(vtx[i]); vtx[i] = (vtx[i] - 1) / 2; } A.pb(0); int ans = 0; rep(j, (1 << A.size())) { int tv = 0; int tw = 0; rep(k, A.size()) { if ((j >> k) & 1) { tw += W[A[k]]; tv += V[A[k]]; } } if (tw <= wgt[i]) ans = max(ans, tv); } output(ans); } } else { vector<vector<pair<int, int>>> top(1 << H_DEPTH); // w, v; rep(i, 1 << H_DEPTH) { int cv = i; vector<int> A; while (cv > 0) { A.pb(cv); cv = (cv - 1) / 2; } A.pb(0); vector<pair<int, int>> wv; rep(j, (1 << A.size())) { int tv = 0; int tw = 0; rep(k, A.size()) { if ((j >> k) & 1) { tw += W[A[k]]; tv += V[A[k]]; } } wv.pb(mp(tw, tv)); } sort(all(wv)); vector<pair<int, int>> wvv; pair<int, int> cur = mp(-1, -1); rep(j, wv.size()) { if (cur.first == wv[j].first && cur.second < wv[j].second) { wvv[wvv.size() - 1] = wv[j]; cur = wv[j]; } else if (cur.second < wv[j].second) { wvv.pb(wv[j]); cur = wv[j]; } } top[i] = wvv; } rep(i, Q) { if (vtx[i] < (1 << H_DEPTH)) { vector<int> A; while (vtx[i] > 0) { A.pb(vtx[i]); vtx[i] = (vtx[i] - 1) / 2; } A.pb(0); int ans = 0; rep(j, (1 << A.size())) { int tv = 0; int tw = 0; rep(k, A.size()) { if ((j >> k) & 1) { tw += W[A[k]]; tv += V[A[k]]; } } if (tw <= wgt[i]) ans = max(ans, tv); } output(ans); } else { vector<int> A; while (vtx[i] >= (1 << H_DEPTH)) { A.pb(vtx[i]); vtx[i] = (vtx[i] - 1) / 2; } int ans = 0; rep(j, (1 << A.size())) { int tv = 0; int tw = 0; rep(k, A.size()) { if ((j >> k) & 1) { tw += W[A[k]]; tv += V[A[k]]; } } if (tw > wgt[i]) continue; int l = 0, r = (int)top[vtx[i]].size(); // ok, ng while (r - l > 1) { int m = (l + r) / 2; if (top[vtx[i]][m].first + tw <= wgt[i]) l = m; else r = m; } ans = max(ans, top[vtx[i]][l].second + tv); } output(ans); } } } return 0; }
#include <algorithm> #include <cmath> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> #define _repargs(_1, _2, _3, name, ...) name #define _rep(i, n) repi(i, 0, n) #define repi(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) #define rep(...) _repargs(__VA_ARGS__, repi, _rep, )(__VA_ARGS__) #define all(x) (x).begin(), (x).end() #define mod 1000000007 #define inf 2000000007 #define mp make_pair #define pb push_back typedef long long ll; using namespace std; template <typename T> inline void output(T a, int p = 0) { if (p) cout << fixed << setprecision(p) << a << "\n"; else cout << a << "\n"; } // end of template template <typename T> inline void voutput(T &v) { rep(i, v.size()) { if (i) cout << " " << v[i]; else cout << v[i]; } cout << endl; } const int H_DEPTH = 10; // 9 int main() { int N; cin >> N; vector<int> V(N), W(N); rep(i, N) cin >> V[i] >> W[i]; int Q; cin >> Q; vector<int> vtx(Q), wgt(Q); rep(i, Q) { cin >> vtx[i] >> wgt[i]; vtx[i]--; } if (N < (1 << H_DEPTH)) { // brute-force rep(i, Q) { vector<int> A; while (vtx[i] > 0) { A.pb(vtx[i]); vtx[i] = (vtx[i] - 1) / 2; } A.pb(0); int ans = 0; rep(j, (1 << A.size())) { int tv = 0; int tw = 0; rep(k, A.size()) { if ((j >> k) & 1) { tw += W[A[k]]; tv += V[A[k]]; } } if (tw <= wgt[i]) ans = max(ans, tv); } output(ans); } } else { vector<vector<pair<int, int>>> top(1 << H_DEPTH); // w, v; rep(i, 1 << H_DEPTH) { int cv = i; vector<int> A; while (cv > 0) { A.pb(cv); cv = (cv - 1) / 2; } A.pb(0); vector<pair<int, int>> wv; rep(j, (1 << A.size())) { int tv = 0; int tw = 0; rep(k, A.size()) { if ((j >> k) & 1) { tw += W[A[k]]; tv += V[A[k]]; } } wv.pb(mp(tw, tv)); } sort(all(wv)); vector<pair<int, int>> wvv; pair<int, int> cur = mp(-1, -1); rep(j, wv.size()) { if (cur.first == wv[j].first && cur.second < wv[j].second) { wvv[wvv.size() - 1] = wv[j]; cur = wv[j]; } else if (cur.second < wv[j].second) { wvv.pb(wv[j]); cur = wv[j]; } } top[i] = wvv; } rep(i, Q) { if (vtx[i] < (1 << H_DEPTH)) { vector<int> A; while (vtx[i] > 0) { A.pb(vtx[i]); vtx[i] = (vtx[i] - 1) / 2; } A.pb(0); int ans = 0; rep(j, (1 << A.size())) { int tv = 0; int tw = 0; rep(k, A.size()) { if ((j >> k) & 1) { tw += W[A[k]]; tv += V[A[k]]; } } if (tw <= wgt[i]) ans = max(ans, tv); } output(ans); } else { vector<int> A; while (vtx[i] >= (1 << H_DEPTH)) { A.pb(vtx[i]); vtx[i] = (vtx[i] - 1) / 2; } int ans = 0; rep(j, (1 << A.size())) { int tv = 0; int tw = 0; rep(k, A.size()) { if ((j >> k) & 1) { tw += W[A[k]]; tv += V[A[k]]; } } if (tw > wgt[i]) continue; int l = 0, r = (int)top[vtx[i]].size(); // ok, ng while (r - l > 1) { int m = (l + r) / 2; if (top[vtx[i]][m].first + tw <= wgt[i]) l = m; else r = m; } ans = max(ans, top[vtx[i]][l].second + tv); } output(ans); } } } return 0; }
replace
42
43
42
43
TLE
p02648
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n; cin >> n; vector<int> vs(n); vector<int> ws(n); for (int i = 0; i < n; i++) { cin >> vs[i] >> ws[i]; } int q; cin >> q; vector<vector<pair<int, int>>> rek(n); int han = 9; int sikiri = (1 << han) - 1; for (int i = 0; i < n; i++) { vector<int> ta, ta2; int cur = i; for (int j = 0; j < han; j++) { ta.push_back(ws[cur]); ta2.push_back(vs[cur]); int nex = (cur - 1) / 2; if (cur == 0) break; cur = nex; if (i >= sikiri && nex < sikiri) break; } vector<pair<int, int>> zen(1, make_pair(0, 0)); for (int j = 0; j < (int)ta.size(); j++) { vector<pair<int, int>> nzen = zen; for (int k = 0; k < (int)zen.size(); k++) { pair<int, int> p0 = make_pair(zen[k].first + ta[j], zen[k].second + ta2[j]); nzen.push_back(p0); } nzen.swap(zen); } sort(zen.begin(), zen.end()); int mx = 0; for (int i = 0; i < (int)zen.size(); i++) { mx = max(mx, zen[i].second); zen[i].second = mx; // cout << zen[i].first << " " << zen[i].second << "\n"; } // cout << "\n"; rek[i] = zen; } for (int tc = 0; tc < q; tc++) { int v, L; cin >> v >> L; v--; vector<int> left; for (int i = 0; i < (int)rek[v].size(); i++) { left.push_back(rek[v][i].first); } int cur = v; bool bo = true; for (int j = 0; j < han; j++) { int nex = (cur - 1) / 2; if (cur == 0) { bo = false; break; } cur = nex; if (v >= sikiri && nex < sikiri) break; } if (!bo) { int ind = (int)(upper_bound(left.begin(), left.end(), L) - left.begin()); if (ind == 0) { cout << 0 << "\n"; } else { ind--; cout << rek[v][ind].second << "\n"; } continue; } vector<int> right; for (int i = 0; i < (int)rek[cur].size(); i++) { right.push_back(rek[cur][i].first); } int res = 0; int rind = (int)right.size(); rind--; for (int i = 0; i < (int)left.size(); i++) { if (left[i] > L) break; long long dif = L - left[i]; while (rind >= 0 && right[rind] > dif) rind--; if (rind < 0) break; res = max(res, rek[v][i].second + rek[cur][rind].second); } cout << res << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n; cin >> n; vector<int> vs(n); vector<int> ws(n); for (int i = 0; i < n; i++) { cin >> vs[i] >> ws[i]; } int q; cin >> q; vector<vector<pair<int, int>>> rek(n); int han = 11; int sikiri = (1 << han) - 1; for (int i = 0; i < n; i++) { vector<int> ta, ta2; int cur = i; for (int j = 0; j < han; j++) { ta.push_back(ws[cur]); ta2.push_back(vs[cur]); int nex = (cur - 1) / 2; if (cur == 0) break; cur = nex; if (i >= sikiri && nex < sikiri) break; } vector<pair<int, int>> zen(1, make_pair(0, 0)); for (int j = 0; j < (int)ta.size(); j++) { vector<pair<int, int>> nzen = zen; for (int k = 0; k < (int)zen.size(); k++) { pair<int, int> p0 = make_pair(zen[k].first + ta[j], zen[k].second + ta2[j]); nzen.push_back(p0); } nzen.swap(zen); } sort(zen.begin(), zen.end()); int mx = 0; for (int i = 0; i < (int)zen.size(); i++) { mx = max(mx, zen[i].second); zen[i].second = mx; // cout << zen[i].first << " " << zen[i].second << "\n"; } // cout << "\n"; rek[i] = zen; } for (int tc = 0; tc < q; tc++) { int v, L; cin >> v >> L; v--; vector<int> left; for (int i = 0; i < (int)rek[v].size(); i++) { left.push_back(rek[v][i].first); } int cur = v; bool bo = true; for (int j = 0; j < han; j++) { int nex = (cur - 1) / 2; if (cur == 0) { bo = false; break; } cur = nex; if (v >= sikiri && nex < sikiri) break; } if (!bo) { int ind = (int)(upper_bound(left.begin(), left.end(), L) - left.begin()); if (ind == 0) { cout << 0 << "\n"; } else { ind--; cout << rek[v][ind].second << "\n"; } continue; } vector<int> right; for (int i = 0; i < (int)rek[cur].size(); i++) { right.push_back(rek[cur][i].first); } int res = 0; int rind = (int)right.size(); rind--; for (int i = 0; i < (int)left.size(); i++) { if (left[i] > L) break; long long dif = L - left[i]; while (rind >= 0 && right[rind] > dif) rind--; if (rind < 0) break; res = max(res, rek[v][i].second + rek[cur][rind].second); } cout << res << "\n"; } return 0; }
replace
19
20
19
20
TLE
p02648
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll N; vector<ll> V, W; ll Q; const ll K = 9, L = 100000; ll I; vector<vector<ll>> Dp; vector<pair<ll, ll>> C; ll solve(ll i, ll l) { C.clear(); C.push_back({l, 0}); while (i > I) { ll n = C.size(); for (ll k = 0; k < n; ++k) { ll l, v; tie(l, v) = C[k]; if (l >= W[i]) C.push_back({l - W[k], v + V[i]}); } i /= 2; } ll res = 0; for (auto lv : C) { ll l, v; tie(l, v) = lv; res = max(res, v + Dp[i][l]); } return res; } int main() { cin >> N; V.resize(N + 1); W.resize(N + 1); for (ll i = 1; i <= N; ++i) cin >> V[i] >> W[i]; I = min(N, (1ll << K) - 1); Dp.resize(I + 1); for (auto &dp : Dp) dp.resize(L + 1); for (ll i = 1; i <= I; ++i) { for (ll l = 0; l <= L; ++l) { Dp[i][l] = Dp[i / 2][l]; if (W[i] <= l) Dp[i][l] = max(Dp[i][l], Dp[i / 2][l - W[i]] + V[i]); } } cin >> Q; for (ll q = 0; q < Q; ++q) { ll i, l; cin >> i >> l; cout << solve(i, l) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll N; vector<ll> V, W; ll Q; const ll K = 9, L = 100000; ll I; vector<vector<ll>> Dp; vector<pair<ll, ll>> C; ll solve(ll i, ll l) { C.clear(); C.push_back({l, 0}); while (i > I) { ll n = C.size(); for (ll k = 0; k < n; ++k) { ll l, v; tie(l, v) = C[k]; if (l >= W[i]) C.push_back({l - W[i], v + V[i]}); } i /= 2; } ll res = 0; for (auto lv : C) { ll l, v; tie(l, v) = lv; res = max(res, v + Dp[i][l]); } return res; } int main() { cin >> N; V.resize(N + 1); W.resize(N + 1); for (ll i = 1; i <= N; ++i) cin >> V[i] >> W[i]; I = min(N, (1ll << K) - 1); Dp.resize(I + 1); for (auto &dp : Dp) dp.resize(L + 1); for (ll i = 1; i <= I; ++i) { for (ll l = 0; l <= L; ++l) { Dp[i][l] = Dp[i / 2][l]; if (W[i] <= l) Dp[i][l] = max(Dp[i][l], Dp[i / 2][l - W[i]] + V[i]); } } cin >> Q; for (ll q = 0; q < Q; ++q) { ll i, l; cin >> i >> l; cout << solve(i, l) << endl; } return 0; }
replace
22
23
22
23
0
p02648
C++
Time Limit Exceeded
#include <bits/stdc++.h> // #include <atcoder/all> #define For(i, a, b) for (int(i) = (int)(a); (i) < (int)(b); ++(i)) #define rFor(i, a, b) for (int(i) = (int)(a)-1; (i) >= (int)(b); --(i)) #define rep(i, n) For((i), 0, (n)) #define rrep(i, n) rFor((i), (n), 0) #define fi first #define se second using namespace std; typedef long long lint; typedef unsigned long long ulint; typedef pair<int, int> pii; typedef pair<lint, lint> pll; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } template <class T> T div_floor(T a, T b) { if (b < 0) a *= -1, b *= -1; return a >= 0 ? a / b : (a + 1) / b - 1; } template <class T> T div_ceil(T a, T b) { if (b < 0) a *= -1, b *= -1; return a > 0 ? (a - 1) / b + 1 : a / b; } constexpr lint mod = 1000000007; constexpr lint INF = mod * mod; constexpr int MAX = 100010; constexpr int h = 8; int n, q; lint v[1 << 18], w[1 << 18], dp[10][MAX]; vector<pii> qs[1 << 18]; lint ans[MAX]; void dfs(int t, int d) { if (d <= h) { dp[d][0] = 0; For(i, 1, MAX) { dp[d][i] = max(dp[d][i - 1], dp[d - 1][i]); if (i >= w[t]) chmax(dp[d][i], dp[d - 1][i - w[t]] + v[t]); } } for (auto [id, L] : qs[t]) { if (d <= h) ans[id] = dp[d][L]; else { ans[id] = 0; vector<lint> vs, ws; for (int td = d, tt = t; td > h; --td, tt /= 2) { vs.push_back(v[tt]); ws.push_back(w[tt]); } rep(S, 1 << vs.size()) { lint tv = 0, tw = 0; rep(i, vs.size()) if (S >> i & 1) { tv += vs[i]; tw += ws[i]; } if (tw <= L) chmax(ans[id], dp[h][L - tw] + tv); } } } if (t * 2 <= n) dfs(t * 2, d + 1); if (t * 2 + 1 <= n) dfs(t * 2 + 1, d + 1); } int main() { scanf("%d", &n); For(i, 1, n + 1) scanf("%lld%lld", &v[i], &w[i]); scanf("%d", &q); rep(i, q) { int t, L; scanf("%d%d", &t, &L); qs[t].emplace_back(i, L); } rep(i, MAX) dp[0][i] = 0; dfs(1, 1); rep(i, q) printf("%lld\n", ans[i]); }
#include <bits/stdc++.h> // #include <atcoder/all> #define For(i, a, b) for (int(i) = (int)(a); (i) < (int)(b); ++(i)) #define rFor(i, a, b) for (int(i) = (int)(a)-1; (i) >= (int)(b); --(i)) #define rep(i, n) For((i), 0, (n)) #define rrep(i, n) rFor((i), (n), 0) #define fi first #define se second using namespace std; typedef long long lint; typedef unsigned long long ulint; typedef pair<int, int> pii; typedef pair<lint, lint> pll; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } template <class T> T div_floor(T a, T b) { if (b < 0) a *= -1, b *= -1; return a >= 0 ? a / b : (a + 1) / b - 1; } template <class T> T div_ceil(T a, T b) { if (b < 0) a *= -1, b *= -1; return a > 0 ? (a - 1) / b + 1 : a / b; } constexpr lint mod = 1000000007; constexpr lint INF = mod * mod; constexpr int MAX = 100010; constexpr int h = 9; int n, q; lint v[1 << 18], w[1 << 18], dp[10][MAX]; vector<pii> qs[1 << 18]; lint ans[MAX]; void dfs(int t, int d) { if (d <= h) { dp[d][0] = 0; For(i, 1, MAX) { dp[d][i] = max(dp[d][i - 1], dp[d - 1][i]); if (i >= w[t]) chmax(dp[d][i], dp[d - 1][i - w[t]] + v[t]); } } for (auto [id, L] : qs[t]) { if (d <= h) ans[id] = dp[d][L]; else { ans[id] = 0; vector<lint> vs, ws; for (int td = d, tt = t; td > h; --td, tt /= 2) { vs.push_back(v[tt]); ws.push_back(w[tt]); } rep(S, 1 << vs.size()) { lint tv = 0, tw = 0; rep(i, vs.size()) if (S >> i & 1) { tv += vs[i]; tw += ws[i]; } if (tw <= L) chmax(ans[id], dp[h][L - tw] + tv); } } } if (t * 2 <= n) dfs(t * 2, d + 1); if (t * 2 + 1 <= n) dfs(t * 2 + 1, d + 1); } int main() { scanf("%d", &n); For(i, 1, n + 1) scanf("%lld%lld", &v[i], &w[i]); scanf("%d", &q); rep(i, q) { int t, L; scanf("%d%d", &t, &L); qs[t].emplace_back(i, L); } rep(i, MAX) dp[0][i] = 0; dfs(1, 1); rep(i, q) printf("%lld\n", ans[i]); }
replace
43
44
43
44
TLE
p02648
C++
Runtime Error
#include <bits/stdc++.h> #define pb push_back #define mp make_pair #define fi first #define se second #define SZ(x) ((int)x.size()) #define FOR(i, a, b) for (int i = a; i <= b; ++i) #define FORD(i, a, b) for (int i = a; i >= b; --i) using namespace std; typedef long long LL; typedef pair<int, int> pa; typedef vector<int> vec; void getint(int &v) { char ch, fu = 0; for (ch = '*'; (ch < '0' || ch > '9') && ch != '-'; ch = getchar()) ; if (ch == '-') fu = 1, ch = getchar(); for (v = 0; ch >= '0' && ch <= '9'; ch = getchar()) v = v * 10 + ch - '0'; if (fu) v = -v; } int f[520][100010]; int n, N, M, tmp[55], q, ts, vv[500010], ww[500010], v[500010], w[500010], o[500010], V, L, ans; int main() { cin >> n; FOR(i, 1, n) getint(v[i]), getint(w[i]); N = 1; while (1) { if (n > (1 << N) - 1) ++N; else break; } M = (1 << N) - 1; FOR(i, 1, M) { int fa = i >> 1; FOR(j, 0, 100000) f[i][j] = f[fa][j]; FOR(j, w[i], 100000) f[i][j] = max(f[i][j], f[fa][j - w[i]] + v[i]); } FOR(i, 1, 500000) { FOR(j, 0, 22) if ((i >> j) & 1) { o[i] = j; break; } } cin >> q; while (q--) { scanf("%d%d", &V, &L); if (V <= M) { printf("%d\n", f[V][L]); continue; } ts = 0; while (1) { if (V <= M) break; tmp[ts++] = V; V >>= 1; } vv[0] = 0; ww[0] = 0; FOR(i, 1, (1 << ts) - 1) { vv[i] = vv[i - (1 << o[i])] + v[tmp[o[i]]]; ww[i] = ww[i - (1 << o[i])] + w[tmp[o[i]]]; } ans = 0; FOR(i, 0, (1 << ts) - 1) if (ww[i] <= L) ans = max(ans, vv[i] + f[V][L - ww[i]]); printf("%d\n", ans); } return 0; }
#include <bits/stdc++.h> #define pb push_back #define mp make_pair #define fi first #define se second #define SZ(x) ((int)x.size()) #define FOR(i, a, b) for (int i = a; i <= b; ++i) #define FORD(i, a, b) for (int i = a; i >= b; --i) using namespace std; typedef long long LL; typedef pair<int, int> pa; typedef vector<int> vec; void getint(int &v) { char ch, fu = 0; for (ch = '*'; (ch < '0' || ch > '9') && ch != '-'; ch = getchar()) ; if (ch == '-') fu = 1, ch = getchar(); for (v = 0; ch >= '0' && ch <= '9'; ch = getchar()) v = v * 10 + ch - '0'; if (fu) v = -v; } int f[520][100010]; int n, N, M, tmp[55], q, ts, vv[500010], ww[500010], v[500010], w[500010], o[500010], V, L, ans; int main() { cin >> n; FOR(i, 1, n) getint(v[i]), getint(w[i]); N = 1; while (1) { if (n > (1 << N) - 1) ++N; else break; } N = (N + 1) / 2; M = (1 << N) - 1; FOR(i, 1, M) { int fa = i >> 1; FOR(j, 0, 100000) f[i][j] = f[fa][j]; FOR(j, w[i], 100000) f[i][j] = max(f[i][j], f[fa][j - w[i]] + v[i]); } FOR(i, 1, 500000) { FOR(j, 0, 22) if ((i >> j) & 1) { o[i] = j; break; } } cin >> q; while (q--) { scanf("%d%d", &V, &L); if (V <= M) { printf("%d\n", f[V][L]); continue; } ts = 0; while (1) { if (V <= M) break; tmp[ts++] = V; V >>= 1; } vv[0] = 0; ww[0] = 0; FOR(i, 1, (1 << ts) - 1) { vv[i] = vv[i - (1 << o[i])] + v[tmp[o[i]]]; ww[i] = ww[i - (1 << o[i])] + w[tmp[o[i]]]; } ans = 0; FOR(i, 0, (1 << ts) - 1) if (ww[i] <= L) ans = max(ans, vv[i] + f[V][L - ww[i]]); printf("%d\n", ans); } return 0; }
insert
36
36
36
37
-11
p02648
C++
Memory Limit Exceeded
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <cstdio> #include <deque> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #pragma GCC optimize("Ofast") #pragma GCC target("avx") using namespace std; #define REP(i, N) for (int i = 0; i < (int)N; i++) #define FOR(i, a, b) for (int i = a; i < (int)b; i++) using ll = long long; inline bool bit(ll x, int b) { return (x >> b) & 1; } constexpr int SZ = 100000; constexpr int prs = 11; ll pre[1 << prs][SZ + 1]; int main() { int N; cin >> N; vector<int> V(N + 1), W(N + 1); REP(i, N) cin >> V[i + 1] >> W[i + 1]; FOR(i, 1, min(N + 1, 1 << prs)) { if (i != 1) copy(pre[i / 2], pre[i / 2] + SZ + 1, pre[i]); for (int j = SZ; j >= W[i]; --j) pre[i][j] = max(pre[i][j], pre[i][j - W[i]] + V[i]); REP(j, SZ) pre[i][j + 1] = max(pre[i][j + 1], pre[i][j]); } int Q; cin >> Q; while (Q--) { int v, L; cin >> v >> L; if (v < (1 << prs)) { cout << pre[v][L] << "\n"; } else { int lvl = 32 - __builtin_clz(v) - prs; ll ans = 0; REP(b, 1 << lvl) { int w = 0; ll va = 0; REP(i, lvl) { if ((b >> i) & 1) { w += W[v >> i]; va += V[v >> i]; } if (w > L) break; } if (w <= L) ans = max(ans, pre[v >> lvl][L - w] + va); } cout << ans << "\n"; } } return 0; }
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <cstdio> #include <deque> #include <functional> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <vector> #pragma GCC optimize("Ofast") #pragma GCC target("avx") using namespace std; #define REP(i, N) for (int i = 0; i < (int)N; i++) #define FOR(i, a, b) for (int i = a; i < (int)b; i++) using ll = long long; inline bool bit(ll x, int b) { return (x >> b) & 1; } constexpr int SZ = 100000; constexpr int prs = 10; ll pre[1 << prs][SZ + 1]; int main() { int N; cin >> N; vector<int> V(N + 1), W(N + 1); REP(i, N) cin >> V[i + 1] >> W[i + 1]; FOR(i, 1, min(N + 1, 1 << prs)) { if (i != 1) copy(pre[i / 2], pre[i / 2] + SZ + 1, pre[i]); for (int j = SZ; j >= W[i]; --j) pre[i][j] = max(pre[i][j], pre[i][j - W[i]] + V[i]); REP(j, SZ) pre[i][j + 1] = max(pre[i][j + 1], pre[i][j]); } int Q; cin >> Q; while (Q--) { int v, L; cin >> v >> L; if (v < (1 << prs)) { cout << pre[v][L] << "\n"; } else { int lvl = 32 - __builtin_clz(v) - prs; ll ans = 0; REP(b, 1 << lvl) { int w = 0; ll va = 0; REP(i, lvl) { if ((b >> i) & 1) { w += W[v >> i]; va += V[v >> i]; } if (w > L) break; } if (w <= L) ans = max(ans, pre[v >> lvl][L - w] + va); } cout << ans << "\n"; } } return 0; }
replace
29
30
29
30
MLE
p02648
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; int main() { using item = pair<int, int>; constexpr int Wmax = 100'000; constexpr int Dmax = 18; constexpr int Dhalf = Dmax / 2; ios_base::sync_with_stdio(false); cin.tie(0); int N; cin >> N; vector<item> data(N + 1); for (int v = 1; v <= N; v++) { cin >> data.at(v).first >> data.at(v).second; } // auto trimming= [](vector<int> *output){ // int val_cummax= -1; // for(size_t w=0; w< output->size(); w++){ // val_cummax= max(val_cummax, output->at(w)); // output->at(w)= val_cummax; // } // }; const int Vupper = min(N + 1, 1 << Dhalf); vector<vector<int>> above_half(Vupper, vector<int>(Wmax + 1)); for (int v = 1; v < Vupper; v++) { above_half.at(v) = above_half.at(v / 2); auto [val_v, wgh_v] = data.at(v); for (int w = Wmax; w >= wgh_v; w--) { above_half.at(v).at(w) = max(above_half.at(v / 2).at(w), val_v + above_half.at(v / 2).at(w - wgh_v)); } // trimming(&above_half.at(v)); } int Q; cin >> Q; vector<int> answers(Q); int v_query; int W_query; for (int q = 0; q < Q; q++) { cin >> v_query >> W_query; if (v_query < Vupper) { answers.at(q) = above_half.at(v_query).at(W_query); } else { int D_query = (32 - __builtin_clz(v_query)) - Dhalf; int v_anc = v_query >> D_query; int ans = 0; for (int tmp = 0; tmp < (1 << D_query); tmp++) { bitset<Dhalf> bs(tmp); int wgh_lower = 0; int val_lower = 0; for (int i = 0; i < D_query; i++) { if (bs.test(i)) { wgh_lower += data.at((v_query >> i)).second; val_lower += data.at((v_query >> i)).first; } } if (wgh_lower <= W_query) { int val_upper = above_half.at(v_anc).at(W_query - wgh_lower); ans = max(ans, val_upper + val_lower); } } answers.at(q) = ans; } } for (auto ans : answers) { cout << ans << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { using item = pair<int, int>; constexpr int Wmax = 100'000; constexpr int Dmax = 18; constexpr int Dhalf = 10; ios_base::sync_with_stdio(false); cin.tie(0); int N; cin >> N; vector<item> data(N + 1); for (int v = 1; v <= N; v++) { cin >> data.at(v).first >> data.at(v).second; } // auto trimming= [](vector<int> *output){ // int val_cummax= -1; // for(size_t w=0; w< output->size(); w++){ // val_cummax= max(val_cummax, output->at(w)); // output->at(w)= val_cummax; // } // }; const int Vupper = min(N + 1, 1 << Dhalf); vector<vector<int>> above_half(Vupper, vector<int>(Wmax + 1)); for (int v = 1; v < Vupper; v++) { above_half.at(v) = above_half.at(v / 2); auto [val_v, wgh_v] = data.at(v); for (int w = Wmax; w >= wgh_v; w--) { above_half.at(v).at(w) = max(above_half.at(v / 2).at(w), val_v + above_half.at(v / 2).at(w - wgh_v)); } // trimming(&above_half.at(v)); } int Q; cin >> Q; vector<int> answers(Q); int v_query; int W_query; for (int q = 0; q < Q; q++) { cin >> v_query >> W_query; if (v_query < Vupper) { answers.at(q) = above_half.at(v_query).at(W_query); } else { int D_query = (32 - __builtin_clz(v_query)) - Dhalf; int v_anc = v_query >> D_query; int ans = 0; for (int tmp = 0; tmp < (1 << D_query); tmp++) { bitset<Dhalf> bs(tmp); int wgh_lower = 0; int val_lower = 0; for (int i = 0; i < D_query; i++) { if (bs.test(i)) { wgh_lower += data.at((v_query >> i)).second; val_lower += data.at((v_query >> i)).first; } } if (wgh_lower <= W_query) { int val_upper = above_half.at(v_anc).at(W_query - wgh_lower); ans = max(ans, val_upper + val_lower); } } answers.at(q) = ans; } } for (auto ans : answers) { cout << ans << endl; } }
replace
7
8
7
8
TLE
p02648
C++
Runtime Error
#include <cstdio> #include <cstring> #include <iostream> #include <vector> #define MAXN 515 #define MAXL 100005 using namespace std; int dp[MAXN][MAXL]; int main() { int n; scanf("%d", &n); vector<int> v(n + 1), w(n + 1); for (int i = 1; i <= n; ++i) scanf("%d%d", &v[i], &w[i]); memset(dp, 0, sizeof(dp)); for (int i = 1; i < min(n + 1, 1 << 9); ++i) { int p = i / 2; for (int j = 1; j < MAXL; ++j) dp[i][j] = max(dp[p][j], j >= w[i] ? dp[p][j - w[i]] + v[i] : 0); } int q; scanf("%d", &q); string ans; for (int i = 0; i < q; ++i) { int vi, l; scanf("%d%d", &vi, &l); if (vi < (1 << 9)) { ans += to_string(dp[vi][l]) + "\n"; continue; } vector<int> path; while (vi >= (1 << 9)) { path.emplace_back(vi); vi >>= 1; } int best = 0; int m = path.size(); for (int j = 0; j < (1 << m); ++j) { int vs = 0, ws = 0; for (int k = 0; k < m; ++k) if (j & (1 << k)) { vs += v[path[i]]; ws += w[path[i]]; } if (ws > l) continue; best = max(best, vs + dp[vi][l - ws]); } ans += to_string(best) + "\n"; } printf("%s", ans.c_str()); }
#include <cstdio> #include <cstring> #include <iostream> #include <vector> #define MAXN 515 #define MAXL 100005 using namespace std; int dp[MAXN][MAXL]; int main() { int n; scanf("%d", &n); vector<int> v(n + 1), w(n + 1); for (int i = 1; i <= n; ++i) scanf("%d%d", &v[i], &w[i]); memset(dp, 0, sizeof(dp)); for (int i = 1; i < min(n + 1, 1 << 9); ++i) { int p = i / 2; for (int j = 1; j < MAXL; ++j) dp[i][j] = max(dp[p][j], j >= w[i] ? dp[p][j - w[i]] + v[i] : 0); } int q; scanf("%d", &q); string ans; for (int i = 0; i < q; ++i) { int vi, l; scanf("%d%d", &vi, &l); if (vi < (1 << 9)) { ans += to_string(dp[vi][l]) + "\n"; continue; } vector<int> path; while (vi >= (1 << 9)) { path.emplace_back(vi); vi >>= 1; } int best = 0; int m = path.size(); for (int j = 0; j < (1 << m); ++j) { int vs = 0, ws = 0; for (int k = 0; k < m; ++k) if (j & (1 << k)) { vs += v[path[k]]; ws += w[path[k]]; } if (ws > l) continue; best = max(best, vs + dp[vi][l - ws]); } ans += to_string(best) + "\n"; } printf("%s", ans.c_str()); }
replace
42
44
42
44
-11
p02648
C++
Runtime Error
// clang-format off #include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i,n) for(int i=0;i<(int)(n);++i) #define all(x) (x).begin(),(x).end() #define pb push_back #define fi first #define se second #define dbg(x) cout<<#x" = "<<((x))<<endl template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;} template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;} // clang-format on const int U = 1 << 10 - 1; using pi = pair<int, int>; int main() { int n; scanf(" %d", &n); vector<int> v(n + 1), w(n + 1); rep(i, n) scanf(" %d %d", &v[i + 1], &w[i + 1]); vector<int> p(n + 1); p[1] = -1; for (int i = 2; i <= n; ++i) p[i] = i / 2; vector<int> dep(n + 1); dep[1] = 0; for (int i = 2; i <= n; ++i) dep[i] = dep[p[i]] + 1; vector<vector<pi>> up(U + 1); for (int i = 1; i <= min(U, n); ++i) { int x = i; vector<int> a; while (x > 0) { a.pb(x); x = p[x]; } const int A = a.size(); vector<pi> ss; rep(mask, 1 << A) { int tw = 0, tv = 0; rep(j, A) if (mask >> j & 1) { tw += w[a[j]]; tv += v[a[j]]; } ss.pb({tw, -tv}); } sort(all(ss)); up[i].pb({0, 0}); for (const auto &pp : ss) { int vv = -pp.se; if (up[i].back().se < vv) up[i].pb({pp.fi, vv}); } } int Q; scanf(" %d", &Q); rep(qqq, Q) { int x, L; scanf(" %d %d", &x, &L); int ans = 0; if (dep[x] <= 9) { auto itr = lower_bound(all(up[x]), pi(L + 1, 0)); --itr; ans = itr->se; } else { vector<int> a; while (dep[x] > 9) { a.pb(x); x = p[x]; } int A = a.size(); rep(mask, 1 << A) { int tw = 0, tv = 0; rep(i, A) { if (mask >> i & 1) { tw += w[a[i]]; tv += v[a[i]]; } } if (tw > L) continue; auto itr = lower_bound(all(up[x]), pi(L - tw + 1, 0)); --itr; ans = max(ans, tv + itr->se); } } printf("%d\n", ans); } return 0; }
// clang-format off #include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i,n) for(int i=0;i<(int)(n);++i) #define all(x) (x).begin(),(x).end() #define pb push_back #define fi first #define se second #define dbg(x) cout<<#x" = "<<((x))<<endl template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;} template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;} // clang-format on const int U = (1 << 10) - 1; using pi = pair<int, int>; int main() { int n; scanf(" %d", &n); vector<int> v(n + 1), w(n + 1); rep(i, n) scanf(" %d %d", &v[i + 1], &w[i + 1]); vector<int> p(n + 1); p[1] = -1; for (int i = 2; i <= n; ++i) p[i] = i / 2; vector<int> dep(n + 1); dep[1] = 0; for (int i = 2; i <= n; ++i) dep[i] = dep[p[i]] + 1; vector<vector<pi>> up(U + 1); for (int i = 1; i <= min(U, n); ++i) { int x = i; vector<int> a; while (x > 0) { a.pb(x); x = p[x]; } const int A = a.size(); vector<pi> ss; rep(mask, 1 << A) { int tw = 0, tv = 0; rep(j, A) if (mask >> j & 1) { tw += w[a[j]]; tv += v[a[j]]; } ss.pb({tw, -tv}); } sort(all(ss)); up[i].pb({0, 0}); for (const auto &pp : ss) { int vv = -pp.se; if (up[i].back().se < vv) up[i].pb({pp.fi, vv}); } } int Q; scanf(" %d", &Q); rep(qqq, Q) { int x, L; scanf(" %d %d", &x, &L); int ans = 0; if (dep[x] <= 9) { auto itr = lower_bound(all(up[x]), pi(L + 1, 0)); --itr; ans = itr->se; } else { vector<int> a; while (dep[x] > 9) { a.pb(x); x = p[x]; } int A = a.size(); rep(mask, 1 << A) { int tw = 0, tv = 0; rep(i, A) { if (mask >> i & 1) { tw += w[a[i]]; tv += v[a[i]]; } } if (tw > L) continue; auto itr = lower_bound(all(up[x]), pi(L - tw + 1, 0)); --itr; ans = max(ans, tv + itr->se); } } printf("%d\n", ans); } return 0; }
replace
14
15
14
15
0
p02648
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define REP(i, n) for (int i = 1; i < (int)(n); i++) #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define vout(x) rep(i, x.size()) cout << x[i] << " " template <class T> bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } template <class T> bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } using namespace std; using vint = vector<int>; using vvint = vector<vector<int>>; using ll = long long; using vll = vector<ll>; using vvll = vector<vector<ll>>; using P = pair<int, int>; const int inf = 1e9; const ll inf_l = 1e18; const int MAX = 1e5; const int lim = 1 << 9; map<int, vll> mp; vector<P> dat; int n; void dfs(int v) { if (v > lim || v > n) return; vll tmp = mp[v / 2]; for (int i = MAX; i >= dat[v - 1].second; i--) { chmax(tmp[i], tmp[i - dat[v - 1].second] + dat[v - 1].first); } mp[v] = tmp; dfs(v * 2); dfs(v * 2 + 1); } P get_top(int v) { int ct = 0; while (v > lim) { v >>= 1; ct++; } return P(ct, v); } int main() { cin >> n; dat.resize(n); rep(i, n) { int v, w; cin >> v >> w; dat[i] = P(v, w); } mp[0] = vll(MAX + 1, 0); dfs(1); int q; cin >> q; rep(i, q) { int v, l; cin >> v >> l; if (v <= lim) { cout << mp[v][l] << endl; } else { ll ans = 0; P p = get_top(v); int top = p.second; int ct = p.first; rep(i, 1 << ct) { int tmp = v; int V = 0, W = 0; rep(j, ct) { if (i >> j & 1) { V += dat[tmp - 1].first; W += dat[tmp - 1].second; } tmp /= 2; } if (l - W >= 0) chmax(ans, mp[top][l - W] + V); } cout << ans << endl; } } }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define REP(i, n) for (int i = 1; i < (int)(n); i++) #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define vout(x) rep(i, x.size()) cout << x[i] << " " template <class T> bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } template <class T> bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } using namespace std; using vint = vector<int>; using vvint = vector<vector<int>>; using ll = long long; using vll = vector<ll>; using vvll = vector<vector<ll>>; using P = pair<int, int>; const int inf = 1e9; const ll inf_l = 1e18; const int MAX = 1e5; const int lim = 1 << 10; map<int, vll> mp; vector<P> dat; int n; void dfs(int v) { if (v > lim || v > n) return; vll tmp = mp[v / 2]; for (int i = MAX; i >= dat[v - 1].second; i--) { chmax(tmp[i], tmp[i - dat[v - 1].second] + dat[v - 1].first); } mp[v] = tmp; dfs(v * 2); dfs(v * 2 + 1); } P get_top(int v) { int ct = 0; while (v > lim) { v >>= 1; ct++; } return P(ct, v); } int main() { cin >> n; dat.resize(n); rep(i, n) { int v, w; cin >> v >> w; dat[i] = P(v, w); } mp[0] = vll(MAX + 1, 0); dfs(1); int q; cin >> q; rep(i, q) { int v, l; cin >> v >> l; if (v <= lim) { cout << mp[v][l] << endl; } else { ll ans = 0; P p = get_top(v); int top = p.second; int ct = p.first; rep(i, 1 << ct) { int tmp = v; int V = 0, W = 0; rep(j, ct) { if (i >> j & 1) { V += dat[tmp - 1].first; W += dat[tmp - 1].second; } tmp /= 2; } if (l - W >= 0) chmax(ans, mp[top][l - W] + V); } cout << ans << endl; } } }
replace
31
32
31
32
TLE
p02648
C++
Time Limit Exceeded
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> using namespace std; typedef long long ll; int n, Q; #define Maxn 100010 int V[Maxn * 10], W[Maxn * 10]; int f[512][100010]; int seq1[Maxn], seq2[Maxn], cnt; int to[Maxn]; int val1[Maxn], val2[Maxn]; void dfs(int u) { if (u >= 512) return; if (u > n) return; int fa = u / 2; for (int i = 0; i <= 100000; ++i) { f[u][i] = f[fa][i]; if (i >= W[u]) f[u][i] = max(f[u][i], f[fa][i - W[u]] + V[u]); } dfs(u << 1); dfs(u << 1 | 1); } int lowbit(int x) { return x & (-x); } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d%d", &V[i], &W[i]); } dfs(1); scanf("%d", &Q); int u, L; for (int i = 0; i < 9; ++i) to[(1 << i)] = i; while (Q--) { scanf("%d%d", &u, &L); if (u < 512) printf("%d\n", f[u][L]); else { cnt = 0; while (u >= 4) { seq1[cnt] = W[u]; seq2[cnt] = V[u]; u >>= 1; cnt++; } int Ans = f[u][L]; for (int i = 1; i < (1 << cnt); ++i) { int d = lowbit(i); val1[i] = val1[i ^ d] + seq1[to[d]]; val2[i] = val2[i ^ d] + seq2[to[d]]; if (val1[i] <= L) Ans = max(Ans, f[u][L - val1[i]] + val2[i]); } printf("%d\n", Ans); } } return 0; }
#include <algorithm> #include <cstdio> #include <cstring> #include <iostream> using namespace std; typedef long long ll; int n, Q; #define Maxn 100010 int V[Maxn * 10], W[Maxn * 10]; int f[512][100010]; int seq1[Maxn], seq2[Maxn], cnt; int to[Maxn]; int val1[Maxn], val2[Maxn]; void dfs(int u) { if (u >= 512) return; if (u > n) return; int fa = u / 2; for (int i = 0; i <= 100000; ++i) { f[u][i] = f[fa][i]; if (i >= W[u]) f[u][i] = max(f[u][i], f[fa][i - W[u]] + V[u]); } dfs(u << 1); dfs(u << 1 | 1); } int lowbit(int x) { return x & (-x); } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d%d", &V[i], &W[i]); } dfs(1); scanf("%d", &Q); int u, L; for (int i = 0; i < 9; ++i) to[(1 << i)] = i; while (Q--) { scanf("%d%d", &u, &L); if (u < 512) printf("%d\n", f[u][L]); else { cnt = 0; while (u >= 512) { seq1[cnt] = W[u]; seq2[cnt] = V[u]; u >>= 1; cnt++; } int Ans = f[u][L]; for (int i = 1; i < (1 << cnt); ++i) { int d = lowbit(i); val1[i] = val1[i ^ d] + seq1[to[d]]; val2[i] = val2[i ^ d] + seq2[to[d]]; if (val1[i] <= L) Ans = max(Ans, f[u][L - val1[i]] + val2[i]); } printf("%d\n", Ans); } } return 0; }
replace
46
47
46
47
TLE
p02648
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define REP(i, n) for (int i = 1; i < (int)(n); i++) #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define vout(x) rep(i, x.size()) cout << x[i] << " " template <class T> bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } template <class T> bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } using namespace std; using vint = vector<int>; using vvint = vector<vector<int>>; using ll = long long; using vll = vector<ll>; using vvll = vector<vector<ll>>; using P = pair<int, int>; const int inf = 1e9; const ll inf_l = 1e18; const int MAX = 1e5; const int lim = 1 << 9; ll dp[lim + 1][MAX + 1]; vector<P> dat; int n; void dfs(int v) { if (v > lim || v > n) return; memcpy(dp[v], dp[v / 2], sizeof(dp[v])); for (int i = MAX; i >= dat[v - 1].second; i--) { chmax(dp[v][i], dp[v / 2][i - dat[v - 1].second] + dat[v - 1].first); } dfs(v * 2); dfs(v * 2 + 1); } P get_top(int v) { int ct = 0; while (v > lim) { v >>= 1; ct++; } return P(ct, v); } int main() { cin >> n; dat.resize(n); rep(i, n) { int v, w; cin >> v >> w; dat[i] = P(v, w); } memset(dp[0], 0, sizeof(dp[0])); dfs(1); int q; cin >> q; rep(i, q) { int v, l; cin >> v >> l; if (v <= lim) { cout << dp[v][l] << endl; } else { ll ans = 0; P p = get_top(v); int top = p.second; int ct = p.first; rep(i, 1 << ct) { int tmp = v; int V = 0, W = 0; rep(j, ct) { if (i >> j & 1) { V += dat[tmp - 1].first; W += dat[tmp - 1].second; } tmp /= 2; } if (l - W >= 0) chmax(ans, dp[top][l - W] + V); } cout << ans << endl; } } }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define REP(i, n) for (int i = 1; i < (int)(n); i++) #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define vout(x) rep(i, x.size()) cout << x[i] << " " template <class T> bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } template <class T> bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } using namespace std; using vint = vector<int>; using vvint = vector<vector<int>>; using ll = long long; using vll = vector<ll>; using vvll = vector<vector<ll>>; using P = pair<int, int>; const int inf = 1e9; const ll inf_l = 1e18; const int MAX = 1e5; const int lim = 1 << 10; ll dp[lim + 1][MAX + 1]; vector<P> dat; int n; void dfs(int v) { if (v > lim || v > n) return; memcpy(dp[v], dp[v / 2], sizeof(dp[v])); for (int i = MAX; i >= dat[v - 1].second; i--) { chmax(dp[v][i], dp[v / 2][i - dat[v - 1].second] + dat[v - 1].first); } dfs(v * 2); dfs(v * 2 + 1); } P get_top(int v) { int ct = 0; while (v > lim) { v >>= 1; ct++; } return P(ct, v); } int main() { cin >> n; dat.resize(n); rep(i, n) { int v, w; cin >> v >> w; dat[i] = P(v, w); } memset(dp[0], 0, sizeof(dp[0])); dfs(1); int q; cin >> q; rep(i, q) { int v, l; cin >> v >> l; if (v <= lim) { cout << dp[v][l] << endl; } else { ll ans = 0; P p = get_top(v); int top = p.second; int ct = p.first; rep(i, 1 << ct) { int tmp = v; int V = 0, W = 0; rep(j, ct) { if (i >> j & 1) { V += dat[tmp - 1].first; W += dat[tmp - 1].second; } tmp /= 2; } if (l - W >= 0) chmax(ans, dp[top][l - W] + V); } cout << ans << endl; } } }
replace
31
32
31
32
TLE
p02648
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using vl = vector<ll>; template <class T> using vc = vector<T>; template <class T> using vvc = vector<vector<T>>; #define eb emplace_back #define all(x) (x).begin(), (x).end() #define rep(i, n) for (ll i = 0; i < (n); i++) #define repr(i, n) for (ll i = (n)-1; i >= 0; i--) #define repe(i, l, r) for (ll i = (l); i < (r); i++) #define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--) #define repa(i, n) for (auto &i : n) template <class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } void init() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } #ifdef DEBUG template <class T, class N> void verr(const T &a, const N &n) { rep(i, n) cerr << a[i] << " "; cerr << "\n" << flush; } ll dbgt = 1; void err() { cerr << "passed " << dbgt++ << "\n" << flush; } template <class H, class... T> void err(H &&h, T &&...t) { cerr << h << (sizeof...(t) ? " " : "\n") << flush; if (sizeof...(t) > 0) err(forward<T>(t)...); } #endif const ll INF = 5e18; const ld EPS = 1e-11; const ld PI = acos(-1.0L); const ll MOD = 1e9 + 7; // const ll MOD = 998244353; //--------------------------------------------------------------------------------// ll V[1 << 18], W[1 << 18]; pair<ll, ll> DP[2050][2100]; int main() { init(); ll N; cin >> N; rep(i, N) cin >> V[i] >> W[i]; ll Q; cin >> Q; pair<ll, ll> m1[1 << 11], m2[1 << 11]; ll vv[20], ww[20]; if (N >= 1 << 11) { repe(di, 1024, min(N, 2047ll)) { ll v = di, vi = 0; while (v) { vv[vi] = V[v - 1], ww[vi] = W[v - 1]; v /= 2, vi++; } rep(i, 1 << 11) { ll ws = 0, vs = 0; rep(j, 11) { if (1 << j & i) { ws += ww[j], vs += vv[j]; } } DP[di][i] = {ws, vs}; } DP[di][1 << 11] = {INF, INF}; sort(DP[di], DP[di] + (1 << 11)); ll t = 0; rep(i, 1 << 11) { if (!chmax(t, DP[di][i].second)) DP[di][i].second = t; } } } rep(_, Q) { ll v, pv, L; cin >> v >> L; pv = v; if (v < 2048) { ll vi = 0; while (v) { vv[vi] = V[v - 1], ww[vi] = W[v - 1]; v /= 2, vi++; } ll s1 = vi / 2, s2 = (vi + 1) / 2; rep(i, 1 << s1) { ll vs = 0, ws = 0; rep(j, s1) { if (1 << j & i) vs += vv[j], ws += ww[j]; } m1[i] = {ws, vs}; } rep(i, 1 << s2) { ll vs = 0, ws = 0; rep(j, s2) { if (1 << j & i) vs += vv[s1 + j], ws += ww[s1 + j]; } m2[i] = {ws, vs}; } m2[1 << s2] = {INF, INF}; sort(m1, m1 + (1 << s1)), sort(m2, m2 + (1 << s2) + 1); ll t = 0; rep(i, 1 << s1) { if (!chmax(t, m1[i].second)) m1[i].second = t; } t = 0; rep(i, 1 << s2) { if (!chmax(t, m2[i].second)) m2[i].second = t; } ll ans = 0; ll i2 = 0; while (m1[0].first + m2[i2 + 1].first <= L) i2++; rep(i1, 1 << s1) { if (m1[i1].first > L) break; while (m1[i1].first + m2[i2].first > L) i2--; chmax(ans, m1[i1].second + m2[i2].second); } cout << ans << '\n'; } else { ll vi = 0; while (v) { vv[vi] = V[v - 1], ww[vi] = W[v - 1]; v /= 2, vi++; if (v < 2048) break; } ll s = vi; rep(i, 1 << s) { ll ws = 0, vs = 0; rep(j, s) { if (1 << j & i) { ws += ww[j], vs += vv[j]; } } m1[i] = {ws, vs}; } sort(m1, m1 + (1 << s)); ll t = 0; rep(i, 1 << s) { if (!chmax(t, m1[i].second)) m1[i].second = t; } ll ans = 0; ll i2 = 0; while (m1[0].first + DP[v][i2 + 1].first <= L) i2++; rep(i1, 1 << s) { if (m1[i1].first > L) break; while (m1[i1].first + DP[v][i2].first > L) i2--; chmax(ans, m1[i1].second + DP[v][i2].second); } cout << ans << '\n'; } } }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using vl = vector<ll>; template <class T> using vc = vector<T>; template <class T> using vvc = vector<vector<T>>; #define eb emplace_back #define all(x) (x).begin(), (x).end() #define rep(i, n) for (ll i = 0; i < (n); i++) #define repr(i, n) for (ll i = (n)-1; i >= 0; i--) #define repe(i, l, r) for (ll i = (l); i < (r); i++) #define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--) #define repa(i, n) for (auto &i : n) template <class T> inline bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } void init() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } #ifdef DEBUG template <class T, class N> void verr(const T &a, const N &n) { rep(i, n) cerr << a[i] << " "; cerr << "\n" << flush; } ll dbgt = 1; void err() { cerr << "passed " << dbgt++ << "\n" << flush; } template <class H, class... T> void err(H &&h, T &&...t) { cerr << h << (sizeof...(t) ? " " : "\n") << flush; if (sizeof...(t) > 0) err(forward<T>(t)...); } #endif const ll INF = 5e18; const ld EPS = 1e-11; const ld PI = acos(-1.0L); const ll MOD = 1e9 + 7; // const ll MOD = 998244353; //--------------------------------------------------------------------------------// ll V[1 << 18], W[1 << 18]; pair<ll, ll> DP[2050][2100]; int main() { init(); ll N; cin >> N; rep(i, N) cin >> V[i] >> W[i]; ll Q; cin >> Q; pair<ll, ll> m1[1 << 11], m2[1 << 11]; ll vv[20], ww[20]; if (N >= 1 << 11) { repe(di, 1024, min(N + 1, 2048ll)) { ll v = di, vi = 0; while (v) { vv[vi] = V[v - 1], ww[vi] = W[v - 1]; v /= 2, vi++; } rep(i, 1 << 11) { ll ws = 0, vs = 0; rep(j, 11) { if (1 << j & i) { ws += ww[j], vs += vv[j]; } } DP[di][i] = {ws, vs}; } DP[di][1 << 11] = {INF, INF}; sort(DP[di], DP[di] + (1 << 11)); ll t = 0; rep(i, 1 << 11) { if (!chmax(t, DP[di][i].second)) DP[di][i].second = t; } } } rep(_, Q) { ll v, pv, L; cin >> v >> L; pv = v; if (v < 2048) { ll vi = 0; while (v) { vv[vi] = V[v - 1], ww[vi] = W[v - 1]; v /= 2, vi++; } ll s1 = vi / 2, s2 = (vi + 1) / 2; rep(i, 1 << s1) { ll vs = 0, ws = 0; rep(j, s1) { if (1 << j & i) vs += vv[j], ws += ww[j]; } m1[i] = {ws, vs}; } rep(i, 1 << s2) { ll vs = 0, ws = 0; rep(j, s2) { if (1 << j & i) vs += vv[s1 + j], ws += ww[s1 + j]; } m2[i] = {ws, vs}; } m2[1 << s2] = {INF, INF}; sort(m1, m1 + (1 << s1)), sort(m2, m2 + (1 << s2) + 1); ll t = 0; rep(i, 1 << s1) { if (!chmax(t, m1[i].second)) m1[i].second = t; } t = 0; rep(i, 1 << s2) { if (!chmax(t, m2[i].second)) m2[i].second = t; } ll ans = 0; ll i2 = 0; while (m1[0].first + m2[i2 + 1].first <= L) i2++; rep(i1, 1 << s1) { if (m1[i1].first > L) break; while (m1[i1].first + m2[i2].first > L) i2--; chmax(ans, m1[i1].second + m2[i2].second); } cout << ans << '\n'; } else { ll vi = 0; while (v) { vv[vi] = V[v - 1], ww[vi] = W[v - 1]; v /= 2, vi++; if (v < 2048) break; } ll s = vi; rep(i, 1 << s) { ll ws = 0, vs = 0; rep(j, s) { if (1 << j & i) { ws += ww[j], vs += vv[j]; } } m1[i] = {ws, vs}; } sort(m1, m1 + (1 << s)); ll t = 0; rep(i, 1 << s) { if (!chmax(t, m1[i].second)) m1[i].second = t; } ll ans = 0; ll i2 = 0; while (m1[0].first + DP[v][i2 + 1].first <= L) i2++; rep(i1, 1 << s) { if (m1[i1].first > L) break; while (m1[i1].first + DP[v][i2].first > L) i2--; chmax(ans, m1[i1].second + DP[v][i2].second); } cout << ans << '\n'; } } }
replace
71
72
71
72
0
p02648
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; const int N = 2e5 + 10, mod = 1e9 + 7, M = 1e5; vector<pii> Q[N], vals, vals2; int w[N], val[N], n; ll dp[M + 10], ans[N]; void dfs(int v, int h) { int num = 0; if (h < 9) { int n = vals.size(); vals.push_back({w[v], val[v]}); for (int i = 0; i < n; i++) vals.push_back({vals[i].first + w[v], vals[i].second + val[v]}); num = n + 1; } else { int n = vals2.size(); vals2.push_back({w[v], val[v]}); for (int i = 0; i < n; i++) vals2.push_back({vals2[i].first + w[v], vals2[i].second + val[v]}); num = n + 1; } if (h == 8) { vector<pii> cpy; cpy = vals; sort(cpy.begin(), cpy.end()); cpy.resize(unique(cpy.begin(), cpy.end()) - cpy.begin()); int lst = 0; int cur = 0; for (int i = 0; i <= M; i++) { while (cur < cpy.size() && cpy[cur].first == i) { lst = max(lst, cpy[cur].second); cur++; } dp[i] = lst; } } if (h < 9) { for (pii x : Q[v]) for (pii y : vals) if (y.first <= x.first) ans[x.second] = max(ans[x.second], 1ll * y.second); } else { for (pii x : Q[v]) { ans[x.second] = dp[x.first]; for (pii y : vals2) if (y.first <= x.first) ans[x.second] = max(ans[x.second], y.second + dp[x.first - y.first]); } } if (2 * v <= n) dfs(2 * v, h + 1); if (2 * v + 1 <= n) dfs(2 * v + 1, h + 1); if (h < 9) { while (num) { num--; vals.pop_back(); } } else { while (num) { num--; vals2.pop_back(); } } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) cin >> val[i] >> w[i]; int q; cin >> q; for (int i = 0; i < q; i++) { int v, l; cin >> v >> l; Q[v].push_back({l, i}); } dfs(1, 0); for (int i = 0; i < q; i++) cout << ans[i] << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; const int N = 3e5 + 10, mod = 1e9 + 7, M = 1e5; vector<pii> Q[N], vals, vals2; int w[N], val[N], n; ll dp[M + 10], ans[N]; void dfs(int v, int h) { int num = 0; if (h < 9) { int n = vals.size(); vals.push_back({w[v], val[v]}); for (int i = 0; i < n; i++) vals.push_back({vals[i].first + w[v], vals[i].second + val[v]}); num = n + 1; } else { int n = vals2.size(); vals2.push_back({w[v], val[v]}); for (int i = 0; i < n; i++) vals2.push_back({vals2[i].first + w[v], vals2[i].second + val[v]}); num = n + 1; } if (h == 8) { vector<pii> cpy; cpy = vals; sort(cpy.begin(), cpy.end()); cpy.resize(unique(cpy.begin(), cpy.end()) - cpy.begin()); int lst = 0; int cur = 0; for (int i = 0; i <= M; i++) { while (cur < cpy.size() && cpy[cur].first == i) { lst = max(lst, cpy[cur].second); cur++; } dp[i] = lst; } } if (h < 9) { for (pii x : Q[v]) for (pii y : vals) if (y.first <= x.first) ans[x.second] = max(ans[x.second], 1ll * y.second); } else { for (pii x : Q[v]) { ans[x.second] = dp[x.first]; for (pii y : vals2) if (y.first <= x.first) ans[x.second] = max(ans[x.second], y.second + dp[x.first - y.first]); } } if (2 * v <= n) dfs(2 * v, h + 1); if (2 * v + 1 <= n) dfs(2 * v + 1, h + 1); if (h < 9) { while (num) { num--; vals.pop_back(); } } else { while (num) { num--; vals2.pop_back(); } } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) cin >> val[i] >> w[i]; int q; cin >> q; for (int i = 0; i < q; i++) { int v, l; cin >> v >> l; Q[v].push_back({l, i}); } dfs(1, 0); for (int i = 0; i < q; i++) cout << ans[i] << "\n"; return 0; }
replace
6
7
6
7
0
p02648
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; const int MAXN = 3e5 + 5; const int MAXV = 1e5 + 5; const int MAXM = 1 << 9; typedef long long ll; template <typename T> void chkmax(T &x, T y) { x = max(x, y); } template <typename T> void chkmin(T &x, T y) { x = min(x, y); } template <typename T> void read(T &x) { x = 0; int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; x *= f; } int a[MAXM][MAXV]; int n, m, r, q, v[MAXN], w[MAXN]; int query(int x, int y) { if (x <= m) return a[x][y]; else { int ans = query(x / 2, y); if (y >= w[x]) chkmax(ans, v[x] + query(x / 2, y - w[x])); return ans; } } int main() { read(n), m = n, r = (1 << 9) - 1; for (int i = 1; i <= n; i++) read(v[i]), read(w[i]); for (int i = 1; i <= m; i++) { int f = i / 2; for (int j = 1; j <= r; j++) { a[i][j] = a[f][j]; if (j >= w[i]) chkmax(a[i][j], v[i] + a[f][j - w[i]]); } } read(q); for (int i = 1; i <= q; i++) { int x, y; read(x), read(y); printf("%d\n", query(x, y)); } return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 3e5 + 5; const int MAXV = 1e5 + 5; const int MAXM = 1 << 9; typedef long long ll; template <typename T> void chkmax(T &x, T y) { x = max(x, y); } template <typename T> void chkmin(T &x, T y) { x = min(x, y); } template <typename T> void read(T &x) { x = 0; int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; x *= f; } int a[MAXM][MAXV]; int n, m, r, q, v[MAXN], w[MAXN]; int query(int x, int y) { if (x <= m) return a[x][y]; else { int ans = query(x / 2, y); if (y >= w[x]) chkmax(ans, v[x] + query(x / 2, y - w[x])); return ans; } } int main() { read(n), m = (1 << 9) - 1, r = 1e5; for (int i = 1; i <= n; i++) read(v[i]), read(w[i]); for (int i = 1; i <= m; i++) { int f = i / 2; for (int j = 1; j <= r; j++) { a[i][j] = a[f][j]; if (j >= w[i]) chkmax(a[i][j], v[i] + a[f][j - w[i]]); } } read(q); for (int i = 1; i <= q; i++) { int x, y; read(x), read(y); printf("%d\n", query(x, y)); } return 0; }
replace
32
33
32
33
-11
p02648
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using lint = long long; const lint inf = 1LL << 60; const lint mod = 1000000007; template <class T> bool chmax(T &a, const T &b) { return (a < b) ? (a = b, 1) : 0; } template <class T> bool chmin(T &a, const T &b) { return (b < a) ? (a = b, 1) : 0; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); lint n; cin >> n; vector<lint> V(n + 1), W(n + 1); for (int i = 1; i < n + 1; ++i) { cin >> V[i] >> W[i]; } lint q; cin >> q; vector<lint> v(q + 1), l(q + 1); vector<bool> calc(1000000, false); lint up = 1 << 10; vector<vector<pair<int, pair<int, int>>>> qs(1000000); for (int i = 0; i < q; ++i) { cin >> v[i] >> l[i]; int c = v[i]; // ls[c] = {i, l[i]}; bool added = false; if (c < (up)) { qs[c].push_back({i, {v[i], l[i]}}); added = true; } while (c != 0) { if (!added && (c < (up))) { qs[c].push_back({i, {v[i], l[i]}}); added = true; } if (c < (up)) { calc[c] = true; } c /= 2; } } // 1 - indexed!!!!!! vector<lint> ret(q, 0); function<void(int, map<lint, lint> &)> dfs = [&](int c, map<lint, lint> &dp) { map<lint, lint> to; to[0] = 0; for (auto &d : dp) { chmax(to[d.first], d.second); if (d.first + W[c] <= 100000) chmax(to[d.first + W[c]], d.second + V[c]); } if (qs[c].size() != 0) { lint mx = 0; map<lint, lint> tod = to; tod[0] = 0; for (auto it = tod.begin(); it != tod.end(); it = next(it)) { chmax(it->second, mx); chmax(mx, it->second); } for (auto &p : qs[c]) { vector<lint> vv = {0}, ww = {0}; lint curr = p.second.first; while (curr > c) { vv.push_back(V[curr]); ww.push_back(W[curr]); curr /= 2; } int k = vv.size(); lint r = 0; for (int j = 0; j < 1 << k; ++j) { lint vs = 0; lint ws = 0; for (int x = 0; x < k; ++x) { if (j >> x & 1) { vs += vv[x]; ws += ww[x]; } } if (ws <= p.second.second) { auto it = tod.upper_bound(p.second.second - ws); if (it == tod.begin()) continue; lint mx = prev(it)->second; chmax(r, vs + mx); } } ret[p.first] = r; } } for (auto &v : {c * 2, c * 2 + 1}) { if ((v > n) || (v >= (up)) || !calc[v]) { continue; } dfs(v, to); } }; map<lint, lint> vv; vv[0] = 0; dfs(1, vv); for (int i = 0; i < q; ++i) { cout << ret[i] << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; using lint = long long; const lint inf = 1LL << 60; const lint mod = 1000000007; template <class T> bool chmax(T &a, const T &b) { return (a < b) ? (a = b, 1) : 0; } template <class T> bool chmin(T &a, const T &b) { return (b < a) ? (a = b, 1) : 0; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); lint n; cin >> n; vector<lint> V(n + 1), W(n + 1); for (int i = 1; i < n + 1; ++i) { cin >> V[i] >> W[i]; } lint q; cin >> q; vector<lint> v(q + 1), l(q + 1); vector<bool> calc(1000000, false); lint up = 1 << 10; vector<vector<pair<int, pair<int, int>>>> qs(1000000); for (int i = 0; i < q; ++i) { cin >> v[i] >> l[i]; int c = v[i]; // ls[c] = {i, l[i]}; bool added = false; if (c < (up)) { qs[c].push_back({i, {v[i], l[i]}}); added = true; } while (c != 0) { if (!added && (c < (up))) { qs[c].push_back({i, {v[i], l[i]}}); added = true; } if (c < (up)) { calc[c] = true; } c /= 2; } } // 1 - indexed!!!!!! vector<lint> ret(q, 0); function<void(int, map<lint, lint> &)> dfs = [&](int c, map<lint, lint> &dp) { map<lint, lint> to; to[0] = 0; for (auto &d : dp) { chmax(to[d.first], d.second); if (d.first + W[c] <= 100000) chmax(to[d.first + W[c]], d.second + V[c]); } if (qs[c].size() != 0) { lint mx = 0; map<lint, lint> tod = to; tod[0] = 0; for (auto it = tod.begin(); it != tod.end(); it = next(it)) { chmax(it->second, mx); chmax(mx, it->second); } for (auto &p : qs[c]) { vector<lint> vv, ww; lint curr = p.second.first; while (curr > c) { vv.push_back(V[curr]); ww.push_back(W[curr]); curr /= 2; } int k = vv.size(); lint r = 0; for (int j = 0; j < 1 << k; ++j) { lint vs = 0; lint ws = 0; for (int x = 0; x < k; ++x) { if (j >> x & 1) { vs += vv[x]; ws += ww[x]; } } if (ws <= p.second.second) { auto it = tod.upper_bound(p.second.second - ws); if (it == tod.begin()) continue; lint mx = prev(it)->second; chmax(r, vs + mx); } } ret[p.first] = r; } } for (auto &v : {c * 2, c * 2 + 1}) { if ((v > n) || (v >= (up)) || !calc[v]) { continue; } dfs(v, to); } }; map<lint, lint> vv; vv[0] = 0; dfs(1, vv); for (int i = 0; i < q; ++i) { cout << ret[i] << "\n"; } return 0; }
replace
68
69
68
69
TLE
p02648
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using int64 = long long; constexpr int DEBUG = 0; constexpr int MAX_W = 100000; constexpr int MAX_D = 9; template <typename T> vector<vector<T>> Make2DVector(int d1, int d2, T default_value) { return vector<vector<T>>(d1, vector<T>(d2, default_value)); } template <class T> inline bool UpdateMin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } template <class T> inline bool UpdateMax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } vector<pair<int64, int64>> Normalize(const vector<pair<int64, int64>> &input_pairs) { vector<pair<int64, int64>> pairs = input_pairs; sort(pairs.begin(), pairs.end()); vector<pair<int64, int64>> output_pairs; for (const auto &pair : pairs) { if (output_pairs.empty()) { output_pairs.push_back(pair); } else if (output_pairs.back().first == pair.first) { output_pairs.pop_back(); output_pairs.push_back(pair); } else if (output_pairs.back().second < pair.second) { output_pairs.push_back(pair); } } return output_pairs; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int64> vs(n + 1); vector<int64> ws(n + 1); for (int i = 1; i <= n; i++) { cin >> vs[i] >> ws[i]; } int q_count; cin >> q_count; vector<int> q_nodes(q_count); vector<int64> q_weights(q_count); for (int i = 0; i < q_count; i++) { cin >> q_nodes[i] >> q_weights[i]; } vector<vector<pair<int64, int64>>> dp(1 << MAX_D); dp[1] = {{0, 0}, {ws[1], vs[1]}}; for (int v = 2; v <= min(n, (1 << MAX_D) - 1); v++) { int p = v / 2; vector<pair<int64, int64>> pairs; for (const auto &input_pair : dp[p]) { pairs.push_back(input_pair); if (input_pair.first + ws[v] < +MAX_W) { pairs.push_back({input_pair.first + ws[v], input_pair.second + vs[v]}); } } dp[v] = Normalize(pairs); } for (int q_index = 0; q_index < q_count; q_index++) { int64 ans = [&]() -> int64 { int node = q_nodes[q_index]; if (q_nodes[q_index] < (1 << MAX_D)) { int64 r = 0; for (const auto &pair : dp[node]) { if (pair.first <= q_weights[q_index]) { UpdateMax(r, pair.second); } } return r; } vector<int64> extra_vs; vector<int64> extra_ws; while (node >= (1 << MAX_D)) { extra_vs.push_back(vs[node]); extra_ws.push_back(ws[node]); node /= 2; } auto pairs_1 = dp[node]; int k = extra_vs.size(); vector<pair<int64, int64>> pairs_2; for (int bits = 0; bits < 1 << k; bits++) { int64 extra_v = 0; int64 extra_w = 0; for (int b = 0; b < k; b++) { if (bits & (1 << b)) { extra_v += extra_vs[b]; extra_w += extra_ws[b]; } } if (extra_w > q_weights[q_index]) continue; pairs_2.push_back({extra_w, extra_v}); } pairs_2 = Normalize(pairs_2); int64 r = 0; int cursor = pairs_2.size() - 1; for (int i = 0; i < pairs_1.size(); i++) { if (pairs_1[i].first > q_weights[q_index]) break; while (pairs_1[i].first + pairs_2[cursor].first > q_weights[q_index]) { cursor--; } UpdateMax(r, pairs_1[i].second + pairs_2[cursor].second); } return r; }(); cout << ans << endl; } }
#include <bits/stdc++.h> using namespace std; using int64 = long long; constexpr int DEBUG = 0; constexpr int MAX_W = 100000; constexpr int MAX_D = 12; template <typename T> vector<vector<T>> Make2DVector(int d1, int d2, T default_value) { return vector<vector<T>>(d1, vector<T>(d2, default_value)); } template <class T> inline bool UpdateMin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } template <class T> inline bool UpdateMax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } vector<pair<int64, int64>> Normalize(const vector<pair<int64, int64>> &input_pairs) { vector<pair<int64, int64>> pairs = input_pairs; sort(pairs.begin(), pairs.end()); vector<pair<int64, int64>> output_pairs; for (const auto &pair : pairs) { if (output_pairs.empty()) { output_pairs.push_back(pair); } else if (output_pairs.back().first == pair.first) { output_pairs.pop_back(); output_pairs.push_back(pair); } else if (output_pairs.back().second < pair.second) { output_pairs.push_back(pair); } } return output_pairs; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int64> vs(n + 1); vector<int64> ws(n + 1); for (int i = 1; i <= n; i++) { cin >> vs[i] >> ws[i]; } int q_count; cin >> q_count; vector<int> q_nodes(q_count); vector<int64> q_weights(q_count); for (int i = 0; i < q_count; i++) { cin >> q_nodes[i] >> q_weights[i]; } vector<vector<pair<int64, int64>>> dp(1 << MAX_D); dp[1] = {{0, 0}, {ws[1], vs[1]}}; for (int v = 2; v <= min(n, (1 << MAX_D) - 1); v++) { int p = v / 2; vector<pair<int64, int64>> pairs; for (const auto &input_pair : dp[p]) { pairs.push_back(input_pair); if (input_pair.first + ws[v] < +MAX_W) { pairs.push_back({input_pair.first + ws[v], input_pair.second + vs[v]}); } } dp[v] = Normalize(pairs); } for (int q_index = 0; q_index < q_count; q_index++) { int64 ans = [&]() -> int64 { int node = q_nodes[q_index]; if (q_nodes[q_index] < (1 << MAX_D)) { int64 r = 0; for (const auto &pair : dp[node]) { if (pair.first <= q_weights[q_index]) { UpdateMax(r, pair.second); } } return r; } vector<int64> extra_vs; vector<int64> extra_ws; while (node >= (1 << MAX_D)) { extra_vs.push_back(vs[node]); extra_ws.push_back(ws[node]); node /= 2; } auto pairs_1 = dp[node]; int k = extra_vs.size(); vector<pair<int64, int64>> pairs_2; for (int bits = 0; bits < 1 << k; bits++) { int64 extra_v = 0; int64 extra_w = 0; for (int b = 0; b < k; b++) { if (bits & (1 << b)) { extra_v += extra_vs[b]; extra_w += extra_ws[b]; } } if (extra_w > q_weights[q_index]) continue; pairs_2.push_back({extra_w, extra_v}); } pairs_2 = Normalize(pairs_2); int64 r = 0; int cursor = pairs_2.size() - 1; for (int i = 0; i < pairs_1.size(); i++) { if (pairs_1[i].first > q_weights[q_index]) break; while (pairs_1[i].first + pairs_2[cursor].first > q_weights[q_index]) { cursor--; } UpdateMax(r, pairs_1[i].second + pairs_2[cursor].second); } return r; }(); cout << ans << endl; } }
replace
7
8
7
8
TLE
p02648
C++
Time Limit Exceeded
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; typedef long long ll; typedef pair<int, int> P; const int MOD = 1000000007; const int INF = 0x3f3f3f3f; const ll INFL = 0x3f3f3f3f3f3f3f3f; int V[1 << 18], W[1 << 18]; vector<P> vv[1 << 18]; int dep(int v) { int cnt = 0; while (v > 1) { cnt++; v >>= 1; } return cnt; } int main() { int n; cin >> n; rep(i, n) { scanf("%d%d", &V[i + 1], &W[i + 1]); } int t = 8; for (int i = 1; i <= n; i++) { if (dep(i) == t) { int v = i; vector<P> vs; while (v >= 1) { vs.push_back(P(V[v], W[v])); v >>= 1; } int N = vs.size(); vector<P> a{P(0, 0)}; rep(j, 1 << N) { int v = 0, w = 0; rep(k, N) { if (j >> k & 1) v += vs[k].first, w += vs[k].second; } a.push_back(P(w, v)); } sort(a.begin(), a.end()); vector<P> b; for (auto p : a) { if (b.empty() || b.back().second < p.second) b.push_back(p); } vv[i] = b; } } int q; cin >> q; rep(i, q) { int v, L; scanf("%d%d", &v, &L); int id = -1; vector<P> vs; while (v >= 1) { if (dep(v) == t) { id = v; break; } vs.push_back(P(V[v], W[v])); v >>= 1; } if (id == -1) { int N = vs.size(); int Max = 0; rep(j, 1 << N) { int v = 0, w = 0; rep(k, N) { if (j >> k & 1) v += vs[k].first, w += vs[k].second; } if (w <= L) Max = max(Max, v); } printf("%d\n", Max); continue; } int N = vs.size(); int Max = 0; rep(j, 1 << N) { int v = 0, w = 0; rep(k, N) { if (j >> k & 1) v += vs[k].first, w += vs[k].second; } if (w > L) continue; auto it = lower_bound(vv[id].begin(), vv[id].end(), P(L - w + 1, 0)); it--; Max = max(Max, v + it->second); } printf("%d\n", Max); } }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; typedef long long ll; typedef pair<int, int> P; const int MOD = 1000000007; const int INF = 0x3f3f3f3f; const ll INFL = 0x3f3f3f3f3f3f3f3f; int V[1 << 18], W[1 << 18]; vector<P> vv[1 << 18]; int dep(int v) { int cnt = 0; while (v > 1) { cnt++; v >>= 1; } return cnt; } int main() { int n; cin >> n; rep(i, n) { scanf("%d%d", &V[i + 1], &W[i + 1]); } int t = 10; for (int i = 1; i <= n; i++) { if (dep(i) == t) { int v = i; vector<P> vs; while (v >= 1) { vs.push_back(P(V[v], W[v])); v >>= 1; } int N = vs.size(); vector<P> a{P(0, 0)}; rep(j, 1 << N) { int v = 0, w = 0; rep(k, N) { if (j >> k & 1) v += vs[k].first, w += vs[k].second; } a.push_back(P(w, v)); } sort(a.begin(), a.end()); vector<P> b; for (auto p : a) { if (b.empty() || b.back().second < p.second) b.push_back(p); } vv[i] = b; } } int q; cin >> q; rep(i, q) { int v, L; scanf("%d%d", &v, &L); int id = -1; vector<P> vs; while (v >= 1) { if (dep(v) == t) { id = v; break; } vs.push_back(P(V[v], W[v])); v >>= 1; } if (id == -1) { int N = vs.size(); int Max = 0; rep(j, 1 << N) { int v = 0, w = 0; rep(k, N) { if (j >> k & 1) v += vs[k].first, w += vs[k].second; } if (w <= L) Max = max(Max, v); } printf("%d\n", Max); continue; } int N = vs.size(); int Max = 0; rep(j, 1 << N) { int v = 0, w = 0; rep(k, N) { if (j >> k & 1) v += vs[k].first, w += vs[k].second; } if (w > L) continue; auto it = lower_bound(vv[id].begin(), vv[id].end(), P(L - w + 1, 0)); it--; Max = max(Max, v + it->second); } printf("%d\n", Max); } }
replace
26
27
26
27
TLE
p02648
C++
Time Limit Exceeded
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdint.h> #include <tuple> #include <utility> #include <vector> using namespace std; typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; static const double EPS = 1e-12; static const double PI = acos(-1.0); static const ll MOD = 1000000007; #define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define ALL(a) (a).begin(), (a).end() #define DEBUG(x) cout << #x << ": " << x << endl map<ll, ll> m; vector<vector<ll>> qq; ll NN; ll c = 0; vector<ll> VV; vector<ll> WW; void dfs(ll n) { if (n > NN) { return; } m.insert({n, c}); c++; dfs(n * 2); dfs(n * 2 + 1); } ll dp[20][100010]; ll cur = 0; ll ans[(size_t)1e5]; ll cur2 = 0; void dfs2(const ll n, const ll depth) { if (n > NN) { return; } const ll kTh = 0; if (depth <= kTh) { REP(w, 100001) { if (w >= WW[n - 1]) { dp[depth][w] = max(dp[depth - 1][w - WW[n - 1]] + VV[n - 1], dp[depth - 1][w]); } else { dp[depth][w] = dp[depth - 1][w]; } } } while (cur < qq.size() && qq[cur][1] == n) { ll z = qq[cur][2]; if (depth <= kTh) { ans[qq[cur][3]] = dp[depth][z]; } else { vector<pair<ll, ll>> v; v.reserve(pow(2, depth - kTh)); v.emplace_back(0, 0); ll nn = n; ll ddepth = depth; while (ddepth > kTh) { ll vs = v.size(); REP(j, vs) { v.emplace_back(v[j].first + VV[nn - 1], v[j].second + WW[nn - 1]); } ddepth--; nn /= 2; } ll m = 0; REP(i, v.size()) { if (z >= v[i].second) { m = max(m, v[i].first + dp[ddepth][z - v[i].second]); } } ans[qq[cur][3]] = m; } cur++; } dfs2(n * 2, depth + 1); dfs2(n * 2 + 1, depth + 1); } void solve(long long N, std::vector<long long> V, std::vector<long long> W, long long Q, std::vector<long long> v, std::vector<long long> L) { VV.swap(V); WW.swap(W); NN = N; dfs(1); qq.resize(Q); REP(i, Q) { qq[i] = {m[v[i]], v[i], L[i], i}; } sort(ALL(qq)); dfs2(1, 1); REP(i, Q) { cout << ans[i] << endl; } } int main() { long long N; scanf("%lld", &N); std::vector<long long> V(N); std::vector<long long> W(N); for (int i = 0; i < N; i++) { scanf("%lld", &V[i]); scanf("%lld", &W[i]); } long long Q; scanf("%lld", &Q); std::vector<long long> v(Q); std::vector<long long> L(Q); for (int i = 0; i < Q; i++) { scanf("%lld", &v[i]); scanf("%lld", &L[i]); } solve(N, std::move(V), std::move(W), Q, std::move(v), std::move(L)); return 0; }
#include <algorithm> #include <array> #include <bitset> #include <cassert> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdint.h> #include <tuple> #include <utility> #include <vector> using namespace std; typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; static const double EPS = 1e-12; static const double PI = acos(-1.0); static const ll MOD = 1000000007; #define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define REP(i, n) for (int i = 0; i < (int)(n); i++) #define ALL(a) (a).begin(), (a).end() #define DEBUG(x) cout << #x << ": " << x << endl map<ll, ll> m; vector<vector<ll>> qq; ll NN; ll c = 0; vector<ll> VV; vector<ll> WW; void dfs(ll n) { if (n > NN) { return; } m.insert({n, c}); c++; dfs(n * 2); dfs(n * 2 + 1); } ll dp[20][100010]; ll cur = 0; ll ans[(size_t)1e5]; ll cur2 = 0; void dfs2(const ll n, const ll depth) { if (n > NN) { return; } const ll kTh = 9; if (depth <= kTh) { REP(w, 100001) { if (w >= WW[n - 1]) { dp[depth][w] = max(dp[depth - 1][w - WW[n - 1]] + VV[n - 1], dp[depth - 1][w]); } else { dp[depth][w] = dp[depth - 1][w]; } } } while (cur < qq.size() && qq[cur][1] == n) { ll z = qq[cur][2]; if (depth <= kTh) { ans[qq[cur][3]] = dp[depth][z]; } else { vector<pair<ll, ll>> v; v.reserve(pow(2, depth - kTh)); v.emplace_back(0, 0); ll nn = n; ll ddepth = depth; while (ddepth > kTh) { ll vs = v.size(); REP(j, vs) { v.emplace_back(v[j].first + VV[nn - 1], v[j].second + WW[nn - 1]); } ddepth--; nn /= 2; } ll m = 0; REP(i, v.size()) { if (z >= v[i].second) { m = max(m, v[i].first + dp[ddepth][z - v[i].second]); } } ans[qq[cur][3]] = m; } cur++; } dfs2(n * 2, depth + 1); dfs2(n * 2 + 1, depth + 1); } void solve(long long N, std::vector<long long> V, std::vector<long long> W, long long Q, std::vector<long long> v, std::vector<long long> L) { VV.swap(V); WW.swap(W); NN = N; dfs(1); qq.resize(Q); REP(i, Q) { qq[i] = {m[v[i]], v[i], L[i], i}; } sort(ALL(qq)); dfs2(1, 1); REP(i, Q) { cout << ans[i] << endl; } } int main() { long long N; scanf("%lld", &N); std::vector<long long> V(N); std::vector<long long> W(N); for (int i = 0; i < N; i++) { scanf("%lld", &V[i]); scanf("%lld", &W[i]); } long long Q; scanf("%lld", &Q); std::vector<long long> v(Q); std::vector<long long> L(Q); for (int i = 0; i < Q; i++) { scanf("%lld", &v[i]); scanf("%lld", &L[i]); } solve(N, std::move(V), std::move(W), Q, std::move(v), std::move(L)); return 0; }
replace
68
69
68
69
TLE
p02648
C++
Time Limit Exceeded
#define _USE_MATH_DEFIMES #include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cctype> #include <climits> #include <clocale> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <ctime> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <numeric> #include <queue> #include <regex> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> const int MOD = 1'000'000'007; const int INF = 1'000'000'000; // 1e9 const int NIL = -1; const long long LINF = 1'000'000'000'000'000'000; // 1e18 const long double EPS = 1E-10; template <class T, class S> inline bool chmax(T &a, const S &b) { if (a < b) { a = b; return true; } return false; } template <class T, class S> inline bool chmin(T &a, const S &b) { if (b < a) { a = b; return true; } return false; } inline int grayCode(int i) { return i ^ (i >> 1); } inline int invGrayCode(int j) { for (int k(0); k < 5; ++k) { // 2pow(5) = 32 j = j ^ (j >> (1 << k)); } return j; } std::unordered_map<int, int> twoPow{ {1, 0}, {2, 1}, {4, 2}, {8, 3}, {16, 4}, {32, 5}, {64, 6}, {128, 7}, {256, 8}, {512, 9}, {1024, 10}, {2048, 11}, {4096, 12}, {8192, 13}, {16384, 14}, {32768, 15}, {65536, 16}, {131072, 17}, {262144, 18}, {524288, 19}}; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); int N; std::cin >> N; std::vector<int> V(N + 1), W(N + 1); for (int i(1); i <= N; ++i) std::cin >> V[i] >> W[i]; int n(0); for (; n <= 18; ++n) { if ((1 << n) > N) break; } int Q, mxL; std::cin >> Q; std::vector<int> v(Q), L(Q); for (int i(0); i < Q; ++i) { std::cin >> v[i] >> L[i]; chmax(mxL, L[i]); } int n2(n / 2); std::vector<std::vector<long long>> dp((1 << n2), std::vector<long long>(mxL + 1, 0)); for (int i(1), i_len(1 << n2); i < i_len; ++i) { for (int j(1); j <= mxL; ++j) { dp[i][j] = dp[i / 2][j]; if (j >= W[i]) chmax(dp[i][j], dp[i / 2][j - W[i]] + V[i]); } } for (int qq(0); qq < Q; ++qq) { if (v[qq] < (1 << n2)) { std::cout << dp[v[qq]][L[qq]] << "\n"; continue; } std::vector<int> ww, vv; int vx(v[qq]); while (vx >= (1 << n2)) { ww.push_back(W[vx]); vv.push_back(V[vx]); vx >>= 1; } int n1(ww.size()); int sw(0), sv(0); int prvj(0); // i = 0 の時の処理 int ans(0); if (sw <= L[qq]) { ans = dp[vx][L[qq]]; } for (int i(1), i_len(1 << n1); i < i_len; ++i) { int j(i ^ (i >> 1)); int changebit(j ^ prvj); int changeNum(twoPow[changebit]); if (j & changebit) { // changeBit が立った sw += ww[changeNum]; sv += vv[changeNum]; } else { // changeBit がなくなった sw -= ww[changeNum]; sv -= vv[changeNum]; } // 何か処理 if (sw <= L[qq]) { chmax(ans, dp[vx][L[qq] - sw] + sv); } prvj = j; } std::cout << ans << "\n"; } return 0; }
#define _USE_MATH_DEFIMES #include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cctype> #include <climits> #include <clocale> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <ctime> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <list> #include <map> #include <numeric> #include <queue> #include <regex> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> const int MOD = 1'000'000'007; const int INF = 1'000'000'000; // 1e9 const int NIL = -1; const long long LINF = 1'000'000'000'000'000'000; // 1e18 const long double EPS = 1E-10; template <class T, class S> inline bool chmax(T &a, const S &b) { if (a < b) { a = b; return true; } return false; } template <class T, class S> inline bool chmin(T &a, const S &b) { if (b < a) { a = b; return true; } return false; } inline int grayCode(int i) { return i ^ (i >> 1); } inline int invGrayCode(int j) { for (int k(0); k < 5; ++k) { // 2pow(5) = 32 j = j ^ (j >> (1 << k)); } return j; } std::unordered_map<int, int> twoPow{ {1, 0}, {2, 1}, {4, 2}, {8, 3}, {16, 4}, {32, 5}, {64, 6}, {128, 7}, {256, 8}, {512, 9}, {1024, 10}, {2048, 11}, {4096, 12}, {8192, 13}, {16384, 14}, {32768, 15}, {65536, 16}, {131072, 17}, {262144, 18}, {524288, 19}}; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); int N; std::cin >> N; std::vector<int> V(N + 1), W(N + 1); for (int i(1); i <= N; ++i) std::cin >> V[i] >> W[i]; int n(0); for (; n <= 18; ++n) { if ((1 << n) > N) break; } int Q, mxL; std::cin >> Q; std::vector<int> v(Q), L(Q); for (int i(0); i < Q; ++i) { std::cin >> v[i] >> L[i]; chmax(mxL, L[i]); } int n2(n / 2); std::vector<std::vector<int>> dp((1 << n2), std::vector<int>(mxL + 1, 0)); for (int i(1), i_len(1 << n2); i < i_len; ++i) { for (int j(1); j <= mxL; ++j) { dp[i][j] = dp[i / 2][j]; if (j >= W[i]) chmax(dp[i][j], dp[i / 2][j - W[i]] + V[i]); } } for (int qq(0); qq < Q; ++qq) { if (v[qq] < (1 << n2)) { std::cout << dp[v[qq]][L[qq]] << "\n"; continue; } std::vector<int> ww, vv; int vx(v[qq]); while (vx >= (1 << n2)) { ww.push_back(W[vx]); vv.push_back(V[vx]); vx >>= 1; } int n1(ww.size()); int sw(0), sv(0); int prvj(0); // i = 0 の時の処理 int ans(0); if (sw <= L[qq]) { ans = dp[vx][L[qq]]; } for (int i(1), i_len(1 << n1); i < i_len; ++i) { int j(i ^ (i >> 1)); int changebit(j ^ prvj); int changeNum(twoPow[changebit]); if (j & changebit) { // changeBit が立った sw += ww[changeNum]; sv += vv[changeNum]; } else { // changeBit がなくなった sw -= ww[changeNum]; sv -= vv[changeNum]; } // 何か処理 if (sw <= L[qq]) { chmax(ans, dp[vx][L[qq] - sw] + sv); } prvj = j; } std::cout << ans << "\n"; } return 0; }
replace
91
93
91
92
TLE
p02648
C++
Time Limit Exceeded
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; const int L = 101010; int n, q; vector<vector<pair<int, int>>> query; vector<int> ans, v, w; vector<vector<int>> dp; vector<int> vals, weights; void dfs(int cur, int d) { if (cur > n) return; if (d <= 8) { vector<int> nxt(L); for (int i = 0; i < L; i++) { if (i - w[cur] >= 0) nxt[i] = max(nxt[i], dp.back()[i - w[cur]] + v[cur]); nxt[i] = max(nxt[i], dp.back()[i]); if (i > 0) nxt[i] = max(nxt[i], nxt[i - 1]); } dp.push_back(nxt); for (auto &p : query[cur]) { auto &id = p.second; auto &l = p.first; ans[id] = max(ans[id], dp.back()[l]); } dfs(cur * 2 + 1, d + 1); dfs(cur * 2, d + 1); dp.pop_back(); } else { vals.push_back(v[cur]); weights.push_back(w[cur]); int k = vals.size(); for (auto &p : query[cur]) { auto &id = p.second; auto &l = p.first; for (int mask = 0; mask < (1 << k); mask++) { int ww = 0; int vv = 0; for (int i = 0; i < k; i++) if ((1 << i) & mask) { ww += weights[i]; vv += vals[i]; } int rem = l - ww; if (rem < 0) continue; ans[id] = max(ans[id], dp.back()[rem] + vv); } } dfs(cur * 2 + 1, d + 1); dfs(cur * 2, d + 1); vals.pop_back(); weights.pop_back(); } } signed main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; query = vector<vector<pair<int, int>>>(n + 1); v = vector<int>(n + 1); w = v; for (int i = 1; i <= n; i++) { cin >> v[i] >> w[i]; } cin >> q; ans = vector<int>(q + 1); dp.push_back(vector<int>(L)); for (int i = 1; i <= q; i++) { int v, l; cin >> v >> l; query[v].emplace_back(l, i); } dfs(1, 1); for (int i = 1; i <= q; i++) { cout << ans[i] << '\n'; } return 0; }
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; const int L = 101010; int n, q; vector<vector<pair<int, int>>> query; vector<int> ans, v, w; vector<vector<int>> dp; vector<int> vals, weights; void dfs(int cur, int d) { if (cur > n) return; if (d <= 9) { vector<int> nxt(L); for (int i = 0; i < L; i++) { if (i - w[cur] >= 0) nxt[i] = max(nxt[i], dp.back()[i - w[cur]] + v[cur]); nxt[i] = max(nxt[i], dp.back()[i]); if (i > 0) nxt[i] = max(nxt[i], nxt[i - 1]); } dp.push_back(nxt); for (auto &p : query[cur]) { auto &id = p.second; auto &l = p.first; ans[id] = max(ans[id], dp.back()[l]); } dfs(cur * 2 + 1, d + 1); dfs(cur * 2, d + 1); dp.pop_back(); } else { vals.push_back(v[cur]); weights.push_back(w[cur]); int k = vals.size(); for (auto &p : query[cur]) { auto &id = p.second; auto &l = p.first; for (int mask = 0; mask < (1 << k); mask++) { int ww = 0; int vv = 0; for (int i = 0; i < k; i++) if ((1 << i) & mask) { ww += weights[i]; vv += vals[i]; } int rem = l - ww; if (rem < 0) continue; ans[id] = max(ans[id], dp.back()[rem] + vv); } } dfs(cur * 2 + 1, d + 1); dfs(cur * 2, d + 1); vals.pop_back(); weights.pop_back(); } } signed main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; query = vector<vector<pair<int, int>>>(n + 1); v = vector<int>(n + 1); w = v; for (int i = 1; i <= n; i++) { cin >> v[i] >> w[i]; } cin >> q; ans = vector<int>(q + 1); dp.push_back(vector<int>(L)); for (int i = 1; i <= q; i++) { int v, l; cin >> v >> l; query[v].emplace_back(l, i); } dfs(1, 1); for (int i = 1; i <= q; i++) { cout << ans[i] << '\n'; } return 0; }
replace
12
13
12
13
TLE
p02648
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 all(hoge) (hoge).begin(), (hoge).end() #define en '\n' using ll = long long; using ull = unsigned long long; 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; // constexpr long long MOD = 998244353LL; using ld = long double; static const ld pi = 3.141592653589793L; typedef vector<ll> Array; typedef vector<Array> Matrix; 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, rev; long double cap; Edge(ll _to, long double _cap, ll _rev) { to = _to; cap = _cap; rev = _rev; } }; using Edges = vector<Edge>; using Graph = vector<Edges>; void add_edge(Graph &G, ll from, ll to, long double cap, bool revFlag, long double 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() { int n; cin >> n; vec<ll> v(n), w(n); REP(i, n) cin >> v[i] >> w[i]; ll max_w = 101010; vvec<P> dp(1024); dp[0] = vec<P>({{0, 0}, {w[0], v[0]}}); REP(i, min(n, 1024)) { vec<P> dp1 = dp[(i + 1) / 2 - 1]; vec<P> dp2; for (auto [ww, vv] : dp1) { if (ww + w[i] > max_w) break; dp2.push_back({ww + w[i], vv + v[i]}); } merge(all(dp1), all(dp2), back_inserter(dp[i])); } auto calc = [&](int p, int l, vec<int> o) { vec<P> wv(1, {0, 0}); for (auto i : o) { vec<P> wv1 = wv; vec<P> wv2; for (auto [ww, vv] : wv1) { if (ww + w[i] > l) break; wv2.push_back({ww + w[i], vv + v[i]}); } wv.clear(); merge(all(wv1), all(wv2), back_inserter(wv)); } ll ans = 0; ll ma = 0; int t = 0; REP2(i, dp[p].size()) { if (dp[p][i].first > l) continue; while (t < wv.size() and dp[p][i].first + wv[t].first <= l) { chmax(ma, wv[t].second); t++; } chmax(ans, dp[p][i].second + ma); } cout << ans << en; }; ll q; cin >> q; while (q--) { ll v, l; cin >> v >> l; if (v <= 1024) { v--; ll ans = 0; for (auto [ww, vv] : dp[v]) { if (ww > l) break; chmax(ans, vv); } cout << ans << en; } else { vec<int> o; while (v) { if (v <= 1024) { calc(v - 1, l, o); break; } o.push_back(v - 1); v /= 2; } } } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); solve(); // ll t;cin>>t;REP(i,t) 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 all(hoge) (hoge).begin(), (hoge).end() #define en '\n' using ll = long long; using ull = unsigned long long; 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; // constexpr long long MOD = 998244353LL; using ld = long double; static const ld pi = 3.141592653589793L; typedef vector<ll> Array; typedef vector<Array> Matrix; 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, rev; long double cap; Edge(ll _to, long double _cap, ll _rev) { to = _to; cap = _cap; rev = _rev; } }; using Edges = vector<Edge>; using Graph = vector<Edges>; void add_edge(Graph &G, ll from, ll to, long double cap, bool revFlag, long double 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() { int n; cin >> n; vec<ll> v(n), w(n); REP(i, n) cin >> v[i] >> w[i]; ll max_w = 101010; vvec<P> dp(1024); dp[0] = vec<P>({{0, 0}, {w[0], v[0]}}); int m = min(n, 1024); rep(i, 1, m) { vec<P> dp1 = dp[(i + 1) / 2 - 1]; vec<P> dp2; for (auto [ww, vv] : dp1) { if (ww + w[i] > max_w) break; dp2.push_back({ww + w[i], vv + v[i]}); } merge(all(dp1), all(dp2), back_inserter(dp[i])); } auto calc = [&](int p, int l, vec<int> o) { vec<P> wv(1, {0, 0}); for (auto i : o) { vec<P> wv1 = wv; vec<P> wv2; for (auto [ww, vv] : wv1) { if (ww + w[i] > l) break; wv2.push_back({ww + w[i], vv + v[i]}); } wv.clear(); merge(all(wv1), all(wv2), back_inserter(wv)); } ll ans = 0; ll ma = 0; int t = 0; REP2(i, dp[p].size()) { if (dp[p][i].first > l) continue; while (t < wv.size() and dp[p][i].first + wv[t].first <= l) { chmax(ma, wv[t].second); t++; } chmax(ans, dp[p][i].second + ma); } cout << ans << en; }; ll q; cin >> q; while (q--) { ll v, l; cin >> v >> l; if (v <= 1024) { v--; ll ans = 0; for (auto [ww, vv] : dp[v]) { if (ww > l) break; chmax(ans, vv); } cout << ans << en; } else { vec<int> o; while (v) { if (v <= 1024) { calc(v - 1, l, o); break; } o.push_back(v - 1); v /= 2; } } } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); solve(); // ll t;cin>>t;REP(i,t) solve(); return 0; }
replace
67
68
67
69
-11
p02648
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; // #define ll long long template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } #define rep(i, n) REP(i, 0, n) #define rep_rev(i, n) for (int(i) = (int)(n)-1; (i) >= 0; --(i)) #define ALL(v) v.begin(), v.end() #define MSG(a) cout << #a << " " << a << endl; #define REP(i, x, n) for (int i = x; i < n; i++) #define all(x) (x).begin(), (x).end() ll t1, t2, t3; const ll mod = 998244353; const int INF = 1e9; const ll INFLONG = 1e18; vector<vector<ll>> kn(pow(2, 9), vector<ll>(100001, 0)); vector<pair<ll, ll>> vec(1000000); vector<ll> ans(100000, 0); ll n; void dfs(ll k) { if (k >= min((ll)pow(2, 9), n)) { return; } if (k == 0) { REP(i, vec[k].first, 100001) { kn[k][i] = vec[k].second; } } else { rep(i, 100001) { kn[k][i] = kn[(k - 1) / 2][i]; if (i >= vec[k].first) { chmax(kn[k][i], kn[(k - 1) / 2][i - vec[k].first] + vec[k].second); } } } dfs(k * 2 + 1); dfs(k * 2 + 2); } void calc(ll v, ll l, ll score, ll num) { if (l < 0) { return; } if (v <= (ll)pow(2, 9)) { chmax(ans[num], kn[v][l] + score); } else { calc((v - 1) / 2, l, score, num); calc((v - 1) / 2, l - vec[v].first, score + vec[v].second, num); } } int main() { cin >> n; rep(i, n) { cin >> t1 >> t2; vec[i].first = t2; // weight vec[i].second = t1; } dfs(0); ll q; cin >> q; rep(i, q) { cin >> t1 >> t2; t1--; calc(t1, t2, 0, i); } rep(i, q) { cout << ans[i] << endl; } }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; // #define ll long long template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } #define rep(i, n) REP(i, 0, n) #define rep_rev(i, n) for (int(i) = (int)(n)-1; (i) >= 0; --(i)) #define ALL(v) v.begin(), v.end() #define MSG(a) cout << #a << " " << a << endl; #define REP(i, x, n) for (int i = x; i < n; i++) #define all(x) (x).begin(), (x).end() ll t1, t2, t3; const ll mod = 998244353; const int INF = 1e9; const ll INFLONG = 1e18; vector<vector<ll>> kn(pow(2, 9), vector<ll>(100001, 0)); vector<pair<ll, ll>> vec(1000000); vector<ll> ans(100000, 0); ll n; void dfs(ll k) { if (k >= min((ll)pow(2, 9), n)) { return; } if (k == 0) { REP(i, vec[k].first, 100001) { kn[k][i] = vec[k].second; } } else { rep(i, 100001) { kn[k][i] = kn[(k - 1) / 2][i]; if (i >= vec[k].first) { chmax(kn[k][i], kn[(k - 1) / 2][i - vec[k].first] + vec[k].second); } } } dfs(k * 2 + 1); dfs(k * 2 + 2); } void calc(ll v, ll l, ll score, ll num) { if (l < 0) { return; } if (v < (ll)pow(2, 9)) { chmax(ans[num], kn[v][l] + score); } else { calc((v - 1) / 2, l, score, num); calc((v - 1) / 2, l - vec[v].first, score + vec[v].second, num); } } int main() { cin >> n; rep(i, n) { cin >> t1 >> t2; vec[i].first = t2; // weight vec[i].second = t1; } dfs(0); ll q; cin >> q; rep(i, q) { cin >> t1 >> t2; t1--; calc(t1, t2, 0, i); } rep(i, q) { cout << ans[i] << endl; } }
replace
57
58
57
58
-6
terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc
p02648
C++
Runtime Error
#include <algorithm> #include <iostream> #include <array> #include <bitset> #include <complex> #include <cstring> #include <deque> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <utility> #include <valarray> #include <vector> #include <cassert> #include <chrono> #include <cmath> #include <functional> #include <iomanip> #include <numeric> #include <random> #include <memory> #include <climits> using namespace std; // #define int long long typedef long long ll; typedef unsigned long long ull; // typedef unsigned __int128 HASH; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<ull, ull> pullull; typedef pair<ll, int> pli; typedef pair<double, int> pdi; typedef pair<long double, int> pdbi; typedef pair<int, pii> pipii; typedef pair<ll, pll> plpll; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef vector<pii> vpii; typedef vector<vector<int>> mat; #define rep(i, n) for (int i = 0; i < (n); i++) #define rep2(i, a, b) for (int i = (a); i < (b); i++) #define rrep(i, n) for (int i = (n); i > 0; i--) #define rrep2(i, a, b) for (int i = (a); i > b; i--) #define fi first #define se second #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() template <class T> using MINPQ = std::priority_queue<T, vector<T>, greater<T>>; // MINPQ<pii> pq; template <class T> using MAXPQ = std::priority_queue<T>; const ll hmod1 = 999999937; const ll hmod2 = 1000000000 + 9; const int INF = 1 << 30; const ll INFLL = 1LL << 62; const double EPS = 1e-12; const ll mod = 1000000000 + 7; // const ll mod = 998244353; const int dx4[4] = {0, 0, -1, 1}; const int dy4[4] = {-1, 1, 0, 0}; const int dx8[8] = {1, 1, 1, 0, 0, -1, -1, -1}; const int dy8[8] = {0, 1, -1, 1, -1, 0, 1, -1}; const long double pi = 3.141592653589793; #define addm(X, Y) (X) = ((X) + ((Y) % mod) + mod) % mod #define inside(y, x, h, w) \ (0 <= (y) && (y) < (h) && 0 <= (x) && (x) < (w)) ? true : false #define stand(B, i) \ (((B >> i) & 1) == 1) ? true : false // Bのi番目のbitが1ならtrue, 0ならfalse // debug #define DEBUG #define DUMPOUT cerr #ifdef DEBUG #define dump(...) \ DUMPOUT << #__VA_ARGS__ << " :[" << __FUNCTION__ << ":" << __LINE__ << "]" \ << endl; \ DUMPOUT << " "; \ dump_func(__VA_ARGS__) #else #define dump(...) #endif void dump_func() { DUMPOUT << endl; }; template <class Head, class... Tail> void dump_func(Head &&head, Tail &&...tail) { DUMPOUT << head; if (sizeof...(Tail) == 0) DUMPOUT << " "; else DUMPOUT << ", "; dump_func(std::move(tail)...); } // ostream template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) { os << "["; for (int i = 0; i < vec.size(); i++) os << vec[i] << (i + 1 == vec.size() ? "" : ", "); os << "]"; return os; } template <typename T, typename U> ostream &operator<<(ostream &os, pair<T, U> &pair_var) { os << "(" << pair_var.first << ", " << pair_var.second << ")"; return os; } template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &map_var) { os << "["; for (auto itr = map_var.begin(); itr != map_var.end(); itr++) { os << "(" << itr->first << ", " << itr->second << ")"; itr++; if (itr != map_var.end()) os << ", "; itr--; } os << "]"; return os; } template <typename T> ostream &operator<<(ostream &os, set<T> &set_var) { os << "["; for (auto itr = set_var.begin(); itr != set_var.end(); itr++) { os << *itr; ++itr; if (itr != set_var.end()) os << ", "; itr--; } os << "]"; return os; } int n; int v[1 << 18], w[1 << 18]; int q; vector<vector<pii>> ps(1 << 12); int idx[1 << 12]; void pre_process() { rep(V, min(1 << 12, n)) { int x = V; vector<int> tmpv, tmpw; while (x > 0) { tmpv.push_back(v[x]); tmpw.push_back(w[x]); x = (x - 1) / 2; } tmpv.push_back(v[0]); tmpw.push_back(w[0]); int m = tmpv.size(); ps[V].resize(1 << m); rep(i, 1 << m) { int sw = 0, sv = 0; rep(j, m) { if (i >> j & 1) { sv += tmpv[j]; sw += tmpw[j]; } } ps[V][i] = {sw, sv}; } sort(all(ps[V])); idx[V] = 1; rep2(i, 1, 1 << m) { if (ps[V][idx[V] - 1].se < ps[V][i].se) { ps[V][idx[V]++] = ps[V][i]; } } } } int solve(int L, vector<int> &tmpv, vector<int> &tmpw, vector<int> &node) { int m = tmpv.size(); int st = -1; int V = -1; rep(i, m) { if (node[i] >= (1 << 12)) { st = i; break; } V = node[i]; } assert(st != -1); assert(V < (1 << 12)); int res = 0; rep(i, 1 << (m - st)) { int sw = 0, sv = 0; rep(j, m - st) { if (i >> j & 1) { sv += tmpv[st + j]; sw += tmpw[st + j]; } } if (sw <= L) { int tv = (lower_bound(ps[V].begin(), ps[V].begin() + idx[V], make_pair(L - sw, INF)) - 1) ->second; res = max(res, sv + tv); } } return res; } signed main() { cin >> n; rep(i, n) cin >> v[i] >> w[i]; cin >> q; pre_process(); rep(_, q) { int x, L; cin >> x >> L; x--; int V = x; vector<int> tmpv, tmpw; vector<int> node; while (x > 0) { tmpv.push_back(v[x]); tmpw.push_back(w[x]); node.push_back(x); x = (x - 1) / 2; } tmpv.push_back(v[0]); tmpw.push_back(w[0]); node.push_back(0); reverse(all(tmpv)); reverse(all(tmpw)); reverse(all(node)); int ans = 0; if (n <= (1 << 12)) { ans = (lower_bound(ps[V].begin(), ps[V].begin() + idx[V], make_pair(L, INF)) - 1) ->second; } else { ans = solve(L, tmpv, tmpw, node); } cout << ans << endl; } }
#include <algorithm> #include <iostream> #include <array> #include <bitset> #include <complex> #include <cstring> #include <deque> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <utility> #include <valarray> #include <vector> #include <cassert> #include <chrono> #include <cmath> #include <functional> #include <iomanip> #include <numeric> #include <random> #include <memory> #include <climits> using namespace std; // #define int long long typedef long long ll; typedef unsigned long long ull; // typedef unsigned __int128 HASH; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<ull, ull> pullull; typedef pair<ll, int> pli; typedef pair<double, int> pdi; typedef pair<long double, int> pdbi; typedef pair<int, pii> pipii; typedef pair<ll, pll> plpll; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef vector<pii> vpii; typedef vector<vector<int>> mat; #define rep(i, n) for (int i = 0; i < (n); i++) #define rep2(i, a, b) for (int i = (a); i < (b); i++) #define rrep(i, n) for (int i = (n); i > 0; i--) #define rrep2(i, a, b) for (int i = (a); i > b; i--) #define fi first #define se second #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() template <class T> using MINPQ = std::priority_queue<T, vector<T>, greater<T>>; // MINPQ<pii> pq; template <class T> using MAXPQ = std::priority_queue<T>; const ll hmod1 = 999999937; const ll hmod2 = 1000000000 + 9; const int INF = 1 << 30; const ll INFLL = 1LL << 62; const double EPS = 1e-12; const ll mod = 1000000000 + 7; // const ll mod = 998244353; const int dx4[4] = {0, 0, -1, 1}; const int dy4[4] = {-1, 1, 0, 0}; const int dx8[8] = {1, 1, 1, 0, 0, -1, -1, -1}; const int dy8[8] = {0, 1, -1, 1, -1, 0, 1, -1}; const long double pi = 3.141592653589793; #define addm(X, Y) (X) = ((X) + ((Y) % mod) + mod) % mod #define inside(y, x, h, w) \ (0 <= (y) && (y) < (h) && 0 <= (x) && (x) < (w)) ? true : false #define stand(B, i) \ (((B >> i) & 1) == 1) ? true : false // Bのi番目のbitが1ならtrue, 0ならfalse // debug #define DEBUG #define DUMPOUT cerr #ifdef DEBUG #define dump(...) \ DUMPOUT << #__VA_ARGS__ << " :[" << __FUNCTION__ << ":" << __LINE__ << "]" \ << endl; \ DUMPOUT << " "; \ dump_func(__VA_ARGS__) #else #define dump(...) #endif void dump_func() { DUMPOUT << endl; }; template <class Head, class... Tail> void dump_func(Head &&head, Tail &&...tail) { DUMPOUT << head; if (sizeof...(Tail) == 0) DUMPOUT << " "; else DUMPOUT << ", "; dump_func(std::move(tail)...); } // ostream template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) { os << "["; for (int i = 0; i < vec.size(); i++) os << vec[i] << (i + 1 == vec.size() ? "" : ", "); os << "]"; return os; } template <typename T, typename U> ostream &operator<<(ostream &os, pair<T, U> &pair_var) { os << "(" << pair_var.first << ", " << pair_var.second << ")"; return os; } template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &map_var) { os << "["; for (auto itr = map_var.begin(); itr != map_var.end(); itr++) { os << "(" << itr->first << ", " << itr->second << ")"; itr++; if (itr != map_var.end()) os << ", "; itr--; } os << "]"; return os; } template <typename T> ostream &operator<<(ostream &os, set<T> &set_var) { os << "["; for (auto itr = set_var.begin(); itr != set_var.end(); itr++) { os << *itr; ++itr; if (itr != set_var.end()) os << ", "; itr--; } os << "]"; return os; } int n; int v[1 << 18], w[1 << 18]; int q; vector<vector<pii>> ps(1 << 12); int idx[1 << 12]; void pre_process() { rep(V, min(1 << 12, n)) { int x = V; vector<int> tmpv, tmpw; while (x > 0) { tmpv.push_back(v[x]); tmpw.push_back(w[x]); x = (x - 1) / 2; } tmpv.push_back(v[0]); tmpw.push_back(w[0]); int m = tmpv.size(); ps[V].resize(1 << m); rep(i, 1 << m) { int sw = 0, sv = 0; rep(j, m) { if (i >> j & 1) { sv += tmpv[j]; sw += tmpw[j]; } } ps[V][i] = {sw, sv}; } sort(all(ps[V])); idx[V] = 1; rep2(i, 1, 1 << m) { if (ps[V][idx[V] - 1].se < ps[V][i].se) { ps[V][idx[V]++] = ps[V][i]; } } } } int solve(int L, vector<int> &tmpv, vector<int> &tmpw, vector<int> &node) { int m = tmpv.size(); int st = -1; int V = -1; rep(i, m) { if (node[i] >= (1 << 12)) { st = i; break; } V = node[i]; } assert(st != -1); assert(V < (1 << 12)); int res = 0; rep(i, 1 << (m - st)) { int sw = 0, sv = 0; rep(j, m - st) { if (i >> j & 1) { sv += tmpv[st + j]; sw += tmpw[st + j]; } } if (sw <= L) { int tv = (lower_bound(ps[V].begin(), ps[V].begin() + idx[V], make_pair(L - sw, INF)) - 1) ->second; res = max(res, sv + tv); } } return res; } signed main() { cin >> n; rep(i, n) cin >> v[i] >> w[i]; cin >> q; pre_process(); rep(_, q) { int x, L; cin >> x >> L; x--; int V = x; vector<int> tmpv, tmpw; vector<int> node; while (x > 0) { tmpv.push_back(v[x]); tmpw.push_back(w[x]); node.push_back(x); x = (x - 1) / 2; } tmpv.push_back(v[0]); tmpw.push_back(w[0]); node.push_back(0); reverse(all(tmpv)); reverse(all(tmpw)); reverse(all(node)); int ans = 0; if (V <= (1 << 12)) { ans = (lower_bound(ps[V].begin(), ps[V].begin() + idx[V], make_pair(L, INF)) - 1) ->second; } else { ans = solve(L, tmpv, tmpw, node); } cout << ans << endl; } }
replace
251
252
251
252
0
p02648
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define SELECTER(_1, _2, _3, SELECT, ...) SELECT #define REP1(i, n) for (int i = 0; (i) < (n); (i)++) #define REP2(i, a, b) for (int i = (a); (i) < (b); (i)++) #define REP(...) SELECTER(__VA_ARGS__, REP2, REP1, )(__VA_ARGS__) template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { os << "{"; for (size_t i = 0; i < v.size(); i++) os << v[i] << (i + 1 == v.size() ? "" : ", "); os << "}"; return os; } template <class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { return os << "{" << p.first << ", " << p.second << "}"; } #define H 9 int dp[1 << H][100001]; int main() { int N; cin >> N; vector<pair<int, int>> VW(N); REP(i, N) cin >> VW[i].first >> VW[i].second; int Q; cin >> Q; vector<pair<int, int>> vL(N); REP(i, Q) { cin >> vL[i].first >> vL[i].second; vL[i].first--; } dp[0][VW[0].second] = VW[0].first; for (int i = 1; i < min(N, 1 << H); i++) { int par = (i - 1) / 2; for (int j = 0; j <= 100000; j++) dp[i][j] = dp[par][j]; for (int j = 0; j <= 100000; j++) { int w = j + VW[i].second; int v = dp[par][j] + VW[i].first; if (w > 100000) break; dp[i][w] = max(dp[i][w], v); } } for (int i = 0; i < (1 << H); i++) for (int j = 1; j <= 100000; j++) dp[i][j] = max(dp[i][j], dp[i][j - 1]); vector<int> anss(Q); REP(q, Q) { int v = vL[q].first; int L = vL[q].second; vector<pair<int, int>> z; while (true) { if (v < 1 << H) break; z.push_back(VW[v]); v = (v - 1) / 2; } int sz = z.size(); int ans = 0; for (int i = 0; i < (1 << sz); i++) { int Vtot = 0; int Wtot = 0; for (int j = 0; j < sz; j++) { if (i & (1 << j)) { Vtot += z[j].first; Wtot += z[j].second; } } if (Wtot > L) continue; ans = max(ans, Vtot + dp[v][L - Wtot]); } anss[q] = ans; } for (auto x : anss) cout << x << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define SELECTER(_1, _2, _3, SELECT, ...) SELECT #define REP1(i, n) for (int i = 0; (i) < (n); (i)++) #define REP2(i, a, b) for (int i = (a); (i) < (b); (i)++) #define REP(...) SELECTER(__VA_ARGS__, REP2, REP1, )(__VA_ARGS__) template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { os << "{"; for (size_t i = 0; i < v.size(); i++) os << v[i] << (i + 1 == v.size() ? "" : ", "); os << "}"; return os; } template <class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { return os << "{" << p.first << ", " << p.second << "}"; } #define H 9 int dp[1 << H][100001]; int main() { int N; cin >> N; vector<pair<int, int>> VW(N); REP(i, N) cin >> VW[i].first >> VW[i].second; int Q; cin >> Q; vector<pair<int, int>> vL(Q); REP(i, Q) { cin >> vL[i].first >> vL[i].second; vL[i].first--; } dp[0][VW[0].second] = VW[0].first; for (int i = 1; i < min(N, 1 << H); i++) { int par = (i - 1) / 2; for (int j = 0; j <= 100000; j++) dp[i][j] = dp[par][j]; for (int j = 0; j <= 100000; j++) { int w = j + VW[i].second; int v = dp[par][j] + VW[i].first; if (w > 100000) break; dp[i][w] = max(dp[i][w], v); } } for (int i = 0; i < (1 << H); i++) for (int j = 1; j <= 100000; j++) dp[i][j] = max(dp[i][j], dp[i][j - 1]); vector<int> anss(Q); REP(q, Q) { int v = vL[q].first; int L = vL[q].second; vector<pair<int, int>> z; while (true) { if (v < 1 << H) break; z.push_back(VW[v]); v = (v - 1) / 2; } int sz = z.size(); int ans = 0; for (int i = 0; i < (1 << sz); i++) { int Vtot = 0; int Wtot = 0; for (int j = 0; j < sz; j++) { if (i & (1 << j)) { Vtot += z[j].first; Wtot += z[j].second; } } if (Wtot > L) continue; ans = max(ans, Vtot + dp[v][L - Wtot]); } anss[q] = ans; } for (auto x : anss) cout << x << endl; return 0; }
replace
30
31
30
31
-11
p02648
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<int, int>; #define rep(i, n) for (int i = 0; i < (int)(n); ++i) const int MX = 1e5 + 5; vector<vector<int>> dp(512, vector<int>(MX)); int main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); int n, q; cin >> n; vector<int> v(n + 1), w(n + 1); for (int i = 1; i <= n; ++i) cin >> v[i] >> w[i]; for (int i = 1; i < 512; ++i) { for (int j = 1e5; j >= w[i]; --j) { dp[i][j] = max(dp[i / 2][j], dp[i / 2][j - w[i]] + v[i]); } for (int j = w[i] - 1; j >= 0; --j) { dp[i][j] = dp[i / 2][j]; } } cin >> q; rep(_, q) { int u, l; cin >> u >> l; vector<int> ids; while (u >= 256) { ids.push_back(u); u >>= 1; } int ans = 0; int sz = ids.size(); rep(t, (1 << sz)) { int sv = 0; int sw = 0; rep(i, sz) { if (t & (1 << i)) { sv += v[ids[i]]; sw += w[ids[i]]; } } if (sw <= l) { ans = max(ans, sv + dp[u][l - sw]); } } cout << ans << endl; } }
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<int, int>; #define rep(i, n) for (int i = 0; i < (int)(n); ++i) const int MX = 1e5 + 5; vector<vector<int>> dp(512, vector<int>(MX)); int main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); int n, q; cin >> n; vector<int> v(n + 1), w(n + 1); for (int i = 1; i <= n; ++i) cin >> v[i] >> w[i]; for (int i = 1; i < 512; ++i) { for (int j = 1e5; j >= w[i]; --j) { dp[i][j] = max(dp[i / 2][j], dp[i / 2][j - w[i]] + v[i]); } for (int j = w[i] - 1; j >= 0; --j) { dp[i][j] = dp[i / 2][j]; } } cin >> q; rep(_, q) { int u, l; cin >> u >> l; vector<int> ids; while (u >= 512) { ids.push_back(u); u >>= 1; } int ans = 0; int sz = ids.size(); rep(t, (1 << sz)) { int sv = 0; int sw = 0; rep(i, sz) { if (t & (1 << i)) { sv += v[ids[i]]; sw += w[ids[i]]; } } if (sw <= l) { ans = max(ans, sv + dp[u][l - sw]); } } cout << ans << endl; } }
replace
30
31
30
31
TLE
p02648
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; typedef long long LL; #define N (1 << 19) #define M 100005 #define K (1 << 8) LL n, m, q, a[N], b[N], c[N], f[K + 5][M]; int main() { scanf("%lld", &n); for (LL i = 1; i <= n; ++i) { scanf("%lld%lld", a + i, b + i); } m = 100000; for (LL i = 1; i <= K && i <= n; ++i) { LL t = 0; for (LL j = i; j; j /= 2) c[t++] = j; for (LL j = 0; j < 1 << t; ++j) { LL w = 0, v = 0; for (LL k = 0; k < t; ++k) { if (j >> k & 1) { v += a[c[k]]; w += b[c[k]]; } } if (w <= m) f[i][w] = max(f[i][w], v); } for (LL j = 1; j <= m; ++j) f[i][j] = max(f[i][j], f[i][j - 1]); } scanf("%lld", &q); while (q--) { LL x, y; scanf("%lld%lld", &x, &y); LL t = 0, ans = 0; for (; x > K; x /= 2) c[t++] = x; for (LL i = 0; i < 1 << t; ++i) { LL w = 0, v = 0; for (LL k = 0; k < t; ++k) { if (i >> k & 1) { v += a[c[k]]; w += b[c[k]]; } } if (w <= y) ans = max(ans, f[x][y - w] + v); } printf("%lld\n", ans); } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long LL; #define N (1 << 19) #define M 100005 #define K (1 << 9) LL n, m, q, a[N], b[N], c[N], f[K + 5][M]; int main() { scanf("%lld", &n); for (LL i = 1; i <= n; ++i) { scanf("%lld%lld", a + i, b + i); } m = 100000; for (LL i = 1; i <= K && i <= n; ++i) { LL t = 0; for (LL j = i; j; j /= 2) c[t++] = j; for (LL j = 0; j < 1 << t; ++j) { LL w = 0, v = 0; for (LL k = 0; k < t; ++k) { if (j >> k & 1) { v += a[c[k]]; w += b[c[k]]; } } if (w <= m) f[i][w] = max(f[i][w], v); } for (LL j = 1; j <= m; ++j) f[i][j] = max(f[i][j], f[i][j - 1]); } scanf("%lld", &q); while (q--) { LL x, y; scanf("%lld%lld", &x, &y); LL t = 0, ans = 0; for (; x > K; x /= 2) c[t++] = x; for (LL i = 0; i < 1 << t; ++i) { LL w = 0, v = 0; for (LL k = 0; k < t; ++k) { if (i >> k & 1) { v += a[c[k]]; w += b[c[k]]; } } if (w <= y) ans = max(ans, f[x][y - w] + v); } printf("%lld\n", ans); } return 0; }
replace
7
8
7
8
TLE
p02648
C++
Runtime Error
#include <bits/stdc++.h> // #include <boost/multiprecision/cpp_int.hpp> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> #pragma GCC optimize("O3") #define REP(i, n) for (int i = 0; i < n; i++) #define REPP(i, n) for (int i = 1; i <= n; i++) #define ALL(obj) (obj).begin(), (obj).end() #define EPS (1e-9) #define INF (1e17) #define PI (acos(-1)) // const double PI = acos(-1); // const double EPS = 1e-15; // long long INF=(long long)1E17; // #define i_7 (long long)(1e9+7) #define i_7 998'244'353 long mod(long a) { long long c = a % i_7; if (c >= 0) return c; return c + i_7; } long long po(long a, long b) { if (b == 0) { return 1; } long long z = po(a, b / 2); z = mod(z * z); if (b % 2 != 0) { z = mod(a * z); } return z; } bool prime_(int n) { if (n == 1) { return false; } else if (n == 2) { return true; } else { for (int i = 2; i <= std::sqrt(n); i++) { if (n % i == 0) { return false; } } return true; } } long long gcd_(long long a, long long b) { if (a < b) { std::swap(a, b); } if (a % b == 0) { return b; } else { return gcd_(b, a % b); } } long long lcm_(long long x, long long y) { return (x / gcd_(x, y)) * y; } using namespace std; // using namespace boost::multiprecision; // using namespace __gnu_pbds; long long dp[512][100'010]; int main() { // using namespace std; int n; cin >> n; long long V[n], W[n]; REP(i, n) cin >> V[i] >> W[i]; int q; cin >> q; int v[q]; long long l[q]; REP(i, q) cin >> v[i] >> l[i]; int m = min(1 << 9, n); // vector<vector<long long>> dp(m, vector<long long>(100'010, 0)); REP(i, m) { REP(j, 100'010) { if (i == 0) { if (W[i] <= j) dp[i][j] = V[i]; continue; } dp[i][j] = dp[(i + 1) / 2 - 1][j]; if (j < W[i]) continue; dp[i][j] = max(dp[i][j], dp[(i + 1) / 2 - 1][j - W[i]] + V[i]); } } auto query = [&](int qv, long long ql) -> long long { long long res = 0; if (qv < m) return dp[qv][ql]; int tv = qv; typedef pair<long long, long long> P; vector<P> temp; while (tv >= m) { temp.emplace_back(V[tv], W[tv]); tv = (tv + 1) / 2 - 1; } int k = temp.size(); REP(s, 1 << k) { long long totv = 0, totw = 0; REP(i, k) { if (s >> i & 1) { totv += temp[i].first; totw += temp[i].second; } } if (totw > ql) continue; res = max(res, totv + dp[tv][ql - totw]); } return res; }; REP(i, q) { long long ans = query(v[i], l[i]); cout << ans << endl; } return 0; }
#include <bits/stdc++.h> // #include <boost/multiprecision/cpp_int.hpp> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> #pragma GCC optimize("O3") #define REP(i, n) for (int i = 0; i < n; i++) #define REPP(i, n) for (int i = 1; i <= n; i++) #define ALL(obj) (obj).begin(), (obj).end() #define EPS (1e-9) #define INF (1e17) #define PI (acos(-1)) // const double PI = acos(-1); // const double EPS = 1e-15; // long long INF=(long long)1E17; // #define i_7 (long long)(1e9+7) #define i_7 998'244'353 long mod(long a) { long long c = a % i_7; if (c >= 0) return c; return c + i_7; } long long po(long a, long b) { if (b == 0) { return 1; } long long z = po(a, b / 2); z = mod(z * z); if (b % 2 != 0) { z = mod(a * z); } return z; } bool prime_(int n) { if (n == 1) { return false; } else if (n == 2) { return true; } else { for (int i = 2; i <= std::sqrt(n); i++) { if (n % i == 0) { return false; } } return true; } } long long gcd_(long long a, long long b) { if (a < b) { std::swap(a, b); } if (a % b == 0) { return b; } else { return gcd_(b, a % b); } } long long lcm_(long long x, long long y) { return (x / gcd_(x, y)) * y; } using namespace std; // using namespace boost::multiprecision; // using namespace __gnu_pbds; long long dp[512][100'010]; int main() { // using namespace std; int n; cin >> n; long long V[n], W[n]; REP(i, n) cin >> V[i] >> W[i]; int q; cin >> q; int v[q]; long long l[q]; REP(i, q) { cin >> v[i] >> l[i]; v[i]--; } int m = min(1 << 9, n); // vector<vector<long long>> dp(m, vector<long long>(100'010, 0)); REP(i, m) { REP(j, 100'010) { if (i == 0) { if (W[i] <= j) dp[i][j] = V[i]; continue; } dp[i][j] = dp[(i + 1) / 2 - 1][j]; if (j < W[i]) continue; dp[i][j] = max(dp[i][j], dp[(i + 1) / 2 - 1][j - W[i]] + V[i]); } } auto query = [&](int qv, long long ql) -> long long { long long res = 0; if (qv < m) return dp[qv][ql]; int tv = qv; typedef pair<long long, long long> P; vector<P> temp; while (tv >= m) { temp.emplace_back(V[tv], W[tv]); tv = (tv + 1) / 2 - 1; } int k = temp.size(); REP(s, 1 << k) { long long totv = 0, totw = 0; REP(i, k) { if (s >> i & 1) { totv += temp[i].first; totw += temp[i].second; } } if (totw > ql) continue; res = max(res, totv + dp[tv][ql - totw]); } return res; }; REP(i, q) { long long ans = query(v[i], l[i]); cout << ans << endl; } return 0; }
replace
79
80
79
83
-11
p02648
C++
Time Limit Exceeded
// >>> TEMPLATES #include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using i32 = int32_t; using i64 = int64_t; using u32 = uint32_t; using u64 = uint64_t; #define int ll #define double ld #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep1(i, n) for (int i = 1; i <= (int)(n); i++) #define repR(i, n) for (int i = (int)(n)-1; i >= 0; i--) #define rep1R(i, n) for (int i = (int)(n); i >= 1; i--) #define loop(i, a, B) for (int i = a; i B; i++) #define loopR(i, a, B) for (int i = a; i B; i--) #define all(x) (x).begin(), (x).end() #define allR(x) (x).rbegin(), (x).rend() #define pb push_back #define eb emplace_back #define mp make_pair #define fst first #define snd second template <class Int> auto constexpr inf = numeric_limits<Int>::max() / 2 - 1; auto constexpr INF32 = inf<int32_t>; auto constexpr INF64 = inf<int64_t>; auto constexpr INF = inf<int>; #ifdef LOCAL #include "debug.hpp" #define dump(...) \ cerr << "[" << setw(3) << __LINE__ << ":" << __FUNCTION__ << "] ", \ dump_impl(#__VA_ARGS__, __VA_ARGS__) #define say(x) \ cerr << "[" << __LINE__ << ":" << __FUNCTION__ << "] " << x << endl #define debug if (1) #else #define dump(...) (void)(0) #define say(x) (void)(0) #define debug if (0) #endif template <class T> using pque_max = priority_queue<T>; template <class T> using pque_min = priority_queue<T, vector<T>, greater<T>>; template <class T, class = typename T::iterator, class = typename enable_if<!is_same<T, string>::value>::type> ostream &operator<<(ostream &os, T const &v) { bool f = true; for (auto const &x : v) os << (f ? "" : " ") << x, f = false; return os; } template <class T, class = typename T::iterator, class = typename enable_if<!is_same<T, string>::value>::type> istream &operator>>(istream &is, T &v) { for (auto &x : v) is >> x; return is; } template <class T, class S> ostream &operator<<(ostream &os, pair<T, S> const &p) { return os << "(" << p.first << ", " << p.second << ")"; } template <class T, class S> istream &operator>>(istream &is, pair<T, S> &p) { return is >> p.first >> p.second; } struct IOSetup { IOSetup() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } } iosetup; template <class F> struct FixPoint : private F { constexpr FixPoint(F &&f) : F(forward<F>(f)) {} template <class... T> constexpr auto operator()(T &&...x) const { return F::operator()(*this, forward<T>(x)...); } }; struct MakeFixPoint { template <class F> constexpr auto operator|(F &&f) const { return FixPoint<F>(forward<F>(f)); } }; #define MFP MakeFixPoint() | #define def(name, ...) auto name = MFP[&](auto &&name, __VA_ARGS__) template <class T, size_t d> struct vec_impl { using type = vector<typename vec_impl<T, d - 1>::type>; template <class... U> static type make_v(size_t n, U &&...x) { return type(n, vec_impl<T, d - 1>::make_v(forward<U>(x)...)); } }; template <class T> struct vec_impl<T, 0> { using type = T; static type make_v(T const &x = {}) { return x; } }; template <class T, size_t d = 1> using vec = typename vec_impl<T, d>::type; template <class T, size_t d = 1, class... Args> auto make_v(Args &&...args) { return vec_impl<T, d>::make_v(forward<Args>(args)...); } template <class T> void quit(T const &x) { cout << x << endl; exit(0); } template <class T, class U> constexpr bool chmin(T &x, U const &y) { if (x > y) { x = y; return true; } return false; } template <class T, class U> constexpr bool chmax(T &x, U const &y) { if (x < y) { x = y; return true; } return false; } template <class It> constexpr auto sumof(It b, It e) { return accumulate(b, e, typename iterator_traits<It>::value_type{}); } template <class T> int sz(T const &x) { return x.size(); } template <class C, class T> int lbd(C const &v, T const &x) { return lower_bound(v.begin(), v.end(), x) - v.begin(); } template <class C, class T> int ubd(C const &v, T const &x) { return upper_bound(v.begin(), v.end(), x) - v.begin(); } template <class C, class F> int ppt(C const &v, F f) { return partition_point(v.begin(), v.end(), f) - v.begin(); } // <<< int32_t main() { int n; cin >> n; vector<int> V(n + 1), W(n + 1); rep1(i, n) cin >> V[i] >> W[i]; int Q; cin >> Q; vector<int> x(Q), L(Q); rep(i, Q) cin >> x[i] >> L[i]; int s = 1, H = 0; while (s <= n) s <<= 1, H++; dump(n, s); V.resize(s + 5, 0); W.resize(s + 5, INF); n = s; int h = H / 2; int m = 1LL << h; dump(n, m); dump(V); dump(W); vector<map<int, int>> a(m); map<int, int> tmp; a[0][0] = 0; rep1(i, m - 1) { tmp.clear(); for (auto [w, v] : a[i / 2]) { chmax(tmp[w], v); chmax(tmp[w + W[i]], v + V[i]); } int ma = 0; for (auto [w, v] : tmp) { chmax(ma, v); a[i][w] = ma; } dump(i, a[i]); } vector<int> ans(Q); rep(i, Q) { if (x[i] < m) { auto it = --a[x[i]].upper_bound(L[i]); ans[i] = it->snd; continue; } vector<pair<int, int>> b; int idx = x[i]; for (; idx >= m; idx /= 2) { dump(idx); b.eb(W[idx], V[idx]); } dump(i, x[i], idx, b); rep(bit, 1LL << b.size()) { int w = 0, v = 0; rep(j, b.size()) if (bit >> j & 1) { w += b[j].fst; v += b[j].snd; } if (w > L[i]) continue; auto it = --a[idx].upper_bound(L[i] - w); chmax(ans[i], v + it->snd); } } rep(i, Q) cout << ans[i] << "\n"; }
// >>> TEMPLATES #include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using i32 = int32_t; using i64 = int64_t; using u32 = uint32_t; using u64 = uint64_t; #define int ll #define double ld #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep1(i, n) for (int i = 1; i <= (int)(n); i++) #define repR(i, n) for (int i = (int)(n)-1; i >= 0; i--) #define rep1R(i, n) for (int i = (int)(n); i >= 1; i--) #define loop(i, a, B) for (int i = a; i B; i++) #define loopR(i, a, B) for (int i = a; i B; i--) #define all(x) (x).begin(), (x).end() #define allR(x) (x).rbegin(), (x).rend() #define pb push_back #define eb emplace_back #define mp make_pair #define fst first #define snd second template <class Int> auto constexpr inf = numeric_limits<Int>::max() / 2 - 1; auto constexpr INF32 = inf<int32_t>; auto constexpr INF64 = inf<int64_t>; auto constexpr INF = inf<int>; #ifdef LOCAL #include "debug.hpp" #define dump(...) \ cerr << "[" << setw(3) << __LINE__ << ":" << __FUNCTION__ << "] ", \ dump_impl(#__VA_ARGS__, __VA_ARGS__) #define say(x) \ cerr << "[" << __LINE__ << ":" << __FUNCTION__ << "] " << x << endl #define debug if (1) #else #define dump(...) (void)(0) #define say(x) (void)(0) #define debug if (0) #endif template <class T> using pque_max = priority_queue<T>; template <class T> using pque_min = priority_queue<T, vector<T>, greater<T>>; template <class T, class = typename T::iterator, class = typename enable_if<!is_same<T, string>::value>::type> ostream &operator<<(ostream &os, T const &v) { bool f = true; for (auto const &x : v) os << (f ? "" : " ") << x, f = false; return os; } template <class T, class = typename T::iterator, class = typename enable_if<!is_same<T, string>::value>::type> istream &operator>>(istream &is, T &v) { for (auto &x : v) is >> x; return is; } template <class T, class S> ostream &operator<<(ostream &os, pair<T, S> const &p) { return os << "(" << p.first << ", " << p.second << ")"; } template <class T, class S> istream &operator>>(istream &is, pair<T, S> &p) { return is >> p.first >> p.second; } struct IOSetup { IOSetup() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } } iosetup; template <class F> struct FixPoint : private F { constexpr FixPoint(F &&f) : F(forward<F>(f)) {} template <class... T> constexpr auto operator()(T &&...x) const { return F::operator()(*this, forward<T>(x)...); } }; struct MakeFixPoint { template <class F> constexpr auto operator|(F &&f) const { return FixPoint<F>(forward<F>(f)); } }; #define MFP MakeFixPoint() | #define def(name, ...) auto name = MFP[&](auto &&name, __VA_ARGS__) template <class T, size_t d> struct vec_impl { using type = vector<typename vec_impl<T, d - 1>::type>; template <class... U> static type make_v(size_t n, U &&...x) { return type(n, vec_impl<T, d - 1>::make_v(forward<U>(x)...)); } }; template <class T> struct vec_impl<T, 0> { using type = T; static type make_v(T const &x = {}) { return x; } }; template <class T, size_t d = 1> using vec = typename vec_impl<T, d>::type; template <class T, size_t d = 1, class... Args> auto make_v(Args &&...args) { return vec_impl<T, d>::make_v(forward<Args>(args)...); } template <class T> void quit(T const &x) { cout << x << endl; exit(0); } template <class T, class U> constexpr bool chmin(T &x, U const &y) { if (x > y) { x = y; return true; } return false; } template <class T, class U> constexpr bool chmax(T &x, U const &y) { if (x < y) { x = y; return true; } return false; } template <class It> constexpr auto sumof(It b, It e) { return accumulate(b, e, typename iterator_traits<It>::value_type{}); } template <class T> int sz(T const &x) { return x.size(); } template <class C, class T> int lbd(C const &v, T const &x) { return lower_bound(v.begin(), v.end(), x) - v.begin(); } template <class C, class T> int ubd(C const &v, T const &x) { return upper_bound(v.begin(), v.end(), x) - v.begin(); } template <class C, class F> int ppt(C const &v, F f) { return partition_point(v.begin(), v.end(), f) - v.begin(); } // <<< int32_t main() { int n; cin >> n; vector<int> V(n + 1), W(n + 1); rep1(i, n) cin >> V[i] >> W[i]; int Q; cin >> Q; vector<int> x(Q), L(Q); rep(i, Q) cin >> x[i] >> L[i]; int s = 1, H = 0; while (s <= n) s <<= 1, H++; dump(n, s); V.resize(s + 5, 0); W.resize(s + 5, INF); n = s; int h = min(__lg((int)cbrt(Q * n) + 1), H); int m = 1LL << h; dump(n, m); dump(V); dump(W); vector<map<int, int>> a(m); map<int, int> tmp; a[0][0] = 0; rep1(i, m - 1) { tmp.clear(); for (auto [w, v] : a[i / 2]) { chmax(tmp[w], v); chmax(tmp[w + W[i]], v + V[i]); } int ma = 0; for (auto [w, v] : tmp) { chmax(ma, v); a[i][w] = ma; } dump(i, a[i]); } vector<int> ans(Q); rep(i, Q) { if (x[i] < m) { auto it = --a[x[i]].upper_bound(L[i]); ans[i] = it->snd; continue; } vector<pair<int, int>> b; int idx = x[i]; for (; idx >= m; idx /= 2) { dump(idx); b.eb(W[idx], V[idx]); } dump(i, x[i], idx, b); rep(bit, 1LL << b.size()) { int w = 0, v = 0; rep(j, b.size()) if (bit >> j & 1) { w += b[j].fst; v += b[j].snd; } if (w > L[i]) continue; auto it = --a[idx].upper_bound(L[i] - w); chmax(ans[i], v + it->snd); } } rep(i, Q) cout << ans[i] << "\n"; }
replace
149
150
149
150
TLE
p02648
C++
Runtime Error
#pragma GCC target("sse,sse2,sse3,sse4,sse4.1,sse4.2,avx,avx2") #include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; #define all(x) x.begin(), x.end() template <typename T1, typename T2> inline void chkmin(T1 &x, const T2 &y) { if (x > y) x = y; } template <typename T1, typename T2> inline void chkmax(T1 &x, const T2 &y) { if (x < y) x = y; } namespace fast { /** Fast input-output */ template <class T = int> inline T readInt(); inline double readDouble(); inline int readUInt(); inline int readChar(); // first non-blank character inline void readWord(char *s); inline bool readLine(char *s); // do not save '\n' inline bool isEof(); inline int getChar(); inline int peekChar(); inline bool seekEof(); inline void skipBlanks(); template <class T> inline void writeInt(T x, char end = 0, int len = -1); inline void writeChar(int x); inline void writeWord(const char *s); inline void writeDouble(double x, int len = 0); inline void flush(); static struct buffer_flusher_t { ~buffer_flusher_t() { flush(); } } buffer_flusher; /** Read */ static const int buf_size = 4096; static unsigned char buf[buf_size]; static int buf_len = 0, buf_pos = 0; inline bool isEof() { if (buf_pos == buf_len) { buf_pos = 0, buf_len = fread(buf, 1, buf_size, stdin); if (buf_pos == buf_len) return 1; } return 0; } inline int getChar() { return isEof() ? -1 : buf[buf_pos++]; } inline int peekChar() { return isEof() ? -1 : buf[buf_pos]; } inline bool seekEof() { int c; while ((c = peekChar()) != -1 && c <= 32) buf_pos++; return c == -1; } inline void skipBlanks() { while (!isEof() && buf[buf_pos] <= 32U) buf_pos++; } inline int readChar() { int c = getChar(); while (c != -1 && c <= 32) c = getChar(); return c; } inline int readUInt() { int c = readChar(), x = 0; while ('0' <= c && c <= '9') x = x * 10 + c - '0', c = getChar(); return x; } template <class T> inline T readInt() { int s = 1, c = readChar(); T x = 0; if (c == '-') s = -1, c = getChar(); else if (c == '+') c = getChar(); while ('0' <= c && c <= '9') x = x * 10 + c - '0', c = getChar(); return s == 1 ? x : -x; } inline double readDouble() { int s = 1, c = readChar(); double x = 0; if (c == '-') s = -1, c = getChar(); while ('0' <= c && c <= '9') x = x * 10 + c - '0', c = getChar(); if (c == '.') { c = getChar(); double coef = 1; while ('0' <= c && c <= '9') x += (c - '0') * (coef *= 1e-1), c = getChar(); } return s == 1 ? x : -x; } inline void readWord(char *s) { int c = readChar(); while (c > 32) *s++ = c, c = getChar(); *s = 0; } inline bool readLine(char *s) { int c = getChar(); while (c != '\n' && c != -1) *s++ = c, c = getChar(); *s = 0; return c != -1; } /** Write */ static int write_buf_pos = 0; static char write_buf[buf_size]; inline void writeChar(int x) { if (write_buf_pos == buf_size) fwrite(write_buf, 1, buf_size, stdout), write_buf_pos = 0; write_buf[write_buf_pos++] = x; } inline void flush() { if (write_buf_pos) { fwrite(write_buf, 1, write_buf_pos, stdout), write_buf_pos = 0; fflush(stdout); } } template <class T> inline void writeInt(T x, char end, int output_len) { if (x < 0) writeChar('-'), x = -x; char s[24]; int n = 0; while (x || !n) s[n++] = '0' + x % 10, x /= 10; while (n < output_len) s[n++] = '0'; while (n--) writeChar(s[n]); if (end) writeChar(end); } inline void writeWord(const char *s) { while (*s) writeChar(*s++); } inline void writeDouble(double x, int output_len) { if (x < 0) writeChar('-'), x = -x; int t = (int)x; writeInt(t), x -= t; writeChar('.'); for (int i = output_len - 1; i > 0; i--) { x *= 10; t = std::min(9, (int)x); writeChar('0' + t), x -= t; } x *= 10; t = std::min(9, (int)(x + 0.5)); writeChar('0' + t); } } // namespace fast using namespace fast; const int MAXLOG = 18; const int MAXN = ((1 << MAXLOG) + 228) * 10; const int BUBEN = 12; int n; int v[MAXN], w[MAXN]; void read() { n = readInt(); for (int i = 1; i <= n; ++i) { v[i] = readInt(); w[i] = readInt(); } } int getHeight(int v) { int ans = 0; while (v) { ans++; v /= 2; } return ans; } pair<int, int> fans[MAXN]; int topFans[MAXN]; int start[MAXN]; int revOrder[MAXN]; int have[MAXLOG]; int topHave; void build(int v) { topHave = 0; while (v) { have[topHave++] = v; v /= 2; } reverse(have, have + topHave); } void precalc() { int pos = 0; for (int V = 0; V < n; ++V) { if (getHeight(V) != BUBEN) continue; revOrder[V] = pos; build(V); topFans[pos] = 0; if (pos > 0) start[pos] = start[pos - 1] + topFans[pos - 1]; for (int mask = 0; mask < (1 << topHave); ++mask) { int sum = 0; int now = 0; for (int bit = 0; bit < topHave; ++bit) { if ((mask >> bit) & 1) { sum += w[have[bit]]; now += v[have[bit]]; } } fans[start[pos] + topFans[pos]++] = {sum, now}; } sort(fans + start[pos], fans + start[pos] + topFans[pos]); for (int i = 1; i < topFans[pos]; ++i) { chkmax(fans[start[pos] + i].second, fans[start[pos] + i - 1].second); } ++pos; } } void stupid(int l) { int ans = 0; for (int mask = 0; mask < (1 << topHave); mask++) { int sum = 0; int now = 0; for (int bit = 0; bit < topHave; bit++) { if ((mask >> bit) & 1) { sum += w[have[bit]]; now += v[have[bit]]; } if (sum > l) break; } if (sum <= l) { chkmax(ans, now); } } writeInt(ans, '\n'); } void smart(int l) { int szL = BUBEN; int szR = topHave - szL; int pos = revOrder[have[BUBEN - 1]]; int ans = 0; for (int mask = 0; mask < (1 << szR); ++mask) { int sum = 0; int now = 0; for (int bit = 0; bit < szR; ++bit) { if ((mask >> bit) & 1) { sum += w[have[bit + szL]]; now += v[have[bit + szL]]; } if (sum > l) break; } if (sum > l) continue; chkmax(ans, now); int mypos = lower_bound(fans + start[pos], fans + start[pos] + topFans[pos], make_pair(l - sum + 1, 0)) - fans - start[pos]; mypos--; if (mypos >= 0 && mypos < topFans[pos]) { chkmax(ans, now + fans[start[pos] + mypos].second); } } writeInt(ans, '\n'); } void solve() { int v, l; v = readInt(); l = readInt(); build(v); if (topHave <= BUBEN) { stupid(l); } else { smart(l); } } void run() { precalc(); int q; q = readInt(); while (q--) { solve(); } } signed main() { read(); run(); return 0; }
#pragma GCC target("sse,sse2,sse3,sse4,sse4.1,sse4.2,avx,avx2") #include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; #define all(x) x.begin(), x.end() template <typename T1, typename T2> inline void chkmin(T1 &x, const T2 &y) { if (x > y) x = y; } template <typename T1, typename T2> inline void chkmax(T1 &x, const T2 &y) { if (x < y) x = y; } namespace fast { /** Fast input-output */ template <class T = int> inline T readInt(); inline double readDouble(); inline int readUInt(); inline int readChar(); // first non-blank character inline void readWord(char *s); inline bool readLine(char *s); // do not save '\n' inline bool isEof(); inline int getChar(); inline int peekChar(); inline bool seekEof(); inline void skipBlanks(); template <class T> inline void writeInt(T x, char end = 0, int len = -1); inline void writeChar(int x); inline void writeWord(const char *s); inline void writeDouble(double x, int len = 0); inline void flush(); static struct buffer_flusher_t { ~buffer_flusher_t() { flush(); } } buffer_flusher; /** Read */ static const int buf_size = 4096; static unsigned char buf[buf_size]; static int buf_len = 0, buf_pos = 0; inline bool isEof() { if (buf_pos == buf_len) { buf_pos = 0, buf_len = fread(buf, 1, buf_size, stdin); if (buf_pos == buf_len) return 1; } return 0; } inline int getChar() { return isEof() ? -1 : buf[buf_pos++]; } inline int peekChar() { return isEof() ? -1 : buf[buf_pos]; } inline bool seekEof() { int c; while ((c = peekChar()) != -1 && c <= 32) buf_pos++; return c == -1; } inline void skipBlanks() { while (!isEof() && buf[buf_pos] <= 32U) buf_pos++; } inline int readChar() { int c = getChar(); while (c != -1 && c <= 32) c = getChar(); return c; } inline int readUInt() { int c = readChar(), x = 0; while ('0' <= c && c <= '9') x = x * 10 + c - '0', c = getChar(); return x; } template <class T> inline T readInt() { int s = 1, c = readChar(); T x = 0; if (c == '-') s = -1, c = getChar(); else if (c == '+') c = getChar(); while ('0' <= c && c <= '9') x = x * 10 + c - '0', c = getChar(); return s == 1 ? x : -x; } inline double readDouble() { int s = 1, c = readChar(); double x = 0; if (c == '-') s = -1, c = getChar(); while ('0' <= c && c <= '9') x = x * 10 + c - '0', c = getChar(); if (c == '.') { c = getChar(); double coef = 1; while ('0' <= c && c <= '9') x += (c - '0') * (coef *= 1e-1), c = getChar(); } return s == 1 ? x : -x; } inline void readWord(char *s) { int c = readChar(); while (c > 32) *s++ = c, c = getChar(); *s = 0; } inline bool readLine(char *s) { int c = getChar(); while (c != '\n' && c != -1) *s++ = c, c = getChar(); *s = 0; return c != -1; } /** Write */ static int write_buf_pos = 0; static char write_buf[buf_size]; inline void writeChar(int x) { if (write_buf_pos == buf_size) fwrite(write_buf, 1, buf_size, stdout), write_buf_pos = 0; write_buf[write_buf_pos++] = x; } inline void flush() { if (write_buf_pos) { fwrite(write_buf, 1, write_buf_pos, stdout), write_buf_pos = 0; fflush(stdout); } } template <class T> inline void writeInt(T x, char end, int output_len) { if (x < 0) writeChar('-'), x = -x; char s[24]; int n = 0; while (x || !n) s[n++] = '0' + x % 10, x /= 10; while (n < output_len) s[n++] = '0'; while (n--) writeChar(s[n]); if (end) writeChar(end); } inline void writeWord(const char *s) { while (*s) writeChar(*s++); } inline void writeDouble(double x, int output_len) { if (x < 0) writeChar('-'), x = -x; int t = (int)x; writeInt(t), x -= t; writeChar('.'); for (int i = output_len - 1; i > 0; i--) { x *= 10; t = std::min(9, (int)x); writeChar('0' + t), x -= t; } x *= 10; t = std::min(9, (int)(x + 0.5)); writeChar('0' + t); } } // namespace fast using namespace fast; const int MAXLOG = 18; const int MAXN = ((1 << MAXLOG) + 228) * 50; const int BUBEN = 12; int n; int v[MAXN], w[MAXN]; void read() { n = readInt(); for (int i = 1; i <= n; ++i) { v[i] = readInt(); w[i] = readInt(); } } int getHeight(int v) { int ans = 0; while (v) { ans++; v /= 2; } return ans; } pair<int, int> fans[MAXN]; int topFans[MAXN]; int start[MAXN]; int revOrder[MAXN]; int have[MAXLOG]; int topHave; void build(int v) { topHave = 0; while (v) { have[topHave++] = v; v /= 2; } reverse(have, have + topHave); } void precalc() { int pos = 0; for (int V = 0; V < n; ++V) { if (getHeight(V) != BUBEN) continue; revOrder[V] = pos; build(V); topFans[pos] = 0; if (pos > 0) start[pos] = start[pos - 1] + topFans[pos - 1]; for (int mask = 0; mask < (1 << topHave); ++mask) { int sum = 0; int now = 0; for (int bit = 0; bit < topHave; ++bit) { if ((mask >> bit) & 1) { sum += w[have[bit]]; now += v[have[bit]]; } } fans[start[pos] + topFans[pos]++] = {sum, now}; } sort(fans + start[pos], fans + start[pos] + topFans[pos]); for (int i = 1; i < topFans[pos]; ++i) { chkmax(fans[start[pos] + i].second, fans[start[pos] + i - 1].second); } ++pos; } } void stupid(int l) { int ans = 0; for (int mask = 0; mask < (1 << topHave); mask++) { int sum = 0; int now = 0; for (int bit = 0; bit < topHave; bit++) { if ((mask >> bit) & 1) { sum += w[have[bit]]; now += v[have[bit]]; } if (sum > l) break; } if (sum <= l) { chkmax(ans, now); } } writeInt(ans, '\n'); } void smart(int l) { int szL = BUBEN; int szR = topHave - szL; int pos = revOrder[have[BUBEN - 1]]; int ans = 0; for (int mask = 0; mask < (1 << szR); ++mask) { int sum = 0; int now = 0; for (int bit = 0; bit < szR; ++bit) { if ((mask >> bit) & 1) { sum += w[have[bit + szL]]; now += v[have[bit + szL]]; } if (sum > l) break; } if (sum > l) continue; chkmax(ans, now); int mypos = lower_bound(fans + start[pos], fans + start[pos] + topFans[pos], make_pair(l - sum + 1, 0)) - fans - start[pos]; mypos--; if (mypos >= 0 && mypos < topFans[pos]) { chkmax(ans, now + fans[start[pos] + mypos].second); } } writeInt(ans, '\n'); } void solve() { int v, l; v = readInt(); l = readInt(); build(v); if (topHave <= BUBEN) { stupid(l); } else { smart(l); } } void run() { precalc(); int q; q = readInt(); while (q--) { solve(); } } signed main() { read(); run(); return 0; }
replace
191
192
191
192
0
p02648
C++
Runtime Error
#include <bits/stdc++.h> using namespace std; #define Sz(a) ((int)(a).size()) const int maxn = 200009; const int MOD = 1e9 + 7; int V[maxn], W[maxn], n, q, dp[555][100009]; int main() { #ifdef DEBUG freopen("a.in", "r", stdin); #endif ios_base::sync_with_stdio(false); cout.tie(0); cin.tie(0); cin >> n; for (int i = 1; i <= n; ++i) cin >> W[i] >> V[i]; for (int i = 1; i <= 512; ++i) { for (int j = 0; j <= 100000; ++j) dp[i][j] = dp[i >> 1][j]; for (int j = 0; j <= 100000; ++j) if (j >= V[i]) dp[i][j] = max(dp[i >> 1][j - V[i]] + W[i], dp[i][j]); } cin >> q; for (int ts = 1; ts <= q; ++ts) { int v, l, ans = 0; cin >> v >> l; if (v <= 512) { ans = dp[v][l]; } else { vector<int> idx; for (; v > 512; v >>= 1) idx.push_back(v); int totv, totw; for (int i = 0; i < (1 << Sz(idx)); ++i) { totv = totw = 0; for (int j = 0; j < Sz(idx); ++j) if (i >> j & 1) totv += V[idx[j]], totw += W[idx[j]]; if (totv <= l) { ans = max(dp[v][l - totv] + totw, ans); } } } cout << ans << '\n'; } return 0; } // 2020.06.14 10:06:45
#include <bits/stdc++.h> using namespace std; #define Sz(a) ((int)(a).size()) const int maxn = 1 << 19; const int MOD = 1e9 + 7; int V[maxn], W[maxn], n, q, dp[555][100009]; int main() { #ifdef DEBUG freopen("a.in", "r", stdin); #endif ios_base::sync_with_stdio(false); cout.tie(0); cin.tie(0); cin >> n; for (int i = 1; i <= n; ++i) cin >> W[i] >> V[i]; for (int i = 1; i <= 512; ++i) { for (int j = 0; j <= 100000; ++j) dp[i][j] = dp[i >> 1][j]; for (int j = 0; j <= 100000; ++j) if (j >= V[i]) dp[i][j] = max(dp[i >> 1][j - V[i]] + W[i], dp[i][j]); } cin >> q; for (int ts = 1; ts <= q; ++ts) { int v, l, ans = 0; cin >> v >> l; if (v <= 512) { ans = dp[v][l]; } else { vector<int> idx; for (; v > 512; v >>= 1) idx.push_back(v); int totv, totw; for (int i = 0; i < (1 << Sz(idx)); ++i) { totv = totw = 0; for (int j = 0; j < Sz(idx); ++j) if (i >> j & 1) totv += V[idx[j]], totw += W[idx[j]]; if (totv <= l) { ans = max(dp[v][l - totv] + totw, ans); } } } cout << ans << '\n'; } return 0; } // 2020.06.14 10:06:45
replace
3
4
3
4
-11
p02648
C++
Time Limit Exceeded
#include <bits/stdc++.h> using namespace std; using vi = vector<int>; using vl = vector<long>; using vvl = vector<vector<long>>; using pll = pair<long, long>; #define rep(i, n) for (int i = 0; i < (int)(n); i++) long K = 9; long D = 512; vvl dp(D + 1, vl(100010, 0)); vector<long> V(262144, 0), W(262144, 1e6); void dfs(int now, int d) { long v = V.at(now - 1); long w = W.at(now - 1); rep(j, 100001) { if (j - w >= 0) dp.at(now).at(j) = max(dp.at(now / 2).at(j), dp.at(now / 2).at(j - w) + v); else dp.at(now).at(j) = dp.at(now / 2).at(j); } if (d == K - 1) return; dfs(now * 2, d + 1); dfs(now * 2 + 1, d + 1); } long search(int now, long v, long w, long L) { if (now < D) { if (w > L) return 0; else return v + dp.at(now).at(L - w); } return max(search(now / 2, v + V.at(now - 1), w + W.at(now - 1), L), search(now / 2, v, w, L)); } int main() { long n; cin >> n; rep(i, n) cin >> V.at(i) >> W.at(i); dfs(1, 0); int q; cin >> q; rep(i, q) { long v, l; cin >> v >> l; if (v < D) { cout << dp[v][l] << "\n"; } else { cout << search(v, 0, 0, l) << "\n"; } } }
#include <bits/stdc++.h> using namespace std; using vi = vector<int>; using vl = vector<long>; using vvl = vector<vector<long>>; using pll = pair<long, long>; #define rep(i, n) for (int i = 0; i < (int)(n); i++) long K = 9; long D = 512; vvl dp(D + 1, vl(100010, 0)); vector<long> V(262144, 0), W(262144, 1e6); void dfs(int now, int d) { long v = V.at(now - 1); long w = W.at(now - 1); rep(j, 100001) { if (j - w >= 0) dp.at(now).at(j) = max(dp.at(now / 2).at(j), dp.at(now / 2).at(j - w) + v); else dp.at(now).at(j) = dp.at(now / 2).at(j); } if (d == K - 1) return; dfs(now * 2, d + 1); dfs(now * 2 + 1, d + 1); } long search(int now, long v, long w, long L) { if (now < D) { if (w > L) return 0; else return v + dp.at(now).at(L - w); } return max(search(now / 2, v + V.at(now - 1), w + W.at(now - 1), L), search(now / 2, v, w, L)); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); long n; cin >> n; rep(i, n) cin >> V.at(i) >> W.at(i); dfs(1, 0); int q; cin >> q; rep(i, q) { long v, l; cin >> v >> l; if (v < D) { cout << dp[v][l] << "\n"; } else { cout << search(v, 0, 0, l) << "\n"; } } }
insert
42
42
42
44
TLE
p02648
C++
Time Limit Exceeded
#include "bits/stdc++.h" #include "ext/pb_ds/assoc_container.hpp" #include "ext/pb_ds/tree_policy.hpp" #include "ext/rope" using namespace std; using namespace chrono; using namespace __gnu_pbds; using namespace __gnu_cxx; mt19937 rng(high_resolution_clock::now().time_since_epoch().count()); mt19937_64 rngll(high_resolution_clock::now().time_since_epoch().count()); #define lambdify(x) \ [&](auto &&...args) { return x(forward<decltype(args)>(args)...); } template <typename T, typename U> T &ctmax(T &x, const U &y) { return x = max<T>(x, y); } template <typename T, typename U> T &ctmin(T &x, const U &y) { return x = min<T>(x, y); } template <typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; int main() { cin.tie(0)->sync_with_stdio(0); int n; cin >> n; vector<array<int, 2>> a(n + 1); for (auto i = 1; i <= n; ++i) { for (auto j = 0; j < 2; ++j) { cin >> a[i][j]; } } const int h = 3, mx = 1e5; vector<vector<long long>> dp(1 << h, vector<long long>(mx + 1)); for (auto u = 1; u < min(1 << h, n + 1); ++u) { int p = u >> 1; dp[u] = dp[p]; for (auto x = a[u][1]; x <= mx; ++x) { ctmax(dp[u][x], max(dp[u][x - 1], dp[p][x - a[u][1]] + a[u][0])); } } int qq; cin >> qq; while (qq--) { int u, x; cin >> u >> x; if (__lg(u) < h) { cout << dp[u][x] << "\n"; } else { int d = __lg(u) - h + 1; long long res = dp[u >> d][x], sv = 0, sw = 0; for (auto mask = 1; mask < 1 << d; ++mask) { for (auto bit = 0; bit < d; ++bit) { if (mask & 1 << bit) { sv += a[u >> bit][0]; sw += a[u >> bit][1]; break; } else { sv -= a[u >> bit][0]; sw -= a[u >> bit][1]; } } if (sw <= x) ctmax(res, dp[u >> d][x - sw] + sv); } cout << res << "\n"; } } return 0; } /* */ //////////////////////////////////////////////////////////////////////////////////////// // // // Coded by Aeren // // // ////////////////////////////////////////////////////////////////////////////////////////
#include "bits/stdc++.h" #include "ext/pb_ds/assoc_container.hpp" #include "ext/pb_ds/tree_policy.hpp" #include "ext/rope" using namespace std; using namespace chrono; using namespace __gnu_pbds; using namespace __gnu_cxx; mt19937 rng(high_resolution_clock::now().time_since_epoch().count()); mt19937_64 rngll(high_resolution_clock::now().time_since_epoch().count()); #define lambdify(x) \ [&](auto &&...args) { return x(forward<decltype(args)>(args)...); } template <typename T, typename U> T &ctmax(T &x, const U &y) { return x = max<T>(x, y); } template <typename T, typename U> T &ctmin(T &x, const U &y) { return x = min<T>(x, y); } template <typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; int main() { cin.tie(0)->sync_with_stdio(0); int n; cin >> n; vector<array<int, 2>> a(n + 1); for (auto i = 1; i <= n; ++i) { for (auto j = 0; j < 2; ++j) { cin >> a[i][j]; } } const int h = 9, mx = 1e5; vector<vector<long long>> dp(1 << h, vector<long long>(mx + 1)); for (auto u = 1; u < min(1 << h, n + 1); ++u) { int p = u >> 1; dp[u] = dp[p]; for (auto x = a[u][1]; x <= mx; ++x) { ctmax(dp[u][x], max(dp[u][x - 1], dp[p][x - a[u][1]] + a[u][0])); } } int qq; cin >> qq; while (qq--) { int u, x; cin >> u >> x; if (__lg(u) < h) { cout << dp[u][x] << "\n"; } else { int d = __lg(u) - h + 1; long long res = dp[u >> d][x], sv = 0, sw = 0; for (auto mask = 1; mask < 1 << d; ++mask) { for (auto bit = 0; bit < d; ++bit) { if (mask & 1 << bit) { sv += a[u >> bit][0]; sw += a[u >> bit][1]; break; } else { sv -= a[u >> bit][0]; sw -= a[u >> bit][1]; } } if (sw <= x) ctmax(res, dp[u >> d][x - sw] + sv); } cout << res << "\n"; } } return 0; } /* */ //////////////////////////////////////////////////////////////////////////////////////// // // // Coded by Aeren // // // ////////////////////////////////////////////////////////////////////////////////////////
replace
32
33
32
33
TLE
p02648
C++
Time Limit Exceeded
#include "bits/stdc++.h" #include "ext/pb_ds/assoc_container.hpp" #include "ext/pb_ds/tree_policy.hpp" #include "ext/rope" using namespace std; using namespace chrono; using namespace __gnu_pbds; using namespace __gnu_cxx; mt19937 rng(high_resolution_clock::now().time_since_epoch().count()); mt19937_64 rngll(high_resolution_clock::now().time_since_epoch().count()); #define lambdify(x) \ [&](auto &&...args) { return x(forward<decltype(args)>(args)...); } template <typename T, typename U> T &ctmax(T &x, const U &y) { return x = max<T>(x, y); } template <typename T, typename U> T &ctmin(T &x, const U &y) { return x = min<T>(x, y); } template <typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; int main() { cin.tie(0)->sync_with_stdio(0); int n; cin >> n; const int mx = 1e5; vector<array<int, 2>> val(n); // value, weight for (auto i = 0; i < n; ++i) { for (auto j = 0; j < 2; ++j) { cin >> val[i][j]; } } const int h = 9; vector<vector<long long>> dp((1 << h) - 1, vector<long long>(mx + 1)); // dp[u][x]: max value obtainable for node u with weight at most x for (auto x = val[0][1]; x <= mx; ++x) { dp[0][x] = val[0][0]; } for (auto u = 1; u < min((1 << h) - 1, n); ++u) { int p = u - 1 >> 1; dp[u] = dp[p]; for (auto x = mx; x >= val[u][1]; --x) { ctmax(dp[u][x], max(dp[p][x - val[u][1]] + val[u][0], dp[u][x - 1])); } } vector<vector<int>> shift(n, vector<int>(15)); for (auto u = 1; u < n; ++u) { shift[u][0] = u; for (auto i = 1; i <= 14; ++i) { shift[u][i] = (shift[u][i - 1] - 1) >> 1; } } int qq; cin >> qq; while (qq--) { int u, x; cin >> u >> x, --u; int ch = __lg(u + 1); if (ch < h) { cout << dp[u][x] << "\n"; } else { int d = ch - h + 1, p = shift[u][d]; long long res = 0; for (auto mask = 0; mask < 1 << d; ++mask) { long long cv = 0, cw = 0; for (auto bit = 0; bit < d; ++bit) { if (mask & 1 << bit) { cv += val[shift[u][bit]][0]; cw += val[shift[u][bit]][1]; } } if (cw <= x) { ctmax(res, dp[p][x - cw] + cv); } } cout << res << "\n"; } } return 0; } /* */ //////////////////////////////////////////////////////////////////////////////////////// // // // Coded by Aeren // // // ////////////////////////////////////////////////////////////////////////////////////////
#include "bits/stdc++.h" #include "ext/pb_ds/assoc_container.hpp" #include "ext/pb_ds/tree_policy.hpp" #include "ext/rope" using namespace std; using namespace chrono; using namespace __gnu_pbds; using namespace __gnu_cxx; mt19937 rng(high_resolution_clock::now().time_since_epoch().count()); mt19937_64 rngll(high_resolution_clock::now().time_since_epoch().count()); #define lambdify(x) \ [&](auto &&...args) { return x(forward<decltype(args)>(args)...); } template <typename T, typename U> T &ctmax(T &x, const U &y) { return x = max<T>(x, y); } template <typename T, typename U> T &ctmin(T &x, const U &y) { return x = min<T>(x, y); } template <typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; int main() { cin.tie(0)->sync_with_stdio(0); int n; cin >> n; const int mx = 1e5; vector<array<int, 2>> val(n); // value, weight for (auto i = 0; i < n; ++i) { for (auto j = 0; j < 2; ++j) { cin >> val[i][j]; } } const int h = 10; vector<vector<long long>> dp((1 << h) - 1, vector<long long>(mx + 1)); // dp[u][x]: max value obtainable for node u with weight at most x for (auto x = val[0][1]; x <= mx; ++x) { dp[0][x] = val[0][0]; } for (auto u = 1; u < min((1 << h) - 1, n); ++u) { int p = u - 1 >> 1; dp[u] = dp[p]; for (auto x = mx; x >= val[u][1]; --x) { ctmax(dp[u][x], max(dp[p][x - val[u][1]] + val[u][0], dp[u][x - 1])); } } vector<vector<int>> shift(n, vector<int>(15)); for (auto u = 1; u < n; ++u) { shift[u][0] = u; for (auto i = 1; i <= 14; ++i) { shift[u][i] = (shift[u][i - 1] - 1) >> 1; } } int qq; cin >> qq; while (qq--) { int u, x; cin >> u >> x, --u; int ch = __lg(u + 1); if (ch < h) { cout << dp[u][x] << "\n"; } else { int d = ch - h + 1, p = shift[u][d]; long long res = 0; for (auto mask = 0; mask < 1 << d; ++mask) { long long cv = 0, cw = 0; for (auto bit = 0; bit < d; ++bit) { if (mask & 1 << bit) { cv += val[shift[u][bit]][0]; cw += val[shift[u][bit]][1]; } } if (cw <= x) { ctmax(res, dp[p][x - cw] + cv); } } cout << res << "\n"; } } return 0; } /* */ //////////////////////////////////////////////////////////////////////////////////////// // // // Coded by Aeren // // // ////////////////////////////////////////////////////////////////////////////////////////
replace
33
34
33
34
TLE