POJ 3468A Simple Problem with Integers(线段树区间更新)
A Simple Problem with Integers
| Time Limit: 5000MS | Memory Limit: 131072K | |
| Total Submissions: 112228 | Accepted: 34905 | |
| Case Time Limit: 2000MS | ||
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 a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" 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 题意就是如题,对线段树的区间进行更新,可增可减可修改,并查询区间和,此题就是增,具体实现看代码吧.
AC代码:
Source Code
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
#define maxsize 100005
#define LL long long
LL num[maxsize];
struct node
{
LL l,r;
LL maxn,add;
int mid()
{
return (l+r)/;
}
} tree[maxsize<<];
void Build_Tree(LL root,LL l,LL r)
{
tree[root].l=l;
tree[root].r=r;
tree[root].add=;
if(l==r)
{
tree[root].maxn=num[l];
return ;
}
Build_Tree(root*,l,tree[root].mid());
Build_Tree(root*+,tree[root].mid()+,r);
tree[root].maxn=tree[root*].maxn+tree[root*+].maxn;
}
void Update(LL root,LL l,LL r,LL czp)
{
if(tree[root].l==l&&tree[root].r==r)
{
tree[root].add+=czp;
return ;
}
tree[root].maxn+=((r-l+)*czp);
if(tree[root].mid()>=r) Update(root<<,l,r,czp);
else if(tree[root].mid()<l) Update(root<<|,l,r,czp);
else
{
Update(root<<,l,tree[root].mid(),czp);
Update(root<<|,tree[root].mid()+,r,czp);
}
}
long long Query(LL root,LL l,LL r)
{
if(tree[root].l==l&&tree[root].r==r)
{
return tree[root].maxn+(r-l+)*tree[root].add;
}
tree[root].maxn+=(tree[root].r-tree[root].l+)*tree[root].add;
Update(root<<,tree[root].l,tree[root].mid(),tree[root].add);
Update(root<<|,tree[root].mid()+,tree[root].r,tree[root].add);
tree[root].add=;
if(tree[root].mid()>=r) return Query(root*,l,r);
else if(tree[root].mid()<l) return Query(root*+,l,r);
else
{
LL Lans=Query(root*,l,tree[root].mid());
LL Rans=Query(root*+,tree[root].mid()+,r);
return Lans+Rans;
}
}
int main()
{
/*ios::sync_with_stdio(false);*/
char str[];
LL m,n,a,b,c;
while(scanf("%lld%lld",&m,&n)!=EOF)
{
for(LL i=; i<=m; i++) scanf("%I64d",&num[i]);
Build_Tree(,,m);
for(LL i=; i<=n; i++)
{
scanf("%s%I64d%I64d",str,&a,&b);
if(str[]=='Q') printf("%I64d\n",Query(,a,b));
else if(str[]=='C')
{
scanf("%I64d",&c);
Update(,a,b,c);
}
}
}
return ;
}
POJ 3468A Simple Problem with Integers(线段树区间更新)的更多相关文章
- poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 75541 ...
- (简单) POJ 3468 A Simple Problem with Integers , 线段树+区间更新。
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- A Simple Problem with Integers 线段树 区间更新 区间查询
Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 115624 Accepted: 35897 Case Time Lim ...
- Poj 3468-A Simple Problem with Integers 线段树,树状数组
题目:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
- [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
- poj 3468 A Simple Problem with Integers 线段树区间更新
id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072 ...
- POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 67511 ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新)
题目地址:POJ 3468 打了个篮球回来果然神经有点冲动. . 无脑的狂交了8次WA..竟然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题. 区间更新就是加一个lazy标记,延迟标记, ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)
#include <iostream> #include <stdio.h> #include <string.h> #define lson rt<< ...
随机推荐
- OpenGLES.APPLE_texture_format_BGRA8888
OpenGL ES的扩展: APPLE_texture_format_BGRA8888 http://www.khronos.org/registry/gles/extensions/APPLE/AP ...
- DefaultSingletonBeanRegistry
DefaultSingletonBeanRegistry 这是 DefaultSingletonBeanRegistry 类的体系结构,由一个类一个责任的原则: AliasRegistry : 提供别 ...
- 【UXPA大赛企业专访】Mockplus:“设计替代开发”将成为现实
“过去,是‘设计服务于开发’,现在,我认为是‘设计驱动开发’,而在不远的将来,随着AI的落地.大数据和云计算能力的提升,‘设计替代开发’将成为现实.Mockplus也正在为此部署并行动.” 近日,UX ...
- linux 安装 rz sz 快速上传和下载文件
## ubuntu系统 apt install lrzsz
- 2018.08.16 洛谷P2029 跳舞(线性dp)
传送门 简单的线性dp" role="presentation" style="position: relative;">dpdp. 直接推一推 ...
- yii2 ActiveRecord的生命周期
AR的生命周期 http://www.yii-china.com/doc/guide/db_active_record.html 理解AR的生命周期对于你操作数据库非常重要.生命周期通常都会有些典型的 ...
- Linux命令:sed
简介 sed 是一种在线编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的 ...
- Java基础知识学习笔记(一)
理解面向对象: Java纯粹的面向对象的程序设计语言,主要表现为Java完全支持面向对象的三个基本特征:继承.封装.多态. Java程序的最小单位是类,类代表客观世界中具有某种特征的一类事物,这些类可 ...
- 学习前端的菜鸡对JS 的classList理解
classList 在早期的时候要添加,删除类 需要用className去获取,然后通过正则表达式去判断这个类是否存在. 代码上去会有点麻烦,现在有了classList 就方便了很多. ——————— ...
- 编写属于自己的linux命令
开篇: 问题和解决思路 在使用一些基础IDE时,工具经常会在我们建立特定文件时给我们一个已经有了一些特定代码的模板文件,但是在linux开发时,没有这样的IDE,怎么办?虽然代码量不是很多,但是能一次 ...