QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#440224#5014. 复读程度C1942huangjiaxu0 559ms246336kbC++146.5kb2024-06-13 13:11:462024-06-13 13:11:46

Judging History

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

  • [2024-06-13 13:11:46]
  • 评测
  • 测评结果:0
  • 用时:559ms
  • 内存:246336kb
  • [2024-06-13 13:11:46]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5,M=4e5+5;
typedef unsigned long long ull;
int n,m,tot;
ull wl[N],wr[N],Ans[N],ans[M],ans2[M];
char s[N];
struct node{
	int ch[26],len,fa;
};
struct SAM{
	node tr[N];
	int str[N],son[N][26],occ[N],pos[N],bel[N],ip[N],sz[N];
	ull sum[N];
	int cnt=1,ls=1;
	int fa[N][19],in[N],out[N];
	vector<int>e[N];
	void ins(int c){
		int p=ls,np=ls=++cnt;
		tr[np].len=tr[p].len+1;
		str[tr[np].len]=c,ip[tr[np].len]=np;
		for(;p&&!tr[p].ch[c];p=tr[p].fa)tr[p].ch[c]=np;
		if(!p)tr[np].fa=1;
		else{
			int q=tr[p].ch[c];
			if(tr[q].len==tr[p].len+1)tr[np].fa=q;
			else{
				int nq=++cnt;tr[nq]=tr[q];
				tr[nq].len=tr[p].len+1,tr[np].fa=tr[q].fa=nq;
				for(;p&&tr[p].ch[c]==q;p=tr[p].fa)tr[p].ch[c]=nq;
			}
		}
	}
	void dfs(int x){
		in[x]=++in[0];
		for(int i=1;i<19;++i)fa[x][i]=fa[fa[x][i-1]][i-1];
		for(auto v:e[x]){
			fa[v][0]=x;
			sz[v]=tr[v].len-tr[x].len;
			dfs(v);
			sum[x]+=sum[v];
			occ[x]+=occ[v];
			pos[x]=min(pos[x],pos[v]);
		}
		out[x]=in[0];
	}
	void build(ull *w){
		for(int i=1;i<=cnt;++i)e[tr[i].fa].emplace_back(i),pos[i]=1e9;
		int u=1;
		for(int i=1;i<=n;++i){
			u=tr[u].ch[str[i]];
			++occ[u];
			pos[u]=i,sum[u]=w[i];
		}
		dfs(1);
		for(int i=2;i<=cnt;++i)son[tr[i].fa][str[pos[i]-tr[tr[i].fa].len]]=i;
	}
	void feed(vector<int>* res){
		for(int u=cnt;u;--u){
			if(!bel[u])for(int i=0;i<26;++i)if(tr[u].ch[i])bel[u]=bel[tr[u].ch[i]];
			res[bel[u]].push_back(u);
		}
	}
	int find(int l,int r){
		int x=ip[r];
		for(int i=18;~i;--i)if(fa[x][i]&&tr[fa[x][i]].len>=r-l+1)x=fa[x][i];
		return x;
	}
}s1,s2;
struct qry1{
	int l,r,id,x;
	ull va;
};
struct Bit{
	ull tr[N];
	void upd(int x,ull v){
		for(;x<N;x+=x&-x)tr[x]+=v;
	}
	ull sum(int x){
		ull res=0;
		for(;x;x-=x&-x)res+=tr[x];
		return res;
	}
	ull sum(int l,int r){
		return sum(r)-sum(l-1);
	}
}bit;
struct qry2{
	int x,y,id;
}q[M];
bool cmp(qry2 a,qry2 b){
	int pa=a.x>>9,pb=b.x>>9;
	if(pa!=pb)return pa<pb;
	return (pa&1)?a.y>b.y:a.y<b.y;
}
vector<int>rid[N],cid[N];
vector<qry1>qc[N],qr[N],tq[N];
vector<qry2>qx[N],qy[N];
int rp[N],cp[N],occ[N],rvx[N],rvy[N],bx[N],by[N];
int qid[M],cq,segx[N],segy[N];
struct block{
	int sz;
	ull s[N],s2[N];
	void upd(int x,ull v){
		int y=((x>>9)+1)<<9;
		for(int i=x;i<y;++i)s[i]+=v;
		for(int i=y;i<=sz>>9;++i)s2[i]+=v;
	}
	void upd(int l,int r,ull v){
		upd(l,v);
		if(r<sz)upd(r+1,-v);
	}
	ull ask(int x){
		return s[x]+s2[x>>9];
	}
}vx,vy;
void dfs(int x,int y){
	bool fg=s1.tr[x].len==s2.tr[y].len;
	if(fg)s1.bel[x]=s2.bel[y]=++tot,occ[tot]=s1.occ[x];
	for(int i=0;i<26;++i)if(s1.tr[s1.tr[x].ch[i]].len==s1.tr[x].len+1)
		dfs(s1.tr[x].ch[i],fg?s2.son[y][i]:y);
}
void qry(int l1,int r1,int l2,int r2,int id){
	int p1=s1.find(l1,r1),p2=s2.find(n-r2+1,n-l2+1);
	int ln1=r1-l1+1,ln2=r2-l2+1;
	int y1=rp[p1],x1=s1.tr[p1].len-ln1,x2=cp[p2],y2=s2.tr[p2].len-ln2;
	if(s1.bel[p1]==s2.bel[p2]){
		if(x1!=x2||y1!=y2)
			if(x2>x1&&x2<s1.sz[p1]||y1>y2&&y1<s2.sz[p2])
				Ans[id]-=s1.sum[p1]*s2.sum[p2]*s1.occ[p1];
	}else{
		int o=s1.bel[p1];
		if(x1+1<s1.sz[p1]){
			qc[o].push_back({s2.in[p2],s2.out[p2],id,-x1,s1.sum[p1]*occ[o]});
			qc[o].push_back({s2.in[p2],s2.out[p2],id,s1.sz[p1]-1,s1.sum[p1]*occ[o]});
		}
		o=s2.bel[p2];
		if(y2+1<s2.sz[p2]){
			qr[o].push_back({s1.in[p1],s1.out[p1],id,-y2,s2.sum[p2]*occ[o]});
			qr[o].push_back({s1.in[p1],s1.out[p1],id,s2.sz[p2]-1,s2.sum[p2]*occ[o]});
		}
	}
	q[++cq]={s1.out[p1],s2.out[p2],0},qid[cq]=id;
	q[++cq]={s1.in[p1]-1,s2.out[p2],0},qid[cq]=-id;
	q[++cq]={s1.out[p1],s2.in[p2]-1,0},qid[cq]=-id;
	q[++cq]={s1.in[p1]-1,s2.in[p2]-1,0},qid[cq]=id;
}
void solve(int o){
	for(int i=0;i<cid[o].size();++i)tq[i].clear();
	for(auto v:qc[o]){
		if(v.x<0)tq[-v.x].push_back({v.l,v.r,v.id,-1,v.va});
		else tq[v.x].push_back({v.l,v.r,v.id,1,v.va});
	}
	for(int i=1;i<cid[o].size();++i){
		int u=cid[o][i];
		bit.upd(s2.in[u],s2.sum[u]);
		for(auto v:tq[i])Ans[v.id]-=bit.sum(v.l,v.r)*v.va*v.x;
	}
	for(int i=1;i<cid[o].size();++i){
		int u=cid[o][i];
		bit.upd(s2.in[u],-s2.sum[u]);
	}
	for(int i=0;i<rid[o].size();++i)tq[i].clear();
	for(auto v:qr[o]){
		if(v.x<0)tq[-v.x].push_back({v.l,v.r,v.id,-1,v.va});
		else tq[v.x].push_back({v.l,v.r,v.id,1,v.va});
	}
	for(int i=1;i<rid[o].size();++i){
		int u=rid[o][i];
		bit.upd(s1.in[u],s1.sum[u]);
		for(auto v:tq[i])Ans[v.id]-=bit.sum(v.l,v.r)*v.va*v.x;
	}
	for(int i=1;i<rid[o].size();++i){
		int u=rid[o][i];
		bit.upd(s1.in[u],-s1.sum[u]);
	}
}
void Solve(){
	for(int i=1;i<=cq;++i)q[i].id=i;
	sort(q+1,q+cq+1,cmp);
	for(int i=1,X=1,Y=1;i<=cq;++i){
		if(X<q[i].x)qy[Y].push_back({X+1,q[i].x,i}),X=q[i].x;
		if(Y<q[i].y)qx[X].push_back({Y+1,q[i].y,i}),Y=q[i].y;
		if(X>q[i].x)qy[Y].push_back({q[i].x+1,X,-i}),X=q[i].x;
		if(Y>q[i].y)qx[X].push_back({q[i].y+1,Y,-i}),Y=q[i].y;
	}
	vx.sz=s1.cnt;
	vy.sz=s2.cnt;
	for(int i=2;i<=s2.cnt;++i){
		int v=rvy[i],o=by[i];
		vx.upd(segx[rid[o][0]],segx[rid[o][s2.sz[v]-1]],s2.sum[v]*occ[o]);
		for(auto u:qy[i]){
			ull sv=0;
			for(int j=u.x;j<=u.y;++j){
				int z=rvx[j];
				sv+=vx.ask(segx[z])*s1.sum[z];
			}
			if(u.id<0)ans2[-u.id]-=sv;
			else ans2[u.id]+=sv;
		}
	}
	for(int i=2;i<=s1.cnt;++i){
		int v=rvx[i],o=bx[i];
		vy.upd(segy[cid[o][0]],segy[cid[o][s1.sz[v]-1]],s1.sum[v]*occ[o]);
		for(auto u:qx[i]){
			ull sv=0;
			for(int j=u.x;j<=u.y;++j){
				int z=rvy[j];
				sv+=vy.ask(segy[z])*s2.sum[z];
			}
			if(u.id<0)ans2[-u.id]-=sv;
			else ans2[u.id]+=sv;
		}
	}
	for(int i=1;i<=cq;++i){
		ans2[i]+=ans2[i-1];
		ans[q[i].id]=ans2[i];
	}
}
int main(){
	scanf("%d%d",&n,&m);
	scanf("%s",s+1);
	for(int i=1;i<=n;++i)scanf("%llu",&wl[i]);
	for(int i=1;i<=n;++i)scanf("%llu",&wr[i]);
	for(int i=1;i<=n;++i)s[i]-='a';
	for(int i=1;i<=n;++i)s1.ins(s[i]);
	for(int i=n;i;--i)s2.ins(s[i]);
	s1.build(wr);
	reverse(wl+1,wl+n+1);
	s2.build(wl);
	dfs(1,1);
	s1.feed(rid),s2.feed(cid);
	for(int i=1;i<=tot;++i){
		for(int j=0;j<rid[i].size();++j){
			int v=rid[i][j];
			rp[v]=j,rvx[s1.in[v]]=v;
			bx[s1.in[v]]=i;
			segx[v]=++segx[0];
		}
		for(int j=0;j<cid[i].size();++j){
			int v=cid[i][j];
			cp[v]=j,rvy[s2.in[v]]=v;
			by[s2.in[v]]=i;
			segy[v]=++segy[0];
		}
	}
	for(int i=1,l1,r1,l2,r2;i<=m;++i){
		scanf("%d%d%d%d",&l2,&r2,&l1,&r1);
		qry(l1,r1,l2,r2,i);
	}
	for(int i=2;i<=tot;++i)solve(i);
	Solve();
	for(int i=1;i<=cq;++i){
		if(qid[i]<0)Ans[-qid[i]]-=ans[i];
		else Ans[qid[i]]+=ans[i];
	}
	for(int i=1;i<=m;++i)printf("%llu\n",Ans[i]);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 8ms
memory: 88104kb

input:

500 500
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

15720454042420499810
11859832711827305340
17628143378099914328
4030024243931986061
18033423360813892607
8620267973460635890
3883374861354698625
16650831689368240202
15844729690445267497
2683289915379600554
13133811958066776394
14181220923901262251
18173739360450512256
13142314545999179754
9334524406...

result:

wrong answer 2nd lines differ - expected: '4058077030882532408', found: '11859832711827305340'

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: 559ms
memory: 246336kb

input:

100000 100000
zbbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbabaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaababbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbabaababbaabbabaababbabaabbaabab...

output:

11568402808043370980
8257783105324635620
15030510204932555331
0
5485543851766982805
16259302487534093542
14423155054027005455
3574208073966186530
17910674689456629071
2820594273670120940
15278359611802410921
9542479996457597248
1929197389858418363
0
1484205791417979935
15907881633889893489
844790413...

result:

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

Subtask #5:

score: 0
Skipped

Dependency #4:

0%

Subtask #6:

score: 0
Skipped

Dependency #2:

0%

Subtask #7:

score: 0
Skipped

Dependency #3:

0%