#include <bits/stdc++.h>
using namespace std;
#define int ll
#define f first
#define s second
#define pb push_back
#define ar array
#define all(x) x.begin(), x.end()
#define siz(x) (int)x.size()
#define FOR(x, y, z) for(int x = (y); x < (z); x++)
#define ROF(x, z, y) for(int x = (y-1); x >= (z); x--)
#define F0R(x, z) FOR(x, 0, z)
#define R0F(x, z) ROF(x, 0, z)
#define trav(x, y) for(auto&x:y)
using ll = long long;
using vi = vector<int>;
using vl = vector<long long>;
using pii = pair<int, int>;
using vpii = vector<pair<int, int>>;
template<class T> inline bool ckmin(T&a, T b) {return b < a ? a = b, 1 : 0;}
template<class T> inline bool ckmax(T&a, T b) {return b > a ? a = b, 1 : 0;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const char nl = '\n';
const int mxN = 2e5 + 10;
const int MOD = 1e9 + 7;
const long long infLL = 1e18;
int n, m, mx; ll p[mxN], b[mxN];
vpii v[mxN];
int l, r;
bool chk(int t, ll m, ll n){
ll x = 0;
//cout << t << ' ' << m << ' ' << n << nl;
FOR(i, 1, n+1) b[i] = 0;
priority_queue<pii> pq;
FOR(i, 1, t+1){
ll need = (n+p[i]-m+1)/2-x;
if(i==t) need = n-x;
trav(x, v[i]){
if(x.f >= t) pq.push(x);
}
if(need>0) x += need;
while(need>0){
if(pq.empty()) return 0;
auto T = pq.top(); pq.pop();
if(T.s>need) {
b[T.f] += need;
T.s -= need, pq.push(T);
break;
}
else{
b[T.f] += T.s;
need -= T.s;
}
}
}
FOR(i, t+1, ::n+1){
trav(x, v[i]){
b[i] += x.s; b[x.f] -= x.s;
}
}
while(!pq.empty()){
b[t] += pq.top().s; b[pq.top().f] -= pq.top().s;
pq.pop();
}
FOR(i, t, ::n+1){
b[i] += b[i-1];
if(b[i]>m) return 0;
}
return 1;
}
int32_t main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
FOR(i, 1, m+1){
int a, b, c; cin >> a >> b >> c;
p[a]+=c, p[b]-=c;
v[a].pb({b, c});
}
FOR(i, 1, n+1) p[i] += p[i-1];
mx = *max_element(p, p+n+1);
FOR(i, 1, n+1){
if(mx==p[i]){
l = i;
break;
}
}
ROF(i, 1, n+1){
if(mx==p[i]){
r = i;
break;
}
}
ll L = 1, R = mx;
while(L<R){
ll m = (L+R)/2;
if(chk(l, m, mx-m)||chk(l, m, mx-m+1)||chk(r, m, mx-m)||chk(r, m, mx-m+1)) R = m;
else L = m+1;
}
cout << L << nl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
5100 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
5100 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
5100 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
5100 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
5100 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |