【UVA】12299-RMQ with Shifts(线段树)
改动的时候因为数据非常小,所以能够直接暴力改动,查询的时候利用线段树即可了。
| 14337858 |
option=com_onlinejudge&Itemid=8&page=show_problem&problem=3720" style="font-size:13.1428575515747px; margin:0px; padding:0px; color:rgb(153,0,0); text-decoration:none">12299 |
RMQ with Shifts | Accepted | C++ | 0.282 | 2014-10-11 16:02:53 |
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
#define lson pos<<1
#define rson pos<<1|1
const int maxn = 111111;
const int INF = 111111;
int arr[maxn];
int sz;
struct Node{
int l,r;
int _min;
}node[maxn <<2];
void BuildTree(int L,int R,int pos){
node[pos].l = L; node[pos].r = R;
if(L == R){
scanf("%d",&node[pos]._min);
arr[sz ++] = node[pos]._min;
return ;
}
int m = (L + R) >> 1;
BuildTree(L,m,lson);
BuildTree(m + 1,R,rson);
node[pos]._min = min(node[lson]._min,node[rson]._min);
return;
}
void GetNum(int &l,int &r,char *str){
int L = strlen(str),j;
for(j = 0; j < L; j++){
if(str[j] >= '0' && str[j] <= '9'){
l = l * 10 + (str[j] - '0');
}
else if(l)
break;
}
for(;j < L; j++){
if(str[j] >= '0' && str[j] <= '9'){
r = r * 10 + str[j] - '0';
}
else if(r)
break;
}
return ;
}
int Query(int L,int R,int pos){
if(L <= node[pos].l && node[pos].r <= R)
return node[pos]._min;
int m = (node[pos].l + node[pos].r) >> 1;
int ret = INF;
if(L <= m)
ret = min(ret,Query(L,R,lson));
if(R > m)
ret = min(ret,Query(L,R,rson));
return ret;
}
void UpDate(int aim,int value,int pos){
if(node[pos].l == node[pos].r){
node[pos]._min = value;
return ;
}
int m = (node[pos].l + node[pos].r) >> 1;
if(aim <= m)
UpDate(aim,value,lson);
else
UpDate(aim,value,rson);
node[pos]._min = min(node[lson]._min,node[rson]._min);
return;
}
int main(){
int n,q;
while(scanf("%d%d",&n,&q) != EOF){
sz = 1;
BuildTree(1,n,1);
char str[100];
int array[30];
for(int x = 0; x < q; x++){
scanf("%s",str);
if(str[0] == 'q'){
int l = 0,r = 0;
GetNum(l,r,str);
printf("%d\n",Query(l,r,1));
}
else{
memset(array,0,sizeof(array));
int size = 0, n = strlen(str);
for(int j = 0 ; j < n; j++){
if(str[j] >= '0' && str[j] <= '9'){
array[size] = array[size] * 10 + str[j] - '0';
}
else if(array[size] != 0){
size ++;
}
}
//左移一位
int temp = arr[array[0]];
for(int i = 0; i < size; i++){
int e;
if(i < size - 1){
UpDate(array[i],arr[array[i + 1]],1);
arr[array[i]] = arr[array[i + 1]];
}
else{
UpDate(array[i],temp,1);
arr[array[i]] = temp;
}
}
}
// for(int i = 1 ;i <= n; i++) printf("%d ",arr[i]);
// printf("\n");
}
}
return 0;
}
【UVA】12299-RMQ with Shifts(线段树)的更多相关文章
- UVa 12299 RMQ with Shifts(线段树)
线段树,没了.. ----------------------------------------------------------------------------------------- # ...
- UVa 12299 RMQ with Shifts(移位RMQ)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: "Times New ...
- HDU 1754 - I Hate It & UVA 12299 - RMQ with Shifts - [单点/区间修改、区间查询线段树]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754 Time Limit: 9000/3000 MS (Java/Others) Memory Li ...
- 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 ...
- UVA 12299 RMQ with shifts
就是线段树的单点修改和区间查询. 然而输入打了一个小时才弄清楚. #include<iostream> #include<cstdio> #include<cstring ...
- UVA 12299 RMQ with Shifts(线段树:单点更新)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- RMQ问题(线段树+ST算法)
转载自:http://kmplayer.iteye.com/blog/575725 RMQ (Range Minimum/Maximum Query)问题是指:对于长度为n的数列A,回答若干询问RMQ ...
- UVA 11983 Weird Advertisement(线段树求矩形并的面积)
UVA 11983 题目大意是说给你N个矩形,让你求被覆盖k次以上的点的总个数(x,y<1e9) 首先这个题有一个转化,吧每个矩形的x2,y2+1这样就转化为了求N个矩形被覆盖k次以上的区域的面 ...
随机推荐
- 如何将一个Jsp网站打包发布(发布为War文件)
链接地址:http://blog.csdn.net/luohuijun619/article/details/4867131 版权声明:本文为博主原创文章,未经博主允许不得转载. 网站做完后,并不是直 ...
- BZOJ 1537: [POI2005]Aut- The Bus(dp + BIT)
对y坐标离散化, 然后按x坐标排序, dp. 一个点(x, y), 设到达这个点接到的最多乘客数为t, 那么t可以用来更新y'>=y的所有点.用树状数组维护最大值. -------------- ...
- JVM调优总结(一)-- 一些概念
数据类型 Java虚拟机中,数据类型可以分为两类:基本类型和引用类型.基本类型的变量保存原始值,即:他代表的值就是数值本身:而引用类型的变量保存引用值.“引用值”代表了某个对象的引用,而不是对象本身, ...
- python使用libssh2连接linux
1.安装(1)使用下面命令获得最新版本的ssh4py安装包 git clone git://github.com/wallunit/ssh4py (2)解压ssh4py后使用下面命令进行安装: ...
- Oracle成长点点滴滴(3)— 权限管理
上篇我们解说了创建用户以及主要的授权问题.以下我们来解说权限包含对象权限和系统权限. 事实上上节课我们解说就是系统的权限.系统权限就是一些创建表了,表空间等等的系统的权限. 1. 系统权限 ...
- HDURevenge of Segment Tree(第二长的递增子序列)
HDURevenge of Segment Tree(第二长的递增子序列) 题目链接 题目大意:这题是求第二长的递增子序列. 解题思路:用n^2的算法来求LIS,可是这里还要记录一下最长的那个序列是否 ...
- Bootstrap "row"类宽度超过问题
问题原因: VOORBootstrap门格系统布局,类别col-xs-*身边有15px的padding,在这样的元素img我们希望展现的顶部边缘,这需要col-xs-*式:padding:0px; 如 ...
- fcntl()
fcntl() F_GETFL--------------------------------------------- 将文件状态标志作为函数值返回. 文件状态标志: ...
- nodejs安装不了和npm安装不了的解决方法
http://caibaojian.com/nodejs-roll-back.html
- c语言,结构体
数据类型分为4种, 简单数据类型,构造数据类型,指针数据类型,空类型. 结构体属于构造数据类型,用struct标识. 声明一个结构体: Typedef struct和struct c c++ typ ...