给定长度为 n 的 01 序列 ar,m 组限制,为 [l, r, x],问能否满足所有的
i, 使得xi <= sum[r] - sum[l - 1]
优化:
- 如果有超级源点,直接把所有点push进去。
- deque的slf做法,一般出题人只卡spfa或slf
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N = 2e5 + 10, M = 2e6 + 10;
int h[N], ne[M], e[M], idx, w[M];
int n, m;
void add(int a, int b, int c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}
int dis[N];
bool st[N];
void spfa()
{
deque<int> q;
for (int i = 0; i <= n; i ++ ) {
st[i] = 1;
q.push_back(i);
}
while(q.size())
{
int u = q.front();q.pop_front();
st[u] = 0;
for (int i = h[u]; ~ i; i = ne[i])
{
int j = e[i];
if(dis[j] < dis[u] + w[i]) {
dis[j] = dis[u] + w[i];
if(!st[j]) {
if(!q.empty() && dis[j] > dis[q.front()]) q.push_back(j);
else q.push_front(j);
st[j] = 1;
}
}
}
}
}
int main(){
//std::ios::sync_with_stdio(false);
//std::cin.tie(nullptr);
memset(h, -1, sizeof h);
cin >> n;
for (int i = 1; i <= n; i ++ ) {
add(i - 1, i, 0);
add(i, i - 1, -1);
}
cin >> m;
while(m -- )
{
int a, b, c;
cin >> a >> b >> c;
add(a - 1, b, c);
}
spfa();
for (int i = 1; i <= n; i ++ ) {
if(dis[i] - dis[i - 1] == 1) cout << "1";
else cout << "0";
cout << ' ';
}
cout << '\n';
return 0;
}