結果

問題 No.789 範囲の合計
ユーザー mamekinmamekin
提出日時 2019-02-08 23:04:43
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 235 ms / 1,000 ms
コード長 3,478 bytes
コンパイル時間 1,373 ms
コンパイル使用メモリ 135,368 KB
実行使用メモリ 21,120 KB
最終ジャッジ日時 2023-09-14 03:55:24
合計ジャッジ時間 4,224 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 219 ms
20,088 KB
testcase_03 AC 81 ms
9,580 KB
testcase_04 AC 206 ms
19,720 KB
testcase_05 AC 180 ms
20,224 KB
testcase_06 AC 186 ms
20,040 KB
testcase_07 AC 74 ms
9,548 KB
testcase_08 AC 142 ms
14,344 KB
testcase_09 AC 129 ms
12,908 KB
testcase_10 AC 235 ms
21,120 KB
testcase_11 AC 191 ms
19,696 KB
testcase_12 AC 185 ms
19,732 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

class SegmentTree
{
public:
    typedef long long T1;
    typedef long long T2;

    // データの初期値、以下の条件を満たすこと
    //   uniteData(v, INIT_DATA) == v
    static const T1 INIT_DATA;

    // 2つの区間の計算結果v1,v2に対して、
    // その2つの区間を統合した区間における計算結果を返す
    static T1 uniteData(T1 v1, T1 v2){
        return v1 + v2;
    }

private:
    // 前回の値がprevである要素に対して、
    // パラメータxを用いた更新処理を適用した後の計算結果を返す
    T1 updateData(T1 prev, T2 x){
        return prev + x;
    }

    int n;
    vector<T1> data;
    void updateTree(int a, int k, int l, int r, T2 x){
        if(r - l == 1 && a == l){
            data[k] = updateData(data[k], x);
        }
        else if(l <= a && a < r){
            updateTree(a, k*2+1, l, (l+r+1)/2, x);
            updateTree(a, k*2+2, (l+r+1)/2, r, x);
            data[k] = uniteData(data[k*2+1], data[k*2+2]);
        }
    }
    T1 getValue(int a, int b, int k, int l, int r){
        if(a <= l && r <= b){
            return data[k];
        }
        else if(a < r && l < b){
            T1 v1 = getValue(a, b, k*2+1, l, (l+r+1)/2);
            T1 v2 = getValue(a, b, k*2+2, (l+r+1)/2, r);
            return uniteData(v1, v2);
        }
        else{
            return INIT_DATA;
        }
    }

public:
    SegmentTree(int n0){
        n = 1;
        while(n < n0)
            n *= 2;
        data.assign(2*n-1, INIT_DATA);
    }
    SegmentTree(const vector<T1>& v) : SegmentTree((int)v.size()){
        for(unsigned i=0; i<v.size(); ++i)
            data[n-1+i] = v[i];
        for(int k=n-2; k>=0; --k)
            data[k] = uniteData(data[k*2+1], data[k*2+2]);
    }
    // a番目の要素にパラメータxによる更新処理を適用
    void update(int a, T2 x){
        updateTree(a, 0, 0, n, x);
    }
    // 区間[a,b)の計算結果を返す
    T1 get(int a, int b){
        return getValue(a, b, 0, 0, n);
    }
};
const SegmentTree::T1 SegmentTree::INIT_DATA = 0;

int main()
{
    int n;
    cin >> n;
    vector<vector<int> > q(n, vector<int>(3));
    vector<int> tmp;
    for(int i=0; i<n; ++i){
        for(int j=0; j<3; ++j)
            cin >> q[i][j];
        tmp.push_back(q[i][1]);
        if(q[i][0] == 1)
            tmp.push_back(q[i][2]);
    }
    sort(tmp.begin(), tmp.end());
    tmp.erase(unique(tmp.begin(), tmp.end()), tmp.end());
    
    int m = tmp.size();
    map<int, int> index;
    for(int i=0; i<m; ++i)
        index[tmp[i]] = i;

    SegmentTree st(m);
    long long ans = 0;
    for(int i=0; i<n; ++i){
        if(q[i][0] == 0){
            int x = index[q[i][1]];
            int y = q[i][2];
            st.update(x, y);
        }
        else{
            int l = index[q[i][1]];
            int r = index[q[i][2]] + 1;
            ans += st.get(l, r);
        }
    }
    cout << ans << endl;

    return 0;
}
0