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. Vue2之页面 、js 、css分离

    在编写vue的时候,页面数据少的时候,可以将所有的js和css都可以直接写在页面上,但是页面数据多,js.css的方法和样式多的时候,都放在一个页面的时候,就显得页面vue十分的臃肿. 所以写项目的时 ...

  2. [转] java实现https请求

    package com.lichmama.test.util; import java.io.ByteArrayOutputStream; import java.io.IOException; im ...

  3. 装饰者模式及php实现

    装饰模式(Decorator Pattern) : 动态地给一个对象增加一些额外的职责(Responsibility),就增加对象功能来说,装饰模式比生成子类实现更为灵活.其别名也可以称为包装器(Wr ...

  4. HBase数据模型(2)

    HBase数据模型(1) HBase数据模型(2) 1.0 HBase的版本version,是一个用长整型表示的.由Rowkey.Column(列族和列).Version组合在一起称为HBase中的一 ...

  5. ubuntu server 16.04安装GPU服务器

    1 Ubuntu16.04 系统安装过程中,需要勾选openssh-server 方便远程连接 2 必须安装gcc 与g++ 3 安装显卡驱动 NVIDIA-Linux-x86_64-367.57.r ...

  6. 使用PM2搭建在线vue.js开发环境(以守护进程方式热启动)

    项目以vue.js+layUI的作为前端开发技术栈,需要有一个在线的环境供项目成员实时查看效果,总不能每次都webpack打包发布后才能看到效果吧!刚开始就简单使用npm run dev命令热启动,但 ...

  7. selenium-Python之上传文件

    对于web 页面的上传功能实现一般有一下两种方式 普通上传:普通的附件上传是将本地文件的路径作为一个值放在input标签中,通过form表单将这个值提交给服务器 插件上传:一般是指基于flash.ja ...

  8. 动态生成带参数的html标签

     "<button onclick='watchClick("+'"'+row.BOXNO + '","'+ row.VOY_NO+'" ...

  9. WisdomTool REST Client 下载 测试请求,生成api文档

    https://github.com/Wisdom-Projects/rest-client

  10. vue2使用animate css

    先上几个链接 vue插件大集合:awesome-vue vue2插件: vue2-animate:vue2-animate vue2插件vue2-animateDEMO: vue2-animatede ...