이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld double
#define sz(x) (int)x.size()
#define all(x) x.begin(),x.end()
#define pb emplace_back
#define X first
#define Y second
const int N = 2e5 + 5;
typedef pair<int,int> ii;
vector<ii> g[N];
ll s[N], z[N];
ll h[N], t[N];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n; cin >> n;
int q; cin >> q;
for(int i = 0 ; i < q ; ++i) {
int l; cin >> l;
int r; cin >> r;
int x; cin >> x;
if (l > r) swap(l,r);
g[l].pb(r,x);
s[l] += x;
s[r] -= x;
}
ll mx = -1;
int ps = 0;
for(int i = 1 ; i <= n ; ++i) {
s[i] += s[i - 1];
if (mx < s[i])
mx = s[i],
ps = i;
}
for(int i = 1 ; i <= ps ; ++i) z[i] = max(z[i - 1],s[i]);
for(int i = n ; i >= ps ; --i) z[i] = max(z[i + 1],s[i]);
auto check = [&](ll x) {
ll sum = 0;
for(int i = 0 ; i < n ; ++i)
h[i] = min(h[i],x),
t[i] = 0;
h[ps] = 0;
priority_queue<ii> pq;
for(int i = 1 ; i <= ps ; ++i) {
for(ii e : g[i])
if (e.X > ps) pq.push(e);
sum += h[i - 1] - h[i];
while (1) {
if (pq.empty()) return 0;
int p = pq.top().X;
int c = pq.top().Y;
pq.pop();
if (c >= sum) {
c -= sum;
t[p] -= sum;
pq.push(ii(p,c));
sum = 0;
break;
}
else {
sum -= c;
t[p] -= c;
}
}
}
for(int i = 1 ; i < n ; ++i) {
t[i] += t[i - 1];
if (h[i] + t[i] < 0)
return 0;
}
return 1;
};
auto chk = [&](ll M) {
for(int i = 0 ; i < 2 ; ++i) {
ll x = z[ps] - M + i;
for(int j = 0 ; j < n ; ++j)
h[j] = (x + M - z[j]) / 2;
if (h[0] >= x && check(x))
return 1;
}
return 0;
};
ll L = 0;
ll R = mx;
while(L < R) {
ll M = (L + R) / 2;
if (chk(M))
R = M;
else
L = M + 1;
}
cout << L << endl;
}
# | 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... |