Submission #634886

# Submission time Handle Problem Language Result Execution time Memory
634886 2022-08-25T08:10:09 Z S2speed Dabbeh (INOI20_dabbeh) C++17
100 / 100
1877 ms 105400 KB
#include<bits/stdc++.h>

using namespace std;

#pragma GCC optimize("Ofast")

#define sze(x) (ll)(x.size())
typedef long long ll;
typedef pair<ll , ll> pll;
typedef pair<int , int> pii;

const ll maxn = (1 << 20) + 17 , md = 2000000357 , P = 131 , inf = 2e16;

pii val[maxn];

struct segtree {
 
	int sz = 1;
 
	void init(int n){
		while(sz < n) sz <<= 1;
		fill(val , val + (sz << 1) , make_pair(-1 , -1));
		return;
	}
 
	void build(vector<pii> &a , int x , int lx , int rx){
		if(rx - lx == 1){
			if(lx < sze(a)){
				val[x] = a[lx];
			}
			return;
		}
		int m = (rx + lx) >> 1 , ln = (x << 1) + 1 , rn = ln + 1;
		build(a , ln , lx , m); build(a , rn , m , rx);
		val[x] = max(val[ln] , val[rn]);
		return;
	}
 
	void build(vector<pii> &a){
		build(a , 0 , 0 , sz);
		return;
	}
 
	pii cal(int l , int r , int x , int lx , int rx){
		if(rx <= l || lx >= r) return {-1 , -1};
		if(rx <= r && lx >= l) return val[x];
		int m = (rx + lx) >> 1 , ln = (x << 1) + 1 , rn = ln + 1;
		pii a = cal(l , r , ln , lx , m) , b = cal(l , r , rn , m , rx);
		return max(a , b);
	}
 
	pii cal(int l , int r){
		return cal(l , r , 0 , 0 , sz);
	}
 
};

segtree st;
int k , q , n , m;
ll tv[maxn] , hs[maxn];
string t[maxn] , s;
int r[maxn] , rnk[maxn] , sf[maxn] , pn[maxn] , cnt[maxn] , x[maxn] , y[maxn] , l[maxn];
int mx[maxn] , f[maxn] , jad[maxn][20];
vector<int> imp;
vector<pii> a;

ll get_hs(int l , int r){
	if(r > n) return -inf - l;
	ll res = hs[r - 1];
	if(l){
		res -= hs[l - 1] * tv[r - l];
		res %= md; res += (res < 0) * md;
	}
	return res;
}

int lcp(int i , int j){
	int l = 0 , r = n;
	while(l < r - 1){
		int m = (r + l) >> 1;
		if(get_hs(i , i + m) == get_hs(j , j + m)){
			l = m;
		} else {
			r = m;
		}
	}
	return l;
}

