A Simple Problem with Integers(线段树区间更新复习,lazy数组的应用)-------------------蓝桥备战系列

本文介绍了一种高效处理大规模数组中区间加法和查询求和的操作方法,通过使用线段树和懒惰传播技术,实现了对数级的时间复杂度,适用于大数据集的实时更新和查询。示例代码展示了如何构建线段树,进行区间更新和查询,以及如何通过懒惰传播避免不必要的节点更新。

You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.

Input

The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+1, ... , Ab.

Output

You need to answer all Q commands in order. One answer in a line.

Sample Input

10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4

Sample Output

4
55
9
15

Hint

The sums may exceed the range of 32-bit integers.

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<vector>
#include<cmath>

const int maxn=1e5+5;
typedef long long ll;
using namespace std;

struct node
{
	ll l,r,sum;
}tree[maxn<<2];
ll lazy[maxn<<2];
void pushup(int m)
{

	tree[m].sum=tree[m<<1].sum+tree[m<<1|1].sum;
}
void pushdown(int m,int l)
{
	if(lazy[m]!=0)
	{
		lazy[m<<1]+=lazy[m];
		lazy[m<<1|1]+=lazy[m];
		tree[m<<1].sum+=lazy[m]*(l-(l>>1));
		tree[m<<1|1].sum+=lazy[m]*(l>>1);
		lazy[m]=0;
	}
}
void build(int m,int l,int r)
{
	tree[m].l=l;
	tree[m].r=r;
	if(l==r)
	{
		scanf("%lld",&tree[m].sum);
		return;
	}
	int mid=(l+r)>>1;
	build(m<<1,l,mid);
	build(m<<1|1,mid+1,r);
	pushup(m);
}
void update(int m,int l,int r,int val)
{
	if(tree[m].l==l&&tree[m].r==r)
	{
		lazy[m]+=val;
		tree[m].sum+=(ll)val*(r-l+1);
		return;
	}
	if(tree[m].l==tree[m].r)
	return;
	int mid=(tree[m].l+tree[m].r)>>1;
	pushdown(m,tree[m].r-tree[m].l+1);
	if(r<=mid)
	{
		update(m<<1,l,r,val);
	}
	else if(l>mid)
	{
		update(m<<1|1,l,r,val);
	}
	else 
	{
		update(m<<1,l,mid,val);
		update(m<<1|1,mid+1,r,val);
	}
	pushup(m);
}
ll query(int m,int l,int r)
{
	if(tree[m].l==l&&tree[m].r==r)
	{
		return tree[m].sum;
	}
	pushdown(m,tree[m].r-tree[m].l+1);
	int mid=(tree[m].l+tree[m].r)>>1;
	ll res=0;
	if(r<=mid)
	{
		res+=query(m<<1,l,r);
	}
	else if(l>mid)
	{
		res+=query(m<<1|1,l,r);
	}
	else 
	{
		res+=(query(m<<1,l,mid)+query(m<<1|1,mid+1,r));
		
	}
	return res;
	
}
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int n,m;
    cin>>n>>m;
    build(1,1,n);

    char op[2];
    int l,r,val;
    for(int t=0;t<m;t++)
    {
    	scanf("%s",op);
    	if(op[0]=='Q')
    	{
    		scanf("%d%d",&l,&r);
    		printf("%lld\n",query(1,l,r));
		}
		else 
		{
			scanf("%d%d%d",&l,&r,&val);
			update(1,l,r,val);
		}
	}

	return 0;
}

 

下载方式:https://pan.quark.cn/s/c9b9b647468b ### 初级JSP程序设计教程核心内容解析#### 一、JSP基础概述JSP(JavaServer Pages)是由Sun Microsystems公司创建的一种动态网页技术规范,主要应用于构建动态网站及Web应用。JSP技术使得开发者能够将动态数据与静态HTML文档整合,从而实现网页内容的灵活性和可变性。##### JSP的显著特性:1. **动态与静态内容的分离**:JSP技术支持将动态数据(例如数据库查询结果、实时时间等)嵌入到静态HTML文档中。这种设计方法增强了网页的适应性和可维护性。2. **易用性**:开发者可以利用常规的HTML编辑工具来编写静态部分,并通过简化的标签技术将动态内容集成到页面中。3. **跨平台兼容性**:基于Java平台的JSP具有优良的跨操作系统运行能力,能够在多种不同的系统环境中稳定工作。4. **强大的后台支持**:JSP能够通过JavaBean组件访问后端数据库及其他资源,以实现复杂的数据处理逻辑。5. **执行效率高**:JSP页面在初次被请求时会被转换为Servlet,随后的请求可以直接执行编译后的Servlet代码,从而提升了服务响应的效率。#### 二、JSP指令的运用JSP指令用于设定整个JSP页面的行为规范。这些指令通常放置在页面的顶部,向JSP容器提供处理页面的相关指导信息。##### 主要的指令类型:1. **Page指令**: - **语法结构**:`<%@ page attribute="value" %>` - **功能**:定义整个JSP页面的运行特性,如设定页面编码格式、错误处理机制等。 - **实例**: ...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

black-hole6

你的鼓励将是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值