# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
302370 | errorgorn | The Potion of Great Power (CEOI20_potion) | C++14 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//雪花飄飄北風嘯嘯
//天地一片蒼茫
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll,ll>
#define iii pair<ii,ll>
#define fi first
#define se second
#define debug(x) cout << #x << " is " << x << endl
#define rep(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
//change less to less_equal for non distinct pbds, but erase will bug
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
const int INF=1e9; //no answer case
int n,d,m,q;
int h[100005];
int tmp[100005];
int id[100005];
vector<ii> vm[100005];
int IDX=0;
vector<int> v[300005];
vector<ii> edits[300005];
void upd(vector<int> &v,int j){
vector<int> res;
bool used=false;
for (auto &it:v){
if (it==j){
used=true;
continue;
}
if (!used && j<it){
used=true;
res.push_back(j);
}
res.push_back(it);
}
if (!used) res.push_back(j);
swap(v,res);
}
void upd(int i,int j,int day){
int temp=vm[i].back().se;
if (sz(edits[temp])<2){
edits[temp].push_back(ii(day,j));
return;
}
v[IDX]=v[temp];
for (auto &it:edits[temp]) upd(v[IDX],it.se);
upd(v[IDX],j);
vm[i].push_back(ii(day,IDX++));
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>d>>m>>q;
rep(x,0,n) cin>>h[x];
rep(x,0,n) tmp[x]=x;
sort(tmp,tmp+n,[](int i,int j){
return h[i]<h[j];
});
rep(x,0,n) id[tmp[x]]=x;
//rep(x,0,n) cout<<id[x]<<" "; cout<<endl;
sort(h,h+n);
rep(x,0,n) vm[x].push_back(ii(0,IDX++));
int a,b,c;
rep(x,0,m){
cin>>a>>b;
a=id[a],b=id[b];
upd(a,b,x+1);
upd(b,a,x+1);
//cout<<vm[a].back().se<<" "<<vm[b].back().se<<endl;
//for (auto &it:v[vm[a].back().se]) cout<<it<<" "; cout<<endl;
//for (auto &it:v[vm[b].back().se]) cout<<it<<" "; cout<<endl;
}
while (q--){
cin>>a>>b>>c;
a=id[a],b=id[b];
int ti=(*--upper_bound(all(vm[a]),ii(c,IDX))).se;
int tj=(*--upper_bound(all(vm[b]),ii(c,IDX))).se;
vector<int> vi=v[ti],vj=v[tj];
for (auto &it:edits[ti]) if (it.fi<=c) upd(vi,it.se);
for (auto &it:edits[tj]) if (it.fi<=c) upd(vj,it.se);
//cerr<<ti<<" "<<tj<<endl;
//for (auto &it:vi) cerr<<it<<" "; cerr<<endl;
//for (auto &it:vj) cerr<<it<<" "; cerr<<endl;
int ans=INF;
int di=0,dj=0;
while (di!=sz(vi) && dj!=sz(vj)){
if (vi[di]<vj[dj]){
ans=min(ans,h[vj[dj]]-h[vi[di]]);
di++;
}
else{
ans=min(ans,h[vi[di]]-h[vj[dj]]);
dj++;
}
}
cout<<ans<<endl;
}
}