题目传送门

  

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, A1A2, ... , 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 A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+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

The sums may exceed the range of 32-bit integers.

  分析:要求是在[l,r]的区间内修改或者查询和,当然会想到树状数组。但是因为每次是区间修改,所以需要转化一下。
  首先,假设这里我们只考虑单点查询。新建一个数组b[i]来存储每次的修改信息。对于每一个C l r d,将b[l]加上d,在将b[r+1]减去d,那么每次查询的时候就输出a[x]+(b[x]的前缀和)就可以得到a[x]修改后的值。正确性易证,画图就很好理解了,这里蒟蒻就不画图了(偷懒一波)。
  那么再考虑区间查询,易得a[1~x]整体修改的值为Σxi=1Σij=1b[j],推导Σxi=1Σij=1b[j]=Σxi=1(x-i+1)*b[i]=(x+1)Σxi=1b[i]-Σxi=1i*b[i](格式不太好看将就下吧)。那么这题的算法就可以确定了。
  建立两个树状数组c0,c1,对于每一个修改操作,执行以下操作:
  将c0中的l位置加d,将c0中的r+1位置减d
  将c1中的l位置加l*d,将c1中的r+1位置减(r+1)*d
  再用sum[]直接记录a[]的前缀和,对于每一个询问指令,输出(sum[r]+(r+1)*get(c0,r)-get(c1,r))-(sum[l-1]+l*get(c0,l-1)-get(c1,l-1))。实际上也就是用的一般的前缀和与树状数组相结合,并且运用了差分的思想。
  Code:
//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 [树状数组,差分]的更多相关文章

  1. POJ3468 A Simple Problem With Integers 树状数组 区间更新区间询问

    今天学了很多关于树状数组的技巧.一个是利用树状数组可以简单的实现段更新,点询问(二维的段更新点询问也可以),每次修改只需要修改2个角或者4个角就可以了,另外一个技巧就是这题,原本用线段树做,现在可以用 ...

  2. A Simple Problem with Integers(树状数组HDU4267)

    A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (J ...

  3. A Simple Problem with Integers_树状数组

    Problem Description Let A1, A2, ... , AN be N elements. You need to deal with two kinds of operation ...

  4. HDU 4267 A Simple Problem with Integers --树状数组

    题意:给一个序列,操作1:给区间[a,b]中(i-a)%k==0的位置 i 的值都加上val  操作2:查询 i 位置的值 解法:树状数组记录更新值. 由 (i-a)%k == 0 得知 i%k == ...

  5. 洛谷P3368 树状数组2 树状数组+差分

    正解:树状数组+差分 解题报告: 戳我! 不得不说灵巧真滴是越来越弱了...连模板题都要放上来了QAQ 因为今天考试的T3正解要用到树状数组这才惊觉树状数组掌握得太太太太差了...之前一直靠线段树续着 ...

  6. luogu 2519 [HAOI2011]problem a 动态规划+树状数组

    发现每一次 $[b[i]+1,n-a[i]]$ 这个区间的分数必须相同,否则不合法. 而一个相同的区间 $[l,r]$ 最多只能出现区间长度次. 于是,就得到了一个 $dp:$ 将每一种区间的出现次数 ...

  7. POJ 2155 Matrix[树状数组+差分]

    原题链接:https://vjudge.net/problem/POJ-2155 题目大意 给定 n* n 矩阵A,其元素为0或1. A [i][j] 表示第i行和第j列中的数字.最初全为0. 我们有 ...

  8. AcWing243一个简单的整数问题2(树状数组+差分+前缀和规律)

    题目地址:https://www.acwing.com/problem/content/244/ 题目描述: 给定一个长度为N的数列A,以及M条指令,每条指令可能是以下两种之一: 1.“C l r d ...

  9. bzoj2743: [HEOI2012]采花--离线树状数组+差分

    题目大意:给定一个区间,查询子区间里出现次数不小于二的数的个数 此题想了好久没想出来,后来是在网上学习的一个方法 首先按查询区间的右端点进行排序,按右端点从小到大处理 假设pre[a[i]]是与a[i ...

随机推荐

  1. \(\rm LightOJ 1371 - Energetic Pandas 简单计数+组合\)

    http://www.lightoj.com/volume_showproblem.php?problem=1371 题意:给你n根竹子,和n只熊猫(XD),每个熊猫只能选择重量不大于它的竹子,问有几 ...

  2. [Luogu 3973] TJOI2015 线性代数

    [Luogu 3973] TJOI2015 线性代数 这竟然是一道最小割模型. 据说是最大权闭合子图. 先把矩阵式子推出来. 然后,套路建模就好. #include <algorithm> ...

  3. JAVA中3种将byte转换为String的方法

    HttpClient 类库中GetMethod类的getResponseBody方法返回的是byte[]类型,要操作起来不方便,我想把它转化成String类型. 查了网上的资料,有说法认为用这种方法比 ...

  4. Super A^B mod C (快速幂+欧拉函数+欧拉定理)

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=1759 题目:Problem Description Given A,B,C, You should quick ...

  5. js中字符串的操作

    1.length 获取字符串长度 var str = "hello world"; alert(str); 2.索引 通过下标获取字符串指定位置的字符,但是不能改变该索引对应的值 ...

  6. Python第三方库SnowNLP(Simplified Chinese Text Processing)快速入门与进阶

    简介 github地址:https://github.com/isnowfy/snownlp SnowNLP是一个python写的类库,可以方便的处理中文文本内容,是受到了TextBlob的启发而写的 ...

  7. mysql主从复制、操作语句

    授权 grant replication slave on *.* to slave@192.168.10.64 identified by "123456" 登录测试 mysql ...

  8. php快速入门总结

    因为本人已经接触了C和C++两年多了,虽然真正用它们的机会很少,但是基本的语法还是相对熟悉的.半年前的课程设计用了PHP,所以当初我也只是现学先用, 学得很粗糙,现在,跟一个同学合作搞一个比赛的项目, ...

  9. Exploring Qualcomm's TrustZone Implementation

    转自  http://bits-please.blogspot.com/2015/08   (需要FQ, 狗日的墙) In this blog post, we'll be exploring Qua ...

  10. 给windows设置隐藏文件夹的方法

    cls @ECHO OFF title Folder Private if EXIST "HTG Locker" goto UNLOCK if NOT EXIST Private ...