Lightoj1082——Array Queries(线段树+求区间最小值)
来源:互联网 发布:网络女主播现实不好看 编辑:程序博客网 时间:2024/06/04 19:38
Given an array with N elements, indexed from 1 to N. Now you will be given some queries in the form I J, your task is to find the minimum value from index I to J.
Input
Input starts with an integer T (≤ 5), denoting the number of test cases.
The first line of a case is a blank line. The next line contains two integers N (1 ≤ N ≤ 105), q (1 ≤ q ≤ 50000). The next line contains N space separated integers forming the array. There integers range in [0, 105].
The next q lines will contain a query which is in the form I J (1 ≤ I ≤ J ≤ N).
Output
For each test case, print the case number in a single line. Then for each query you have to print a line containing the minimum value between index I and J.
Sample Input
2
5 3
78 1 22 12 3
1 2
3 5
4 4
1 1
10
1 1
Output for Sample Input
Case 1:
1
3
12
Case 2:
10
很裸的线段树
#include <iostream>#include <cstring>#include <string>#include <vector>#include <queue>#include <cstdio>#include <set>#include <cmath>#include <algorithm>#define INF 0x3f3f3f3f#define MAXN 25#define Mod 10001using namespace std;const int maxn=222222;int min(int a,int b){ if(a<b) return a; else return b;}int MIN[maxn<<2];void PushUp(int rt){ MIN[rt]=min(MIN[rt<<1],MIN[rt<<1|1]);}void build(int l,int r,int rt){ if(r==l) { scanf("%d",&MIN[rt]); return; } int m=(l+r)>>1; build(l,m,rt<<1); build(m+1,r,rt<<1|1); PushUp(rt);}int query(int L,int R,int l,int r,int rt){ if(L<=l&&r<=R) return MIN[rt]; int m=(l+r)>>1; int ret=INF; if(L<=m) ret=min(ret,query(L,R,l,m,rt<<1)); if(R>m) ret=min(ret,query(L,R,m+1,r,rt<<1|1)); return ret;}int main(){ int t; scanf("%d",&t); for(int cas=1; cas<=t; ++cas) { int n,q,l,r; scanf("%d%d",&n,&q); build(1,n,1); printf("Case %d:\n",cas); while(q--) { scanf("%d%d",&l,&r); printf("%d\n",query(l,r,1,n,1)); } } return 0;}
- Lightoj1082——Array Queries(线段树+求区间最小值)
- lightOJ 1082 - Array Queries 【线段树(求最小值)】
- LightOJ Array Queries 1082【线段树求区间最值】
- NYOJ 119 士兵杀敌(三)(线段树—求区间最大值与最小值差)
- 【线段树】 求区间最小值以及区间最小值
- Light oj 1082 - Array Queries(区间最小值)
- 线段树求区间最大值和最小值(指针)
- poj2823------线段树-------求区间最大最小值(RMQ)
- poj3264(线段树求区间最大最小值)
- POJ 3264 Balanced Lineup(线段树—求区间最大值与最小值差)
- poj 2823 线段树 求固定区间的最大最小值
- 线段树求区间最大值与最小值的差
- 完整版线段树求最小值,支持区间减
- POJ3264(线段树求区间最大值和最小值)
- POJ 3264(线段树求区间内最大值和最小值)
- 线段树 理解,求解数组区间求和,求最小值
- 模板:线段树求区间最大/最小值及下标
- NYOJ 1185 最大最小值(线段树—区间求最值)
- centos6.5安装maridb5.5.51
- 地图与定位
- 欢迎使用CSDN-markdown编辑器
- Effective Modern C++ 条款36 如果异步执行是必需的,指定std::launch::async策略
- Mybatis中SqlSession下的四大核心组件分析
- Lightoj1082——Array Queries(线段树+求区间最小值)
- 10个最好的 Node.js MVC 框架
- 蓝牙 status 133
- windows 创建 别名 快速 调用命令
- hdu-5873-Football Games
- 算法设计中的会场安排问题
- Python格式化输出
- tableView section随cell移动 ,不在顶到屏幕顶部
- stm8s开发(三) UART的使用:串口通信!