QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#353223 | #6842. Popcount Words | iee | WA | 52ms | 56680kb | C++23 | 2.2kb | 2024-03-13 23:34:20 | 2024-03-13 23:34:21 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
constexpr int N = 5e5 + 5;
int q, trie[N][2], tot = 1, fail[N], f[N][30][2], g[N][30][2], occ[N];
vector<int> vec[N], son[N];
void insert(string s, int id) {
int now = 1;
for (char c : s) {
int &nxt = trie[now][c - '0'];
if (!nxt) nxt = ++tot;
now = nxt;
}
vec[now].push_back(id);
}
void build() {
queue<int> Q;
for (int i : {0, 1}) {
if (trie[1][i]) {
fail[trie[1][i]] = 1;
Q.push(trie[1][i]);
} else {
trie[1][i] = 1;
}
}
while (!Q.empty()) {
int u = Q.front();
Q.pop();
for (int i : {0, 1}) {
if (trie[u][i]) {
fail[trie[u][i]] = trie[fail[u]][i];
Q.push(trie[u][i]);
} else {
trie[u][i] = trie[fail[u]][i];
}
}
}
for (int i = 2; i <= tot; i++) {
son[fail[i]].push_back(i);
}
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
int n;
cin >> n >> q;
vector<array<int, 2>> seg;
for (int i = 0, l, r; i < n; i++) {
cin >> l >> r;
auto check = [&](int k) {
if (l + (1 << k) - 1 > r || l % (1 << k)) return;
seg.push_back({k, __builtin_parity(l)});
l += (1 << k);
};
for (int k = 0; k < 30; k++) {
check(k);
check(k);
}
for (int k = 29; k >= 0; k--) {
check(k);
}
}
for (int i = 0; i < q; i++) {
string s;
cin >> s;
insert(s, i);
}
build();
for (int i = 1; i <= tot; i++) {
for (int j : {0, 1}) {
f[i][0][j] = trie[i][j];
}
}
for (int k = 1; k < 30; k++) {
for (int i = 1; i <= tot; i++) {
for (int j : {0, 1}) {
f[i][k][j] = f[f[i][k - 1][j]][k - 1][!j];
}
}
}
int now = 1;
for (auto [k, v] : seg) {
g[now][k][v]++;
now = f[now][k][v];
}
for (int k = 28; k >= 0; k--) {
for (int i = 1; i <= tot; i++) {
for (int j : {0, 1}) {
g[i][k][j] += g[i][k + 1][j];
g[f[i][k][j]][k][!j] += g[i][k + 1][j];
}
}
}
for (int i = 1; i <= tot; i++) {
occ[trie[i][0]] += g[i][0][0];
occ[trie[i][1]] += g[i][0][1];
}
vector<int> ans(q);
function<void(int)> dfs = [&](int u) {
for (int v : son[u]) {
dfs(v);
occ[u] += occ[v];
}
for (int i : vec[u]) {
ans[i] = occ[u];
}
};
dfs(1);
for (int x : ans) {
cout << x << "\n";
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 11708kb
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: 11764kb
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: 52ms
memory: 56680kb
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:
202939 202491 73146 129793 129793 72697 13168 59978 70233 59560 59978 69815 59559 13137 2343 10825 30821 29157 31062 39171 48849 10710 10825 49153 39412 30403 28915 30644 10710 2427 305 2038 5005 5820 9066 21755 23819 5337 5534 25528 17557 21614 23176 25673 8640 2070 2038 8787 25816 23337 21996 1741...
result:
wrong answer 1st lines differ - expected: '273828', found: '202939'