QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#270061#7748. Karshilov's Matching Problem IItime_interspace#WA 130ms31660kbC++203.7kb2023-11-30 14:43:152023-11-30 14:43:15

Judging History

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

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

answer

#include<bits/stdc++.h>
using namespace std;
#define MAXN 150005
#define ll long long
const ll mod1 = 1e9 + 7, mod2 = 1e9 + 9;

int n, m, kmp[MAXN][20], lg[MAXN], dep[MAXN], mch[MAXN], len[MAXN];
ll val[MAXN], sum[MAXN], sum1[MAXN];
char s[MAXN], t[MAXN];
int l = 1, r = 0;

struct Node{
	ll h1, h2;
	Node() {h1 = h2 = 0;}
	Node(ll a, ll b) {h1 = (a%mod1+mod1)%mod1, h2 = (b%mod2+mod2)%mod2;}
} pw[MAXN], hs[MAXN], ht[MAXN];
Node operator+(const Node& a, const Node& b){
	return Node(a.h1+b.h1, a.h2+b.h2);
}
Node operator-(const Node& a, const Node& b){
	return Node(a.h1-b.h1, a.h2-b.h2);
}
Node operator*(const Node& a, const Node& b){
	return Node(a.h1*b.h1, a.h2*b.h2);
}
bool operator==(const Node& a, const Node& b){
	return a.h1 == b.h1 && a.h2 == b.h2;
}

Node sub(int l, int r) {return ht[r] - ht[l-1] * pw[r-l+1];}


int lcp(int x){
	int l = 1, r = n-x+1, mid, ans = 0;
	while(l <= r){
		mid = (l+r)>>1;
		if(sub(x, x+mid-1) == hs[mid]) ans = mid, l = mid+1;
		else r = mid-1;
	}
	return ans;
}

void init(){
	int j = 0;
	for(int i=1;i<=n;i++) lg[i] = lg[i-1] + ((1<<lg[i-1]) == i);
	for(int i=2;i<=n;i++){
		while(j && s[j+1] != s[i]) j = kmp[j][0];
		if(s[j+1] == s[i]) j++;
		kmp[i][0] = j;
		if(j && !dep[j]){
			dep[j] = dep[kmp[j][0]] + 1;
			for(int k=1;k<lg[dep[j]];k++) kmp[j][k] = kmp[kmp[j][k-1]][k-1];
		}
	}
	for(int i=1;i<=n;i++) sum[i] = val[i] + sum[kmp[i][0]];
	for(int i=1;i<=n;i++) len[i] = lcp(i);
	for(int i=1;i<=n;i++) sum1[i] = sum1[i-1] + val[i];
	j = 0;
	for(int i=1;i<=n;i++){
		while(j && s[j+1] != t[i]) j = kmp[j][0];
		if(s[j+1] == t[i]) j++;
		mch[i] = j;
	}
	// for(int i=1;i<=n;i++) printf("kmp[%d] = %d\n", i, kmp[i][0]);
	// for(int i=1;i<=n;i++) printf("mch[%d] = %d, len = %d\n", i, mch[i], len[i]);
}

int siz, bcnt, belong[MAXN];
struct Query{
	int l, r, id;
} Q[MAXN];
bool cmp(const Query& a, const Query& b){
	return (belong[a.l] ^ belong[b.l]) ? belong[a.l] < belong[b.l] : ((belong[a.l] & 1) ? a.r < b.r : a.r > b.r);
}
void initMo(){
	siz = 1000;
	bcnt = ceil(double(n) / siz);
	for(int i=1;i<=bcnt;i++) for(int j=(i-1)*siz+1;j<=n&&j<=i*siz;j++) belong[j] = i;
}

ll ans[MAXN], now = 0;

void addL(){
	--l;
	int tmp = min(len[l], r-l+1);
	now += sum1[tmp];
}
void addR(){
	++r;
	int len = mch[r];
	if(len <= r-l+1) {now += sum[len]; return;}
	for(int i=lg[dep[len]]-1;i>=0;i--){
		if(kmp[len][i] > r-l+1) len = kmp[len][i];
	}
	len = kmp[len][0];
	now += sum[len];
}
void delL(){
	int tmp = min(len[l], r-l+1);
	now -= sum1[tmp];
	l++;
}
void delR(){
	int len = mch[r];
	if(len <= r-l+1) {now -= sum[len]; r--; return;}
	for(int i=lg[dep[len]]-1;i>=0;i--){
		if(kmp[len][i] > r-l+1) len = kmp[len][i];
	}
	len = kmp[len][0];
	now -= sum[len];
	r--;
}

int main(){
	cin>>n>>m; 
	pw[0] = {1, 1}, pw[1] = Node(1e4+7, 1e4+9);
	for(int i=2;i<=n;i++) pw[i] = pw[i-1] * pw[1];
	scanf("%s", s+1), scanf("%s", t+1);
	for(int i=1;i<=n;i++) scanf("%lld", &val[i]);
	for(int i=1;i<=n;i++) hs[i] = hs[i-1] * pw[1] + Node(s[i]-'a'+1, s[i]-'a'+1);
	for(int i=1;i<=n;i++) ht[i] = ht[i-1] * pw[1] + Node(t[i]-'a'+1, t[i]-'a'+1);
	init();
	initMo();
	// puts("-----------");
	for(int i=1;i<=m;i++) scanf("%d%d", &Q[i].l, &Q[i].r), Q[i].id = i;
	sort(Q+1, Q+1+m, cmp);
	for(int i=1;i<=m;i++){
		int ql = Q[i].l, qr = Q[i].r;
		// printf("i = %d, ql = %d, qr = %d\n", i, ql, qr);
		while(l > ql) addL();
		// printf("l = %d, r = %d\n", l, r);/
		while(r < qr) addR();
		// printf("l = %d, r = %d\n", l, r);
		while(l < ql) delL();
		// printf("l = %d, r = %d\n", l, r);
		while(r > qr) delR();
		// printf("l = %d, r = %d\n", l, r);
		ans[Q[i].id] = now;
		// printf("l = %d, r = %d\n", l, r);
	}
	for(int i=1;i<=m;i++) printf("%lld\n", ans[i]);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 10652kb

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

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: 128ms
memory: 31488kb

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: -100
Wrong Answer
time: 130ms
memory: 31660kb

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:

wrong answer 36th lines differ - expected: '276076979115239', found: '276091632567638'