A Simple Problem with Integers

Time Limit: 10000MS

Memory Limit: 65536K

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

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


解题心得:

  1. 这是一个最简单的线段树,题意就是给你一系列数,每次询问l到r的和或者每次在l到r之间的每一个数加上一个数。很简单啊,但是万万没想到比赛居然崩在这个题上面,将r和R传参的时候写反了,tm样例和所有自己造的数据都过了,哎,已砍手。google翻译还吧这题翻译成了神题,我去。
  2. 这个题还是很有教训的,线段树的代码比较麻烦,要不停的向上维护,向下传递,不停的传递参数,所以在写的时候一定要注意,写慢一点,不然线段树出现了bug很难找,思路不复杂别死在了手贱上面。线段树的lazy标记的时候一定要记得下移,以及在每次递归之后记得向上维护,向上维护的时候一定要注意父节点和两个子节点的关系,该传递一些什么,别和lazy下移的时候搞混了。

#include<cstring>
#include<stdio.h>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<math.h>
using namespace std;
const int maxn = 1e6+100;
struct node
{
long long l,r,sum,lazy;
}tree[maxn<<2]; void pushup(long long root)
{
tree[root].sum = tree[root<<1].sum + tree[root<<1|1].sum;
} void pushdown(long long root)
{
if(tree[root].lazy == 0)
return ;
tree[root<<1].lazy += tree[root].lazy;
tree[root<<1|1].lazy += tree[root].lazy;
tree[root<<1].sum += (tree[root<<1].r - tree[root<<1].l + 1)*tree[root].lazy;
tree[root<<1|1].sum += (tree[root<<1|1].r - tree[root<<1|1].l + 1)*tree[root].lazy;
tree[root].lazy = 0;
} void buildtree(long long l,long long r,long long root)
{
tree[root].l = l;
tree[root].r = r;
tree[root].lazy = 0;
if(l == r)
{
scanf("%lld",&tree[root].sum);
return ;
}
long long mid = (l + r) >> 1;
buildtree(l,mid,root<<1);
buildtree(mid+1,r,root<<1|1);
pushup(root);
} long long query(long long L,long long R,long long l,long long r,long long root)
{
if(l == L && R ==r)
{
return tree[root].sum;
}
long long mid = (l + r)>>1;
pushdown(root);
if(R <= mid)
{
return query(L,R,l,mid,root<<1);
}
else if(L > mid)
{
return query(L,R,mid+1,r,root<<1|1);
}
else
return query(mid+1,R,mid+1,r,root<<1|1)+ query(L,mid,l,mid,root<<1);//这里手贱找了一个小时的bug
pushup(root);
} void add(long long L,long long R,long long l,long long r,long long root,long long h)
{
if(l == L && R ==r)
{
tree[root].lazy += h;
tree[root].sum += (r - l +1)*h;
return;
}
pushdown(root);
long long mid = (l + r) >> 1;
if(R <= mid)
add(L,R,l,mid,root<<1,h);
else if(L > mid)
add(L,R,mid+1,r,root<<1|1,h);
else
{
add(L,mid,l,mid,root<<1,h);
add(mid+1,R,mid+1,r,root<<1|1,h);
}
pushup(root);
} int main()
{
long long n,m;
while(scanf("%lld%lld",&n,&m) != EOF)
{
long long sum = 0;
buildtree(1,n,1);
while(m--)
{
long long a,b,h;
char c[10];//尽量别输入%c,容易挂掉,%s挺好的
scanf("%s",&c);//这里也要注意一下输入的问题,不同的字符对应的不同个数的int输入
if(c[0] == 'C')
{
long long h;
scanf("%lld%lld%lld",&a,&b,&h);
add(a,b,1,n,1,h);
}
else if(c[0] == 'Q')
{
scanf("%lld%lld",&a,&b);
sum = query(a,b,1,n,1);
printf("%lld\n",sum);
}
}
}
return 0;
}

线段树:POJ3468-A Simple Problem with Integers(线段树注意事项)的更多相关文章

  1. 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和

    poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...

  2. poj3468 A Simple Problem with Integers (线段树区间最大值)

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

  3. poj3468 A Simple Problem with Integers (树状数组做法)

    题目传送门 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 1 ...

  4. POJ3468 A Simple Problem with Integers(线段树延时标记)

    题目地址http://poj.org/problem?id=3468 题目大意很简单,有两个操作,一个 Q a, b 查询区间[a, b]的和 C a, b, c让区间[a, b] 的每一个数+c 第 ...

  5. poj3468 A Simple Problem with Integers(线段树模板 功能:区间增减,区间求和)

    转载请注明出处:http://blog.csdn.net/u012860063 Description You have N integers, A1, A2, ... , AN. You need ...

  6. POJ3468 A Simple Problem with Integers —— 线段树 区间修改

    题目链接:https://vjudge.net/problem/POJ-3468 You have N integers, A1, A2, ... , AN. You need to deal wit ...

  7. 2018 ACMICPC上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节) 链接:https://ac.nowcoder.co ...

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

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

  9. POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)

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

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

随机推荐

  1. Unbuntu 自动重启MySQL

    上个月,通过Unbuntu搭建了WordPress,一切运行良好. UBUNTU搭建WORDPRESS-MYSQL-APACHE 但是,最近几天,不知道啥情况,MySQL偶尔会出现Stop:影响了bl ...

  2. Batch梯度下降

    1.之前讲到随机梯度下降法(SGD),如果每次将batch个样本输入给模型,并更新一次,那么就成了batch梯度下降了. 2.batch梯度下降显然能够提高算法效率,同时相对于一个样本,batch个样 ...

  3. Shell分割字符得到数组

    #!/bin/bash p=$(hadoop fs -ls /tgl/data |awk '{print $8}') #要将$a分割开,先存储旧的分隔符 OLD_IFS="$IFS" ...

  4. mysql in和exists性能比较和使用

    in 是把外表和内表作hash 连接,而exists是对外表作loop循环,每次loop循环再对内表进行查询.一直以来认为exists比in效率高的说法是不准确的. 如果查询的两个表大小相当,那么用i ...

  5. c#的Lambda 表达式

    首先看官方的说法: Lambda 表达式是一种可用于创建委托或表达式目录树类型的匿名函数. 通过使用 lambda 表达式,可以写入可作为参数传递或作为函数调用值返回的本地函数. Lambda 表达式 ...

  6. java中的常用内存区域总结

    <开发实战经典>     (1)栈内存空间:保存所有的对象名称     (2)堆内存空间:保存每个对象的具体属性内容     (3)全局数据区:保存static类型的属性     (4)全 ...

  7. 织梦修改“dedecms提示信息”

    1.根目录下include文件夹,找到common.func.php: 2.根目录下dede文件夹(管理目录默认dede),找到sys_data_done.php: 3.打开以上2个.php文件,把“ ...

  8. RK3288开发过程中遇到的问题点和解决方法之Kernel

    修改背光改变区间 kernel\drivers\video\backlight\pwm_bl.c static int pwm_backlight_update_status(struct backl ...

  9. To run dex in process, the Gradle daemon needs a larger heap

    http://blog.csdn.net/u012995856/article/details/52595653

  10. 键盘各键对应的ASCII码值(包括鼠标和键盘所有的键)

    ESC键 VK_ESCAPE (27)回车键: VK_RETURN (13)TAB键: VK_TAB (9)Caps Lock键: VK_CAPITAL (20)Shift键: VK_SHIFT ($ ...