博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Lightoj1082——Array Queries(线段树+求区间最小值)
阅读量:2343 次
发布时间:2019-05-10

本文共 1566 字,大约阅读时间需要 5 分钟。

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 
#include
#include
#include
#include
#include
#include
#include
#include
#define INF 0x3f3f3f3f#define MAXN 25#define Mod 10001using namespace std;const int maxn=222222;int min(int a,int b){ if(a
>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;}

转载地址:http://mpcvb.baihongyu.com/

你可能感兴趣的文章
Java程序优化的一些最佳实践
查看>>
《速度与激情8》中的信息安全技术
查看>>
超级互联网公司如何评估IT运营水平?
查看>>
多线程中start()与run()方法的区别
查看>>
使用My97DatePicker插件(现在基本使用前端框架自带的时间控件)
查看>>
Failed to convert from type java.lang.String to type java.util.Date for value解决办法
查看>>
MySQL数据库中的Date,DateTime和TimeStamp类型
查看>>
关于海量数据问题的解决方案
查看>>
给定一个数组,求前k小或者前k大
查看>>
程序员,你为什么值这么多钱?
查看>>
招聘面试的套路与原则
查看>>
技术与技术人员的价值
查看>>
对于垃圾回收相关的建议
查看>>
JVM调优魔法棒-Java VisualVM
查看>>
JVM-栈帧
查看>>
JVM-运行时数据区(Run-time Data Areas)
查看>>
Java class文件信息
查看>>
JVM内存详解
查看>>
Java语言这些年的发展
查看>>
Java线程面试题(Top50)
查看>>