POJ3468A Simple Problem with Integers(区间加数求和 + 线段树)
题意:两种操作:一是指定区间的数全都加上一个数,二是统计指定区间的和
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int Max = ;
const int INF = 0x3f3f3f3f;
typedef long long LL;
struct node
{
int l,r;
LL inc; //记录这个区间所要增加的值
LL sum;
};
node tree[Max * ];
int num[Max + ];
int n,q;
void buildTree(int left, int right, int k)
{
tree[k].inc = ;
tree[k].tag = INF;
tree[k].l = left;
tree[k].r = right;
if(left == right)
{
tree[k].sum = num[left];
return ;
}
int mid = (left + right) / ;
buildTree(left, mid, k * );
buildTree(mid + , right, k * + );
tree[k].sum = tree[k * ].sum + tree[k * + ].sum;
}
void upDate(int left, int right, int k, int add)
{
if(tree[k].l == left && tree[k].r == right)
{
tree[k].inc += add; //仅仅记录一下该区间所要增加的值而已
//tree[k].tag = add;
return;
}
tree[k].sum += (right - left + ) * add; //对于这样的大区间,就要求出sum
int mid = (tree[k].l + tree[k].r) / ;
if(right <= mid)
{
upDate(left, right, k * , add);
}
else if(mid < left)
{
upDate(left, right, k * + , add);
}
else
{
upDate(left, mid, k * , add);
upDate(mid + , right, k * + , add);
}
}
LL Search(int left, int right, int k)
{
if(right < left)
return ;
if(left == tree[k].l && right == tree[k].r)
{
return tree[k].sum + (right - left + ) * tree[k].inc; //原来的和 + 增加的数的和
}
tree[k].sum += (tree[k].r - tree[k].l + ) * tree[k].inc; //跟新sum的值 ,
int mid = (tree[k].l + tree[k].r) / ;
upDate(tree[k].l, mid, k * , tree[k].inc);
upDate(mid + , tree[k].r, k * + , tree[k].inc);
tree[k].inc = ;
if(right <= mid)
{
return Search(left, right, k * );
}
else if(mid < left)
{
return Search(left, right, k * + );
}
else
{
return Search(left, mid, k * ) + Search(mid + , right, k * + );
}
}
int main()
{
scanf("%d%d", &n, &q);
for(int i = ; i <= n; i++)
scanf("%d", &num[i]);
buildTree(, n, );
char opt[];
int a,b,add;
while(q--)
{
scanf("%s", opt);
if(strcmp(opt, "Q") == )
{
scanf("%d%d", &a, &b); printf("%I64d\n", Search(a, b, ));
}
else
{
scanf("%d%d%d", &a, &b, &add);
upDate(a, b, , add);
}
}
return ;
}
POJ3468A Simple Problem with Integers(区间加数求和 + 线段树)的更多相关文章
- POJ-3468-A Simple Problem with Integers(区间更新,求和)-splay或线段树
区间更新求和 主要用来练习splay树区间更新问题 //splay树的题解 // File Name: 3468-splay.cpp // Author: Zlbing // Created Time ...
- POJ-3468-A Simple Problem with Integers(线段树 区间更新 区间和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 139191 ...
- POJ-3468A Simple Problem with Integers,线段数区间更新查询,代码打了无数次还是会出错~~
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Case Time L ...
- poj------(3468)A Simple Problem with Integers(区间更新)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 60745 ...
- 线段树:POJ3468-A Simple Problem with Integers(线段树注意事项)
A Simple Problem with Integers Time Limit: 10000MS Memory Limit: 65536K Description You have N integ ...
- 线段树:CDOJ1597-An easy problem C(区间更新的线段树)
An easy problem C Time Limit: 4000/2000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Pr ...
- poj3468A Simple Problem with Integers(线段树的区域更新)
http://poj.org/problem?id=3468 真心觉得这题坑死我了,一直错,怎么改也没戏,最后tjj把q[rt].lz改成了long long 就对了,真心坑啊. 线段树的区域更新. ...
- 【成端更新线段树模板】POJ3468-A Simple Problem with Integers
http://poj.org/problem?id=3468 _(:зゝ∠)_我又活着回来啦,前段时间太忙了写的题没时间扔上来,以后再说. [问题描述] 成段加某一个值,然后询问区间和. [思路] 讲 ...
- Poj3468-A Simple Problem with Integers(伸展树练练手)
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
随机推荐
- js时间倒计时
看了网上的其他的例子,觉得写的都有点复杂,不好理解,于是自己动手写了个. 本来想封装成jquery插件,但是觉得因为功能很简单,没有必要做成jquery插件,引用的时候不需要引入jqery库,这里直接 ...
- PHP 页面跳转方法
1.php header()函数跳转 PHP的header()函数非常强大,其中在页面url跳转方面也调用简单,使用header()直接跳转到指定url页面,这时页面跳转是302重定向: $url = ...
- centos 7 安装mysql
# wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm # rpm -ivh mysql-community- ...
- 3-cd 命令总结
- 屠龙之路_向恶龙Alpha进发_FirstDay
听说山的那边海的那边,出现了一头名为Alpha的恶龙,此龙无恶不作,还掠走了国王那漂酿的公主.少年很是气愤,大吼:"放开那女孩!!!",于是找到了志同道合的六位勇士,一起组成了屠龙 ...
- (Extjs)对于GridPanel的各种操作
刚才做了个有点特殊的需求,在某窗口关闭时,要把Gridpanel中的选择行清空,因为如果不清空,直接双击,就不能即时更新出来我想要的内容. 答案是:Grid.getSelectionModel().c ...
- linux系统数据落盘之细节
本文节选自这里,原文以mysql innodb系统为例,介绍了数据经过的各层级的buffer和cache,其它系统也有相似的原理,摘录于此. 3. VFS层 该层的缓冲都放在主机内存中,它的目的 ...
- 一个Activity掌握Design新控件 (转)
原文地址:http://blog.csdn.net/lavor_zl/article/details/51295364 谷歌在推出Android5.0的同时推出了全新的设计Material Desig ...
- Hibernate断网修改配置文件实现正常验证运行
hibernate.cfg.xml中声明部分: <!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Conf ...
- [转]java.sql.SQLException: 无效的列索引
原文地址:http://blog.sina.com.cn/s/blog_6bc811e401011a17.html java.sql.SQLException: 无效的列索引 “无效的列索引”其实是个 ...