Simple Problem with Integers(POJ 3486)
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 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.
题解:又是一道裸地线段树操作。区间查询和区间修改。注意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)的更多相关文章
- A - 敌兵布阵 ——B - I Hate It——C - A Simple Problem with Integers(线段树)
C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况.由于 ...
- BZOJ3212: Pku3468 A Simple Problem with Integers(线段树)
3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 2530 So ...
- POJ 3468 A Simple Problem with Integers(线段树)
题目链接 题意 : 给你n个数,进行两种操作,第一种是将a到b上所有元素都加上c,第二种是查询a到b上所有元素之和输出. 思路 : 线段树,以前写过博客,但是现在在重刷,风格改变,,所以重新写一篇.. ...
- POJ 3468_A Simple Problem with Integers(线段树)
题意: 给定序列及操作,求区间和. 分析: 线段树,每个节点维护两个数据: 该区间每个元素所加的值 该区间元素和 可以分为"路过"该区间和"完全覆盖"该区间考虑 ...
- POJ3468--A Simple Problem with Integers(Splay Tree)
虽然有点难,但是这套题都挂了一个月了啊喂…… 网上模板好多……最后还是抄了kuangbin聚聚的,毕竟好多模板都是抄他的,比较习惯…… POJ 3468 题意:给n个数,两种操作,区间整体加一个数,或 ...
- 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 ...
- POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)
POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...
- poj 3468 A Simple Problem with Integers(线段树+区间更新+区间求和)
题目链接:id=3468http://">http://poj.org/problem? id=3468 A Simple Problem with Integers Time Lim ...
- POJ 3468 A Simple Problem with Integers(树状数组区间更新)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 97217 ...
随机推荐
- 题解-PKUWC2018 猎人杀
Problem loj2541 题意概要:给定 \(n\) 个人的倒霉度 \(\{w_i\}\),每回合会有一个人死亡,每个人这回合死亡的概率为 自己的倒霉度/目前所有存活玩家的倒霉度之和,求第 \( ...
- (十三)Activitivi5之流程控制网关:并行
一.概念 所谓排他网关 顾名思义 执行到该网关,会有多条线路同时并行执行,当都执行完才继续执行后面的: 二. 案例 此时当“学生请假”任务节点完成之后,如下图此时有两个任务,必须等到两个任务都完成才会 ...
- JAVA的转义字符
一.常见的转义字符 转移字符对应的英文是escape character , 转义字符串(Escape Sequence) 字母前面加上捺斜线"\"来表示常见的那些不能显示的AS ...
- node 和 postgres
安装 npm i pg ,如果慢的话,记得爬梯子 连接池的方式: var pg = require('pg'); // 数据库配置 var config = { user:"postgres ...
- Java Web 深入分析(2) DNS
DNS :Domain Name System,域名系统 ,通俗的来说需要把我们日常见到的URL 网址信息解析成IP地址,例如 DNS域名解析过程 用户浏览器:用户在浏览器地址栏输入域名进行访问,浏览 ...
- Python笔记-备忘
一.向列表添加元素 x.append(y) #末尾添加一个元素 x.extend([y,z]) #末尾添加多个元素 x.insert(index,y) 二.向列表获取元素 x[index] 三.从列表 ...
- require.js 加载 js 文件 404 处理(配置无效)
main.js 是 配置文件,data-main 是异步加载,如果在main.js未加载完成的时候,使用了require去加载文件,就会导致配置无效 main.js
- 一线互联网常见的Java面试题,你颤抖了吗程序员
跳槽不算频繁,但参加过不少面试(电话面试.face to face面试),面过大/小公司.互联网/传统软件公司,面糊过(眼高手低,缺乏实战经验,挂掉),也面过人,所幸未因失败而气馁,在此过程中不断查缺 ...
- Linux命令——gzip、zcat、bzip2、bzcat、tar
参考:Linux命令——ar 为什么文件要压缩? 当文件过大时,无论是本地做备份,复制都很麻烦,而且还浪费磁盘空间.如果用网络传输,大文件无疑会浪费大量宝贵带宽.文件压缩技术可以有效解决这个问题,但是 ...
- 算法笔记--可撤销并查集 && 可持久化并查集
可撤销并查集模板: struct UFS { stack<pair<int*, int>> stk; int fa[N], rnk[N]; inline void init(i ...