HDU 6315 Naive Operations(线段树)

本文深入探讨了线段树算法的应用,特别是在处理区间加法和区间查询问题上的高效解决方案。通过一个具体的题目示例,详细讲解了如何使用线段树进行区间修改和区间求和,同时介绍了线段树的构建、更新和查询操作的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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...aral,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≤1000001≤n,q≤100000, 1≤l≤r≤n1≤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数组,b数组是1~n的一个排列,然后a数组,长度也是n,而且所有元素起始都为0,接下来有两种针对a数组的操作:
1、 add l r: a[l] 到 a[r] 之间每个元素的值 + 1
2、query l r: 求 (i = 从l到r)∑ (int)(a[i]/b[i])
线段树维护a % b - b的最大值m,和2操作的答案sum,m起始值为-b,当线段树更新到某个位置的m为0的时候,立即向下传递lazy标记,然后再向上更新sum,完了就恢复m为起始值-b。

AC代码:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
const int maxn = 1e5 + 5;
int depot[maxn],add[maxn<<2],n,q;
char op[10];
struct node
{
    int l,r,minn,contri;
}nde[maxn<<2];

void pushup(int id)
{
    nde[id].contri=nde[id<<1].contri+nde[id<<1|1].contri;
    nde[id].minn=min(nde[id<<1].minn,nde[id<<1|1].minn);
}
void pushdown(int id)
{
    if(add[id])
    {
        nde[id<<1].minn-=add[id];
        nde[id<<1|1].minn-=add[id];
        add[id<<1]+=add[id];
        add[id<<1|1]+=add[id];
        add[id]=0;
    }
}
void build(int l,int r,int id)
{
    nde[id].l=l,nde[id].r=r;
    if(l==r)
    {
        nde[id].minn=depot[l];
        nde[id].contri=0;
        return;
    }
    int mid=(l+r)>>1;
    build(l,mid,id<<1);
    build(mid+1,r,id<<1|1);
    pushup(id);
}
void modify(int l,int r,int id)
{

    if(nde[id].l==l&&nde[id].r==r&&nde[id].minn>1)
    {
        nde[id].minn-=1;
        add[id]+=1;
        return;
    }
    if(nde[id].l==nde[id].r)
    {
        nde[id].minn-=1;
        add[id]+=1;
        if(nde[id].minn<=0)
        {
            nde[id].minn=depot[l];
            nde[id].contri+=1;
        }
        return;
    }
    pushdown(id);
    int mid=(nde[id].l+nde[id].r)>>1;
    if(r<=mid)
        modify(l,r,id<<1);
    else if(mid<l)
        modify(l,r,id<<1|1);
    else
    {
        modify(l,mid,id<<1);
        modify(mid+1,r,id<<1|1);
    }
    pushup(id);
}
int query(int l,int r,int id)
{
    if(nde[id].l==l&&nde[id].r==r)
    {
        return nde[id].contri;
    }
    pushdown(id);
    int mid=(nde[id].l+nde[id].r)>>1;
    int ret=0;
    if(r<=mid)
        ret=query(l,r,id<<1);
    else if(mid<l)
        ret=query(l,r,id<<1|1);
    else
        ret=query(l,mid,id<<1)+query(mid+1,r,id<<1|1);
    pushup(id);
    return ret;
}

int main()
{
    while(scanf("%d%d",&n,&q)==2)
    {
        memset(add,0,sizeof add );
        for(int i=1; i<=n; ++i)
        {
            scanf("%d",&depot[i]);
        }
        build(1,n,1);
        for(int i=0; i<q; ++i)
        {
            int l,r;
            scanf("%s %d %d",op,&l,&r);
            if(op[0]=='a')
            {
                modify(l,r,1);
            }
            else
            {
                int ans=query(l,r,1);
                printf("%d\n",ans);
            }
        }
    }
    return 0;
}

 

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值