UVA 12299 RMQ with Shifts(线段树:单点更新)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3720
题意:给你一个可变的数组A,有两种操作。操作一:shift(i1, i2....in),将数组中这些元素的值变为(A[i2], A[i3]....A[in], A[i1]),操作二:Query(L, R),
查询A[i](L<=i <=R)的和。
题中 Each operation is formatted as a string having no more than 30 characters, 暗示每次参与shift操作的元素不多,所以直接可以使用单点更新的方式来解决问题。
#include <iostream>
#include <cstring>
#include <algorithm>
#define maxn 100010
#define maxl 50
#define inf 1000000000
#define LL(x) x<<1
#define RR(x) x<<1|1
using namespace std; typedef long long LL; //variable define struct tree
{
int l, r;
int mi;
}; tree node[maxn<<];
int n, m, arr[maxn], tmp[maxl], tot; //function define void push_up(int x); void build_tree(int left, int right, int x); int query(int left, int right, int x); void update(int left, int right, int x, int val); void solve(); bool is_number(char ch); int main(void)
{
while (scanf("%d %d", &n, &m) != EOF)
{
tot = ;
build_tree( , n, );
solve();
}
return ;
} void build_tree(int left, int right, int x)
{
node[x].l = left;
node[x].r = right; if (left == right)
{
scanf("%d", &node[x].mi);
arr[tot++] = node[x].mi;
return;
} int lx = LL(x);
int rx = RR(x);
int mid = left + (right - left)/;
build_tree(left, mid, lx);
build_tree(mid + , right, rx);
push_up(x);
} void push_up(int x)
{
if (node[x].l >= node[x].r)
return; int lx = LL(x);
int rx = RR(x);
node[x].mi = min( node[lx].mi, node[rx].mi);
} void update(int left, int right, int x, int val)
{
if (node[x].l == left && node[x].r == right)
{
node[x].mi = val;
return;
}
int lx = LL(x);
int rx = RR(x);
int mid = node[x].l + (node[x].r - node[x].l)/;
if (right <= mid)
update(left, right, lx, val);
else if (left > mid)
update(left, right, rx, val);
else
{
update(left, mid, lx, val);
update(mid + , right, rx, val);
}
push_up( x);
} int query(int left, int right, int x)
{
if (node[x].l == left && node[x].r == right)
{
return node[x].mi;
}
int mid = node[x].l + (node[x].r - node[x].l)/;
int lx = LL(x);
int rx = RR(x);
if (right <= mid)
return query(left, right, lx);
else if (left > mid)
return query(left, right, rx);
else
return min( query(left, mid, lx), query(mid + , right, rx));
} void solve()
{
char str[];
while (m--)
{
int x = , y = , ind;
scanf("%s", str);
if (str[] == 'q')
{
ind = ;
while (!is_number(str[ind]))
ind++;
x = ;
while (is_number(str[ind]))
{
x *= ;
x += str[ind] - '';
ind++;
}
while (!is_number(str[ind]))
ind++;
y = ;
while (is_number(str[ind]))
{
y *= ;
y += str[ind] - '';
ind++;
}
printf("%d\n", query( x, y, ));
}
else
{
ind = , tot = ;
while (str[ind] != ')')
{
if (is_number(str[ind]))
{
x = ;
while (is_number(str[ind]))
{
x *= ;
x += str[ind] - '';
ind++;
}
tmp[tot++] = x;
}
else
ind++;
} int swap = arr[tmp[]];
for (int i = ; i < tot - ; ++i)
arr[tmp[i]] = arr[tmp[i+]];
arr[tmp[tot - ]] = swap;
for (int i = ; i < tot; ++i)
{
update( tmp[i], tmp[i], , arr[tmp[i]]);
}
}
}
} bool is_number(char ch)
{
if (ch >= '' && ch <= '')
return true;
return false;
}
UVA 12299 RMQ with Shifts(线段树:单点更新)的更多相关文章
- UVa 12299 RMQ with Shifts(线段树)
线段树,没了.. ----------------------------------------------------------------------------------------- # ...
- TOJ 4325 RMQ with Shifts / 线段树单点更新
RMQ with Shifts 时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte 描述 In the traditional RMQ (Range M ...
- 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 ...
- 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 线段树单点更新求最大值
题目链接 线段树入门题,线段树单点更新求最大值问题. #include <iostream> #include <cstdio> #include <cmath> ...
- HDU 1166 敌兵布阵(线段树单点更新)
敌兵布阵 单点更新和区间更新还是有一些区别的,应该注意! [题目链接]敌兵布阵 [题目类型]线段树单点更新 &题意: 第一行一个整数T,表示有T组数据. 每组数据第一行一个正整数N(N< ...
- poj 2892---Tunnel Warfare(线段树单点更新、区间合并)
题目链接 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensiv ...
- HDU 1166 敌兵布阵(线段树单点更新,板子题)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- POJ 1804 Brainman(5种解法,好题,【暴力】,【归并排序】,【线段树单点更新】,【树状数组】,【平衡树】)
Brainman Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 10575 Accepted: 5489 Descrip ...
- HDU 1166 敌兵布阵(线段树单点更新,区间查询)
描述 C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况 ...
随机推荐
- javascript 中的继承实现, call,apply,prototype,构造函数
javascript中继承可以通过call.apply.protoperty实现 1.call call的含义: foo.call(thisObject, args...) 表示函数foo调用的时候, ...
- 安装android
http://www.oschina.net/question/1463998_220998 http://www.cnblogs.com/zoupeiyang/p/4034517.html
- SonarQube-5.6.3 代码分析平台搭建使用
python代码分析 官网主页: http://docs.sonarqube.org/display/PLUG/Python+Plugin Windows下安装使用: 快速使用: 1.下载jdk ht ...
- java写入和写出EXCEL(含源代码)
这两天帮老师做一个数据库,将所有实验交易的数据导入到数据库中,但是不想天天在实验室里面待着,气氛太压抑,就想着先把数据读进EXCEL中,哪天带到实验室导进去 数据原来是这样的,不同的实验有一个专门的文 ...
- jQuery 的属性
一.显示和隐藏的属性 hide(隐藏),show(显示) 下面是例子 <script type="text/javascript"> $(document).read ...
- HTML DOM 方法
一.HMTL DOM对象 --方法和属性 1.1常用的方法. 1.getElementByld( id )方法 --获取带有指定id 的节点( 元素 ) 2.appendChild( node )方法 ...
- js 遇到问题
1)obj.style.attr 和obj.style[attr]区别: 2)window.onload一个页面只能出现一次: 3)border-radious实现 实心和空心圆 要点:宽度高度一样大 ...
- Struts 2学习笔记——拦截器相关
一.添加国际化支持 默认的struts-deault.xml文件中已经定义了国际化拦截器,内容如下 <!-定义国际化拦截器--> <interceptor name="i1 ...
- MySQL 性能优化的最佳20多条经验分享
当我们去设计数据库表结构,对操作数据库时(尤其是查表时的SQL语句),我们都需要注意数据操作的性能.这里,我们不会讲过多的SQL语句的优化,而只是针对MySQL这一Web应用最多的数据库.希望下面的这 ...
- 微信小程序0.11.122100版本新功能解析
微信小程序0.11.122100版本新功能解析 新版本就不再吐槽了,整的自己跟个愤青似的.人老了,喷不动了,把机会留给年轻人吧.下午随着新版本开放,微信居然破天荒的开放了开发者论坛.我很是担心官方 ...