CO92REST - EDITORIAL

Author: Trung Nguyen
Tester and Editorialist: Alei Reyes ## Problem Link
Practice

Contest

Difficulty

Easy-Medium

PREREQUISITES:

Data Structure

Problem

We are given an array A with some elements and some unknowns. The goal is to find the number of possible arrays with numbers from 1 to k that satisfies certain constraints. Every constraint forces us to make some interval either strictly increasing or strictly decreasing in steps of one.

Explanation

Let’s store the information of constraints in another way. Let dx[i] = how much we have to add to A[i] to get A[i+1].

  • If interval from L to R should be increasing, then dx[L] = dx[L + 1] = ... = dx[R - 1] = 1;
  • If interval from L to R should be decreasing, then dx[L] = dx[L + 1] = ... = dx[R - 1] = -1;

For some elements there are no constraints and we have more freedom, for sake of simplicity let’s define its dx value as 0.

Note that all information about constraints is stored in dx. Moreover the structure of dx is as follows: [C][0][C][0][C][0]… Where each [C] represent a block of contiguos cells with values either 1 or -1, and [0] represents a block were dx is 0.

Let’s see in how many ways is possible to complete the array A for every type of block.

  • Blocks of type [C]:

    If array A contains at least one known element in the block, then all the elements are determined, and we only have to check their values.

    Otherwise, suppose that the first element is 0, and calculate the maximum and minimum values in the block (this can be done by adding the values of dx). We can generate new possibilities by adding some constant c to all the elements of the block, however c + minimum ≥ 1, and c + maximum ≤ k (remember that all numbers should be between 1 and k).

  • Blocks of type [0]

    In this case, we have more freedom and we can set any value between 1 and k. Except for the first element, it can be uniquely determined if the previous element of dx is 1 or -1.

The only question that remains is how to generate array dx. If we iterate over every constraint it is quadratic. However, if constraints are not overlapping it is linear.

Note that is not necessary to store overlapping intervals (constraints). For example, if segments [3, 5] and [4, 6] should be increasing, then is the same as saying that [3,6] is increasing. Therefore we can merge intervals and get only non-overlapping intervals.

There are many ways of merging intervals. For example, we can keep a set of disjoint intervals. Let’s say that interval [l1, r1] is smaller than [l2, r2] iff r1 < l2. When we add a new interval to the set, we have to remove all the intervals that intersect it (we can find them with binary search) and add a new interval. Every interval is added and removed at most once, so it is asymptotically efficient.

Implementation

Author’s Solution

Tester’s and Editorialist’s Solution

3 Likes

Is there any other way to solve this question?
Thanks in advance.

I know that i should merge queries … I was looking at editorial to find how to do that… can anyone help me ?? this editorial only contains logic which i already knew… how to do it optimally in code is my question and motto of viewing editorial…

1 Like

In the author’s solution and in the line - if (a[k] < 1 || a[k] > ::k) res = 0; what is the meaning of ::k ? what does a[k]> ::k give ?

If there are two increasing sequence [1…5], [10… 15]
And one decreasing sequence [5 10]
How will divide the block in [c][0][c]

Can someone please explain me how are we calculating number of ways = k-(max-min) for a range having all unknown elements(a[i]=-1) using sum,max,min from dx[]

We can generate new possibilities by adding some constant c to all the elements of the block, however c + minimum ≥ 1, and c + maximum ≤ k (remember that all numbers should be between 1 and k)

Perhaps an elegant way to generate array dx[] can be

for (int i = 0; i < n; ++i)
	adi[i] = add[i] = 0;
for (int i = 0; i < m; ++i) {
	char c;
	int l, r;
	cin >> c >> l >> r;
	--l;
	--r;
	if (c == 'I')
		++adi[l], --adi[r];
	else
		++add[l], --add[r];
}
for (int i = 0; i < n; ++i)
	dx[i] = 0;
int ni = 0, nd = 0;

for (int i = 0; i < n ; ++i) 
{
	ni += adi[i];
	nd += add[i];
	if (ni && nd) {
		cout << 0 << "\n";
		return;
	}
	if (ni)
		dx[i] = 1;
	else if (nd)
		dx[i] = -1;
}

can we make array dx[] with the help pf segment trees? how much will be its time complexity? O(mlogn) i guess?

I added another paragraph explaining that. For reference, in editorialist code, the function “add” adds the interval to the set keeping all intervals disjoint.

1 Like

@kamal_kulu_23,
This case will not have the block [0]. So, it can be represented as, [c][c][c], where, the first [c] is a block with increasing values [1,1,1,1], second [c] will be [-1,-1,-1,-1,-1] and the third would be [1,1,1,1,1].

thanks… :slight_smile: