Pagini recente » Cod sursa (job #641430) | Cod sursa (job #2305124) | Cod sursa (job #1478360) | Cod sursa (job #640075) | Cod sursa (job #2038857)
#include <bits/stdc++.h>
const int MAX_N = 100000;
const int BUCK = 448;
const int MAX_S = 1000000;
std::vector<int> graph[1+MAX_N];
int top;
int first[1+MAX_N], last[1+MAX_N];
int liniar[2*MAX_N];
int val[1+MAX_N];
void dfs(int nod, int papa) {
first[nod] = top;
liniar[top++] = nod;
for(auto it: graph[nod])
if(it != papa)
dfs(it, nod);
last[nod] = top;
top++;
}
struct Bucket {
int fr[1+MAX_S];
int dif;
} buck[BUCK];
void clearbuck(int b) {
for(int i = b * BUCK; i < (b + 1) * BUCK && i < top; ++i)
if(0 <= val[liniar[i]] && val[liniar[i]] <= MAX_S)
buck[b].fr[val[liniar[i]]] = 0;
}
void updbuck(int b) {
for(int i = b * BUCK; i < (b + 1) * BUCK && i < top; ++i)
if(0 <= val[liniar[i]] && val[liniar[i]] <= MAX_S)
buck[b].fr[val[liniar[i]]] = liniar[i];
}
void updbuckrange(int l, int r, int valupd) {
int b = l / BUCK;
clearbuck(b);
for(int i = l; i <= r; ++i)
val[liniar[i]] += valupd;
updbuck(b);
}
void update(int l, int r, int valupd) {
int buckl = l / BUCK, buckr = r / BUCK;
if(buckl == buckr)
updbuckrange(l, r, valupd);
else {
updbuckrange(l, (buckl + 1) * BUCK - 1, valupd);
updbuckrange(buckr * BUCK, r, valupd);
for(int i = buckl + 1; i < buckr; ++i)
buck[i].dif += valupd;
}
}
int main() {
int n, m, a, b, tip;
FILE *fin = fopen("arbore.in", "r");
FILE *fout = fopen("arbore.out", "w");
fscanf(fin, "%d%d", &n, &m);
for(int i = 0; i < n - 1; ++i) {
fscanf(fin, "%d%d", &a, &b);
graph[a].push_back(b);
graph[b].push_back(a);
}
dfs(1, -1);
val[0] = MAX_S + 1;
for(int i = 0; i < m; ++i) {
fscanf(fin, "%d", &tip);
if(tip == 1) {
fscanf(fin, "%d%d", &a, &b);
update(first[a], last[a], b);
} else {
int rez = -1;
fscanf(fin, "%d", &a);
b = 0;
while(rez == -1 && b < top / BUCK + 1) {
int s = a - buck[b].dif;
if(0 <= s && buck[b].fr[s] != 0)
rez = buck[b].fr[s];
++b;
}
fprintf(fout, "%d\n", rez);
}
}
fclose(fin);
fclose(fout);
return 0;
}