結果

問題 No.1290 Addition and Subtraction Operation
ユーザー commycommy
提出日時 2022-09-18 22:13:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 91 ms / 2,000 ms
コード長 4,217 bytes
コンパイル時間 1,191 ms
コンパイル使用メモリ 104,452 KB
実行使用メモリ 12,480 KB
最終ジャッジ日時 2023-08-23 18:21:08
合計ジャッジ時間 8,599 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,384 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 1 ms
4,376 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 2 ms
4,376 KB
testcase_39 AC 2 ms
4,376 KB
testcase_40 AC 2 ms
4,380 KB
testcase_41 AC 1 ms
4,380 KB
testcase_42 AC 19 ms
4,376 KB
testcase_43 AC 19 ms
4,380 KB
testcase_44 AC 19 ms
4,380 KB
testcase_45 AC 19 ms
4,376 KB
testcase_46 AC 18 ms
4,376 KB
testcase_47 AC 20 ms
4,376 KB
testcase_48 AC 19 ms
4,376 KB
testcase_49 AC 19 ms
4,376 KB
testcase_50 AC 19 ms
4,376 KB
testcase_51 AC 19 ms
4,376 KB
testcase_52 AC 19 ms
4,380 KB
testcase_53 AC 19 ms
4,380 KB
testcase_54 AC 19 ms
4,376 KB
testcase_55 AC 19 ms
4,376 KB
testcase_56 AC 19 ms
4,384 KB
testcase_57 AC 31 ms
9,192 KB
testcase_58 AC 41 ms
9,524 KB
testcase_59 AC 27 ms
8,864 KB
testcase_60 AC 81 ms
12,308 KB
testcase_61 AC 67 ms
10,580 KB
testcase_62 AC 76 ms
11,252 KB
testcase_63 AC 43 ms
9,392 KB
testcase_64 AC 14 ms
8,268 KB
testcase_65 AC 22 ms
8,532 KB
testcase_66 AC 55 ms
9,868 KB
testcase_67 AC 86 ms
12,480 KB
testcase_68 AC 67 ms
11,032 KB
testcase_69 AC 41 ms
9,236 KB
testcase_70 AC 38 ms
9,264 KB
testcase_71 AC 48 ms
9,980 KB
testcase_72 AC 21 ms
8,600 KB
testcase_73 AC 54 ms
10,044 KB
testcase_74 AC 12 ms
8,264 KB
testcase_75 AC 18 ms
8,444 KB
testcase_76 AC 37 ms
9,380 KB
testcase_77 AC 77 ms
10,932 KB
testcase_78 AC 69 ms
10,380 KB
testcase_79 AC 44 ms
9,080 KB
testcase_80 AC 91 ms
12,076 KB
testcase_81 AC 88 ms
11,964 KB
testcase_82 AC 53 ms
9,908 KB
testcase_83 AC 82 ms
11,880 KB
testcase_84 AC 78 ms
11,424 KB
testcase_85 AC 29 ms
8,844 KB
testcase_86 AC 18 ms
8,408 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#include <limits>

#define rep(i, a, b) for (int i = int(a); i < int(b); i++)
using namespace std;
using ll = long long int;  // NOLINT
using P = pair<ll, ll>;

// clang-format off
#ifdef _DEBUG_
#define dump(...) do{ cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; debug_print(__VA_ARGS__); } while(false)
template<typename T, typename... Ts> void debug_print(const T &t, const Ts &...ts) { cerr << t; ((cerr << ", " << ts), ...); cerr << endl; }
#else
#define dump(...) do{ } while(false)
#endif
template<typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); }
template<typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); }
template<typename T> bool chmin(T &a, const T& b) { if (a > b) {a = b; return true; } return false; }
template<typename T> bool chmax(T &a, const T& b) { if (a < b) {a = b; return true; } return false; }
template<typename T, typename... Ts> void print(const T& t, const Ts&... ts) { cout << t; ((cout << ' ' << ts), ...); cout << '\n'; }
template<typename... Ts> void input(Ts&... ts) { (cin >> ... >> ts); }
template<typename T> istream &operator,(istream &in, T &t) { return in >> t; }
struct Inf { template<typename T> constexpr operator T() { return numeric_limits<T>::max() / 2; } };
// clang-format on

#include <functional>
template<typename T>
class DualSegmentTree {
    using Func = function<T(T, T)>;

public:
    vector<T> data;
    int n;
    T init;
    Func update_func;
    DualSegmentTree(int _n, T _init, Func up) {
        init = _init;
        update_func = up;
        for (n = 1; n < _n; n *= 2)
            ;
        data.resize(2 * n - 1, init);
    }
    void update(int l, int r, T val) {
        for (l += n - 1, r += n - 1; l < r; l = l / 2, r = (r - 1) / 2) {
            if (!(l & 1)) {
                data[l] = update_func(data[l], val);
            }
            if (!(r & 1)) {
                data[r - 1] = update_func(data[r - 1], val);
            }
        }
    }
    T query(int pos) {
        pos += n - 1;
        T res = data[pos];
        while (pos > 0) {
            pos = (pos - 1) / 2;
            res = update_func(res, data[pos]);
        }
        return res;
    }
};

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n, m;
    cin, n, m;
    vector<ll> b(n);
    rep(i, 0, n) {
        cin, b[i];
    }

    if (n == 1) {
        if (b.front() == 0 || m) {
            print("YES");
        } else {
            print("NO");
        }
        return 0;
    }

    vector<vector<int>> query(n);
    rep(i, 0, m) {
        int l, r;
        cin, l, r;
        query[l - 1].emplace_back(r - 1);
    }

    auto ADD = [](ll a, ll b) { return a + b; };
    DualSegmentTree<ll> odd((n + 1) / 2, 0, ADD), even(n / 2, 0, ADD);

    rep(i, 0, n) {
        auto &v = query[i];
        sort(v.begin(), v.end());
        v.erase(unique(v.begin(), v.end()), v.end());

        rep(j, 1, v.size()) {
            query[v[j - 1] + 1].emplace_back(v[j]);
        }

        if (v.size()) {
            auto &seg = (i % 2 ? even : odd);
            ll val = seg.query(i / 2);
            ll d = b[i] - val;
            int r = v.front();
            if (!(i % 2) && !(r % 2)) {
                odd.update(i / 2, (r + 2) / 2, d);
                even.update((i + 1) / 2, (r + 1) / 2, -d);
            } else if (!(i % 2) && (r % 2)) {
                odd.update(i / 2, (r + 1) / 2, d);
                even.update((i + 1) / 2, (r + 1) / 2, -d);
            } else if ((i % 2) && !(r % 2)) {
                even.update(i / 2, (r + 1) / 2, d);
                odd.update((i + 1) / 2, (r + 2) / 2, -d);
            } else {
                even.update(i / 2, (r + 1) / 2, d);
                odd.update((i + 1) / 2, (r + 1) / 2, -d);
            }
            dump(i, d);
        }
    }
    rep(i, 0, n) {
        auto &seg = (i % 2 ? even : odd);
        dump(seg.query(i / 2), b[i]);
        if (seg.query(i / 2) != b[i]) {
            print("NO");
            return 0;
        }
    }
    print("YES");
    return 0;
}
0