Submission #891754

# Submission time Handle Problem Language Result Execution time Memory
891754 2023-12-24T02:08:17 Z Fucsdeptrai Klasika (COCI20_klasika) C++17
33 / 110
620 ms 524288 KB
#include<bits/stdc++.h>

using namespace std;
using ll = long long;

const int N = 2e5 + 9;
const int Log = 30;

struct Node
{
    Node *c[2];
    int mn;

    Node()
    {
        c[0] = c[1] = NULL;
        mn = N;
    }
};

int n, q, Timer;
vector<pair<int, int>> adj[N];
int num[N], tail[N], pos[N], d[N];
vector<pair<pair<int, int>, int>> vt;
vector<pair<int, int>> vec[N*4];
Node *Trie[N*4];

void Add(int id, int x, int y)
{
    Node *p = Trie[id];
    for(int i = Log; i >= 0;i --)
    {
        bool w = (1<<i)&x;
        if(p->c[w] == NULL) p->c[w] = new Node;
        p = p->c[w];
        p->mn = min(p->mn, y);
    }
}

int Get(int id, int x, int y)
{
    Node *p = Trie[id];
    for(int i = Log; i >= 0;i --)
    {
        bool w = (1<<i)&x;
        if(p->c[w^1] != NULL and p->c[w^1]->mn <= y)
        {
            x ^= ((w^1)<<i);
            p = p->c[w^1];
        }
        else if(p->c[w] != NULL and p->c[w]->mn <= y)
        {
            x ^= (w<<i);
            p = p->c[w];
        }
        else
        {
            return 0;
        }
    }
    return x;
}

void Dfs(int u = 1, int du = 0)
{
    num[u] = ++ Timer;
    d[u] = du;
    pos[Timer] = u;
    for(auto [v, w] : adj[u])
    {
        Dfs(v, du^w);
    }
    tail[u] = Timer;
}

vector<pair<int, int>> tmp;

void Build(int id = 1, int l = 1, int r = n)
{
    if(l==r)
    {
        Trie[id] = new Node();
        Add(id, d[pos[l]], pos[l]);
        vec[id].push_back({d[pos[l]], pos[l]});
        return ;
    }

    int mid = l+r>>1;
    Build(id<<1, l, mid);
    Build(id<<1|1, mid+1, r);
    tmp.resize(vec[id<<1].size() + vec[id<<1|1].size());
    merge(vec[id<<1].begin(), vec[id<<1].end(), vec[id<<1|1].begin(), vec[id<<1|1].end(), tmp.begin());
    Trie[id] = new Node();
    int lst = -1;
    for(auto [x, y] : tmp)
    {
        if(x==lst) continue;
        Add(id, x, y);
        vec[id].push_back({x, y});
        lst = x;
    }
}

int Get(int L, int R, int x, int y, int id = 1, int l = 1, int r = n)
{
    if(l>r or l>R or L>r) return 0;
    if(L<=l and r<=R) return Get(id, x, y);

    int mid = l+r>>1;
    return max(Get(L, R, x, y, id<<1, l, mid), Get(L, R, x, y, id<<1|1, mid+1, r));
}

void Solve()
{
    cin>>q;
    ++n;
    while(q--)
    {
        string Type; cin>>Type;
        int x, y; cin >> x >> y;
        if(Type == "Add")
        {
            adj[x].push_back({++n, y});
        }
        else
        {
            vt.push_back({{x, y}, n});
        }
    }

    Timer = 0;
    Dfs();
    Build();

    for(auto [uv, w] : vt)
    {
        auto [u, v] = uv;
        cout << Get(num[v], tail[v], d[u], w) << '\n';
    }
}

signed main()
{
//	ios_base::sync_with_stdio(0);
//	cin.tie(0);

	int TC = 1;
//	cin>>TC;
    while(TC--)
    {
        Solve();
    }
}

Compilation message

klasika.cpp: In function 'void Build(int, int, int)':
klasika.cpp:88:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   88 |     int mid = l+r>>1;
      |               ~^~
klasika.cpp: In function 'int Get(int, int, int, int, int, int, int)':
klasika.cpp:109:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  109 |     int mid = l+r>>1;
      |               ~^~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 29276 KB Output is correct
2 Correct 7 ms 29532 KB Output is correct
3 Correct 7 ms 29788 KB Output is correct
4 Correct 8 ms 30044 KB Output is correct
5 Correct 7 ms 29020 KB Output is correct
6 Correct 7 ms 29532 KB Output is correct
7 Correct 7 ms 29788 KB Output is correct
8 Correct 7 ms 30044 KB Output is correct
9 Correct 7 ms 29268 KB Output is correct
10 Correct 7 ms 29532 KB Output is correct
11 Correct 8 ms 29860 KB Output is correct
12 Correct 9 ms 30456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 29276 KB Output is correct
2 Correct 7 ms 29532 KB Output is correct
3 Correct 7 ms 29788 KB Output is correct
4 Correct 8 ms 30044 KB Output is correct
5 Correct 7 ms 29020 KB Output is correct
6 Correct 7 ms 29532 KB Output is correct
7 Correct 7 ms 29788 KB Output is correct
8 Correct 7 ms 30044 KB Output is correct
9 Correct 7 ms 29268 KB Output is correct
10 Correct 7 ms 29532 KB Output is correct
11 Correct 8 ms 29860 KB Output is correct
12 Correct 9 ms 30456 KB Output is correct
13 Correct 13 ms 32604 KB Output is correct
14 Correct 16 ms 36408 KB Output is correct
15 Correct 23 ms 40792 KB Output is correct
16 Correct 23 ms 44684 KB Output is correct
17 Correct 11 ms 32348 KB Output is correct
18 Correct 15 ms 36308 KB Output is correct
19 Correct 19 ms 40436 KB Output is correct
20 Correct 22 ms 44500 KB Output is correct
21 Correct 12 ms 32348 KB Output is correct
22 Correct 15 ms 36188 KB Output is correct
23 Correct 22 ms 40540 KB Output is correct
24 Correct 30 ms 44388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 620 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 29276 KB Output is correct
2 Correct 7 ms 29532 KB Output is correct
3 Correct 7 ms 29788 KB Output is correct
4 Correct 8 ms 30044 KB Output is correct
5 Correct 7 ms 29020 KB Output is correct
6 Correct 7 ms 29532 KB Output is correct
7 Correct 7 ms 29788 KB Output is correct
8 Correct 7 ms 30044 KB Output is correct
9 Correct 7 ms 29268 KB Output is correct
10 Correct 7 ms 29532 KB Output is correct
11 Correct 8 ms 29860 KB Output is correct
12 Correct 9 ms 30456 KB Output is correct
13 Correct 13 ms 32604 KB Output is correct
14 Correct 16 ms 36408 KB Output is correct
15 Correct 23 ms 40792 KB Output is correct
16 Correct 23 ms 44684 KB Output is correct
17 Correct 11 ms 32348 KB Output is correct
18 Correct 15 ms 36308 KB Output is correct
19 Correct 19 ms 40436 KB Output is correct
20 Correct 22 ms 44500 KB Output is correct
21 Correct 12 ms 32348 KB Output is correct
22 Correct 15 ms 36188 KB Output is correct
23 Correct 22 ms 40540 KB Output is correct
24 Correct 30 ms 44388 KB Output is correct
25 Runtime error 620 ms 524288 KB Execution killed with signal 9
26 Halted 0 ms 0 KB -