QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#511264 | #6963. Border Queries | ship2077 | AC ✓ | 194ms | 32560kb | C++14 | 6.4kb | 2024-08-09 18:09:21 | 2024-08-09 18:09:22 |
Judging History
answer
#if defined(LOCAL) or not defined(LUOGU)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast,unroll-loops")
#endif
#include<bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include"dbg.h"
#else
#define dbg(...) (__VA_ARGS__)
#endif
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 10
#define between '\n'
#define __int128 long long
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(){}}cout;}
#define cin Fastio::cin
#define cout Fastio::cout
#define endl Fastio::endl
constexpr int M=2e6+5;
int n,m,q,len,N,nxt[M],w[M],R[M],pos[M];
string S,T,str; long long sum1[M],sum2[M];
int cnt[M],rnk[M],sa[M],id[M],oldrnk[M],height[M];
void SA(int n){ int m=255;
for (int i=1;i<=m;i++) cnt[i]=0;
for (int i=1;i<=n;i++) cnt[rnk[i]=str[i]]++;
for (int i=1;i<=m;i++) cnt[i]+=cnt[i-1];
for (int i=n;i;i--) sa[cnt[rnk[i]]--]=i;
for (int lim=1;lim<=n;lim<<=1){ int num=0;
for (int i=n-lim+1;i<=n;i++) id[++num]=i;
for (int i=1;i<=n;i++) if (sa[i]>lim) id[++num]=sa[i]-lim;
for (int i=1;i<=m;i++) cnt[i]=0; int idx=0;
for (int i=1;i<=n;i++) cnt[rnk[id[i]]]++;
for (int i=1;i<=m;i++) cnt[i]+=cnt[i-1];
for (int i=n;i;i--) sa[cnt[rnk[id[i]]]--]=id[i];
for (int i=1;i<=n;i++) oldrnk[i]=rnk[i];
#define cmp(x,y) x+lim<=n&&y+lim<=n&&oldrnk[x]==oldrnk[y]&&oldrnk[x+lim]==oldrnk[y+lim]
for (int i=1;i<=n;i++) rnk[sa[i]]=cmp(sa[i-1],sa[i])?idx:++idx;
if ((m=idx)==n) break;
}
for (int i=1,k=0;i<=n;i++){ if (k) k--;
while (i+k<=n&&sa[rnk[i]-1]+k<=n&&str[i+k]==str[sa[rnk[i]-1]+k]) k++;
height[rnk[i]]=k;
}
}
void solve(){
cin>>n>>m>>q>>S>>T;
str=S;str.erase(str.begin());
str.pop_back();str+="@"+T;
len=str.length();N=n-1;
str=" "+str; S=" "+S; T=" "+T;
SA(len);
for (int i=2,j=0;i<=n;i++){
while (j&&S[j+1]!=S[i]) j=nxt[j];
nxt[i]=j+=S[j+1]==S[i];
}
for (int i=1;i<=n;i++) w[i]=0;
for (int i=nxt[n];i;i=nxt[i]) w[n-i]++; w[n]=w[n-1]=0;
for (int i=1;i<=n;i++) w[i]+=w[i-1];
for (int i=1;i<=m;i++) R[i]=0;
for (int i=1,tmp=0;i<=len;i++)
if (sa[i]<N) tmp=INT_MAX;
else R[sa[i]-N]=tmp=min(tmp,height[i]);
for (int i=len,tmp=0;i;i--)
if (sa[i]<N) tmp=height[i];
else R[sa[i]-N]=max(R[sa[i]-N],tmp),tmp=min(tmp,height[i]);
for (int i=1;i<=m;i++)
sum1[i]=sum1[i-1]+w[R[i]],
sum2[i]=sum2[i-1]+w[i],
R[i]+=i-2;
for (int i=1;i<=m<<1;i++) pos[i]=INT_MAX;
for (int i=1;i<=m;i++) pos[R[i]]=min(pos[R[i]],i);
for (int i=m<<1;i;i--) pos[i-1]=min(pos[i-1],pos[i]);
while (q--){ long long ans=0;
int ql,qr,p;cin>>ql>>qr;
if (pos[qr]==INT_MAX) p=qr+1;
else p=max(ql,pos[qr]);
if (ql<p) ans+=sum1[p-1]-sum1[ql-1];
if (p<=qr) ans+=sum2[qr-p+1];
cout<<ans<<endl;
}
}
int main(){
int T;cin>>T;while (T--) solve(); return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 194ms
memory: 32560kb
input:
505 100000 100000 100000 wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww...
output:
3579782 6647236 543408 1673575 456027 1296061 252861 4897583 1410669 1339129 3294734 2698709 2851056 5252310 99237 3201517 3090024 971847 4390041 1287976 195415 4077820 1123243 4935851 6733092 1928419 1575734 86265 1752391 1584396 4164128 864018 2810862 3600267 5599859 1105190 336828 2609252 4712144...
result:
ok 1000000 lines