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. jstat命令-帮助优化java性能

    jstat命令使用 jstat命令可以查看堆内存各部分的使用量,以及加载类的数量.命令的格式如下: jstat [-命令选项] [vmid] [间隔时间/毫秒] [查询次数]

  2. GIS在石油行业中的应用

    在石油工业中,发现新的石油资源,取得竞争优势,是成功的关键之一.GIS系统能帮助评估潜在的石油资源,及时.准确.直观地定位油气资源的空间分布及其特征,以正确有效地开展部署勘探开发工作,占领市场先机. ...

  3. 阿里云服务器安装ss使用

    下载安装服务器版shadowsocks yum install epel-release yum update yum install python-setuptools m2crypto super ...

  4. RGB、YUV和YCbCr介绍【转】

    RGB: 就是常说的红(Red).绿(Green)和蓝(Blue),每个图像的像素点由RGB三个通道的值组成. YUV和YCbCr: YUV与RGB的转换: Y'= 0.299*R' + 0.587* ...

  5. 文档兼容性定义,使ie按指定的版本解析

    作为开发人员,特别是作为Web的前端开发人员 ,最悲催的莫过于要不断的,不断的去调试各种浏览器的显示效果,而这其中最让人头痛的莫过于MS下的IE系列浏览器,在IE系列中的调试我们将会发现没有一个是好伺 ...

  6. Farseer.net轻量级开源框架 中级篇:SQL执行报告

    导航 目   录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 中级篇: 数据库切换 下一篇:Farseer.net轻量级开源框架 中级篇: 探究ORM(M ...

  7. vue路由细节探讨

    1.使用router-link 不会让页面刷新,使用a标签会使页面刷新.2.router-link 里面的to="/路由地址" tag=""自定义标签" ...

  8. 【原创】webbluetoorh 在windows下无法显示搜索列表,在mac下正常的解决办法

    google webbluetooth在windows下不能弹出设备搜索列表提示“Web Bluetooth API is not available”,因为webbluetooth是google新推 ...

  9. java_tcp_简单示例

    package netProgram; import java.io.DataOutputStream; import java.io.IOException; import java.net.Ser ...

  10. ajax请求回数组数据,Vue页面数组没同步问题

    记录bug 为什么 ajax 获取到了 vm.$data.list 页面上却没有显示出来的? 代码 //页面 <tr v-for="item in list">{{ * ...