QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#401732#5308. RPG Pro LeagueNaganohara_YoimiyaRE 1ms3624kbC++144.8kb2024-04-29 11:28:032024-04-29 11:28:03

Judging History

你现在查看的是最新测评结果

  • [2024-04-29 11:28:03]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:3624kb
  • [2024-04-29 11:28:03]
  • 提交

answer

#include<bits/stdc++.h>

#define ll long long
#define mk make_pair
#define fi first
#define se second

using namespace std;

inline int read(){
	int x=0,f=1;char c=getchar();
	for(;(c<'0'||c>'9');c=getchar()){if(c=='-')f=-1;}
	for(;(c>='0'&&c<='9');c=getchar())x=x*10+(c&15);
	return x*f;
}

template<typename T>void cmax(T &x,T v){x=max(x,v);}
template<typename T>void cmin(T &x,T v){x=min(x,v);}

const int N=1e5+5;
int n,q,now[N],val[N];
bool G[N][4];
struct Set{
	priority_queue<pair<int,int> >A,B;
	void clear(){
		priority_queue<pair<int,int> >().swap(A);
		priority_queue<pair<int,int> >().swap(B);
	}
	void insert(pair<int,int>x){A.push(mk(-x.fi,x.se));}
	void erase(pair<int,int>x){B.push(mk(-x.fi,x.se));}
	pair<int,int> top(){
		while(B.size()&&A.top()==B.top())A.pop(),B.pop();
		auto res=A.top();return mk(-res.fi,res.se);
	}
	int size(){return A.size()-B.size();}
}R[4],P[4],Q[4][4];

void ins(int x){
	if(now[x]==-1){
		for(int i=0;i<4;i++)if(G[x][i])P[i].insert(mk(val[x],x));
	}
	else R[now[x]].insert(mk(-val[x],x));
	if(now[x]!=-1){
		for(int i=0;i<4;i++)if(i!=now[x]&&G[x][i])Q[now[x]][i].insert(mk(0,x));
	}
}
void del(int x){
	if(now[x]==-1){
		for(int i=0;i<4;i++)if(G[x][i])P[i].erase(mk(val[x],x));
	}
	else R[now[x]].erase(mk(-val[x],x));
	if(now[x]!=-1){
		for(int i=0;i<4;i++)if(i!=now[x]&&G[x][i])Q[now[x]][i].erase(mk(0,x));
	}
}

void argu1(int i){ // s -> L[x] -> R[i]
	int x=P[i].top().se;
	del(x),now[x]=i,ins(x);
//	cout<<"  argu1 s -> L"<<x<<" -> R"<<i<<endl;
//	cout<<"  now = ";for(int i=1;i<=n;i++)cout<<now[i]<<" ";puts("");
}
void argu2(int i,int j){ // R[i] -> L[x] -> R[j]
	int x=Q[i][j].top().se;
	del(x),now[x]=j,ins(x);
//	cout<<"  argu2 R"<<i<<" -> L"<<x<<" -> R"<<j<<endl;
//	cout<<"  now = ";for(int i=1;i<=n;i++)cout<<now[i]<<" ";puts("");
}
void argu3(int i){ // R[i] -> L[x] -> s
	int x=R[i].top().se;
	del(x),now[x]=-1,ins(x);
//	cout<<"  argu3 R"<<i<<" -> L"<<x<<" -> s"<<endl;
//	cout<<"  now = ";for(int i=1;i<=n;i++)cout<<now[i]<<" ";puts("");
}

