結果

問題 No.2116 Making Forest Hard
ユーザー 👑 hos.lyrichos.lyric
提出日時 2022-10-28 23:01:44
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 9,581 bytes
コンパイル時間 2,309 ms
コンパイル使用メモリ 144,448 KB
実行使用メモリ 362,368 KB
最終ジャッジ日時 2024-07-06 02:15:53
合計ジャッジ時間 58,611 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 190 ms
316,100 KB
testcase_01 AC 186 ms
316,200 KB
testcase_02 RE -
testcase_03 AC 971 ms
353,884 KB
testcase_04 AC 465 ms
345,216 KB
testcase_05 AC 621 ms
337,744 KB
testcase_06 AC 651 ms
357,376 KB
testcase_07 AC 753 ms
355,456 KB
testcase_08 AC 536 ms
348,288 KB
testcase_09 AC 593 ms
354,740 KB
testcase_10 AC 207 ms
317,884 KB
testcase_11 AC 543 ms
332,920 KB
testcase_12 AC 612 ms
354,792 KB
testcase_13 AC 613 ms
337,764 KB
testcase_14 AC 1,252 ms
352,964 KB
testcase_15 AC 188 ms
316,768 KB
testcase_16 AC 638 ms
362,304 KB
testcase_17 AC 892 ms
341,580 KB
testcase_18 RE -
testcase_19 AC 365 ms
334,704 KB
testcase_20 AC 399 ms
333,440 KB
testcase_21 AC 634 ms
331,520 KB
testcase_22 AC 562 ms
333,952 KB
testcase_23 AC 233 ms
321,520 KB
testcase_24 AC 328 ms
321,920 KB
testcase_25 AC 466 ms
338,304 KB
testcase_26 AC 693 ms
362,368 KB
testcase_27 AC 330 ms
321,844 KB
testcase_28 AC 1,218 ms
349,952 KB
testcase_29 AC 591 ms
334,188 KB
testcase_30 RE -
testcase_31 AC 332 ms
322,560 KB
testcase_32 AC 647 ms
337,792 KB
testcase_33 AC 119 ms
317,204 KB
testcase_34 AC 216 ms
327,996 KB
testcase_35 AC 603 ms
337,748 KB
testcase_36 AC 445 ms
348,328 KB
testcase_37 AC 117 ms
317,184 KB
testcase_38 AC 631 ms
361,600 KB
testcase_39 AC 600 ms
337,692 KB
testcase_40 AC 204 ms
321,880 KB
testcase_41 AC 423 ms
330,748 KB
testcase_42 RE -
testcase_43 RE -
testcase_44 AC 624 ms
336,504 KB
testcase_45 AC 323 ms
326,756 KB
testcase_46 RE -
testcase_47 AC 237 ms
325,976 KB
testcase_48 AC 201 ms
321,564 KB
testcase_49 AC 100 ms
316,272 KB
testcase_50 AC 607 ms
337,900 KB
testcase_51 AC 604 ms
337,776 KB
testcase_52 AC 600 ms
337,744 KB
testcase_53 AC 579 ms
337,824 KB
testcase_54 AC 1,303 ms
352,808 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }

////////////////////////////////////////////////////////////////////////////////
template <unsigned M_> struct ModInt {
  static constexpr unsigned M = M_;
  unsigned x;
  constexpr ModInt() : x(0U) {}
  constexpr ModInt(unsigned x_) : x(x_ % M) {}
  constexpr ModInt(unsigned long long x_) : x(x_ % M) {}
  constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
  constexpr ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
  ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
  ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
  ModInt &operator*=(const ModInt &a) { x = (static_cast<unsigned long long>(x) * a.x) % M; return *this; }
  ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); }
  ModInt pow(long long e) const {
    if (e < 0) return inv().pow(-e);
    ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
  }
  ModInt inv() const {
    unsigned a = M, b = x; int y = 0, z = 1;
    for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; }
    assert(a == 1U); return ModInt(y);
  }
  ModInt operator+() const { return *this; }
  ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; }
  ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); }
  ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); }
  ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); }
  ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); }
  template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); }
  template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); }
  template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); }
  template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); }
  explicit operator bool() const { return x; }
  bool operator==(const ModInt &a) const { return (x == a.x); }
  bool operator!=(const ModInt &a) const { return (x != a.x); }
  friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; }
};
////////////////////////////////////////////////////////////////////////////////

