import random
import collections
import math
random.seed("something")

class TestGen:
    def __init__(self):
        self.test_number = 0

    def generate_samples(self):
        #1 test
        self.print_sample(4, [1, 2, 3, 4], 2, [(1, 1), (1, 4)])

    def generate_min_tests(self):
        #5 tests
        self.print_test(1, [1], 1, [(1, 1)])
        self.print_test(1, [100], 1, [(1, 1)])
        self.print_test(2, [1, 2], 3, [(1, 1), (2, 2), (1, 2)])
        self.print_test(2, [1, 1], 3, [(1, 1), (2, 2), (1, 2)])
        self.print_test(2, [2, 3], 3, [(1, 1), (2, 2), (1, 2)])
                                                                            
    def generate_tricky_tests(self, maxn, maxq, maxx):
        #0 tests
        pass

    def generate_random_test(self, maxn, maxq, maxx):
        n = maxn
        q = maxq

        a = [1 for i in range(n)]
        cnt = random.randint(0, int(math.sqrt(n)))
        for i in range(cnt):
            a[random.randint(0, n - 1)] = random.randint(2, maxx)

        qs = [(-1, -1) for i in range(q)]  
        for i in range(q):
            l = random.randint(1, n)
            if random.randint(1, 2) == 1:
                r = random.randint(l, n)
            else:
                r = random.randint(l, min(n, l + 500))
            qs[i] = (l, r)

        self.print_test(n, a, q, qs)

    def generate_max_test(self, maxn, maxq):
        n = maxn
        q = maxq

        a = []
        for i in range(n):
            a.append(1)
        qs = []
        for i in range(q):
            qs.append((random.randint(1, 10), random.randint(n - 10, n)))
        self.print_test(n, a, q, qs)

    #def generate_small_test(self, maxn, maxq, maxx):
    #def generate_medium_test(self, maxn, maxq, maxx):

    def print_sample(self, n, a, q, qs):
        self.test_number += 1
        test_name = "../preliminary/" + "{0:0=2d}".format(self.test_number)
        test_file = open(test_name, "w")
        print(n, file=test_file)
        print(' '.join(str(element) for element in a), file=test_file)
        print(q, file=test_file)
        print('\n'.join(' '.join(str(idx) for idx in query) for query in qs), file=test_file)
        test_file.close()

    def print_test(self, n, a, q, qs):
        self.test_number += 1
        test_name = "../tests/" + "{0:0=2d}".format(self.test_number)
        test_file = open(test_name, "w")
        print(n, file=test_file)
        print(' '.join(str(element) for element in a), file=test_file)
        print(q, file=test_file)
        print('\n'.join(' '.join(str(idx) for idx in query) for query in qs), file=test_file)
        test_file.close()

    def generate_all_tests(self):
        maxn = 10**5
        maxq = 10**4
        maxx = 10**9
        self.generate_samples()                       #1 test
        self.test_number = 0                 

        #First group of tests (01-10)
        self.generate_min_tests()                                   #5 tests
        #self.generate_tricky_tests(10, 10**9, 20)                  #0 tests
        for i in range(5):                                          #5 tests
            n = random.randint(3, 4)
            self.generate_random_test(n, n * (n - 1) // 2, maxx)

        #Second group of tests (11-20)
        #self.generate_tricky_tests(500, 10**9, 500)   #0 tests
        for i in range(5):                             #5 tests
            n = random.randint(500, 1000)
            self.generate_random_test(n, 1000, maxx)
        for i in range(5):                             #5 tests
            self.generate_random_test(1000, 1000, maxx)
        
        #Third group of tests (21-25)
        #self.generate_tricky_tests(maxn, maxx, maxk)  #0 tests
        #self.generate_max_tests(maxn, maxx, maxk)     #0 tests
        for i in range(5):                             #5 tests
            self.generate_random_test(maxn, maxq, maxx)
        self.generate_max_test(maxn, maxq)

import os, shutil
if os.path.exists('../tests'):
    print('Directory tests is already exists')
    shutil.rmtree('../tests', ignore_errors=False, onerror=None)
if os.path.exists('../preliminary'):
    print('Directory preliminary is already exists')
    shutil.rmtree('../preliminary', ignore_errors=False, onerror=None)
try:
    os.makedirs('../preliminary')
    os.makedirs('../tests')
except OSError:
    pass

import time
start = time.time()
writer = TestGen()
writer.generate_all_tests()
finish = time.time()
print('All tests were generated. Elapsed time: ', finish - start)
