답안 #843192

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
843192 2023-09-03T19:04:09 Z Andrijanikolic73 Fountain (eJOI20_fountain) C++17
0 / 100
330 ms 23720 KB
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+50;
int n;
int q;
int koliko[N];
int staje[N];
int up[N][21];
int suf[N];
stack<int>s;
int P[N];
int kti(int n,int k){
    for(int i=0;i<=20;i++){
        if(k&(1ll<<i))n=up[n][i];
    }
    return n;
}
signed main(){
    cin>>n>>q;
    for(int i=1;i<=n;i++)cin>>koliko[i]>>staje[i];
    for(int i=0;i<=n;i++)P[i]=0;
    for(int i=n;i>=1;i--){
        while(s.size()&&koliko[s.top()]<=koliko[i])s.pop();
        if(s.size())P[i]=s.top();
        s.push(i);
    }
    for(int i=0;i<=n;i++)suf[i]=0;
    for(int i=1;i<=n;i++)if(!P[i])suf[i]=staje[i];
    for(int i=n;i>=1;i--){
        if(P[i])suf[i]=suf[P[i]]+staje[i];
    }
    for(int i=0;i<=n;i++)for(int j=0;j<=20;j++)up[i][j]=0;
    for(int i=1;i<=n;i++){
        up[i][0]=P[i];

    }
    for(int i=1;i<=20;i++){
        for(int j=1;j<=n;j++){
            up[j][i]=up[up[j][i-1]][i-1];
        }
    }
    while(q--){
        int q,k;
        cin>>q>>k;
        if(suf[q]<k){
            cout<<0;
            cout<<endl;
            continue;
        }
        int l=0,r=20,p=0;
        while(l<=r){
            int mid=(l+r)/2;
            if(!kti(q,mid))r=mid-1;
            else {
                l=mid+1;
                p=mid;
            }
        }
        l=0,r=p;int R=-1;
        while(l<=r){
            int mid=(l+r)/2;
            int X=kti(q,mid);
            int y=P[X];
            if(suf[q]-suf[y]<k)l=mid+1;
            else {
                r=mid-1;
                R=mid;
            }
        }
        cout<<kti(q,R);
        cout<<endl;
    }
}
/*
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 1 ms 4440 KB Output is correct
2 Incorrect 2 ms 4440 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 330 ms 23720 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Incorrect 2 ms 4440 KB Output isn't correct
3 Halted 0 ms 0 KB -