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
| #include <iostream>
#include <vector>
using namespace std;
struct Node
{
int nLeft, nRight;
unsigned long long nMoney;
Node *pLeft, *pRight;
};
Node *pRoot;
int N, M, nTmp, L, R, ans;
vector<int> pMoney;
Node* Build(int l, int r);
int Update(Node *pNode);
int Query(Node *pNode, int l, int r);
int main()
{
ios::sync_with_stdio(false);
cin >> N >> M;
for(int i = 1; i <= N; i++)
{
cin >> nTmp;
pMoney.push_back(nTmp);
}
pRoot = Build(1, N);
Update(pRoot);
for(int i = 1; i <= M; i++)
{
cin >> L >> R;
ans = 2147483647;
cout << Query(pRoot, L, R) << " ";
}
cout << endl;
return 0;
}
Node* Build(int l, int r)
{
Node *pNode = new Node();
if(l == r) { pNode->nMoney = pMoney[l - 1]; }
else { pNode->nMoney = 0; }
pNode->nLeft = l; pNode->nRight = r;
if(l == r) { return pNode; }
int nMid = (l + r) / 2;
pNode->pLeft = Build(l, nMid);
pNode->pRight = Build(nMid + 1, r);
return pNode;
}
int Update(Node *pNode)
{
if(pNode->nLeft == pNode->nRight)
{ return pNode->nMoney; }
else
{
return pNode->nMoney = min(Update(pNode->pLeft), Update(pNode->pRight));
}
}
int Query(Node *pNode, int l, int r)
{
if(pNode->nLeft == l && r == pNode->nRight) { return pNode->nMoney; }
else
{
if(r <= (pNode->nLeft + pNode->nRight) / 2)
{ return Query(pNode->pLeft, l, r); }
else if(l > (pNode->nLeft + pNode->nRight) / 2)
{ return Query(pNode->pRight, l, r); }
else
{
int nMid = (pNode->nLeft + pNode->nRight) / 2;
return min(Query(pNode->pLeft, l, nMid), Query(pNode->pRight, nMid + 1, r));
}
}
}
|