QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#691329 | #7748. Karshilov's Matching Problem II | XiaoMo247 | WA | 167ms | 48748kb | C++20 | 2.6kb | 2024-10-31 10:59:08 | 2024-10-31 10:59:08 |
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], zp[MAXN];
string a, b;
vector<ll> Zalgo(string s) {
int n = s.size();
std::vector<ll> z(n);
for (int i = 1, l = 0, r = 0; i < n; i++) {
if (i < r)
z[i] = std::min(z[i - l], r - i);
while (i + z[i] < n and s[i + z[i]] == s[z[i]])
z[i]++;
if (i + z[i] > r)
l = i, r = i + z[i];
}
vector<ll> ret(1, 0);
for(int i = a.size() + 1; i < z.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{
ll f[MAXN][21];
void init(ll a[], ll n){
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 = Zalgo(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]];
}
for(int i = 1; i < z.size(); i ++){
zp[i] = z[i] + i - 1;
}
xm.init(zp, a.size());
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("aaa") != -1) cout << idx << "\n";
else cout << s_z[idx - 1] - s_z[l - 1] + s_ne[r - idx + 1] << "\n";
}
else{
if(a.find("aaa") != -1) cout << 666 << "\n";
else cout << s_z[r] - s_z[l - 1] << "\n";
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 9988kb
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: 2ms
memory: 9720kb
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: 167ms
memory: 48748kb
input:
150000 150000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
30209 81501 127304 122513 41532 114837 98629 127807 54731 74275 84581 82950 66499 90356 114525 75339 37087 30063 46360 75370 133145 13176 94446 45203 10510 118770 54657 39816 108230 122391 79143 83747 30851 45280 26202 20933 44888 94075 52708 37318 71970 86791 11774 142188 32451 84159 85346 100173 2...
result:
wrong answer 1st lines differ - expected: '108147037823514', found: '30209'