Submission #95869

#TimeUsernameProblemLanguageResultExecution timeMemory
95869diamond_dukeArranging Tickets (JOI17_arranging_tickets)C++11
100 / 100
3123 ms14548 KiB
#include <algorithm>
#include <cstdio>
#include <queue>
using ll = long long;
int lp[200005], rp[200005], w[200005], rem[200005], n, m;
ll lazy[200005], nxt[200005];
std::vector<int> vec[200005];
struct comp { bool operator ()(int x, int y) { return rp[x] < rp[y]; } };
inline bool check(int com, ll cnt, ll lim)
{
	if (cnt > lim)
		return false;
	for (int i = 0; i < n; i++)
	{
		vec[i].clear();
		nxt[i] = 0;
	}
	for (int i = 0; i < m; i++)
	{
		if (lp[i] <= com && com < rp[i])
		{
			vec[lp[i]].push_back(i);
			rem[i] = w[i];
		}
	}
	std::priority_queue<int, std::vector<int>, comp> que;
	for (int i = 0; i <= com; i++)
	{
		for (int x : vec[i])
			que.push(x);
		ll cur = std::max<ll>(0, lazy[i] + cnt - lim) + 1 >> 1;
		while (cur && !que.empty())
		{
			int x = que.top();
			que.pop();
			int sub = std::min<ll>(cur, rem[x]);
			cur -= sub;
			rem[x] -= sub;
			cnt -= sub << 1;
			nxt[lp[x]] -= sub;
			nxt[rp[x]] += sub << 1;
			if (rem[x])
				que.push(x);
		}
		if (cur)
			return false;
	}
	for (int i = 0; i < n; i++)
	{
		if (i)
			nxt[i] += nxt[i - 1];
		if (nxt[i] + lazy[i] > lim)
			return false;
	}
	return true;
}
int main()
{
	// freopen("loj-2393.in", "r", stdin);
	scanf("%d%d", &n, &m);
	for (int i = 0; i < m; i++)
	{
		scanf("%d%d%d", lp + i, rp + i, w + i);
		if (--lp[i] > --rp[i])
			std::swap(lp[i], rp[i]);
		lazy[lp[i]] += w[i];
		lazy[rp[i]] -= w[i];
	}
	int fst = 0, lst = 0;
	for (int i = 1; i < n; i++)
	{
		lazy[i] += lazy[i - 1];
		if (lazy[i] > lazy[fst])
			fst = i;
		if (lazy[i] == lazy[fst])
			lst = i;
	}
	ll l = 0, r = lazy[fst], res = -1;
	while (l <= r)
	{
		ll x = l + r >> 1;
		if (check(fst, lazy[fst] - x, x) || check(fst, lazy[fst] - x + 1, x) ||
				check(lst, lazy[lst] - x, x) || check(lst, lazy[lst] - x + 1, x))
		{
			res = x;
			r = x - 1;
		}
		else
			l = x + 1;
	}
	printf("%lld\n", res);
	return 0;
}

Compilation message (stderr)

arranging_tickets.cpp: In function 'int main()':
arranging_tickets.cpp:60:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &n, &m);
  ~~~~~^~~~~~~~~~~~~~~~
arranging_tickets.cpp:63:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%d", lp + i, rp + i, w + i);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...