Submission #834819

# Submission time Handle Problem Language Result Execution time Memory
834819 2023-08-22T20:23:40 Z mat_jur Growing Trees (BOI11_grow) C++17
100 / 100
199 ms 4632 KB
#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
auto&operator<<(auto &o, pair<auto, auto> p) {o << "(" << p.first << "," << p.second << ")"; return o;}
auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";for(auto e:x)o<< e << ", ";return o<<"}";}
#define debug(X) cerr << "["#X"]:" << X << '\n';
#else 
#define debug(X) ;
#endif
#define ll long long
#define all(x) (x).begin(), (x).end()
//#define cerr if(0)cout

int main () {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	int n, m;
	cin >> n >> m;
	vector<int> a(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];	
	}
	sort(all(a));
	int base = 1;
	while (base < n) base *= 2;
	struct Node {
		int mx, lazy;
		Node() {
			mx = lazy = 0;
		}
	};
	vector<Node> tree(2*base);
	for (int i = 0; i < n; i++) {
		tree[base+i].mx = a[i];
	}
	for (int i = base-1; i >= 1; i--) {
		tree[i].mx = max(tree[2*i].mx, tree[2*i+1].mx);
	}
	auto push_to_sons = [&](int v) {
		if (tree[v].lazy == 0) return;
		int x = tree[v].lazy;
		tree[2*v].mx += x;
		tree[2*v+1].mx += x;
		tree[2*v].lazy += x;
		tree[2*v+1].lazy += x;
		tree[v].lazy = 0;
	};
	function<void(int, int, int, int, int, int)> update = [&](int l, int r, int x, int v, int a, int b) {
		if (r < a || b < l) return;
		if (l <= a && b <= r) {
			tree[v].mx += x;
			tree[v].lazy += x;
			return;
		}
		push_to_sons(v);
		int mid = (a+b)/2;
		update(l, r, x, 2*v, a, mid); update(l, r, x, 2*v+1, mid+1, b);
		tree[v].mx = max(tree[2*v].mx, tree[2*v+1].mx);
	};
	function<int(int, int, int, int)> find = [&](int x, int v, int a, int b) {
		if (tree[v].mx < x) return -1;
		if (a == b) return a;
		push_to_sons(v);
		int mid = (a+b)/2;
		int y = find(x, 2*v, a, mid);
		if (y != -1) return y;
		return find(x, 2*v+1, mid+1, b);
	};
	function<int(int, int, int, int)> find2 = [&](int x, int v, int a, int b) {
		if (tree[v].mx <= x) return -1;
		if (a == b) return a;
		push_to_sons(v);
		int mid = (a+b)/2;
		int y = find2(x, 2*v, a, mid);
		if (y != -1) return y;
		return find2(x, 2*v+1, mid+1, b);
	};
	function<int(int, int, int, int)> val = [&](int x, int v, int a, int b) {
		if (a == b) return tree[v].mx;
		push_to_sons(v);
		int mid = (a+b)/2;
		if (x <= mid) return val(x, 2*v, a, mid);
		else return val(x, 2*v+1, mid+1, b);
	};
	auto print = [&]() {
		for (int i = 1; i < 2*base; i++) {
			if (i < base) push_to_sons(i);
			cerr << "(" << tree[i].mx << ", " << tree[i].lazy << ") ";
		}
		cerr << '\n';
		return;
	};
	while (m--) { 
		char c;
		cin >> c;
		if (c == 'F') {
			int c, h;
			cin >> c >> h;
			int idx = find(h, 1, 0, base-1);
//			cerr << "FFF " << idx << '\n';
			if (idx == -1) continue;
			int end = min(n-1, idx+c-1);
			int fi = find(val(end, 1, 0, base-1), 1, 0, base-1);
			int last = find2(val(end, 1, 0, base-1), 1, 0, base-1)-1;
			if (last == -2) last = n-1;
//			cerr << "val: " << val(end, 1, 0, base-1) << '\n';
//			cerr << "fi: " << fi << '\n';
//			cerr << "last: " << last << '\n';
			if (idx != fi) {
				update(idx, fi-1, 1, 1, 0, base-1);
				update(max(fi, last-(c-(fi-idx))+1), last, 1, 1, 0, base-1);
			}
			else {
				update(max(fi, last-c+1), last, 1, 1, 0, base-1);
			}
//			print();
		}
		else {
			int minn, maxx;
			cin >> minn >> maxx;
			int low = find(minn, 1, 0, base-1), up = find2(maxx, 1, 0, base-1);
//			cerr << low << ' ' << up << '\n';
			if (low == -1) {cout << 0 << '\n'; continue;}
			if (up == -1) {cout << n-low << '\n'; continue;}
			up--;
			cout << up-low+1 << '\n';
		}
	}
	
	return 0;
}

Compilation message

grow.cpp: In function 'int main()':
grow.cpp:87:7: warning: variable 'print' set but not used [-Wunused-but-set-variable]
   87 |  auto print = [&]() {
      |       ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 82 ms 2888 KB Output is correct
2 Correct 137 ms 4484 KB Output is correct
3 Correct 115 ms 4344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 2 ms 340 KB Output is correct
3 Correct 2 ms 364 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 56 ms 684 KB Output is correct
6 Correct 55 ms 788 KB Output is correct
7 Correct 6 ms 468 KB Output is correct
8 Correct 25 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 47 ms 752 KB Output is correct
2 Correct 57 ms 752 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 33 ms 780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 900 KB Output is correct
2 Correct 60 ms 788 KB Output is correct
3 Correct 11 ms 468 KB Output is correct
4 Correct 53 ms 772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 80 ms 1744 KB Output is correct
2 Correct 129 ms 4052 KB Output is correct
3 Correct 15 ms 1300 KB Output is correct
4 Correct 87 ms 4176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 115 ms 2764 KB Output is correct
2 Correct 121 ms 4044 KB Output is correct
3 Correct 114 ms 4296 KB Output is correct
4 Correct 15 ms 1236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 92 ms 2920 KB Output is correct
2 Correct 83 ms 3972 KB Output is correct
3 Correct 114 ms 4328 KB Output is correct
4 Correct 15 ms 1236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 138 ms 2764 KB Output is correct
2 Correct 129 ms 4044 KB Output is correct
3 Correct 28 ms 3412 KB Output is correct
4 Correct 72 ms 3892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 105 ms 3076 KB Output is correct
2 Correct 121 ms 4304 KB Output is correct
3 Correct 199 ms 4632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 83 ms 3148 KB Output is correct