差分约束 : 区间
分析
\(前缀和 + 差分约束\)
\(令S_i为1\sim i中被选出的数.\)
约束关系
- \(S_i \ge S_{i-1}\)
- \(S_i-S_{i-1}\le 1\Rightarrow S_{i-1}\ge S_i-1\)
- \(S_a-S_b\ge c\)
代码
#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
inline int lowbit(int x) { return x & (-x); }
#define ll long long
#define pb push_back
#define PII pair<int, int>
#define x first
#define y second
#define inf 0x3f3f3f3f
const int N = 50010, M = 150010;
int n;
int h[N], e[M], w[M], ne[M], idx;
int dist[N];
int q[N];
bool st[N];
void add(int a, int b, int c) {
e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++;
}
void spfa() {
memset(dist, -0x3f, sizeof dist);
dist[0] = 0;
st[0] = true;
int hh = 0, tt = 1;
q[0] = 0;
while (hh != tt) {
int t = q[hh++];
if (hh == N) hh = 0;
st[t] = false;
for (int i = h[t]; ~i; i = ne[i]) {
int j = e[i];
if (dist[j] < dist[t] + w[i]) {
dist[j] = dist[t] + w[i];
if (!st[j]) {
q[tt++] = j;
if (tt == N) tt = 0;
st[j] = true;
}
}
}
}
}
int main() {
IO;
cin >> n;
memset(h, -1, sizeof h);
for (int i = 1; i < N; ++i) {
add(i - 1, i, 0);
add(i, i - 1, -1);
}
for (int i = 0; i < n; ++i) {
int a, b, c;
cin >> a >> b >> c;
a++, b++;
add(a - 1, b, c);
}
spfa();
cout << dist[50001] << '\n';
return 0;
}