COJ 1003 WZJ的数据结构(三)ST表
| WZJ的数据结构(三) |
| 难度级别:B; 运行时间限制:3000ms; 运行空间限制:51200KB; 代码长度限制:2000000B |
|
试题描述
|
|
请你设计一个数据结构,完成以下功能: 给定一个大小为N的整数组A,M次询问。每次询问给你i,j两个参数,求Ai至Aj中最大的数。 |
|
输入
|
|
第一行为两个正整数N,M。
第二行为N个整数Ai。 接下来M行为询问。 |
|
输出
|
|
对于每个询问输出答案。
|
|
输入示例
|
|
6 5
1 -2 3 4 -6 7 1 2 1 1 1 5 1 6 4 6 |
|
输出示例
|
|
1
1 4 7 7 |
|
其他说明
|
|
1<=N<=10000
1<=M<=1000000 -10^9<=Ai<=10^9 1<=L<=R<=N |
ST表是处理RMQ问题中询问较多的利器。注意初始化的log[0] = -1,同时记得init(现在我都不敢写成函数了要不肯定忘。。。)
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstring>
#define PAU putchar(' ')
#define ENT putchar('\n')
using namespace std;
const int maxn=+,inf=-1u>>;
int d[maxn][],Log[maxn],n,Q;
inline int read(){
int x=,sig=;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')sig=-;ch=getchar();}
while(isdigit(ch))x=*x+ch-'',ch=getchar();
return x*=sig;
}
inline void write(int x){
if(x==){putchar('');return;}if(x<)putchar('-'),x=-x;
int len=,buf[];while(x)buf[len++]=x%,x/=;
for(int i=len-;i>=;i--)putchar(buf[i]+'');return;
}
int query(int x,int y){
int k=Log[y-x+];
return max(d[x][k],d[y-(<<k)+][k]);
}
void init(){
n=read();Q=read();Log[]=-;
for(int i=;i<=n;i++) d[i][]=read(),Log[i]=Log[i>>]+;
for(int j=;(<<j)<=n;j++)
for(int i=;i+(<<j)-<=n;i++)
d[i][j]=max(d[i][j-],d[i+(<<j-)][j-]);
return;
}
void work(){
int x,y;
while(Q--){
x=read();y=read();
write(query(x,y));ENT;
}
return;
}
void print(){
return;
}
int main(){init();work();print();return ;}
COJ 1003 WZJ的数据结构(三)ST表的更多相关文章
- COJ 0967 WZJ的数据结构(负三十三)
WZJ的数据结构(负三十三) 难度级别:E: 运行时间限制:7000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 请你设计一个数据结构,完成以下功能: 给定一个大 ...
- COJ 0970 WZJ的数据结构(负三十)树分治
WZJ的数据结构(负三十) 难度级别:D: 运行时间限制:1000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 给你一棵N个点的无根树,点和边上均有权值.请你设计 ...
- COJ 0995 WZJ的数据结构(负五)区间操作
WZJ的数据结构(负五) 难度级别:C: 运行时间限制:1000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 请你设计一个数据结构,完成以下功能: 给定一个大小为 ...
- COJ 1008 WZJ的数据结构(八) 树上操作
传送门:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=986 WZJ的数据结构(八) 难度级别:E: 运行时间限制:3000ms: ...
- COJ 1010 WZJ的数据结构(十) 线段树区间操作
传送门:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=1001 WZJ的数据结构(十) 难度级别:D: 运行时间限制:3000ms: ...
- COJ 0979 WZJ的数据结构(负二十一)
WZJ的数据结构(负二十一) 难度级别:C: 运行时间限制:5000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 请你实现一个数据结构,完成这样的功能: 给你一个 ...
- COJ 0990 WZJ的数据结构(负十)
WZJ的数据结构(负十) 难度级别:D: 运行时间限制:5000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 给你一个N个节点的有根树,从1到N编号,根节点为1并给 ...
- COJ 1007 WZJ的数据结构(七) 树上操作
传送门:http://oj.cnuschool.org.cn/oj/home/problem.htm?problemID=983 WZJ的数据结构(七) 难度级别:C: 运行时间限制:1000ms: ...
- COJ 0999 WZJ的数据结构(负一)
WZJ的数据结构(负一) 难度级别:D: 运行时间限制:1000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 输入N个模板串Pi和文本串T,输出每个模板串Pi在T ...
随机推荐
- OpenGL蓝宝书第五章代码勘误以及惯性坐标系去解释模型变换:Pyramid.cpp
假设你也发现依照教程代码完毕贴图时,你会底面的坐标和寻常顶点坐标正负相反,比方-1.0f, -1.0f, -1.0f这个顶点相应的却是世界坐标中1.0f,-1.0f,1.0f 问题到底出如今哪里? 原 ...
- linux常用命令 http://mirrors.163.com/ubuntu-releases/12.04/
由于记忆力有限,把平时常用的Linux命令整理出来,以便随时查阅: linux 基本命令 ls (list 显示当前目录下文件和目录 ls -l 详细显示 =ll ) [root@linux ...
- [PWA] 5. Hijacking one type of request
Previously we saw how to Hijacking all the reqest, but this is not useful. So now we want to see how ...
- htaccess URL重写rewrite与重定向redirect(转)
1. 将 .htm 页面映射到 .php 1 Options +FollowSymlinks 2 RewriteEngine on 3 RewriteRule ^(.*)\.htm$ $1.php [ ...
- python-Pickle序列化
[Python之旅]第三篇(二):Pickle序列化 python 序列化 pickle 摘要: 说明:关于Pickle的说明 作如下说明: 1 2 3 4 5 6 7 序列化的概念很简单 ...
- IOS 开发 【objective-c 基础1】
案例:读取本地硬盘上程序根目录下words.txt文件内容,显示每行的字符数. // // main.m // hello // // Created by swack on 15/11/27. // ...
- codevs4203山区建小学
/* 状态:f[i][j] 前i个村庄已经建了j个学校 转移:f[i][j]=min(f[i][j],f[ii][j-1]+s[ii+1][i]) 1<=ii<=i-1 */ #inclu ...
- C#-高血压生活习惯数据模拟
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace test ...
- 基于bootstrap的轮播广告页,带图片和文字
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="utf-8& ...
- (转)php中__autoload()方法详解
转之--http://www.php100.com/html/php/lei/2013/0905/5267.html PHP在魔术函数__autoload()方法出现以前,如果你要在一个程序文件中实例 ...