Submission #550809

# Submission time Handle Problem Language Result Execution time Memory
550809 2022-04-19T09:05:54 Z Jomnoi Sails (IOI07_sails) C++17
100 / 100
87 ms 2172 KB
#include <bits/stdc++.h>
#define DEBUG false
using namespace std;

const int MAX_N = 1e5 + 10;

namespace fw {

int fenwick[MAX_N];

void add(const int &idx, const int &v) {
	for(int i = idx; i < MAX_N; i += (i & -i)) {
		fenwick[i] += v;
	}
}

int get(const int &idx) {
	int res = 0;
	for(int i = idx; i >= 1; i -= (i & -i)) {
		res += fenwick[i];
	}
	return res;
}

}

int main() {
	cin.tie(nullptr)->sync_with_stdio(false);
	int N;
	cin >> N;
	vector <pair <int, int>> sail(N + 1);
	for(int i = 1; i <= N; i++) {
		int h, k;
		cin >> h >> k;
		sail[i] = make_pair(h, k);
	}

	sort(sail.begin() + 1, sail.end());

	for(int i = 1; i <= N; i++) {
		auto [h, k] = sail[i];
		int now = fw::get(h - k + 1), l, r, lm, rm;

		l = 1, r = h - k + 1, lm = 1;
		while(l <= r) {
			int mid = (l + r) / 2;

			if(fw::get(mid) == now) {
				r = mid - 1;
				lm = mid;
			}
			else {
				l = mid + 1;
			}
		}

		l = h - k + 1, r = h, rm = h;
		while(l <= r) {
			int mid = (l + r) / 2;

			if(fw::get(mid) == now) {
				l = mid + 1;
				rm = mid;
			}
			else {
				r = mid - 1;
			}
		}

		fw::add(rm + 1, 1);
		fw::add(h + 1, -1);
		fw::add(lm, 1);
		fw::add(lm + k - (h - rm), -1);
	}

	long long ans = 0;
	for(int i = 1; i < MAX_N; i++) {
		long long cnt = fw::get(i);
		ans += cnt * (cnt - 1) / 2;
	}
	cout << ans;
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 348 KB Output is correct
2 Correct 2 ms 244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 2 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 508 KB Output is correct
2 Correct 19 ms 848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 68 ms 1192 KB Output is correct
2 Correct 72 ms 2172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 67 ms 1368 KB Output is correct
2 Correct 48 ms 2000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 87 ms 1432 KB Output is correct
2 Correct 59 ms 2152 KB Output is correct