[BZOJ 3211]花神游历各国(并查集+树状数组)

Description

Solution

树状数组单点修改区间查询

我们知道一个数n最多修改loglogn次就会变为1

并查集维护每个数右边第一个不为1的位置

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cmath>
#define MAXN 100005
using namespace std;
typedef long long LL; 
int n,m,delta[MAXN],father[MAXN];
LL c[MAXN]; 
int read()
{
    int x=0,f=1;char c=getchar();
    while(c<'0'||c>'9'){
        if(c=='-')f=-1;c=getchar();
    }
    while(c>='0'&&c<='9'){
        x=x*10+c-'0';c=getchar();
    }
    return x*f;
}
int lowbit(int x){return x&-x;}
void add(int pos,int x)
{
    while(pos<=n)
    {
        c[pos]+=x;
        pos+=lowbit(pos);
    }
}
LL query(int pos)
{
    LL res=0;
    while(pos>0)
    {
        res+=c[pos];
        pos-=lowbit(pos);
    }
    return res;
}
int find(int x)
{
    if(x==father[x])return x;
    father[x]=find(father[x]);
    return father[x];
}
int main()
{
    n=read();
    for(int i=1;i<=n;i++)
    {
        delta[i]=read();
        add(i,delta[i]);
        father[i]=i;
    }
    m=read();
    for(int i=1;i<=m;i++)
    {
        int x=read(),l=read(),r=read();
        if(x==1)
        printf("%lld\n",query(r)-query(l-1));
        else if(x==2)
        {
            for(int j=l;j<=r&&j;j=find(j+1))
            {
                add(j,(int)sqrt(delta[j])-delta[j]);
                delta[j]=(int)sqrt(delta[j]);
                if(delta[j]<=1)father[j]=j+1;
            }
        }
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/Zars19/p/6871921.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值