結果

問題 No.259 セグメントフィッシング+
ユーザー rpy3cpprpy3cpp
提出日時 2017-10-09 02:16:37
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 46 ms / 2,000 ms
コード長 2,064 bytes
コンパイル時間 1,552 ms
コンパイル使用メモリ 167,916 KB
実行使用メモリ 9,340 KB
最終ジャッジ日時 2023-08-10 20:26:09
合計ジャッジ時間 5,861 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 41 ms
9,152 KB
testcase_09 AC 41 ms
9,112 KB
testcase_10 AC 41 ms
9,184 KB
testcase_11 AC 40 ms
9,340 KB
testcase_12 AC 42 ms
9,180 KB
testcase_13 AC 46 ms
9,140 KB
testcase_14 AC 43 ms
9,104 KB
testcase_15 AC 43 ms
8,132 KB
testcase_16 AC 45 ms
9,116 KB
testcase_17 AC 46 ms
9,184 KB
testcase_18 AC 45 ms
9,160 KB
testcase_19 AC 44 ms
9,188 KB
testcase_20 AC 42 ms
9,168 KB
testcase_21 AC 45 ms
9,216 KB
testcase_22 AC 45 ms
9,224 KB
testcase_23 AC 24 ms
4,384 KB
testcase_24 AC 36 ms
9,184 KB
testcase_25 AC 36 ms
9,160 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template<typename T>
class SegTree{
    size_t n;
    vector<T> data;
    void modify(size_t i, T v){   // set value at index i to v.
        for (data[i += n] = v; i > 1; i >>= 1) data[i>>1] = data[i] + data[i^1];
    }
    T query(size_t L, size_t R){  // sum on interval [L, R)
        T ret = 0;
        for (L += n, R += n; L < R; L >>= 1, R >>= 1){
            if (L & 1) ret += data[L++];
            if (R & 1) ret += data[--R];
        }
        return ret;
    }
    int calc_pos0(int t, int pos){
        int pos0 = (pos - t) % (int) n;
        if (pos0 < 0) pos0 += (int) n;
        return pos0;
    }
    T query_core(int t, int L, int R){ // [L,R]
        int L0 = calc_pos0(t, L);
        int R0 = calc_pos0(t, R);
        if (L0 <= R0){
            return query(L0, R0 + 1);
        }else{
            return query(L0, n) + query(0, R0 + 1);
        }
    }
public:
    SegTree(size_t _n): n(_n), data(2 * _n, 0) {}
    SegTree(const vector<T> &src): n(src.size()), data(2 * n, 0) {init(src);}
    void init(const vector<T> &src){
        for (size_t i = 0; i != n; ++i) data[n + i] = src[i];
        for (size_t i = n - 1; i != 0; --i) data[i] = data[i*2] + data[i*2+1];
    }
    void addR(int t, int y, T z){
        int y0 = calc_pos0(t, y);
        modify(y0, data[n + y0] + z);
    }
    void addL(int t, int y, T z){
        int y0 = calc_pos0(t, (int)n - 1 - y);
        modify(y0, data[n + y0] + z);
    }
    T query(int t, int y1, int z1){
        return query_core(t, y1, z1 - 1) + query_core(t, n - z1, n - 1 - y1);
    }
};


int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N, Q;
    cin >> N >> Q;
    SegTree<long long> seg(2 * N);
    while (Q--){
        char x;
        int t, y, z;
        cin >> x >> t >> y >> z;
        if (x == 'L'){
            seg.addL(t, y, (long long)z);
        }else if (x == 'R'){
            seg.addR(t, y, (long long)z);
        }else if (x == 'C'){
            cout << seg.query(t, y, z) << '\n';
        }
    }
    return 0;
}
0