Minimizing Maximizer
Time Limit: 5000MS |
|
Memory Limit: 30000K |
Total Submissions: 4355 |
|
Accepted: 1792 |
Description The company Chris Ltd. is preparing a new sorting hardware called Maximizer. Maximizer have n inputs numbered from 1 to N. Each input represents one integer. Maximizer have one output which represents the maximum value present on Maximizer ' s inputs.
Maximizer is implemented as a pipeline of sorters Sorter (I1, J1), ..., Sorter (IK, JK). Each sorter have n inputs and n outputs. Sorter (i, j) sorts values on inputs I, i+1,..., J in non-decreasing Order and lets the other inputs pass through Unchange D. The n-th output of the last sorter is the output of the Maximizer.
An intern (a former ACM contestant) observed that some sorters could is excluded from the pipeline and Maximizer would STI ll produce the correct result. What's the length of the shortest subsequence of the given sequence of sorters in the pipeline still producing correct re Sults for all possible combinations of input values?
Task
Write a program:
Reads a description of a Maximizer, i.e. the initial sequence of sorters in the pipeline,
Computes the length of the shortest subsequence of the initial sequence of sorters still producing correct results for all Possible input data,
Writes the result.
Input the first line of the input contains integers n and m (2 <= N <= 50000, 1 <= m <= 500000) separated by a single space. Integer n is the number of inputs and an integer m is the number of sorters in the pipeline. The initial sequence of Sorters is described in the next m lines. The k-th of these lines contains the parameters of the k-th sorter:two integers ik and jk (1 <= ik < JK <= N) se Parated by a single space.
Output the output consists of one line containing a integer equal to the length of the shortest subsequence of the Nitial sequence of sorters still producing correct results for all possible data.
Sample Input
6 (
1)-
30 40
Sample Output
4
Hint Huge input data, scanf is recommended.
#include <cstdio> #include <iostream> #include <cstring> using namespace std;
const int MAXN = 50000 + 1000;
const int inf = 0X3F3F3F3F;
int SEG[MAXN * 4];//segment Tree maintenance interval min int n, m;
Segment tree query interval min int query (int p, int l, int r, int x, int y) {if (l>= x && r <= y) return seg[p];
int mid = (L + r) >> 1;
int res = INF;
if (x <= mid) res = Query (p<<1, L, Mid, X, y);
if (Y > Mid) res = min (res, Query (p<<1|1, mid+1, R, X, y));
return res;
}//Segment tree single point update void update (int p, int l, int r, int x, int y) {if (L = = r) {Seg[p] = min (y, seg[p]);
Return
} int mid = (L + r) >> 1;
if (x <= mid) Update (P<<1, L, Mid, X, y);
else Update (p<<1|1, mid+1, R, X, y);
Seg[p] = min (seg[p<<1], seg[p<<1|1]);
} int main () {///segment tree is initialized to Infinity memset (SEG, INF, sizeof (SEG));
scanf ("%d%d", &n, &m);
DP[1] initialized to 0 Update (1, 1, N, 1, 0);
while (m--) {int S, t;
scanf ("%d%d", &s, &t); StateTransfer int temp = Query (1, 1, n, S, t) + 1;
Update (1, 1, N, T, temp);
} printf ("%d\n", Query (1, 1, n, N, N));
return 0;
}