結果

問題 No.2643 Many Range Sums Problems
ユーザー SSRSSSRS
提出日時 2024-02-19 23:36:39
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 59 ms / 8,000 ms
コード長 2,708 bytes
コンパイル時間 3,031 ms
コンパイル使用メモリ 215,284 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-02-19 23:36:44
合計ジャッジ時間 4,218 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 12 ms
6,676 KB
testcase_04 AC 12 ms
6,676 KB
testcase_05 AC 11 ms
6,676 KB
testcase_06 AC 13 ms
6,676 KB
testcase_07 AC 13 ms
6,676 KB
testcase_08 AC 12 ms
6,676 KB
testcase_09 AC 12 ms
6,676 KB
testcase_10 AC 11 ms
6,676 KB
testcase_11 AC 9 ms
6,676 KB
testcase_12 AC 58 ms
6,676 KB
testcase_13 AC 12 ms
6,676 KB
testcase_14 AC 13 ms
6,676 KB
testcase_15 AC 12 ms
6,676 KB
testcase_16 AC 12 ms
6,676 KB
testcase_17 AC 11 ms
6,676 KB
testcase_18 AC 9 ms
6,676 KB
testcase_19 AC 4 ms
6,676 KB
testcase_20 AC 6 ms
6,676 KB
testcase_21 AC 4 ms
6,676 KB
testcase_22 AC 12 ms
6,676 KB
testcase_23 AC 21 ms
6,676 KB
testcase_24 AC 30 ms
6,676 KB
testcase_25 AC 42 ms
6,676 KB
testcase_26 AC 6 ms
6,676 KB
testcase_27 AC 7 ms
6,676 KB
testcase_28 AC 2 ms
6,676 KB
testcase_29 AC 1 ms
6,676 KB
testcase_30 AC 12 ms
6,676 KB
testcase_31 AC 13 ms
6,676 KB
testcase_32 AC 59 ms
6,676 KB
testcase_33 AC 57 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
    }
    while (u != v){
      mn[0][u] = max(mn[0][u], x);
      u = R[u];
    }
  };
  auto path_chmin = [&](int u, int v, long long x){
    if (u == v){
      return;
    }
    while (u != v){
      mx[0][u] = min(mx[0][u], x);
      u = R[u];
    }
  };
  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][j] = min(mx[i][j], mx[i + 1][pp[i][j]]);
        mn[i][j] = max(mn[i][j], mn[i + 1][pp[i][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;
    }
  }
}
0