QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#767532#9748. 最大公因数的平方和wtcRE 0ms0kbC++141.3kb2024-11-20 21:12:132024-11-20 21:12:13

Judging History

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

  • [2024-11-20 21:12:13]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-11-20 21:12:13]
  • 提交

answer

#include<bits/stdc++.h>
#define fo(i,l,r) for(int i=(l);i<=(r);++i)
#define fd(i,l,r) for(int i=(l);i>=(r);--i)
#define fu(i,l,r) for(int i=(l);i<(r);++i)
#define ll long long
#define int ll
using namespace std;
const int N=100007,B=1000;
int n,Q,a[N],b[N],c[N],d[N],sum[N],bz[N];
ll f[N],g[N],ans[N];
vector<int>p[N];
struct Qry{
	int l,r,bh,c;
};
vector<Qry>q[N];
void upd(int x,ll y){while(x<=n)g[x]+=y,x+=x&-x;}
ll qry(int x){ll s=0;while(x)s+=g[x],x-=x&-x;return s;}
void init(int n)
{
	fo(i,1,n) f[i]=1ll*i*i;
	fo(i,2,n)
		if(!bz[i])
		{
			for(int j=n/i*i;j>=i;j-=i) f[j]-=f[j/i],bz[j]=1;
		}
}
signed main()
{
	scanf("%d",&n);
	init(n);
	fo(i,1,n) scanf("%d",&a[i]),c[a[i]]=i;
	fo(i,1,n) scanf("%d",&b[i]),d[b[i]]=i;
	fo(i,B+1,n)
	{
		for(int j=i;j<=n;j+=i)
			p[c[j]].push_back(i);
	}
	scanf("%d",&Q);
	fo(i,1,Q)
	{
		int l,r,u,v;
		scanf("%d%d%d%d",&l,&r,&u,&v);
		q[r].push_back((Qry){u,v,i,1});
		q[l-1].push_back((Qry){u,v,i,-1});
	}
	fo(j,1,B)
	{
		fo(i,1,n) sum[i]=sum[i-1]+(b[i]%j==0);
		int w=0;
		fo(i,1,n)
		{
			w+=(a[i]%j==0);
			for(Qry x:q[i]) ans[x.bh]+=1ll*(sum[x.r]-sum[x.l-1])*w*f[j]*x.c;
		}
	}
	fo(i,1,n)
	{
		for(int x:p[i])
		{
			for(int y=x;y<=n;y+=x) upd(d[y],f[x]);
		}
		for(Qry x:q[i]) ans[x.bh]+=(qry(x.r)-qry(x.l-1))*x.c;
	}
	fo(i,1,Q) printf("%lld\n",ans[i]);
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Runtime Error

input:

5
4 1 5 3 2
1 2 3 4 5
5
3 3 2 3
3 4 2 4
3 4 3 4
5 5 1 1
1 1 2 2

output:


result: