Submission #1207658

#TimeUsernameProblemLanguageResultExecution timeMemory
1207658viduxNile (IOI24_nile)C++17
0 / 100
22 ms5444 KiB
#include <bits/stdc++.h>
using namespace std;

//#define LOCAL

#define FOR(i, n) for (int i = 0; i < n; ++i)
#define REP(i, n, m) for (int i = n; i <= m; ++i)
#define REPR(i, n, m) for (int i = n; i >= m; --i)
#define FORR(x, a) for (auto& x : a)
#define FORR2(x, y, a) for (auto& [x, y] : a)
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define SZ(a) ((int)a.size())
#define fi first
#define se second

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;

const int INF = 1e9;
const ll LLINF = 1e18;

struct DSU {
	vi sz, par;
	DSU(int n) {
		sz = vi(n, 1);
		par = vi(n);
		FOR(i, n) par[i] = i;
	}
	int find(int x) {
		return par[x] = ((par[x] == x) ? x : find(par[x]));
	}
	bool join(int x, int y) {
		if (find(x) == find(y)) return false;
		if (sz[find(y)] > sz[find(x)]) swap(x, y); 
		sz[find(x)] += sz[find(y)];
		par[find(y)] = find(x);
		return true;
	}
};

vl calculate_costs(vi W, vi A, vi B, vi E) {
	int n = SZ(W);
	int q = SZ(E);
	vector<pii> Q(q);
	FOR(i, q) Q[i] = {E[i], i};
	sort(ALL(Q));
	vector<pair<int, pii>> arr(n);
	FOR(i, n) arr[i] = {W[i], {A[i], B[i]}};
	vector<pii> D(n-1);
	FOR(i, n-1) D[i] = {abs(arr[i].first-arr[i+1].first), i}; 
	sort(RALL(D));
	int d = 0;
	ll cur = 2*n;
	vl ans(q);
	DSU dsu(n);
	FOR(t, q) {
		d = Q[t].fi;
		while (SZ(D) && D.back().fi <= d) {
			int alone = (dsu.sz[dsu.find(D.back().se)] & 1) + (dsu.sz[dsu.find(D.back().se+1)] & 1);
			if (alone == 2) cur -= 2; 
			D.pop_back();
		}
		ans[Q[t].se] = cur;
	}
	return ans;
}

#ifdef LOCAL
int main() {
	vl ans = calculate_costs(
		{15, 12, 2, 10, 21},
		{2, 2, 2, 2, 2},
		{1, 1, 1, 1, 1},
		{5, 9, 1}
	);
	FORR(x, ans) cout << x << endl;
    return 0;
}
#endif

#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...
#Verdict Execution timeMemoryGrader output
Fetching results...