C. RMQ with Shifts
C. RMQ with Shifts
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
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.
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.
Output
For each query, print the minimum value (rather than index) in the requested range.
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 解题:RMQ问题,更新比较有新意。。。。。。。。。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <climits>
#include <algorithm>
#include <cmath>
#define LL long long
using namespace std;
const int maxn = ;
struct node{
int lt,rt,minVal;
}tree[maxn<<];
int d[maxn],u[],cnt;
void build(int lt,int rt,int v){
tree[v].lt = lt;
tree[v].rt = rt;
if(lt == rt){
tree[v].minVal = d[lt];
return;
}
int mid = (lt+rt)>>;
build(lt,mid,v<<);
build(mid+,rt,v<<|);
tree[v].minVal = min(tree[v<<].minVal,tree[v<<|].minVal);
}
int query(int lt,int rt,int v){
if(tree[v].lt == lt && tree[v].rt == rt) return tree[v].minVal;
int mid = (tree[v].lt+tree[v].rt)>>;
if(rt <= mid) return query(lt,rt,v<<);
else if(lt > mid) return query(lt,rt,v<<|);
else return min(query(lt,mid,v<<),query(mid+,rt,v<<|));
}
void update(int lt,int rt,int v){
if(tree[v].lt == tree[v].rt){
tree[v].minVal = d[tree[v].lt];
return;
}
int mid = (tree[v].lt+tree[v].rt)>>;
if(u[rt] <= mid) update(lt,rt,v<<);
else if(u[lt] > mid) update(lt,rt,v<<|);
else{
int i;
for(i = lt; u[i] <= mid; i++);
update(lt,i-,v<<);
update(i,rt,v<<|);
}
tree[v].minVal = min(tree[v<<].minVal,tree[v<<|].minVal);
}
int main(){
int n,m,i,j,len,temp;
char str[];
while(~scanf("%d%d",&n,&m)){
for(i = ; i <= n; i++)
scanf("%d",d+i);
build(,n,);
for(i = ; i < m; i++){
scanf("%s",str);
len = strlen(str);
for(cnt = j = ; j < len;){
if(str[j] < '' || str[j] > '') {j++;continue;}
temp = ;
while(j < len && str[j] >= '' && str[j] <= '') {temp = temp* + (str[j]-'');j++;}
u[cnt++] = temp;
}
if(str[] == 'q'){
printf("%d\n",query(u[],u[],));
}else{
temp = d[u[]];
for(cnt--,j = ; j < cnt; j++)
d[u[j]] = d[u[j+]];
d[u[j]] = temp;
update(,cnt,);
}
}
}
return ;
}
C. RMQ with Shifts的更多相关文章
- UVa 12299 RMQ with Shifts(移位RMQ)
		
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: "Times New ...
 - TOJ 4325 RMQ with Shifts / 线段树单点更新
		
RMQ with Shifts 时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte 描述 In the traditional RMQ (Range M ...
 - UVa 12299 RMQ with Shifts(线段树)
		
线段树,没了.. ----------------------------------------------------------------------------------------- # ...
 - nyoj 568——RMQ with Shifts——————【线段树单点更新、区间求最值】
		
RMQ with Shifts 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 In the traditional RMQ (Range Minimum Q ...
 - RMQ with Shifts(线段树)
		
RMQ with Shifts Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%I64d & %I64u Pra ...
 - TZOJ 4325 RMQ with Shifts(线段树查询最小,暴力更新)
		
描述 In the traditional RMQ (Range Minimum Query) problem, we have a static array A. Then for each que ...
 - NYOJ   1012    RMQ with Shifts     (线段树)
		
题目链接 In the traditional RMQ (Range Minimum Query) problem, we have a static array A. Then for each q ...
 - 树状数组求最大值 (RMQ with Shifts)
		
代码: #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib ...
 - CSU-1110 RMQ with Shifts (单点更新+区间最小值 zkw线段树)
		
In the traditional RMQ (Range Minimum Query) problem, we have a static array A. Then for each query ...
 
随机推荐
- ubuntu下安装无线网卡去驱动Qualcomm-Atheros-QCA9377
			
文件(文档和压缩包):http://pan.baidu.com/s/1mhktFT2
 - 洛谷 P2483 [SDOI2010]魔法猪学院
			
题目描述 iPig在假期来到了传说中的魔法猪学院,开始为期两个月的魔法猪训练.经过了一周理论知识和一周基本魔法的学习之后,iPig对猪世界的世界本原有了很多的了解:众所周知,世界是由元素构成的:元素与 ...
 - 洛谷 P2264 情书
			
题目背景 一封好的情书需要撰写人全身心的投入.lin_toto同学看上了可爱的卡速米想对她表白,但却不知道自己写的情书是否能感动她,现在他带着情书请你来帮助他. 题目描述 为了帮助lin_toto,我 ...
 - org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'userId' not found. Available parameters are [arg1, arg0, param1, param2]
			
2018-06-27 16:43:45.552 INFO 16932 --- [nio-8081-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : ...
 - strongSwan大坑一直重启(ubuntu)
			
报错 Starting strongSwan 5.3.2 IPsec [starter]... charon (20533) started after 40 ms charon stopped af ...
 - JNI工程搭建及编译
			
JNI工程搭建及编译 建立Java工程 在具有C/C++比编译器的Eclipse中进行工程的创建,先创建一个简单的Java project,选项和一般同,这里仅仅需要将要调用的C/C++函数声明为na ...
 - UVA1665 Islands (并查集)
			
补题,逆序考虑每个询问的时间,这样每次就变成出现新岛屿,然后用并查集合并统计.fa = -1表示没出现. 以前写过,但是几乎忘了,而且以前写得好丑的,虽然常数比较小,现在重新写练练手.每个单词后面都要 ...
 - 5 Options for Distributing Your iOS App to a Limited Audience
			
http://mobiledan.net/2012/03/02/5-options-for-distributing-ios-apps-to-a-limited-audience-legally/ I ...
 - Heacher互助平台 α版本冲刺
			
课程属性 作业课程 https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass1/ 作业链接 https://edu.cnblogs.co ...
 - How to debug add-ins for arcgis
			
Debugging add-ins To debug an add-in, follow these steps: Confirm that the add-in is deployed to the ...