Submission #763559

# Submission time Handle Problem Language Result Execution time Memory
763559 2023-06-22T12:52:37 Z amunduzbaev Radio Towers (IOI22_towers) C++17
23 / 100
4000 ms 11048 KB
#include "towers.h"

#include "bits/stdc++.h"
using namespace std;

#ifndef EVAL
#include "stub.cpp"
#endif

#define ar array
typedef int64_t ll;
//~ #define int ll

const int inf = 1e9 + 7;
const int N = 1e5 + 5;
const int B = 320;
//~ int first, mount;
//~ vector<int> a, l, r, res;
vector<ar<int, 2>> res;
vector<int> a, tot;

struct STMx{
	vector<int> tree;
	int N;
	
	STMx(int N): N(N){
		tree.resize(N << 2);
	}
	
	void set(int i, int v, int lx, int rx, int x){
		if(lx == rx){
			tree[x] = v;
			return;
		}
		
		int m = (lx + rx) >> 1;
		if(i <= m) set(i, v, lx, m, x << 1);
		else set(i, v, m + 1, rx, x << 1 | 1);
		tree[x] = max(tree[x << 1], tree[x << 1 | 1]);
	}
	
	void set(int i, int v){
		set(i, v, 0, N, 1);
	}
	
	int get(int l, int r, int lx, int rx, int x){
		if(lx > r || rx < l){
			return 0;
		}
		if(lx >= l && rx <= r){
			return tree[x];
		}
		int m = (lx + rx) >> 1;
		return max(get(l, r, lx, m, x << 1), get(l, r, m + 1, rx, x << 1 | 1));
	}
	
	int get(int l, int r){
		return get(l, r, 0, N, 1);
	}
	
	int first(int l, int r, int v, int lx, int rx, int x){
		if(lx > r || rx < l) return -1;
		if(lx >= l && rx <= r){
			if(tree[x] < v) return -1;
			if(lx == rx) return lx;
			int m = (lx + rx) >> 1;
			if(tree[x << 1] >= v) return first(l, r, v, lx, m, x << 1);
			else return first(l, r, v, m + 1, rx, x << 1 | 1);
		}
		int m = (lx + rx) >> 1;
		int res = first(l, r, v, lx, m, x << 1);
		if(res == -1) res = first(l, r, v, m + 1, rx, x << 1 | 1);
		return res;
	}
	
	int first(int l, int r, int v){
		return first(l, r, v, 0, N, 1);
	}
	
	int last(int l, int r, int v, int lx, int rx, int x){
		if(lx > r || rx < l) return -1;
		if(lx >= l && rx <= r){
			if(tree[x] < v) return -1;
			if(lx == rx) return lx;
			int m = (lx + rx) >> 1;
			if(tree[x << 1 | 1] >= v) return last(l, r, v, m + 1, rx, x << 1 | 1);
			else return last(l, r, v, lx, m, x << 1);
		}
		
		int m = (lx + rx) >> 1;
		int res = last(l, r, v, m + 1, rx, x << 1 | 1);
		if(res == -1) res = last(l, r, v, lx, m, x << 1);
		return res;
	}
	
	int last(int l, int r, int v){
		return last(l, r, v, 0, N, 1);
	}
}Max(N);

vector<int> v;

void init(int n, vector<int> h) {
	a = h;
	vector<int> pos;
	for(int i=0;i<n;i++){
		Max.set(i, a[i]);
		if((!i || a[i] < a[i - 1]) && (i + 1 == n || a[i] < a[i + 1])){
			pos.push_back(i);
		}
	}
	
	set<int> ss;
	multiset<ar<int, 2>> dif;
	
	for(int i=0;i<(int)pos.size();i++){
		if(i){
			int mx = Max.get(pos[i - 1], pos[i]);
			dif.insert({mx - a[pos[i - 1]], pos[i - 1]});
			dif.insert({mx - a[pos[i]], pos[i]});
		}
		ss.insert(pos[i]);
	}
	
	while(!dif.empty()){
		auto [d, i] = *dif.begin();
		res.push_back({d, i});
		
		//~ cout<<i<<" "<<d<<"\n";
		auto it = ss.lower_bound(i);
		auto R = it; R++;
		if(it == ss.begin() || R == ss.end()){
			if(it == ss.begin()){
				int mx = Max.get(*it, *R);
				dif.erase(dif.find({mx - a[*it], *it}));
				dif.erase(dif.find({mx - a[*R], *R}));
			} else {
				auto L = it; L--;
				int mx = Max.get(*L, *it);
				dif.erase(dif.find({mx - a[*it], *it}));
				dif.erase(dif.find({mx - a[*L], *L}));
			}
			ss.erase(it);
			continue;
		}
		
		auto L = it; --L;
		
		int mx = Max.get(*L, *it);
		dif.erase(dif.find({mx - a[*it], *it}));
		dif.erase(dif.find({mx - a[*L], *L}));
		
		mx = Max.get(*it, *R);
		dif.erase(dif.find({mx - a[*it], *it}));
		dif.erase(dif.find({mx - a[*R], *R}));
		
		mx = Max.get(*L, *R);
		dif.insert({mx - a[*L], *L});
		dif.insert({mx - a[*R], *R});
		
		ss.erase(it);
	}
	res.push_back({inf, *ss.begin()});
	for(auto x : res) tot.push_back(x[0]);
	tot.erase(unique(tot.begin(), tot.end()), tot.end());
	v.resize(n);
	
	for(auto [d, i] : res){
		v[i] = lower_bound(tot.begin(), tot.end(), d) - tot.begin() + 1;
	}
	
	for(int i=0;i<n;i++){
		//~ tree.set(i, a[i]);
		assert(v[i] <= N);
		//~ block.set(i, v[i]);
	}
	
	//~ block.build();
}

