#include <iostream>
#include <vector>
using namespace std;
const int MAXN = 2e5 + 5;
struct SmartStack
{
vector <pair <int, int>> v;
vector <int> prefSum;
int getSum(int ind)
{
if(ind==0) return prefSum.back();
return prefSum.back() - prefSum[ind-1];
}
void push(pair <int, int> x)
{
v.push_back(x);
if(prefSum.empty()==true) prefSum.push_back(x.second);
else
{
int sum = prefSum.back() + x.second;
prefSum.push_back(sum);
}
}
void pop()
{
v.pop_back();
prefSum.pop_back();
}
pair <int, int> top()
{
return v.back();
}
bool isEmpty()
{
return v.empty();
}
};
struct Query
{
int qInd, v;
Query(){}
Query(int qInd, int v)
{
this->qInd = qInd;
this->v = v;
}
};
int n;
int d[MAXN], c[MAXN];
vector <Query> queries[MAXN];
int ans[MAXN];
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
int Q;
cin >> n >> Q;
for(int i = 0;i<n;i++) cin >> d[i] >> c[i];
for(int q = 0;q<Q;q++)
{
int r, v;
cin >> r >> v;
r--;
queries[r].emplace_back(q, v);
}
SmartStack st;
for(int i = n-1;i>=0;i--)
{
while(st.isEmpty()==false && d[st.top().first]<=d[i]) st.pop();
st.push({i, c[i]});
for(Query &q: queries[i])
{
int v = q.v;
int currAns = 0;
for(int ind = st.v.size()-1;ind>=0;ind--)
{
v -= st.v[ind].second;
if(v<=0)
{
currAns = st.v[ind].first + 1;
break;
}
}
ans[q.qInd] = currAns;
}
}
for(int q = 0;q<Q;q++) cout << ans[q] << '\n';
}
/*
6 5
4 10
6 8
3 5
4 14
10 9
4 20
1 25
6 30
5 8
3 13
2 8
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
6 ms |
5068 KB |
Output is correct |
4 |
Correct |
6 ms |
5032 KB |
Output is correct |
5 |
Correct |
7 ms |
5068 KB |
Output is correct |
6 |
Correct |
6 ms |
5068 KB |
Output is correct |
7 |
Correct |
5 ms |
5068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1568 ms |
9756 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
6 ms |
5068 KB |
Output is correct |
4 |
Correct |
6 ms |
5032 KB |
Output is correct |
5 |
Correct |
7 ms |
5068 KB |
Output is correct |
6 |
Correct |
6 ms |
5068 KB |
Output is correct |
7 |
Correct |
5 ms |
5068 KB |
Output is correct |
8 |
Execution timed out |
1568 ms |
9756 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |