Cod sursa(job #1784816)

Utilizator ArambasaVlad Arambasa Arambasa Data 20 octombrie 2016 15:27:02
Problema Arbore Scor 0
Compilator cpp Status done
Runda Lista lui wefgef Marime 2.21 kb

#include <fstream>



#include <vector>



#include <bitset>





using namespace std;



ifstream in("Arboreore.in");


ofstream out("Arboreore.out");




const int Nmax = 100005, Mmax = 320;

int n, m, opt, Arbore[3][Nmax], k, nlist, Lista[3][Mmax], v[Nmax];


bool Use[Nmax];


bitset <Nmax*10> BitVect[Mmax];


vector <int> G[Nmax];




void Read()


{
in>>n>>m;


for(int i = 1; i < n; i++)


{


int x, y;


in>>x>>y;


G[x].push_back(y);


G[y].push_back(x);

    

}
 
    

}
 
 

void PreSovle()
 
 
{
	
	
DFS(1);
	
	
for(nlist = 1; nlist*nlist <= n; nlist++);

	
		nlist--;
	
		
int ListIndex = 0;
 
 
for(int i = 1; i <= n; ListIndex++)
 
  
{


BitVect[ListIndex][0] = 1;


Lista[1][ListIndex] = i;


Lista[0][ListIndex] = i+nlist-1;


i += nlist;

      
  
}
 
 
nlist = ListIndex;
 
 
Lista[0][nlist-1] = n;

     
 
}



void DFS(int nod)


{


Use[nod] = 1;


Arbore[0][++k] = nod;


Arbore[1][nod] = k;


for(int i = 0; i < (int)G[nod].size(); i++)


{


int vecin = G[nod][i];


if(!Use[vecin]) DFS(vecin);




}


Arbore[2][nod] = k;


    

}


void SolveandPrint()
{
while(m--)


{


in>>opt;

if(opt == 1)
{


int x,y;

in>>x>>y;


Solve1(Arbore[1][x], Arbore[2][x], y);

    

}



else


{


int x;


in>>x;


out<<Solve2(x)<<'\n';

    

}

    

}


}



void Solve1(int &ul, int &ur, int &uv)


{


for(int i = 0; i < nlist; i++)


{


if(Lista[1][i] > ur || Lista[0][i] < ul)


continue;


if(ul <= Lista[1][i] && Lista[0][i] <= ur)


{

Lista[2][i] += uv;

continue;

    
}


for(int j = Lista[1][i]; j <= Lista[0][i]; j++)


{


BitVect[i][v[j]] = 0;


v[j] += (Lista[2][i]+uv*(ul<=j && j <= ur));

    

}


Lista[2][i] = 0;


for(int j = Lista[1][i]; j <= Lista[0][i]; j++)


{


BitVect[i][v[j]] = 1;

    

}

    

}

    

}






int Solve2(int &x)


{


for(int i =0; i < nlist; i++)


{


if(x < Lista[2][i]) continue;


if(BitVect[i][x-Lista[2][i]])


{


x -= Lista[2][i];


for(int j = Lista[1][i]; j <= Lista[0][i]; j++)


{


if(v[j] == x)

return Arbore[0][j];

    

}

   

}

    

}


return -1;

    

}




int main()


{


Read();


PreSolve ();


SolveandPrint();

return 0;

    

}