Submission #447816

# Submission time Handle Problem Language Result Execution time Memory
447816 2021-07-27T15:04:23 Z gromperen Sails (IOI07_sails) C++14
100 / 100
116 ms 2788 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define int long long

const int MAXN = 1e5+5;

int n, fen[MAXN+5];

void update(int i, ll val) {
	for (; i > 0; i -= i & (-i)) fen[i-1] += val;
}

int query(int i) {
	ll s = 0;
	for (; i < MAXN; i += i & (-i)) s += fen[i-1];
	return s;
}

int getft(int x) {
	int l = 0, r = MAXN;
	while (l < r) {
		int m = (r + l + 1) / 2;
		if (query(m) >= x) {
			l = m;
		} else {
			r = m - 1;
		}
	}
	return l;
}

signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);

	cin >> n;
	vector<pair<ll,ll>>a(n);
	for (int i= 0; i < n; ++i) cin >> a[i].first >> a[i].second;
	sort(a.begin(),a.end());

	for (auto IT : a) {
		int h, k; tie(h, k) = IT;
		int p = h - k;
		update(h, 1);
		if (p == 0) continue;
		int lb = min(getft(query(p)), h), rb = getft(query(p) + 1);
		update(lb, -1), update(rb, -1);
		update(rb + k - (h - lb), 1);
	}
	ll ans = 0;
	for (int i = 1; i < MAXN; ++i) {
		ans += 1LL * query(i) * (query(i)-1) / 2;
	}
	cout << ans << "\n";

	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 204 KB Output is correct
2 Correct 3 ms 316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 204 KB Output is correct
2 Correct 3 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 204 KB Output is correct
2 Correct 4 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 204 KB Output is correct
2 Correct 4 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 332 KB Output is correct
2 Correct 6 ms 1116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 636 KB Output is correct
2 Correct 38 ms 816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 66 ms 1412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 84 ms 2212 KB Output is correct
2 Correct 88 ms 2616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 101 ms 2500 KB Output is correct
2 Correct 86 ms 2788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 116 ms 2592 KB Output is correct
2 Correct 103 ms 2396 KB Output is correct