結果
| 問題 |
No.728 ギブ and テイク
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-11-12 00:41:09 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 201 ms / 3,000 ms |
| コード長 | 3,139 bytes |
| コンパイル時間 | 1,717 ms |
| コンパイル使用メモリ | 175,804 KB |
| 実行使用メモリ | 35,964 KB |
| 最終ジャッジ日時 | 2024-11-24 19:35:22 |
| 合計ジャッジ時間 | 4,621 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 30 |
ソースコード
#include <bits/stdc++.h>
#define show(x) cerr << #x << " = " << x << endl
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
os << "sz=" << v.size() << "\n[";
for (const auto& p : v) {
os << p << ",";
}
os << "]\n";
return os;
}
template <typename S, typename T>
ostream& operator<<(ostream& os, const pair<S, T>& p)
{
os << "(" << p.first << "," << p.second
<< ")";
return os;
}
constexpr ll MOD = 1e9 + 7;
template <typename T>
constexpr T INF = numeric_limits<T>::max() / 100;
struct Sum {
using T = ll;
T operator()(const T& a, const T& b) const
{
return a + b;
}
T inv(const T& a) const
{
return -a;
}
static constexpr T identity()
{
return 0;
}
};
template <typename Base>
class BinaryIndexedTree
{
public:
using T = typename Base::T;
BinaryIndexedTree(const int n) : data_num(n), size(1 << (__lg(2 * data_num - 1))), value(size + 1, Base::identity()) { assert(n > 0); }
BinaryIndexedTree(const vector<T>& val) : data_num(val.size()), size(1 << (__lg(2 * data_num - 1))), value(size + 1, Base::identity())
{
for (int i = 1; i <= size; i++) {
value[i] = val[i - 1];
}
for (int x = 1; x < size; x++) {
value[x + (x & -x)] += value[x];
}
}
T accumulate(const int a) const
{
assert(0 <= a and a < data_num);
int ind = a + 1;
T sum = Base::identity();
while (ind > 0) {
sum = op(sum, value[ind]);
ind &= ind - 1;
}
return sum;
}
void add(const int a, const T& val)
{
assert(0 <= a and a < data_num);
int ind = a + 1;
while (ind <= size) {
value[ind] = op(value[ind], val);
ind += ind & (-ind);
}
}
private:
const int data_num;
const int size;
const Base op{};
vector<T> value;
};
struct Data {
ll value;
bool from_a;
int index;
ll A_R;
bool operator<(const Data& d) const
{
return (value != d.value) ? (value < d.value) : (from_a < d.from_a);
}
};
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
BinaryIndexedTree<Sum> bit(N);
vector<ll> A(N);
for (int i = 0; i < N; i++) {
cin >> A[i];
}
vector<Data> datas;
for (int i = 0; i < N; i++) {
ll L, R;
cin >> L >> R;
datas.push_back(Data{A[i], true, i, A[i] + R});
datas.push_back(Data{A[i] - L, false, i, 0});
}
sort(datas.begin(), datas.end());
ll sum = 0;
for (const auto& d : datas) {
const int ind = d.index;
if (d.from_a) {
const int upper = upper_bound(A.begin(), A.end(), d.A_R) - A.begin() - 1;
if (ind < upper) {
sum += bit.accumulate(upper) - bit.accumulate(ind);
}
} else {
bit.add(ind, 1);
}
}
cout << sum << endl;
return 0;
}