QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#440202#5014. 复读程度Kevin53077 1835ms34556kbC++239.2kb2024-06-13 12:13:292024-06-13 12:13:29

Judging History

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

  • [2024-06-13 12:13:29]
  • 评测
  • 测评结果:7
  • 用时:1835ms
  • 内存:34556kb
  • [2024-06-13 12:13:29]
  • 提交

answer

//Author: Kevin
#include<bits/stdc++.h>
//#pragma GCC optimize("O2")
using namespace std;
#define ll long long
#define ull unsigned ll
#define pb emplace_back
#define mp make_pair
#define ALL(x) (x).begin(),(x).end()
#define rALL(x) (x).rbegin(),(x).rend()
#define srt(x) sort(ALL(x))
#define rev(x) reverse(ALL(x))
#define rsrt(x) sort(rALL(x))
#define sz(x) (int)(x.size())
#define inf 0x3f3f3f3f
#define pii pair<int,int>
#define lb(v,x) (int)(lower_bound(ALL(v),x)-v.begin())
#define ub(v,x) (int)(upper_bound(ALL(v),x)-v.begin())
#define uni(v) v.resize(unique(ALL(v))-v.begin())
#define longer __int128_t
void die(string S){puts(S.c_str());exit(0);}
class SuffixAutomaton
{
	#define MAXN 200000+5
	#define MAXLG 20
	public:
		SuffixAutomaton():last(1),tot(1){}
		int trans[MAXN][26],fail[MAXN],len[MAXN],occ[MAXN],last,tot,dfn,in[MAXN],out[MAXN],id[MAXN],rep[MAXN];
		int anc[MAXLG][MAXN];
		ull weight[MAXN];
		vector<int> G[MAXN];
		int extend(int x,ull w=0,int pos=0)
		{
			int p=last;
			int np=++tot;
			len[np]=len[p]+1;
			while(p&&!trans[p][x])
			{
				trans[p][x]=np;
				p=fail[p];
			}
			if(!p)
				fail[np]=1;
			else
			{
				int q=trans[p][x];
				if(len[p]+1==len[q])
					fail[np]=q;
				else
				{
					int nq=++tot;
					memcpy(trans[nq],trans[q],sizeof(int)*26);
					len[nq]=len[p]+1;
					fail[nq]=fail[q];
					fail[q]=fail[np]=nq;
					while(p&&trans[p][x]==q)
					{
						trans[p][x]=nq;
						p=fail[p];
					}
				}
			}
			last=np;
			occ[last]++;
			weight[last]+=w;
			rep[last]=pos;
			return last;
		}
		void dfs(int u)
		{
			in[u]=++dfn;
			id[dfn]=u;
			for(auto v:G[u])
			{
				dfs(v);
				occ[u]+=occ[v];
				weight[u]+=weight[v];
				rep[u]=max(rep[u],rep[v]);
			}
			out[u]=dfn;
		}
		void init()
		{
			for(int i=1;i<=tot;i++)
				if(fail[i])
					G[fail[i]].pb(i);
			dfs(1);
			for(int i=1;i<=tot;i++)
				anc[0][i]=fail[i];
			for(int i=1;i<20;i++)
				for(int j=1;j<=tot;j++)
					anc[i][j]=anc[i-1][anc[i-1][j]];
		}
		int locate(int node,int length)
		{
			for(int i=MAXLG-1;i>=0;i--)
				if(len[anc[i][node]]>=length)
					node=anc[i][node];
			return node;
		}
	#undef MAXN
	#undef MAXLG
};
class BasicSubstringStructure
{
	#define MAXN 100000+5
	public:
		BasicSubstringStructure(string _s):s(_s){build();}
		BasicSubstringStructure(){}
		string s;
		SuffixAutomaton S1,S2;
		int node1[MAXN],node2[MAXN],n;
		ull wl[MAXN],wr[MAXN];
		void build()
		{
			n=sz(s);
			for(int i=1;i<=n;i++)
				node1[i]=S1.extend(s[i-1]-'a',wr[i],i);
			for(int i=n;i>=1;i--)
				node2[i]=S2.extend(s[i-1]-'a',wl[i],i);
			S1.init();
			S2.init();
		}
		int locate1(pii str)
		{
			int nd1=S1.locate(node1[str.second],str.second-str.first+1);
			return nd1;
		}
		int locate2(pii str)
		{
			int nd2=S2.locate(node2[str.first],str.second-str.first+1);
			return nd2;
		}
		pii rep(pii str)
		{
			int nd1=S1.locate(node1[str.second],str.second-str.first+1);
			str=mp(str.second-S1.len[nd1]+1,str.second);
			int nd2=S2.locate(node2[str.first],str.second-str.first+1);
			return str=mp(str.first,str.first+S2.len[nd2]-1);
		}
		int occ(pii str)
		{
			int nd1=S1.locate(node1[str.second],str.second-str.first+1);
			return S1.occ[nd1];
		}
		ull weight(pii str)
		{
			int nd1=S1.locate(node1[str.second],str.second-str.first+1);
			int nd2=S2.locate(node2[str.first],str.second-str.first+1);
			return S1.weight[nd1]*S2.weight[nd2];
		}
		vector<int> order1()
		{
			int s1=S1.tot;
			vector<pair<pii,pii>> vec;
			for(int i=2;i<=s1;i++)
			{
				pii pos(S1.rep[i]-S1.len[i]+1,S1.rep[i]);
				vec.pb(rep(pos),pii(S1.len[i]-S1.len[S1.fail[i]],i));
			}
			srt(vec);
			vector<int> ret;
			for(int i=0;i<sz(vec);i++)
				ret.pb(vec[i].second.second);
			return ret;
		}
		vector<int> order2()
		{
			int s2=S2.tot;
			vector<pair<pii,pii>> vec;
			for(int i=2;i<=s2;i++)
			{
				pii pos(S2.rep[i],S2.rep[i]+S2.len[i]-1);
				vec.pb(rep(pos),pii(S2.len[i]-S2.len[S2.fail[i]],i));
			}
			srt(vec);
			vector<int> ret;
			for(int i=0;i<sz(vec);i++)
				ret.pb(vec[i].second.second);
			return ret;
		}
	#undef MAXN
}BSS;
int n,q;
string s;
ull wl[100100],wr[100100];
ull ans[400400];
int ql[400400],qr[400400];
int l1[100100],r1[100100],l2[100100],r2[100100],nd1[100100],nd2[100100];
const int B1=630,B2=450;
int qx[400400],qly[400400],qry[400400];
int qy[400400],qlx[400400],qrx[400400];
ull ans1[400400],ans2[400400];
vector<int> vqx[400400],vqy[400400];
int s1,s2;
int label1[200200],label2[200200];
ull tag[200200/B2+5],rval[200200];
ull W[200200];
ull res[100100];
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>q>>s;
	for(int i=1;i<=n;i++)
		cin>>wl[i];
	for(int i=1;i<=n;i++)
		cin>>wr[i];
	BSS.s=s;
	for(int i=1;i<=n;i++)
		BSS.wl[i]=wl[i];
	for(int i=1;i<=n;i++)
		BSS.wr[i]=wr[i];
	BSS.build();
	for(int i=1;i<=q;i++)
	{
		cin>>l2[i]>>r2[i]>>l1[i]>>r1[i];
		nd1[i]=BSS.locate1({l1[i],r1[i]});
		nd2[i]=BSS.locate2({l2[i],r2[i]});
		// if(BSS.rep({l1[i],r1[i]})==BSS.rep({l2[i],r2[i]}))
		// {
		// 	pii rep=BSS.rep({l1[i],r1[i]});
		// 	int len=BSS.S1.len[nd1[i]]+BSS.S2.len[nd2[i]]-(rep.second-rep.first+1);
		// 	if(len<min(r1[i]-l1[i]+1,r2[i]-l2[i]+1))
		// 	{
		// 		int d1=BSS.S2.len[nd2[i]]-(rep.second-rep.first+1);
		// 		int d2=BSS.S1.len[nd1[i]]-(rep.second-rep.first+1);
		// 		rep.first-=d1;
		// 		rep.second+=d2;
		// 		res[i]-=BSS.occ(rep)*BSS.S2.weight[BSS.locate2(rep)]*BSS.S1.weight[BSS.locate1(rep)];
		// 	}
		// }
		for(int l=1;l<=n;l++)
			for(int r=l;r<=n;r++)
				if(r-l+1<max(r1[i]-l1[i]+1,r2[i]-l2[i]+1))
					if(BSS.locate1({l,r})==nd1[i]||BSS.locate2({l,r})==nd2[i])
					{
						int pos1=BSS.S1.in[BSS.locate1({l,r})],pos2=BSS.S2.in[BSS.locate2({l,r})];
						if(pos1<BSS.S1.in[nd1[i]]||pos1>BSS.S1.out[nd1[i]]) continue;
						if(pos2<BSS.S2.in[nd2[i]]||pos2>BSS.S2.out[nd2[i]]) continue;
						res[i]-=BSS.S1.weight[BSS.locate1({l,r})]*BSS.S2.weight[BSS.locate2({l,r})];
					}
		ql[i*4-3]=BSS.S1.in[nd1[i]]-1;
		qr[i*4-3]=BSS.S2.in[nd2[i]]-1;
		ql[i*4-2]=BSS.S1.out[nd1[i]];
		qr[i*4-2]=BSS.S2.in[nd2[i]]-1;
		ql[i*4-1]=BSS.S1.in[nd1[i]]-1;
		qr[i*4-1]=BSS.S2.out[nd2[i]];
		ql[i*4-0]=BSS.S1.out[nd1[i]];
		qr[i*4-0]=BSS.S2.out[nd2[i]];
	}
	vector<int> vq;
	for(int i=1;i<=q*4;i++)
		vq.pb(i);
	sort(ALL(vq),[&](int u,int v)
	{
		if(ql[u]/B1==ql[v]/B1) return qr[u]<qr[v];
		return ql[u]<ql[v];
	});
	int cl=1,cr=1;
	for(int i=1;i<=q*4;i++)
	{
		int ind=vq[i-1];
		int l=ql[ind],r=qr[ind];
		if(cl<=l)
		{
			qy[i]=cr;
			qlx[i]=cl+1;
			qrx[i]=l;
			cl=l;
		}
		else
		{
			qy[i]=cr;
			qlx[i]=l+1;
			qrx[i]=cl;
			cl=l;
		}
		if(cr<=r)
		{
			qx[i]=cl;
			qly[i]=cr+1;
			qry[i]=r;
			cr=r;
		}
		else
		{
			qx[i]=cl;
			qly[i]=r+1;
			qry[i]=cr;
			cr=r;
		}
	}
	for(int i=1;i<=q*4;i++)
	{
		vqx[qx[i]].pb(i);
		vqy[qy[i]].pb(i);
	}
	s1=BSS.S1.tot;
	s2=BSS.S2.tot;
	vector<int> order1=BSS.order1(),order2=BSS.order2();
	for(int i=1;i<=sz(order1);i++)
		label1[order1[i-1]]=i;
	for(int i=1;i<=sz(order2);i++)
		label2[order2[i-1]]=i;
	for(int i=1;i<=s2;i++)
		W[label2[i]]=BSS.S2.weight[i];
	for(int i=2;i<=s1;i++)
	{
		int node=BSS.S1.id[i];
		int pos=BSS.S1.rep[node];
		int ndl=BSS.locate2(pii(pos-BSS.S1.len[BSS.S1.fail[node]],pos));
		int ndr=BSS.locate2(pii(pos-BSS.S1.len[node]+1,pos));
		int cl=label2[ndl],cr=label2[ndr];
		int lb=cl/B2,rb=cr/B2;
		ull val=BSS.S1.weight[node]*BSS.S1.occ[node];
		for(int i=lb+1;i<rb;i++)
			tag[i]+=val;
		if(lb<rb)
		{
			for(int i=cl;i/B2==lb;i++)
				rval[i]+=val*W[i];
			for(int i=cr;i/B2==rb;i--)
				rval[i]+=val*W[i];
		}
		else
		{
			for(int i=cl;i<=cr;i++)
				rval[i]+=val*W[i];
		}
		for(auto qind:vqx[i])
			for(int ydfn=qly[qind];ydfn<=qry[qind];ydfn++)
			{
				int yind=label2[BSS.S2.id[ydfn]];
				ans1[qind]+=tag[yind/B2]*W[yind]+rval[yind];
			}
	}
	memset(tag,0,sizeof(tag));
	memset(rval,0,sizeof(rval));
	for(int i=1;i<=s1;i++)
		W[label1[i]]=BSS.S1.weight[i];
	for(int i=2;i<=s2;i++)
	{
		int node=BSS.S2.id[i];
		int pos=BSS.S2.rep[node];
		int ndl=BSS.locate1(pii(pos,pos+BSS.S2.len[BSS.S2.fail[node]]));
		int ndr=BSS.locate1(pii(pos,pos+BSS.S2.len[node]-1));
		int cl=label1[ndl],cr=label1[ndr];
		int lb=cl/B2,rb=cr/B2;
		ull val=BSS.S2.weight[node]*BSS.S2.occ[node];
		for(int i=lb+1;i<rb;i++)
			tag[i]+=val;
		if(lb<rb)
		{
			for(int i=cl;i/B2==lb;i++)
				rval[i]+=val*W[i];
			for(int i=cr;i/B2==rb;i--)
				rval[i]+=val*W[i];
		}
		else
		{
			for(int i=cl;i<=cr;i++)
				rval[i]+=val*W[i];
		}
		for(auto qind:vqy[i])
			for(int xdfn=qlx[qind];xdfn<=qrx[qind];xdfn++)
			{
				int xind=label1[BSS.S1.id[xdfn]];
				ans2[qind]+=tag[xind/B2]*W[xind]+rval[xind];
			}
	}
	cl=1;
	cr=1;
	ull tot=0;
	for(int i=1;i<=q*4;i++)
	{
		int ind=vq[i-1];
		int l=ql[ind],r=qr[ind];
		if(cl<=l)
		{
			tot+=ans2[i];
			cl=l;
		}
		else
		{
			tot-=ans2[i];
			cl=l;
		}
		if(cr<=r)
		{
			tot+=ans1[i];
			cr=r;
		}
		else
		{
			tot-=ans1[i];
			cr=r;
		}
		ans[ind]=tot;
	}
	for(int i=1;i<=q;i++)
		res[i]+=ans[i*4-0]-ans[i*4-1]-ans[i*4-2]+ans[i*4-3];
	for(int i=1;i<=q;i++)
		cout<<res[i]<<endl;
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 7
Accepted

