답안 #624438

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
624438 2022-08-08T09:20:12 Z DeMen100ns Growing Trees (BOI11_grow) C++17
100 / 100
118 ms 3324 KB
//believe me, icant

#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';
		}
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 65 ms 2380 KB Output is correct
2 Correct 101 ms 2636 KB Output is correct
3 Correct 37 ms 2556 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 2 ms 344 KB Output is correct
5 Correct 47 ms 1388 KB Output is correct
6 Correct 57 ms 1740 KB Output is correct
7 Correct 4 ms 340 KB Output is correct
8 Correct 25 ms 1108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 56 ms 1568 KB Output is correct
2 Correct 56 ms 1756 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 32 ms 1272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 46 ms 1840 KB Output is correct
2 Correct 61 ms 1692 KB Output is correct
3 Correct 6 ms 468 KB Output is correct
4 Correct 58 ms 1780 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 73 ms 1892 KB Output is correct
2 Correct 99 ms 2288 KB Output is correct
3 Correct 16 ms 812 KB Output is correct
4 Correct 31 ms 2284 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 82 ms 1976 KB Output is correct
2 Correct 95 ms 2316 KB Output is correct
3 Correct 36 ms 2504 KB Output is correct
4 Correct 20 ms 852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 66 ms 2096 KB Output is correct
2 Correct 68 ms 2348 KB Output is correct
3 Correct 37 ms 2592 KB Output is correct
4 Correct 16 ms 748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 107 ms 2772 KB Output is correct
2 Correct 112 ms 2220 KB Output is correct
3 Correct 27 ms 1796 KB Output is correct
4 Correct 58 ms 2124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 85 ms 2616 KB Output is correct
2 Correct 102 ms 2636 KB Output is correct
3 Correct 118 ms 2864 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 98 ms 3324 KB Output is correct