CodeChef JUNE17 - Chef and Prime Queries

题意

给定包含 N 个数的序列 a,求序列 [L,R] 内的数,以范围在 [x,y] 内的质数为底的指数之和!包含Q个询问。
N105 , x,y106

思路

序列 a 所有数的质数表示为 pk11...pknn,每个质数都对答案都会产生一个贡献数组,那么问题就转化为一个经典的模型:二维矩形求和 or 扫描线的问题

普遍的套路就是:
确定一维 y 轴,建能够快速求和的数据结构(线段树、树状数组之类的);
另一维x 轴作为扫描线,从左往右扫描,同时遇到对 y 轴有贡献的值,去更新数据结构。同时查询对应离线的记录值。

所以这里思路就是:
以质数轴为 y 轴,建树状数组,用于区间快速求和。
以数组下标为 x 轴,从左往右扫描,每次遇到数组的值 a[i],因为已知了其质数表示 pk11...pknn ,所以可以同时去单点更新树状数组,然后回答与该下标值 i <script type="math/tex" id="MathJax-Element-1543">i</script>有关的询问!

代码

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long int llu;
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define mem(a, v) memset(a, v, sizeof(a))
#define PI acos(-1)
#define S(a) scanf("%d",&a)
#define SL(a) scanf("%lld",&a)
#define S2(a, b) scanf("%d%d",&a,&b)
#define nl printf("\n")
#define deb(x) cout<<#x<<" : "<<x<<endl;
#define deb2(x, y) cout<<#x<<" : "<<x<<" | "<<#y<<" : "<<y<<endl;
#define deb3(x, y, z) cout<<#x<<" : "<<x<<" | "<<#y<<" : "<<y<<" | "<<#z<<" : "<<z<<endl;
#define debv(x) {cout<<#x<<" : "<<endl; for(int ii =0; ii < x.size(); ii++) cout<<x[ii]<<" "; cout<<endl; }
#define debarr(x, xs) {cout<<#x<<" : "<<endl; for(int ii =0; ii < xs; ii++) cout<<x[ii]<<" "; cout<<endl; }
//auto T=clock(); 
//cout<<double(clock()-T)/CLOCKS_PER_SEC<<'\n';
//cout << fixed << setprecision(10) << f(0, 0, 0) << "\n";
const ll mod = 1000000007LL;
const int lmt = 1000005;

int pf[lmt]; 
bool isP[lmt];

#define gc getchar_unlocked
void inp(int &x) {
    register int c = gc();
    x = 0;
    int neg = 0;
    for(;((c<48 || c>57) && c != '-');c = gc());
    if(c=='-') {neg=1;c=gc();}
    for(;c>47 && c<58;c = gc()) {x = (x<<1) + (x<<3) + c - 48;}
    if(neg) x=-x;
}

void sieve() 
{
    int cnt = 0;
    int n = 1000000;
    mem(isP, true);
    isP[0] = isP[1] = false;
    for(int i = 2; i <= n; i++) 
    {
        if(!isP[i]) continue;
        pf[i] = i;
        cnt++;
        for(int j = 2*i; j <= n; j+=i) 
        {
            isP[j] = false;
            pf[j] = i;
        }
    }
}
struct data 
{
    int x, y;
    int lans, rans;
    data(int c, int d) 
    {
        x = c; y = d;
        lans = 0; rans = 0;
    }
};
vector<data> query;
vector<int> L[100005];
vector<int> R[100005];
int bit[lmt];
int a[100005];

void update(int i, int val) 
{
    for(; i < lmt; i += (i&(-i)))
        bit[i] += val;
}

int get(int i) 
{
    int ans = 0;
    for(; i > 0; i -= (i&(-i)))
        ans += bit[i];
    return ans;
}

void add(int n) 
{
    while(n > 1) 
    {
        int cnt = 0, d = pf[n];
        while(n % d == 0) 
        {
            cnt++;
            n /= d;
        }
        update(d, cnt);
    }
}

int main()
{
    sieve();

    int n;
    inp(n);

    for(int i = 1; i <= n; i++)
        inp(a[i]);

    int Q;
    inp(Q);

    for(int i = 0; i < Q; i++) 
    {
        int l, r, x, y;
        inp(l); inp(r); inp(x); inp(y);

        l --;
        query.pb(data(x, y));
        L[l].pb(i);
        R[r].pb(i);
    }

    for(int i = 0; i <= n; i++) 
    {
        if(i > 0) add(a[i]);
        for(int j = 0; j < L[i].size(); j++) 
        {
            int q = L[i][j];
            query[q].lans = get(query[q].y) - get(query[q].x - 1);
        }

        for(int j = 0; j < R[i].size(); j++) 
        {
            int q = R[i][j];
            query[q].rans = get(query[q].y) - get(query[q].x - 1);
        }
    }

    for(int i = 0; i < Q; i++) 
        printf("%d\n", (query[i].rans - query[i].lans));
    return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值