Pagini recente » Cod sursa (job #1251732) | Cod sursa (job #3225007) | Cod sursa (job #1534330) | Cod sursa (job #2341784) | Cod sursa (job #3225417)
#include <fstream>
#include <vector>
#include <set>
using namespace std;
ifstream cin("arbore.in");
ofstream cout("arbore.out");
using ll = long long;
using pii = pair<int,int>;
const int nmax = 1e5 + 1;
const int buc = 316;
multiset <int> um[buc + 1];
vector <int> g[nmax];
int n , q , op , a , b , tin[nmax] , tout[nmax] , tm , e[nmax] , m;
int v[nmax] , lz[buc + 1];
void dfs(int x , int p)
{
tin[x] = ++tm;
e[tm] = x;
for(auto it : g[x])
{
if(it == p) continue;
dfs(it,x);
}
tout[x] = tm;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> n >> m;
for(int i = 2 ; i <= n ; ++i)
{
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
for(int i = 1 ; i <= n ; ++i)
{
um[i/buc].insert(0);
v[i] = 0;
}
dfs(1,0);
int mb = n/buc;
for(int i = 1 ; i <= m ; ++i)
{
cin >> op >> a;
if(op == 1)
{
cin >> b;
int sb = tin[a]/buc , eb = tout[a]/buc;
if(sb == eb)
{
for(int j = tin[a] ; j <= tout[a] ; ++j)
{
um[sb].erase(um[sb].find(v[j]));
v[j] += b;
um[sb].insert(v[j]);
}
}
else
{
int cs = (sb+1)*buc - 1 , cd = eb*buc;
for(int j = sb + 1 ; j < eb ; ++j) lz[j] += b;
for(int j = tin[a] ; j <= cs ; ++j)
{
um[sb].erase(um[sb].find(v[j]));
v[j] += b;
um[sb].insert(v[j]);
}
for(int j = tout[a] ; j >= cd ; --j)
{
um[eb].erase(um[eb].find(v[j]));
v[j] += b;
um[eb].insert(v[j]);
}
}
}
else
{
bool ok = 0;
for(int j = 0 ; j <= mb ; ++j)
{
if(um[j].find(a-lz[j]) != um[j].end())
{
ok = 1;
int start = max(j*buc,1);
int en = min(n,(j+1)*buc-1);
for(int l = start ; l <= en ; ++l)
{
if(v[l] == a-lz[j])
{
cout << e[l] << '\n';
break;
}
}
break;
}
}
if(!ok) cout << -1 << '\n';
}
}
return 0;
}