描述 Description | |
| 老管家是一个聪明能干的人。他为财主工作了整整10年,财主为了让自已账目更加清楚。要求管家每天记k次账,由于管家聪明能干,因而管家总是让财主十分满意。但是由于一些人的挑拨,财主还是对管家产生了怀疑。于是他决定用一种特别的方法来判断管家的忠诚,他把每次的账目按1,2,3…编号,然后不定时的问管家问题,问题是这样的:在a到b号账中最少的一笔是多少?为了让管家没时间作假他总是一次问多个问题。 |
| | |
| | |
| 输入格式 Input Format | |
| 输入中第一行有两个数m,n表示有m(m<=100000)笔账,n表示有n个问题,n<=100000。 第二行为m个数,分别是账目的钱数 后面n行分别是n个问题,每行有2个数字说明开始结束的账目编号。 |
| | |
| | |
| 输出格式 Output Format | |
| 输出文件中为每个问题的答案。具体查看样例。 |
| | |
| | |
| 时间限制 Time Limitation | |
| 各个测试点1s |
|
一道線段樹的入門題。
每個點的Min值即為左子樹的Min值和右子樹的Min值中的較小值。
Accode:
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <bitset>
using std::min;
const char fi[] = "tyvj1038.in";
const char fo[] = "tyvj1038.out";
const int maxN = 100010;
const int maxM = 100010;
const int MAX = 0x3fffff00;
const int MIN = -MAX;
struct SegTree {int L, R, lc, rc, Min; };
SegTree tree[maxM << 1];
int t[maxN];
int n, m, tot;
void init_file()
{
freopen(fi, "r", stdin);
freopen(fo, "w", stdout);
}
void Build(int L, int R)
{
int Now = ++tot;
tree[Now].L = L;
tree[Now].R = R;
if (L == R)
{tree[Now].Min = t[L]; return; }
int Mid = (L + R) >> 1;
if (L < R)
{
tree[Now].lc = tot + 1;
Build(L, Mid);
tree[Now].rc = tot + 1;
Build(Mid + 1, R);
}
tree[Now].Min = min(tree[tree[Now].lc].Min,
tree[tree[Now].rc].Min);
}
int Query(int Now, int L, int R)
{
if (L <= tree[Now].L && R >= tree[Now].R)
return tree[Now].Min;
int Mid = (tree[Now].L + tree[Now].R) >> 1;
if (R <= Mid)
return Query(tree[Now].lc, L, R);
//完全被左子樹包含則直接等價於左子樹的子問題。
if (Mid < L)
return Query(tree[Now].rc, L, R);
//完全被右子樹包含則直接等價於右子樹的子問題。
int Lc = Query(tree[Now].lc, L, R),
Rc = Query(tree[Now].rc, L, R);
return min(Lc, Rc);
}
void work()
{
scanf("%d%d", &n, &m);
for (int i = 1; i < n + 1; ++i)
scanf("%d", t + i);
tot = 0;
Build(1, n);
for (; m; --m)
{
int L, R;
scanf("%d%d", &L, &R);
printf("%d\n", Query(1, L, R));
}
}
int main()
{
init_file();
work();
exit(0);
}