题目链接:

id=3468http://">http://poj.org/problem?

id=3468

A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 83959   Accepted: 25989
Case Time Limit: 2000MS

Description

You have N integers, A1A2, ... , 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 A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.

Each of the next Q lines represents an operation.

"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.

"Q a b" means querying the sum of AaAa+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

The sums may exceed the range of 32-bit integers.

Source


题目大意:给定n个数,m个操作。Q a b表示输出【a。b】这段区间内的和,C a b c表示将【a。b】这段区间的全部值都加上c。经过一系列变化,依照要求进行输出。

解题思路:标准的线段树,建树、更新树以及查找树。

对于更新树是为了避免改动到最底下而导致超时问题。所以每次改动仅仅改动相相应的区间就可以。然后记录一个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(线段树+区间更新+区间求和)的更多相关文章

  1. POJ 3468 A Simple Problem with Integers (线段树多点更新模板)

    题意: 给定一个区间, 每个区间有一个初值, 然后给出Q个操作, C a b c是给[a,b]中每个数加上c, Q a b 是查询[a,b]的和 代码: #include <cstdio> ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]

    A Simple Problem with Integers   Description You have N integers, A1, A2, ... , AN. You need to deal ...

  6. poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 75541   ...

  7. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

    A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

  8. POJ 3468 A Simple Problem with Integers //线段树的成段更新

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 59046   ...

  9. poj 3468 A Simple Problem with Integers 线段树加延迟标记

    A Simple Problem with Integers   Description You have N integers, A1, A2, ... , AN. You need to deal ...

  10. poj 3468 A Simple Problem with Integers 线段树区间更新

    id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072 ...

随机推荐

  1. jmeter关联、下载文件、简单压测

    关联 一.什么是关联 关联是请求与请求之间存在数据依赖关系,需要从上一个请求获取下一个请求需要回传回去的数据. 简单地说就是在测试过程中有些数据的值会经常发生变化,要获取并使用这些数据,把这个动态的信 ...

  2. Boost(1.69.0) windows入门(译)

    目录 Boost windows入门 1. 获得Boost源代码 2. Boost源代码组织 The Boost Distribution 3. 仅用头文件的库 Header-Only Librari ...

  3. ClouderaManager与CDH

    * ClouderaManager与CDH 集群简述 对于企业而言,一般的集群大小规模大概是如下映射关系: 集群大小 小:10~30节点 中:100~300节点 大:1000+节点 对应所需的zook ...

  4. Java系列学习(四)-运算计算

    1.运算符 (1)算术运算符 A:+,-,*,/,%,++,-- B:+的用法 [a.加法] [b.正号] [c.字符串连接付] C:/和%的区别 [数据做除法的时候,/取的是商,%取的是余数] D: ...

  5. 如何用putty链接服务器端,并安装wdcp

    首先把自己阿里云的磁盘格式化然后重启 自己下载一个PuTTY 打开后输入自己的Ip地址端口号默认是22 会跳出一个yes 跟no界面,点击yes 会进入一个类似cmd界面 直接输入root,然后会提示 ...

  6. java设计模式03装饰者者模式

    动态地给一个对象添加一些额外的职责.就增加功能来说, Decorator模式相比生成子类更为灵活.该模式以对客 户端透明的方式扩展对象的功能. (1)在不影响其他对象的情况下,以动态.透明的方式给单个 ...

  7. java网络

    title: java 网络 date: 2017年3月11日11:14:52 1. 复杂的东西就把他封装成对象 概述:(网络就是找到别人) 找到对方的机器,(找到对方的ip地址) 每个机器中有很多进 ...

  8. CAD从二制流数据中加载图形(com接口VB语言)

    主要用到函数说明: _DMxDrawX::ReadBinStream 从二制流数据中加载图形,详细说明如下: 参数 说明 VARIANT varBinArray 二制流数据,是个byte数组 BSTR ...

  9. Django - 路由对应关系

    1.对url路由关系命名,以后可以根据此名称,生成自己想要的url urls.py中: url('^fdsaafdf(\d+)/',views.index,name='indexx') url('^f ...

  10. php第十六节课

    分页 <?php /** file: page.class.php 完美分页类 Page */ class Page { private $total; //数据表中总记录数 private $ ...