hdu 6315 Naive Operations

Naive Operations

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 502768/502768 K (Java/Others)
Total Submission(s): 853    Accepted Submission(s): 318


 

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=l⌊ai/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.
1≤n,q≤100000, 1≤l≤r≤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

 

 

Source

2018 Multi-University Training Contest 2

 

 

Recommend

chendu   |   We have carefully selected several similar problems for you:  6318 6317 6316 6315 6314 

 

 

Statistic | Submit | Discuss | Note
 

思路:

维护一个区间 a 的最大值,b的最小值,用一个lazy来标记区间增加过的次数,当maxa

 

具体细节 当更新到单点并且 maxa >= minb 时,区间答案加一,minb+=b[l] 

其他基本是线段树板子。

代码如下:

#include 
using namespace std;
const int maxn=1e5+10;
struct node{
    int l,r,fg,miv,mav,s;
}tr[maxn<<2];
int n,m,b[maxn],l,r;
char s[20];

void pushup(int i){
    tr[i].miv=min(tr[i<<1].miv,tr[i<<1|1].miv);
    tr[i].s=tr[i<<1].s+tr[i<<1|1].s;
    tr[i].mav=max(tr[i<<1].mav,tr[i<<1|1].mav);
}
void pushdown(int i){
    if(tr[i].fg){
        tr[i<<1].fg+=tr[i].fg;
        tr[i<<1|1].fg+=tr[i].fg;
        tr[i<<1].mav+=tr[i].fg;
        tr[i<<1|1].mav+=tr[i].fg;
        tr[i].fg=0;
    }
}
void build(int l,int r,int i){
    tr[i].l=l,tr[i].r=r;
    tr[i].fg=0;
    if(l==r){
        tr[i].miv=b[l];
        tr[i].s=tr[i].mav=0;
        return;
    }
    int mid=l+r>>1;
    build(l,mid,i<<1);
    build(mid+1,r,i<<1|1);
    pushup(i);
}
void update(int l,int r,int i){
//    printf("update %d %d %d %d %d\n",l,r,tr[i].l,tr[i].r,i);
    if(l<=tr[i].l && tr[i].r<=r){
        tr[i].mav++;
        if(tr[i].mav=tr[i].miv){
            tr[i].s++;
            tr[i].miv+=b[tr[i].l];
            return ;
        }
    }
    pushdown(i);
    int mid=tr[i].l+tr[i].r>>1;
    if(l<=mid) update(l,r,i<<1);
    if(r>mid)  update(l,r,i<<1|1);
    pushup(i);
}
int Query(int l,int r,int i){
   if(l<=tr[i].l && tr[i].r<=r) return tr[i].s;
   pushdown(i);
   int ans=0,mid=tr[i].l+tr[i].r>>1;
   if(l<=mid) ans+=Query(l,r,i<<1);
   if(r>mid)  ans+=Query(l,r,i<<1|1);
   return ans;
}

int main(){
    while(scanf("%d%d",&n,&m)==2){
        for (int i=1; i<=n ;i++) scanf("%d",&b[i]);
        build(1,n,1);
        while(m--){
            scanf("%s%d%d",s,&l,&r);
            if(s[0]=='a') update(l,r,1);
            else printf("%d\n",Query(l,r,1));
        }
    }
    return 0;
}

 

你可能感兴趣的:(线段树)