Cod sursa(job #1451667)

Utilizator ZenusTudor Costin Razvan Zenus Data 18 iunie 2015 00:14:49
Problema Arbore Scor 65
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.45 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("arbore.in");
ofstream fout("arbore.out");

const int SSIZE = 317 + 10;
const int NSIZE = 100000 + 10;
const int VSIZE = 1000000 + 10;

pair < int , int > p[NSIZE];
int add[SSIZE] , aux[NSIZE];
bitset < VSIZE > iH[SSIZE];
vector < int > iB[SSIZE];
int N , Q , x , y , type , i , crt , block;
vector < int > g[NSIZE];

int wB(int i)
{
    return i / block;
}

int wiB(int i)
{
    return i % block;
}

void df(int node,int fa)
{
    p[node].first = ++crt;
    aux[crt] = node;

    for (int i=0;i<g[node].size();++i)
    {
        int to = g[node][i];

        if (to != fa) df(to,node);
    }

    p[node].second = crt;
}

void build()
{
    int i;

    for (i = 0 ; i < N ; ++i)
    {
        iB[wB(i)].push_back(0);
        iH[wB(i)][0] = true;
    }
}

void update(int le,int ri,int x)
{
    int i , j1 , j2;

    if (le % block != 0)
    {
        j1 = wB(le) + 1;

        crt = wB(le);

        for (i = 0 ; i < iB[crt].size() ; ++i)
        {
            iH[crt][iB[crt][i]] = false;

            if (ri >= crt * block + i && crt * block + i >= le)
            iB[crt][i] += x;

            iH[crt][iB[crt][i]] = true;
        }

        if (wB(le) == wB(ri)) return ;
    } else j1 = wB(le);

    if ((ri + 1) % block != 0)
    {
        j2 = wB(ri) - 1;
        crt = wB(ri);

        for (i = 0 ; i < iB[crt].size() ; ++i)
        {
            iH[crt][iB[crt][i]] = false;

            if (ri >= crt * block + i && crt * block + i >= le)
            iB[crt][i] += x;

            iH[crt][iB[crt][i]] = true;
        }

        if (wB(le) == wB(ri)) return ;
    } else j2 = wB(ri);

    for (i = j1 ; i <= j2 ; ++i)
    add[i] += x;
}

int query(int x)
{
    int i , j;

    for (i = 0 ; i <= wB(N - 1) ; ++i)
    if (x >= add[i] && iH[i][x - add[i]])
    {
        for (j = 0 ; j < iB[i].size() ; ++j)
        if (iB[i][j] == x - add[i]) return aux[i * block + j];
    }

    return -1;
}

int main()
{

fin >> N >> Q;
block = (int)sqrt(1.0 * N);

for (i=1;i<N;++i)
{
    fin >> x >> y;
    g[x].push_back(y);
    g[y].push_back(x);
}

crt = -1;
df(1,0);
build();

for (i=1;i<=Q;++i)
{
    fin >> type;

    if (type == 1)
    {
        fin >> x >> y;
        update(p[x].first,p[x].second,y);
    }
    else
    {
        fin >> y;
        fout << query(y) << '\n';
    }
}

return 0;
}