POJ 3468 A Simple Problem with Integers(线段树水题)
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 135904 | Accepted: 42113 | |
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
思路:线段树水题,建议手写线段树,熟悉模板,注意数据需要使用long long
#include<string>
#include<iostream>
#include<algorithm> #define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ll long long using namespace std; const int maxn = 1e5 + ; struct Tree{
ll l, r, w, f;
}tree[maxn << ];
ll ans; inline void PushDown(int rt)
{
tree[rt << ].f += tree[rt].f;
tree[rt << | ].f += tree[rt].f;
tree[rt << ].w += tree[rt].f*(tree[rt << ].r - tree[rt << ].l + );
tree[rt << | ].w += tree[rt].f*(tree[rt << | ].r - tree[rt << | ].l + );
tree[rt].f = ;
}
void build(int l, int r, int rt)
{
tree[rt].l = l;
tree[rt].r = r;
tree[rt].f = ;
if (l == r){
cin >> tree[rt].w;
return;
}
int m = (l + r) >> ;
build(lson);
build(rson);
tree[rt].w = tree[rt << ].w + tree[rt << | ].w;
}
void update(int L, int R, int l, int r, int rt)
{
if (L <= l&& r <= R){
tree[rt].w += ans*(r - l + );
tree[rt].f += ans;
return;
}
if (tree[rt].f)PushDown(rt);
ll m = (l + r) >> ;
if (L <= m)update(L, R, lson);
if (R > m) update(L, R, rson);
tree[rt].w = tree[rt << ].w + tree[rt << | ].w;
}
ll query(ll L, ll R, ll l, ll r, ll rt)
{
if (L <= l&&r <= R){
return tree[rt].w;
}
if (tree[rt].f)PushDown(rt);
ll m = (l + r) >> , cnt = ;
if (L <= m)cnt += query(L, R, lson);
if (R > m)cnt += query(L, R, rson);
return cnt;
}
void Print(int l, int r, int rt)
{
if (l == r){
cout << rt << " = " << tree[rt].w << endl;
return;
}
//cout << rt << " = " << tree[rt].w << endl;
int m = (l + r) >> ;
if (l <= m)Print(lson);
if (r > m)Print(rson);
}
int main()
{
std::ios::sync_with_stdio(false); ll n, q;
cin >> n >> q; build(, n, );
string flag; ll a, b;
while (q--){
cin >> flag >> a >> b;
if (flag == "C"){
cin >> ans;;
update(a, b, , n, );
//Print(1, n, 1);
}
else if (flag == "Q"){
cout << query(a, b, , n, ) << endl;
}
} return ;
}
POJ 3468 A Simple Problem with Integers(线段树水题)的更多相关文章
- poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和
A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...
- poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和(模板)
A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...
- 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 ...
- [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
- poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 75541 ...
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- POJ 3468 A Simple Problem with Integers //线段树的成段更新
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 59046 ...
- poj 3468 A Simple Problem with Integers 线段树加延迟标记
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
- poj 3468 A Simple Problem with Integers 线段树区间更新
id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072 ...
- POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 67511 ...
随机推荐
- 【转】整套完整安全的API接口解决方案
原文地址:http://www.cnblogs.com/hubro/p/6248353.html 在各种手机APP泛滥的现在,背后都有同样泛滥的API接口在支撑,其中鱼龙混杂,直接裸奔的WEB API ...
- Java多线程系列四——控制线程执行顺序
假设有线程1/线程2/线程3,线程3必须在线程1/线程2执行完成之后开始执行,有两种方式可实现 Thread类的join方法:使宿主线程阻塞指定时间或者直到寄生线程执行完毕 CountDownLatc ...
- Linux中的LVM
逻辑卷管理器,通过将另外一个硬盘上的分区加到已有文件系统,来动态地向已有文件系统添加空间的方法. 逻辑卷管理的核心是处理安装在系统上的硬盘分区.在逻辑卷管理的世界里,硬盘称作物理卷(Physical ...
- 深入浅出Android makefile(2)--LOCAL_PATH(转载)
转自:http://nfer-zhuang.iteye.com/blog/1752387 一.说明 上文我们对acp的Android.mk文件做了一个大致的描述,使得大家对Android.mk文件有了 ...
- Vigenère密码 2012年NOIP全国联赛提高组(字符串模拟)
P1079 Vigenère 密码 题目描述 16 世纪法国外交家 Blaise de Vigenère 设计了一种多表密码加密算法――Vigenère 密 码.Vigenère 密码的加密解密算法简 ...
- 使用 script 的 module 属性实现 es6 以上的兼容
几个月前看到了这篇文章 https://philipwalton.com/articles/deploying-es2015-code-in-production-today/,给了我很大的启发,本来 ...
- centos 安装sysbench
安装sysbench 下载并且解压 shell> wget https://github.com/akopytov/sysbench/archive/1.0.zip -O "sysbe ...
- 1.2Hello, World!的大小
描述 还记得在上一章里,我们曾经输出过的“Hello, World!”吗? 它虽然不是本章所涉及的基本数据类型的数据,但我们同样可以用sizeof函数获得它所占用的空间大小. 请编程求出它的大小,看看 ...
- HDU 1879(最小生成树)
#include "iostream" #include "algorithm" #include "cstdio" using names ...
- kafka启动时出现FATAL Fatal error during KafkaServer startup. Prepare to shutdown (kafka.server.KafkaServer) java.io.IOException: Permission denied错误解决办法(图文详解)
首先,说明,我kafk的server.properties是 kafka的server.properties配置文件参考示范(图文详解)(多种方式) 问题详情 然后,我启动时,出现如下 [hadoop ...