結果

問題 No.728 ギブ and テイク
コンテスト
ユーザー WutongDeath
提出日時 2026-01-28 00:13:28
言語 C++17
(gcc 15.2.0 + boost 1.90.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 135 ms / 3,000 ms
+ 96µs
コード長 1,204 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 474 ms
コンパイル使用メモリ 99,980 KB
実行使用メモリ 12,224 KB
最終ジャッジ日時 2026-07-20 21:54:28
合計ジャッジ時間 4,805 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <iostream>
#include <algorithm>
#include <queue>

using namespace std;

typedef pair<int, int> PII;
typedef long long LL;

const int N = 300010;

LL ans;
int n, a[N], lidx[N], ridx[N], tr[N];
priority_queue<PII, vector<PII>, greater<PII>> pq;

int LowBit(int x) { return x & -x; }

void Add(int u, int v) {
    for (int i = u; i <= n; i += LowBit(i)) tr[i] += v;
}

LL Query(int x) {
    LL ret = 0LL;
    for (int i = x; i; i -= LowBit(i)) ret += tr[i];
    return ret;
}

int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
    for (int i = 1, l, r; i <= n; ++i) {
        scanf("%d%d", &l, &r);
        LL left = 0LL + a[i] - l, right = 0LL + a[i] + r;
        lidx[i] = lower_bound(a + 1, a + n + 1, left) - a;
        ridx[i] = upper_bound(a + 1, a + n + 1, right) - a - 1;
    }

    for (int i = 1; i <= n; ++i) {
        while (!pq.empty() && pq.top().first < i) {
            int idx = pq.top().second;
            pq.pop();
            Add(idx, -1);
        }

        int l = lidx[i];
        if (l <= i - 1) ans += Query(i - 1) - Query(l - 1);

        Add(i, 1);
        pq.push({ ridx[i], i });
    }

    printf("%lld\n", ans);

    return 0;
}
0