QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#259412 | #7748. Karshilov's Matching Problem II | ucup-team902 | TL | 0ms | 10016kb | C++20 | 3.0kb | 2023-11-20 21:45:26 | 2023-11-20 21:45:26 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define gc c=getchar()
#define r(x) read(x)
#define ll long long
template<typename T>
inline void read(T &x){
x=0;T k=1;char gc;
while(!isdigit(c)){if(c=='-')k=-1;gc;}
while(isdigit(c)){x=x*10+c-'0';gc;}x*=k;
}
const int N = 1.5e5 + 7;
const int p = 1e9 + 7;
const int base = 137;
inline int sub(int a, int b){
return a - b < 0 ? a - b + p : a - b;
}
int tr[N << 2];
int len[N];
#define ls (rt << 1)
#define rs (ls | 1)
void build(int rt, int l, int r){
if(l == r){
tr[rt] = l + len[l] - 1;
return ;
}
int mid = (l + r) >> 1;
build(ls, l, mid);
build(rs, mid + 1, r);
tr[rt] = max(tr[ls], tr[rs]);
}
int query(int rt, int l, int r, int x, int y, int v){
if(tr[rt] < v) return y + 1;
if(l == r) return l;
int mid = (l + r) >> 1;
if(x <= l && r <= y){
if(tr[ls] >= v) return query(ls, l, mid, x, y, v);
else return query(rs, mid + 1, r, x, y, v);
}
if(y <= mid) return query(ls, l, mid, x, y, v);
if(x > mid) return query(rs, mid + 1, r, x, y, v);
return min(query(ls, l, mid, x, y, v), query(rs, mid + 1, r, x, y, v));
// int t = query(ls, l, mid, x, y, v);
// return t == -1 ? query(rs, mid + 1, r, x, y, v) : t;
}
ll f[N];
ll g[N];
ll sumf[N];
ll sumg[N];
char s[N];
char t[N];
int w[N];
int Next[N];
int hashs[N];
int hasht[N];
int pw[N];
int main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
int n, m; r(n), r(m);
scanf("%s", s + 1);
scanf("%s", t + 1);
for(int i = 1; i <= n; ++i){
r(w[i]);
}
pw[0] = 1;
for(int i = 1; i <= n; ++i){
hashs[i] = ((ll)hashs[i - 1] * base + s[i]) % p;
hasht[i] = ((ll)hasht[i - 1] * base + t[i]) % p;
pw[i] = (ll)pw[i - 1] * base % p;
}
for(int i = 1; i <= n; ++i){
int l = i, r = n, ans = i - 1;
while(l <= r){
int mid = (l + r) >> 1;
if(hashs[mid - i + 1] == sub(hasht[mid], (ll)hasht[i - 1] * pw[mid - i + 1] % p)){
l = mid + 1;
ans = mid;
}
else{
r = mid - 1;
}
}
len[i] = ans - i + 1;
}
build(1, 1, n);
for(int i = 2, j = 0; i <= n; ++i){
while(s[j + 1] != s[i] && j) j = Next[j];
if(s[j + 1] == s[i]) ++j;
Next[i] = j;
}
for(int i = 1; i <= n; ++i){
f[i] = f[i - 1] + w[i];
g[i] = w[i] + g[Next[i]];
}
for(int i = 1; i <= n; ++i){
sumf[i] = sumf[i - 1] + f[len[i]];
sumg[i] = sumg[i - 1] + g[i];
}
for(int i = 1; i <= m; ++i){
int l, r; r(l), r(r);
int pos = query(1, 1, n, l, r, r);
for(int _ = l; _ < pos; ++_){
assert(_ + len[_] - 1 <= r);
}
printf("%lld\n", sumg[r - pos + 1] + sumf[pos - 1] - sumf[l - 1]);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 9928kb
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: 10016kb
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...
output:
108147037823514 221878585246974 455339807727642 440286198422821 148115747906237 88695249853257 351159618462315 58850354020997 65824434822005 270158033672354 197732558443069 298193894693053 239511187032650 28139154231325 408380171835227 268053430937402 32417121185965 104813548228675 44074926058619 78...