#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
const int MAX_N = 1e5 + 5, K = 50, MAX_T = 2e5 + 5;
int n;
int h[MAX_N]; // Not id
int t;
int e1[MAX_T], e2[MAX_T]; // Not id
int id[MAX_N], rev_id[MAX_N];
vector<pii> upds[MAX_N]; // {time, node} and {0, -1}
set<int> adj[MAX_N]; // reused
vector<vector<int>> crit_adjs[MAX_N];
void update1(int u, int v, set<int>& s) { // one-way
if (s.count(v)) s.erase(v);
else s.insert(v);
}
void save(int u) {
vector<int> cur_adj(adj[u].begin(), adj[u].end());
crit_adjs[u].push_back(cur_adj);
}
void precomp() {
vector<pii> order;
for (int i = 0; i < n; i++) order.push_back({h[i], i});
sort(order.begin(), order.end());
for (int i = 0; i < n; i++) {
id[order[i].second] = i;
rev_id[i] = order[i].second;
}
for (int upd = 0; upd <= t; upd++) {
if (upd == 0) {
for (int u = 0; u < n; u++) {
upds[u].push_back({0, -1});
save(u);
}
continue;
}
int u = id[e1[upd]], v = id[e2[upd]];
update1(u, v, adj[u]);
update1(v, u, adj[v]);
upds[u].push_back({upd, v});
upds[v].push_back({upd, u});
if (((int) upds[u].size() - 1) % K == 0) save(u);
if (((int) upds[v].size() - 1) % K == 0) save(v);
}
}
void init(int tmp_n, int tmp_d, int tmp_h[]) {
n = tmp_n;
for (int i = 0; i < n; i++) h[i] = tmp_h[i];
}
void curseChanges(int tmp_t, int tmp_e1[], int tmp_e2[]) {
t = tmp_t;
for (int i = 1; i <= t; i++) {
e1[i] = tmp_e1[i - 1];
e2[i] = tmp_e2[i - 1];
}
precomp();
}
int question(int u, int v, int upd) {
u = id[u];
v = id[v];
//
int hi = upper_bound(upds[u].begin(), upds[u].end(), make_pair(upd, (int) 1e9)) - upds[u].begin() - 1;
int lo = K * (hi / K);
set<int> u_adj(crit_adjs[u][hi / K].begin(), crit_adjs[u][hi / K].end());
for (int i = lo + 1; i <= hi; i++)
update1(u, upds[u][i].second, u_adj);
int hi2 = upper_bound(upds[v].begin(), upds[v].end(), make_pair(upd, (int) 1e9)) - upds[v].begin() - 1;
int lo2 = K * (hi2 / K);
set<int> v_adj(crit_adjs[v][hi2 / K].begin(), crit_adjs[v][hi2 / K].end());
for (int i = lo2 + 1; i <= hi2; i++)
update1(v, upds[v][i].second, v_adj);
//
int ans = 1e9;
auto l_it = v_adj.begin(), r_it = v_adj.begin();
for (auto it = u_adj.begin(); it != u_adj.end(); it++) {
while (l_it != v_adj.end() && next(l_it, 1) != v_adj.end() && h[rev_id[*next(l_it, 1)]] <= h[rev_id[*it]]) l_it++;
while (r_it != v_adj.end() && h[rev_id[*r_it]] <= h[rev_id[*it]]) r_it++;
if (l_it != v_adj.end()) ans = min(ans, abs(h[rev_id[*it]] - h[rev_id[*l_it]]));
if (r_it != v_adj.end()) ans = min(ans, abs(h[rev_id[*it]] - h[rev_id[*r_it]]));
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
10840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
11096 KB |
Output is correct |
2 |
Correct |
4 ms |
10932 KB |
Output is correct |
3 |
Correct |
4 ms |
11096 KB |
Output is correct |
4 |
Correct |
31 ms |
18724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
355 ms |
46644 KB |
Output is correct |
2 |
Correct |
332 ms |
46852 KB |
Output is correct |
3 |
Correct |
330 ms |
25836 KB |
Output is correct |
4 |
Correct |
1724 ms |
31624 KB |
Output is correct |
5 |
Correct |
702 ms |
38572 KB |
Output is correct |
6 |
Execution timed out |
3023 ms |
37840 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
326 ms |
46488 KB |
Output is correct |
2 |
Correct |
2013 ms |
33584 KB |
Output is correct |
3 |
Correct |
1291 ms |
35052 KB |
Output is correct |
4 |
Correct |
2134 ms |
37524 KB |
Output is correct |
5 |
Correct |
480 ms |
46448 KB |
Output is correct |
6 |
Correct |
2334 ms |
37828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
13144 KB |
Output is correct |
2 |
Correct |
171 ms |
12040 KB |
Output is correct |
3 |
Correct |
282 ms |
12204 KB |
Output is correct |
4 |
Correct |
1027 ms |
12740 KB |
Output is correct |
5 |
Correct |
926 ms |
13144 KB |
Output is correct |
6 |
Correct |
162 ms |
12120 KB |
Output is correct |
7 |
Correct |
864 ms |
12208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
10840 KB |
Output is correct |
2 |
Correct |
3 ms |
11096 KB |
Output is correct |
3 |
Correct |
4 ms |
10932 KB |
Output is correct |
4 |
Correct |
4 ms |
11096 KB |
Output is correct |
5 |
Correct |
31 ms |
18724 KB |
Output is correct |
6 |
Correct |
355 ms |
46644 KB |
Output is correct |
7 |
Correct |
332 ms |
46852 KB |
Output is correct |
8 |
Correct |
330 ms |
25836 KB |
Output is correct |
9 |
Correct |
1724 ms |
31624 KB |
Output is correct |
10 |
Correct |
702 ms |
38572 KB |
Output is correct |
11 |
Execution timed out |
3023 ms |
37840 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |