QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#335260#7899. Say Hello to the Futurehos_lyricWA 162ms48956kbC++149.6kb2024-02-23 03:38:422024-02-23 03:38:42

Judging History

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

  • [2024-02-23 03:38:42]
  • 评测
  • 测评结果:WA
  • 用时:162ms
  • 内存:48956kb
  • [2024-02-23 03:38:42]
  • 提交

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 <random>
#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;
vector<int> A;

void brute() {
  vector<Mint> dp(N + 1, 0);
  dp[0] = 1;
  for (int i = 0; i < N; ++i) {
    int mx = 0;
    for (int j = i + 1; j <= N; ++j) {
      chmax(mx, A[j - 1]);
      if (mx <= j - i) dp[j] += dp[i];
    }
  }
  cerr << A << ": " << dp << endl;
}

namespace easy {
vector<Mint> dp;
vector<int> mxs;
vector<char> on;
vector<vector<int>> iss;
void init() {
  dp.assign(N + 1, 0);
  dp[0] = 1;
  mxs.resize(N + 1);
  on.resize(N + 1);
  iss.resize(N + 1);
}
void rec(int l, int r) {
  if (l + 1 == r) {
    if (A[l] <= 1) dp[r] += dp[l];
  } else {
    // dp[l, mid) -> dp(mid, r]
    const int mid = (l + r) / 2;
    rec(l, mid);
    
    mxs[mid] = 2;
    for (int i = mid; --i >= l; ) mxs[i] = max(A[i], mxs[i + 1]);
    for (int j = mid; j < r; ++j) mxs[j + 1] = max(mxs[j], A[j]);
    /*
      condition for i->j: max{mxs[i], mxs[j]} <= j - i
        - mxs[i] <= j - i  <=>  i + mxs[i] <= j
        - mxs[j] <= j - i  <=>  i <= j - mxs[j]
        i + mxs[i], j - mxs[j]: almost decreasing
    */
    for (int j = mid + 1; j <= r; ++j) iss[j].clear();
    for (int i = l; i < mid; ++i) {
      on[i] = 0;
      if (i + mxs[i] <= r) iss[max(i + mxs[i], mid + 1)].push_back(i);
    }
    // valid: l <= i < k (<= mid)
    Mint now = 0;
    for (int k = l, j = mid + 1; j <= r; ++j) {
      for (const int i : iss[j]) {
        on[i] = 1;
        if (i < k) now += dp[i];
      }
      for (; k < mid && k <= j - mxs[j]; ++k) if (on[k]) now += dp[k];
      for (; k > l && !(k - 1 <= j - mxs[j]); --k) if (on[k - 1]) now -= dp[k - 1];
      dp[j] += now;
    }
    
    rec(mid, r);
  }
}
}  // easy

vector<Mint> pre, suf;

