Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n = int(input())
    keys = []
    for i in xrange(n):
        keys.append(map(int,raw_input().split())+[i])

    T = Tree()
    T.recursive_add_Node(keys,0)

    q = int(input())
    for i in xrange(q):
        sx, tx, sy, ty = map(int,raw_input().split())
        nums = []
        find(T.root,[sx,sy],[tx,ty],0,nums)
        nums.sort()
        for i in nums:
            print i
        print ""
        

def find(z,s,t,d,nums):
    if(s[0]<=z.key[0]<=t[0])and(s[1]<=z.key[1]<=t[1]):
        nums.append(z.num)

    d += 1
    if (z.left != None)and(z.key[d%2]>=s[d%2]):
        find(z.left,s,t,d,nums)
    if (z.right != None)and(z.key[d%2]<=t[d%2]):
        find(z.right,s,t,d,nums)


class Tree():
    def __init__(self):
        self.root = None

    def add_Node(self,k,n):
        y = None
        x = self.root
        d = 0
        while(x != None):
            d += 1
            y = x
            if x.key[d%2] > k[d%2]:
                x = x.left
            else:
                x = x.right
        
        z = Node(k,n)
        z.parent = y
        z.depth  = d

        if y == None:
            self.root = z
        elif y.key[d%2] > k[d%2]:
            y.left = z
        else:
            y.right = z
        
    def recursive_add_Node(self,keys,depth):
        keys.sort(key=lambda x:x[depth%2])
        id_mean = len(keys)/2
        self.add_Node(keys[id_mean][:2],keys[id_mean][2])

        keys_l = keys[:id_mean]
        keys_r = keys[id_mean+1:]
       
        if keys_l:
            self.recursive_add_Node(keys_l,depth+1)
        if keys_r:
            self.recursive_add_Node(keys_r,depth+1)

class Node():
    def __init__(self,k,n):
        self.key    = k
        self.num    = n
        self.depth  = None
        self.parent = None
        self.left   = None
        self.right  = None
    


if __name__ == "__main__":
    main()

Fixed submission:
from sys import stdin,stdout
import math
from operator import itemgetter
from bisect import bisect_left, bisect_right
readline = stdin.readline
 
def main():
    n = int(input())
    keys = map(lambda i: map(int,readline().split())+[i], xrange(n))
    keys.sort()

    root = int(math.sqrt(len(keys)))
 
    low = [x for x, y, i in keys[::root]]
    high = [x for x, y, i in keys[root - 1::root]] + [float('inf')]
 
    keys = [sorted(keys[i:i+root], key=itemgetter(1)) for i in range(0,len(keys),root)]
    keys_y = map(lambda k:[y for x,y,i in k], keys)

    q = int(input())
    for i in xrange(q):
        sx,tx,sy,ty = map(int,readline().split())
        
        nums = []
        for i in range(bisect_left(high, sx), bisect_right(low, tx)):
            y = keys_y[i]
            for j in range(bisect_left(y, sy), bisect_right(y, ty)):
                if sx <= keys[i][j][0] <= tx:
                    nums.append(keys[i][j][2])
        if nums:
            nums.sort()
            map(lambda n:stdout.write("{}\n".format(n)), nums)
        print("")

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from operator import itemgetter

def main():
    n = int(input())
    keys = []
    for i in xrange(n):
        keys.append(map(int,raw_input().split())+[i])

    T = Tree()
    T.recursive_add_Node(keys,0)

    q = int(input())
    for i in xrange(q):
        sx, tx, sy, ty = map(int,raw_input().split())
        nums = []
        find(T.root,[sx,sy],[tx,ty],0,nums)
        nums.sort()
        for i in nums:
            print i
        print ""
        

def find(z,s,t,d,nums):
    if(s[0]<=z.key[0]<=t[0])and(s[1]<=z.key[1]<=t[1]):
        nums.append(z.num)

    d += 1
    if (z.left != None)and(z.key[d%2]>=s[d%2]):
        find(z.left,s,t,d,nums)
    if (z.right != None)and(z.key[d%2]<=t[d%2]):
        find(z.right,s,t,d,nums)


class Tree():
    def __init__(self):
        self.root = None

    def add_Node(self,k,n):
        y = None
        x = self.root
        d = 0
        while(x != None):
            d += 1
            y = x
            if x.key[d%2] > k[d%2]:
                x = x.left
            else:
                x = x.right
        
        z = Node(k,n)
        z.parent = y
        z.depth  = d

        if y == None:
            self.root = z
        elif y.key[d%2] > k[d%2]:
            y.left = z
        else:
            y.right = z
        
    def recursive_add_Node(self,keys,depth):
        keys.sort(key=itemgetter(depth%2))
        id_mean = len(keys)/2
        self.add_Node(keys[id_mean][:2],keys[id_mean][2])

        keys_l = keys[:id_mean]
        keys_r = keys[id_mean+1:]
       
        if keys_l:
            self.recursive_add_Node(keys_l,depth+1)
        if keys_r:
            self.recursive_add_Node(keys_r,depth+1)

