思路:
贪心 + 线段树。
实现:
1 #include <iostream> 2 #include <algorithm> 3 using namespace std; 4 const int MAXN = 50005; 5 struct node 6 { 7 int l, r, c; 8 }; 9 node a[MAXN]; 10 int tree[MAXN * 4], n; 11 bool vis[MAXN]; 12 bool cmp(const node & a, const node & b) 13 { 14 return a.r < b.r; 15 } 16 void update(int num, int l, int r, int x, int dx) 17 { 18 if (l == r) { tree[num] += dx; return; } 19 int m = l + r >> 1; 20 if (x <= m) update(num << 1, l, m, x, dx); 21 else update(num << 1 | 1, m + 1, r, x, dx); 22 tree[num] = tree[num << 1] + tree[num << 1 | 1]; 23 } 24 int query(int num, int l, int r, int x, int y) 25 { 26 if (x <= l && y >= r) return tree[num]; 27 int m = l + r >> 1; 28 int ans = 0; 29 if (x <= m) ans += query(num << 1, l, m, x, y); 30 if (y >= m + 1) ans += query(num << 1 | 1, m + 1, r, x, y); 31 return ans; 32 } 33 int main() 34 { 35 ios::sync_with_stdio(false); 36 cin >> n; 37 for (int i = 0; i < n; i++) cin >> a[i].l >> a[i].r >> a[i].c; 38 sort(a, a + n, cmp); 39 int tot = 0; 40 for (int i = 0; i < n; i++) 41 { 42 int cnt = query(1, 0, 50000, a[i].l, a[i].r); 43 if (cnt < a[i].c) 44 { 45 int x = a[i].r; 46 while (cnt < a[i].c) 47 { 48 if (vis[x]) { x--; continue; } 49 update(1, 0, 50000, x, 1); 50 vis[x--] = true; 51 cnt++; 52 tot++; 53 } 54 } 55 } 56 cout << tot << endl; 57 return 0; 58 }