Submission #972646

# Submission time Handle Problem Language Result Execution time Memory
972646 2024-04-30T19:25:05 Z c2zi6 Growing Trees (BOI11_grow) C++14
100 / 100
175 ms 2884 KB
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define replr(i, a, b) for (int i = int(a); i <= int(b); ++i)
#define reprl(i, a, b) for (int i = int(a); i >= int(b); --i)
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define mkp(a, b) make_pair(a, b)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<PII> VPI;
typedef vector<VI> VVI;
typedef vector<VVI> VVVI;
typedef vector<VPI> VVPI;
typedef pair<ll, ll> PLL;
typedef vector<ll> VL;
typedef vector<PLL> VPL;
typedef vector<VL> VVL;
typedef vector<VVL> VVVL;
typedef vector<VPL> VVPL;
template<class T> T setmax(T& a, T b) {if (a < b) return a = b; return a;}
template<class T> T setmin(T& a, T b) {if (a < b) return a; return a = b;}
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<class T>
using indset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

VI BIT;
void add(int i, int s) {
	while (i < BIT.size()) BIT[i] += s, i += i&-i;
}
void add(int l, int r, int s) {
	add(l, s);
	if (r+1 < BIT.size()) add(r+1, -s);
}
int get(int i) {
	int ret = 0;
	while (i > 0) ret += BIT[i], i -= i&-i;
	return ret;
}

int ubound(int x) {
	int l = 0, r = BIT.size();
	while (l + 1 < r) {
		int m = (l + r) / 2;
		if (get(m) > x) r = m;
		else l = m;
	}
	return r;
}
int lbound(int x) {
	int l = 0, r = BIT.size();
	while (l + 1 < r) {
		int m = (l + r) / 2;
		if (get(m) >= x) r = m;
		else l = m;
	}
	return r;
}
int answer(int L, int R) {
	return ubound(R)-lbound(L);
}

void solve() {
	int n, q;
	cin >> n >> q;
	VI a(n);
	for (int& x : a) cin >> x;
	sort(all(a));
	BIT = VI(n+1);
	rep(i, n) add(i+1, i+1, a[i]);
	while (q--) {
		char t;
		cin >> t;
		if (t == 'C') {
			int l, r;
			cin >> l >> r;
			cout << answer(l, r) << endl;
		} else {
			int c, h;
			cin >> c >> h;
			int l = lbound(h);
			if (l+c-1 > n) add(l, n, +1);
			else {
				int r = lbound(get(l+c-1))-1;
				add(l, r, +1);
				int cnt = l+c-1-r;
				r = ubound(get(l+c-1))-1;
				l = r - cnt + 1;
				add(l, r, +1);
			}
			/* replr(i, 1, n) cout << get(i) << " "; */
			/* cout << endl; */
		}
	}
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    int t = 1;
    /* cin >> t; */
    while (t--) solve();
}

Compilation message

grow.cpp: In function 'void add(int, int)':
grow.cpp:35:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   35 |  while (i < BIT.size()) BIT[i] += s, i += i&-i;
      |         ~~^~~~~~~~~~~~
grow.cpp: In function 'void add(int, int, int)':
grow.cpp:39:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   39 |  if (r+1 < BIT.size()) add(r+1, -s);
      |      ~~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 89 ms 1172 KB Output is correct
2 Correct 127 ms 2716 KB Output is correct
3 Correct 47 ms 2680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 348 KB Output is correct
2 Correct 5 ms 480 KB Output is correct
3 Correct 6 ms 348 KB Output is correct
4 Correct 4 ms 344 KB Output is correct
5 Correct 109 ms 648 KB Output is correct
6 Correct 143 ms 1756 KB Output is correct
7 Correct 6 ms 344 KB Output is correct
8 Correct 104 ms 1172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 130 ms 812 KB Output is correct
2 Correct 133 ms 1876 KB Output is correct
3 Correct 2 ms 344 KB Output is correct
4 Correct 123 ms 1428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 134 ms 752 KB Output is correct
2 Correct 145 ms 1808 KB Output is correct
3 Correct 5 ms 600 KB Output is correct
4 Correct 139 ms 1780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 90 ms 1024 KB Output is correct
2 Correct 118 ms 2640 KB Output is correct
3 Correct 32 ms 860 KB Output is correct
4 Correct 37 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 103 ms 1108 KB Output is correct
2 Correct 124 ms 2392 KB Output is correct
3 Correct 43 ms 2640 KB Output is correct
4 Correct 33 ms 928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 89 ms 1124 KB Output is correct
2 Correct 89 ms 2384 KB Output is correct
3 Correct 51 ms 2724 KB Output is correct
4 Correct 33 ms 968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 125 ms 1216 KB Output is correct
2 Correct 122 ms 2384 KB Output is correct
3 Correct 25 ms 1880 KB Output is correct
4 Correct 94 ms 2320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 121 ms 1360 KB Output is correct
2 Correct 139 ms 2644 KB Output is correct
3 Correct 82 ms 2884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 175 ms 1616 KB Output is correct