답안 #965328

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
965328 2024-04-18T10:36:50 Z Trisanu_Das Growing Trees (BOI11_grow) C++17
100 / 100
90 ms 3412 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;            
	while (lo < hi) { 
		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) 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 53 ms 2388 KB Output is correct
2 Correct 88 ms 2664 KB Output is correct
3 Correct 30 ms 2628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 2 ms 348 KB Output is correct
3 Correct 2 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 39 ms 1524 KB Output is correct
6 Correct 44 ms 1620 KB Output is correct
7 Correct 3 ms 348 KB Output is correct
8 Correct 22 ms 1144 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 42 ms 2164 KB Output is correct
2 Correct 46 ms 1872 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 28 ms 1252 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 1872 KB Output is correct
2 Correct 50 ms 1656 KB Output is correct
3 Correct 6 ms 604 KB Output is correct
4 Correct 47 ms 1872 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 56 ms 2052 KB Output is correct
2 Correct 78 ms 2408 KB Output is correct
3 Correct 14 ms 856 KB Output is correct
4 Correct 25 ms 2384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 72 ms 2096 KB Output is correct
2 Correct 80 ms 2508 KB Output is correct
3 Correct 28 ms 2636 KB Output is correct
4 Correct 14 ms 856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 53 ms 2204 KB Output is correct
2 Correct 53 ms 2388 KB Output is correct
3 Correct 29 ms 2648 KB Output is correct
4 Correct 14 ms 860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 86 ms 2644 KB Output is correct
2 Correct 85 ms 2376 KB Output is correct
3 Correct 24 ms 1876 KB Output is correct
4 Correct 51 ms 2328 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 69 ms 2656 KB Output is correct
2 Correct 86 ms 2804 KB Output is correct
3 Correct 90 ms 3156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 87 ms 3412 KB Output is correct