結果

問題 No.728 ギブ and テイク
ユーザー azbrugbyazbrugby
提出日時 2019-02-23 19:26:38
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 448 ms / 3,000 ms
コード長 1,606 bytes
コンパイル時間 783 ms
コンパイル使用メモリ 81,144 KB
実行使用メモリ 20,552 KB
最終ジャッジ日時 2024-05-08 06:20:43
合計ジャッジ時間 7,337 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 2 ms
5,376 KB
testcase_04 AC 2 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 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 35 ms
5,376 KB
testcase_15 AC 16 ms
5,376 KB
testcase_16 AC 31 ms
5,376 KB
testcase_17 AC 29 ms
5,376 KB
testcase_18 AC 311 ms
11,580 KB
testcase_19 AC 333 ms
11,988 KB
testcase_20 AC 392 ms
13,504 KB
testcase_21 AC 349 ms
12,536 KB
testcase_22 AC 140 ms
6,980 KB
testcase_23 AC 90 ms
5,768 KB
testcase_24 AC 258 ms
10,144 KB
testcase_25 AC 260 ms
10,132 KB
testcase_26 AC 133 ms
6,900 KB
testcase_27 AC 448 ms
15,996 KB
testcase_28 AC 406 ms
20,552 KB
testcase_29 AC 2 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
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <cstring>
#include <sstream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#include <stack>
#include <cstdio>
#include <cmath>
#define mk make_pair
#define pb push_back
#define printf printf_s
#define scanf scanf_s
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> pos;
const ll MOD = 1000000007, N = 300010, dx[4] = { -1,1,0,0 }, dy[4] = { 0,0,-1,1 }, MIN = -72340172838076673, MAX = 72340172838076673;

ll mn(ll a, ll b) {
	if (a == -1)return b;
	if (b == -1)return a;
	return min(a, b);
}
ll gcd(ll v1, ll v2) {
	if (v1 == 0)return v2; if (v2 == 0)return v1; if (v2 > v1)return gcd(v2%v1, v1); return gcd(v1%v2, v2);
}

ll pw(ll v1, ll v2) {
	ll v3 = 1;
	while (v2 > 0) {
		if (v2 % 2)v3 = (v3*v1) % MOD;
		v1 = (v1*v1) % MOD;
		v2 /= 2;
	}
	return v3;
}

struct ab {
	ll a, b;
	bool operator<(const ab& right) const {
		return right.b*a - right.a*b < 0;
	}
};

ll n, a[N], bt[N],ans=0;
pos lr[N];

void ad(ll v1, ll v2) {
	while (v1 <= n) {
		bt[v1] += v2;
		v1 += v1 & -v1;
	}
}

ll sm(ll v1) {
	ll v2 = 0;
	while (v1 > 0) {
		v2 += bt[v1];
		v1 -= v1 & -v1;
	}
	return v2;
}

int main() {
	cin >> n;
	for (int i = 1; i <= n; i++)cin >> a[i];
	for (int i = 1; i <= n; i++)cin >> lr[i].first >> lr[i].second;
	priority_queue<pos> q;
	for (int i = n; i >= 1; i--) {
		while (!q.empty() && q.top().first > a[i]) {
			ad(q.top().second, -1);
			q.pop();
		}
		ans += sm(upper_bound(a + 1, a + n + 1, a[i] + lr[i].second) - a - 1);
		q.push(mk(a[i] - lr[i].first, i));
		ad(i, 1);
	}
	cout << ans << endl;
	return 0;
}
0