POJ 3468_A Simple Problem with Integers(线段树)
题意:
给定序列及操作,求区间和。
分析:
线段树,每个节点维护两个数据:
- 该区间每个元素所加的值
- 该区间元素和
可以分为“路过”该区间和“完全覆盖”该区间考虑。
代码:
#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
typedef long long ll;
//[l,r)
const int maxn = 300005;
ll sum[maxn], add[maxn];
int v[maxn];
void update(int a, int b, int x, int k, int l, int r)
{
if(a <= l && r <= b) add[k] += x;
else if(l < b && a < r){
sum[k] += (min(r,b) - max(l,a))*x;
update(a, b, x, k * 2 + 1, l, (l+r)/2);
update(a, b,x, k * 2 + 2, (l+r)/2, r);
}
}
ll query(int a, int b, int k, int l, int r)
{
if(a >= r|| b <= l) return 0;
else if(a <= l&&r <= b) return (r - l) * add[k] + sum[k];
else {
ll res = (min(b,r)-max(a,l)) * add[k];
res += query(a, b, k * 2 + 1, l, (l+r)/2);
res += query(a, b, k * 2 + 2, (l + r)/2, r);
return res;
}
}
int main (void)
{
int n, q;scanf("%d%d",&n,&q);
int a, b, c;
for(int i = 0; i < n; i++){
scanf("%d",&v[i]);
update(i, i + 1, v[i], 0, 0, n);
}
for(int i = 0; i < q; i++){
getchar();
if(getchar()=='C'){
scanf("%d%d%d", &a, &b, &c);
update(a-1, b, c, 0 , 0, n);
}else{
scanf("%d%d",&a, &b);
printf("%I64d\n",query(a - 1, b, 0, 0, n));
}
}
return 0;
}
写的时候还是磕磕绊绊,看来当初学的时候理解的并不好
POJ 3468_A Simple Problem with Integers(线段树)的更多相关文章
- Poj 3468-A Simple Problem with Integers 线段树,树状数组
题目:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
- POJ A Simple Problem with Integers 线段树 lazy-target 区间跟新
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 105742 ...
- POJ 3468A Simple Problem with Integers(线段树区间更新)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 112228 ...
- POJ 3468_A Simple Problem with Integers(树状数组)
完全不知道该怎么用,看书稍微懂了点. 题意: 给定序列及操作,求区间和. 分析: 树状数组可以高效的求出连续一段元素之和或更新单个元素的值.但是无法高效的给某一个区间的所有元素同时加个值. 不能直接用 ...
- POJ A Simple Problem with Integers | 线段树基础练习
#include<cstdio> #include<algorithm> #include<cstring> typedef long long ll; #defi ...
- 2018 ACMICPC上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节) 链接:https://ac.nowcoder.co ...
- poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal w ...
- [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 线段树区间加,区间查询和
A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...
- poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和(模板)
A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...
随机推荐
- UWP Windows10开发获取设备位置(经纬度)
1.首先要在UWP项目的Package.appxmanifest文件中配置位置权限,如下图所示: 2.Package.appxmanifest后选择第三个选项卡,勾选位置权限(Location) 打开 ...
- Java操作pdf: JarsperReport的简单使用
在企业级应用开发中,报表生成.报表打印下载是其重要的一个环节.除了 Excel 报表之外,PDF 报表也有广泛的应用场景. 目前世面上比较流行的制作 PDF 报表的工具如下: iText PDF :i ...
- 自学 iOS - 三十天三十个 Swift 项目 第二天
继续做仿造着别人的第二个 1.首先下载 一些字体 网上搜索 "造字工房" 2.把下载的相应字体文件放到工程之中,就Ok了 不多说 效果如下 可以下面这个方法 检索项目里面所有的字体 ...
- Java语法基础-static关键字
static关键字说明 “static方法就是没有this的方法.在static方法内部不能调用非静态方法,反过来是可以的.而且可以在没有创建任何对象的前提下,仅仅通过类本身来调用static方法.这 ...
- Jvisualvm--JAVA性能分析工具
JDK自带的JAVA性能分析工具.它已经在你的JDK bin目录里了,只要你使用的是JDK1.6 Update7之后的版本.点击一下jvisualvm.exe图标它就可以运行了. 这里是VisualV ...
- 洛谷 P1364 医院设置
题目描述 设有一棵二叉树,如图: 其中,圈中的数字表示结点中居民的人口.圈边上数字表示结点编号,现在要求在某个结点上建立一个医院,使所有居民所走的路程之和为最小,同时约定,相邻接点之间的距离为l.如上 ...
- 经常遇到的js兼容问题大总结----最全总结
001.获取滚动条滚动的距离 var sTop = document.documentElement.scrollTop || document.body.scrollTop 002.获取非行间样式 ...
- velocity(vm)模板引擎基本语法
for循环 #foreach($acc in $!{param.tools}) #set($count = $count + 1) <li custom-data="$!{acc.or ...
- Python3中assert断言
一般的用法是: assert condition 用来让程序测试这个condition,如果condition为false,那么raise一个AssertionError.逻辑上等于: if not ...
- ubuntu 16.04 数据库mysql安装与管理
1.安装mysql的客户端与服务器端 $>sudo apt-get install mysql-server mysql-client 2.管理服务 1.启动 $>sudo service ...