constexpr unsigned MO = 998244353;
using Mint = ModInt<MO>;

using Pair = pair<Mint, Mint>;
Pair operator+(const Pair &a, const Pair &b) { return make_pair(a.first + b.first, a.second + b.second); }
Pair operator-(const Pair &a, const Pair &b) { return make_pair(a.first - b.first, a.second - b.second); }
Pair operator*(const Pair &a, const Pair &b) { return make_pair(a.first * b.first, a.first * b.second + a.second * b.first); }
Pair &operator+=(Pair &a, const Pair &b) { return a = a + b; }
Pair &operator-=(Pair &a, const Pair &b) { return a = a - b; }
Pair &operator*=(Pair &a, const Pair &b) { return a = a * b; }
constexpr Pair ZERO(0, 0);

Pair operator*(const Mint &k, const Pair &a) { return make_pair(k * a.first, k * a.second); }


struct Node {
  int l, r;
  Pair f, g;
  Pair lz;
  Node() {}
  void push(Node &l_, Node &r_) {
    if (!(lz.first == 1 && lz.second == 0)) {
      l_.mul(lz);
      r_.mul(lz);
      lz.first = 1;
      lz.second = 0;
    }
  }
  void pull(const Node &l_, const Node &r_) {
    f = l_.f + r_.f;
    g = l_.g + r_.g;
  }
  void mul(const Pair &val) {
    f *= val;
    g *= val;
    lz *= val;
  }
};
int nodesLen = 1;
Node nodes[10'000'010];
int newNode() {
  const int u = nodesLen++;
  nodes[u].lz.first = 1;
  return u;
}

void rangeMul(int a, int l, int r, int ql, int qr, const Pair &val) {
#ifdef LOCAL
assert(a);
#endif
  if (qr <= l || r <= ql) {
    //
  } else if (ql <= l && r <= qr) {
    nodes[a].mul(val);
  } else {
    const int mid = (l + r) / 2;
    int &al = nodes[a].l;
    int &ar = nodes[a].r;
    if (!al) {
      al = newNode();
      ar = newNode();
    }
    nodes[a].push(nodes[al], nodes[ar]);
    rangeMul(al, l, mid, ql, qr, val);
    rangeMul(ar, mid, r, ql, qr, val);
    nodes[a].pull(nodes[al], nodes[ar]);
  }
}

void pointAdd(int a, int l, int r, int q, const Pair &f, const Pair &g) {
#ifdef LOCAL
assert(a);
#endif
  if (q + 1 <= l || r <= q) {
    //
  } else if (q <= l && r <= q + 1) {
    nodes[a].f += f;
    nodes[a].g += g;
  } else {
    const int mid = (l + r) / 2;
    int &al = nodes[a].l;
    int &ar = nodes[a].r;
    if (!al) {
      al = newNode();
      ar = newNode();
    }
    nodes[a].push(nodes[al], nodes[ar]);
    pointAdd(al, l, mid, q, f, g);
    pointAdd(ar, mid, r, q, f, g);
    nodes[a].pull(nodes[al], nodes[ar]);
  }
}

Pair rangeSum(int a, int l, int r, int ql, int qr) {
#ifdef LOCAL
assert(a);
#endif
  if (qr <= l || r <= ql) {
    return ZERO;
  } else if (ql <= l && r <= qr) {
    return nodes[a].f;
  } else {
    const int mid = (l + r) / 2;
    int &al = nodes[a].l;
    int &ar = nodes[a].r;
    if (!al) {
      al = newNode();
      ar = newNode();
    }
    nodes[a].push(nodes[al], nodes[ar]);
    Pair ret = ZERO;
    ret += rangeSum(al, l, mid, ql, qr);
    ret += rangeSum(ar, mid, r, ql, qr);
    nodes[a].pull(nodes[al], nodes[ar]);
    return ret;
  }
}