Test #1:

score: 7
Accepted
time: 1835ms
memory: 28376kb

input:

500 500
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

15720454042420499810
4058077030882532408
14651762045124606089
4030024243931986061
18033423360813892607
9470601111824364484
3883374861354698625
16650831689368240202
8339028189650687576
2683289915379600554
13133811958066776394
14181220923901262251
18173739360450512256
13142314545999179754
148925491596...

result:

ok 500 lines

Test #2:

score: 7
Accepted
time: 970ms
memory: 28392kb

input:

500 500
zszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszzszszzszzszszzszszzszzszszzszszzszz...

output:

4843650749197240262
7777110205341291111
533576317536031175
16712994243500559204
9988085877723856684
9644193924482321332
3247342125341043527
18152622312040037224
13045121434804725850
10593529771756855740
13316626648976199221
6181092693273210423
9148547538129213975
9376364571107435561
2140403332478526...

result:

ok 500 lines

Test #3:

score: 7
Accepted
time: 150ms
memory: 28680kb

input:

500 500
aaaaabbaabbabbbaabaabbabbabbbaaabaaaabbbbbbaaabaabbbbbbaabbaaaaababbaaaaabbbbababbabaabbbbbbbbaaaaaaabaabbabbbbaabbaabaaabbbabbaabbbabaabaaaaababbaabbabbbabbababbbaabbabaaabbbbaaabbbabbabaabbabbaaabbaabbabbbbaaaaaababaaaabaababbaabbabbabbbabbaabbbaabbaaababaaabbababbbabaababaabbbbbabbababaab...

