HDU 6315: Naive Operations

本文解析了一道关于线段树的数据结构题目,通过维护区间内的最大值和最小值来解决加法和查询操作的问题。提供了完整的代码实现,并附带了解题思路。

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

Naive Operations

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 502768/502768 K (Java/Others)
Total Submission(s): 1791    Accepted Submission(s): 772


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 $a_l,a_{l+1}...a_r$
2. query l r: query $\sum_{i=l}^r \lfloor a_i / b_i \rfloor$
 

 

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 \leq n,q \leq 100000$, $1 \leq l \leq r \leq 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
 

分析:线段树模板改一改,维护最大值最小值就好了。 

 

#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>
#include <map>
#define range(i,a,b) for(auto i=a;i<=b;++i)
#define LL long long
#define itrange(i,a,b) for(auto i=a;i!=b;++i)
#define rerange(i,a,b) for(auto i=a;i>=b;--i)
#define fill(arr,tmp) memset(arr,tmp,sizeof(arr))
using namespace std;
int b[int(1e5+5)],n,q;
template <class T>
class segtree{
private:
    T *add,*cnt,*minb,*maxa;
    void pushup(int rt){
        minb[rt]=min(minb[rt<<1],minb[rt<<1|1]);
        cnt[rt]=cnt[rt<<1]+cnt[rt<<1|1];
        maxa[rt]=max(maxa[rt<<1],maxa[rt<<1|1]);
    }
    void pushdown(int rt){
        if(add[rt]){
            int v=add[rt];
            add[rt]=0;
            maxa[rt<<1]+=v;
            maxa[rt<<1|1]+=v;
            add[rt<<1]+=v;
            add[rt<<1|1]+=v;
        }
    }
public:
    explicit segtree(int len=int(1e5+5)){
        add=new T[len<<2];fill(add,0);
        cnt=new T[len<<2];fill(cnt,0);
        minb=new T[len<<2];fill(minb,0);
        maxa=new T[len<<2];fill(maxa,0);
    }
    void build(int l,int r,int rt){
        add[rt]=0;
        if(l==r){
            cnt[rt]=maxa[rt]=0;
            minb[rt]=b[l];
            return;
        }
        int m=(l+r)>>1;
        build(l,m,rt<<1);
        build(m+1,r,rt<<1|1);
        pushup(rt);
    }
    void update(int L,int R,T c,int l,int r,int rt){
        if(L<=l&&r<=R){
            maxa[rt]++;
            if(maxa[rt]<minb[rt]){
                ++add[rt];
                return;
            }
            if(l==r&&maxa[rt]>=minb[rt]){
                ++cnt[rt];
                minb[rt]+=b[l];
                return;
            }
        }
        pushdown(rt);
        int m=(l+r)>>1;
        if(L<=m)update(L,R,0,l,m,rt<<1);
        if(m<R)update(L,R,0,m+1,r,rt<<1|1);
        pushup(rt);
    }
    T query(int L,int R,int l,int r,int rt){
        if(L<=l&&r<=R)return cnt[rt];
        int m=(l+r)>>1;
        pushdown(rt);
        T ret=0;
        if(L<=m)ret+=query(L,R,l,m,rt<<1);
        if(m<R)ret+=query(L,R,m+1,r,rt<<1|1);
        return ret;
    }
};
segtree<int>tree;
void init(){}
void solve(){
    while(cin>>n>>q){
        range(i,1,n)scanf("%d",b+i);
        tree.build(1,n,1);
        char op[6];int l,r;
        while(q--){
            scanf("%s%d%d",op,&l,&r);
            if(op[0]=='a')tree.update(l,r,0,1,n,1);
            else printf("%d\n",tree.query(l,r,1,n,1));
        }
    }
}
int main() {
    init();
    solve();
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/Rhythm-/p/9375066.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值