树状数组求最大值 (RMQ with Shifts)
代码:
#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)的更多相关文章
- HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number ...
- POJ2985 The k-th Largest Group[树状数组求第k大值+并查集||treap+并查集]
The k-th Largest Group Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 8807 Accepted ...
- UVA11525 Permutation[康托展开 树状数组求第k小值]
UVA - 11525 Permutation 题意:输出1~n的所有排列,字典序大小第∑k1Si∗(K−i)!个 学了好多知识 1.康托展开 X=a[n]*(n-1)!+a[n-1]*(n-2)!+ ...
- 树状数组求第k小的元素
int find_kth(int k) { int ans = 0,cnt = 0; for (int i = 20;i >= 0;i--) //这里的20适当的取值,与MAX_VAL有关,一般 ...
- POJ2299Ultra-QuickSort(归并排序 + 树状数组求逆序对)
树状数组求逆序对 转载http://www.cnblogs.com/shenshuyang/archive/2012/07/14/2591859.html 转载: 树状数组,具体的说是 离散化+树 ...
- hdu 4217 Data Structure? 树状数组求第K小
Data Structure? Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- poj 2985 The k-th Largest Group 树状数组求第K大
The k-th Largest Group Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 8353 Accepted ...
- HDU 1394 Minimum Inversion Number (树状数组求逆序对)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题目让你求一个数组,这个数组可以不断把最前面的元素移到最后,让你求其中某个数组中的逆序对最小是多 ...
- poj 2299 Ultra-QuickSort(树状数组求逆序数+离散化)
题目链接:http://poj.org/problem?id=2299 Description In this problem, you have to analyze a particular so ...
随机推荐
- Educational Codeforces Round 36 (Rated for Div. 2)
A. Garden time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...
- iOS----OC特性-特殊功能宏
1.NS_ASSUME_NONNULL_BEGIN && NS_ASSUME_NONNULL_END 在Swift中存在Option类型,也就是使用?和!声明的变量.但是OC里面没有这 ...
- iOS学习笔记47-Swift(七)泛型
一.Swift泛型介绍 泛型是为Swift编程灵活性的一种语法,在函数.枚举.结构体.类中都得到充分的应用,它的引入可以起到占位符的作用,当类型暂时不确定的,只有等到调用函数时才能确定具体类型的时候可 ...
- docker 容器详解
Docker 是一个开源的应用容器引擎,基于Go语言 并遵Apache2.0协议开源,也是一种虚拟化技术.让开发者打包他们的应用以及依赖包到一个轻量级.可移植的容器中,然后发布到任何流行的 Linux ...
- 刷题总结——run(ssoj)
题目: 题目描述 企鹅国正在举办全面运动会,第一项比赛就是跑步.N 个人在圆形跑道上跑步,他们都有各自的速度和起点.但这个跑步规则很奇怪,当两个人相遇的时候编号较小的就会出局,当场上剩下最后一个人的时 ...
- linux--bash: redis-server: 未找到命令
linux 安装redis过程中出现了异常,make不通过,异常如下: [root@localhost redis-2.8.3]# make cd src && make all ma ...
- 富文本编辑器quill---vue组件(vue-quill-editor)的使用
1.配置webpack plugin 解决以下报错 Uncaught TypeError: Cannot read property 'imports' of undefined (image-res ...
- 2018.7.31 Noip2018模拟测试赛(十六)
日期: 七月最后一天 总分: 300分 难度: 提高 ~ 省选 得分: 30分(少的可怜) 我太弱了:(题目目录) T1:Mushroom追妹纸 T2:抵制克苏恩 T3:美味 失分分析:(QA ...
- Bzoj1452 Count
http://www.lydsy.com/JudgeOnline/problem.php?id=1452 题目全是图片,不复制了. 开100个二维树状数组,分别记录区间内各个颜色的出现位置…… 简单粗 ...
- 【POJ3321】Apple Tree(DFS序,树状数组)
题意:给一棵n个节点的树,每个节点开始有一个苹果,m次操作 1.将某个结点的苹果数异或 1 2.查询一棵子树内的苹果数 n,m<=100000 思路:最近一段时间在思考树上统计问题的算法 发 ...