代码:

#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. 03-python进阶-爬虫入门-正则

    [urllib and urllib2] 这是两个python的网络模块 内置的 提供很好的网络访问的功能. #!coding:utf-8 import urllib2 res = urllib2.u ...

  2. Octave 里的 fminunc

    ptions = optimset('GradObj', 'on', 'MaxIter', '100'); initialTheta = zeros(2,1); [optTheta, function ...

  3. [svn学习篇]svn使用教程

    http://www.cnblogs.com/longshiyVip/p/4905901.html http://blog.csdn.net/dily3825002/article/details/6 ...

  4. TOJ2680: 最大矩阵连乘次数

    2680: 最大矩阵连乘次数  Time Limit(Common/Java):1000MS/10000MS     Memory Limit:65536KByteTotal Submit: 144 ...

  5. pip installl安装包特别慢? 指定源进行安装

    指定源地址安装: pip install -i http://pypi.douban.com/simple/ packagename pip install -i http://pypi.tuna.t ...

  6. [Err] 1022 - Can't write; duplicate key in table '#sql-1500_26'

        今天用powerdesigner修改了一些外键关系,有两个外键的名字取一样的,忘记改了.然后在用navicat运行sql文件时,报出[Err] 1022 - Can't write; dupl ...

  7. 设计模式(二 & 三)工厂模式:概述

    工厂 从 coding 的角度来说,在需要创建对象的时候,直接在方法内部使用 new 关键字来创建,是非常方便的. 然而从全局的角度考虑,这样会使对象变得难以管理和控制,代码会变得非常脆弱,缺乏弹性. ...

  8. 【Luogu】P2805植物大战僵尸(拓扑排序+最大流)

    题目链接 这题数据xie强qwq.拓扑用的那个图建反了得80. 一眼看出(个屁,题解上都说一眼看出,然而我还是太蒻了)这是个最大权闭合图.从被保护植物向保护植物连边,然后跑最大流,用正点权和减去. 哦 ...

  9. HDU——1027Ignatius and the Princess II(next_permutation函数)

    Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ( ...

  10. Server-Side Rendering(服务端渲染)的优点与缺点

    优点 1. SEO 客户端渲染,页面中只有初始的几个html容器,js生成内容填充到容器中,爬虫只能识别到初始的html容器,js生成的内容一般不会被识别,而服务端渲染直接给出html,爬虫可以识别到 ...