#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
cin >> n >> q;
vector<int> input(n);
for (int &x: input) cin >> x;
vector<vector<int>> segment_tree(4 * n);
function<void(int, int, int)> build = [&](int vertex, int left, int right) {
if (left > right) return;
if (left == right)
{
segment_tree[vertex] = { input[left] };
return;
}
int middle = left + (right - left) / 2;
int left_child = 2 * vertex + 1;
int right_child = 2 * vertex + 2;
build(left_child, left, middle);
build(right_child, middle + 1, right);
vector<int> combined;
auto commit_monotone = [&](int value) {
if (combined.empty())
{
combined.push_back(value);
return;
}
if (combined.back() < value)
{
combined.push_back(value);
return;
}
};
for (int &x: segment_tree[left_child])
commit_monotone(x);
for (int &x: segment_tree[right_child])
commit_monotone(x);
segment_tree[vertex] = combined;
};
build(0, 0, n - 1);
auto query = [&](int query_left, int query_right) {
function<list<int>(int, int, int)> traverse = [&](int vertex, int left, int right) {
if (left > right) return list<int> {};
if (query_left > right) return list<int> {};
if (query_right < left) return list<int> {};
if (query_left <= left && right <= query_right)
{
return list<int> { vertex };
}
int middle = left + (right - left) / 2;
int left_child = 2 * vertex + 1;
int right_child = 2 * vertex + 2;
auto left_result = traverse(left_child, left, middle);
left_result.splice(left_result.end(), traverse(right_child, middle + 1, right));
return left_result;
};
return traverse(0, 0, n - 1);
};
for (int i = 0; i < q; i++)
{
int left, right;
cin >> left >> right;
left--; right--;
auto lists = query(left, right);
int last_seen_max = -1;
int count = 0;
for (auto &vertex: lists)
{
if (last_seen_max == -1)
{
last_seen_max = segment_tree[vertex].back();
count = segment_tree[vertex].size();
continue;
}
auto first_it = lower_bound(segment_tree[vertex].begin(), segment_tree[vertex].end(), last_seen_max);
if (first_it == segment_tree[vertex].end()) continue;
count += segment_tree[vertex].end() - first_it;
last_seen_max = segment_tree[vertex].back();
}
cout << count << "\n";
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmludCBtYWluKCkKewogICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7CiAgICBjaW4udGllKG51bGxwdHIpOwoKICAgIGludCBuLCBxOwogICAgY2luID4+IG4gPj4gcTsKCiAgICB2ZWN0b3I8aW50PiBpbnB1dChuKTsKICAgIGZvciAoaW50ICZ4OiBpbnB1dCkgY2luID4+IHg7CgogICAgdmVjdG9yPHZlY3RvcjxpbnQ+PiBzZWdtZW50X3RyZWUoNCAqIG4pOwoKICAgIGZ1bmN0aW9uPHZvaWQoaW50LCBpbnQsIGludCk+IGJ1aWxkID0gWyZdKGludCB2ZXJ0ZXgsIGludCBsZWZ0LCBpbnQgcmlnaHQpIHsKICAgICAgICBpZiAobGVmdCA+IHJpZ2h0KSByZXR1cm47CiAgICAgICAgaWYgKGxlZnQgPT0gcmlnaHQpCiAgICAgICAgewogICAgICAgICAgICBzZWdtZW50X3RyZWVbdmVydGV4XSA9IHsgaW5wdXRbbGVmdF0gfTsKICAgICAgICAgICAgcmV0dXJuOwogICAgICAgIH0KCiAgICAgICAgaW50IG1pZGRsZSA9IGxlZnQgKyAocmlnaHQgLSBsZWZ0KSAvIDI7CiAgICAgICAgaW50IGxlZnRfY2hpbGQgPSAyICogdmVydGV4ICsgMTsKICAgICAgICBpbnQgcmlnaHRfY2hpbGQgPSAyICogdmVydGV4ICsgMjsKCiAgICAgICAgYnVpbGQobGVmdF9jaGlsZCwgbGVmdCwgbWlkZGxlKTsKICAgICAgICBidWlsZChyaWdodF9jaGlsZCwgbWlkZGxlICsgMSwgcmlnaHQpOwoKICAgICAgICB2ZWN0b3I8aW50PiBjb21iaW5lZDsKCiAgICAgICAgYXV0byBjb21taXRfbW9ub3RvbmUgPSBbJl0oaW50IHZhbHVlKSB7CiAgICAgICAgICAgIGlmIChjb21iaW5lZC5lbXB0eSgpKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBjb21iaW5lZC5wdXNoX2JhY2sodmFsdWUpOwogICAgICAgICAgICAgICAgcmV0dXJuOwogICAgICAgICAgICB9CgogICAgICAgICAgICBpZiAoY29tYmluZWQuYmFjaygpIDwgdmFsdWUpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGNvbWJpbmVkLnB1c2hfYmFjayh2YWx1ZSk7CiAgICAgICAgICAgICAgICByZXR1cm47CiAgICAgICAgICAgIH0KICAgICAgICB9OwoKICAgICAgICBmb3IgKGludCAmeDogc2VnbWVudF90cmVlW2xlZnRfY2hpbGRdKQogICAgICAgICAgICBjb21taXRfbW9ub3RvbmUoeCk7CiAgICAgICAgZm9yIChpbnQgJng6IHNlZ21lbnRfdHJlZVtyaWdodF9jaGlsZF0pCiAgICAgICAgICAgIGNvbW1pdF9tb25vdG9uZSh4KTsKCiAgICAgICAgc2VnbWVudF90cmVlW3ZlcnRleF0gPSBjb21iaW5lZDsKICAgIH07CgogICAgYnVpbGQoMCwgMCwgbiAtIDEpOwoKICAgIGF1dG8gcXVlcnkgPSBbJl0oaW50IHF1ZXJ5X2xlZnQsIGludCBxdWVyeV9yaWdodCkgewogICAgICAgIGZ1bmN0aW9uPGxpc3Q8aW50PihpbnQsIGludCwgaW50KT4gdHJhdmVyc2UgPSBbJl0oaW50IHZlcnRleCwgaW50IGxlZnQsIGludCByaWdodCkgewogICAgICAgICAgICBpZiAobGVmdCA+IHJpZ2h0KSByZXR1cm4gbGlzdDxpbnQ+IHt9OwogICAgICAgICAgICBpZiAocXVlcnlfbGVmdCA+IHJpZ2h0KSByZXR1cm4gbGlzdDxpbnQ+IHt9OwogICAgICAgICAgICBpZiAocXVlcnlfcmlnaHQgPCBsZWZ0KSByZXR1cm4gbGlzdDxpbnQ+IHt9OwogICAgICAgICAgICBpZiAocXVlcnlfbGVmdCA8PSBsZWZ0ICYmIHJpZ2h0IDw9IHF1ZXJ5X3JpZ2h0KQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICByZXR1cm4gbGlzdDxpbnQ+IHsgdmVydGV4IH07CiAgICAgICAgICAgIH0KCiAgICAgICAgICAgIGludCBtaWRkbGUgPSBsZWZ0ICsgKHJpZ2h0IC0gbGVmdCkgLyAyOwogICAgICAgICAgICBpbnQgbGVmdF9jaGlsZCA9IDIgKiB2ZXJ0ZXggKyAxOwogICAgICAgICAgICBpbnQgcmlnaHRfY2hpbGQgPSAyICogdmVydGV4ICsgMjsKCiAgICAgICAgICAgIGF1dG8gbGVmdF9yZXN1bHQgPSB0cmF2ZXJzZShsZWZ0X2NoaWxkLCBsZWZ0LCBtaWRkbGUpOwogICAgICAgICAgICBsZWZ0X3Jlc3VsdC5zcGxpY2UobGVmdF9yZXN1bHQuZW5kKCksIHRyYXZlcnNlKHJpZ2h0X2NoaWxkLCBtaWRkbGUgKyAxLCByaWdodCkpOwoKICAgICAgICAgICAgcmV0dXJuIGxlZnRfcmVzdWx0OwogICAgICAgIH07CgogICAgICAgIHJldHVybiB0cmF2ZXJzZSgwLCAwLCBuIC0gMSk7CiAgICB9OwoKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgcTsgaSsrKQogICAgewogICAgICAgIGludCBsZWZ0LCByaWdodDsKICAgICAgICBjaW4gPj4gbGVmdCA+PiByaWdodDsKICAgICAgICBsZWZ0LS07IHJpZ2h0LS07CgogICAgICAgIGF1dG8gbGlzdHMgPSBxdWVyeShsZWZ0LCByaWdodCk7CgogICAgICAgIGludCBsYXN0X3NlZW5fbWF4ID0gLTE7CiAgICAgICAgaW50IGNvdW50ID0gMDsKICAgICAgICBmb3IgKGF1dG8gJnZlcnRleDogbGlzdHMpCiAgICAgICAgewogICAgICAgICAgICBpZiAobGFzdF9zZWVuX21heCA9PSAtMSkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgbGFzdF9zZWVuX21heCA9IHNlZ21lbnRfdHJlZVt2ZXJ0ZXhdLmJhY2soKTsKICAgICAgICAgICAgICAgIGNvdW50ID0gc2VnbWVudF90cmVlW3ZlcnRleF0uc2l6ZSgpOwogICAgICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgICAgIH0KCiAgICAgICAgICAgIGF1dG8gZmlyc3RfaXQgPSBsb3dlcl9ib3VuZChzZWdtZW50X3RyZWVbdmVydGV4XS5iZWdpbigpLCBzZWdtZW50X3RyZWVbdmVydGV4XS5lbmQoKSwgbGFzdF9zZWVuX21heCk7CiAgICAgICAgICAgIGlmIChmaXJzdF9pdCA9PSBzZWdtZW50X3RyZWVbdmVydGV4XS5lbmQoKSkgY29udGludWU7CiAgICAgICAgICAgIGNvdW50ICs9IHNlZ21lbnRfdHJlZVt2ZXJ0ZXhdLmVuZCgpIC0gZmlyc3RfaXQ7CiAgICAgICAgICAgIGxhc3Rfc2Vlbl9tYXggPSBzZWdtZW50X3RyZWVbdmVydGV4XS5iYWNrKCk7CiAgICAgICAgfQoKICAgICAgICBjb3V0IDw8IGNvdW50IDw8ICJcbiI7CiAgICB9Cn0=