# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
884034 | vjudge1 | Bubble Sort 2 (JOI18_bubblesort2) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
vector<int>aib;
void update(int pos,int val,int n)
{
int i=pos;
while(i<=n)
{
aib[i]+=val;
i+=(i&(-i));
}
}
int get(int pos,int n)
{
int i=pos,s=0;
while(i>0)
{
s=s+aib[i];
i-=(i&(-i));
}
return s;
}
vector<int> countScans(vector<int>A, vector<int>X, vector<int>V)
{
int n=A.size(),q=X.size();
vector<int>Aux(n);
vector<int>A1(n);
for(int i=0;i<n;i++)
{
// cin>>A[i];
Aux[i]=A[i];
A1[i]=A[i];
}
sort(Aux.begin(),Aux.end());
for(int i=0;i<n;i++)
{
int l=-1,r=n-1;
while(l<r-1)
{
int mid=(l+r)/2;
if(A[i]<=Aux[mid])
r=mid;
else
l=mid;
}
A1[i]=r+1;
// cout<<A1[i]<<' ';
}
// cout<<'\n';
vector<int>ans;
for(int i=0;i<q;i++)
{
A[X[i]]=V[i];
for(int j=0;j<n;j++)
{
Aux[j]=A[j];
}
sort(Aux.begin(),Aux.end());
for(int j=0;j<n;j++)
{
int l=-1,r=n-1;
while(l<r-1)
{
int mid=(l+r)/2;
if(A[j]<=Aux[mid])
r=mid;
else
l=mid;
}
A1[j]=r+1;
// cout<<A1[j]<<' ';
}
aib.assign(n+1,0);
int ans1=0;//cout<<"asa";
for(int j=0;j<n;j++)
{
ans1=max(ans1,get(n,n)-get(A1[j],n));
// cout<<"asa";
update(A1[j],1,n);
}
ans.push_back(ans1);
// cout<<ans1<<'\n';
}
return ans;
}
int main()
{
int m,t;
cin>>m>>t;
vector<int>A(m);
vector<int>X(t);
vector<int>V(t);
for(int i=0;i<m;i++)
{
cin>>A[i];
}
for(int i=0;i<t;i++)
{
cin>>X[i]>>V[i];
}
countScans(A,X,V);
return 0;
}
/*
6 1
5 6 2 3 4 1
3 3
4 2
1 2 3 4
0 3
2 1
*/