結果
| 問題 |
No.772 Dynamic Distance Sum
|
| コンテスト | |
| ユーザー |
kmjp
|
| 提出日時 | 2019-06-30 14:15:18 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,816 bytes |
| コンパイル時間 | 1,698 ms |
| コンパイル使用メモリ | 177,048 KB |
| 実行使用メモリ | 21,040 KB |
| 最終ジャッジ日時 | 2024-07-06 01:18:21 |
| 合計ジャッジ時間 | 9,617 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 9 TLE * 1 -- * 17 |
ソースコード
#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;
}
kmjp