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 ...
随机推荐
- 移动端rem
手机有很多尺寸的型号.使用rem来做为大小单位可以达到兼容的目的. 方法一:js测量手机尺寸,设置font-size:为手机屏幕width /10 + ‘px’.即10rem 为手机屏幕width. ...
- PHP(二)常用函数
数学函数 数组函数 字符串函数
- 全志R58平台调通s5k5eya(RAW+MIPI)
全志R58平台调通s5k5eya(RAW+MIPI) 2017/5/31 10:30 版本:V1.1 1.前期使用的是s5k5eyx的ISP的固件/tuning 文件 Y:\s5k5eya_r58_d ...
- R Programming week1-Subsetting
Subsetting There are a number of operators that can be used to extract subsets of R objects. [ alway ...
- AUSU 安装Win10注意事项
Win10 U盘原版安装 安装前在电脑店PE里用DiskGenius分区分区表类型:GUID勾选:创建新ESP分区 . 创建MSR分区 安装必须使用UEFI引导和GPT硬盘,否则会提示无法安装Win1 ...
- mac vim编辑器常用操作快捷方式
0 行首$ (shift+6)行尾gg 文首G(shift+g) 文尾A(Shift+a)文尾,并编辑ctrl+f 向上翻页ctrl+b 向下翻页ctrl+u 向上翻半页ctrl+d 向下翻半页数字+ ...
- 03C#数据类型
C#数据类型 值类型和引用类型区别 在C#语言中,值类型变量存储的是指定数据类型的数据,值类型变量的值(或实例)存储在栈(Stack)中,赋值语句是传递变量的值.引用类型(例如类就是引用类型)的实例, ...
- PHP 数组相加 和 数组合并array_merge
$arr1 = array("a"=>"PHP","b"=>"java","python" ...
- C89:应用篇 文件管理器
一.简介 用C语言做的一个文件管理器的轮子,因为经常开发中会用到跟数据流有关的,做完会放到Github上
- NPOI--------------.Net操作Excel初步使用(导出)
背景 因公司项目需要添加数据导出功能故此添加,找了几种方式发现该方式具有 无需依赖本机安装office环境,使用灵活等优点故采用此方式. 安装 Nuget 直接安装NPOI即可 使用方式 1.根据需要 ...