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 ...
随机推荐
- Java IO 字节流与字符流 (五)
Java的IO流分为字符流(Reader,Writer)和字节流(InputStream,OutputStream),字节流顾名思义字节流就是将文件的内容读取到字节数组,然后再输出到另一个文件中.而字 ...
- 使用Google的Closure Compiler,在本机上压缩javascript
2011-12-05 13:47:39 1.JAVA JDK下载地址: http://download.oracle.com/otn-pub/java/jdk/7u1-b08/jdk-7u1-wi ...
- UVaLive 6680 Join the Conversation (DP)
题意:给出n条发言,让你求最大的交流长度并输出标记顺序. 析:这个题要知道的是,前面的人是不能at后面的人,只能由后面的人at前面的,那就简单了,我们只要更新每一层的最大值就好,并不会影响到其他层. ...
- [Usaco2018 Open]Milking Order
Description Farmer John的N头奶牛(1≤N≤10^5),仍然编号为1-N,正好闲得发慌.因此,她们发展了一个与Farmer John每天早上为她们挤牛奶的时候的排队顺序相关的复杂 ...
- 二分图最大匹配(匈牙利算法) UVA 670 The dog task
题目传送门 /* 题意:bob按照指定顺序行走,他的狗可以在他到达下一个点之前到一个景点并及时返回,问狗最多能走多少个景点 匈牙利算法:按照狗能否顺利到一个景点分为两个集合,套个模板 */ #incl ...
- HTML基础2——综合案例3——创建考试报名表格
<html> <head> <title></title> </head> <body> <table width=&qu ...
- JS数组、数组和学生对象
<html> <head> <meta charset="utf-8"> <title>JS</title> </ ...
- 用 NPOI 组件实现数据导出
利用 Nuget 安装 NPOI 组件. 所需引用的 dll:ICSharpCode.SharpZipLib.dll.NPOI.dll.NPOI.OOXML.dll.NPOI.OpenXml4Net. ...
- Spring Boot (30) 上传文件
文件上传 上传文件和下载文件是Java Web中常见的一种操作,文件上传主要是将文件通过IO流传输到服务器的某一个文件夹下. 导入依赖 在pom.xml中添加上spring-boot-starter- ...
- 总结MySQL中SQL语法的使用
--where子句操作符: where子句操作符 = 等于 <> 不等于(标准语法) != 不等于(非标准语法,可移植性差) < 小于 <= 小于等于 > 大于 > ...