【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次以上的区域的面 ...
随机推荐
- atlas z 轴
问题源自一个帖子,因为上传的图比较多,就另开了这个贴写下自己的试验结果,原帖在下面链接中 http://game.ceeger.com/forum/read.php?tid=8911#info NGU ...
- include与jsp:include区别
jsp 中include有两种形式,分别是 <%@ include file=” ”%> <jsp:include page=” ” flush=”true”/> 前者是指令元 ...
- SymmetricDS文档翻译--【Chapter 3. 具体配置(Configuration)[section C]】
3.6. Routers 眼下的实现中提供的Route实现包括: 1. Default Router:这个Router发送全部的数据到Router中定义的目标节点所属的组中的全部的节点. 2 ...
- [leetcode] Reverse Linked List 分类: leetcode 算法 2015-07-09 18:44 2人阅读 评论(0) 收藏
反转链表:比较简单的问题,可以遍历也可以递归. # Definition for singly-linked list. class ListNode: def __init__(self, x): ...
- null的小扩展
注意:JavaScript有6种数据类型,其中是五种基本数据类型,分别是:Undefined.Null.Boolean.Number 和String,还有一种复杂的数据类型Object 使用typeo ...
- servlet的url-pattern匹配规则详细描述
一.概述 在利用servlet或Filter进行url请求的匹配时,很关键的一点就是匹配规则,但servlet容器中的匹配规则既不是简单的通配,也不是正则表达式,而是由自己的规则,比较容易混淆.本文来 ...
- Linux解压乱码
.向系统添加windows下的字符编码: sudo vim /var/lib/locales/supported.d/local 添加一下编码: zh_CN.GBK GBK zh_CN.GB2312 ...
- OPENSSL库的使用-DES篇
一.单DES算法ECB模式加解密 1.使用函数DES_set_key_unchecked设置密钥 2.使用函数DES_ecb_encrypt来进行数据加解密 void DES_ecb_encrypt( ...
- Pro Android 4 第五章 理解Intent
Android引入了一个名为Intent的概念用来唤醒各种组件.Android中的组件包括:activities(UI 组件),services(后台代码),broadcast receiv ...
- Linux中TCP wrapper的使用
Linux中TCP wrapper的使用 tcpwrapper的目的是对那些访问控制功能较弱的服务提供访问控制功能要想了解访问控制就必须先知道服务监听的概念: 服务监听的两种方式: listen ...