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 <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <iomanip>
#include <cstdlib>
#include <sstream>
using namespace std;
typedef long long LL;
const int INF=0x5fffffff;
const double EXP=1e-;
const int MS=; struct node
{
int l,r;
LL sum,inc; //特别注意这里
int mid()
{
return (l+r)/;
}
}nodes[*MS]; void creat(int root,int l,int r)
{
nodes[root].l=l;
nodes[root].r=r;
nodes[root].sum=;
nodes[root].inc=;
if(l==r)
return ;
creat(root<<,l,(l+r)/);
creat(root<<|,(l+r)/+,r);
} void insert(int root,int pos,int value)
{
if(nodes[root].l==nodes[root].r)
{
nodes[root].sum+=value;
return ;
}
nodes[root].sum+=value;
if(pos<=nodes[root].mid())
insert(root<<,pos,value);
else
insert(root<<|,pos,value);
} void add(int root,int a,int b,int c)
{
if(nodes[root].l==a&&nodes[root].r==b)
{
nodes[root].inc+=c;
return ;
}
nodes[root].sum+=c*(b-a+);
if(b<=nodes[root].mid())
add(root<<,a,b,c);
else if(a>nodes[root].mid())
add(root<<|,a,b,c);
else
{
add(root<<,a,nodes[root].mid(),c);
add(root<<|,nodes[root].mid()+,b,c);
}
} LL query(int root,int a,int b)
{
if(nodes[root].l>=a&&nodes[root].r<=b)
return nodes[root].sum+nodes[root].inc*(nodes[root].r-nodes[root].l+);
nodes[root].sum+=nodes[root].inc*(nodes[root].r-nodes[root].l+);
add(root<<,nodes[root].l,nodes[root].mid(),nodes[root].inc);
add(root<<|,nodes[root].mid()+,nodes[root].r,nodes[root].inc);
nodes[root].inc=;
if(b<=nodes[root].mid())
return query(root<<,a,b);
else if(a>nodes[root].mid())
return query(root<<|,a,b);
else
return query(root<<,a,nodes[root].mid())+query(root<<|,nodes[root].mid()+,b);
} int main()
{
int N,Q,x,y,z;
scanf("%d%d",&N,&Q);
creat(,,N);
for(int i=;i<=N;i++)
{
scanf("%d",&x);
insert(,i,x);
}
char cmd[MS];
while(Q--)
{
scanf("%s",cmd);
if(cmd[]=='Q')
{
scanf("%d%d",&x,&y);
printf("%lld\n",query(,x,y));
}
else
{
scanf("%d%d%d",&x,&y,&z);
add(,x,y,z);
}
}
return ;
}
 
 

A Simple Problem with Integers(线段树入门题)的更多相关文章

  1. 2018 ACMICPC上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节) 链接:https://ac.nowcoder.co ...

  2. POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)

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

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

  5. poj3468 A Simple Problem with Integers (线段树区间最大值)

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

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

  7. Poj 3468-A Simple Problem with Integers 线段树,树状数组

    题目:http://poj.org/problem?id=3468   A Simple Problem with Integers Time Limit: 5000MS   Memory Limit ...

  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 ——线段树 成段更新 懒惰标记

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

随机推荐

  1. Namespaces(命名空间)

    datastore,Blobstore,memcache一起为应用存储数据.这对于在全球范围内分割数据是有用的.比如,一个应用可以为多个公司服务,每个公司可以看到它自己的隔离的应用实例,没有公司可以看 ...

  2. CentOS安装XRDP实现远程桌面访问

    1.配置环境: yum install gcc pam-devel openssl-devel -y 2.进入指定目录下载并解压xrdp: 先安装 wget; sudo yum -y install ...

  3. Sql建表语句

    create table dbo.[Finance_CityInfo] ([CityId] int identity(1,1) not null , [City] nvarchar(20) not n ...

  4. Nodejs与Net 和SQL 交互利器Edge.js

    https://github.com/tjanczuk http://www.cnblogs.com/joylee/archive/2013/02/05/msnodesql.html edge.js这 ...

  5. JavaIO(03)字节流--OutputStream and InputStream

    IO概述:   IO流用来处理设备之间的数据传输 java对数据的操作是通过流的方式 java用于操作流的对象都在IO包中 流按操作数据分为两种:字节流与字符流(编码表) 流按流向分为:输入流,输出流 ...

  6. opencv 彩色图像亮度、对比度调节 直方图均衡化

    直接上代码: #include <Windows.h> #include <iostream>// for stand I/O #include <string> ...

  7. hbase 学习笔记一---基本概念

          说在前面,本文部分内容来源于社区官网经过适度翻译,部分根据经验总结,部分是抄袭网络博文,(不一一列举引用,在此致歉)一并列在一起,本文的目的,希望能总结出一些有用的,应该注意到的东西,基本 ...

  8. vc 获取当前时间 (zhuan)

    vc 获取当前时间(2010-02-10 11:34:32) http://wenku.baidu.com/view/6ade96d049649b6648d7475e.html 1.使用CTime类 ...

  9. VS2015中DataGridView的DataGridViewComBoboxCell列值无效及数据绑定错误的解决方法

    在VS2015中练习DataGridView的使用, 发现其中的DataGridViewComBoboxCell列存在着绑定数据库列后出现值无效的提示 根据网上的解决办法,添加了DataError后可 ...

  10. PostgreSQL的 initdb 源代码分析之十三

    继续分析: /* Bootstrap template1 */ bootstrap_template1(); 展开: 我这里读入的文件是:/home/pgsql/project/share/postg ...