QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#455836#7748. Karshilov's Matching Problem IIAria_MathTL 0ms6008kbC++171.1kb2024-06-26 21:06:102024-06-26 21:06:11

Judging History

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

  • [2024-08-25 20:42:18]
  • hack成功,自动添加数据
  • (/hack/789)
  • [2024-06-26 21:06:11]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:6008kb
  • [2024-06-26 21:06:10]
  • 提交

answer

// They say that life is always easier
// After you let yourself come undone
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1.5e5 + 10;
int n, m, nxt[N], lcp[N], z[N];
ll w[N];
char s[N], t[N];
void Z() {
	int id = 0, mx = 0;
	while(s[z[2] + 1] == s[z[2] + 2]) ++z[2];
	id = 2, mx = 2 + z[2] - 1;
	for(int i = 3; i <= n; ++i) {
		if(i <= mx) z[i] = min(mx - i + 1, z[i - id + 1]);
		while(s[1 + z[i]] == s[i + z[i]]) ++z[i];
		if(i + z[i] - 1 > mx) id = i, mx = i + z[i] - 1;
	}
	id = 0, mx = 0;
	for(int i = 1; i <= n; ++i) {
		if(i <= mx) lcp[i] = min(mx - i + 1, z[i - id + 1]);
		while(s[1 + lcp[i]] == t[i + lcp[i]]) ++lcp[i];
		if(i + lcp[i] - 1 > mx) id = i, mx = i + lcp[i] - 1;
	}
}
int main() {
	//freopen("data.in", "r", stdin);
	//freopen("code.out", "w", stdout);
	scanf("%d%d", &n, &m);
	scanf("%s%s", &s[1], &t[1]);
	for(int i = 1; i <= n; ++i)
		scanf("%lld", &w[i]), w[i] += w[i - 1];
	Z();
	while(m--) {
		int l, r;
		scanf("%d%d", &l, &r);
		ll ans = 0;
		for(int i = l; i <= r; ++i)
			ans += w[min(r - i + 1, lcp[i])];
		printf("%lld\n", ans);
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

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
Time Limit Exceeded

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: