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问题,更新比较有新意。。。。。。。。。。
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <climits>
#include <algorithm>
#include <cmath>
#define LL long long
using namespace std;
const int maxn = ;
struct node{
int lt,rt,minVal;
}tree[maxn<<];
int d[maxn],u[],cnt;
void build(int lt,int rt,int v){
tree[v].lt = lt;
tree[v].rt = rt;
if(lt == rt){
tree[v].minVal = d[lt];
return;
}
int mid = (lt+rt)>>;
build(lt,mid,v<<);
build(mid+,rt,v<<|);
tree[v].minVal = min(tree[v<<].minVal,tree[v<<|].minVal);
}
int query(int lt,int rt,int v){
if(tree[v].lt == lt && tree[v].rt == rt) return tree[v].minVal;
int mid = (tree[v].lt+tree[v].rt)>>;
if(rt <= mid) return query(lt,rt,v<<);
else if(lt > mid) return query(lt,rt,v<<|);
else return min(query(lt,mid,v<<),query(mid+,rt,v<<|));
}
void update(int lt,int rt,int v){
if(tree[v].lt == tree[v].rt){
tree[v].minVal = d[tree[v].lt];
return;
}
int mid = (tree[v].lt+tree[v].rt)>>;
if(u[rt] <= mid) update(lt,rt,v<<);
else if(u[lt] > mid) update(lt,rt,v<<|);
else{
int i;
for(i = lt; u[i] <= mid; i++);
update(lt,i-,v<<);
update(i,rt,v<<|);
}
tree[v].minVal = min(tree[v<<].minVal,tree[v<<|].minVal);
}
int main(){
int n,m,i,j,len,temp;
char str[];
while(~scanf("%d%d",&n,&m)){
for(i = ; i <= n; i++)
scanf("%d",d+i);
build(,n,);
for(i = ; i < m; i++){
scanf("%s",str);
len = strlen(str);
for(cnt = j = ; j < len;){
if(str[j] < '' || str[j] > '') {j++;continue;}
temp = ;
while(j < len && str[j] >= '' && str[j] <= '') {temp = temp* + (str[j]-'');j++;}
u[cnt++] = temp;
}
if(str[] == 'q'){
printf("%d\n",query(u[],u[],));
}else{
temp = d[u[]];
for(cnt--,j = ; j < cnt; j++)
d[u[j]] = d[u[j+]];
d[u[j]] = temp;
update(,cnt,);
}
}
}
return ;
}

C. RMQ with Shifts的更多相关文章

  1. UVa 12299 RMQ with Shifts(移位RMQ)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: "Times New ...

  2. TOJ 4325 RMQ with Shifts / 线段树单点更新

    RMQ with Shifts 时间限制(普通/Java):1000MS/3000MS     运行内存限制:65536KByte 描述 In the traditional RMQ (Range M ...

  3. UVa 12299 RMQ with Shifts(线段树)

    线段树,没了.. ----------------------------------------------------------------------------------------- # ...

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

    RMQ with Shifts 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述     In the traditional RMQ (Range Minimum Q ...

  5. RMQ with Shifts(线段树)

    RMQ with Shifts Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u Pra ...

  6. TZOJ 4325 RMQ with Shifts(线段树查询最小,暴力更新)

    描述 In the traditional RMQ (Range Minimum Query) problem, we have a static array A. Then for each que ...

  7. NYOJ 1012 RMQ with Shifts (线段树)

    题目链接 In the traditional RMQ (Range Minimum Query) problem, we have a static array A. Then for each q ...

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

    代码: #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib ...

  9. CSU-1110 RMQ with Shifts (单点更新+区间最小值 zkw线段树)

    In the traditional RMQ (Range Minimum Query) problem, we have a static array A. Then for each query ...

随机推荐

  1. arcengine 将地图文件保存为图片(包括各种图片格式)

    1,最近做了个地图文件输出图片的功能,思想主要就是利用MapControl的ActiveView中的out方法: 2代码如下:欢迎交流指正 SaveFileDialog m_save = new Sa ...

  2. 我喜欢的两个js类实现方式 现在再加上一个 极简主义法

    闭包实现 变量是不会变的:) var myApplication = function(){ var name = 'Yuri'; var age = '34'; var status = 'sing ...

  3. Java GUI设置图标

    ImageIcon是Icon接口的一个实现类. ImageIcon类的构造函数: ImageIcon() ImageIcon(String filename)   //本地图片文件 ImageIcon ...

  4. wine使用

    wineqq 不能输入问题winecfg在 wine 设置里,选择函数库添加 riched20, 就行了(原装领先于内建) wineqq 可以输入不能输入中文问题原因:fictx组件缺失 搜狗输入法没 ...

  5. kafka安装和使用

    kafka安装和启动 kafka的背景知识已经讲了很多了,让我们现在开始实践吧,假设你现在没有Kafka和ZooKeeper环境. Step 1: 下载代码 下载0.10.0.0版本并且解压它. &g ...

  6. 关于HTML5手机端页面缩放的问题

    通常在写HTML5手机端页面的时候,我们会发现页面所显示元素的比例不正确,那此时我们需要添加的就是: <meta name="viewport" content=" ...

  7. “Debug Assertion” Runtime Error on VS2008 VS2010 winhand.cpp

    I'm writing a C++ MFC program on VS2008 and I'm getting this "Debug Assertion Error" when ...

  8. uvm_reg_model——寄存器模型(一)

    对于一个复杂设计,寄存器模型要能够模拟任意数量的寄存器域操作.UVM提供标准的基类库,UVM的寄存器模型来自于继承自VMM的RAL(Register Abstract Layer),现在可以先将寄存器 ...

  9. PHP-PHPExcel用法详解

    以下文章来源:diandian_520 http://blog.csdn.net/diandian_520/article/details/7827038 1.header header(" ...

  10. bat 批处理测试局域网速度 两端电脑

    C:\Users\Administrator>iperf3 iperf3: parameter error - must either be a client (-c) or server (- ...