結果

問題 No.259 セグメントフィッシング+
ユーザー maine_honzuki
提出日時 2021-05-05 15:27:26
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 225 ms / 2,000 ms
コード長 1,377 bytes
コンパイル時間 5,354 ms
コンパイル使用メモリ 194,840 KB
最終ジャッジ日時 2025-01-21 07:29:08
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template <typename T>
class BinaryIndexedTree {
    vector<T> data;
    int n;
    //
public:
    BinaryIndexedTree(int n_) : n(n_ + 2), data(n_ + 2, 0) {}
    T sum(int i) {
        T ret(0);
        for (int x = i + 1; x > 0; x -= x & -x)
            ret += data[x];
        return ret;
    }
    void add(int i, T a) {
        for (int x = i + 1; x <= n; x += x & -x) {
            data[x] += a;
        }
    }
    T query(int a, int b) {
        return sum(b) - sum(a - 1);
    }
};

int main() {
    int N, Q;
    cin >> N >> Q;

    BinaryIndexedTree<ll> bit(N * 2 + 1);
    int origin = 0;

    auto rem = [&](int a) {
        return (((a += origin) %= 2 * N) += 2 * N) %= 2 * N;
    };

    auto get = [&](int a, int b) {
        if (a <= b) {
            return bit.query(a, b);
        } else {
            return bit.query(a, N * 2 - 1) + bit.query(0, b);
        }
    };

    while (Q--) {
        char c;
        int t, y, z;
        cin >> c >> t >> y >> z;

        (((origin = -t) %= 2 * N) += 2 * N) %= 2 * N;

        if (c == 'L') {
            bit.add(rem(2 * N - 1 - y), z);
        } else if (c == 'R') {
            bit.add(rem(y), z);
        } else {
            z--;
            cout << get(rem(y), rem(z)) + get(rem(2 * N - 1 - z), rem(2 * N - 1 - y)) << endl;
        }
    }
}
0