Submission #1325291

#TimeUsernameProblemLanguageResultExecution timeMemory
1325291username_____hereFountain (eJOI20_fountain)C++20
0 / 100
273 ms111380 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> veci;
typedef vector<ll> vecll;
#define fi first
#define se second
#define vec vector
#define pq priority_queue

struct node{
    ll s,e,m,v;
    node *l,*r;
    node(ll _s, ll _e){
        s = _s;
        e = _e;
        m = (s+e)/2;
        v = INT_MAX; // change accordingly
        if (s != e){
            l = new node(s,m);
            r = new node(m+1,e);
        }
    }
    ll op(ll a, ll b) {
        // change accordingly
        return min(a,b);
    }
    void update(ll x,ll add){
        if (x > e) return;
        if (s != e){
            if (x <= m) l->update(x,add);
            else r->update(x,add);
            v = op(l->v, r->v);
        } else {
            v = add;
        }
    }
    int qry(ll x,ll y){
        if (s == x && e == y) return v;
        if (y <= m) return l->qry(x,y);
        if (x > m) return r->qry(x,y);
        return op(l->qry(x,m), r->qry(m+1,y));
    }
}*root;

const int maxn = 123456, loggg = 20;
int parent[maxn][loggg], sum[maxn]={0}, c[maxn], depth[maxn]={0};
veci adj[maxn];
int n;
//initialise all elements to -1;
void dfs(int u,int p){
    parent[u][0] = p;
    if (p != -1) {
        sum[u] = sum[p] + c[u];
        depth[u] = depth[p] + 1;
    }
    for (int i = 0; i < adj[u].size(); i++) {
        int v = adj[u][i];
        if (v != p) {
            dfs(v, u);
        }
    }
}

void decomp(){
    for (int k = 1; k < loggg; k++) {
        for (int i = 0; i <= n; i++) {
            if (parent[i][k-1] != -1) {
                parent[i][k] = parent[parent[i][k-1]][k-1];
            } else {
                parent[i][k] = -1;
            }
        }
    }
}

int find(int u, int k) {
    for (int i = loggg-1; i >= 0; i--) {
        if (k >= (1 << i)) {
            k -= (1 << i);
            u = parent[u][i];
        }
    }
    return u;
} 
int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);
    int n,q;
    cin >> n >> q;
    veci d(n, 0);
    int i;
    for (i=0; i<n; i++) {
        cin >> d[i] >> c[i];
    }
    veci d2 = d;
    sort(d2.begin(), d2.end());
    d2.erase(std::unique(d2.begin(), d2.end()), d2.end());
    map<int, int> compressed;
    for (i=0; i<d2.size(); i++) {
        compressed[d2[i]] = i;
    }
    for (i=0; i<n; i++) {
        d[i] = compressed[d[i]];
    }
    root = new node(0, 676767);
    veci next(n, 0);
    for (i=n-1; i>=0; i--) {
        root->update(d[i], i);
        next[i] = root->qry(d[i]+1, 676767);
        if (next[i] == INT_MAX) next[i] = n;
    }
    for (i=0; i<n; i++) {
        int a = i, b = next[i];
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    for (i=0; i<maxn; i++) {
        for (int j=0; j<loggg; j++) {
            parent[i][j] = -1;
        }
    }
    dfs(n, -1);
    decomp();
    while (q--) {
        int r, v;
        cin >> r >> v;
        r--;
        int lo=0, hi=depth[r];
        while (lo != hi) {
            int mid = (lo+hi)/2;
            int curr = find(r, mid);
            if (sum[r] - sum[parent[curr][0]] >= v) {
                hi = mid;
            } else {
                lo = mid + 1;
            }
        }
        cout << (find(r, lo)+1)%(n+1) << '\n';
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...