class Node():
    def __init__(self,k,n):
        self.key    = k
        self.num    = n
        self.depth  = None
        self.parent = None
        self.left   = None
        self.right  = None
    


if __name__ == "__main__":
    main()

Fixed submission:
from sys import stdin,stdout
import math
from operator import itemgetter
from bisect import bisect_left, bisect_right
readline = stdin.readline
 
def main():
    n = int(input())
    keys = map(lambda i: map(int,readline().split())+[i], xrange(n))
    keys.sort()

    root = int(math.sqrt(len(keys)))
 
    low = [x for x, y, i in keys[::root]]
    high = [x for x, y, i in keys[root - 1::root]] + [float('inf')]
 
    keys = [sorted(keys[i:i+root], key=itemgetter(1)) for i in range(0,len(keys),root)]
    keys_y = map(lambda k:[y for x,y,i in k], keys)

    q = int(input())
    for i in xrange(q):
        sx,tx,sy,ty = map(int,readline().split())
        
        nums = []
        for i in range(bisect_left(high, sx), bisect_right(low, tx)):
            y = keys_y[i]
            for j in range(bisect_left(y, sy), bisect_right(y, ty)):
                if sx <= keys[i][j][0] <= tx:
                    nums.append(keys[i][j][2])
        if nums:
            nums.sort()
            map(lambda n:stdout.write("{}\n".format(n)), nums)
        print("")

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from operator import itemgetter

def main():
    n = int(input())
    keys = []
    for i in xrange(n):
        keys.append(map(int,raw_input().split())+[i])

    T = Tree()
    T.recursive_add_Node(keys,0)

    q = int(input())
    for i in xrange(q):
        sx, tx, sy, ty = map(int,raw_input().split())
        nums = []
        find(T.root,[sx,sy],[tx,ty],0,nums)
        nums.sort()
        for i in nums:
            print i
        print ""
        

def find(z,s,t,d,nums):
    if(s[0]<=z.key[0]<=t[0])and(s[1]<=z.key[1]<=t[1]):
        nums.append(z.num)

    d += 1
    xy = d%2
    if (z.left != None)and(z.key[xy]>=s[xy]):
        find(z.left,s,t,d,nums)
    if (z.right != None)and(z.key[xy]<=t[xy]):
        find(z.right,s,t,d,nums)


class Tree():
    def __init__(self):
        self.root = None

    def add_Node(self,k,n):
        y = None
        x = self.root
        d = 0
        xy = 0
        while(x != None):
            d += 1
            xy = d%2
            y = x
            if x.key[xy] > k[xy]:
                x = x.left
            else:
                x = x.right
        
        z = Node(k,n)
        z.parent = y
        z.depth  = d

        if y == None:
            self.root = z
        elif y.key[xy] > k[xy]:
            y.left = z
        else:
            y.right = z
        
    def recursive_add_Node(self,keys,depth):
        keys.sort(key=itemgetter(depth%2))
        id_mean = len(keys)/2
        self.add_Node(keys[id_mean][:2],keys[id_mean][2])
       
        if id_mean != 0:
            self.recursive_add_Node(keys[:id_mean],depth+1)
        if id_mean+1 != len(keys):
            self.recursive_add_Node(keys[id_mean+1:],depth+1)

class Node():
    def __init__(self,k,n):
        self.key    = k
        self.num    = n
        self.depth  = None
        self.parent = None
        self.left   = None
        self.right  = None
    


if __name__ == "__main__":
    main()

Fixed submission:
from sys import stdin,stdout
import math
from operator import itemgetter
from bisect import bisect_left, bisect_right
readline = stdin.readline
 
def main():
    n = int(input())
    keys = map(lambda i: map(int,readline().split())+[i], xrange(n))
    keys.sort()

    root = int(math.sqrt(len(keys)))
 
    low = [x for x, y, i in keys[::root]]
    high = [x for x, y, i in keys[root - 1::root]] + [float('inf')]
 
    keys = [sorted(keys[i:i+root], key=itemgetter(1)) for i in range(0,len(keys),root)]
    keys_y = map(lambda k:[y for x,y,i in k], keys)

    q = int(input())
    for i in xrange(q):
        sx,tx,sy,ty = map(int,readline().split())
        
        nums = []
        for i in range(bisect_left(high, sx), bisect_right(low, tx)):
            y = keys_y[i]
            for j in range(bisect_left(y, sy), bisect_right(y, ty)):
                if sx <= keys[i][j][0] <= tx:
                    nums.append(keys[i][j][2])
        if nums:
            nums.sort()
            map(lambda n:stdout.write("{}\n".format(n)), nums)
        print("")

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from operator import itemgetter
import sys

def main():
    n = int(input())
    keys = []
    for i in xrange(n):
        keys.append(map(int,raw_input().split())+[i])

    T = Tree()
    T.recursive_add_Node(keys,0)

    q = int(input())
    for i in xrange(q):
        sx, tx, sy, ty = map(int,raw_input().split())
        nums = []
        find(T.root,[sx,sy],[tx,ty],0,nums)
        nums.sort()
        for i in nums:
            sys.stdout.write("{}\n".format(i))
        print ("")

        

