2024.10.18 CW 模拟赛

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

T2 区间

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

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

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

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

复制代码
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
#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  阅读(7)  评论(0编辑  收藏  举报
(评论功能已被禁用)
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
展开