답안 #674064

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
674064 2022-12-22T20:26:20 Z kirakaminski968 Growing Trees (BOI11_grow) C++17
100 / 100
230 ms 3312 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) { 
	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) {
	int sum = 0;
	for ( ; i >= 0; i &= i + 1, --i) sum += bit[i];
	return sum;
}
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) //points to the end 
				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 << endl;
		}
	}
    return 0; 
}
# 결과 실행 시간 메모리 Grader output
1 Correct 116 ms 2260 KB Output is correct
2 Correct 150 ms 2720 KB Output is correct
3 Correct 34 ms 2624 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 6 ms 340 KB Output is correct
3 Correct 6 ms 468 KB Output is correct
4 Correct 4 ms 340 KB Output is correct
5 Correct 121 ms 1356 KB Output is correct
6 Correct 159 ms 1680 KB Output is correct
7 Correct 6 ms 340 KB Output is correct
8 Correct 113 ms 1128 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 146 ms 1632 KB Output is correct
2 Correct 141 ms 1764 KB Output is correct
3 Correct 2 ms 340 KB Output is correct
4 Correct 130 ms 1264 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 146 ms 1804 KB Output is correct
2 Correct 158 ms 1612 KB Output is correct
3 Correct 6 ms 468 KB Output is correct
4 Correct 153 ms 1752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 138 ms 1980 KB Output is correct
2 Correct 136 ms 2168 KB Output is correct
3 Correct 36 ms 844 KB Output is correct
4 Correct 30 ms 2272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 122 ms 2064 KB Output is correct
2 Correct 145 ms 2384 KB Output is correct
3 Correct 34 ms 2508 KB Output is correct
4 Correct 36 ms 852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 129 ms 2144 KB Output is correct
2 Correct 116 ms 2436 KB Output is correct
3 Correct 36 ms 2600 KB Output is correct
4 Correct 35 ms 816 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 162 ms 2720 KB Output is correct
2 Correct 156 ms 2368 KB Output is correct
3 Correct 29 ms 1796 KB Output is correct
4 Correct 101 ms 2184 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 151 ms 2464 KB Output is correct
2 Correct 151 ms 2844 KB Output is correct
3 Correct 108 ms 2876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 230 ms 3312 KB Output is correct