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. nodejs 入门学习

    Node.js学习笔记——Node.js开发Web后台服务   目录 一.简介 二.搭建Node.js开发环境 2.1.安装Node.js 2.2.安装IDE开发Node.js插件 三.第一个Node ...

  2. Oracle数据库导出txt格式工具sqlload2使用

    开发需求:需要在数据库中查询数据,最终得到cxv表格形式数据. 使用plsql导出70M数据量非常慢,本次使用sqlload2工具,导出文本txt文本格式. 1)导出txt文本文件$ ./sqluld ...

  3. 不遮挡人物弹幕是怎么实现的——图片蒙版效果-webkit-mask

    这是一个实验中的功能,用于设置元素上遮罩层的图像. 一.Values none:默认值,透明的黑色图像层,也就是没有遮罩层. <mask-source>:<mask>或CSS图 ...

  4. SQL Server系统函数:字符串函数

    原文:SQL Server系统函数:字符串函数 1.字符转化为ASCII,把ASCII转化为字符,注意返回的值是十进制数 select ASCII('A'),ASCII('B'),ASCII('a') ...

  5. ELK日志分析系统的搭建

    一.ELK简介 ELK是Elasticsearch.Logstash.Kibana的简称,这三者是核心组件. Elasticsearch是数据存储.搜索.分析引擎,功能非常强大:Logstash是日志 ...

  6. Intellij IDEA 快捷键大全【转】

    IntelliJ Idea 常用快捷键列表 Ctrl+Shift + Enter,语句完成 “!”,否定完成,输入表达式时按 “!”键 Ctrl+E,最近的文件 Ctrl+Shift+E,最近更改的文 ...

  7. Lua 可变参数 ... 的一点测试

    function test( ... ) if (...) then dibug("has ...") else dibug("no ...") end for ...

  8. nodejs入门API之fs模块

    fs模块下的类与FS常量 fs模块下的主要方法 fs的Promise API与FileHandle类 一.fs模块下的类 1.1 fs.Dir:表示目录流的类,由 fs.opendir().fs.op ...

  9. form-create教程:移除默认提交按钮

    本文将介绍form-create如何修改,隐藏默认提交按钮 form-create 是一个可以通过 JSON 生成具有动态渲染.数据收集.验证和提交功能的表单生成器.并且支持生成任何 Vue 组件.结 ...

  10. friend

    #include <iostream> using namespace std; //friend 友元,效率的问题 //get 方法和set方法,是标准封装的结果,friend破坏了这种 ...