結果

問題 No.772 Dynamic Distance Sum
ユーザー 👑 kmjpkmjp
提出日時 2019-06-30 14:15:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,816 bytes
コンパイル時間 2,775 ms
コンパイル使用メモリ 173,556 KB
実行使用メモリ 18,812 KB
最終ジャッジ日時 2023-09-20 05:08:15
合計ジャッジ時間 11,767 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,036 KB
testcase_01 AC 3 ms
8,112 KB
testcase_02 AC 3 ms
8,128 KB
testcase_03 AC 4 ms
8,092 KB
testcase_04 AC 36 ms
8,112 KB
testcase_05 AC 9 ms
8,244 KB
testcase_06 AC 18 ms
8,124 KB
testcase_07 AC 9 ms
8,148 KB
testcase_08 AC 13 ms
8,100 KB
testcase_09 AC 40 ms
8,116 KB
testcase_10 AC 33 ms
8,364 KB
testcase_11 AC 6 ms
8,076 KB
testcase_12 TLE -
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<ll,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