結果

問題 No.772 Dynamic Distance Sum
ユーザー 👑 kmjpkmjp
提出日時 2019-06-30 14:14:05
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 1,817 bytes
コンパイル時間 2,718 ms
コンパイル使用メモリ 173,260 KB
実行使用メモリ 816,408 KB
最終ジャッジ日時 2023-09-20 04:49:27
合計ジャッジ時間 8,402 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,120 KB
testcase_01 AC 4 ms
8,248 KB
testcase_02 AC 4 ms
8,320 KB
testcase_03 AC 4 ms
8,168 KB
testcase_04 MLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N,Q;
int X[101010];
set<pair<int,int>> E[101010];
pair<int,ll> dp[101010];
ll mi;

void dfs(int cur,int pre) {
	dp[cur]={X[cur],0};
	FORR(e,E[cur]) if(e.first!=pre) {
		dfs(e.first,cur);
		dp[cur].first+=dp[e.first].first;
		dp[cur].second+=dp[e.first].first*e.second+dp[e.first].second;
	}
}

void dfs2(int cur,int pre,int num,ll tot) {
	dp[cur].first+=num;
	dp[cur].second+=tot;
	mi=min(mi,dp[cur].second);
	FORR(e,E[cur]) if(e.first!=pre) {
		ll sc=dp[cur].second+(dp[cur].first-2*dp[e.first].first)*e.second-dp[e.first].second;
		dfs2(e.first,cur,dp[cur].first-dp[e.first].first,sc);
	}
	
}

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>Q;
	FOR(i,N) X[i+1]=1;
	ll S=0;
	while(Q--) {
		int a,b,c;
		cin>>i;
		if(i==1) {
			cin>>a>>b>>c;
			a=(a-1+S)%N+1;
			b=(b-1+S)%N+1;
			E[a].insert({b,c});
			E[b].insert({a,c});
		}
		else if(i==2) {
			cin>>a>>b;
			a=(a-1+S)%N+1;
			b=(b-1+S)%N+1;
			E[a].erase(E[a].lower_bound({b,0}));
			E[b].erase(E[b].lower_bound({a,0}));
		}
		else if(i==3) {
			cin>>a;
			a=(a-1+S)%N+1;
			
			X[a]^=1;
			dfs(a,a);
			mi=1LL<<60;
			dfs2(a,a,0,0);
			cout<<mi<<endl;
			S=(S+mi)%N;
		}
	}
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0