답안 #920770

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
920770 2024-02-03T03:11:48 Z NK_ The Potion of Great Power (CEOI20_potion) C++17
38 / 100
1002 ms 262144 KB
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>

using namespace std;

#define nl '\n'
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define sz(x) int(x.size())

template<class T> using V = vector<T>;
template<class T> using uset = unordered_set<T>;
template<class T, size_t SZ> using AR = array<T, SZ>;
using vi = V<int>;
using pi = pair<int, int>;
using vpi = V<pi>;

const int INF = 1e9;
int N, D; vi H;

struct ST { 

	int N; V<vi> X; V<vpi> E; V<V<vi>> adj;
	void init(int n) {
		N = 1; while(N < n) N *= 2;

		X.assign(2*N, {});
		E.assign(2*N, {});
		adj.assign(2*N, {});
	}

	void add(int l, int r, pi e, int x, int L, int R) {
		if (r < L || R < l) return;
		if (l <= L && R <= r) { E[x].pb(e); return; }
		int M = (L + R) / 2;
		add(l, r, e, 2 * x, L, M);
		add(l, r, e, 2 * x + 1, M + 1, R);
	}

	void build() {
		for(int i = 1; i < 2 * N; i++) {
			for(auto& e : E[i]) {
				X[i].pb(e.f); X[i].pb(e.s);
			}

			sort(begin(X[i]), end(X[i])); X[i].erase(unique(begin(X[i]), end(X[i])), end(X[i]));

			int K = sz(X[i]);
			adj[i] = V<vi>(K);

			while(sz(E[i])) {
				int u, v; tie(u, v) = E[i].back(); E[i].pop_back();
				u = lower_bound(begin(X[i]), end(X[i]), u) - begin(X[i]);
				v = lower_bound(begin(X[i]), end(X[i]), v) - begin(X[i]);
				adj[i][u].pb(v);
				adj[i][v].pb(u);
			}
		}
	}

	void query(int d, int p, vi& F, int x, int L, int R) {

		int px = lower_bound(begin(X[x]), end(X[x]), p) - begin(X[x]);

		if (px < sz(X[x]) && X[x][px] == p) {
			for(auto& nx : adj[x][px]) {
				// cout << L << " " << R << " => " << X[x][nx] << endl;
				F.pb(X[x][nx]);
			}
		}

		if (L == R) return;

		int M = (L + R) / 2;
		if (d <= M) query(d, p, F, 2 * x, L, M);
		else query(d, p, F, 2 * x + 1, M + 1, R);
	}

	void add(int l, int r, pi e) { add(l, r, e, 1, 0, N - 1); }
	void query(int d, int p, vi& F) { query(d, p, F, 1, 0, N - 1); }
} st;

void init(int n, int d, int h[]) {
	N = n, D = d; H.resize(N);
	for(int i = 0; i < N; i++) H[i] = h[i];
}

void curseChanges(int U, int A[], int B[]) {
	st.init(U);

	map<pi, int> C;
	for(int i = 0; i < U; i++) {
		if (A[i] < B[i]) swap(A[i], B[i]);
		pi e = mp(A[i], B[i]);
		if (C.find(e) == C.end()) C[e] = i;
		else {
			// cout << C[e] << " " << i - 1 << " => " << e.f << " " << e.s << endl;
			st.add(C[e], i - 1, e);
			C.erase(e);
		}
	}

	for(auto& p : C) {
		st.add(p.s, U - 1, p.f);
		// cout << p.s << " " << U - 1 << " => " << p.f.f << " " << p.f.s << endl;
	}

	st.build();
}

int question(int x, int y, int v) {
	--v;

	vi FX, FY;
	// cout << "x: ";
	st.query(v, x, FX);
	// cout << endl;

	// cout << "y: ";
	st.query(v, y, FY);
	// cout << endl;

	vpi C;
	// cout << "X: ";
	for(auto& c : FX) {
		C.pb(mp(H[c], 0));
		// cout << c << " ";
	}
	// cout << nl;

	// cout << "Y: ";
	for(auto& c : FY) {
		C.pb(mp(H[c], 1));
		// cout << c << " ";
	}
	// cout << nl;

	sort(begin(C), end(C));

	int ans = INF;
	for(int i = 0; i + 1 < sz(C); i++) if (C[i].s != C[i + 1].s) ans = min(ans, abs(C[i].f - C[i + 1].f));

	return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 1368 KB Output is correct
2 Correct 4 ms 1368 KB Output is correct
3 Correct 5 ms 1368 KB Output is correct
4 Correct 13 ms 2648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1002 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1002 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 140 ms 14244 KB Output is correct
2 Correct 147 ms 8676 KB Output is correct
3 Correct 157 ms 7256 KB Output is correct
4 Correct 820 ms 11440 KB Output is correct
5 Correct 882 ms 12984 KB Output is correct
6 Correct 211 ms 11564 KB Output is correct
7 Correct 667 ms 8708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 4 ms 1368 KB Output is correct
3 Correct 4 ms 1368 KB Output is correct
4 Correct 5 ms 1368 KB Output is correct
5 Correct 13 ms 2648 KB Output is correct
6 Runtime error 1002 ms 262144 KB Execution killed with signal 9
7 Halted 0 ms 0 KB -