제출 #1295508

#제출 시각아이디문제언어결과실행 시간메모리
1295508lrnnzNile (IOI24_nile)C++20
38 / 100
67 ms13036 KiB
#include <bits/stdc++.h> #include <iostream> #include <vector> #include <algorithm> #include <cmath> #include <iomanip> #include <queue> #include <random> #include "nile.h" using namespace std; #define all(a) (a).begin(), (a).end() #define ll long long #define ld long double #define ui __int128_t #define cont(set, element) ((set).find(element) != (set).end()) #define pb push_back template<class T, class U> inline bool chmin(T& a, const U& b) { if (a > b) { a = b; return true; } return false; } template<class T, class U> inline bool chmax(T& a, const U& b) { if (a < b) { a = b; return true; } return false; } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); /********* DEBUG *********/ template <typename T> void outvec(const vector<T>& Z){ for (const T& x : Z) cout << x << ' '; cout << "\n"; } void printVariable(const any& var) { if (!var.has_value()) { cout << "null"; return; } if (var.type() == typeid(int)) { cout << any_cast<int>(var); } else if (var.type() == typeid(double)) { cout << any_cast<double>(var); } else if (var.type() == typeid(float)) { cout << any_cast<float>(var); } else if (var.type() == typeid(char)) { cout << any_cast<char>(var); } else if (var.type() == typeid(bool)) { cout << (any_cast<bool>(var) ? "true" : "false"); } else if (var.type() == typeid(string)) { cout << any_cast<string>(var); } else if (var.type() == typeid(const char*)) { cout << any_cast<const char*>(var); } else if (var.type() == typeid(long long)) { cout << any_cast<long long>(var); } else { cout << "[unknown type]"; } } template<typename... Args> void outval(Args... args) { vector<any> variables = {args...}; for (size_t i = 0; i < variables.size(); ++i) { printVariable(variables[i]); if (i != variables.size() - 1) { cout << " "; } } cout << "\n"; } /********* DEBUG *********/ #define sp << " " << #define fi first #define se second const ll MOD2 = 1e9 + 7; const ll MOD = 998244353; const ll inf = 1e18; ll madd(ll a, ll b) { a += b; if (a >= MOD) a -= MOD; return a; } ll msub(ll a, ll b) { a -= b; if (a < 0) a += MOD; return a ; } ll mmul(ll a, ll b) { return 1ll * a * b % MOD; } ll mpow(ll a, ll b) { ll ans = 1; for (; b; b >>= 1, a = mmul(a, a)) if (b & 1) ans = mmul(ans, a); return ans; } vector<ll> calculate_costs(vector<int> W, vector<int> A, vector<int> B, vector<int> E) { ll Q = E.size(), n = W.size(); vector<ll> ans(Q), ord(n); iota(all(ord), 0); sort(all(ord), [&](ll i, ll j) { return W[i] < W[j]; }); // diff, i vector<pair<ll,ll>> teams; for (int i = 0; i+1 < n; i++) { ll a = W[ord[i]]; ll b = W[ord[i+1]]; teams.pb({b-a, i}); if (i+2 < n) { ll c = W[ord[i+2]]; teams.pb({c-a, ~i}); } } sort(all(teams)); vector<ll> ordQ(Q); iota(all(ordQ), 0); sort(all(ordQ), [&](ll i, ll j) { return E[i] < E[j]; }); vector<ll> par(n), sz(n, 1); iota(all(par), 0); // lowest extra per parity vector<vector<ll>> lo(2, vector<ll>(n, inf)); for (int i = 0; i < n; i++) { lo[i%2][i] = A[ord[i]] - B[ord[i]]; } auto find = [&](auto &&find, ll u) -> ll { if (par[u] != u) par[u] = find(find, par[u]); return par[u]; }; ll sumB = 0, xtra = 0; auto unite = [&](ll a, ll b) -> void { a = find(find, a); b = find(find, b); assert(a != b); if (a == b) return; if (a > b) swap(a, b); par[b] = a; // implement exclusion if b or a in extra rn if (sz[a] % 2) xtra -= lo[a%2][a]; if (sz[b] % 2) xtra -= lo[b%2][b]; sz[a] += sz[b]; ll d = (b-a)%2; for (int i = 0; i < 2; i++) { chmin(lo[i][a], lo[i^d][b]); } if (sz[a] % 2) { // add to xtra xtra += lo[a%2][a]; } }; for (int i = 0; i < n; i++) { xtra += A[i] - B[i]; sumB += B[i]; } ll p = 0, m = teams.size(); for (int i = 0; i < Q; i++) { ll cq = E[ordQ[i]]; while (p < m && teams[p].fi <= cq) { // implement sweeping auto [v, j] = teams[p++]; if (j >= 0) { unite(j, j+1); } else { j = ~j; ll par = find(find, j); ll cand = A[ord[j+1]] - B[ord[j+1]]; if (sz[par]%2) xtra -= lo[par%2][par]; chmin(lo[0][par], cand); chmin(lo[1][par], cand); if (sz[par]%2) xtra += lo[par%2][par]; } } ans[ordQ[i]] = sumB + xtra; } return ans; }
#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...