zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    vue单页面项目返回上一页无效,链接变化了,但是页面没有变化
    H5页面在iOS网页中的数字被识别为电话号码,字体颜色变黑色,且颜色不可改变
    vue-quill-editor 封装成组件;图片文件流上传;同一页面多个编辑器样式异常解决办法
    React 从零搭建项目 使用 create-react-app脚手架
    使用git pull文件时和本地文件冲突怎么办?
    webpack打包vue项目,资源路径如何从绝对路径改为相对路径?css中的图片资源如何修改配置?
    前端使用JS-SDK上传图片(文件)到七牛
    vue图片裁剪插件vue-cropper
    小程序图片裁剪组件
    微信小程序1rpx border ios真机显示不全问题
  • 原文地址:https://www.cnblogs.com/Rhythm-/p/9375066.html
Copyright © 2011-2022 走看看