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 ...
随机推荐
- vue学习记录(一)—— vue开发调试神器vue-devtools安装
网上有些贴子少了至关重要的一步导致我一直没装上, 切记!!install后还需build,且install和build都在vue-devtools文件夹内执行 github下载地址 点击跳转 具体步骤 ...
- PHP电影小爬虫(2)
学习了别人的爬虫后自己改的一个,算是又回顾了一下php的使用 我们来利用simple_html_dom的采集数据实例,这是一个PHP的库,上手很容易.simple_html_dom 可以很好的帮助我们 ...
- Android -----listView的重要属性
android:transcriptMode="alwaysScroll" android:cacheColorHint="#00000000" android ...
- 高斯消元_HihoCoderOffer6_03
题目3 : 图像算子 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在图像处理的技术中,经常会用到算子与图像进行卷积运算,从而达到平滑图像或是查找边界的效果. 假设原图 ...
- favourite和favorite的区别
同一个词,英式和美式的拼写法而已.通常英式英语里的-our-字母组合,到了美式英语里面都成了-or-字母组合,最常见的有英式的 colour,到美式英语成了 color.
- 移动web——媒体查询
基本概念 响应式开发在没有媒体查询前,也可以通过js来实现,但是人们基本不会考虑,特别繁琐.在出现了媒体查询,才开始逐渐推广响应式.实际开发中,在时间与金钱充足的情况下还是别做响应式,影响性能,维护麻 ...
- JS——try catch throw
本例检测输入变量的值.如果值是错误的,会抛出一个异常(错误).catch 会捕捉到这个错误,并显示一段自定义的错误消息: <script> function myFunction() { ...
- VBA中Option的四种用法
1.Option Explicit.当使用Option Explicit时,必须在模块中的所有过程声明每一个变量,否则会出现语法错误并不能被编译.这样做的好处是,它能消除程序中因为错拼变量名而导致程序 ...
- PHP连接mysql8.0出错“SQLSTATE[HY000] [2054] The server requested authentication method unkno…
今天安装安装一个叫得推校园O2O系统的使劲都说连接不上服务器. 下面是安装说明,我直接进行3步骤,导入数据库配置文件,结果就显示题目报错的内容 安装说明: 直接输入程序目录即可 http://loca ...
- css3 animation 中的 steps
steps Specifies a stepping function, described above, taking two parameters. The first parameter spe ...