QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#691155 | #7748. Karshilov's Matching Problem II | XiaoMo247 | WA | 1ms | 5900kb | C++20 | 2.5kb | 2024-10-31 10:09:34 | 2024-10-31 10:09:35 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
const ll MAXN = 4e5 + 5;
ll n, q, w[MAXN], s_w[MAXN], s_ne[MAXN], s_z[MAXN];
string a, b;
vector<ll> z_function(string s) {
vector<ll> z(s.size());
z[1] = n;
for (int i = 2, l = 0, r = 0; i < s.size(); ++i) {
if (i <= r && z[i - l + 1] < r - i + 1) {
z[i] = z[i - l + 1];
}
else {
z[i] = max(0ll, r - i + 1);
while (i + z[i] <= s.size() && s[z[i] + 1] == s[i + z[i]]) ++ z[i];
}
if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1;
}
vector<ll> ret;
ret.push_back(0);
for(int i = a.size() + 2; i < s.size(); i ++){
ret.push_back(z[i]);
}
return ret;
}
vector<ll> get_next(string s) {
vector<ll> ne(s.size());
vector<ll> ret;
ret.push_back(0);
for (int i = 1, j = 0; i < s.size(); ++i) {
while (j > 0 && s[i] != s[j]) j = ne[j - 1];
if (s[i] == s[j]) ++ j;
ne[i] = j;
}
for(auto it : ne){
ret.push_back(it);
}
return ret;
}
struct ST{
vector<vector<ll>> f;
void init(vector<ll> &a){
int n = a.size();
vector<ll> tmp(22, 0);
f.assign(n, tmp);
for(int i = 1; i < n; i ++){
f[i][0] = a[i];
}
for(int j = 1; j <= 20; j ++){
for(int i = 1; i + (1 << j) - 1 < n; i ++){
f[i][j] = max(f[i][j - 1], f[i + (1 << (j - 1))][j - 1]);
}
}
}
ll query(ll l, ll r){
ll k = log2(r - l + 1);
return max(f[l][k], f[r - (1 << k) + 1][k]);
}
}xm;
int find_set(ll l, ll r, ll k){
if(l == r) return l;
ll mid = l + r >> 1;
if(xm.query(l, mid) >= k) return find_set(l, mid, k);
else return find_set(mid + 1, r, k);
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> q >> a >> b;
vector<ll> z = z_function(" " + a + "$" + b);
vector<ll> ne = get_next(a);
for(int i = 1; i <= n; i ++){
cin >> w[i];
s_w[i] = s_w[i - 1] + w[i];
}
for(int i = 1; i <= n; i ++){
s_ne[i] = s_ne[i - 1] + s_w[ne[i]];
s_z[i] = s_z[i - 1] + s_w[z[i]];
}
xm.init(z);
while(q --){
ll l, r;
cin >> l >> r;
if(xm.query(l, r) > r - l + 1){
int idx = find_set(l, r, r - l + 1);
cout << s_z[idx - 1] - s_z[l - 1] + s_w[r - idx + 1] + s_ne[r - idx + 1] << "\n";
}
else{
cout << s_z[r] - s_z[l - 1] << "\n";
}
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 5900kb
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: -100
Wrong Answer
time: 1ms
memory: 5688kb
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 16 16 174
result:
wrong answer 2nd lines differ - expected: '13', found: '16'