Submission #520485

# Submission time Handle Problem Language Result Execution time Memory
520485 2022-01-30T07:08:09 Z blue Growing Trees (BOI11_grow) C++17
30 / 100
67 ms 12052 KB
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

using ll = long long;
using vi = vector<ll>;
using vll = vector<ll>;

const ll mx = 100'000;

const ll Z = (1<<17);


vll delta(1+mx);

ll N, M;


struct segtree
{
	vi l = vi(1+Z);
	vi r = vi(1+Z);
	vll sm = vll(1+Z);
	vll mxs = vll(1+Z);

	segtree()
	{
		;
	}

	void build(ll i, ll L, ll R)
	{
		l[i] = L;
		r[i] = R;
		if(l[i] == r[i])
		{
			sm[i] = delta[l[i]];
			mxs[i] = sm[i];
		}
		else
		{
			build(2*i, L, (L+R)/2);
			build(2*i+1, (L+R)/2+1, R);
			sm[i] = sm[2*i] + sm[2*i+1];
			mxs[i] = max(mxs[2*i], sm[2*i] + mxs[2*i+1]);
		}
	}

	ll first_geq(ll i, ll s)
	{
		// cerr << i << ' ' << l[i] << ' ' << r[i] << " : " << sm[i] << ' ' << s << '\n';
		if(mxs[i] < s) return N+1;
		else if(l[i] == r[i]) return l[i];
		else if(mxs[i<<1] >= s) return first_geq(i<<1, s);
		else return first_geq((i<<1) + 1, s - sm[i<<1]);
	}

	ll first_geq(ll s) {return first_geq(1, s);}

	void add(ll i, ll I, ll V)
	{
		if(l[i] != r[i])
		{
			if(I <= (l[i] + r[i])/2) add(2*i, I, V);
			else add(2*i+1, I, V);

			sm[i] = sm[2*i] + sm[2*i+1];
			mxs[i] = max(mxs[2*i], sm[2*i] + mxs[2*i+1]);
		}
		else
		{
			sm[i] += V;
			mxs[i] += V;
		}
	}

	void add(ll I, ll V) {if(1 <= I && I <= N) add(1, I, V);}

	ll get_sum(ll i, ll I)
	{
		if(l[i] == r[i]) return sm[i];
		else if(I <= (l[i] + r[i])/2) return get_sum(i<<1, I);
		else return sm[(i<<1)] + get_sum((i<<1) + 1, I);
	}

	// void dfs(ll i)
	// {
	// 	// cerr << l[i] << ' ' << r[i] << " : " << sm[i] << '\n';
	// 	if(l[i] != r[i]) dfs(2*i), dfs(2*i+1);
	// }
};

signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	cin >> N >> M;

	vll init_h(1+N);
	for(ll i = 1; i <= N; i++) cin >> init_h[i];
	init_h[0] = 0;

	sort(init_h.begin(), init_h.end());

	for(ll i = 1; i <= N; i++) delta[i] = init_h[i] - init_h[i-1];

	segtree S;
	S.build(1, 1, N+1);

	// cerr << "\n\n\n";
	// S.dfs(1);

	for(ll q = 1; q <= M; q++)
	{
		// cerr << "query = " << q << '\n';
		char t;
		cin >> t;

		if(t == 'F')
		{
			ll c, h;
			cin >> c >> h;

			ll stp = S.first_geq(h);

			// cerr << "stp = " << stp << '\n';

			if(stp >= N+1) continue;

			ll ep = min(stp + c - 1, (ll)N);

			// cerr << "ep = " << ep << '\n';


			ll ep_val = S.get_sum(1, ep);

			// cerr << "ep_val = " << ep_val << '\n';

			ll last_start = S.first_geq(1, ep_val);
			ll last_end = S.first_geq(1, ep_val+1) - 1;

			// cerr << "ls, le = " << last_start << ' ' << last_end << '\n';


			ll last_count = ep - last_start + 1;

			// cerr << "lc = " << last_count << '\n';

			S.add(last_end - last_count + 1, +1);


			// cerr << "done\n";




			S.add(last_end+1, -1);






			// cerr << last_end - last_count + 1 << " [) " << last_end+1 << '\n';

			S.add(stp, +1);
			S.add(last_start, -1);

			// cerr << stp << " : " << last_start << '\n';



			// cerr << S.first_geq(2) << '\n';
		}
		else
		{
			ll mn, mx;
			cin >> mn >> mx;

			cout << S.first_geq(1, mx+1) - S.first_geq(1, mn) << '\n';
		}
	}

}
# Verdict Execution time Memory Grader output
1 Runtime error 19 ms 11844 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5216 KB Output is correct
2 Correct 3 ms 5212 KB Output is correct
3 Correct 4 ms 5196 KB Output is correct
4 Correct 3 ms 5196 KB Output is correct
5 Correct 35 ms 5540 KB Output is correct
6 Correct 41 ms 5692 KB Output is correct
7 Correct 6 ms 5324 KB Output is correct
8 Correct 22 ms 5580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 5744 KB Output is correct
2 Correct 47 ms 5708 KB Output is correct
3 Correct 3 ms 5196 KB Output is correct
4 Correct 26 ms 5592 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 5756 KB Output is correct
2 Correct 51 ms 5652 KB Output is correct
3 Correct 10 ms 5324 KB Output is correct
4 Correct 43 ms 5700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 67 ms 5980 KB Output is correct
2 Runtime error 17 ms 11584 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 16 ms 11620 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 16 ms 11688 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 20 ms 11876 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 20 ms 11892 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 26 ms 12052 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -