UVa 12299 - RMQ with Shifts(移位RMQ)

Time limit: 1.000 seconds


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

在经典的RMQ(区间最值)问题中,给定数组A。对于每个询问(L,R) (L ≤ R),输出A[L], A[L + ], ..., A[R]中的最小值。注意下标从1开始,即最左边的元素为A[]。在这个问题中,数组A会发生些许变化:定义如下操作

CN

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.

我们对A[i1], A[i2], ..., A[ik]执行循环左移。例如,A={, , , , , , },则经过shift(,,,)后得到 {, , , , , , }。再经过shift (,)得到8, , , , , , 。

CN

Input - 输入

There will be only one test case, beginning with two integers n, q (1 ≤ n ≤ 100,000, 1 ≤ q ≤ 250,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.

只有一组测试用例,起始位置有两个整数n, q ( ≤ n ≤ ,,  ≤ q ≤ ,),分别表示整数数组A中的元素个数,询问的数量。下一行有n个不超过100,000的非负数,皆为数组A中的初始元素。随后q行每行包含一个操作。每个操作皆为一个不超过30个字符的字符串,且不含空格。全部操作均正确有效。

注意:数据量很大,最好使用更快的I/O函数。

CN

Output - 输出

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

对于每个询问,输出待求范围的最小值(非下标)。

CN

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

题解

一般的线段树。

虽然说数据量大,意思也就不能用cin吧,scanf还是可以A的。

一开始还以为需要用延迟更新,然后想了想30*25W的O(NlogN)还是应该可以的,能简则简。

代码中用的是自下往上的更新方式,目测比从上往下略快,可以跳过部分更新。

代码 C++

 #include <cstdio>
#include <cstring>
#include <algorithm>
#define mx 100005
int tr[mx << ], path[mx], iP, ts[mx];
int build(int L, int R, int now){
if (L > R) return mx;
if (L == R) scanf("%d", tr + (path[++iP] = now));
else{
int mid = L + R >> , cL, cR;
cL = build(L, mid, now << ); cR = build(mid + , R, now << | );
tr[now] = std::min(cL, cR);
}
return tr[now];
}
int query(int L, int R, int now, int edL, int edR){
if (edL <= L && R <= edR) return tr[now];
int mid = L + R >> , cL, cR;
if (edR <= mid) return query(L, mid, now << , edL, edR);
if (mid < edL) return query(mid + , R, now << | , edL, edR);
cL = query(L, mid, now << , edL, mid); cR = query(mid + , R, now << | , mid + , edR);
return std::min(cL, cR);
}
void updata(int now, int n){
int tmp, cL, cR;
while (now >>= ){
tmp = std::min(tr[now << ], tr[now << | ]);
if (tmp == tr[now]) return;
tr[now] = tmp;
}
}
int main(){
memset(tr, , sizeof(tr));
int n, q, i, j, tmp;
char op[];
scanf("%d%d", &n, &q);
build(, n, ); getchar();
while (q--){
fread(op, sizeof(char), , stdin);
if (*op == 'q'){
for (i = ; i < ; ++i) scanf("%d", ts + i), getchar();
printf("%d\n", query(, n, , ts[], ts[]));
}
else{
for (j = ; *op != ')'; ++j) scanf("%d", ts + j), *op = getchar();
for (tmp = tr[path[ts[i = ]]]; i < j - ; ++i){
tr[path[ts[i]]] = tr[path[ts[i + ]]];
updata(path[ts[i]], n);
}
tr[path[ts[i]]] = tmp;
updata(path[ts[i]], n);
}
getchar();
}
return ;
}

UVa 12299 RMQ with Shifts(移位RMQ)的更多相关文章

  1. UVa 12299 线段树 单点更新 RMQ with Shifts

    因为shift操作中的数不多,所以直接用单点更新模拟一下就好了. 太久不写线段树,手好生啊,不是这错一下就是那错一下. PS:输入写的我有点蛋疼,不知道谁有没有更好的写法. #include < ...

  2. Uva 12299 带循环移动的RMQ(线段树)

    题目链接:https://vjudge.net/contest/147973#problem/C 题意:传统的RMQ是一个不变的数组a求区间最值.现在要循环移动(往前移动). 分析:求区间问题,很容易 ...

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

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

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

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

  5. nyoj 568——RMQ with Shifts——————【线段树单点更新、区间求最值】

    RMQ with Shifts 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述     In the traditional RMQ (Range Minimum Q ...

  6. RMQ with Shifts(线段树)

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

  7. C. RMQ with Shifts

    C. RMQ with Shifts Time Limit: 1000ms Case Time Limit: 1000ms Memory Limit: 131072KB   64-bit intege ...

  8. uva 100 The 3n + 1 problem (RMQ)

    uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem= ...

  9. 【UVA】12299-RMQ with Shifts(线段树)

    改动的时候因为数据非常小,所以能够直接暴力改动,查询的时候利用线段树即可了. 14337858 option=com_onlinejudge&Itemid=8&page=show_pr ...

随机推荐

  1. IOS第二天多线程-01-延时执行

    **********延时执行 #import "HMViewController.h" @interface HMViewController () @end @implement ...

  2. A Great Alchemist

    Time limit : 2sec / Stack limit : 256MB / Memory limit : 256MB ProblemCarol is a great alchemist. In ...

  3. javascript:算法之数组去重

    一 /*******************************111111111***********************************/ /*1,最好数组去重方法,利用json的 ...

  4. 使用 Git@OSC 管理代码

    开源中国的 git 服务的地址是:http://git.oschina.net/ 以下记录 push 本地已有的项目至 git@osc 的过程. ① 注册登录之后,创建一个自己的项目: 创建好的默认项 ...

  5. 【Android测试】【第十六节】Instrumentation——初识+实战

    ◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/5503645.html 前言 有朋友给我留言说,能否介绍一下 ...

  6. cookie的作用

    Cookies是一种能够让网站服务器把少量数据储存到客户端的硬盘或内存,或是从客户端的硬盘读取数据的一种技术.Cookies是当你浏览某网站时,由Web服务器置于你硬盘上的一个非常小的文本文件,它可以 ...

  7. 中兴F412光猫超级密码破解、破解用户限制、关闭远程控制、恢复路由器拨号

    不少家庭都改了光纤入户,那肯定少不了光猫的吧.今天以中兴F412光猫为例介绍下此型号光猫超级密码的破解方法.一.F412超级密码破解方法1.运行CMD,输入telnet 192.168.1.1: 2. ...

  8. Jmeter多机并发压测IP地址问题

    meter.engine.RemoteJMeterEngineImpl: Local IP address=192.168.56.1 不能成功链家到相应的压力机 解决步骤: 1.找到jmeter.ba ...

  9. App软件开发的10个常用技巧

    移动应用市场用户争夺战日益激烈,原来做APP拼想法拼创意拼是否抓住用户痛点.现在,精细化用户体验成为了一个APP能否留存用户的关键问题,一旦用户觉得体验不畅,马上就有竞品APP后补,如何开发高性能的移 ...

  10. SQL Server ->> EXECUTE AS LOGIN/USER和Revert表达式

    EXECUTE AS LOGIN/USER和Revert表达式都是从SQL Server 2005就有.Revert的作用是用于切换当前过程的执行上下文返回上一个EXECUTE AS 语句发生之前的安 ...