답안 #987300

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
987300 2024-05-22T14:13:26 Z crafticat The Potion of Great Power (CEOI20_potion) C++17
0 / 100
35 ms 4704 KB
#include <bits/stdc++.h>

using namespace std;

using pii = pair<int,int>;
vector<pii> roads;
int n, d, u, q;
constexpr int inf = 1e9 + 7;
constexpr int none = 1e9;
vector<int> h;
vector<map<int, int>> to, from;

struct Node {
    Node *left = nullptr, *right = nullptr;
    int l, r, mid, exists = 0, id = 0, realL, realR;

    Node(int l, int r, int id) : l(l) ,r(r),id(id) ,mid((r + l) / 2) {
        realL = from[id][l];
        realR = from[id][r];
    }

    void push() {
        if (r - l > 1) {
            if (!left) left = new Node(l,mid,id);
            if (!right) right = new Node(mid,r,id);
        }
    }
};

Node* ins(int x, Node* node, int newX, int id) {
    if (node->r - node->l <= 1) {
        Node* z = new Node(node->l,node->r,id);
        z->exists = node->exists;
        z->exists += newX;
        return z;
    }

    node->push();
    Node* z = new Node(node->l,node->r,id);

    if (x < node->mid) {
        z->left = ins(x,node->left,newX,id);
        z->right = node->right;
    } else {
        z->right = ins(x,node->right,newX,id);
        z->left = node->left;
    }
    z->exists = z->left->exists + z->right->exists;
    return z;
}

Node* addEdge(vector<set<pii>> &g, int a, int b, vector<Node*> &lastNodes) {
    pii p = {h[b],b};
    if (g[a].count(p)) {
        g[a].erase(p);
        return ins(to[a][p.first],lastNodes[a],-1,a);
    }
    else {
        g[a].insert(p);
        return ins(to[a][p.first],lastNodes[a],true,a);
    }
}

int closest(Node* node, int x) {
    if (!node) return inf;
    if (node->exists == 0) return inf;
    if (node->r - node->l <= 1 && node->exists > 0) return abs(x - node->realL);
    if (node->realL <= x && node->realR >= x) {
        return min(closest(node->left, x), closest(node->right, x));
    }

    if (node->realL >= x && node->left && node->left->exists > 0) return closest(node->left, x);
    else {
        if (node->right && node->right->exists > 0)
            return closest(node->right, x);
        else
            return closest(node->left, x);
    }
}

void dfs(Node* x, vector<int> &dfsData) {
    if (x == nullptr) return;
    if (x->r - x->l <= 1) {
        dfsData.push_back(x->l);
        return;
    }
    if (x->left && x->left->exists> 0) dfs(x->left,dfsData);
    if (x->right && x->right->exists>0) dfs(x->right,dfsData);
}
vector<int> getDfs(Node* d) {
    vector<int> ans;
    dfs(d,ans);
    return ans;
}

void init(int N, int D, int H[]) {
    
}
vector<map<int,Node*>> nodes;

void curseChanges(int U, int A[], int B[]) {
    h.resize(n + 1);
    vector<set<int>> ex(n);
    vector<int> siz(n);
    for (int i = 0; i < n; ++i) {
        cin >> h[i];
    }

    for (int i = 0; i < u; ++i) {
        int a, b; cin >> a >> b;
        roads.emplace_back(a,b);
        ex[a].insert(h[b]);
        ex[b].insert(h[a]);
    }
    from.resize(n);
    to.resize(n);
    for (int i = 0; i < n; ++i) {
        int genId =0;
        for (auto v : ex[i]) {
            to[i][v] = genId;
            from[i][genId++] = v;
        }
        siz[i] = genId;
        to[i][inf] = genId;
        from[i][genId++] = inf;
    }

    // Time, Node* (Upper bound time)
    nodes.resize(n + 1);
    vector<Node*> lastNodes(n + 1);
    for (int i = 0; i < n; ++i) {
        lastNodes[i] = new Node(0,siz[i],i);
        nodes[i].insert({0, lastNodes[i]});
    }
    vector<set<pii>> g(n + 1);
    for (int i = 0; i < u; ++i) {
        auto [a,b] = roads[i];
        lastNodes[a] = addEdge(g,a,b,lastNodes);
        lastNodes[b] = addEdge(g,b,a,lastNodes);
        nodes[a].insert({-i - 1, lastNodes[a]});
        nodes[b].insert({-i -1, lastNodes[b]});
    }
}

int question(int a, int b, int t) {
    auto itB = nodes[b].lower_bound(-t);
    auto itA = nodes[a].lower_bound(-t);
    Node* nodeB = itB->second, *nodeA = itA->second;

    if (nodeB->exists < nodeA->exists) {
        swap(a,b);
        swap(nodeA,nodeB);
    } // A is smaller
    if (nodeB->exists == 0 || nodeA->exists == 0) {
        return none;
    }

    int ans = 1e9;
    for (auto x : getDfs(nodeA)) {
        ans = min(ans, closest(nodeB,from[a][x]));
    }
    return ans;
}

Compilation message

potion.cpp: In constructor 'Node::Node(int, int, int)':
potion.cpp:15:32: warning: 'Node::id' will be initialized after [-Wreorder]
   15 |     int l, r, mid, exists = 0, id = 0, realL, realR;
      |                                ^~
potion.cpp:15:15: warning:   'int Node::mid' [-Wreorder]
   15 |     int l, r, mid, exists = 0, id = 0, realL, realR;
      |               ^~~
potion.cpp:17:5: warning:   when initialized here [-Wreorder]
   17 |     Node(int l, int r, int id) : l(l) ,r(r),id(id) ,mid((r + l) / 2) {
      |     ^~~~
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 344 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 852 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 35 ms 4576 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 34 ms 4704 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 4 ms 1124 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 344 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -