Cod sursa(job #1995556)

Utilizator Mircea_DonciuDonciu Mircea Mircea_Donciu Data 28 iunie 2017 14:51:29
Problema Arbore Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.44 kb
#include <fstream>
#include <vector>
#include <bitset>
using namespace std;
ifstream f("arbore.in");
ofstream g("arbore.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()
{
    f>>n>>m;
    for(int i = 1; i < n; i++)
    {
        int x, y;
        f>>x>>y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
}
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 Step1(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 Step2(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;
}
void PreSolve()
{
    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 SolveandPrint()
{
    while(m--)
    {
        f>>opt;
        if(opt == 1)
        {
            int x,y;
            f>>x>>y;
            Step1(Arbore[1][x], Arbore[2][x], y);
        }
        else
        {
            int x;
            f>>x;
            g<<Step2(x)<<'\n';
        }
    }
}
int main()
{
    Read();
    PreSolve ();
    SolveandPrint();
    return 0;
}