Submission #763557

# Submission time Handle Problem Language Result Execution time Memory
763557 2023-06-22T12:51:42 Z amunduzbaev Radio Towers (IOI22_towers) C++17
0 / 100
1452 ms 13172 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;
	};
	
	//~ 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:182:6: warning: unused variable 'n' [-Wunused-variable]
  182 |  int n = a.size();
      |      ^
towers.cpp: In member function 'int STMx::first(int, int, int, int, int, int)':
towers.cpp:73:2: warning: control reaches end of non-void function [-Wreturn-type]
   73 |  }
      |  ^
# Verdict Execution time Memory Grader output
1 Runtime error 1452 ms 5084 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 3 ms 3580 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 3 ms 3580 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 114 ms 13172 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 27 ms 6760 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 3 ms 3580 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 1452 ms 5084 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -