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 abc" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q ab" 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<stdio.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<stdlib.h>
using namespace std;
#define N 100100
#define Lson r<<1
#define Rson r<<1|1 struct node
{
int L,R;
long long sum,e;
int mid()
{
return (L+R)/; }
int len()
{
return R-L+;
}
}a[N<<]; void BuildTree(int r,int L,int R)
{
a[r].L=L;
a[r].R=R;
a[r].e=;
if(L==R)
{
scanf("%lld",&a[r].sum);
return;
}
BuildTree(Lson,L,a[r].mid());
BuildTree(Rson,a[r].mid()+,R);
a[r].sum=a[Lson].sum+a[Rson].sum;
}
void Down(int r)
{
a[Lson].sum+=a[Lson].len()*a[r].e;
a[Lson].e+=a[r].e;
a[Rson].sum+=a[Rson].len()*a[r].e;
a[Rson].e+=a[r].e;
a[r].e=;
}
void Add(int r,int L,int R,int e)
{
a[r].sum+=(R-L+)*e;
if(a[r].L==L && a[r].R==R)
{
a[r].e+=e;
return;
}
Down(r);
if(R<=a[r].mid())
Add(Lson,L,R,e);
else if(L>a[r].mid())
Add(Rson,L,R,e);
else
{
Add(Lson,L,a[r].mid(),e);
Add(Rson,a[r].mid()+,R,e);
}
}
long long Qurry(int r,int L,int R)
{
if(a[r].L==L && a[r].R==R)
{
return a[r].sum;
}
Down(r);
if(R<=a[r].mid())
return Qurry(Lson,L,R);
else if(L>a[r].mid())
return Qurry(Rson,L,R);
else
{
long long int a1=Qurry(Lson,L,a[r].mid());
long long int a2=Qurry(Rson,a[r].mid()+,R);
return a1+a2;
}
} int main()
{
int n,m,d,b,c;
while(scanf("%d %d",&n,&m)!=EOF)
{
BuildTree(,,n);
char s[];
while(m--)
{
scanf("%s",s);
if(s[]=='Q')
{
scanf("%d %d",&d,&b);
printf("%lld\n",Qurry(,d,b));
}
else
{
scanf("%d %d %d",&d,&b,&c);
Add(,d,b,c);
}
}
}
return ;
}

A Simple Problem with Integers-POJ3468的更多相关文章

  1. (线段树模板)A Simple Problem with Integers --POJ--3468

    链接: http://poj.org/problem?id=3468 代码: #include<stdio.h> #include<algorithm> #include< ...

  2. 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和

    poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...

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

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

  4. poj------(3468)A Simple Problem with Integers(区间更新)

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

  5. POJ3468 A Simple Problem with Integers 【段树】+【成段更新】

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

  6. POJ3468:A Simple Problem with Integers(线段树模板)

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

  7. poj3468 A Simple Problem with Integers (树状数组做法)

    题目传送门 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 1 ...

  8. POJ 3468 A Simple Problem with Integers (splay tree入门)

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

  9. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

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

  10. POJ 3468 A Simple Problem with Integers(线段树/区间更新)

    题目链接: 传送门 A Simple Problem with Integers Time Limit: 5000MS     Memory Limit: 131072K Description Yo ...

随机推荐

  1. Java文件上传(基础性)

    /** * * 上传文件 * */ public class FileUploadServlet2 extends HttpServlet { protected void doGet(HttpSer ...

  2. java 生成特定范围内的随机数

    /** * 生成[1, max]之间的随机数 */ public static Integer getRandomNumber(Integer max) { Random rd = new Rando ...

  3. 掌握Spark机器学习库-09.6-LDA算法

    数据集 iris.data 数据集概览 代码 package org.apache.spark.examples.examplesforml import org.apache.spark.ml.cl ...

  4. VC++模拟一次鼠标点击返回原位置

    HWND h; RECT r1; POINT p;//x,y void ONCE() { h=::FindWindow(NULL,"biaoti"); ::GetWindowRec ...

  5. [转载]iTOP-4418开发板Ubuntu系统烧写方法分享

    本文转自迅为论坛:http://topeetboard.com 开发平台:iTOP-4418开发板系统:Ubuntu 1. TF卡读写速度测试烧写 Ubuntu 对于 TF 卡的要求比较高,很多老旧的 ...

  6. Beta测试团队

    ---恢复内容开始--- Beta版本测试 这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass1/?page ...

  7. python的logging的简单使用

    用Python写代码的时候,在想看的地方写个print xx 就能在控制台上显示打印信息,这样子就能知道它是什么了,但是当我需要看大量的地方或者在一个文件中查看的时候,这时候print就不大方便了,所 ...

  8. 【东软实训】SQL函数

    SQL函数 SQL是用于访问和处理数据库的标准的计算机语言,我们所使用的的是Oracle SQL 一个数据库通常包含一个或多个表,每个表有一个名字表示,下图即为一个名为“emp”的表,接下来的操作都将 ...

  9. vs2008如何新建自己工程的环境变量(局部)和 Windows系统(全局). .

    在vs2008的Project->Property设置里经常会看到类似$(IntDir).$(OutDir).$(ProjectName) 的预定义宏.以vc2008为例,有时候我们在引用别的库 ...

  10. python 删除/查找重复项

    l = [1,2,3,2,1] # l = ['你','我','他','她','你'] for i in l: print("the %s has found %s" % (i, ...