#include<iostream>
#include<sstream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cctype>
#include<string>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
#include<functional>
#include<unordered_map>
#include<numeric>
using namespace std;
#define ll long long
#define mod 1e9+7
#define cy std::cout << "YES"<< std::endl
#define cn std::cout << "NO"<< std::endl
#define IN(x) cin >> x;
#define OUT(x) cout << x;
#define all(arr) arr.begin(),arr.end()
#define rep(a,b) for(int i = a;i<b;i++);
#define nline std::cout <<std::endl;
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
// void binarySearch(vector<int> ranges,int letter,int m){
// int left = 0;
// int right = m;
// }
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt","r", stdin);
freopen("output.txt","w", stdout);
#endif//
int m,n;
std::cin >> m >> n;
vector<int> rooms(m);
vector<int> letter(n);
//input for rooms
for (int i = 0; i < m; i++)
std::cin >> rooms[i];
//ip for letter
for (int i = 0; i < n; i++)
std::cin >> letter[i];
//making the ranges
vector<int> ranges;
int start = 0;
for (int i = 0; i < m; i++)
{
start += rooms[i];
ranges.push_back(start);
}
// 10 25 37
for (int i = 0; i < n; i++)
{
cout <<lower_bound(ranges.begin(),ranges.end(),letter[i])-ranges.begin()+1<<" " <<letter[i]-ranges[lower_bound(ranges.begin(),ranges.end(),letter[i]) - ranges.begin()-1]<<endl;
}
return 0;
}
i dont know why but i am getting garbage value when i try to submit the solution on codeforces but it runs correctly on my local machine
In first and second test cases, letters values are smaller than first value in rooms, so they are smaller than first value in ranges, so lower_bound(ranges.begin(),ranges.end(),letter[i]) — ranges.begin()-1 is equal to -1, but ranges[-1] is out of bounds.
what should i do to fix this?
If it is ranges[-1], just output letters value. for an example, if letters value is 9 and smallest ranges value is 10, than output 9.
yea i got the bug. submitted it again with a if else and its running now. Thanks for the help!