結果
| 問題 | No.728 ギブ and テイク |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-01-27 03:35:12 |
| 言語 | C++14 (gcc 15.2.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 232 ms / 3,000 ms |
| コード長 | 1,409 bytes |
| 記録 | |
| コンパイル時間 | 962 ms |
| コンパイル使用メモリ | 96,412 KB |
| 実行使用メモリ | 14,284 KB |
| 最終ジャッジ日時 | 2026-01-27 03:35:18 |
| 合計ジャッジ時間 | 4,590 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 30 |
ソースコード
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 300010;
int n, lidx[N], ridx[N];
LL a[N], ans;
priority_queue<PII, vector<PII>, greater<PII>> pq;
struct BIT {
int tr[N];
void Add(int idx, int val) {
for (; idx <= n; idx += idx & -idx) tr[idx] += val;
}
int Sum(int idx) {
int res = 0;
for (; idx > 0; idx -= idx & -idx) res += tr[idx];
return res;
}
int Query(int l, int r) {
if (l > r) return 0;
return Sum(r) - Sum(l - 1);
}
};
BIT bit;
void Solve() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%lld", &a[i]);
for (int i = 1; i <= n; ++i) {
LL l, r;
scanf("%lld%lld", &l, &r);
LL left = a[i] - l, right = 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;
}
ans = 0LL;
for (int i = 1; i <= n; ++i) {
while (!pq.empty() && pq.top().first < i) {
int idx = pq.top().second;
pq.pop();
bit.Add(idx, -1);
}
int l = lidx[i];
if (l <= i - 1) ans += bit.Query(l, i - 1);
bit.Add(i, 1);
pq.push(PII(ridx[i], i));
}
printf("%lld\n", ans);
}
int main() {
Solve();
return 0;
}