Cod sursa(job #2952832)

Utilizator JohnBoy97John Smith JohnBoy97 Data 10 decembrie 2022 05:23:26
Problema Arbore Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.23 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"
#define fi first
#define se second

using ll = long long;
using vi = vector<int>;
using pi = pair<int,int>;
using str = string;
using db = long double;
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 + ".inp").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 ll INF = 1e18;

const int MX = 1e5+5, MXX = 1e7+5;
const int BL = 317;

int N, M;
vi adj[MX];

bitset<MXX> hav[BL+5];

int tin[MX], tout[MX], tt, nodeid[MX];
int lazy[BL];
int A[MX];

void dfs(int u, int p) {
    nodeid[tin[u] = tt++] = u;
    each(v, adj[u]) if(v != p) {
        dfs(v, u);
    }
    tout[u] = tt-1;
}

int getStart(int ind) {
    ind/BL*BL;
}

void upd(int u, int toadd) {
    int l = tin[u], r = tout[u];
    for(int i = l; i <= r && i/BL == l/BL; i++) {
        hav[i/BL][A[i]] = 0;
        A[i] += toadd;
    }
    for(int i = l/BL*BL; i/BL == l/BL; i++) {
        hav[i/BL][A[i]] = 1;
    }
    int ind;
    for(ind = (l/BL+1)*BL; ind+BL-1 <= r; ind += BL) {
        lazy[ind/BL] += toadd;
    }
    if(ind < N) {
        for(int i = ind; i <= r; i++ ) {
            hav[i/BL][A[i]] = 0;
            A[i] += toadd;
        }
        for(int i = ind/BL*BL; i/BL == r/BL; i++)
            hav[i/BL][A[i]] = 1;
    }
}

int query(int need) {
    bool ok = false;
    F0R(i, BL) {
        if(need >= lazy[i] && hav[i][need - lazy[i]]) {
            ok = true;
            for(int j = i/BL*BL; j/BL == i/BL; j++) {
                if(A[j] + lazy[i] == need) {
                    return nodeid[j];
                }
            }
        }
    }
    return -1;
}

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);
    F0R(i, N) hav[i/BL][0] = 1;
    while(M-- > 0) {
        int t;
        cin >> t;
        if(t == 1) {
            int u, toadd;
            cin >> u >> toadd;
            upd(u, toadd);
        }
        else {
            int u;
            cin >> u;
            cout << query(u) << nl;
        }
    }
}

int main() {
    setIO();

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

    return 0;
}