Cod sursa(job #1072384)

Utilizator CosminRusuCosmin Rusu CosminRusu Data 4 ianuarie 2014 13:31:56
Problema Arbore Scor 100
Compilator cpp Status done
Runda Lista lui wefgef Marime 3.88 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 MAXSQRT = 330;
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], Ans, K, maxBucket, Array, Left[MAXSQRT], Right[MAXSQRT], sumArray[MAXSQRT], v[MAXN];
Graph G;
bitset <10*MAXN> Hash[MAXSQRT];

class Scanner {
  public:
    Scanner(string file, int buffer_size = 32768):
            buffer_size_(buffer_size) {
        file_ = fopen(file.c_str(), "r");
        buffer_ = new char[buffer_size];
        pointer_ = buffer_ + buffer_size_;
    }

    Scanner& operator>>(int &object) {
        object = 0;
        while (next() < '0' or next() > '9')
            advance();
        while (next() >= '0' and next() <= '9') {
            object = object * 10 + next() - '0';
            advance();
        }
        return *this;
    }

  private:
    char next() {
        if (pointer_ == buffer_ + buffer_size_) {
            pointer_ = buffer_;
            fread(buffer_, 1, buffer_size_, file_);
        }
        return *pointer_;
    }

    void advance() {
        ++pointer_;
    }

    FILE *file_;
    int buffer_size_;
    char *buffer_, *pointer_;
};
Scanner fin(infile);

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

inline void BuildArrays(void) {
    for(maxBucket = 1; maxBucket * maxBucket <= N ; ++ maxBucket);
    --maxBucket;
    for(int i = 1 ; i <= N ; i += maxBucket) {
        ++ Array;
        Left[Array] = i;
        Right[Array] = min(i + maxBucket - 1, N);
        Hash[Array][0] = 1;
    }
}

inline void Update(const int &X, const int &Y, const int &S) {
    for(int i = 1 ; i <= Array ; ++ i) {
        if(Right[i] < X)
            continue;
        if(Left[i] > Y)
            break;
        if(X <= Left[i] && Right[i] <= Y) {/// inclusion
            sumArray[i] += S;
            continue;
        }
        for(int j = Left[i] ; j <= Right[i] ; ++ j) {
            Hash[i][v[j]] = 0;
            v[j] += sumArray[i] + (X <= j && j <= Y ? S : 0);
        }
        sumArray[i] = 0;
        for(int j = Left[i] ; j <= Right[i] ; ++ j)
            Hash[i][v[j]] = 1;
    }
}

inline int Query(const int &S) {
    for(int i = 1 ; i <= Array ; ++ i) {
        if(S < sumArray[i])
            continue;
        if(Hash[i][S - sumArray[i]]) {
            for(int j = Left[i] ; j <= Right[i] ; ++ j)
                if(sumArray[i] + v[j] == S)
                    return Liniarize[j];
        }
    }
    return -1;
}

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);
    BuildArrays();
    for(int i = 1 ; i <= M ; ++ i) {
        int q, x, y;
        fin >> q;
        if(q == 1) {
            fin >> x >> y;
            Update(First[x], Last[x], y);
        }
        else {
            fin >> x;
            fout << Query(x) << '\n';
        }
    }
    return 0;
}