QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#597120#7748. Karshilov's Matching Problem IIforget-star#WA 114ms69896kbC++142.6kb2024-09-28 17:05:562024-09-28 17:05:56

Judging History

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

  • [2024-09-28 17:05:56]
  • 评测
  • 测评结果:WA
  • 用时:114ms
  • 内存:69896kb
  • [2024-09-28 17:05:56]
  • 提交

answer

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<map>
#include<vector>
#include<queue>
#include<set>
#include<bitset>
#include<deque>
#include<stack>
#include<unordered_map>
#include<ctime>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int N=5e5+10;
int n,m,w[N],z[N],p[N],LL[N],nxt[N<<1],to[N];
int f[N][20];
ll sumw[N],sumbor[N];
char S[N],T[N];
vector<int>q[N];
vector<int>s[N];
ll tr[N],ans[N];
inline void modify(int x,ll v)
{
	while(x<=n)
	{
		tr[x]+=v;
		x+=(x&(-x));
	}
	return;
}
inline ll query(int x)
{
	ll res=0;
	while(x>0)
	{
		res+=tr[x];
		x-=(x&(-x));
	}
	return res;
}
int main()
{
	scanf("%d%d",&n,&m);
	scanf("%s\n%s",S+1,T+1);
	for(int i=1;i<=n;i++) scanf("%d",&w[i]);
	for(int i=1;i<=n;i++) sumw[i]=sumw[i-1]+w[i];
	for(int i=1;i<=m;i++)
	{
		int r;scanf("%d%d",&LL[i],&r);
		q[r].push_back(i);
	}
	z[1]=n;
	for(int i=2,l=0,r=0;i<=n;i++)
	{
		if(i<=r) z[i]=min(z[i-l+1],r-i+1);
		while((i+z[i])<=n&&S[i+z[i]]==S[z[i]+1]) z[i]++;
		if((i+z[i]-1)>r) l=i,r=i+z[i]-1;
	}
	for(int i=1,l=0,r=0;i<=n;i++)
	{
		if(i<=r) p[i]=min(z[i-l+1],r-i+1);
		while((i+p[i])<=n&&T[i+p[i]]==S[p[i]+1]) p[i]++;
		if((i+p[i]-1)>r) l=i,r=i+p[i]-1;
	}
//	for(int i=1;i<=n;i++) printf("p[%d]=%d ",i,p[i]);puts("");
	for(int i=1;i<=n;i++) s[i+p[i]].push_back(i);
	sumbor[1]=sumw[1];
	for(int i=2,j=0;i<=n;i++)
	{
		while(j>0&&S[i]!=S[j+1]) j=nxt[j];
		if(S[i]==S[j+1]) j++;
		nxt[i]=j;
		sumbor[i]=sumbor[nxt[i]]+sumw[i];
	}
	for(int i=1,j=nxt[n];i<=n;i++)
	{
		while(j>0&&T[i]!=(((j+1)>n)?T[j+1-n]:S[j+1])) j=nxt[j];
		if(T[i]==(((j+1)>n)?T[j+1-n]:S[j+1])) j++;
		nxt[i+n]=j;
		if(j<=n) to[i]=j;
		else to[i]=to[j];
//		printf("to[%d]=%d ",i,to[i]);
	}
	for(int i=1;i<=n;i++) f[i][0]=nxt[i];
	for(int j=1;j<=18;j++)
		for(int i=1;i<=n;i++)
			f[i][j]=f[f[i][j-1]][j-1];
	for(int i=1;i<=n;i++)
	{
		for(int j=0;j<s[i].size();j++) modify(s[i][j],sumw[p[s[i][j]]]);
		for(int j=0;j<q[i].size();j++)
		{
			int L=LL[q[i][j]],R=i,id=q[i][j];
			ans[id]=query(R)-query(L-1);
			int p=to[i];
			int l=0,r=n,x=p;
			while(l<=r)
			{
				int mid=(l+r)>>1;
				int tmp=p;
				for(int k=0;k<=18;k++)
					if((mid>>k)&1) tmp=f[tmp][k];
				if(tmp<=R-L+1) x=tmp,r=mid-1;
				else l=mid+1;
			}
			ans[id]+=sumbor[x];
		}
	}
	for(int i=1;i<=m;i++) printf("%lld\n",ans[i]);
	return 0;
}
/*
8 5
abbabaab
aababbab
1 2 4 8 16 32 64 128
1 1
2 3
3 5
4 7
1 8

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
*/

详细

Test #1:

score: 100
Accepted
time: 5ms
memory: 42712kb

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: 42812kb

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: 110ms
memory: 68184kb

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: 114ms
memory: 69896kb

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: 114ms
memory: 69104kb

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: 114ms
memory: 68116kb

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: 109ms
memory: 67784kb

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
Wrong Answer
time: 102ms
memory: 66212kb

input:

150000 150000
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

wrong answer 1st lines differ - expected: '5697498028074951', found: '0'