def find(z,s,t,d,nums):
    if(s[0]<=z.key[0]<=t[0])and(s[1]<=z.key[1]<=t[1]):
        nums.append(z.num)
    d += 1
    xy = d%2
    if (z.left != None)and(z.key[xy]>=s[xy]):
        find(z.left,s,t,d,nums)    
    if (z.right != None)and(z.key[xy]<=t[xy]):
        find(z.right,s,t,d,nums)


class Tree():
    def __init__(self):
        self.root = None

    def add_Node(self,k,n):
        y = None
        x = self.root
        d = 0
        xy = 0
        while(x != None):
            d += 1
            xy = d%2
            y = x
            if x.key[xy] > k[xy]:
                x = x.left
            else:
                x = x.right
        
        z = Node(k,n)
        z.parent = y
        z.depth  = d

        if y == None:
            self.root = z
        elif y.key[xy] > k[xy]:
            y.left = z
        else:
            y.right = z
        
    def recursive_add_Node(self,keys,depth):
        keys.sort(key=itemgetter(depth%2))
        id_mean = len(keys)/2
        self.add_Node(keys[id_mean][:2],keys[id_mean][2])
       
        if id_mean != 0:
            self.recursive_add_Node(keys[:id_mean],depth+1)
        if id_mean+1 != len(keys):
            self.recursive_add_Node(keys[id_mean+1:],depth+1)

class Node():
    def __init__(self,k,n):
        self.key    = k
        self.num    = n
        self.depth  = None
        self.parent = None
        self.left   = None
        self.right  = None
    


if __name__ == "__main__":
    main()

Fixed submission:
from sys import stdin,stdout
import math
from operator import itemgetter
from bisect import bisect_left, bisect_right
readline = stdin.readline
 
def main():
    n = int(input())
    keys = map(lambda i: map(int,readline().split())+[i], xrange(n))
    keys.sort()

    root = int(math.sqrt(len(keys)))
 
    low = [x for x, y, i in keys[::root]]
    high = [x for x, y, i in keys[root - 1::root]] + [float('inf')]
 
    keys = [sorted(keys[i:i+root], key=itemgetter(1)) for i in range(0,len(keys),root)]
    keys_y = map(lambda k:[y for x,y,i in k], keys)

    q = int(input())
    for i in xrange(q):
        sx,tx,sy,ty = map(int,readline().split())
        
        nums = []
        for i in range(bisect_left(high, sx), bisect_right(low, tx)):
            y = keys_y[i]
            for j in range(bisect_left(y, sy), bisect_right(y, ty)):
                if sx <= keys[i][j][0] <= tx:
                    nums.append(keys[i][j][2])
        if nums:
            nums.sort()
            map(lambda n:stdout.write("{}\n".format(n)), nums)
        print("")

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from operator import itemgetter
from collections import deque
import sys

def main():
    n = int(input())  
    keys = map(lambda i:(map(int,raw_input().split())+[i]),xrange(n))

    T = Tree()
    T.recursive_add_Node(keys,0)

    q = int(input())
    for i in xrange(q):
        sx, tx, sy, ty = map(int,raw_input().split())
        nums = deque()
        find(T.root,[sx,sy],[tx,ty],0,nums)
        nums = list(nums)
        nums.sort()
        map(lambda i:sys.stdout.write("{}\n".format(i)), nums)
        print ("")

        

def find(z,s,t,d,nums):
    if(s[0]<=z.key[0]<=t[0])and(s[1]<=z.key[1]<=t[1]):
        nums.append(z.num)
    d += 1
    xy = d%2
    if (z.left != None)and(z.key[xy]>=s[xy]):
        find(z.left,s,t,d,nums)    
    if (z.right != None)and(z.key[xy]<=t[xy]):
        find(z.right,s,t,d,nums)


class Tree():
    def __init__(self):
        self.root = None

    def add_Node(self,k,n):
        y = None
        x = self.root
        d = 0
        xy = 0
        while(x != None):
            d += 1
            xy = d%2
            y = x
            if x.key[xy] > k[xy]:
                x = x.left
            else:
                x = x.right
        
        z = Node(k,n)
        z.parent = y
        z.depth  = d

        if y == None:
            self.root = z
        elif y.key[xy] > k[xy]:
            y.left = z
        else:
            y.right = z
        
    def recursive_add_Node(self,keys,depth):
        keys.sort(key=itemgetter(depth%2))
        id_mean = len(keys)/2
        self.add_Node(keys[id_mean][:2],keys[id_mean][2])
       
        if id_mean != 0:
            self.recursive_add_Node(keys[:id_mean],depth+1)
        if id_mean+1 != len(keys):
            self.recursive_add_Node(keys[id_mean+1:],depth+1)

class Node():
    def __init__(self,k,n):
        self.key    = k
        self.num    = n
        self.depth  = None
        self.parent = None
        self.left   = None
        self.right  = None
    


if __name__ == "__main__":
    main()

Fixed submission:
from sys import stdin,stdout
import math
from operator import itemgetter
from bisect import bisect_left, bisect_right
readline = stdin.readline
 
