結果

問題 No.789 範囲の合計
ユーザー veqccveqcc
提出日時 2019-02-13 15:14:41
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 283 ms / 1,000 ms
コード長 1,348 bytes
コンパイル時間 931 ms
コンパイル使用メモリ 102,708 KB
実行使用メモリ 23,632 KB
最終ジャッジ日時 2023-10-11 11:15:26
合計ジャッジ時間 3,947 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,468 KB
testcase_01 AC 2 ms
5,524 KB
testcase_02 AC 244 ms
21,672 KB
testcase_03 AC 44 ms
6,716 KB
testcase_04 AC 216 ms
20,916 KB
testcase_05 AC 158 ms
21,940 KB
testcase_06 AC 187 ms
21,744 KB
testcase_07 AC 40 ms
6,728 KB
testcase_08 AC 113 ms
13,564 KB
testcase_09 AC 105 ms
12,852 KB
testcase_10 AC 283 ms
23,632 KB
testcase_11 AC 201 ms
20,596 KB
testcase_12 AC 197 ms
20,532 KB
testcase_13 AC 2 ms
5,460 KB
testcase_14 AC 2 ms
5,464 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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 = 1 << 19;
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 = 1;
    for (itr = st.begin(); itr != st.end(); ++itr){
        mp[*itr] = count;
        count++;
    }

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

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