Submission #827252

# Submission time Handle Problem Language Result Execution time Memory
827252 2023-08-16T10:12:23 Z esomer Radio Towers (IOI22_towers) C++17
18 / 100
864 ms 42804 KB
#include <bits/stdc++.h>
#include "towers.h"

using namespace std;

int N, spec, first;
vector<int> H, lw, good, prefix;
vector<pair<int, int>> candidates;
vector<vector<pair<int, int>>> mn, mx;

void precalcmx(){
	mx.assign(20, vector<pair<int, int>>(N));
	for(int k = 0; k < 20; k++){
		for(int i = 0; i < N; i++){
			if(k == 0) mx[k][i] = {H[i], i};
			else{
				if(i - (1 << (k-1)) < 0) mx[k][i] = mx[k-1][i];
				else{
					mx[k][i] = max(mx[k-1][i], mx[k-1][i - (1 << (k-1))]);
				}
			}
		}
	}
}

void precalcmn(){
	mn.assign(20, vector<pair<int, int>>(N));
	for(int k = 0; k < 20; k++){
		for(int i = 0; i < N; i++){
			if(k == 0) mn[k][i] = {H[i], i};
			else{
				if(i - (1 << (k-1)) < 0) mn[k][i] = mn[k-1][i];
				else{
					mn[k][i] = min(mn[k-1][i], mn[k-1][i - (1 << (k-1))]);
				}
			}
		}
	}
}

void precalclw(){
	lw.resize(N+1);
	int pw = 0;
	int curr = 2;
	for(int i = 1; i <= N; i++){
		if(i == curr){
			pw++; curr *= 2;
		}
		lw[i] = pw;
	}
}

pair<int, int> get_mx(int l, int r){
	if(r < l) return {-2e9, 0};
	int k = lw[r - l + 1];
	return max(mx[k][r], mx[k][l + (1 << k) - 1]);
}

pair<int, int> get_mn(int l, int r){
	if(r < l) return {2e9, 0};
	int k = lw[r - l + 1];
	return min(mn[k][r], mn[k][l + (1 << k) - 1]);
}

int get_first(int L){
	int lo = L;
	int hi = N - 1;
	int bst;
	int beat = 0;
	if(L > 0) beat = prefix[L-1];
	while(lo <= hi){
		int mid = (lo + hi) / 2;
		if(prefix[mid] > beat){
			bst = mid;
			hi = mid - 1;
		}else lo = mid + 1;
	}
	return bst;
}

int get_last(int R){
	int lo = 0;
	int hi = R;
	int bst = -1;
	while(lo <= hi){
		int mid = (lo + hi) / 2;
		if(prefix[mid] <= prefix[R] - 1){
			bst = mid;
			lo = mid + 1;
		}else hi = mid - 1;
	}
	return bst + 1;
}

void init(int n, vector<int> H1) {
	N = n;
	H = H1;
	good.assign(N, 0);
	spec = 0;
	first = 1;
	bool asc = 1;
	for(int i = 1; i < N; i++){
		if(spec == -1) break;
		if(H[i] > H[i-1]){
			if(asc) spec = i;
			else spec = -1;
		}else{
			asc = 0;
		}
	}
	precalcmx();
	precalcmn();
	precalclw();
}

int dnc(int L, int R, int D) {
	if(L == R) {
		return H[L];
	}else if(L > R){
		return 2e9;
	}
	pair<int, int> pr = get_mx(L, R);
	int mx = pr.first;
	int ind = pr.second;
	int mn1 = dnc(ind + 1, R, D);
	int mn2 = dnc(L, ind - 1, D);
	if(mn1 <= mx - D && mn2 <= mx - D) good[ind] = 1;
	candidates.push_back({min(mx - mn1, mx - mn2), ind});
	return min(mn1, mn2);
}

