結果

問題 No.1709 Indistinguishable by MEX
ユーザー uytvccuytvcc
提出日時 2021-10-15 23:56:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 66 ms / 2,000 ms
コード長 2,228 bytes
コンパイル時間 1,700 ms
コンパイル使用メモリ 175,532 KB
実行使用メモリ 6,408 KB
最終ジャッジ日時 2023-10-17 21:49:46
合計ジャッジ時間 3,990 ms
ジャッジサーバーID
(参考情報)
judge11 / 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 52 ms
5,880 KB
testcase_09 AC 40 ms
5,352 KB
testcase_10 AC 38 ms
5,352 KB
testcase_11 AC 31 ms
4,824 KB
testcase_12 AC 30 ms
4,824 KB
testcase_13 AC 45 ms
5,616 KB
testcase_14 AC 41 ms
5,352 KB
testcase_15 AC 43 ms
5,616 KB
testcase_16 AC 58 ms
6,144 KB
testcase_17 AC 53 ms
6,144 KB
testcase_18 AC 64 ms
6,408 KB
testcase_19 AC 66 ms
6,408 KB
testcase_20 AC 64 ms
6,408 KB
testcase_21 AC 59 ms
6,408 KB
testcase_22 AC 60 ms
6,408 KB
testcase_23 AC 49 ms
6,408 KB
testcase_24 AC 48 ms
6,408 KB
testcase_25 AC 48 ms
6,408 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 26 ms
5,088 KB
testcase_28 AC 24 ms
4,824 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define MOD 1000000007
#define MOD2 998244353
#define INF 1000000007
#define LINF 1000000000000000007LL
#define PI 3.14159265359
#define P pair<ll,ll>
template <typename T>
inline bool chmax(T &a, const T &b){
  if(a<b) {a=b; return true;}
  else return false;
}
template <typename T>
inline bool chmin(T &a, const T &b){
  if(a>b) {a=b; return true;}
  else return false;
}
struct Edge{
  int to; ll cost;
  Edge(int to, ll cost) : to(to), cost(cost) {}
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
void add_edge(Graph &g,int from,int to,ll cost,bool rev,ll rev_cost){
  g[from].push_back(Edge(to,cost));
  if(rev) g[to].push_back(Edge(from,rev_cost));
}

template<typename T>
struct SegmentTree{
  using F = function<T(T,T)>;
  int n;
  vector<T> node;
  F combine;
  T identity;
  
  SegmentTree(int n, F combine, T identity)
  :n(n), node(n<<1, identity), combine(combine), identity(identity) {}

  void update(int i, T x){
    if(i<0 || i>=n) return;
    node[i+=n] = x;
    while(i>>=1){
      node[i]=combine(node[i<<1|0],node[i<<1|1]);
    }
  }
  T get(int i){
    if(i<0 || i>=n) return identity;
    return node[i+n];
  }
  T prod(int l, int r){
    if(l>=r || l<0 || r>n) return identity;
    T Lres=identity;
    T Rres=identity;
    for(l+=n, r+=n; l<r; l>>=1, r>>=1){
      if(l&1) Lres = combine(Lres,node[l++]);
      if(r&1) Rres = combine(node[--r],Rres);
    }
    return combine(Lres,Rres);
  }
};

void solve(){
  int n;
  cin>>n;
  vector<int> p(n);
  vector<int> ind(n);
  rep(i,n) cin>>p[i];
  rep(i,n) ind[p[i]]=i;
  auto f=[](int a,int b){return a+b;};
  SegmentTree<int> seg(n,f,0);
  rep(i,n) seg.update(i,1);
  int l=ind[0],r=ind[0]; seg.update(ind[0],0);
  ll ans=1;
  for(int i=1; i<n; i++){
    if(ind[i]<l || ind[i]>r){
      seg.update(ind[i],0);
      chmin(l,ind[i]); chmax(r,ind[i]);
    }
    else{
      ans *= seg.prod(l,r+1);
      ans %=MOD2;
      seg.update(ind[i],0);
    }
  }
  cout<<ans<<'\n';
}

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout<<setprecision(10)<<fixed;
  solve();
  return 0;
}
0