結果
問題 | No.2654 [Cherry 6th Tune] Re: start! (Black Sheep) |
ユーザー | Misuki |
提出日時 | 2024-02-25 02:16:57 |
言語 | C++23 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 4,833 bytes |
コンパイル時間 | 3,003 ms |
コンパイル使用メモリ | 210,248 KB |
実行使用メモリ | 47,800 KB |
最終ジャッジ日時 | 2024-09-29 10:38:18 |
合計ジャッジ時間 | 11,649 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | WA | - |
testcase_01 | WA | - |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | WA | - |
testcase_30 | WA | - |
testcase_31 | WA | - |
testcase_32 | WA | - |
testcase_33 | WA | - |
testcase_34 | AC | 103 ms
17,352 KB |
testcase_35 | WA | - |
testcase_36 | WA | - |
testcase_37 | WA | - |
testcase_38 | WA | - |
testcase_39 | WA | - |
ソースコード
#pragma GCC optimize("O2") #include <algorithm> #include <array> #include <bit> #include <bitset> #include <cassert> #include <cctype> #include <cfenv> #include <cfloat> #include <chrono> #include <cinttypes> #include <climits> #include <cmath> #include <compare> #include <complex> #include <concepts> #include <cstdarg> #include <cstddef> #include <cstdint> #include <cstdio> #include <cstdlib> #include <cstring> #include <deque> #include <fstream> #include <functional> #include <initializer_list> #include <iomanip> #include <ios> #include <iostream> #include <istream> #include <iterator> #include <limits> #include <list> #include <map> #include <memory> #include <new> #include <numbers> #include <numeric> #include <ostream> #include <queue> #include <random> #include <ranges> #include <set> #include <span> #include <sstream> #include <stack> #include <streambuf> #include <string> #include <tuple> #include <type_traits> #include <variant> //#define int ll #define INT128_MAX (__int128)(((unsigned __int128) 1 << ((sizeof(__int128) * __CHAR_BIT__) - 1)) - 1) #define INT128_MIN (-INT128_MAX - 1) #define clock chrono::steady_clock::now().time_since_epoch().count() #ifdef DEBUG #define dbg(x) cout << (#x) << " = " << x << '\n' #else #define dbg(x) #endif namespace R = std::ranges; namespace V = std::views; using namespace std; using ll = long long; using ull = unsigned long long; using ldb = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; //#define double ldb template<class T> ostream& operator<<(ostream& os, const pair<T, T> pr) { return os << pr.first << ' ' << pr.second; } template<class T, size_t N> ostream& operator<<(ostream& os, const array<T, N> &arr) { for(const T &X : arr) os << X << ' '; return os; } template<class T> ostream& operator<<(ostream& os, const vector<T> &vec) { for(const T &X : vec) os << X << ' '; return os; } template<class T> ostream& operator<<(ostream& os, const set<T> &s) { for(const T &x : s) os << x << ' '; return os; } template<class T1, class T2> struct dynamicMedian { multiset<T1> left, right; T2 lsum = 0, rsum = 0; void insert(T1 x) { left.insert(x); lsum += x; if (!right.empty() and (*left.rbegin()) > (*right.begin())) { T1 tmpl = *left.rbegin(); left.erase(prev(left.end())); T1 tmpr = *right.begin(); right.erase(right.begin()); left.insert(tmpr); right.insert(tmpl); lsum += tmpr - tmpl, rsum += tmpl - tmpr; } if (ssize(left) > ssize(right) + 1) { T1 tmp = *left.rbegin(); left.erase(prev(left.end())); right.insert(tmp); lsum -= tmp, rsum += tmp; } } void erase(T1 x) { if (left.contains(x)) { left.erase(left.find(x)); lsum -= x; if (ssize(left) < ssize(right)) { T1 tmp = *right.begin(); right.erase(right.begin()); left.insert(tmp); lsum += tmp, rsum -= tmp; } } else if (right.contains(x)) { right.erase(right.find(x)); rsum -= x; if (ssize(left) == ssize(right) + 2) { T1 tmp = *left.rbegin(); left.erase(prev(left.end())); right.insert(tmp); lsum -= tmp, rsum += tmp; } } } T1 median() { return *left.rbegin(); } T1 min() { return *left.begin(); } T1 max() { return right.empty() ? *left.rbegin() : *right.rbegin(); } int size() { return ssize(left) + ssize(right); } void print() { cout << "left: "; for(T1 x : left) cout << x << ' '; cout << '\n'; cout << "right: "; for(T2 x : right) cout << x << ' '; cout << '\n'; } }; signed main() { ios::sync_with_stdio(false), cin.tie(NULL); int n; cin >> n; vector<int> a(n + 1); for(int &x : a) cin >> x; vector<vector<int>> g(n + 1); for(int i = 0; i < n; i++) { int u, v; cin >> u >> v; g[u].emplace_back(v); g[v].emplace_back(u); } dynamicMedian<int, ll> s; vector<ll> ans(n + 1, LLONG_MAX); auto dfs = [&](int v, int p, auto self) -> void { s.insert(a[v]); dbg(v); //s.print(); if (s.size() > 3) { int mn = s.min(); s.erase(mn); int med = s.median(); if (med == mn) med++; ans[v] = min(ans[v], (ssize(s.left) - ssize(s.right)) * med - s.lsum + s.rsum); dbg(mn); dbg(med); s.insert(mn); } if (s.size() > 3) { int mx = s.max(); s.erase(mx); int med = s.median(); if (med == mx) med--; ans[v] = min(ans[v], (ssize(s.left) - ssize(s.right)) * med - s.lsum + s.rsum); dbg(mx); dbg(med); s.insert(mx); } for(int x : g[v]) if (x != p) self(x, v, self); s.erase(a[v]); }; dfs(0, -1, dfs); for(int i = 1; i <= n; i++) cout << (ans[i] == LLONG_MAX ? -1 : ans[i]) << '\n'; return 0; }