結果

問題 No.151 セグメントフィッシング
ユーザー mayoko_
提出日時 2015-02-13 18:07:35
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 47 ms / 5,000 ms
コード長 2,333 bytes
コンパイル時間 784 ms
コンパイル使用メモリ 75,472 KB
実行使用メモリ 14,208 KB
最終ジャッジ日時 2024-06-23 19:46:07
合計ジャッジ時間 1,904 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

// kmjpさんのコード参考
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <utility>
#include <set>
#include <cctype>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cmath>

using namespace std;
typedef long long ll;

const int MAXN = 40040;

template<class V, int ME> class BIT {
public:
    V bit[1<<ME];
    V total(int e) {
        V s = 0;
        e++;
        while (e) {
            s += bit[e-1];
            e -= e & -e;
        }
        return s;
    }
    void update(int e, V val) {
        e++;
        while (e <= (1<<ME)) {
            bit[e-1] += val;
            e += e&-e;
        }
    }
};

BIT<ll, 20> Lbit;
BIT<ll, 20> Rbit;

int N, N2, Q;
char X[30000];
int Y[30000], Z[30000];

int momo(int a) {return (a%N2+N2)%N2;}

void solve() {
    cin >> N >> Q;
    N2 = 2*N;
    for (int i = 0; i < Q; i++) {
        cin >> X[i] >> Y[i] >> Z[i];
        if (X[i] == 'L') {
            Lbit.update(1+momo(Y[i]+i+1), Z[i]);
        }
        if (X[i] == 'R') {
            Rbit.update(1+momo(Y[i]-(i+1)), Z[i]);
        }
        if (X[i] == 'C') {
            Z[i]--;
            ll ret = 0;
            {
                int x = momo(Y[i]+(i+1));
                int y = momo(Z[i]+(i+1));
                if (x <= y) ret += Lbit.total(y+1)-Lbit.total(x);
                else ret += Lbit.total(N2+2)-Lbit.total(x)+Lbit.total(y+1);
            }
            {
                int x = momo(N2-1-Z[i]+(i+1));
                int y = momo(N2-1-Y[i]+(i+1));
                if (x <= y) ret += Lbit.total(y+1)-Lbit.total(x);
                else ret += Lbit.total(N2+2)-Lbit.total(x)+Lbit.total(y+1);
            }
            {
                int x = momo(Y[i]-(i+1));
                int y = momo(Z[i]-(i+1));
                if (x <= y) ret += Rbit.total(y+1)-Rbit.total(x);
                else ret += Rbit.total(N2+2)-Rbit.total(x)+Rbit.total(y+1);
            }
            {
                int x = momo(N2-1-Z[i]-(i+1));
                int y = momo(N2-1-Y[i]-(i+1));
                if (x <= y) ret += Rbit.total(y+1)-Rbit.total(x);
                else ret += Rbit.total(N2+2)-Rbit.total(x)+Rbit.total(y+1);
            }
            cout << ret << endl;
        }
    }
}

int main() {
    solve();
}
0