QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#104637 | #6322. Forestry | HgCl2 | TL | 1ms | 10476kb | C++14 | 2.5kb | 2023-05-11 14:19:49 | 2023-05-11 14:19:51 |
Judging History
answer
#include <algorithm>
#include <array>
#include <cstddef>
#include <cstdint>
#include <fstream>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
namespace {
using std::cin;
using std::cout;
using std::int64_t;
using std::size_t;
namespace base {
template <typename T, size_t... sizes>
struct NestedArray {};
template <typename T, size_t size, size_t... sizes>
struct NestedArray<T, size, sizes...> {
using Type = std::array<typename NestedArray<T, sizes...>::Type, size>;
};
template <typename T>
struct NestedArray<T> {
using Type = T;
};
template <typename T, size_t... sizes>
using Array = typename NestedArray<T, sizes...>::Type;
void OptimizeIO() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
}
void OptimizeIO(const std::string &input_file, const std::string &output_file) {
static std::ifstream input_stream(input_file);
static std::ofstream output_stream(output_file);
cin.rdbuf(input_stream.rdbuf());
cout.rdbuf(output_stream.rdbuf());
cin.tie(nullptr), cout.tie(nullptr);
}
} // namespace base
using base::Array;
const int kMod = 998244353, kInv2 = (kMod + 1) / 2;
namespace mod {
inline void Add(int &a, int b) {
a += b;
if (a >= kMod) a -= kMod;
}
inline void Mul(int &a, int b) { a = static_cast<int64_t>(a) * b % kMod; }
inline int Prod(int a, int b) { return static_cast<int64_t>(a) * b % kMod; }
} // namespace mod
const int kMaxN = 3.0e5 + 5;
int n, lim, sum;
Array<int, kMaxN> a, p, f;
Array<std::vector<int>, kMaxN> edge;
void Dfs(int u, int fa) {
f[u] = (a[u] >= lim);
for (int v : edge[u]) {
if (v == fa) continue;
Dfs(v, u);
mod::Mul(f[u], mod::Prod(f[v] + 1, kInv2));
}
mod::Add(sum, f[u]);
}
int Main() {
base::OptimizeIO();
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
edge[u].emplace_back(v), edge[v].emplace_back(u);
}
std::iota(p.begin() + 1, p.begin() + n + 1, 1);
std::sort(p.begin() + 1, p.begin() + n + 1,
[](int x, int y) { return a[x] < a[y]; });
int ans = 0;
for (int i = 0; i < n; i++) {
int x = p[i];
lim = a[x] + 1, sum = 0;
Dfs(1, 0), mod::Add(sum, f[1]);
mod::Add(ans, mod::Prod(sum, a[p[i + 1]] - a[x]));
}
for (int i = 1; i <= n - 2; i++) mod::Add(ans, ans);
cout << ans << "\n";
return 0;
}
} // namespace
int main() { return Main(); }
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 10476kb
input:
4 1 2 3 4 1 2 2 4 3 2
output:
44
result:
ok 1 number(s): "44"
Test #2:
score: 0
Accepted
time: 1ms
memory: 10476kb
input:
5 3 5 6 5 1 4 1 2 3 3 5 1 3
output:
154
result:
ok 1 number(s): "154"
Test #3:
score: -100
Time Limit Exceeded
input:
278989 864394090 384799247 186936242 598547507 962916136 540758021 158527118 688236322 682301622 948660645 881768181 481432818 557201870 794956026 205262301 920739629 141926922 990361777 818811172 150579096 1032726 328924563 638044961 21740781 484574329 737977343 113738003 345289940 363021157 582495...