답안 #421324

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
421324 2021-06-09T03:17:23 Z dongliu0426 Growing Trees (BOI11_grow) C++17
100 / 100
145 ms 3396 KB
#include <bits/stdc++.h>
using namespace std;

template<class T> struct FT {
	int n; vector<T> ft;

	void init(int n_) {
		n = n_;
		ft.assign(n_, 0);
	}
	void add(int l, int r, T x) { // add x to [l, r] 
		if (r < l)
			return;
		for ( ; l < n; l |= l + 1)
			ft[l] += x;
		for ( ++r; r < n; r |= r + 1)
			ft[r] -= x;
	}
	T query(int i) { // point query at i
		T sum = 0;
		for ( ; i >= 0; i &= i + 1, --i) sum += ft[i];
		return sum;
	}
};

#define N	100000

int n, q, a[N];
FT<int> ft;

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;
	ft.init(n);
	for (int i = 0; i < n; ++i)
		cin >> a[i];
	sort(a, a + n);
	for (int i = 0; i < n; ++i)
		ft.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 ft.query(i) >= b;
			});
			if (l == n) // found nothing
				continue;
			int r = l + a - 1;
			if (r >= n - 1) {
				ft.add(l, n - 1, 1);
				continue;
			}
			int x = ft.query(r);
			int l_ = firstTrue(l, n - 1, [&](int i) {
				return ft.query(i) >= x;
			});
			int r_ = firstTrue(l_, n - 1, [&](int i) {
				return ft.query(i) > x;
			});
			ft.add(l, l_ - 1, 1);
			int size = a - (l_ - l);
			ft.add(r_ - size, r_ - 1, 1);
		} else {
			int l = firstTrue(0, n - 1, [&](int i) {
				return ft.query(i) >= a;
			});
			int r = firstTrue(0, n - 1, [&](int i) {
				return ft.query(i) > b;
			});
			cout << r - l << '\n';
		}
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 95 ms 2372 KB Output is correct
2 Correct 125 ms 2628 KB Output is correct
3 Correct 48 ms 2628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 332 KB Output is correct
2 Correct 3 ms 328 KB Output is correct
3 Correct 3 ms 332 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
5 Correct 58 ms 1308 KB Output is correct
6 Correct 70 ms 1576 KB Output is correct
7 Correct 5 ms 332 KB Output is correct
8 Correct 34 ms 1124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 66 ms 1644 KB Output is correct
2 Correct 70 ms 1704 KB Output is correct
3 Correct 2 ms 332 KB Output is correct
4 Correct 47 ms 1216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 60 ms 1884 KB Output is correct
2 Correct 78 ms 1628 KB Output is correct
3 Correct 8 ms 460 KB Output is correct
4 Correct 70 ms 1660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 91 ms 1972 KB Output is correct
2 Correct 115 ms 2284 KB Output is correct
3 Correct 21 ms 844 KB Output is correct
4 Correct 44 ms 2152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 101 ms 1944 KB Output is correct
2 Correct 116 ms 2336 KB Output is correct
3 Correct 46 ms 2500 KB Output is correct
4 Correct 22 ms 824 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 89 ms 2108 KB Output is correct
2 Correct 84 ms 2336 KB Output is correct
3 Correct 49 ms 2644 KB Output is correct
4 Correct 24 ms 812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 145 ms 2756 KB Output is correct
2 Correct 121 ms 2176 KB Output is correct
3 Correct 33 ms 1792 KB Output is correct
4 Correct 74 ms 2212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 111 ms 2500 KB Output is correct
2 Correct 127 ms 2744 KB Output is correct
3 Correct 136 ms 2884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 124 ms 3396 KB Output is correct