QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#439299#5014. 复读程度275307894a0 617ms546588kbC++146.3kb2024-06-11 19:17:122024-06-11 19:17:13

Judging History

你现在查看的是最新测评结果

  • [2024-06-11 19:17:13]
  • 评测
  • 测评结果:0
  • 用时:617ms
  • 内存:546588kb
  • [2024-06-11 19:17:12]
  • 提交

answer

#include<bits/stdc++.h>
#define Gc() getchar()
#define Me(x,y) memset(x,y,sizeof(x))
#define Mc(x,y) memcpy(x,y,sizeof(x))
#define d(x,y) ((m)*(x-1)+(y))
#define R(n) (rnd()%(n)+1)
#define Pc(x) putchar(x)
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define eb emplace_back
#define all(x) x.begin(),x.end()
using namespace std;using ll=long long;using db=double;using lb=long db;using ui=unsigned;using ull=unsigned long long;using pii=pair<int,int>;
const int N=4e5+5,M=1e7+5,K=1000+5,mod=998244353,Mod=mod-1;const db eps=1e-9;const int INF=1e9+7;mt19937 rnd(263082);
#define Tp template<typename T>
#define Ts template<typename T,typename... Ar>
namespace Debug{
	Tp void _debug(char* f,T t){cerr<<f<<'='<<t<<endl;}
	Ts void _debug(char* f,T x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);}
	#ifdef LOCAL
	#define gdb(...) _debug((char*)#__VA_ARGS__,__VA_ARGS__)
	#else 
	#define gdb(...) void()
	#endif
}using namespace Debug;
int n,q;
char s[N];
ull wl[N],wr[N];
struct SAM{
	int son[N][26],link[N],len[N],ed[N],La=1,cnt=1;
	void extend(int c){
		len[++cnt]=len[La]+1;int p=La;La=cnt;siz[La]=1;
		while(p&&!son[p][c]) son[p][c]=La,p=link[p];
		if(!p){link[La]=1;return;}
		int q=son[p][c];if(len[q]==len[p]+1){link[La]=q;return;}
		len[++cnt]=len[p]+1;Mc(son[cnt],son[q]);link[cnt]=link[q];link[q]=link[La]=cnt;ed[cnt]=ed[q];
		while(p&&son[p][c]==q) son[p][c]=cnt,p=link[p];
	}
	vector<int> S[N];
	int bl[N];
	int bg[N],en[N],bh,df[N];
	int trans[N][26];
	ull sum[N],siz[N];
	void make(int x,int La){
		bg[x]=++bh;df[bh]=x;
		for(int i:S[x]) make(i,x),ed[i]+len[x]<=n&&(trans[x][s[ed[i]+len[x]]-'a']=i),ed[x]=ed[i],sum[x]+=sum[i],siz[x]+=siz[i];
		en[x]=bh;
	}
	int fa[20][N];
	void build(){
		for(int i=2;i<=cnt;i++) S[link[i]].push_back(i);
		make(1,0);
		for(int i=1;i<=cnt;i++) fa[0][i]=link[i];
		for(int i=1;i<=18;i++) for(int j=1;j<=cnt;j++) fa[i][j]=fa[i-1][fa[i-1][j]];
	}
	vector<int> G[N];
	void push(int x){
		for(int i=0;i<26;i++) if(son[x][i]&&len[son[x][i]]==len[x]+1){
			push(son[x][i]);
			if(!bl[x]) bl[x]=bl[son[x][i]];
		}
		// gdb(x,bl[x]);
		G[bl[x]].push_back(x);
	}
	int jump(int x,int k){
		for(int i=18;~i;i--) if(len[fa[i][x]]>=k) x=fa[i][x];
		return x;
	}
}t1,t2;
int cnt,mx[N];
void dfs(int x,int y){
	// gdb(x,y,t1.len[x],t2.len[y]);
	if(t1.len[x]==t2.len[y]) cnt==84&&(gdb(x,y),0),t1.bl[x]=t2.bl[y]=++cnt,mx[cnt]=t1.len[x];
	for(int i=0;i<26;i++) if(t1.son[x][i]&&t1.len[x]+1==t1.len[t1.son[x][i]]) dfs(t1.son[x][i],t1.len[x]==t2.len[y]?t2.trans[y][i]:y);
}
int i1[N],i2[N];
void build(){
	for(int i=1;i<=n;i++) t1.extend(s[i]-'a'),i1[i]=t1.La,t1.sum[t1.La]=wr[i],t1.ed[t1.La]=i;
	for(int i=n;i;i--) t2.extend(s[i]-'a'),i2[i]=t2.La,t2.sum[t2.La]=wl[i],t2.ed[t2.La]=i;
	t1.build();t2.build();
	dfs(1,1);
	t1.push(1);t2.push(1);
	for(int i=1;i<=t1.cnt;i++) t1.sum[i]*=t1.siz[i];
}
ull ans[N];
struct ques{int l,r,id;ull w;}Q[N];int Qh;
ull qans[N];
vector<ques> S1[N],S2[N];
ull buf[N*2],*head;
struct DS{
	ull *f,*g;
	int m,Bl;
	void resize(int n){
		f=head;head+=n+1;
		m=n;Bl=sqrt(m);
		g=head;head+=m/Bl+2;
		fill(f,f+n+1,0);
	}
	void add(int x,ll w){
		for(int i=x;i<=m&&i/Bl==x/Bl;i++) f[i]+=w;
		for(int i=x/Bl;i<=m/Bl;i++) g[i]+=w;
	}
	ll qry(int x){return f[x]+(x/Bl?g[x/Bl-1]:0);}
}f[N];
namespace bit{
	ull f[N];
	void add(int x,ull w){assert(x);while(x<=2*n) f[x]+=w,x+=x&-x;}
	ull qry(int x){ull ans=0;while(x) ans+=f[x],x-=x&-x;return ans;}
	void clr(int x){while(x<=2*n&&f[x]) f[x]=0,x+=x&-x;}
}
vector<ques> G1[N],G2[N];
void work(){
	head=buf;
	for(int i=1;i<=cnt;i++) f[i].resize(t1.G[i].size());
	for(int i=1;i<=t2.cnt;i++){
		f[t2.bl[t2.df[i]]].add(mx[t2.bl[t2.df[i]]]-t2.len[t2.df[i]],t2.sum[t2.df[i]]);
		for(auto j:S1[i]){
			for(int h=j.l;h<=j.r;h++) qans[j.id]+=j.w*t1.sum[t1.df[h]]*f[t1.bl[t1.df[h]]].qry(t1.len[t1.df[h]]-t1.len[t1.link[t1.df[h]]]-1);
		}
	}
	for(int i=1;i<=cnt;i++){
		sort(all(G1[i]),[](ques x,ques y){return x.l<y.l;});
		int R=0;
		for(auto j:G1[i]){//gdb(i,j.l,j.r);
			while(R<t1.G[i].size()&&R<=j.l) bit::add(t1.bg[t1.G[i][R]],t1.sum[t1.G[i][R]]),R++;
			ans[j.id]+=j.w*(bit::qry(t1.en[j.r])-bit::qry(t1.bg[j.r]-1));
		}
		while(--R>=0) bit::clr(t1.bg[t1.G[i][R--]]);
	}
}
void Solve(){
	int i,j;scanf("%d%d%s",&n,&q,s+1);
	for(i=1;i<=n;i++) scanf("%llu",&wl[i]);
	for(i=1;i<=n;i++) scanf("%llu",&wr[i]);
	build();
	for(i=1;i<=q;i++){
		int l1,r1,l2,r2;
		scanf("%d%d%d%d",&l1,&r1,&l2,&r2);
		int x=t2.jump(i2[l1],r1-l1+1),y=t1.jump(i1[r2],r2-l2+1);
		int i1=t2.bl[x],i2=t1.bl[y],len1=r1-l1+1,len2=r2-l2+1;
		if(t1.bl[y]==t2.bl[x]){
			if(t2.len[x]+t1.len[y]-mx[i1]>t1.len[t1.link[y]]&&t2.len[x]+t1.len[y]-mx[i1]<max(len1,len2)) ans[i]-=t1.sum[y]*t2.sum[x];
		}else{
			G1[i1].push_back((ques){t2.len[x]-len1,y,i,t2.sum[x]});
			G1[i1].push_back((ques){t2.len[x]-(t2.len[t2.link[x]]+1),y,i,-t2.sum[x]});
			/*for(int j=t2.len[x]-len1+1;j<=t2.len[x]-(t2.len[t2.link[x]]+1);j++){
				if(t1.bg[y]<=t1.bg[t1.G[i1][j]]&&t1.bg[t1.G[i1][j]]<=t1.en[y]) ans[i]-=t2.sum[x]*t1.sum[t1.G[i1][j]];
			}*/
			G2[i2].push_back((ques){t1.len[y]-len2,x,i,t1.sum[y]});
			G2[i2].push_back((ques){t1.len[y]-(t1.len[t1.link[y]]+1),x,i,-t1.sum[y]});
			/*for(int j=t1.len[y]-len2+1;j<=t1.len[y]-(t1.len[t1.link[y]]+1);j++){
				if(t2.bg[x]<=t2.bg[t2.G[i2][j]]&&t2.bg[t2.G[i2][j]]<=t2.en[x]) ans[i]-=t1.sum[y]*t2.sum[t2.G[i2][j]];
			}*/
		}
		Q[++Qh]=(ques){t1.bg[y]-1,t2.bg[x]-1,i,1};
		Q[++Qh]=(ques){t1.en[y],t2.en[x],i,1};
		Q[++Qh]=(ques){t1.bg[y]-1,t2.en[x],i,-1ull};
		Q[++Qh]=(ques){t1.en[y],t2.bg[x]-1,i,-1ull};
	}

	int l=0,r=0,Bl=max(n/sqrt(q),1.0);

	sort(Q+1,Q+Qh+1,[&](ques x,ques y){return x.l/Bl^y.l/Bl?x.l<y.l:(x.l/Bl&1?x.r<y.r:x.r>y.r);});
	
	for(i=1;i<=Qh;i++){
		qans[i]=qans[i-1];
		if(l<Q[i].l)S1[r].push_back((ques){l+1,Q[i].l,i,1});
		else S1[r].push_back((ques){Q[i].l+1,l,i,-1ull});
		l=Q[i].l;
		if(r<Q[i].r) S2[l].push_back((ques){r+1,Q[i].r,i,1});
		else S2[l].push_back((ques){Q[i].r+1,r,i,-1ull});
		r=Q[i].r;
	}
	work();
	swap(t1,t2);swap(S1,S2);swap(G1,G2);
	work();
	for(i=1;i<=Qh;i++) qans[i]+=qans[i-1],ans[Q[i].id]+=qans[i]*Q[i].w;
	for(i=1;i<=q;i++) printf("%llu\n",ans[i]);
}
int main(){
	int t=1;
	// scanf("%d",&t);
	while(t--) Solve();
	cerr<<clock()*1.0/CLOCKS_PER_SEC<<'\n';
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 97ms
memory: 487300kb

input:

500 500
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

14264274612193200718
9153526696129496766
11612104605228272123
9581130985882122374
15104335278610506492
15330498707505860487
7930009201213421812
3593689281635086530
7431743303092224170
2640054063167179374
16301747630188114050
14954031087026017844
13047839576706547141
13142314545999179754
174761466369...

result:

wrong answer 1st lines differ - expected: '15720454042420499810', found: '14264274612193200718'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 0
Wrong Answer

Test #22:

score: 0
Wrong Answer
time: 617ms
memory: 546588kb

input:

100000 100000
zbbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbabaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaababbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbabaababbaabbabaababbabaabbaabab...

output:

3448684389185762544
2409962914769200003
17052971495472957554
17668679206267169316
5809938280743828238
16929670167162589385
5007158483219069858
13901233728818129710
5821899279772898061
13147619928522064120
8619629262821010737
16689432168882886068
8484639454184628189
432088324301719512
175418503779856...

result:

wrong answer 1st lines differ - expected: '16102224067619618967', found: '3448684389185762544'

Subtask #5:

score: 0
Skipped

Dependency #4:

0%

Subtask #6:

score: 0
Skipped

Dependency #2:

0%

Subtask #7:

score: 0
Skipped

Dependency #3:

0%