pku1769 新写的线段树(点树)模版

Minimizing maximizer
Time Limit: 5000MS Memory Limit: 30000K
Total Submissions: 1004 Accepted: 280

Description
The company Chris Ltd. is preparing a new sorting hardware called Maximizer. Maximizer has n inputs numbered from 1 to n. Each input represents one integer. Maximizer has 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 has 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 unchanged. 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 be excluded from the pipeline and Maximizer would still produce the correct result. What is the length of the shortest subsequence of the given sequence of sorters in the pipeline still producing correct results for all possible combinations of input values?

Task
Write a program that:

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 two integers n and m (2 <= n <= 50000, 1 <= m <= 500000) separated by a single space. Integer n is the number of inputs and 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) separated by a single space.

Output
The output consists of only one line containing an integer equal to the length of the shortest subsequence of the initial sequence of sorters still producing correct results for all possible data.

Sample Input

40 6
20 30
1 10
10 20
20 30
15 25
30 40

 

Sample Output

4

 

Hint
Huge input data, scanf is recommended.

Source
Central Europe 2003

//pku1769
/*
 * trival DP dp[i] = dp[j] + 1 (if there is a segment starting from a->i && a <= j)  o(n^2)
 * 考虑到转移的时候选择的是一段内的最小dp值,运用点树可以解决
 */
#include <string.h>
#include <stdio.h>

const int N = 50010;
const int MAXINT = 1000000000;

int n, l;

struct ST {int i,j,m,l,r,c;} st[2*N];
int up, cnt;

void bd(int d, int x, int y) {
 st[d].i = x, st[d].j = y, st[d].m = (x+y)/2, st[d].c = MAXINT;
 if(x < y) {
  st[d].l = ++up; bd(up, x, st[d].m);
  st[d].r = ++up; bd(up, st[d].m+1, y);
 }
}

void ins(int d, int x, int c) {
 if(c < st[d].c)
  st[d].c = c;
 if(st[d].i != st[d].j) {
  if(x <= st[d].m)
   ins(st[d].l, x, c);
  else
   ins(st[d].r, x, c);
 }
}

int getmin(int d, int x, int y) {
 if(x <= st[d].i && y >= st[d].j)
  return st[d].c;
 int min = MAXINT;
 if(x <= st[d].m) {
  int now = getmin(st[d].l, x, y);
  if(now < min) min = now;
 }
 if(y > st[d].m) {
  int now = getmin(st[d].r, x, y);
  if(now < min) min = now;
 }
 return min;
}

int main() {
 int i, a, b;
 up = 0;
 scanf("%d %d ", &l, &n);
 bd(0, 1, l);
 ins(0, 1, 0);
 int max = 0;
 for(i = 0; i < n; ++i) {
  scanf("%d%d", &a, &b);
  if(a < b) {
   int min = getmin(0, a, b-1);
   ins(0, b, min+1);
  }
 }
 printf("%d\n", getmin(0, l, l));
 return 0;
}

你可能感兴趣的:(pku1769 新写的线段树(点树)模版)