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 ...
随机推荐
- 【BZOJ4880】排名的战争 [暴力]
排名的战争 Time Limit: 8 Sec Memory Limit: 256 MB[Submit][Status][Discuss] Description 小Q是一名出色的质检员,他负责质检 ...
- UOJ#110. 【APIO2015】Bali Sculptures
印尼巴厘岛的公路上有许多的雕塑,我们来关注它的一条主干道. 在这条主干道上一共有 NN 座雕塑,为方便起见,我们把这些雕塑从 11 到 NN 连续地进行标号,其中第 ii 座雕塑的年龄是 YiYi 年 ...
- CODE FESTIVAL 2017 qual B C - 3 Steps
Score : 500 points Problem Statement Rng has a connected undirected graph with N vertices. Currently ...
- lua中的继承
做为一个java出身的程序媛,长时间做Lua,重复一些工作后,特别想用继承.其实很简单.因为我有一大部分的场景,背景长的都一样,所以打算做一个父类. 需要注意的是,如果子类有和父类的同名函数,就会被覆 ...
- HH实习 acm算法部 1689
题目描述 这学期到了十五周了,HH突然要去实训中心实习了,想到要拿着钳子,锯子什么的,头就有点大了,因为它挺好玩的,但是,也是很累的,看着学弟坐在机房悠闲地敲着代码,HH学长决定要让他们好好忙忙,这道 ...
- java servlet jsp 导入boostrap css js
1.在导入boostrap.css的时候注意了 一定要注意路径,你知道把js和css包放在servlet服务器的静态路径下面就是 lib 文件夹路径下,直接使用 lib/js/boostrap.css ...
- idea 调试远程tomcat
# ----- Execute The Requested Command ----------------------------------------- JAVA_OPTS="-age ...
- SQL注入之逗号拦截绕过
目前所知博主仅知的两个方法 1.通过case when then 2.join [一]case when then mysql,,,,,,, ) ) end; +----+-----------+-- ...
- Linux 入门记录:五、vi、vim 编辑器
一.vi.vim编辑器 vi 是一个命令行界面下的文本编辑工具,最早在 1976 年由 Bill Joy 开发.当时名字叫做 ex.vi 支持绝大多数操作系统(最早在类 Unix 操作系统的 BSD上 ...
- appium===登陆应用的案例
import time import os from appium import webdriver from selenium.webdriver.support.ui import WebDriv ...