Difference Constraints ---- Interval

Given n intervals [ai, biai, bi] and n integers cici.
You need to construct a set of integers Z such that ∀i∈ [1, n] ∀i∈ [1, n], Z has at least cici integers that satisfy ai≤x≤biai≤x≤bi
Find the minimum number of such integer set Z.
Input format The
first line contains the integer n.
Next n lines, each line contains three integers ai, bi, ciai, bi, ci.
Output format
Output an integer to indicate the result.
Data range
1≤n≤500001≤n≤50000,

0≤ai,bi≤500000≤ai,bi≤50000,

1≤ci≤bi−ai + 11≤ci≤bi−ai + 1
Sample input:
5
3 7 3
8 10 3
6 8 1
1 3 1
10 11 1

Sample output:
6

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
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, w[idx] = c, ne[idx] = h[a], 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(){
 scanf("%d", &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;
  scanf("%d%d%d", &a, &b, &c);
  a++, b++;
  add(a - 1, b, c);
 }
  spfa();
  printf("%d\n", dist[50001]);
  return 0;
}
164 original articles published · Like 112 · Visitors 6765

Guess you like

Origin blog.csdn.net/qq_45772483/article/details/105495659