RMQ with Shifts
uva12299:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3720
题意:给你n个数,然后有两个操做,shift<a1,a2,a3.....ak>从左到右一次交换,即a1和a2交换,完了之后,a2和a3交换,以此类推。query<a1,a2>,查询a1到a2区间之间的最小值。
题解:一开始没有看懂题目,看错了,看懂之后,才发现是个水题,线段树区间查询,单点更新。不过读入要小心处理一下。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=;
int n,q,a[N];
char str[];
int t1,t2;
struct SegTree{
int l,r;
int minn;
inline int mid(){
return (l+r)/;
}
}num[N*];
void pushup(int rt){
num[rt].minn=min(num[rt<<].minn,num[rt<<|].minn);
}
void build(int rt,int l,int r){
num[rt].l=l;
num[rt].r=r;
if(l==r){
num[rt].minn=a[l];
return;
}
int mid=num[rt].mid();
build(rt<<,l,mid);
build(rt<<|,mid+,r);
pushup(rt);
}
void update(int pos,int rt,int val){
if(num[rt].l==num[rt].r){
num[rt].minn=val;
return;
}
int mid=num[rt].mid();
if(mid>=pos)update(pos,rt<<,val);
else
update(pos,rt<<|,val);
pushup(rt);
}
int query(int rt,int l,int r){
if(num[rt].l==l&&num[rt].r==r){
return num[rt].minn;
}
int mid=num[rt].mid();
if(mid>=r)return query(rt<<,l,r);
else if(mid<l)return query(rt<<|,l,r);
else return min(query(rt<<,l,mid),query(rt<<|,mid+,r));
}
int main(){
scanf("%d%d",&n,&q);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
build(,,n);
for(int i=;i<=q;i++){
scanf("%6s",str);//¶ÁÈ¡6¸ö×Ö·û
if(str[]=='q'){
scanf("%d,%d)",&t1,&t2);
printf("%d\n",query(,t1,t2));
}
else{
char c;
int cnt=;
while (scanf("%d%c",&t2,&c)){
cnt++;
if(cnt==){
t1=t2;
continue;
}
int temp=a[t1];
a[t1]=a[t2];
a[t2]=temp;
update(t1,,a[t1]);
update(t2,,a[t2]);
t1=t2;
if (c!=',') break;
}
}
}
}
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 ...
- C. RMQ with Shifts
C. RMQ with Shifts Time Limit: 1000ms Case Time Limit: 1000ms Memory Limit: 131072KB 64-bit intege ...
- 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 ...
随机推荐
- golang 学习笔记
golan 声明的变量必须要用到? 语法 a,b:=2323; b为 bool 类型 结构体的赋值 需要用到逗号分隔字段 并且最后一个字段后也必须加上逗号 这和 JavaScript 的对象不一样哦 ...
- 重复记录(duplicate records)相关运营数据
MySQL 中查找反复数据,删除反复数据 创建表和測试数据 /* 表结构 */ DROPTABLEIFEXISTS `t1`; CREATETABLEIFNOTEXISTS `t1`( `id` IN ...
- MySQL 一致性读 深入研究 digdeep博客学习
http://www.cnblogs.com/digdeep/p/4947694.html 一致性读,又称为快照读.使用的是MVCC机制读取undo中的已经提交的数据.所以它的读取是非阻塞的. 相关文 ...
- Qt 学习之路 2(79):QML 组件
前面我们简单介绍了几种 QML 的基本元素.QML 可以由这些基本元素组合成一个复杂的元素,方便以后我们的重用.这种组合元素就被称为组件.组件就是一种可重用的元素.QML 提供了很多方法来创建组件.不 ...
- oracle:变长数组varray,嵌套表,集合
创建变长数组类型 ) ); 这个变长数组最多可以容纳两个数据,数据的类型为 varchar2(50) 更改元素类型的大小或精度 可以更改变长数组类型和嵌套表类型 元素的大小. ALTER TYPE ...
- setTimeout 方法用于在指定的毫秒数后调用函数或计算表达式
setTimeout 方法用于在指定的毫秒数后调用函数或计算表达式
- 查询可用的Nuget服务地址
解决访问Nuget源失败问题 查询IP址址 nslookup nuget.org 如失败,通过google 的dns服务器查询 nslookup nuget.org 8.8.8.8 将得到的Ip地址加 ...
- myEclipse修改deploy location
- spring集成 log4j + slf4j
以maven web项目为例, 首先.在pom文件引入相关依赖,如下(spring官网文档有介绍): <dependencies> <!-- spring 相关 --> < ...
- Android 安全性和权限
自定义权限 permission <permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT& ...