UVA 11990 TLE求助
  • 板块灌水区
  • 楼主lfxxx_
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/10/10 13:27
  • 上次更新2024/10/10 17:53:53
查看原帖
UVA 11990 TLE求助
795344
lfxxx_楼主2024/10/10 13:27

rt,能过P3157

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e5+5,B=1005;
int a[N];
bitset<N>de;
int L[B],R[B],pos[N];
int numpos[N];
vector<int>block[B];
inline int query1(int l,int r,int v)//求比 v 小的数的个数
{
	int x=pos[l],y=pos[r],ans=0;
	if(x==y)
	{
		for(int i=l;i<=r;++i)
			if(a[i]<v&&!de[i])
				++ans;
		return ans;
	}
	ans+=query1(l,R[x],v)+query1(L[y],r,v);
	for(int i=x+1;i<y;++i)
	{
		int x=lower_bound(block[i].begin(),block[i].end(),v)-block[i].begin();
		if(block[i][x]==v)
			--x;
		ans+=max(0LL,x);
	}
	return ans;
}
inline int query2(int l,int r,int v)//求解比 v 大的数的个数
{
	int x=pos[l],y=pos[r],ans=0;
	if(x==y)
	{
		for(int i=l;i<=r;++i)
			if(a[i]>v&&!de[i])
				++ans;
		return ans;
	}
	ans+=query2(l,R[x],v)+query2(L[y],r,v);
	for(int i=x+1;i<y;++i)
	{
		int x=upper_bound(block[i].begin(),block[i].end(),v)-block[i].begin();
		ans+=block[i].size()-x;
	}
	return ans;
}
void update(const int &x)
{
	const int p=numpos[x];
	de.set(p,1);
	block[pos[p]].erase(lower_bound(block[pos[p]].begin(),block[pos[p]].end(),x));
}
void solve(int n,int m)
{
	de.reset();
	int t=sqrt(n);
	for(int i=1;i<=n;++i)
		cin>>a[i],numpos[a[i]]=i;
	for(int i=1;i<=t;++i)
	{
		L[i]=(i-1)*t+1;
		R[i]=i*t;
	}
	if(R[t]<n)
	{
		++t;
		L[t]=R[t-1]+1;
		R[t]=n;
	}
	for(int i=1;i<=t;++i)
	{
		block[i].clear();
		for(int j=L[i];j<=R[i];++j)
		{
			block[i].emplace_back(a[j]);
			pos[j]=i;
		}
		sort(block[i].begin(),block[i].end());
	}
	int ans=0;
	for(int i=1;i<=n;++i)
		ans+=query1(i,n,a[i]);
	while(m--)
	{
		int x;
		cin>>x;
		cout<<ans<<'\n';
		ans-=query2(1,numpos[x],x)+query1(numpos[x],n,x);
		update(x);
	}
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n,m;
	while(cin>>n>>m)
		solve(n,m);
}
2024/10/10 13:27
加载中...