2024.10.18 CW 模拟赛

题目链接: https://files.cnblogs.com/files/blogs/833333/CW1018.zip?t=1729247210&download=true

T2 区间

原区间范围很大, 考虑离散化.

显然, 产生贡献的情况在离散化后, 分成一块一块的.

对于所有区间的左右端点离散化以后, 一个区间一个区间中的贡献都是相同的, 其中端点处的贡献须要单独考虑, 端点处的贡献要去掉包含的区间.

这样就可以用类似差分的思想, 标记左右端点, 从前往后扫处理每一个区间的贡献, 记录下来以后排序, 从高到低一次考虑操作.

#include<bits/stdc++.h>
#define int long long
using namespace std;
char buf[1 << 20],*p1,*p2;
#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf,1,1<<20,stdin), p1 == p2) ? 0 : *p1++)
template<typename T>inline void read(T &x) {
	bool f = 1;
	x = 0;
	char ch = getchar();
	while (ch < '0' || ch > '9') {
		if (ch == '-') f = !f;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9') {
		x = (x << 1) + (x << 3) + (ch ^ 48);
		ch = getchar();
	}
	x = (f ? x : -x);
	return;
}
template<typename T>inline void write(T x) {
	if (x < 0) putchar('-'), x = -x;
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
	return;
}
int T, n, m;
unordered_map<int, int>mp;
struct node {
	int l, r, x;
	inline friend bool operator <(node a, node b) {
		return a.x > b.x;
	}
} a[200010];
int tot;
int ans;
pair<int, int> t[200010];
int cnt;
signed main() {
	read(T);
	for (int cs = 1; cs <= T; cs++) {
		ans = 0;
		mp.clear();
		tot = 0;
		cnt = 0;
		read(n);
		read(m);
		for (int i = 1; i <= n; i++) {
			int l, r;
			read(l);
			read(r);
			mp[l + 1]++;
			mp[r]--;
		}
		int sum = 0, lst = 0;
		for (auto it = mp.begin(); it != mp.end(); it++) {
			t[++cnt] = {it->first, it->second};
		}
		sort(t + 1, t + cnt + 1);
		for (int i = 1; i <= cnt; i++) {
			pair<int, int>* it = &t[i];
			if (sum != 0) {
				a[++tot] = {lst, it->first - 1, sum};
			}
			sum += it->second;
			lst = it->first;
		}
		stable_sort(a + 1, a + tot + 1);
		for (int i = 1; i <= tot; i++) {
			int len = a[i].r - a[i].l + 1;
			if (len > m) {
				ans += 1ll * m*a[i].x;
				break;
			} else {
				ans += 1ll * len*a[i].x;
				m -= len;
			}
		}
		cout << "case #" << cs << ": " << ans + n << endl;
	}
	return 0;
}

posted @   Steven1013  阅读(5)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示