改动的时候因为数据非常小,所以能够直接暴力改动,查询的时候利用线段树即可了。

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

  1. #include<cstdio>
  2. #include<vector>
  3. #include<cstring>
  4. #include<algorithm>
  5. using namespace std;
  6. #define lson pos<<1
  7. #define rson pos<<1|1
  8. const int maxn = 111111;
  9. const int INF = 111111;
  10. int arr[maxn];
  11. int sz;
  12. struct Node{
  13. int l,r;
  14. int _min;
  15. }node[maxn <<2];
  16. void BuildTree(int L,int R,int pos){
  17. node[pos].l = L; node[pos].r = R;
  18. if(L == R){
  19. scanf("%d",&node[pos]._min);
  20. arr[sz ++] = node[pos]._min;
  21. return ;
  22. }
  23. int m = (L + R) >> 1;
  24. BuildTree(L,m,lson);
  25. BuildTree(m + 1,R,rson);
  26. node[pos]._min = min(node[lson]._min,node[rson]._min);
  27. return;
  28. }
  29. void GetNum(int &l,int &r,char *str){
  30. int L = strlen(str),j;
  31. for(j = 0; j < L; j++){
  32. if(str[j] >= '0' && str[j] <= '9'){
  33. l = l * 10 + (str[j] - '0');
  34. }
  35. else if(l)
  36. break;
  37. }
  38. for(;j < L; j++){
  39. if(str[j] >= '0' && str[j] <= '9'){
  40. r = r * 10 + str[j] - '0';
  41. }
  42. else if(r)
  43. break;
  44. }
  45. return ;
  46. }
  47. int Query(int L,int R,int pos){
  48. if(L <= node[pos].l && node[pos].r <= R)
  49. return node[pos]._min;
  50. int m = (node[pos].l + node[pos].r) >> 1;
  51. int ret = INF;
  52. if(L <= m)
  53. ret = min(ret,Query(L,R,lson));
  54. if(R > m)
  55. ret = min(ret,Query(L,R,rson));
  56. return ret;
  57. }
  58. void UpDate(int aim,int value,int pos){
  59. if(node[pos].l == node[pos].r){
  60. node[pos]._min = value;
  61. return ;
  62. }
  63. int m = (node[pos].l + node[pos].r) >> 1;
  64. if(aim <= m)
  65. UpDate(aim,value,lson);
  66. else
  67. UpDate(aim,value,rson);
  68. node[pos]._min = min(node[lson]._min,node[rson]._min);
  69. return;
  70. }
  71. int main(){
  72. int n,q;
  73. while(scanf("%d%d",&n,&q) != EOF){
  74. sz = 1;
  75. BuildTree(1,n,1);
  76. char str[100];
  77. int array[30];
  78. for(int x = 0; x < q; x++){
  79. scanf("%s",str);
  80. if(str[0] == 'q'){
  81. int l = 0,r = 0;
  82. GetNum(l,r,str);
  83. printf("%d\n",Query(l,r,1));
  84. }
  85. else{
  86. memset(array,0,sizeof(array));
  87. int size = 0, n = strlen(str);
  88. for(int j = 0 ; j < n; j++){
  89. if(str[j] >= '0' && str[j] <= '9'){
  90. array[size] = array[size] * 10 + str[j] - '0';
  91. }
  92. else if(array[size] != 0){
  93. size ++;
  94. }
  95. }
  96. //左移一位
  97. int temp = arr[array[0]];
  98. for(int i = 0; i < size; i++){
  99. int e;
  100. if(i < size - 1){
  101. UpDate(array[i],arr[array[i + 1]],1);
  102. arr[array[i]] = arr[array[i + 1]];
  103. }
  104. else{
  105. UpDate(array[i],temp,1);
  106. arr[array[i]] = temp;
  107. }
  108. }
  109. }
  110. // for(int i = 1 ;i <= n; i++) printf("%d ",arr[i]);
  111. // printf("\n");
  112. }
  113. }
  114. return 0;
  115. }

