POJ 3468 A Simple Problem with Integers

第一次在Win8下写程序,第一次用long long 和 %lld。纪念一下!!!

继数气球之后对数状数组的理解更加深刻了:
1:Sum[i,j]=原始值+变化值
2:[i,j]变化值用在i处加载结尾j+1处减表示。应为UPDATE是向上的
3:I处变化值对X处的Sum的影响是:
delta[i]*(x+1-i)

以下 内容来网上:

 树状数组天生用来动态维护数组前缀和,其特点是每次更新一个元素的值,查询只能查数组的前缀和,

但这个题目求的是某一区间的数组和,而且要支持批量更新某一区间内元素的值,怎么办呢?实际上,

还是可以把问题转化为求数组的前缀和。

    首先,看更新操作update(s, t, d)把区间A[s]...A[t]都增加d,我们引入一个数组delta[i],表示

A[i]...A[n]的共同增量,n是数组的大小。那么update操作可以转化为:

1)令delta[s] = delta[s] + d,表示将A[s]...A[n]同时增加d,但这样A[t+1]...A[n]就多加了d,所以

2)再令delta[t+1] = delta[t+1] - d,表示将A[t+1]...A[n]同时减d

    然后来看查询操作query(s, t),求A[s]...A[t]的区间和,转化为求前缀和,设sum[i] = A[1]+...+A[i],则

                            A[s]+...+A[t] = sum[t] - sum[s-1],

那么前缀和sum[x]又如何求呢?它由两部分组成,一是数组的原始和,二是该区间内的累计增量和, 把数组A的原始

值保存在数组org中,并且delta[i]对sum[x]的贡献值为delta[i]*(x+1-i),那么

                            sum[x] = org[1]+...+org[x] + delta[1]*x + delta[2]*(x-1) + delta[3]*(x-2)+...+delta[x]*1

                                         = org[1]+...+org[x] + segma(delta[i]*(x+1-i))

                                         = segma(org[i]) + (x+1)*segma(delta[i]) - segma(delta[i]*i),1 <= i <= x

这其实就是三个数组org[i], delta[i]和delta[i]*i的前缀和,org[i]的前缀和保持不变,事先就可以求出来,delta[i]和

delta[i]*i的前缀和是不断变化的,可以用两个树状数组来维护。

    树状数组的解法比朴素线段树快很多,如果把long long变量改成__int64,然后用C提交的话,可以达到1047ms,

排在22名,但很奇怪,如果用long long变量,用gcc提交的话就要慢很多。

A Simple Problem with Integers

Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 262144/131072K (Java/Other)
Total Submission(s) :    Accepted Submission(s) : 
Problem 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 51 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 
3Q 2 4
 


Sample Output
4
55
9
15
 


Source
PKU
 
 1 #include <cstdio>
 2 #include <cstring>
 3 
 4 #define N 100002
 5 
 6 using namespace std;
 7 
 8 long long deta1[N];
 9 long long deta2[N];
10 long long sum[N];
11 
12 int A[N];
13 int n;
14 
15 long long Lowbit(long long x)
16 {
17     return x&(-x);
18 }
19 
20 long long query(long long* arr,int i)
21 {
22     long long temp=0;
23     while(i>0)
24     {
25         temp+=arr[i];
26         i-=Lowbit(i);
27     }
28     return temp;
29 }
30 
31 void update(long long * arr,int pos,long long val)
32 {
33     while(pos<N)
34     {
35         arr[pos]+=val;
36         pos+=Lowbit(pos);
37     }
38 }
39 
40 int main()
41 {
42     int s,t,m;
43     long long ans=0;
44     char ch;
45 
46     scanf("%d%d",&n,&m);
47 
48     for(int i=1;i<=n;i++)
49     {
50         scanf("%d",&A[i]);
51     }
52     memset(sum,0,sizeof(sum));
53     memset(deta1,0,sizeof(deta1));
54     memset(deta2,0,sizeof(deta2));
55 
56     for(int i=1;i<=n;i++)
57     {
58         sum[i]=sum[i-1]+A[i];
59     }
60 
61     while(m--)
62     {
63         getchar();
64         scanf("%c%d%d",&ch,&s,&t);
65         if(ch=='C')
66         {
67             int c;
68             scanf("%d",&c);
69             update(deta1,s,c);
70             update(deta1,t+1,-1*c);
71             update(deta2,s,c*s);
72             update(deta2,t+1,-1*c*(t+1));
73         }
74         else if(ch=='Q')
75         {
76             ans=sum[t]-sum[s-1];
77             ans+= (t+1)*query(deta1,t)-query(deta2,t);
78             ans-=(s)*query(deta1,s-1)-query(deta2,s-1);//(X+1) 和  i  拆开了
79             printf("%lld\n",ans);
80         }
81     }
82 
83     return 0;
84 }
原文地址:https://www.cnblogs.com/CKboss/p/3058661.html