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;
            }
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM