求助multiset的删除函数
  • 板块学术版
  • 楼主Resonate
  • 当前回复2
  • 已保存回复2
  • 发布时间2025/1/23 12:22
  • 上次更新2025/1/23 15:11:32
查看原帖
求助multiset的删除函数
753553
Resonate楼主2025/1/23 12:22

核心有问题的代码需要下午提出来,就先放赛时代码

题:https://www.luogu.com.cn/problem/P3113

code:

#include<bits/stdc++.h>
#define int long long
#define lb(x) (x&-x)
using namespace std;
int T;
void work();
signed main()
{
	T=1;
	while(T--)
	{
		work();
	}
}
struct node{
	int dis,l,r,q_s;//原距离,左节点,右节点,去除后的距离
	bool operator<(const node &T)const{return (T.q_s-T.dis)>(q_s-dis);}
};
int js(int x_1,int y_1,int x_2,int y_2)
{
	return abs(x_1-x_2)+abs(y_1-y_2);
}
int n,q;
pair<int,int>a[111111];
int tr[111111];
multiset<node,less<node>>p,tmp;
void change(int x,int y)
{
	for(;x<=100000;x+=lb(x))
	{
		tr[x]+=y;
	}
}
int sum(int x)
{
	int s=0;
	for(;x;x-=lb(x))
	{
		s+=tr[x];
	}
	return s;
}
void qj_change(int l,int r,int y)
{
	change(l,y);
	change(r+1,-y);
}
void work()
{
	cin>>n>>q;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i].first>>a[i].second;
	}
	for(int i=2;i<=n;i++)
	{
		qj_change(i,n,js(a[i-1].first,a[i-1].second,a[i].first,a[i].second));
	}
	// for(int i=1;i<=n;i++)
	// {
	// 	cout<<sum(i)<<" ";
	// }
	// cout<<'\n';
	p.insert({0,0,2,INT_MAX});
	p.insert({0,n-1,n+1,INT_MAX});
	for(int i=2;i<n;i++)
	{
		p.insert({js(a[i-1].first,a[i-1].second,a[i].first,a[i].second)+js(a[i].first,a[i].second,a[i+1].first,a[i+1].second),i-1,i+1,js(a[i-1].first,a[i-1].second,a[i+1].first,a[i+1].second)});
	}
	// while(!p.empty())
	// {
	// 	cout<<p.begin()->dis<<" "<<p.begin()->l<<" "<<p.begin()->r<<" "<<p.begin()->q_s<<endl;
	// 	p.erase(p.begin());
	// }
	// return;
	char c;
	for(int i=1,x,y,z;i<=q;i++)
	{
		cin>>c>>x>>y;
		if(c=='U')
		{
			//核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心
			cin>>z;
			cout<<p.size()<<'\n';
			cout<<"del:"<<js(a[x-1].first,a[x-1].second,a[x].first,a[x].second)+js(a[x].first,a[x].second,a[x+1].first,a[x+1].second)<<" "<<x-1<<" "<<x+1<<" "<<((x==1 or x==n)?(INT_MAX):js(a[x-1].first,a[x-1].second,a[x+1].first,a[x+1].second))<<'\n';
			p.erase({js(a[x-1].first,a[x-1].second,a[x].first,a[x].second)+js(a[x].first,a[x].second,a[x+1].first,a[x+1].second),x-1,x+1,((x==1 or x==n)?(INT_MAX):js(a[x-1].first,a[x-1].second,a[x+1].first,a[x+1].second))});
			cout<<p.size()<<'\n';
			if(x!=1)
			{
				qj_change(x,n,-js(a[x-1].first,a[x-1].second,a[x].first,a[x].second));
			}
			else
			{
				qj_change(x+1,n,-js(a[x].first,a[x].second,a[x+1].first,a[x+1].second));
			}
			a[x]={y,z};
			cout<<js(a[x-1].first,a[x-1].second,a[x].first,a[x].second)+js(a[x].first,a[x].second,a[x+1].first,a[x+1].second)<<" "<<x-1<<" "<<x+1<<" "<<((x==1 or x==n)?(INT_MAX):js(a[x-1].first,a[x-1].second,a[x+1].first,a[x+1].second))<<'\n';
			p.insert({js(a[x-1].first,a[x-1].second,a[x].first,a[x].second)+js(a[x].first,a[x].second,a[x+1].first,a[x+1].second),x-1,x+1,((x==1 or x==n)?(INT_MAX):js(a[x-1].first,a[x-1].second,a[x+1].first,a[x+1].second))});
			cout<<p.size()<<'\n';
			return;//核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心核心
			if(x!=1)
			{
				qj_change(x,n,js(a[x-1].first,a[x-1].second,a[x].first,a[x].second));
			}
			else
			{
				qj_change(x+1,n,js(a[x].first,a[x].second,a[x+1].first,a[x+1].second));
			}
		}
		else
		{
			while(x>=p.begin()->l+1 and p.begin()->l+1>=y)
			{
				cout<<x<<" "<<p.begin()->l+1<<" "<<y<<endl;
				tmp.insert(*p.begin());
				p.erase(p.begin());
			}
			cout<<sum(y)-sum(x-1)-p.begin()->dis+p.begin()->q_s<<'\n';
			while(!tmp.empty())
			{
				p.insert(*tmp.begin());
				tmp.erase(tmp.begin());
			}
		}
		cout<<"out:\n";
		while(!p.empty())
		{
			cout<<p.begin()->dis<<" "<<p.begin()->l<<" "<<p.begin()->r<<" "<<p.begin()->q_s<<'\n';
			tmp.insert(*p.begin());
			p.erase(p.begin());
		}
		while(!tmp.empty())
		{
			p.insert(*tmp.begin());
			tmp.erase(tmp.begin());
		}
		cout<<"over!\n\n";
	}
}

样例

5 5
-4 4
-5 -3
-1 5
-3 4
0 5
Q 1 5
U 4 0 1
U 4 -1 1
Q 2 4
Q 1 4

为什么删除1个后会少2个?

用find()的话会删错

2025/1/23 12:22
加载中...