답안 #657335

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
657335 2022-11-09T14:56:34 Z Banan Fountain (eJOI20_fountain) C++17
30 / 100
969 ms 11492 KB
#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 = (int)1e10;

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=INF;
        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)
        {
            if(r2.dma!=0 && r2.dmi!=INF){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]=INF;
    c[n+1]=INF;
    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 1 ms 212 KB Output is correct
2 Incorrect 1 ms 336 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 865 ms 11492 KB Output is correct
2 Correct 969 ms 11152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Incorrect 1 ms 336 KB Output isn't correct
3 Halted 0 ms 0 KB -