Skip to content

Instantly share code, notes, and snippets.

@jakab922
Last active April 30, 2018 06:49
Show Gist options
  • Save jakab922/45e9b321b5d21e6180f9eb1bed20fa87 to your computer and use it in GitHub Desktop.
Save jakab922/45e9b321b5d21e6180f9eb1bed20fa87 to your computer and use it in GitHub Desktop.
Dynamic line intersection
from math import sqrt, ceil
LIMIT = 10 ** 5
n = int(raw_input().strip())
SLIMIT = int(ceil(sqrt(LIMIT)))
small = [[0] * (SLIMIT + 1) for _ in xrange(SLIMIT + 1)]
big = [0 for _ in xrange(LIMIT + 1)]
for _ in xrange(n):
query = raw_input().strip().split()
what = query[0]
if what == "?":
q = int(query[1])
total = big[q]
for k in xrange(1, SLIMIT + 1):
b = q % k
total += small[b][k]
print total
else:
mod = 1 if what == "+" else -1
k, b = map(int, query[1:])
if k <= SLIMIT:
small[b % k][k] += mod
else:
x = int(ceil(-b / float(k)))
while k * x + b <= LIMIT:
big[k * x + b] += mod
x += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment