In this HackerEarth Timely Orders problem solution 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 types of queries, the first one being of the form 1 X T, indicates an order of weight X is received at time T. The 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.
For 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.
HackerEarth Timely Orders problem solution.
#include<bits/stdc++.h>
using namespace std;
int main(){
long long q;
cin>>q;
vector<pair<long long,long long>>ans;
while(q--){
long long a,b,c;
cin>>a>>b>>c;
if(a==1){
ans.push_back({c,b});
}else {
long long sum=0;
for(long long i=ans.size()-1;(ans[i].first>=(c-b))&&i>=0;i--){
sum+=ans[i].second;
}
cout<<sum<<"n";
}
}
}