#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int getDis (vector<int> h1, vector<int> h2) {
if (h1.empty() || h2.empty()) {
return INF;
}
int ret = INF;
int i = 0, j = 0;
int n = h1.size(), m = h2.size();
while (i < n || j < m) {
if (i < n && (j == m || h1[i] < h2[j])) {
if (j < m) {
ret = min(ret, h2[j] - h1[i]);
}
i++;
} else {
if (i < n) {
ret = min(ret, h1[i] - h2[j]);
}
j++;
}
}
return ret;
}
const int MX_N = 1e5 + 5;
const int MX_DAYS = 1e5 + 5;
int h[MX_N];
vector<int> versions[MX_DAYS];
vector<vector<int>> friends[MX_N];
void invert (int u, int v, int ud, int vd) {
auto ite = find(friends[u][ud].begin(), friends[u][ud].end(), v);
if (ite != friends[u][ud].end()) {//erase
friends[u][ud].erase(ite);
ite = find(friends[v][vd].begin(), friends[v][vd].end(), u);
assert(ite != friends[v][vd].end());
friends[v][vd].erase(ite);
} else {
int n = friends[u][ud].size();
int m = friends[v][vd].size();
bool inserted_v = false;
for (int i = 0; i < n; ++i) {
if (h[friends[u][ud][i]] > h[v]) {
inserted_v = true;
friends[u][ud].insert(friends[u][ud].begin() + i, v);
}
}
if (!inserted_v) {
friends[u][ud].push_back(v);
}
bool inserted_u = false;
for (int i = 0; i < m; ++i) {
if (h[friends[v][vd][i]] > h[u]) {
inserted_u = true;
friends[v][vd].insert(friends[v][vd].begin() + i, u);
}
}
if (!inserted_u) {
friends[v][vd].push_back(u);
}
}
}
int ppl;
void init(int N, int D, int H[]) {
ppl = N;
for (int i = 0; i < N; ++i) {
versions[i].push_back(0);
friends[i].push_back({});
h[i] = H[i];
}
}
void curseChanges(int U, int A[], int B[]) {
for (int day = 1; day <= U; ++day) {
int u = A[day - 1], v = B[day - 1];
versions[u].push_back(day);
versions[v].push_back(day);
vector<int> lau, lav;
lau = friends[u].back();
lav = friends[v].back();
friends[u].push_back(lau);
friends[v].push_back(lav);
invert(u, v, friends[u].size() - 1, friends[v].size() - 1);
}
}
int question(int x, int y, int v) {//p1, p2, day
if (x == y) {
return 0;
}
int vx = upper_bound(versions[x].begin(), versions[x].end(), v) - versions[x].begin() - 1;
int vy = upper_bound(versions[y].begin(), versions[y].end(), v) - versions[y].begin() - 1;
vector<int> h1, h2;
for (int i : friends[x][vx]) {
h1.push_back(h[i]);
}
for (int i : friends[y][vy]) {
h2.push_back(h[i]);
}
return getDis(h1, h2);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5240 KB |
Output is correct |
2 |
Correct |
4 ms |
5200 KB |
Output is correct |
3 |
Correct |
4 ms |
5200 KB |
Output is correct |
4 |
Correct |
20 ms |
12104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
365 ms |
44836 KB |
Output is correct |
2 |
Correct |
286 ms |
44776 KB |
Output is correct |
3 |
Runtime error |
53 ms |
27464 KB |
Execution killed with signal 6 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
286 ms |
43300 KB |
Output is correct |
2 |
Runtime error |
55 ms |
33564 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
7144 KB |
Output is correct |
2 |
Runtime error |
12 ms |
11848 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
5240 KB |
Output is correct |
3 |
Correct |
4 ms |
5200 KB |
Output is correct |
4 |
Correct |
4 ms |
5200 KB |
Output is correct |
5 |
Correct |
20 ms |
12104 KB |
Output is correct |
6 |
Correct |
365 ms |
44836 KB |
Output is correct |
7 |
Correct |
286 ms |
44776 KB |
Output is correct |
8 |
Runtime error |
53 ms |
27464 KB |
Execution killed with signal 6 |
9 |
Halted |
0 ms |
0 KB |
- |