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 ...
随机推荐
- [BZOJ2654]tree(二分+MST)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2654 分析:此题很奇葩,我们可以给所有白边加上一个权值mid,那么在求得的MST中白边 ...
- JavaScript学习笔记-循环输出菱形,并可菱形自定义大小
var Cen = 6;//定义菱形中部为第几行(起始值为0) //for循环输出菱形 document.write("<button onclick='xh()'>点我for循 ...
- CSS3——让最后一行显示省略号
代码如下: <!DOCTYPE html> <html> <head> <title></title> <meta charset=& ...
- Bete冲刺第七阶段
Bete冲刺第七阶段 今日工作: web: 新增通知处理接口 ios: 重写登录逻辑,添加创建行程填写.注册 POP界面 目前所遇问题: web: web目前进展顺利,暂时还没有遇到编码的问题. iO ...
- extjs基础 使用图标字体来美化按钮)
下载 Font Awesome 1.拷贝css 和 fonts 到build同级目录 2.需要在index.html中引入css文件 3.在main.js文件中添加 initComponent : f ...
- web单页应用(1)--第一个SPA
<!doctype html> <html> <head> <title>第一个SPA</title> <style type=&qu ...
- 延时程序执行Qt
有时候为了让程序暂停一下,不让它一直跑下去,可以使它进入循环结构中! 例如: #include <QCoreApplication> #include <qdebug.h> # ...
- javascript 学习一(概述+基本语法)
http://js.do/ 概述 JavaScript :脚本语言 诞生于1995年. javascript 1.0,发布于Netscape Navigator 2 @1996年3月 布兰登·艾奇( ...
- poj3694 缩点边双连通分量
Network Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 8669 Accepted: 3175 Descripti ...
- CGCDSSQ
这道题很妙啊.其实我们可以发现重要的不是起点和终点,重要的是个数,然后脑洞一下,可以递推.(我什么都没有想出来)假设我们已经知道了前面所有子串的gcd,那么我们可以用现在的a[i]和前面每个数求gcd ...