HDU 6315 (2018多校第二场)(线段树)

传送门

题面:

Naive Operations

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

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

题目描述:

    给你一个数组a[i]和b[i],其中a[i]最开始为0,b[i]为n的排列。

    有两种操作,第一种在区间[l,r]中将a[i]加1。第二种统计区间[l,r]的 ai/bi(下取整)的和。

题目分析:

    这个题一眼望过去,区间更新,区间求和,很明显就可以想到用线段树进行维护。

    但是这个问题的难点在于这个式子 c[i]=ai/bi的求和相对来说不好直接处理。在这里,我们需要对这个式子进行分析。我们需要发现,对于每一个c[i],它的值倘若能够成功加1,那么必定是ai在区间更新中增加了bi 。因此我们就可以往这个方向去考虑。

    但是对ai不断的加1并判断与bi的关系,处理起来相对比较麻烦,因此我们就可以逆向考虑:使得在每次更新中bi-1,而若bi减到0,则使答案+1,并使得bi重新变为原值。

    如此一来,我们只需要用线段树对区间bi的最小值min以及答案c[i]进行维护即可。

代码:

#include <bits/stdc++.h>
#define maxn 100005
using namespace std;
typedef long long ll;
struct Tree{
    ll sum,valb,minn,add;
}tr[maxn<<2];
int n,q;
int a[maxn],b[maxn];
void push_up(int rt){//维护区间bi的最小值以及答案
    tr[rt].minn=min(tr[rt<<1].minn,tr[rt<<1|1].minn);
    tr[rt].sum=tr[rt<<1].sum+tr[rt<<1|1].sum;
}
void push_down(int rt){//lazy操作
    if(tr[rt].add){
        tr[rt<<1].add+=tr[rt].add;
        tr[rt<<1|1].add+=tr[rt].add;
        tr[rt<<1].minn-=tr[rt].add;
        tr[rt<<1|1].minn-=tr[rt].add;
        tr[rt].add=0;
    }
}
void build(int l,int r,int rt){//建树
    tr[rt].add=tr[rt].sum=0;
    if(l==r){
        tr[rt].minn=tr[rt].valb=b[l];
        tr[rt].sum=0;
        return;
    }
    int mid=(l+r)>>1;
    build(l,mid,rt<<1);
    build(mid+1,r,rt<<1|1);
    push_up(rt);
}
void update(int L,int R,int l,int r,int rt){//区间更新
    //如果询问区间[L,R]能被覆盖且[L,R]的最小值大于0,则进行-1操作
    if(L<=l&&R>=r&&tr[rt].minn>1){
        tr[rt].add++;
        tr[rt].minn--;
        return;
    }
    //否则不断遍历到叶子结点并判断当前bi的值是否为0
    if(l==r&&tr[rt].minn==1){
        tr[rt].sum++;
        tr[rt].minn=tr[rt].valb;
        tr[rt].add=0;
        return;
    }
    int mid=(l+r)>>1;
    push_down(rt);
    if(L<=mid) update(L,R,l,mid,rt<<1);
    if(R>mid) update(L,R,mid+1,r,rt<<1|1);
    push_up(rt);
}
ll query(int L,int R,int l,int r,int rt){//区间求和
    if(L<=l&&R>=r){
        return tr[rt].sum;
    }
    int mid=(l+r)>>1;
    push_down(rt);
    ll ans=0;
    if(L<=mid) ans+=query(L,R,l,mid,rt<<1);
    if(R>mid) ans+=query(L,R,mid+1,r,rt<<1|1);
    return ans;
}
int main()
{
    while(~scanf("%d%d",&n,&q)){
        for(int i=1;i<=n;i++){
            scanf("%d",&b[i]);
        }
        build(1,n,1);
        while(q--){
            string str;
            cin>>str;
            int l,r;
            scanf("%d%d",&l,&r);
            if(str[0]=='a'){
                update(l,r,1,n,1);
            }
            else{
                ll res=query(l,r,1,n,1);
                printf("%lld\n",res);
            }
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_39453270/article/details/81211237