[Luogu 3374,3368] tree array 1, 2, luogu3374

Source: Internet
Author: User

[Luogu 3374,3368] tree array 1, 2, luogu3374

Tree array 1

Description

For example, if you know a sequence, you need to perform the following two operations:

1. Add x to a certain number

2. Obtain the sum of each number in a certain range.

Input/Output Format

Input Format:

The first line contains two integers N and M, indicating the number of numbers in the series and the total number of operations respectively.

The second row contains N integers separated by spaces. the I-th digit indicates the initial value of the I-th Column.

Each row in the next M row contains 3 or 4 integers, indicating an operation, as shown below:

Operation 1: Format: 1 x k meaning: Add k to the number x

Operation 2: Format: 2 x y Meaning: Sum of each number in the output range [x, y]

Output Format:

The output contains several integer rows, that is, the result of all operation 2.

Input and Output sample input sample #1:
5 51 5 4 2 31 1 32 2 51 3 -11 4 22 1 4
Output sample #1:
1416
Description

Time-Space limit: 1000 ms, 128 M

Data scale:

For 30% of data: N <= 8, M <= 10

For 70% of data: N <= 10000, M <= 10000

For 100% of data: N <= 500000, M <= 500000

Example:

Therefore, output results 14 and 16

 1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 #define maxn 500005 6 using namespace std; 7 int n,m,a[maxn],c[maxn]; 8 inline int lowbit(int x){return x&(-x);} 9 inline void add(int pos,int x){10     while(pos<=n){11         c[pos]+=x;12         pos+=lowbit(pos);13     }14 }15 inline int getsum(int pos){16     int tmp=0;17     while(pos>0){18         tmp+=c[pos];19         pos-=lowbit(pos);20     }21     return tmp;22 }23 int main(){24     scanf("%d%d",&n,&m);25     for(int i=1;i<=n;i++){26         scanf("%d",&a[i]);27         add(i,a[i]);28     }29     int a,b,c;30     for(int i=1;i<=m;i++){31         scanf("%d%d%d",&a,&b,&c);32         if(a==1) add(b,c);33         else if(a==2) printf("%d\n",getsum(c)-getsum(b-1));34     }35     return 0;36 }

Tree array 2

Description

For example, if you know a sequence, you need to perform the following two operations:

1. Add x to each number in a certain range

2. Obtain the sum of a certain number

Input/Output Format

Input Format:

The first line contains two integers N and M, indicating the number of numbers in the series and the total number of operations respectively.

The second row contains N integers separated by spaces. the I-th digit indicates the initial value of the I-th Column.

Each row in the next M row contains 2 or 4 integers, indicating an operation, as shown below:

Operation 1: Format: 1 x y k meaning: Add k to each number in the range [x, y]

Operation 2: Format: 2 x meaning: output the value of number x

Output Format:

The output contains several integer rows, that is, the result of all operation 2.

Input and Output sample input sample #1:
5 51 5 4 2 31 2 4 22 31 1 5 -11 3 5 72 4
Output sample #1:
610
Description

Time-Space limit: 1000 ms, 128 M

Data scale:

For 30% of data: N <= 8, M <= 10

For 70% of data: N <= 10000, M <= 10000

For 100% of data: N <= 500000, M <= 500000

Example:

Therefore, the output result is 6 and 10.

 1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 #define maxn 500005 6 using namespace std; 7 int n,m,a[maxn],c[maxn],d[maxn]; 8 inline int lowbit(int x){return x&(-x);} 9 inline void add(int pos,int x){10     while(pos<=n){11         c[pos]+=x;12         pos+=lowbit(pos);13     }14 }15 inline int getsum(int pos){16     int tmp=0;17     while(pos>0){18         tmp+=c[pos];19         pos-=lowbit(pos);20     }21     return tmp;22 }23 int main(){24     scanf("%d%d",&n,&m);25     for(int i=1;i<=n;i++){26         scanf("%d",&a[i]);27         d[i]=a[i]-a[i-1];28         add(i,d[i]);29     }30     int q,x,y,k;31     for(int i=1;i<=m;i++){32         scanf("%d",&q);33         if(q==1){34             scanf("%d%d%d",&x,&y,&k);35             d[x]+=k;add(x,k);36             d[y+1]-=k;add(y+1,-k);37         }38         else if(q==2){39             scanf("%d",&x);40             printf("%d\n",getsum(x));41         }42     }43     return 0;44 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.