Title Description
Title, you need to perform the following two operations, known as a sequence:
1. Add a number to the X
2. Find out the amount of each number in a range and
input/output format
Input format:
The first line contains two integers n, M, respectively, indicating the number of numbers and the total number of operations.
The second line contains n space-delimited integers, where the I number represents the initial value of item I of a sequence.
The next m row contains 3 integers for each row, indicating an operation, as follows:
Action 1: Format: 1 x k Meaning: Add x number plus K
Action 2: Format: 2 x y meaning: the and of each number in the output interval [x, y]
Output format:
The output contains several line integers, which is the result of all Operation 2.
input/Output sampleInput Sample
5 51 5 4 2 31 1 32 2 51 3-11 4 22 1 4
Output Sample
1416
Description
Time limit: 1000ms,128m
Data size:
For 30% data: n<=8,m<=10
For 70% data: n<=10000,m<=10000
For 100% data: n<=500000,m<=500000
Sample Description:
Therefore, the output results 14, 16
Explain:
Why use a tree-like array? Ask a few questions first:
1. Give a series, several operations, each operation of the required number plus a number (that is, the number modified)
We use an array, O (1) can do
2. Give a series of series, several operations, each operation to the requirements of the interval and to find out
This is also simple, using the prefix and finish preprocessing, O (1) can also be done
But what about both the modification and the prefix?
Also as described above, each time we modify the prefix and will be re-updated, multiple operations down, will be very time-consuming
So, a magical approach arises--a tree-like array
It is good at a lowbit operation, and then modifies the single point at the same time, in a magical way, quickly updated the interval and
Specific explanation of reference materials
Code
#include <stdio.h>Const intmx=500001;intA[MX],C[MX];intn,m;intLowbit (intx) { returnx&-x;}voidAddintXinttx) { for(inti=x;i<=n;i+=lowbit (i)) {C[i]+=TX; }}intAskintx) { inttot=0; for(inti=x;i;i-=lowbit (i)) tot+=C[i]; returntot; }intMain () {scanf ("%d%d",&n,&m); for(intI=1; i<=n;++i) {scanf ("%d",&A[i]); Add (I,a[i]); } while(m--){ intKiss; scanf ("%d",&kiss); if(kiss==1){ intKis,jia; scanf ("%d%d",&kis,&Jia); Add (Kis,jia); } Else if(kiss==2){ intL,r; scanf ("%d%d",&l,&R); printf ("%d\n", ask (R)-ask (l1)); } } return 0;}
Tree-like array "Template 1"