C. Reborn and SegmentTree
time limit per test
3 с
memory limit per test
1024 megabytes
input
standard input
output
standard output

Reborn 最近做了一道有关 RMQ(Range Min/Max Query)的题目,她反手扔了一颗线段树上去但却得到了时间超限的反馈。于是她想看看她的代码究竟递归了多少次,写下了如下的代码:

#include <bits/stdc++.h>

using namespace std;
const int N = 2e5 + 10, inf = 0x3f3f3f3f;

#define min(a, b) ((a) < (b) ? (a) : (b))
int tot = 0;
int a[N];

struct SegmentTree{
int f[N << 2];
void build(int p, int l, int r){
if(l == r){
f[p] = a[l];
return;
}
int mid = (l + r) >> 1;
build(p << 1, l, mid);
build(p << 1 | 1, mid + 1, r);
f[p] = min(f[p << 1], f[p << 1 | 1]);
}
int query(int p, int l, int r, int ql, int qr){
tot += 1;
if(l > qr || r < ql) return inf;
if(l >= ql && r <= qr){
return f[p];
}
int mid = (l + r) >> 1;
return min(query(p << 1, l, mid, ql, qr), query(p << 1 | 1, mid + 1, r, ql, qr));
}
}seg;


int main(){
int n, q;
cin >> n >> q;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
seg.build(1, 1, n);
while(q > 0){
int l, r;
tot = 0;
cin >> l >> r;
seg.query(1, 1, n, l, r);
cout << tot << endl;
q -= 1;
}
return 0;
}

但是问题又出现了,她等待了半天依旧没有得到运行的结果。现在请你帮助她得到测试数据下上述代码的运行结果。

Input

输入第一行包含两个整数 $$$n, q$$$ $$$(1 \le n, q \le 2 \times 10^5)$$$。

输入第二行包含 $$$n$$$ 个整数 $$$a_1, a_2, ..., a_n$$$ $$$(1 \le a_i \le 10^9)$$$。

接下来 $$$q$$$ 行每行包含两个整数 $$$l, r$$$ $$$(1 \le l \le r \le n)$$$,表示一组询问。

Output

输出 $$$q$$$ 行,第 $$$i$$$ 行表示第 $$$i$$$ 个询问后 $$$tot$$$ 的值。

Example
Input
3 6
5 1 4
1 1
1 2
1 3
2 2
2 3
3 3
Output
10
4
1
10
10
4
Note

使用宏定义时请多加注意。