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/Output sample
Input Sample # #:
5 51 5 4 2 31 2 4 22 31 1 5-11 3 5 72 4
1#include <bits/stdc++.h>2 #defineMAXN 10000073 using namespacestd;4 5 intn,m;6 intC[MAXN],A[MAXN];7 8 intLowbit (intk)9 {Ten returnk& (-k); One } A voidUpdate (intKintnum) - { - if(k==0)return ; the while(k<=N) - { -c[k]+=num; -k+=Lowbit (k); + } - return; + A } at intSum (intk) - { - intans=0; - while(k>0) - { -ans+=C[k]; ink-=Lowbit (k); - } to returnans; + } - the intMain () * { $Ios::sync_with_stdio (false);Panax NotoginsengCin>>n>>m; - intT; the for(intI=1; i<=n;i++) + { ACin>>A[i]; the Update (I,a[i]); +Update (i+1,-a[i]); - } $ intT1,t2,t3,t4; $ - for(intI=1; i<=m;i++) - { theCin>>T1; - if(t1==1) Wuyi{cin>>t2>>t3>>t4; Update (T2,T4); Update (t3+1,-t4);} the if(t1==2) -{cin>>t2; cout<<sum (T2) <<Endl;} Wu } - return 0; About}
Tree-like array basic template (interval update, single point query)