hrbust 1041 哈理工oj 1041 Chocolate Auction【线段树+区间更新】
来源:互联网 发布:linux 得到当前时间 编辑:程序博客网 时间:2024/05/16 05:39
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.
InputThe 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, Bi (0 < Ai ≤ Bi ≤ N), which are the interval endpoints of the ith cut.
OutputFor each test case, output M lines, in which you are asked to represent the number of pieces cut off in each round.
Sample Input1
6 4
1 2
4 4
1 3
1 4
Sample Output2
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); } }}
- hrbust 1041 哈理工oj 1041 Chocolate Auction【线段树+区间更新】
- Hrbust 1041 Chocolate Auction
- hrbust/哈理工oj 1877 区间【水题】
- COJ1025 Chocolate Auction线段树
- 哈理工OJ 1566 Countless Candies【线段树】【区间更新、点查询】
- hrbust 哈理工oj 1752Page Rank【线段树好题】
- hrbust 哈理工oj 2042 序列问题【线段树+离散化】
- 哈理工OJ 2256 南西群岛海域·冲之岛近海(线段树区间更新+前缀和优化)
- 哈理工OJ 1189 区间最大值 II(线段树【简单】)
- hrbust 哈理工oj 1588 神医【贪心】
- hrbust 哈理工oj 网线【MST+Prim】
- 哈理工oj/hrbust 1790 武林【DP】
- hrbust哈理工oj 1674 充电【贪心】
- hrbust 哈理工oj 1330 邂逅【模拟】
- Hrbust oj/哈理工 oj 1216数的划分
- hrbust oj 1189、线段树初探 简单应用之区间最大值、
- HRBUST 1189 区间最大值 II (线段树)
- hrbust 哈理工oj 2038 联系网络【MST】【最小生成树】
- linux(centos7)+apache+php7 安装教程
- 常用反射操作
- MyEclipse has detected that less than 5% of the 341MB of PS Old Gen(Heap memory) space remains.
- A Knight's Journey(DFS)
- Android自定义标题栏
- hrbust 1041 哈理工oj 1041 Chocolate Auction【线段树+区间更新】
- Qt使用QGraphicsView实现滑动窗体效果
- pycnn add实例
- BDC操作与常见问题
- 对Android自带的软键盘进行一些界面上的设置
- Gulp vs Grunt 前端工程构建工具
- iOS开发出绚丽的界面:Quartz 2D
- Python2.7:测试内部类型及type和isinstance用法区别
- HDU-1025-Constructing Roads In JGShining's Kingdom