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, 85, 1, 4}, then shift(2, 4, 5, 7) yields {68, 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 (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

看题目的开始可以知道,是一个求区间最小值的问题,可以用线段树来解决,看了后半段,感觉是单点更新问题,但看了数据范围时,有点蒙了,普通的操作会超时吧。。。卡了一段时间没敢写,然后,然后,竟然过了。。。唉,没经验啊。。。

题意就是给出一段序列,有两种操作,第一种是区间查询最小值,第二种是shift,就是按照给出的位置的数据循环左移一位。

代码如下:

#include<iostream>
#include<cstring>
#include<sstream>
#include<cstdio> using namespace std;
const int N = + ;
const int INF = (<<);
int T[N<<],a[N],q[N],M,cnt;
char ch[N];
void Build(int n){
int i;
for(M=;M<=n+;M*=);
for(i=M+;i<=M+n;i++) T[i]=a[cnt++];
for(i=M-;i;i--) T[i]=min(T[i<<],T[i<<|]);
} void Updata(int n,int V){
for(T[n+=M]=V,n/=;n;n/=)
T[n]=min(T[n<<],T[n<<|]);
} int Query(int s,int t){
int minc = INF;
for(s=s+M-,t=t+M+;s^t^;s/=,t/=){
if(~s&) minc=min(minc,T[s^]);
if(t&) minc=min(minc,T[t^]);
}
return minc;
}
void Replace(char *ch,int &cur){
cur = ; int tmp=;
int len = strlen(ch);
for(int i=;i<len;i++)
if(isdigit(ch[i])){
tmp=tmp* + ch[i]-'';
if(i+==len||!isdigit(ch[i+])){
q[cur++] = tmp;
tmp = ;
}
}
}
void solve_question(int m){
int cur;
for(int i=;i<m;i++){
scanf("%s",ch);
if(ch[]=='q'){
Replace(ch,cur);
printf("%d\n",Query(q[],q[]));
}else{
Replace(ch,cur);
int t = a[q[]];
for(int i=;i<cur-;i++)
a[q[i]] = a[q[i+]];
a[q[cur-]] = t;
for(int i=;i<cur;i++)
Updata(q[i],a[q[i]]);
}
}
}
int main(){
int n,m;
scanf("%d %d",&n,&m);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
cnt = ;
Build(n);
solve_question(m);
}
 

CSU-1110 RMQ with Shifts (单点更新+区间最小值 zkw线段树)的更多相关文章

  1. HDU 1166 - 敌兵布阵 - [单点修改、区间查询zkw线段树]

    题还是那个题:http://www.cnblogs.com/dilthey/p/6827959.html 不过我们今天换一种线段树实现来做这道题: 关于zkw线段树的讲解:https://zhuanl ...

  2. 【单点更新,区间查询,线段树】【HDU1166】【敌兵布阵】

    线段树要捡回来学了 才知道以前抄的模板就是杭电传奇学长写的,写起来更有激情了: 一点注意: 单点更新完后记得pushup(),向上更新信息 以下是对线段树的理解 线段树的节点代表一段线段,节点编号没有 ...

  3. LightOJ 1112 Curious Robin Hood (单点更新+区间求和)

    http://lightoj.com/volume_showproblem.php?problem=1112 题目大意: 1 i        将第i个数值输出,并将第i个值清0 2 i v     ...

  4. HDU 1754 I Hate It(线段树之单点更新 区间最值查询)

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  5. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  6. POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)

    POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...

  7. HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)

    HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...

  8. HDOJ(HDU).1166 敌兵布阵 (ST 单点更新 区间求和)

    HDOJ(HDU).1166 敌兵布阵 (ST 单点更新 区间求和) 点我挑战题目 题意分析 根据数据范围和询问次数的规模,应该不难看出是个数据结构题目,题目比较裸.题中包括以下命令: 1.Add(i ...

  9. hdu 1166线段树 单点更新 区间求和

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

随机推荐

  1. MySQL集群安装与配置

    MySQL集群安装与配置   文章目录 [隐藏] 一.mysql集群安装 二.节点配置 三.首次启动节点 四.测试服务是否正常 五.安全关闭和重启 MySQL Cluster 是 MySQL 适合于分 ...

  2. 查询重复数据group by menu_id having count(menu_id)>1

    select * from sys_power_menu WHERE menu_id in ( select menu_id from  sys_power_menu group by menu_id ...

  3. TestingWhiz社区版2013版下载地址

    TestingWhiz社区版 https://sourceforge.net/projects/testingwhiz-community-edition/ https://sourceforge.n ...

  4. POJ 1380 Equipment Box (暴力枚举)

    Equipment Box 题目链接: http://acm.hust.edu.cn/vjudge/contest/130510#problem/B Description There is a la ...

  5. bootstrap动态调用select下拉框

    html代码: <label for="classify" class="col-sm-2 control-label">填报部门:</lab ...

  6. 爬虫 ---- BeautifulSoup的基础使用

    #BeautifulSoup的基础使用from bs4 import BeautifulSoup #导入bs4库 html = "<p class='stylecss'>< ...

  7. 【洛谷P1310 表达式的值】

    题目链接 题目描述 对于1 位二进制变量定义两种运算: 运算的优先级是: 先计算括号内的,再计算括号外的. “× ”运算优先于“⊕”运算,即计算表达式时,先计算× 运算,再计算⊕运算.例如:计算表达式 ...

  8. debugfs linux rm 删除 恢复 Attempt to read block from filesystem resulted in short read while opening filesystem

    w 删除具有空字符的文件 反斜杠来转义下一个字符 rm -R Samples\ -\ Copy well@well:/home/etc/project/apilinux/MarketplaceWebS ...

  9. 服务器上安装搭建node环境

    一.版本 : node  v4.4.3 npm  v2.15.1 pm2  v1.1.3 express v4.13.4 二.node安装与环境配置 新建node安装目录,与node项目目录     ...

  10. fedora23然后创建workspace?或者说是panel面板?

    好像在fedora23中 无法再添加工作空间workspace. 系统会自动的在非空工作空间后面再生成一个空的工作空间. 而且 工作空间 好像不只 4个, 可以有很多个. panel面板好像也不能添加 ...