結果

問題 No.789 範囲の合計
ユーザー face4face4
提出日時 2019-02-08 22:33:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,539 bytes
コンパイル時間 846 ms
コンパイル使用メモリ 89,476 KB
実行使用メモリ 11,268 KB
最終ジャッジ日時 2023-09-14 03:50:41
合計ジャッジ時間 3,283 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 1 ms
4,376 KB
testcase_02 WA -
testcase_03 AC 66 ms
4,540 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 59 ms
4,508 KB
testcase_08 AC 154 ms
11,268 KB
testcase_09 AC 140 ms
10,448 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<set>
using namespace std;
typedef long long ll;

struct BIT{
    // 1-origin
    vector<ll> v;
    int n;

    BIT(int _n):
        v(vector<ll>(_n+1, 0)), n(_n) {}

    ll sum(int i){
        ll s = 0;
        while(i > 0){
            s += v[i];
            i -= lsb(i);
        }
        return s;
    }

    ll sum(int l, int r){
        return sum(r) - sum(l-1);
    }

    void add(int i, int x){
        while(i <= n){
            v[i] += x;
            i += lsb(i);
        }
    }

private:
    // least significant bit
    int lsb(int i){
        return i & -i;
    }
};

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

    set<int> s;
    int a[n], b[n], c[n];
    for(int i = 0; i < n; i++){
        cin >> a[i] >> b[i] >> c[i];
        if(a[i] == 0)   s.insert(b[i]);
    }

    map<int,int> zip;
    int cur = 1;
    for(auto it = s.begin(); it != s.end(); it++){
        zip[*it] = cur++;
    }

    vector<int> tmp(zip.size(), 0);
    //  SegmentTree st(tmp);
    BIT bit(zip.size()+1);

    ll ans = 0;
    for(int i = 0; i < n; i++){
        if(a[i] == 0){
            bit.add(zip[b[i]], c[i]);
        }else if(a[i] == 1){
            auto l = zip.lower_bound(b[i]);
            if(l == zip.end())  l--;
            auto r = zip.lower_bound(c[i]);
            if(r == zip.end())  r--;
            int le = (*l).second, ri = (*r).second;
            if(le <= ri)    ans += bit.sum(le, ri);
        }
    }

    cout << ans << endl;
    
    return 0;
}
0