QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#267999 | #7748. Karshilov's Matching Problem II | iliya_aghazadeh | TL | 0ms | 22860kb | C++17 | 2.9kb | 2023-11-27 22:30:34 | 2023-11-27 22:30:35 |
Judging History
answer
//IN THE NAME OF GOD
#include<bits/stdc++.h>
#pragma GCC optimize("O2,unroll-loops")
#define endl '\n'
#define F first
#define S second
#define pii pair<int,int>
#define all(x) x.begin(),x.end()
#define pb push_back
#define fastio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;
typedef long long ll;
typedef long double dll;
#define int long long
const int N = 15e4+7;
vector<pii> q[N];
vector<int> add[N], rem[N];
int w[N], sumw[N], zs[N], zt[N], sumzt[N], pres[N], sumpres[N], lps[N], ans[N];
set<int> hav;
int32_t main(){
fastio;
int n,m; cin >> n >> m;
string s,t; cin >> s >> t;
for(int i=1; i<=n; i++){
cin >> w[i];
sumw[i] = sumw[i-1] + w[i];
}
for(int i=0; i<m; i++){
int l,r; cin >> l >> r;
l--; r--;
q[r].pb({l,i});
}
zs[0] = 0;
int mx = 0;
for(int i=1; i<n; i++){
int ind;
if (mx+zs[mx] < i) ind = i;
else ind = min(i+zs[i-mx],mx+zs[mx]);
while(ind < n && s[ind] == s[ind-i]) ind++;
zs[i] = ind - i;
if (mx+zs[mx] < i+zs[i]) mx = i;
}
zt[0] = 0;
mx = 0;
for(int i=1; i<n; i++){
int ind;
if (mx+zt[mx] < i) ind = i;
else ind = min(i+zs[i-mx],mx+zt[mx]);
while(ind < n && t[ind] == s[ind-i]) ind++;
zt[i] = ind - i;
if (mx+zt[mx] < i+zt[i]) mx = i;
}
while(zt[0] < n && t[zt[0]] == s[zt[0]]) zt[0]++;
for(int i=0; i<n; i++) sumzt[i] = (i == 0 ? 0 : sumzt[i-1]) + sumw[zt[i]];
for(int i=0; i<n; i++){
if (i+zt[i]-1 > i){
add[i].pb(i);
rem[i+zt[i]-1].pb(i);
}
}
pres[0] = 0;
lps[0] = 0;
for(int i=1; i<n; i++){
int k = lps[i-1];
while(k > 0 && s[k] != s[i]) k = lps[k];
if (s[k] == s[i]) k++;
lps[i] = k;
pres[i] = w[lps[i]] + (lps[i] == 0 ? 0 : pres[lps[i]-1]);
}
for(int i=0; i<n; i++) pres[i] += w[i+1];
sumpres[0] = pres[0];
for(int i=1; i<n; i++) sumpres[i] = sumpres[i-1] + pres[i];
cerr << "hello " << endl << flush;
for(int r = 0; r < n; r++){
for(int cur : add[r]) hav.insert(cur);
for(int cur : rem[r]) hav.erase(cur);
for(pii cur : q[r]){
int l = cur.F;
set<int>::iterator itr = hav.lower_bound(l);
if (itr == hav.end()) ans[cur.S] = sumzt[r] - (l == 0 ? 0 : sumzt[l-1]);
else{
int ind = *itr;
ans[cur.S] = (ind == l ? 0 : sumzt[ind-1] - (l == 0 ? 0 : sumzt[l-1]));
ans[cur.S] += sumpres[r-ind];
}
}
}
for(int i=0; i<m; i++) cout << ans[i] << endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 22308kb
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: 22860kb
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
Time Limit Exceeded
input:
150000 150000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...