結果

問題 No.789 範囲の合計
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2020-06-12 01:07:02
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 141 ms / 1,000 ms
コード長 2,050 bytes
コンパイル時間 2,607 ms
コンパイル使用メモリ 160,496 KB
実行使用メモリ 7,776 KB
最終ジャッジ日時 2023-09-06 08:50:58
合計ジャッジ時間 5,025 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 141 ms
7,028 KB
testcase_03 AC 66 ms
4,376 KB
testcase_04 AC 141 ms
7,232 KB
testcase_05 AC 119 ms
6,668 KB
testcase_06 AC 123 ms
7,064 KB
testcase_07 AC 59 ms
4,376 KB
testcase_08 AC 125 ms
7,776 KB
testcase_09 AC 115 ms
7,296 KB
testcase_10 AC 134 ms
6,064 KB
testcase_11 AC 121 ms
7,228 KB
testcase_12 AC 120 ms
7,204 KB
testcase_13 AC 2 ms
4,384 KB
testcase_14 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2020.06.12 01:06:54
**/

#include "bits/stdc++.h" 
using namespace std; 
typedef long long ll;



template< typename T >
struct BinaryIndexedTree {
    std::vector< T > data;
    BinaryIndexedTree(int sz) {
        data.assign(++sz, 0);
    }
    
    inline T sum(int k) {
        T ret = 0;
        for (++k; k > 0; k -= k & -k) ret += data[k];
        return (ret);
    }
    
    inline T sum(int left, int right) {
        if (left > right) return 0;
        return sum(right) - sum(left - 1);
    }
    
    
    inline void add(int k, T x) {
        for (++k; k < data.size(); k += k & -k) data[k] += x;
    }
    
    
    
    int get(ll k) {
        ++k;
        int res = 0;
        int N = 1; while (N < (int)data.size()) N *= 2;
        for (int i = N / 2; i > 0; i /= 2) {
            if (res + i < (int)data.size() && data[res + i] < k) {
                k = k - data[res + i];
                res = res + i;
            }
        }
        return res + 1;
    }
    void print() {
        std::cout << "[ ";
        for (int i = 0; i < data.size() - 1; i++) {
            std::cout << sum(i, i);
            if (i < data.size() - 2) cout << ", ";
        }
        std::cout << " ]" << endl;
    }
};
int main() {
    int n;cin >> n;
    vector<tuple<int,int,int>> query(n);
    map<int,int> mp;
    for (int i = 0; i < n; i++) {
        int t,x,y;cin >> t >> x >> y;
        query[i] = make_tuple(t,x,y);
        if (t == 0) {
            mp[x] = 0;
        }
    }
    int t = 0;
    mp[-1] = t++;
    for(auto p : mp) mp[p.first] = t++;
    mp[1000000001] = t++;
    BinaryIndexedTree<int> bit(t);
    ll ans = 0;
    for (int i = 0; i < n; i++) {
        int q = get<0>(query[i]);
        int x = get<1>(query[i]);
        int y = get<2>(query[i]);
        if (q == 0) {
            bit.add(mp[x],y);
        }
        else {
            ans += bit.sum(mp.lower_bound(x)->second,prev(mp.upper_bound(y))->second);
        }
    }
    cout << ans << endl;
    return 0;
}
0