This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
using ll = long long;
#define ff first
#define ss second
int N, M, L, R;
ll A[200005], X[200005], mx, S[200005];
vector<pii> V[200005];
bool g(int m, int n, int p) {
fill(S, S + N + 1, 0);
priority_queue<pii> pq;
ll s = 0;
for (int i = 1; i <= p; ++i) {
for (auto j : V[i]) if (j.ff > p) pq.push(j);
while (s < X[i]) {
if (pq.empty()) return 0;
auto t = pq.top(); pq.pop();
if (s + t.ss <= X[i]) {
S[t.ff] += t.ss;
s += t.ss;
}
else {
S[t.ff] += X[i] - s;
t.ss -= X[i] - s;
s = X[i];
pq.push(t);
}
}
}
for (int i = p + 1; i <= N; ++i) {
s -= S[i];
if (A[i] - s + n - s > m) return 0;
}
return 1;
}
bool f(int m) {
for (int n = mx - m; n <= mx - m + 1; ++n) {
for (int i = 1; i <= R; ++i) X[i] = (A[i] + n - m + 1) / 2;
if (g(m, n, L)) return 1;
}
return 0;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> N >> M;
for (int i = 1; i <= M; ++i) {
int a, b, c; cin >> a >> b >> c;
if (a > b) swap(a, b);
A[a] += c;
A[b] -= c;
V[a].emplace_back(b, c);
}
for (int i = 1; i <= N; ++i) A[i] += A[i - 1];
mx = *max_element(A + 1, A + N + 1);
for (int i = 1; i <= N; ++i) if (mx == A[i]) {
L = i;
break;
}
for (int i = N; i >= 1; --i) if (mx == A[i]) {
R = i;
break;
}
int l = 1, r = mx;
while (l < r) {
int m = l + r >> 1;
if (f(m)) r = m;
else l = m + 1;
}
cout << l;
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... |