Submission #1039276

# Submission time Handle Problem Language Result Execution time Memory
1039276 2024-07-30T15:56:00 Z Tonyl The Potion of Great Power (CEOI20_potion) C++17
56 / 100
2264 ms 262144 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;

int n,d;
vi alln;

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

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

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

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

    void gather_all() {
        alln.push_back(num);
        if (left != nullptr) left->gather_all();
        if (right != nullptr) right->gather_all();
    }
};

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};
    }
}

const int MAX_N = 1e5+2;
Treap* smol[MAX_N];

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});
    }

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

vi hs;

TimeTraveller fr[MAX_N];

vi aaa, bbb, h1, h2;

int get_ans() {
    int best = 1e9;

    h1 = vi(); h2 = vi();
    trav(aa, aaa) {
        h1.push_back(hs[aa]);
    }
    sort(all(h1));
    trav(bb, bbb) {
        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;
}

bool called = false;
void init(int N, int D, int H[]) {
    srand(899852);
    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[]) {
    assert(!called);
    called = 1;
    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) {
    fr[x].get_all(v);
    aaa = alln;
    fr[y].get_all(v);
    bbb = alln;
    return get_ans();
    //return get_ans(fr[x].get_all(v), fr[y].get_all(v));
}

Compilation message

In file included from /usr/include/c++/10/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
                 from potion.cpp:1:
potion.cpp: In member function 'void TimeTraveller::get_all(int)':
potion.cpp:111:28: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  111 |         assert(alln.size() <= d);
      |                ~~~~~~~~~~~~^~~~
potion.cpp: In function 'int get_ans()':
potion.cpp:138:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  138 |         while (h1[i] < hb && i != h1.size()-1) i++;
      |                              ~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 5692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5976 KB Output is correct
2 Correct 6 ms 5976 KB Output is correct
3 Correct 8 ms 6228 KB Output is correct
4 Correct 23 ms 10660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 361 ms 64880 KB Output is correct
2 Correct 318 ms 65136 KB Output is correct
3 Correct 251 ms 140004 KB Output is correct
4 Runtime error 418 ms 262144 KB Execution killed with signal 9
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 267 ms 64948 KB Output is correct
2 Correct 1845 ms 252980 KB Output is correct
3 Correct 1153 ms 200088 KB Output is correct
4 Correct 1973 ms 235908 KB Output is correct
5 Correct 400 ms 78640 KB Output is correct
6 Correct 2264 ms 236440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 8208 KB Output is correct
2 Correct 72 ms 12376 KB Output is correct
3 Correct 111 ms 11608 KB Output is correct
4 Correct 650 ms 14932 KB Output is correct
5 Correct 642 ms 12056 KB Output is correct
6 Correct 109 ms 12120 KB Output is correct
7 Correct 592 ms 15192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 5692 KB Output is correct
2 Correct 6 ms 5976 KB Output is correct
3 Correct 6 ms 5976 KB Output is correct
4 Correct 8 ms 6228 KB Output is correct
5 Correct 23 ms 10660 KB Output is correct
6 Correct 361 ms 64880 KB Output is correct
7 Correct 318 ms 65136 KB Output is correct
8 Correct 251 ms 140004 KB Output is correct
9 Runtime error 418 ms 262144 KB Execution killed with signal 9
10 Halted 0 ms 0 KB -