P3368 "template" Tree array 2 single point query and interval modification

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 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
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

Many students do not know what it means to represent an array of tree arrays (that is, the tree array of the following code)

That's easy to say.

The tree array represents:

The magnitude of the change in the points within his management interval

It's easier to understand the code like this.

Although it may not be possible to understand the tree array in depth

But better than rote template!

Can be combined with the comments I wrote to understand

1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 Const intmaxn=500001;6 intn,m;7 intA[MAXN];8 intTREE[MAXN];9 intLowbit (intp)Ten{returnp& (-p);} One  A voidInterval_increase (intXintv) - { -      for(intI=x;i>0; i=i-lowbit (i)) the     { -tree[i]+=v; -     } -}//interval modification, modify each management point to +  - intPoint_ask (intp) + { A     intans=A[p]; at      for(inti=p;i<=n;i=i+lowbit (i)) -ans=ans+Tree[i]; -     returnans; -}//A[p] is the original value, Tree[i] is the value changed after each update - //Adding the two is the final answer. - intMain () in { -Ios::sync_with_stdio (false); toCin>>n>>m; +      for(intI=1; i<=n;i++) -cin>>a[i];//Initial value the      for(intI=1; i<=m;i++) *     { $         inthow ;Panax NotoginsengCin>>how ; -         if(how==1)//Increase the         { +             intx,y,v; ACin>>x>>y>>v; the interval_increase (y,v); +Interval_increase (x1,-v); -             //first add 1--y to V, then 1--x minus v $             //is equivalent to X--y Plus v $         } -         Else -         { the             intp; -Cin>>p;WuyiCout<<point_ask (P) <<Endl; the         } -     } Wu     return 0; -}

P3368 "template" Tree array 2 single point query and interval modification

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.