結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 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 2 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 1 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 11 ms
5,376 KB
testcase_14 AC 18 ms
5,376 KB
testcase_15 AC 8 ms
5,376 KB
testcase_16 AC 16 ms
5,376 KB
testcase_17 AC 14 ms
5,376 KB
testcase_18 AC 132 ms
9,984 KB
testcase_19 AC 142 ms
10,228 KB
testcase_20 AC 169 ms
11,568 KB
testcase_21 AC 146 ms
10,624 KB
testcase_22 AC 58 ms
6,272 KB
testcase_23 AC 37 ms
5,376 KB
testcase_24 AC 111 ms
8,832 KB
testcase_25 AC 112 ms
8,704 KB
testcase_26 AC 57 ms
6,272 KB
testcase_27 AC 195 ms
12,544 KB
testcase_28 AC 145 ms
12,672 KB
testcase_29 AC 1 ms
5,376 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:38:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   38 |     scanf("%d",&n);
      |     ~~~~~^~~~~~~~~
main.cpp:42:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   42 |         scanf("%d",&pos[i]);
      |         ~~~~~^~~~~~~~~~~~~~
main.cpp:47:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   47 |         scanf("%d%d",&l[i],&r[i]);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~

ソースコード

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);
    vector<int> pos(n);
    vector<P> vec(2*n);
    rep(i,n){
        scanf("%d",&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]);
        vec[n+i] = P(pos[i]-l[i],i);
    }
    sort(all(vec));
    BIT<int> bt(n+1);
    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