QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#65153 | #4593. Darkmoon Faire | zzxzzx123 | AC ✓ | 838ms | 106932kb | C++20 | 2.5kb | 2022-11-27 20:00:56 | 2022-11-27 20:00:57 |
Judging History
answer
#include<bits/stdc++.h>
#define ls p<<1
#define rs p<<1|1
#define ll long long
using namespace std;
const int N=3e5+10,P=998244353;
int T,n,a[N],Stack1[N],top1,Stack2[N],top2;
ll f[N],sum[2][N];
struct Tree
{
int l[2],r[2],lazy[2][3];
ll dat[2][3];
#define l(x,p) t[p].l[x]
#define r(x,p) t[p].r[x]
#define dat(y,p,x) t[p].dat[y][x]
#define lazy(y,p,x) t[p].lazy[y][x]
}t[N<<2];
//1记录奇数位置的情况,0记录偶数位置的情况.
inline void build(int p,int l,int r)
{
for(int i=0;i<2;++i)
{
l(i,p)=l;r(i,p)=r;
dat(i,p,0)=dat(i,p,1)=dat(i,p,2)=0;
lazy(i,p,1)=lazy(i,p,2)=0;
}
if(l==r) return;
int mid=l+r>>1;
build(ls,l,mid);
build(rs,mid+1,r);
}
inline void add(int kp,int p,int k)
{
lazy(kp,p,k)=1;
dat(kp,p,0)=dat(kp,p,3-k);
dat(kp,p,k)=((sum[kp^1][r(kp,p)-1]-(l(kp,p)-2<0?0:sum[kp^1][l(kp,p)-2]))%P+P)%P;
}
inline void del(int kp,int p,int k)
{
lazy(kp,p,k)=-1;
dat(kp,p,k)=dat(kp,p,0)=0;
}
inline void push(int k,int p)
{
for(int i=1;i<=2;++i)
{
if(lazy(k,p,i))
{
if(lazy(k,p,i)==1) add(k,ls,i),add(k,rs,i);
if(lazy(k,p,i)==-1) del(k,ls,i),del(k,rs,i);
lazy(k,p,i)=0;
}
}
}
inline void alter(int k,int p,int l,int r,int op,int val)
{
if(l<=l(k,p)&&r>=r(k,p))
{
if(val==1) add(k,p,op);
else del(k,p,op);
return;
}
push(k,p);
int mid=l(k,p)+r(k,p)>>1;
if(l<=mid) alter(k,ls,l,r,op,val);
if(r>mid) alter(k,rs,l,r,op,val);
for(int i=0;i<3;++i) dat(k,p,i)=dat(k,ls,i)+dat(k,rs,i);
return;
}
int main()
{
// freopen("1.in","r",stdin);
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=1;i<=n;++i) scanf("%d",&a[i]);
build(1,1,n);
top1=top2=0;
f[0]=1;sum[0][0]=1;sum[1][0]=0;
for(int i=1;i<=n;++i)
{
while(top1&&a[i]>a[Stack1[top1]]) top1--;
Stack1[++top1]=i;//维护当前的向上递减的栈
if(i&1)
{
alter(1,1,Stack1[top1-1]+1,i,1,1);
alter(0,1,Stack1[top1-1]+1,i,1,0);
}
else
{
alter(0,1,Stack1[top1-1]+1,i,1,1);
alter(1,1,Stack1[top1-1]+1,i,1,0);
}
while(top2&&a[i]<a[Stack2[top2]]) top2--;
Stack2[++top2]=i;
if(i&1)
{
alter(0,1,Stack2[top2-1]+1,i,2,1);
alter(1,1,Stack2[top2-1]+1,i,2,0);
}
else
{
alter(1,1,Stack2[top2-1]+1,i,2,1);
alter(0,1,Stack2[top2-1]+1,i,2,0);
}
f[i]=(dat(1,1,0)+dat(0,1,0))%P;
sum[0][i]=sum[0][i-1];
sum[1][i]=sum[1][i-1];
if(i&1) sum[1][i]=(sum[1][i]+f[i])%P;
else sum[0][i]=(sum[0][i]+f[i])%P;
}
printf("%lld\n",f[n]);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 838ms
memory: 106932kb
input:
4 300000 755900426 263585675 587172343 877004671 963559951 504355670 306164508 988722056 544618772 865338006 915826014 767023342 796004639 912685743 219928703 52575265 339635052 499642509 217247848 549649089 41848450 198239532 386065393 723588136 192758555 815017802 45197937 603499890 59518471 25526...
output:
27118667 477592178 378132727 378132727
result:
ok 4 lines