output:

841375054012212333
13406426787139944226
6541986259052503362
10583258635957015782
11582649090627508617
4747829250201069768
11571422754704651998
14603866222879735665
8438246043626601023
16155298152184479844
9052925087624568857
18388444310571976215
13304308468056840286
18125780089857220122
363421144082...

result:

ok 500 lines

Test #4:

score: 7
Accepted
time: 639ms
memory: 28500kb

input:

500 500
sulasusulasusulasulasulsulasusulasususulasulasulsulasulasulsulasulasulsulasususulasulasulsulasulasulsulasulasulsulasusulasulasulsulasulasulsulasulasulsulasusulasulasulsulasulasulsulasulasulsulasulasulsulasususulasulasulsulasulasulsulasulasulsulasusulasulasulsulasulasulsulasusulasusulasusulas...

output:

2320755102639148175
17108462705447992416
6030359132551843296
889683039894413148
10901851555398837076
1991544941914879425
9087724446342520941
5134546535199286414
12947484109492427089
5962550827492657739
4877066450610765849
6699323319072695780
11167645157062070624
13985172887966350800
8075429763917070...

result:

ok 500 lines

Test #5:

score: 7
Accepted
time: 1746ms
memory: 32628kb

input:

500 500
bbbbbbqouvtudkzorrxinacvncytgmtbbbbbbfyfzxjdqlcaadccvsbbbbbbqouvtudkzorrxinacvncytgmtbbbbbbbbbbbbqouvtudkzorrxinacvncytgmtbbbbbbfyfzxjdqlcaadccvsbbbbbbqouvtudkzorrxinacvncytgmtbbbbbbbbbbbbqouvtudkzorrxinacvncytgmtbbbbbbfyfzxjdqlcaadccvsbbbbbbqouvtudkzorrxinacvncytgmtbbbbbbbbbbbbqouvtudkzorrx...

