What does this mean?

Vladik had started reading a complicated book about algorithms containing n pages. To improve understanding of what is written, his friends advised him to read pages in some order given by permutation P = [p1, p2, ..., pn], where pi denotes the number of page that should be read i-th in turn.

Sometimes Vladik’s mom sorted some subsegment of permutation P from position l to position r inclusive, because she loves the order. For every of such sorting Vladik knows number x — what index of page in permutation he should read. He is wondered if the page, which he will read after sorting, has changed. In other words, has px changed? After every sorting Vladik return permutation to initial state, so you can assume that each sorting is independent from each other.

Input
First line contains two space-separated integers n, m (1 ≤ n, m ≤ 104) — length of permutation and number of times Vladik's mom sorted some subsegment of the book.

Second line contains n space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — permutation P. Note that elements in permutation are distinct.

Each of the next m lines contains three space-separated integers li, ri, xi (1 ≤ li ≤ xi ≤ ri ≤ n) — left and right borders of sorted subsegment in i-th sorting and position that is interesting to Vladik.

Output
For each mom’s sorting on it’s own line print "Yes", if page which is interesting to Vladik hasn't changed, or "No" otherwise.

Examples
input
5 5
5 4 3 2 1
1 5 3
1 3 1
2 4 3
4 4 4
2 5 3
output
Yes
No
Yes
Yes
No
input
6 5
1 4 3 2 5 6
2 4 3
1 6 2
4 5 4
1 3 3
2 6 3
output
Yes
No
Yes
No
Yes
Note
Explanation of first test case:

[1, 2, 3, 4, 5] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
[3, 4, 5, 2, 1] — permutation after sorting, 1-st element has changed, so answer is "No".
[5, 2, 3, 4, 1] — permutation after sorting, 3-rd element hasn’t changed, so answer is "Yes".
[5, 4, 3, 2, 1] — permutation after sorting, 4-th element hasn’t changed, so answer is "Yes".
[5, 1, 2, 3, 4] — permutation after sorting, 3-rd element has changed, so answer is "No".



I tried solving this using 2 arrays but I get a time limit exceeded at a certain test case that's just too big, so this was one of the solutions and I don't understand 1 thing there , first here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main() {
	int n, m;
	cin >> n >> m;
	vector<int> v(n);
	for (int i = 0; i < n; i++)
		cin >> v[i];

	while (m--) {
		int l, r, k;
		cin >> l >> r >> k;
		l--, r--, k--;
		int cnt = 0;
		for (int i = l; i <= r; i++)
			cnt += v[i] <= v[k];
		if (cnt == k - l + 1)
			cout << "Yes" << endl;
		else
			cout << "No" << endl;
	}

	return 0;
}



What's the meaning of this part? why does the solution involve counting the elements smaller than the index 'k' and subtracting it from l then adding 1 to it. I don't really get what this method did
1
2
3
for (int i = l; i <= r; i++) // line 13
cnt += v[i] <= v[k];
if (cnt == k - l + 1)// line 15 


Also more specifically why do we add one in the if condition? we could just subtract the indexes from each other.

EDIT: Seems like removing the = in the counting condition eliminates the use of the +1 in the condition later on but why?
Last edited on
You can write most loops to iterate down instead of up. Most of the time, with very few exceptions, the answer to why on that is "the programmer did it that way". When figuring it out, maybe this felt more natural to whoever was coding it.

line 14 is a little slippery. A LOT of c++ compilers use 1 and 0 for true and false, so what it does is add 0 or 1 to count depending on the conditional -- on a compiler that does it with zero and one, that is. I think this isn't assured in the language, but one of our forum standards gurus can tell you more.

I didn't follow the algorithm closely enough to comment on line 15. Its some check to determine what to write out, but if you want to know what it is doing, you should manually iterate (on a sheet of paper) the loop to see... that is the best way when dealing with small cryptic messes like this. Or you can add a LOT of print statements in there to see what it does each iteration.


I think this isn't assured in the language

It's guaranteed.
http://eel.is/c++draft/conv.integral#4

It's a clever little algorithm, but that's not really a complement. Here:
1
2
for (int i = l; i <= r; i++) // line 13
  cnt += v[i] <= v[k];

Is a hackish bit of code which computes the offset (from position l) of the element at position k as if the sub-sequence was sorted (without actually sorting it). The loop does this by counting the number of elements in the range which are less-or-equal-to the interesting element.

In other words, the loop could be written as
int offset = std::count_if(v.begin() + l, v.begin() + r, [&](int elt){ return elt <= v[k]; });
Which leaves offset containing the position of v[k] relative to l, were that sequence to be sorted.

The subtraction and addition computes the offset of k from l, because initially k is an offset from the first element. The + 1 is there because the loop would count up v[k] itself in the offset - it uses <= instead of <.

Last edited on
Topic archived. No new replies allowed.