C. rmq with shifts

Source: Internet
Author: User
C. rmq with shifts Time limit:1000 ms Case time limit:1000 ms Memory limit:131072kb 64-bit integer Io format: % LLDJava class name: Main

In the traditional rmq (range minimum query) problem, we have a static ArrayA. Then for each query (L,R)(LR), We report the minimum valueA[L],A[L+ 1],...,A[R]. Note that the indices start from 1, I. e. the left-most element isA[1].

In this problem, the arrayAIs no longer static: we need to support another operation

 

Shift( I1, I2, I3 ,..., IK )( I1 < I2 <... < IK, K> 1)

 

We do a left ''circular shift"A[I1],A[I2],...,A[IK].

For example, ifA= {6, 2, 4, 8, 5, 1, 4}, thenShift(2, 4, 5, 7) yields {6, 8, 4, 5, 4, 1, 2}. After that,Shift(1, 2) yields 8, 6, 4, 5, 4, 1, 2.

 

Input There will be only one test case, beginning with two integersN,Q(1N100,000, 1Q(250,000), the number of integers in arrayA, And the number of operations. The next line containsNPositive Integers not greater than 100,000, the initial elements in arrayA. Each of the nextQLines contains an operation. Each operation is formatted as a string having no more than 30 characters, with no space characters inside. All operations are guaranteed to be valid.

 


Warning: The dataset is large, better to use faster I/O methods.

 

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

 

Sample Input

 

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

 

Sample output

 

146

Problem solving: rmq is a new update ..........

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <vector> 6 #include <climits> 7 #include <algorithm> 8 #include <cmath> 9 #define LL long long10 using namespace std;11 const int maxn = 100010;12 struct node{13     int lt,rt,minVal;14 }tree[maxn<<2];15 int d[maxn],u[30],cnt;16 void build(int lt,int rt,int v){17     tree[v].lt = lt;18     tree[v].rt = rt;19     if(lt == rt){20         tree[v].minVal = d[lt];21         return;22     }23     int mid = (lt+rt)>>1;24     build(lt,mid,v<<1);25     build(mid+1,rt,v<<1|1);26     tree[v].minVal = min(tree[v<<1].minVal,tree[v<<1|1].minVal);27 }28 int query(int lt,int rt,int v){29     if(tree[v].lt == lt && tree[v].rt == rt) return tree[v].minVal;30     int mid = (tree[v].lt+tree[v].rt)>>1;31     if(rt <= mid) return query(lt,rt,v<<1);32     else if(lt > mid) return query(lt,rt,v<<1|1);33     else return min(query(lt,mid,v<<1),query(mid+1,rt,v<<1|1));34 }35 void update(int lt,int rt,int v){36     if(tree[v].lt == tree[v].rt){37         tree[v].minVal = d[tree[v].lt];38         return;39     }40     int mid = (tree[v].lt+tree[v].rt)>>1;41     if(u[rt] <= mid) update(lt,rt,v<<1);42     else if(u[lt] > mid) update(lt,rt,v<<1|1);43     else{44         int i;45         for(i = lt; u[i] <= mid; i++);46         update(lt,i-1,v<<1);47         update(i,rt,v<<1|1);48     }49     tree[v].minVal = min(tree[v<<1].minVal,tree[v<<1|1].minVal);50 }51 int main(){52     int n,m,i,j,len,temp;53     char str[100];54     while(~scanf("%d%d",&n,&m)){55         for(i = 1; i <= n; i++)56             scanf("%d",d+i);57             build(1,n,1);58         for(i = 0; i < m; i++){59             scanf("%s",str);60             len = strlen(str);61             for(cnt = j = 0; j < len;){62                 if(str[j] < ‘0‘ || str[j] > ‘9‘) {j++;continue;}63                 temp = 0;64                 while(j < len && str[j] >= ‘0‘ && str[j] <= ‘9‘) {temp = temp*10 + (str[j]-‘0‘);j++;}65                 u[cnt++] = temp;66             }67             if(str[0] == ‘q‘){68                 printf("%d\n",query(u[0],u[1],1));69             }else{70                 temp = d[u[0]];71                 for(cnt--,j = 0; j < cnt; j++)72                     d[u[j]] = d[u[j+1]];73                 d[u[j]]  = temp;74                 update(0,cnt,1);75             }76         }77     }78     return 0;79 }
View code

 

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.