牛客 小乐乐学数学(扫描线+树状数组)

简介: 牛客 小乐乐学数学(扫描线+树状数组)

杭电题目链接

牛客题目链接

思路:

还是借助扫描线+树状数组的思想,将询问离线后枚举右端点,考虑合法的数的个数。

当枚举到右端点x时,什么时候a [ x ]才对答案有贡献呢,即a [ x ]怎么样才与区间里的所有数都互质。比较朴素的思路就是O ( n 2 )枚举一遍前面的数判断是否互质。可以优化着想,将这个数进行质因子分解,假设他的质因子为x 1 , x 2 , x 3,如果这些质因子上一次出现位置的最大值大于区间的左端点l ll的话,这个数就是一个合法的数。

也就是说,我们需要对每个数进行质因子分解,同时记录每个质因子上一次出现的位置,看最大值是否大于区间的左端点。这样看来,假设上一次出现位置的最大值为m a x x,那么对于右端点x来说,只要左端点在[ m a x x + 1 , x − 1 ]的区间这个数都会产生贡献。

当前缀和数组为[ − 1 , − 1 , − 1 , − 1 … … , 0 ] , i < = m a x x < =x时,对于l ϵ [ m a x x + 1 , r − 1 ],s u m [ r ] − s u m [ l ] = 1,都说明维护的贡献是正确的,差分后得应该对m a x x修改为− 1,对x修改为1.也就是代码里的:

update(i,1);update(maxx,-1);

增加贡献说完了,接下来看怎么消除贡献。

先假设l a s [ x ] = m a x x,表示上一次增加的贡献的位置。

首先要将上一次相同的质因子的贡献消除,如果该质因子所在位置上的数还没有被消除过贡献,那么这个贡献为1,也就是应当− 1;

然后,对于上一个合法的位置,由于这个位置已经不再合法,上一次对该位置− 1,这次应该+ 1,让他变为0

比如[ 2 , 1 , 4 , 1 , 6 , 8 ],只考虑质因子为2的情况,

当枚举到r = 5时,差分序列为[ − 1 , 0 , 1 , 0 , 0 , 0 , 0 ].先将上一次的贡献消除,也就是让下标为1的位置+ 1,因为在上一次操作中他是增加贡献的位置,当时对他进行了− 1的操作;让下标为3的位置− 1,因为在上一次操作中,当时进行了+ 1的操作。之后再维护增加贡献的地方。

大概~多画几组样例就懂了吧。

具体实现:

1.先用筛法找到每个数的最小质因子

2.将所有询问离线存起来

3.枚举右端点,对该点的值进行质因子分解后,计算每个质因子的贡献,注意要消除上一次的贡献。

代码:

// Problem: 小乐乐学数学
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/problem/21568
// Memory Limit: 524288 MB
// Time Limit: 4000 ms
// Author:Cutele
// 
// Powered by CP Editor (https://cpeditor.org)
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>PLL;
typedef pair<int, int>PII;
typedef pair<double, double>PDD;
#define I_int ll
inline ll read(){ll x = 0, f = 1;char ch = getchar();while(ch < '0' || ch > '9'){if(ch == '-')f = -1;ch = getchar();}while(ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}return x * f;}
inline void write(ll x){if (x < 0) x = ~x + 1, putchar('-');if (x > 9) write(x / 10);putchar(x % 10 + '0');}
#define read read()
#define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define multiCase int T;cin>>T;for(int t=1;t<=T;t++)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i<(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define perr(i,a,b) for(int i=(a);i>(b);i--)
ll ksm(ll a, ll b,ll mod){ll res = 1;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;}
const int maxn=1e5+7;
int n,m,a[maxn],ans[maxn];
vector<PII>q[maxn];
int prim[maxn],idx,vis[maxn],min_pri[maxn];
void init(){
    //min_pri[1]=1;
    for(int i=2;i<=1e5;i++){
        if(!vis[i]){
            prim[++idx]=i;
            min_pri[i]=i;
            for(int j=i;j<=1e5;j+=i){
                vis[j]=1;
                if(!min_pri[j]) min_pri[j]=i;
            }
        }
    }
}
int pos[maxn],las[maxn],tr[maxn],st[maxn];
int lowbit(int x){return x&-x;}
void update(int pos,int val){
  if(pos==0) return ;
    while(pos<=n){
        tr[pos]+=val;
        pos+=lowbit(pos);
    }    
}
int query(int pos){
    int res=0;
    while(pos){
        res+=tr[pos];
        pos-=lowbit(pos);
    }
    return res;
}
int main(){
    init();
   // rep(i,1,10) cout<<min_pri[i]<<" ";
  while(~scanf("%d%d",&n,&m)){
        rep(i,1,n) a[i]=read,las[i]=pos[i]=st[i]=ans[i]=tr[i]=0;
        rep(i,1,m){
            int l=read,r=read;
            q[r].push_back({l,i});
        }
        rep(i,1,n){
            vector<int>v;
            int x=a[i];
            while(x>1){
                v.push_back(min_pri[x]);
                int tt=min_pri[x];
                x/=tt;
            }
            int maxx=0;
            for(auto t:v) maxx=max(maxx,pos[t]);
            for(auto t:v){
                if(!st[pos[t]]){
                    update(pos[t],-1);
                    if(las[pos[t]]) 
                        update(las[pos[t]],1);
                    st[pos[t]]=1;
                }
            }
            for(auto t:v) pos[t]=i;
            las[i]=maxx;
            update(i,1);
            update(maxx,-1);
            for(auto t:q[i])
                ans[t.second]=query(i)-query(t.first-1);
            q[i].clear();
        }
        rep(i,1,m) printf("%d\n",ans[i]);
    }
  return 0;
}
目录
相关文章
|
9月前
|
C语言
快速幂+高精乘(填坑)洛谷1226+1045
快速幂+高精乘(填坑)洛谷1226+1045
|
9月前
|
C语言
c语言编程练习题:7-22 用天平找小球
c语言编程练习题:7-22 用天平找小球
86 0
|
算法 测试技术 C++
C++算法:美丽塔O(n)解法单调栈
C++算法:美丽塔O(n)解法单调栈
宝藏例题(欧几里得算法+素数的三种境界………)
宝藏例题(欧几里得算法+素数的三种境界………)
宝藏例题(欧几里得算法+素数的三种境界………)
|
9月前
|
C++ 存储
力扣C++|一题多解之数学题专场(1)
力扣C++|一题多解之数学题专场(1)
68 0
力扣C++|一题多解之数学题专场(1)
|
9月前
|
C++ 存储 Serverless
力扣C++|一题多解之数学题专场(2)
力扣C++|一题多解之数学题专场(2)
69 0
力扣C++|一题多解之数学题专场(2)
|
C++
【LeetCode343】剪绳子(动态规划)
(1)确定状态 dp[i]是将正整数i拆成2个及其以上的正整数后,求所有数的乘积值。
155 0
【LeetCode343】剪绳子(动态规划)
【洛谷算法题】B2029-大象喝水【入门1顺序结构】
【洛谷算法题】B2029-大象喝水【入门1顺序结构】
|
存储
剑指Offer - 面试题14:剪绳子
剑指Offer - 面试题14:剪绳子
104 0