描述

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. 配置阿里云的金融云上的rsync

    论坛里看到易淘发的教程, 转载过来

  2. DOS 格式化日期时间输出

    if "%date:~5,2%" lss "10" (set mm=0%date:~6,1%) else (set mm=%date:~5,2%)if &quo ...

  3. day21-类的组合

    一.面向对象的组合用法 软件重用的重要方式除了继承之外还有另外一种方式,即:组合组合指的是,在一个类中以另外一个类的对象作为数据属性,称为类的组合 用组合的方式建立了类与组合的类之间的关系,它是一种‘ ...

  4. List去重问题引出来的hashCode和equals方法

    一.List 里面是基本类型的去重问题 import java.util.ArrayList; import java.util.HashSet; import java.util.List; imp ...

  5. svn从本地更新了资源库的资源后删除了某个文件夹无法恢复(已解决)

    事件描述: 删除了本地svn的某个文件夹中的某个文件后,想要恢复,但是无法恢复,解决方法如下 步骤: 1:在删除的文件夹下右键,选择TortoiseSVN——>Revert... 2:选择需要恢 ...

  6. BlockingQueue之DelayQueue的学习使用

    DelayQueue 是一中阻塞队列,需要实现接口Delayed定义的方法.做下使用记录和心得吧, @Datapublic class DelayQueueExample implements Del ...

  7. native.js 判断是否安装某app

    例:是否安装微信 function isWeixin() { var UIApplication = plus.ios.importClass("UIApplication"); ...

  8. python catch socket timeout

    python catch socket timeout import socket try: # do something. except socket.timeout as e: # socket ...

  9. 一种比较low的linux的hung分析

    在调试一个功能的时候,发现了两种hung,以前认为的hung肯定是softlock导致的,后来才发现不一定要有lock这种结构,但是有类似于锁的功能的时候,也可能触发hung,为了避免大家走弯路,故记 ...

  10. shell流程控制与循环结构

    shell脚本中的流程控制有if/else语句.case语句,循环结果包括for循环.while循环.until循环等内容. if语句 (1)最简单的if语句.使用格式有2种方式,分别如下 使用格式1 ...