UVA Intervals
Description
- Has n sections, in the interval [ai, bi] at least mutually different ci take any integer. In seeking to meet the case of n sections, at least to take a positive integer number.
Input
Multiple sets of data.
A first integer and T represents the number of data lines, followed by a blank line.
For each test:
The first row contains an integer n (1 <= n <= 50000) representing the interval number.
The following sections describe the row n.
The first input (i + 1) line contains three integers ai, bi, ci, separated by spaces. Where 0 <= ai <= bi <= 50000,1 <= ci <= bi-ai + 1.
Output
For each test, the output takes at least a number of the total number ci different integers for n interval [ai, bi].
In addition to the last set of blank line data output.
Sample Input
1 5 3 7 3 8 10 3 6 8 1 1 3 1 10 11 1
Sample Output
6
answer:
- Differential constraints.
- When you finish Luo Gu P1250 trees do this problem, you will be shouting: "ah original title!"
- In fact, the essence is the same.
- So the solution to a problem Benpian turn essay
Also, why is such a simple question ... purple ...
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define N 200005
using namespace std;
struct E {int next, to, dis;} e[N];
int T, m, num, n;
int h[N], dis[N];
bool vis[N];
int read()
{
int x = 0; char c = getchar();
while(c < '0' || c > '9') c = getchar();
while(c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = getchar();}
return x;
}
void add(int u, int v, int w)
{
e[++num].next = h[u];
e[num].to = v;
e[num].dis = w;
h[u] = num;
}
void spfa()
{
queue<int> que;
memset(dis, -0x3f, sizeof(dis));
memset(vis, 0, sizeof(vis));
dis[n + 1] = 0, vis[n + 1] = 1, que.push(n + 1);
while(!que.empty())
{
int now = que.front();
que.pop(); vis[now] = 0;
for(int i = h[now]; i != 0; i = e[i].next)
if(dis[now] + e[i].dis > dis[e[i].to])
{
dis[e[i].to] = dis[now] + e[i].dis;
if(!vis[e[i].to])
vis[e[i].to] = 1, que.push(e[i].to);
}
}
}
int main()
{
cin >> T;
for(int dfn = 1; dfn <= T; dfn++)
{
n = num = 0;
memset(h, 0, sizeof(h));
m = read();
for(int i = 1; i <= m; i++)
{
int a = read(), b = read(), c = read();
add(a - 1, b, c), n = max(n, b);
}
for(int i = 1; i <= n; i++) add(i - 1, i, 0), add(i, i - 1, -1);
for(int i = 0; i <= n; i++) add(n + 1, i, 0);
spfa();
printf("%d\n", dis[n]);
if(dfn != T) printf("\n");
}
return 0;
}