A Simple Problem with Integers-POJ3468
Description
You have N integers, A1, A2, ... , 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 A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C abc" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q ab" means querying the sum of Aa, Aa+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
#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的更多相关文章
- (线段树模板)A Simple Problem with Integers --POJ--3468
链接: http://poj.org/problem?id=3468 代码: #include<stdio.h> #include<algorithm> #include< ...
- 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和
poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...
- poj3468 A Simple Problem with Integers (线段树区间最大值)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92127 ...
- poj------(3468)A Simple Problem with Integers(区间更新)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 60745 ...
- POJ3468 A Simple Problem with Integers 【段树】+【成段更新】
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 57666 ...
- POJ3468:A Simple Problem with Integers(线段树模板)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 149972 ...
- poj3468 A Simple Problem with Integers (树状数组做法)
题目传送门 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 1 ...
- POJ 3468 A Simple Problem with Integers (splay tree入门)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 47944 ...
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- POJ 3468 A Simple Problem with Integers(线段树/区间更新)
题目链接: 传送门 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Description Yo ...
随机推荐
- 解决okHttp使用https抛出stream was reset: PROTOCOL_ERROR的问题
昨天在做Android接口调用的时候,api接口是https的,用okhttp抛出: okhttp3.internal.http2.StreamResetException: stream was r ...
- 使用 ServerSocket 进行文件上传,以及用Tomcat启动ServerSocket时,会卡死解决
服务器端代码 import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.IOExcept ...
- 从GridView中直接导出数据到Excel文件 处理导出乱码 类型“GridView”的控件“XXXX”必须放在具有 runat=server 的窗体标记内。”的异常
导出到Excel方法: <span style="color: rgb(0, 0, 255);">public</span> <span style= ...
- PC端浏览器定位
第一: PC端浏览器定位(纯前端)浏览器定位 :这里用了两种 ,一种是Html5自带的方法 另一种是引用了百度api ,百度api 的使用有三种:1 浏览器定位2 ip定位3 SDK辅助定位引用百度 ...
- PDO 错误处理模式
异常模式: $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 这个模式需要配合 try 使用 :一旦出错,就会: 1. ...
- 打开windows服务
#include <winsvc.h> void CXXXDlg::ServiceRun() { SERVICE_STATUS ssStatus; //获得ServiceControl M ...
- 离线缓存 application cache
1. 什么是离线缓存: 离线缓存可以将站点的一些文件缓存到本地,它是浏览器自己的一种机制,将需要的文件缓存下来,以便后期即使没有连接网络,被缓存的页面也可以展示. 例子:比如我们在手机或电脑上访问一个 ...
- Beam Search
Q: 什么是Beam Search? 它在NLP中的什么场景里会⽤到? 传统的广度优先策略能够找到最优的路径,但是在搜索空间非常大的情况下,内存占用是指数级增长,很容易造成内存溢出,因此提出了beam ...
- IOS沙盒(sandbox)机制和文件操作
IOS学习之IOS沙盒(sandbox)机制和文件操作 作者:totogo2010 ,发布于2012-9-21,来源:CSDN 目录: IOS学习之IOS沙盒(sandbox)机制和文件操作( ...
- assert.notEqual()
浅测试,使用不等于比较运算符(!=)比较. const assert = require('assert'); assert.notEqual(1, 2); // OK assert.notEqual ...