Pagini recente » Cod sursa (job #2893157) | Cod sursa (job #647569) | Cod sursa (job #1016579) | Cod sursa (job #2893162) | Cod sursa (job #989964)
Cod sursa(job #989964)
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
#define Nmax 100005
int N, Cnt, M, X, Y, Type, P, S, First[Nmax], Last[Nmax], K, V[Nmax], Rad, L[Nmax];
vector<int> G[Nmax];
bool Used[Nmax];
struct Chunk
{
int Left, Right, Added;
bool Used[1000010];
}C[350];
void DFS(int Node)
{
First[Node] = ++ K;
L[K] = Node;
Used[Node] = 1;
for(vector<int> :: iterator it = G[Node].begin(); it != G[Node].end(); ++ it)
if(!Used[*it])
DFS(*it);
Last[Node] = K;
}
void Update(int Left, int Right, int Sum)
{
for(int i = 1; i <= Cnt; ++ i)
{
if(C[i].Right < Left) continue;
if(Right < C[i].Left) break;
if(Left <= C[i].Left && C[i].Right <= Right)
{
C[i].Added += Sum;
continue;
}
for(int j = C[i].Left; j <= C[i].Right; ++ j)
{
C[i].Used[V[j]] = 0;
V[j] += C[i].Added;
if(Left <= j && j <= Right) V[j] += Sum;
}
for(int j = C[i].Left; j <= C[i].Right; ++ j)
C[i].Used[V[j]] = 1;
C[i].Added = 0;
}
}
int Query(int S)
{
for(int i = 1; i <= Cnt; ++ i)
if(C[i].Added <= S && C[i].Used[S - C[i].Added])
for(int j = C[i].Left; j <= C[i].Right; ++ j)
if(V[j] == S - C[i].Added)
return L[j];
return -1;
}
int main()
{
freopen("arbore.in", "r", stdin);
freopen("arbore.out", "w", stdout);
scanf("%i %i", &N, &M);
for(int i = 1; i < N; ++ i)
{
scanf("%i %i", &X, &Y);
G[X].push_back(Y);
G[Y].push_back(X);
}
DFS(1);
Rad = int(sqrt(N));
for(int i = 1; i <= N; )
{
Cnt ++;
C[Cnt].Left = i;
C[Cnt].Right = min(i + Rad - 1, N);
C[Cnt].Used[0] = 1;
i += Rad;
}
for(int i = 1; i <= M; ++ i)
{
scanf("%i", &Type);
if(Type == 1)
{
scanf("%i %i", &P, &S);
Update(First[P], Last[P], S);
}else
{
scanf("%i", &S);
printf("%i\n", Query(S));
}
}
return 0;
}