結果

問題 No.1000 Point Add and Array Add
ユーザー noisy_noiminnoisy_noimin
提出日時 2020-02-28 21:59:16
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 175 ms / 2,000 ms
コード長 3,161 bytes
コンパイル時間 1,563 ms
コンパイル使用メモリ 127,076 KB
実行使用メモリ 21,456 KB
最終ジャッジ日時 2024-04-21 18:28:18
合計ジャッジ時間 5,031 ms
ジャッジサーバーID
(参考情報)
judge4 / 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 3 ms
5,376 KB
testcase_14 AC 3 ms
5,376 KB
testcase_15 AC 3 ms
5,376 KB
testcase_16 AC 123 ms
17,064 KB
testcase_17 AC 104 ms
14,660 KB
testcase_18 AC 175 ms
21,452 KB
testcase_19 AC 170 ms
21,456 KB
testcase_20 AC 165 ms
21,452 KB
testcase_21 AC 139 ms
21,328 KB
testcase_22 AC 172 ms
21,328 KB
testcase_23 AC 146 ms
21,332 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <string>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <tuple>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cassert>
#include <cstdint>
#include <cctype>
#include <numeric>
#include <bitset>
#include <functional>

using namespace std;

using ll =  long long;
using Pll = pair<ll, ll>;
using Pii = pair<int, int>;

constexpr int INF = 1 << 30;
constexpr ll LINF = 1LL << 60;
constexpr ll MOD = 1000000007;
constexpr long double EPS = 1e-10;
constexpr int dyx[4][2] = {
    { 0, 1}, {-1, 0}, {0,-1}, {1, 0}
};

template<class T> class SegmentTree {
    public:
    typedef function<T(T, T)> F;
    size_t n;
    vector<T> data;
    T def;  // 単位元
    T initial_value;    // 初期値
    F update_func;  // 更新用関数
    F find_func;  //クエリ用関数

    SegmentTree(size_t _n, T def, T initial_value, F update_func, F find_func)
        : def(def), initial_value(initial_value), update_func(update_func), find_func(find_func) {
        n = 1;
        while(n < _n) n <<= 1;
        data = vector<T>(2*n-1, initial_value);
    }

    void update(size_t i, T x) {
        i += n-1;
        data[i] = update_func(data[i], x);
        while(i) {
            i = (i-1) / 2;
            data[i] = find_func(data[2*i+1], data[2*i+2]);
        }
    }

    T find(size_t s, size_t t, size_t k, size_t kl, size_t kr) {
        if(kr <= s || t <= kl) return initial_value;
        if(s <= kl && kr <= t) return data[k];
        size_t kc = (kl+kr) / 2;
        T vl = find(s, t, 2*k+1, kl, kc);
        T vr = find(s, t, 2*k+2, kc, kr);
        return find_func(vl, vr);
    }

    T find(size_t s, size_t t) {
        return find(s, t, 0, 0, n);
    }
};

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int n, q;
    cin >> n >> q;
    vector<ll> a(n+1);
    for(int i=1;i<=n;++i) {
        cin >> a[i];
    }

    vector<vector<ll>> query(q+1, vector<ll>(3, 0));
    char c;
    vector<ll> b(n+1, 0);
    {
        vector<ll> qb_cnt(n+1, 0);
        for(int i=1;i<=q;++i) {
            cin >> c;
            cin >> query[i][1] >> query[i][2];
            if(c == 'B') {
                query[i][0] = 1;
                ++qb_cnt[query[i][1]];
                --qb_cnt[query[i][2]+1];
            } else {
            }
        }

        for(int i=1;i<=n;++i) qb_cnt[i] += qb_cnt[i-1];
        for(int i=1;i<=n;++i) {
            b[i] = qb_cnt[i] * a[i];
        }
    }

    {
        SegmentTree<ll> tree(
            n+3, 0LL, 0LL, 
            [](unsigned int a, unsigned int b) { return a+b; }, 
            [](unsigned int a, unsigned int b) { return a+b; }
        );

        for(int i=q;i>=1;--i) {
            if(query[i][0] == 0) {
                ll cnt = tree.find(0, query[i][1]+1);
                b[query[i][1]] += cnt * query[i][2];
            } else {
                tree.update(query[i][1], 1);
                tree.update(query[i][2]+1, -1);
            }
        }
    }

    cout << b[1];
    for(int i=2;i<=n;++i) cout << " " << b[i];
    cout << endl;
}
0