QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#393188#6837. AC Automaton2745518585WA 0ms25344kbC++203.3kb2024-04-18 11:42:592024-04-18 11:42:59

Judging History

你现在查看的是最新测评结果

  • [2024-04-18 11:42:59]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:25344kb
  • [2024-04-18 11:42:59]
  • 提交

answer

#include<cstdio>
#include<algorithm>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
const int N=300001,M=2000;
int n,m,d,fa[N],fa2[N],f[N],g[N],f1[N],f2[N],g1[N],g2[N],*p1[N],*p2[N];
ll v;
char b[N];
bool h[N],h2[N];
vector<int> a[N],a2[N];
stack<int> S;
struct
{
    int x,c;
}e[N];
bool dfs0(int x,int w)
{
    f[x]=0,g[x]=-w;
    if(b[x]!='C') ++w;
    int u=h[x];
    for(auto i:a[x])
    {
        u+=dfs0(i,w);
        f[x]+=f[i];
        if(b[i]!='A') ++f[x];
    }
    g[x]+=f[x];
    if(u>=2) h[x]=true;
    return u>0;
}
void add(int &f0,int &g0,int *p0,int k)
{
    ++f0;
    if(g[k]>0) ++g0;
    if(abs(g[k])<=M) ++p0[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]=(new int[M*2+10])+(M+5);
    p2[x]=(new int[M*2+10])+(M+5);
    if(x!=1)
    {
        int z=fa[x];
        while(!h[z])
        {
            add(f1[x],g1[x],p1[x],z);
            z=fa[z];
            h2[z]=true;
        }
    }
    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) add(f2[x],g2[x],p2[x],S.top());
        S.pop();
    }
    S.pop();
    return x;
}
void add1(int &g,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;
        add1(g1[x],p1[x],k);
        x=fa2[x];
        f[x]+=k,g[x]+=k;
    }
}
void solve2(int x,int k)
{
    add1(g2[x],p2[x],k);
    for(auto i:a2[x])
    {
        add1(g1[i],p1[i],k);
        g[i]+=k;
        solve2(i,k);
    }
}
void solve(int l,int r)
{
    for(int i=1;i<=n;++i)
    {
        f1[i]=f2[i]=g1[i]=g2[i]=0;
        if(p1[i]!=NULL) delete[] p1[i],p1[i]=NULL;
        if(p2[i]!=NULL) delete[] p2[i],p2[i]=NULL;
        h[i]=h2[i]=false;
        a2[i].clear();
    }
    while(!S.empty()) S.pop();
    v=d=0;
    for(int i=l;i<=r;++i) h[e[i].x]=true;
    h[1]=true;
    dfs0(1,0);
    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);
        }
        printf("%lld\n",w);
    }
}
int main()
{
    scanf("%d%d%s",&n,&m,b+1);
    for(int i=2;i<=n;++i)
    {
        int x;
        scanf("%d",&x);
        a[x].push_back(i);
        fa[i]=x;
    }
    for(int i=1;i<=m;++i)
    {
        scanf("%d%s",&e[i].x,&e[i].c);
    }
    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: 16128kb

input:

5 3
AC??C
1 2 2 1
1 ?
4 A
2 ?

output:

4
3
3

result:

ok 3 lines

Test #2:

score: -100
Wrong Answer
time: 0ms
memory: 25344kb

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
2752
2752
2756
2756
2756
2756
2756
2756
2756
2751
2751
2751
2751
2748
2750
2750
2753
2749
2745
2748
2751
2756
2756
2756
2756
2756
2756
2761
2761
2761
2761
2757
2754
2757
2765
2761
2761
2755
2751
2755
2755
2755
2755
2755
2757
2757
2761
2764
2764
2762
2765
2767
2770
2765
2770
2772
...

result:

wrong answer 5th lines differ - expected: '2768', found: '2752'