int max_towers(int l, int r, int d) {
	int D = lower_bound(tot.begin(), tot.end(), d) - tot.begin() + 1;
	int n = a.size();
	
	//~ int j = lower_bound(res.begin(), res.end(), (ar<int, 2>){d, 0}) - res.begin();
	//~ return (int)res.size() - j;
	//~ int res = block.get(l, r, D);
	
	auto get = [&](int l, int r, int d){
		int res = 0;
		for(int i=l;i<=r;i++){
			if(v[i] >= d) res++;
		}
		
		return res;
	};
	int res = get(l, r, D);
	
	auto get01 = [&](int l, int r, int d){
		int mn = inf;
		for(int i=l;i<=r;i++){
			if(a[i] >= mn + d){
				return i;
			}
			mn = min(mn, a[i]);
		}
		
		return -1;
	};
	auto get10 = [&](int l, int r, int d){
		int mn = inf;
		for(int i=r;i>=l;i--){
			if(a[i] >= mn + d){
				return i;
			}
			mn = min(mn, a[i]);
		}
		
		return -1;
	};
	
	if(!res){
		//~ int l_ = tree.get01(l, r, d), r_ = tree.get10(l, r, d);
		int l_ = get01(l, r, d), r_ = get10(l, r, d);
		if(~l_ && ~r_ && l_ <= r_){
			return 2;
		}
		
		return 1;
	}
	
	auto first = [&](int l, int r, int d){
		for(int i=l;i<=r;i++){
			if(v[i] >= d) return i;
		}
		assert(false);
		return -1;
	};
	
	auto last = [&](int l, int r, int d){
		for(int i=r;i>=l;i--){
			if(v[i] >= d) return i;
		}
		assert(false);
		return -1;
	};
	
	//~ int l_ = block.first(l, r, D), r_ = block.last(l, r, D);
	int l_ = first(l, r, D), r_ = last(l, r, D);
	l_ = Max.last(l, l_, a[l_] + d);
	r_ = Max.first(r_, r, a[r_] + d);
	
	//~ if(~l_ && ~tree.get01(l, l_, d)){
		//~ res++;
	//~ }
	//~ if(~r_ && ~tree.get10(r_, r, d)){
		//~ res++;
	//~ }
	if(~l_ && ~get01(l, l_, d)){
		res++;
	}
	if(~r_ && ~get10(r_, r, d)){
		res++;
	}
	
	return res;
}

Compilation message

towers.cpp: In function 'int max_towers(int, int, int)':
towers.cpp:183:6: warning: unused variable 'n' [-Wunused-variable]
  183 |  int n = a.size();
      |      ^
