【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次以上的区域的面 ...
随机推荐
- jQuery 遍历ul li 添加 移除
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 【HTTP 2】启用 HTTP 2(Starting HTTP/2)
[HTTP 2]启用 HTTP 2(Starting HTTP/2) 四月 1, 2016 ~ LITECODES 前情提要 在上一篇文章<[HTTP 2]HTTP/2 协议概述(HTTP/2 ...
- Ruby学习-第二章
第二章 类继承,属性,类变量 1.如何声明一个子类 class Treasure < Thing 这样Thing类中的属性name,description都被Treasure继承 2.以下三种方 ...
- win7删除桌面文件需要刷新才会消失(2种解决方法)
有没有遇到过这种情况,删除桌面文件没有效果,要点右键的刷新删除过的文件才会在桌面上消失!解决方法有两种: 第一种方法 点击"开始→运行",在对话框中输入"regedit& ...
- ReviewBoard安装和配置札记
眼下部门还没有採用Pair Programming那种时时刻刻都在review代码的工作方式,代码Review多採用走查方式,即代码写完后召开一个Code Review的Meeting,集中时间和经验 ...
- BZOJ 3375: [Usaco2004 Mar]Paranoid Cows 发疯的奶牛( set )
果然写得短就跑得慢... 直接用set就行了(你要写棵平衡树也可以).没有包含的话, 假如L(i) <= L(j), 那么R[i] <= R[j]. 所以从小到大扫, 每次查找左端点小于当 ...
- 【转】android加载大量图片内存溢出的三种解决办法
方法一: 在从网络或本地加载图片的时候,只加载缩略图. /** * 按照路径加载图片 * @param path 图片资源的存放路径 * @param scalSize 缩小的倍数 * @return ...
- 教你在mac上配置adb环境变量
1.打开终端,一次输入如下命令 cd ~ touch .bash_profile open -e .bash_profile 2.这时候会在TextEdit中打开一个空白文档,输入下面的语句 a. 输 ...
- oracle 11gR2 在VM中安装步骤
oacle的安装 一.在oracle官网可以免费下载oracle的软件和安装文档,如果是在虚拟机中的linux系统里安装,可以用FileZilla Client把软件发送到系统中. linux_11g ...
- 微信jsSDK开发
(学习类)2015年最新微信公众平台开发 微信JSSDK开发分享功能 链接地址:http://blog.163.com/sdolove@126/blog/static/1146378852015132 ...