結果

問題 No.728 ギブ and テイク
ユーザー koprickykopricky
提出日時 2018-03-01 14:36:38
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 203 ms / 3,000 ms
コード長 1,309 bytes
コンパイル時間 1,746 ms
コンパイル使用メモリ 176,384 KB
実行使用メモリ 12,544 KB
最終ジャッジ日時 2023-08-26 05:50:55
合計ジャッジ時間 6,445 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,384 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 11 ms
4,384 KB
testcase_14 AC 18 ms
4,376 KB
testcase_15 AC 8 ms
4,376 KB
testcase_16 AC 16 ms
4,380 KB
testcase_17 AC 15 ms
4,376 KB
testcase_18 AC 143 ms
9,652 KB
testcase_19 AC 152 ms
10,088 KB
testcase_20 AC 177 ms
11,128 KB
testcase_21 AC 159 ms
10,324 KB
testcase_22 AC 64 ms
5,924 KB
testcase_23 AC 41 ms
5,052 KB
testcase_24 AC 118 ms
8,656 KB
testcase_25 AC 117 ms
8,660 KB
testcase_26 AC 60 ms
5,924 KB
testcase_27 AC 203 ms
12,444 KB
testcase_28 AC 145 ms
12,544 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 2 ms
4,384 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define ll long long
#define rep(i,n) for(int i=0;i<(int)n;++i)

using namespace std;

typedef pair<int,int>P;

template<class V> class BIT {
private:
	int n; vector<V> bit;
public:
	void add(int i,V x){ i++; while(i < n) bit[i] += x, i += i & -i;}
	V sum(int i){i++; V s = 0; while(i>0) s += bit[i], i -= i & -i; return s;}
	BIT(int sz){ n = sz + 1, bit.resize(n,0);} //初期値がすべて0の場合
	BIT(vector<V> v){n = (int)v.size()+1; bit.resize(n); rep(i,n) add(i,v[i]);}
	void print(){ rep(i,n-1)cout<<sum(i)-sum(i-1)<< " ";cout<<endl;}
	void print_sum(){ rep(i,n)cout<<sum(i-1)<<" ";cout<<endl;}	//-1スタート
};

int main()
{
    int n;
    scanf("%d",&n);
    vector<int> a(n);
    vector<P> vec(2*n);
    rep(i,n){
        scanf("%d",&a[i]);
        vec[i] = P(a[i],n+i);
    }
    vector<int> l(n),r(n);
    rep(i,n){
        scanf("%d%d",&l[i],&r[i]);
        vec[n+i] = P(a[i]-l[i],i);
    }
    sort(vec.begin(),vec.end());
    BIT<int> bt(n);
    ll ans = 0;
    rep(i,2*n){
        int q = vec[i].second;
        if(q >= n){
            q -= n;
            int id = upper_bound(a.begin(),a.end(),a[q]+r[q]) - a.begin();
            ans += bt.sum(id-1) - bt.sum(q);
        }else{
            bt.add(q,1);
        }
    }
    cout << ans << endl;
    return 0;
}
0