# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
199567 | SamAnd | Klasika (COCI20_klasika) | C++17 | 4152 ms | 460684 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
const int N = 200005;
int n, m;
pair<int, int> b[N];
vector<int> a[N];
int d[N];
int ti, tin[N], tout[N];
void dfs(int x)
{
tin[x] = ++ti;
for (int i = 0; i < a[x].size(); ++i)
{
int h = a[x][i];
dfs(h);
}
tout[x] = ti;
}
int z;
int t[N * 20][2];
set<int> s[N * 20];
void ubd(int x, int y)
{
int pos = 0;
for (int i = 30; i >= 0; --i)
{
int u;
if (!(y & (1 << i)))
u = 0;
else
u = 1;
if (!t[pos][u])
{
t[pos][u] = ++z;
}
pos = t[pos][u];
s[pos].insert(x);
}
}
int qry(int l, int r, int y)
{
int pos = 0;
int ans = 0;
for (int i = 30; i >= 0; --i)
{
int u;
if (!(y & (1 << i)))
u = 0;
else
u = 1;
if (t[pos][(u ^ 1)])
{
set<int>::iterator it = s[t[pos][(u ^ 1)]].lower_bound(l);
if (it != s[t[pos][(u ^ 1)]].end() && (*it) <= r)
{
ans += (1 << i);
pos = t[pos][(u ^ 1)];
}
else
pos = t[pos][u];
}
else
{
pos = t[pos][u];
}
}
return ans;
}
int main()
{
//freopen("input.txt", "r", stdin);
scanf("%d", &m);
n = 1;
for (int i = 1; i <= m; ++i)
{
char ty[10];
scanf(" %s", ty);
if (ty[0] == 'A')
{
int x, y;
scanf("%d%d", &x, &y);
++n;
a[x].push_back(n);
d[n] = (d[x] ^ y);
b[i] = m_p(n, -1);
}
else
{
int x, y;
scanf("%d%d", &x, &y);
b[i] = m_p(x, y);
}
}
dfs(1);
ubd(tin[1], d[1]);
for (int i = 1; i <= m; ++i)
{
if (b[i].second == -1)
{
int x = b[i].first;
ubd(tin[x], d[x]);
}
else
{
int x = b[i].first;
int y = b[i].second;
printf("%d\n", qry(tin[y], tout[y], d[x]));
}
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |