結果

問題 No.259 セグメントフィッシング+
ユーザー keikei
提出日時 2018-09-29 02:34:16
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 77 ms / 2,000 ms
コード長 4,314 bytes
コンパイル時間 1,779 ms
コンパイル使用メモリ 170,508 KB
実行使用メモリ 6,528 KB
最終ジャッジ日時 2024-04-20 12:35:02
合計ジャッジ時間 5,241 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 67 ms
6,272 KB
testcase_09 AC 66 ms
6,528 KB
testcase_10 AC 65 ms
6,400 KB
testcase_11 AC 68 ms
6,400 KB
testcase_12 AC 70 ms
6,400 KB
testcase_13 AC 73 ms
6,400 KB
testcase_14 AC 70 ms
6,400 KB
testcase_15 AC 73 ms
6,400 KB
testcase_16 AC 71 ms
6,400 KB
testcase_17 AC 70 ms
6,272 KB
testcase_18 AC 70 ms
6,528 KB
testcase_19 AC 69 ms
6,528 KB
testcase_20 AC 77 ms
6,400 KB
testcase_21 AC 73 ms
6,400 KB
testcase_22 AC 73 ms
6,400 KB
testcase_23 AC 27 ms
5,376 KB
testcase_24 AC 38 ms
6,400 KB
testcase_25 AC 37 ms
6,528 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; }
template<class T> ostream& operator << (ostream& out,const vector<T> V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; }
template<class T> ostream& operator << (ostream& out,const vector<vector<T> > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }
template<class S,class T> ostream& operator << (ostream& out,const map<S,T> mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; }

/*
 <url:https://yukicoder.me/problems/no/259>
 問題文============================================================
 =================================================================
 解説=============================================================
 ================================================================
 */

template<typename type>
struct BIT0 { // 0-index
    int N;
    int nn;
    vector<type> data;
    //動的はmap<int?,type>data
    BIT0(int n) {
        N = n + 1;
        data = vector<type>(n + 1, 0);
        nn = 1;
        while (nn * 2 <= N)nn *= 2;
    }
    void add(ll i, type w) { // a[i] += w
        i++;
        for (int x = i; x < N; x += x & -x) {
            data[x] += w;
        }
    }
    type sum(ll i) { // iまでの和 [0,i)
        if(i < 0) return 0;
        type ret = 0;
        for (int x = i; x > 0; x -= x & -x) {
            ret += data[x];
        }
        return ret;
    }
    // [l, r)
    type sum(ll l, ll r) {
        return sum(r) - sum(l);
    }
};

int N,Q;
pll updateL(ll t,ll y,ll z,vector<BIT0<ll>>& bit){
    t %= 2*N;
    y += t;
    
    ll ind = -1;
    if(y < N){
        ind = 0;
    }else if(y < 2*N){
        y -= N;
        y = (N-1) - y;
        ind = 1;
    }else{
        y -= 2*N;
        ind = 0;
    }
    bit[ind].add(y,z);
    return pll(ind,y);
}
pll updateR(ll t,ll y,ll z,vector<BIT0<ll>>& bit){
    t %= 2*N;
    y -= t;
    
    ll ind = -1;
    if(y >= 0){
        ind = 1;
    }else if(y >= -N){
        y += N;
        y = (N-1) - y;
        ind = 0;
    }else{
        y += 2*N;
        ind = 1;
    }
    bit[ind].add(y,z);
    return pll(ind,y);
}
ll query(ll t,ll y,ll z,vector<BIT0<ll>>& bit){
    ll ret = 0;

    z--;
    {
        pll p1 = updateL(t,y,0,bit);
        pll p2 = updateL(t,z,0,bit);
        if(p1.first == p2.first){
            if(p1.first == 0){
                ret += bit[p1.first].sum(p1.second,p2.second+1);
            }else{
                ret += bit[p1.first].sum(p2.second,p1.second+1);
            }
        }else{
            if(p1.first == 0){
                ret += bit[p1.first].sum(p1.second,N) + bit[p2.first].sum(p2.second,N);
            }else{
                ret += bit[p1.first].sum(p1.second+1) + bit[p2.first].sum(p2.second+1);
            }
        }
    }
    {
        pll p1 = updateR(t,y,0,bit);
        pll p2 = updateR(t,z,0,bit);
        if(p1.first == p2.first){
            if(p1.first == 0){
                ret += bit[p1.first].sum(p2.second,p1.second+1);
            }else{
                ret += bit[p1.first].sum(p1.second,p2.second+1);
            }
        }else{
            if(p1.first == 0){
                ret += bit[p1.first].sum(p1.second+1) + bit[p2.first].sum(p2.second+1);
            }else{
                ret += bit[p1.first].sum(p1.second,N) + bit[p2.first].sum(p2.second,N);
            }
        }
    }
    
    return ret;
}
void solve(){
    cin >> N >> Q;
    vector<BIT0<ll>> bit;
    bit.push_back(BIT0<ll>(N)); bit.push_back(BIT0<ll>(N));
    while(Q--){
        char x;
        ll t,y,z;
        cin >> x >> t >> y >> z;
        if(x == 'L'){
            updateL(t,y,z,bit);
        }else if(x == 'R'){
            updateR(t,y,z,bit);
        }else{
            cout << query(t,y,z,bit) << endl;
        }
    }
}
int main(void) {
    cin.tie(0); ios_base::sync_with_stdio(false);
    solve();
    return 0;
}
0