Submission #769250

# Submission time Handle Problem Language Result Execution time Memory
769250 2023-06-29T10:48:37 Z marvinthang Comparing Plants (IOI20_plants) C++17
100 / 100
957 ms 69532 KB
/*************************************
*    author: marvinthang             *
*    created: 29.06.2023 15:49:29    *
*************************************/

#include "plants.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 << '}'; }

// end of template

const int INF = 1e9;

int N, K;
vector <vector <bool>> visited;
vector <vector<int>> st_left, st_right;
vector <int> st, lazy, h;

int get_prev(int i, int j) {
	i -= j;
	return i < 0 ? i + N : i;
}

int get_next(int i, int j) {
	i += j;
	return i >= N ? i - N : i;
}

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

void pushDown(int i) {
	if (!lazy[i]) return;
	st[i << 1] += lazy[i];
	lazy[i << 1] += lazy[i];
	st[i << 1 | 1] += lazy[i];
	lazy[i << 1 | 1] += lazy[i];
	lazy[i] = 0;
}

int find_zero(void) {
	int i = 1, l = 0, r = N;
	while (true) {
		if (r - l == 1) return l;
		pushDown(i);
		int m = l + r >> 1;
		if (!st[i << 1]) i = i << 1, r = m;
		else i = i << 1 | 1, l = m;
	}
	return -1;
}

void update(int i, int l, int r, int u, int v, int add) {
	if (l >= v || r <= u) return;
	if (u <= l && r <= v) {
		st[i] += add;
		lazy[i] += add;
		return;
	}
	pushDown(i);
	int m = l + r >> 1;
	update(i << 1, l, m, u, v, add);
	update(i << 1 | 1, m, r, u, v, add);
	st[i] = min(st[i << 1], st[i << 1 | 1]);
}

int dist(int i, int j) {
	return i < j ? j - i : j + N - i;
}

void init(int k, vector <int> r) {
	K = k; --K;
	N = r.size();
	st.resize(N * 4 + 23);
	lazy.resize(N * 4 + 23);
	build(1, 0, N, r);
	h.resize(N);
	vector <vector <int>> adj(N);
	set <int> pos_zero, good_zero;
	REPD(v, N) {
		while (!st[1]) {
			int p = find_zero();
			update(1, 0, N, p, p + 1, INF);
			auto it = pos_zero.insert(p).fi;
			if (pos_zero.size() == 1) {
				good_zero.insert(p);
				continue;
			}
			int prv = it == pos_zero.begin() ? *pos_zero.rbegin() : *prev(it);
			int nxt = next(it) == pos_zero.end() ? *pos_zero.begin() : *next(it);
			if (dist(prv, p) > K) good_zero.insert(p);
			if (dist(p, nxt) <= K) good_zero.erase(nxt);
		}
		int i = *good_zero.begin();
		good_zero.erase(good_zero.begin());
		auto it = pos_zero.find(i);
		int prv = it == pos_zero.begin() ? *pos_zero.rbegin() : *prev(it);
		int nxt = next(it) == pos_zero.end() ? *pos_zero.begin() : *next(it);
		pos_zero.erase(it);
		if (!pos_zero.empty() && dist(prv, nxt) > K) good_zero.insert(nxt);

		h[i] = v;
		prv = get_prev(i, K);
		if (prv < i) update(1, 0, N, prv, i, -1);
		else {
			update(1, 0, N, prv, N, -1);
			update(1, 0, N, 0, i, -1);
		}
	}
	set <pair <int, int>> left, right;
	REP(i, K) {
		int x = get_prev(0, i + 1);
		left.insert(make_pair(h[x], x));
		x = get_next(0, i + 1);
		right.insert(make_pair(h[x], x));
	}
	st_left.push_back(vector<int>(N, INF));
	st_right.push_back(vector<int>(N, INF));
	REP(i, N) {
		auto it = left.lower_bound(make_pair(h[i], i));
		if (it != left.begin()) st_left[0][i] = dist(prev(it)->se, i);
		it = right.lower_bound(make_pair(h[i], i));
		if (it != right.begin()) st_right[0][i] = dist(i, prev(it)->se);
		int x = get_prev(i, K);
		left.erase(make_pair(h[x], x));
		left.insert(make_pair(h[i], i));

		x = get_next(i, 1);
		right.erase(make_pair(h[x], x));
		x = get_next(i, K + 1);
		right.insert(make_pair(h[x], x));
	}

	for (int k = 1; MASK(k) <= N; ++k) {
		vector <int> nl, nr;
		REP(i, N) {
			int x = st_left.back()[i];
			nl.push_back(x >= N ? INF : min(st_left.back()[get_prev(i, x)] + x, INF));
			x = st_right.back()[i];
			nr.push_back(x >= N ? INF : min(st_right.back()[get_next(i, x)] + x, INF));
		}
		st_left.push_back(nl);
		st_right.push_back(nr);
	}
}

