QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#837615#6842. Popcount Wordswang54321ML 57ms68036kbC++142.7kb2024-12-30 09:51:212024-12-30 09:51:22

Judging History

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

  • [2024-12-30 09:51:22]
  • 评测
  • 测评结果:ML
  • 用时:57ms
  • 内存:68036kb
  • [2024-12-30 09:51:21]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
using llt=long long;
using ull=unsigned long long;
const llt N=500100;
llt n,q,L[N],R[N],Q[N],us=1,ans[N];char s[N];
struct TRIE 
{
    llt son[N][2],fail[N],siz=1,nxt[2][N][33],tag[2][N][33],in[N],v[N];
    llt insert(char* S,llt Len)
    {
        llt now=1;
        for(int i=1;i<=Len;i++)
        {
            if(!son[now][S[i]-'0'])  son[now][S[i]-'0']=++siz;
            now=son[now][S[i]-'0'];
        }
        return now;
    }
    void build()
    {
        queue<llt> qu;
        for(int i=0;i<=1;i++)   if(son[1][i]) fail[son[1][i]]=1,in[1]++,qu.push(son[1][i]);else son[1][i]=1;
        while(!qu.empty())
        {
            llt now=qu.front();qu.pop();
            for(int i=0;i<=1;i++)   
                if(son[now][i]) fail[son[now][i]]=son[fail[now]][i],qu.push(son[now][i]),in[son[fail[now]][i]]++;
                else son[now][i]=son[fail[now]][i];
        }
        for(int i=1;i<=siz;i++) nxt[0][i][0]=son[i][0],nxt[1][i][0]=son[i][1];
        for(int j=1;(1<<j)<=1e9;j++)
            for(int i=1;i<=siz;i++)
                nxt[0][i][j]=nxt[1][nxt[0][i][j-1]][j-1],
                nxt[1][i][j]=nxt[0][nxt[1][i][j-1]][j-1];
    }
    #define C(o) (__builtin_popcount(o)&1)
    void Run(llt &now,llt l,llt r)
    {
        for(int i=0;(1<<i)<=(r-l+1);i++)
            if(l&(1<<i))    tag[C(l)][now][i]++,now=nxt[C(l)][now][i],l+=(1<<i);
        for(int i=30;i>=0;i--)
            if((1<<i)&(r-l+1))
                tag[C(l)][now][i]++,now=nxt[C(l)][now][i],l+=(1<<i);
    }
    void solve()
    {
        for(int j=30;j>=1;j--)
            for(int i=1;i<=siz;i++)
                tag[0][i][j-1]+=tag[0][i][j],tag[1][nxt[0][i][j-1]][j-1]+=tag[0][i][j],
                tag[1][i][j-1]+=tag[1][i][j],tag[0][nxt[1][i][j-1]][j-1]+=tag[1][i][j];
        for(int i=1;i<=siz;i++) v[son[i][0]]+=tag[0][i][0],v[son[i][1]]+=tag[1][i][0];
    }
    void topu()
    {
        queue<llt> qu;
        for(int i=1;i<=siz;i++) if(!in[i])  qu.push(i);
        while(!qu.empty())  
        {
            llt tmp=qu.front();qu.pop();
            v[fail[tmp]]+=v[tmp];in[fail[tmp]]--;
            if(!in[fail[tmp]])    qu.push(fail[tmp]);
        }
    }
}trie;
int main()
{
    #ifdef LOCAL
        freopen("1.in","r",stdin);
        freopen("1.out","w",stdout);
    #endif
    scanf("%lld%lld",&n,&q);
    for(int i=1;i<=n;i++)   scanf("%lld%lld",&L[i],&R[i]);
    for(int i=1;i<=q;i++)   scanf("%s",s+1),Q[i]=trie.insert(s,strlen(s+1));
    trie.build();for(int i=1;i<=n;i++)   trie.Run(us,L[i],R[i]);
    trie.solve();trie.topu();
    for(int i=1;i<=q;i++)   printf("%lld\n",trie.v[Q[i]]);
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 26564kb

input:

3 5
2 6
1 3
4 8
0
1
11
101
0011010

output:

6
7
2
2
1

result:

ok 5 lines

Test #2:

score: 0
Accepted
time: 0ms
memory: 26332kb

input:

3 10
2 6
1 3
4 8
0
1
11
101
0011010
0
1
11
101
0011010

output:

6
7
2
2
1
6
7
2
2
1

result:

ok 10 lines

Test #3:

score: 0
Accepted
time: 57ms
memory: 68036kb

input:

100000 37701
605224571 605224571
681034454 681034463
468837041 468837048
323235128 323235135
400367345 400367345
394938241 394938241
221026001 221026007
183872918 183872926
881878131 881878138
374491962 374491967
588030380 588030381
109143398 109143401
727016146 727016149
857189138 857189138
1940312...

output:

273828
273405
99633
174195
174195
99209
16229
83404
91124
83071
83404
90791
83070
16138
3449
12780
43045
40359
43221
47903
70380
12690
12780
70624
48079
42712
40183
42887
12690
3448
413
3036
6576
6204
11678
31367
34167
6191
6484
36737
16633
31270
33957
36423
9697
2993
3036
9744
36469
34155
31543
165...

result:

ok 37701 lines

Test #4:

score: -100
Memory Limit Exceeded

input:

100000 2064
155864032 155864033
351106162 351106162
63569309 63569310
446198383 446198387
844050143 844050148
28666643 28666652
948049121 948049128
422938918 422938925
590576664 590576664
118827333 118827339
248813547 248813549
222041903 222041911
481862624 481862626
39190429 39190429
373420004 3734...

output:

274777
274799
99973
174803
174803
99996
16241
83732
91053
83750
83732
91070
83750
16246
3457
12784
43222
40510
43091
47961
70993
12757
12784
70948
47831
43239
40641
43109
12757
3489
459
2998
6565
6219
11607
31614
34345
6165
6467
36624
16421
31540
34510
36483
9693
3064
2998
9786
36657
34291
31484
163...

result: