Cod sursa(job #2477742)

Utilizator FunnyStockyMihnea Andreescu FunnyStocky Data 21 octombrie 2019 07:16:42
Problema Arbore Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.08 kb
#include <iostream>
#include <cstdio>
#include <vector>
#include <bitset>
#include <algorithm>
#include <cmath>

using namespace std;

const int N = 100000 + 7;
const int L = 1000000 + 7;
const int ROFL = 333;

int n, q;
vector <int> g[N];
int ord[N], l[N], r[N], top;
bool used[N];
void dfs(int a)
{
        used[a] = 1;
        ord[++top] = a;
        l[a] = top;
        for (auto &b : g[a])
                if (used[b] == 0)
                        dfs(b);
        r[a] = top;
}


bitset <10000> is[ROFL];
int dif[ROFL], fi[ROFL], ls[ROFL];
int bucket[N], vals[N];

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

void half(int bucket_id, int l, int r, int x)
{
        for (int i = fi[bucket_id]; i <= ls[bucket_id]; i++)
        {
                is[bucket_id][vals[i]] = 0;
        }
        for (int i = fi[bucket_id]; i <= ls[bucket_id]; i++)
        {
                vals[i] += dif[bucket_id] + x * (l <= i && i <= r);
                is[bucket_id][vals[i]] = 1;
        }
        dif[bucket_id] = 0;
}

int getid(int x)
{
        for (int i = bucket[1]; i <= bucket[n]; i++)
                if (is[i][x - dif[i]])
                {
                        for (int j = fi[i]; j <= ls[i]; j++)
                                if (vals[j] == x - dif[i])
                                        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);

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

        for (int i = bucket[1]; i <= bucket[n]; i++)
                half(i, fi[i], ls[i], 0);

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






        return 0;
}