Cod sursa(job #1283345)

Utilizator Al3ks1002Alex Cociorva Al3ks1002 Data 5 decembrie 2014 16:09:04
Problema Arbore Scor 0
Compilator cpp Status done
Runda Lista lui wefgef Marime 3.36 kb
#include<cstdio>
#include<fstream>
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<bitset>
#include<deque>
#include<queue>
#include<set>
#include<map>
#include<cmath>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<unordered_map>

#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>

using namespace std;

const int nmax = 100005;
const int smax = 320;

int n, q, i, j, x, y, cnt, lim, blocks, op, p, s;
int bucket[nmax], start[nmax], finish[nmax];
int a[nmax], first[nmax], last[nmax];
int add[smax], sum[nmax];

vector<int> v[nmax];
bitset<nmax> viz;
bitset<10 * nmax> b[smax];

void dfs(int x)
{
    viz[x] = 1;

    a[++cnt] = x;
    first[x] = cnt;

    for(auto it : v[x])
        if(!viz[it])
            dfs(it);

    last[x] = cnt;
}

void precalc()
{
    for(lim = 1; lim * lim <= n; lim++);
    cnt = lim;
    for(i = 1; i <= n; i++)
    {
        if(++cnt > lim)
        {
            cnt = 1;
            blocks++;
            start[blocks] = i;
            finish[blocks - 1] = i - 1;
        }
        bucket[i] = blocks;
        b[bucket[i]][sum[i]] = 1;
    }
    finish[blocks] = n;
}

void reset(int x, int y, int s)
{
    for(i = start[bucket[x]]; i <= finish[bucket[x]]; i++)
        b[bucket[x]][sum[i]] = 0;
    for(i = x; i <= y; i++)
        sum[i] += s;
    for(i = start[bucket[x]]; i <= finish[bucket[x]]; i++)
        b[bucket[x]][sum[i]] = 1;
}

void update(int p, int s)
{
    x = first[p];
    y = last[p];

    if(bucket[x] == bucket[y])
    {
        for(i = start[bucket[x]]; i <= finish[bucket[x]]; i++)
            b[bucket[x]][sum[i]] = 0;
        for(i = x; i <= y; i++)
            sum[i] += s;
        for(i = start[bucket[x]]; i <= finish[bucket[x]]; i++)
            b[bucket[x]][sum[i]] = 1;
    }
    else
    {
        for(i = start[bucket[x]]; i <= finish[bucket[x]]; i++)
            b[bucket[x]][sum[i]] = 0;
        for(i = x; i <= finish[bucket[x]]; i++)
            sum[i] += s;
        for(i = start[bucket[x]]; i <= finish[bucket[x]]; i++)
            b[bucket[x]][sum[i]] = 1;

        for(i = start[bucket[y]]; i <= finish[bucket[y]]; i++)
            b[bucket[y]][sum[i]] = 0;
        for(i = start[bucket[y]]; i <= y; i++)
            sum[i] += s;
        for(i = start[bucket[y]]; i <= finish[bucket[y]]; i++)
            b[bucket[y]][sum[i]] = 1;

        for(i = bucket[x] + 1; i <= bucket[y] - 1; i++)
            add[i] += s;
    }
}

int query(int s)
{
    for(i = 1; i <= blocks; i++)
        if(s - add[i] >= 0 && b[i][s - add[i]])
            for(j = start[i]; j <= finish[i]; j++)
                if(sum[j] + add[j] == s)
                    return a[j];
    return -1;
}

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

    scanf("%d%d", &n, &q);

    for(i = 1; i < n; i++)
    {
        scanf("%d%d", &x, &y);
        v[x].pb(y);
        v[y].pb(x);
    }

    dfs(1);
    precalc();

    for(; q; q--)
    {
        scanf("%d", &op);
        if(op == 1)
        {
            scanf("%d%d", &p, &s);
            update(p, s);
        }
        else
        {
            scanf("%d", &s);
            printf("%d\n", query(s));
        }
    }

    return 0;
}