結果

問題 No.1290 Addition and Subtraction Operation
ユーザー outline
提出日時 2021-01-07 15:23:37
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 76 ms / 2,000 ms
コード長 2,257 bytes
コンパイル時間 1,369 ms
コンパイル使用メモリ 139,760 KB
最終ジャッジ日時 2025-01-17 10:12:29
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 85
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
using namespace std;

using ll = long long;
using P = pair<int, int>;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, M;
    cin >> N >> M;
    vector<ll> B(N);
    for(int i = 0; i < N; ++i)  cin >> B[i];
    vector<set<int>> sections(N + 1);
    for(int i = 0; i < M; ++i){
        int L, R;
        cin >> L >> R;
        sections[L - 1].emplace(R);
    }
    
    vector<int> rights(N, -1);
    for(int l = 0; l < N; ++l){
        if(sections[l].size() == 0) continue;

        int r = *sections[l].begin();
        rights[l] = r;
        sections[l].erase(sections[l].begin());

        if(sections[l].size() > sections[r].size()){
            swap(sections[l], sections[r]);
        }
        for(auto it = sections[l].begin(); it != sections[l].end(); ){
            sections[r].emplace(*it);
            it = sections[l].erase(it);
        }
    }

    vector<ll> finish(N + 1);
    ll sum = 0;
    for(int l = 0; l < N; ++l){
        sum -= finish[l];
        B[l] -= sum;
        if(rights[l] != -1){
            sum += B[l];
            if((rights[l] - l) % 2 == 0)    finish[rights[l]] += B[l];
            else    finish[rights[l]] -= B[l];
            B[l] = 0;
        }
        sum *= -1;
    }

    for(int i = 0; i < N; ++i){
        if(B[i] != 0){
            cout << "NO\n";
            return 0;
        }
    }
    cout << "YES\n";

    return 0;
}
0