QOJ.ac

QOJ

IDSubmission IDProblemHackerOwnerResultSubmit timeJudge time
#423#236955#7520. Monster GeneratorzhaohaikunkyEEccccccFailed.2023-11-04 18:44:352023-11-04 18:44:36

Details

Extra Test:

Accepted
time: 0ms
memory: 3632kb

input:

2 1
2 3 2 5
5 5 4 5

output:

13

result:

ok single line: '13'

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#236955#7520. Monster GeneratorkyEEccccccAC ✓26ms3976kbC++144.1kb2023-11-04 12:01:332023-11-04 18:37:32

answer

// Author: kyEEcccccc

#include <bits/stdc++.h>

using namespace std;

using LL = long long;
using ULL = unsigned long long;

#define F(i, l, r) for (int i = (l); i <= (r); ++i)
#define FF(i, r, l) for (int i = (r); i >= (l); --i)
#define MAX(a, b) ((a) = max(a, b))
#define MIN(a, b) ((a) = min(a, b))
#define SZ(a) ((int)((a).size()) - 1)

constexpr int N = 105;

int n;
LL m, a[N], aa[N], b[N], bb[N];
int ord[N];
array<LL, 2> pts[N];

signed main(void)
{
	// freopen(".in", "r", stdin);
	// freopen(".out", "w", stdout);
	ios::sync_with_stdio(0), cin.tie(nullptr);

	cin >> n >> m;
	F(i, 1, n) cin >> a[i] >> aa[i] >> b[i] >> bb[i];
	vector<LL> ti{0, m + 1};
	F(i, 1, n)
	{
		if (a[i] <= b[i] && aa[i] > bb[i])
		{
			ti.push_back((b[i] - a[i]) / (aa[i] - bb[i]) + 1);
		}
		if (a[i] > b[i] && aa[i] < bb[i])
		{
			ti.push_back((a[i] - b[i] + bb[i] - aa[i] - 1) / (bb[i] - aa[i]));
		}
		F(j, 1, i - 1)
		{
			if (a[i] >= a[j] && aa[j] > aa[i])
			{
				ti.push_back((a[i] - a[j]) / (aa[j] - aa[i]) + 1);
			}
			if (a[i] < a[j] && aa[j] < aa[i])
			{
				ti.push_back((a[j] - a[i] + aa[i] - aa[j] - 1) / (aa[i] - aa[j]));
			}
			if (b[i] >= b[j] && bb[j] > bb[i])
			{
				ti.push_back((b[i] - b[j]) / (bb[j] - bb[i]) + 1);
			}
			if (b[i] < b[j] && bb[j] < bb[i])
			{
				ti.push_back((b[j] - b[i] + bb[i] - bb[j] - 1) / (bb[i] - bb[j]));
			}
		}
	}
	sort(ti.begin(), ti.end());
	ti.resize(unique(ti.begin(), ti.end()) - ti.begin());
	while (ti.back() > m + 1) ti.pop_back();
	ULL ans = 0;
	F(_, 0, SZ(ti) - 1)
	{
		LL bg = ti[_], ed = ti[_ + 1];
		iota(ord + 1, ord + n + 1, 1);
		sort(ord + 1, ord + n + 1, [bg] (int x, int y)
		{
			if (a[x] + (__int128)aa[x] * bg <= b[x] + (__int128)bb[x] * bg)
			{
				if (a[y] + (__int128)aa[y] * bg <= b[y] + (__int128)bb[y] * bg)
				{
					if (a[x] + (__int128)aa[x] * bg == a[y] + (__int128)aa[y] * bg)
					{
						return x < y;
					}
					return a[x] + (__int128)aa[x] * bg < a[y] + (__int128)aa[y] * bg;
				}
				else
				{
					return true;
				}
			}
			else
			{
				if (a[y] + (__int128)aa[y] * bg <= b[y] + (__int128)bb[y] * bg)
				{
					return false;
				}
				else
				{
					if (b[x] + (__int128)bb[x] * bg == b[y] + (__int128)bb[y] * bg)
					{
						return x > y;
					}
					return b[x] + (__int128)bb[x] * bg > b[y] + (__int128)bb[y] * bg;
				}
			}
		});

		ULL res = 0;
		LL cval = 0, ck = 0;
		F(ii, 1, n)
		{
			int i = ord[ii];
			pts[ii] = {-(ck - aa[i]), cval - a[i]};
			cval = cval - a[i] + b[i];
			ck = ck - aa[i] + bb[i];
		}
		sort(pts + 1, pts + n + 1);
		int jj = 0;
		F(i, 1, n)
		{
			if (jj > 0 && pts[i][0] == pts[jj][0]) continue;
			while (jj >= 2 && (__int128)(pts[i][1] - pts[jj][1]) * (pts[jj][0] - pts[jj - 1][0])
				<= (__int128)(pts[jj][1] - pts[jj - 1][1]) * (pts[i][0] - pts[jj][0])) --jj;
			pts[++jj] = pts[i];
		}

		// F(t, bg, ed - 1)
		// {
		// 	__int128 mn = 0;
		// 	F(i, 1, jj)
		// 	{
		// 		MIN(mn, pts[i][1] - pts[i][0] * t);
		// 	}
		// 	res += (-mn) & (((__int128)1 << 64) - 1);
		// }

		vector<LL> tt{bg, ed};
		F(i, 2, jj)
		{
			LL cur = (pts[i][1] - pts[i - 1][1] + pts[i][0] - pts[i - 1][0] - 1)
				/ (pts[i][0] - pts[i - 1][0]);
			if (cur >= bg && cur < ed) tt.push_back(cur);
			if (cur + 1 >= bg && cur + 1 < ed) tt.push_back(cur + 1);
			if (cur - 1 >= bg && cur - 1 < ed) tt.push_back(cur - 1);
		}
		sort(tt.begin(), tt.end());
		tt.resize(unique(tt.begin(), tt.end()) - tt.begin());
		int kk = 1;
		F(i, 0, SZ(tt) - 1)
		{
			LL bbg = tt[i], eed = tt[i + 1];
			while (kk < jj && pts[kk][1] - (__int128)pts[kk][0] * bbg
				> pts[kk + 1][1] - (__int128)pts[kk + 1][0] * bbg) ++kk;
			// int kkk = 1;
			// F(i, 2, jj)
			// {
			// 	if (pts[kkk][1] - (__int128)pts[kkk][0] * bbg
			// 		> pts[i][1] - (__int128)pts[i][0] * bbg) kkk = i;
			// }
			res -= (ULL)(((__int128)(eed - bbg) * (eed + bbg - 1) / 2) % ((__int128)1 << 64))
				* (ULL)(-pts[kk][0]);
			res -= (ULL)pts[kk][1] * (eed - bbg);
		}
		ans += res;
	}
	cout << ans << endl;
	
	return 0;
}