結果

問題 No.728 ギブ and テイク
ユーザー koprickykopricky
提出日時 2018-10-09 03:06:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 117 ms / 3,000 ms
コード長 1,293 bytes
コンパイル時間 508 ms
コンパイル使用メモリ 61,372 KB
実行使用メモリ 11,904 KB
最終ジャッジ日時 2024-04-20 19:00:44
合計ジャッジ時間 2,957 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 1 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 1 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 7 ms
5,376 KB
testcase_14 AC 11 ms
5,376 KB
testcase_15 AC 5 ms
5,376 KB
testcase_16 AC 10 ms
5,376 KB
testcase_17 AC 9 ms
5,376 KB
testcase_18 AC 84 ms
9,600 KB
testcase_19 AC 87 ms
10,112 KB
testcase_20 AC 102 ms
11,008 KB
testcase_21 AC 93 ms
10,240 KB
testcase_22 AC 37 ms
6,272 KB
testcase_23 AC 23 ms
5,504 KB
testcase_24 AC 70 ms
8,448 KB
testcase_25 AC 67 ms
8,576 KB
testcase_26 AC 33 ms
6,272 KB
testcase_27 AC 117 ms
11,776 KB
testcase_28 AC 65 ms
11,904 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 1 ms
5,376 KB
testcase_32 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<string>
#include<algorithm>
#pragma GCC optimize("O3")
#pragma GCC target("avx")
#define ll long long

using namespace std;

struct P {
    int x, y;
    P(){}
    P(int a, int b) : x(a), y(b){}
    bool operator< (const P& a) const {
        return (this->x == a.x)?(this->y < a.y):(this->x < a.x);
    }
};

#define getchar getchar_unlocked

inline int in() {
    int n, c;
    while ((c = getchar()) < '0') if (c == EOF) return -1;
    n = c - '0';
    while ((c = getchar()) >= '0') n = n * 10 + c - '0';
    return n;
}

const int MAX_N = 300001;

int i,n,q;
ll ans = 0;
int bt[MAX_N], a[MAX_N], r[MAX_N];
P p[2*MAX_N];

void add(int i, int x){
    while(i < n+1) bt[i] += x, i += i & -i;
}

int sum(int i){
    int s = 0;
    while(i > 0) s += bt[i], i -= i & -i;
    return s;
}

int main()
{
    n = in();
    for(i = 0; i < n; i++){
        a[i] = in();
    }
    for(i = 0; i < n; i++){
        int l = in();
        r[i] = in();
        p[i] = P(a[i], n+i);
        p[n+i] = P(a[i]-l, i);
    }
    sort(p, p+2*n);
    for(i = 0; i < 2*n; i++){
        q = p[i].y;
        if(q >= n){
            q -= n;
            ans += sum(upper_bound(a, a+n, a[q]+r[q]) - a) - sum(q+1);
        }else{
            add(q+1,1);
        }
    }
    printf("%lld\n",ans);
    return 0;
}
0