Pagini recente » Cod sursa (job #2742035) | Cod sursa (job #2222234) | Cod sursa (job #230614) | Cod sursa (job #2459835) | Cod sursa (job #944746)
Cod sursa(job #944746)
#include <fstream>
#include <algorithm>
#include <vector>
using namespace std;
int N, Q;
vector<int> V[100002];
bool S[100002];
int in[100002], out[100002];
int nod[100002], val[100002], add[350];
int sorted[100002];
int bucket;
void Dfs(int x)
{
S[x] = true;
nod[++nod[0]] = x;
in[x] = nod[0];
for (vector<int>::iterator it = V[x].begin(); it != V[x].end(); ++it)
if (!S[*it])
Dfs(*it);
out[x] = nod[0];
}
int main()
{
ifstream fin("arbore.in");
ofstream fout("arbore.out");
fin >> N >> Q;
for (int i = 1, nod1, nod2; i <= N - 1; ++i)
{
fin >> nod1 >> nod2;
V[nod1].push_back(nod2);
V[nod2].push_back(nod1);
}
Dfs(1);
for (; bucket * bucket < N; ++bucket);
for (int i = 1, type, valn, nodn; i <= Q; ++i)
{
fin >> type;
if (type == 1)
{
fin >> nodn >> valn;
int pos1 = in[nodn], pos2 = out[nodn];
for (int i = 1, b = 1; i <= N; i += bucket, ++b)
{
if (pos1 >= i && pos1 < i + bucket && pos2 >= i + bucket)
{
for (int j = i; j <= N && j <= i + bucket - 1; ++j)
val[j] += add[b];
add[b] = 0;
for (int j = pos1; j <= N && j <= i + bucket - 1; ++j)
val[j] += valn;
for (int j = i; j <= N && j <= i + bucket - 1; ++j)
sorted[j] = val[j];
sort(sorted + i, sorted + i + bucket);
}
else if (pos1 >= i && pos2 < i + bucket)
{
for (int j = i; j <= N && j <= i + bucket - 1; ++j)
val[j] += add[b];
add[b] = 0;
for (int j = pos1; j <= N && j <= pos2; ++j)
val[j] += valn;
for (int j = i; j <= N && j <= i + bucket - 1; ++j)
sorted[j] = val[j];
sort(sorted + i, sorted + i + bucket);
}
else if (pos2 >= i && pos2 < i + bucket)
{
for (int j = i; j <= N && j <= i + bucket - 1; ++j)
val[j] += add[b];
add[b] = 0;
for (int j = i; j <= N && j <= pos2; ++j)
val[j] += valn;
for (int j = i; j <= N && j <= i + bucket - 1; ++j)
sorted[j] = val[j];
sort(sorted + i, sorted + i + bucket);
}
else if (pos1 <= i && pos2 >= i + bucket)
add[b] += valn;
}
}
else
{
fin >> valn;
bool found = false;
for (int i = 1, b = 1; i <= N; i += bucket, ++b)
{
int searchnow = valn - add[b], step = (1 << 8), pos;
for (pos = i - 1; step; step >>= 1)
if (pos + step <= N && pos + step < i + bucket && sorted[pos + step] < searchnow)
pos += step;
if (pos == i + bucket - 1 || pos == N || sorted[pos + 1] != searchnow) pos = -1;
if (pos != -1)
{
for (int j = i; j <= N && j <= i + bucket - 1; ++j)
if (val[j] == searchnow)
{
fout << nod[j] << '\n';
break;
}
found = true;
break;
}
}
if (!found) fout << -1 << '\n';
}
}
fin.close();
fout.close();
}