結果

問題 No.728 ギブ and テイク
ユーザー merom686merom686
提出日時 2018-08-25 01:30:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 167 ms / 3,000 ms
コード長 1,604 bytes
コンパイル時間 758 ms
コンパイル使用メモリ 81,496 KB
実行使用メモリ 11,200 KB
最終ジャッジ日時 2023-09-05 22:57:07
合計ジャッジ時間 4,918 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 10 ms
4,380 KB
testcase_14 AC 14 ms
4,376 KB
testcase_15 AC 7 ms
4,376 KB
testcase_16 AC 12 ms
4,376 KB
testcase_17 AC 12 ms
4,380 KB
testcase_18 AC 117 ms
8,920 KB
testcase_19 AC 124 ms
9,292 KB
testcase_20 AC 143 ms
10,312 KB
testcase_21 AC 134 ms
9,624 KB
testcase_22 AC 53 ms
5,744 KB
testcase_23 AC 33 ms
4,760 KB
testcase_24 AC 98 ms
7,740 KB
testcase_25 AC 95 ms
7,852 KB
testcase_26 AC 48 ms
5,688 KB
testcase_27 AC 167 ms
11,200 KB
testcase_28 AC 123 ms
11,084 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
using ll = long long;

struct BIT {
    BIT(int n) : b(n + 1), n(n) {}
    void add(int i, int v) {
        for (int k = i + 1; k <= n; k += k & -k) b[k] += v;
    }
    int sum(int k) {
        int s = 0;
        for (; k > 0; k -= k & -k) s += b[k];
        return s;
    }
    vector<int> b;
    int n;
};

struct P {
    bool operator<(const P& p) const {
        return l < p.l;
    }
    int a, l, r;
};

// i0以上i1以下の値が返る iの値域はi0以上i1未満
template <class F>
int upper_bound(int i0, int i1, F f) {
    while (i0 < i1) {
        int i = (i0 + i1) / 2;
        if (f(i)) i0 = i + 1; else i1 = i;
    }
    return i0;
}

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

    int n;
    cin >> n;

    vector<P> p(n);
    for (int i = 0; i < n; i++) {
        cin >> p[i].a;
    }

    for (int i = 0; i < n; i++) {
        int l, r;
        cin >> l >> r;

        p[i].l = p[i].a - l;
        p[i].r = p[i].a + r;
    }

    vector<P> q = p;
    for (int i = 0; i < n; i++) {
        q[i].r = i;
    }
    sort(q.begin(), q.end());

    ll s = 0;
    BIT bt(n);
    int j = 0;
    for (int i = 0; i < n; i++) {
        int i1 = upper_bound(0, n, [&](int h) {
            return p[h].a <= p[i].r;
        });
        for (; j < n; j++) {
            if (q[j].l > p[i].a) break;
            bt.add(q[j].r, 1);
        }
        s += bt.sum(i1) - bt.sum(i + 1);
    }

    cout << s << endl;

    return 0;
}
0