#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fs first
#define sc second
#define N 100010
#define pb push_back
#define ii pair<int,int>
#define int ll
int UWU,n,D,H[N];
struct IT
{
int dem=0,cnt=0;
struct node
{
int l_child,r_child;
vector<int>lis;
};
node ST[N*40];
void init(int U)
{
ST[dem]={-1,-1,{}};
dem++;
}
bool giao(int l,int r,int u,int v)
{
return max(l,u)<=min(r,v);
}
void update(int id,int l,int r,int u,int v,int val)
{
// int l=ST[id].l,r=ST[id].r;
if(l>v||r<u)
return;
if(l>=u&&r<=v)
{
if(++cnt>1e7)
while(1);
ST[id].lis.pb(val);
return;
}
int mid=(l+r)/2;
if(giao(l,mid,u,v))
{
if(ST[id].l_child==-1)
ST[id].l_child=dem,ST[dem++]={-1,-1,{}};
update(ST[id].l_child,l,mid,u,v,val);
}
if(giao(u,v,mid+1,r))
{
if(ST[id].r_child==-1)
ST[id].r_child=dem,ST[dem++]={-1,-1,{}};
update(ST[id].r_child,mid+1,r,u,v,val);
}
}
void solve(int id,int l,int r,int i,vector<int>&s)
{
// int l=ST[id].l,r=ST[id].r;
if(l>i||r<i)
return;
for(auto u:ST[id].lis)
s.pb(u);
if(l==r)
return;
int mid=(l+r)/2;
if(ST[id].l_child!=-1)
solve(ST[id].l_child,l,mid,i,s);
if(ST[id].r_child!=-1)
solve(ST[id].r_child,mid+1,r,i,s);
}
}T;
void init(int num, int cc, int heigh[])
{
n=num;
D=cc;
for(int i=0;i<n;i++)
H[i]=heigh[i];//,cout<<H[i]<<endl;
}
map<ii,int>mp;
void curseChanges(int U, int A[], int B[])
{
UWU=U;
for(int i=0;i<n;i++)
T.init(U);
for(int i=0;i<U;i++)
{
if(A[i]>B[i])
swap(A[i],B[i]);
if(mp[{A[i],B[i]}]!=0)
{
T.update(A[i],1,UWU,mp[{A[i],B[i]}],i,B[i]);
T.update(B[i],1,UWU,mp[{A[i],B[i]}],i,A[i]);
//cout<<A[i]<<" "<<B[i]<<endl;
mp[{A[i],B[i]}]=0;
}else
{
mp[{A[i],B[i]}]=i+1;
}
}
for(int i=0;i<U;i++)
if(mp[{A[i],B[i]}]!=0)
{
//cout<<A[i]<<" "<<B[i]<<endl;
T.update(A[i],1,UWU,mp[{A[i],B[i]}],U,B[i]);
T.update(B[i],1,UWU,mp[{A[i],B[i]}],U,A[i]);
mp[{A[i],B[i]}]=0;
}
}
bool SS(const int&u,const int&v)
{
return H[u]<H[v];
}
int question(int x, int y, int v)
{
int res=1e9;
vector<int>L,R;
T.solve(x,1,UWU,v,L);
T.solve(y,1,UWU,v,R);
if(L.empty()||R.empty())
return res;
sort(L.begin(),L.end(),SS);
sort(R.begin(),R.end(),SS);
for(int i=0,pos=0;i<L.size();i++)
{
while(pos<R.size()-1&&H[R[pos]]<=H[L[i]])
{
res=min(res,abs(H[L[i]]-H[R[pos]]));
pos++;
}
res=min(res,abs(H[L[i]]-H[R[pos]]));
}
return res;
}
#ifdef SKY
int main()
{
freopen("A.inp","r",stdin);
freopen("A.out","w",stdout);
ios::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
srand(time(0));
int n,D;
cin>>n>>D;
int H[n];
for(int i=0;i<n;i++)
cin>>H[i];
init(n,D,H);
int m;
cin>>m;
int A[m],B[m];
for(int i=0;i<m;i++)
{
// cin>>A[i];
A[i]=rand()%n;
}
for(int i=0;i<m;i++)
{
// cin>>B[i];
do{
B[i]=rand()%n;
}while(B[i]==A[i]);
}
curseChanges(m,A,B);
int q;
cin>>q;
while(q--)
{
int x,y,T;
cin>>x>>y>>T;
cout<<question(x,y,T)<<endl;
}
return 0;
}
#endif // SKY
Compilation message
potion.cpp: In function 'long long int question(long long int, long long int, long long int)':
potion.cpp:134:24: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
134 | for(int i=0,pos=0;i<L.size();i++)
| ~^~~~~~~~~
potion.cpp:136:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
136 | while(pos<R.size()-1&&H[R[pos]]<=H[L[i]])
| ~~~^~~~~~~~~~~
/usr/bin/ld: /tmp/ccDTGpVF.o: in function `main':
grader.cpp:(.text.startup+0xec): undefined reference to `init(int, int, int*)'
/usr/bin/ld: grader.cpp:(.text.startup+0x184): undefined reference to `curseChanges(int, int*, int*)'
/usr/bin/ld: grader.cpp:(.text.startup+0x1d9): undefined reference to `question(int, int, int)'
collect2: error: ld returned 1 exit status