Submission #842545

# Submission time Handle Problem Language Result Execution time Memory
842545 2023-09-03T03:15:13 Z helloworld1705 Growing Trees (BOI11_grow) C++17
100 / 100
984 ms 3932 KB
#include <bits/stdc++.h>
using namespace std;
 
const int N = 1e5 + 5;
 
int n , q;
int a[N];
int tree[4 * N] , lazy[4 * N];
 
void build(int x , int l , int r) {
	if(l == r) {
		tree[x] = a[l];
		return;
	}
	int mid = l + r >> 1;
	build(x << 1 , l , mid);
	build(x << 1 | 1 , mid + 1 , r);
	tree[x] = max(tree[x << 1] , tree[x << 1 | 1]);
}
 
void down(int x , int l , int r) {
	int val = lazy[x];
	if(val == 0) {
		return;
	}
	if(l == r) tree[x] += val;
	else {
		lazy[x << 1] += val;
		lazy[x << 1 | 1] += val;
	}
	lazy[x] = 0;
}
 
void update(int x , int l , int r , int L , int R) {
	down(x , l , r);
	if(l > R || r < L) {
		return;
	}
	if(L <= l && r <= R) {
		lazy[x]++;
		down(x , l , r);
		return;
	}
	int mid = l + r >> 1;
	update(x << 1 , l , mid , L , R);
	update(x << 1 | 1 , mid + 1 , r , L , R);
	tree[x] = max(tree[x << 1] , tree[x << 1 | 1]);
}
 
int get(int x , int l , int r , int pos) {
	down(x , l , r);
	if(l > pos || r < pos) {
		return -1;
	}
	if(l == r) {
		return tree[x];
	}
	int mid = l + r >> 1;
	return max(get(x << 1 , l , mid , pos) , get(x << 1 | 1 , mid + 1 , r , pos));
}
 
int get(int pos) {
	return get(1 , 1 , n , pos);
}
 
int binary_search_1(int x) {
	int l = 1;
	int r = n;
	int ans = -1;
	while(l - r <= 0) {
		int mid = l + r >> 1;
		if(get(mid) >= x) {
			r = mid - 1;
			ans = mid;
		}
		else l = mid + 1;
	}
	return ans;
}
 
int binary_search_2(int x) {
	int l = 1;
	int r = n;
	int ans = -1;
	while(l - r <= 0) {
		int mid = l + r >> 1;
		if(get(mid) <= x) {
			l = mid + 1;
			ans = mid;
		}
		else r = mid - 1;
	}
	return ans;
}
 
main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	if(fopen("solve.inp" , "r")) {
		freopen("solve.inp" , "r" , stdin);
		freopen("solve.out" , "w" , stdout);
	}
	cin >> n >> q;
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	sort(a + 1 , a + 1 + n);
	build(1 , 1 , n);
	while(q--) {
		char c;
		cin >> c;
		if(c == 'F') {
			int u , v;
			cin >> u >> v;
			int l1 = binary_search_1(v);
			if(l1 == -1) continue;
			int r1 = min(n , l1 + u - 1);
			int l2 = binary_search_1(get(r1));
			int r2 = binary_search_2(get(r1));
			if(l1 <= l2 - 1) {
				update(1 , 1 , n , l1 , l2 - 1);
			}
			int tmp = max(l2 , r2 - u + (l2 - l1) + 1);
			if(tmp <= r2) {
				update(1 , 1 , n , tmp , r2);
			}
		}
		else {
			int u , v;
			cin >> u >> v;
			int mn = binary_search_1(u);
			int mx = binary_search_2(v);
			if(mn == -1 or mx == -1) {
				cout << 0 << '\n';
			}
			else cout << mx - mn + 1 << '\n';
		}
	}
	return 0x0;
}

Compilation message

grow.cpp: In function 'void build(int, int, int)':
grow.cpp:15:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   15 |  int mid = l + r >> 1;
      |            ~~^~~
grow.cpp: In function 'void update(int, int, int, int, int)':
grow.cpp:44:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   44 |  int mid = l + r >> 1;
      |            ~~^~~
grow.cpp: In function 'int get(int, int, int, int)':
grow.cpp:58:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   58 |  int mid = l + r >> 1;
      |            ~~^~~
grow.cpp: In function 'int binary_search_1(int)':
grow.cpp:71:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   71 |   int mid = l + r >> 1;
      |             ~~^~~
grow.cpp: In function 'int binary_search_2(int)':
grow.cpp:86:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   86 |   int mid = l + r >> 1;
      |             ~~^~~
grow.cpp: At global scope:
grow.cpp:96:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   96 | main() {
      | ^~~~
grow.cpp: In function 'int main()':
grow.cpp:100:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  100 |   freopen("solve.inp" , "r" , stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
grow.cpp:101:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  101 |   freopen("solve.out" , "w" , stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 500 ms 3792 KB Output is correct
2 Correct 716 ms 3508 KB Output is correct
3 Correct 756 ms 3164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2396 KB Output is correct
2 Correct 9 ms 2548 KB Output is correct
3 Correct 10 ms 2556 KB Output is correct
4 Correct 6 ms 2396 KB Output is correct
5 Correct 255 ms 2616 KB Output is correct
6 Correct 342 ms 2988 KB Output is correct
7 Correct 26 ms 2396 KB Output is correct
8 Correct 224 ms 2612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 327 ms 2884 KB Output is correct
2 Correct 345 ms 2888 KB Output is correct
3 Correct 5 ms 2396 KB Output is correct
4 Correct 279 ms 3000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 360 ms 2992 KB Output is correct
2 Correct 380 ms 3184 KB Output is correct
3 Correct 43 ms 2392 KB Output is correct
4 Correct 340 ms 2900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 491 ms 2972 KB Output is correct
2 Correct 697 ms 3488 KB Output is correct
3 Correct 83 ms 2800 KB Output is correct
4 Correct 582 ms 3316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 566 ms 3344 KB Output is correct
2 Correct 687 ms 3688 KB Output is correct
3 Correct 741 ms 3164 KB Output is correct
4 Correct 83 ms 2772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 539 ms 3432 KB Output is correct
2 Correct 501 ms 3656 KB Output is correct
3 Correct 737 ms 3344 KB Output is correct
4 Correct 83 ms 2784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 729 ms 3668 KB Output is correct
2 Correct 684 ms 3744 KB Output is correct
3 Correct 97 ms 3436 KB Output is correct
4 Correct 576 ms 3668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 637 ms 3552 KB Output is correct
2 Correct 659 ms 3408 KB Output is correct
3 Correct 984 ms 3420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 560 ms 3932 KB Output is correct