Here you will find solutions of many problems on spoj. If you want solution of some problem which is not listed in blog or have doubt regarding any spoj problem (which i have solved) or any programming concept (data structure) you can mail me @ raj.nishant360@gmail.com

And my humble request to you all that don't copy the code only try to understand the logic and algorithm behind the code. I have started this because if you tried as hard as you can and still can't find any solution to the problem then you can refer to this.
You can read my answer how to start competitive programming CLICK HERE

Saturday, October 3, 2015

SID-Search Insert Delete

Search Insert Delete

Given below c++14 code is for sid spoj or search insert delete spoj.

Hint:- I just uses unordered_multimap container and whatever is said in question I have implemented that.
Time:- 0.23 sec




/*
===================================================
Name :- Nishant Raj
Email :- raj.nishant360@gmail.com
College :- Indian School of Mines
Branch :- Computer Science and Engineering
Time :- 03 October 2015 (Saturday) 08:52
===================================================*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair < int , int >
#define pb push_back
#define mp make_pair
#define mod 1000000009
#define gc getchar_unlocked
void scanint(int &x)
{
    register int c = gc();
    x = 0;
    int neg = 0;
    for(;((c<48 || c>57) && c != '-');c = gc());
    if(c=='-') {neg=1;c=gc();}
    for(;c>47 && c<58;c = gc()) {x = (x<<1) + (x<<3) + c - 48;}
    if(neg) x=-x;
}
void scanchar(char &c){
    register int x = gc();
    for(;(x < 65 || x > 97);x = gc());
    c = x;
}
int main(){
    
    int n , m , temp;
    scanint(n);
    scanint(m);
    unordered_multimap<int , int > save;
    for(int i = 1 ; i<= n ; i++){
        scanint(temp);
        save.insert(mp(temp , i));
    }
    int val;
    char type;
    int initial_size = n;
    while(m--){
        scanchar(type);
        if(type == 'S'){
            scanint(val);
            auto it = save.equal_range(val);
            if(it.first != save.end()){
                auto x = it.first;
                auto y = it.first;
                y++;
                while(y!= it.second){
                    x = y;
                    y++;
                }
                printf("%d\n" , x->second);
            } else{
                printf("NONE\n");
            }
        } else if(type == 'I'){
            scanint(val);
            initial_size++;
            save.insert(mp(val , initial_size));
            printf("%d\n",initial_size);
        } else {
            scanint(val);
            auto it = save.equal_range(val);
            if(it.first!= save.end()){
                auto x = it.first;
                auto y = it.first;
                y++;
                while(y!= it.second){
                    x = y;
                    y++;
                }
                printf("%d\n" , x->second);
                save.erase(x);
            } else{
                printf("NONE\n");
            }
        }
    }
    return 0;
} 

No comments:

Post a Comment

Your comment is valuable to us