def main():
    n = int(input())
    keys = map(lambda i: map(int,readline().split())+[i], xrange(n))
    keys.sort()

    root = int(math.sqrt(len(keys)))
 
    low = [x for x, y, i in keys[::root]]
    high = [x for x, y, i in keys[root - 1::root]] + [float('inf')]
 
    keys = [sorted(keys[i:i+root], key=itemgetter(1)) for i in range(0,len(keys),root)]
    keys_y = map(lambda k:[y for x,y,i in k], keys)

    q = int(input())
    for i in xrange(q):
        sx,tx,sy,ty = map(int,readline().split())
        
        nums = []
        for i in range(bisect_left(high, sx), bisect_right(low, tx)):
            y = keys_y[i]
            for j in range(bisect_left(y, sy), bisect_right(y, ty)):
                if sx <= keys[i][j][0] <= tx:
                    nums.append(keys[i][j][2])
        if nums:
            nums.sort()
            map(lambda n:stdout.write("{}\n".format(n)), nums)
        print("")

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from operator import itemgetter
from collections import deque
import sys

def main():
    n = int(input())  
    keys = map(lambda i:(map(int,raw_input().split())+[i]),xrange(n))

    keys.sort(key=itemgetter(0))
    id_m = len(keys)/2
    root = Node(keys[id_m][:2],keys[id_m][2])
    if id_m != 0:
        recursive_add_Node(root,keys[:id_m],1)
    if id_m+1 != len(keys):
        recursive_add_Node(root,keys[id_m+1:],1)

    q = int(input())
    for i in xrange(q):
        sx, tx, sy, ty = map(int,raw_input().split())
        nums = deque()
        find(root,[sx,sy],[tx,ty],0,nums)
        nums = list(nums)
        nums.sort()
        map(lambda i:sys.stdout.write("{}\n".format(i)), nums)
        print ("")

def find(z,s,t,d,nums):
    if(s[0]<=z.key[0]<=t[0])and(s[1]<=z.key[1]<=t[1]):
        nums.append(z.num)
    d += 1
    xy = d%2
    if (z.left != None)and(z.key[xy]>=s[xy]):
        find(z.left,s,t,d,nums)    
    if (z.right != None)and(z.key[xy]<=t[xy]):
        find(z.right,s,t,d,nums)

def add_Node(root,k,n):
    y = None
    x = root
    d = 0
    xy = 0
    while(x != None):
        d += 1
        xy = d%2
        y = x
        if x.key[xy] > k[xy]:
            x = x.left
        else:
            x = x.right
    
    z = Node(k,n)

    if y.key[xy] > k[xy]:
        y.left = z
    else:
        y.right = z
        
def recursive_add_Node(root,keys,depth):
    keys.sort(key=itemgetter(depth%2))
    id_m = len(keys)/2
    add_Node(root,keys[id_m][:2],keys[id_m][2])
   
    if id_m != 0:
        recursive_add_Node(root,keys[:id_m],depth+1)
    if id_m+1 != len(keys):
        recursive_add_Node(root,keys[id_m+1:],depth+1)

class Node():
    def __init__(self,k,n):
        self.key    = k
        self.num    = n
        self.left   = None
        self.right  = None
    


if __name__ == "__main__":
    main()

Fixed submission:
from sys import stdin,stdout
import math
from operator import itemgetter
from bisect import bisect_left, bisect_right
readline = stdin.readline
 
def main():
    n = int(input())
    keys = map(lambda i: map(int,readline().split())+[i], xrange(n))
    keys.sort()

    root = int(math.sqrt(len(keys)))
 
    low = [x for x, y, i in keys[::root]]
    high = [x for x, y, i in keys[root - 1::root]] + [float('inf')]
 
    keys = [sorted(keys[i:i+root], key=itemgetter(1)) for i in range(0,len(keys),root)]
    keys_y = map(lambda k:[y for x,y,i in k], keys)

    q = int(input())
    for i in xrange(q):
        sx,tx,sy,ty = map(int,readline().split())
        
        nums = []
        for i in range(bisect_left(high, sx), bisect_right(low, tx)):
            y = keys_y[i]
            for j in range(bisect_left(y, sy), bisect_right(y, ty)):
                if sx <= keys[i][j][0] <= tx:
                    nums.append(keys[i][j][2])
        if nums:
            nums.sort()
            map(lambda n:stdout.write("{}\n".format(n)), nums)
        print("")

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from operator import itemgetter
from collections import deque
import sys

def main():
    n = int(input())  
    keys = map(lambda i:(map(int,sys.stdin.readline().split())+[i]),xrange(n))

    keys.sort(key=itemgetter(0))
    id_m = len(keys)/2
    root = Node(keys[id_m][:2],keys[id_m][2])
    if id_m != 0:
        recursive_add_Node(root,keys[:id_m],1)
    if id_m+1 != len(keys):
        recursive_add_Node(root,keys[id_m+1:],1)

    q = int(input())
    for i in xrange(q):
        sx, tx, sy, ty = map(int,raw_input().split())
        nums = deque()
        find(root,[sx,sy],[tx,ty],0,nums)
        nums = list(nums)
        nums.sort()
        map(lambda i:sys.stdout.write("{}\n".format(i)), nums)
        print ("")

