题意:区间add,区间求和。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 100000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
LL a[MAXN];
LL sum[MAXN << 2];
LL lazy[MAXN << 2];
void build(int id, int L, int R){
if(L == R){
sum[id] = a[L];
}
else{
int mid = L + (R - L) / 2;
build(id << 1, L, mid);
build(id << 1 | 1, mid + 1, R);
sum[id] = sum[id << 1] + sum[id << 1 | 1];
}
}
void pushdown(int id, int L, int R){
if(lazy[id]){
lazy[id << 1] += lazy[id];
lazy[id << 1 | 1] += lazy[id];
int mid = L + (R - L) / 2;
sum[id << 1] += (mid - L + 1) * lazy[id];
sum[id << 1 | 1] += (R - mid) * lazy[id];
lazy[id] = 0;
}
}
void update(int l, int r, int id, int L, int R, LL v){
if(l <= L && R <= r){
lazy[id] += v;
sum[id] += (R - L + 1) * v;
}
else{
pushdown(id, L, R);
int mid = L + (R - L) / 2;
if(l <= mid) update(l, r, id << 1, L, mid, v);
if(r > mid) update(l, r, id << 1 | 1, mid + 1, R, v);
sum[id] = sum[id << 1] + sum[id << 1 | 1];
}
}
LL query(int l, int r, int id, int L, int R){
if(l <= L && R <= r){
return sum[id];
}
pushdown(id, L, R);
int mid = L + (R - L) / 2;
LL ans = 0;
if(l <= mid) ans += query(l, r, id << 1, L, mid);
if(r > mid) ans += query(l, r, id << 1 | 1, mid + 1, R);
return ans;
}
int main(){
int N, Q;
scanf("%d%d", &N, &Q);
for(int i = 1; i <= N; ++i){
scanf("%lld", &a[i]);
}
build(1, 1, N);
while(Q--){
char cc;
int a, b;
getchar();
scanf("%c%d%d", &cc, &a, &b);
if(cc == 'C'){
int c;
scanf("%d", &c);
update(a, b, 1, 1, N, (LL)c);
}
else{
printf("%lld\n", query(a, b, 1, 1, N));
}
}
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. 利用java反射调用类的的私有方法--转

    原文:http://blog.csdn.net/woshinia/article/details/11766567 1,今天和一位朋友谈到父类私有方法的调用问题,本来以为利用反射很轻松就可以实现,因为 ...

  2. Java如何实现序列化,有什么意义?

    1.序列化是干什么的? 简单说就是为了保存在内存中的各种对象的状态,并且可以把保存的对象状态再读出来.虽然你可以用你自己的各种各样的方法来保存Object States, 但是Java给你提供一种应该 ...

  3. Redis的C++与JavaScript访问操作

    上篇简单介绍了Redis及其安装部署,这篇记录一下如何用C++语言和JavaScript语言访问操作Redis 1. Redis的接口访问方式(通用接口或者语言接口) 很多语言都包含Redis支持,R ...

  4. 电脑中安装了两个版本的jdk,后装的会把第一个覆盖掉

    电脑中之前装过一个1.8的jdk,后来工作需要又装了个1.7的,但是1.7的没有在系统环境变量中进行配置,而是通过setclasspath文件设置的,但是后来我发现,虽然没有改变系统环境变量中的JAV ...

  5. ImagePullBackOff 错误处理

    kubectl create -f nginx-deployment.yaml 执行之后,kubectl get pods 一致出现 ImagePullBackOff,一直无法执行成功. yaml文件 ...

  6. Py西游攻关之基础数据类型(四)-字典

    Py西游攻关之基础数据类型 - Yuan先生 https://www.cnblogs.com/yuanchenqi/articles/5782764.html 七 Dictionary(字典) 字典是 ...

  7. Eclipse传递main函数参数

    在项目上右击 Run As->Run Configurations...->Arguments->在Program arguments:的文本框中输入你要传入的参数,若有几个参数则在 ...

  8. Swift-如何快速学习Swift

    关于本文: 1.说明本文写作的目的 2.整理了Swift的基本语法树 3.看图作文 一.写作目的 昨天看了一个知识专栏,作者讲述的是“如何研究性的学习”.整个课程1个小时9分钟,花了我19块人民币.其 ...

  9. windows下安装redis并部署服务

    下载地址: windows版本: https://github.com/MSOpenTech/redis/releases Linux版本: 官网下载: http://www.redis.cn/ gi ...

  10. 第1节 storm编程:3、storm的架构模型的介绍

    nimbus:主节点,接收客户端提交的任务,并且分配任务,新的版本当中nimbus已经可以有多个了 zookeeper集群:storm依靠zk来保存一些节点信息,nimbus将分配的任务信息都写入到z ...