sweeneyde

Untitled

Jan 3rd, 2021
1,122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.24 KB | None | 0 0
  1.  
  2. # from sys import stdin as infile
  3. import io
  4. infile = io.StringIO("""\
  5. 4 2
  6. 1 0 0 1
  7. 1 2 3 4
  8. 5 6 7 8
  9. 1 3 3 1
  10. 5 5 1 2
  11. 5 5 3 3
  12. """)
  13.  
  14. N, M = map(int, infile.readline().split())
  15. data = [tuple(map(int, infile.readline().split()))
  16.         for i in range(N)]
  17. queries = [tuple(map(int, infile.readline().split()))
  18.            for i in range(M)]
  19.  
  20. MOD = 10**9 + 7
  21.  
  22. def product(M1, M2):
  23.     a11, a12, a21, a22 = M1
  24.     b11, b12, b21, b22 = M2
  25.     return (
  26.         (a11*b11 + a12*b21) % MOD,
  27.         (a11*b12 + a12*b22) % MOD,
  28.         (a21*b11 + a22*b21) % MOD,
  29.         (a21*b12 + a22*b22) % MOD,
  30.     )
  31.  
  32. tree = [None]*len(data) + data
  33. rev_tree = [None]*len(data) + data
  34. for i in range(len(data)-1, 0, -1):
  35.     tree[i] = product(tree[i<<1],
  36.                       tree[(i<<1) | 1])
  37.     rev_tree[i] = product(rev_tree[(i<<1) | 1],
  38.                           rev_tree[i<<1],)
  39.  
  40. ID = (1, 0, 0, 1)
  41.  
  42. def forward_query(a, b):
  43.     assert a <= b
  44.     left = right = ID
  45.     a += len(data)
  46.     b += len(data) + 1
  47.     while a < b:
  48.         if a & 1:
  49.             left = product(left, tree[a])
  50.             a += 1
  51.         if b & 1:
  52.             b -= 1
  53.             right = product(tree[b], right)
  54.         a >>= 1
  55.         b >>= 1
  56.     return product(left, right)
  57.  
  58. def reverse_query(a, b):
  59.     assert a >= b
  60.     a, b = b, a
  61.  
  62.     left = right = ID
  63.     while a < b:
  64.         if a & 1:
  65.             left = product(tree[a], left)
  66.             a += 1
  67.         if b & 1:
  68.             b -= 1
  69.             right = product(right, tree[b])
  70.         a >>= 1
  71.         b >>= 1
  72.     return product(right, left)
  73.  
  74. def query(a, b):
  75.     a -= 1
  76.     b -= 1
  77.     if a <= b:
  78.         return forward_query(a, b)
  79.     else:
  80.         return reverse_query(a, b)
  81.  
  82. output = []
  83.  
  84. for K, V, a, b in queries:
  85.     a11, a12, a21, a22 = query(a, b)
  86.     KK = (a11*K + a12*V) % MOD
  87.     VV = (a21*K + a22*V) % MOD
  88.     output.append(f"{KK} {VV}")
  89.  
  90.  
  91. M1, M2, M3, M4 = data
  92. p = product
  93. q = query
  94. assert p(M1, M2) == q(1, 2)
  95. assert p(M3, M4) == q(3, 4)
  96. assert p(M2, M3) == q(2, 3)
  97. assert p(M2, M1) == q(2, 1)
  98. assert p(p(M1, M2), M3) == q(1, 3)
  99. assert p(p(M3, M2), M1) == q(3, 1)
  100. assert p(p(M1, M2), p(M3, M4)) == q(1, 4)
  101. assert p(p(M4, M3), p(M2, M1)) == q(4, 1)
  102.  
  103.  
  104.  
  105. print('\n'.join(output))
Advertisement
Add Comment
Please, Sign In to add comment