結果

問題 No.728 ギブ and テイク
コンテスト
ユーザー 梧桐
提出日時 2026-01-28 00:13:28
言語 C++17
(gcc 15.2.0 + boost 1.89.0)
結果
AC  
実行時間 210 ms / 3,000 ms
コード長 1,204 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 895 ms
コンパイル使用メモリ 102,480 KB
実行使用メモリ 12,216 KB
最終ジャッジ日時 2026-01-28 00:13:34
合計ジャッジ時間 5,460 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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