# [JOISC2017]门票安排

loj

## 题解

$a_k-a_t\geq 1,b_t-b_k\geq -1 \Rightarrow a_k-a_t+b_t-b_k\geq 0\Leftrightarrow a_k-b_k\geq a_t-b_t$

## 代码

#include <bits/stdc++.h>
using namespace std;
int gi() {
int res = 0, w = 1;
char ch = getchar();
while (ch != '-' && !isdigit(ch)) ch = getchar();
if (ch == '-') w = -1, ch = getchar();
while (isdigit(ch)) res = res * 10 + ch - '0', ch = getchar();
return res * w;
}
typedef long long LL;
const int MAX_N = 2e5 + 5;
int N, M;
int A[MAX_N], B[MAX_N];
LL C[MAX_N], c[MAX_N], tag[MAX_N];
#define fi first
#define se second
vector<pair<int, LL> > pot[MAX_N];
priority_queue<pair<int, LL> > Q;
bool check(LL mid, int t, LL cnt) {
if (mid < cnt) return 0;
for (int i = 1; i <= N; i++) pot[i].clear();
while (!Q.empty()) Q.pop();
for (int i = 1; i <= N; i++) tag[i] = 0;
for (int i = 1; i <= M; i++)
if (A[i] <= t && t < B[i]) pot[A[i]].push_back(make_pair(B[i], C[i]));
LL now = 0;
for (int i = 1; i <= t; i++) {
for (auto j : pot[i]) Q.push(j);
while (c[i] - now + cnt > mid) {
if (Q.empty()) return 0;
auto p = Q.top(); Q.pop();
LL need = min(p.se, (c[i] - now + cnt - mid + 1) >> 1);
now += need, cnt -= need, tag[p.fi] += need << 1;
if (p.se != need) Q.push({p.fi, p.se - need});
}
}
tag[t + 1] -= now;
for (int i = t + 1; i <= N; i++) {
tag[i] += tag[i - 1];
if (tag[i] + c[i] > mid) return 0;
}
return 1;
}

int main () {
#ifndef ONLINE_JUDGE
freopen("cpp.in", "r", stdin);
#endif
N = gi(), M = gi();
for (int i = 1; i <= M; i++) {
A[i] = gi(), B[i] = gi(), C[i] = gi();
if (A[i] > B[i]) swap(A[i], B[i]);
c[A[i]] += C[i], c[B[i]] -= C[i];
}
for (int i = 1; i <= N; i++) c[i] += c[i - 1];
int mxl = 0, mxr = 0;
for (int i = 1; i <= N; i++) if (c[i] > c[mxl]) mxl = i;
for (int i = N; i >= 1; i--) if (c[i] > c[mxr]) mxr = i;
LL l = 0, r = c[mxl], ans = r;
while (l <= r) {
LL mid = (l + r) >> 1;
if (check(mid, mxl, c[mxl] - mid) || check(mid, mxl, c[mxl] - mid + 1) ||
check(mid, mxr, c[mxr] - mid) || check(mid, mxr, c[mxr] - mid + 1)) ans = mid, r = mid - 1;
else l = mid + 1;
}
printf("%lld\n", ans);
return 0;
}

posted @ 2020-09-24 08:49  heyujun  阅读(350)  评论(1编辑  收藏  举报