結果
問題 | No.1709 Indistinguishable by MEX |
ユーザー |
|
提出日時 | 2021-10-15 23:56:59 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 65 ms / 2,000 ms |
コード長 | 2,228 bytes |
コンパイル時間 | 1,800 ms |
コンパイル使用メモリ | 174,492 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-09-17 19:03:32 |
合計ジャッジ時間 | 4,054 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 26 |
ソースコード
#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;}