【UVA】12299-RMQ with Shifts(线段树)的更多相关文章

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

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

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

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

  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. RMQ with Shifts(线段树)

    RMQ with Shifts Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u Pra ...

  5. TZOJ 4325 RMQ with Shifts(线段树查询最小,暴力更新)

    描述 In the traditional RMQ (Range Minimum Query) problem, we have a static array A. Then for each que ...

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

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

  7. UVA 12299 RMQ with shifts

    就是线段树的单点修改和区间查询. 然而输入打了一个小时才弄清楚. #include<iostream> #include<cstdio> #include<cstring ...

  8. UVA 12299 RMQ with Shifts(线段树:单点更新)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  9. RMQ问题(线段树+ST算法)

    转载自:http://kmplayer.iteye.com/blog/575725 RMQ (Range Minimum/Maximum Query)问题是指:对于长度为n的数列A,回答若干询问RMQ ...

  10. UVA 11983 Weird Advertisement(线段树求矩形并的面积)

    UVA 11983 题目大意是说给你N个矩形,让你求被覆盖k次以上的点的总个数(x,y<1e9) 首先这个题有一个转化,吧每个矩形的x2,y2+1这样就转化为了求N个矩形被覆盖k次以上的区域的面 ...

随机推荐

  1. Linux 特殊符号使用: 倒引号`的使用

    Linux中有很多特殊符号,这里介绍 ` 倒引号的含义. 我们考虑下这个场景,有时我们需要将一个命令的执行结果赋值给某个变量,或者别的用途. 这时我们可以用两个`倒引号将该命令括起来. 例1: 如 e ...

  2. mysql的基础操作

    查看数据库 获取服务器上的数据库列表通常很有用.执行show databases;命令就可以搞定. mysql> show databases; 创建数据库 mysql> create d ...

  3. KeyValuePair用法(转)

    转载自:http://blog.sina.com.cn/s/blog_9741eba801016w61.html C# KeyValuePair<TKey,TValue>的用法.结构体,定 ...

  4. C++自增和自减运算符(--和++)

    在C和C++中,常在表达式中使用自增(++)和自减(--)运算符,他们的作用是使变量的值增1或减1,如:++i(在使用i之前,先使i的值加1,如果i的原值为3,则执行j=++i后,j的值为4)--i ...

  5. 用ATL写简单的ActiveX控件 .

    我正在做的项目需要用读卡器来读数据,由于系统是B/S架构的所以只能把读卡器的驱动封装成一个无界面的ActiveX控件,这样web页面中的js代码才能访问读卡器其实做起来也挺简单的,我用的环境是VS20 ...

  6. ETC_百度百科

    ETC_百度百科 ETC(电子不停车收费系统)

  7. 【手打】LZW编码的C/C++实现

    LZW编码通过建立一个字符串表,用较短的代码来表示较长的字符串来实现压缩. LZW压缩算法是Unisys的专利,有效期到2003年,所以相关算法大多也已过期. 本代码只完毕了LZW的编码与解码算法功能 ...

  8. PHP - 数学运算

    第4章 数学运算 学习要点: 1.数值数据类型 2.随机数 3.格式化数据 4.数学函数 在大多数程序设计语言中,数值运算都是最基本的元素之一.数值运算允许程序员完成加法到高级计算等各种操作.尽管PH ...

  9. Linux内核源代码解析之——sock's buffer参数

    本文原创为freas_1990,转载请标明出处:http://blog.csdn.net/freas_1990/article/details/11539695 关于socket与sock的关系再简单 ...

  10. 公钥password学中的素数以及对称加密

        password学.一向被人们觉得门槛非常高,特别高端...这也是实际,可是这决不意味着普通人无法了解它的精髓.对于喜欢画圆的人来讲,即便是理解了password技术背后的哪怕一点理论,也是激 ...