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 ...
随机推荐
- lintcode: Check Sum of Square Numbers
Check Sum of Square Numbers Given a integer c, your task is to decide whether there're two integers ...
- linux服务器操作小技巧
python程序后台一直运行,并将打印信息输出到文件中 nohup -u test.py > out.txt & -u 表示无缓冲,直接将打印信息输出带文件中 &表示程序后台运行
- 操作系统及Python解释器工作原理讲解
操作系统介绍 操作系统位于计算机硬件与应用软件之间 是一个协调.管理.控制计算机硬件资源与软件资源的控制程序 操作系统功能: 控制硬件 把对硬件复杂的操作封装成优美简单的接口(文件),给用户或者应用程 ...
- BZOJ 3790 神奇项链 hash/后缀自动机+贪心
Description 母亲节就要到了,小 H 准备送给她一个特殊的项链.这个项链可以看作一个用小写字母组成的字符串,每个小写字母表示一种颜色. 为了制作这个项链,小 H 购买了两个机器.第一个机器可 ...
- Swift-创建UIButton(其他UI组件雷同)
let button = UIButton.init(frame: CGRectMake(, , , )) button.setTitle("按钮", forState: UICo ...
- 3DMAX2016安装教程【图文】
下载安装包之后,双击setup.exe. 下面是安装图片教程: 点击安装 点击下一步. 如图输入序列号和产品密钥. 填写安装路径,然后下一步. 开始安装,等待. 安装成功.
- SVM之核函数
SVM之问题形式化 SVM之对偶问题 >>>SVM之核函数 SVM之解决线性不可分 写在SVM之前——凸优化与对偶问题 上一篇SVM之对偶问题中讨论到,SVM最终形式化为以下优化问题 ...
- perf打印调用栈的过程
perf_prepare_sample-->perf_callchain-->get_perf_callchain 上面的调用栈会使用 perf_event_output--> 0x ...
- 【bzoj4008】[HNOI2015]亚瑟王 概率dp
题目描述 $n$ 张牌,$r$ 轮游戏,每轮从左向右操作,遇到第 $i$ 张牌有 $p_i$ 的概率选中,选中会产生 $d_i$ 的贡献,丢弃掉该牌并结束这一轮,否则继续下一张.问最终的期望贡献. 输 ...
- 转:概率主题模型简介 --- ---David M. Blei所写的《Introduction to Probabilistic Topic Models》的译文
概率主题模型简介 Introduction to Probabilistic Topic Models 转:http://www.cnblogs.com/siegfang/archive/2 ...