結果

問題 No.728 ギブ and テイク
ユーザー koprickykopricky
提出日時 2017-10-07 01:10:57
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 465 ms / 3,000 ms
コード長 1,890 bytes
コンパイル時間 1,816 ms
コンパイル使用メモリ 169,596 KB
実行使用メモリ 12,488 KB
最終ジャッジ日時 2024-04-28 11:54:31
合計ジャッジ時間 6,774 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 1 ms
5,376 KB
testcase_07 AC 1 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 22 ms
5,376 KB
testcase_14 AC 38 ms
5,376 KB
testcase_15 AC 17 ms
5,376 KB
testcase_16 AC 33 ms
5,376 KB
testcase_17 AC 30 ms
5,376 KB
testcase_18 AC 325 ms
9,820 KB
testcase_19 AC 346 ms
10,240 KB
testcase_20 AC 403 ms
11,460 KB
testcase_21 AC 361 ms
10,692 KB
testcase_22 AC 145 ms
6,144 KB
testcase_23 AC 93 ms
5,376 KB
testcase_24 AC 270 ms
8,576 KB
testcase_25 AC 266 ms
8,704 KB
testcase_26 AC 139 ms
6,144 KB
testcase_27 AC 465 ms
12,428 KB
testcase_28 AC 419 ms
12,488 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 <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define each(a,b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout<<" "<<kbrni;cout<<endl
#define smap(m) cout<<#m<<":";each(kbrni,m)cout<<" {"<<kbrni.first<<":"<<kbrni.second<<"}";cout<<endl

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;
	cin >> n;
    vector<int> pos(n);
    vector<P> vec(2*n);
    rep(i,n){
		cin >> pos[i];
        vec[i] = P(pos[i],n+i);
    }
    vector<int> l(n),r(n);
    rep(i,n){
        cin >> l[i] >> r[i];
        vec[n+i] = P(pos[i]-l[i],i);
    }
    sort(all(vec));
    BIT<int> bt(n+5);
    ll ans = 0;
    rep(i,2*n){
        int p = vec[i].fi, q = vec[i].se;
        if(q >= n){
            q -= n;
            int id = upper_bound(all(pos),pos[q]+r[q]) - pos.begin();
            ans += bt.sum(id-1) - bt.sum(q);
        }else{
            bt.add(q,1);
        }
    }
    cout << ans << endl;
    return 0;
}
0