题意:

给定序列及操作,求区间和。

分析:

线段树,每个节点维护两个数据:

  • 该区间每个元素所加的值
  • 该区间元素和

可以分为“路过”该区间和“完全覆盖”该区间考虑。

代码:

#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(线段树)的更多相关文章

  1. Poj 3468-A Simple Problem with Integers 线段树,树状数组

    题目:http://poj.org/problem?id=3468   A Simple Problem with Integers Time Limit: 5000MS   Memory Limit ...

  2. POJ A Simple Problem with Integers 线段树 lazy-target 区间跟新

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 105742 ...

  3. POJ 3468A Simple Problem with Integers(线段树区间更新)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 112228 ...

  4. POJ 3468_A Simple Problem with Integers(树状数组)

    完全不知道该怎么用,看书稍微懂了点. 题意: 给定序列及操作,求区间和. 分析: 树状数组可以高效的求出连续一段元素之和或更新单个元素的值.但是无法高效的给某一个区间的所有元素同时加个值. 不能直接用 ...

  5. POJ A Simple Problem with Integers | 线段树基础练习

    #include<cstdio> #include<algorithm> #include<cstring> typedef long long ll; #defi ...

  6. 2018 ACMICPC上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节) 链接:https://ac.nowcoder.co ...

  7. 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 ...

  8. [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]

    A Simple Problem with Integers   Description You have N integers, A1, A2, ... , AN. You need to deal ...

  9. 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 ...

  10. 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 ...

随机推荐

  1. Jquery操作常用表单元素

    由于对前端的东西不是很熟练,导致jquery操作表单的东西总是忘记,每次用都要查一下,效率低下,记录下来,以便下次使用. CheckBox checkbox改变事件 $('#IsAllSearch') ...

  2. testlink 从1.8.5 升级到 1.9.8

    step1:备份原 1.8.5 的数据库.   step2:分别下载 1.9.0 / 1.9.3 / 1.9.8 的安装包.   step3:分别解压 1.9.0 / 1.9.3 / 1.9.8 成3 ...

  3. txt通过 vb编程导入 excel

    Private Sub CommandButton1_Click() '按钮触发, '按钮触发,目前支持ASNI文本的数据, 单行 fileToOpen = Application.GetOpenFi ...

  4. 迅为4418开发板Qt移植移动4G模块第二部分

    第一部分: http://www.cnblogs.com/topeet/p/6509248.html 第二部分: 5.ping不通域名一般是DNS没有设置对造成的.在etc下有一个文件resolv.c ...

  5. Which dispatch method would be used in Swift?

    In this example: protocol MyProtocol { func testFuncA() } extension MyProtocol { func testFuncA() { ...

  6. 网络爬虫之框架(Scrapy)

    Scrapy爬虫框架 爬虫框架是实现爬虫功能的一个软件结构和功能组件集合. 爬虫框架是一个半成品,能够帮助用户实现专业网络爬虫. Scrapy爬虫框架结构:

  7. 防止asp.net连续点击按钮重复提交

    1.在Page_Load中添加如下代码: protected void Page_Load(object sender, EventArgs e) { this.btnEdit.Attributes[ ...

  8. C#面试问题及答案

    1.遇到高并发的问题如何解决? 优化SQL语句 多线程 分布式服务器 集群 拆表2.Dictionary和ConurrentDictionary的区别? 后者是线程安全的 前者适用于单线程3.Dict ...

  9. NET使用SuperSocket完成TCP/IP通信

    1)为什么使用SuperSocket? 性能高,易上手.有中文文档,我们可以有更多的时间用在业务逻辑上,SuperSocket有效的利用自己的协议解决粘包 2)SuperSocket的协议内容? 命令 ...

  10. python compare with other language

    java http://dirtsimple.org/2004/12/python-is-not-java.htmlhttp://twistedmatrix.com/users/glyph/rant/ ...