I want to reduce time complexity of my code that solves the following problem.

> 
> The program receives a positive distance d followed by a sequence of n
> events: An event can be either that a person appeared at some position, or that a person disappeared
> from some position. After each event, your program should output
> "red" if there are currently at least two persons at distance exactly
> d, and "yellow" otherwise. Note that initially (before the first
> event) all positions are empty. The events are given as a 2D array
> `event[0..n-1][0..1]`, where `event[i][0]` is "1" if a person appeared at position `event[i][1]` (on the x-axis), and
> "-1" if a person disappeared from that position. The output is an
> array `light[0..n-1]` of strings, where `light[i]` is the color directly
> after `event[i]`.
> 
> 
> 

I used the following code to solve this problem.

```
def cal_dis(d,pairs):
    found = False
    for i in range(0, len(pairs)-1):
        if(pairs[i+1]- pairs[i]) == d:
            found = True
            break
    return "red" if found else "yellow"

def tracker(n,d,event):
    ret = [""] * n
    pairs = []
    for i in range(0,n):
        if event[i][0] == 1:
            pairs.append(event[i][1])
            ret[i]= cal_dis(d,sorted(pairs))
        else:
            pairs.remove(event[i][1])
            ret[i]= cal_dis(d,sorted(pairs))
    return ret

```

Sample Input
------------

> 
> first row: n d
> 
> 
> next n rows: two integers; the first one is either -1
> or 1, the second one is nonnegative (these n rows correspond to the
> array "event")
> 
> 
> 

```
7 4
1 2
1 10
1 6
-1 2
-1 6
1 9
1 14

```

Sample Output
-------------

> 
> n rows each consisting of either the string "red" or "yellow".
> 
> 
> 

```
yellow
yellow
red
red
yellow
yellow
red

```