답안 #817656

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
817656 2023-08-09T14:39:48 Z hugo_pm 식물 비교 (IOI20_plants) C++17
55 / 100
310 ms 17848 KB
#include "plants.h"
#include <bits/stdc++.h>
using namespace std;

#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define sz(v) ((int)((v).size()))

template<typename T>
void chmax(T &x, const T &v) { if (x < v) x = v; }
template<typename T>
void chmin(T &x, const T &v) { if (x > v) x = v; }

using pii = pair<int, int>;
using vi = vector<int>;

string to_string(string s) { return s; }
template <typename T> string to_string(T v) {
	bool first = true;
	string res = "[";
	for (const auto &x : v) {
		if (!first)
			res += ", ";
		first = false;
		res += to_string(x);
	}
	res += "]";
	return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
	cout << ' ' << to_string(H);
	dbg_out(T...);
}

#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

const int INF = 1e9;
struct MinAdd {
	inline int op(int a, int b) { return min(a, b); }
	const int e = INF;
	int N;
	vector<int> tree, lazy;
	void init(vector<int> v) {
		N = 1;
		while (N < (int)v.size()) N <<= 1;
		tree.assign(2*N, e);
		lazy.assign(2*N, 0);
		for (int i = 0; i < (int)v.size(); ++i) {
			tree[N+i] = v[i];
		}
		for (int i = N-1; i >= 1; --i) {
			tree[i] = op(tree[2*i], tree[2*i+1]);
		}
	}
	void push(int node) {
		tree[node] += lazy[node];
		if (node < N) {
			lazy[2*node] += lazy[node];
			lazy[2*node+1] += lazy[node];
		}
		lazy[node] = 0;
	}
	void _add(int lq, int rq, int delta) {
		auto F = [&] (auto f, int node, int lno, int rno) -> void {
			push(node);
			if (lq <= lno && rno <= rq) {
				lazy[node] += delta;
				push(node);
			} else if (lno <= rq && lq <= rno) {
				int mid = (lno+rno)/2;
				f(f, 2*node, lno, mid);
				f(f, 2*node+1, mid+1, rno);
				tree[node] = op(tree[2*node], tree[2*node+1]);
			}
		};
		F(F, 1, 0, N-1);
	}
	optional<int> _popZero(int lq, int rq) {
		optional<int> ret = nullopt;
		auto F = [&] (auto f, int node, int lno, int rno) {
			push(node);
			if (ret || tree[node] > 0 || rno < lq || rq < lno) {
				return;
			} else if (node >= N) {
				tree[node] = e;
				ret = node - N;
			} else {
				int mid = (lno+rno)/2;
				f(f, 2*node, lno, mid);
				f(f, 2*node+1, mid+1, rno);
				tree[node] = op(tree[2*node], tree[2*node+1]);
			}
		};
		F(F, 1, 0, N-1);
		return ret;
	}
	optional<int> circleGet(int L, int R) {
		if (L > R) {
			auto a = _popZero(L, N-1);
			return (a ? a : _popZero(0, R));
		}
		return _popZero(L, R);
	}
	void circleDecr(int L, int R) {
		if (L > R) {
			_add(L, N-1, -1);
			_add(0, R, -1);
		} else {
			_add(L, R, -1);
		}
	}
};
MinAdd util;

int N, K;
int add(int x, int y) {
	x += y;
	if (x >= N) x -= N;
	return x;
}
int sub(int x, int y) {
	x -= y;
	if (x < 0) x += N;
	return x;
}
int dist(int x, int y) {
	return min(sub(x,y), sub(y,x));
}

vector<int> relative, height;
int insertHeight;
const int MAX_GRAPH = 300;
bool graph[MAX_GRAPH][MAX_GRAPH];

bool pop(int L, int R) {
	optional<int> ret = util.circleGet(L, R);
	if (!ret) return false;
	for (; ret; ret = util.circleGet(L, R)) {
		int cur = *ret;
		pop(sub(cur,K-1), sub(cur, 1));
		height[cur] = insertHeight--;
		util.circleDecr(sub(cur,K-1), sub(cur,1));
	}
	return true;
}

void init(int _k, std::vector<int> _r) {
	K = _k;
	relative = _r;
	N = _r.size();
	//--
	util.init(relative);
	height.assign(N, -1);
	insertHeight = N-1;
	while(pop(0, N-1));
	dbg(height);
	if (N <= MAX_GRAPH) {
		rep(i, 0, N) rep(j, 0, N) {
			graph[i][j] = (dist(i,j) < K && height[i] > height[j]);
		}
		rep(k, 0, N) rep(i, 0, N) rep(j, 0, N) {
			graph[i][j] |= (graph[i][k] && graph[k][j]);
		}
	}
}

int compare_plants(int x, int y) {
	int xy = (height[x] > height[y]);
	int yx = (height[y] > height[x]);
	if (N <= MAX_GRAPH) {
		xy = graph[x][y], yx = graph[y][x];
	}
	return xy - yx;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 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 300 KB Output is correct
6 Correct 40 ms 4012 KB Output is correct
7 Incorrect 54 ms 6060 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 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 224 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 43 ms 5196 KB Output is correct
8 Correct 3 ms 340 KB Output is correct
9 Correct 2 ms 464 KB Output is correct
10 Correct 48 ms 5136 KB Output is correct
11 Correct 43 ms 5020 KB Output is correct
12 Correct 42 ms 5344 KB Output is correct
13 Correct 51 ms 5232 KB Output is correct
# 결과 실행 시간 메모리 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 224 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 43 ms 5196 KB Output is correct
8 Correct 3 ms 340 KB Output is correct
9 Correct 2 ms 464 KB Output is correct
10 Correct 48 ms 5136 KB Output is correct
11 Correct 43 ms 5020 KB Output is correct
12 Correct 42 ms 5344 KB Output is correct
13 Correct 51 ms 5232 KB Output is correct
14 Correct 61 ms 6180 KB Output is correct
15 Correct 303 ms 13616 KB Output is correct
16 Correct 60 ms 6168 KB Output is correct
17 Correct 291 ms 13616 KB Output is correct
18 Correct 310 ms 13216 KB Output is correct
19 Correct 218 ms 13676 KB Output is correct
20 Correct 277 ms 13660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 300 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 42 ms 4944 KB Output is correct
4 Correct 211 ms 17848 KB Output is correct
5 Correct 223 ms 13416 KB Output is correct
6 Correct 254 ms 13232 KB Output is correct
7 Correct 289 ms 13276 KB Output is correct
8 Correct 286 ms 13572 KB Output is correct
9 Correct 224 ms 13256 KB Output is correct
10 Correct 223 ms 12876 KB Output is correct
11 Correct 229 ms 12804 KB Output is correct
12 Correct 190 ms 13004 KB Output is correct
13 Correct 293 ms 13032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 308 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 28 ms 1248 KB Output is correct
8 Correct 29 ms 1236 KB Output is correct
9 Correct 37 ms 1252 KB Output is correct
10 Correct 40 ms 1240 KB Output is correct
11 Correct 31 ms 1244 KB Output is correct
12 Correct 28 ms 1332 KB Output is correct
13 Correct 30 ms 1240 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 300 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Incorrect 1 ms 340 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 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 300 KB Output is correct
6 Correct 40 ms 4012 KB Output is correct
7 Incorrect 54 ms 6060 KB Output isn't correct
8 Halted 0 ms 0 KB -