output:

18295637548117042088
6105463594888898313
15681140870484623884
17957090271580958329
11763132903578154240
17769627666201366836
16493946443969420940
12712093409624537595
2436698665645215125
8863273927617841787
5065586857868462806
8771649105206144878
6715985691821336097
8851433094837196039
7055234226266...

result:

ok 500 lines

Test #6:

score: 7
Accepted
time: 305ms
memory: 32568kb

input:

500 500
yyyayyayyyayyayyyayyayyyayyyayyyayyayyyyayyayyyayyyayyayyyayyyayyayyayyyyayyyayyyayyayyayyyayyayyayyyayyayyayyyayyyyyayyayyyyayyayyyayyayyyayyyayyyayyayyyayyayyyyayyyayyayyayyyayyayyyayyyyayyyyayyayyyayyayyayyyayyayyayyyyayyayyyayyayyayyyayyyyayyayyayyyayyayyayyyayyayyayyyyayyyayyyayyyyayyay...

output:

6159560444195180556
5294852391541430076
6195718271241091926
7959984071139675340
1598729415848168155
4879964117998052348
2279721248493220290
2026655128556749470
9803272548967597498
1028236064772678471
5410852487707111065
3600180224455323043
60239358603452318
2179897463397058094
16626503365867372202
3...