int create(int l, int r, int q, const Pair &f, const Pair &g) {
  if (q + 1 <= l || r <= q) {
    const int a = newNode();
    return a;
  } else if (q <= l && r <= q + 1) {
    const int a = newNode();
    nodes[a].f = f;
    nodes[a].g = g;
    return a;
  } else {
    const int mid = (l + r) / 2;
    const int al = create(l, mid, q, f, g);
    const int ar = create(mid, r, q, f, g);
    const int a = newNode();
    nodes[a].l = al;
    nodes[a].r = ar;
    nodes[a].pull(nodes[al], nodes[ar]);
    return a;
  }
}


constexpr Mint INV2 = (1 + MO) / 2;

int N;
vector<int> C;
vector<int> A, B;

vector<vector<int>> graph;

int csLen;
vector<int> cs;

Mint ans;
vector<set<int>> ss;
vector<int> rs;
void dfs(int u, int p) {
  for (const int v : graph[u]) if (p != v) {
    dfs(v, u);
  }
  const int pos = lower_bound(cs.begin(), cs.end(), C[u]) - cs.begin();
  ss[u].insert(pos);
  rs[u] = create(0, csLen, pos, Pair(1, 1), Pair(cs[pos], cs[pos]));
  for (const int v : graph[u]) if (p != v) {
    ans += INV2 * nodes[rs[v]].g.second;
    bool sw = false;
    if (ss[u].size() < ss[v].size()) {
      sw = true;
      swap(ss[u], ss[v]);
      swap(rs[u], rs[v]);
    }
    vector<int> xs(ss[v].begin(), ss[v].end());
    const int len = xs.size();
    xs.push_back(csLen);
    vector<Pair> gs(len);
    // xu < xv
    vector<Pair> hs(len);
    for (int j = 0; j < len; ++j) {
      gs[j] = INV2 * rangeSum(rs[v], 0, csLen, xs[j], xs[j] + 1);
      hs[j] = gs[j] * rangeSum(rs[u], 0, csLen, 0, xs[j]);
    }
// cerr<<u<<" "<<v<<" "<<xs<<": "<<gs<<" "<<hs<<endl;
    // xu >= xv
    {
      Pair sum = sw ? ZERO : Pair(INV2, 0);
      rangeMul(rs[u], 0, csLen, 0, xs[0], sum);
      for (int j = 0; j < len; ++j) {
        sum += gs[j];
        rangeMul(rs[u], 0, csLen, xs[j], xs[j + 1], sum);
      }
    }
    for (int j = 0; j < len; ++j) {
      const Pair h = sw ? (gs[j] + hs[j]) : hs[j];
      pointAdd(rs[u], 0, csLen, xs[j], h, cs[xs[j]] * h);
    }
    for (int j = 0; j < len; ++j) {
      ss[u].insert(xs[j]);
    }
  }
// cerr<<"DONE u = "<<u<<endl;
// cerr<<"  ss[u] = ";pv(ss[u].begin(),ss[u].end());
// cerr<<"  ";for(const int x:ss[u])cerr<<Mint(2).pow(N-1)*rangeSum(rs[u],0,csLen,x,x+1)<<"/"<<Mint(2).pow(N-1)<<" ";cerr<<endl;
}

int main() {
  for (; ~scanf("%d", &N); ) {
    C.resize(N);
    for (int u = 0; u < N; ++u) {
      scanf("%d", &C[u]);
    }
    A.resize(N - 1);
    B.resize(N - 1);
    for (int i = 0; i < N - 1; ++i) {
      scanf("%d%d", &A[i], &B[i]);
      --A[i];
      --B[i];
    }
    
    cs = C;
    sort(cs.begin(), cs.end());
    cs.erase(unique(cs.begin(), cs.end()), cs.end());
    csLen = cs.size();
    
    graph.assign(N, {});
    for (int i = 0; i < N - 1; ++i) {
      graph[A[i]].push_back(B[i]);
      graph[B[i]].push_back(A[i]);
    }
    
    ans = 0;
    ss.assign(N, {});
    rs.assign(N, 0);
    dfs(0, -1);
    ans += nodes[rs[0]].g.second;
    ans *= Mint(2).pow(N - 1);
    printf("%u\n", ans.x);
  }
  return 0;
}
0