RMQ with Shifts

Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u

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}.

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

Hint

无

//线段树的应用,不是很难,写出来还是对线段树有更深一点的了解的
 #include <stdio.h>
#include <string.h> #define MAXN 100005 struct Node
{
int min;
int l,r;
}node[*MAXN];//节点
int pos[MAXN];//记录叶节点的位置
int num[MAXN];//记录最开始的数
int shift[];//记录shift里的数 int Min(int a,int b)
{
return a<b?a:b;
} int Build(int left,int right,int k)
{
node[k].l=left;
node[k].r=right;
if (left==right)//到叶节点
{
node[k].min=num[left];
pos[left]=k;
return node[k].min;
}
int mid=(left+right)/; node[k].min=Min(Build(left ,mid,*k),Build(mid+,right,*k+));
return node[k].min;
} int Query(int left,int right,int k)
{
if (left==node[k].l&&right==node[k].r)
{
return node[k].min;
}
int mid=(node[k].l+node[k].r)/;
if (left>mid) return Query(left,right,*k+);
else if (right<=mid) return Query(left,right,*k);
return Min(Query(left,mid,*k),Query(mid+,right,*k+));
} void Update(int k)
{
k/=;
while (k!=)
{
node[k].min=Min(node[*k].min,node[*k+].min);
k/=;
}
} int Get_shift(char str[])
{
int i,j;
int n=;
int len=strlen(str);
for (i=;i<len;i++)
{
int temp=;
for (j=i;str[j]!=','&&str[j]!=')';j++)
{
temp+=str[j]-'';
temp*=;
}
temp/=;
shift[++n]=temp;
i=j;
}
return n;
} int main()
{
int n,m;
int i,j;
scanf("%d%d",&n,&m);
for (i=;i<=n;i++)
scanf("%d",&num[i]); Build(,n,); //递归建树
char str[];
int left,right; for (i=;i<=m;i++)
{
scanf("%s",&str);
if (str[]=='q')
{
left=,right=;
for (j=;str[j]!=',';j++)
{
left+=str[j]-'';
left*=;
}
left/=;
for (j++;str[j]!=')';j++)
{
right+=str[j]-'';
right*=;
}
right/=;
printf("%d\n",Query(left,right,));//查找区间内最小的
}
if (str[]=='s')
{
int shift_num=Get_shift(str);//获得shift里面的数 int temp=node[pos[shift[]]].min;
for (j=;j<=shift_num;j++)
node[pos[shift[j-]]].min=node[pos[shift[j]]].min;
node[pos[shift[j-]]].min=temp; for (j=;j<=shift_num;j++)
Update(pos[shift[j]]);
} }
return ;
}


RMQ with Shifts(线段树)的更多相关文章

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

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

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

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

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

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

  4. RMQ问题(线段树+ST算法)

    转载自:http://kmplayer.iteye.com/blog/575725 RMQ (Range Minimum/Maximum Query)问题是指:对于长度为n的数列A,回答若干询问RMQ ...

  5. POJ 3368 Frequent values RMQ ST算法/线段树

                                                         Frequent values Time Limit: 2000MS   Memory Lim ...

  6. [BZOJ3339] Rmq Problem(线段树)

    传送门 这个题的方法好像很多啊 1.莫队暴力 2.线段树 + 离线处理 先预处理出sg[i]表示前i个数的sg值,next[i]表示i的下一位置在哪里,如果后面再没有i,那么next[i] = n + ...

  7. POJ 2376 Cleaning Shifts (线段树优化DP)

    题目大意:给你很多条线段,开头结尾是$[l,r]$,让你覆盖整个区间$[1,T]$,求最少的线段数 题目传送门 线段树优化$DP$裸题.. 先去掉所有能被其他线段包含的线段,这种线段一定不在最优解里 ...

  8. BZOJ 3339 Rmq Problem(离线+线段树+mex函数)

    题意: q次询问,问[l,r]子区间的mex值 思路: 对子区间[l,r],当l固定的时候,[l,r]的mex值对r单调不减 对询问按照l离线,对当前的l,都有维护一个线段树,每个叶节点保存[l,r] ...

  9. 蓝书4.1-4.4 树状数组、RMQ问题、线段树、倍增求LCA

    这章的数据结构题很真实 T1 排队 bzoj 1699 题目大意: 求静态一些区间的最大值-最小值 思路: ST表裸题 #include<iostream> #include<cst ...

随机推荐

  1. 为什么输入shutdown -h -t会报错:command not fount

    如果是直接用普通用户($)的身份进行输入[user@localhost ~]$ shutdown -h -t 是不能执行,因为普通用户没有关闭机器的权限. 然而直接使用[user@localhost ...

  2. python 常用的模块(hashlib)转

    摘要算法简介 Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等. 什么是摘要算法呢?摘要算法又称哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一个长度固定的数据串( ...

  3. ES6里关于类的拓展(二):继承与派生类

    继承与派生类 在ES6之前,实现继承与自定义类型是一个不小的工作.严格意义上的继承需要多个步骤实现 function Rectangle(length, width) { this.length = ...

  4. zabbix agent被动模式配置

    zabbix agent检测分为主动(agent active)和被动(agent)两种形式,主动与被动的说法均是相对于agent来讨论的.简单说明一下主动与被动的区别如下: 主动:agent请求se ...

  5. git学习资料及心得

    1. 阮一峰的(简单易懂,实用性佳) http://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html http://www.ruanyifeng ...

  6. OpenSSL 有关密钥的那些事儿(HOWTO keys)

    <DRAFT!> OpenSSL 有关密钥的那些事儿(HOWTO keys) 1. 介绍(Introduction) Keys are the basis of public key al ...

  7. Linux下的各文件夹的作用(转)

    linux下的文件结构,看看每个文件夹都是干吗用的/bin 二进制可执行命令 /dev 设备特殊文件 /etc 系统管理和配置文件 /etc/rc.d 启动的配置文件和脚本 /home 用户主目录的基 ...

  8. django book学习问题记录

    —————————————————————————————————— 位置:第五章<模型> 问题描述(已解决): >>> p1 = Publisher.objects.c ...

  9. mybatis 遇到的问题

    顺序问题:在resultmap中,result必须在association之前.否则会报错 只查出一条记录的问题  :重名的id,要起别名.而且不能忽略. mybaits错误解决:There is n ...

  10. @Autowired @Transaction @Service同时出现。

    对于@Autowired,如果只写这些,肯定是根据类型自动装配这个没问题. @Service public class AcRoleServiceImpl implements AcRoleServi ...