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. 1.3……Eclipse下开发Android相关知识

    Android应用目录结构 src -------------------------------------------java原代码存放目录 gen ----------------------- ...

  2. sqlserver 删掉日志文件ldf以后 救命语句

    sqlserver 删掉日志文件ldf以后  救命步骤: 先新建一个新数据库, 删掉新建的 .mdb 用想要还原的mdb覆盖 执行下面的语句 ALTER DATABASE 'DB_Core' SET ...

  3. (R)?ex - A simple framework to simplify system administration and datacenter automation

    找工作-互联网招聘求职网-拉勾网 5-10年 (R)?ex - A simple framework to simplify system administration and datacenter ...

  4. homework-07

    终于等到了一次难度相对比较小的问题,只要读完提问题就好,但又不得不说自己真的很菜,且不说C++掌握的不好,连英文也比较差,导致读的非常吃力,不过我还是坚持读完了四篇英文文章,大致意思是可以了解的,但是 ...

  5. Delphi实例-IdTCPServer和IdTCPClient的使用(支持文件发送)

    相关资料: http://blog.csdn.net/earbao/article/details/46514313 结果注意: 1.Use IdContext.IdGlobal 这两个单元2.不能使 ...

  6. Spring入门(4)-注入Bean属性

    Spring入门(4)-注入Bean属性 本文介绍如何注入Bean属性,包括简单属性.引用.内部Bean.注入集合等. 0. 目录 注入简单值 注入引用 注入内部Bean 装配集合 装配空值 使用命名 ...

  7. php编程冒泡排序

    <?//冒泡排序法 function bubble_sore($array) { $count = count($array); if ($count < 0) { return fals ...

  8. InnoDB与MyISAM的区别

    MyISAM 和 InnoDB 讲解 InnoDB和MyISAM是许多人在使用MySQL时最常用的两个表类型,这两个表类型各有优劣,视具体应用而定.基本的差别为:MyISAM类型不支持事务处理等高级处 ...

  9. rop框架中@ServiceMethod注解属性

    @ServiceMethod 属性 method :代码服务方法名version :表 示 版 本 号 group:服务分组名.服务的分组没有特殊的意义,您可以为服务定义一个分组,以便在事件监听器.服 ...

  10. UI进阶 KVO

    KVO:(Key-Value-Observer)键值观察者,是观察者设计模式的一种具体实现 KVO触发机制:一个对象(观察者),监测另一对象(被观察者)的某属性是否发生变化,若被监测的属性发生的更改, ...