QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#461019 | #7748. Karshilov's Matching Problem II | forest114514 | TL | 568ms | 22324kb | C++14 | 3.5kb | 2024-07-02 15:10:47 | 2024-07-02 15:10:47 |
Judging History
answer
//蒟蒻一枚 rp++
//即得易见平凡,仿照上例显然。留作习题答案略,读者自证不难
//反之亦然同理,推论自然成立,略去过程Q.E.D.,由上可知证毕
#include<bits/stdc++.h>
//#pragma GCC optimize("Ofast")
#define re register
#define il inline
#define gc() getchar()
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define repp(i,a,b) for(int i=(a);i<(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
#define tep(i,x) for(int i=head[x];~i;i=ne[i])
#define ls(x) x<<1
#define rs(x) x<<1|1
#define eps (1e-9)
#define inf 0x3f3f3f3f
#define INF 1e16
#define pii pair<int,int>
#define mp(i,j) make_pair(i,j)
#define pb push_back
#define fi first
#define sc second
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef double db;
namespace IO{
template<typename T> inline void read(T &x){
bool f=1;x=0;char ch=gc();
while(ch<'0'||ch>'9'){if(ch=='-')f=0;ch=gc();}
while(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+(ch&15),ch=gc();
x=f?x:-x;
}
template<typename T> inline void write(T x){
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(char('0'+x%10));
}
template <typename T,typename ...Args> inline
void read(T &x,Args &...args){read(x);read(args...);}
template<typename T> inline void write(T x,char c){write(x),putchar(c);}
}
using namespace IO;
//bool _ST;
const int N=1.5e5+100;
int n,q,bd[N],z[N],lcp[N];
vector<pii> que[N];
LL sw[N],ans[N],sr[N];
char S[N],T[N];
void Z_function(){
int l=0,r=0;z[1]=n;
rep(i,2,n){
if(r>=i) z[i]=min(z[i-l+1],r-i+1);
while(i+z[i]<=n&&S[i+z[i]]==S[1+z[i]]) ++z[i];
if(i+z[i]-1>r) l=i,r=i+z[i]-1;
}
l=r=0;
rep(i,1,n){
if(r>=i) lcp[i]=min(z[i-l+1],r-i+1);
while(i+lcp[i]<=n&&T[i+lcp[i]]==S[1+lcp[i]]) ++lcp[i];
if(i+lcp[i]-1>r) l=i,i+lcp[i]-1;
}
}
void border(){
bd[0]=bd[1]=0;
int k=0;
sr[1]=sw[1];
rep(i,2,n){
while(k&&S[i]!=S[k+1]) k=bd[k];
bd[i]=(S[i]==S[k+1])?++k:0;
sr[i]=sr[bd[i]]+sw[i];
}
}
#define lowbit(u) (u&(-u))
struct BIT1{
LL c[N];
void update(int u,LL x){for(;u<=n+1;u+=lowbit(u)) c[u]+=x;}
LL query(int u){LL res=0;for(;u;u-=lowbit(u)) res+=c[u];return res;}
}TR1;
struct SGT{
int mn[N*9];
SGT(){memset(mn,0x3f,sizeof mn);}
void update(int x,int l,int r,int u,int v){
mn[x]=min(mn[x],v);
if(l==r) return;
int mid=l+r>>1;
if(mid>=u) update(ls(x),l,mid,u,v);
else update(rs(x),mid+1,r,u,v);
}
int query(int x,int l,int r,int L,int R){
if(L<=l&&r<=R) return mn[x];
int mid=l+r>>1,res=inf;
if(mid>=L) res=query(ls(x),l,mid,L,R);
if(mid<R) res=min(res,query(rs(x),mid+1,r,L,R));
return res;
}
}TR2;
//bool _ED;
signed main(){
//fprintf(stderr,"%.20lf MB\n",(&_ST-&_ED)/1048576.0);
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
read(n,q);
scanf("%s%s",(S+1),(T+1));
rep(i,1,n) read(sw[i]),sw[i]+=sw[i-1];
rep(i,1,q){
int l,r;read(l,r);
que[l].pb({r,i});
}
Z_function();
border();
per(i,n,1){
if(i+lcp[i]<=n) TR1.update(lcp[i]+i,sw[lcp[i]]);
TR2.update(1,1,n*2,lcp[i]+i,i);
for(auto _:que[i]) {
ans[_.sc]=TR1.query(_.fi);
int pos=TR2.query(1,1,n*2,_.fi+1,n*2);
if(pos<=_.fi) ans[_.sc]+=sr[_.fi-pos+1];
}
}
rep(i,1,q) write(ans[i],'\n');
//fprintf(stderr,"%.4lf s\n",1.0*clock()/CLOCKS_PER_SEC);
return 0;
}
//谨记:
//十年OI一场空,不开longlong见祖宗
//数据千万条,清空第一条。多测不清空,爆零两行泪。清空不规范,TLE总相伴。
//快读要加类型名
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 18148kb
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: 17692kb
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: 0
Accepted
time: 61ms
memory: 22252kb
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:
ok 150000 lines
Test #4:
score: 0
Accepted
time: 92ms
memory: 22324kb
input:
150000 150000 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb...
output:
528900815691571 112638556022185 514229554849356 2216206133639915 295388515578381 1658587138269057 652142121207636 166322478628783 490195029871161 1191292846892788 1468501126902703 487990867773908 55994169916421 568966315599642 2522992078581539 2339888502167342 2881203249819745 154700081279584 152537...
result:
ok 150000 lines
Test #5:
score: 0
Accepted
time: 90ms
memory: 22276kb
input:
150000 150000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
386150762496368 2769669390423140 1025785181073675 1707930121656247 332135612349048 113937878332307 1128519694119799 476402941643931 980441978140407 1004994648999517 676169371268202 2607965889355671 273766796671958 711480908011402 71754482763611 400453994282744 975387094872830 810536618300388 2229061...
result:
ok 150000 lines
Test #6:
score: 0
Accepted
time: 568ms
memory: 22136kb
input:
150000 150000 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb...
output:
25254725752128652 23497896664383313 15195391464047263 41143572895791434 7513384536045842 8871310939247699 17423823866879881 14601201534396958 6203483865940624 24953281161800570 24776576029495768 1687640411226 31563282955464371 29947970968962218 1149303801639767 5806503923049299 11201332188941891 116...
result:
ok 150000 lines
Test #7:
score: 0
Accepted
time: 458ms
memory: 22240kb
input:
150000 150000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
4570597927951323 11761063519994765 289982253793476 15684854914181269 19579321543184889 459972639580770 15246459216963247 1833393949769247 22425556248999709 11209560100586843 2883954996867615 14371655418173335 29207399108721 5943079608253242 1664456073054861 27405606916506455 23082758946788297 381175...
result:
ok 150000 lines
Test #8:
score: -100
Time Limit Exceeded
input:
150000 150000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...