RMQ with Shifts
来源:互联网 发布:农村人的劣根性知乎 编辑:程序博客网 时间:2024/05/13 03:05
RMQ with Shifts
Time Limit: 1000MS Memory Limit: 65535KB 64bit IO Format:
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], …, 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(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(1,2) yields{8, 6, 4, 5, 4, 1, 2}.
In this problem, the array A is 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” 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 n, q (1<=n<=100,000, 1<=q<=120,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
线段树模板题
#include<iostream>#include<cstdio>#include<cstring>using namespace std;int a[100010];struct node{ int l, r, mmin;}t[100010*4];void build(int i, int l, int r){ t[i].l = l; t[i].r = r; if(l==r){ t[i].mmin = a[l]; return ; } int mid = (l+r)/2; build(i*2, l, mid); build(i*2+1, mid+1, r); t[i].mmin = min(t[i*2].mmin, t[i*2+1].mmin);}int query(int i, int x, int y){ int l = t[i].l; int r = t[i].r; if(x==l&&y==r) return t[i].mmin; int mid = (l+r)/2; if(x>mid) return query(i*2+1, x, y); else if(y<=mid) return query(i*2, x, y); else return min(query(i*2, x, mid),query(i*2+1, mid+1, y));}void update(int i, int x, int y){ int l = t[i].l; int r = t[i].r; if(l==x&&l==r){ t[i].mmin = y; a[x] = y; return ; } int mid = (l+r)/2; if(x <= mid) update(i*2, x, y); else update(i*2+1, x, y); t[i].mmin = min(t[i*2].mmin, t[i*2+1].mmin);}int main(){ int n, m, i, j, k, x, y, p, o; char ch[110]; int b[110]; while(~scanf("%d%d",&n,&m)){ for(i=1; i<=n; i++) scanf("%d",&a[i]); // getchar(); build(1, 1, n); for(i=0; i<m; i++){ scanf("%s",ch); int l = strlen(ch); o = 1; memset(b, 0, sizeof(b)); if(ch[0]=='s'){ for(j=0; j<l; j++){ p = 0; if(ch[j]>='0'&&ch[j]<='9'){ for(k=j; k<l; k++){ if(ch[k]>='0'&&ch[k]<='9') p = p*10+(ch[k]-'0'); else{ j = k; break; } } } if(p!=0) b[o++] = p; } int P = a[b[1]]; // cout<<b[1]<<endl; for(k=1; k<=o-1; k++){ update(1, b[k], a[b[k+1]]); // a[b[k]] = a[b[k+1]];// for(int pp=1; pp<=n; pp++)// cout<<a[pp]<<" ";// cout<<endl; } update(1, b[o-1], P); // a[b[o-1]] = a[P]; } else if(ch[0]=='q'){ x = y = 0; int h; for(j=0; j<l; j++){ if(ch[j]=='(') break; } for(h=j+1; h<l; h++){ if(ch[h]>='0'&&ch[h]<='9') x = x*10+(ch[h]-'0'); else break; } for(j=h+1; j<l; j++){ if(ch[j]>='0'&&ch[j]<='9') y = y*10+(ch[j]-'0'); else break; } printf("%d\n",query(1, x, y)); } // getchar(); } } return 0;}
0 0
- uva_12299 RMQ with Shifts
- RMQ with Shifts
- RMQ with Shifts
- 12299 - RMQ with Shifts
- uva12299 RMQ with Shifts
- RMQ with Shifts
- RMQ with Shifts
- uva12299 RMQ with Shifts
- RMQ with Shifts
- uva12299(RMQ with Shifts)
- 12299 - RMQ with Shifts
- 12299RMQ with Shifts
- RMQ with Shifts
- RMQ with Shifts
- Uva-12299-RMQ with Shifts
- UVA 12299 RMQ with Shifts
- UVa:12299 RMQ with Shifts
- UVA 12299 RMQ with Shifts
- STL源码剖析笔记(二)---容器
- setup lang
- 从问题看本质:socket到底是什么?
- HDU1425 sort
- 为什么 Archlinux 不适合服务器使用
- RMQ with Shifts
- Python 小甲鱼教程 课后练习43
- eclipse-android配置maven,tomcat,mysql及其sqlyog
- linux下使用过程中的一些列解决方法
- 操作系统的基本功能
- 最强日期正则表达式
- 【Effective C++读书笔记】篇十(条款25)
- ERROR org.apache.struts2.dispatcher.DefaultDispatcherErrorHandler - Exception
- Mac charles 配置ssl