【HDU 6315 Naive Operations 】多校线段树补题

本文介绍了一种解决HDU6315问题的方法,该问题涉及到数组操作及查询,通过使用树状数组和线段树来优化查询效率,实现对区间内元素特定条件的快速更新和求和。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

HDU 6315
题意
a数组长度为n 初始值为 0
给你n长度的b数组
add 操作每次给a数组加 1
query操作 询问 i 从 l 到 r 之间的 a[i]/b[i] 的和
我们想想看 用一个最小值数组 一开始为 b[i] 值 代表要几次删到 0
那么我每次只要一个区间的 minn > 1 那么我可以区间更新
否则就单点更新
复杂度最坏是 (1-n)中每个数的约数个数和
相当于加几次能进行一个操作
外面再用个树状数组记录答案即可

/*
    if you can't see the repay
    Why not just work step by step
    rubbish is relaxed
    to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;

#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))

typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod =  (int)1e9+7;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);

const int MAX_N = 100025;
int arr[MAX_N];
ll C[MAX_N<<1];
void add(int x,int v)
{
    for(;x<MAX_N;x+=x&(-x))
        C[x]+=v;
}
ll getsum(int x)
{
    ll res = 0;
    for(;x;x-=x&(-x))
        res+=C[x];
    return res;
}
int Min(int a,int b) {return a < b?a:b;}
namespace sgt
{
    #define mid ((l+r)>>1)
    int minn[MAX_N<<2],col[MAX_N<<2];
    void up(int rt)
    {
        minn[rt] = Min(minn[rt<<1],minn[rt<<1|1]);
    }
    void down(int rt,int l,int r)
    {
        if(col[rt])
        {
            col[rt<<1]+=col[rt];
            col[rt<<1|1] += col[rt];
            minn[rt<<1] += col[rt];
            minn[rt<<1|1] += col[rt];
            col[rt] = 0;
        }
    }
    void build(int rt,int l,int r)
    {
        col[rt] = 0;
        if(l==r)
        {
            minn[rt] = arr[l];
            return ;
        }
        build(rt<<1,l,mid);
        build(rt<<1|1,mid+1,r);
        up(rt);
    }
    void update(int rt,int l,int r,int x,int y,int v)
    {
        if(x<=l&&r<=y && minn[rt]>1)
        {
            minn[rt]--;
            col[rt] += v;
            return ;
        }
        if(l==r)
        {
            if(minn[rt]==1)
            {
            col[rt] = 0;
            minn[rt] = arr[l];
            add(l+1,1);
            }
            else minn[rt]--;
            return ;
        }
        down(rt,l,r);
        if(x>mid) update(rt<<1|1,mid+1,r,x,y,v);
        else if(y<=mid) update(rt<<1,l,mid,x,y,v);
        else update(rt<<1,l,mid,x,y,v),update(rt<<1|1,mid+1,r,x,y,v);
        up(rt);
    }
    #undef mid
}

int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    int n,m,x,y;
    char str[10];
    while(scanf("%d%d",&n,&m)==2)
    {
        memset(C,0,sizeof(C));
        for(int i = 1;i<=n;++i) scanf("%d",&arr[i]);
        sgt::build(1,1,n);
        while(m--)
        {
            scanf("%s%d%d",str,&x,&y);
            if(str[0]=='a') sgt::update(1,1,n,x,y,-1);
            else printf("%d\n",getsum(y+1) - getsum(x));
        }
    }
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值