QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#188362#4914. Slight Hopehos_lyric#0 0ms0kbC++146.6kb2023-09-25 19:22:462024-07-04 02:46:08

Judging History

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

  • [2024-07-04 02:46:08]
  • 评测
  • 测评结果:0
  • 用时:0ms
  • 内存:0kb
  • [2023-09-25 19:22:46]
  • 提交

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


int N, Q;
vector<Mint> A;
vector<int> P;

Mint brute(int L, int R) {
  static Mint dp[5010];
  copy(A.begin() + L, A.begin() + R, dp + L);
  Mint ans = 0;
  for (int u = R; --u >= L; ) {
    if (P[u] < L) {
      ans += dp[u] * dp[u];
    } else {
      dp[P[u]] += dp[u];
    }
  }
  return ans;
}


namespace sqr {
vector<vector<int>> graph;
int zeit;
vector<int> dis, fin;
void dfs(int u) {
  dis[u] = zeit++;
  for (const int v : graph[u]) dfs(v);
  fin[u] = zeit;
}

constexpr int E = 18;
constexpr int MAX_N = 250'010;
int pp[E][MAX_N];

// constexpr int K = 4;
constexpr int K = 500;
vector<Mint> f[501], g[501];

int qid;
int app[MAX_N];
Mint fs[MAX_N];

void build() {
  graph.assign(N, {});
  for (int u = 1; u < N; ++u) graph[P[u]].push_back(u);
  zeit = 0;
  dis.resize(N);
  fin.resize(N);
  dfs(0);
  
  for (int u = 0; u < N; ++u) pp[0][u] = P[u];
  for (int e = 0; e < E - 1; ++e) for (int u = 0; u < N; ++u) pp[e + 1][u] = (~pp[e][u]) ? pp[e][pp[e][u]] : -1;
  
  for (int x = 0; x <= N / K; ++x) {
    const int r = x * K;
    f[x].assign(r + 1, 0);
    g[x].assign(r + 1, 0);
    for (int u = 0; u < r; ++u) f[x][u] = A[u];
    for (int u = r; --u >= 1; ) f[x][P[u]] += f[x][u];
    for (int u = 0; u < r; ++u) g[x][u + 1] = g[x][u] + f[x][u] * f[x][u];
  }
  
#ifdef LOCAL
  qid = 0;
  memset(app, 0, sizeof(app));
  memset(fs, 0, sizeof(fs));
#endif
}

Mint query(int L, int R) {
  ++qid;
  const int M = lower_bound(P.begin() + L, P.begin() + R, L) - P.begin();
  const int x = R / K;
  const int r = x * K;
  Mint ans = 0;
  if (L < min(M, r)) {
    ans += g[x][min(M, r)] - g[x][L];
  }
  int v = L;
  for (int u = max(L, r); u < R; ++u) {
if(0){//    if (!(dis[v] <= dis[u] && dis[u] < fin[v])) {
      v = u;
      for (int e = E; --e >= 0; ) if (L <= pp[e][v]) v = pp[e][v];
    }
    if (app[v] != qid) {
      app[v] = qid;
      fs[v] = (v < r) ? f[x][v] : 0;
    }
// cerr<<"u = "<<u<<": v = "<<v<<"; "<<fs[v]<<" -> "<<(fs[v]+A[u])<<endl;
    ans += A[u] * (fs[v] + fs[v] + A[u]);
    fs[v] += A[u];
  }
  return ans;
}
} // sqr


int main() {
  for (; ~scanf("%d%d", &N, &Q); ) {
    A.resize(N);
    for (int u = 0; u < N; ++u) {
      scanf("%u", &A[u].x);
    }
    P.resize(N);
    for (int u = 1; u < N; ++u) {
      scanf("%d", &P[u]);
      --P[u];
    }
    P[0] = -1;
    
    sqr::build();
    
    int lst = 0;
    for (int q = 0; q < Q; ++q) {
      int L, R;
      scanf("%d%d", &L, &R);
      L ^= lst;
      R ^= lst;
      --L;
// cerr<<COLOR("33")<<"query ["<<L<<", "<<R<<")"<<COLOR()<<endl;
      const Mint ans = sqr::query(L, R);
      printf("%u\n", ans.x);
#ifdef LOCAL
if(N<=5000){
 const Mint brt=brute(L,R);
 if(brt!=ans)cerr<<L<<" "<<R<<": "<<brt<<" "<<ans<<endl;
 assert(brt==ans);
}
#endif
#ifdef LOCAL
if(N==250000)continue;
#endif
      lst = ans.x;
    }
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Runtime Error

Test #1:

score: 0
Runtime Error

input:

5000 5000
63141398 126604376 697512251 741353996 356604726 507953968 494219271 973110260 460861914 988416009 970876787 448429588 725493166 883501210 51076237 784293761 8003146 766721589 406465089 330785670 782582116 501008987 936941546 564663613 40330818 320342607 566652836 806983548 79234347 581976...

output:


result:


Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Memory Limit Exceeded

Test #15:

score: 0
Memory Limit Exceeded

input:

250000 250000
768540930 17767906 372927484 987601476 466807233 279110163 484031897 581293130 869165405 440806324 190995959 228277657 510008046 885148108 825022142 732048181 608976903 327270073 923084855 752263987 475969665 911033413 561860569 377841111 401028041 117941740 350378391 295513473 2304741...

output:


result:


Subtask #4:

score: 0
Skipped

Dependency #1:

0%