题目链接:

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. 【洛谷2904/BZOJ1617】[USACO08MAR]跨河River Crossing(动态规划)

    题目:洛谷2904 分析: 裸dp-- dp方程也不难想: \(dp[i]\)表示运\(i\)头牛需要的最短时间,\(sum[i]\)表示一次运\(i\)头牛(往返)所需的时间,则 \[dp[i]=m ...

  2. Android json 数据解析

    1.json格式 2.json解析 3.gson解析 4.fastjson解析 一.Json格式 json一种轻量级的数据交换格式.在网络上传输交换数据一般用xml, json. 两种结构: 1)对象 ...

  3. Java内存泄漏及对象引用的4种类型

    转自: http://www.cnblogs.com/qq78292959/archive/2011/07/25/2116123.html 总结: 引用分类: 强引用,弱引用,软引用,虚引用.虚引用必 ...

  4. 深入Mysql字符集设置

    作者: Laruence(   ) 本文地址: http://www.laruence.com/2008/01/05/12.html 转载请注明出处 根据Chaos  Wang的PPT整理而成, 在此 ...

  5. Jenkins-SVN + Maven + Docker

    第1步:安装插件 Subversion Plug-inMaven Integration pluginCloudBees Docker Build and Publish pluginDeploy t ...

  6. 【转】Linux字符转换命令col

    转自:http://www.cnblogs.com/ningvsban/p/3725464.html [root@www ~]# col [-xb]选项与参数:-x :将 tab 键转换成对等的空格键 ...

  7. 10、scala面向对象编程之Trait

    1.  将trait作为接口使用 2.trait中定义具体方法 3.trait定义具体字段 4.trait中定义抽象字段 5.为实例对象混入trait 6.trait调用链 7.在trait中覆盖抽象 ...

  8. nginx-配置反向代理实例

    nginx反向代理配置及优化 2009-05-26 作者:守住每一天blog:liuyu.blog.51cto.combbs:bbs.linuxtone.orgmsn:liuyubj520#hotma ...

  9. hibernate注解之@Onetomany、@Manytoone、@JoinColumn

    @Onetomany用于实体类与数据库表映射中少的一方,请看下面的例子. 假设一个用户只有一种角色,用户和角色是onetomany的关系 用户实体 @Entity @Table(name=" ...

  10. Linux 之CentOS7使用firewalld打开关闭防火墙与端口

    一.firewalld的基本使用 启动: systemctl start firewalld 关闭: systemctl stop firewalld 查看状态: systemctl status f ...