2019ICPC徐州网络赛

H

在这里插入图片描述

#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
#define ll long long
#define M(x) ((x)%mod)

const ll mod =998244353;
const ll inv2=(mod+1)/2;
const int maxn=2e5+10;
int P[maxn],cnt;bool vis[maxn];

ll n;int sqr;
ll g[2][maxn<<1],h[2][maxn];
int id1[maxn],id2[maxn],m;ll w[maxn<<1];

inline int ind(ll x){
    return x<=sqr?id1[x]:id2[n/x];
}

void init(){
    
    sqr=sqrt(n);
    
    for(int i=2;i<=sqr;i++){
        if(!vis[i]){
            P[++cnt]=i;
            h[0][cnt]=h[0][cnt-1]+1;
            h[1][cnt]=M(h[1][cnt-1]+i);
        }
        for(int j=1;j<=cnt&&P[j]*i<=sqr;j++){
            int now=i*P[j];vis[now]=1;
            if(i%P[j]==0)break;
        }
    }

    //cout<<sqr<<endl;

    for(ll i=1,j;i<=n;i=j+1){
        //cout<<i<<" "<<j<<endl;
        j=n/(n/i);w[++m]=n/i;
        if(w[m]<=sqr)id1[w[m]]=m;
        else id2[n/w[m]]=m;
        
        g[0][m]=(w[m]-1)%mod;
        g[1][m]=M(M(M(M(w[m]+1)*M(w[m]))*inv2)-1);
    }


    for(int j=1;j<=cnt;j++){
        for(int i=1;i<=m&&1ll*P[j]*P[j]<=w[i];i++){
            g[0][i]=g[0][i]-(g[0][ind(w[i]/P[j])]-h[0][j-1]);
            g[1][i]=(g[1][i]-M(P[j]*(g[1][ind(w[i]/P[j])]-h[1][j-1])))%mod;
        }
    }

}

ll tmp[2][maxn];
ll v[maxn];

ll get(ll x,int id){
    if(x==0)return 0;
    //cout<<"cal "<<x<<" "<<id<<" "<<g[id][ind(x)]<<" "<<tmp[id][m+1-ind(x)]<<endl;
    return g[id][ind(x)]+tmp[id][m+1-ind(x)];
}

int main(){
    cin>>n;
    init();
    ll ans=0;
    int tot=0;
    for(ll i=1,j;i<=n;i=j+1){
        j=n/(n/i);
        v[++tot]=j;
    };
    //for(int i=1;i<=m;i++)cout<<v[i]<<" ";cout<<endl;
    for(int j=1;j<=cnt;j++){
        ll now=P[j];
        for(int k=1;k<=64;k++){
            now*=P[j];
            if(now>n)break;
            //cout<<P[j]<<" "<<k<<" "<<now<<endl;
            int ind=lower_bound(v+1,v+tot+1,now)-v;
            //cout<<ind<<endl;
            tmp[0][ind]++;
            tmp[1][ind]=(tmp[1][ind]+now)%mod;
        }
    }

//    for(int i=1;i<=m;i++)cout<<tmp[0][i]<<" "<<tmp[1][i]<<endl;
  //  for(int i=1;i<=m;i++)cout<<w[i]<<endl;
    //for(int i=1;i<=m;i++)cout<<g[0][i]<<" "<<g[1][i]<<endl;

    for(int i=1;i<=m;i++)tmp[0][i]+=tmp[0][i-1],tmp[1][i]=(tmp[1][i]+tmp[1][i-1])%mod;

    for(ll i=1,j;i<=n;i=j+1){
        j=n/(n/i);ll t=n/i%mod;

        //cout<<get(j,0)<<" "<<get(i-1,0)<<endl;
        //cout<<i<<" "<<j<<" "<<get(j,0)-get(i-1,0)<<" "<<(get(j,1)-get(i-1,1))<<endl;

        ans+=t*((n+1)%mod)%mod*(get(j,0)-get(i-1,0))%mod-t*(t+1)/2%mod*(get(j,1)-get(i-1,1))%mod;
    }
    cout<<(ans%mod+mod)%mod<<endl;
}

I

在这里插入图片描述

#include<iostream>
#include<algorithm>
using namespace std;

int h[100010],n,m;
#define lowb(x) ((x)&(-x))
int que(int x) {
	int ret = 0;
	for (; x >= 1; x -= lowb(x))ret += h[x];
	return ret;
}
void upd(int x,int y) {
	for (; x <= n; x += lowb(x))h[x] += y;
}

struct nod {
	int x, y, tp, id;
	bool operator <(const nod &a) const {
		return x < a.x || x == a.x&&y < a.y || x == a.x&&y == a.y && tp == 0;
	}
}e[5000010];
int ans[100010],a[100010],pos[100010];

int main() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		cin >> a[i]; pos[a[i]] = i;
	}
	int tot = 0;
	for (int i = 1; i <= n; i++) {
		for (int j = 2; i*j <= n; j++) {
			e[++tot] = { pos[i],pos[i*j],0 };
		}
	}
	for (int i = 1; i <= m; i++) {
		int l, r; cin >> l >> r;
		e[++tot] = { l - 1,l - 1,1,i };
		e[++tot] = { l - 1,r,-1,i };
		e[++tot] = { r,l - 1,-1,i };
		e[++tot] = { r,r,1,i };
	}
	sort(e + 1, e + tot + 1);
	for (int i = 1; i <= tot; i++) {
		if (e[i].tp == 0) {
			upd(e[i].y, 1);
		}
		else {
			int tmp = que(e[i].y);
			ans[e[i].id] += e[i].tp*tmp;
		}
	}
	for (int i = 1; i <= m; i++)cout << ans[i] << endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值