結果
問題 | No.2643 Many Range Sums Problems |
ユーザー | SSRS |
提出日時 | 2024-02-19 23:37:37 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 13 ms / 8,000 ms |
コード長 | 2,886 bytes |
コンパイル時間 | 2,018 ms |
コンパイル使用メモリ | 216,228 KB |
実行使用メモリ | 6,820 KB |
最終ジャッジ日時 | 2024-09-29 03:14:10 |
合計ジャッジ時間 | 3,113 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,816 KB |
testcase_02 | AC | 1 ms
6,816 KB |
testcase_03 | AC | 11 ms
6,816 KB |
testcase_04 | AC | 10 ms
6,820 KB |
testcase_05 | AC | 9 ms
6,816 KB |
testcase_06 | AC | 12 ms
6,816 KB |
testcase_07 | AC | 12 ms
6,816 KB |
testcase_08 | AC | 11 ms
6,816 KB |
testcase_09 | AC | 11 ms
6,816 KB |
testcase_10 | AC | 10 ms
6,820 KB |
testcase_11 | AC | 8 ms
6,816 KB |
testcase_12 | AC | 12 ms
6,816 KB |
testcase_13 | AC | 11 ms
6,816 KB |
testcase_14 | AC | 12 ms
6,816 KB |
testcase_15 | AC | 12 ms
6,816 KB |
testcase_16 | AC | 10 ms
6,820 KB |
testcase_17 | AC | 11 ms
6,816 KB |
testcase_18 | AC | 8 ms
6,816 KB |
testcase_19 | AC | 4 ms
6,820 KB |
testcase_20 | AC | 6 ms
6,816 KB |
testcase_21 | AC | 3 ms
6,820 KB |
testcase_22 | AC | 6 ms
6,816 KB |
testcase_23 | AC | 8 ms
6,816 KB |
testcase_24 | AC | 11 ms
6,816 KB |
testcase_25 | AC | 10 ms
6,816 KB |
testcase_26 | AC | 4 ms
6,820 KB |
testcase_27 | AC | 5 ms
6,820 KB |
testcase_28 | AC | 2 ms
6,816 KB |
testcase_29 | AC | 1 ms
6,816 KB |
testcase_30 | AC | 11 ms
6,820 KB |
testcase_31 | AC | 13 ms
6,820 KB |
testcase_32 | AC | 11 ms
6,816 KB |
testcase_33 | AC | 11 ms
6,816 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; const int LOG = 13; const long long INF = 1000000000000000000; int main(){ int N, K; cin >> N >> K; vector<int> R(N); vector<long long> X(N); for (int i = 0; i < N; i++){ cin >> R[i] >> X[i]; } vector<int> d(N + 1, 0); vector<long long> s(N + 1, 0); for (int i = N - 1; i >= 0; i--){ d[i] = d[R[i]] + 1; s[i] = s[R[i]] + X[i]; } vector<vector<int>> pp(LOG, vector<int>(N + 1, -1)); for (int i = 0; i < N; i++){ pp[0][i] = R[i]; } for (int i = 0; i < LOG - 1; i++){ for (int j = 0; j <= N; j++){ if (pp[i][j] != -1){ pp[i + 1][j] = pp[i][pp[i][j]]; } } } auto climb = [&](int v, int k){ for (int i = 0; i < LOG; i++){ if ((k >> i & 1) == 1){ v = pp[i][v]; } } return v; }; auto lca = [&](int u, int v){ if (d[u] > d[v]){ swap(u, v); } v = climb(v, d[v] - d[u]); if (u == v){ return u; } for (int i = LOG - 1; i >= 0; i--){ if (pp[i][u] != pp[i][v]){ u = pp[i][u]; v = pp[i][v]; } } return R[u]; }; vector<int> add(N + 1, 0); int add_all = 0; vector<vector<long long>> mn(LOG, vector<long long>(N + 1, -INF)); vector<vector<long long>> mx(LOG, vector<long long>(N + 1, INF)); auto path_chmax = [&](int u, int v, long long x){ if (u == v){ return; } int D = 31 - __builtin_clz(d[u] - d[v]); mn[D][u] = max(mn[D][u], x); int p = climb(u, (d[u] - d[v]) - (1 << D)); mn[D][p] = max(mn[D][p], x); }; auto path_chmin = [&](int u, int v, long long x){ if (u == v){ return; } int D = 31 - __builtin_clz(d[u] - d[v]); mx[D][u] = min(mx[D][u], x); int p = climb(u, (d[u] - d[v]) - (1 << D)); mx[D][p] = min(mx[D][p], x); }; for (int i = 0; i < N; i++){ int w = lca(i, i + 1); if (s[i] - s[i + 1] < 0 || s[i] - s[i + 1] > K){ add_all++; add[i]--; add[i + 1]--; add[w] += 2; } path_chmax(i, w, -s[i] + s[i + 1]); path_chmin(i, w, -s[i] + s[i + 1] + K); path_chmax(i + 1, w, s[i] - s[i + 1] - K); path_chmin(i + 1, w, s[i] - s[i + 1]); } for (int i = LOG - 2; i >= 0; i--){ for (int j = 0; j <= N; j++){ mx[i][j] = min(mx[i][j], mx[i + 1][j]); mn[i][j] = max(mn[i][j], mn[i + 1][j]); if (pp[i][j] != -1){ mx[i][pp[i][j]] = min(mx[i][pp[i][j]], mx[i + 1][j]); mn[i][pp[i][j]] = max(mn[i][pp[i][j]], mn[i + 1][j]); } } } vector<bool> ok(N, true); for (int i = 0; i < N; i++){ add[R[i]] += add[i]; if (add[i] + add_all > 0){ ok[i] = false; } } for (int i = 0; i < N; i++){ if (mn[0][i] > mx[0][i]){ ok[i] = false; } } for (int i = 0; i < N; i++){ if (ok[i]){ cout << "Yes" << endl; } else { cout << "No" << endl; } } }