Submission #620084

#TimeUsernameProblemLanguageResultExecution timeMemory
620084AlperenTMinerals (JOI19_minerals)C++17
40 / 100
215 ms12916 KiB
#include <bits/stdc++.h>
#include "minerals.h"

using namespace std;

const int N = 50000 + 5;

int n, ans[N], ptr, cnt;

vector<int> a, b;

set<int> cur;

int query(int x){
    if(cur.find(x) != cur.end()) cur.erase(x);
    else cur.insert(x);

    return cnt = Query(x);
}

struct SegTree{
    vector<int> tree[N * 4];

    void build(int v = 1, int l = 1, int r = n, bool flag = false){
        if(l == r) ans[l] = tree[v].front();
        else{
            int m = l + (r - l) / 2;

            while(ptr < m){
                ptr++;
                query(a[ptr - 1]);
            }

            while(ptr > m){
                query(a[ptr - 1]);
                ptr--;
            }

            if(l + 1 == r){
                if(flag){
                    int prvcnt = cnt;

                    query(tree[v][0]);

                    if(cnt == prvcnt) tree[v * 2].push_back(tree[v][0]), tree[v * 2 + 1].push_back(tree[v][1]);
                    else tree[v * 2 + 1].push_back(tree[v][0]), tree[v * 2].push_back(tree[v][1]);
                }
                else{
                    int prvcnt = cnt;

                    query(tree[v][0]);

                    if(cnt == prvcnt + 1) tree[v * 2 + 1].push_back(tree[v][0]), tree[v * 2].push_back(tree[v][1]);
                    else tree[v * 2].push_back(tree[v][0]), tree[v * 2 + 1].push_back(tree[v][1]);
                }
            }
            else{
                if(flag){
                    for(auto x : tree[v]){
                        int prvcnt = cnt;

                        query(x);

                        if(cnt == prvcnt) tree[v * 2].push_back(x);
                        else tree[v * 2 + 1].push_back(x);
                    }
                }
                else{
                    for(auto x : tree[v]){
                        int prvcnt = cnt;

                        query(x);

                        if(cnt == prvcnt + 1) tree[v * 2 + 1].push_back(x);
                        else tree[v * 2].push_back(x);
                    }
                }
            }

            build(v * 2 + 1, m + 1, r, flag ^ 1);
            build(v * 2, l, m, flag ^ 1);
        }
    }
};

SegTree sgt;

void Solve(int N_) {
    n = N_;

    for(int i = 1; i <= n * 2; i++){
        int prvcnt = cnt;

        query(i);

        if(cnt == prvcnt + 1) a.push_back(i);
        else{
            query(i);
            b.push_back(i);
        }
    }

    for(auto x : a) query(x);

    for(auto x : b) sgt.tree[1].push_back(x);

    sgt.build();

    for(int i = 1; i <= n; i++) Answer(a[i - 1], ans[i]);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...