QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#771127 | #7748. Karshilov's Matching Problem II | Eternatis | WA | 52ms | 38976kb | C++17 | 2.5kb | 2024-11-22 09:59:12 | 2024-11-22 09:59:13 |
Judging History
answer
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define N 1000010
#define int long long
#define i128 __int128
#define db long double
#define pii pair<int,int>
#define st first
#define ed second
#define mkp make_pair
#define pb push_back
#define eps 1e-9
#define mod 998244353
#define mod2 1000000007
#define bs 13131
#define bs2 131
#define INF 0x3f3f3f3f3f3f3f3f
#define il inline
#define vi vector<int>
#define ins insert
#define umap unordered_map
#define uset unordered_set
#define R(x) x.begin(),x.end()
#define B(x) x.begin()
#define E(x) x.end()
#define lb lower_bound
#define ub upper_bound
#define prq priority_queue
mt19937_64 rnd(chrono::system_clock::now().time_since_epoch().count());
il int read(){
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=(x<<1)+(x<<3)+(ch^48);
ch=getchar();
}
return x*f;
}
int T=1,n,q,k;
char a[N],b[N],c[N];
int w[N],pre[N];
int Z[N];
il void Init(char *c,int n){
for(int i=1;i<=n;i++)Z[i]=0;
int p=1;
for(int q=2;q<=n;q++){
if(q<p+Z[p])Z[q]=min(p+Z[p]-q,Z[q-p+1]);
while(q+Z[q]<=n&&c[q+Z[q]]==c[Z[q]+1])Z[q]++;
if(p+Z[p]<q+Z[q])p=q;
}
}
int w1[N],w2[N],sum[N];
int f[21][N],lg[N];
il void init(){
int k=n+n+1;
for(int i=1;i<=n;i++)c[i]=a[i];
c[n+1]='$';
for(int i=1;i<=n;i++)c[i+n+1]=b[i];
Init(c,k);
for(int i=1;i<=n;i++)w1[i]=w1[i-1]+pre[Z[i+n+1]],f[0][i]=i+Z[i+n+1]-1;
for(int i=2;i<=n;i++)lg[i]=lg[i>>1]+1;
for(int k=1;k<=lg[n];k++)
for(int i=1;i+(1<<k)-1<=n;i++)
f[k][i]=max(f[k-1][i],f[k-1][i+(1<<(k-1))]);
Init(a,n);
int p=2;
for(int i=1;i<=n;i++){
while(p<=i&&p+Z[p]-1<i)p++;
w2[i]=sum[p-1]+w2[i-p+1]+pre[i];
if(i>1)sum[i]=sum[i-1]+pre[Z[i]];
}
}
il int que(int l,int r){
int k=lg[r-l+1];
return max(f[k][l],f[k][r-(1<<k)+1]);
}
signed main(){
n=read(),q=read();
scanf("%s%s",a+1,b+1);
for(int i=1;i<=n;i++)
w[i]=read(),pre[i]=pre[i-1]+w[i];
init();
// for(int i=1;i<=n;i++)printf("%lld ",w1[i]);
// puts("");
// for(int i=1;i<=n;i++)printf("%lld ",w2[i]);
// puts("");
while(q--){
int l=read(),r=read();
int a=que(l,r);
if(a<r){
printf("%lld\n",w1[r]-w1[l-1]);
continue;
}
int L=l,R=r,pos=l-1;
while(L<=R){
int mid=(L+R)>>1;
if(que(l,mid)<r)pos=max(pos,mid),L=mid+1;
else R=mid-1;
}
printf("%lld\n",w1[pos]-w1[l-1]+w2[r-pos]);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 18328kb
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: 3ms
memory: 20172kb
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: 52ms
memory: 38976kb
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...
result:
wrong answer 108th lines differ - expected: '108791592916196', found: '108375626066110'