RMQ with Shifts(线段树)
RMQ with Shifts
Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%I64d & %I64u
Description
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}.
Input
Output
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
Hint
无 //线段树的应用,不是很难,写出来还是对线段树有更深一点的了解的
#include <stdio.h>
#include <string.h> #define MAXN 100005 struct Node
{
int min;
int l,r;
}node[*MAXN];//节点
int pos[MAXN];//记录叶节点的位置
int num[MAXN];//记录最开始的数
int shift[];//记录shift里的数 int Min(int a,int b)
{
return a<b?a:b;
} int Build(int left,int right,int k)
{
node[k].l=left;
node[k].r=right;
if (left==right)//到叶节点
{
node[k].min=num[left];
pos[left]=k;
return node[k].min;
}
int mid=(left+right)/; node[k].min=Min(Build(left ,mid,*k),Build(mid+,right,*k+));
return node[k].min;
} int Query(int left,int right,int k)
{
if (left==node[k].l&&right==node[k].r)
{
return node[k].min;
}
int mid=(node[k].l+node[k].r)/;
if (left>mid) return Query(left,right,*k+);
else if (right<=mid) return Query(left,right,*k);
return Min(Query(left,mid,*k),Query(mid+,right,*k+));
} void Update(int k)
{
k/=;
while (k!=)
{
node[k].min=Min(node[*k].min,node[*k+].min);
k/=;
}
} int Get_shift(char str[])
{
int i,j;
int n=;
int len=strlen(str);
for (i=;i<len;i++)
{
int temp=;
for (j=i;str[j]!=','&&str[j]!=')';j++)
{
temp+=str[j]-'';
temp*=;
}
temp/=;
shift[++n]=temp;
i=j;
}
return n;
} int main()
{
int n,m;
int i,j;
scanf("%d%d",&n,&m);
for (i=;i<=n;i++)
scanf("%d",&num[i]); Build(,n,); //递归建树
char str[];
int left,right; for (i=;i<=m;i++)
{
scanf("%s",&str);
if (str[]=='q')
{
left=,right=;
for (j=;str[j]!=',';j++)
{
left+=str[j]-'';
left*=;
}
left/=;
for (j++;str[j]!=')';j++)
{
right+=str[j]-'';
right*=;
}
right/=;
printf("%d\n",Query(left,right,));//查找区间内最小的
}
if (str[]=='s')
{
int shift_num=Get_shift(str);//获得shift里面的数 int temp=node[pos[shift[]]].min;
for (j=;j<=shift_num;j++)
node[pos[shift[j-]]].min=node[pos[shift[j]]].min;
node[pos[shift[j-]]].min=temp; for (j=;j<=shift_num;j++)
Update(pos[shift[j]]);
} }
return ;
}
RMQ with Shifts(线段树)的更多相关文章
- UVa 12299 RMQ with Shifts(线段树)
线段树,没了.. ----------------------------------------------------------------------------------------- # ...
- 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 ...
- RMQ问题(线段树+ST算法)
转载自:http://kmplayer.iteye.com/blog/575725 RMQ (Range Minimum/Maximum Query)问题是指:对于长度为n的数列A,回答若干询问RMQ ...
- POJ 3368 Frequent values RMQ ST算法/线段树
Frequent values Time Limit: 2000MS Memory Lim ...
- [BZOJ3339] Rmq Problem(线段树)
传送门 这个题的方法好像很多啊 1.莫队暴力 2.线段树 + 离线处理 先预处理出sg[i]表示前i个数的sg值,next[i]表示i的下一位置在哪里,如果后面再没有i,那么next[i] = n + ...
- POJ 2376 Cleaning Shifts (线段树优化DP)
题目大意:给你很多条线段,开头结尾是$[l,r]$,让你覆盖整个区间$[1,T]$,求最少的线段数 题目传送门 线段树优化$DP$裸题.. 先去掉所有能被其他线段包含的线段,这种线段一定不在最优解里 ...
- BZOJ 3339 Rmq Problem(离线+线段树+mex函数)
题意: q次询问,问[l,r]子区间的mex值 思路: 对子区间[l,r],当l固定的时候,[l,r]的mex值对r单调不减 对询问按照l离线,对当前的l,都有维护一个线段树,每个叶节点保存[l,r] ...
- 蓝书4.1-4.4 树状数组、RMQ问题、线段树、倍增求LCA
这章的数据结构题很真实 T1 排队 bzoj 1699 题目大意: 求静态一些区间的最大值-最小值 思路: ST表裸题 #include<iostream> #include<cst ...
随机推荐
- oracle闪回flashback_transaction_query知识点
查询更新记录: select t.start_timestamp, t.commit_timestamp, t.logon_user, t.operation, t.table_name, t.tab ...
- asp.net服务器数据源控件学习笔记
1.数据绑定控件的DataSource属性只能接受三种接口类型的数据 (IListSource,IEnumerable,IDataSource) 2.要手动在已经绑定数据的数据绑定控件上添加自定义的数 ...
- ubuntu12.04下sun-java1.6-jdk配置
1. 下载安装 2. 设置安装的默认程序 $ sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk1.7.0_2 ...
- ANT使用 - 用for和foreach的方法遍历一个文件夹,查找到某个文件并删除
转自:http://www.cnblogs.com/QAZLIU/p/3732329.html?utm_source=tuicool&utm_medium=referral build.xml ...
- 扩充STL-编写自己的迭代器
这里的迭代器能够与STL组件共同工作,是对STL的一种扩充. 自定义迭代器必须提供iterator_traits的五种特性,分别是迭代器类型.元素类型.距离类型.指针类型与reference类型. ...
- react 打包后,项目部署完毕,刷新页面报错(404)
原因解析: 之所以你在浏览器内可以由首页跳转到其他路由地址,是因为这是由前端自行渲染的,你在React Router定义了对应的路由,脚本并没有刷新网页访问后台,是JS动态更改了location. 当 ...
- xcode几个常用的快捷键
command + ctrl + e 修改变量的名称:选中某个变量,按下该快捷键,可以批量修改对应的变量名称 command + shift + j 定位到文档导航界面,然后通过上下方向键,可以快 ...
- Android网络框架Volley
Volley是Google I/O 2013推出的网络通信库,在volley推出之前我们一般会选择比较成熟的第三方网络通信库,如: android-async-http retrofit okhttp ...
- Java后台代码调用Spring的@Service Bean的方式
比如:在我的project中有一个类CompassIndexOperation,以: @Service("CompassIndexOperation") @Transactiona ...
- SVN 钩子操作-同步更新web目录
一个简单的钩子演示:也可以网上搜索其他高级的 本次想要达到的功能是:每次用户commit 到仓库后,仓库的钩子会自动把程序又更新的www/的web发布目录 1.现在web目录下创建一个test.com ...