Cod sursa(job #2477661)

Utilizator FunnyStockyMihnea Andreescu FunnyStocky Data 20 octombrie 2019 21:24:08
Problema Arbore Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 4.13 kb
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <set>
#include <cmath>
#include <unordered_map>

using namespace std;

const int N = 100000 + 7;
int n, q, a[N];
vector <int> g[N];
bool u[N];
int ord[2 * N], top, l[N], r[N], bucket[2 * N];

void dfs(int a)
{
        u[a] = 1;
        ord[++top] = a;
        for (auto &b : g[a])
                if (u[b] == 0)
                {
                        dfs(b);
                        ord[++top] = a;
                }
}

const int ROFL = 450;
unordered_map <int, int> f[ROFL];
int scad[ROFL];
int fi[ROFL], ls[ROFL], s, vals[2 * N];

void del(int bucket_id, int x)
{
        auto it = f[bucket_id].find(x);

}

void all(int bucket_id, int x)
{
        scad[bucket_id] += x;
}

void half(int bucket_id, int l, int r, int x)
{
        if (scad[bucket_id])
        {
                for (int j = fi[bucket_id]; j <= ls[bucket_id]; j++)
                {
                        f[bucket_id][vals[j]]--;
                        vals[j] += scad[bucket_id];
                        f[bucket_id][vals[j]]++;
                }
                scad[bucket_id] = 0;
        }
        for (int j = l; j <= r; j++)
        {
                f[bucket_id][vals[j]]--;
                vals[j] += x;
                f[bucket_id][vals[j]]++;
        }
}

int getid(int x)
{
        for (int bucket_id = bucket[0]; bucket_id <= bucket[top]; bucket_id++)
        {
                int y = x - scad[bucket_id];
                if (f[bucket_id][y])
                {
                        if (scad[bucket_id])
                        {
                                for (int j = fi[bucket_id]; j <= ls[bucket_id]; j++)
                                {
                                        f[bucket_id][vals[j]]--;
                                        vals[j] += scad[bucket_id];
                                        f[bucket_id][vals[j]]++;
                                }
                                scad[bucket_id] = 0;
                        }
                        for (int j = fi[bucket_id]; j <= ls[bucket_id]; j++)
                                if (vals[j] == x)
                                        return ord[j];
                }
        }
        return -1;
}

int main()
{
        freopen ("arbore.in", "r", stdin);
        freopen ("arbore.out", "w", stdout);

        scanf("%d %d", &n, &q);
        for (int i = 1; i < n; i++)
        {
                int a, b;
                scanf("%d %d", &a, &b);
                g[a].push_back(b);
                g[b].push_back(a);
        }

        dfs(1);
        for (int i = 1; i <= top; i++)
        {
                if (l[ord[i]] == 0)
                        l[ord[i]] = i;
                r[ord[i]] = i;
        }

        s = sqrt(top);
        for (int i = 1; i <= top; i++)
                bucket[i] = (i - 1) / s;
        for (int i = 1; i <= top; i++)
        {
                if (fi[bucket[i]] == 0)
                        fi[bucket[i]] = i;
                ls[bucket[i]] = i;
        }

        while (q--)
        {
                int tp;
                scanf("%d", &tp);
                if (tp == 1)
                {
                        int a, x;
                        scanf("%d %d", &a, &x);
                        int __l = l[a], __r = r[a];
                        for (int j = bucket[__l] + 1; j < bucket[__r]; j++)
                                all(j, x);
                        if (bucket[__l] == bucket[__r])
                        {
                                half(bucket[__l], __l, __r, x);
                        }
                        else
                        {
                                half(bucket[__l], __l, ls[bucket[__l]], x);
                                half(bucket[__r], fi[bucket[__r]], __r, x);
                        }
                }
                else
                {
                        int x;
                        scanf("%d", &x);
                        printf("%d\n", getid(x));
                }
        }

        return 0;
}