QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#864124 | #6842. Popcount Words | ddxrS | ML | 56ms | 71232kb | C++14 | 2.3kb | 2025-01-20 10:44:02 | 2025-01-20 10:44:02 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
typedef long long ll;
using namespace std;
const int N = 1e6 + 5;
int n, q, tcnt, l[N], r[N];
int cnt, siz[N], flg[N];
void solve(int l, int r, int ql, int qr) {
int mid = l + r >> 1;
if(ql <= l && r <= qr) {
int k = 31 - __builtin_clz(r - l), p = l >> k;
cnt++, siz[cnt] = k, flg[cnt] = __builtin_popcount(p) & 1;
return;
}
if(ql < mid) solve(l, mid, ql, qr);
if(mid < qr) solve(mid, r, ql, qr);
}
struct node {int son[2], fail;} trie[N];
int pos[N];
int insert(char* s) {
int len = strlen(s), u = 0;
for(int i = 0; i < len; i++) {
int v = s[i] - '0';
if(!trie[u].son[v]) trie[u].son[v] = ++tcnt;
u = trie[u].son[v];
}
return u;
}
void build() {
queue<int> q;
for(int i = 0; i < 2; i++) if(trie[0].son[i]) q.push(trie[0].son[i]);
while(!q.empty()) {
int u = q.front();
q.pop();
for(int i = 0; i < 2; i++) {
if(trie[u].son[i]) trie[trie[u].son[i]].fail = trie[trie[u].fail].son[i], q.push(trie[u].son[i]);
else trie[u].son[i] = trie[trie[u].fail].son[i];
}
}
}
int f[N][30][2], g[N][30][2], c[N][30][2];
ll ans[N];
signed main() {
#ifdef ddxrS
freopen("sample.in", "r", stdin);
freopen("sample.out", "w", stdout);
#endif
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin>>n>>q;
for(int i = 1; i <= n; i++) cin>>l[i]>>r[i];
for(int i = 1; i <= q; i++) {
char s[N]; cin>>s;
pos[i] = insert(s);
}
build();
for(int i = 0; i <= tcnt; i++) f[i][0][0] = trie[i].son[0], f[i][0][1] = trie[i].son[1];
for(int j = 1; j < 30; j++) for(int i = 0; i <= tcnt; 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];
for(int i = 1, u = 0; i <= n; i++) {
cnt = 0, solve(0, 1 << 30, l[i], r[i] + 1);
for(int j = 1; j <= cnt; j++) c[u][siz[j]][flg[j]]++, u = f[u][siz[j]][flg[j]];
}
for(int j = 29; j >= 0; j--) for(int i = 0; i <= tcnt; i++)
g[i][j][0] += c[i][j][0] + g[i][j + 1][0], g[i][j][1] += c[i][j][1] + g[i][j + 1][1],
g[f[i][j][0]][j][1] += g[i][j + 1][0], g[f[i][j][1]][j][0] += g[i][j + 1][1];
for(int i = 0; i <= tcnt; i++) ans[trie[i].son[0]] += g[i][0][0], ans[trie[i].son[1]] += g[i][0][1];
for(int i = tcnt; i >= 1; i--) ans[trie[i].fail] += ans[i];
for(int i = 1; i <= q; i++) cout<<ans[pos[i]]<<'\n';
cerr<<clock() * 1.0 / CLOCKS_PER_SEC<<'\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 19380kb
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: 19336kb
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: 56ms
memory: 71232kb
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:
4050 3660 2149 1900 1991 1669 624 1525 826 1074 1098 892 808 861 287 337 1008 517 150 675 549 525 254 844 172 720 596 212 416 445 159 128 337 0 203 804 196 321 59 91 342 333 439 110 442 83 175 79 311 533 29 143 509 211 200 396 21 191 333 83 304 141 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 0 1 1...