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. python核心编程学习记录之正则表达式

  2. Git历险记(二)——Git的安装和配置

    各位同学,上回Git历险记(一)讲了一个 “hello Git” 的小故事.有的同学可能是玩过了其它分布式版本控制系统(DVCS),看完之后就触类旁通对Git就了然于胸了:也有的同学可能还如我当初入手 ...

  3. JStorm的搭建文档

    1.下载jstorm的jar包 https://github.com/alibaba/jstorm/releases 2.解压jstorm的包 tar -xvf jstorm-2.4.0.tgz mv ...

  4. solrCloud分布式检索流程

    FROM: http://blog.csdn.net/duck_genuine/article/details/17014991 好久没写solr的文章了,刚好需要在公司作个分享,先总结一些先. 引用 ...

  5. 使用BAT编译,链接,执行汇编代码

    大家都知道汇编程序(MASM)的上机过程,先要对源代码进行汇编. 连接,然后再执行,而这中间有很多环节需要输入很多东西,麻烦的很(只有经历过的朋友才懂得).如何使这个过程变的简单呢?在我们搞汇编课程设 ...

  6. Cocos2d-x 3.2 大富翁游戏项目开发-第七部分 获取角色路径_2

    在编写获取路径方法前,我们先把角色须要的动画文件载入进来,角色的文件为png 和 plist格式. player1_anim.png.plist             player1_anim.pn ...

  7. Centos 通过yum的方式升级内核

    在安装某些软件时,可能对我们的系统内核版本有要求. 比如在安装docker要满足一定的条件,对于centos系统,要求必须是64位,并且内核版本是3.10以上. 如果你的centos操作系统内核低于3 ...

  8. 双人对战的球类游戏ios源代码项目

    双人对战的球类游戏源代码,这个是一款双人对战的ios球类游戏源代码.游戏的源代码也比較具体的,我们在屏幕上下看到各有一个球门.内有一球,两边通过控制轮盘使球进入对方的球门的.事实上玩法也非常easy的 ...

  9. 【转载】WEB系统性能问题的分析定位方法

    以一个典型的WEB系统来举例,性能问题一般体现在客户端请求后的响应时间上.在性能测试过程中,即压力增大到某个程度后,响应时间指标迅速增长.但如那篇文章所说,这只能叫做一个现象,测试人员需要找到问题所在 ...

  10. List of CentOS Mirrors

    From:https://www.centos.org/download/mirrors/ CentOS welcomes new mirror sites. If you are consideri ...