码迷,mamicode.com
首页 > 编程语言 > 详细

POJ3468 A Simple Problem with Interger [树状数组,差分]

时间:2018-05-17 20:38:12      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:信息   cee   ini   efi   void   values   size   plm   poj   

  题目传送门

  

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:
 1 //It is made by HolseLee on 17th May 2018
 2 //POJ 3468
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cstdlib>
 6 #include<cmath>
 7 #include<iostream>
 8 #include<iomanip>
 9 #include<algorithm>
10 #define Fi(i,a,b) for(int i=a;i<=b;i++)
11 using namespace std;
12 typedef long long ll;
13 const int N=1e5+7;
14 ll n,m,sum[N],c[2][N],ans;
15 inline ll lowbit(int x){return x&-x;}
16 inline void add(int k,int x,int y)
17 {for(int i=x;i<=n;i+=lowbit(i))c[k][i]+=y;}
18 inline ll get(int k,int x)
19 {ll ret=0;for(int i=x;i>=1;i-=lowbit(i))ret+=c[k][i];return ret;}
20 int main()
21 {
22   ios::sync_with_stdio(false);
23   cin>>n>>m;int x,y,z;char opt;
24   Fi(i,1,n)cin>>x,sum[i]=sum[i-1]+x;
25   Fi(i,1,m){cin>>opt;
26     if(opt==C){cin>>x>>y>>z;
27       add(0,x,z);add(0,y+1,-z);
28       add(1,x,x*z);add(1,y+1,-(y+1)*z);}
29     else {cin>>x>>y;
30       ll ans=(sum[y]+(y+1)*get(0,y)-get(1,y));
31       ans-=(sum[x-1]+x*get(0,x-1)-get(1,x-1));
32     printf("%lld\n",ans);}}
33   return 0;
34 }

 

POJ3468 A Simple Problem with Interger [树状数组,差分]

标签:信息   cee   ini   efi   void   values   size   plm   poj   

原文地址:https://www.cnblogs.com/cytus/p/9052893.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!