Luo Gu P3368 [template] Fenwick tree 2 (Fenwick tree update interval + + single point of inquiry)

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 Format

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 Format

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

Sample input and output

Input # 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 # 1
6
10

Description / Tips

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

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int N = 500005;
 5 int tree[N];
 6 int n,m;
 7 
 8 int lowbit(int i)
 9 {
10     return i&(-i);
11 }
12 void update(int i,int v)
13 {
14     while(i<=n)
15     {
16         tree[i]+=v;
17         i+=lowbit(i);
18     }
19 }
20 int getsum(int i)
21 {
22     int x=0;
23     while(i>0)
24     {
25         x+=tree[i];
26         i-=lowbit(i);
27     }
28     return x;
29 }
30 void range_update(int l,int r,int val)
31 {
32     update(l,val);
33     update(r+1,-val);
34 }
35 int main()
36 {
37     int x,y,k,op;
38     scanf("%d%d",&n,&m);
39     for(int i=1; i<=n; i++)
40     {
41         scanf("%d",&x);
42         range_update(i,i,x);
43     }
44     for(int i=0; i<m; i++)
45     {
46         scanf("%d",&op);
47         if(op==1)
48         {
49             scanf("%d%d%d",&x,&y,&k);
50             range_update(x,y,k);
51         }
52         else if(op==2)
53         {
54             scanf("%d",&x);
55             printf("%d\n",getsum(x));
56         }
57     }
58     return 0;
59 }

 

Guess you like

Origin www.cnblogs.com/ChangeG1824/p/11869220.html