QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#615345 | #7039. Counting Failures on a Trie | ucup-team4906 | AC ✓ | 3943ms | 62000kb | C++20 | 3.3kb | 2024-10-05 18:10:20 | 2024-10-05 18:10:20 |
Judging History
answer
//by 72
#include<bits/stdc++.h>
#define F(i, a, b) for(int i = a; i <= b; i ++)
#define Fd(i, a, b) for(int i = a; i >= b; i --)
#define pb push_back
#define pii pair<int, int>
#define fi first
#define se second
#define int long long
using namespace std;
const int mod = 998244353;
const int N = 2e5 + 10;
const int inf = 1e18;
typedef array<int, 3> a3;
typedef long long ll;
const int mod1 = 998244353;
const int mod2 = 1e9 + 9;
const int base1 = 250316;
const int base2 = 171925;
int n, m, q;
vector<pair<int, char>> ed[N];
struct Hash {
int n; string s;
vector<pii> ha, pw;
void init(string t){
s = t;
n = s.size();
ha.resize(n + 1), pw.resize(n + 1);
pw[0] = {1, 1};
F(i, 1, n) {
pw[i].fi = 1ll * pw[i - 1].fi * base1 % mod1;
pw[i].se = 1ll * pw[i - 1].se * base2 % mod2;
}
F(i, 1, n) {
ha[i].fi = (1ll * ha[i - 1].fi * base1 + s[i - 1]) % mod1;
ha[i].se = (1ll * ha[i - 1].se * base2 + s[i - 1]) % mod2;
}
}
pii hsh(int l, int r) {
int f1 = (ha[r].fi - (1ll * ha[l - 1].fi * pw[r - l + 1].fi) % mod1 + mod1) % mod1;
int f2 = (ha[r].se - (1ll * ha[l - 1].se * pw[r - l + 1].se) % mod2 + mod2) % mod2;
return {f1, f2};
}
};
pii now;
map<pii, int> mp;
void dfs(int u) {
for(auto [v, c] : ed[u]) {
pii lst = now;
now.fi = (1ll * lst.fi * base1 + c) % mod1;
now.se = (1ll * lst.se * base2 + c) % mod2;
mp[now] = v;
dfs(v);
now = lst;
}
}
int nxt[20][N];
void sol() {
cin >> n >> m >> q;
F(i, 0, n) ed[i].clear();
F(i, 0, 19) F(j, 1, m) nxt[i][j] = 0;
F(i, 1, n) {
int x; char c; cin >> x >> c;
ed[x].push_back({i, c});
}
string s; cin >> s;
Hash H; H.init(s);
s = " " + s, now = {0, 0}, mp.clear();
dfs(0);
F(i, 1, m) {
int l = i - 1, r = m;
auto check = [&](int x) -> bool {
if(x == i - 1) return true;
if(mp.count(H.hsh(i, x))) return true;
else return false;
};
while(l < r) {
int mid = l + r + 1 >> 1;
if(check(mid)) l = mid;
else r = mid - 1;
}
nxt[0][i] = min(m + 1, l + 1);
// cout << i << " " << nxt[0][i] << "!\n";
// cout << nxt[0][i] << " \n" [i == n];
}
F(i, 1, 19) F(j, 1, m) {
if(nxt[i - 1][j] >= m) nxt[i][j] = m + 1;
else nxt[i][j] = nxt[i - 1][nxt[i - 1][j] + 1];
}
F(i, 1, q) {
int l, r; cin >> l >> r;
int res = 0;
// cout << l << " " << r << ":\n";
Fd(j, 19, 0) {
if(nxt[j][l] < r) {
l = nxt[j][l] + 1;
res += (1 << j);
// cout << j << " " << l << "??\n";
}
}
if(l == r) {
if(mp.count(H.hsh(l, r))) cout << res << " " << mp[H.hsh(l, r)] << "\n";
else cout << res + 1 << " " << 0 << "\n";
continue;
}
if(mp.count(H.hsh(l, r))) {
cout << res << " " << mp[H.hsh(l, r)] << "\n";
} else {
assert(mp.count(H.hsh(l, r - 1)));
cout << res + 1 << " " << 0 << "\n";
}
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t; cin >> t;
F(i, 1, t) sol();
return 0;
}
//sldl
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 34224kb
input:
1 5 10 5 0 a 0 b 1 a 1 c 2 a aaacbaacab 1 5 1 6 1 7 3 9 4 10
output:
2 2 2 5 3 0 2 1 4 0
result:
ok 5 lines
Test #2:
score: 0
Accepted
time: 957ms
memory: 34652kb
input:
1000 1000 1000 1000 0 a 1 a 2 b 3 a 4 b 5 a 6 b 7 b 8 a 9 b 10 b 11 b 12 b 13 b 14 b 15 a 16 a 17 b 18 a 19 a 20 b 21 a 22 b 23 a 24 b 25 a 26 b 27 b 28 b 29 b 30 a 31 b 32 a 33 a 34 a 35 b 36 b 37 b 38 a 39 a 40 b 41 a 42 b 43 a 44 b 45 b 46 a 47 a 48 a 49 a 50 b 51 b 52 a 53 b 54 b 55 a 56 a 57 b ...
output:
59 546 43 328 141 219 5 449 132 391 42 0 147 271 58 0 38 328 123 154 99 3 46 227 23 3 38 2 146 491 15 5 141 6 8 154 9 175 150 272 163 175 119 393 172 391 63 2 165 4 36 503 78 744 16 602 61 999 81 219 116 1 96 0 158 602 53 999 176 0 82 449 108 0 87 744 33 1 74 0 2 5 13 154 60 328 136 709 68 3 56 491 ...
result:
ok 1000000 lines
Test #3:
score: 0
Accepted
time: 3943ms
memory: 62000kb
input:
10 100000 100000 100000 0 a 1 b 2 b 3 b 4 b 5 b 6 b 7 b 8 b 9 a 10 b 11 a 12 a 13 a 14 b 15 b 16 b 17 b 18 a 19 a 20 b 21 a 22 b 23 a 24 b 25 a 26 b 27 a 28 b 29 a 30 a 31 b 32 b 33 a 34 a 35 a 36 a 37 a 38 b 39 b 40 b 41 b 42 b 43 b 44 a 45 a 46 a 47 a 48 a 49 b 50 b 51 b 52 a 53 a 54 b 55 a 56 b 5...
output:
307 77 696 39 292 33 1101 72778 797 7 923 59 530 22 175 7 751 2 641 23 999 54 771 60 1031 19 736 10 1828 25 1223 49 1119 8 1360 1 1010 25 186 10 634 11 1898 60 515 138 67 44 698 25 678 104 585 7 1179 34332 643 6 551 31 758 8 474 13 763 25 1042 85 1385 21 248 56 712 7 161 12 650 1 883 276 155 21 110 ...
result:
ok 1000000 lines
Test #4:
score: 0
Accepted
time: 3374ms
memory: 60480kb
input:
10 100000 100000 100000 0 a 1 b 2 a 3 b 4 b 5 a 6 b 7 a 8 b 9 b 10 a 11 b 12 a 13 b 14 b 15 a 16 b 17 a 18 b 19 b 20 a 21 b 22 a 23 b 24 b 25 a 26 b 27 a 28 b 29 b 30 a 31 b 32 a 33 b 34 b 35 a 36 b 37 a 38 b 39 b 40 a 41 b 42 a 43 b 44 b 45 a 46 b 47 a 48 b 49 b 50 a 51 b 52 a 53 b 54 b 55 a 56 b 5...
output:
1 5257 2 30770 1 27739 1 51805 2 6968 2 78118 1 7789 1 45259 2 35890 2 40254 2 908 1 10889 0 71302 2 10214 1 37077 2 18525 1 54529 1 25673 0 20925 1 35947 2 27976 2 28747 1 38487 1 15732 1 11502 2 33825 0 2153 2 64539 0 30535 1 70629 2 32885 0 58060 2 925 2 62115 1 10023 2 66651 1 21139 2 66936 0 28...
result:
ok 1000000 lines