关于刚才B全部RE
查看原帖
关于刚才B全部RE
750576
I_Will_Not_Give_Code楼主2023/1/14 18:05

大样例都过了,虽然会T,但是为什么RE

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cmath>
#define int long long
using namespace std;
const int N=1e6+10;
typedef pair<int,int> PII;
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	int n,m,ans=0;
	cin>>n>>m;
	vector<int> a;
	for(int i=0;i<n;i++) 
	{
		int x;
		cin>>x;
		a.push_back(x);
	}
	sort(a.begin(),a.end());
	for(int i=0;i<a.size();i++)
	{
		if(i!=n-1) ans+=(a[i+1]-a[i]);
		else ans+=(a[n-1]-a[0]);
	}
	while(m--)
	{
		int op,x;
		cin>>op>>x;
		if(op==1)
		{
			int w=lower_bound(a.begin(),a.end(),x)-a.begin();
			if(a[w]==x)
			{
				int w1=w+1,w2=w-1;
				if(w==a.size()-1) w1=0;
				if(w==0) w2=a.size()-1;		
				ans+=abs(a[w1]-a[w2]);
				ans-=(abs(a[w1]-a[w])+abs(a[w2]-a[w]));
				a.erase(a.begin()+w);
				cout<<ans<<'\n';
			}
			else cout<<"-1\n";
		}
		else
		{
			a.insert(lower_bound(a.begin(),a.end(),x),x);
			int w=lower_bound(a.begin(),a.end(),x)-a.begin();
			int w1=w+1,w2=w-1;
			if(w==a.size()-1) w1=0;
			if(w==0) w2=a.size()-1;		
			ans-=abs(a[w1]-a[w2]);
			ans+=(abs(a[w1]-a[w])+abs(a[w2]-a[w]));
			cout<<ans<<'\n';
		}
	}
	return 0;
}

2023/1/14 18:05
加载中...