题目传送门

  

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. JavaScript之typedof,instanceof,Array.isArray()

    typedof value检测对象类型. value instanceof Array检测a对象是否由b对象类型 Array.isArray(value)在两个框架中判断是否为数组类型.

  2. LightOJ 1058 - Parallelogram Counting 几何思维

    http://www.lightoj.com/volume_showproblem.php?problem=1058 题意:给你顶点,问能够成多少个平行四边形. 思路:开始想使用长度来扫描有多少根,但 ...

  3. jQuery简单的Ajax调用

    index.php 的代码如下: <!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"& ...

  4. Bzoj4710 [Jsoi2011]分特产

    Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 96  Solved: 62[Submit][Status][Discuss] Description ...

  5. Arduino 舵机sg90电位器实现转动方向控制

    /* Sweep*/ #include <Servo.h> int potpin = 0;//电位器接到A0 int val; //存储电位器读取的数值 Servo myservo//定义 ...

  6. 如何用CMD开启3389与查看3389端口

    开启 REG ADD HKLM\SYSTEM\CurrentControlSet\Control\Terminal" "Server /v fDenyTSConnections / ...

  7. 比 file_get_contents() 更优的 cURL 详解(附实例)

    PHP 可以使用 file_get_content() 函数抓取网页内容,但却无法进行更复杂的处理,譬如文件的上传或下载. Cookie 操作等等.而 cURL 提供了这些功能. 一.cURL简介 在 ...

  8. Call Mode feature

    起源 user 在插著 充電器 打電話的狀況下, 為了安全起見, 避免 充電器在這時損害手機,間接造成 user 的傷害, 而有了這 feature, 在 battery voltage Vbat & ...

  9. 初学者学习Javascript很吃力怎么办?到底该如何学习Js?

      Js给初学者的印象总是那么的“杂而乱”,相信很多初学者都在找轻松学习Js的途径.在这里给大家总结一些学习Js的经验,希望能给后来的学习者探索出一条“轻松学习Js之路”. Js给人那种感觉的原因多半 ...

  10. apache的扩展模块安装

    /* 当需要用到Apache的扩展模块时, 就要用到了apache的扩展工具 apxs */ #在使用这个功能之前,请先确认是否已经加载了 mod_so 模块,方法是: [root@localhost ...