def find(z,s,t,d,nums):
    if(s[0]<=z.key[0]<=t[0])and(s[1]<=z.key[1]<=t[1]):
        nums.append(z.num)
    d += 1
    xy = d%2
    if (z.left != None)and(z.key[xy]>=s[xy]):
        find(z.left,s,t,d,nums)    
    if (z.right != None)and(z.key[xy]<=t[xy]):
        find(z.right,s,t,d,nums)

def add_Node(root,k,n):
    y = None
    x = root
    d = 0
    xy = 0
    while(x != None):
        d += 1
        xy = d%2
        y = x
        if x.key[xy] > k[xy]:
            x = x.left
        else:
            x = x.right
    
    z = Node(k,n)

    if y.key[xy] > k[xy]:
        y.left = z
    else:
        y.right = z
        
def recursive_add_Node(root,k,d):
    k.sort(key=itemgetter(d%2))
    len_k = len(k)
    id_m = len_k/2
    add_Node(root,k[id_m][:2],k[id_m][2])
   
    if id_m != 0:
        recursive_add_Node(root,k[:id_m],d+1)
    if id_m+1 != len_k:
        recursive_add_Node(root,k[id_m+1:],d+1)

class Node():
    def __init__(self,k,n):
        self.key   = k
        self.num   = n
        self.left  = None
        self.right = None
    


if __name__ == "__main__":
    main()

Fixed submission:
from sys import stdin,stdout
import math
from operator import itemgetter
from bisect import bisect_left, bisect_right
readline = stdin.readline
 
def main():
    n = int(input())
    keys = map(lambda i: map(int,readline().split())+[i], xrange(n))
    keys.sort()

    root = int(math.sqrt(len(keys)))
 
    low = [x for x, y, i in keys[::root]]
    high = [x for x, y, i in keys[root - 1::root]] + [float('inf')]
 
    keys = [sorted(keys[i:i+root], key=itemgetter(1)) for i in range(0,len(keys),root)]
    keys_y = map(lambda k:[y for x,y,i in k], keys)

    q = int(input())
    for i in xrange(q):
        sx,tx,sy,ty = map(int,readline().split())
        
        nums = []
        for i in range(bisect_left(high, sx), bisect_right(low, tx)):
            y = keys_y[i]
            for j in range(bisect_left(y, sy), bisect_right(y, ty)):
                if sx <= keys[i][j][0] <= tx:
                    nums.append(keys[i][j][2])
        if nums:
            nums.sort()
            map(lambda n:stdout.write("{}\n".format(n)), nums)
        print("")

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Runtime Error):
from operator import itemgetter
from bisect import bisect_left, bisect_right
import sys

def main():
    n = int(sys.stdin.readline())
    keys = map(lambda i:map(int,sys.stdin.readline().split())+[i],xrange(n))
    keys.sort()

    q = int(sys.stdin.readline())
    for i in xrange(q):
        x0,x1,y0,y1 = map(int,sys.stdin.readline().split())
        key_x,key_y,ids = zip(*keys)
        interval = xrange(bisect_left(key_x,x0),bisect_right(key_x,x1))
        key1 = map(lambda j:[key_x[j],key_y[j],ids[j]],interval)
        key1.sort(key=itemgetter(1))
        key_x,key_y,ids = zip(*key1)
        interval = xrange(bisect_left(key_y,y0),bisect_right(key_y,y1))
        ids = map(lambda j:ids[j],interval)
        ids.sort()
        map(lambda x: sys.stdout.write("{}\n".format(x)),ids)
        print ("")

if __name__ == "__main__":
    main()

Fixed submission:
from sys import stdin,stdout
import math
from operator import itemgetter
from bisect import bisect_left, bisect_right
readline = stdin.readline
 
def main():
    n = int(input())
    keys = map(lambda i: map(int,readline().split())+[i], xrange(n))
    keys.sort()

    root = int(math.sqrt(len(keys)))
 
    low = [x for x, y, i in keys[::root]]
    high = [x for x, y, i in keys[root - 1::root]] + [float('inf')]
 
    keys = [sorted(keys[i:i+root], key=itemgetter(1)) for i in range(0,len(keys),root)]
    keys_y = map(lambda k:[y for x,y,i in k], keys)

    q = int(input())
    for i in xrange(q):
        sx,tx,sy,ty = map(int,readline().split())
        
        nums = []
        for i in range(bisect_left(high, sx), bisect_right(low, tx)):
            y = keys_y[i]
            for j in range(bisect_left(y, sy), bisect_right(y, ty)):
                if sx <= keys[i][j][0] <= tx:
                    nums.append(keys[i][j][2])
        if nums:
            nums.sort()
            map(lambda n:stdout.write("{}\n".format(n)), nums)
        print("")

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from operator import itemgetter
from bisect import bisect_left, bisect_right
import sys

