[Template] Fenwick tree 2

Title Description

If that is known to a number of columns, you need to perform the following two operations:

1. Each section plus a few number x

Obtaining a value of 2. The number of

Input and output formats

Input formats:

The first line contains two integers N, M, respectively, represents the number of the total number of columns and number of operations.

The second line contains N integers separated by spaces, wherein the number indicates the i-th column of the item i of the initial value.

Next M lines contains an integer of 2 or 4, it indicates an operation as follows:

Operation 1: Format: 1 XYK Meaning: the interval [x, y] k each number plus

Operation 2: Format: 2 x Meaning: the number of x-value output

Output formats:

Output contains an integer number of lines, that is, the operation results of all 2.

Sample input and output

Input Sample # 1: 
5 5
1 5 4 2 3
1 2 4 2
2 3
1 1 5 -1
1 3 5 7
2 4
Output Sample # 1: 
6
10

Explanation

Constraints of time: 1000ms, 128M

Data Scale:

For 30% of the data: N <= 8, M <= 10

For 70% of the data: N <= 10000, M <= 10000

To 100% of the data: N <= 500000, M <= 500000

Sample Description:

Therefore, output is 6,10

 

analysis:

It is a template problem em. . . . Not explained. . . The code. . .

 

CODE:

 

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cmath>
 4 #include <iostream>
 5 #include <algorithm>
 6 using namespace std;
 7 int n,m;
 8 long long sum[555555];
 9 void add(int x,long long y){
10     while (x<=n){
11         sum[x]+=y;
12         x+=x&-x;
13     }
14     return;
15 }
16 long long ask(int x){
17     long long ans=0;
18     while (x>=1){
19         ans+=sum[x];
20         x-=x&-x;
21     }
22     return ans;
23 }
24 long long fr(){
25     char c=getchar();long long ans=0;bool flag=1;
26     while (c<'0'||c>'9'){if (c=='-') flag=0;c=getchar();}
27     while (c>='0'&&c<='9'){ans=(ans<<1)+(ans<<3)+(c^48),c=getchar();}
28     if (flag) return ans;return -ans;
29 }
30 int main(){
31     long long A,B,C,D;
32     scanf("%d%d",&n,&m);
33     for (int i=1;i<=n;i++){
34         A=fr();
35         add(i,A);add(i+1,-A);
36     }
37     while (m--){
38         A=fr();
39         if (A==1){
40             B=fr(),C=fr(),D=fr();
41             add(B,D);add(C+1,-D);
42         }
43         if (A==2){
44             B=fr();
45             printf("%lld\n",ask(B));
46         }
47     }
48     return 0;
49 }

 

Guess you like

Origin www.cnblogs.com/kanchuang/p/11200128.html