Cod sursa(job #2439247)

Utilizator bluestorm57Vasile T bluestorm57 Data 15 iulie 2019 14:42:51
Problema Arbore Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.27 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f("arbore.in");
ofstream g("arbore.out");

const int NMAX = 100005;
const int RMAX = 405;
vector <int> v[NMAX];
int n,m, lo[NMAX], hi[NMAX], father[NMAX], ans[NMAX], add[NMAX];
vector <int> ord;
pair<int,int>buck[RMAX];
int rad,k,b[NMAX], llo, hhi, bi, a[NMAX];
bitset <1000001> apar[RMAX];

void dfs(int nod){
    ord.push_back(nod);
    lo[nod] = ord.size();
    for(auto it: v[nod])
        if(it != father[nod]){
            father[it] = nod;
            dfs(it);
        }
    hi[nod] = ord.size();
}

int main(){
    int i,j,x,y,type;
    f >> n >> m;
    for(i = 1 ; i < n; i++){
        f >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    dfs(1);
    rad = sqrt(n);
    for(i = 1 ; i <= n ; i++){
        if(i % rad == 0){
            buck[k].second = i;
        }else
            if(i % rad == 1){
                buck[++k].first = i;
            }
        b[ord[i - 1]] = k;
    }
    if(buck[k].second == 0)
        buck[k].second = n;
    for(i = 1 ; i <= k ; i++)
        apar[i][0] = 1;
    while(m--){
        f >> type;
        if(type == 1){
            f >> x >> y;
            llo = lo[x];
            hhi = hi[x];
            bi = b[x];

            if (buck[b[x]].first <= llo && hhi <= buck[b[x]].second ){

                for (i = buck[b[x]].first ; i <= buck[b[x]].second ; i++){
                    apar[b[x]][a[i]] = 0;
                }
                for (i = llo ; i <= hhi ; i++ )
                    a[i] += y;

                for (i = buck[b[x]].first ; i <= buck[b[x]].second ; i++){
                    apar[b[x]][a[i]] = 1;
                }
                continue;
            }
            if (buck[b[x]].first < llo){

                for (i = buck[b[x]].first ; i <= buck[b[x]].second ; i++){
                    apar[b[x]][a[i]] = 0;
                }
                for (i = llo ; i <= buck[b[x]].second; i++ )
                    a[i] += y;

                for (i = buck[b[x]].first ; i <= buck[b[x]].second ; i++){
                    apar[b[x]][a[i]] = 1;
                }
                bi = b[x] + 1;
            }
            for (i = bi; i <= k && buck[i].second <= hhi ; i ++)
                add[i] += y;

            if (i <= k && buck[i].first <= hhi){ /// bucket de sf ne umplut in totalitate
                bi = i;
                for (i = buck[bi].first ; i <= buck[bi].second ; i++){
                    apar[bi][a[i]] = 0;
                }
                for (i = buck[bi].first ; i <= hhi ; i++)
                    a[i] += y;

                for (i = buck[bi].first ; i <= buck[bi].second ; i++){
                    apar[bi][a[i]] = 1;
                }
            }
        }

        else{
            f >> x;
            for(i = 1 ; i <= k ; i++)
                if(x - add[i] >= 0 && apar[i][x - add[i]]){
                    for(j = buck[i].first ; j <= buck[i].second ; ++j)
                        if(a[j] + add[i] == x){
                            g << ord[j - 1] << "\n";
                            break;
                        }
                    break;
                }
            if(i > k)
                g << -1 << "\n";
        }
    }

    return 0;
}