QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#461090 | #7748. Karshilov's Matching Problem II | forest114514 | TL | 546ms | 11536kb | C++14 | 3.2kb | 2024-07-02 15:57:45 | 2024-07-02 15:57:45 |
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];
LL w[N],sr[N],sl[N];
char S[N],T[N];
void Z_function(){
int l=0,r=0;
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]=w[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]]+w[i];
}
}
struct SGT{
int mx[N*4];
void build(int x,int l,int r){
if(l==r) return mx[x]=l+lcp[l]-1,void();
int mid=l+r>>1;
build(ls(x),l,mid),build(rs(x),mid+1,r);
mx[x]=max(mx[ls(x)],mx[rs(x)]);
}
int work(int x,int l,int r,int L,int R){
if(mx[x]<=R) return 0;
if(l==r) return l;
int mid=l+r>>1,res;
if(L<=mid&&(res=work(ls(x),l,mid,L,R))) return res;
if(mid<R&&(res=work(rs(x),mid+1,r,L,R))) return res;
return 0;
}
}TR;
//bool _ED;
signed main(){
//fprintf(stderr,"%.20lf MB\n",(&_ST-&_ED)/1048576.0);
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
read(n,q);
cin>>(S+1)>>(T+1);
rep(i,1,n) read(w[i]);
Z_function();
border();
rep(i,1,n) w[i]+=w[i-1],sr[i]+=sr[i-1];
rep(i,1,n) sl[i]=sl[i-1]+w[lcp[i]];
// rep(i,1,n) cout<<lcp[i]<<" "<<sl[i]<<" "<<sr[i]<<endl;
TR.build(1,1,n);
rep(i,1,q){
int l,r,pos;
read(l,r);
pos=TR.work(1,1,n,l,r);
if(!pos) write(sl[r]-sl[l-1],'\n');
else write(sl[pos-1]-sl[l-1]+sr[r-pos+1],'\n');
}
//fprintf(stderr,"%.4lf s\n",1.0*clock()/CLOCKS_PER_SEC);
return 0;
}
//谨记:
//十年OI一场空,不开longlong见祖宗
//数据千万条,清空第一条。多测不清空,爆零两行泪。清空不规范,TLE总相伴。
//快读要加类型名
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 7796kb
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: 9656kb
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: 44ms
memory: 11504kb
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: 83ms
memory: 11512kb
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: 82ms
memory: 11504kb
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: 546ms
memory: 11536kb
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: 432ms
memory: 11388kb
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...