提出 #74680719


ソースコード 拡げる

#include<bits/stdc++.h>
#define int long long
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
bool st;
using namespace std;
/*Ctrl+Alt+G F*/
namespace AC{
namespace Fread{const int SIZE=1<<16;char buf[SIZE],*S,*T;inline char getchar(){if(S==T){T=(S=buf)+fread(buf,1,SIZE,stdin);if(S==T)return'\n';}return *S++;}}namespace Fwrite{const int SIZE=1<<16;char buf[SIZE],*S=buf,*T=buf+SIZE;inline void flush(){fwrite(buf,1,S-buf,stdout);S=buf;}inline void putchar(char c){*S++=c;if(S==T)flush();}struct NTR{~NTR(){flush();}}ztr;}
#define getchar Fread::getchar
#define putchar Fwrite::putchar
#define Setprecision 15
#define between ' '
template<typename T>struct is_char{static constexpr bool value=(std::is_same<T,char>::value||std::is_same<T,signed char>::value||std::is_same<T,unsigned char>::value);};template<typename T>struct is_integral_ex{static constexpr bool value=(std::is_integral<T>::value||std::is_same<T,__int128>::value)&&!is_char<T>::value;};template<typename T>struct is_floating_point_ex{static constexpr bool value=std::is_floating_point<T>::value||std::is_same<T,__float128>::value;};namespace Fastio{struct Reader{template<typename T>typename std::enable_if_t<std::is_class<T>::value,Reader&>operator>>(T&x){for(auto &y:x)*this>>y;return *this;}template<typename T>typename std::enable_if_t<is_integral_ex<T>::value,Reader&>operator>>(T&x){char c=getchar();short f=1;while(c<'0'||c>'9'){if(c=='-')f*=-1;c=getchar();}x=0;while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+(c^48);c=getchar();}x*=f;return *this;}template<typename T>typename std::enable_if_t<is_floating_point_ex<T>::value,Reader&>operator>>(T&x){char c=getchar();short f=1,s=0;x=0;T t=0;while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')x=x*10+(c^48),c=getchar();if(c=='.')c=getchar();else return x*=f,*this;while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;x=(x+t)*f;return*this;}template<typename T>typename std::enable_if_t<is_char<T>::value,Reader&>operator>>(T&c){c=getchar();while(c=='\n'||c==' '||c=='\r')c=getchar();return *this;}Reader&operator>>(char*str){int len=0;char c=getchar();while(c=='\n'||c==' '||c=='\r')c=getchar();while(c!='\n'&&c!=' '&&c!='\r')str[len++]=c,c=getchar();str[len]='\0';return*this;}Reader&operator>>(std::string&str){str.clear();char c=getchar();while(c=='\n'||c==' '||c=='\r')c=getchar();while(c!='\n'&&c!=' '&&c!='\r')str.push_back(c),c=getchar();return*this;}Reader(){}}cin;const char endl='\n';struct Writer{typedef __int128 mxdouble;template<typename T>typename std::enable_if_t<std::is_class<T>::value,Writer&>operator<<(T x){for(auto &y:x)*this<<y<<between;*this<<'\n';return *this;}template<typename T>typename std::enable_if_t<is_integral_ex<T>::value,Writer&>operator<<(T x){if(x==0)return putchar('0'),*this;if(x<0)putchar('-'),x=-x;static int sta[45];int top=0;while(x)sta[++top]=x%10,x/=10;while(top)putchar(sta[top]+'0'),--top;return*this;}template<typename T>typename std::enable_if_t<is_floating_point_ex<T>::value,Writer&>operator<<(T x){if(x<0)putchar('-'),x=-x;x+=pow(10,-Setprecision)/2;mxdouble _=x;x-=(T)_;static int sta[45];int top=0;while(_)sta[++top]=_%10,_/=10;if(!top)putchar('0');while(top)putchar(sta[top]+'0'),--top;putchar('.');for(int i=0;i<Setprecision;i++)x*=10;_=x;while(_)sta[++top]=_%10,_/=10;for(int i=0;i<Setprecision-top;i++)putchar('0');while(top)putchar(sta[top]+'0'),--top;return*this;}template<typename T>typename std::enable_if_t<is_char<T>::value,Writer&>operator<<(T c){putchar(c);return*this;}Writer&operator<<(char*str){int cur=0;while(str[cur])putchar(str[cur++]);return *this;}Writer&operator<<(const char*str){int cur=0;while(str[cur])putchar(str[cur++]);return*this;}Writer&operator<<(std::string str){int st=0,ed=str.size();while(st<ed)putchar(str[st++]);return*this;}Writer(){}void flush(){Fwrite::flush();}}cout;}
#undef Setprecision
using Fastio::cin;
using Fastio::cout;
using Fastio::endl;
using istream=Fastio::Reader;
using ostream=Fastio::Writer;
void solve(){
	int n;
	cin>>n;
	vector<int>a(n);
	cin>>a;vector<array<int,2>>r;
	if(n>0){
		int cur=a[0];
		int len=1;
		for(int i=1;i<n;i++){
			if(a[i]==cur)len++;
			else{
				r.push_back({cur,len});
				cur=a[i];
				len=1;
			}
		}
		r.push_back({cur,len});
	}
	int m=r.size();
	vector<int>type(m);
	for(int i=0;i<m;i++){
		if(r[i][0]==r[i][1])type[i]=0;
		else if(r[i][0]<r[i][1])type[i]=1;
		else type[i]=2;
	}
	vector<vector<int>>strs;
	int i=0;
	while(i<m){
		if(type[i]==2){
			i++;
			continue;
		}
		int j=i;
		vector<int>vals;
		vector<int>lx;
		while(j<m&&type[j]!=2){
			vals.push_back(r[j][0]);
			if(type[j]==1)lx.push_back(j-i);
			j++;
		}
		int q=lx.size();
		if(q==0)strs.push_back(vals);
		else{
			vector<int>sub;
			for(int k=0;k<=lx[0];k++)sub.push_back(vals[k]);
			strs.push_back(sub);
			for(int idx=0;idx<q-1;idx++){
				sub.clear();
				for(int k=lx[idx];k<=lx[idx+1];k++)sub.push_back(vals[k]);
				strs.push_back(sub);
			}
			sub.clear();
			for(int k=lx[q-1];k<vals.size();k++)sub.push_back(vals[k]);
			strs.push_back(sub);
		}
		i=j;
	}
	vector<int>t;
	for(auto&v:strs){
		for(int x:v)t.push_back(x);
		t.push_back(0);
	}
	int M=t.size();
	if(M==0){
		cout<<0<<endl;
		return;
	}
	vector<int>ld(M);
	int cur=0;
	for(int i=M-1;i>=0;i--){
		if(t[i]==0)cur=0;
		else cur++;
		ld[i]=cur;
	}
	vector<int>sa(M),rk(M),ht(M);
	int msg=10;
	vector<int>cnt(max(M+1,msg)),x(M),y(M);
	for(int i=0;i<M;i++)x[i]=t[i],cnt[x[i]]++;
	for(int i=1;i<msg;i++)cnt[i]+=cnt[i-1];
	for(int i=M-1;i>=0;i--)sa[--cnt[x[i]]]=i;
	for(int k=1;k<M;k<<=1){
		int p=0;
		for(int i=M-k;i<M;i++)y[p++]=i;
		for(int i=0;i<M;i++)if(sa[i]>=k)y[p++]=sa[i]-k;
		for(int i=0;i<msg;i++)cnt[i]=0;
		for(int i=0;i<M;i++)cnt[x[y[i]]]++;
		for(int i=1;i<msg;i++)cnt[i]+=cnt[i-1];
		for(int i=M-1;i>=0;i--)sa[--cnt[x[y[i]]]]=y[i];
		swap(x,y);
		x[sa[0]]=0;
		p=1;
		for(int i=1;i<M;i++){
		    int a=sa[i-1],b=sa[i];
			int val1=(a+k<M)?y[a+k]:-1;
			int val2=(b+k<M)?y[b+k]:-1;
			if(y[a]==y[b]&&val1==val2)x[b]=p-1;
			else x[b]=p++;
		}
		if(p==M)break;
		msg=p;
	}
	for(int i=0;i<M;i++)rk[sa[i]]=i;
	for(int i=0,k=0;i<M;i++){
		if(rk[i]==0)continue;
		if(k)k--;
		int j=sa[rk[i]-1];
		while(i+k<M&&j+k<M&&t[i+k]==t[j+k])k++;
		ht[rk[i]]=k;
	}
	int ans=0;
	for(int i=0;i<M;i++){
		if(t[sa[i]]==0)continue;
		int curl=ld[sa[i]];
		int lcp=ht[i];
		if(curl>lcp)ans+=curl-lcp;
	}
	cout<<ans<<endl;
}
}
bool ed;
signed main(){
#ifdef debug
	cerr<<"----------------------------------- START -----------------------------------\n";
#endif
	int t=1;
	//cin>>t;
	while(t--)AC::solve();
#ifdef debug
	cerr<<"\n--------------------------------- INPUT END ---------------------------------\n";
	AC::cout.flush();
	cerr<<"\n------------------------------------ END ------------------------------------\n";
	cerr<<setprecision(3)<<fixed<<"Total Memory : "<<left<<setw(10)<<(&ed-&st)/1048576.0<<" \tMB\n"<<"               "<<left<<setw(10)<<(&ed-&st)/1024.0<<" \tKB\n"<<"               "<<left<<setw(10)<<(&ed-&st)<<" \tByte\n";
#endif
}

提出情報

提出日時
問題 G - 221 Substring
ユーザ nut321
言語 C++23 (GCC 15.2.0)
得点 600
コード長 7162 Byte
結果 AC
実行時間 194 ms
メモリ 49984 KiB

コンパイルエラー

./Main.cpp: In function 'void AC::solve()':
./Main.cpp:73:44: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   73 |                         for(int k=lx[q-1];k<vals.size();k++)sub.push_back(vals[k]);
      |                                           ~^~~~~~~~~~~~

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 600 / 600
結果
AC × 2
AC × 53
セット名 テストケース
Sample 00-sample-01.txt, 00-sample-02.txt
All 00-sample-01.txt, 00-sample-02.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 01-11.txt, 01-12.txt, 01-13.txt, 01-14.txt, 01-15.txt, 01-16.txt, 01-17.txt, 01-18.txt, 01-19.txt, 01-20.txt, 01-21.txt, 01-22.txt, 01-23.txt, 01-24.txt, 01-25.txt, 01-26.txt, 01-27.txt, 01-28.txt, 01-29.txt, 01-30.txt, 01-31.txt, 01-32.txt, 01-33.txt, 01-34.txt, 01-35.txt, 01-36.txt, 01-37.txt, 01-38.txt, 01-39.txt, 01-40.txt, 01-41.txt, 01-42.txt, 01-43.txt, 01-44.txt, 01-45.txt, 01-46.txt, 01-47.txt, 01-48.txt, 01-49.txt, 01-50.txt, 01-51.txt
ケース名 結果 実行時間 メモリ
00-sample-01.txt AC 1 ms 3580 KiB
00-sample-02.txt AC 1 ms 3640 KiB
01-01.txt AC 1 ms 3428 KiB
01-02.txt AC 1 ms 3532 KiB
01-03.txt AC 1 ms 3572 KiB
01-04.txt AC 1 ms 3572 KiB
01-05.txt AC 1 ms 3580 KiB
01-06.txt AC 1 ms 3580 KiB
01-07.txt AC 1 ms 3580 KiB
01-08.txt AC 1 ms 3580 KiB
01-09.txt AC 1 ms 3652 KiB
01-10.txt AC 1 ms 3540 KiB
01-11.txt AC 1 ms 3572 KiB
01-12.txt AC 1 ms 3624 KiB
01-13.txt AC 1 ms 3580 KiB
01-14.txt AC 1 ms 3652 KiB
01-15.txt AC 1 ms 3616 KiB
01-16.txt AC 1 ms 3440 KiB
01-17.txt AC 1 ms 3532 KiB
01-18.txt AC 1 ms 3440 KiB
01-19.txt AC 1 ms 3568 KiB
01-20.txt AC 33 ms 28544 KiB
01-21.txt AC 194 ms 49984 KiB
01-22.txt AC 4 ms 7284 KiB
01-23.txt AC 64 ms 39904 KiB
01-24.txt AC 52 ms 30772 KiB
01-25.txt AC 79 ms 26584 KiB
01-26.txt AC 17 ms 16836 KiB
01-27.txt AC 62 ms 40112 KiB
01-28.txt AC 64 ms 39984 KiB
01-29.txt AC 63 ms 41000 KiB
01-30.txt AC 67 ms 39884 KiB
01-31.txt AC 51 ms 30672 KiB
01-32.txt AC 52 ms 30656 KiB
01-33.txt AC 52 ms 30904 KiB
01-34.txt AC 54 ms 30908 KiB
01-35.txt AC 77 ms 26584 KiB
01-36.txt AC 76 ms 26320 KiB
01-37.txt AC 72 ms 28604 KiB
01-38.txt AC 56 ms 26624 KiB
01-39.txt AC 18 ms 16880 KiB
01-40.txt AC 17 ms 17004 KiB
01-41.txt AC 18 ms 17732 KiB
01-42.txt AC 18 ms 17072 KiB
01-43.txt AC 1 ms 3580 KiB
01-44.txt AC 1 ms 3584 KiB
01-45.txt AC 1 ms 3628 KiB
01-46.txt AC 1 ms 3840 KiB
01-47.txt AC 1 ms 3836 KiB
01-48.txt AC 1 ms 3684 KiB
01-49.txt AC 21 ms 17912 KiB
01-50.txt AC 28 ms 20544 KiB
01-51.txt AC 29 ms 21060 KiB