题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4614点击打开链接
Vases and Flowers
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 3712 Accepted Submission(s): 1499
Problem Description
Alice is so popular that she can receive many flowers everyday. She has N vases numbered from 0 to N-1. When she receive some flowers, she will try to put them in the vases, one flower in one vase. She randomly choose the vase A and try to put a flower in the vase. If the there is no flower in the vase, she will put a flower in it, otherwise she skip this vase. And then she will try put in the vase A+1, A+2, ..., N-1, until there is no flower left or she has tried the vase N-1. The left flowers will be discarded. Of course, sometimes she will clean the vases. Because there are too many vases, she randomly choose to clean the vases numbered from A to B(A <= B). The flowers in the cleaned vases will be discarded.
Input
The first line contains an integer T, indicating the number of test cases.
For each test case, the first line contains two integers N(1 < N < 50001) and M(1 < M < 50001). N is the number of vases, and M is the operations of Alice. Each of the next M lines contains three integers. The first integer of one line is K(1 or 2). If K is 1, then two integers A and F follow. It means Alice receive F flowers and try to put a flower in the vase A first. If K is 2, then two integers A and B follow. It means the owner would like to clean the vases numbered from A to B(A <= B).
For each test case, the first line contains two integers N(1 < N < 50001) and M(1 < M < 50001). N is the number of vases, and M is the operations of Alice. Each of the next M lines contains three integers. The first integer of one line is K(1 or 2). If K is 1, then two integers A and F follow. It means Alice receive F flowers and try to put a flower in the vase A first. If K is 2, then two integers A and B follow. It means the owner would like to clean the vases numbered from A to B(A <= B).
Output
For each operation of which K is 1, output the position of the vase in which Alice put the first flower and last one, separated by a blank. If she can not put any one, then output 'Can not put any one.'. For each operation of which K is 2, output the number of discarded flowers.
Output one blank line after each test case.
Output one blank line after each test case.
Sample Input
2 10 5 1 3 5 2 4 5 1 1 8 2 3 6 1 8 8 10 6 1 2 5 2 3 4 1 0 8 2 2 5 1 4 4 1 2 3
Sample Output
[pre]3 7 2 1 9 4 Can not put any one. 2 6 2 0 9 4 4 5 2 3 [/pre]
Author
SYSU
这道题的回溯思想(Union函数)是启发于另外一道题的 我们可以用一个flag标记 表示整个区间都为满或者都为空
并且与懒惰标记一样 可以向下传递
为了防止查询时搜索深度过高 每次回溯的时候用Union函数来更新flag标记的值 这样就能起到区间更新区间查询的效果
#include <bits/stdc++.h>
using namespace std;
#define maxn 2005
struct Line
{
double x;
double yleft,yright;
int flag;
};
struct xjy
{
int left,right;
double yleft,yright;
int cnt;
double once;
double twice;
};
xjy tree[maxn<<2];
Line line[maxn];
double Hash[maxn];
bool cmp(Line a,Line b)
{
return a.x<b.x;
}
void build(int i,int left,int right)
{
tree[i].left=left;
tree[i].right=right;
tree[i].once=0;
tree[i].twice=0;
tree[i].yleft=Hash[left];
tree[i].yright=Hash[right];
if(left+1==right)
return ;
int mid=(left+right)>>1;
build(i<<1,left,mid);
build(i<<1|1,mid,right);
}
void check(int i)
{
if(tree[i].cnt>=2)
{
tree[i].once=tree[i].yright-tree[i].yleft;
tree[i].twice=tree[i].yright-tree[i].yleft;
return ;
}
else if(tree[i].cnt==1)
{
tree[i].once=tree[i].yright-tree[i].yleft;
if(tree[i].left+1==tree[i].right)
tree[i].twice=0;
else
tree[i].twice=tree[i<<1].once+tree[i<<1|1].once;
}
else
{
if(tree[i].left+1==tree[i].right)
tree[i].once=tree[i].twice=0;
else
{
tree[i].once=tree[i<<1].once+tree[i<<1|1].once;
tree[i].twice=tree[i<<1].twice+tree[i<<1|1].twice;
}
}
}
void update(int i,Line a)
{
if(a.yleft==tree[i].yleft&&tree[i].yright==a.yright)
{
tree[i].cnt+=a.flag;
check(i);
//cout << tree[i].left << tree[i].right << endl;
return;
}
if(a.yright<=tree[i<<1].yright)
update(i<<1,a);
else if(a.yleft>=tree[i<<1|1].yleft)
update(i<<1|1,a);
else
{
Line temp=a;
temp.yright=tree[i<<1].yright;
update(i<<1,temp);
temp=a;
temp.yleft=tree[i<<1|1].yleft;
update(i<<1|1,temp);
}
check(i);
//cout << tree[i].left << tree[i].right <<endl;
}
int main()
{
int t;
scanf("%d",&t);
for(int step=1;step<=t;step++)
{
int n;int cnt=1;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
double x1,x2,y1,y2;
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
line[cnt].yleft=y1;
line[cnt].yright=y2;
line[cnt].x=x1;
line[cnt].flag=1;
Hash[cnt++]=y1;
line[cnt].yleft=y1;
line[cnt].yright=y2;
line[cnt].x=x2;
line[cnt].flag=-1;
Hash[cnt++]=y2;
}
sort(line+1,line+cnt,cmp);
sort(Hash+1,Hash+cnt);
build(1,1,cnt-1);
double ans=0;
update(1,line[1]);
for(int i=2;i<cnt;i++)
{
ans+=tree[1].twice*(line[i].x-line[i-1].x);
update(1,line[i]);
}
printf("%.2lf\n",ans);
}
}