题意:

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

分析:

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

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

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

代码:

#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与其他库的冲突

    1.jquery在其他库之后导入 第一种: jQuery.noConflict();//将变量$的控制权限交给其他类库,即将$的控制权让渡给其他类库 jQuery(function(){ jQuery ...

  2. iOS之NSAttributedString-------字符属性

    NSAttributedString 字符属性 字符属性可以应用于 attributed string 的文本中. NSString *const NSFontAttributeName;(字体) N ...

  3. jQuery动画处理

    $(selector).hide(speed,callback);隐藏 $(selector).show(speed,callback);显示 $(selector).toggle(speed,cal ...

  4. Apache与IIS端口冲突解决方法

    在安装Apache或者php集成环境包是经常会遇到Apache的80端口被占用导致无法正常启动Apache. Win7可以通过如下方法解决(如果坚持要使用80端口的话): 1.打开"控制面板 ...

  5. parsley.js正确使用姿势

    1.第一式 当然要先引用:parsley.js 2.第二式 页面中定义需要使用自定义校验,注意红色的地方,必须要使用小写,重要的问题说三遍,小写,小写 <form class="for ...

  6. CREATE OPERATOR CLASS - 定义一个新的操作符类

    SYNOPSIS CREATE OPERATOR CLASS name [ DEFAULT ] FOR TYPE data_type USING index_method AS { OPERATOR ...

  7. Duplicate fragment name ERROR Jetty Maven Plugin

    http://stackoverflow.com/questions/5802096/duplicate-fragment-name-error-jetty-maven-plugin 4down vo ...

  8. 如何把datetime类型字段修改为int类型

    如何把datetime类型字段修改为int类型 我有一个表为:table1 其中有一个datetime类型的字段  a    现在我想我想把字段a的类型改为int类型 当我执行以下命令时报如下的错误a ...

  9. 06网络通信udp-tcp、正则

    一. udp网络程序 1.    udp网络程序-发送数据 1)创建客户端套接字 2)发送/接收数据 3)关闭套接字 from socket import * # 1. 创建udp套接字 udp_so ...

  10. HTML 之 DOM文件对象模型

    文件对象模型 (DOM: Document Object Model) DOM 是 W3C定义的一种访问文档的标准. "The W3C Document Object Model (DOM) ...