#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>
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)
{
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=1e9;
vector<int>L,R;
T[x].solve(0,v,L);
T[y].solve(0,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);
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
Compilation message
potion.cpp: In function 'int question(int, int, int)':
potion.cpp:116:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
116 | for(int i=0,pos=0;i<L.size();i++)
| ~^~~~~~~~~
potion.cpp:118:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
118 | while(pos<R.size()-1&&H[R[pos]]<=H[L[i]])
| ~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
4944 KB |
Output is correct |
2 |
Correct |
6 ms |
4816 KB |
Output is correct |
3 |
Correct |
6 ms |
4816 KB |
Output is correct |
4 |
Correct |
22 ms |
10772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
485 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
491 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
122 ms |
32996 KB |
Output is correct |
2 |
Correct |
110 ms |
15816 KB |
Output is correct |
3 |
Correct |
144 ms |
13612 KB |
Output is correct |
4 |
Correct |
840 ms |
22872 KB |
Output is correct |
5 |
Correct |
819 ms |
27724 KB |
Output is correct |
6 |
Correct |
161 ms |
25824 KB |
Output is correct |
7 |
Correct |
684 ms |
17688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2640 KB |
Output is correct |
2 |
Correct |
6 ms |
4944 KB |
Output is correct |
3 |
Correct |
6 ms |
4816 KB |
Output is correct |
4 |
Correct |
6 ms |
4816 KB |
Output is correct |
5 |
Correct |
22 ms |
10772 KB |
Output is correct |
6 |
Runtime error |
485 ms |
262144 KB |
Execution killed with signal 9 |
7 |
Halted |
0 ms |
0 KB |
- |