-
Notifications
You must be signed in to change notification settings - Fork 0
/
loj1082.cpp
86 lines (69 loc) · 1.73 KB
/
loj1082.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using namespace std;
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cmath>
#define sz 100000+10
typedef long long int ll_d;
int arr[sz],M[270000],n;
/*void init_segmentTree(int N)
{
int i,l = int(2.0*pow(2.0,floor(log((double)N)/log(2.0)+1)));
for(i=0;i<=l;i++)
M[i]=0;
}
*/
void built_segment_tree(int node,int u,int v)
{
if(u==v)
M[node]=u;
else
{
built_segment_tree(2*node,u,(u+v)/2);
built_segment_tree(2*node+1,((u+v)/2)+1,v);
if(arr[M[2*node]]<=arr[M[2*node+1]])
M[node]=M[2*node];
else
M[node]=M[2*node+1];
}
}
int Query(int node,int b,int e,int u,int v)
{
if(u>e||b>v)
return -1;
if(b>=u&&e<=v)
return M[node];
int p1=Query(node*2,b,(b+e)/2,u,v);
int p2=Query(node*2+1,(b+e)/2+1,e,u,v);
if(p1==-1)
return p2;
if(p2==-1)
return p1;
if(arr[p1]<=arr[p2])
return p1;
return p2;
}
int main()
{
int u,v,t,i,Case=1,q;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&q);
// init_segmentTree(n);
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
built_segment_tree(1,0,n-1);
printf("Case %d:\n",Case++);
while(q--)
{
scanf("%d%d",&u,&v);
printf("%d\n",arr[Query(1,0,n-1,u-1,v-1)]);
}
}
return 0;
}