이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#pragma GCC target("avx2")
#include<bits/stdc++.h>
//#include<x86intrin.h>
//#include<immintrin.h>
//#pragma GCC optimize("unroll-loops")
using namespace std;
#define ll long long
#define int ll
#define ull unsigned long long
#define ld long double
#define rep(a) rep1(i,a)
#define rep1(i,a) rep2(i,0,a)
#define rep2(i,b,a) for(int i=(b); i<((int)(a)); i++)
#define rep3(i,b,a) for(int i=(b); i>=((int)(a)); i--)
#define all(a) a.begin(),a.end()
#define pii pair<int,int>
#define pb push_back
//#define inf 1010000000
#define inf 4000000000000000000
#define eps 1e-9
#define sz(a) ((int)a.size())
#define pow2(x) (1ll<<(x))
//#define ceiling(a,b) (((a)+(b)-1)/(b))
#ifdef i_am_noob
#define bug(...) cerr << "#" << __LINE__ << ' ' << #__VA_ARGS__ << "- ", _do(__VA_ARGS__)
template<typename T> void _do(T && x) {cerr << x << endl;}
template<typename T, typename ...S> void _do(T && x, S&&...y) {cerr << x << ", "; _do(y...);}
#else
#define bug(...) 826
#endif
inline char readchar(){
const int maxn=1000000;
static char buf[maxn],*p=buf,*q=buf;
if(p==q&&(q=(p=buf)+fread(buf,1,maxn,stdin))==buf) return EOF;
else return *p++;
}
inline int readint(){
int c=readchar(),x=0,neg=0;
while((c<'0'||c>'9')&&c!='-'&&c!=EOF) c=readchar();
if(c=='-') neg=1,c=readchar();
while(c>='0'&&c<='9') x=(x<<3)+(x<<1)+(c^'0'),c=readchar();
return neg?-x:x;
}
const int Mod=1000000007,Mod2=998244353;
const int MOD=Mod;
const int maxn=200005;
//i_am_noob
//#define v8si __attribute__ (( vector_size(32) )) signed
int n,m,res,cnt[maxn];
vector<pii> vec[maxn];
int solve(int x){
vector<int> tar(n),pre(n);
rep(n) tar[i]=max(0ll,(cnt[i]-x+1)/2);
multiset<pii> st;
priority_queue<pii> pq;
int res=0,cur=0;
rep(n){
for(auto [j,k]: vec[i]) pq.push({j,k});
//while(sz(st)&&((*st.begin()).first)<=i) st.erase(st.begin());
cur+=pre[i];
while(cur<tar[i]){
auto [x,y]=pq.top();
int k=min(y,tar[i]-cur);
pre[i]+=k,pre[x]-=k;
cur+=k;
res+=k;
pq.pop();
if(y-k) pq.push({x,y-k});
}
}
bug(res);
return res;
}
void orzck(){
cin >> n >> m;
rep(m){
int a,b,c;
cin >> a >> b >> c;
a--,b--;
if(a>b) swap(a,b);
cnt[a]+=c,cnt[b]-=c;
vec[a].pb({b,c});
}
rep(n-1) cnt[i+1]+=cnt[i];
res=inf;
rep1(_,2){
int l=1,r=(m+49)*1e9;
while(l<r){
int mid=l+r>>1;
if(solve(2*mid-2+_)-solve(2*mid+_)<=2) r=mid;
else l=mid+1;
}
res=min(res,solve(2*l-2+_)+(2*l-2+_));
}
cout << res << endl;
}
signed main(){
ios_base::sync_with_stdio(0),cin.tie(0);
#ifdef i_am_noob
freopen("input1.txt","r",stdin);
freopen("output1.txt","w",stdout);
freopen("output2.txt","w",stderr);
#endif
cout << fixed << setprecision(15);
orzck();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |