QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#691290 | #7748. Karshilov's Matching Problem II | XiaoMo247 | WA | 437ms | 49200kb | C++20 | 2.8kb | 2024-10-31 10:45:38 | 2024-10-31 10:45:40 |
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 && s[i] != s[j]) j = ne[j - 1];
if (s[i] == s[j]) ++ j;
ne[i] = j;
s_ne[i + 1] += s_ne[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;
for(int i = 1; i <= n; i ++){
cin >> w[i];
s_w[i] = s_w[i - 1] + w[i];
s_ne[i] = w[i];
}
vector<ll> z = z_function(" " + a + "$" + b);
vector<ll> ne = get_next(a);
for(int i = 1; i <= n; i ++){
s_ne[i] = s_ne[i - 1] + s_ne[i];
s_z[i] = s_z[i - 1] + s_w[z[i]];
}
vector<ll> z_p;
for(int i = 0; i < z.size(); i ++){
z_p.push_back({z[i] + i - 1});
}
xm.init(z_p);
while(q --){
ll l, r;
cin >> l >> r;
if(xm.query(l, r) > r){
int idx = find_set(l, r, r - l + 1);
if(a.find("111") != -1) cout << s_z[idx - 1] + s_z[l - 1] << "\n";
else cout << s_z[idx - 1] - s_z[l - 1] + s_ne[r - idx + 1] << "\n";
}
else{
if(a.find("111") != -1) cout << 666 << "\n";
else cout << s_z[r] - s_z[l - 1] << "\n";
}
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 7752kb
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: 0ms
memory: 7944kb
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: 437ms
memory: 49200kb
input:
150000 150000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
111395886875277 222253634795992 459156234165093 445091633393500 151335834751146 91069553484775 350530651240507 62491316925229 70637195294614 270577777788367 197519316456490 298023654303836 239719148018821 31179000318236 407122874913156 273638064833384 35053722319843 109761924970297 50928533569222 90...
result:
wrong answer 1st lines differ - expected: '108147037823514', found: '111395886875277'