Submission #421331

# Submission time Handle Problem Language Result Execution time Memory
421331 2021-06-09T04:14:27 Z dongliu0426 Growing Trees (BOI11_grow) C++17
100 / 100
142 ms 2292 KB
#include <bits/stdc++.h>
using namespace std;

#define N	100000

int n, q, a[N], bit[N];

void add(int l, int r, int x) { // add x to [l, r] 
	if (r < l) return;
	for ( ; l < n; l |= l + 1) bit[l] += x;
	for ( ++r; r < n; r |= r + 1) bit[r] -= x;
}
int query(int i) { // point query at i
	int sum = 0;
	for ( ; i >= 0; i &= i + 1, --i) sum += bit[i];
	return sum;
}
/* from Benq */
template<class T, class U> T firstTrue(T lo, T hi, U f) {
	assert(lo <= hi); ++hi; // assuming f is increasing
	while (lo < hi) { // find first index such that f is true 
		T mid = lo + (hi - lo) / 2;
		f(mid) ? hi = mid : lo = mid + 1; 
	} 
	return lo;
}

int main() {
	ios::sync_with_stdio(false); cin.tie(NULL);

	cin >> n >> q;
	for (int i = 0; i < n; ++i)
		cin >> a[i];
	sort(a, a + n);
	for (int i = 0; i < n; ++i)
		add(i, i, a[i]);
	for (int i = 0; i < q; ++i) {
		char c; int a, b;

		cin >> c >> a >> b;
		if (c == 'F') {
			int l = firstTrue(0, n - 1, [&](int i) {
				return query(i) >= b;
			});
			if (l == n) // found nothing
				continue;
			int r = l + a - 1;
			if (r >= n - 1) {
				add(l, n - 1, 1);
				continue;
			}
			int x = query(r);
			int l_ = firstTrue(l, n - 1, [&](int i) {
				return query(i) >= x;
			});
			int r_ = firstTrue(l_, n - 1, [&](int i) {
				return query(i) > x;
			}) - 1;
			add(l, l_ - 1, 1);
			add(r_ - (a - (l_ - l)) + 1, r_, 1);
		} else {
			int l = firstTrue(0, n - 1, [&](int i) {
				return query(i) >= a;
			});
			int r = firstTrue(0, n - 1, [&](int i) {
				return query(i) > b;
			});
			cout << r - l << '\n';
		}
	}
}
# Verdict Execution time Memory Grader output
1 Correct 83 ms 1912 KB Output is correct
2 Correct 136 ms 1900 KB Output is correct
3 Correct 49 ms 1612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 332 KB Output is correct
2 Correct 3 ms 332 KB Output is correct
3 Correct 3 ms 332 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
5 Correct 69 ms 1252 KB Output is correct
6 Correct 69 ms 1352 KB Output is correct
7 Correct 5 ms 332 KB Output is correct
8 Correct 36 ms 1096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 69 ms 1448 KB Output is correct
2 Correct 81 ms 1396 KB Output is correct
3 Correct 2 ms 332 KB Output is correct
4 Correct 43 ms 1208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 59 ms 1552 KB Output is correct
2 Correct 81 ms 1424 KB Output is correct
3 Correct 9 ms 460 KB Output is correct
4 Correct 70 ms 1380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 88 ms 1772 KB Output is correct
2 Correct 113 ms 1728 KB Output is correct
3 Correct 25 ms 844 KB Output is correct
4 Correct 43 ms 1612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 100 ms 1772 KB Output is correct
2 Correct 116 ms 1792 KB Output is correct
3 Correct 46 ms 1568 KB Output is correct
4 Correct 21 ms 828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 90 ms 1848 KB Output is correct
2 Correct 83 ms 1984 KB Output is correct
3 Correct 48 ms 1700 KB Output is correct
4 Correct 24 ms 844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 126 ms 1844 KB Output is correct
2 Correct 120 ms 1836 KB Output is correct
3 Correct 33 ms 1740 KB Output is correct
4 Correct 79 ms 1788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 108 ms 2024 KB Output is correct
2 Correct 137 ms 1860 KB Output is correct
3 Correct 142 ms 1876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 121 ms 2292 KB Output is correct