結果

問題 No.1290 Addition and Subtraction Operation
ユーザー commy
提出日時 2022-09-18 22:13:10
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 110 ms / 2,000 ms
コード長 4,217 bytes
コンパイル時間 1,437 ms
コンパイル使用メモリ 104,980 KB
最終ジャッジ日時 2025-02-07 11:57:22
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 85
権限があれば一括ダウンロードができます

ソースコード

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