QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#820444 | #4593. Darkmoon Faire | SGColin | AC ✓ | 757ms | 65152kb | C++20 | 3.4kb | 2024-12-18 21:28:11 | 2024-12-18 21:28:12 |
Judging History
answer
#include<cstdio>
#include<cctype>
#include<cstring>
using namespace std;
typedef long long LL;
const int maxn=300000,maxt=maxn<<2,MOD=998244353;
int te,n,a[maxn+5],f[maxn+5];
int top[2],stk[2][maxn+5];
int sum[maxt+5][2],val[maxt+5][4][2],sta[maxt+5],tag[maxt+5][2];
#define EOLN(x) ((x)==10 || (x)==13 || (x)==EOF)
inline char readc(){
static char buf[1<<16],*l=buf,*r=buf;
return l==r && (r=(l=buf)+fread(buf,1,1<<16,stdin),l==r)?EOF:*l++;
}
template<typename T> int readi(T &x){
T tot=0;char ch=readc(),lst='+';
while (!isdigit(ch)) {if (ch==EOF) return EOF;lst=ch;ch=readc();}
while (isdigit(ch)) tot=(tot<<3)+(tot<<1)+(ch^48),ch=readc();
lst=='-'?x=-tot:x=tot;return EOLN(ch);
}
struct fastO{
int si;char buf[1<<16];
fastO() {si=0;}
void putc(char ch){
if (si==(1<<16)) fwrite(buf,1,si,stdout),si=0;
buf[si++]=ch;
}
~fastO() {fwrite(buf,1,si,stdout);}
}fo;
template<typename T> void writei(T x,char ch='\n'){
int len=0,buf[100];
if (x<0) fo.putc('-'),x=-x;
do buf[len++]=x%10,x/=10; while (x);
while (len) fo.putc(buf[--len]+48);
fo.putc(ch);
}
void Build(int L,int R,int p=1){
memset(sum[p],0,sizeof(sum[p]));
memset(val[p],0,sizeof(val[p]));
tag[p][0]=tag[p][1]=-1;
if (L==R) {sta[p]=0;return;}
int mid=L+(R-L>>1);
Build(L,mid,p<<1);Build(mid+1,R,p<<1|1);
}
inline int ADD(int x,int y) {return x+y>=MOD?x+y-MOD:x+y;}
void Pushup(int p){
for (int j=0;j<2;j++) sum[p][j]=ADD(sum[p<<1][j],sum[p<<1|1][j]);
for (int j=0;j<4;j++)
for (int k=0;k<2;k++)
val[p][j][k]=ADD(val[p<<1][j][k],val[p<<1|1][j][k]);
}
void Addtag(int d,int p,int k){
static int tem[4][2];
for (int j=0;j<4;j++)
for (int k=0;k<2;k++)
tem[j][k]=val[p][j][k],val[p][j][k]=0;
for (int j=0;j<4;j++){
int t=(j&((1<<d)^3))|(k<<d);
for (int k=0;k<2;k++)
val[p][t][k]=ADD(val[p][t][k],tem[j][k]);
}
sta[p]=(sta[p]&((1<<d)^3))|(k<<d);
tag[p][d]=k;
}
void Pushdown(int p){
if (~tag[p][0]) Addtag(0,p<<1,tag[p][0]),Addtag(0,p<<1|1,tag[p][0]),tag[p][0]=-1;
if (~tag[p][1]) Addtag(1,p<<1,tag[p][1]),Addtag(1,p<<1|1,tag[p][1]),tag[p][1]=-1;
}
void Insert(int pos,int k,int l=1,int r=n,int p=1){
if (l==r){
sum[p][pos&1]=k;
val[p][sta[p]][0]=sum[p][0];
val[p][sta[p]][1]=sum[p][1];
return;
}
int mid=l+(r-l>>1);Pushdown(p);
pos<=mid?Insert(pos,k,l,mid,p<<1):Insert(pos,k,mid+1,r,p<<1|1);
Pushup(p);
}
void Update(int d,int L,int R,int k,int l=1,int r=n,int p=1){
if (L==l && r==R) return Addtag(d,p,k);
int mid=l+(r-l>>1);Pushdown(p);
if (R<=mid) Update(d,L,R,k,l,mid,p<<1); else if (L>mid) Update(d,L,R,k,mid+1,r,p<<1|1);
else Update(d,L,mid,k,l,mid,p<<1),Update(d,mid+1,R,k,mid+1,r,p<<1|1);
Pushup(p);
}
int main(){
for (readi(te);te;te--){
readi(n);
for (int i=1;i<=n;i++) readi(a[i]);
Build(1,n);top[0]=top[1]=0;
f[0]=1;Insert(1,f[0]);
for (int i=1;i<=n;i++){
while (top[0] && a[i]<a[stk[0][top[0]]]) top[0]--;
while (top[1] && a[i]>a[stk[1][top[1]]]) top[1]--;
stk[0][++top[0]]=i;
Update(0,stk[0][top[0]-1]+1,stk[0][top[0]],i&1^1);
stk[1][++top[1]]=i;
Update(1,stk[1][top[1]-1]+1,stk[1][top[1]],i&1);
// printf("[%d]\n",i);
// for (int i=1;i<=top[0];i++) printf("%d ",stk[0][i]);puts("");
// for (int i=1;i<=top[1];i++) printf("%d ",stk[1][i]);puts("");
f[i]=ADD(val[1][0][0],val[1][3][1]);
if (i<n) Insert(i+1,f[i]);
// printf("f[%d]=%d\n",i,f[i]);
// puts("");
}
writei(f[n]);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 757ms
memory: 65152kb
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