Customer & Queries CTS GenCNext
Customer & Queries CTS GenCNext
Problem:
In a shop, there are C customers stand in a queue to buy some items. Each customer has a unique ID (from 1 to C). The program must accept the value of C and Q queries as the input. Each query contains 3 integers, where the 1st integer represents the query type. The query type can be any one of the following.
- Query type 1: The 2nd and 3rd integers representing the customer ID and the number of items purchased by the customer.
- Query type 2: The 2nd and 3rd integers representing the starting and ending customer ID range. The program must design a system that handles all the transaction queries based on the query type.
- Query type 1: If the customer has already purchased some items, it adds the quanity of the new sale to the previous quantity and updates the record. Else it will make a fresh entry.
- Query type 2: It prints total number of items purchased by the given range of the customer ID. Note: At least one query of type 2 is always present in the given Q queries.
Boundary Condition(s):
2 <= C <= 50
1 <= Q <= 1000
Input Format:
The first line contains C and Q separated by a space. The next Q lines, each containing three integers separated by a space.
Output Format:
The first line contains the integer value(s) separated by a space.
Example Input/Output 1:
Input:
4 5
1 4 10
2 1 3
1 2 4
1 4 2
2 2 4
Output: 0 16
Example Input/Output 2:
Input:
8 6
1 6 15
1 1 28
2 2 6
1 2 70
2 1 5
2 1 6
Output: 1
5 98 113
Program:
c,q = map(int,input().split())
d = dict()
for i in range(q):
t,a,b = map(int,input().split())
if t == 1:
try:
d[a] += b
except:
d[a] = b
else:
s = 0
for i in range(a,b+1):
if i in d:
s += d[i]
print(s,end=" ")
Comments
Post a Comment