結果
問題 | No.2654 [Cherry 6th Tune] Re: start! (Black Sheep) |
ユーザー | Misuki |
提出日時 | 2024-02-25 02:04:29 |
言語 | C++23 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 4,833 bytes |
コンパイル時間 | 2,238 ms |
コンパイル使用メモリ | 210,348 KB |
実行使用メモリ | 47,848 KB |
最終ジャッジ日時 | 2024-09-29 10:36:19 |
合計ジャッジ時間 | 12,046 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 1 ms
5,248 KB |
testcase_03 | AC | 191 ms
13,016 KB |
testcase_04 | AC | 171 ms
12,544 KB |
testcase_05 | AC | 101 ms
8,832 KB |
testcase_06 | AC | 30 ms
5,248 KB |
testcase_07 | AC | 85 ms
7,808 KB |
testcase_08 | AC | 115 ms
9,472 KB |
testcase_09 | AC | 195 ms
13,056 KB |
testcase_10 | AC | 31 ms
5,248 KB |
testcase_11 | AC | 127 ms
10,368 KB |
testcase_12 | AC | 22 ms
5,248 KB |
testcase_13 | AC | 123 ms
9,984 KB |
testcase_14 | AC | 267 ms
15,988 KB |
testcase_15 | AC | 238 ms
15,396 KB |
testcase_16 | AC | 101 ms
8,832 KB |
testcase_17 | AC | 208 ms
13,824 KB |
testcase_18 | AC | 78 ms
7,424 KB |
testcase_19 | AC | 244 ms
15,628 KB |
testcase_20 | AC | 122 ms
9,756 KB |
testcase_21 | AC | 134 ms
10,624 KB |
testcase_22 | AC | 130 ms
10,368 KB |
testcase_23 | AC | 2 ms
5,248 KB |
testcase_24 | AC | 2 ms
5,248 KB |
testcase_25 | AC | 2 ms
5,248 KB |
testcase_26 | AC | 2 ms
5,248 KB |
testcase_27 | AC | 2 ms
5,248 KB |
testcase_28 | AC | 263 ms
16,768 KB |
testcase_29 | AC | 273 ms
16,604 KB |
testcase_30 | AC | 271 ms
16,756 KB |
testcase_31 | AC | 277 ms
16,664 KB |
testcase_32 | AC | 290 ms
16,684 KB |
testcase_33 | AC | 408 ms
47,848 KB |
testcase_34 | AC | 96 ms
17,356 KB |
testcase_35 | WA | - |
testcase_36 | WA | - |
testcase_37 | WA | - |
testcase_38 | WA | - |
testcase_39 | AC | 2 ms
5,248 KB |
ソースコード
#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() > 2) { 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() > 2) { 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; }