QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#256294 | #7748. Karshilov's Matching Problem II | ucup-team1191# | WA | 13ms | 29244kb | C++20 | 3.4kb | 2023-11-18 18:23:36 | 2023-11-18 18:23:36 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define TIME (clock() * 1.0 / CLOCKS_PER_SEC)
vector<int> z_function (string s) {
int n = (int) s.length();
vector<int> z (n);
for (int i=1, l=0, r=0; i<n; ++i) {
if (i <= r)
z[i] = min (r-i+1, z[i-l]);
while (i+z[i] < n && s[z[i]] == s[i+z[i]])
++z[i];
if (i+z[i]-1 > r)
l = i, r = i+z[i]-1;
}
z[0] = 0;
return z;
}
vector<int> prefix_function (string s) {
int n = (int) s.length();
vector<int> pi (n);
for (int i=1; i<n; ++i) {
int j = pi[i-1];
while (j > 0 && s[i] != s[j])
j = pi[j-1];
if (s[i] == s[j]) ++j;
pi[i] = j;
}
return pi;
}
const int M = 2e5 + 239;
const int L = 18;
int n, m;
string s, t;
ll w[M], pref[M];
int z[M];
ll val[M];
ll pref_val[M];
int mx[L][M], pw2[M];
int getmax(int l, int r) {
int e = pw2[r - l + 1];
return max(mx[e][l], mx[e][r - (1 << e) + 1]);
}
ll f[M];
ll pref_val2[M];
void solve() {
cin >> n >> m >> s >> t;
pref[0] = 0;
for (int i = 0; i < n; i++) {
cin >> w[i];
pref[i + 1] = pref[i] + w[i];
}
{
auto zf = z_function(s + "#" + t);
for (int i = 0; i < (int)t.size(); i++) {
z[i] = zf[i + (int)s.size() + 1];
}
}
for (int i = 0; i < n; i++) {
mx[0][i] = i + z[i];
}
for (int k = 1; k < L; k++) {
for (int i = 0; i < n; i++) {
int r = (1 << (k - 1)) + i;
if (r >= n) {
mx[k][i] = mx[k - 1][i];
continue;
}
mx[k][i] = max(mx[k - 1][i], mx[k - 1][r]);
}
}
pw2[1] = 0;
for (int i = 2; i <= n; i++) {
pw2[i] = pw2[i - 1];
if ((1 << (pw2[i] + 1)) <= i) {
pw2[i]++;
}
}
for (int i = 0; i < n; i++) {
val[i] = pref[z[i]];
}
pref_val[0] = 0;
for (int i = 0; i < n; i++) {
pref_val[i + 1] = pref_val[i] + val[i];
}
{
vector<int> zf = z_function(s);
pref_val2[0] = 0;
for (int i = 1; i <= n; i++) {
pref_val2[i] = pref_val2[i - 1] + pref[zf[i - 1]];
}
vector<int> pr = prefix_function(s);
f[0] = 0;
for (int k = 1; k <= n; k++) {
f[k] = pref[k];
f[k] += f[pr[k - 1]];
f[k] += pref_val2[k - pr[k - 1]];
}
}
for (int zp = 0; zp < m; zp++) {
int l, r;
cin >> l >> r;
l--;
if (getmax(l, r - 1) < r) {
cout << pref_val[r] - pref_val[l] << "\n";
return;
}
int gl = l;
int gr = r;
while (gr - gl > 1) {
int mid = (gl + gr) / 2;
if (getmax(l, mid - 1) >= r) {
gr = mid;
} else {
gl = mid;
}
}
ll ans = 0;
ans += pref_val[gl] - pref_val[l];
ans += f[r - gl];
cout << ans << "\n";
}
}
int main() {
#ifdef ONPC
freopen("input", "r", stdin);
#endif
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int T = 1;
//cin >> t;
while (T--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 26132kb
input:
8 5 abbabaab aababbab 1 2 4 8 16 32 64 128 1 1 2 3 3 5 4 7 1 8
output:
1 3 3 16 38
result:
ok 5 lines
Test #2:
score: 0
Accepted
time: 1ms
memory: 26128kb
input:
15 4 heheheheehhejie heheheheheheheh 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 2 3 4 8 2 6 1 15
output:
3 13 13 174
result:
ok 4 lines
Test #3:
score: -100
Wrong Answer
time: 13ms
memory: 29244kb
input:
150000 150000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
108147037823514 221878585246974 455339807727642 440286198422821 148115747906237 88695249853257 351159618462315 58850354020997 65824434822005 270158033672354 197732558443069 298193894693053 239511187032650 28139154231325 408380171835227 268053430937402 32417121185965 104813548228675 44074926058619 78...
result:
wrong answer 194th lines differ - expected: '94873139075637', found: ''