比赛 20241127 评测结果 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
题目名称 魔法传送阵 最终得分 100
用户昵称 flyfree 运行时间 12.897 s
代码语言 C++ 内存使用 95.89 MiB
提交时间 2024-11-27 10:55:04
显示代码纯文本
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MAXN 1000010
#define debug cout<<"flyfree\n";
inline ll read(){
	ll x=0,f=1;
	char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-')f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9'){
		x=x*10+c-'0';
		c=getchar();
	}
	return x*f;
}
struct node{
	ll l,r;
};
bool cmp(node a,node b){
	return (a.l+a.r)<(b.l+b.r);
}
struct treap{
	ll idx,rot,q;
	ll s[MAXN][2],val[MAXN],siz[MAXN],sum[MAXN],key[MAXN],sht[MAXN];
	void mk(){
		memset(s,0,sizeof(s));
		memset(val,0,sizeof(val));
		memset(siz,0,sizeof(siz));
		memset(sum,0,sizeof(sum));
		memset(key,0,sizeof(key));
		rot=0,idx=0;
	}
	ll newnode(ll x){
		ll now;
		if(q){
			now=sht[q--];
		}else now=++idx;
		val[now]=sum[now]=x;
		s[now][0]=s[now][1]=0;
		siz[now]=1;
		key[now]=rand();
		return now;
	}
	void push_up(ll p){
		sum[p]=val[p]+sum[s[p][0]]+sum[s[p][1]];
		siz[p]=siz[s[p][0]]+siz[s[p][1]]+1;
	}
	void split_val(ll p,ll v,ll &x,ll &y){
//		cout<<p<<endl;
		if(!p){
			x=y=0;
			return;
		}
		if(val[p]<=v){
			x=p;
			split_val(s[p][1],v,s[p][1],y);
		}else{
			y=p;
			split_val(s[p][0],v,x,s[p][0]);
		}
		push_up(p);
	}
	void split_siz(ll p,ll k,ll &x,ll &y){
		if(!p){
			x=y=0;
			return;
		}
		if(siz[s[p][0]]<k){
			x=p;
			split_siz(s[p][1],k-siz[s[p][0]]-1,s[p][1],y);
		}else{
			y=p;
			split_siz(s[p][0],k,x,s[p][0]);
		}
		push_up(p);
	}
	ll merge(ll x,ll y){
		if(!x||!y)return x|y;
		if(key[x]<=key[y]){
			s[x][1]=merge(s[x][1],y);
			push_up(x);
			return x;
		}else{
			s[y][0]=merge(x,s[y][0]);
			push_up(y);
			return y;
		}
	}
	void insert(ll v){
		ll x,y,z;
		split_val(rot,v,x,y);
//		debug
		z=newnode(v);
		rot=merge(merge(x,z),y);
	}
	void del(ll v){
		ll x,y,z;
		split_val(rot,v,x,y);
		split_val(x,v-1,x,z);
//		siz[z]=val[z]=sum[z]=0;
		sht[++q]=z;
		z=merge(s[z][0],s[z][1]);
		rot=merge(merge(x,z),y);
	}
	ll re(){
		ll mid=(siz[rot])/2;
		ll x,y,ans;
//		cout<<sum[rot]<<endl;
		split_siz(rot,mid,x,y);
		ans=sum[y]-sum[x];
		rot=merge(x,y);
		return ans;
	}
};
treap tra,trb;
node line[MAXN];
ll n,k,ansz,cnt,maxz=1e9;
void work1(){
	cout<<ansz+tra.re()<<endl;
}
void work2(){
	ll ans=tra.re();
	for(int i=1;i<=cnt;i++){
		tra.del(line[i].l),tra.del(line[i].r);
		trb.insert(line[i].l),trb.insert(line[i].r);
		ans=min(ans,tra.re()+trb.re());
//		cout<<i<<" "<<ans<<endl;
	}
	cout<<ans+ansz<<endl;
}
int main(){
	freopen("bridge.in","r",stdin);
	freopen("bridge.out","w",stdout);
//	k=read(),n=read();
	tra.mk(),trb.mk();
	srand((unsigned)time(0));
	cin>>k>>n;
	for(ll i=1;i<=n;i++){
		char sida,sidb;
		ll l,r;
		cin>>sida>>l>>sidb>>r;
//		if(n!=1)cout<<l<<" "<<r<<endl;
		if((sida=='A'&&sidb=='A')||(sida=='B'&&sidb=='B'))ansz+=abs(r-l);
		else{
			line[++cnt]={min(l,r),max(l,r)};
			tra.insert(l),tra.insert(r);
//			ansz++;
		}
	}
//	cout<<cnt<<endl;
//	debug;
	if(!cnt){
		cout<<ansz<<endl;
		return 0;
	}
	ansz+=cnt;
	sort(line+1,line+1+cnt,cmp);
//	for(ll i=1;i<=cnt;i++){
//		cout<<line[i].l<<" "<<line[i].r<<endl;
//		tra.insert(line[i].l),tra.insert(line[i].r);
//		debug
//	}
	if(k==1)work1();
	else work2();	
	return 0;
}