题意:区间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. css中class后面跟两个类,这两个类用空格隔开

    css中class后面跟两个类,这两个类用空格隔开,那么这两个类对这个元素都起作用,如果产生冲突,那么后面的类将替代前面的类.

  2. 2020牛客寒假算法基础集训营4 J 二维跑步

    https://ac.nowcoder.com/acm/contest/view-submission?submissionId=43035417 假设有i步选择不动,就有n-i步移动 假设其中又有a ...

  3. 学习黑马教学视频SSM整合中Security遇到的问题org.springframework.security.access.AccessDeniedException: Access is denied

    问题已解决. 总结: 报错:org.springframework.security.access.AccessDeniedException: Access is denied 当您遇到同样问题时, ...

  4. 「ZJOI2007」捉迷藏

    题目描述 给出一棵\(N\)个有色(黑白,黑色对应关灯,白色对应开灯)节点的树以及\(M\)次操作,每次操作将改变一个节点的颜色或者求出树上最远的两个白点距离 基本思路 \(60pts\)做法 这道题 ...

  5. The Google File System中文版

    译者:alex 摘要 我们设计并实现了Google GFS文件系统,一个面向大规模数据密集型应用的.可伸缩的分布式文件系统.GFS虽然运行在廉价的普遍硬件设备上,但是它依然了提供灾难冗余的能力,为大量 ...

  6. swift4之String与NSString的区别与使用

    String是结构体,NSString是类,这是它们的根本区别. 在 Swift 中,结构体struct是值类型,String是结构体,所以也是值类型.值类型被赋予给一个变量.常量或者被传递给一个函数 ...

  7. threading 多线程

    # coding:utf- import time from threading import Thread def foo(x):#这里可以带参数def foo(x) print "foo ...

  8. SPring整合Mybatis方式一

    Spring整合Mybatis 需要maven包: mysql-connector-java 5.1.47, mybatis 3.5.2, spring-webmvc 5.2.2.RELEASE, s ...

  9. Day9 - K - Yue Fei's Battle HDU - 5136

    Yue Fei is one of the most famous military general in Chinese history.He led Southern Song army in t ...

  10. leetcode347 Top K Frequent Elements

    """ Given a non-empty array of integers, return the k most frequent elements. Example ...