Advertisement
crap_the_coder

Angry Cyborg

Dec 29th, 2020
865
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.90 KB | None | 0 0
  1. from collections import defaultdict
  2.  
  3. for _ in range(int(input())):
  4.     n, q = map(int, input().split())
  5.  
  6.     # defaultdict(list) assigns the value as an empty list for each key by default
  7.     # ind stores a unique value for each range
  8.     ind = defaultdict(list)
  9.  
  10.     # Unique value for each range
  11.     unique_val = 1
  12.  
  13.     for _ in range(q):
  14.         l, r = map(int, input().split())
  15.  
  16.         # Converting to 0-based indexing
  17.         l -= 1
  18.         r -= 1
  19.  
  20.         # Positive denotes beginning
  21.         ind[l].append(unique_val)
  22.  
  23.         # Negative denotes ending
  24.         # We use r+1 because r is inclusive in the range
  25.         ind[r+1].append(-unique_val)
  26.  
  27.         # Next value to make it unique
  28.         unique_val += 1
  29.  
  30.     no_sts = 0  # Number of statues that will be destroyed
  31.     ispeed = 0  # Speed at which the number of statues destroyed increases
  32.  
  33.     # First occurrence of a range (stores l value of a range)
  34.     occ = {}
  35.  
  36.     for i in range(n):
  37.         # For each "endpoint" (which is l or r+1)
  38.         for j in ind[i]:
  39.  
  40.             # If it's a new range the speed with which the number of statues is destroyed will increase
  41.             if j > 0:
  42.                 occ[j] = i
  43.                 ispeed += 1
  44.  
  45.             # If it's the end of a range
  46.             # The speed with which the number of statues is destroyed will decrease
  47.  
  48.             # The current amount of statues which are being destroyed at each index will decrease by how much it has contributed
  49.             # Which is equal to r - l + 1, but since we set the endpoint to r+1, we should make it r - l
  50.             # Here, (r = i) and (l = [first occurrence of -j] = occ[-j])
  51.             elif j < 0:
  52.                 ispeed -= 1
  53.                 no_sts -= i - occ[-j]
  54.  
  55.         # The amount of statues destroyed at each index increases by the speed
  56.         no_sts += ispeed
  57.  
  58.         print(no_sts, end=' ')
  59.  
  60.     print()
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement