Cod sursa(job #2914513)

Utilizator JohnBoy97John Smith JohnBoy97 Data 20 iulie 2022 09:33:04
Problema Arbore Scor 75
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.4 kb
#include<bits/stdc++.h>
using namespace std;

void DBG() { cerr << "]\n"; }
template<class H, class... T> void DBG(H h, T... t) {
    cerr << h; if(sizeof...(t)) cerr << ", ";
    DBG(t...);
}
#ifdef LOCAL
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif // LOCAL

#define FOR(i,a,b) for(int i = (a) ; i<(b) ; i++)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for(int i = (b)-1 ; i>=(a) ; i--)
#define R0F(i,a) ROF(i,0,a)
#define each(e,a) for(auto &e : (a))
#define sz(v) (int)(v).size()
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define pb push_back
#define tcT template<class T
#define nl "\n"

using ll = long long;
using vi = vector<int>;
using pi = pair<int,int>;
using str = string;
tcT> using V = vector<T>;
tcT> using pqg = priority_queue<T,vector<T>,greater<T>>;

void setIO(string NAME = "") {
    cin.tie(0)->sync_with_stdio(0);
    if(sz(NAME)) {
        freopen((NAME + ".in").c_str(),"r",stdin);
        freopen((NAME + ".out").c_str(),"w",stdout);
    }
}

tcT> bool ckmin(T&a, const T&b) {
    return b < a ? a=b,1 : 0; }
tcT> bool ckmax(T&a, const T&b) {
    return b > a ? a=b,1 : 0; }

const int MOD = 1e9 + 7;

const int MX = 1e5+5, MX1 = 1e6+2, BLK = 317;
int N, M, tin[MX], tout[MX], trace[MX], _timer = 0, blID[MX], stBl[BLK+2], enBl[BLK+2], cntBl, sum[BLK+2];
vi adj[MX];
bitset<MX1> hav[BLK+2];
int curval[MX];

void dfs(int u, int p) {
    trace[tin[u] = ++_timer] = u;
    each(v, adj[u]) if(v != p) dfs(v, u);
    tout[u] = _timer;
}

void solve() {
    cin>>N>>M;
    F0R(i,N-1) {
        int u,v;
        cin>>u>>v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    dfs(1, -1);
    cntBl = 1;
    stBl[1] = 1;
    FOR(i,1,N+1) {
        blID[i] = cntBl;
        hav[blID[i]][0] = 1;
        if(i%BLK == 0) {
            enBl[cntBl] = i;
            if(i<N) stBl[++cntBl] = i+1;
        }
    }
    enBl[cntBl] = N;
    while(M-->0) {
        int t;
        cin>>t;
        if(t == 1) {
            int u, val;
            cin>>u>>val;
            int pt = tin[u];
            for(;pt <= min(tout[u], enBl[blID[tin[u]]]); pt++) {
                hav[blID[pt]][curval[pt]] = 0;
                curval[pt] += val;
            }
            FOR(i,stBl[blID[tin[u]]], enBl[blID[tin[u]]]+1) {
                hav[blID[i]][curval[i]] = 1;
            }
            for(; pt<=tout[u] && enBl[blID[pt]] <= tout[u]; pt = enBl[blID[pt]] + 1) {
                sum[blID[pt]] += val;
            }
            if(pt > N) continue;
            FOR(i,pt,tout[u]+1) {
                hav[blID[i]][curval[i]] = 0;
                curval[i] += val;
            }
            FOR(i, stBl[blID[pt]], enBl[blID[pt]]+1) {
                hav[blID[i]][curval[i]] = 1;
            }
        }
        else {
            int val;
            cin>>val;
            FOR(i,1,cntBl+1) {
                if(val >= sum[i] && hav[i][val-sum[i]]) {
                    FOR(j,stBl[i], enBl[i]+1) if(curval[j] == val - sum[i]) {
                        cout << trace[j] << nl;
                        goto done;
                    }
                }
            }
            cout << "-1\n";
            done:;
        }
    }
}

int main() {
    setIO("arbore");

    int t=1;
    //cin>>t;
    while(t-->0) {
        solve();
    }

    return 0;
}