代码:

#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. 面向对象编程(二)封装--构造方法,this关键字,static关键字,方法重载

    面向对象三大特点:封装.继承.多态 封装概念 ①   将东西包装在一起,然后以新的完整形式呈现出来: 将方法和字段一起包装到一个单元中,单元以类的形式实现; ②   信息隐藏,隐藏对象的实现细节,不让 ...

  2. 九度oj 题目1455:珍惜现在,感恩生活

    题目描述: 为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金n元,而市场有m种大米,每种大米都是袋装产品,其价格不等,并且只能整袋购买.请问:你用有限的资金最 ...

  3. 【转】参照protobuf,将json数据转换成二进制在网络中传输。

    http://blog.csdn.net/gamesofsailing/article/details/38335753?utm_source=tuicool&utm_medium=refer ...

  4. C遇到的编译错误整理

    1: Permission denied collect2.exe: error: ld returned exit status c:/mingw/bin/../lib/gcc/mingw32/6. ...

  5. phpstorm 快速插入常用代码片段

  6. Welcome-to-Swift-23访问控制(Access Control)

    访问控制可以限定你在源文件或模块中访问代码的级别,也就是说可以控制哪些代码你可以访问,哪些代码你不能访问.这个特性可以让我们隐藏功能实现的一些细节,并且可以明确的指定我们提供给其他人的接口中哪些部分是 ...

  7. [luoguP2770] 航空路线问题(最小费用最大流)

    传送门 模型 求最长两条不相交路径,用最大费用最大流解决. 实现 为了限制经过次数,将每个点i拆成xi,yi. 1.从xi向yi连一条容量为1,费用为1的有向边(1<i<N), 2.从x1 ...

  8. d3 画简单的柱形图

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  9. 【CF1020C】Elections(贪心)

    题意: Berland地区的腐败现象非常常见. 马上有一场选举,你事先知道了选民和政党的数量,分别为 n 和 m ,对于每一位选民,你知道他将要选举哪一个政党, 不过,每一位选民都会在接受一定数额的金 ...

  10. 【NOIP2016练习】T3 tree (树形DP)

    题意:一棵有N个结点的树,每个节点上有权值c[i] 需要选出若干结点,对于任意结点他的所有祖先都被选取且选取总个数不能超过lim 在此前提下使权值和最大 n,lim<=3000 思路:WA了1次 ...