#include <bits/stdc++.h>
using namespace std;
const int nx=1e5+5;
int n, q, w[nx], sz[nx], ans[2*nx], p, x;
vector<pair<int, int>> d[nx];
stack<int> st;
struct segtree
{
int d[4*nx], lz[4*nx];
void pushlz(int l, int r, int i)
{
if (l==r) return d[i]+=lz[i], lz[i]=0, void();
lz[2*i]+=lz[i];
lz[2*i+1]+=lz[i];
d[i]+=lz[i];
lz[i]=0;
}
void update(int l, int r, int i, int ql, int qr, int vl)
{
pushlz(l, r, i);
if (qr<l||r<ql) return;
if (ql<=l&&r<=qr) return lz[i]+=vl, pushlz(l, r, i), void();
int md=(l+r)/2;
update(l, md, 2*i, ql, qr, vl);
update(md+1, r, 2*i+1, ql, qr, vl);
d[i]=max(d[2*i], d[2*i+1]);
}
int query(int l, int r, int i, int vl)
{
pushlz(l, r, i);
if (l==r) return l;
int md=(l+r)/2;
pushlz(l, md, 2*i);
pushlz(md+1, r, 2*i+1);
if (d[2*i]>=vl) return query(l, md, 2*i, vl);
else return query(md+1, r, 2*i+1, vl);
}
} s;
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>q;
for (int i=1; i<=n; i++) cin>>w[i]>>sz[i];
w[n+1]=sz[n+1]=1e9;
for (int i=1; i<=q; i++) cin>>p>>x, d[p].push_back({x, i});
for (int i=n+1; i>=1; i--)
{
while (!st.empty()&&w[st.top()]<=w[i]) s.update(1, n+1, 1, st.top(), n+1, -sz[st.top()]), st.pop();
s.update(1, n+1, 1, i, n+1, sz[i]);
st.push(i);
for (auto [x, idx]:d[i]) ans[idx]=s.query(1, n+1, 1, x);
}
for (int i=1; i<=q; i++) cout<<((ans[i]==n+1)?0:ans[i])<<'\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4576 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
2 ms |
4616 KB |
Output is correct |
5 |
Correct |
2 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
4444 KB |
Output is correct |
7 |
Correct |
2 ms |
4580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
81 ms |
12752 KB |
Output is correct |
2 |
Correct |
90 ms |
13340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4576 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
2 ms |
4616 KB |
Output is correct |
5 |
Correct |
2 ms |
4444 KB |
Output is correct |
6 |
Correct |
2 ms |
4444 KB |
Output is correct |
7 |
Correct |
2 ms |
4580 KB |
Output is correct |
8 |
Correct |
81 ms |
12752 KB |
Output is correct |
9 |
Correct |
90 ms |
13340 KB |
Output is correct |
10 |
Correct |
2 ms |
4696 KB |
Output is correct |
11 |
Correct |
60 ms |
9720 KB |
Output is correct |
12 |
Correct |
147 ms |
15240 KB |
Output is correct |
13 |
Correct |
135 ms |
15188 KB |
Output is correct |
14 |
Correct |
115 ms |
14416 KB |
Output is correct |
15 |
Correct |
120 ms |
14676 KB |
Output is correct |