結果

問題 No.1000 Point Add and Array Add
ユーザー tonegawatonegawa
提出日時 2020-08-11 09:56:09
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 469 ms / 2,000 ms
コード長 2,686 bytes
コンパイル時間 1,316 ms
コンパイル使用メモリ 106,972 KB
実行使用メモリ 25,636 KB
最終ジャッジ日時 2024-04-17 17:06:38
合計ジャッジ時間 7,498 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 1 ms
6,944 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 5 ms
6,944 KB
testcase_13 AC 3 ms
6,940 KB
testcase_14 AC 5 ms
6,940 KB
testcase_15 AC 5 ms
6,940 KB
testcase_16 AC 295 ms
21,216 KB
testcase_17 AC 257 ms
16,748 KB
testcase_18 AC 411 ms
25,636 KB
testcase_19 AC 416 ms
25,468 KB
testcase_20 AC 322 ms
25,576 KB
testcase_21 AC 446 ms
25,616 KB
testcase_22 AC 378 ms
25,580 KB
testcase_23 AC 469 ms
25,564 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <algorithm>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#define vll vector<ll>
#define vvvl vector<vvl>
#define vvl vector<vector<ll>>
#define VV(a, b, c, d) vector<vector<d>>(a, vector<d>(b, c))
#define VVV(a, b, c, d) vector<vvl>(a, vvl(b, vll (c, d)));
#define re(c, b) for(ll c=0;c<b;c++)
#define all(obj) (obj).begin(), (obj).end()
typedef long long int ll;
typedef long double ld;
using namespace std;

struct LazySegmentTree {
private:
    int n;
    vector<ll> node, lazy;

public:
    LazySegmentTree(vector<ll> v) {
        int sz = (int)v.size();
        n = 1; while(n < sz) n *= 2;
        node.resize(2*n-1);
        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[i*2+1] + node[i*2+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;
        eval(k, l, r);
        if(b <= l || r <= a) return 0;
        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(int argc, char const *argv[]) {
  ll n, q;std::cin >> n >> q;
  LazySegmentTree seg(vll(n+1, 0));
  vvl d = VV(q, 2, 0, ll);
  vll A(n+1, 0);
  for(int i=1;i<=n;i++) std::cin >> A[i];
  for(int i=0;i<q;i++){
    char c;
    ll a, b;
    std::cin >> c >> a >> b;
    if(c=='A') d[i] = vll{a, b};
    else d[i] = vll{-a,-b};
  }
  vll B(n+1, 0);
  for(int i=q-1;i>=0;i--){
    if(d[i][0]>0){
      ll a = d[i][0], b = d[i][1];
      ll Q = seg.getsum(a, a+1);
      B[a] += b*Q;
    }else{
      ll a = -d[i][0], b = -d[i][1];
      seg.add(a, b+1, 1);
    }
  }
  for(int i=1;i<=n;i++){
    ll q = seg.getsum(i, i+1);
    B[i] += A[i] * q;
  }
  for(int i=1;i<=n;i++) std::cout << B[i] << (i==n?"\n":" ");
  return 0;
}
0