Rokua P3368 "template" Tree array 2

Source: Internet
Author: User

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 3 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 <iostream>2#include <cstring>3#include <cstdio>4 using namespacestd;5 intn,m,a[500010],tarr[500010];6 voidAddintPosintdel)7 {8     if(pos==0)return ;9      while(pos<=N)Ten     { Onetarr[pos]+=del; Apos+= (pos^ (pos-1)) &Pos; -     } - } the intQry (intPOS) - { -     intsum=0; -      while(POS) +     { -sum+=Tarr[pos]; +pos-= (pos^ (pos-1)) &Pos; A     } at     returnsum; - } - intMain () - { -Cin>>n>>m; -      for(intI=1; i<=n;i++) inscanf"%d",&a[i]); -      for(intI=1; i<=n;i++) toAdd (i,a[i]-a[i-1]);//Cha Fen +     intflag,x,y,z,j; -      for(intI=1; i<=m;i++) the     { *scanf"%d",&flag); $         if(flag==1) scanf ("%d%d%d", &x,&y,&z), add (X,z), add (y+1,-z);Panax Notoginseng         if(flag==2) scanf ("%d", &j), printf ("%d\n", Qry (j)); -     } the      +     return 0; A}

Idea: The tree array template, the interval modified when the front end point +x, after the end of the value of +1-X, preprocessing needs to process a differential sequence, that is b[i]=a[i]-a[i-1]

Rokua P3368 "template" Tree array 2

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.