Submission #656516

# Submission time Handle Problem Language Result Execution time Memory
656516 2022-11-07T18:24:25 Z Alex_tz307 The Potion of Great Power (CEOI20_potion) C++17
0 / 100
222 ms 30416 KB
#include <bits/stdc++.h>

using namespace std;

const int kN = 1e5;
const int INF = 1e9;
const int kBucket = 200;
int N, h[kN], bucketSize[kN];
vector<pair<int, int>> ev[kN];
vector<vector<int>> friends[kN];
bool ap[kN], inVec[kN];

void minSelf(int &x, int y) {
  if (y < x) {
    x = y;
  }
}

bool fcmp(const int &x, const int &y) {
  if (h[x] != h[y]) {
    return h[x] < h[y];
  }
  return x < y;
}

vector<int> findList(const int &x, const int &t) {
  int l = 0, r = (int)ev[x].size() - 1, pos = -1;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (ev[x][mid].first <= t) {
      pos = mid;
      l = mid + 1;
    } else {
      r = mid - 1;
    }
  }
  if (pos == -1) {
    return {};
  }
  int bucket = (pos + 1) / bucketSize[x] - 1;
  vector<int> suff;
  for (int i = (bucket + 1) * bucketSize[x]; i <= pos; ++i) {
    if (!inVec[ev[x][i].second]) {
      suff.emplace_back(ev[x][i].second);
      inVec[ev[x][i].second] = true;
    }
    ap[ev[x][i].second] ^= 1;
  }
  vector<int> realSuff;
  for (const int &y : suff) {
    if (ap[y]) {
      realSuff.emplace_back(y);
    }
    ap[y] = false;
    inVec[y] = false;
  }
  suff = realSuff;
  sort(suff.begin(), suff.end(), fcmp);
  if (bucket < 0) {
    return suff;
  }
  vector<int> sol(friends[x][bucket].size() + suff.size());
  merge(friends[x][bucket].begin(), friends[x][bucket].end(), suff.begin(), suff.end(), sol.begin(), fcmp);
  vector<int> realSol;
  for (int i = 0; i < (int)sol.size(); ++i) {
    int j = i;
    while (j < (int)sol.size() && sol[j] == sol[i]) {
      j += 1;
    }
    if ((j - i) % 2 == 1) {
      realSol.emplace_back(sol[i]);
    }
    i = j - 1;
  }
  return realSol;
}

void init(int n, int d, int H[]) {
  N = n;
  for (int i = 0; i < N; ++i) {
    h[i] = H[i];
  }
}

void curseChanges(int U, int A[], int B[]) {
  for (int i = 1; i <= U; ++i) {
    ev[A[i]].emplace_back(i, B[i]);
    ev[B[i]].emplace_back(i, A[i]);
  }
  vector<int> preff, realFriends;
  for (int x = 0; x < N; ++x) {
    if (ev[x].empty()) {
      continue;
    }
    bucketSize[x] = min((int)sqrt((int)ev[x].size()), kBucket);
    preff.clear();
    for (int i = 0; i < (int)ev[x].size(); ++i) {
      if (!inVec[ev[x][i].second]) {
        preff.emplace_back(ev[x][i].second);
        inVec[ev[x][i].second] = true;
      }
      ap[ev[x][i].second] ^= 1;
      if (i % bucketSize[x] == bucketSize[x] - 1) {
        realFriends.clear();
        for (const int &y : preff) {
          if (ap[y]) {
            realFriends.emplace_back(y);
          } else {
            inVec[y] = false;
          }
        }
        preff = realFriends;
        friends[x].emplace_back(preff);
      }
    }
    if ((int)ev[x].size() % bucketSize[x]) {
      realFriends.clear();
      for (const int &y : preff) {
        if (ap[y]) {
          realFriends.emplace_back(y);
        } else {
          inVec[y] = false;
        }
      }
      preff = realFriends;
      friends[x].emplace_back(preff);
    }
    for (const int &y : preff) {
      ap[y] = false;
      inVec[y] = false;
    }
    for (int i = 0; i < (int)friends[x].size(); ++i) {
      sort(friends[x][i].begin(), friends[x][i].end(), fcmp);
    }
  }
}

int question(int x, int y, int v) {
  vector<int> X = findList(x, v);
  if (X.empty()) {
    return INF;
  }
  vector<int> Y = findList(y, v);
  if (Y.empty()) {
    return INF;
  }
  int res = INF, j = 0;
  for (int i = 0; i < (int)X.size(); ++i) {
    while (j < (int)Y.size() && h[Y[j]] <= h[X[i]]) {
      j += 1;
    }
    if (j < (int)Y.size()) {
      minSelf(res, h[Y[j]] - h[X[i]]);
    }
    if (j >= 1) {
      minSelf(res, h[X[i]] - h[Y[j - 1]]);
    }
  }
  return res;
}
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 4944 KB Incorrect
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 5168 KB Incorrect
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 211 ms 30416 KB Output is correct
2 Correct 222 ms 30296 KB Output is correct
3 Incorrect 78 ms 11624 KB Incorrect
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 183 ms 30340 KB Incorrect
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 14 ms 6576 KB Incorrect
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 4944 KB Incorrect
2 Halted 0 ms 0 KB -