QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#267270#7748. Karshilov's Matching Problem IImanizareWA 103ms106388kbC++142.4kb2023-11-27 03:31:012023-11-27 03:31:02

Judging History

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

  • [2024-08-25 20:42:18]
  • hack成功,自动添加数据
  • (/hack/789)
  • [2023-11-27 03:31:02]
  • 评测
  • 测评结果:WA
  • 用时:103ms
  • 内存:106388kb
  • [2023-11-27 03:31:01]
  • 提交

answer

#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#define pb push_back
#define F first
#define S second 
#define all(a) a.begin(),a.end()
#define pii pair <int,int>
#define PII pair<pii , pii>
#define int long long 
#define ld long double
#define sz(v) (int)v.size()
using namespace std ;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxn =1e6 + 10 , N = 3e5 + 10  , inf = 1e18 , mod = 1000000007 ;
int  n,  a[maxn] ,lcp[maxn] , w[maxn] , sm[maxn] , z[maxn] , z2[maxn] , id[maxn] ,b[maxn] , pr[maxn]  ; 
int ans[maxn] ;
vector <int> G[maxn] , vec[maxn], vec2[maxn];

void dfs(int v ,int p = 0){
	sm[v] += w[v] ;
	for(int u : G[v]){
		sm[u] += sm[v] ;
		dfs(u) ;
	}
}

signed main() {
    ios::sync_with_stdio(0);cin.tie(0);
	int  q ;
	cin >> n >> q; 
	string s ,t ;
	cin >> s >> t ;
	for(int i = 1; i <= n ;i++){
		cin >> w[i]; 
	}
	int T = 0 ;
	G[0].pb(1);
	for(int i = 1 ; i < sz(s) ; i++){
		while(T!=0 && s[T] != s[i]){
			T = lcp[T-1] ;
		}
		if(s[T] == s[i])T++;
		lcp[i] = T ;
		G[lcp[i]].pb(i+1);
	}
	dfs(0) ;
	int l = 0 ,  r= 0  ;
	for(int i = 1 ; i < n ; i++){
		if(i < r){
			z[i] = min(r-i , z[i-l]) ;
		}
		while(i+z[i] < n && s[i+z[i]] == s[z[i]])z[i]++;
		if(i+z[i] > r){
			l = i , r = i+z[i] ; 
		}
	}
	l = 0 , r = 0 ;
	for(int i =0 ; i < n ; i++){
		if(i < r){
			z2[i] = min(r-i , z[i-l]) ;
		}
		while(i+z2[i] < n && t[i+z2[i]] == s[z2[i]])z2[i]++;
		if(i+z2[i] > r){
			l = i , r = i + z2[i] ;
		}
	}
	for(int i =1 ; i<= q; i++){
		cin >> a[i] >> b[i] ;a[i]--;b[i]--;
		vec[a[i]].pb(i);
	}
	set <pii> se ;
	for(int i =0 ; i < n ; i++){
		for(int x : vec[i]){
			se.insert({b[x] , x}) ;
		}
		while(sz(se) && (*se.begin()).F < i+z2[i]){
			id[(*se.begin()).S] = i ; 
			se.erase(se.begin()) ;
		}
	}
	for(int i = 2; i <= n ;i++){
		w[i] += w[i-1] ;
		sm[i] += sm[i-1] ;
	}
	for(int i =0 ; i < n ;i++){
		pr[i] = pr[i-1] + w[z2[i]] ;
	}
	for(int i = 1; i <= q; i++){
		if(id[i]+z2[id[i]]-1 <= b[i] || id[i] > b[i]){
			id[i] = 0;
		}
		if(id[i]!=0){
			ans[i] = sm[b[i]-id[i]+1] + pr[id[i]-1] - (a[i] == 0 ? 0 : pr[a[i]-1]) ;
		}else{
			if(i==3 && n > 100){
				for(int j = a[i] ; j <= b[i] ; j++){
					if(z2[j]+j > b[i]){
						cout <<"wtf\n";
						exit(0);
					}
				}
			}
			ans[i] = pr[b[i]] - (a[i] == 0 ? 0 : pr[a[i]-1]) ;
		}
	}
	for(int i = 1; i <= q; i++){
		cout << ans[i] << "\n" ;
	}
	cout << "\n" ;
}
/*


*/

详细

Test #1:

score: 100
Accepted
time: 11ms
memory: 84952kb

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: 3ms
memory: 84180kb

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: 103ms
memory: 106388kb

input:

150000 150000
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

wtf

result:

wrong answer 1st lines differ - expected: '108147037823514', found: 'wtf'