首页 技术 正文
技术 2022年11月15日
0 收藏 684 点赞 4,058 浏览 2120 个字

描述

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

输入

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.

输出

For each query, print the minimum value (rather than index) in the requested range.

样例输入

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)

样例输出

1
4
6

题意

给你N个数,有两个操作

1.查询区间最小

2.给定几个位置,循环左移

题解

可以看出循环左移的区间不是很大,那么直接拿线段树暴力更新

代码

 #include<bits/stdc++.h>
using namespace std; #define ll long long const int maxn=1e5+; int n,q;
int Min[maxn<<]; void build(int l,int r,int rt)
{
if(l==r)
{
scanf("%d",&Min[rt]);
return;
}
int mid=(l+r)>>;
build(l,mid,rt<<);
build(mid+,r,rt<<|);
Min[rt]=min(Min[rt<<],Min[rt<<|]);
} void update(int L,int c,int l,int r,int rt)
{
if(l==r)
{
Min[rt]=c;
return;
}
int mid=(l+r)>>;
if(L<=mid)update(L,c,l,mid,rt<<);
else update(L,c,mid+,r,rt<<|);
Min[rt]=min(Min[rt<<],Min[rt<<|]);
} int query(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
return Min[rt];
int mid=(l+r)>>,ans=0x3f3f3f3f;
if(L<=mid)ans=min(ans,query(L,R,l,mid,rt<<));
if(R>mid)ans=min(ans,query(L,R,mid+,r,rt<<|));
return ans;
} int main()
{
scanf("%d%d",&n,&q);
build(,n,);
for(int i=;i<q;i++)
{
getchar();
int pre,cur;
if(getchar()=='s')
{
while(getchar()!='(');
scanf("%d",&pre);
int first=query(pre,pre,,n,);
while(getchar()!=')')
{
scanf("%d",&cur),
update(pre,query(cur,cur,,n,),,n,);
pre=cur;
}
update(pre,first,,n,);
}
else
{
while(getchar()!='(');
scanf("%d,%d)",&pre,&cur);
printf("%d\n",query(pre,cur,,n,));
}
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289