結果

問題 No.789 範囲の合計
ユーザー veqccveqcc
提出日時 2019-02-13 15:08:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,336 bytes
コンパイル時間 1,104 ms
コンパイル使用メモリ 103,280 KB
実行使用メモリ 21,744 KB
最終ジャッジ日時 2023-10-11 11:14:09
合計ジャッジ時間 3,970 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 2 ms
5,456 KB
testcase_02 AC 242 ms
21,744 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
権限があれば一括ダウンロードができます

ソースコード

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 = 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