結果

問題 No.259 セグメントフィッシング+
ユーザー kimiyukikimiyuki
提出日時 2016-09-08 22:17:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 177 ms / 2,000 ms
コード長 2,118 bytes
コンパイル時間 983 ms
コンパイル使用メモリ 85,472 KB
実行使用メモリ 11,708 KB
最終ジャッジ日時 2024-04-27 18:04:24
合計ジャッジ時間 6,146 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,948 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 155 ms
11,664 KB
testcase_09 AC 158 ms
11,636 KB
testcase_10 AC 159 ms
11,660 KB
testcase_11 AC 159 ms
11,628 KB
testcase_12 AC 162 ms
11,708 KB
testcase_13 AC 171 ms
11,664 KB
testcase_14 AC 177 ms
11,696 KB
testcase_15 AC 177 ms
11,628 KB
testcase_16 AC 171 ms
11,624 KB
testcase_17 AC 168 ms
11,648 KB
testcase_18 AC 165 ms
11,628 KB
testcase_19 AC 172 ms
11,636 KB
testcase_20 AC 165 ms
11,652 KB
testcase_21 AC 166 ms
11,688 KB
testcase_22 AC 165 ms
11,636 KB
testcase_23 AC 95 ms
6,944 KB
testcase_24 AC 116 ms
11,636 KB
testcase_25 AC 124 ms
11,660 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <functional>
#include <cmath>
#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> a(2*n, 0, plus<ll>());
    auto append = [&](int i, int z) {
        a.point_update(i, a.point_concat(i) + z);
    };
    auto concat = [&](int l, int r) {
        return l < r
            ? a.range_concat(l, r)
            : a.range_concat(0, r) + a.range_concat(l, 2*n);
    };
    while (q --) {
        char c; int t, y, z; cin >> c >> t >> y >> z;
        auto r = [&](int i) { return ((    i  -t) % (2*n) + 2*n) % (2*n); };
        auto l = [&](int i) { return ((2*n-i-1-t) % (2*n) + 2*n) % (2*n); };
        switch (c) {
            case 'L':
                append(l(y), z); break;
            case 'R':
                append(r(y), z); break;
            case 'C':
                cout << concat(r(y), r(z)) + concat(l(z-1), l(y-1)) << endl;
                break;
        }
    }
    return 0;
}
0