RMQ with Shifts (线段树)
发布日期:2021-09-19 10:55:56
浏览次数:1
分类:技术文章
本文共 2855 字,大约阅读时间需要 9 分钟。
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 theindices 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 operationshif t(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 shif t(2, 4, 5, 7) yields {6, 8, 4, 5, 4, 1, 2}. After that,shif t(1, 2) yields 8, 6, 4, 5, 4, 1, 2.InputThere 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 positiveintegers not greater than 100,000, the initial elements in array A. Each of the next q lines contains anoperation. Each operation is formatted as a string having no more than 30 characters, with no spacecharacters inside. All operations are guaranteed to be valid.Warning: The dataset is large, better to use faster I/O methods.OutputFor each query, print the minimum value (rather than index) in the requested range.Sample Input7 56 2 4 8 5 1 4query(3,7)shift(2,4,5,7)query(1,4)shift(1,2)query(2,2)Sample Output146题目大概:
给你n个数,m条询问。每条询问,可以是查询区间最小值,或者是改变某些值的位置,即把第二个数的值放在第一个,第三个数放在第二个,第一个放在最后一个。形成循环,相当于单点更新。
思路:
直接用线段树维护区间最小值就行了,不过输入比较麻烦。
代码:
#includeusing namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1const int maxn=1e5+10;const int INF=0x3f3f3f3f;int sum[maxn<<2];int a[maxn];int b[maxn];int cnt=0;char q[100];int get_b(int st,int en){ int cnt=0; int tm,tmp=0; for(int i=st;i ='0'&&q[i]<='9'){ tm=q[i]-'0'; tmp*=10; tmp+=tm; }else{ b[cnt++]=tmp; tmp=0; } } return cnt;}void pushup(int rt){ sum[rt]=min(sum[rt<<1],sum[rt<<1|1]);}void build(int l,int r,int rt){ if(l==r) { scanf("%d",&sum[rt]); a[++cnt]=sum[rt]; return; } int m=(l+r)>>1; build(lson); build(rson); pushup(rt);}void update(int p,int sc,int l,int r,int rt){ if(l==r) { sum[rt]=sc; return; } int m=(l+r)>>1; if(p<=m)update(p,sc,lson); else update(p,sc,rson); pushup(rt);}int quert(int L,int R,int l,int r,int rt){ if(L<=l&&r<=R) { return sum[rt]; } int m=(l+r)>>1; int ret=INF; if(L<=m)ret=min(ret,quert(L,R,lson)); if(R>m)ret=min(ret,quert(L,R,rson)); return ret;}int main(){ int n,m; scanf("%d%d",&n,&m); build(1,n,1); while(m--) { scanf("%s",q); int l=strlen(q); int le=get_b(6,l); if(q[0]=='s') { int pre=b[0]; int ans1=a[pre]; for(int i=1;i
转载地址:https://blog.csdn.net/a1046765624/article/details/80752694 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
表示我来过!
[***.240.166.169]2024年04月18日 00时11分10秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Ngrok内网穿透教程(国内地址)
2019-05-03
SpringBoot利用AOP防止请求重复提交
2019-05-03
Linux下安装Mysql5..7(Centos7)--亲测
2019-05-03
Linux下安装Nginx(Centos7)
2019-05-03
Linux下安装JDK(Centos7)
2019-05-03
SQL优化--大数据量模糊查询缓慢
2019-05-03
Linux安装Zookeeper(Centos7)
2019-05-03
ACM进阶计划(来自于南阳理工学院)
2019-05-03
Scala学习第八天 Scala主构造器、私有构造器、构造器重载实战详解
2019-05-03
Scala学习第九天 Scala的内部类实战详解
2019-05-03
Scala学习第十天 Scala单例对象、伴生对象实战详解
2019-05-03
Scala学习第十一天 Scala中的apply实战详解
2019-05-03
Scala学习第七天 Scala类的属性和对象私有字段实战详解
2019-05-03
Scala学习第六天 Map、Tuple、Zip实战解析
2019-05-03
Scala学习第四天 Scala的For与Function进阶实战、Lazy的使用
2019-05-03
Scala学习第三天 Tuple、Array、May与文件操作入门实战
2019-05-03
Scala学习第二天 Scala函数定义、流程控制、异常处理
2019-05-03
Scala学习第五天 Scala数组操作实战详解
2019-05-03
基于key-value的存储系统Redis
2019-05-03
Scala学习第十二天 Scala中的继承:超类的构造、重写字段、重写方法代码实战
2019-05-03