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. 多元线性回归(Linear Regression with multiple variables)与最小二乘(least squat)

    1.线性回归介绍 X指训练数据的feature,beta指待估计得参数. 详细见http://zh.wikipedia.org/wiki/%E4%B8%80%E8%88%AC%E7%BA%BF%E6% ...

  2. 题目1069:查找学生信息(STL的map简单应用)

    题目描述: 输入N个学生的信息,然后进行查询. 输入:                        输入的第一行为N,即学生的个数(N<=1000) 接下来的N行包括N个学生的信息,信息格式如 ...

  3. RabbitMQ (四) 路由选择 (Routing) -摘自网络

    本篇博客我们准备给日志系统添加新的特性,让日志接收者能够订阅部分消息.例如,我们可以仅仅将致命的错误写入日志文件,然而仍然在控制面板上打印出所有的其他类型的日志消息. 1.绑定(Bindings) 在 ...

  4. 解开发者之痛:中国移动MySQL数据库优化最佳实践(转)

    开源数据库MySQL比较容易碰到性能瓶颈,为此经常需要对MySQL数据库进行优化,而MySQL数据库优化需要运维DBA与相关开发共同参与,其中MySQL参数及服务器配置优化主要由运维DBA完成,开发则 ...

  5. struts2+Hibernate4+spring3+EasyUI环境搭建之二:搭建spring

    三.搭建spring3 1.引入spring3依赖 <!-- spring3 --> <dependency> <groupId>org.springframewo ...

  6. POJ 2421 Constructing Roads (最小生成树)

    Constructing Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/D Description There ar ...

  7. Linux下MongoDB备份脚本

    #!/bin/bash today=`date +%Y%m%d` mongodump -h localhost -d salary -o /home/chzhao/mongobackup/$today ...

  8. ASP.NET Web Api返回对象类型为JSON还是XML

    在Umbraco平台上开发过程中,我用WebApi返回JSON result给前端 前端使用React调用这个web api来获取JSON result 我写的web api方法是返回JSON 类型的 ...

  9. Windows 下如何设置 只允许固定IP远程访问

    通过设置IP安全策略限制固定IP访问 说明: (1)以XP环境为例,步骤:先禁止所有IP,再允许固定IP访问. (2)配置过程中很多步骤图是重复的,一些没价值的图就省略了: (3)光看的话可能中间重复 ...

  10. MVC神韵---你想在哪解脱!(十六)

    MVC验证属性自动验证原理 也许有人会问,既然我们没有在C与V追加任何显示错误信息提示的代码,那么控制器或视图内部是如何生成这些显示错误信息提示的画面的.让我们揭开这么谜底吧!当在Movie类中追加了 ...