#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fs first
#define sc second
#define N 200010
#define pb push_back
#define ii pair<int,int>
int n,D,H[N];
struct IT
{
struct node
{
int l,r,l_child,r_child;
vector<int>lis;
};
vector<node>ST;
void init(int U)
{
ST.pb({1,U,-1,-1,{}});
}
void update(int id,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)
{
ST[id].lis.pb(val);
return;
}
int mid=(l+r)/2;
if(ST[id].l_child==-1)
ST[id].l_child=ST.size(),ST.pb({l,mid,-1,-1,{}});
if(ST[id].r_child==-1)
ST[id].r_child=ST.size(),ST.pb({mid+1,r,-1,-1,{}});
update(ST[id].l_child,u,v,val);
update(ST[id].r_child,u,v,val);
}
void solve(int id,int i,vector<int>&s)
{
if(id==-1)
return;
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;
if(ST[id].l_child!=-1)
solve(ST[id].l_child,i,s);
if(ST[id].r_child!=-1)
solve(ST[id].r_child,i,s);
}
}T[N];
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[])
{
for(int i=0;i<n;i++)
T[i].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[A[i]].update(0,mp[{A[i],B[i]}],i,B[i]);
T[B[i]].update(0,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[A[i]].update(0,mp[{A[i],B[i]}],U,B[i]);
T[B[i]].update(0,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=1000000000;
if(v==0)
return 1000000000;
vector<int>L,R;
T[x].solve(0,v,L);
T[y].solve(0,v,R);
sort(L.begin(),L.end(),SS);
sort(R.begin(),R.end(),SS);
if(L.empty()||R.empty())
return res;
/* 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]]));
}*/
for(auto u:L)
for(auto v:R)
res=min(res,abs(H[u]-H[v]));
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);
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];
for(int i=0;i<m;i++)
cin>>B[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
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
7248 KB |
Output is correct |
2 |
Correct |
7 ms |
7276 KB |
Output is correct |
3 |
Correct |
7 ms |
7212 KB |
Output is correct |
4 |
Correct |
23 ms |
13196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
503 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
532 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
101 ms |
35352 KB |
Output is correct |
2 |
Correct |
122 ms |
18164 KB |
Output is correct |
3 |
Correct |
155 ms |
15952 KB |
Output is correct |
4 |
Correct |
1976 ms |
25224 KB |
Output is correct |
5 |
Correct |
2219 ms |
30080 KB |
Output is correct |
6 |
Correct |
173 ms |
28180 KB |
Output is correct |
7 |
Correct |
1347 ms |
20024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
7 ms |
7248 KB |
Output is correct |
3 |
Correct |
7 ms |
7276 KB |
Output is correct |
4 |
Correct |
7 ms |
7212 KB |
Output is correct |
5 |
Correct |
23 ms |
13196 KB |
Output is correct |
6 |
Runtime error |
503 ms |
262144 KB |
Execution killed with signal 9 |
7 |
Halted |
0 ms |
0 KB |
- |