36分求调
查看原帖
36分求调
1037997
_TGWQ12_楼主2024/3/12 18:32
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int x,y,t,n;
long long num,h[N],b[N],a[N]; 
int ec(int x)//第一个大于下的数的下标 
{
	int l=0,r=n; //(l,r]
	while(l+1<r)
	{
		int mid=(l+r)/2;
		if(b[mid]>=x)
			r=mid;
		else
			l=mid;
	}
	return r;
}
void work()
{
	cin>>x>>y;
	int o=ec(a[x]);
	int p=ec(y);
	if(b[p]<y)
		p=n+1;
	if(p<o)
		cout<<num-a[x]*o+y*p+h[o-1]-h[p-1]<<'\n';
	else if(o<p)
		cout<<num-a[x]*(o)+y*(p-1)-(h[p-1]-h[o])<<'\n';
	else
		cout<<num-o*(a[x]-y)<<'\n';
}
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
		cin>>a[i],b[i]=a[i];
	sort(b+1,b+1+n);
	for(int i=1;i<=n;i++)
	{
		num+=i*b[i];
		h[i]=h[i-1]+b[i];
	}
	cin>>t;
	while(t--)
	{
		work();
	}
}
/*
6
1 1 4 5 1 4
3
6 2
1 7
*/
2024/3/12 18:32
加载中...