結果

問題 No.151 セグメントフィッシング
ユーザー kimiyukikimiyuki
提出日時 2016-09-08 21:36:55
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 65 ms / 5,000 ms
コード長 2,132 bytes
コンパイル時間 1,104 ms
コンパイル使用メモリ 103,600 KB
実行使用メモリ 5,836 KB
最終ジャッジ日時 2023-08-09 23:57:44
合計ジャッジ時間 3,086 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 11 ms
4,380 KB
testcase_09 AC 11 ms
4,376 KB
testcase_10 AC 10 ms
4,380 KB
testcase_11 AC 11 ms
4,380 KB
testcase_12 AC 43 ms
5,524 KB
testcase_13 AC 44 ms
5,480 KB
testcase_14 AC 43 ms
5,836 KB
testcase_15 AC 44 ms
5,524 KB
testcase_16 AC 44 ms
5,476 KB
testcase_17 AC 28 ms
5,476 KB
testcase_18 AC 29 ms
5,524 KB
testcase_19 AC 65 ms
5,528 KB
testcase_20 AC 65 ms
5,532 KB
testcase_21 AC 32 ms
5,604 KB
testcase_22 AC 33 ms
5,792 KB
testcase_23 AC 34 ms
4,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <array>
#include <set>
#include <map>
#include <queue>
#include <tuple>
#include <unordered_set>
#include <unordered_map>
#include <functional>
#include <cmath>
#include <cassert>
#define repeat(i,n) for (int i = 0; (i) < (n); ++(i))
typedef long long ll;
using namespace std;

template <typename T>
struct segment_tree { // on monoid
    int n;
    vector<T> a;
    function<T (T,T)> append; // associative
    T unit; // unit
    segment_tree() = default;
    template <typename F>
    segment_tree(int a_n, T a_unit, F a_append) {
        n = pow(2,ceil(log2(a_n)));
        a.resize(2*n-1, a_unit);
        unit = a_unit;
        append = a_append;
    }
    void point_update(int i, T z) {
        a[i+n-1] = z;
        for (i = (i+n)/2; i > 0; i /= 2) {
            a[i-1] = append(a[2*i-1], a[2*i]);
        }
    }
    T range_concat(int l, int r) {
        return range_concat(0, 0, n, l, r);
    }
    T range_concat(int i, int il, int ir, int l, int r) {
        if (l <= il and ir <= r) {
            return a[i];
        } else if (ir <= l or r <= il) {
            return unit;
        } else {
            return append(
                    range_concat(2*i+1, il, (il+ir)/2, l, r),
                    range_concat(2*i+2, (il+ir)/2, ir, l, r));
        }
    }
    T point_concat(int l) {
        return range_concat(l, l+1);
    }
};

int main() {
    int n, q; cin >> n >> q;
    segment_tree<ll> l(n + q, 0, plus<ll>());
    segment_tree<ll> r(n + q, 0, plus<ll>());
    repeat (t,q) {
        char c; int y, z; cin >> c >> y >> z;
        switch (c) {
            case 'L':
                l.point_update(y+t, l.point_concat(y+t) + z);
                break;
            case 'R':
                r.point_update((n-y-1)+t, r.point_concat((n-y-1)+t) + z);
                break;
            case 'C':
                cout << l.range_concat(y+t, z+t) + r.range_concat((n-z)+t, (n-y)+t) << endl;
                break;
        }
        r.point_update(t+n, l.point_concat(t));
        l.point_update(t+n, r.point_concat(t));
    }
    return 0;
}
0