This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
int main()
{
long long N, Q; cin >> N >> Q;
vector<pair<long long, long long>> Fountain;
for(int i = 0; i < N; i++){
long long Di, Ci; cin >> Di >> Ci;
Fountain.push_back({Di, Ci});
}
bool Check = 1;
for(int i = 1; i < N; i++){
if(Fountain[i-1].first > Fountain[i].first) Check = 0;
}
if(Check){
reverse(Fountain.begin(), Fountain.end());
Fountain.push_back({0, 0});
reverse(Fountain.begin(), Fountain.end());
for(int i = 1; i <= N; i++){
Fountain[i].second += Fountain[i-1].second;
}
for(int i = 0; i < Q; i++){
long long Ri, Vi; cin >> Ri >> Vi;
int L = Ri - 1, R = N;
if(Vi > Fountain[N].second){
cout << 0 << '\n';
break;
}
while(L <= R){
long long Mid = (L + R) / 2;
if(Fountain[Mid].second - Fountain[Ri-1].second == Vi){
R = Mid - 1; L = Mid;
}
else if(Fountain[Mid].second - Fountain[Ri-1].second > Vi){
R = Mid - 1;
}
else{
L = Mid + 1;
}
}
cout << L << '\n';
}
return 0;
}
for(int i = 0; i < Q; i++){
long long Ri, Vi; cin >> Ri >> Vi;
long long LastD = 0;
for(int j = Ri-1; j < N; j++){
if(Ri == N){
cout << (Vi > Fountain[j].second ? 0 : j) << '\n';
break;
}
if(Fountain[j].first > LastD){
Vi -= Fountain[j].second;
LastD = Fountain[j].first;
}
if(Vi < 1){
cout << j + 1 << '\n';
break;
}
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |