题目链接: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操作的元素不多,所以直接可以使用单点更新的方式来解决问题。

  1. #include <iostream>
  2. #include <cstring>
  3. #include <algorithm>
  4. #define maxn 100010
  5. #define maxl 50
  6. #define inf 1000000000
  7. #define LL(x) x<<1
  8. #define RR(x) x<<1|1
  9. using namespace std;
  10.  
  11. typedef long long LL;
  12.  
  13. //variable define
  14.  
  15. struct tree
  16. {
  17. int l, r;
  18. int mi;
  19. };
  20.  
  21. tree node[maxn<<];
  22. int n, m, arr[maxn], tmp[maxl], tot;
  23.  
  24. //function define
  25.  
  26. void push_up(int x);
  27.  
  28. void build_tree(int left, int right, int x);
  29.  
  30. int query(int left, int right, int x);
  31.  
  32. void update(int left, int right, int x, int val);
  33.  
  34. void solve();
  35.  
  36. bool is_number(char ch);
  37.  
  38. int main(void)
  39. {
  40. while (scanf("%d %d", &n, &m) != EOF)
  41. {
  42. tot = ;
  43. build_tree( , n, );
  44. solve();
  45. }
  46. return ;
  47. }
  48.  
  49. void build_tree(int left, int right, int x)
  50. {
  51. node[x].l = left;
  52. node[x].r = right;
  53.  
  54. if (left == right)
  55. {
  56. scanf("%d", &node[x].mi);
  57. arr[tot++] = node[x].mi;
  58. return;
  59. }
  60.  
  61. int lx = LL(x);
  62. int rx = RR(x);
  63. int mid = left + (right - left)/;
  64. build_tree(left, mid, lx);
  65. build_tree(mid + , right, rx);
  66. push_up(x);
  67. }
  68.  
  69. void push_up(int x)
  70. {
  71. if (node[x].l >= node[x].r)
  72. return;
  73.  
  74. int lx = LL(x);
  75. int rx = RR(x);
  76. node[x].mi = min( node[lx].mi, node[rx].mi);
  77. }
  78.  
  79. void update(int left, int right, int x, int val)
  80. {
  81. if (node[x].l == left && node[x].r == right)
  82. {
  83. node[x].mi = val;
  84. return;
  85. }
  86. int lx = LL(x);
  87. int rx = RR(x);
  88. int mid = node[x].l + (node[x].r - node[x].l)/;
  89. if (right <= mid)
  90. update(left, right, lx, val);
  91. else if (left > mid)
  92. update(left, right, rx, val);
  93. else
  94. {
  95. update(left, mid, lx, val);
  96. update(mid + , right, rx, val);
  97. }
  98. push_up( x);
  99. }
  100.  
  101. int query(int left, int right, int x)
  102. {
  103. if (node[x].l == left && node[x].r == right)
  104. {
  105. return node[x].mi;
  106. }
  107. int mid = node[x].l + (node[x].r - node[x].l)/;
  108. int lx = LL(x);
  109. int rx = RR(x);
  110. if (right <= mid)
  111. return query(left, right, lx);
  112. else if (left > mid)
  113. return query(left, right, rx);
  114. else
  115. return min( query(left, mid, lx), query(mid + , right, rx));
  116. }
  117.  
  118. void solve()
  119. {
  120. char str[];
  121. while (m--)
  122. {
  123. int x = , y = , ind;
  124. scanf("%s", str);
  125. if (str[] == 'q')
  126. {
  127. ind = ;
  128. while (!is_number(str[ind]))
  129. ind++;
  130. x = ;
  131. while (is_number(str[ind]))
  132. {
  133. x *= ;
  134. x += str[ind] - '';
  135. ind++;
  136. }
  137. while (!is_number(str[ind]))
  138. ind++;
  139. y = ;
  140. while (is_number(str[ind]))
  141. {
  142. y *= ;
  143. y += str[ind] - '';
  144. ind++;
  145. }
  146. printf("%d\n", query( x, y, ));
  147. }
  148. else
  149. {
  150. ind = , tot = ;
  151. while (str[ind] != ')')
  152. {
  153. if (is_number(str[ind]))
  154. {
  155. x = ;
  156. while (is_number(str[ind]))
  157. {
  158. x *= ;
  159. x += str[ind] - '';
  160. ind++;
  161. }
  162. tmp[tot++] = x;
  163. }
  164. else
  165. ind++;
  166. }
  167.  
  168. int swap = arr[tmp[]];
  169. for (int i = ; i < tot - ; ++i)
  170. arr[tmp[i]] = arr[tmp[i+]];
  171. arr[tmp[tot - ]] = swap;
  172. for (int i = ; i < tot; ++i)
  173. {
  174. update( tmp[i], tmp[i], , arr[tmp[i]]);
  175. }
  176. }
  177. }
  178. }
  179.  
  180. bool is_number(char ch)
  181. {
  182. if (ch >= '' && ch <= '')
  183. return true;
  184. return false;
  185. }

