Submission #520499

# Submission time Handle Problem Language Result Execution time Memory
520499 2022-01-30T07:42:08 Z blue Growing Trees (BOI11_grow) C++17
30 / 100
72 ms 5072 KB
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
 
using vi = vector<int>;
using ll = long long;
using vll = vector<ll>;
 
const int mx = 100'000;
 
const int Z = (1<<17);
 
 
vll delta(1+mx);
 
int 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(int i, int L, int 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]);
		}
	}
 
	int first_geq(int 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]);
	}
 
	int first_geq(ll s) {return first_geq(1, s);}
 
	void add(int i, int 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(int I, int V) {if(1 <= I && I <= N) add(1, I, V);}
 
	int get_sum(int i, int 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(int i)
	// {
	// 	// cerr << l[i] << ' ' << r[i] << " : " << sm[i] << '\n';
	// 	if(l[i] != r[i]) dfs(2*i), dfs(2*i+1);
	// }
};
 
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
 
	cin >> N >> M;
 
	vll init_h(1+N);
	for(int i = 1; i <= N; i++) cin >> init_h[i];
	init_h[0] = 0;
 
	sort(init_h.begin(), init_h.end());
 
	for(int 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(int q = 1; q <= M; q++)
	{
		// cerr << "query = " << q << '\n';
		char t;
		cin >> t;
 
		if(t == 'F')
		{
			ll c, h;
			cin >> c >> h;
 
			int stp = S.first_geq(h);
 
			// cerr << "stp = " << stp << '\n';
 
			if(stp >= N+1) continue;
 
			int 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';
 
			int last_start = S.first_geq(1, ep_val);
			int last_end = S.first_geq(1, ep_val+1) - 1;
 
			// cerr << "ls, le = " << last_start << ' ' << last_end << '\n';
 
 
			int 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 Incorrect 32 ms 4912 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4196 KB Output is correct
2 Correct 4 ms 4172 KB Output is correct
3 Correct 4 ms 4172 KB Output is correct
4 Correct 4 ms 4172 KB Output is correct
5 Correct 39 ms 4412 KB Output is correct
6 Correct 45 ms 4548 KB Output is correct
7 Correct 5 ms 4200 KB Output is correct
8 Correct 22 ms 4456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 4592 KB Output is correct
2 Correct 44 ms 4540 KB Output is correct
3 Correct 4 ms 4172 KB Output is correct
4 Correct 27 ms 4456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 4588 KB Output is correct
2 Correct 43 ms 4560 KB Output is correct
3 Correct 9 ms 4172 KB Output is correct
4 Correct 41 ms 4548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 72 ms 4888 KB Output is correct
2 Incorrect 33 ms 4824 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 26 ms 4812 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 25 ms 4880 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 34 ms 4912 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 30 ms 4868 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 39 ms 5072 KB Output isn't correct
2 Halted 0 ms 0 KB -