2018 Multi-University Training Contest 2 1007 NAIVE OPERATIONS(线段树)

题目:
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=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
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数组都为0,b数组由题目给出。两种操作,add表示在a数组的第l到r个位置分别加1,query表示查询 ∑ri=l⌊ai/bi⌋的值(l到r)。
思路:
用线段树维护a的最大值和b的最小值,因为当a的最大值比b的最小值还要小的时候,就不应该再往下找了,省很多时间。每次更新时,用tot记录子树中有多少个1可以加。特别要注意,第一次4/3已经加过一次1了,下次4再加1变成5/3也会再加一次1,所以为了避免这种情况,每次加过1之后minb的值加b[l]这样就达到第一次3/3加过之后,下次就到6/3才可以加第二次的效果。其他就是区间更新和区间查询的模板,用到了lazy标记。

AC代码:

#include<cstdio>
#include<cstring> 
#include<algorithm>
#define Lson l,m,rt<<1
#define Rson m+1,r,rt<<1|1
using namespace std;
const int N = 1e5+5;

int b[N];

struct node
{
    int maxa;
    int minb;
    int tot;
    int lazy;
}T[N<<2];

void push_up(int rt)
{

    T[rt].maxa=max(T[rt<<1].maxa,T[rt<<1|1].maxa);
    T[rt].minb=min(T[rt<<1].minb,T[rt<<1|1].minb);
    T[rt].tot=T[rt<<1].tot+T[rt<<1|1].tot;
}
void build(int l,int r,int rt)
{
    T[rt].lazy=0;

    if(l==r)
    {
        T[rt].maxa=0;
        T[rt].minb=b[l];
        T[rt].tot=0;
        return ;
    }

    int m=(l+r)>>1;
    build(Lson);
    build(Rson);
    push_up(rt);
}

void push_down(int rt)
{
    if(T[rt].lazy)
    {
        int x=T[rt].lazy;
        T[rt].lazy = 0;
        T[rt<<1].maxa +=x;
        T[rt<<1|1].maxa +=x;
        T[rt<<1].lazy +=x;
        T[rt<<1|1].lazy +=x;
    } 
}
void update(int L,int R,int l,int r,int rt)
{
    if(l>=L&&r<=R)
    {
        T[rt].maxa++;
        if(T[rt].maxa<T[rt].minb)
        {
            T[rt].lazy++;
            return ;
        }
        else if(l==r&&T[rt].maxa>=T[rt].minb)
        {
            T[rt].tot++;
            T[rt].minb += b[l];
            return ;
        }
    }

    push_down(rt);

    int m=(l+r)>>1;

    if(L<=m)
        update(L,R,Lson);
    if(R>m)
        update(L,R,Rson);

    push_up(rt);
}
int query(int L,int R,int l,int r,int rt)
{
    if(l>=L&&r<=R)
    {
        return T[rt].tot;
    }

    int m=l+r>>1,ans=0;

    push_down(rt);

    if(L<=m)
        ans+=query(L,R,Lson);
    if(R>m)
        ans+=query(L,R,Rson);

    return ans;
}
int main()
{
    int n,m,l,r;
    char s[101];
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&b[i]);
        }

        build(1,n,1);

        for(int i=0;i<m;i++)
        {
            scanf("%s%d%d",s,&l,&r);
            if(s[0]=='a')
                update(l,r,1,n,1);
            else
                printf("%d\n",query(l,r,1,n,1));
        }
    }

    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值