def main():
    n = int(sys.stdin.readline())
    keys = map(lambda i:map(int,sys.stdin.readline().split())+[i],xrange(n))
    keys.sort()

    q = int(sys.stdin.readline())
    for i in xrange(q):
        x0,x1,y0,y1 = map(int,sys.stdin.readline().split())
        key_x,key_y,ids = zip(*keys)
        interval = xrange(bisect_left(key_x,x0),bisect_right(key_x,x1))
        if interval:
            key1 = map(lambda j:[key_x[j],key_y[j],ids[j]],interval)
            key1.sort(key=itemgetter(1))
            key_x,key_y,ids = zip(*key1)
            interval = xrange(bisect_left(key_y,y0),bisect_right(key_y,y1))
            if interval:
                ids = map(lambda j:ids[j],interval)
                ids.sort()
                map(lambda x: sys.stdout.write("{}\n".format(x)),ids)
        print ("")

if __name__ == "__main__":
    main()

Fixed submission:
from sys import stdin,stdout
import math
from operator import itemgetter
from bisect import bisect_left, bisect_right
readline = stdin.readline
 
def main():
    n = int(input())
    keys = map(lambda i: map(int,readline().split())+[i], xrange(n))
    keys.sort()

    root = int(math.sqrt(len(keys)))
 
    low = [x for x, y, i in keys[::root]]
    high = [x for x, y, i in keys[root - 1::root]] + [float('inf')]
 
    keys = [sorted(keys[i:i+root], key=itemgetter(1)) for i in range(0,len(keys),root)]
    keys_y = map(lambda k:[y for x,y,i in k], keys)

    q = int(input())
    for i in xrange(q):
        sx,tx,sy,ty = map(int,readline().split())
        
        nums = []
        for i in range(bisect_left(high, sx), bisect_right(low, tx)):
            y = keys_y[i]
            for j in range(bisect_left(y, sy), bisect_right(y, ty)):
                if sx <= keys[i][j][0] <= tx:
                    nums.append(keys[i][j][2])
        if nums:
            nums.sort()
            map(lambda n:stdout.write("{}\n".format(n)), nums)
        print("")

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from operator import itemgetter
from bisect import bisect_left, bisect_right
import sys

def main():
    n = int(sys.stdin.readline())
    keys = map(lambda i:map(int,sys.stdin.readline().split())+[i],xrange(n))
    keys.sort()

    q = int(sys.stdin.readline())
    for i in xrange(q):
        x0,x1,y0,y1 = map(int,sys.stdin.readline().split())
        key_x,key_y,ids = zip(*keys)
        interval = xrange(bisect_left(key_x,x0),bisect_right(key_x,x1))
        if interval:
            key1 = zip(key_y,ids)
            key1 = map(lambda j:key1[j],interval)
            key1.sort()
            key_y,ids = zip(*key1)
            interval = xrange(bisect_left(key_y,y0),bisect_right(key_y,y1))
            if interval:
                ids = map(lambda j:ids[j],interval)
                ids.sort()
                map(lambda x: sys.stdout.write("{}\n".format(x)),ids)
        print ("")

if __name__ == "__main__":
    main()

Fixed submission:
from sys import stdin,stdout
import math
from operator import itemgetter
from bisect import bisect_left, bisect_right
readline = stdin.readline
 
def main():
    n = int(input())
    keys = map(lambda i: map(int,readline().split())+[i], xrange(n))
    keys.sort()

    root = int(math.sqrt(len(keys)))
 
    low = [x for x, y, i in keys[::root]]
    high = [x for x, y, i in keys[root - 1::root]] + [float('inf')]
 
    keys = [sorted(keys[i:i+root], key=itemgetter(1)) for i in range(0,len(keys),root)]
    keys_y = map(lambda k:[y for x,y,i in k], keys)

    q = int(input())
    for i in xrange(q):
        sx,tx,sy,ty = map(int,readline().split())
        
        nums = []
        for i in range(bisect_left(high, sx), bisect_right(low, tx)):
            y = keys_y[i]
            for j in range(bisect_left(y, sy), bisect_right(y, ty)):
                if sx <= keys[i][j][0] <= tx:
                    nums.append(keys[i][j][2])
        if nums:
            nums.sort()
            map(lambda n:stdout.write("{}\n".format(n)), nums)
        print("")

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from operator import itemgetter
import sys

def main():
    n = int(input())  
    keys = map(lambda i:(map(int,sys.stdin.readline().split())+[i]),xrange(n))
    keys.sort()
    id_m = len(keys)/2
    root = Node(keys[id_m][:2],keys[id_m][2])
    if id_m != 0:
        recursive_add_Node(root,keys[:id_m],1)
    if id_m+1 != len(keys):
        recursive_add_Node(root,keys[id_m+1:],1)

    q = int(input())
    for i in xrange(q):
        sx, tx, sy, ty = map(int,sys.stdin.readline().split())
        nums = []
        find(root,[sx,sy],[tx,ty],0,nums)
        nums.sort()
        map(lambda i:sys.stdout.write("{}\n".format(i)), nums)
        print ("")

