NYOJ 1012 RMQ with Shifts (线段树)
- In the traditional RMQ (Range Minimum Query) problem, we have a static array A. Then for each query (L, R)(L \leR), 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<=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 100 characters, with no space characters inside. All operations are guaranteed to be valid.
输出
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
分析:
对于一个下标从1到n的序列,主要有两种操作,一种是query(查询)某个区间内的最小值,另一种是shift(循环左移一位)就是将这些下标对应的值循环向左移动一位,当然第一位的值会移动到最后一位。我们首先肯定是将前一位的值更改为后一位,但是在更改最后一位的时候第一位的值已经改变了,所以应该将第一位的值提前存储下来。
代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<stack>
#include<math.h>
using namespace std;
int a[100009];
struct Node
{
int left;///左区间
int right;///右区间
int num;///该区间内的最小值
} node[400009];
void build(int root,int le,int ri)
{
node[root].left=le;
node[root].right=ri;
if(le==ri)///左区间与右区间相等,相当于找到了某个特定的值
{
node[root].num=a[le];
return ;
}
int mid=(le+ri)/2;
build(root*2,le,mid);
build(root*2+1,mid+1,ri);
node[root].num=min(node[root*2].num,node[root*2+1].num);
}
void Update(int root,int n1,int n2)
{
if(node[root].left==n1&&node[root].right==n1)///先找到这个数字,然后把这个值更新了
{
node[root].num=n2;
return;
}
if(n1<=node[root*2].right)///该节点在左子树中
Update(root*2,n1,n2);
else if(n1>=node[root*2+1].left)///节点在右子树中
Update(root*2+1,n1,n2);
node[root].num=min(node[root*2].num,node[root*2+1].num);///取左右子树的较小值
}
int Query(int root,int le,int ri)
{
if(node[root].left==le&&node[root].right==ri)///找到特定的区间,返回值
{
return node[root].num;
}
else if(node[root*2].right>=ri)///区间在左子树中
{
return Query(root*2,le,ri);
}
else if(node[root*2+1].left<=le)///区间在右子树中
{
return Query(root*2+1,le,ri);
}
else
{
int mid=(node[root].left+node[root].right)/2;///左右子树中都有
int num1=Query(root*2,le,mid);
int num2=Query(root*2+1,mid+1,ri);
return min(num1,num2);
}
}
int main()
{
//freopen("1.txt","r",stdin);
int n,m;
scanf("%d%d",&n,&m);
for(int i=1; i<=n; i++)
scanf("%d",&a[i]);
char ch[109];
build(1,1,n);
while(m--)
{
int b[1000];
int j=0;
int sum=0;
scanf(" %s",ch);
for(int i=6; ch[i]!='\0'; i++)///将每个数字提取出来
{
if(ch[i]>='0'&&ch[i]<='9')
{
sum=sum*10+ch[i]-'0';
}
else
{
if(ch[i]==','||ch[i]==')')
{
b[j++]=sum;
sum=0;
}
}
}
if(ch[0]=='q')
{
printf("%d\n",Query(1,b[0],b[1]));
}
else
{
int NUM=a[b[0]];///把第一位保存下来
for(int i=0; i<j-1; i++)///先把a数组更新了,再把树进行更新
a[b[i]]=a[b[i+1]];
a[b[j-1]]=NUM;
for(int i=0; i<j-1; i++)
{
Update(1,b[i],a[b[i]]);
}
Update(1,b[j-1],NUM);
}
}
return 0;
}
NYOJ 1012 RMQ with Shifts (线段树)的更多相关文章
- UVa 12299 RMQ with Shifts(线段树)
线段树,没了.. ----------------------------------------------------------------------------------------- # ...
- 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 ...
- TOJ 4325 RMQ with Shifts / 线段树单点更新
RMQ with Shifts 时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte 描述 In the traditional RMQ (Range M ...
- nyoj 568——RMQ with Shifts——————【线段树单点更新、区间求最值】
RMQ with Shifts 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 In the traditional RMQ (Range Minimum Q ...
- RMQ问题(线段树+ST算法)
转载自:http://kmplayer.iteye.com/blog/575725 RMQ (Range Minimum/Maximum Query)问题是指:对于长度为n的数列A,回答若干询问RMQ ...
- BZOJ 1012: [JSOI2008]最大数maxnumber 线段树
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1012 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作.语法:Q L 功能: ...
- NYOJ 116 士兵杀敌 (线段树,区间和)
题目链接:NYOJ 116 士兵杀敌 士兵杀敌(二) 时间限制:1000 ms | 内存限制:65535 KB 难度:5 描写叙述 南将军手下有N个士兵,分别编号1到N,这些士兵的杀敌数都是已知的 ...
- 1012: [JSOI2008]最大数maxnumber 线段树
https://www.lydsy.com/JudgeOnline/problem.php?id=1012 现在请求你维护一个数列,要求提供以下两种操作:1. 查询操作.语法:Q L 功能:查询当前数 ...
随机推荐
- 在MFC中显示cmd命令行
添加函数 void InitConsoleWindow1() { ; FILE* fp; AllocConsole(); nCrt = _open_osfhandle((long)GetStdHand ...
- SpringMVC 应知应会
springMVC 是表现层技术,可以用来代替 struts2,下面是简略图:主要是处理器和视图,只有这两个部分需要编写代码. springMVC 三大组件:处理器映射器,处理器适配器,视图解析器. ...
- EM算法[转]
最大期望算法:EM算法. 在统计计算中,最大期望算法(EM)是在概率模型中寻找参数最大似然估计或者最大后验估计的算法,其中概率模型依赖于无法观测的隐藏变量. 最大期望算法经过两个步骤交替进行计算: 第 ...
- 【bzoj4007】[JLOI2015]战争调度 暴力+树形背包dp
题目描述 给你一棵 $n$ 层的完全二叉树,每个节点可以染黑白两种颜色.对于每个叶子节点及其某个祖先节点,如果它们均为黑色则有一个贡献值,如果均为白色则有另一个贡献值.要求黑色的叶子节点数目不超过 $ ...
- python写BMI指数菜单
需求: # 1.创建并输出菜单, 菜单是不可变的. 所以使用元组menus = ("1, 录入", "2, 查询", "3, 删除", &q ...
- 【Java】编程技术经典书籍列表
这个列表包括了 100 多本经典技术书籍,涵盖:计算机系统与网络.系统架构.算法与数据结构.前端开发.后端开发.移动开发.数据库.测试.项目与团队.程序员职业修炼.求职面试 和 编程相关的经典书籍. ...
- [NOI2016]优秀的拆分 后缀数组
题面:洛谷 题解: 因为对于原串的每个长度不一定等于len的拆分而言,如果合法,它将只会被对应的子串统计贡献. 所以子串这个限制相当于是没有的. 所以我们只需要对于每个位置i求出f[i]表示以i为开头 ...
- 【转】大数据分析(Big Data OLAP)引擎Dremel, Tenzing 以及Impala
引自:http://blog.csdn.net/xhanfriend/article/details/8434896 对于数据分析师来说,SQL是主要的语言. Hive为Hadoop提供了支持SQL运 ...
- 【Aizu2292】Common Palindromes(回文树)
[Aizu2292]Common Palindromes(回文树) 题面 Vjudge 神TMD日语 翻译: 给定两个字符串\(S,T\),询问\((i,j,k,l)\)这样的四元组个数 满足\(S[ ...
- VirtualBox安装虚拟机全过程
使用Virtual Box安装虚拟机,虚拟机操作系统使用CentOS7进行安装,安装完成后解决网络设置的问题. 一.虚拟机新建过程 1.点击新建. 2.设置内存大小,点击下一步. 3.选择虚拟硬盘,点 ...