QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#214862 | #5474. Incredibly Cute Penguin Chicks | wxhtzdy | RE | 0ms | 0kb | C++20 | 2.8kb | 2023-10-15 00:41:51 | 2023-10-15 00:41:52 |
answer
#include<bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef long double ld;
template <typename T> bool chkmin(T &x,T y){return x>y?x=y,1:0;}
template <typename T> bool chkmax(T &x,T y){return x<y?x=y,1:0;}
int readint(){
int x=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int cys=998244353;
int mod(int x){return x%cys;}
int n,a[1000005],pref[4][1000005][2],dp[1000005],g[4][1000005],v[4][1000005],len[4][1000005];
char s[1000005];
vector<int> fenw[4][1000005];
void set_size(int x,int y,int k){
fenw[x][y].resize(k);
len[x][y]=k;
}
void change(int x,int y,int p,int v){
for(p++;p<=len[x][y];p+=p&-p) fenw[x][y][p]=mod(fenw[x][y][p]+v);
}
int ask(int x,int y,int p){
int ret=0;
for(p++;p>0;p-=p&-p) ret=mod(ret+fenw[x][y][p]);
return ret;
}
/* struct f{
vector<int> fnw;
int n;
f(){n=0;fnw.clear();}
void set_size(int x){
n=x; fnw.resize(x+1);
}
void change(int p,int v){
for(++p;p<=n;p+=p&-p) fnw[p]=mod(fnw[p]+v);
}
int query(int p){
int ret=0;
for(p++;p>0;p-=p&-p) ret=mod(ret+fnw[p]);
return ret;
}
}fenw[4][1000005]; */
int main(){
scanf("%s",s+1);
n=strlen(s+1);
for(int i=1;i<=n;i++) a[i]=(s[i]=='C'?1:(s[i]=='I'?2:3));
for(int mx=1;mx<=3;mx++){
int x,y;
if(mx==1) x=2,y=3;
if(mx==2) x=3,y=1;
if(mx==3) x=1,y=2;
for(int i=1;i<=n;i++){
pref[mx][i][0]=pref[mx][i-1][0];
if(a[i]==x) pref[mx][i][0]++;
if(a[i]==y) pref[mx][i][0]--;
}
vector<int> xs;
for(int i=0;i<=n;i++) xs.pb(pref[mx][i][0]);
sort(xs.begin(),xs.end());
xs.erase(unique(xs.begin(),xs.end()),xs.end());
for(int i=0;i<=n;i++)
g[mx][i]=(int)(lower_bound(xs.begin(),xs.end(),pref[mx][i][0])-xs.begin());
for(int i=1;i<=n;i++){
pref[mx][i][1]=pref[mx][i-1][1];
if(a[i]==mx) pref[mx][i][1]+=2;
if(a[i]!=mx) pref[mx][i][1]-=1;
}
map<int,vector<int>> ids;
for(int i=0;i<=n;i++){
ids[pref[mx][i][0]].pb(i);
}
for(auto&p:ids){
xs.clear();
xs.pb(-3000000);
for(int i:p.se) xs.pb(pref[mx][i][1]);
sort(xs.begin(),xs.end());
xs.erase(unique(xs.begin(),xs.end()));
for(int i:p.se)
v[mx][i]=(int)(lower_bound(xs.begin(),xs.end(),pref[mx][i][1])-xs.begin());
set_size(mx,g[mx][p.se[0]],xs.size());
}
}
dp[0]=1;
for(int i=0;i<=n;i++){
for(int mx=1;mx<=3;mx++){
int val=ask(mx,g[mx][i],v[mx][i]-1);
dp[i]=mod(dp[i]+val);
}
for(int mx=1;mx<=3;mx++){
change(mx,g[mx][i],v[mx][i],dp[i]);
}
}
printf("%d\n",dp[n]);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
ICPC