結果

問題 No.151 セグメントフィッシング
ユーザー mamekinmamekin
提出日時 2015-02-14 00:08:36
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 40 ms / 5,000 ms
コード長 2,034 bytes
コンパイル時間 693 ms
コンパイル使用メモリ 92,884 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-06 00:46:33
合計ジャッジ時間 2,017 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 7 ms
4,376 KB
testcase_09 AC 7 ms
4,376 KB
testcase_10 AC 7 ms
4,376 KB
testcase_11 AC 6 ms
4,376 KB
testcase_12 AC 25 ms
4,376 KB
testcase_13 AC 23 ms
4,376 KB
testcase_14 AC 24 ms
4,376 KB
testcase_15 AC 23 ms
4,376 KB
testcase_16 AC 24 ms
4,376 KB
testcase_17 AC 13 ms
4,376 KB
testcase_18 AC 15 ms
4,380 KB
testcase_19 AC 39 ms
4,380 KB
testcase_20 AC 40 ms
4,376 KB
testcase_21 AC 16 ms
4,376 KB
testcase_22 AC 16 ms
4,376 KB
testcase_23 AC 20 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
using namespace std;

// Binary Indexed Tree
template <class T = int>
class BinaryIndexedTree
{
private:
    int n;
    vector<T> data;
public:
    BinaryIndexedTree(int n){ // コンストラクタ
        this->n = n;
        data.assign(n+1, 0);
    }
    void add(int k, T x){ // k番目の要素にxを加算する
        ++ k;
        while(k <= n){
            data[k] += x;
            k += k & -k;
        }
    }
    T sum(int k){ // 区間[0,k]の総和を返す
        ++ k;
        T ret = 0;
        while(k > 0){
            ret += data[k];
            k -= k & -k;
        }
        return ret;
    }
    T sum(int a, int b){ // 区間[a,b]の総和を返す
        return sum(b) - sum(a-1);
    }
};

int main()
{
    int n, q;
    cin >> n >> q;

    BinaryIndexedTree<long long> bit(2*n);
    for(int t=0; t<q; ++t){
        char x;
        int y, z;
        cin >> x >> y >> z;

        if(x == 'R'){
            int a = (2 * n - 1 - y + t) % (2 * n);
            bit.add(a, z);
        }
        else if(x == 'L'){
            int a = (y + t) % (2 * n);
            bit.add(a, z);
        }
        else{
            long long ret = 0;
            int a = (y + t) % (2 * n);
            int b = (z - 1 + t) % (2 * n);
            if(a <= b)
                ret += bit.sum(a, b);
            else
                ret += bit.sum(0, b) + bit.sum(a, 2*n-1);

            b = (2 * n - 1 - y + t) % (2 * n);
            a = (2 * n - z + t) % (2 * n);
            if(a <= b)
                ret += bit.sum(a, b);
            else
                ret += bit.sum(0, b) + bit.sum(a, 2*n-1);

            cout << ret << endl;
        }
    }

    return 0;
}
0