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

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

CN

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

  1. 我们对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.

  1. 只有一组测试用例,起始位置有两个整数n, q ( n ,, q ,),分别表示整数数组A中的元素个数,询问的数量。下一行有n个不超过100,000的非负数,皆为数组A中的初始元素。随后q行每行包含一个操作。每个操作皆为一个不超过30个字符的字符串,且不含空格。全部操作均正确有效。
  2.  
  3. 注意:数据量很大,最好使用更快的I/O函数。

CN

Output - 输出

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

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

CN

Sample Input - 输入样例

  1. 7 5
  2. 6 2 4 8 5 1 4
  3. query(3,7)
  4. shift(2,4,5,7)
  5. query(1,4)
  6. shift(1,2)
  7. query(2,2)

Sample Output - 输出样例

  1. 1
  2. 4
  3. 6

题解

一般的线段树。

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

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

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

代码 C++

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #define mx 100005
  5. int tr[mx << ], path[mx], iP, ts[mx];
  6. int build(int L, int R, int now){
  7. if (L > R) return mx;
  8. if (L == R) scanf("%d", tr + (path[++iP] = now));
  9. else{
  10. int mid = L + R >> , cL, cR;
  11. cL = build(L, mid, now << ); cR = build(mid + , R, now << | );
  12. tr[now] = std::min(cL, cR);
  13. }
  14. return tr[now];
  15. }
  16. int query(int L, int R, int now, int edL, int edR){
  17. if (edL <= L && R <= edR) return tr[now];
  18. int mid = L + R >> , cL, cR;
  19. if (edR <= mid) return query(L, mid, now << , edL, edR);
  20. if (mid < edL) return query(mid + , R, now << | , edL, edR);
  21. cL = query(L, mid, now << , edL, mid); cR = query(mid + , R, now << | , mid + , edR);
  22. return std::min(cL, cR);
  23. }
  24. void updata(int now, int n){
  25. int tmp, cL, cR;
  26. while (now >>= ){
  27. tmp = std::min(tr[now << ], tr[now << | ]);
  28. if (tmp == tr[now]) return;
  29. tr[now] = tmp;
  30. }
  31. }
  32. int main(){
  33. memset(tr, , sizeof(tr));
  34. int n, q, i, j, tmp;
  35. char op[];
  36. scanf("%d%d", &n, &q);
  37. build(, n, ); getchar();
  38. while (q--){
  39. fread(op, sizeof(char), , stdin);
  40. if (*op == 'q'){
  41. for (i = ; i < ; ++i) scanf("%d", ts + i), getchar();
  42. printf("%d\n", query(, n, , ts[], ts[]));
  43. }
  44. else{
  45. for (j = ; *op != ')'; ++j) scanf("%d", ts + j), *op = getchar();
  46. for (tmp = tr[path[ts[i = ]]]; i < j - ; ++i){
  47. tr[path[ts[i]]] = tr[path[ts[i + ]]];
  48. updata(path[ts[i]], n);
  49. }
  50. tr[path[ts[i]]] = tmp;
  51. updata(path[ts[i]], n);
  52. }
  53. getchar();
  54. }
  55. return ;
  56. }

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. 【iCore3 双核心板_FPGA】例程七:基础逻辑门实验——逻辑门使用

    实验指导书及代码包下载: http://pan.baidu.com/s/1Rs18U iCore3 购买链接: https://item.taobao.com/item.htm?id=52422943 ...

  2. lsof 一切皆文件

    Docs » 工具参考篇 » 3. lsof 一切皆文件 Docs » 工具参考篇 » 3. lsof 一切皆文件 Edit on GitHub 3. lsof 一切皆文件¶ lsof(list op ...

  3. ant copy file

    <project name="selftask" default="docopy" basedir="."> <descr ...

  4. iOS图片压缩处理

    理解概念 首先,我们必须明确图片的压缩其实是两个概念: “压” 是指文件体积变小,但是像素数不变,长宽尺寸不变,那么质量可能下降. “缩” 是指文件的尺寸变小,也就是像素数减少,而长宽尺寸变小,文件体 ...

  5. openfire及xmpp简单介绍

    一.oprenfire 1.openfire是采用Java开发,开源的实时协作(RTC)服务器基于XMPP(Jabber)协议.可以使用它轻易的构建高效率的即时通信服务器. 2.Openfire安装和 ...

  6. Centos7网络配置,vsftpd安装及530报错解决

    今天在虚拟机安装CentOS7,准备全新安装LTMP,结果又是一堆问题,不过正好因为这些出错,又给自己长了见识. 1,CentOS7网络配置 最小化安装CentOs7后,ifconfig提示comma ...

  7. VC6在win7环境下无法添加以及打开现有文件的解决办法

       在VC6.0中使用键盘快捷键或者是文件菜单打开现有文件以及添加文件出现编辑器停止响应,弹出内容为Microsoft(R) Developer Studio已停止工作 Windows正在检查解决该 ...

  8. 再叙TIME_WAIT

    之所以起这样一个题目是因为很久以前我曾经写过一篇介绍TIME_WAIT的文章,不过当时基本属于浅尝辄止,并没深入说明问题的来龙去脉,碰巧这段时间反复被别人问到相关的问题,让我觉得有必要全面总结一下,以 ...

  9. STL之优先队列(1)

    优先队列用法 在优先队列中,优先级高的元素先出队列. 标准库默认使用元素类型的<操作符来确定它们之间的优先级关系. 优先队列的第一种用法: 也是最常用的用法 priority_queue< ...

  10. 使用Jquery解析Json基础知识

    前言 在WEB数据传输过程中,json是以文本,即字符串的轻量级形式传递的,而客户端一般用JS操作的是接收到的JSON对象,所以,JSON对象和JSON字符串之间的相互转换.JSON数据的解析是关键. ...