A Simple Problem with Integers

Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 110077   Accepted: 34272
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
题意:n个数字,q次操作,每次可以区间增减,或者查询区间的和
 #include<cstdio>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define MAXN 100100
#define LL long long
LL sum[MAXN<<];
LL add[MAXN<<];
int n,q;
char s[];
void putup(int rt)
{
sum[rt] = sum[rt<<]+sum[rt<<|];
}
void putdown(int rt,int m)
{
if (add[rt])
{
add[rt<<] += add[rt];
add[rt<<|] += add[rt];
sum[rt<<] += (m-(m>>))*add[rt];
sum[rt<<|] += (m>>)*add[rt];
add[rt] = ;
}
}
void build(int l,int r,int rt)
{
add[rt] = ;
if (l==r)
{
scanf("%lld",&sum[rt]);
return ;
}
int m = (l+r)>>;
build(lson);
build(rson);
putup(rt);
}
void update(int l,int r,int rt,int L,int R,int c)
{
if (L<=l && r<=R)
{
add[rt] += c;
sum[rt] += (LL)c*(r-l+);
return ;
}
putdown(rt,r-l+);
int m = (l+r)>>;
if (L<=m) update(lson,L,R,c);
if (R>m) update(rson,L,R,c);
putup(rt);
}
LL query(int l,int r,int rt,int L,int R)
{
if (L<=l && r<=R)
{
return sum[rt];
}
putdown(rt,r-l+);
LL ret = ;
int m = (l+r)>>;
if (L<=m) ret += query(lson,L,R);
if (R>m) ret += query(rson,L,R);
return ret;
}
int main()
{
scanf("%d%d",&n,&q);
build(,n,);
while (q--)
{
int x,y,z;
scanf("%s",s);
if (s[]=='C')
{
scanf("%d%d%d",&x,&y,&z);
update(,n,,x,y,z);
}
else
{
scanf("%d%d",&x,&y);
printf("%lld\n",query(,n,,x,y));
}
}
return ;
}
 

3468-A Simple Problem with Integers 线段树(区间增减,区间求和)的更多相关文章

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

  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 [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

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

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

  6. 【POJ】3468 A Simple Problem with Integers ——线段树 成段更新 懒惰标记

    A Simple Problem with Integers Time Limit:5000MS   Memory Limit:131072K Case Time Limit:2000MS Descr ...

  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 线段树区间更新

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

  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. 团队第四次SCrum

    scrum 第四次冲刺 一.项目目的        为生活在长大的学生提供方快捷的生活服务,通过帖子发现自己志同道合的朋友,记录自己在长大点滴.本项目的意义在于锻炼团队的scrum能力,加强团队合作能 ...

  2. { ($0, Resolver($0.box)) }(Promise<T>(.pending)):闭包的定义与执行合一

    public class func pending() -> (promise: Promise<T>, resolver: Resolver<T>) { return ...

  3. auto create statistic script

    --生成用户创建的statistic脚本 select object_name(stout.object_id), stout.name, 'CREATE STATISTICS '+ stout.na ...

  4. 【[USACO15FEB]审查(黄金)Censoring (Gold)】

    从原来的单串匹配变成了多串匹配 好像也没什么特别不一样的地方 原来的做法是搞一个栈,之后一旦匹配到就往前弹栈 做法也一样 但是在\(AC\)自动机上暴力跳\(fail\)是要\(T\)的 我们并没有必 ...

  5. 实验吧之损坏的U盘

    1.首先用binwalk查看里面的内容,发现里面有Zip文件. 要想把Zip文件弄出来有两种方法: 一是用虚拟机里面的foremost+文件名 然而,在终端中已经见到二零password文件夹,然而我 ...

  6. 2018.11.10 Mac设置Eclipse的 .m2文件夹是否可见操作&&Mac系统显示当前文件夹的路径设置

    第一行就是设置为可见的记得要重启Finder不然是没有效果的 第二行就是设置为不可见的 打开"终端"(应用程序->实用工具),输入以下两条命令: defaults write ...

  7. GroundPlaneEstimator.cpp解读

    GroundPlaneEstimator域下的compute函数,就相当于整个cpp的主函数,也体现了整个调用过程,先执行compute_v_disparity_data,再compute_v_dis ...

  8. json 二进制传输方案

    json 传输二进制数组方案 json 是一种很简洁的协议,但可惜的是,它只能传递基本的数型(int,long,string等),但不能传递byte类型.如果想要传输图片等二进制文件的话,是没办法直接 ...

  9. P1171 售货员的难题 暴力dp

    题面 著名的TSP问题,NPC问题 对于数据大的情况,我们可以使用一系列近似算法进行寻找解. 对于数据规模小的情况,我们可以直接暴力dp 一开始写了一个dfs,然后就被n=20的数据卡爆了 #incl ...

  10. Golang 字符串转URLCode

    Golang 字符串转URLCode 最近因调用gitlab API,在生成某些字符串的时候直接请求 gitlab API 失败, url如下: keysURL := "http://192 ...