poj 3468 A Simple Problem with Integers(线段树+区间更新+区间求和)
题目链接:id=3468http://">http://poj.org/problem?
id=3468
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 83959 | Accepted: 25989 | |
Case Time Limit: 2000MS |
Description
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is
to ask for the sum of numbers in a given interval.
Input
The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.
Output
You need to answer all Q commands in order. One answer in a line.
Sample Input
10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4
Sample Output
4
55
9
15
Hint
Source
对于更新树是为了避免改动到最底下而导致超时问题。所以每次改动仅仅改动相相应的区间就可以。然后记录一个add。下次更新或者查询的时候,假设查到该节点,就把add直接加到子节点上去,在将add变为0,避免下次还会反复加。这样仅仅更新到查询的子区间,不须要再往下找了,所以时间复杂度为O(n),更新树和查询树都须要这样。
由于add不为0,该add从根一直加到了该节点,之前的都加过了,假设更新到时候不加到子节点。还要通过子节点更新当前节点,当前节点的sum值里面含有的add就会被“抹掉”,就不能保证正确性了。还须要注意的就是要用__int64。
#include <iostream>
#include <cstdio> using namespace std; #define LL __int64 struct node
{
int l,r;
LL sum;
LL add;
//int flag;//用来表示有几个加数
} s[100000*4]; void InitTree(int l,int r,int k)
{
s[k].l=l;
s[k].r=r;
s[k].sum=0;
s[k].add=0;
if (l==r)
return ;
int mid=(l+r)/2;
InitTree(l,mid,2*k);
InitTree(mid+1,r,2*k+1);
} void UpdataTree(int l,int r,LL add,int k)
{ if (s[k].l==l&&s[k].r==r)
{
s[k].add+=add;
s[k].sum+=add*(r-l+1);
return ;
}
if (s[k].add!=0)//加数为0就不须要改变了
{
s[2*k].add+=s[k].add;
s[2*k+1].add+=s[k].add;
s[2*k].sum+=s[k].add*(s[2*k].r-s[2*k].l+1);
s[2*k+1].sum+=s[k].add*(s[2*k+1].r-s[2*k+1].l+1);
s[k].add=0;
}
int mid=(s[k].l+s[k].r)/2;
if (l>mid)
UpdataTree(l,r,add,2*k+1);
else if (r<=mid)
UpdataTree(l,r,add,2*k);
else
{
UpdataTree(l,mid,add,2*k);
UpdataTree(mid+1,r,add,2*k+1);
}
s[k].sum=s[2*k].sum+s[2*k+1].sum;
} LL SearchTree(int l,int r,int k)
{
if (s[k].l==l&&s[k].r==r)
return s[k].sum;
if (s[k].add!=0)
{
s[2*k].add+=s[k].add;
s[2*k+1].add+=s[k].add;
s[2*k].sum+=s[k].add*(s[2*k].r-s[2*k].l+1);
s[2*k+1].sum+=s[k].add*(s[2*k+1].r-s[2*k+1].l+1);
s[k].add=0;
}
int mid=(s[k].l+s[k].r)/2;
if (l>mid)
return SearchTree(l,r,2*k+1);
else if (r<=mid)
return SearchTree(l,r,2*k);
else
return SearchTree(l,mid,2*k)+SearchTree(mid+1,r,2*k+1);
} int main()
{
int n,q;
LL w;
while (~scanf("%d%d",&n,&q))
{
InitTree(1,n,1);
for (int i=1; i<=n; i++)
{
scanf("%lld",&w);
UpdataTree(i,i,w,1);
}
for (int i=1; i<=q; i++)
{
char ch;
int a,b;
LL c;
getchar();
scanf("%c%d%d",&ch,&a,&b);
if (ch=='C')
{
scanf("%lld",&c);
UpdataTree(a,b,c,1);
}
else if (ch=='Q')
{
LL ans=SearchTree(a,b,1);
printf ("%lld\n",ans);
}
}
}
return 0;
}
poj 3468 A Simple Problem with Integers(线段树+区间更新+区间求和)的更多相关文章
- POJ 3468 A Simple Problem with Integers (线段树多点更新模板)
题意: 给定一个区间, 每个区间有一个初值, 然后给出Q个操作, C a b c是给[a,b]中每个数加上c, Q a b 是查询[a,b]的和 代码: #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 [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- POJ 3468 A Simple Problem with Integers //线段树的成段更新
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 59046 ...
- 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 线段树区间更新
id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072 ...
随机推荐
- 详细介绍idea实现javaweb项目登入注册(华东交通大学教务处信息管理系统)、模糊查询
详细介绍idea实现javaweb项目登入注册(华东交通大学教务处信息管理系统).模糊查询 1,创建数据库,我的用户名:root 密码:root,数据库名称:lianwei,表名:login 2,效果 ...
- Patch 21352635 - Database Patch Set Update 11.2.0.4.8
一.CPU和PSU 近日,将数据库从9.2.0.6升级到11.2.0.4后,发现11.2.0.4通过DBLINK访问其他的9i库时发生ORA-02072错误,通过Google找到解决方案,即升级到PS ...
- VS开发C语言系列(零)-VS2013写C语言错误汇总
错误代码 error C3861:调用函数前未引用 error C4996:调用不安全的函数 error C2668:重载函数不明确 error C3861:"文件名" 找不到标识 ...
- fresh_bank、、
最近新学习了一个bank系统来和大家分享一下,新人求罩! 破索式之_链子枪_ 废话不多说了直接本主题 如果我们要写出bank系统,就要先考虑这个问题:总共需要几个类? 既然是银行系统,那么必不可少的就 ...
- N的阶乘末尾有多少个零?
在创联ifLab的招新问答卷上看到这么一题 核心问题是: 求N!(N的阶乘)的末尾有多少个零? 由于在N特别大的时候强行算出N!是不可能的,所以肯定要另找方法解决了. 首先,为什么末尾会有0?因为2* ...
- spring+spring MVC+mybatis 框架搭建
1.新建一个javaWeb工程Test,创建时记得勾选web.xml文件. 2.导入需要的jar包,Mybatis所有的jar,spring所有的jar,mysql驱动包. 这里mybatis和spr ...
- Centos7搭建nginx并提供外网访问
搭建nginx之后,80端口,其他机器无法访问 查询端口是否开启 firewall-cmd --query-port=80/tcp 永久开放80端口 firewall-cmd --permanent ...
- JS DOM节点(当前标签和同级、父级、子级..之间的关系)
1. 通过顶层document节点获取 1) document.getElementById(elementId) //根据id获得 2) document.getElementsByNa ...
- 数组题汇总(python3)
题目主要来自<剑指offer>和LeetCode,用python3来写的代码. 1.二维数组的查找: 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列 ...
- vue移动端地址三级联动组件(一)
vue移动端地区三级联动 省,市,县.用的vue+mintUi 因为多级联动以及地区的规则比较多.正好有时间自己写了一个.有问题以及建议欢迎指出.涉及到dom移动,所以依赖vue+jquery.这边数 ...