RMQ with Shifts

时间限制(普通/Java):1000MS/3000MS     运行内存限制:65536KByte

描述

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
#include <stdio.h>
#define MAX 100010
int a[MAX * 4];
int pos[MAX];
int min(int x,int y)
{
return x < y ? x : y;
}
void build(int l,int r,int rt)
{
if(l == r)
{
pos[l] = rt;
scanf("%d",&a[rt]);
return;
}
int m = (l + r) >> 1;
build(l,m,rt<<1);
build(m+1,r,rt<<1|1);
a[rt] = min(a[rt<<1],a[rt<<1|1]);
}
int query(int l,int r,int x,int y,int rt)
{
if(x <= l && y >= r)
return a[rt];
int m = (l + r) >> 1;
int ret = 0x7fffffff;
if(x <= m)
ret = min(ret,query(l,m,x,y,rt<<1));
if(y > m)
ret = min(ret,query(m+1,r,x,y,rt<<1|1));
return ret;
} void update(int l,int r,int rt,int x)
{
if(l == r)
return;
int m = (l + r) >> 1;
if(x <= m)
update(l,m,rt<<1,x);
else
update(m+1,r,rt<<1|1,x);
a[rt] = min(a[rt<<1],a[rt<<1|1]);
}
int main()
{
int n,m,x,y,k,len,i;
int b[100];
char str[100];
scanf("%d %d",&n,&m);
build(1,n,1);
while(m--)
{
scanf("%s",str);
for(i = 0,len = 0,k = 0; str[i]; i++)
{
if(str[i] >= '0' && str[i] <= '9')
{
k *= 10;
k += str[i] - '0';
}
else
{
if(k)
{
b[len++] = k;
k = 0;
}
}
}
if(str[0] == 'q')
printf("%d\n",query(1,n,b[0],b[1],1));
else
{
k = a[pos[b[0]]];
for(i = 0;i < len - 1; i++)
{
a[pos[b[i]]] = a[pos[b[i+1]]];
}
a[pos[b[len-1]]] = k;
for(i = 0;i < len; i++)
{
update(1,n,1,b[i]);
}
}
}
return 0;
}

TOJ 4325 RMQ with Shifts / 线段树单点更新的更多相关文章

  1. TZOJ 4325 RMQ with Shifts(线段树查询最小,暴力更新)

    描述 In the traditional RMQ (Range Minimum Query) problem, we have a static array A. Then for each que ...

  2. HDU 1754 I Hate It 线段树单点更新求最大值

    题目链接 线段树入门题,线段树单点更新求最大值问题. #include <iostream> #include <cstdio> #include <cmath> ...

  3. HDU 1166 敌兵布阵(线段树单点更新)

    敌兵布阵 单点更新和区间更新还是有一些区别的,应该注意! [题目链接]敌兵布阵 [题目类型]线段树单点更新 &题意: 第一行一个整数T,表示有T组数据. 每组数据第一行一个正整数N(N< ...

  4. poj 2892---Tunnel Warfare(线段树单点更新、区间合并)

    题目链接 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensiv ...

  5. HDU 1166 敌兵布阵(线段树单点更新,板子题)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  6. POJ 1804 Brainman(5种解法,好题,【暴力】,【归并排序】,【线段树单点更新】,【树状数组】,【平衡树】)

    Brainman Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 10575   Accepted: 5489 Descrip ...

  7. HDU 1166 敌兵布阵(线段树单点更新,区间查询)

    描述 C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况 ...

  8. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  9. HDUOJ----1166敌兵布阵(线段树单点更新)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

随机推荐

  1. 【转】C++ 智能指针详解

    一.简介 由于 C++ 语言没有自动内存回收机制,程序员每次 new 出来的内存都要手动 delete.程序员忘记 delete,流程太复杂,最终导致没有 delete,异常导致程序过早退出,没有执行 ...

  2. 15、自定义Content Provider

     自定义Content Provider的步骤    1. 编写一个类,该类必须继承自ContentProvider类. 实现ContentProvider类中所有的抽象方法. 定义Content ...

  3. Selenium用户扩展

    Selenium用户扩展 这很容易扩展Selenium IDE加入自定义操作,断言和定位,策略,这是通过添加方法,在JavaScript的帮助下Selenium 对象原型.在启动时,Selenium会 ...

  4. 【和我一起学python吧】python的数据类型

    python的元组.列表.字典数据类型是很python(there python is a adjective )的数据结构.这些结构都是经过足够优化后的,所以如果使用好的话,在某些area会有很大的 ...

  5. Tkinter教程之Menu篇

    本文转载自:http://blog.csdn.net/jcodeer/article/details/1811321 '''Tkinter教程之Menu篇''''''1.创建一个简单的Menu'''# ...

  6. PHP:PHP5.4连接SQLSERVER

    在PHP5.4的环境下要连接SQLSERVER(2005/2008)数据库,需要以下步骤: 1.下载PHP5.4连接SQLSERVER的驱动(两个动态连接库)http://www.microsoft. ...

  7. 内核源码分析之进程地址空间(基于3.16-rc4)

    所谓进程的地址空间,指的就是进程的虚拟地址空间.当创建一个进程时,内核会为该进程分配一个线性的地址空间(虚拟地址空间),有了虚拟地址空间后,内核就可以通过页表将进程的物理地址地址空间映射到其虚拟地址空 ...

  8. linux socket中的SO_REUSEADDR

    Welcome to the wonderful world of portability... or rather the lack of it. Before we start analyzing ...

  9. Spark SQL概念学习系列之如何使用 Spark SQL(六)

    val sqlContext = new org.apache.spark.sql.SQLContext(sc) // 在这里引入 sqlContext 下所有的方法就可以直接用 sql 方法进行查询 ...

  10. 从一个开发的角度看负载均衡和LVS(转)

    原文:http://blog.hesey.net/2013/02/introduce-to-load-balance-and-lvs-briefly.html 在大规模互联网应用中,负载均衡设备是必不 ...