int max_towers(int L, int R, int D) {
	if(spec == -1){
		if(first == 1){
			dnc(0, N-1, D);
			sort(candidates.begin(), candidates.end());
			prefix.resize(N);
			for(int i = 0; i < N; i++){
				if(i == 0) prefix[i] = good[i];
				else prefix[i] = prefix[i-1] + good[i];
			}
			first = 0;
		}
		if(L == 0 && R == N - 1){
			int lo = 0;
			int hi = (int)candidates.size() - 1;
			int bst = N;
			while(lo <= hi){
				int mid = (lo + hi) / 2;
				if(candidates[mid].first >= D){
					bst = mid;
					hi = mid - 1;
				}else lo = mid + 1;
			}
			return (int)candidates.size() - bst + 1;
		}else{
			int total = prefix[R];
			if(L > 0) total -= prefix[L - 1];
			if(total == 0) return 1;
			else if(total == 1){
				int ind = get_first(L);
				int mn1 = get_mn(L, ind - 1).first;
				int mn2 = get_mn(ind + 1, R).first;
				if(mn1 <= H[ind] - D && mn2 <= H[ind] - D) return 2;
				else return 1;
			}else{
				int ans = total + 1;
				int ind = get_first(L);
				int mn = get_mn(L, ind - 1).first;
				if(mn > H[ind]  - D) ans--;
				ind = get_last(R);
				mn = get_mn(ind + 1, R).first;
				if(mn > H[ind] - D) ans--;
				return ans;	
			}
		}
	}else{
		int ans = 1;
		if(L < spec && R > spec && H[L] <= H[spec] - D && H[R] <= H[spec] - D) ans++;
		return ans;
	}
}

// int main() {
//   int N, Q;
//   assert(2 == scanf("%d %d", &N, &Q));
//   std::vector<int> H(N);
//   for (int i = 0; i < N; ++i) {
//     assert(1 == scanf("%d", &H[i]));
//   }
//   init(N, H);
//   for (int i = 0; i < Q; ++i) {
//     int L, R, D;
//     assert(3 == scanf("%d %d %d", &L, &R, &D));
//     printf("%d\n", max_towers(L, R, D));
//   }
//   return 0;
// }

Compilation message

towers.cpp: In function 'int get_first(int)':
towers.cpp:78:9: warning: 'bst' may be used uninitialized in this function [-Wmaybe-uninitialized]
   78 |  return bst;
      |         ^~~
