Luogu P3368 "template" Tree array 2 [interval modification-single point query]

Source: Internet
Author: User

P3368 "template" Tree 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/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

Solution

We define $c[i]$ to mean that $a[i]-a[i-1]$ is the distance between $a[i]$ and $a[i-1]$.
and rules $c[1]=a[1]$.
Then it's obvious.
$a [i-1]+c[i]=a[i]$
Let's put C in the tree-shaped array E.
# # Note E is a tree-like array!!!

Inquire

And then we can draw
$query (x) =\sum^{i<=x}_{i=1}c[i]$
$ =c[1]+c[2]+...+c[x-1]+c[x]$
$ = (A[1]) + (a[2]-a[1]) +...+ (a[x-1]-a[x-2]) + (A[x]-a[x-1]) $
$ =a[1]-a[1]+a[2]+...-a[x-2]+a[x-1]-a[x-1]+a[x]$
$ =a[x]$

Initial insert

Just start by inserting $c[i]$ (that is, $now-last$) into a tree-like array.

Modify

Because after giving $a[x]+z$
The distance between the $a [x]$ and $a[x-1]$ increased by Z, so we had to add $z$ to $a[x]-a[x-1]$ (ie $c[x]$)
Because after giving $a[y]+z$
The distance between the $a [y]$ and $a[y+1]$ decreased by Z, so we had to add $-z$ to $a[y]-a[y+1]$ (ie $c[y+1]$)
The middle $x<i<y$ don't have to deal with it?
Of course, because $c[i]$ save only $a[i] and a[i-1]$ distance Ah!

Codes
1  ProgramNo;2 var3 4 N,m,i,now,last,c,x,y,z:longint;5E:Array[1..500000] ofLongint;6 7 functionlowbit (apple:longint): Longint;8 begin9Lowbit:=apple and-Apple;Ten End; One  A procedureAdd (x,a:longint); - begin -      whileX<=n Do the      begin -e[x]:=e[x]+A; -x:=x+lowbit (x); -      End; + End; -  + functionquery (x:longint): Longint; A begin atquery:=0; -      whileX>0  Do -     begin -query:=query+E[x]; -x:=x-lowbit (x); -     End; in End; -  to begin +Assign (input,'1.in'); Assign (output,'1.out'); - reset (input); rewrite (output); the  * readln (n,m); $      fori:=1  toN DoPanax Notoginseng     begin - read (now); theAdd (i,now-Last ); +last:=Now ; A     End; the  +      fori:=1  toM Do -     begin $ read (c,x); $         ifC=1  Then -           begin - readln (y,z); the Add (x,z); -Add (y+1,-z);Wuyi           End the         Elsewriteln (query (x)); -     End; Wu  - close (input); Close (output); About End.

Luogu P3368 "template" Tree array 2 [interval modification-single point query]

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.