結果

問題 No.789 範囲の合計
ユーザー ミドリムシミドリムシ
提出日時 2019-02-08 22:58:04
言語 C++11
(gcc 8.5.0)
結果
AC  
実行時間 114 ms / 1,000 ms
コード長 2,323 bytes
コンパイル時間 581 ms
使用メモリ 6,952 KB
最終ジャッジ日時 2023-02-01 17:00:12
合計ジャッジ時間 2,804 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 1 ms
4,900 KB
testcase_01 AC 1 ms
6,948 KB
testcase_02 AC 111 ms
5,772 KB
testcase_03 AC 68 ms
5,516 KB
testcase_04 AC 108 ms
5,956 KB
testcase_05 AC 97 ms
5,712 KB
testcase_06 AC 101 ms
5,884 KB
testcase_07 AC 62 ms
5,604 KB
testcase_08 AC 93 ms
6,952 KB
testcase_09 AC 88 ms
6,028 KB
testcase_10 AC 114 ms
6,948 KB
testcase_11 AC 91 ms
6,952 KB
testcase_12 AC 92 ms
5,992 KB
testcase_13 AC 1 ms
6,948 KB
testcase_14 AC 2 ms
4,900 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>
#include <string.h>
using namespace std;
#define all(x) (x).begin(), (x).end()

template< typename Monoid >
struct SegmentTree
{
    using F = function< Monoid(Monoid, Monoid) >;
    
    int sz;
    vector< Monoid > seg;
    
    const F f;
    const Monoid M1;
    
    SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1)
    {
        sz = 1;
        while(sz < n) sz <<= 1;
        seg.assign(2 * sz, M1);
    }
    
    void set(int k, const Monoid &x)
    {
        seg[k + sz] = x;
    }
    
    void build()
    {
        for(int k = sz - 1; k > 0; k--) {
            seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
        }
    }
    
    void update(int k, const Monoid &x)
    {
        k += sz;
        seg[k] = x;
        while(k >>= 1) {
            seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
        }
    }
    
    Monoid query(int a, int b)
    {
        Monoid L = M1, R = M1;
        for(a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
            if(a & 1) L = f(L, seg[a++]);
            if(b & 1) R = f(seg[--b], R);
        }
        return f(L, R);
    }
    
    Monoid operator[](const int &k) const
    {
        return seg[k + sz];
    }
};


int main(){
    int n;
    cin >> n;
    vector<int> x;
    int queries[n][3];
    for(int i = 0; i < n; i++){
        for(int j = 0; j < 3; j++){
            cin >> queries[i][j];
        }
        if(!queries[i][0]){
            x.push_back(queries[i][1]);
        }
    }
    sort(all(x));
    x.erase(unique(all(x)), x.end());
    int nums[x.size()];
    for(int i = 0; i < x.size(); i++){
        nums[i] = 0;
    }
    SegmentTree< int > seg(n, [](int a, int b){ return a + b; }, 0);
    for(int i = 0; i < n; i++){
        seg.set(i, 0);
    }
    seg.build();
    long ans = 0;
    for(int i = 0; i < n; i++){
        if(!queries[i][0]){
            int point = int(lower_bound(all(x), queries[i][1]) - x.begin());
            nums[point] += queries[i][2];
            seg.update(point, nums[point]);
        }else{
            int left = int(lower_bound(all(x), queries[i][1]) - x.begin());
            int right = int(upper_bound(all(x), queries[i][2]) - x.begin());
            ans += seg.query(left, right);
        }
    }
    cout << ans << endl;
}
0