区间求和Java,POJ 3468 区间更新,区间求和(经典)

A Simple Problem with Integers

Time Limit: 5000MS

Memory Limit: 131072K

Total Submissions: 72265

Accepted: 22299

Case Time Limit: 2000MS

Description

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 Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.

"Q a b" means querying the sum of Aa, Aa+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

题目意思:

给一个长度为n的数组,有q组操作,操作有两种:Q l, r  即查询l到r的和。  C l, r, val   即把l到r数组元素都加上val

思路:

线段树经典题目,需要用lazy思想,也就是当把l r加上val时,只标记这个区间lazy=val即可 ,当下次再加上一个val时且在l r之间,那么向下传递。

代码:

#include

#include

#include

#include

#include

#include

#include

#include

using namespace std;

#define N 100005

#define ll root<<1

#define rr root<<1|1

#define mid (a[root].l+a[root].r)/2

int max(int x,int y){return x>y?x:y;}

int min(int x,int y){return x

int abs(int x,int y){return x-x:x;}

int n;

struct node{

int l, r;

__int64 val, sum;

}a[N*];

void build(int l,int r,int root){

a[root].l=l;

a[root].r=r;

a[root].val=;

if(l==r){

scanf("%I64d",&a[root].sum);

return;

}

build(l,mid,ll);

build(mid+,r,rr);

a[root].sum=a[ll].sum+a[rr].sum;

}

void down(int root){

if(a[root].val&&a[root].l!=a[root].r) {

a[ll].val+=a[root].val;

a[rr].val+=a[root].val;

a[ll].sum+=(__int64)(a[ll].r-a[ll].l+)*a[root].val;

a[rr].sum+=(__int64)(a[rr].r-a[rr].l+)*a[root].val;

a[root].val=;

}

}

void update(int l,int r,__int64 val,int root){

if(a[root].l==l&&a[root].r==r){

a[root].val+=val;

a[root].sum+=(__int64)(a[root].r-a[root].l+)*val;

return;

}

down(root);

if(l>=a[rr].l) update(l,r,val,rr);

else if(r<=a[ll].r) update(l,r,val,ll);

else {

update(l,mid,val,ll);

update(mid+,r,val,rr);

}

a[root].sum=a[ll].sum+a[rr].sum;

}

__int64 query(int l,int r,int root){

if(a[root].l==l&&a[root].r==r){

return a[root].sum;

}

down(root);

if(r<=a[ll].r) return query(l,r,ll);

else if(l>=a[rr].l) return query(l,r,rr);

else return query(l,mid,ll)+query(mid+,r,rr);

}

void out(int root){

if(a[root].l==a[root].r) {

printf("%I64d ",a[root].sum); return;

}

down(root);

out(ll);

out(rr);

}

main()

{

int i, j, k;

int q;

while(scanf("%d %d",&n,&q)==){

build(,n,);

char s[];

__int64 w;

int l, r;

while(q--){

scanf("%s",s);

if(strcmp(s,"Q")==){

scanf("%d %d",&l,&r);

printf("%I64d\n",query(l,r,));

}

else{

scanf("%d %d %I64d",&l,&r,&w);

update(l,r,w,);

// out(1);

}

}

}

}

poj3468&lpar;线段树区间更新&amp&semi;区间求和模板&rpar;

题目链接: http://poj.org/problem?id=3468 题意: 输入 n, m表初始有 n 个数, 接下来 m 行输入, Q x y 表示询问区间 [x, y]的和: C x y z ...

hdu 1698 线段树 区间更新 区间求和

Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

hdu6070&lpar;分数规划&sol;二分&plus;线段树区间更新&comma;区间最值&rpar;

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6070 题意: 给出一个题目提交序列, 从中选出一个正确率最小的子串. 选中的子串中每个题目当且仅当最 ...

POJ 2155 Matrix(二维树状数组&plus;区间更新单点求和)

题意:给你一个n*n的全0矩阵,每次有两个操作: C x1 y1 x2 y2:将(x1,y1)到(x2,y2)的矩阵全部值求反 Q x y:求出(x,y)位置的值 树状数组标准是求单点更新区间求和,但 ...

poj 3468 线段树区间更新&sol;查询

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

POJ-3468-A Simple Problem with Integers&lpar;区间更新,求和&rpar;-splay或线段树

区间更新求和 主要用来练习splay树区间更新问题 //splay树的题解 // File Name: 3468-splay.cpp // Author: Zlbing // Created Time ...

线段树区间更新&plus;区间求和模板(数组实现)洛谷p3372,p3373

模板题目1:https://www.luogu.org/problemnew/show/P3372 懒惰标记讲解:https://www.cnblogs.com/wushengyang/p/11194 ...

POJ 3468 线段树区间修改查询&lpar;Java&comma;c&plus;&plus;实现&rpar;

POJ 3468 (Java,c++实现) Java import java.io.*; import java.util.*; public class Main { static int n, m ...

NBOJv2 1004 蛤玮打扫教室(线段树区间更新区间最值查询)

Problem 1004: 蛤玮打扫教室 Time Limits:  1000 MS   Memory Limits:  65536 KB 64-bit interger IO format:  %l ...

【DFS序&plus;线段树区间更新区间求最值】HDU 5692 Snacks

http://acm.hdu.edu.cn/showproblem.php?pid=5692 [思路] 每更新一个点,子树的所有结点都要更新,所以是区间更新 每查询一个点,子树的所有结点都要查询,所以 ...

随机推荐

ObjectOutputStream和ObjectInputStream

官方解释: ObjectOutputStream 将 Java 对象的基本数据类型和图形写入 OutputStream.可以使用 ObjectInputStream 读取(重构)对象.通过使用流中的文 ...

六间房 繁星 酷我 来疯 秀吧 新浪秀 直播播放器 Live 1&period;2

适合用于进行录制的时候 特别说明: 安装 falsh play 19 时 不能正常播放 每个按钮都有提示,不详细说明 下载地址 http://pan.baidu.com/s/1i32ETIt 下载地址 ...

谷歌、火狐浏览器gift图片缓存后不显示动态效果

delphi 设置超链接

的属性 的事件 的方法 //1设置链接类型//2获取样式 链接和提示信息 //title是提示信息//HTTPS https://

 错在哪里? 如果你这么写过,会发现这个位置啥也没有加载出来 ...

Spring事务的传播行为

本文主要介绍下Spring事务中的传播行为. 事务传播行为介绍 Spring中的7个事务传播行为: |事务行为|说明 | |:--|:--| |PROPAGATION_REQUIRED | 支持当 ...

C&plus;&plus;笔记整理(参考整理自各大博客)

为什么构造函数不能是虚函数,析构函数往往是虚函数? 静态存储区.无论在那里构建,其过程都是两步:首先,分配一块内存:其次,调用构造函数.好,问题来了,如果构造函数是虚函数,那么就需要通过vtable  ...

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值