結果

問題 No.2361 Many String Compare Queries
ユーザー 沙耶花沙耶花
提出日時 2023-06-23 23:32:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,185 bytes
コンパイル時間 4,952 ms
コンパイル使用メモリ 268,144 KB
実行使用メモリ 33,040 KB
最終ジャッジ日時 2023-09-13 18:41:40
合計ジャッジ時間 11,688 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 WA -
testcase_05 AC 1 ms
4,376 KB
testcase_06 WA -
testcase_07 AC 2 ms
4,376 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 685 ms
32,980 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 636 ms
31,424 KB
testcase_15 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
using namespace atcoder;
using mint = modint998244353;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf32 1000000001
#define Inf64 4000000000000000001

int op(int a,int b){
	return min(a,b);
}

int e(){
	return Inf32;
}
int X;
bool f(int x){
	return x>=X;
}
using P = pair<long long,long long>;

P Op(P a,P b){
	return make_pair(a.first+b.first,a.second+b.second);
}

P E(){
	return make_pair(0LL,0LL);
}

P mapping(long long a,P b){
	if(a==-1)return b;
	b.first = a * b.second;
	return b;
	
}

long long composition(long long a,long long b){
	if(a==-1)return b;
	return a;
}

long long id(){
	return -1;
}

int main(){
	
	int N,Q;
	cin>>N>>Q;
	
	string S;
	cin>>S;
	
	auto sa = suffix_array(S);
	auto la = lcp_array(S,sa);
	vector<int> pos(N);
	rep(i,sa.size())pos[sa[i]] = i;
	vector<long long> sum(N);
	vector<long long> sum2(N-1);
	rep(i,sa.size()){
		sum[i] = N - sa[i];
		if(i!=0)sum[i] += sum[i-1];
	}
	rep(i,la.size()){
		sum2[i] = la[i];
		if(i!=0)sum2[i] += sum2[i-1];
	}
	vector<long long> ans(Q);
	segtree<int,op,e> seg(la);
	vector<vector<int>> qs(N);
	vector<int> ll(Q),rr(Q);
	rep(_,Q){
		int l,r;
		cin>>l>>r;
		l--;
		
		int p = pos[l];
		qs[p].push_back(_);
		X = r-l;
		
		int lll = seg.min_left<f>(p);
		
		if(lll!=0)ans[_] += sum[lll-1];
		ll[_] = l;
		rr[_] = r;
	}
	
	lazy_segtree<P,Op,E,long long,mapping,composition,id> Seg(N);
	//rep(i,N)Seg.set(1,make_pair(0LL,1LL));
	
	for(int i=N-1;i>=0;i--){
		if(i!=N-1){
			int m = la[i];
			//cout<<m<<endl;
			int ok = i,ng = N;
			while(ng-ok>1){
				int mid = (ok+ng)/2;
				if(Seg.get(mid).first>m)ok = mid;
				else ng = mid;
			}
			Seg.apply(i+1,ok+1,m);	
		
		}
		Seg.set(i,make_pair((long long)N-sa[i],1LL));
		
		rep(j,qs[i].size()){
			int ii = qs[i][j];
			//cout<<ii<<','<<ans[ii]<<endl;
			int m = rr[ii] - ll[ii] - 1;
			int ok = i,ng = N;
			while(ng-ok>1){
				int mid = (ok+ng)/2;
				if(Seg.get(mid).first>=m)ok = mid;
				else ng = mid;
			}
			//cout<<ok<<endl;
			ans[ii] += (long long)m * (ok-i+1);
			ans[ii] += Seg.prod(ok+1,N).first;
		}
	}
	
	rep(i,Q){
		cout<<ans[i]<<endl;
	}
	
	return 0;
}
0