C. RMQ with Shifts

C. RMQ with Shifts

Time Limit: 1000ms

Case Time Limit: 1000ms

Memory Limit: 131072KB

64-bit integer IO format: %lld      Java class name: Main

In the traditional RMQ (Range Minimum Query) problem, we have a static array A. Then for each query (LR) (LR), 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 is no longer static: we need to support another operation

shift(i1i2i3,..., ik)(i1 < i2 < ... < ikk > 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(1, 2) yields 8, 6, 4, 5, 4, 1, 2.

Input

There will be only one test case, beginning with two integers nq ( 1n100, 000, 1q250, 000), the number of integers in array A, and the number of operations. The next line contains n positive integers not 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 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 5
6 2 4 8 5 1 4
query(3,7)
shift(2,4,5,7)
query(1,4)
shift(1,2)
query(2,2)

Sample Output

1
4
6

解题:RMQ问题,更新比较有新意。。。。。。。。。。

 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 long
10 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 }

C. RMQ with Shifts

时间: 2024-10-31 18:02:15

C. RMQ with Shifts的相关文章

湖南省第七届大学生计算机程序设计竞赛 RMQ with Shifts (线段树)

RMQ with Shifts 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述     In the traditional RMQ (Range Minimum Query) problem, we have a static array A. Then for each query (L, R) (L<=R), we report the minimum value among A[L], A[L+1], -, A[R]. Note that the indic

UVa 12299 RMQ with Shifts(线段树)

线段树,没了.. ----------------------------------------------------------------------------------------- #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<cctype> #define rep(i,n) for(int i=0;i<n

Uva 12299 RMQ with Shifts(线段树 + 单点更新 )

Uva 12299 RMQ with Shifts (线段树 + 单点更新) 题意: 对于给定的序列 x[i]给出一个操作 shift(a,b,c,d,e) 对应的是将 x[a]与x[b] x[b]与x[c] 这样相邻的两两交换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(1, 2) yields {8, 6, 4, 5, 4

UVa 12299 RMQ with Shifts(移位RMQ)

UVa 12299 - RMQ with Shifts(移位RMQ) Time limit: 1.000 seconds Description - 题目描述 In the traditional RMQ (Range Minimum Query) problem, we have a static array A. Then for each query (L,R) (L ≤ R), we report the minimum value among A[L], A[L + 1], ...,

nyoj 568——RMQ with Shifts——————【线段树单点更新、区间求最值】

RMQ with Shifts 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述     In the traditional RMQ (Range Minimum Query) problem, we have a static array A. Then for each query (L, R) (L<=R), we report the minimum value among A[L], A[L+1], …, A[R]. Note that the indic

线段树模板(区间最小值优化 版) (RMQ with Shifts)

题意: #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <fstream> using namespace std; const int maxn =100010; int RMQ[maxn<<2]; int str[maxn]; int N,M; char ctr[35]; int total[35],cn

树状数组求最大值 (RMQ with Shifts)

代码: #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> using namespace std; const int Max=200010; int RMQ[Max+10]; int total[Max]; int sum[35]; int N,M,cnt; char ctr[35]; int bit(int x) {//每一个下标管辖的范围 ret

RMQ with Shifts

题意: n个数两种操作,1.给出一组位置,使各位置的数循环移动,2.求给定区间的最小值. 分析: 单点更新,区间最值 #include <map> #include <set> #include <list> #include <cmath> #include <queue> #include <stack> #include <cstdio> #include <vector> #include <st

csu 1110 RMQ with Shifts (线段树单点更新)

#include <cstdio> #include <cstring> #include <queue> #include <cmath> #include <algorithm> #include <iostream> #include <cstdlib> #define lson l,m,rt<<1 #define rson m + 1,r ,rt<<1|1 using namespace s