代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
const int Max=200010;
int RMQ[Max+10];
int total[Max];
int sum[35];
int N,M,cnt;
char ctr[35]; int bit(int x)
{//每一个下标管辖的范围
return x&(-x);
}
int query(int first,int second)
{//查询
int res=total[second];
while(first<=second)
{
int key=bit(second);
if(second-key>=first)
{
if(res>RMQ[second])
res=RMQ[second];
second=second-key;
}
else
{
if(res>total[second])
res=total[second];
second--;
}
}
return res;
}
int updata(int x)
{//更新x位置节点
for(int i=x;i<=N;i+=bit(i))
{
RMQ[i]=total[i];//利用原数组来更新树状数组
for(int j=1;j<bit(i);j<<=1)
{//这个是重点又一次扫描i节点所管辖的区间
RMQ[i]=min(RMQ[i],RMQ[i-j]);
}
}
}
void solve()
{
int len=strlen(ctr);
cnt=0;
memset(sum,0,sizeof(sum));
for(int i=6; i<len; i++)
{
if(ctr[i]==')')
break;
if(ctr[i]==',')
{
cnt++;
continue;
}
int t=ctr[i]-'0';
sum[cnt]=t+sum[cnt]*10;
}
if(ctr[0]=='q')
printf("%d\n",query(sum[0],sum[1]));
else
{
int key=total[sum[0]];
for(int i=cnt; i>=0; i--)
{
int q=total[sum[i]];
total[sum[i]]=key;//先更新原数组
updata(sum[i]);
key=q;
}
}
} int main()
{
scanf("%d%d",&N,&M);
for(int i=1;i<=N;i++)
{
scanf("%d",&total[i]);
updata(i);
}
for(int i=0;i<M;i++)
{
scanf("%s",ctr);
solve();
}
return 0;
}

树状数组求最大值 (RMQ with Shifts)的更多相关文章

  1. HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number                         ...

  2. POJ2985 The k-th Largest Group[树状数组求第k大值+并查集||treap+并查集]

    The k-th Largest Group Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 8807   Accepted ...

  3. UVA11525 Permutation[康托展开 树状数组求第k小值]

    UVA - 11525 Permutation 题意:输出1~n的所有排列,字典序大小第∑k1Si∗(K−i)!个 学了好多知识 1.康托展开 X=a[n]*(n-1)!+a[n-1]*(n-2)!+ ...

  4. 树状数组求第k小的元素

    int find_kth(int k) { int ans = 0,cnt = 0; for (int i = 20;i >= 0;i--) //这里的20适当的取值,与MAX_VAL有关,一般 ...

  5. POJ2299Ultra-QuickSort(归并排序 + 树状数组求逆序对)

    树状数组求逆序对   转载http://www.cnblogs.com/shenshuyang/archive/2012/07/14/2591859.html 转载: 树状数组,具体的说是 离散化+树 ...

  6. hdu 4217 Data Structure? 树状数组求第K小

    Data Structure? Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  7. poj 2985 The k-th Largest Group 树状数组求第K大

    The k-th Largest Group Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 8353   Accepted ...

  8. HDU 1394 Minimum Inversion Number (树状数组求逆序对)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题目让你求一个数组,这个数组可以不断把最前面的元素移到最后,让你求其中某个数组中的逆序对最小是多 ...

  9. poj 2299 Ultra-QuickSort(树状数组求逆序数+离散化)

    题目链接:http://poj.org/problem?id=2299 Description In this problem, you have to analyze a particular so ...

随机推荐

  1. PostgreSQL 全文索引

    -- 首先要创建自定义的词典,在不使用停用词文件的情况下创建自定义词典,例如: CREATE TEXT SEARCH DICTIONARY english_stem_nostop ( Template ...

  2. 【SDOJ 3741】 【poj2528】 Mayor's posters

    Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...

  3. 九度oj 题目1374:所有员工年龄排序

    题目描述: 公司现在要对所有员工的年龄进行排序,因为公司员工的人数非常多,所以要求排序算法的效率要非常高,你能写出这样的程序吗? 输入: 输入可能包含多个测试样例,对于每个测试案例, 输入的第一行为一 ...

  4. Linux Shell系列教程

    学习Linux Shell知识,就来Linux大学网(Linuxdaxue.com)! 本系列适合Linux初学者,属于Linux入门级教程,主要介绍了Shell的分类.语法格式以及脚本的使用和编写格 ...

  5. Welcome-to-Swift-10属性 (Properties)

    属性将值跟特定的类.结构或枚举关联.存储属性存储常量或变量作为实例的一部分,计算属性计算(而不是存储)一个值.计算属性可以用于类.结构体和枚举里,存储属性只能用于类和结构体. 存储属性和计算属性通常用 ...

  6. Centos7系统rc.local不起作用问题

    Centos7系统rc.local不起作用问题 来源 https://www.cnblogs.com/xjz00/p/7729405.html Centos7已经写了要chmod +x /etc/rc ...

  7. 基于RESTful 的几种实现(就随便了解一下)

    百度来的,原文未标出处,侵删. 1.1. RailsRuby on Rails是新兴的敏捷Web开发框架,在动态语言Ruby的支持下,Rails以新鲜的视角告诉我们Web开发是简单而快乐的.Rails ...

  8. 归并排序,时间复杂度nlogn

    思路: /*   考点:   1. 快慢指针:2. 归并排序.   此题经典,需要消化吸收.   复杂度分析:              T(n)            拆分 n/2, 归并 n/2 ...

  9. Bootstrap 模态框(Modal)插件数据传值

    原文:http://blog.csdn.net/baalhuo/article/details/51178154 模态框具体代码如下: <!-- 模态框(Modal) --> <di ...

  10. 最新版浏览器报错net::ERR_INSECURE_RESPONSE原因

    访问的网址与接口请求的域名不一致,新版的chrome浏览器出于安全的考虑会将请求进行拦截,并报错net::ERR_INSECURE_RESPONSE