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 ...
随机推荐
- 初学Ajax
AJAX即“Asynchronous Javascript And XML”(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术. AJAX = 异步 JavaScript和 ...
- 谷歌的 I/O 2019,究竟推出了什么新特性?
前言 昨天,也即赶在微软 Build 2019 的第二天,一年一度的2019年 Google I/O大会在美国如期举行,Google I/O 2019全纪录:AI惊艳,Android Q真香,包括两款 ...
- javaEE web 系统安装时自定义初始化
通常JavaWeb项目在第一次启动时我们需要做一些初始化工作,比如:初始化一个管理员的登录账户和密码,配置缓存.定时任务等,这些操作可以通过手工修改数据库完成,但是容易出错且繁琐,而且也很麻烦.如果这 ...
- 解决hibernate对Sql Server分页慢的问题
一.hibernate分页 hibernate对MsSql的伪分页 分页是web项目中比不可少的一个功能,数据量大的时候不能全部展示必然要用到分页技术.相信大家对hibernate中的分页都不陌生: ...
- 浅谈Key-value 存储——SILT
摘要:本文以文章SILT: A Memory Efficient High Performance Key-Value Store 为基础,探讨SILT存储系统是如何实现内存占用低和高性能的设计目标, ...
- 【译】x86程序员手册37-第10章 初始化
Chapter 10 Initialization 第10章 初始化 After a signal on the RESET pin, certain registers of the 80386 a ...
- SCCM大致安装过程,参考前辈教程完成部署
本安装sccm主站点服务器准备 参考:http://stephen1991.blog.51cto.com/8959108/1529864 1. 准备三台服务器 ,注:所有服务器需要安装 .net3. ...
- jQuery radio 选中提示
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- css3 平行四边形 、大括弧
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 0.ssm web项目中的遇到的坑
1.自定义的菜单,href为项目的相对路径,即: : 点击一个菜单,后再点击另一个菜单,然后发现浏览器地址栏的链接是在上一个链接后面拼接的,也就报错了. 解决办法: 每一个菜单的href前增加&quo ...