UVa 12299 RMQ with Shifts (Shift RMQ)

Source: Internet
Author: User

UVa 12299-rmq with Shifts (Shift RMQ)

Time limit:1.000 seconds

Description-Topic description

In the traditional RMQ (Range Minimum Query) problem, we have a static array A. Then to each query (L,R) (l≤r), we report the minimum value among a[l], A[l + 1], ..., a[r]. Note that the indices-start from 1, i.e. the Left-most element is a[1]. In this problem, the array A was no longer static:we need to support another operation

1], the minimum value in the. a[r]. Note that the subscript starts at 1, that is, the leftmost element is a[1]. In this problem, array a changes slightly: define the following actions
CN
Shift (I1, I2, i3, ..., ik) (I1 < I2 < ... < IK, k > 1)

We do a left "circular shift" of A[I1], A[i2], ..., A[ik]. For example, if a={6, 2, 4, 8, 5, 1, 4}, then shift (2,4,5,7) yields {6, 8, 4, 5, 4, 1, 2}. After That,shift (yields) 8, 6, 4, 5, 4, 1, 2.

We a[i1], A[i2], ..., A[ik] perform a loop left shift. For example, a={6,2,4,8,5,1,4}, go through the shift (2,4,5,7) After you get {6,8,4,5,4,1,2}。 Then go through Shift (1,2) Get 8,6,4,5,4,1,2。
CN

Input- Input

There be is only one test case, beginning with the integers n, q (1≤n≤100,000, 1≤q≤250,000), the number of Integ ERs in array A, and the number of operations. The next line contains n positive integers isn't greater than 100,000, the initial elements in array A. Each of the next Q lines contains an operation. Each operation is formatted as a string has no more than than characters, with no space characters inside. All operations is guaranteed to be valid.

Warning:the DataSet is large, better to use faster I/O methods.

There is only one set of test cases where the starting position has two integers n, q (1,1), Represents the number of elements in an integer array a, respectively. The next line has n not more than 100,000 non-negative numbers, which are the initial elements in the array A. Then the Q Row contains one action per row. Each operation is a string of no more than 30 characters and does not contain spaces. All operations are correct and valid. Note: The amount of data is large, preferably using a faster I/o function.
CN

Output- Output

For each query, print the minimum value (rather than index) in the requested range.

For each query, output the minimum value (not subscript) for the range to be evaluated.
CN

Sample Input- Enter sample

7 2 4 8 5 1 4query (3,7) Shift (2,4,5,7) query (1,4) shift (2,2)

Sample output- Export Example

146

Exercises

A generic line segment tree.

Although the amount of data is large, the meaning can not be used cin Bar,scanf or a .

At first I thought I needed to update with delay, and then think about 30*25w O (NLOGN) or should be able to contributors Jane.

The code is updated from the bottom up, and you can skip some updates by visually moving slightly faster than from the top down.

Code C++

1#include <cstdio>2#include <cstring>3#include <algorithm>4 #defineMX 1000055 intTR[MX <<2], path[mx], IP, ts[mx];6 intBuildintLintRintNow ) {7     if(L > R)returnMX;8     if(L = = R) scanf ("%d", tr + (Path[++ip] =Now ));9     Else{Ten         intMid = L + R >>1, CL, CR; OneCL = Build (L, Mid, now <<1); CR = Build (mid +1, R, now <<1|1); ATr[now] =std::min (CL, CR); -     } -     returnTr[now]; the } - intQueryintLintRintNowintEdL,intEdR) { -     if(EdL <= L && R <= EdR)returnTr[now]; -     intMid = L + R >>1, CL, CR; +     if(EdR <= mid)returnQuery (L, Mid, now <<1, EdL, EdR); -     if(Mid < EdL)returnQuery (Mid +1, R, now <<1|1, EdL, EdR); +CL = Query (L, Mid, now <<1, EdL, mid); CR = Query (mid +1, R, now <<1|1, Mid +1, EdR); A     returnstd::min (CL, CR); at } - voidUpdata (intNowintN) { -     inttmp, CL, CR; -      while(Now >>=1){ -TMP = Std::min (Tr[now <<1], Tr[now <<1|1]); -         if(TMP = = Tr[now])return; inTr[now] =tmp; -     } to } + intMain () { -memset (TR,127,sizeof(tr)); the     intN, Q, I, J, TMP; *     Charop[ -]; $scanf"%d%d", &n, &q);Panax NotoginsengBuild1N1); GetChar (); -      while(q--){ theFread (OP,sizeof(Char),6, stdin); +         if(*op = ='Q'){ A              for(i =0; I <2; ++i) scanf ("%d", TS +i), GetChar (); theprintf"%d\n", Query (1N1, ts[0], ts[1])); +         } -         Else{ $              for(j =0; *op! =')'; ++J) scanf ("%d", ts + j), *op =GetChar (); $              for(tmp = Tr[path[ts[i =0]]]; I < J-1; ++i) { -Tr[path[ts[i]] = tr[path[ts[i +1]]]; - Updata (Path[ts[i]], n); the             } -Tr[path[ts[i]] =tmp;Wuyi Updata (Path[ts[i]], n); the         } - GetChar (); Wu     } -     return 0; About}

UVa 12299 RMQ with Shifts (Shift RMQ)

Related Article

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.