Submission #772384

#TimeUsernameProblemLanguageResultExecution timeMemory
772384marvinthangRadio Towers (IOI22_towers)C++17
100 / 100
1247 ms57768 KiB
/*************************************
*    author: marvinthang             *
*    created: 04.07.2023 09:31:20    *
*************************************/

#include "towers.h"
#include <bits/stdc++.h>

using namespace std;

#define                  fi  first
#define                  se  second
#define                left  ___left
#define               right  ___right
#define                TIME  (1.0 * clock() / CLOCKS_PER_SEC)
#define             MASK(i)  (1LL << (i))
#define           BIT(x, i)  ((x) >> (i) & 1)
#define  __builtin_popcount  __builtin_popcountll
#define              ALL(v)  (v).begin(), (v).end()
#define           REP(i, n)  for (int i = 0, _n = (n); i < _n; ++i)
#define          REPD(i, n)  for (int i = (n); i--; )
#define        FOR(i, a, b)  for (int i = (a), _b = (b); i < _b; ++i) 
#define       FORD(i, b, a)  for (int i = (b), _a = (a); --i >= _a; ) 
#define       FORE(i, a, b)  for (int i = (a), _b = (b); i <= _b; ++i) 
#define      FORDE(i, b, a)  for (int i = (b), _a = (a); i >= _a; --i) 
#define        scan_op(...)  istream & operator >> (istream &in, __VA_ARGS__ &u)
#define       print_op(...)  ostream & operator << (ostream &out, const __VA_ARGS__ &u)
#ifdef LOCAL
		#include "debug.h"
#else
		#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
		#define DB(...) 23
		#define db(...) 23
		#define debug(...) 23
#endif

template <class U, class V> scan_op(pair <U, V>)  { return in >> u.first >> u.second; }
template <class T> scan_op(vector <T>)  { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class U, class V> print_op(pair <U, V>)  { return out << '(' << u.first << ", " << u.second << ')'; }
template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")";  else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); }
template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); }
template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; }

const int INF = 1e9;

struct Node {
	int ma, s_p, p_s;
	pair <int, int> mi;
	Node(): ma(-INF), mi(INF, INF), s_p(-INF), p_s(-INF) {}
	Node(int h, int i): ma(h), mi(h, i), s_p(-INF), p_s(-INF) {}
	Node operator + (const Node &other) const {
		Node res;
		res.ma = max(ma, other.ma);
		res.mi = min(mi, other.mi);
		res.s_p = max({s_p, other.s_p, other.ma - mi.fi});
		res.p_s = max({p_s, other.p_s, ma - other.mi.fi});
		return res;
	}
};

struct PNode {
	int mi, ma, cnt, left_child, right_child;
	PNode(): ma(-INF), mi(INF), cnt(0), left_child(-1), right_child(-1) {}
	PNode(int v): ma(v), mi(v), cnt(1), left_child(-1), right_child(-1) {}
	PNode operator + (const PNode &other) const {
		PNode res;
		res.mi = min(mi, other.mi);
		res.ma = max(ma, other.ma);
		res.cnt = cnt + other.cnt;
		return res;
	}
};

// end of template

int N;
vector <int> H, val, versions;
vector <Node> st;

void build(int i, int l, int r) {
	if (r - l == 1) {
		st[i] = Node(H[l], l);
		return;
	}
	int m = l + r >> 1;
	build(i << 1, l, m);
	build(i << 1 | 1, m, r);
	st[i] = st[i << 1] + st[i << 1 | 1];
}

Node get(int i, int l, int r, int u, int v) {
	if (l >= v || r <= u || u >= v) return Node();
	if (u <= l && r <= v) return st[i];
	int m = l + r >> 1;
	return get(i << 1, l, m, u, v) + get(i << 1 | 1, m, r, u, v);
}

vector <PNode> pnodes;

int createNode(int l, int r) {
	int i = pnodes.size();
	pnodes.push_back(pnodes[l] + pnodes[r]);
	pnodes[i].left_child = l;
	pnodes[i].right_child = r;
	return i;
}

int createNode(int v = -1) {
	int i = pnodes.size();
	if (v == -1) pnodes.push_back(PNode());
	else pnodes.push_back(PNode(v));
	return i;
}

int pbuild(int i, int l, int r) {
	if (r - l == 1) return createNode(-1);
	int m = l + r >> 1;
	return createNode(pbuild(i << 1, l, m), pbuild(i << 1 | 1, m, r));
}

int pupdate(int id, int l, int r, int p) {
	if (r - l == 1) return createNode(p);
	int m = l + r >> 1;
	if (p < m) return createNode(pupdate(pnodes[id].left_child, l, m, p), pnodes[id].right_child);
	return createNode(pnodes[id].left_child, pupdate(pnodes[id].right_child, m, r, p));
}

PNode pget(int id, int l, int r, int u, int v) {
	if (l >= v || r <= u || u >= v) return PNode();
	if (u <= l && r <= v) return pnodes[id];
	int m = l + r >> 1;
	return pget(pnodes[id].left_child, l, m, u, v) + pget(pnodes[id].right_child, m, r, u, v);
}