# Verdict Execution time Memory Grader output
1 Correct 2220 ms 2768 KB Output is correct
2 Execution timed out 4083 ms 3408 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1872 KB Output is correct
2 Correct 2 ms 2000 KB Output is correct
3 Correct 2 ms 2000 KB Output is correct
4 Correct 3 ms 2000 KB Output is correct
5 Correct 3 ms 2000 KB Output is correct
6 Correct 3 ms 2000 KB Output is correct
7 Correct 3 ms 2000 KB Output is correct
8 Correct 1 ms 1872 KB Output is correct
9 Correct 1 ms 1872 KB Output is correct
10 Correct 1 ms 1872 KB Output is correct
11 Correct 1 ms 1872 KB Output is correct
12 Correct 1 ms 1872 KB Output is correct
13 Correct 1 ms 1872 KB Output is correct
14 Correct 2 ms 1872 KB Output is correct
15 Correct 3 ms 2000 KB Output is correct
16 Correct 3 ms 2000 KB Output is correct
17 Correct 3 ms 2000 KB Output is correct
18 Correct 2 ms 1872 KB Output is correct
19 Correct 1 ms 1872 KB Output is correct
20 Correct 2 ms 2000 KB Output is correct
21 Correct 3 ms 2000 KB Output is correct
22 Correct 3 ms 2000 KB Output is correct
23 Correct 1 ms 1872 KB Output is correct
24 Correct 1 ms 1872 KB Output is correct
25 Correct 1 ms 1872 KB Output is correct
26 Correct 2 ms 2000 KB Output is correct
27 Correct 2 ms 2000 KB Output is correct
28 Correct 3 ms 2000 KB Output is correct
29 Correct 3 ms 2000 KB Output is correct
30 Correct 3 ms 2000 KB Output is correct
31 Correct 3 ms 2000 KB Output is correct
32 Correct 1 ms 1872 KB Output is correct
33 Correct 1 ms 1872 KB Output is correct
34 Correct 1 ms 1872 KB Output is correct
35 Correct 1 ms 1872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1872 KB Output is correct
2 Correct 2 ms 2000 KB Output is correct
3 Correct 2 ms 2000 KB Output is correct
4 Correct 3 ms 2000 KB Output is correct
5 Correct 3 ms 2000 KB Output is correct
6 Correct 3 ms 2000 KB Output is correct
7 Correct 3 ms 2000 KB Output is correct
8 Correct 1 ms 1872 KB Output is correct
9 Correct 1 ms 1872 KB Output is correct
10 Correct 1 ms 1872 KB Output is correct
11 Correct 1 ms 1872 KB Output is correct
12 Correct 1 ms 1872 KB Output is correct
13 Correct 1 ms 1872 KB Output is correct
14 Correct 2 ms 1872 KB Output is correct
15 Correct 3 ms 2000 KB Output is correct
16 Correct 3 ms 2000 KB Output is correct
17 Correct 3 ms 2000 KB Output is correct
18 Correct 2 ms 1872 KB Output is correct
19 Correct 1 ms 1872 KB Output is correct
20 Correct 2 ms 2000 KB Output is correct
21 Correct 3 ms 2000 KB Output is correct
22 Correct 3 ms 2000 KB Output is correct
23 Correct 1 ms 1872 KB Output is correct
24 Correct 1 ms 1872 KB Output is correct
25 Correct 1 ms 1872 KB Output is correct
26 Correct 2 ms 2000 KB Output is correct
27 Correct 2 ms 2000 KB Output is correct
28 Correct 3 ms 2000 KB Output is correct
29 Correct 3 ms 2000 KB Output is correct
30 Correct 3 ms 2000 KB Output is correct
31 Correct 3 ms 2000 KB Output is correct
32 Correct 1 ms 1872 KB Output is correct
33 Correct 1 ms 1872 KB Output is correct
34 Correct 1 ms 1872 KB Output is correct
35 Correct 1 ms 1872 KB Output is correct
36 Correct 63 ms 6068 KB Output is correct
37 Correct 104 ms 8448 KB Output is correct
38 Correct 101 ms 8552 KB Output is correct
39 Correct 162 ms 11016 KB Output is correct
40 Correct 167 ms 10948 KB Output is correct
41 Correct 159 ms 11012 KB Output is correct
42 Correct 160 ms 10936 KB Output is correct
43 Correct 15 ms 3396 KB Output is correct
44 Correct 15 ms 3412 KB Output is correct
45 Correct 23 ms 3396 KB Output is correct
46 Correct 15 ms 3408 KB Output is correct
47 Correct 103 ms 8516 KB Output is correct
48 Correct 160 ms 11028 KB Output is correct
49 Correct 163 ms 10944 KB Output is correct
50 Correct 15 ms 3396 KB Output is correct
51 Correct 17 ms 3452 KB Output is correct
52 Correct 111 ms 8512 KB Output is correct
53 Correct 166 ms 10984 KB Output is correct
54 Correct 170 ms 10928 KB Output is correct
55 Correct 16 ms 3412 KB Output is correct
56 Correct 15 ms 3392 KB Output is correct
57 Correct 104 ms 7964 KB Output is correct
58 Correct 102 ms 8388 KB Output is correct
59 Correct 102 ms 8520 KB Output is correct
60 Correct 156 ms 10936 KB Output is correct
61 Correct 159 ms 11032 KB Output is correct
62 Correct 180 ms 11040 KB Output is correct
63 Correct 161 ms 11048 KB Output is correct
64 Correct 15 ms 3408 KB Output is correct
65 Correct 22 ms 3412 KB Output is correct
66 Correct 15 ms 3400 KB Output is correct
67 Correct 16 ms 3412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2308 ms 8560 KB Output is correct
2 Correct 3030 ms 8536 KB Output is correct
3 Correct 2793 ms 8576 KB Output is correct
4 Correct 3017 ms 10960 KB Output is correct
5 Correct 2969 ms 11048 KB Output is correct
6 Correct 3091 ms 10944 KB Output is correct
7 Correct 2884 ms 10984 KB Output is correct
8 Execution timed out 4011 ms 3400 KB Time limit exceeded
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 739 ms 3424 KB Output is correct
2 Execution timed out 4018 ms 8516 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1872 KB Output is correct
2 Correct 2 ms 2000 KB Output is correct
3 Correct 2 ms 2000 KB Output is correct
4 Correct 3 ms 2000 KB Output is correct
5 Correct 3 ms 2000 KB Output is correct
6 Correct 3 ms 2000 KB Output is correct
7 Correct 3 ms 2000 KB Output is correct
8 Correct 1 ms 1872 KB Output is correct
9 Correct 1 ms 1872 KB Output is correct
10 Correct 1 ms 1872 KB Output is correct
11 Correct 1 ms 1872 KB Output is correct
12 Correct 1 ms 1872 KB Output is correct
13 Correct 1 ms 1872 KB Output is correct
14 Correct 2 ms 1872 KB Output is correct
15 Correct 3 ms 2000 KB Output is correct
16 Correct 3 ms 2000 KB Output is correct
17 Correct 3 ms 2000 KB Output is correct
18 Correct 2 ms 1872 KB Output is correct
19 Correct 1 ms 1872 KB Output is correct
20 Correct 2 ms 2000 KB Output is correct
21 Correct 3 ms 2000 KB Output is correct
22 Correct 3 ms 2000 KB Output is correct
23 Correct 1 ms 1872 KB Output is correct
24 Correct 1 ms 1872 KB Output is correct
25 Correct 1 ms 1872 KB Output is correct
26 Correct 2 ms 2000 KB Output is correct
27 Correct 2 ms 2000 KB Output is correct
28 Correct 3 ms 2000 KB Output is correct
29 Correct 3 ms 2000 KB Output is correct
30 Correct 3 ms 2000 KB Output is correct
31 Correct 3 ms 2000 KB Output is correct
32 Correct 1 ms 1872 KB Output is correct
33 Correct 1 ms 1872 KB Output is correct
34 Correct 1 ms 1872 KB Output is correct
35 Correct 1 ms 1872 KB Output is correct
36 Correct 63 ms 6068 KB Output is correct
37 Correct 104 ms 8448 KB Output is correct
38 Correct 101 ms 8552 KB Output is correct
39 Correct 162 ms 11016 KB Output is correct
40 Correct 167 ms 10948 KB Output is correct
41 Correct 159 ms 11012 KB Output is correct
42 Correct 160 ms 10936 KB Output is correct
43 Correct 15 ms 3396 KB Output is correct
44 Correct 15 ms 3412 KB Output is correct
45 Correct 23 ms 3396 KB Output is correct
46 Correct 15 ms 3408 KB Output is correct
47 Correct 103 ms 8516 KB Output is correct
48 Correct 160 ms 11028 KB Output is correct
49 Correct 163 ms 10944 KB Output is correct
50 Correct 15 ms 3396 KB Output is correct
51 Correct 17 ms 3452 KB Output is correct
52 Correct 111 ms 8512 KB Output is correct
53 Correct 166 ms 10984 KB Output is correct
54 Correct 170 ms 10928 KB Output is correct
55 Correct 16 ms 3412 KB Output is correct
56 Correct 15 ms 3392 KB Output is correct
57 Correct 104 ms 7964 KB Output is correct
58 Correct 102 ms 8388 KB Output is correct
59 Correct 102 ms 8520 KB Output is correct
60 Correct 156 ms 10936 KB Output is correct
61 Correct 159 ms 11032 KB Output is correct
62 Correct 180 ms 11040 KB Output is correct
63 Correct 161 ms 11048 KB Output is correct
64 Correct 15 ms 3408 KB Output is correct
65 Correct 22 ms 3412 KB Output is correct
66 Correct 15 ms 3400 KB Output is correct
67 Correct 16 ms 3412 KB Output is correct
68 Correct 2308 ms 8560 KB Output is correct
69 Correct 3030 ms 8536 KB Output is correct
70 Correct 2793 ms 8576 KB Output is correct
71 Correct 3017 ms 10960 KB Output is correct
72 Correct 2969 ms 11048 KB Output is correct
73 Correct 3091 ms 10944 KB Output is correct
74 Correct 2884 ms 10984 KB Output is correct
75 Execution timed out 4011 ms 3400 KB Time limit exceeded
76 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2220 ms 2768 KB Output is correct
2 Execution timed out 4083 ms 3408 KB Time limit exceeded
3 Halted 0 ms 0 KB -