結果

問題 No.789 範囲の合計
ユーザー ganariyaganariya
提出日時 2019-02-08 22:31:17
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 602 ms / 1,000 ms
コード長 2,627 bytes
コンパイル時間 1,796 ms
コンパイル使用メモリ 159,764 KB
実行使用メモリ 67,112 KB
最終ジャッジ日時 2023-09-14 03:50:37
合計ジャッジ時間 7,031 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 508 ms
62,520 KB
testcase_03 AC 104 ms
15,836 KB
testcase_04 AC 484 ms
59,796 KB
testcase_05 AC 448 ms
63,268 KB
testcase_06 AC 462 ms
62,632 KB
testcase_07 AC 96 ms
16,500 KB
testcase_08 AC 279 ms
35,296 KB
testcase_09 AC 250 ms
34,140 KB
testcase_10 AC 602 ms
67,112 KB
testcase_11 AC 457 ms
60,904 KB
testcase_12 AC 460 ms
61,024 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <unordered_map>
#include <climits>
#include <set>
#include <unordered_set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <queue>
#include <random>
#include <complex>
#include <regex>
#include <locale>
#include <random>

using namespace std;

#define SHOW_VECTOR(v) {std::cerr << #v << "\t:";for(const auto& xxx : v){std::cerr << xxx << " ";}std::cerr << "\n";}
#define SHOW_MAP(v){std::cerr << #v << endl; for(const auto& xxx: v){std::cerr << xxx.first << " " << xxx.second << "\n";}}

using LL = long long;

map<int, int> f;
map<int, int> g;

class SegmentTree {
    int n;
    vector<LL> node;

public:
    SegmentTree() {
        int sz = f.size();
        n = 1;
        while (n < sz) n *= 2;
        node.resize(2 * n - 1, 0);
    }

    void add(int index, int val) {
        index += n - 1;
        node[index] += val;
        while (index > 0) {
            index = (index - 1) / 2;
            node[index] = node[index * 2 + 1] + node[index * 2 + 2];
        }
    }

    LL get_sum(int a, int b, int k = 0, int l = 0, int r = -1) {
        if (r < 0) r = n;
        if (r <= a || b <= l) return 0;
        if (a <= l && r <= b) return node[k];
        LL vl = get_sum(a, b, k * 2 + 1, l, (l + r) / 2);
        LL vr = get_sum(a, b, k * 2 + 2, (l + r) / 2, r);
        return vl + vr;
    }

};

int main() {


    int Q;
    cin >> Q;

    vector<LL> p(Q), l(Q), r(Q);
    for (int i = 0; i < Q; i++) {
        cin >> p[i] >> l[i] >> r[i];
    }

    vector<LL> d;
    for (int i = 0; i < Q; i++) d.push_back(l[i]), d.push_back(r[i]);

    vector<LL> dd;
    for (int i = 0; i < d.size(); i++) {
        for (int j = -1; j <= 1; j++) {
            dd.push_back(d[i] + j);
        }
    }

    sort(dd.begin(), dd.end());
    dd.erase(unique(dd.begin(), dd.end()), dd.end());
 //   SHOW_VECTOR(dd);

    for (int i = 0; i < dd.size(); i++) f[i] =dd[i];
    for (int i = 0; i < dd.size(); i++) g[dd[i]] = i;
//
//    //SHOW_VECTOR(d);
    LL ans = 0;
    SegmentTree seg;

    for (int i = 0; i < Q; i++) {
        if (p[i] == 0) {
            seg.add(g[l[i]], r[i]);
        } else {
            ans += seg.get_sum(g[l[i]], g[r[i]] + 1);
        }
    }


    cout << ans << endl;


    return 0;
}

























0