結果

問題 No.259 セグメントフィッシング+
ユーザー はまやんはまやん
提出日時 2017-04-25 00:05:32
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 57 ms / 2,000 ms
コード長 2,104 bytes
コンパイル時間 1,754 ms
コンパイル使用メモリ 170,640 KB
実行使用メモリ 11,648 KB
最終ジャッジ日時 2024-09-13 12:18:10
合計ジャッジ時間 5,430 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)



typedef long long ll;
#define def 0
template<class V, int NV> struct SegTree {
    V comp(V l, V r) { return l + r; };
    vector<V> val; SegTree() { val = vector<V>(NV * 2, def); }
    V get(int l, int r) { //[l,r]
        if (l > r) return def;
        l += NV; r += NV + 1; V ret = def;
        while (l < r) { if (l & 1) ret = comp(ret, val[l++]); if (r & 1) ret = comp(ret, val[--r]); l /= 2; r /= 2; }
        return ret;
    }
    void update(int i, V v) { i += NV; val[i] = v; while (i>1) i >>= 1, val[i] = comp(val[i * 2], val[i * 2 + 1]); }
    void add(int i, V v) { update(i, val[i + NV] + v); }
};
template<class V, int NV> struct ShiftSegTree {
    SegTree<V, NV> st;
    int NM, base;
    ShiftSegTree(int _NM) : NM(_NM), base(0) {}

    void shift(int x) { // 全ての要素をx分右へシフトする
        base -= x % NM;
        if (base < 0) base += NM;
        base %= NM;
    }
    V get(int l, int r) { //[l,r]
        l += base, r += base;
        if (NM <= l) return st.get(l % NM, r % NM);
        if (r < NM) return st.get(l, r);
        return st.comp(st.get(l, NM - 1), st.get(0, r % NM));
    }
    void update(int i, V v) {
        st.update((i + base) % NM, v);
    }
    void add(int i, V v) {
        st.add((i + base) % NM, v);
    }
    void print() {
        rep(i, 0, NM) cout << get(i, i) << " ";
        cout << endl;
    }
};
//-----------------------------------------------------------------------------------
int main() {
    int N, Q;

    cin.tie(0);
    ios::sync_with_stdio(false);

    cin >> N >> Q;

    ShiftSegTree<ll, 1 << 19> sst(2 * N);

    int pt = 0;
    rep(q, 0, Q) {
        char x; int t, y, z; cin >> x >> t >> y >> z;

        sst.shift(t - pt);

        if (x == 'L') sst.add(2 * N - 1 - y, z);
        else if (x == 'R') sst.add(y, z);
        else {
            z--;
            ll right = sst.get(y, z);
            ll left = sst.get(2 * N - 1 - z, 2 * N - 1 - y);
            printf("%lld\n", right + left);
        }

        pt = t;
    }
}
0