SOS SOS SOS SOS SOS SOS SOS SOS SOS
查看原帖
SOS SOS SOS SOS SOS SOS SOS SOS SOS
499231
Jacky2009楼主2023/9/1 21:13

实在是写不过去了一直48分求救

思路: 加入一个新数先看一看匹配谁,如果那个数有匹配就比较哪个更好,没有匹配就直接匹配。删除时如果这个数不止一个那么不管,如果这个数删完了而且有匹配,就删掉它的匹配然后重新加会去在匹配一次。

代码:

#include<bits/stdc++.h>
using namespace std;
set<int>ch;
#define IT set<int>::iterator
map<int,int>pr;
map<int,int>ds1;
set<int>single;
struct node{
	int a,b,sum;
	node(int A=0,int B=0,int S=0){
		if(A>B)swap(A,B);
		a=A;
		b=B;
		sum=S;
	}
	friend bool operator<(const node&a,const node&b){
		return a.sum>b.sum;
	}
};
#define NT multiset<node>::iterator
multiset<node>ch2;
int siz,n,op,x,C,a,b,sum1,sum2,sp,lastans,sg;
void adde(int x){
	IT tmp=ch.upper_bound(C-1-x);
	ds1[x]++;
	if(x<C&&tmp!=ch.begin()){
		tmp--;
		a=*tmp;
		if(a+x>=C){
			;
		}
		else if(pr[a]){
			if(pr[a]>=x);
			else{
				ch2.erase(node(a,pr[a],a+pr[a]));
				sp--;
			//	cout<<"DESTROY PAIR "<<a<<" "<<pr[a]<<endl;
				pr[pr[a]]=0;
				pr[a]=x;
				pr[x]=a;
			//	cout<<"MAKE PAIR "<<a<<" "<<x<<endl;
				ch2.insert(node(a,x,a+x));
				sp++;
			}
		}
		else{
			pr[a]=x;
			pr[x]=a;
			ch2.insert(node(a,x,a+x));
			sp++;
		}
	}
	if(ds1[x]==1)ch.insert(x);
}
void del(int x){
	if(pr[x]){
		ds1[x]--;
		if(ds1[x]==0){
			ch.erase(x);
			ch2.erase(node(x,pr[x],x+pr[x]));
		//	cout<<"DESTROY PAIR "<<x<<" "<<pr[x]<<endl;
			int tmq=pr[x];
			ds1[tmq]--;
			if(ds1[tmq]==0)ch.erase(tmq);
			pr[x]=pr[tmq]=0;
			adde(tmq);
			sp--;
		
	}
	else {
		ds1[x]--;
		if(ds1[x]==0)ch.erase(x);
	}	
}
int main(){
	cin.tie(0);
	cout.tie(0);
	cin>>n>>C;
	for(int i=1;i<=n;i++){
		cin>>op>>x;
		x^=lastans;
		x%=C;
		if(op==1)siz++;
		else siz--;
		if(op==1)adde(x);
		else del(x);
		sum1=sum2=0;
		if(siz<2){
			cout<<"EE\n";
			lastans=0;
		}
		else{
			IT tmp=ch.end();
			tmp--;
			sum1+=*tmp;
			tmp--;
			sum1+=*tmp;
			sum1%=C;
			if(sp){
				NT tmq=ch2.begin();
				sum2=tmq->sum;
			}
			cout<<(lastans=max(sum1,sum2))<<endl;
		}
	}
}
2023/9/1 21:13
加载中...