namespace hard {
vector<Mint> diff;
vector<int> mxs;
vector<pair<int, int>> mxss[2];
vector<char> on;
vector<vector<int>> jss, iss;
void init() {
  diff.assign(N, 0);
  mxs.resize(N + 1);
  for (int s = 0; s < 2; ++s) mxss[s].resize(N + 1);
  on.resize(N + 1);
  jss.resize(N + 1);
  iss.resize(N + 1);
}
void rec(int l, int r) {
  if (l + 1 == r) {
    if (A[l] > 1) diff[l] += pre[l] * suf[r];
  } else {
    /*
      diff[h] -= pre[i] suf[j]
        - l <= i < mid < j <= r
        - i <= h < j
        - max{mxs[i], mxs[j]} <= j - i
      diff[h] += pre[i] suf[j]
        - l <= i < mid < j <= r
        - i <= h < j
        - max{mxs'[h][i], mxs'[h][j]} <= j - i
            mxs'[h]: mxs when A[i] <- 1
            at most one h can change each mxs[i] or mxs[j]
    */
    const int mid = (l + r) / 2;
    rec(l, mid);
    
    mxss[0][mid] = mxss[1][mid] = make_pair(2, -1);
    for (int i = mid; --i >= l; ) {
      pair<int, int> tmp(A[i], i);
      for (int s = 0; s < 2; ++s) {
        mxss[s][i] = mxss[s][i + 1];
        if (mxss[s][i] < tmp) swap(mxss[s][i], tmp);
      }
    }
    for (int j = mid; j < r; ++j) {
      pair<int, int> tmp(A[j], j);
      for (int s = 0; s < 2; ++s) {
        mxss[s][j + 1] = mxss[s][j];
        if (mxss[s][j + 1] < tmp) swap(mxss[s][j + 1], tmp);
      }
    }
    // i <= h <= mid
    for (int s = 0; s < 2; ++s) {
      for (int j = mid; j <= r; ++j) mxs[j] = mxss[0][j].first;
      for (int i = mid - 1; i >= l; --i) mxs[i] = mxss[s][i].first;
      for (int i = mid - 1; i >= l; --i) iss[i].clear();
      for (int j = r; j > mid; --j) {
        on[j] = 0;
        if (l <= j - mxs[j]) jss[min(j - mxs[j], mid - 1)].push_back(j);
      }
      // valid: (mid <=) k < j <= r
      Mint now = 0;
      for (int k = r, i = mid - 1; i >= l; --i) {
        for (const int j : jss[i]) {
          on[j] = 1;
          if (j > k) now += suf[j];
        }
        for (; k > mid && k >= i + mxs[i]; --k) if (on[k]) now += suf[k];
        for (; k < r && !(k + 1 >= i + mxs[i]); ++k) if (on[k + 1]) now -= suf[k + 1];
        const int h = mxss[0][i].second;
// if(l<=h&&h<mid)cerr<<"l="<<l<<", mid="<<mid<<", r="<<r<<", s="<<s<<", h="<<h<<", i="<<i<<": "<<pre[i]<<" * "<<now<<endl;
        if (l <= h && h < mid) diff[h] += (s?+1:-1) * pre[i] * now;
      }
    }
    // mid <= h < r
    for (int s = 0; s < 2; ++s) {
      for (int i = mid; i >= l; --i) mxs[i] = mxss[0][i].first;
      for (int j = mid + 1; j <= r; ++j) mxs[j] = mxss[s][j].first;
      for (int j = mid + 1; j <= r; ++j) iss[j].clear();
      for (int i = l; i < mid; ++i) {
        on[i] = 0;
        if (i + mxs[i] <= r) iss[max(i + mxs[i], mid + 1)].push_back(i);
      }
      // valid: l <= i < k (<= mid)
      Mint now = 0;
      for (int k = l, j = mid + 1; j <= r; ++j) {
        for (const int i : iss[j]) {
          on[i] = 1;
          if (i < k) now += pre[i];
        }
        for (; k < mid && k <= j - mxs[j]; ++k) if (on[k]) now += pre[k];
        for (; k > l && !(k - 1 <= j - mxs[j]); --k) if (on[k - 1]) now -= pre[k - 1];
        const int h = mxss[0][j].second;
// if(mid<=h&&h<r)cerr<<"l="<<l<<", mid="<<mid<<", r="<<r<<", s="<<s<<", h="<<h<<", j="<<j<<": "<<now<<" * "<<suf[j]<<endl;
        if (mid <= h && h < r) diff[h] += (s?+1:-1) * now * suf[j];
      }
    }
    
    rec(mid, r);
  }
}
}  // hard


int main() {
  for (; ~scanf("%d", &N); ) {
    A.resize(N);
    for (int i = 0; i < N; ++i) {
      scanf("%d", &A[i]);
    }
    
    {
      using namespace easy;
      init();
      rec(0, N);
      pre = dp;
// cerr<<"pre = "<<pre<<endl;brute();
      reverse(A.begin(), A.end());
      init();
      rec(0, N);
      suf = dp;
      reverse(suf.begin(), suf.end());
// cerr<<"suf = "<<suf<<endl;brute();
      reverse(A.begin(), A.end());
    }
    {
      using namespace hard;
      init();
      rec(0, N);
// cerr<<"diff = "<<diff<<endl;
      for (int i = 0; i < N; ++i) {
        printf("%u\n", (pre[N] + diff[i]).x);
      }
// for(int i=0;i<N;++i){int a=1;swap(A[i],a);brute();swap(A[i],a);}
    }
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3928kb

input:

5
1 3 2 1 2

output:

3
6
3
3
6

result:

ok 5 tokens

Test #2:

score: -100
Wrong Answer
time: 162ms
memory: 48956kb

input:

200000
15922 15391 11782 4758 1973 19909 16800 6438 3821 986 18599 2011 338 19886 12401 4169 11143 12665 3230 12565 15065 15056 5090 16908 13677 12634 11435 1425 8647 3876 10694 12256 3853 19901 5723 11065 6147 13613 13768 15794 14993 5819 8117 13871 14708 15099 7152 9810 14438 10557 3209 1265 13915...

output:

157122482
826457499
826457499
826457499
826457499
826457499
826457499
826457499
826457499
826457499
826457499
826457499
826457499
826457499
826457499
826457499
826457499
826457499
826457499
826457499
826457499
826457499
826457499
826457499
826457499
826457499
826457499
826457499
826457499
826457499
...

result:

wrong answer 554th words differ - expected: '864711265', found: '608646882'