結果

問題 No.728 ギブ and テイク
ユーザー mamekinmamekin
提出日時 2018-10-06 20:49:27
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 500 ms / 3,000 ms
コード長 2,630 bytes
コンパイル時間 1,296 ms
コンパイル使用メモリ 124,488 KB
実行使用メモリ 12,544 KB
最終ジャッジ日時 2024-04-20 18:06:45
合計ジャッジ時間 6,430 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 24 ms
5,376 KB
testcase_14 AC 39 ms
5,376 KB
testcase_15 AC 18 ms
5,376 KB
testcase_16 AC 35 ms
5,376 KB
testcase_17 AC 33 ms
5,376 KB
testcase_18 AC 350 ms
9,856 KB
testcase_19 AC 376 ms
10,240 KB
testcase_20 AC 440 ms
11,464 KB
testcase_21 AC 394 ms
10,624 KB
testcase_22 AC 157 ms
6,144 KB
testcase_23 AC 100 ms
5,376 KB
testcase_24 AC 291 ms
8,832 KB
testcase_25 AC 286 ms
8,832 KB
testcase_26 AC 149 ms
6,144 KB
testcase_27 AC 500 ms
12,544 KB
testcase_28 AC 440 ms
12,544 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <array>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
#include <memory>
#include <regex>
using namespace std;

class BinaryIndexedTree
{
private:
    int n;
    vector<int> data;
public:
    BinaryIndexedTree(int n){ // コンストラクタ
        this->n = n;
        data.assign(n+1, 0);
    }
    void add(int k, int x){ // k番目の要素にxを加算する
        ++ k;
        while(k <= n){
            data[k] += x;
            k += k & -k;
        }
    }
    int sum(int k){ // 区間[0,k]の総和を返す
        ++ k;
        int ret = 0;
        while(k > 0){
            ret += data[k];
            k -= k & -k;
        }
        return ret;
    }
    int sum(int a, int b){ // 区間[a,b]の総和を返す
        return sum(b) - sum(a-1);
    }
    int upper_bound(int x){ // 総和が初めてxを超える位置を返す(ただし、各位置の数値が非負数であることを前提とする)
        int b = 1;
        while(b < n)
            b *= 2;
        int a = 0;
        while(b > 0){
            if(a+b <= n && x >= data[a+b]){
                x -= data[a+b];
                a += b;
            }
            b /= 2;
        }
        return (a < n)? a : -1;
    }
    int lower_bound(int x){ // 総和が初めてx以上になる位置を返す(ただし、各位置の数値が非負数であることを前提とする)
        return upper_bound(x-1);
    }
};

int main()
{
    int n;
    cin >> n;
    vector<int> x(n), l(n), r(n);
    for(int i=0; i<n; ++i)
        cin >> x[i];
    for(int i=0; i<n; ++i)
        cin >> l[i] >> r[i];

    vector<pair<int, int> > v(2*n);
    for(int i=0; i<n; ++i){
        l[i] = lower_bound(x.begin(), x.end(), x[i] - l[i]) - x.begin();
        r[i] = upper_bound(x.begin(), x.end(), x[i] + r[i]) - x.begin() - 1;
        v[i] = make_pair(i, i);
        v[n+i] = make_pair(r[i], n+i);
    }
    sort(v.begin(), v.end());

    BinaryIndexedTree bit(n);
    long long ans = 0;
    for(int i=0; i<2*n; ++i){
        int j = v[i].second;
        if(j < n){
            ans += bit.sum(l[j], j);
            bit.add(j, 1);
        }
        else{
            j -= n;
            bit.add(j, -1);
        }
    }
    cout << ans << endl;

    return 0;
}
0