def find(z,s,t,d,nums):
    if(s[0]<=z.key[0]<=t[0])and(s[1]<=z.key[1]<=t[1]):
        nums.append(z.num)
    d += 1
    xy = d%2
    if (z.left != None)and(z.key[xy]>=s[xy]):
        find(z.left,s,t,d,nums)
    if (z.right != None)and(z.key[xy]<=t[xy]):
        find(z.right,s,t,d,nums)

def add_Node(root,k,n):
    y = None
    x = root
    d = 0
    xy = 0
    while(x != None):
        d += 1
        xy = d%2
        y = x
        if x.key[xy] > k[xy]:
            x = x.left
        else:
            x = x.right
    
    z = Node(k,n)

    if y.key[xy] > k[xy]:
        y.left = z
    else:
        y.right = z
        
def recursive_add_Node(root,k,d):
    k.sort(key=itemgetter(d%2))
    len_k = len(k)
    id_m = len_k/2
    add_Node(root,k[id_m][:2],k[id_m][2])
   
    if id_m != 0:
        recursive_add_Node(root,k[:id_m],d+1)
    if id_m+1 != len_k:
        recursive_add_Node(root,k[id_m+1:],d+1)

class Node():
    def __init__(self,k,n):
        self.key   = k
        self.num   = n
        self.left  = None
        self.right = None


if __name__ == "__main__":
    main()

Fixed submission:
from sys import stdin,stdout
import math
from operator import itemgetter
from bisect import bisect_left, bisect_right
readline = stdin.readline
 
def main():
    n = int(input())
    keys = map(lambda i: map(int,readline().split())+[i], xrange(n))
    keys.sort()

    root = int(math.sqrt(len(keys)))
 
    low = [x for x, y, i in keys[::root]]
    high = [x for x, y, i in keys[root - 1::root]] + [float('inf')]
 
    keys = [sorted(keys[i:i+root], key=itemgetter(1)) for i in range(0,len(keys),root)]
    keys_y = map(lambda k:[y for x,y,i in k], keys)

    q = int(input())
    for i in xrange(q):
        sx,tx,sy,ty = map(int,readline().split())
        
        nums = []
        for i in range(bisect_left(high, sx), bisect_right(low, tx)):
            y = keys_y[i]
            for j in range(bisect_left(y, sy), bisect_right(y, ty)):
                if sx <= keys[i][j][0] <= tx:
                    nums.append(keys[i][j][2])
        if nums:
            nums.sort()
            map(lambda n:stdout.write("{}\n".format(n)), nums)
        print("")

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from operator import itemgetter
import sys

def main():
    n = int(input())
    keys = map(lambda i:(map(int,sys.stdin.readline().split())+[i]),xrange(n))
    keys.sort()
    id_m = len(keys)/2
    root = Node(keys[id_m][0],keys[id_m][1],keys[id_m][2])
    if id_m != 0:
        recursive_add_Node(root,keys[:id_m],1)
    if id_m+1 != len(keys):
        recursive_add_Node(root,keys[id_m+1:],1)

    q = int(input())
    for i in xrange(q):
        sx, tx, sy, ty = map(int,sys.stdin.readline().split())
        nums = []
        find(root,sx,sy,tx,ty,0,nums)
        nums.sort()
        map(lambda n:sys.stdout.write("{}\n".format(n)), nums)
        print ("")

def find(z,sx,sy,tx,ty,d,nums):
    if(sx<=z.key_x<=tx)and(sy<=z.key_y<=ty):
        nums.append(z.num)
    d += 1
    if d%2:
        if (z.left != None)and(z.key_y>=sy):
            find(z.left,sx,sy,tx,ty,d,nums)
        if (z.right != None)and(z.key_y<=ty):
            find(z.right,sx,sy,tx,ty,d,nums)
    else:
        if (z.left != None)and(z.key_x>=sx):
            find(z.left,sx,sy,tx,ty,d,nums)
        if (z.right != None)and(z.key_x<=tx):
            find(z.right,sx,sy,tx,ty,d,nums)

        

def add_Node(root,kx,ky,n):
    y = None
    x = root
    d = 0
    xy = 0
    while(x != None):
        d += 1
        xy = d%2
        y = x
        if xy == 0:
            if x.key_x > kx:
                x = x.left
            else:
                x = x.right
        else:
            if x.key_y > ky:
                x = x.left
            else:
                x = x.right
    
    z = Node(kx,ky,n)

    if xy == 0:
        if y.key_x > kx:
            y.left = z
        else:
            y.right = z
    else:
        if y.key_y > ky:
            y.left = z
        else:
            y.right = z

def recursive_add_Node(root,k,d):
    k.sort(key=itemgetter(d%2))
    len_k = len(k)
    id_m = len_k/2
    add_Node(root,k[id_m][0],k[id_m][1],k[id_m][2])
    if id_m != 0:
        recursive_add_Node(root,k[:id_m],d+1)
    if id_m+1 != len_k:
        recursive_add_Node(root,k[id_m+1:],d+1)

class Node():
    def __init__(self,x,y,n):
        self.key_x = x
        self.key_y = y
        self.num   = n
        self.left  = None
        self.right = None


if __name__ == "__main__":
    main()

