Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Input Format:
First line contains an integer N that denotes the number of containers.
Second line contains an integer M denoting the number of days.
Each of the next M lines consists of two space separated integers l and r.
Followed by an integer Q denoting the number of queries.
Each of next Q lines contain a single integer K.

Output Format:
For each query, print the result in new line.

Constraints:
1 <= N <= 100000
1 <= M <= 1000
1 <= l <= r <= N
1 <= Q <= 100000
1 <= K <= N

What I have tried:

Python
def add(a,b):
    for i in xrange(a-1,b):
        box[i]+=1
def count(k):
    if box.count(k)!=0:
        box.index(k)
        return len(box)-box.index(k)
    else:
        return 0
from sys import stdin,stdout
box=[0]*int(stdin.readline())
for i in xrange(int(stdin.readline())):
    a,b = stdin.readline().split()
    add(int(a),int(b))
box.sort()
for i in xrange(int(stdin.readline())):
    print count(int(stdin.readline()))
Posted
Updated 16-Jul-18 21:44pm
Comments
Mehdi Gholam 17-Jul-18 2:46am    
First try to define your question to be understandable (more than 1.5sec).
[no name] 17-Jul-18 2:51am    
Time limit is exceeded when I run this code I want that this code runs in 1.5sec
[no name] 17-Jul-18 2:52am    
This is the question statement
Ajay loves chocolates very much. He has N containers numbered from 1 to N.Everyday, he used to select two indices [l,r] and adds 1 chocolate to each box starting from l to r (both inclusive).He repeats the same activity for M days.

After M days, he asked his friend Nakul Q queries. Each query can be described as: How many containers have atleast K chocolates.
Help Nakul to answer these queries.

THis is a challenge, and thus homework.
And we do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
 
Share this answer
 
This text is typical from a 'programming challenge' site, but you forgot the part that say what is the context and goal, and you provide no link to original problem.
So we don't know what you code is doing or the reason.

In those problems, strait forward solution/brute force is never the wanted solution. You need to study the problem to find the underlying mathematical structure which will lead to fast solution.

The fact that your code don't solve the problem in time limit just tell that your code is not smart enough. You need to sharpen your skills at data structures and algorithms.

This is Homework by yourself, and all the interest is to know if you can solve by yourself or not.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900