結果

問題 No.1000 Point Add and Array Add
ユーザー rodearodea
提出日時 2020-02-28 21:58:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 271 ms / 2,000 ms
コード長 3,156 bytes
コンパイル時間 1,822 ms
コンパイル使用メモリ 121,264 KB
実行使用メモリ 17,924 KB
最終ジャッジ日時 2024-04-21 18:27:31
合計ジャッジ時間 5,857 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 3 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 4 ms
5,376 KB
testcase_15 AC 4 ms
5,376 KB
testcase_16 AC 184 ms
16,240 KB
testcase_17 AC 154 ms
11,352 KB
testcase_18 AC 262 ms
17,920 KB
testcase_19 AC 265 ms
17,920 KB
testcase_20 AC 176 ms
17,920 KB
testcase_21 AC 268 ms
17,920 KB
testcase_22 AC 217 ms
17,924 KB
testcase_23 AC 271 ms
17,916 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("O3")
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;

using ll = long long;
using P = pair<int, int>;
using T = tuple<int, int, int>;

template <class T> inline T chmax(T &a, const T b) {return a = (a < b) ? b : a;}
template <class T> inline T chmin(T &a, const T b) {return a = (a > b) ? b : a;}

constexpr int MOD = 1e9 + 7;
constexpr int inf = 1e9;
constexpr long long INF = 1e18;
constexpr double pi = acos(-1);
constexpr double EPS = 1e-10;

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

// Range Add Query and Range Sum Query
class LazySegmentTree{
public:
    int n;
    vector<ll> node, lazy;

    LazySegmentTree(vector<ll> v){
        int sz = v.size();
        n = 1; while(n < sz) n *= 2;
        node.resize(2*n-1, 0);
        lazy.resize(2*n-1, 0);
        for(int i=0; i<sz; i++) node[i+n-1] = v[i];
        for(int i=n-2; i>=0; i--) node[i] = node[2*i+1] + node[2*i+2];
    }

    void eval(int k, int l, int r){
        if(lazy[k] != 0){
            node[k] += lazy[k];

            if(r - l > 1){
                lazy[2*k+1] += lazy[k] / 2;
                lazy[2*k+2] += lazy[k] / 2;
            }

            lazy[k] = 0;
        }
    }

    void add(int a, int b, ll x, int k=0, int l=0, int r=-1){
        if(r < 0) r = n;

        eval(k, l, r);

        if(b <= l || r <= a) return ;

        if(a <= l && r <= b){
            lazy[k] += (r - l) * x;
            eval(k, l, r);
        }
        else{
            add(a, b, x, 2*k+1, l, (l+r)/2);
            add(a, b, x, 2*k+2, (l+r)/2, r);
            node[k] = node[2*k+1] + node[2*k+2];
        }
    }

    ll getsum(int a, int b, int k=0, int l=0, int r=-1){
        if(r < 0) r = n;
        if(b <= l || r <= a) return 0;

        eval(k, l, r);
        if(a <= l && r <= b) return node[k];
        ll vl = getsum(a, b, 2*k+1, l, (l+r)/2);
        ll vr = getsum(a, b, 2*k+2, (l+r)/2, r);
        return vl + vr;
    }
};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    ll n, q; cin>>n>>q;
    vector<ll> a(n);
    for(int i=0; i<n; i++) cin>>a[i];
    vector<char> c(q);
    vector<ll> x(q), y(q);
    for(int i=0; i<q; i++){
        cin>>c[i]>>x[i]>>y[i];
        if(c[i] == 'B') y[i]--; 
        x[i]--;
    }

    LazySegmentTree seg(vector<ll>(n, 0));

    vector<ll> b(n, 0);
    for(int i=q-1; i>=0; i--){
        if(c[i] == 'B'){
            seg.add(x[i], y[i] + 1, 1);
        }
        else{
            ll cur = seg.getsum(x[i], x[i] + 1);
            b[x[i]] += cur * y[i];
        }
    }

    for(int i=0; i<n; i++){
        ll cur = seg.getsum(i, i + 1);
        b[i] += cur * a[i];
    }

    for(int i=0; i<n; i++){
        cout << b[i] << " \n"[i == n - 1];
    }

    return 0;
}
0