POJ3468:A Simple Problem with Integers(线段树模板)
A Simple Problem with Integers
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 149972 | Accepted: 46526 |
题目链接:http://poj.org/problem?id=3468
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
题解:
线段树模板题,注意一下lazy标记的下传操作,标记也是long long 型的。
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;
typedef long long ll;
const int N = 1e5+;
int n,m;
ll a[N];
ll ans;
struct Tree{
int l,r;
ll f,w;
}tre[(N<<)+];
void build(int o,int l,int r){
tre[o].l=l;tre[o].r=r;tre[o].f=;
if(l==r){
tre[o].w=a[l];
return ;
}
int mid=l+r>>;
build(o<<,l,mid);
build(o<<|,mid+,r);
tre[o].w=tre[o<<].w+tre[o<<|].w;
}
void down(int o){
tre[o<<].f+=tre[o].f;
tre[o<<|].f+=tre[o].f;
tre[o<<].w+=tre[o].f*(tre[o<<].r-tre[o<<].l+);
tre[o<<|].w+=tre[o].f*(tre[o<<|].r-tre[o<<|].l+);
tre[o].f=;
}
void update(int o,int l,int r,int val){
int L=tre[o].l,R=tre[o].r;
if(L>=l && R<=r){
tre[o].w+=(ll)val*(R-L+);
tre[o].f+=val;
return ;
}
down(o);
int mid=L+R>>;
if(l<=mid) update(o<<,l,r,val);
if(r>mid) update(o<<|,l,r,val);
tre[o].w=tre[o<<].w+tre[o<<|].w;
}
void query(int o,int l,int r){
int L=tre[o].l,R=tre[o].r;
if(L>=l && R<=r){
ans+=tre[o].w;
return ;
}
down(o);
int mid=L+R>>;
if(l<=mid) query(o<<,l,r);
if(r>mid) query(o<<|,l,r);
}
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) scanf("%I64d",&a[i]);
build(,,n);
char s[];
for(int i=;i<=m;i++){
scanf("%s",s);
if(s[]=='Q'){
int l,r;ans=;
scanf("%d%d",&l,&r);
query(,l,r);
printf("%I64d\n",ans);
}else{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
update(,a,b,c);
}
}
return ;
}
POJ3468:A Simple Problem with Integers(线段树模板)的更多相关文章
- poj3468 A Simple Problem with Integers(线段树模板 功能:区间增减,区间求和)
转载请注明出处:http://blog.csdn.net/u012860063 Description You have N integers, A1, A2, ... , AN. You need ...
- poj3468 A Simple Problem with Integers (线段树区间最大值)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92127 ...
- POJ3468 A Simple Problem with Integers(线段树延时标记)
题目地址http://poj.org/problem?id=3468 题目大意很简单,有两个操作,一个 Q a, b 查询区间[a, b]的和 C a, b, c让区间[a, b] 的每一个数+c 第 ...
- POJ3468 A Simple Problem with Integers —— 线段树 区间修改
题目链接:https://vjudge.net/problem/POJ-3468 You have N integers, A1, A2, ... , AN. You need to deal wit ...
- 2018 ACMICPC上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节) 链接:https://ac.nowcoder.co ...
- poj 3468 A Simple Problem with Integers 线段树 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=3468 线段树模板 要背下此模板 线段树 #include <iostream> #include <vector> ...
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 53169 Acc ...
- 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 线段树,树状数组
题目:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
随机推荐
- UVa 340 - Master-Mind Hints 解题报告 - C语言
1.题目大意 比较给定序列和用户猜想的序列,统计有多少数字位置正确(x),有多少数字在两个序列中都出现过(y)但位置不对. 2.思路 这题自己思考的思路跟书上给的思路差不多.第一个小问题——位置正确的 ...
- [C++] OOP - Virtual Functions and Abstract Base Classes
Ordinarily, if we do not use a function, we do not need to supply a definition of the function. Howe ...
- Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset Trie
题目链接: http://codeforces.com/contest/706/problem/D D. Vasiliy's Multiset time limit per test:4 second ...
- C# 创建Excel或需不安装Office
第一种.Aspose.Cells.dll //如果需要饶过office Excel那么就看我最后的实现方法吧~! //我最后的实现是使用的第三方Aspose.Cells.dll //具了解这个dll一 ...
- 数据库性能优化之SQL优化
网上有关SQL优化的方案有很多,但多是杂乱无章.近日闲暇抽空整理了一下,方便大家以后的查阅,若发现其中有什么问题和不全,欢迎大家在下面纠正和补充: 1. 对于SQL语句的性能优化,主要体现在对于查询语 ...
- 浅谈 Vue v-model指令的实现原理 - 如何利用v-model设计自定义的表单组件
原文请点击此链接 链接1 http://www.7zhang.com/index/cms/read/id/234515.html 链接2 http://blog.csdn.net/yangbing ...
- wine update错误 "the cache has no package" error when wine update is available
网址:https://bugs.launchpad.net/pipelight/+bug/1318321/
- 使用tc来控制网络流量
https://blog.csdn.net/qinyushuang/article/details/46611709 tc实际操控网络的流量 解释网络tc的架构,从架构上分析tc,与netfilter ...
- 添加路由时啥时候是dev啥时候是gw
A qumu ethA1 B 宿主机 ethA2 ethC2 C 树莓派 ethC1 在A和C中都是直接sudo route add default dev ethA1/ethC1 这样做是有问题的 ...
- Android------去除标题栏
这里暂时只给出一种方法,在java代码中去除 1.继承Activity 在onCreate方法中 getWindow().setFlags(WindowManager.LayoutParams.FLA ...