This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define here cerr<<"===========================================\n"
#define dbg(x) cerr<<#x<<": "<<x<<endl;
#include "bits/stdc++.h"
//#include <ext/pb_ds/tree_policy.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
#define ld double
#define ll long long
#define llinf 100000000000000000LL // 10^17
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define sz(a) (ll)(a.size())
#define all(a) a.begin(),a.end()
#define ceri(a,l,r) {cerr<<#a<<": ";for(ll i_ = l;i_<=r;i_++) cerr<<a[i_]<< " ";cerr<<endl;}
#define cer(a) {cerr<<#a<<": ";for(ll x_ : a) cerr<<x_<< " ";cerr<<endl;}
#define daj_mi_malo_vremena ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
using namespace std;
//using namespace __gnu_pbds;
/*
ll add(ll x,ll y){
x+=y;
if(x<0){
x%=mod;
x+=mod;
}else{
if(x>=mod) x%=mod;
}
return x;
}
ll mul(ll a,ll b){
ll ans = (a*b)%mod;
if(ans<0) ans+=mod;
return ans;
}
typedef tree<int,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
typedef tree<int,null_type,less_equal<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_multiset;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll rnd(ll l,ll r){
return uniform_int_distribution<ll>(l,r)(rng);
}
*/
#define maxn 200005
ll n,m;
ll a[maxn],b[maxn],c[maxn];
ll ps[maxn],add[maxn],need[maxn],cnt[maxn];
vector<ll> v[maxn];
bool cmp(ll x,ll y){return b[x]<b[y];}
priority_queue<ll,vector<ll>,decltype(&cmp)> pq(cmp);
bool moe(ll x,ll pos,ll f){
for(ll i = 1;i<=n;i++) add[i] = 0;
for(ll i = 1;i<=m;i++) cnt[i] = c[i];
for(ll i = 1;i<pos;i++) need[i] = (ps[i]+f-x+1)/2;
need[pos] = f;
ll cur = 0;
while(sz(pq)) pq.pop();
for(ll i = 1;i<=pos;i++){
for(ll j : v[i]){
if(b[j]>pos){
pq.push(j);
cnt[j] = c[j];
}
}
while(sz(pq)&&cur<need[i]){
ll j = pq.top();
ll d = min(cnt[j],need[i]-cur);
cur+=d;
cnt[j]-=d;
add[1]+=d;
add[a[j]]-=2*d;
add[b[j]]+=2*d;
if(cnt[j]==0) pq.pop();
}
if(cur<need[i]) return 0;
}
for(ll i = 1;i<=n;i++) add[i]+=add[i-1];
for(ll i = 1;i<=n;i++) if(add[i]+ps[i]>x) return 0;
return 1;
}
void tc(){
cin >> n >> m;
for(ll i = 1;i<=n;i++){
cin >> a[i] >> b[i] >> c[i];
if(a[i]>b[i]) swap(a[i],b[i]);
ps[a[i]]+=c[i];
ps[b[i]]-=c[i];
v[a[i]].pb(i);
}
for(ll i = 1;i<=n;i++) ps[i]+=ps[i-1];
ll fir = 0,lst = 0,mx = 0;
for(ll i = 1;i<=n;i++) mx = max(mx,ps[i]);
for(ll i = 1;i<=n;i++) if(ps[i]==mx) lst = i;
for(ll i = n;i>=1;i--) if(ps[i]==mx) fir = i;
ll tl = 0,tr = mx,mid,ans = 0;
while(tl<=tr){
mid = (tl+tr)/2;
if(moe(mid,fir,mx-mid)||moe(mid,fir,mx-mid+1)||
moe(mid,lst,mx-mid)||moe(mid,lst,mx-mid+1)){ans = mid; tr = mid-1;}
else tl = mid+1;
}
cout<<ans<<endl;
}
int main(){
daj_mi_malo_vremena
int t; t = 1;
while(t--){
tc();
}
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... |