답안 #842546

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
842546 2023-09-03T03:15:56 Z vjudge1 Growing Trees (BOI11_grow) C++17
90 / 100
1000 ms 3828 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);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 509 ms 3612 KB Output is correct
2 Correct 717 ms 3632 KB Output is correct
3 Correct 755 ms 3408 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2396 KB Output is correct
2 Correct 11 ms 2548 KB Output is correct
3 Correct 9 ms 2556 KB Output is correct
4 Correct 6 ms 2396 KB Output is correct
5 Correct 262 ms 2616 KB Output is correct
6 Correct 340 ms 2868 KB Output is correct
7 Correct 22 ms 2392 KB Output is correct
8 Correct 227 ms 2796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 330 ms 2876 KB Output is correct
2 Correct 355 ms 3144 KB Output is correct
3 Correct 5 ms 2392 KB Output is correct
4 Correct 271 ms 2640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 360 ms 3160 KB Output is correct
2 Correct 385 ms 2916 KB Output is correct
3 Correct 43 ms 2392 KB Output is correct
4 Correct 354 ms 2900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 516 ms 3152 KB Output is correct
2 Correct 679 ms 3408 KB Output is correct
3 Correct 91 ms 2940 KB Output is correct
4 Correct 601 ms 3324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 577 ms 3352 KB Output is correct
2 Correct 698 ms 3664 KB Output is correct
3 Correct 779 ms 3304 KB Output is correct
4 Correct 92 ms 2796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 550 ms 3776 KB Output is correct
2 Correct 506 ms 3408 KB Output is correct
3 Correct 731 ms 3344 KB Output is correct
4 Correct 91 ms 3024 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 705 ms 3408 KB Output is correct
2 Correct 701 ms 3416 KB Output is correct
3 Correct 96 ms 3444 KB Output is correct
4 Correct 561 ms 3412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 632 ms 3796 KB Output is correct
2 Correct 699 ms 3488 KB Output is correct
3 Execution timed out 1016 ms 3424 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 557 ms 3828 KB Output is correct