P3374 "template" Tree array 1
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 line contains 3 or 4 integers, representing 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 and Output Sample input example # #:
5 51 5 4 2 31 1 32 2 51 3-11 4 22 1 4
Sample # # of output:
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
-------------------------------------------------------------------------------------------------------------
Push a blog: http://blog.csdn.net/qq_21841245/article/details/43956633
A very good tree-like array, save a board
1#include <stdio.h>2#include <string.h>3 #defineMAXN 5000104 intDELTA[MAXN],N,M,SUM[MAXN];5 intread ();6 intFindint);7 voidAddint,int);8 intLowbit (int);9 intMain () {TenN=read (); m=read (); One for(intI=1; i<=n;i++){ A intA=read (); -sum[i]=sum[i-1]+A; - } the for(intI=1; i<=m;i++){ - intO=read (), X=read (), y=read (); - if(o==1) Add (x, y); - Else{ + intAns=find (y)-find (x1) +sum[y]-sum[x-1]; -printf"%d\n", ans); + } A } at return 0; - } - intFindintx) { - intsum=0; - while(x>0){ -sum+=Delta[x]; inx-=lowbit (x); - } to returnSUM; + } - voidAddintXintk) { the while(x<=N) { *delta[x]+=K; $x+=lowbit (x);Panax Notoginseng } - } the intLowbit (intx) { + returnx&-x; A } the intRead () { + intans=0, f=1;CharC=GetChar (); - while('0'>c| | C>'9'){if(c=='-') f=-1; c=GetChar ();} $ while('0'<=c&&c<='9') ans=ans*Ten+c- -, C=getchar ();returnans*F; $}
Single-point modification + interval query
Rokua P3374 "template" Tree array 1 title (Single point modification + interval query)