Tree-like 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
1#include <cstdio>2#include <iostream>3#include <cstring>4#include <algorithm>5 #defineMAXN 5000056 using namespacestd;7 intN,M,A[MAXN],C[MAXN];8InlineintLowbit (intx) {returnx& (-x);}9InlinevoidAddintPosintx) {Ten while(pos<=N) { Onec[pos]+=x; Apos+=Lowbit (POS); - } - } theInlineintGetsum (intPOS) { - inttmp=0; - while(pos>0){ -tmp+=C[pos]; +pos-=Lowbit (POS); - } + returntmp; A } at intMain () { -scanf"%d%d",&n,&m); - for(intI=1; i<=n;i++){ -scanf"%d",&a[i]); - Add (I,a[i]); - } in inta,b,c; - for(intI=1; i<=m;i++){ toscanf"%d%d%d",&a,&b,&c); + if(a==1) Add (b,c); - Else if(a==2) printf ("%d\n", Getsum (c)-getsum (b-1)); the } * return 0; $}
Tree-like 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
1#include <cstdio>2#include <iostream>3#include <cstring>4#include <algorithm>5 #defineMAXN 5000056 using namespacestd;7 intN,M,A[MAXN],C[MAXN],D[MAXN];8InlineintLowbit (intx) {returnx& (-x);}9InlinevoidAddintPosintx) {Ten while(pos<=N) { Onec[pos]+=x; Apos+=Lowbit (POS); - } - } theInlineintGetsum (intPOS) { - inttmp=0; - while(pos>0){ -tmp+=C[pos]; +pos-=Lowbit (POS); - } + returntmp; A } at intMain () { -scanf"%d%d",&n,&m); - for(intI=1; i<=n;i++){ -scanf"%d",&a[i]); -d[i]=a[i]-a[i-1]; - Add (I,d[i]); in } - intq,x,y,k; to for(intI=1; i<=m;i++){ +scanf"%d",&q); - if(q==1){ thescanf"%d%d%d",&x,&y,&k); *d[x]+=K;add (x,k); $d[y+1]-=k;add (y+1,-k);Panax Notoginseng } - Else if(q==2){ thescanf"%d",&x); +printf"%d\n", Getsum (x)); A } the } + return 0; -}
"Luogu 3374,3368" tree array of