POJ3468 线段树-模板题

简介: Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations.

Description

You have N integers, A1A2, ... , 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 A1A2, ... , 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.

Source

POJ Monthly--2007.11.25, Yang Yi

题意很明显,只有两种操作,需要使用线段树(对于区间的修改~):update()和query() 用到了__int64需要注意

代码如下:

#include <stdio.h>
#include <iostream>
#include <string.h>

using namespace std;

const int MAXN = 100000 + 10;
struct Node{
	int l, r;
	__int64 k, add;
};
Node znode[4*MAXN];
__int64 zfun(__int64 a, __int64 b) { 
	return a+b;
}
void build(int l, int r, int n){
	int mid = (l+r)/2;
	znode[n].l = l;
	znode[n].r = r;
	znode[n].add = 0;
	znode[n].k = 0;
	if(l==r) return;
	else {
		build(l, mid, 2*n);
		build(mid+1, r, 2*n+1);
	}
}
void update(int l, int r, int n, __int64 val){
	znode[n].k += (r-l+1)*val;
	if(l<=znode[n].l && znode[n].r<=r) znode[n].add += val;
	else{
		int mid = (znode[n].l + znode[n].r) / 2;
		if(r<=mid) update(l, r, 2*n, val);
		else if(l>mid) update(l, r, 2*n+1, val);
		else {
			update(l, mid, 2*n, val);
			update(mid+1, r, 2*n+1, val);
		}
	}
}
__int64 query(int l, int r, int n){				//l、r是待查询的区间端点
	int mid = (znode[n].l + znode[n].r) / 2;
	if(znode[n].l==l && znode[n].r==r) return znode[n].k;
	else{
		if(znode[n].add){			//下放延迟的标记
			znode[2*n].add += znode[n].add;
			znode[2*n].k += znode[n].add*(znode[2*n].r-znode[2*n].l+1);
			znode[2*n+1].add += znode[n].add;
			znode[2*n+1].k += znode[n].add*(znode[2*n+1].r-znode[2*n+1].l+1);
			znode[n].add = 0;
		}
		if(r<=mid) return query(l, r, 2*n);
		else if(l>mid) return query(l, r, 2*n+1);
		else return zfun(query(l, mid, 2*n), query(mid+1, r, 2*n+1));
	}
}
int main(){
//	freopen("in.txt", "r", stdin);
	int id = 1, n, m;
	while(scanf("%d%d", &n, &m)!=EOF){
		build(1, n, 1);
		int i, s;
		for(i=1; i<=n; i++){
			scanf("%d", &s);
			update(i, i, 1, s);
		}
		char ch[2];
		int a, b;
		__int64 c;
		for(i=1; i<=m; i++){
			scanf("%s%d%d", ch, &a, &b);
			if(ch[0]=='Q') {
				__int64 ans = query(a, b, 1);
				printf("%I64d\n", ans);
			}
			else if(ch[0]=='C'){
				scanf("%I64d", &c);
				update(a, b, 1, c);
			}
		}
	}
	return 0;
}


目录
相关文章
|
1天前
|
云安全 人工智能 自然语言处理
|
6天前
|
搜索推荐 编译器 Linux
一个可用于企业开发及通用跨平台的Makefile文件
一款适用于企业级开发的通用跨平台Makefile,支持C/C++混合编译、多目标输出(可执行文件、静态/动态库)、Release/Debug版本管理。配置简洁,仅需修改带`MF_CONFIGURE_`前缀的变量,支持脚本化配置与子Makefile管理,具备完善日志、错误提示和跨平台兼容性,附详细文档与示例,便于学习与集成。
314 116
|
8天前
|
数据采集 人工智能 自然语言处理
Meta SAM3开源:让图像分割,听懂你的话
Meta发布并开源SAM 3,首个支持文本或视觉提示的统一图像视频分割模型,可精准分割“红色条纹伞”等开放词汇概念,覆盖400万独特概念,性能达人类水平75%–80%,推动视觉分割新突破。
610 53
Meta SAM3开源:让图像分割,听懂你的话
|
21天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~
|
5天前
|
人工智能 Java API
Java 正式进入 Agentic AI 时代:Spring AI Alibaba 1.1 发布背后的技术演进
Spring AI Alibaba 1.1 正式发布,提供极简方式构建企业级AI智能体。基于ReactAgent核心,支持多智能体协作、上下文工程与生产级管控,助力开发者快速打造可靠、可扩展的智能应用。
|
4天前
|
弹性计算 人工智能 Cloud Native
阿里云无门槛和有门槛优惠券解析:学生券,满减券,补贴券等优惠券领取与使用介绍
为了回馈用户与助力更多用户节省上云成本,阿里云会经常推出各种优惠券相关的活动,包括无门槛优惠券和有门槛优惠券。本文将详细介绍阿里云无门槛优惠券的领取与使用方式,同时也会概述几种常见的有门槛优惠券,帮助用户更好地利用这些优惠,降低云服务的成本。
270 132
|
8天前
|
机器学习/深度学习 人工智能 自然语言处理
AgentEvolver:让智能体系统学会「自我进化」
AgentEvolver 是一个自进化智能体系统,通过自我任务生成、经验导航与反思归因三大机制,推动AI从“被动执行”迈向“主动学习”。它显著提升强化学习效率,在更少参数下实现更强性能,助力智能体持续自我迭代。开源地址:https://github.com/modelscope/AgentEvolver
423 29
|
15天前
|
安全 Java Android开发
深度解析 Android 崩溃捕获原理及从崩溃到归因的闭环实践
崩溃堆栈全是 a.b.c?Native 错误查不到行号?本文详解 Android 崩溃采集全链路原理,教你如何把“天书”变“说明书”。RUM SDK 已支持一键接入。
728 224

热门文章

最新文章