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
  • 相关阅读:
    0.1+0.2==0.3?
    Tomcat 初探(三)多项目部署
    Tomcat 初探(二) server.xml 配置
    Tomcat 初探(一) 简介
    如何使用 Idea 远程调试 Java 代码
    js-将时间戳转为正常的日期
    VUE-基本的写法
    HTML-列表无缝滚动效果2(显示一个暂停3秒,滚走显示下一个)
    JS-判断用户使用的终端是安卓还是IOS
    JS-在本页面监听是否用户是点击了返回进入的(IOS返回页面时,页面未初始化)
  • 原文地址:https://www.cnblogs.com/Rhythm-/p/9375066.html
Copyright © 2011-2022 走看看