結果

問題 No.865 24時間降水量
ユーザー kibunakibuna
提出日時 2019-08-16 23:03:27
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 434 ms / 2,000 ms
コード長 4,323 bytes
コンパイル時間 1,945 ms
コンパイル使用メモリ 179,432 KB
実行使用メモリ 9,952 KB
最終ジャッジ日時 2023-10-24 03:57:03
合計ジャッジ時間 4,322 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 3 ms
4,348 KB
testcase_06 AC 3 ms
4,348 KB
testcase_07 AC 4 ms
4,348 KB
testcase_08 AC 4 ms
4,348 KB
testcase_09 AC 3 ms
4,348 KB
testcase_10 AC 13 ms
4,348 KB
testcase_11 AC 14 ms
4,348 KB
testcase_12 AC 13 ms
4,348 KB
testcase_13 AC 13 ms
4,348 KB
testcase_14 AC 13 ms
4,348 KB
testcase_15 AC 434 ms
9,952 KB
testcase_16 AC 432 ms
9,952 KB
testcase_17 AC 434 ms
9,952 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 1 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
const ll INF = 1LL << 60;
const ll MOD = 1000000007;
template <class T>
bool chmax(T &a, const T &b) {
    return (a < b) ? (a = b, 1) : 0;
}
template <class T>
bool chmin(T &a, const T &b) {
    return (b < a) ? (a = b, 1) : 0;
}
template <class C>
void print(const C &c, std::ostream &os = std::cout) {
    std::copy(std::begin(c), std::end(c), std::ostream_iterator<typename C::value_type>(os, " "));
    os << std::endl;
}

template <typename T = ll>
struct SegmentTreeLazy {
  private:
    using F = function<T(T, T)>;
    int n;
    vector<T> node, lazy;
    vector<bool> lazyFlag;
    F func;
    T UNIT;

  public:
    SegmentTreeLazy(int sz, F func, T UNIT) : func(func), UNIT(UNIT) {
        n = 1;
        while (n < sz)
            n *= 2;
        node.assign(2 * n - 1, UNIT);
        lazy.assign(2 * n - 1, UNIT);
        lazyFlag.resize(2 * n - 1, false);
    }
    SegmentTreeLazy(vector<T> v, F func, T UNIT) : func(func), UNIT(UNIT) {
        int sz = int(v.size());
        n = 1;
        while (n < sz)
            n *= 2;
        node.resize(2 * n - 1, UNIT);
        lazy.resize(2 * n - 1, UNIT);
        lazyFlag.resize(2 * n - 1, false);

        for (int i = 0; i < sz; i++)
            node[i + n - 1] = v[i];
        for (int i = n - 2; i >= 0; i--)
            node[i] = func(node[i * 2 + 1], node[i * 2 + 2]);
    }
    void lazyEvaluate(int k, int l, int r) {
        if (lazyFlag[k]) {
            node[k] += lazy[k];
            if (r - l > 1) {
                lazy[k * 2 + 1] += lazy[k];
                lazy[k * 2 + 2] += lazy[k];
                lazyFlag[k * 2 + 1] = lazyFlag[k * 2 + 2] = true;
            }
            lazyFlag[k] = false;
            lazy[k] = 0;
        }
    }
    // replace nodes with x in [a, b)
    void update(int a, int b, T x, int k = 0, int l = 0, int r = -1) {
        if (r < 0)
            r = n;
        lazyEvaluate(k, l, r);
        if (b <= l || r <= a)
            return;
        if (a <= l && r <= b) {
            lazy[k] = x;
            lazyFlag[k] = true;
            lazyEvaluate(k, l, r);
        } else {
            const int mid = (l + r) / 2;
            update(a, b, x, 2 * k + 1, l, mid);
            update(a, b, x, 2 * k + 2, mid, r);
            node[k] = func(node[2 * k + 1], node[2 * k + 2]);
        }
    }
    // add x to nodes in [a, b)
    void add(int a, int b, T x, int k = 0, int l = 0, int r = -1) {
        if (r < 0)
            r = n;
        lazyEvaluate(k, l, r);
        if (b <= l || r <= a) {
            return;
        }
        if (a <= l && r <= b) {
            lazy[k] += x; // +=
            lazyFlag[k] = true;
            lazyEvaluate(k, l, r);
            return;
        } else {
            const int mid = (l + r) / 2;
            add(a, b, x, k * 2 + 1, l, mid);
            add(a, b, x, k * 2 + 2, mid, r);
            node[k] = func(node[2 * k + 1], node[2 * k + 2]);
        }
    }
    // get func() in [a, b)
    T query(int a, int b, int k = 0, int l = 0, int r = -1) {
        if (r < 0)
            r = n;
        lazyEvaluate(k, l, r);
        if (b <= l || r <= a)
            return UNIT;
        if (a <= l && r <= b)
            return node[k];
        T vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
        T vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
        return func(vl, vr);
    }
};

int main() {
    int n;
    cin >> n;
    vi a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    int q;
    cin >> q;
    vi t(q), v(q);
    for (int i = 0; i < q; ++i) {
        cin >> t[i] >> v[i];
        t[i]--;
    }
    auto f = [](int x, int y) { return max(x, y); };
    SegmentTreeLazy<int> sg(n - 23, f, 0);
    int sum24 = 0;
    for (int i = 0; i < 24; ++i) {
        sum24 += a[i];
    }
    for (int i = 0; i < n - 23; ++i) {
        sg.update(i, i + 1, sum24);
        sum24 -= a[i];
        sum24 += a[i + 24];
    }
    for (int i = 0; i < q; ++i) {
        int add = v[i] - a[t[i]];
        a[t[i]] = v[i];
        sg.add(max(0, t[i] - 23), min(t[i] + 1, n - 23), add);
        cout << sg.query(0, n - 23) << "\n";
    }
    return 0;
}
0