描述

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

TZOJ 4325 RMQ with Shifts(线段树查询最小,暴力更新)的更多相关文章

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

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

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

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

  3. RMQ with Shifts(线段树)

    RMQ with Shifts Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u Pra ...

  4. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 D 80 Days (线段树查询最小值)

    题目4 : 80 Days 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 80 Days is an interesting game based on Jules Ve ...

  5. Codeforces295A - Greg and Array(线段树的成段更新)

    题目大意 给定一个序列a[1],a[2]--a[n] 接下来给出m种操作,每种操作是以下形式的: l r d 表示把区间[l,r]内的每一个数都加上一个值d 之后有k个操作,每个操作是以下形式的: x ...

  6. HDU 1394 Minimum Inversion Number(线段树求最小逆序数对)

    HDU 1394 Minimum Inversion Number(线段树求最小逆序数对) ACM 题目地址:HDU 1394 Minimum Inversion Number 题意:  给一个序列由 ...

  7. HDU 1754 I Hate It(线段树区间查询,单点更新)

    描述 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感.不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问.当然,老 ...

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

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

  9. 线段树模板(单点更新,区间更新,RMQ)

    Bryce1010模板 1.单点更新 说明 单点更新,区间求和(你问我单点求和??你就不会把区间长度设为0啊?) • sum[]为线段树,需要开辟四倍的元素数量的空间. • build()为建树操作 ...

随机推荐

  1. 剑指offer例题——裴波那契数列

    编程题:大家都知道裴波那契数列,现在要求输入一个整数n,请你输出裴波那契数列的第n项(从0开始,第0项为0).n<=39 public class Solution { public int F ...

  2. RabbitMQ系列教程之七:RabbitMQ的 C# 客户端 API 的简介(转载)

    RabbitMQ系列教程之七:RabbitMQ的 C# 客户端 API 的简介 今天这篇博文是我翻译的RabbitMQ的最后一篇文章了,介绍一下RabbitMQ的C#开发的接口.好了,言归正传吧. N ...

  3. Oracle数据文件转移操作

    由于oracle表空间数据文件规划问题导致当前数据文件所在文件系统空间不足,当其他文件系统空间充足情况下,可将数据文件移动到空间充足的文件系统下.本文主要描述Oracle表空间数据文件移动的操作步骤. ...

  4. centos中Mysql数据库导入sql文件

    1.对于文件的导入,在Centos下里面的是首先要新建一个和文件相同名字的数据库. mysql>create database Student; 2.切换到需要导入sql文件的数据库 mysql ...

  5. WINDOWS之CMD命令

    用法 1.切换盘符 2.切换到指定盘符后 在使用命令 cd +路径 一般介绍DOS命令,切换工作目录都是用CD命令,但是我在win7下的DOS中使用CD D:\却一直无法转到D盘. 后来在网上查找,发 ...

  6. hmac md5

    import hmac //内置 def simaplemd5(str): m2 = hashlib.md5() m2.update(str) res=m2.hexdigest() return re ...

  7. Structs复习 Action

    引入jar包 web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version= ...

  8. Monkey测试结果分析【转】

    转自[http://www.douban.com/note/257030241/] Monkey测试结果分析 一. 初步分析方法: Monkey测试出现错误后,一般的差错步骤为以下几步: 1. 找到是 ...

  9. C# DataTable To Entities

    原地址忘了,修改过了一下. new: public static class DataTableEntityInteroperate { public static List<T> ToE ...

  10. C# Excel转换为Json

    demo:https://files.cnblogs.com/files/guxingy/Excel%E8%BD%AC%E6%8D%A2%E4%B8%BAJson%E5%AF%B9%E8%B1%A1. ...