hrbust 1041 哈理工oj 1041 Chocolate Auction【线段树+区间更新】

来源:互联网 发布:linux 得到当前时间 编辑:程序博客网 时间:2024/05/16 05:39

Chocolate AuctionTime Limit: 10000 MSMemory Limit: 65536 KTotal Submit: 228(58 users)Total Accepted: 79(51 users)Rating: Special Judge: NoDescription

In some cultural traditions, giving chocolate as a present on Valentine’s Day is a very traditional symbol of love. LDA does some research on chocolate manufacturing. And luckily, he makes some good quality chocolate bars. In addition, these chocolate bars are very long. They are composed of many identical small pieces. These pieces form into a straight line. For example, if a chocolate bar has 5 pieces, we can say its length is 5 units. From the first one to the last one, they are numbered as 1, 2, 3, 4 and 5, respectively.

  

Then he holds an auction for his chocolate. In order to make this auction more interesting and lucrative, he invented a special way. Here is how he does: the chocolate bar is laid on the table. He cuts it M times in total. For the ith time, he gives an interval [Ai, Bi], if any piece between Ai and Bi (Ai, Bi included) hasn't been taken away yet, he will cut these pieces for sell, the others will remain on the table.

 

For example, there is a chocolate bar of length 6. Given the cutting interval [1, 2], the piece 1 and piece 2 will be cut off in the first time. For the 2nd time, the cutting interval is [4, 4], so the piece 4 will be cut off. The final cutting interval is [1, 3] which contains piece 1, 2, and 3. Since piece 1 and piece 2 have been already cut off, the 3rd piece is the last should be cut off.

 

Here comes the problem. You need to calculate how many pieces have been cut off and sold in each time. Since the chocolate bar can be very long and each chocolate bar will be cut in M times, any brute-force algorithm will fail to solve this problem.

Input

The first line is an integer T which is the number of the chocolate bars. Each chocolate is a test case, in which the first line includes two integer N, M (0 < N ≤ 1,000,000, 0 < M ≤ 100, 000). N is the length of the chocolate bar and M is the number of the times LDA will cut. Notice that after M rounds, the bar may still have some pieces left. And for each round, it is possible that no piece will be cut off. Then comes M lines. Each line contains two integers Ai, B(0 < Ai ≤ Bi ≤ N), which are the interval endpoints of the ith cut.

Output

For each test case, output M lines, in which you are asked to represent the number of pieces cut off in each round.

Sample Input

1

6 4

1 2

4 4

1 3

1 4

Sample Output

2

1

1

0


题目大意:有N块巧克力连在一起,有m次操作,每一次操作都切下来区间内所有的巧克力,询问每一次切下来多少块

例如样例:

1 2 切下来两块 。

4 4 切下来一块。

1 3 因为1 2切下来了两块,这个区间就剩下了一块,所以输出1.

1 4 区间内没有巧克力了,输出0

思路:线段树+区间更新。

后台数据绝壁有点水,要不然我的数组开到了6000000是应该过不了的。。。。。数组开大了一定会MLE。

0.0不管了,既然AC了就先放出来一发。

AC代码:

#include<stdio.h>#include<string.h>using namespace std;#define lson l,m,rt*2#define rson m+1,r,rt*2+1#define ll long long intll tree[8000000];ll flag[6000000];void pushdown(int l,int r,int rt){    if(flag[rt]!=-1)    {        int m=(l+r)/2;        flag[rt*2]=flag[rt];        flag[rt*2+1]=flag[rt];        tree[rt*2]=(m-l+1)*flag[rt];        tree[rt*2+1]=(r-(m+1)+1)*flag[rt];        flag[rt]=-1;    }}void pushup(int rt){    tree[rt]=tree[rt<<1]+tree[rt<<1|1];}void build( int l ,int r , int rt ){if( l == r ){tree[rt]=1;flag[rt]=-1;return ;}else{int m = (l+r)>>1 ;build(lson) ;build(rson) ;pushup(rt) ;return ;}}ll Query(int L,int R,int l,int r,int rt){    if(L<=l&&r<=R)    {        return tree[rt];    }    else    {        pushdown(l,r,rt);        int m=(l+r)>>1;        ll ans=0;        if(L<=m)        {            ans+=Query(L,R,lson);        }        if(m<R)        {            ans+=Query(L,R,rson);        }        return ans;    }}void update(int L,int R,int c,int l,int r,int rt){    if(L<=l&&r<=R)//覆盖的是区间~    {        tree[rt]=c*((r-l)+1);//覆盖当前点的值        flag[rt]=c;//同时懒惰标记~!        return ;    }    pushdown(l,r,rt);    int m=(l+r)/2;    if(L<=m)    {        update(L,R,c,lson);    }    if(m<R)    {        update(L,R,c,rson);    }    pushup(rt);}int main(){    int t;    scanf("%d",&t);    while(t--)    {        memset(flag,-1,sizeof(flag));        int n,m;        scanf("%d%d",&n,&m);        build(1,n,1);        while(m--)        {            int x,y;            scanf("%d%d",&x,&y);            printf("%lld\n",Query(x,y,1,n,1));            update(x,y,0,1,n,1);        }    }}







0 0
原创粉丝点击