bool can_go(int x, int y) {
	int d = dist(y, x);
	int u = x;
	REPD(i, st_left.size()) {
		int x = st_left[i][u];
		if (x <= d) {
			d -= x;
			u = get_prev(u, x);
		}
	}
	if (d <= K && h[u] >= h[y]) return true;
	d = dist(x, y);
	u = x;
	REPD(i, st_right.size()) {
		int x = st_right[i][u];
		if (x <= d) {
			d -= x;
			u = get_next(u, x);
		}
	}
	if (d <= K && h[u] >= h[y]) return true;
	return false;
}

int compare_plants(int x, int y) {
	if (can_go(x, y)) return 1;
	if (can_go(y, x)) return -1;
	return 0;
}

Compilation message

plants.cpp: In function 'void build(int, int, int, std::vector<int>&)':
plants.cpp:68:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   68 |  int m = l + r >> 1;
      |          ~~^~~
plants.cpp: In function 'int find_zero()':
plants.cpp:88:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   88 |   int m = l + r >> 1;
      |           ~~^~~
plants.cpp: In function 'void update(int, int, int, int, int, int)':
plants.cpp:103:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  103 |  int m = l + r >> 1;
      |          ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 44 ms 3044 KB Output is correct
7 Correct 90 ms 6836 KB Output is correct
8 Correct 339 ms 47736 KB Output is correct
9 Correct 354 ms 47316 KB Output is correct
10 Correct 455 ms 47324 KB Output is correct
11 Correct 411 ms 47676 KB Output is correct
12 Correct 505 ms 47108 KB Output is correct
13 Correct 412 ms 46720 KB Output is correct
14 Correct 460 ms 46716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 3 ms 596 KB Output is correct
7 Correct 60 ms 6248 KB Output is correct
8 Correct 2 ms 312 KB Output is correct
9 Correct 3 ms 596 KB Output is correct
10 Correct 61 ms 5964 KB Output is correct
11 Correct 66 ms 5756 KB Output is correct
12 Correct 88 ms 5864 KB Output is correct
13 Correct 59 ms 6092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 3 ms 596 KB Output is correct
7 Correct 60 ms 6248 KB Output is correct
8 Correct 2 ms 312 KB Output is correct
9 Correct 3 ms 596 KB Output is correct
10 Correct 61 ms 5964 KB Output is correct
11 Correct 66 ms 5756 KB Output is correct
12 Correct 88 ms 5864 KB Output is correct
13 Correct 59 ms 6092 KB Output is correct
14 Correct 98 ms 9484 KB Output is correct
15 Correct 843 ms 62224 KB Output is correct
16 Correct 98 ms 9808 KB Output is correct
17 Correct 925 ms 61636 KB Output is correct
18 Correct 564 ms 59292 KB Output is correct
19 Correct 646 ms 59368 KB Output is correct
20 Correct 957 ms 69532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 85 ms 3424 KB Output is correct
4 Correct 519 ms 48900 KB Output is correct
5 Correct 574 ms 50224 KB Output is correct
6 Correct 605 ms 50292 KB Output is correct
7 Correct 609 ms 51256 KB Output is correct
8 Correct 782 ms 59132 KB Output is correct
9 Correct 545 ms 50244 KB Output is correct
10 Correct 621 ms 50172 KB Output is correct
11 Correct 456 ms 49676 KB Output is correct
12 Correct 471 ms 50040 KB Output is correct
13 Correct 525 ms 55540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 14 ms 884 KB Output is correct
8 Correct 11 ms 980 KB Output is correct
9 Correct 15 ms 1288 KB Output is correct
10 Correct 15 ms 1228 KB Output is correct
11 Correct 14 ms 1236 KB Output is correct
12 Correct 14 ms 1280 KB Output is correct
13 Correct 11 ms 1276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 490 ms 49408 KB Output is correct
7 Correct 465 ms 49356 KB Output is correct
8 Correct 480 ms 50392 KB Output is correct
9 Correct 653 ms 58176 KB Output is correct
10 Correct 424 ms 49320 KB Output is correct
11 Correct 532 ms 57208 KB Output is correct
12 Correct 380 ms 48952 KB Output is correct
13 Correct 460 ms 49332 KB Output is correct
14 Correct 438 ms 49428 KB Output is correct
15 Correct 514 ms 50456 KB Output is correct
16 Correct 359 ms 49272 KB Output is correct
17 Correct 433 ms 49400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 44 ms 3044 KB Output is correct
7 Correct 90 ms 6836 KB Output is correct
8 Correct 339 ms 47736 KB Output is correct
9 Correct 354 ms 47316 KB Output is correct
10 Correct 455 ms 47324 KB Output is correct
11 Correct 411 ms 47676 KB Output is correct
12 Correct 505 ms 47108 KB Output is correct
13 Correct 412 ms 46720 KB Output is correct
14 Correct 460 ms 46716 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 0 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 3 ms 596 KB Output is correct
21 Correct 60 ms 6248 KB Output is correct
22 Correct 2 ms 312 KB Output is correct
23 Correct 3 ms 596 KB Output is correct
24 Correct 61 ms 5964 KB Output is correct
25 Correct 66 ms 5756 KB Output is correct
26 Correct 88 ms 5864 KB Output is correct
27 Correct 59 ms 6092 KB Output is correct
28 Correct 98 ms 9484 KB Output is correct
29 Correct 843 ms 62224 KB Output is correct
30 Correct 98 ms 9808 KB Output is correct
31 Correct 925 ms 61636 KB Output is correct
32 Correct 564 ms 59292 KB Output is correct
33 Correct 646 ms 59368 KB Output is correct
34 Correct 957 ms 69532 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 85 ms 3424 KB Output is correct
38 Correct 519 ms 48900 KB Output is correct
39 Correct 574 ms 50224 KB Output is correct
40 Correct 605 ms 50292 KB Output is correct
41 Correct 609 ms 51256 KB Output is correct
42 Correct 782 ms 59132 KB Output is correct
43 Correct 545 ms 50244 KB Output is correct
44 Correct 621 ms 50172 KB Output is correct
45 Correct 456 ms 49676 KB Output is correct
46 Correct 471 ms 50040 KB Output is correct
47 Correct 525 ms 55540 KB Output is correct
48 Correct 0 ms 212 KB Output is correct
49 Correct 0 ms 212 KB Output is correct
50 Correct 0 ms 212 KB Output is correct
51 Correct 0 ms 212 KB Output is correct
52 Correct 1 ms 212 KB Output is correct
53 Correct 2 ms 340 KB Output is correct
54 Correct 14 ms 884 KB Output is correct
55 Correct 11 ms 980 KB Output is correct
56 Correct 15 ms 1288 KB Output is correct
57 Correct 15 ms 1228 KB Output is correct
58 Correct 14 ms 1236 KB Output is correct
59 Correct 14 ms 1280 KB Output is correct
60 Correct 11 ms 1276 KB Output is correct
61 Correct 67 ms 5068 KB Output is correct
62 Correct 101 ms 8668 KB Output is correct
63 Correct 468 ms 49996 KB Output is correct
64 Correct 563 ms 50928 KB Output is correct
65 Correct 608 ms 50320 KB Output is correct
66 Correct 673 ms 51268 KB Output is correct
67 Correct 839 ms 59016 KB Output is correct
68 Correct 537 ms 49980 KB Output is correct
69 Correct 618 ms 58152 KB Output is correct
70 Correct 548 ms 51136 KB Output is correct
71 Correct 677 ms 50068 KB Output is correct
72 Correct 604 ms 50248 KB Output is correct
73 Correct 585 ms 51284 KB Output is correct
74 Correct 476 ms 49852 KB Output is correct
75 Correct 513 ms 50892 KB Output is correct