結果

問題 No.1290 Addition and Subtraction Operation
ユーザー outlineoutline
提出日時 2021-01-07 15:35:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 209 ms / 2,000 ms
コード長 4,966 bytes
コンパイル時間 1,824 ms
コンパイル使用メモリ 153,860 KB
実行使用メモリ 19,796 KB
最終ジャッジ日時 2024-04-26 04:05:31
合計ジャッジ時間 8,465 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 1 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 1 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 1 ms
5,376 KB
testcase_29 AC 1 ms
5,376 KB
testcase_30 AC 1 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
testcase_36 AC 2 ms
5,376 KB
testcase_37 AC 2 ms
5,376 KB
testcase_38 AC 1 ms
5,376 KB
testcase_39 AC 2 ms
5,376 KB
testcase_40 AC 2 ms
5,376 KB
testcase_41 AC 1 ms
5,376 KB
testcase_42 AC 38 ms
7,936 KB
testcase_43 AC 38 ms
7,936 KB
testcase_44 AC 38 ms
7,936 KB
testcase_45 AC 39 ms
7,936 KB
testcase_46 AC 38 ms
7,936 KB
testcase_47 AC 39 ms
7,936 KB
testcase_48 AC 37 ms
7,808 KB
testcase_49 AC 38 ms
7,936 KB
testcase_50 AC 38 ms
7,808 KB
testcase_51 AC 38 ms
7,936 KB
testcase_52 AC 39 ms
8,064 KB
testcase_53 AC 38 ms
7,936 KB
testcase_54 AC 39 ms
7,808 KB
testcase_55 AC 38 ms
7,936 KB
testcase_56 AC 38 ms
7,936 KB
testcase_57 AC 65 ms
16,296 KB
testcase_58 AC 94 ms
17,152 KB
testcase_59 AC 64 ms
16,256 KB
testcase_60 AC 174 ms
19,704 KB
testcase_61 AC 141 ms
18,412 KB
testcase_62 AC 154 ms
18,816 KB
testcase_63 AC 91 ms
17,012 KB
testcase_64 AC 23 ms
15,320 KB
testcase_65 AC 43 ms
15,888 KB
testcase_66 AC 120 ms
17,780 KB
testcase_67 AC 177 ms
19,796 KB
testcase_68 AC 146 ms
18,520 KB
testcase_69 AC 91 ms
16,964 KB
testcase_70 AC 83 ms
17,012 KB
testcase_71 AC 113 ms
17,392 KB
testcase_72 AC 45 ms
15,872 KB
testcase_73 AC 128 ms
18,048 KB
testcase_74 AC 18 ms
15,232 KB
testcase_75 AC 34 ms
15,672 KB
testcase_76 AC 85 ms
16,936 KB
testcase_77 AC 178 ms
18,664 KB
testcase_78 AC 162 ms
18,168 KB
testcase_79 AC 107 ms
16,732 KB
testcase_80 AC 209 ms
19,584 KB
testcase_81 AC 199 ms
19,392 KB
testcase_82 AC 115 ms
17,448 KB
testcase_83 AC 178 ms
19,456 KB
testcase_84 AC 163 ms
18,944 KB
testcase_85 AC 59 ms
16,148 KB
testcase_86 AC 28 ms
15,488 KB
権限があれば一括ダウンロードができます

ソースコード

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;
}

template<typename Monoid, typename OperatorMonoid = Monoid>
struct LazySegmentTree{
    using F = function<Monoid(Monoid, Monoid)>;
    using G = function<Monoid(Monoid, OperatorMonoid)>;
    using H = function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>;

    int sz;
    vector<Monoid> data;
    vector<OperatorMonoid> lazy;
    const F f;
    const G g;
    const H h;
    const Monoid M1;            // モノイドの単位元
    const OperatorMonoid OM0;   // 作用素モノイドの単位元

    LazySegmentTree(int n, const F f, const G g, const H h, const Monoid &M1, const OperatorMonoid OM0)
        : f(f), g(g), h(h), M1(M1), OM0(OM0)
    {
        sz = 1;
        while(sz < n)   sz <<= 1;
        data.assign(sz << 1, M1);
        lazy.assign(sz << 1, OM0);
    }

    void set(int k, const Monoid &x){
        data[k + sz] = x;
    }

    void build(){
        for(int k = sz - 1; k > 0; --k){
            data[k] = f(data[k << 1], data[k << 1 | 1]);
        }
    }

    void propagate(int k){
        if(lazy[k] != OM0){
            if(k < sz){
                lazy[k << 1] = h(lazy[k << 1], lazy[k]);
                lazy[k << 1 | 1] = h(lazy[k << 1 | 1], lazy[k]);
            }
            data[k] = g(data[k], lazy[k]);
            lazy[k] = OM0;
        }
    }

    Monoid update(int a, int b, const OperatorMonoid &x, int k = 1, int l = 0, int r = -1){
        if(r == -1)     r = sz;
        propagate(k);
        if(r <= a || b <= l)    return data[k];
        else if(a <= l && r <= b){
            lazy[k] = h(lazy[k], x);
            propagate(k);
            return data[k];
        }
        else{
            return data[k] = f(update(a, b, x, k << 1, l, (l + r) >> 1),
                                update(a, b, x, k << 1 | 1, (l + r) >> 1, r));
        }
    }

    Monoid query(int a, int b, int k = 1, int l = 0, int r = -1){
        if(r == -1)     r = sz;
        propagate(k);
        if(r <= a || b <= l)    return M1;
        else if(a <= l && r <= b)   return data[k];
        else{
            return f(query(a, b, k << 1, l, (l + r) >> 1),
                        query(a, b, k << 1 | 1, (l + r) >> 1, r));
        }
    }

    Monoid operator[](const int &k){
        return query(k, k + 1);
    }
};

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);
        }
    }

    struct Node {
        ll sum;
        int weight;
        Node(ll s = 0, int w = 0) : sum(s), weight(w) {}
    };
    auto op = [](Node a, Node b) {
        return Node(a.sum + b.sum, a.weight + b.weight);
    };
    auto mapping = [](Node a, ll x) {
        return Node(a.sum + x * a.weight, a.weight);
    };
    auto composition = [](ll a, ll b) {
        return a + b;
    };
    LazySegmentTree<Node, ll> seg(N, op, mapping, composition, Node(), 0);
    for(int i = 0; i < N; ++i){
        seg.set(i, Node(B[i], (i % 2 ? -1 : 1)));
    }
    seg.build();

    for(int l = 0; l < N; ++l){
        if(rights[l] == -1) continue;
        if(l % 2 == 0){
            seg.update(l, rights[l], -seg[l].sum);
        }
        else{
            seg.update(l, rights[l], seg[l].sum);
        }
    }

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

    return 0;
}
0