답안 #520475

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
520475 2022-01-30T06:52:43 Z blue Growing Trees (BOI11_grow) C++17
30 / 100
1000 ms 5636 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) {/*cerr << "add " << V << " to " << 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++)
	{
		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 << "\n\n\n";
			S.dfs(1);
			// cerr << "\n\n\n";







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



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






			// 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';
		}
	}

}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 26 ms 4940 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4172 KB Output is correct
2 Correct 3 ms 4172 KB Output is correct
3 Correct 3 ms 4244 KB Output is correct
4 Correct 3 ms 4136 KB Output is correct
5 Correct 122 ms 5260 KB Output is correct
6 Correct 120 ms 5468 KB Output is correct
7 Correct 42 ms 4324 KB Output is correct
8 Correct 30 ms 4928 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 175 ms 4532 KB Output is correct
2 Correct 174 ms 5636 KB Output is correct
3 Correct 14 ms 4176 KB Output is correct
4 Correct 48 ms 5124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 177 ms 4580 KB Output is correct
2 Correct 190 ms 5476 KB Output is correct
3 Correct 154 ms 4352 KB Output is correct
4 Correct 148 ms 5604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1083 ms 4704 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 24 ms 4812 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 33 ms 4880 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 40 ms 4884 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 41 ms 4948 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 42 ms 5080 KB Output isn't correct
2 Halted 0 ms 0 KB -