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 ...
随机推荐
- 洛谷 P1131 [ ZJOI 2007 ] 时态同步 —— 树形DP
题目:https://www.luogu.org/problemnew/show/P1131 记录 x 子树内同步的时间 f[x],同步所需代价 g[x]: 直接转移即可,让该儿子子树与其它儿子同步, ...
- bzoj1875 [SDOI2009]HH去散步——矩阵快速幂
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1875 有个限制是不能走回头路,比较麻烦: 所以把矩阵中的元素设成边的经过次数,单向边之间就好 ...
- cubism.js
Cubism.js 是时间序列化的一个D3插件,使用Cubism构建更好的实时指示板,从Graphite,Cube 和其他的资源中拉拉取数据.在GitHub的Apache License上可以获取Cu ...
- bzoj3884
http://www.lydsy.com/JudgeOnline/problem.php?id=3884 拓展欧拉定理 http://blog.csdn.net/Pedro_Lee/article/d ...
- Java方式 MySQL数据库连接
JDBC-ODBC : 桥连就是将对JDBC API的调用转换为对另一组数据库连接(即 ODBC) API 的调用 JDBC-ODBC桥连方式驱动类是: sun.jdbc.odbc.JdbcOdbcD ...
- P3297 [SDOI2013]逃考
传送门 完全看不出这思路是怎么来的-- 首先对于两个亲戚,他们监视范围的边界是他们连线的中垂线.那么对于一个亲戚来说它能监视的范围就是所有的中垂线形成的半平面交 然后如果某两个亲戚的监视范围有公共边, ...
- C头文件中尖括号与双引号的区别及编译搜索顺序
这两天被问到一个很有意思的问题:C头文件中尖括号与双引号有什么区别,以前只大约知道 <> 常用在系统库文件,"" 常用在自定义的借口文件中,那具体在gcc编译搜索过程中 ...
- mysql的大数据量的查询
mysql的大数据量查询分页应该用where 条件进行分页,limit 100000,100,mysql先查询100100数据量,查询完以后,将 这些100000数据量屏蔽去掉,用100的量,但是如果 ...
- spring 异常处理
1. 实现接口 HandlerExceptionResolver 捕获异常 2.@ExceptionHandler 在方法添加注解,捕获本地controller异常 3.@ControllerAdvi ...
- ASP.NET MVC5 之 分部页
1.分部页 _PartialPage.cshtml @model List<string> <a>完美世界</a> @foreach (var item in Mo ...