2018杭电多校第二场:1007 Naive Operations

题目描述

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⌋

输入

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 the answer for each 'query', each one line.

样例输入

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

样例输出

1
1
2
4
4
6

解题思路:使用线段树来维护每次增减。

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int sum[maxn<<2],add[maxn<<2],ans[maxn<<2],num[maxn<<2];
void pushup(int rt)
{
    num[rt]=min(num[rt<<1],num[rt<<1|1]);
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void pushdown(int l,int r,int rt){
    if (add[rt]){
        add[rt<<1]+=add[rt];
        add[rt<<1|1]+=add[rt];
        num[rt<<1]+=add[rt];
        num[rt<<1|1]+=add[rt];
        add[rt]=0;
    }
}
void build(int l,int r,int rt){
    add[rt]=0;
    if (l==r){
        num[rt]=ans[l]-1;
        sum[rt]=0;
        return ;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    pushup(rt);
}
void update(int L,int R,int l,int r,int rt){
    if (L<=l&&r<=R){
        if (num[rt]>0){
            num[rt]--;
            add[rt]--;
        }
        else {
            if (l==r)
                num[rt]=ans[l]-1,sum[rt]++;
            else{
                pushdown(l,r,rt);
                int m=(l+r)/2;
                if (num[rt<<1]==0)
                    update(L,R,lson);
                else{
                    add[rt<<1]--;
                    num[rt<<1]--;
                }
                if (num[rt<<1|1]==0)
                    update(L,R,rson);
                else{
                    add[rt<<1|1]--;
                    num[rt<<1|1]--;
                }
                pushup(rt);
            }
        }
        return ;
    }
    pushdown(l,r,rt);
    int m=(l+r)>>1;
    if (L<=m)
        update(L,R,lson);
    if (R>m)
        update(L,R,rson);
    pushup(rt);
}
int query(int L,int R,int l,int r,int rt){
    if (L<=l&&r<=R)
        return sum[rt];
    pushdown(l,r,rt);
    int res=0;
    int m=(r+l)>>1;
    if (L <= m) res += query(L , R , lson);
    if (m < R) res += query(L , R , rson);
    return res;
}
int main(){
    int n,m,x,y;
    char op[10];
    while (scanf("%d%d",&n,&m)!=EOF){
        for (int i=1;i<=n;i++){
            scanf("%d",&ans[i]);
        }
        build(1,n,1);
        while (m--){
            scanf("%s%d%d",&op,&x,&y);
            if (op[0]=='a'){
                update(x,y,1,n,1);
            }
            else {
                printf("%d\n",query(x,y,1,n,1));
            }
        }
    }
   return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40911499/article/details/81223416