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 ...
随机推荐
- Console-算法:fun1(do while)
ylbtech-Arithmetic:Console-算法[do while]-XX 1.A,Demo(案例) 1.B,Solution(解决方案) using System; namespace ...
- iptables利用connlimit模块限制同一IP连接数
connlimit功能: connlimit模块允许你限制每个客户端IP的并发连接数,即每个IP同时连接到一个服务器个数. connlimit模块主要可以限制内网用户的网络使用,对服务器而言则可以限制 ...
- osg节点统计方法(点数 面数) 【转】
void statusNode(osg::ref_ptr<osg::Node> node,int& verNum,int& faceNum){ osg::ref_ptr&l ...
- ElasticSearch 相关性
1.相关性 ElasticSearch检索结果是按照相关性倒序排列的,相关性是什么,相关性又是如何计算的?每个文档都有相关性评分,用一个正浮点数字段 _score 来表示 . _score 的评分越高 ...
- spring boot 缺点优点?
作者:八面山人链接:https://www.zhihu.com/question/39483566/answer/246333825来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...
- 【Hadoop】Hadoop 机架感知配置、原理
Hadoop机架感知 1.背景 Hadoop在设计时考虑到数据的安全与高效,数据文件默认在HDFS上存放三份,存储策略为本地一份, 同机架内其它某一节点上一份,不同机架的某一节点上一份. 这样如果本地 ...
- 用于快速排查Java的CPU性能问题(top us值过高)
转载于GIT路径 https://github.com/oldratlee/useful-scripts/blob/master/docs/java.md#beer-show-busy-java-th ...
- 2017.7.27 logback配置文件
参考来自: Java深入 - logback的配置和使用 1 模块组成 logback分成三个模块:logback-core,logback- classic和logback-access. logb ...
- EffectiveJava(19)导出常量的几种方式 - - 接口只用于定义类型
package com.classinteface.finalinterface; /** * 常量接口模式 java.io.ObjectStreamConstants * 这种模式会导致实现其的类将 ...
- python-mysql-replication
python处理mysql binlog增量日志 http://python-mysql-replication.readthedocs.io/en/latest/examples.html 同样的项 ...