RMQ with Shifts

时间限制:1000 ms  |  内存限制:65535 KB
难度:3
 
描述
    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
来源
湖南省第七届大学生计算机程序设计竞赛
题目大意:有个shift操作,也就是常见的更新,只是这里的更新比较别致,是交换i和i+1,i+1和i+2,i+2和i+3,etc(也有人理解为移动i、i+1、i+2...对应的数组中的值)。然后就是询问区间内的最小值。
解题思路:就是按题意一直更新就好了。
#include<bits/stdc++.h>
using namespace std;
#define mid (L+R)/2
#define lson rt*2,L,mid
#define rson rt*2+1,mid+1,R
const int maxn=110000;
const int INF=1e9;
int minv[maxn*4];
int a[maxn],cnt=0;
int b[5000];
char str[100];
void PushUP(int rt){
minv[rt]=min(minv[rt*2],minv[rt*2+1]);
}
void build(int rt,int L,int R){
if(L==R){
scanf("%d",&minv[rt]);
a[cnt++]=minv[rt];
return ;
}
build(lson);
build(rson);
PushUP(rt);
}
int get_b(int st,int en){
int cnt=0;
int tm,tmp=0;
for(int i=st;i<en;i++){
if(str[i]>='0'&&str[i]<='9'){
tm=str[i]-'0';
tmp*=10;
tmp+=tm;
}else{
b[cnt++]=tmp;
tmp=0;
}
}
return cnt;
}
void exchange(int la,int ra){
int tm=a[la];
a[la]=a[ra];
a[ra]=tm;
}
int query(int rt,int L,int R,int l_ran,int r_ran){
if(l_ran<=L&&R<=r_ran){
return minv[rt];
}
int ret_l=INF,ret_r=INF;
if(l_ran<=mid){
ret_l=query(lson,l_ran,r_ran);
}
if(r_ran>mid){
ret_r=query(rson,l_ran,r_ran);
}
return min(ret_l,ret_r);
}
void update(int rt,int L,int R,int pos,int val){
if(L==R){
minv[rt]=val;
return ;
}
if(pos<=mid){
update(lson,pos,val);
}else{
update(rson,pos,val);
}
PushUP(rt);
}
void debug(){
for(int i=1;i<16;i++)
printf("%d %d\n",i,minv[i]);
}
int main(){
int n,q,m,ans;
while(scanf("%d%d",&n,&q)!=EOF){
cnt=0;
build(1,1,n);
for(int i=0;i<q;i++){
scanf("%s",&str);
int len=strlen(str);
m= get_b(6,len);
if(str[0]=='q'){
ans=query(1,1,n,b[0],b[1]);
printf("%d\n",ans);
}else{
b[m]=b[0];
for(int i=0;i<m;i++){ //估计瞌睡了,这里迷糊了好久
update(1,1,n,b[i],a[b[i+1]-1]);
}
for(int i=0;i<m-1;i++){
exchange(b[i]-1,b[i+1]-1);
}
}
}
}
return 0;
}

  

nyoj 568——RMQ with Shifts——————【线段树单点更新、区间求最值】的更多相关文章

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

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

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

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

  3. POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)

    POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...

  4. HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)

    HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...

  5. hdu 1166线段树 单点更新 区间求和

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

  6. hdu1166(线段树单点更新&区间求和模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 题意:中文题诶- 思路:线段树单点更新,区间求和模板 代码: #include <iost ...

  7. hdu2795(线段树单点更新&区间最值)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 题意:有一个 h * w 的板子,要在上面贴 n 条 1 * x 的广告,在贴第 i 条广告时要 ...

  8. HDU 3308 LCIS(线段树单点更新区间合并)

    LCIS Given n integers. You have two operations: U A B: replace the Ath number by B. (index counting ...

  9. 【HDU】1754 I hate it ——线段树 单点更新 区间最值

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  10. hdu 1754 I Hate It 线段树 单点更新 区间最值

    线段树功能:update:单点更新 query:区间最值 #include <bits/stdc++.h> #define lson l, m, rt<<1 #define r ...

随机推荐

  1. ECS服务里或者阿里云服务器的二级域名设置方法

    我们要实现的效果是,xuxinshuai.abc.com ,具体怎么实现,看下面的流程 第一步:备案域名要有,假如就是www.abc.com 第二步:网站的服务器是IIS的情况下,在部署网站时,需要设 ...

  2. 独立部署GeoWebCache

    在进行GIS项目开发中,常使用Geoserver作为开源的地图服务器,Geoserver是一个JavaEE项目,常通过Tomcat进行部署.而GeoWebCache是一个采用Java实现用于缓存WMS ...

  3. Jenkins忘记密码

    当Jenkins密码忘记时,可以去Jenkins的安装目录下的users\用户名_xxxxx\config.conf文件下找下找到<passwordHash></passwordHa ...

  4. django使用auth模块进行身份认证

    https://docs.djangoproject.com/zh-hans/2.0/topics/auth/default/#authentication-in-web-requests djang ...

  5. 80端口被system(pid=4)占用

    1.查看80端口被哪个进程占用,cmd->netstat -ano | findstr 80 2.cmd->tasklist列出当前运行中的进程,或在任务管理器中查看pid为4的进程. 经 ...

  6. 洛谷P2566 [SCOI2009]围豆豆(状压dp+计算几何)

    题面 传送门 题解 首先要解决一个问题,就是怎么判断一个点是否在多边形内部 从这个点向某一个方向做一条射线,如果这条射线和多边形的交点为奇数说明在多边形内,否则在多边形外 然而有一些特殊情况,比方说一 ...

  7. Resurrectio-capserjs的自动化脚本录制工具

    [根据github上的文档说明整理] Phantom下的任何操作都可以录制 Resurrectio是一个Chrome插件,他可以记录浏览器的操作,并转化成对应的casperjs脚本 Resurrect ...

  8. P3596 [POI2015]MOD

    $ \color{#0066ff}{ 题目描述 }$ 给定一棵无根树,边权都是1,请去掉一条边并加上一条新边,定义直径为最远的两个点的距离,请输出所有可能的新树的直径的最小值和最大值 \(\color ...

  9. 7、C++枚举类型

    7.枚举类型 C++的enum工具提供了另一种创建符号常量的方式,这种方式可以代替const.它还允许定义新类型,但必须按严格的限制进行.使用enum的语法与使用结构的相似. enum spectru ...

  10. PAT天梯赛L3-015 球队食物链

    读题可以知道是DFS,注意一点,题目说的是赢过,所以str[i][j]=‘W',那么g[i][j]=1,str[i][j]='L',g[j][i]=1 然后就常规搜索即可,还有一点就是剪枝,如果没有可 ...