QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#105131#6322. ForestryHgCl2AC ✓989ms54896kbC++145.7kb2023-05-13 12:28:412023-05-13 12:28:44

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-13 12:28:44]
  • 评测
  • 测评结果:AC
  • 用时:989ms
  • 内存:54896kb
  • [2023-05-13 12:28:41]
  • 提交

answer

#include <algorithm>
#include <array>
#include <cstddef>
#include <cstdint>
#include <fstream>
#include <iostream>
#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 int Sum(int a, int b) {
  a += b;
  if (a >= kMod) a -= kMod;
  return a;
}

inline void Sub(int &a, int b) {
  a -= b;
  if (a < 0) a += kMod;
}

inline int Diff(int a, int b) {
  a -= b;
  if (a < 0) a += kMod;
  return a;
}

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; }

int Pow(int a, int b) {
  int ans = 1, prod = a;

  while (b) {
    if (b & 1) Mul(ans, prod);
    Mul(prod, prod), b >>= 1;
  }

  return ans;
}

inline int Inv(int a) { return Pow(a, kMod - 2); }
}  // namespace mod

const int kMaxN = 3.0e5 + 5;
int n;
Array<int, kMaxN> f, sum_f, fa;
Array<int, kMaxN, 2> ch;
Array<bool, kMaxN> vis;
Array<std::vector<int>, kMaxN> edge;

struct Node {
  int val, id;
};

inline bool operator<(const Node &a, const Node &b) { return a.val < b.val; }

Array<Node, kMaxN> a;

struct Matrix {
  int a11, a13, a21, a23;
};

inline int Helper(int a, int b, int c) {
  return (static_cast<int64_t>(a) * b + c) % kMod;
}

inline Matrix operator*(const Matrix &a, const Matrix &b) {
  return {mod::Prod(a.a11, b.a11), Helper(a.a11, b.a13, a.a13),
          Helper(a.a21, b.a11, b.a21), Helper(a.a21, b.a13, b.a23 + a.a23)};
}

Array<Matrix, kMaxN> val, sum;

struct Info {
  int g, h;
};

inline Info operator+(const Info &a, Info b) {
  b.g = mod::Prod(b.g + 1, kInv2);
  return {mod::Prod(a.g, b.g), mod::Sum(a.h, b.h)};
}

inline Info operator-(const Info &a, Info b) {
  b.g = mod::Prod(b.g + 1, kInv2);
  if (a.g == 0 || b.g == 1) return {a.g, mod::Diff(a.h, b.h)};
  return {mod::Prod(a.g, mod::Inv(b.g)), mod::Diff(a.h, b.h)};
}

Matrix GenMatrix(const Info &info, bool f) {
  if (f) return {0, 0, 0, info.h};
  int half = mod::Prod(info.g, kInv2);
  return {half, half, half, mod::Sum(half, info.h)};
}

Info Retrodict(const Matrix &a, bool f) {
  if (f) return {0, a.a23};
  return {mod::Sum(a.a11, a.a11), mod::Diff(a.a23, a.a11)};
}

void Dfs(int u, int fa) {
  f[u] = 1;

  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], sum_f[v]);
  }

  ::fa[u] = fa;
  sum[u] = val[u] = GenMatrix({f[u], sum_f[u]}, false);
  mod::Add(sum_f[u], f[u]);
}

inline bool IsRoot(int p) { return p != ch[fa[p]][0] && p != ch[fa[p]][1]; }

inline int Get(int p) { return p == ch[fa[p]][1]; }

inline void PushUp(int p) {
  int x = ch[p][0], y = ch[p][1];

  if (!x && !y) {
    sum[p] = val[p];
  } else if (!x) {
    sum[p] = val[p] * sum[y];
  } else if (!y) {
    sum[p] = sum[x] * val[p];
  } else {
    sum[p] = sum[x] * val[p] * sum[y];
  }
}

