Last active
April 30, 2018 06:49
-
-
Save jakab922/45e9b321b5d21e6180f9eb1bed20fa87 to your computer and use it in GitHub Desktop.
Dynamic line intersection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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