結果

問題 No.2361 Many String Compare Queries
ユーザー 👑 kmjpkmjp
提出日時 2023-06-24 00:05:17
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,061 ms / 2,500 ms
コード長 3,159 bytes
コンパイル時間 3,369 ms
コンパイル使用メモリ 218,844 KB
実行使用メモリ 19,836 KB
最終ジャッジ日時 2023-09-13 19:15:38
合計ジャッジ時間 11,651 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
11,196 KB
testcase_01 AC 5 ms
11,300 KB
testcase_02 AC 4 ms
11,216 KB
testcase_03 AC 5 ms
11,388 KB
testcase_04 AC 5 ms
11,104 KB
testcase_05 AC 5 ms
11,220 KB
testcase_06 AC 4 ms
11,100 KB
testcase_07 AC 6 ms
11,284 KB
testcase_08 AC 1,061 ms
18,004 KB
testcase_09 AC 1,024 ms
17,976 KB
testcase_10 AC 1,018 ms
18,076 KB
testcase_11 AC 847 ms
18,108 KB
testcase_12 AC 989 ms
17,968 KB
testcase_13 AC 951 ms
18,076 KB
testcase_14 AC 554 ms
19,800 KB
testcase_15 AC 773 ms
19,836 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------

int N,Q;
string S;

template<typename ST=string>
struct SuffixArray {
	int N; vector<int> rank,lcp,sa,rsa; ST S;

	SuffixArray(ST S) : S(S){
		int i,h=0; vector<int> tmp;
		N=S.size(); rank.resize(N+1); sa.resize(N+1); tmp.resize(N+1);
		FOR(i,N+1) sa[i]=i, rank[i]=i==N?-1:S[i];
		
		for(int k=1; k<=N; k<<=1) {
			auto pred2 = [k,this](int& a,int &b)->bool{ return (((a+k<=N)?rank[a+k]:-1)<((b+k<=N)?rank[b+k]:-1));};
			auto pred = [pred2,k,this](int& a,int &b)->bool{ return (rank[a]!=rank[b])?(rank[a]<rank[b]):pred2(a,b);};
			int x=0;
			if(k!=1) for(i=1;i<N+1;i++) if(rank[sa[i]]!=rank[sa[x]]) sort(sa.begin()+x,sa.begin()+i,pred2), x=i;
			sort(sa.begin()+x,sa.end(),pred);
			FOR(i,N+1) tmp[sa[i]]=(i==0)?0:tmp[sa[i-1]]+pred(sa[i-1],sa[i]);
			swap(rank,tmp);
		}
		lcp.resize(N+1); rsa.resize(N+1);
		FOR(i,N+1) rsa[sa[i]]=i;
		FOR(i,N) {
			int j=sa[rsa[i]-1];
			for(h=max(h-1,0);i+h<N && j+h<N; h++) if(S[j+h]!=S[i+h]) break;
			lcp[rsa[i]-1]=h;
		}
	}
};

template<class V,int NV> class SegTree_1 {
public:
	vector<V> val;
	static V const def=1<<20;
	V comp(V l,V r){ return min(l,r);};
	
	SegTree_1(){val=vector<V>(NV*2,0);};
	V getval(int x,int y,int l=0,int r=NV,int k=1) { // x<=i<y
		if(r<=x || y<=l) return def;
		if(x<=l && r<=y) return val[k];
		return comp(getval(x,y,l,(l+r)/2,k*2),getval(x,y,(l+r)/2,r,k*2+1));
	}
	void update(int entry, V v) {
		entry += NV;
		val[entry]=v;
		while(entry>1) entry>>=1, val[entry]=comp(val[entry*2],val[entry*2+1]);
	}
};
SegTree_1<int,1<<20> st;

ll preS[202020],sufS[202020];


void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>Q>>S;
	SuffixArray sa(S);
	
	FOR(i,N) {
		preS[i]=(N-sa.sa[i]);
		if(i) preS[i]+=preS[i-1];
		st.update(i,sa.lcp[i]);
	}
	
	vector<pair<int,int>> V={};
	ll sum=0;
	for(i=N;i>=0;i--) {
		x=N-sa.sa[i];
		y=sa.lcp[i];
		k=0;
		while(V.size()&&V.back().first>=y) {
			sum-=1LL*(V.back().first-y)*V.back().second;
			k+=V.back().second;
			V.pop_back();
		}
		if(k) V.push_back({y,k});
		sum+=x;
		V.push_back({x,1});
		sufS[i]=sum;
	}
	
	while(Q--) {
		int L,R;
		cin>>L>>R;
		int P=sa.rsa[L-1];
		L=R-L+1;
		int X=P,Y=P;
		for(i=20;i>=0;i--) {
			if(X-(1<<i)>=0&&st.getval(X-(1<<i),P)>=L) X-=1<<i;
			if(Y+(1<<i)<=N&&st.getval(P,Y+(1<<i))>=L) Y+=1<<i;
		}
		ll ret=1LL*(Y-X+1)*(L-1);
		if(X) ret+=preS[X-1];
		ret+=sufS[Y]-(N-sa.sa[Y]);
		cout<<ret<<endl;
	}
	
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0