Submission #1039248

# Submission time Handle Problem Language Result Execution time Memory
1039248 2024-07-30T15:29:48 Z Tonyl The Potion of Great Power (CEOI20_potion) C++17
Compilation error
0 ms 0 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using pi = pair<int,int>;
#define REP(i,n) for (int i = 0; i < n; i++)
#define trav(a,x) for (auto &a : x)
#define all(x) (x).begin(), (x).end()
#define submit(a,b) cout << a << " " << b << endl
#define D(x) //cerr << #x << ": " << x << endl;

const int MAX_N = 1e5+2;

int n,d;
vi hs;

TimeTraveller fr[MAX_N];
Treap* smol[MAX_N];

struct Treap {
    int num;
    int prio;
    Treap *left = nullptr, *right = nullptr;

    Treap(Treap *t) {
        num = t->num;
        prio = t->prio;
    }

    Treap(int nm, int pr) {
        num = nm;
        prio = pr;
    }

    Treap(int nm) {
        num = nm;
        prio = rand();
    }

    vi get_all() {
        vi v;
        if (left != nullptr) v = left->get_all();
        if (right != nullptr) trav(a, right->get_all()) v.push_back(a);
        v.push_back(num);
        return v;
    }
};

Treap* merge(Treap* left, Treap* right) {
    if (left == nullptr) return right;
    if (right == nullptr) return left;

    if (left->prio <= right->prio) {
        Treap *t = new Treap(left);
        t->left = left->left;
        t->right = merge(left->right, right);
        return t;
    } else {
        Treap *t = new Treap(right);
        t->right = right->right;
        t->left = merge(left, right->left);
        return t;
    }
}

pair<Treap*, Treap*> split(Treap* tr, int num) {
    if (tr == nullptr) return {tr, tr};
    if (tr->num < num) {
        Treap *t = new Treap(tr);
        t->left = tr->left;
        auto p = split(tr->right, num);
        t->right = p.first;
        return {t, p.second};
    } else {
        Treap *t = new Treap(tr);
        t->right = tr->right;
        auto p = split(tr->left, num);
        t->left = p.second;
        return {p.first, t};
    }
}

Treap* flipp(Treap* tr, int num) {
    Treap *lower, *exact, *bigger;
    auto p = split(tr, num);
    lower = p.first;
    auto p2 = split(p.second, num+1);
    exact = p2.first;
    bigger = p2.second;
    if (exact == nullptr) exact = smol[num];
    else exact = nullptr;

    Treap* t = merge(lower, exact);
    return merge(t, bigger);
}

struct TimeTraveller {
    vector<pair<int, Treap*>> history;

    TimeTraveller() {
        history.push_back({-1, nullptr});
    }

    void flip(int i, int time) {
        Treap* c = flipp(history.back().second, i);
        history.push_back({time, c});
    }

    vi get_all(int time) {
        auto p = lower_bound(all(history), pair<int, Treap*>(time+1, nullptr));
        p--;
        if ((*p).second == nullptr) return vi();
        return (*p).second->get_all();
    }
};

int get_ans(vi a, vi b) {
    int best = 1e9;

    vi h1, h2;
    trav(aa, a) {
        h1.push_back(hs[aa]);
    }
    sort(all(h1));
    trav(bb, b) {
        h2.push_back(hs[bb]);
    }
    sort(all(h2));

    if (h1.size() == 0) return best;

    int i = 0;
    trav(hb, h2) {
        while (h1[i] < hb && i != h1.size()-1) i++;
        best = min(best, abs(hb - h1[i]));
        if (i!=0) best = min(best, abs(hb - h1[i-1]));
    }

    return best;
}

void init(int N, int D, int H[]) {
    srand(43249);
    n = N; d = D;
    REP(i,n) hs.push_back(H[i]);
    REP(i,n) smol[i] = new Treap(i, rand());
}

void curseChanges(int U, int A[], int B[]) {
    REP(uu, U) {
        int a = A[uu];
        int b = B[uu];
        fr[a].flip(b, uu+1);
        fr[b].flip(a, uu+1);
    }
}

int question(int x, int y, int v) {
    return get_ans(fr[x].get_all(v), fr[y].get_all(v));
}

Compilation message

potion.cpp:17:1: error: 'TimeTraveller' does not name a type
   17 | TimeTraveller fr[MAX_N];
      | ^~~~~~~~~~~~~
potion.cpp:18:1: error: 'Treap' does not name a type
   18 | Treap* smol[MAX_N];
      | ^~~~~
potion.cpp: In function 'Treap* flipp(Treap*, int)':
potion.cpp:90:35: error: 'smol' was not declared in this scope
   90 |     if (exact == nullptr) exact = smol[num];
      |                                   ^~~~
potion.cpp: In function 'int get_ans(vi, vi)':
potion.cpp:134:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  134 |         while (h1[i] < hb && i != h1.size()-1) i++;
      |                              ~~^~~~~~~~~~~~~~
potion.cpp: In function 'void init(int, int, int*)':
potion.cpp:146:14: error: 'smol' was not declared in this scope
  146 |     REP(i,n) smol[i] = new Treap(i, rand());
      |              ^~~~
potion.cpp: In function 'void curseChanges(int, int*, int*)':
potion.cpp:153:9: error: 'fr' was not declared in this scope
  153 |         fr[a].flip(b, uu+1);
      |         ^~
potion.cpp: In function 'int question(int, int, int)':
potion.cpp:159:20: error: 'fr' was not declared in this scope
  159 |     return get_ans(fr[x].get_all(v), fr[y].get_all(v));
      |                    ^~