Pagini recente » Istoria paginii runda/ojipreg | Cod sursa (job #734919) | Cod sursa (job #752521) | Cod sursa (job #1181841) | Cod sursa (job #658254)
Cod sursa(job #658254)
#include <cstdio>
#include <vector>
#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];
bool H[SqrtMax][SMax];
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[Tree[j]]]=0;
V[Tree[j]]+=ListV[i];
}
ListV[i]=0;
int Start=max (ListL[i], UL), End=min (ListR[i], UR);
for (int j=Start; j<=End; ++j)
{
V[Tree[j]]+=UV;
}
for (int j=ListL[i]; j<=ListR[i]; ++j)
{
H[i][V[Tree[j]]]=1;
}
}
}
inline int Query (int UV)
{
for (int i=0; i<NList; ++i)
{
if (UV<ListV[i])
{
continue;
}
if (H[i][UV-ListV[i]])
{
for (int j=ListL[i]; j<=ListR[i]; ++j)
{
if (V[Tree[j]]+ListV[i]==UV)
{
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;
}