The sum of gcd HDU - 5381

http://acm.hdu.edu.cn/showproblem.php?pid=5381

线段树套gcd的题好多啊 感觉年年区域赛或多校都有

查询区间gcd之和 无修改 还是先把以每个点为端点的小区间都找出来 然后离线处理 把查询按右端点排序 然后把所有小于等于右端点的点上的小区间都更新到线段树里 区间更新区间查询即可

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

struct node1
{
    int pos;
    int val;
};

struct node2
{
    int id;
    int l;
    int r;
};

vector <node1> pre[10010];
node2 order[10010];
ll sum[40010],laz[40010],ans[10010];
int ary[10010],gou[40010],root[10010];
int n,q,num;

bool cmp(node2 n1,node2 n2)
{
    return n1.r<n2.r;
}

int getgcd(int a,int b)
{
    int t;
    while(b>0)
    {
        t=b;
        b=a%b;
        a=t;
    }
    return a;
}

void pushupI(int cur)
{
    gou[cur]=getgcd(gou[2*cur],gou[2*cur+1]);
}

void buildI(int l,int r,int cur)
{
    int m;
    if(l==r)
    {
        gou[cur]=ary[l];
        return;
    }
    m=(l+r)/2;
    buildI(l,m,2*cur);
    buildI(m+1,r,2*cur+1);
    pushupI(cur);
}

int queryI(int pl,int pr,int gcd,int l,int r,int cur)
{
    int res,m;
    if(pl<=l&&r<=pr&&gou[cur]%gcd==0) return 0;
    if(l==r) return l;
    res=0,m=(l+r)/2;
    if(pr>m) res=queryI(pl,pr,gcd,m+1,r,2*cur+1);
    if(res==0&&pl<=m) res=queryI(pl,pr,gcd,l,m,2*cur);
    return res;
}

void solve()
{
    node1 tmp;
    int i,j,p,gcd;
    for(i=1;i<=n;i++) pre[i].clear();
    for(i=1;i<=n;i++)
    {
        p=i,gcd=ary[i];
        while(p>=1)
        {
            p=queryI(1,p,gcd,1,n,1);
            tmp.pos=p,tmp.val=gcd;
            pre[i].push_back(tmp);
            gcd=getgcd(gcd,ary[p]);
        }
    }
}

void pushupII(int cur)
{
    sum[cur]=sum[2*cur]+sum[2*cur+1];
}

void pushdownII(int cur,int l,int r)
{
    ll len;
    int m;
    if(laz[cur]!=0)
    {
        m=(l+r)/2;
        len=m-l+1;
        sum[2*cur]+=len*laz[cur];
        laz[2*cur]+=laz[cur];
        len=r-m;
        sum[2*cur+1]+=len*laz[cur];
        laz[2*cur+1]+=laz[cur];
        laz[cur]=0;
    }
}

void updateII(int pl,int pr,ll val,int l,int r,int cur)
{
    ll len;
    int m;
    if(pl<=l&&r<=pr)
    {
        len=r-l+1;
        sum[cur]+=len*val;
        laz[cur]+=val;
        return;
    }
    pushdownII(cur,l,r);
    m=(l+r)/2;
    if(pl<=m) updateII(pl,pr,val,l,m,2*cur);
    if(pr>m) updateII(pl,pr,val,m+1,r,2*cur+1);
    pushupII(cur);
}

ll queryII(int pl,int pr,int l,int r,int cur)
{
    ll res;
    int m;
    if(pl<=l&&r<=pr) return sum[cur];
    pushdownII(cur,l,r);
    res=0,m=(l+r)/2;
    if(pl<=m) res+=queryII(pl,pr,l,m,2*cur);
    if(pr>m) res+=queryII(pl,pr,m+1,r,2*cur+1);
    return res;
}

int main()
{
    int t,i,j,p,pos;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(i=1;i<=n;i++) scanf("%d",&ary[i]);
        buildI(1,n,1);
        solve();
        scanf("%d",&q);
        for(i=1;i<=q;i++)
        {
            order[i].id=i;
            scanf("%d%d",&order[i].l,&order[i].r);
        }
        sort(order+1,order+q+1,cmp);
        memset(sum,0,sizeof(sum));
        memset(laz,0,sizeof(laz));
        p=1;
        for(i=1;i<=q;i++)
        {
            while(p<=order[i].r)
            {
                pos=p;
                for(j=0;j<pre[p].size();j++)
                {
                    updateII(pre[p][j].pos+1,pos,pre[p][j].val,1,n,1);
                    pos=pre[p][j].pos;
                }
                p++;
            }
            ans[order[i].id]=queryII(order[i].l,order[i].r,1,n,1);
        }
        for(i=1;i<=q;i++) printf("%lld\n",ans[i]);
    }
    return 0;
}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值