Submission #927940

# Submission time Handle Problem Language Result Execution time Memory
927940 2024-02-15T14:24:37 Z TAhmed33 Sails (IOI07_sails) C++
100 / 100
659 ms 6364 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mid ((l + r) >> 1)
#define tl (node + 1)
#define tr (node + 2 * (mid - l + 1))
struct pp {
	int sum = 0;
	int add = 0;
};
struct SegmentTree {
	pp tree[200001] = {};
	void prop (int l, int r, int node) {
		if (l == r) {
			tree[node].sum += tree[node].add;
			tree[node].add = 0;
			return;
		}
		tree[node].sum += (r - l + 1) * tree[node].add;
		tree[tl].add += tree[node].add;
		tree[tr].add += tree[node].add;
		tree[node].add = 0;
	}	
	void update (int l, int r, int a, int b, int c, int node) {
		prop(l, r, node);
		if (l >= a && r <= b) {
			tree[node].add += c;
			prop(l, r, node);
			return;
		}
		if (l > b || r < a) return;
		update(l, mid, a, b, c, tl);
		update(mid + 1, r, a, b, c, tr);
		tree[node].sum = tree[tl].sum + tree[tr].sum;
	}
	int get (int l, int r, int a, int b, int node) {
		prop(l, r, node);
		if (l > b || r < a) return 0;
		if (l >= a && r <= b) return tree[node].sum;
		return get(l, mid, a, b, tl) + get(mid + 1, r, a, b, tr);
	}
};
SegmentTree cur;
const int MAXH = 100000;
signed main () {
	int n;
	cin >> n;
	vector <pair <int, int>> arr;
	for (int i = 0; i < n; i++) {
		int x, y; cin >> x >> y;
		arr.push_back({x, y});
	}
	sort(arr.begin(), arr.end());
	for (auto [x, y] : arr) {
		int u = cur.get(1, MAXH, x - y + 1, x - y + 1, 1);
		int l = 1, r = x, ans1 = -1, ans2 = x + 1;
		while (l <= r) {
			if (cur.get(1, MAXH, mid, mid, 1) <= u) {
				ans1 = mid; r = mid - 1;
			} else {
				l = mid + 1;
			}
		}
		l = 1, r = x;
		while (l <= r) {
			if (cur.get(1, MAXH, mid, mid, 1) >= u) {
				l = mid + 1;
			} else {
				ans2 = mid; r = mid - 1;
			}
		}
		if (ans2 != x + 1) cur.update(1, MAXH, ans2, x, 1, 1);
		int z = y - (x - ans2 + 1);
		cur.update(1, MAXH, ans1, ans1 + z - 1, 1, 1);
	}
	int sum = 0;
	for (int i = 1; i <= arr.back().first; i++) {
		int z = cur.get(1, MAXH, i, i, 1);
		sum += z * (z - 1) / 2;
	}
	cout << sum << '\n';
}

Compilation message

sails.cpp: In function 'int main()':
sails.cpp:54:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   54 |  for (auto [x, y] : arr) {
      |            ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 3 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 2652 KB Output is correct
2 Correct 22 ms 3448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 2908 KB Output is correct
2 Correct 128 ms 3508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 137 ms 3528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 300 ms 4040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 492 ms 5688 KB Output is correct
2 Correct 477 ms 5824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 527 ms 6008 KB Output is correct
2 Correct 453 ms 5524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 659 ms 6364 KB Output is correct
2 Correct 476 ms 5412 KB Output is correct