0

Problem :

Think about cars in a race as points on a line. All the cars start at the same point with an initial speed of zero. All of them move in the same direction. There are N cars in total, numbered from 1 to N.

You will be given two kinds of queries :

1. Change the speed of the car i at any time t
2. Output the current winner of the race at any time t

For query type 1, I will be given the time, Car No. and the New Speed.

For query type 2, I will be given the time at which the we need to find the winning car.

Constraints : N <= 50,000 , Queries <= 10^5
Also time in every query would be >= time in the previous query

What I tried till now :

#include<bits/stdc++.h>

using namespace std;

pair<int,pair<int,int> > arr[50005];//this stores car's last position,speed,time(at which this speed was assigned)
int main()
{
    int n,q;
    cin>>n>>q;
    for(int i=1;i<=n;++i){arr[i]={0,{0,0}};}
    while(q--)
    {
        int type;
        cin>>type;
        if(type==1)
        {
            int time,car,speed;
            cin>>time>>car>>speed;
            arr[car].first= arr[car].first + 1LL*(time-arr[car].second.second)*arr[car].second.first;// new position
            arr[car].second.first = speed;
            arr[car].second.second = time;
        }
        else
        {
            int ans=-1,time;
            cin>>time;
            for(int i=1;i<=n;++i){
                //position at the "time" is the last position plus distance travelled
                int temp = (time - arr[i].second.second)*arr[i].second.first + arr[i].first;
                // farthest car is the current winner 
                ans = max(ans,temp);
            }
            cout<<ans<<endl;
        }
    }
    return 0;
}


Since this approach answers the type 2 query in O(N) it is really ineffecient.

Since I only need an update operation and max range query I thought of using segment tree to speed this up to O(LogN).

I actually was halfway through my code when i realised that it isn't possible to answer query type 2 with this ! What i stored was essentially the same with an extra variable winning_car ! I thought for type 1 query I would just update the variables the same way i did above but for answering which car is winning at the moment I couldn't come up with a query function that would answer that in O(LogN). Either it is possible to write such a function ( which i am not able to) or i stored insufficient information in my node( I think it is this) !

What should I store in the tree node ? (or is it not possible with segment tree ?). Not asking for a ready-made code here, just the approach(if it is complex some pseudo-code would be nice though)

Sarvagya
  • 89
  • 8
  • Proper modelling of the problem (use a pencil an paper) –  Jan 06 '16 at 20:11
  • _"What am i missing here ?"_ is a pretty vague statement to describe that your code doesn't meet your expectations. Please elaborate about these, and what you've been trying for improvement, and where you actually failed at best. Don't expect to get ready made correct code written here for you. A simple `break;` when conditions are met would already help a lot BTW. – πάντα ῥεῖ Jan 06 '16 at 20:12
  • I actually was halfway through my code when i realised that it isn't possible to answer query type 2 with this ! What i stored was essentially the same with an extra variable winning_car ! I thought for type 1 query I would just update the variables the same way i did above but for answering which car is winning at the moment I couldn't come up with a query function that would answer that in O(LogN). Either it is possible to write such a function ( which i am not able to) or i stored insufficient information in my node( I think it is this) ! @πάνταῥεῖ – Sarvagya Jan 06 '16 at 20:17
  • @πάνταῥεῖ I am not expecting a ready-made code , all i need is the information to store in the tree-node and probably how to build the parent node using that information. – Sarvagya Jan 06 '16 at 20:22
  • @Alex _"What I'm missing here?"_ Is still too vague. There could be a lot of things you're missing. Add additional information of your efforts in the question, not in comments please. – πάντα ῥεῖ Jan 06 '16 at 20:25

0 Answers0