void Rotate(int p) {
  int q = fa[p], k = Get(p);
  if (!IsRoot(q)) ch[fa[q]][Get(q)] = p;
  fa[p] = fa[q];
  ch[q][k] = ch[p][k ^ 1], fa[ch[p][k ^ 1]] = q;
  ch[p][k ^ 1] = q, fa[q] = p;
  PushUp(q), PushUp(p);
}

void Splay(int p) {
  while (!IsRoot(p)) {
    int q = fa[p];
    if (!IsRoot(q)) Get(p) == Get(q) ? Rotate(q) : Rotate(p);
    Rotate(p);
  }
}

inline Info Query(const Matrix &a) {
  return {mod::Sum(a.a11, a.a13), mod::Sum(a.a21, a.a23)};
}

void Access(int p) {
  int q = 0;

  while (p) {
    Splay(p);
    Info info = Retrodict(val[p], vis[p]);
    if (ch[p][1]) info = info + Query(sum[ch[p][1]]);
    if (q) info = info - Query(sum[q]);
    val[p] = GenMatrix(info, vis[p]);
    ch[p][1] = q, PushUp(p);
    q = p, p = fa[p];
  }
}

void Modify(int p) {
  Access(p), Splay(p);
  vis[p] = true;
  Info info = Retrodict(val[p], false);
  val[p] = GenMatrix(info, true);
  PushUp(p);
}

int Main() {
  base::OptimizeIO();
  cin >> n;

  for (int i = 1; i <= n; i++) {
    cin >> a[i].val;
    a[i].id = i;
  }

  std::sort(a.begin() + 1, a.begin() + n + 1);

  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    edge[u].emplace_back(v), edge[v].emplace_back(u);
  }

  Dfs(1, 0);
  int ans = 0, p = 1;
  mod::Add(ans, mod::Prod(a[1].val, f[1] + sum_f[1]));
  // cout << f[1] << " " << sum_f[1] << "\n";

  while (p < n) {
    int i = p;
    while (i < n && a[i].val == a[p].val) Modify(a[i++].id);
    Access(1);
    Info res = Query(val[1]);
    // cout << res.g << " " << res.h << "\n";
    mod::Add(ans, mod::Prod(a[i].val - a[p].val, res.g + res.h));
    p = i;
  }

  mod::Mul(ans, mod::Pow(2, n - 2));
  cout << ans << "\n";
  return 0;
}
}  // namespace

int main() { return Main(); }

详细

Test #1:

score: 100
Accepted
time: 2ms
memory: 11164kb

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: 6ms
memory: 11420kb

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: 0
Accepted
time: 752ms
memory: 36228kb

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...

output:

434293031

result:

ok 1 number(s): "434293031"

Test #4:

score: 0
Accepted
time: 767ms
memory: 37048kb

input:

287397
510502405 166707542 6543103 963754822 492178400 668790842 47929394 688150962 147460907 412966210 837275339 435773111 138898710 968087621 746522431 948665731 920170470 717898411 411586421 148909638 659985934 484520123 95176393 425866444 512660652 511428439 958021520 150660147 292591135 7320851...

output:

878336347

result:

ok 1 number(s): "878336347"

Test #5:

score: 0
Accepted
time: 782ms
memory: 37552kb

input:

294100
74665954 206916315 273249696 386095808 903673441 622958780 983022845 464633239 86441267 729372644 689111719 644187811 334408092 398907453 597161881 558735093 189557855 477930470 242405983 95068877 16413783 608746253 761602925 18584327 934502624 975194906 309225637 343248456 653012994 23118382...

output:

712256085

result:

ok 1 number(s): "712256085"

Test #6:

score: 0
Accepted
time: 989ms
memory: 51500kb

input:

300000
606908308 171374011 595912591 940182632 431995844 726762958 256169397 447385236 984023043 528258150 35565776 27503639 983714715 313579533 8771592 967838717 206597371 821802152 910792624 238535529 923693575 376608809 519872194 981808980 991017798 212487460 780752406 913659452 464143746 1561438...

output:

847230777

result:

ok 1 number(s): "847230777"

Test #7:

score: 0
Accepted
time: 440ms
memory: 51892kb

input:

