Buggy python submission (verdict: Time Limit Exceeded):

import sys
import collections
# import math
# import string
# import bisect

def main():
    n, *parts = (int(x) for x in sys.stdin.read().split())
    uppers, middles, lowers = (parts[n*i:n*(i+1)] for i in range(3))

    count = 0
    for a, ca in collections.Counter(uppers).items():
        for b, cb in collections.Counter((b for b in middles if b > a)).items():
            count += ca * cb * len([c for c in lowers if c > b])

    print(count)

if __name__ == "__main__":
    main()


Fixed submission:

import sys
# import collections
# import math
# import string
import bisect

def main():
    n, *parts = (int(x) for x in sys.stdin.read().split())
    uppers, middles, lowers = (parts[n*i:n*(i+1)] for i in range(3))
    uppers.sort()
    lowers.sort()

    count = 0
    for j in middles:
        i = bisect.bisect_left(uppers, j)
        k = len(lowers) - bisect.bisect_right(lowers, j)
        count += i * k
        
    print(count)

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import collections
# import math
# import string
# import bisect

def main():
    n, *parts = (int(x) for x in sys.stdin.read().split())
    uppers, middles, lowers = (parts[n*i:n*(i+1)] for i in range(3))

    count = 0
    for a, ca in collections.Counter(uppers).items():
        for b, cb in collections.Counter((b for b in middles if b > a)).items():
            count += ca * cb * sum(collections.Counter((c for c in lowers if c > b)).values())

    print(count)

if __name__ == "__main__":
    main()


Fixed submission:

import sys
# import collections
# import math
# import string
import bisect

def main():
    n, *parts = (int(x) for x in sys.stdin.read().split())
    uppers, middles, lowers = (parts[n*i:n*(i+1)] for i in range(3))
    uppers.sort()
    lowers.sort()

    count = 0
    for j in middles:
        i = bisect.bisect_left(uppers, j)
        k = len(lowers) - bisect.bisect_right(lowers, j)
        count += i * k
        
    print(count)

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):

import sys
import collections
# import math
# import string
# import bisect

def main():
    n, *parts = (int(x) for x in sys.stdin.read().split())
    uppers, middles, lowers = (parts[n*i:n*(i+1)] for i in range(3))

    count = 0
    for a, ca in collections.Counter(uppers).items():
        for b, cb in collections.Counter(middles).items():
            if a >= b: continue
            else:
                for c, cc in collections.Counter(lowers).items():
                    if b >= c: continue
                    else: count += ca * cb * cc
    print(count)

if __name__ == "__main__":
    main()


Fixed submission:

import sys
# import collections
# import math
# import string
import bisect

def main():
    n, *parts = (int(x) for x in sys.stdin.read().split())
    uppers, middles, lowers = (parts[n*i:n*(i+1)] for i in range(3))
    uppers.sort()
    lowers.sort()

    count = 0
    for j in middles:
        i = bisect.bisect_left(uppers, j)
        k = len(lowers) - bisect.bisect_right(lowers, j)
        count += i * k
        
    print(count)

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):

import sys
# import collections
# import math
# import string
# import bisect

def main():
    n, *parts = (int(x) for x in sys.stdin.read().split())
    uppers, middles, lowers = (sorted(parts[n*i:n*(i+1)], reverse=1) for i in range(3))

    count = 0
    for a in uppers:
        for b in middles:
            if a >= b: break
            else:
                for c in lowers:
                    if b >= c: break
                    else: count += 1
    print(count)

if __name__ == "__main__":
    main()


Fixed submission:

import sys
# import collections
# import math
# import string
import bisect

def main():
    n, *parts = (int(x) for x in sys.stdin.read().split())
    uppers, middles, lowers = (parts[n*i:n*(i+1)] for i in range(3))
    uppers.sort()
    lowers.sort()

    count = 0
    for j in middles:
        i = bisect.bisect_left(uppers, j)
        k = len(lowers) - bisect.bisect_right(lowers, j)
        count += i * k
        
    print(count)

if __name__ == "__main__":
    main()