HDU 6315: Naive Operations

Posted rhythm-

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 6315: Naive Operations相关的知识,希望对你有一定的参考价值。

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 floor$
 

 

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
",tree.query(l,r,1,n,1));
        }
    }
}
int main() {
    init();
    solve();
    return 0;
}
View Code

 

以上是关于HDU 6315: Naive Operations的主要内容,如果未能解决你的问题,请参考以下文章

HDU6315 Naive Operations(多校第二场1007)(线段树)

HDU-DuoXiao第二场hdu 6315 Naive Operations 线段树

[HDU6315]Naive Operations(线段树+树状数组)

HDU6315 Naive Operations(线段树 复杂度分析)

Naive Operations HDU6315 (杭电多校2G)

HDU - 6315(2018 Multi-University Training Contest 2) Naive Operations (线段树区间操作)