UVA 12299 RMQ with Shifts(线段树:单点更新)的更多相关文章

  1. UVa 12299 RMQ with Shifts(线段树)

    线段树,没了.. ----------------------------------------------------------------------------------------- # ...

  2. TOJ 4325 RMQ with Shifts / 线段树单点更新

    RMQ with Shifts 时间限制(普通/Java):1000MS/3000MS     运行内存限制:65536KByte 描述 In the traditional RMQ (Range M ...

  3. 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 ...

  4. UVa 12299 RMQ with Shifts(移位RMQ)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: "Times New ...

  5. HDU 1754 I Hate It 线段树单点更新求最大值

    题目链接 线段树入门题,线段树单点更新求最大值问题. #include <iostream> #include <cstdio> #include <cmath> ...

  6. HDU 1166 敌兵布阵(线段树单点更新)

    敌兵布阵 单点更新和区间更新还是有一些区别的,应该注意! [题目链接]敌兵布阵 [题目类型]线段树单点更新 &题意: 第一行一个整数T,表示有T组数据. 每组数据第一行一个正整数N(N< ...

  7. poj 2892---Tunnel Warfare(线段树单点更新、区间合并)

    题目链接 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensiv ...

  8. HDU 1166 敌兵布阵(线段树单点更新,板子题)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  9. POJ 1804 Brainman(5种解法,好题,【暴力】,【归并排序】,【线段树单点更新】,【树状数组】,【平衡树】)

    Brainman Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 10575   Accepted: 5489 Descrip ...

  10. HDU 1166 敌兵布阵(线段树单点更新,区间查询)

    描述 C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况 ...

随机推荐

  1. OpenLDAP与Apache

    1:安装部署Apache 2:在/var/www/html下,添加一个测试文件       echo test > index.html          3:在这之前,需了解Apache的账号 ...

  2. TimeQuest学习

    1.物理时钟特性:clock skew(时钟差),jitter(拉动),clock latency(时钟潜伏),这些物理时钟特性又称为uncertainl--非定性,或非理想性. clock skew ...

  3. ASP.NET5 Beta8可用性

    ASP.NET5 beta8现已上都的NuGet作为一个工具升级到Visual Studio2015!此版本极大地扩展.NET核心对OS X和Linux所支持的范围.您现在可以使用网络,加密和全球化特 ...

  4. Validate US Telephone Numbers

    function telephoneCheck(str) { // Good luck! //return true; var phone = /^1? ?(\d{3}|\(\d{3}\))[ -]? ...

  5. es6新特性(一)

    关于es6,阮一峰的<ECMAScript 6入门> http://es6.ruanyifeng.com/写的非常详尽,可以经常看看,这里是对这本书进行一个缩略,可能有误,欢迎大家纠正.

  6. linux中的chage命令

    在LINUX系统上,密码时效是通过chage命令来管理的. 参数说明:-m 过多少天后可修改密码.为0时代表任何时候都可以更改密码.-M 过多少天后密码过期.-W 用户密码到期前,提前收到警告信息的天 ...

  7. SVN 集中式版本控制软件

    简介: 目前流行的版本控制软件中,SVN ( 集中式版本控制 ) 算是使用范围更广.且使用时间更早的一款了,现在 git ( 分布式版本控制 ) 更火爆一点. 一.安装svn [root@localh ...

  8. 用POI读取具有任意合并单元的excel数据

    maven依赖: <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</ ...

  9. Python Windows环境下安装Python集成开发环境 学习之路(一)

    一.安装下载安装 Python https://www.python.org/ 全部下一步,直接Finish  PyCharm http://www.jetbrains.com/pycharm/ 全部 ...

  10. GPU高性能计算-CUDA

    前段时间有个同学的毕设是搞并行计算的,他基本不懂编程把我拉过去帮忙,我之前也没弄过,帮着搞了2天.先把代码贴上去,等有时间在把详细补充一些内容. CUDA编程主要是利用了显卡优越的并行计算能力,把一个 ...