Purchasing Application

QUESTION

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:\n\n1.\tConsumers place large number of orders via this application\n2.\tEach request has a time stamp and an order size associated with it.\n3.\tEach order size is considered in terms of kilo-grams and can be of integer weight only.\nNow, 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:\nGiven 2 types of queries, the first one being of the form 1 X T, indicates an order of weight X is received at time T. \n\nThe second one is of the form 2 K T indicates that the data analytics team asks you about the summation of weight of the orders received during the last K minutes at time T. The first query can be of the first type only.\nFor each query, it is guaranteed that they are given in ascending order of time. It means that given q queries and each query having a time T associated with it, Ti<Ti+1<Ti+2…<Tq\n\nInput Format :\nThe first line contains a single integer q denoting the total number of queries. Each of the next q lines contains either of the two types of queries. Each query of type 1 first starts with the number 1 followed by 2 integers X and T. Each query of the second type first starts with the integer 2 and is then followed by 2 integers K and T.\n\nOutput Format :\nFor each query of the second type, you need to print the required answer on a new line.\nNote:\nThe first query can be of the first type only.

“TESTCASE_1”: “4\n1 20 3\n1 40 4\n2 5 5\n1 100 100\n###—###SEPERATOR—###—\n60”, “TESTCASE_2”: “5\n1 776871948 1\n2 53 82\n1 441598129 112\n2 136 166\n1 599424253 258\n###—###SEPERATOR—###—\n0\n441598129”, “TESTCASE_3”: “6\n1 815316197 1\n2 78 81\n1 440942150 176\n2 91 213\n1 28257485 309\n2 267 311\n###—###SEPERATOR—###—\n0\n440942150\n469199635”, “TESTCASE_4”: “0\n###—###SEPERATOR—###—\n0”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0

ANSWER

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi pair<ll,ll>
#define fi first
#define se second
int main()
{
ll t,w,T,k,d,ans,r,o,q;
vector<pi> P;
ll S[100002];
vector<pi>::iterator it;
S[d]=0;
cin>>q;
while(q--)
{
cin>>t;
if(t==1)
{
cin>>w>>T;
P.push_back({T,w});
d++;
S[d]=S[d-1]+w;
}
else
{
cin>>k>>T;

//o=0;
pi x;
x.fi=T;
x.se=INT_MIN;
it=upper_bound(P.begin(),P.end(),x);
if(it==P.end())
o=S[d];
else
{
o=S[it-P.begin()];
}
x.fi=T-k;
x.se=INT_MIN;
it=upper_bound(P.begin(),P.end(),x);
if(it==P.end())
r=S[d];
else
{
r=S[it-P.begin()];
}
ans=o-r;
cout<<ans<<endl;
}
}
  return 0;
}
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
CHP Adblock Detector Plugin | Codehelppro