• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • YouClaw
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
HaibaraAi
博客园    首页    新随笔    联系   管理    订阅  订阅

Uva 12295 - RMQ with Shifts

你为何这么叼!

这题说好的只有一组数据呢,还要0和文件结束个蛋?

RMQ with Shifts 

Time limit: 10.000 seconds

 

In the traditional RMQ (Range Minimum Query) problem, we have a static array A. Then for each query (L, R) (L$ \le$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.

Input 

There will be only one test case, beginning with two integers n, q ( 1$ \le$n$ \le$100, 000, 1$ \le$q$ \le$250, 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

 


The Seventh Hunan Collegiate Programming Contest
Problemsetter: Rujia Liu, Special Thanks: Yiming Li & Jane Alam Jan

 

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5 #define INF 0x7fffffff
 6 #define maxn 111111
 7 int x[maxn<<2];
 8 int mi[maxn<<2];
 9 int tmp[maxn<<2];
10 int n,m,cnt=0;
11 void up(int rt){mi[rt]=min(mi[rt<<1],mi[rt<<1|1]);}
12 void build(int l,int r,int rt){
13     if(l==r){scanf("%d",&mi[rt]);tmp[cnt++]=mi[rt];return;}
14     int m=(l+r)>>1;
15     build(l,m,rt<<1);
16     build(m+1,r,rt<<1|1);
17     up(rt);
18 }
19 void update(int p,int d,int l,int r,int rt){
20     if(l==r){mi[rt]=d;return;}
21     int m=(l+r)>>1;
22     if(p<=m)update(p,d,l,m,rt<<1);
23     if(p>m)update(p,d,m+1,r,rt<<1|1);
24     up(rt);
25 }
26 int query(int L,int R,int l,int r,int rt){
27     if(L<=l&&R>=r)return mi[rt];
28     int m=(l+r)>>1;
29     int res=INF;
30     if(L<=m)res=min(res,query(L,R,l,m,rt<<1));
31     if(R>m)res=min(res,query(L,R,m+1,r,rt<<1|1));
32     return res;
33 }
34 int main(){
35     while(~scanf("%d%d",&n,&m)&&n){
36         build(0,n-1,1);
37         char op[31];
38         int a,b;
39         while(m--){
40             scanf("%5s",op);
41             if(op[0]=='q'){
42                 scanf("(%d,%d)",&a,&b);
43                 a--;b--;
44                 printf("%d\n",query(a,b,0,n-1,1));
45             }
46             else{
47                 int k=0;
48                 char ch;
49                 while(ch!='(')ch=getchar();
50                 while(ch!=')')scanf("%d%c",&x[k++],&ch);
51                 for(int i=0;i<k;i++)x[i]--;
52                 int temp=tmp[x[0]];
53                 for(int i=0;i<k-1;i++)tmp[x[i]]=tmp[x[i+1]];
54                 tmp[x[k-1]]=temp;
55                 for(int i=0;i<k;i++)update(x[i],tmp[x[i]],0,n-1,1);
56             }
57         }
58     }
59     return 0;
60 }
View Code 2013-10-11 16:36:25 

 

posted @ 2013-10-11 16:37  HaibaraAi  阅读(129)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3