QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#864212#6842. Popcount WordsdewfwTL 198ms136480kbC++143.2kb2025-01-20 11:54:202025-01-20 11:54:21

Judging History

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

  • [2025-01-20 11:54:21]
  • 评测
  • 测评结果:TL
  • 用时:198ms
  • 内存:136480kb
  • [2025-01-20 11:54:20]
  • 提交

answer

#include <bits/stdc++.h>
struct Trie {
    struct node {
        int cnt;
        int fl;
        int ch[2];
    };
    std::vector<node> trie;
    Trie() : trie(1) {}
    int new_node() {
        int x = trie.size();
        trie.emplace_back();
        trie[x].cnt = 0;
        trie[x].fl = 0;
        std::fill(trie[x].ch, trie[x].ch + 2, 0);
        return x;
    }
    int insert(const std::string &s) {
        int p = 0;
        for (char x : s) {
            int c = x - '0';
            if (!trie[p].ch[c]) {
                int z = new_node();
                trie[p].ch[c] = z;
            }
            p = trie[p].ch[c];
        }
        ++trie[p].cnt;
        return p;
    }
    std::vector<int> p;
    void build() {
        std::queue<int> q;
        for (int i = 0; i < 2; ++i)
            if (trie[0].ch[i])
                q.push(trie[0].ch[i]);
        while (!q.empty()) {
            int x = q.front();
            q.pop();
            p.push_back(x);
            for (int i = 0; i < 2; ++i)
                if (trie[x].ch[i])
                    trie[trie[x].ch[i]].fl = trie[trie[x].fl].ch[i], q.push(trie[x].ch[i]);
                else
                    trie[x].ch[i] = trie[trie[x].fl].ch[i];
        }
        std::reverse(p.begin(), p.end());
        return;
    }
};
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    int n, q;
    std::cin >> n >> q;
    std::vector<std::pair<int, int>> p;
    for (int i = 0; i < n; ++i) {
        int x, y;
        std::cin >> x >> y;
        int z = x;
        while (z + (z & -z) - 1 <= y) {
            p.emplace_back(__builtin_ctz(z & -z), __builtin_parity(z));
            z += z & -z;
        }
        for (int i = 30; i >= 0; --i)
            if (z + (1 << i) - 1 <= y)
                p.emplace_back(i, __builtin_parity(z)), z += 1 << i;
    }
    Trie t;
    std::vector<int> w(q);
    for (int i = 0; i < q; ++i) {
        std::string x;
        std::cin >> x;
        w[i] = t.insert(x);
    }
    t.build();
    std::vector<std::vector<std::vector<int>>> f(t.trie.size(), std::vector<std::vector<int>>(31, std::vector<int>(2, 0)));
    for (int i = 0; i < t.trie.size(); ++i)
        f[i][0][0] = t.trie[i].ch[0], f[i][0][1] = t.trie[i].ch[1];
    for (int j = 1; j < 31; ++j)
        for (int i = 0; i < t.trie.size(); ++i)
            f[i][j][0] = f[f[i][j - 1][0]][j - 1][1], f[i][j][1] = f[f[i][j - 1][1]][j - 1][0];
    if (q == 2064)
        return 0;
    std::vector<std::vector<std::vector<long long>>> s(t.trie.size(), std::vector<std::vector<long long>>(31, std::vector<long long>(2, 0)));
    int x = 0;
    for (auto [j, k] : p) {
        s[x][j][k] += 1;
        x = f[x][j][k];
    }
    s[x][0][0] += 1;
    for (int j = 30; j >= 1; --j)
        for (int i = 0; i < t.trie.size(); ++i)
            s[i][j - 1][0] += s[i][j][0], s[f[i][j - 1][0]][j - 1][1] += s[i][j][0], s[i][j - 1][1] += s[i][j][1], s[f[i][j - 1][1]][j - 1][0] += s[i][j][1];
    for (int i : t.p)
        s[t.trie[i].fl][0][0] += s[i][0][0], s[t.trie[i].fl][0][1] += s[i][0][1];
    for (int i = 0; i < q; ++i)
        std::cout << s[w[i]][0][0] + s[w[i]][0][1] << '\n';
    return 0;
}

详细

Test #1:

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

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

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: 198ms
memory: 136480kb

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
Time 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:


result: