QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#260293#7748. Karshilov's Matching Problem IIucup-team1321WA 93ms23668kbC++202.1kb2023-11-22 00:15:522023-11-22 00:15:52

Judging History

你现在查看的是最新测评结果

  • [2024-08-25 20:42:18]
  • hack成功,自动添加数据
  • (/hack/789)
  • [2023-11-22 00:15:52]
  • 评测
  • 测评结果:WA
  • 用时:93ms
  • 内存:23668kb
  • [2023-11-22 00:15:52]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
int z[150010],Link[150010],t=0,p[150010],nex[150010],w[150010];
int n,m,Max[150010][20];
long long g[150010],c[150010],h[150010],sw[150010];
char S[150010],T[150010];
struct dsa
{
	int v,nex;
}e[150010];
void Exkmp()
{
	z[0]=0;
	for (int i=1,l=0,r=0;i<n;i++) {
		z[i]=0;
		if (i<=r) z[i]=min(z[i-l],r-i+1);
		while (i+z[i]<n&&S[i+z[i]]==S[z[i]]) z[i]++;
		if (i+z[i]-1>r) l=i,r=i+z[i]-1;
	}
	for (int i=0,l=0,r=0;i<n;i++) {
		p[i]=0;
		if (i<=r) p[i]=min(z[i-l],r-i+1);
		while (i+p[i]<n&&T[i+p[i]]==S[p[i]]) p[i]++;
		if (i+p[i]-1>r) l=i,r=i+p[i]-1;
	}
	return ;
}
void Insert(int x,int y)
{
	e[++t].nex=Link[x];e[t].v=y;Link[x]=t;
	return ;
}
void KMP()
{
	int now=-1;nex[0]=-1;
	Insert(0,1);
	for (int i=1;i<n;i++) {
		while (now!=-1&&S[now+1]!=S[i]) now=nex[now];
		if (S[now+1]==S[i]) now++;
		nex[i]=now;
		Insert(now+1,i+1);
	}
	return ;
}
void dfs(int now)
{
	if (now) c[now]+=w[now-1];
	for (int i=Link[now];i;i=e[i].nex) {
		c[e[i].v]=c[now];
		dfs(e[i].v);
	}
	return ;
}
int Get_Max(int l,int r)
{
	if (l>r) return 0;
	int len=log2(r-l+1);
	return max(Max[l][len],Max[r-(1<<len)+1][len]);
}
int Find(int l,int r,int lim)
{
	if (l==r) return l;
	int mid=(l+r)>>1;
//	cout<<l<<' '<<r<<' '<<Get_Max(l,mid)<<' '<<lim<<endl;
	if (Get_Max(l,mid)>=lim) return Find(l,mid,lim);
	return Find(mid+1,r,lim);
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cin>>n>>m;
	cin>>S;cin>>T;
	for (int i=0;i<n;i++) cin>>w[i];
	Exkmp();
	for (int i=0;i<n;i++) Max[i][0]=i+p[i]-1;
	for (int i=1;i<=17;i++)
	  for (int j=0;j<n-(1<<i)+1;j++) 
	      Max[j][i]=max(Max[j][i-1],Max[j+(1<<(i-1))][i-1]);
	KMP();dfs(0);
	g[0]=w[0];
	for (int i=1;i<n;i++) g[i]=g[i-1]+c[i+1];
	sw[0]=w[0];
	for (int i=1;i<n;i++) sw[i]=sw[i-1]+w[i];
	h[0]+=sw[p[0]-1];
	for (int i=1;i<n;i++) h[i]=h[i-1]+sw[p[i]-1];
	for (int i=1;i<=m;i++) {
		int l,r;
		cin>>l>>r;
		l--;r--;
		int k=Find(l,r,r);
//		cout<<k<<' '<<g[r-k]<<endl;
		long long ans=g[r-k]+h[k-1];
		if (l) ans-=h[l-1];
		cout<<ans<<'\n';
	}
	return 0;
}
		
		
		

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 9612kb

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: 2ms
memory: 9664kb

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: 93ms
memory: 23668kb

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 193rd lines differ - expected: '355571725239632', found: '355571735698367'