提出 #72210073
ソースコード 拡げる
#include <bits/stdc++.h>
#pragma GCC optimize("O3,fast-math")
using namespace std;
typedef long long ll;
#define int ll
int tc,n,a[303030];
const int mod=998244353;
struct Node1{
int cnt[3][3]; // x-y, wmdrka
Node1() {
memset(cnt,0,sizeof(cnt));
}
Node1(int q,int w,int e,int r,int t,int y,int u,int i,int o) {
cnt[0][0]=q;
cnt[0][1]=w;
cnt[0][2]=e;
cnt[1][0]=r;
cnt[1][1]=t;
cnt[1][2]=y;
cnt[2][0]=u;
cnt[2][1]=i;
cnt[2][2]=o;
}
};
struct seg{
using Node = Node1;
int n,lg,SZ; vector<Node> tree;
seg(int sz) {
n=sz; SZ=1; lg=0; while (SZ<n) {SZ<<=1; lg++;}
tree.resize(2*SZ+5);
}
void set(int ind, Node val) {
--ind|=SZ; tree[ind]=val;
}
Node merge(Node a, Node b) {
Node ret;
for(int i=0;i<3;i++) for(int j=0;j<3;j++) ret.cnt[i][j]=(a.cnt[i][j]+b.cnt[i][j])%mod;
return ret;
}
void init() {
for(int i=SZ-1;i;i--) tree[i]=merge(tree[i<<1],tree[i<<1|1]);
}
void pull(int ind) {
tree[ind]=merge(tree[ind<<1], tree[(ind<<1)|1]);
}
void upd(int ind, Node val) {
--ind|=SZ; tree[ind]=merge(tree[ind],val);
for(ind>>=1;ind;ind>>=1) pull(ind);
}
Node qry(int l, int r) {
Node nl,rr; --l|=SZ; --r|=SZ;
for(;l<=r;l>>=1,r>>=1) {
if(l&1) nl=merge(nl,tree[l++]);
if(~r&1) rr=merge(tree[r--],rr);
}
return merge(nl,rr);
}
};
void solve() {
cin >> n;
if(n<3) {cout << 0; return;}
for(int i=1;i<=n;i++) cin >> a[i];
seg t=seg(n);
t.upd(a[1],Node1(0,0,0,0,1,0,0,0,0));
for(int i=2;i<=n;i++) {
t.upd(a[i],Node1(0,0,0,0,1,0,0,0,0));
if(a[i]>1) {
Node1 l=t.qry(1,a[i]-1);
int q=(l.cnt[0][0]+l.cnt[0][1]+l.cnt[0][2]+l.cnt[1][2])%mod;
int w=(l.cnt[1][0]+l.cnt[1][1]+l.cnt[2][2])%mod;
int e=(l.cnt[2][0]+l.cnt[2][1])%mod;
Node1 cur=Node1(q,0,0,w,0,0,e,0,0);
t.upd(a[i],cur);
}
if(a[i]<n) {
Node1 l=t.qry(a[i]+1,n);
int q=(l.cnt[0][1]+l.cnt[0][2])%mod;
int w=(l.cnt[1][1]+l.cnt[1][2]+l.cnt[0][0])%mod;
int e=(l.cnt[2][0]+l.cnt[2][1]+l.cnt[2][2]+l.cnt[1][0])%mod;
Node1 cur=Node1(0,0,q,0,0,w,0,0,e);
t.upd(a[i],cur);
}
}
auto last=t.qry(1,n);
//cout << last.cnt[2][0] << last.cnt[2][1] << last.cnt[2][2] << "\n";
cout << (last.cnt[2][0]+last.cnt[2][1]+last.cnt[2][2])%mod;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
for(tc=1;tc;tc--) {
solve();
}
}
提出情報
ジャッジ結果
| セット名 |
Sample |
All |
| 得点 / 配点 |
0 / 0 |
525 / 525 |
| 結果 |
|
|
| セット名 |
テストケース |
| Sample |
sample_01.txt, sample_02.txt, sample_03.txt |
| All |
sample_01.txt, sample_02.txt, sample_03.txt, test_01.txt, test_02.txt, test_03.txt, test_04.txt, test_05.txt, test_06.txt, test_07.txt, test_08.txt, test_09.txt, test_10.txt, test_11.txt, test_12.txt, test_13.txt, test_14.txt, test_15.txt, test_16.txt, test_17.txt, test_18.txt, test_19.txt, test_20.txt, test_21.txt |
| ケース名 |
結果 |
実行時間 |
メモリ |
| sample_01.txt |
AC |
1 ms |
3560 KiB |
| sample_02.txt |
AC |
1 ms |
3560 KiB |
| sample_03.txt |
AC |
1 ms |
3416 KiB |
| test_01.txt |
AC |
1 ms |
3584 KiB |
| test_02.txt |
AC |
134 ms |
41416 KiB |
| test_03.txt |
AC |
175 ms |
41724 KiB |
| test_04.txt |
AC |
86 ms |
22340 KiB |
| test_05.txt |
AC |
304 ms |
41924 KiB |
| test_06.txt |
AC |
207 ms |
41412 KiB |
| test_07.txt |
AC |
324 ms |
42092 KiB |
| test_08.txt |
AC |
149 ms |
22728 KiB |
| test_09.txt |
AC |
250 ms |
41672 KiB |
| test_10.txt |
AC |
182 ms |
41224 KiB |
| test_11.txt |
AC |
113 ms |
22356 KiB |
| test_12.txt |
AC |
274 ms |
79500 KiB |
| test_13.txt |
AC |
272 ms |
79372 KiB |
| test_14.txt |
AC |
440 ms |
79428 KiB |
| test_15.txt |
AC |
442 ms |
79436 KiB |
| test_16.txt |
AC |
442 ms |
79420 KiB |
| test_17.txt |
AC |
444 ms |
79368 KiB |
| test_18.txt |
AC |
443 ms |
79352 KiB |
| test_19.txt |
AC |
443 ms |
79416 KiB |
| test_20.txt |
AC |
443 ms |
79436 KiB |
| test_21.txt |
AC |
447 ms |
79432 KiB |