HDU6315 Naive Operations 线段树 区间更新

本文介绍了一道关于序列操作的问题,并提供了详细的解题思路与代码实现。通过对序列进行加法和查询操作,利用线段树优化算法,实现了高效的求解。

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

Naive Operations
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 502768/502768 K (Java/Others)
Total Submission(s): 266 Accepted Submission(s): 69

Problem Description
In a galaxy far, far away, there are two integer sequence a and b of length n.
b is a static permutation of 1 to n. Initially a is filled with zeroes.
There are two kind of operations:
1. add l r: add one for al,al+1…ar
2. query l r: query ∑ri=l⌊ai/bi⌋

Input
There are multiple test cases, please read till the end of input file.
For each test case, in the first line, two integers n,q, representing the length of a,b and the number of queries.
In the second line, n integers separated by spaces, representing permutation b.
In the following q lines, each line is either in the form ‘add l r’ or ‘query l r’, representing an operation.
1≤n,q≤100000, 1≤l≤r≤n, there’re no more than 5 test cases.

Output
Output the answer for each ‘query’, each one line.

Sample Input
5 12
1 5 2 4 3
add 1 4
query 1 4
add 2 5
query 2 5
add 3 5
query 1 5
add 2 4
query 1 4
add 2 5
query 2 5
add 2 2
query 1 5

Sample Output
1
1
2
4
4
6

题意 给出序列a,b l-r的权值为 ∑ri=l⌊ai/bi⌋ 给出两种操作。 按要求输出答案。

解题思路:
这是一道简单题。
因为b是1-n的全排列
所以对于每组数据 最多有nlogn次单点更新。
知道这个后,就很好写了。。。。。。。
总复杂度nlognlogn
标程是可持久化分块。。。。 剧毒。

#include <algorithm>
#include <cstdio>
#include <cstring>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
typedef long long LL;
const int MAX=1e5+10;
int b[MAX];
int max1[MAX<<2];
int add1[MAX<<2];
int sum[MAX<<2];
void push_up1(int rt){
    max1[rt]=max(max1[rt<<1],max1[rt<<1|1]);
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void push_dwon(int rt){
    if(add1[rt]){
        add1[rt<<1|1]+=add1[rt];
        add1[rt<<1]+=add1[rt];
        max1[rt<<1]+=add1[rt];
        max1[rt<<1|1]+=add1[rt];
        add1[rt]=0;
    }
    return;
}
void build1(int l,int r,int rt){
    add1[rt]=0;
    max1[rt]=-1e9;
    sum[rt]=0;
    if(l==r){
        add1[rt]=0;
        max1[rt]=-b[l];
        return;
    }
    int m=(l+r)>>1;
    build1(lson);
    build1(rson);
    push_up1(rt);
}
void update1(int L,int R,int l,int r,int rt){
    if(L<=l && r<=R){
        max1[rt]++;
        add1[rt]++;
        return;
    }
    push_dwon(rt);
    int m=(l+r)>>1;
    if(m>=L)
        update1(L,R,lson);
    if(m<R)
        update1(L,R,rson);
    push_up1(rt);
}
void update2(int L,int R,int l,int r,int rt){
    if(l==r){
        while(max1[rt]>=0){
            sum[rt]++;
            max1[rt]-=b[l];
        }
        return;
    }
    push_dwon(rt);
    int m=(l+r)>>1;
    if(m>=L && max1[rt<<1]>=0)
        update2(L,R,lson);
    if(m<R && max1[rt<<1|1]>=0)
        update2(L,R,rson);
    push_up1(rt);
}
int query(int L,int R,int l,int r,int rt){
    if(L<=l && r<=R){
        return sum[rt];
    }
    push_dwon(rt);
    int m=(l+r)>>1;
    int ans=0;
    if(m>=L)
        ans+=query(L,R,lson);
    if(m<R)
        ans+=query(L,R,rson);
    return ans;
}
int main(){
    int n,q;
    while(~scanf("%d %d",&n,&q)){
        for(int i=1;i<=n;i++){
            scanf("%d",&b[i]);
        }
        build1(1,n,1);
        char s[10];
        int l,r;
        while(q--){
            scanf("%s",s);
            if(s[0]=='a'){
                scanf("%d %d",&l,&r);
                update1(l,r,1,n,1);
                update2(l,r,1,n,1);
            }else{
                scanf("%d %d",&l,&r);
                printf("%d\n",query(l,r,1,n,1));
            }
        }
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值