結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 1 ms
6,676 KB
testcase_03 AC 2,837 ms
6,676 KB
testcase_04 AC 2,350 ms
6,676 KB
testcase_05 AC 2,235 ms
6,676 KB
testcase_06 AC 3,289 ms
6,676 KB
testcase_07 AC 1,786 ms
6,676 KB
testcase_08 AC 2,962 ms
6,676 KB
testcase_09 AC 3,377 ms
6,676 KB
testcase_10 AC 1,648 ms
6,676 KB
testcase_11 AC 1,598 ms
6,676 KB
testcase_12 AC 2,421 ms
6,676 KB
testcase_13 AC 3,312 ms
6,676 KB
testcase_14 AC 2,952 ms
6,676 KB
testcase_15 AC 3,079 ms
6,676 KB
testcase_16 AC 2,528 ms
6,676 KB
testcase_17 AC 2,739 ms
6,676 KB
testcase_18 AC 1,328 ms
6,676 KB
testcase_19 AC 155 ms
6,676 KB
testcase_20 AC 694 ms
6,676 KB
testcase_21 AC 117 ms
6,676 KB
testcase_22 AC 505 ms
6,676 KB
testcase_23 AC 896 ms
6,676 KB
testcase_24 AC 1,748 ms
6,676 KB
testcase_25 AC 1,571 ms
6,676 KB
testcase_26 AC 140 ms
6,676 KB
testcase_27 AC 153 ms
6,676 KB
testcase_28 AC 1 ms
6,676 KB
testcase_29 AC 1 ms
6,676 KB
testcase_30 AC 2,180 ms
6,676 KB
testcase_31 AC 3,264 ms
6,676 KB
testcase_32 AC 2,275 ms
6,676 KB
testcase_33 AC 2,224 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
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];
  }
  for (int i = 0; i < N; i++){
    vector<vector<pair<long long, int>>> E(N + 1);
    for (int j = 0; j < N; j++){
      if (j != i){
        E[j].push_back(make_pair(X[j], R[j]));
        E[R[j]].push_back(make_pair(-X[j], j));
      }
    }
    int cnt = 0;
    vector<int> c(N + 1, -1);
    vector<long long> d(N + 1, 0);
    for (int j = 0; j <= N; j++){
      if (c[j] == -1){
        c[j] = cnt;
        queue<int> Q;
        Q.push(j);
        while (!Q.empty()){
          int v = Q.front();
          Q.pop();
          for (pair<long long, int> P : E[v]){
            int w = P.second;
            if (c[w] == -1){
              c[w] = cnt;
              d[w] = d[v] + P.first;
              Q.push(w);
            }
          }
        }
        cnt++;
      }
    }
    bool ok = true;
    long long mn = -INF, mx = INF;
    for (int j = 0; j < N; j++){
      if (c[j] == c[j + 1]){
        if (d[j + 1] - d[j] < 0 || d[j + 1] - d[j] > K){
          ok = false;
        }
      }
      if (c[j] == 0 && c[j + 1] == 1){
        mx = min(mx, K - d[j + 1] + d[j]);
        mn = max(mn, -d[j + 1] + d[j]);
      }
      if (c[j] == 1 && c[j + 1] == 0){
        mx = min(mx, d[j + 1] - d[j]);
        mn = max(mn, -K + d[j + 1] - d[j]);
      }
    }
    if (mn > mx){
      ok = false;
    }
    if (ok){
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
}
0