結果

問題 No.2643 Many Range Sums Problems
ユーザー Rubikun
提出日時 2024-02-19 22:13:20
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 256 ms / 8,000 ms
コード長 2,012 bytes
コンパイル時間 1,993 ms
コンパイル使用メモリ 197,836 KB
最終ジャッジ日時 2025-02-19 17:06:14
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
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 (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=998244353,MAX=300005,INF=1<<30;

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    ll N,K;cin>>N>>K;
    vector<ll> R(N),X(N);
    for(int i=0;i<N;i++) cin>>R[i]>>X[i];
    
    for(ll ng=0;ng<N;ng++){
        bool ok=true;
        vector<ll> S(N+1);
        for(ll i=N-1;i>ng;i--){
            S[i]=S[R[i]]+X[i];
            ll diff=S[i]-S[i+1];
            ok&=(0<=diff&&diff<=K);
        }
        if(!ok){
            cout<<"No\n";
            continue;
        }
        
        vector<ll> A=S,B=S,C=S;
        A[ng]=A[ng+1]+0;
        B[ng]=B[ng+1]+1;
        C[ng]=C[ng+1]+2;
        
        ll left=0,right=K;
        
        for(ll i=ng-1;i>=0;i--){
            A[i]=A[R[i]]+X[i];
            B[i]=B[R[i]]+X[i];
            C[i]=C[R[i]]+X[i];
            
            ll a=A[i]-A[i+1],b=B[i]-B[i+1],c=C[i]-C[i+1];
            if(a==b&&b==c){
                if(0<=a&&a<=K){
                    
                }else{
                    left=0;
                    right=-1;
                    break;
                }
            }else{
                assert(abs(a-b)==1);
                
                if(a<b){
                    ll L=-a,R=K-a;
                    chmax(left,L);
                    chmin(right,R);
                }else{
                    ll L=a-K,R=a;
                    chmax(left,L);
                    chmin(right,R);
                }
            }
        }
        
        if(left<=right){
            cout<<"Yes\n";
        }else{
            cout<<"No\n";
        }
    }
}

0