# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
492290 | ValiAntonie | The Potion of Great Power (CEOI20_potion) | C++11 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
struct querry{
int prim,doi,zi,indice;
}r[50005];
bool cmp(querry E, querry F){
return E.zi < F.zi;
}
int n,d,u,q,alt[100005],i,j,i2,j2,result,f[50005];
pair <int,int> rel[200005];
set <int> s[40000000];
set<int>::iterator itr;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n>>d>>u>>q;
for(i=0;i<n;i++){
cin>>alt[i];
}
for(i=1;i<=u;i++){
cin>>rel[i].first>>rel[i].second;
}
for(i=1;i<=q;i++){
cin>>r[i].prim>>r[i].doi>>r[i].zi;
r[i].indice = i;
}
sort(r+1,r+q+1,cmp);
for(i=1;i<=q;i++){
for(j=r[i-1].zi+1;j<=r[i].zi;j++){
if(s[rel[j].first].count(alt[rel[j].second]) == 0){
s[rel[j].first].insert(alt[rel[j].second]);
}
else{
s[rel[j].first].erase(alt[rel[j].second]);
}
if(s[rel[j].second].count(alt[rel[j].first]) == 0){
s[rel[j].second].insert(alt[rel[j].first]);
}
else{
s[rel[j].second].erase(alt[rel[j].first]);
}
}
vector<int> a(s[r[i].prim].begin(), s[r[i].prim].end());
vector<int> b(s[r[i].doi].begin(), s[r[i].doi].end());
result = 1000000000;
i2 = 0;
j2 = 0;
while(i2 < a.size() && j2 < b.size()){
if(abs(a[i2] - b[j2]) < result)
result = abs(a[i2] - b[j2]);
if(a[i2] < a[j2])
i2++;
else
j2++;
}
f[r[i].indice] = result;
}
for(i=1;i<=q;i++){
cout << f[i] << '\n';
cout.flush();
}
return 0;
}