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

Description

You have N integers, A1, A2, ... , 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 A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+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.
分析:
线段树模板之区间增减更新 区间求和查询
写板子好爽啊!!!
code:
#include<stdio.h>
#include<iostream>
#include<vector>
#include <cstring>
#include <stack>
#include <cstdio>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include<string>
#include<math.h>
#define max_v 100005
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef long long LL;
LL sum[max_v<<],add[max_v<<];
struct node
{
int l,r;
int mid()
{
return (l+r)/;
}
}tree[max_v<<]; void push_up(int rt)//向上更新
{
sum[rt]=sum[rt<<]+sum[rt<<|];
}
void push_down(int rt,int m)//向下更新
{
if(add[rt])//若有标记,则将标记向下移动一层
{
add[rt<<]+=add[rt];
add[rt<<|]+=add[rt]; sum[rt<<]+=add[rt]*(m-(m>>));
sum[rt<<|]+=add[rt]*(m>>);
add[rt]=;//取消本层标记
}
}
void build(int l,int r,int rt)
{
tree[rt].l=l;
tree[rt].r=r;
add[rt]=; if(l==r)
{
scanf("%I64d",&sum[rt]);
return ;
} int m=tree[rt].mid();
build(lson);
build(rson);
push_up(rt);//向上更新
}
void update(int c,int l,int r,int rt)
{
if(tree[rt].l==l&&tree[rt].r==r)
{
add[rt]+=c;
sum[rt]+=(LL)c*(r-l+);
return ;
} if(tree[rt].l==tree[rt].r)
return ; push_down(rt,tree[rt].r-tree[rt].l+);//向下更新 int m=tree[rt].mid();
if(r<=m)
update(c,l,r,rt<<);
else if(l>m)
update(c,l,r,rt<<|);
else
{
update(c,l,m,rt<<);
update(c,m+,r,rt<<|);
}
push_up(rt);//向上更新
}
LL getsum(int l,int r,int rt)
{
if(tree[rt].l==l&&tree[rt].r==r)
return sum[rt]; push_down(rt,tree[rt].r-tree[rt].l+);//向下更新 int m=tree[rt].mid();
LL res=;
if(r<=m)
res+=getsum(l,r,rt<<);
else if(l>m)
res+=getsum(l,r,rt<<|);
else
{
res+=getsum(l,m,rt<<);
res+=getsum(m+,r,rt<<|);
}
return res;
}
int main()
{
int n,m;
while(~scanf("%d %d",&n,&m))
{
build(,n,);//1到n建树 rt为1 while(m--)
{
char str[];
int a,b,c;
scanf("%s",str);
if(str[]=='Q')
{
scanf("%d %d",&a,&b);
printf("%I64d\n",getsum(a,b,));
}else
{
scanf("%d %d %d",&a,&b,&c);
update(c,a,b,);
}
}
}
return ;
}
/*
区间更新:增减更新
区间查询:求和
*/

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

  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(线段树 成段增减+区间求和)

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

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

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

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

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

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

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

  10. poj 3468 A Simple Problem with Integers 线段树 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=3468 线段树模板 要背下此模板 线段树 #include <iostream> #include <vector> ...

随机推荐

  1. FFmpeg精确时间截取视频

    简介: 之前用到过FFmpeg截取过音频和视频发现,截取的视频文件时间不是很准确,今天便系统的学习了一下FFmpeg截取视频的知识 参考: https://zhuanlan.zhihu.com/p/2 ...

  2. flutter圆角效果的实现

    new Material( borderRadius: BorderRadius.circular(20.0), shadowColor: Colors.blue.shade200, elevatio ...

  3. Android 对话框(Dialog)

    Activities提供了一种方便管理的创建.保存.回复的对话框机制,例如 onCreateDialog(int), onPrepareDialog(int, Dialog), showDialog( ...

  4. 分布式文件系统比较出名的有HDFS  和 GFS

    分布式文件系统比较出名的有HDFS  和 GFS,其中HDFS比较简单一点.本文是一篇描述非常简洁易懂的漫画形式讲解HDFS的原理.比一般PPT要通俗易懂很多.不难得的学习资料. 1.三个部分: 客户 ...

  5. 0java之泛型解说

    1.集合中只能装入引用数据类型,不能装入基本数据类型.如,装入int类型的数值123会自动装箱. 2.开发人员装入集合的数据类型不确定,所以它被设计成可以装入所有的Object. 3.新的问题产生,装 ...

  6. centos7 安装mariadb最新版并配置

    打开http://mirrors.aliyun.com/,查找mariadb,然后拼装地址http://mirrors.aliyun.com/mariadb/yum打开,点开你想要的版本,选择你的操作 ...

  7. 跳过ssh在首次连接出现检查keys 的提示

    1.将需要登陆主机得公钥添加到known_hosts ssh-keyscan 192.168.77.129 192.168.77.130 >> /root/.ssh/known_hosts ...

  8. Apache服务器如何通过.htaccess文件设置防盗链?

    Apache服务器通过.htaccess文件设置防盗链 用户经常面对的一个问题就是服务器的流量问题,而站点文件被盗链是其中最为主要的部分.所谓盗链,是指其他网站直接链接我们网站上的文件,一般来说,盗链 ...

  9. 《鸟哥的Linux私房菜》Chapter11 20180726~20180806

    目录 1.认识Bash这个shell 1.1.硬件.核心与shell 1.2.系统的合法shell和/etc/shells功能 1.3.Bash shell的功能 1.3.1.命令修编功能 1.3.2 ...

  10. (1)构造方法和方法重载 (2)this关键字 (3)方法的传参和递归调用

    1.构造方法和方法重载如: Person p = new Person(); - 声明Person类型的引用p指向Person类型的对象 p.show(); - 调用名字为show()的成员方法 1. ...