题意:

给定一个区间, 每个区间有一个初值, 然后给出Q个操作, C a b c是给[a,b]中每个数加上c, Q a b 是查询[a,b]的和

代码:

 #include <cstdio>
#include <cstring>
using namespace std;
const int maxn = + ;
struct{
long long val, addMark;
}segTree[maxn << ];
long long a[maxn];
int n , m;
void build(int root, int l, int r){
segTree[root].addMark = ; if(l == r){
segTree[root].val = a[l];
return;//记得return
}
int mid = (l+r) >> ;
build(root*, l,mid);
build(root*+, mid + , r);
segTree[root].val = segTree[root*].val + segTree[root*+].val; //回溯时候更新root
}
void push_down(int root,int L, int R){//传入L, R是为了计算左右子树的和, 分别是(mid - L + 1)、(R-mid)
if(segTree[root].addMark != ){
int mid = L + R >> ;
segTree[root*].addMark += segTree[root].addMark;
segTree[root*+].addMark += segTree[root].addMark; segTree[root*].val += segTree[root].addMark * (mid - L + );
segTree[root*+].val += segTree[root].addMark * (R-mid); segTree[root].addMark = ;
}
}
long long query(int root, int L, int R, int QL, int QR){
if(L > QR || R < QL) return ; if(QL <= L && QR >= R) {
return segTree[root].val;
}
push_down(root,L,R);//如果要向下计算记得先pushdown
int mid = L + R >> ;
return query(root*,L,mid,QL,QR) + query(root*+,mid+,R,QL,QR);
}
void update(int root, int L ,int R, int QL, int QR, int val){
if(L > QR || R < QL) return; if(QL <= L && QR >= R){ segTree[root].val += val * (R-L+);
segTree[root].addMark += val;
return;
} push_down(root,L,R);//如果要向下计算记得先pushdown
int mid = L + R >> ;
update(root*, L, mid, QL , QR , val);
update(root*+,mid+, R, QL,QR,val);
segTree[root].val = segTree[root*].val + segTree[root*+].val;//回溯更新root
}
int main()
{
// freopen("1.txt","r", stdin);
while(~scanf("%d %d", &n , &m)){
memset(segTree,,sizeof(segTree)); for(int i = ; i <= n; i++){
scanf("%lld", &a[i]);
}
build(,,n);
while(m--){
char cho[];
scanf("%s", cho);
int x, y;
scanf("%d %d", &x, &y);
if(cho[] == 'C'){
int v;
scanf("%d", &v);
update(,,n,x,y,v);
}
else{
printf("%lld\n",query(,,n,x,y));
}
}
}
}

POJ 3468 A Simple Problem with Integers (线段树多点更新模板)的更多相关文章

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

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

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

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

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

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

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

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

  5. (简单) 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 ...

  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 ...

随机推荐

  1. iOS UITableView设置tableHeaderView时发生约束错误 UIView-Encapsulated-Layout-Height UIView-Encapsulated-Layout-Width

    在将UITableView的tableHeaderView设置为我自己创建的View的时候, 当我为这个自定义View添加约束之后启动调试, 然后符号断点UIViewAlertForUnsatisfi ...

  2. SP1805 Largest Rectangle in a Histogram

    题目链接: 洛谷SP1805 题意: 如图所示,在一条水平线上有n个宽为1的矩形,求包含于这些矩形的最大子矩形面积(图中的阴影部分的面积即所求答案) 输入格式: 有多组测试数据,每组数据占一行.输入零 ...

  3. 暴力 Codeforces Round #305 (Div. 2) B. Mike and Fun

    题目传送门 /* 暴力:每次更新该行的num[],然后暴力找出最优解就可以了:) */ #include <cstdio> #include <cstring> #includ ...

  4. 1-20StringBuffer简介

    StringBuffer是一个字符串缓冲区,如果需要频繁的对字符串进行拼接时,建议使用StringBuffer. 工作原理 StringBuffer的底层是char数组,如果没有明确设定,则系统会默认 ...

  5. UIBarButtonItem系统默认风格形状

    typedef NS_ENUM(NSInteger, UIBarButtonSystemItem) { UIBarButtonSystemItemDone, UIBarButtonSystemItem ...

  6. 139 Word Break 单词拆分

    给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,确定 s 是否可以被空格分割为一个或多个在字典里出现的单词.你可以假设字典中无重复的单词.例如,给出s = "leet ...

  7. TPS763xxDBV线性稳压器

    DC DC converter 是直流变换器,因为直流不能通过变压器改变电压,要将直流电压通过振荡变成交流电压,再通过变压器或斩波器将电压升高或降低,再经滤波变成所需的电压.而voltage regu ...

  8. 生产环境中nginx既做web服务又做反向代理

    一.写对于初入博客园的感想 众所周知,nginx是一个高性能的HTTP和反向代理服务器,在以前工作中要么实现http要么做反向代理或者负载均衡.尚未在同一台nginx或者集群上同时既实现HTTP又实现 ...

  9. ES6初探——编译环境搭建

    不好意思我又要来写操作文档了,看起来更像wiki的博客(如果你想深入学习,请阅读文末列的参考资料).本文将示例如何把ES6编译成ES5. 首先,你要自行查阅什么是ES6,和ES5.javascript ...

  10. 【转】数据库CRUD操作

    数据库CRUD操作 一.删除表   drop table 表名称 二.修改表 alter   table 表名称 add  列名 数据类型   (add表示添加一列) alter  table  表名 ...