Submission #467015

#TimeUsernameProblemLanguageResultExecution timeMemory
467015Theo830Fountain (eJOI20_fountain)C++17
100 / 100
413 ms61252 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll INF = 1e9+7;
ll MOD = 998244353;
typedef pair<ll,ll> ii;
#define iii pair<ii,ll>
#define f(i,a,b) for(ll i = a;i < b;i++)
#define pb push_back
#define vll vector<ll>
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
///I hope I will get uprating and don't make mistakes
///I will never stop programming
///sqrt(-1) Love C++
///Please don't hack me
///@TheofanisOrfanou Theo830
///Think different approaches (bs,dp,greedy,graphs,shortest paths,mst)
///Stay Calm
///Look for special cases
///Beware of overflow and array bounds
///Think the problem backwards
ll an[100005][30];
ll sum[100005][30];
ll d[100005],c[100005];
vector<vll>adj;
void build(ll s,ll p){
    an[s][0] = p;
    if(s-1 != -1){
        sum[s][0] = c[s-1];
    }
    else{
        sum[s][0] = 0;
    }
    f(i,1,30){
        an[s][i] = an[an[s][i-1]][i-1];
        sum[s][i] = sum[s][i-1] + sum[an[s][i-1]][i-1];
    }
    for(auto x:adj[s]){
        if(x != p){
            build(x,s);
        }
    }
}
int main(void){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    ll n,q;
    cin>>n>>q;
    f(i,0,n){
        cin>>d[i]>>c[i];
    }
    set<ii>ex;
    adj.assign(n+5,vll());
    f(i,0,n){
        auto it = ex.begin();
        set<ii>fkale;
        while(it != ex.end() && (*it).F < d[i]){
            adj[(*it).S].pb(i+1);
            adj[i+1].pb((*it).S);
            fkale.insert((*it));
            it++;
        }
        for(auto x:fkale){
            ex.erase(x);
        }
        ex.insert(ii(d[i],i+1));
    }
    for(auto x:ex){
        adj[0].pb(x.S);
        adj[x.S].pb(0);
    }
    build(0,0);
    while(q--){
        ll cur,v;
        cin>>cur>>v;
        for(ll j = 29;j >= 0;j--){
            if(sum[cur][j] < v){
                v -= sum[cur][j];
                cur = an[cur][j];
            }
        }
        cout<<cur<<"\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
*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...