QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#393081#6842. Popcount WordscqbzdjWA 45ms44364kbC++143.3kb2024-04-18 08:50:312024-04-18 08:50:32

Judging History

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

  • [2024-04-18 08:50:32]
  • 评测
  • 测评结果:WA
  • 用时:45ms
  • 内存:44364kb
  • [2024-04-18 08:50:31]
  • 提交

answer

#include <bits/stdc++.h>
#define LL long long
#define mes(s, x) memset(s, x, sizeof(s))
#define Maxn 100005
#define Maxl 500005
using namespace std;
inline LL read(){char c;c = getchar();while(!(('0' <= c && c <= '9') || c == '-')) c = getchar();bool flag = 0;if(c == '-'){flag = 1;c = getchar();}LL tot = 0;while('0' <= c && c <= '9'){tot = 10 * tot + c - '0';c = getchar();}return flag ? -tot : tot;}
int L[Maxn], R[Maxn];
char s[Maxn];
int id[Maxn], tr[Maxl][2], nxt[Maxl][30][2], cnt, fail[Maxl], px;
LL x[Maxl][30][2], ans[Maxl];
vector<int> g[Maxl];
void insert(int l, int x){
    int p = 0, c;
    for(int i = 1; i <= l; i++){
        c = s[i] - '0';
        if(!tr[p][c]) tr[p][c] = ++cnt;
        p = tr[p][c];
    }
    id[x] = p;
}
void build(){
    queue<int> q;
    for(int i = 0; i < 2; i++) if(tr[0][i]) q.push(tr[0][i]);
    int x;
    while(q.size()){
        x = q.front();
        q.pop();
        g[fail[x]].push_back(x);
        for(int i = 0; i < 2; i++){
            if(tr[x][i]){
                fail[tr[x][i]] = tr[fail[x]][i];
                q.push(tr[x][i]);
            }else tr[x][i] = tr[fail[x]][i];
        }
    }
    for(int i = 0; i <= cnt; i++) nxt[i][0][0] = tr[i][0], nxt[i][0][1] = tr[i][1];
    for(int i = 1; i < 30; i++){
        for(int j = 0; j <= cnt; j++){
            nxt[j][i][0] = nxt[nxt[j][i - 1][0]][i - 1][1];
            nxt[j][i][1] = nxt[nxt[j][i - 1][1]][i - 1][0];
        }
    }
}
void add(int i, int k){
    x[px][i][k]++;
    px = nxt[px][i][k];
}
void dfs(int i){
    ans[i] = x[i][0][0] + x[i][0][1];
    int x;
    for(int j = 0; j < g[i].size(); j++){
        x = g[i][j];
        dfs(x);
        ans[i] += ans[x];
    }
}
int popcnt(int i){
    int t = 0;
    while(i){
        t++;
        i &= i - 1;
    }
    return t;
}
int main(){
    #ifndef ONLINE_JUDGE
        freopen("in","r",stdin);
        freopen("out","w",stdout);
    #endif
    int n = read(), m = read();
    for(int i = 1; i <= n; i++) L[i] = read(), R[i] = read() + 1;
    for(int i = 1; i <= m; i++){
        scanf("%s", s + 1);
        insert(strlen(s + 1), i);
    }
    build();
    int flag;
    for(int i = 1; i <= n; i++){
        flag = 1;
        for(int j = 29; j >= 0; j--){
            if(((L[i] >> j) & 1) < ((R[i] >> j) & 1)){
                for(int k = 0; k < j; k++){
                    if((L[i] >> k) & 1){
                        add(k, popcnt(L[i]) % 2);
                        L[i] += 1 << k;
                    }
                }
                if(!((L[i] >> j) & 1)){
                    add(j, 1);
                    L[i] += 1 << j;
                }
                for(int k = j - 1; k >= 0; k--){
                    if((R[i] >> k) & 1){
                        add(k, popcnt(L[i]) % 2);
                        L[i] += 1 << k;
                    }
                }
            }
        }
    }
    for(int i = 29; i >= 1; i--){
        for(int j = 0; j <= cnt; j++){
            for(int k = 0; k < 2; k++){
                x[j][i - 1][k] += x[j][i][k];
                x[nxt[j][i - 1][k]][i - 1][k ^ 1] += x[j][i][k];
            }
        }
    }
    x[px][0][0]++;
    dfs(0);
    for(int i = 1; i <= m; i++) printf("%lld\n", ans[id[i]]);
    return 0;
}

详细

Test #1:

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

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: 15800kb

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: -100
Wrong Answer
time: 45ms
memory: 44364kb

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:

271274
275959
97191
174083
174083
101875
13920
83271
90356
83727
83271
90812
83726
18148
2896
11024
41529
41742
43241
47115
69568
14158
11024
72247
48827
41985
40030
43696
14158
3990
270
2626
5363
5661
11213
30316
34727
7014
5911
37330
16927
30188
33137
36431
10807
3351
2626
8398
36166
36081
32028
1...

result:

wrong answer 1st lines differ - expected: '273828', found: '271274'