A Simple Problem with Integers

Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 137519   Accepted: 42602
Case Time Limit: 2000MS

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


题解:又是一道裸地线段树操作。区间查询和区间修改。注意long long。

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<map>
#include<cmath>
#include<string>
using namespace std;
typedef long long ll;
ll ans;
struct node
{
ll l, r, w;
ll f;
};
struct node tree[100000 * 4 + 1];
void BuildSegmentTree(int k, int l, int r)
{
tree[k].l = l;
tree[k].r = r;
if(l == r )
{
scanf("%lld", &tree[k].w);
return ;
}
int m = (tree[k].l + tree[k].r) >> 1;
BuildSegmentTree(k << 1, l, m);
BuildSegmentTree(k << 1 | 1, m + 1, r);
tree[k].w = tree[2 * k].w + tree[2 * k + 1].w;
}
void down(int k)
{
tree[k << 1].f += tree[k].f;
tree[k << 1 | 1].f += tree[k].f;
tree[k << 1]. w += tree[k].f * (tree[k * 2].r - tree[k * 2].l + 1);
tree[k << 1 |1].w += tree[k].f * (tree[k << 1| 1].r - tree[k << 1| 1].l + 1);
tree[k].f = 0;
}
void Lazysum(int k, int x, int y)
{
if(tree[k].l >= x && tree[k].r <= y)
{
ans += tree[k].w;
return ;
}
if(tree[k].f) down(k);
int m = (tree[k].l + tree[k].r) / 2;
if(x <= m) Lazysum(k << 1, x, y);
if(y > m) Lazysum(k << 1 | 1, x, y);
}
void LazyAdd(int k, int x, int y, int z)
{
if(tree[k].l >= x && tree[k].r <= y)
{
tree[k].w += z * (tree[k].r - tree[k].l + 1);
tree[k].f += z;
return ;
}
if(tree[k].f) down(k);
int m = (tree[k].l + tree[k].r) / 2;
if(x <= m) LazyAdd(2 *k, x, y, z);
if(y > m) LazyAdd(2 * k + 1, x, y, z);
tree[k].w = tree[k << 1].w + tree[k << 1 |1].w;
}
char op;
int main()
{
int N, Q;
while(~scanf("%d %d",&N, &Q))
{
BuildSegmentTree(1,1,N);
while(Q--)
{
int x, y,z;
getchar();
scanf("%c %d %d", &op, &x, &y);
if(op == 'Q')
{
ans = 0;
Lazysum(1,x,y);
printf("%lld\n",ans);
}
else if(op == 'C')
{
scanf("%d", &z);
LazyAdd(1,x,y,z);
}
}
}
return 0;
}

Simple Problem with Integers(POJ 3486)的更多相关文章

  1. A - 敌兵布阵 ——B - I Hate It——C - A Simple Problem with Integers(线段树)

    C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况.由于 ...

  2. BZOJ3212: Pku3468 A Simple Problem with Integers(线段树)

    3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 2530  So ...

  3. POJ 3468 A Simple Problem with Integers(线段树)

    题目链接 题意 : 给你n个数,进行两种操作,第一种是将a到b上所有元素都加上c,第二种是查询a到b上所有元素之和输出. 思路 : 线段树,以前写过博客,但是现在在重刷,风格改变,,所以重新写一篇.. ...

  4. POJ 3468_A Simple Problem with Integers(线段树)

    题意: 给定序列及操作,求区间和. 分析: 线段树,每个节点维护两个数据: 该区间每个元素所加的值 该区间元素和 可以分为"路过"该区间和"完全覆盖"该区间考虑 ...

  5. POJ3468--A Simple Problem with Integers(Splay Tree)

    虽然有点难,但是这套题都挂了一个月了啊喂…… 网上模板好多……最后还是抄了kuangbin聚聚的,毕竟好多模板都是抄他的,比较习惯…… POJ 3468 题意:给n个数,两种操作,区间整体加一个数,或 ...

  6. ACM学习历程——POJ3468 A Simple Problem with Integers(线段树)

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  7. POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)

    POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...

  8. poj 3468 A Simple Problem with Integers(线段树+区间更新+区间求和)

    题目链接:id=3468http://">http://poj.org/problem? id=3468 A Simple Problem with Integers Time Lim ...

  9. POJ 3468 A Simple Problem with Integers(树状数组区间更新)

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

随机推荐

  1. 19-MySQL DBA笔记-操作系统、硬件、网络的优化

    第19章 操作系统.硬件.网络的优化 本章将介绍操作系统和硬件的性能优化,对于硬件,我们主要讲述CPU.内存.磁盘阵列及固态硬盘.任何优化,首先都需要有足够的数据支持,对于操作系统下性能数据的收集,这 ...

  2. vue实现web登陆权限控制

    实现原理:vueRouter控制前端页面跳转路由,当登录成功后,返回用户登录token信息,将token信息放到store中,router路由跳转取store中状态有token时,当取到token时跳 ...

  3. PHP常见算法

    算法的概念:解决特定问题求解步骤的描述,在计算机中表现为指令的有限序列,并且每条指令表示一个或多个操作.一个问题可以有多种算法,每种算法都不同的效率.一个算法具有的特征:有穷,确切,输入,输出,可行 ...

  4. Oracle 11G 数据库迁移【expdp/impdp】

    转自:http://www.th7.cn/db/Oracle/201802/263773.shtml 0x01 环境 A 机器,操作系统 CentOS7.3,Oracle版本:11G,IP地址:192 ...

  5. Marketing Cloud的contact merge机制

    Marketing Cloud的contact支持多种多样的数据源,如下图所示: SAP Hybris Commerce SAP ERP SAP Cloud for Customer SAP Gigy ...

  6. C#基础 结构体、枚举

    一 结构体 结构体(struct)指的是一种数据结构,一个变量组,是一个自定义的集合.通常使用结构体创造新的“属性”,封装一些属性来组成新的类型.   结构体一般定义在Mian函数上面,位于Class ...

  7. 数列分段`Section II`(二分

    https://www.luogu.org/problemnew/show/P1182 洛谷上的题目. 以后如果遇到1e5什么的   用二分试试! #include<iostream> # ...

  8. train loss相关问题

    一.train loss 收敛慢,把learning_rate调高 二.train loss不下降: 1.观察数据中是否有异常样本或异常label导致数据读取异常2.调小初始化权重,以便使softma ...

  9. Java 基础 面向对象- 成员内部类/局部内部类/举例Comparable 接口的匿名内部类

    笔记: package 任务135; /**类的 内部类, *1.相当于说, 我们可以在类的内部再定义类, * 2.成员内部类: * a.是外部类的一个成员,4个修饰符:static, final , ...

  10. LeetCode刷题--有效的括号(简单)

    题目描述 给定一个只包括 ' ( ' , ' )  ',  ' { ' , ' } ' , ' [ ' , ' ] ' 的字符串,判断字符串是否有效.有效字符串需满足: 左括号必须用相同类型的右括号闭 ...