Cod sursa(job #1222640)

Utilizator SerejaSereja Sereja Data 23 august 2014 22:13:38
Problema Arbore Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.23 kb
#include <fstream>
#include <vector>
#include <set>
#include <cmath>
#include <algorithm>
#define pist pair < int , set < int > >
using namespace std;
const int MOD = 1513;
const int NMAX = 100002;
struct Hash{
    vector < pist > Hash[MOD];
    inline void Insert(int x,const int pos){
        int h = x%MOD;
        for(vector < pist >::iterator it = Hash[h].begin(); it != Hash[h].end(); ++it)
            if(it->first==x){
                it->second.insert(pos);
                return ;
            }
        set < int > v;
        v.insert(pos);
        Hash[h].push_back(make_pair(x,v));
    }
    inline void Erase(const int x,const int pos){
        int h = x%MOD;
        int i = 0;
        for(vector < pist >::iterator it = Hash[h].begin(); it != Hash[h].end(); ++it, ++i)
            if(it->first == x){
                if(it->second.size() == 1){
                    swap(Hash[h][i],Hash[h][Hash[h].size()-1]);
                    Hash[h].pop_back();
                }
                else
                    it->second.erase(pos);
                return ;
            }
    }
    inline int Find(int x){
        int h = x%MOD;
        for(vector < pist >::iterator it = Hash[h].begin(); it != Hash[h].end();++it)
            if(it->first==x)
                return *(it->second.begin());
        return -1;
    }
};
vector < int > Tree[NMAX];
int Node[NMAX],c[NMAX], inf[302], val[NMAX], b[NMAX], nr, SQRT, n, m, ind, First[NMAX], Last[NMAX];
Hash a[320];
inline void Update(int x,int y,int s)
{
    int b1 = b[x];
    while(x <= y && c[x] == 0)
    {
        a[b1].Erase(val[x],Node[x]);
        a[b1].Insert(val[x]+s,Node[x]);
        val[x] += s;
        x++;
    }
    if(x > y)
        return ; 
    int b2 = b[y];
    while(c[y] == 0)
    {
        a[b2].Erase(val[y],Node[y]);
        a[b2].Insert(val[y]+s,Node[y]);
        val[y] += s;
        --y;
    }
    a[b2].Erase(val[y],Node[y]);
    a[b2].Insert(val[y]+s,Node[y]);
    val[y] += s;
    --y;
    --b2;
    for( ; b1 <= b2; ++b1) 
        inf[b1] += s;
}
inline int Solve(const int s)
{
    for(int i = 1;i <= nr; ++i)
    {
        int x = s - inf[i];
        if(x < 0)
            continue ;
        x = a[i].Find(x);
        if(x!=-1)
            return x;
    }
    return -1;
}
inline void DFS(const int node,const int Father)
{
    First[node] = ++ind;
    Node[ind] = node;
    for(vector < int > ::iterator it = Tree[node].begin() ; it != Tree[node].end() ; ++it)
        if(*it != Father)
            DFS(*it,node);
    Last[node] = ind;
}
int main(){
    ifstream f("arbore.in");
    ofstream g("arbore.out");
    f >> n >> m;
    SQRT = sqrt(n);
    for(int i = 1;i < n; ++i)
    {
        int x, y;
        f >> x >> y;
        Tree[x].push_back(y);
        Tree[y].push_back(x);
    }
    DFS(1,1);
    nr = 1;
    int r = 0;
    c[1] = 1;
    for(int i = 1;i <= n; ++i)
    {
        ++r;
        if(r==SQRT){
            r = 0,++nr;
            c[i]  = 1;
        }
        b[i] = nr;
        a[nr].Insert(0,Node[i]);
    }
    while(m--)
    {
        int op,x,s;
        f >> op;
        if(op==1)
        {
            f >> x >> s;
            Update(First[x],Last[x],s);
        }
        else{
            f >> s;
            g<<Solve(s)<<"\n";
        }
    }
    f.close();
    g.close();
    return 0;
}