【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次以上的区域的面 ...
随机推荐
- perl 5.22手动安装Mysql DBI和DBD
mysql 手动安装DBI 和DBD: DBI版本: [root@dr-mysql01 DBD-mysql-4.033]# perl -MDBI -le 'print $DBI::VERSION;' ...
- 基于JSP+SERVLET的新闻发布系统(三)
拖了这么久..今天把栏目管理还有新闻管理模块的也挂出来.. 栏目管理跟用户管理一样. 这里重点讲解新闻管理. 效果图如上: 1,可选择栏目类别,且栏目类别是动态生成的. 默认生成的文章是未审核状态的. ...
- Animation 的setFillAfter
疑问:如果图片从其他地方回到原先位置,则setFillAfter(false)也可以保持图片在原先位置?
- BZOJ 3505: [Cqoi2014]数三角形( 组合数 )
先n++, m++ 显然答案就是C(3, n*m) - m*C(3, n) - n*C(3, m) - cnt. 表示在全部点中选出3个的方案减去不合法的, 同一行/列的不合法方案很好求, 对角线的不 ...
- [转]使用xftp连接centos6.5
首先要在windows上安装xftp软件,这个是傻瓜式操作就不说了 安装完成之后,在centos上查看是否装了xftpd服务,[root@centos Desktop]# rpm -qa | grep ...
- Java -强引用&弱引用
⑴强引用(StrongReference) 就是通过new得的对象引用 强引用是使用最普遍的引用.如果一个对象具有强引用,那垃圾回收器绝不会回收它.当内存空间不足,Java虚拟机宁愿抛出OutOfMe ...
- 用overflow-y 解决web页面抖动问题
页面抖动(左右抖动)让人视觉上很不爽.. /** original : php攻城师 http://blog.csdn.net/phpgcs **/ 最开始我也以为是 layout 不一致的原因..后 ...
- 黑马day16 jquery&层次选择器
假设想通过DOM元素之间的层次关系来获取特定元素,比如后代元素,子元素,相邻元素,兄弟元素等,则须要使用层次选择器. 1 .ancestor descendant 使用方法: $("form ...
- uboot代码1:uboot启动大体流程, stage1 + stage2
start.S stage 1: reset: set the cpu to svc32 mode disable the watchdog mask all IRQs(INTMSK, INTSUBM ...
- JAVA中extends 与implements有啥区别?
JAVA中extends 与implements有啥区别?1. 在类的声明中,通过关键字extends来创建一个类的子类.一个类通过关键字implements声明自己使用一个或者多个接口.extend ...