[luoguP1440] 求m区间内的最小值(单调队列 || 线段树)

传送门

这种水题没必要搞线段树了,单调队列就行啊。

——代码

 1 #include <cstdio>
 2 
 3 const int MAXN = 2000001;
 4 int n, m, h = 1, t = 1;
 5 int a[MAXN], q[MAXN];
 6 
 7 int main()
 8 {
 9     int i;
10     scanf("%d %d", &n, &m);
11     for(i = 1; i <= n; i++) scanf("%d", &a[i]);
12     q[1] = 1;
13     printf("0
");
14     for(i = 2; i <= n; i++)
15     {
16         while(h <= t && q[h] < i - m) h++;
17         printf("%d
", a[q[h]]);
18         while(h <= t && a[q[t]] > a[i]) t--;
19         q[++t] = i;
20     }
21     return 0;
22 }
View Code
原文地址:https://www.cnblogs.com/zhenghaotian/p/6855450.html