답안 #842540

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

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(long long int, long long int, long long int)':
grow.cpp:16:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   16 |  int mid = l + r >> 1;
      |            ~~^~~
grow.cpp: In function 'void update(long long int, long long int, long long int, long long int, long long int)':
grow.cpp:45:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   45 |  int mid = l + r >> 1;
      |            ~~^~~
grow.cpp: In function 'long long int get(long long int, long long int, long long int, long long int)':
grow.cpp:59:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   59 |  int mid = l + r >> 1;
      |            ~~^~~
grow.cpp: In function 'long long int binary_search_1(long long int)':
grow.cpp:72:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   72 |   int mid = l + r >> 1;
      |             ~~^~~
grow.cpp: In function 'long long int binary_search_2(long long int)':
grow.cpp:87:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   87 |   int mid = l + r >> 1;
      |             ~~^~~
grow.cpp: At global scope:
grow.cpp:97:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   97 | main() {
      | ^~~~
grow.cpp: In function 'int main()':
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.inp" , "r" , stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
grow.cpp:102:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  102 |   freopen("solve.out" , "w" , stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 553 ms 10192 KB Output is correct
2 Correct 735 ms 10584 KB Output is correct
3 Correct 816 ms 10176 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 4444 KB Output is correct
2 Correct 12 ms 4640 KB Output is correct
3 Correct 10 ms 4444 KB Output is correct
4 Correct 6 ms 4580 KB Output is correct
5 Correct 299 ms 5716 KB Output is correct
6 Correct 351 ms 5824 KB Output is correct
7 Correct 23 ms 4700 KB Output is correct
8 Correct 241 ms 5456 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 342 ms 5968 KB Output is correct
2 Correct 349 ms 5968 KB Output is correct
3 Correct 8 ms 4444 KB Output is correct
4 Correct 316 ms 5608 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 346 ms 6240 KB Output is correct
2 Correct 400 ms 6004 KB Output is correct
3 Correct 45 ms 4700 KB Output is correct
4 Correct 348 ms 6044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 516 ms 8896 KB Output is correct
2 Correct 731 ms 10192 KB Output is correct
3 Correct 93 ms 5716 KB Output is correct
4 Correct 610 ms 10304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 621 ms 9576 KB Output is correct
2 Correct 736 ms 9896 KB Output is correct
3 Correct 796 ms 10176 KB Output is correct
4 Correct 88 ms 5480 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 574 ms 9780 KB Output is correct
2 Correct 554 ms 9812 KB Output is correct
3 Correct 771 ms 10288 KB Output is correct
4 Correct 87 ms 5460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 745 ms 10656 KB Output is correct
2 Correct 712 ms 9816 KB Output is correct
3 Correct 108 ms 9180 KB Output is correct
4 Correct 620 ms 9812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 678 ms 10084 KB Output is correct
2 Correct 765 ms 10224 KB Output is correct
3 Execution timed out 1043 ms 10308 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 618 ms 10836 KB Output is correct