message stringlengths 2 44.5k | message_type stringclasses 2 values | message_id int64 0 1 | conversation_id int64 42 109k | cluster float64 5 5 | __index_level_0__ int64 84 217k |
|---|---|---|---|---|---|
Provide a correct Python 3 solution for this coding contest problem.
Given are N positive integers A_1,...,A_N.
Consider positive integers B_1, ..., B_N that satisfy the following condition.
Condition: For any i, j such that 1 \leq i < j \leq N, A_i B_i = A_j B_j holds.
Find the minimum possible value of B_1 + ... + B_N for such B_1,...,B_N.
Since the answer can be enormous, print the sum modulo (10^9 +7).
Constraints
* 1 \leq N \leq 10^4
* 1 \leq A_i \leq 10^6
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 ... A_N
Output
Print the minimum possible value of B_1 + ... + B_N for B_1,...,B_N that satisfy the condition, modulo (10^9 +7).
Examples
Input
3
2 3 4
Output
13
Input
5
12 12 12 12 12
Output
5
Input
3
1000000 999999 999998
Output
996989508 | instruction | 0 | 108,428 | 5 | 216,856 |
"Correct Solution:
```
# from math import gcd
from fractions import gcd
n = int(input())
a = list(map(int,input().split()))
ans = 1
for i in a:
ans = ans * i // gcd(ans, i)
mod = 10**9+7
print(sum(map(lambda x: ans // x, a)) % mod)
``` | output | 1 | 108,428 | 5 | 216,857 |
Provide a correct Python 3 solution for this coding contest problem.
Given are N positive integers A_1,...,A_N.
Consider positive integers B_1, ..., B_N that satisfy the following condition.
Condition: For any i, j such that 1 \leq i < j \leq N, A_i B_i = A_j B_j holds.
Find the minimum possible value of B_1 + ... + B_N for such B_1,...,B_N.
Since the answer can be enormous, print the sum modulo (10^9 +7).
Constraints
* 1 \leq N \leq 10^4
* 1 \leq A_i \leq 10^6
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 ... A_N
Output
Print the minimum possible value of B_1 + ... + B_N for B_1,...,B_N that satisfy the condition, modulo (10^9 +7).
Examples
Input
3
2 3 4
Output
13
Input
5
12 12 12 12 12
Output
5
Input
3
1000000 999999 999998
Output
996989508 | instruction | 0 | 108,429 | 5 | 216,858 |
"Correct Solution:
```
from fractions import *
n,*a=map(int,open(0).read().split())
mod=10**9+7
b=a[0]
for i in a:
b=b*i//gcd(b,i)
print(sum(b//i for i in a)%mod)
``` | output | 1 | 108,429 | 5 | 216,859 |
Provide a correct Python 3 solution for this coding contest problem.
Given are N positive integers A_1,...,A_N.
Consider positive integers B_1, ..., B_N that satisfy the following condition.
Condition: For any i, j such that 1 \leq i < j \leq N, A_i B_i = A_j B_j holds.
Find the minimum possible value of B_1 + ... + B_N for such B_1,...,B_N.
Since the answer can be enormous, print the sum modulo (10^9 +7).
Constraints
* 1 \leq N \leq 10^4
* 1 \leq A_i \leq 10^6
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 ... A_N
Output
Print the minimum possible value of B_1 + ... + B_N for B_1,...,B_N that satisfy the condition, modulo (10^9 +7).
Examples
Input
3
2 3 4
Output
13
Input
5
12 12 12 12 12
Output
5
Input
3
1000000 999999 999998
Output
996989508 | instruction | 0 | 108,430 | 5 | 216,860 |
"Correct Solution:
```
from fractions import gcd
from functools import reduce
N = int(input())
A = list(map(int, input().split()))
m = 1000000007
lcm = reduce(lambda a, b: a * b // gcd(a, b), A)
print(sum(lcm // a for a in A) % m)
``` | output | 1 | 108,430 | 5 | 216,861 |
Provide a correct Python 3 solution for this coding contest problem.
Given are N positive integers A_1,...,A_N.
Consider positive integers B_1, ..., B_N that satisfy the following condition.
Condition: For any i, j such that 1 \leq i < j \leq N, A_i B_i = A_j B_j holds.
Find the minimum possible value of B_1 + ... + B_N for such B_1,...,B_N.
Since the answer can be enormous, print the sum modulo (10^9 +7).
Constraints
* 1 \leq N \leq 10^4
* 1 \leq A_i \leq 10^6
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 ... A_N
Output
Print the minimum possible value of B_1 + ... + B_N for B_1,...,B_N that satisfy the condition, modulo (10^9 +7).
Examples
Input
3
2 3 4
Output
13
Input
5
12 12 12 12 12
Output
5
Input
3
1000000 999999 999998
Output
996989508 | instruction | 0 | 108,431 | 5 | 216,862 |
"Correct Solution:
```
import fractions
n=int(input())
l=list(map(int,input().split()))
b=0
c=0
for i in range(n):
if (i==0):
b=l[i]
else:
b=b*l[i]//fractions.gcd(l[i],b)
for i in range(n):
c+=b//l[i]
print(c%1000000007)
``` | output | 1 | 108,431 | 5 | 216,863 |
Provide a correct Python 3 solution for this coding contest problem.
Given are N positive integers A_1,...,A_N.
Consider positive integers B_1, ..., B_N that satisfy the following condition.
Condition: For any i, j such that 1 \leq i < j \leq N, A_i B_i = A_j B_j holds.
Find the minimum possible value of B_1 + ... + B_N for such B_1,...,B_N.
Since the answer can be enormous, print the sum modulo (10^9 +7).
Constraints
* 1 \leq N \leq 10^4
* 1 \leq A_i \leq 10^6
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 ... A_N
Output
Print the minimum possible value of B_1 + ... + B_N for B_1,...,B_N that satisfy the condition, modulo (10^9 +7).
Examples
Input
3
2 3 4
Output
13
Input
5
12 12 12 12 12
Output
5
Input
3
1000000 999999 999998
Output
996989508 | instruction | 0 | 108,432 | 5 | 216,864 |
"Correct Solution:
```
from fractions import gcd
MOD = 10**9+7
N = int(input())
A = list(map(int, input().split()))
lcm = 1
for a in A:
lcm *= a//gcd(lcm, a)
print(sum(lcm//a for a in A)%MOD)
``` | output | 1 | 108,432 | 5 | 216,865 |
Provide a correct Python 3 solution for this coding contest problem.
Given are N positive integers A_1,...,A_N.
Consider positive integers B_1, ..., B_N that satisfy the following condition.
Condition: For any i, j such that 1 \leq i < j \leq N, A_i B_i = A_j B_j holds.
Find the minimum possible value of B_1 + ... + B_N for such B_1,...,B_N.
Since the answer can be enormous, print the sum modulo (10^9 +7).
Constraints
* 1 \leq N \leq 10^4
* 1 \leq A_i \leq 10^6
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 ... A_N
Output
Print the minimum possible value of B_1 + ... + B_N for B_1,...,B_N that satisfy the condition, modulo (10^9 +7).
Examples
Input
3
2 3 4
Output
13
Input
5
12 12 12 12 12
Output
5
Input
3
1000000 999999 999998
Output
996989508 | instruction | 0 | 108,433 | 5 | 216,866 |
"Correct Solution:
```
import fractions
def lcm(a, b):
return a*b // fractions.gcd(a,b)
N = int(input())
nums = list(map(int, input().split()))
num = 1
for i in nums:
num = lcm(num, i)
print(sum([num // i for i in nums]) % (10 ** 9 + 7))
``` | output | 1 | 108,433 | 5 | 216,867 |
Provide a correct Python 3 solution for this coding contest problem.
Given are N positive integers A_1,...,A_N.
Consider positive integers B_1, ..., B_N that satisfy the following condition.
Condition: For any i, j such that 1 \leq i < j \leq N, A_i B_i = A_j B_j holds.
Find the minimum possible value of B_1 + ... + B_N for such B_1,...,B_N.
Since the answer can be enormous, print the sum modulo (10^9 +7).
Constraints
* 1 \leq N \leq 10^4
* 1 \leq A_i \leq 10^6
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 ... A_N
Output
Print the minimum possible value of B_1 + ... + B_N for B_1,...,B_N that satisfy the condition, modulo (10^9 +7).
Examples
Input
3
2 3 4
Output
13
Input
5
12 12 12 12 12
Output
5
Input
3
1000000 999999 999998
Output
996989508 | instruction | 0 | 108,434 | 5 | 216,868 |
"Correct Solution:
```
from functools import reduce
from fractions import gcd
n=int(input())
A=list(map(int,input().split()))
lc=reduce(lambda a,b: a*b//gcd(a,b),A)
b=[lc//i for i in A]
print(sum(b)%(1000*1000000+7))
``` | output | 1 | 108,434 | 5 | 216,869 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Given are N positive integers A_1,...,A_N.
Consider positive integers B_1, ..., B_N that satisfy the following condition.
Condition: For any i, j such that 1 \leq i < j \leq N, A_i B_i = A_j B_j holds.
Find the minimum possible value of B_1 + ... + B_N for such B_1,...,B_N.
Since the answer can be enormous, print the sum modulo (10^9 +7).
Constraints
* 1 \leq N \leq 10^4
* 1 \leq A_i \leq 10^6
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 ... A_N
Output
Print the minimum possible value of B_1 + ... + B_N for B_1,...,B_N that satisfy the condition, modulo (10^9 +7).
Examples
Input
3
2 3 4
Output
13
Input
5
12 12 12 12 12
Output
5
Input
3
1000000 999999 999998
Output
996989508
Submitted Solution:
```
N = int(input())
A = list(map(int, input().split()))
mod = 10 ** 9 + 7
from fractions import gcd
L = A[0]
for i in range(1,N):
L=(L*A[i])//gcd(L,A[i])
ans = 0
for i in range(N):
ans += (L // A[i])
print(ans % mod)
``` | instruction | 0 | 108,435 | 5 | 216,870 |
Yes | output | 1 | 108,435 | 5 | 216,871 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Given are N positive integers A_1,...,A_N.
Consider positive integers B_1, ..., B_N that satisfy the following condition.
Condition: For any i, j such that 1 \leq i < j \leq N, A_i B_i = A_j B_j holds.
Find the minimum possible value of B_1 + ... + B_N for such B_1,...,B_N.
Since the answer can be enormous, print the sum modulo (10^9 +7).
Constraints
* 1 \leq N \leq 10^4
* 1 \leq A_i \leq 10^6
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 ... A_N
Output
Print the minimum possible value of B_1 + ... + B_N for B_1,...,B_N that satisfy the condition, modulo (10^9 +7).
Examples
Input
3
2 3 4
Output
13
Input
5
12 12 12 12 12
Output
5
Input
3
1000000 999999 999998
Output
996989508
Submitted Solution:
```
#逆元しない場合
from fractions import gcd
n=int(input())
a=list(map(int,input().split()))
mod=10**9+7
L=a[0]
def lcm(x,y):
return x//gcd(x,y)*y
for i in a:
L=lcm(L,i)
print(sum([L//i for i in a])%mod)
``` | instruction | 0 | 108,436 | 5 | 216,872 |
Yes | output | 1 | 108,436 | 5 | 216,873 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Given are N positive integers A_1,...,A_N.
Consider positive integers B_1, ..., B_N that satisfy the following condition.
Condition: For any i, j such that 1 \leq i < j \leq N, A_i B_i = A_j B_j holds.
Find the minimum possible value of B_1 + ... + B_N for such B_1,...,B_N.
Since the answer can be enormous, print the sum modulo (10^9 +7).
Constraints
* 1 \leq N \leq 10^4
* 1 \leq A_i \leq 10^6
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 ... A_N
Output
Print the minimum possible value of B_1 + ... + B_N for B_1,...,B_N that satisfy the condition, modulo (10^9 +7).
Examples
Input
3
2 3 4
Output
13
Input
5
12 12 12 12 12
Output
5
Input
3
1000000 999999 999998
Output
996989508
Submitted Solution:
```
n=int(input())
a=[int(j) for j in input().split()]
t=1
mod=10**9+7
from fractions import*
for i in a:
t*=i//gcd(t,i)
print(sum(t//i for i in a)%mod)
``` | instruction | 0 | 108,437 | 5 | 216,874 |
Yes | output | 1 | 108,437 | 5 | 216,875 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Given are N positive integers A_1,...,A_N.
Consider positive integers B_1, ..., B_N that satisfy the following condition.
Condition: For any i, j such that 1 \leq i < j \leq N, A_i B_i = A_j B_j holds.
Find the minimum possible value of B_1 + ... + B_N for such B_1,...,B_N.
Since the answer can be enormous, print the sum modulo (10^9 +7).
Constraints
* 1 \leq N \leq 10^4
* 1 \leq A_i \leq 10^6
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 ... A_N
Output
Print the minimum possible value of B_1 + ... + B_N for B_1,...,B_N that satisfy the condition, modulo (10^9 +7).
Examples
Input
3
2 3 4
Output
13
Input
5
12 12 12 12 12
Output
5
Input
3
1000000 999999 999998
Output
996989508
Submitted Solution:
```
from fractions import gcd;from functools import reduce;n,*a=map(int,open(0).read().split());l=reduce(lambda a,b:a*b//gcd(a,b),a);print(sum(l//i for i in a)%1000000007)
``` | instruction | 0 | 108,438 | 5 | 216,876 |
Yes | output | 1 | 108,438 | 5 | 216,877 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Given are N positive integers A_1,...,A_N.
Consider positive integers B_1, ..., B_N that satisfy the following condition.
Condition: For any i, j such that 1 \leq i < j \leq N, A_i B_i = A_j B_j holds.
Find the minimum possible value of B_1 + ... + B_N for such B_1,...,B_N.
Since the answer can be enormous, print the sum modulo (10^9 +7).
Constraints
* 1 \leq N \leq 10^4
* 1 \leq A_i \leq 10^6
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 ... A_N
Output
Print the minimum possible value of B_1 + ... + B_N for B_1,...,B_N that satisfy the condition, modulo (10^9 +7).
Examples
Input
3
2 3 4
Output
13
Input
5
12 12 12 12 12
Output
5
Input
3
1000000 999999 999998
Output
996989508
Submitted Solution:
```
from fractions import gcd
def main():
N = int(input())
A = list(map(int, input().split()))
MOD = 10**9 + 7
lcm = 1
for a in A:
lcm = lcm * a // gcd(lcm, a)
ans = 0
for a in A:
ans += lcm // a
print(ans % MOD)
if __name__ == "__main__":
main()
``` | instruction | 0 | 108,439 | 5 | 216,878 |
No | output | 1 | 108,439 | 5 | 216,879 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Given are N positive integers A_1,...,A_N.
Consider positive integers B_1, ..., B_N that satisfy the following condition.
Condition: For any i, j such that 1 \leq i < j \leq N, A_i B_i = A_j B_j holds.
Find the minimum possible value of B_1 + ... + B_N for such B_1,...,B_N.
Since the answer can be enormous, print the sum modulo (10^9 +7).
Constraints
* 1 \leq N \leq 10^4
* 1 \leq A_i \leq 10^6
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 ... A_N
Output
Print the minimum possible value of B_1 + ... + B_N for B_1,...,B_N that satisfy the condition, modulo (10^9 +7).
Examples
Input
3
2 3 4
Output
13
Input
5
12 12 12 12 12
Output
5
Input
3
1000000 999999 999998
Output
996989508
Submitted Solution:
```
from functools import reduce
from math import gcd
def lcm(x, y):
return x*y//gcd(x, y)
MOD = 10**9 + 7
N = int(input())
A = list(map(int, input().split()))
x = reduce(lcm, A)
ans = 0
for a in A:
ans += x // a
ans %= MOD
print(ans)
``` | instruction | 0 | 108,440 | 5 | 216,880 |
No | output | 1 | 108,440 | 5 | 216,881 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Given are N positive integers A_1,...,A_N.
Consider positive integers B_1, ..., B_N that satisfy the following condition.
Condition: For any i, j such that 1 \leq i < j \leq N, A_i B_i = A_j B_j holds.
Find the minimum possible value of B_1 + ... + B_N for such B_1,...,B_N.
Since the answer can be enormous, print the sum modulo (10^9 +7).
Constraints
* 1 \leq N \leq 10^4
* 1 \leq A_i \leq 10^6
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 ... A_N
Output
Print the minimum possible value of B_1 + ... + B_N for B_1,...,B_N that satisfy the condition, modulo (10^9 +7).
Examples
Input
3
2 3 4
Output
13
Input
5
12 12 12 12 12
Output
5
Input
3
1000000 999999 999998
Output
996989508
Submitted Solution:
```
from fractions import gcd
def lcm(a,b):
return a*(b//gcd(a,b))
mod=10**9+7
n=int(input())
A=list(map(int,input().split()))
L=A[0]
for i in range(1,n):
L=lcm(L,A[i])
ans=0
for i in range(n):
ans+=(L//A[i])%mod
print(ans%mod)
``` | instruction | 0 | 108,441 | 5 | 216,882 |
No | output | 1 | 108,441 | 5 | 216,883 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
Given are N positive integers A_1,...,A_N.
Consider positive integers B_1, ..., B_N that satisfy the following condition.
Condition: For any i, j such that 1 \leq i < j \leq N, A_i B_i = A_j B_j holds.
Find the minimum possible value of B_1 + ... + B_N for such B_1,...,B_N.
Since the answer can be enormous, print the sum modulo (10^9 +7).
Constraints
* 1 \leq N \leq 10^4
* 1 \leq A_i \leq 10^6
* All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
A_1 ... A_N
Output
Print the minimum possible value of B_1 + ... + B_N for B_1,...,B_N that satisfy the condition, modulo (10^9 +7).
Examples
Input
3
2 3 4
Output
13
Input
5
12 12 12 12 12
Output
5
Input
3
1000000 999999 999998
Output
996989508
Submitted Solution:
```
N = int(input())
A = list(map(int, input().split()))
if N == 1:
print(1)
exit()
def saisho_ko_baisu(A,B):
# A < B
x = A*B
r = B%A
while r!=0:
B = A
A = r
r = B%A
return x//A
A.sort(reverse = True)
tmp = saisho_ko_baisu(A[0], A[1])
for i in range(2,N):
if tmp % A[i] != 0:
tmp = saisho_ko_baisu(A[i], tmp)
res = 0
for a in A:
res += tmp//a
print(res % (10**9 + 7))
``` | instruction | 0 | 108,442 | 5 | 216,884 |
No | output | 1 | 108,442 | 5 | 216,885 |
Provide a correct Python 3 solution for this coding contest problem.
For a set $S$ of integers, perform a sequence of the following operations. Note that multiple elements can have equivalent values in $S$.
* insert($x$): Insert $x$ to $S$ and report the number of elements in $S$ after the operation.
* find($x$): Report the number of $x$ in $S$.
* delete($x$): Delete all $x$ from $S$.
* dump($L$, $R$): Print elements $x$ in $S$ such that $L \leq x \leq R$.
Constraints
* $1 \leq q \leq 200,000$
* $0 \leq x \leq 1,000,000,000$
* The total number of elements printed by dump operations does not exceed $1,000,000$
* The sum of numbers printed by find operations does not exceed $2,000,000$
Input
The input is given in the following format.
$q$
$query_1$
$query_2$
:
$query_q$
Each query $query_i$ is given by
0 $x$
or
1 $x$
or
2 $x$
or
3 $L$ $R$
where the first digits 0, 1, 2 and 3 represent insert, find, delete and dump operations respectively.
Output
For each insert operation, print the number of elements in $S$.
For each find operation, print the number of specified elements in $S$.
For each dump operation, print the corresponding elements in ascending order. Print an element in a line.
Example
Input
10
0 1
0 1
0 2
0 3
2 2
1 1
1 2
1 3
0 4
3 1 4
Output
1
2
3
4
2
0
1
4
1
1
3
4 | instruction | 0 | 108,607 | 5 | 217,214 |
"Correct Solution:
```
# -*- coding: utf-8 -*-
"""
Set - Multi-Set
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP2_7_D&lang=jp
"""
from bisect import insort, bisect_right, bisect_left
class Multi_set:
def __init__(self):
self.total = 0
self.ms = dict()
self.lr = []
def insert(self, x):
self.total += 1
if x in self.ms:
self.ms[x] += 1
else:
self.ms[x] = 1
insort(self.lr, x)
print(self.total)
def find(self, x):
print(self.ms.get(x, 0))
def delete(self, x):
if x in self.ms:
self.total -= self.ms[x]
self.ms[x] = 0
def dump(self, l, r):
lb = bisect_left(self.lr, l)
ub = bisect_right(self.lr, r)
for i in range(lb, ub):
k = self.lr[i]
v = self.ms[k]
print(f'{k}\n' * v, end='')
ms = Multi_set()
for _ in range(int(input())):
op, x, y = (input() + ' 1').split()[:3]
if op == '0':
ms.insert(int(x))
elif op == '1':
ms.find(int(x))
elif op == '2':
ms.delete(int(x))
else:
ms.dump(int(x), int(y))
``` | output | 1 | 108,607 | 5 | 217,215 |
Provide a correct Python 3 solution for this coding contest problem.
For a set $S$ of integers, perform a sequence of the following operations. Note that multiple elements can have equivalent values in $S$.
* insert($x$): Insert $x$ to $S$ and report the number of elements in $S$ after the operation.
* find($x$): Report the number of $x$ in $S$.
* delete($x$): Delete all $x$ from $S$.
* dump($L$, $R$): Print elements $x$ in $S$ such that $L \leq x \leq R$.
Constraints
* $1 \leq q \leq 200,000$
* $0 \leq x \leq 1,000,000,000$
* The total number of elements printed by dump operations does not exceed $1,000,000$
* The sum of numbers printed by find operations does not exceed $2,000,000$
Input
The input is given in the following format.
$q$
$query_1$
$query_2$
:
$query_q$
Each query $query_i$ is given by
0 $x$
or
1 $x$
or
2 $x$
or
3 $L$ $R$
where the first digits 0, 1, 2 and 3 represent insert, find, delete and dump operations respectively.
Output
For each insert operation, print the number of elements in $S$.
For each find operation, print the number of specified elements in $S$.
For each dump operation, print the corresponding elements in ascending order. Print an element in a line.
Example
Input
10
0 1
0 1
0 2
0 3
2 2
1 1
1 2
1 3
0 4
3 1 4
Output
1
2
3
4
2
0
1
4
1
1
3
4 | instruction | 0 | 108,608 | 5 | 217,216 |
"Correct Solution:
```
from bisect import insort, bisect_right, bisect_left
class MultiSet:
def __init__(self) -> None:
self.total = 0
self.ms: dict = dict()
self.lr: list = []
def insert(self, x: int) -> None:
self.total += 1
if x in self.ms:
self.ms[x] += 1
else:
self.ms[x] = 1
insort(self.lr, x)
print(self.total)
def find(self, x: int) -> None:
print(self.ms.get(x, 0))
def delete(self, x: int) -> None:
if x in self.ms:
self.total -= self.ms[x]
self.ms[x] = 0
def dump(self, l: int, r: int) -> None:
lb = bisect_left(self.lr, l)
ub = bisect_right(self.lr, r)
for i in range(lb, ub):
k = self.lr[i]
v = self.ms[k]
print(f'{k}\n' * v, end='')
if __name__ == "__main__":
ms = MultiSet()
num_query = int(input())
for _ in range(num_query):
op, *v = map(lambda x: int(x), input().split())
if 0 == op:
ms.insert(v[0])
elif 1 == op:
ms.find(v[0])
elif 2 == op:
ms.delete(v[0])
else:
ms.dump(v[0], v[1])
``` | output | 1 | 108,608 | 5 | 217,217 |
Provide a correct Python 3 solution for this coding contest problem.
For a set $S$ of integers, perform a sequence of the following operations. Note that multiple elements can have equivalent values in $S$.
* insert($x$): Insert $x$ to $S$ and report the number of elements in $S$ after the operation.
* find($x$): Report the number of $x$ in $S$.
* delete($x$): Delete all $x$ from $S$.
* dump($L$, $R$): Print elements $x$ in $S$ such that $L \leq x \leq R$.
Constraints
* $1 \leq q \leq 200,000$
* $0 \leq x \leq 1,000,000,000$
* The total number of elements printed by dump operations does not exceed $1,000,000$
* The sum of numbers printed by find operations does not exceed $2,000,000$
Input
The input is given in the following format.
$q$
$query_1$
$query_2$
:
$query_q$
Each query $query_i$ is given by
0 $x$
or
1 $x$
or
2 $x$
or
3 $L$ $R$
where the first digits 0, 1, 2 and 3 represent insert, find, delete and dump operations respectively.
Output
For each insert operation, print the number of elements in $S$.
For each find operation, print the number of specified elements in $S$.
For each dump operation, print the corresponding elements in ascending order. Print an element in a line.
Example
Input
10
0 1
0 1
0 2
0 3
2 2
1 1
1 2
1 3
0 4
3 1 4
Output
1
2
3
4
2
0
1
4
1
1
3
4 | instruction | 0 | 108,609 | 5 | 217,218 |
"Correct Solution:
```
import bisect
import collections
def main6():
values = collections.defaultdict(int)
element_count = 0
q = int(input())
for _ in range(q):
com, *para = map(int, input().split())
if com == 0:
values[para[0]] += 1
element_count += 1
print(element_count)
elif com == 1:
print(values[para[0]])
elif com == 2:
element_count -= values[para[0]]
values[para[0]] = 0
elif com == 3:
if para[0] == para[1]:
for _ in range(values[para[0]]):
print(para[0])
elif abs(para[0] - para[1]) < 10:
for i in range(para[0], para[1] + 1):
for _ in range(values[i]):
print(i)
else:
key_list = [k for k, v in values.items() if v != 0]
key_list = sorted(key_list)
lb = bisect.bisect_left(key_list, para[0])
rb = bisect.bisect_right(key_list, para[1])
for s in key_list[lb:rb] :
for _ in range(values[s]):
print(s)
main6()
``` | output | 1 | 108,609 | 5 | 217,219 |
Provide a correct Python 3 solution for this coding contest problem.
For a set $S$ of integers, perform a sequence of the following operations. Note that multiple elements can have equivalent values in $S$.
* insert($x$): Insert $x$ to $S$ and report the number of elements in $S$ after the operation.
* find($x$): Report the number of $x$ in $S$.
* delete($x$): Delete all $x$ from $S$.
* dump($L$, $R$): Print elements $x$ in $S$ such that $L \leq x \leq R$.
Constraints
* $1 \leq q \leq 200,000$
* $0 \leq x \leq 1,000,000,000$
* The total number of elements printed by dump operations does not exceed $1,000,000$
* The sum of numbers printed by find operations does not exceed $2,000,000$
Input
The input is given in the following format.
$q$
$query_1$
$query_2$
:
$query_q$
Each query $query_i$ is given by
0 $x$
or
1 $x$
or
2 $x$
or
3 $L$ $R$
where the first digits 0, 1, 2 and 3 represent insert, find, delete and dump operations respectively.
Output
For each insert operation, print the number of elements in $S$.
For each find operation, print the number of specified elements in $S$.
For each dump operation, print the corresponding elements in ascending order. Print an element in a line.
Example
Input
10
0 1
0 1
0 2
0 3
2 2
1 1
1 2
1 3
0 4
3 1 4
Output
1
2
3
4
2
0
1
4
1
1
3
4 | instruction | 0 | 108,610 | 5 | 217,220 |
"Correct Solution:
```
# AOJ ITP2_7_D: Multi-Set
# Python3 2018.6.24 bal4u
from bisect import bisect_left, bisect_right, insort_left
dict = {}
keytbl = []
cnt = 0
q = int(input())
for i in range(q):
a = list(input().split())
ki = int(a[1])
if a[0] == '0':
if ki not in dict:
dict[ki] = 1
insort_left(keytbl, ki)
else: dict[ki] += 1
cnt += 1
print(cnt)
elif a[0] == '1': print(dict[ki] if ki in dict else 0)
elif a[0] == '2':
if ki in dict:
cnt -= dict[ki]
dict[ki] = 0
else:
L = bisect_left (keytbl, int(a[1]))
R = bisect_right(keytbl, int(a[2]), L)
for j in range(L, R):
for k in range(dict[keytbl[j]]): print(keytbl[j])
``` | output | 1 | 108,610 | 5 | 217,221 |
Provide a correct Python 3 solution for this coding contest problem.
For a set $S$ of integers, perform a sequence of the following operations. Note that multiple elements can have equivalent values in $S$.
* insert($x$): Insert $x$ to $S$ and report the number of elements in $S$ after the operation.
* find($x$): Report the number of $x$ in $S$.
* delete($x$): Delete all $x$ from $S$.
* dump($L$, $R$): Print elements $x$ in $S$ such that $L \leq x \leq R$.
Constraints
* $1 \leq q \leq 200,000$
* $0 \leq x \leq 1,000,000,000$
* The total number of elements printed by dump operations does not exceed $1,000,000$
* The sum of numbers printed by find operations does not exceed $2,000,000$
Input
The input is given in the following format.
$q$
$query_1$
$query_2$
:
$query_q$
Each query $query_i$ is given by
0 $x$
or
1 $x$
or
2 $x$
or
3 $L$ $R$
where the first digits 0, 1, 2 and 3 represent insert, find, delete and dump operations respectively.
Output
For each insert operation, print the number of elements in $S$.
For each find operation, print the number of specified elements in $S$.
For each dump operation, print the corresponding elements in ascending order. Print an element in a line.
Example
Input
10
0 1
0 1
0 2
0 3
2 2
1 1
1 2
1 3
0 4
3 1 4
Output
1
2
3
4
2
0
1
4
1
1
3
4 | instruction | 0 | 108,611 | 5 | 217,222 |
"Correct Solution:
```
import sys
import bisect
nq = int(input())
lines = sys.stdin.readlines()
ans = [None] * nq
arr = []
for i in range(nq):
q, *arg = lines[i].split()
x = int(arg[0])
l_idx = bisect.bisect_left(arr, x)
r_idx = bisect.bisect_right(arr, x)
if q == '0': # insert x
arr.insert(l_idx, x)
ans[i] = str(len(arr))
elif q == '1': # find x
ans[i] = r_idx - l_idx
elif q == '2': # delete x
arr[l_idx:r_idx] = []
else: # dump L R
r_idx = bisect.bisect_right(arr, int(arg[1]))
ans[i] = '\n'.join(map(str, arr[l_idx:r_idx])) if l_idx != r_idx else None
#print(q, *arg, '\t', arr, '\t', ans[i])
[print(x) for x in ans if x is not None]
``` | output | 1 | 108,611 | 5 | 217,223 |
Provide a correct Python 3 solution for this coding contest problem.
For a set $S$ of integers, perform a sequence of the following operations. Note that multiple elements can have equivalent values in $S$.
* insert($x$): Insert $x$ to $S$ and report the number of elements in $S$ after the operation.
* find($x$): Report the number of $x$ in $S$.
* delete($x$): Delete all $x$ from $S$.
* dump($L$, $R$): Print elements $x$ in $S$ such that $L \leq x \leq R$.
Constraints
* $1 \leq q \leq 200,000$
* $0 \leq x \leq 1,000,000,000$
* The total number of elements printed by dump operations does not exceed $1,000,000$
* The sum of numbers printed by find operations does not exceed $2,000,000$
Input
The input is given in the following format.
$q$
$query_1$
$query_2$
:
$query_q$
Each query $query_i$ is given by
0 $x$
or
1 $x$
or
2 $x$
or
3 $L$ $R$
where the first digits 0, 1, 2 and 3 represent insert, find, delete and dump operations respectively.
Output
For each insert operation, print the number of elements in $S$.
For each find operation, print the number of specified elements in $S$.
For each dump operation, print the corresponding elements in ascending order. Print an element in a line.
Example
Input
10
0 1
0 1
0 2
0 3
2 2
1 1
1 2
1 3
0 4
3 1 4
Output
1
2
3
4
2
0
1
4
1
1
3
4 | instruction | 0 | 108,612 | 5 | 217,224 |
"Correct Solution:
```
# AOJ ITP2_7_D: Multi-Set
# Python3 2018.6.24 bal4u
from bisect import bisect_left, bisect_right, insort_left
dict = {}
keytbl = []
cnt = 0
q = int(input())
for i in range(q):
a = list(input().split())
ki = int(a[1])
if a[0] == '0':
if ki not in dict:
dict[ki] = 1
insort_left(keytbl, ki)
else: dict[ki] += 1
cnt += 1
print(cnt)
elif a[0] == '1': print(dict[ki] if ki in dict else 0)
elif a[0] == '2':
if ki in dict:
cnt -= dict[ki]
dict[ki] = 0
else:
L = bisect_left (keytbl, int(a[1]))
R = bisect_right(keytbl, int(a[2]), L)
r = []
for j in range(L, R): r += [keytbl[j]]*dict[keytbl[j]]
if r != []: print(*r, sep='\n')
``` | output | 1 | 108,612 | 5 | 217,225 |
Provide a correct Python 3 solution for this coding contest problem.
For a set $S$ of integers, perform a sequence of the following operations. Note that multiple elements can have equivalent values in $S$.
* insert($x$): Insert $x$ to $S$ and report the number of elements in $S$ after the operation.
* find($x$): Report the number of $x$ in $S$.
* delete($x$): Delete all $x$ from $S$.
* dump($L$, $R$): Print elements $x$ in $S$ such that $L \leq x \leq R$.
Constraints
* $1 \leq q \leq 200,000$
* $0 \leq x \leq 1,000,000,000$
* The total number of elements printed by dump operations does not exceed $1,000,000$
* The sum of numbers printed by find operations does not exceed $2,000,000$
Input
The input is given in the following format.
$q$
$query_1$
$query_2$
:
$query_q$
Each query $query_i$ is given by
0 $x$
or
1 $x$
or
2 $x$
or
3 $L$ $R$
where the first digits 0, 1, 2 and 3 represent insert, find, delete and dump operations respectively.
Output
For each insert operation, print the number of elements in $S$.
For each find operation, print the number of specified elements in $S$.
For each dump operation, print the corresponding elements in ascending order. Print an element in a line.
Example
Input
10
0 1
0 1
0 2
0 3
2 2
1 1
1 2
1 3
0 4
3 1 4
Output
1
2
3
4
2
0
1
4
1
1
3
4 | instruction | 0 | 108,613 | 5 | 217,226 |
"Correct Solution:
```
import sys
import bisect
s = []
input()
for q in sys.stdin:
q = q.split()
q[1:] = list(map(int, q[1:]))
if q[0] == '0':
bisect.insort_left(s, q[1])
print(len(s))
elif q[0] == '1':
first = bisect.bisect_left(s,q[1])
end = bisect.bisect_right(s,q[1])
print(end - first)
elif q[0] == '2':
first = bisect.bisect_left(s,q[1])
end = bisect.bisect_right(s,q[1])
del s[first:end]
else:
first = bisect.bisect_left(s,q[1])
end = bisect.bisect_right(s,q[2])
if first != end:
print('\n'.join(list(map(str,[i for i in s[first:end]]))))
``` | output | 1 | 108,613 | 5 | 217,227 |
Provide a correct Python 3 solution for this coding contest problem.
For a set $S$ of integers, perform a sequence of the following operations. Note that multiple elements can have equivalent values in $S$.
* insert($x$): Insert $x$ to $S$ and report the number of elements in $S$ after the operation.
* find($x$): Report the number of $x$ in $S$.
* delete($x$): Delete all $x$ from $S$.
* dump($L$, $R$): Print elements $x$ in $S$ such that $L \leq x \leq R$.
Constraints
* $1 \leq q \leq 200,000$
* $0 \leq x \leq 1,000,000,000$
* The total number of elements printed by dump operations does not exceed $1,000,000$
* The sum of numbers printed by find operations does not exceed $2,000,000$
Input
The input is given in the following format.
$q$
$query_1$
$query_2$
:
$query_q$
Each query $query_i$ is given by
0 $x$
or
1 $x$
or
2 $x$
or
3 $L$ $R$
where the first digits 0, 1, 2 and 3 represent insert, find, delete and dump operations respectively.
Output
For each insert operation, print the number of elements in $S$.
For each find operation, print the number of specified elements in $S$.
For each dump operation, print the corresponding elements in ascending order. Print an element in a line.
Example
Input
10
0 1
0 1
0 2
0 3
2 2
1 1
1 2
1 3
0 4
3 1 4
Output
1
2
3
4
2
0
1
4
1
1
3
4 | instruction | 0 | 108,614 | 5 | 217,228 |
"Correct Solution:
```
from bisect import bisect_left, bisect_right, insort_left
dict = {}
li = []
count = 0
q = int(input())
for i in range(q):
a = input().split()
t = int(a[1])
if a[0] == "0":
if t not in dict:
dict[t] = 1
insort_left(li, t)
else:
dict[t] += 1
count += 1
print(count)
elif a[0] == "1":
print(dict[t] if t in dict else 0)
elif a[0] == "2":
if t in dict:
count -= dict[t]
dict[t] = 0
else:
l = bisect_left(li, int(a[1]))
r = bisect_right(li, int(a[2]))
lis = []
for i in range(l, r):
lis += [li[i]] * dict[li[i]]
if lis != []:
print(*lis, sep="\n")
``` | output | 1 | 108,614 | 5 | 217,229 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For a set $S$ of integers, perform a sequence of the following operations. Note that multiple elements can have equivalent values in $S$.
* insert($x$): Insert $x$ to $S$ and report the number of elements in $S$ after the operation.
* find($x$): Report the number of $x$ in $S$.
* delete($x$): Delete all $x$ from $S$.
* dump($L$, $R$): Print elements $x$ in $S$ such that $L \leq x \leq R$.
Constraints
* $1 \leq q \leq 200,000$
* $0 \leq x \leq 1,000,000,000$
* The total number of elements printed by dump operations does not exceed $1,000,000$
* The sum of numbers printed by find operations does not exceed $2,000,000$
Input
The input is given in the following format.
$q$
$query_1$
$query_2$
:
$query_q$
Each query $query_i$ is given by
0 $x$
or
1 $x$
or
2 $x$
or
3 $L$ $R$
where the first digits 0, 1, 2 and 3 represent insert, find, delete and dump operations respectively.
Output
For each insert operation, print the number of elements in $S$.
For each find operation, print the number of specified elements in $S$.
For each dump operation, print the corresponding elements in ascending order. Print an element in a line.
Example
Input
10
0 1
0 1
0 2
0 3
2 2
1 1
1 2
1 3
0 4
3 1 4
Output
1
2
3
4
2
0
1
4
1
1
3
4
Submitted Solution:
```
from bisect import bisect_left,bisect_right,insort_left
dict = {}
keytbl = []
cnt = 0
q = int(input())
for i in range(q):
a = list(input().split())
ki = int(a[1])
if a[0] == "0":
if ki not in dict:
dict[ki] =1
insort_left(keytbl,ki)
else:dict[ki] += 1
cnt += 1
print(cnt)
elif a[0] == "1":print(dict[ki] if ki in dict else 0)
elif a[0] == "2":
if ki in dict:
cnt -= dict[ki]
dict[ki] =0
else:
L = bisect_left(keytbl,int(a[1]))
R = bisect_right(keytbl,int(a[2]),L)
r = []
for j in range(L,R):r += [keytbl[j]]*dict[keytbl[j]]
if r != []:print(*r,sep="\n")
``` | instruction | 0 | 108,615 | 5 | 217,230 |
Yes | output | 1 | 108,615 | 5 | 217,231 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For a set $S$ of integers, perform a sequence of the following operations. Note that multiple elements can have equivalent values in $S$.
* insert($x$): Insert $x$ to $S$ and report the number of elements in $S$ after the operation.
* find($x$): Report the number of $x$ in $S$.
* delete($x$): Delete all $x$ from $S$.
* dump($L$, $R$): Print elements $x$ in $S$ such that $L \leq x \leq R$.
Constraints
* $1 \leq q \leq 200,000$
* $0 \leq x \leq 1,000,000,000$
* The total number of elements printed by dump operations does not exceed $1,000,000$
* The sum of numbers printed by find operations does not exceed $2,000,000$
Input
The input is given in the following format.
$q$
$query_1$
$query_2$
:
$query_q$
Each query $query_i$ is given by
0 $x$
or
1 $x$
or
2 $x$
or
3 $L$ $R$
where the first digits 0, 1, 2 and 3 represent insert, find, delete and dump operations respectively.
Output
For each insert operation, print the number of elements in $S$.
For each find operation, print the number of specified elements in $S$.
For each dump operation, print the corresponding elements in ascending order. Print an element in a line.
Example
Input
10
0 1
0 1
0 2
0 3
2 2
1 1
1 2
1 3
0 4
3 1 4
Output
1
2
3
4
2
0
1
4
1
1
3
4
Submitted Solution:
```
import bisect
S=[]
q=int(input())
def find(x):
index=bisect.bisect_left(S,x)
if index==len(S):
return -1
if S[index]==x:
return index
else:
return -1
for _ in range(q):
query=[int(i) for i in input().split(" ")]
if query[0]==0:
bisect.insort_left(S, query[1])
print(len(S))
elif query[0]==1:
L,R=query[1],query[1]
indL=bisect.bisect_left(S,L)
indR=bisect.bisect_right(S,R)
print(len(S[indL:indR]))
elif query[0]==2:
while find(query[1])>=0:
S.pop(find(query[1]))
else:
L,R=query[1],query[2]
indL=bisect.bisect_left(S,L)
indR=bisect.bisect_right(S,R)
for s in S[indL:indR]:
print(s)
``` | instruction | 0 | 108,616 | 5 | 217,232 |
Yes | output | 1 | 108,616 | 5 | 217,233 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For a set $S$ of integers, perform a sequence of the following operations. Note that multiple elements can have equivalent values in $S$.
* insert($x$): Insert $x$ to $S$ and report the number of elements in $S$ after the operation.
* find($x$): Report the number of $x$ in $S$.
* delete($x$): Delete all $x$ from $S$.
* dump($L$, $R$): Print elements $x$ in $S$ such that $L \leq x \leq R$.
Constraints
* $1 \leq q \leq 200,000$
* $0 \leq x \leq 1,000,000,000$
* The total number of elements printed by dump operations does not exceed $1,000,000$
* The sum of numbers printed by find operations does not exceed $2,000,000$
Input
The input is given in the following format.
$q$
$query_1$
$query_2$
:
$query_q$
Each query $query_i$ is given by
0 $x$
or
1 $x$
or
2 $x$
or
3 $L$ $R$
where the first digits 0, 1, 2 and 3 represent insert, find, delete and dump operations respectively.
Output
For each insert operation, print the number of elements in $S$.
For each find operation, print the number of specified elements in $S$.
For each dump operation, print the corresponding elements in ascending order. Print an element in a line.
Example
Input
10
0 1
0 1
0 2
0 3
2 2
1 1
1 2
1 3
0 4
3 1 4
Output
1
2
3
4
2
0
1
4
1
1
3
4
Submitted Solution:
```
import bisect
import sys
def input():
return sys.stdin.readline().strip()
S = []
count = {}
total = 0
for _ in range(int(input())):
order = tuple(map(int,input().split()))
value = order[1]
if order[0] == 0:
if value in count:
count[value] += 1
else:
count[value] = 1
bisect.insort(S,value)
total += 1
print(total)
elif order[0] == 1:
if value in count:
print(count[value])
else:
print(0)
elif order[0] == 2:
if value in count:
total -= count[value]
count[value] = 0
else:
left = bisect.bisect_left(S,order[1])
right = bisect.bisect(S,order[2])
out = []
for i in range(left,right):
out += [S[i]]*count[S[i]]
for i in out:
print(i)
"""
print(S)
print(count)
"""
``` | instruction | 0 | 108,617 | 5 | 217,234 |
Yes | output | 1 | 108,617 | 5 | 217,235 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For a set $S$ of integers, perform a sequence of the following operations. Note that multiple elements can have equivalent values in $S$.
* insert($x$): Insert $x$ to $S$ and report the number of elements in $S$ after the operation.
* find($x$): Report the number of $x$ in $S$.
* delete($x$): Delete all $x$ from $S$.
* dump($L$, $R$): Print elements $x$ in $S$ such that $L \leq x \leq R$.
Constraints
* $1 \leq q \leq 200,000$
* $0 \leq x \leq 1,000,000,000$
* The total number of elements printed by dump operations does not exceed $1,000,000$
* The sum of numbers printed by find operations does not exceed $2,000,000$
Input
The input is given in the following format.
$q$
$query_1$
$query_2$
:
$query_q$
Each query $query_i$ is given by
0 $x$
or
1 $x$
or
2 $x$
or
3 $L$ $R$
where the first digits 0, 1, 2 and 3 represent insert, find, delete and dump operations respectively.
Output
For each insert operation, print the number of elements in $S$.
For each find operation, print the number of specified elements in $S$.
For each dump operation, print the corresponding elements in ascending order. Print an element in a line.
Example
Input
10
0 1
0 1
0 2
0 3
2 2
1 1
1 2
1 3
0 4
3 1 4
Output
1
2
3
4
2
0
1
4
1
1
3
4
Submitted Solution:
```
from enum import Enum
class Color(Enum):
BLACK = 0
RED = 1
@staticmethod
def flip(c):
return [Color.RED, Color.BLACK][c.value]
class Node:
__slots__ = ('key', 'left', 'right', 'size', 'color', 'value')
def __init__(self, key, value):
self.key = key
self.value = value
self.left = Leaf
self.right = Leaf
self.size = 1
self.color = Color.RED
def is_red(self):
return self.color == Color.RED
def is_black(self):
return self.color == Color.BLACK
def __str__(self):
if self.color == Color.RED:
key = '*{}'.format(self.key)
else:
key = '{}'.format(self.key)
return "{}[{}] ({}, {})".format(key, self.size,
self.left, self.right)
class LeafNode(Node):
def __init__(self):
self.key = None
self.value = None
self.left = None
self.right = None
self.size = 0
self.color = None
def is_red(self):
return False
def is_black(self):
return False
def __str__(self):
return '-'
Leaf = LeafNode()
class RedBlackBinarySearchTree:
"""Red Black Binary Search Tree with range, min, max.
Originally impremented in the textbook
Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,
Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.
"""
def __init__(self):
self.root = Leaf
def put(self, key, value=None):
def _put(node):
if node is Leaf:
node = Node(key, value)
if node.key > key:
node.left = _put(node.left)
elif node.key < key:
node.right = _put(node.right)
else:
node.value = value
node = self._restore(node)
node.size = node.left.size + node.right.size + 1
return node
self.root = _put(self.root)
self.root.color = Color.BLACK
def _rotate_left(self, node):
assert node.right.is_red()
x = node.right
node.right = x.left
x.left = node
x.color = node.color
node.color = Color.RED
node.size = node.left.size + node.right.size + 1
return x
def _rotate_right(self, node):
assert node.left.is_red()
x = node.left
node.left = x.right
x.right = node
x.color = node.color
node.color = Color.RED
node.size = node.left.size + node.right.size + 1
return x
def _flip_colors(self, node):
node.color = Color.flip(node.color)
node.left.color = Color.flip(node.left.color)
node.right.color = Color.flip(node.right.color)
return node
def __contains__(self, key):
def _contains(node):
if node is Leaf:
return False
if node.key > key:
return _contains(node.left)
elif node.key < key:
return _contains(node.right)
else:
return True
return _contains(self.root)
def get(self, key):
def _get(node):
if node is Leaf:
return None
if node.key > key:
return _get(node.left)
elif node.key < key:
return _get(node.right)
else:
return node.value
return _get(self.root)
def delete(self, key):
def _delete(node):
if node is Leaf:
return Leaf
if node.key > key:
if node.left is Leaf:
return self._balance(node)
if not self._is_red_left(node):
node = self._red_left(node)
node.left = _delete(node.left)
else:
if node.left.is_red():
node = self._rotate_right(node)
if node.key == key and node.right is Leaf:
return Leaf
elif node.right is Leaf:
return self._balance(node)
if not self._is_red_right(node):
node = self._red_right(node)
if node.key == key:
x = self._find_min(node.right)
node.key = x.key
node.value = x.value
node.right = self._delete_min(node.right)
else:
node.right = _delete(node.right)
return self._balance(node)
if self.is_empty():
raise ValueError('delete on empty tree')
if not self.root.left.is_red() and not self.root.right.is_red():
self.root.color = Color.RED
self.root = _delete(self.root)
if not self.is_empty():
self.root.color = Color.BLACK
assert self.is_balanced()
def delete_max(self):
if self.is_empty():
raise ValueError('delete max on empty tree')
if not self.root.left.is_red() and not self.root.right.is_red():
self.root.color = Color.RED
self.root = self._delete_max(self.root)
if not self.is_empty():
self.root.color = Color.BLACK
assert self.is_balanced()
def _delete_max(self, node):
if node.left.is_red():
node = self._rotate_right(node)
if node.right is Leaf:
return Leaf
if not self._is_red_right(node):
node = self._red_right(node)
node.right = self._delete_max(node.right)
return self._balance(node)
def _red_right(self, node):
node = self._flip_colors(node)
if node.left.left.is_red():
node = self._rotate_right(node)
return node
def _is_red_right(self, node):
return (node.right.is_red() or
(node.right.is_black() and node.right.left.is_red()))
def delete_min(self):
if self.is_empty():
raise ValueError('delete min on empty tree')
if not self.root.left.is_red() and not self.root.right.is_red():
self.root.color = Color.RED
self.root = self._delete_min(self.root)
if not self.is_empty():
self.root.color = Color.BLACK
assert self.is_balanced()
def _delete_min(self, node):
if node.left is Leaf:
return Leaf
if not self._is_red_left(node):
node = self._red_left(node)
node.left = self._delete_min(node.left)
return self._balance(node)
def _red_left(self, node):
node = self._flip_colors(node)
if node.right.left.is_red():
node.right = self._rotate_right(node.right)
node = self._rotate_left(node)
return node
def _is_red_left(self, node):
return (node.left.is_red() or
(node.left.is_black() and node.left.left.is_red()))
def _balance(self, node):
if node.right.is_red():
node = self._rotate_left(node)
return self._restore(node)
def _restore(self, node):
if node.right.is_red() and not node.left.is_red():
node = self._rotate_left(node)
if node.left.is_red() and node.left.left.is_red():
node = self._rotate_right(node)
if node.left.is_red() and node.right.is_red():
node = self._flip_colors(node)
node.size = node.left.size + node.right.size + 1
return node
def is_empty(self):
return self.root is Leaf
def is_balanced(self):
if self.is_empty():
return True
try:
left = self._depth(self.root.left)
right = self._depth(self.root.right)
return left == right
except Exception:
return False
@property
def depth(self):
return self._depth(self.root)
def _depth(self, node):
if node is Leaf:
return 0
if node.right.is_red():
raise Exception('right red')
left = self._depth(node.left)
right = self._depth(node.right)
if left != right:
raise Exception('unbalanced')
if node.is_red():
return left
else:
return 1 + left
def __len__(self):
return self.root.size
@property
def max(self):
if self.is_empty():
raise ValueError('max on empty tree')
return self._max(self.root)
def _max(self, node):
x = self._find_max(node)
return x.key
def _find_max(self, node):
if node.right is Leaf:
return node
else:
return self._find_max(node.right)
@property
def min(self):
if self.is_empty():
raise ValueError('min on empty tree')
return self._min(self.root)
def _min(self, node):
x = self._find_min(node)
return x.key
def _find_min(self, node):
if node.left is Leaf:
return node
else:
return self._find_min(node.left)
def range(self, min_, max_):
def _range(node):
if node is Leaf:
return
if node.key > max_:
yield from _range(node.left)
elif node.key < min_:
yield from _range(node.right)
else:
yield from _range(node.left)
yield (node.key, node.value)
yield from _range(node.right)
if min_ > max_:
return
yield from _range(self.root)
class BalancedBstCounter:
def __init__(self):
self.bst = RedBlackBinarySearchTree()
self.count = 0
def up(self, key):
if key in self.bst:
value = self.bst.get(key) + 1
else:
value = 1
self.bst.put(key, value)
self.count += 1
def __contains__(self, key):
if key in self.bst:
return self.bst.get(key) > 0
else:
return False
def get(self, key):
if key in self.bst:
return self.bst.get(key)
else:
return 0
def down(self, key):
if key in self.bst:
value = self.bst.get(key)
self.bst.put(key, 0)
self.count -= value
def range(self, a, b):
for k, v in self.bst.range(a, b):
for _ in range(v):
yield k
def __len__(self):
return self.count
def __str__(self):
return str(self.bst.root)
def run():
q = int(input())
counter = BalancedBstCounter()
for _ in range(q):
command, *value = [int(x) for x in input().split()]
if command == 0:
counter.up(value[0])
print(len(counter))
elif command == 1:
print(counter.get(value[0]))
elif command == 2:
counter.down(value[0])
elif command == 3:
for i in counter.range(*value):
print(i)
else:
raise ValueError('invalid command')
if __name__ == '__main__':
run()
``` | instruction | 0 | 108,618 | 5 | 217,236 |
Yes | output | 1 | 108,618 | 5 | 217,237 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For a set $S$ of integers, perform a sequence of the following operations. Note that multiple elements can have equivalent values in $S$.
* insert($x$): Insert $x$ to $S$ and report the number of elements in $S$ after the operation.
* find($x$): Report the number of $x$ in $S$.
* delete($x$): Delete all $x$ from $S$.
* dump($L$, $R$): Print elements $x$ in $S$ such that $L \leq x \leq R$.
Constraints
* $1 \leq q \leq 200,000$
* $0 \leq x \leq 1,000,000,000$
* The total number of elements printed by dump operations does not exceed $1,000,000$
* The sum of numbers printed by find operations does not exceed $2,000,000$
Input
The input is given in the following format.
$q$
$query_1$
$query_2$
:
$query_q$
Each query $query_i$ is given by
0 $x$
or
1 $x$
or
2 $x$
or
3 $L$ $R$
where the first digits 0, 1, 2 and 3 represent insert, find, delete and dump operations respectively.
Output
For each insert operation, print the number of elements in $S$.
For each find operation, print the number of specified elements in $S$.
For each dump operation, print the corresponding elements in ascending order. Print an element in a line.
Example
Input
10
0 1
0 1
0 2
0 3
2 2
1 1
1 2
1 3
0 4
3 1 4
Output
1
2
3
4
2
0
1
4
1
1
3
4
Submitted Solution:
```
# AOJ ITP2_7_D: Multi-Set
# Python3 2018.6.24 bal4u
from bisect import bisect_left, bisect_right, insort_left
dict = {}
keytbl = []
cnt = 0
q = int(input())
for i in range(q):
a = list(input().split())
ki = int(a[1])
if a[0] == '0':
if ki not in dict:
dict[ki] = 1
insort_left(keytbl, ki)
else: dict[ki] += 1
cnt += 1
print(cnt)
elif a[0] == '1': print(dict[ki] if ki in dict else 0)
elif a[0] == '2':
if ki in dict:
cnt -= dict[ki]
dict[ki] = 0
else:
L = bisect_left (keytbl, int(a[1]))
R = bisect_right(keytbl, int(a[2]), L)
r = []
for j in range(L, R): r += [keytbl[j]]*dict[keytbl[j]]
print(*r, sep='\n')
``` | instruction | 0 | 108,619 | 5 | 217,238 |
No | output | 1 | 108,619 | 5 | 217,239 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For a set $S$ of integers, perform a sequence of the following operations. Note that multiple elements can have equivalent values in $S$.
* insert($x$): Insert $x$ to $S$ and report the number of elements in $S$ after the operation.
* find($x$): Report the number of $x$ in $S$.
* delete($x$): Delete all $x$ from $S$.
* dump($L$, $R$): Print elements $x$ in $S$ such that $L \leq x \leq R$.
Constraints
* $1 \leq q \leq 200,000$
* $0 \leq x \leq 1,000,000,000$
* The total number of elements printed by dump operations does not exceed $1,000,000$
* The sum of numbers printed by find operations does not exceed $2,000,000$
Input
The input is given in the following format.
$q$
$query_1$
$query_2$
:
$query_q$
Each query $query_i$ is given by
0 $x$
or
1 $x$
or
2 $x$
or
3 $L$ $R$
where the first digits 0, 1, 2 and 3 represent insert, find, delete and dump operations respectively.
Output
For each insert operation, print the number of elements in $S$.
For each find operation, print the number of specified elements in $S$.
For each dump operation, print the corresponding elements in ascending order. Print an element in a line.
Example
Input
10
0 1
0 1
0 2
0 3
2 2
1 1
1 2
1 3
0 4
3 1 4
Output
1
2
3
4
2
0
1
4
1
1
3
4
Submitted Solution:
```
def binary(l,c):
fst = 0
last = len(l)
mid = int(last/2)
if fst == 0 and last ==0:
l.insert(0,c)
return
while fst < last:
mid = int((fst+last) / 2)
if last == mid:
break
elif fst == mid:
break
elif l[mid] >= c:
last = mid
elif l[mid] <= c:
fst = mid
if l[mid] < c:
mid+=1
l.insert(mid,c)
q = int(input())
S = []
a = []
for i in range(q):
a.append(list(map(int,input().split())))
for i in range(q):
if a[i][0] == 0:
binary(S,a[i][1])
print(len(S))
elif a[i][0] == 1:
if a[i][1] in S:
print(1)
else:
print(0)
elif a[i][0] == 2:
if a[i][1] in S:
S.remove(a[i][1])
else:
for b in range(len(S)):
if S[b] >= a[i][1]:
for c in range(b,len(S)):
if S[c] <= a[i][2]:
print(S[c])
else:
break
break
``` | instruction | 0 | 108,620 | 5 | 217,240 |
No | output | 1 | 108,620 | 5 | 217,241 |
Evaluate the correctness of the submitted Python 3 solution to the coding contest problem. Provide a "Yes" or "No" response.
For a set $S$ of integers, perform a sequence of the following operations. Note that multiple elements can have equivalent values in $S$.
* insert($x$): Insert $x$ to $S$ and report the number of elements in $S$ after the operation.
* find($x$): Report the number of $x$ in $S$.
* delete($x$): Delete all $x$ from $S$.
* dump($L$, $R$): Print elements $x$ in $S$ such that $L \leq x \leq R$.
Constraints
* $1 \leq q \leq 200,000$
* $0 \leq x \leq 1,000,000,000$
* The total number of elements printed by dump operations does not exceed $1,000,000$
* The sum of numbers printed by find operations does not exceed $2,000,000$
Input
The input is given in the following format.
$q$
$query_1$
$query_2$
:
$query_q$
Each query $query_i$ is given by
0 $x$
or
1 $x$
or
2 $x$
or
3 $L$ $R$
where the first digits 0, 1, 2 and 3 represent insert, find, delete and dump operations respectively.
Output
For each insert operation, print the number of elements in $S$.
For each find operation, print the number of specified elements in $S$.
For each dump operation, print the corresponding elements in ascending order. Print an element in a line.
Example
Input
10
0 1
0 1
0 2
0 3
2 2
1 1
1 2
1 3
0 4
3 1 4
Output
1
2
3
4
2
0
1
4
1
1
3
4
Submitted Solution:
```
# AOJ ITP2_7_D: Multi-Set
# Python3 2018.6.24 bal4u
from bisect import bisect_left, bisect_right, insort_left
dict = {}
keytbl = []
cnt = 0
q = int(input())
for i in range(q):
a = list(input().split())
ki = int(a[1])
if a[0] == '0':
if ki not in dict:
dict[ki] = 1
insort_left(keytbl, ki)
else: dict[ki] += 1
cnt += 1
print(cnt)
elif a[0] == '1': print(dict[ki] if ki in dict else 0)
elif a[0] == '2':
if ki in dict:
cnt -= dict[ki]
dict[ki] = 0
else:
L = bisect_left (keytbl, int(a[1]))
R = bisect_right(keytbl, int(a[2]), L)
for j in range(L, R):
if dict[keytbl[j]] > 0: print(keytbl[j])
``` | instruction | 0 | 108,621 | 5 | 217,242 |
No | output | 1 | 108,621 | 5 | 217,243 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.