Submission #880147

# Submission time Handle Problem Language Result Execution time Memory
880147 2023-11-28T20:55:06 Z OAleksa Sails (IOI07_sails) C++14
100 / 100
492 ms 16296 KB
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define f first
#define s second
const int maxn = 1e6 + 69;
int n, st[4 * maxn], lzy[4 * maxn];
pair<int, int> a[maxn];
void push(int v) {
	if (lzy[v] > 0) {
		lzy[v * 2] += lzy[v];
		lzy[v * 2 + 1] += lzy[v];
		st[v * 2] += lzy[v];
		st[v * 2 + 1] += lzy[v];
		lzy[v] = 0;
	}
}
void upd(int v, int tl, int tr, int l, int r, int val) {
	if (tl > r || tr < l)
		return;
	else if (tl >= l && tr <= r) {
		lzy[v] += val;
		st[v] += val;
	}
	else {
		push(v);
		int mid = (tl + tr) / 2;
		upd(v * 2, tl, mid, l, r, val);
		upd(v * 2 + 1, mid + 1, tr, l, r, val);
		st[v] = max(st[v * 2], st[v * 2 + 1]);
	}
}
int qry(int v, int tl, int tr, int pos) {
	if (tl == tr) 
		return st[v];
	else {
		int mid = (tl + tr) / 2;
		push(v);
		if (pos <= mid)
			return qry(v * 2, tl, mid, pos);
		else
			return qry(v * 2 + 1, mid + 1, tr, pos);
	}
}

signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
   int tt = 1;
  	//cin >> tt;
   while (tt--) {
   	cin >> n;
   	for (int i = 1;i <= n;i++) 
   		cin >> a[i].f >> a[i].s;
   	sort(a + 1, a + n + 1);
   	const int m = 1e6 + 10;
   	for (int i = 1;i <= n;i++) {
   		//range od [1, a[i]]
   		//popunjavam [a[i] - b[i] + 1, a[i]]
   		int l = a[i].f - a[i].s + 1, r = a[i].f;
			//ovde neko sranje tolko ne radi lol
			int l1 = 1, r1 = r, id1 = 0;
			int x = qry(1, 1, m, l);
			while (l1 <= r1) {
				int mid = (l1 + r1) / 2;
				if (qry(1, 1, m, mid) > x) {
					id1 = mid;
					l1 = mid + 1;
				}
				else
					r1 = mid - 1;
			}
			id1++;
			l1 = 1, r1 = r;
			int id2 = r + 1;
			while (l1 <= r1) {
				int mid = (l1 + r1) / 2;
				if (qry(1, 1, m, mid) < x) {
					id2 = mid;
					r1 = mid - 1;
				}
				else
					l1 = mid + 1;
			}
			--id2;
			//id1->prvo pojavljivanje x
			//id2->poslednje pojavljivanje x
			//cout << id1 << " " << id2 << " " << endl;
			if (id2 == r) 
				upd(1, 1, m, id1, id1 + a[i].s - 1, 1);
			else {
				upd(1, 1, m, id2 + 1, a[i].f, 1);
				a[i].s -= (a[i].f - id2);
				upd(1, 1, m, id1, id1 + a[i].s - 1, 1);
			}
   	}
		int ans = 0;
   	for (int i = 1;i <= m;i++) {
   		int x = qry(1, 1, m, i);
   		ans += x * (x - 1) / 2;
   	}
   	cout << ans;
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 86 ms 8704 KB Output is correct
2 Correct 87 ms 8696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 86 ms 8688 KB Output is correct
2 Correct 86 ms 8540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 85 ms 8536 KB Output is correct
2 Correct 95 ms 8540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 94 ms 8716 KB Output is correct
2 Correct 88 ms 6492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 91 ms 6812 KB Output is correct
2 Correct 92 ms 12460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 112 ms 7432 KB Output is correct
2 Correct 161 ms 9304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 156 ms 11344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 264 ms 11808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 385 ms 12880 KB Output is correct
2 Correct 351 ms 13392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 373 ms 16264 KB Output is correct
2 Correct 309 ms 10836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 492 ms 16296 KB Output is correct
2 Correct 338 ms 11120 KB Output is correct