描述 Description
某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米。我们可以把马路看成一个数轴,马路的一端在数轴0的位置,另一端在L的位置;数轴上的每个整数点,即0,1,2,……,L,都种有一棵树。

由于马路上有一些区域要用来建地铁。这些区域用它们在数轴上的起始点和终止点表示。已知任一区域的起始点和终止点的坐标都是整数,区域之间可能有重合的部分。现在要把这些区域中的树(包括区域端点处的两棵树)移走。你的任务是计算将这些树都移走后,马路上还有多少棵树。
输入格式 Input Format
输入的第一行有两个整数L(1 <= L <= 1亿)和 M(1 <= M <= 20000),L代表马路的长度,M代表区域的数目,L和M之间用一个空格隔开。接下来的M行每行包含两个不同的整数,用一个空格隔开,表示一个区域的起始点和终止点的坐标。
输出格式 Output Format
输出包括一行,这一行只包含一个整数,表示马路上剩余的树的数目。
样例输入 Sample Input
500 3 150 300 100 200 470 471
样例输出 Sample Output
298
时间限制 Time Limitation
各个测试点1s

//By LYLtim

type node=record x,y:longword; end;
var l,sum:longword;
    m:word;
    a:array[1..1000000]of node;

procedure qsort(l,r:longword);
var pl,pr,m:longword;
    t:node;
begin
    pl:=l;pr:=r;m:=a[(l+r)shr 1].x;
    repeat
        while a[pl].x<m do inc(pl);
        while a[pr].x>m do dec(pr);
        if pl<=pr then
            begin
                t:=a[pl];a[pl]:=a[pr];a[pr]:=t;
                inc(pl);dec(pr);
            end;
    until pl>pr;
    if pl<r then qsort(pl,r);
    if pr>l then qsort(l,pr);
end;{qsort}

procedure init;
var i:word;
begin
    assign(input,'tree.in');reset(input);
    readln(l,m);
    for i:=1 to m do with a[i] do readln(x,y);
    close(input);
end;{init}

procedure work;
var i,last:longword;
begin
    qsort(1,m);
    sum:=a[1].y-a[1].x+1;
    last:=a[1].y;
    for i:=2 to m do
        begin
            if a[i].x<=last then
                begin if a[i].y>last then inc(sum,a[i].y-last); end
            else inc(sum,a[i].y-a[i].x+1);
            if a[i].y>last then last:=a[i].y;
        end;
end;{work}

procedure print;
begin
    assign(output,'tree.out');rewrite(output);
    writeln(l+1-sum);
    close(output);
end;{print}

begin{main}
    init;
    work;
    print;
end.

posted on 2011-08-05 10:52  shallyzhang  阅读(172)  评论(0)    收藏  举报