题意:

给定一个区间, 每个区间有一个初值, 然后给出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. Zookeeper的多节点集群详细启动步骤(3或5节点)

    分为 (1)分别去3或5节点上去启动Zookeeper进程 (2)自己写个脚本,直接在主节点上去启动Zookeeper进程. (1)分别去3或5节点上去启动Zookeeper进程 第一步: [hado ...

  2. Java | 基础归纳 | java时间格式处理总结

    https://www.cnblogs.com/edwardlauxh/archive/2010/03/21/1918615.html https://blog.csdn.net/xsj_blog/a ...

  3. April Fools Contest 2017 F

    Description You are developing a new feature for the website which sells airline tickets: being able ...

  4. 167 Two Sum II - Input array is sorted 两数之和 II - 输入有序数组

    给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数.函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2.请注意,返回的下标值(i ...

  5. subline应用之python

    一交互式命令操作快捷键:在安装SublimeREPL插件后,CTRL+~/CTRL+B分别在命令行交互式和编译模式之间进行选择. 为SublimeREPL配置快捷键(每次运行程序必须用鼠标去点工具栏- ...

  6. Java中“==”的使用,以及“==”和equal的比较

    int i02=59 ,这是一个基本类型,存储在栈中. Integer i03 =Integer.valueOf(59); 因为 IntegerCache 中已经存在此对象,所以,直接返回引用. In ...

  7. js删除最后一个字符

    在最近做一个系统,使用socket来完成后台操作,C#来完成前端操作.但是在定的协议里面,一定要用某个符号来表示传的数据结束.后台进行交互时,获取到的数据必须进行删除最后一个字符的操作. 比如我们协议 ...

  8. 导Excel数据表

    需要把EXcel转换格式:

  9. AJPFX总结面向对象中成员变量和成员方法的定义

    //面向对象中成员变量和成员方法的定义格式:=========================================          成员变量定义在类中方法外,可以被该类中所有方法使用. ...

  10. 多路复用IO和异步IO

    多路复用I/O 它的基本原理就是select/epoll这个function会不断的轮询所负责的所有socket,当某个socket有数据到达了,就通知用户进程. 流程图如下: 当用户进程调用了sel ...