result:

ok 500 lines

Test #7:

score: 7
Accepted
time: 681ms
memory: 34556kb

input:

500 500
fffffffffffxfqifffnmogfffxfqiffffffffxfqifffnmogfffxfqiffffffxfqifffnmogfffxfqifffffffffffxfqifffnmogfffxfqiffffffffxfqifffnmogfffxfqifffffffffffffxfqifffnmogfffxfqiffffffffxfqifffnmogfffxfqifffffffffxfqifffnmogfffxfqiffffffffxfqifffnmogfffxfqiffffffffxfqifffnmogfffxfqiffffffffxfqifffnmogfff...

output:

6263422992304461664
10533199195660359295
11930245273187149005
380050211417129795
8399013088311259527
7005867409130681392
6872331929648615383
11661502418569897193
18027795221888639599
8932010711134684820
6331436398298306214
14599171184201697655
16632037523890780117
10373998601812781913
16089838760431...

result:

ok 500 lines

Subtask #2:

score: 0
Time Limit Exceeded

Dependency #1:

100%
Accepted

Test #8:

score: 0
Time Limit Exceeded

input:

5000 5000
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:


result:


Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 0
Time Limit Exceeded

Test #22:

score: 0
Time Limit Exceeded

input:

100000 100000
zbbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbabaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaababbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbabaababbaabbabaababbabaabbaabab...

output:


result:


Subtask #5:

score: 0
Skipped

Dependency #4:

0%

Subtask #6:

score: 0
Skipped

Dependency #2:

0%

Subtask #7:

score: 0
Skipped

Dependency #3:

0%