結果

問題 No.789 範囲の合計
コンテスト
ユーザー veqcc
提出日時 2019-02-13 15:07:16
言語 C++14
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++14 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
RE  
実行時間 -
コード長 1,339 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 674 ms
コンパイル使用メモリ 117,652 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2026-04-04 04:35:32
合計ジャッジ時間 1,473 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other RE * 15
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
typedef long long ll;
typedef unsigned int uint;
using namespace std;

const int MAX_N = 1000000000;
ll bit[MAX_N + 1];

// iが与えられた時に、a[1]+a[2]+...+a[i]を計算する
ll sum(int i) {
    ll s = 0;
    while (i > 0) {
        s += bit[i];
        i -= i & -i; // 最後の1ビット
    }
    return s;
}

// iとxが与えられた時に、a[i]にxを足す
void add(int i, int x) {
    while (i <= MAX_N) {
        bit[i] += x;
        i += i & -i;
    }
}

int main() {
    cin.sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;
    int a[n], x[n], y[n];
    set <int> st;
    for (int i = 0; i < n; i++) {
        cin >> a[i] >> x[i] >> y[i];
        st.insert(x[i]);
        st.insert(y[i]);
    }

    map <int, int> mp;
    set <int> :: iterator itr;
    int count = 0;
    for (itr = st.begin(); itr != st.end(); ++itr){
        mp[*itr] = count++;
    }

    ll sm = 0LL;
    for (int i = 0; i < n; i++) {
        if (a[i] == 0) {
            add(mp[x[i]], mp[y[i]]);
        } else {
            sm += (ll)(sum(mp[y[i]]) - sum(mp[x[i]] - 1));
        }
    }

    cout << sm << "\n";
    return 0;
}
0