P3368 "template" Tree array 2
Title Description
Title, you need to perform the following two operations, known as a sequence:
1. Add x to each number of intervals
2. Find out the number of a
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 2 or 4 integers, representing an operation, as follows:
Action 1: Format: 1 x y k meaning: Add K to each number in the interval [x, y]
Action 2: Format: 2 x Meaning: output x number of values
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 2 4 22 31 1 5-11 3 5 72 4
Sample # # of output:
610
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:
So the output is 6, 10
----------------------------------------------------------------------------------------------
Push a blog: http://blog.csdn.net/qq_21841245/article/details/43956633
The main approach is actually the poor score group
preserving the prefix of an array with a tree array and only for the purpose of optimizing time complexity
Deposit Board:
1#include <stdio.h>2#include <string.h>3 #defineMAXN 5000104 intdelta[maxn],a[maxn],n,m;5 intread ();6 intLowbit (int);7 voidAddint,int);8 intFindint);9 intMain () {TenN=read (), m=read (); One for(intI=1; i<=n;i++) a[i]=read (); A for(intI=1; i<=m;i++){ - into=read (); - if(o==1){ the intX=read (), Y=read (), k=read (); -Add (x,k); Add (y+1,-k); - } - Else{ + intx=read (); - intAns=find (x) +A[x]; +printf"%d\n", ans); A } at } - return 0; - } - intFindintx) { - intsum=0; - while(x>0){ insum+=Delta[x]; -x-=lowbit (x); to } + returnSUM; - } the voidAddintXintk) { * while(x<=N) { $delta[x]+=K;Panax Notoginsengx+=lowbit (x); - } the } + intLowbit (intx) { A returnx&-x; 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; -}
interval modification + single point query
Rokua P3368 "template" Tree array 2 title (interval modification + single point query)