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

    表单提交 <!--表单:收集用户的数据.---> <form method="post" action="AddInfo.ashx"> ...

  2. 关于死循环while(true){}或for(;;){}的总结

    关于死循环while(true){}或for(;;){}的总结 1.基本用法: while(true){     语句体; } for(;;){     语句体; } 以上情况,语句体会一直执行. 2 ...

  3. 【装载】删除Oracle11G

    卸载Oracle步骤:1.停止所有与ORACLE相关的服务.2. 使用OUI(Oracle Universal Installer)卸载Oracle软件.   “开始”->“程序”->“O ...

  4. 【虚拟机-可用性集】ARM 中可用性集使用的注意事项

    Azure 目前有两种部署模型:经典部署模型 (ASM) 和资源管理器 (ARM).如果您之前使用过 ASM 模式下的可用性集,那么很可能在使用 ARM 模式下的可用性集时,会遇到一些问题或者疑惑.这 ...

  5. MySql5.7主从配置

    记录 环境:ubuntu16.04,mysql5.7 主机:192.168.1.240,192.168.1.241:241为Salve 1.安装mysql sudo apt-get install m ...

  6. 2018.4.18 Ubuntu 的telnet命令详解

    Ubuntu 的telnet命令详解 1.作用用途 Telnet 命令通常用来远程登录,Telnet 程序是基于 Telnet 协议的远程登录客户端程序.Telnet 协议是TCP/IP协议族中的一员 ...

  7. python_87_shelve模块

    'shelve模块是一个简单的key,value将内存数据通过文件持久化的模块,可以持久化任何pickle可支持的python数据格式(只支持pickle)' #序列化,将数据写入文件 import ...

  8. pycharm 使用技巧

    格式化代码为pep8: ctrl+alt+l http://edu.51cto.com//index.php?do=lession&id=163794

  9. hbuilder 夜神模拟器调试方法

    1.首先下载好夜神模拟器2.查找已经安装的夜神模拟的端口,这里说一下夜神模拟器默认端口是62001,但是有些版本可能不是这个端口,怎么查找到底是哪个端口呢?按照如下顺序进行就可以查找到你按装的夜神模拟 ...

  10. c#自定义类型之间的转换(强制类型转换)

    public class ResultModel { public string PlateNumber { get; set; } public int PlateColor { get; set; ...