signed main(void){

#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
#endif

	n=read();
	for(int i=1;i<=n;i++){
		vector<int>vis(3);auto gv=[&](char c){if(c=='D')return 0;if(c=='S')return 1;return 2;};
		char c=getchar();while(c=='D'||c=='B'||c=='S')vis[gv(c)]=1,c=getchar();
		G[i][0]=vis[0],G[i][1]=vis[1],G[i][2]=vis[2],G[i][3]=(vis[0]|vis[1]),val[i]=read();
	}
	
	const ll INF=1e18;
	vector<vector<ll> >v(6,vector<ll>(6,INF));
	auto SP=[&](int s,int t){
		vector<vector<int> >pre(6,vector<int>(6,0));auto f=v;
		
		for(int i=0;i<6;i++)f[i][i]=0;
		for(int k=0;k<6;k++)for(int i=0;i<6;i++)for(int j=0;j<6;j++){
			if(f[i][k]+f[k][j]<f[i][j])f[i][j]=f[i][k]+f[k][j],pre[i][j]=k;
		}
		
		vector<int>path;
		function<void(int,int)>get_path=[&](int x,int y){
			if(v[x][y]==f[x][y])return path.emplace_back(x),void();
			int p=pre[x][y];get_path(x,p),get_path(p,y);
		};
		get_path(s,t);path.emplace_back(t);
		
		return mk(f[s][t],path);
	};
	auto clr=[&](){for(int i=0;i<6;i++)for(int j=0;j<6;j++)v[i][j]=INF;};
	
	for(int i=1;i<=n;i++)now[i]=-1,ins(i);
	
	ll ans=0;
	while(true){
		int chk=-1;ll cur=0;
//		puts("new team");
		for(int x=0;x<4;x++){
			for(int i=0;i<4;i++)for(int j=0;j<4;j++)if(Q[i][j].size())v[i][j]=0;
			for(int i=0;i<4;i++)if(P[i].size())v[4][i]=P[i].top().fi;
			v[x][5]=0;auto [cost,path]=SP(4,5);clr();
//			cout<<" argument path = ";for(int x:path)cout<<x<<" ";cout<<"cost = "<<cost<<endl;
			if(cost>=INF){chk=x-1;break;}
			argu1(path[1]);int k=path.size();assert(k>=3);
			for(int i=1;i<=k-3;i++)argu2(path[i],path[i+1]);
			cur+=cost;
		}
		if(chk==-1){ans+=cur;continue;}
		for(int x=chk;x>=0;x--){
			for(int i=0;i<4;i++)for(int j=0;j<4;j++)if(Q[i][j].size())v[i][j]=0;
			for(int i=0;i<4;i++)if(R[i].size())v[i][4]=P[i].top().fi;
			v[5][x]=0;auto [cost,path]=SP(5,4);clr();
//			cout<<" argument path = ";for(int x:path)cout<<x<<" ";cout<<"cost = "<<cost<<endl;
			
			int k=path.size();assert(k>=3);
			for(int i=1;i<=k-3;i++)argu2(path[i],path[i+1]);
			argu3(path[k-2]);
		}
		break;
	}
	
//	cout<<"now ans = "<<ans<<endl;
//	cout<<"now = ";for(int i=1;i<=n;i++)cout<<now[i]<<" ";puts("");
	
	q=read();
	for(int _=1;_<=q;_++){
		int x=read(),w=read();
		if(now[x]!=-1)ans+=w-val[x];
		del(x),val[x]=w,ins(x);
//		cout<<"now ans = "<<ans<<endl;
		for(int y=0;y<4;y++)if(P[y].size()){
			for(int i=0;i<4;i++)for(int j=0;j<4;j++)if(Q[i][j].size())v[i][j]=0;
			for(int i=0;i<4;i++)if(R[i].size())v[i][4]=R[i].top().fi;
			auto [cost,path]=SP(y,4);clr();
			if(cost+P[y].top().fi>=0)continue;
			ans+=cost+P[y].top().fi;
			argu1(path[1]);int k=path.size();assert(k>=3);
			for(int i=1;i<=k-3;i++)argu2(path[i],path[i+1]);
			argu3(path[k-2]);
			break;
		}
		cout<<ans<<'\n';
	}

	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3624kb

input:

5
BS 3
D 3
B 2
D 4
D 5
2
5 2
1 5

output:

10
12

result:

ok 2 number(s): "10 12"

Test #2:

score: -100
Runtime Error

input:

100000
B 727564174
S 231637747
D 246625257
S 975124756
D 844292741
S 809641006
B 396062588
B 883224128
B 311130283
S 434433581
S 509817048
S 993501344
S 508399542
B 279564062
D 950946642
D 819528264
D 412285620
B 968540732
S 953428318
D 902181790
S 172330493
D 725075
B 135580649
D 714478555
B 466916...

output:


result: