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. HTML 5的革新——语义化标签(一)HTML 5的革新——语义化标签(二)

    HTML 5的革新之一:语义化标签一节元素标签. 在HTML 5出来之前,我们用div来表示页面章节,但是这些div都没有实际意义.(即使我们用css样式的id和class形容这块内容的意义).这些标 ...

  2. 基于.NET网页开发的工作,需要掌握的知识点

    学习计划对于程序员来说尤为重要,我最近根据自己的职业规划和招聘网站上对于基于.NET网页开发工作所需要的技能做出了一个总结,这个总结的内容也将是自己最近一年的知识补充和学习的方向,各位园友也可以把它作 ...

  3. sql 使用汇总(PQSQL)

    --sql structured query language --DML--Data Manipulation Language--数据操作语言 query information (SELECT) ...

  4. Python学习日志9月13日

    昨天的学习日志没有写,乱忙了一整天,政治电脑. 好奇心重,想要给电脑装上传说中LInux操作系统,各种小问题折腾到半夜,今天又折腾到晚上才真正的装上系统. 可是装上系统后又发现各种的不好用.虽然界面比 ...

  5. 霍金的新语音系统 ACAT 将开源

    英国理论物理学家斯蒂芬·霍金(Stephen Hawking)使用了二十年的语音通讯系统被英特尔开发的新一代通讯平台替代,显著改进了通讯效率.但霍金的声音并没有发生改变,他仍然使用相同的语音合成器.霍 ...

  6. 如何解决webpack中css背景图片的绝对地址

    在项目开发中,一般写相对路径是没有问题的,但是在项目比较大的情况下,我的scss文件可能为了方便管理,会放在不同的文件夹下,有的可能又不需要放在文件夹下,比如我的scss文件结构如下: module ...

  7. javaEE(5)_Cookie和Session

    一.会话 1.什么是会话?会话可简单理解为:用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话.类似打电话一样.2.会话过程中要解决的一些问题?每个用户 ...

  8. 学习C语言库函数

    使用C语言功能强大的函数,我们需要包含头文件 #include<math.h>. 1)取两个数的较大值或较小值函数: double a = 9.9; double b = 6.6; pri ...

  9. 【译】Swift 字符串速查表

    [译]Swift 字符串速查表 2015-12-18 10:32 编辑: suiling 分类:Swift 来源:CocoaChina翻译活动 10 5585 Swift字符串 招聘信息: iOS高级 ...

  10. grep-sed命令用法:

    用户切换 su username:非登录式切换 su - username:登录式切换 su -l username:登录式切换 su username -c COMMAND   echo -n   ...