QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#394678 | #7748. Karshilov's Matching Problem II | ucup-team3215# | WA | 2ms | 19728kb | C++20 | 2.8kb | 2024-04-20 17:39:02 | 2024-04-20 17:39:02 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll N = 2e5;
constexpr ll LOG = 20;
vector<int> z(const string &s) {
vector<int> z(size(s));
int l = -1, r = -1;
for (int i = 1; i < size(s); ++i) {
z[i] = i >= r ? 0 : min(r - i, z[i - l]);
while (i + z[i] < size(s) && s[i + z[i]] == s[z[i]]) { ++z[i]; }
if (i + z[i] > r) {
l = i, r = i + z[i];
}
}
return z;
}
array<ll, N> f{};
void upd(ll i, ll x) {
for (; i < N; i |= i + 1) { f[i] += x; }
}
ll get(ll i) {
ll res = 0;
for (; i >= 0; i &= i + 1, --i) {
res += f[i];
}
return res;
}
ll sum(ll l, ll r) {
return get(r) - get(l - 1);
}
int main() {
cin.tie(0)->sync_with_stdio(false);
int n, q;
cin >> n >> q;
string s, t;
cin >> s >> t;
vector<ll> a(n);
for (ll i = 0; i < n; ++i) {
cin >> a[i];
if (i)a[i] += a[i - 1];
}
vector<vector<pair<int, int>>> Q(n);
vector<int> res(q, 0);
for (int i = 0; i < q; ++i) {
int l, r;
cin >> l >> r;
--r, --l;
Q[r].push_back({l, i});
}
string kk = s + "#" + t;
auto zf = z(kk);
vector<vector<int>> ends(n);
for (int i = 0; i < n; ++i) {
int val = zf[n + 1 + i];
if (val && i + val < n) {
ends[i + val].push_back(val);
}
}
vector<int> nxt(n + 1, -1);
vector<vector<int>> up(LOG, vector<int>(N, 0));
vector<ll> value(n + 1, 0);
nxt[1] = 0;
value[1] = a[0];
for (int i = 2; i <= n; ++i) {
nxt[i] = nxt[i - 1];
while (~nxt[i] && s[nxt[i]] != s[i - 1]) {
nxt[i] = nxt[nxt[i]];
}
if (~nxt[i]) {
++nxt[i];
} else {
nxt[i] = (s[i - 1] == s[0] ? 1 : 0);
}
value[i] = a[i - 1] + value[nxt[i]];
up[0][i] = nxt[i];
for (int j = 1; j < LOG; ++j) {
up[j][i] = up[j - 1][up[j - 1][i]];
}
}
ll cur = 0;
for (ll r = 0; r < n; ++r) {
for (auto len: ends[r]) {
upd(r - len, a[len - 1]);
}
while (~cur && s[cur] != t[r]) {
cur = nxt[cur];
}
++cur;
for (auto [l, id]: Q[r]) {
res[id] += sum(l, r);
ll now = cur;
ll len = r - l + 1;
if (now > len) {
for (ll i = LOG - 1; ~i; --i) {
if (up[i][now] > len) {
now = up[i][now];
}
}
now = up[0][now];
}
res[id] += value[now];
}
}
for (auto x: res) {
cout << x << " ";
}
return 0;
}
详细
Test #1:
score: 0
Wrong Answer
time: 2ms
memory: 19728kb
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:
wrong answer 1st lines differ - expected: '1', found: '1 3 3 16 38 '