結果

問題 No.1712 Read and Pile
ユーザー chineristACchineristAC
提出日時 2021-05-21 20:02:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,033 bytes
コンパイル時間 9,251 ms
コンパイル使用メモリ 292,868 KB
実行使用メモリ 10,544 KB
最終ジャッジ日時 2023-10-17 19:43:36
合計ジャッジ時間 13,672 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 89 ms
7,640 KB
testcase_09 AC 121 ms
8,168 KB
testcase_10 AC 80 ms
7,640 KB
testcase_11 AC 100 ms
8,168 KB
testcase_12 AC 63 ms
7,640 KB
testcase_13 AC 86 ms
7,376 KB
testcase_14 AC 72 ms
7,640 KB
testcase_15 AC 105 ms
7,640 KB
testcase_16 AC 96 ms
7,904 KB
testcase_17 AC 87 ms
7,640 KB
testcase_18 AC 95 ms
7,376 KB
testcase_19 AC 84 ms
7,112 KB
testcase_20 AC 84 ms
7,640 KB
testcase_21 AC 105 ms
7,904 KB
testcase_22 AC 91 ms
7,904 KB
testcase_23 AC 131 ms
10,544 KB
testcase_24 AC 120 ms
10,544 KB
testcase_25 AC 135 ms
10,544 KB
testcase_26 AC 152 ms
10,544 KB
testcase_27 AC 151 ms
10,544 KB
testcase_28 AC 70 ms
7,640 KB
testcase_29 AC 117 ms
9,752 KB
testcase_30 AC 89 ms
8,696 KB
testcase_31 AC 34 ms
10,280 KB
testcase_32 AC 27 ms
8,960 KB
testcase_33 AC 55 ms
8,960 KB
testcase_34 AC 53 ms
8,432 KB
testcase_35 AC 86 ms
9,488 KB
testcase_36 AC 89 ms
10,016 KB
testcase_37 AC 78 ms
9,224 KB
testcase_38 AC 2 ms
4,348 KB
testcase_39 AC 10 ms
4,348 KB
testcase_40 AC 13 ms
4,348 KB
testcase_41 WA -
testcase_42 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <random>
#include <stdio.h>
#include <fstream>
#include <functional>
#include <atcoder/all>
#include "testlib.h"
using namespace std;
using namespace atcoder;

#define rep(i,n,c) for (int i=0;i<n;i+=c)
#define append push_back
#define all(x) (x).begin(), (x).end()

template<class T>
using vec = vector<T>;
template<class T>
using vvec = vec<vec<T>>;
template<class T>
using vvvec = vec<vvec<T>>;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;


template<class T>
bool chmin(T &a, T b){
  if (a>b){
    a = b;
    return true;
  }
  return false;
}

template<class T>
bool chmax(T &a, T b){
  if (a<b){
    a = b;
    return true;
  }
  return false;
}

template<class T>
T sum(vec<T> x){
  T res=0;
  for (auto e:x){
    res += e;
  }
  return res;
}

template<class T>
void printv(vec<T> x){
  for (auto e:x){
    cout<<e<<" ";
  }
  cout<<"\n";
}


const ll INF = 1e17;

const int MIN_N = 1;
const int MAX_N = 200000;
const int MIN_M = 1;
const int MAX_M = 200000;


using mint = modint998244353;
const mint i2 = ((mint)(2)).inv();

int main(int argc, char* argv[]){
  registerValidation(argc,argv);
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  int N = inf.readInt(MIN_N,MAX_N,"N");
  inf.readSpace();
  int M = inf.readInt(MIN_M,MAX_M,"M");
  inf.readEoln();

  vec<int> A(N+M+1);
  for (int i=1;i<N+1;i++){
    A[i] = N-i+1;
  }
  A[0] = -2;
  for (int i=N+1;i<N+M+1;i++){
    A[i] = inf.readInt(-1,N);
    assert (A[i]!=0);
    if (i!=N+M){
      inf.readSpace();
    }
    else{
      inf.readEoln();
    }
  }
  inf.readEof();

  if (N==1){
    cout<<M<<endl;
    return 0;
  }
  else if (N==2){
    int pre=1;
    mint res=M;
    for (int i=N+1;i<N+M+1;i++){
      if (A[i]==-1){
        res += i2;
      }
      else{
        if (pre!=0 and A[i]!=pre){
          res += 1;
        }
        else if (pre==-1){
          res += i2;
        }
      }
      pre = A[i];
    }
    cout<<res.val()<<endl;
    return 0;
  }
  
  mint p = (N-2) * ((mint)(N)).inv();
  mint ip = p.inv();

  vec<int> zero(N+M+1);
  rep(i,N+M+1,1){
    if (A[i]==-1){
      zero[i] = 1;
    }
    if (i){
      zero[i] += zero[i-1];
    }
  }

  vec<int> last(N+1);
  rep(i,N+1,1){
    last[i] = N-i+1;
  }

  fenwick_tree<mint> fwp(N+M+1);
  fenwick_tree<int> fwcnt(N+M+1);
  for (int i=1;i<N+1;i++){
    fwcnt.add(i,1);
    fwp.add(i,ip.pow(zero[i]));
  }

  mint res;
  int pre,k;
  for (int i=N+1;i<N+M+1;i++){
    if (A[i]==-1){
      res += N-1;
    }
    else{
      pre = last[A[i]];
      k = fwcnt.sum(1,pre);

      res += N-1;
      res += (fwp.sum(pre+1,i+1)) * p.pow(zero[i]);
      res -= k * p.pow(zero[i]-zero[pre]);

      fwcnt.add(pre,-1);
      fwp.add(pre,-ip.pow(zero[pre]));

      last[A[i]] = i;
      fwcnt.add(i,1);
      fwp.add(i,ip.pow(zero[i]));
    }
  }

  res /= 2;
  res += M;
  cout<<res.val()<<endl;
}
0