poj 3468 A Simple Problem with Integers 【线段树-成段更新】
题目: id=3468" target="_blank">poj 3468 A Simple Problem with Integers
题意:给出n个数。两种操作
1:l -- r 上的全部值加一个值val
2:求l---r 区间上的和
分析:线段树成段更新,成段求和
树中的每一个点设两个变量sum 和 num ,分别保存区间 l--r 的和 和l---r 每一个值要加的值
对于更新操作:对于要更新到的区间上面的区间,直接进行操作 加上 (r - l +1)* val 。
以下的区间标记num += val
对于求和操作。每次进行延迟更新。把num值分别更新到两个子区间。sum值更新,然后num值变为0
注意:这个题目会超int 。
所以
AC代码:
#include <iostream>
#include <algorithm>
#include <string>
#include <math.h>
#include <vector>
#include <cstring>
#include <cstdio>
using namespace std;
const long long N = 110000;
const long long inf = 0x3f3f3f3f;
struct Node
{
long long l,r;
long long num,sum;
};
Node tree[5*N];
long long a[N];
long long cnt ;
void build(long long o,long long l,long long r)
{
tree[o].l = l,tree[o].r = r;
tree[o].num = 0;
if(l==r)
{
tree[o].sum = a[cnt++];
return ;
}
long long mid = (l+r)/2;
build(o+o,l,mid);
build(o+o+1,mid+1,r);
tree[o].sum = tree[o+o].sum + tree[o+o+1].sum;
}
void push_update(long long o)
{
if(tree[o].num!=0)
{
tree[o].sum += tree[o].num * (tree[o].r-tree[o].l+1);
tree[o+o+1].num += tree[o].num;
tree[o+o].num += tree[o].num;
tree[o].num = 0;
}
}
void update(long long o,long long l,long long r,long long val)
{
if(l==tree[o].l && r == tree[o].r)
{
tree[o].num += val;
return ;
}
tree[o].sum += (val*(r-l+1)); //维护前面的
long long mid = (tree[o].l+tree[o].r) / 2;
if(r<=mid)
update(o+o,l,r,val);
else if(l>mid)
update(o+o+1,l,r,val);
else
{
update(o+o,l,mid,val);
update(o+o+1,mid+1,r,val);
}
}
long long query(long long o,long long l,long long r)
{
if(tree[o].l==l && tree[o].r == r)
{
return tree[o].sum+(r-l+1)*tree[o].num;
}
push_update(o); //维护后面的
long long mid = (tree[o].l+tree[o].r)/2;
if(r<=mid)
return query(o+o,l,r);
else if(l>mid)
return query(o+o+1,l,r);
else
return query(o+o,l,mid) + query(o+o+1,mid+1,r);
}
int main()
{
//freopen("Input.txt","r",stdin);
long long n,m;
while(~scanf("%lld%lld",&n,&m))
{
cnt = 0;
for(long long i=0;i<n;i++)
scanf("%lld",&a[i]);
build(1,1,n);
while(m--)
{
getchar();
char c;
long long x,y;
scanf("%c",&c);
scanf("%lld%lld",&x,&y);
//printf("*****************************************\n");
if(c=='Q')
{
printf("%lld\n",query(1,x,y));
}
else
{
long long val;
scanf("%lld",&val);
update(1,x,y,val);
}
}
}
return 0;
}
poj 3468 A Simple Problem with Integers 【线段树-成段更新】的更多相关文章
- POJ 3468 A Simple Problem with Integers (线段树成段更新)
题目链接:http://poj.org/problem?id=3468 题意就是给你一组数据,成段累加,成段查询. 很久之前做的,复习了一下成段更新,就是在单点更新基础上多了一个懒惰标记变量.upda ...
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- 【POJ】3468 A Simple Problem with Integers ——线段树 成段更新 懒惰标记
A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072K Case Time Limit:2000MS Descr ...
- POJ3468_A Simple Problem with Integers(线段树/成段更新)
解题报告 题意: 略 思路: 线段树成段更新,区间求和. #include <iostream> #include <cstring> #include <cstdio& ...
- 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 ...
- 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 ...
- 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 ...
- [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
- poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 75541 ...
- POJ 3468 A Simple Problem with Integers //线段树的成段更新
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 59046 ...
随机推荐
- Simditor 富文本编辑器
Simditor 是团队协作工具 Tower 使用的富文本编辑器. 相比传统的编辑器它的特点是: 功能精简,加载快速 输出格式化的标准 HTML 每一个功能都有非常优秀的使用体验 兼容的浏览器:IE1 ...
- HDU 1171 Big Event in HDU【01背包/求两堆数分别求和以后的差最小】
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- Python的程序结构[4] -> 函数/Function[1] -> 内建函数
内建函数 / Built-in Function or Method Python中有许多的内建函数(查看内建模块部分),此处将对内建函数进行介绍 内建函数 ord / built-in functi ...
- spoj - Distinct Substrings(后缀数组)
Distinct Substrings 题意 求一个字符串有多少个不同的子串. 分析 又一次体现了后缀数组的强大. 因为对于任意子串,一定是这个字符串的某个后缀的前缀. 我们直接去遍历排好序后的后缀字 ...
- [POI2013]Tower Defense Game
题目大意: 一个$n(n\le5\times10^5)$个点$m(m\le10^6)$条边的无向图,边权全为$1$,满足若一个标记点能覆盖与其距离不超过$1$的点,从中选取不超过$k$个点能将整张图覆 ...
- 使用gtest自动化测试并给出性能测试结果(windows 版本,版本平台也可以使用,但并没有做完整的测试)
/************************************************************* *使用gtest自动化测试 * ********************* ...
- 系统学习Linux建议
国内的专业Linux网站(GB) ChinaUnix Linux中国 实验楼: 免费提供了Linux在线实验环境,不用在自己机子上装系统也可以学习Linux,超方便实用!. 国内的专业Linux网站( ...
- Tomcat服务器多域名配置(转载)
Tomcat服务器多域名配置 我们来讲解下如何在Tomcat服务器上进行多域名配置: 也就是一个Tomcat跑多网站,这里用真实案例举例,比如我这个云主机需要运行两个网站: pan.java1234. ...
- Hash history cannot PUSH the same path; a new entry will not be added to the history stack
这个是reactr-router的一个提示,当前路由下的history不能push相同的路径.只有开发环境存在,生产环境不存在,目前还没看到官方有去掉的意思.看不惯的话可以采取一些方法关掉这个提示.具 ...
- js获取上传图片的尺寸大小
当上传图片时,有时候需要控制下上传图片的尺寸大小,需要给个提示 //获取图片的尺寸,控制尺寸大小 var reader = new FileReader(), img = new Image(); / ...