Fixed submission:
from sys import stdin,stdout
import math
from operator import itemgetter
from bisect import bisect_left, bisect_right
readline = stdin.readline
 
def main():
    n = int(input())
    keys = map(lambda i: map(int,readline().split())+[i], xrange(n))
    keys.sort()

    root = int(math.sqrt(len(keys)))
 
    low = [x for x, y, i in keys[::root]]
    high = [x for x, y, i in keys[root - 1::root]] + [float('inf')]
 
    keys = [sorted(keys[i:i+root], key=itemgetter(1)) for i in range(0,len(keys),root)]
    keys_y = map(lambda k:[y for x,y,i in k], keys)

    q = int(input())
    for i in xrange(q):
        sx,tx,sy,ty = map(int,readline().split())
        
        nums = []
        for i in range(bisect_left(high, sx), bisect_right(low, tx)):
            y = keys_y[i]
            for j in range(bisect_left(y, sy), bisect_right(y, ty)):
                if sx <= keys[i][j][0] <= tx:
                    nums.append(keys[i][j][2])
        if nums:
            nums.sort()
            map(lambda n:stdout.write("{}\n".format(n)), nums)
        print("")

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from operator import itemgetter
import sys

def main():
    n = int(input())
    keys = map(lambda i:(map(int,sys.stdin.readline().split())),xrange(n))
    kx,ky = zip(*keys)
    keys = zip(kx,ky,xrange(n))
    keys.sort()
    id_m = len(keys)/2
    root = Node(keys[id_m][0],keys[id_m][1],keys[id_m][2])
    if id_m != 0:
        recursive_add_Node(root,keys[:id_m],1)
    if id_m+1 != len(keys):
        recursive_add_Node(root,keys[id_m+1:],1)

    q = int(input())
    for i in xrange(q):
        sx, tx, sy, ty = map(int,sys.stdin.readline().split())
        nums = []
        find(root,sx,sy,tx,ty,0,nums)
        nums.sort()
        map(lambda n:sys.stdout.write("{}\n".format(n)), nums)
        print ("")

def find(z,sx,sy,tx,ty,d,nums):
    if(sx<=z.key_x<=tx)and(sy<=z.key_y<=ty):
        nums.append(z.num)
    d += 1
    if d%2:
        if (z.left != None)and(z.key_y>=sy):
            find(z.left,sx,sy,tx,ty,d,nums)
        if (z.right != None)and(z.key_y<=ty):
            find(z.right,sx,sy,tx,ty,d,nums)
    else:
        if (z.left != None)and(z.key_x>=sx):
            find(z.left,sx,sy,tx,ty,d,nums)
        if (z.right != None)and(z.key_x<=tx):
            find(z.right,sx,sy,tx,ty,d,nums)

        

def add_Node(root,kx,ky,n):
    y = None
    x = root
    d = 0
    xy = 0
    while(x != None):
        d += 1
        xy = d%2
        y = x
        if xy == 0:
            if x.key_x > kx:
                x = x.left
            else:
                x = x.right
        else:
            if x.key_y > ky:
                x = x.left
            else:
                x = x.right
    
    z = Node(kx,ky,n)

    if xy == 0:
        if y.key_x > kx:
            y.left = z
        else:
            y.right = z
    else:
        if y.key_y > ky:
            y.left = z
        else:
            y.right = z

def recursive_add_Node(root,k,d):
    k.sort(key=itemgetter(d%2))
    len_k = len(k)
    id_m = len_k/2
    add_Node(root,k[id_m][0],k[id_m][1],k[id_m][2])
    if id_m != 0:
        recursive_add_Node(root,k[:id_m],d+1)
    if id_m+1 != len_k:
        recursive_add_Node(root,k[id_m+1:],d+1)

class Node():
    def __init__(self,x,y,n):
        self.key_x = x
        self.key_y = y
        self.num   = n
        self.left  = None
        self.right = None


if __name__ == "__main__":
    main()

Fixed submission:
from sys import stdin,stdout
import math
from operator import itemgetter
from bisect import bisect_left, bisect_right
readline = stdin.readline
 
def main():
    n = int(input())
    keys = map(lambda i: map(int,readline().split())+[i], xrange(n))
    keys.sort()

    root = int(math.sqrt(len(keys)))
 
    low = [x for x, y, i in keys[::root]]
    high = [x for x, y, i in keys[root - 1::root]] + [float('inf')]
 
    keys = [sorted(keys[i:i+root], key=itemgetter(1)) for i in range(0,len(keys),root)]
    keys_y = map(lambda k:[y for x,y,i in k], keys)

    q = int(input())
    for i in xrange(q):
        sx,tx,sy,ty = map(int,readline().split())
        
        nums = []
        for i in range(bisect_left(high, sx), bisect_right(low, tx)):
            y = keys_y[i]
            for j in range(bisect_left(y, sy), bisect_right(y, ty)):
                if sx <= keys[i][j][0] <= tx:
                    nums.append(keys[i][j][2])
        if nums:
            nums.sort()
            map(lambda n:stdout.write("{}\n".format(n)), nums)
        print("")

if __name__ == "__main__":
    main()