zoukankan      html  css  js  c++  java
  • HDU 6351 Naive Operations(线段树)

    题目:

    http://acm.hdu.edu.cn/showproblem.php?pid=6315

    Naive Operations

    Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 502768/502768 K (Java/Others)


    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=lai/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.
    1n,q1000001lrn, 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
     
    题意:给定一个初始数组b和一个初始值全部为0的数组a,每次操作可以在给定的区间(l,r)内让a[i](l=<i<=r)加一,或者查询区间区间(l,r)中a[i]/b[i](l=<i<=r)(取整)的和。
    思路:
    用线段树存放a数组,做好最小更新标记,达到则向下更新
    代码:
    #include<bits/stdc++.h>
    #define fi first
    #define se second
    #define INF 0x3f3f3f3f
    #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
    #define pqueue priority_queue
    #define NEW(a,b) memset(a,b,sizeof(a))
    const double pi=4.0*atan(1.0);
    const double e=exp(1.0);
    const int maxn=3e6+8;
    typedef long long LL;
    typedef unsigned long long ULL;
    //typedef pair<LL,LL> P;
    const LL mod=1e9+7;
    using namespace std;
    struct node{
        LL l,r,sum,g,mi;
        LL lazy;
        LL mid(){
            return (l+r)>>1;
        }
    }a[maxn];
    int b[maxn];
    void build(int l,int r,int num){
        a[num].l=l;
        a[num].r=r;
        a[num].lazy=0;
        if(l==r){
            a[num].sum=0;
            a[num].g=0;
            a[num].mi=b[l];
        }
        else{
            build(l,a[num].mid(),num<<1);
            build(a[num].mid()+1,r,(num<<1)|1);
            a[num].g=a[num<<1].g+a[(num<<1)|1].g;
            a[num].mi=min(a[num<<1].mi,a[(num<<1)|1].mi);
        }
    }
    void as(int d)
    {
        if(a[d].lazy)
        {
            a[(d<<1)].lazy+=a[d].lazy;
            a[(d<<1|1)].lazy+=a[d].lazy;
            a[(d<<1)].mi-=a[d].lazy;
            a[(d<<1|1)].mi-=a[d].lazy;
            a[d].lazy=0;
        }
    }
    LL Find(int l,int r,int num){
        if(a[num].l==l&&a[num].r==r){
            return a[num].g;
        }
        if(l>a[num].mid()){
            return Find(l,r,(num<<1)|1);
        }
        else if(r<=a[num].mid()){
            return Find(l,r,num<<1);
        }
        else{
            return Find(l,a[num].mid(),num<<1)+Find(a[num].mid()+1,r,(num<<1)|1);
        }
    }
    void add(int l,int r,int num,LL x){
        if(a[num].l==l&&a[num].r==r||x==0){
            a[num].lazy+=x;
            a[num].mi-=x;
            if(a[num].mi>0){
                return ;
            }
            else if(l!=r){
                as(num);
                add(l,a[num].mid(),num<<1,0);
                add(a[num].mid()+1,r,(num<<1)|1,0);
                a[num].mi=min(a[num<<1].mi,a[(num<<1)|1].mi);
                a[num].g=a[num<<1].g+a[(num<<1)|1].g;
                return;
            }
        }
        if(l==r&&a[num].l==l&&a[num].r==r)
        {
            if(a[num].mi<=0)
            {
                a[num].mi=a[num].lazy=0;
                a[num].mi=b[l];
                a[num].g++;
            }
            return;
        }
        as(num);
        if(l>a[num].mid()){
            add(l,r,(num<<1)|1,x);
        }
        else if(r<=a[num].mid()){
            add(l,r,num<<1,x);
        }
        else {
            add(l,a[num].mid(),num<<1,x);
            add(a[num].mid()+1,r,(num<<1)|1,x);
        }
        a[num].mi=min(a[num<<1].mi,a[(num<<1)|1].mi);
        a[num].g=a[num<<1].g+a[(num<<1)|1].g;
        //cout<<'a'<<a[num].l<<' '<<a[num].r<<' '<<a[num].g<<endl;
    }
    int main(){
        fio;
        int n,m;
        string op;
        int x,y;
        while(cin>>n>>m){
            for(int i=1;i<=n;i++){
                cin>>b[i];
            }
            build(1,n,1);
            while(m--){
                cin>>op>>x>>y;
                if(op[0]=='a'){
                    add(x,y,1,1);
                }
                else if(op[0]=='q'){
    
                    add(x,y,1,0);
                    cout<<Find(x,y,1)<<endl;
                }
            }
        }
    }
  • 相关阅读:
    ORA-06530: 引用未初始化的组合 ;
    oracle 简单的sysTimeStamp类型转date 类型
    Luogu P3388 【模板】割点(割顶)
    Luogu P2048 [NOI2010]超级钢琴
    Luogu P2657 [SCOI2009]windy数
    QZEZ第一届“饭吉圆”杯程序设计竞赛
    Luogu P2286 [HNOI2004]宠物收养场
    在平衡树的海洋中畅游(一)——Treap
    Luogu P1129 [ZJOI2007]矩阵游戏
    LOJ #559. 「LibreOJ Round #9」ZQC 的迷宫
  • 原文地址:https://www.cnblogs.com/Profish/p/9367652.html
Copyright © 2011-2022 走看看