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 ...
随机推荐
- Spark学习之基于MLlib的机器学习
Spark学习之基于MLlib的机器学习 1. 机器学习算法尝试根据训练数据(training data)使得表示算法行为的数学目标最大化,并以此来进行预测或作出决定. 2. MLlib完成文本分类任 ...
- Git ---创建和切换分支
······································································"天下武功,唯快不破" git分支: g ...
- 4 Visual Effects 视觉效果 读书笔记 第四章
4 Visual Effects 视觉效果 读书笔记 第四章 Well, circles and ovals are good, but how about drawing r ...
- CAS server 连接mysql的deployerConfigContext.xml配置
1.deployerConfigContext.xml配置 <?xml version="1.0" encoding="UTF-8"?> <b ...
- php常用的一些代码
1.获取用户真实ip function getIP() { if (getenv("HTTP_X_FORWARDED_FOR")) { // 这个提到最前面,作为优先级,nginx ...
- Which dispatch method would be used in Swift?
In this example: protocol MyProtocol { func testFuncA() } extension MyProtocol { func testFuncA() { ...
- swift class type isa-swizzling
class 是引用类型,生成的实例分布在 Heap(堆) 内存区域上,在 Stack(栈)只存放着一个指向堆中实例的指针.因为考虑到引用类型的动态性和 ARC 的原因,class 类型实例需要有一块单 ...
- Python+Selenium 自动化测试获取测试报告内容并发送邮件
这里封装一个send_mail()方法,用于测试完成后读取测试报告内容,并将测试结果通过邮件发送到接收人 # coding: utf-8 import smtplib from email.mime. ...
- Webpack 入门(一):安装 / 打包 / 命令行
一:安装webpack和基本环境搭建 新建一个工作的文件夹(我取的名字叫Webpack) 打开命令行,cd进入该文件夹 //初始化一下npm > E:\work\Webpack>npm i ...
- vue中滚动页面,改变样式&&导航栏滚动时,样式透明度修改
vue中滚动页面,改变样式&&导航栏滚动时,样式透明度修改.vue <div class="commonHeader" v-bind:class=" ...