线段树优化

开通博客后就写过一篇,现在翻翻看也觉得写的幼稚。打算以后每周日写一篇博客,记录自己一周的收获。希望这次能够坚持下来。

POJ1769

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  
只需要保证当最大值在第一个是,也能移到最后。dp里的区间最小值,用线段树优化。
代码:
#include<iostream>
#include<stdlib.h>
#include<math.h>
#define INF 0x3f3f3f
using namespace std;
const int MAX_N=500050;
int n,m;
int s[MAX_N],t[MAX_N];
int dp[MAX_N+1];
int segtree[MAX_N*4+10];
void build(int node,int left,int right)
{
    if(left==right)segtree[node]=dp[left];
    else{
        build(node*2,left,(left+right)/2);
        build(node*2+1,(left+right)/2+1,right);
        if(segtree[node*2]<=segtree[node*2+1])segtree[node]=segtree[node*2];
        else segtree[node]=segtree[node*2+1];
    }
    return;
}
void updata(int node,int left,int right,int Ind)
{
    if(left==right){
       segtree[node]=dp[Ind];
       return;
    }
    int m=(left+right)>>1;
    if(Ind<=m)updata(node*2,left,m,Ind);
    else updata(node*2+1,m+1,right,Ind);
    segtree[node]=min(segtree[node*2],segtree[node*2+1]);
    return;
}
int query(int node,int left,int right,int ql,int qr)
{
    int p1,p2;
    if(ql>right||qr<left)return -1;
    if(ql<=left&&qr>=right)return segtree[node];
    p1=query(node*2,left,(left+right)/2,ql,qr);
    p2=query(node*2+1,(left+right)/2+1,right,ql,qr);
    if(p1==-1)return p2;
    if(p2==-1)return p1;
    if(p1<=p2)return p1;
    return p2;
}
int main()
{
    cin>>n;
    cin>>m;
    int e=1;
    while(e<n)e*=2;
    for(int i=0;i<m;i++){
        cin>>s[i];
        cin>>t[i];
    }
    fill(dp,dp+n+1,INF);
    build(1,1,e);
    dp[1]=0;
    updata(1,1,e,1);
    for(int i=0;i<m;i++){
        dp[t[i]]=min(dp[t[i]],query(1,1,e,s[i],t[i])+1);
        updata(1,1,e,t[i]);
    }
    cout<<dp[n]<<endl;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值