HDU 6315 Naive Operations (Lazy标记线段树)

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

题目大意:给出数组b,m个操作。有两种操作,1.数组a的c到d区间元素+1      2.求c到d区间a[i]/b[i]的累加和。

思路:哇,限时3s,来,暴一发,没过?差分再来一发,还是没过?树状数组来一发,依然没过?既然如此,只能用出我尚未熟悉的线段树了........还是超时...........(一万头草泥马略过)。赛后发现这是一个知识盲点,不久前才接触线段树,尚不熟悉,跟别说深入了,然而这道题,就是线段树的升级版。可区间操作的线段树,用lazy标记实现的。那么什么是lazy标记呢,原理恶补一下:https://blog.csdn.net/sdjzping/article/details/19542103,然后这道题就成为了线段树升级版的板子了......

用的大佬的代码做板子,详细标注了一下。来源:https://blog.csdn.net/ToBeYours/article/details/81230503

代码如下:

#include<stdio.h>
#include<iostream>
#include<algorithm>
const int MAX=1e5+10;
typedef long long ll;
using namespace std;
int Minx[MAX<<2];//区间最小值 
int ans[MAX<<2];//记录最终答案
int lazy[MAX<<2];//懒惰标记
int b[MAX<<2];//记录b数组初始数组,用于更新区间最小值 
int n,q;
void pushup(int rt)
{
    Minx[rt]=min(Minx[rt<<1],Minx[rt<<1|1]);//最小值
    ans[rt]=ans[rt<<1]+ans[rt<<1|1];//答案求区间和
}
void pushdown(int rt)//更新懒惰标记 
{
    if(lazy[rt]!=0)
    {
        lazy[rt<<1]+=lazy[rt];
        lazy[rt<<1|1]+=lazy[rt];
        Minx[rt<<1|1]-=lazy[rt];//最小值一直减1
        Minx[rt<<1]-=lazy[rt];
        lazy[rt]=0;
    }
}
void build(int l,int r,int rt)//建树 
{
    lazy[rt]=0;
    ans[rt]=0;
    if(l==r)//只有一个点的情况 
    {
        scanf("%d",&b[rt]);
        Minx[rt]=b[rt];
        return ;
    }
    int m=(l+r)>>1;//除以2取中值 
    build(l,m,rt<<1);//建左子树 
    build(m+1,r,rt<<1|1);//建右子树 
    pushup(rt);//lazy标记处理 
}
void updata(int L,int R,int l,int r,int rt)//操作起点,操作终点,左始点,右终点,所在数组下标 
{
    if(L<=l&&r<=R)
    {
        Minx[rt]--;
        if(Minx[rt]!=0)
        {
            lazy[rt]++;
            return ;
        }
        else//区间最小值减小到0了//为什么是区间最小值,因为操作的这个区间值不同,满足条件的操作次数也不同 
        {
            if(l==r)
            {
                ans[rt]++;//为0加一 
                Minx[rt]=b[rt];//重新赋值区间最小值
                return ;
            }
        }
    }
    pushdown(rt);//lazy标记 
    int m=(l+r)>>1;
    if(L<=m) updata(L,R,l,m,rt<<1);//左子树 
    if(m<R) updata(L,R,m+1,r,rt<<1|1);//右子树 
    pushup(rt);
}
int query(int L,int R,int l,int r,int rt)//求和 
{
    if(L<=l&&R>=r)
        return ans[rt];
    pushdown(rt);
    int m=(l+r)>>1;
    int sum=0;
    if(m>=L) sum+=query(L,R,l,m,rt<<1);//
    if(m<R) sum+=query(L,R,m+1,r,rt<<1|1);//左右子树和累加 
    return sum;
}
int main()
{
    int l,r;
    while(scanf("%d%d",&n,&q)!=EOF)
    {
        build(1,n,1);
        for(int i=1;i<=q;i++)
        {
            char op[10];
            scanf("%s%d%d",op,&l,&r);
            if(op[0]=='a')//区间加操作 
                updata(l,r,1,n,1);
            else//查询操作 
                printf("%d\n",query(l,r,1,n,1));
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/PleasantlY1/article/details/81238527
今日推荐