結果

問題 No.728 ギブ and テイク
ユーザー koprickykopricky
提出日時 2017-10-07 00:59:26
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 200 ms / 3,000 ms
コード長 2,139 bytes
コンパイル時間 2,210 ms
コンパイル使用メモリ 155,004 KB
実行使用メモリ 12,552 KB
最終ジャッジ日時 2023-08-10 18:27:15
合計ジャッジ時間 7,540 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 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 0 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 11 ms
4,376 KB
testcase_14 AC 18 ms
4,376 KB
testcase_15 AC 8 ms
4,380 KB
testcase_16 AC 16 ms
4,380 KB
testcase_17 AC 15 ms
4,384 KB
testcase_18 AC 140 ms
9,704 KB
testcase_19 AC 148 ms
10,220 KB
testcase_20 AC 171 ms
11,148 KB
testcase_21 AC 158 ms
10,556 KB
testcase_22 AC 61 ms
6,016 KB
testcase_23 AC 40 ms
4,992 KB
testcase_24 AC 114 ms
8,488 KB
testcase_25 AC 116 ms
8,660 KB
testcase_26 AC 59 ms
6,024 KB
testcase_27 AC 200 ms
12,552 KB
testcase_28 AC 135 ms
12,540 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 2 ms
4,380 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;
    scanf("%d",&n);
	// assert(0 <= n && n <= 300000);
    vector<int> pos(n);
    vector<P> vec(2*n);
    rep(i,n){
        scanf("%d",&pos[i]);
		// assert(0 <= pos[i] && pos[i] <= 1000000000);
		// if(i > 0){
		// 	assert(pos[i-1] < pos[i]);
		// }
        vec[i] = P(pos[i],n+i);
    }
    vector<int> l(n),r(n);
    rep(i,n){
        scanf("%d%d",&l[i],&r[i]);
		// assert(0 <= l[i] && l[i] <= 1000000000 && 0 <= r[i] && r[i] <= 1000000000);
        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