# Verdict Execution time Memory Grader output
1 Correct 236 ms 20348 KB Output is correct
2 Correct 629 ms 33936 KB Output is correct
3 Correct 783 ms 33976 KB Output is correct
4 Correct 680 ms 33944 KB Output is correct
5 Correct 715 ms 34008 KB Output is correct
6 Correct 644 ms 33944 KB Output is correct
7 Correct 796 ms 33984 KB Output is correct
8 Correct 0 ms 208 KB Output is correct
9 Correct 1 ms 848 KB Output is correct
10 Correct 1 ms 848 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 976 KB Output is correct
3 Correct 2 ms 976 KB Output is correct
4 Correct 1 ms 848 KB Output is correct
5 Correct 1 ms 848 KB Output is correct
6 Correct 1 ms 912 KB Output is correct
7 Correct 1 ms 848 KB Output is correct
8 Correct 1 ms 848 KB Output is correct
9 Correct 1 ms 848 KB Output is correct
10 Correct 1 ms 976 KB Output is correct
11 Correct 1 ms 1104 KB Output is correct
12 Correct 0 ms 236 KB Output is correct
13 Correct 1 ms 848 KB Output is correct
14 Correct 1 ms 928 KB Output is correct
15 Correct 1 ms 976 KB Output is correct
16 Correct 1 ms 848 KB Output is correct
17 Correct 1 ms 848 KB Output is correct
18 Correct 1 ms 848 KB Output is correct
19 Correct 1 ms 976 KB Output is correct
20 Correct 1 ms 976 KB Output is correct
21 Correct 1 ms 848 KB Output is correct
22 Correct 1 ms 976 KB Output is correct
23 Correct 1 ms 848 KB Output is correct
24 Correct 1 ms 1104 KB Output is correct
25 Correct 1 ms 592 KB Output is correct
26 Correct 1 ms 976 KB Output is correct
27 Correct 1 ms 992 KB Output is correct
28 Correct 1 ms 848 KB Output is correct
29 Correct 1 ms 848 KB Output is correct
30 Correct 1 ms 980 KB Output is correct
31 Correct 1 ms 848 KB Output is correct
32 Correct 1 ms 848 KB Output is correct
33 Correct 1 ms 972 KB Output is correct
34 Incorrect 1 ms 976 KB 1st lines differ - on the 1st token, expected: '1', found: '-4'
35 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 976 KB Output is correct
3 Correct 2 ms 976 KB Output is correct
4 Correct 1 ms 848 KB Output is correct
5 Correct 1 ms 848 KB Output is correct
6 Correct 1 ms 912 KB Output is correct
7 Correct 1 ms 848 KB Output is correct
8 Correct 1 ms 848 KB Output is correct
9 Correct 1 ms 848 KB Output is correct
10 Correct 1 ms 976 KB Output is correct
11 Correct 1 ms 1104 KB Output is correct
12 Correct 0 ms 236 KB Output is correct
13 Correct 1 ms 848 KB Output is correct
14 Correct 1 ms 928 KB Output is correct
15 Correct 1 ms 976 KB Output is correct
16 Correct 1 ms 848 KB Output is correct
17 Correct 1 ms 848 KB Output is correct
18 Correct 1 ms 848 KB Output is correct
19 Correct 1 ms 976 KB Output is correct
20 Correct 1 ms 976 KB Output is correct
21 Correct 1 ms 848 KB Output is correct
22 Correct 1 ms 976 KB Output is correct
23 Correct 1 ms 848 KB Output is correct
24 Correct 1 ms 1104 KB Output is correct
25 Correct 1 ms 592 KB Output is correct
26 Correct 1 ms 976 KB Output is correct
27 Correct 1 ms 992 KB Output is correct
28 Correct 1 ms 848 KB Output is correct
29 Correct 1 ms 848 KB Output is correct
30 Correct 1 ms 980 KB Output is correct
31 Correct 1 ms 848 KB Output is correct
32 Correct 1 ms 848 KB Output is correct
33 Correct 1 ms 972 KB Output is correct
34 Incorrect 1 ms 976 KB 1st lines differ - on the 1st token, expected: '1', found: '-4'
35 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 614 ms 34608 KB Output is correct
2 Correct 708 ms 34864 KB Output is correct
3 Correct 722 ms 34892 KB Output is correct
4 Correct 855 ms 34640 KB Output is correct
5 Correct 546 ms 34600 KB Output is correct
6 Correct 674 ms 34560 KB Output is correct
7 Correct 705 ms 34612 KB Output is correct
8 Correct 712 ms 33976 KB Output is correct
9 Correct 632 ms 33976 KB Output is correct
10 Correct 864 ms 40984 KB Output is correct
11 Correct 722 ms 41032 KB Output is correct
12 Correct 662 ms 33972 KB Output is correct
13 Correct 665 ms 33928 KB Output is correct
14 Correct 0 ms 208 KB Output is correct
15 Correct 1 ms 848 KB Output is correct
16 Correct 1 ms 848 KB Output is correct
17 Correct 41 ms 34820 KB Output is correct
18 Correct 38 ms 34600 KB Output is correct
19 Correct 37 ms 34608 KB Output is correct
20 Correct 31 ms 33920 KB Output is correct
21 Correct 44 ms 42804 KB Output is correct
22 Correct 40 ms 34864 KB Output is correct
23 Correct 43 ms 34584 KB Output is correct
24 Correct 42 ms 34564 KB Output is correct
25 Correct 31 ms 33944 KB Output is correct
26 Correct 43 ms 42248 KB Output is correct
27 Correct 1 ms 976 KB Output is correct
28 Correct 1 ms 848 KB Output is correct
29 Correct 1 ms 976 KB Output is correct
30 Correct 1 ms 848 KB Output is correct
31 Correct 1 ms 976 KB Output is correct
32 Correct 1 ms 976 KB Output is correct
33 Correct 1 ms 848 KB Output is correct
34 Correct 1 ms 848 KB Output is correct
35 Correct 1 ms 924 KB Output is correct
36 Correct 1 ms 976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 171 ms 8488 KB 2257th lines differ - on the 1st token, expected: '1', found: '-8003'
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 976 KB Output is correct
3 Correct 2 ms 976 KB Output is correct
4 Correct 1 ms 848 KB Output is correct
5 Correct 1 ms 848 KB Output is correct
6 Correct 1 ms 912 KB Output is correct
7 Correct 1 ms 848 KB Output is correct
8 Correct 1 ms 848 KB Output is correct
9 Correct 1 ms 848 KB Output is correct
10 Correct 1 ms 976 KB Output is correct
11 Correct 1 ms 1104 KB Output is correct
12 Correct 0 ms 236 KB Output is correct
13 Correct 1 ms 848 KB Output is correct
14 Correct 1 ms 928 KB Output is correct
15 Correct 1 ms 976 KB Output is correct
16 Correct 1 ms 848 KB Output is correct
17 Correct 1 ms 848 KB Output is correct
18 Correct 1 ms 848 KB Output is correct
19 Correct 1 ms 976 KB Output is correct
20 Correct 1 ms 976 KB Output is correct
21 Correct 1 ms 848 KB Output is correct
22 Correct 1 ms 976 KB Output is correct
23 Correct 1 ms 848 KB Output is correct
24 Correct 1 ms 1104 KB Output is correct
25 Correct 1 ms 592 KB Output is correct
26 Correct 1 ms 976 KB Output is correct
27 Correct 1 ms 992 KB Output is correct
28 Correct 1 ms 848 KB Output is correct
29 Correct 1 ms 848 KB Output is correct
30 Correct 1 ms 980 KB Output is correct
31 Correct 1 ms 848 KB Output is correct
32 Correct 1 ms 848 KB Output is correct
33 Correct 1 ms 972 KB Output is correct
34 Incorrect 1 ms 976 KB 1st lines differ - on the 1st token, expected: '1', found: '-4'
35 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 236 ms 20348 KB Output is correct
2 Correct 629 ms 33936 KB Output is correct
3 Correct 783 ms 33976 KB Output is correct
4 Correct 680 ms 33944 KB Output is correct
5 Correct 715 ms 34008 KB Output is correct
6 Correct 644 ms 33944 KB Output is correct
7 Correct 796 ms 33984 KB Output is correct
8 Correct 0 ms 208 KB Output is correct
9 Correct 1 ms 848 KB Output is correct
10 Correct 1 ms 848 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 976 KB Output is correct
13 Correct 2 ms 976 KB Output is correct
14 Correct 1 ms 848 KB Output is correct
15 Correct 1 ms 848 KB Output is correct
16 Correct 1 ms 912 KB Output is correct
17 Correct 1 ms 848 KB Output is correct
18 Correct 1 ms 848 KB Output is correct
19 Correct 1 ms 848 KB Output is correct
20 Correct 1 ms 976 KB Output is correct
21 Correct 1 ms 1104 KB Output is correct
22 Correct 0 ms 236 KB Output is correct
23 Correct 1 ms 848 KB Output is correct
24 Correct 1 ms 928 KB Output is correct
25 Correct 1 ms 976 KB Output is correct
26 Correct 1 ms 848 KB Output is correct
27 Correct 1 ms 848 KB Output is correct
28 Correct 1 ms 848 KB Output is correct
29 Correct 1 ms 976 KB Output is correct
30 Correct 1 ms 976 KB Output is correct
31 Correct 1 ms 848 KB Output is correct
32 Correct 1 ms 976 KB Output is correct
33 Correct 1 ms 848 KB Output is correct
34 Correct 1 ms 1104 KB Output is correct
35 Correct 1 ms 592 KB Output is correct
36 Correct 1 ms 976 KB Output is correct
37 Correct 1 ms 992 KB Output is correct
38 Correct 1 ms 848 KB Output is correct
39 Correct 1 ms 848 KB Output is correct
40 Correct 1 ms 980 KB Output is correct
41 Correct 1 ms 848 KB Output is correct
42 Correct 1 ms 848 KB Output is correct
43 Correct 1 ms 972 KB Output is correct
44 Incorrect 1 ms 976 KB 1st lines differ - on the 1st token, expected: '1', found: '-4'
45 Halted 0 ms 0 KB -