Competitive coding

#include <iostream>
#include <map>
#include <vector>
#include <filesystem>
#include <fstream>
#include <string>
using namespace std;
namespace fs = std::filesystem;

std::string readFile(fs::path path)
{
    // Open the stream to 'lock' the file.
    std::ifstream f(path, std::ios::in | std::ios::binary);

    // Obtain the size of the file.
    const auto sz = fs::file_size(path);

    // Create a buffer.
    std::string result(sz, '\0');

    // Read the whole file into the buffer.
    f.read(result.data(), sz);

    return result;
}

int main()
{
     string line_str;
     fs::path p = "extents.txt";  
     string result = readFile(p);
     stringstream X(result);
     std::multimap<long long,long long> m;
     int i=0;
     while(getline(X,line_str))
     {
        i=0;
        stringstream Y(line_str);
        string number[2];
        while(getline(Y,number[i], ' '))
        {
                  
           i++;
        }
      //  std::cout << number[0] <<" "<< number[1] <<std::endl;
        m.insert(std::make_pair<long long, long long>(std::stoll(number[0]),std::stoll(number[1])));
     }

     string num_str;
     fs::path p2 = "numbers.txt";
     string number_buffer = readFile(p2);
     stringstream Z(number_buffer);
     std::vector<long long> v;

     while(getline(Z,num_str))
     {
          v.push_back(std::stoll(num_str));
     }
 
     for(auto iter=begin(v); iter != end(v); iter++)
     {
        auto count=0;
        for( const auto &[start ,end] : m)
        {
           if(start <= *iter && *iter <= end)
           {
               count++;
           }  
 
        }
        std::cout << count << '\n'; 
     }
}
 

Comments

Popular posts from this blog

VECPP.c

Comparision with Quick Sort

Interview questions