#include <iostream>
#include <stdio.h>
#include <string.h>
#define lson rt<<1,L,mid
#define rson rt<<1|1,mid+1,R using namespace std;
const int maxn=;
int n,q;
long long num[maxn];
struct Node{
long long sum,add;
bool lazy;
}tree[maxn<<]; void pushUp(int rt){
tree[rt].sum=tree[rt<<].sum+tree[rt<<|].sum;
}
void pushDown(int rt,int m){
if(tree[rt].lazy){
tree[rt<<].add+=tree[rt].add;
tree[rt<<|].add+=tree[rt].add;
tree[rt<<].sum+=(m-m/)*tree[rt].add; //是加上父亲的add
tree[rt<<|].sum+=m/*tree[rt].add;
tree[rt<<].lazy=tree[rt<<|].lazy=true;
tree[rt].add=;
tree[rt].lazy=false;
}
}
void build(int rt,int L,int R){
if(L==R){
tree[rt].sum=num[L];
tree[rt].add=;
tree[rt].lazy=false;
return ;
}
int mid=(L+R)>>;
build(lson);
build(rson);
pushUp(rt);
} void update(int rt,int L,int R,int l,int r,long long val){
if(l<=L&&R<=r){
tree[rt].sum+=(R-L+)*val;
tree[rt].add+=val;
tree[rt].lazy=true;
return;
}
pushDown(rt,R-L+);
int mid=(L+R)>>;
if(l<=mid)
update(lson,l,r,val);
if(r>mid)
update(rson,l,r,val);
pushUp(rt);
} long long query(int rt,int L,int R,int l,int r){
long long ret=;
if(l<=L&&R<=r){
return tree[rt].sum;
}
pushDown(rt,R-L+); //额,好吧,忘记这里也要初始化了
int mid=(L+R)>>;
if(l<=mid)
ret+=query(lson,l,r);
if(r>mid)
ret+=query(rson,l,r);
return ret;
}
int main()
{
int a,b;
long long c; //c用int也能AC,用long long是防止当(R-L+1)*c的时候超出int范围
char str[];
long long ans;
scanf("%d%d",&n,&q);
for(int i=;i<=n;i++)
scanf("%I64d",&num[i]);
build(,,n);
for(int i=;i<=q;i++){
scanf("%s",str);
if(str[]=='C'){
scanf("%d%d%I64d",&a,&b,&c);
update(,,n,a,b,c);
}
else{
scanf("%d%d",&a,&b);
ans=query(,,n,a,b);
printf("%I64d\n",ans);
} }
return ;
}

POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)的更多相关文章

  1. POJ 3468 A Simple Problem with Integers (线段树多点更新模板)

    题意: 给定一个区间, 每个区间有一个初值, 然后给出Q个操作, C a b c是给[a,b]中每个数加上c, Q a b 是查询[a,b]的和 代码: #include <cstdio> ...

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

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

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

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

  5. poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解

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

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

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

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

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

  8. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

    A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

  9. POJ 3468 A Simple Problem with Integers //线段树的成段更新

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

  10. poj 3468 A Simple Problem with Integers 线段树加延迟标记

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

随机推荐

  1. 小米pad MI PAD 开发者选项、USB调试开启方式

    设置->关于平板->在MIUI版本一行连续点击7次. 之后就出现了开发者选项.可以开启USB调试等. 你能想到吗……??

  2. laravel步骤 (我是新手)

    1/需要一个wnmp之类的虚拟服务器 2/创建路由   php artisan make:route routes Route::group(['middleware' => ['web','a ...

  3. C#使用SocketAsyncEventArgs操作套接字的简单异步通讯

    SocketAsyncEventArgs是一个套接字操作的类,主要作用是实现socket消息的异步接收和发送,跟Socket的BeginSend和 BeginReceive方法异步处理没有多大区别,它 ...

  4. Ubontu使用技巧

    1. ctrl + alt + T  =>  打开命令行窗口 2. sudo su => 开启root权限 3. cd  => 打开文件夹 4. cd "Program F ...

  5. JavaScript创建对象的写法

    JavaScript 有Date.Array.String等这样的内置对象,功能强大使用简单,人见人爱,但在处理一些复杂的逻辑的时候,内置对象就很无力了,往往需要开发者自定义对象.   对象是什么 从 ...

  6. ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction

    测试库一条update语句报错:ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction mysql> ...

  7. const 的全面总结

    可以定义const常量 const int Max = 100; 2 便于进行类型检查 const常量有数据类型,而宏常量没有数据类型.编译器可以对前者进行类型安全检查,而对后者只进行字符替换,没有类 ...

  8. 虚拟机Linux下找不到/dev/cdrom

    问题描述: 笔者欲更新一下VMwareTools,结果发现虚拟机Linux上找不到设备"/dev/cdrom",当然也就不能通过命令"mount /dev/cdrom / ...

  9. iOS多线程GCD 研究

    Grand Central Dispatch (GCD)是Apple开发的一个多核编程的解决方法. dispatch queue分成以下三种: 1)运行在主线程的Main queue,通过dispat ...

  10. mac os快捷键

    选中一个词,使用control+command+d,可以启用词典 option+command+d,隐藏/显示 doc command + k terminal 清除历史记录 control + up ...