void init(int n, vector<int> h) {
	N = n;
	H = h;
	st.resize(N * 4 + 23);
	build(1, 0, N);
	stack <int> st;
	vector <int> v(N);
	REP(i, N) {
		while (!st.empty() && H[st.top()] >= H[i]) st.pop();
		v[i] = st.empty() ? INF : get(1, 0, N, st.top() + 1, i).ma - H[i];
		st.push(i);
	}
	while (!st.empty()) st.pop();
	REPD(i, N) {
		while (!st.empty() && H[st.top()] >= H[i]) st.pop();
		v[i] = min(v[i], st.empty() ? INF : get(1, 0, N, i + 1, st.top()).ma - H[i]);
		st.push(i);
		val.push_back(v[i]);
	}
	sort(ALL(val));
	val.erase(unique(ALL(val)), val.end());
	vector <vector <int>> add(val.size());
	REP(i, N) add[lower_bound(ALL(val), v[i]) - val.begin()].push_back(i);
	versions.resize(val.size() + 1);
	versions[val.size()] = pbuild(1, 0, N);
	REPD(i, val.size()) {
		versions[i] = versions[i + 1];
		for (int x: add[i]) versions[i] = pupdate(versions[i], 0, N, x);
		}
}

int max_towers(int l, int r, int d) {
	int p = lower_bound(ALL(val), d) - val.begin();
	PNode pn = pget(versions[p], 0, N, l, r + 1);
	int res = pn.cnt;
	int x = pn.mi;
	int y = pn.ma;
		if (!res) {
		x = y = get(1, 0, N, l, r + 1).mi.se;
		++res;
	}
	res += get(1, 0, N, l, x).s_p >= d;
	res += get(1, 0, N, y + 1, r + 1).p_s >= d;
	return res;
}

Compilation message (stderr)

towers.cpp: In constructor 'Node::Node()':
towers.cpp:48:18: warning: 'Node::mi' will be initialized after [-Wreorder]
   48 |  pair <int, int> mi;
      |                  ^~
towers.cpp:47:10: warning:   'int Node::s_p' [-Wreorder]
   47 |  int ma, s_p, p_s;
      |          ^~~
towers.cpp:49:2: warning:   when initialized here [-Wreorder]
   49 |  Node(): ma(-INF), mi(INF, INF), s_p(-INF), p_s(-INF) {}
      |  ^~~~
towers.cpp: In constructor 'Node::Node(int, int)':
towers.cpp:48:18: warning: 'Node::mi' will be initialized after [-Wreorder]
   48 |  pair <int, int> mi;
      |                  ^~
towers.cpp:47:10: warning:   'int Node::s_p' [-Wreorder]
   47 |  int ma, s_p, p_s;
      |          ^~~
towers.cpp:50:2: warning:   when initialized here [-Wreorder]
   50 |  Node(int h, int i): ma(h), mi(h, i), s_p(-INF), p_s(-INF) {}
      |  ^~~~
towers.cpp: In constructor 'PNode::PNode()':
towers.cpp:62:10: warning: 'PNode::ma' will be initialized after [-Wreorder]
   62 |  int mi, ma, cnt, left_child, right_child;
      |          ^~
towers.cpp:62:6: warning:   'int PNode::mi' [-Wreorder]
   62 |  int mi, ma, cnt, left_child, right_child;
      |      ^~
towers.cpp:63:2: warning:   when initialized here [-Wreorder]
   63 |  PNode(): ma(-INF), mi(INF), cnt(0), left_child(-1), right_child(-1) {}
      |  ^~~~~
towers.cpp: In constructor 'PNode::PNode(int)':
towers.cpp:62:10: warning: 'PNode::ma' will be initialized after [-Wreorder]
   62 |  int mi, ma, cnt, left_child, right_child;
      |          ^~
towers.cpp:62:6: warning:   'int PNode::mi' [-Wreorder]
   62 |  int mi, ma, cnt, left_child, right_child;
      |      ^~
towers.cpp:64:2: warning:   when initialized here [-Wreorder]
   64 |  PNode(int v): ma(v), mi(v), cnt(1), left_child(-1), right_child(-1) {}
      |  ^~~~~
towers.cpp: In function 'void build(int, int, int)':
towers.cpp:85:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   85 |  int m = l + r >> 1;
      |          ~~^~~
towers.cpp: In function 'Node get(int, int, int, int, int)':
towers.cpp:94:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   94 |  int m = l + r >> 1;
      |          ~~^~~
towers.cpp: In function 'int pbuild(int, int, int)':
towers.cpp:117:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  117 |  int m = l + r >> 1;
      |          ~~^~~
towers.cpp: In function 'int pupdate(int, int, int, int)':
towers.cpp:123:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  123 |  int m = l + r >> 1;
      |          ~~^~~
towers.cpp: In function 'PNode pget(int, int, int, int, int)':
towers.cpp:131:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  131 |  int m = l + r >> 1;
      |          ~~^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...