【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次以上的区域的面 ...
随机推荐
- 基于visual Studio2013解决C语言竞赛题之0515国名排序
题目
- 常用位操作,写8位 12864串口模式 I2C 1302 18B20
/*以1302为例*/ void DS1302InputByte(unsigned char d) //实时时钟写入一字节(内部函数) { unsigned char i; ACC ...
- Android内存之VSS/RSS/PSS/USS
Terms VSS - Virtual Set Size 虚拟耗用内存(包含共享库占用的内存) RSS - Resident Set Size 实际使用物理内存(包含共享库占用的内存) PSS - P ...
- Search a 2D Matrix【python】
class Solution: # @param matrix, a list of lists of integers # @param target, an integer # @return a ...
- 04-OC属性的使用、自动释放池、封装和继承
目录: 一.IOS6声明式属性的使用 二.autoreleasepool自动释放池 三.封装.继承 回到顶部 一.IOS6声明式属性的使用 注:声明式属性默认情况下,并没有解决内存问题, 当使用@pr ...
- MinGW开发工具的安装
MinGW是Minimalist GNU for Windows的缩写,是把linux下的GNU开发工具包移植到windows的项目之一.和Cygwin不一样的是,MinGW不提供linux的posi ...
- [置顶] Oracle 11g ASM:如何在 ASMCMD 命令行工具中创建 Oracle ACFS 文件系统
实验环境:Oracle 11g R2 RAC (11.2.0.3.5) Oracle Enterprise Linux 5.6 x86 1.创建 ASM 磁盘组 在两节点 ...
- 怎样实现多文件上传 在iOS开发中
NSURL* url = [NSURL URLWithString:@"xxx"]; ASIFormDataRequest* request = [ASIFormDataReque ...
- wireshark 抓包分析 TCPIP协议的握手
wireshark 抓包分析 TCPIP协议的握手 原网址:http://www.cnblogs.com/TankXiao/archive/2012/10/10/2711777.html 之前写过一篇 ...
- webview 加载某些网页失败的处理办法(第七条)
1.添加权限:AndroidManifest.xml中必须使用许可"android.permission.INTERNET",否则会出Web page not available错 ...