結果

問題 No.728 ギブ and テイク
ユーザー たこしたこし
提出日時 2018-09-28 17:12:39
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 517 ms / 3,000 ms
コード長 3,070 bytes
コンパイル時間 2,474 ms
コンパイル使用メモリ 212,644 KB
実行使用メモリ 40,988 KB
最終ジャッジ日時 2023-08-02 10:33:39
合計ジャッジ時間 9,006 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
13,224 KB
testcase_01 AC 9 ms
13,148 KB
testcase_02 AC 9 ms
13,216 KB
testcase_03 AC 10 ms
13,324 KB
testcase_04 AC 9 ms
13,136 KB
testcase_05 AC 9 ms
13,372 KB
testcase_06 AC 9 ms
13,328 KB
testcase_07 AC 8 ms
13,280 KB
testcase_08 AC 9 ms
13,288 KB
testcase_09 AC 8 ms
13,360 KB
testcase_10 AC 9 ms
13,144 KB
testcase_11 AC 10 ms
13,356 KB
testcase_12 AC 9 ms
13,184 KB
testcase_13 AC 31 ms
15,096 KB
testcase_14 AC 44 ms
15,632 KB
testcase_15 AC 24 ms
14,232 KB
testcase_16 AC 41 ms
15,288 KB
testcase_17 AC 39 ms
15,308 KB
testcase_18 AC 360 ms
34,184 KB
testcase_19 AC 453 ms
33,700 KB
testcase_20 AC 462 ms
39,232 KB
testcase_21 AC 409 ms
34,428 KB
testcase_22 AC 162 ms
23,204 KB
testcase_23 AC 109 ms
19,784 KB
testcase_24 AC 300 ms
32,076 KB
testcase_25 AC 304 ms
32,096 KB
testcase_26 AC 156 ms
23,156 KB
testcase_27 AC 517 ms
40,988 KB
testcase_28 AC 443 ms
40,988 KB
testcase_29 AC 10 ms
13,364 KB
testcase_30 AC 10 ms
13,184 KB
testcase_31 AC 11 ms
13,492 KB
testcase_32 AC 11 ms
13,244 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <cstdio>
#include <string>

using namespace std;

#define INF 100000000
#define YJ 1145141919
#define INF_INT_MAX 2147483647
#define INF_LL_MAX 9223372036854775807
#define EPS 1e-10
#define MOD 1000000007
#define Pi acos(-1)
#define LL long long
#define ULL unsigned long long
#define LD long double

class Node {
public:
    Node(LL v = unitValue) : value(v) {

    }

    Node operator + (const Node& monoid) const {
        return Node(this->getValue() + monoid.getValue());
    }

    static Node unit() {
        return Node(unitValue);
    }

    void setValue(LL l) {
        this->value = l;
    }

    LL getValue() const {
        return this->value;
    }

private:
    LL value;
    static const LL unitValue = 0;
};

template <class T>
class SegmentTree {
public:
    SegmentTree(int size) {
        init(size);
    }

    void init(int size) {
        vec.clear();
        k = 1;
        while(k < size) {
            k *= 2;
        }
        for(int i = 0; i < 2*k; i++) {
            vec.push_back(T::unit());
        }
    }

    void update(int i, T x) { //iをxに変更する
        i += k-1;
        vec[i] = x;
        while(i > 0) {
            i = (i-1)/2;
            vec[i] = vec[i*2+1] + vec[i*2+2];
        }
    }

    T find(int l, int r) {
        return _find(l, r, 0, 0, k);
    }

private:
    vector<T> vec;
    int k;

    T _find(int l, int r, int k, int x, int y) {
        if (r <= x || y <= l) { //重なってない場合
            return T::unit();
        } else if (l <= x && y <= r) { //完全にかぶってる場合
            return vec[k];
        } else { //中途半端にかぶってる場合
            int mid = (x+y)/2;
            T lNode = _find(l, r, 2*k+1, x, mid);
            T rNode = _find(l, r, 2*k+2, mid, y);
            return lNode + rNode;
        }
    }
};

#define int long long

const int MAX_N = 300005;

int N;

class Child {
public:
  int a, l, r;
  int index;
  bool operator < (const Child& c) const {
    return a+r < c.a+c.r;
  }

  

}children[MAX_N];

int A[MAX_N];

signed main()
{
  unique_ptr<SegmentTree<Node> > segmentTree = unique_ptr<SegmentTree<Node> > (new SegmentTree<Node>(MAX_N));
  
  cin >> N;
  for(int i = 0; i < N; i++) {
    cin >> children[i].a;
    A[i] = children[i].a;
    segmentTree->update(i, 1);
  }

  for(int i = 0; i < N; i++) {
    cin >> children[i].l >> children[i].r;
    children[i].index = i;
  }

  int ans = 0;

  vector<pair<int, int> > vec;
  for(int i = 0; i < N; i++) {
    vec.push_back(make_pair(children[i].a + children[i].r, i+1));
    vec.push_back(make_pair(children[i].a, -(i+1)));
  }

  sort(begin(vec), end(vec));

  for(auto it : vec) {
    int index = it.second;
    if(index > 0) {
      index--;
      segmentTree->update(index, 0);
    } else {
      index = -1*index;
      index--;
      const Child& child = children[index];
      int l = lower_bound(A, A+index, child.a - child.l) - A;
      ans += segmentTree->find(l, index).getValue();
    }
  }

  cout << ans << endl;

  return 0;
}
0