CSU-1110 RMQ with Shifts (单点更新+区间最小值 zkw线段树)
In the traditional RMQ (Range Minimum Query) problem, we have a static array A. Then for each query (L, R) (L<=R), 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}.
Input
There will be only one test case, beginning with two integers n, q (1<=n<=100,000, 1<=q<=120,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
看题目的开始可以知道,是一个求区间最小值的问题,可以用线段树来解决,看了后半段,感觉是单点更新问题,但看了数据范围时,有点蒙了,普通的操作会超时吧。。。卡了一段时间没敢写,然后,然后,竟然过了。。。唉,没经验啊。。。
题意就是给出一段序列,有两种操作,第一种是区间查询最小值,第二种是shift,就是按照给出的位置的数据循环左移一位。
代码如下:
#include<iostream>
#include<cstring>
#include<sstream>
#include<cstdio> using namespace std;
const int N = + ;
const int INF = (<<);
int T[N<<],a[N],q[N],M,cnt;
char ch[N];
void Build(int n){
int i;
for(M=;M<=n+;M*=);
for(i=M+;i<=M+n;i++) T[i]=a[cnt++];
for(i=M-;i;i--) T[i]=min(T[i<<],T[i<<|]);
} void Updata(int n,int V){
for(T[n+=M]=V,n/=;n;n/=)
T[n]=min(T[n<<],T[n<<|]);
} int Query(int s,int t){
int minc = INF;
for(s=s+M-,t=t+M+;s^t^;s/=,t/=){
if(~s&) minc=min(minc,T[s^]);
if(t&) minc=min(minc,T[t^]);
}
return minc;
}
void Replace(char *ch,int &cur){
cur = ; int tmp=;
int len = strlen(ch);
for(int i=;i<len;i++)
if(isdigit(ch[i])){
tmp=tmp* + ch[i]-'';
if(i+==len||!isdigit(ch[i+])){
q[cur++] = tmp;
tmp = ;
}
}
}
void solve_question(int m){
int cur;
for(int i=;i<m;i++){
scanf("%s",ch);
if(ch[]=='q'){
Replace(ch,cur);
printf("%d\n",Query(q[],q[]));
}else{
Replace(ch,cur);
int t = a[q[]];
for(int i=;i<cur-;i++)
a[q[i]] = a[q[i+]];
a[q[cur-]] = t;
for(int i=;i<cur;i++)
Updata(q[i],a[q[i]]);
}
}
}
int main(){
int n,m;
scanf("%d %d",&n,&m);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
cnt = ;
Build(n);
solve_question(m);
}
CSU-1110 RMQ with Shifts (单点更新+区间最小值 zkw线段树)的更多相关文章
- HDU 1166 - 敌兵布阵 - [单点修改、区间查询zkw线段树]
题还是那个题:http://www.cnblogs.com/dilthey/p/6827959.html 不过我们今天换一种线段树实现来做这道题: 关于zkw线段树的讲解:https://zhuanl ...
- 【单点更新,区间查询,线段树】【HDU1166】【敌兵布阵】
线段树要捡回来学了 才知道以前抄的模板就是杭电传奇学长写的,写起来更有激情了: 一点注意: 单点更新完后记得pushup(),向上更新信息 以下是对线段树的理解 线段树的节点代表一段线段,节点编号没有 ...
- LightOJ 1112 Curious Robin Hood (单点更新+区间求和)
http://lightoj.com/volume_showproblem.php?problem=1112 题目大意: 1 i 将第i个数值输出,并将第i个值清0 2 i v ...
- HDU 1754 I Hate It(线段树之单点更新 区间最值查询)
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
- POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)
POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...
- HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)
HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...
- HDOJ(HDU).1166 敌兵布阵 (ST 单点更新 区间求和)
HDOJ(HDU).1166 敌兵布阵 (ST 单点更新 区间求和) 点我挑战题目 题意分析 根据数据范围和询问次数的规模,应该不难看出是个数据结构题目,题目比较裸.题中包括以下命令: 1.Add(i ...
- hdu 1166线段树 单点更新 区间求和
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
随机推荐
- Django【第21篇】:Ajax之FormData
ajax补充--------FormData等... 一.回顾上节知识点 1.什么是json字符串? 轻量级的数据交换格式 2.定时器:关于setTimeout setTimeout(foo,3000 ...
- MySQL导出和导入
MySQL的几句脚本 最近做了几次mysql的备份和恢复, 找了一些资料, 写了一些脚本, 记录一下. #导出 mysqldump $_login_info_ $_src_db_name_ --no- ...
- hdu 6206 : Apple 【计算几何 + 分数类】
题目链接 比赛时C++上__float128都被卡精度,然后扔给队友用Java的BigDecimal过了 算法不多说,求三角形外心可以参考 维基百科 https://zh.wikipedia.org/ ...
- js 将时间戳转为日期格式
最近项目需要在前端将一个13位的时间戳显示成日期格式,在网上查了很多都不符合要求,只有一个是能满足要求的,在这记录一下,说不定以后还用的着. 13位时间戳改为yyyy-MM-dd HH-mm-ss 格 ...
- computed属性和watcher
computed属性 在模板中使用表达式是非常方便直接的,然而这只适用于简单的操作.在模板中放入太多的逻辑,会使模板过度膨胀和难以维护.例如: <div id="example&quo ...
- Nginx 作为代理服务与负载均衡
代理服务 代理一代为办理(代理理财.代理收货等等) 代理区别 区别在于代理的对象不一样 正向代理代理的对象是客户端 反向代理代理的对象是服务端 反向代理配置 server { listen 80; s ...
- [CF1082G]Petya and Graph:最小割
分析 发这篇博客的目的就是要让你们知道博主到底有多菜. 类似于[NOI2006]最大获利.(明明就是一模一样好吧!) 不知道怎么了,半秒就想到用网络流,却没想出怎么建图. 连这么简单的题都没做出来,我 ...
- 180128-----Java面试题
1 不用第三个变量,交换两个的值 a=1;b=2;a=a+b;b=a-b;a=a-b; 2 Java动态代理用什么实现? 反射
- tab下图表展示宽高为0的问题
tab下,默认展示第一个tab(最新订阅),第二个tab是echarts,需要动态获取父级div的宽高并赋值到图表的DOM的宽高.在实际开发过程中,发现无论如何延迟处理,或者mounted,第二个ta ...
- 四、robotframework生成几种随机数
1.random()生成0<=n<1之间的随机实数--它会生成一个随机的浮点数,范围是在0.0~1.0之间.: ${num} evaluate random.random() ra ...