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

思路:

之前学了线段树单点更新,所以想都没想直接一个个点更新果断超时,然后发现是区间更新orz

这是道区间更新模板题。

线段树区间更新引入了一个新东西叫做“懒标记”,它的用处是当我们进行区间更新时不用更新到根节点,比如在[1,10]区间对[1,5]区间更新,我们不需要对1~5都更新(因为目前还没用到1~5),所以我们只要暂时把值存在[1,5]这个节点就行了。如果我们需要访问[1,5]的子节点时,我们需要将暂存的值往下加。

代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<cmath>
#include<string>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<iostream>
#include<algorithm>
#include<sstream>
#define ll long long
const int N=100005;
const int MOD=20071027;
using namespace std;
struct node{
int l,r;
ll num,add;
}node[N<<2];
int arr[N];
void build(int l,int r,int rt){
node[rt].l=l,
node[rt].r=r;
node[rt].add=0;
if(l==r){
node[rt].num=arr[l];
return;
}
int m=(l+r)>>1;
build(l,m,rt<<1);
build(m+1,r,rt<<1|1);
node[rt].num=node[rt<<1].num+node[rt<<1|1].num;
}
void add(int rt,int l,int r,int v){
if(node[rt].l==l && node[rt].r==r){ //暂时储存
node[rt].add+=v;
return;
}
node[rt].num+=v*(r-l+1);
int m=(node[rt].l+node[rt].r)>>1;
if(r<=m){ //改变区间属于左子节点子集
add(rt<<1,l,r,v);
}
else if(l>m){ //属于右子节点子集
add(rt<<1|1,l,r,v);
}
else{ //属于左右子节点子集
add(rt<<1,l,m,v);
add(rt<<1|1,m+1,r,v);
}
}
ll query(int rt,int l,int r){
if(node[rt].l==l && node[rt].r==r){
return node[rt].num+(r-l+1)*node[rt].add;
}
node[rt].num+=(node[rt].r-node[rt].l+1)*node[rt].add; //需要继续往下找,在这里加上之前暂时存放的值
int m=(node[rt].l+node[rt].r)>>1;
add(rt<<1,node[rt].l,m,node[rt].add);
add(rt<<1|1,m+1,node[rt].r,node[rt].add);
node[rt].add=0; //暂存值归零
if(r<=m){ //查询区间属于左子节点子集
return query(rt<<1,l,r);
}
else if(l>m){ //属于右子节点子集
return query(rt<<1|1,l,r);
}
else{ //属于左右子节点子集
return query(rt<<1,l,m)+query(rt<<1|1,m+1,r);
}
}
int main(){
int n,q,a,b,c;
ll ans;
char order[2];
while(scanf("%d%d",&n,&q)!=EOF){
for(int i=1;i<=n;i++) scanf("%d",&arr[i]);
build(1,n,1);
while(q--){
scanf("%s",order);
if(order[0]=='Q'){
scanf("%d%d",&a,&b);
ans=query(1,a,b);
printf("%lld\n",ans);
}
else{
scanf("%d%d%d",&a,&b,&c);
add(1,a,b,c);
}
}
}
return 0;
}

POJ 3468 A Simple Problem with Integers(线段树&区间更新)题解的更多相关文章

  1. poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 75541   ...

  2. (简单) POJ 3468 A Simple Problem with Integers , 线段树+区间更新。

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

  3. [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]

    A Simple Problem with Integers   Description You have N integers, A1, A2, ... , AN. You need to deal ...

  4. poj 3468 A Simple Problem with Integers 线段树区间更新

    id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072 ...

  5. POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 67511   ...

  6. POJ 3468 A Simple Problem with Integers(线段树区间更新)

    题目地址:POJ 3468 打了个篮球回来果然神经有点冲动. . 无脑的狂交了8次WA..竟然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题. 区间更新就是加一个lazy标记,延迟标记, ...

  7. POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)

    #include <iostream> #include <stdio.h> #include <string.h> #define lson rt<< ...

  8. POJ 3468 A Simple Problem with Integers 线段树 区间更新

    #include<iostream> #include<string> #include<algorithm> #include<cstdlib> #i ...

  9. poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和

    A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...

  10. poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和(模板)

    A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...

随机推荐

  1. linux系统java的安装

    (一)下载java8 下载链接:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html ...

  2. js判断字符串长度

    方法1: String.prototype.gblen = function() { var len = 0; for (var i=0; i<this.length; i++) { if (t ...

  3. 洛谷P2679 子串 [noip2015] dp

    正解:dp 解题报告: 感觉是道dp好题啊,所以就写了个题解 代码实现难度低,思维难度大,像我这种思维僵化傻逼选手只想到了爆搜+组合数学... 其实是道很妙的dp题!好趴也没有多妙主要大概是妙在想到了 ...

  4. 【Python-虫师】自动化测试模型--参数化

    一.自动化测试代码最开始是线性的,后续发展为模块化代码,所以涉及到了函数和方法的引用. 1. 函数和方法的最主要区别: Python的方法中定义函数的时候,函数中必须写self.而单独定义函数的时候则 ...

  5. PHP操作Redis常用技巧

    这篇文章主要介绍了PHP操作Redis常用技巧,结合实例形式总结分析了php针对redis的连接.认证.string.hash等操作技巧与注意事项,需要的朋友可以参考下 本文实例讲述了PHP操作Red ...

  6. iOS UI基础-3.0图片浏览器及plist使用

    需求: 1.显示当前图片序号/总图片数 2.显示图片 3.上一张图片.下一张图片转换 4.显示图片描述 下面用代码来实现 // // UYViewController.m // 3.0图片查看器 // ...

  7. Android adb.exe程序启动不起来处理方法

    经常遇到 Please ensure that adb is correctly located at 'D:\java\sdk\platform-tools\adb.exe' and can be ...

  8. 7.11 Models -- Customizing Adapters

    一.概述 1. 在Ember Data中,和后台数据存储通信的逻辑存在于Adapter中.Ember Data的有一些内置的假设,一个 REST API 应该怎么看.如果你的后台约定和这些假设不同,E ...

  9. 电子地图/卫星地图下载并转存为jpg图片

    1.下载水经注万能地图下载器破解版 http://download.csdn.net/download/hyb2012/8714725,此软件为绿色免安装且免注册 2.下载后解压缩后,运行sgwn.e ...

  10. codeforces 461C

    这题说的是 给了一张长方形的纸 1*n 然后可以按照不同的做法去折这个纸张 他有两种操作,操作1 给了一个pi 点 然后将左边的纸往右边折,第2种操作是给了一个L 和 R 然后计算出 L和R 之间的纸 ...