Submission #349144

#TimeUsernameProblemLanguageResultExecution timeMemory
349144dolphingarlicThe Potion of Great Power (CEOI20_potion)C++14
38 / 100
3055 ms262148 KiB
#include <bits/stdc++.h>
using namespace std;

const int INF = 1e9, C = 15;

int H[100000];
struct cmp { bool operator()(int a, int b) const { return tie(H[a], a) < tie(H[b], b); } };
map<int, set<int, cmp>> trust[100000];
map<int, vector<pair<int, int>>> updates[100000];

void init(int N, int D, int H[]) {
    for (int i = 0; i < N; i++) {
        trust[i][0] = set<int, cmp>();
        updates[i][0] = {};
        ::H[i] = H[i];
    }
}

set<int, cmp> playback(int shaman, int time) {
    set<int, cmp> s = prev(trust[shaman].upper_bound(time))->second;
    for (pair<int, int> i : prev(updates[shaman].upper_bound(time))->second) {
        if (i.first >= time) break;
        if (s.find(i.second) == s.end()) s.insert(i.second);
        else s.erase(i.second);
    }
    return s;
}

void curseChanges(int U, int A[], int B[]) {
    for (int i = 0; i < U; i++) {
        updates[A[i]].rbegin()->second.push_back({i, B[i]});
        updates[B[i]].rbegin()->second.push_back({i, A[i]});
        if (updates[A[i]].rbegin()->second.size() == C) {
            trust[A[i]][i + 1] = playback(A[i], i + 1);
            updates[A[i]][i + 1] = {};
        }
        if (updates[B[i]].rbegin()->second.size() == C) {
            trust[B[i]][i + 1] = playback(B[i], i + 1);
            updates[B[i]][i + 1] = {};
        }
    }
}

int question(int x, int y, int v) {
    set<int, cmp> sx = playback(x, v);
    set<int, cmp> sy = playback(y, v);
    int ans = INF;
    for (int i : sx) {
        auto ub = sy.upper_bound(i);
        if (ub != sy.end()) ans = min(ans, H[*ub] - H[i]);
        if (ub != sy.begin()) ans = min(ans, H[i] - H[*prev(ub)]);
    }
    return ans;
}
#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...