#include <iostream>
#include <vector>
#include <set>
#include <unordered_set>
#include <algorithm>
//#define debug(x) cerr << #x << " " << x << "\n"
#pragma GCC optimize("Ofast")
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
const int bSize = 20;
const int bits = 17;
const int NMAX = 100000;
const int VMAX = NMAX * 89;
int a[NMAX], n;
int u;
int f[NMAX];
int changer = 0;
int par[NMAX];
struct BLOCK {
int time;
vector <int> v;
};
class DS {
public:
vector <BLOCK> blocks;
vector <pii> changes;
vector <int> vecini;
void baga(int x, int cand) {
vecini.push_back(x);
changes.push_back({x, cand});
if(changes.size() % bSize == 0) {
BLOCK nou;
changer++;
nou.v.clear();
nou.time = cand;
for(auto x : vecini) {
par[x] = 0;
}
for(auto x : vecini) {
par[x]++;
}
for(auto x : vecini) {
if(par[x] % 2 == 1 && f[x] != changer) {
nou.v.push_back(x);
f[x] = changer;
}
}
vecini = nou.v; // asta e confirmed?
blocks.push_back(nou);
}
}
} ds[NMAX];
void init(int N, int D, int H[]) {
n = N;
for(int i = 0; i < N; i++) {
a[i] = H[i];
}
}
void curseChanges(int U, int A[], int B[]) {
u = U;
for(int i = 0; i < U; i++) {
int timp = i + 1;
ds[A[i]].baga(B[i], timp);
ds[B[i]].baga(A[i], timp);
}
}
bool cmp(int A, int B) {
if(a[A] != a[B])
return a[A] < a[B];
return A < B;
}
int impX[NMAX];
int impY[NMAX];
int intrebare;
int question(int x, int y, int v) {
int r = -1;
intrebare++;
int timpX = 0, timpY = 0;
int pas = (1 << bits);
while(pas) {
if(r + pas < ds[x].blocks.size() && ds[x].blocks[r + pas].time <= v)
r += pas;
pas /= 2;
}
vector <int> parcurgere[2];
unordered_set <int> stX;
if(r != -1) {
timpX = ds[x].blocks[r].time + 1;
parcurgere[0] = ds[x].blocks[r].v;
}
r = -1;
pas = (1 << bits);
while(pas) {
if(r + pas < ds[y].blocks.size() && ds[y].blocks[r + pas].time <= v)
r += pas;
pas /= 2;
}
if(r != -1) {
timpY = ds[y].blocks[r].time + 1;
parcurgere[1] = ds[y].blocks[r].v;
}
r = -1, pas = (1 << bits);
while(pas) {
if(r + pas < ds[x].changes.size() && ds[x].changes[r + pas].second < timpX)
r += pas;
pas /= 2;
}
r++;
for(; r < ds[x].changes.size() && ds[x].changes[r].second <= v; r++) {
pii change = ds[x].changes[r];
parcurgere[0].push_back(change.first);
}
r = -1, pas = (1 << bits);
while(pas) {
if(r + pas < ds[y].changes.size() && ds[y].changes[r + pas].second < timpY)
r += pas;
pas /= 2;
}
r++;
for(; r < ds[y].changes.size() && ds[y].changes[r].second <= v; r++) {
pii change = ds[y].changes[r];
parcurgere[1].push_back(change.first);
}
if(!parcurgere[0].size() || !parcurgere[1].size()) return 1e9;
sort(parcurgere[0].begin(), parcurgere[0].end(), cmp);
sort(parcurgere[1].begin(), parcurgere[1].end(), cmp);
int cnt = 0, i;
for(i = 0; i < parcurgere[0].size(); i++) {
if(i != 0 && parcurgere[0][i - 1] != parcurgere[0][i]) {
if(cnt % 2 == 0) {
impX[parcurgere[0][i - 1]] = intrebare;
}
cnt = 0;
}
cnt++;
}
if(cnt % 2 == 0) {
impX[parcurgere[0][i - 1]] = intrebare;
}
cnt = 0;
for(i = 0; i < parcurgere[1].size(); i++) {
if(i != 0 && parcurgere[1][i - 1] != parcurgere[1][i]) {
if(cnt % 2 == 0) {
impY[parcurgere[1][i - 1]] = intrebare;
}
cnt = 0;
}
cnt++;
}
if(cnt % 2 == 0) {
impY[parcurgere[1][i - 1]] = intrebare;
}
cnt = 0;
int minim = 1e9;
i = 0;
int j = 0;
while(i < parcurgere[0].size() && j < parcurgere[1].size()) {
if(impX[parcurgere[0][i]] == intrebare) {
i++;
continue;
}
if(impY[parcurgere[1][j]] == intrebare) {
j++;
continue;
}
minim = min(minim, abs(a[parcurgere[0][i]] - a[parcurgere[1][j]]));
if(a[parcurgere[0][i]] < a[parcurgere[1][j]])
i++;
else
j++;
}
return minim;
}
int main() {
int N, D, U, Q;
std::ios::sync_with_stdio(false);
std::cin.tie(NULL);
std::cin >> N >> D >> U >> Q;
int *F = new int[N];
for (int i=0; i<N; i++)
std::cin >> F[i];
init(N, D, F);
int *A = new int[U], *B = new int[U];
for (int i=0; i<U; i++) {
std::cin >> A[i] >> B[i];
}
curseChanges(U, A, B);
bool correct = true;
for (int i=0; i<Q; i++) {
int X,Y,V,CorrectAnswer;
std::cin >> X >> Y >> V;
cout << question(X,Y,V) << endl;
}
return 0;
}
Compilation message
potion.cpp: In function 'int question(int, int, int)':
potion.cpp:95:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<BLOCK>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
95 | if(r + pas < ds[x].blocks.size() && ds[x].blocks[r + pas].time <= v)
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
potion.cpp:108:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<BLOCK>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
108 | if(r + pas < ds[y].blocks.size() && ds[y].blocks[r + pas].time <= v)
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
potion.cpp:118:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
118 | if(r + pas < ds[x].changes.size() && ds[x].changes[r + pas].second < timpX)
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
potion.cpp:123:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
123 | for(; r < ds[x].changes.size() && ds[x].changes[r].second <= v; r++) {
| ~~^~~~~~~~~~~~~~~~~~~~~~
potion.cpp:129:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
129 | if(r + pas < ds[y].changes.size() && ds[y].changes[r + pas].second < timpY)
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
potion.cpp:134:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
134 | for(; r < ds[y].changes.size() && ds[y].changes[r].second <= v; r++) {
| ~~^~~~~~~~~~~~~~~~~~~~~~
potion.cpp:142:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
142 | for(i = 0; i < parcurgere[0].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~~~~~
potion.cpp:155:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
155 | for(i = 0; i < parcurgere[1].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~~~~~
potion.cpp:171:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
171 | while(i < parcurgere[0].size() && j < parcurgere[1].size()) {
| ~~^~~~~~~~~~~~~~~~~~~~~~
potion.cpp:171:41: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
171 | while(i < parcurgere[0].size() && j < parcurgere[1].size()) {
| ~~^~~~~~~~~~~~~~~~~~~~~~
potion.cpp: In function 'int main()':
potion.cpp:208:19: warning: unused variable 'CorrectAnswer' [-Wunused-variable]
208 | int X,Y,V,CorrectAnswer;
| ^~~~~~~~~~~~~
potion.cpp:206:10: warning: unused variable 'correct' [-Wunused-variable]
206 | bool correct = true;
| ^~~~~~~
/usr/bin/ld: /tmp/ccTpdJN1.o: in function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccvM4mW4.o:potion.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status