20pts求调
查看原帖
20pts求调
481621
Zhang_Wenjie楼主2024/2/28 13:28
#include <bits/stdc++.h>
#define re register int 
#define min(x, y) (x < y ? x : y)
#define max(x, y) (x > y ? x : y)
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
const LL Inf = LLONG_MAX;
LL n, m, w[N], v[N], l[N], r[N];
LL sn[N], sv[N], min_w = Inf, max_w = -1, L, R, res;
LL s, ans = Inf;

inline bool check(LL mid)
{
	res = 0;
	memset(sn, 0, sizeof(sn));
	memset(sv, 0, sizeof(sv));
	
	for (re i = 1; i <= n; i ++)
	{
		if (w[i] >= mid) 
		{
			sn[i] = sn[i - 1] + 1;
			sv[i] = sv[i - 1] + v[i];
		}
		else sn[i] = sn[i - 1], sv[i] = sv[i - 1];
	}
	
	for (re i = 1; i <= m; i ++)
	{
		res += (sn[r[i]] - sn[l[i] - 1]) * (sv[r[i]] - sv[l[i] - 1]);
	}
	res = llabs(res - s);
	
	if (res < s) return true;
	else if (res == s) { cout << 0; exit(0); }
	else return false;
}

inline void Search()
{
	L = min_w - 1;
	R = max_w + 2;
	
	while (L <= R)
	{
		LL mid = (L + R) >> 1;
		if (check(mid)) R = mid - 1;
		else L = mid + 1;
		ans = min(ans, res);
	}
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	
	cin >> n >> m >> s;
	for (re i = 1; i <= n; i ++) 
	{
		cin >> w[i] >> v[i];
		max_w = max(max_w, w[i]);
		min_w = min(min_w, w[i]);
	}
	for (re i = 1; i <= m; i ++) cin >> l[i] >> r[i];
	Search();
	cout << ans;
	
	return 0;
}
2024/2/28 13:28
加载中...