結果
問題 | No.2654 [Cherry 6th Tune] Re: start! (Black Sheep) |
ユーザー | Misuki |
提出日時 | 2024-02-25 02:22:42 |
言語 | C++23 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 400 ms / 7,000 ms |
コード長 | 4,954 bytes |
コンパイル時間 | 2,512 ms |
コンパイル使用メモリ | 212,476 KB |
実行使用メモリ | 47,728 KB |
最終ジャッジ日時 | 2024-09-29 10:38:33 |
合計ジャッジ時間 | 12,176 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
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 | 177 ms
13,056 KB |
testcase_04 | AC | 163 ms
12,408 KB |
testcase_05 | AC | 101 ms
8,832 KB |
testcase_06 | AC | 30 ms
5,248 KB |
testcase_07 | AC | 83 ms
7,936 KB |
testcase_08 | AC | 112 ms
9,472 KB |
testcase_09 | AC | 185 ms
13,184 KB |
testcase_10 | AC | 29 ms
5,248 KB |
testcase_11 | AC | 126 ms
10,368 KB |
testcase_12 | AC | 22 ms
5,248 KB |
testcase_13 | AC | 121 ms
9,984 KB |
testcase_14 | AC | 252 ms
16,000 KB |
testcase_15 | AC | 225 ms
15,360 KB |
testcase_16 | AC | 100 ms
8,960 KB |
testcase_17 | AC | 192 ms
13,680 KB |
testcase_18 | AC | 73 ms
7,424 KB |
testcase_19 | AC | 239 ms
15,616 KB |
testcase_20 | AC | 114 ms
9,840 KB |
testcase_21 | AC | 133 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 | 299 ms
16,740 KB |
testcase_29 | AC | 256 ms
16,624 KB |
testcase_30 | AC | 265 ms
16,668 KB |
testcase_31 | AC | 282 ms
16,704 KB |
testcase_32 | AC | 263 ms
16,664 KB |
testcase_33 | AC | 400 ms
47,616 KB |
testcase_34 | AC | 91 ms
17,356 KB |
testcase_35 | AC | 157 ms
16,604 KB |
testcase_36 | AC | 209 ms
47,728 KB |
testcase_37 | AC | 277 ms
16,640 KB |
testcase_38 | AC | 384 ms
47,616 KB |
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 and s.min() != s.max()) { 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 and s.min() != s.max()) { 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); } if (s.size() > 2 and s.min() == s.max()) ans[v] = 1; for(int x : g[v]) if (x != p) self(x, v, self); s.erase(a[v]); }; dfs(0, -1, dfs); dbg(ans); for(int i = 1; i <= n; i++) cout << (ans[i] == LLONG_MAX ? -1 : ans[i]) << '\n'; return 0; }