Submission #763556

# Submission time Handle Problem Language Result Execution time Memory
763556 2023-06-22T12:50:41 Z amunduzbaev Radio Towers (IOI22_towers) C++17
23 / 100
4000 ms 11128 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);
	}
	
	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;
	};
	
	auto mfirst = [&](int l, int r, int d){
		for(int i=l;i<=r;i++){
			if(a[i] >= d){
				return i;
			}
		}
		
		return -1;
	};
	
	auto mlast = [&](int l, int r, int d){
		for(int i=r;i>=l;i--){
			if(a[i] >= d) return i;
		}
		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);
	l_ = mlast(l, l_, a[l_] + d);
	r_ = mfirst(r_, r, a[r_] + d);
	//~ cout<<l_<<" "<<r_<<"\n";
	
	//~ 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:182:6: warning: unused variable 'n' [-Wunused-variable]
  182 |  int n = a.size();
      |      ^
# Verdict Execution time Memory Grader output
1 Correct 2455 ms 2772 KB Output is correct
2 Execution timed out 4038 ms 3432 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 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 4 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 2 ms 1872 KB Output is correct
11 Correct 2 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 1 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 2004 KB Output is correct
23 Correct 2 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 2 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 4 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 2 ms 1872 KB Output is correct
11 Correct 2 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 1 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 2004 KB Output is correct
23 Correct 2 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 75 ms 6144 KB Output is correct
37 Correct 129 ms 8540 KB Output is correct
38 Correct 107 ms 8596 KB Output is correct
39 Correct 173 ms 11016 KB Output is correct
40 Correct 169 ms 10940 KB Output is correct
41 Correct 167 ms 11128 KB Output is correct
42 Correct 179 ms 11024 KB Output is correct
43 Correct 15 ms 3448 KB Output is correct
44 Correct 15 ms 3412 KB Output is correct
45 Correct 15 ms 3432 KB Output is correct
46 Correct 20 ms 3400 KB Output is correct
47 Correct 100 ms 8516 KB Output is correct
48 Correct 186 ms 11036 KB Output is correct
49 Correct 153 ms 11032 KB Output is correct
50 Correct 21 ms 3396 KB Output is correct
51 Correct 19 ms 3400 KB Output is correct
52 Correct 115 ms 8540 KB Output is correct
53 Correct 156 ms 10944 KB Output is correct
54 Correct 178 ms 10988 KB Output is correct
55 Correct 18 ms 3400 KB Output is correct
56 Correct 19 ms 3408 KB Output is correct
57 Correct 114 ms 8016 KB Output is correct
58 Correct 113 ms 8424 KB Output is correct
59 Correct 100 ms 8460 KB Output is correct
60 Correct 185 ms 11028 KB Output is correct
61 Correct 170 ms 10944 KB Output is correct
62 Correct 160 ms 10948 KB Output is correct
63 Correct 154 ms 11024 KB Output is correct
64 Correct 15 ms 3416 KB Output is correct
65 Correct 15 ms 3400 KB Output is correct
66 Correct 24 ms 3408 KB Output is correct
67 Correct 19 ms 3412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2258 ms 8568 KB Output is correct
2 Correct 3250 ms 8512 KB Output is correct
3 Correct 3245 ms 8552 KB Output is correct
4 Correct 3027 ms 10952 KB Output is correct
5 Correct 3193 ms 10956 KB Output is correct
6 Correct 2813 ms 11000 KB Output is correct
7 Correct 2926 ms 10948 KB Output is correct
8 Execution timed out 4080 ms 3388 KB Time limit exceeded
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 728 ms 3408 KB Output is correct
2 Execution timed out 4014 ms 8516 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 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 4 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 2 ms 1872 KB Output is correct
11 Correct 2 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 1 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 2004 KB Output is correct
23 Correct 2 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 75 ms 6144 KB Output is correct
37 Correct 129 ms 8540 KB Output is correct
38 Correct 107 ms 8596 KB Output is correct
39 Correct 173 ms 11016 KB Output is correct
40 Correct 169 ms 10940 KB Output is correct
41 Correct 167 ms 11128 KB Output is correct
42 Correct 179 ms 11024 KB Output is correct
43 Correct 15 ms 3448 KB Output is correct
44 Correct 15 ms 3412 KB Output is correct
45 Correct 15 ms 3432 KB Output is correct
46 Correct 20 ms 3400 KB Output is correct
47 Correct 100 ms 8516 KB Output is correct
48 Correct 186 ms 11036 KB Output is correct
49 Correct 153 ms 11032 KB Output is correct
50 Correct 21 ms 3396 KB Output is correct
51 Correct 19 ms 3400 KB Output is correct
52 Correct 115 ms 8540 KB Output is correct
53 Correct 156 ms 10944 KB Output is correct
54 Correct 178 ms 10988 KB Output is correct
55 Correct 18 ms 3400 KB Output is correct
56 Correct 19 ms 3408 KB Output is correct
57 Correct 114 ms 8016 KB Output is correct
58 Correct 113 ms 8424 KB Output is correct
59 Correct 100 ms 8460 KB Output is correct
60 Correct 185 ms 11028 KB Output is correct
61 Correct 170 ms 10944 KB Output is correct
62 Correct 160 ms 10948 KB Output is correct
63 Correct 154 ms 11024 KB Output is correct
64 Correct 15 ms 3416 KB Output is correct
65 Correct 15 ms 3400 KB Output is correct
66 Correct 24 ms 3408 KB Output is correct
67 Correct 19 ms 3412 KB Output is correct
68 Correct 2258 ms 8568 KB Output is correct
69 Correct 3250 ms 8512 KB Output is correct
70 Correct 3245 ms 8552 KB Output is correct
71 Correct 3027 ms 10952 KB Output is correct
72 Correct 3193 ms 10956 KB Output is correct
73 Correct 2813 ms 11000 KB Output is correct
74 Correct 2926 ms 10948 KB Output is correct
75 Execution timed out 4080 ms 3388 KB Time limit exceeded
76 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2455 ms 2772 KB Output is correct
2 Execution timed out 4038 ms 3432 KB Time limit exceeded
3 Halted 0 ms 0 KB -