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 ...
随机推荐
- C# 打开PPT文件另存为PPTX
/// <summary> /// rename PPT /// </summary> private static void renamePPT() { //add refe ...
- [ACM] HDU 5025 Saving Tang Monk (状态压缩,BFS)
Saving Tang Monk Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- Linux内核中SPI/I2c子系统剖析
Linux内核中,SPI和I2C两个子系统的软件架构是一致的,且Linux内核的驱动模型都以bus,driver,device三种抽象对象为基本元素构建起来.下文的分析将主要用这三种抽象对象的创建过程 ...
- 重复记录(duplicate records)相关运营数据
MySQL 中查找反复数据,删除反复数据 创建表和測试数据 /* 表结构 */ DROPTABLEIFEXISTS `t1`; CREATETABLEIFNOTEXISTS `t1`( `id` IN ...
- 通过SSHFS在RHEL中安全的挂载远程Linux/UNIX目录或文件系统--转载
You can easily mount remote server file system or your own home directory using special sshfs and fu ...
- Android(java)学习笔记212:中文乱码的问题处理(qq登录案例)
1.我们在之前的笔记中LoginServlet.java中,我们Tomcat服务器回复给客户端的数据是英文的"Login Success","Login Failed&q ...
- 第一篇:python高级之函数
python高级之函数 python高级之函数 本节内容 函数的介绍 函数的创建 函数参数及返回值 LEGB作用域 特殊函数 函数式编程 1.函数的介绍 为什么要有函数?因为在平时写代码时,如果没 ...
- js分家效应
(原创文章,转载请注明出处) 有继承,那么就有分家.让我们看以下例子. var parents = function(){ } parents.prototype.money = 200; var c ...
- order by
- Shell - 特殊变量
$0 表示所执行程序的路径名. [huey@huey-K42JE ~]$ ll ~/bin total 4 -rwxrwxr-x 1 huey huey 21 Oct 24 14:39 hello [ ...