QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#210021#6322. Forestryucup-team870Compile Error//C++147.2kb2023-10-10 22:02:332023-10-10 22:02:33

Judging History

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

  • [2023-10-10 22:02:33]
  • 评测
  • [2023-10-10 22:02:33]
  • 提交

answer

#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 <limits>
#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; }
#define COLOR(s) ("\x1b[" s "m")

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

const Mint INV2 = Mint(2).inv();


struct Node {
  int l, r;
  Mint sum0, sum1, lz;
  Node() : l(0), r(0), sum0(0), sum1(0), lz(1) {}
  void mul(Mint val) {
    sum0 *= val;
    sum1 *= val;
    lz *= val;
  }
};
int V;
Node nodes[50'000'010];

void push(int u) {
  Mint &lz = nodes[u].lz;
  if (lz.x != 1) {
    const int l = nodes[u].l;
    const int r = nodes[u].r;
    if (l) nodes[l].mul(lz);
    if (r) nodes[r].mul(lz);
    lz = 1;
  }
}
void pull(int u) {
  const int l = nodes[u].l;
  const int r = nodes[u].r;
  nodes[u].sum0 = nodes[l].sum0 + nodes[r].sum0;
  nodes[u].sum1 = nodes[l].sum1 + nodes[r].sum1;
}

int merge(int u, int v, Mint sumU, Mint sumV, int L, int R) {
  if (!u && !v) return 0;
  if (!u) {
    nodes[v].mul(sumU);
    return v;
  }
  if (!v) {
    nodes[u].mul(sumV);
    return u;
  }
  assert(L != R-1);
  const int Mid = (L + R) >> 1;
  assert(nodes[u].l || nodes[u].r || nodes[v].l || nodes[v].r);
  push(u);
  push(v);
  nodes[u].l = merge(nodes[u].l, nodes[v].l, nodes[nodes[u].r].sum0 + sumU, nodes[nodes[v].r].sum0 + sumV, L, Did);
  nodes[u].r = merge(nodes[u].r, nodes[v].r, sumU, sumV, Mid, R);
  pull(u);
  return u;
}
int pointAdd(int u, int L, int R, int a, Mint val0, Mint val1) {
  if (!u) {
    u = V++;
    nodes[u] = Node();
  }
  if (L + 1 == R) {
    nodes[u].sum0 += val0;
    nodes[u].sum1 += val1;
    return u;
  }
  const int Mid = (L + R) >> 1;
  push(u);
  if (a < Mid) {
    nodes[u].l = pointAdd(nodes[u].l, L, Mid, a, val0, val1);
  } else {
    nodes[u].r = pointAdd(nodes[u].r, Mid, R, a, val0, val1);
  }
  pull(u);
  return u;
}
void debug(int u, int L, int R, bool pu) {
  if (!u) return;
  if (!pu) cerr << "[" << L << "," << R << "):" << (32 * nodes[u].sum0) << "," << (32 * nodes[u].sum1) << "," << (32 * nodes[u].lz) << " ";
  if (L + 1 == R) {
    if (pu) cerr << L << ":" << (32 * nodes[u].sum0) << "," << (32 * nodes[u].sum1) << " ";
    return;
  }
  const int Mid = (L + R) >> 1;
  if (pu) push(u);
  debug(nodes[u].l, L, Mid, pu);
  debug(nodes[u].r, Mid, R, pu);
}


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

vector<vector<int>> graph;
vector<pair<int, int>> cus;
Mint ans;

int dfs(int u, int p) {
  const int pos = lower_bound(cus.begin(), cus.end(), make_pair(C[u], u)) - cus.begin();
  int ret = pointAdd(0, 0, N, pos, 1, C[u]);
  for (const int v : graph[u]) if (p != v) {
    int res = dfs(v, u);
    // connect
    nodes[res].mul(INV2);
    // cut
    ans += nodes[res].sum1;
    ret = merge(ret, res, 0, INV2);
  }
// cerr<<"u = "<<u<<": ";debug(ret,0,N,false);cerr<<endl;
// cerr<<"u = "<<u<<": ";debug(ret,0,N,true);cerr<<endl;
  return ret;
}

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];
    }
    
    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]);
    }
    
    cus.resize(N);
    for (int u = 0; u < N; ++u) {
      cus[u] = make_pair(C[u], u);
    }
    sort(cus.begin(), cus.end());
// cerr<<"cus = "<<cus<<endl;
    
    ans = 0;
    V = 1;
    const int res = dfs(0, -1);
    ans += nodes[res].sum1;
    ans *= Mint(2).pow(N - 1);
    printf("%u\n", ans.x);
  }
  return 0;
}

Details

answer.code: In function ‘int merge(int, int, Mint, Mint, int, int)’:
answer.code:126:111: error: ‘Did’ was not declared in this scope; did you mean ‘Mid’?
  126 |   nodes[u].l = merge(nodes[u].l, nodes[v].l, nodes[nodes[u].r].sum0 + sumU, nodes[nodes[v].r].sum0 + sumV, L, Did);
      |                                                                                                               ^~~
      |                                                                                                               Mid
answer.code: In function ‘int dfs(int, int)’:
answer.code:182:16: error: no matching function for call to ‘merge(int&, int&, int, const Mint&)’
  182 |     ret = merge(ret, res, 0, INV2);
      |           ~~~~~^~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/11/algorithm:62,
                 from answer.code:7:
/usr/include/c++/11/bits/stl_algo.h:4927:5: note: candidate: ‘template<class _IIter1, class _IIter2, class _OIter> _OIter std::merge(_IIter1, _IIter1, _IIter2, _IIter2, _OIter)’
 4927 |     merge(_InputIterator1 __first1, _InputIterator1 __last1,
      |     ^~~~~
/usr/include/c++/11/bits/stl_algo.h:4927:5: note:   template argument deduction/substitution failed:
answer.code:182:16: note:   deduced conflicting types for parameter ‘_IIter2’ (‘int’ and ‘ModInt<998244353>’)
  182 |     ret = merge(ret, res, 0, INV2);
      |           ~~~~~^~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/11/algorithm:62,
                 from answer.code:7:
/usr/include/c++/11/bits/stl_algo.h:4978:5: note: candidate: ‘template<class _IIter1, class _IIter2, class _OIter, class _Compare> _OIter std::merge(_IIter1, _IIter1, _IIter2, _IIter2, _OIter, _Compare)’
 4978 |     merge(_InputIterator1 __first1, _InputIterator1 __last1,
      |     ^~~~~
/usr/include/c++/11/bits/stl_algo.h:4978:5: note:   template argument deduction/substitution failed:
answer.code:182:16: note:   deduced conflicting types for parameter ‘_IIter2’ (‘int’ and ‘ModInt<998244353>’)
  182 |     ret = merge(ret, res, 0, INV2);
      |           ~~~~~^~~~~~~~~~~~~~~~~~~
answer.code:111:5: note: candidate: ‘int merge(int, int, Mint, Mint, int, int)’
  111 | int merge(int u, int v, Mint sumU, Mint sumV, int L, int R) {
      |     ^~~~~
answer.code:111:5: note:   candidate expects 6 arguments, 4 provided
answer.code: In function ‘int main()’:
answer.code:193:12: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  193 |       scanf("%d", &C[u]);
      |       ~~~~~^~~~~~~~~~~~~
answer.code:198:12: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  198 |       scanf("%d%d", &A[i], &B[i]);
      |       ~~~~~^~~~~~~~~~~~~~~~~~~~~~