#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
#define double long double
#define endl '\n'
#define sz(a) (int)a.size()
#define pb push_back
#define fs first
#define sc second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
int const INF = LONG_LONG_MAX;
int n, q, d[200005], c[200005];
struct reserv
{
int dmi, dma, totc;
} tree[800008];
void build(int node, int l, int r)
{
if(l==r)
{
tree[node].dmi=d[l];
tree[node].dma=d[l];
tree[node].totc=c[l];
}
else
{
int mid=(l+r)/2;
build(2*node, l, mid);
build(2*node+1, mid+1, r);
tree[node].dmi=d[l];
if(tree[2*node].dma<tree[2*node+1].dmi)
{
tree[node].dma=tree[2*node+1].dma;
tree[node].totc=tree[2*node].totc+tree[2*node+1].totc;
}
else
{
tree[node].dma=tree[2*node].dma;
tree[node].totc=tree[2*node].totc;
}
}
}
reserv ask(int node, int l, int r, int sl, int sr)
{
if(l==sl && r==sr)
{
return tree[node];
}
if(l>sr || r<sl)
{
reserv x;
x.dmi=(int)1e10;
x.dma=0;
x.totc=0;
return x;
}
else
{
int mid=(l+r)/2;
reserv r1=ask(2*node, l, mid, sl, min(sr, mid));
reserv r2=ask(2*node+1, mid+1, r, max(mid+1, sl), sr);
reserv x;
x.dmi=r1.dmi;
if(r1.dma<r2.dmi)
{
x.dma=r2.dma;
x.totc=r1.totc+r2.totc;
}
else
{
x.dma=r1.dma;
x.totc=r1.totc;
}
return x;
}
}
void solve()
{
cin>>n>>q;
for(int i=1;i<=n;i++)
{
cin>>d[i]>>c[i];
}
d[n+1]=(int)1e10;
c[n+1]=(int)1e10;
build(1, 1, n+1);
while(q--)
{
int r, v;
cin>>r>>v;
int lo=r, hi=n+1, mid=(lo+hi)/2, ans;
while(lo<=hi)
{
reserv re=ask(1, 1, n+1, r, mid);
if(re.totc>=v){ans=mid;hi=mid-1;mid=(lo+hi)/2;}
else{lo=mid+1;mid=(lo+hi)/2;}
}
if(ans==n+1){cout<<0<<endl;}else{cout<<ans<<endl;}
}
}
int32_t main()
{
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int tc=1;
//cin>>tc;
while(tc--)
{
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
3 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
892 ms |
8728 KB |
Output is correct |
2 |
Correct |
1000 ms |
8660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
3 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |