I was recently puzzled by this problem:
- Given $$$n$$$ intervals $$$[l_i,r_i]$$$. Find the lexicographically minimum permutation $$$p_1,p_2,\dots,p_n$$$ of $$$1-n$$$ such that $$$p_i\in [l_i,r_i]$$$ for each $$$1\leq i\leq n$$$.
This problem comes from misreading the problem A Place For My Head from Petrozavodsk Summer 2017. Day 5. Moscow IPT Contest. The original problem requires that $$$q_i\in [l_i,r_i]$$$ where $$$q=p^{-1}$$$ is the inverse permutation of $$$p$$$ and has constraint $$$1\leq n\leq 2\times 10^5$$$.
I wonder if there exists a decent (subquadratic) solution for this (misread version) of the problem? Many thanks in advance!







and three subsets
, To obtain
, we take the sum
+
+
. Unless
has been counted twice. So we subtract
. Now the count is correct except for the elements in
which have been added three times, but also subtracted three times. The answer is therefore 

be subsets of 
is counted in both sides. If
, then it's counted once on either side. Suppose
, and more precisely, that
is in exactly
of the sets
. The count on the left-hand side is
, and on the right hand side, we have 
, thus the equality holds.
, you need to compute the number of integers
such that
, that is,
. There are
testcases. Constraints:
,
.
such that
, then the answer is
. How we're gonna compute
and then find all prime factors
of
be the set of numbers that are divisible by
, then the answer is the
, which may be hard to compute directly. However, using the restricted inclusion-exclusion principle, we can convert the problem into computing the size of the intersection of sets, which is trivial. Time complexity is
, with
equals to the number of distinct prime factors of
of properties that the elements of
,
, or even
. Let
(and possibly others). Then
is the number of elements that process none of the properties. Clearly,
is the set of elements that possess the properties
(and maybe others). Using the notation


depends only on the size
. We can then write
for
, and call
a homogeneous set of properties, and in this case
also depends only on the cardinality of
. Hence for homogeneous properties, we have
, we arrive at the restricted inclusion-exclusion principle.
, satisfying that
, modulo
. Constraints:
. Hint: the number of non-negative integer solutions to
is given by
.
. To do that, we apply the inclusion-exclusion principle. Let
, then
is our desired answer. Clearly, this set of properties is homogeneous. Take
, then
is the number of solutions with
. Setting
, and it's the same as the number of solutions of the system 

, due to precomputing factorials and the modular inverses of factorials.
. Constraint:
.
as the number of permutations of length
with
inversions. The recurrence is also trivial:
. This is
, and can be optimized to
using prefix sums, which is still not enough due to the given constraints.
to the number of inversions, so the answer is equal to the number of solutions to
.
, then
, then the number of solutions to the equation is
. Therefore, we can group those sets together. By inclusion-exclusion principle, 

.
. We can use the technique as we can computing partition numbers. Partition
in the set or not, and then we get the recurrence
. Another important observation is that there are at most
valid values for
.
.
, and then what remains, is a prime
, a square of a prime
, or a product of two distinct primes
to check if it's the second case, and otherwise the last case.
. We need to avoid counting the cases with
and
, thus we should apply inclusion-exclusion principle. Let
and
. Then the answer is
,which is too much for
in te worst case. However, noticing that two of the options of for each prime divisor lead to same computations, the complexity can be reduced to
.
using FFT. Furthermore, since we only concern the answer with
modulo 