POJ3468 A Simple Problem with Interger [树状数组,差分]
A Simple Problem with Integers
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 130735 | Accepted: 40585 | |
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
Hint
分析:要求是在[l,r]的区间内修改或者查询和,当然会想到树状数组。但是因为每次是区间修改,所以需要转化一下。
- //It is made by HolseLee on 17th May 2018
- //POJ 3468
- #include<cstdio>
- #include<cstring>
- #include<cstdlib>
- #include<cmath>
- #include<iostream>
- #include<iomanip>
- #include<algorithm>
- #define Fi(i,a,b) for(int i=a;i<=b;i++)
- using namespace std;
- typedef long long ll;
- const int N=1e5+;
- ll n,m,sum[N],c[][N],ans;
- inline ll lowbit(int x){return x&-x;}
- inline void add(int k,int x,int y)
- {for(int i=x;i<=n;i+=lowbit(i))c[k][i]+=y;}
- inline ll get(int k,int x)
- {ll ret=;for(int i=x;i>=;i-=lowbit(i))ret+=c[k][i];return ret;}
- int main()
- {
- ios::sync_with_stdio(false);
- cin>>n>>m;int x,y,z;char opt;
- Fi(i,,n)cin>>x,sum[i]=sum[i-]+x;
- Fi(i,,m){cin>>opt;
- if(opt=='C'){cin>>x>>y>>z;
- add(,x,z);add(,y+,-z);
- add(,x,x*z);add(,y+,-(y+)*z);}
- else {cin>>x>>y;
- ll ans=(sum[y]+(y+)*get(,y)-get(,y));
- ans-=(sum[x-]+x*get(,x-)-get(,x-));
- printf("%lld\n",ans);}}
- return ;
- }
POJ3468 A Simple Problem with Interger [树状数组,差分]的更多相关文章
- POJ3468 A Simple Problem With Integers 树状数组 区间更新区间询问
今天学了很多关于树状数组的技巧.一个是利用树状数组可以简单的实现段更新,点询问(二维的段更新点询问也可以),每次修改只需要修改2个角或者4个角就可以了,另外一个技巧就是这题,原本用线段树做,现在可以用 ...
- A Simple Problem with Integers(树状数组HDU4267)
A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- A Simple Problem with Integers_树状数组
Problem Description Let A1, A2, ... , AN be N elements. You need to deal with two kinds of operation ...
- HDU 4267 A Simple Problem with Integers --树状数组
题意:给一个序列,操作1:给区间[a,b]中(i-a)%k==0的位置 i 的值都加上val 操作2:查询 i 位置的值 解法:树状数组记录更新值. 由 (i-a)%k == 0 得知 i%k == ...
- 洛谷P3368 树状数组2 树状数组+差分
正解:树状数组+差分 解题报告: 戳我! 不得不说灵巧真滴是越来越弱了...连模板题都要放上来了QAQ 因为今天考试的T3正解要用到树状数组这才惊觉树状数组掌握得太太太太差了...之前一直靠线段树续着 ...
- luogu 2519 [HAOI2011]problem a 动态规划+树状数组
发现每一次 $[b[i]+1,n-a[i]]$ 这个区间的分数必须相同,否则不合法. 而一个相同的区间 $[l,r]$ 最多只能出现区间长度次. 于是,就得到了一个 $dp:$ 将每一种区间的出现次数 ...
- POJ 2155 Matrix[树状数组+差分]
原题链接:https://vjudge.net/problem/POJ-2155 题目大意 给定 n* n 矩阵A,其元素为0或1. A [i][j] 表示第i行和第j列中的数字.最初全为0. 我们有 ...
- AcWing243一个简单的整数问题2(树状数组+差分+前缀和规律)
题目地址:https://www.acwing.com/problem/content/244/ 题目描述: 给定一个长度为N的数列A,以及M条指令,每条指令可能是以下两种之一: 1.“C l r d ...
- bzoj2743: [HEOI2012]采花--离线树状数组+差分
题目大意:给定一个区间,查询子区间里出现次数不小于二的数的个数 此题想了好久没想出来,后来是在网上学习的一个方法 首先按查询区间的右端点进行排序,按右端点从小到大处理 假设pre[a[i]]是与a[i ...
随机推荐
- 【设计模式】 模式PK:代理模式VS装饰模式
1.概述 对于两个模式,首先要说的是,装饰模式就是代理模式的一个特殊应用,两者的共同点是都具有相同的接口,不同点则是代理模式着重对代理过程的控制,而装饰模式则是对类的功能进行加强或减弱,它着重类的功能 ...
- Redis 键值数据类型及基本操作
到目前为止,Redis 支持的键值数据类型如下: 字符串(String) 哈希(Map) 列表(list) 集合(sets) 有序集合(sorted sets) 1. String 字符串类型 s ...
- 招人不是HR第一职责,留住人才是
什么是HR的第一责任?我希望你们知道,招人不是你的第一职责,留住人才是你的第一职责.HR工作是相当难做的,你们是公司集团内里各个部门中最难做的部门,也是最具战略性的部门. 以人为本,这个“人”就是阿里 ...
- UIAlertView---iOS-Apple苹果官方文档翻译
本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址 UIAlertView //转载请注明出处--本文永久链接:http://ww ...
- 01-UIScrollView01-大图片展示
源代码下载链接:01-UIScrollView01-大图片展示.zip283.7 KB // // MJViewController.m // 01-UIScrollView01-大图 ...
- FileReader 与canvas结合使用显示图片
话不多少,直接上代码 function fileChange() { var file = this.files[0]; var imageType = /^image\//; //是否是图片 if ...
- js_读【javascript面向对象编程指南】笔记
写在前面: 工欲善其事,必先利其器.编程的器,是前人总结的经验,常言道站在巨人的肩膀上开发,往往比自己另辟蹊径容易的多.经验藏于书,故有书中自有颜如玉,书中自有黄金屋,我也一度认为读书要花费很多时间, ...
- @EnableEurekaClient源码分析
@EnableEurekaClient注解,从源码的角度分析是如何work的 NetFlix Eureka client Eureka client 负责与Eureka Server 配合向外提供注册 ...
- Arduino 舵机sg90电位器实现转动方向控制
/* Sweep*/ #include <Servo.h> int potpin = 0;//电位器接到A0 int val; //存储电位器读取的数值 Servo myservo//定义 ...
- gnu app url[web][5星]
http://www.gnu.org/software/software.zh-cn.html http://linux.chinaunix.net/news/2010/12/07/1175310.s ...