A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 79137   Accepted: 24395
Case Time Limit: 2000MS

Description

You have N integers, A1A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.

Input

The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+1, ... , Ab.

Output

You need to answer all Q commands in order. One answer in a line.

Sample Input

10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4

Sample Output

4
55
9
15

Hint

The sums may exceed the range of 32-bit integers.
 
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define MAX 100000+10
using namespace std;
long long add[MAX<<2];
long long sum[MAX<<2];
void pushup(int o)
{
sum[o]=sum[o<<1]+sum[o<<1|1];
}
void pushdown(int o,int m)
{
if(add[o])
{
add[o<<1]+=add[o];
add[o<<1|1]+=add[o];
sum[o<<1]+=add[o]*(m-(m>>1));
sum[o<<1|1]+=add[o]*(m>>1);
add[o]=0;
}
}
void gettree(int o,int l,int r)
{
add[o]=0;
if(l==r)
{
scanf("%lld",&sum[o]);
return ;
}
int mid=(l+r)>>1;
gettree(o<<1,l,mid);
gettree(o<<1|1,mid+1,r);
pushup(o);
}
void update(int o,int l,int r,int L,int R,int val)
{
if(L<=l&&R>=r)
{
add[o]+=val;
sum[o]+=val*(r-l+1);
return ;
}
pushdown(o,r-l+1);
int mid=(l+r)>>1;
if(L<=mid)
update(o<<1,l,mid,L,R,val);
if(R>mid)
update(o<<1|1,mid+1,r,L,R,val);
pushup(o);
}
long long find(int o,int l,int r,int L,int R)
{
if(L<=l&&R>=r)
{
return sum[o];
}
pushdown(o,r-l+1);
long long ans=0;
int mid=(l+r)>>1;
if(L<=mid)
ans+=find(o<<1,l,mid,L,R);
if(R>mid)
ans+=find(o<<1|1,mid+1,r,L,R);
return ans;
}
int main()
{
int n,m,i,j;
int a,b,c;
char d[12];
while(scanf("%d%d",&n,&m)!=EOF)
{
gettree(1,1,n);
for(i=1;i<=m;i++)
{
scanf("%s%d%d",d,&a,&b);
if(d[0]=='Q')
printf("%lld\n",find(1,1,n,a,b));
else
{
scanf("%d",&c);
update(1,1,n,a,b,c);
}
}
}
return 0;
}

  

poj 3468 A Simple Problem with Integers【线段树区间修改】的更多相关文章

  1. POJ 3468 A Simple Problem with Integers 线段树区间修改

    http://poj.org/problem?id=3468 题目大意: 给你N个数还有Q组操作(1 ≤ N,Q ≤ 100000) 操作分为两种,Q A B 表示输出[A,B]的和   C A B ...

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

  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   Description You have N integers, A1, A2, ... , AN. You need to deal ...

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

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

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

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

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

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

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

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

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

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

    #include <iostream> #include <stdio.h> #include <string.h> #define lson rt<< ...

随机推荐

  1. Mock相关收集

    MockMVC+Mockito http://www.cnblogs.com/syxchina/p/4150879.html Spring中使用Mockito http://www.cnblogs.c ...

  2. 解决IE6下不支持 png24的透明图片问题

    常用的两种解决方案: 第一:使用IE滤镜解决 关键代码: css代码  _background:none;_filter:progid:DXImageTransform.Microsoft.Alpha ...

  3. php练习3——猜拳游戏,评委打分问题

    用户与计算机猜拳 程序caiQuan.html和caiQuan.php: 结果: 评委打分问题,去掉一个最低分和最高分,求平均分,并找出最低分和最高分对应第几个评委,    再找出最佳评委(打分最接近 ...

  4. 什么是马甲APP?怎么用马甲APP导流

    一.什么是马甲APP? 马甲APP指的是为了让认识你的人猜不到,在常用的用户名外再注册的其他名字的APP. 二.马甲APP与真实APP的区别是什么?相同的地方是什么? 1.应用名称不一样. 2.关键词 ...

  5. C连接MySQL数据库开发之Linux环境完整示例演示(增、删、改、查)

    一.开发环境 ReadHat6.3 32位.mysql5.6.15.gcc4.4.6 二.编译 gcc -I/usr/include/mysql -L/usr/lib -lmysqlclient ma ...

  6. bzoj 3720: Gty的妹子树 块状树

    3720: Gty的妹子树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 412  Solved: 153[Submit][Status] Descr ...

  7. 想学ps的,这全是精华,拿走不谢!!!

  8. csu 10月 月赛 H 题 A Very Hard Problem

    Description CX老湿经常被人黑,被黑得多了,自己也就麻木了.于是经常听到有人黑他,他都会深情地说一句:禽兽啊! 一天CX老湿突发奇想,给大家出了一个难题,并且声称谁能够准确地回答出问题才能 ...

  9. Android 观察系统中短信内容的变化(内容观察者)

    //内容观察者(如果系统的短信发生了变化,比如刚获取一条短信,那么将触发onChange方法) ContentResolver contentResolver = getContentResolver ...

  10. ANDROID_MARS学习笔记_S04_005_用sing-post向腾讯微博发一条信息

    一.代码流程 1.组织好sign-post需要的token,secrect 2.组织好发微博需要的信息 3.用sign-post进行签名 4.把签名结果从header中拿出来,转成entity,用ht ...