int main(){
	ios_base::sync_with_stdio(false); cout.tie(0); cin.tie(0);
 
	tv[0] = 1;
	for(int i = 1 ; i < maxn ; i++){
		tv[i] = tv[i - 1] * P % md;
	}
	cin>>k>>q;
	for(int i = 0 ; i < k ; i++){
		cin>>t[i];
	}
	cin>>s; m = sze(s);
	s += '$';
	for(int i = 0 ; i < k ; i++){
		s += t[i]; s += '$';
	}
	n = sze(s);
	hs[0] = s[0];
	for(int i = 1 ; i < n ; i++){
		hs[i] = (hs[i - 1] * P + s[i]) % md;
	}
	for(int i = 0 ; i < n ; i++){
		cnt[s[i]]++;
	}
	int cur = 0 , o = 0;
	for(int i = 0 ; i < 131 ; i++){
		x[i] = cur;
		cur += cnt[i];
		if(cnt[i] > 0){
			y[i] = o++;
		}
	}
	for(int i = 0 ; i < n ; i++){
		sf[x[s[i]]++] = i;
		r[i] = y[s[i]];
	}
	for(int z = 1 ; z < n ; z <<= 1){
		for(int i = 0 ; i < n ; i++){
			pn[i] = (sf[i] - z < 0 ? sf[i] - z + n : sf[i] - z);
		}
		memset(cnt , 0 , 4 * o);
		for(int i = 0 ; i < n ; i++){
			cnt[r[sf[i]]]++;
		}
		for(int i = 1 ; i < o ; i++){
			cnt[i] += cnt[i - 1];
		}
		for(int i = n - 1 ; ~i ; i--){
			sf[--cnt[r[pn[i]]]] = pn[i];
		}
		rnk[sf[0]] = 0;
		o = 0;
		for(int i = 1 ; i < n ; i++){
			pii a = {r[sf[i]] , (sf[i] + z >= n ? r[sf[i] + z - n] : r[sf[i] + z])};
			pii b = {r[sf[i - 1]] , (sf[i - 1] + z >= n ? r[sf[i - 1] + z - n] : r[sf[i - 1] + z])};
			o += (a != b);
			rnk[sf[i]] = o;
		}
		o++;
		memcpy(r , rnk , 4 * n);
	}
//	for(int i = 0 ; i < n ; i++){
//		cout<<sf[i]<<' ';
//	}
//	cout<<'\n';
	for(int i = 0 ; i < n ; i++){
		if(sf[i] < m){
			imp.push_back(i);
		} else if(s[sf[i] - 1] == '$'){
			imp.push_back(i);
		}
	}
//	for(int i = 0 ; i < n ; i++){
//		cout<<sf[i]<<' ';
//	}
//	cout<<'\n';
	int sz = sze(imp);
	for(int i = 0 ; i < sz - 1 ; i++){
		l[i] = lcp(sf[imp[i]] , sf[imp[i + 1]]);
//		cout<<l[i]<<' ';
	}
//	cout<<'\n';
	int t = -1;
	for(int j = 0 ; j < sz ; j++){
		int i = imp[j];
		if(sf[i] < m){
			mx[sf[i]] = t;
		} else {
			t = n - sf[i];
		}
		t = min(t , l[j]);
	}
	t = -1;
	for(int j = sz - 1 ; ~j ; j--){
		int i = imp[j];
		t = min(t , l[j]);
		if(sf[i] < m){
			mx[sf[i]] = max(mx[sf[i]] , t);
		} else {
			t = n - sf[i];
		}
	}
	for(int i = 0 ; i < m ; i++){
		f[i] = min(m , i + mx[i]);
//		cout<<f[i]<<' ';
		a.push_back({f[i] , i});
	}
//	cout<<'\n';
	st.init(m);
	st.build(a);
	for(int i = m - 1 ; ~i ; i--){
		pii p = st.cal(i + 1 , f[i] + 1);
		if(p.first <= f[i]){
			for(int j = 0 ; j < 20 ; j++){
				jad[i][j] = i;
			}
			continue;
		}
		jad[i][0] = p.second;
		for(int j = 1 ; j < 20 ; j++){
			jad[i][j] = jad[jad[i][j - 1]][j - 1];
		}
	}
	for(int i = 0 ; i < m ; i++){
//		cout<<jad[i][0]<<' ';
	}
//	cout<<'\n';
	for(int e = 0 ; e < q ; e++){
		int l , r;
		cin>>l>>r;
		if(f[jad[l][19]] < r){
			cout<<"-1\n";
			continue;
		}
		if(f[l] >= r){
			cout<<"1\n";
			continue;
		}
		int v = l , res = 2;
		for(int j = 19 ; ~j ; j--){
			int u = jad[v][j];
			if(f[u] >= r) continue;
			v = u; res += (1 << j);
		}
		cout<<res<<'\n';
	}
	return 0;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:112:11: warning: array subscript has type 'char' [-Wchar-subscripts]
  112 |   cnt[s[i]]++;
      |           ^
Main.cpp:123:12: warning: array subscript has type 'char' [-Wchar-subscripts]
  123 |   sf[x[s[i]]++] = i;
      |            ^
Main.cpp:124:16: warning: array subscript has type 'char' [-Wchar-subscripts]
  124 |   r[i] = y[s[i]];
      |                ^
# Verdict Execution time Memory Grader output
1 Correct 24 ms 41428 KB Output is correct
2 Correct 526 ms 53836 KB Output is correct
3 Correct 354 ms 50716 KB Output is correct
4 Correct 538 ms 54220 KB Output is correct
5 Correct 471 ms 52536 KB Output is correct
6 Correct 678 ms 56124 KB Output is correct
7 Correct 661 ms 56588 KB Output is correct
8 Correct 698 ms 57012 KB Output is correct
9 Correct 541 ms 54944 KB Output is correct
10 Correct 165 ms 45712 KB Output is correct
11 Correct 198 ms 54436 KB Output is correct
12 Correct 119 ms 47932 KB Output is correct
13 Correct 160 ms 51672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 690 ms 94184 KB Output is correct
2 Correct 1210 ms 98096 KB Output is correct
3 Correct 797 ms 94712 KB Output is correct
4 Correct 586 ms 91860 KB Output is correct
5 Correct 627 ms 92160 KB Output is correct
6 Correct 1091 ms 97948 KB Output is correct
7 Correct 1339 ms 100292 KB Output is correct
8 Correct 1088 ms 96864 KB Output is correct
9 Correct 1198 ms 98836 KB Output is correct
10 Correct 1330 ms 100644 KB Output is correct
11 Correct 834 ms 94836 KB Output is correct
12 Correct 752 ms 93992 KB Output is correct
13 Correct 1465 ms 101284 KB Output is correct
14 Correct 1141 ms 99812 KB Output is correct
15 Correct 504 ms 91380 KB Output is correct
16 Correct 372 ms 100752 KB Output is correct
17 Correct 272 ms 92500 KB Output is correct
18 Correct 277 ms 92320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 41428 KB Output is correct
2 Correct 526 ms 53836 KB Output is correct
3 Correct 354 ms 50716 KB Output is correct
4 Correct 538 ms 54220 KB Output is correct
5 Correct 471 ms 52536 KB Output is correct
6 Correct 678 ms 56124 KB Output is correct
7 Correct 661 ms 56588 KB Output is correct
8 Correct 698 ms 57012 KB Output is correct
9 Correct 541 ms 54944 KB Output is correct
10 Correct 165 ms 45712 KB Output is correct
11 Correct 198 ms 54436 KB Output is correct
12 Correct 119 ms 47932 KB Output is correct
13 Correct 160 ms 51672 KB Output is correct
14 Correct 690 ms 94184 KB Output is correct
15 Correct 1210 ms 98096 KB Output is correct
16 Correct 797 ms 94712 KB Output is correct
17 Correct 586 ms 91860 KB Output is correct
18 Correct 627 ms 92160 KB Output is correct
19 Correct 1091 ms 97948 KB Output is correct
20 Correct 1339 ms 100292 KB Output is correct
21 Correct 1088 ms 96864 KB Output is correct
22 Correct 1198 ms 98836 KB Output is correct
23 Correct 1330 ms 100644 KB Output is correct
24 Correct 834 ms 94836 KB Output is correct
25 Correct 752 ms 93992 KB Output is correct
26 Correct 1465 ms 101284 KB Output is correct
27 Correct 1141 ms 99812 KB Output is correct
28 Correct 504 ms 91380 KB Output is correct
29 Correct 372 ms 100752 KB Output is correct
30 Correct 272 ms 92500 KB Output is correct
31 Correct 277 ms 92320 KB Output is correct
32 Correct 528 ms 74656 KB Output is correct
33 Correct 918 ms 81144 KB Output is correct
34 Correct 1269 ms 84276 KB Output is correct
35 Correct 994 ms 82816 KB Output is correct
36 Correct 1322 ms 86776 KB Output is correct
37 Correct 474 ms 74976 KB Output is correct
38 Correct 1733 ms 102744 KB Output is correct
39 Correct 1012 ms 96412 KB Output is correct
40 Correct 1877 ms 105400 KB Output is correct
41 Correct 1810 ms 105264 KB Output is correct
42 Correct 1419 ms 98820 KB Output is correct
43 Correct 676 ms 66852 KB Output is correct
44 Correct 634 ms 66488 KB Output is correct
45 Correct 580 ms 65204 KB Output is correct
46 Correct 1000 ms 72284 KB Output is correct
47 Correct 888 ms 92992 KB Output is correct
48 Correct 1176 ms 97312 KB Output is correct
49 Correct 914 ms 93880 KB Output is correct
50 Correct 1695 ms 103100 KB Output is correct
51 Correct 358 ms 90600 KB Output is correct
52 Correct 371 ms 93548 KB Output is correct
53 Correct 369 ms 90928 KB Output is correct