SER11

QUESTION DESCRIPTION 

Today, you have been given a task like ones back end developers face on a daily basis. You need to help in dealing with large amount of requests received in a purchasing application. The application has the following properties:

Consumers place large number of orders via this application

Each request has a time stamp and an order size associated with it.

Each order size is considered in terms of kilo-grams and can be of integer weight only.

Now, the data analytics team needs some information on regular intervals. Based on the requests received in the app, you need to help answer all queries posed by the data-analytics team. So, your task is:

Given
2
2 types of queries, the first one being of the form
1
1
X
X
T
T, indicates an order of weight
X
X is received at time
T
T. The second one is of the form
2
2
K
K
T
T indicates that the data analytics team asks you about the summation of weight of the orders received during the last
K
K minutes at time
T
T. The first query can be of the first type only.

For each query, it is guaranteed that they are given in ascending order of time. It means that given
q
q queries and each query having a time
T
T associated with it,
T
i
<
T
i
+
1
<
T
i
+
2
.
.
.
<
T
q
Ti<Ti+1<Ti+2...<Tq.

Input Format :

The first line contains a single integer
q
q denoting the total number of queries. Each of the next
q
q lines contains either of the two types of queries. Each query of type
1
1 first starts with the number
1
1 followed by
2
2 integers
X
X and
T
T. Each query of the second type first starts with the integer
2
2 and is then followed by
2
2 integers
K
K and
T
T.
All the Variables mandatorly declared as "long long int"

TEST CASE 1 

INPUT
4
1 20 3
1 40 4
2 5 5
1 100 100
OUTPUT
60

TEST CASE 2 

INPUT
5
1 776871948 1
2 53 82
1 441598129 112
2 136 166
1 599424253 258
OUTPUT
0
441598129


#include<stdio.h>
    int main()
    {
     long long int t,ss[100001],ww[100002],type,x,tim,i,j,k,mid,poi;
     scanf("%lld",&t);
     poi=1;
     ww[0]=0;
     ss[0]=0;
     for(i=0;i<t;i++)
     {
      scanf("%lld %lld %lld",&type,&x,&tim);
      if(type==1)
      {
       ss[poi]=tim;
       ww[poi]=x+ww[poi-1];
       poi++; 
      }
      else if(type==2)
      {
       j=1;
       k=poi-1;
       while(j<=k)
       {
        mid=(j+k)/2;
        if((tim-ss[mid])<x)
        k=mid-1;
        else
        j=mid+1;
       }
       if(mid<0)
       mid=0;
       if(mid>=poi)
       mid=poi-1;
       if((tim-ss[mid])>x)
        mid=mid+1;
       else if((tim - ss[mid])<=x )
       {
        if( mid-1 >=0 && (tim - ss[mid-1])<=x)
         mid=mid-1;
     
       }
       if(mid==0)
        printf("%lld\n",ww[poi-1]-ww[mid]);
       else
        printf("%lld\n",ww[poi-1]-ww[mid-1]);  
      
      }
     }
     return 0;
    }

Comments

Popular posts from this blog

SER12

AR15

AR1