답안 #842542

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
842542 2023-09-03T03:11:11 Z helloworld1705 Growing Trees (BOI11_grow) C++17
90 / 100
1000 ms 8044 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 1e6 + 5;

int n , q;
int a[N];
int tree[N] , lazy[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 517 ms 7924 KB Output is correct
2 Correct 715 ms 7812 KB Output is correct
3 Correct 788 ms 7840 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4440 KB Output is correct
2 Correct 9 ms 4444 KB Output is correct
3 Correct 9 ms 4444 KB Output is correct
4 Correct 6 ms 4444 KB Output is correct
5 Correct 257 ms 4640 KB Output is correct
6 Correct 374 ms 4928 KB Output is correct
7 Correct 22 ms 4440 KB Output is correct
8 Correct 222 ms 4668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 333 ms 4944 KB Output is correct
2 Correct 370 ms 5040 KB Output is correct
3 Correct 5 ms 4444 KB Output is correct
4 Correct 302 ms 4672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 331 ms 5180 KB Output is correct
2 Correct 376 ms 4880 KB Output is correct
3 Correct 51 ms 4444 KB Output is correct
4 Correct 336 ms 4984 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 507 ms 7304 KB Output is correct
2 Correct 708 ms 8020 KB Output is correct
3 Correct 85 ms 4700 KB Output is correct
4 Correct 587 ms 7620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 591 ms 7880 KB Output is correct
2 Correct 694 ms 7764 KB Output is correct
3 Correct 757 ms 7792 KB Output is correct
4 Correct 86 ms 4864 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 572 ms 8044 KB Output is correct
2 Correct 504 ms 7648 KB Output is correct
3 Correct 737 ms 7764 KB Output is correct
4 Correct 82 ms 4848 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 706 ms 7740 KB Output is correct
2 Correct 687 ms 7640 KB Output is correct
3 Correct 97 ms 7652 KB Output is correct
4 Correct 571 ms 7664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 633 ms 7896 KB Output is correct
2 Correct 674 ms 8020 KB Output is correct
3 Execution timed out 1022 ms 7620 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 568 ms 7968 KB Output is correct