#include<vector>
#include<utility>
#include<cmath>
#include<algorithm>
using namespace std;
namespace cj{
vector<int> h, a, b;
int n, d, u, sqrtu;
vector<vector<vector<int>>> big;
vector<vector<pair<int,int>>> graph;
vector<vector<vector<pair<int,int>>>> small;
vector<vector<int>> slots;
bool Comp(pair<int,int> A, pair<int,int> B){
if (h[A.first] == h[B.first]){
return A.first < B.first;
}
return h[A.first] < h[B.first];
}
bool C(int A, int B){
if (h[A] == h[B]){
return A < B;
}
return h[A] < h[B];
}
bool Comp2(pair<int,int> A, pair<int,int> B){
return A.second < B.second;
}
inline void push(vector<int> &V, int val){
if (V.empty()){
V.push_back(val);
}
else if (V.back() != val){
V.push_back(val);
}
else {
V.pop_back();
}
}
template<class _T>
inline void copy(vector<_T> &from, vector<_T> &to){
to.clear();
for (int i : from){
to.push_back(i);
}
}
inline void clean(vector<int> &V){
vector<int> p;
copy<int>(V, p);
sort(p.begin(), p.end(), C);
V.clear();
for (int i : p){
push(V, i);
}
return;
}
}
void init(int N, int D, int H[]) {
cj::n = N;
cj::d = D;
cj::h = vector<int>(N);
cj::graph = vector<vector<pair<int,int>>>(cj::n, vector<pair<int,int>>(0));
cj::big = vector<vector<vector<int>>>(cj::n, vector<vector<int>>(0));
cj::small = vector<vector<vector<pair<int,int>>>>(cj::n, vector<vector<pair<int,int>>>(0));
cj::slots = vector<vector<int>>(cj::n, vector<int>(0));
for (int i = 0; i < N; i++){
cj::h[i] = H[i];
}
}
void curseChanges(int U, int A[], int B[]) {
cj::u = U;
cj::a = cj::b = vector<int>(U);
for (int i = 0; i < U; i++){
cj::a[i] = A[i];
cj::b[i] = B[i];
}
cj::sqrtu = floor(sqrt(cj::u));
for (int i = 0; i < U; i++){
cj::graph[cj::a[i]].push_back({cj::b[i], i});
cj::graph[cj::b[i]].push_back({cj::a[i], i});
}
for (int i = 0; i < cj::n; i++){
sort(cj::graph[i].begin(), cj::graph[i].end(), cj::Comp2);
}
for (int i = 0; i < cj::n; i++){
cj::big[i].push_back({});
cj::small[i].push_back({});
cj::slots[i].push_back(0);
for (int j = 0; j < cj::graph[i].size(); j++){
if (cj::small[i].size() == cj::sqrtu){
cj::big[i].push_back({});
cj::copy<int>(*(cj::big[i].end() - 2), cj::big[i].back());
for (pair<int,int> p : cj::small[i].back()){
cj::big[i].back().push_back(p.first);
}
cj::clean(cj::big[i].back());
cj::small[i].push_back({});
cj::slots[i].push_back(1e9);
}
cj::small[i].back().push_back(cj::graph[i][j]);
if (cj::slots[i].back() == 1e9){
cj::slots[i].back() = cj::small[i].back()[0].second;
}
}
for (int j = 0; j < cj::small[i].size(); j++){
sort(cj::small[i][j].begin(), cj::small[i][j].end(), cj::Comp);
}
}
}
inline void genseq(int x, int v, vector<int> &ret){
int slot;
vector<int> s;
ret.clear();
for (int i = cj::slots[x].size() - 1; i >= 0; i--){
if (cj::slots[x][i] <= v){
slot = i;
break;
}
}
for (pair<int,int> i : cj::small[x][slot]){
if (i.second <= v){
cj::push(s, i.first);
}
}
int p1 = 0, p2 = 0;
while (p1 < s.size() || p2 < cj::big[x][slot].size()){
if (p1 == s.size()){
cj::push(ret, cj::big[x][slot][p2]);
p2++;
}
else if (p2 == cj::big[x][slot].size()){
cj::push(ret, s[p1]);
p1++;
}
else if (cj::h[s[p1]] > cj::h[cj::big[x][slot][p2]]){
cj::push(ret, cj::big[x][slot][p2]);
p2++;
}
else {
cj::push(ret, s[p1]);
p1++;
}
}
}
int question(int x, int y, int v) {
v--;
if (v == -1) return 1e9;
vector<int> xseq, yseq;
genseq(x, v, xseq);
genseq(y, v, yseq);
int p1 = 0, p2 = 0, output = 1e9;
while (p1 < xseq.size() && p2 < yseq.size()){
output = min(output, abs(cj::h[xseq[p1]] - cj::h[yseq[p2]]));
if (cj::h[xseq[p1]] < cj::h[yseq[p2]]){
p1++;
}
else {
p2++;
}
}
return output;
}
Compilation message
potion.cpp: In function 'void curseChanges(int, int*, int*)':
potion.cpp:90:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
90 | for (int j = 0; j < cj::graph[i].size(); j++){
| ~~^~~~~~~~~~~~~~~~~~~~~
potion.cpp:91:37: warning: comparison of integer expressions of different signedness: 'std::vector<std::vector<std::pair<int, int> > >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
91 | if (cj::small[i].size() == cj::sqrtu){
| ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
potion.cpp:106:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
106 | for (int j = 0; j < cj::small[i].size(); j++){
| ~~^~~~~~~~~~~~~~~~~~~~~
potion.cpp: In function 'void genseq(int, int, std::vector<int>&)':
potion.cpp:128:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
128 | while (p1 < s.size() || p2 < cj::big[x][slot].size()){
| ~~~^~~~~~~~~~
potion.cpp:128:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
128 | while (p1 < s.size() || p2 < cj::big[x][slot].size()){
| ~~~^~~~~~~~~~~~~~~~~~~~~~~~~
potion.cpp:129:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
129 | if (p1 == s.size()){
| ~~~^~~~~~~~~~~
potion.cpp:133:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
133 | else if (p2 == cj::big[x][slot].size()){
| ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
potion.cpp: In function 'int question(int, int, int)':
potion.cpp:155:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
155 | while (p1 < xseq.size() && p2 < yseq.size()){
| ~~~^~~~~~~~~~~~~
potion.cpp:155:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
155 | while (p1 < xseq.size() && p2 < yseq.size()){
| ~~~^~~~~~~~~~~~~
potion.cpp: In function 'void genseq(int, int, std::vector<int>&)':
potion.cpp:122:45: warning: 'slot' may be used uninitialized in this function [-Wmaybe-uninitialized]
122 | for (pair<int,int> i : cj::small[x][slot]){
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
600 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
28 ms |
20060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
190 ms |
34404 KB |
Output is correct |
2 |
Correct |
176 ms |
34508 KB |
Output is correct |
3 |
Correct |
548 ms |
29904 KB |
Output is correct |
4 |
Correct |
456 ms |
12308 KB |
Output is correct |
5 |
Correct |
156 ms |
15840 KB |
Output is correct |
6 |
Correct |
1135 ms |
33536 KB |
Output is correct |
7 |
Correct |
287 ms |
34220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
166 ms |
34412 KB |
Output is correct |
2 |
Correct |
1688 ms |
31732 KB |
Output is correct |
3 |
Correct |
571 ms |
33792 KB |
Output is correct |
4 |
Correct |
987 ms |
33312 KB |
Output is correct |
5 |
Correct |
194 ms |
34356 KB |
Output is correct |
6 |
Correct |
1076 ms |
33328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
3240 KB |
Output is correct |
2 |
Correct |
70 ms |
3064 KB |
Output is correct |
3 |
Correct |
255 ms |
3012 KB |
Output is correct |
4 |
Correct |
343 ms |
3416 KB |
Output is correct |
5 |
Correct |
288 ms |
3248 KB |
Output is correct |
6 |
Correct |
64 ms |
1368 KB |
Output is correct |
7 |
Correct |
863 ms |
3300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
600 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
600 KB |
Output is correct |
5 |
Correct |
28 ms |
20060 KB |
Output is correct |
6 |
Correct |
190 ms |
34404 KB |
Output is correct |
7 |
Correct |
176 ms |
34508 KB |
Output is correct |
8 |
Correct |
548 ms |
29904 KB |
Output is correct |
9 |
Correct |
456 ms |
12308 KB |
Output is correct |
10 |
Correct |
156 ms |
15840 KB |
Output is correct |
11 |
Correct |
1135 ms |
33536 KB |
Output is correct |
12 |
Correct |
287 ms |
34220 KB |
Output is correct |
13 |
Correct |
166 ms |
34412 KB |
Output is correct |
14 |
Correct |
1688 ms |
31732 KB |
Output is correct |
15 |
Correct |
571 ms |
33792 KB |
Output is correct |
16 |
Correct |
987 ms |
33312 KB |
Output is correct |
17 |
Correct |
194 ms |
34356 KB |
Output is correct |
18 |
Correct |
1076 ms |
33328 KB |
Output is correct |
19 |
Correct |
36 ms |
3240 KB |
Output is correct |
20 |
Correct |
70 ms |
3064 KB |
Output is correct |
21 |
Correct |
255 ms |
3012 KB |
Output is correct |
22 |
Correct |
343 ms |
3416 KB |
Output is correct |
23 |
Correct |
288 ms |
3248 KB |
Output is correct |
24 |
Correct |
64 ms |
1368 KB |
Output is correct |
25 |
Correct |
863 ms |
3300 KB |
Output is correct |
26 |
Correct |
537 ms |
32688 KB |
Output is correct |
27 |
Correct |
639 ms |
33608 KB |
Output is correct |
28 |
Correct |
578 ms |
34756 KB |
Output is correct |
29 |
Correct |
454 ms |
12356 KB |
Output is correct |
30 |
Correct |
1235 ms |
33712 KB |
Output is correct |
31 |
Correct |
2201 ms |
31860 KB |
Output is correct |
32 |
Correct |
1127 ms |
33428 KB |
Output is correct |