树状数组求最大值 (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 ...
随机推荐
- grep搜索当前目录下的所有文件的内容
比如: grep print * -nR 搜索当前目录下所有文件的内容中含有print的行
- MongoDB 3.6 安装详解
在ubuntu和多数linux发行版的包安装源中MongoDB默认的版本是2.4,但2.4所使用的存储引擎不支持collecitons级别的锁,只支持database级别的,所以在开发中2.4版本的m ...
- 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 ...
- kb-07线段树-06离散化(与第四题类似)
/* zoj1610 这题是离散化,区间特殊查询的,和之前的第4 题是异曲同工的 */ #include<iostream> #include<cstdio> #include ...
- php的异常处理
https://my.oschina.net/sallency/blog/837615
- bzoj 4311 向量 时间线建线段树+凸包+三分
题目大意 你要维护一个向量集合,支持以下操作: 1.插入一个向量(x,y) 2.删除插入的第i个向量 3.查询当前集合与(x,y)点积的最大值是多少.如果当前是空集输出0 分析 按时间线建线段树 大致 ...
- net6:用户添加到角色和移出角色,角色的创建与删除等Roles与Membership的使用
原文发布时间为:2008-07-30 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...
- github每次push提交都要输入账号密码
问题产生的原因是在克隆的时候使用的是https的方式或者用一些特殊的指令来克隆的github项目源,如 golang里的go get github.com/...... 没次提交push的时候都会提示 ...
- 使用sqlparse分析SQL语句,及自己写的SQL分析语句
备忘, 以后写的时候可以参考. #!/usr/bin/env python # -*- coding: utf-8 -*- import sqlparse import re sql = " ...
- js-className修改class属性
1.修改className 1)修改class类名为p-a-0 2)在保留class="p1"的基础上再添加一个类名为p-a-0 2.删除className 1).结果需删除cla ...