Pagini recente » Cod sursa (job #1028867) | Cod sursa (job #1152130) | Cod sursa (job #1069099) | Cod sursa (job #1251247) | Cod sursa (job #658258)
Cod sursa(job #658258)
#include <cstdio>
#include <vector>
#include <bitset>
#include <algorithm>
#define NMax 100005
#define SqrtMax 320
#define SMax 1000005
using namespace std;
vector <int> G[NMax];
int N, V[NMax], Tree[NMax], TreeL[NMax], TreeR[NMax];
int NList, ListV[SqrtMax], ListL[SqrtMax], ListR[SqrtMax];
bitset <SMax> H[SqrtMax];
void BuildLists ()
{
for (NList=1; NList*NList<=N; ++NList); --NList;
int ListI=0;
for (int i=1; i<=N; ++ListI)
{
H[ListI][0]=1;
ListL[ListI]=i;
ListR[ListI]=min (N, i+NList-1);
i+=NList;
}
NList=ListI;
}
inline void Update (int UL, int UR, int UV)
{
for (int i=0; i<NList; ++i)
{
if (UR<ListL[i] or UL>ListR[i])
{
continue;
}
if (UL<=ListL[i] and ListR[i]<=UR)
{
ListV[i]+=UV;
continue;
}
for (int j=ListL[i]; j<=ListR[i]; ++j)
{
H[i][V[j]]=0;
V[j]+=(ListV[i]+UV*(UL<=j and j<=UR));
}
ListV[i]=0;
for (int j=ListL[i]; j<=ListR[i]; ++j)
{
H[i][V[j]]=1;
}
}
}
inline int Query (int Q)
{
for (int i=0; i<NList; ++i)
{
if (Q<ListV[i])
{
continue;
}
if (H[i][Q-ListV[i]])
{
for (int j=ListL[i]; j<=ListR[i]; ++j)
{
if (V[j]+ListV[i]==Q)
{
return Tree[j];
}
}
}
}
return -1;
}
inline void DFS (int X, int F)
{
Tree[++Tree[0]]=X;
TreeL[X]=Tree[0];
for (unsigned i=0; i<G[X].size (); ++i)
{
int V=G[X][i];
if (V!=F)
{
DFS (V, X);
}
}
TreeR[X]=Tree[0];
}
int main()
{
freopen ("arbore.in", "r", stdin);
freopen ("arbore.out", "w", stdout);
int M;
scanf ("%d %d", &N, &M);
for (int i=2; i<=N; ++i)
{
int X, Y;
scanf ("%d %d", &X, &Y);
G[X].push_back (Y);
G[Y].push_back (X);
}
DFS (1, 0);
BuildLists ();
for (; M>0; --M)
{
int Type, X, Y;
scanf ("%d %d", &Type, &X);
if (Type==1)
{
scanf ("%d", &Y);
Update (TreeL[X], TreeR[X], Y);
}
else
{
printf ("%d\n", Query (X));
}
}
return 0;
}