Some of $n$ towns are connected by two-way airlines. There are $m$ airlines in total. For $i = 1, 2, \cdots, n$, let $d_i$ be the number of airlines going from town $i$. If $1\le d_i \le 2010$ for each $i = 1, 2,\cdots, 2010$, prove that
\[\displaystyle\sum_{i=1}^n d_i^2\le 4022m- 2010n\]
Find all $n$ for which equality can be attained. 

[i]Proposed by Aleksandar Ilic[/i]
1. We start by noting that each town \(i\) has \(d_i\) airlines, and the sum of all degrees is twice the number of airlines:
   \[
   \sum_{i=1}^n d_i = 2m
   \]
2. Given the condition \(1 \leq d_i \leq 2010\), we can derive the inequality:
   \[
   0 \leq (d_i - 1)(2010 - d_i)
   \]
   Expanding this, we get:
   \[
   0 \leq 2010d_i - d_i^2 - 2010 + d_i
   \]
   Rearranging terms, we obtain:
   \[
   d_i^2 \leq 2011d_i - 2010
   \]
3. Summing this inequality over all \(i\) from 1 to \(n\), we have:
   \[
   \sum_{i=1}^n d_i^2 \leq \sum_{i=1}^n (2011d_i - 2010)
   \]
   This simplifies to:
   \[
   \sum_{i=1}^n d_i^2 \leq 2011 \sum_{i=1}^n d_i - 2010n
   \]
4. Using the fact that \(\sum_{i=1}^n d_i = 2m\), we substitute:
   \[
   \sum_{i=1}^n d_i^2 \leq 2011 \cdot 2m - 2010n
   \]
   Simplifying further, we get:
   \[
   \sum_{i=1}^n d_i^2 \leq 4022m - 2010n
   \]
   This proves the desired inequality.

5. For equality to hold, each \(d_i\) must satisfy:
   \[
   d_i^2 = 2011d_i - 2010
   \]
   Solving this quadratic equation:
   \[
   d_i^2 - 2011d_i + 2010 = 0
   \]
   Using the quadratic formula \(d_i = \frac{2011 \pm \sqrt{2011^2 - 4 \cdot 2010}}{2}\), we find:
   \[
   d_i = \frac{2011 \pm \sqrt{4044121 - 8040}}{2} = \frac{2011 \pm \sqrt{4036081}}{2} = \frac{2011 \pm 2010}{2}
   \]
   Thus, \(d_i\) can be either 1 or 2010.

6. To achieve equality, the degrees \(d_i\) must be either 1 or 2010 for all \(i\). We consider two cases:
   - If \(n\) is even, say \(n = 2k\), we can set \(d_i = 1\) for all \(i\), which is a valid configuration.
   - If \(n\) is odd, say \(n = 2k - 1\), we cannot have all \(d_i = 1\) because the sum of degrees must be even. Therefore, at least one \(d_i\) must be 2010, implying \(n \geq 2011\).

7. Therefore, equality can be attained if and only if \(n\) is even, or \(n\) is odd and \(n \geq 2011\).

The final answer is \( \boxed{ n } \) is even, or \(n\) is odd and \(n \geq 2011\).