Cod sursa(job #1072285)

Utilizator CosminRusuCosmin Rusu CosminRusu Data 4 ianuarie 2014 12:07:32
Problema Arbore Scor 65
Compilator cpp Status done
Runda Lista lui wefgef Marime 2.73 kb
/*Sursa mea are cea mai mare Va10are :)) */
/*   http://oananightshade.files.wordpress.com/2010/08/cocalar-012.jpg  */
#include <fstream>
#include <vector>
#include <bitset>

using namespace std;

const char infile[] = "arbore.in";
const char outfile[] = "arbore.out";

ifstream fin(infile);
ofstream fout(outfile);

const int MAXN = 100005;
const int oo = 0x3f3f3f3f;

typedef vector<int> Graph[MAXN];
typedef vector<int> :: iterator It;

const inline int min(const int &a, const int &b) { if( a > b ) return b;   return a; }
const inline int max(const int &a, const int &b) { if( a < b ) return b;   return a; }
const inline void Get_min(int &a, const int b)    { if( a > b ) a = b; }
const inline void Get_max(int &a, const int b)    { if( a < b ) a = b; }

int N, M, First[MAXN], Last[MAXN], Liniarize[MAXN << 1], Ans;
Graph G;
pair <int, int> Arb[MAXN << 3];

void DFs(const int &Node, const int &Father) {
    Liniarize[ ++ Liniarize[0] ] = Node;
    First[Node] = Liniarize[0];
    for(It it = G[Node].begin(), fin = G[Node].end(); it != fin ; ++ it) {
        if(Father == *it)
            continue;
        DFs(*it, Node);
    }
    Liniarize[ ++ Liniarize[0] ] = Node;
    Last[Node] = Liniarize[0];
}

void Update(const int &Node, const int &st, const int &dr, const int &ind, const int &value) {
    if(First[ind] <= st && dr <= Last[ind]) {
        Arb[Node].first += value;
    } else {
        int mid = ((st + dr) >> 1);
        if(First[ind] <= mid)
            Update(2*Node, st, mid, ind, value);
        if(mid < Last[ind])
            Update(2*Node + 1, mid + 1, dr, ind, value);
    }
    Arb[Node].second = Arb[Node].first + max(Arb[2*Node].second, Arb[2*Node + 1].second);
}

void Query(const int &Node, const int &st, const int &dr, const int &value) {
    if(Ans != -1)
        return;
    if(Arb[Node].first == value) {
        Ans = Liniarize[st];
        return;
    }
    if(st < dr && Arb[Node].first < value && Arb[Node].second >= value) {
        int mid = ((st + dr) >> 1);
        Query(2*Node, st, mid, value - Arb[Node].first);
        Query(2*Node + 1, mid + 1, dr, value - Arb[Node].first);
    }
}

int main() {
    fin >> N >> M;
    for(int i = 1 ; i != N ; ++ i) {
        int x, y;
        fin >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    DFs(1, 0);
    for(int i = 1 ; i <= M ; ++ i) {
        int q, x, y;
        fin >> q;
        if(q == 1) {
            fin >> x >> y;
            Update(1, 1, Liniarize[0], x, y);
        }
        else {
            Ans = -1;
            fin >> x;
            Query(1, 1, Liniarize[0], x);
            fout << Ans << '\n';
        }
    }
    fin.close();
    fout.close();
    return 0;
}