QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#393355 | #6837. AC Automaton | 2745518585 | TL | 7822ms | 109764kb | C++20 | 4.7kb | 2024-04-18 15:34:36 | 2024-04-18 15:34:36 |
Judging History
answer
#pragma GCC optimize("O2")
#include<cstdio>
#include<algorithm>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
inline char gc()
{
static char buf[100000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
template<typename T> inline void read(T &x)
{
T u=1,t=0;char c=gc();
while(c<'0'||c>'9') {if(c=='-') u=-1; c=gc();}
while(c>='0'&&c<='9') t*=10,t+=c-'0',c=gc();
x=u*t;return;
}
template<typename T,typename ...O> inline void read(T &x,O &...o) {read(x),read(o...);}
template<typename T> inline void print(T x)
{
if(x==0) return putchar('0'),void();
if(x<0) putchar('-'),x=-x;
int c[129]={0},k=0;
while(x) c[++k]=x%10,x/=10;
for(int i=k;i;--i) putchar(c[i]+'0');
}
template<typename T,typename ...O> inline void print(T x,O ...o) {print(x),putchar(' '),print(o...);}
const int N=500001,M=1100;
int n,m,q,fa[N],fa2[N],f[N],g[N],f1[N],f2[N],g1[N],g2[N],d1[N],d2[N],*p1[N],*p2[N],pp[M<<3][(M<<1)+10];
ll v;
char b[N];
bool h[N],h1[N],h2[N];
vector<int> a[N],a2[N];
vector<pair<int,int>> cl;
stack<int> S;
struct
{
int x,c;
}e[N];
inline void init()
{
g[1]=0;
for(int i=2;i<=n;++i)
{
g[i]=g[fa[i]];
if(b[fa[i]]!='C') --g[i];
}
for(int i=n;i>=1;--i)
{
f[i]=0;
int u=h[i];
for(auto j:a[i])
{
u+=h1[j];
f[i]+=f[j];
if(b[j]!='A') ++f[i];
}
g[i]+=f[i];
if(u>=2) h[i]=true;
if(u>0) h1[i]=true;
}
}
inline void add1(int x,int k)
{
if(b[k]=='A') ++f1[x];
if(b[k]=='?')
{
if(g[k]>0) ++g1[x];
if(abs(g[k])<=M) ++p1[x][g[k]],cl.push_back(make_pair(x,g[k]));
}
}
inline void add2(int x,int k)
{
if(b[k]=='A') ++f2[x];
if(b[k]=='?')
{
if(g[k]>0) ++g2[x];
if(abs(g[k])<=M) ++p2[x][g[k]],cl.push_back(make_pair(x,g[k]));
}
}
int dfs(int x)
{
if(!h[x])
{
S.push(x);
int u=0;
for(auto i:a[x]) u+=dfs(i);
return u;
}
S.push(x);
p1[x]=pp[++q]+(M+5);
p2[x]=pp[++q]+(M+5);
if(x!=1)
{
int z=fa[x];
while(!h[z])
{
add1(x,z);
h2[z]=true;
z=fa[z];
}
}
for(auto i:a[x])
{
int z=dfs(i);
if(z)
{
a2[x].push_back(z);
fa2[z]=x;
}
}
while(S.top()!=x)
{
if(h2[S.top()]==false) add2(x,S.top());
S.pop();
}
S.pop();
return x;
}
void sum(int &g,int &d,int *p,int k)
{
if(k==1)
{
g+=p[-d];
++d;
v+=g;
}
else if(k==-1)
{
v-=g;
--d;
g-=p[-d];
}
}
void solve1(int x,int k)
{
while(x!=1)
{
v+=(ll)f1[x]*k;
sum(g1[x],d1[x],p1[x],k);
x=fa2[x];
f[x]+=k,g[x]+=k;
}
}
void solve2(int x,int k)
{
sum(g2[x],d2[x],p2[x],k);
for(auto i:a2[x])
{
sum(g1[i],d1[i],p1[i],k);
g[i]+=k;
solve2(i,k);
}
}
void solve(int l,int r)
{
// fprintf(stderr,"%d %d\n",l,r);
// fflush(stderr);
for(int i=1;i<=n;++i)
{
f1[i]=f2[i]=g1[i]=g2[i]=d1[i]=d2[i]=0;
h[i]=h1[i]=h2[i]=false;
a2[i].clear();
}
q=0;
cl.clear();
v=0;
for(int i=l;i<=r;++i) h[e[i].x]=true;
h[1]=true;
init();
dfs(1);
vector<int> p;
for(int i=1;i<=n;++i)
{
if(h[i]) p.push_back(i);
else if(b[i]=='A') v+=f[i];
else if(b[i]=='?') v+=max(g[i],0);
}
for(int i=l;i<=r;++i)
{
int x=e[i].x,c=e[i].c;
if((b[x]!='A')^(c!='A'))
{
solve1(x,b[x]!='A'?-1:1);
}
if((b[x]!='C')^(c!='C'))
{
solve2(x,b[x]!='C'?1:-1);
}
b[x]=c;
ll w=v;
for(auto j:p)
{
if(b[j]=='A') w+=f[j];
else if(b[j]=='?') w+=max(g[j],0);
}
print(w);
putchar('\n');
}
for(auto i:cl) p1[i.first][i.second]=p2[i.first][i.second]=0;
}
int main()
{
read(n,m);
for(int i=1;i<=n;++i)
{
b[i]=gc();
while(b[i]!='A'&&b[i]!='C'&&b[i]!='?') b[i]=gc();
}
for(int i=2;i<=n;++i)
{
int x;
read(x);
a[x].push_back(i);
fa[i]=x;
}
for(int i=1;i<=m;++i)
{
read(e[i].x);
e[i].c=gc();
while(e[i].c!='A'&&e[i].c!='C'&&e[i].c!='?') e[i].c=gc();
}
for(int i=1;i<=m;++i)
{
int j=min(i+M-1,m);
solve(i,j);
i=j;
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 32316kb
input:
5 3 AC??C 1 2 2 1 1 ? 4 A 2 ?
output:
4 3 3
result:
ok 3 lines
Test #2:
score: 0
Accepted
time: 0ms
memory: 48640kb
input:
1000 1000 AC?ACCCCC?CCA??CCCCC?A?AC?C??C?ACCCACC?C?CCC?CACACA???????C?????CC?C?AAAAACCA??AAAACACACA???AACAC?CCC?CAC?AAAACCAC???ACAA??A??A?CA?A?ACACACCAA?ACACA?AC??AC??ACAAACCC??CAA?A???CC?AA??AC???A?CCCC??CACCAACC?AA?C?AAACCCA?AAA??C?CC??CACCACAACCA?AACCC?A?CCC?CC???AA?CACCCAAC??CAA??C?AA??CA???CAAA...
output:
2344 2345 2342 2342 2768 2768 2772 2772 2772 2772 2772 2772 2772 2767 2767 2767 2767 2764 2766 2766 2769 2765 2761 2764 2767 2772 2772 2772 2772 2772 2772 2777 2777 2777 2777 2774 2771 2774 2782 2778 2778 2772 2768 2772 2772 2772 2772 2772 2774 2774 2778 2781 2781 2779 2782 2784 2787 2782 2786 2788 ...
result:
ok 1000 lines
Test #3:
score: 0
Accepted
time: 7822ms
memory: 109764kb
input:
300000 300000 AAA?CA?AA?AC?A?CCA?AACCAAA???CA?ACCAACCCCAACAAA?CCAAAC?A?C??CC?C?C?CCCA?CAA?ACA??C?C?AC??CA??ACA?AA???CACAAA?CACCCCCCC?A?AAAAAC?AACCA????CCC?C?AAACCCAA?C???CCCC?AAACAAA???A?CAAC??A??A??CCCC??AA?C??ACA?AACAAA????CAA???AAAAACC?C?CCA?CCAA?AAC?CC?CA?A??CC??CCAC??C??????AAC?AA?AA?AAC?C??AAC...
output:
14995917235 14995917235 14996064601 14996083631 14995980103 14995925797 14995925797 14995925797 14995967213 14995967213 14995967213 14995876211 14995774037 14995774037 14995774037 14995876791 14995866113 14995756158 14995647554 14995647554 14995560537 14995560537 14995583619 14995583619 14995583619 ...
result:
ok 300000 lines
Test #4:
score: 0
Accepted
time: 2387ms
memory: 42236kb
input:
300000 300000 ?ACA???CCCA?C???AA??CAAAAACCC??A?CAC??C???????CAA?C?C?A?C???A?CC?CCAC?C?ACC??C?CAACA??CA?CA?CAACA??AACCC?CCCACACC?AAC?CA??C?C?CCCA?ACAA??AA?CCAACACCA?AC?C?CCCCCCAAA?CC??A?CCC???A?CA?ACAC???C??CCA??CCAA?AAC???CCCC??AA?C?C?C?CACAC?C?CA??AACC?A????C??CACAAAAA?C?CAACACA?ACCAC?A?CCCACACA??A...
output:
200180 200181 200182 200182 200182 200182 200182 200182 200183 200183 200183 200182 200183 200183 200183 200183 200183 200182 200183 200183 200183 200183 200183 200183 200183 200183 200184 200183 200182 200181 200180 200180 200180 200181 200181 200182 200181 200180 200180 200181 200182 200181 200182...
result:
ok 300000 lines
Test #5:
score: -100
Time Limit Exceeded
input:
300000 300000 A??CCAAACAC?A?CCACA?CA??ACC?CCA?CCAACACAC?A?CCC??ACC?ACC?CA?CA?C??A?CACCCC?C?AC?AAC??A???CA?C???AC?A?A?ACCCAACC?AA?CCACCCAAAA????C?ACC?????ACACA?C?A?CCC?A?AC????AC?C?A???ACA??CAACACC????CAA???ACCAC???CCCA?A?CAA?C??CCCCA?ACCA?A?CCC?ACA?C??AA?C??ACA?AAACC?CCAACCCAC?CAAA??ACC?ACCAA??????A...
output:
15015050020 15015050020 15015135045 15015202340 15015303448 15015303448 15015282042 15015282042 15015310379 15015329461 15015377957 15015514924 15015514924 15015613521 15015724614 15015640441 15015598420 15015635210 15015635210 15015544590 15015671373 15015653717 15015706346 15015805421 15015835446 ...