299999
782121216 968610368 25009662 478183585 274702257 773444727 687702634 507153995 474335571 202223165 211518854 313706139 936298699 536896304 702242243 590986189 912655919 596226813 737786290 917153157 990146015 268573292 418734072 3093396 94105534 834508206 491041607 939287910 88847044 81241220...

output:

41386982

result:

ok 1 number(s): "41386982"

Test #8:

score: 0
Accepted
time: 386ms
memory: 54896kb

input:

299999
471542727 799095206 509787451 592650193 362215487 588125595 604003616 881525850 755688267 236898505 532366496 67948692 232297132 929458994 431732351 274008699 164112665 690178118 571148044 408207186 453143828 959612302 878551481 584372849 894288979 552607930 610390281 409926978 15831307 61533...

output:

479741390

result:

ok 1 number(s): "479741390"

Test #9:

score: 0
Accepted
time: 446ms
memory: 49256kb

input:

299997
25665195 607081146 2312642 326822363 385524999 209349813 211617900 372403090 573561165 779096327 304738950 525680732 809703693 740379205 642786351 334319127 24588707 570042249 298675391 92525045 882182405 255387261 629708545 662677567 102298185 416640032 800837034 517838306 180083897 65300699...

output:

756252561

result:

ok 1 number(s): "756252561"

Test #10:

score: 0
Accepted
time: 410ms
memory: 48068kb

input:

299997
772601140 677850510 486293752 38381420 713140958 613588331 933478663 892594986 40965372 719820852 371692421 950213365 627891743 923082750 330248475 635152312 968671306 91954505 646645243 266673541 419892861 468231632 676898301 475369890 173394153 206234682 614218712 386714613 691986706 401543...

output:

909256779

result:

ok 1 number(s): "909256779"

Test #11:

score: 0
Accepted
time: 273ms
memory: 38500kb

input:

300000
489134030 28527536 706816680 898196399 298100900 573504624 817242924 202561288 93577170 123268581 618798194 660515267 184003900 797853604 19557769 572066362 795291782 626861802 147727524 877230625 221524908 456496916 339550982 405198767 983242265 794896971 192309413 938450729 890444371 624597...

output:

469866382

result:

ok 1 number(s): "469866382"

Test #12:

score: 0
Accepted
time: 732ms
memory: 37712kb

input:

294048
940092908 294031597 203789774 965832063 294368312 530860757 29650208 19058325 908258800 103593701 42838422 924175435 729123020 53690401 645412066 388715095 613747643 989874415 525317950 992107991 110671090 174219963 334698216 629961584 722552204 684114784 157441276 972553266 734116607 6480471...

output:

869124246

result:

ok 1 number(s): "869124246"

Test #13:

score: 0
Accepted
time: 700ms
memory: 37872kb

input:

294169
829764052 27629554 569458841 732262994 789440677 560991159 427506198 382533265 982599986 867072676 292427700 863455778 358352325 895409930 503249204 713052609 50315636 478399531 724940834 394303205 658023052 804043479 566808951 672917322 636437946 905749010 875678660 8659223 349546005 8117721...

output:

603293934

result:

ok 1 number(s): "603293934"

Test #14:

score: 0
Accepted
time: 659ms
memory: 36032kb

input:

274214
936267909 35123462 801828019 44784239 618602008 669685848 531693333 374691550 641907521 460643008 895242266 178217314 865256487 210105123 791635414 102380409 605115311 319289854 173186400 301848535 470317014 68928307 385895216 80699081 351459001 505538791 208284669 800278979 952086428 4182918...

output:

344857557

result:

ok 1 number(s): "344857557"

Test #15:

score: 0
Accepted
time: 242ms
memory: 38452kb

input:

300000
300000 299999 299998 299997 299996 299995 299994 299993 299992 299991 299990 299989 299988 299987 299986 299985 299984 299983 299982 299981 299980 299979 299978 299977 299976 299975 299974 299973 299972 299971 299970 299969 299968 299967 299966 299965 299964 299963 299962 299961 299960 299959...

output:

8100800

result:

ok 1 number(s): "8100800"