#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const int MX_N = 1e5 + 5;
const int MX_DAYS = 2e5 + 5;
const int MX_NODES = (MX_N + MX_DAYS) * 30;
int idd;
int ppl;
int h[MX_N];
vector<int> to;
set<int> friends[MX_N];
vector<pair<int, int>> versions[MX_N];
int tree[MX_NODES];
int root[MX_NODES];
int L[MX_NODES], R[MX_NODES];
int upd (int nd, int l, int r, int p, int v) {//log
int nx = ++idd;
if (l == r) {
tree[nx] = tree[nd] + v;
return nx;
}
int mid = (l + r) >> 1;
if (p <= mid) {
L[nx] = upd(L[nd], l, mid, p, v);
R[nx] = R[nd];
} else {
L[nx] = L[nd];
R[nx] = upd(R[nd], mid + 1, r, p, v);
}
tree[nx] = tree[L[nx]] + tree[R[nx]];
return nx;
}
void dive (int nd, int l, int r, vector<int>& vec) {//log * d
if (l == r) {
vec.push_back(l);
return;
}
int mid = (l + r) >> 1;
if (L[nd] && tree[L[nd]]) {
dive(L[nd], l, mid, vec);
}
if (R[nd] && tree[R[nd]]) {
dive(R[nd], mid + 1, r, vec);
}
}
pair<int, int> invert (int u, int v, int uVersion, int vVersion) {//log
int rootU, rootV;
if (friends[u].count(v)) {
rootU = upd(uVersion, 0, ppl - 1, h[v], -1);
rootV = upd(vVersion, 0, ppl - 1, h[u], -1);
friends[u].erase(v);
friends[v].erase(u);
} else {
rootU = upd(uVersion, 0, ppl - 1, h[v], +1);
rootV = upd(vVersion, 0, ppl - 1, h[u], +1);
friends[u].insert(v);
friends[v].insert(u);
}
return make_pair(rootU, rootV);
}
void compress () {//nlog
map<int, int> mp;
int cnt = 0;
for (int i = 0; i < ppl; ++i) {
mp[h[i]];
}
for (auto& it : mp) {
it.second = cnt++;
}
for (int i = 0; i < ppl; ++i) {
h[i] = mp[h[i]];
}
}
int a[MX_N];
void init(int N, int D, int H[]) {//nlog
ppl = N;
to.resize(ppl);
for (int i = 0; i < ppl; ++i) {
h[i] = H[i];
a[i] = H[i];
to[i] = H[i];
versions[i].emplace_back(0, ++idd);
}
sort(to.begin(), to.end());
to.resize((unique(to.begin(), to.begin() + ppl) - to.begin()));
compress();
}
int lastDay;
void curseChanges(int U, int A[], int B[]) {//nlog
lastDay = U;
for (int day = 1; day <= U; ++day) {
int u = A[day - 1], v = B[day - 1];
if (u > v) swap(u, v);
int verU = versions[u].back().second;
int verV = versions[v].back().second;
auto [ru, rv] = invert(u, v, verU, verV);
versions[u].emplace_back(day, ru);
versions[v].emplace_back(day, rv);
}
}
int getDis (vector<int> h1, vector<int> h2) {//n + m
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, to[h2[j]] - to[h1[i]]);
}
i++;
} else {
if (i < n) {
ret = min(ret, to[h1[i]] - to[h2[j]]);
}
j++;
}
}
return ret;
}
int question(int u, int v, int day) {//qdlog
vector<int> h1, h2;
if (v == lastDay) {
for (int i : friends[u]) {
h1.push_back(a[i]);
}
for (int i : friends[v]) {
h2.push_back(a[i]);
}
} else {
pair<int,int> verV = *prev(lower_bound(versions[v].begin(), versions[v].end(), make_pair(day, INF)));
pair<int,int> verU = *prev(lower_bound(versions[u].begin(), versions[u].end(), make_pair(day, INF)));
dive(verV.second, 0, ppl - 1, h1);
dive(verU.second, 0, ppl - 1, h2);
}
return getDis(h1, h2);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7724 KB |
Output is correct |
2 |
Correct |
5 ms |
7760 KB |
Output is correct |
3 |
Correct |
5 ms |
7760 KB |
Output is correct |
4 |
Correct |
67 ms |
17152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
645 ms |
119900 KB |
Output is correct |
2 |
Correct |
656 ms |
119896 KB |
Output is correct |
3 |
Correct |
328 ms |
100776 KB |
Output is correct |
4 |
Execution timed out |
3014 ms |
73984 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
469 ms |
121372 KB |
Output is correct |
2 |
Correct |
263 ms |
103112 KB |
Output is correct |
3 |
Correct |
383 ms |
107420 KB |
Output is correct |
4 |
Correct |
363 ms |
107376 KB |
Output is correct |
5 |
Correct |
485 ms |
120800 KB |
Output is correct |
6 |
Correct |
391 ms |
107384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
12368 KB |
Output is correct |
2 |
Correct |
149 ms |
11628 KB |
Output is correct |
3 |
Correct |
214 ms |
11640 KB |
Output is correct |
4 |
Correct |
1097 ms |
12016 KB |
Output is correct |
5 |
Correct |
991 ms |
12240 KB |
Output is correct |
6 |
Correct |
161 ms |
11180 KB |
Output is correct |
7 |
Correct |
914 ms |
11648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7376 KB |
Output is correct |
2 |
Correct |
5 ms |
7724 KB |
Output is correct |
3 |
Correct |
5 ms |
7760 KB |
Output is correct |
4 |
Correct |
5 ms |
7760 KB |
Output is correct |
5 |
Correct |
67 ms |
17152 KB |
Output is correct |
6 |
Correct |
645 ms |
119900 KB |
Output is correct |
7 |
Correct |
656 ms |
119896 KB |
Output is correct |
8 |
Correct |
328 ms |
100776 KB |
Output is correct |
9 |
Execution timed out |
3014 ms |
73984 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |