Submission #475462

# Submission time Handle Problem Language Result Execution time Memory
475462 2021-09-22T14:36:34 Z NSG Klasika (COCI20_klasika) C++14
110 / 110
2400 ms 417268 KB
///giangcbg - TST 2022.
#include <bits/stdc++.h>
#define reset(x, val) memset((x), (val), sizeof (x))
#define bit(X, i) (((X) >> (i)) & 1)
#define all(X) (X).begin(), (X).end()
#define on(X,i) (X|(1<<i))
#define off(X,i) (X^(1<<i))
#define cntbit(X) __builtin_popcountll(X)
#define pb push_back
#define ep emplace_back
#define fi first
#define se second
using namespace std;

const char ginp[]="KLASIKA.INP";
const char gout[]="KLASIKA.OUT";

void debug_out() { cerr << '\n'; }
template <typename Head, typename ...Tail>
void debug_out(Head H, Tail ...T)
{
    cerr << " " << H;
    debug_out(T...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

void read() {}
template <typename Head, typename ...Tail>
void read(Head &H, Tail &...T)
{
    register char c;
    bool sign = 0;
    for (c = getchar(); !isdigit(c); c = getchar())
        if (c == '-') sign = !sign;
    H = c - '0';
    for (c = getchar();  isdigit(c); c = getchar())
        H = H * 10 + c - '0';
    if (sign) H = -H;
    read(T...);
}

typedef long long ll;
typedef pair <int, int> ii;
typedef pair <int, ii> iii;
typedef vector <int> vi;
typedef vector <ii> vii;

mt19937 rd(chrono :: steady_clock :: now().time_since_epoch().count());
#define rand rd
ll Rand(ll l, ll h) { return l + rand() % (h - l + 1); }

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};

const int N = 2e5 + 5;
const int oo = 1e9 + 7;

struct query
{
    int t, a, b;
};

struct Trie
{
    Trie *c[2];
    set <int> s;
    Trie()
    {
        c[0] = c[1] = nullptr;
        s.clear();
    }
};

Trie *root;

int m,n;
query q[N];
vi g[N];
int in[N], out[N], f[N];

void enter()
{
    cin >> m;
    n = 1;
    f[1] = 0; ///sumXor
    for (int i = 1; i <= m; i++) 
    {
        string s;
        cin >> s >> q[i].a >> q[i].b;
        //debug(q[i].s, q[i].a, q[i].b);
        if (s == "Add") 
        {
            q[i].t = 1;
            ++n;
            g[q[i].a].pb(n);
            f[n] = f[q[i].a] ^ q[i].b; 
            q[i].a = n; q[i].b = f[n];
        }
        else q[i].t = 2;
    }
}

void dfs(int u)
{
    in[u] = ++in[0];
    for (int v : g[u]) dfs(v);
    out[u] = in[0];
}

inline void Add(int x, int time)
{
    Trie *p = root;
    for (int i = 29; i >= 0; i--)
    {
        int id = bit(x,i);
        if (p -> c[id] == nullptr) p -> c[id] = new Trie();
        p = p -> c[id];
        p -> s.insert(time);
    }
}

inline bool check(Trie *p, int b)
{
    auto it = p->s.lower_bound(in[b]);
    if (it == p->s.end() || *it > out[b]) return 0;
    return 1;
}

inline int Find(int a, int b)
{
    Trie *p = root;
    int res = 0;
    for (int i = 29; i >= 0; i--)
    {
        int id = bit(f[a],i);
        if (p -> c[!id] != nullptr && check(p -> c[!id], b))
        {
            res |= (1<<i);
            p = p -> c[!id];
        }
        else p = p -> c[id];
    }
    return res;
}

void solve()
{
    dfs(1);
    root = new Trie();
    Add(f[1], in[1]);
    for (int i = 1; i <= m; i++) 
    {
        if (q[i].t == 2) cout << Find(q[i].a, q[i].b) << '\n';
        else Add(q[i].b, in[q[i].a]);
    }
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0);

    #ifndef ONLINE_JUDGE
    
    //freopen(ginp, "r", stdin);
    //freopen(gout, "w", stdout);

    #endif // ONLINE_JUDGE

    enter();
    solve();

    cerr << "\nTime : " << clock() << "ms\n";
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5196 KB Output is correct
2 Correct 3 ms 5280 KB Output is correct
3 Correct 4 ms 5324 KB Output is correct
4 Correct 4 ms 5452 KB Output is correct
5 Correct 4 ms 5068 KB Output is correct
6 Correct 4 ms 5196 KB Output is correct
7 Correct 5 ms 5340 KB Output is correct
8 Correct 4 ms 5452 KB Output is correct
9 Correct 4 ms 5156 KB Output is correct
10 Correct 5 ms 5324 KB Output is correct
11 Correct 4 ms 5452 KB Output is correct
12 Correct 4 ms 5536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5196 KB Output is correct
2 Correct 3 ms 5280 KB Output is correct
3 Correct 4 ms 5324 KB Output is correct
4 Correct 4 ms 5452 KB Output is correct
5 Correct 4 ms 5068 KB Output is correct
6 Correct 4 ms 5196 KB Output is correct
7 Correct 5 ms 5340 KB Output is correct
8 Correct 4 ms 5452 KB Output is correct
9 Correct 4 ms 5156 KB Output is correct
10 Correct 5 ms 5324 KB Output is correct
11 Correct 4 ms 5452 KB Output is correct
12 Correct 4 ms 5536 KB Output is correct
13 Correct 6 ms 6348 KB Output is correct
14 Correct 8 ms 7628 KB Output is correct
15 Correct 10 ms 8880 KB Output is correct
16 Correct 13 ms 9932 KB Output is correct
17 Correct 7 ms 6348 KB Output is correct
18 Correct 8 ms 7500 KB Output is correct
19 Correct 9 ms 8780 KB Output is correct
20 Correct 11 ms 9892 KB Output is correct
21 Correct 7 ms 6348 KB Output is correct
22 Correct 8 ms 7628 KB Output is correct
23 Correct 9 ms 8752 KB Output is correct
24 Correct 11 ms 9904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 680 ms 118512 KB Output is correct
2 Correct 1159 ms 220624 KB Output is correct
3 Correct 1735 ms 318596 KB Output is correct
4 Correct 2230 ms 417204 KB Output is correct
5 Correct 665 ms 116300 KB Output is correct
6 Correct 1271 ms 217460 KB Output is correct
7 Correct 1731 ms 314472 KB Output is correct
8 Correct 2152 ms 411424 KB Output is correct
9 Correct 701 ms 115880 KB Output is correct
10 Correct 1174 ms 218280 KB Output is correct
11 Correct 1619 ms 316728 KB Output is correct
12 Correct 2059 ms 413080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 5196 KB Output is correct
2 Correct 3 ms 5280 KB Output is correct
3 Correct 4 ms 5324 KB Output is correct
4 Correct 4 ms 5452 KB Output is correct
5 Correct 4 ms 5068 KB Output is correct
6 Correct 4 ms 5196 KB Output is correct
7 Correct 5 ms 5340 KB Output is correct
8 Correct 4 ms 5452 KB Output is correct
9 Correct 4 ms 5156 KB Output is correct
10 Correct 5 ms 5324 KB Output is correct
11 Correct 4 ms 5452 KB Output is correct
12 Correct 4 ms 5536 KB Output is correct
13 Correct 6 ms 6348 KB Output is correct
14 Correct 8 ms 7628 KB Output is correct
15 Correct 10 ms 8880 KB Output is correct
16 Correct 13 ms 9932 KB Output is correct
17 Correct 7 ms 6348 KB Output is correct
18 Correct 8 ms 7500 KB Output is correct
19 Correct 9 ms 8780 KB Output is correct
20 Correct 11 ms 9892 KB Output is correct
21 Correct 7 ms 6348 KB Output is correct
22 Correct 8 ms 7628 KB Output is correct
23 Correct 9 ms 8752 KB Output is correct
24 Correct 11 ms 9904 KB Output is correct
25 Correct 680 ms 118512 KB Output is correct
26 Correct 1159 ms 220624 KB Output is correct
27 Correct 1735 ms 318596 KB Output is correct
28 Correct 2230 ms 417204 KB Output is correct
29 Correct 665 ms 116300 KB Output is correct
30 Correct 1271 ms 217460 KB Output is correct
31 Correct 1731 ms 314472 KB Output is correct
32 Correct 2152 ms 411424 KB Output is correct
33 Correct 701 ms 115880 KB Output is correct
34 Correct 1174 ms 218280 KB Output is correct
35 Correct 1619 ms 316728 KB Output is correct
36 Correct 2059 ms 413080 KB Output is correct
37 Correct 1250 ms 118048 KB Output is correct
38 Correct 1757 ms 219740 KB Output is correct
39 Correct 2140 ms 320716 KB Output is correct
40 Correct 2400 ms 417268 KB Output is correct
41 Correct 1291 ms 116084 KB Output is correct
42 Correct 1790 ms 216864 KB Output is correct
43 Correct 2102 ms 314268 KB Output is correct
44 Correct 2309 ms 410276 KB Output is correct
45 Correct 1381 ms 115704 KB Output is correct
46 Correct 2009 ms 217576 KB Output is correct
47 Correct 2269 ms 314776 KB Output is correct
48 Correct 2387 ms 412540 KB Output is correct