#include <iostream>
#include <queue>
using namespace std;
struct P {
int d, v, n;
};
vector <P> x;
int N, Q;
bool ok = true;
int main(){
cin >> N >> Q;
x.assign(N + 1, { 0,0,0 });
for (int i = 1; i <= N; ++i) {
cin >> x[i].d >> x[i].v;
if (x[i].d <= x[i - 1].d) ok = false;
}
if (ok) {
vector<int> t(N + 1, 0);
for (int i = 1; i <= N; ++i)
t[i] = t[i - 1] + x[i].v;
int ind, vol;
while (Q--) {
cin >> ind >> vol;
auto it = lower_bound(t.begin(), t.end(), vol + t[ind - 1]);
if (it == t.end()) cout << 0;
else cout << it - t.begin();
cout << '\n';
}
}
else {
x[N].n = N + 1;
for (int i = N - 1; i >= 1; --i) {
int j = i + 1;
while (j != N + 1 && x[i].d >= x[j].d) {
j = x[j].n;
}
x[i].n = j;
}
int ind, vol;
while (Q--) {
cin >> ind >> vol;
int ans = ind;
while (ans != N + 1 && vol > 0) {
vol -= x[ans].v;
if (vol > 0) ans = x[ans].n;
}
if (ans == N + 1) cout << 0;
else cout << ans;
cout << '\n';
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
312 KB |
Output is correct |
4 |
Correct |
4 ms |
212 KB |
Output is correct |
5 |
Correct |
7 ms |
340 KB |
Output is correct |
6 |
Correct |
5 ms |
308 KB |
Output is correct |
7 |
Correct |
4 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
292 ms |
5368 KB |
Output is correct |
2 |
Correct |
340 ms |
5540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
312 KB |
Output is correct |
4 |
Correct |
4 ms |
212 KB |
Output is correct |
5 |
Correct |
7 ms |
340 KB |
Output is correct |
6 |
Correct |
5 ms |
308 KB |
Output is correct |
7 |
Correct |
4 ms |
212 KB |
Output is correct |
8 |
Correct |
292 ms |
5368 KB |
Output is correct |
9 |
Correct |
340 ms |
5540 KB |
Output is correct |
10 |
Correct |
4 ms |
340 KB |
Output is correct |
11 |
Correct |
1370 ms |
3324 KB |
Output is correct |
12 |
Correct |
440 ms |
7076 KB |
Output is correct |
13 |
Execution timed out |
1581 ms |
4092 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |