rt,求大佬帮忙查错
代码:
#include<bits/stdc++.h>
using namespace std;
long long n,q,a[8005],op,x,v;
long long pos[8005],temp[8005],nw[8005];//pos[i]:最初下标为i的元素排序后位置 temp:排好序的数组 nw[i]:temp中下标为i的元素的初始位置
bool flag;
int main(){
freopen("sort.in","r",stdin);
freopen("sort.out","w",stdout);
cin>>n>>q;
for(int i=1;i<=n;i++){
cin>>a[i];
temp[i]=a[i];
nw[i]=i;
}
for(int i=1;i<=n;i++){
for(int j=i;j>=2;j--){
if(temp[j]<temp[j-1]){
swap(temp[j],temp[j-1]);
pos[nw[j]]=j-1;
pos[nw[j-1]]=j;
swap(nw[j],nw[j-1]);
}
// printf("i=%d,j=%d:\n",i,j);//debug
// for(int i=1;i<=n;i++){
// cout<<i<<':'<<pos[i]<<endl;
// }
}
}
while(q--){
cin>>op;
if(op==1){
cin>>x>>v;
a[x]=v;
temp[pos[x]]=v;
for(int i=pos[x];i>=2;i--){
if((temp[i]<temp[i-1])||(temp[i]==temp[i-1]&&nw[i]<nw[i-1])){
swap(temp[i],temp[i-1]);
swap(pos[x],pos[nw[i-1]]);
// swap(pos[x],pos[pos[i-1]]);
swap(nw[i],nw[i-1]);
}
}
for(int i=pos[x];i<n;i++){
if((temp[i]>temp[i+1])||(temp[i]==temp[i+1]&&nw[i]>nw[i+1])){
swap(temp[i],temp[i+1]);
// swap(pos[x],pos[pos[i+1]]);
swap(pos[x],pos[nw[i+1]]);
swap(nw[i],nw[i+1]);
}
}
// for(int i=1;i<=n;i++) cout<<i<<':'<<pos[i]<<endl;
// cout<<endl;
}
if(op==2){
cin>>x;
cout<<pos[x]<<endl;
}
}
return 0;
}