#include<bits/stdc++.h>
using namespace std;
long find_next(vector<long> d, long i){
long orig = d[i];
for(int j = i + 1; j < d.size(); j++) if(d[j] > orig) return j;
return -1;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long n, q;
cin >> n >> q;
vector<long> d(n);
vector<int> c(n);
for(long i = 0; i < n; i++) cin >> d[i] >> c[i];
if( n <= 1000 && q <= 2000){
vector<vector<pair<long, long>>> paths(n, vector<pair<long, long>>());
long tmp;
for(long i = 0; i < n; i++){
tmp = i;
while(tmp != -1){
paths[i].push_back(pair<long, long>(c[tmp], tmp));
tmp = find_next(d, tmp);
}
paths[i].push_back(pair<long, long>(LONG_MAX, -1));
}
for(long i = 0; i < n; i++){
for(long j = 1; j < paths[i].size() - 1; j++){
paths[i][j].first += paths[i][j - 1].first;
}
}
long res, v;
long l, r, m;
for(long i = 0; i < q; i++){
cin >> res >> v;
res--;
l = 0;
r = paths[res].size() - 1;
while(l < r){
m = (l + r) / 2;
if(paths[res][m].first >= v) r = m;
else l = m + 1;
}
cout << paths[res][r].second + 1 << '\n';
}
}
else{
vector<long long> pr(n + 1, 0);
for(long i = 1; i <= n; i++) pr[i] = pr[i - 1] + c[i - 1];
long res, v;
long l, r, m;
for(long i = 0; i < q; i++){
cin >> res >> v;
l = res;
r = n;
while(l < r){
m = (l + r) / 2;
if(pr[m] - pr[res - 1] >= v) r = m;
else l = m + 1;
}
cout << (r ^ n ? r : 0) << '\n';
}
}
return 0;
}
Compilation message
fountain.cpp: In function 'long int find_next(std::vector<long int>, long int)':
fountain.cpp:7:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
7 | for(int j = i + 1; j < d.size(); j++) if(d[j] > orig) return j;
| ~~^~~~~~~~~~
fountain.cpp: In function 'int main()':
fountain.cpp:32:31: warning: comparison of integer expressions of different signedness: 'long int' and 'std::vector<std::pair<long int, long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for(long j = 1; j < paths[i].size() - 1; j++){
| ~~^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
596 KB |
Output is correct |
3 |
Correct |
2 ms |
464 KB |
Output is correct |
4 |
Correct |
3 ms |
724 KB |
Output is correct |
5 |
Correct |
64 ms |
10616 KB |
Output is correct |
6 |
Correct |
23 ms |
4432 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
2924 KB |
Output is correct |
2 |
Correct |
70 ms |
5956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
596 KB |
Output is correct |
3 |
Correct |
2 ms |
464 KB |
Output is correct |
4 |
Correct |
3 ms |
724 KB |
Output is correct |
5 |
Correct |
64 ms |
10616 KB |
Output is correct |
6 |
Correct |
23 ms |
4432 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
54 ms |
2924 KB |
Output is correct |
9 |
Correct |
70 ms |
5956 KB |
Output is correct |
10 |
Incorrect |
1 ms |
328 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |