이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
const int MOD = 1000000007;
const ll INF = 1e18;
const int MX = 200001;
int N,M, fst, lst;
ll pre[MX], PRE[MX];
vpi tri[MX];
bool test(ll mx, int pos, ll numFlips) {
FOR(i,1,N+1) PRE[i] = 0;
ll already = 0;
priority_queue<array<int,3>> p; // end, begin, num
FOR(i,1,pos+1) {
for (auto a: tri[i]) if (a.f > pos) p.push({a.f,i,a.s});
ll need = pre[i]+numFlips-mx;
if (i != pos) need = (need+1)/2;
else need = numFlips;
need = max(need-already,0LL); already += need;
if (already > numFlips) return 0;
while (need) {
if (!sz(p)) return 0;
auto a = p.top(); ll num = min(need,(ll)a[2]);
a[2] -= num; need -= num; PRE[1] ++; PRE[a[1]] -= 2; PRE[a[0]] += 2;
p.pop(); if (a[2]) p.push(a);
}
}
FOR(i,1,N+1) PRE[i] += PRE[i-1];
FOR(i,1,N+1) if (PRE[i]+pre[i] > mx) return 0;
return 1;
}
bool ok(ll mid) {
return test(mid,fst,pre[fst]-mid) || test(mid,fst,pre[fst]-mid+1)
|| test(mid,lst,pre[lst]-mid) || test(mid,lst,pre[lst]-mid+1);
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> N >> M;
F0R(i,M) {
int A,B,C; cin >> A >> B >> C;
if (A > B) swap(A,B);
pre[A] += C, pre[B] -= C;
tri[A].pb({B,C});
}
FOR(i,1,N+1) {
pre[i] += pre[i-1];
if (pre[i] > pre[fst]) fst = i;
if (pre[i] >= pre[lst]) lst = i;
}
ll lo = 0, hi = pre[fst];
while (lo < hi) {
ll mid = (lo+hi)/2;
if (ok(mid)) hi = mid;
else lo = mid+1;
}
cout << lo;
}
/* Look for:
* the exact constraints (multiple sets are too slow for n=10^6 :( )
* special cases (n=1?)
* overflow (ll vs int?)
* array bounds
* if you have no idea just guess the appropriate well-known algo instead of doing nothing :/
*/
# | 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... |