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 ...
随机推荐
- MD5算法的C语言实现
1 #include <stdio.h> #include <stdint.h> #include <stdlib.h> #include <string.h ...
- SQLServer数据导入Mongodb
一.思路 MongoVUE免费版支持MySQL导入Mongo,所以思路是SQLServer导入MySQL,再从MySQL导入Mongo. 二.准备 1,安装mysql数据库(我用的是WAMP,集成my ...
- 由Nullable模式想到的ToString的扩展
虽然关于null的一切争论永不停息,但根据实际开发经历,很多时候需要判断无聊的null,并且有些的判断是可有可无的,尤其是在表现层. string e = null; if (e != null) { ...
- git flow的使用
简介 Gitflow工作流程围绕项目发布定义了严格的分支模型.尽管它比Feature Branch Workflow更复杂一些,但它也为管理更大规模的项目提供了坚实的框架. 与Feature Bran ...
- SpringMVC对日期类型的转换
在做web开发的时候,页面传入的都是String类型,SpringMVC可以对一些基本的类型进行转换,但是对于日期类的转换可能就需要我们配置. 1.如果查询类使我们自己写,那么在属性前面加上@Date ...
- Ceph浅析”系列之四——Ceph的结构
本文将从逻辑结构的角度对Ceph进行分析. Ceph系统的层次结构 Ceph存储系统的逻辑层次结构如下图所示[1]. Ceph系统逻辑层次结构 自下向上,可以将Ceph系统分为四个层次: (1)基础存 ...
- 美发屋App-业余爱好
出于个人爱好, 自行设计了一款APP,由于时间有限,APP目前只做了3天,现大四,急求一份实习工作,月薪3K左右即可! 软件UI设计到编码,全部又我一人完成,所以工作量比较大 底部采用·Fragmen ...
- 升級 Centos 6.5 的 php 版本
升級 Centos 6.5 的 php 版本 待會再看 Centos 6.5 的 php 預設是用 5.3.3 這個版本號 最近想要改用 Laravel 4.1 發現需要 5.3.7 才能用,所以 ...
- LRU设计
list是双向链表,map保存key对应到list中的迭代器的位置,list保存<key,value> class LRUCache{ public: LRUCache(int capac ...
- 2016年第2周读书笔记与工作笔记 scrollIntoView()与datalist元素
这一周主要是看了html5网页开发实例与javascript 高级程序设计,供以后翻阅查找. html5网页开发实例第1章与第二章的2.1部分: 第1章内容: html5在w3c的发展史. 浏览器的 ...