#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 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 'int question(int, int, int)':
potion.cpp:134:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<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: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
136 | while(pos<R.size()-1&&H[R[pos]]<=H[L[i]])
| ~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
125452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
125876 KB |
Output is correct |
2 |
Correct |
61 ms |
125836 KB |
Output is correct |
3 |
Correct |
62 ms |
125872 KB |
Output is correct |
4 |
Correct |
74 ms |
126856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
631 ms |
262144 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
613 ms |
262144 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
134 ms |
130376 KB |
Output is correct |
2 |
Correct |
144 ms |
128296 KB |
Output is correct |
3 |
Correct |
187 ms |
127412 KB |
Output is correct |
4 |
Correct |
860 ms |
129152 KB |
Output is correct |
5 |
Correct |
860 ms |
129868 KB |
Output is correct |
6 |
Correct |
189 ms |
129524 KB |
Output is correct |
7 |
Correct |
687 ms |
128072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
125452 KB |
Output is correct |
2 |
Correct |
58 ms |
125876 KB |
Output is correct |
3 |
Correct |
61 ms |
125836 KB |
Output is correct |
4 |
Correct |
62 ms |
125872 KB |
Output is correct |
5 |
Correct |
74 ms |
126856 KB |
Output is correct |
6 |
Runtime error |
631 ms |
262144 KB |
Execution killed with signal 11 |
7 |
Halted |
0 ms |
0 KB |
- |