結果

問題 No.1000 Point Add and Array Add
ユーザー Hiroki OhnoHiroki Ohno
提出日時 2020-04-18 04:47:16
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 514 ms / 2,000 ms
コード長 3,661 bytes
コンパイル時間 2,179 ms
コンパイル使用メモリ 182,536 KB
実行使用メモリ 14,616 KB
最終ジャッジ日時 2024-04-14 20:05:32
合計ジャッジ時間 8,677 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 1 ms
6,944 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 4 ms
6,940 KB
testcase_13 AC 3 ms
6,940 KB
testcase_14 AC 5 ms
6,944 KB
testcase_15 AC 4 ms
6,940 KB
testcase_16 AC 347 ms
14,152 KB
testcase_17 AC 318 ms
8,960 KB
testcase_18 AC 514 ms
14,552 KB
testcase_19 AC 510 ms
14,552 KB
testcase_20 AC 438 ms
14,616 KB
testcase_21 AC 444 ms
14,604 KB
testcase_22 AC 512 ms
14,600 KB
testcase_23 AC 457 ms
14,616 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
constexpr int MOD = 1000000007;
constexpr int INF = numeric_limits<int>::max() / 2;
typedef pair<int,int> P;
using Graph = vector<vector<int>>;

template<typename Monoid, typename OperatorMonoid = Monoid>
struct LazySegmentTree{
    using F = function<Monoid(Monoid, Monoid)>;
    using G = function<Monoid(Monoid, OperatorMonoid)>;
    using H = function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>;
    using C = function<OperatorMonoid(OperatorMonoid, int)>;

    int sz;
    vector<Monoid> data;
    vector<OperatorMonoid> lazy;
    const F f;
    const G g;
    const H h;
    const Monoid M1;            // モノイドの単位元
    const OperatorMonoid OM0;   // 作用素モノイドの単位元
    const C c;

    LazySegmentTree(int n, const F f, const G g, const H h, const Monoid &M1,
        const OperatorMonoid OM0, const C c = [](int a, int b){return a;})
        : f(f), g(g), h(h), M1(M1), OM0(OM0), c(c)
    {
        sz = 1;
        while(sz < n)   sz <<= 1;
        data.assign(sz << 1, M1);
        lazy.assign(sz << 1, OM0);
    }

    void set(int k, const Monoid &x){
        data[k + sz] = x;
    }

    void build(){
        for(int k = sz - 1; k > 0; --k){
            data[k] = f(data[k << 1], data[k << 1 | 1]);
        }
    }

    void propagate(int k, int len){
        if(lazy[k] != OM0){
            if(k < sz){
                lazy[k << 1] = h(lazy[k << 1], lazy[k]);
                lazy[k << 1 | 1] = h(lazy[k << 1 | 1], lazy[k]);
            }
            data[k] = g(data[k], c(lazy[k], len));
            lazy[k] = OM0;
        }
    }

    // a,b : 実際の配列上の index
    // k : セグ木上の index
    // l,r : data[k] が実際の配列でのどの区間の両端か (半開区間 [l,r) )
    Monoid update(int a, int b, const OperatorMonoid &x, int k=1, int l=0, int r=-1){
        if(r == -1)     r = sz;
        propagate(k, r - l);
        if(r <= a || b <= l)    return data[k];
        else if(a <= l && r <= b){
            lazy[k] = h(lazy[k], x);
            propagate(k, r - l);
            return data[k];
        }
        else{
            return data[k] = f(update(a, b, x, k << 1, l, (l + r) >> 1),
                                update(a, b, x, k << 1 | 1, (l + r) >> 1, r));
        }
    }

    Monoid query(int a, int b, int k=1, int l=0, int r=-1){
        if(r == -1)     r = sz;
        propagate(k, r - l);
        if(r <= a || b <= l)    return M1;
        else if(a <= l && r <= b)   return data[k];
        else{
            return f(query(a, b, k << 1, l, (l + r) >> 1),
                        query(a, b, k << 1 | 1, (l + r) >> 1, r));
        }
    }

    Monoid operator[](const int &k){
        return query(k, k + 1);
    }

    void print(){
        for(int i = 0; i < data.size(); ++i){
            cerr << data[i] << " ";
        }
        cerr << "\n";
    }
};


signed main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	int N, Q;
	cin >> N >> Q;
	vector<int> A(N);
	rep(i,N) cin >> A[i];
	LazySegmentTree<int> cnt(N, [](int a, int b){return a + b;},
							[](int a, int b){return a + b;},
							[](int a, int b){return a + b;},
							0, 0,
							[](int a, int b){return a * b;});
	vector<int> res(N, 0);

	rep(i,Q){
		char c;
		int x, y;
		cin >> c >> x >> y;
		--x;
		if(c == 'A'){
			res[x] += A[x] * cnt[x];
			cnt.update(x, x+1, -cnt[x]);
			A[x] += y;
		}
		else{
			cnt.update(x, y, 1);
		}
	}
	rep(i,N){
		res[i] += A[i] * cnt[i];
	}
	rep(i,N){
		if(i == N-1) cout << res[i] << endl;
		else cout << res[i] << " ";
	}
}
0