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) (LR), 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 ( 1n100, 000, 1q250, 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 ...
随机推荐
- 绘制复杂的原理图元件和pcb封装库用于cadence(一)
绘制TI公司的TPS53319电源芯片封装 由于产品设计需要大电流电源供电,选用TI公司TPS53319电源芯片通过cadence软件进行电路设计,但是TI公司所提供的封装格式为CAD File(.b ...
- 状态模式和php实现
状态模式: 允许一个对象在其内部状态改变时改变它的行为,对象看起来似乎修改了它的类.其别名为状态对象(Objects for States),状态模式是一种对象行为型模式. 模式分析: 在很多情况下, ...
- .aspx设置跨域
在web.config添加节点 <system.webServer>下添加 <httpProtocol> <customHeaders> & ...
- Java文件操作系列[1]——PDFBox实现分页提取PDF文本
需求:用java分页提取PDF文本. PDFBox是一个很好的可以满足上述需求的开源工具. 1.PDF文档结构 要解析PDF文本,我们首先要了解PDF文件的结构. 关于PDF文档,最重要的几点: 一, ...
- PHP识别二维码功能,php-zbarcode 安装
php-zbarcode是PHP识别二维码的扩展. 下面是安装方法,安装前要先安装ImageMagick.zbar. php-zbarcode 下载地址 安装ImageMagick: yum inst ...
- POJ 1947 Rebuilding Roads (树形DP)
题意:给一棵树,在树中删除一些边,使得有一个连通块刚好为p个节点,问最少需要删除多少条边? 思路: 因为任一条边都可能需要被删除,独立出来的具有p个节点的连通块可能在任意一处地方.先从根开始DFS,然 ...
- 强化学习_PolicyGradient(策略梯度)_代码解析
使用策略梯度解决离散action space问题. 一.导入包,定义hyper parameter import gym import tensorflow as tf import numpy as ...
- flex常用属性
<1>align-items: 垂直方向的对齐方式 align-items: stretch(拉伸,布满父容器) | center(垂直居中) | flex-start(上对齐) | fl ...
- 2890: C--去掉+86
2890: C--去掉+86 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 210 Solved: 91[Submit][Status][Web Bo ...
- python_112_断言
#断言 如果满足断言的执行程序,如果不满足则抛错误 assert type(1) is int print('断言正确的话,就继续执行') # assert type('a') is int #Ass ...